nbas.pas 63 KB

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