nbas.pas 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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 pass_typecheck: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 pass_typecheck: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 dogetcopy : tnode;override;
  54. function pass_1 : tnode;override;
  55. function pass_typecheck: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 simplify : tnode; override;
  62. function pass_1 : tnode;override;
  63. function pass_typecheck:tnode;override;
  64. procedure printnodetree(var t:text);override;
  65. property statement : tnode read left write left;
  66. property next : tnode read right write right;
  67. end;
  68. tstatementnodeclass = class of tstatementnode;
  69. tblocknode = class(tunarynode)
  70. constructor create(l : tnode);virtual;
  71. destructor destroy; override;
  72. function simplify : tnode; override;
  73. function pass_1 : tnode;override;
  74. function pass_typecheck:tnode;override;
  75. {$ifdef state_tracking}
  76. function track_state_pass(exec_known:boolean):boolean;override;
  77. {$endif state_tracking}
  78. property statements : tnode read left write left;
  79. end;
  80. tblocknodeclass = class of tblocknode;
  81. ttempcreatenode = class;
  82. ttempinfoflag = (ti_may_be_in_reg,ti_valid,ti_nextref_set_hookoncopy_nil,ti_addr_taken);
  83. ttempinfoflags = set of ttempinfoflag;
  84. const
  85. tempinfostoreflags = [ti_may_be_in_reg,ti_addr_taken];
  86. type
  87. { to allow access to the location by temp references even after the temp has }
  88. { already been disposed and to make sure the coherency between temps and }
  89. { temp references is kept after a getcopy }
  90. ptempinfo = ^ttempinfo;
  91. ttempinfo = record
  92. { set to the copy of a tempcreate pnode (if it gets copied) so that the }
  93. { refs and deletenode can hook to this copy once they get copied too }
  94. hookoncopy : ptempinfo;
  95. typedef : tdef;
  96. typedefderef : tderef;
  97. temptype : ttemptype;
  98. owner : ttempcreatenode;
  99. withnode : tnode;
  100. location : tlocation;
  101. flags : ttempinfoflags;
  102. end;
  103. { a node which will create a (non)persistent temp of a given type with a given }
  104. { size (the size is separate to allow creating "void" temps with a custom size) }
  105. ttempcreatenode = class(tnode)
  106. size: aint;
  107. tempinfo: ptempinfo;
  108. { * persistent temps are used in manually written code where the temp }
  109. { be usable among different statements and where you can manually say }
  110. { when the temp has to be freed (using a ttempdeletenode) }
  111. { * non-persistent temps are mostly used in typeconversion helpers, }
  112. { where the node that receives the temp becomes responsible for }
  113. { freeing it. In this last case, you must use only one reference }
  114. { to it and *not* generate a ttempdeletenode }
  115. constructor create(_typedef: tdef; _size: aint; _temptype: ttemptype;allowreg:boolean); virtual;
  116. constructor create_withnode(_typedef: tdef; _size: aint; _temptype: ttemptype; allowreg:boolean; withnode: tnode); virtual;
  117. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  118. procedure ppuwrite(ppufile:tcompilerppufile);override;
  119. procedure buildderefimpl;override;
  120. procedure derefimpl;override;
  121. procedure derefnode;override;
  122. function dogetcopy: tnode; override;
  123. function pass_1 : tnode; override;
  124. function pass_typecheck: tnode; override;
  125. function docompare(p: tnode): boolean; override;
  126. procedure printnodedata(var t:text);override;
  127. end;
  128. ttempcreatenodeclass = class of ttempcreatenode;
  129. { a node which is a reference to a certain temp }
  130. ttemprefnode = class(tnode)
  131. tempinfo: ptempinfo;
  132. constructor create(const temp: ttempcreatenode); virtual;
  133. constructor create_offset(const temp: ttempcreatenode;aoffset:longint);
  134. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  135. procedure ppuwrite(ppufile:tcompilerppufile);override;
  136. function dogetcopy: tnode; override;
  137. procedure derefnode;override;
  138. function pass_1 : tnode; override;
  139. function pass_typecheck : tnode; override;
  140. procedure mark_write;override;
  141. function docompare(p: tnode): boolean; override;
  142. procedure printnodedata(var t:text);override;
  143. protected
  144. offset : longint;
  145. private
  146. tempidx : longint;
  147. end;
  148. ttemprefnodeclass = class of ttemprefnode;
  149. { a node which removes a temp }
  150. ttempdeletenode = class(tnode)
  151. tempinfo: ptempinfo;
  152. constructor create(const temp: ttempcreatenode); virtual;
  153. { this will convert the persistant temp to a normal temp
  154. for returning to the other nodes }
  155. constructor create_normal_temp(const temp: ttempcreatenode);
  156. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  157. procedure ppuwrite(ppufile:tcompilerppufile);override;
  158. function dogetcopy: tnode; override;
  159. procedure derefnode;override;
  160. function pass_1: tnode; override;
  161. function pass_typecheck: tnode; override;
  162. function docompare(p: tnode): boolean; override;
  163. destructor destroy; override;
  164. procedure printnodedata(var t:text);override;
  165. protected
  166. release_to_normal : boolean;
  167. private
  168. tempidx : longint;
  169. end;
  170. ttempdeletenodeclass = class of ttempdeletenode;
  171. var
  172. cnothingnode : tnothingnodeclass;
  173. cerrornode : terrornodeclass;
  174. casmnode : tasmnodeclass;
  175. cstatementnode : tstatementnodeclass;
  176. cblocknode : tblocknodeclass;
  177. ctempcreatenode : ttempcreatenodeclass;
  178. ctemprefnode : ttemprefnodeclass;
  179. ctempdeletenode : ttempdeletenodeclass;
  180. { Create a blocknode and statement node for multiple statements
  181. generated internally by the parser }
  182. function internalstatements(var laststatement:tstatementnode):tblocknode;
  183. function laststatement(block:tblocknode):tstatementnode;
  184. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  185. implementation
  186. uses
  187. cutils,
  188. verbose,globals,systems,
  189. symconst,symdef,defutil,defcmp,
  190. pass_1,
  191. nld,ncal,nflw,
  192. procinfo
  193. ;
  194. {*****************************************************************************
  195. Helpers
  196. *****************************************************************************}
  197. function internalstatements(var laststatement:tstatementnode):tblocknode;
  198. begin
  199. { create dummy initial statement }
  200. laststatement := cstatementnode.create(cnothingnode.create,nil);
  201. internalstatements := cblocknode.create(laststatement);
  202. end;
  203. function laststatement(block:tblocknode):tstatementnode;
  204. begin
  205. result:=tstatementnode(block.left);
  206. while assigned(result) and assigned(result.right) do
  207. result:=tstatementnode(result.right);
  208. end;
  209. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  210. begin
  211. if assigned(laststatement.right) then
  212. internalerror(200204201);
  213. laststatement.right:=cstatementnode.create(n,nil);
  214. laststatement:=tstatementnode(laststatement.right);
  215. end;
  216. {*****************************************************************************
  217. TFIRSTNOTHING
  218. *****************************************************************************}
  219. constructor tnothingnode.create;
  220. begin
  221. inherited create(nothingn);
  222. end;
  223. function tnothingnode.pass_typecheck:tnode;
  224. begin
  225. result:=nil;
  226. resultdef:=voidtype;
  227. end;
  228. function tnothingnode.pass_1 : tnode;
  229. begin
  230. result:=nil;
  231. expectloc:=LOC_VOID;
  232. end;
  233. {*****************************************************************************
  234. TFIRSTERROR
  235. *****************************************************************************}
  236. constructor terrornode.create;
  237. begin
  238. inherited create(errorn);
  239. end;
  240. function terrornode.pass_typecheck:tnode;
  241. begin
  242. result:=nil;
  243. include(flags,nf_error);
  244. codegenerror:=true;
  245. resultdef:=generrordef;
  246. end;
  247. function terrornode.pass_1 : tnode;
  248. begin
  249. result:=nil;
  250. expectloc:=LOC_VOID;
  251. codegenerror:=true;
  252. end;
  253. procedure terrornode.mark_write;
  254. begin
  255. end;
  256. {*****************************************************************************
  257. TSTATEMENTNODE
  258. *****************************************************************************}
  259. constructor tstatementnode.create(l,r : tnode);
  260. begin
  261. inherited create(statementn,l,r);
  262. end;
  263. function tstatementnode.simplify : tnode;
  264. begin
  265. result:=nil;
  266. { these "optimizations" are only to make it more easy to recognise }
  267. { blocknodes which at the end of inlining only contain one single }
  268. { statement. Simplifying inside blocknode.simplify could be dangerous }
  269. { because if the main blocknode which makes up a procedure/function }
  270. { body were replaced with a statementn/nothingn, this could cause }
  271. { problems elsewhere in the compiler which expects a blocknode }
  272. { remove next statement if it's a nothing-statement (since if it's }
  273. { the last, it won't remove itself -- see next simplification) }
  274. while assigned(right) and
  275. (tstatementnode(right).left.nodetype = nothingn) do
  276. begin
  277. result:=tstatementnode(right).right;
  278. tstatementnode(right).right:=nil;
  279. right.free;
  280. right:=result;
  281. result:=nil;
  282. end;
  283. { Remove initial nothingn if there are other statements. If there }
  284. { are no other statements, returning nil doesn't help (will be }
  285. { interpreted as "can't be simplified") and replacing the }
  286. { statementnode with a nothingnode cannot be done (because it's }
  287. { possible this statementnode is a child of a blocknode, and }
  288. { blocknodes are expected to only contain statementnodes) }
  289. if (left.nodetype = nothingn) and
  290. assigned(right) then
  291. begin
  292. result:=right;
  293. right:=nil;
  294. exit;
  295. end;
  296. { if the current statement contains a block with one statement, }
  297. { replace the current statement with that block's statement }
  298. if (left.nodetype = blockn) and
  299. assigned(tblocknode(left).left) and
  300. not assigned(tstatementnode(tblocknode(left).left).right) then
  301. begin
  302. result:=tblocknode(left).left;
  303. tstatementnode(result).right:=right;
  304. right:=nil;
  305. tblocknode(left).left:=nil;
  306. exit;
  307. end;
  308. end;
  309. function tstatementnode.pass_typecheck:tnode;
  310. begin
  311. result:=nil;
  312. resultdef:=voidtype;
  313. { left is the statement itself calln assignn or a complex one }
  314. typecheckpass(left);
  315. if (not (cs_extsyntax in current_settings.moduleswitches)) and
  316. assigned(left.resultdef) and
  317. not((left.nodetype=calln) and
  318. { don't complain when the value is used. And also not for constructors }
  319. ((cnf_return_value_used in tcallnode(left).callnodeflags) or
  320. (tcallnode(left).procdefinition.proctypeoption=potype_constructor))) and
  321. not(is_void(left.resultdef)) then
  322. CGMessage(parser_e_illegal_expression);
  323. if codegenerror then
  324. exit;
  325. { right is the next statement in the list }
  326. if assigned(right) then
  327. typecheckpass(right);
  328. if codegenerror then
  329. exit;
  330. end;
  331. function tstatementnode.pass_1 : tnode;
  332. begin
  333. result:=nil;
  334. { left is the statement itself calln assignn or a complex one }
  335. firstpass(left);
  336. if codegenerror then
  337. exit;
  338. expectloc:=left.expectloc;
  339. { right is the next in the list }
  340. if assigned(right) then
  341. firstpass(right);
  342. if codegenerror then
  343. exit;
  344. end;
  345. procedure tstatementnode.printnodetree(var t:text);
  346. begin
  347. printnodelist(t);
  348. end;
  349. {*****************************************************************************
  350. TBLOCKNODE
  351. *****************************************************************************}
  352. constructor tblocknode.create(l : tnode);
  353. begin
  354. inherited create(blockn,l);
  355. end;
  356. destructor tblocknode.destroy;
  357. var
  358. hp, next: tstatementnode;
  359. begin
  360. hp := tstatementnode(left);
  361. left := nil;
  362. while assigned(hp) do
  363. begin
  364. next := tstatementnode(hp.right);
  365. hp.right := nil;
  366. hp.free;
  367. hp := next;
  368. end;
  369. inherited destroy;
  370. end;
  371. function tblocknode.simplify: tnode;
  372. begin
  373. result := nil;
  374. { Warning: never replace a blocknode with another node type, }
  375. { since the block may be the main block of a procedure/function/ }
  376. { main program body, and those nodes should always be blocknodes }
  377. { since that's what the compiler expects elsewhere. }
  378. { if the current block contains only one statement, and }
  379. { this one statement only contains another block, replace }
  380. { this block with that other block. }
  381. if assigned(left) and
  382. not assigned(tstatementnode(left).right) and
  383. (tstatementnode(left).left.nodetype = blockn) then
  384. begin
  385. result:=tstatementnode(left).left;
  386. tstatementnode(left).left:=nil;
  387. exit;
  388. end;
  389. end;
  390. function tblocknode.pass_typecheck:tnode;
  391. var
  392. hp : tstatementnode;
  393. begin
  394. result:=nil;
  395. resultdef:=voidtype;
  396. hp:=tstatementnode(left);
  397. while assigned(hp) do
  398. begin
  399. if assigned(hp.left) then
  400. begin
  401. codegenerror:=false;
  402. typecheckpass(hp.left);
  403. if not(codegenerror) and
  404. not(cs_extsyntax in current_settings.moduleswitches) and
  405. (hp.left.nodetype=calln) and
  406. not(is_void(hp.left.resultdef)) and
  407. not(cnf_return_value_used in tcallnode(hp.left).callnodeflags) and
  408. not((tcallnode(hp.left).procdefinition.proctypeoption=potype_constructor) and
  409. assigned(tprocdef(tcallnode(hp.left).procdefinition)._class) and
  410. is_object(tprocdef(tcallnode(hp.left).procdefinition)._class)) then
  411. CGMessagePos(hp.left.fileinfo,parser_e_illegal_expression);
  412. { the resultdef of the block is the last type that is
  413. returned. Normally this is a voidtype. But when the
  414. compiler inserts a block of multiple statements then the
  415. last entry can return a value }
  416. resultdef:=hp.left.resultdef;
  417. end;
  418. hp:=tstatementnode(hp.right);
  419. end;
  420. end;
  421. function tblocknode.pass_1 : tnode;
  422. var
  423. hp : tstatementnode;
  424. count : longint;
  425. begin
  426. result:=nil;
  427. expectloc:=LOC_VOID;
  428. count:=0;
  429. hp:=tstatementnode(left);
  430. while assigned(hp) do
  431. begin
  432. if assigned(hp.left) then
  433. begin
  434. codegenerror:=false;
  435. firstpass(hp.left);
  436. hp.expectloc:=hp.left.expectloc;
  437. end;
  438. expectloc:=hp.expectloc;
  439. inc(count);
  440. hp:=tstatementnode(hp.right);
  441. end;
  442. end;
  443. {$ifdef state_tracking}
  444. function Tblocknode.track_state_pass(exec_known:boolean):boolean;
  445. var hp:Tstatementnode;
  446. begin
  447. track_state_pass:=false;
  448. hp:=Tstatementnode(left);
  449. while assigned(hp) do
  450. begin
  451. if hp.left.track_state_pass(exec_known) then
  452. track_state_pass:=true;
  453. hp:=Tstatementnode(hp.right);
  454. end;
  455. end;
  456. {$endif state_tracking}
  457. {*****************************************************************************
  458. TASMNODE
  459. *****************************************************************************}
  460. constructor tasmnode.create(p : TAsmList);
  461. begin
  462. inherited create(asmn);
  463. p_asm:=p;
  464. currenttai:=nil;
  465. used_regs_int:=[];
  466. used_regs_fpu:=[];
  467. end;
  468. constructor tasmnode.create_get_position;
  469. begin
  470. inherited create(asmn);
  471. p_asm:=nil;
  472. include(flags,nf_get_asm_position);
  473. currenttai:=nil;
  474. end;
  475. destructor tasmnode.destroy;
  476. begin
  477. if assigned(p_asm) then
  478. p_asm.free;
  479. inherited destroy;
  480. end;
  481. constructor tasmnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  482. var
  483. hp : tai;
  484. begin
  485. inherited ppuload(t,ppufile);
  486. if not(nf_get_asm_position in flags) then
  487. begin
  488. p_asm:=TAsmList.create;
  489. repeat
  490. hp:=ppuloadai(ppufile);
  491. if hp=nil then
  492. break;
  493. p_asm.concat(hp);
  494. until false;
  495. end
  496. else
  497. p_asm:=nil;
  498. currenttai:=nil;
  499. end;
  500. procedure tasmnode.ppuwrite(ppufile:tcompilerppufile);
  501. var
  502. hp : tai;
  503. begin
  504. inherited ppuwrite(ppufile);
  505. {$warning FIXME Add saving of register sets}
  506. if not(nf_get_asm_position in flags) then
  507. begin
  508. hp:=tai(p_asm.first);
  509. while assigned(hp) do
  510. begin
  511. ppuwriteai(ppufile,hp);
  512. hp:=tai(hp.next);
  513. end;
  514. { end is marked by a nil }
  515. ppuwriteai(ppufile,nil);
  516. end;
  517. end;
  518. procedure tasmnode.buildderefimpl;
  519. var
  520. hp : tai;
  521. begin
  522. inherited buildderefimpl;
  523. if not(nf_get_asm_position in flags) then
  524. begin
  525. hp:=tai(p_asm.first);
  526. while assigned(hp) do
  527. begin
  528. hp.buildderefimpl;
  529. hp:=tai(hp.next);
  530. end;
  531. end;
  532. end;
  533. procedure tasmnode.derefimpl;
  534. var
  535. hp : tai;
  536. begin
  537. inherited derefimpl;
  538. if not(nf_get_asm_position in flags) then
  539. begin
  540. hp:=tai(p_asm.first);
  541. while assigned(hp) do
  542. begin
  543. hp.derefimpl;
  544. hp:=tai(hp.next);
  545. end;
  546. end;
  547. end;
  548. function tasmnode.dogetcopy: tnode;
  549. var
  550. n: tasmnode;
  551. begin
  552. n := tasmnode(inherited dogetcopy);
  553. if assigned(p_asm) then
  554. begin
  555. n.p_asm:=TAsmList.create;
  556. n.p_asm.concatlistcopy(p_asm);
  557. end
  558. else n.p_asm := nil;
  559. n.currenttai:=currenttai;
  560. result:=n;
  561. end;
  562. function tasmnode.pass_typecheck:tnode;
  563. begin
  564. result:=nil;
  565. resultdef:=voidtype;
  566. if not(nf_get_asm_position in flags) then
  567. include(current_procinfo.flags,pi_has_assembler_block);
  568. end;
  569. function tasmnode.pass_1 : tnode;
  570. begin
  571. result:=nil;
  572. expectloc:=LOC_VOID;
  573. end;
  574. function tasmnode.docompare(p: tnode): boolean;
  575. begin
  576. { comparing of asmlists is not implemented (JM) }
  577. docompare := false;
  578. end;
  579. {*****************************************************************************
  580. TEMPCREATENODE
  581. *****************************************************************************}
  582. constructor ttempcreatenode.create(_typedef:tdef; _size: aint; _temptype: ttemptype;allowreg:boolean);
  583. begin
  584. inherited create(tempcreaten);
  585. size := _size;
  586. new(tempinfo);
  587. fillchar(tempinfo^,sizeof(tempinfo^),0);
  588. tempinfo^.typedef := _typedef;
  589. tempinfo^.temptype := _temptype;
  590. tempinfo^.owner := self;
  591. tempinfo^.withnode := nil;
  592. if allowreg and
  593. { temp must fit a single register }
  594. (tstoreddef(_typedef).is_fpuregable or
  595. (tstoreddef(_typedef).is_intregable and
  596. (_size<=TCGSize2Size[OS_64]))) and
  597. { size of register operations must be known }
  598. (def_cgsize(_typedef)<>OS_NO) and
  599. { no init/final needed }
  600. not (_typedef.needs_inittable) and
  601. ((_typedef.typ <> pointerdef) or
  602. (is_object(tpointerdef(_typedef).pointeddef) or
  603. not tpointerdef(_typedef).pointeddef.needs_inittable)) then
  604. include(tempinfo^.flags,ti_may_be_in_reg);
  605. end;
  606. constructor ttempcreatenode.create_withnode(_typedef: tdef; _size: aint; _temptype: ttemptype; allowreg:boolean; withnode: tnode);
  607. begin
  608. self.create(_typedef,_size,_temptype,allowreg);
  609. tempinfo^.withnode:=withnode.getcopy;
  610. end;
  611. function ttempcreatenode.dogetcopy: tnode;
  612. var
  613. n: ttempcreatenode;
  614. begin
  615. n := ttempcreatenode(inherited dogetcopy);
  616. n.size := size;
  617. new(n.tempinfo);
  618. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  619. n.tempinfo^.owner:=n;
  620. n.tempinfo^.typedef := tempinfo^.typedef;
  621. n.tempinfo^.temptype := tempinfo^.temptype;
  622. n.tempinfo^.flags := tempinfo^.flags * tempinfostoreflags;
  623. if assigned(tempinfo^.withnode) then
  624. n.tempinfo^.withnode := tempinfo^.withnode.getcopy
  625. else
  626. n.tempinfo^.withnode := nil;
  627. { when the tempinfo has already a hookoncopy then it is not
  628. reset by a tempdeletenode }
  629. if assigned(tempinfo^.hookoncopy) then
  630. internalerror(200211262);
  631. { signal the temprefs that the temp they point to has been copied, }
  632. { so that if the refs get copied as well, they can hook themselves }
  633. { to the copy of the temp }
  634. tempinfo^.hookoncopy := n.tempinfo;
  635. exclude(tempinfo^.flags,ti_nextref_set_hookoncopy_nil);
  636. result := n;
  637. end;
  638. constructor ttempcreatenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  639. begin
  640. inherited ppuload(t,ppufile);
  641. size:=ppufile.getlongint;
  642. new(tempinfo);
  643. fillchar(tempinfo^,sizeof(tempinfo^),0);
  644. ppufile.getsmallset(tempinfo^.flags);
  645. ppufile.getderef(tempinfo^.typedefderef);
  646. tempinfo^.temptype := ttemptype(ppufile.getbyte);
  647. tempinfo^.owner:=self;
  648. tempinfo^.withnode:=ppuloadnode(ppufile);
  649. end;
  650. procedure ttempcreatenode.ppuwrite(ppufile:tcompilerppufile);
  651. begin
  652. inherited ppuwrite(ppufile);
  653. ppufile.putlongint(size);
  654. ppufile.putsmallset(tempinfo^.flags);
  655. ppufile.putderef(tempinfo^.typedefderef);
  656. ppufile.putbyte(byte(tempinfo^.temptype));
  657. ppuwritenode(ppufile,tempinfo^.withnode);
  658. end;
  659. procedure ttempcreatenode.buildderefimpl;
  660. begin
  661. inherited buildderefimpl;
  662. tempinfo^.typedefderef.build(tempinfo^.typedef);
  663. if assigned(tempinfo^.withnode) then
  664. tempinfo^.withnode.buildderefimpl;
  665. end;
  666. procedure ttempcreatenode.derefimpl;
  667. begin
  668. inherited derefimpl;
  669. tempinfo^.typedef:=tdef(tempinfo^.typedefderef.resolve);
  670. if assigned(tempinfo^.withnode) then
  671. tempinfo^.withnode.derefimpl;
  672. end;
  673. procedure ttempcreatenode.derefnode;
  674. begin
  675. inherited derefnode;
  676. if assigned(tempinfo^.withnode) then
  677. tempinfo^.withnode.derefnode;
  678. end;
  679. function ttempcreatenode.pass_1 : tnode;
  680. begin
  681. result := nil;
  682. expectloc:=LOC_VOID;
  683. if (tempinfo^.typedef.needs_inittable) then
  684. include(current_procinfo.flags,pi_needs_implicit_finally);
  685. if assigned(tempinfo^.withnode) then
  686. firstpass(tempinfo^.withnode);
  687. end;
  688. function ttempcreatenode.pass_typecheck: tnode;
  689. begin
  690. result := nil;
  691. { a tempcreatenode doesn't have a resultdef, only temprefnodes do }
  692. resultdef := voidtype;
  693. if assigned(tempinfo^.withnode) then
  694. typecheckpass(tempinfo^.withnode);
  695. end;
  696. function ttempcreatenode.docompare(p: tnode): boolean;
  697. begin
  698. result :=
  699. inherited docompare(p) and
  700. (ttempcreatenode(p).size = size) and
  701. (ttempcreatenode(p).tempinfo^.flags*tempinfostoreflags=tempinfo^.flags*tempinfostoreflags) and
  702. (ttempcreatenode(p).tempinfo^.withnode.isequal(tempinfo^.withnode)) and
  703. equal_defs(ttempcreatenode(p).tempinfo^.typedef,tempinfo^.typedef);
  704. end;
  705. procedure ttempcreatenode.printnodedata(var t:text);
  706. begin
  707. inherited printnodedata(t);
  708. writeln(t,printnodeindention,'size = ',size,', temptypedef = "',tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  709. end;
  710. {*****************************************************************************
  711. TEMPREFNODE
  712. *****************************************************************************}
  713. constructor ttemprefnode.create(const temp: ttempcreatenode);
  714. begin
  715. inherited create(temprefn);
  716. tempinfo := temp.tempinfo;
  717. offset:=0;
  718. end;
  719. constructor ttemprefnode.create_offset(const temp: ttempcreatenode;aoffset:longint);
  720. begin
  721. self.create(temp);
  722. offset := aoffset;
  723. end;
  724. function ttemprefnode.dogetcopy: tnode;
  725. var
  726. n: ttemprefnode;
  727. begin
  728. n := ttemprefnode(inherited dogetcopy);
  729. n.offset := offset;
  730. if assigned(tempinfo^.hookoncopy) then
  731. { if the temp has been copied, assume it becomes a new }
  732. { temp which has to be hooked by the copied reference }
  733. begin
  734. { hook the ref to the copied temp }
  735. n.tempinfo := tempinfo^.hookoncopy;
  736. { if we passed a ttempdeletenode that changed the temp }
  737. { from a persistent one into a normal one, we must be }
  738. { the last reference (since our parent should free the }
  739. { temp (JM) }
  740. if (ti_nextref_set_hookoncopy_nil in tempinfo^.flags) then
  741. tempinfo^.hookoncopy := nil;
  742. end
  743. else
  744. { if the temp we refer to hasn't been copied, assume }
  745. { we're just a new reference to that temp }
  746. begin
  747. n.tempinfo := tempinfo;
  748. end;
  749. if not assigned(n.tempinfo) then
  750. internalerror(2005071901);
  751. result := n;
  752. end;
  753. constructor ttemprefnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  754. begin
  755. inherited ppuload(t,ppufile);
  756. tempidx:=ppufile.getlongint;
  757. offset:=ppufile.getlongint;
  758. end;
  759. procedure ttemprefnode.ppuwrite(ppufile:tcompilerppufile);
  760. begin
  761. inherited ppuwrite(ppufile);
  762. ppufile.putlongint(tempinfo^.owner.ppuidx);
  763. ppufile.putlongint(offset);
  764. end;
  765. procedure ttemprefnode.derefnode;
  766. var
  767. temp : ttempcreatenode;
  768. begin
  769. inherited derefnode;
  770. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  771. if temp.nodetype<>tempcreaten then
  772. internalerror(200311075);
  773. tempinfo:=temp.tempinfo;
  774. end;
  775. function ttemprefnode.pass_1 : tnode;
  776. begin
  777. expectloc := LOC_REFERENCE;
  778. if not tempinfo^.typedef.needs_inittable and
  779. (ti_may_be_in_reg in tempinfo^.flags) then
  780. begin
  781. if tempinfo^.typedef.typ=floatdef then
  782. begin
  783. if (tempinfo^.temptype = tt_persistent) then
  784. expectloc := LOC_CFPUREGISTER
  785. else
  786. expectloc := LOC_FPUREGISTER;
  787. end
  788. else
  789. begin
  790. if (tempinfo^.temptype = tt_persistent) then
  791. expectloc := LOC_CREGISTER
  792. else
  793. expectloc := LOC_REGISTER;
  794. end;
  795. end;
  796. result := nil;
  797. end;
  798. function ttemprefnode.pass_typecheck: tnode;
  799. begin
  800. { check if the temp is already resultdef passed }
  801. if not assigned(tempinfo^.typedef) then
  802. internalerror(200108233);
  803. result := nil;
  804. resultdef := tempinfo^.typedef;
  805. end;
  806. function ttemprefnode.docompare(p: tnode): boolean;
  807. begin
  808. result :=
  809. inherited docompare(p) and
  810. (ttemprefnode(p).tempinfo = tempinfo) and
  811. (ttemprefnode(p).offset = offset);
  812. end;
  813. procedure Ttemprefnode.mark_write;
  814. begin
  815. include(flags,nf_write);
  816. end;
  817. procedure ttemprefnode.printnodedata(var t:text);
  818. begin
  819. inherited printnodedata(t);
  820. writeln(t,printnodeindention,'temptypedef = "',tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  821. end;
  822. {*****************************************************************************
  823. TEMPDELETENODE
  824. *****************************************************************************}
  825. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  826. begin
  827. inherited create(tempdeleten);
  828. tempinfo := temp.tempinfo;
  829. release_to_normal := false;
  830. end;
  831. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  832. begin
  833. inherited create(tempdeleten);
  834. tempinfo := temp.tempinfo;
  835. release_to_normal := true;
  836. if tempinfo^.temptype <> tt_persistent then
  837. internalerror(200204211);
  838. end;
  839. function ttempdeletenode.dogetcopy: tnode;
  840. var
  841. n: ttempdeletenode;
  842. begin
  843. n:=ttempdeletenode(inherited dogetcopy);
  844. n.release_to_normal:=release_to_normal;
  845. if assigned(tempinfo^.hookoncopy) then
  846. { if the temp has been copied, assume it becomes a new }
  847. { temp which has to be hooked by the copied deletenode }
  848. begin
  849. { hook the tempdeletenode to the copied temp }
  850. n.tempinfo:=tempinfo^.hookoncopy;
  851. { the temp shall not be used, reset hookoncopy }
  852. { Only if release_to_normal is false, otherwise }
  853. { the temp can still be referenced once more (JM) }
  854. if (not release_to_normal) then
  855. tempinfo^.hookoncopy:=nil
  856. else
  857. include(tempinfo^.flags,ti_nextref_set_hookoncopy_nil);
  858. end
  859. else
  860. { if the temp we refer to hasn't been copied, we have a }
  861. { problem since that means we now have two delete nodes }
  862. { for one temp }
  863. internalerror(200108234);
  864. result:=n;
  865. end;
  866. constructor ttempdeletenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  867. begin
  868. inherited ppuload(t,ppufile);
  869. tempidx:=ppufile.getlongint;
  870. release_to_normal:=(ppufile.getbyte<>0);
  871. end;
  872. procedure ttempdeletenode.ppuwrite(ppufile:tcompilerppufile);
  873. begin
  874. inherited ppuwrite(ppufile);
  875. ppufile.putlongint(tempinfo^.owner.ppuidx);
  876. ppufile.putbyte(byte(release_to_normal));
  877. end;
  878. procedure ttempdeletenode.derefnode;
  879. var
  880. temp : ttempcreatenode;
  881. begin
  882. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  883. if temp.nodetype<>tempcreaten then
  884. internalerror(200311075);
  885. tempinfo:=temp.tempinfo;
  886. end;
  887. function ttempdeletenode.pass_1 : tnode;
  888. begin
  889. expectloc:=LOC_VOID;
  890. result := nil;
  891. end;
  892. function ttempdeletenode.pass_typecheck: tnode;
  893. begin
  894. result := nil;
  895. resultdef := voidtype;
  896. end;
  897. function ttempdeletenode.docompare(p: tnode): boolean;
  898. begin
  899. result :=
  900. inherited docompare(p) and
  901. (ttemprefnode(p).tempinfo = tempinfo);
  902. end;
  903. destructor ttempdeletenode.destroy;
  904. begin
  905. if assigned(tempinfo^.withnode) then
  906. begin
  907. tempinfo^.withnode.free;
  908. end;
  909. dispose(tempinfo);
  910. end;
  911. procedure ttempdeletenode.printnodedata(var t:text);
  912. begin
  913. inherited printnodedata(t);
  914. writeln(t,printnodeindention,'release_to_normal: ',release_to_normal,', temptypedef = "',tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  915. end;
  916. begin
  917. cnothingnode:=tnothingnode;
  918. cerrornode:=terrornode;
  919. casmnode:=tasmnode;
  920. cstatementnode:=tstatementnode;
  921. cblocknode:=tblocknode;
  922. ctempcreatenode:=ttempcreatenode;
  923. ctemprefnode:=ttemprefnode;
  924. ctempdeletenode:=ttempdeletenode;
  925. end.