nflw.pas 40 KB

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