nbas.pas 40 KB

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