nbas.pas 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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_addr_taken);
  83. ttempinfoflags = set of ttempinfoflag;
  84. const
  85. tempinfostoreflags = [ti_may_be_in_reg,ti_addr_taken];
  86. type
  87. { to allow access to the location by temp references even after the temp has }
  88. { already been disposed and to make sure the coherency between temps and }
  89. { temp references is kept after a getcopy }
  90. ptempinfo = ^ttempinfo;
  91. ttempinfo = record
  92. { set to the copy of a tempcreate pnode (if it gets copied) so that the }
  93. { refs and deletenode can hook to this copy once they get copied too }
  94. hookoncopy : ptempinfo;
  95. typedef : tdef;
  96. typedefderef : tderef;
  97. temptype : ttemptype;
  98. owner : ttempcreatenode;
  99. withnode : tnode;
  100. location : tlocation;
  101. flags : ttempinfoflags;
  102. end;
  103. { a node which will create a (non)persistent temp of a given type with a given }
  104. { size (the size is separate to allow creating "void" temps with a custom size) }
  105. ttempcreatenode = class(tnode)
  106. size: aint;
  107. tempinfo: ptempinfo;
  108. { * persistent temps are used in manually written code where the temp }
  109. { be usable among different statements and where you can manually say }
  110. { when the temp has to be freed (using a ttempdeletenode) }
  111. { * non-persistent temps are mostly used in typeconversion helpers, }
  112. { where the node that receives the temp becomes responsible for }
  113. { freeing it. In this last case, you must use only one reference }
  114. { to it and *not* generate a ttempdeletenode }
  115. constructor create(_typedef: tdef; _size: aint; _temptype: ttemptype;allowreg:boolean); virtual;
  116. constructor create_withnode(_typedef: tdef; _size: aint; _temptype: ttemptype; allowreg:boolean; withnode: tnode); virtual;
  117. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  118. procedure ppuwrite(ppufile:tcompilerppufile);override;
  119. procedure buildderefimpl;override;
  120. procedure derefimpl;override;
  121. procedure derefnode;override;
  122. function dogetcopy: tnode; override;
  123. function pass_1 : tnode; override;
  124. function pass_typecheck: tnode; override;
  125. function docompare(p: tnode): boolean; override;
  126. procedure printnodedata(var t:text);override;
  127. end;
  128. ttempcreatenodeclass = class of ttempcreatenode;
  129. { a node which is a reference to a certain temp }
  130. ttemprefnode = class(tnode)
  131. tempinfo: ptempinfo;
  132. constructor create(const temp: ttempcreatenode); virtual;
  133. constructor create_offset(const temp: ttempcreatenode;aoffset:longint);
  134. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  135. procedure ppuwrite(ppufile:tcompilerppufile);override;
  136. function dogetcopy: tnode; override;
  137. procedure derefnode;override;
  138. function pass_1 : tnode; override;
  139. function pass_typecheck : tnode; override;
  140. procedure mark_write;override;
  141. function docompare(p: tnode): boolean; override;
  142. procedure printnodedata(var t:text);override;
  143. protected
  144. offset : longint;
  145. private
  146. tempidx : longint;
  147. end;
  148. ttemprefnodeclass = class of ttemprefnode;
  149. { a node which removes a temp }
  150. ttempdeletenode = class(tnode)
  151. tempinfo: ptempinfo;
  152. constructor create(const temp: ttempcreatenode); virtual;
  153. { this will convert the persistant temp to a normal temp
  154. for returning to the other nodes }
  155. constructor create_normal_temp(const temp: ttempcreatenode);
  156. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  157. procedure ppuwrite(ppufile:tcompilerppufile);override;
  158. function dogetcopy: tnode; override;
  159. procedure derefnode;override;
  160. function pass_1: tnode; override;
  161. function pass_typecheck: tnode; override;
  162. function docompare(p: tnode): boolean; override;
  163. destructor destroy; override;
  164. procedure printnodedata(var t:text);override;
  165. protected
  166. release_to_normal : boolean;
  167. private
  168. tempidx : longint;
  169. end;
  170. ttempdeletenodeclass = class of ttempdeletenode;
  171. var
  172. cnothingnode : tnothingnodeclass;
  173. cerrornode : terrornodeclass;
  174. casmnode : tasmnodeclass;
  175. cstatementnode : tstatementnodeclass;
  176. cblocknode : tblocknodeclass;
  177. ctempcreatenode : ttempcreatenodeclass;
  178. ctemprefnode : ttemprefnodeclass;
  179. ctempdeletenode : ttempdeletenodeclass;
  180. { Create a blocknode and statement node for multiple statements
  181. generated internally by the parser }
  182. function internalstatements(var laststatement:tstatementnode):tblocknode;
  183. function laststatement(block:tblocknode):tstatementnode;
  184. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  185. implementation
  186. uses
  187. cutils,
  188. verbose,globals,systems,
  189. symconst,symdef,defutil,defcmp,
  190. pass_1,
  191. nutils,nld,ncal,nflw,
  192. procinfo
  193. ;
  194. {*****************************************************************************
  195. Helpers
  196. *****************************************************************************}
  197. function internalstatements(var laststatement:tstatementnode):tblocknode;
  198. begin
  199. { create dummy initial statement }
  200. laststatement := cstatementnode.create(cnothingnode.create,nil);
  201. internalstatements := cblocknode.create(laststatement);
  202. end;
  203. function laststatement(block:tblocknode):tstatementnode;
  204. begin
  205. result:=tstatementnode(block.left);
  206. while assigned(result) and assigned(result.right) do
  207. result:=tstatementnode(result.right);
  208. end;
  209. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  210. begin
  211. if assigned(laststatement.right) then
  212. internalerror(200204201);
  213. laststatement.right:=cstatementnode.create(n,nil);
  214. laststatement:=tstatementnode(laststatement.right);
  215. end;
  216. {*****************************************************************************
  217. TFIRSTNOTHING
  218. *****************************************************************************}
  219. constructor tnothingnode.create;
  220. begin
  221. inherited create(nothingn);
  222. end;
  223. function tnothingnode.pass_typecheck:tnode;
  224. begin
  225. result:=nil;
  226. resultdef:=voidtype;
  227. end;
  228. function tnothingnode.pass_1 : tnode;
  229. begin
  230. result:=nil;
  231. expectloc:=LOC_VOID;
  232. end;
  233. {*****************************************************************************
  234. TFIRSTERROR
  235. *****************************************************************************}
  236. constructor terrornode.create;
  237. begin
  238. inherited create(errorn);
  239. end;
  240. function terrornode.pass_typecheck:tnode;
  241. begin
  242. result:=nil;
  243. include(flags,nf_error);
  244. codegenerror:=true;
  245. resultdef:=generrordef;
  246. end;
  247. function terrornode.pass_1 : tnode;
  248. begin
  249. result:=nil;
  250. expectloc:=LOC_VOID;
  251. codegenerror:=true;
  252. end;
  253. procedure terrornode.mark_write;
  254. begin
  255. end;
  256. {*****************************************************************************
  257. TSTATEMENTNODE
  258. *****************************************************************************}
  259. constructor tstatementnode.create(l,r : tnode);
  260. begin
  261. inherited create(statementn,l,r);
  262. end;
  263. function is_exit_statement(var n: tnode; arg: pointer): foreachnoderesult;
  264. begin
  265. if (n.nodetype<>exitn) then
  266. result:=fen_false
  267. else
  268. result:=fen_norecurse_true;
  269. end;
  270. function no_exit_statement_in_block(n: tnode): boolean;
  271. begin
  272. result:=not foreachnodestatic(n,@is_exit_statement,nil);
  273. end;
  274. function tstatementnode.simplify : tnode;
  275. begin
  276. result:=nil;
  277. { these "optimizations" are only to make it more easy to recognise }
  278. { blocknodes which at the end of inlining only contain one single }
  279. { statement. Simplifying inside blocknode.simplify could be dangerous }
  280. { because if the main blocknode which makes up a procedure/function }
  281. { body were replaced with a statementn/nothingn, this could cause }
  282. { problems elsewhere in the compiler which expects a blocknode }
  283. { remove next statement if it's a nothing-statement (since if it's }
  284. { the last, it won't remove itself -- see next simplification) }
  285. while assigned(right) and
  286. (tstatementnode(right).left.nodetype = nothingn) do
  287. begin
  288. result:=tstatementnode(right).right;
  289. tstatementnode(right).right:=nil;
  290. right.free;
  291. right:=result;
  292. result:=nil;
  293. end;
  294. { Remove initial nothingn if there are other statements. If there }
  295. { are no other statements, returning nil doesn't help (will be }
  296. { interpreted as "can't be simplified") and replacing the }
  297. { statementnode with a nothingnode cannot be done (because it's }
  298. { possible this statementnode is a child of a blocknode, and }
  299. { blocknodes are expected to only contain statementnodes) }
  300. if (left.nodetype = nothingn) and
  301. assigned(right) then
  302. begin
  303. result:=right;
  304. right:=nil;
  305. exit;
  306. end;
  307. { if the current statement contains a block with one statement, }
  308. { replace the current statement with that block's statement }
  309. { (but only if the block does not have nf_block_with_exit set }
  310. { or has no exit statement, because otherwise it needs an own }
  311. { exit label, see tests/test/tinline10) }
  312. if (left.nodetype = blockn) and
  313. (not(nf_block_with_exit in left.flags) or
  314. no_exit_statement_in_block(left)) and
  315. assigned(tblocknode(left).left) and
  316. not assigned(tstatementnode(tblocknode(left).left).right) then
  317. begin
  318. result:=tblocknode(left).left;
  319. tstatementnode(result).right:=right;
  320. right:=nil;
  321. tblocknode(left).left:=nil;
  322. exit;
  323. end;
  324. end;
  325. function tstatementnode.pass_typecheck:tnode;
  326. begin
  327. result:=nil;
  328. resultdef:=voidtype;
  329. { left is the statement itself calln assignn or a complex one }
  330. typecheckpass(left);
  331. if (not (cs_extsyntax in current_settings.moduleswitches)) and
  332. assigned(left.resultdef) and
  333. not((left.nodetype=calln) and
  334. { don't complain when the value is used. And also not for constructors }
  335. ((cnf_return_value_used in tcallnode(left).callnodeflags) or
  336. (tcallnode(left).procdefinition.proctypeoption=potype_constructor))) and
  337. not(is_void(left.resultdef)) then
  338. CGMessage(parser_e_illegal_expression);
  339. if codegenerror then
  340. exit;
  341. { right is the next statement in the list }
  342. if assigned(right) then
  343. typecheckpass(right);
  344. if codegenerror then
  345. exit;
  346. end;
  347. function tstatementnode.pass_1 : tnode;
  348. begin
  349. result:=nil;
  350. { left is the statement itself calln assignn or a complex one }
  351. firstpass(left);
  352. if codegenerror then
  353. exit;
  354. expectloc:=left.expectloc;
  355. { right is the next in the list }
  356. if assigned(right) then
  357. firstpass(right);
  358. if codegenerror then
  359. exit;
  360. end;
  361. procedure tstatementnode.printnodetree(var t:text);
  362. begin
  363. printnodelist(t);
  364. end;
  365. {*****************************************************************************
  366. TBLOCKNODE
  367. *****************************************************************************}
  368. constructor tblocknode.create(l : tnode);
  369. begin
  370. inherited create(blockn,l);
  371. end;
  372. destructor tblocknode.destroy;
  373. var
  374. hp, next: tstatementnode;
  375. begin
  376. hp := tstatementnode(left);
  377. left := nil;
  378. while assigned(hp) do
  379. begin
  380. next := tstatementnode(hp.right);
  381. hp.right := nil;
  382. hp.free;
  383. hp := next;
  384. end;
  385. inherited destroy;
  386. end;
  387. function tblocknode.simplify: tnode;
  388. begin
  389. result := nil;
  390. { Warning: never replace a blocknode with another node type, }
  391. { since the block may be the main block of a procedure/function/ }
  392. { main program body, and those nodes should always be blocknodes }
  393. { since that's what the compiler expects elsewhere. }
  394. { if the current block contains only one statement, and }
  395. { this one statement only contains another block, replace }
  396. { this block with that other block. }
  397. if assigned(left) and
  398. not assigned(tstatementnode(left).right) and
  399. (tstatementnode(left).left.nodetype = blockn) then
  400. begin
  401. result:=tstatementnode(left).left;
  402. tstatementnode(left).left:=nil;
  403. { make sure the nf_block_with_exit flag is safeguarded }
  404. result.flags:=result.flags+(flags * [nf_block_with_exit]);
  405. exit;
  406. end;
  407. end;
  408. function tblocknode.pass_typecheck:tnode;
  409. var
  410. hp : tstatementnode;
  411. begin
  412. result:=nil;
  413. resultdef:=voidtype;
  414. hp:=tstatementnode(left);
  415. while assigned(hp) do
  416. begin
  417. if assigned(hp.left) then
  418. begin
  419. codegenerror:=false;
  420. typecheckpass(hp.left);
  421. if not(codegenerror) and
  422. not(cs_extsyntax in current_settings.moduleswitches) and
  423. (hp.left.nodetype=calln) and
  424. not(is_void(hp.left.resultdef)) and
  425. not(cnf_return_value_used in tcallnode(hp.left).callnodeflags) and
  426. not((tcallnode(hp.left).procdefinition.proctypeoption=potype_constructor) and
  427. assigned(tprocdef(tcallnode(hp.left).procdefinition)._class) and
  428. is_object(tprocdef(tcallnode(hp.left).procdefinition)._class)) then
  429. CGMessagePos(hp.left.fileinfo,parser_e_illegal_expression);
  430. { the resultdef of the block is the last type that is
  431. returned. Normally this is a voidtype. But when the
  432. compiler inserts a block of multiple statements then the
  433. last entry can return a value }
  434. resultdef:=hp.left.resultdef;
  435. end;
  436. hp:=tstatementnode(hp.right);
  437. end;
  438. end;
  439. function tblocknode.pass_1 : tnode;
  440. var
  441. hp : tstatementnode;
  442. count : longint;
  443. begin
  444. result:=nil;
  445. expectloc:=LOC_VOID;
  446. count:=0;
  447. hp:=tstatementnode(left);
  448. while assigned(hp) do
  449. begin
  450. if assigned(hp.left) then
  451. begin
  452. codegenerror:=false;
  453. firstpass(hp.left);
  454. hp.expectloc:=hp.left.expectloc;
  455. end;
  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. if allowreg and
  611. { temp must fit a single register }
  612. (tstoreddef(_typedef).is_fpuregable or
  613. (tstoreddef(_typedef).is_intregable and
  614. (_size<=TCGSize2Size[OS_64]))) and
  615. { size of register operations must be known }
  616. (def_cgsize(_typedef)<>OS_NO) and
  617. { no init/final needed }
  618. not (_typedef.needs_inittable) and
  619. ((_typedef.typ <> pointerdef) or
  620. (is_object(tpointerdef(_typedef).pointeddef) or
  621. not tpointerdef(_typedef).pointeddef.needs_inittable)) then
  622. include(tempinfo^.flags,ti_may_be_in_reg);
  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. n.tempinfo^.flags := tempinfo^.flags * tempinfostoreflags;
  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. exclude(tempinfo^.flags,ti_nextref_set_hookoncopy_nil);
  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. ppufile.getsmallset(tempinfo^.flags);
  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.putsmallset(tempinfo^.flags);
  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^.flags*tempinfostoreflags=tempinfo^.flags*tempinfostoreflags) 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(ptrint(tempinfo),sizeof(ptrint)*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 (ti_nextref_set_hookoncopy_nil in tempinfo^.flags) 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. (ti_may_be_in_reg in tempinfo^.flags) 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(ptrint(tempinfo),sizeof(ptrint)*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. include(tempinfo^.flags,ti_nextref_set_hookoncopy_nil);
  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(ptrint(tempinfo),sizeof(ptrint)*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.