nbas.pas 57 KB

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