nbas.pas 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  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. cgbase,cgutils,
  23. 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. {$ifdef DEBUG_NODE_XML}
  32. procedure XMLPrintNodeTree(var T: Text); override;
  33. {$endif DEBUG_NODE_XML}
  34. end;
  35. tnothingnodeclass = class of tnothingnode;
  36. terrornode = class(tnode)
  37. constructor create;virtual;
  38. function pass_1 : tnode;override;
  39. function pass_typecheck:tnode;override;
  40. procedure mark_write;override;
  41. end;
  42. terrornodeclass = class of terrornode;
  43. tspecializenode = class(tunarynode)
  44. sym:tsym;
  45. getaddr:boolean;
  46. inheriteddef:tdef;
  47. unit_specific:boolean;
  48. constructor create(l:tnode;g:boolean;s:tsym;u:boolean);virtual;
  49. constructor create_inherited(l:tnode;g:boolean;s:tsym;i:tdef);virtual;
  50. function pass_1:tnode;override;
  51. function pass_typecheck:tnode;override;
  52. end;
  53. tspecializenodeclass = class of tspecializenode;
  54. tfinalizetempsnode = class(tnode)
  55. constructor create;virtual;
  56. function pass_1 : tnode;override;
  57. function pass_typecheck:tnode;override;
  58. function docompare(p: tnode): boolean; override;
  59. end;
  60. tfinalizetempsnodeclass = class of tfinalizetempsnode;
  61. tasmnode = class(tnode)
  62. p_asm : TAsmList;
  63. currenttai : tai;
  64. { Used registers in assembler block }
  65. has_registerlist : boolean;
  66. constructor create(p : TAsmList);virtual;
  67. constructor create_get_position;
  68. destructor destroy;override;
  69. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  70. procedure ppuwrite(ppufile:tcompilerppufile);override;
  71. procedure buildderefimpl;override;
  72. procedure derefimpl;override;
  73. function dogetcopy : tnode;override;
  74. function pass_1 : tnode;override;
  75. function pass_typecheck:tnode;override;
  76. function docompare(p: tnode): boolean; override;
  77. {$ifdef DEBUG_NODE_XML}
  78. procedure XMLPrintNodeData(var T: Text); override;
  79. {$endif DEBUG_NODE_XML}
  80. end;
  81. tasmnodeclass = class of tasmnode;
  82. tstatementnode = class(tbinarynode)
  83. constructor create(l,r : tnode);virtual;
  84. function simplify(forinline : boolean) : tnode; override;
  85. function pass_1 : tnode;override;
  86. function pass_typecheck:tnode;override;
  87. procedure printnodetree(var t:text);override;
  88. property statement : tnode read left write left;
  89. property next : tnode read right write right;
  90. end;
  91. tstatementnodeclass = class of tstatementnode;
  92. tblocknode = class(tunarynode)
  93. constructor create(l : tnode);virtual;
  94. destructor destroy; override;
  95. function simplify(forinline : boolean) : tnode; override;
  96. function pass_1 : tnode;override;
  97. function pass_typecheck:tnode;override;
  98. {$ifdef state_tracking}
  99. function track_state_pass(exec_known:boolean):boolean;override;
  100. {$endif state_tracking}
  101. property statements : tnode read left write left;
  102. end;
  103. tblocknodeclass = class of tblocknode;
  104. ttempcreatenode = class;
  105. ttempinfoflag = (
  106. { temp can be kept in a register as far as the original creator is
  107. concerned }
  108. ti_may_be_in_reg,
  109. { the ttempcreatenode has been process and the temp's location is
  110. valid (-> the ttempdeletenode has not yet been processed, or
  111. in case it's a "create_to_normal()" one, the final ttemprefnode
  112. has not yet been processed) }
  113. ti_valid,
  114. { when performing a getcopy of a nodetree, we have to hook up the
  115. copies of ttemprefnodes and ttempdestroynode to the copied
  116. ttempinfo. this is done by setting hookoncopy in the original
  117. ttempinfo to point to the new one. if the temp is deleted via a
  118. regular ttempdeletenode, the hookoncopy is simply set to nil once
  119. it's processed. otherwise, it sets the ti_nextref_set_hookoncopy_nil
  120. and after processing the final ttemprefnode, hookoncopy is set to nil
  121. }
  122. ti_nextref_set_hookoncopy_nil,
  123. { the address of this temp is taken (-> cannot be kept in a register,
  124. even if the creator didn't mind)
  125. }
  126. ti_addr_taken,
  127. { temps can get an extra node tree that contains the value to which
  128. they should be initialised when they are created. this initialisation
  129. has to be performed right before the first reference to the temp.
  130. this flag indicates that the ttempcreatenode has been
  131. processed by pass_generate_code, but that the first ttemprefnode
  132. hasn't yet and hence will have to perform the initialisation
  133. }
  134. ti_executeinitialisation,
  135. { in case an expression like "inc(x[func()],1)" is translated into
  136. a regular addition, you have to create a temp to hold the address
  137. representing x[func()], since otherwise func() will be called twice
  138. and that can spell trouble in case it has side effects. on platforms
  139. without pointers, we cannot just take the address though. this flag
  140. has to be combined with ti_executeinitialisation above and will,
  141. rather than loading the value at the calculated location and store
  142. it in the temp, keep a copy of the calculated location if possible
  143. and required (not possible for regvars, because SSA may change their
  144. register, but not required for them either since calculating their
  145. location has no side-effects
  146. }
  147. ti_reference,
  148. { this temp only allows reading (makes it possible to safely use as
  149. reference under more circumstances)
  150. }
  151. ti_readonly,
  152. { if this is a managed temp, it doesn't have to be finalised before use
  153. }
  154. ti_nofini,
  155. { the value described by this temp. node is const/immutable, this is important for
  156. managed types like ansistrings where temp. refs are pointers to the actual value
  157. -- in this case, assignments to the temp do not increase the
  158. reference count, and if the assigned value was a temp itself then
  159. that temp is not deallocated until this temp is deleted (since
  160. otherwise the assigned value may be freed before the last use of
  161. the temp) }
  162. ti_const,
  163. { the temp. needs no final sync instruction if it is located in a register,
  164. so there are no loops involved in the usage of the temp.
  165. }
  166. ti_no_final_regsync,
  167. { this applied only to delete nodes: the single purpose of the temp. delete node is to clean up memory. In case
  168. of cse it might happen that the tempcreate node is optimized away so tempinfo is never initialized properly but
  169. the allocated memory must be disposed
  170. If a temp. node has this flag set, the life time of the temp. data must be determined by reg. life, the temp.
  171. location (in the sense of stack space/register) is never release }
  172. ti_cleanup_only
  173. );
  174. ttempinfoflags = set of ttempinfoflag;
  175. const
  176. tempinfostoreflags = [ti_may_be_in_reg,ti_addr_taken,ti_reference,ti_readonly,ti_no_final_regsync,ti_nofini,ti_const];
  177. type
  178. { to allow access to the location by temp references even after the temp has }
  179. { already been disposed and to make sure the coherency between temps and }
  180. { temp references is kept after a getcopy }
  181. ptempinfo = ^ttempinfo;
  182. ttempinfo = object
  183. private
  184. flags : ttempinfoflags;
  185. public
  186. { set to the copy of a tempcreate pnode (if it gets copied) so that the }
  187. { refs and deletenode can hook to this copy once they get copied too }
  188. hookoncopy : ptempinfo;
  189. typedef : tdef;
  190. typedefderef : tderef;
  191. temptype : ttemptype;
  192. owner : ttempcreatenode;
  193. withnode : tnode;
  194. location : tlocation;
  195. tempinitcode : tnode;
  196. end;
  197. ttempinfoaccessor = class
  198. class procedure settempinfoflags(tempinfo: ptempinfo; const flags: ttempinfoflags); virtual;
  199. class function gettempinfoflags(tempinfo: ptempinfo): ttempinfoflags; static; inline;
  200. end;
  201. ttempinfoaccessorclass = class of ttempinfoaccessor;
  202. ttempbasenode = class(tnode)
  203. protected
  204. class var tempinfoaccessor: ttempinfoaccessorclass;
  205. protected
  206. procedure settempinfoflags(const tempflags: ttempinfoflags); inline;
  207. function gettempinfoflags: ttempinfoflags; inline;
  208. public
  209. tempinfo: ptempinfo;
  210. procedure includetempflag(flag: ttempinfoflag); inline;
  211. procedure excludetempflag(flag: ttempinfoflag); inline;
  212. property tempflags: ttempinfoflags read gettempinfoflags write settempinfoflags;
  213. {$ifdef DEBUG_NODE_XML}
  214. procedure XMLPrintNodeInfo(var T: Text); override;
  215. procedure XMLPrintNodeData(var T: Text); override;
  216. {$endif DEBUG_NODE_XML}
  217. end;
  218. { a node which will create a (non)persistent temp of a given type with a given }
  219. { size (the size is separate to allow creating "void" temps with a custom size) }
  220. ttempcreatenode = class(ttempbasenode)
  221. size: tcgint;
  222. ftemplvalue : tnode;
  223. { * persistent temps are used in manually written code where the temp }
  224. { be usable among different statements and where you can manually say }
  225. { when the temp has to be freed (using a ttempdeletenode) }
  226. { * non-persistent temps are mostly used in typeconversion helpers, }
  227. { where the node that receives the temp becomes responsible for }
  228. { freeing it. In this last case, you must use only one reference }
  229. { to it and *not* generate a ttempdeletenode }
  230. constructor create(_typedef: tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean); virtual;
  231. constructor create_withnode(_typedef: tdef; _size: tcgint; _temptype: ttemptype; allowreg:boolean; withnode: tnode); virtual;
  232. constructor create_value(_typedef:tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean; templvalue: tnode);
  233. constructor create_reference(_typedef:tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean; templvalue: tnode; readonly: boolean);
  234. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  235. procedure ppuwrite(ppufile:tcompilerppufile);override;
  236. procedure buildderefimpl;override;
  237. procedure derefimpl;override;
  238. function dogetcopy: tnode; override;
  239. function pass_1 : tnode; override;
  240. function pass_typecheck: tnode; override;
  241. function docompare(p: tnode): boolean; override;
  242. procedure printnodedata(var t:text);override;
  243. {$ifdef DEBUG_NODE_XML}
  244. procedure XMLPrintNodeData(var T: Text); override;
  245. {$endif DEBUG_NODE_XML}
  246. end;
  247. ttempcreatenodeclass = class of ttempcreatenode;
  248. { a node which is a reference to a certain temp }
  249. ttemprefnode = class(ttempbasenode)
  250. constructor create(const temp: ttempcreatenode); virtual;
  251. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  252. procedure ppuwrite(ppufile:tcompilerppufile);override;
  253. procedure resolveppuidx;override;
  254. function dogetcopy: tnode; override;
  255. function pass_1 : tnode; override;
  256. function pass_typecheck : tnode; override;
  257. procedure mark_write;override;
  258. function docompare(p: tnode): boolean; override;
  259. procedure printnodedata(var t:text);override;
  260. private
  261. tempidx : longint;
  262. end;
  263. ttemprefnodeclass = class of ttemprefnode;
  264. { a node which removes a temp }
  265. ttempdeletenode = class(ttempbasenode)
  266. constructor create(const temp: ttempcreatenode); virtual;
  267. { this will convert the persistant temp to a normal temp
  268. for returning to the other nodes }
  269. constructor create_normal_temp(const temp: ttempcreatenode);
  270. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  271. procedure ppuwrite(ppufile:tcompilerppufile);override;
  272. procedure resolveppuidx;override;
  273. function dogetcopy: tnode; override;
  274. function pass_1: tnode; override;
  275. function pass_typecheck: tnode; override;
  276. function docompare(p: tnode): boolean; override;
  277. destructor destroy; override;
  278. procedure printnodedata(var t:text);override;
  279. {$ifdef DEBUG_NODE_XML}
  280. procedure XMLPrintNodeData(var T: Text); override;
  281. {$endif DEBUG_NODE_XML}
  282. protected
  283. release_to_normal : boolean;
  284. private
  285. tempidx : longint;
  286. end;
  287. ttempdeletenodeclass = class of ttempdeletenode;
  288. var
  289. cnothingnode : tnothingnodeclass = tnothingnode;
  290. cerrornode : terrornodeclass = terrornode;
  291. cspecializenode : tspecializenodeclass = tspecializenode;
  292. cfinalizetempsnode: tfinalizetempsnodeclass = tfinalizetempsnode;
  293. casmnode : tasmnodeclass = tasmnode;
  294. cstatementnode : tstatementnodeclass = tstatementnode;
  295. cblocknode : tblocknodeclass = tblocknode;
  296. ctempinfoaccessor : ttempinfoaccessorclass = ttempinfoaccessor;
  297. ctempcreatenode : ttempcreatenodeclass = ttempcreatenode;
  298. ctemprefnode : ttemprefnodeclass = ttemprefnode;
  299. ctempdeletenode : ttempdeletenodeclass = ttempdeletenode;
  300. { Create a blocknode and statement node for multiple statements
  301. generated internally by the parser }
  302. function internalstatements(out laststatement:tstatementnode):tblocknode;
  303. function laststatement(block:tblocknode):tstatementnode;
  304. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  305. { if the complexity of n is "high", creates a reference temp to n's
  306. location and replace n with a ttemprefnode referring to that location }
  307. function maybereplacewithtempref(var n: tnode; var block: tblocknode; var stat: tstatementnode; size: ASizeInt; readonly: boolean): ttempcreatenode;
  308. { same as above, but create a regular temp rather than reference temp }
  309. function maybereplacewithtemp(var n: tnode; var block: tblocknode; var stat: tstatementnode; size: ASizeInt; allowreg: boolean): ttempcreatenode;
  310. implementation
  311. uses
  312. verbose,globals,systems,
  313. ppu,
  314. symconst,symdef,defutil,defcmp,
  315. pass_1,
  316. nutils,nld,ncnv,
  317. procinfo
  318. {$ifdef DEBUG_NODE_XML}
  319. {$ifndef jvm}
  320. ,
  321. cpubase,
  322. cutils,
  323. itcpugas
  324. {$endif jvm}
  325. {$endif DEBUG_NODE_XML}
  326. ;
  327. {*****************************************************************************
  328. Helpers
  329. *****************************************************************************}
  330. function internalstatements(out laststatement:tstatementnode):tblocknode;
  331. begin
  332. { create dummy initial statement }
  333. laststatement := cstatementnode.create(cnothingnode.create,nil);
  334. internalstatements := cblocknode.create(laststatement);
  335. end;
  336. function laststatement(block:tblocknode):tstatementnode;
  337. begin
  338. result:=tstatementnode(block.left);
  339. while assigned(result) and assigned(result.right) do
  340. result:=tstatementnode(result.right);
  341. end;
  342. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  343. begin
  344. if assigned(laststatement.right) then
  345. internalerror(200204201);
  346. laststatement.right:=cstatementnode.create(n,nil);
  347. laststatement:=tstatementnode(laststatement.right);
  348. end;
  349. function maybereplacewithtempref(var n: tnode; var block: tblocknode; var stat: tstatementnode; size: ASizeInt; readonly: boolean): ttempcreatenode;
  350. begin
  351. result:=nil;
  352. if (node_complexity(n)>4) or
  353. might_have_sideeffects(n) then
  354. begin
  355. result:=ctempcreatenode.create_reference(n.resultdef,size,tt_persistent,true,n,readonly);
  356. typecheckpass(tnode(result));
  357. n:=ctemprefnode.create(result);
  358. typecheckpass(n);
  359. if not assigned(stat) then
  360. block:=internalstatements(stat);
  361. addstatement(stat,result)
  362. end;
  363. end;
  364. function maybereplacewithtemp(var n: tnode; var block: tblocknode; var stat: tstatementnode; size: ASizeInt; allowreg: boolean): ttempcreatenode;
  365. begin
  366. result:=nil;
  367. if (node_complexity(n)>4) or
  368. might_have_sideeffects(n) then
  369. begin
  370. result:=ctempcreatenode.create_value(n.resultdef,size,tt_persistent,allowreg,n);
  371. typecheckpass(tnode(result));
  372. n:=ctemprefnode.create(result);
  373. typecheckpass(n);
  374. if not assigned(stat) then
  375. block:=internalstatements(stat);
  376. addstatement(stat,result)
  377. end;
  378. end;
  379. {*****************************************************************************
  380. TFIRSTNOTHING
  381. *****************************************************************************}
  382. constructor tnothingnode.create;
  383. begin
  384. inherited create(nothingn);
  385. end;
  386. function tnothingnode.pass_typecheck:tnode;
  387. begin
  388. result:=nil;
  389. resultdef:=voidtype;
  390. end;
  391. function tnothingnode.pass_1 : tnode;
  392. begin
  393. result:=nil;
  394. expectloc:=LOC_VOID;
  395. end;
  396. {$ifdef DEBUG_NODE_XML}
  397. procedure TNothingNode.XMLPrintNodeTree(var T: Text);
  398. begin
  399. Write(T, PrintNodeIndention, '<', nodetype2str[nodetype]);
  400. XMLPrintNodeInfo(T);
  401. { "Nothing nodes" contain no data, so just use "/>" to terminate it early }
  402. WriteLn(T, ' />');
  403. end;
  404. {$endif DEBUG_NODE_XML}
  405. {*****************************************************************************
  406. TFIRSTERROR
  407. *****************************************************************************}
  408. constructor terrornode.create;
  409. begin
  410. inherited create(errorn);
  411. end;
  412. function terrornode.pass_typecheck:tnode;
  413. begin
  414. result:=nil;
  415. include(flags,nf_error);
  416. codegenerror:=true;
  417. resultdef:=generrordef;
  418. end;
  419. function terrornode.pass_1 : tnode;
  420. begin
  421. result:=nil;
  422. expectloc:=LOC_VOID;
  423. codegenerror:=true;
  424. end;
  425. procedure terrornode.mark_write;
  426. begin
  427. end;
  428. {*****************************************************************************
  429. TSPECIALIZENODE
  430. *****************************************************************************}
  431. constructor tspecializenode.create(l:tnode;g:boolean;s:tsym;u:boolean);
  432. begin
  433. inherited create(specializen,l);
  434. sym:=s;
  435. getaddr:=g;
  436. unit_specific:=u;
  437. end;
  438. constructor tspecializenode.create_inherited(l:tnode;g:boolean;s:tsym;i:tdef);
  439. begin
  440. create(l,g,s,false);
  441. inheriteddef:=i;
  442. end;
  443. function tspecializenode.pass_typecheck:tnode;
  444. begin
  445. result:=nil;
  446. resultdef:=cundefinedtype;
  447. end;
  448. function tspecializenode.pass_1:tnode;
  449. begin
  450. { such a node should not reach pass_1 }
  451. internalerror(2015071704);
  452. result:=nil;
  453. expectloc:=LOC_VOID;
  454. codegenerror:=true;
  455. end;
  456. {*****************************************************************************
  457. TFINALIZETEMPSNODE
  458. *****************************************************************************}
  459. constructor tfinalizetempsnode.create;
  460. begin
  461. inherited create(finalizetempsn);
  462. end;
  463. function tfinalizetempsnode.pass_1: tnode;
  464. begin
  465. result:=nil;
  466. expectloc:=LOC_VOID;
  467. end;
  468. function tfinalizetempsnode.pass_typecheck: tnode;
  469. begin
  470. resultdef:=voidtype;
  471. result:=nil;
  472. end;
  473. function tfinalizetempsnode.docompare(p: tnode): boolean;
  474. begin
  475. { these nodes should never be coalesced }
  476. result:=false;
  477. end;
  478. {*****************************************************************************
  479. TSTATEMENTNODE
  480. *****************************************************************************}
  481. constructor tstatementnode.create(l,r : tnode);
  482. begin
  483. inherited create(statementn,l,r);
  484. end;
  485. function is_exit_statement(var n: tnode; arg: pointer): foreachnoderesult;
  486. begin
  487. if (n.nodetype<>exitn) then
  488. result:=fen_false
  489. else
  490. result:=fen_norecurse_true;
  491. end;
  492. function no_exit_statement_in_block(n: tnode): boolean;
  493. begin
  494. result:=not foreachnodestatic(n,@is_exit_statement,nil);
  495. end;
  496. function tstatementnode.simplify(forinline: boolean) : tnode;
  497. begin
  498. result:=nil;
  499. { these "optimizations" are only to make it more easy to recognise }
  500. { blocknodes which at the end of inlining only contain one single }
  501. { statement. Simplifying inside blocknode.simplify could be dangerous }
  502. { because if the main blocknode which makes up a procedure/function }
  503. { body were replaced with a statementn/nothingn, this could cause }
  504. { problems elsewhere in the compiler which expects a blocknode }
  505. { remove next statement if it's a nothing-statement (since if it's }
  506. { the last, it won't remove itself -- see next simplification) }
  507. while assigned(right) and
  508. (tstatementnode(right).left.nodetype = nothingn) do
  509. begin
  510. result:=tstatementnode(right).right;
  511. tstatementnode(right).right:=nil;
  512. right.free;
  513. right:=result;
  514. result:=nil;
  515. end;
  516. { Remove initial nothingn if there are other statements. If there }
  517. { are no other statements, returning nil doesn't help (will be }
  518. { interpreted as "can't be simplified") and replacing the }
  519. { statementnode with a nothingnode cannot be done (because it's }
  520. { possible this statementnode is a child of a blocknode, and }
  521. { blocknodes are expected to only contain statementnodes) }
  522. if (left.nodetype = nothingn) and
  523. assigned(right) then
  524. begin
  525. result:=right;
  526. right:=nil;
  527. exit;
  528. end;
  529. { if the current statement contains a block with one statement,
  530. replace the current statement with that block's statement
  531. (but only if the block does not have nf_block_with_exit set
  532. or has no exit statement, because otherwise it needs an own
  533. exit label, see tests/test/tinline10)
  534. Further, it might not be the user code entry
  535. }
  536. if (left.nodetype = blockn) and
  537. ((left.flags*[nf_block_with_exit,nf_usercode_entry]=[]) or
  538. ((left.flags*[nf_block_with_exit,nf_usercode_entry]=[nf_block_with_exit]) and no_exit_statement_in_block(left))) and
  539. assigned(tblocknode(left).left) and
  540. not assigned(tstatementnode(tblocknode(left).left).right) then
  541. begin
  542. result:=tblocknode(left).left;
  543. tstatementnode(result).right:=right;
  544. right:=nil;
  545. tblocknode(left).left:=nil;
  546. exit;
  547. end;
  548. end;
  549. function tstatementnode.pass_typecheck:tnode;
  550. begin
  551. result:=nil;
  552. resultdef:=voidtype;
  553. { left is the statement itself calln assignn or a complex one }
  554. typecheckpass(left);
  555. if codegenerror then
  556. exit;
  557. { right is the next statement in the list }
  558. if assigned(right) then
  559. typecheckpass(right);
  560. if codegenerror then
  561. exit;
  562. end;
  563. function tstatementnode.pass_1 : tnode;
  564. begin
  565. result:=nil;
  566. { left is the statement itself calln assignn or a complex one }
  567. firstpass(left);
  568. if codegenerror then
  569. exit;
  570. expectloc:=left.expectloc;
  571. { right is the next in the list }
  572. if assigned(right) then
  573. firstpass(right);
  574. if codegenerror then
  575. exit;
  576. end;
  577. procedure tstatementnode.printnodetree(var t:text);
  578. begin
  579. printnodelist(t);
  580. end;
  581. {*****************************************************************************
  582. TBLOCKNODE
  583. *****************************************************************************}
  584. constructor tblocknode.create(l : tnode);
  585. begin
  586. inherited create(blockn,l);
  587. end;
  588. destructor tblocknode.destroy;
  589. var
  590. hp, next: tstatementnode;
  591. begin
  592. hp := tstatementnode(left);
  593. left := nil;
  594. while assigned(hp) do
  595. begin
  596. next := tstatementnode(hp.right);
  597. hp.right := nil;
  598. hp.free;
  599. hp := next;
  600. end;
  601. inherited destroy;
  602. end;
  603. function NodesEqual(var n: tnode; arg: pointer): foreachnoderesult;
  604. begin
  605. if n.IsEqual(tnode(arg)) then
  606. result:=fen_norecurse_true
  607. else
  608. result:=fen_false;
  609. end;
  610. function tblocknode.simplify(forinline : boolean): tnode;
  611. {$ifdef break_inlining}
  612. var
  613. a : array[0..3] of tstatementnode;
  614. {$endif break_inlining}
  615. begin
  616. result := nil;
  617. { Warning: never replace a blocknode with another node type,
  618. since the block may be the main block of a procedure/function/
  619. main program body, and those nodes should always be blocknodes
  620. since that's what the compiler expects elsewhere. }
  621. if assigned(left) and
  622. not assigned(tstatementnode(left).right) then
  623. begin
  624. case tstatementnode(left).left.nodetype of
  625. blockn:
  626. begin
  627. { if the current block contains only one statement, and
  628. this one statement only contains another block, replace
  629. this block with that other block. }
  630. result:=tstatementnode(left).left;
  631. tstatementnode(left).left:=nil;
  632. { make sure the nf_block_with_exit flag is safeguarded }
  633. result.flags:=result.flags+(flags*[nf_block_with_exit,nf_usercode_entry]);
  634. exit;
  635. end;
  636. nothingn:
  637. begin
  638. { if the block contains only a statement with a nothing node,
  639. get rid of the statement }
  640. left.Free;
  641. left:=nil;
  642. exit;
  643. end;
  644. else
  645. ;
  646. end;
  647. end;
  648. {$ifdef break_inlining}
  649. { simple sequence of tempcreate, assign and return temp.? }
  650. if GetStatements(left,a) and
  651. (a[0].left.nodetype=tempcreaten) and
  652. (a[1].left.nodetype=assignn) and
  653. (actualtargetnode(@tassignmentnode(a[1].left).left)^.nodetype=temprefn) and
  654. (a[2].left.nodetype=tempdeleten) and
  655. (a[3].left.nodetype=temprefn) and
  656. (ttempcreatenode(a[0].left).tempinfo=ttemprefnode(actualtargetnode(@tassignmentnode(a[1].left).left)^).tempinfo) and
  657. (ttempcreatenode(a[0].left).tempinfo=ttempdeletenode(a[2].left).tempinfo) and
  658. (ttempcreatenode(a[0].left).tempinfo=ttemprefnode(a[3].left).tempinfo) and
  659. { the temp. node might not be references inside the assigned expression }
  660. not(foreachnodestatic(tassignmentnode(a[1].left).right,@NodesEqual,actualtargetnode(@tassignmentnode(a[1].left).left)^)) then
  661. begin
  662. result:=tassignmentnode(a[1].left).right;
  663. tassignmentnode(a[1].left).right:=nil;
  664. { ensure the node is first passed, so the resultdef does not get changed if the
  665. the type conv. below is merged }
  666. firstpass(result);
  667. result:=ctypeconvnode.create_internal(result,ttemprefnode(a[3].left).resultdef);
  668. firstpass(result);
  669. exit;
  670. end;
  671. {$endif break_inlining}
  672. end;
  673. function tblocknode.pass_typecheck:tnode;
  674. var
  675. hp : tstatementnode;
  676. begin
  677. result:=nil;
  678. resultdef:=voidtype;
  679. hp:=tstatementnode(left);
  680. while assigned(hp) do
  681. begin
  682. if assigned(hp.left) then
  683. begin
  684. codegenerror:=false;
  685. typecheckpass(hp.left);
  686. { the resultdef of the block is the last type that is
  687. returned. Normally this is a voidtype. But when the
  688. compiler inserts a block of multiple statements then the
  689. last entry can return a value }
  690. resultdef:=hp.left.resultdef;
  691. end;
  692. hp:=tstatementnode(hp.right);
  693. end;
  694. end;
  695. function tblocknode.pass_1 : tnode;
  696. var
  697. hp : tstatementnode;
  698. //count : longint;
  699. begin
  700. result:=nil;
  701. expectloc:=LOC_VOID;
  702. //count:=0;
  703. hp:=tstatementnode(left);
  704. while assigned(hp) do
  705. begin
  706. if assigned(hp.left) then
  707. begin
  708. codegenerror:=false;
  709. firstpass(hp.left);
  710. hp.expectloc:=hp.left.expectloc;
  711. end;
  712. expectloc:=hp.expectloc;
  713. //inc(count);
  714. hp:=tstatementnode(hp.right);
  715. end;
  716. end;
  717. {$ifdef state_tracking}
  718. function Tblocknode.track_state_pass(exec_known:boolean):boolean;
  719. var hp:Tstatementnode;
  720. begin
  721. track_state_pass:=false;
  722. hp:=Tstatementnode(left);
  723. while assigned(hp) do
  724. begin
  725. if hp.left.track_state_pass(exec_known) then
  726. track_state_pass:=true;
  727. hp:=Tstatementnode(hp.right);
  728. end;
  729. end;
  730. {$endif state_tracking}
  731. {*****************************************************************************
  732. TASMNODE
  733. *****************************************************************************}
  734. constructor tasmnode.create(p : TAsmList);
  735. begin
  736. inherited create(asmn);
  737. p_asm:=p;
  738. currenttai:=nil;
  739. end;
  740. constructor tasmnode.create_get_position;
  741. begin
  742. inherited create(asmn);
  743. p_asm:=nil;
  744. include(flags,nf_get_asm_position);
  745. currenttai:=nil;
  746. end;
  747. destructor tasmnode.destroy;
  748. begin
  749. if assigned(p_asm) then
  750. p_asm.free;
  751. inherited destroy;
  752. end;
  753. constructor tasmnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  754. var
  755. hp : tai;
  756. begin
  757. inherited ppuload(t,ppufile);
  758. if not(nf_get_asm_position in flags) then
  759. begin
  760. p_asm:=TAsmList.create;
  761. repeat
  762. hp:=ppuloadai(ppufile);
  763. if hp=nil then
  764. break;
  765. p_asm.concat(hp);
  766. if hp.typ=ait_section then
  767. inc(p_asm.section_count);
  768. until false;
  769. end
  770. else
  771. p_asm:=nil;
  772. currenttai:=nil;
  773. end;
  774. procedure tasmnode.ppuwrite(ppufile:tcompilerppufile);
  775. var
  776. hp : tai;
  777. begin
  778. inherited ppuwrite(ppufile);
  779. { TODO: FIXME Add saving of register sets}
  780. if not(nf_get_asm_position in flags) then
  781. begin
  782. hp:=tai(p_asm.first);
  783. while assigned(hp) do
  784. begin
  785. ppuwriteai(ppufile,hp);
  786. hp:=tai(hp.next);
  787. end;
  788. { end is marked by a nil }
  789. ppuwriteai(ppufile,nil);
  790. end;
  791. end;
  792. procedure tasmnode.buildderefimpl;
  793. var
  794. hp : tai;
  795. begin
  796. inherited buildderefimpl;
  797. if not(nf_get_asm_position in flags) then
  798. begin
  799. hp:=tai(p_asm.first);
  800. while assigned(hp) do
  801. begin
  802. hp.buildderefimpl;
  803. hp:=tai(hp.next);
  804. end;
  805. end;
  806. end;
  807. procedure tasmnode.derefimpl;
  808. var
  809. hp : tai;
  810. begin
  811. inherited derefimpl;
  812. if not(nf_get_asm_position in flags) then
  813. begin
  814. hp:=tai(p_asm.first);
  815. while assigned(hp) do
  816. begin
  817. hp.derefimpl;
  818. hp:=tai(hp.next);
  819. end;
  820. end;
  821. end;
  822. function tasmnode.dogetcopy: tnode;
  823. var
  824. n: tasmnode;
  825. begin
  826. n := tasmnode(inherited dogetcopy);
  827. if assigned(p_asm) then
  828. begin
  829. n.p_asm:=TAsmList.create;
  830. n.p_asm.concatlistcopy(p_asm);
  831. end
  832. else n.p_asm := nil;
  833. n.currenttai:=currenttai;
  834. n.has_registerlist:=has_registerlist;
  835. result:=n;
  836. end;
  837. function tasmnode.pass_typecheck:tnode;
  838. begin
  839. result:=nil;
  840. resultdef:=voidtype;
  841. if not(nf_get_asm_position in flags) then
  842. include(current_procinfo.flags,pi_has_assembler_block);
  843. end;
  844. function tasmnode.pass_1 : tnode;
  845. begin
  846. result:=nil;
  847. expectloc:=LOC_VOID;
  848. end;
  849. function tasmnode.docompare(p: tnode): boolean;
  850. begin
  851. { comparing of asmlists is not implemented (JM) }
  852. docompare := false;
  853. end;
  854. {$ifdef DEBUG_NODE_XML}
  855. procedure TAsmNode.XMLPrintNodeData(var T: Text);
  856. procedure PadString(var S: string; Len: Integer);
  857. var
  858. X, C: Integer;
  859. begin
  860. C := Length(S);
  861. if C < Len then
  862. begin
  863. SetLength(S, 7);
  864. for X := C + 1 to Len do
  865. S[X] := ' '
  866. end;
  867. end;
  868. {$ifndef jvm}
  869. function FormatOp(const Oper: POper): string;
  870. begin
  871. case Oper^.typ of
  872. top_const:
  873. begin
  874. case Oper^.val of
  875. -15..15:
  876. Result := '$' + tostr(Oper^.val);
  877. $10..$FF:
  878. Result := '$0x' + hexstr(Oper^.val, 2);
  879. $100..$FFFF:
  880. Result := '$0x' + hexstr(Oper^.val, 4);
  881. $10000..$FFFFFFFF:
  882. Result := '$0x' + hexstr(Oper^.val, 8);
  883. else
  884. Result := '$0x' + hexstr(Oper^.val, 16);
  885. end;
  886. end;
  887. top_reg:
  888. Result := gas_regname(Oper^.reg);
  889. top_ref:
  890. with Oper^.ref^ do
  891. begin
  892. {$if defined(x86)}
  893. if segment <> NR_NO then
  894. Result := gas_regname(segment) + ':'
  895. else
  896. {$endif defined(x86)}
  897. Result := '';
  898. if Assigned(symbol) then
  899. begin
  900. Result := Result + symbol.Name;
  901. if offset > 0 then
  902. Result := Result + '+';
  903. end;
  904. if offset <> 0 then
  905. Result := Result + tostr(offset)
  906. else
  907. Result := Result;
  908. if (base <> NR_NO) or (index <> NR_NO) then
  909. begin
  910. Result := Result + '(';
  911. if base <> NR_NO then
  912. begin
  913. Result := Result + gas_regname(base);
  914. if index <> NR_NO then
  915. Result := Result + ',';
  916. end;
  917. if index <> NR_NO then
  918. Result := Result + gas_regname(index);
  919. if scalefactor <> 0 then
  920. Result := Result + ',' + tostr(scalefactor) + ')'
  921. else
  922. Result := Result + ')';
  923. end;
  924. end;
  925. top_bool:
  926. begin
  927. if Oper^.b then
  928. Result := 'TRUE'
  929. else
  930. Result := 'FALSE';
  931. end
  932. else
  933. Result := '';
  934. end;
  935. end;
  936. {$if defined(x86)}
  937. procedure ProcessInstruction(p: tai); inline;
  938. var
  939. ThisOp, ThisOper: string;
  940. X: Integer;
  941. begin
  942. case p.typ of
  943. ait_label:
  944. WriteLn(T, PrintNodeIndention, tai_label(p).labsym.name);
  945. ait_instruction:
  946. begin
  947. ThisOp := gas_op2str[taicpu(p).opcode]+cond2str[taicpu(p).condition];
  948. if gas_needsuffix[taicpu(p).opcode] <> AttSufNONE then
  949. ThisOp := ThisOp + gas_opsize2str[taicpu(p).opsize];
  950. { Pad the opcode with spaces so the succeeding operands are aligned }
  951. PadString(ThisOp, 7);
  952. Write(T, PrintNodeIndention, ' ', ThisOp); { Extra indentation to account for label formatting }
  953. for X := 0 to taicpu(p).ops - 1 do
  954. begin
  955. Write(T, ' ');
  956. ThisOper := FormatOp(taicpu(p).oper[X]);
  957. if X < taicpu(p).ops - 1 then
  958. begin
  959. ThisOper := ThisOper + ',';
  960. PadString(ThisOper, 7);
  961. end;
  962. Write(T, ThisOper);
  963. end;
  964. WriteLn(T);
  965. end;
  966. else
  967. { Do nothing };
  968. end;
  969. end;
  970. var
  971. hp: tai;
  972. begin
  973. if not Assigned(p_asm) then
  974. Exit;
  975. hp := tai(p_asm.First);
  976. while Assigned(hp) do
  977. begin
  978. ProcessInstruction(hp);
  979. hp := tai(hp.Next);
  980. end;
  981. {$else defined(x86)}
  982. begin
  983. WriteLn(T, PrintNodeIndention, '(Assembler output not currently supported on this platform)');
  984. {$endif defined(x86)}
  985. {$else jvm}
  986. begin
  987. WriteLn(T, PrintNodeIndention, '(Should assembly language even be possible under JVM?)');
  988. {$endif jvm}
  989. end;
  990. {$endif DEBUG_NODE_XML}
  991. {*****************************************************************************
  992. TEMPBASENODE
  993. *****************************************************************************}
  994. class procedure ttempinfoaccessor.settempinfoflags(tempinfo: ptempinfo; const flags: ttempinfoflags);
  995. begin
  996. tempinfo^.flags:=flags;
  997. end;
  998. class function ttempinfoaccessor.gettempinfoflags(tempinfo: ptempinfo): ttempinfoflags;
  999. begin
  1000. result:=tempinfo^.flags;
  1001. end;
  1002. {*****************************************************************************
  1003. TEMPBASENODE
  1004. *****************************************************************************}
  1005. procedure ttempbasenode.settempinfoflags(const tempflags: ttempinfoflags);
  1006. begin
  1007. ctempinfoaccessor.settempinfoflags(tempinfo,tempflags);
  1008. end;
  1009. function ttempbasenode.gettempinfoflags: ttempinfoflags;
  1010. begin
  1011. result:=ctempinfoaccessor.gettempinfoflags(tempinfo);
  1012. end;
  1013. procedure ttempbasenode.includetempflag(flag: ttempinfoflag);
  1014. begin
  1015. { go through settempinfoflags() so it can filter out unsupported tempflags }
  1016. settempinfoflags(gettempinfoflags+[flag])
  1017. end;
  1018. procedure ttempbasenode.excludetempflag(flag: ttempinfoflag);
  1019. begin
  1020. { go through settempinfoflags() so it can prevent required tempflags from
  1021. being removed (if any) }
  1022. settempinfoflags(gettempinfoflags-[flag])
  1023. end;
  1024. {$ifdef DEBUG_NODE_XML}
  1025. procedure TTempBaseNode.XMLPrintNodeInfo(var T: Text);
  1026. begin
  1027. inherited XMLPrintNodeInfo(T);
  1028. { The raw pointer is the only way to uniquely identify the temp }
  1029. Write(T, ' id="', WritePointer(tempinfo), '"');
  1030. end;
  1031. procedure TTempBaseNode.XMLPrintNodeData(var T: Text);
  1032. var
  1033. Flag: TTempInfoFlag;
  1034. NotFirst: Boolean;
  1035. begin
  1036. inherited XMLPrintNodeData(t);
  1037. if not assigned(tempinfo) then
  1038. exit;
  1039. WriteLn(T, PrintNodeIndention, '<typedef>', SanitiseXMLString(tempinfo^.typedef.typesymbolprettyname), '</typedef>');
  1040. NotFirst := False;
  1041. for Flag := Low(TTempInfoFlag) to High(TTempInfoFlag) do
  1042. if (Flag in tempinfo^.flags) then
  1043. if not NotFirst then
  1044. begin
  1045. Write(T, PrintNodeIndention, '<tempflags>', Flag);
  1046. NotFirst := True;
  1047. end
  1048. else
  1049. Write(T, ',', Flag);
  1050. if NotFirst then
  1051. WriteLn(T, '</tempflags>')
  1052. else
  1053. WriteLn(T, PrintNodeIndention, '<tempflags />');
  1054. WriteLn(T, PrintNodeIndention, '<temptype>', tempinfo^.temptype, '</temptype>');
  1055. end;
  1056. {$endif DEBUG_NODE_XML}
  1057. {*****************************************************************************
  1058. TEMPCREATENODE
  1059. *****************************************************************************}
  1060. constructor ttempcreatenode.create(_typedef:tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean);
  1061. begin
  1062. inherited create(tempcreaten);
  1063. size := _size;
  1064. new(tempinfo);
  1065. fillchar(tempinfo^,sizeof(tempinfo^),0);
  1066. tempinfo^.typedef := _typedef;
  1067. tempinfo^.temptype := _temptype;
  1068. tempinfo^.owner := self;
  1069. tempinfo^.withnode := nil;
  1070. if allowreg and
  1071. { temp must fit a single register }
  1072. (tstoreddef(_typedef).is_fpuregable or
  1073. (tstoreddef(_typedef).is_intregable and
  1074. (_size<=TCGSize2Size[OS_64]))) and
  1075. { size of register operations must be known }
  1076. (def_cgsize(_typedef)<>OS_NO) and
  1077. { no init/final needed }
  1078. not is_managed_type(_typedef) then
  1079. includetempflag(ti_may_be_in_reg);
  1080. end;
  1081. constructor ttempcreatenode.create_withnode(_typedef: tdef; _size: tcgint; _temptype: ttemptype; allowreg:boolean; withnode: tnode);
  1082. begin
  1083. self.create(_typedef,_size,_temptype,allowreg);
  1084. tempinfo^.withnode:=withnode.getcopy;
  1085. end;
  1086. constructor ttempcreatenode.create_value(_typedef:tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean; templvalue: tnode);
  1087. begin
  1088. self.create(_typedef,_size,_temptype,allowreg);
  1089. // store in ppuwrite
  1090. ftemplvalue:=templvalue;
  1091. // create from stored ftemplvalue in ppuload
  1092. tempinfo^.tempinitcode:=cassignmentnode.create(ctemprefnode.create(self),ftemplvalue);
  1093. end;
  1094. constructor ttempcreatenode.create_reference(_typedef: tdef; _size: tcgint; _temptype: ttemptype; allowreg: boolean; templvalue: tnode; readonly: boolean);
  1095. begin
  1096. // store in ppuwrite
  1097. self.create(_typedef,_size,_temptype,allowreg);
  1098. ftemplvalue:=templvalue;
  1099. // no assignment node, just the tempvalue
  1100. tempinfo^.tempinitcode:=ftemplvalue;
  1101. includetempflag(ti_reference);
  1102. if readonly then
  1103. includetempflag(ti_readonly);
  1104. end;
  1105. function ttempcreatenode.dogetcopy: tnode;
  1106. var
  1107. n: ttempcreatenode;
  1108. begin
  1109. n := ttempcreatenode(inherited dogetcopy);
  1110. n.size := size;
  1111. new(n.tempinfo);
  1112. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  1113. n.tempinfo^.owner:=n;
  1114. n.tempinfo^.typedef := tempinfo^.typedef;
  1115. n.tempinfo^.temptype := tempinfo^.temptype;
  1116. n.tempflags := tempflags * tempinfostoreflags;
  1117. { when the tempinfo has already a hookoncopy then it is not
  1118. reset by a tempdeletenode }
  1119. if assigned(tempinfo^.hookoncopy) then
  1120. internalerror(200211262);
  1121. { signal the temprefs that the temp they point to has been copied, }
  1122. { so that if the refs get copied as well, they can hook themselves }
  1123. { to the copy of the temp }
  1124. tempinfo^.hookoncopy := n.tempinfo;
  1125. excludetempflag(ti_nextref_set_hookoncopy_nil);
  1126. if assigned(tempinfo^.withnode) then
  1127. n.tempinfo^.withnode := tempinfo^.withnode.getcopy
  1128. else
  1129. n.tempinfo^.withnode := nil;
  1130. if assigned(tempinfo^.tempinitcode) then
  1131. n.tempinfo^.tempinitcode := tempinfo^.tempinitcode.getcopy
  1132. else
  1133. n.tempinfo^.tempinitcode := nil;
  1134. result := n;
  1135. end;
  1136. constructor ttempcreatenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1137. begin
  1138. inherited ppuload(t,ppufile);
  1139. size:=ppufile.getlongint;
  1140. new(tempinfo);
  1141. fillchar(tempinfo^,sizeof(tempinfo^),0);
  1142. ppufile.getset(tppuset2(tempinfo^.flags));
  1143. ppufile.getderef(tempinfo^.typedefderef);
  1144. tempinfo^.temptype := ttemptype(ppufile.getbyte);
  1145. tempinfo^.owner:=self;
  1146. tempinfo^.withnode:=ppuloadnode(ppufile);
  1147. ftemplvalue:=ppuloadnode(ppufile);
  1148. end;
  1149. procedure ttempcreatenode.ppuwrite(ppufile:tcompilerppufile);
  1150. begin
  1151. inherited ppuwrite(ppufile);
  1152. ppufile.putlongint(size);
  1153. ppufile.putset(tppuset2(tempinfo^.flags));
  1154. ppufile.putderef(tempinfo^.typedefderef);
  1155. ppufile.putbyte(byte(tempinfo^.temptype));
  1156. ppuwritenode(ppufile,tempinfo^.withnode);
  1157. ppuwritenode(ppufile,ftemplvalue);
  1158. end;
  1159. procedure ttempcreatenode.buildderefimpl;
  1160. begin
  1161. inherited buildderefimpl;
  1162. tempinfo^.typedefderef.build(tempinfo^.typedef);
  1163. if assigned(tempinfo^.withnode) then
  1164. tempinfo^.withnode.buildderefimpl;
  1165. if assigned(ftemplvalue) then
  1166. ftemplvalue.buildderefimpl;
  1167. end;
  1168. procedure ttempcreatenode.derefimpl;
  1169. begin
  1170. inherited derefimpl;
  1171. tempinfo^.typedef:=tdef(tempinfo^.typedefderef.resolve);
  1172. if assigned(tempinfo^.withnode) then
  1173. tempinfo^.withnode.derefimpl;
  1174. if assigned(ftemplvalue) then
  1175. begin
  1176. ftemplvalue.derefimpl;
  1177. tempinfo^.tempinitcode:=cassignmentnode.create(ctemprefnode.create(self),ftemplvalue);
  1178. end;
  1179. end;
  1180. function ttempcreatenode.pass_1 : tnode;
  1181. begin
  1182. result := nil;
  1183. expectloc:=LOC_VOID;
  1184. { temps which are immutable do not need to be initialized/finalized }
  1185. if (tempinfo^.typedef.needs_inittable) and not(ti_const in tempflags) then
  1186. include(current_procinfo.flags,pi_needs_implicit_finally);
  1187. if assigned(tempinfo^.withnode) then
  1188. firstpass(tempinfo^.withnode);
  1189. if assigned(tempinfo^.tempinitcode) then
  1190. firstpass(tempinfo^.tempinitcode);
  1191. inc(current_procinfo.estimatedtempsize,size);
  1192. { if a temp. create node is loaded from a ppu, it could be that the unit was compiled with other settings which
  1193. enabled a certain type to be stored in a register while the current settings do not support this, so correct this here
  1194. if needed
  1195. }
  1196. if not(tstoreddef(tempinfo^.typedef).is_fpuregable) and not(tstoreddef(tempinfo^.typedef).is_intregable) and (ti_may_be_in_reg in tempflags) then
  1197. excludetempflag(ti_may_be_in_reg);
  1198. end;
  1199. function ttempcreatenode.pass_typecheck: tnode;
  1200. begin
  1201. result := nil;
  1202. { a tempcreatenode doesn't have a resultdef, only temprefnodes do }
  1203. resultdef := voidtype;
  1204. if assigned(tempinfo^.withnode) then
  1205. typecheckpass(tempinfo^.withnode);
  1206. if assigned(tempinfo^.tempinitcode) then
  1207. typecheckpass(tempinfo^.tempinitcode);
  1208. end;
  1209. function ttempcreatenode.docompare(p: tnode): boolean;
  1210. begin
  1211. result :=
  1212. inherited docompare(p) and
  1213. (ttempcreatenode(p).size = size) and
  1214. (ttempcreatenode(p).tempflags*tempinfostoreflags=tempflags*tempinfostoreflags) and
  1215. equal_defs(ttempcreatenode(p).tempinfo^.typedef,tempinfo^.typedef) and
  1216. (ttempcreatenode(p).tempinfo^.withnode.isequal(tempinfo^.withnode)) and
  1217. (ttempcreatenode(p).tempinfo^.tempinitcode.isequal(tempinfo^.tempinitcode));
  1218. end;
  1219. procedure ttempcreatenode.printnodedata(var t:text);
  1220. var
  1221. f: ttempinfoflag;
  1222. first: Boolean;
  1223. begin
  1224. inherited printnodedata(t);
  1225. writeln(t,printnodeindention,'size = ',size,', temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  1226. tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  1227. write(t,printnodeindention,'[');
  1228. first:=true;
  1229. for f in tempflags do
  1230. begin
  1231. if not(first) then
  1232. write(t,',');
  1233. write(t,f);
  1234. first:=false;
  1235. end;
  1236. writeln(t,']');
  1237. writeln(t,printnodeindention,'tempinit =');
  1238. printnode(t,tempinfo^.tempinitcode);
  1239. end;
  1240. {$ifdef DEBUG_NODE_XML}
  1241. procedure TTempCreateNode.XMLPrintNodeData(var T: Text);
  1242. begin
  1243. inherited XMLPrintNodeData(T);
  1244. WriteLn(T, PrintNodeIndention, '<size>', size, '</size>');
  1245. if Assigned(TempInfo^.TempInitCode) then
  1246. begin
  1247. WriteLn(T, PrintNodeIndention, '<tempinit>');
  1248. PrintNodeIndent;
  1249. XMLPrintNode(T, TempInfo^.TempInitCode);
  1250. PrintNodeUnindent;
  1251. WriteLn(T, PrintNodeIndention, '</tempinit>');
  1252. end
  1253. else
  1254. WriteLn(T, PrintNodeIndention, '<tempinit />');
  1255. end;
  1256. {$endif DEBUG_NODE_XML}
  1257. {*****************************************************************************
  1258. TEMPREFNODE
  1259. *****************************************************************************}
  1260. constructor ttemprefnode.create(const temp: ttempcreatenode);
  1261. begin
  1262. inherited create(temprefn);
  1263. tempinfo := temp.tempinfo;
  1264. end;
  1265. function ttemprefnode.dogetcopy: tnode;
  1266. var
  1267. n: ttemprefnode;
  1268. begin
  1269. n := ttemprefnode(inherited dogetcopy);
  1270. if assigned(tempinfo^.hookoncopy) then
  1271. { if the temp has been copied, assume it becomes a new }
  1272. { temp which has to be hooked by the copied reference }
  1273. begin
  1274. { hook the ref to the copied temp }
  1275. n.tempinfo := tempinfo^.hookoncopy;
  1276. { if we passed a ttempdeletenode that changed the temp }
  1277. { from a persistent one into a normal one, we must be }
  1278. { the last reference (since our parent should free the }
  1279. { temp (JM) }
  1280. if (ti_nextref_set_hookoncopy_nil in tempflags) then
  1281. tempinfo^.hookoncopy := nil;
  1282. end
  1283. else
  1284. { if the temp we refer to hasn't been copied, assume }
  1285. { we're just a new reference to that temp }
  1286. begin
  1287. n.tempinfo := tempinfo;
  1288. end;
  1289. if not assigned(n.tempinfo) then
  1290. internalerror(2005071901);
  1291. result := n;
  1292. end;
  1293. constructor ttemprefnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1294. begin
  1295. inherited ppuload(t,ppufile);
  1296. tempidx:=ppufile.getlongint;
  1297. end;
  1298. procedure ttemprefnode.ppuwrite(ppufile:tcompilerppufile);
  1299. begin
  1300. inherited ppuwrite(ppufile);
  1301. ppufile.putlongint(tempinfo^.owner.ppuidx);
  1302. end;
  1303. procedure ttemprefnode.resolveppuidx;
  1304. var
  1305. temp : ttempcreatenode;
  1306. begin
  1307. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  1308. if temp.nodetype<>tempcreaten then
  1309. internalerror(200311075);
  1310. tempinfo:=temp.tempinfo;
  1311. end;
  1312. function ttemprefnode.pass_1 : tnode;
  1313. begin
  1314. expectloc := LOC_REFERENCE;
  1315. if not tempinfo^.typedef.needs_inittable and
  1316. (ti_may_be_in_reg in tempflags) then
  1317. begin
  1318. if tempinfo^.typedef.typ=floatdef then
  1319. begin
  1320. if not use_vectorfpu(tempinfo^.typedef) then
  1321. if (tempinfo^.temptype = tt_persistent) then
  1322. expectloc := LOC_CFPUREGISTER
  1323. else
  1324. expectloc := LOC_FPUREGISTER
  1325. else
  1326. if (tempinfo^.temptype = tt_persistent) then
  1327. expectloc := LOC_CMMREGISTER
  1328. else
  1329. expectloc := LOC_MMREGISTER
  1330. end
  1331. else
  1332. begin
  1333. if (tempinfo^.temptype = tt_persistent) then
  1334. expectloc := LOC_CREGISTER
  1335. else
  1336. expectloc := LOC_REGISTER;
  1337. end;
  1338. end;
  1339. result := nil;
  1340. end;
  1341. function ttemprefnode.pass_typecheck: tnode;
  1342. begin
  1343. { check if the temp is already resultdef passed }
  1344. if not assigned(tempinfo^.typedef) then
  1345. internalerror(200108233);
  1346. result := nil;
  1347. resultdef := tempinfo^.typedef;
  1348. end;
  1349. function ttemprefnode.docompare(p: tnode): boolean;
  1350. begin
  1351. result :=
  1352. inherited docompare(p) and
  1353. (ttemprefnode(p).tempinfo = tempinfo);
  1354. end;
  1355. procedure ttemprefnode.mark_write;
  1356. begin
  1357. include(flags,nf_write);
  1358. end;
  1359. procedure ttemprefnode.printnodedata(var t:text);
  1360. var
  1361. f : ttempinfoflag;
  1362. notfirst : Boolean;
  1363. begin
  1364. inherited printnodedata(t);
  1365. write(t,printnodeindention,'temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  1366. tempinfo^.typedef.GetTypeName,'", (tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2),' flags = [');
  1367. notfirst:=false;
  1368. for f in tempinfo^.flags do
  1369. begin
  1370. if notfirst then
  1371. write(t,',');
  1372. write(t,f);
  1373. notfirst:=true;
  1374. end;
  1375. writeln(t,'])');
  1376. end;
  1377. {*****************************************************************************
  1378. TEMPDELETENODE
  1379. *****************************************************************************}
  1380. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  1381. begin
  1382. inherited create(tempdeleten);
  1383. tempinfo := temp.tempinfo;
  1384. release_to_normal := false;
  1385. end;
  1386. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  1387. begin
  1388. inherited create(tempdeleten);
  1389. tempinfo := temp.tempinfo;
  1390. release_to_normal := true;
  1391. if tempinfo^.temptype <> tt_persistent then
  1392. internalerror(200204211);
  1393. end;
  1394. function ttempdeletenode.dogetcopy: tnode;
  1395. var
  1396. n: ttempdeletenode;
  1397. begin
  1398. n:=ttempdeletenode(inherited dogetcopy);
  1399. n.release_to_normal:=release_to_normal;
  1400. if assigned(tempinfo^.hookoncopy) then
  1401. { if the temp has been copied, assume it becomes a new }
  1402. { temp which has to be hooked by the copied deletenode }
  1403. begin
  1404. { hook the tempdeletenode to the copied temp }
  1405. n.tempinfo:=tempinfo^.hookoncopy;
  1406. { the temp shall not be used, reset hookoncopy }
  1407. { Only if release_to_normal is false, otherwise }
  1408. { the temp can still be referenced once more (JM) }
  1409. if (not release_to_normal) then
  1410. tempinfo^.hookoncopy:=nil
  1411. else
  1412. includetempflag(ti_nextref_set_hookoncopy_nil);
  1413. end
  1414. else
  1415. { if the temp we refer to hasn't been copied, we have a }
  1416. { problem since that means we now have two delete nodes }
  1417. { for one temp }
  1418. internalerror(200108234);
  1419. result:=n;
  1420. end;
  1421. constructor ttempdeletenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1422. begin
  1423. inherited ppuload(t,ppufile);
  1424. tempidx:=ppufile.getlongint;
  1425. release_to_normal:=(ppufile.getbyte<>0);
  1426. end;
  1427. procedure ttempdeletenode.ppuwrite(ppufile:tcompilerppufile);
  1428. begin
  1429. inherited ppuwrite(ppufile);
  1430. ppufile.putlongint(tempinfo^.owner.ppuidx);
  1431. ppufile.putbyte(byte(release_to_normal));
  1432. end;
  1433. procedure ttempdeletenode.resolveppuidx;
  1434. var
  1435. temp : ttempcreatenode;
  1436. begin
  1437. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  1438. if temp.nodetype<>tempcreaten then
  1439. internalerror(2003110701);
  1440. tempinfo:=temp.tempinfo;
  1441. end;
  1442. function ttempdeletenode.pass_1 : tnode;
  1443. begin
  1444. expectloc:=LOC_VOID;
  1445. result := nil;
  1446. end;
  1447. function ttempdeletenode.pass_typecheck: tnode;
  1448. begin
  1449. result := nil;
  1450. resultdef := voidtype;
  1451. end;
  1452. function ttempdeletenode.docompare(p: tnode): boolean;
  1453. begin
  1454. result :=
  1455. inherited docompare(p) and
  1456. (ttemprefnode(p).tempinfo = tempinfo);
  1457. end;
  1458. destructor ttempdeletenode.destroy;
  1459. begin
  1460. tempinfo^.withnode.free;
  1461. tempinfo^.tempinitcode.free;
  1462. dispose(tempinfo);
  1463. inherited destroy;
  1464. end;
  1465. procedure ttempdeletenode.printnodedata(var t:text);
  1466. begin
  1467. inherited printnodedata(t);
  1468. writeln(t,printnodeindention,'release_to_normal: ',release_to_normal,', temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  1469. tempinfo^.typedef.GetTypeName,'", temptype = ',tempinfo^.temptype,', tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  1470. end;
  1471. {$ifdef DEBUG_NODE_XML}
  1472. procedure TTempDeleteNode.XMLPrintNodeData(var T: Text);
  1473. begin
  1474. inherited XMLPrintNodeData(T);
  1475. WriteLn(T, PrintNodeIndention, '<release_to_normal>', release_to_normal, '</release_to_normal>');
  1476. end;
  1477. {$endif DEBUG_NODE_XML}
  1478. end.