nbas.pas 33 KB

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