nbas.pas 56 KB

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