nflw.pas 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877
  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(tunarynode)
  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)}
  277. cpuinfo,
  278. {$endif defined(xtensa) or defined(i386)}
  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.destroy;
  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.destroy;
  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.destroy;
  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.destroy;
  1369. condition.destroy;
  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.destroy;
  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. function tifnode.internalsimplify(warn: boolean) : tnode;
  1407. {$if defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64)}
  1408. var
  1409. thenstmnt, elsestmnt: tnode;
  1410. in_nr: tinlinenumber;
  1411. paratype: tdef;
  1412. {$endif defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64)}
  1413. begin
  1414. result:=nil;
  1415. { optimize constant expressions }
  1416. if (left.nodetype=ordconstn) then
  1417. begin
  1418. if tordconstnode(left).value.uvalue<>0 then
  1419. begin
  1420. if assigned(right) then
  1421. result:=right
  1422. else
  1423. result:=cnothingnode.create;
  1424. right:=nil;
  1425. if warn and assigned(t1) and not(nf_internal in left.flags) then
  1426. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  1427. end
  1428. else
  1429. begin
  1430. if assigned(t1) then
  1431. result:=t1
  1432. else
  1433. result:=cnothingnode.create;
  1434. t1:=nil;
  1435. if warn and assigned(right) and not(nf_internal in left.flags) then
  1436. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1437. end;
  1438. end;
  1439. {$ifndef llvm}
  1440. {$if defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64)}
  1441. { use min/max intrinsic?
  1442. convert (with <op> being <, >, >=, <=
  1443. if a <op> b then
  1444. x:=a
  1445. else
  1446. x:=b;
  1447. and
  1448. if a <op> b then
  1449. x:=a;
  1450. into appropriate min/max intrinsics
  1451. }
  1452. elsestmnt:=nil;
  1453. in_nr:=Default(tinlinenumber);
  1454. if (cs_opt_level2 in current_settings.optimizerswitches) and
  1455. (left.nodetype in [gtn,gten,ltn,lten]) and IsSingleStatement(right,thenstmnt) and
  1456. ((t1=nil) or IsSingleStatement(t1,elsestmnt)) and
  1457. (thenstmnt.nodetype=assignn) and ((t1=nil) or (elsestmnt.nodetype=assignn)) and
  1458. not(might_have_sideeffects(left)) and
  1459. ((t1=nil) or tassignmentnode(thenstmnt).left.isequal(tassignmentnode(elsestmnt).left)) and
  1460. {$if defined(i386) or defined(x86_64)}
  1461. {$ifdef i386}
  1462. (((current_settings.fputype>=fpu_sse) and is_single(tassignmentnode(thenstmnt).left.resultdef)) or
  1463. ((current_settings.fputype>=fpu_sse2) and is_double(tassignmentnode(thenstmnt).left.resultdef)) or
  1464. ((CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) and is_32bitint(tassignmentnode(thenstmnt).left.resultdef))
  1465. ) and
  1466. {$else i386}
  1467. (is_single(tassignmentnode(thenstmnt).left.resultdef) or
  1468. is_double(tassignmentnode(thenstmnt).left.resultdef) or
  1469. is_32bitint(tassignmentnode(thenstmnt).left.resultdef) or
  1470. is_64bitint(tassignmentnode(thenstmnt).left.resultdef)
  1471. ) and
  1472. {$endif i386}
  1473. {$endif defined(i386) or defined(x86_64)}
  1474. {$if defined(xtensa)}
  1475. (CPUXTENSA_HAS_MINMAX in cpu_capabilities[current_settings.cputype]) and is_32bitint(tassignmentnode(thenstmnt).right.resultdef) and
  1476. {$endif defined(xtensa)}
  1477. {$if defined(aarch64)}
  1478. (is_single(tassignmentnode(thenstmnt).left.resultdef) or is_double(tassignmentnode(thenstmnt).left.resultdef) or
  1479. is_32bitint(tassignmentnode(thenstmnt).left.resultdef) or is_64bitint(tassignmentnode(thenstmnt).left.resultdef)) and
  1480. {$endif defined(aarch64)}
  1481. (
  1482. { the right size of the assignment in the then clause must either }
  1483. { equal to the left ... }
  1484. (tassignmentnode(thenstmnt).right.isequal(taddnode(left).left) and
  1485. { ... and the else clause must be either not exist }
  1486. { and the left side of the assignment in the then clause must be }
  1487. { equal to the right operand of the comparison operator }
  1488. (
  1489. ((t1=nil) and (tassignmentnode(thenstmnt).left.isequal(taddnode(left).right))) or
  1490. { or the else clause exists and the right side of the assignment in the else clause }
  1491. { must be equal to the right side of the comparison operator }
  1492. (assigned(elsestmnt) and tassignmentnode(elsestmnt).right.isequal(taddnode(left).right)))
  1493. ) or
  1494. { ... or right operand of the comparison operator }
  1495. (tassignmentnode(thenstmnt).right.isequal(taddnode(left).right) and
  1496. { ... and the else clause must be either not exist }
  1497. { and the left side of the assignment in the then clause must be }
  1498. { equal to the left operand of the comparison operator }
  1499. (
  1500. ((t1=nil) and (tassignmentnode(thenstmnt).left.isequal(taddnode(left).left))) or
  1501. { or the else clause exists and the right side of the assignment in the else clause }
  1502. { must be equal to the left side of the comparison operator }
  1503. (assigned(elsestmnt) and tassignmentnode(elsestmnt).right.isequal(taddnode(left).left))
  1504. )
  1505. )
  1506. ) then
  1507. begin
  1508. paratype:=tassignmentnode(thenstmnt).left.resultdef;
  1509. if ((left.nodetype in [gtn,gten]) and
  1510. tassignmentnode(thenstmnt).right.isequal(taddnode(left).left)) or
  1511. ((left.nodetype in [ltn,lten]) and
  1512. tassignmentnode(thenstmnt).right.isequal(taddnode(left).right)) then
  1513. begin
  1514. if is_double(paratype) then
  1515. in_nr:=in_max_double
  1516. else if is_single(paratype) then
  1517. in_nr:=in_max_single
  1518. else if is_u32bitint(paratype) then
  1519. in_nr:=in_max_dword
  1520. else if is_s32bitint(paratype) then
  1521. in_nr:=in_max_longint
  1522. else if is_u64bitint(paratype) then
  1523. in_nr:=in_max_qword
  1524. else if is_s64bitint(paratype) then
  1525. in_nr:=in_max_int64;
  1526. end
  1527. else
  1528. begin
  1529. if is_double(paratype) then
  1530. in_nr:=in_min_double
  1531. else if is_single(paratype) then
  1532. in_nr:=in_min_single
  1533. else if is_u32bitint(paratype) then
  1534. in_nr:=in_min_dword
  1535. else if is_s32bitint(paratype) then
  1536. in_nr:=in_min_longint
  1537. else if is_u64bitint(paratype) then
  1538. in_nr:=in_min_qword
  1539. else if is_s64bitint(paratype) then
  1540. in_nr:=in_min_int64;
  1541. end;
  1542. { for inline nodes, the first parameter is the last one in the linked list
  1543. Due to the defined behaviour for the min/max intrinsics that in case of a NaN
  1544. the second parameter is taken, we have to put the else part into the second parameter
  1545. thus pass it to the first callparanode call }
  1546. if t1=nil then
  1547. Result:=cassignmentnode.create_internal(tassignmentnode(thenstmnt).left.getcopy,
  1548. cinlinenode.create(in_nr,false,ccallparanode.create(tassignmentnode(thenstmnt).left.getcopy,
  1549. ccallparanode.create(tassignmentnode(thenstmnt).right.getcopy,nil)))
  1550. )
  1551. else
  1552. Result:=cassignmentnode.create_internal(tassignmentnode(thenstmnt).left.getcopy,
  1553. cinlinenode.create(in_nr,false,ccallparanode.create(tassignmentnode(elsestmnt).right.getcopy,
  1554. ccallparanode.create(tassignmentnode(thenstmnt).right.getcopy,nil)))
  1555. );
  1556. node_reset_pass1_write(Result);
  1557. end;
  1558. {$endif defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64)}
  1559. {$endif llvm}
  1560. end;
  1561. function tifnode.simplify(forinline : boolean) : tnode;
  1562. begin
  1563. result:=internalsimplify(false);
  1564. end;
  1565. function tifnode.pass_typecheck:tnode;
  1566. begin
  1567. result:=nil;
  1568. resultdef:=voidtype;
  1569. typecheckpass(left);
  1570. { tp procvar support }
  1571. maybe_call_procvar(left,true);
  1572. { if path }
  1573. if assigned(right) then
  1574. typecheckpass(right);
  1575. { else path }
  1576. if assigned(t1) then
  1577. typecheckpass(t1);
  1578. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1579. if codegenerror then
  1580. exit;
  1581. if not(is_boolean(left.resultdef)) and
  1582. not(is_typeparam(left.resultdef)) then
  1583. inserttypeconv(left,pasbool1type);
  1584. result:=internalsimplify(not(nf_internal in flags));
  1585. end;
  1586. function tifnode.pass_1 : tnode;
  1587. begin
  1588. result:=nil;
  1589. expectloc:=LOC_VOID;
  1590. firstpass(left);
  1591. { if path }
  1592. if assigned(right) then
  1593. firstpass(right);
  1594. { else path }
  1595. if assigned(t1) then
  1596. firstpass(t1);
  1597. { leave if we've got an error in one of the paths }
  1598. if codegenerror then
  1599. exit;
  1600. end;
  1601. {*****************************************************************************
  1602. TFORNODE
  1603. *****************************************************************************}
  1604. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  1605. begin
  1606. inherited create(forn,l,r,_t1,_t2);
  1607. if back then
  1608. include(loopflags,lnf_backward);
  1609. include(loopflags,lnf_testatbegin);
  1610. end;
  1611. destructor tfornode.destroy;
  1612. begin
  1613. if assigned(loopiteration) then
  1614. loopiteration.destroy;
  1615. inherited destroy;
  1616. end;
  1617. function tfornode.simplify(forinline : boolean) : tnode;
  1618. begin
  1619. result:=nil;
  1620. { Can we spare the first comparision? }
  1621. if (t1.nodetype=ordconstn) and
  1622. (right.nodetype=ordconstn) and
  1623. (
  1624. (
  1625. (lnf_backward in loopflags) and
  1626. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1627. ) or
  1628. (
  1629. not(lnf_backward in loopflags) and
  1630. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1631. )
  1632. ) then
  1633. exclude(loopflags,lnf_testatbegin);
  1634. if (t1.nodetype=ordconstn) and
  1635. (right.nodetype=ordconstn) and
  1636. (
  1637. (
  1638. (lnf_backward in loopflags) and
  1639. (tordconstnode(right).value<tordconstnode(t1).value)
  1640. ) or
  1641. (
  1642. not(lnf_backward in loopflags) and
  1643. (tordconstnode(right).value>tordconstnode(t1).value)
  1644. )
  1645. ) then
  1646. result:=cnothingnode.create;
  1647. end;
  1648. function tfornode.pass_typecheck:tnode;
  1649. var
  1650. res : tnode;
  1651. rangedef: tdef;
  1652. begin
  1653. result:=nil;
  1654. resultdef:=voidtype;
  1655. { process the loopvar, from and to, varstates are already set }
  1656. typecheckpass(left);
  1657. typecheckpass(right);
  1658. typecheckpass(t1);
  1659. set_varstate(left,vs_written,[]);
  1660. { Make sure that the loop var and the
  1661. from and to values are compatible types }
  1662. if not(m_iso in current_settings.modeswitches) then
  1663. rangedef:=left.resultdef
  1664. else
  1665. rangedef:=get_iso_range_type(left.resultdef);
  1666. check_ranges(right.fileinfo,right,rangedef);
  1667. inserttypeconv(right,rangedef);
  1668. check_ranges(t1.fileinfo,t1,rangedef);
  1669. inserttypeconv(t1,rangedef);
  1670. if assigned(t2) then
  1671. typecheckpass(t2);
  1672. result:=simplify(false);
  1673. { loop unrolling }
  1674. if not(assigned(result)) and
  1675. (cs_opt_loopunroll in current_settings.optimizerswitches) and
  1676. assigned(t2) and
  1677. { statements must be error free }
  1678. not(tnf_error in t2.transientflags) then
  1679. begin
  1680. typecheckpass(t2);
  1681. res:=t2.simplify(false);
  1682. if assigned(res) then
  1683. t2:=res;
  1684. res:=unroll_loop(self);
  1685. if assigned(res) then
  1686. begin
  1687. typecheckpass(res);
  1688. result:=res;
  1689. exit;
  1690. end;
  1691. end;
  1692. end;
  1693. function tfornode.pass_1 : tnode;
  1694. begin
  1695. result:=nil;
  1696. expectloc:=LOC_VOID;
  1697. firstpass(left);
  1698. firstpass(right);
  1699. firstpass(t1);
  1700. if assigned(t2) then
  1701. firstpass(t2);
  1702. end;
  1703. function checkcontinue(var n:tnode; arg: pointer): foreachnoderesult;
  1704. begin
  1705. if n.nodetype=continuen then
  1706. result:=fen_norecurse_true
  1707. else
  1708. result:=fen_false;
  1709. end;
  1710. function tfornode.makewhileloop : tnode;
  1711. var
  1712. ifblock,loopblock : tblocknode;
  1713. ifstatements,statements,loopstatements : tstatementnode;
  1714. fromtemp,totemp : ttempcreatenode;
  1715. do_loopvar_at_end : Boolean;
  1716. { if the lower and/or upper bound are variable, we need a surrounding if }
  1717. needsifblock : Boolean;
  1718. cond : tnodetype;
  1719. fromexpr : tnode;
  1720. toexpr, leftcopy: tnode;
  1721. { if the upper bound is not constant, it must be store in a temp initially }
  1722. usetotemp : boolean;
  1723. { if the lower bound is not constant, it must be store in a temp before calculating the upper bound }
  1724. usefromtemp : boolean;
  1725. storefilepos: tfileposinfo;
  1726. countermin, countermax: Tconstexprint;
  1727. procedure iterate_counter(var s : tstatementnode;fw : boolean);
  1728. var
  1729. leftcopy: tnode;
  1730. begin
  1731. { get rid of nf_write etc. as the left node is now only read }
  1732. leftcopy:=left.getcopy;
  1733. node_reset_flags(leftcopy,[nf_modify,nf_write],[tnf_pass1_done]);
  1734. if fw then
  1735. addstatement(s,
  1736. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_succ_x,false,leftcopy)))
  1737. else
  1738. addstatement(s,
  1739. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_pred_x,false,leftcopy)));
  1740. end;
  1741. function iterate_counter_func(arg : tnode;fw : boolean) : tnode;
  1742. begin
  1743. if fw then
  1744. result:=cinlinenode.createintern(in_succ_x,false,arg)
  1745. else
  1746. result:=cinlinenode.createintern(in_pred_x,false,arg);
  1747. end;
  1748. begin
  1749. result:=nil;
  1750. totemp:=nil;
  1751. fromtemp:=nil;
  1752. storefilepos:=current_filepos;
  1753. current_filepos:=fileinfo;
  1754. case left.resultdef.typ of
  1755. enumdef:
  1756. begin
  1757. countermin:=tenumdef(left.resultdef).min;
  1758. countermax:=tenumdef(left.resultdef).max;
  1759. end;
  1760. orddef:
  1761. begin
  1762. countermin:=torddef(left.resultdef).low;
  1763. countermax:=torddef(left.resultdef).high;
  1764. end;
  1765. else
  1766. Internalerror(2020012601);
  1767. end;
  1768. { check if we can pred/succ the loop var at the end }
  1769. do_loopvar_at_end:=(lnf_dont_mind_loopvar_on_exit in loopflags) and
  1770. is_constnode(right) and is_constnode(t1) and
  1771. { 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
  1772. because we either get an overflow/underflow or the compiler removes the check as it never can be true }
  1773. { checking just the min./max. value depending on the pure size of the counter does not work as the check might
  1774. get optimized away
  1775. 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
  1776. 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
  1777. not((lnf_backward in loopflags) and not(is_signed(left.resultdef)) and (get_ordinal_value(t1)=0)) and
  1778. not((lnf_backward in loopflags) and is_signed(left.resultdef) and (get_ordinal_value(t1)=(-Tconstexprint(1 shl (left.resultdef.size*8-1))))) and
  1779. }
  1780. not(not(lnf_backward in loopflags) and (get_ordinal_value(t1)=countermax)) and
  1781. not((lnf_backward in loopflags) and (get_ordinal_value(t1)=countermin)) and
  1782. { neither might the for loop contain a continue statement as continue in a while loop would skip the increment at the end
  1783. of the loop, this could be overcome by replacing the continue statement with an pred/succ; continue sequence }
  1784. not(foreachnodestatic(t2,@checkcontinue,nil)) and
  1785. { if the loop is unrolled and there is a jump into the loop,
  1786. then we can't do the trick with incrementing the loop var only at the
  1787. end
  1788. }
  1789. not(assigned(entrylabel));
  1790. needsifblock:=not(is_constnode(right)) or not(is_constnode(t1));
  1791. { convert the for loop into a while loop }
  1792. result:=internalstatements(statements);
  1793. ifblock:=internalstatements(ifstatements);
  1794. loopblock:=internalstatements(loopstatements);
  1795. usefromtemp:=(might_have_sideeffects(t1) and not(is_const(right))) or (node_complexity(right)>1);
  1796. usetotemp:=not(is_const(t1));
  1797. if needsifblock then
  1798. begin
  1799. { do not generate a temp. for the from node, if it is a const, it can be copied directly since
  1800. no side effect might change it }
  1801. if usefromtemp then
  1802. begin
  1803. fromtemp:=ctempcreatenode.create(right.resultdef,right.resultdef.size,tt_persistent,true);
  1804. { the if block might be optimized out, so we put the deletetempnode after the if-block, however,
  1805. this causes a long life time of the fromtemp. If the final regsync is left away, the reg. allocator
  1806. figures out the needed life time. As their are no loops involved between the uses of the fromtemp,
  1807. this does no hurt }
  1808. fromtemp.includetempflag(ti_no_final_regsync);
  1809. addstatement(statements,fromtemp);
  1810. { while it would be beneficial to fold the initial reverse succ/pred into this assignment, this is
  1811. not possible because it might wrap around and the if check later on goes wrong }
  1812. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(fromtemp),right.getcopy));
  1813. end;
  1814. if usetotemp then
  1815. begin
  1816. totemp:=ctempcreatenode.create(t1.resultdef,t1.resultdef.size,tt_persistent,true);
  1817. addstatement(statements,totemp);
  1818. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(totemp),t1.getcopy));
  1819. end;
  1820. if usefromtemp then
  1821. begin
  1822. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,ctemprefnode.create(fromtemp)));
  1823. if not(do_loopvar_at_end) then
  1824. iterate_counter(ifstatements,lnf_backward in loopflags);
  1825. end
  1826. else
  1827. begin
  1828. if not(do_loopvar_at_end) then
  1829. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
  1830. iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
  1831. else
  1832. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
  1833. end;
  1834. end
  1835. else
  1836. begin
  1837. if not(do_loopvar_at_end) then
  1838. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
  1839. iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
  1840. else
  1841. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
  1842. end;
  1843. if assigned(entrylabel) then
  1844. addstatement(ifstatements,cgotonode.create(tlabelnode(entrylabel).labsym));
  1845. if not(do_loopvar_at_end) then
  1846. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1847. { avoid copying t2, it is used only once and it might be big }
  1848. addstatement(loopstatements,t2);
  1849. t2:=nil;
  1850. if do_loopvar_at_end then
  1851. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1852. if do_loopvar_at_end then
  1853. begin
  1854. if lnf_backward in loopflags then
  1855. cond:=ltn
  1856. else
  1857. cond:=gtn;
  1858. end
  1859. else
  1860. begin
  1861. if lnf_backward in loopflags then
  1862. cond:=lten
  1863. else
  1864. cond:=gten;
  1865. end;
  1866. { get rid of nf_write etc. as the left node is now only read }
  1867. leftcopy:=left.getcopy;
  1868. node_reset_flags(leftcopy,[nf_modify,nf_write],[tnf_pass1_done]);
  1869. if needsifblock then
  1870. begin
  1871. if usetotemp then
  1872. toexpr:=ctemprefnode.create(totemp)
  1873. else
  1874. toexpr:=t1.getcopy;
  1875. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,leftcopy,toexpr),loopblock,false,true));
  1876. if usefromtemp then
  1877. fromexpr:=ctemprefnode.create(fromtemp)
  1878. else
  1879. fromexpr:=right.getcopy;
  1880. if usetotemp then
  1881. toexpr:=ctemprefnode.create(totemp)
  1882. else
  1883. toexpr:=t1.getcopy;
  1884. if lnf_backward in loopflags then
  1885. addstatement(statements,cifnode.create(caddnode.create_internal(gten,
  1886. fromexpr,toexpr),ifblock,nil))
  1887. else
  1888. addstatement(statements,cifnode.create(caddnode.create_internal(lten,
  1889. fromexpr,toexpr),ifblock,nil));
  1890. if usetotemp then
  1891. addstatement(statements,ctempdeletenode.create(totemp));
  1892. if usefromtemp then
  1893. addstatement(statements,ctempdeletenode.create(fromtemp));
  1894. end
  1895. else
  1896. begin
  1897. { is a simple comparision for equality sufficient? }
  1898. if do_loopvar_at_end and (lnf_backward in loopflags) and (lnf_counter_not_used in loopflags) then
  1899. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(equaln,leftcopy,
  1900. caddnode.create_internal(subn,t1.getcopy,cordconstnode.create(1,t1.resultdef,false))),loopblock,false,true))
  1901. else
  1902. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,leftcopy,t1.getcopy),loopblock,false,true));
  1903. addstatement(statements,ifblock);
  1904. end;
  1905. current_filepos:=storefilepos;
  1906. end;
  1907. {*****************************************************************************
  1908. TEXITNODE
  1909. *****************************************************************************}
  1910. constructor texitnode.create(l:tnode);
  1911. begin
  1912. inherited create(exitn,l);
  1913. if assigned(left) then
  1914. begin
  1915. { add assignment to funcretsym }
  1916. left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
  1917. left:=cassignmentnode.create(
  1918. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1919. left);
  1920. end;
  1921. end;
  1922. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1923. begin
  1924. inherited ppuload(t,ppufile);
  1925. end;
  1926. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1927. begin
  1928. inherited ppuwrite(ppufile);
  1929. end;
  1930. function texitnode.pass_typecheck:tnode;
  1931. var
  1932. newstatement : tstatementnode;
  1933. ressym: tsym;
  1934. resdef: tdef;
  1935. begin
  1936. result:=nil;
  1937. newstatement:=nil;
  1938. if assigned(left) then
  1939. begin
  1940. result:=internalstatements(newstatement);
  1941. addstatement(newstatement,left);
  1942. left:=nil;
  1943. end;
  1944. { if the function result has been migrated to the parentfpstruct,
  1945. we have to load it back to the original location (from which the
  1946. code generator will load it into the function result location),
  1947. because the code to this that we add in tnodeutils.wrap_proc_body()
  1948. gets inserted before the exit label to which this node will jump }
  1949. if (target_info.system in systems_fpnestedstruct) and
  1950. not(nf_internal in flags) and
  1951. current_procinfo.procdef.get_funcretsym_info(ressym,resdef) and
  1952. (tabstractnormalvarsym(ressym).inparentfpstruct) then
  1953. begin
  1954. if not assigned(result) then
  1955. result:=internalstatements(newstatement);
  1956. cnodeutils.load_parentfpstruct_nested_funcret(ressym,newstatement);
  1957. end;
  1958. if assigned(result) then
  1959. begin
  1960. addstatement(newstatement,self.getcopy);
  1961. { ensure we don't insert the function result loading code again for
  1962. this node }
  1963. include(newstatement.left.flags,nf_internal);
  1964. end;
  1965. resultdef:=voidtype;
  1966. end;
  1967. function texitnode.pass_1 : tnode;
  1968. begin
  1969. result:=nil;
  1970. expectloc:=LOC_VOID;
  1971. if assigned(left) then
  1972. internalerror(2011052801);
  1973. end;
  1974. {*****************************************************************************
  1975. TBREAKNODE
  1976. *****************************************************************************}
  1977. constructor tbreaknode.create;
  1978. begin
  1979. inherited create(breakn);
  1980. end;
  1981. function tbreaknode.pass_typecheck:tnode;
  1982. begin
  1983. result:=nil;
  1984. resultdef:=voidtype;
  1985. end;
  1986. function tbreaknode.pass_1 : tnode;
  1987. begin
  1988. result:=nil;
  1989. expectloc:=LOC_VOID;
  1990. end;
  1991. {*****************************************************************************
  1992. TCONTINUENODE
  1993. *****************************************************************************}
  1994. constructor tcontinuenode.create;
  1995. begin
  1996. inherited create(continuen);
  1997. end;
  1998. function tcontinuenode.pass_typecheck:tnode;
  1999. begin
  2000. result:=nil;
  2001. resultdef:=voidtype;
  2002. end;
  2003. function tcontinuenode.pass_1 : tnode;
  2004. begin
  2005. result:=nil;
  2006. expectloc:=LOC_VOID;
  2007. end;
  2008. {*****************************************************************************
  2009. TGOTONODE
  2010. *****************************************************************************}
  2011. constructor tgotonode.create(p : tlabelsym);
  2012. begin
  2013. inherited create(goton);
  2014. exceptionblock:=current_exceptblock;
  2015. labelnode:=nil;
  2016. labelsym:=p;
  2017. end;
  2018. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2019. begin
  2020. inherited ppuload(t,ppufile);
  2021. labelnodeidx:=ppufile.getlongint;
  2022. exceptionblock:=ppufile.getbyte;
  2023. end;
  2024. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  2025. begin
  2026. inherited ppuwrite(ppufile);
  2027. labelnodeidx:=labelnode.ppuidx;
  2028. ppufile.putlongint(labelnodeidx);
  2029. ppufile.putbyte(exceptionblock);
  2030. end;
  2031. procedure tgotonode.buildderefimpl;
  2032. begin
  2033. inherited buildderefimpl;
  2034. end;
  2035. procedure tgotonode.derefimpl;
  2036. begin
  2037. inherited derefimpl;
  2038. end;
  2039. procedure tgotonode.resolveppuidx;
  2040. begin
  2041. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  2042. if labelnode.nodetype<>labeln then
  2043. internalerror(200809021);
  2044. end;
  2045. function tgotonode.pass_typecheck:tnode;
  2046. begin
  2047. result:=nil;
  2048. resultdef:=voidtype;
  2049. end;
  2050. function tgotonode.pass_1 : tnode;
  2051. var
  2052. p2 : tprocinfo;
  2053. begin
  2054. result:=nil;
  2055. expectloc:=LOC_VOID;
  2056. { The labelnode can already be set when
  2057. this node was copied }
  2058. if not(assigned(labelnode)) then
  2059. begin
  2060. { inner procedure goto? }
  2061. if assigned(labelsym.code) and
  2062. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  2063. { generated by the optimizer? }
  2064. not(assigned(labelsym.owner))) then
  2065. labelnode:=tlabelnode(labelsym.code)
  2066. else if ((m_non_local_goto in current_settings.modeswitches) and
  2067. assigned(labelsym.owner)) or
  2068. { nested exits don't need the non local goto switch }
  2069. (labelsym.realname='$nestedexit') then
  2070. begin
  2071. if current_procinfo.procdef.parast.symtablelevel>=labelsym.owner.symtablelevel then
  2072. begin
  2073. { don't mess with the exception blocks, global gotos in/out side exception blocks are not allowed }
  2074. if exceptionblock>0 then
  2075. CGMessage(cg_e_goto_inout_of_exception_block);
  2076. { goto across procedures using exception?
  2077. this is not allowed because we cannot
  2078. easily unwind the exception frame
  2079. stack
  2080. }
  2081. p2:=current_procinfo;
  2082. while true do
  2083. begin
  2084. if ((cs_implicit_exceptions in current_settings.moduleswitches) and ((p2.flags*[pi_needs_implicit_finally,pi_has_implicit_finally])<>[])) or
  2085. ((p2.flags*[pi_uses_exceptions])<>[]) then
  2086. Message(cg_e_goto_across_procedures_with_exceptions_not_allowed);
  2087. if labelsym.owner=p2.procdef.localst then
  2088. break;
  2089. p2:=p2.parent
  2090. end;
  2091. if assigned(labelsym.jumpbuf) then
  2092. begin
  2093. result:=ccallnode.createintern('fpc_longjmp',
  2094. ccallparanode.create(cordconstnode.create(1,sinttype,true),
  2095. ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
  2096. nil)));
  2097. end
  2098. else
  2099. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  2100. end
  2101. else
  2102. CGMessagePos(self.fileinfo,cg_e_interprocedural_goto_only_to_outer_scope_allowed);
  2103. end
  2104. else
  2105. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  2106. end;
  2107. { check if we don't mess with exception blocks }
  2108. if assigned(labelnode) and
  2109. (exceptionblock<>labelnode.exceptionblock) then
  2110. CGMessage(cg_e_goto_inout_of_exception_block);
  2111. end;
  2112. function tgotonode.dogetcopy : tnode;
  2113. var
  2114. p : tgotonode;
  2115. begin
  2116. p:=tgotonode(inherited dogetcopy);
  2117. p.exceptionblock:=exceptionblock;
  2118. { generate labelnode if not done yet }
  2119. if not(assigned(labelnode)) then
  2120. begin
  2121. if assigned(labelsym) and assigned(labelsym.code) then
  2122. labelnode:=tlabelnode(labelsym.code)
  2123. end;
  2124. p.labelsym:=labelsym;
  2125. { do not copy the label node here as we do not know if the label node is part of the tree or not,
  2126. this will be fixed after the copying in node.setuplabelnode: if the labelnode has copiedto set,
  2127. labelnode of the goto node is update }
  2128. if assigned(labelnode) then
  2129. p.labelnode:=labelnode
  2130. else
  2131. begin
  2132. { don't trigger IE when there was already an error, i.e. the
  2133. label is not defined. See tw11763 (PFV) }
  2134. if (errorcount=0) and
  2135. { don't trigger IE if it's a global goto }
  2136. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  2137. not(assigned(labelsym.owner))) then
  2138. internalerror(200610291);
  2139. end;
  2140. result:=p;
  2141. end;
  2142. function tgotonode.docompare(p: tnode): boolean;
  2143. begin
  2144. docompare := false;
  2145. end;
  2146. {*****************************************************************************
  2147. TLABELNODE
  2148. *****************************************************************************}
  2149. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  2150. begin
  2151. inherited create(labeln,l);
  2152. exceptionblock:=current_exceptblock;
  2153. labsym:=alabsym;
  2154. { Register labelnode in labelsym }
  2155. labsym.code:=self;
  2156. end;
  2157. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2158. begin
  2159. inherited ppuload(t,ppufile);
  2160. exceptionblock:=ppufile.getbyte;
  2161. end;
  2162. destructor tlabelnode.destroy;
  2163. begin
  2164. if assigned(labsym) then
  2165. begin
  2166. if not assigned(labsym.Owner) then
  2167. labsym.Free // Free labelsym if it has no owner
  2168. else
  2169. if labsym.code=pointer(self) then
  2170. begin
  2171. { Remove reference in labelsym, this is to prevent
  2172. goto's to this label }
  2173. labsym.code:=nil;
  2174. end;
  2175. end;
  2176. inherited destroy;
  2177. end;
  2178. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  2179. begin
  2180. inherited ppuwrite(ppufile);
  2181. ppufile.putbyte(exceptionblock);
  2182. end;
  2183. procedure tlabelnode.buildderefimpl;
  2184. begin
  2185. inherited buildderefimpl;
  2186. end;
  2187. procedure tlabelnode.derefimpl;
  2188. begin
  2189. inherited derefimpl;
  2190. end;
  2191. function tlabelnode.pass_typecheck:tnode;
  2192. begin
  2193. result:=nil;
  2194. { left could still be unassigned }
  2195. if assigned(left) then
  2196. typecheckpass(left);
  2197. resultdef:=voidtype;
  2198. end;
  2199. function tlabelnode.pass_1 : tnode;
  2200. begin
  2201. result:=nil;
  2202. expectloc:=LOC_VOID;
  2203. if not (nf_internal in flags) then
  2204. include(current_procinfo.flags,pi_has_label);
  2205. if assigned(left) then
  2206. firstpass(left);
  2207. if (m_non_local_goto in current_settings.modeswitches) and
  2208. { the owner can be Nil for internal labels }
  2209. assigned(labsym.owner) and
  2210. (current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
  2211. CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
  2212. end;
  2213. function tlabelnode.dogetcopy : tnode;
  2214. begin
  2215. if not(assigned(copiedto)) then
  2216. copiedto:=tlabelnode(inherited dogetcopy);
  2217. copiedto.exceptionblock:=exceptionblock;
  2218. result:=copiedto;
  2219. end;
  2220. function tlabelnode.docompare(p: tnode): boolean;
  2221. begin
  2222. docompare := false;
  2223. end;
  2224. {*****************************************************************************
  2225. TRAISENODE
  2226. *****************************************************************************}
  2227. constructor traisenode.create(l,taddr,tframe:tnode);
  2228. begin
  2229. inherited create(raisen,l,taddr,tframe);
  2230. end;
  2231. function traisenode.pass_typecheck:tnode;
  2232. begin
  2233. result:=nil;
  2234. resultdef:=voidtype;
  2235. if assigned(left) then
  2236. begin
  2237. { first para must be a _class_ }
  2238. typecheckpass(left);
  2239. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2240. if codegenerror then
  2241. exit;
  2242. if not is_class(left.resultdef) and
  2243. not is_javaclass(left.resultdef) then
  2244. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2245. { insert needed typeconvs for addr,frame }
  2246. if assigned(right) then
  2247. begin
  2248. { addr }
  2249. typecheckpass(right);
  2250. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2251. inserttypeconv(right,voidcodepointertype);
  2252. { frame }
  2253. if assigned(third) then
  2254. begin
  2255. typecheckpass(third);
  2256. set_varstate(third,vs_read,[vsf_must_be_valid]);
  2257. inserttypeconv(third,voidpointertype);
  2258. end;
  2259. end;
  2260. end;
  2261. end;
  2262. function traisenode.pass_1 : tnode;
  2263. var
  2264. statements : tstatementnode;
  2265. current_addr : tlabelnode;
  2266. raisenode : tcallnode;
  2267. begin
  2268. result:=internalstatements(statements);
  2269. if assigned(left) then
  2270. begin
  2271. { first para must be a class }
  2272. firstpass(left);
  2273. { insert needed typeconvs for addr,frame }
  2274. if assigned(right) then
  2275. begin
  2276. { addr }
  2277. firstpass(right);
  2278. { frame }
  2279. if assigned(third) then
  2280. firstpass(third)
  2281. else
  2282. third:=cpointerconstnode.Create(0,voidpointertype);
  2283. end
  2284. else
  2285. begin
  2286. third:=cinlinenode.create(in_get_frame,false,nil);
  2287. current_addr:=clabelnode.create(cnothingnode.create,clabelsym.create('$raiseaddr'));
  2288. include(current_addr.flags,nf_internal);
  2289. addstatement(statements,current_addr);
  2290. right:=caddrnode.create(cloadnode.create(current_addr.labsym,current_addr.labsym.owner));
  2291. { raise address off by one so we are for sure inside the action area for the raise }
  2292. if tf_use_psabieh in target_info.flags then
  2293. right:=caddnode.create_internal(addn,right,cordconstnode.create(1,sizesinttype,false));
  2294. end;
  2295. raisenode:=ccallnode.createintern('fpc_raiseexception',
  2296. ccallparanode.create(third,
  2297. ccallparanode.create(right,
  2298. ccallparanode.create(left,nil)))
  2299. );
  2300. include(raisenode.callnodeflags,cnf_call_never_returns);
  2301. addstatement(statements,raisenode);
  2302. end
  2303. else
  2304. begin
  2305. addstatement(statements,ccallnode.createintern('fpc_popaddrstack',nil));
  2306. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  2307. include(raisenode.callnodeflags,cnf_call_never_returns);
  2308. addstatement(statements,raisenode);
  2309. end;
  2310. left:=nil;
  2311. right:=nil;
  2312. third:=nil;
  2313. end;
  2314. {*****************************************************************************
  2315. TTRYEXCEPTNODE
  2316. *****************************************************************************}
  2317. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  2318. begin
  2319. inherited create(tryexceptn,l,r,_t1,nil);
  2320. end;
  2321. function ttryexceptnode.pass_typecheck:tnode;
  2322. begin
  2323. result:=nil;
  2324. typecheckpass(left);
  2325. { on statements }
  2326. if assigned(right) then
  2327. typecheckpass(right);
  2328. { else block }
  2329. if assigned(t1) then
  2330. typecheckpass(t1);
  2331. resultdef:=voidtype;
  2332. end;
  2333. function ttryexceptnode.pass_1 : tnode;
  2334. begin
  2335. result:=nil;
  2336. expectloc:=LOC_VOID;
  2337. firstpass(left);
  2338. { on statements }
  2339. if assigned(right) then
  2340. firstpass(right);
  2341. { else block }
  2342. if assigned(t1) then
  2343. firstpass(t1);
  2344. include(current_procinfo.flags,pi_do_call);
  2345. include(current_procinfo.flags,pi_uses_exceptions);
  2346. adjust_estimated_stack_size;
  2347. end;
  2348. function ttryexceptnode.simplify(forinline: boolean): tnode;
  2349. begin
  2350. result:=nil;
  2351. { empty try -> can never raise exception -> do nothing }
  2352. if has_no_code(left) then
  2353. result:=cnothingnode.create;
  2354. end;
  2355. procedure ttryexceptnode.adjust_estimated_stack_size;
  2356. begin
  2357. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size*2);
  2358. end;
  2359. {*****************************************************************************
  2360. TTRYFINALLYNODE
  2361. *****************************************************************************}
  2362. constructor ttryfinallynode.create(l,r:tnode);
  2363. begin
  2364. inherited create(tryfinallyn,l,r,nil);
  2365. third:=nil;
  2366. implicitframe:=false;
  2367. end;
  2368. constructor ttryfinallynode.create_implicit(l,r:tnode);
  2369. begin
  2370. inherited create(tryfinallyn,l,r,nil);
  2371. third:=nil;
  2372. implicitframe:=true;
  2373. end;
  2374. function ttryfinallynode.pass_typecheck:tnode;
  2375. begin
  2376. result:=nil;
  2377. resultdef:=voidtype;
  2378. typecheckpass(left);
  2379. // "try block" is "used"? (JM)
  2380. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  2381. typecheckpass(right);
  2382. // "except block" is "used"? (JM)
  2383. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  2384. if assigned(third) then
  2385. begin
  2386. typecheckpass(third);
  2387. set_varstate(third,vs_readwritten,[vsf_must_be_valid]);
  2388. end;
  2389. end;
  2390. function ttryfinallynode.pass_1 : tnode;
  2391. begin
  2392. result:=nil;
  2393. expectloc:=LOC_VOID;
  2394. firstpass(left);
  2395. firstpass(right);
  2396. if assigned(third) then
  2397. firstpass(third);
  2398. include(current_procinfo.flags,pi_do_call);
  2399. { pi_uses_exceptions is an information for the optimizer and it
  2400. is only interested in exceptions if they appear inside the body,
  2401. so ignore implicit frames when setting the flag }
  2402. if not(implicitframe) then
  2403. include(current_procinfo.flags,pi_uses_exceptions);
  2404. adjust_estimated_stack_size;
  2405. end;
  2406. function ttryfinallynode.simplify(forinline : boolean): tnode;
  2407. begin
  2408. result:=nil;
  2409. { if the try contains no code, we can kill
  2410. the try and except and return only the
  2411. finally part }
  2412. if has_no_code(left) then
  2413. begin
  2414. result:=right;
  2415. right:=nil;
  2416. end
  2417. { if the finally block contains no code, we can kill
  2418. it and just return the try part }
  2419. else if has_no_code(right) and not(assigned(third)) and not(implicitframe) then
  2420. begin
  2421. result:=left;
  2422. left:=nil;
  2423. end;
  2424. end;
  2425. function ttryfinallynode.dogetcopy: tnode;
  2426. begin
  2427. result:=inherited dogetcopy;
  2428. ttryfinallynode(result).implicitframe:=implicitframe;
  2429. end;
  2430. procedure ttryfinallynode.adjust_estimated_stack_size;
  2431. begin
  2432. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size);
  2433. end;
  2434. {*****************************************************************************
  2435. TONNODE
  2436. *****************************************************************************}
  2437. constructor tonnode.create(l,r:tnode);
  2438. begin
  2439. inherited create(onn,l,r);
  2440. excepTSymtable:=nil;
  2441. excepttype:=nil;
  2442. end;
  2443. destructor tonnode.destroy;
  2444. begin
  2445. { copied nodes don't need to release the symtable }
  2446. if assigned(excepTSymtable) then
  2447. excepTSymtable.free;
  2448. inherited destroy;
  2449. end;
  2450. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2451. begin
  2452. inherited ppuload(t,ppufile);
  2453. excepTSymtable:=nil;
  2454. excepttype:=nil;
  2455. end;
  2456. function tonnode.dogetcopy : tnode;
  2457. var
  2458. n : tonnode;
  2459. begin
  2460. n:=tonnode(inherited dogetcopy);
  2461. if assigned(exceptsymtable) then
  2462. n.exceptsymtable:=exceptsymtable.getcopy
  2463. else
  2464. n.exceptsymtable:=nil;
  2465. n.excepttype:=excepttype;
  2466. result:=n;
  2467. end;
  2468. function tonnode.pass_typecheck:tnode;
  2469. begin
  2470. result:=nil;
  2471. resultdef:=voidtype;
  2472. if not is_class(excepttype) and
  2473. not is_javaclass(excepttype) then
  2474. CGMessage1(type_e_class_type_expected,excepttype.typename);
  2475. if assigned(left) then
  2476. typecheckpass(left);
  2477. if assigned(right) then
  2478. typecheckpass(right);
  2479. end;
  2480. function tonnode.pass_1 : tnode;
  2481. begin
  2482. result:=nil;
  2483. include(current_procinfo.flags,pi_do_call);
  2484. expectloc:=LOC_VOID;
  2485. if assigned(left) then
  2486. firstpass(left);
  2487. if assigned(right) then
  2488. firstpass(right);
  2489. end;
  2490. function tonnode.docompare(p: tnode): boolean;
  2491. begin
  2492. docompare := false;
  2493. end;
  2494. end.