nbas.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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_is_inlined_result,
  83. ti_addr_taken);
  84. ttempinfoflags = set of ttempinfoflag;
  85. const
  86. tempinfostoreflags = [ti_may_be_in_reg,ti_is_inlined_result,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. end;
  104. { a node which will create a (non)persistent temp of a given type with a given }
  105. { size (the size is separate to allow creating "void" temps with a custom size) }
  106. ttempcreatenode = class(tnode)
  107. size: aint;
  108. tempinfo: ptempinfo;
  109. { * persistent temps are used in manually written code where the temp }
  110. { be usable among different statements and where you can manually say }
  111. { when the temp has to be freed (using a ttempdeletenode) }
  112. { * non-persistent temps are mostly used in typeconversion helpers, }
  113. { where the node that receives the temp becomes responsible for }
  114. { freeing it. In this last case, you must use only one reference }
  115. { to it and *not* generate a ttempdeletenode }
  116. constructor create(_typedef: tdef; _size: aint; _temptype: ttemptype;allowreg:boolean); virtual;
  117. constructor create_withnode(_typedef: tdef; _size: aint; _temptype: ttemptype; allowreg:boolean; withnode: tnode); virtual;
  118. constructor create_inlined_result(_typedef: tdef; _size: aint; _temptype: ttemptype; allowreg:boolean); virtual;
  119. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  120. procedure ppuwrite(ppufile:tcompilerppufile);override;
  121. procedure buildderefimpl;override;
  122. procedure derefimpl;override;
  123. procedure derefnode;override;
  124. function dogetcopy: tnode; override;
  125. function pass_1 : tnode; override;
  126. function pass_typecheck: tnode; override;
  127. function docompare(p: tnode): boolean; override;
  128. procedure printnodedata(var t:text);override;
  129. end;
  130. ttempcreatenodeclass = class of ttempcreatenode;
  131. { a node which is a reference to a certain temp }
  132. ttemprefnode = class(tnode)
  133. tempinfo: ptempinfo;
  134. constructor create(const temp: ttempcreatenode); virtual;
  135. constructor create_offset(const temp: ttempcreatenode;aoffset:longint);
  136. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  137. procedure ppuwrite(ppufile:tcompilerppufile);override;
  138. function dogetcopy: tnode; override;
  139. procedure derefnode;override;
  140. function pass_1 : tnode; override;
  141. function pass_typecheck : tnode; override;
  142. procedure mark_write;override;
  143. function docompare(p: tnode): boolean; override;
  144. procedure printnodedata(var t:text);override;
  145. protected
  146. offset : longint;
  147. private
  148. tempidx : longint;
  149. end;
  150. ttemprefnodeclass = class of ttemprefnode;
  151. { a node which removes a temp }
  152. ttempdeletenode = class(tnode)
  153. tempinfo: ptempinfo;
  154. constructor create(const temp: ttempcreatenode); virtual;
  155. { this will convert the persistant temp to a normal temp
  156. for returning to the other nodes }
  157. constructor create_normal_temp(const temp: ttempcreatenode);
  158. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  159. procedure ppuwrite(ppufile:tcompilerppufile);override;
  160. function dogetcopy: tnode; override;
  161. procedure derefnode;override;
  162. function pass_1: tnode; override;
  163. function pass_typecheck: tnode; override;
  164. function docompare(p: tnode): boolean; override;
  165. destructor destroy; override;
  166. procedure printnodedata(var t:text);override;
  167. protected
  168. release_to_normal : boolean;
  169. private
  170. tempidx : longint;
  171. end;
  172. ttempdeletenodeclass = class of ttempdeletenode;
  173. var
  174. cnothingnode : tnothingnodeclass;
  175. cerrornode : terrornodeclass;
  176. casmnode : tasmnodeclass;
  177. cstatementnode : tstatementnodeclass;
  178. cblocknode : tblocknodeclass;
  179. ctempcreatenode : ttempcreatenodeclass;
  180. ctemprefnode : ttemprefnodeclass;
  181. ctempdeletenode : ttempdeletenodeclass;
  182. { Create a blocknode and statement node for multiple statements
  183. generated internally by the parser }
  184. function internalstatements(var laststatement:tstatementnode):tblocknode;
  185. function laststatement(block:tblocknode):tstatementnode;
  186. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  187. implementation
  188. uses
  189. cutils,
  190. verbose,globals,systems,
  191. symconst,symdef,defutil,defcmp,
  192. pass_1,
  193. nld,ncal,nflw,
  194. procinfo
  195. ;
  196. {*****************************************************************************
  197. Helpers
  198. *****************************************************************************}
  199. function internalstatements(var laststatement:tstatementnode):tblocknode;
  200. begin
  201. { create dummy initial statement }
  202. laststatement := cstatementnode.create(cnothingnode.create,nil);
  203. internalstatements := cblocknode.create(laststatement);
  204. end;
  205. function laststatement(block:tblocknode):tstatementnode;
  206. begin
  207. result:=tstatementnode(block.left);
  208. while assigned(result) and assigned(result.right) do
  209. result:=tstatementnode(result.right);
  210. end;
  211. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  212. begin
  213. if assigned(laststatement.right) then
  214. internalerror(200204201);
  215. laststatement.right:=cstatementnode.create(n,nil);
  216. laststatement:=tstatementnode(laststatement.right);
  217. end;
  218. {*****************************************************************************
  219. TFIRSTNOTHING
  220. *****************************************************************************}
  221. constructor tnothingnode.create;
  222. begin
  223. inherited create(nothingn);
  224. end;
  225. function tnothingnode.pass_typecheck:tnode;
  226. begin
  227. result:=nil;
  228. resultdef:=voidtype;
  229. end;
  230. function tnothingnode.pass_1 : tnode;
  231. begin
  232. result:=nil;
  233. expectloc:=LOC_VOID;
  234. end;
  235. {*****************************************************************************
  236. TFIRSTERROR
  237. *****************************************************************************}
  238. constructor terrornode.create;
  239. begin
  240. inherited create(errorn);
  241. end;
  242. function terrornode.pass_typecheck:tnode;
  243. begin
  244. result:=nil;
  245. include(flags,nf_error);
  246. codegenerror:=true;
  247. resultdef:=generrordef;
  248. end;
  249. function terrornode.pass_1 : tnode;
  250. begin
  251. result:=nil;
  252. expectloc:=LOC_VOID;
  253. codegenerror:=true;
  254. end;
  255. procedure terrornode.mark_write;
  256. begin
  257. end;
  258. {*****************************************************************************
  259. TSTATEMENTNODE
  260. *****************************************************************************}
  261. constructor tstatementnode.create(l,r : tnode);
  262. begin
  263. inherited create(statementn,l,r);
  264. end;
  265. function tstatementnode.simplify : tnode;
  266. begin
  267. result:=nil;
  268. { these "optimizations" are only to make it more easy to recognise }
  269. { blocknodes which at the end of inlining only contain one single }
  270. { statement. Simplifying inside blocknode.simplify could be dangerous }
  271. { because if the main blocknode which makes up a procedure/function }
  272. { body were replaced with a statementn/nothingn, this could cause }
  273. { problems elsewhere in the compiler which expects a blocknode }
  274. { remove next statement if it's a nothing-statement (since if it's }
  275. { the last, it won't remove itself -- see next simplification) }
  276. while assigned(right) and
  277. (tstatementnode(right).left.nodetype = nothingn) do
  278. begin
  279. result:=tstatementnode(right).right;
  280. tstatementnode(right).right:=nil;
  281. right.free;
  282. right:=result;
  283. result:=nil;
  284. end;
  285. { Remove initial nothingn if there are other statements. If there }
  286. { are no other statements, returning nil doesn't help (will be }
  287. { interpreted as "can't be simplified") and replacing the }
  288. { statementnode with a nothingnode cannot be done (because it's }
  289. { possible this statementnode is a child of a blocknode, and }
  290. { blocknodes are expected to only contain statementnodes) }
  291. if (left.nodetype = nothingn) and
  292. assigned(right) then
  293. begin
  294. result:=right;
  295. right:=nil;
  296. exit;
  297. end;
  298. { if the current statement contains a block with one statement, }
  299. { replace the current statement with that block's statement }
  300. if (left.nodetype = blockn) and
  301. assigned(tblocknode(left).left) and
  302. not assigned(tstatementnode(tblocknode(left).left).right) then
  303. begin
  304. result:=tblocknode(left).left;
  305. tstatementnode(result).right:=right;
  306. right:=nil;
  307. tblocknode(left).left:=nil;
  308. exit;
  309. end;
  310. end;
  311. function tstatementnode.pass_typecheck:tnode;
  312. begin
  313. result:=nil;
  314. resultdef:=voidtype;
  315. { left is the statement itself calln assignn or a complex one }
  316. typecheckpass(left);
  317. if (not (cs_extsyntax in current_settings.moduleswitches)) and
  318. assigned(left.resultdef) and
  319. not((left.nodetype=calln) and
  320. { don't complain when the value is used. And also not for constructors }
  321. ((cnf_return_value_used in tcallnode(left).callnodeflags) or
  322. (tcallnode(left).procdefinition.proctypeoption=potype_constructor))) and
  323. not(is_void(left.resultdef)) then
  324. CGMessage(parser_e_illegal_expression);
  325. if codegenerror then
  326. exit;
  327. { right is the next statement in the list }
  328. if assigned(right) then
  329. typecheckpass(right);
  330. if codegenerror then
  331. exit;
  332. end;
  333. function tstatementnode.pass_1 : tnode;
  334. begin
  335. result:=nil;
  336. { left is the statement itself calln assignn or a complex one }
  337. firstpass(left);
  338. if codegenerror then
  339. exit;
  340. expectloc:=left.expectloc;
  341. registersint:=left.registersint;
  342. registersfpu:=left.registersfpu;
  343. {$ifdef SUPPORT_MMX}
  344. registersmmx:=left.registersmmx;
  345. {$endif SUPPORT_MMX}
  346. { right is the next in the list }
  347. if assigned(right) then
  348. firstpass(right);
  349. if codegenerror then
  350. exit;
  351. end;
  352. procedure tstatementnode.printnodetree(var t:text);
  353. begin
  354. printnodelist(t);
  355. end;
  356. {*****************************************************************************
  357. TBLOCKNODE
  358. *****************************************************************************}
  359. constructor tblocknode.create(l : tnode);
  360. begin
  361. inherited create(blockn,l);
  362. end;
  363. destructor tblocknode.destroy;
  364. var
  365. hp, next: tstatementnode;
  366. begin
  367. hp := tstatementnode(left);
  368. left := nil;
  369. while assigned(hp) do
  370. begin
  371. next := tstatementnode(hp.right);
  372. hp.right := nil;
  373. hp.free;
  374. hp := next;
  375. end;
  376. inherited destroy;
  377. end;
  378. function tblocknode.simplify: tnode;
  379. var
  380. hp, next: tstatementnode;
  381. begin
  382. result := nil;
  383. { Warning: never replace a blocknode with another node type, }
  384. { since the block may be the main block of a procedure/function/ }
  385. { main program body, and those nodes should always be blocknodes }
  386. { since that's what the compiler expects elsewhere. }
  387. { if the current block contains only one statement, and }
  388. { this one statement only contains another block, replace }
  389. { this block with that other block. }
  390. if assigned(left) and
  391. not assigned(tstatementnode(left).right) and
  392. (tstatementnode(left).left.nodetype = blockn) then
  393. begin
  394. result:=tstatementnode(left).left;
  395. tstatementnode(left).left:=nil;
  396. exit;
  397. end;
  398. end;
  399. function tblocknode.pass_typecheck:tnode;
  400. var
  401. hp : tstatementnode;
  402. begin
  403. result:=nil;
  404. resultdef:=voidtype;
  405. hp:=tstatementnode(left);
  406. while assigned(hp) do
  407. begin
  408. if assigned(hp.left) then
  409. begin
  410. codegenerror:=false;
  411. typecheckpass(hp.left);
  412. if not(codegenerror) and
  413. not(cs_extsyntax in current_settings.moduleswitches) and
  414. (hp.left.nodetype=calln) and
  415. not(is_void(hp.left.resultdef)) and
  416. not(cnf_return_value_used in tcallnode(hp.left).callnodeflags) and
  417. not((tcallnode(hp.left).procdefinition.proctypeoption=potype_constructor) and
  418. assigned(tprocdef(tcallnode(hp.left).procdefinition)._class) and
  419. is_object(tprocdef(tcallnode(hp.left).procdefinition)._class)) then
  420. CGMessagePos(hp.left.fileinfo,parser_e_illegal_expression);
  421. { the resultdef of the block is the last type that is
  422. returned. Normally this is a voidtype. But when the
  423. compiler inserts a block of multiple statements then the
  424. last entry can return a value }
  425. resultdef:=hp.left.resultdef;
  426. end;
  427. hp:=tstatementnode(hp.right);
  428. end;
  429. end;
  430. function tblocknode.pass_1 : tnode;
  431. var
  432. hp : tstatementnode;
  433. count : longint;
  434. begin
  435. result:=nil;
  436. expectloc:=LOC_VOID;
  437. count:=0;
  438. hp:=tstatementnode(left);
  439. while assigned(hp) do
  440. begin
  441. if assigned(hp.left) then
  442. begin
  443. codegenerror:=false;
  444. firstpass(hp.left);
  445. hp.expectloc:=hp.left.expectloc;
  446. hp.registersint:=hp.left.registersint;
  447. hp.registersfpu:=hp.left.registersfpu;
  448. {$ifdef SUPPORT_MMX}
  449. hp.registersmmx:=hp.left.registersmmx;
  450. {$endif SUPPORT_MMX}
  451. end
  452. else
  453. hp.registersint:=0;
  454. if hp.registersint>registersint then
  455. registersint:=hp.registersint;
  456. if hp.registersfpu>registersfpu then
  457. registersfpu:=hp.registersfpu;
  458. {$ifdef SUPPORT_MMX}
  459. if hp.registersmmx>registersmmx then
  460. registersmmx:=hp.registersmmx;
  461. {$endif}
  462. expectloc:=hp.expectloc;
  463. inc(count);
  464. hp:=tstatementnode(hp.right);
  465. end;
  466. end;
  467. {$ifdef state_tracking}
  468. function Tblocknode.track_state_pass(exec_known:boolean):boolean;
  469. var hp:Tstatementnode;
  470. begin
  471. track_state_pass:=false;
  472. hp:=Tstatementnode(left);
  473. while assigned(hp) do
  474. begin
  475. if hp.left.track_state_pass(exec_known) then
  476. track_state_pass:=true;
  477. hp:=Tstatementnode(hp.right);
  478. end;
  479. end;
  480. {$endif state_tracking}
  481. {*****************************************************************************
  482. TASMNODE
  483. *****************************************************************************}
  484. constructor tasmnode.create(p : TAsmList);
  485. begin
  486. inherited create(asmn);
  487. p_asm:=p;
  488. currenttai:=nil;
  489. used_regs_int:=[];
  490. used_regs_fpu:=[];
  491. end;
  492. constructor tasmnode.create_get_position;
  493. begin
  494. inherited create(asmn);
  495. p_asm:=nil;
  496. include(flags,nf_get_asm_position);
  497. currenttai:=nil;
  498. end;
  499. destructor tasmnode.destroy;
  500. begin
  501. if assigned(p_asm) then
  502. p_asm.free;
  503. inherited destroy;
  504. end;
  505. constructor tasmnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  506. var
  507. hp : tai;
  508. begin
  509. inherited ppuload(t,ppufile);
  510. if not(nf_get_asm_position in flags) then
  511. begin
  512. p_asm:=TAsmList.create;
  513. repeat
  514. hp:=ppuloadai(ppufile);
  515. if hp=nil then
  516. break;
  517. p_asm.concat(hp);
  518. until false;
  519. end
  520. else
  521. p_asm:=nil;
  522. currenttai:=nil;
  523. end;
  524. procedure tasmnode.ppuwrite(ppufile:tcompilerppufile);
  525. var
  526. hp : tai;
  527. begin
  528. inherited ppuwrite(ppufile);
  529. {$warning FIXME Add saving of register sets}
  530. if not(nf_get_asm_position in flags) then
  531. begin
  532. hp:=tai(p_asm.first);
  533. while assigned(hp) do
  534. begin
  535. ppuwriteai(ppufile,hp);
  536. hp:=tai(hp.next);
  537. end;
  538. { end is marked by a nil }
  539. ppuwriteai(ppufile,nil);
  540. end;
  541. end;
  542. procedure tasmnode.buildderefimpl;
  543. var
  544. hp : tai;
  545. begin
  546. inherited buildderefimpl;
  547. if not(nf_get_asm_position in flags) then
  548. begin
  549. hp:=tai(p_asm.first);
  550. while assigned(hp) do
  551. begin
  552. hp.buildderefimpl;
  553. hp:=tai(hp.next);
  554. end;
  555. end;
  556. end;
  557. procedure tasmnode.derefimpl;
  558. var
  559. hp : tai;
  560. begin
  561. inherited derefimpl;
  562. if not(nf_get_asm_position in flags) then
  563. begin
  564. hp:=tai(p_asm.first);
  565. while assigned(hp) do
  566. begin
  567. hp.derefimpl;
  568. hp:=tai(hp.next);
  569. end;
  570. end;
  571. end;
  572. function tasmnode.dogetcopy: tnode;
  573. var
  574. n: tasmnode;
  575. begin
  576. n := tasmnode(inherited dogetcopy);
  577. if assigned(p_asm) then
  578. begin
  579. n.p_asm:=TAsmList.create;
  580. n.p_asm.concatlistcopy(p_asm);
  581. end
  582. else n.p_asm := nil;
  583. n.currenttai:=currenttai;
  584. result:=n;
  585. end;
  586. function tasmnode.pass_typecheck:tnode;
  587. begin
  588. result:=nil;
  589. resultdef:=voidtype;
  590. if not(nf_get_asm_position in flags) then
  591. include(current_procinfo.flags,pi_has_assembler_block);
  592. end;
  593. function tasmnode.pass_1 : tnode;
  594. begin
  595. result:=nil;
  596. expectloc:=LOC_VOID;
  597. end;
  598. function tasmnode.docompare(p: tnode): boolean;
  599. begin
  600. { comparing of asmlists is not implemented (JM) }
  601. docompare := false;
  602. end;
  603. {*****************************************************************************
  604. TEMPCREATENODE
  605. *****************************************************************************}
  606. constructor ttempcreatenode.create(_typedef:tdef; _size: aint; _temptype: ttemptype;allowreg:boolean);
  607. begin
  608. inherited create(tempcreaten);
  609. size := _size;
  610. new(tempinfo);
  611. fillchar(tempinfo^,sizeof(tempinfo^),0);
  612. tempinfo^.typedef := _typedef;
  613. tempinfo^.temptype := _temptype;
  614. tempinfo^.owner := self;
  615. tempinfo^.withnode := nil;
  616. if allowreg and
  617. { temp must fit a single register }
  618. (tstoreddef(_typedef).is_fpuregable or
  619. (tstoreddef(_typedef).is_intregable and
  620. (_size<=TCGSize2Size[OS_64]))) and
  621. { size of register operations must be known }
  622. (def_cgsize(_typedef)<>OS_NO) and
  623. { no init/final needed }
  624. not (_typedef.needs_inittable) and
  625. ((_typedef.typ <> pointerdef) or
  626. (is_object(tpointerdef(_typedef).pointeddef) or
  627. not tpointerdef(_typedef).pointeddef.needs_inittable)) then
  628. include(tempinfo^.flags,ti_may_be_in_reg);
  629. end;
  630. constructor ttempcreatenode.create_withnode(_typedef: tdef; _size: aint; _temptype: ttemptype; allowreg:boolean; withnode: tnode);
  631. begin
  632. self.create(_typedef,_size,_temptype,allowreg);
  633. tempinfo^.withnode:=withnode.getcopy;
  634. end;
  635. constructor ttempcreatenode.create_inlined_result(_typedef: tdef; _size: aint; _temptype: ttemptype; allowreg:boolean);
  636. begin
  637. self.create(_typedef,_size,_temptype,allowreg);
  638. include(tempinfo^.flags,ti_is_inlined_result);
  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. { when the tempinfo has already a hookoncopy then it is not
  657. reset by a tempdeletenode }
  658. if assigned(tempinfo^.hookoncopy) then
  659. internalerror(200211262);
  660. { signal the temprefs that the temp they point to has been copied, }
  661. { so that if the refs get copied as well, they can hook themselves }
  662. { to the copy of the temp }
  663. tempinfo^.hookoncopy := n.tempinfo;
  664. exclude(tempinfo^.flags,ti_nextref_set_hookoncopy_nil);
  665. result := n;
  666. end;
  667. constructor ttempcreatenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  668. begin
  669. inherited ppuload(t,ppufile);
  670. size:=ppufile.getlongint;
  671. new(tempinfo);
  672. fillchar(tempinfo^,sizeof(tempinfo^),0);
  673. ppufile.getsmallset(tempinfo^.flags);
  674. ppufile.getderef(tempinfo^.typedefderef);
  675. tempinfo^.temptype := ttemptype(ppufile.getbyte);
  676. tempinfo^.owner:=self;
  677. tempinfo^.withnode:=ppuloadnode(ppufile);
  678. end;
  679. procedure ttempcreatenode.ppuwrite(ppufile:tcompilerppufile);
  680. begin
  681. inherited ppuwrite(ppufile);
  682. ppufile.putlongint(size);
  683. ppufile.putsmallset(tempinfo^.flags);
  684. ppufile.putderef(tempinfo^.typedefderef);
  685. ppufile.putbyte(byte(tempinfo^.temptype));
  686. ppuwritenode(ppufile,tempinfo^.withnode);
  687. end;
  688. procedure ttempcreatenode.buildderefimpl;
  689. begin
  690. inherited buildderefimpl;
  691. tempinfo^.typedefderef.build(tempinfo^.typedef);
  692. if assigned(tempinfo^.withnode) then
  693. tempinfo^.withnode.buildderefimpl;
  694. end;
  695. procedure ttempcreatenode.derefimpl;
  696. begin
  697. inherited derefimpl;
  698. tempinfo^.typedef:=tdef(tempinfo^.typedefderef.resolve);
  699. if assigned(tempinfo^.withnode) then
  700. tempinfo^.withnode.derefimpl;
  701. end;
  702. procedure ttempcreatenode.derefnode;
  703. begin
  704. inherited derefnode;
  705. if assigned(tempinfo^.withnode) then
  706. tempinfo^.withnode.derefnode;
  707. end;
  708. function ttempcreatenode.pass_1 : tnode;
  709. begin
  710. result := nil;
  711. expectloc:=LOC_VOID;
  712. if (tempinfo^.typedef.needs_inittable) then
  713. include(current_procinfo.flags,pi_needs_implicit_finally);
  714. if assigned(tempinfo^.withnode) then
  715. firstpass(tempinfo^.withnode);
  716. end;
  717. function ttempcreatenode.pass_typecheck: tnode;
  718. begin
  719. result := nil;
  720. { a tempcreatenode doesn't have a resultdef, only temprefnodes do }
  721. resultdef := voidtype;
  722. if assigned(tempinfo^.withnode) then
  723. typecheckpass(tempinfo^.withnode);
  724. end;
  725. function ttempcreatenode.docompare(p: tnode): boolean;
  726. begin
  727. result :=
  728. inherited docompare(p) and
  729. (ttempcreatenode(p).size = size) and
  730. (ttempcreatenode(p).tempinfo^.flags*tempinfostoreflags=tempinfo^.flags*tempinfostoreflags) 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(ptrint(tempinfo),sizeof(ptrint)*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 (ti_nextref_set_hookoncopy_nil in tempinfo^.flags) 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. (ti_may_be_in_reg in tempinfo^.flags) 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(ptrint(tempinfo),sizeof(ptrint)*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. include(tempinfo^.flags,ti_nextref_set_hookoncopy_nil);
  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(ptrint(tempinfo),sizeof(ptrint)*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.