nflw.pas 79 KB

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