nbas.pas 41 KB

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