nbas.pas 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. begin
  376. result := nil;
  377. { Warning: never replace a blocknode with another node type, }
  378. { since the block may be the main block of a procedure/function/ }
  379. { main program body, and those nodes should always be blocknodes }
  380. { since that's what the compiler expects elsewhere. }
  381. { if the current block contains only one statement, and }
  382. { this one statement only contains another block, replace }
  383. { this block with that other block. }
  384. if assigned(left) and
  385. not assigned(tstatementnode(left).right) and
  386. (tstatementnode(left).left.nodetype = blockn) then
  387. begin
  388. result:=tstatementnode(left).left;
  389. tstatementnode(left).left:=nil;
  390. exit;
  391. end;
  392. end;
  393. function tblocknode.pass_typecheck:tnode;
  394. var
  395. hp : tstatementnode;
  396. begin
  397. result:=nil;
  398. resultdef:=voidtype;
  399. hp:=tstatementnode(left);
  400. while assigned(hp) do
  401. begin
  402. if assigned(hp.left) then
  403. begin
  404. codegenerror:=false;
  405. typecheckpass(hp.left);
  406. if not(codegenerror) and
  407. not(cs_extsyntax in current_settings.moduleswitches) and
  408. (hp.left.nodetype=calln) and
  409. not(is_void(hp.left.resultdef)) and
  410. not(cnf_return_value_used in tcallnode(hp.left).callnodeflags) and
  411. not((tcallnode(hp.left).procdefinition.proctypeoption=potype_constructor) and
  412. assigned(tprocdef(tcallnode(hp.left).procdefinition)._class) and
  413. is_object(tprocdef(tcallnode(hp.left).procdefinition)._class)) then
  414. CGMessagePos(hp.left.fileinfo,parser_e_illegal_expression);
  415. { the resultdef of the block is the last type that is
  416. returned. Normally this is a voidtype. But when the
  417. compiler inserts a block of multiple statements then the
  418. last entry can return a value }
  419. resultdef:=hp.left.resultdef;
  420. end;
  421. hp:=tstatementnode(hp.right);
  422. end;
  423. end;
  424. function tblocknode.pass_1 : tnode;
  425. var
  426. hp : tstatementnode;
  427. count : longint;
  428. begin
  429. result:=nil;
  430. expectloc:=LOC_VOID;
  431. count:=0;
  432. hp:=tstatementnode(left);
  433. while assigned(hp) do
  434. begin
  435. if assigned(hp.left) then
  436. begin
  437. codegenerror:=false;
  438. firstpass(hp.left);
  439. hp.expectloc:=hp.left.expectloc;
  440. hp.registersint:=hp.left.registersint;
  441. hp.registersfpu:=hp.left.registersfpu;
  442. {$ifdef SUPPORT_MMX}
  443. hp.registersmmx:=hp.left.registersmmx;
  444. {$endif SUPPORT_MMX}
  445. end
  446. else
  447. hp.registersint:=0;
  448. if hp.registersint>registersint then
  449. registersint:=hp.registersint;
  450. if hp.registersfpu>registersfpu then
  451. registersfpu:=hp.registersfpu;
  452. {$ifdef SUPPORT_MMX}
  453. if hp.registersmmx>registersmmx then
  454. registersmmx:=hp.registersmmx;
  455. {$endif}
  456. expectloc:=hp.expectloc;
  457. inc(count);
  458. hp:=tstatementnode(hp.right);
  459. end;
  460. end;
  461. {$ifdef state_tracking}
  462. function Tblocknode.track_state_pass(exec_known:boolean):boolean;
  463. var hp:Tstatementnode;
  464. begin
  465. track_state_pass:=false;
  466. hp:=Tstatementnode(left);
  467. while assigned(hp) do
  468. begin
  469. if hp.left.track_state_pass(exec_known) then
  470. track_state_pass:=true;
  471. hp:=Tstatementnode(hp.right);
  472. end;
  473. end;
  474. {$endif state_tracking}
  475. {*****************************************************************************
  476. TASMNODE
  477. *****************************************************************************}
  478. constructor tasmnode.create(p : TAsmList);
  479. begin
  480. inherited create(asmn);
  481. p_asm:=p;
  482. currenttai:=nil;
  483. used_regs_int:=[];
  484. used_regs_fpu:=[];
  485. end;
  486. constructor tasmnode.create_get_position;
  487. begin
  488. inherited create(asmn);
  489. p_asm:=nil;
  490. include(flags,nf_get_asm_position);
  491. currenttai:=nil;
  492. end;
  493. destructor tasmnode.destroy;
  494. begin
  495. if assigned(p_asm) then
  496. p_asm.free;
  497. inherited destroy;
  498. end;
  499. constructor tasmnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  500. var
  501. hp : tai;
  502. begin
  503. inherited ppuload(t,ppufile);
  504. if not(nf_get_asm_position in flags) then
  505. begin
  506. p_asm:=TAsmList.create;
  507. repeat
  508. hp:=ppuloadai(ppufile);
  509. if hp=nil then
  510. break;
  511. p_asm.concat(hp);
  512. until false;
  513. end
  514. else
  515. p_asm:=nil;
  516. currenttai:=nil;
  517. end;
  518. procedure tasmnode.ppuwrite(ppufile:tcompilerppufile);
  519. var
  520. hp : tai;
  521. begin
  522. inherited ppuwrite(ppufile);
  523. {$warning FIXME Add saving of register sets}
  524. if not(nf_get_asm_position in flags) then
  525. begin
  526. hp:=tai(p_asm.first);
  527. while assigned(hp) do
  528. begin
  529. ppuwriteai(ppufile,hp);
  530. hp:=tai(hp.next);
  531. end;
  532. { end is marked by a nil }
  533. ppuwriteai(ppufile,nil);
  534. end;
  535. end;
  536. procedure tasmnode.buildderefimpl;
  537. var
  538. hp : tai;
  539. begin
  540. inherited buildderefimpl;
  541. if not(nf_get_asm_position in flags) then
  542. begin
  543. hp:=tai(p_asm.first);
  544. while assigned(hp) do
  545. begin
  546. hp.buildderefimpl;
  547. hp:=tai(hp.next);
  548. end;
  549. end;
  550. end;
  551. procedure tasmnode.derefimpl;
  552. var
  553. hp : tai;
  554. begin
  555. inherited derefimpl;
  556. if not(nf_get_asm_position in flags) then
  557. begin
  558. hp:=tai(p_asm.first);
  559. while assigned(hp) do
  560. begin
  561. hp.derefimpl;
  562. hp:=tai(hp.next);
  563. end;
  564. end;
  565. end;
  566. function tasmnode.dogetcopy: tnode;
  567. var
  568. n: tasmnode;
  569. begin
  570. n := tasmnode(inherited dogetcopy);
  571. if assigned(p_asm) then
  572. begin
  573. n.p_asm:=TAsmList.create;
  574. n.p_asm.concatlistcopy(p_asm);
  575. end
  576. else n.p_asm := nil;
  577. n.currenttai:=currenttai;
  578. result:=n;
  579. end;
  580. function tasmnode.pass_typecheck:tnode;
  581. begin
  582. result:=nil;
  583. resultdef:=voidtype;
  584. if not(nf_get_asm_position in flags) then
  585. include(current_procinfo.flags,pi_has_assembler_block);
  586. end;
  587. function tasmnode.pass_1 : tnode;
  588. begin
  589. result:=nil;
  590. expectloc:=LOC_VOID;
  591. end;
  592. function tasmnode.docompare(p: tnode): boolean;
  593. begin
  594. { comparing of asmlists is not implemented (JM) }
  595. docompare := false;
  596. end;
  597. {*****************************************************************************
  598. TEMPCREATENODE
  599. *****************************************************************************}
  600. constructor ttempcreatenode.create(_typedef:tdef; _size: aint; _temptype: ttemptype;allowreg:boolean);
  601. begin
  602. inherited create(tempcreaten);
  603. size := _size;
  604. new(tempinfo);
  605. fillchar(tempinfo^,sizeof(tempinfo^),0);
  606. tempinfo^.typedef := _typedef;
  607. tempinfo^.temptype := _temptype;
  608. tempinfo^.owner := self;
  609. tempinfo^.withnode := nil;
  610. tempinfo^.may_be_in_reg:=
  611. allowreg and
  612. { temp must fit a single register }
  613. (tstoreddef(_typedef).is_fpuregable or
  614. (tstoreddef(_typedef).is_intregable and
  615. (_size<=TCGSize2Size[OS_64]))) and
  616. { size of register operations must be known }
  617. (def_cgsize(_typedef)<>OS_NO) and
  618. { no init/final needed }
  619. not (_typedef.needs_inittable) and
  620. ((_typedef.typ <> pointerdef) or
  621. (is_object(tpointerdef(_typedef).pointeddef) or
  622. not tpointerdef(_typedef).pointeddef.needs_inittable));
  623. end;
  624. constructor ttempcreatenode.create_withnode(_typedef: tdef; _size: aint; _temptype: ttemptype; allowreg:boolean; withnode: tnode);
  625. begin
  626. self.create(_typedef,_size,_temptype,allowreg);
  627. tempinfo^.withnode:=withnode.getcopy;
  628. end;
  629. function ttempcreatenode.dogetcopy: tnode;
  630. var
  631. n: ttempcreatenode;
  632. begin
  633. n := ttempcreatenode(inherited dogetcopy);
  634. n.size := size;
  635. new(n.tempinfo);
  636. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  637. n.tempinfo^.owner:=n;
  638. n.tempinfo^.typedef := tempinfo^.typedef;
  639. n.tempinfo^.temptype := tempinfo^.temptype;
  640. if assigned(tempinfo^.withnode) then
  641. n.tempinfo^.withnode := tempinfo^.withnode.getcopy
  642. else
  643. n.tempinfo^.withnode := nil;
  644. { when the tempinfo has already a hookoncopy then it is not
  645. reset by a tempdeletenode }
  646. if assigned(tempinfo^.hookoncopy) then
  647. internalerror(200211262);
  648. { signal the temprefs that the temp they point to has been copied, }
  649. { so that if the refs get copied as well, they can hook themselves }
  650. { to the copy of the temp }
  651. tempinfo^.hookoncopy := n.tempinfo;
  652. tempinfo^.nextref_set_hookoncopy_nil := false;
  653. result := n;
  654. end;
  655. constructor ttempcreatenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  656. begin
  657. inherited ppuload(t,ppufile);
  658. size:=ppufile.getlongint;
  659. new(tempinfo);
  660. fillchar(tempinfo^,sizeof(tempinfo^),0);
  661. tempinfo^.may_be_in_reg:=boolean(ppufile.getbyte);
  662. ppufile.getderef(tempinfo^.typedefderef);
  663. tempinfo^.temptype := ttemptype(ppufile.getbyte);
  664. tempinfo^.owner:=self;
  665. tempinfo^.withnode:=ppuloadnode(ppufile);
  666. end;
  667. procedure ttempcreatenode.ppuwrite(ppufile:tcompilerppufile);
  668. begin
  669. inherited ppuwrite(ppufile);
  670. ppufile.putlongint(size);
  671. ppufile.putbyte(byte(tempinfo^.may_be_in_reg));
  672. ppufile.putderef(tempinfo^.typedefderef);
  673. ppufile.putbyte(byte(tempinfo^.temptype));
  674. ppuwritenode(ppufile,tempinfo^.withnode);
  675. end;
  676. procedure ttempcreatenode.buildderefimpl;
  677. begin
  678. inherited buildderefimpl;
  679. tempinfo^.typedefderef.build(tempinfo^.typedef);
  680. if assigned(tempinfo^.withnode) then
  681. tempinfo^.withnode.buildderefimpl;
  682. end;
  683. procedure ttempcreatenode.derefimpl;
  684. begin
  685. inherited derefimpl;
  686. tempinfo^.typedef:=tdef(tempinfo^.typedefderef.resolve);
  687. if assigned(tempinfo^.withnode) then
  688. tempinfo^.withnode.derefimpl;
  689. end;
  690. procedure ttempcreatenode.derefnode;
  691. begin
  692. inherited derefnode;
  693. if assigned(tempinfo^.withnode) then
  694. tempinfo^.withnode.derefnode;
  695. end;
  696. function ttempcreatenode.pass_1 : tnode;
  697. begin
  698. result := nil;
  699. expectloc:=LOC_VOID;
  700. if (tempinfo^.typedef.needs_inittable) then
  701. include(current_procinfo.flags,pi_needs_implicit_finally);
  702. if assigned(tempinfo^.withnode) then
  703. firstpass(tempinfo^.withnode);
  704. end;
  705. function ttempcreatenode.pass_typecheck: tnode;
  706. begin
  707. result := nil;
  708. { a tempcreatenode doesn't have a resultdef, only temprefnodes do }
  709. resultdef := voidtype;
  710. if assigned(tempinfo^.withnode) then
  711. typecheckpass(tempinfo^.withnode);
  712. end;
  713. function ttempcreatenode.docompare(p: tnode): boolean;
  714. begin
  715. result :=
  716. inherited docompare(p) and
  717. (ttempcreatenode(p).size = size) and
  718. (ttempcreatenode(p).tempinfo^.may_be_in_reg = tempinfo^.may_be_in_reg) and
  719. (ttempcreatenode(p).tempinfo^.withnode.isequal(tempinfo^.withnode)) and
  720. equal_defs(ttempcreatenode(p).tempinfo^.typedef,tempinfo^.typedef);
  721. end;
  722. procedure ttempcreatenode.printnodedata(var t:text);
  723. begin
  724. inherited printnodedata(t);
  725. writeln(t,printnodeindention,'size = ',size,', temptypedef = "',tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptruint(tempinfo),sizeof(ptruint)*2));
  726. end;
  727. {*****************************************************************************
  728. TEMPREFNODE
  729. *****************************************************************************}
  730. constructor ttemprefnode.create(const temp: ttempcreatenode);
  731. begin
  732. inherited create(temprefn);
  733. tempinfo := temp.tempinfo;
  734. offset:=0;
  735. end;
  736. constructor ttemprefnode.create_offset(const temp: ttempcreatenode;aoffset:longint);
  737. begin
  738. self.create(temp);
  739. offset := aoffset;
  740. end;
  741. function ttemprefnode.dogetcopy: tnode;
  742. var
  743. n: ttemprefnode;
  744. begin
  745. n := ttemprefnode(inherited dogetcopy);
  746. n.offset := offset;
  747. if assigned(tempinfo^.hookoncopy) then
  748. { if the temp has been copied, assume it becomes a new }
  749. { temp which has to be hooked by the copied reference }
  750. begin
  751. { hook the ref to the copied temp }
  752. n.tempinfo := tempinfo^.hookoncopy;
  753. { if we passed a ttempdeletenode that changed the temp }
  754. { from a persistent one into a normal one, we must be }
  755. { the last reference (since our parent should free the }
  756. { temp (JM) }
  757. if (tempinfo^.nextref_set_hookoncopy_nil) then
  758. tempinfo^.hookoncopy := nil;
  759. end
  760. else
  761. { if the temp we refer to hasn't been copied, assume }
  762. { we're just a new reference to that temp }
  763. begin
  764. n.tempinfo := tempinfo;
  765. end;
  766. if not assigned(n.tempinfo) then
  767. internalerror(2005071901);
  768. result := n;
  769. end;
  770. constructor ttemprefnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  771. begin
  772. inherited ppuload(t,ppufile);
  773. tempidx:=ppufile.getlongint;
  774. offset:=ppufile.getlongint;
  775. end;
  776. procedure ttemprefnode.ppuwrite(ppufile:tcompilerppufile);
  777. begin
  778. inherited ppuwrite(ppufile);
  779. ppufile.putlongint(tempinfo^.owner.ppuidx);
  780. ppufile.putlongint(offset);
  781. end;
  782. procedure ttemprefnode.derefnode;
  783. var
  784. temp : ttempcreatenode;
  785. begin
  786. inherited derefnode;
  787. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  788. if temp.nodetype<>tempcreaten then
  789. internalerror(200311075);
  790. tempinfo:=temp.tempinfo;
  791. end;
  792. function ttemprefnode.pass_1 : tnode;
  793. begin
  794. expectloc := LOC_REFERENCE;
  795. if not tempinfo^.typedef.needs_inittable and
  796. tempinfo^.may_be_in_reg then
  797. begin
  798. if tempinfo^.typedef.typ=floatdef then
  799. begin
  800. if (tempinfo^.temptype = tt_persistent) then
  801. expectloc := LOC_CFPUREGISTER
  802. else
  803. expectloc := LOC_FPUREGISTER;
  804. end
  805. else
  806. begin
  807. if (tempinfo^.temptype = tt_persistent) then
  808. expectloc := LOC_CREGISTER
  809. else
  810. expectloc := LOC_REGISTER;
  811. end;
  812. end;
  813. result := nil;
  814. end;
  815. function ttemprefnode.pass_typecheck: tnode;
  816. begin
  817. { check if the temp is already resultdef passed }
  818. if not assigned(tempinfo^.typedef) then
  819. internalerror(200108233);
  820. result := nil;
  821. resultdef := tempinfo^.typedef;
  822. end;
  823. function ttemprefnode.docompare(p: tnode): boolean;
  824. begin
  825. result :=
  826. inherited docompare(p) and
  827. (ttemprefnode(p).tempinfo = tempinfo) and
  828. (ttemprefnode(p).offset = offset);
  829. end;
  830. procedure Ttemprefnode.mark_write;
  831. begin
  832. include(flags,nf_write);
  833. end;
  834. procedure ttemprefnode.printnodedata(var t:text);
  835. begin
  836. inherited printnodedata(t);
  837. writeln(t,printnodeindention,'temptypedef = "',tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptruint(tempinfo),sizeof(ptruint)*2));
  838. end;
  839. {*****************************************************************************
  840. TEMPDELETENODE
  841. *****************************************************************************}
  842. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  843. begin
  844. inherited create(tempdeleten);
  845. tempinfo := temp.tempinfo;
  846. release_to_normal := false;
  847. end;
  848. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  849. begin
  850. inherited create(tempdeleten);
  851. tempinfo := temp.tempinfo;
  852. release_to_normal := true;
  853. if tempinfo^.temptype <> tt_persistent then
  854. internalerror(200204211);
  855. end;
  856. function ttempdeletenode.dogetcopy: tnode;
  857. var
  858. n: ttempdeletenode;
  859. begin
  860. n := ttempdeletenode(inherited dogetcopy);
  861. n.release_to_normal := release_to_normal;
  862. if assigned(tempinfo^.hookoncopy) then
  863. { if the temp has been copied, assume it becomes a new }
  864. { temp which has to be hooked by the copied deletenode }
  865. begin
  866. { hook the tempdeletenode to the copied temp }
  867. n.tempinfo := tempinfo^.hookoncopy;
  868. { the temp shall not be used, reset hookoncopy }
  869. { Only if release_to_normal is false, otherwise }
  870. { the temp can still be referenced once more (JM) }
  871. if (not release_to_normal) then
  872. tempinfo^.hookoncopy:=nil
  873. else
  874. tempinfo^.nextref_set_hookoncopy_nil := true;
  875. end
  876. else
  877. { if the temp we refer to hasn't been copied, we have a }
  878. { problem since that means we now have two delete nodes }
  879. { for one temp }
  880. internalerror(200108234);
  881. result := n;
  882. end;
  883. constructor ttempdeletenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  884. begin
  885. inherited ppuload(t,ppufile);
  886. tempidx:=ppufile.getlongint;
  887. release_to_normal:=(ppufile.getbyte<>0);
  888. end;
  889. procedure ttempdeletenode.ppuwrite(ppufile:tcompilerppufile);
  890. begin
  891. inherited ppuwrite(ppufile);
  892. ppufile.putlongint(tempinfo^.owner.ppuidx);
  893. ppufile.putbyte(byte(release_to_normal));
  894. end;
  895. procedure ttempdeletenode.derefnode;
  896. var
  897. temp : ttempcreatenode;
  898. begin
  899. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  900. if temp.nodetype<>tempcreaten then
  901. internalerror(200311075);
  902. tempinfo:=temp.tempinfo;
  903. end;
  904. function ttempdeletenode.pass_1 : tnode;
  905. begin
  906. expectloc:=LOC_VOID;
  907. result := nil;
  908. end;
  909. function ttempdeletenode.pass_typecheck: tnode;
  910. begin
  911. result := nil;
  912. resultdef := voidtype;
  913. end;
  914. function ttempdeletenode.docompare(p: tnode): boolean;
  915. begin
  916. result :=
  917. inherited docompare(p) and
  918. (ttemprefnode(p).tempinfo = tempinfo);
  919. end;
  920. destructor ttempdeletenode.destroy;
  921. begin
  922. if assigned(tempinfo^.withnode) then
  923. begin
  924. tempinfo^.withnode.free;
  925. end;
  926. dispose(tempinfo);
  927. end;
  928. procedure ttempdeletenode.printnodedata(var t:text);
  929. begin
  930. inherited printnodedata(t);
  931. writeln(t,printnodeindention,'release_to_normal: ',release_to_normal,', temptypedef = "',tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptruint(tempinfo),sizeof(ptruint)*2));
  932. end;
  933. begin
  934. cnothingnode:=tnothingnode;
  935. cerrornode:=terrornode;
  936. casmnode:=tasmnode;
  937. cstatementnode:=tstatementnode;
  938. cblocknode:=tblocknode;
  939. ctempcreatenode:=ttempcreatenode;
  940. ctemprefnode:=ttemprefnode;
  941. ctempdeletenode:=ttempdeletenode;
  942. end.