nflw.pas 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Type checking and register allocation for nodes that influence
  4. the flow
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nflw;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. node,cpubase,
  24. symconst,symtype,symbase,symdef,symsym,
  25. optloop;
  26. type
  27. { flags used by loop nodes }
  28. tloopflag = (
  29. { set if it is a for ... downto ... do loop }
  30. lnf_backward,
  31. { Do we need to parse childs to set var state? }
  32. lnf_varstate,
  33. { Do a test at the begin of the loop?}
  34. lnf_testatbegin,
  35. { Negate the loop test? }
  36. lnf_checknegate,
  37. { Should the value of the loop variable on exit be correct. }
  38. lnf_dont_mind_loopvar_on_exit,
  39. { Loop simplify flag }
  40. lnf_simplify_processing,
  41. { set if in a for loop the counter is not used, so an easier exit check
  42. can be carried out }
  43. lnf_counter_not_used);
  44. tloopflags = set of tloopflag;
  45. const
  46. { loop flags which must match to consider loop nodes equal regarding the flags }
  47. loopflagsequal = [lnf_backward];
  48. type
  49. tlabelnode = class;
  50. tloopnode = class(tbinarynode)
  51. t1,t2 : tnode;
  52. loopflags : tloopflags;
  53. constructor create(tt : tnodetype;l,r,_t1,_t2 : tnode);virtual;
  54. destructor destroy;override;
  55. function dogetcopy : tnode;override;
  56. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  57. procedure ppuwrite(ppufile:tcompilerppufile);override;
  58. procedure buildderefimpl;override;
  59. procedure derefimpl;override;
  60. procedure insertintolist(l : tnodelist);override;
  61. procedure printnodetree(var t:text);override;
  62. {$ifdef DEBUG_NODE_XML}
  63. procedure XMLPrintNodeInfo(var T: Text); override;
  64. procedure XMLPrintNodeTree(var T: Text); override;
  65. {$endif DEBUG_NODE_XML}
  66. function docompare(p: tnode): boolean; override;
  67. end;
  68. twhilerepeatnode = class(tloopnode)
  69. { l: condition; r: body; tab: test at begin; cn: negate condition
  70. x,y,true,false: while loop
  71. x,y,false,true: repeat until loop }
  72. constructor create(l,r:Tnode;tab,cn:boolean);virtual;reintroduce;
  73. function pass_typecheck:tnode;override;
  74. function pass_1 : tnode;override;
  75. {$ifdef state_tracking}
  76. function track_state_pass(exec_known:boolean):boolean;override;
  77. {$endif}
  78. end;
  79. twhilerepeatnodeclass = class of twhilerepeatnode;
  80. tifnode = class(tloopnode)
  81. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  82. constructor create_internal(l,r,_t1 : tnode);virtual;reintroduce;
  83. function pass_typecheck:tnode;override;
  84. function pass_1 : tnode;override;
  85. function simplify(forinline : boolean) : tnode;override;
  86. private
  87. function internalsimplify(warn: boolean) : tnode;
  88. end;
  89. tifnodeclass = class of tifnode;
  90. tfornode = class(tloopnode)
  91. { if count isn divisable by unrolls then
  92. the for loop must jump to this label to get the correct
  93. number of executions }
  94. entrylabel,
  95. { this is a dummy node used by the dfa to store life information for the loop iteration }
  96. loopiteration : tnode;
  97. loopvar_notid:cardinal;
  98. constructor create(l,r,_t1,_t2 : tnode;back : boolean);virtual;reintroduce;
  99. destructor destroy;override;
  100. function pass_typecheck:tnode;override;
  101. function pass_1 : tnode;override;
  102. function makewhileloop : tnode;
  103. function simplify(forinline : boolean) : tnode;override;
  104. end;
  105. tfornodeclass = class of tfornode;
  106. texitnode = class(tunarynode)
  107. constructor create(l:tnode);virtual;
  108. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  109. procedure ppuwrite(ppufile:tcompilerppufile);override;
  110. function pass_typecheck:tnode;override;
  111. function pass_1 : tnode;override;
  112. property resultexpr : tnode read left write left;
  113. end;
  114. texitnodeclass = class of texitnode;
  115. tbreaknode = class(tnode)
  116. constructor create;virtual;
  117. function pass_typecheck:tnode;override;
  118. function pass_1 : tnode;override;
  119. end;
  120. tbreaknodeclass = class of tbreaknode;
  121. tcontinuenode = class(tnode)
  122. constructor create;virtual;
  123. function pass_typecheck:tnode;override;
  124. function pass_1 : tnode;override;
  125. end;
  126. tcontinuenodeclass = class of tcontinuenode;
  127. tgotonode = class(tnode)
  128. private
  129. labelnodeidx : longint;
  130. public
  131. { * Set when creating the gotonode (since that's all we know at that
  132. point).
  133. * Used in pass_typecheck to find the corresponding labelnode (when a
  134. labelnode is created for a tlabelsym, the label assigns itself to
  135. the "code" field of the labelsym), which is then assigned to the
  136. labelnode field
  137. * After this, the labelsym is (and must) never be used anymore, and
  138. instead the labelnode must always be used. The reason is that the
  139. labelsym may not be owned by anything, and will be freed by the
  140. label node when it gets freed
  141. * The above is the reason why the labelsym field does not get copied
  142. by tgotonode.dogetcopy, but instead the copy of the labelnode gets
  143. tracked (both the labelnode and its goto nodes must always all be
  144. copied).
  145. The labelnode itself will not copy the labelsym either in dogetcopy.
  146. Instead, since the link between the gotos and the labels gets
  147. tracked via node tree references, the label node will generate a new
  148. asmlabel on the fly and the goto node will get it from there (if the
  149. goto node gets processed before the label node has been processed,
  150. it will ask the label node to generate the asmsymbol at that point).
  151. The original tlabelsym will get emitted only for the original
  152. label node. It is only actually used if there is a reference to it
  153. from
  154. * an inline assembly block. Since inline assembly blocks cannot be
  155. inlined at this point, it doesn't matter that this would break
  156. in case the node gets copied
  157. * global goto/label. Inlining is not supported for these, so no
  158. problem here either for now.
  159. * a load node (its symtableentry field). Since the symtableentry
  160. of loadnodes is always expected to be valid, we cannot do like
  161. with the goto nodes. Instead, we will create a new labelsym
  162. when performing a dogetcopy of such a load node and assign this
  163. labelsym to the copied labelnode (and vice versa)
  164. }
  165. labelsym : tlabelsym;
  166. labelnode : tlabelnode;
  167. exceptionblock : integer;
  168. constructor create(p : tlabelsym);virtual;
  169. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  170. procedure ppuwrite(ppufile:tcompilerppufile);override;
  171. procedure buildderefimpl;override;
  172. procedure derefimpl;override;
  173. procedure resolveppuidx;override;
  174. function dogetcopy : tnode;override;
  175. function pass_typecheck:tnode;override;
  176. function pass_1 : tnode;override;
  177. function docompare(p: tnode): boolean; override;
  178. end;
  179. tgotonodeclass = class of tgotonode;
  180. tlabelnode = class(tnode)
  181. exceptionblock : integer;
  182. { when copying trees, this points to the newly created copy of a label }
  183. copiedto : tlabelnode;
  184. labsym : tlabelsym;
  185. constructor create(l:tnode;alabsym:tlabelsym);virtual;
  186. destructor destroy;override;
  187. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  188. procedure ppuwrite(ppufile:tcompilerppufile);override;
  189. procedure buildderefimpl;override;
  190. procedure derefimpl;override;
  191. function dogetcopy : tnode;override;
  192. function pass_typecheck:tnode;override;
  193. function pass_1 : tnode;override;
  194. function docompare(p: tnode): boolean; override;
  195. end;
  196. tlabelnodeclass = class of tlabelnode;
  197. traisenode = class(ttertiarynode)
  198. constructor create(l,taddr,tframe:tnode);virtual;
  199. function pass_typecheck:tnode;override;
  200. function pass_1 : tnode;override;
  201. end;
  202. traisenodeclass = class of traisenode;
  203. ttryexceptnode = class(tloopnode)
  204. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  205. function pass_typecheck:tnode;override;
  206. function pass_1 : tnode;override;
  207. function simplify(forinline: boolean): tnode; override;
  208. protected
  209. procedure adjust_estimated_stack_size; virtual;
  210. end;
  211. ttryexceptnodeclass = class of ttryexceptnode;
  212. { the third node is to store a copy of the finally code for llvm:
  213. it needs one copy to execute in case an exception occurs, and
  214. one in case no exception occurs }
  215. ttryfinallynode = class(ttertiarynode)
  216. implicitframe : boolean;
  217. constructor create(l,r:tnode);virtual;reintroduce;
  218. constructor create_implicit(l,r:tnode);virtual;
  219. function pass_typecheck:tnode;override;
  220. function pass_1 : tnode;override;
  221. function simplify(forinline:boolean): tnode;override;
  222. protected
  223. procedure adjust_estimated_stack_size; virtual;
  224. public
  225. function dogetcopy: tnode;override;
  226. end;
  227. ttryfinallynodeclass = class of ttryfinallynode;
  228. tonnode = class(tbinarynode)
  229. excepTSymtable : TSymtable;
  230. excepttype : tobjectdef;
  231. constructor create(l,r:tnode);virtual;
  232. destructor destroy;override;
  233. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  234. function pass_typecheck:tnode;override;
  235. function pass_1 : tnode;override;
  236. function dogetcopy : tnode;override;
  237. function docompare(p: tnode): boolean; override;
  238. end;
  239. tonnodeclass = class of tonnode;
  240. var
  241. cwhilerepeatnode : twhilerepeatnodeclass=twhilerepeatnode;
  242. cifnode : tifnodeclass = tifnode;
  243. cfornode : tfornodeclass = tfornode;
  244. cexitnode : texitnodeclass = texitnode;
  245. cgotonode : tgotonodeclass = tgotonode;
  246. clabelnode : tlabelnodeclass = tlabelnode;
  247. craisenode : traisenodeclass = traisenode;
  248. ctryexceptnode : ttryexceptnodeclass = ttryexceptnode;
  249. ctryfinallynode : ttryfinallynodeclass = ttryfinallynode;
  250. connode : tonnodeclass = tonnode;
  251. cbreaknode : tbreaknodeclass = tbreaknode;
  252. ccontinuenode : tcontinuenodeclass = tcontinuenode;
  253. // for-in loop helpers
  254. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  255. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  256. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  257. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  258. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  259. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  260. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  261. { converts all for nodes in the tree into while nodes,
  262. returns true if something was converted }
  263. function ConvertForLoops(var n : tnode) : Boolean;
  264. implementation
  265. uses
  266. globtype,systems,constexp,compinnr,
  267. cutils,verbose,globals,ppu,
  268. symtable,paramgr,defcmp,defutil,htypechk,pass_1,
  269. ncal,nadd,ncon,nmem,nld,ncnv,nbas,nutils,ninl,nset,ngenutil,
  270. {$ifdef state_tracking}
  271. nstate,
  272. {$endif}
  273. {$ifdef i8086}
  274. cpuinfo,
  275. {$endif i8086}
  276. {$if defined(xtensa) or defined(i386) or defined(riscv)}
  277. cpuinfo,
  278. {$endif defined(xtensa) or defined(i386) or defined(riscv)}
  279. cgbase,procinfo
  280. ;
  281. // for-in loop helpers
  282. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  283. begin
  284. result:=cfornode.create(hloopvar,
  285. cinlinenode.create(in_low_x,false,expr.getcopy),
  286. cinlinenode.create(in_high_x,false,expr.getcopy),
  287. hloopbody,
  288. false);
  289. end;
  290. function create_objc_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  291. var
  292. mainstatement, outerloopbodystatement, innerloopbodystatement, tempstatement: tstatementnode;
  293. state, mutationcheck, currentamount, innerloopcounter, items, expressiontemp: ttempcreatenode;
  294. outerloop, innerloop, hp: tnode;
  295. itemsarraydef: tarraydef;
  296. sym: tsym;
  297. begin
  298. { Objective-C enumerators require Objective-C 2.0 }
  299. if not(m_objectivec2 in current_settings.modeswitches) then
  300. begin
  301. result:=cerrornode.create;
  302. MessagePos(expr.fileinfo,parser_e_objc_enumerator_2_0);
  303. exit;
  304. end;
  305. { Requires the NSFastEnumeration protocol and NSFastEnumerationState
  306. record }
  307. maybeloadcocoatypes;
  308. if not assigned(objc_fastenumeration) or
  309. not assigned(objc_fastenumerationstate) then
  310. begin
  311. result:=cerrornode.create;
  312. MessagePos(expr.fileinfo,parser_e_objc_missing_enumeration_defs);
  313. exit;
  314. end;
  315. (* Original code:
  316. for hloopvar in expression do
  317. <hloopbody>
  318. Pascal code equivalent into which it has to be transformed
  319. (sure would be nice if the compiler had some kind of templates ;) :
  320. var
  321. state: NSFastEnumerationState;
  322. expressiontemp: NSFastEnumerationProtocol;
  323. mutationcheck,
  324. currentamount,
  325. innerloopcounter: culong;
  326. { size can be increased/decreased if desired }
  327. items: array[1..16] of id;
  328. begin
  329. fillchar(state,sizeof(state),0);
  330. expressiontemp:=expression;
  331. repeat
  332. currentamount:=expressiontemp.countByEnumeratingWithState_objects_count(@state,@items,length(items));
  333. if currentamount=0 then
  334. begin
  335. { "The iterating variable is set to nil when the loop ends by
  336. exhausting the source pool of objects" }
  337. hloopvar:=nil;
  338. break;
  339. end;
  340. mutationcheck:=state.mutationsptr^;
  341. innerloopcounter:=culong(-1);
  342. repeat
  343. { at the start so that "continue" in <loopbody> works correctly }
  344. { don't use for-loop, because then the value of the iteration
  345. counter is undefined on exit and we have to check it in the
  346. outer repeat/until condition }
  347. {$push}
  348. {$r-,q-}
  349. inc(innerloopcounter);
  350. {$pop}
  351. if innerloopcounter=currentamount then
  352. break;
  353. if mutationcheck<>state.mutationsptr^ then
  354. { raises Objective-C exception... }
  355. objc_enumerationMutation(expressiontemp);
  356. hloopvar:=state.itemsPtr[innerloopcounter];
  357. { if continue in loopbody -> jumps to start, increases count and checks }
  358. { if break in loopbody: goes to outer repeat/until and innerloopcount
  359. will be < currentamount -> stops }
  360. <hloopbody>
  361. until false;
  362. { if the inner loop terminated early, "break" was used and we have
  363. to stop }
  364. { "If the loop is terminated early, the iterating variable is left
  365. pointing to the last iteration item." }
  366. until innerloopcounter<currentamount;
  367. end;
  368. *)
  369. result:=internalstatements(mainstatement);
  370. { the fast enumeration state }
  371. state:=ctempcreatenode.create(objc_fastenumerationstate,objc_fastenumerationstate.size,tt_persistent,false);
  372. typecheckpass(tnode(state));
  373. addstatement(mainstatement,state);
  374. { the temporary items array }
  375. itemsarraydef:=carraydef.create(1,16,u32inttype);
  376. itemsarraydef.elementdef:=objc_idtype;
  377. items:=ctempcreatenode.create(itemsarraydef,itemsarraydef.size,tt_persistent,false);
  378. addstatement(mainstatement,items);
  379. typecheckpass(tnode(items));
  380. { temp for the expression/collection through which we iterate }
  381. expressiontemp:=ctempcreatenode.create(objc_fastenumeration,objc_fastenumeration.size,tt_persistent,true);
  382. addstatement(mainstatement,expressiontemp);
  383. { currentamount temp (not really clean: we use ptruint instead of
  384. culong) }
  385. currentamount:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  386. typecheckpass(tnode(currentamount));
  387. addstatement(mainstatement,currentamount);
  388. { mutationcheck temp (idem) }
  389. mutationcheck:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  390. typecheckpass(tnode(mutationcheck));
  391. addstatement(mainstatement,mutationcheck);
  392. { innerloopcounter temp (idem) }
  393. innerloopcounter:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  394. typecheckpass(tnode(innerloopcounter));
  395. addstatement(mainstatement,innerloopcounter);
  396. { initialise the state with 0 }
  397. addstatement(mainstatement,ccallnode.createinternfromunit('SYSTEM','FILLCHAR',
  398. ccallparanode.create(genintconstnode(0),
  399. ccallparanode.create(genintconstnode(objc_fastenumerationstate.size),
  400. ccallparanode.create(ctemprefnode.create(state),nil)
  401. )
  402. )
  403. ));
  404. { this will also check whether the expression (potentially) conforms
  405. to the NSFastEnumeration protocol (use expr.getcopy, because the
  406. caller will free expr) }
  407. addstatement(mainstatement,cassignmentnode.create(ctemprefnode.create(expressiontemp),expr.getcopy));
  408. { we add the "repeat..until" afterwards, now just create the body }
  409. outerloop:=internalstatements(outerloopbodystatement);
  410. { the countByEnumeratingWithState_objects_count call }
  411. hp:=ccallparanode.create(cinlinenode.create(in_length_x,false,ctypenode.create(itemsarraydef)),
  412. ccallparanode.create(caddrnode.create(ctemprefnode.create(items)),
  413. ccallparanode.create(caddrnode.create(ctemprefnode.create(state)),nil)
  414. )
  415. );
  416. sym:=search_struct_member(objc_fastenumeration,'COUNTBYENUMERATINGWITHSTATE_OBJECTS_COUNT');
  417. if not assigned(sym) or
  418. (sym.typ<>procsym) then
  419. internalerror(2010061901);
  420. hp:=ccallnode.create(hp,tprocsym(sym),sym.owner,ctemprefnode.create(expressiontemp),[],nil);
  421. addstatement(outerloopbodystatement,cassignmentnode.create(
  422. ctemprefnode.create(currentamount),hp));
  423. { if currentamount = 0, bail out (use copy of hloopvar, because we
  424. have to use it again below) }
  425. hp:=internalstatements(tempstatement);
  426. addstatement(tempstatement,cassignmentnode.create(
  427. hloopvar.getcopy,cnilnode.create));
  428. addstatement(tempstatement,cbreaknode.create);
  429. addstatement(outerloopbodystatement,cifnode.create(
  430. caddnode.create(equaln,ctemprefnode.create(currentamount),genintconstnode(0)),
  431. hp,nil));
  432. { initial value of mutationcheck }
  433. hp:=ctemprefnode.create(state);
  434. typecheckpass(hp);
  435. hp:=cderefnode.create(genloadfield(hp,'MUTATIONSPTR'));
  436. addstatement(outerloopbodystatement,cassignmentnode.create(
  437. ctemprefnode.create(mutationcheck),hp));
  438. { initialise innerloopcounter }
  439. addstatement(outerloopbodystatement,cassignmentnode.create(
  440. ctemprefnode.create(innerloopcounter),cordconstnode.create(-1,ptruinttype,false)));
  441. { and now the inner loop, again adding the repeat/until afterwards }
  442. innerloop:=internalstatements(innerloopbodystatement);
  443. { inc(innerloopcounter) without range/overflowchecking (because
  444. we go from culong(-1) to 0 during the first iteration }
  445. hp:=cinlinenode.create(
  446. in_inc_x,false,ccallparanode.create(
  447. ctemprefnode.create(innerloopcounter),nil));
  448. hp.localswitches:=hp.localswitches-[cs_check_range,cs_check_overflow];
  449. addstatement(innerloopbodystatement,hp);
  450. { if innerloopcounter=currentamount then break to the outer loop }
  451. addstatement(innerloopbodystatement,cifnode.create(
  452. caddnode.create(equaln,
  453. ctemprefnode.create(innerloopcounter),
  454. ctemprefnode.create(currentamount)),
  455. cbreaknode.create,
  456. nil));
  457. { verify that the collection didn't change in the mean time }
  458. hp:=ctemprefnode.create(state);
  459. typecheckpass(hp);
  460. addstatement(innerloopbodystatement,cifnode.create(
  461. caddnode.create(unequaln,
  462. ctemprefnode.create(mutationcheck),
  463. cderefnode.create(genloadfield(hp,'MUTATIONSPTR'))
  464. ),
  465. ccallnode.createinternfromunit('OBJC','OBJC_ENUMERATIONMUTATION',
  466. ccallparanode.create(ctemprefnode.create(expressiontemp),nil)),
  467. nil));
  468. { finally: actually get the next element }
  469. hp:=ctemprefnode.create(state);
  470. typecheckpass(hp);
  471. hp:=genloadfield(hp,'ITEMSPTR');
  472. typecheckpass(hp);
  473. { don't simply use a vecn, because indexing a pointer won't work in
  474. non-FPC modes }
  475. if hp.resultdef.typ<>pointerdef then
  476. internalerror(2010061904);
  477. inserttypeconv(hp,
  478. carraydef.create_from_pointer(tpointerdef(hp.resultdef)));
  479. hp:=cvecnode.create(hp,ctemprefnode.create(innerloopcounter));
  480. addstatement(innerloopbodystatement,
  481. cassignmentnode.create(hloopvar,hp));
  482. { the actual loop body! }
  483. addstatement(innerloopbodystatement,hloopbody);
  484. { create the inner repeat/until and add it to the body of the outer
  485. one }
  486. hp:=cwhilerepeatnode.create(
  487. { repeat .. until false }
  488. cordconstnode.create(0,pasbool1type,false),innerloop,false,true);
  489. addstatement(outerloopbodystatement,hp);
  490. { create the outer repeat/until and add it to the the main body }
  491. hp:=cwhilerepeatnode.create(
  492. { repeat .. until innerloopcounter<currentamount }
  493. caddnode.create(ltn,
  494. ctemprefnode.create(innerloopcounter),
  495. ctemprefnode.create(currentamount)),
  496. outerloop,false,true);
  497. addstatement(mainstatement,hp);
  498. { release the temps }
  499. addstatement(mainstatement,ctempdeletenode.create(state));
  500. addstatement(mainstatement,ctempdeletenode.create(mutationcheck));
  501. addstatement(mainstatement,ctempdeletenode.create(currentamount));
  502. addstatement(mainstatement,ctempdeletenode.create(innerloopcounter));
  503. addstatement(mainstatement,ctempdeletenode.create(items));
  504. addstatement(mainstatement,ctempdeletenode.create(expressiontemp));
  505. end;
  506. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  507. var
  508. loopstatement, loopbodystatement: tstatementnode;
  509. loopvar, stringvar: ttempcreatenode;
  510. stringindex, loopbody, forloopnode, fromn, ton: tnode;
  511. begin
  512. { result is a block of statements }
  513. result:=internalstatements(loopstatement);
  514. { create a temp variable for expression }
  515. stringvar := ctempcreatenode.create(
  516. expr.resultdef,
  517. expr.resultdef.size,
  518. tt_persistent,true);
  519. addstatement(loopstatement,stringvar);
  520. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(stringvar),expr.getcopy));
  521. { create a loop counter: signed integer with size of string length }
  522. loopvar := ctempcreatenode.create(
  523. sinttype,
  524. sinttype.size,
  525. tt_persistent,true);
  526. addstatement(loopstatement,loopvar);
  527. stringindex:=ctemprefnode.create(loopvar);
  528. loopbody:=internalstatements(loopbodystatement);
  529. // for-in loop variable := string_expression[index]
  530. addstatement(loopbodystatement,
  531. cassignmentnode.create(hloopvar, cvecnode.create(ctemprefnode.create(stringvar),stringindex)));
  532. { add the actual statement to the loop }
  533. addstatement(loopbodystatement,hloopbody);
  534. if tstringdef(expr.resultdef).stringtype=st_shortstring then
  535. begin
  536. fromn:=genintconstnode(1);
  537. ton:=cinlinenode.create(in_length_x,false,ctemprefnode.create(stringvar));
  538. end
  539. else
  540. begin
  541. fromn:=cinlinenode.createintern(in_low_x,false,ctemprefnode.create(stringvar));
  542. ton:= cinlinenode.create(in_high_x,false,ctemprefnode.create(stringvar));
  543. end;
  544. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  545. fromn,
  546. ton,
  547. loopbody,
  548. false);
  549. addstatement(loopstatement,forloopnode);
  550. { free the loop counter }
  551. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  552. { free the temp variable for expression }
  553. addstatement(loopstatement,ctempdeletenode.create(stringvar));
  554. end;
  555. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  556. var
  557. loopstatement, loopbodystatement: tstatementnode;
  558. loopvar, arrayvar: ttempcreatenode;
  559. arrayindex, lowbound, highbound, loopbody, forloopnode, expression: tnode;
  560. is_string: boolean;
  561. tmpdef, convertdef: tdef;
  562. elementcount: aword;
  563. begin
  564. expression := expr;
  565. { result is a block of statements }
  566. result:=internalstatements(loopstatement);
  567. is_string:=ado_IsConstString in tarraydef(expr.resultdef).arrayoptions;
  568. // if array element type <> loovar type then create a conversion if possible
  569. if compare_defs(tarraydef(expression.resultdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible then
  570. begin
  571. tmpdef:=expression.resultdef;
  572. elementcount:=1;
  573. while assigned(tmpdef) and (tmpdef.typ=arraydef) and
  574. (tarraydef(tmpdef).arrayoptions = []) and
  575. (compare_defs(tarraydef(tmpdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible) do
  576. begin
  577. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  578. tmpdef:=tarraydef(tmpdef).elementdef;
  579. end;
  580. if assigned(tmpdef) and (tmpdef.typ=arraydef) and (tarraydef(tmpdef).arrayoptions = []) then
  581. begin
  582. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  583. convertdef:=carraydef.create(0,elementcount-1,s32inttype);
  584. tarraydef(convertdef).elementdef:=tarraydef(tmpdef).elementdef;
  585. expression:=expr.getcopy;
  586. expression:=ctypeconvnode.create_internal(expression,convertdef);
  587. typecheckpass(expression);
  588. addstatement(loopstatement,expression);
  589. end;
  590. end;
  591. if (node_complexity(expression) > 1) and
  592. not(is_open_array(expression.resultdef)) and not(is_array_of_const(expression.resultdef)) then
  593. begin
  594. { create a temp variable for expression }
  595. arrayvar := ctempcreatenode.create(
  596. expression.resultdef,
  597. expression.resultdef.size,
  598. tt_persistent,true);
  599. if is_string then
  600. begin
  601. lowbound:=genintconstnode(1);
  602. highbound:=cinlinenode.create(in_length_x,false,ctemprefnode.create(arrayvar))
  603. end
  604. else
  605. begin
  606. lowbound:=cinlinenode.create(in_low_x,false,ctemprefnode.create(arrayvar));
  607. highbound:=cinlinenode.create(in_high_x,false,ctemprefnode.create(arrayvar));
  608. end;
  609. addstatement(loopstatement,arrayvar);
  610. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(arrayvar),expression.getcopy));
  611. end
  612. else
  613. begin
  614. arrayvar:=nil;
  615. if is_string then
  616. begin
  617. lowbound:=genintconstnode(1);
  618. highbound:=cinlinenode.create(in_length_x,false,expression.getcopy);
  619. end
  620. else
  621. begin
  622. lowbound:=cinlinenode.create(in_low_x,false,expression.getcopy);
  623. highbound:=cinlinenode.create(in_high_x,false,expression.getcopy);
  624. end;
  625. end;
  626. { create a loop counter }
  627. loopvar := ctempcreatenode.create(
  628. tarraydef(expression.resultdef).rangedef,
  629. tarraydef(expression.resultdef).rangedef.size,
  630. tt_persistent,true);
  631. addstatement(loopstatement,loopvar);
  632. arrayindex:=ctemprefnode.create(loopvar);
  633. loopbody:=internalstatements(loopbodystatement);
  634. // for-in loop variable := array_expression[index]
  635. if assigned(arrayvar) then
  636. addstatement(loopbodystatement,
  637. cassignmentnode.create(hloopvar,cvecnode.create(ctemprefnode.create(arrayvar),arrayindex)))
  638. else
  639. addstatement(loopbodystatement,
  640. cassignmentnode.create(hloopvar,cvecnode.create(expression.getcopy,arrayindex)));
  641. { add the actual statement to the loop }
  642. addstatement(loopbodystatement,hloopbody);
  643. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  644. lowbound,
  645. highbound,
  646. loopbody,
  647. false);
  648. addstatement(loopstatement,forloopnode);
  649. { free the loop counter }
  650. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  651. { free the temp variable for expression if needed }
  652. if arrayvar<>nil then
  653. addstatement(loopstatement,ctempdeletenode.create(arrayvar));
  654. end;
  655. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  656. var
  657. loopstatement, loopbodystatement: tstatementnode;
  658. loopvar, setvar: ttempcreatenode;
  659. loopbody, forloopnode: tnode;
  660. begin
  661. // first check is set is empty and if it so then skip other processing
  662. if not Assigned(tsetdef(expr.resultdef).elementdef) then
  663. begin
  664. result:=cnothingnode.create;
  665. // free unused nodes
  666. hloopvar.free;
  667. hloopbody.free;
  668. exit;
  669. end;
  670. { result is a block of statements }
  671. result:=internalstatements(loopstatement);
  672. { create a temp variable for expression }
  673. setvar := ctempcreatenode.create(
  674. expr.resultdef,
  675. expr.resultdef.size,
  676. tt_persistent,true);
  677. addstatement(loopstatement,setvar);
  678. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(setvar),expr.getcopy));
  679. { create a loop counter }
  680. loopvar := ctempcreatenode.create(
  681. tsetdef(expr.resultdef).elementdef,
  682. tsetdef(expr.resultdef).elementdef.size,
  683. tt_persistent,true);
  684. addstatement(loopstatement,loopvar);
  685. // if loopvar in set then
  686. // begin
  687. // hloopvar := loopvar
  688. // for-in loop body
  689. // end
  690. loopbody:=cifnode.create(
  691. cinnode.create(ctemprefnode.create(loopvar),ctemprefnode.create(setvar)),
  692. internalstatements(loopbodystatement),
  693. nil);
  694. addstatement(loopbodystatement,cassignmentnode.create(hloopvar,ctemprefnode.create(loopvar)));
  695. { add the actual statement to the loop }
  696. addstatement(loopbodystatement,hloopbody);
  697. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  698. cinlinenode.create(in_low_x,false,ctemprefnode.create(setvar)),
  699. cinlinenode.create(in_high_x,false,ctemprefnode.create(setvar)),
  700. loopbody,
  701. false);
  702. addstatement(loopstatement,forloopnode);
  703. { free the loop counter }
  704. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  705. { free the temp variable for expression }
  706. addstatement(loopstatement,ctempdeletenode.create(setvar));
  707. end;
  708. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  709. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  710. var
  711. loopstatement, loopbodystatement: tstatementnode;
  712. enumvar: ttempcreatenode;
  713. loopbody, whileloopnode,
  714. enum_get, enum_move, enum_current, enum_get_params: tnode;
  715. propaccesslist: tpropaccesslist;
  716. enumerator_is_class: boolean;
  717. enumerator_destructor: tprocdef;
  718. begin
  719. { result is a block of statements }
  720. result:=internalstatements(loopstatement);
  721. enumerator_is_class := is_class(enumerator_get.returndef);
  722. { create a temp variable for enumerator }
  723. enumvar := ctempcreatenode.create(
  724. enumerator_get.returndef,
  725. enumerator_get.returndef.size,
  726. tt_persistent,true);
  727. addstatement(loopstatement,enumvar);
  728. if enumerator_get.proctypeoption=potype_operator then
  729. begin
  730. enum_get_params:=ccallparanode.create(expr.getcopy,nil);
  731. enum_get:=ccallnode.create(enum_get_params, tprocsym(enumerator_get.procsym), nil, nil, [],nil);
  732. tcallnode(enum_get).procdefinition:=enumerator_get;
  733. addsymref(enumerator_get.procsym,enumerator_get);
  734. end
  735. else
  736. enum_get:=ccallnode.create(nil, tprocsym(enumerator_get.procsym), enumerator_get.owner, expr.getcopy, [],nil);
  737. addstatement(loopstatement,
  738. cassignmentnode.create(
  739. ctemprefnode.create(enumvar),
  740. enum_get
  741. ));
  742. loopbody:=internalstatements(loopbodystatement);
  743. { for-in loop variable := enumerator.current }
  744. if enumerator_current.getpropaccesslist(palt_read,propaccesslist) then
  745. begin
  746. case propaccesslist.firstsym^.sym.typ of
  747. fieldvarsym :
  748. begin
  749. { generate access code }
  750. enum_current:=ctemprefnode.create(enumvar);
  751. propaccesslist_to_node(enum_current,enumerator_current.owner,propaccesslist);
  752. include(enum_current.flags,nf_isproperty);
  753. end;
  754. procsym :
  755. begin
  756. { generate the method call }
  757. enum_current:=ccallnode.create(nil,tprocsym(propaccesslist.firstsym^.sym),enumerator_current.owner,ctemprefnode.create(enumvar),[],nil);
  758. include(enum_current.flags,nf_isproperty);
  759. end
  760. else
  761. begin
  762. enum_current:=cerrornode.create;
  763. Message(type_e_mismatch);
  764. end;
  765. end;
  766. end
  767. else
  768. enum_current:=cerrornode.create;
  769. addstatement(loopbodystatement,
  770. cassignmentnode.create(hloopvar, enum_current));
  771. { add the actual statement to the loop }
  772. addstatement(loopbodystatement,hloopbody);
  773. enum_move:=ccallnode.create(nil, tprocsym(enumerator_move.procsym), enumerator_move.owner, ctemprefnode.create(enumvar), [],nil);
  774. whileloopnode:=cwhilerepeatnode.create(enum_move,loopbody,true,false);
  775. if enumerator_is_class then
  776. begin
  777. { insert a try-finally and call the destructor for the enumerator in the finally section }
  778. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  779. if assigned(enumerator_destructor) then
  780. begin
  781. whileloopnode:=ctryfinallynode.create(
  782. whileloopnode, // try node
  783. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym), // finally node
  784. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[],nil));
  785. end;
  786. { if getenumerator <> nil then do the loop }
  787. whileloopnode:=cifnode.create(
  788. caddnode.create(unequaln, ctemprefnode.create(enumvar), cnilnode.create),
  789. whileloopnode,
  790. nil);
  791. end;
  792. addstatement(loopstatement, whileloopnode);
  793. if is_object(enumerator_get.returndef) then
  794. begin
  795. // call the object destructor too
  796. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  797. if assigned(enumerator_destructor) then
  798. begin
  799. addstatement(loopstatement,
  800. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym),
  801. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[],nil));
  802. end;
  803. end;
  804. { free the temp variable for enumerator }
  805. addstatement(loopstatement,ctempdeletenode.create(enumvar));
  806. end;
  807. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  808. var
  809. pd, movenext: tprocdef;
  810. helperdef: tobjectdef;
  811. current: tpropertysym;
  812. storefilepos: tfileposinfo;
  813. begin
  814. storefilepos:=current_filepos;
  815. current_filepos:=hloopvar.fileinfo;
  816. if expr.nodetype=typen then
  817. begin
  818. if (expr.resultdef.typ=enumdef) and tenumdef(expr.resultdef).has_jumps then
  819. begin
  820. result:=cerrornode.create;
  821. hloopvar.free;
  822. hloopbody.free;
  823. MessagePos1(expr.fileinfo,parser_e_for_in_loop_cannot_be_used_for_the_type,expr.resultdef.typename);
  824. end
  825. else
  826. result:=create_type_for_in_loop(hloopvar, hloopbody, expr);
  827. end
  828. else
  829. begin
  830. { loop is made for an expression }
  831. // Objective-C uses different conventions (and it's only supported for Objective-C 2.0)
  832. if is_objc_class_or_protocol(hloopvar.resultdef) or
  833. is_objc_class_or_protocol(expr.resultdef) then
  834. begin
  835. result:=create_objc_for_in_loop(hloopvar,hloopbody,expr);
  836. if result.nodetype=errorn then
  837. begin
  838. hloopvar.free;
  839. hloopbody.free;
  840. end;
  841. end
  842. { "for x in [] do ..." always results in a never executed loop body }
  843. else if (is_array_constructor(expr.resultdef) and
  844. (tarraydef(expr.resultdef).elementdef=voidtype)) then
  845. begin
  846. if assigned(hloopbody) then
  847. MessagePos(hloopbody.fileinfo,cg_w_unreachable_code);
  848. result:=cnothingnode.create;
  849. end
  850. else
  851. begin
  852. if is_array_constructor(expr.resultdef) then
  853. tarrayconstructornode(expr).force_type(hloopvar.resultdef);
  854. // search for operator first
  855. pd:=search_enumerator_operator(expr.resultdef, hloopvar.resultdef);
  856. // if there is no operator then search for class/object enumerator method
  857. if (pd=nil) and (expr.resultdef.typ in [objectdef,recorddef]) then
  858. begin
  859. { first search using the helper hierarchy }
  860. if search_last_objectpascal_helper(tabstractrecorddef(expr.resultdef),nil,helperdef) then
  861. repeat
  862. pd:=helperdef.search_enumerator_get;
  863. helperdef:=helperdef.childof;
  864. until (pd<>nil) or (helperdef=nil);
  865. { we didn't find an enumerator in a helper, so search in the
  866. class/record/object itself }
  867. if pd=nil then
  868. pd:=tabstractrecorddef(expr.resultdef).search_enumerator_get;
  869. end;
  870. if pd<>nil then
  871. begin
  872. // seach movenext and current symbols
  873. movenext:=tabstractrecorddef(pd.returndef).search_enumerator_move;
  874. if movenext = nil then
  875. begin
  876. result:=cerrornode.create;
  877. hloopvar.free;
  878. hloopbody.free;
  879. MessagePos1(expr.fileinfo,sym_e_no_enumerator_move,pd.returndef.typename);
  880. end
  881. else
  882. begin
  883. current:=tpropertysym(tabstractrecorddef(pd.returndef).search_enumerator_current);
  884. if current = nil then
  885. begin
  886. result:=cerrornode.create;
  887. hloopvar.free;
  888. hloopbody.free;
  889. MessagePos1(expr.fileinfo,sym_e_no_enumerator_current,pd.returndef.typename);
  890. end
  891. else
  892. result:=create_enumerator_for_in_loop(hloopvar, hloopbody, expr, pd, movenext, current);
  893. end;
  894. end
  895. else
  896. begin
  897. { prefer set if loop var could be a set var and the loop
  898. expression can indeed be a set }
  899. if (expr.nodetype=arrayconstructorn) and
  900. (hloopvar.resultdef.typ in [enumdef,orddef]) and
  901. arrayconstructor_can_be_set(expr) then
  902. begin
  903. expr:=arrayconstructor_to_set(expr,false);
  904. typecheckpass(expr);
  905. end;
  906. case expr.resultdef.typ of
  907. stringdef:
  908. result:=create_string_for_in_loop(hloopvar, hloopbody, expr);
  909. arraydef:
  910. result:=create_array_for_in_loop(hloopvar, hloopbody, expr);
  911. setdef:
  912. result:=create_set_for_in_loop(hloopvar, hloopbody, expr);
  913. undefineddef:
  914. result:=cnothingnode.create;
  915. else
  916. begin
  917. result:=cerrornode.create;
  918. hloopvar.free;
  919. hloopbody.free;
  920. MessagePos1(expr.fileinfo,sym_e_no_enumerator,expr.resultdef.typename);
  921. end;
  922. end;
  923. end;
  924. end;
  925. end;
  926. current_filepos:=storefilepos;
  927. end;
  928. function _ConvertForLoops(var n: tnode; arg: pointer): foreachnoderesult;
  929. var
  930. hp : tnode;
  931. begin
  932. Result:=fen_false;
  933. if n.nodetype=forn then
  934. begin
  935. Result:=fen_true;
  936. hp:=n;
  937. n:=tfornode(n).makewhileloop;
  938. do_firstpass(n);
  939. hp.Free;
  940. end;
  941. end;
  942. function ConvertForLoops(var n : tnode) : boolean;
  943. begin
  944. result:=foreachnodestatic(pm_postprocess,n,@_ConvertForLoops,nil);
  945. end;
  946. {****************************************************************************
  947. TLOOPNODE
  948. *****************************************************************************}
  949. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  950. begin
  951. inherited create(tt,l,r);
  952. t1:=_t1;
  953. t2:=_t2;
  954. fileinfo:=l.fileinfo;
  955. end;
  956. destructor tloopnode.destroy;
  957. begin
  958. t1.free;
  959. t2.free;
  960. inherited destroy;
  961. end;
  962. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  963. begin
  964. inherited ppuload(t,ppufile);
  965. t1:=ppuloadnode(ppufile);
  966. t2:=ppuloadnode(ppufile);
  967. ppufile.getset(tppuset1(loopflags));
  968. end;
  969. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  970. begin
  971. inherited ppuwrite(ppufile);
  972. ppuwritenode(ppufile,t1);
  973. ppuwritenode(ppufile,t2);
  974. ppufile.putset(tppuset1(loopflags));
  975. end;
  976. procedure tloopnode.buildderefimpl;
  977. begin
  978. inherited buildderefimpl;
  979. if assigned(t1) then
  980. t1.buildderefimpl;
  981. if assigned(t2) then
  982. t2.buildderefimpl;
  983. end;
  984. procedure tloopnode.derefimpl;
  985. begin
  986. inherited derefimpl;
  987. if assigned(t1) then
  988. t1.derefimpl;
  989. if assigned(t2) then
  990. t2.derefimpl;
  991. end;
  992. function tloopnode.dogetcopy : tnode;
  993. var
  994. p : tloopnode;
  995. begin
  996. p:=tloopnode(inherited dogetcopy);
  997. if assigned(t1) then
  998. p.t1:=t1.dogetcopy
  999. else
  1000. p.t1:=nil;
  1001. if assigned(t2) then
  1002. p.t2:=t2.dogetcopy
  1003. else
  1004. p.t2:=nil;
  1005. p.loopflags:=loopflags;
  1006. dogetcopy:=p;
  1007. end;
  1008. procedure tloopnode.insertintolist(l : tnodelist);
  1009. begin
  1010. end;
  1011. procedure tloopnode.printnodetree(var t:text);
  1012. begin
  1013. write(t,printnodeindention,'(');
  1014. printnodeindent;
  1015. printnodeinfo(t);
  1016. writeln(t);
  1017. printnode(t,left);
  1018. printnode(t,right);
  1019. printnode(t,t1);
  1020. printnode(t,t2);
  1021. printnodeunindent;
  1022. writeln(t,printnodeindention,')');
  1023. end;
  1024. {$ifdef DEBUG_NODE_XML}
  1025. procedure TLoopNode.XMLPrintNodeInfo(var T: Text);
  1026. var
  1027. i: TLoopFlag;
  1028. First: Boolean;
  1029. begin
  1030. inherited XMLPrintNodeInfo(T);
  1031. First := True;
  1032. for i := Low(TLoopFlag) to High(TLoopFlag) do
  1033. if i in loopflags then
  1034. begin
  1035. if First then
  1036. begin
  1037. Write(T, ' loopflags="', i);
  1038. First := False;
  1039. end
  1040. else
  1041. Write(T, ',', i)
  1042. end;
  1043. if not First then
  1044. Write(T, '"');
  1045. end;
  1046. procedure TLoopNode.XMLPrintNodeTree(var T: Text);
  1047. begin
  1048. Write(T, PrintNodeIndention, '<', nodetype2str[nodetype]);
  1049. XMLPrintNodeInfo(T);
  1050. WriteLn(T, '>');
  1051. PrintNodeIndent;
  1052. if Assigned(Left) then
  1053. begin
  1054. if nodetype = forn then
  1055. WriteLn(T, PrintNodeIndention, '<counter>')
  1056. else
  1057. WriteLn(T, PrintNodeIndention, '<condition>');
  1058. PrintNodeIndent;
  1059. XMLPrintNode(T, Left);
  1060. PrintNodeUnindent;
  1061. if nodetype = forn then
  1062. WriteLn(T, PrintNodeIndention, '</counter>')
  1063. else
  1064. WriteLn(T, PrintNodeIndention, '</condition>');
  1065. end;
  1066. if Assigned(Right) then
  1067. begin
  1068. case nodetype of
  1069. ifn:
  1070. WriteLn(T, PrintNodeIndention, '<then>');
  1071. forn:
  1072. WriteLn(T, PrintNodeIndention, '<first>');
  1073. else
  1074. WriteLn(T, PrintNodeIndention, '<right>');
  1075. end;
  1076. PrintNodeIndent;
  1077. XMLPrintNode(T, Right);
  1078. PrintNodeUnindent;
  1079. case nodetype of
  1080. ifn:
  1081. WriteLn(T, PrintNodeIndention, '</then>');
  1082. forn:
  1083. WriteLn(T, PrintNodeIndention, '</first>');
  1084. else
  1085. WriteLn(T, PrintNodeIndention, '</right>');
  1086. end;
  1087. end;
  1088. if Assigned(t1) then
  1089. begin
  1090. case nodetype of
  1091. ifn:
  1092. WriteLn(T, PrintNodeIndention, '<else>');
  1093. forn:
  1094. WriteLn(T, PrintNodeIndention, '<last>');
  1095. else
  1096. WriteLn(T, PrintNodeIndention, '<t1>');
  1097. end;
  1098. PrintNodeIndent;
  1099. XMLPrintNode(T, t1);
  1100. PrintNodeUnindent;
  1101. case nodetype of
  1102. ifn:
  1103. WriteLn(T, PrintNodeIndention, '</else>');
  1104. forn:
  1105. WriteLn(T, PrintNodeIndention, '</last>');
  1106. else
  1107. WriteLn(T, PrintNodeIndention, '</t1>');
  1108. end;
  1109. end;
  1110. if Assigned(t2) then
  1111. begin
  1112. if nodetype <> forn then
  1113. begin
  1114. WriteLn(T, PrintNodeIndention, '<loop>');
  1115. PrintNodeIndent;
  1116. end;
  1117. XMLPrintNode(T, t2);
  1118. if nodetype <> forn then
  1119. begin
  1120. PrintNodeUnindent;
  1121. WriteLn(T, PrintNodeIndention, '</loop>');
  1122. end;
  1123. end;
  1124. PrintNodeUnindent;
  1125. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  1126. end;
  1127. {$endif DEBUG_NODE_XML}
  1128. function tloopnode.docompare(p: tnode): boolean;
  1129. begin
  1130. docompare :=
  1131. inherited docompare(p) and
  1132. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  1133. t1.isequal(tloopnode(p).t1) and
  1134. t2.isequal(tloopnode(p).t2);
  1135. end;
  1136. {****************************************************************************
  1137. TWHILEREPEATNODE
  1138. *****************************************************************************}
  1139. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  1140. begin
  1141. inherited create(whilerepeatn,l,r,nil,nil);
  1142. if tab then
  1143. include(loopflags, lnf_testatbegin);
  1144. if cn then
  1145. include(loopflags,lnf_checknegate);
  1146. end;
  1147. function twhilerepeatnode.pass_typecheck:tnode;
  1148. var
  1149. t:Tunarynode;
  1150. begin
  1151. result:=nil;
  1152. resultdef:=voidtype;
  1153. typecheckpass(left);
  1154. { tp procvar support }
  1155. maybe_call_procvar(left,true);
  1156. {A not node can be removed.}
  1157. if left.nodetype=notn then
  1158. begin
  1159. t:=Tunarynode(left);
  1160. left:=Tunarynode(left).left;
  1161. t.left:=nil;
  1162. t.free;
  1163. {Symdif operator, in case you are wondering:}
  1164. loopflags:=loopflags >< [lnf_checknegate];
  1165. end;
  1166. { loop instruction }
  1167. if assigned(right) then
  1168. typecheckpass(right);
  1169. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1170. if codegenerror then
  1171. exit;
  1172. if not(is_boolean(left.resultdef)) and
  1173. not(is_typeparam(left.resultdef)) then
  1174. inserttypeconv(left,pasbool1type);
  1175. { Give warnings for code that will never be executed for
  1176. while false do }
  1177. if (lnf_testatbegin in loopflags) and
  1178. (left.nodetype=ordconstn) and
  1179. (tordconstnode(left).value.uvalue=0) and
  1180. not(nf_internal in left.flags) and
  1181. assigned(right) then
  1182. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1183. end;
  1184. {$ifdef prefetchnext}
  1185. type
  1186. passignmentquery = ^tassignmentquery;
  1187. tassignmentquery = record
  1188. towhat: tnode;
  1189. source: tassignmentnode;
  1190. statementcount: cardinal;
  1191. end;
  1192. function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
  1193. var
  1194. query: passignmentquery absolute arg;
  1195. temp, prederef: tnode;
  1196. begin
  1197. result := fen_norecurse_false;
  1198. if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
  1199. inc(query^.statementcount);
  1200. { make sure there's something else in the loop besides going to the }
  1201. { next item }
  1202. if (query^.statementcount > 1) and
  1203. (n.nodetype = assignn) then
  1204. begin
  1205. { skip type conversions of assignment target }
  1206. temp := tassignmentnode(n).left;
  1207. while (temp.nodetype = typeconvn) do
  1208. temp := ttypeconvnode(temp).left;
  1209. { assignment to x of the while assigned(x) check? }
  1210. if not(temp.isequal(query^.towhat)) then
  1211. exit;
  1212. { right hand side of assignment dereferenced field of }
  1213. { x? (no derefn in case of class) }
  1214. temp := tassignmentnode(n).right;
  1215. while (temp.nodetype = typeconvn) do
  1216. temp := ttypeconvnode(temp).left;
  1217. if (temp.nodetype <> subscriptn) then
  1218. exit;
  1219. prederef := tsubscriptnode(temp).left;
  1220. temp := prederef;
  1221. while (temp.nodetype = typeconvn) do
  1222. temp := ttypeconvnode(temp).left;
  1223. { see tests/test/prefetch1.pp }
  1224. if (temp.nodetype = derefn) then
  1225. temp := tderefnode(temp).left
  1226. else
  1227. temp := prederef;
  1228. if temp.isequal(query^.towhat) then
  1229. begin
  1230. query^.source := tassignmentnode(n);
  1231. result := fen_norecurse_true;
  1232. end
  1233. end
  1234. { don't check nodes which can't contain an assignment or whose }
  1235. { final assignment can vary a lot }
  1236. else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
  1237. result := fen_false;
  1238. end;
  1239. function findassignment(where: tnode; towhat: tnode): tassignmentnode;
  1240. var
  1241. query: tassignmentquery;
  1242. begin
  1243. query.towhat := towhat;
  1244. query.source := nil;
  1245. query.statementcount := 0;
  1246. if foreachnodestatic(where,@checkassignment,@query) then
  1247. result := query.source
  1248. else
  1249. result := nil;
  1250. end;
  1251. {$endif prefetchnext}
  1252. function twhilerepeatnode.pass_1 : tnode;
  1253. {$ifdef prefetchnext}
  1254. var
  1255. runnernode, prefetchcode: tnode;
  1256. assignmentnode: tassignmentnode;
  1257. prefetchstatements: tstatementnode;
  1258. {$endif prefetchnext}
  1259. begin
  1260. result:=nil;
  1261. expectloc:=LOC_VOID;
  1262. firstpass(left);
  1263. if codegenerror then
  1264. exit;
  1265. { loop instruction }
  1266. if assigned(right) then
  1267. begin
  1268. firstpass(right);
  1269. if codegenerror then
  1270. exit;
  1271. end;
  1272. {$ifdef prefetchnext}
  1273. { do at the end so all complex typeconversions are already }
  1274. { converted to calln's }
  1275. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1276. (lnf_testatbegin in loopflags) then
  1277. begin
  1278. { get first component of the while check }
  1279. runnernode := left;
  1280. while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
  1281. runnernode := tunarynode(runnernode).left;
  1282. { is it an assigned(x) check? }
  1283. if ((runnernode.nodetype = inlinen) and
  1284. (tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
  1285. ((runnernode.nodetype = unequaln) and
  1286. (taddnode(runnernode).right.nodetype = niln)) then
  1287. begin
  1288. runnernode := tunarynode(runnernode).left;
  1289. { in case of in_assigned_x, there's a callparan in between }
  1290. if (runnernode.nodetype = callparan) then
  1291. runnernode := tcallparanode(runnernode).left;
  1292. while (runnernode.nodetype = typeconvn) do
  1293. runnernode := ttypeconvnode(runnernode).left;
  1294. { is there an "x := x(^).somefield"? }
  1295. assignmentnode := findassignment(right,runnernode);
  1296. if assigned(assignmentnode) then
  1297. begin
  1298. prefetchcode := internalstatements(prefetchstatements);
  1299. addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
  1300. cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
  1301. addstatement(prefetchstatements,right);
  1302. right := prefetchcode;
  1303. typecheckpass(right);
  1304. end;
  1305. end;
  1306. end;
  1307. {$endif prefetchnext}
  1308. end;
  1309. {$ifdef state_tracking}
  1310. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  1311. var condition:Tnode;
  1312. code:Tnode;
  1313. done:boolean;
  1314. value:boolean;
  1315. change:boolean;
  1316. firsttest:boolean;
  1317. factval:Tnode;
  1318. begin
  1319. track_state_pass:=false;
  1320. done:=false;
  1321. firsttest:=true;
  1322. {For repeat until statements, first do a pass through the code.}
  1323. if not(lnf_testatbegin in flags) then
  1324. begin
  1325. code:=right.getcopy;
  1326. if code.track_state_pass(exec_known) then
  1327. track_state_pass:=true;
  1328. code.free;
  1329. end;
  1330. repeat
  1331. condition:=left.getcopy;
  1332. code:=right.getcopy;
  1333. change:=condition.track_state_pass(exec_known);
  1334. factval:=aktstate.find_fact(left);
  1335. if factval<>nil then
  1336. begin
  1337. condition.free;
  1338. condition:=factval.getcopy;
  1339. change:=true;
  1340. end;
  1341. if change then
  1342. begin
  1343. track_state_pass:=true;
  1344. {Force new resultdef pass.}
  1345. condition.resultdef:=nil;
  1346. do_typecheckpass(condition);
  1347. end;
  1348. if is_constboolnode(condition) then
  1349. begin
  1350. {Try to turn a while loop into a repeat loop.}
  1351. if firsttest then
  1352. exclude(flags,testatbegin);
  1353. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  1354. if value then
  1355. begin
  1356. if code.track_state_pass(exec_known) then
  1357. track_state_pass:=true;
  1358. end
  1359. else
  1360. done:=true;
  1361. end
  1362. else
  1363. begin
  1364. {Remove any modified variables from the state.}
  1365. code.track_state_pass(false);
  1366. done:=true;
  1367. end;
  1368. code.free;
  1369. condition.free;
  1370. firsttest:=false;
  1371. until done;
  1372. {The loop condition is also known, for example:
  1373. while i<10 do
  1374. begin
  1375. ...
  1376. end;
  1377. When the loop is done, we do know that i<10 = false.
  1378. }
  1379. condition:=left.getcopy;
  1380. if condition.track_state_pass(exec_known) then
  1381. begin
  1382. track_state_pass:=true;
  1383. {Force new resultdef pass.}
  1384. condition.resultdef:=nil;
  1385. do_typecheckpass(condition);
  1386. end;
  1387. if not is_constboolnode(condition) then
  1388. aktstate.store_fact(condition,
  1389. cordconstnode.create(byte(checknegate),pasbool1type,true))
  1390. else
  1391. condition.free;
  1392. end;
  1393. {$endif}
  1394. {*****************************************************************************
  1395. TIFNODE
  1396. *****************************************************************************}
  1397. constructor tifnode.create(l,r,_t1 : tnode);
  1398. begin
  1399. inherited create(ifn,l,r,_t1,nil);
  1400. end;
  1401. constructor tifnode.create_internal(l,r,_t1 : tnode);
  1402. begin
  1403. create(l,r,_t1);
  1404. include(flags,nf_internal);
  1405. end;
  1406. {$ifndef llvm}
  1407. {$if defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64) or defined(riscv)}
  1408. {$define HAS_MINMAX_INTRINSICS}
  1409. {$endif defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64) or defined(riscv)}
  1410. {$endif llvm}
  1411. function tifnode.internalsimplify(warn: boolean) : tnode;
  1412. {$if defined(HAS_MINMAX_INTRINSICS)}
  1413. var
  1414. thenstmnt, elsestmnt: tnode;
  1415. in_nr: tinlinenumber;
  1416. paratype: tdef;
  1417. {$endif defined(HAS_MINMAX_INTRINSICS)}
  1418. begin
  1419. result:=nil;
  1420. { optimize constant expressions }
  1421. if (left.nodetype=ordconstn) then
  1422. begin
  1423. if tordconstnode(left).value.uvalue<>0 then
  1424. begin
  1425. if assigned(right) then
  1426. result:=right
  1427. else
  1428. result:=cnothingnode.create;
  1429. right:=nil;
  1430. if warn and assigned(t1) and not(nf_internal in left.flags) then
  1431. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  1432. end
  1433. else
  1434. begin
  1435. if assigned(t1) then
  1436. result:=t1
  1437. else
  1438. result:=cnothingnode.create;
  1439. t1:=nil;
  1440. if warn and assigned(right) and not(nf_internal in left.flags) then
  1441. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1442. end;
  1443. end;
  1444. {$if defined(HAS_MINMAX_INTRINSICS)}
  1445. { use min/max intrinsic?
  1446. convert (with <op> being <, >, >=, <=
  1447. if a <op> b then
  1448. x:=a
  1449. else
  1450. x:=b;
  1451. and
  1452. if a <op> b then
  1453. x:=a;
  1454. into appropriate min/max intrinsics
  1455. }
  1456. elsestmnt:=nil;
  1457. in_nr:=Default(tinlinenumber);
  1458. if (cs_opt_level2 in current_settings.optimizerswitches) and
  1459. (left.nodetype in [gtn,gten,ltn,lten]) and IsSingleStatement(right,thenstmnt) and
  1460. ((t1=nil) or IsSingleStatement(t1,elsestmnt)) and
  1461. (thenstmnt.nodetype=assignn) and ((t1=nil) or (elsestmnt.nodetype=assignn)) and
  1462. not(might_have_sideeffects(left)) and
  1463. ((t1=nil) or tassignmentnode(thenstmnt).left.isequal(tassignmentnode(elsestmnt).left)) and
  1464. {$if defined(i386) or defined(x86_64)}
  1465. {$ifdef i386}
  1466. (((current_settings.fputype>=fpu_sse) and is_single(tassignmentnode(thenstmnt).left.resultdef)) or
  1467. ((current_settings.fputype>=fpu_sse2) and is_double(tassignmentnode(thenstmnt).left.resultdef)) or
  1468. ((CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) and is_32bitint(tassignmentnode(thenstmnt).left.resultdef))
  1469. ) and
  1470. {$else i386}
  1471. (is_single(tassignmentnode(thenstmnt).left.resultdef) or
  1472. is_double(tassignmentnode(thenstmnt).left.resultdef) or
  1473. is_32bitint(tassignmentnode(thenstmnt).left.resultdef) or
  1474. is_64bitint(tassignmentnode(thenstmnt).left.resultdef)
  1475. ) and
  1476. {$endif i386}
  1477. {$endif defined(i386) or defined(x86_64)}
  1478. {$if defined(xtensa)}
  1479. (CPUXTENSA_HAS_MINMAX in cpu_capabilities[current_settings.cputype]) and is_32bitint(tassignmentnode(thenstmnt).right.resultdef) and
  1480. {$endif defined(xtensa)}
  1481. {$if defined(aarch64)}
  1482. (is_single(tassignmentnode(thenstmnt).left.resultdef) or is_double(tassignmentnode(thenstmnt).left.resultdef) or
  1483. is_32bitint(tassignmentnode(thenstmnt).left.resultdef) or is_64bitint(tassignmentnode(thenstmnt).left.resultdef)) and
  1484. {$endif defined(aarch64)}
  1485. {$if defined(riscv)}
  1486. { RiscV fmin/fmax/fminm/fmaxm uses the IEEE semantics (2008 or 201x) of min/max regarding NaN (using either
  1487. always the NaN or non-NaN operand instead of the second one in case on is NaN), so
  1488. we can use them only when fast math is on }
  1489. ((cs_opt_fastmath in current_settings.optimizerswitches) and
  1490. ((is_single(tassignmentnode(thenstmnt).left.resultdef) and (CPURV_HAS_F in cpu_capabilities[current_settings.cputype])) or
  1491. (is_double(tassignmentnode(thenstmnt).left.resultdef) and (CPURV_HAS_D in cpu_capabilities[current_settings.cputype])) or
  1492. (is_quad(tassignmentnode(thenstmnt).left.resultdef) and (CPURV_HAS_Q in cpu_capabilities[current_settings.cputype])))) and
  1493. {$endif defined(riscv)}
  1494. (
  1495. { the right size of the assignment in the then clause must either }
  1496. { equal to the left ... }
  1497. (tassignmentnode(thenstmnt).right.isequal(taddnode(left).left) and
  1498. { ... and the else clause must be either not exist }
  1499. { and the left side of the assignment in the then clause must be }
  1500. { equal to the right operand of the comparison operator }
  1501. (
  1502. ((t1=nil) and (tassignmentnode(thenstmnt).left.isequal(taddnode(left).right))) or
  1503. { or the else clause exists and the right side of the assignment in the else clause }
  1504. { must be equal to the right side of the comparison operator }
  1505. (assigned(elsestmnt) and tassignmentnode(elsestmnt).right.isequal(taddnode(left).right)))
  1506. ) or
  1507. { ... or right operand of the comparison operator }
  1508. (tassignmentnode(thenstmnt).right.isequal(taddnode(left).right) and
  1509. { ... and the else clause must be either not exist }
  1510. { and the left side of the assignment in the then clause must be }
  1511. { equal to the left operand of the comparison operator }
  1512. (
  1513. ((t1=nil) and (tassignmentnode(thenstmnt).left.isequal(taddnode(left).left))) or
  1514. { or the else clause exists and the right side of the assignment in the else clause }
  1515. { must be equal to the left side of the comparison operator }
  1516. (assigned(elsestmnt) and tassignmentnode(elsestmnt).right.isequal(taddnode(left).left))
  1517. )
  1518. )
  1519. ) then
  1520. begin
  1521. paratype:=tassignmentnode(thenstmnt).left.resultdef;
  1522. if ((left.nodetype in [gtn,gten]) and
  1523. tassignmentnode(thenstmnt).right.isequal(taddnode(left).left)) or
  1524. ((left.nodetype in [ltn,lten]) and
  1525. tassignmentnode(thenstmnt).right.isequal(taddnode(left).right)) then
  1526. begin
  1527. if is_double(paratype) then
  1528. in_nr:=in_max_double
  1529. else if is_single(paratype) then
  1530. in_nr:=in_max_single
  1531. else if is_u32bitint(paratype) then
  1532. in_nr:=in_max_dword
  1533. else if is_s32bitint(paratype) then
  1534. in_nr:=in_max_longint
  1535. else if is_u64bitint(paratype) then
  1536. in_nr:=in_max_qword
  1537. else if is_s64bitint(paratype) then
  1538. in_nr:=in_max_int64;
  1539. end
  1540. else
  1541. begin
  1542. if is_double(paratype) then
  1543. in_nr:=in_min_double
  1544. else if is_single(paratype) then
  1545. in_nr:=in_min_single
  1546. else if is_u32bitint(paratype) then
  1547. in_nr:=in_min_dword
  1548. else if is_s32bitint(paratype) then
  1549. in_nr:=in_min_longint
  1550. else if is_u64bitint(paratype) then
  1551. in_nr:=in_min_qword
  1552. else if is_s64bitint(paratype) then
  1553. in_nr:=in_min_int64;
  1554. end;
  1555. { for inline nodes, the first parameter is the last one in the linked list
  1556. Due to the defined behaviour for the min/max intrinsics that in case of a NaN
  1557. the second parameter is taken, we have to put the else part into the second parameter
  1558. thus pass it to the first callparanode call }
  1559. if t1=nil then
  1560. Result:=cassignmentnode.create_internal(tassignmentnode(thenstmnt).left.getcopy,
  1561. cinlinenode.create(in_nr,false,ccallparanode.create(tassignmentnode(thenstmnt).left.getcopy,
  1562. ccallparanode.create(tassignmentnode(thenstmnt).right.getcopy,nil)))
  1563. )
  1564. else
  1565. Result:=cassignmentnode.create_internal(tassignmentnode(thenstmnt).left.getcopy,
  1566. cinlinenode.create(in_nr,false,ccallparanode.create(tassignmentnode(elsestmnt).right.getcopy,
  1567. ccallparanode.create(tassignmentnode(thenstmnt).right.getcopy,nil)))
  1568. );
  1569. node_reset_pass1_write(Result);
  1570. end;
  1571. {$endif defined(HAS_MINMAX_INTRINSICS)}
  1572. end;
  1573. function tifnode.simplify(forinline : boolean) : tnode;
  1574. begin
  1575. result:=internalsimplify(false);
  1576. end;
  1577. function tifnode.pass_typecheck:tnode;
  1578. begin
  1579. result:=nil;
  1580. resultdef:=voidtype;
  1581. typecheckpass(left);
  1582. { tp procvar support }
  1583. maybe_call_procvar(left,true);
  1584. { if path }
  1585. if assigned(right) then
  1586. typecheckpass(right);
  1587. { else path }
  1588. if assigned(t1) then
  1589. typecheckpass(t1);
  1590. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1591. if codegenerror then
  1592. exit;
  1593. if not(is_boolean(left.resultdef)) and
  1594. not(is_typeparam(left.resultdef)) then
  1595. inserttypeconv(left,pasbool1type);
  1596. result:=internalsimplify(not(nf_internal in flags));
  1597. end;
  1598. function tifnode.pass_1 : tnode;
  1599. begin
  1600. result:=nil;
  1601. expectloc:=LOC_VOID;
  1602. firstpass(left);
  1603. { if path }
  1604. if assigned(right) then
  1605. firstpass(right);
  1606. { else path }
  1607. if assigned(t1) then
  1608. firstpass(t1);
  1609. { leave if we've got an error in one of the paths }
  1610. if codegenerror then
  1611. exit;
  1612. end;
  1613. {*****************************************************************************
  1614. TFORNODE
  1615. *****************************************************************************}
  1616. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  1617. begin
  1618. inherited create(forn,l,r,_t1,_t2);
  1619. if back then
  1620. include(loopflags,lnf_backward);
  1621. include(loopflags,lnf_testatbegin);
  1622. end;
  1623. destructor tfornode.destroy;
  1624. begin
  1625. loopiteration.free;
  1626. inherited destroy;
  1627. end;
  1628. function tfornode.simplify(forinline : boolean) : tnode;
  1629. begin
  1630. result:=nil;
  1631. { Can we spare the first comparision? }
  1632. if (t1.nodetype=ordconstn) and
  1633. (right.nodetype=ordconstn) and
  1634. (
  1635. (
  1636. (lnf_backward in loopflags) and
  1637. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1638. ) or
  1639. (
  1640. not(lnf_backward in loopflags) and
  1641. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1642. )
  1643. ) then
  1644. exclude(loopflags,lnf_testatbegin);
  1645. if (t1.nodetype=ordconstn) and
  1646. (right.nodetype=ordconstn) and
  1647. (
  1648. (
  1649. (lnf_backward in loopflags) and
  1650. (tordconstnode(right).value<tordconstnode(t1).value)
  1651. ) or
  1652. (
  1653. not(lnf_backward in loopflags) and
  1654. (tordconstnode(right).value>tordconstnode(t1).value)
  1655. )
  1656. ) then
  1657. result:=cnothingnode.create;
  1658. end;
  1659. function tfornode.pass_typecheck:tnode;
  1660. var
  1661. res : tnode;
  1662. rangedef: tdef;
  1663. begin
  1664. result:=nil;
  1665. resultdef:=voidtype;
  1666. { process the loopvar, from and to, varstates are already set }
  1667. typecheckpass(left);
  1668. typecheckpass(right);
  1669. typecheckpass(t1);
  1670. set_varstate(left,vs_written,[]);
  1671. { Make sure that the loop var and the
  1672. from and to values are compatible types }
  1673. if not(m_iso in current_settings.modeswitches) then
  1674. rangedef:=left.resultdef
  1675. else
  1676. rangedef:=get_iso_range_type(left.resultdef);
  1677. check_ranges(right.fileinfo,right,rangedef);
  1678. inserttypeconv(right,rangedef);
  1679. check_ranges(t1.fileinfo,t1,rangedef);
  1680. inserttypeconv(t1,rangedef);
  1681. if assigned(t2) then
  1682. typecheckpass(t2);
  1683. result:=simplify(false);
  1684. { loop unrolling }
  1685. if not(assigned(result)) and
  1686. (cs_opt_loopunroll in current_settings.optimizerswitches) and
  1687. assigned(t2) and
  1688. { statements must be error free }
  1689. not(tnf_error in t2.transientflags) then
  1690. begin
  1691. typecheckpass(t2);
  1692. res:=t2.simplify(false);
  1693. if assigned(res) then
  1694. t2:=res;
  1695. res:=unroll_loop(self);
  1696. if assigned(res) then
  1697. begin
  1698. typecheckpass(res);
  1699. result:=res;
  1700. exit;
  1701. end;
  1702. end;
  1703. end;
  1704. function tfornode.pass_1 : tnode;
  1705. begin
  1706. result:=nil;
  1707. expectloc:=LOC_VOID;
  1708. firstpass(left);
  1709. firstpass(right);
  1710. firstpass(t1);
  1711. if assigned(t2) then
  1712. firstpass(t2);
  1713. end;
  1714. function tfornode.makewhileloop : tnode;
  1715. var
  1716. ifblock,loopblock : tblocknode;
  1717. ifstatements,statements,loopstatements : tstatementnode;
  1718. fromtemp,totemp : ttempcreatenode;
  1719. do_loopvar_at_end : Boolean;
  1720. { if the lower and/or upper bound are variable, we need a surrounding if }
  1721. needsifblock : Boolean;
  1722. cond : tnodetype;
  1723. fromexpr : tnode;
  1724. toexpr, leftcopy: tnode;
  1725. { if the upper bound is not constant, it must be store in a temp initially }
  1726. usetotemp : boolean;
  1727. { if the lower bound is not constant, it must be store in a temp before calculating the upper bound }
  1728. usefromtemp : boolean;
  1729. storefilepos: tfileposinfo;
  1730. countermin, countermax: Tconstexprint;
  1731. procedure iterate_counter(var s : tstatementnode;fw : boolean);
  1732. var
  1733. leftcopy: tnode;
  1734. begin
  1735. { get rid of nf_write etc. as the left node is now only read }
  1736. leftcopy:=left.getcopy;
  1737. node_reset_flags(leftcopy,[nf_modify,nf_write],[tnf_pass1_done]);
  1738. if fw then
  1739. addstatement(s,
  1740. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_succ_x,false,leftcopy)))
  1741. else
  1742. addstatement(s,
  1743. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_pred_x,false,leftcopy)));
  1744. end;
  1745. function iterate_counter_func(arg : tnode;fw : boolean) : tnode;
  1746. begin
  1747. if fw then
  1748. result:=cinlinenode.createintern(in_succ_x,false,arg)
  1749. else
  1750. result:=cinlinenode.createintern(in_pred_x,false,arg);
  1751. end;
  1752. begin
  1753. result:=nil;
  1754. totemp:=nil;
  1755. fromtemp:=nil;
  1756. storefilepos:=current_filepos;
  1757. current_filepos:=fileinfo;
  1758. case left.resultdef.typ of
  1759. enumdef:
  1760. begin
  1761. countermin:=tenumdef(left.resultdef).min;
  1762. countermax:=tenumdef(left.resultdef).max;
  1763. end;
  1764. orddef:
  1765. begin
  1766. countermin:=torddef(left.resultdef).low;
  1767. countermax:=torddef(left.resultdef).high;
  1768. end;
  1769. else
  1770. Internalerror(2020012601);
  1771. end;
  1772. { check if we can pred/succ the loop var at the end }
  1773. do_loopvar_at_end:=(lnf_dont_mind_loopvar_on_exit in loopflags) and
  1774. is_constnode(t1) and
  1775. { we cannot test at the end after the pred/succ if the to value is equal to the max./min. value of the counter variable
  1776. because we either get an overflow/underflow or the compiler removes the check as it never can be true }
  1777. { checking just the min./max. value depending on the pure size of the counter does not work as the check might
  1778. get optimized away
  1779. not(not(lnf_backward in loopflags) and not(is_signed(left.resultdef)) and (get_ordinal_value(t1)=((1 shl (left.resultdef.size*8))-1))) and
  1780. not(not(lnf_backward in loopflags) and is_signed(left.resultdef) and (get_ordinal_value(t1)=((1 shl (left.resultdef.size*8-1))-1))) and
  1781. not((lnf_backward in loopflags) and not(is_signed(left.resultdef)) and (get_ordinal_value(t1)=0)) and
  1782. not((lnf_backward in loopflags) and is_signed(left.resultdef) and (get_ordinal_value(t1)=(-Tconstexprint(1 shl (left.resultdef.size*8-1))))) and
  1783. }
  1784. not(not(lnf_backward in loopflags) and (get_ordinal_value(t1)=countermax)) and
  1785. not((lnf_backward in loopflags) and (get_ordinal_value(t1)=countermin)) and
  1786. { neither might the for loop contain a continue statement as continue in a while loop would skip the increment at the end
  1787. of the loop, this could be overcome by replacing the continue statement with an pred/succ; continue sequence }
  1788. not(has_node_of_type(t2,[continuen])) and
  1789. { if the loop is unrolled and there is a jump into the loop,
  1790. then we can't do the trick with incrementing the loop var only at the
  1791. end
  1792. }
  1793. not(assigned(entrylabel));
  1794. needsifblock:=not(is_constnode(right)) or not(is_constnode(t1));
  1795. { convert the for loop into a while loop }
  1796. result:=internalstatements(statements);
  1797. ifblock:=internalstatements(ifstatements);
  1798. loopblock:=internalstatements(loopstatements);
  1799. usefromtemp:=(might_have_sideeffects(t1) and not(is_const(right))) or (node_complexity(right)>1);
  1800. usetotemp:=not(is_const(t1));
  1801. if needsifblock then
  1802. begin
  1803. { do not generate a temp. for the from node, if it is a const, it can be copied directly since
  1804. no side effect might change it }
  1805. if usefromtemp then
  1806. begin
  1807. fromtemp:=ctempcreatenode.create(right.resultdef,right.resultdef.size,tt_persistent,true);
  1808. { the if block might be optimized out, so we put the deletetempnode after the if-block, however,
  1809. this causes a long life time of the fromtemp. If the final regsync is left away, the reg. allocator
  1810. figures out the needed life time. As their are no loops involved between the uses of the fromtemp,
  1811. this does no hurt }
  1812. fromtemp.includetempflag(ti_no_final_regsync);
  1813. addstatement(statements,fromtemp);
  1814. { while it would be beneficial to fold the initial reverse succ/pred into this assignment, this is
  1815. not possible because it might wrap around and the if check later on goes wrong }
  1816. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(fromtemp),right.getcopy));
  1817. end;
  1818. if usetotemp then
  1819. begin
  1820. totemp:=ctempcreatenode.create(t1.resultdef,t1.resultdef.size,tt_persistent,true);
  1821. addstatement(statements,totemp);
  1822. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(totemp),t1.getcopy));
  1823. end;
  1824. if usefromtemp then
  1825. begin
  1826. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,ctemprefnode.create(fromtemp)));
  1827. if not(do_loopvar_at_end) then
  1828. iterate_counter(ifstatements,lnf_backward in loopflags);
  1829. end
  1830. else
  1831. begin
  1832. if not(do_loopvar_at_end) then
  1833. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
  1834. iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
  1835. else
  1836. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
  1837. end;
  1838. end
  1839. else
  1840. begin
  1841. if not(do_loopvar_at_end) then
  1842. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
  1843. iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
  1844. else
  1845. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
  1846. end;
  1847. if assigned(entrylabel) then
  1848. addstatement(ifstatements,cgotonode.create(tlabelnode(entrylabel).labsym));
  1849. if not(do_loopvar_at_end) then
  1850. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1851. { avoid copying t2, it is used only once and it might be big }
  1852. addstatement(loopstatements,t2);
  1853. t2:=nil;
  1854. if do_loopvar_at_end then
  1855. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1856. if do_loopvar_at_end then
  1857. begin
  1858. if lnf_backward in loopflags then
  1859. cond:=ltn
  1860. else
  1861. cond:=gtn;
  1862. end
  1863. else
  1864. begin
  1865. if lnf_backward in loopflags then
  1866. cond:=lten
  1867. else
  1868. cond:=gten;
  1869. end;
  1870. { get rid of nf_write etc. as the left node is now only read }
  1871. leftcopy:=left.getcopy;
  1872. node_reset_flags(leftcopy,[nf_modify,nf_write],[tnf_pass1_done]);
  1873. if needsifblock then
  1874. begin
  1875. if usetotemp then
  1876. toexpr:=ctemprefnode.create(totemp)
  1877. else
  1878. toexpr:=t1.getcopy;
  1879. { checking against zero might improve the generated assembler,
  1880. doing this transformation for other values is normally not beneficial }
  1881. if do_loopvar_at_end and (lnf_backward in loopflags) and is_constintnode(toexpr) and (tordconstnode(toexpr).value=1) and
  1882. (countermin<tordconstnode(toexpr).value) then
  1883. begin
  1884. tordconstnode(toexpr).value:=tordconstnode(toexpr).value-1;
  1885. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(equaln,leftcopy,toexpr),loopblock,false,true))
  1886. end
  1887. else
  1888. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,leftcopy,toexpr),loopblock,false,true));
  1889. if usefromtemp then
  1890. fromexpr:=ctemprefnode.create(fromtemp)
  1891. else
  1892. fromexpr:=right.getcopy;
  1893. if usetotemp then
  1894. toexpr:=ctemprefnode.create(totemp)
  1895. else
  1896. toexpr:=t1.getcopy;
  1897. if lnf_backward in loopflags then
  1898. addstatement(statements,cifnode.create(caddnode.create_internal(gten,
  1899. fromexpr,toexpr),ifblock,nil))
  1900. else
  1901. addstatement(statements,cifnode.create(caddnode.create_internal(lten,
  1902. fromexpr,toexpr),ifblock,nil));
  1903. if usetotemp then
  1904. addstatement(statements,ctempdeletenode.create(totemp));
  1905. if usefromtemp then
  1906. addstatement(statements,ctempdeletenode.create(fromtemp));
  1907. end
  1908. else
  1909. begin
  1910. { is a simple comparison for equality sufficient? }
  1911. if do_loopvar_at_end and (lnf_backward in loopflags) and (lnf_counter_not_used in loopflags) then
  1912. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(equaln,leftcopy,
  1913. caddnode.create_internal(subn,t1.getcopy,cordconstnode.create(1,t1.resultdef,false))),loopblock,false,true))
  1914. else
  1915. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,leftcopy,t1.getcopy),loopblock,false,true));
  1916. addstatement(statements,ifblock);
  1917. end;
  1918. current_filepos:=storefilepos;
  1919. end;
  1920. {*****************************************************************************
  1921. TEXITNODE
  1922. *****************************************************************************}
  1923. constructor texitnode.create(l:tnode);
  1924. begin
  1925. inherited create(exitn,l);
  1926. if assigned(left) then
  1927. begin
  1928. { add assignment to funcretsym }
  1929. left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
  1930. left:=cassignmentnode.create(
  1931. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1932. left);
  1933. end;
  1934. end;
  1935. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1936. begin
  1937. inherited ppuload(t,ppufile);
  1938. end;
  1939. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1940. begin
  1941. inherited ppuwrite(ppufile);
  1942. end;
  1943. function texitnode.pass_typecheck:tnode;
  1944. var
  1945. newstatement : tstatementnode;
  1946. ressym: tsym;
  1947. resdef: tdef;
  1948. begin
  1949. result:=nil;
  1950. newstatement:=nil;
  1951. if assigned(left) then
  1952. begin
  1953. result:=internalstatements(newstatement);
  1954. addstatement(newstatement,left);
  1955. left:=nil;
  1956. end;
  1957. { if the function result has been migrated to the parentfpstruct,
  1958. we have to load it back to the original location (from which the
  1959. code generator will load it into the function result location),
  1960. because the code to this that we add in tnodeutils.wrap_proc_body()
  1961. gets inserted before the exit label to which this node will jump }
  1962. if (target_info.system in systems_fpnestedstruct) and
  1963. not(nf_internal in flags) and
  1964. current_procinfo.procdef.get_funcretsym_info(ressym,resdef) and
  1965. (tabstractnormalvarsym(ressym).inparentfpstruct) then
  1966. begin
  1967. if not assigned(result) then
  1968. result:=internalstatements(newstatement);
  1969. cnodeutils.load_parentfpstruct_nested_funcret(ressym,newstatement);
  1970. end;
  1971. if assigned(result) then
  1972. begin
  1973. addstatement(newstatement,self.getcopy);
  1974. { ensure we don't insert the function result loading code again for
  1975. this node }
  1976. include(newstatement.left.flags,nf_internal);
  1977. end;
  1978. resultdef:=voidtype;
  1979. end;
  1980. function texitnode.pass_1 : tnode;
  1981. begin
  1982. result:=nil;
  1983. expectloc:=LOC_VOID;
  1984. if assigned(left) then
  1985. internalerror(2011052801);
  1986. end;
  1987. {*****************************************************************************
  1988. TBREAKNODE
  1989. *****************************************************************************}
  1990. constructor tbreaknode.create;
  1991. begin
  1992. inherited create(breakn);
  1993. end;
  1994. function tbreaknode.pass_typecheck:tnode;
  1995. begin
  1996. result:=nil;
  1997. resultdef:=voidtype;
  1998. end;
  1999. function tbreaknode.pass_1 : tnode;
  2000. begin
  2001. result:=nil;
  2002. expectloc:=LOC_VOID;
  2003. end;
  2004. {*****************************************************************************
  2005. TCONTINUENODE
  2006. *****************************************************************************}
  2007. constructor tcontinuenode.create;
  2008. begin
  2009. inherited create(continuen);
  2010. end;
  2011. function tcontinuenode.pass_typecheck:tnode;
  2012. begin
  2013. result:=nil;
  2014. resultdef:=voidtype;
  2015. end;
  2016. function tcontinuenode.pass_1 : tnode;
  2017. begin
  2018. result:=nil;
  2019. expectloc:=LOC_VOID;
  2020. end;
  2021. {*****************************************************************************
  2022. TGOTONODE
  2023. *****************************************************************************}
  2024. constructor tgotonode.create(p : tlabelsym);
  2025. begin
  2026. inherited create(goton);
  2027. exceptionblock:=current_exceptblock;
  2028. labelnode:=nil;
  2029. labelsym:=p;
  2030. end;
  2031. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2032. begin
  2033. inherited ppuload(t,ppufile);
  2034. labelnodeidx:=ppufile.getlongint;
  2035. exceptionblock:=ppufile.getbyte;
  2036. end;
  2037. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  2038. begin
  2039. inherited ppuwrite(ppufile);
  2040. labelnodeidx:=labelnode.ppuidx;
  2041. ppufile.putlongint(labelnodeidx);
  2042. ppufile.putbyte(exceptionblock);
  2043. end;
  2044. procedure tgotonode.buildderefimpl;
  2045. begin
  2046. inherited buildderefimpl;
  2047. end;
  2048. procedure tgotonode.derefimpl;
  2049. begin
  2050. inherited derefimpl;
  2051. end;
  2052. procedure tgotonode.resolveppuidx;
  2053. begin
  2054. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  2055. if labelnode.nodetype<>labeln then
  2056. internalerror(200809021);
  2057. end;
  2058. function tgotonode.pass_typecheck:tnode;
  2059. begin
  2060. result:=nil;
  2061. resultdef:=voidtype;
  2062. end;
  2063. function tgotonode.pass_1 : tnode;
  2064. var
  2065. p2 : tprocinfo;
  2066. begin
  2067. result:=nil;
  2068. expectloc:=LOC_VOID;
  2069. { The labelnode can already be set when
  2070. this node was copied }
  2071. if not(assigned(labelnode)) then
  2072. begin
  2073. { inner procedure goto? }
  2074. if assigned(labelsym.code) and
  2075. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  2076. { generated by the optimizer? }
  2077. not(assigned(labelsym.owner))) then
  2078. labelnode:=tlabelnode(labelsym.code)
  2079. else if ((m_non_local_goto in current_settings.modeswitches) and
  2080. assigned(labelsym.owner)) or
  2081. { nested exits don't need the non local goto switch }
  2082. (labelsym.realname='$nestedexit') then
  2083. begin
  2084. if current_procinfo.procdef.parast.symtablelevel>=labelsym.owner.symtablelevel then
  2085. begin
  2086. { don't mess with the exception blocks, global gotos in/out side exception blocks are not allowed }
  2087. if exceptionblock>0 then
  2088. CGMessage(cg_e_goto_inout_of_exception_block);
  2089. { goto across procedures using exception?
  2090. this is not allowed because we cannot
  2091. easily unwind the exception frame
  2092. stack
  2093. }
  2094. p2:=current_procinfo;
  2095. while true do
  2096. begin
  2097. if ((cs_implicit_exceptions in current_settings.moduleswitches) and ((p2.flags*[pi_needs_implicit_finally,pi_has_implicit_finally])<>[])) or
  2098. ((p2.flags*[pi_uses_exceptions])<>[]) then
  2099. Message(cg_e_goto_across_procedures_with_exceptions_not_allowed);
  2100. if labelsym.owner=p2.procdef.localst then
  2101. break;
  2102. p2:=p2.parent
  2103. end;
  2104. if assigned(labelsym.jumpbuf) then
  2105. begin
  2106. result:=ccallnode.createintern('fpc_longjmp',
  2107. ccallparanode.create(cordconstnode.create(1,sinttype,true),
  2108. ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
  2109. nil)));
  2110. end
  2111. else
  2112. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  2113. end
  2114. else
  2115. CGMessagePos(self.fileinfo,cg_e_interprocedural_goto_only_to_outer_scope_allowed);
  2116. end
  2117. else
  2118. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  2119. end;
  2120. { check if we don't mess with exception blocks }
  2121. if assigned(labelnode) and
  2122. (exceptionblock<>labelnode.exceptionblock) then
  2123. CGMessage(cg_e_goto_inout_of_exception_block);
  2124. end;
  2125. function tgotonode.dogetcopy : tnode;
  2126. var
  2127. p : tgotonode;
  2128. begin
  2129. p:=tgotonode(inherited dogetcopy);
  2130. p.exceptionblock:=exceptionblock;
  2131. { generate labelnode if not done yet }
  2132. if not(assigned(labelnode)) then
  2133. begin
  2134. if assigned(labelsym) and assigned(labelsym.code) then
  2135. labelnode:=tlabelnode(labelsym.code)
  2136. end;
  2137. p.labelsym:=labelsym;
  2138. { do not copy the label node here as we do not know if the label node is part of the tree or not,
  2139. this will be fixed after the copying in node.setuplabelnode: if the labelnode has copiedto set,
  2140. labelnode of the goto node is update }
  2141. if assigned(labelnode) then
  2142. p.labelnode:=labelnode
  2143. else
  2144. begin
  2145. { don't trigger IE when there was already an error, i.e. the
  2146. label is not defined. See tw11763 (PFV) }
  2147. if (errorcount=0) and
  2148. { don't trigger IE if it's a global goto }
  2149. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  2150. not(assigned(labelsym.owner))) then
  2151. internalerror(200610291);
  2152. end;
  2153. result:=p;
  2154. end;
  2155. function tgotonode.docompare(p: tnode): boolean;
  2156. begin
  2157. docompare := false;
  2158. end;
  2159. {*****************************************************************************
  2160. TLABELNODE
  2161. *****************************************************************************}
  2162. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  2163. begin
  2164. inherited create(labeln);
  2165. exceptionblock:=current_exceptblock;
  2166. labsym:=alabsym;
  2167. { Register labelnode in labelsym }
  2168. labsym.code:=self;
  2169. end;
  2170. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2171. begin
  2172. inherited ppuload(t,ppufile);
  2173. exceptionblock:=ppufile.getbyte;
  2174. end;
  2175. destructor tlabelnode.destroy;
  2176. begin
  2177. if assigned(labsym) then
  2178. begin
  2179. if not assigned(labsym.Owner) then
  2180. labsym.Free // Free labelsym if it has no owner
  2181. else
  2182. if labsym.code=pointer(self) then
  2183. begin
  2184. { Remove reference in labelsym, this is to prevent
  2185. goto's to this label }
  2186. labsym.code:=nil;
  2187. end;
  2188. end;
  2189. inherited destroy;
  2190. end;
  2191. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  2192. begin
  2193. inherited ppuwrite(ppufile);
  2194. ppufile.putbyte(exceptionblock);
  2195. end;
  2196. procedure tlabelnode.buildderefimpl;
  2197. begin
  2198. inherited buildderefimpl;
  2199. end;
  2200. procedure tlabelnode.derefimpl;
  2201. begin
  2202. inherited derefimpl;
  2203. end;
  2204. function tlabelnode.pass_typecheck:tnode;
  2205. begin
  2206. result:=nil;
  2207. resultdef:=voidtype;
  2208. end;
  2209. function tlabelnode.pass_1 : tnode;
  2210. begin
  2211. result:=nil;
  2212. expectloc:=LOC_VOID;
  2213. if not (nf_internal in flags) then
  2214. include(current_procinfo.flags,pi_has_label);
  2215. if (m_non_local_goto in current_settings.modeswitches) and
  2216. { the owner can be Nil for internal labels }
  2217. assigned(labsym.owner) and
  2218. (current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
  2219. CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
  2220. end;
  2221. function tlabelnode.dogetcopy : tnode;
  2222. begin
  2223. if not(assigned(copiedto)) then
  2224. copiedto:=tlabelnode(inherited dogetcopy);
  2225. copiedto.exceptionblock:=exceptionblock;
  2226. result:=copiedto;
  2227. end;
  2228. function tlabelnode.docompare(p: tnode): boolean;
  2229. begin
  2230. docompare := false;
  2231. end;
  2232. {*****************************************************************************
  2233. TRAISENODE
  2234. *****************************************************************************}
  2235. constructor traisenode.create(l,taddr,tframe:tnode);
  2236. begin
  2237. inherited create(raisen,l,taddr,tframe);
  2238. end;
  2239. function traisenode.pass_typecheck:tnode;
  2240. begin
  2241. result:=nil;
  2242. resultdef:=voidtype;
  2243. if assigned(left) then
  2244. begin
  2245. { first para must be a _class_ }
  2246. typecheckpass(left);
  2247. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2248. if codegenerror then
  2249. exit;
  2250. if not is_class(left.resultdef) and
  2251. not is_javaclass(left.resultdef) then
  2252. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2253. { insert needed typeconvs for addr,frame }
  2254. if assigned(right) then
  2255. begin
  2256. { addr }
  2257. typecheckpass(right);
  2258. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2259. inserttypeconv(right,voidcodepointertype);
  2260. { frame }
  2261. if assigned(third) then
  2262. begin
  2263. typecheckpass(third);
  2264. set_varstate(third,vs_read,[vsf_must_be_valid]);
  2265. inserttypeconv(third,voidpointertype);
  2266. end;
  2267. end;
  2268. end;
  2269. end;
  2270. function traisenode.pass_1 : tnode;
  2271. var
  2272. statements : tstatementnode;
  2273. current_addr : tlabelnode;
  2274. raisenode : tcallnode;
  2275. begin
  2276. result:=internalstatements(statements);
  2277. if assigned(left) then
  2278. begin
  2279. { first para must be a class }
  2280. firstpass(left);
  2281. { insert needed typeconvs for addr,frame }
  2282. if assigned(right) then
  2283. begin
  2284. { addr }
  2285. firstpass(right);
  2286. { frame }
  2287. if assigned(third) then
  2288. firstpass(third)
  2289. else
  2290. third:=cpointerconstnode.Create(0,voidpointertype);
  2291. end
  2292. else
  2293. begin
  2294. third:=cinlinenode.create(in_get_frame,false,nil);
  2295. current_addr:=clabelnode.create(cnothingnode.create,clabelsym.create('$raiseaddr'));
  2296. include(current_addr.flags,nf_internal);
  2297. addstatement(statements,current_addr);
  2298. right:=caddrnode.create(cloadnode.create(current_addr.labsym,current_addr.labsym.owner));
  2299. { raise address off by one so we are for sure inside the action area for the raise }
  2300. if tf_use_psabieh in target_info.flags then
  2301. right:=caddnode.create_internal(addn,right,cordconstnode.create(1,sizesinttype,false));
  2302. end;
  2303. raisenode:=ccallnode.createintern('fpc_raiseexception',
  2304. ccallparanode.create(third,
  2305. ccallparanode.create(right,
  2306. ccallparanode.create(left,nil)))
  2307. );
  2308. include(raisenode.callnodeflags,cnf_call_never_returns);
  2309. addstatement(statements,raisenode);
  2310. end
  2311. else
  2312. begin
  2313. addstatement(statements,ccallnode.createintern('fpc_popaddrstack',nil));
  2314. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  2315. include(raisenode.callnodeflags,cnf_call_never_returns);
  2316. addstatement(statements,raisenode);
  2317. end;
  2318. left:=nil;
  2319. right:=nil;
  2320. third:=nil;
  2321. end;
  2322. {*****************************************************************************
  2323. TTRYEXCEPTNODE
  2324. *****************************************************************************}
  2325. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  2326. begin
  2327. inherited create(tryexceptn,l,r,_t1,nil);
  2328. end;
  2329. function ttryexceptnode.pass_typecheck:tnode;
  2330. begin
  2331. result:=nil;
  2332. typecheckpass(left);
  2333. { on statements }
  2334. if assigned(right) then
  2335. typecheckpass(right);
  2336. { else block }
  2337. if assigned(t1) then
  2338. typecheckpass(t1);
  2339. resultdef:=voidtype;
  2340. end;
  2341. function ttryexceptnode.pass_1 : tnode;
  2342. begin
  2343. result:=nil;
  2344. expectloc:=LOC_VOID;
  2345. firstpass(left);
  2346. { on statements }
  2347. if assigned(right) then
  2348. firstpass(right);
  2349. { else block }
  2350. if assigned(t1) then
  2351. firstpass(t1);
  2352. include(current_procinfo.flags,pi_do_call);
  2353. include(current_procinfo.flags,pi_uses_exceptions);
  2354. adjust_estimated_stack_size;
  2355. end;
  2356. function ttryexceptnode.simplify(forinline: boolean): tnode;
  2357. begin
  2358. result:=nil;
  2359. { empty try -> can never raise exception -> do nothing }
  2360. if has_no_code(left) then
  2361. result:=cnothingnode.create;
  2362. end;
  2363. procedure ttryexceptnode.adjust_estimated_stack_size;
  2364. begin
  2365. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size*2);
  2366. end;
  2367. {*****************************************************************************
  2368. TTRYFINALLYNODE
  2369. *****************************************************************************}
  2370. constructor ttryfinallynode.create(l,r:tnode);
  2371. begin
  2372. inherited create(tryfinallyn,l,r,nil);
  2373. third:=nil;
  2374. implicitframe:=false;
  2375. end;
  2376. constructor ttryfinallynode.create_implicit(l,r:tnode);
  2377. begin
  2378. inherited create(tryfinallyn,l,r,nil);
  2379. third:=nil;
  2380. implicitframe:=true;
  2381. end;
  2382. function ttryfinallynode.pass_typecheck:tnode;
  2383. begin
  2384. result:=nil;
  2385. resultdef:=voidtype;
  2386. typecheckpass(left);
  2387. // "try block" is "used"? (JM)
  2388. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  2389. typecheckpass(right);
  2390. // "except block" is "used"? (JM)
  2391. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  2392. if assigned(third) then
  2393. begin
  2394. typecheckpass(third);
  2395. set_varstate(third,vs_readwritten,[vsf_must_be_valid]);
  2396. end;
  2397. end;
  2398. function ttryfinallynode.pass_1 : tnode;
  2399. begin
  2400. result:=nil;
  2401. expectloc:=LOC_VOID;
  2402. firstpass(left);
  2403. firstpass(right);
  2404. if assigned(third) then
  2405. firstpass(third);
  2406. include(current_procinfo.flags,pi_do_call);
  2407. { pi_uses_exceptions is an information for the optimizer and it
  2408. is only interested in exceptions if they appear inside the body,
  2409. so ignore implicit frames when setting the flag }
  2410. if not(implicitframe) then
  2411. include(current_procinfo.flags,pi_uses_exceptions);
  2412. adjust_estimated_stack_size;
  2413. end;
  2414. function ttryfinallynode.simplify(forinline : boolean): tnode;
  2415. begin
  2416. result:=nil;
  2417. { if the try contains no code, we can kill
  2418. the try and except and return only the
  2419. finally part }
  2420. if has_no_code(left) then
  2421. begin
  2422. result:=right;
  2423. right:=nil;
  2424. end
  2425. { if the finally block contains no code, we can kill
  2426. it and just return the try part }
  2427. else if has_no_code(right) and not(assigned(third)) and not(implicitframe) then
  2428. begin
  2429. result:=left;
  2430. left:=nil;
  2431. end;
  2432. end;
  2433. function ttryfinallynode.dogetcopy: tnode;
  2434. begin
  2435. result:=inherited dogetcopy;
  2436. ttryfinallynode(result).implicitframe:=implicitframe;
  2437. end;
  2438. procedure ttryfinallynode.adjust_estimated_stack_size;
  2439. begin
  2440. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size);
  2441. end;
  2442. {*****************************************************************************
  2443. TONNODE
  2444. *****************************************************************************}
  2445. constructor tonnode.create(l,r:tnode);
  2446. begin
  2447. inherited create(onn,l,r);
  2448. excepTSymtable:=nil;
  2449. excepttype:=nil;
  2450. end;
  2451. destructor tonnode.destroy;
  2452. begin
  2453. { copied nodes don't need to release the symtable }
  2454. if assigned(excepTSymtable) then
  2455. excepTSymtable.free;
  2456. inherited destroy;
  2457. end;
  2458. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2459. begin
  2460. inherited ppuload(t,ppufile);
  2461. excepTSymtable:=nil;
  2462. excepttype:=nil;
  2463. end;
  2464. function tonnode.dogetcopy : tnode;
  2465. var
  2466. n : tonnode;
  2467. begin
  2468. n:=tonnode(inherited dogetcopy);
  2469. if assigned(exceptsymtable) then
  2470. n.exceptsymtable:=exceptsymtable.getcopy
  2471. else
  2472. n.exceptsymtable:=nil;
  2473. n.excepttype:=excepttype;
  2474. result:=n;
  2475. end;
  2476. function tonnode.pass_typecheck:tnode;
  2477. begin
  2478. result:=nil;
  2479. resultdef:=voidtype;
  2480. if not is_class(excepttype) and
  2481. not is_javaclass(excepttype) then
  2482. CGMessage1(type_e_class_type_expected,excepttype.typename);
  2483. if assigned(left) then
  2484. typecheckpass(left);
  2485. if assigned(right) then
  2486. typecheckpass(right);
  2487. end;
  2488. function tonnode.pass_1 : tnode;
  2489. begin
  2490. result:=nil;
  2491. include(current_procinfo.flags,pi_do_call);
  2492. expectloc:=LOC_VOID;
  2493. if assigned(left) then
  2494. firstpass(left);
  2495. if assigned(right) then
  2496. firstpass(right);
  2497. end;
  2498. function tonnode.docompare(p: tnode): boolean;
  2499. begin
  2500. docompare := false;
  2501. end;
  2502. end.