nbas.pas 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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. cpuinfo,cpubase,cgbase,cgutils,
  23. aasmbase,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. end;
  32. tnothingnodeclass = class of tnothingnode;
  33. terrornode = class(tnode)
  34. constructor create;virtual;
  35. function pass_1 : tnode;override;
  36. function pass_typecheck:tnode;override;
  37. procedure mark_write;override;
  38. end;
  39. terrornodeclass = class of terrornode;
  40. tasmnode = class(tnode)
  41. p_asm : TAsmList;
  42. currenttai : tai;
  43. { Used registers in assembler block }
  44. has_registerlist : boolean;
  45. constructor create(p : TAsmList);virtual;
  46. constructor create_get_position;
  47. destructor destroy;override;
  48. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  49. procedure ppuwrite(ppufile:tcompilerppufile);override;
  50. procedure buildderefimpl;override;
  51. procedure derefimpl;override;
  52. function dogetcopy : tnode;override;
  53. function pass_1 : tnode;override;
  54. function pass_typecheck:tnode;override;
  55. function docompare(p: tnode): boolean; override;
  56. end;
  57. tasmnodeclass = class of tasmnode;
  58. tstatementnode = class(tbinarynode)
  59. constructor create(l,r : tnode);virtual;
  60. function simplify(forinline : boolean) : tnode; override;
  61. function pass_1 : tnode;override;
  62. function pass_typecheck:tnode;override;
  63. procedure printnodetree(var t:text);override;
  64. property statement : tnode read left write left;
  65. property next : tnode read right write right;
  66. end;
  67. tstatementnodeclass = class of tstatementnode;
  68. tblocknode = class(tunarynode)
  69. constructor create(l : tnode);virtual;
  70. destructor destroy; override;
  71. function simplify(forinline : boolean) : tnode; override;
  72. function pass_1 : tnode;override;
  73. function pass_typecheck:tnode;override;
  74. {$ifdef state_tracking}
  75. function track_state_pass(exec_known:boolean):boolean;override;
  76. {$endif state_tracking}
  77. property statements : tnode read left write left;
  78. end;
  79. tblocknodeclass = class of tblocknode;
  80. ttempcreatenode = class;
  81. ttempinfoflag = (
  82. { temp can be kept in a register as far as the original creator is
  83. concerned }
  84. ti_may_be_in_reg,
  85. { the ttempcreatenode has been process and the temp's location is
  86. valid (-> the ttempdeletenode has not yet been processed, or
  87. in case it's a "create_to_normal()" one, the final ttemprefnode
  88. has not yet been processed) }
  89. ti_valid,
  90. { when performing a getcopy of a nodetree, we have to hook up the
  91. copies of ttemprefnodes and ttempdestroynode to the copied
  92. ttempinfo. this is done by setting hookoncopy in the original
  93. ttempinfo to point to the new one. if the temp is deleted via a
  94. regular ttempdeletenode, the hookoncopy is simply set to nil once
  95. it's processed. otherwise, it sets the ti_nextref_set_hookoncopy_nil
  96. and after processing the final ttemprefnode, hookoncopy is set to nil
  97. }
  98. ti_nextref_set_hookoncopy_nil,
  99. { the address of this temp is taken (-> cannot be kept in a register,
  100. even if the creator didn't mind)
  101. }
  102. ti_addr_taken,
  103. { temps can get an extra node tree that contains the value to which
  104. they should be initialised when they are created. this initialisation
  105. has to be performed right before the first reference to the temp.
  106. this flag indicates that the ttempcreatenode has been
  107. processed by pass_generate_code, but that the first ttemprefnode
  108. hasn't yet and hence will have to perform the initialisation
  109. }
  110. ti_executeinitialisation,
  111. { in case an expression like "inc(x[func()],1)" is translated into
  112. a regular addition, you have to create a temp to hold the address
  113. representing x[func()], since otherwise func() will be called twice
  114. and that can spell trouble in case it has side effects. on platforms
  115. without pointers, we cannot just take the address though. this flag
  116. has to be combined with ti_executeinitialisation above and will,
  117. rather than loading the value at the calculated location and store
  118. it in the temp, keep a copy of the calculated location if possible
  119. and required (not possible for regvars, because SSA may change their
  120. register, but not required for them either since calculating their
  121. location has no side-effects
  122. }
  123. ti_reference,
  124. { this temp only allows reading (makes it possible to safely use as
  125. reference under more circumstances)
  126. }
  127. ti_readonly,
  128. { if this is a managed temp, it doesn't have to be finalised before use
  129. }
  130. ti_nofini,
  131. { the value described by this temp. node is const/immutable, this is important for
  132. managed types like ansistrings where temp. refs are pointers to the actual value }
  133. ti_const
  134. );
  135. ttempinfoflags = set of ttempinfoflag;
  136. const
  137. tempinfostoreflags = [ti_may_be_in_reg,ti_addr_taken,ti_reference,ti_readonly];
  138. type
  139. { to allow access to the location by temp references even after the temp has }
  140. { already been disposed and to make sure the coherency between temps and }
  141. { temp references is kept after a getcopy }
  142. ptempinfo = ^ttempinfo;
  143. ttempinfo = record
  144. { set to the copy of a tempcreate pnode (if it gets copied) so that the }
  145. { refs and deletenode can hook to this copy once they get copied too }
  146. hookoncopy : ptempinfo;
  147. typedef : tdef;
  148. typedefderef : tderef;
  149. temptype : ttemptype;
  150. owner : ttempcreatenode;
  151. withnode : tnode;
  152. location : tlocation;
  153. flags : ttempinfoflags;
  154. tempinitcode : tnode;
  155. end;
  156. { a node which will create a (non)persistent temp of a given type with a given }
  157. { size (the size is separate to allow creating "void" temps with a custom size) }
  158. ttempcreatenode = class(tnode)
  159. size: tcgint;
  160. tempinfo: ptempinfo;
  161. ftemplvalue : tnode;
  162. { * persistent temps are used in manually written code where the temp }
  163. { be usable among different statements and where you can manually say }
  164. { when the temp has to be freed (using a ttempdeletenode) }
  165. { * non-persistent temps are mostly used in typeconversion helpers, }
  166. { where the node that receives the temp becomes responsible for }
  167. { freeing it. In this last case, you must use only one reference }
  168. { to it and *not* generate a ttempdeletenode }
  169. constructor create(_typedef: tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean); virtual;
  170. constructor create_withnode(_typedef: tdef; _size: tcgint; _temptype: ttemptype; allowreg:boolean; withnode: tnode); virtual;
  171. constructor create_value(_typedef:tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean; templvalue: tnode);
  172. constructor create_reference(_typedef:tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean; templvalue: tnode; readonly: boolean);
  173. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  174. procedure ppuwrite(ppufile:tcompilerppufile);override;
  175. procedure buildderefimpl;override;
  176. procedure derefimpl;override;
  177. function dogetcopy: tnode; override;
  178. function pass_1 : tnode; override;
  179. function pass_typecheck: tnode; override;
  180. function docompare(p: tnode): boolean; override;
  181. procedure printnodedata(var t:text);override;
  182. end;
  183. ttempcreatenodeclass = class of ttempcreatenode;
  184. { a node which is a reference to a certain temp }
  185. ttemprefnode = class(tnode)
  186. tempinfo: ptempinfo;
  187. constructor create(const temp: ttempcreatenode); virtual;
  188. constructor create_offset(const temp: ttempcreatenode;aoffset:longint);
  189. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  190. procedure ppuwrite(ppufile:tcompilerppufile);override;
  191. procedure resolveppuidx;override;
  192. function dogetcopy: tnode; override;
  193. function pass_1 : tnode; override;
  194. function pass_typecheck : tnode; override;
  195. procedure mark_write;override;
  196. function docompare(p: tnode): boolean; override;
  197. procedure printnodedata(var t:text);override;
  198. protected
  199. offset : longint;
  200. private
  201. tempidx : longint;
  202. end;
  203. ttemprefnodeclass = class of ttemprefnode;
  204. { a node which removes a temp }
  205. ttempdeletenode = class(tnode)
  206. tempinfo: ptempinfo;
  207. constructor create(const temp: ttempcreatenode); virtual;
  208. { this will convert the persistant temp to a normal temp
  209. for returning to the other nodes }
  210. constructor create_normal_temp(const temp: ttempcreatenode);
  211. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  212. procedure ppuwrite(ppufile:tcompilerppufile);override;
  213. procedure resolveppuidx;override;
  214. function dogetcopy: tnode; override;
  215. function pass_1: tnode; override;
  216. function pass_typecheck: tnode; override;
  217. function docompare(p: tnode): boolean; override;
  218. destructor destroy; override;
  219. procedure printnodedata(var t:text);override;
  220. protected
  221. release_to_normal : boolean;
  222. private
  223. tempidx : longint;
  224. end;
  225. ttempdeletenodeclass = class of ttempdeletenode;
  226. var
  227. cnothingnode : tnothingnodeclass = tnothingnode;
  228. cerrornode : terrornodeclass = terrornode;
  229. casmnode : tasmnodeclass = tasmnode;
  230. cstatementnode : tstatementnodeclass = tstatementnode;
  231. cblocknode : tblocknodeclass = tblocknode;
  232. ctempcreatenode : ttempcreatenodeclass = ttempcreatenode;
  233. ctemprefnode : ttemprefnodeclass = ttemprefnode;
  234. ctempdeletenode : ttempdeletenodeclass = ttempdeletenode;
  235. { Create a blocknode and statement node for multiple statements
  236. generated internally by the parser }
  237. function internalstatements(out laststatement:tstatementnode):tblocknode;
  238. function laststatement(block:tblocknode):tstatementnode;
  239. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  240. { if the complexity of n is "high", creates a reference temp to n's
  241. location and replace n with a ttemprefnode referring to that location }
  242. function maybereplacewithtempref(var n: tnode; var block: tblocknode; var stat: tstatementnode; size: ASizeInt; readonly: boolean): ttempcreatenode;
  243. implementation
  244. uses
  245. cutils,
  246. verbose,globals,systems,
  247. symconst,symdef,defutil,defcmp,
  248. pass_1,
  249. nutils,nld,ncal,nflw,
  250. procinfo
  251. ;
  252. {*****************************************************************************
  253. Helpers
  254. *****************************************************************************}
  255. function internalstatements(out laststatement:tstatementnode):tblocknode;
  256. begin
  257. { create dummy initial statement }
  258. laststatement := cstatementnode.create(cnothingnode.create,nil);
  259. internalstatements := cblocknode.create(laststatement);
  260. end;
  261. function laststatement(block:tblocknode):tstatementnode;
  262. begin
  263. result:=tstatementnode(block.left);
  264. while assigned(result) and assigned(result.right) do
  265. result:=tstatementnode(result.right);
  266. end;
  267. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  268. begin
  269. if assigned(laststatement.right) then
  270. internalerror(200204201);
  271. laststatement.right:=cstatementnode.create(n,nil);
  272. laststatement:=tstatementnode(laststatement.right);
  273. end;
  274. function maybereplacewithtempref(var n: tnode; var block: tblocknode; var stat: tstatementnode; size: ASizeInt; readonly: boolean): ttempcreatenode;
  275. begin
  276. result:=nil;
  277. if node_complexity(n) > 4 then
  278. begin
  279. result:=ctempcreatenode.create_reference(n.resultdef,size,tt_persistent,true,n,readonly);
  280. typecheckpass(tnode(result));
  281. n:=ctemprefnode.create(result);
  282. typecheckpass(n);
  283. if not assigned(stat) then
  284. block:=internalstatements(stat);
  285. addstatement(stat,result)
  286. end;
  287. end;
  288. {*****************************************************************************
  289. TFIRSTNOTHING
  290. *****************************************************************************}
  291. constructor tnothingnode.create;
  292. begin
  293. inherited create(nothingn);
  294. end;
  295. function tnothingnode.pass_typecheck:tnode;
  296. begin
  297. result:=nil;
  298. resultdef:=voidtype;
  299. end;
  300. function tnothingnode.pass_1 : tnode;
  301. begin
  302. result:=nil;
  303. expectloc:=LOC_VOID;
  304. end;
  305. {*****************************************************************************
  306. TFIRSTERROR
  307. *****************************************************************************}
  308. constructor terrornode.create;
  309. begin
  310. inherited create(errorn);
  311. end;
  312. function terrornode.pass_typecheck:tnode;
  313. begin
  314. result:=nil;
  315. include(flags,nf_error);
  316. codegenerror:=true;
  317. resultdef:=generrordef;
  318. end;
  319. function terrornode.pass_1 : tnode;
  320. begin
  321. result:=nil;
  322. expectloc:=LOC_VOID;
  323. codegenerror:=true;
  324. end;
  325. procedure terrornode.mark_write;
  326. begin
  327. end;
  328. {*****************************************************************************
  329. TSTATEMENTNODE
  330. *****************************************************************************}
  331. constructor tstatementnode.create(l,r : tnode);
  332. begin
  333. inherited create(statementn,l,r);
  334. end;
  335. function is_exit_statement(var n: tnode; arg: pointer): foreachnoderesult;
  336. begin
  337. if (n.nodetype<>exitn) then
  338. result:=fen_false
  339. else
  340. result:=fen_norecurse_true;
  341. end;
  342. function no_exit_statement_in_block(n: tnode): boolean;
  343. begin
  344. result:=not foreachnodestatic(n,@is_exit_statement,nil);
  345. end;
  346. function tstatementnode.simplify(forinline: boolean) : tnode;
  347. begin
  348. result:=nil;
  349. { these "optimizations" are only to make it more easy to recognise }
  350. { blocknodes which at the end of inlining only contain one single }
  351. { statement. Simplifying inside blocknode.simplify could be dangerous }
  352. { because if the main blocknode which makes up a procedure/function }
  353. { body were replaced with a statementn/nothingn, this could cause }
  354. { problems elsewhere in the compiler which expects a blocknode }
  355. { remove next statement if it's a nothing-statement (since if it's }
  356. { the last, it won't remove itself -- see next simplification) }
  357. while assigned(right) and
  358. (tstatementnode(right).left.nodetype = nothingn) do
  359. begin
  360. result:=tstatementnode(right).right;
  361. tstatementnode(right).right:=nil;
  362. right.free;
  363. right:=result;
  364. result:=nil;
  365. end;
  366. { Remove initial nothingn if there are other statements. If there }
  367. { are no other statements, returning nil doesn't help (will be }
  368. { interpreted as "can't be simplified") and replacing the }
  369. { statementnode with a nothingnode cannot be done (because it's }
  370. { possible this statementnode is a child of a blocknode, and }
  371. { blocknodes are expected to only contain statementnodes) }
  372. if (left.nodetype = nothingn) and
  373. assigned(right) then
  374. begin
  375. result:=right;
  376. right:=nil;
  377. exit;
  378. end;
  379. { if the current statement contains a block with one statement,
  380. replace the current statement with that block's statement
  381. (but only if the block does not have nf_block_with_exit set
  382. or has no exit statement, because otherwise it needs an own
  383. exit label, see tests/test/tinline10)
  384. Further, it might not be the user code entry
  385. }
  386. if (left.nodetype = blockn) and
  387. ((left.flags*[nf_block_with_exit,nf_usercode_entry]=[]) or
  388. ((left.flags*[nf_block_with_exit,nf_usercode_entry]=[nf_block_with_exit]) and no_exit_statement_in_block(left))) and
  389. assigned(tblocknode(left).left) and
  390. not assigned(tstatementnode(tblocknode(left).left).right) then
  391. begin
  392. result:=tblocknode(left).left;
  393. tstatementnode(result).right:=right;
  394. right:=nil;
  395. tblocknode(left).left:=nil;
  396. exit;
  397. end;
  398. end;
  399. function tstatementnode.pass_typecheck:tnode;
  400. begin
  401. result:=nil;
  402. resultdef:=voidtype;
  403. { left is the statement itself calln assignn or a complex one }
  404. typecheckpass(left);
  405. if codegenerror then
  406. exit;
  407. { right is the next statement in the list }
  408. if assigned(right) then
  409. typecheckpass(right);
  410. if codegenerror then
  411. exit;
  412. end;
  413. function tstatementnode.pass_1 : tnode;
  414. begin
  415. result:=nil;
  416. { left is the statement itself calln assignn or a complex one }
  417. firstpass(left);
  418. if codegenerror then
  419. exit;
  420. expectloc:=left.expectloc;
  421. { right is the next in the list }
  422. if assigned(right) then
  423. firstpass(right);
  424. if codegenerror then
  425. exit;
  426. end;
  427. procedure tstatementnode.printnodetree(var t:text);
  428. begin
  429. printnodelist(t);
  430. end;
  431. {*****************************************************************************
  432. TBLOCKNODE
  433. *****************************************************************************}
  434. constructor tblocknode.create(l : tnode);
  435. begin
  436. inherited create(blockn,l);
  437. end;
  438. destructor tblocknode.destroy;
  439. var
  440. hp, next: tstatementnode;
  441. begin
  442. hp := tstatementnode(left);
  443. left := nil;
  444. while assigned(hp) do
  445. begin
  446. next := tstatementnode(hp.right);
  447. hp.right := nil;
  448. hp.free;
  449. hp := next;
  450. end;
  451. inherited destroy;
  452. end;
  453. function tblocknode.simplify(forinline : boolean): tnode;
  454. begin
  455. result := nil;
  456. { Warning: never replace a blocknode with another node type, }
  457. { since the block may be the main block of a procedure/function/ }
  458. { main program body, and those nodes should always be blocknodes }
  459. { since that's what the compiler expects elsewhere. }
  460. if assigned(left) and
  461. not assigned(tstatementnode(left).right) then
  462. begin
  463. case tstatementnode(left).left.nodetype of
  464. blockn:
  465. begin
  466. { if the current block contains only one statement, and
  467. this one statement only contains another block, replace
  468. this block with that other block. }
  469. result:=tstatementnode(left).left;
  470. tstatementnode(left).left:=nil;
  471. { make sure the nf_block_with_exit flag is safeguarded }
  472. result.flags:=result.flags+(flags*[nf_block_with_exit,nf_usercode_entry]);
  473. exit;
  474. end;
  475. nothingn:
  476. begin
  477. { if the block contains only a statement with a nothing node,
  478. get rid of the statement }
  479. left.Free;
  480. left:=nil;
  481. exit;
  482. end;
  483. end;
  484. end;
  485. end;
  486. function tblocknode.pass_typecheck:tnode;
  487. var
  488. hp : tstatementnode;
  489. begin
  490. result:=nil;
  491. resultdef:=voidtype;
  492. hp:=tstatementnode(left);
  493. while assigned(hp) do
  494. begin
  495. if assigned(hp.left) then
  496. begin
  497. codegenerror:=false;
  498. typecheckpass(hp.left);
  499. { the resultdef of the block is the last type that is
  500. returned. Normally this is a voidtype. But when the
  501. compiler inserts a block of multiple statements then the
  502. last entry can return a value }
  503. resultdef:=hp.left.resultdef;
  504. end;
  505. hp:=tstatementnode(hp.right);
  506. end;
  507. end;
  508. function tblocknode.pass_1 : tnode;
  509. var
  510. hp : tstatementnode;
  511. //count : longint;
  512. begin
  513. result:=nil;
  514. expectloc:=LOC_VOID;
  515. //count:=0;
  516. hp:=tstatementnode(left);
  517. while assigned(hp) do
  518. begin
  519. if assigned(hp.left) then
  520. begin
  521. codegenerror:=false;
  522. firstpass(hp.left);
  523. hp.expectloc:=hp.left.expectloc;
  524. end;
  525. expectloc:=hp.expectloc;
  526. //inc(count);
  527. hp:=tstatementnode(hp.right);
  528. end;
  529. end;
  530. {$ifdef state_tracking}
  531. function Tblocknode.track_state_pass(exec_known:boolean):boolean;
  532. var hp:Tstatementnode;
  533. begin
  534. track_state_pass:=false;
  535. hp:=Tstatementnode(left);
  536. while assigned(hp) do
  537. begin
  538. if hp.left.track_state_pass(exec_known) then
  539. track_state_pass:=true;
  540. hp:=Tstatementnode(hp.right);
  541. end;
  542. end;
  543. {$endif state_tracking}
  544. {*****************************************************************************
  545. TASMNODE
  546. *****************************************************************************}
  547. constructor tasmnode.create(p : TAsmList);
  548. begin
  549. inherited create(asmn);
  550. p_asm:=p;
  551. currenttai:=nil;
  552. end;
  553. constructor tasmnode.create_get_position;
  554. begin
  555. inherited create(asmn);
  556. p_asm:=nil;
  557. include(flags,nf_get_asm_position);
  558. currenttai:=nil;
  559. end;
  560. destructor tasmnode.destroy;
  561. begin
  562. if assigned(p_asm) then
  563. p_asm.free;
  564. inherited destroy;
  565. end;
  566. constructor tasmnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  567. var
  568. hp : tai;
  569. begin
  570. inherited ppuload(t,ppufile);
  571. if not(nf_get_asm_position in flags) then
  572. begin
  573. p_asm:=TAsmList.create;
  574. repeat
  575. hp:=ppuloadai(ppufile);
  576. if hp=nil then
  577. break;
  578. p_asm.concat(hp);
  579. until false;
  580. end
  581. else
  582. p_asm:=nil;
  583. currenttai:=nil;
  584. end;
  585. procedure tasmnode.ppuwrite(ppufile:tcompilerppufile);
  586. var
  587. hp : tai;
  588. begin
  589. inherited ppuwrite(ppufile);
  590. { TODO: FIXME Add saving of register sets}
  591. if not(nf_get_asm_position in flags) then
  592. begin
  593. hp:=tai(p_asm.first);
  594. while assigned(hp) do
  595. begin
  596. ppuwriteai(ppufile,hp);
  597. hp:=tai(hp.next);
  598. end;
  599. { end is marked by a nil }
  600. ppuwriteai(ppufile,nil);
  601. end;
  602. end;
  603. procedure tasmnode.buildderefimpl;
  604. var
  605. hp : tai;
  606. begin
  607. inherited buildderefimpl;
  608. if not(nf_get_asm_position in flags) then
  609. begin
  610. hp:=tai(p_asm.first);
  611. while assigned(hp) do
  612. begin
  613. hp.buildderefimpl;
  614. hp:=tai(hp.next);
  615. end;
  616. end;
  617. end;
  618. procedure tasmnode.derefimpl;
  619. var
  620. hp : tai;
  621. begin
  622. inherited derefimpl;
  623. if not(nf_get_asm_position in flags) then
  624. begin
  625. hp:=tai(p_asm.first);
  626. while assigned(hp) do
  627. begin
  628. hp.derefimpl;
  629. hp:=tai(hp.next);
  630. end;
  631. end;
  632. end;
  633. function tasmnode.dogetcopy: tnode;
  634. var
  635. n: tasmnode;
  636. begin
  637. n := tasmnode(inherited dogetcopy);
  638. if assigned(p_asm) then
  639. begin
  640. n.p_asm:=TAsmList.create;
  641. n.p_asm.concatlistcopy(p_asm);
  642. end
  643. else n.p_asm := nil;
  644. n.currenttai:=currenttai;
  645. n.has_registerlist:=has_registerlist;
  646. result:=n;
  647. end;
  648. function tasmnode.pass_typecheck:tnode;
  649. begin
  650. result:=nil;
  651. resultdef:=voidtype;
  652. if not(nf_get_asm_position in flags) then
  653. include(current_procinfo.flags,pi_has_assembler_block);
  654. end;
  655. function tasmnode.pass_1 : tnode;
  656. begin
  657. result:=nil;
  658. expectloc:=LOC_VOID;
  659. end;
  660. function tasmnode.docompare(p: tnode): boolean;
  661. begin
  662. { comparing of asmlists is not implemented (JM) }
  663. docompare := false;
  664. end;
  665. {*****************************************************************************
  666. TEMPCREATENODE
  667. *****************************************************************************}
  668. constructor ttempcreatenode.create(_typedef:tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean);
  669. begin
  670. inherited create(tempcreaten);
  671. size := _size;
  672. new(tempinfo);
  673. fillchar(tempinfo^,sizeof(tempinfo^),0);
  674. tempinfo^.typedef := _typedef;
  675. tempinfo^.temptype := _temptype;
  676. tempinfo^.owner := self;
  677. tempinfo^.withnode := nil;
  678. if allowreg and
  679. { temp must fit a single register }
  680. (tstoreddef(_typedef).is_fpuregable or
  681. (tstoreddef(_typedef).is_intregable and
  682. (_size<=TCGSize2Size[OS_64]))) and
  683. { size of register operations must be known }
  684. (def_cgsize(_typedef)<>OS_NO) and
  685. { no init/final needed }
  686. not is_managed_type(_typedef) then
  687. include(tempinfo^.flags,ti_may_be_in_reg);
  688. end;
  689. constructor ttempcreatenode.create_withnode(_typedef: tdef; _size: tcgint; _temptype: ttemptype; allowreg:boolean; withnode: tnode);
  690. begin
  691. self.create(_typedef,_size,_temptype,allowreg);
  692. tempinfo^.withnode:=withnode.getcopy;
  693. end;
  694. constructor ttempcreatenode.create_value(_typedef:tdef; _size: tcgint; _temptype: ttemptype;allowreg:boolean; templvalue: tnode);
  695. begin
  696. self.create(_typedef,_size,_temptype,allowreg);
  697. // store in ppuwrite
  698. ftemplvalue:=templvalue;
  699. // create from stored ftemplvalue in ppuload
  700. tempinfo^.tempinitcode:=cassignmentnode.create(ctemprefnode.create(self),ftemplvalue);
  701. end;
  702. constructor ttempcreatenode.create_reference(_typedef: tdef; _size: tcgint; _temptype: ttemptype; allowreg: boolean; templvalue: tnode; readonly: boolean);
  703. begin
  704. // store in ppuwrite
  705. self.create(_typedef,_size,_temptype,allowreg);
  706. ftemplvalue:=templvalue;
  707. // no assignment node, just the tempvalue
  708. tempinfo^.tempinitcode:=ftemplvalue;
  709. include(tempinfo^.flags,ti_reference);
  710. if readonly then
  711. include(tempinfo^.flags,ti_readonly);
  712. end;
  713. function ttempcreatenode.dogetcopy: tnode;
  714. var
  715. n: ttempcreatenode;
  716. begin
  717. n := ttempcreatenode(inherited dogetcopy);
  718. n.size := size;
  719. new(n.tempinfo);
  720. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  721. n.tempinfo^.owner:=n;
  722. n.tempinfo^.typedef := tempinfo^.typedef;
  723. n.tempinfo^.temptype := tempinfo^.temptype;
  724. n.tempinfo^.flags := tempinfo^.flags * tempinfostoreflags;
  725. { when the tempinfo has already a hookoncopy then it is not
  726. reset by a tempdeletenode }
  727. if assigned(tempinfo^.hookoncopy) then
  728. internalerror(200211262);
  729. { signal the temprefs that the temp they point to has been copied, }
  730. { so that if the refs get copied as well, they can hook themselves }
  731. { to the copy of the temp }
  732. tempinfo^.hookoncopy := n.tempinfo;
  733. exclude(tempinfo^.flags,ti_nextref_set_hookoncopy_nil);
  734. if assigned(tempinfo^.withnode) then
  735. n.tempinfo^.withnode := tempinfo^.withnode.getcopy
  736. else
  737. n.tempinfo^.withnode := nil;
  738. if assigned(tempinfo^.tempinitcode) then
  739. n.tempinfo^.tempinitcode := tempinfo^.tempinitcode.getcopy
  740. else
  741. n.tempinfo^.tempinitcode := nil;
  742. result := n;
  743. end;
  744. constructor ttempcreatenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  745. begin
  746. inherited ppuload(t,ppufile);
  747. size:=ppufile.getlongint;
  748. new(tempinfo);
  749. fillchar(tempinfo^,sizeof(tempinfo^),0);
  750. ppufile.getsmallset(tempinfo^.flags);
  751. ppufile.getderef(tempinfo^.typedefderef);
  752. tempinfo^.temptype := ttemptype(ppufile.getbyte);
  753. tempinfo^.owner:=self;
  754. tempinfo^.withnode:=ppuloadnode(ppufile);
  755. ftemplvalue:=ppuloadnode(ppufile);
  756. end;
  757. procedure ttempcreatenode.ppuwrite(ppufile:tcompilerppufile);
  758. begin
  759. inherited ppuwrite(ppufile);
  760. ppufile.putlongint(size);
  761. ppufile.putsmallset(tempinfo^.flags);
  762. ppufile.putderef(tempinfo^.typedefderef);
  763. ppufile.putbyte(byte(tempinfo^.temptype));
  764. ppuwritenode(ppufile,tempinfo^.withnode);
  765. ppuwritenode(ppufile,ftemplvalue);
  766. end;
  767. procedure ttempcreatenode.buildderefimpl;
  768. begin
  769. inherited buildderefimpl;
  770. tempinfo^.typedefderef.build(tempinfo^.typedef);
  771. if assigned(tempinfo^.withnode) then
  772. tempinfo^.withnode.buildderefimpl;
  773. if assigned(ftemplvalue) then
  774. ftemplvalue.buildderefimpl;
  775. end;
  776. procedure ttempcreatenode.derefimpl;
  777. begin
  778. inherited derefimpl;
  779. tempinfo^.typedef:=tdef(tempinfo^.typedefderef.resolve);
  780. if assigned(tempinfo^.withnode) then
  781. tempinfo^.withnode.derefimpl;
  782. if assigned(ftemplvalue) then
  783. begin
  784. ftemplvalue.derefimpl;
  785. tempinfo^.tempinitcode:=cassignmentnode.create(ctemprefnode.create(self),ftemplvalue);
  786. end;
  787. end;
  788. function ttempcreatenode.pass_1 : tnode;
  789. begin
  790. result := nil;
  791. expectloc:=LOC_VOID;
  792. { temps which are immutable do not need to be initialized/finalized }
  793. if (tempinfo^.typedef.needs_inittable) and not(ti_const in tempinfo^.flags) then
  794. include(current_procinfo.flags,pi_needs_implicit_finally);
  795. if (cs_create_pic in current_settings.moduleswitches) and
  796. (tf_pic_uses_got in target_info.flags) and
  797. is_rtti_managed_type(tempinfo^.typedef) then
  798. include(current_procinfo.flags,pi_needs_got);
  799. if assigned(tempinfo^.withnode) then
  800. firstpass(tempinfo^.withnode);
  801. if assigned(tempinfo^.tempinitcode) then
  802. firstpass(tempinfo^.tempinitcode);
  803. inc(current_procinfo.estimatedtempsize,size);;
  804. end;
  805. function ttempcreatenode.pass_typecheck: tnode;
  806. begin
  807. result := nil;
  808. { a tempcreatenode doesn't have a resultdef, only temprefnodes do }
  809. resultdef := voidtype;
  810. if assigned(tempinfo^.withnode) then
  811. typecheckpass(tempinfo^.withnode);
  812. if assigned(tempinfo^.tempinitcode) then
  813. typecheckpass(tempinfo^.tempinitcode);
  814. end;
  815. function ttempcreatenode.docompare(p: tnode): boolean;
  816. begin
  817. result :=
  818. inherited docompare(p) and
  819. (ttempcreatenode(p).size = size) and
  820. (ttempcreatenode(p).tempinfo^.flags*tempinfostoreflags=tempinfo^.flags*tempinfostoreflags) and
  821. equal_defs(ttempcreatenode(p).tempinfo^.typedef,tempinfo^.typedef) and
  822. (ttempcreatenode(p).tempinfo^.withnode.isequal(tempinfo^.withnode)) and
  823. (ttempcreatenode(p).tempinfo^.tempinitcode.isequal(tempinfo^.tempinitcode));
  824. end;
  825. procedure ttempcreatenode.printnodedata(var t:text);
  826. begin
  827. inherited printnodedata(t);
  828. writeln(t,printnodeindention,'size = ',size,', temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  829. tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  830. writeln(t,printnodeindention,'tempinit =');
  831. printnode(t,tempinfo^.tempinitcode);
  832. end;
  833. {*****************************************************************************
  834. TEMPREFNODE
  835. *****************************************************************************}
  836. constructor ttemprefnode.create(const temp: ttempcreatenode);
  837. begin
  838. inherited create(temprefn);
  839. tempinfo := temp.tempinfo;
  840. offset:=0;
  841. end;
  842. constructor ttemprefnode.create_offset(const temp: ttempcreatenode;aoffset:longint);
  843. begin
  844. self.create(temp);
  845. offset := aoffset;
  846. end;
  847. function ttemprefnode.dogetcopy: tnode;
  848. var
  849. n: ttemprefnode;
  850. begin
  851. n := ttemprefnode(inherited dogetcopy);
  852. n.offset := offset;
  853. if assigned(tempinfo^.hookoncopy) then
  854. { if the temp has been copied, assume it becomes a new }
  855. { temp which has to be hooked by the copied reference }
  856. begin
  857. { hook the ref to the copied temp }
  858. n.tempinfo := tempinfo^.hookoncopy;
  859. { if we passed a ttempdeletenode that changed the temp }
  860. { from a persistent one into a normal one, we must be }
  861. { the last reference (since our parent should free the }
  862. { temp (JM) }
  863. if (ti_nextref_set_hookoncopy_nil in tempinfo^.flags) then
  864. tempinfo^.hookoncopy := nil;
  865. end
  866. else
  867. { if the temp we refer to hasn't been copied, assume }
  868. { we're just a new reference to that temp }
  869. begin
  870. n.tempinfo := tempinfo;
  871. end;
  872. if not assigned(n.tempinfo) then
  873. internalerror(2005071901);
  874. result := n;
  875. end;
  876. constructor ttemprefnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  877. begin
  878. inherited ppuload(t,ppufile);
  879. tempidx:=ppufile.getlongint;
  880. offset:=ppufile.getlongint;
  881. end;
  882. procedure ttemprefnode.ppuwrite(ppufile:tcompilerppufile);
  883. begin
  884. inherited ppuwrite(ppufile);
  885. ppufile.putlongint(tempinfo^.owner.ppuidx);
  886. ppufile.putlongint(offset);
  887. end;
  888. procedure ttemprefnode.resolveppuidx;
  889. var
  890. temp : ttempcreatenode;
  891. begin
  892. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  893. if temp.nodetype<>tempcreaten then
  894. internalerror(200311075);
  895. tempinfo:=temp.tempinfo;
  896. end;
  897. function ttemprefnode.pass_1 : tnode;
  898. begin
  899. expectloc := LOC_REFERENCE;
  900. if not tempinfo^.typedef.needs_inittable and
  901. (ti_may_be_in_reg in tempinfo^.flags) then
  902. begin
  903. if tempinfo^.typedef.typ=floatdef then
  904. begin
  905. if not use_vectorfpu(tempinfo^.typedef) then
  906. if (tempinfo^.temptype = tt_persistent) then
  907. expectloc := LOC_CFPUREGISTER
  908. else
  909. expectloc := LOC_FPUREGISTER
  910. else
  911. if (tempinfo^.temptype = tt_persistent) then
  912. expectloc := LOC_CMMREGISTER
  913. else
  914. expectloc := LOC_MMREGISTER
  915. end
  916. else
  917. begin
  918. if (tempinfo^.temptype = tt_persistent) then
  919. expectloc := LOC_CREGISTER
  920. else
  921. expectloc := LOC_REGISTER;
  922. end;
  923. end;
  924. result := nil;
  925. end;
  926. function ttemprefnode.pass_typecheck: tnode;
  927. begin
  928. { check if the temp is already resultdef passed }
  929. if not assigned(tempinfo^.typedef) then
  930. internalerror(200108233);
  931. result := nil;
  932. resultdef := tempinfo^.typedef;
  933. end;
  934. function ttemprefnode.docompare(p: tnode): boolean;
  935. begin
  936. result :=
  937. inherited docompare(p) and
  938. (ttemprefnode(p).tempinfo = tempinfo) and
  939. (ttemprefnode(p).offset = offset);
  940. end;
  941. procedure ttemprefnode.mark_write;
  942. begin
  943. include(flags,nf_write);
  944. end;
  945. procedure ttemprefnode.printnodedata(var t:text);
  946. begin
  947. inherited printnodedata(t);
  948. writeln(t,printnodeindention,'temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  949. tempinfo^.typedef.GetTypeName,'", tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  950. end;
  951. {*****************************************************************************
  952. TEMPDELETENODE
  953. *****************************************************************************}
  954. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  955. begin
  956. inherited create(tempdeleten);
  957. tempinfo := temp.tempinfo;
  958. release_to_normal := false;
  959. end;
  960. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  961. begin
  962. inherited create(tempdeleten);
  963. tempinfo := temp.tempinfo;
  964. release_to_normal := true;
  965. if tempinfo^.temptype <> tt_persistent then
  966. internalerror(200204211);
  967. end;
  968. function ttempdeletenode.dogetcopy: tnode;
  969. var
  970. n: ttempdeletenode;
  971. begin
  972. n:=ttempdeletenode(inherited dogetcopy);
  973. n.release_to_normal:=release_to_normal;
  974. if assigned(tempinfo^.hookoncopy) then
  975. { if the temp has been copied, assume it becomes a new }
  976. { temp which has to be hooked by the copied deletenode }
  977. begin
  978. { hook the tempdeletenode to the copied temp }
  979. n.tempinfo:=tempinfo^.hookoncopy;
  980. { the temp shall not be used, reset hookoncopy }
  981. { Only if release_to_normal is false, otherwise }
  982. { the temp can still be referenced once more (JM) }
  983. if (not release_to_normal) then
  984. tempinfo^.hookoncopy:=nil
  985. else
  986. include(tempinfo^.flags,ti_nextref_set_hookoncopy_nil);
  987. end
  988. else
  989. { if the temp we refer to hasn't been copied, we have a }
  990. { problem since that means we now have two delete nodes }
  991. { for one temp }
  992. internalerror(200108234);
  993. result:=n;
  994. end;
  995. constructor ttempdeletenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  996. begin
  997. inherited ppuload(t,ppufile);
  998. tempidx:=ppufile.getlongint;
  999. release_to_normal:=(ppufile.getbyte<>0);
  1000. end;
  1001. procedure ttempdeletenode.ppuwrite(ppufile:tcompilerppufile);
  1002. begin
  1003. inherited ppuwrite(ppufile);
  1004. ppufile.putlongint(tempinfo^.owner.ppuidx);
  1005. ppufile.putbyte(byte(release_to_normal));
  1006. end;
  1007. procedure ttempdeletenode.resolveppuidx;
  1008. var
  1009. temp : ttempcreatenode;
  1010. begin
  1011. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  1012. if temp.nodetype<>tempcreaten then
  1013. internalerror(200311075);
  1014. tempinfo:=temp.tempinfo;
  1015. end;
  1016. function ttempdeletenode.pass_1 : tnode;
  1017. begin
  1018. expectloc:=LOC_VOID;
  1019. result := nil;
  1020. end;
  1021. function ttempdeletenode.pass_typecheck: tnode;
  1022. begin
  1023. result := nil;
  1024. resultdef := voidtype;
  1025. end;
  1026. function ttempdeletenode.docompare(p: tnode): boolean;
  1027. begin
  1028. result :=
  1029. inherited docompare(p) and
  1030. (ttemprefnode(p).tempinfo = tempinfo);
  1031. end;
  1032. destructor ttempdeletenode.destroy;
  1033. begin
  1034. tempinfo^.withnode.free;
  1035. tempinfo^.tempinitcode.free;
  1036. dispose(tempinfo);
  1037. end;
  1038. procedure ttempdeletenode.printnodedata(var t:text);
  1039. begin
  1040. inherited printnodedata(t);
  1041. writeln(t,printnodeindention,'release_to_normal: ',release_to_normal,', temptypedef = ',tempinfo^.typedef.typesymbolprettyname,' = "',
  1042. tempinfo^.typedef.GetTypeName,'", temptype = ',tempinfo^.temptype,', tempinfo = $',hexstr(ptrint(tempinfo),sizeof(ptrint)*2));
  1043. end;
  1044. end.