nbas.pas 34 KB

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