nflw.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  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,_t1: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. { for compatibilty }
  176. function genloopnode(t : tnodetype;l,r,n1 : tnode;back : boolean) : tnode;
  177. var
  178. cwhilerepeatnode : twhilerepeatnodeclass;
  179. cifnode : tifnodeclass;
  180. cfornode : tfornodeclass;
  181. cexitnode : texitnodeclass;
  182. cbreaknode : tbreaknodeclass;
  183. ccontinuenode : tcontinuenodeclass;
  184. cgotonode : tgotonodeclass;
  185. clabelnode : tlabelnodeclass;
  186. craisenode : traisenodeclass;
  187. ctryexceptnode : ttryexceptnodeclass;
  188. ctryfinallynode : ttryfinallynodeclass;
  189. connode : tonnodeclass;
  190. implementation
  191. uses
  192. globtype,systems,
  193. cutils,verbose,globals,
  194. symconst,paramgr,defcmp,defutil,htypechk,pass_1,
  195. ncal,nadd,ncon,nmem,nld,ncnv,nbas,cgobj,
  196. {$ifdef state_tracking}
  197. nstate,
  198. {$endif}
  199. cgbase,procinfo
  200. ;
  201. function genloopnode(t : tnodetype;l,r,n1 : tnode;back : boolean) : tnode;
  202. var
  203. p : tnode;
  204. begin
  205. case t of
  206. ifn:
  207. p:=cifnode.create(l,r,n1);
  208. whilerepeatn:
  209. if back then
  210. {Repeat until.}
  211. p:=cwhilerepeatnode.create(l,r,n1,false,true)
  212. else
  213. {While do.}
  214. p:=cwhilerepeatnode.create(l,r,n1,true,false);
  215. forn:
  216. p:=cfornode.create(l,r,n1,nil,back);
  217. end;
  218. resulttypepass(p);
  219. genloopnode:=p;
  220. end;
  221. {****************************************************************************
  222. TLOOPNODE
  223. *****************************************************************************}
  224. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  225. begin
  226. inherited create(tt,l,r);
  227. t1:=_t1;
  228. t2:=_t2;
  229. fileinfo:=l.fileinfo;
  230. end;
  231. destructor tloopnode.destroy;
  232. begin
  233. t1.free;
  234. t2.free;
  235. inherited destroy;
  236. end;
  237. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  238. begin
  239. inherited ppuload(t,ppufile);
  240. t1:=ppuloadnode(ppufile);
  241. t2:=ppuloadnode(ppufile);
  242. end;
  243. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  244. begin
  245. inherited ppuwrite(ppufile);
  246. ppuwritenode(ppufile,t1);
  247. ppuwritenode(ppufile,t2);
  248. end;
  249. procedure tloopnode.buildderefimpl;
  250. begin
  251. inherited buildderefimpl;
  252. if assigned(t1) then
  253. t1.buildderefimpl;
  254. if assigned(t2) then
  255. t2.buildderefimpl;
  256. end;
  257. procedure tloopnode.derefimpl;
  258. begin
  259. inherited derefimpl;
  260. if assigned(t1) then
  261. t1.derefimpl;
  262. if assigned(t2) then
  263. t2.derefimpl;
  264. end;
  265. function tloopnode.getcopy : tnode;
  266. var
  267. p : tloopnode;
  268. begin
  269. p:=tloopnode(inherited getcopy);
  270. if assigned(t1) then
  271. p.t1:=t1.getcopy
  272. else
  273. p.t1:=nil;
  274. if assigned(t2) then
  275. p.t2:=t2.getcopy
  276. else
  277. p.t2:=nil;
  278. p.loopflags:=loopflags;
  279. getcopy:=p;
  280. end;
  281. procedure tloopnode.insertintolist(l : tnodelist);
  282. begin
  283. end;
  284. procedure tloopnode.printnodetree(var t:text);
  285. begin
  286. write(t,printnodeindention,'(');
  287. printnodeindent;
  288. printnodeinfo(t);
  289. writeln(t);
  290. printnode(t,left);
  291. printnode(t,right);
  292. printnode(t,t1);
  293. printnode(t,t2);
  294. printnodeunindent;
  295. writeln(t,printnodeindention,')');
  296. end;
  297. function tloopnode.docompare(p: tnode): boolean;
  298. begin
  299. docompare :=
  300. inherited docompare(p) and
  301. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  302. t1.isequal(tloopnode(p).t1) and
  303. t2.isequal(tloopnode(p).t2);
  304. end;
  305. {****************************************************************************
  306. TWHILEREPEATNODE
  307. *****************************************************************************}
  308. constructor Twhilerepeatnode.create(l,r,_t1:Tnode;tab,cn:boolean);
  309. begin
  310. inherited create(whilerepeatn,l,r,_t1,nil);
  311. if tab then
  312. include(loopflags, lnf_testatbegin);
  313. if cn then
  314. include(loopflags,lnf_checknegate);
  315. end;
  316. function twhilerepeatnode.det_resulttype:tnode;
  317. var
  318. t:Tunarynode;
  319. begin
  320. result:=nil;
  321. resulttype:=voidtype;
  322. resulttypepass(left);
  323. {A not node can be removed.}
  324. if left.nodetype=notn then
  325. begin
  326. t:=Tunarynode(left);
  327. left:=Tunarynode(left).left;
  328. t.left:=nil;
  329. t.destroy;
  330. {Symdif operator, in case you are wondering:}
  331. loopflags:=loopflags >< [lnf_checknegate];
  332. end;
  333. { loop instruction }
  334. if assigned(right) then
  335. resulttypepass(right);
  336. set_varstate(left,vs_used,true);
  337. if codegenerror then
  338. exit;
  339. if not is_boolean(left.resulttype.def) then
  340. begin
  341. CGMessage1(type_e_boolean_expr_expected,left.resulttype.def.typename);
  342. exit;
  343. end;
  344. end;
  345. function twhilerepeatnode.pass_1 : tnode;
  346. var
  347. old_t_times : longint;
  348. begin
  349. result:=nil;
  350. expectloc:=LOC_VOID;
  351. old_t_times:=cg.t_times;
  352. { calc register weight }
  353. if not(cs_littlesize in aktglobalswitches ) then
  354. cg.t_times:=cg.t_times*8;
  355. firstpass(left);
  356. if codegenerror then
  357. exit;
  358. registersint:=left.registersint;
  359. registersfpu:=left.registersfpu;
  360. {$ifdef SUPPORT_MMX}
  361. registersmmx:=left.registersmmx;
  362. {$endif SUPPORT_MMX}
  363. { loop instruction }
  364. if assigned(right) then
  365. begin
  366. firstpass(right);
  367. if codegenerror then
  368. exit;
  369. if registersint<right.registersint then
  370. registersint:=right.registersint;
  371. if registersfpu<right.registersfpu then
  372. registersfpu:=right.registersfpu;
  373. {$ifdef SUPPORT_MMX}
  374. if registersmmx<right.registersmmx then
  375. registersmmx:=right.registersmmx;
  376. {$endif SUPPORT_MMX}
  377. end;
  378. cg.t_times:=old_t_times;
  379. end;
  380. {$ifdef state_tracking}
  381. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  382. var condition:Tnode;
  383. code:Tnode;
  384. done:boolean;
  385. value:boolean;
  386. change:boolean;
  387. firsttest:boolean;
  388. factval:Tnode;
  389. begin
  390. track_state_pass:=false;
  391. done:=false;
  392. firsttest:=true;
  393. {For repeat until statements, first do a pass through the code.}
  394. if not(lnf_testatbegin in flags) then
  395. begin
  396. code:=right.getcopy;
  397. if code.track_state_pass(exec_known) then
  398. track_state_pass:=true;
  399. code.destroy;
  400. end;
  401. repeat
  402. condition:=left.getcopy;
  403. code:=right.getcopy;
  404. change:=condition.track_state_pass(exec_known);
  405. factval:=aktstate.find_fact(left);
  406. if factval<>nil then
  407. begin
  408. condition.destroy;
  409. condition:=factval.getcopy;
  410. change:=true;
  411. end;
  412. if change then
  413. begin
  414. track_state_pass:=true;
  415. {Force new resulttype pass.}
  416. condition.resulttype.def:=nil;
  417. do_resulttypepass(condition);
  418. end;
  419. if is_constboolnode(condition) then
  420. begin
  421. {Try to turn a while loop into a repeat loop.}
  422. if firsttest then
  423. exclude(flags,testatbegin);
  424. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  425. if value then
  426. begin
  427. if code.track_state_pass(exec_known) then
  428. track_state_pass:=true;
  429. end
  430. else
  431. done:=true;
  432. end
  433. else
  434. begin
  435. {Remove any modified variables from the state.}
  436. code.track_state_pass(false);
  437. done:=true;
  438. end;
  439. code.destroy;
  440. condition.destroy;
  441. firsttest:=false;
  442. until done;
  443. {The loop condition is also known, for example:
  444. while i<10 do
  445. begin
  446. ...
  447. end;
  448. When the loop is done, we do know that i<10 = false.
  449. }
  450. condition:=left.getcopy;
  451. if condition.track_state_pass(exec_known) then
  452. begin
  453. track_state_pass:=true;
  454. {Force new resulttype pass.}
  455. condition.resulttype.def:=nil;
  456. do_resulttypepass(condition);
  457. end;
  458. if not is_constboolnode(condition) then
  459. aktstate.store_fact(condition,
  460. cordconstnode.create(byte(checknegate),booltype,true))
  461. else
  462. condition.destroy;
  463. end;
  464. {$endif}
  465. {*****************************************************************************
  466. TIFNODE
  467. *****************************************************************************}
  468. constructor tifnode.create(l,r,_t1 : tnode);
  469. begin
  470. inherited create(ifn,l,r,_t1,nil);
  471. end;
  472. function tifnode.det_resulttype:tnode;
  473. begin
  474. result:=nil;
  475. resulttype:=voidtype;
  476. resulttypepass(left);
  477. { if path }
  478. if assigned(right) then
  479. resulttypepass(right);
  480. { else path }
  481. if assigned(t1) then
  482. resulttypepass(t1);
  483. set_varstate(left,vs_used,true);
  484. if codegenerror then
  485. exit;
  486. if not is_boolean(left.resulttype.def) then
  487. Message1(type_e_boolean_expr_expected,left.resulttype.def.typename);
  488. end;
  489. function tifnode.pass_1 : tnode;
  490. var
  491. old_t_times : longint;
  492. hp : tnode;
  493. begin
  494. result:=nil;
  495. expectloc:=LOC_VOID;
  496. old_t_times:=cg.t_times;
  497. firstpass(left);
  498. registersint:=left.registersint;
  499. registersfpu:=left.registersfpu;
  500. {$ifdef SUPPORT_MMX}
  501. registersmmx:=left.registersmmx;
  502. {$endif SUPPORT_MMX}
  503. { determines registers weigths }
  504. if not(cs_littlesize in aktglobalswitches) then
  505. cg.t_times:=cg.t_times div 2;
  506. if cg.t_times=0 then
  507. cg.t_times:=1;
  508. { if path }
  509. if assigned(right) then
  510. begin
  511. firstpass(right);
  512. if registersint<right.registersint then
  513. registersint:=right.registersint;
  514. if registersfpu<right.registersfpu then
  515. registersfpu:=right.registersfpu;
  516. {$ifdef SUPPORT_MMX}
  517. if registersmmx<right.registersmmx then
  518. registersmmx:=right.registersmmx;
  519. {$endif SUPPORT_MMX}
  520. end;
  521. { else path }
  522. if assigned(t1) then
  523. begin
  524. firstpass(t1);
  525. if registersint<t1.registersint then
  526. registersint:=t1.registersint;
  527. if registersfpu<t1.registersfpu then
  528. registersfpu:=t1.registersfpu;
  529. {$ifdef SUPPORT_MMX}
  530. if registersmmx<t1.registersmmx then
  531. registersmmx:=t1.registersmmx;
  532. {$endif SUPPORT_MMX}
  533. end;
  534. { leave if we've got an error in one of the paths }
  535. if codegenerror then
  536. exit;
  537. if left.nodetype=ordconstn then
  538. begin
  539. { optimize }
  540. if tordconstnode(left).value=1 then
  541. begin
  542. hp:=right;
  543. right:=nil;
  544. { we cannot set p to nil !!! }
  545. if assigned(hp) then
  546. result:=hp
  547. else
  548. result:=cnothingnode.create;
  549. end
  550. else
  551. begin
  552. hp:=t1;
  553. t1:=nil;
  554. { we cannot set p to nil !!! }
  555. if assigned(hp) then
  556. result:=hp
  557. else
  558. result:=cnothingnode.create;
  559. end;
  560. end;
  561. cg.t_times:=old_t_times;
  562. end;
  563. {*****************************************************************************
  564. TFORNODE
  565. *****************************************************************************}
  566. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  567. begin
  568. inherited create(forn,l,r,_t1,_t2);
  569. if back then
  570. include(loopflags,lnf_backward);
  571. include(loopflags,lnf_testatbegin);
  572. end;
  573. procedure Tfornode.loop_var_access(not_type:Tnotification_flag;
  574. symbol:Tsym);
  575. begin
  576. {If there is a read access, the value of the loop counter is important;
  577. at the end of the loop the loop variable should contain the value it
  578. had in the last iteration.}
  579. if not_type=vn_onwrite then
  580. begin
  581. writeln('Loopvar does not matter on exit');
  582. end
  583. else
  584. begin
  585. exclude(loopflags,lnf_dont_mind_loopvar_on_exit);
  586. writeln('Loopvar does matter on exit');
  587. end;
  588. Tabstractvarsym(symbol).unregister_notification(loopvar_notid);
  589. end;
  590. function tfornode.det_resulttype:tnode;
  591. var
  592. hp : tnode;
  593. begin
  594. result:=nil;
  595. resulttype:=voidtype;
  596. {Can we spare the first comparision?}
  597. if (right.nodetype=ordconstn) and
  598. (Tassignmentnode(left).right.nodetype=ordconstn) and
  599. (
  600. (
  601. (lnf_backward in loopflags) and
  602. (Tordconstnode(Tassignmentnode(left).right).value>=Tordconstnode(right).value)
  603. ) or
  604. (
  605. not(lnf_backward in loopflags) and
  606. (Tordconstnode(Tassignmentnode(left).right).value<=Tordconstnode(right).value)
  607. )
  608. ) then
  609. exclude(loopflags,lnf_testatbegin);
  610. { save counter var }
  611. t2:=tassignmentnode(left).left.getcopy;
  612. resulttypepass(left);
  613. set_varstate(left,vs_used,true);
  614. if assigned(t1) then
  615. begin
  616. resulttypepass(t1);
  617. if codegenerror then
  618. exit;
  619. end;
  620. { process count var }
  621. resulttypepass(t2);
  622. set_varstate(t2,vs_used,false);
  623. if codegenerror then
  624. exit;
  625. resulttypepass(right);
  626. set_varstate(right,vs_used,true);
  627. inserttypeconv(right,t2.resulttype);
  628. end;
  629. function tfornode.pass_1 : tnode;
  630. var
  631. old_t_times : longint;
  632. begin
  633. result:=nil;
  634. expectloc:=LOC_VOID;
  635. { Calc register weight }
  636. old_t_times:=cg.t_times;
  637. if not(cs_littlesize in aktglobalswitches) then
  638. cg.t_times:=cg.t_times*8;
  639. firstpass(left);
  640. if assigned(t1) then
  641. begin
  642. firstpass(t1);
  643. if codegenerror then
  644. exit;
  645. end;
  646. registersint:=t1.registersint;
  647. registersfpu:=t1.registersfpu;
  648. {$ifdef SUPPORT_MMX}
  649. registersmmx:=left.registersmmx;
  650. {$endif SUPPORT_MMX}
  651. if left.registersint>registersint then
  652. registersint:=left.registersint;
  653. if left.registersfpu>registersfpu then
  654. registersfpu:=left.registersfpu;
  655. {$ifdef SUPPORT_MMX}
  656. if left.registersmmx>registersmmx then
  657. registersmmx:=left.registersmmx;
  658. {$endif SUPPORT_MMX}
  659. { process count var }
  660. firstpass(t2);
  661. if codegenerror then
  662. exit;
  663. if t2.registersint>registersint then
  664. registersint:=t2.registersint;
  665. if t2.registersfpu>registersfpu then
  666. registersfpu:=t2.registersfpu;
  667. {$ifdef SUPPORT_MMX}
  668. if t2.registersmmx>registersmmx then
  669. registersmmx:=t2.registersmmx;
  670. {$endif SUPPORT_MMX}
  671. firstpass(right);
  672. {$ifdef loopvar_dont_mind}
  673. { Check count var, record fields are also allowed in tp7 }
  674. include(loopflags,lnf_dont_mind_loopvar_on_exit);
  675. hp:=t2;
  676. while (hp.nodetype=subscriptn) or
  677. ((hp.nodetype=vecn) and
  678. is_constintnode(tvecnode(hp).right)) do
  679. hp:=tunarynode(hp).left;
  680. if (hp.nodetype=loadn) and (Tloadnode(hp).symtableentry.typ=varsym) then
  681. loopvar_notid:=Tvarsym(Tloadnode(hp).symtableentry).
  682. register_notification([vn_onread,vn_onwrite],@loop_var_access);
  683. {$endif}
  684. if right.registersint>registersint then
  685. registersint:=right.registersint;
  686. if right.registersfpu>registersfpu then
  687. registersfpu:=right.registersfpu;
  688. {$ifdef SUPPORT_MMX}
  689. if right.registersmmx>registersmmx then
  690. registersmmx:=right.registersmmx;
  691. {$endif SUPPORT_MMX}
  692. { we need at least one register for comparisons PM }
  693. if registersint=0 then
  694. inc(registersint);
  695. cg.t_times:=old_t_times;
  696. end;
  697. {*****************************************************************************
  698. TEXITNODE
  699. *****************************************************************************}
  700. constructor texitnode.create(l:tnode);
  701. begin
  702. inherited create(exitn,l);
  703. end;
  704. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  705. begin
  706. inherited ppuload(t,ppufile);
  707. end;
  708. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  709. begin
  710. inherited ppuwrite(ppufile);
  711. end;
  712. function texitnode.det_resulttype:tnode;
  713. begin
  714. result:=nil;
  715. if assigned(left) then
  716. begin
  717. { add assignment to funcretsym }
  718. inserttypeconv(left,current_procinfo.procdef.rettype);
  719. left:=cassignmentnode.create(
  720. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  721. left);
  722. resulttypepass(left);
  723. set_varstate(left,vs_used,true);
  724. end;
  725. resulttype:=voidtype;
  726. end;
  727. function texitnode.pass_1 : tnode;
  728. begin
  729. result:=nil;
  730. expectloc:=LOC_VOID;
  731. if assigned(left) then
  732. begin
  733. firstpass(left);
  734. if codegenerror then
  735. exit;
  736. registersint:=left.registersint;
  737. registersfpu:=left.registersfpu;
  738. {$ifdef SUPPORT_MMX}
  739. registersmmx:=left.registersmmx;
  740. {$endif SUPPORT_MMX}
  741. end;
  742. end;
  743. {*****************************************************************************
  744. TBREAKNODE
  745. *****************************************************************************}
  746. constructor tbreaknode.create;
  747. begin
  748. inherited create(breakn);
  749. end;
  750. function tbreaknode.det_resulttype:tnode;
  751. begin
  752. result:=nil;
  753. resulttype:=voidtype;
  754. end;
  755. function tbreaknode.pass_1 : tnode;
  756. begin
  757. result:=nil;
  758. expectloc:=LOC_VOID;
  759. end;
  760. {*****************************************************************************
  761. TCONTINUENODE
  762. *****************************************************************************}
  763. constructor tcontinuenode.create;
  764. begin
  765. inherited create(continuen);
  766. end;
  767. function tcontinuenode.det_resulttype:tnode;
  768. begin
  769. result:=nil;
  770. resulttype:=voidtype;
  771. end;
  772. function tcontinuenode.pass_1 : tnode;
  773. begin
  774. result:=nil;
  775. expectloc:=LOC_VOID;
  776. end;
  777. {*****************************************************************************
  778. TGOTONODE
  779. *****************************************************************************}
  780. constructor tgotonode.create(p : tlabelsym);
  781. begin
  782. inherited create(goton);
  783. exceptionblock:=aktexceptblock;
  784. labsym:=p;
  785. end;
  786. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  787. begin
  788. inherited ppuload(t,ppufile);
  789. ppufile.getderef(labsymderef);
  790. exceptionblock:=ppufile.getbyte;
  791. end;
  792. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  793. begin
  794. inherited ppuwrite(ppufile);
  795. ppufile.putderef(labsymderef);
  796. ppufile.putbyte(exceptionblock);
  797. end;
  798. procedure tgotonode.buildderefimpl;
  799. begin
  800. inherited buildderefimpl;
  801. labsymderef.build(labsym);
  802. end;
  803. procedure tgotonode.derefimpl;
  804. begin
  805. inherited derefimpl;
  806. labsym:=tlabelsym(labsymderef.resolve);
  807. end;
  808. function tgotonode.det_resulttype:tnode;
  809. begin
  810. result:=nil;
  811. resulttype:=voidtype;
  812. end;
  813. function tgotonode.pass_1 : tnode;
  814. begin
  815. result:=nil;
  816. expectloc:=LOC_VOID;
  817. { check if }
  818. if assigned(labsym) and
  819. assigned(labsym.code) and
  820. (exceptionblock<>tlabelnode(labsym.code).exceptionblock) then
  821. CGMessage(cg_e_goto_inout_of_exception_block);
  822. end;
  823. function tgotonode.getcopy : tnode;
  824. var
  825. p : tgotonode;
  826. begin
  827. p:=tgotonode(inherited getcopy);
  828. p.labsym:=labsym;
  829. p.exceptionblock:=exceptionblock;
  830. result:=p;
  831. end;
  832. function tgotonode.docompare(p: tnode): boolean;
  833. begin
  834. docompare := false;
  835. end;
  836. {*****************************************************************************
  837. TLABELNODE
  838. *****************************************************************************}
  839. constructor tlabelnode.createcase(p : tasmlabel;l:tnode);
  840. begin
  841. inherited create(labeln,l);
  842. { it shouldn't be possible to jump to case labels using goto }
  843. exceptionblock:=-1;
  844. labsym:=nil;
  845. labelnr:=p;
  846. end;
  847. constructor tlabelnode.create(p : tlabelsym;l:tnode);
  848. begin
  849. inherited create(labeln,l);
  850. exceptionblock:=aktexceptblock;
  851. labsym:=p;
  852. labelnr:=p.lab;
  853. { save the current labelnode in the labelsym }
  854. p.code:=self;
  855. end;
  856. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  857. begin
  858. inherited ppuload(t,ppufile);
  859. ppufile.getderef(labsymderef);
  860. labelnr:=tasmlabel(ppufile.getasmsymbol);
  861. exceptionblock:=ppufile.getbyte;
  862. end;
  863. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  864. begin
  865. inherited ppuwrite(ppufile);
  866. ppufile.putderef(labsymderef);
  867. ppufile.putasmsymbol(labelnr);
  868. ppufile.putbyte(exceptionblock);
  869. end;
  870. procedure tlabelnode.buildderefimpl;
  871. begin
  872. inherited buildderefimpl;
  873. labsymderef.build(labsym);
  874. end;
  875. procedure tlabelnode.derefimpl;
  876. begin
  877. inherited derefimpl;
  878. labsym:=tlabelsym(labsymderef.resolve);
  879. objectlibrary.derefasmsymbol(tasmsymbol(labelnr));
  880. end;
  881. function tlabelnode.det_resulttype:tnode;
  882. begin
  883. result:=nil;
  884. { left could still be unassigned }
  885. if assigned(left) then
  886. resulttypepass(left);
  887. resulttype:=voidtype;
  888. end;
  889. function tlabelnode.pass_1 : tnode;
  890. begin
  891. result:=nil;
  892. expectloc:=LOC_VOID;
  893. if assigned(left) then
  894. begin
  895. firstpass(left);
  896. registersint:=left.registersint;
  897. registersfpu:=left.registersfpu;
  898. {$ifdef SUPPORT_MMX}
  899. registersmmx:=left.registersmmx;
  900. {$endif SUPPORT_MMX}
  901. end;
  902. end;
  903. function tlabelnode.getcopy : tnode;
  904. var
  905. p : tlabelnode;
  906. begin
  907. p:=tlabelnode(inherited getcopy);
  908. p.labelnr:=labelnr;
  909. p.exceptionblock:=exceptionblock;
  910. p.labsym:=labsym;
  911. result:=p;
  912. end;
  913. function tlabelnode.docompare(p: tnode): boolean;
  914. begin
  915. docompare := false;
  916. end;
  917. {*****************************************************************************
  918. TRAISENODE
  919. *****************************************************************************}
  920. constructor traisenode.create(l,taddr,tframe:tnode);
  921. begin
  922. inherited create(raisen,l,taddr);
  923. frametree:=tframe;
  924. end;
  925. constructor traisenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  926. begin
  927. inherited ppuload(t,ppufile);
  928. frametree:=ppuloadnode(ppufile);
  929. end;
  930. procedure traisenode.ppuwrite(ppufile:tcompilerppufile);
  931. begin
  932. inherited ppuwrite(ppufile);
  933. ppuwritenode(ppufile,frametree);
  934. end;
  935. procedure traisenode.buildderefimpl;
  936. begin
  937. inherited buildderefimpl;
  938. if assigned(frametree) then
  939. frametree.buildderefimpl;
  940. end;
  941. procedure traisenode.derefimpl;
  942. begin
  943. inherited derefimpl;
  944. if assigned(frametree) then
  945. frametree.derefimpl;
  946. end;
  947. function traisenode.getcopy : tnode;
  948. var
  949. n : traisenode;
  950. begin
  951. n:=traisenode(inherited getcopy);
  952. if assigned(frametree) then
  953. n.frametree:=frametree.getcopy
  954. else
  955. n.frametree:=nil;
  956. getcopy:=n;
  957. end;
  958. procedure traisenode.insertintolist(l : tnodelist);
  959. begin
  960. end;
  961. function traisenode.det_resulttype:tnode;
  962. begin
  963. result:=nil;
  964. resulttype:=voidtype;
  965. if assigned(left) then
  966. begin
  967. { first para must be a _class_ }
  968. resulttypepass(left);
  969. set_varstate(left,vs_used,true);
  970. if codegenerror then
  971. exit;
  972. if not(is_class(left.resulttype.def)) then
  973. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  974. { insert needed typeconvs for addr,frame }
  975. if assigned(right) then
  976. begin
  977. { addr }
  978. resulttypepass(right);
  979. inserttypeconv(right,voidpointertype);
  980. { frame }
  981. if assigned(frametree) then
  982. begin
  983. resulttypepass(frametree);
  984. inserttypeconv(frametree,voidpointertype);
  985. end;
  986. end;
  987. end;
  988. end;
  989. function traisenode.pass_1 : tnode;
  990. begin
  991. result:=nil;
  992. include(current_procinfo.flags,pi_do_call);
  993. expectloc:=LOC_VOID;
  994. if assigned(left) then
  995. begin
  996. { first para must be a _class_ }
  997. firstpass(left);
  998. { insert needed typeconvs for addr,frame }
  999. if assigned(right) then
  1000. begin
  1001. { addr }
  1002. firstpass(right);
  1003. { frame }
  1004. if assigned(frametree) then
  1005. firstpass(frametree);
  1006. end;
  1007. left_right_max;
  1008. end;
  1009. end;
  1010. function traisenode.docompare(p: tnode): boolean;
  1011. begin
  1012. docompare := false;
  1013. end;
  1014. {*****************************************************************************
  1015. TTRYEXCEPTNODE
  1016. *****************************************************************************}
  1017. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  1018. begin
  1019. inherited create(tryexceptn,l,r,_t1,nil);
  1020. end;
  1021. function ttryexceptnode.det_resulttype:tnode;
  1022. begin
  1023. result:=nil;
  1024. resulttypepass(left);
  1025. { on statements }
  1026. if assigned(right) then
  1027. resulttypepass(right);
  1028. { else block }
  1029. if assigned(t1) then
  1030. resulttypepass(t1);
  1031. resulttype:=voidtype;
  1032. end;
  1033. function ttryexceptnode.pass_1 : tnode;
  1034. begin
  1035. result:=nil;
  1036. include(current_procinfo.flags,pi_do_call);
  1037. expectloc:=LOC_VOID;
  1038. firstpass(left);
  1039. { on statements }
  1040. if assigned(right) then
  1041. begin
  1042. firstpass(right);
  1043. registersint:=max(registersint,right.registersint);
  1044. registersfpu:=max(registersfpu,right.registersfpu);
  1045. {$ifdef SUPPORT_MMX}
  1046. registersmmx:=max(registersmmx,right.registersmmx);
  1047. {$endif SUPPORT_MMX}
  1048. end;
  1049. { else block }
  1050. if assigned(t1) then
  1051. begin
  1052. firstpass(t1);
  1053. registersint:=max(registersint,t1.registersint);
  1054. registersfpu:=max(registersfpu,t1.registersfpu);
  1055. {$ifdef SUPPORT_MMX}
  1056. registersmmx:=max(registersmmx,t1.registersmmx);
  1057. {$endif SUPPORT_MMX}
  1058. end;
  1059. end;
  1060. {*****************************************************************************
  1061. TTRYFINALLYNODE
  1062. *****************************************************************************}
  1063. constructor ttryfinallynode.create(l,r:tnode);
  1064. begin
  1065. inherited create(tryfinallyn,l,r,nil,nil);
  1066. implicitframe:=false;
  1067. end;
  1068. constructor ttryfinallynode.create_implicit(l,r,_t1:tnode);
  1069. begin
  1070. inherited create(tryfinallyn,l,r,_t1,nil);
  1071. implicitframe:=true;
  1072. end;
  1073. function ttryfinallynode.det_resulttype:tnode;
  1074. begin
  1075. result:=nil;
  1076. include(current_procinfo.flags,pi_do_call);
  1077. resulttype:=voidtype;
  1078. resulttypepass(left);
  1079. set_varstate(left,vs_used,true);
  1080. resulttypepass(right);
  1081. set_varstate(right,vs_used,true);
  1082. { special finally block only executed when there was an exception }
  1083. if assigned(t1) then
  1084. begin
  1085. resulttypepass(t1);
  1086. set_varstate(t1,vs_used,true);
  1087. end;
  1088. end;
  1089. function ttryfinallynode.pass_1 : tnode;
  1090. begin
  1091. result:=nil;
  1092. expectloc:=LOC_VOID;
  1093. firstpass(left);
  1094. firstpass(right);
  1095. left_right_max;
  1096. if assigned(t1) then
  1097. begin
  1098. firstpass(t1);
  1099. registersint:=max(registersint,t1.registersint);
  1100. registersfpu:=max(registersfpu,t1.registersfpu);
  1101. {$ifdef SUPPORT_MMX}
  1102. registersmmx:=max(registersmmx,t1.registersmmx);
  1103. {$endif SUPPORT_MMX}
  1104. end;
  1105. end;
  1106. {*****************************************************************************
  1107. TONNODE
  1108. *****************************************************************************}
  1109. constructor tonnode.create(l,r:tnode);
  1110. begin
  1111. inherited create(onn,l,r);
  1112. exceptsymtable:=nil;
  1113. excepttype:=nil;
  1114. end;
  1115. destructor tonnode.destroy;
  1116. begin
  1117. { copied nodes don't need to release the symtable }
  1118. if assigned(exceptsymtable) then
  1119. exceptsymtable.free;
  1120. inherited destroy;
  1121. end;
  1122. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1123. begin
  1124. inherited ppuload(t,ppufile);
  1125. exceptsymtable:=nil;
  1126. excepttype:=nil;
  1127. end;
  1128. function tonnode.getcopy : tnode;
  1129. var
  1130. n : tonnode;
  1131. begin
  1132. n:=tonnode(inherited getcopy);
  1133. n.exceptsymtable:=exceptsymtable.getcopy;
  1134. n.excepttype:=excepttype;
  1135. result:=n;
  1136. end;
  1137. function tonnode.det_resulttype:tnode;
  1138. begin
  1139. result:=nil;
  1140. resulttype:=voidtype;
  1141. if not(is_class(excepttype)) then
  1142. CGMessage1(type_e_class_type_expected,excepttype.typename);
  1143. if assigned(left) then
  1144. resulttypepass(left);
  1145. if assigned(right) then
  1146. resulttypepass(right);
  1147. end;
  1148. function tonnode.pass_1 : tnode;
  1149. begin
  1150. result:=nil;
  1151. include(current_procinfo.flags,pi_do_call);
  1152. expectloc:=LOC_VOID;
  1153. registersint:=0;
  1154. registersfpu:=0;
  1155. {$ifdef SUPPORT_MMX}
  1156. registersmmx:=0;
  1157. {$endif SUPPORT_MMX}
  1158. if assigned(left) then
  1159. begin
  1160. firstpass(left);
  1161. registersint:=left.registersint;
  1162. registersfpu:=left.registersfpu;
  1163. {$ifdef SUPPORT_MMX}
  1164. registersmmx:=left.registersmmx;
  1165. {$endif SUPPORT_MMX}
  1166. end;
  1167. if assigned(right) then
  1168. begin
  1169. firstpass(right);
  1170. registersint:=max(registersint,right.registersint);
  1171. registersfpu:=max(registersfpu,right.registersfpu);
  1172. {$ifdef SUPPORT_MMX}
  1173. registersmmx:=max(registersmmx,right.registersmmx);
  1174. {$endif SUPPORT_MMX}
  1175. end;
  1176. end;
  1177. function tonnode.docompare(p: tnode): boolean;
  1178. begin
  1179. docompare := false;
  1180. end;
  1181. begin
  1182. cwhilerepeatnode:=twhilerepeatnode;
  1183. cifnode:=tifnode;
  1184. cfornode:=tfornode;
  1185. cexitnode:=texitnode;
  1186. cgotonode:=tgotonode;
  1187. clabelnode:=tlabelnode;
  1188. craisenode:=traisenode;
  1189. ctryexceptnode:=ttryexceptnode;
  1190. ctryfinallynode:=ttryfinallynode;
  1191. connode:=tonnode;
  1192. end.
  1193. {
  1194. $Log$
  1195. Revision 1.104 2005-01-03 17:55:57 florian
  1196. + first batch of patches to support tdef.getcopy fully
  1197. Revision 1.103 2004/12/26 16:22:01 peter
  1198. * fix lineinfo for with blocks
  1199. Revision 1.102 2004/11/08 22:09:59 peter
  1200. * tvarsym splitted
  1201. Revision 1.101 2004/10/15 09:14:17 mazen
  1202. - remove $IFDEF DELPHI and related code
  1203. - remove $IFDEF FPCPROCVAR and related code
  1204. Revision 1.100 2004/09/13 20:28:26 peter
  1205. * for loop variable assignment is not allowed anymore
  1206. Revision 1.99 2004/08/30 12:09:45 michael
  1207. + Patch from peter to fix bug 3272
  1208. Revision 1.98 2004/06/20 08:55:29 florian
  1209. * logs truncated
  1210. Revision 1.97 2004/06/16 20:07:08 florian
  1211. * dwarf branch merged
  1212. Revision 1.96 2004/05/23 15:04:13 peter
  1213. * remvoe writeln
  1214. Revision 1.95.2.1 2004/04/28 19:55:51 peter
  1215. * new warning for ordinal-pointer when size is different
  1216. * fixed some cg_e_ messages to the correct section type_e_ or parser_e_
  1217. Revision 1.95 2004/03/18 16:19:03 peter
  1218. * fixed operator overload allowing for pointer-string
  1219. * replaced some type_e_mismatch with more informational messages
  1220. Revision 1.94 2004/02/05 01:24:08 florian
  1221. * several fixes to compile x86-64 system
  1222. }