nbas.pas 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. This unit implements some basic nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nbas;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. cpuinfo,cpubase,cgbase,cgutils,
  23. aasmbase,aasmtai,aasmcpu,
  24. node,
  25. symtype;
  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. { Used registers in assembler block }
  44. used_regs_int,
  45. used_regs_fpu : tcpuregisterset;
  46. constructor create(p : taasmoutput);virtual;
  47. constructor create_get_position;
  48. destructor destroy;override;
  49. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  50. procedure ppuwrite(ppufile:tcompilerppufile);override;
  51. procedure buildderefimpl;override;
  52. procedure derefimpl;override;
  53. function _getcopy : tnode;override;
  54. function pass_1 : tnode;override;
  55. function det_resulttype:tnode;override;
  56. function docompare(p: tnode): boolean; override;
  57. end;
  58. tasmnodeclass = class of tasmnode;
  59. tstatementnode = class(tbinarynode)
  60. constructor create(l,r : tnode);virtual;
  61. function pass_1 : tnode;override;
  62. function det_resulttype:tnode;override;
  63. procedure printnodetree(var t:text);override;
  64. end;
  65. tstatementnodeclass = class of tstatementnode;
  66. tblocknode = class(tunarynode)
  67. constructor create(l : tnode);virtual;
  68. destructor destroy; override;
  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. ttempcreatenode = class;
  77. { to allow access to the location by temp references even after the temp has }
  78. { already been disposed and to make sure the coherency between temps and }
  79. { temp references is kept after a getcopy }
  80. ptempinfo = ^ttempinfo;
  81. ttempinfo = record
  82. { set to the copy of a tempcreate pnode (if it gets copied) so that the }
  83. { refs and deletenode can hook to this copy once they get copied too }
  84. hookoncopy : ptempinfo;
  85. restype : ttype;
  86. temptype : ttemptype;
  87. owner : ttempcreatenode;
  88. may_be_in_reg : boolean;
  89. valid : boolean;
  90. nextref_set_hookoncopy_nil : boolean;
  91. location : tlocation;
  92. end;
  93. { a node which will create a (non)persistent temp of a given type with a given }
  94. { size (the size is separate to allow creating "void" temps with a custom size) }
  95. ttempcreatenode = class(tnode)
  96. size: aint;
  97. tempinfo: ptempinfo;
  98. { * persistent temps are used in manually written code where the temp }
  99. { be usable among different statements and where you can manually say }
  100. { when the temp has to be freed (using a ttempdeletenode) }
  101. { * non-persistent temps are mostly used in typeconversion helpers, }
  102. { where the node that receives the temp becomes responsible for }
  103. { freeing it. In this last case, you must use only one reference }
  104. { to it and *not* generate a ttempdeletenode }
  105. constructor create(const _restype: ttype; _size: aint; _temptype: ttemptype;allowreg:boolean); virtual;
  106. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  107. procedure ppuwrite(ppufile:tcompilerppufile);override;
  108. procedure buildderefimpl;override;
  109. procedure derefimpl;override;
  110. function _getcopy: tnode; override;
  111. function pass_1 : tnode; override;
  112. function det_resulttype: tnode; override;
  113. function docompare(p: tnode): boolean; override;
  114. procedure printnodedata(var t:text);override;
  115. end;
  116. ttempcreatenodeclass = class of ttempcreatenode;
  117. { a node which is a reference to a certain temp }
  118. ttemprefnode = class(tnode)
  119. constructor create(const temp: ttempcreatenode); virtual;
  120. constructor create_offset(const temp: ttempcreatenode;aoffset:longint);
  121. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  122. procedure ppuwrite(ppufile:tcompilerppufile);override;
  123. function _getcopy: tnode; override;
  124. procedure derefnode;override;
  125. function pass_1 : tnode; override;
  126. function det_resulttype : tnode; override;
  127. procedure mark_write;override;
  128. function docompare(p: tnode): boolean; override;
  129. protected
  130. tempinfo: ptempinfo;
  131. offset : longint;
  132. private
  133. tempidx : longint;
  134. end;
  135. ttemprefnodeclass = class of ttemprefnode;
  136. { a node which removes a temp }
  137. ttempdeletenode = class(tnode)
  138. constructor create(const temp: ttempcreatenode); virtual;
  139. { this will convert the persistant temp to a normal temp
  140. for returning to the other nodes }
  141. constructor create_normal_temp(const temp: ttempcreatenode);
  142. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  143. procedure ppuwrite(ppufile:tcompilerppufile);override;
  144. function _getcopy: tnode; override;
  145. procedure derefnode;override;
  146. function pass_1: tnode; override;
  147. function det_resulttype: tnode; override;
  148. function docompare(p: tnode): boolean; override;
  149. destructor destroy; override;
  150. protected
  151. tempinfo: ptempinfo;
  152. release_to_normal : boolean;
  153. private
  154. tempidx : longint;
  155. end;
  156. ttempdeletenodeclass = class of ttempdeletenode;
  157. var
  158. cnothingnode : tnothingnodeclass;
  159. cerrornode : terrornodeclass;
  160. casmnode : tasmnodeclass;
  161. cstatementnode : tstatementnodeclass;
  162. cblocknode : tblocknodeclass;
  163. ctempcreatenode : ttempcreatenodeclass;
  164. ctemprefnode : ttemprefnodeclass;
  165. ctempdeletenode : ttempdeletenodeclass;
  166. { Create a blocknode and statement node for multiple statements
  167. generated internally by the parser }
  168. function internalstatements(var laststatement:tstatementnode):tblocknode;
  169. function laststatement(block:tblocknode):tstatementnode;
  170. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  171. implementation
  172. uses
  173. cutils,
  174. verbose,globals,systems,
  175. symconst,symdef,defutil,defcmp,
  176. pass_1,
  177. nld,ncal,nflw,
  178. procinfo
  179. ;
  180. {*****************************************************************************
  181. Helpers
  182. *****************************************************************************}
  183. function internalstatements(var laststatement:tstatementnode):tblocknode;
  184. begin
  185. { create dummy initial statement }
  186. laststatement := cstatementnode.create(cnothingnode.create,nil);
  187. internalstatements := cblocknode.create(laststatement);
  188. end;
  189. function laststatement(block:tblocknode):tstatementnode;
  190. begin
  191. result:=tstatementnode(block.left);
  192. while assigned(result) and assigned(result.right) do
  193. result:=tstatementnode(result.right);
  194. end;
  195. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  196. begin
  197. if assigned(laststatement.right) then
  198. internalerror(200204201);
  199. laststatement.right:=cstatementnode.create(n,nil);
  200. laststatement:=tstatementnode(laststatement.right);
  201. end;
  202. {*****************************************************************************
  203. TFIRSTNOTHING
  204. *****************************************************************************}
  205. constructor tnothingnode.create;
  206. begin
  207. inherited create(nothingn);
  208. end;
  209. function tnothingnode.det_resulttype:tnode;
  210. begin
  211. result:=nil;
  212. resulttype:=voidtype;
  213. end;
  214. function tnothingnode.pass_1 : tnode;
  215. begin
  216. result:=nil;
  217. expectloc:=LOC_VOID;
  218. end;
  219. {*****************************************************************************
  220. TFIRSTERROR
  221. *****************************************************************************}
  222. constructor terrornode.create;
  223. begin
  224. inherited create(errorn);
  225. end;
  226. function terrornode.det_resulttype:tnode;
  227. begin
  228. result:=nil;
  229. include(flags,nf_error);
  230. codegenerror:=true;
  231. resulttype:=generrortype;
  232. end;
  233. function terrornode.pass_1 : tnode;
  234. begin
  235. result:=nil;
  236. expectloc:=LOC_VOID;
  237. codegenerror:=true;
  238. end;
  239. procedure terrornode.mark_write;
  240. begin
  241. end;
  242. {*****************************************************************************
  243. TSTATEMENTNODE
  244. *****************************************************************************}
  245. constructor tstatementnode.create(l,r : tnode);
  246. begin
  247. inherited create(statementn,l,r);
  248. end;
  249. function tstatementnode.det_resulttype:tnode;
  250. begin
  251. result:=nil;
  252. resulttype:=voidtype;
  253. { left is the statement itself calln assignn or a complex one }
  254. resulttypepass(left);
  255. if (not (cs_extsyntax in aktmoduleswitches)) and
  256. assigned(left.resulttype.def) and
  257. not((left.nodetype=calln) and
  258. { don't complain when funcretrefnode is set, because then the
  259. value is already used. And also not for constructors }
  260. (assigned(tcallnode(left).funcretnode) or
  261. (tcallnode(left).procdefinition.proctypeoption=potype_constructor))) and
  262. not(is_void(left.resulttype.def)) then
  263. CGMessage(parser_e_illegal_expression);
  264. if codegenerror then
  265. exit;
  266. { right is the next statement in the list }
  267. if assigned(right) then
  268. resulttypepass(right);
  269. if codegenerror then
  270. exit;
  271. end;
  272. function tstatementnode.pass_1 : tnode;
  273. begin
  274. result:=nil;
  275. { left is the statement itself calln assignn or a complex one }
  276. firstpass(left);
  277. if codegenerror then
  278. exit;
  279. expectloc:=left.expectloc;
  280. registersint:=left.registersint;
  281. registersfpu:=left.registersfpu;
  282. {$ifdef SUPPORT_MMX}
  283. registersmmx:=left.registersmmx;
  284. {$endif SUPPORT_MMX}
  285. { right is the next in the list }
  286. if assigned(right) then
  287. firstpass(right);
  288. if codegenerror then
  289. exit;
  290. end;
  291. procedure tstatementnode.printnodetree(var t:text);
  292. begin
  293. printnodelist(t);
  294. end;
  295. {*****************************************************************************
  296. TBLOCKNODE
  297. *****************************************************************************}
  298. constructor tblocknode.create(l : tnode);
  299. begin
  300. inherited create(blockn,l);
  301. end;
  302. destructor tblocknode.destroy;
  303. var
  304. hp, next: tstatementnode;
  305. begin
  306. hp := tstatementnode(left);
  307. left := nil;
  308. while assigned(hp) do
  309. begin
  310. next := tstatementnode(hp.right);
  311. hp.right := nil;
  312. hp.free;
  313. hp := next;
  314. end;
  315. inherited destroy;
  316. end;
  317. function tblocknode.det_resulttype:tnode;
  318. var
  319. hp : tstatementnode;
  320. begin
  321. result:=nil;
  322. resulttype:=voidtype;
  323. hp:=tstatementnode(left);
  324. while assigned(hp) do
  325. begin
  326. if assigned(hp.left) then
  327. begin
  328. codegenerror:=false;
  329. resulttypepass(hp.left);
  330. if not(codegenerror) and
  331. not(cs_extsyntax in aktmoduleswitches) and
  332. (hp.left.nodetype=calln) and
  333. not(is_void(hp.left.resulttype.def)) and
  334. not(cnf_return_value_used in tcallnode(hp.left).callnodeflags) and
  335. not((tcallnode(hp.left).procdefinition.proctypeoption=potype_constructor) and
  336. assigned(tprocdef(tcallnode(hp.left).procdefinition)._class) and
  337. is_object(tprocdef(tcallnode(hp.left).procdefinition)._class)) then
  338. CGMessagePos(hp.left.fileinfo,parser_e_illegal_expression);
  339. { the resulttype of the block is the last type that is
  340. returned. Normally this is a voidtype. But when the
  341. compiler inserts a block of multiple statements then the
  342. last entry can return a value }
  343. resulttype:=hp.left.resulttype;
  344. end;
  345. hp:=tstatementnode(hp.right);
  346. end;
  347. end;
  348. function tblocknode.pass_1 : tnode;
  349. var
  350. hp : tstatementnode;
  351. count : longint;
  352. begin
  353. result:=nil;
  354. expectloc:=LOC_VOID;
  355. count:=0;
  356. hp:=tstatementnode(left);
  357. while assigned(hp) do
  358. begin
  359. (*
  360. if cs_regvars in aktglobalswitches then
  361. begin
  362. { node transformations }
  363. { concat function result to exit }
  364. { this is wrong for string or other complex
  365. result types !!! }
  366. if {ret_in_acc(current_procinfo.procdef.rettype.def) and }
  367. (is_ordinal(current_procinfo.procdef.rettype.def) or
  368. is_smallset(current_procinfo.procdef.rettype.def)) and
  369. assigned(hp.right) and
  370. assigned(tstatementnode(hp.right).left) and
  371. (tstatementnode(hp.right).left.nodetype=exitn) and
  372. (hp.left.nodetype=assignn) and
  373. { !!!! this tbinarynode should be tassignmentnode }
  374. (tbinarynode(hp.left).left.nodetype=loadn) and
  375. (is_funcret_sym(tloadnode(tbinarynode(hp.left).left).symtableentry)) then
  376. begin
  377. if assigned(texitnode(tstatementnode(hp.right).left).left) then
  378. CGMessage(cg_n_inefficient_code)
  379. else
  380. begin
  381. texitnode(tstatementnode(hp.right).left).left:=tassignmentnode(hp.left).right;
  382. tassignmentnode(hp.left).right:=nil;
  383. hp.left.free;
  384. hp.left:=nil;
  385. end;
  386. end
  387. { warning if unreachable code occurs and elimate this }
  388. else if (hp.left.nodetype in
  389. [exitn,breakn,continuen,goton]) and
  390. { statement node (JM) }
  391. assigned(hp.right) and
  392. { kind of statement! (JM) }
  393. assigned(tstatementnode(hp.right).left) and
  394. (tstatementnode(hp.right).left.nodetype<>labeln) then
  395. begin
  396. { use correct line number }
  397. aktfilepos:=hp.right.fileinfo;
  398. hp.right.free;
  399. hp.right:=nil;
  400. CGMessage(cg_w_unreachable_code);
  401. { old lines }
  402. aktfilepos:=hp.left.fileinfo;
  403. end;
  404. end;
  405. *)
  406. if assigned(hp.left) then
  407. begin
  408. codegenerror:=false;
  409. firstpass(hp.left);
  410. hp.expectloc:=hp.left.expectloc;
  411. hp.registersint:=hp.left.registersint;
  412. hp.registersfpu:=hp.left.registersfpu;
  413. {$ifdef SUPPORT_MMX}
  414. hp.registersmmx:=hp.left.registersmmx;
  415. {$endif SUPPORT_MMX}
  416. end
  417. else
  418. hp.registersint:=0;
  419. if hp.registersint>registersint then
  420. registersint:=hp.registersint;
  421. if hp.registersfpu>registersfpu then
  422. registersfpu:=hp.registersfpu;
  423. {$ifdef SUPPORT_MMX}
  424. if hp.registersmmx>registersmmx then
  425. registersmmx:=hp.registersmmx;
  426. {$endif}
  427. expectloc:=hp.expectloc;
  428. inc(count);
  429. hp:=tstatementnode(hp.right);
  430. end;
  431. end;
  432. {$ifdef state_tracking}
  433. function Tblocknode.track_state_pass(exec_known:boolean):boolean;
  434. var hp:Tstatementnode;
  435. begin
  436. track_state_pass:=false;
  437. hp:=Tstatementnode(left);
  438. while assigned(hp) do
  439. begin
  440. if hp.left.track_state_pass(exec_known) then
  441. track_state_pass:=true;
  442. hp:=Tstatementnode(hp.right);
  443. end;
  444. end;
  445. {$endif state_tracking}
  446. {*****************************************************************************
  447. TASMNODE
  448. *****************************************************************************}
  449. constructor tasmnode.create(p : taasmoutput);
  450. begin
  451. inherited create(asmn);
  452. p_asm:=p;
  453. currenttai:=nil;
  454. used_regs_int:=[];
  455. used_regs_fpu:=[];
  456. end;
  457. constructor tasmnode.create_get_position;
  458. begin
  459. inherited create(asmn);
  460. p_asm:=nil;
  461. include(flags,nf_get_asm_position);
  462. currenttai:=nil;
  463. end;
  464. destructor tasmnode.destroy;
  465. begin
  466. if assigned(p_asm) then
  467. p_asm.free;
  468. inherited destroy;
  469. end;
  470. constructor tasmnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  471. var
  472. hp : tai;
  473. begin
  474. inherited ppuload(t,ppufile);
  475. if not(nf_get_asm_position in flags) then
  476. begin
  477. p_asm:=taasmoutput.create;
  478. repeat
  479. hp:=ppuloadai(ppufile);
  480. if hp=nil then
  481. break;
  482. p_asm.concat(hp);
  483. until false;
  484. end
  485. else
  486. p_asm:=nil;
  487. currenttai:=nil;
  488. end;
  489. procedure tasmnode.ppuwrite(ppufile:tcompilerppufile);
  490. var
  491. hp : tai;
  492. begin
  493. inherited ppuwrite(ppufile);
  494. {$warning FIXME Add saving of register sets}
  495. if not(nf_get_asm_position in flags) then
  496. begin
  497. hp:=tai(p_asm.first);
  498. while assigned(hp) do
  499. begin
  500. ppuwriteai(ppufile,hp);
  501. hp:=tai(hp.next);
  502. end;
  503. { end is marked by a nil }
  504. ppuwriteai(ppufile,nil);
  505. end;
  506. end;
  507. procedure tasmnode.buildderefimpl;
  508. var
  509. hp : tai;
  510. begin
  511. inherited buildderefimpl;
  512. if not(nf_get_asm_position in flags) then
  513. begin
  514. hp:=tai(p_asm.first);
  515. while assigned(hp) do
  516. begin
  517. hp.buildderefimpl;
  518. hp:=tai(hp.next);
  519. end;
  520. end;
  521. end;
  522. procedure tasmnode.derefimpl;
  523. var
  524. hp : tai;
  525. begin
  526. inherited derefimpl;
  527. if not(nf_get_asm_position in flags) then
  528. begin
  529. hp:=tai(p_asm.first);
  530. while assigned(hp) do
  531. begin
  532. hp.derefimpl;
  533. hp:=tai(hp.next);
  534. end;
  535. end;
  536. end;
  537. function tasmnode._getcopy: tnode;
  538. var
  539. n: tasmnode;
  540. begin
  541. n := tasmnode(inherited _getcopy);
  542. if assigned(p_asm) then
  543. begin
  544. n.p_asm:=taasmoutput.create;
  545. n.p_asm.concatlistcopy(p_asm);
  546. end
  547. else n.p_asm := nil;
  548. n.currenttai:=currenttai;
  549. result:=n;
  550. end;
  551. function tasmnode.det_resulttype:tnode;
  552. begin
  553. result:=nil;
  554. resulttype:=voidtype;
  555. if not(nf_get_asm_position in flags) then
  556. include(current_procinfo.flags,pi_has_assembler_block);
  557. end;
  558. function tasmnode.pass_1 : tnode;
  559. begin
  560. result:=nil;
  561. expectloc:=LOC_VOID;
  562. end;
  563. function tasmnode.docompare(p: tnode): boolean;
  564. begin
  565. { comparing of asmlists is not implemented (JM) }
  566. docompare := false;
  567. end;
  568. {*****************************************************************************
  569. TEMPCREATENODE
  570. *****************************************************************************}
  571. constructor ttempcreatenode.create(const _restype: ttype; _size: aint; _temptype: ttemptype;allowreg:boolean);
  572. begin
  573. inherited create(tempcreaten);
  574. size := _size;
  575. new(tempinfo);
  576. fillchar(tempinfo^,sizeof(tempinfo^),0);
  577. tempinfo^.restype := _restype;
  578. tempinfo^.temptype := _temptype;
  579. tempinfo^.owner:=self;
  580. tempinfo^.may_be_in_reg:=
  581. allowreg and
  582. { temp must fit a single register }
  583. (tstoreddef(_restype.def).is_fpuregable or
  584. (tstoreddef(_restype.def).is_intregable and
  585. (_size<=TCGSize2Size[OS_64]))) and
  586. { size of register operations must be known }
  587. (def_cgsize(_restype.def)<>OS_NO) and
  588. { no init/final needed }
  589. not (_restype.def.needs_inittable) and
  590. ((_restype.def.deftype <> pointerdef) or
  591. (not tpointerdef(_restype.def).pointertype.def.needs_inittable));
  592. end;
  593. function ttempcreatenode._getcopy: tnode;
  594. var
  595. n: ttempcreatenode;
  596. begin
  597. n := ttempcreatenode(inherited _getcopy);
  598. n.size := size;
  599. new(n.tempinfo);
  600. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  601. n.tempinfo^.owner:=n;
  602. n.tempinfo^.restype := tempinfo^.restype;
  603. n.tempinfo^.temptype := tempinfo^.temptype;
  604. { when the tempinfo has already a hookoncopy then it is not
  605. reset by a tempdeletenode }
  606. if assigned(tempinfo^.hookoncopy) then
  607. internalerror(200211262);
  608. { signal the temprefs that the temp they point to has been copied, }
  609. { so that if the refs get copied as well, they can hook themselves }
  610. { to the copy of the temp }
  611. tempinfo^.hookoncopy := n.tempinfo;
  612. tempinfo^.nextref_set_hookoncopy_nil := false;
  613. result := n;
  614. end;
  615. constructor ttempcreatenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  616. begin
  617. inherited ppuload(t,ppufile);
  618. size:=ppufile.getlongint;
  619. new(tempinfo);
  620. fillchar(tempinfo^,sizeof(tempinfo^),0);
  621. tempinfo^.may_be_in_reg:=boolean(ppufile.getbyte);
  622. ppufile.gettype(tempinfo^.restype);
  623. tempinfo^.temptype := ttemptype(ppufile.getbyte);
  624. tempinfo^.owner:=self;
  625. end;
  626. procedure ttempcreatenode.ppuwrite(ppufile:tcompilerppufile);
  627. begin
  628. inherited ppuwrite(ppufile);
  629. ppufile.putlongint(size);
  630. ppufile.putbyte(byte(tempinfo^.may_be_in_reg));
  631. ppufile.puttype(tempinfo^.restype);
  632. ppufile.putbyte(byte(tempinfo^.temptype));
  633. end;
  634. procedure ttempcreatenode.buildderefimpl;
  635. begin
  636. tempinfo^.restype.buildderef;
  637. end;
  638. procedure ttempcreatenode.derefimpl;
  639. begin
  640. tempinfo^.restype.resolve;
  641. end;
  642. function ttempcreatenode.pass_1 : tnode;
  643. begin
  644. result := nil;
  645. expectloc:=LOC_VOID;
  646. if (tempinfo^.restype.def.needs_inittable) then
  647. include(current_procinfo.flags,pi_needs_implicit_finally);
  648. end;
  649. function ttempcreatenode.det_resulttype: tnode;
  650. begin
  651. result := nil;
  652. { a tempcreatenode doesn't have a resulttype, only temprefnodes do }
  653. resulttype := voidtype;
  654. end;
  655. function ttempcreatenode.docompare(p: tnode): boolean;
  656. begin
  657. result :=
  658. inherited docompare(p) and
  659. (ttempcreatenode(p).size = size) and
  660. (ttempcreatenode(p).tempinfo^.may_be_in_reg = tempinfo^.may_be_in_reg) and
  661. equal_defs(ttempcreatenode(p).tempinfo^.restype.def,tempinfo^.restype.def);
  662. end;
  663. procedure ttempcreatenode.printnodedata(var t:text);
  664. begin
  665. inherited printnodedata(t);
  666. writeln(t,printnodeindention,'size = ',size);
  667. end;
  668. {*****************************************************************************
  669. TEMPREFNODE
  670. *****************************************************************************}
  671. constructor ttemprefnode.create(const temp: ttempcreatenode);
  672. begin
  673. inherited create(temprefn);
  674. tempinfo := temp.tempinfo;
  675. offset:=0;
  676. end;
  677. constructor ttemprefnode.create_offset(const temp: ttempcreatenode;aoffset:longint);
  678. begin
  679. self.create(temp);
  680. offset := aoffset;
  681. end;
  682. function ttemprefnode._getcopy: tnode;
  683. var
  684. n: ttemprefnode;
  685. begin
  686. n := ttemprefnode(inherited _getcopy);
  687. n.offset := offset;
  688. if assigned(tempinfo^.hookoncopy) then
  689. { if the temp has been copied, assume it becomes a new }
  690. { temp which has to be hooked by the copied reference }
  691. begin
  692. { hook the ref to the copied temp }
  693. n.tempinfo := tempinfo^.hookoncopy;
  694. { if we passed a ttempdeletenode that changed the temp }
  695. { from a persistent one into a normal one, we must be }
  696. { the last reference (since our parent should free the }
  697. { temp (JM) }
  698. if (tempinfo^.nextref_set_hookoncopy_nil) then
  699. tempinfo^.hookoncopy := nil;
  700. end
  701. else
  702. { if the temp we refer to hasn't been copied, assume }
  703. { we're just a new reference to that temp }
  704. begin
  705. n.tempinfo := tempinfo;
  706. end;
  707. result := n;
  708. end;
  709. constructor ttemprefnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  710. begin
  711. inherited ppuload(t,ppufile);
  712. tempidx:=ppufile.getlongint;
  713. offset:=ppufile.getlongint;
  714. end;
  715. procedure ttemprefnode.ppuwrite(ppufile:tcompilerppufile);
  716. begin
  717. inherited ppuwrite(ppufile);
  718. ppufile.putlongint(tempinfo^.owner.ppuidx);
  719. ppufile.putlongint(offset);
  720. end;
  721. procedure ttemprefnode.derefnode;
  722. var
  723. temp : ttempcreatenode;
  724. begin
  725. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  726. if temp.nodetype<>tempcreaten then
  727. internalerror(200311075);
  728. tempinfo:=temp.tempinfo;
  729. end;
  730. function ttemprefnode.pass_1 : tnode;
  731. begin
  732. expectloc := LOC_REFERENCE;
  733. if not tempinfo^.restype.def.needs_inittable and
  734. tempinfo^.may_be_in_reg then
  735. begin
  736. if tempinfo^.restype.def.deftype=floatdef then
  737. begin
  738. if (tempinfo^.temptype = tt_persistent) then
  739. expectloc := LOC_CFPUREGISTER
  740. else
  741. expectloc := LOC_FPUREGISTER;
  742. end
  743. else
  744. begin
  745. if (tempinfo^.temptype = tt_persistent) then
  746. expectloc := LOC_CREGISTER
  747. else
  748. expectloc := LOC_REGISTER;
  749. end;
  750. end;
  751. result := nil;
  752. end;
  753. function ttemprefnode.det_resulttype: tnode;
  754. begin
  755. { check if the temp is already resulttype passed }
  756. if not assigned(tempinfo^.restype.def) then
  757. internalerror(200108233);
  758. result := nil;
  759. resulttype := tempinfo^.restype;
  760. end;
  761. function ttemprefnode.docompare(p: tnode): boolean;
  762. begin
  763. result :=
  764. inherited docompare(p) and
  765. (ttemprefnode(p).tempinfo = tempinfo) and
  766. (ttemprefnode(p).offset = offset);
  767. end;
  768. procedure Ttemprefnode.mark_write;
  769. begin
  770. include(flags,nf_write);
  771. end;
  772. {*****************************************************************************
  773. TEMPDELETENODE
  774. *****************************************************************************}
  775. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  776. begin
  777. inherited create(tempdeleten);
  778. tempinfo := temp.tempinfo;
  779. release_to_normal := false;
  780. end;
  781. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  782. begin
  783. inherited create(tempdeleten);
  784. tempinfo := temp.tempinfo;
  785. release_to_normal := true;
  786. if tempinfo^.temptype <> tt_persistent then
  787. internalerror(200204211);
  788. end;
  789. function ttempdeletenode._getcopy: tnode;
  790. var
  791. n: ttempdeletenode;
  792. begin
  793. n := ttempdeletenode(inherited _getcopy);
  794. n.release_to_normal := release_to_normal;
  795. if assigned(tempinfo^.hookoncopy) then
  796. { if the temp has been copied, assume it becomes a new }
  797. { temp which has to be hooked by the copied deletenode }
  798. begin
  799. { hook the tempdeletenode to the copied temp }
  800. n.tempinfo := tempinfo^.hookoncopy;
  801. { the temp shall not be used, reset hookoncopy }
  802. { Only if release_to_normal is false, otherwise }
  803. { the temp can still be referenced once more (JM) }
  804. if (not release_to_normal) then
  805. tempinfo^.hookoncopy:=nil
  806. else
  807. tempinfo^.nextref_set_hookoncopy_nil := true;
  808. end
  809. else
  810. { if the temp we refer to hasn't been copied, we have a }
  811. { problem since that means we now have two delete nodes }
  812. { for one temp }
  813. internalerror(200108234);
  814. result := n;
  815. end;
  816. constructor ttempdeletenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  817. begin
  818. inherited ppuload(t,ppufile);
  819. tempidx:=ppufile.getlongint;
  820. release_to_normal:=(ppufile.getbyte<>0);
  821. end;
  822. procedure ttempdeletenode.ppuwrite(ppufile:tcompilerppufile);
  823. begin
  824. inherited ppuwrite(ppufile);
  825. ppufile.putlongint(tempinfo^.owner.ppuidx);
  826. ppufile.putbyte(byte(release_to_normal));
  827. end;
  828. procedure ttempdeletenode.derefnode;
  829. var
  830. temp : ttempcreatenode;
  831. begin
  832. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  833. if temp.nodetype<>tempcreaten then
  834. internalerror(200311075);
  835. tempinfo:=temp.tempinfo;
  836. end;
  837. function ttempdeletenode.pass_1 : tnode;
  838. begin
  839. expectloc:=LOC_VOID;
  840. result := nil;
  841. end;
  842. function ttempdeletenode.det_resulttype: tnode;
  843. begin
  844. result := nil;
  845. resulttype := voidtype;
  846. end;
  847. function ttempdeletenode.docompare(p: tnode): boolean;
  848. begin
  849. result :=
  850. inherited docompare(p) and
  851. (ttemprefnode(p).tempinfo = tempinfo);
  852. end;
  853. destructor ttempdeletenode.destroy;
  854. begin
  855. dispose(tempinfo);
  856. end;
  857. begin
  858. cnothingnode:=tnothingnode;
  859. cerrornode:=terrornode;
  860. casmnode:=tasmnode;
  861. cstatementnode:=tstatementnode;
  862. cblocknode:=tblocknode;
  863. ctempcreatenode:=ttempcreatenode;
  864. ctemprefnode:=ttemprefnode;
  865. ctempdeletenode:=ttempdeletenode;
  866. end.