nflw.pas 95 KB

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