nflw.pas 99 KB

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