nflw.pas 101 KB

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