nbas.pas 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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. function laststatement(block:tblocknode):tstatementnode;
  176. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  177. implementation
  178. uses
  179. cutils,
  180. verbose,globals,globtype,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_reg(const _restype: ttype; _size: longint; _temptype: ttemptype);
  578. begin
  579. create(_restype,_size,_temptype);
  580. tempinfo^.may_be_in_reg:=
  581. { temp must fit a single register }
  582. ((_restype.def.deftype = floatdef) or
  583. (_size<=TCGSize2Size[OS_INT])) and
  584. { size of register operations must be known }
  585. (def_cgsize(_restype.def)<>OS_NO) and
  586. { no init/final needed }
  587. not (_restype.def.needs_inittable) and
  588. ((_restype.def.deftype <> pointerdef) or
  589. (not tpointerdef(_restype.def).pointertype.def.needs_inittable));
  590. end;
  591. constructor ttempcreatenode.create(const _restype: ttype; _size: longint; _temptype: ttemptype);
  592. begin
  593. inherited create(tempcreaten);
  594. size := _size;
  595. new(tempinfo);
  596. fillchar(tempinfo^,sizeof(tempinfo^),0);
  597. tempinfo^.restype := _restype;
  598. tempinfo^.temptype := _temptype;
  599. tempinfo^.owner:=self;
  600. end;
  601. function ttempcreatenode.getcopy: tnode;
  602. var
  603. n: ttempcreatenode;
  604. begin
  605. n := ttempcreatenode(inherited getcopy);
  606. n.size := size;
  607. new(n.tempinfo);
  608. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  609. n.tempinfo^.owner:=n;
  610. n.tempinfo^.restype := tempinfo^.restype;
  611. n.tempinfo^.temptype := tempinfo^.temptype;
  612. { when the tempinfo has already a hookoncopy then it is not
  613. reset by a tempdeletenode }
  614. if assigned(tempinfo^.hookoncopy) then
  615. internalerror(200211262);
  616. { signal the temprefs that the temp they point to has been copied, }
  617. { so that if the refs get copied as well, they can hook themselves }
  618. { to the copy of the temp }
  619. tempinfo^.hookoncopy := n.tempinfo;
  620. tempinfo^.nextref_set_hookoncopy_nil := false;
  621. result := n;
  622. end;
  623. constructor ttempcreatenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  624. begin
  625. inherited ppuload(t,ppufile);
  626. size:=ppufile.getlongint;
  627. new(tempinfo);
  628. fillchar(tempinfo^,sizeof(tempinfo^),0);
  629. tempinfo^.may_be_in_reg:=boolean(ppufile.getbyte);
  630. ppufile.gettype(tempinfo^.restype);
  631. tempinfo^.temptype := ttemptype(ppufile.getbyte);
  632. tempinfo^.owner:=self;
  633. end;
  634. procedure ttempcreatenode.ppuwrite(ppufile:tcompilerppufile);
  635. begin
  636. inherited ppuwrite(ppufile);
  637. ppufile.putlongint(size);
  638. ppufile.putbyte(byte(tempinfo^.may_be_in_reg));
  639. ppufile.puttype(tempinfo^.restype);
  640. ppufile.putbyte(byte(tempinfo^.temptype));
  641. end;
  642. procedure ttempcreatenode.buildderefimpl;
  643. begin
  644. tempinfo^.restype.buildderef;
  645. end;
  646. procedure ttempcreatenode.derefimpl;
  647. begin
  648. tempinfo^.restype.resolve;
  649. end;
  650. function ttempcreatenode.pass_1 : tnode;
  651. begin
  652. result := nil;
  653. expectloc:=LOC_VOID;
  654. if (tempinfo^.restype.def.needs_inittable) then
  655. include(current_procinfo.flags,pi_needs_implicit_finally);
  656. end;
  657. function ttempcreatenode.det_resulttype: tnode;
  658. begin
  659. result := nil;
  660. { a tempcreatenode doesn't have a resulttype, only temprefnodes do }
  661. resulttype := voidtype;
  662. end;
  663. function ttempcreatenode.docompare(p: tnode): boolean;
  664. begin
  665. result :=
  666. inherited docompare(p) and
  667. (ttempcreatenode(p).size = size) and
  668. (ttempcreatenode(p).tempinfo^.may_be_in_reg = tempinfo^.may_be_in_reg) and
  669. equal_defs(ttempcreatenode(p).tempinfo^.restype.def,tempinfo^.restype.def);
  670. end;
  671. procedure ttempcreatenode.printnodedata(var t:text);
  672. begin
  673. inherited printnodedata(t);
  674. writeln(t,printnodeindention,'size = ',size);
  675. end;
  676. {*****************************************************************************
  677. TEMPREFNODE
  678. *****************************************************************************}
  679. constructor ttemprefnode.create(const temp: ttempcreatenode);
  680. begin
  681. inherited create(temprefn);
  682. tempinfo := temp.tempinfo;
  683. offset:=0;
  684. end;
  685. constructor ttemprefnode.create_offset(const temp: ttempcreatenode;aoffset:longint);
  686. begin
  687. self.create(temp);
  688. offset := aoffset;
  689. end;
  690. function ttemprefnode.getcopy: tnode;
  691. var
  692. n: ttemprefnode;
  693. begin
  694. n := ttemprefnode(inherited getcopy);
  695. n.offset := offset;
  696. if assigned(tempinfo^.hookoncopy) then
  697. { if the temp has been copied, assume it becomes a new }
  698. { temp which has to be hooked by the copied reference }
  699. begin
  700. { hook the ref to the copied temp }
  701. n.tempinfo := tempinfo^.hookoncopy;
  702. { if we passed a ttempdeletenode that changed the temp }
  703. { from a persistent one into a normal one, we must be }
  704. { the last reference (since our parent should free the }
  705. { temp (JM) }
  706. if (tempinfo^.nextref_set_hookoncopy_nil) then
  707. tempinfo^.hookoncopy := nil;
  708. end
  709. else
  710. { if the temp we refer to hasn't been copied, assume }
  711. { we're just a new reference to that temp }
  712. begin
  713. n.tempinfo := tempinfo;
  714. end;
  715. result := n;
  716. end;
  717. constructor ttemprefnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  718. begin
  719. inherited ppuload(t,ppufile);
  720. tempidx:=ppufile.getlongint;
  721. offset:=ppufile.getlongint;
  722. end;
  723. procedure ttemprefnode.ppuwrite(ppufile:tcompilerppufile);
  724. begin
  725. inherited ppuwrite(ppufile);
  726. ppufile.putlongint(tempinfo^.owner.ppuidx);
  727. ppufile.putlongint(offset);
  728. end;
  729. procedure ttemprefnode.derefnode;
  730. var
  731. temp : ttempcreatenode;
  732. begin
  733. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  734. if temp.nodetype<>tempcreaten then
  735. internalerror(200311075);
  736. tempinfo:=temp.tempinfo;
  737. end;
  738. function ttemprefnode.pass_1 : tnode;
  739. begin
  740. expectloc := LOC_REFERENCE;
  741. if tempinfo^.may_be_in_reg then
  742. begin
  743. if (tempinfo^.temptype = tt_persistent) then
  744. begin
  745. { !!tell rgobj this register is now a regvar, so it can't be freed!! }
  746. expectloc := LOC_CREGISTER
  747. end
  748. else
  749. expectloc := LOC_REGISTER;
  750. end;
  751. result := nil;
  752. end;
  753. function ttemprefnode.det_resulttype: tnode;
  754. begin
  755. { check if the temp is already resulttype passed }
  756. if not assigned(tempinfo^.restype.def) then
  757. internalerror(200108233);
  758. result := nil;
  759. resulttype := tempinfo^.restype;
  760. end;
  761. function ttemprefnode.docompare(p: tnode): boolean;
  762. begin
  763. result :=
  764. inherited docompare(p) and
  765. (ttemprefnode(p).tempinfo = tempinfo) and
  766. (ttemprefnode(p).offset = offset);
  767. end;
  768. procedure Ttemprefnode.mark_write;
  769. begin
  770. include(flags,nf_write);
  771. end;
  772. {*****************************************************************************
  773. TEMPDELETENODE
  774. *****************************************************************************}
  775. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  776. begin
  777. inherited create(tempdeleten);
  778. tempinfo := temp.tempinfo;
  779. release_to_normal := false;
  780. end;
  781. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  782. begin
  783. inherited create(tempdeleten);
  784. tempinfo := temp.tempinfo;
  785. release_to_normal := true;
  786. if tempinfo^.temptype <> tt_persistent then
  787. internalerror(200204211);
  788. end;
  789. function ttempdeletenode.getcopy: tnode;
  790. var
  791. n: ttempdeletenode;
  792. begin
  793. n := ttempdeletenode(inherited getcopy);
  794. n.release_to_normal := release_to_normal;
  795. if assigned(tempinfo^.hookoncopy) then
  796. { if the temp has been copied, assume it becomes a new }
  797. { temp which has to be hooked by the copied deletenode }
  798. begin
  799. { hook the tempdeletenode to the copied temp }
  800. n.tempinfo := tempinfo^.hookoncopy;
  801. { the temp shall not be used, reset hookoncopy }
  802. { Only if release_to_normal is false, otherwise }
  803. { the temp can still be referenced once more (JM) }
  804. if (not release_to_normal) then
  805. tempinfo^.hookoncopy:=nil
  806. else
  807. tempinfo^.nextref_set_hookoncopy_nil := true;
  808. end
  809. else
  810. { if the temp we refer to hasn't been copied, we have a }
  811. { problem since that means we now have two delete nodes }
  812. { for one temp }
  813. internalerror(200108234);
  814. result := n;
  815. end;
  816. constructor ttempdeletenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  817. begin
  818. inherited ppuload(t,ppufile);
  819. tempidx:=ppufile.getlongint;
  820. release_to_normal:=(ppufile.getbyte<>0);
  821. end;
  822. procedure ttempdeletenode.ppuwrite(ppufile:tcompilerppufile);
  823. begin
  824. inherited ppuwrite(ppufile);
  825. ppufile.putlongint(tempinfo^.owner.ppuidx);
  826. ppufile.putbyte(byte(release_to_normal));
  827. end;
  828. procedure ttempdeletenode.derefnode;
  829. var
  830. temp : ttempcreatenode;
  831. begin
  832. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  833. if temp.nodetype<>tempcreaten then
  834. internalerror(200311075);
  835. tempinfo:=temp.tempinfo;
  836. end;
  837. function ttempdeletenode.pass_1 : tnode;
  838. begin
  839. expectloc:=LOC_VOID;
  840. result := nil;
  841. end;
  842. function ttempdeletenode.det_resulttype: tnode;
  843. begin
  844. result := nil;
  845. resulttype := voidtype;
  846. end;
  847. function ttempdeletenode.docompare(p: tnode): boolean;
  848. begin
  849. result :=
  850. inherited docompare(p) and
  851. (ttemprefnode(p).tempinfo = tempinfo);
  852. end;
  853. destructor ttempdeletenode.destroy;
  854. begin
  855. dispose(tempinfo);
  856. end;
  857. begin
  858. cnothingnode:=tnothingnode;
  859. cerrornode:=terrornode;
  860. casmnode:=tasmnode;
  861. cstatementnode:=tstatementnode;
  862. cblocknode:=tblocknode;
  863. ctempcreatenode:=ttempcreatenode;
  864. ctemprefnode:=ttemprefnode;
  865. ctempdeletenode:=ttempdeletenode;
  866. end.
  867. {
  868. $Log$
  869. Revision 1.88 2004-10-12 14:36:38 peter
  870. * gen high tree makes copy in temp when there is a calln
  871. Revision 1.87 2004/09/26 17:45:30 peter
  872. * simple regvar support, not yet finished
  873. Revision 1.86 2004/07/16 19:45:15 jonas
  874. + temps can now also hold fpu values in registers (take care with use,
  875. bacause of the x86 fpu stack)
  876. * fpu parameters to node-inlined procedures can now also be put in
  877. a register
  878. Revision 1.85 2004/06/20 08:55:29 florian
  879. * logs truncated
  880. Revision 1.84 2004/06/16 20:07:07 florian
  881. * dwarf branch merged
  882. Revision 1.83 2004/05/23 18:28:41 peter
  883. * methodpointer is loaded into a temp when it was a calln
  884. Revision 1.82 2004/05/23 15:06:20 peter
  885. * implicit_finally flag must be set in pass1
  886. * add check whether the implicit frame is generated when expected
  887. Revision 1.81.2.1 2004/04/28 19:55:51 peter
  888. * new warning for ordinal-pointer when size is different
  889. * fixed some cg_e_ messages to the correct section type_e_ or parser_e_
  890. Revision 1.81 2004/03/10 20:41:17 peter
  891. * maybe_in_reg moved to tempinfo
  892. * fixed expectloc for maybe_in_reg
  893. }