nbas.pas 37 KB

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