nflw.pas 79 KB

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