nflw.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  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. optunrol;
  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 _getcopy : 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;
  62. function det_resulttype: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;
  71. function det_resulttype:tnode;override;
  72. function pass_1 : tnode;override;
  73. end;
  74. tifnodeclass = class of tifnode;
  75. tfornode = class(tloopnode)
  76. { if count isn divisable by unrolls then
  77. the for loop must jump to this label to get the correct
  78. number of executions }
  79. entrylabel : tnode;
  80. loopvar_notid:cardinal;
  81. constructor create(l,r,_t1,_t2 : tnode;back : boolean);virtual;
  82. procedure loop_var_access(not_type:Tnotification_flag;symbol:Tsym);
  83. function det_resulttype:tnode;override;
  84. function pass_1 : tnode;override;
  85. end;
  86. tfornodeclass = class of tfornode;
  87. texitnode = class(tunarynode)
  88. constructor create(l:tnode);virtual;
  89. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  90. procedure ppuwrite(ppufile:tcompilerppufile);override;
  91. function det_resulttype:tnode;override;
  92. function pass_1 : tnode;override;
  93. end;
  94. texitnodeclass = class of texitnode;
  95. tbreaknode = class(tnode)
  96. constructor create;virtual;
  97. function det_resulttype:tnode;override;
  98. function pass_1 : tnode;override;
  99. end;
  100. tbreaknodeclass = class of tbreaknode;
  101. tcontinuenode = class(tnode)
  102. constructor create;virtual;
  103. function det_resulttype:tnode;override;
  104. function pass_1 : tnode;override;
  105. end;
  106. tcontinuenodeclass = class of tcontinuenode;
  107. tgotonode = class(tnode)
  108. { we still need this for resolving forward gotos }
  109. labelsym : tlabelsym;
  110. labelnode : tlabelnode;
  111. exceptionblock : integer;
  112. { internlab : tinterngotolabel;}
  113. constructor create(p : tlabelnode);virtual;
  114. { as long as we don't know the label node we can't resolve it }
  115. constructor create_sym(p : tlabelsym);virtual;
  116. { constructor createintern(g:tinterngotolabel);}
  117. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  118. procedure ppuwrite(ppufile:tcompilerppufile);override;
  119. procedure buildderefimpl;override;
  120. procedure derefimpl;override;
  121. function _getcopy : tnode;override;
  122. function det_resulttype:tnode;override;
  123. function pass_1 : tnode;override;
  124. function docompare(p: tnode): boolean; override;
  125. end;
  126. tgotonodeclass = class of tgotonode;
  127. tlabelnode = class(tunarynode)
  128. exceptionblock : integer;
  129. { when copying trees, this points to the newly created copy of a label }
  130. copiedto : tlabelnode;
  131. { contains all goto nodesrefering to this label }
  132. referinggotonodes : tlist;
  133. constructor create(l:tnode);virtual;
  134. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  135. procedure ppuwrite(ppufile:tcompilerppufile);override;
  136. procedure buildderefimpl;override;
  137. procedure derefimpl;override;
  138. function _getcopy : tnode;override;
  139. function det_resulttype:tnode;override;
  140. function pass_1 : tnode;override;
  141. function docompare(p: tnode): boolean; override;
  142. end;
  143. tlabelnodeclass = class of tlabelnode;
  144. traisenode = class(tbinarynode)
  145. frametree : tnode;
  146. constructor create(l,taddr,tframe:tnode);virtual;
  147. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  148. procedure ppuwrite(ppufile:tcompilerppufile);override;
  149. procedure buildderefimpl;override;
  150. procedure derefimpl;override;
  151. function _getcopy : tnode;override;
  152. procedure insertintolist(l : tnodelist);override;
  153. function det_resulttype:tnode;override;
  154. function pass_1 : tnode;override;
  155. function docompare(p: tnode): boolean; override;
  156. end;
  157. traisenodeclass = class of traisenode;
  158. ttryexceptnode = class(tloopnode)
  159. constructor create(l,r,_t1 : tnode);virtual;
  160. function det_resulttype:tnode;override;
  161. function pass_1 : tnode;override;
  162. end;
  163. ttryexceptnodeclass = class of ttryexceptnode;
  164. ttryfinallynode = class(tloopnode)
  165. implicitframe : boolean;
  166. constructor create(l,r:tnode);virtual;
  167. constructor create_implicit(l,r,_t1:tnode);virtual;
  168. function det_resulttype:tnode;override;
  169. function pass_1 : tnode;override;
  170. end;
  171. ttryfinallynodeclass = class of ttryfinallynode;
  172. tonnode = class(tbinarynode)
  173. exceptsymtable : tsymtable;
  174. excepttype : tobjectdef;
  175. constructor create(l,r:tnode);virtual;
  176. destructor destroy;override;
  177. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  178. function det_resulttype:tnode;override;
  179. function pass_1 : tnode;override;
  180. function _getcopy : tnode;override;
  181. function docompare(p: tnode): boolean; override;
  182. end;
  183. tonnodeclass = class of tonnode;
  184. var
  185. cwhilerepeatnode : twhilerepeatnodeclass;
  186. cifnode : tifnodeclass;
  187. cfornode : tfornodeclass;
  188. cexitnode : texitnodeclass;
  189. cbreaknode : tbreaknodeclass;
  190. ccontinuenode : tcontinuenodeclass;
  191. cgotonode : tgotonodeclass;
  192. clabelnode : tlabelnodeclass;
  193. craisenode : traisenodeclass;
  194. ctryexceptnode : ttryexceptnodeclass;
  195. ctryfinallynode : ttryfinallynodeclass;
  196. connode : tonnodeclass;
  197. implementation
  198. uses
  199. globtype,systems,
  200. cutils,verbose,globals,
  201. symconst,paramgr,defcmp,defutil,htypechk,pass_1,
  202. ncal,nadd,ncon,nmem,nld,ncnv,nbas,cgobj,nutils,
  203. {$ifdef state_tracking}
  204. nstate,
  205. {$endif}
  206. cgbase,procinfo
  207. ;
  208. {****************************************************************************
  209. TLOOPNODE
  210. *****************************************************************************}
  211. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  212. begin
  213. inherited create(tt,l,r);
  214. t1:=_t1;
  215. t2:=_t2;
  216. fileinfo:=l.fileinfo;
  217. end;
  218. destructor tloopnode.destroy;
  219. begin
  220. t1.free;
  221. t2.free;
  222. inherited destroy;
  223. end;
  224. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  225. begin
  226. inherited ppuload(t,ppufile);
  227. t1:=ppuloadnode(ppufile);
  228. t2:=ppuloadnode(ppufile);
  229. end;
  230. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  231. begin
  232. inherited ppuwrite(ppufile);
  233. ppuwritenode(ppufile,t1);
  234. ppuwritenode(ppufile,t2);
  235. end;
  236. procedure tloopnode.buildderefimpl;
  237. begin
  238. inherited buildderefimpl;
  239. if assigned(t1) then
  240. t1.buildderefimpl;
  241. if assigned(t2) then
  242. t2.buildderefimpl;
  243. end;
  244. procedure tloopnode.derefimpl;
  245. begin
  246. inherited derefimpl;
  247. if assigned(t1) then
  248. t1.derefimpl;
  249. if assigned(t2) then
  250. t2.derefimpl;
  251. end;
  252. function tloopnode._getcopy : tnode;
  253. var
  254. p : tloopnode;
  255. begin
  256. p:=tloopnode(inherited _getcopy);
  257. if assigned(t1) then
  258. p.t1:=t1._getcopy
  259. else
  260. p.t1:=nil;
  261. if assigned(t2) then
  262. p.t2:=t2._getcopy
  263. else
  264. p.t2:=nil;
  265. p.loopflags:=loopflags;
  266. _getcopy:=p;
  267. end;
  268. procedure tloopnode.insertintolist(l : tnodelist);
  269. begin
  270. end;
  271. procedure tloopnode.printnodetree(var t:text);
  272. begin
  273. write(t,printnodeindention,'(');
  274. printnodeindent;
  275. printnodeinfo(t);
  276. writeln(t);
  277. printnode(t,left);
  278. printnode(t,right);
  279. printnode(t,t1);
  280. printnode(t,t2);
  281. printnodeunindent;
  282. writeln(t,printnodeindention,')');
  283. end;
  284. function tloopnode.docompare(p: tnode): boolean;
  285. begin
  286. docompare :=
  287. inherited docompare(p) and
  288. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  289. t1.isequal(tloopnode(p).t1) and
  290. t2.isequal(tloopnode(p).t2);
  291. end;
  292. {****************************************************************************
  293. TWHILEREPEATNODE
  294. *****************************************************************************}
  295. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  296. begin
  297. inherited create(whilerepeatn,l,r,nil,nil);
  298. if tab then
  299. include(loopflags, lnf_testatbegin);
  300. if cn then
  301. include(loopflags,lnf_checknegate);
  302. end;
  303. function twhilerepeatnode.det_resulttype:tnode;
  304. var
  305. t:Tunarynode;
  306. begin
  307. result:=nil;
  308. resulttype:=voidtype;
  309. resulttypepass(left);
  310. { tp procvar support }
  311. maybe_call_procvar(left,true);
  312. {A not node can be removed.}
  313. if left.nodetype=notn then
  314. begin
  315. t:=Tunarynode(left);
  316. left:=Tunarynode(left).left;
  317. t.left:=nil;
  318. t.destroy;
  319. {Symdif operator, in case you are wondering:}
  320. loopflags:=loopflags >< [lnf_checknegate];
  321. end;
  322. { loop instruction }
  323. if assigned(right) then
  324. resulttypepass(right);
  325. set_varstate(left,vs_read,[vsf_must_be_valid]);
  326. if codegenerror then
  327. exit;
  328. if not is_boolean(left.resulttype.def) then
  329. begin
  330. if left.resulttype.def.deftype=variantdef then
  331. inserttypeconv(left,booltype)
  332. else
  333. CGMessage1(type_e_boolean_expr_expected,left.resulttype.def.typename);
  334. end;
  335. { Give warnings for code that will never be executed for
  336. while false do }
  337. if (lnf_testatbegin in loopflags) and
  338. (left.nodetype=ordconstn) and
  339. (tordconstnode(left).value=0) and
  340. assigned(right) then
  341. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  342. end;
  343. function twhilerepeatnode.pass_1 : tnode;
  344. var
  345. old_t_times : longint;
  346. begin
  347. result:=nil;
  348. expectloc:=LOC_VOID;
  349. old_t_times:=cg.t_times;
  350. { calc register weight }
  351. if not(cs_littlesize in aktglobalswitches ) then
  352. cg.t_times:=cg.t_times*8;
  353. firstpass(left);
  354. if codegenerror then
  355. exit;
  356. registersint:=left.registersint;
  357. registersfpu:=left.registersfpu;
  358. {$ifdef SUPPORT_MMX}
  359. registersmmx:=left.registersmmx;
  360. {$endif SUPPORT_MMX}
  361. { loop instruction }
  362. if assigned(right) then
  363. begin
  364. firstpass(right);
  365. if codegenerror then
  366. exit;
  367. if registersint<right.registersint then
  368. registersint:=right.registersint;
  369. if registersfpu<right.registersfpu then
  370. registersfpu:=right.registersfpu;
  371. {$ifdef SUPPORT_MMX}
  372. if registersmmx<right.registersmmx then
  373. registersmmx:=right.registersmmx;
  374. {$endif SUPPORT_MMX}
  375. end;
  376. cg.t_times:=old_t_times;
  377. end;
  378. {$ifdef state_tracking}
  379. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  380. var condition:Tnode;
  381. code:Tnode;
  382. done:boolean;
  383. value:boolean;
  384. change:boolean;
  385. firsttest:boolean;
  386. factval:Tnode;
  387. begin
  388. track_state_pass:=false;
  389. done:=false;
  390. firsttest:=true;
  391. {For repeat until statements, first do a pass through the code.}
  392. if not(lnf_testatbegin in flags) then
  393. begin
  394. code:=right.getcopy;
  395. if code.track_state_pass(exec_known) then
  396. track_state_pass:=true;
  397. code.destroy;
  398. end;
  399. repeat
  400. condition:=left.getcopy;
  401. code:=right.getcopy;
  402. change:=condition.track_state_pass(exec_known);
  403. factval:=aktstate.find_fact(left);
  404. if factval<>nil then
  405. begin
  406. condition.destroy;
  407. condition:=factval.getcopy;
  408. change:=true;
  409. end;
  410. if change then
  411. begin
  412. track_state_pass:=true;
  413. {Force new resulttype pass.}
  414. condition.resulttype.def:=nil;
  415. do_resulttypepass(condition);
  416. end;
  417. if is_constboolnode(condition) then
  418. begin
  419. {Try to turn a while loop into a repeat loop.}
  420. if firsttest then
  421. exclude(flags,testatbegin);
  422. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  423. if value then
  424. begin
  425. if code.track_state_pass(exec_known) then
  426. track_state_pass:=true;
  427. end
  428. else
  429. done:=true;
  430. end
  431. else
  432. begin
  433. {Remove any modified variables from the state.}
  434. code.track_state_pass(false);
  435. done:=true;
  436. end;
  437. code.destroy;
  438. condition.destroy;
  439. firsttest:=false;
  440. until done;
  441. {The loop condition is also known, for example:
  442. while i<10 do
  443. begin
  444. ...
  445. end;
  446. When the loop is done, we do know that i<10 = false.
  447. }
  448. condition:=left.getcopy;
  449. if condition.track_state_pass(exec_known) then
  450. begin
  451. track_state_pass:=true;
  452. {Force new resulttype pass.}
  453. condition.resulttype.def:=nil;
  454. do_resulttypepass(condition);
  455. end;
  456. if not is_constboolnode(condition) then
  457. aktstate.store_fact(condition,
  458. cordconstnode.create(byte(checknegate),booltype,true))
  459. else
  460. condition.destroy;
  461. end;
  462. {$endif}
  463. {*****************************************************************************
  464. TIFNODE
  465. *****************************************************************************}
  466. constructor tifnode.create(l,r,_t1 : tnode);
  467. begin
  468. inherited create(ifn,l,r,_t1,nil);
  469. end;
  470. function tifnode.det_resulttype:tnode;
  471. begin
  472. result:=nil;
  473. resulttype:=voidtype;
  474. resulttypepass(left);
  475. { tp procvar support }
  476. maybe_call_procvar(left,true);
  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_read,[vsf_must_be_valid]);
  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. var
  596. unrollres : tnode;
  597. begin
  598. result:=nil;
  599. resulttype:=voidtype;
  600. { loop unrolling }
  601. if cs_loopunroll in aktglobalswitches then
  602. begin
  603. unrollres:=unroll_loop(self);
  604. if assigned(unrollres) then
  605. begin
  606. resulttypepass(unrollres);
  607. result:=unrollres;
  608. exit;
  609. end;
  610. end;
  611. { process the loopvar, from and to, varstates are already set }
  612. resulttypepass(left);
  613. resulttypepass(right);
  614. resulttypepass(t1);
  615. {Can we spare the first comparision?}
  616. if (t1.nodetype=ordconstn) and
  617. (right.nodetype=ordconstn) and
  618. (
  619. (
  620. (lnf_backward in loopflags) and
  621. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  622. ) or
  623. (
  624. not(lnf_backward in loopflags) and
  625. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  626. )
  627. ) then
  628. exclude(loopflags,lnf_testatbegin);
  629. { Make sure that the loop var and the
  630. from and to values are compatible types }
  631. inserttypeconv(right,left.resulttype);
  632. inserttypeconv(t1,left.resulttype);
  633. if assigned(t2) then
  634. resulttypepass(t2);
  635. end;
  636. function tfornode.pass_1 : tnode;
  637. var
  638. old_t_times : longint;
  639. begin
  640. result:=nil;
  641. expectloc:=LOC_VOID;
  642. firstpass(left);
  643. if left.registersint>registersint then
  644. registersint:=left.registersint;
  645. if left.registersfpu>registersfpu then
  646. registersfpu:=left.registersfpu;
  647. {$ifdef SUPPORT_MMX}
  648. if left.registersmmx>registersmmx then
  649. registersmmx:=left.registersmmx;
  650. {$endif SUPPORT_MMX}
  651. firstpass(right);
  652. if right.registersint>registersint then
  653. registersint:=right.registersint;
  654. if right.registersfpu>registersfpu then
  655. registersfpu:=right.registersfpu;
  656. {$ifdef SUPPORT_MMX}
  657. if right.registersmmx>registersmmx then
  658. registersmmx:=right.registersmmx;
  659. {$endif SUPPORT_MMX}
  660. firstpass(t1);
  661. if t1.registersint>registersint then
  662. registersint:=t1.registersint;
  663. if t1.registersfpu>registersfpu then
  664. registersfpu:=t1.registersfpu;
  665. {$ifdef SUPPORT_MMX}
  666. if t1.registersmmx>registersmmx then
  667. registersmmx:=t1.registersmmx;
  668. {$endif SUPPORT_MMX}
  669. if assigned(t2) then
  670. begin
  671. { Calc register weight }
  672. old_t_times:=cg.t_times;
  673. if not(cs_littlesize in aktglobalswitches) then
  674. cg.t_times:=cg.t_times*8;
  675. firstpass(t2);
  676. if codegenerror then
  677. exit;
  678. if t2.registersint>registersint then
  679. registersint:=t2.registersint;
  680. if t2.registersfpu>registersfpu then
  681. registersfpu:=t2.registersfpu;
  682. {$ifdef SUPPORT_MMX}
  683. if t2.registersmmx>registersmmx then
  684. registersmmx:=t2.registersmmx;
  685. {$endif SUPPORT_MMX}
  686. cg.t_times:=old_t_times;
  687. end;
  688. { we need at least one register for comparisons PM }
  689. if registersint=0 then
  690. inc(registersint);
  691. end;
  692. {*****************************************************************************
  693. TEXITNODE
  694. *****************************************************************************}
  695. constructor texitnode.create(l:tnode);
  696. begin
  697. inherited create(exitn,l);
  698. end;
  699. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  700. begin
  701. inherited ppuload(t,ppufile);
  702. end;
  703. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  704. begin
  705. inherited ppuwrite(ppufile);
  706. end;
  707. function texitnode.det_resulttype:tnode;
  708. begin
  709. result:=nil;
  710. if assigned(left) then
  711. begin
  712. { add assignment to funcretsym }
  713. inserttypeconv(left,current_procinfo.procdef.rettype);
  714. left:=cassignmentnode.create(
  715. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  716. left);
  717. resulttypepass(left);
  718. set_varstate(left,vs_read,[vsf_must_be_valid]);
  719. end;
  720. resulttype:=voidtype;
  721. end;
  722. function texitnode.pass_1 : tnode;
  723. begin
  724. result:=nil;
  725. expectloc:=LOC_VOID;
  726. if assigned(left) then
  727. begin
  728. firstpass(left);
  729. if codegenerror then
  730. exit;
  731. registersint:=left.registersint;
  732. registersfpu:=left.registersfpu;
  733. {$ifdef SUPPORT_MMX}
  734. registersmmx:=left.registersmmx;
  735. {$endif SUPPORT_MMX}
  736. end;
  737. end;
  738. {*****************************************************************************
  739. TBREAKNODE
  740. *****************************************************************************}
  741. constructor tbreaknode.create;
  742. begin
  743. inherited create(breakn);
  744. end;
  745. function tbreaknode.det_resulttype:tnode;
  746. begin
  747. result:=nil;
  748. resulttype:=voidtype;
  749. end;
  750. function tbreaknode.pass_1 : tnode;
  751. begin
  752. result:=nil;
  753. expectloc:=LOC_VOID;
  754. end;
  755. {*****************************************************************************
  756. TCONTINUENODE
  757. *****************************************************************************}
  758. constructor tcontinuenode.create;
  759. begin
  760. inherited create(continuen);
  761. end;
  762. function tcontinuenode.det_resulttype:tnode;
  763. begin
  764. result:=nil;
  765. resulttype:=voidtype;
  766. end;
  767. function tcontinuenode.pass_1 : tnode;
  768. begin
  769. result:=nil;
  770. expectloc:=LOC_VOID;
  771. end;
  772. {*****************************************************************************
  773. TGOTONODE
  774. *****************************************************************************}
  775. constructor tgotonode.create(p : tlabelnode);
  776. begin
  777. inherited create(goton);
  778. exceptionblock:=aktexceptblock;
  779. labelnode:=p;
  780. labelsym:=nil;
  781. end;
  782. constructor tgotonode.create_sym(p : tlabelsym);
  783. begin
  784. inherited create(goton);
  785. exceptionblock:=aktexceptblock;
  786. if assigned(p.code) then
  787. labelnode:=tlabelnode(p.code)
  788. else
  789. labelnode:=nil;
  790. labelsym:=p;
  791. end;
  792. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  793. begin
  794. inherited ppuload(t,ppufile);
  795. labelnode:=tlabelnode(ppuloadnoderef(ppufile));
  796. exceptionblock:=ppufile.getbyte;
  797. end;
  798. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  799. begin
  800. inherited ppuwrite(ppufile);
  801. ppuwritenoderef(ppufile,labelnode);
  802. ppufile.putbyte(exceptionblock);
  803. end;
  804. procedure tgotonode.buildderefimpl;
  805. begin
  806. inherited buildderefimpl;
  807. //!!! deref(labelnode);
  808. end;
  809. procedure tgotonode.derefimpl;
  810. begin
  811. inherited derefimpl;
  812. //!!! deref(labelnode);
  813. end;
  814. function tgotonode.det_resulttype:tnode;
  815. begin
  816. result:=nil;
  817. resulttype:=voidtype;
  818. end;
  819. function tgotonode.pass_1 : tnode;
  820. begin
  821. result:=nil;
  822. expectloc:=LOC_VOID;
  823. if not(assigned(labelnode)) then
  824. begin
  825. if assigned(labelsym.code) then
  826. labelnode:=tlabelnode(labelsym.code)
  827. else
  828. internalerror(200506183);
  829. end;
  830. { check if we don't mess with exception blocks }
  831. if assigned(labelnode) and
  832. (exceptionblock<>labelnode.exceptionblock) then
  833. CGMessage(cg_e_goto_inout_of_exception_block);
  834. end;
  835. function tgotonode._getcopy : tnode;
  836. var
  837. p : tgotonode;
  838. i : aint;
  839. begin
  840. p:=tgotonode(inherited _getcopy);
  841. {
  842. p.exceptionblock:=exceptionblock;
  843. { When we copying, we do an ugly trick to determine if the label used
  844. by the current goto node is already copied: if the referinggotonodes
  845. contains the current label, it isn't copied yet, so copy also the
  846. label node and set the copiedto field to the newly created node.
  847. If a label to copy is reached the copiedto field is checked. If it's non nil
  848. the copiedto field is returned and the copiedto field is reset to nil.
  849. }
  850. { assume no copying }
  851. newlabelnode:=labelnode;
  852. for i:=0 to labelnode.copiedto.referingotonodes.count-1 do
  853. begin
  854. { copy labelnode? }
  855. if labelnode.copiedto.referinggotonodes[i]=self then
  856. begin
  857. oldlabelnode.copiedto:=newlabelnode;
  858. end;
  859. end;
  860. p.labelnode:=newlabelnode;
  861. p.labelnode.referinggotonodes.add(self);
  862. }
  863. result:=p;
  864. end;
  865. function tgotonode.docompare(p: tnode): boolean;
  866. begin
  867. docompare := false;
  868. end;
  869. {*****************************************************************************
  870. TLABELNODE
  871. *****************************************************************************}
  872. constructor tlabelnode.create(l:tnode);
  873. begin
  874. inherited create(labeln,l);
  875. exceptionblock:=aktexceptblock;
  876. end;
  877. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  878. begin
  879. inherited ppuload(t,ppufile);
  880. exceptionblock:=ppufile.getbyte;
  881. end;
  882. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  883. begin
  884. inherited ppuwrite(ppufile);
  885. ppufile.putbyte(exceptionblock);
  886. end;
  887. procedure tlabelnode.buildderefimpl;
  888. begin
  889. inherited buildderefimpl;
  890. end;
  891. procedure tlabelnode.derefimpl;
  892. begin
  893. inherited derefimpl;
  894. end;
  895. function tlabelnode.det_resulttype:tnode;
  896. begin
  897. result:=nil;
  898. { left could still be unassigned }
  899. if assigned(left) then
  900. resulttypepass(left);
  901. resulttype:=voidtype;
  902. end;
  903. function tlabelnode.pass_1 : tnode;
  904. begin
  905. result:=nil;
  906. expectloc:=LOC_VOID;
  907. if assigned(left) then
  908. begin
  909. firstpass(left);
  910. registersint:=left.registersint;
  911. registersfpu:=left.registersfpu;
  912. {$ifdef SUPPORT_MMX}
  913. registersmmx:=left.registersmmx;
  914. {$endif SUPPORT_MMX}
  915. end;
  916. end;
  917. function tlabelnode._getcopy : tnode;
  918. var
  919. p : tlabelnode;
  920. begin
  921. p:=tlabelnode(inherited _getcopy);
  922. p.exceptionblock:=exceptionblock;
  923. result:=p;
  924. end;
  925. function tlabelnode.docompare(p: tnode): boolean;
  926. begin
  927. docompare := false;
  928. end;
  929. {*****************************************************************************
  930. TRAISENODE
  931. *****************************************************************************}
  932. constructor traisenode.create(l,taddr,tframe:tnode);
  933. begin
  934. inherited create(raisen,l,taddr);
  935. frametree:=tframe;
  936. end;
  937. constructor traisenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  938. begin
  939. inherited ppuload(t,ppufile);
  940. frametree:=ppuloadnode(ppufile);
  941. end;
  942. procedure traisenode.ppuwrite(ppufile:tcompilerppufile);
  943. begin
  944. inherited ppuwrite(ppufile);
  945. ppuwritenode(ppufile,frametree);
  946. end;
  947. procedure traisenode.buildderefimpl;
  948. begin
  949. inherited buildderefimpl;
  950. if assigned(frametree) then
  951. frametree.buildderefimpl;
  952. end;
  953. procedure traisenode.derefimpl;
  954. begin
  955. inherited derefimpl;
  956. if assigned(frametree) then
  957. frametree.derefimpl;
  958. end;
  959. function traisenode._getcopy : tnode;
  960. var
  961. n : traisenode;
  962. begin
  963. n:=traisenode(inherited _getcopy);
  964. if assigned(frametree) then
  965. n.frametree:=frametree._getcopy
  966. else
  967. n.frametree:=nil;
  968. _getcopy:=n;
  969. end;
  970. procedure traisenode.insertintolist(l : tnodelist);
  971. begin
  972. end;
  973. function traisenode.det_resulttype:tnode;
  974. begin
  975. result:=nil;
  976. resulttype:=voidtype;
  977. if assigned(left) then
  978. begin
  979. { first para must be a _class_ }
  980. resulttypepass(left);
  981. set_varstate(left,vs_read,[vsf_must_be_valid]);
  982. if codegenerror then
  983. exit;
  984. if not(is_class(left.resulttype.def)) then
  985. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  986. { insert needed typeconvs for addr,frame }
  987. if assigned(right) then
  988. begin
  989. { addr }
  990. resulttypepass(right);
  991. inserttypeconv(right,voidpointertype);
  992. { frame }
  993. if assigned(frametree) then
  994. begin
  995. resulttypepass(frametree);
  996. inserttypeconv(frametree,voidpointertype);
  997. end;
  998. end;
  999. end;
  1000. end;
  1001. function traisenode.pass_1 : tnode;
  1002. begin
  1003. result:=nil;
  1004. include(current_procinfo.flags,pi_do_call);
  1005. expectloc:=LOC_VOID;
  1006. if assigned(left) then
  1007. begin
  1008. { first para must be a _class_ }
  1009. firstpass(left);
  1010. { insert needed typeconvs for addr,frame }
  1011. if assigned(right) then
  1012. begin
  1013. { addr }
  1014. firstpass(right);
  1015. { frame }
  1016. if assigned(frametree) then
  1017. firstpass(frametree);
  1018. end;
  1019. left_right_max;
  1020. end;
  1021. end;
  1022. function traisenode.docompare(p: tnode): boolean;
  1023. begin
  1024. docompare := false;
  1025. end;
  1026. {*****************************************************************************
  1027. TTRYEXCEPTNODE
  1028. *****************************************************************************}
  1029. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  1030. begin
  1031. inherited create(tryexceptn,l,r,_t1,nil);
  1032. end;
  1033. function ttryexceptnode.det_resulttype:tnode;
  1034. begin
  1035. result:=nil;
  1036. resulttypepass(left);
  1037. { on statements }
  1038. if assigned(right) then
  1039. resulttypepass(right);
  1040. { else block }
  1041. if assigned(t1) then
  1042. resulttypepass(t1);
  1043. resulttype:=voidtype;
  1044. end;
  1045. function ttryexceptnode.pass_1 : tnode;
  1046. begin
  1047. result:=nil;
  1048. include(current_procinfo.flags,pi_do_call);
  1049. expectloc:=LOC_VOID;
  1050. firstpass(left);
  1051. { on statements }
  1052. if assigned(right) then
  1053. begin
  1054. firstpass(right);
  1055. registersint:=max(registersint,right.registersint);
  1056. registersfpu:=max(registersfpu,right.registersfpu);
  1057. {$ifdef SUPPORT_MMX}
  1058. registersmmx:=max(registersmmx,right.registersmmx);
  1059. {$endif SUPPORT_MMX}
  1060. end;
  1061. { else block }
  1062. if assigned(t1) then
  1063. begin
  1064. firstpass(t1);
  1065. registersint:=max(registersint,t1.registersint);
  1066. registersfpu:=max(registersfpu,t1.registersfpu);
  1067. {$ifdef SUPPORT_MMX}
  1068. registersmmx:=max(registersmmx,t1.registersmmx);
  1069. {$endif SUPPORT_MMX}
  1070. end;
  1071. end;
  1072. {*****************************************************************************
  1073. TTRYFINALLYNODE
  1074. *****************************************************************************}
  1075. constructor ttryfinallynode.create(l,r:tnode);
  1076. begin
  1077. inherited create(tryfinallyn,l,r,nil,nil);
  1078. implicitframe:=false;
  1079. end;
  1080. constructor ttryfinallynode.create_implicit(l,r,_t1:tnode);
  1081. begin
  1082. inherited create(tryfinallyn,l,r,_t1,nil);
  1083. implicitframe:=true;
  1084. end;
  1085. function ttryfinallynode.det_resulttype:tnode;
  1086. begin
  1087. result:=nil;
  1088. include(current_procinfo.flags,pi_do_call);
  1089. resulttype:=voidtype;
  1090. resulttypepass(left);
  1091. // "try block" is "used"? (JM)
  1092. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  1093. resulttypepass(right);
  1094. // "except block" is "used"? (JM)
  1095. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  1096. { special finally block only executed when there was an exception }
  1097. if assigned(t1) then
  1098. begin
  1099. resulttypepass(t1);
  1100. // "finally block" is "used"? (JM)
  1101. set_varstate(t1,vs_readwritten,[vsf_must_be_valid]);
  1102. end;
  1103. end;
  1104. function ttryfinallynode.pass_1 : tnode;
  1105. begin
  1106. result:=nil;
  1107. expectloc:=LOC_VOID;
  1108. firstpass(left);
  1109. firstpass(right);
  1110. left_right_max;
  1111. if assigned(t1) then
  1112. begin
  1113. firstpass(t1);
  1114. registersint:=max(registersint,t1.registersint);
  1115. registersfpu:=max(registersfpu,t1.registersfpu);
  1116. {$ifdef SUPPORT_MMX}
  1117. registersmmx:=max(registersmmx,t1.registersmmx);
  1118. {$endif SUPPORT_MMX}
  1119. end;
  1120. end;
  1121. {*****************************************************************************
  1122. TONNODE
  1123. *****************************************************************************}
  1124. constructor tonnode.create(l,r:tnode);
  1125. begin
  1126. inherited create(onn,l,r);
  1127. exceptsymtable:=nil;
  1128. excepttype:=nil;
  1129. end;
  1130. destructor tonnode.destroy;
  1131. begin
  1132. { copied nodes don't need to release the symtable }
  1133. if assigned(exceptsymtable) then
  1134. exceptsymtable.free;
  1135. inherited destroy;
  1136. end;
  1137. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1138. begin
  1139. inherited ppuload(t,ppufile);
  1140. exceptsymtable:=nil;
  1141. excepttype:=nil;
  1142. end;
  1143. function tonnode._getcopy : tnode;
  1144. var
  1145. n : tonnode;
  1146. begin
  1147. n:=tonnode(inherited _getcopy);
  1148. n.exceptsymtable:=exceptsymtable.getcopy;
  1149. n.excepttype:=excepttype;
  1150. result:=n;
  1151. end;
  1152. function tonnode.det_resulttype:tnode;
  1153. begin
  1154. result:=nil;
  1155. resulttype:=voidtype;
  1156. if not(is_class(excepttype)) then
  1157. CGMessage1(type_e_class_type_expected,excepttype.typename);
  1158. if assigned(left) then
  1159. resulttypepass(left);
  1160. if assigned(right) then
  1161. resulttypepass(right);
  1162. end;
  1163. function tonnode.pass_1 : tnode;
  1164. begin
  1165. result:=nil;
  1166. include(current_procinfo.flags,pi_do_call);
  1167. expectloc:=LOC_VOID;
  1168. registersint:=0;
  1169. registersfpu:=0;
  1170. {$ifdef SUPPORT_MMX}
  1171. registersmmx:=0;
  1172. {$endif SUPPORT_MMX}
  1173. if assigned(left) then
  1174. begin
  1175. firstpass(left);
  1176. registersint:=left.registersint;
  1177. registersfpu:=left.registersfpu;
  1178. {$ifdef SUPPORT_MMX}
  1179. registersmmx:=left.registersmmx;
  1180. {$endif SUPPORT_MMX}
  1181. end;
  1182. if assigned(right) then
  1183. begin
  1184. firstpass(right);
  1185. registersint:=max(registersint,right.registersint);
  1186. registersfpu:=max(registersfpu,right.registersfpu);
  1187. {$ifdef SUPPORT_MMX}
  1188. registersmmx:=max(registersmmx,right.registersmmx);
  1189. {$endif SUPPORT_MMX}
  1190. end;
  1191. end;
  1192. function tonnode.docompare(p: tnode): boolean;
  1193. begin
  1194. docompare := false;
  1195. end;
  1196. begin
  1197. cwhilerepeatnode:=twhilerepeatnode;
  1198. cifnode:=tifnode;
  1199. cfornode:=tfornode;
  1200. cexitnode:=texitnode;
  1201. cgotonode:=tgotonode;
  1202. clabelnode:=tlabelnode;
  1203. craisenode:=traisenode;
  1204. ctryexceptnode:=ttryexceptnode;
  1205. ctryfinallynode:=ttryfinallynode;
  1206. connode:=tonnode;
  1207. end.