nflw.pas 40 KB

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