nbas.pas 40 KB

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