nbas.pas 43 KB

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