nbas.pas 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. {$ifndef newra}
  279. if releasetemp then
  280. include(flags,nf_releasetemps);
  281. {$endif newra}
  282. end;
  283. function tblocknode.det_resulttype:tnode;
  284. var
  285. hp : tstatementnode;
  286. begin
  287. result:=nil;
  288. resulttype:=voidtype;
  289. hp:=tstatementnode(left);
  290. while assigned(hp) do
  291. begin
  292. if assigned(hp.left) then
  293. begin
  294. codegenerror:=false;
  295. resulttypepass(hp.left);
  296. if (not (cs_extsyntax in aktmoduleswitches)) and
  297. assigned(hp.left.resulttype.def) and
  298. not((hp.left.nodetype=calln) and
  299. { don't complain when funcretnode is set, because then the
  300. value is already used. And also not for constructors }
  301. (assigned(tcallnode(hp.left).funcretnode) or
  302. (tcallnode(hp.left).procdefinition.proctypeoption=potype_constructor))) and
  303. not(is_void(hp.left.resulttype.def)) then
  304. CGMessagePos(hp.left.fileinfo,cg_e_illegal_expression);
  305. { the resulttype of the block is the last type that is
  306. returned. Normally this is a voidtype. But when the
  307. compiler inserts a block of multiple statements then the
  308. last entry can return a value }
  309. resulttype:=hp.left.resulttype;
  310. end;
  311. hp:=tstatementnode(hp.right);
  312. end;
  313. end;
  314. function tblocknode.pass_1 : tnode;
  315. var
  316. hp : tstatementnode;
  317. count : longint;
  318. begin
  319. result:=nil;
  320. expectloc:=LOC_VOID;
  321. count:=0;
  322. hp:=tstatementnode(left);
  323. while assigned(hp) do
  324. begin
  325. (*
  326. if cs_regalloc in aktglobalswitches then
  327. begin
  328. { node transformations }
  329. { concat function result to exit }
  330. { this is wrong for string or other complex
  331. result types !!! }
  332. if {ret_in_acc(current_procdef.rettype.def) and }
  333. (is_ordinal(current_procdef.rettype.def) or
  334. is_smallset(current_procdef.rettype.def)) and
  335. assigned(hp.right) and
  336. assigned(tstatementnode(hp.right).left) and
  337. (tstatementnode(hp.right).left.nodetype=exitn) and
  338. (hp.left.nodetype=assignn) and
  339. { !!!! this tbinarynode should be tassignmentnode }
  340. (tbinarynode(hp.left).left.nodetype=loadn) and
  341. (is_funcret_sym(tloadnode(tbinarynode(hp.left).left).symtableentry)) then
  342. begin
  343. if assigned(texitnode(tstatementnode(hp.right).left).left) then
  344. CGMessage(cg_n_inefficient_code)
  345. else
  346. begin
  347. texitnode(tstatementnode(hp.right).left).left:=tassignmentnode(hp.left).right;
  348. tassignmentnode(hp.left).right:=nil;
  349. hp.left.free;
  350. hp.left:=nil;
  351. end;
  352. end
  353. { warning if unreachable code occurs and elimate this }
  354. else if (hp.left.nodetype in
  355. [exitn,breakn,continuen,goton]) and
  356. { statement node (JM) }
  357. assigned(hp.right) and
  358. { kind of statement! (JM) }
  359. assigned(tstatementnode(hp.right).left) and
  360. (tstatementnode(hp.right).left.nodetype<>labeln) then
  361. begin
  362. { use correct line number }
  363. aktfilepos:=hp.right.fileinfo;
  364. hp.right.free;
  365. hp.right:=nil;
  366. CGMessage(cg_w_unreachable_code);
  367. { old lines }
  368. aktfilepos:=hp.left.fileinfo;
  369. end;
  370. end;
  371. *)
  372. if assigned(hp.left) then
  373. begin
  374. codegenerror:=false;
  375. firstpass(hp.left);
  376. hp.expectloc:=hp.left.expectloc;
  377. hp.registers32:=hp.left.registers32;
  378. hp.registersfpu:=hp.left.registersfpu;
  379. {$ifdef SUPPORT_MMX}
  380. hp.registersmmx:=hp.left.registersmmx;
  381. {$endif SUPPORT_MMX}
  382. end
  383. else
  384. hp.registers32:=0;
  385. if hp.registers32>registers32 then
  386. registers32:=hp.registers32;
  387. if hp.registersfpu>registersfpu then
  388. registersfpu:=hp.registersfpu;
  389. {$ifdef SUPPORT_MMX}
  390. if hp.registersmmx>registersmmx then
  391. registersmmx:=hp.registersmmx;
  392. {$endif}
  393. expectloc:=hp.expectloc;
  394. inc(count);
  395. hp:=tstatementnode(hp.right);
  396. end;
  397. end;
  398. {$ifdef state_tracking}
  399. function Tblocknode.track_state_pass(exec_known:boolean):boolean;
  400. var hp:Tstatementnode;
  401. begin
  402. track_state_pass:=false;
  403. hp:=Tstatementnode(left);
  404. while assigned(hp) do
  405. begin
  406. if hp.left.track_state_pass(exec_known) then
  407. track_state_pass:=true;
  408. hp:=Tstatementnode(hp.right);
  409. end;
  410. end;
  411. {$endif state_tracking}
  412. {*****************************************************************************
  413. TASMNODE
  414. *****************************************************************************}
  415. constructor tasmnode.create(p : taasmoutput);
  416. begin
  417. inherited create(asmn);
  418. p_asm:=p;
  419. getposition:=false;
  420. currenttai:=nil;
  421. end;
  422. constructor tasmnode.create_get_position;
  423. begin
  424. inherited create(asmn);
  425. p_asm:=nil;
  426. getposition:=true;
  427. currenttai:=nil;
  428. end;
  429. destructor tasmnode.destroy;
  430. begin
  431. if assigned(p_asm) then
  432. p_asm.free;
  433. inherited destroy;
  434. end;
  435. constructor tasmnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  436. var
  437. hp : tai;
  438. begin
  439. inherited ppuload(t,ppufile);
  440. getposition:=boolean(ppufile.getbyte);
  441. if not getposition then
  442. begin
  443. p_asm:=taasmoutput.create;
  444. repeat
  445. hp:=ppuloadai(ppufile);
  446. if hp=nil then
  447. break;
  448. p_asm.concat(hp);
  449. until false;
  450. end
  451. else
  452. p_asm:=nil;
  453. currenttai:=nil;
  454. end;
  455. procedure tasmnode.ppuwrite(ppufile:tcompilerppufile);
  456. var
  457. hp : tai;
  458. begin
  459. inherited ppuwrite(ppufile);
  460. ppufile.putbyte(byte(getposition));
  461. if not getposition then
  462. begin
  463. hp:=tai(p_asm.first);
  464. while assigned(hp) do
  465. begin
  466. ppuwriteai(ppufile,hp);
  467. hp:=tai(hp.next);
  468. end;
  469. { end is marked by a nil }
  470. ppuwriteai(ppufile,nil);
  471. end;
  472. end;
  473. procedure tasmnode.derefimpl;
  474. var
  475. hp : tai;
  476. begin
  477. inherited derefimpl;
  478. hp:=tai(p_asm.first);
  479. while assigned(hp) do
  480. begin
  481. hp.derefimpl;
  482. hp:=tai(hp.next);
  483. end;
  484. end;
  485. function tasmnode.getcopy: tnode;
  486. var
  487. n: tasmnode;
  488. begin
  489. n := tasmnode(inherited getcopy);
  490. if assigned(p_asm) then
  491. begin
  492. n.p_asm:=taasmoutput.create;
  493. n.p_asm.concatlistcopy(p_asm);
  494. end
  495. else n.p_asm := nil;
  496. n.getposition:=getposition;
  497. n.currenttai:=currenttai;
  498. getcopy := n;
  499. end;
  500. function tasmnode.det_resulttype:tnode;
  501. begin
  502. result:=nil;
  503. resulttype:=voidtype;
  504. if not getposition then
  505. include(current_procinfo.flags,pi_uses_asm);
  506. end;
  507. function tasmnode.pass_1 : tnode;
  508. begin
  509. result:=nil;
  510. expectloc:=LOC_VOID;
  511. end;
  512. function tasmnode.docompare(p: tnode): boolean;
  513. begin
  514. { comparing of asmlists is not implemented (JM) }
  515. docompare := false;
  516. end;
  517. {*****************************************************************************
  518. TEMPCREATENODE
  519. *****************************************************************************}
  520. constructor ttempcreatenode.create(const _restype: ttype; _size: longint; _temptype: ttemptype);
  521. begin
  522. inherited create(tempcreaten);
  523. size := _size;
  524. new(tempinfo);
  525. fillchar(tempinfo^,sizeof(tempinfo^),0);
  526. tempinfo^.restype := _restype;
  527. tempinfo^.temptype := _temptype;
  528. end;
  529. function ttempcreatenode.getcopy: tnode;
  530. var
  531. n: ttempcreatenode;
  532. begin
  533. n := ttempcreatenode(inherited getcopy);
  534. n.size := size;
  535. new(n.tempinfo);
  536. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  537. n.tempinfo^.restype := tempinfo^.restype;
  538. { when the tempinfo has already a hookoncopy then it is not
  539. reset by a tempdeletenode }
  540. if assigned(tempinfo^.hookoncopy) then
  541. internalerror(200211262);
  542. { signal the temprefs that the temp they point to has been copied, }
  543. { so that if the refs get copied as well, they can hook themselves }
  544. { to the copy of the temp }
  545. tempinfo^.hookoncopy := n.tempinfo;
  546. tempinfo^.nextref_set_hookoncopy_nil := false;
  547. result := n;
  548. end;
  549. function ttempcreatenode.pass_1 : tnode;
  550. begin
  551. result := nil;
  552. expectloc:=LOC_VOID;
  553. end;
  554. function ttempcreatenode.det_resulttype: tnode;
  555. begin
  556. result := nil;
  557. { a tempcreatenode doesn't have a resulttype, only temprefnodes do }
  558. resulttype := voidtype;
  559. end;
  560. function ttempcreatenode.docompare(p: tnode): boolean;
  561. begin
  562. result :=
  563. inherited docompare(p) and
  564. (ttempcreatenode(p).size = size) and
  565. equal_defs(ttempcreatenode(p).tempinfo^.restype.def,tempinfo^.restype.def);
  566. end;
  567. procedure ttempcreatenode.printnodedata(var t:text);
  568. begin
  569. inherited printnodedata(t);
  570. writeln(t,printnodeindention,'size = ',size);
  571. end;
  572. {*****************************************************************************
  573. TEMPREFNODE
  574. *****************************************************************************}
  575. constructor ttemprefnode.create(const temp: ttempcreatenode);
  576. begin
  577. inherited create(temprefn);
  578. tempinfo := temp.tempinfo;
  579. offset:=0;
  580. end;
  581. constructor ttemprefnode.create_offset(const temp: ttempcreatenode;aoffset:longint);
  582. begin
  583. self.create(temp);
  584. offset := aoffset;
  585. end;
  586. function ttemprefnode.getcopy: tnode;
  587. var
  588. n: ttemprefnode;
  589. begin
  590. n := ttemprefnode(inherited getcopy);
  591. n.offset := offset;
  592. if assigned(tempinfo^.hookoncopy) then
  593. { if the temp has been copied, assume it becomes a new }
  594. { temp which has to be hooked by the copied reference }
  595. begin
  596. { hook the ref to the copied temp }
  597. n.tempinfo := tempinfo^.hookoncopy;
  598. { if we passed a ttempdeletenode that changed the temp }
  599. { from a persistent one into a normal one, we must be }
  600. { the last reference (since our parent should free the }
  601. { temp (JM) }
  602. if (tempinfo^.nextref_set_hookoncopy_nil) then
  603. tempinfo^.hookoncopy := nil;
  604. end
  605. else
  606. { if the temp we refer to hasn't been copied, assume }
  607. { we're just a new reference to that temp }
  608. begin
  609. n.tempinfo := tempinfo;
  610. end;
  611. result := n;
  612. end;
  613. function ttemprefnode.pass_1 : tnode;
  614. begin
  615. expectloc:=LOC_REFERENCE;
  616. result := nil;
  617. end;
  618. function ttemprefnode.det_resulttype: tnode;
  619. begin
  620. { check if the temp is already resulttype passed }
  621. if not assigned(tempinfo^.restype.def) then
  622. internalerror(200108233);
  623. result := nil;
  624. resulttype := tempinfo^.restype;
  625. end;
  626. function ttemprefnode.docompare(p: tnode): boolean;
  627. begin
  628. result :=
  629. inherited docompare(p) and
  630. (ttemprefnode(p).tempinfo = tempinfo) and
  631. (ttemprefnode(p).offset = offset);
  632. end;
  633. procedure Ttemprefnode.mark_write;
  634. begin
  635. include(flags,nf_write);
  636. end;
  637. procedure ttemprefnode.changelocation(const ref: treference);
  638. begin
  639. { check if the temp is valid }
  640. if not tempinfo^.valid then
  641. internalerror(200306081);
  642. if (tempinfo^.temptype = tt_persistent) then
  643. tg.ChangeTempType(exprasmlist,tempinfo^.ref,tt_normal);
  644. tg.ungettemp(exprasmlist,tempinfo^.ref);
  645. tempinfo^.ref := ref;
  646. tg.ChangeTempType(exprasmlist,tempinfo^.ref,tempinfo^.temptype);
  647. { adapt location }
  648. location.reference := ref;
  649. inc(location.reference.offset,offset);
  650. end;
  651. {*****************************************************************************
  652. TEMPDELETENODE
  653. *****************************************************************************}
  654. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  655. begin
  656. inherited create(tempdeleten);
  657. tempinfo := temp.tempinfo;
  658. release_to_normal := false;
  659. end;
  660. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  661. begin
  662. inherited create(tempdeleten);
  663. tempinfo := temp.tempinfo;
  664. release_to_normal := true;
  665. if tempinfo^.temptype <> tt_persistent then
  666. internalerror(200204211);
  667. end;
  668. function ttempdeletenode.getcopy: tnode;
  669. var
  670. n: ttempdeletenode;
  671. begin
  672. n := ttempdeletenode(inherited getcopy);
  673. n.release_to_normal := release_to_normal;
  674. if assigned(tempinfo^.hookoncopy) then
  675. { if the temp has been copied, assume it becomes a new }
  676. { temp which has to be hooked by the copied deletenode }
  677. begin
  678. { hook the tempdeletenode to the copied temp }
  679. n.tempinfo := tempinfo^.hookoncopy;
  680. { the temp shall not be used, reset hookoncopy }
  681. { Only if release_to_normal is false, otherwise }
  682. { the temp can still be referenced once more (JM) }
  683. if (not release_to_normal) then
  684. tempinfo^.hookoncopy:=nil
  685. else
  686. tempinfo^.nextref_set_hookoncopy_nil := true;
  687. end
  688. else
  689. { if the temp we refer to hasn't been copied, we have a }
  690. { problem since that means we now have two delete nodes }
  691. { for one temp }
  692. internalerror(200108234);
  693. result := n;
  694. end;
  695. function ttempdeletenode.pass_1 : tnode;
  696. begin
  697. expectloc:=LOC_VOID;
  698. result := nil;
  699. end;
  700. function ttempdeletenode.det_resulttype: tnode;
  701. begin
  702. result := nil;
  703. resulttype := voidtype;
  704. end;
  705. function ttempdeletenode.docompare(p: tnode): boolean;
  706. begin
  707. result :=
  708. inherited docompare(p) and
  709. (ttemprefnode(p).tempinfo = tempinfo);
  710. end;
  711. destructor ttempdeletenode.destroy;
  712. begin
  713. dispose(tempinfo);
  714. end;
  715. begin
  716. cnothingnode:=tnothingnode;
  717. cerrornode:=terrornode;
  718. casmnode:=tasmnode;
  719. cstatementnode:=tstatementnode;
  720. cblocknode:=tblocknode;
  721. ctempcreatenode:=ttempcreatenode;
  722. ctemprefnode:=ttemprefnode;
  723. ctempdeletenode:=ttempdeletenode;
  724. end.
  725. {
  726. $Log$
  727. Revision 1.56 2003-06-09 18:26:46 peter
  728. * remove temptype, use tempinfo.temptype instead
  729. Revision 1.55 2003/06/09 12:20:47 peter
  730. * getposition added to retrieve the the current tai item
  731. Revision 1.54 2003/06/08 18:27:15 jonas
  732. + ability to change the location of a ttempref node with changelocation()
  733. method. Useful to use instead of copying the contents from one temp to
  734. another
  735. + some shortstring optimizations in tassignmentnode that avoid some
  736. copying (required some shortstring optimizations to be moved from
  737. resulttype to firstpass, because they work on callnodes and string
  738. addnodes are only changed to callnodes in the firstpass)
  739. * allow setting/changing the funcretnode of callnodes after the
  740. resulttypepass has been done, funcretnode is now a property
  741. (all of the above should have a quite big effect on callparatemp)
  742. Revision 1.53 2003/05/30 21:01:44 jonas
  743. - disabled "result := value; exit;" -> exit(value) optimization because
  744. a) it was wrong
  745. b) exit(value) works now exactly the same as that
  746. (it was only activated with -Or)
  747. Revision 1.52 2003/05/23 14:27:35 peter
  748. * remove some unit dependencies
  749. * current_procinfo changes to store more info
  750. Revision 1.51 2003/05/17 13:30:08 jonas
  751. * changed tt_persistant to tt_persistent :)
  752. * tempcreatenode now doesn't accept a boolean anymore for persistent
  753. temps, but a ttemptype, so you can also create ansistring temps etc
  754. Revision 1.50 2003/05/13 19:14:41 peter
  755. * failn removed
  756. * inherited result code check moven to pexpr
  757. Revision 1.49 2003/05/11 14:45:12 peter
  758. * tloadnode does not support objectsymtable,withsymtable anymore
  759. * withnode cleanup
  760. * direct with rewritten to use temprefnode
  761. Revision 1.48 2003/04/27 11:21:33 peter
  762. * aktprocdef renamed to current_procdef
  763. * procinfo renamed to current_procinfo
  764. * procinfo will now be stored in current_module so it can be
  765. cleaned up properly
  766. * gen_main_procsym changed to create_main_proc and release_main_proc
  767. to also generate a tprocinfo structure
  768. * fixed unit implicit initfinal
  769. Revision 1.47 2003/04/25 20:59:33 peter
  770. * removed funcretn,funcretsym, function result is now in varsym
  771. and aliases for result and function name are added using absolutesym
  772. * vs_hidden parameter for funcret passed in parameter
  773. * vs_hidden fixes
  774. * writenode changed to printnode and released from extdebug
  775. * -vp option added to generate a tree.log with the nodetree
  776. * nicer printnode for statements, callnode
  777. Revision 1.46 2002/04/25 20:15:39 florian
  778. * block nodes within expressions shouldn't release the used registers,
  779. fixed using a flag till the new rg is ready
  780. Revision 1.45 2003/04/23 08:41:34 jonas
  781. * fixed ttemprefnode.compare and .getcopy to take offset field into
  782. account
  783. Revision 1.44 2003/04/22 23:50:22 peter
  784. * firstpass uses expectloc
  785. * checks if there are differences between the expectloc and
  786. location.loc from secondpass in EXTDEBUG
  787. Revision 1.43 2003/04/21 15:00:22 jonas
  788. * fixed tstatementnode.det_resulttype and tststatementnode.pass_1
  789. * fixed some getcopy issues with ttemp*nodes
  790. Revision 1.42 2003/04/17 07:50:24 daniel
  791. * Some work on interference graph construction
  792. Revision 1.41 2003/04/12 14:53:59 jonas
  793. * ttempdeletenode.create now sets the nodetype to tempdeleten instead of
  794. temprefn
  795. Revision 1.40 2003/03/17 20:30:46 peter
  796. * errornode.mark_write added
  797. Revision 1.39 2003/01/03 12:15:55 daniel
  798. * Removed ifdefs around notifications
  799. ifdefs around for loop optimizations remain
  800. Revision 1.38 2002/11/27 02:37:12 peter
  801. * case statement inlining added
  802. * fixed inlining of write()
  803. * switched statementnode left and right parts so the statements are
  804. processed in the correct order when getcopy is used. This is
  805. required for tempnodes
  806. Revision 1.37 2002/11/25 17:43:17 peter
  807. * splitted defbase in defutil,symutil,defcmp
  808. * merged isconvertable and is_equal into compare_defs(_ext)
  809. * made operator search faster by walking the list only once
  810. Revision 1.36 2002/10/05 15:15:19 peter
  811. * don't complain in X- mode for internal generated function calls
  812. with funcretrefnode set
  813. * give statement error at the correct line position instead of the
  814. block begin
  815. Revision 1.35 2002/09/01 08:01:16 daniel
  816. * Removed sets from Tcallnode.det_resulttype
  817. + Added read/write notifications of variables. These will be usefull
  818. for providing information for several optimizations. For example
  819. the value of the loop variable of a for loop does matter is the
  820. variable is read after the for loop, but if it's no longer used
  821. or written, it doesn't matter and this can be used to optimize
  822. the loop code generation.
  823. Revision 1.34 2002/08/18 20:06:23 peter
  824. * inlining is now also allowed in interface
  825. * renamed write/load to ppuwrite/ppuload
  826. * tnode storing in ppu
  827. * nld,ncon,nbas are already updated for storing in ppu
  828. Revision 1.33 2002/08/17 22:09:44 florian
  829. * result type handling in tcgcal.pass_2 overhauled
  830. * better tnode.printnodetree
  831. * some ppc stuff fixed
  832. Revision 1.32 2002/08/17 09:23:34 florian
  833. * first part of procinfo rewrite
  834. Revision 1.31 2002/08/15 19:10:35 peter
  835. * first things tai,tnode storing in ppu
  836. Revision 1.30 2002/07/20 11:57:53 florian
  837. * types.pas renamed to defbase.pas because D6 contains a types
  838. unit so this would conflicts if D6 programms are compiled
  839. + Willamette/SSE2 instructions to assembler added
  840. Revision 1.29 2002/07/19 11:41:35 daniel
  841. * State tracker work
  842. * The whilen and repeatn are now completely unified into whilerepeatn. This
  843. allows the state tracker to change while nodes automatically into
  844. repeat nodes.
  845. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  846. 'not(a>b)' is optimized into 'a<=b'.
  847. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  848. by removing the notn and later switchting the true and falselabels. The
  849. same is done with 'repeat until not a'.
  850. Revision 1.28 2002/07/14 18:00:43 daniel
  851. + Added the beginning of a state tracker. This will track the values of
  852. variables through procedures and optimize things away.
  853. Revision 1.27 2002/07/01 18:46:22 peter
  854. * internal linker
  855. * reorganized aasm layer
  856. Revision 1.26 2002/06/24 12:43:00 jonas
  857. * fixed errors found with new -CR code from Peter when cycling with -O2p3r
  858. Revision 1.25 2002/05/18 13:34:09 peter
  859. * readded missing revisions
  860. Revision 1.24 2002/05/16 19:46:37 carl
  861. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  862. + try to fix temp allocation (still in ifdef)
  863. + generic constructor calls
  864. + start of tassembler / tmodulebase class cleanup
  865. Revision 1.22 2002/04/23 19:16:34 peter
  866. * add pinline unit that inserts compiler supported functions using
  867. one or more statements
  868. * moved finalize and setlength from ninl to pinline
  869. Revision 1.21 2002/04/21 19:02:03 peter
  870. * removed newn and disposen nodes, the code is now directly
  871. inlined from pexpr
  872. * -an option that will write the secondpass nodes to the .s file, this
  873. requires EXTDEBUG define to actually write the info
  874. * fixed various internal errors and crashes due recent code changes
  875. Revision 1.20 2002/04/04 19:05:57 peter
  876. * removed unused units
  877. * use tlocation.size in cg.a_*loc*() routines
  878. Revision 1.19 2002/03/31 20:26:33 jonas
  879. + a_loadfpu_* and a_loadmm_* methods in tcg
  880. * register allocation is now handled by a class and is mostly processor
  881. independent (+rgobj.pas and i386/rgcpu.pas)
  882. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  883. * some small improvements and fixes to the optimizer
  884. * some register allocation fixes
  885. * some fpuvaroffset fixes in the unary minus node
  886. * push/popusedregisters is now called rg.save/restoreusedregisters and
  887. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  888. also better optimizable)
  889. * fixed and optimized register saving/restoring for new/dispose nodes
  890. * LOC_FPU locations now also require their "register" field to be set to
  891. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  892. - list field removed of the tnode class because it's not used currently
  893. and can cause hard-to-find bugs
  894. }