nbas.pas 41 KB

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