nbas.pas 41 KB

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