nbas.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. This unit implements some basic nodes
  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 nbas;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cpubase,
  23. aasmbase,aasmtai,aasmcpu,
  24. node,
  25. tgobj,
  26. symtype,symppu;
  27. type
  28. tnothingnode = class(tnode)
  29. constructor create;virtual;
  30. function pass_1 : tnode;override;
  31. function det_resulttype:tnode;override;
  32. end;
  33. tnothingnodeclass = class of tnothingnode;
  34. terrornode = class(tnode)
  35. constructor create;virtual;
  36. function pass_1 : tnode;override;
  37. function det_resulttype:tnode;override;
  38. procedure mark_write;override;
  39. end;
  40. terrornodeclass = class of terrornode;
  41. tasmnode = class(tnode)
  42. p_asm : taasmoutput;
  43. currenttai : tai;
  44. getposition : boolean;
  45. constructor create(p : taasmoutput);virtual;
  46. constructor create_get_position;
  47. destructor destroy;override;
  48. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  49. procedure ppuwrite(ppufile:tcompilerppufile);override;
  50. procedure derefimpl;override;
  51. function getcopy : tnode;override;
  52. function pass_1 : tnode;override;
  53. function det_resulttype:tnode;override;
  54. function docompare(p: tnode): boolean; override;
  55. end;
  56. tasmnodeclass = class of tasmnode;
  57. tstatementnode = class(tbinarynode)
  58. constructor create(l,r : tnode);virtual;
  59. function pass_1 : tnode;override;
  60. function det_resulttype:tnode;override;
  61. procedure printnodetree(var t:text);override;
  62. end;
  63. tstatementnodeclass = class of tstatementnode;
  64. tblocknode = class(tunarynode)
  65. constructor create(l : tnode;releasetemp : boolean);virtual;
  66. function pass_1 : tnode;override;
  67. function det_resulttype:tnode;override;
  68. {$ifdef state_tracking}
  69. function track_state_pass(exec_known:boolean):boolean;override;
  70. {$endif state_tracking}
  71. end;
  72. tblocknodeclass = class of tblocknode;
  73. { to allow access to the location by temp references even after the temp has }
  74. { already been disposed and to make sure the coherency between temps and }
  75. { temp references is kept after a getcopy }
  76. ptempinfo = ^ttempinfo;
  77. ttempinfo = record
  78. { set to the copy of a tempcreate pnode (if it gets copied) so that the }
  79. { refs and deletenode can hook to this copy once they get copied too }
  80. hookoncopy : ptempinfo;
  81. ref : treference;
  82. restype : ttype;
  83. temptype : ttemptype;
  84. valid : boolean;
  85. nextref_set_hookoncopy_nil : boolean;
  86. end;
  87. { a node which will create a (non)persistent temp of a given type with a given }
  88. { size (the size is separate to allow creating "void" temps with a custom size) }
  89. ttempcreatenode = class(tnode)
  90. size: longint;
  91. tempinfo: ptempinfo;
  92. { * persistent temps are used in manually written code where the temp }
  93. { be usable among different statements and where you can manually say }
  94. { when the temp has to be freed (using a ttempdeletenode) }
  95. { * non-persistent temps are mostly used in typeconversion helpers, }
  96. { where the node that receives the temp becomes responsible for }
  97. { freeing it. In this last case, you should use only one reference }
  98. { to it and *not* generate a ttempdeletenode }
  99. constructor create(const _restype: ttype; _size: longint; _temptype: ttemptype); virtual;
  100. function getcopy: tnode; override;
  101. function pass_1 : tnode; override;
  102. function det_resulttype: tnode; override;
  103. function docompare(p: tnode): boolean; override;
  104. procedure printnodedata(var t:text);override;
  105. end;
  106. ttempcreatenodeclass = class of ttempcreatenode;
  107. { a node which is a reference to a certain temp }
  108. ttemprefnode = class(tnode)
  109. constructor create(const temp: ttempcreatenode); virtual;
  110. constructor create_offset(const temp: ttempcreatenode;aoffset:longint);
  111. function getcopy: tnode; override;
  112. function pass_1 : tnode; override;
  113. function det_resulttype : tnode; override;
  114. procedure mark_write;override;
  115. function docompare(p: tnode): boolean; override;
  116. { Changes the location of this temp to ref. Useful when assigning }
  117. { another temp to this one. The current location will be freed. }
  118. { Can only be called in pass 2 (since earlier, the temp location }
  119. { isn't known yet) }
  120. procedure changelocation(const ref: treference);
  121. protected
  122. tempinfo: ptempinfo;
  123. offset : longint;
  124. end;
  125. ttemprefnodeclass = class of ttemprefnode;
  126. { a node which removes a temp }
  127. ttempdeletenode = class(tnode)
  128. constructor create(const temp: ttempcreatenode);
  129. { this will convert the persistant temp to a normal temp
  130. for returning to the other nodes }
  131. constructor create_normal_temp(const temp: ttempcreatenode);
  132. function getcopy: tnode; override;
  133. function pass_1: tnode; override;
  134. function det_resulttype: tnode; override;
  135. function docompare(p: tnode): boolean; override;
  136. destructor destroy; override;
  137. protected
  138. tempinfo: ptempinfo;
  139. release_to_normal : boolean;
  140. end;
  141. ttempdeletenodeclass = class of ttempdeletenode;
  142. var
  143. cnothingnode : tnothingnodeclass;
  144. cerrornode : terrornodeclass;
  145. casmnode : tasmnodeclass;
  146. cstatementnode : tstatementnodeclass;
  147. cblocknode : tblocknodeclass;
  148. ctempcreatenode : ttempcreatenodeclass;
  149. ctemprefnode : ttemprefnodeclass;
  150. ctempdeletenode : ttempdeletenodeclass;
  151. { Create a blocknode and statement node for multiple statements
  152. generated internally by the parser }
  153. function internalstatements(var laststatement:tstatementnode;releasetemp : boolean):tblocknode;
  154. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  155. implementation
  156. uses
  157. cutils,
  158. verbose,globals,globtype,systems,
  159. symconst,symdef,symsym,symutil,defutil,defcmp,
  160. pass_1,
  161. nld,ncal,nflw,rgobj,cginfo,cgbase
  162. ;
  163. {*****************************************************************************
  164. Helpers
  165. *****************************************************************************}
  166. function internalstatements(var laststatement:tstatementnode;releasetemp : boolean):tblocknode;
  167. begin
  168. { create dummy initial statement }
  169. laststatement := cstatementnode.create(cnothingnode.create,nil);
  170. internalstatements := cblocknode.create(laststatement,releasetemp);
  171. end;
  172. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  173. begin
  174. if assigned(laststatement.right) then
  175. internalerror(200204201);
  176. laststatement.right:=cstatementnode.create(n,nil);
  177. laststatement:=tstatementnode(laststatement.right);
  178. end;
  179. {*****************************************************************************
  180. TFIRSTNOTHING
  181. *****************************************************************************}
  182. constructor tnothingnode.create;
  183. begin
  184. inherited create(nothingn);
  185. end;
  186. function tnothingnode.det_resulttype:tnode;
  187. begin
  188. result:=nil;
  189. resulttype:=voidtype;
  190. end;
  191. function tnothingnode.pass_1 : tnode;
  192. begin
  193. result:=nil;
  194. expectloc:=LOC_VOID;
  195. end;
  196. {*****************************************************************************
  197. TFIRSTERROR
  198. *****************************************************************************}
  199. constructor terrornode.create;
  200. begin
  201. inherited create(errorn);
  202. end;
  203. function terrornode.det_resulttype:tnode;
  204. begin
  205. result:=nil;
  206. include(flags,nf_error);
  207. codegenerror:=true;
  208. resulttype:=generrortype;
  209. end;
  210. function terrornode.pass_1 : tnode;
  211. begin
  212. result:=nil;
  213. expectloc:=LOC_VOID;
  214. codegenerror:=true;
  215. end;
  216. procedure terrornode.mark_write;
  217. begin
  218. end;
  219. {*****************************************************************************
  220. TSTATEMENTNODE
  221. *****************************************************************************}
  222. constructor tstatementnode.create(l,r : tnode);
  223. begin
  224. inherited create(statementn,l,r);
  225. end;
  226. function tstatementnode.det_resulttype:tnode;
  227. begin
  228. result:=nil;
  229. resulttype:=voidtype;
  230. { left is the statement itself calln assignn or a complex one }
  231. resulttypepass(left);
  232. if (not (cs_extsyntax in aktmoduleswitches)) and
  233. assigned(left.resulttype.def) and
  234. not((left.nodetype=calln) and
  235. { don't complain when funcretrefnode is set, because then the
  236. value is already used. And also not for constructors }
  237. (assigned(tcallnode(left).funcretnode) or
  238. (tcallnode(left).procdefinition.proctypeoption=potype_constructor))) and
  239. not(is_void(left.resulttype.def)) then
  240. CGMessage(cg_e_illegal_expression);
  241. if codegenerror then
  242. exit;
  243. { right is the next statement in the list }
  244. if assigned(right) then
  245. resulttypepass(right);
  246. if codegenerror then
  247. exit;
  248. end;
  249. function tstatementnode.pass_1 : tnode;
  250. begin
  251. result:=nil;
  252. { left is the statement itself calln assignn or a complex one }
  253. firstpass(left);
  254. if codegenerror then
  255. exit;
  256. expectloc:=left.expectloc;
  257. registers32:=left.registers32;
  258. registersfpu:=left.registersfpu;
  259. {$ifdef SUPPORT_MMX}
  260. registersmmx:=left.registersmmx;
  261. {$endif SUPPORT_MMX}
  262. { right is the next in the list }
  263. if assigned(right) then
  264. firstpass(right);
  265. if codegenerror then
  266. exit;
  267. end;
  268. procedure tstatementnode.printnodetree(var t:text);
  269. begin
  270. printnodelist(t);
  271. end;
  272. {*****************************************************************************
  273. TBLOCKNODE
  274. *****************************************************************************}
  275. constructor tblocknode.create(l : tnode;releasetemp : boolean);
  276. begin
  277. inherited create(blockn,l);
  278. end;
  279. function tblocknode.det_resulttype:tnode;
  280. var
  281. hp : tstatementnode;
  282. begin
  283. result:=nil;
  284. resulttype:=voidtype;
  285. hp:=tstatementnode(left);
  286. while assigned(hp) do
  287. begin
  288. if assigned(hp.left) then
  289. begin
  290. codegenerror:=false;
  291. resulttypepass(hp.left);
  292. if (not (cs_extsyntax in aktmoduleswitches)) and
  293. assigned(hp.left.resulttype.def) and
  294. not((hp.left.nodetype=calln) and
  295. { don't complain when funcretnode is set, because then the
  296. value is already used. And also not for constructors }
  297. (assigned(tcallnode(hp.left).funcretnode) or
  298. (tcallnode(hp.left).procdefinition.proctypeoption=potype_constructor))) and
  299. not(is_void(hp.left.resulttype.def)) then
  300. CGMessagePos(hp.left.fileinfo,cg_e_illegal_expression);
  301. { the resulttype of the block is the last type that is
  302. returned. Normally this is a voidtype. But when the
  303. compiler inserts a block of multiple statements then the
  304. last entry can return a value }
  305. resulttype:=hp.left.resulttype;
  306. end;
  307. hp:=tstatementnode(hp.right);
  308. end;
  309. end;
  310. function tblocknode.pass_1 : tnode;
  311. var
  312. hp : tstatementnode;
  313. count : longint;
  314. begin
  315. result:=nil;
  316. expectloc:=LOC_VOID;
  317. count:=0;
  318. hp:=tstatementnode(left);
  319. while assigned(hp) do
  320. begin
  321. (*
  322. if cs_regvars in aktglobalswitches then
  323. begin
  324. { node transformations }
  325. { concat function result to exit }
  326. { this is wrong for string or other complex
  327. result types !!! }
  328. if {ret_in_acc(current_procinfo.procdef.rettype.def) and }
  329. (is_ordinal(current_procinfo.procdef.rettype.def) or
  330. is_smallset(current_procinfo.procdef.rettype.def)) and
  331. assigned(hp.right) and
  332. assigned(tstatementnode(hp.right).left) and
  333. (tstatementnode(hp.right).left.nodetype=exitn) and
  334. (hp.left.nodetype=assignn) and
  335. { !!!! this tbinarynode should be tassignmentnode }
  336. (tbinarynode(hp.left).left.nodetype=loadn) and
  337. (is_funcret_sym(tloadnode(tbinarynode(hp.left).left).symtableentry)) then
  338. begin
  339. if assigned(texitnode(tstatementnode(hp.right).left).left) then
  340. CGMessage(cg_n_inefficient_code)
  341. else
  342. begin
  343. texitnode(tstatementnode(hp.right).left).left:=tassignmentnode(hp.left).right;
  344. tassignmentnode(hp.left).right:=nil;
  345. hp.left.free;
  346. hp.left:=nil;
  347. end;
  348. end
  349. { warning if unreachable code occurs and elimate this }
  350. else if (hp.left.nodetype in
  351. [exitn,breakn,continuen,goton]) and
  352. { statement node (JM) }
  353. assigned(hp.right) and
  354. { kind of statement! (JM) }
  355. assigned(tstatementnode(hp.right).left) and
  356. (tstatementnode(hp.right).left.nodetype<>labeln) then
  357. begin
  358. { use correct line number }
  359. aktfilepos:=hp.right.fileinfo;
  360. hp.right.free;
  361. hp.right:=nil;
  362. CGMessage(cg_w_unreachable_code);
  363. { old lines }
  364. aktfilepos:=hp.left.fileinfo;
  365. end;
  366. end;
  367. *)
  368. if assigned(hp.left) then
  369. begin
  370. codegenerror:=false;
  371. firstpass(hp.left);
  372. hp.expectloc:=hp.left.expectloc;
  373. hp.registers32:=hp.left.registers32;
  374. hp.registersfpu:=hp.left.registersfpu;
  375. {$ifdef SUPPORT_MMX}
  376. hp.registersmmx:=hp.left.registersmmx;
  377. {$endif SUPPORT_MMX}
  378. end
  379. else
  380. hp.registers32:=0;
  381. if hp.registers32>registers32 then
  382. registers32:=hp.registers32;
  383. if hp.registersfpu>registersfpu then
  384. registersfpu:=hp.registersfpu;
  385. {$ifdef SUPPORT_MMX}
  386. if hp.registersmmx>registersmmx then
  387. registersmmx:=hp.registersmmx;
  388. {$endif}
  389. expectloc:=hp.expectloc;
  390. inc(count);
  391. hp:=tstatementnode(hp.right);
  392. end;
  393. end;
  394. {$ifdef state_tracking}
  395. function Tblocknode.track_state_pass(exec_known:boolean):boolean;
  396. var hp:Tstatementnode;
  397. begin
  398. track_state_pass:=false;
  399. hp:=Tstatementnode(left);
  400. while assigned(hp) do
  401. begin
  402. if hp.left.track_state_pass(exec_known) then
  403. track_state_pass:=true;
  404. hp:=Tstatementnode(hp.right);
  405. end;
  406. end;
  407. {$endif state_tracking}
  408. {*****************************************************************************
  409. TASMNODE
  410. *****************************************************************************}
  411. constructor tasmnode.create(p : taasmoutput);
  412. begin
  413. inherited create(asmn);
  414. p_asm:=p;
  415. getposition:=false;
  416. currenttai:=nil;
  417. end;
  418. constructor tasmnode.create_get_position;
  419. begin
  420. inherited create(asmn);
  421. p_asm:=nil;
  422. getposition:=true;
  423. currenttai:=nil;
  424. end;
  425. destructor tasmnode.destroy;
  426. begin
  427. if assigned(p_asm) then
  428. p_asm.free;
  429. inherited destroy;
  430. end;
  431. constructor tasmnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  432. var
  433. hp : tai;
  434. begin
  435. inherited ppuload(t,ppufile);
  436. getposition:=boolean(ppufile.getbyte);
  437. if not getposition then
  438. begin
  439. p_asm:=taasmoutput.create;
  440. repeat
  441. hp:=ppuloadai(ppufile);
  442. if hp=nil then
  443. break;
  444. p_asm.concat(hp);
  445. until false;
  446. end
  447. else
  448. p_asm:=nil;
  449. currenttai:=nil;
  450. end;
  451. procedure tasmnode.ppuwrite(ppufile:tcompilerppufile);
  452. var
  453. hp : tai;
  454. begin
  455. inherited ppuwrite(ppufile);
  456. ppufile.putbyte(byte(getposition));
  457. if not getposition then
  458. begin
  459. hp:=tai(p_asm.first);
  460. while assigned(hp) do
  461. begin
  462. ppuwriteai(ppufile,hp);
  463. hp:=tai(hp.next);
  464. end;
  465. { end is marked by a nil }
  466. ppuwriteai(ppufile,nil);
  467. end;
  468. end;
  469. procedure tasmnode.derefimpl;
  470. var
  471. hp : tai;
  472. begin
  473. inherited derefimpl;
  474. hp:=tai(p_asm.first);
  475. while assigned(hp) do
  476. begin
  477. hp.derefimpl;
  478. hp:=tai(hp.next);
  479. end;
  480. end;
  481. function tasmnode.getcopy: tnode;
  482. var
  483. n: tasmnode;
  484. begin
  485. n := tasmnode(inherited getcopy);
  486. if assigned(p_asm) then
  487. begin
  488. n.p_asm:=taasmoutput.create;
  489. n.p_asm.concatlistcopy(p_asm);
  490. end
  491. else n.p_asm := nil;
  492. n.getposition:=getposition;
  493. n.currenttai:=currenttai;
  494. getcopy := n;
  495. end;
  496. function tasmnode.det_resulttype:tnode;
  497. begin
  498. result:=nil;
  499. resulttype:=voidtype;
  500. if not getposition then
  501. include(current_procinfo.flags,pi_uses_asm);
  502. end;
  503. function tasmnode.pass_1 : tnode;
  504. begin
  505. result:=nil;
  506. expectloc:=LOC_VOID;
  507. end;
  508. function tasmnode.docompare(p: tnode): boolean;
  509. begin
  510. { comparing of asmlists is not implemented (JM) }
  511. docompare := false;
  512. end;
  513. {*****************************************************************************
  514. TEMPCREATENODE
  515. *****************************************************************************}
  516. constructor ttempcreatenode.create(const _restype: ttype; _size: longint; _temptype: ttemptype);
  517. begin
  518. inherited create(tempcreaten);
  519. size := _size;
  520. new(tempinfo);
  521. fillchar(tempinfo^,sizeof(tempinfo^),0);
  522. tempinfo^.restype := _restype;
  523. tempinfo^.temptype := _temptype;
  524. end;
  525. function ttempcreatenode.getcopy: tnode;
  526. var
  527. n: ttempcreatenode;
  528. begin
  529. n := ttempcreatenode(inherited getcopy);
  530. n.size := size;
  531. new(n.tempinfo);
  532. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  533. n.tempinfo^.restype := tempinfo^.restype;
  534. n.tempinfo^.temptype := tempinfo^.temptype;
  535. { when the tempinfo has already a hookoncopy then it is not
  536. reset by a tempdeletenode }
  537. if assigned(tempinfo^.hookoncopy) then
  538. internalerror(200211262);
  539. { signal the temprefs that the temp they point to has been copied, }
  540. { so that if the refs get copied as well, they can hook themselves }
  541. { to the copy of the temp }
  542. tempinfo^.hookoncopy := n.tempinfo;
  543. tempinfo^.nextref_set_hookoncopy_nil := false;
  544. result := n;
  545. end;
  546. function ttempcreatenode.pass_1 : tnode;
  547. begin
  548. result := nil;
  549. expectloc:=LOC_VOID;
  550. end;
  551. function ttempcreatenode.det_resulttype: tnode;
  552. begin
  553. result := nil;
  554. { a tempcreatenode doesn't have a resulttype, only temprefnodes do }
  555. resulttype := voidtype;
  556. end;
  557. function ttempcreatenode.docompare(p: tnode): boolean;
  558. begin
  559. result :=
  560. inherited docompare(p) and
  561. (ttempcreatenode(p).size = size) and
  562. equal_defs(ttempcreatenode(p).tempinfo^.restype.def,tempinfo^.restype.def);
  563. end;
  564. procedure ttempcreatenode.printnodedata(var t:text);
  565. begin
  566. inherited printnodedata(t);
  567. writeln(t,printnodeindention,'size = ',size);
  568. end;
  569. {*****************************************************************************
  570. TEMPREFNODE
  571. *****************************************************************************}
  572. constructor ttemprefnode.create(const temp: ttempcreatenode);
  573. begin
  574. inherited create(temprefn);
  575. tempinfo := temp.tempinfo;
  576. offset:=0;
  577. end;
  578. constructor ttemprefnode.create_offset(const temp: ttempcreatenode;aoffset:longint);
  579. begin
  580. self.create(temp);
  581. offset := aoffset;
  582. end;
  583. function ttemprefnode.getcopy: tnode;
  584. var
  585. n: ttemprefnode;
  586. begin
  587. n := ttemprefnode(inherited getcopy);
  588. n.offset := offset;
  589. if assigned(tempinfo^.hookoncopy) then
  590. { if the temp has been copied, assume it becomes a new }
  591. { temp which has to be hooked by the copied reference }
  592. begin
  593. { hook the ref to the copied temp }
  594. n.tempinfo := tempinfo^.hookoncopy;
  595. { if we passed a ttempdeletenode that changed the temp }
  596. { from a persistent one into a normal one, we must be }
  597. { the last reference (since our parent should free the }
  598. { temp (JM) }
  599. if (tempinfo^.nextref_set_hookoncopy_nil) then
  600. tempinfo^.hookoncopy := nil;
  601. end
  602. else
  603. { if the temp we refer to hasn't been copied, assume }
  604. { we're just a new reference to that temp }
  605. begin
  606. n.tempinfo := tempinfo;
  607. end;
  608. result := n;
  609. end;
  610. function ttemprefnode.pass_1 : tnode;
  611. begin
  612. expectloc:=LOC_REFERENCE;
  613. result := nil;
  614. end;
  615. function ttemprefnode.det_resulttype: tnode;
  616. begin
  617. { check if the temp is already resulttype passed }
  618. if not assigned(tempinfo^.restype.def) then
  619. internalerror(200108233);
  620. result := nil;
  621. resulttype := tempinfo^.restype;
  622. end;
  623. function ttemprefnode.docompare(p: tnode): boolean;
  624. begin
  625. result :=
  626. inherited docompare(p) and
  627. (ttemprefnode(p).tempinfo = tempinfo) and
  628. (ttemprefnode(p).offset = offset);
  629. end;
  630. procedure Ttemprefnode.mark_write;
  631. begin
  632. include(flags,nf_write);
  633. end;
  634. procedure ttemprefnode.changelocation(const ref: treference);
  635. begin
  636. { check if the temp is valid }
  637. if not tempinfo^.valid then
  638. internalerror(200306081);
  639. if (tempinfo^.temptype = tt_persistent) then
  640. tg.ChangeTempType(exprasmlist,tempinfo^.ref,tt_normal);
  641. tg.ungettemp(exprasmlist,tempinfo^.ref);
  642. tempinfo^.ref := ref;
  643. tg.ChangeTempType(exprasmlist,tempinfo^.ref,tempinfo^.temptype);
  644. { adapt location }
  645. location.reference := ref;
  646. inc(location.reference.offset,offset);
  647. end;
  648. {*****************************************************************************
  649. TEMPDELETENODE
  650. *****************************************************************************}
  651. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  652. begin
  653. inherited create(tempdeleten);
  654. tempinfo := temp.tempinfo;
  655. release_to_normal := false;
  656. end;
  657. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  658. begin
  659. inherited create(tempdeleten);
  660. tempinfo := temp.tempinfo;
  661. release_to_normal := true;
  662. if tempinfo^.temptype <> tt_persistent then
  663. internalerror(200204211);
  664. end;
  665. function ttempdeletenode.getcopy: tnode;
  666. var
  667. n: ttempdeletenode;
  668. begin
  669. n := ttempdeletenode(inherited getcopy);
  670. n.release_to_normal := release_to_normal;
  671. if assigned(tempinfo^.hookoncopy) then
  672. { if the temp has been copied, assume it becomes a new }
  673. { temp which has to be hooked by the copied deletenode }
  674. begin
  675. { hook the tempdeletenode to the copied temp }
  676. n.tempinfo := tempinfo^.hookoncopy;
  677. { the temp shall not be used, reset hookoncopy }
  678. { Only if release_to_normal is false, otherwise }
  679. { the temp can still be referenced once more (JM) }
  680. if (not release_to_normal) then
  681. tempinfo^.hookoncopy:=nil
  682. else
  683. tempinfo^.nextref_set_hookoncopy_nil := true;
  684. end
  685. else
  686. { if the temp we refer to hasn't been copied, we have a }
  687. { problem since that means we now have two delete nodes }
  688. { for one temp }
  689. internalerror(200108234);
  690. result := n;
  691. end;
  692. function ttempdeletenode.pass_1 : tnode;
  693. begin
  694. expectloc:=LOC_VOID;
  695. result := nil;
  696. end;
  697. function ttempdeletenode.det_resulttype: tnode;
  698. begin
  699. result := nil;
  700. resulttype := voidtype;
  701. end;
  702. function ttempdeletenode.docompare(p: tnode): boolean;
  703. begin
  704. result :=
  705. inherited docompare(p) and
  706. (ttemprefnode(p).tempinfo = tempinfo);
  707. end;
  708. destructor ttempdeletenode.destroy;
  709. begin
  710. dispose(tempinfo);
  711. end;
  712. begin
  713. cnothingnode:=tnothingnode;
  714. cerrornode:=terrornode;
  715. casmnode:=tasmnode;
  716. cstatementnode:=tstatementnode;
  717. cblocknode:=tblocknode;
  718. ctempcreatenode:=ttempcreatenode;
  719. ctemprefnode:=ttemprefnode;
  720. ctempdeletenode:=ttempdeletenode;
  721. end.
  722. {
  723. $Log$
  724. Revision 1.60 2003-09-03 15:55:00 peter
  725. * NEWRA branch merged
  726. Revision 1.59.2.1 2003/08/27 20:23:55 peter
  727. * remove old ra code
  728. Revision 1.59 2003/08/09 18:56:54 daniel
  729. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  730. allocator
  731. * Some preventive changes to i386 spillinh code
  732. Revision 1.58 2003/06/13 21:19:30 peter
  733. * current_procdef removed, use current_procinfo.procdef instead
  734. Revision 1.57 2003/06/10 09:10:47 jonas
  735. * patch from Peter to fix tempinfo copying
  736. Revision 1.56 2003/06/09 18:26:46 peter
  737. * remove temptype, use tempinfo.temptype instead
  738. Revision 1.55 2003/06/09 12:20:47 peter
  739. * getposition added to retrieve the the current tai item
  740. Revision 1.54 2003/06/08 18:27:15 jonas
  741. + ability to change the location of a ttempref node with changelocation()
  742. method. Useful to use instead of copying the contents from one temp to
  743. another
  744. + some shortstring optimizations in tassignmentnode that avoid some
  745. copying (required some shortstring optimizations to be moved from
  746. resulttype to firstpass, because they work on callnodes and string
  747. addnodes are only changed to callnodes in the firstpass)
  748. * allow setting/changing the funcretnode of callnodes after the
  749. resulttypepass has been done, funcretnode is now a property
  750. (all of the above should have a quite big effect on callparatemp)
  751. Revision 1.53 2003/05/30 21:01:44 jonas
  752. - disabled "result := value; exit;" -> exit(value) optimization because
  753. a) it was wrong
  754. b) exit(value) works now exactly the same as that
  755. (it was only activated with -Or)
  756. Revision 1.52 2003/05/23 14:27:35 peter
  757. * remove some unit dependencies
  758. * current_procinfo changes to store more info
  759. Revision 1.51 2003/05/17 13:30:08 jonas
  760. * changed tt_persistant to tt_persistent :)
  761. * tempcreatenode now doesn't accept a boolean anymore for persistent
  762. temps, but a ttemptype, so you can also create ansistring temps etc
  763. Revision 1.50 2003/05/13 19:14:41 peter
  764. * failn removed
  765. * inherited result code check moven to pexpr
  766. Revision 1.49 2003/05/11 14:45:12 peter
  767. * tloadnode does not support objectsymtable,withsymtable anymore
  768. * withnode cleanup
  769. * direct with rewritten to use temprefnode
  770. Revision 1.48 2003/04/27 11:21:33 peter
  771. * aktprocdef renamed to current_procinfo.procdef
  772. * procinfo renamed to current_procinfo
  773. * procinfo will now be stored in current_module so it can be
  774. cleaned up properly
  775. * gen_main_procsym changed to create_main_proc and release_main_proc
  776. to also generate a tprocinfo structure
  777. * fixed unit implicit initfinal
  778. Revision 1.47 2003/04/25 20:59:33 peter
  779. * removed funcretn,funcretsym, function result is now in varsym
  780. and aliases for result and function name are added using absolutesym
  781. * vs_hidden parameter for funcret passed in parameter
  782. * vs_hidden fixes
  783. * writenode changed to printnode and released from extdebug
  784. * -vp option added to generate a tree.log with the nodetree
  785. * nicer printnode for statements, callnode
  786. Revision 1.46 2002/04/25 20:15:39 florian
  787. * block nodes within expressions shouldn't release the used registers,
  788. fixed using a flag till the new rg is ready
  789. Revision 1.45 2003/04/23 08:41:34 jonas
  790. * fixed ttemprefnode.compare and .getcopy to take offset field into
  791. account
  792. Revision 1.44 2003/04/22 23:50:22 peter
  793. * firstpass uses expectloc
  794. * checks if there are differences between the expectloc and
  795. location.loc from secondpass in EXTDEBUG
  796. Revision 1.43 2003/04/21 15:00:22 jonas
  797. * fixed tstatementnode.det_resulttype and tststatementnode.pass_1
  798. * fixed some getcopy issues with ttemp*nodes
  799. Revision 1.42 2003/04/17 07:50:24 daniel
  800. * Some work on interference graph construction
  801. Revision 1.41 2003/04/12 14:53:59 jonas
  802. * ttempdeletenode.create now sets the nodetype to tempdeleten instead of
  803. temprefn
  804. Revision 1.40 2003/03/17 20:30:46 peter
  805. * errornode.mark_write added
  806. Revision 1.39 2003/01/03 12:15:55 daniel
  807. * Removed ifdefs around notifications
  808. ifdefs around for loop optimizations remain
  809. Revision 1.38 2002/11/27 02:37:12 peter
  810. * case statement inlining added
  811. * fixed inlining of write()
  812. * switched statementnode left and right parts so the statements are
  813. processed in the correct order when getcopy is used. This is
  814. required for tempnodes
  815. Revision 1.37 2002/11/25 17:43:17 peter
  816. * splitted defbase in defutil,symutil,defcmp
  817. * merged isconvertable and is_equal into compare_defs(_ext)
  818. * made operator search faster by walking the list only once
  819. Revision 1.36 2002/10/05 15:15:19 peter
  820. * don't complain in X- mode for internal generated function calls
  821. with funcretrefnode set
  822. * give statement error at the correct line position instead of the
  823. block begin
  824. Revision 1.35 2002/09/01 08:01:16 daniel
  825. * Removed sets from Tcallnode.det_resulttype
  826. + Added read/write notifications of variables. These will be usefull
  827. for providing information for several optimizations. For example
  828. the value of the loop variable of a for loop does matter is the
  829. variable is read after the for loop, but if it's no longer used
  830. or written, it doesn't matter and this can be used to optimize
  831. the loop code generation.
  832. Revision 1.34 2002/08/18 20:06:23 peter
  833. * inlining is now also allowed in interface
  834. * renamed write/load to ppuwrite/ppuload
  835. * tnode storing in ppu
  836. * nld,ncon,nbas are already updated for storing in ppu
  837. Revision 1.33 2002/08/17 22:09:44 florian
  838. * result type handling in tcgcal.pass_2 overhauled
  839. * better tnode.printnodetree
  840. * some ppc stuff fixed
  841. Revision 1.32 2002/08/17 09:23:34 florian
  842. * first part of procinfo rewrite
  843. Revision 1.31 2002/08/15 19:10:35 peter
  844. * first things tai,tnode storing in ppu
  845. Revision 1.30 2002/07/20 11:57:53 florian
  846. * types.pas renamed to defbase.pas because D6 contains a types
  847. unit so this would conflicts if D6 programms are compiled
  848. + Willamette/SSE2 instructions to assembler added
  849. Revision 1.29 2002/07/19 11:41:35 daniel
  850. * State tracker work
  851. * The whilen and repeatn are now completely unified into whilerepeatn. This
  852. allows the state tracker to change while nodes automatically into
  853. repeat nodes.
  854. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  855. 'not(a>b)' is optimized into 'a<=b'.
  856. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  857. by removing the notn and later switchting the true and falselabels. The
  858. same is done with 'repeat until not a'.
  859. Revision 1.28 2002/07/14 18:00:43 daniel
  860. + Added the beginning of a state tracker. This will track the values of
  861. variables through procedures and optimize things away.
  862. Revision 1.27 2002/07/01 18:46:22 peter
  863. * internal linker
  864. * reorganized aasm layer
  865. Revision 1.26 2002/06/24 12:43:00 jonas
  866. * fixed errors found with new -CR code from Peter when cycling with -O2p3r
  867. Revision 1.25 2002/05/18 13:34:09 peter
  868. * readded missing revisions
  869. Revision 1.24 2002/05/16 19:46:37 carl
  870. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  871. + try to fix temp allocation (still in ifdef)
  872. + generic constructor calls
  873. + start of tassembler / tmodulebase class cleanup
  874. Revision 1.22 2002/04/23 19:16:34 peter
  875. * add pinline unit that inserts compiler supported functions using
  876. one or more statements
  877. * moved finalize and setlength from ninl to pinline
  878. Revision 1.21 2002/04/21 19:02:03 peter
  879. * removed newn and disposen nodes, the code is now directly
  880. inlined from pexpr
  881. * -an option that will write the secondpass nodes to the .s file, this
  882. requires EXTDEBUG define to actually write the info
  883. * fixed various internal errors and crashes due recent code changes
  884. Revision 1.20 2002/04/04 19:05:57 peter
  885. * removed unused units
  886. * use tlocation.size in cg.a_*loc*() routines
  887. Revision 1.19 2002/03/31 20:26:33 jonas
  888. + a_loadfpu_* and a_loadmm_* methods in tcg
  889. * register allocation is now handled by a class and is mostly processor
  890. independent (+rgobj.pas and i386/rgcpu.pas)
  891. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  892. * some small improvements and fixes to the optimizer
  893. * some register allocation fixes
  894. * some fpuvaroffset fixes in the unary minus node
  895. * push/popusedregisters is now called rg.save/restoreusedregisters and
  896. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  897. also better optimizable)
  898. * fixed and optimized register saving/restoring for new/dispose nodes
  899. * LOC_FPU locations now also require their "register" field to be set to
  900. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  901. - list field removed of the tnode class because it's not used currently
  902. and can cause hard-to-find bugs
  903. }