nbas.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. This unit implements some basic nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nbas;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,
  23. cpuinfo,cpubase,cgbase,cgutils,
  24. aasmbase,aasmtai,aasmcpu,
  25. node,
  26. symtype;
  27. type
  28. tnothingnode = class(tnode)
  29. constructor create;virtual;
  30. function pass_1 : tnode;override;
  31. function det_resulttype:tnode;override;
  32. end;
  33. tnothingnodeclass = class of tnothingnode;
  34. terrornode = class(tnode)
  35. constructor create;virtual;
  36. function pass_1 : tnode;override;
  37. function det_resulttype:tnode;override;
  38. procedure mark_write;override;
  39. end;
  40. terrornodeclass = class of terrornode;
  41. tasmnode = class(tnode)
  42. p_asm : taasmoutput;
  43. currenttai : tai;
  44. { Used registers in assembler block }
  45. used_regs_int,
  46. used_regs_fpu : tcpuregisterset;
  47. constructor create(p : taasmoutput);virtual;
  48. constructor create_get_position;
  49. destructor destroy;override;
  50. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  51. procedure ppuwrite(ppufile:tcompilerppufile);override;
  52. procedure buildderefimpl;override;
  53. procedure derefimpl;override;
  54. function getcopy : tnode;override;
  55. function pass_1 : tnode;override;
  56. function det_resulttype:tnode;override;
  57. function docompare(p: tnode): boolean; override;
  58. end;
  59. tasmnodeclass = class of tasmnode;
  60. tstatementnode = class(tbinarynode)
  61. constructor create(l,r : tnode);virtual;
  62. function pass_1 : tnode;override;
  63. function det_resulttype:tnode;override;
  64. procedure printnodetree(var t:text);override;
  65. end;
  66. tstatementnodeclass = class of tstatementnode;
  67. tblocknode = class(tunarynode)
  68. constructor create(l : tnode);virtual;
  69. destructor destroy; override;
  70. function pass_1 : tnode;override;
  71. function det_resulttype:tnode;override;
  72. {$ifdef state_tracking}
  73. function track_state_pass(exec_known:boolean):boolean;override;
  74. {$endif state_tracking}
  75. end;
  76. tblocknodeclass = class of tblocknode;
  77. ttempcreatenode = class;
  78. ttemplocation = record
  79. case loc: tcgloc of
  80. LOC_REFERENCE: (ref: treference);
  81. LOC_REGISTER: (reg: tregister);
  82. end;
  83. { to allow access to the location by temp references even after the temp has }
  84. { already been disposed and to make sure the coherency between temps and }
  85. { temp references is kept after a getcopy }
  86. ptempinfo = ^ttempinfo;
  87. ttempinfo = record
  88. { set to the copy of a tempcreate pnode (if it gets copied) so that the }
  89. { refs and deletenode can hook to this copy once they get copied too }
  90. hookoncopy : ptempinfo;
  91. restype : ttype;
  92. temptype : ttemptype;
  93. owner : ttempcreatenode;
  94. may_be_in_reg : boolean;
  95. valid : boolean;
  96. nextref_set_hookoncopy_nil : boolean;
  97. loc : ttemplocation;
  98. end;
  99. { a node which will create a (non)persistent temp of a given type with a given }
  100. { size (the size is separate to allow creating "void" temps with a custom size) }
  101. ttempcreatenode = class(tnode)
  102. size: longint;
  103. tempinfo: ptempinfo;
  104. { * persistent temps are used in manually written code where the temp }
  105. { be usable among different statements and where you can manually say }
  106. { when the temp has to be freed (using a ttempdeletenode) }
  107. { * non-persistent temps are mostly used in typeconversion helpers, }
  108. { where the node that receives the temp becomes responsible for }
  109. { freeing it. In this last case, you must use only one reference }
  110. { to it and *not* generate a ttempdeletenode }
  111. constructor create(const _restype: ttype; _size: longint; _temptype: ttemptype;allowreg:boolean); virtual;
  112. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  113. procedure ppuwrite(ppufile:tcompilerppufile);override;
  114. procedure buildderefimpl;override;
  115. procedure derefimpl;override;
  116. function getcopy: tnode; override;
  117. function pass_1 : tnode; override;
  118. function det_resulttype: tnode; override;
  119. function docompare(p: tnode): boolean; override;
  120. procedure printnodedata(var t:text);override;
  121. end;
  122. ttempcreatenodeclass = class of ttempcreatenode;
  123. { a node which is a reference to a certain temp }
  124. ttemprefnode = class(tnode)
  125. constructor create(const temp: ttempcreatenode); virtual;
  126. constructor create_offset(const temp: ttempcreatenode;aoffset:longint);
  127. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  128. procedure ppuwrite(ppufile:tcompilerppufile);override;
  129. function getcopy: tnode; override;
  130. procedure derefnode;override;
  131. function pass_1 : tnode; override;
  132. function det_resulttype : tnode; override;
  133. procedure mark_write;override;
  134. function docompare(p: tnode): boolean; override;
  135. protected
  136. tempinfo: ptempinfo;
  137. offset : longint;
  138. private
  139. tempidx : longint;
  140. end;
  141. ttemprefnodeclass = class of ttemprefnode;
  142. { a node which removes a temp }
  143. ttempdeletenode = class(tnode)
  144. constructor create(const temp: ttempcreatenode); virtual;
  145. { this will convert the persistant temp to a normal temp
  146. for returning to the other nodes }
  147. constructor create_normal_temp(const temp: ttempcreatenode);
  148. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  149. procedure ppuwrite(ppufile:tcompilerppufile);override;
  150. function getcopy: tnode; override;
  151. procedure derefnode;override;
  152. function pass_1: tnode; override;
  153. function det_resulttype: tnode; override;
  154. function docompare(p: tnode): boolean; override;
  155. destructor destroy; override;
  156. protected
  157. tempinfo: ptempinfo;
  158. release_to_normal : boolean;
  159. private
  160. tempidx : longint;
  161. end;
  162. ttempdeletenodeclass = class of ttempdeletenode;
  163. var
  164. cnothingnode : tnothingnodeclass;
  165. cerrornode : terrornodeclass;
  166. casmnode : tasmnodeclass;
  167. cstatementnode : tstatementnodeclass;
  168. cblocknode : tblocknodeclass;
  169. ctempcreatenode : ttempcreatenodeclass;
  170. ctemprefnode : ttemprefnodeclass;
  171. ctempdeletenode : ttempdeletenodeclass;
  172. { Create a blocknode and statement node for multiple statements
  173. generated internally by the parser }
  174. function internalstatements(var laststatement:tstatementnode):tblocknode;
  175. function laststatement(block:tblocknode):tstatementnode;
  176. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  177. implementation
  178. uses
  179. cutils,
  180. verbose,globals,systems,
  181. symconst,symdef,defutil,defcmp,
  182. pass_1,
  183. nld,ncal,nflw,
  184. procinfo
  185. ;
  186. {*****************************************************************************
  187. Helpers
  188. *****************************************************************************}
  189. function internalstatements(var laststatement:tstatementnode):tblocknode;
  190. begin
  191. { create dummy initial statement }
  192. laststatement := cstatementnode.create(cnothingnode.create,nil);
  193. internalstatements := cblocknode.create(laststatement);
  194. end;
  195. function laststatement(block:tblocknode):tstatementnode;
  196. begin
  197. result:=tstatementnode(block.left);
  198. while assigned(result) do
  199. result:=tstatementnode(result.right);
  200. end;
  201. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  202. begin
  203. if assigned(laststatement.right) then
  204. internalerror(200204201);
  205. laststatement.right:=cstatementnode.create(n,nil);
  206. laststatement:=tstatementnode(laststatement.right);
  207. end;
  208. {*****************************************************************************
  209. TFIRSTNOTHING
  210. *****************************************************************************}
  211. constructor tnothingnode.create;
  212. begin
  213. inherited create(nothingn);
  214. end;
  215. function tnothingnode.det_resulttype:tnode;
  216. begin
  217. result:=nil;
  218. resulttype:=voidtype;
  219. end;
  220. function tnothingnode.pass_1 : tnode;
  221. begin
  222. result:=nil;
  223. expectloc:=LOC_VOID;
  224. end;
  225. {*****************************************************************************
  226. TFIRSTERROR
  227. *****************************************************************************}
  228. constructor terrornode.create;
  229. begin
  230. inherited create(errorn);
  231. end;
  232. function terrornode.det_resulttype:tnode;
  233. begin
  234. result:=nil;
  235. include(flags,nf_error);
  236. codegenerror:=true;
  237. resulttype:=generrortype;
  238. end;
  239. function terrornode.pass_1 : tnode;
  240. begin
  241. result:=nil;
  242. expectloc:=LOC_VOID;
  243. codegenerror:=true;
  244. end;
  245. procedure terrornode.mark_write;
  246. begin
  247. end;
  248. {*****************************************************************************
  249. TSTATEMENTNODE
  250. *****************************************************************************}
  251. constructor tstatementnode.create(l,r : tnode);
  252. begin
  253. inherited create(statementn,l,r);
  254. end;
  255. function tstatementnode.det_resulttype:tnode;
  256. begin
  257. result:=nil;
  258. resulttype:=voidtype;
  259. { left is the statement itself calln assignn or a complex one }
  260. resulttypepass(left);
  261. if (not (cs_extsyntax in aktmoduleswitches)) and
  262. assigned(left.resulttype.def) and
  263. not((left.nodetype=calln) and
  264. { don't complain when funcretrefnode is set, because then the
  265. value is already used. And also not for constructors }
  266. (assigned(tcallnode(left).funcretnode) or
  267. (tcallnode(left).procdefinition.proctypeoption=potype_constructor))) and
  268. not(is_void(left.resulttype.def)) then
  269. CGMessage(parser_e_illegal_expression);
  270. if codegenerror then
  271. exit;
  272. { right is the next statement in the list }
  273. if assigned(right) then
  274. resulttypepass(right);
  275. if codegenerror then
  276. exit;
  277. end;
  278. function tstatementnode.pass_1 : tnode;
  279. begin
  280. result:=nil;
  281. { left is the statement itself calln assignn or a complex one }
  282. firstpass(left);
  283. if codegenerror then
  284. exit;
  285. expectloc:=left.expectloc;
  286. registersint:=left.registersint;
  287. registersfpu:=left.registersfpu;
  288. {$ifdef SUPPORT_MMX}
  289. registersmmx:=left.registersmmx;
  290. {$endif SUPPORT_MMX}
  291. { right is the next in the list }
  292. if assigned(right) then
  293. firstpass(right);
  294. if codegenerror then
  295. exit;
  296. end;
  297. procedure tstatementnode.printnodetree(var t:text);
  298. begin
  299. printnodelist(t);
  300. end;
  301. {*****************************************************************************
  302. TBLOCKNODE
  303. *****************************************************************************}
  304. constructor tblocknode.create(l : tnode);
  305. begin
  306. inherited create(blockn,l);
  307. end;
  308. destructor tblocknode.destroy;
  309. var
  310. hp, next: tstatementnode;
  311. begin
  312. hp := tstatementnode(left);
  313. left := nil;
  314. while assigned(hp) do
  315. begin
  316. next := tstatementnode(hp.right);
  317. hp.right := nil;
  318. hp.free;
  319. hp := next;
  320. end;
  321. inherited destroy;
  322. end;
  323. function tblocknode.det_resulttype:tnode;
  324. var
  325. hp : tstatementnode;
  326. begin
  327. result:=nil;
  328. resulttype:=voidtype;
  329. hp:=tstatementnode(left);
  330. while assigned(hp) do
  331. begin
  332. if assigned(hp.left) then
  333. begin
  334. codegenerror:=false;
  335. resulttypepass(hp.left);
  336. if not(codegenerror) and
  337. not(cs_extsyntax in aktmoduleswitches) and
  338. (hp.left.nodetype=calln) and
  339. not(is_void(hp.left.resulttype.def)) and
  340. not(cnf_return_value_used in tcallnode(hp.left).callnodeflags) and
  341. not((tcallnode(hp.left).procdefinition.proctypeoption=potype_constructor) and
  342. assigned(tprocdef(tcallnode(hp.left).procdefinition)._class) and
  343. is_object(tprocdef(tcallnode(hp.left).procdefinition)._class)) then
  344. CGMessagePos(hp.left.fileinfo,parser_e_illegal_expression);
  345. { the resulttype of the block is the last type that is
  346. returned. Normally this is a voidtype. But when the
  347. compiler inserts a block of multiple statements then the
  348. last entry can return a value }
  349. resulttype:=hp.left.resulttype;
  350. end;
  351. hp:=tstatementnode(hp.right);
  352. end;
  353. end;
  354. function tblocknode.pass_1 : tnode;
  355. var
  356. hp : tstatementnode;
  357. count : longint;
  358. begin
  359. result:=nil;
  360. expectloc:=LOC_VOID;
  361. count:=0;
  362. hp:=tstatementnode(left);
  363. while assigned(hp) do
  364. begin
  365. (*
  366. if cs_regvars in aktglobalswitches then
  367. begin
  368. { node transformations }
  369. { concat function result to exit }
  370. { this is wrong for string or other complex
  371. result types !!! }
  372. if {ret_in_acc(current_procinfo.procdef.rettype.def) and }
  373. (is_ordinal(current_procinfo.procdef.rettype.def) or
  374. is_smallset(current_procinfo.procdef.rettype.def)) and
  375. assigned(hp.right) and
  376. assigned(tstatementnode(hp.right).left) and
  377. (tstatementnode(hp.right).left.nodetype=exitn) and
  378. (hp.left.nodetype=assignn) and
  379. { !!!! this tbinarynode should be tassignmentnode }
  380. (tbinarynode(hp.left).left.nodetype=loadn) and
  381. (is_funcret_sym(tloadnode(tbinarynode(hp.left).left).symtableentry)) then
  382. begin
  383. if assigned(texitnode(tstatementnode(hp.right).left).left) then
  384. CGMessage(cg_n_inefficient_code)
  385. else
  386. begin
  387. texitnode(tstatementnode(hp.right).left).left:=tassignmentnode(hp.left).right;
  388. tassignmentnode(hp.left).right:=nil;
  389. hp.left.free;
  390. hp.left:=nil;
  391. end;
  392. end
  393. { warning if unreachable code occurs and elimate this }
  394. else if (hp.left.nodetype in
  395. [exitn,breakn,continuen,goton]) and
  396. { statement node (JM) }
  397. assigned(hp.right) and
  398. { kind of statement! (JM) }
  399. assigned(tstatementnode(hp.right).left) and
  400. (tstatementnode(hp.right).left.nodetype<>labeln) then
  401. begin
  402. { use correct line number }
  403. aktfilepos:=hp.right.fileinfo;
  404. hp.right.free;
  405. hp.right:=nil;
  406. CGMessage(cg_w_unreachable_code);
  407. { old lines }
  408. aktfilepos:=hp.left.fileinfo;
  409. end;
  410. end;
  411. *)
  412. if assigned(hp.left) then
  413. begin
  414. codegenerror:=false;
  415. firstpass(hp.left);
  416. hp.expectloc:=hp.left.expectloc;
  417. hp.registersint:=hp.left.registersint;
  418. hp.registersfpu:=hp.left.registersfpu;
  419. {$ifdef SUPPORT_MMX}
  420. hp.registersmmx:=hp.left.registersmmx;
  421. {$endif SUPPORT_MMX}
  422. end
  423. else
  424. hp.registersint:=0;
  425. if hp.registersint>registersint then
  426. registersint:=hp.registersint;
  427. if hp.registersfpu>registersfpu then
  428. registersfpu:=hp.registersfpu;
  429. {$ifdef SUPPORT_MMX}
  430. if hp.registersmmx>registersmmx then
  431. registersmmx:=hp.registersmmx;
  432. {$endif}
  433. expectloc:=hp.expectloc;
  434. inc(count);
  435. hp:=tstatementnode(hp.right);
  436. end;
  437. end;
  438. {$ifdef state_tracking}
  439. function Tblocknode.track_state_pass(exec_known:boolean):boolean;
  440. var hp:Tstatementnode;
  441. begin
  442. track_state_pass:=false;
  443. hp:=Tstatementnode(left);
  444. while assigned(hp) do
  445. begin
  446. if hp.left.track_state_pass(exec_known) then
  447. track_state_pass:=true;
  448. hp:=Tstatementnode(hp.right);
  449. end;
  450. end;
  451. {$endif state_tracking}
  452. {*****************************************************************************
  453. TASMNODE
  454. *****************************************************************************}
  455. constructor tasmnode.create(p : taasmoutput);
  456. begin
  457. inherited create(asmn);
  458. p_asm:=p;
  459. currenttai:=nil;
  460. used_regs_int:=[];
  461. used_regs_fpu:=[];
  462. end;
  463. constructor tasmnode.create_get_position;
  464. begin
  465. inherited create(asmn);
  466. p_asm:=nil;
  467. include(flags,nf_get_asm_position);
  468. currenttai:=nil;
  469. end;
  470. destructor tasmnode.destroy;
  471. begin
  472. if assigned(p_asm) then
  473. p_asm.free;
  474. inherited destroy;
  475. end;
  476. constructor tasmnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  477. var
  478. hp : tai;
  479. begin
  480. inherited ppuload(t,ppufile);
  481. if not(nf_get_asm_position in flags) then
  482. begin
  483. p_asm:=taasmoutput.create;
  484. repeat
  485. hp:=ppuloadai(ppufile);
  486. if hp=nil then
  487. break;
  488. p_asm.concat(hp);
  489. until false;
  490. end
  491. else
  492. p_asm:=nil;
  493. currenttai:=nil;
  494. end;
  495. procedure tasmnode.ppuwrite(ppufile:tcompilerppufile);
  496. var
  497. hp : tai;
  498. begin
  499. inherited ppuwrite(ppufile);
  500. {$warning FIXME Add saving of register sets}
  501. if not(nf_get_asm_position in flags) then
  502. begin
  503. hp:=tai(p_asm.first);
  504. while assigned(hp) do
  505. begin
  506. ppuwriteai(ppufile,hp);
  507. hp:=tai(hp.next);
  508. end;
  509. { end is marked by a nil }
  510. ppuwriteai(ppufile,nil);
  511. end;
  512. end;
  513. procedure tasmnode.buildderefimpl;
  514. var
  515. hp : tai;
  516. begin
  517. inherited buildderefimpl;
  518. if not(nf_get_asm_position in flags) then
  519. begin
  520. hp:=tai(p_asm.first);
  521. while assigned(hp) do
  522. begin
  523. hp.buildderefimpl;
  524. hp:=tai(hp.next);
  525. end;
  526. end;
  527. end;
  528. procedure tasmnode.derefimpl;
  529. var
  530. hp : tai;
  531. begin
  532. inherited derefimpl;
  533. if not(nf_get_asm_position in flags) then
  534. begin
  535. hp:=tai(p_asm.first);
  536. while assigned(hp) do
  537. begin
  538. hp.derefimpl;
  539. hp:=tai(hp.next);
  540. end;
  541. end;
  542. end;
  543. function tasmnode.getcopy: tnode;
  544. var
  545. n: tasmnode;
  546. begin
  547. n := tasmnode(inherited getcopy);
  548. if assigned(p_asm) then
  549. begin
  550. n.p_asm:=taasmoutput.create;
  551. n.p_asm.concatlistcopy(p_asm);
  552. end
  553. else n.p_asm := nil;
  554. n.currenttai:=currenttai;
  555. getcopy := n;
  556. end;
  557. function tasmnode.det_resulttype:tnode;
  558. begin
  559. result:=nil;
  560. resulttype:=voidtype;
  561. if not(nf_get_asm_position in flags) then
  562. include(current_procinfo.flags,pi_has_assembler_block);
  563. end;
  564. function tasmnode.pass_1 : tnode;
  565. begin
  566. result:=nil;
  567. expectloc:=LOC_VOID;
  568. end;
  569. function tasmnode.docompare(p: tnode): boolean;
  570. begin
  571. { comparing of asmlists is not implemented (JM) }
  572. docompare := false;
  573. end;
  574. {*****************************************************************************
  575. TEMPCREATENODE
  576. *****************************************************************************}
  577. constructor ttempcreatenode.create(const _restype: ttype; _size: longint; _temptype: ttemptype;allowreg:boolean);
  578. begin
  579. inherited create(tempcreaten);
  580. size := _size;
  581. new(tempinfo);
  582. fillchar(tempinfo^,sizeof(tempinfo^),0);
  583. tempinfo^.restype := _restype;
  584. tempinfo^.temptype := _temptype;
  585. tempinfo^.owner:=self;
  586. tempinfo^.may_be_in_reg:=
  587. allowreg and
  588. { temp must fit a single register }
  589. ((_restype.def.deftype = floatdef) or
  590. (_size<=TCGSize2Size[OS_INT])) and
  591. { size of register operations must be known }
  592. (def_cgsize(_restype.def)<>OS_NO) and
  593. { no init/final needed }
  594. not (_restype.def.needs_inittable) and
  595. ((_restype.def.deftype <> pointerdef) or
  596. (not tpointerdef(_restype.def).pointertype.def.needs_inittable));
  597. end;
  598. function ttempcreatenode.getcopy: tnode;
  599. var
  600. n: ttempcreatenode;
  601. begin
  602. n := ttempcreatenode(inherited getcopy);
  603. n.size := size;
  604. new(n.tempinfo);
  605. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  606. n.tempinfo^.owner:=n;
  607. n.tempinfo^.restype := tempinfo^.restype;
  608. n.tempinfo^.temptype := tempinfo^.temptype;
  609. { when the tempinfo has already a hookoncopy then it is not
  610. reset by a tempdeletenode }
  611. if assigned(tempinfo^.hookoncopy) then
  612. internalerror(200211262);
  613. { signal the temprefs that the temp they point to has been copied, }
  614. { so that if the refs get copied as well, they can hook themselves }
  615. { to the copy of the temp }
  616. tempinfo^.hookoncopy := n.tempinfo;
  617. tempinfo^.nextref_set_hookoncopy_nil := false;
  618. result := n;
  619. end;
  620. constructor ttempcreatenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  621. begin
  622. inherited ppuload(t,ppufile);
  623. size:=ppufile.getlongint;
  624. new(tempinfo);
  625. fillchar(tempinfo^,sizeof(tempinfo^),0);
  626. tempinfo^.may_be_in_reg:=boolean(ppufile.getbyte);
  627. ppufile.gettype(tempinfo^.restype);
  628. tempinfo^.temptype := ttemptype(ppufile.getbyte);
  629. tempinfo^.owner:=self;
  630. end;
  631. procedure ttempcreatenode.ppuwrite(ppufile:tcompilerppufile);
  632. begin
  633. inherited ppuwrite(ppufile);
  634. ppufile.putlongint(size);
  635. ppufile.putbyte(byte(tempinfo^.may_be_in_reg));
  636. ppufile.puttype(tempinfo^.restype);
  637. ppufile.putbyte(byte(tempinfo^.temptype));
  638. end;
  639. procedure ttempcreatenode.buildderefimpl;
  640. begin
  641. tempinfo^.restype.buildderef;
  642. end;
  643. procedure ttempcreatenode.derefimpl;
  644. begin
  645. tempinfo^.restype.resolve;
  646. end;
  647. function ttempcreatenode.pass_1 : tnode;
  648. begin
  649. result := nil;
  650. expectloc:=LOC_VOID;
  651. if (tempinfo^.restype.def.needs_inittable) then
  652. include(current_procinfo.flags,pi_needs_implicit_finally);
  653. end;
  654. function ttempcreatenode.det_resulttype: tnode;
  655. begin
  656. result := nil;
  657. { a tempcreatenode doesn't have a resulttype, only temprefnodes do }
  658. resulttype := voidtype;
  659. end;
  660. function ttempcreatenode.docompare(p: tnode): boolean;
  661. begin
  662. result :=
  663. inherited docompare(p) and
  664. (ttempcreatenode(p).size = size) and
  665. (ttempcreatenode(p).tempinfo^.may_be_in_reg = tempinfo^.may_be_in_reg) and
  666. equal_defs(ttempcreatenode(p).tempinfo^.restype.def,tempinfo^.restype.def);
  667. end;
  668. procedure ttempcreatenode.printnodedata(var t:text);
  669. begin
  670. inherited printnodedata(t);
  671. writeln(t,printnodeindention,'size = ',size);
  672. end;
  673. {*****************************************************************************
  674. TEMPREFNODE
  675. *****************************************************************************}
  676. constructor ttemprefnode.create(const temp: ttempcreatenode);
  677. begin
  678. inherited create(temprefn);
  679. tempinfo := temp.tempinfo;
  680. offset:=0;
  681. end;
  682. constructor ttemprefnode.create_offset(const temp: ttempcreatenode;aoffset:longint);
  683. begin
  684. self.create(temp);
  685. offset := aoffset;
  686. end;
  687. function ttemprefnode.getcopy: tnode;
  688. var
  689. n: ttemprefnode;
  690. begin
  691. n := ttemprefnode(inherited getcopy);
  692. n.offset := offset;
  693. if assigned(tempinfo^.hookoncopy) then
  694. { if the temp has been copied, assume it becomes a new }
  695. { temp which has to be hooked by the copied reference }
  696. begin
  697. { hook the ref to the copied temp }
  698. n.tempinfo := tempinfo^.hookoncopy;
  699. { if we passed a ttempdeletenode that changed the temp }
  700. { from a persistent one into a normal one, we must be }
  701. { the last reference (since our parent should free the }
  702. { temp (JM) }
  703. if (tempinfo^.nextref_set_hookoncopy_nil) then
  704. tempinfo^.hookoncopy := nil;
  705. end
  706. else
  707. { if the temp we refer to hasn't been copied, assume }
  708. { we're just a new reference to that temp }
  709. begin
  710. n.tempinfo := tempinfo;
  711. end;
  712. result := n;
  713. end;
  714. constructor ttemprefnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  715. begin
  716. inherited ppuload(t,ppufile);
  717. tempidx:=ppufile.getlongint;
  718. offset:=ppufile.getlongint;
  719. end;
  720. procedure ttemprefnode.ppuwrite(ppufile:tcompilerppufile);
  721. begin
  722. inherited ppuwrite(ppufile);
  723. ppufile.putlongint(tempinfo^.owner.ppuidx);
  724. ppufile.putlongint(offset);
  725. end;
  726. procedure ttemprefnode.derefnode;
  727. var
  728. temp : ttempcreatenode;
  729. begin
  730. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  731. if temp.nodetype<>tempcreaten then
  732. internalerror(200311075);
  733. tempinfo:=temp.tempinfo;
  734. end;
  735. function ttemprefnode.pass_1 : tnode;
  736. begin
  737. expectloc := LOC_REFERENCE;
  738. if tempinfo^.may_be_in_reg then
  739. begin
  740. if (tempinfo^.temptype = tt_persistent) then
  741. begin
  742. { !!tell rgobj this register is now a regvar, so it can't be freed!! }
  743. expectloc := LOC_CREGISTER
  744. end
  745. else
  746. expectloc := LOC_REGISTER;
  747. end;
  748. result := nil;
  749. end;
  750. function ttemprefnode.det_resulttype: tnode;
  751. begin
  752. { check if the temp is already resulttype passed }
  753. if not assigned(tempinfo^.restype.def) then
  754. internalerror(200108233);
  755. result := nil;
  756. resulttype := tempinfo^.restype;
  757. end;
  758. function ttemprefnode.docompare(p: tnode): boolean;
  759. begin
  760. result :=
  761. inherited docompare(p) and
  762. (ttemprefnode(p).tempinfo = tempinfo) and
  763. (ttemprefnode(p).offset = offset);
  764. end;
  765. procedure Ttemprefnode.mark_write;
  766. begin
  767. include(flags,nf_write);
  768. end;
  769. {*****************************************************************************
  770. TEMPDELETENODE
  771. *****************************************************************************}
  772. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  773. begin
  774. inherited create(tempdeleten);
  775. tempinfo := temp.tempinfo;
  776. release_to_normal := false;
  777. end;
  778. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  779. begin
  780. inherited create(tempdeleten);
  781. tempinfo := temp.tempinfo;
  782. release_to_normal := true;
  783. if tempinfo^.temptype <> tt_persistent then
  784. internalerror(200204211);
  785. end;
  786. function ttempdeletenode.getcopy: tnode;
  787. var
  788. n: ttempdeletenode;
  789. begin
  790. n := ttempdeletenode(inherited getcopy);
  791. n.release_to_normal := release_to_normal;
  792. if assigned(tempinfo^.hookoncopy) then
  793. { if the temp has been copied, assume it becomes a new }
  794. { temp which has to be hooked by the copied deletenode }
  795. begin
  796. { hook the tempdeletenode to the copied temp }
  797. n.tempinfo := tempinfo^.hookoncopy;
  798. { the temp shall not be used, reset hookoncopy }
  799. { Only if release_to_normal is false, otherwise }
  800. { the temp can still be referenced once more (JM) }
  801. if (not release_to_normal) then
  802. tempinfo^.hookoncopy:=nil
  803. else
  804. tempinfo^.nextref_set_hookoncopy_nil := true;
  805. end
  806. else
  807. { if the temp we refer to hasn't been copied, we have a }
  808. { problem since that means we now have two delete nodes }
  809. { for one temp }
  810. internalerror(200108234);
  811. result := n;
  812. end;
  813. constructor ttempdeletenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  814. begin
  815. inherited ppuload(t,ppufile);
  816. tempidx:=ppufile.getlongint;
  817. release_to_normal:=(ppufile.getbyte<>0);
  818. end;
  819. procedure ttempdeletenode.ppuwrite(ppufile:tcompilerppufile);
  820. begin
  821. inherited ppuwrite(ppufile);
  822. ppufile.putlongint(tempinfo^.owner.ppuidx);
  823. ppufile.putbyte(byte(release_to_normal));
  824. end;
  825. procedure ttempdeletenode.derefnode;
  826. var
  827. temp : ttempcreatenode;
  828. begin
  829. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  830. if temp.nodetype<>tempcreaten then
  831. internalerror(200311075);
  832. tempinfo:=temp.tempinfo;
  833. end;
  834. function ttempdeletenode.pass_1 : tnode;
  835. begin
  836. expectloc:=LOC_VOID;
  837. result := nil;
  838. end;
  839. function ttempdeletenode.det_resulttype: tnode;
  840. begin
  841. result := nil;
  842. resulttype := voidtype;
  843. end;
  844. function ttempdeletenode.docompare(p: tnode): boolean;
  845. begin
  846. result :=
  847. inherited docompare(p) and
  848. (ttemprefnode(p).tempinfo = tempinfo);
  849. end;
  850. destructor ttempdeletenode.destroy;
  851. begin
  852. dispose(tempinfo);
  853. end;
  854. begin
  855. cnothingnode:=tnothingnode;
  856. cerrornode:=terrornode;
  857. casmnode:=tasmnode;
  858. cstatementnode:=tstatementnode;
  859. cblocknode:=tblocknode;
  860. ctempcreatenode:=ttempcreatenode;
  861. ctemprefnode:=ttemprefnode;
  862. ctempdeletenode:=ttempdeletenode;
  863. end.
  864. {
  865. $Log$
  866. Revision 1.90 2004-11-21 17:54:59 peter
  867. * ttempcreatenode.create_reg merged into .create with parameter
  868. whether a register is allowed
  869. * funcret_paraloc renamed to funcretloc
  870. Revision 1.89 2004/10/31 21:45:03 peter
  871. * generic tlocation
  872. * move tlocation to cgutils
  873. Revision 1.88 2004/10/12 14:36:38 peter
  874. * gen high tree makes copy in temp when there is a calln
  875. Revision 1.87 2004/09/26 17:45:30 peter
  876. * simple regvar support, not yet finished
  877. Revision 1.86 2004/07/16 19:45:15 jonas
  878. + temps can now also hold fpu values in registers (take care with use,
  879. bacause of the x86 fpu stack)
  880. * fpu parameters to node-inlined procedures can now also be put in
  881. a register
  882. Revision 1.85 2004/06/20 08:55:29 florian
  883. * logs truncated
  884. Revision 1.84 2004/06/16 20:07:07 florian
  885. * dwarf branch merged
  886. Revision 1.83 2004/05/23 18:28:41 peter
  887. * methodpointer is loaded into a temp when it was a calln
  888. Revision 1.82 2004/05/23 15:06:20 peter
  889. * implicit_finally flag must be set in pass1
  890. * add check whether the implicit frame is generated when expected
  891. Revision 1.81.2.1 2004/04/28 19:55:51 peter
  892. * new warning for ordinal-pointer when size is different
  893. * fixed some cg_e_ messages to the correct section type_e_ or parser_e_
  894. Revision 1.81 2004/03/10 20:41:17 peter
  895. * maybe_in_reg moved to tempinfo
  896. * fixed expectloc for maybe_in_reg
  897. }