nflw.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  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,[vsf_must_be_valid]);
  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. { Give warnings for code that will never be executed for
  325. while false do }
  326. if (lnf_testatbegin in loopflags) and
  327. (left.nodetype=ordconstn) and
  328. (tordconstnode(left).value=0) and
  329. assigned(right) then
  330. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  331. end;
  332. function twhilerepeatnode.pass_1 : tnode;
  333. var
  334. old_t_times : longint;
  335. begin
  336. result:=nil;
  337. expectloc:=LOC_VOID;
  338. old_t_times:=cg.t_times;
  339. { calc register weight }
  340. if not(cs_littlesize in aktglobalswitches ) then
  341. cg.t_times:=cg.t_times*8;
  342. firstpass(left);
  343. if codegenerror then
  344. exit;
  345. registersint:=left.registersint;
  346. registersfpu:=left.registersfpu;
  347. {$ifdef SUPPORT_MMX}
  348. registersmmx:=left.registersmmx;
  349. {$endif SUPPORT_MMX}
  350. { loop instruction }
  351. if assigned(right) then
  352. begin
  353. firstpass(right);
  354. if codegenerror then
  355. exit;
  356. if registersint<right.registersint then
  357. registersint:=right.registersint;
  358. if registersfpu<right.registersfpu then
  359. registersfpu:=right.registersfpu;
  360. {$ifdef SUPPORT_MMX}
  361. if registersmmx<right.registersmmx then
  362. registersmmx:=right.registersmmx;
  363. {$endif SUPPORT_MMX}
  364. end;
  365. cg.t_times:=old_t_times;
  366. end;
  367. {$ifdef state_tracking}
  368. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  369. var condition:Tnode;
  370. code:Tnode;
  371. done:boolean;
  372. value:boolean;
  373. change:boolean;
  374. firsttest:boolean;
  375. factval:Tnode;
  376. begin
  377. track_state_pass:=false;
  378. done:=false;
  379. firsttest:=true;
  380. {For repeat until statements, first do a pass through the code.}
  381. if not(lnf_testatbegin in flags) then
  382. begin
  383. code:=right.getcopy;
  384. if code.track_state_pass(exec_known) then
  385. track_state_pass:=true;
  386. code.destroy;
  387. end;
  388. repeat
  389. condition:=left.getcopy;
  390. code:=right.getcopy;
  391. change:=condition.track_state_pass(exec_known);
  392. factval:=aktstate.find_fact(left);
  393. if factval<>nil then
  394. begin
  395. condition.destroy;
  396. condition:=factval.getcopy;
  397. change:=true;
  398. end;
  399. if change then
  400. begin
  401. track_state_pass:=true;
  402. {Force new resulttype pass.}
  403. condition.resulttype.def:=nil;
  404. do_resulttypepass(condition);
  405. end;
  406. if is_constboolnode(condition) then
  407. begin
  408. {Try to turn a while loop into a repeat loop.}
  409. if firsttest then
  410. exclude(flags,testatbegin);
  411. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  412. if value then
  413. begin
  414. if code.track_state_pass(exec_known) then
  415. track_state_pass:=true;
  416. end
  417. else
  418. done:=true;
  419. end
  420. else
  421. begin
  422. {Remove any modified variables from the state.}
  423. code.track_state_pass(false);
  424. done:=true;
  425. end;
  426. code.destroy;
  427. condition.destroy;
  428. firsttest:=false;
  429. until done;
  430. {The loop condition is also known, for example:
  431. while i<10 do
  432. begin
  433. ...
  434. end;
  435. When the loop is done, we do know that i<10 = false.
  436. }
  437. condition:=left.getcopy;
  438. if condition.track_state_pass(exec_known) then
  439. begin
  440. track_state_pass:=true;
  441. {Force new resulttype pass.}
  442. condition.resulttype.def:=nil;
  443. do_resulttypepass(condition);
  444. end;
  445. if not is_constboolnode(condition) then
  446. aktstate.store_fact(condition,
  447. cordconstnode.create(byte(checknegate),booltype,true))
  448. else
  449. condition.destroy;
  450. end;
  451. {$endif}
  452. {*****************************************************************************
  453. TIFNODE
  454. *****************************************************************************}
  455. constructor tifnode.create(l,r,_t1 : tnode);
  456. begin
  457. inherited create(ifn,l,r,_t1,nil);
  458. end;
  459. function tifnode.det_resulttype:tnode;
  460. begin
  461. result:=nil;
  462. resulttype:=voidtype;
  463. resulttypepass(left);
  464. { if path }
  465. if assigned(right) then
  466. resulttypepass(right);
  467. { else path }
  468. if assigned(t1) then
  469. resulttypepass(t1);
  470. set_varstate(left,vs_used,[vsf_must_be_valid]);
  471. if codegenerror then
  472. exit;
  473. if not is_boolean(left.resulttype.def) then
  474. begin
  475. if left.resulttype.def.deftype=variantdef then
  476. inserttypeconv(left,booltype)
  477. else
  478. Message1(type_e_boolean_expr_expected,left.resulttype.def.typename);
  479. end;
  480. { optimize constant expressions }
  481. if left.nodetype=ordconstn then
  482. begin
  483. if tordconstnode(left).value=1 then
  484. begin
  485. if assigned(right) then
  486. result:=right
  487. else
  488. result:=cnothingnode.create;
  489. right:=nil;
  490. if assigned(t1) then
  491. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  492. end
  493. else
  494. begin
  495. if assigned(t1) then
  496. result:=t1
  497. else
  498. result:=cnothingnode.create;
  499. t1:=nil;
  500. if assigned(right) then
  501. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  502. end;
  503. end;
  504. end;
  505. function tifnode.pass_1 : tnode;
  506. var
  507. old_t_times : longint;
  508. begin
  509. result:=nil;
  510. expectloc:=LOC_VOID;
  511. old_t_times:=cg.t_times;
  512. firstpass(left);
  513. registersint:=left.registersint;
  514. registersfpu:=left.registersfpu;
  515. {$ifdef SUPPORT_MMX}
  516. registersmmx:=left.registersmmx;
  517. {$endif SUPPORT_MMX}
  518. { determines registers weigths }
  519. if not(cs_littlesize in aktglobalswitches) then
  520. cg.t_times:=cg.t_times div 2;
  521. if cg.t_times=0 then
  522. cg.t_times:=1;
  523. { if path }
  524. if assigned(right) then
  525. begin
  526. firstpass(right);
  527. if registersint<right.registersint then
  528. registersint:=right.registersint;
  529. if registersfpu<right.registersfpu then
  530. registersfpu:=right.registersfpu;
  531. {$ifdef SUPPORT_MMX}
  532. if registersmmx<right.registersmmx then
  533. registersmmx:=right.registersmmx;
  534. {$endif SUPPORT_MMX}
  535. end;
  536. { else path }
  537. if assigned(t1) then
  538. begin
  539. firstpass(t1);
  540. if registersint<t1.registersint then
  541. registersint:=t1.registersint;
  542. if registersfpu<t1.registersfpu then
  543. registersfpu:=t1.registersfpu;
  544. {$ifdef SUPPORT_MMX}
  545. if registersmmx<t1.registersmmx then
  546. registersmmx:=t1.registersmmx;
  547. {$endif SUPPORT_MMX}
  548. end;
  549. { leave if we've got an error in one of the paths }
  550. if codegenerror then
  551. exit;
  552. cg.t_times:=old_t_times;
  553. end;
  554. {*****************************************************************************
  555. TFORNODE
  556. *****************************************************************************}
  557. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  558. begin
  559. inherited create(forn,l,r,_t1,_t2);
  560. if back then
  561. include(loopflags,lnf_backward);
  562. include(loopflags,lnf_testatbegin);
  563. end;
  564. procedure Tfornode.loop_var_access(not_type:Tnotification_flag;
  565. symbol:Tsym);
  566. begin
  567. {If there is a read access, the value of the loop counter is important;
  568. at the end of the loop the loop variable should contain the value it
  569. had in the last iteration.}
  570. if not_type=vn_onwrite then
  571. begin
  572. writeln('Loopvar does not matter on exit');
  573. end
  574. else
  575. begin
  576. exclude(loopflags,lnf_dont_mind_loopvar_on_exit);
  577. writeln('Loopvar does matter on exit');
  578. end;
  579. Tabstractvarsym(symbol).unregister_notification(loopvar_notid);
  580. end;
  581. function tfornode.det_resulttype:tnode;
  582. begin
  583. result:=nil;
  584. resulttype:=voidtype;
  585. { process the loopvar, from and to, varstates are already set }
  586. resulttypepass(left);
  587. resulttypepass(right);
  588. resulttypepass(t1);
  589. {Can we spare the first comparision?}
  590. if (t1.nodetype=ordconstn) and
  591. (right.nodetype=ordconstn) and
  592. (
  593. (
  594. (lnf_backward in loopflags) and
  595. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  596. ) or
  597. (
  598. not(lnf_backward in loopflags) and
  599. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  600. )
  601. ) then
  602. exclude(loopflags,lnf_testatbegin);
  603. { Make sure that the loop var and the
  604. from and to values are compatible types }
  605. inserttypeconv(right,left.resulttype);
  606. inserttypeconv(t1,left.resulttype);
  607. if assigned(t2) then
  608. resulttypepass(t2);
  609. end;
  610. function tfornode.pass_1 : tnode;
  611. var
  612. old_t_times : longint;
  613. begin
  614. result:=nil;
  615. expectloc:=LOC_VOID;
  616. firstpass(left);
  617. if left.registersint>registersint then
  618. registersint:=left.registersint;
  619. if left.registersfpu>registersfpu then
  620. registersfpu:=left.registersfpu;
  621. {$ifdef SUPPORT_MMX}
  622. if left.registersmmx>registersmmx then
  623. registersmmx:=left.registersmmx;
  624. {$endif SUPPORT_MMX}
  625. firstpass(right);
  626. if right.registersint>registersint then
  627. registersint:=right.registersint;
  628. if right.registersfpu>registersfpu then
  629. registersfpu:=right.registersfpu;
  630. {$ifdef SUPPORT_MMX}
  631. if right.registersmmx>registersmmx then
  632. registersmmx:=right.registersmmx;
  633. {$endif SUPPORT_MMX}
  634. firstpass(t1);
  635. if t1.registersint>registersint then
  636. registersint:=t1.registersint;
  637. if t1.registersfpu>registersfpu then
  638. registersfpu:=t1.registersfpu;
  639. {$ifdef SUPPORT_MMX}
  640. if t1.registersmmx>registersmmx then
  641. registersmmx:=t1.registersmmx;
  642. {$endif SUPPORT_MMX}
  643. if assigned(t2) then
  644. begin
  645. { Calc register weight }
  646. old_t_times:=cg.t_times;
  647. if not(cs_littlesize in aktglobalswitches) then
  648. cg.t_times:=cg.t_times*8;
  649. firstpass(t2);
  650. if codegenerror then
  651. exit;
  652. if t2.registersint>registersint then
  653. registersint:=t2.registersint;
  654. if t2.registersfpu>registersfpu then
  655. registersfpu:=t2.registersfpu;
  656. {$ifdef SUPPORT_MMX}
  657. if t2.registersmmx>registersmmx then
  658. registersmmx:=t2.registersmmx;
  659. {$endif SUPPORT_MMX}
  660. cg.t_times:=old_t_times;
  661. end;
  662. { we need at least one register for comparisons PM }
  663. if registersint=0 then
  664. inc(registersint);
  665. end;
  666. {*****************************************************************************
  667. TEXITNODE
  668. *****************************************************************************}
  669. constructor texitnode.create(l:tnode);
  670. begin
  671. inherited create(exitn,l);
  672. end;
  673. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  674. begin
  675. inherited ppuload(t,ppufile);
  676. end;
  677. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  678. begin
  679. inherited ppuwrite(ppufile);
  680. end;
  681. function texitnode.det_resulttype:tnode;
  682. begin
  683. result:=nil;
  684. if assigned(left) then
  685. begin
  686. { add assignment to funcretsym }
  687. inserttypeconv(left,current_procinfo.procdef.rettype);
  688. left:=cassignmentnode.create(
  689. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  690. left);
  691. resulttypepass(left);
  692. set_varstate(left,vs_used,[vsf_must_be_valid]);
  693. end;
  694. resulttype:=voidtype;
  695. end;
  696. function texitnode.pass_1 : tnode;
  697. begin
  698. result:=nil;
  699. expectloc:=LOC_VOID;
  700. if assigned(left) then
  701. begin
  702. firstpass(left);
  703. if codegenerror then
  704. exit;
  705. registersint:=left.registersint;
  706. registersfpu:=left.registersfpu;
  707. {$ifdef SUPPORT_MMX}
  708. registersmmx:=left.registersmmx;
  709. {$endif SUPPORT_MMX}
  710. end;
  711. end;
  712. {*****************************************************************************
  713. TBREAKNODE
  714. *****************************************************************************}
  715. constructor tbreaknode.create;
  716. begin
  717. inherited create(breakn);
  718. end;
  719. function tbreaknode.det_resulttype:tnode;
  720. begin
  721. result:=nil;
  722. resulttype:=voidtype;
  723. end;
  724. function tbreaknode.pass_1 : tnode;
  725. begin
  726. result:=nil;
  727. expectloc:=LOC_VOID;
  728. end;
  729. {*****************************************************************************
  730. TCONTINUENODE
  731. *****************************************************************************}
  732. constructor tcontinuenode.create;
  733. begin
  734. inherited create(continuen);
  735. end;
  736. function tcontinuenode.det_resulttype:tnode;
  737. begin
  738. result:=nil;
  739. resulttype:=voidtype;
  740. end;
  741. function tcontinuenode.pass_1 : tnode;
  742. begin
  743. result:=nil;
  744. expectloc:=LOC_VOID;
  745. end;
  746. {*****************************************************************************
  747. TGOTONODE
  748. *****************************************************************************}
  749. constructor tgotonode.create(p : tlabelsym);
  750. begin
  751. inherited create(goton);
  752. exceptionblock:=aktexceptblock;
  753. labsym:=p;
  754. end;
  755. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  756. begin
  757. inherited ppuload(t,ppufile);
  758. ppufile.getderef(labsymderef);
  759. exceptionblock:=ppufile.getbyte;
  760. end;
  761. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  762. begin
  763. inherited ppuwrite(ppufile);
  764. ppufile.putderef(labsymderef);
  765. ppufile.putbyte(exceptionblock);
  766. end;
  767. procedure tgotonode.buildderefimpl;
  768. begin
  769. inherited buildderefimpl;
  770. labsymderef.build(labsym);
  771. end;
  772. procedure tgotonode.derefimpl;
  773. begin
  774. inherited derefimpl;
  775. labsym:=tlabelsym(labsymderef.resolve);
  776. end;
  777. function tgotonode.det_resulttype:tnode;
  778. begin
  779. result:=nil;
  780. resulttype:=voidtype;
  781. end;
  782. function tgotonode.pass_1 : tnode;
  783. begin
  784. result:=nil;
  785. expectloc:=LOC_VOID;
  786. { check if }
  787. if assigned(labsym) and
  788. assigned(labsym.code) and
  789. (exceptionblock<>tlabelnode(labsym.code).exceptionblock) then
  790. CGMessage(cg_e_goto_inout_of_exception_block);
  791. end;
  792. function tgotonode.getcopy : tnode;
  793. var
  794. p : tgotonode;
  795. begin
  796. p:=tgotonode(inherited getcopy);
  797. p.labsym:=labsym;
  798. p.exceptionblock:=exceptionblock;
  799. result:=p;
  800. end;
  801. function tgotonode.docompare(p: tnode): boolean;
  802. begin
  803. docompare := false;
  804. end;
  805. {*****************************************************************************
  806. TLABELNODE
  807. *****************************************************************************}
  808. constructor tlabelnode.createcase(p : tasmlabel;l:tnode);
  809. begin
  810. inherited create(labeln,l);
  811. { it shouldn't be possible to jump to case labels using goto }
  812. exceptionblock:=-1;
  813. labsym:=nil;
  814. labelnr:=p;
  815. end;
  816. constructor tlabelnode.create(p : tlabelsym;l:tnode);
  817. begin
  818. inherited create(labeln,l);
  819. exceptionblock:=aktexceptblock;
  820. labsym:=p;
  821. labelnr:=p.lab;
  822. { save the current labelnode in the labelsym }
  823. p.code:=self;
  824. end;
  825. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  826. begin
  827. inherited ppuload(t,ppufile);
  828. ppufile.getderef(labsymderef);
  829. labelnr:=tasmlabel(ppufile.getasmsymbol);
  830. exceptionblock:=ppufile.getbyte;
  831. end;
  832. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  833. begin
  834. inherited ppuwrite(ppufile);
  835. ppufile.putderef(labsymderef);
  836. ppufile.putasmsymbol(labelnr);
  837. ppufile.putbyte(exceptionblock);
  838. end;
  839. procedure tlabelnode.buildderefimpl;
  840. begin
  841. inherited buildderefimpl;
  842. labsymderef.build(labsym);
  843. end;
  844. procedure tlabelnode.derefimpl;
  845. begin
  846. inherited derefimpl;
  847. labsym:=tlabelsym(labsymderef.resolve);
  848. objectlibrary.derefasmsymbol(tasmsymbol(labelnr));
  849. end;
  850. function tlabelnode.det_resulttype:tnode;
  851. begin
  852. result:=nil;
  853. { left could still be unassigned }
  854. if assigned(left) then
  855. resulttypepass(left);
  856. resulttype:=voidtype;
  857. end;
  858. function tlabelnode.pass_1 : tnode;
  859. begin
  860. result:=nil;
  861. expectloc:=LOC_VOID;
  862. if assigned(left) then
  863. begin
  864. firstpass(left);
  865. registersint:=left.registersint;
  866. registersfpu:=left.registersfpu;
  867. {$ifdef SUPPORT_MMX}
  868. registersmmx:=left.registersmmx;
  869. {$endif SUPPORT_MMX}
  870. end;
  871. end;
  872. function tlabelnode.getcopy : tnode;
  873. var
  874. p : tlabelnode;
  875. begin
  876. p:=tlabelnode(inherited getcopy);
  877. p.labelnr:=labelnr;
  878. p.exceptionblock:=exceptionblock;
  879. p.labsym:=labsym;
  880. result:=p;
  881. end;
  882. function tlabelnode.docompare(p: tnode): boolean;
  883. begin
  884. docompare := false;
  885. end;
  886. {*****************************************************************************
  887. TRAISENODE
  888. *****************************************************************************}
  889. constructor traisenode.create(l,taddr,tframe:tnode);
  890. begin
  891. inherited create(raisen,l,taddr);
  892. frametree:=tframe;
  893. end;
  894. constructor traisenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  895. begin
  896. inherited ppuload(t,ppufile);
  897. frametree:=ppuloadnode(ppufile);
  898. end;
  899. procedure traisenode.ppuwrite(ppufile:tcompilerppufile);
  900. begin
  901. inherited ppuwrite(ppufile);
  902. ppuwritenode(ppufile,frametree);
  903. end;
  904. procedure traisenode.buildderefimpl;
  905. begin
  906. inherited buildderefimpl;
  907. if assigned(frametree) then
  908. frametree.buildderefimpl;
  909. end;
  910. procedure traisenode.derefimpl;
  911. begin
  912. inherited derefimpl;
  913. if assigned(frametree) then
  914. frametree.derefimpl;
  915. end;
  916. function traisenode.getcopy : tnode;
  917. var
  918. n : traisenode;
  919. begin
  920. n:=traisenode(inherited getcopy);
  921. if assigned(frametree) then
  922. n.frametree:=frametree.getcopy
  923. else
  924. n.frametree:=nil;
  925. getcopy:=n;
  926. end;
  927. procedure traisenode.insertintolist(l : tnodelist);
  928. begin
  929. end;
  930. function traisenode.det_resulttype:tnode;
  931. begin
  932. result:=nil;
  933. resulttype:=voidtype;
  934. if assigned(left) then
  935. begin
  936. { first para must be a _class_ }
  937. resulttypepass(left);
  938. set_varstate(left,vs_used,[vsf_must_be_valid]);
  939. if codegenerror then
  940. exit;
  941. if not(is_class(left.resulttype.def)) then
  942. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  943. { insert needed typeconvs for addr,frame }
  944. if assigned(right) then
  945. begin
  946. { addr }
  947. resulttypepass(right);
  948. inserttypeconv(right,voidpointertype);
  949. { frame }
  950. if assigned(frametree) then
  951. begin
  952. resulttypepass(frametree);
  953. inserttypeconv(frametree,voidpointertype);
  954. end;
  955. end;
  956. end;
  957. end;
  958. function traisenode.pass_1 : tnode;
  959. begin
  960. result:=nil;
  961. include(current_procinfo.flags,pi_do_call);
  962. expectloc:=LOC_VOID;
  963. if assigned(left) then
  964. begin
  965. { first para must be a _class_ }
  966. firstpass(left);
  967. { insert needed typeconvs for addr,frame }
  968. if assigned(right) then
  969. begin
  970. { addr }
  971. firstpass(right);
  972. { frame }
  973. if assigned(frametree) then
  974. firstpass(frametree);
  975. end;
  976. left_right_max;
  977. end;
  978. end;
  979. function traisenode.docompare(p: tnode): boolean;
  980. begin
  981. docompare := false;
  982. end;
  983. {*****************************************************************************
  984. TTRYEXCEPTNODE
  985. *****************************************************************************}
  986. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  987. begin
  988. inherited create(tryexceptn,l,r,_t1,nil);
  989. end;
  990. function ttryexceptnode.det_resulttype:tnode;
  991. begin
  992. result:=nil;
  993. resulttypepass(left);
  994. { on statements }
  995. if assigned(right) then
  996. resulttypepass(right);
  997. { else block }
  998. if assigned(t1) then
  999. resulttypepass(t1);
  1000. resulttype:=voidtype;
  1001. end;
  1002. function ttryexceptnode.pass_1 : tnode;
  1003. begin
  1004. result:=nil;
  1005. include(current_procinfo.flags,pi_do_call);
  1006. expectloc:=LOC_VOID;
  1007. firstpass(left);
  1008. { on statements }
  1009. if assigned(right) then
  1010. begin
  1011. firstpass(right);
  1012. registersint:=max(registersint,right.registersint);
  1013. registersfpu:=max(registersfpu,right.registersfpu);
  1014. {$ifdef SUPPORT_MMX}
  1015. registersmmx:=max(registersmmx,right.registersmmx);
  1016. {$endif SUPPORT_MMX}
  1017. end;
  1018. { else block }
  1019. if assigned(t1) then
  1020. begin
  1021. firstpass(t1);
  1022. registersint:=max(registersint,t1.registersint);
  1023. registersfpu:=max(registersfpu,t1.registersfpu);
  1024. {$ifdef SUPPORT_MMX}
  1025. registersmmx:=max(registersmmx,t1.registersmmx);
  1026. {$endif SUPPORT_MMX}
  1027. end;
  1028. end;
  1029. {*****************************************************************************
  1030. TTRYFINALLYNODE
  1031. *****************************************************************************}
  1032. constructor ttryfinallynode.create(l,r:tnode);
  1033. begin
  1034. inherited create(tryfinallyn,l,r,nil,nil);
  1035. implicitframe:=false;
  1036. end;
  1037. constructor ttryfinallynode.create_implicit(l,r,_t1:tnode);
  1038. begin
  1039. inherited create(tryfinallyn,l,r,_t1,nil);
  1040. implicitframe:=true;
  1041. end;
  1042. function ttryfinallynode.det_resulttype:tnode;
  1043. begin
  1044. result:=nil;
  1045. include(current_procinfo.flags,pi_do_call);
  1046. resulttype:=voidtype;
  1047. resulttypepass(left);
  1048. set_varstate(left,vs_used,[vsf_must_be_valid]);
  1049. resulttypepass(right);
  1050. set_varstate(right,vs_used,[vsf_must_be_valid]);
  1051. { special finally block only executed when there was an exception }
  1052. if assigned(t1) then
  1053. begin
  1054. resulttypepass(t1);
  1055. set_varstate(t1,vs_used,[vsf_must_be_valid]);
  1056. end;
  1057. end;
  1058. function ttryfinallynode.pass_1 : tnode;
  1059. begin
  1060. result:=nil;
  1061. expectloc:=LOC_VOID;
  1062. firstpass(left);
  1063. firstpass(right);
  1064. left_right_max;
  1065. if assigned(t1) then
  1066. begin
  1067. firstpass(t1);
  1068. registersint:=max(registersint,t1.registersint);
  1069. registersfpu:=max(registersfpu,t1.registersfpu);
  1070. {$ifdef SUPPORT_MMX}
  1071. registersmmx:=max(registersmmx,t1.registersmmx);
  1072. {$endif SUPPORT_MMX}
  1073. end;
  1074. end;
  1075. {*****************************************************************************
  1076. TONNODE
  1077. *****************************************************************************}
  1078. constructor tonnode.create(l,r:tnode);
  1079. begin
  1080. inherited create(onn,l,r);
  1081. exceptsymtable:=nil;
  1082. excepttype:=nil;
  1083. end;
  1084. destructor tonnode.destroy;
  1085. begin
  1086. { copied nodes don't need to release the symtable }
  1087. if assigned(exceptsymtable) then
  1088. exceptsymtable.free;
  1089. inherited destroy;
  1090. end;
  1091. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1092. begin
  1093. inherited ppuload(t,ppufile);
  1094. exceptsymtable:=nil;
  1095. excepttype:=nil;
  1096. end;
  1097. function tonnode.getcopy : tnode;
  1098. var
  1099. n : tonnode;
  1100. begin
  1101. n:=tonnode(inherited getcopy);
  1102. n.exceptsymtable:=exceptsymtable.getcopy;
  1103. n.excepttype:=excepttype;
  1104. result:=n;
  1105. end;
  1106. function tonnode.det_resulttype:tnode;
  1107. begin
  1108. result:=nil;
  1109. resulttype:=voidtype;
  1110. if not(is_class(excepttype)) then
  1111. CGMessage1(type_e_class_type_expected,excepttype.typename);
  1112. if assigned(left) then
  1113. resulttypepass(left);
  1114. if assigned(right) then
  1115. resulttypepass(right);
  1116. end;
  1117. function tonnode.pass_1 : tnode;
  1118. begin
  1119. result:=nil;
  1120. include(current_procinfo.flags,pi_do_call);
  1121. expectloc:=LOC_VOID;
  1122. registersint:=0;
  1123. registersfpu:=0;
  1124. {$ifdef SUPPORT_MMX}
  1125. registersmmx:=0;
  1126. {$endif SUPPORT_MMX}
  1127. if assigned(left) then
  1128. begin
  1129. firstpass(left);
  1130. registersint:=left.registersint;
  1131. registersfpu:=left.registersfpu;
  1132. {$ifdef SUPPORT_MMX}
  1133. registersmmx:=left.registersmmx;
  1134. {$endif SUPPORT_MMX}
  1135. end;
  1136. if assigned(right) then
  1137. begin
  1138. firstpass(right);
  1139. registersint:=max(registersint,right.registersint);
  1140. registersfpu:=max(registersfpu,right.registersfpu);
  1141. {$ifdef SUPPORT_MMX}
  1142. registersmmx:=max(registersmmx,right.registersmmx);
  1143. {$endif SUPPORT_MMX}
  1144. end;
  1145. end;
  1146. function tonnode.docompare(p: tnode): boolean;
  1147. begin
  1148. docompare := false;
  1149. end;
  1150. begin
  1151. cwhilerepeatnode:=twhilerepeatnode;
  1152. cifnode:=tifnode;
  1153. cfornode:=tfornode;
  1154. cexitnode:=texitnode;
  1155. cgotonode:=tgotonode;
  1156. clabelnode:=tlabelnode;
  1157. craisenode:=traisenode;
  1158. ctryexceptnode:=ttryexceptnode;
  1159. ctryfinallynode:=ttryfinallynode;
  1160. connode:=tonnode;
  1161. end.
  1162. {
  1163. $Log$
  1164. Revision 1.112 2005-03-25 22:20:19 peter
  1165. * add hint when passing an uninitialized variable to a var parameter
  1166. Revision 1.111 2005/03/24 23:06:43 peter
  1167. * don't remove repeat until node in repeat until true;
  1168. Revision 1.110 2005/02/23 20:38:09 florian
  1169. + variants can be used as cond. expr. in if, while, repeat ... until statements
  1170. Revision 1.109 2005/02/14 17:13:06 peter
  1171. * truncate log
  1172. Revision 1.108 2005/01/31 20:23:53 peter
  1173. * set varstate before parsing the instruction block in for statements
  1174. Revision 1.107 2005/01/31 16:16:21 peter
  1175. * for-node cleanup, checking for uninitialzed from and to values
  1176. is now supported
  1177. Revision 1.106 2005/01/16 14:44:03 peter
  1178. * fix unreachable code check for repeat loop
  1179. Revision 1.105 2005/01/16 10:50:32 peter
  1180. * give warning for unreachable code in while/if statements
  1181. Revision 1.104 2005/01/03 17:55:57 florian
  1182. + first batch of patches to support tdef.getcopy fully
  1183. }