nbas.pas 64 KB

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