nbas.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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 (cs_create_pic in current_settings.moduleswitches) and
  780. (tf_pic_uses_got in target_info.flags) and
  781. is_rtti_managed_type(tempinfo^.typedef) then
  782. include(current_procinfo.flags,pi_needs_got);
  783. if assigned(tempinfo^.withnode) then
  784. firstpass(tempinfo^.withnode);
  785. if assigned(tempinfo^.tempinitcode) then
  786. firstpass(tempinfo^.tempinitcode);
  787. inc(current_procinfo.estimatedtempsize,size);;
  788. end;
  789. function ttempcreatenode.pass_typecheck: tnode;
  790. begin
  791. result := nil;
  792. { a tempcreatenode doesn't have a resultdef, only temprefnodes do }
  793. resultdef := voidtype;
  794. if assigned(tempinfo^.withnode) then
  795. typecheckpass(tempinfo^.withnode);
  796. if assigned(tempinfo^.tempinitcode) then
  797. typecheckpass(tempinfo^.tempinitcode);
  798. end;
  799. function ttempcreatenode.docompare(p: tnode): boolean;
  800. begin
  801. result :=
  802. inherited docompare(p) and
  803. (ttempcreatenode(p).size = size) and
  804. (ttempcreatenode(p).tempinfo^.flags*tempinfostoreflags=tempinfo^.flags*tempinfostoreflags) and
  805. equal_defs(ttempcreatenode(p).tempinfo^.typedef,tempinfo^.typedef) and
  806. (ttempcreatenode(p).tempinfo^.withnode.isequal(tempinfo^.withnode)) and
  807. (ttempcreatenode(p).tempinfo^.tempinitcode.isequal(tempinfo^.tempinitcode));
  808. end;
  809. procedure ttempcreatenode.printnodedata(var t:text);
  810. begin
  811. inherited printnodedata(t);
  812. writeln(t,printnodeindention,'size = ',size,', temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  813. tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  814. writeln(t,printnodeindention,'tempinit =');
  815. printnode(t,tempinfo^.tempinitcode);
  816. end;
  817. {*****************************************************************************
  818. TEMPREFNODE
  819. *****************************************************************************}
  820. constructor ttemprefnode.create(const temp: ttempcreatenode);
  821. begin
  822. inherited create(temprefn);
  823. tempinfo := temp.tempinfo;
  824. offset:=0;
  825. end;
  826. constructor ttemprefnode.create_offset(const temp: ttempcreatenode;aoffset:longint);
  827. begin
  828. self.create(temp);
  829. offset := aoffset;
  830. end;
  831. function ttemprefnode.dogetcopy: tnode;
  832. var
  833. n: ttemprefnode;
  834. begin
  835. n := ttemprefnode(inherited dogetcopy);
  836. n.offset := offset;
  837. if assigned(tempinfo^.hookoncopy) then
  838. { if the temp has been copied, assume it becomes a new }
  839. { temp which has to be hooked by the copied reference }
  840. begin
  841. { hook the ref to the copied temp }
  842. n.tempinfo := tempinfo^.hookoncopy;
  843. { if we passed a ttempdeletenode that changed the temp }
  844. { from a persistent one into a normal one, we must be }
  845. { the last reference (since our parent should free the }
  846. { temp (JM) }
  847. if (ti_nextref_set_hookoncopy_nil in tempinfo^.flags) then
  848. tempinfo^.hookoncopy := nil;
  849. end
  850. else
  851. { if the temp we refer to hasn't been copied, assume }
  852. { we're just a new reference to that temp }
  853. begin
  854. n.tempinfo := tempinfo;
  855. end;
  856. if not assigned(n.tempinfo) then
  857. internalerror(2005071901);
  858. result := n;
  859. end;
  860. constructor ttemprefnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  861. begin
  862. inherited ppuload(t,ppufile);
  863. tempidx:=ppufile.getlongint;
  864. offset:=ppufile.getlongint;
  865. end;
  866. procedure ttemprefnode.ppuwrite(ppufile:tcompilerppufile);
  867. begin
  868. inherited ppuwrite(ppufile);
  869. ppufile.putlongint(tempinfo^.owner.ppuidx);
  870. ppufile.putlongint(offset);
  871. end;
  872. procedure ttemprefnode.resolveppuidx;
  873. var
  874. temp : ttempcreatenode;
  875. begin
  876. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  877. if temp.nodetype<>tempcreaten then
  878. internalerror(200311075);
  879. tempinfo:=temp.tempinfo;
  880. end;
  881. function ttemprefnode.pass_1 : tnode;
  882. begin
  883. expectloc := LOC_REFERENCE;
  884. if not tempinfo^.typedef.needs_inittable and
  885. (ti_may_be_in_reg in tempinfo^.flags) then
  886. begin
  887. if tempinfo^.typedef.typ=floatdef then
  888. begin
  889. if not use_vectorfpu(tempinfo^.typedef) then
  890. if (tempinfo^.temptype = tt_persistent) then
  891. expectloc := LOC_CFPUREGISTER
  892. else
  893. expectloc := LOC_FPUREGISTER
  894. else
  895. if (tempinfo^.temptype = tt_persistent) then
  896. expectloc := LOC_CMMREGISTER
  897. else
  898. expectloc := LOC_MMREGISTER
  899. end
  900. else
  901. begin
  902. if (tempinfo^.temptype = tt_persistent) then
  903. expectloc := LOC_CREGISTER
  904. else
  905. expectloc := LOC_REGISTER;
  906. end;
  907. end;
  908. result := nil;
  909. end;
  910. function ttemprefnode.pass_typecheck: tnode;
  911. begin
  912. { check if the temp is already resultdef passed }
  913. if not assigned(tempinfo^.typedef) then
  914. internalerror(200108233);
  915. result := nil;
  916. resultdef := tempinfo^.typedef;
  917. end;
  918. function ttemprefnode.docompare(p: tnode): boolean;
  919. begin
  920. result :=
  921. inherited docompare(p) and
  922. (ttemprefnode(p).tempinfo = tempinfo) and
  923. (ttemprefnode(p).offset = offset);
  924. end;
  925. procedure ttemprefnode.mark_write;
  926. begin
  927. include(flags,nf_write);
  928. end;
  929. procedure ttemprefnode.printnodedata(var t:text);
  930. begin
  931. inherited printnodedata(t);
  932. writeln(t,printnodeindention,'temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  933. tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  934. end;
  935. {*****************************************************************************
  936. TEMPDELETENODE
  937. *****************************************************************************}
  938. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  939. begin
  940. inherited create(tempdeleten);
  941. tempinfo := temp.tempinfo;
  942. release_to_normal := false;
  943. end;
  944. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  945. begin
  946. inherited create(tempdeleten);
  947. tempinfo := temp.tempinfo;
  948. release_to_normal := true;
  949. if tempinfo^.temptype <> tt_persistent then
  950. internalerror(200204211);
  951. end;
  952. function ttempdeletenode.dogetcopy: tnode;
  953. var
  954. n: ttempdeletenode;
  955. begin
  956. n:=ttempdeletenode(inherited dogetcopy);
  957. n.release_to_normal:=release_to_normal;
  958. if assigned(tempinfo^.hookoncopy) then
  959. { if the temp has been copied, assume it becomes a new }
  960. { temp which has to be hooked by the copied deletenode }
  961. begin
  962. { hook the tempdeletenode to the copied temp }
  963. n.tempinfo:=tempinfo^.hookoncopy;
  964. { the temp shall not be used, reset hookoncopy }
  965. { Only if release_to_normal is false, otherwise }
  966. { the temp can still be referenced once more (JM) }
  967. if (not release_to_normal) then
  968. tempinfo^.hookoncopy:=nil
  969. else
  970. include(tempinfo^.flags,ti_nextref_set_hookoncopy_nil);
  971. end
  972. else
  973. { if the temp we refer to hasn't been copied, we have a }
  974. { problem since that means we now have two delete nodes }
  975. { for one temp }
  976. internalerror(200108234);
  977. result:=n;
  978. end;
  979. constructor ttempdeletenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  980. begin
  981. inherited ppuload(t,ppufile);
  982. tempidx:=ppufile.getlongint;
  983. release_to_normal:=(ppufile.getbyte<>0);
  984. end;
  985. procedure ttempdeletenode.ppuwrite(ppufile:tcompilerppufile);
  986. begin
  987. inherited ppuwrite(ppufile);
  988. ppufile.putlongint(tempinfo^.owner.ppuidx);
  989. ppufile.putbyte(byte(release_to_normal));
  990. end;
  991. procedure ttempdeletenode.resolveppuidx;
  992. var
  993. temp : ttempcreatenode;
  994. begin
  995. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  996. if temp.nodetype<>tempcreaten then
  997. internalerror(200311075);
  998. tempinfo:=temp.tempinfo;
  999. end;
  1000. function ttempdeletenode.pass_1 : tnode;
  1001. begin
  1002. expectloc:=LOC_VOID;
  1003. result := nil;
  1004. end;
  1005. function ttempdeletenode.pass_typecheck: tnode;
  1006. begin
  1007. result := nil;
  1008. resultdef := voidtype;
  1009. end;
  1010. function ttempdeletenode.docompare(p: tnode): boolean;
  1011. begin
  1012. result :=
  1013. inherited docompare(p) and
  1014. (ttemprefnode(p).tempinfo = tempinfo);
  1015. end;
  1016. destructor ttempdeletenode.destroy;
  1017. begin
  1018. tempinfo^.withnode.free;
  1019. tempinfo^.tempinitcode.free;
  1020. dispose(tempinfo);
  1021. end;
  1022. procedure ttempdeletenode.printnodedata(var t:text);
  1023. begin
  1024. inherited printnodedata(t);
  1025. writeln(t,printnodeindention,'release_to_normal: ',release_to_normal,', temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  1026. tempinfo^.typedef.GetTypeName,'", temptype = ',tempinfo^.temptype,', tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  1027. end;
  1028. end.