ninl.pas 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005
  1. {
  2. Copyright (c) 1998-2007 by Florian Klaempfl
  3. Type checking and register allocation for inline nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ninl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,htypechk,cpuinfo,symtype;
  22. {$i compinnr.inc}
  23. type
  24. tinlinenode = class(tunarynode)
  25. inlinenumber : byte;
  26. constructor create(number : byte;is_const:boolean;l : tnode);virtual;
  27. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  28. procedure ppuwrite(ppufile:tcompilerppufile);override;
  29. function dogetcopy : tnode;override;
  30. function pass_1 : tnode;override;
  31. function pass_typecheck:tnode;override;
  32. function simplify: tnode;override;
  33. function docompare(p: tnode): boolean; override;
  34. { pack and unpack are changed into for-loops by the compiler }
  35. function first_pack_unpack: tnode; virtual;
  36. { All the following routines currently
  37. call compilerprocs, unless they are
  38. overriden in which case, the code
  39. generator handles them.
  40. }
  41. function first_pi: tnode ; virtual;
  42. function first_arctan_real: tnode; virtual;
  43. function first_abs_real: tnode; virtual;
  44. function first_sqr_real: tnode; virtual;
  45. function first_sqrt_real: tnode; virtual;
  46. function first_ln_real: tnode; virtual;
  47. function first_cos_real: tnode; virtual;
  48. function first_sin_real: tnode; virtual;
  49. function first_exp_real: tnode; virtual;
  50. function first_frac_real: tnode; virtual;
  51. function first_round_real: tnode; virtual;
  52. function first_trunc_real: tnode; virtual;
  53. function first_int_real: tnode; virtual;
  54. function first_abs_long: tnode; virtual;
  55. private
  56. function handle_str: tnode;
  57. function handle_reset_rewrite_typed: tnode;
  58. function handle_text_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  59. function handle_typed_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  60. function handle_read_write: tnode;
  61. function handle_val: tnode;
  62. end;
  63. tinlinenodeclass = class of tinlinenode;
  64. var
  65. cinlinenode : tinlinenodeclass;
  66. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  67. implementation
  68. uses
  69. verbose,globals,systems,constexp,
  70. globtype, cutils,
  71. symconst,symdef,symsym,symtable,paramgr,defutil,
  72. pass_1,
  73. ncal,ncon,ncnv,nadd,nld,nbas,nflw,nmem,nmat,nutils,
  74. cgbase,procinfo
  75. ;
  76. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  77. begin
  78. geninlinenode:=cinlinenode.create(number,is_const,l);
  79. end;
  80. {*****************************************************************************
  81. TINLINENODE
  82. *****************************************************************************}
  83. constructor tinlinenode.create(number : byte;is_const:boolean;l : tnode);
  84. begin
  85. inherited create(inlinen,l);
  86. if is_const then
  87. include(flags,nf_inlineconst);
  88. inlinenumber:=number;
  89. end;
  90. constructor tinlinenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  91. begin
  92. inherited ppuload(t,ppufile);
  93. inlinenumber:=ppufile.getbyte;
  94. end;
  95. procedure tinlinenode.ppuwrite(ppufile:tcompilerppufile);
  96. begin
  97. inherited ppuwrite(ppufile);
  98. ppufile.putbyte(inlinenumber);
  99. end;
  100. function tinlinenode.dogetcopy : tnode;
  101. var
  102. n : tinlinenode;
  103. begin
  104. n:=tinlinenode(inherited dogetcopy);
  105. n.inlinenumber:=inlinenumber;
  106. result:=n;
  107. end;
  108. function tinlinenode.handle_str : tnode;
  109. var
  110. lenpara,
  111. fracpara,
  112. newparas,
  113. tmppara,
  114. dest,
  115. source : tcallparanode;
  116. procname: string;
  117. is_real,is_enum : boolean;
  118. rt : aint;
  119. begin
  120. result := cerrornode.create;
  121. { make sure we got at least two parameters (if we got only one, }
  122. { this parameter may not be encapsulated in a callparan) }
  123. if not assigned(left) or
  124. (left.nodetype <> callparan) then
  125. begin
  126. CGMessage1(parser_e_wrong_parameter_size,'Str');
  127. exit;
  128. end;
  129. { get destination string }
  130. dest := tcallparanode(left);
  131. { get source para (number) }
  132. source := dest;
  133. while assigned(source.right) do
  134. source := tcallparanode(source.right);
  135. { destination parameter must be a normal (not a colon) parameter, this
  136. check is needed because str(v:len) also has 2 parameters }
  137. if (source=dest) or
  138. (cpf_is_colon_para in tcallparanode(dest).callparaflags) then
  139. begin
  140. CGMessage1(parser_e_wrong_parameter_size,'Str');
  141. exit;
  142. end;
  143. is_real:=(source.resultdef.typ = floatdef) or is_currency(source.resultdef);
  144. is_enum:=source.left.resultdef.typ=enumdef;
  145. if ((dest.left.resultdef.typ<>stringdef) and
  146. not(is_chararray(dest.left.resultdef))) or
  147. not(is_real or is_enum or
  148. (source.left.resultdef.typ=orddef)) then
  149. begin
  150. CGMessagePos(fileinfo,parser_e_illegal_expression);
  151. exit;
  152. end;
  153. { get len/frac parameters }
  154. lenpara := nil;
  155. fracpara := nil;
  156. if (cpf_is_colon_para in tcallparanode(dest.right).callparaflags) then
  157. begin
  158. lenpara := tcallparanode(dest.right);
  159. { we can let the callnode do the type checking of these parameters too, }
  160. { but then the error messages aren't as nice }
  161. if not is_integer(lenpara.resultdef) then
  162. begin
  163. CGMessagePos1(lenpara.fileinfo,
  164. type_e_integer_expr_expected,lenpara.resultdef.typename);
  165. exit;
  166. end;
  167. if (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  168. begin
  169. { parameters are in reverse order! }
  170. fracpara := lenpara;
  171. lenpara := tcallparanode(lenpara.right);
  172. if not is_real then
  173. begin
  174. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  175. exit
  176. end;
  177. if not is_integer(lenpara.resultdef) then
  178. begin
  179. CGMessagePos1(lenpara.fileinfo,
  180. type_e_integer_expr_expected,lenpara.resultdef.typename);
  181. exit;
  182. end;
  183. end;
  184. end;
  185. { generate the parameter list for the compilerproc }
  186. newparas := dest;
  187. { if we have a float parameter, insert the realtype, len and fracpara parameters }
  188. if is_real then
  189. begin
  190. { insert realtype parameter }
  191. if not is_currency(source.resultdef) then
  192. begin
  193. rt:=ord(tfloatdef(source.left.resultdef).floattype);
  194. newparas.right := ccallparanode.create(cordconstnode.create(
  195. rt,s32inttype,true),newparas.right);
  196. tmppara:=tcallparanode(newparas.right);
  197. end
  198. else
  199. tmppara:=newparas;
  200. { if necessary, insert a fraction parameter }
  201. if not assigned(fracpara) then
  202. begin
  203. tmppara.right := ccallparanode.create(
  204. cordconstnode.create(int64(-1),s32inttype,false),
  205. tmppara.right);
  206. fracpara := tcallparanode(tmppara.right);
  207. end;
  208. { if necessary, insert a length para }
  209. if not assigned(lenpara) then
  210. fracpara.right := ccallparanode.create(
  211. cordconstnode.create(int64(-32767),s32inttype,false),
  212. fracpara.right);
  213. end
  214. else if is_enum then
  215. begin
  216. {Insert a reference to the ord2string index.}
  217. newparas.right:=Ccallparanode.create(
  218. Caddrnode.create_internal(
  219. Crttinode.create(Tenumdef(source.left.resultdef),fullrtti,rdt_normal)
  220. ),
  221. newparas.right);
  222. {Insert a reference to the typinfo.}
  223. newparas.right:=Ccallparanode.create(
  224. Caddrnode.create_internal(
  225. Crttinode.create(Tenumdef(source.left.resultdef),fullrtti,rdt_ord2str)
  226. ),
  227. newparas.right);
  228. {Insert a type conversion from the enumeration to longint.}
  229. source.left:=Ctypeconvnode.create_internal(source.left,s32inttype);
  230. typecheckpass(source.left);
  231. { if necessary, insert a length para }
  232. if not assigned(lenpara) then
  233. Tcallparanode(Tcallparanode(newparas.right).right).right:=
  234. Ccallparanode.create(
  235. cordconstnode.create(int64(-1),s32inttype,false),
  236. Tcallparanode(Tcallparanode(newparas.right).right).right
  237. );
  238. end
  239. else
  240. { for a normal parameter, insert a only length parameter if one is missing }
  241. if not assigned(lenpara) then
  242. newparas.right := ccallparanode.create(cordconstnode.create(int64(-1),s32inttype,false),
  243. newparas.right);
  244. { remove the parameters from the original node so they won't get disposed, }
  245. { since they're reused }
  246. left := nil;
  247. { create procedure name }
  248. if is_chararray(dest.resultdef) then
  249. procname:='fpc_chararray_'
  250. else
  251. procname := 'fpc_' + tstringdef(dest.resultdef).stringtypname+'_';
  252. if is_real then
  253. if is_currency(source.resultdef) then
  254. procname := procname + 'currency'
  255. else
  256. procname := procname + 'float'
  257. else if is_enum then
  258. procname:=procname+'enum'
  259. else
  260. case torddef(source.resultdef).ordtype of
  261. {$ifdef cpu64bitaddr}
  262. u64bit:
  263. procname := procname + 'uint';
  264. {$else}
  265. u32bit:
  266. procname := procname + 'uint';
  267. u64bit:
  268. procname := procname + 'qword';
  269. scurrency,
  270. s64bit:
  271. procname := procname + 'int64';
  272. {$endif}
  273. else
  274. procname := procname + 'sint';
  275. end;
  276. { free the errornode we generated in the beginning }
  277. result.free;
  278. { create the call node, }
  279. result := ccallnode.createintern(procname,newparas);
  280. end;
  281. function tinlinenode.handle_reset_rewrite_typed: tnode;
  282. begin
  283. { since this is a "in_xxxx_typedfile" node, we can be sure we have }
  284. { a typed file as argument and we don't have to check it again (JM) }
  285. { add the recsize parameter }
  286. { note: for some reason, the parameter of intern procedures with only one }
  287. { parameter is gets lifted out of its original tcallparanode (see round }
  288. { line 1306 of ncal.pas), so recreate a tcallparanode here (JM) }
  289. left := ccallparanode.create(cordconstnode.create(
  290. tfiledef(left.resultdef).typedfiledef.size,s32inttype,true),
  291. ccallparanode.create(left,nil));
  292. { create the correct call }
  293. if inlinenumber=in_reset_typedfile then
  294. result := ccallnode.createintern('fpc_reset_typed',left)
  295. else
  296. result := ccallnode.createintern('fpc_rewrite_typed',left);
  297. { make sure left doesn't get disposed, since we use it in the new call }
  298. left := nil;
  299. end;
  300. procedure maybe_convert_to_string(var n: tnode);
  301. begin
  302. { stringconstnodes are arrays of char. It's much more }
  303. { efficient to write a constant string, so convert }
  304. { either to shortstring or ansistring depending on }
  305. { length }
  306. if (n.nodetype=stringconstn) then
  307. if is_chararray(n.resultdef) then
  308. if (tstringconstnode(n).len<=255) then
  309. inserttypeconv(n,cshortstringtype)
  310. else
  311. inserttypeconv(n,cansistringtype)
  312. else if is_widechararray(n.resultdef) then
  313. inserttypeconv(n,cwidestringtype);
  314. end;
  315. function Tinlinenode.handle_text_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  316. {Read(ln)/write(ln) for text files.}
  317. const procprefixes:array[boolean] of string[15]=('fpc_write_text_','fpc_read_text_');
  318. var error_para,is_real,special_handling,found_error,do_read:boolean;
  319. p1:Tnode;
  320. nextpara,
  321. indexpara,
  322. lenpara,
  323. para,
  324. fracpara:Tcallparanode;
  325. temp:Ttempcreatenode;
  326. readfunctype:Tdef;
  327. name:string[31];
  328. begin
  329. para:=Tcallparanode(params);
  330. found_error:=false;
  331. do_read:=inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  332. while assigned(para) do
  333. begin
  334. { is this parameter faulty? }
  335. error_para:=false;
  336. { is this parameter a real? }
  337. is_real:=false;
  338. { type used for the read(), this is used to check
  339. whether a temp is needed for range checking }
  340. readfunctype:=nil;
  341. { can't read/write types }
  342. if para.left.nodetype=typen then
  343. begin
  344. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  345. error_para := true;
  346. end;
  347. { support writeln(procvar) }
  348. if para.left.resultdef.typ=procvardef then
  349. begin
  350. p1:=ccallnode.create_procvar(nil,para.left);
  351. typecheckpass(p1);
  352. para.left:=p1;
  353. end;
  354. if inlinenumber in [in_write_x,in_writeln_x] then
  355. { prefer strings to chararrays }
  356. maybe_convert_to_string(para.left);
  357. case para.left.resultdef.typ of
  358. stringdef :
  359. name:=procprefixes[do_read]+tstringdef(para.left.resultdef).stringtypname;
  360. pointerdef :
  361. begin
  362. if (not is_pchar(para.left.resultdef)) or do_read then
  363. begin
  364. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  365. error_para := true;
  366. end
  367. else
  368. name:=procprefixes[do_read]+'pchar_as_pointer';
  369. end;
  370. floatdef :
  371. begin
  372. is_real:=true;
  373. if Tfloatdef(para.left.resultdef).floattype=s64currency then
  374. name := procprefixes[do_read]+'currency'
  375. else
  376. begin
  377. name := procprefixes[do_read]+'float';
  378. readfunctype:=pbestrealtype^;
  379. end;
  380. end;
  381. enumdef:
  382. begin
  383. name:=procprefixes[do_read]+'enum';
  384. readfunctype:=s32inttype;
  385. end;
  386. orddef :
  387. begin
  388. case Torddef(para.left.resultdef).ordtype of
  389. {$ifdef cpu64bitaddr}
  390. s64bit,
  391. {$endif cpu64bitaddr}
  392. s8bit,
  393. s16bit,
  394. s32bit :
  395. begin
  396. name := procprefixes[do_read]+'sint';
  397. readfunctype:=sinttype;
  398. end;
  399. {$ifdef cpu64bitaddr}
  400. u64bit,
  401. {$endif cpu64bitaddr}
  402. u8bit,
  403. u16bit,
  404. u32bit :
  405. begin
  406. name := procprefixes[do_read]+'uint';
  407. readfunctype:=uinttype;
  408. end;
  409. uchar :
  410. begin
  411. name := procprefixes[do_read]+'char';
  412. readfunctype:=cchartype;
  413. end;
  414. uwidechar :
  415. begin
  416. name := procprefixes[do_read]+'widechar';
  417. readfunctype:=cwidechartype;
  418. end;
  419. {$ifndef cpu64bitaddr}
  420. s64bit :
  421. begin
  422. name := procprefixes[do_read]+'int64';
  423. readfunctype:=s64inttype;
  424. end;
  425. u64bit :
  426. begin
  427. name := procprefixes[do_read]+'qword';
  428. readfunctype:=u64inttype;
  429. end;
  430. {$endif not cpu64bitaddr}
  431. scurrency:
  432. begin
  433. name := procprefixes[do_read]+'currency';
  434. readfunctype:=s64currencytype;
  435. is_real:=true;
  436. end;
  437. pasbool,
  438. bool8bit,
  439. bool16bit,
  440. bool32bit,
  441. bool64bit:
  442. if do_read then
  443. begin
  444. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  445. error_para := true;
  446. end
  447. else
  448. begin
  449. name := procprefixes[do_read]+'boolean';
  450. readfunctype:=booltype;
  451. end
  452. else
  453. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  454. error_para := true;
  455. end;
  456. end;
  457. variantdef :
  458. name:=procprefixes[do_read]+'variant';
  459. arraydef :
  460. begin
  461. if is_chararray(para.left.resultdef) then
  462. name := procprefixes[do_read]+'pchar_as_array'
  463. else
  464. begin
  465. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  466. error_para := true;
  467. end
  468. end
  469. else
  470. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  471. error_para := true;
  472. end;
  473. { check for length/fractional colon para's }
  474. fracpara:=nil;
  475. lenpara:=nil;
  476. indexpara:=nil;
  477. if assigned(para.right) and
  478. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  479. begin
  480. lenpara := tcallparanode(para.right);
  481. if assigned(lenpara.right) and
  482. (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  483. fracpara:=tcallparanode(lenpara.right);
  484. end;
  485. { get the next parameter now already, because we're going }
  486. { to muck around with the pointers }
  487. if assigned(fracpara) then
  488. nextpara := tcallparanode(fracpara.right)
  489. else if assigned(lenpara) then
  490. nextpara := tcallparanode(lenpara.right)
  491. else
  492. nextpara := tcallparanode(para.right);
  493. { check if a fracpara is allowed }
  494. if assigned(fracpara) and not is_real then
  495. begin
  496. CGMessagePos(fracpara.fileinfo,parser_e_illegal_colon_qualifier);
  497. error_para := true;
  498. end
  499. else if assigned(lenpara) and do_read then
  500. begin
  501. { I think this is already filtered out by parsing, but I'm not sure (JM) }
  502. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  503. error_para := true;
  504. end;
  505. { adjust found_error }
  506. found_error := found_error or error_para;
  507. if not error_para then
  508. begin
  509. special_handling:=false;
  510. { create dummy frac/len para's if necessary }
  511. if not do_read then
  512. begin
  513. { difference in default value for floats and the rest :( }
  514. if not is_real then
  515. begin
  516. if not assigned(lenpara) then
  517. lenpara := ccallparanode.create(
  518. cordconstnode.create(0,s32inttype,false),nil)
  519. else
  520. { make sure we don't pass the successive }
  521. { parameters too. We also already have a }
  522. { reference to the next parameter in }
  523. { nextpara }
  524. lenpara.right := nil;
  525. end
  526. else
  527. begin
  528. if not assigned(lenpara) then
  529. lenpara := ccallparanode.create(
  530. cordconstnode.create(int64(-32767),s32inttype,false),nil);
  531. { also create a default fracpara if necessary }
  532. if not assigned(fracpara) then
  533. fracpara := ccallparanode.create(
  534. cordconstnode.create(int64(-1),s32inttype,false),nil);
  535. { add it to the lenpara }
  536. lenpara.right := fracpara;
  537. if not is_currency(para.left.resultdef) then
  538. begin
  539. { and add the realtype para (this also removes the link }
  540. { to any parameters coming after it) }
  541. fracpara.right := ccallparanode.create(
  542. cordconstnode.create(ord(tfloatdef(para.left.resultdef).floattype),
  543. s32inttype,true),nil);
  544. end;
  545. end;
  546. if para.left.resultdef.typ=enumdef then
  547. begin
  548. {To write(ln) an enum we need a some extra parameters.}
  549. {Insert a reference to the ord2string index.}
  550. indexpara:=Ccallparanode.create(
  551. Caddrnode.create_internal(
  552. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_normal)
  553. ),
  554. nil);
  555. {Insert a reference to the typinfo.}
  556. indexpara:=Ccallparanode.create(
  557. Caddrnode.create_internal(
  558. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_ord2str)
  559. ),
  560. indexpara);
  561. {Insert a type conversion to to convert the enum to longint.}
  562. para.left:=Ctypeconvnode.create_internal(para.left,s32inttype);
  563. typecheckpass(para.left);
  564. end;
  565. end
  566. else
  567. begin
  568. {To read(ln) an enum we need a an extra parameter.}
  569. if para.left.resultdef.typ=enumdef then
  570. begin
  571. {Insert a reference to the string2ord index.}
  572. indexpara:=Ccallparanode.create(Caddrnode.create_internal(
  573. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_str2ord)
  574. ),nil);
  575. {Insert a type conversion to to convert the enum to longint.}
  576. para.left:=Ctypeconvnode.create_internal(para.left,s32inttype);
  577. typecheckpass(para.left);
  578. end;
  579. { special handling of reading small numbers, because the helpers }
  580. { expect a longint/card/bestreal var parameter. Use a temp. can't }
  581. { use functions because then the call to FPC_IOCHECK destroys }
  582. { their result before we can store it }
  583. if (readfunctype<>nil) and (para.left.resultdef<>readfunctype) then
  584. special_handling:=true;
  585. end;
  586. if special_handling then
  587. begin
  588. { since we're not going to pass the parameter as var-parameter }
  589. { to the read function, manually check whether the parameter }
  590. { can be used as var-parameter (e.g., whether it isn't a }
  591. { property) }
  592. valid_for_var(para.left,true);
  593. { create the parameter list: the temp ... }
  594. temp := ctempcreatenode.create(readfunctype,readfunctype.size,tt_persistent,false);
  595. addstatement(Tstatementnode(newstatement),temp);
  596. { ... and the file }
  597. p1 := ccallparanode.create(ctemprefnode.create(temp),
  598. filepara.getcopy);
  599. Tcallparanode(Tcallparanode(p1).right).right:=indexpara;
  600. { create the call to the helper }
  601. addstatement(Tstatementnode(newstatement),
  602. ccallnode.createintern(name,tcallparanode(p1)));
  603. { assign the result to the original var (this automatically }
  604. { takes care of range checking) }
  605. addstatement(Tstatementnode(newstatement),
  606. cassignmentnode.create(para.left,
  607. ctemprefnode.create(temp)));
  608. { release the temp location }
  609. addstatement(Tstatementnode(newstatement),ctempdeletenode.create(temp));
  610. { statement of para is used }
  611. para.left := nil;
  612. { free the enclosing tcallparanode, but not the }
  613. { parameters coming after it }
  614. para.right := nil;
  615. para.free;
  616. end
  617. else
  618. { read of non s/u-8/16bit, or a write }
  619. begin
  620. { add the filepara to the current parameter }
  621. para.right := filepara.getcopy;
  622. {Add the lenpara and the indexpara(s) (fracpara and realtype are
  623. already linked with the lenpara if necessary).}
  624. if indexpara=nil then
  625. Tcallparanode(para.right).right:=lenpara
  626. else
  627. begin
  628. if lenpara=nil then
  629. Tcallparanode(para.right).right:=indexpara
  630. else
  631. begin
  632. Tcallparanode(para.right).right:=lenpara;
  633. lenpara.right:=indexpara;
  634. end;
  635. { indexpara.right:=lenpara;}
  636. end;
  637. { in case of writing a chararray, add whether it's }
  638. { zero-based }
  639. if para.left.resultdef.typ=arraydef then
  640. para := ccallparanode.create(cordconstnode.create(
  641. ord(tarraydef(para.left.resultdef).lowrange=0),booltype,false),para);
  642. { create the call statement }
  643. addstatement(Tstatementnode(newstatement),
  644. ccallnode.createintern(name,para));
  645. end
  646. end
  647. else
  648. { error_para = true }
  649. begin
  650. { free the parameter, since it isn't referenced anywhere anymore }
  651. para.right := nil;
  652. para.free;
  653. if assigned(lenpara) then
  654. begin
  655. lenpara.right := nil;
  656. lenpara.free;
  657. end;
  658. if assigned(fracpara) then
  659. begin
  660. fracpara.right := nil;
  661. fracpara.free;
  662. end;
  663. end;
  664. { process next parameter }
  665. para := nextpara;
  666. end;
  667. { if no error, add the write(ln)/read(ln) end calls }
  668. if not found_error then
  669. begin
  670. case inlinenumber of
  671. in_read_x,
  672. in_readstr_x:
  673. name:='fpc_read_end';
  674. in_write_x,
  675. in_writestr_x:
  676. name:='fpc_write_end';
  677. in_readln_x:
  678. name:='fpc_readln_end';
  679. in_writeln_x:
  680. name:='fpc_writeln_end';
  681. end;
  682. addstatement(Tstatementnode(newstatement),ccallnode.createintern(name,filepara));
  683. end;
  684. handle_text_read_write:=found_error;
  685. end;
  686. function Tinlinenode.handle_typed_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  687. {Read/write for typed files.}
  688. const procprefixes:array[boolean] of string[15]=('fpc_typed_write','fpc_typed_read');
  689. procnamesdisplay:array[boolean,boolean] of string[8] = (('Write','Read'),('WriteStr','ReadStr'));
  690. var found_error,do_read,is_rwstr:boolean;
  691. para,nextpara:Tcallparanode;
  692. p1:Tnode;
  693. temp:Ttempcreatenode;
  694. begin
  695. found_error:=false;
  696. para:=Tcallparanode(params);
  697. do_read:=inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  698. is_rwstr := inlinenumber in [in_readstr_x,in_writestr_x];
  699. { add the typesize to the filepara }
  700. if filepara.resultdef.typ=filedef then
  701. filepara.right := ccallparanode.create(cordconstnode.create(
  702. tfiledef(filepara.resultdef).typedfiledef.size,s32inttype,true),nil);
  703. { check for "no parameters" (you need at least one extra para for typed files) }
  704. if not assigned(para) then
  705. begin
  706. CGMessage1(parser_e_wrong_parameter_size,procnamesdisplay[is_rwstr,do_read]);
  707. found_error := true;
  708. end;
  709. { process all parameters }
  710. while assigned(para) do
  711. begin
  712. { check if valid parameter }
  713. if para.left.nodetype=typen then
  714. begin
  715. CGMessagePos(para.left.fileinfo,type_e_cant_read_write_type);
  716. found_error := true;
  717. end;
  718. { support writeln(procvar) }
  719. if (para.left.resultdef.typ=procvardef) then
  720. begin
  721. p1:=ccallnode.create_procvar(nil,para.left);
  722. typecheckpass(p1);
  723. para.left:=p1;
  724. end;
  725. if filepara.resultdef.typ=filedef then
  726. inserttypeconv(para.left,tfiledef(filepara.resultdef).typedfiledef);
  727. if assigned(para.right) and
  728. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  729. begin
  730. CGMessagePos(para.right.fileinfo,parser_e_illegal_colon_qualifier);
  731. { skip all colon para's }
  732. nextpara := tcallparanode(tcallparanode(para.right).right);
  733. while assigned(nextpara) and (cpf_is_colon_para in nextpara.callparaflags) do
  734. nextpara := tcallparanode(nextpara.right);
  735. found_error := true;
  736. end
  737. else
  738. { get next parameter }
  739. nextpara := tcallparanode(para.right);
  740. { When we have a call, we have a problem: you can't pass the }
  741. { result of a call as a formal const parameter. Solution: }
  742. { assign the result to a temp and pass this temp as parameter }
  743. { This is not very efficient, but write(typedfile,x) is }
  744. { already slow by itself anyway (no buffering) (JM) }
  745. { Actually, thge same goes for every non-simple expression }
  746. { (such as an addition, ...) -> put everything but load nodes }
  747. { into temps (JM) }
  748. { of course, this must only be allowed for writes!!! (JM) }
  749. if not(do_read) and (para.left.nodetype <> loadn) then
  750. begin
  751. { create temp for result }
  752. temp := ctempcreatenode.create(para.left.resultdef,
  753. para.left.resultdef.size,tt_persistent,false);
  754. addstatement(Tstatementnode(newstatement),temp);
  755. { assign result to temp }
  756. addstatement(Tstatementnode(newstatement),
  757. cassignmentnode.create(ctemprefnode.create(temp),
  758. para.left));
  759. { replace (reused) paranode with temp }
  760. para.left := ctemprefnode.create(temp);
  761. end;
  762. { add fileparameter }
  763. para.right := filepara.getcopy;
  764. { create call statment }
  765. { since the parameters are in the correct order, we have to insert }
  766. { the statements always at the end of the current block }
  767. addstatement(Tstatementnode(newstatement),
  768. Ccallnode.createintern(procprefixes[do_read],para
  769. ));
  770. { if we used a temp, free it }
  771. if para.left.nodetype = temprefn then
  772. addstatement(Tstatementnode(newstatement),ctempdeletenode.create(temp));
  773. { process next parameter }
  774. para := nextpara;
  775. end;
  776. { free the file parameter }
  777. filepara.free;
  778. handle_typed_read_write:=found_error;
  779. end;
  780. function tinlinenode.handle_read_write: tnode;
  781. var
  782. filepara,
  783. nextpara,
  784. params : tcallparanode;
  785. newstatement : tstatementnode;
  786. newblock : tblocknode;
  787. filetemp : Ttempcreatenode;
  788. name : string[31];
  789. textsym : ttypesym;
  790. is_typed,
  791. do_read,
  792. is_rwstr,
  793. found_error : boolean;
  794. begin
  795. filepara := nil;
  796. is_typed := false;
  797. filetemp := nil;
  798. do_read := inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  799. is_rwstr := inlinenumber in [in_readstr_x,in_writestr_x];
  800. { if we fail, we can quickly exit this way. We must generate something }
  801. { instead of the inline node, because firstpass will bomb with an }
  802. { internalerror if it encounters a read/write }
  803. result := cerrornode.create;
  804. { reverse the parameters (needed to get the colon parameters in the }
  805. { correct order when processing write(ln) }
  806. left := reverseparameters(tcallparanode(left));
  807. if is_rwstr then
  808. begin
  809. filepara := tcallparanode(left);
  810. { needs at least two parameters: source/dest string + min. 1 value }
  811. if not(assigned(filepara)) or
  812. not(assigned(filepara.right)) then
  813. begin
  814. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,'ReadStr/WriteStr');
  815. exit;
  816. end
  817. else if (filepara.resultdef.typ <> stringdef) then
  818. begin
  819. { convert chararray to string, or give an appropriate error message }
  820. { (if you want to optimize to use shortstring, keep in mind that }
  821. { readstr internally always uses ansistring, and to account for }
  822. { chararrays with > 255 characters) }
  823. inserttypeconv(filepara.left,cansistringtype);
  824. filepara.resultdef:=filepara.left.resultdef;
  825. if codegenerror then
  826. exit;
  827. end
  828. end
  829. else if assigned(left) then
  830. begin
  831. { check if we have a file parameter and if yes, what kind it is }
  832. filepara := tcallparanode(left);
  833. if (filepara.resultdef.typ=filedef) then
  834. begin
  835. if (tfiledef(filepara.resultdef).filetyp=ft_untyped) then
  836. begin
  837. CGMessagePos(fileinfo,type_e_no_read_write_for_untyped_file);
  838. exit;
  839. end
  840. else
  841. begin
  842. if (tfiledef(filepara.resultdef).filetyp=ft_typed) then
  843. begin
  844. if (inlinenumber in [in_readln_x,in_writeln_x]) then
  845. begin
  846. CGMessagePos(fileinfo,type_e_no_readln_writeln_for_typed_file);
  847. exit;
  848. end;
  849. is_typed := true;
  850. end
  851. end;
  852. end
  853. else
  854. filepara := nil;
  855. end;
  856. { create a blocknode in which the successive write/read statements will be }
  857. { put, since they belong together. Also create a dummy statement already to }
  858. { make inserting of additional statements easier }
  859. newblock:=internalstatements(newstatement);
  860. { if we don't have a filepara, create one containing the default }
  861. if not assigned(filepara) or
  862. is_rwstr then
  863. begin
  864. { since the input/output variables are threadvars loading them into
  865. a temp once is faster. Create a temp which will hold a pointer to the file }
  866. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  867. addstatement(newstatement,filetemp);
  868. { make sure the resultdef of the temp (and as such of the }
  869. { temprefs coming after it) is set (necessary because the }
  870. { temprefs will be part of the filepara, of which we need }
  871. { the resultdef later on and temprefs can only be }
  872. { typecheckpassed if the resultdef of the temp is known) }
  873. typecheckpass(tnode(filetemp));
  874. if not is_rwstr then
  875. begin
  876. { assign the address of the file to the temp }
  877. if do_read then
  878. name := 'input'
  879. else
  880. name := 'output';
  881. addstatement(newstatement,
  882. cassignmentnode.create(ctemprefnode.create(filetemp),
  883. ccallnode.createintern('fpc_get_'+name,nil)));
  884. end
  885. else
  886. begin
  887. if (do_read) then
  888. name := 'fpc_setupreadstr_'
  889. else
  890. name := 'fpc_setupwritestr_';
  891. name:=name+tstringdef(filepara.resultdef).stringtypname;
  892. { remove the source/destination string parameter from the }
  893. { parameter chain }
  894. left:=filepara.right;
  895. filepara.right:=nil;
  896. { pass the source/destination string to the setup routine, which }
  897. { will store the string's address in the returned textrec }
  898. addstatement(newstatement,
  899. cassignmentnode.create(ctemprefnode.create(filetemp),
  900. ccallnode.createintern(name,filepara)));
  901. end;
  902. { create a new fileparameter as follows: file_type(temp^) }
  903. { (so that we pass the value and not the address of the temp }
  904. { to the read/write routine) }
  905. textsym:=search_system_type('TEXT');
  906. filepara := ccallparanode.create(ctypeconvnode.create_internal(
  907. cderefnode.create(ctemprefnode.create(filetemp)),textsym.typedef),nil);
  908. end
  909. else
  910. { remove filepara from the parameter chain }
  911. begin
  912. left := filepara.right;
  913. filepara.right := nil;
  914. { the file para is a var parameter, but it must be valid already }
  915. set_varstate(filepara.left,vs_readwritten,[vsf_must_be_valid]);
  916. { check if we should make a temp to store the result of a complex }
  917. { expression (better heuristics, anyone?) (JM) }
  918. if (filepara.left.nodetype <> loadn) then
  919. begin
  920. { create a temp which will hold a pointer to the file }
  921. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  922. { add it to the statements }
  923. addstatement(newstatement,filetemp);
  924. { make sure the resultdef of the temp (and as such of the }
  925. { temprefs coming after it) is set (necessary because the }
  926. { temprefs will be part of the filepara, of which we need }
  927. { the resultdef later on and temprefs can only be }
  928. { typecheckpassed if the resultdef of the temp is known) }
  929. typecheckpass(tnode(filetemp));
  930. { assign the address of the file to the temp }
  931. addstatement(newstatement,
  932. cassignmentnode.create(ctemprefnode.create(filetemp),
  933. caddrnode.create_internal(filepara.left)));
  934. typecheckpass(newstatement.left);
  935. { create a new fileparameter as follows: file_type(temp^) }
  936. { (so that we pass the value and not the address of the temp }
  937. { to the read/write routine) }
  938. nextpara := ccallparanode.create(ctypeconvnode.create_internal(
  939. cderefnode.create(ctemprefnode.create(filetemp)),filepara.left.resultdef),nil);
  940. { replace the old file para with the new one }
  941. filepara.left := nil;
  942. filepara.free;
  943. filepara := nextpara;
  944. end;
  945. end;
  946. { the resultdef of the filepara must be set since it's }
  947. { used below }
  948. filepara.get_paratype;
  949. { now, filepara is nowhere referenced anymore, so we can safely dispose it }
  950. { if something goes wrong or at the end of the procedure }
  951. { we're going to reuse the paranodes, so make sure they don't get freed }
  952. { twice }
  953. params:=Tcallparanode(left);
  954. left := nil;
  955. if is_typed then
  956. found_error:=handle_typed_read_write(filepara,Ttertiarynode(params),newstatement)
  957. else
  958. found_error:=handle_text_read_write(filepara,Ttertiarynode(params),newstatement);
  959. { if we found an error, simply delete the generated blocknode }
  960. if found_error then
  961. newblock.free
  962. else
  963. begin
  964. { deallocate the temp for the file para if we used one }
  965. if assigned(filetemp) then
  966. addstatement(newstatement,ctempdeletenode.create(filetemp));
  967. { otherwise return the newly generated block of instructions, }
  968. { but first free the errornode we generated at the beginning }
  969. result.free;
  970. result := newblock
  971. end;
  972. end;
  973. function tinlinenode.handle_val: tnode;
  974. var
  975. procname,
  976. suffix : string[31];
  977. sourcepara,
  978. destpara,
  979. codepara,
  980. sizepara,
  981. newparas : tcallparanode;
  982. orgcode,tc : tnode;
  983. newstatement : tstatementnode;
  984. newblock : tblocknode;
  985. tempcode : ttempcreatenode;
  986. begin
  987. { for easy exiting if something goes wrong }
  988. result := cerrornode.create;
  989. { check the amount of parameters }
  990. if not(assigned(left)) or
  991. not(assigned(tcallparanode(left).right)) then
  992. begin
  993. CGMessage1(parser_e_wrong_parameter_size,'Val');
  994. exit;
  995. end;
  996. { reverse parameters for easier processing }
  997. left := reverseparameters(tcallparanode(left));
  998. { get the parameters }
  999. tempcode := nil;
  1000. orgcode := nil;
  1001. sizepara := nil;
  1002. sourcepara := tcallparanode(left);
  1003. destpara := tcallparanode(sourcepara.right);
  1004. codepara := tcallparanode(destpara.right);
  1005. { check if codepara is valid }
  1006. if assigned(codepara) and
  1007. (
  1008. (codepara.resultdef.typ <> orddef)
  1009. {$ifndef cpu64bitaddr}
  1010. or is_64bitint(codepara.resultdef)
  1011. {$endif not cpu64bitaddr}
  1012. ) then
  1013. begin
  1014. CGMessagePos1(codepara.fileinfo,type_e_integer_expr_expected,codepara.resultdef.typename);
  1015. exit;
  1016. end;
  1017. { check if dest para is valid }
  1018. if not(destpara.resultdef.typ in [orddef,floatdef,enumdef]) then
  1019. begin
  1020. CGMessagePos(destpara.fileinfo,type_e_integer_or_real_expr_expected);
  1021. exit;
  1022. end;
  1023. { we're going to reuse the exisiting para's, so make sure they }
  1024. { won't be disposed }
  1025. left := nil;
  1026. { create the blocknode which will hold the generated statements + }
  1027. { an initial dummy statement }
  1028. newblock:=internalstatements(newstatement);
  1029. { do we need a temp for code? Yes, if no code specified, or if }
  1030. { code is not a 32bit parameter (we already checked whether the }
  1031. { the code para, if specified, was an orddef) }
  1032. if not assigned(codepara) or
  1033. (codepara.resultdef.size<>sinttype.size) then
  1034. begin
  1035. tempcode := ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1036. addstatement(newstatement,tempcode);
  1037. { set the resultdef of the temp (needed to be able to get }
  1038. { the resultdef of the tempref used in the new code para) }
  1039. typecheckpass(tnode(tempcode));
  1040. { create a temp codepara, but save the original code para to }
  1041. { assign the result to later on }
  1042. if assigned(codepara) then
  1043. begin
  1044. orgcode := codepara.left;
  1045. codepara.left := ctemprefnode.create(tempcode);
  1046. end
  1047. else
  1048. codepara := ccallparanode.create(ctemprefnode.create(tempcode),nil);
  1049. { we need its resultdef later on }
  1050. codepara.get_paratype;
  1051. end
  1052. else if (torddef(codepara.resultdef).ordtype = torddef(sinttype).ordtype) then
  1053. { because code is a var parameter, it must match types exactly }
  1054. { however, since it will return values in [0..255], both longints }
  1055. { and cardinals are fine. Since the formal code para type is }
  1056. { longint, insert a typecoversion to longint for cardinal para's }
  1057. begin
  1058. codepara.left := ctypeconvnode.create_internal(codepara.left,sinttype);
  1059. { make it explicit, oterwise you may get a nonsense range }
  1060. { check error if the cardinal already contained a value }
  1061. { > $7fffffff }
  1062. codepara.get_paratype;
  1063. end;
  1064. { create the procedure name }
  1065. procname := 'fpc_val_';
  1066. case destpara.resultdef.typ of
  1067. orddef:
  1068. begin
  1069. case torddef(destpara.resultdef).ordtype of
  1070. {$ifdef cpu64bitaddr}
  1071. s64bit,
  1072. {$endif cpu64bitaddr}
  1073. s8bit,
  1074. s16bit,
  1075. s32bit:
  1076. begin
  1077. suffix := 'sint_';
  1078. { we also need a destsize para in this case }
  1079. sizepara := ccallparanode.create(cordconstnode.create
  1080. (destpara.resultdef.size,s32inttype,true),nil);
  1081. end;
  1082. {$ifdef cpu64bitaddr}
  1083. u64bit,
  1084. {$endif cpu64bitaddr}
  1085. u8bit,
  1086. u16bit,
  1087. u32bit:
  1088. suffix := 'uint_';
  1089. {$ifndef cpu64bitaddr}
  1090. s64bit: suffix := 'int64_';
  1091. u64bit: suffix := 'qword_';
  1092. {$endif not cpu64bitaddr}
  1093. scurrency: suffix := 'currency_';
  1094. else
  1095. internalerror(200304225);
  1096. end;
  1097. end;
  1098. floatdef:
  1099. suffix:='real_';
  1100. enumdef:
  1101. begin
  1102. suffix:='enum_';
  1103. sizepara:=Ccallparanode.create(Caddrnode.create_internal(
  1104. Crttinode.create(Tenumdef(destpara.resultdef),fullrtti,rdt_str2ord)
  1105. ),nil);
  1106. end;
  1107. end;
  1108. procname := procname + suffix;
  1109. { play a trick to have tcallnode handle invalid source parameters: }
  1110. { the shortstring-longint val routine by default }
  1111. if (sourcepara.resultdef.typ = stringdef) then
  1112. procname := procname + tstringdef(sourcepara.resultdef).stringtypname
  1113. { zero-based arrays (of char) can be implicitely converted to ansistring }
  1114. else if is_zero_based_array(sourcepara.resultdef) then
  1115. procname := procname + 'ansistr'
  1116. else
  1117. procname := procname + 'shortstr';
  1118. { set up the correct parameters for the call: the code para... }
  1119. newparas := codepara;
  1120. { and the source para }
  1121. codepara.right := sourcepara;
  1122. { sizepara either contains nil if none is needed (which is ok, since }
  1123. { then the next statement severes any possible links with other paras }
  1124. { that sourcepara may have) or it contains the necessary size para and }
  1125. { its right field is nil }
  1126. sourcepara.right := sizepara;
  1127. { create the call and assign the result to dest (val helpers are functions).
  1128. Use a trick to prevent a type size mismatch warning to be generated by the
  1129. assignment node. First convert implicitly to the resultdef. This will insert
  1130. the range check. The Second conversion is done explicitly to hide the implicit conversion
  1131. for the assignment node and therefor preventing the warning (PFV)
  1132. The implicit conversion is avoided for enums because implicit conversion between
  1133. longint (which is what fpc_val_enum_shortstr returns) and enumerations is not
  1134. possible. (DM).
  1135. The implicit conversion is also avoided for COMP type if it is handled by FPU (x86)
  1136. to prevent warning about automatic type conversion. }
  1137. if (destpara.resultdef.typ=enumdef) or
  1138. ((destpara.resultdef.typ=floatdef) and (tfloatdef(destpara.resultdef).floattype=s64comp))
  1139. then
  1140. tc:=ccallnode.createintern(procname,newparas)
  1141. else
  1142. tc:=ctypeconvnode.create(ccallnode.createintern(procname,newparas),destpara.left.resultdef);
  1143. addstatement(newstatement,cassignmentnode.create(
  1144. destpara.left,ctypeconvnode.create_internal(tc,destpara.left.resultdef)));
  1145. { dispose of the enclosing paranode of the destination }
  1146. destpara.left := nil;
  1147. destpara.right := nil;
  1148. destpara.free;
  1149. { check if we used a temp for code and whether we have to store }
  1150. { it to the real code parameter }
  1151. if assigned(orgcode) then
  1152. addstatement(newstatement,cassignmentnode.create(
  1153. orgcode,
  1154. ctypeconvnode.create_internal(
  1155. ctemprefnode.create(tempcode),orgcode.resultdef)));
  1156. { release the temp if we allocated one }
  1157. if assigned(tempcode) then
  1158. addstatement(newstatement,ctempdeletenode.create(tempcode));
  1159. { free the errornode }
  1160. result.free;
  1161. { and return it }
  1162. result := newblock;
  1163. end;
  1164. {$maxfpuregisters 0}
  1165. function getpi : bestreal;
  1166. begin
  1167. {$ifdef x86}
  1168. { x86 has pi in hardware }
  1169. result:=pi;
  1170. {$else x86}
  1171. {$ifdef cpuextended}
  1172. result:=MathPiExtended.Value;
  1173. {$else cpuextended}
  1174. result:=MathPi.Value;
  1175. {$endif cpuextended}
  1176. {$endif x86}
  1177. end;
  1178. function tinlinenode.simplify: tnode;
  1179. function do_lowhigh(def:tdef) : tnode;
  1180. var
  1181. v : tconstexprint;
  1182. enum : tenumsym;
  1183. hp : tnode;
  1184. begin
  1185. case def.typ of
  1186. orddef:
  1187. begin
  1188. set_varstate(left,vs_read,[]);
  1189. if inlinenumber=in_low_x then
  1190. v:=torddef(def).low
  1191. else
  1192. v:=torddef(def).high;
  1193. hp:=cordconstnode.create(v,def,true);
  1194. typecheckpass(hp);
  1195. do_lowhigh:=hp;
  1196. end;
  1197. enumdef:
  1198. begin
  1199. set_varstate(left,vs_read,[]);
  1200. enum:=tenumsym(tenumdef(def).firstenum);
  1201. v:=tenumdef(def).maxval;
  1202. if inlinenumber=in_high_x then
  1203. while assigned(enum) and (enum.value <> v) do
  1204. enum:=enum.nextenum;
  1205. if not assigned(enum) then
  1206. internalerror(309993)
  1207. else
  1208. hp:=genenumnode(enum);
  1209. do_lowhigh:=hp;
  1210. end;
  1211. else
  1212. internalerror(87);
  1213. end;
  1214. end;
  1215. function getconstrealvalue : bestreal;
  1216. begin
  1217. case left.nodetype of
  1218. ordconstn:
  1219. getconstrealvalue:=tordconstnode(left).value;
  1220. realconstn:
  1221. getconstrealvalue:=trealconstnode(left).value_real;
  1222. else
  1223. internalerror(309992);
  1224. end;
  1225. end;
  1226. procedure setconstrealvalue(r : bestreal);
  1227. begin
  1228. result:=crealconstnode.create(r,pbestrealtype^);
  1229. end;
  1230. function handle_ln_const(r : bestreal) : tnode;
  1231. begin
  1232. if r<=0.0 then
  1233. if (cs_check_range in current_settings.localswitches) or
  1234. (cs_check_overflow in current_settings.localswitches) then
  1235. begin
  1236. result:=crealconstnode.create(0,pbestrealtype^);
  1237. CGMessage(type_e_wrong_math_argument)
  1238. end
  1239. else
  1240. begin
  1241. if r=0.0 then
  1242. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1243. else
  1244. result:=crealconstnode.create(MathNegInf.Value,pbestrealtype^)
  1245. end
  1246. else
  1247. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1248. end;
  1249. function handle_sqrt_const(r : bestreal) : tnode;
  1250. begin
  1251. if r<0.0 then
  1252. if (cs_check_range in current_settings.localswitches) or
  1253. (cs_check_overflow in current_settings.localswitches) then
  1254. begin
  1255. result:=crealconstnode.create(0,pbestrealtype^);
  1256. CGMessage(type_e_wrong_math_argument)
  1257. end
  1258. else
  1259. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1260. else
  1261. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1262. end;
  1263. var
  1264. hp : tnode;
  1265. vl,vl2 : TConstExprInt;
  1266. vr : bestreal;
  1267. checkrange: boolean;
  1268. begin { simplify }
  1269. result:=nil;
  1270. { handle intern constant functions in separate case }
  1271. if nf_inlineconst in flags then
  1272. begin
  1273. { no parameters? }
  1274. if not assigned(left) then
  1275. internalerror(200501231)
  1276. else
  1277. begin
  1278. vl:=0;
  1279. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1280. case left.nodetype of
  1281. realconstn :
  1282. begin
  1283. { Real functions are all handled with internproc below }
  1284. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename)
  1285. end;
  1286. ordconstn :
  1287. vl:=tordconstnode(left).value;
  1288. callparan :
  1289. begin
  1290. { both exists, else it was not generated }
  1291. vl:=tordconstnode(tcallparanode(left).left).value;
  1292. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1293. end;
  1294. else
  1295. CGMessage(parser_e_illegal_expression);
  1296. end;
  1297. case inlinenumber of
  1298. in_const_abs :
  1299. if vl.signed then
  1300. hp:=genintconstnode(abs(vl.svalue))
  1301. else
  1302. hp:=genintconstnode(vl.uvalue);
  1303. in_const_sqr:
  1304. if vl.signed then
  1305. hp:=genintconstnode(sqr(vl.svalue))
  1306. else
  1307. hp:=genintconstnode(sqr(vl.uvalue));
  1308. in_const_odd :
  1309. hp:=cordconstnode.create(qword(odd(int64(vl))),booltype,true);
  1310. in_const_swap_word :
  1311. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  1312. in_const_swap_long :
  1313. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  1314. in_const_swap_qword :
  1315. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resultdef,true);
  1316. in_const_ptr:
  1317. begin
  1318. {Don't construct pointers from negative values.}
  1319. if (vl.signed and (vl.svalue<0)) or (vl2.signed and (vl2.svalue<0)) then
  1320. cgmessage(parser_e_range_check_error);
  1321. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidfarpointertype);
  1322. end
  1323. else
  1324. internalerror(88);
  1325. end;
  1326. end;
  1327. if hp=nil then
  1328. hp:=cerrornode.create;
  1329. result:=hp;
  1330. end
  1331. else
  1332. begin
  1333. case inlinenumber of
  1334. in_lo_long,
  1335. in_hi_long,
  1336. in_lo_qword,
  1337. in_hi_qword,
  1338. in_lo_word,
  1339. in_hi_word :
  1340. begin
  1341. if left.nodetype=ordconstn then
  1342. begin
  1343. case inlinenumber of
  1344. in_lo_word :
  1345. result:=cordconstnode.create(tordconstnode(left).value and $ff,u8inttype,true);
  1346. in_hi_word :
  1347. result:=cordconstnode.create(tordconstnode(left).value shr 8,u8inttype,true);
  1348. in_lo_long :
  1349. result:=cordconstnode.create(tordconstnode(left).value and $ffff,u16inttype,true);
  1350. in_hi_long :
  1351. result:=cordconstnode.create(tordconstnode(left).value shr 16,u16inttype,true);
  1352. in_lo_qword :
  1353. result:=cordconstnode.create(tordconstnode(left).value and $ffffffff,u32inttype,true);
  1354. in_hi_qword :
  1355. result:=cordconstnode.create(tordconstnode(left).value shr 32,u32inttype,true);
  1356. end;
  1357. end;
  1358. end;
  1359. in_ord_x:
  1360. begin
  1361. case left.resultdef.typ of
  1362. orddef :
  1363. begin
  1364. case torddef(left.resultdef).ordtype of
  1365. pasbool,
  1366. uchar:
  1367. begin
  1368. { change to byte() }
  1369. result:=ctypeconvnode.create_internal(left,u8inttype);
  1370. left:=nil;
  1371. end;
  1372. uwidechar :
  1373. begin
  1374. { change to word() }
  1375. result:=ctypeconvnode.create_internal(left,u16inttype);
  1376. left:=nil;
  1377. end;
  1378. bool8bit:
  1379. begin
  1380. { change to shortint() }
  1381. result:=ctypeconvnode.create_internal(left,s8inttype);
  1382. left:=nil;
  1383. end;
  1384. bool16bit :
  1385. begin
  1386. { change to smallint() }
  1387. result:=ctypeconvnode.create_internal(left,s16inttype);
  1388. left:=nil;
  1389. end;
  1390. bool32bit :
  1391. begin
  1392. { change to longint() }
  1393. result:=ctypeconvnode.create_internal(left,s32inttype);
  1394. left:=nil;
  1395. end;
  1396. bool64bit :
  1397. begin
  1398. { change to int64() }
  1399. result:=ctypeconvnode.create_internal(left,s64inttype);
  1400. left:=nil;
  1401. end;
  1402. uvoid :
  1403. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1404. else
  1405. begin
  1406. { all other orddef need no transformation }
  1407. result:=left;
  1408. left:=nil;
  1409. end;
  1410. end;
  1411. end;
  1412. enumdef :
  1413. begin
  1414. result:=ctypeconvnode.create_internal(left,s32inttype);
  1415. left:=nil;
  1416. end;
  1417. pointerdef :
  1418. begin
  1419. if m_mac in current_settings.modeswitches then
  1420. begin
  1421. result:=ctypeconvnode.create_internal(left,ptruinttype);
  1422. left:=nil;
  1423. end
  1424. end;
  1425. end;
  1426. (*
  1427. if (left.nodetype=ordconstn) then
  1428. begin
  1429. result:=cordconstnode.create(
  1430. tordconstnode(left).value,sinttype,true);
  1431. end
  1432. else if (m_mac in current_settings.modeswitches) and
  1433. (left.ndoetype=pointerconstn) then
  1434. result:=cordconstnode.create(
  1435. tpointerconstnode(left).value,ptruinttype,true);
  1436. *)
  1437. end;
  1438. in_chr_byte:
  1439. begin
  1440. { convert to explicit char() }
  1441. result:=ctypeconvnode.create_internal(left,cchartype);
  1442. left:=nil;
  1443. end;
  1444. in_length_x:
  1445. begin
  1446. case left.resultdef.typ of
  1447. stringdef :
  1448. begin
  1449. if (left.nodetype=stringconstn) then
  1450. begin
  1451. result:=cordconstnode.create(
  1452. tstringconstnode(left).len,sinttype,true);
  1453. end;
  1454. end;
  1455. orddef :
  1456. begin
  1457. { length of char is always one }
  1458. if is_char(left.resultdef) or
  1459. is_widechar(left.resultdef) then
  1460. begin
  1461. result:=cordconstnode.create(1,sinttype,false);
  1462. end
  1463. end;
  1464. arraydef :
  1465. begin
  1466. if not is_open_array(left.resultdef) and
  1467. not is_array_of_const(left.resultdef) and
  1468. not is_dynamic_array(left.resultdef) then
  1469. result:=cordconstnode.create(tarraydef(left.resultdef).highrange-
  1470. tarraydef(left.resultdef).lowrange+1,
  1471. sinttype,true);
  1472. end;
  1473. end;
  1474. end;
  1475. in_assigned_x:
  1476. begin
  1477. if is_constnode(tcallparanode(left).left) or
  1478. (tcallparanode(left).left.nodetype = pointerconstn) then
  1479. begin
  1480. { let an add node figure it out }
  1481. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  1482. tcallparanode(left).left := nil;
  1483. end;
  1484. end;
  1485. in_pred_x,
  1486. in_succ_x:
  1487. begin
  1488. { only perform range checking if the result is an enum }
  1489. checkrange:=(resultdef.typ=enumdef);
  1490. if (left.nodetype=ordconstn) then
  1491. begin
  1492. if (inlinenumber=in_succ_x) then
  1493. result:=cordconstnode.create(tordconstnode(left).value+1,left.resultdef,checkrange)
  1494. else
  1495. result:=cordconstnode.create(tordconstnode(left).value-1,left.resultdef,checkrange);
  1496. end;
  1497. end;
  1498. in_low_x,
  1499. in_high_x:
  1500. begin
  1501. case left.resultdef.typ of
  1502. orddef,
  1503. enumdef:
  1504. begin
  1505. result:=do_lowhigh(left.resultdef);
  1506. end;
  1507. setdef:
  1508. begin
  1509. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  1510. end;
  1511. arraydef:
  1512. begin
  1513. if (inlinenumber=in_low_x) then
  1514. begin
  1515. result:=cordconstnode.create(int64(tarraydef(
  1516. left.resultdef).lowrange),tarraydef(left.resultdef).rangedef,true);
  1517. end
  1518. else if not is_open_array(left.resultdef) and
  1519. not is_array_of_const(left.resultdef) and
  1520. not is_dynamic_array(left.resultdef) then
  1521. result:=cordconstnode.create(int64(tarraydef(left.resultdef).highrange),
  1522. tarraydef(left.resultdef).rangedef,true);
  1523. end;
  1524. stringdef:
  1525. begin
  1526. if inlinenumber=in_low_x then
  1527. begin
  1528. result:=cordconstnode.create(0,u8inttype,false);
  1529. end
  1530. else if not is_ansistring(left.resultdef) and
  1531. not is_widestring(left.resultdef) then
  1532. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true)
  1533. end;
  1534. end;
  1535. end;
  1536. in_exp_real :
  1537. begin
  1538. if left.nodetype in [ordconstn,realconstn] then
  1539. begin
  1540. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  1541. if (trealconstnode(result).value_real=MathInf.Value) and
  1542. ((cs_check_range in current_settings.localswitches) or
  1543. (cs_check_overflow in current_settings.localswitches)) then
  1544. begin
  1545. result:=crealconstnode.create(0,pbestrealtype^);
  1546. CGMessage(parser_e_range_check_error);
  1547. end;
  1548. end
  1549. end;
  1550. in_trunc_real :
  1551. begin
  1552. if left.nodetype in [ordconstn,realconstn] then
  1553. begin
  1554. vr:=getconstrealvalue;
  1555. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1556. begin
  1557. CGMessage(parser_e_range_check_error);
  1558. result:=cordconstnode.create(1,s64inttype,false)
  1559. end
  1560. else
  1561. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  1562. end
  1563. end;
  1564. in_round_real :
  1565. begin
  1566. if left.nodetype in [ordconstn,realconstn] then
  1567. begin
  1568. vr:=getconstrealvalue;
  1569. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1570. begin
  1571. CGMessage(parser_e_range_check_error);
  1572. result:=cordconstnode.create(1,s64inttype,false)
  1573. end
  1574. else
  1575. result:=cordconstnode.create(round(vr),s64inttype,true)
  1576. end
  1577. end;
  1578. in_frac_real :
  1579. begin
  1580. if left.nodetype in [ordconstn,realconstn] then
  1581. setconstrealvalue(frac(getconstrealvalue))
  1582. end;
  1583. in_int_real :
  1584. begin
  1585. if left.nodetype in [ordconstn,realconstn] then
  1586. setconstrealvalue(int(getconstrealvalue));
  1587. end;
  1588. in_pi_real :
  1589. begin
  1590. if block_type=bt_const then
  1591. setconstrealvalue(getpi)
  1592. end;
  1593. in_cos_real :
  1594. begin
  1595. if left.nodetype in [ordconstn,realconstn] then
  1596. setconstrealvalue(cos(getconstrealvalue))
  1597. end;
  1598. in_sin_real :
  1599. begin
  1600. if left.nodetype in [ordconstn,realconstn] then
  1601. setconstrealvalue(sin(getconstrealvalue))
  1602. end;
  1603. in_arctan_real :
  1604. begin
  1605. if left.nodetype in [ordconstn,realconstn] then
  1606. setconstrealvalue(arctan(getconstrealvalue))
  1607. end;
  1608. in_abs_real :
  1609. begin
  1610. if left.nodetype in [ordconstn,realconstn] then
  1611. setconstrealvalue(abs(getconstrealvalue))
  1612. end;
  1613. in_abs_long:
  1614. begin
  1615. if left.nodetype=ordconstn then
  1616. begin
  1617. if tordconstnode(left).value<0 then
  1618. result:=cordconstnode.create((-tordconstnode(left).value),s32inttype,false)
  1619. else
  1620. result:=cordconstnode.create((tordconstnode(left).value),s32inttype,false);
  1621. end
  1622. end;
  1623. in_sqr_real :
  1624. begin
  1625. if left.nodetype in [ordconstn,realconstn] then
  1626. setconstrealvalue(sqr(getconstrealvalue))
  1627. end;
  1628. in_sqrt_real :
  1629. begin
  1630. if left.nodetype in [ordconstn,realconstn] then
  1631. begin
  1632. vr:=getconstrealvalue;
  1633. if vr<0.0 then
  1634. result:=handle_sqrt_const(vr)
  1635. else
  1636. setconstrealvalue(sqrt(vr));
  1637. end
  1638. end;
  1639. in_ln_real :
  1640. begin
  1641. if left.nodetype in [ordconstn,realconstn] then
  1642. begin
  1643. vr:=getconstrealvalue;
  1644. if vr<=0.0 then
  1645. result:=handle_ln_const(vr)
  1646. else
  1647. setconstrealvalue(ln(vr));
  1648. end
  1649. end;
  1650. in_assert_x_y :
  1651. begin
  1652. if not(cs_do_assertion in current_settings.localswitches) then
  1653. { we need a valid node, so insert a nothingn }
  1654. result:=cnothingnode.create;
  1655. end;
  1656. end;
  1657. end;
  1658. end;
  1659. function tinlinenode.pass_typecheck:tnode;
  1660. procedure setfloatresultdef;
  1661. begin
  1662. if (left.resultdef.typ=floatdef) and
  1663. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,s128real]) then
  1664. resultdef:=left.resultdef
  1665. else
  1666. begin
  1667. if (left.nodetype <> ordconstn) then
  1668. inserttypeconv(left,pbestrealtype^);
  1669. resultdef:=pbestrealtype^;
  1670. end;
  1671. end;
  1672. procedure handle_pack_unpack;
  1673. var
  1674. source, target, index: tcallparanode;
  1675. unpackedarraydef, packedarraydef: tarraydef;
  1676. tempindex: TConstExprInt;
  1677. begin
  1678. resultdef:=voidtype;
  1679. unpackedarraydef := nil;
  1680. packedarraydef := nil;
  1681. source := tcallparanode(left);
  1682. if (inlinenumber = in_unpack_x_y_z) then
  1683. begin
  1684. target := tcallparanode(source.right);
  1685. index := tcallparanode(target.right);
  1686. { source must be a packed array }
  1687. if not is_packed_array(source.left.resultdef) then
  1688. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.GetTypeName)
  1689. else
  1690. packedarraydef := tarraydef(source.left.resultdef);
  1691. { target can be any kind of array, as long as it's not packed }
  1692. if (target.left.resultdef.typ <> arraydef) or
  1693. is_packed_array(target.left.resultdef) then
  1694. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.GetTypeName)
  1695. else
  1696. unpackedarraydef := tarraydef(target.left.resultdef);
  1697. end
  1698. else
  1699. begin
  1700. index := tcallparanode(source.right);
  1701. target := tcallparanode(index.right);
  1702. { source can be any kind of array, as long as it's not packed }
  1703. if (source.left.resultdef.typ <> arraydef) or
  1704. is_packed_array(source.left.resultdef) then
  1705. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.GetTypeName)
  1706. else
  1707. unpackedarraydef := tarraydef(source.left.resultdef);
  1708. { target must be a packed array }
  1709. if not is_packed_array(target.left.resultdef) then
  1710. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.GetTypeName)
  1711. else
  1712. packedarraydef := tarraydef(target.left.resultdef);
  1713. end;
  1714. if assigned(unpackedarraydef) then
  1715. begin
  1716. { index must be compatible with the unpacked array's indextype }
  1717. inserttypeconv(index.left,unpackedarraydef.rangedef);
  1718. { range check at compile time if possible }
  1719. if assigned(packedarraydef) and
  1720. (index.left.nodetype = ordconstn) and
  1721. not is_special_array(unpackedarraydef) then
  1722. begin
  1723. testrange(unpackedarraydef,tordconstnode(index.left).value,false);
  1724. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  1725. testrange(unpackedarraydef,tempindex,false);
  1726. end;
  1727. end;
  1728. { source array is read and must be valid }
  1729. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  1730. { target array is written }
  1731. valid_for_assignment(target.left,true);
  1732. set_varstate(target.left,vs_written,[]);
  1733. { index in the unpacked array is read and must be valid }
  1734. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  1735. { if the size of the arrays is 0 (array of empty records), }
  1736. { do nothing }
  1737. if (source.resultdef.size = 0) then
  1738. result:=cnothingnode.create;
  1739. end;
  1740. var
  1741. hightree,
  1742. hp : tnode;
  1743. begin
  1744. result:=nil;
  1745. { when handling writeln "left" contains no valid address }
  1746. if assigned(left) then
  1747. begin
  1748. if left.nodetype=callparan then
  1749. tcallparanode(left).get_paratype
  1750. else
  1751. typecheckpass(left);
  1752. end;
  1753. if not(nf_inlineconst in flags) then
  1754. begin
  1755. case inlinenumber of
  1756. in_lo_long,
  1757. in_hi_long,
  1758. in_lo_qword,
  1759. in_hi_qword,
  1760. in_lo_word,
  1761. in_hi_word :
  1762. begin
  1763. { give warning for incompatibility with tp and delphi }
  1764. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  1765. ((m_tp7 in current_settings.modeswitches) or
  1766. (m_delphi in current_settings.modeswitches)) then
  1767. CGMessage(type_w_maybe_wrong_hi_lo);
  1768. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1769. if not is_integer(left.resultdef) then
  1770. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  1771. case inlinenumber of
  1772. in_lo_word,
  1773. in_hi_word :
  1774. resultdef:=u8inttype;
  1775. in_lo_long,
  1776. in_hi_long :
  1777. resultdef:=u16inttype;
  1778. in_lo_qword,
  1779. in_hi_qword :
  1780. resultdef:=u32inttype;
  1781. end;
  1782. end;
  1783. in_sizeof_x:
  1784. begin
  1785. { the constant evaluation of in_sizeof_x happens in pexpr where possible }
  1786. set_varstate(left,vs_read,[]);
  1787. if paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  1788. begin
  1789. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1790. if assigned(hightree) then
  1791. begin
  1792. hp:=caddnode.create(addn,hightree,
  1793. cordconstnode.create(1,sinttype,false));
  1794. if (left.resultdef.typ=arraydef) then
  1795. if not is_packed_array(tarraydef(left.resultdef)) then
  1796. begin
  1797. if (tarraydef(left.resultdef).elesize<>1) then
  1798. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1799. left.resultdef).elesize,sinttype,true));
  1800. end
  1801. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  1802. begin
  1803. { no packed open array support yet }
  1804. if (hp.nodetype <> ordconstn) then
  1805. internalerror(2006081511);
  1806. hp.free;
  1807. hp := cordconstnode.create(left.resultdef.size,sinttype,true);
  1808. {
  1809. hp:=
  1810. ctypeconvnode.create_explicit(sinttype,
  1811. cmoddivnode.create(divn,
  1812. caddnode.create(addn,
  1813. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1814. left.resultdef).elepackedbitsize,s64inttype,true)),
  1815. cordconstnode.create(a,s64inttype,true)),
  1816. cordconstnode.create(8,s64inttype,true)),
  1817. sinttype);
  1818. }
  1819. end;
  1820. result:=hp;
  1821. end;
  1822. end
  1823. else
  1824. resultdef:=sinttype;
  1825. end;
  1826. in_typeof_x:
  1827. begin
  1828. set_varstate(left,vs_read,[]);
  1829. resultdef:=voidpointertype;
  1830. end;
  1831. in_ord_x:
  1832. begin
  1833. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1834. case left.resultdef.typ of
  1835. orddef,
  1836. enumdef :
  1837. ;
  1838. pointerdef :
  1839. begin
  1840. if not(m_mac in current_settings.modeswitches) then
  1841. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1842. end
  1843. else
  1844. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1845. end;
  1846. end;
  1847. in_chr_byte:
  1848. begin
  1849. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1850. end;
  1851. in_length_x:
  1852. begin
  1853. if ((left.resultdef.typ=arraydef) and
  1854. (not is_special_array(left.resultdef) or
  1855. is_open_array(left.resultdef))) or
  1856. (left.resultdef.typ=orddef) then
  1857. set_varstate(left,vs_read,[])
  1858. else
  1859. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1860. case left.resultdef.typ of
  1861. variantdef:
  1862. begin
  1863. inserttypeconv(left,cansistringtype);
  1864. end;
  1865. stringdef :
  1866. begin
  1867. { we don't need string convertions here, }
  1868. { except if from widestring to ansistring }
  1869. { and vice versa (that can change the }
  1870. { length) }
  1871. if (left.nodetype=typeconvn) and
  1872. (ttypeconvnode(left).left.resultdef.typ=stringdef) and
  1873. not(is_widestring(left.resultdef) xor
  1874. is_widestring(ttypeconvnode(left).left.resultdef)) then
  1875. begin
  1876. hp:=ttypeconvnode(left).left;
  1877. ttypeconvnode(left).left:=nil;
  1878. left.free;
  1879. left:=hp;
  1880. end;
  1881. end;
  1882. orddef :
  1883. begin
  1884. { will be handled in simplify }
  1885. if not is_char(left.resultdef) and
  1886. not is_widechar(left.resultdef) then
  1887. CGMessage(type_e_mismatch);
  1888. end;
  1889. pointerdef :
  1890. begin
  1891. if is_pchar(left.resultdef) then
  1892. begin
  1893. hp := ccallparanode.create(left,nil);
  1894. result := ccallnode.createintern('fpc_pchar_length',hp);
  1895. { make sure the left node doesn't get disposed, since it's }
  1896. { reused in the new node (JM) }
  1897. left:=nil;
  1898. exit;
  1899. end
  1900. else if is_pwidechar(left.resultdef) then
  1901. begin
  1902. hp := ccallparanode.create(left,nil);
  1903. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  1904. { make sure the left node doesn't get disposed, since it's }
  1905. { reused in the new node (JM) }
  1906. left:=nil;
  1907. exit;
  1908. end
  1909. else
  1910. CGMessage(type_e_mismatch);
  1911. end;
  1912. arraydef :
  1913. begin
  1914. if is_open_array(left.resultdef) or
  1915. is_array_of_const(left.resultdef) then
  1916. begin
  1917. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1918. if assigned(hightree) then
  1919. result:=caddnode.create(addn,hightree,
  1920. cordconstnode.create(1,sinttype,false));
  1921. exit;
  1922. end
  1923. else if is_dynamic_array(left.resultdef) then
  1924. begin
  1925. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1926. result := ccallnode.createintern('fpc_dynarray_length',hp);
  1927. { make sure the left node doesn't get disposed, since it's }
  1928. { reused in the new node (JM) }
  1929. left:=nil;
  1930. exit;
  1931. end
  1932. else
  1933. begin
  1934. { will be handled in simplify }
  1935. end;
  1936. end
  1937. else
  1938. CGMessage(type_e_mismatch);
  1939. end;
  1940. { shortstring return an 8 bit value as the length
  1941. is the first byte of the string }
  1942. if is_shortstring(left.resultdef) then
  1943. resultdef:=u8inttype
  1944. else
  1945. resultdef:=sinttype;
  1946. end;
  1947. in_typeinfo_x:
  1948. begin
  1949. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1950. resultdef:=voidpointertype;
  1951. end;
  1952. in_assigned_x:
  1953. begin
  1954. { the parser has already made sure the expression is valid }
  1955. { there could be a procvar, which is 2*sizeof(pointer), while we }
  1956. { must only check the first pointer -> can't just convert to an }
  1957. { add node in all cases }
  1958. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  1959. resultdef:=booltype;
  1960. end;
  1961. in_ofs_x :
  1962. internalerror(2000101001);
  1963. in_seg_x :
  1964. begin
  1965. set_varstate(left,vs_read,[]);
  1966. result:=cordconstnode.create(0,s32inttype,false);
  1967. end;
  1968. in_pred_x,
  1969. in_succ_x:
  1970. begin
  1971. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1972. resultdef:=left.resultdef;
  1973. if not is_ordinal(resultdef) then
  1974. CGMessage(type_e_ordinal_expr_expected)
  1975. else
  1976. begin
  1977. if (resultdef.typ=enumdef) and
  1978. (tenumdef(resultdef).has_jumps) and
  1979. not(m_delphi in current_settings.modeswitches) then
  1980. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  1981. end;
  1982. end;
  1983. in_initialize_x,
  1984. in_finalize_x,
  1985. in_setlength_x:
  1986. begin
  1987. { inlined from pinline }
  1988. internalerror(200204231);
  1989. end;
  1990. in_inc_x,
  1991. in_dec_x:
  1992. begin
  1993. resultdef:=voidtype;
  1994. if assigned(left) then
  1995. begin
  1996. { first param must be var }
  1997. valid_for_var(tcallparanode(left).left,true);
  1998. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1999. if (left.resultdef.typ in [enumdef,pointerdef]) or
  2000. is_ordinal(left.resultdef) or
  2001. is_currency(left.resultdef) then
  2002. begin
  2003. { value of left gets changed -> must be unique }
  2004. set_unique(tcallparanode(left).left);
  2005. { two paras ? }
  2006. if assigned(tcallparanode(left).right) then
  2007. begin
  2008. if is_integer(tcallparanode(left).right.resultdef) then
  2009. begin
  2010. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2011. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  2012. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2013. { should be handled in the parser (JM) }
  2014. internalerror(2006020901);
  2015. end
  2016. else
  2017. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  2018. end;
  2019. end
  2020. else
  2021. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  2022. end
  2023. else
  2024. CGMessagePos(fileinfo,type_e_mismatch);
  2025. end;
  2026. in_read_x,
  2027. in_readln_x,
  2028. in_readstr_x,
  2029. in_write_x,
  2030. in_writeln_x,
  2031. in_writestr_x :
  2032. begin
  2033. result := handle_read_write;
  2034. end;
  2035. in_settextbuf_file_x :
  2036. begin
  2037. resultdef:=voidtype;
  2038. { now we know the type of buffer }
  2039. hp:=ccallparanode.create(cordconstnode.create(
  2040. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  2041. result:=ccallnode.createintern('SETTEXTBUF',hp);
  2042. left:=nil;
  2043. end;
  2044. { the firstpass of the arg has been done in firstcalln ? }
  2045. in_reset_typedfile,
  2046. in_rewrite_typedfile :
  2047. begin
  2048. result := handle_reset_rewrite_typed;
  2049. end;
  2050. in_str_x_string :
  2051. begin
  2052. result:=handle_str;
  2053. end;
  2054. in_val_x :
  2055. begin
  2056. result:=handle_val;
  2057. end;
  2058. in_include_x_y,
  2059. in_exclude_x_y:
  2060. begin
  2061. resultdef:=voidtype;
  2062. { the parser already checks whether we have two (and exactly two) }
  2063. { parameters (JM) }
  2064. { first param must be var }
  2065. valid_for_var(tcallparanode(left).left,true);
  2066. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2067. { check type }
  2068. if (left.resultdef.typ=setdef) then
  2069. begin
  2070. { insert a type conversion }
  2071. { to the type of the set elements }
  2072. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2073. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  2074. tsetdef(left.resultdef).elementdef);
  2075. end
  2076. else
  2077. CGMessage(type_e_mismatch);
  2078. end;
  2079. in_pack_x_y_z,
  2080. in_unpack_x_y_z :
  2081. begin
  2082. handle_pack_unpack;
  2083. end;
  2084. in_slice_x:
  2085. begin
  2086. result:=nil;
  2087. resultdef:=tcallparanode(left).left.resultdef;
  2088. if (resultdef.typ <> arraydef) then
  2089. CGMessagePos(left.fileinfo,type_e_mismatch)
  2090. else if is_packed_array(resultdef) then
  2091. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  2092. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  2093. CGMessagePos1(tcallparanode(left).right.fileinfo,
  2094. type_e_integer_expr_expected,
  2095. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  2096. end;
  2097. in_low_x,
  2098. in_high_x:
  2099. begin
  2100. case left.resultdef.typ of
  2101. orddef,
  2102. enumdef,
  2103. setdef:
  2104. ;
  2105. arraydef:
  2106. begin
  2107. if (inlinenumber=in_low_x) then
  2108. set_varstate(left,vs_read,[])
  2109. else
  2110. begin
  2111. if is_open_array(left.resultdef) or
  2112. is_array_of_const(left.resultdef) then
  2113. begin
  2114. set_varstate(left,vs_read,[]);
  2115. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2116. end
  2117. else
  2118. if is_dynamic_array(left.resultdef) then
  2119. begin
  2120. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2121. { can't use inserttypeconv because we need }
  2122. { an explicit type conversion (JM) }
  2123. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  2124. result := ccallnode.createintern('fpc_dynarray_high',hp);
  2125. { make sure the left node doesn't get disposed, since it's }
  2126. { reused in the new node (JM) }
  2127. left:=nil;
  2128. end
  2129. else
  2130. begin
  2131. set_varstate(left,vs_read,[]);
  2132. end;
  2133. end;
  2134. end;
  2135. stringdef:
  2136. begin
  2137. if inlinenumber=in_low_x then
  2138. begin
  2139. set_varstate(left,vs_read,[]);
  2140. end
  2141. else
  2142. begin
  2143. if is_open_string(left.resultdef) then
  2144. begin
  2145. set_varstate(left,vs_read,[]);
  2146. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  2147. end
  2148. else if is_ansistring(left.resultdef) or
  2149. is_widestring(left.resultdef) then
  2150. CGMessage(type_e_mismatch)
  2151. end;
  2152. end;
  2153. else
  2154. CGMessage(type_e_mismatch);
  2155. end;
  2156. end;
  2157. in_exp_real,
  2158. in_frac_real,
  2159. in_int_real,
  2160. in_cos_real,
  2161. in_sin_real,
  2162. in_arctan_real,
  2163. in_abs_real,
  2164. in_ln_real :
  2165. begin
  2166. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2167. { converting an int64 to double on platforms without }
  2168. { extended can cause precision loss }
  2169. if not(left.nodetype in [ordconstn,realconstn]) then
  2170. inserttypeconv(left,pbestrealtype^);
  2171. resultdef:=pbestrealtype^;
  2172. end;
  2173. in_trunc_real,
  2174. in_round_real :
  2175. begin
  2176. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2177. { for direct float rounding, no best real type cast should be necessary }
  2178. if not((left.resultdef.typ=floatdef) and
  2179. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,s128real])) and
  2180. { converting an int64 to double on platforms without }
  2181. { extended can cause precision loss }
  2182. not(left.nodetype in [ordconstn,realconstn]) then
  2183. inserttypeconv(left,pbestrealtype^);
  2184. resultdef:=s64inttype;
  2185. end;
  2186. in_pi_real :
  2187. begin
  2188. resultdef:=pbestrealtype^;
  2189. end;
  2190. in_abs_long:
  2191. begin
  2192. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2193. inserttypeconv(left,s32inttype);
  2194. resultdef:=s32inttype;
  2195. end;
  2196. in_sqr_real,
  2197. in_sqrt_real :
  2198. begin
  2199. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2200. setfloatresultdef;
  2201. end;
  2202. {$ifdef SUPPORT_MMX}
  2203. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2204. begin
  2205. end;
  2206. {$endif SUPPORT_MMX}
  2207. {$ifdef SUPPORT_UNALIGNED}
  2208. in_unaligned_x:
  2209. begin
  2210. resultdef:=left.resultdef;
  2211. end;
  2212. {$endif SUPPORT_UNALIGNED}
  2213. in_assert_x_y :
  2214. begin
  2215. resultdef:=voidtype;
  2216. if assigned(left) then
  2217. begin
  2218. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2219. { check type }
  2220. if is_boolean(left.resultdef) then
  2221. begin
  2222. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2223. { must always be a string }
  2224. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  2225. end
  2226. else
  2227. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  2228. end
  2229. else
  2230. CGMessage(type_e_mismatch);
  2231. if (cs_do_assertion in current_settings.localswitches) then
  2232. include(current_procinfo.flags,pi_do_call);
  2233. end;
  2234. in_prefetch_var,
  2235. in_get_frame,
  2236. in_get_caller_frame,
  2237. in_get_caller_addr:
  2238. begin
  2239. resultdef:=voidpointertype;
  2240. end;
  2241. else
  2242. internalerror(8);
  2243. end;
  2244. end;
  2245. if not assigned(result) and not
  2246. codegenerror then
  2247. result:=simplify;
  2248. end;
  2249. function tinlinenode.pass_1 : tnode;
  2250. var
  2251. hp,hpp,resultnode : tnode;
  2252. shiftconst: longint;
  2253. tempnode: ttempcreatenode;
  2254. newstatement: tstatementnode;
  2255. newblock: tblocknode;
  2256. begin
  2257. result:=nil;
  2258. { if we handle writeln; left contains no valid address }
  2259. if assigned(left) then
  2260. begin
  2261. if left.nodetype=callparan then
  2262. tcallparanode(left).firstcallparan
  2263. else
  2264. firstpass(left);
  2265. end;
  2266. { intern const should already be handled }
  2267. if nf_inlineconst in flags then
  2268. internalerror(200104044);
  2269. case inlinenumber of
  2270. in_lo_qword,
  2271. in_hi_qword,
  2272. in_lo_long,
  2273. in_hi_long,
  2274. in_lo_word,
  2275. in_hi_word:
  2276. begin
  2277. shiftconst := 0;
  2278. case inlinenumber of
  2279. in_hi_qword:
  2280. shiftconst := 32;
  2281. in_hi_long:
  2282. shiftconst := 16;
  2283. in_hi_word:
  2284. shiftconst := 8;
  2285. end;
  2286. if shiftconst <> 0 then
  2287. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  2288. cordconstnode.create(shiftconst,u32inttype,false)),resultdef)
  2289. else
  2290. result := ctypeconvnode.create_internal(left,resultdef);
  2291. left := nil;
  2292. firstpass(result);
  2293. end;
  2294. in_sizeof_x:
  2295. begin
  2296. expectloc:=LOC_REGISTER;
  2297. end;
  2298. in_typeof_x:
  2299. begin
  2300. expectloc:=LOC_REGISTER;
  2301. end;
  2302. in_length_x:
  2303. begin
  2304. if is_shortstring(left.resultdef) then
  2305. expectloc:=left.expectloc
  2306. else
  2307. begin
  2308. { ansi/wide string }
  2309. expectloc:=LOC_REGISTER;
  2310. end;
  2311. end;
  2312. in_typeinfo_x:
  2313. begin
  2314. expectloc:=LOC_REGISTER;
  2315. end;
  2316. in_assigned_x:
  2317. begin
  2318. expectloc := LOC_JUMP;
  2319. end;
  2320. in_pred_x,
  2321. in_succ_x:
  2322. begin
  2323. expectloc:=LOC_REGISTER;
  2324. end;
  2325. in_setlength_x,
  2326. in_initialize_x,
  2327. in_finalize_x:
  2328. begin
  2329. expectloc:=LOC_VOID;
  2330. end;
  2331. in_inc_x,
  2332. in_dec_x:
  2333. begin
  2334. expectloc:=LOC_VOID;
  2335. { range/overflow checking doesn't work properly }
  2336. { with the inc/dec code that's generated (JM) }
  2337. if (current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  2338. { No overflow check for pointer operations, because inc(pointer,-1) will always
  2339. trigger an overflow. For uint32 it works because then the operation is done
  2340. in 64bit. Range checking is not applicable to pointers either }
  2341. (tcallparanode(left).left.resultdef.typ<>pointerdef) then
  2342. { convert to simple add (JM) }
  2343. begin
  2344. newblock := internalstatements(newstatement);
  2345. { extra parameter? }
  2346. if assigned(tcallparanode(left).right) then
  2347. begin
  2348. { Yes, use for add node }
  2349. hpp := tcallparanode(tcallparanode(left).right).left;
  2350. tcallparanode(tcallparanode(left).right).left := nil;
  2351. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2352. CGMessage(parser_e_illegal_expression);
  2353. end
  2354. else
  2355. begin
  2356. { no, create constant 1 }
  2357. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  2358. end;
  2359. typecheckpass(hpp);
  2360. if not((hpp.resultdef.typ=orddef) and
  2361. {$ifndef cpu64bitaddr}
  2362. (torddef(hpp.resultdef).ordtype<>u32bit)) then
  2363. {$else not cpu64bitaddr}
  2364. (torddef(hpp.resultdef).ordtype<>u64bit)) then
  2365. {$endif not cpu64bitaddr}
  2366. inserttypeconv_internal(hpp,sinttype);
  2367. { make sure we don't call functions part of the left node twice (and generally }
  2368. { optimize the code generation) }
  2369. if node_complexity(tcallparanode(left).left) > 1 then
  2370. begin
  2371. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2372. addstatement(newstatement,tempnode);
  2373. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2374. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  2375. hp := cderefnode.create(ctemprefnode.create(tempnode));
  2376. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  2377. end
  2378. else
  2379. begin
  2380. hp := tcallparanode(left).left.getcopy;
  2381. tempnode := nil;
  2382. end;
  2383. resultnode := hp.getcopy;
  2384. { avoid type errors from the addn/subn }
  2385. if not is_integer(resultnode.resultdef) then
  2386. begin
  2387. inserttypeconv_internal(hp,sinttype);
  2388. inserttypeconv_internal(hpp,sinttype);
  2389. end;
  2390. { addition/substraction depending on inc/dec }
  2391. if inlinenumber = in_inc_x then
  2392. hpp := caddnode.create(addn,hp,hpp)
  2393. else
  2394. hpp := caddnode.create(subn,hp,hpp);
  2395. { assign result of addition }
  2396. if not(is_integer(resultnode.resultdef)) then
  2397. inserttypeconv(hpp,torddef.create(
  2398. {$ifdef cpu64bitaddr}
  2399. s64bit,
  2400. {$else cpu64bitaddr}
  2401. s32bit,
  2402. {$endif cpu64bitaddr}
  2403. get_min_value(resultnode.resultdef),
  2404. get_max_value(resultnode.resultdef)))
  2405. else
  2406. inserttypeconv(hpp,resultnode.resultdef);
  2407. { avoid any possible warnings }
  2408. inserttypeconv_internal(hpp,resultnode.resultdef);
  2409. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  2410. { deallocate the temp }
  2411. if assigned(tempnode) then
  2412. addstatement(newstatement,ctempdeletenode.create(tempnode));
  2413. { firstpass it }
  2414. firstpass(newblock);
  2415. { return new node }
  2416. result := newblock;
  2417. end;
  2418. end;
  2419. in_include_x_y,
  2420. in_exclude_x_y:
  2421. begin
  2422. expectloc:=LOC_VOID;
  2423. end;
  2424. in_pack_x_y_z,
  2425. in_unpack_x_y_z:
  2426. begin
  2427. result:=first_pack_unpack;
  2428. end;
  2429. in_exp_real:
  2430. begin
  2431. result:= first_exp_real;
  2432. end;
  2433. in_round_real:
  2434. begin
  2435. result:= first_round_real;
  2436. end;
  2437. in_trunc_real:
  2438. begin
  2439. result:= first_trunc_real;
  2440. end;
  2441. in_int_real:
  2442. begin
  2443. result:= first_int_real;
  2444. end;
  2445. in_frac_real:
  2446. begin
  2447. result:= first_frac_real;
  2448. end;
  2449. in_cos_real:
  2450. begin
  2451. result:= first_cos_real;
  2452. end;
  2453. in_sin_real:
  2454. begin
  2455. result := first_sin_real;
  2456. end;
  2457. in_arctan_real:
  2458. begin
  2459. result := first_arctan_real;
  2460. end;
  2461. in_pi_real :
  2462. begin
  2463. result := first_pi;
  2464. end;
  2465. in_abs_real:
  2466. begin
  2467. result := first_abs_real;
  2468. end;
  2469. in_abs_long:
  2470. begin
  2471. result := first_abs_long;
  2472. end;
  2473. in_sqr_real:
  2474. begin
  2475. result := first_sqr_real;
  2476. end;
  2477. in_sqrt_real:
  2478. begin
  2479. result := first_sqrt_real;
  2480. end;
  2481. in_ln_real:
  2482. begin
  2483. result := first_ln_real;
  2484. end;
  2485. {$ifdef SUPPORT_MMX}
  2486. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2487. begin
  2488. end;
  2489. {$endif SUPPORT_MMX}
  2490. in_assert_x_y :
  2491. begin
  2492. expectloc:=LOC_VOID;
  2493. end;
  2494. in_low_x,
  2495. in_high_x:
  2496. internalerror(200104047);
  2497. in_slice_x:
  2498. internalerror(2005101501);
  2499. in_ord_x,
  2500. in_chr_byte:
  2501. begin
  2502. { should not happend as it's converted to typeconv }
  2503. internalerror(200104045);
  2504. end;
  2505. in_ofs_x :
  2506. internalerror(2000101001);
  2507. in_seg_x :
  2508. internalerror(200104046);
  2509. in_settextbuf_file_x,
  2510. in_reset_typedfile,
  2511. in_rewrite_typedfile,
  2512. in_str_x_string,
  2513. in_val_x,
  2514. in_read_x,
  2515. in_readln_x,
  2516. in_write_x,
  2517. in_writeln_x :
  2518. begin
  2519. { should be handled by pass_typecheck }
  2520. internalerror(200108234);
  2521. end;
  2522. in_get_frame:
  2523. begin
  2524. include(current_procinfo.flags,pi_needs_stackframe);
  2525. expectloc:=LOC_CREGISTER;
  2526. end;
  2527. in_get_caller_frame:
  2528. begin
  2529. expectloc:=LOC_REGISTER;
  2530. end;
  2531. in_get_caller_addr:
  2532. begin
  2533. expectloc:=LOC_REGISTER;
  2534. end;
  2535. in_prefetch_var:
  2536. begin
  2537. expectloc:=LOC_VOID;
  2538. end;
  2539. {$ifdef SUPPORT_UNALIGNED}
  2540. in_unaligned_x:
  2541. begin
  2542. expectloc:=tcallparanode(left).left.expectloc;
  2543. end;
  2544. {$endif SUPPORT_UNALIGNED}
  2545. else
  2546. internalerror(89);
  2547. end;
  2548. end;
  2549. {$maxfpuregisters default}
  2550. function tinlinenode.docompare(p: tnode): boolean;
  2551. begin
  2552. docompare :=
  2553. inherited docompare(p) and
  2554. (inlinenumber = tinlinenode(p).inlinenumber);
  2555. end;
  2556. function tinlinenode.first_pi : tnode;
  2557. begin
  2558. result:=crealconstnode.create(getpi,pbestrealtype^);
  2559. end;
  2560. function tinlinenode.first_arctan_real : tnode;
  2561. begin
  2562. { create the call to the helper }
  2563. { on entry left node contains the parameter }
  2564. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  2565. ccallparanode.create(left,nil));
  2566. left := nil;
  2567. end;
  2568. function tinlinenode.first_abs_real : tnode;
  2569. begin
  2570. { create the call to the helper }
  2571. { on entry left node contains the parameter }
  2572. first_abs_real := ccallnode.createintern('fpc_abs_real',
  2573. ccallparanode.create(left,nil));
  2574. left := nil;
  2575. end;
  2576. function tinlinenode.first_sqr_real : tnode;
  2577. begin
  2578. { create the call to the helper }
  2579. { on entry left node contains the parameter }
  2580. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  2581. ccallparanode.create(left,nil)),resultdef);
  2582. left := nil;
  2583. end;
  2584. function tinlinenode.first_sqrt_real : tnode;
  2585. begin
  2586. { create the call to the helper }
  2587. { on entry left node contains the parameter }
  2588. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  2589. ccallparanode.create(left,nil)),resultdef);
  2590. left := nil;
  2591. end;
  2592. function tinlinenode.first_ln_real : tnode;
  2593. begin
  2594. { create the call to the helper }
  2595. { on entry left node contains the parameter }
  2596. first_ln_real := ccallnode.createintern('fpc_ln_real',
  2597. ccallparanode.create(left,nil));
  2598. left := nil;
  2599. end;
  2600. function tinlinenode.first_cos_real : tnode;
  2601. begin
  2602. { create the call to the helper }
  2603. { on entry left node contains the parameter }
  2604. first_cos_real := ccallnode.createintern('fpc_cos_real',
  2605. ccallparanode.create(left,nil));
  2606. left := nil;
  2607. end;
  2608. function tinlinenode.first_sin_real : tnode;
  2609. begin
  2610. { create the call to the helper }
  2611. { on entry left node contains the parameter }
  2612. first_sin_real := ccallnode.createintern('fpc_sin_real',
  2613. ccallparanode.create(left,nil));
  2614. left := nil;
  2615. end;
  2616. function tinlinenode.first_exp_real : tnode;
  2617. begin
  2618. { create the call to the helper }
  2619. { on entry left node contains the parameter }
  2620. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  2621. left := nil;
  2622. end;
  2623. function tinlinenode.first_int_real : tnode;
  2624. begin
  2625. { create the call to the helper }
  2626. { on entry left node contains the parameter }
  2627. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  2628. left := nil;
  2629. end;
  2630. function tinlinenode.first_frac_real : tnode;
  2631. begin
  2632. { create the call to the helper }
  2633. { on entry left node contains the parameter }
  2634. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  2635. left := nil;
  2636. end;
  2637. function tinlinenode.first_round_real : tnode;
  2638. begin
  2639. { create the call to the helper }
  2640. { on entry left node contains the parameter }
  2641. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  2642. left := nil;
  2643. end;
  2644. function tinlinenode.first_trunc_real : tnode;
  2645. begin
  2646. { create the call to the helper }
  2647. { on entry left node contains the parameter }
  2648. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  2649. left := nil;
  2650. end;
  2651. function tinlinenode.first_abs_long : tnode;
  2652. begin
  2653. result:=nil;
  2654. end;
  2655. function tinlinenode.first_pack_unpack: tnode;
  2656. var
  2657. loopstatement : tstatementnode;
  2658. loop : tblocknode;
  2659. loopvar : ttempcreatenode;
  2660. tempnode,
  2661. source,
  2662. target,
  2663. index,
  2664. unpackednode,
  2665. packednode,
  2666. sourcevecindex,
  2667. targetvecindex,
  2668. loopbody : tnode;
  2669. temprangedef : tdef;
  2670. ulorange,
  2671. uhirange,
  2672. plorange,
  2673. phirange : TConstExprInt;
  2674. begin
  2675. { transform into a for loop which assigns the data of the (un)packed }
  2676. { array to the other one }
  2677. source := left;
  2678. if (inlinenumber = in_unpack_x_y_z) then
  2679. begin
  2680. target := tcallparanode(source).right;
  2681. index := tcallparanode(target).right;
  2682. packednode := tcallparanode(source).left;
  2683. unpackednode := tcallparanode(target).left;
  2684. end
  2685. else
  2686. begin
  2687. index := tcallparanode(source).right;
  2688. target := tcallparanode(index).right;
  2689. packednode := tcallparanode(target).left;
  2690. unpackednode := tcallparanode(source).left;
  2691. end;
  2692. source := tcallparanode(source).left;
  2693. target := tcallparanode(target).left;
  2694. index := tcallparanode(index).left;
  2695. loop := internalstatements(loopstatement);
  2696. loopvar := ctempcreatenode.create(
  2697. tarraydef(packednode.resultdef).rangedef,
  2698. tarraydef(packednode.resultdef).rangedef.size,
  2699. tt_persistent,true);
  2700. addstatement(loopstatement,loopvar);
  2701. { For range checking: we have to convert to an integer type (in case the index type }
  2702. { is an enum), add the index and loop variable together, convert the result }
  2703. { implicitly to an orddef with range equal to the rangedef to get range checking }
  2704. { and finally convert it explicitly back to the actual rangedef to avoid type }
  2705. { errors }
  2706. temprangedef:=nil;
  2707. getrange(unpackednode.resultdef,ulorange,uhirange);
  2708. getrange(packednode.resultdef,plorange,phirange);
  2709. temprangedef:=torddef.create(torddef(sinttype).ordtype,ulorange,uhirange);
  2710. sourcevecindex := ctemprefnode.create(loopvar);
  2711. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  2712. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  2713. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  2714. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  2715. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  2716. if (inlinenumber = in_pack_x_y_z) then
  2717. begin
  2718. { swap source and target vec indices }
  2719. tempnode := sourcevecindex;
  2720. sourcevecindex := targetvecindex;
  2721. targetvecindex := tempnode;
  2722. end;
  2723. { create the assignment in the loop body }
  2724. loopbody :=
  2725. cassignmentnode.create(
  2726. cvecnode.create(target.getcopy,targetvecindex),
  2727. cvecnode.create(source.getcopy,sourcevecindex)
  2728. );
  2729. { create the actual for loop }
  2730. tempnode := cfornode.create(
  2731. ctemprefnode.create(loopvar),
  2732. cinlinenode.create(in_low_x,false,packednode.getcopy),
  2733. cinlinenode.create(in_high_x,false,packednode.getcopy),
  2734. loopbody,
  2735. false);
  2736. addstatement(loopstatement,tempnode);
  2737. { free the loop counter }
  2738. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  2739. result := loop;
  2740. end;
  2741. begin
  2742. cinlinenode:=tinlinenode;
  2743. end.