nbas.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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. is_inlined_result : boolean;
  100. end;
  101. { a node which will create a (non)persistent temp of a given type with a given }
  102. { size (the size is separate to allow creating "void" temps with a custom size) }
  103. ttempcreatenode = class(tnode)
  104. size: aint;
  105. tempinfo: ptempinfo;
  106. { * persistent temps are used in manually written code where the temp }
  107. { be usable among different statements and where you can manually say }
  108. { when the temp has to be freed (using a ttempdeletenode) }
  109. { * non-persistent temps are mostly used in typeconversion helpers, }
  110. { where the node that receives the temp becomes responsible for }
  111. { freeing it. In this last case, you must use only one reference }
  112. { to it and *not* generate a ttempdeletenode }
  113. constructor create(_typedef: tdef; _size: aint; _temptype: ttemptype;allowreg:boolean); virtual;
  114. constructor create_withnode(_typedef: tdef; _size: aint; _temptype: ttemptype; allowreg:boolean; withnode: tnode); virtual;
  115. constructor create_inlined_result(_typedef: tdef; _size: aint; _temptype: ttemptype; allowreg:boolean); virtual;
  116. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  117. procedure ppuwrite(ppufile:tcompilerppufile);override;
  118. procedure buildderefimpl;override;
  119. procedure derefimpl;override;
  120. procedure derefnode;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. function dogetcopy: tnode; override;
  136. procedure derefnode;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. function dogetcopy: tnode; override;
  158. procedure derefnode;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. 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 tstatementnode.simplify : tnode;
  263. begin
  264. result:=nil;
  265. { these "optimizations" are only to make it more easy to recognise }
  266. { blocknodes which at the end of inlining only contain one single }
  267. { statement. Simplifying inside blocknode.simplify could be dangerous }
  268. { because if the main blocknode which makes up a procedure/function }
  269. { body were replaced with a statementn/nothingn, this could cause }
  270. { problems elsewhere in the compiler which expects a blocknode }
  271. { remove next statement if it's a nothing-statement (since if it's }
  272. { the last, it won't remove itself -- see next simplification) }
  273. while assigned(right) and
  274. (tstatementnode(right).left.nodetype = nothingn) do
  275. begin
  276. result:=tstatementnode(right).right;
  277. tstatementnode(right).right:=nil;
  278. right.free;
  279. right:=result;
  280. result:=nil;
  281. end;
  282. { Remove initial nothingn if there are other statements. If there }
  283. { are no other statements, returning nil doesn't help (will be }
  284. { interpreted as "can't be simplified") and replacing the }
  285. { statementnode with a nothingnode cannot be done (because it's }
  286. { possible this statementnode is a child of a blocknode, and }
  287. { blocknodes are expected to only contain statementnodes) }
  288. if (left.nodetype = nothingn) and
  289. assigned(right) then
  290. begin
  291. result:=right;
  292. right:=nil;
  293. exit;
  294. end;
  295. { if the current statement contains a block with one statement, }
  296. { replace the current statement with that block's statement }
  297. if (left.nodetype = blockn) and
  298. assigned(tblocknode(left).left) and
  299. not assigned(tstatementnode(tblocknode(left).left).right) then
  300. begin
  301. result:=tblocknode(left).left;
  302. tstatementnode(result).right:=right;
  303. right:=nil;
  304. tblocknode(left).left:=nil;
  305. exit;
  306. end;
  307. end;
  308. function tstatementnode.pass_typecheck:tnode;
  309. begin
  310. result:=nil;
  311. resultdef:=voidtype;
  312. { left is the statement itself calln assignn or a complex one }
  313. typecheckpass(left);
  314. if (not (cs_extsyntax in current_settings.moduleswitches)) and
  315. assigned(left.resultdef) and
  316. not((left.nodetype=calln) and
  317. { don't complain when funcretrefnode is set, because then the
  318. value is already used. And also not for constructors }
  319. (assigned(tcallnode(left).funcretnode) or
  320. (tcallnode(left).procdefinition.proctypeoption=potype_constructor))) and
  321. not(is_void(left.resultdef)) then
  322. CGMessage(parser_e_illegal_expression);
  323. if codegenerror then
  324. exit;
  325. { right is the next statement in the list }
  326. if assigned(right) then
  327. typecheckpass(right);
  328. if codegenerror then
  329. exit;
  330. end;
  331. function tstatementnode.pass_1 : tnode;
  332. begin
  333. result:=nil;
  334. { left is the statement itself calln assignn or a complex one }
  335. firstpass(left);
  336. if codegenerror then
  337. exit;
  338. expectloc:=left.expectloc;
  339. registersint:=left.registersint;
  340. registersfpu:=left.registersfpu;
  341. {$ifdef SUPPORT_MMX}
  342. registersmmx:=left.registersmmx;
  343. {$endif SUPPORT_MMX}
  344. { right is the next in the list }
  345. if assigned(right) then
  346. firstpass(right);
  347. if codegenerror then
  348. exit;
  349. end;
  350. procedure tstatementnode.printnodetree(var t:text);
  351. begin
  352. printnodelist(t);
  353. end;
  354. {*****************************************************************************
  355. TBLOCKNODE
  356. *****************************************************************************}
  357. constructor tblocknode.create(l : tnode);
  358. begin
  359. inherited create(blockn,l);
  360. end;
  361. destructor tblocknode.destroy;
  362. var
  363. hp, next: tstatementnode;
  364. begin
  365. hp := tstatementnode(left);
  366. left := nil;
  367. while assigned(hp) do
  368. begin
  369. next := tstatementnode(hp.right);
  370. hp.right := nil;
  371. hp.free;
  372. hp := next;
  373. end;
  374. inherited destroy;
  375. end;
  376. function tblocknode.simplify: tnode;
  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. constructor ttempcreatenode.create_inlined_result(_typedef: tdef; _size: aint; _temptype: ttemptype; allowreg:boolean);
  632. begin
  633. self.create(_typedef,_size,_temptype,allowreg);
  634. tempinfo^.is_inlined_result:=true;
  635. end;
  636. function ttempcreatenode.dogetcopy: tnode;
  637. var
  638. n: ttempcreatenode;
  639. begin
  640. n := ttempcreatenode(inherited dogetcopy);
  641. n.size := size;
  642. new(n.tempinfo);
  643. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  644. n.tempinfo^.owner:=n;
  645. n.tempinfo^.typedef := tempinfo^.typedef;
  646. n.tempinfo^.temptype := tempinfo^.temptype;
  647. n.tempinfo^.may_be_in_reg := tempinfo^.may_be_in_reg;
  648. n.tempinfo^.is_inlined_result := tempinfo^.is_inlined_result;
  649. if assigned(tempinfo^.withnode) then
  650. n.tempinfo^.withnode := tempinfo^.withnode.getcopy
  651. else
  652. n.tempinfo^.withnode := nil;
  653. { when the tempinfo has already a hookoncopy then it is not
  654. reset by a tempdeletenode }
  655. if assigned(tempinfo^.hookoncopy) then
  656. internalerror(200211262);
  657. { signal the temprefs that the temp they point to has been copied, }
  658. { so that if the refs get copied as well, they can hook themselves }
  659. { to the copy of the temp }
  660. tempinfo^.hookoncopy := n.tempinfo;
  661. tempinfo^.nextref_set_hookoncopy_nil := false;
  662. result := n;
  663. end;
  664. constructor ttempcreatenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  665. begin
  666. inherited ppuload(t,ppufile);
  667. size:=ppufile.getlongint;
  668. new(tempinfo);
  669. fillchar(tempinfo^,sizeof(tempinfo^),0);
  670. tempinfo^.may_be_in_reg:=boolean(ppufile.getbyte);
  671. tempinfo^.is_inlined_result:=boolean(ppufile.getbyte);
  672. ppufile.getderef(tempinfo^.typedefderef);
  673. tempinfo^.temptype := ttemptype(ppufile.getbyte);
  674. tempinfo^.owner:=self;
  675. tempinfo^.withnode:=ppuloadnode(ppufile);
  676. end;
  677. procedure ttempcreatenode.ppuwrite(ppufile:tcompilerppufile);
  678. begin
  679. inherited ppuwrite(ppufile);
  680. ppufile.putlongint(size);
  681. ppufile.putbyte(byte(tempinfo^.may_be_in_reg));
  682. ppufile.putbyte(byte(tempinfo^.is_inlined_result));
  683. ppufile.putderef(tempinfo^.typedefderef);
  684. ppufile.putbyte(byte(tempinfo^.temptype));
  685. ppuwritenode(ppufile,tempinfo^.withnode);
  686. end;
  687. procedure ttempcreatenode.buildderefimpl;
  688. begin
  689. inherited buildderefimpl;
  690. tempinfo^.typedefderef.build(tempinfo^.typedef);
  691. if assigned(tempinfo^.withnode) then
  692. tempinfo^.withnode.buildderefimpl;
  693. end;
  694. procedure ttempcreatenode.derefimpl;
  695. begin
  696. inherited derefimpl;
  697. tempinfo^.typedef:=tdef(tempinfo^.typedefderef.resolve);
  698. if assigned(tempinfo^.withnode) then
  699. tempinfo^.withnode.derefimpl;
  700. end;
  701. procedure ttempcreatenode.derefnode;
  702. begin
  703. inherited derefnode;
  704. if assigned(tempinfo^.withnode) then
  705. tempinfo^.withnode.derefnode;
  706. end;
  707. function ttempcreatenode.pass_1 : tnode;
  708. begin
  709. result := nil;
  710. expectloc:=LOC_VOID;
  711. if (tempinfo^.typedef.needs_inittable) then
  712. include(current_procinfo.flags,pi_needs_implicit_finally);
  713. if assigned(tempinfo^.withnode) then
  714. firstpass(tempinfo^.withnode);
  715. end;
  716. function ttempcreatenode.pass_typecheck: tnode;
  717. begin
  718. result := nil;
  719. { a tempcreatenode doesn't have a resultdef, only temprefnodes do }
  720. resultdef := voidtype;
  721. if assigned(tempinfo^.withnode) then
  722. typecheckpass(tempinfo^.withnode);
  723. end;
  724. function ttempcreatenode.docompare(p: tnode): boolean;
  725. begin
  726. result :=
  727. inherited docompare(p) and
  728. (ttempcreatenode(p).size = size) and
  729. (ttempcreatenode(p).tempinfo^.may_be_in_reg = tempinfo^.may_be_in_reg) and
  730. (ttempcreatenode(p).tempinfo^.is_inlined_result = tempinfo^.is_inlined_result) and
  731. (ttempcreatenode(p).tempinfo^.withnode.isequal(tempinfo^.withnode)) and
  732. equal_defs(ttempcreatenode(p).tempinfo^.typedef,tempinfo^.typedef);
  733. end;
  734. procedure ttempcreatenode.printnodedata(var t:text);
  735. begin
  736. inherited printnodedata(t);
  737. writeln(t,printnodeindention,'size = ',size,', temptypedef = "',tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptruint(tempinfo),sizeof(ptruint)*2));
  738. end;
  739. {*****************************************************************************
  740. TEMPREFNODE
  741. *****************************************************************************}
  742. constructor ttemprefnode.create(const temp: ttempcreatenode);
  743. begin
  744. inherited create(temprefn);
  745. tempinfo := temp.tempinfo;
  746. offset:=0;
  747. end;
  748. constructor ttemprefnode.create_offset(const temp: ttempcreatenode;aoffset:longint);
  749. begin
  750. self.create(temp);
  751. offset := aoffset;
  752. end;
  753. function ttemprefnode.dogetcopy: tnode;
  754. var
  755. n: ttemprefnode;
  756. begin
  757. n := ttemprefnode(inherited dogetcopy);
  758. n.offset := offset;
  759. if assigned(tempinfo^.hookoncopy) then
  760. { if the temp has been copied, assume it becomes a new }
  761. { temp which has to be hooked by the copied reference }
  762. begin
  763. { hook the ref to the copied temp }
  764. n.tempinfo := tempinfo^.hookoncopy;
  765. { if we passed a ttempdeletenode that changed the temp }
  766. { from a persistent one into a normal one, we must be }
  767. { the last reference (since our parent should free the }
  768. { temp (JM) }
  769. if (tempinfo^.nextref_set_hookoncopy_nil) then
  770. tempinfo^.hookoncopy := nil;
  771. end
  772. else
  773. { if the temp we refer to hasn't been copied, assume }
  774. { we're just a new reference to that temp }
  775. begin
  776. n.tempinfo := tempinfo;
  777. end;
  778. if not assigned(n.tempinfo) then
  779. internalerror(2005071901);
  780. result := n;
  781. end;
  782. constructor ttemprefnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  783. begin
  784. inherited ppuload(t,ppufile);
  785. tempidx:=ppufile.getlongint;
  786. offset:=ppufile.getlongint;
  787. end;
  788. procedure ttemprefnode.ppuwrite(ppufile:tcompilerppufile);
  789. begin
  790. inherited ppuwrite(ppufile);
  791. ppufile.putlongint(tempinfo^.owner.ppuidx);
  792. ppufile.putlongint(offset);
  793. end;
  794. procedure ttemprefnode.derefnode;
  795. var
  796. temp : ttempcreatenode;
  797. begin
  798. inherited derefnode;
  799. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  800. if temp.nodetype<>tempcreaten then
  801. internalerror(200311075);
  802. tempinfo:=temp.tempinfo;
  803. end;
  804. function ttemprefnode.pass_1 : tnode;
  805. begin
  806. expectloc := LOC_REFERENCE;
  807. if not tempinfo^.typedef.needs_inittable and
  808. tempinfo^.may_be_in_reg then
  809. begin
  810. if tempinfo^.typedef.typ=floatdef then
  811. begin
  812. if (tempinfo^.temptype = tt_persistent) then
  813. expectloc := LOC_CFPUREGISTER
  814. else
  815. expectloc := LOC_FPUREGISTER;
  816. end
  817. else
  818. begin
  819. if (tempinfo^.temptype = tt_persistent) then
  820. expectloc := LOC_CREGISTER
  821. else
  822. expectloc := LOC_REGISTER;
  823. end;
  824. end;
  825. result := nil;
  826. end;
  827. function ttemprefnode.pass_typecheck: tnode;
  828. begin
  829. { check if the temp is already resultdef passed }
  830. if not assigned(tempinfo^.typedef) then
  831. internalerror(200108233);
  832. result := nil;
  833. resultdef := tempinfo^.typedef;
  834. end;
  835. function ttemprefnode.docompare(p: tnode): boolean;
  836. begin
  837. result :=
  838. inherited docompare(p) and
  839. (ttemprefnode(p).tempinfo = tempinfo) and
  840. (ttemprefnode(p).offset = offset);
  841. end;
  842. procedure Ttemprefnode.mark_write;
  843. begin
  844. include(flags,nf_write);
  845. end;
  846. procedure ttemprefnode.printnodedata(var t:text);
  847. begin
  848. inherited printnodedata(t);
  849. writeln(t,printnodeindention,'temptypedef = "',tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptruint(tempinfo),sizeof(ptruint)*2));
  850. end;
  851. {*****************************************************************************
  852. TEMPDELETENODE
  853. *****************************************************************************}
  854. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  855. begin
  856. inherited create(tempdeleten);
  857. tempinfo := temp.tempinfo;
  858. release_to_normal := false;
  859. end;
  860. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  861. begin
  862. inherited create(tempdeleten);
  863. tempinfo := temp.tempinfo;
  864. release_to_normal := true;
  865. if tempinfo^.temptype <> tt_persistent then
  866. internalerror(200204211);
  867. end;
  868. function ttempdeletenode.dogetcopy: tnode;
  869. var
  870. n: ttempdeletenode;
  871. begin
  872. n := ttempdeletenode(inherited dogetcopy);
  873. n.release_to_normal := release_to_normal;
  874. if assigned(tempinfo^.hookoncopy) then
  875. { if the temp has been copied, assume it becomes a new }
  876. { temp which has to be hooked by the copied deletenode }
  877. begin
  878. { hook the tempdeletenode to the copied temp }
  879. n.tempinfo := tempinfo^.hookoncopy;
  880. { the temp shall not be used, reset hookoncopy }
  881. { Only if release_to_normal is false, otherwise }
  882. { the temp can still be referenced once more (JM) }
  883. if (not release_to_normal) then
  884. tempinfo^.hookoncopy:=nil
  885. else
  886. tempinfo^.nextref_set_hookoncopy_nil := true;
  887. end
  888. else
  889. { if the temp we refer to hasn't been copied, we have a }
  890. { problem since that means we now have two delete nodes }
  891. { for one temp }
  892. internalerror(200108234);
  893. result := n;
  894. end;
  895. constructor ttempdeletenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  896. begin
  897. inherited ppuload(t,ppufile);
  898. tempidx:=ppufile.getlongint;
  899. release_to_normal:=(ppufile.getbyte<>0);
  900. end;
  901. procedure ttempdeletenode.ppuwrite(ppufile:tcompilerppufile);
  902. begin
  903. inherited ppuwrite(ppufile);
  904. ppufile.putlongint(tempinfo^.owner.ppuidx);
  905. ppufile.putbyte(byte(release_to_normal));
  906. end;
  907. procedure ttempdeletenode.derefnode;
  908. var
  909. temp : ttempcreatenode;
  910. begin
  911. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  912. if temp.nodetype<>tempcreaten then
  913. internalerror(200311075);
  914. tempinfo:=temp.tempinfo;
  915. end;
  916. function ttempdeletenode.pass_1 : tnode;
  917. begin
  918. expectloc:=LOC_VOID;
  919. result := nil;
  920. end;
  921. function ttempdeletenode.pass_typecheck: tnode;
  922. begin
  923. result := nil;
  924. resultdef := voidtype;
  925. end;
  926. function ttempdeletenode.docompare(p: tnode): boolean;
  927. begin
  928. result :=
  929. inherited docompare(p) and
  930. (ttemprefnode(p).tempinfo = tempinfo);
  931. end;
  932. destructor ttempdeletenode.destroy;
  933. begin
  934. if assigned(tempinfo^.withnode) then
  935. begin
  936. tempinfo^.withnode.free;
  937. end;
  938. dispose(tempinfo);
  939. end;
  940. procedure ttempdeletenode.printnodedata(var t:text);
  941. begin
  942. inherited printnodedata(t);
  943. writeln(t,printnodeindention,'release_to_normal: ',release_to_normal,', temptypedef = "',tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptruint(tempinfo),sizeof(ptruint)*2));
  944. end;
  945. begin
  946. cnothingnode:=tnothingnode;
  947. cerrornode:=terrornode;
  948. casmnode:=tasmnode;
  949. cstatementnode:=tstatementnode;
  950. cblocknode:=tblocknode;
  951. ctempcreatenode:=ttempcreatenode;
  952. ctemprefnode:=ttemprefnode;
  953. ctempdeletenode:=ttempdeletenode;
  954. end.