nbas.pas 36 KB

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