nflw.pas 87 KB

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