nbas.pas 36 KB

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