nbas.pas 45 KB

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