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