nbas.pas 42 KB

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