nflw.pas 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850
  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: 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. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  535. genintconstnode(1),
  536. cinlinenode.create(in_length_x,false,ctemprefnode.create(stringvar)),
  537. loopbody,
  538. false);
  539. addstatement(loopstatement,forloopnode);
  540. { free the loop counter }
  541. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  542. { free the temp variable for expression }
  543. addstatement(loopstatement,ctempdeletenode.create(stringvar));
  544. end;
  545. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  546. var
  547. loopstatement, loopbodystatement: tstatementnode;
  548. loopvar, arrayvar: ttempcreatenode;
  549. arrayindex, lowbound, highbound, loopbody, forloopnode, expression: tnode;
  550. is_string: boolean;
  551. tmpdef, convertdef: tdef;
  552. elementcount: aword;
  553. begin
  554. expression := expr;
  555. { result is a block of statements }
  556. result:=internalstatements(loopstatement);
  557. is_string:=ado_IsConstString in tarraydef(expr.resultdef).arrayoptions;
  558. // if array element type <> loovar type then create a conversion if possible
  559. if compare_defs(tarraydef(expression.resultdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible then
  560. begin
  561. tmpdef:=expression.resultdef;
  562. elementcount:=1;
  563. while assigned(tmpdef) and (tmpdef.typ=arraydef) and
  564. (tarraydef(tmpdef).arrayoptions = []) and
  565. (compare_defs(tarraydef(tmpdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible) do
  566. begin
  567. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  568. tmpdef:=tarraydef(tmpdef).elementdef;
  569. end;
  570. if assigned(tmpdef) and (tmpdef.typ=arraydef) and (tarraydef(tmpdef).arrayoptions = []) then
  571. begin
  572. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  573. convertdef:=carraydef.create(0,elementcount-1,s32inttype);
  574. tarraydef(convertdef).elementdef:=tarraydef(tmpdef).elementdef;
  575. expression:=expr.getcopy;
  576. expression:=ctypeconvnode.create_internal(expression,convertdef);
  577. typecheckpass(expression);
  578. addstatement(loopstatement,expression);
  579. end;
  580. end;
  581. if (node_complexity(expression) > 1) and
  582. not(is_open_array(expression.resultdef)) and not(is_array_of_const(expression.resultdef)) then
  583. begin
  584. { create a temp variable for expression }
  585. arrayvar := ctempcreatenode.create(
  586. expression.resultdef,
  587. expression.resultdef.size,
  588. tt_persistent,true);
  589. if is_string then
  590. begin
  591. lowbound:=genintconstnode(1);
  592. highbound:=cinlinenode.create(in_length_x,false,ctemprefnode.create(arrayvar))
  593. end
  594. else
  595. begin
  596. lowbound:=cinlinenode.create(in_low_x,false,ctemprefnode.create(arrayvar));
  597. highbound:=cinlinenode.create(in_high_x,false,ctemprefnode.create(arrayvar));
  598. end;
  599. addstatement(loopstatement,arrayvar);
  600. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(arrayvar),expression.getcopy));
  601. end
  602. else
  603. begin
  604. arrayvar:=nil;
  605. if is_string then
  606. begin
  607. lowbound:=genintconstnode(1);
  608. highbound:=cinlinenode.create(in_length_x,false,expression.getcopy);
  609. end
  610. else
  611. begin
  612. lowbound:=cinlinenode.create(in_low_x,false,expression.getcopy);
  613. highbound:=cinlinenode.create(in_high_x,false,expression.getcopy);
  614. end;
  615. end;
  616. { create a loop counter }
  617. loopvar := ctempcreatenode.create(
  618. tarraydef(expression.resultdef).rangedef,
  619. tarraydef(expression.resultdef).rangedef.size,
  620. tt_persistent,true);
  621. addstatement(loopstatement,loopvar);
  622. arrayindex:=ctemprefnode.create(loopvar);
  623. loopbody:=internalstatements(loopbodystatement);
  624. // for-in loop variable := array_expression[index]
  625. if assigned(arrayvar) then
  626. addstatement(loopbodystatement,
  627. cassignmentnode.create(hloopvar,cvecnode.create(ctemprefnode.create(arrayvar),arrayindex)))
  628. else
  629. addstatement(loopbodystatement,
  630. cassignmentnode.create(hloopvar,cvecnode.create(expression.getcopy,arrayindex)));
  631. { add the actual statement to the loop }
  632. addstatement(loopbodystatement,hloopbody);
  633. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  634. lowbound,
  635. highbound,
  636. loopbody,
  637. false);
  638. addstatement(loopstatement,forloopnode);
  639. { free the loop counter }
  640. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  641. { free the temp variable for expression if needed }
  642. if arrayvar<>nil then
  643. addstatement(loopstatement,ctempdeletenode.create(arrayvar));
  644. end;
  645. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  646. var
  647. loopstatement, loopbodystatement: tstatementnode;
  648. loopvar, setvar: ttempcreatenode;
  649. loopbody, forloopnode: tnode;
  650. begin
  651. // first check is set is empty and if it so then skip other processing
  652. if not Assigned(tsetdef(expr.resultdef).elementdef) then
  653. begin
  654. result:=cnothingnode.create;
  655. // free unused nodes
  656. hloopvar.free;
  657. hloopbody.free;
  658. exit;
  659. end;
  660. { result is a block of statements }
  661. result:=internalstatements(loopstatement);
  662. { create a temp variable for expression }
  663. setvar := ctempcreatenode.create(
  664. expr.resultdef,
  665. expr.resultdef.size,
  666. tt_persistent,true);
  667. addstatement(loopstatement,setvar);
  668. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(setvar),expr.getcopy));
  669. { create a loop counter }
  670. loopvar := ctempcreatenode.create(
  671. tsetdef(expr.resultdef).elementdef,
  672. tsetdef(expr.resultdef).elementdef.size,
  673. tt_persistent,true);
  674. addstatement(loopstatement,loopvar);
  675. // if loopvar in set then
  676. // begin
  677. // hloopvar := loopvar
  678. // for-in loop body
  679. // end
  680. loopbody:=cifnode.create(
  681. cinnode.create(ctemprefnode.create(loopvar),ctemprefnode.create(setvar)),
  682. internalstatements(loopbodystatement),
  683. nil);
  684. addstatement(loopbodystatement,cassignmentnode.create(hloopvar,ctemprefnode.create(loopvar)));
  685. { add the actual statement to the loop }
  686. addstatement(loopbodystatement,hloopbody);
  687. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  688. cinlinenode.create(in_low_x,false,ctemprefnode.create(setvar)),
  689. cinlinenode.create(in_high_x,false,ctemprefnode.create(setvar)),
  690. loopbody,
  691. false);
  692. addstatement(loopstatement,forloopnode);
  693. { free the loop counter }
  694. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  695. { free the temp variable for expression }
  696. addstatement(loopstatement,ctempdeletenode.create(setvar));
  697. end;
  698. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  699. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  700. var
  701. loopstatement, loopbodystatement: tstatementnode;
  702. enumvar: ttempcreatenode;
  703. loopbody, whileloopnode,
  704. enum_get, enum_move, enum_current, enum_get_params: tnode;
  705. propaccesslist: tpropaccesslist;
  706. enumerator_is_class: boolean;
  707. enumerator_destructor: tprocdef;
  708. begin
  709. { result is a block of statements }
  710. result:=internalstatements(loopstatement);
  711. enumerator_is_class := is_class(enumerator_get.returndef);
  712. { create a temp variable for enumerator }
  713. enumvar := ctempcreatenode.create(
  714. enumerator_get.returndef,
  715. enumerator_get.returndef.size,
  716. tt_persistent,true);
  717. addstatement(loopstatement,enumvar);
  718. if enumerator_get.proctypeoption=potype_operator then
  719. begin
  720. enum_get_params:=ccallparanode.create(expr.getcopy,nil);
  721. enum_get:=ccallnode.create(enum_get_params, tprocsym(enumerator_get.procsym), nil, nil, [],nil);
  722. tcallnode(enum_get).procdefinition:=enumerator_get;
  723. addsymref(enumerator_get.procsym,enumerator_get);
  724. end
  725. else
  726. enum_get:=ccallnode.create(nil, tprocsym(enumerator_get.procsym), enumerator_get.owner, expr.getcopy, [],nil);
  727. addstatement(loopstatement,
  728. cassignmentnode.create(
  729. ctemprefnode.create(enumvar),
  730. enum_get
  731. ));
  732. loopbody:=internalstatements(loopbodystatement);
  733. { for-in loop variable := enumerator.current }
  734. if enumerator_current.getpropaccesslist(palt_read,propaccesslist) then
  735. begin
  736. case propaccesslist.firstsym^.sym.typ of
  737. fieldvarsym :
  738. begin
  739. { generate access code }
  740. enum_current:=ctemprefnode.create(enumvar);
  741. propaccesslist_to_node(enum_current,enumerator_current.owner,propaccesslist);
  742. include(enum_current.flags,nf_isproperty);
  743. end;
  744. procsym :
  745. begin
  746. { generate the method call }
  747. enum_current:=ccallnode.create(nil,tprocsym(propaccesslist.firstsym^.sym),enumerator_current.owner,ctemprefnode.create(enumvar),[],nil);
  748. include(enum_current.flags,nf_isproperty);
  749. end
  750. else
  751. begin
  752. enum_current:=cerrornode.create;
  753. Message(type_e_mismatch);
  754. end;
  755. end;
  756. end
  757. else
  758. enum_current:=cerrornode.create;
  759. addstatement(loopbodystatement,
  760. cassignmentnode.create(hloopvar, enum_current));
  761. { add the actual statement to the loop }
  762. addstatement(loopbodystatement,hloopbody);
  763. enum_move:=ccallnode.create(nil, tprocsym(enumerator_move.procsym), enumerator_move.owner, ctemprefnode.create(enumvar), [],nil);
  764. whileloopnode:=cwhilerepeatnode.create(enum_move,loopbody,true,false);
  765. if enumerator_is_class then
  766. begin
  767. { insert a try-finally and call the destructor for the enumerator in the finally section }
  768. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  769. if assigned(enumerator_destructor) then
  770. begin
  771. whileloopnode:=ctryfinallynode.create(
  772. whileloopnode, // try node
  773. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym), // finally node
  774. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[],nil));
  775. end;
  776. { if getenumerator <> nil then do the loop }
  777. whileloopnode:=cifnode.create(
  778. caddnode.create(unequaln, ctemprefnode.create(enumvar), cnilnode.create),
  779. whileloopnode,
  780. nil);
  781. end;
  782. addstatement(loopstatement, whileloopnode);
  783. if is_object(enumerator_get.returndef) then
  784. begin
  785. // call the object destructor too
  786. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  787. if assigned(enumerator_destructor) then
  788. begin
  789. addstatement(loopstatement,
  790. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym),
  791. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[],nil));
  792. end;
  793. end;
  794. { free the temp variable for enumerator }
  795. addstatement(loopstatement,ctempdeletenode.create(enumvar));
  796. end;
  797. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  798. var
  799. pd, movenext: tprocdef;
  800. helperdef: tobjectdef;
  801. current: tpropertysym;
  802. storefilepos: tfileposinfo;
  803. begin
  804. storefilepos:=current_filepos;
  805. current_filepos:=hloopvar.fileinfo;
  806. if expr.nodetype=typen then
  807. begin
  808. if (expr.resultdef.typ=enumdef) and tenumdef(expr.resultdef).has_jumps then
  809. begin
  810. result:=cerrornode.create;
  811. hloopvar.free;
  812. hloopbody.free;
  813. MessagePos1(expr.fileinfo,parser_e_for_in_loop_cannot_be_used_for_the_type,expr.resultdef.typename);
  814. end
  815. else
  816. result:=create_type_for_in_loop(hloopvar, hloopbody, expr);
  817. end
  818. else
  819. begin
  820. { loop is made for an expression }
  821. // Objective-C uses different conventions (and it's only supported for Objective-C 2.0)
  822. if is_objc_class_or_protocol(hloopvar.resultdef) or
  823. is_objc_class_or_protocol(expr.resultdef) then
  824. begin
  825. result:=create_objc_for_in_loop(hloopvar,hloopbody,expr);
  826. if result.nodetype=errorn then
  827. begin
  828. hloopvar.free;
  829. hloopbody.free;
  830. end;
  831. end
  832. { "for x in [] do ..." always results in a never executed loop body }
  833. else if (is_array_constructor(expr.resultdef) and
  834. (tarraydef(expr.resultdef).elementdef=voidtype)) then
  835. begin
  836. if assigned(hloopbody) then
  837. MessagePos(hloopbody.fileinfo,cg_w_unreachable_code);
  838. result:=cnothingnode.create;
  839. end
  840. else
  841. begin
  842. // search for operator first
  843. pd:=search_enumerator_operator(expr.resultdef, hloopvar.resultdef);
  844. // if there is no operator then search for class/object enumerator method
  845. if (pd=nil) and (expr.resultdef.typ in [objectdef,recorddef]) then
  846. begin
  847. { first search using the helper hierarchy }
  848. if search_last_objectpascal_helper(tabstractrecorddef(expr.resultdef),nil,helperdef) then
  849. repeat
  850. pd:=helperdef.search_enumerator_get;
  851. helperdef:=helperdef.childof;
  852. until (pd<>nil) or (helperdef=nil);
  853. { we didn't find an enumerator in a helper, so search in the
  854. class/record/object itself }
  855. if pd=nil then
  856. pd:=tabstractrecorddef(expr.resultdef).search_enumerator_get;
  857. end;
  858. if pd<>nil then
  859. begin
  860. // seach movenext and current symbols
  861. movenext:=tabstractrecorddef(pd.returndef).search_enumerator_move;
  862. if movenext = nil then
  863. begin
  864. result:=cerrornode.create;
  865. hloopvar.free;
  866. hloopbody.free;
  867. MessagePos1(expr.fileinfo,sym_e_no_enumerator_move,pd.returndef.typename);
  868. end
  869. else
  870. begin
  871. current:=tpropertysym(tabstractrecorddef(pd.returndef).search_enumerator_current);
  872. if current = nil then
  873. begin
  874. result:=cerrornode.create;
  875. hloopvar.free;
  876. hloopbody.free;
  877. MessagePos1(expr.fileinfo,sym_e_no_enumerator_current,pd.returndef.typename);
  878. end
  879. else
  880. result:=create_enumerator_for_in_loop(hloopvar, hloopbody, expr, pd, movenext, current);
  881. end;
  882. end
  883. else
  884. begin
  885. { prefer set if loop var could be a set var and the loop
  886. expression can indeed be a set }
  887. if (expr.nodetype=arrayconstructorn) and
  888. (hloopvar.resultdef.typ in [enumdef,orddef]) and
  889. arrayconstructor_can_be_set(expr) then
  890. begin
  891. expr:=arrayconstructor_to_set(expr,false);
  892. typecheckpass(expr);
  893. end;
  894. case expr.resultdef.typ of
  895. stringdef:
  896. result:=create_string_for_in_loop(hloopvar, hloopbody, expr);
  897. arraydef:
  898. result:=create_array_for_in_loop(hloopvar, hloopbody, expr);
  899. setdef:
  900. result:=create_set_for_in_loop(hloopvar, hloopbody, expr);
  901. undefineddef:
  902. result:=cnothingnode.create;
  903. else
  904. begin
  905. result:=cerrornode.create;
  906. hloopvar.free;
  907. hloopbody.free;
  908. MessagePos1(expr.fileinfo,sym_e_no_enumerator,expr.resultdef.typename);
  909. end;
  910. end;
  911. end;
  912. end;
  913. end;
  914. current_filepos:=storefilepos;
  915. end;
  916. function _ConvertForLoops(var n: tnode; arg: pointer): foreachnoderesult;
  917. var
  918. hp : tnode;
  919. begin
  920. Result:=fen_false;
  921. if n.nodetype=forn then
  922. begin
  923. Result:=fen_true;
  924. hp:=n;
  925. n:=tfornode(n).makewhileloop;
  926. do_firstpass(n);
  927. hp.Free;
  928. end;
  929. end;
  930. function ConvertForLoops(var n : tnode) : boolean;
  931. begin
  932. result:=foreachnodestatic(pm_postprocess,n,@_ConvertForLoops,nil);
  933. end;
  934. {****************************************************************************
  935. TLOOPNODE
  936. *****************************************************************************}
  937. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  938. begin
  939. inherited create(tt,l,r);
  940. t1:=_t1;
  941. t2:=_t2;
  942. fileinfo:=l.fileinfo;
  943. end;
  944. destructor tloopnode.destroy;
  945. begin
  946. t1.free;
  947. t2.free;
  948. inherited destroy;
  949. end;
  950. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  951. begin
  952. inherited ppuload(t,ppufile);
  953. t1:=ppuloadnode(ppufile);
  954. t2:=ppuloadnode(ppufile);
  955. ppufile.getset(tppuset1(loopflags));
  956. end;
  957. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  958. begin
  959. inherited ppuwrite(ppufile);
  960. ppuwritenode(ppufile,t1);
  961. ppuwritenode(ppufile,t2);
  962. ppufile.putset(tppuset1(loopflags));
  963. end;
  964. procedure tloopnode.buildderefimpl;
  965. begin
  966. inherited buildderefimpl;
  967. if assigned(t1) then
  968. t1.buildderefimpl;
  969. if assigned(t2) then
  970. t2.buildderefimpl;
  971. end;
  972. procedure tloopnode.derefimpl;
  973. begin
  974. inherited derefimpl;
  975. if assigned(t1) then
  976. t1.derefimpl;
  977. if assigned(t2) then
  978. t2.derefimpl;
  979. end;
  980. function tloopnode.dogetcopy : tnode;
  981. var
  982. p : tloopnode;
  983. begin
  984. p:=tloopnode(inherited dogetcopy);
  985. if assigned(t1) then
  986. p.t1:=t1.dogetcopy
  987. else
  988. p.t1:=nil;
  989. if assigned(t2) then
  990. p.t2:=t2.dogetcopy
  991. else
  992. p.t2:=nil;
  993. p.loopflags:=loopflags;
  994. dogetcopy:=p;
  995. end;
  996. procedure tloopnode.insertintolist(l : tnodelist);
  997. begin
  998. end;
  999. procedure tloopnode.printnodetree(var t:text);
  1000. begin
  1001. write(t,printnodeindention,'(');
  1002. printnodeindent;
  1003. printnodeinfo(t);
  1004. writeln(t);
  1005. printnode(t,left);
  1006. printnode(t,right);
  1007. printnode(t,t1);
  1008. printnode(t,t2);
  1009. printnodeunindent;
  1010. writeln(t,printnodeindention,')');
  1011. end;
  1012. {$ifdef DEBUG_NODE_XML}
  1013. procedure TLoopNode.XMLPrintNodeInfo(var T: Text);
  1014. var
  1015. i: TLoopFlag;
  1016. First: Boolean;
  1017. begin
  1018. inherited XMLPrintNodeInfo(T);
  1019. First := True;
  1020. for i := Low(TLoopFlag) to High(TLoopFlag) do
  1021. if i in loopflags then
  1022. begin
  1023. if First then
  1024. begin
  1025. Write(T, ' loopflags="', i);
  1026. First := False;
  1027. end
  1028. else
  1029. Write(T, ',', i)
  1030. end;
  1031. if not First then
  1032. Write(T, '"');
  1033. end;
  1034. procedure TLoopNode.XMLPrintNodeTree(var T: Text);
  1035. begin
  1036. Write(T, PrintNodeIndention, '<', nodetype2str[nodetype]);
  1037. XMLPrintNodeInfo(T);
  1038. WriteLn(T, '>');
  1039. PrintNodeIndent;
  1040. if Assigned(Left) then
  1041. begin
  1042. if nodetype = forn then
  1043. WriteLn(T, PrintNodeIndention, '<counter>')
  1044. else
  1045. WriteLn(T, PrintNodeIndention, '<condition>');
  1046. PrintNodeIndent;
  1047. XMLPrintNode(T, Left);
  1048. PrintNodeUnindent;
  1049. if nodetype = forn then
  1050. WriteLn(T, PrintNodeIndention, '</counter>')
  1051. else
  1052. WriteLn(T, PrintNodeIndention, '</condition>');
  1053. end;
  1054. if Assigned(Right) then
  1055. begin
  1056. case nodetype of
  1057. ifn:
  1058. WriteLn(T, PrintNodeIndention, '<then>');
  1059. forn:
  1060. WriteLn(T, PrintNodeIndention, '<first>');
  1061. else
  1062. WriteLn(T, PrintNodeIndention, '<right>');
  1063. end;
  1064. PrintNodeIndent;
  1065. XMLPrintNode(T, Right);
  1066. PrintNodeUnindent;
  1067. case nodetype of
  1068. ifn:
  1069. WriteLn(T, PrintNodeIndention, '</then>');
  1070. forn:
  1071. WriteLn(T, PrintNodeIndention, '</first>');
  1072. else
  1073. WriteLn(T, PrintNodeIndention, '</right>');
  1074. end;
  1075. end;
  1076. if Assigned(t1) then
  1077. begin
  1078. case nodetype of
  1079. ifn:
  1080. WriteLn(T, PrintNodeIndention, '<else>');
  1081. forn:
  1082. WriteLn(T, PrintNodeIndention, '<last>');
  1083. else
  1084. WriteLn(T, PrintNodeIndention, '<t1>');
  1085. end;
  1086. PrintNodeIndent;
  1087. XMLPrintNode(T, t1);
  1088. PrintNodeUnindent;
  1089. case nodetype of
  1090. ifn:
  1091. WriteLn(T, PrintNodeIndention, '</else>');
  1092. forn:
  1093. WriteLn(T, PrintNodeIndention, '</last>');
  1094. else
  1095. WriteLn(T, PrintNodeIndention, '</t1>');
  1096. end;
  1097. end;
  1098. if Assigned(t2) then
  1099. begin
  1100. if nodetype <> forn then
  1101. begin
  1102. WriteLn(T, PrintNodeIndention, '<loop>');
  1103. PrintNodeIndent;
  1104. end;
  1105. XMLPrintNode(T, t2);
  1106. if nodetype <> forn then
  1107. begin
  1108. PrintNodeUnindent;
  1109. WriteLn(T, PrintNodeIndention, '</loop>');
  1110. end;
  1111. end;
  1112. PrintNodeUnindent;
  1113. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  1114. end;
  1115. {$endif DEBUG_NODE_XML}
  1116. function tloopnode.docompare(p: tnode): boolean;
  1117. begin
  1118. docompare :=
  1119. inherited docompare(p) and
  1120. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  1121. t1.isequal(tloopnode(p).t1) and
  1122. t2.isequal(tloopnode(p).t2);
  1123. end;
  1124. {****************************************************************************
  1125. TWHILEREPEATNODE
  1126. *****************************************************************************}
  1127. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  1128. begin
  1129. inherited create(whilerepeatn,l,r,nil,nil);
  1130. if tab then
  1131. include(loopflags, lnf_testatbegin);
  1132. if cn then
  1133. include(loopflags,lnf_checknegate);
  1134. end;
  1135. function twhilerepeatnode.pass_typecheck:tnode;
  1136. var
  1137. t:Tunarynode;
  1138. begin
  1139. result:=nil;
  1140. resultdef:=voidtype;
  1141. typecheckpass(left);
  1142. { tp procvar support }
  1143. maybe_call_procvar(left,true);
  1144. {A not node can be removed.}
  1145. if left.nodetype=notn then
  1146. begin
  1147. t:=Tunarynode(left);
  1148. left:=Tunarynode(left).left;
  1149. t.left:=nil;
  1150. t.destroy;
  1151. {Symdif operator, in case you are wondering:}
  1152. loopflags:=loopflags >< [lnf_checknegate];
  1153. end;
  1154. { loop instruction }
  1155. if assigned(right) then
  1156. typecheckpass(right);
  1157. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1158. if codegenerror then
  1159. exit;
  1160. if not(is_boolean(left.resultdef)) and
  1161. not(is_typeparam(left.resultdef)) then
  1162. inserttypeconv(left,pasbool1type);
  1163. { Give warnings for code that will never be executed for
  1164. while false do }
  1165. if (lnf_testatbegin in loopflags) and
  1166. (left.nodetype=ordconstn) and
  1167. (tordconstnode(left).value.uvalue=0) and
  1168. not(nf_internal in left.flags) and
  1169. assigned(right) then
  1170. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1171. end;
  1172. {$ifdef prefetchnext}
  1173. type
  1174. passignmentquery = ^tassignmentquery;
  1175. tassignmentquery = record
  1176. towhat: tnode;
  1177. source: tassignmentnode;
  1178. statementcount: cardinal;
  1179. end;
  1180. function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
  1181. var
  1182. query: passignmentquery absolute arg;
  1183. temp, prederef: tnode;
  1184. begin
  1185. result := fen_norecurse_false;
  1186. if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
  1187. inc(query^.statementcount);
  1188. { make sure there's something else in the loop besides going to the }
  1189. { next item }
  1190. if (query^.statementcount > 1) and
  1191. (n.nodetype = assignn) then
  1192. begin
  1193. { skip type conversions of assignment target }
  1194. temp := tassignmentnode(n).left;
  1195. while (temp.nodetype = typeconvn) do
  1196. temp := ttypeconvnode(temp).left;
  1197. { assignment to x of the while assigned(x) check? }
  1198. if not(temp.isequal(query^.towhat)) then
  1199. exit;
  1200. { right hand side of assignment dereferenced field of }
  1201. { x? (no derefn in case of class) }
  1202. temp := tassignmentnode(n).right;
  1203. while (temp.nodetype = typeconvn) do
  1204. temp := ttypeconvnode(temp).left;
  1205. if (temp.nodetype <> subscriptn) then
  1206. exit;
  1207. prederef := tsubscriptnode(temp).left;
  1208. temp := prederef;
  1209. while (temp.nodetype = typeconvn) do
  1210. temp := ttypeconvnode(temp).left;
  1211. { see tests/test/prefetch1.pp }
  1212. if (temp.nodetype = derefn) then
  1213. temp := tderefnode(temp).left
  1214. else
  1215. temp := prederef;
  1216. if temp.isequal(query^.towhat) then
  1217. begin
  1218. query^.source := tassignmentnode(n);
  1219. result := fen_norecurse_true;
  1220. end
  1221. end
  1222. { don't check nodes which can't contain an assignment or whose }
  1223. { final assignment can vary a lot }
  1224. else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
  1225. result := fen_false;
  1226. end;
  1227. function findassignment(where: tnode; towhat: tnode): tassignmentnode;
  1228. var
  1229. query: tassignmentquery;
  1230. begin
  1231. query.towhat := towhat;
  1232. query.source := nil;
  1233. query.statementcount := 0;
  1234. if foreachnodestatic(where,@checkassignment,@query) then
  1235. result := query.source
  1236. else
  1237. result := nil;
  1238. end;
  1239. {$endif prefetchnext}
  1240. function twhilerepeatnode.pass_1 : tnode;
  1241. {$ifdef prefetchnext}
  1242. var
  1243. runnernode, prefetchcode: tnode;
  1244. assignmentnode: tassignmentnode;
  1245. prefetchstatements: tstatementnode;
  1246. {$endif prefetchnext}
  1247. begin
  1248. result:=nil;
  1249. expectloc:=LOC_VOID;
  1250. firstpass(left);
  1251. if codegenerror then
  1252. exit;
  1253. { loop instruction }
  1254. if assigned(right) then
  1255. begin
  1256. firstpass(right);
  1257. if codegenerror then
  1258. exit;
  1259. end;
  1260. {$ifdef prefetchnext}
  1261. { do at the end so all complex typeconversions are already }
  1262. { converted to calln's }
  1263. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1264. (lnf_testatbegin in loopflags) then
  1265. begin
  1266. { get first component of the while check }
  1267. runnernode := left;
  1268. while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
  1269. runnernode := tunarynode(runnernode).left;
  1270. { is it an assigned(x) check? }
  1271. if ((runnernode.nodetype = inlinen) and
  1272. (tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
  1273. ((runnernode.nodetype = unequaln) and
  1274. (taddnode(runnernode).right.nodetype = niln)) then
  1275. begin
  1276. runnernode := tunarynode(runnernode).left;
  1277. { in case of in_assigned_x, there's a callparan in between }
  1278. if (runnernode.nodetype = callparan) then
  1279. runnernode := tcallparanode(runnernode).left;
  1280. while (runnernode.nodetype = typeconvn) do
  1281. runnernode := ttypeconvnode(runnernode).left;
  1282. { is there an "x := x(^).somefield"? }
  1283. assignmentnode := findassignment(right,runnernode);
  1284. if assigned(assignmentnode) then
  1285. begin
  1286. prefetchcode := internalstatements(prefetchstatements);
  1287. addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
  1288. cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
  1289. addstatement(prefetchstatements,right);
  1290. right := prefetchcode;
  1291. typecheckpass(right);
  1292. end;
  1293. end;
  1294. end;
  1295. {$endif prefetchnext}
  1296. end;
  1297. {$ifdef state_tracking}
  1298. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  1299. var condition:Tnode;
  1300. code:Tnode;
  1301. done:boolean;
  1302. value:boolean;
  1303. change:boolean;
  1304. firsttest:boolean;
  1305. factval:Tnode;
  1306. begin
  1307. track_state_pass:=false;
  1308. done:=false;
  1309. firsttest:=true;
  1310. {For repeat until statements, first do a pass through the code.}
  1311. if not(lnf_testatbegin in flags) then
  1312. begin
  1313. code:=right.getcopy;
  1314. if code.track_state_pass(exec_known) then
  1315. track_state_pass:=true;
  1316. code.destroy;
  1317. end;
  1318. repeat
  1319. condition:=left.getcopy;
  1320. code:=right.getcopy;
  1321. change:=condition.track_state_pass(exec_known);
  1322. factval:=aktstate.find_fact(left);
  1323. if factval<>nil then
  1324. begin
  1325. condition.destroy;
  1326. condition:=factval.getcopy;
  1327. change:=true;
  1328. end;
  1329. if change then
  1330. begin
  1331. track_state_pass:=true;
  1332. {Force new resultdef pass.}
  1333. condition.resultdef:=nil;
  1334. do_typecheckpass(condition);
  1335. end;
  1336. if is_constboolnode(condition) then
  1337. begin
  1338. {Try to turn a while loop into a repeat loop.}
  1339. if firsttest then
  1340. exclude(flags,testatbegin);
  1341. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  1342. if value then
  1343. begin
  1344. if code.track_state_pass(exec_known) then
  1345. track_state_pass:=true;
  1346. end
  1347. else
  1348. done:=true;
  1349. end
  1350. else
  1351. begin
  1352. {Remove any modified variables from the state.}
  1353. code.track_state_pass(false);
  1354. done:=true;
  1355. end;
  1356. code.destroy;
  1357. condition.destroy;
  1358. firsttest:=false;
  1359. until done;
  1360. {The loop condition is also known, for example:
  1361. while i<10 do
  1362. begin
  1363. ...
  1364. end;
  1365. When the loop is done, we do know that i<10 = false.
  1366. }
  1367. condition:=left.getcopy;
  1368. if condition.track_state_pass(exec_known) then
  1369. begin
  1370. track_state_pass:=true;
  1371. {Force new resultdef pass.}
  1372. condition.resultdef:=nil;
  1373. do_typecheckpass(condition);
  1374. end;
  1375. if not is_constboolnode(condition) then
  1376. aktstate.store_fact(condition,
  1377. cordconstnode.create(byte(checknegate),pasbool1type,true))
  1378. else
  1379. condition.destroy;
  1380. end;
  1381. {$endif}
  1382. {*****************************************************************************
  1383. TIFNODE
  1384. *****************************************************************************}
  1385. constructor tifnode.create(l,r,_t1 : tnode);
  1386. begin
  1387. inherited create(ifn,l,r,_t1,nil);
  1388. end;
  1389. constructor tifnode.create_internal(l,r,_t1 : tnode);
  1390. begin
  1391. create(l,r,_t1);
  1392. include(flags,nf_internal);
  1393. end;
  1394. function tifnode.internalsimplify(warn: boolean) : tnode;
  1395. {$if defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64)}
  1396. var
  1397. thenstmnt, elsestmnt: tnode;
  1398. in_nr: tinlinenumber;
  1399. paratype: tdef;
  1400. {$endif defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64)}
  1401. begin
  1402. result:=nil;
  1403. { optimize constant expressions }
  1404. if (left.nodetype=ordconstn) then
  1405. begin
  1406. if tordconstnode(left).value.uvalue<>0 then
  1407. begin
  1408. if assigned(right) then
  1409. result:=right
  1410. else
  1411. result:=cnothingnode.create;
  1412. right:=nil;
  1413. if warn and assigned(t1) and not(nf_internal in left.flags) then
  1414. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  1415. end
  1416. else
  1417. begin
  1418. if assigned(t1) then
  1419. result:=t1
  1420. else
  1421. result:=cnothingnode.create;
  1422. t1:=nil;
  1423. if warn and assigned(right) and not(nf_internal in left.flags) then
  1424. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1425. end;
  1426. end;
  1427. {$ifndef llvm}
  1428. {$if defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64)}
  1429. { use min/max intrinsic?
  1430. convert (with <op> being <, >, >=, <=
  1431. if a <op> b then
  1432. x:=a
  1433. else
  1434. x:=b;
  1435. and
  1436. if a <op> b then
  1437. x:=a;
  1438. into appropriate min/max intrinsics
  1439. }
  1440. elsestmnt:=nil;
  1441. in_nr:=Default(tinlinenumber);
  1442. if (cs_opt_level2 in current_settings.optimizerswitches) and
  1443. (left.nodetype in [gtn,gten,ltn,lten]) and IsSingleStatement(right,thenstmnt) and
  1444. ((t1=nil) or IsSingleStatement(t1,elsestmnt)) and
  1445. (thenstmnt.nodetype=assignn) and ((t1=nil) or (elsestmnt.nodetype=assignn)) and
  1446. not(might_have_sideeffects(left)) and
  1447. ((t1=nil) or tassignmentnode(thenstmnt).left.isequal(tassignmentnode(elsestmnt).left)) and
  1448. {$if defined(i386) or defined(x86_64)}
  1449. {$ifdef i386}
  1450. (((current_settings.fputype>=fpu_sse) and is_single(tassignmentnode(thenstmnt).left.resultdef)) or
  1451. ((current_settings.fputype>=fpu_sse2) and is_double(tassignmentnode(thenstmnt).left.resultdef))
  1452. ) and
  1453. {$else i386}
  1454. (is_single(tassignmentnode(thenstmnt).left.resultdef) or is_double(tassignmentnode(thenstmnt).left.resultdef)) and
  1455. {$endif i386}
  1456. {$endif defined(i386) or defined(x86_64)}
  1457. {$if defined(xtensa)}
  1458. (CPUXTENSA_HAS_MINMAX in cpu_capabilities[current_settings.cputype]) and is_32bitint(tassignmentnode(thenstmnt).right.resultdef) and
  1459. {$endif defined(xtensa)}
  1460. {$if defined(aarch64)}
  1461. (is_single(tassignmentnode(thenstmnt).left.resultdef) or is_double(tassignmentnode(thenstmnt).left.resultdef) or
  1462. is_32bitint(tassignmentnode(thenstmnt).right.resultdef)) and
  1463. {$endif defined(aarch64)}
  1464. (
  1465. { the right size of the assignment in the then clause must either }
  1466. { equal to the left ... }
  1467. (tassignmentnode(thenstmnt).right.isequal(taddnode(left).left) and
  1468. { ... and the else clause must be either not exist }
  1469. { and the left side of the assignment in the then clause must be }
  1470. { equal to the right operand of the comparison operator }
  1471. (
  1472. ((t1=nil) and (tassignmentnode(thenstmnt).left.isequal(taddnode(left).right))) or
  1473. { or the else clause exists and the right side of the assignment in the else clause }
  1474. { must be equal to the right side of the comparison operator }
  1475. (assigned(elsestmnt) and tassignmentnode(elsestmnt).right.isequal(taddnode(left).right)))
  1476. ) or
  1477. { ... or right operand of the comparison operator }
  1478. (tassignmentnode(thenstmnt).right.isequal(taddnode(left).right) and
  1479. { ... and the else clause must be either not exist }
  1480. { and the left side of the assignment in the then clause must be }
  1481. { equal to the left operand of the comparison operator }
  1482. (
  1483. ((t1=nil) and (tassignmentnode(thenstmnt).left.isequal(taddnode(left).left))) or
  1484. { or the else clause exists and the right side of the assignment in the else clause }
  1485. { must be equal to the left side of the comparison operator }
  1486. (assigned(elsestmnt) and tassignmentnode(elsestmnt).right.isequal(taddnode(left).left))
  1487. )
  1488. )
  1489. ) then
  1490. begin
  1491. paratype:=tassignmentnode(thenstmnt).left.resultdef;
  1492. if ((left.nodetype in [gtn,gten]) and
  1493. tassignmentnode(thenstmnt).right.isequal(taddnode(left).left)) or
  1494. ((left.nodetype in [ltn,lten]) and
  1495. tassignmentnode(thenstmnt).right.isequal(taddnode(left).right)) then
  1496. begin
  1497. if is_double(paratype) then
  1498. in_nr:=in_max_double
  1499. else if is_single(paratype) then
  1500. in_nr:=in_max_single
  1501. else if is_u32bitint(paratype) then
  1502. in_nr:=in_max_dword
  1503. else if is_s32bitint(paratype) then
  1504. in_nr:=in_max_longint;
  1505. end
  1506. else
  1507. begin
  1508. if is_double(paratype) then
  1509. in_nr:=in_min_double
  1510. else if is_single(paratype) then
  1511. in_nr:=in_min_single
  1512. else if is_u32bitint(paratype) then
  1513. in_nr:=in_min_dword
  1514. else if is_s32bitint(paratype) then
  1515. in_nr:=in_min_longint;
  1516. end;
  1517. { for inline nodes, the first parameter is the last one in the linked list
  1518. Due to the defined behaviour for the min/max intrinsics that in case of a NaN
  1519. the second parameter is taken, we have to put the else part into the second parameter
  1520. thus pass it to the first callparanode call }
  1521. if t1=nil then
  1522. Result:=cassignmentnode.create_internal(tassignmentnode(thenstmnt).left.getcopy,
  1523. cinlinenode.create(in_nr,false,ccallparanode.create(tassignmentnode(thenstmnt).left.getcopy,
  1524. ccallparanode.create(tassignmentnode(thenstmnt).right.getcopy,nil)))
  1525. )
  1526. else
  1527. Result:=cassignmentnode.create_internal(tassignmentnode(thenstmnt).left.getcopy,
  1528. cinlinenode.create(in_nr,false,ccallparanode.create(tassignmentnode(elsestmnt).right.getcopy,
  1529. ccallparanode.create(tassignmentnode(thenstmnt).right.getcopy,nil)))
  1530. );
  1531. end;
  1532. {$endif defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64)}
  1533. {$endif llvm}
  1534. end;
  1535. function tifnode.simplify(forinline : boolean) : tnode;
  1536. begin
  1537. result:=internalsimplify(false);
  1538. end;
  1539. function tifnode.pass_typecheck:tnode;
  1540. begin
  1541. result:=nil;
  1542. resultdef:=voidtype;
  1543. typecheckpass(left);
  1544. { tp procvar support }
  1545. maybe_call_procvar(left,true);
  1546. { if path }
  1547. if assigned(right) then
  1548. typecheckpass(right);
  1549. { else path }
  1550. if assigned(t1) then
  1551. typecheckpass(t1);
  1552. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1553. if codegenerror then
  1554. exit;
  1555. if not(is_boolean(left.resultdef)) and
  1556. not(is_typeparam(left.resultdef)) then
  1557. inserttypeconv(left,pasbool1type);
  1558. result:=internalsimplify(not(nf_internal in flags));
  1559. end;
  1560. function tifnode.pass_1 : tnode;
  1561. begin
  1562. result:=nil;
  1563. expectloc:=LOC_VOID;
  1564. firstpass(left);
  1565. { if path }
  1566. if assigned(right) then
  1567. firstpass(right);
  1568. { else path }
  1569. if assigned(t1) then
  1570. firstpass(t1);
  1571. { leave if we've got an error in one of the paths }
  1572. if codegenerror then
  1573. exit;
  1574. end;
  1575. {*****************************************************************************
  1576. TFORNODE
  1577. *****************************************************************************}
  1578. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  1579. begin
  1580. inherited create(forn,l,r,_t1,_t2);
  1581. if back then
  1582. include(loopflags,lnf_backward);
  1583. include(loopflags,lnf_testatbegin);
  1584. end;
  1585. destructor tfornode.destroy;
  1586. begin
  1587. if assigned(loopiteration) then
  1588. loopiteration.destroy;
  1589. inherited destroy;
  1590. end;
  1591. function tfornode.simplify(forinline : boolean) : tnode;
  1592. begin
  1593. result:=nil;
  1594. { Can we spare the first comparision? }
  1595. if (t1.nodetype=ordconstn) and
  1596. (right.nodetype=ordconstn) and
  1597. (
  1598. (
  1599. (lnf_backward in loopflags) and
  1600. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1601. ) or
  1602. (
  1603. not(lnf_backward in loopflags) and
  1604. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1605. )
  1606. ) then
  1607. exclude(loopflags,lnf_testatbegin);
  1608. if (t1.nodetype=ordconstn) and
  1609. (right.nodetype=ordconstn) and
  1610. (
  1611. (
  1612. (lnf_backward in loopflags) and
  1613. (tordconstnode(right).value<tordconstnode(t1).value)
  1614. ) or
  1615. (
  1616. not(lnf_backward in loopflags) and
  1617. (tordconstnode(right).value>tordconstnode(t1).value)
  1618. )
  1619. ) then
  1620. result:=cnothingnode.create;
  1621. end;
  1622. function tfornode.pass_typecheck:tnode;
  1623. var
  1624. res : tnode;
  1625. rangedef: tdef;
  1626. begin
  1627. result:=nil;
  1628. resultdef:=voidtype;
  1629. { process the loopvar, from and to, varstates are already set }
  1630. typecheckpass(left);
  1631. typecheckpass(right);
  1632. typecheckpass(t1);
  1633. set_varstate(left,vs_written,[]);
  1634. { Make sure that the loop var and the
  1635. from and to values are compatible types }
  1636. if not(m_iso in current_settings.modeswitches) then
  1637. rangedef:=left.resultdef
  1638. else
  1639. rangedef:=get_iso_range_type(left.resultdef);
  1640. check_ranges(right.fileinfo,right,rangedef);
  1641. inserttypeconv(right,rangedef);
  1642. check_ranges(t1.fileinfo,t1,rangedef);
  1643. inserttypeconv(t1,rangedef);
  1644. if assigned(t2) then
  1645. typecheckpass(t2);
  1646. result:=simplify(false);
  1647. { loop unrolling }
  1648. if not(assigned(result)) and
  1649. (cs_opt_loopunroll in current_settings.optimizerswitches) and
  1650. assigned(t2) and
  1651. { statements must be error free }
  1652. not(nf_error in t2.flags) then
  1653. begin
  1654. typecheckpass(t2);
  1655. res:=t2.simplify(false);
  1656. if assigned(res) then
  1657. t2:=res;
  1658. res:=unroll_loop(self);
  1659. if assigned(res) then
  1660. begin
  1661. typecheckpass(res);
  1662. result:=res;
  1663. exit;
  1664. end;
  1665. end;
  1666. end;
  1667. function tfornode.pass_1 : tnode;
  1668. begin
  1669. result:=nil;
  1670. expectloc:=LOC_VOID;
  1671. firstpass(left);
  1672. firstpass(right);
  1673. firstpass(t1);
  1674. if assigned(t2) then
  1675. firstpass(t2);
  1676. end;
  1677. function checkcontinue(var n:tnode; arg: pointer): foreachnoderesult;
  1678. begin
  1679. if n.nodetype=continuen then
  1680. result:=fen_norecurse_true
  1681. else
  1682. result:=fen_false;
  1683. end;
  1684. function tfornode.makewhileloop : tnode;
  1685. var
  1686. ifblock,loopblock : tblocknode;
  1687. ifstatements,statements,loopstatements : tstatementnode;
  1688. fromtemp,totemp : ttempcreatenode;
  1689. do_loopvar_at_end : Boolean;
  1690. { if the lower and/or upper bound are variable, we need a surrounding if }
  1691. needsifblock : Boolean;
  1692. cond : tnodetype;
  1693. fromexpr : tnode;
  1694. toexpr, leftcopy: tnode;
  1695. { if the upper bound is not constant, it must be store in a temp initially }
  1696. usetotemp : boolean;
  1697. { if the lower bound is not constant, it must be store in a temp before calculating the upper bound }
  1698. usefromtemp : boolean;
  1699. storefilepos: tfileposinfo;
  1700. countermin, countermax: Tconstexprint;
  1701. procedure iterate_counter(var s : tstatementnode;fw : boolean);
  1702. var
  1703. leftcopy: tnode;
  1704. begin
  1705. { get rid of nf_write etc. as the left node is now only read }
  1706. leftcopy:=left.getcopy;
  1707. node_reset_flags(leftcopy,[nf_pass1_done,nf_modify,nf_write]);
  1708. if fw then
  1709. addstatement(s,
  1710. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_succ_x,false,leftcopy)))
  1711. else
  1712. addstatement(s,
  1713. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_pred_x,false,leftcopy)));
  1714. end;
  1715. function iterate_counter_func(arg : tnode;fw : boolean) : tnode;
  1716. begin
  1717. if fw then
  1718. result:=cinlinenode.createintern(in_succ_x,false,arg)
  1719. else
  1720. result:=cinlinenode.createintern(in_pred_x,false,arg);
  1721. end;
  1722. begin
  1723. result:=nil;
  1724. totemp:=nil;
  1725. fromtemp:=nil;
  1726. storefilepos:=current_filepos;
  1727. current_filepos:=fileinfo;
  1728. case left.resultdef.typ of
  1729. enumdef:
  1730. begin
  1731. countermin:=tenumdef(left.resultdef).min;
  1732. countermax:=tenumdef(left.resultdef).max;
  1733. end;
  1734. orddef:
  1735. begin
  1736. countermin:=torddef(left.resultdef).low;
  1737. countermax:=torddef(left.resultdef).high;
  1738. end;
  1739. else
  1740. Internalerror(2020012601);
  1741. end;
  1742. { check if we can pred/succ the loop var at the end }
  1743. do_loopvar_at_end:=(lnf_dont_mind_loopvar_on_exit in loopflags) and
  1744. is_constnode(right) and is_constnode(t1) and
  1745. { 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
  1746. because we either get an overflow/underflow or the compiler removes the check as it never can be true }
  1747. { checking just the min./max. value depending on the pure size of the counter does not work as the check might
  1748. get optimized away
  1749. 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
  1750. 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
  1751. not((lnf_backward in loopflags) and not(is_signed(left.resultdef)) and (get_ordinal_value(t1)=0)) and
  1752. not((lnf_backward in loopflags) and is_signed(left.resultdef) and (get_ordinal_value(t1)=(-Tconstexprint(1 shl (left.resultdef.size*8-1))))) and
  1753. }
  1754. not(not(lnf_backward in loopflags) and (get_ordinal_value(t1)=countermax)) and
  1755. not((lnf_backward in loopflags) and (get_ordinal_value(t1)=countermin)) and
  1756. { neither might the for loop contain a continue statement as continue in a while loop would skip the increment at the end
  1757. of the loop, this could be overcome by replacing the continue statement with an pred/succ; continue sequence }
  1758. not(foreachnodestatic(t2,@checkcontinue,nil)) and
  1759. { if the loop is unrolled and there is a jump into the loop,
  1760. then we can't do the trick with incrementing the loop var only at the
  1761. end
  1762. }
  1763. not(assigned(entrylabel));
  1764. needsifblock:=not(is_constnode(right)) or not(is_constnode(t1));
  1765. { convert the for loop into a while loop }
  1766. result:=internalstatements(statements);
  1767. ifblock:=internalstatements(ifstatements);
  1768. loopblock:=internalstatements(loopstatements);
  1769. usefromtemp:=(might_have_sideeffects(t1) and not(is_const(right))) or (node_complexity(right)>1);
  1770. usetotemp:=not(is_const(t1));
  1771. if needsifblock then
  1772. begin
  1773. { do not generate a temp. for the from node, if it is a const, it can be copied directly since
  1774. no side effect might change it }
  1775. if usefromtemp then
  1776. begin
  1777. fromtemp:=ctempcreatenode.create(right.resultdef,right.resultdef.size,tt_persistent,true);
  1778. { the if block might be optimized out, so we put the deletetempnode after the if-block, however,
  1779. this causes a long life time of the fromtemp. If the final regsync is left away, the reg. allocator
  1780. figures out the needed life time. As their are no loops involved between the uses of the fromtemp,
  1781. this does no hurt }
  1782. fromtemp.includetempflag(ti_no_final_regsync);
  1783. addstatement(statements,fromtemp);
  1784. { while it would be beneficial to fold the initial reverse succ/pred into this assignment, this is
  1785. not possible because it might wrap around and the if check later on goes wrong }
  1786. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(fromtemp),right.getcopy));
  1787. end;
  1788. if usetotemp then
  1789. begin
  1790. totemp:=ctempcreatenode.create(t1.resultdef,t1.resultdef.size,tt_persistent,true);
  1791. addstatement(statements,totemp);
  1792. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(totemp),t1.getcopy));
  1793. end;
  1794. if usefromtemp then
  1795. begin
  1796. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,ctemprefnode.create(fromtemp)));
  1797. if not(do_loopvar_at_end) then
  1798. iterate_counter(ifstatements,lnf_backward in loopflags);
  1799. end
  1800. else
  1801. begin
  1802. if not(do_loopvar_at_end) then
  1803. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
  1804. iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
  1805. else
  1806. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
  1807. end;
  1808. end
  1809. else
  1810. begin
  1811. if not(do_loopvar_at_end) then
  1812. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
  1813. iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
  1814. else
  1815. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
  1816. end;
  1817. if assigned(entrylabel) then
  1818. addstatement(ifstatements,cgotonode.create(tlabelnode(entrylabel).labsym));
  1819. if not(do_loopvar_at_end) then
  1820. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1821. { avoid copying t2, it is used only once and it might be big }
  1822. addstatement(loopstatements,t2);
  1823. t2:=nil;
  1824. if do_loopvar_at_end then
  1825. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1826. if do_loopvar_at_end then
  1827. begin
  1828. if lnf_backward in loopflags then
  1829. cond:=ltn
  1830. else
  1831. cond:=gtn;
  1832. end
  1833. else
  1834. begin
  1835. if lnf_backward in loopflags then
  1836. cond:=lten
  1837. else
  1838. cond:=gten;
  1839. end;
  1840. { get rid of nf_write etc. as the left node is now only read }
  1841. leftcopy:=left.getcopy;
  1842. node_reset_flags(leftcopy,[nf_pass1_done,nf_modify,nf_write]);
  1843. if needsifblock then
  1844. begin
  1845. if usetotemp then
  1846. toexpr:=ctemprefnode.create(totemp)
  1847. else
  1848. toexpr:=t1.getcopy;
  1849. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,leftcopy,toexpr),loopblock,false,true));
  1850. if usefromtemp then
  1851. fromexpr:=ctemprefnode.create(fromtemp)
  1852. else
  1853. fromexpr:=right.getcopy;
  1854. if usetotemp then
  1855. toexpr:=ctemprefnode.create(totemp)
  1856. else
  1857. toexpr:=t1.getcopy;
  1858. if lnf_backward in loopflags then
  1859. addstatement(statements,cifnode.create(caddnode.create_internal(gten,
  1860. fromexpr,toexpr),ifblock,nil))
  1861. else
  1862. addstatement(statements,cifnode.create(caddnode.create_internal(lten,
  1863. fromexpr,toexpr),ifblock,nil));
  1864. if usetotemp then
  1865. addstatement(statements,ctempdeletenode.create(totemp));
  1866. if usefromtemp then
  1867. addstatement(statements,ctempdeletenode.create(fromtemp));
  1868. end
  1869. else
  1870. begin
  1871. { is a simple comparision for equality sufficient? }
  1872. if do_loopvar_at_end and (lnf_backward in loopflags) and (lnf_counter_not_used in loopflags) then
  1873. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(equaln,leftcopy,
  1874. caddnode.create_internal(subn,t1.getcopy,cordconstnode.create(1,t1.resultdef,false))),loopblock,false,true))
  1875. else
  1876. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,leftcopy,t1.getcopy),loopblock,false,true));
  1877. addstatement(statements,ifblock);
  1878. end;
  1879. current_filepos:=storefilepos;
  1880. end;
  1881. {*****************************************************************************
  1882. TEXITNODE
  1883. *****************************************************************************}
  1884. constructor texitnode.create(l:tnode);
  1885. begin
  1886. inherited create(exitn,l);
  1887. if assigned(left) then
  1888. begin
  1889. { add assignment to funcretsym }
  1890. left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
  1891. left:=cassignmentnode.create(
  1892. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1893. left);
  1894. end;
  1895. end;
  1896. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1897. begin
  1898. inherited ppuload(t,ppufile);
  1899. end;
  1900. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1901. begin
  1902. inherited ppuwrite(ppufile);
  1903. end;
  1904. function texitnode.pass_typecheck:tnode;
  1905. var
  1906. newstatement : tstatementnode;
  1907. ressym: tsym;
  1908. resdef: tdef;
  1909. begin
  1910. result:=nil;
  1911. newstatement:=nil;
  1912. if assigned(left) then
  1913. begin
  1914. result:=internalstatements(newstatement);
  1915. addstatement(newstatement,left);
  1916. left:=nil;
  1917. end;
  1918. { if the function result has been migrated to the parentfpstruct,
  1919. we have to load it back to the original location (from which the
  1920. code generator will load it into the function result location),
  1921. because the code to this that we add in tnodeutils.wrap_proc_body()
  1922. gets inserted before the exit label to which this node will jump }
  1923. if (target_info.system in systems_fpnestedstruct) and
  1924. not(nf_internal in flags) and
  1925. current_procinfo.procdef.get_funcretsym_info(ressym,resdef) and
  1926. (tabstractnormalvarsym(ressym).inparentfpstruct) then
  1927. begin
  1928. if not assigned(result) then
  1929. result:=internalstatements(newstatement);
  1930. cnodeutils.load_parentfpstruct_nested_funcret(ressym,newstatement);
  1931. end;
  1932. if assigned(result) then
  1933. begin
  1934. addstatement(newstatement,self.getcopy);
  1935. { ensure we don't insert the function result loading code again for
  1936. this node }
  1937. include(newstatement.left.flags,nf_internal);
  1938. end;
  1939. resultdef:=voidtype;
  1940. end;
  1941. function texitnode.pass_1 : tnode;
  1942. begin
  1943. result:=nil;
  1944. expectloc:=LOC_VOID;
  1945. if assigned(left) then
  1946. internalerror(2011052801);
  1947. end;
  1948. {*****************************************************************************
  1949. TBREAKNODE
  1950. *****************************************************************************}
  1951. constructor tbreaknode.create;
  1952. begin
  1953. inherited create(breakn);
  1954. end;
  1955. function tbreaknode.pass_typecheck:tnode;
  1956. begin
  1957. result:=nil;
  1958. resultdef:=voidtype;
  1959. end;
  1960. function tbreaknode.pass_1 : tnode;
  1961. begin
  1962. result:=nil;
  1963. expectloc:=LOC_VOID;
  1964. end;
  1965. {*****************************************************************************
  1966. TCONTINUENODE
  1967. *****************************************************************************}
  1968. constructor tcontinuenode.create;
  1969. begin
  1970. inherited create(continuen);
  1971. end;
  1972. function tcontinuenode.pass_typecheck:tnode;
  1973. begin
  1974. result:=nil;
  1975. resultdef:=voidtype;
  1976. end;
  1977. function tcontinuenode.pass_1 : tnode;
  1978. begin
  1979. result:=nil;
  1980. expectloc:=LOC_VOID;
  1981. end;
  1982. {*****************************************************************************
  1983. TGOTONODE
  1984. *****************************************************************************}
  1985. constructor tgotonode.create(p : tlabelsym);
  1986. begin
  1987. inherited create(goton);
  1988. exceptionblock:=current_exceptblock;
  1989. labelnode:=nil;
  1990. labelsym:=p;
  1991. end;
  1992. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1993. begin
  1994. inherited ppuload(t,ppufile);
  1995. labelnodeidx:=ppufile.getlongint;
  1996. exceptionblock:=ppufile.getbyte;
  1997. end;
  1998. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  1999. begin
  2000. inherited ppuwrite(ppufile);
  2001. labelnodeidx:=labelnode.ppuidx;
  2002. ppufile.putlongint(labelnodeidx);
  2003. ppufile.putbyte(exceptionblock);
  2004. end;
  2005. procedure tgotonode.buildderefimpl;
  2006. begin
  2007. inherited buildderefimpl;
  2008. end;
  2009. procedure tgotonode.derefimpl;
  2010. begin
  2011. inherited derefimpl;
  2012. end;
  2013. procedure tgotonode.resolveppuidx;
  2014. begin
  2015. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  2016. if labelnode.nodetype<>labeln then
  2017. internalerror(200809021);
  2018. end;
  2019. function tgotonode.pass_typecheck:tnode;
  2020. begin
  2021. result:=nil;
  2022. resultdef:=voidtype;
  2023. end;
  2024. function tgotonode.pass_1 : tnode;
  2025. var
  2026. p2 : tprocinfo;
  2027. begin
  2028. result:=nil;
  2029. expectloc:=LOC_VOID;
  2030. { The labelnode can already be set when
  2031. this node was copied }
  2032. if not(assigned(labelnode)) then
  2033. begin
  2034. { inner procedure goto? }
  2035. if assigned(labelsym.code) and
  2036. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  2037. { generated by the optimizer? }
  2038. not(assigned(labelsym.owner))) then
  2039. labelnode:=tlabelnode(labelsym.code)
  2040. else if ((m_non_local_goto in current_settings.modeswitches) and
  2041. assigned(labelsym.owner)) or
  2042. { nested exits don't need the non local goto switch }
  2043. (labelsym.realname='$nestedexit') then
  2044. begin
  2045. if current_procinfo.procdef.parast.symtablelevel>=labelsym.owner.symtablelevel then
  2046. begin
  2047. { don't mess with the exception blocks, global gotos in/out side exception blocks are not allowed }
  2048. if exceptionblock>0 then
  2049. CGMessage(cg_e_goto_inout_of_exception_block);
  2050. { goto across procedures using exception?
  2051. this is not allowed because we cannot
  2052. easily unwind the exception frame
  2053. stack
  2054. }
  2055. p2:=current_procinfo;
  2056. while true do
  2057. begin
  2058. if ((cs_implicit_exceptions in current_settings.moduleswitches) and ((p2.flags*[pi_needs_implicit_finally,pi_has_implicit_finally])<>[])) or
  2059. ((p2.flags*[pi_uses_exceptions])<>[]) then
  2060. Message(cg_e_goto_across_procedures_with_exceptions_not_allowed);
  2061. if labelsym.owner=p2.procdef.localst then
  2062. break;
  2063. p2:=p2.parent
  2064. end;
  2065. if assigned(labelsym.jumpbuf) then
  2066. begin
  2067. result:=ccallnode.createintern('fpc_longjmp',
  2068. ccallparanode.create(cordconstnode.create(1,sinttype,true),
  2069. ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
  2070. nil)));
  2071. end
  2072. else
  2073. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  2074. end
  2075. else
  2076. CGMessagePos(self.fileinfo,cg_e_interprocedural_goto_only_to_outer_scope_allowed);
  2077. end
  2078. else
  2079. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  2080. end;
  2081. { check if we don't mess with exception blocks }
  2082. if assigned(labelnode) and
  2083. (exceptionblock<>labelnode.exceptionblock) then
  2084. CGMessage(cg_e_goto_inout_of_exception_block);
  2085. end;
  2086. function tgotonode.dogetcopy : tnode;
  2087. var
  2088. p : tgotonode;
  2089. begin
  2090. p:=tgotonode(inherited dogetcopy);
  2091. p.exceptionblock:=exceptionblock;
  2092. { generate labelnode if not done yet }
  2093. if not(assigned(labelnode)) then
  2094. begin
  2095. if assigned(labelsym) and assigned(labelsym.code) then
  2096. labelnode:=tlabelnode(labelsym.code)
  2097. end;
  2098. p.labelsym:=labelsym;
  2099. { do not copy the label node here as we do not know if the label node is part of the tree or not,
  2100. this will be fixed after the copying in node.setuplabelnode: if the labelnode has copiedto set,
  2101. labelnode of the goto node is update }
  2102. if assigned(labelnode) then
  2103. p.labelnode:=labelnode
  2104. else
  2105. begin
  2106. { don't trigger IE when there was already an error, i.e. the
  2107. label is not defined. See tw11763 (PFV) }
  2108. if (errorcount=0) and
  2109. { don't trigger IE if it's a global goto }
  2110. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  2111. not(assigned(labelsym.owner))) then
  2112. internalerror(200610291);
  2113. end;
  2114. result:=p;
  2115. end;
  2116. function tgotonode.docompare(p: tnode): boolean;
  2117. begin
  2118. docompare := false;
  2119. end;
  2120. {*****************************************************************************
  2121. TLABELNODE
  2122. *****************************************************************************}
  2123. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  2124. begin
  2125. inherited create(labeln,l);
  2126. exceptionblock:=current_exceptblock;
  2127. labsym:=alabsym;
  2128. { Register labelnode in labelsym }
  2129. labsym.code:=self;
  2130. end;
  2131. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2132. begin
  2133. inherited ppuload(t,ppufile);
  2134. exceptionblock:=ppufile.getbyte;
  2135. end;
  2136. destructor tlabelnode.destroy;
  2137. begin
  2138. if assigned(labsym) then
  2139. begin
  2140. if not assigned(labsym.Owner) then
  2141. labsym.Free // Free labelsym if it has no owner
  2142. else
  2143. if labsym.code=pointer(self) then
  2144. begin
  2145. { Remove reference in labelsym, this is to prevent
  2146. goto's to this label }
  2147. labsym.code:=nil;
  2148. end;
  2149. end;
  2150. inherited destroy;
  2151. end;
  2152. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  2153. begin
  2154. inherited ppuwrite(ppufile);
  2155. ppufile.putbyte(exceptionblock);
  2156. end;
  2157. procedure tlabelnode.buildderefimpl;
  2158. begin
  2159. inherited buildderefimpl;
  2160. end;
  2161. procedure tlabelnode.derefimpl;
  2162. begin
  2163. inherited derefimpl;
  2164. end;
  2165. function tlabelnode.pass_typecheck:tnode;
  2166. begin
  2167. result:=nil;
  2168. { left could still be unassigned }
  2169. if assigned(left) then
  2170. typecheckpass(left);
  2171. resultdef:=voidtype;
  2172. end;
  2173. function tlabelnode.pass_1 : tnode;
  2174. begin
  2175. result:=nil;
  2176. expectloc:=LOC_VOID;
  2177. if not (nf_internal in flags) then
  2178. include(current_procinfo.flags,pi_has_label);
  2179. if assigned(left) then
  2180. firstpass(left);
  2181. if (m_non_local_goto in current_settings.modeswitches) and
  2182. { the owner can be Nil for internal labels }
  2183. assigned(labsym.owner) and
  2184. (current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
  2185. CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
  2186. end;
  2187. function tlabelnode.dogetcopy : tnode;
  2188. begin
  2189. if not(assigned(copiedto)) then
  2190. copiedto:=tlabelnode(inherited dogetcopy);
  2191. copiedto.exceptionblock:=exceptionblock;
  2192. result:=copiedto;
  2193. end;
  2194. function tlabelnode.docompare(p: tnode): boolean;
  2195. begin
  2196. docompare := false;
  2197. end;
  2198. {*****************************************************************************
  2199. TRAISENODE
  2200. *****************************************************************************}
  2201. constructor traisenode.create(l,taddr,tframe:tnode);
  2202. begin
  2203. inherited create(raisen,l,taddr,tframe);
  2204. end;
  2205. function traisenode.pass_typecheck:tnode;
  2206. begin
  2207. result:=nil;
  2208. resultdef:=voidtype;
  2209. if assigned(left) then
  2210. begin
  2211. { first para must be a _class_ }
  2212. typecheckpass(left);
  2213. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2214. if codegenerror then
  2215. exit;
  2216. if not is_class(left.resultdef) and
  2217. not is_javaclass(left.resultdef) then
  2218. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2219. { insert needed typeconvs for addr,frame }
  2220. if assigned(right) then
  2221. begin
  2222. { addr }
  2223. typecheckpass(right);
  2224. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2225. inserttypeconv(right,voidcodepointertype);
  2226. { frame }
  2227. if assigned(third) then
  2228. begin
  2229. typecheckpass(third);
  2230. set_varstate(third,vs_read,[vsf_must_be_valid]);
  2231. inserttypeconv(third,voidpointertype);
  2232. end;
  2233. end;
  2234. end;
  2235. end;
  2236. function traisenode.pass_1 : tnode;
  2237. var
  2238. statements : tstatementnode;
  2239. current_addr : tlabelnode;
  2240. raisenode : tcallnode;
  2241. begin
  2242. result:=internalstatements(statements);
  2243. if assigned(left) then
  2244. begin
  2245. { first para must be a class }
  2246. firstpass(left);
  2247. { insert needed typeconvs for addr,frame }
  2248. if assigned(right) then
  2249. begin
  2250. { addr }
  2251. firstpass(right);
  2252. { frame }
  2253. if assigned(third) then
  2254. firstpass(third)
  2255. else
  2256. third:=cpointerconstnode.Create(0,voidpointertype);
  2257. end
  2258. else
  2259. begin
  2260. third:=cinlinenode.create(in_get_frame,false,nil);
  2261. current_addr:=clabelnode.create(cnothingnode.create,clabelsym.create('$raiseaddr'));
  2262. include(current_addr.flags,nf_internal);
  2263. addstatement(statements,current_addr);
  2264. right:=caddrnode.create(cloadnode.create(current_addr.labsym,current_addr.labsym.owner));
  2265. { raise address off by one so we are for sure inside the action area for the raise }
  2266. if tf_use_psabieh in target_info.flags then
  2267. right:=caddnode.create_internal(addn,right,cordconstnode.create(1,sizesinttype,false));
  2268. end;
  2269. raisenode:=ccallnode.createintern('fpc_raiseexception',
  2270. ccallparanode.create(third,
  2271. ccallparanode.create(right,
  2272. ccallparanode.create(left,nil)))
  2273. );
  2274. include(raisenode.callnodeflags,cnf_call_never_returns);
  2275. addstatement(statements,raisenode);
  2276. end
  2277. else
  2278. begin
  2279. addstatement(statements,ccallnode.createintern('fpc_popaddrstack',nil));
  2280. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  2281. include(raisenode.callnodeflags,cnf_call_never_returns);
  2282. addstatement(statements,raisenode);
  2283. end;
  2284. left:=nil;
  2285. right:=nil;
  2286. third:=nil;
  2287. end;
  2288. {*****************************************************************************
  2289. TTRYEXCEPTNODE
  2290. *****************************************************************************}
  2291. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  2292. begin
  2293. inherited create(tryexceptn,l,r,_t1,nil);
  2294. end;
  2295. function ttryexceptnode.pass_typecheck:tnode;
  2296. begin
  2297. result:=nil;
  2298. typecheckpass(left);
  2299. { on statements }
  2300. if assigned(right) then
  2301. typecheckpass(right);
  2302. { else block }
  2303. if assigned(t1) then
  2304. typecheckpass(t1);
  2305. resultdef:=voidtype;
  2306. end;
  2307. function ttryexceptnode.pass_1 : tnode;
  2308. begin
  2309. result:=nil;
  2310. expectloc:=LOC_VOID;
  2311. firstpass(left);
  2312. { on statements }
  2313. if assigned(right) then
  2314. firstpass(right);
  2315. { else block }
  2316. if assigned(t1) then
  2317. firstpass(t1);
  2318. include(current_procinfo.flags,pi_do_call);
  2319. include(current_procinfo.flags,pi_uses_exceptions);
  2320. adjust_estimated_stack_size;
  2321. end;
  2322. function ttryexceptnode.simplify(forinline: boolean): tnode;
  2323. begin
  2324. result:=nil;
  2325. { empty try -> can never raise exception -> do nothing }
  2326. if has_no_code(left) then
  2327. result:=cnothingnode.create;
  2328. end;
  2329. procedure ttryexceptnode.adjust_estimated_stack_size;
  2330. begin
  2331. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size*2);
  2332. end;
  2333. {*****************************************************************************
  2334. TTRYFINALLYNODE
  2335. *****************************************************************************}
  2336. constructor ttryfinallynode.create(l,r:tnode);
  2337. begin
  2338. inherited create(tryfinallyn,l,r,nil);
  2339. third:=nil;
  2340. implicitframe:=false;
  2341. end;
  2342. constructor ttryfinallynode.create_implicit(l,r:tnode);
  2343. begin
  2344. inherited create(tryfinallyn,l,r,nil);
  2345. third:=nil;
  2346. implicitframe:=true;
  2347. end;
  2348. function ttryfinallynode.pass_typecheck:tnode;
  2349. begin
  2350. result:=nil;
  2351. resultdef:=voidtype;
  2352. typecheckpass(left);
  2353. // "try block" is "used"? (JM)
  2354. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  2355. typecheckpass(right);
  2356. // "except block" is "used"? (JM)
  2357. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  2358. if assigned(third) then
  2359. begin
  2360. typecheckpass(third);
  2361. set_varstate(third,vs_readwritten,[vsf_must_be_valid]);
  2362. end;
  2363. end;
  2364. function ttryfinallynode.pass_1 : tnode;
  2365. begin
  2366. result:=nil;
  2367. expectloc:=LOC_VOID;
  2368. firstpass(left);
  2369. firstpass(right);
  2370. if assigned(third) then
  2371. firstpass(third);
  2372. include(current_procinfo.flags,pi_do_call);
  2373. { pi_uses_exceptions is an information for the optimizer and it
  2374. is only interested in exceptions if they appear inside the body,
  2375. so ignore implicit frames when setting the flag }
  2376. if not(implicitframe) then
  2377. include(current_procinfo.flags,pi_uses_exceptions);
  2378. adjust_estimated_stack_size;
  2379. end;
  2380. function ttryfinallynode.simplify(forinline : boolean): tnode;
  2381. begin
  2382. result:=nil;
  2383. { if the try contains no code, we can kill
  2384. the try and except and return only the
  2385. finally part }
  2386. if has_no_code(left) then
  2387. begin
  2388. result:=right;
  2389. right:=nil;
  2390. end
  2391. { if the finally block contains no code, we can kill
  2392. it and just return the try part }
  2393. else if has_no_code(right) and not(assigned(third)) and not(implicitframe) then
  2394. begin
  2395. result:=left;
  2396. left:=nil;
  2397. end;
  2398. end;
  2399. function ttryfinallynode.dogetcopy: tnode;
  2400. begin
  2401. result:=inherited dogetcopy;
  2402. ttryfinallynode(result).implicitframe:=implicitframe;
  2403. end;
  2404. procedure ttryfinallynode.adjust_estimated_stack_size;
  2405. begin
  2406. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size);
  2407. end;
  2408. {*****************************************************************************
  2409. TONNODE
  2410. *****************************************************************************}
  2411. constructor tonnode.create(l,r:tnode);
  2412. begin
  2413. inherited create(onn,l,r);
  2414. excepTSymtable:=nil;
  2415. excepttype:=nil;
  2416. end;
  2417. destructor tonnode.destroy;
  2418. begin
  2419. { copied nodes don't need to release the symtable }
  2420. if assigned(excepTSymtable) then
  2421. excepTSymtable.free;
  2422. inherited destroy;
  2423. end;
  2424. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2425. begin
  2426. inherited ppuload(t,ppufile);
  2427. excepTSymtable:=nil;
  2428. excepttype:=nil;
  2429. end;
  2430. function tonnode.dogetcopy : tnode;
  2431. var
  2432. n : tonnode;
  2433. begin
  2434. n:=tonnode(inherited dogetcopy);
  2435. if assigned(exceptsymtable) then
  2436. n.exceptsymtable:=exceptsymtable.getcopy
  2437. else
  2438. n.exceptsymtable:=nil;
  2439. n.excepttype:=excepttype;
  2440. result:=n;
  2441. end;
  2442. function tonnode.pass_typecheck:tnode;
  2443. begin
  2444. result:=nil;
  2445. resultdef:=voidtype;
  2446. if not is_class(excepttype) and
  2447. not is_javaclass(excepttype) then
  2448. CGMessage1(type_e_class_type_expected,excepttype.typename);
  2449. if assigned(left) then
  2450. typecheckpass(left);
  2451. if assigned(right) then
  2452. typecheckpass(right);
  2453. end;
  2454. function tonnode.pass_1 : tnode;
  2455. begin
  2456. result:=nil;
  2457. include(current_procinfo.flags,pi_do_call);
  2458. expectloc:=LOC_VOID;
  2459. if assigned(left) then
  2460. firstpass(left);
  2461. if assigned(right) then
  2462. firstpass(right);
  2463. end;
  2464. function tonnode.docompare(p: tnode): boolean;
  2465. begin
  2466. docompare := false;
  2467. end;
  2468. end.