nbas.pas 64 KB

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