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); virtual;
  112. constructor create_reg(const _restype: ttype; _size: longint; _temptype: ttemptype); virtual;
  113. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  114. procedure ppuwrite(ppufile:tcompilerppufile);override;
  115. procedure buildderefimpl;override;
  116. procedure derefimpl;override;
  117. function getcopy: tnode; override;
  118. function pass_1 : tnode; override;
  119. function det_resulttype: tnode; override;
  120. function docompare(p: tnode): boolean; override;
  121. procedure printnodedata(var t:text);override;
  122. end;
  123. ttempcreatenodeclass = class of ttempcreatenode;
  124. { a node which is a reference to a certain temp }
  125. ttemprefnode = class(tnode)
  126. constructor create(const temp: ttempcreatenode); virtual;
  127. constructor create_offset(const temp: ttempcreatenode;aoffset:longint);
  128. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  129. procedure ppuwrite(ppufile:tcompilerppufile);override;
  130. function getcopy: tnode; override;
  131. procedure derefnode;override;
  132. function pass_1 : tnode; override;
  133. function det_resulttype : tnode; override;
  134. procedure mark_write;override;
  135. function docompare(p: tnode): boolean; override;
  136. protected
  137. tempinfo: ptempinfo;
  138. offset : longint;
  139. private
  140. tempidx : longint;
  141. end;
  142. ttemprefnodeclass = class of ttemprefnode;
  143. { a node which removes a temp }
  144. ttempdeletenode = class(tnode)
  145. constructor create(const temp: ttempcreatenode); virtual;
  146. { this will convert the persistant temp to a normal temp
  147. for returning to the other nodes }
  148. constructor create_normal_temp(const temp: ttempcreatenode);
  149. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  150. procedure ppuwrite(ppufile:tcompilerppufile);override;
  151. function getcopy: tnode; override;
  152. procedure derefnode;override;
  153. function pass_1: tnode; override;
  154. function det_resulttype: tnode; override;
  155. function docompare(p: tnode): boolean; override;
  156. destructor destroy; override;
  157. protected
  158. tempinfo: ptempinfo;
  159. release_to_normal : boolean;
  160. private
  161. tempidx : longint;
  162. end;
  163. ttempdeletenodeclass = class of ttempdeletenode;
  164. var
  165. cnothingnode : tnothingnodeclass;
  166. cerrornode : terrornodeclass;
  167. casmnode : tasmnodeclass;
  168. cstatementnode : tstatementnodeclass;
  169. cblocknode : tblocknodeclass;
  170. ctempcreatenode : ttempcreatenodeclass;
  171. ctemprefnode : ttemprefnodeclass;
  172. ctempdeletenode : ttempdeletenodeclass;
  173. { Create a blocknode and statement node for multiple statements
  174. generated internally by the parser }
  175. function internalstatements(var laststatement:tstatementnode):tblocknode;
  176. function laststatement(block:tblocknode):tstatementnode;
  177. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  178. implementation
  179. uses
  180. cutils,
  181. verbose,globals,systems,
  182. symconst,symdef,defutil,defcmp,
  183. pass_1,
  184. nld,ncal,nflw,
  185. procinfo
  186. ;
  187. {*****************************************************************************
  188. Helpers
  189. *****************************************************************************}
  190. function internalstatements(var laststatement:tstatementnode):tblocknode;
  191. begin
  192. { create dummy initial statement }
  193. laststatement := cstatementnode.create(cnothingnode.create,nil);
  194. internalstatements := cblocknode.create(laststatement);
  195. end;
  196. function laststatement(block:tblocknode):tstatementnode;
  197. begin
  198. result:=tstatementnode(block.left);
  199. while assigned(result) do
  200. result:=tstatementnode(result.right);
  201. end;
  202. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  203. begin
  204. if assigned(laststatement.right) then
  205. internalerror(200204201);
  206. laststatement.right:=cstatementnode.create(n,nil);
  207. laststatement:=tstatementnode(laststatement.right);
  208. end;
  209. {*****************************************************************************
  210. TFIRSTNOTHING
  211. *****************************************************************************}
  212. constructor tnothingnode.create;
  213. begin
  214. inherited create(nothingn);
  215. end;
  216. function tnothingnode.det_resulttype:tnode;
  217. begin
  218. result:=nil;
  219. resulttype:=voidtype;
  220. end;
  221. function tnothingnode.pass_1 : tnode;
  222. begin
  223. result:=nil;
  224. expectloc:=LOC_VOID;
  225. end;
  226. {*****************************************************************************
  227. TFIRSTERROR
  228. *****************************************************************************}
  229. constructor terrornode.create;
  230. begin
  231. inherited create(errorn);
  232. end;
  233. function terrornode.det_resulttype:tnode;
  234. begin
  235. result:=nil;
  236. include(flags,nf_error);
  237. codegenerror:=true;
  238. resulttype:=generrortype;
  239. end;
  240. function terrornode.pass_1 : tnode;
  241. begin
  242. result:=nil;
  243. expectloc:=LOC_VOID;
  244. codegenerror:=true;
  245. end;
  246. procedure terrornode.mark_write;
  247. begin
  248. end;
  249. {*****************************************************************************
  250. TSTATEMENTNODE
  251. *****************************************************************************}
  252. constructor tstatementnode.create(l,r : tnode);
  253. begin
  254. inherited create(statementn,l,r);
  255. end;
  256. function tstatementnode.det_resulttype:tnode;
  257. begin
  258. result:=nil;
  259. resulttype:=voidtype;
  260. { left is the statement itself calln assignn or a complex one }
  261. resulttypepass(left);
  262. if (not (cs_extsyntax in aktmoduleswitches)) and
  263. assigned(left.resulttype.def) and
  264. not((left.nodetype=calln) and
  265. { don't complain when funcretrefnode is set, because then the
  266. value is already used. And also not for constructors }
  267. (assigned(tcallnode(left).funcretnode) or
  268. (tcallnode(left).procdefinition.proctypeoption=potype_constructor))) and
  269. not(is_void(left.resulttype.def)) then
  270. CGMessage(parser_e_illegal_expression);
  271. if codegenerror then
  272. exit;
  273. { right is the next statement in the list }
  274. if assigned(right) then
  275. resulttypepass(right);
  276. if codegenerror then
  277. exit;
  278. end;
  279. function tstatementnode.pass_1 : tnode;
  280. begin
  281. result:=nil;
  282. { left is the statement itself calln assignn or a complex one }
  283. firstpass(left);
  284. if codegenerror then
  285. exit;
  286. expectloc:=left.expectloc;
  287. registersint:=left.registersint;
  288. registersfpu:=left.registersfpu;
  289. {$ifdef SUPPORT_MMX}
  290. registersmmx:=left.registersmmx;
  291. {$endif SUPPORT_MMX}
  292. { right is the next in the list }
  293. if assigned(right) then
  294. firstpass(right);
  295. if codegenerror then
  296. exit;
  297. end;
  298. procedure tstatementnode.printnodetree(var t:text);
  299. begin
  300. printnodelist(t);
  301. end;
  302. {*****************************************************************************
  303. TBLOCKNODE
  304. *****************************************************************************}
  305. constructor tblocknode.create(l : tnode);
  306. begin
  307. inherited create(blockn,l);
  308. end;
  309. destructor tblocknode.destroy;
  310. var
  311. hp, next: tstatementnode;
  312. begin
  313. hp := tstatementnode(left);
  314. left := nil;
  315. while assigned(hp) do
  316. begin
  317. next := tstatementnode(hp.right);
  318. hp.right := nil;
  319. hp.free;
  320. hp := next;
  321. end;
  322. inherited destroy;
  323. end;
  324. function tblocknode.det_resulttype:tnode;
  325. var
  326. hp : tstatementnode;
  327. begin
  328. result:=nil;
  329. resulttype:=voidtype;
  330. hp:=tstatementnode(left);
  331. while assigned(hp) do
  332. begin
  333. if assigned(hp.left) then
  334. begin
  335. codegenerror:=false;
  336. resulttypepass(hp.left);
  337. if not(codegenerror) and
  338. not(cs_extsyntax in aktmoduleswitches) and
  339. (hp.left.nodetype=calln) and
  340. not(is_void(hp.left.resulttype.def)) and
  341. not(cnf_return_value_used in tcallnode(hp.left).callnodeflags) and
  342. not((tcallnode(hp.left).procdefinition.proctypeoption=potype_constructor) and
  343. assigned(tprocdef(tcallnode(hp.left).procdefinition)._class) and
  344. is_object(tprocdef(tcallnode(hp.left).procdefinition)._class)) then
  345. CGMessagePos(hp.left.fileinfo,parser_e_illegal_expression);
  346. { the resulttype of the block is the last type that is
  347. returned. Normally this is a voidtype. But when the
  348. compiler inserts a block of multiple statements then the
  349. last entry can return a value }
  350. resulttype:=hp.left.resulttype;
  351. end;
  352. hp:=tstatementnode(hp.right);
  353. end;
  354. end;
  355. function tblocknode.pass_1 : tnode;
  356. var
  357. hp : tstatementnode;
  358. count : longint;
  359. begin
  360. result:=nil;
  361. expectloc:=LOC_VOID;
  362. count:=0;
  363. hp:=tstatementnode(left);
  364. while assigned(hp) do
  365. begin
  366. (*
  367. if cs_regvars in aktglobalswitches then
  368. begin
  369. { node transformations }
  370. { concat function result to exit }
  371. { this is wrong for string or other complex
  372. result types !!! }
  373. if {ret_in_acc(current_procinfo.procdef.rettype.def) and }
  374. (is_ordinal(current_procinfo.procdef.rettype.def) or
  375. is_smallset(current_procinfo.procdef.rettype.def)) and
  376. assigned(hp.right) and
  377. assigned(tstatementnode(hp.right).left) and
  378. (tstatementnode(hp.right).left.nodetype=exitn) and
  379. (hp.left.nodetype=assignn) and
  380. { !!!! this tbinarynode should be tassignmentnode }
  381. (tbinarynode(hp.left).left.nodetype=loadn) and
  382. (is_funcret_sym(tloadnode(tbinarynode(hp.left).left).symtableentry)) then
  383. begin
  384. if assigned(texitnode(tstatementnode(hp.right).left).left) then
  385. CGMessage(cg_n_inefficient_code)
  386. else
  387. begin
  388. texitnode(tstatementnode(hp.right).left).left:=tassignmentnode(hp.left).right;
  389. tassignmentnode(hp.left).right:=nil;
  390. hp.left.free;
  391. hp.left:=nil;
  392. end;
  393. end
  394. { warning if unreachable code occurs and elimate this }
  395. else if (hp.left.nodetype in
  396. [exitn,breakn,continuen,goton]) and
  397. { statement node (JM) }
  398. assigned(hp.right) and
  399. { kind of statement! (JM) }
  400. assigned(tstatementnode(hp.right).left) and
  401. (tstatementnode(hp.right).left.nodetype<>labeln) then
  402. begin
  403. { use correct line number }
  404. aktfilepos:=hp.right.fileinfo;
  405. hp.right.free;
  406. hp.right:=nil;
  407. CGMessage(cg_w_unreachable_code);
  408. { old lines }
  409. aktfilepos:=hp.left.fileinfo;
  410. end;
  411. end;
  412. *)
  413. if assigned(hp.left) then
  414. begin
  415. codegenerror:=false;
  416. firstpass(hp.left);
  417. hp.expectloc:=hp.left.expectloc;
  418. hp.registersint:=hp.left.registersint;
  419. hp.registersfpu:=hp.left.registersfpu;
  420. {$ifdef SUPPORT_MMX}
  421. hp.registersmmx:=hp.left.registersmmx;
  422. {$endif SUPPORT_MMX}
  423. end
  424. else
  425. hp.registersint:=0;
  426. if hp.registersint>registersint then
  427. registersint:=hp.registersint;
  428. if hp.registersfpu>registersfpu then
  429. registersfpu:=hp.registersfpu;
  430. {$ifdef SUPPORT_MMX}
  431. if hp.registersmmx>registersmmx then
  432. registersmmx:=hp.registersmmx;
  433. {$endif}
  434. expectloc:=hp.expectloc;
  435. inc(count);
  436. hp:=tstatementnode(hp.right);
  437. end;
  438. end;
  439. {$ifdef state_tracking}
  440. function Tblocknode.track_state_pass(exec_known:boolean):boolean;
  441. var hp:Tstatementnode;
  442. begin
  443. track_state_pass:=false;
  444. hp:=Tstatementnode(left);
  445. while assigned(hp) do
  446. begin
  447. if hp.left.track_state_pass(exec_known) then
  448. track_state_pass:=true;
  449. hp:=Tstatementnode(hp.right);
  450. end;
  451. end;
  452. {$endif state_tracking}
  453. {*****************************************************************************
  454. TASMNODE
  455. *****************************************************************************}
  456. constructor tasmnode.create(p : taasmoutput);
  457. begin
  458. inherited create(asmn);
  459. p_asm:=p;
  460. currenttai:=nil;
  461. used_regs_int:=[];
  462. used_regs_fpu:=[];
  463. end;
  464. constructor tasmnode.create_get_position;
  465. begin
  466. inherited create(asmn);
  467. p_asm:=nil;
  468. include(flags,nf_get_asm_position);
  469. currenttai:=nil;
  470. end;
  471. destructor tasmnode.destroy;
  472. begin
  473. if assigned(p_asm) then
  474. p_asm.free;
  475. inherited destroy;
  476. end;
  477. constructor tasmnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  478. var
  479. hp : tai;
  480. begin
  481. inherited ppuload(t,ppufile);
  482. if not(nf_get_asm_position in flags) then
  483. begin
  484. p_asm:=taasmoutput.create;
  485. repeat
  486. hp:=ppuloadai(ppufile);
  487. if hp=nil then
  488. break;
  489. p_asm.concat(hp);
  490. until false;
  491. end
  492. else
  493. p_asm:=nil;
  494. currenttai:=nil;
  495. end;
  496. procedure tasmnode.ppuwrite(ppufile:tcompilerppufile);
  497. var
  498. hp : tai;
  499. begin
  500. inherited ppuwrite(ppufile);
  501. {$warning FIXME Add saving of register sets}
  502. if not(nf_get_asm_position in flags) then
  503. begin
  504. hp:=tai(p_asm.first);
  505. while assigned(hp) do
  506. begin
  507. ppuwriteai(ppufile,hp);
  508. hp:=tai(hp.next);
  509. end;
  510. { end is marked by a nil }
  511. ppuwriteai(ppufile,nil);
  512. end;
  513. end;
  514. procedure tasmnode.buildderefimpl;
  515. var
  516. hp : tai;
  517. begin
  518. inherited buildderefimpl;
  519. if not(nf_get_asm_position in flags) then
  520. begin
  521. hp:=tai(p_asm.first);
  522. while assigned(hp) do
  523. begin
  524. hp.buildderefimpl;
  525. hp:=tai(hp.next);
  526. end;
  527. end;
  528. end;
  529. procedure tasmnode.derefimpl;
  530. var
  531. hp : tai;
  532. begin
  533. inherited derefimpl;
  534. if not(nf_get_asm_position in flags) then
  535. begin
  536. hp:=tai(p_asm.first);
  537. while assigned(hp) do
  538. begin
  539. hp.derefimpl;
  540. hp:=tai(hp.next);
  541. end;
  542. end;
  543. end;
  544. function tasmnode.getcopy: tnode;
  545. var
  546. n: tasmnode;
  547. begin
  548. n := tasmnode(inherited getcopy);
  549. if assigned(p_asm) then
  550. begin
  551. n.p_asm:=taasmoutput.create;
  552. n.p_asm.concatlistcopy(p_asm);
  553. end
  554. else n.p_asm := nil;
  555. n.currenttai:=currenttai;
  556. getcopy := n;
  557. end;
  558. function tasmnode.det_resulttype:tnode;
  559. begin
  560. result:=nil;
  561. resulttype:=voidtype;
  562. if not(nf_get_asm_position in flags) then
  563. include(current_procinfo.flags,pi_has_assembler_block);
  564. end;
  565. function tasmnode.pass_1 : tnode;
  566. begin
  567. result:=nil;
  568. expectloc:=LOC_VOID;
  569. end;
  570. function tasmnode.docompare(p: tnode): boolean;
  571. begin
  572. { comparing of asmlists is not implemented (JM) }
  573. docompare := false;
  574. end;
  575. {*****************************************************************************
  576. TEMPCREATENODE
  577. *****************************************************************************}
  578. constructor ttempcreatenode.create_reg(const _restype: ttype; _size: longint; _temptype: ttemptype);
  579. begin
  580. create(_restype,_size,_temptype);
  581. tempinfo^.may_be_in_reg:=
  582. { temp must fit a single register }
  583. ((_restype.def.deftype = floatdef) or
  584. (_size<=TCGSize2Size[OS_INT])) and
  585. { size of register operations must be known }
  586. (def_cgsize(_restype.def)<>OS_NO) and
  587. { no init/final needed }
  588. not (_restype.def.needs_inittable) and
  589. ((_restype.def.deftype <> pointerdef) or
  590. (not tpointerdef(_restype.def).pointertype.def.needs_inittable));
  591. end;
  592. constructor ttempcreatenode.create(const _restype: ttype; _size: longint; _temptype: ttemptype);
  593. begin
  594. inherited create(tempcreaten);
  595. size := _size;
  596. new(tempinfo);
  597. fillchar(tempinfo^,sizeof(tempinfo^),0);
  598. tempinfo^.restype := _restype;
  599. tempinfo^.temptype := _temptype;
  600. tempinfo^.owner:=self;
  601. end;
  602. function ttempcreatenode.getcopy: tnode;
  603. var
  604. n: ttempcreatenode;
  605. begin
  606. n := ttempcreatenode(inherited getcopy);
  607. n.size := size;
  608. new(n.tempinfo);
  609. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  610. n.tempinfo^.owner:=n;
  611. n.tempinfo^.restype := tempinfo^.restype;
  612. n.tempinfo^.temptype := tempinfo^.temptype;
  613. { when the tempinfo has already a hookoncopy then it is not
  614. reset by a tempdeletenode }
  615. if assigned(tempinfo^.hookoncopy) then
  616. internalerror(200211262);
  617. { signal the temprefs that the temp they point to has been copied, }
  618. { so that if the refs get copied as well, they can hook themselves }
  619. { to the copy of the temp }
  620. tempinfo^.hookoncopy := n.tempinfo;
  621. tempinfo^.nextref_set_hookoncopy_nil := false;
  622. result := n;
  623. end;
  624. constructor ttempcreatenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  625. begin
  626. inherited ppuload(t,ppufile);
  627. size:=ppufile.getlongint;
  628. new(tempinfo);
  629. fillchar(tempinfo^,sizeof(tempinfo^),0);
  630. tempinfo^.may_be_in_reg:=boolean(ppufile.getbyte);
  631. ppufile.gettype(tempinfo^.restype);
  632. tempinfo^.temptype := ttemptype(ppufile.getbyte);
  633. tempinfo^.owner:=self;
  634. end;
  635. procedure ttempcreatenode.ppuwrite(ppufile:tcompilerppufile);
  636. begin
  637. inherited ppuwrite(ppufile);
  638. ppufile.putlongint(size);
  639. ppufile.putbyte(byte(tempinfo^.may_be_in_reg));
  640. ppufile.puttype(tempinfo^.restype);
  641. ppufile.putbyte(byte(tempinfo^.temptype));
  642. end;
  643. procedure ttempcreatenode.buildderefimpl;
  644. begin
  645. tempinfo^.restype.buildderef;
  646. end;
  647. procedure ttempcreatenode.derefimpl;
  648. begin
  649. tempinfo^.restype.resolve;
  650. end;
  651. function ttempcreatenode.pass_1 : tnode;
  652. begin
  653. result := nil;
  654. expectloc:=LOC_VOID;
  655. if (tempinfo^.restype.def.needs_inittable) then
  656. include(current_procinfo.flags,pi_needs_implicit_finally);
  657. end;
  658. function ttempcreatenode.det_resulttype: tnode;
  659. begin
  660. result := nil;
  661. { a tempcreatenode doesn't have a resulttype, only temprefnodes do }
  662. resulttype := voidtype;
  663. end;
  664. function ttempcreatenode.docompare(p: tnode): boolean;
  665. begin
  666. result :=
  667. inherited docompare(p) and
  668. (ttempcreatenode(p).size = size) and
  669. (ttempcreatenode(p).tempinfo^.may_be_in_reg = tempinfo^.may_be_in_reg) and
  670. equal_defs(ttempcreatenode(p).tempinfo^.restype.def,tempinfo^.restype.def);
  671. end;
  672. procedure ttempcreatenode.printnodedata(var t:text);
  673. begin
  674. inherited printnodedata(t);
  675. writeln(t,printnodeindention,'size = ',size);
  676. end;
  677. {*****************************************************************************
  678. TEMPREFNODE
  679. *****************************************************************************}
  680. constructor ttemprefnode.create(const temp: ttempcreatenode);
  681. begin
  682. inherited create(temprefn);
  683. tempinfo := temp.tempinfo;
  684. offset:=0;
  685. end;
  686. constructor ttemprefnode.create_offset(const temp: ttempcreatenode;aoffset:longint);
  687. begin
  688. self.create(temp);
  689. offset := aoffset;
  690. end;
  691. function ttemprefnode.getcopy: tnode;
  692. var
  693. n: ttemprefnode;
  694. begin
  695. n := ttemprefnode(inherited getcopy);
  696. n.offset := offset;
  697. if assigned(tempinfo^.hookoncopy) then
  698. { if the temp has been copied, assume it becomes a new }
  699. { temp which has to be hooked by the copied reference }
  700. begin
  701. { hook the ref to the copied temp }
  702. n.tempinfo := tempinfo^.hookoncopy;
  703. { if we passed a ttempdeletenode that changed the temp }
  704. { from a persistent one into a normal one, we must be }
  705. { the last reference (since our parent should free the }
  706. { temp (JM) }
  707. if (tempinfo^.nextref_set_hookoncopy_nil) then
  708. tempinfo^.hookoncopy := nil;
  709. end
  710. else
  711. { if the temp we refer to hasn't been copied, assume }
  712. { we're just a new reference to that temp }
  713. begin
  714. n.tempinfo := tempinfo;
  715. end;
  716. result := n;
  717. end;
  718. constructor ttemprefnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  719. begin
  720. inherited ppuload(t,ppufile);
  721. tempidx:=ppufile.getlongint;
  722. offset:=ppufile.getlongint;
  723. end;
  724. procedure ttemprefnode.ppuwrite(ppufile:tcompilerppufile);
  725. begin
  726. inherited ppuwrite(ppufile);
  727. ppufile.putlongint(tempinfo^.owner.ppuidx);
  728. ppufile.putlongint(offset);
  729. end;
  730. procedure ttemprefnode.derefnode;
  731. var
  732. temp : ttempcreatenode;
  733. begin
  734. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  735. if temp.nodetype<>tempcreaten then
  736. internalerror(200311075);
  737. tempinfo:=temp.tempinfo;
  738. end;
  739. function ttemprefnode.pass_1 : tnode;
  740. begin
  741. expectloc := LOC_REFERENCE;
  742. if tempinfo^.may_be_in_reg then
  743. begin
  744. if (tempinfo^.temptype = tt_persistent) then
  745. begin
  746. { !!tell rgobj this register is now a regvar, so it can't be freed!! }
  747. expectloc := LOC_CREGISTER
  748. end
  749. else
  750. expectloc := LOC_REGISTER;
  751. end;
  752. result := nil;
  753. end;
  754. function ttemprefnode.det_resulttype: tnode;
  755. begin
  756. { check if the temp is already resulttype passed }
  757. if not assigned(tempinfo^.restype.def) then
  758. internalerror(200108233);
  759. result := nil;
  760. resulttype := tempinfo^.restype;
  761. end;
  762. function ttemprefnode.docompare(p: tnode): boolean;
  763. begin
  764. result :=
  765. inherited docompare(p) and
  766. (ttemprefnode(p).tempinfo = tempinfo) and
  767. (ttemprefnode(p).offset = offset);
  768. end;
  769. procedure Ttemprefnode.mark_write;
  770. begin
  771. include(flags,nf_write);
  772. end;
  773. {*****************************************************************************
  774. TEMPDELETENODE
  775. *****************************************************************************}
  776. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  777. begin
  778. inherited create(tempdeleten);
  779. tempinfo := temp.tempinfo;
  780. release_to_normal := false;
  781. end;
  782. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  783. begin
  784. inherited create(tempdeleten);
  785. tempinfo := temp.tempinfo;
  786. release_to_normal := true;
  787. if tempinfo^.temptype <> tt_persistent then
  788. internalerror(200204211);
  789. end;
  790. function ttempdeletenode.getcopy: tnode;
  791. var
  792. n: ttempdeletenode;
  793. begin
  794. n := ttempdeletenode(inherited getcopy);
  795. n.release_to_normal := release_to_normal;
  796. if assigned(tempinfo^.hookoncopy) then
  797. { if the temp has been copied, assume it becomes a new }
  798. { temp which has to be hooked by the copied deletenode }
  799. begin
  800. { hook the tempdeletenode to the copied temp }
  801. n.tempinfo := tempinfo^.hookoncopy;
  802. { the temp shall not be used, reset hookoncopy }
  803. { Only if release_to_normal is false, otherwise }
  804. { the temp can still be referenced once more (JM) }
  805. if (not release_to_normal) then
  806. tempinfo^.hookoncopy:=nil
  807. else
  808. tempinfo^.nextref_set_hookoncopy_nil := true;
  809. end
  810. else
  811. { if the temp we refer to hasn't been copied, we have a }
  812. { problem since that means we now have two delete nodes }
  813. { for one temp }
  814. internalerror(200108234);
  815. result := n;
  816. end;
  817. constructor ttempdeletenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  818. begin
  819. inherited ppuload(t,ppufile);
  820. tempidx:=ppufile.getlongint;
  821. release_to_normal:=(ppufile.getbyte<>0);
  822. end;
  823. procedure ttempdeletenode.ppuwrite(ppufile:tcompilerppufile);
  824. begin
  825. inherited ppuwrite(ppufile);
  826. ppufile.putlongint(tempinfo^.owner.ppuidx);
  827. ppufile.putbyte(byte(release_to_normal));
  828. end;
  829. procedure ttempdeletenode.derefnode;
  830. var
  831. temp : ttempcreatenode;
  832. begin
  833. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  834. if temp.nodetype<>tempcreaten then
  835. internalerror(200311075);
  836. tempinfo:=temp.tempinfo;
  837. end;
  838. function ttempdeletenode.pass_1 : tnode;
  839. begin
  840. expectloc:=LOC_VOID;
  841. result := nil;
  842. end;
  843. function ttempdeletenode.det_resulttype: tnode;
  844. begin
  845. result := nil;
  846. resulttype := voidtype;
  847. end;
  848. function ttempdeletenode.docompare(p: tnode): boolean;
  849. begin
  850. result :=
  851. inherited docompare(p) and
  852. (ttemprefnode(p).tempinfo = tempinfo);
  853. end;
  854. destructor ttempdeletenode.destroy;
  855. begin
  856. dispose(tempinfo);
  857. end;
  858. begin
  859. cnothingnode:=tnothingnode;
  860. cerrornode:=terrornode;
  861. casmnode:=tasmnode;
  862. cstatementnode:=tstatementnode;
  863. cblocknode:=tblocknode;
  864. ctempcreatenode:=ttempcreatenode;
  865. ctemprefnode:=ttemprefnode;
  866. ctempdeletenode:=ttempdeletenode;
  867. end.
  868. {
  869. $Log$
  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. }