nbas.pas 36 KB

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