nflw.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Type checking and register allocation for nodes that influence
  5. the flow
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit nflw;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. node,cpubase,
  24. aasmbase,aasmtai,aasmcpu,symnot,
  25. symtype,symbase,symdef,symsym;
  26. type
  27. { flags used by loop nodes }
  28. tloopflag = (
  29. { set if it is a for ... downto ... do loop }
  30. lnf_backward,
  31. { Do we need to parse childs to set var state? }
  32. lnf_varstate,
  33. { Do a test at the begin of the loop?}
  34. lnf_testatbegin,
  35. { Negate the loop test? }
  36. lnf_checknegate,
  37. { Should the value of the loop variable on exit be correct. }
  38. lnf_dont_mind_loopvar_on_exit);
  39. tloopflags = set of tloopflag;
  40. const
  41. { loop flags which must match to consider loop nodes equal regarding the flags }
  42. loopflagsequal = [lnf_backward];
  43. type
  44. tloopnode = class(tbinarynode)
  45. t1,t2 : tnode;
  46. loopflags : tloopflags;
  47. constructor create(tt : tnodetype;l,r,_t1,_t2 : tnode);virtual;
  48. destructor destroy;override;
  49. function getcopy : tnode;override;
  50. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  51. procedure ppuwrite(ppufile:tcompilerppufile);override;
  52. procedure buildderefimpl;override;
  53. procedure derefimpl;override;
  54. procedure insertintolist(l : tnodelist);override;
  55. procedure printnodetree(var t:text);override;
  56. function docompare(p: tnode): boolean; override;
  57. end;
  58. twhilerepeatnode = class(tloopnode)
  59. constructor create(l,r:Tnode;tab,cn:boolean);virtual;
  60. function det_resulttype:tnode;override;
  61. function pass_1 : tnode;override;
  62. {$ifdef state_tracking}
  63. function track_state_pass(exec_known:boolean):boolean;override;
  64. {$endif}
  65. end;
  66. twhilerepeatnodeclass = class of twhilerepeatnode;
  67. tifnode = class(tloopnode)
  68. constructor create(l,r,_t1 : tnode);virtual;
  69. function det_resulttype:tnode;override;
  70. function pass_1 : tnode;override;
  71. end;
  72. tifnodeclass = class of tifnode;
  73. tfornode = class(tloopnode)
  74. loopvar_notid:cardinal;
  75. constructor create(l,r,_t1,_t2 : tnode;back : boolean);virtual;
  76. procedure loop_var_access(not_type:Tnotification_flag;symbol:Tsym);
  77. function det_resulttype:tnode;override;
  78. function pass_1 : tnode;override;
  79. end;
  80. tfornodeclass = class of tfornode;
  81. texitnode = class(tunarynode)
  82. constructor create(l:tnode);virtual;
  83. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  84. procedure ppuwrite(ppufile:tcompilerppufile);override;
  85. function det_resulttype:tnode;override;
  86. function pass_1 : tnode;override;
  87. end;
  88. texitnodeclass = class of texitnode;
  89. tbreaknode = class(tnode)
  90. constructor create;virtual;
  91. function det_resulttype:tnode;override;
  92. function pass_1 : tnode;override;
  93. end;
  94. tbreaknodeclass = class of tbreaknode;
  95. tcontinuenode = class(tnode)
  96. constructor create;virtual;
  97. function det_resulttype:tnode;override;
  98. function pass_1 : tnode;override;
  99. end;
  100. tcontinuenodeclass = class of tcontinuenode;
  101. tgotonode = class(tnode)
  102. labsym : tlabelsym;
  103. labsymderef : tderef;
  104. exceptionblock : integer;
  105. { internlab : tinterngotolabel;}
  106. constructor create(p : tlabelsym);virtual;
  107. { constructor createintern(g:tinterngotolabel);}
  108. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  109. procedure ppuwrite(ppufile:tcompilerppufile);override;
  110. procedure buildderefimpl;override;
  111. procedure derefimpl;override;
  112. function getcopy : tnode;override;
  113. function det_resulttype:tnode;override;
  114. function pass_1 : tnode;override;
  115. function docompare(p: tnode): boolean; override;
  116. end;
  117. tgotonodeclass = class of tgotonode;
  118. tlabelnode = class(tunarynode)
  119. labelnr : tasmlabel;
  120. labsym : tlabelsym;
  121. labsymderef : tderef;
  122. exceptionblock : integer;
  123. constructor createcase(p : tasmlabel;l:tnode);virtual;
  124. constructor create(p : tlabelsym;l:tnode);virtual;
  125. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  126. procedure ppuwrite(ppufile:tcompilerppufile);override;
  127. procedure buildderefimpl;override;
  128. procedure derefimpl;override;
  129. function getcopy : tnode;override;
  130. function det_resulttype:tnode;override;
  131. function pass_1 : tnode;override;
  132. function docompare(p: tnode): boolean; override;
  133. end;
  134. tlabelnodeclass = class of tlabelnode;
  135. traisenode = class(tbinarynode)
  136. frametree : tnode;
  137. constructor create(l,taddr,tframe:tnode);virtual;
  138. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  139. procedure ppuwrite(ppufile:tcompilerppufile);override;
  140. procedure buildderefimpl;override;
  141. procedure derefimpl;override;
  142. function getcopy : tnode;override;
  143. procedure insertintolist(l : tnodelist);override;
  144. function det_resulttype:tnode;override;
  145. function pass_1 : tnode;override;
  146. function docompare(p: tnode): boolean; override;
  147. end;
  148. traisenodeclass = class of traisenode;
  149. ttryexceptnode = class(tloopnode)
  150. constructor create(l,r,_t1 : tnode);virtual;
  151. function det_resulttype:tnode;override;
  152. function pass_1 : tnode;override;
  153. end;
  154. ttryexceptnodeclass = class of ttryexceptnode;
  155. ttryfinallynode = class(tloopnode)
  156. implicitframe : boolean;
  157. constructor create(l,r:tnode);virtual;
  158. constructor create_implicit(l,r,_t1:tnode);virtual;
  159. function det_resulttype:tnode;override;
  160. function pass_1 : tnode;override;
  161. end;
  162. ttryfinallynodeclass = class of ttryfinallynode;
  163. tonnode = class(tbinarynode)
  164. exceptsymtable : tsymtable;
  165. excepttype : tobjectdef;
  166. constructor create(l,r:tnode);virtual;
  167. destructor destroy;override;
  168. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  169. function det_resulttype:tnode;override;
  170. function pass_1 : tnode;override;
  171. function getcopy : tnode;override;
  172. function docompare(p: tnode): boolean; override;
  173. end;
  174. tonnodeclass = class of tonnode;
  175. var
  176. cwhilerepeatnode : twhilerepeatnodeclass;
  177. cifnode : tifnodeclass;
  178. cfornode : tfornodeclass;
  179. cexitnode : texitnodeclass;
  180. cbreaknode : tbreaknodeclass;
  181. ccontinuenode : tcontinuenodeclass;
  182. cgotonode : tgotonodeclass;
  183. clabelnode : tlabelnodeclass;
  184. craisenode : traisenodeclass;
  185. ctryexceptnode : ttryexceptnodeclass;
  186. ctryfinallynode : ttryfinallynodeclass;
  187. connode : tonnodeclass;
  188. implementation
  189. uses
  190. globtype,systems,
  191. cutils,verbose,globals,
  192. symconst,paramgr,defcmp,defutil,htypechk,pass_1,
  193. ncal,nadd,ncon,nmem,nld,ncnv,nbas,cgobj,
  194. {$ifdef state_tracking}
  195. nstate,
  196. {$endif}
  197. cgbase,procinfo
  198. ;
  199. {****************************************************************************
  200. TLOOPNODE
  201. *****************************************************************************}
  202. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  203. begin
  204. inherited create(tt,l,r);
  205. t1:=_t1;
  206. t2:=_t2;
  207. fileinfo:=l.fileinfo;
  208. end;
  209. destructor tloopnode.destroy;
  210. begin
  211. t1.free;
  212. t2.free;
  213. inherited destroy;
  214. end;
  215. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  216. begin
  217. inherited ppuload(t,ppufile);
  218. t1:=ppuloadnode(ppufile);
  219. t2:=ppuloadnode(ppufile);
  220. end;
  221. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  222. begin
  223. inherited ppuwrite(ppufile);
  224. ppuwritenode(ppufile,t1);
  225. ppuwritenode(ppufile,t2);
  226. end;
  227. procedure tloopnode.buildderefimpl;
  228. begin
  229. inherited buildderefimpl;
  230. if assigned(t1) then
  231. t1.buildderefimpl;
  232. if assigned(t2) then
  233. t2.buildderefimpl;
  234. end;
  235. procedure tloopnode.derefimpl;
  236. begin
  237. inherited derefimpl;
  238. if assigned(t1) then
  239. t1.derefimpl;
  240. if assigned(t2) then
  241. t2.derefimpl;
  242. end;
  243. function tloopnode.getcopy : tnode;
  244. var
  245. p : tloopnode;
  246. begin
  247. p:=tloopnode(inherited getcopy);
  248. if assigned(t1) then
  249. p.t1:=t1.getcopy
  250. else
  251. p.t1:=nil;
  252. if assigned(t2) then
  253. p.t2:=t2.getcopy
  254. else
  255. p.t2:=nil;
  256. p.loopflags:=loopflags;
  257. getcopy:=p;
  258. end;
  259. procedure tloopnode.insertintolist(l : tnodelist);
  260. begin
  261. end;
  262. procedure tloopnode.printnodetree(var t:text);
  263. begin
  264. write(t,printnodeindention,'(');
  265. printnodeindent;
  266. printnodeinfo(t);
  267. writeln(t);
  268. printnode(t,left);
  269. printnode(t,right);
  270. printnode(t,t1);
  271. printnode(t,t2);
  272. printnodeunindent;
  273. writeln(t,printnodeindention,')');
  274. end;
  275. function tloopnode.docompare(p: tnode): boolean;
  276. begin
  277. docompare :=
  278. inherited docompare(p) and
  279. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  280. t1.isequal(tloopnode(p).t1) and
  281. t2.isequal(tloopnode(p).t2);
  282. end;
  283. {****************************************************************************
  284. TWHILEREPEATNODE
  285. *****************************************************************************}
  286. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  287. begin
  288. inherited create(whilerepeatn,l,r,nil,nil);
  289. if tab then
  290. include(loopflags, lnf_testatbegin);
  291. if cn then
  292. include(loopflags,lnf_checknegate);
  293. end;
  294. function twhilerepeatnode.det_resulttype:tnode;
  295. var
  296. t:Tunarynode;
  297. begin
  298. result:=nil;
  299. resulttype:=voidtype;
  300. resulttypepass(left);
  301. {A not node can be removed.}
  302. if left.nodetype=notn then
  303. begin
  304. t:=Tunarynode(left);
  305. left:=Tunarynode(left).left;
  306. t.left:=nil;
  307. t.destroy;
  308. {Symdif operator, in case you are wondering:}
  309. loopflags:=loopflags >< [lnf_checknegate];
  310. end;
  311. { loop instruction }
  312. if assigned(right) then
  313. resulttypepass(right);
  314. set_varstate(left,vs_used,true);
  315. if codegenerror then
  316. exit;
  317. if not is_boolean(left.resulttype.def) then
  318. begin
  319. if left.resulttype.def.deftype=variantdef then
  320. inserttypeconv(left,booltype)
  321. else
  322. CGMessage1(type_e_boolean_expr_expected,left.resulttype.def.typename);
  323. end;
  324. { optimize constant expressions }
  325. if (left.nodetype=ordconstn) then
  326. begin
  327. { while false do }
  328. if (lnf_testatbegin in loopflags) and
  329. (tordconstnode(left).value=0) then
  330. begin
  331. if assigned(right) then
  332. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  333. result:=cnothingnode.create;
  334. end
  335. else
  336. { repeat until true }
  337. if not(lnf_testatbegin in loopflags) and
  338. (tordconstnode(left).value=1) then
  339. begin
  340. result:=right;
  341. right:=nil;
  342. end;
  343. end;
  344. end;
  345. function twhilerepeatnode.pass_1 : tnode;
  346. var
  347. old_t_times : longint;
  348. begin
  349. result:=nil;
  350. expectloc:=LOC_VOID;
  351. old_t_times:=cg.t_times;
  352. { calc register weight }
  353. if not(cs_littlesize in aktglobalswitches ) then
  354. cg.t_times:=cg.t_times*8;
  355. firstpass(left);
  356. if codegenerror then
  357. exit;
  358. registersint:=left.registersint;
  359. registersfpu:=left.registersfpu;
  360. {$ifdef SUPPORT_MMX}
  361. registersmmx:=left.registersmmx;
  362. {$endif SUPPORT_MMX}
  363. { loop instruction }
  364. if assigned(right) then
  365. begin
  366. firstpass(right);
  367. if codegenerror then
  368. exit;
  369. if registersint<right.registersint then
  370. registersint:=right.registersint;
  371. if registersfpu<right.registersfpu then
  372. registersfpu:=right.registersfpu;
  373. {$ifdef SUPPORT_MMX}
  374. if registersmmx<right.registersmmx then
  375. registersmmx:=right.registersmmx;
  376. {$endif SUPPORT_MMX}
  377. end;
  378. cg.t_times:=old_t_times;
  379. end;
  380. {$ifdef state_tracking}
  381. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  382. var condition:Tnode;
  383. code:Tnode;
  384. done:boolean;
  385. value:boolean;
  386. change:boolean;
  387. firsttest:boolean;
  388. factval:Tnode;
  389. begin
  390. track_state_pass:=false;
  391. done:=false;
  392. firsttest:=true;
  393. {For repeat until statements, first do a pass through the code.}
  394. if not(lnf_testatbegin in flags) then
  395. begin
  396. code:=right.getcopy;
  397. if code.track_state_pass(exec_known) then
  398. track_state_pass:=true;
  399. code.destroy;
  400. end;
  401. repeat
  402. condition:=left.getcopy;
  403. code:=right.getcopy;
  404. change:=condition.track_state_pass(exec_known);
  405. factval:=aktstate.find_fact(left);
  406. if factval<>nil then
  407. begin
  408. condition.destroy;
  409. condition:=factval.getcopy;
  410. change:=true;
  411. end;
  412. if change then
  413. begin
  414. track_state_pass:=true;
  415. {Force new resulttype pass.}
  416. condition.resulttype.def:=nil;
  417. do_resulttypepass(condition);
  418. end;
  419. if is_constboolnode(condition) then
  420. begin
  421. {Try to turn a while loop into a repeat loop.}
  422. if firsttest then
  423. exclude(flags,testatbegin);
  424. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  425. if value then
  426. begin
  427. if code.track_state_pass(exec_known) then
  428. track_state_pass:=true;
  429. end
  430. else
  431. done:=true;
  432. end
  433. else
  434. begin
  435. {Remove any modified variables from the state.}
  436. code.track_state_pass(false);
  437. done:=true;
  438. end;
  439. code.destroy;
  440. condition.destroy;
  441. firsttest:=false;
  442. until done;
  443. {The loop condition is also known, for example:
  444. while i<10 do
  445. begin
  446. ...
  447. end;
  448. When the loop is done, we do know that i<10 = false.
  449. }
  450. condition:=left.getcopy;
  451. if condition.track_state_pass(exec_known) then
  452. begin
  453. track_state_pass:=true;
  454. {Force new resulttype pass.}
  455. condition.resulttype.def:=nil;
  456. do_resulttypepass(condition);
  457. end;
  458. if not is_constboolnode(condition) then
  459. aktstate.store_fact(condition,
  460. cordconstnode.create(byte(checknegate),booltype,true))
  461. else
  462. condition.destroy;
  463. end;
  464. {$endif}
  465. {*****************************************************************************
  466. TIFNODE
  467. *****************************************************************************}
  468. constructor tifnode.create(l,r,_t1 : tnode);
  469. begin
  470. inherited create(ifn,l,r,_t1,nil);
  471. end;
  472. function tifnode.det_resulttype:tnode;
  473. begin
  474. result:=nil;
  475. resulttype:=voidtype;
  476. resulttypepass(left);
  477. { if path }
  478. if assigned(right) then
  479. resulttypepass(right);
  480. { else path }
  481. if assigned(t1) then
  482. resulttypepass(t1);
  483. set_varstate(left,vs_used,true);
  484. if codegenerror then
  485. exit;
  486. if not is_boolean(left.resulttype.def) then
  487. begin
  488. if left.resulttype.def.deftype=variantdef then
  489. inserttypeconv(left,booltype)
  490. else
  491. Message1(type_e_boolean_expr_expected,left.resulttype.def.typename);
  492. end;
  493. { optimize constant expressions }
  494. if left.nodetype=ordconstn then
  495. begin
  496. if tordconstnode(left).value=1 then
  497. begin
  498. if assigned(right) then
  499. result:=right
  500. else
  501. result:=cnothingnode.create;
  502. right:=nil;
  503. if assigned(t1) then
  504. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  505. end
  506. else
  507. begin
  508. if assigned(t1) then
  509. result:=t1
  510. else
  511. result:=cnothingnode.create;
  512. t1:=nil;
  513. if assigned(right) then
  514. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  515. end;
  516. end;
  517. end;
  518. function tifnode.pass_1 : tnode;
  519. var
  520. old_t_times : longint;
  521. begin
  522. result:=nil;
  523. expectloc:=LOC_VOID;
  524. old_t_times:=cg.t_times;
  525. firstpass(left);
  526. registersint:=left.registersint;
  527. registersfpu:=left.registersfpu;
  528. {$ifdef SUPPORT_MMX}
  529. registersmmx:=left.registersmmx;
  530. {$endif SUPPORT_MMX}
  531. { determines registers weigths }
  532. if not(cs_littlesize in aktglobalswitches) then
  533. cg.t_times:=cg.t_times div 2;
  534. if cg.t_times=0 then
  535. cg.t_times:=1;
  536. { if path }
  537. if assigned(right) then
  538. begin
  539. firstpass(right);
  540. if registersint<right.registersint then
  541. registersint:=right.registersint;
  542. if registersfpu<right.registersfpu then
  543. registersfpu:=right.registersfpu;
  544. {$ifdef SUPPORT_MMX}
  545. if registersmmx<right.registersmmx then
  546. registersmmx:=right.registersmmx;
  547. {$endif SUPPORT_MMX}
  548. end;
  549. { else path }
  550. if assigned(t1) then
  551. begin
  552. firstpass(t1);
  553. if registersint<t1.registersint then
  554. registersint:=t1.registersint;
  555. if registersfpu<t1.registersfpu then
  556. registersfpu:=t1.registersfpu;
  557. {$ifdef SUPPORT_MMX}
  558. if registersmmx<t1.registersmmx then
  559. registersmmx:=t1.registersmmx;
  560. {$endif SUPPORT_MMX}
  561. end;
  562. { leave if we've got an error in one of the paths }
  563. if codegenerror then
  564. exit;
  565. cg.t_times:=old_t_times;
  566. end;
  567. {*****************************************************************************
  568. TFORNODE
  569. *****************************************************************************}
  570. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  571. begin
  572. inherited create(forn,l,r,_t1,_t2);
  573. if back then
  574. include(loopflags,lnf_backward);
  575. include(loopflags,lnf_testatbegin);
  576. end;
  577. procedure Tfornode.loop_var_access(not_type:Tnotification_flag;
  578. symbol:Tsym);
  579. begin
  580. {If there is a read access, the value of the loop counter is important;
  581. at the end of the loop the loop variable should contain the value it
  582. had in the last iteration.}
  583. if not_type=vn_onwrite then
  584. begin
  585. writeln('Loopvar does not matter on exit');
  586. end
  587. else
  588. begin
  589. exclude(loopflags,lnf_dont_mind_loopvar_on_exit);
  590. writeln('Loopvar does matter on exit');
  591. end;
  592. Tabstractvarsym(symbol).unregister_notification(loopvar_notid);
  593. end;
  594. function tfornode.det_resulttype:tnode;
  595. begin
  596. result:=nil;
  597. resulttype:=voidtype;
  598. { process the loopvar, from and to, varstates are already set }
  599. resulttypepass(left);
  600. resulttypepass(right);
  601. resulttypepass(t1);
  602. {Can we spare the first comparision?}
  603. if (t1.nodetype=ordconstn) and
  604. (right.nodetype=ordconstn) and
  605. (
  606. (
  607. (lnf_backward in loopflags) and
  608. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  609. ) or
  610. (
  611. not(lnf_backward in loopflags) and
  612. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  613. )
  614. ) then
  615. exclude(loopflags,lnf_testatbegin);
  616. { Make sure that the loop var and the
  617. from and to values are compatible types }
  618. inserttypeconv(right,left.resulttype);
  619. inserttypeconv(t1,left.resulttype);
  620. if assigned(t2) then
  621. resulttypepass(t2);
  622. end;
  623. function tfornode.pass_1 : tnode;
  624. var
  625. old_t_times : longint;
  626. begin
  627. result:=nil;
  628. expectloc:=LOC_VOID;
  629. firstpass(left);
  630. if left.registersint>registersint then
  631. registersint:=left.registersint;
  632. if left.registersfpu>registersfpu then
  633. registersfpu:=left.registersfpu;
  634. {$ifdef SUPPORT_MMX}
  635. if left.registersmmx>registersmmx then
  636. registersmmx:=left.registersmmx;
  637. {$endif SUPPORT_MMX}
  638. firstpass(right);
  639. if right.registersint>registersint then
  640. registersint:=right.registersint;
  641. if right.registersfpu>registersfpu then
  642. registersfpu:=right.registersfpu;
  643. {$ifdef SUPPORT_MMX}
  644. if right.registersmmx>registersmmx then
  645. registersmmx:=right.registersmmx;
  646. {$endif SUPPORT_MMX}
  647. firstpass(t1);
  648. if t1.registersint>registersint then
  649. registersint:=t1.registersint;
  650. if t1.registersfpu>registersfpu then
  651. registersfpu:=t1.registersfpu;
  652. {$ifdef SUPPORT_MMX}
  653. if t1.registersmmx>registersmmx then
  654. registersmmx:=t1.registersmmx;
  655. {$endif SUPPORT_MMX}
  656. if assigned(t2) then
  657. begin
  658. { Calc register weight }
  659. old_t_times:=cg.t_times;
  660. if not(cs_littlesize in aktglobalswitches) then
  661. cg.t_times:=cg.t_times*8;
  662. firstpass(t2);
  663. if codegenerror then
  664. exit;
  665. if t2.registersint>registersint then
  666. registersint:=t2.registersint;
  667. if t2.registersfpu>registersfpu then
  668. registersfpu:=t2.registersfpu;
  669. {$ifdef SUPPORT_MMX}
  670. if t2.registersmmx>registersmmx then
  671. registersmmx:=t2.registersmmx;
  672. {$endif SUPPORT_MMX}
  673. cg.t_times:=old_t_times;
  674. end;
  675. { we need at least one register for comparisons PM }
  676. if registersint=0 then
  677. inc(registersint);
  678. end;
  679. {*****************************************************************************
  680. TEXITNODE
  681. *****************************************************************************}
  682. constructor texitnode.create(l:tnode);
  683. begin
  684. inherited create(exitn,l);
  685. end;
  686. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  687. begin
  688. inherited ppuload(t,ppufile);
  689. end;
  690. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  691. begin
  692. inherited ppuwrite(ppufile);
  693. end;
  694. function texitnode.det_resulttype:tnode;
  695. begin
  696. result:=nil;
  697. if assigned(left) then
  698. begin
  699. { add assignment to funcretsym }
  700. inserttypeconv(left,current_procinfo.procdef.rettype);
  701. left:=cassignmentnode.create(
  702. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  703. left);
  704. resulttypepass(left);
  705. set_varstate(left,vs_used,true);
  706. end;
  707. resulttype:=voidtype;
  708. end;
  709. function texitnode.pass_1 : tnode;
  710. begin
  711. result:=nil;
  712. expectloc:=LOC_VOID;
  713. if assigned(left) then
  714. begin
  715. firstpass(left);
  716. if codegenerror then
  717. exit;
  718. registersint:=left.registersint;
  719. registersfpu:=left.registersfpu;
  720. {$ifdef SUPPORT_MMX}
  721. registersmmx:=left.registersmmx;
  722. {$endif SUPPORT_MMX}
  723. end;
  724. end;
  725. {*****************************************************************************
  726. TBREAKNODE
  727. *****************************************************************************}
  728. constructor tbreaknode.create;
  729. begin
  730. inherited create(breakn);
  731. end;
  732. function tbreaknode.det_resulttype:tnode;
  733. begin
  734. result:=nil;
  735. resulttype:=voidtype;
  736. end;
  737. function tbreaknode.pass_1 : tnode;
  738. begin
  739. result:=nil;
  740. expectloc:=LOC_VOID;
  741. end;
  742. {*****************************************************************************
  743. TCONTINUENODE
  744. *****************************************************************************}
  745. constructor tcontinuenode.create;
  746. begin
  747. inherited create(continuen);
  748. end;
  749. function tcontinuenode.det_resulttype:tnode;
  750. begin
  751. result:=nil;
  752. resulttype:=voidtype;
  753. end;
  754. function tcontinuenode.pass_1 : tnode;
  755. begin
  756. result:=nil;
  757. expectloc:=LOC_VOID;
  758. end;
  759. {*****************************************************************************
  760. TGOTONODE
  761. *****************************************************************************}
  762. constructor tgotonode.create(p : tlabelsym);
  763. begin
  764. inherited create(goton);
  765. exceptionblock:=aktexceptblock;
  766. labsym:=p;
  767. end;
  768. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  769. begin
  770. inherited ppuload(t,ppufile);
  771. ppufile.getderef(labsymderef);
  772. exceptionblock:=ppufile.getbyte;
  773. end;
  774. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  775. begin
  776. inherited ppuwrite(ppufile);
  777. ppufile.putderef(labsymderef);
  778. ppufile.putbyte(exceptionblock);
  779. end;
  780. procedure tgotonode.buildderefimpl;
  781. begin
  782. inherited buildderefimpl;
  783. labsymderef.build(labsym);
  784. end;
  785. procedure tgotonode.derefimpl;
  786. begin
  787. inherited derefimpl;
  788. labsym:=tlabelsym(labsymderef.resolve);
  789. end;
  790. function tgotonode.det_resulttype:tnode;
  791. begin
  792. result:=nil;
  793. resulttype:=voidtype;
  794. end;
  795. function tgotonode.pass_1 : tnode;
  796. begin
  797. result:=nil;
  798. expectloc:=LOC_VOID;
  799. { check if }
  800. if assigned(labsym) and
  801. assigned(labsym.code) and
  802. (exceptionblock<>tlabelnode(labsym.code).exceptionblock) then
  803. CGMessage(cg_e_goto_inout_of_exception_block);
  804. end;
  805. function tgotonode.getcopy : tnode;
  806. var
  807. p : tgotonode;
  808. begin
  809. p:=tgotonode(inherited getcopy);
  810. p.labsym:=labsym;
  811. p.exceptionblock:=exceptionblock;
  812. result:=p;
  813. end;
  814. function tgotonode.docompare(p: tnode): boolean;
  815. begin
  816. docompare := false;
  817. end;
  818. {*****************************************************************************
  819. TLABELNODE
  820. *****************************************************************************}
  821. constructor tlabelnode.createcase(p : tasmlabel;l:tnode);
  822. begin
  823. inherited create(labeln,l);
  824. { it shouldn't be possible to jump to case labels using goto }
  825. exceptionblock:=-1;
  826. labsym:=nil;
  827. labelnr:=p;
  828. end;
  829. constructor tlabelnode.create(p : tlabelsym;l:tnode);
  830. begin
  831. inherited create(labeln,l);
  832. exceptionblock:=aktexceptblock;
  833. labsym:=p;
  834. labelnr:=p.lab;
  835. { save the current labelnode in the labelsym }
  836. p.code:=self;
  837. end;
  838. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  839. begin
  840. inherited ppuload(t,ppufile);
  841. ppufile.getderef(labsymderef);
  842. labelnr:=tasmlabel(ppufile.getasmsymbol);
  843. exceptionblock:=ppufile.getbyte;
  844. end;
  845. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  846. begin
  847. inherited ppuwrite(ppufile);
  848. ppufile.putderef(labsymderef);
  849. ppufile.putasmsymbol(labelnr);
  850. ppufile.putbyte(exceptionblock);
  851. end;
  852. procedure tlabelnode.buildderefimpl;
  853. begin
  854. inherited buildderefimpl;
  855. labsymderef.build(labsym);
  856. end;
  857. procedure tlabelnode.derefimpl;
  858. begin
  859. inherited derefimpl;
  860. labsym:=tlabelsym(labsymderef.resolve);
  861. objectlibrary.derefasmsymbol(tasmsymbol(labelnr));
  862. end;
  863. function tlabelnode.det_resulttype:tnode;
  864. begin
  865. result:=nil;
  866. { left could still be unassigned }
  867. if assigned(left) then
  868. resulttypepass(left);
  869. resulttype:=voidtype;
  870. end;
  871. function tlabelnode.pass_1 : tnode;
  872. begin
  873. result:=nil;
  874. expectloc:=LOC_VOID;
  875. if assigned(left) then
  876. begin
  877. firstpass(left);
  878. registersint:=left.registersint;
  879. registersfpu:=left.registersfpu;
  880. {$ifdef SUPPORT_MMX}
  881. registersmmx:=left.registersmmx;
  882. {$endif SUPPORT_MMX}
  883. end;
  884. end;
  885. function tlabelnode.getcopy : tnode;
  886. var
  887. p : tlabelnode;
  888. begin
  889. p:=tlabelnode(inherited getcopy);
  890. p.labelnr:=labelnr;
  891. p.exceptionblock:=exceptionblock;
  892. p.labsym:=labsym;
  893. result:=p;
  894. end;
  895. function tlabelnode.docompare(p: tnode): boolean;
  896. begin
  897. docompare := false;
  898. end;
  899. {*****************************************************************************
  900. TRAISENODE
  901. *****************************************************************************}
  902. constructor traisenode.create(l,taddr,tframe:tnode);
  903. begin
  904. inherited create(raisen,l,taddr);
  905. frametree:=tframe;
  906. end;
  907. constructor traisenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  908. begin
  909. inherited ppuload(t,ppufile);
  910. frametree:=ppuloadnode(ppufile);
  911. end;
  912. procedure traisenode.ppuwrite(ppufile:tcompilerppufile);
  913. begin
  914. inherited ppuwrite(ppufile);
  915. ppuwritenode(ppufile,frametree);
  916. end;
  917. procedure traisenode.buildderefimpl;
  918. begin
  919. inherited buildderefimpl;
  920. if assigned(frametree) then
  921. frametree.buildderefimpl;
  922. end;
  923. procedure traisenode.derefimpl;
  924. begin
  925. inherited derefimpl;
  926. if assigned(frametree) then
  927. frametree.derefimpl;
  928. end;
  929. function traisenode.getcopy : tnode;
  930. var
  931. n : traisenode;
  932. begin
  933. n:=traisenode(inherited getcopy);
  934. if assigned(frametree) then
  935. n.frametree:=frametree.getcopy
  936. else
  937. n.frametree:=nil;
  938. getcopy:=n;
  939. end;
  940. procedure traisenode.insertintolist(l : tnodelist);
  941. begin
  942. end;
  943. function traisenode.det_resulttype:tnode;
  944. begin
  945. result:=nil;
  946. resulttype:=voidtype;
  947. if assigned(left) then
  948. begin
  949. { first para must be a _class_ }
  950. resulttypepass(left);
  951. set_varstate(left,vs_used,true);
  952. if codegenerror then
  953. exit;
  954. if not(is_class(left.resulttype.def)) then
  955. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  956. { insert needed typeconvs for addr,frame }
  957. if assigned(right) then
  958. begin
  959. { addr }
  960. resulttypepass(right);
  961. inserttypeconv(right,voidpointertype);
  962. { frame }
  963. if assigned(frametree) then
  964. begin
  965. resulttypepass(frametree);
  966. inserttypeconv(frametree,voidpointertype);
  967. end;
  968. end;
  969. end;
  970. end;
  971. function traisenode.pass_1 : tnode;
  972. begin
  973. result:=nil;
  974. include(current_procinfo.flags,pi_do_call);
  975. expectloc:=LOC_VOID;
  976. if assigned(left) then
  977. begin
  978. { first para must be a _class_ }
  979. firstpass(left);
  980. { insert needed typeconvs for addr,frame }
  981. if assigned(right) then
  982. begin
  983. { addr }
  984. firstpass(right);
  985. { frame }
  986. if assigned(frametree) then
  987. firstpass(frametree);
  988. end;
  989. left_right_max;
  990. end;
  991. end;
  992. function traisenode.docompare(p: tnode): boolean;
  993. begin
  994. docompare := false;
  995. end;
  996. {*****************************************************************************
  997. TTRYEXCEPTNODE
  998. *****************************************************************************}
  999. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  1000. begin
  1001. inherited create(tryexceptn,l,r,_t1,nil);
  1002. end;
  1003. function ttryexceptnode.det_resulttype:tnode;
  1004. begin
  1005. result:=nil;
  1006. resulttypepass(left);
  1007. { on statements }
  1008. if assigned(right) then
  1009. resulttypepass(right);
  1010. { else block }
  1011. if assigned(t1) then
  1012. resulttypepass(t1);
  1013. resulttype:=voidtype;
  1014. end;
  1015. function ttryexceptnode.pass_1 : tnode;
  1016. begin
  1017. result:=nil;
  1018. include(current_procinfo.flags,pi_do_call);
  1019. expectloc:=LOC_VOID;
  1020. firstpass(left);
  1021. { on statements }
  1022. if assigned(right) then
  1023. begin
  1024. firstpass(right);
  1025. registersint:=max(registersint,right.registersint);
  1026. registersfpu:=max(registersfpu,right.registersfpu);
  1027. {$ifdef SUPPORT_MMX}
  1028. registersmmx:=max(registersmmx,right.registersmmx);
  1029. {$endif SUPPORT_MMX}
  1030. end;
  1031. { else block }
  1032. if assigned(t1) then
  1033. begin
  1034. firstpass(t1);
  1035. registersint:=max(registersint,t1.registersint);
  1036. registersfpu:=max(registersfpu,t1.registersfpu);
  1037. {$ifdef SUPPORT_MMX}
  1038. registersmmx:=max(registersmmx,t1.registersmmx);
  1039. {$endif SUPPORT_MMX}
  1040. end;
  1041. end;
  1042. {*****************************************************************************
  1043. TTRYFINALLYNODE
  1044. *****************************************************************************}
  1045. constructor ttryfinallynode.create(l,r:tnode);
  1046. begin
  1047. inherited create(tryfinallyn,l,r,nil,nil);
  1048. implicitframe:=false;
  1049. end;
  1050. constructor ttryfinallynode.create_implicit(l,r,_t1:tnode);
  1051. begin
  1052. inherited create(tryfinallyn,l,r,_t1,nil);
  1053. implicitframe:=true;
  1054. end;
  1055. function ttryfinallynode.det_resulttype:tnode;
  1056. begin
  1057. result:=nil;
  1058. include(current_procinfo.flags,pi_do_call);
  1059. resulttype:=voidtype;
  1060. resulttypepass(left);
  1061. set_varstate(left,vs_used,true);
  1062. resulttypepass(right);
  1063. set_varstate(right,vs_used,true);
  1064. { special finally block only executed when there was an exception }
  1065. if assigned(t1) then
  1066. begin
  1067. resulttypepass(t1);
  1068. set_varstate(t1,vs_used,true);
  1069. end;
  1070. end;
  1071. function ttryfinallynode.pass_1 : tnode;
  1072. begin
  1073. result:=nil;
  1074. expectloc:=LOC_VOID;
  1075. firstpass(left);
  1076. firstpass(right);
  1077. left_right_max;
  1078. if assigned(t1) then
  1079. begin
  1080. firstpass(t1);
  1081. registersint:=max(registersint,t1.registersint);
  1082. registersfpu:=max(registersfpu,t1.registersfpu);
  1083. {$ifdef SUPPORT_MMX}
  1084. registersmmx:=max(registersmmx,t1.registersmmx);
  1085. {$endif SUPPORT_MMX}
  1086. end;
  1087. end;
  1088. {*****************************************************************************
  1089. TONNODE
  1090. *****************************************************************************}
  1091. constructor tonnode.create(l,r:tnode);
  1092. begin
  1093. inherited create(onn,l,r);
  1094. exceptsymtable:=nil;
  1095. excepttype:=nil;
  1096. end;
  1097. destructor tonnode.destroy;
  1098. begin
  1099. { copied nodes don't need to release the symtable }
  1100. if assigned(exceptsymtable) then
  1101. exceptsymtable.free;
  1102. inherited destroy;
  1103. end;
  1104. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1105. begin
  1106. inherited ppuload(t,ppufile);
  1107. exceptsymtable:=nil;
  1108. excepttype:=nil;
  1109. end;
  1110. function tonnode.getcopy : tnode;
  1111. var
  1112. n : tonnode;
  1113. begin
  1114. n:=tonnode(inherited getcopy);
  1115. n.exceptsymtable:=exceptsymtable.getcopy;
  1116. n.excepttype:=excepttype;
  1117. result:=n;
  1118. end;
  1119. function tonnode.det_resulttype:tnode;
  1120. begin
  1121. result:=nil;
  1122. resulttype:=voidtype;
  1123. if not(is_class(excepttype)) then
  1124. CGMessage1(type_e_class_type_expected,excepttype.typename);
  1125. if assigned(left) then
  1126. resulttypepass(left);
  1127. if assigned(right) then
  1128. resulttypepass(right);
  1129. end;
  1130. function tonnode.pass_1 : tnode;
  1131. begin
  1132. result:=nil;
  1133. include(current_procinfo.flags,pi_do_call);
  1134. expectloc:=LOC_VOID;
  1135. registersint:=0;
  1136. registersfpu:=0;
  1137. {$ifdef SUPPORT_MMX}
  1138. registersmmx:=0;
  1139. {$endif SUPPORT_MMX}
  1140. if assigned(left) then
  1141. begin
  1142. firstpass(left);
  1143. registersint:=left.registersint;
  1144. registersfpu:=left.registersfpu;
  1145. {$ifdef SUPPORT_MMX}
  1146. registersmmx:=left.registersmmx;
  1147. {$endif SUPPORT_MMX}
  1148. end;
  1149. if assigned(right) then
  1150. begin
  1151. firstpass(right);
  1152. registersint:=max(registersint,right.registersint);
  1153. registersfpu:=max(registersfpu,right.registersfpu);
  1154. {$ifdef SUPPORT_MMX}
  1155. registersmmx:=max(registersmmx,right.registersmmx);
  1156. {$endif SUPPORT_MMX}
  1157. end;
  1158. end;
  1159. function tonnode.docompare(p: tnode): boolean;
  1160. begin
  1161. docompare := false;
  1162. end;
  1163. begin
  1164. cwhilerepeatnode:=twhilerepeatnode;
  1165. cifnode:=tifnode;
  1166. cfornode:=tfornode;
  1167. cexitnode:=texitnode;
  1168. cgotonode:=tgotonode;
  1169. clabelnode:=tlabelnode;
  1170. craisenode:=traisenode;
  1171. ctryexceptnode:=ttryexceptnode;
  1172. ctryfinallynode:=ttryfinallynode;
  1173. connode:=tonnode;
  1174. end.
  1175. {
  1176. $Log$
  1177. Revision 1.110 2005-02-23 20:38:09 florian
  1178. + variants can be used as cond. expr. in if, while, repeat ... until statements
  1179. Revision 1.109 2005/02/14 17:13:06 peter
  1180. * truncate log
  1181. Revision 1.108 2005/01/31 20:23:53 peter
  1182. * set varstate before parsing the instruction block in for statements
  1183. Revision 1.107 2005/01/31 16:16:21 peter
  1184. * for-node cleanup, checking for uninitialzed from and to values
  1185. is now supported
  1186. Revision 1.106 2005/01/16 14:44:03 peter
  1187. * fix unreachable code check for repeat loop
  1188. Revision 1.105 2005/01/16 10:50:32 peter
  1189. * give warning for unreachable code in while/if statements
  1190. Revision 1.104 2005/01/03 17:55:57 florian
  1191. + first batch of patches to support tdef.getcopy fully
  1192. }