ninl.pas 126 KB

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