nflw.pas 80 KB

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