nbas.pas 63 KB

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