nbas.pas 58 KB

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