nbas.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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,aasmdata,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 : TAsmList;
  42. currenttai : tai;
  43. { Used registers in assembler block }
  44. used_regs_int,
  45. used_regs_fpu : tcpuregisterset;
  46. constructor create(p : TAsmList);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. if assigned(hp.left) then
  362. begin
  363. codegenerror:=false;
  364. firstpass(hp.left);
  365. hp.expectloc:=hp.left.expectloc;
  366. hp.registersint:=hp.left.registersint;
  367. hp.registersfpu:=hp.left.registersfpu;
  368. {$ifdef SUPPORT_MMX}
  369. hp.registersmmx:=hp.left.registersmmx;
  370. {$endif SUPPORT_MMX}
  371. end
  372. else
  373. hp.registersint:=0;
  374. if hp.registersint>registersint then
  375. registersint:=hp.registersint;
  376. if hp.registersfpu>registersfpu then
  377. registersfpu:=hp.registersfpu;
  378. {$ifdef SUPPORT_MMX}
  379. if hp.registersmmx>registersmmx then
  380. registersmmx:=hp.registersmmx;
  381. {$endif}
  382. expectloc:=hp.expectloc;
  383. inc(count);
  384. hp:=tstatementnode(hp.right);
  385. end;
  386. end;
  387. {$ifdef state_tracking}
  388. function Tblocknode.track_state_pass(exec_known:boolean):boolean;
  389. var hp:Tstatementnode;
  390. begin
  391. track_state_pass:=false;
  392. hp:=Tstatementnode(left);
  393. while assigned(hp) do
  394. begin
  395. if hp.left.track_state_pass(exec_known) then
  396. track_state_pass:=true;
  397. hp:=Tstatementnode(hp.right);
  398. end;
  399. end;
  400. {$endif state_tracking}
  401. {*****************************************************************************
  402. TASMNODE
  403. *****************************************************************************}
  404. constructor tasmnode.create(p : TAsmList);
  405. begin
  406. inherited create(asmn);
  407. p_asm:=p;
  408. currenttai:=nil;
  409. used_regs_int:=[];
  410. used_regs_fpu:=[];
  411. end;
  412. constructor tasmnode.create_get_position;
  413. begin
  414. inherited create(asmn);
  415. p_asm:=nil;
  416. include(flags,nf_get_asm_position);
  417. currenttai:=nil;
  418. end;
  419. destructor tasmnode.destroy;
  420. begin
  421. if assigned(p_asm) then
  422. p_asm.free;
  423. inherited destroy;
  424. end;
  425. constructor tasmnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  426. var
  427. hp : tai;
  428. begin
  429. inherited ppuload(t,ppufile);
  430. if not(nf_get_asm_position in flags) then
  431. begin
  432. p_asm:=TAsmList.create;
  433. repeat
  434. hp:=ppuloadai(ppufile);
  435. if hp=nil then
  436. break;
  437. p_asm.concat(hp);
  438. until false;
  439. end
  440. else
  441. p_asm:=nil;
  442. currenttai:=nil;
  443. end;
  444. procedure tasmnode.ppuwrite(ppufile:tcompilerppufile);
  445. var
  446. hp : tai;
  447. begin
  448. inherited ppuwrite(ppufile);
  449. {$warning FIXME Add saving of register sets}
  450. if not(nf_get_asm_position in flags) then
  451. begin
  452. hp:=tai(p_asm.first);
  453. while assigned(hp) do
  454. begin
  455. ppuwriteai(ppufile,hp);
  456. hp:=tai(hp.next);
  457. end;
  458. { end is marked by a nil }
  459. ppuwriteai(ppufile,nil);
  460. end;
  461. end;
  462. procedure tasmnode.buildderefimpl;
  463. var
  464. hp : tai;
  465. begin
  466. inherited buildderefimpl;
  467. if not(nf_get_asm_position in flags) then
  468. begin
  469. hp:=tai(p_asm.first);
  470. while assigned(hp) do
  471. begin
  472. hp.buildderefimpl;
  473. hp:=tai(hp.next);
  474. end;
  475. end;
  476. end;
  477. procedure tasmnode.derefimpl;
  478. var
  479. hp : tai;
  480. begin
  481. inherited derefimpl;
  482. if not(nf_get_asm_position in flags) then
  483. begin
  484. hp:=tai(p_asm.first);
  485. while assigned(hp) do
  486. begin
  487. hp.derefimpl;
  488. hp:=tai(hp.next);
  489. end;
  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:=TAsmList.create;
  500. n.p_asm.concatlistcopy(p_asm);
  501. end
  502. else n.p_asm := nil;
  503. n.currenttai:=currenttai;
  504. result:=n;
  505. end;
  506. function tasmnode.det_resulttype:tnode;
  507. begin
  508. result:=nil;
  509. resulttype:=voidtype;
  510. if not(nf_get_asm_position in flags) then
  511. include(current_procinfo.flags,pi_has_assembler_block);
  512. end;
  513. function tasmnode.pass_1 : tnode;
  514. begin
  515. result:=nil;
  516. expectloc:=LOC_VOID;
  517. end;
  518. function tasmnode.docompare(p: tnode): boolean;
  519. begin
  520. { comparing of asmlists is not implemented (JM) }
  521. docompare := false;
  522. end;
  523. {*****************************************************************************
  524. TEMPCREATENODE
  525. *****************************************************************************}
  526. constructor ttempcreatenode.create(const _restype: ttype; _size: aint; _temptype: ttemptype;allowreg:boolean);
  527. begin
  528. inherited create(tempcreaten);
  529. size := _size;
  530. new(tempinfo);
  531. fillchar(tempinfo^,sizeof(tempinfo^),0);
  532. tempinfo^.restype := _restype;
  533. tempinfo^.temptype := _temptype;
  534. tempinfo^.owner:=self;
  535. tempinfo^.may_be_in_reg:=
  536. allowreg and
  537. { temp must fit a single register }
  538. (tstoreddef(_restype.def).is_fpuregable or
  539. (tstoreddef(_restype.def).is_intregable and
  540. (_size<=TCGSize2Size[OS_64]))) and
  541. { size of register operations must be known }
  542. (def_cgsize(_restype.def)<>OS_NO) and
  543. { no init/final needed }
  544. not (_restype.def.needs_inittable) and
  545. ((_restype.def.deftype <> pointerdef) or
  546. (not tpointerdef(_restype.def).pointertype.def.needs_inittable));
  547. end;
  548. function ttempcreatenode._getcopy: tnode;
  549. var
  550. n: ttempcreatenode;
  551. begin
  552. n := ttempcreatenode(inherited _getcopy);
  553. n.size := size;
  554. new(n.tempinfo);
  555. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  556. n.tempinfo^.owner:=n;
  557. n.tempinfo^.restype := tempinfo^.restype;
  558. n.tempinfo^.temptype := tempinfo^.temptype;
  559. { when the tempinfo has already a hookoncopy then it is not
  560. reset by a tempdeletenode }
  561. if assigned(tempinfo^.hookoncopy) then
  562. internalerror(200211262);
  563. { signal the temprefs that the temp they point to has been copied, }
  564. { so that if the refs get copied as well, they can hook themselves }
  565. { to the copy of the temp }
  566. tempinfo^.hookoncopy := n.tempinfo;
  567. tempinfo^.nextref_set_hookoncopy_nil := false;
  568. result := n;
  569. end;
  570. constructor ttempcreatenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  571. begin
  572. inherited ppuload(t,ppufile);
  573. size:=ppufile.getlongint;
  574. new(tempinfo);
  575. fillchar(tempinfo^,sizeof(tempinfo^),0);
  576. tempinfo^.may_be_in_reg:=boolean(ppufile.getbyte);
  577. ppufile.gettype(tempinfo^.restype);
  578. tempinfo^.temptype := ttemptype(ppufile.getbyte);
  579. tempinfo^.owner:=self;
  580. end;
  581. procedure ttempcreatenode.ppuwrite(ppufile:tcompilerppufile);
  582. begin
  583. inherited ppuwrite(ppufile);
  584. ppufile.putlongint(size);
  585. ppufile.putbyte(byte(tempinfo^.may_be_in_reg));
  586. ppufile.puttype(tempinfo^.restype);
  587. ppufile.putbyte(byte(tempinfo^.temptype));
  588. end;
  589. procedure ttempcreatenode.buildderefimpl;
  590. begin
  591. tempinfo^.restype.buildderef;
  592. end;
  593. procedure ttempcreatenode.derefimpl;
  594. begin
  595. tempinfo^.restype.resolve;
  596. end;
  597. function ttempcreatenode.pass_1 : tnode;
  598. begin
  599. result := nil;
  600. expectloc:=LOC_VOID;
  601. if (tempinfo^.restype.def.needs_inittable) then
  602. include(current_procinfo.flags,pi_needs_implicit_finally);
  603. end;
  604. function ttempcreatenode.det_resulttype: tnode;
  605. begin
  606. result := nil;
  607. { a tempcreatenode doesn't have a resulttype, only temprefnodes do }
  608. resulttype := voidtype;
  609. end;
  610. function ttempcreatenode.docompare(p: tnode): boolean;
  611. begin
  612. result :=
  613. inherited docompare(p) and
  614. (ttempcreatenode(p).size = size) and
  615. (ttempcreatenode(p).tempinfo^.may_be_in_reg = tempinfo^.may_be_in_reg) and
  616. equal_defs(ttempcreatenode(p).tempinfo^.restype.def,tempinfo^.restype.def);
  617. end;
  618. procedure ttempcreatenode.printnodedata(var t:text);
  619. begin
  620. inherited printnodedata(t);
  621. writeln(t,printnodeindention,'size = ',size,', temprestype = "',tempinfo^.restype.def.gettypename,'", tempinfo = $',hexstr(ptruint(tempinfo),sizeof(ptruint)*2));
  622. end;
  623. {*****************************************************************************
  624. TEMPREFNODE
  625. *****************************************************************************}
  626. constructor ttemprefnode.create(const temp: ttempcreatenode);
  627. begin
  628. inherited create(temprefn);
  629. tempinfo := temp.tempinfo;
  630. offset:=0;
  631. end;
  632. constructor ttemprefnode.create_offset(const temp: ttempcreatenode;aoffset:longint);
  633. begin
  634. self.create(temp);
  635. offset := aoffset;
  636. end;
  637. function ttemprefnode._getcopy: tnode;
  638. var
  639. n: ttemprefnode;
  640. begin
  641. n := ttemprefnode(inherited _getcopy);
  642. n.offset := offset;
  643. if assigned(tempinfo^.hookoncopy) then
  644. { if the temp has been copied, assume it becomes a new }
  645. { temp which has to be hooked by the copied reference }
  646. begin
  647. { hook the ref to the copied temp }
  648. n.tempinfo := tempinfo^.hookoncopy;
  649. { if we passed a ttempdeletenode that changed the temp }
  650. { from a persistent one into a normal one, we must be }
  651. { the last reference (since our parent should free the }
  652. { temp (JM) }
  653. if (tempinfo^.nextref_set_hookoncopy_nil) then
  654. tempinfo^.hookoncopy := nil;
  655. end
  656. else
  657. { if the temp we refer to hasn't been copied, assume }
  658. { we're just a new reference to that temp }
  659. begin
  660. n.tempinfo := tempinfo;
  661. end;
  662. if not assigned(n.tempinfo) then
  663. internalerror(2005071901);
  664. result := n;
  665. end;
  666. constructor ttemprefnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  667. begin
  668. inherited ppuload(t,ppufile);
  669. tempidx:=ppufile.getlongint;
  670. offset:=ppufile.getlongint;
  671. end;
  672. procedure ttemprefnode.ppuwrite(ppufile:tcompilerppufile);
  673. begin
  674. inherited ppuwrite(ppufile);
  675. ppufile.putlongint(tempinfo^.owner.ppuidx);
  676. ppufile.putlongint(offset);
  677. end;
  678. procedure ttemprefnode.derefnode;
  679. var
  680. temp : ttempcreatenode;
  681. begin
  682. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  683. if temp.nodetype<>tempcreaten then
  684. internalerror(200311075);
  685. tempinfo:=temp.tempinfo;
  686. end;
  687. function ttemprefnode.pass_1 : tnode;
  688. begin
  689. expectloc := LOC_REFERENCE;
  690. if not tempinfo^.restype.def.needs_inittable and
  691. tempinfo^.may_be_in_reg then
  692. begin
  693. if tempinfo^.restype.def.deftype=floatdef then
  694. begin
  695. if (tempinfo^.temptype = tt_persistent) then
  696. expectloc := LOC_CFPUREGISTER
  697. else
  698. expectloc := LOC_FPUREGISTER;
  699. end
  700. else
  701. begin
  702. if (tempinfo^.temptype = tt_persistent) then
  703. expectloc := LOC_CREGISTER
  704. else
  705. expectloc := LOC_REGISTER;
  706. end;
  707. end;
  708. result := nil;
  709. end;
  710. function ttemprefnode.det_resulttype: tnode;
  711. begin
  712. { check if the temp is already resulttype passed }
  713. if not assigned(tempinfo^.restype.def) then
  714. internalerror(200108233);
  715. result := nil;
  716. resulttype := tempinfo^.restype;
  717. end;
  718. function ttemprefnode.docompare(p: tnode): boolean;
  719. begin
  720. result :=
  721. inherited docompare(p) and
  722. (ttemprefnode(p).tempinfo = tempinfo) and
  723. (ttemprefnode(p).offset = offset);
  724. end;
  725. procedure Ttemprefnode.mark_write;
  726. begin
  727. include(flags,nf_write);
  728. end;
  729. procedure ttemprefnode.printnodedata(var t:text);
  730. begin
  731. inherited printnodedata(t);
  732. writeln(t,printnodeindention,'temprestype = "',tempinfo^.restype.def.gettypename,'", tempinfo = $',hexstr(ptruint(tempinfo),sizeof(ptruint)*2));
  733. end;
  734. {*****************************************************************************
  735. TEMPDELETENODE
  736. *****************************************************************************}
  737. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  738. begin
  739. inherited create(tempdeleten);
  740. tempinfo := temp.tempinfo;
  741. release_to_normal := false;
  742. end;
  743. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  744. begin
  745. inherited create(tempdeleten);
  746. tempinfo := temp.tempinfo;
  747. release_to_normal := true;
  748. if tempinfo^.temptype <> tt_persistent then
  749. internalerror(200204211);
  750. end;
  751. function ttempdeletenode._getcopy: tnode;
  752. var
  753. n: ttempdeletenode;
  754. begin
  755. n := ttempdeletenode(inherited _getcopy);
  756. n.release_to_normal := release_to_normal;
  757. if assigned(tempinfo^.hookoncopy) then
  758. { if the temp has been copied, assume it becomes a new }
  759. { temp which has to be hooked by the copied deletenode }
  760. begin
  761. { hook the tempdeletenode to the copied temp }
  762. n.tempinfo := tempinfo^.hookoncopy;
  763. { the temp shall not be used, reset hookoncopy }
  764. { Only if release_to_normal is false, otherwise }
  765. { the temp can still be referenced once more (JM) }
  766. if (not release_to_normal) then
  767. tempinfo^.hookoncopy:=nil
  768. else
  769. tempinfo^.nextref_set_hookoncopy_nil := true;
  770. end
  771. else
  772. { if the temp we refer to hasn't been copied, we have a }
  773. { problem since that means we now have two delete nodes }
  774. { for one temp }
  775. internalerror(200108234);
  776. result := n;
  777. end;
  778. constructor ttempdeletenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  779. begin
  780. inherited ppuload(t,ppufile);
  781. tempidx:=ppufile.getlongint;
  782. release_to_normal:=(ppufile.getbyte<>0);
  783. end;
  784. procedure ttempdeletenode.ppuwrite(ppufile:tcompilerppufile);
  785. begin
  786. inherited ppuwrite(ppufile);
  787. ppufile.putlongint(tempinfo^.owner.ppuidx);
  788. ppufile.putbyte(byte(release_to_normal));
  789. end;
  790. procedure ttempdeletenode.derefnode;
  791. var
  792. temp : ttempcreatenode;
  793. begin
  794. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  795. if temp.nodetype<>tempcreaten then
  796. internalerror(200311075);
  797. tempinfo:=temp.tempinfo;
  798. end;
  799. function ttempdeletenode.pass_1 : tnode;
  800. begin
  801. expectloc:=LOC_VOID;
  802. result := nil;
  803. end;
  804. function ttempdeletenode.det_resulttype: tnode;
  805. begin
  806. result := nil;
  807. resulttype := voidtype;
  808. end;
  809. function ttempdeletenode.docompare(p: tnode): boolean;
  810. begin
  811. result :=
  812. inherited docompare(p) and
  813. (ttemprefnode(p).tempinfo = tempinfo);
  814. end;
  815. destructor ttempdeletenode.destroy;
  816. begin
  817. dispose(tempinfo);
  818. end;
  819. procedure ttempdeletenode.printnodedata(var t:text);
  820. begin
  821. inherited printnodedata(t);
  822. writeln(t,printnodeindention,'release_to_normal: ',release_to_normal,', temprestype = "',tempinfo^.restype.def.gettypename,'", tempinfo = $',hexstr(ptruint(tempinfo),sizeof(ptruint)*2));
  823. end;
  824. begin
  825. cnothingnode:=tnothingnode;
  826. cerrornode:=terrornode;
  827. casmnode:=tasmnode;
  828. cstatementnode:=tstatementnode;
  829. cblocknode:=tblocknode;
  830. ctempcreatenode:=ttempcreatenode;
  831. ctemprefnode:=ttemprefnode;
  832. ctempdeletenode:=ttempdeletenode;
  833. end.