nbas.pas 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  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. FirstNode: Boolean;
  699. //count : longint;
  700. begin
  701. result:=nil;
  702. expectloc:=LOC_VOID;
  703. //count:=0;
  704. hp:=tstatementnode(left);
  705. FirstNode := True;
  706. while assigned(hp) do
  707. begin
  708. if assigned(hp.left) then
  709. begin
  710. { Calling firstpass on a statement node normally causes
  711. problems, so we have to be a little bit hacky if we want
  712. to remove nothing nodes }
  713. codegenerror:=false;
  714. firstpass(tnode(hp));
  715. { If the first node gets deleted, left must be updated,
  716. otherwise it will be a dangling pointer }
  717. if FirstNode then
  718. begin
  719. { If multiple statements are to be removed, they would
  720. have all been removed by the firstpass call above, so
  721. the next one will not be removed, hence it's safe to
  722. set FirstNode to false }
  723. FirstNode := False;
  724. left := hp;
  725. end;
  726. if assigned(hp.left) then
  727. hp.expectloc:=hp.left.expectloc;
  728. end;
  729. expectloc:=hp.expectloc;
  730. //inc(count);
  731. hp:=tstatementnode(hp.right);
  732. end;
  733. end;
  734. {$ifdef state_tracking}
  735. function Tblocknode.track_state_pass(exec_known:boolean):boolean;
  736. var hp:Tstatementnode;
  737. begin
  738. track_state_pass:=false;
  739. hp:=Tstatementnode(left);
  740. while assigned(hp) do
  741. begin
  742. if hp.left.track_state_pass(exec_known) then
  743. track_state_pass:=true;
  744. hp:=Tstatementnode(hp.right);
  745. end;
  746. end;
  747. {$endif state_tracking}
  748. {*****************************************************************************
  749. TASMNODE
  750. *****************************************************************************}
  751. constructor tasmnode.create(p : TAsmList);
  752. begin
  753. inherited create(asmn);
  754. p_asm:=p;
  755. currenttai:=nil;
  756. end;
  757. constructor tasmnode.create_get_position;
  758. begin
  759. inherited create(asmn);
  760. p_asm:=nil;
  761. include(flags,nf_get_asm_position);
  762. currenttai:=nil;
  763. end;
  764. destructor tasmnode.destroy;
  765. begin
  766. if assigned(p_asm) then
  767. p_asm.free;
  768. inherited destroy;
  769. end;
  770. constructor tasmnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  771. var
  772. hp : tai;
  773. begin
  774. inherited ppuload(t,ppufile);
  775. if not(nf_get_asm_position in flags) then
  776. begin
  777. p_asm:=TAsmList.create;
  778. repeat
  779. hp:=ppuloadai(ppufile);
  780. if hp=nil then
  781. break;
  782. p_asm.concat(hp);
  783. if hp.typ=ait_section then
  784. inc(p_asm.section_count);
  785. until false;
  786. end
  787. else
  788. p_asm:=nil;
  789. currenttai:=nil;
  790. end;
  791. procedure tasmnode.ppuwrite(ppufile:tcompilerppufile);
  792. var
  793. hp : tai;
  794. begin
  795. inherited ppuwrite(ppufile);
  796. { TODO: FIXME Add saving of register sets}
  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. ppuwriteai(ppufile,hp);
  803. hp:=tai(hp.next);
  804. end;
  805. { end is marked by a nil }
  806. ppuwriteai(ppufile,nil);
  807. end;
  808. end;
  809. procedure tasmnode.buildderefimpl;
  810. var
  811. hp : tai;
  812. begin
  813. inherited buildderefimpl;
  814. if not(nf_get_asm_position in flags) then
  815. begin
  816. hp:=tai(p_asm.first);
  817. while assigned(hp) do
  818. begin
  819. hp.buildderefimpl;
  820. hp:=tai(hp.next);
  821. end;
  822. end;
  823. end;
  824. procedure tasmnode.derefimpl;
  825. var
  826. hp : tai;
  827. begin
  828. inherited derefimpl;
  829. if not(nf_get_asm_position in flags) then
  830. begin
  831. hp:=tai(p_asm.first);
  832. while assigned(hp) do
  833. begin
  834. hp.derefimpl;
  835. hp:=tai(hp.next);
  836. end;
  837. end;
  838. end;
  839. function tasmnode.dogetcopy: tnode;
  840. var
  841. n: tasmnode;
  842. begin
  843. n := tasmnode(inherited dogetcopy);
  844. if assigned(p_asm) then
  845. begin
  846. n.p_asm:=TAsmList.create;
  847. n.p_asm.concatlistcopy(p_asm);
  848. end
  849. else n.p_asm := nil;
  850. n.currenttai:=currenttai;
  851. n.has_registerlist:=has_registerlist;
  852. result:=n;
  853. end;
  854. function tasmnode.pass_typecheck:tnode;
  855. begin
  856. result:=nil;
  857. resultdef:=voidtype;
  858. if not(nf_get_asm_position in flags) then
  859. include(current_procinfo.flags,pi_has_assembler_block);
  860. end;
  861. function tasmnode.pass_1 : tnode;
  862. begin
  863. result:=nil;
  864. expectloc:=LOC_VOID;
  865. end;
  866. function tasmnode.docompare(p: tnode): boolean;
  867. begin
  868. { comparing of asmlists is not implemented (JM) }
  869. docompare := false;
  870. end;
  871. {$ifdef DEBUG_NODE_XML}
  872. procedure TAsmNode.XMLPrintNodeData(var T: Text);
  873. procedure PadString(var S: string; Len: Integer);
  874. var
  875. X, C: Integer;
  876. begin
  877. C := Length(S);
  878. if C < Len then
  879. begin
  880. SetLength(S, 7);
  881. for X := C + 1 to Len do
  882. S[X] := ' '
  883. end;
  884. end;
  885. {$ifndef jvm}
  886. function FormatOp(const Oper: POper): string;
  887. begin
  888. case Oper^.typ of
  889. top_const:
  890. begin
  891. case Oper^.val of
  892. -15..15:
  893. Result := '$' + tostr(Oper^.val);
  894. $10..$FF:
  895. Result := '$0x' + hexstr(Oper^.val, 2);
  896. $100..$FFFF:
  897. Result := '$0x' + hexstr(Oper^.val, 4);
  898. $10000..$FFFFFFFF:
  899. Result := '$0x' + hexstr(Oper^.val, 8);
  900. else
  901. Result := '$0x' + hexstr(Oper^.val, 16);
  902. end;
  903. end;
  904. top_reg:
  905. Result := gas_regname(Oper^.reg);
  906. top_ref:
  907. with Oper^.ref^ do
  908. begin
  909. {$if defined(x86)}
  910. if segment <> NR_NO then
  911. Result := gas_regname(segment) + ':'
  912. else
  913. {$endif defined(x86)}
  914. Result := '';
  915. if Assigned(symbol) then
  916. begin
  917. Result := Result + symbol.Name;
  918. if offset > 0 then
  919. Result := Result + '+';
  920. end;
  921. if offset <> 0 then
  922. Result := Result + tostr(offset)
  923. else
  924. Result := Result;
  925. if (base <> NR_NO) or (index <> NR_NO) then
  926. begin
  927. Result := Result + '(';
  928. if base <> NR_NO then
  929. begin
  930. Result := Result + gas_regname(base);
  931. if index <> NR_NO then
  932. Result := Result + ',';
  933. end;
  934. if index <> NR_NO then
  935. Result := Result + gas_regname(index);
  936. if scalefactor <> 0 then
  937. Result := Result + ',' + tostr(scalefactor) + ')'
  938. else
  939. Result := Result + ')';
  940. end;
  941. end;
  942. top_bool:
  943. begin
  944. if Oper^.b then
  945. Result := 'TRUE'
  946. else
  947. Result := 'FALSE';
  948. end
  949. else
  950. Result := '';
  951. end;
  952. end;
  953. {$if defined(x86)}
  954. procedure ProcessInstruction(p: tai); inline;
  955. var
  956. ThisOp, ThisOper: string;
  957. X: Integer;
  958. begin
  959. case p.typ of
  960. ait_label:
  961. WriteLn(T, PrintNodeIndention, tai_label(p).labsym.name);
  962. ait_instruction:
  963. begin
  964. ThisOp := gas_op2str[taicpu(p).opcode]+cond2str[taicpu(p).condition];
  965. if gas_needsuffix[taicpu(p).opcode] <> AttSufNONE then
  966. ThisOp := ThisOp + gas_opsize2str[taicpu(p).opsize];
  967. { Pad the opcode with spaces so the succeeding operands are aligned }
  968. PadString(ThisOp, 7);
  969. Write(T, PrintNodeIndention, ' ', ThisOp); { Extra indentation to account for label formatting }
  970. for X := 0 to taicpu(p).ops - 1 do
  971. begin
  972. Write(T, ' ');
  973. ThisOper := FormatOp(taicpu(p).oper[X]);
  974. if X < taicpu(p).ops - 1 then
  975. begin
  976. ThisOper := ThisOper + ',';
  977. PadString(ThisOper, 7);
  978. end;
  979. Write(T, ThisOper);
  980. end;
  981. WriteLn(T);
  982. end;
  983. else
  984. { Do nothing };
  985. end;
  986. end;
  987. var
  988. hp: tai;
  989. begin
  990. if not Assigned(p_asm) then
  991. Exit;
  992. hp := tai(p_asm.First);
  993. while Assigned(hp) do
  994. begin
  995. ProcessInstruction(hp);
  996. hp := tai(hp.Next);
  997. end;
  998. {$else defined(x86)}
  999. begin
  1000. WriteLn(T, PrintNodeIndention, '(Assembler output not currently supported on this platform)');
  1001. {$endif defined(x86)}
  1002. {$else jvm}
  1003. begin
  1004. WriteLn(T, PrintNodeIndention, '(Should assembly language even be possible under JVM?)');
  1005. {$endif jvm}
  1006. end;
  1007. {$endif DEBUG_NODE_XML}
  1008. {*****************************************************************************
  1009. TEMPBASENODE
  1010. *****************************************************************************}
  1011. class procedure ttempinfoaccessor.settempinfoflags(tempinfo: ptempinfo; const flags: ttempinfoflags);
  1012. begin
  1013. tempinfo^.flags:=flags;
  1014. end;
  1015. class function ttempinfoaccessor.gettempinfoflags(tempinfo: ptempinfo): ttempinfoflags;
  1016. begin
  1017. result:=tempinfo^.flags;
  1018. end;
  1019. {*****************************************************************************
  1020. TEMPBASENODE
  1021. *****************************************************************************}
  1022. procedure ttempbasenode.settempinfoflags(const tempflags: ttempinfoflags);
  1023. begin
  1024. ctempinfoaccessor.settempinfoflags(tempinfo,tempflags);
  1025. end;
  1026. function ttempbasenode.gettempinfoflags: ttempinfoflags;
  1027. begin
  1028. result:=ctempinfoaccessor.gettempinfoflags(tempinfo);
  1029. end;
  1030. procedure ttempbasenode.includetempflag(flag: ttempinfoflag);
  1031. begin
  1032. { go through settempinfoflags() so it can filter out unsupported tempflags }
  1033. settempinfoflags(gettempinfoflags+[flag])
  1034. end;
  1035. procedure ttempbasenode.excludetempflag(flag: ttempinfoflag);
  1036. begin
  1037. { go through settempinfoflags() so it can prevent required tempflags from
  1038. being removed (if any) }
  1039. settempinfoflags(gettempinfoflags-[flag])
  1040. end;
  1041. {$ifdef DEBUG_NODE_XML}
  1042. procedure TTempBaseNode.XMLPrintNodeInfo(var T: Text);
  1043. begin
  1044. inherited XMLPrintNodeInfo(T);
  1045. { The raw pointer is the only way to uniquely identify the temp }
  1046. Write(T, ' id="', WritePointer(tempinfo), '"');
  1047. end;
  1048. procedure TTempBaseNode.XMLPrintNodeData(var T: Text);
  1049. var
  1050. Flag: TTempInfoFlag;
  1051. NotFirst: Boolean;
  1052. begin
  1053. inherited XMLPrintNodeData(t);
  1054. if not assigned(tempinfo) then
  1055. exit;
  1056. WriteLn(T, PrintNodeIndention, '<typedef>', SanitiseXMLString(tempinfo^.typedef.typesymbolprettyname), '</typedef>');
  1057. NotFirst := False;
  1058. for Flag := Low(TTempInfoFlag) to High(TTempInfoFlag) do
  1059. if (Flag in tempinfo^.flags) then
  1060. if not NotFirst then
  1061. begin
  1062. Write(T, PrintNodeIndention, '<tempflags>', Flag);
  1063. NotFirst := True;
  1064. end
  1065. else
  1066. Write(T, ',', Flag);
  1067. if NotFirst then
  1068. WriteLn(T, '</tempflags>')
  1069. else
  1070. WriteLn(T, PrintNodeIndention, '<tempflags />');
  1071. WriteLn(T, PrintNodeIndention, '<temptype>', tempinfo^.temptype, '</temptype>');
  1072. end;
  1073. {$endif DEBUG_NODE_XML}
  1074. {*****************************************************************************
  1075. TEMPCREATENODE
  1076. *****************************************************************************}
  1077. constructor ttempcreatenode.create(_typedef:tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean);
  1078. begin
  1079. inherited create(tempcreaten);
  1080. size := _size;
  1081. new(tempinfo);
  1082. fillchar(tempinfo^,sizeof(tempinfo^),0);
  1083. tempinfo^.typedef := _typedef;
  1084. tempinfo^.temptype := _temptype;
  1085. tempinfo^.owner := self;
  1086. tempinfo^.withnode := nil;
  1087. if allowreg and
  1088. { temp must fit a single register }
  1089. (tstoreddef(_typedef).is_fpuregable or
  1090. (tstoreddef(_typedef).is_intregable and
  1091. (_size<=TCGSize2Size[OS_64]))) and
  1092. { size of register operations must be known }
  1093. (def_cgsize(_typedef)<>OS_NO) and
  1094. { no init/final needed }
  1095. not is_managed_type(_typedef) then
  1096. includetempflag(ti_may_be_in_reg);
  1097. end;
  1098. constructor ttempcreatenode.create_withnode(_typedef: tdef; _size: tcgint; _temptype: ttemptype; allowreg:boolean; withnode: tnode);
  1099. begin
  1100. self.create(_typedef,_size,_temptype,allowreg);
  1101. tempinfo^.withnode:=withnode.getcopy;
  1102. end;
  1103. constructor ttempcreatenode.create_value(_typedef:tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean; templvalue: tnode);
  1104. begin
  1105. self.create(_typedef,_size,_temptype,allowreg);
  1106. // store in ppuwrite
  1107. ftemplvalue:=templvalue;
  1108. // create from stored ftemplvalue in ppuload
  1109. tempinfo^.tempinitcode:=cassignmentnode.create(ctemprefnode.create(self),ftemplvalue);
  1110. end;
  1111. constructor ttempcreatenode.create_reference(_typedef: tdef; _size: tcgint; _temptype: ttemptype; allowreg: boolean; templvalue: tnode; readonly: boolean);
  1112. begin
  1113. // store in ppuwrite
  1114. self.create(_typedef,_size,_temptype,allowreg);
  1115. ftemplvalue:=templvalue;
  1116. // no assignment node, just the tempvalue
  1117. tempinfo^.tempinitcode:=ftemplvalue;
  1118. includetempflag(ti_reference);
  1119. if readonly then
  1120. includetempflag(ti_readonly);
  1121. end;
  1122. function ttempcreatenode.dogetcopy: tnode;
  1123. var
  1124. n: ttempcreatenode;
  1125. begin
  1126. n := ttempcreatenode(inherited dogetcopy);
  1127. n.size := size;
  1128. new(n.tempinfo);
  1129. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  1130. n.tempinfo^.owner:=n;
  1131. n.tempinfo^.typedef := tempinfo^.typedef;
  1132. n.tempinfo^.temptype := tempinfo^.temptype;
  1133. n.tempflags := tempflags * tempinfostoreflags;
  1134. { when the tempinfo has already a hookoncopy then it is not
  1135. reset by a tempdeletenode }
  1136. if assigned(tempinfo^.hookoncopy) then
  1137. internalerror(200211262);
  1138. { signal the temprefs that the temp they point to has been copied, }
  1139. { so that if the refs get copied as well, they can hook themselves }
  1140. { to the copy of the temp }
  1141. tempinfo^.hookoncopy := n.tempinfo;
  1142. excludetempflag(ti_nextref_set_hookoncopy_nil);
  1143. if assigned(tempinfo^.withnode) then
  1144. n.tempinfo^.withnode := tempinfo^.withnode.getcopy
  1145. else
  1146. n.tempinfo^.withnode := nil;
  1147. if assigned(tempinfo^.tempinitcode) then
  1148. n.tempinfo^.tempinitcode := tempinfo^.tempinitcode.getcopy
  1149. else
  1150. n.tempinfo^.tempinitcode := nil;
  1151. result := n;
  1152. end;
  1153. constructor ttempcreatenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1154. begin
  1155. inherited ppuload(t,ppufile);
  1156. size:=ppufile.getlongint;
  1157. new(tempinfo);
  1158. fillchar(tempinfo^,sizeof(tempinfo^),0);
  1159. ppufile.getset(tppuset2(tempinfo^.flags));
  1160. ppufile.getderef(tempinfo^.typedefderef);
  1161. tempinfo^.temptype := ttemptype(ppufile.getbyte);
  1162. tempinfo^.owner:=self;
  1163. tempinfo^.withnode:=ppuloadnode(ppufile);
  1164. ftemplvalue:=ppuloadnode(ppufile);
  1165. end;
  1166. procedure ttempcreatenode.ppuwrite(ppufile:tcompilerppufile);
  1167. begin
  1168. inherited ppuwrite(ppufile);
  1169. ppufile.putlongint(size);
  1170. ppufile.putset(tppuset2(tempinfo^.flags));
  1171. ppufile.putderef(tempinfo^.typedefderef);
  1172. ppufile.putbyte(byte(tempinfo^.temptype));
  1173. ppuwritenode(ppufile,tempinfo^.withnode);
  1174. ppuwritenode(ppufile,ftemplvalue);
  1175. end;
  1176. procedure ttempcreatenode.buildderefimpl;
  1177. begin
  1178. inherited buildderefimpl;
  1179. tempinfo^.typedefderef.build(tempinfo^.typedef);
  1180. if assigned(tempinfo^.withnode) then
  1181. tempinfo^.withnode.buildderefimpl;
  1182. if assigned(ftemplvalue) then
  1183. ftemplvalue.buildderefimpl;
  1184. end;
  1185. procedure ttempcreatenode.derefimpl;
  1186. begin
  1187. inherited derefimpl;
  1188. tempinfo^.typedef:=tdef(tempinfo^.typedefderef.resolve);
  1189. if assigned(tempinfo^.withnode) then
  1190. tempinfo^.withnode.derefimpl;
  1191. if assigned(ftemplvalue) then
  1192. begin
  1193. ftemplvalue.derefimpl;
  1194. tempinfo^.tempinitcode:=cassignmentnode.create(ctemprefnode.create(self),ftemplvalue);
  1195. end;
  1196. end;
  1197. function ttempcreatenode.pass_1 : tnode;
  1198. begin
  1199. result := nil;
  1200. expectloc:=LOC_VOID;
  1201. { temps which are immutable do not need to be initialized/finalized }
  1202. if (tempinfo^.typedef.needs_inittable) and not(ti_const in tempflags) then
  1203. include(current_procinfo.flags,pi_needs_implicit_finally);
  1204. if assigned(tempinfo^.withnode) then
  1205. firstpass(tempinfo^.withnode);
  1206. if assigned(tempinfo^.tempinitcode) then
  1207. firstpass(tempinfo^.tempinitcode);
  1208. inc(current_procinfo.estimatedtempsize,size);
  1209. { if a temp. create node is loaded from a ppu, it could be that the unit was compiled with other settings which
  1210. enabled a certain type to be stored in a register while the current settings do not support this, so correct this here
  1211. if needed
  1212. }
  1213. if not(tstoreddef(tempinfo^.typedef).is_fpuregable) and not(tstoreddef(tempinfo^.typedef).is_intregable) and (ti_may_be_in_reg in tempflags) then
  1214. excludetempflag(ti_may_be_in_reg);
  1215. end;
  1216. function ttempcreatenode.pass_typecheck: tnode;
  1217. begin
  1218. result := nil;
  1219. { a tempcreatenode doesn't have a resultdef, only temprefnodes do }
  1220. resultdef := voidtype;
  1221. if assigned(tempinfo^.withnode) then
  1222. typecheckpass(tempinfo^.withnode);
  1223. if assigned(tempinfo^.tempinitcode) then
  1224. typecheckpass(tempinfo^.tempinitcode);
  1225. end;
  1226. function ttempcreatenode.docompare(p: tnode): boolean;
  1227. begin
  1228. result :=
  1229. inherited docompare(p) and
  1230. (ttempcreatenode(p).size = size) and
  1231. (ttempcreatenode(p).tempflags*tempinfostoreflags=tempflags*tempinfostoreflags) and
  1232. equal_defs(ttempcreatenode(p).tempinfo^.typedef,tempinfo^.typedef) and
  1233. (ttempcreatenode(p).tempinfo^.withnode.isequal(tempinfo^.withnode)) and
  1234. (ttempcreatenode(p).tempinfo^.tempinitcode.isequal(tempinfo^.tempinitcode));
  1235. end;
  1236. procedure ttempcreatenode.printnodedata(var t:text);
  1237. var
  1238. f: ttempinfoflag;
  1239. first: Boolean;
  1240. begin
  1241. inherited printnodedata(t);
  1242. writeln(t,printnodeindention,'size = ',size,', temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  1243. tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  1244. write(t,printnodeindention,'[');
  1245. first:=true;
  1246. for f in tempflags do
  1247. begin
  1248. if not(first) then
  1249. write(t,',');
  1250. write(t,f);
  1251. first:=false;
  1252. end;
  1253. writeln(t,']');
  1254. writeln(t,printnodeindention,'tempinit =');
  1255. printnode(t,tempinfo^.tempinitcode);
  1256. end;
  1257. {$ifdef DEBUG_NODE_XML}
  1258. procedure TTempCreateNode.XMLPrintNodeData(var T: Text);
  1259. begin
  1260. inherited XMLPrintNodeData(T);
  1261. WriteLn(T, PrintNodeIndention, '<size>', size, '</size>');
  1262. if Assigned(TempInfo^.TempInitCode) then
  1263. begin
  1264. WriteLn(T, PrintNodeIndention, '<tempinit>');
  1265. PrintNodeIndent;
  1266. XMLPrintNode(T, TempInfo^.TempInitCode);
  1267. PrintNodeUnindent;
  1268. WriteLn(T, PrintNodeIndention, '</tempinit>');
  1269. end
  1270. else
  1271. WriteLn(T, PrintNodeIndention, '<tempinit />');
  1272. end;
  1273. {$endif DEBUG_NODE_XML}
  1274. {*****************************************************************************
  1275. TEMPREFNODE
  1276. *****************************************************************************}
  1277. constructor ttemprefnode.create(const temp: ttempcreatenode);
  1278. begin
  1279. inherited create(temprefn);
  1280. tempinfo := temp.tempinfo;
  1281. end;
  1282. function ttemprefnode.dogetcopy: tnode;
  1283. var
  1284. n: ttemprefnode;
  1285. begin
  1286. n := ttemprefnode(inherited dogetcopy);
  1287. if assigned(tempinfo^.hookoncopy) then
  1288. { if the temp has been copied, assume it becomes a new }
  1289. { temp which has to be hooked by the copied reference }
  1290. begin
  1291. { hook the ref to the copied temp }
  1292. n.tempinfo := tempinfo^.hookoncopy;
  1293. { if we passed a ttempdeletenode that changed the temp }
  1294. { from a persistent one into a normal one, we must be }
  1295. { the last reference (since our parent should free the }
  1296. { temp (JM) }
  1297. if (ti_nextref_set_hookoncopy_nil in tempflags) then
  1298. tempinfo^.hookoncopy := nil;
  1299. end
  1300. else
  1301. { if the temp we refer to hasn't been copied, assume }
  1302. { we're just a new reference to that temp }
  1303. begin
  1304. n.tempinfo := tempinfo;
  1305. end;
  1306. if not assigned(n.tempinfo) then
  1307. internalerror(2005071901);
  1308. result := n;
  1309. end;
  1310. constructor ttemprefnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1311. begin
  1312. inherited ppuload(t,ppufile);
  1313. tempidx:=ppufile.getlongint;
  1314. end;
  1315. procedure ttemprefnode.ppuwrite(ppufile:tcompilerppufile);
  1316. begin
  1317. inherited ppuwrite(ppufile);
  1318. ppufile.putlongint(tempinfo^.owner.ppuidx);
  1319. end;
  1320. procedure ttemprefnode.resolveppuidx;
  1321. var
  1322. temp : ttempcreatenode;
  1323. begin
  1324. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  1325. if temp.nodetype<>tempcreaten then
  1326. internalerror(200311075);
  1327. tempinfo:=temp.tempinfo;
  1328. end;
  1329. function ttemprefnode.pass_1 : tnode;
  1330. begin
  1331. expectloc := LOC_REFERENCE;
  1332. if not tempinfo^.typedef.needs_inittable and
  1333. (ti_may_be_in_reg in tempflags) then
  1334. begin
  1335. if tempinfo^.typedef.typ=floatdef then
  1336. begin
  1337. if not use_vectorfpu(tempinfo^.typedef) then
  1338. if (tempinfo^.temptype = tt_persistent) then
  1339. expectloc := LOC_CFPUREGISTER
  1340. else
  1341. expectloc := LOC_FPUREGISTER
  1342. else
  1343. if (tempinfo^.temptype = tt_persistent) then
  1344. expectloc := LOC_CMMREGISTER
  1345. else
  1346. expectloc := LOC_MMREGISTER
  1347. end
  1348. else
  1349. begin
  1350. if (tempinfo^.temptype = tt_persistent) then
  1351. expectloc := LOC_CREGISTER
  1352. else
  1353. expectloc := LOC_REGISTER;
  1354. end;
  1355. end;
  1356. result := nil;
  1357. end;
  1358. function ttemprefnode.pass_typecheck: tnode;
  1359. begin
  1360. { check if the temp is already resultdef passed }
  1361. if not assigned(tempinfo^.typedef) then
  1362. internalerror(200108233);
  1363. result := nil;
  1364. resultdef := tempinfo^.typedef;
  1365. end;
  1366. function ttemprefnode.docompare(p: tnode): boolean;
  1367. begin
  1368. result :=
  1369. inherited docompare(p) and
  1370. (ttemprefnode(p).tempinfo = tempinfo);
  1371. end;
  1372. procedure ttemprefnode.mark_write;
  1373. begin
  1374. include(flags,nf_write);
  1375. end;
  1376. procedure ttemprefnode.printnodedata(var t:text);
  1377. var
  1378. f : ttempinfoflag;
  1379. notfirst : Boolean;
  1380. begin
  1381. inherited printnodedata(t);
  1382. write(t,printnodeindention,'temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  1383. tempinfo^.typedef.GetTypeName,'", (tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2),' flags = [');
  1384. notfirst:=false;
  1385. for f in tempinfo^.flags do
  1386. begin
  1387. if notfirst then
  1388. write(t,',');
  1389. write(t,f);
  1390. notfirst:=true;
  1391. end;
  1392. writeln(t,'])');
  1393. end;
  1394. {*****************************************************************************
  1395. TEMPDELETENODE
  1396. *****************************************************************************}
  1397. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  1398. begin
  1399. inherited create(tempdeleten);
  1400. tempinfo := temp.tempinfo;
  1401. release_to_normal := false;
  1402. end;
  1403. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  1404. begin
  1405. inherited create(tempdeleten);
  1406. tempinfo := temp.tempinfo;
  1407. release_to_normal := true;
  1408. if tempinfo^.temptype <> tt_persistent then
  1409. internalerror(200204211);
  1410. end;
  1411. function ttempdeletenode.dogetcopy: tnode;
  1412. var
  1413. n: ttempdeletenode;
  1414. begin
  1415. n:=ttempdeletenode(inherited dogetcopy);
  1416. n.release_to_normal:=release_to_normal;
  1417. if assigned(tempinfo^.hookoncopy) then
  1418. { if the temp has been copied, assume it becomes a new }
  1419. { temp which has to be hooked by the copied deletenode }
  1420. begin
  1421. { hook the tempdeletenode to the copied temp }
  1422. n.tempinfo:=tempinfo^.hookoncopy;
  1423. { the temp shall not be used, reset hookoncopy }
  1424. { Only if release_to_normal is false, otherwise }
  1425. { the temp can still be referenced once more (JM) }
  1426. if (not release_to_normal) then
  1427. tempinfo^.hookoncopy:=nil
  1428. else
  1429. includetempflag(ti_nextref_set_hookoncopy_nil);
  1430. end
  1431. else
  1432. { if the temp we refer to hasn't been copied, we have a }
  1433. { problem since that means we now have two delete nodes }
  1434. { for one temp }
  1435. internalerror(200108234);
  1436. result:=n;
  1437. end;
  1438. constructor ttempdeletenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1439. begin
  1440. inherited ppuload(t,ppufile);
  1441. tempidx:=ppufile.getlongint;
  1442. release_to_normal:=(ppufile.getbyte<>0);
  1443. end;
  1444. procedure ttempdeletenode.ppuwrite(ppufile:tcompilerppufile);
  1445. begin
  1446. inherited ppuwrite(ppufile);
  1447. ppufile.putlongint(tempinfo^.owner.ppuidx);
  1448. ppufile.putbyte(byte(release_to_normal));
  1449. end;
  1450. procedure ttempdeletenode.resolveppuidx;
  1451. var
  1452. temp : ttempcreatenode;
  1453. begin
  1454. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  1455. if temp.nodetype<>tempcreaten then
  1456. internalerror(2003110701);
  1457. tempinfo:=temp.tempinfo;
  1458. end;
  1459. function ttempdeletenode.pass_1 : tnode;
  1460. begin
  1461. expectloc:=LOC_VOID;
  1462. result := nil;
  1463. end;
  1464. function ttempdeletenode.pass_typecheck: tnode;
  1465. begin
  1466. result := nil;
  1467. resultdef := voidtype;
  1468. end;
  1469. function ttempdeletenode.docompare(p: tnode): boolean;
  1470. begin
  1471. result :=
  1472. inherited docompare(p) and
  1473. (ttemprefnode(p).tempinfo = tempinfo);
  1474. end;
  1475. destructor ttempdeletenode.destroy;
  1476. begin
  1477. tempinfo^.withnode.free;
  1478. tempinfo^.tempinitcode.free;
  1479. dispose(tempinfo);
  1480. inherited destroy;
  1481. end;
  1482. procedure ttempdeletenode.printnodedata(var t:text);
  1483. begin
  1484. inherited printnodedata(t);
  1485. writeln(t,printnodeindention,'release_to_normal: ',release_to_normal,', temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  1486. tempinfo^.typedef.GetTypeName,'", temptype = ',tempinfo^.temptype,', tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  1487. end;
  1488. {$ifdef DEBUG_NODE_XML}
  1489. procedure TTempDeleteNode.XMLPrintNodeData(var T: Text);
  1490. begin
  1491. inherited XMLPrintNodeData(T);
  1492. WriteLn(T, PrintNodeIndention, '<release_to_normal>', release_to_normal, '</release_to_normal>');
  1493. end;
  1494. {$endif DEBUG_NODE_XML}
  1495. end.