nflw.pas 79 KB

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