nbas.pas 62 KB

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