nflw.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  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. implementation
  194. uses
  195. globtype,systems,constexp,
  196. cutils,verbose,globals,
  197. symconst,paramgr,defcmp,defutil,htypechk,pass_1,
  198. ncal,nadd,ncon,nmem,nld,ncnv,nbas,cgobj,nutils,
  199. {$ifdef prefetchnext}
  200. ninl,
  201. {$endif prefetchnext}
  202. {$ifdef state_tracking}
  203. nstate,
  204. {$endif}
  205. cgbase,procinfo
  206. ;
  207. {****************************************************************************
  208. TLOOPNODE
  209. *****************************************************************************}
  210. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  211. begin
  212. inherited create(tt,l,r);
  213. t1:=_t1;
  214. t2:=_t2;
  215. fileinfo:=l.fileinfo;
  216. end;
  217. destructor tloopnode.destroy;
  218. begin
  219. t1.free;
  220. t2.free;
  221. inherited destroy;
  222. end;
  223. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  224. begin
  225. inherited ppuload(t,ppufile);
  226. t1:=ppuloadnode(ppufile);
  227. t2:=ppuloadnode(ppufile);
  228. end;
  229. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  230. begin
  231. inherited ppuwrite(ppufile);
  232. ppuwritenode(ppufile,t1);
  233. ppuwritenode(ppufile,t2);
  234. end;
  235. procedure tloopnode.buildderefimpl;
  236. begin
  237. inherited buildderefimpl;
  238. if assigned(t1) then
  239. t1.buildderefimpl;
  240. if assigned(t2) then
  241. t2.buildderefimpl;
  242. end;
  243. procedure tloopnode.derefimpl;
  244. begin
  245. inherited derefimpl;
  246. if assigned(t1) then
  247. t1.derefimpl;
  248. if assigned(t2) then
  249. t2.derefimpl;
  250. end;
  251. function tloopnode.dogetcopy : tnode;
  252. var
  253. p : tloopnode;
  254. begin
  255. p:=tloopnode(inherited dogetcopy);
  256. if assigned(t1) then
  257. p.t1:=t1.dogetcopy
  258. else
  259. p.t1:=nil;
  260. if assigned(t2) then
  261. p.t2:=t2.dogetcopy
  262. else
  263. p.t2:=nil;
  264. p.loopflags:=loopflags;
  265. dogetcopy:=p;
  266. end;
  267. procedure tloopnode.insertintolist(l : tnodelist);
  268. begin
  269. end;
  270. procedure tloopnode.printnodetree(var t:text);
  271. begin
  272. write(t,printnodeindention,'(');
  273. printnodeindent;
  274. printnodeinfo(t);
  275. writeln(t);
  276. printnode(t,left);
  277. printnode(t,right);
  278. printnode(t,t1);
  279. printnode(t,t2);
  280. printnodeunindent;
  281. writeln(t,printnodeindention,')');
  282. end;
  283. function tloopnode.docompare(p: tnode): boolean;
  284. begin
  285. docompare :=
  286. inherited docompare(p) and
  287. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  288. t1.isequal(tloopnode(p).t1) and
  289. t2.isequal(tloopnode(p).t2);
  290. end;
  291. {****************************************************************************
  292. TWHILEREPEATNODE
  293. *****************************************************************************}
  294. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  295. begin
  296. inherited create(whilerepeatn,l,r,nil,nil);
  297. if tab then
  298. include(loopflags, lnf_testatbegin);
  299. if cn then
  300. include(loopflags,lnf_checknegate);
  301. end;
  302. function twhilerepeatnode.pass_typecheck:tnode;
  303. var
  304. t:Tunarynode;
  305. begin
  306. result:=nil;
  307. resultdef:=voidtype;
  308. typecheckpass(left);
  309. { tp procvar support }
  310. maybe_call_procvar(left,true);
  311. {A not node can be removed.}
  312. if left.nodetype=notn then
  313. begin
  314. t:=Tunarynode(left);
  315. left:=Tunarynode(left).left;
  316. t.left:=nil;
  317. t.destroy;
  318. {Symdif operator, in case you are wondering:}
  319. loopflags:=loopflags >< [lnf_checknegate];
  320. end;
  321. { loop instruction }
  322. if assigned(right) then
  323. typecheckpass(right);
  324. set_varstate(left,vs_read,[vsf_must_be_valid]);
  325. if codegenerror then
  326. exit;
  327. if not is_boolean(left.resultdef) then
  328. begin
  329. if left.resultdef.typ=variantdef then
  330. inserttypeconv(left,booltype)
  331. else
  332. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  333. end;
  334. { Give warnings for code that will never be executed for
  335. while false do }
  336. if (lnf_testatbegin in loopflags) and
  337. (left.nodetype=ordconstn) and
  338. (tordconstnode(left).value.uvalue=0) and
  339. assigned(right) then
  340. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  341. end;
  342. {$ifdef prefetchnext}
  343. type
  344. passignmentquery = ^tassignmentquery;
  345. tassignmentquery = record
  346. towhat: tnode;
  347. source: tassignmentnode;
  348. statementcount: cardinal;
  349. end;
  350. function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
  351. var
  352. query: passignmentquery absolute arg;
  353. temp, prederef: tnode;
  354. begin
  355. result := fen_norecurse_false;
  356. if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
  357. inc(query^.statementcount);
  358. { make sure there's something else in the loop besides going to the }
  359. { next item }
  360. if (query^.statementcount > 1) and
  361. (n.nodetype = assignn) then
  362. begin
  363. { skip type conversions of assignment target }
  364. temp := tassignmentnode(n).left;
  365. while (temp.nodetype = typeconvn) do
  366. temp := ttypeconvnode(temp).left;
  367. { assignment to x of the while assigned(x) check? }
  368. if not(temp.isequal(query^.towhat)) then
  369. exit;
  370. { right hand side of assignment dereferenced field of }
  371. { x? (no derefn in case of class) }
  372. temp := tassignmentnode(n).right;
  373. while (temp.nodetype = typeconvn) do
  374. temp := ttypeconvnode(temp).left;
  375. if (temp.nodetype <> subscriptn) then
  376. exit;
  377. prederef := tsubscriptnode(temp).left;
  378. temp := prederef;
  379. while (temp.nodetype = typeconvn) do
  380. temp := ttypeconvnode(temp).left;
  381. { see tests/test/prefetch1.pp }
  382. if (temp.nodetype = derefn) then
  383. temp := tderefnode(temp).left
  384. else
  385. temp := prederef;
  386. if temp.isequal(query^.towhat) then
  387. begin
  388. query^.source := tassignmentnode(n);
  389. result := fen_norecurse_true;
  390. end
  391. end
  392. { don't check nodes which can't contain an assignment or whose }
  393. { final assignment can vary a lot }
  394. else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
  395. result := fen_false;
  396. end;
  397. function findassignment(where: tnode; towhat: tnode): tassignmentnode;
  398. var
  399. query: tassignmentquery;
  400. begin
  401. query.towhat := towhat;
  402. query.source := nil;
  403. query.statementcount := 0;
  404. if foreachnodestatic(where,@checkassignment,@query) then
  405. result := query.source
  406. else
  407. result := nil;
  408. end;
  409. {$endif prefetchnext}
  410. function twhilerepeatnode.pass_1 : tnode;
  411. {$ifdef prefetchnext}
  412. var
  413. runnernode, prefetchcode: tnode;
  414. assignmentnode: tassignmentnode;
  415. prefetchstatements: tstatementnode;
  416. {$endif prefetchnext}
  417. begin
  418. result:=nil;
  419. expectloc:=LOC_VOID;
  420. firstpass(left);
  421. if codegenerror then
  422. exit;
  423. { loop instruction }
  424. if assigned(right) then
  425. begin
  426. firstpass(right);
  427. if codegenerror then
  428. exit;
  429. end;
  430. {$ifdef prefetchnext}
  431. { do at the end so all complex typeconversions are already }
  432. { converted to calln's }
  433. if (cs_opt_level1 in current_settings.optimizerswitches) and
  434. (lnf_testatbegin in loopflags) then
  435. begin
  436. { get first component of the while check }
  437. runnernode := left;
  438. while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
  439. runnernode := tunarynode(runnernode).left;
  440. { is it an assigned(x) check? }
  441. if ((runnernode.nodetype = inlinen) and
  442. (tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
  443. ((runnernode.nodetype = unequaln) and
  444. (taddnode(runnernode).right.nodetype = niln)) then
  445. begin
  446. runnernode := tunarynode(runnernode).left;
  447. { in case of in_assigned_x, there's a callparan in between }
  448. if (runnernode.nodetype = callparan) then
  449. runnernode := tcallparanode(runnernode).left;
  450. while (runnernode.nodetype = typeconvn) do
  451. runnernode := ttypeconvnode(runnernode).left;
  452. { is there an "x := x(^).somefield"? }
  453. assignmentnode := findassignment(right,runnernode);
  454. if assigned(assignmentnode) then
  455. begin
  456. prefetchcode := internalstatements(prefetchstatements);
  457. addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
  458. cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
  459. addstatement(prefetchstatements,right);
  460. right := prefetchcode;
  461. typecheckpass(right);
  462. end;
  463. end;
  464. end;
  465. {$endif prefetchnext}
  466. end;
  467. {$ifdef state_tracking}
  468. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  469. var condition:Tnode;
  470. code:Tnode;
  471. done:boolean;
  472. value:boolean;
  473. change:boolean;
  474. firsttest:boolean;
  475. factval:Tnode;
  476. begin
  477. track_state_pass:=false;
  478. done:=false;
  479. firsttest:=true;
  480. {For repeat until statements, first do a pass through the code.}
  481. if not(lnf_testatbegin in flags) then
  482. begin
  483. code:=right.getcopy;
  484. if code.track_state_pass(exec_known) then
  485. track_state_pass:=true;
  486. code.destroy;
  487. end;
  488. repeat
  489. condition:=left.getcopy;
  490. code:=right.getcopy;
  491. change:=condition.track_state_pass(exec_known);
  492. factval:=aktstate.find_fact(left);
  493. if factval<>nil then
  494. begin
  495. condition.destroy;
  496. condition:=factval.getcopy;
  497. change:=true;
  498. end;
  499. if change then
  500. begin
  501. track_state_pass:=true;
  502. {Force new resultdef pass.}
  503. condition.resultdef:=nil;
  504. do_typecheckpass(condition);
  505. end;
  506. if is_constboolnode(condition) then
  507. begin
  508. {Try to turn a while loop into a repeat loop.}
  509. if firsttest then
  510. exclude(flags,testatbegin);
  511. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  512. if value then
  513. begin
  514. if code.track_state_pass(exec_known) then
  515. track_state_pass:=true;
  516. end
  517. else
  518. done:=true;
  519. end
  520. else
  521. begin
  522. {Remove any modified variables from the state.}
  523. code.track_state_pass(false);
  524. done:=true;
  525. end;
  526. code.destroy;
  527. condition.destroy;
  528. firsttest:=false;
  529. until done;
  530. {The loop condition is also known, for example:
  531. while i<10 do
  532. begin
  533. ...
  534. end;
  535. When the loop is done, we do know that i<10 = false.
  536. }
  537. condition:=left.getcopy;
  538. if condition.track_state_pass(exec_known) then
  539. begin
  540. track_state_pass:=true;
  541. {Force new resultdef pass.}
  542. condition.resultdef:=nil;
  543. do_typecheckpass(condition);
  544. end;
  545. if not is_constboolnode(condition) then
  546. aktstate.store_fact(condition,
  547. cordconstnode.create(byte(checknegate),booltype,true))
  548. else
  549. condition.destroy;
  550. end;
  551. {$endif}
  552. {*****************************************************************************
  553. TIFNODE
  554. *****************************************************************************}
  555. constructor tifnode.create(l,r,_t1 : tnode);
  556. begin
  557. inherited create(ifn,l,r,_t1,nil);
  558. end;
  559. function tifnode.internalsimplify(warn: boolean) : tnode;
  560. begin
  561. result:=nil;
  562. { optimize constant expressions }
  563. if (left.nodetype=ordconstn) then
  564. begin
  565. if tordconstnode(left).value.uvalue=1 then
  566. begin
  567. if assigned(right) then
  568. result:=right
  569. else
  570. result:=cnothingnode.create;
  571. right:=nil;
  572. if warn and assigned(t1) then
  573. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  574. end
  575. else
  576. begin
  577. if assigned(t1) then
  578. result:=t1
  579. else
  580. result:=cnothingnode.create;
  581. t1:=nil;
  582. if warn and assigned(right) then
  583. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  584. end;
  585. end;
  586. end;
  587. function tifnode.simplify : tnode;
  588. begin
  589. result:=internalsimplify(false);
  590. end;
  591. function tifnode.pass_typecheck:tnode;
  592. begin
  593. result:=nil;
  594. resultdef:=voidtype;
  595. typecheckpass(left);
  596. { tp procvar support }
  597. maybe_call_procvar(left,true);
  598. { if path }
  599. if assigned(right) then
  600. typecheckpass(right);
  601. { else path }
  602. if assigned(t1) then
  603. typecheckpass(t1);
  604. set_varstate(left,vs_read,[vsf_must_be_valid]);
  605. if codegenerror then
  606. exit;
  607. if not is_boolean(left.resultdef) then
  608. begin
  609. if left.resultdef.typ=variantdef then
  610. inserttypeconv(left,booltype)
  611. else
  612. Message1(type_e_boolean_expr_expected,left.resultdef.typename);
  613. end;
  614. result:=internalsimplify(true);
  615. end;
  616. function tifnode.pass_1 : tnode;
  617. begin
  618. result:=nil;
  619. expectloc:=LOC_VOID;
  620. firstpass(left);
  621. { if path }
  622. if assigned(right) then
  623. firstpass(right);
  624. { else path }
  625. if assigned(t1) then
  626. firstpass(t1);
  627. { leave if we've got an error in one of the paths }
  628. if codegenerror then
  629. exit;
  630. end;
  631. {*****************************************************************************
  632. TFORNODE
  633. *****************************************************************************}
  634. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  635. begin
  636. inherited create(forn,l,r,_t1,_t2);
  637. if back then
  638. include(loopflags,lnf_backward);
  639. include(loopflags,lnf_testatbegin);
  640. end;
  641. procedure Tfornode.loop_var_access(not_type:Tnotification_flag;
  642. symbol:Tsym);
  643. begin
  644. {If there is a read access, the value of the loop counter is important;
  645. at the end of the loop the loop variable should contain the value it
  646. had in the last iteration.}
  647. if not_type=vn_onwrite then
  648. begin
  649. writeln('Loopvar does not matter on exit');
  650. end
  651. else
  652. begin
  653. exclude(loopflags,lnf_dont_mind_loopvar_on_exit);
  654. writeln('Loopvar does matter on exit');
  655. end;
  656. Tabstractvarsym(symbol).unregister_notification(loopvar_notid);
  657. end;
  658. function tfornode.simplify : tnode;
  659. begin
  660. result:=nil;
  661. if (t1.nodetype=ordconstn) and
  662. (right.nodetype=ordconstn) and
  663. (
  664. (
  665. (lnf_backward in loopflags) and
  666. (tordconstnode(right).value<tordconstnode(t1).value)
  667. ) or
  668. (
  669. not(lnf_backward in loopflags) and
  670. (tordconstnode(right).value>tordconstnode(t1).value)
  671. )
  672. ) then
  673. result:=cnothingnode.create;
  674. end;
  675. function tfornode.pass_typecheck:tnode;
  676. var
  677. res : tnode;
  678. begin
  679. result:=nil;
  680. resultdef:=voidtype;
  681. { process the loopvar, from and to, varstates are already set }
  682. typecheckpass(left);
  683. typecheckpass(right);
  684. typecheckpass(t1);
  685. { loop unrolling }
  686. if cs_opt_loopunroll in current_settings.optimizerswitches then
  687. begin
  688. res:=unroll_loop(self);
  689. if assigned(res) then
  690. begin
  691. typecheckpass(res);
  692. result:=res;
  693. exit;
  694. end;
  695. end;
  696. { Can we spare the first comparision? }
  697. if (t1.nodetype=ordconstn) and
  698. (right.nodetype=ordconstn) and
  699. (
  700. (
  701. (lnf_backward in loopflags) and
  702. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  703. ) or
  704. (
  705. not(lnf_backward in loopflags) and
  706. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  707. )
  708. ) then
  709. exclude(loopflags,lnf_testatbegin);
  710. { Make sure that the loop var and the
  711. from and to values are compatible types }
  712. check_ranges(right.fileinfo,right,left.resultdef);
  713. inserttypeconv(right,left.resultdef);
  714. check_ranges(t1.fileinfo,t1,left.resultdef);
  715. inserttypeconv(t1,left.resultdef);
  716. if assigned(t2) then
  717. typecheckpass(t2);
  718. end;
  719. function tfornode.pass_1 : tnode;
  720. begin
  721. result:=nil;
  722. expectloc:=LOC_VOID;
  723. firstpass(left);
  724. firstpass(right);
  725. firstpass(t1);
  726. if assigned(t2) then
  727. begin
  728. firstpass(t2);
  729. if codegenerror then
  730. exit;
  731. end;
  732. end;
  733. {*****************************************************************************
  734. TEXITNODE
  735. *****************************************************************************}
  736. constructor texitnode.create(l:tnode);
  737. begin
  738. inherited create(exitn,l);
  739. end;
  740. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  741. begin
  742. inherited ppuload(t,ppufile);
  743. end;
  744. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  745. begin
  746. inherited ppuwrite(ppufile);
  747. end;
  748. function texitnode.pass_typecheck:tnode;
  749. begin
  750. result:=nil;
  751. if assigned(left) then
  752. begin
  753. { add assignment to funcretsym }
  754. inserttypeconv(left,current_procinfo.procdef.returndef);
  755. left:=cassignmentnode.create(
  756. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  757. left);
  758. typecheckpass(left);
  759. set_varstate(left,vs_read,[vsf_must_be_valid]);
  760. end;
  761. resultdef:=voidtype;
  762. end;
  763. function texitnode.pass_1 : tnode;
  764. begin
  765. result:=nil;
  766. expectloc:=LOC_VOID;
  767. if assigned(left) then
  768. begin
  769. firstpass(left);
  770. if codegenerror then
  771. exit;
  772. end;
  773. end;
  774. {*****************************************************************************
  775. TBREAKNODE
  776. *****************************************************************************}
  777. constructor tbreaknode.create;
  778. begin
  779. inherited create(breakn);
  780. end;
  781. function tbreaknode.pass_typecheck:tnode;
  782. begin
  783. result:=nil;
  784. resultdef:=voidtype;
  785. end;
  786. function tbreaknode.pass_1 : tnode;
  787. begin
  788. result:=nil;
  789. expectloc:=LOC_VOID;
  790. end;
  791. {*****************************************************************************
  792. TCONTINUENODE
  793. *****************************************************************************}
  794. constructor tcontinuenode.create;
  795. begin
  796. inherited create(continuen);
  797. end;
  798. function tcontinuenode.pass_typecheck:tnode;
  799. begin
  800. result:=nil;
  801. resultdef:=voidtype;
  802. end;
  803. function tcontinuenode.pass_1 : tnode;
  804. begin
  805. result:=nil;
  806. expectloc:=LOC_VOID;
  807. end;
  808. {*****************************************************************************
  809. TGOTONODE
  810. *****************************************************************************}
  811. constructor tgotonode.create(p : tlabelsym);
  812. begin
  813. inherited create(goton);
  814. exceptionblock:=current_exceptblock;
  815. labelnode:=nil;
  816. labelsym:=p;
  817. end;
  818. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  819. begin
  820. inherited ppuload(t,ppufile);
  821. labelnodeidx:=ppufile.getlongint;
  822. exceptionblock:=ppufile.getbyte;
  823. end;
  824. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  825. begin
  826. inherited ppuwrite(ppufile);
  827. labelnodeidx:=labelnode.ppuidx;
  828. ppufile.putlongint(labelnodeidx);
  829. ppufile.putbyte(exceptionblock);
  830. end;
  831. procedure tgotonode.buildderefimpl;
  832. begin
  833. inherited buildderefimpl;
  834. end;
  835. procedure tgotonode.derefimpl;
  836. begin
  837. inherited derefimpl;
  838. end;
  839. procedure tgotonode.resolveppuidx;
  840. begin
  841. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  842. if labelnode.nodetype<>labeln then
  843. internalerror(200809021);
  844. end;
  845. function tgotonode.pass_typecheck:tnode;
  846. begin
  847. result:=nil;
  848. resultdef:=voidtype;
  849. end;
  850. function tgotonode.pass_1 : tnode;
  851. begin
  852. result:=nil;
  853. expectloc:=LOC_VOID;
  854. include(current_procinfo.flags,pi_has_goto);
  855. { The labelnode can already be set when
  856. this node was copied }
  857. if not assigned(labelnode) then
  858. begin
  859. if assigned(labelsym.code) then
  860. labelnode:=tlabelnode(labelsym.code)
  861. else
  862. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  863. end;
  864. { check if we don't mess with exception blocks }
  865. if assigned(labelnode) and
  866. (exceptionblock<>labelnode.exceptionblock) then
  867. CGMessage(cg_e_goto_inout_of_exception_block);
  868. end;
  869. function tgotonode.dogetcopy : tnode;
  870. var
  871. p : tgotonode;
  872. begin
  873. p:=tgotonode(inherited dogetcopy);
  874. p.exceptionblock:=exceptionblock;
  875. { generate labelnode if not done yet }
  876. if not(assigned(labelnode)) then
  877. begin
  878. if assigned(labelsym) and assigned(labelsym.code) then
  879. labelnode:=tlabelnode(labelsym.code)
  880. end;
  881. p.labelsym:=labelsym;
  882. if assigned(labelnode) then
  883. p.labelnode:=tlabelnode(labelnode.dogetcopy)
  884. else
  885. begin
  886. { don't trigger IE when there was already an error, i.e. the
  887. label is not defined. See tw11763 (PFV) }
  888. if errorcount=0 then
  889. internalerror(200610291);
  890. end;
  891. result:=p;
  892. end;
  893. function tgotonode.docompare(p: tnode): boolean;
  894. begin
  895. docompare := false;
  896. end;
  897. {*****************************************************************************
  898. TLABELNODE
  899. *****************************************************************************}
  900. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  901. begin
  902. inherited create(labeln,l);
  903. exceptionblock:=current_exceptblock;
  904. labsym:=alabsym;
  905. { Register labelnode in labelsym }
  906. labsym.code:=self;
  907. end;
  908. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  909. begin
  910. inherited ppuload(t,ppufile);
  911. exceptionblock:=ppufile.getbyte;
  912. end;
  913. destructor tlabelnode.destroy;
  914. begin
  915. { Remove reference in labelsym, this is to prevent
  916. goto's to this label }
  917. if assigned(labsym) and (labsym.code=pointer(self)) then
  918. labsym.code:=nil;
  919. inherited destroy;
  920. end;
  921. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  922. begin
  923. inherited ppuwrite(ppufile);
  924. ppufile.putbyte(exceptionblock);
  925. end;
  926. procedure tlabelnode.buildderefimpl;
  927. begin
  928. inherited buildderefimpl;
  929. end;
  930. procedure tlabelnode.derefimpl;
  931. begin
  932. inherited derefimpl;
  933. end;
  934. function tlabelnode.pass_typecheck:tnode;
  935. begin
  936. result:=nil;
  937. { left could still be unassigned }
  938. if assigned(left) then
  939. typecheckpass(left);
  940. resultdef:=voidtype;
  941. end;
  942. function tlabelnode.pass_1 : tnode;
  943. begin
  944. result:=nil;
  945. expectloc:=LOC_VOID;
  946. if assigned(left) then
  947. firstpass(left);
  948. end;
  949. function tlabelnode.dogetcopy : tnode;
  950. begin
  951. if not(assigned(copiedto)) then
  952. copiedto:=tlabelnode(inherited dogetcopy);
  953. copiedto.exceptionblock:=exceptionblock;
  954. result:=copiedto;
  955. end;
  956. function tlabelnode.docompare(p: tnode): boolean;
  957. begin
  958. docompare := false;
  959. end;
  960. {*****************************************************************************
  961. TRAISENODE
  962. *****************************************************************************}
  963. constructor traisenode.create(l,taddr,tframe:tnode);
  964. begin
  965. inherited create(raisen,l,taddr,tframe);
  966. end;
  967. function traisenode.pass_typecheck:tnode;
  968. begin
  969. result:=nil;
  970. resultdef:=voidtype;
  971. if assigned(left) then
  972. begin
  973. { first para must be a _class_ }
  974. typecheckpass(left);
  975. set_varstate(left,vs_read,[vsf_must_be_valid]);
  976. if codegenerror then
  977. exit;
  978. if not(is_class(left.resultdef)) then
  979. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  980. { insert needed typeconvs for addr,frame }
  981. if assigned(right) then
  982. begin
  983. { addr }
  984. typecheckpass(right);
  985. inserttypeconv(right,voidpointertype);
  986. { frame }
  987. if assigned(third) then
  988. begin
  989. typecheckpass(third);
  990. inserttypeconv(third,voidpointertype);
  991. end;
  992. end;
  993. end;
  994. end;
  995. function traisenode.pass_1 : tnode;
  996. begin
  997. result:=nil;
  998. include(current_procinfo.flags,pi_do_call);
  999. expectloc:=LOC_VOID;
  1000. if assigned(left) then
  1001. begin
  1002. { first para must be a _class_ }
  1003. firstpass(left);
  1004. { insert needed typeconvs for addr,frame }
  1005. if assigned(right) then
  1006. begin
  1007. { addr }
  1008. firstpass(right);
  1009. { frame }
  1010. if assigned(third) then
  1011. firstpass(third);
  1012. end;
  1013. end;
  1014. end;
  1015. {*****************************************************************************
  1016. TTRYEXCEPTNODE
  1017. *****************************************************************************}
  1018. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  1019. begin
  1020. inherited create(tryexceptn,l,r,_t1,nil);
  1021. end;
  1022. function ttryexceptnode.pass_typecheck:tnode;
  1023. begin
  1024. result:=nil;
  1025. typecheckpass(left);
  1026. { on statements }
  1027. if assigned(right) then
  1028. typecheckpass(right);
  1029. { else block }
  1030. if assigned(t1) then
  1031. typecheckpass(t1);
  1032. resultdef:=voidtype;
  1033. end;
  1034. function ttryexceptnode.pass_1 : tnode;
  1035. begin
  1036. result:=nil;
  1037. include(current_procinfo.flags,pi_do_call);
  1038. expectloc:=LOC_VOID;
  1039. firstpass(left);
  1040. { on statements }
  1041. if assigned(right) then
  1042. firstpass(right);
  1043. { else block }
  1044. if assigned(t1) then
  1045. firstpass(t1);
  1046. end;
  1047. {*****************************************************************************
  1048. TTRYFINALLYNODE
  1049. *****************************************************************************}
  1050. constructor ttryfinallynode.create(l,r:tnode);
  1051. begin
  1052. inherited create(tryfinallyn,l,r,nil,nil);
  1053. implicitframe:=false;
  1054. end;
  1055. constructor ttryfinallynode.create_implicit(l,r,_t1:tnode);
  1056. begin
  1057. inherited create(tryfinallyn,l,r,_t1,nil);
  1058. implicitframe:=true;
  1059. end;
  1060. function ttryfinallynode.pass_typecheck:tnode;
  1061. begin
  1062. result:=nil;
  1063. include(current_procinfo.flags,pi_do_call);
  1064. resultdef:=voidtype;
  1065. typecheckpass(left);
  1066. // "try block" is "used"? (JM)
  1067. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  1068. typecheckpass(right);
  1069. // "except block" is "used"? (JM)
  1070. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  1071. { special finally block only executed when there was an exception }
  1072. if assigned(t1) then
  1073. begin
  1074. typecheckpass(t1);
  1075. // "finally block" is "used"? (JM)
  1076. set_varstate(t1,vs_readwritten,[vsf_must_be_valid]);
  1077. end;
  1078. end;
  1079. function ttryfinallynode.pass_1 : tnode;
  1080. begin
  1081. result:=nil;
  1082. expectloc:=LOC_VOID;
  1083. firstpass(left);
  1084. firstpass(right);
  1085. if assigned(t1) then
  1086. firstpass(t1);
  1087. end;
  1088. function ttryfinallynode.simplify: tnode;
  1089. begin
  1090. result:=nil;
  1091. { if the try contains no code, we can kill
  1092. the try and except and return only the
  1093. finally part }
  1094. if has_no_code(left) then
  1095. begin
  1096. result:=right;
  1097. right:=nil;
  1098. end;
  1099. end;
  1100. {*****************************************************************************
  1101. TONNODE
  1102. *****************************************************************************}
  1103. constructor tonnode.create(l,r:tnode);
  1104. begin
  1105. inherited create(onn,l,r);
  1106. excepTSymtable:=nil;
  1107. excepttype:=nil;
  1108. end;
  1109. destructor tonnode.destroy;
  1110. begin
  1111. { copied nodes don't need to release the symtable }
  1112. if assigned(excepTSymtable) then
  1113. excepTSymtable.free;
  1114. inherited destroy;
  1115. end;
  1116. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1117. begin
  1118. inherited ppuload(t,ppufile);
  1119. excepTSymtable:=nil;
  1120. excepttype:=nil;
  1121. end;
  1122. function tonnode.dogetcopy : tnode;
  1123. var
  1124. n : tonnode;
  1125. begin
  1126. n:=tonnode(inherited dogetcopy);
  1127. if assigned(exceptsymtable) then
  1128. n.exceptsymtable:=exceptsymtable.getcopy
  1129. else
  1130. n.exceptsymtable:=nil;
  1131. n.excepttype:=excepttype;
  1132. result:=n;
  1133. end;
  1134. function tonnode.pass_typecheck:tnode;
  1135. begin
  1136. result:=nil;
  1137. resultdef:=voidtype;
  1138. if not(is_class(excepttype)) then
  1139. CGMessage1(type_e_class_type_expected,excepttype.typename);
  1140. if assigned(left) then
  1141. typecheckpass(left);
  1142. if assigned(right) then
  1143. typecheckpass(right);
  1144. end;
  1145. function tonnode.pass_1 : tnode;
  1146. begin
  1147. result:=nil;
  1148. include(current_procinfo.flags,pi_do_call);
  1149. expectloc:=LOC_VOID;
  1150. if assigned(left) then
  1151. firstpass(left);
  1152. if assigned(right) then
  1153. firstpass(right);
  1154. end;
  1155. function tonnode.docompare(p: tnode): boolean;
  1156. begin
  1157. docompare := false;
  1158. end;
  1159. begin
  1160. cwhilerepeatnode:=twhilerepeatnode;
  1161. cifnode:=tifnode;
  1162. cfornode:=tfornode;
  1163. cexitnode:=texitnode;
  1164. cgotonode:=tgotonode;
  1165. clabelnode:=tlabelnode;
  1166. craisenode:=traisenode;
  1167. ctryexceptnode:=ttryexceptnode;
  1168. ctryfinallynode:=ttryfinallynode;
  1169. connode:=tonnode;
  1170. end.