nbas.pas 38 KB

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