nbas.pas 43 KB

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