nbas.pas 62 KB

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