nflw.pas 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  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. tloopflags = set of tloopflag;
  41. const
  42. { loop flags which must match to consider loop nodes equal regarding the flags }
  43. loopflagsequal = [lnf_backward];
  44. type
  45. tlabelnode = class;
  46. tloopnode = class(tbinarynode)
  47. t1,t2 : tnode;
  48. loopflags : tloopflags;
  49. constructor create(tt : tnodetype;l,r,_t1,_t2 : tnode);virtual;
  50. destructor destroy;override;
  51. function dogetcopy : tnode;override;
  52. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  53. procedure ppuwrite(ppufile:tcompilerppufile);override;
  54. procedure buildderefimpl;override;
  55. procedure derefimpl;override;
  56. procedure insertintolist(l : tnodelist);override;
  57. procedure printnodetree(var t:text);override;
  58. function docompare(p: tnode): boolean; override;
  59. end;
  60. twhilerepeatnode = class(tloopnode)
  61. constructor create(l,r:Tnode;tab,cn:boolean);virtual;reintroduce;
  62. function pass_typecheck:tnode;override;
  63. function pass_1 : tnode;override;
  64. {$ifdef state_tracking}
  65. function track_state_pass(exec_known:boolean):boolean;override;
  66. {$endif}
  67. end;
  68. twhilerepeatnodeclass = class of twhilerepeatnode;
  69. tifnode = class(tloopnode)
  70. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  71. function pass_typecheck:tnode;override;
  72. function pass_1 : tnode;override;
  73. function simplify : tnode;override;
  74. private
  75. function internalsimplify(warn: boolean) : tnode;
  76. end;
  77. tifnodeclass = class of tifnode;
  78. tfornode = class(tloopnode)
  79. { if count isn divisable by unrolls then
  80. the for loop must jump to this label to get the correct
  81. number of executions }
  82. entrylabel : tnode;
  83. loopvar_notid:cardinal;
  84. constructor create(l,r,_t1,_t2 : tnode;back : boolean);virtual;reintroduce;
  85. procedure loop_var_access(not_type:Tnotification_flag;symbol:Tsym);
  86. function pass_typecheck:tnode;override;
  87. function pass_1 : tnode;override;
  88. function simplify : tnode;override;
  89. end;
  90. tfornodeclass = class of tfornode;
  91. texitnode = class(tunarynode)
  92. constructor create(l:tnode);virtual;
  93. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  94. procedure ppuwrite(ppufile:tcompilerppufile);override;
  95. function pass_typecheck:tnode;override;
  96. function pass_1 : tnode;override;
  97. end;
  98. texitnodeclass = class of texitnode;
  99. tbreaknode = class(tnode)
  100. constructor create;virtual;
  101. function pass_typecheck:tnode;override;
  102. function pass_1 : tnode;override;
  103. end;
  104. tbreaknodeclass = class of tbreaknode;
  105. tcontinuenode = class(tnode)
  106. constructor create;virtual;
  107. function pass_typecheck:tnode;override;
  108. function pass_1 : tnode;override;
  109. end;
  110. tcontinuenodeclass = class of tcontinuenode;
  111. tgotonode = class(tnode)
  112. private
  113. labelnodeidx : longint;
  114. public
  115. labelsym : tlabelsym;
  116. labelnode : tlabelnode;
  117. exceptionblock : integer;
  118. constructor create(p : tlabelsym);virtual;
  119. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  120. procedure ppuwrite(ppufile:tcompilerppufile);override;
  121. procedure buildderefimpl;override;
  122. procedure derefimpl;override;
  123. procedure resolveppuidx;override;
  124. function dogetcopy : tnode;override;
  125. function pass_typecheck:tnode;override;
  126. function pass_1 : tnode;override;
  127. function docompare(p: tnode): boolean; override;
  128. end;
  129. tgotonodeclass = class of tgotonode;
  130. tlabelnode = class(tunarynode)
  131. exceptionblock : integer;
  132. { when copying trees, this points to the newly created copy of a label }
  133. copiedto : tlabelnode;
  134. labsym : tlabelsym;
  135. constructor create(l:tnode;alabsym:tlabelsym);virtual;
  136. destructor destroy;override;
  137. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  138. procedure ppuwrite(ppufile:tcompilerppufile);override;
  139. procedure buildderefimpl;override;
  140. procedure derefimpl;override;
  141. function dogetcopy : tnode;override;
  142. function pass_typecheck:tnode;override;
  143. function pass_1 : tnode;override;
  144. function docompare(p: tnode): boolean; override;
  145. end;
  146. tlabelnodeclass = class of tlabelnode;
  147. traisenode = class(ttertiarynode)
  148. constructor create(l,taddr,tframe:tnode);virtual;
  149. function pass_typecheck:tnode;override;
  150. function pass_1 : tnode;override;
  151. end;
  152. traisenodeclass = class of traisenode;
  153. ttryexceptnode = class(tloopnode)
  154. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  155. function pass_typecheck:tnode;override;
  156. function pass_1 : tnode;override;
  157. end;
  158. ttryexceptnodeclass = class of ttryexceptnode;
  159. ttryfinallynode = class(tloopnode)
  160. implicitframe : boolean;
  161. constructor create(l,r:tnode);virtual;reintroduce;
  162. constructor create_implicit(l,r,_t1:tnode);virtual;
  163. function pass_typecheck:tnode;override;
  164. function pass_1 : tnode;override;
  165. function simplify: tnode;override;
  166. end;
  167. ttryfinallynodeclass = class of ttryfinallynode;
  168. tonnode = class(tbinarynode)
  169. excepTSymtable : TSymtable;
  170. excepttype : tobjectdef;
  171. constructor create(l,r:tnode);virtual;
  172. destructor destroy;override;
  173. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  174. function pass_typecheck:tnode;override;
  175. function pass_1 : tnode;override;
  176. function dogetcopy : tnode;override;
  177. function docompare(p: tnode): boolean; override;
  178. end;
  179. tonnodeclass = class of tonnode;
  180. var
  181. cwhilerepeatnode : twhilerepeatnodeclass;
  182. cifnode : tifnodeclass;
  183. cfornode : tfornodeclass;
  184. cexitnode : texitnodeclass;
  185. cbreaknode : tbreaknodeclass;
  186. ccontinuenode : tcontinuenodeclass;
  187. cgotonode : tgotonodeclass;
  188. clabelnode : tlabelnodeclass;
  189. craisenode : traisenodeclass;
  190. ctryexceptnode : ttryexceptnodeclass;
  191. ctryfinallynode : ttryfinallynodeclass;
  192. connode : tonnodeclass;
  193. // for-in loop helpers
  194. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  195. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  196. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  197. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  198. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  199. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  200. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  201. implementation
  202. uses
  203. globtype,systems,constexp,
  204. cutils,verbose,globals,
  205. symconst,symtable,paramgr,defcmp,defutil,htypechk,pass_1,
  206. ncal,nadd,ncon,nmem,nld,ncnv,nbas,cgobj,nutils,ninl,nset,
  207. {$ifdef state_tracking}
  208. nstate,
  209. {$endif}
  210. cgbase,procinfo
  211. ;
  212. // for-in loop helpers
  213. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  214. begin
  215. result:=cfornode.create(hloopvar,
  216. cinlinenode.create(in_low_x,false,expr.getcopy),
  217. cinlinenode.create(in_high_x,false,expr.getcopy),
  218. hloopbody,
  219. false);
  220. end;
  221. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  222. var
  223. loopstatement, loopbodystatement: tstatementnode;
  224. loopvar, stringvar: ttempcreatenode;
  225. stringindex, loopbody, forloopnode: tnode;
  226. begin
  227. { result is a block of statements }
  228. result:=internalstatements(loopstatement);
  229. { create a temp variable for expression }
  230. stringvar := ctempcreatenode.create(
  231. expr.resultdef,
  232. expr.resultdef.size,
  233. tt_persistent,true);
  234. addstatement(loopstatement,stringvar);
  235. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(stringvar),expr.getcopy));
  236. { create a loop counter: signed integer with size of string length }
  237. loopvar := ctempcreatenode.create(
  238. sinttype,
  239. sinttype.size,
  240. tt_persistent,true);
  241. addstatement(loopstatement,loopvar);
  242. stringindex:=ctemprefnode.create(loopvar);
  243. loopbody:=internalstatements(loopbodystatement);
  244. // for-in loop variable := string_expression[index]
  245. addstatement(loopbodystatement,
  246. cassignmentnode.create(hloopvar, cvecnode.create(ctemprefnode.create(stringvar),stringindex)));
  247. { add the actual statement to the loop }
  248. addstatement(loopbodystatement,hloopbody);
  249. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  250. genintconstnode(1),
  251. cinlinenode.create(in_length_x,false,ctemprefnode.create(stringvar)),
  252. loopbody,
  253. false);
  254. addstatement(loopstatement,forloopnode);
  255. { free the loop counter }
  256. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  257. { free the temp variable for expression }
  258. addstatement(loopstatement,ctempdeletenode.create(stringvar));
  259. end;
  260. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  261. var
  262. loopstatement, loopbodystatement: tstatementnode;
  263. loopvar, arrayvar: ttempcreatenode;
  264. arrayindex, lowbound, highbound, loopbody, forloopnode: tnode;
  265. begin
  266. { result is a block of statements }
  267. result:=internalstatements(loopstatement);
  268. if (node_complexity(expr) > 1) and not is_open_array(expr.resultdef) then
  269. begin
  270. { create a temp variable for expression }
  271. arrayvar := ctempcreatenode.create(
  272. expr.resultdef,
  273. expr.resultdef.size,
  274. tt_persistent,true);
  275. lowbound:=cinlinenode.create(in_low_x,false,ctemprefnode.create(arrayvar));
  276. highbound:=cinlinenode.create(in_high_x,false,ctemprefnode.create(arrayvar));
  277. addstatement(loopstatement,arrayvar);
  278. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(arrayvar),expr.getcopy));
  279. end
  280. else
  281. begin
  282. arrayvar:=nil;
  283. lowbound:=cinlinenode.create(in_low_x,false,expr.getcopy);
  284. highbound:=cinlinenode.create(in_high_x,false,expr.getcopy);
  285. end;
  286. { create a loop counter }
  287. loopvar := ctempcreatenode.create(
  288. tarraydef(expr.resultdef).rangedef,
  289. tarraydef(expr.resultdef).rangedef.size,
  290. tt_persistent,true);
  291. addstatement(loopstatement,loopvar);
  292. arrayindex:=ctemprefnode.create(loopvar);
  293. loopbody:=internalstatements(loopbodystatement);
  294. // for-in loop variable := array_expression[index]
  295. if assigned(arrayvar) then
  296. addstatement(loopbodystatement,
  297. cassignmentnode.create(hloopvar,cvecnode.create(ctemprefnode.create(arrayvar),arrayindex)))
  298. else
  299. addstatement(loopbodystatement,
  300. cassignmentnode.create(hloopvar,cvecnode.create(expr.getcopy,arrayindex)));
  301. { add the actual statement to the loop }
  302. addstatement(loopbodystatement,hloopbody);
  303. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  304. lowbound,
  305. highbound,
  306. loopbody,
  307. false);
  308. addstatement(loopstatement,forloopnode);
  309. { free the loop counter }
  310. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  311. { free the temp variable for expression if needed }
  312. if arrayvar<>nil then
  313. addstatement(loopstatement,ctempdeletenode.create(arrayvar));
  314. end;
  315. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  316. var
  317. loopstatement, loopbodystatement: tstatementnode;
  318. loopvar, setvar: ttempcreatenode;
  319. loopbody, forloopnode: tnode;
  320. begin
  321. { result is a block of statements }
  322. result:=internalstatements(loopstatement);
  323. { create a temp variable for expression }
  324. setvar := ctempcreatenode.create(
  325. expr.resultdef,
  326. expr.resultdef.size,
  327. tt_persistent,true);
  328. addstatement(loopstatement,setvar);
  329. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(setvar),expr.getcopy));
  330. { create a loop counter }
  331. loopvar := ctempcreatenode.create(
  332. tsetdef(expr.resultdef).elementdef,
  333. tsetdef(expr.resultdef).elementdef.size,
  334. tt_persistent,true);
  335. addstatement(loopstatement,loopvar);
  336. // if loopvar in set then
  337. // begin
  338. // hloopvar := loopvar
  339. // for-in loop body
  340. // end
  341. loopbody:=cifnode.create(
  342. cinnode.create(ctemprefnode.create(loopvar),ctemprefnode.create(setvar)),
  343. internalstatements(loopbodystatement),
  344. nil
  345. );
  346. addstatement(loopbodystatement,cassignmentnode.create(hloopvar,ctemprefnode.create(loopvar)));
  347. { add the actual statement to the loop }
  348. addstatement(loopbodystatement,hloopbody);
  349. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  350. cinlinenode.create(in_low_x,false,ctemprefnode.create(setvar)),
  351. cinlinenode.create(in_high_x,false,ctemprefnode.create(setvar)),
  352. loopbody,
  353. false);
  354. addstatement(loopstatement,forloopnode);
  355. { free the loop counter }
  356. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  357. { free the temp variable for expression }
  358. addstatement(loopstatement,ctempdeletenode.create(setvar));
  359. end;
  360. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  361. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  362. var
  363. loopstatement, loopbodystatement: tstatementnode;
  364. enumvar: ttempcreatenode;
  365. loopbody, whileloopnode,
  366. enum_get, enum_move, enum_current, enum_get_params: tnode;
  367. propaccesslist: tpropaccesslist;
  368. enumerator_is_class: boolean;
  369. enumerator_destructor: tprocdef;
  370. begin
  371. { result is a block of statements }
  372. result:=internalstatements(loopstatement);
  373. enumerator_is_class := is_class(enumerator_get.returndef);
  374. { create a temp variable for enumerator }
  375. enumvar := ctempcreatenode.create(
  376. enumerator_get.returndef,
  377. enumerator_get.returndef.size,
  378. tt_persistent,true);
  379. addstatement(loopstatement,enumvar);
  380. if enumerator_get.proctypeoption=potype_operator then
  381. begin
  382. enum_get_params:=ccallparanode.create(expr.getcopy,nil);
  383. enum_get:=ccallnode.create(enum_get_params, tprocsym(enumerator_get.procsym), nil, nil, []);
  384. tcallnode(enum_get).procdefinition:=enumerator_get;
  385. addsymref(enumerator_get.procsym);
  386. end
  387. else
  388. enum_get:=ccallnode.create(nil, tprocsym(enumerator_get.procsym), enumerator_get.owner, expr.getcopy, []);
  389. addstatement(loopstatement,
  390. cassignmentnode.create(
  391. ctemprefnode.create(enumvar),
  392. enum_get
  393. ));
  394. loopbody:=internalstatements(loopbodystatement);
  395. { for-in loop variable := enumerator.current }
  396. if getpropaccesslist(enumerator_current,palt_read,propaccesslist) then
  397. begin
  398. case propaccesslist.firstsym^.sym.typ of
  399. fieldvarsym :
  400. begin
  401. { generate access code }
  402. enum_current:=ctemprefnode.create(enumvar);
  403. propaccesslist_to_node(enum_current,enumerator_current.owner,propaccesslist);
  404. include(enum_current.flags,nf_isproperty);
  405. end;
  406. procsym :
  407. begin
  408. { generate the method call }
  409. enum_current:=ccallnode.create(nil,tprocsym(propaccesslist.firstsym^.sym),enumerator_current.owner,ctemprefnode.create(enumvar),[]);
  410. include(enum_current.flags,nf_isproperty);
  411. end
  412. else
  413. begin
  414. enum_current:=cerrornode.create;
  415. Message(type_e_mismatch);
  416. end;
  417. end;
  418. end
  419. else
  420. enum_current:=cerrornode.create;
  421. addstatement(loopbodystatement,
  422. cassignmentnode.create(hloopvar, enum_current));
  423. { add the actual statement to the loop }
  424. addstatement(loopbodystatement,hloopbody);
  425. enum_move:=ccallnode.create(nil, tprocsym(enumerator_move.procsym), enumerator_move.owner, ctemprefnode.create(enumvar), []);
  426. whileloopnode:=cwhilerepeatnode.create(enum_move,loopbody,true,false);
  427. if enumerator_is_class then
  428. begin
  429. { insert a try-finally and call the destructor for the enumerator in the finally section }
  430. enumerator_destructor:=tobjectdef(enumerator_get.returndef).Finddestructor;
  431. if assigned(enumerator_destructor) then
  432. begin
  433. whileloopnode:=ctryfinallynode.create(
  434. whileloopnode, // try node
  435. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym), // finally node
  436. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[]));
  437. end;
  438. { if getenumerator <> nil then do the loop }
  439. whileloopnode:=cifnode.create(
  440. caddnode.create(unequaln, ctemprefnode.create(enumvar), cnilnode.create),
  441. whileloopnode,
  442. nil
  443. );
  444. end;
  445. addstatement(loopstatement, whileloopnode);
  446. if is_object(enumerator_get.returndef) then
  447. begin
  448. // call the object destructor too
  449. enumerator_destructor:=tobjectdef(enumerator_get.returndef).Finddestructor;
  450. if assigned(enumerator_destructor) then
  451. begin
  452. addstatement(loopstatement,
  453. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym),
  454. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[]));
  455. end;
  456. end;
  457. { free the temp variable for enumerator }
  458. addstatement(loopstatement,ctempdeletenode.create(enumvar));
  459. end;
  460. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  461. var
  462. pd, movenext: tprocdef;
  463. current: tpropertysym;
  464. begin
  465. if expr.nodetype=typen then
  466. result:=create_type_for_in_loop(hloopvar, hloopbody, expr)
  467. else
  468. begin
  469. { loop is made for an expression }
  470. // search for operator first
  471. pd:=search_enumerator_operator(expr.resultdef);
  472. // if there is no operator then search for class/object enumerator method
  473. if (pd=nil) and (expr.resultdef.typ=objectdef) then
  474. pd:=tobjectdef(expr.resultdef).search_enumerator_get;
  475. if pd<>nil then
  476. begin
  477. // seach movenext and current symbols
  478. movenext:=tobjectdef(pd.returndef).search_enumerator_move;
  479. if movenext = nil then
  480. begin
  481. result:=cerrornode.create;
  482. Message1(sym_e_no_enumerator_move,pd.returndef.GetTypeName);
  483. end
  484. else
  485. begin
  486. current:=tpropertysym(tobjectdef(pd.returndef).search_enumerator_current);
  487. if current = nil then
  488. begin
  489. result:=cerrornode.create;
  490. Message1(sym_e_no_enumerator_current,pd.returndef.GetTypeName);
  491. end
  492. else
  493. result:=create_enumerator_for_in_loop(hloopvar, hloopbody, expr, pd, movenext, current);
  494. end;
  495. end
  496. else
  497. begin
  498. case expr.resultdef.typ of
  499. stringdef: result:=create_string_for_in_loop(hloopvar, hloopbody, expr);
  500. arraydef: result:=create_array_for_in_loop(hloopvar, hloopbody, expr);
  501. setdef: result:=create_set_for_in_loop(hloopvar, hloopbody, expr);
  502. else
  503. begin
  504. result:=cerrornode.create;
  505. Message1(sym_e_no_enumerator,expr.resultdef.GetTypeName);
  506. end;
  507. end;
  508. end;
  509. end;
  510. end;
  511. {****************************************************************************
  512. TLOOPNODE
  513. *****************************************************************************}
  514. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  515. begin
  516. inherited create(tt,l,r);
  517. t1:=_t1;
  518. t2:=_t2;
  519. fileinfo:=l.fileinfo;
  520. end;
  521. destructor tloopnode.destroy;
  522. begin
  523. t1.free;
  524. t2.free;
  525. inherited destroy;
  526. end;
  527. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  528. begin
  529. inherited ppuload(t,ppufile);
  530. t1:=ppuloadnode(ppufile);
  531. t2:=ppuloadnode(ppufile);
  532. end;
  533. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  534. begin
  535. inherited ppuwrite(ppufile);
  536. ppuwritenode(ppufile,t1);
  537. ppuwritenode(ppufile,t2);
  538. end;
  539. procedure tloopnode.buildderefimpl;
  540. begin
  541. inherited buildderefimpl;
  542. if assigned(t1) then
  543. t1.buildderefimpl;
  544. if assigned(t2) then
  545. t2.buildderefimpl;
  546. end;
  547. procedure tloopnode.derefimpl;
  548. begin
  549. inherited derefimpl;
  550. if assigned(t1) then
  551. t1.derefimpl;
  552. if assigned(t2) then
  553. t2.derefimpl;
  554. end;
  555. function tloopnode.dogetcopy : tnode;
  556. var
  557. p : tloopnode;
  558. begin
  559. p:=tloopnode(inherited dogetcopy);
  560. if assigned(t1) then
  561. p.t1:=t1.dogetcopy
  562. else
  563. p.t1:=nil;
  564. if assigned(t2) then
  565. p.t2:=t2.dogetcopy
  566. else
  567. p.t2:=nil;
  568. p.loopflags:=loopflags;
  569. dogetcopy:=p;
  570. end;
  571. procedure tloopnode.insertintolist(l : tnodelist);
  572. begin
  573. end;
  574. procedure tloopnode.printnodetree(var t:text);
  575. begin
  576. write(t,printnodeindention,'(');
  577. printnodeindent;
  578. printnodeinfo(t);
  579. writeln(t);
  580. printnode(t,left);
  581. printnode(t,right);
  582. printnode(t,t1);
  583. printnode(t,t2);
  584. printnodeunindent;
  585. writeln(t,printnodeindention,')');
  586. end;
  587. function tloopnode.docompare(p: tnode): boolean;
  588. begin
  589. docompare :=
  590. inherited docompare(p) and
  591. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  592. t1.isequal(tloopnode(p).t1) and
  593. t2.isequal(tloopnode(p).t2);
  594. end;
  595. {****************************************************************************
  596. TWHILEREPEATNODE
  597. *****************************************************************************}
  598. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  599. begin
  600. inherited create(whilerepeatn,l,r,nil,nil);
  601. if tab then
  602. include(loopflags, lnf_testatbegin);
  603. if cn then
  604. include(loopflags,lnf_checknegate);
  605. end;
  606. function twhilerepeatnode.pass_typecheck:tnode;
  607. var
  608. t:Tunarynode;
  609. begin
  610. result:=nil;
  611. resultdef:=voidtype;
  612. typecheckpass(left);
  613. { tp procvar support }
  614. maybe_call_procvar(left,true);
  615. {A not node can be removed.}
  616. if left.nodetype=notn then
  617. begin
  618. t:=Tunarynode(left);
  619. left:=Tunarynode(left).left;
  620. t.left:=nil;
  621. t.destroy;
  622. {Symdif operator, in case you are wondering:}
  623. loopflags:=loopflags >< [lnf_checknegate];
  624. end;
  625. { loop instruction }
  626. if assigned(right) then
  627. typecheckpass(right);
  628. set_varstate(left,vs_read,[vsf_must_be_valid]);
  629. if codegenerror then
  630. exit;
  631. if not is_boolean(left.resultdef) then
  632. begin
  633. if left.resultdef.typ=variantdef then
  634. inserttypeconv(left,booltype)
  635. else
  636. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  637. end;
  638. { Give warnings for code that will never be executed for
  639. while false do }
  640. if (lnf_testatbegin in loopflags) and
  641. (left.nodetype=ordconstn) and
  642. (tordconstnode(left).value.uvalue=0) and
  643. assigned(right) then
  644. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  645. end;
  646. {$ifdef prefetchnext}
  647. type
  648. passignmentquery = ^tassignmentquery;
  649. tassignmentquery = record
  650. towhat: tnode;
  651. source: tassignmentnode;
  652. statementcount: cardinal;
  653. end;
  654. function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
  655. var
  656. query: passignmentquery absolute arg;
  657. temp, prederef: tnode;
  658. begin
  659. result := fen_norecurse_false;
  660. if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
  661. inc(query^.statementcount);
  662. { make sure there's something else in the loop besides going to the }
  663. { next item }
  664. if (query^.statementcount > 1) and
  665. (n.nodetype = assignn) then
  666. begin
  667. { skip type conversions of assignment target }
  668. temp := tassignmentnode(n).left;
  669. while (temp.nodetype = typeconvn) do
  670. temp := ttypeconvnode(temp).left;
  671. { assignment to x of the while assigned(x) check? }
  672. if not(temp.isequal(query^.towhat)) then
  673. exit;
  674. { right hand side of assignment dereferenced field of }
  675. { x? (no derefn in case of class) }
  676. temp := tassignmentnode(n).right;
  677. while (temp.nodetype = typeconvn) do
  678. temp := ttypeconvnode(temp).left;
  679. if (temp.nodetype <> subscriptn) then
  680. exit;
  681. prederef := tsubscriptnode(temp).left;
  682. temp := prederef;
  683. while (temp.nodetype = typeconvn) do
  684. temp := ttypeconvnode(temp).left;
  685. { see tests/test/prefetch1.pp }
  686. if (temp.nodetype = derefn) then
  687. temp := tderefnode(temp).left
  688. else
  689. temp := prederef;
  690. if temp.isequal(query^.towhat) then
  691. begin
  692. query^.source := tassignmentnode(n);
  693. result := fen_norecurse_true;
  694. end
  695. end
  696. { don't check nodes which can't contain an assignment or whose }
  697. { final assignment can vary a lot }
  698. else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
  699. result := fen_false;
  700. end;
  701. function findassignment(where: tnode; towhat: tnode): tassignmentnode;
  702. var
  703. query: tassignmentquery;
  704. begin
  705. query.towhat := towhat;
  706. query.source := nil;
  707. query.statementcount := 0;
  708. if foreachnodestatic(where,@checkassignment,@query) then
  709. result := query.source
  710. else
  711. result := nil;
  712. end;
  713. {$endif prefetchnext}
  714. function twhilerepeatnode.pass_1 : tnode;
  715. {$ifdef prefetchnext}
  716. var
  717. runnernode, prefetchcode: tnode;
  718. assignmentnode: tassignmentnode;
  719. prefetchstatements: tstatementnode;
  720. {$endif prefetchnext}
  721. begin
  722. result:=nil;
  723. expectloc:=LOC_VOID;
  724. firstpass(left);
  725. if codegenerror then
  726. exit;
  727. { loop instruction }
  728. if assigned(right) then
  729. begin
  730. firstpass(right);
  731. if codegenerror then
  732. exit;
  733. end;
  734. {$ifdef prefetchnext}
  735. { do at the end so all complex typeconversions are already }
  736. { converted to calln's }
  737. if (cs_opt_level1 in current_settings.optimizerswitches) and
  738. (lnf_testatbegin in loopflags) then
  739. begin
  740. { get first component of the while check }
  741. runnernode := left;
  742. while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
  743. runnernode := tunarynode(runnernode).left;
  744. { is it an assigned(x) check? }
  745. if ((runnernode.nodetype = inlinen) and
  746. (tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
  747. ((runnernode.nodetype = unequaln) and
  748. (taddnode(runnernode).right.nodetype = niln)) then
  749. begin
  750. runnernode := tunarynode(runnernode).left;
  751. { in case of in_assigned_x, there's a callparan in between }
  752. if (runnernode.nodetype = callparan) then
  753. runnernode := tcallparanode(runnernode).left;
  754. while (runnernode.nodetype = typeconvn) do
  755. runnernode := ttypeconvnode(runnernode).left;
  756. { is there an "x := x(^).somefield"? }
  757. assignmentnode := findassignment(right,runnernode);
  758. if assigned(assignmentnode) then
  759. begin
  760. prefetchcode := internalstatements(prefetchstatements);
  761. addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
  762. cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
  763. addstatement(prefetchstatements,right);
  764. right := prefetchcode;
  765. typecheckpass(right);
  766. end;
  767. end;
  768. end;
  769. {$endif prefetchnext}
  770. end;
  771. {$ifdef state_tracking}
  772. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  773. var condition:Tnode;
  774. code:Tnode;
  775. done:boolean;
  776. value:boolean;
  777. change:boolean;
  778. firsttest:boolean;
  779. factval:Tnode;
  780. begin
  781. track_state_pass:=false;
  782. done:=false;
  783. firsttest:=true;
  784. {For repeat until statements, first do a pass through the code.}
  785. if not(lnf_testatbegin in flags) then
  786. begin
  787. code:=right.getcopy;
  788. if code.track_state_pass(exec_known) then
  789. track_state_pass:=true;
  790. code.destroy;
  791. end;
  792. repeat
  793. condition:=left.getcopy;
  794. code:=right.getcopy;
  795. change:=condition.track_state_pass(exec_known);
  796. factval:=aktstate.find_fact(left);
  797. if factval<>nil then
  798. begin
  799. condition.destroy;
  800. condition:=factval.getcopy;
  801. change:=true;
  802. end;
  803. if change then
  804. begin
  805. track_state_pass:=true;
  806. {Force new resultdef pass.}
  807. condition.resultdef:=nil;
  808. do_typecheckpass(condition);
  809. end;
  810. if is_constboolnode(condition) then
  811. begin
  812. {Try to turn a while loop into a repeat loop.}
  813. if firsttest then
  814. exclude(flags,testatbegin);
  815. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  816. if value then
  817. begin
  818. if code.track_state_pass(exec_known) then
  819. track_state_pass:=true;
  820. end
  821. else
  822. done:=true;
  823. end
  824. else
  825. begin
  826. {Remove any modified variables from the state.}
  827. code.track_state_pass(false);
  828. done:=true;
  829. end;
  830. code.destroy;
  831. condition.destroy;
  832. firsttest:=false;
  833. until done;
  834. {The loop condition is also known, for example:
  835. while i<10 do
  836. begin
  837. ...
  838. end;
  839. When the loop is done, we do know that i<10 = false.
  840. }
  841. condition:=left.getcopy;
  842. if condition.track_state_pass(exec_known) then
  843. begin
  844. track_state_pass:=true;
  845. {Force new resultdef pass.}
  846. condition.resultdef:=nil;
  847. do_typecheckpass(condition);
  848. end;
  849. if not is_constboolnode(condition) then
  850. aktstate.store_fact(condition,
  851. cordconstnode.create(byte(checknegate),booltype,true))
  852. else
  853. condition.destroy;
  854. end;
  855. {$endif}
  856. {*****************************************************************************
  857. TIFNODE
  858. *****************************************************************************}
  859. constructor tifnode.create(l,r,_t1 : tnode);
  860. begin
  861. inherited create(ifn,l,r,_t1,nil);
  862. end;
  863. function tifnode.internalsimplify(warn: boolean) : tnode;
  864. begin
  865. result:=nil;
  866. { optimize constant expressions }
  867. if (left.nodetype=ordconstn) then
  868. begin
  869. if tordconstnode(left).value.uvalue=1 then
  870. begin
  871. if assigned(right) then
  872. result:=right
  873. else
  874. result:=cnothingnode.create;
  875. right:=nil;
  876. if warn and assigned(t1) then
  877. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  878. end
  879. else
  880. begin
  881. if assigned(t1) then
  882. result:=t1
  883. else
  884. result:=cnothingnode.create;
  885. t1:=nil;
  886. if warn and assigned(right) then
  887. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  888. end;
  889. end;
  890. end;
  891. function tifnode.simplify : tnode;
  892. begin
  893. result:=internalsimplify(false);
  894. end;
  895. function tifnode.pass_typecheck:tnode;
  896. begin
  897. result:=nil;
  898. resultdef:=voidtype;
  899. typecheckpass(left);
  900. { tp procvar support }
  901. maybe_call_procvar(left,true);
  902. { if path }
  903. if assigned(right) then
  904. typecheckpass(right);
  905. { else path }
  906. if assigned(t1) then
  907. typecheckpass(t1);
  908. set_varstate(left,vs_read,[vsf_must_be_valid]);
  909. if codegenerror then
  910. exit;
  911. if not is_boolean(left.resultdef) then
  912. begin
  913. if left.resultdef.typ=variantdef then
  914. inserttypeconv(left,booltype)
  915. else
  916. Message1(type_e_boolean_expr_expected,left.resultdef.typename);
  917. end;
  918. result:=internalsimplify(true);
  919. end;
  920. function tifnode.pass_1 : tnode;
  921. begin
  922. result:=nil;
  923. expectloc:=LOC_VOID;
  924. firstpass(left);
  925. { if path }
  926. if assigned(right) then
  927. firstpass(right);
  928. { else path }
  929. if assigned(t1) then
  930. firstpass(t1);
  931. { leave if we've got an error in one of the paths }
  932. if codegenerror then
  933. exit;
  934. end;
  935. {*****************************************************************************
  936. TFORNODE
  937. *****************************************************************************}
  938. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  939. begin
  940. inherited create(forn,l,r,_t1,_t2);
  941. if back then
  942. include(loopflags,lnf_backward);
  943. include(loopflags,lnf_testatbegin);
  944. end;
  945. procedure Tfornode.loop_var_access(not_type:Tnotification_flag;
  946. symbol:Tsym);
  947. begin
  948. {If there is a read access, the value of the loop counter is important;
  949. at the end of the loop the loop variable should contain the value it
  950. had in the last iteration.}
  951. if not_type=vn_onwrite then
  952. begin
  953. writeln('Loopvar does not matter on exit');
  954. end
  955. else
  956. begin
  957. exclude(loopflags,lnf_dont_mind_loopvar_on_exit);
  958. writeln('Loopvar does matter on exit');
  959. end;
  960. Tabstractvarsym(symbol).unregister_notification(loopvar_notid);
  961. end;
  962. function tfornode.simplify : tnode;
  963. begin
  964. result:=nil;
  965. if (t1.nodetype=ordconstn) and
  966. (right.nodetype=ordconstn) and
  967. (
  968. (
  969. (lnf_backward in loopflags) and
  970. (tordconstnode(right).value<tordconstnode(t1).value)
  971. ) or
  972. (
  973. not(lnf_backward in loopflags) and
  974. (tordconstnode(right).value>tordconstnode(t1).value)
  975. )
  976. ) then
  977. result:=cnothingnode.create;
  978. end;
  979. function tfornode.pass_typecheck:tnode;
  980. var
  981. res : tnode;
  982. begin
  983. result:=nil;
  984. resultdef:=voidtype;
  985. { process the loopvar, from and to, varstates are already set }
  986. typecheckpass(left);
  987. typecheckpass(right);
  988. typecheckpass(t1);
  989. set_varstate(left,vs_written,[]);
  990. { loop unrolling }
  991. if cs_opt_loopunroll in current_settings.optimizerswitches then
  992. begin
  993. res:=unroll_loop(self);
  994. if assigned(res) then
  995. begin
  996. typecheckpass(res);
  997. result:=res;
  998. exit;
  999. end;
  1000. end;
  1001. { Can we spare the first comparision? }
  1002. if (t1.nodetype=ordconstn) and
  1003. (right.nodetype=ordconstn) and
  1004. (
  1005. (
  1006. (lnf_backward in loopflags) and
  1007. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1008. ) or
  1009. (
  1010. not(lnf_backward in loopflags) and
  1011. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1012. )
  1013. ) then
  1014. exclude(loopflags,lnf_testatbegin);
  1015. { Make sure that the loop var and the
  1016. from and to values are compatible types }
  1017. check_ranges(right.fileinfo,right,left.resultdef);
  1018. inserttypeconv(right,left.resultdef);
  1019. check_ranges(t1.fileinfo,t1,left.resultdef);
  1020. inserttypeconv(t1,left.resultdef);
  1021. if assigned(t2) then
  1022. typecheckpass(t2);
  1023. end;
  1024. function tfornode.pass_1 : tnode;
  1025. begin
  1026. result:=nil;
  1027. expectloc:=LOC_VOID;
  1028. firstpass(left);
  1029. firstpass(right);
  1030. firstpass(t1);
  1031. if assigned(t2) then
  1032. begin
  1033. firstpass(t2);
  1034. if codegenerror then
  1035. exit;
  1036. end;
  1037. end;
  1038. {*****************************************************************************
  1039. TEXITNODE
  1040. *****************************************************************************}
  1041. constructor texitnode.create(l:tnode);
  1042. begin
  1043. inherited create(exitn,l);
  1044. end;
  1045. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1046. begin
  1047. inherited ppuload(t,ppufile);
  1048. end;
  1049. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1050. begin
  1051. inherited ppuwrite(ppufile);
  1052. end;
  1053. function texitnode.pass_typecheck:tnode;
  1054. begin
  1055. result:=nil;
  1056. if assigned(left) then
  1057. begin
  1058. { add assignment to funcretsym }
  1059. inserttypeconv(left,current_procinfo.procdef.returndef);
  1060. left:=cassignmentnode.create(
  1061. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1062. left);
  1063. typecheckpass(left);
  1064. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1065. end;
  1066. resultdef:=voidtype;
  1067. end;
  1068. function texitnode.pass_1 : tnode;
  1069. begin
  1070. result:=nil;
  1071. expectloc:=LOC_VOID;
  1072. if assigned(left) then
  1073. begin
  1074. firstpass(left);
  1075. if codegenerror then
  1076. exit;
  1077. end;
  1078. end;
  1079. {*****************************************************************************
  1080. TBREAKNODE
  1081. *****************************************************************************}
  1082. constructor tbreaknode.create;
  1083. begin
  1084. inherited create(breakn);
  1085. end;
  1086. function tbreaknode.pass_typecheck:tnode;
  1087. begin
  1088. result:=nil;
  1089. resultdef:=voidtype;
  1090. end;
  1091. function tbreaknode.pass_1 : tnode;
  1092. begin
  1093. result:=nil;
  1094. expectloc:=LOC_VOID;
  1095. end;
  1096. {*****************************************************************************
  1097. TCONTINUENODE
  1098. *****************************************************************************}
  1099. constructor tcontinuenode.create;
  1100. begin
  1101. inherited create(continuen);
  1102. end;
  1103. function tcontinuenode.pass_typecheck:tnode;
  1104. begin
  1105. result:=nil;
  1106. resultdef:=voidtype;
  1107. end;
  1108. function tcontinuenode.pass_1 : tnode;
  1109. begin
  1110. result:=nil;
  1111. expectloc:=LOC_VOID;
  1112. end;
  1113. {*****************************************************************************
  1114. TGOTONODE
  1115. *****************************************************************************}
  1116. constructor tgotonode.create(p : tlabelsym);
  1117. begin
  1118. inherited create(goton);
  1119. exceptionblock:=current_exceptblock;
  1120. labelnode:=nil;
  1121. labelsym:=p;
  1122. end;
  1123. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1124. begin
  1125. inherited ppuload(t,ppufile);
  1126. labelnodeidx:=ppufile.getlongint;
  1127. exceptionblock:=ppufile.getbyte;
  1128. end;
  1129. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  1130. begin
  1131. inherited ppuwrite(ppufile);
  1132. labelnodeidx:=labelnode.ppuidx;
  1133. ppufile.putlongint(labelnodeidx);
  1134. ppufile.putbyte(exceptionblock);
  1135. end;
  1136. procedure tgotonode.buildderefimpl;
  1137. begin
  1138. inherited buildderefimpl;
  1139. end;
  1140. procedure tgotonode.derefimpl;
  1141. begin
  1142. inherited derefimpl;
  1143. end;
  1144. procedure tgotonode.resolveppuidx;
  1145. begin
  1146. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  1147. if labelnode.nodetype<>labeln then
  1148. internalerror(200809021);
  1149. end;
  1150. function tgotonode.pass_typecheck:tnode;
  1151. begin
  1152. result:=nil;
  1153. resultdef:=voidtype;
  1154. end;
  1155. function tgotonode.pass_1 : tnode;
  1156. begin
  1157. result:=nil;
  1158. expectloc:=LOC_VOID;
  1159. include(current_procinfo.flags,pi_has_goto);
  1160. { The labelnode can already be set when
  1161. this node was copied }
  1162. if not assigned(labelnode) then
  1163. begin
  1164. if assigned(labelsym.code) then
  1165. labelnode:=tlabelnode(labelsym.code)
  1166. else
  1167. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1168. end;
  1169. { check if we don't mess with exception blocks }
  1170. if assigned(labelnode) and
  1171. (exceptionblock<>labelnode.exceptionblock) then
  1172. CGMessage(cg_e_goto_inout_of_exception_block);
  1173. end;
  1174. function tgotonode.dogetcopy : tnode;
  1175. var
  1176. p : tgotonode;
  1177. begin
  1178. p:=tgotonode(inherited dogetcopy);
  1179. p.exceptionblock:=exceptionblock;
  1180. { generate labelnode if not done yet }
  1181. if not(assigned(labelnode)) then
  1182. begin
  1183. if assigned(labelsym) and assigned(labelsym.code) then
  1184. labelnode:=tlabelnode(labelsym.code)
  1185. end;
  1186. p.labelsym:=labelsym;
  1187. if assigned(labelnode) then
  1188. p.labelnode:=tlabelnode(labelnode.dogetcopy)
  1189. else
  1190. begin
  1191. { don't trigger IE when there was already an error, i.e. the
  1192. label is not defined. See tw11763 (PFV) }
  1193. if errorcount=0 then
  1194. internalerror(200610291);
  1195. end;
  1196. result:=p;
  1197. end;
  1198. function tgotonode.docompare(p: tnode): boolean;
  1199. begin
  1200. docompare := false;
  1201. end;
  1202. {*****************************************************************************
  1203. TLABELNODE
  1204. *****************************************************************************}
  1205. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  1206. begin
  1207. inherited create(labeln,l);
  1208. exceptionblock:=current_exceptblock;
  1209. labsym:=alabsym;
  1210. { Register labelnode in labelsym }
  1211. labsym.code:=self;
  1212. end;
  1213. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1214. begin
  1215. inherited ppuload(t,ppufile);
  1216. exceptionblock:=ppufile.getbyte;
  1217. end;
  1218. destructor tlabelnode.destroy;
  1219. begin
  1220. { Remove reference in labelsym, this is to prevent
  1221. goto's to this label }
  1222. if assigned(labsym) and (labsym.code=pointer(self)) then
  1223. labsym.code:=nil;
  1224. inherited destroy;
  1225. end;
  1226. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  1227. begin
  1228. inherited ppuwrite(ppufile);
  1229. ppufile.putbyte(exceptionblock);
  1230. end;
  1231. procedure tlabelnode.buildderefimpl;
  1232. begin
  1233. inherited buildderefimpl;
  1234. end;
  1235. procedure tlabelnode.derefimpl;
  1236. begin
  1237. inherited derefimpl;
  1238. end;
  1239. function tlabelnode.pass_typecheck:tnode;
  1240. begin
  1241. result:=nil;
  1242. { left could still be unassigned }
  1243. if assigned(left) then
  1244. typecheckpass(left);
  1245. resultdef:=voidtype;
  1246. end;
  1247. function tlabelnode.pass_1 : tnode;
  1248. begin
  1249. result:=nil;
  1250. expectloc:=LOC_VOID;
  1251. if assigned(left) then
  1252. firstpass(left);
  1253. end;
  1254. function tlabelnode.dogetcopy : tnode;
  1255. begin
  1256. if not(assigned(copiedto)) then
  1257. copiedto:=tlabelnode(inherited dogetcopy);
  1258. copiedto.exceptionblock:=exceptionblock;
  1259. result:=copiedto;
  1260. end;
  1261. function tlabelnode.docompare(p: tnode): boolean;
  1262. begin
  1263. docompare := false;
  1264. end;
  1265. {*****************************************************************************
  1266. TRAISENODE
  1267. *****************************************************************************}
  1268. constructor traisenode.create(l,taddr,tframe:tnode);
  1269. begin
  1270. inherited create(raisen,l,taddr,tframe);
  1271. end;
  1272. function traisenode.pass_typecheck:tnode;
  1273. begin
  1274. result:=nil;
  1275. resultdef:=voidtype;
  1276. if assigned(left) then
  1277. begin
  1278. { first para must be a _class_ }
  1279. typecheckpass(left);
  1280. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1281. if codegenerror then
  1282. exit;
  1283. if not(is_class(left.resultdef)) then
  1284. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  1285. { insert needed typeconvs for addr,frame }
  1286. if assigned(right) then
  1287. begin
  1288. { addr }
  1289. typecheckpass(right);
  1290. inserttypeconv(right,voidpointertype);
  1291. { frame }
  1292. if assigned(third) then
  1293. begin
  1294. typecheckpass(third);
  1295. inserttypeconv(third,voidpointertype);
  1296. end;
  1297. end;
  1298. end;
  1299. end;
  1300. function traisenode.pass_1 : tnode;
  1301. begin
  1302. result:=nil;
  1303. include(current_procinfo.flags,pi_do_call);
  1304. expectloc:=LOC_VOID;
  1305. if assigned(left) then
  1306. begin
  1307. { first para must be a _class_ }
  1308. firstpass(left);
  1309. { insert needed typeconvs for addr,frame }
  1310. if assigned(right) then
  1311. begin
  1312. { addr }
  1313. firstpass(right);
  1314. { frame }
  1315. if assigned(third) then
  1316. firstpass(third);
  1317. end;
  1318. end;
  1319. end;
  1320. {*****************************************************************************
  1321. TTRYEXCEPTNODE
  1322. *****************************************************************************}
  1323. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  1324. begin
  1325. inherited create(tryexceptn,l,r,_t1,nil);
  1326. end;
  1327. function ttryexceptnode.pass_typecheck:tnode;
  1328. begin
  1329. result:=nil;
  1330. typecheckpass(left);
  1331. { on statements }
  1332. if assigned(right) then
  1333. typecheckpass(right);
  1334. { else block }
  1335. if assigned(t1) then
  1336. typecheckpass(t1);
  1337. resultdef:=voidtype;
  1338. end;
  1339. function ttryexceptnode.pass_1 : tnode;
  1340. begin
  1341. result:=nil;
  1342. include(current_procinfo.flags,pi_do_call);
  1343. expectloc:=LOC_VOID;
  1344. firstpass(left);
  1345. { on statements }
  1346. if assigned(right) then
  1347. firstpass(right);
  1348. { else block }
  1349. if assigned(t1) then
  1350. firstpass(t1);
  1351. end;
  1352. {*****************************************************************************
  1353. TTRYFINALLYNODE
  1354. *****************************************************************************}
  1355. constructor ttryfinallynode.create(l,r:tnode);
  1356. begin
  1357. inherited create(tryfinallyn,l,r,nil,nil);
  1358. implicitframe:=false;
  1359. end;
  1360. constructor ttryfinallynode.create_implicit(l,r,_t1:tnode);
  1361. begin
  1362. inherited create(tryfinallyn,l,r,_t1,nil);
  1363. implicitframe:=true;
  1364. end;
  1365. function ttryfinallynode.pass_typecheck:tnode;
  1366. begin
  1367. result:=nil;
  1368. include(current_procinfo.flags,pi_do_call);
  1369. resultdef:=voidtype;
  1370. typecheckpass(left);
  1371. // "try block" is "used"? (JM)
  1372. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  1373. typecheckpass(right);
  1374. // "except block" is "used"? (JM)
  1375. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  1376. { special finally block only executed when there was an exception }
  1377. if assigned(t1) then
  1378. begin
  1379. typecheckpass(t1);
  1380. // "finally block" is "used"? (JM)
  1381. set_varstate(t1,vs_readwritten,[vsf_must_be_valid]);
  1382. end;
  1383. end;
  1384. function ttryfinallynode.pass_1 : tnode;
  1385. begin
  1386. result:=nil;
  1387. expectloc:=LOC_VOID;
  1388. firstpass(left);
  1389. firstpass(right);
  1390. if assigned(t1) then
  1391. firstpass(t1);
  1392. end;
  1393. function ttryfinallynode.simplify: tnode;
  1394. begin
  1395. result:=nil;
  1396. { if the try contains no code, we can kill
  1397. the try and except and return only the
  1398. finally part }
  1399. if has_no_code(left) then
  1400. begin
  1401. result:=right;
  1402. right:=nil;
  1403. end;
  1404. end;
  1405. {*****************************************************************************
  1406. TONNODE
  1407. *****************************************************************************}
  1408. constructor tonnode.create(l,r:tnode);
  1409. begin
  1410. inherited create(onn,l,r);
  1411. excepTSymtable:=nil;
  1412. excepttype:=nil;
  1413. end;
  1414. destructor tonnode.destroy;
  1415. begin
  1416. { copied nodes don't need to release the symtable }
  1417. if assigned(excepTSymtable) then
  1418. excepTSymtable.free;
  1419. inherited destroy;
  1420. end;
  1421. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1422. begin
  1423. inherited ppuload(t,ppufile);
  1424. excepTSymtable:=nil;
  1425. excepttype:=nil;
  1426. end;
  1427. function tonnode.dogetcopy : tnode;
  1428. var
  1429. n : tonnode;
  1430. begin
  1431. n:=tonnode(inherited dogetcopy);
  1432. if assigned(exceptsymtable) then
  1433. n.exceptsymtable:=exceptsymtable.getcopy
  1434. else
  1435. n.exceptsymtable:=nil;
  1436. n.excepttype:=excepttype;
  1437. result:=n;
  1438. end;
  1439. function tonnode.pass_typecheck:tnode;
  1440. begin
  1441. result:=nil;
  1442. resultdef:=voidtype;
  1443. if not(is_class(excepttype)) then
  1444. CGMessage1(type_e_class_type_expected,excepttype.typename);
  1445. if assigned(left) then
  1446. typecheckpass(left);
  1447. if assigned(right) then
  1448. typecheckpass(right);
  1449. end;
  1450. function tonnode.pass_1 : tnode;
  1451. begin
  1452. result:=nil;
  1453. include(current_procinfo.flags,pi_do_call);
  1454. expectloc:=LOC_VOID;
  1455. if assigned(left) then
  1456. firstpass(left);
  1457. if assigned(right) then
  1458. firstpass(right);
  1459. end;
  1460. function tonnode.docompare(p: tnode): boolean;
  1461. begin
  1462. docompare := false;
  1463. end;
  1464. begin
  1465. cwhilerepeatnode:=twhilerepeatnode;
  1466. cifnode:=tifnode;
  1467. cfornode:=tfornode;
  1468. cexitnode:=texitnode;
  1469. cgotonode:=tgotonode;
  1470. clabelnode:=tlabelnode;
  1471. craisenode:=traisenode;
  1472. ctryexceptnode:=ttryexceptnode;
  1473. ctryfinallynode:=ttryfinallynode;
  1474. connode:=tonnode;
  1475. end.