nbas.pas 36 KB

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