nbas.pas 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  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 is_exit_statement(var n: tnode; arg: pointer): foreachnoderesult;
  514. begin
  515. if (n.nodetype<>exitn) then
  516. result:=fen_false
  517. else
  518. result:=fen_norecurse_true;
  519. end;
  520. function no_exit_statement_in_block(n: tnode): boolean;
  521. begin
  522. result:=not foreachnodestatic(n,@is_exit_statement,nil);
  523. end;
  524. function tstatementnode.simplify(forinline: boolean) : tnode;
  525. begin
  526. result:=nil;
  527. { these "optimizations" are only to make it more easy to recognise }
  528. { blocknodes which at the end of inlining only contain one single }
  529. { statement. Simplifying inside blocknode.simplify could be dangerous }
  530. { because if the main blocknode which makes up a procedure/function }
  531. { body were replaced with a statementn/nothingn, this could cause }
  532. { problems elsewhere in the compiler which expects a blocknode }
  533. { remove next statement if it's a nothing-statement (since if it's }
  534. { the last, it won't remove itself -- see next simplification) }
  535. while assigned(right) and
  536. (tstatementnode(right).left.nodetype = nothingn) do
  537. begin
  538. result:=tstatementnode(right).right;
  539. tstatementnode(right).right:=nil;
  540. right.free;
  541. right:=result;
  542. result:=nil;
  543. end;
  544. { Remove initial nothingn if there are other statements. If there }
  545. { are no other statements, returning nil doesn't help (will be }
  546. { interpreted as "can't be simplified") and replacing the }
  547. { statementnode with a nothingnode cannot be done (because it's }
  548. { possible this statementnode is a child of a blocknode, and }
  549. { blocknodes are expected to only contain statementnodes) }
  550. if (left.nodetype = nothingn) and
  551. assigned(right) then
  552. begin
  553. result:=right;
  554. right:=nil;
  555. exit;
  556. end;
  557. { if the current statement contains a block with one statement,
  558. replace the current statement with that block's statement
  559. (but only if the block does not have nf_block_with_exit set
  560. or has no exit statement, because otherwise it needs an own
  561. exit label, see tests/test/tinline10)
  562. Further, it might not be the user code entry
  563. }
  564. if (left.nodetype = blockn) and
  565. ((left.flags*[nf_block_with_exit,nf_usercode_entry]=[]) or
  566. ((left.flags*[nf_block_with_exit,nf_usercode_entry]=[nf_block_with_exit]) and no_exit_statement_in_block(left))) and
  567. assigned(tblocknode(left).left) and
  568. not assigned(tstatementnode(tblocknode(left).left).right) then
  569. begin
  570. result:=tblocknode(left).left;
  571. tstatementnode(result).right:=right;
  572. right:=nil;
  573. tblocknode(left).left:=nil;
  574. exit;
  575. end;
  576. end;
  577. function tstatementnode.pass_typecheck:tnode;
  578. begin
  579. result:=nil;
  580. resultdef:=voidtype;
  581. { left is the statement itself calln assignn or a complex one }
  582. typecheckpass(left);
  583. if codegenerror then
  584. exit;
  585. { right is the next statement in the list }
  586. if assigned(right) then
  587. typecheckpass(right);
  588. if codegenerror then
  589. exit;
  590. end;
  591. function tstatementnode.pass_1 : tnode;
  592. begin
  593. result:=nil;
  594. { left is the statement itself calln assignn or a complex one }
  595. firstpass(left);
  596. if codegenerror then
  597. exit;
  598. expectloc:=left.expectloc;
  599. { right is the next in the list }
  600. if assigned(right) then
  601. firstpass(right);
  602. if codegenerror then
  603. exit;
  604. end;
  605. procedure tstatementnode.printnodetree(var t:text);
  606. begin
  607. printnodelist(t);
  608. end;
  609. {*****************************************************************************
  610. TBLOCKNODE
  611. *****************************************************************************}
  612. constructor tblocknode.create(l : tnode);
  613. begin
  614. inherited create(blockn,l);
  615. blocknodeflags:=[];
  616. end;
  617. destructor tblocknode.destroy;
  618. var
  619. hp, next: tstatementnode;
  620. begin
  621. hp := tstatementnode(left);
  622. left := nil;
  623. while assigned(hp) do
  624. begin
  625. next := tstatementnode(hp.right);
  626. hp.right := nil;
  627. hp.free;
  628. hp := next;
  629. end;
  630. inherited destroy;
  631. end;
  632. constructor tblocknode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  633. begin
  634. inherited ppuload(t,ppufile);
  635. ppufile.getset(tppuset1(blocknodeflags));
  636. end;
  637. procedure tblocknode.ppuwrite(ppufile:tcompilerppufile);
  638. begin
  639. inherited ppuwrite(ppufile);
  640. ppufile.putset(tppuset1(blocknodeflags));
  641. end;
  642. function tblocknode.dogetcopy : tnode;
  643. begin
  644. Result:=inherited dogetcopy;
  645. TBlockNode(Result).blocknodeflags:=blocknodeflags;
  646. end;
  647. function NodesEqual(var n: tnode; arg: pointer): foreachnoderesult;
  648. begin
  649. if n.IsEqual(tnode(arg)) then
  650. result:=fen_norecurse_true
  651. else
  652. result:=fen_false;
  653. end;
  654. function tblocknode.simplify(forinline : boolean): tnode;
  655. var
  656. n, p, first, last: tstatementnode;
  657. {$ifdef break_inlining}
  658. a : array[0..3] of tstatementnode;
  659. {$endif break_inlining}
  660. begin
  661. result := nil;
  662. { Warning: never replace a blocknode with another node type,
  663. since the block may be the main block of a procedure/function/
  664. main program body, and those nodes should always be blocknodes
  665. since that's what the compiler expects elsewhere. }
  666. if assigned(left) then
  667. begin
  668. if not assigned(tstatementnode(left).right) then
  669. begin
  670. { Block has a lone statement }
  671. case tstatementnode(left).left.nodetype of
  672. blockn:
  673. begin
  674. { if the current block contains only one statement, and
  675. this one statement only contains another block, replace
  676. this block with that other block. }
  677. result:=tstatementnode(left).left;
  678. tstatementnode(left).left:=nil;
  679. { make sure the nf_block_with_exit flag is safeguarded }
  680. result.flags:=result.flags+(flags*[nf_block_with_exit,nf_usercode_entry]);
  681. exit;
  682. end;
  683. nothingn:
  684. begin
  685. { if the block contains only a statement with a nothing node,
  686. get rid of the statement }
  687. left.Free;
  688. left:=nil;
  689. exit;
  690. end;
  691. else
  692. ;
  693. end;
  694. end
  695. else if forinline or (cs_opt_level2 in current_settings.optimizerswitches) then
  696. begin
  697. n := TStatementNode(left);
  698. while Assigned(n) do
  699. begin
  700. case n.left.nodetype of
  701. raisen, exitn, breakn, continuen, goton:
  702. { Remove all subsequent statements, stopping only at a
  703. label or asm block since code can theoretically jump
  704. to them. Also, don't delete temp creates and deletes }
  705. begin
  706. last := n;
  707. p := TStatementNode(n.Next);
  708. while Assigned(p) do
  709. begin
  710. if (TStatementNode(p).left.nodetype in [labeln, asmn, tempcreaten, tempdeleten, blockn]) then
  711. Break;
  712. last := p;
  713. p := TStatementNode(p.Next);
  714. end;
  715. if (last <> n) then
  716. begin
  717. last.next := nil; { Make sure we stop one before p }
  718. n.next.Free;
  719. n.next := p;
  720. n := p;
  721. Continue;
  722. end;
  723. end;
  724. else
  725. ;
  726. end;
  727. { Look one statement ahead in case it can be deleted - we
  728. need the previous statement to stitch the tree correctly
  729. }
  730. p := TStatementNode(n.Next);
  731. if not Assigned(p) then
  732. Break;
  733. if Assigned(p.left) then
  734. begin
  735. case p.left.nodetype of
  736. blockn:
  737. if (bnf_strippable in TBlockNode(p.left).blocknodeflags) and
  738. ((p.left.flags * [nf_block_with_exit] = []) or no_exit_statement_in_block(p.left)) then
  739. begin
  740. { Attempt to merge this block into the main statement
  741. set }
  742. { Find last statement }
  743. first := TStatementNode(TBlockNode(p.left).PruneKeepLeft());
  744. if Assigned(first) then
  745. begin
  746. last := first;
  747. while Assigned(last.right) do
  748. last := TStatementNode(last.right);
  749. n.right := first;
  750. end
  751. else
  752. last := n;
  753. last.right := p.PruneKeepRight();
  754. { make sure the nf_usercode_entry flag is safeguarded }
  755. Flags := Flags + (p.left.flags * [nf_usercode_entry]);
  756. p.Free;
  757. Continue;
  758. end;
  759. else
  760. ;
  761. end;
  762. end;
  763. n := TStatementNode(n.Next);
  764. end;
  765. end;
  766. end;
  767. {$ifdef break_inlining}
  768. { simple sequence of tempcreate, assign and return temp.? }
  769. if GetStatements(left,a) and
  770. (a[0].left.nodetype=tempcreaten) and
  771. (a[1].left.nodetype=assignn) and
  772. (actualtargetnode(@tassignmentnode(a[1].left).left)^.nodetype=temprefn) and
  773. (a[2].left.nodetype=tempdeleten) and
  774. (a[3].left.nodetype=temprefn) and
  775. (ttempcreatenode(a[0].left).tempinfo=ttemprefnode(actualtargetnode(@tassignmentnode(a[1].left).left)^).tempinfo) and
  776. (ttempcreatenode(a[0].left).tempinfo=ttempdeletenode(a[2].left).tempinfo) and
  777. (ttempcreatenode(a[0].left).tempinfo=ttemprefnode(a[3].left).tempinfo) and
  778. { the temp. node might not be references inside the assigned expression }
  779. not(foreachnodestatic(tassignmentnode(a[1].left).right,@NodesEqual,actualtargetnode(@tassignmentnode(a[1].left).left)^)) then
  780. begin
  781. result:=tassignmentnode(a[1].left).right;
  782. tassignmentnode(a[1].left).right:=nil;
  783. { ensure the node is first passed, so the resultdef does not get changed if the
  784. the type conv. below is merged }
  785. firstpass(result);
  786. result:=ctypeconvnode.create_internal(result,ttemprefnode(a[3].left).resultdef);
  787. firstpass(result);
  788. exit;
  789. end;
  790. {$endif break_inlining}
  791. end;
  792. function tblocknode.pass_typecheck:tnode;
  793. var
  794. hp : tstatementnode;
  795. begin
  796. result:=nil;
  797. resultdef:=voidtype;
  798. hp:=tstatementnode(left);
  799. while assigned(hp) do
  800. begin
  801. if assigned(hp.left) then
  802. begin
  803. codegenerror:=false;
  804. typecheckpass(hp.left);
  805. { the resultdef of the block is the last type that is
  806. returned. Normally this is a voidtype. But when the
  807. compiler inserts a block of multiple statements then the
  808. last entry can return a value }
  809. resultdef:=hp.left.resultdef;
  810. end;
  811. hp:=tstatementnode(hp.right);
  812. end;
  813. end;
  814. function tblocknode.pass_1 : tnode;
  815. var
  816. hp : tstatementnode;
  817. FirstNode: Boolean;
  818. //count : longint;
  819. begin
  820. result:=nil;
  821. expectloc:=LOC_VOID;
  822. //count:=0;
  823. hp:=tstatementnode(left);
  824. FirstNode := True;
  825. while assigned(hp) do
  826. begin
  827. if assigned(hp.left) then
  828. begin
  829. { Calling firstpass on a statement node normally causes
  830. problems, so we have to be a little bit hacky if we want
  831. to remove nothing nodes }
  832. codegenerror:=false;
  833. firstpass(tnode(hp));
  834. { If the first node gets deleted, left must be updated,
  835. otherwise it will be a dangling pointer }
  836. if FirstNode then
  837. begin
  838. { If multiple statements are to be removed, they would
  839. have all been removed by the firstpass call above, so
  840. the next one will not be removed, hence it's safe to
  841. set FirstNode to false }
  842. FirstNode := False;
  843. left := hp;
  844. end;
  845. if assigned(hp.left) then
  846. hp.expectloc:=hp.left.expectloc;
  847. end;
  848. expectloc:=hp.expectloc;
  849. //inc(count);
  850. hp:=tstatementnode(hp.right);
  851. end;
  852. end;
  853. {$ifdef state_tracking}
  854. function Tblocknode.track_state_pass(exec_known:boolean):boolean;
  855. var hp:Tstatementnode;
  856. begin
  857. track_state_pass:=false;
  858. hp:=Tstatementnode(left);
  859. while assigned(hp) do
  860. begin
  861. if hp.left.track_state_pass(exec_known) then
  862. track_state_pass:=true;
  863. hp:=Tstatementnode(hp.right);
  864. end;
  865. end;
  866. {$endif state_tracking}
  867. {$ifdef DEBUG_NODE_XML}
  868. procedure TBlockNode.XMLPrintNodeInfo(var T: Text);
  869. var
  870. i_bnf: TBlockNodeFlag;
  871. First: Boolean;
  872. begin
  873. inherited XMLPrintNodeInfo(T);
  874. First := True;
  875. for i_bnf in blocknodeflags do
  876. begin
  877. if First then
  878. begin
  879. Write(T, ' blocknodeflags="', i_bnf);
  880. First := False;
  881. end
  882. else
  883. Write(T, ',', i_bnf)
  884. end;
  885. if not First then
  886. write(T,'"');
  887. end;
  888. {$endif DEBUG_NODE_XML}
  889. {*****************************************************************************
  890. TASMNODE
  891. *****************************************************************************}
  892. constructor tasmnode.create(p : TAsmList);
  893. begin
  894. inherited create(asmn);
  895. p_asm:=p;
  896. asmnodeflags:=[];
  897. currenttai:=nil;
  898. end;
  899. constructor tasmnode.create_get_position;
  900. begin
  901. inherited create(asmn);
  902. p_asm:=nil;
  903. asmnodeflags:=[asmnf_get_asm_position];
  904. currenttai:=nil;
  905. end;
  906. destructor tasmnode.destroy;
  907. begin
  908. if assigned(p_asm) then
  909. p_asm.free;
  910. inherited destroy;
  911. end;
  912. constructor tasmnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  913. var
  914. hp : tai;
  915. begin
  916. inherited ppuload(t,ppufile);
  917. ppufile.getset(tppuset1(asmnodeflags));
  918. if not(asmnf_get_asm_position in asmnodeflags) then
  919. begin
  920. p_asm:=TAsmList.create;
  921. repeat
  922. hp:=ppuloadai(ppufile);
  923. if hp=nil then
  924. break;
  925. p_asm.concat(hp);
  926. if hp.typ=ait_section then
  927. inc(p_asm.section_count);
  928. until false;
  929. end
  930. else
  931. p_asm:=nil;
  932. currenttai:=nil;
  933. end;
  934. procedure tasmnode.ppuwrite(ppufile:tcompilerppufile);
  935. var
  936. hp : tai;
  937. begin
  938. inherited ppuwrite(ppufile);
  939. ppufile.putset(tppuset1(asmnodeflags));
  940. { TODO: FIXME Add saving of register sets}
  941. if not(asmnf_get_asm_position in asmnodeflags) then
  942. begin
  943. hp:=tai(p_asm.first);
  944. while assigned(hp) do
  945. begin
  946. ppuwriteai(ppufile,hp);
  947. hp:=tai(hp.next);
  948. end;
  949. { end is marked by a nil }
  950. ppuwriteai(ppufile,nil);
  951. end;
  952. end;
  953. procedure tasmnode.buildderefimpl;
  954. var
  955. hp : tai;
  956. begin
  957. inherited buildderefimpl;
  958. if not(asmnf_get_asm_position in asmnodeflags) then
  959. begin
  960. hp:=tai(p_asm.first);
  961. while assigned(hp) do
  962. begin
  963. hp.buildderefimpl;
  964. hp:=tai(hp.next);
  965. end;
  966. end;
  967. end;
  968. procedure tasmnode.derefimpl;
  969. var
  970. hp : tai;
  971. begin
  972. inherited derefimpl;
  973. if not(asmnf_get_asm_position in asmnodeflags) then
  974. begin
  975. hp:=tai(p_asm.first);
  976. while assigned(hp) do
  977. begin
  978. hp.derefimpl;
  979. hp:=tai(hp.next);
  980. end;
  981. end;
  982. end;
  983. function tasmnode.dogetcopy: tnode;
  984. var
  985. n: tasmnode;
  986. begin
  987. n:=tasmnode(inherited dogetcopy);
  988. n.asmnodeflags:=asmnodeflags;
  989. if assigned(p_asm) then
  990. begin
  991. n.p_asm:=TAsmList.create;
  992. n.p_asm.concatlistcopy(p_asm);
  993. end
  994. else
  995. n.p_asm:=nil;
  996. n.currenttai:=currenttai;
  997. result:=n;
  998. end;
  999. function tasmnode.pass_typecheck:tnode;
  1000. begin
  1001. result:=nil;
  1002. resultdef:=voidtype;
  1003. if not(asmnf_get_asm_position in asmnodeflags) then
  1004. include(current_procinfo.flags,pi_has_assembler_block);
  1005. end;
  1006. function tasmnode.pass_1 : tnode;
  1007. begin
  1008. result:=nil;
  1009. expectloc:=LOC_VOID;
  1010. end;
  1011. function tasmnode.docompare(p: tnode): boolean;
  1012. begin
  1013. { comparing of asmlists is not implemented (JM) }
  1014. docompare := false;
  1015. end;
  1016. {$ifdef DEBUG_NODE_XML}
  1017. procedure TAsmNode.XMLPrintNodeInfo(var T: Text);
  1018. var
  1019. i: TAsmNodeFlag;
  1020. First: Boolean;
  1021. begin
  1022. inherited XMLPrintNodeInfo(T);
  1023. First := True;
  1024. for i in asmnodeflags do
  1025. begin
  1026. if First then
  1027. begin
  1028. Write(T, ' asmnodeflags="', i);
  1029. First := False;
  1030. end
  1031. else
  1032. Write(T, ',', i)
  1033. end;
  1034. if not First then
  1035. Write(T, '"');
  1036. end;
  1037. procedure TAsmNode.XMLProcessInstruction(var T: Text; p: tai);
  1038. var
  1039. ThisOp, ThisOper: string;
  1040. X: Integer;
  1041. begin
  1042. case p.typ of
  1043. { Instructions are handled on a per-platform basis }
  1044. ait_label:
  1045. WriteLn(T, PrintNodeIndention, tai_label(p).labsym.name, ':');
  1046. ait_const:
  1047. begin
  1048. case tai_const(p).consttype of
  1049. aitconst_64bit:
  1050. WriteLn(T, PrintNodeIndention, '.quad 0x', hexstr(tai_const(p).value, 16));
  1051. aitconst_32bit:
  1052. WriteLn(T, PrintNodeIndention, '.long 0x', hexstr(tai_const(p).value, 8));
  1053. aitconst_16bit:
  1054. WriteLn(T, PrintNodeIndention, '.word 0x', hexstr(tai_const(p).value, 4));
  1055. aitconst_8bit:
  1056. WriteLn(T, PrintNodeIndention, '.byte 0x', hexstr(tai_const(p).value, 2));
  1057. else
  1058. WriteLn(T, PrintNodeIndention, '; (Other constant)');
  1059. end;
  1060. end;
  1061. ait_realconst:
  1062. WriteLn(T, PrintNodeIndention, '; (Real constant)');
  1063. else
  1064. { Do nothing };
  1065. end;
  1066. end;
  1067. class procedure TAsmNode.XMLPadString(var S: string; Len: Integer);
  1068. var
  1069. X, C: Integer;
  1070. begin
  1071. C := Length(S);
  1072. if C < Len then
  1073. begin
  1074. SetLength(S, Len);
  1075. for X := C + 1 to Len do
  1076. S[X] := ' '
  1077. end;
  1078. end;
  1079. function TAsmNode.XMLFormatOp(const Oper: POper): string;
  1080. begin
  1081. case Oper^.typ of
  1082. top_reg:
  1083. Result := gas_regname(Oper^.reg);
  1084. top_local:
  1085. { Local variable }
  1086. Result := TSym(Oper^.localoper^.localsym).prettyname;
  1087. top_bool:
  1088. begin
  1089. if Oper^.b then
  1090. Result := 'TRUE'
  1091. else
  1092. Result := 'FALSE';
  1093. end;
  1094. else
  1095. Result := '(unk)';
  1096. end;
  1097. end;
  1098. procedure TAsmNode.XMLPrintNodeData(var T: Text);
  1099. begin
  1100. if not Assigned(p_asm) then
  1101. Exit;
  1102. WriteLn(T, PrintNodeIndention, '(Assembler output not currently supported on this platform)');
  1103. end;
  1104. {$endif DEBUG_NODE_XML}
  1105. {*****************************************************************************
  1106. TEMPBASENODE
  1107. *****************************************************************************}
  1108. class procedure ttempinfoaccessor.settempinfoflags(tempinfo: ptempinfo; const flags: ttempinfoflags);
  1109. begin
  1110. tempinfo^.flags:=flags;
  1111. end;
  1112. class function ttempinfoaccessor.gettempinfoflags(tempinfo: ptempinfo): ttempinfoflags;
  1113. begin
  1114. result:=tempinfo^.flags;
  1115. end;
  1116. {*****************************************************************************
  1117. TEMPBASENODE
  1118. *****************************************************************************}
  1119. procedure ttempbasenode.settempinfoflags(const tempflags: ttempinfoflags);
  1120. begin
  1121. ctempinfoaccessor.settempinfoflags(tempinfo,tempflags);
  1122. end;
  1123. function ttempbasenode.gettempinfoflags: ttempinfoflags;
  1124. begin
  1125. result:=ctempinfoaccessor.gettempinfoflags(tempinfo);
  1126. end;
  1127. procedure ttempbasenode.includetempflag(flag: ttempinfoflag);
  1128. begin
  1129. { go through settempinfoflags() so it can filter out unsupported tempflags }
  1130. settempinfoflags(gettempinfoflags+[flag])
  1131. end;
  1132. procedure ttempbasenode.excludetempflag(flag: ttempinfoflag);
  1133. begin
  1134. { go through settempinfoflags() so it can prevent required tempflags from
  1135. being removed (if any) }
  1136. settempinfoflags(gettempinfoflags-[flag])
  1137. end;
  1138. {$ifdef DEBUG_NODE_XML}
  1139. procedure TTempBaseNode.XMLPrintNodeInfo(var T: Text);
  1140. begin
  1141. inherited XMLPrintNodeInfo(T);
  1142. { The raw pointer is the only way to uniquely identify the temp }
  1143. Write(T, ' id="', WritePointer(tempinfo), '"');
  1144. end;
  1145. procedure TTempBaseNode.XMLPrintNodeData(var T: Text);
  1146. var
  1147. Flag: TTempInfoFlag;
  1148. NotFirst: Boolean;
  1149. begin
  1150. inherited XMLPrintNodeData(t);
  1151. if not assigned(tempinfo) then
  1152. exit;
  1153. WriteLn(T, PrintNodeIndention, '<typedef>', SanitiseXMLString(tempinfo^.typedef.typesymbolprettyname), '</typedef>');
  1154. NotFirst := False;
  1155. for Flag := Low(TTempInfoFlag) to High(TTempInfoFlag) do
  1156. if (Flag in tempinfo^.flags) then
  1157. if not NotFirst then
  1158. begin
  1159. Write(T, PrintNodeIndention, '<tempflags>', Flag);
  1160. NotFirst := True;
  1161. end
  1162. else
  1163. Write(T, ',', Flag);
  1164. if NotFirst then
  1165. WriteLn(T, '</tempflags>')
  1166. else
  1167. WriteLn(T, PrintNodeIndention, '<tempflags />');
  1168. WriteLn(T, PrintNodeIndention, '<temptype>', tempinfo^.temptype, '</temptype>');
  1169. end;
  1170. {$endif DEBUG_NODE_XML}
  1171. {*****************************************************************************
  1172. TEMPCREATENODE
  1173. *****************************************************************************}
  1174. constructor ttempcreatenode.create(_typedef:tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean);
  1175. begin
  1176. inherited create(tempcreaten);
  1177. size := _size;
  1178. new(tempinfo);
  1179. fillchar(tempinfo^,sizeof(tempinfo^),0);
  1180. tempinfo^.typedef := _typedef;
  1181. tempinfo^.temptype := _temptype;
  1182. tempinfo^.owner := self;
  1183. tempinfo^.withnode := nil;
  1184. if allowreg and
  1185. { temp must fit a single register }
  1186. (tstoreddef(_typedef).is_fpuregable or
  1187. (tstoreddef(_typedef).is_intregable and
  1188. (_size<=TCGSize2Size[OS_64]))) and
  1189. { size of register operations must be known }
  1190. (def_cgsize(_typedef)<>OS_NO) and
  1191. { no init/final needed }
  1192. not is_managed_type(_typedef) then
  1193. includetempflag(ti_may_be_in_reg);
  1194. end;
  1195. constructor ttempcreatenode.create_withnode(_typedef: tdef; _size: tcgint; _temptype: ttemptype; allowreg:boolean; withnode: tnode);
  1196. begin
  1197. self.create(_typedef,_size,_temptype,allowreg);
  1198. tempinfo^.withnode:=withnode.getcopy;
  1199. end;
  1200. constructor ttempcreatenode.create_value(_typedef:tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean; templvalue: tnode);
  1201. begin
  1202. self.create(_typedef,_size,_temptype,allowreg);
  1203. // store in ppuwrite
  1204. ftemplvalue:=templvalue;
  1205. // create from stored ftemplvalue in ppuload
  1206. tempinfo^.tempinitcode:=cassignmentnode.create(ctemprefnode.create(self),ftemplvalue);
  1207. end;
  1208. constructor ttempcreatenode.create_reference(_typedef: tdef; _size: tcgint; _temptype: ttemptype; allowreg: boolean; templvalue: tnode; readonly: boolean);
  1209. begin
  1210. // store in ppuwrite
  1211. self.create(_typedef,_size,_temptype,allowreg);
  1212. ftemplvalue:=templvalue;
  1213. // no assignment node, just the tempvalue
  1214. tempinfo^.tempinitcode:=ftemplvalue;
  1215. includetempflag(ti_reference);
  1216. if readonly then
  1217. includetempflag(ti_readonly);
  1218. end;
  1219. function ttempcreatenode.dogetcopy: tnode;
  1220. var
  1221. n: ttempcreatenode;
  1222. begin
  1223. n := ttempcreatenode(inherited dogetcopy);
  1224. n.size := size;
  1225. new(n.tempinfo);
  1226. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  1227. n.tempinfo^.owner:=n;
  1228. n.tempinfo^.typedef := tempinfo^.typedef;
  1229. n.tempinfo^.temptype := tempinfo^.temptype;
  1230. n.tempflags := tempflags * tempinfostoreflags;
  1231. { when the tempinfo has already a hookoncopy then it is not
  1232. reset by a tempdeletenode }
  1233. if assigned(tempinfo^.hookoncopy) then
  1234. internalerror(200211262);
  1235. { signal the temprefs that the temp they point to has been copied, }
  1236. { so that if the refs get copied as well, they can hook themselves }
  1237. { to the copy of the temp }
  1238. tempinfo^.hookoncopy := n.tempinfo;
  1239. excludetempflag(ti_nextref_set_hookoncopy_nil);
  1240. if assigned(tempinfo^.withnode) then
  1241. n.tempinfo^.withnode := tempinfo^.withnode.getcopy
  1242. else
  1243. n.tempinfo^.withnode := nil;
  1244. if assigned(tempinfo^.tempinitcode) then
  1245. n.tempinfo^.tempinitcode := tempinfo^.tempinitcode.getcopy
  1246. else
  1247. n.tempinfo^.tempinitcode := nil;
  1248. result := n;
  1249. end;
  1250. constructor ttempcreatenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1251. begin
  1252. inherited ppuload(t,ppufile);
  1253. size:=ppufile.getlongint;
  1254. new(tempinfo);
  1255. fillchar(tempinfo^,sizeof(tempinfo^),0);
  1256. ppufile.getset(tppuset2(tempinfo^.flags));
  1257. ppufile.getderef(tempinfo^.typedefderef);
  1258. tempinfo^.temptype := ttemptype(ppufile.getbyte);
  1259. tempinfo^.owner:=self;
  1260. tempinfo^.withnode:=ppuloadnode(ppufile);
  1261. ftemplvalue:=ppuloadnode(ppufile);
  1262. end;
  1263. procedure ttempcreatenode.ppuwrite(ppufile:tcompilerppufile);
  1264. begin
  1265. inherited ppuwrite(ppufile);
  1266. ppufile.putlongint(size);
  1267. ppufile.putset(tppuset2(tempinfo^.flags));
  1268. ppufile.putderef(tempinfo^.typedefderef);
  1269. ppufile.putbyte(byte(tempinfo^.temptype));
  1270. ppuwritenode(ppufile,tempinfo^.withnode);
  1271. ppuwritenode(ppufile,ftemplvalue);
  1272. end;
  1273. procedure ttempcreatenode.buildderefimpl;
  1274. begin
  1275. inherited buildderefimpl;
  1276. tempinfo^.typedefderef.build(tempinfo^.typedef);
  1277. if assigned(tempinfo^.withnode) then
  1278. tempinfo^.withnode.buildderefimpl;
  1279. if assigned(ftemplvalue) then
  1280. ftemplvalue.buildderefimpl;
  1281. end;
  1282. procedure ttempcreatenode.derefimpl;
  1283. begin
  1284. inherited derefimpl;
  1285. tempinfo^.typedef:=tdef(tempinfo^.typedefderef.resolve);
  1286. if assigned(tempinfo^.withnode) then
  1287. tempinfo^.withnode.derefimpl;
  1288. if assigned(ftemplvalue) then
  1289. begin
  1290. ftemplvalue.derefimpl;
  1291. tempinfo^.tempinitcode:=cassignmentnode.create(ctemprefnode.create(self),ftemplvalue);
  1292. end;
  1293. end;
  1294. function ttempcreatenode.pass_1 : tnode;
  1295. begin
  1296. result := nil;
  1297. expectloc:=LOC_VOID;
  1298. { temps which are immutable do not need to be initialized/finalized }
  1299. if (tempinfo^.typedef.needs_inittable) and not(ti_const in tempflags) then
  1300. include(current_procinfo.flags,pi_needs_implicit_finally);
  1301. if assigned(tempinfo^.withnode) then
  1302. firstpass(tempinfo^.withnode);
  1303. if assigned(tempinfo^.tempinitcode) then
  1304. firstpass(tempinfo^.tempinitcode);
  1305. inc(current_procinfo.estimatedtempsize,size);
  1306. { if a temp. create node is loaded from a ppu, it could be that the unit was compiled with other settings which
  1307. enabled a certain type to be stored in a register while the current settings do not support this, so correct this here
  1308. if needed
  1309. }
  1310. if not(tstoreddef(tempinfo^.typedef).is_fpuregable) and not(tstoreddef(tempinfo^.typedef).is_intregable) and (ti_may_be_in_reg in tempflags) then
  1311. excludetempflag(ti_may_be_in_reg);
  1312. end;
  1313. function ttempcreatenode.pass_typecheck: tnode;
  1314. begin
  1315. result := nil;
  1316. { a tempcreatenode doesn't have a resultdef, only temprefnodes do }
  1317. resultdef := voidtype;
  1318. if assigned(tempinfo^.withnode) then
  1319. typecheckpass(tempinfo^.withnode);
  1320. if assigned(tempinfo^.tempinitcode) then
  1321. typecheckpass(tempinfo^.tempinitcode);
  1322. end;
  1323. function ttempcreatenode.docompare(p: tnode): boolean;
  1324. begin
  1325. result :=
  1326. inherited docompare(p) and
  1327. (ttempcreatenode(p).size = size) and
  1328. (ttempcreatenode(p).tempflags*tempinfostoreflags=tempflags*tempinfostoreflags) and
  1329. equal_defs(ttempcreatenode(p).tempinfo^.typedef,tempinfo^.typedef) and
  1330. (ttempcreatenode(p).tempinfo^.withnode.isequal(tempinfo^.withnode)) and
  1331. (ttempcreatenode(p).tempinfo^.tempinitcode.isequal(tempinfo^.tempinitcode));
  1332. end;
  1333. procedure ttempcreatenode.printnodedata(var t:text);
  1334. var
  1335. f: ttempinfoflag;
  1336. first: Boolean;
  1337. begin
  1338. inherited printnodedata(t);
  1339. writeln(t,printnodeindention,'size = ',size,', temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  1340. tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  1341. write(t,printnodeindention,'[');
  1342. first:=true;
  1343. for f in tempflags do
  1344. begin
  1345. if not(first) then
  1346. write(t,',');
  1347. write(t,f);
  1348. first:=false;
  1349. end;
  1350. writeln(t,']');
  1351. writeln(t,printnodeindention,'tempinit =');
  1352. printnode(t,tempinfo^.tempinitcode);
  1353. end;
  1354. {$ifdef DEBUG_NODE_XML}
  1355. procedure TTempCreateNode.XMLPrintNodeData(var T: Text);
  1356. begin
  1357. inherited XMLPrintNodeData(T);
  1358. WriteLn(T, PrintNodeIndention, '<size>', size, '</size>');
  1359. if Assigned(TempInfo^.TempInitCode) then
  1360. begin
  1361. WriteLn(T, PrintNodeIndention, '<tempinit>');
  1362. PrintNodeIndent;
  1363. XMLPrintNode(T, TempInfo^.TempInitCode);
  1364. PrintNodeUnindent;
  1365. WriteLn(T, PrintNodeIndention, '</tempinit>');
  1366. end
  1367. else
  1368. WriteLn(T, PrintNodeIndention, '<tempinit />');
  1369. end;
  1370. {$endif DEBUG_NODE_XML}
  1371. {*****************************************************************************
  1372. TEMPREFNODE
  1373. *****************************************************************************}
  1374. constructor ttemprefnode.create(const temp: ttempcreatenode);
  1375. begin
  1376. inherited create(temprefn);
  1377. tempinfo := temp.tempinfo;
  1378. end;
  1379. function ttemprefnode.dogetcopy: tnode;
  1380. var
  1381. n: ttemprefnode;
  1382. begin
  1383. n := ttemprefnode(inherited dogetcopy);
  1384. if assigned(tempinfo^.hookoncopy) then
  1385. { if the temp has been copied, assume it becomes a new }
  1386. { temp which has to be hooked by the copied reference }
  1387. begin
  1388. { hook the ref to the copied temp }
  1389. n.tempinfo := tempinfo^.hookoncopy;
  1390. { if we passed a ttempdeletenode that changed the temp }
  1391. { from a persistent one into a normal one, we must be }
  1392. { the last reference (since our parent should free the }
  1393. { temp (JM) }
  1394. if (ti_nextref_set_hookoncopy_nil in tempflags) then
  1395. tempinfo^.hookoncopy := nil;
  1396. end
  1397. else
  1398. { if the temp we refer to hasn't been copied, assume }
  1399. { we're just a new reference to that temp }
  1400. begin
  1401. n.tempinfo := tempinfo;
  1402. end;
  1403. if not assigned(n.tempinfo) then
  1404. internalerror(2005071901);
  1405. result := n;
  1406. end;
  1407. constructor ttemprefnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1408. begin
  1409. inherited ppuload(t,ppufile);
  1410. tempidx:=ppufile.getlongint;
  1411. end;
  1412. procedure ttemprefnode.ppuwrite(ppufile:tcompilerppufile);
  1413. begin
  1414. inherited ppuwrite(ppufile);
  1415. ppufile.putlongint(tempinfo^.owner.ppuidx);
  1416. end;
  1417. procedure ttemprefnode.resolveppuidx;
  1418. var
  1419. temp : ttempcreatenode;
  1420. begin
  1421. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  1422. if temp.nodetype<>tempcreaten then
  1423. internalerror(200311075);
  1424. tempinfo:=temp.tempinfo;
  1425. end;
  1426. function ttemprefnode.pass_1 : tnode;
  1427. begin
  1428. expectloc := LOC_REFERENCE;
  1429. if not tempinfo^.typedef.needs_inittable and
  1430. (ti_may_be_in_reg in tempflags) then
  1431. begin
  1432. if tempinfo^.typedef.typ=floatdef then
  1433. begin
  1434. if not use_vectorfpu(tempinfo^.typedef) then
  1435. if (tempinfo^.temptype = tt_persistent) then
  1436. expectloc := LOC_CFPUREGISTER
  1437. else
  1438. expectloc := LOC_FPUREGISTER
  1439. else
  1440. if (tempinfo^.temptype = tt_persistent) then
  1441. expectloc := LOC_CMMREGISTER
  1442. else
  1443. expectloc := LOC_MMREGISTER
  1444. end
  1445. else
  1446. begin
  1447. if (tempinfo^.temptype = tt_persistent) then
  1448. expectloc := LOC_CREGISTER
  1449. else
  1450. expectloc := LOC_REGISTER;
  1451. end;
  1452. end;
  1453. result := nil;
  1454. end;
  1455. function ttemprefnode.pass_typecheck: tnode;
  1456. begin
  1457. { check if the temp is already resultdef passed }
  1458. if not assigned(tempinfo^.typedef) then
  1459. internalerror(200108233);
  1460. result := nil;
  1461. resultdef := tempinfo^.typedef;
  1462. end;
  1463. function ttemprefnode.docompare(p: tnode): boolean;
  1464. begin
  1465. result :=
  1466. inherited docompare(p) and
  1467. (ttemprefnode(p).tempinfo = tempinfo);
  1468. end;
  1469. procedure ttemprefnode.mark_write;
  1470. begin
  1471. include(flags,nf_write);
  1472. end;
  1473. procedure ttemprefnode.printnodedata(var t:text);
  1474. var
  1475. f : ttempinfoflag;
  1476. notfirst : Boolean;
  1477. begin
  1478. inherited printnodedata(t);
  1479. write(t,printnodeindention,'temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  1480. tempinfo^.typedef.GetTypeName,'", (tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2),' flags = [');
  1481. notfirst:=false;
  1482. for f in tempinfo^.flags do
  1483. begin
  1484. if notfirst then
  1485. write(t,',');
  1486. write(t,f);
  1487. notfirst:=true;
  1488. end;
  1489. writeln(t,'])');
  1490. end;
  1491. {*****************************************************************************
  1492. TEMPDELETENODE
  1493. *****************************************************************************}
  1494. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  1495. begin
  1496. inherited create(tempdeleten);
  1497. tempinfo := temp.tempinfo;
  1498. release_to_normal := false;
  1499. end;
  1500. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  1501. begin
  1502. inherited create(tempdeleten);
  1503. tempinfo := temp.tempinfo;
  1504. release_to_normal := true;
  1505. if tempinfo^.temptype <> tt_persistent then
  1506. internalerror(200204211);
  1507. end;
  1508. function ttempdeletenode.dogetcopy: tnode;
  1509. var
  1510. n: ttempdeletenode;
  1511. begin
  1512. n:=ttempdeletenode(inherited dogetcopy);
  1513. n.release_to_normal:=release_to_normal;
  1514. if assigned(tempinfo^.hookoncopy) then
  1515. { if the temp has been copied, assume it becomes a new }
  1516. { temp which has to be hooked by the copied deletenode }
  1517. begin
  1518. { hook the tempdeletenode to the copied temp }
  1519. n.tempinfo:=tempinfo^.hookoncopy;
  1520. { the temp shall not be used, reset hookoncopy }
  1521. { Only if release_to_normal is false, otherwise }
  1522. { the temp can still be referenced once more (JM) }
  1523. if (not release_to_normal) then
  1524. tempinfo^.hookoncopy:=nil
  1525. else
  1526. includetempflag(ti_nextref_set_hookoncopy_nil);
  1527. end
  1528. else
  1529. { if the temp we refer to hasn't been copied, we have a }
  1530. { problem since that means we now have two delete nodes }
  1531. { for one temp }
  1532. internalerror(200108234);
  1533. result:=n;
  1534. end;
  1535. constructor ttempdeletenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1536. begin
  1537. inherited ppuload(t,ppufile);
  1538. tempidx:=ppufile.getlongint;
  1539. release_to_normal:=(ppufile.getbyte<>0);
  1540. end;
  1541. procedure ttempdeletenode.ppuwrite(ppufile:tcompilerppufile);
  1542. begin
  1543. inherited ppuwrite(ppufile);
  1544. ppufile.putlongint(tempinfo^.owner.ppuidx);
  1545. ppufile.putbyte(byte(release_to_normal));
  1546. end;
  1547. procedure ttempdeletenode.resolveppuidx;
  1548. var
  1549. temp : ttempcreatenode;
  1550. begin
  1551. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  1552. if temp.nodetype<>tempcreaten then
  1553. internalerror(2003110701);
  1554. tempinfo:=temp.tempinfo;
  1555. end;
  1556. function ttempdeletenode.pass_1 : tnode;
  1557. begin
  1558. expectloc:=LOC_VOID;
  1559. result := nil;
  1560. end;
  1561. function ttempdeletenode.pass_typecheck: tnode;
  1562. begin
  1563. result := nil;
  1564. resultdef := voidtype;
  1565. end;
  1566. function ttempdeletenode.docompare(p: tnode): boolean;
  1567. begin
  1568. result :=
  1569. inherited docompare(p) and
  1570. (ttemprefnode(p).tempinfo = tempinfo);
  1571. end;
  1572. destructor ttempdeletenode.destroy;
  1573. begin
  1574. tempinfo^.withnode.free;
  1575. tempinfo^.tempinitcode.free;
  1576. dispose(tempinfo);
  1577. inherited destroy;
  1578. end;
  1579. procedure ttempdeletenode.printnodedata(var t:text);
  1580. begin
  1581. inherited printnodedata(t);
  1582. writeln(t,printnodeindention,'release_to_normal: ',release_to_normal,', temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  1583. tempinfo^.typedef.GetTypeName,'", temptype = ',tempinfo^.temptype,', tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  1584. end;
  1585. {$ifdef DEBUG_NODE_XML}
  1586. procedure TTempDeleteNode.XMLPrintNodeData(var T: Text);
  1587. begin
  1588. inherited XMLPrintNodeData(T);
  1589. WriteLn(T, PrintNodeIndention, '<release_to_normal>', release_to_normal, '</release_to_normal>');
  1590. end;
  1591. {$endif DEBUG_NODE_XML}
  1592. end.