nbas.pas 33 KB

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