nflw.pas 85 KB

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