nbas.pas 37 KB

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