nbas.pas 42 KB

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