nbas.pas 60 KB

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