nbas.pas 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  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) do
  689. begin
  690. case n.left.nodetype of
  691. raisen, exitn, breakn, continuen, goton:
  692. { Remove all subsequent statements, stopping only at a
  693. label or asm block since code can theoretically jump
  694. to them. Also, don't delete temp creates and deletes }
  695. begin
  696. last := n;
  697. p := TStatementNode(n.Next);
  698. while Assigned(p) do
  699. begin
  700. if (TStatementNode(p).left.nodetype in [labeln, asmn, tempcreaten, tempdeleten, blockn]) then
  701. Break;
  702. last := p;
  703. p := TStatementNode(p.Next);
  704. end;
  705. if (last <> n) then
  706. begin
  707. last.next := nil; { Make sure we stop one before p }
  708. n.next.Free;
  709. n.next := p;
  710. n := p;
  711. Continue;
  712. end;
  713. end;
  714. else
  715. ;
  716. end;
  717. { Look one statement ahead in case it can be deleted - we
  718. need the previous statement to stitch the tree correctly
  719. }
  720. p := TStatementNode(n.Next);
  721. if not Assigned(p) then
  722. Break;
  723. if Assigned(p.left) then
  724. begin
  725. case p.left.nodetype of
  726. blockn:
  727. if (bnf_strippable in TBlockNode(p.left).blocknodeflags) and
  728. ((p.left.flags * [nf_block_with_exit] = []) or no_exit_statement_in_block(p.left)) then
  729. begin
  730. { Attempt to merge this block into the main statement
  731. set }
  732. { Find last statement }
  733. first := TStatementNode(TBlockNode(p.left).PruneKeepLeft());
  734. if Assigned(first) then
  735. begin
  736. last := first;
  737. while Assigned(last.right) do
  738. last := TStatementNode(last.right);
  739. n.right := first;
  740. end
  741. else
  742. last := n;
  743. last.right := p.PruneKeepRight();
  744. { make sure the nf_usercode_entry flag is safeguarded }
  745. Flags := Flags + (p.left.flags * [nf_usercode_entry]);
  746. p.Free;
  747. Continue;
  748. end;
  749. else
  750. ;
  751. end;
  752. end;
  753. n := TStatementNode(n.Next);
  754. end;
  755. end;
  756. end;
  757. {$ifdef break_inlining}
  758. { simple sequence of tempcreate, assign and return temp.? }
  759. if GetStatements(left,a) and
  760. (a[0].left.nodetype=tempcreaten) and
  761. (a[1].left.nodetype=assignn) and
  762. (actualtargetnode(@tassignmentnode(a[1].left).left)^.nodetype=temprefn) and
  763. (a[2].left.nodetype=tempdeleten) and
  764. (a[3].left.nodetype=temprefn) and
  765. (ttempcreatenode(a[0].left).tempinfo=ttemprefnode(actualtargetnode(@tassignmentnode(a[1].left).left)^).tempinfo) and
  766. (ttempcreatenode(a[0].left).tempinfo=ttempdeletenode(a[2].left).tempinfo) and
  767. (ttempcreatenode(a[0].left).tempinfo=ttemprefnode(a[3].left).tempinfo) and
  768. { the temp. node might not be references inside the assigned expression }
  769. not(foreachnodestatic(tassignmentnode(a[1].left).right,@NodesEqual,actualtargetnode(@tassignmentnode(a[1].left).left)^)) then
  770. begin
  771. result:=tassignmentnode(a[1].left).right;
  772. tassignmentnode(a[1].left).right:=nil;
  773. { ensure the node is first passed, so the resultdef does not get changed if the
  774. the type conv. below is merged }
  775. firstpass(result);
  776. result:=ctypeconvnode.create_internal(result,ttemprefnode(a[3].left).resultdef);
  777. firstpass(result);
  778. exit;
  779. end;
  780. {$endif break_inlining}
  781. end;
  782. function tblocknode.pass_typecheck:tnode;
  783. var
  784. hp : tstatementnode;
  785. begin
  786. result:=nil;
  787. resultdef:=voidtype;
  788. hp:=tstatementnode(left);
  789. while assigned(hp) do
  790. begin
  791. if assigned(hp.left) then
  792. begin
  793. codegenerror:=false;
  794. typecheckpass(hp.left);
  795. { the resultdef of the block is the last type that is
  796. returned. Normally this is a voidtype. But when the
  797. compiler inserts a block of multiple statements then the
  798. last entry can return a value }
  799. resultdef:=hp.left.resultdef;
  800. end;
  801. hp:=tstatementnode(hp.right);
  802. end;
  803. end;
  804. function tblocknode.pass_1 : tnode;
  805. var
  806. hp : tstatementnode;
  807. FirstNode: Boolean;
  808. //count : longint;
  809. begin
  810. result:=nil;
  811. expectloc:=LOC_VOID;
  812. //count:=0;
  813. hp:=tstatementnode(left);
  814. FirstNode := True;
  815. while assigned(hp) do
  816. begin
  817. if assigned(hp.left) then
  818. begin
  819. { Calling firstpass on a statement node normally causes
  820. problems, so we have to be a little bit hacky if we want
  821. to remove nothing nodes }
  822. codegenerror:=false;
  823. firstpass(tnode(hp));
  824. { If the first node gets deleted, left must be updated,
  825. otherwise it will be a dangling pointer }
  826. if FirstNode then
  827. begin
  828. { If multiple statements are to be removed, they would
  829. have all been removed by the firstpass call above, so
  830. the next one will not be removed, hence it's safe to
  831. set FirstNode to false }
  832. FirstNode := False;
  833. left := hp;
  834. end;
  835. if assigned(hp.left) then
  836. hp.expectloc:=hp.left.expectloc;
  837. end;
  838. expectloc:=hp.expectloc;
  839. //inc(count);
  840. hp:=tstatementnode(hp.right);
  841. end;
  842. end;
  843. {$ifdef state_tracking}
  844. function Tblocknode.track_state_pass(exec_known:boolean):boolean;
  845. var hp:Tstatementnode;
  846. begin
  847. track_state_pass:=false;
  848. hp:=Tstatementnode(left);
  849. while assigned(hp) do
  850. begin
  851. if hp.left.track_state_pass(exec_known) then
  852. track_state_pass:=true;
  853. hp:=Tstatementnode(hp.right);
  854. end;
  855. end;
  856. {$endif state_tracking}
  857. {$ifdef DEBUG_NODE_XML}
  858. procedure TBlockNode.XMLPrintNodeInfo(var T: Text);
  859. var
  860. i_bnf: TBlockNodeFlag;
  861. First: Boolean;
  862. begin
  863. inherited XMLPrintNodeInfo(T);
  864. First := True;
  865. for i_bnf in blocknodeflags do
  866. begin
  867. if First then
  868. begin
  869. Write(T, ' blocknodeflags="', i_bnf);
  870. First := False;
  871. end
  872. else
  873. Write(T, ',', i_bnf)
  874. end;
  875. if not First then
  876. write(T,'"');
  877. end;
  878. {$endif DEBUG_NODE_XML}
  879. {*****************************************************************************
  880. TASMNODE
  881. *****************************************************************************}
  882. constructor tasmnode.create(p : TAsmList);
  883. begin
  884. inherited create(asmn);
  885. p_asm:=p;
  886. asmnodeflags:=[];
  887. currenttai:=nil;
  888. end;
  889. constructor tasmnode.create_get_position;
  890. begin
  891. inherited create(asmn);
  892. p_asm:=nil;
  893. asmnodeflags:=[asmnf_get_asm_position];
  894. currenttai:=nil;
  895. end;
  896. destructor tasmnode.destroy;
  897. begin
  898. if assigned(p_asm) then
  899. p_asm.free;
  900. inherited destroy;
  901. end;
  902. constructor tasmnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  903. var
  904. hp : tai;
  905. begin
  906. inherited ppuload(t,ppufile);
  907. ppufile.getset(tppuset1(asmnodeflags));
  908. if not(asmnf_get_asm_position in asmnodeflags) then
  909. begin
  910. p_asm:=TAsmList.create;
  911. repeat
  912. hp:=ppuloadai(ppufile);
  913. if hp=nil then
  914. break;
  915. p_asm.concat(hp);
  916. if hp.typ=ait_section then
  917. inc(p_asm.section_count);
  918. until false;
  919. end
  920. else
  921. p_asm:=nil;
  922. currenttai:=nil;
  923. end;
  924. procedure tasmnode.ppuwrite(ppufile:tcompilerppufile);
  925. var
  926. hp : tai;
  927. begin
  928. inherited ppuwrite(ppufile);
  929. ppufile.putset(tppuset1(asmnodeflags));
  930. { TODO: FIXME Add saving of register sets}
  931. if not(asmnf_get_asm_position in asmnodeflags) then
  932. begin
  933. hp:=tai(p_asm.first);
  934. while assigned(hp) do
  935. begin
  936. ppuwriteai(ppufile,hp);
  937. hp:=tai(hp.next);
  938. end;
  939. { end is marked by a nil }
  940. ppuwriteai(ppufile,nil);
  941. end;
  942. end;
  943. procedure tasmnode.buildderefimpl;
  944. var
  945. hp : tai;
  946. begin
  947. inherited buildderefimpl;
  948. if not(asmnf_get_asm_position in asmnodeflags) then
  949. begin
  950. hp:=tai(p_asm.first);
  951. while assigned(hp) do
  952. begin
  953. hp.buildderefimpl;
  954. hp:=tai(hp.next);
  955. end;
  956. end;
  957. end;
  958. procedure tasmnode.derefimpl;
  959. var
  960. hp : tai;
  961. begin
  962. inherited derefimpl;
  963. if not(asmnf_get_asm_position in asmnodeflags) then
  964. begin
  965. hp:=tai(p_asm.first);
  966. while assigned(hp) do
  967. begin
  968. hp.derefimpl;
  969. hp:=tai(hp.next);
  970. end;
  971. end;
  972. end;
  973. function tasmnode.dogetcopy: tnode;
  974. var
  975. n: tasmnode;
  976. begin
  977. n:=tasmnode(inherited dogetcopy);
  978. n.asmnodeflags:=asmnodeflags;
  979. if assigned(p_asm) then
  980. begin
  981. n.p_asm:=TAsmList.create;
  982. n.p_asm.concatlistcopy(p_asm);
  983. end
  984. else
  985. n.p_asm:=nil;
  986. n.currenttai:=currenttai;
  987. result:=n;
  988. end;
  989. function tasmnode.pass_typecheck:tnode;
  990. begin
  991. result:=nil;
  992. resultdef:=voidtype;
  993. if not(asmnf_get_asm_position in asmnodeflags) then
  994. include(current_procinfo.flags,pi_has_assembler_block);
  995. end;
  996. function tasmnode.pass_1 : tnode;
  997. begin
  998. result:=nil;
  999. expectloc:=LOC_VOID;
  1000. end;
  1001. function tasmnode.docompare(p: tnode): boolean;
  1002. begin
  1003. { comparing of asmlists is not implemented (JM) }
  1004. docompare := false;
  1005. end;
  1006. {$ifdef DEBUG_NODE_XML}
  1007. procedure TAsmNode.XMLPrintNodeInfo(var T: Text);
  1008. var
  1009. i: TAsmNodeFlag;
  1010. First: Boolean;
  1011. begin
  1012. inherited XMLPrintNodeInfo(T);
  1013. First := True;
  1014. for i in asmnodeflags do
  1015. begin
  1016. if First then
  1017. begin
  1018. Write(T, ' asmnodeflags="', i);
  1019. First := False;
  1020. end
  1021. else
  1022. Write(T, ',', i)
  1023. end;
  1024. if not First then
  1025. Write(T, '"');
  1026. end;
  1027. procedure TAsmNode.XMLPrintNodeData(var T: Text);
  1028. procedure PadString(var S: string; Len: Integer);
  1029. var
  1030. X, C: Integer;
  1031. begin
  1032. C := Length(S);
  1033. if C < Len then
  1034. begin
  1035. SetLength(S, 7);
  1036. for X := C + 1 to Len do
  1037. S[X] := ' '
  1038. end;
  1039. end;
  1040. {$ifndef jvm}
  1041. function FormatOp(const Oper: POper): string;
  1042. begin
  1043. case Oper^.typ of
  1044. top_const:
  1045. begin
  1046. case Oper^.val of
  1047. -15..15:
  1048. Result := '$' + tostr(Oper^.val);
  1049. $10..$FF:
  1050. Result := '$0x' + hexstr(Oper^.val, 2);
  1051. $100..$FFFF:
  1052. Result := '$0x' + hexstr(Oper^.val, 4);
  1053. $10000..$FFFFFFFF:
  1054. Result := '$0x' + hexstr(Oper^.val, 8);
  1055. else
  1056. Result := '$0x' + hexstr(Oper^.val, 16);
  1057. end;
  1058. end;
  1059. top_reg:
  1060. Result := gas_regname(Oper^.reg);
  1061. top_ref:
  1062. with Oper^.ref^ do
  1063. begin
  1064. {$if defined(x86)}
  1065. if segment <> NR_NO then
  1066. Result := gas_regname(segment) + ':'
  1067. else
  1068. {$endif defined(x86)}
  1069. Result := '';
  1070. if Assigned(symbol) then
  1071. begin
  1072. Result := Result + symbol.Name;
  1073. if offset > 0 then
  1074. Result := Result + '+';
  1075. end;
  1076. if offset <> 0 then
  1077. Result := Result + tostr(offset)
  1078. else
  1079. Result := Result;
  1080. if (base <> NR_NO) or (index <> NR_NO) then
  1081. begin
  1082. Result := Result + '(';
  1083. if base <> NR_NO then
  1084. begin
  1085. Result := Result + gas_regname(base);
  1086. if index <> NR_NO then
  1087. Result := Result + ',';
  1088. end;
  1089. if index <> NR_NO then
  1090. Result := Result + gas_regname(index);
  1091. if scalefactor <> 0 then
  1092. Result := Result + ',' + tostr(scalefactor) + ')'
  1093. else
  1094. Result := Result + ')';
  1095. end;
  1096. end;
  1097. top_bool:
  1098. begin
  1099. if Oper^.b then
  1100. Result := 'TRUE'
  1101. else
  1102. Result := 'FALSE';
  1103. end
  1104. else
  1105. Result := '';
  1106. end;
  1107. end;
  1108. {$if defined(x86)}
  1109. procedure ProcessInstruction(p: tai); inline;
  1110. var
  1111. ThisOp, ThisOper: string;
  1112. X: Integer;
  1113. begin
  1114. case p.typ of
  1115. ait_label:
  1116. WriteLn(T, PrintNodeIndention, tai_label(p).labsym.name);
  1117. ait_instruction:
  1118. begin
  1119. ThisOp := gas_op2str[taicpu(p).opcode]+cond2str[taicpu(p).condition];
  1120. if gas_needsuffix[taicpu(p).opcode] <> AttSufNONE then
  1121. ThisOp := ThisOp + gas_opsize2str[taicpu(p).opsize];
  1122. { Pad the opcode with spaces so the succeeding operands are aligned }
  1123. PadString(ThisOp, 7);
  1124. Write(T, PrintNodeIndention, ' ', ThisOp); { Extra indentation to account for label formatting }
  1125. for X := 0 to taicpu(p).ops - 1 do
  1126. begin
  1127. Write(T, ' ');
  1128. ThisOper := FormatOp(taicpu(p).oper[X]);
  1129. if X < taicpu(p).ops - 1 then
  1130. begin
  1131. ThisOper := ThisOper + ',';
  1132. PadString(ThisOper, 7);
  1133. end;
  1134. Write(T, ThisOper);
  1135. end;
  1136. WriteLn(T);
  1137. end;
  1138. else
  1139. { Do nothing };
  1140. end;
  1141. end;
  1142. var
  1143. hp: tai;
  1144. begin
  1145. if not Assigned(p_asm) then
  1146. Exit;
  1147. hp := tai(p_asm.First);
  1148. while Assigned(hp) do
  1149. begin
  1150. ProcessInstruction(hp);
  1151. hp := tai(hp.Next);
  1152. end;
  1153. {$else defined(x86)}
  1154. begin
  1155. WriteLn(T, PrintNodeIndention, '(Assembler output not currently supported on this platform)');
  1156. {$endif defined(x86)}
  1157. {$else jvm}
  1158. begin
  1159. WriteLn(T, PrintNodeIndention, '(Should assembly language even be possible under JVM?)');
  1160. {$endif jvm}
  1161. end;
  1162. {$endif DEBUG_NODE_XML}
  1163. {*****************************************************************************
  1164. TEMPBASENODE
  1165. *****************************************************************************}
  1166. class procedure ttempinfoaccessor.settempinfoflags(tempinfo: ptempinfo; const flags: ttempinfoflags);
  1167. begin
  1168. tempinfo^.flags:=flags;
  1169. end;
  1170. class function ttempinfoaccessor.gettempinfoflags(tempinfo: ptempinfo): ttempinfoflags;
  1171. begin
  1172. result:=tempinfo^.flags;
  1173. end;
  1174. {*****************************************************************************
  1175. TEMPBASENODE
  1176. *****************************************************************************}
  1177. procedure ttempbasenode.settempinfoflags(const tempflags: ttempinfoflags);
  1178. begin
  1179. ctempinfoaccessor.settempinfoflags(tempinfo,tempflags);
  1180. end;
  1181. function ttempbasenode.gettempinfoflags: ttempinfoflags;
  1182. begin
  1183. result:=ctempinfoaccessor.gettempinfoflags(tempinfo);
  1184. end;
  1185. procedure ttempbasenode.includetempflag(flag: ttempinfoflag);
  1186. begin
  1187. { go through settempinfoflags() so it can filter out unsupported tempflags }
  1188. settempinfoflags(gettempinfoflags+[flag])
  1189. end;
  1190. procedure ttempbasenode.excludetempflag(flag: ttempinfoflag);
  1191. begin
  1192. { go through settempinfoflags() so it can prevent required tempflags from
  1193. being removed (if any) }
  1194. settempinfoflags(gettempinfoflags-[flag])
  1195. end;
  1196. {$ifdef DEBUG_NODE_XML}
  1197. procedure TTempBaseNode.XMLPrintNodeInfo(var T: Text);
  1198. begin
  1199. inherited XMLPrintNodeInfo(T);
  1200. { The raw pointer is the only way to uniquely identify the temp }
  1201. Write(T, ' id="', WritePointer(tempinfo), '"');
  1202. end;
  1203. procedure TTempBaseNode.XMLPrintNodeData(var T: Text);
  1204. var
  1205. Flag: TTempInfoFlag;
  1206. NotFirst: Boolean;
  1207. begin
  1208. inherited XMLPrintNodeData(t);
  1209. if not assigned(tempinfo) then
  1210. exit;
  1211. WriteLn(T, PrintNodeIndention, '<typedef>', SanitiseXMLString(tempinfo^.typedef.typesymbolprettyname), '</typedef>');
  1212. NotFirst := False;
  1213. for Flag := Low(TTempInfoFlag) to High(TTempInfoFlag) do
  1214. if (Flag in tempinfo^.flags) then
  1215. if not NotFirst then
  1216. begin
  1217. Write(T, PrintNodeIndention, '<tempflags>', Flag);
  1218. NotFirst := True;
  1219. end
  1220. else
  1221. Write(T, ',', Flag);
  1222. if NotFirst then
  1223. WriteLn(T, '</tempflags>')
  1224. else
  1225. WriteLn(T, PrintNodeIndention, '<tempflags />');
  1226. WriteLn(T, PrintNodeIndention, '<temptype>', tempinfo^.temptype, '</temptype>');
  1227. end;
  1228. {$endif DEBUG_NODE_XML}
  1229. {*****************************************************************************
  1230. TEMPCREATENODE
  1231. *****************************************************************************}
  1232. constructor ttempcreatenode.create(_typedef:tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean);
  1233. begin
  1234. inherited create(tempcreaten);
  1235. size := _size;
  1236. new(tempinfo);
  1237. fillchar(tempinfo^,sizeof(tempinfo^),0);
  1238. tempinfo^.typedef := _typedef;
  1239. tempinfo^.temptype := _temptype;
  1240. tempinfo^.owner := self;
  1241. tempinfo^.withnode := nil;
  1242. if allowreg and
  1243. { temp must fit a single register }
  1244. (tstoreddef(_typedef).is_fpuregable or
  1245. (tstoreddef(_typedef).is_intregable and
  1246. (_size<=TCGSize2Size[OS_64]))) and
  1247. { size of register operations must be known }
  1248. (def_cgsize(_typedef)<>OS_NO) and
  1249. { no init/final needed }
  1250. not is_managed_type(_typedef) then
  1251. includetempflag(ti_may_be_in_reg);
  1252. end;
  1253. constructor ttempcreatenode.create_withnode(_typedef: tdef; _size: tcgint; _temptype: ttemptype; allowreg:boolean; withnode: tnode);
  1254. begin
  1255. self.create(_typedef,_size,_temptype,allowreg);
  1256. tempinfo^.withnode:=withnode.getcopy;
  1257. end;
  1258. constructor ttempcreatenode.create_value(_typedef:tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean; templvalue: tnode);
  1259. begin
  1260. self.create(_typedef,_size,_temptype,allowreg);
  1261. // store in ppuwrite
  1262. ftemplvalue:=templvalue;
  1263. // create from stored ftemplvalue in ppuload
  1264. tempinfo^.tempinitcode:=cassignmentnode.create(ctemprefnode.create(self),ftemplvalue);
  1265. end;
  1266. constructor ttempcreatenode.create_reference(_typedef: tdef; _size: tcgint; _temptype: ttemptype; allowreg: boolean; templvalue: tnode; readonly: boolean);
  1267. begin
  1268. // store in ppuwrite
  1269. self.create(_typedef,_size,_temptype,allowreg);
  1270. ftemplvalue:=templvalue;
  1271. // no assignment node, just the tempvalue
  1272. tempinfo^.tempinitcode:=ftemplvalue;
  1273. includetempflag(ti_reference);
  1274. if readonly then
  1275. includetempflag(ti_readonly);
  1276. end;
  1277. function ttempcreatenode.dogetcopy: tnode;
  1278. var
  1279. n: ttempcreatenode;
  1280. begin
  1281. n := ttempcreatenode(inherited dogetcopy);
  1282. n.size := size;
  1283. new(n.tempinfo);
  1284. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  1285. n.tempinfo^.owner:=n;
  1286. n.tempinfo^.typedef := tempinfo^.typedef;
  1287. n.tempinfo^.temptype := tempinfo^.temptype;
  1288. n.tempflags := tempflags * tempinfostoreflags;
  1289. { when the tempinfo has already a hookoncopy then it is not
  1290. reset by a tempdeletenode }
  1291. if assigned(tempinfo^.hookoncopy) then
  1292. internalerror(200211262);
  1293. { signal the temprefs that the temp they point to has been copied, }
  1294. { so that if the refs get copied as well, they can hook themselves }
  1295. { to the copy of the temp }
  1296. tempinfo^.hookoncopy := n.tempinfo;
  1297. excludetempflag(ti_nextref_set_hookoncopy_nil);
  1298. if assigned(tempinfo^.withnode) then
  1299. n.tempinfo^.withnode := tempinfo^.withnode.getcopy
  1300. else
  1301. n.tempinfo^.withnode := nil;
  1302. if assigned(tempinfo^.tempinitcode) then
  1303. n.tempinfo^.tempinitcode := tempinfo^.tempinitcode.getcopy
  1304. else
  1305. n.tempinfo^.tempinitcode := nil;
  1306. result := n;
  1307. end;
  1308. constructor ttempcreatenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1309. begin
  1310. inherited ppuload(t,ppufile);
  1311. size:=ppufile.getlongint;
  1312. new(tempinfo);
  1313. fillchar(tempinfo^,sizeof(tempinfo^),0);
  1314. ppufile.getset(tppuset2(tempinfo^.flags));
  1315. ppufile.getderef(tempinfo^.typedefderef);
  1316. tempinfo^.temptype := ttemptype(ppufile.getbyte);
  1317. tempinfo^.owner:=self;
  1318. tempinfo^.withnode:=ppuloadnode(ppufile);
  1319. ftemplvalue:=ppuloadnode(ppufile);
  1320. end;
  1321. procedure ttempcreatenode.ppuwrite(ppufile:tcompilerppufile);
  1322. begin
  1323. inherited ppuwrite(ppufile);
  1324. ppufile.putlongint(size);
  1325. ppufile.putset(tppuset2(tempinfo^.flags));
  1326. ppufile.putderef(tempinfo^.typedefderef);
  1327. ppufile.putbyte(byte(tempinfo^.temptype));
  1328. ppuwritenode(ppufile,tempinfo^.withnode);
  1329. ppuwritenode(ppufile,ftemplvalue);
  1330. end;
  1331. procedure ttempcreatenode.buildderefimpl;
  1332. begin
  1333. inherited buildderefimpl;
  1334. tempinfo^.typedefderef.build(tempinfo^.typedef);
  1335. if assigned(tempinfo^.withnode) then
  1336. tempinfo^.withnode.buildderefimpl;
  1337. if assigned(ftemplvalue) then
  1338. ftemplvalue.buildderefimpl;
  1339. end;
  1340. procedure ttempcreatenode.derefimpl;
  1341. begin
  1342. inherited derefimpl;
  1343. tempinfo^.typedef:=tdef(tempinfo^.typedefderef.resolve);
  1344. if assigned(tempinfo^.withnode) then
  1345. tempinfo^.withnode.derefimpl;
  1346. if assigned(ftemplvalue) then
  1347. begin
  1348. ftemplvalue.derefimpl;
  1349. tempinfo^.tempinitcode:=cassignmentnode.create(ctemprefnode.create(self),ftemplvalue);
  1350. end;
  1351. end;
  1352. function ttempcreatenode.pass_1 : tnode;
  1353. begin
  1354. result := nil;
  1355. expectloc:=LOC_VOID;
  1356. { temps which are immutable do not need to be initialized/finalized }
  1357. if (tempinfo^.typedef.needs_inittable) and not(ti_const in tempflags) then
  1358. include(current_procinfo.flags,pi_needs_implicit_finally);
  1359. if assigned(tempinfo^.withnode) then
  1360. firstpass(tempinfo^.withnode);
  1361. if assigned(tempinfo^.tempinitcode) then
  1362. firstpass(tempinfo^.tempinitcode);
  1363. inc(current_procinfo.estimatedtempsize,size);
  1364. { if a temp. create node is loaded from a ppu, it could be that the unit was compiled with other settings which
  1365. enabled a certain type to be stored in a register while the current settings do not support this, so correct this here
  1366. if needed
  1367. }
  1368. if not(tstoreddef(tempinfo^.typedef).is_fpuregable) and not(tstoreddef(tempinfo^.typedef).is_intregable) and (ti_may_be_in_reg in tempflags) then
  1369. excludetempflag(ti_may_be_in_reg);
  1370. end;
  1371. function ttempcreatenode.pass_typecheck: tnode;
  1372. begin
  1373. result := nil;
  1374. { a tempcreatenode doesn't have a resultdef, only temprefnodes do }
  1375. resultdef := voidtype;
  1376. if assigned(tempinfo^.withnode) then
  1377. typecheckpass(tempinfo^.withnode);
  1378. if assigned(tempinfo^.tempinitcode) then
  1379. typecheckpass(tempinfo^.tempinitcode);
  1380. end;
  1381. function ttempcreatenode.docompare(p: tnode): boolean;
  1382. begin
  1383. result :=
  1384. inherited docompare(p) and
  1385. (ttempcreatenode(p).size = size) and
  1386. (ttempcreatenode(p).tempflags*tempinfostoreflags=tempflags*tempinfostoreflags) and
  1387. equal_defs(ttempcreatenode(p).tempinfo^.typedef,tempinfo^.typedef) and
  1388. (ttempcreatenode(p).tempinfo^.withnode.isequal(tempinfo^.withnode)) and
  1389. (ttempcreatenode(p).tempinfo^.tempinitcode.isequal(tempinfo^.tempinitcode));
  1390. end;
  1391. procedure ttempcreatenode.printnodedata(var t:text);
  1392. var
  1393. f: ttempinfoflag;
  1394. first: Boolean;
  1395. begin
  1396. inherited printnodedata(t);
  1397. writeln(t,printnodeindention,'size = ',size,', temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  1398. tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  1399. write(t,printnodeindention,'[');
  1400. first:=true;
  1401. for f in tempflags do
  1402. begin
  1403. if not(first) then
  1404. write(t,',');
  1405. write(t,f);
  1406. first:=false;
  1407. end;
  1408. writeln(t,']');
  1409. writeln(t,printnodeindention,'tempinit =');
  1410. printnode(t,tempinfo^.tempinitcode);
  1411. end;
  1412. {$ifdef DEBUG_NODE_XML}
  1413. procedure TTempCreateNode.XMLPrintNodeData(var T: Text);
  1414. begin
  1415. inherited XMLPrintNodeData(T);
  1416. WriteLn(T, PrintNodeIndention, '<size>', size, '</size>');
  1417. if Assigned(TempInfo^.TempInitCode) then
  1418. begin
  1419. WriteLn(T, PrintNodeIndention, '<tempinit>');
  1420. PrintNodeIndent;
  1421. XMLPrintNode(T, TempInfo^.TempInitCode);
  1422. PrintNodeUnindent;
  1423. WriteLn(T, PrintNodeIndention, '</tempinit>');
  1424. end
  1425. else
  1426. WriteLn(T, PrintNodeIndention, '<tempinit />');
  1427. end;
  1428. {$endif DEBUG_NODE_XML}
  1429. {*****************************************************************************
  1430. TEMPREFNODE
  1431. *****************************************************************************}
  1432. constructor ttemprefnode.create(const temp: ttempcreatenode);
  1433. begin
  1434. inherited create(temprefn);
  1435. tempinfo := temp.tempinfo;
  1436. end;
  1437. function ttemprefnode.dogetcopy: tnode;
  1438. var
  1439. n: ttemprefnode;
  1440. begin
  1441. n := ttemprefnode(inherited dogetcopy);
  1442. if assigned(tempinfo^.hookoncopy) then
  1443. { if the temp has been copied, assume it becomes a new }
  1444. { temp which has to be hooked by the copied reference }
  1445. begin
  1446. { hook the ref to the copied temp }
  1447. n.tempinfo := tempinfo^.hookoncopy;
  1448. { if we passed a ttempdeletenode that changed the temp }
  1449. { from a persistent one into a normal one, we must be }
  1450. { the last reference (since our parent should free the }
  1451. { temp (JM) }
  1452. if (ti_nextref_set_hookoncopy_nil in tempflags) then
  1453. tempinfo^.hookoncopy := nil;
  1454. end
  1455. else
  1456. { if the temp we refer to hasn't been copied, assume }
  1457. { we're just a new reference to that temp }
  1458. begin
  1459. n.tempinfo := tempinfo;
  1460. end;
  1461. if not assigned(n.tempinfo) then
  1462. internalerror(2005071901);
  1463. result := n;
  1464. end;
  1465. constructor ttemprefnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1466. begin
  1467. inherited ppuload(t,ppufile);
  1468. tempidx:=ppufile.getlongint;
  1469. end;
  1470. procedure ttemprefnode.ppuwrite(ppufile:tcompilerppufile);
  1471. begin
  1472. inherited ppuwrite(ppufile);
  1473. ppufile.putlongint(tempinfo^.owner.ppuidx);
  1474. end;
  1475. procedure ttemprefnode.resolveppuidx;
  1476. var
  1477. temp : ttempcreatenode;
  1478. begin
  1479. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  1480. if temp.nodetype<>tempcreaten then
  1481. internalerror(200311075);
  1482. tempinfo:=temp.tempinfo;
  1483. end;
  1484. function ttemprefnode.pass_1 : tnode;
  1485. begin
  1486. expectloc := LOC_REFERENCE;
  1487. if not tempinfo^.typedef.needs_inittable and
  1488. (ti_may_be_in_reg in tempflags) then
  1489. begin
  1490. if tempinfo^.typedef.typ=floatdef then
  1491. begin
  1492. if not use_vectorfpu(tempinfo^.typedef) then
  1493. if (tempinfo^.temptype = tt_persistent) then
  1494. expectloc := LOC_CFPUREGISTER
  1495. else
  1496. expectloc := LOC_FPUREGISTER
  1497. else
  1498. if (tempinfo^.temptype = tt_persistent) then
  1499. expectloc := LOC_CMMREGISTER
  1500. else
  1501. expectloc := LOC_MMREGISTER
  1502. end
  1503. else
  1504. begin
  1505. if (tempinfo^.temptype = tt_persistent) then
  1506. expectloc := LOC_CREGISTER
  1507. else
  1508. expectloc := LOC_REGISTER;
  1509. end;
  1510. end;
  1511. result := nil;
  1512. end;
  1513. function ttemprefnode.pass_typecheck: tnode;
  1514. begin
  1515. { check if the temp is already resultdef passed }
  1516. if not assigned(tempinfo^.typedef) then
  1517. internalerror(200108233);
  1518. result := nil;
  1519. resultdef := tempinfo^.typedef;
  1520. end;
  1521. function ttemprefnode.docompare(p: tnode): boolean;
  1522. begin
  1523. result :=
  1524. inherited docompare(p) and
  1525. (ttemprefnode(p).tempinfo = tempinfo);
  1526. end;
  1527. procedure ttemprefnode.mark_write;
  1528. begin
  1529. include(flags,nf_write);
  1530. end;
  1531. procedure ttemprefnode.printnodedata(var t:text);
  1532. var
  1533. f : ttempinfoflag;
  1534. notfirst : Boolean;
  1535. begin
  1536. inherited printnodedata(t);
  1537. write(t,printnodeindention,'temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  1538. tempinfo^.typedef.GetTypeName,'", (tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2),' flags = [');
  1539. notfirst:=false;
  1540. for f in tempinfo^.flags do
  1541. begin
  1542. if notfirst then
  1543. write(t,',');
  1544. write(t,f);
  1545. notfirst:=true;
  1546. end;
  1547. writeln(t,'])');
  1548. end;
  1549. {*****************************************************************************
  1550. TEMPDELETENODE
  1551. *****************************************************************************}
  1552. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  1553. begin
  1554. inherited create(tempdeleten);
  1555. tempinfo := temp.tempinfo;
  1556. release_to_normal := false;
  1557. end;
  1558. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  1559. begin
  1560. inherited create(tempdeleten);
  1561. tempinfo := temp.tempinfo;
  1562. release_to_normal := true;
  1563. if tempinfo^.temptype <> tt_persistent then
  1564. internalerror(200204211);
  1565. end;
  1566. function ttempdeletenode.dogetcopy: tnode;
  1567. var
  1568. n: ttempdeletenode;
  1569. begin
  1570. n:=ttempdeletenode(inherited dogetcopy);
  1571. n.release_to_normal:=release_to_normal;
  1572. if assigned(tempinfo^.hookoncopy) then
  1573. { if the temp has been copied, assume it becomes a new }
  1574. { temp which has to be hooked by the copied deletenode }
  1575. begin
  1576. { hook the tempdeletenode to the copied temp }
  1577. n.tempinfo:=tempinfo^.hookoncopy;
  1578. { the temp shall not be used, reset hookoncopy }
  1579. { Only if release_to_normal is false, otherwise }
  1580. { the temp can still be referenced once more (JM) }
  1581. if (not release_to_normal) then
  1582. tempinfo^.hookoncopy:=nil
  1583. else
  1584. includetempflag(ti_nextref_set_hookoncopy_nil);
  1585. end
  1586. else
  1587. { if the temp we refer to hasn't been copied, we have a }
  1588. { problem since that means we now have two delete nodes }
  1589. { for one temp }
  1590. internalerror(200108234);
  1591. result:=n;
  1592. end;
  1593. constructor ttempdeletenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1594. begin
  1595. inherited ppuload(t,ppufile);
  1596. tempidx:=ppufile.getlongint;
  1597. release_to_normal:=(ppufile.getbyte<>0);
  1598. end;
  1599. procedure ttempdeletenode.ppuwrite(ppufile:tcompilerppufile);
  1600. begin
  1601. inherited ppuwrite(ppufile);
  1602. ppufile.putlongint(tempinfo^.owner.ppuidx);
  1603. ppufile.putbyte(byte(release_to_normal));
  1604. end;
  1605. procedure ttempdeletenode.resolveppuidx;
  1606. var
  1607. temp : ttempcreatenode;
  1608. begin
  1609. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  1610. if temp.nodetype<>tempcreaten then
  1611. internalerror(2003110701);
  1612. tempinfo:=temp.tempinfo;
  1613. end;
  1614. function ttempdeletenode.pass_1 : tnode;
  1615. begin
  1616. expectloc:=LOC_VOID;
  1617. result := nil;
  1618. end;
  1619. function ttempdeletenode.pass_typecheck: tnode;
  1620. begin
  1621. result := nil;
  1622. resultdef := voidtype;
  1623. end;
  1624. function ttempdeletenode.docompare(p: tnode): boolean;
  1625. begin
  1626. result :=
  1627. inherited docompare(p) and
  1628. (ttemprefnode(p).tempinfo = tempinfo);
  1629. end;
  1630. destructor ttempdeletenode.destroy;
  1631. begin
  1632. tempinfo^.withnode.free;
  1633. tempinfo^.tempinitcode.free;
  1634. dispose(tempinfo);
  1635. inherited destroy;
  1636. end;
  1637. procedure ttempdeletenode.printnodedata(var t:text);
  1638. begin
  1639. inherited printnodedata(t);
  1640. writeln(t,printnodeindention,'release_to_normal: ',release_to_normal,', temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  1641. tempinfo^.typedef.GetTypeName,'", temptype = ',tempinfo^.temptype,', tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  1642. end;
  1643. {$ifdef DEBUG_NODE_XML}
  1644. procedure TTempDeleteNode.XMLPrintNodeData(var T: Text);
  1645. begin
  1646. inherited XMLPrintNodeData(T);
  1647. WriteLn(T, PrintNodeIndention, '<release_to_normal>', release_to_normal, '</release_to_normal>');
  1648. end;
  1649. {$endif DEBUG_NODE_XML}
  1650. end.