nflw.pas 102 KB

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