nbas.pas 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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. 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. getposition : boolean;
  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. 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. may_be_in_reg: boolean;
  104. { * persistent temps are used in manually written code where the temp }
  105. { be usable among different statements and where you can manually say }
  106. { when the temp has to be freed (using a ttempdeletenode) }
  107. { * non-persistent temps are mostly used in typeconversion helpers, }
  108. { where the node that receives the temp becomes responsible for }
  109. { freeing it. In this last case, you must use only one reference }
  110. { to it and *not* generate a ttempdeletenode }
  111. constructor create(const _restype: ttype; _size: longint; _temptype: ttemptype); virtual;
  112. constructor create_reg(const _restype: ttype; _size: longint; _temptype: ttemptype); virtual;
  113. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  114. procedure ppuwrite(ppufile:tcompilerppufile);override;
  115. procedure buildderefimpl;override;
  116. procedure derefimpl;override;
  117. function getcopy: tnode; override;
  118. function pass_1 : tnode; override;
  119. function det_resulttype: tnode; override;
  120. function docompare(p: tnode): boolean; override;
  121. procedure printnodedata(var t:text);override;
  122. end;
  123. ttempcreatenodeclass = class of ttempcreatenode;
  124. { a node which is a reference to a certain temp }
  125. ttemprefnode = class(tnode)
  126. constructor create(const temp: ttempcreatenode); virtual;
  127. constructor create_offset(const temp: ttempcreatenode;aoffset:longint);
  128. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  129. procedure ppuwrite(ppufile:tcompilerppufile);override;
  130. function getcopy: tnode; override;
  131. procedure derefnode;override;
  132. function pass_1 : tnode; override;
  133. function det_resulttype : tnode; override;
  134. procedure mark_write;override;
  135. function docompare(p: tnode): boolean; override;
  136. protected
  137. tempinfo: ptempinfo;
  138. offset : longint;
  139. private
  140. tempidx : longint;
  141. end;
  142. ttemprefnodeclass = class of ttemprefnode;
  143. { a node which removes a temp }
  144. ttempdeletenode = class(tnode)
  145. constructor create(const temp: ttempcreatenode); virtual;
  146. { this will convert the persistant temp to a normal temp
  147. for returning to the other nodes }
  148. constructor create_normal_temp(const temp: ttempcreatenode);
  149. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  150. procedure ppuwrite(ppufile:tcompilerppufile);override;
  151. function getcopy: tnode; override;
  152. procedure derefnode;override;
  153. function pass_1: tnode; override;
  154. function det_resulttype: tnode; override;
  155. function docompare(p: tnode): boolean; override;
  156. destructor destroy; override;
  157. protected
  158. tempinfo: ptempinfo;
  159. release_to_normal : boolean;
  160. private
  161. tempidx : longint;
  162. end;
  163. ttempdeletenodeclass = class of ttempdeletenode;
  164. var
  165. cnothingnode : tnothingnodeclass;
  166. cerrornode : terrornodeclass;
  167. casmnode : tasmnodeclass;
  168. cstatementnode : tstatementnodeclass;
  169. cblocknode : tblocknodeclass;
  170. ctempcreatenode : ttempcreatenodeclass;
  171. ctemprefnode : ttemprefnodeclass;
  172. ctempdeletenode : ttempdeletenodeclass;
  173. { Create a blocknode and statement node for multiple statements
  174. generated internally by the parser }
  175. function internalstatements(var laststatement:tstatementnode):tblocknode;
  176. 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. procedure addstatement(var laststatement:tstatementnode;n:tnode);
  196. begin
  197. if assigned(laststatement.right) then
  198. internalerror(200204201);
  199. laststatement.right:=cstatementnode.create(n,nil);
  200. laststatement:=tstatementnode(laststatement.right);
  201. end;
  202. {*****************************************************************************
  203. TFIRSTNOTHING
  204. *****************************************************************************}
  205. constructor tnothingnode.create;
  206. begin
  207. inherited create(nothingn);
  208. end;
  209. function tnothingnode.det_resulttype:tnode;
  210. begin
  211. result:=nil;
  212. resulttype:=voidtype;
  213. end;
  214. function tnothingnode.pass_1 : tnode;
  215. begin
  216. result:=nil;
  217. expectloc:=LOC_VOID;
  218. end;
  219. {*****************************************************************************
  220. TFIRSTERROR
  221. *****************************************************************************}
  222. constructor terrornode.create;
  223. begin
  224. inherited create(errorn);
  225. end;
  226. function terrornode.det_resulttype:tnode;
  227. begin
  228. result:=nil;
  229. include(flags,nf_error);
  230. codegenerror:=true;
  231. resulttype:=generrortype;
  232. end;
  233. function terrornode.pass_1 : tnode;
  234. begin
  235. result:=nil;
  236. expectloc:=LOC_VOID;
  237. codegenerror:=true;
  238. end;
  239. procedure terrornode.mark_write;
  240. begin
  241. end;
  242. {*****************************************************************************
  243. TSTATEMENTNODE
  244. *****************************************************************************}
  245. constructor tstatementnode.create(l,r : tnode);
  246. begin
  247. inherited create(statementn,l,r);
  248. end;
  249. function tstatementnode.det_resulttype:tnode;
  250. begin
  251. result:=nil;
  252. resulttype:=voidtype;
  253. { left is the statement itself calln assignn or a complex one }
  254. resulttypepass(left);
  255. if (not (cs_extsyntax in aktmoduleswitches)) and
  256. assigned(left.resulttype.def) and
  257. not((left.nodetype=calln) and
  258. { don't complain when funcretrefnode is set, because then the
  259. value is already used. And also not for constructors }
  260. (assigned(tcallnode(left).funcretnode) or
  261. (tcallnode(left).procdefinition.proctypeoption=potype_constructor))) and
  262. not(is_void(left.resulttype.def)) then
  263. CGMessage(cg_e_illegal_expression);
  264. if codegenerror then
  265. exit;
  266. { right is the next statement in the list }
  267. if assigned(right) then
  268. resulttypepass(right);
  269. if codegenerror then
  270. exit;
  271. end;
  272. function tstatementnode.pass_1 : tnode;
  273. begin
  274. result:=nil;
  275. { left is the statement itself calln assignn or a complex one }
  276. firstpass(left);
  277. if codegenerror then
  278. exit;
  279. expectloc:=left.expectloc;
  280. registersint:=left.registersint;
  281. registersfpu:=left.registersfpu;
  282. {$ifdef SUPPORT_MMX}
  283. registersmmx:=left.registersmmx;
  284. {$endif SUPPORT_MMX}
  285. { right is the next in the list }
  286. if assigned(right) then
  287. firstpass(right);
  288. if codegenerror then
  289. exit;
  290. end;
  291. procedure tstatementnode.printnodetree(var t:text);
  292. begin
  293. printnodelist(t);
  294. end;
  295. {*****************************************************************************
  296. TBLOCKNODE
  297. *****************************************************************************}
  298. constructor tblocknode.create(l : tnode);
  299. begin
  300. inherited create(blockn,l);
  301. end;
  302. destructor tblocknode.destroy;
  303. var
  304. hp, next: tstatementnode;
  305. begin
  306. hp := tstatementnode(left);
  307. left := nil;
  308. while assigned(hp) do
  309. begin
  310. next := tstatementnode(hp.right);
  311. hp.right := nil;
  312. hp.free;
  313. hp := next;
  314. end;
  315. inherited destroy;
  316. end;
  317. function tblocknode.det_resulttype:tnode;
  318. var
  319. hp : tstatementnode;
  320. begin
  321. result:=nil;
  322. resulttype:=voidtype;
  323. hp:=tstatementnode(left);
  324. while assigned(hp) do
  325. begin
  326. if assigned(hp.left) then
  327. begin
  328. codegenerror:=false;
  329. resulttypepass(hp.left);
  330. if not(codegenerror) and
  331. not(cs_extsyntax in aktmoduleswitches) and
  332. (hp.left.nodetype=calln) and
  333. not(is_void(hp.left.resulttype.def)) and
  334. not(nf_return_value_used in tcallnode(hp.left).flags) and
  335. not((tcallnode(hp.left).procdefinition.proctypeoption=potype_constructor) and
  336. assigned(tprocdef(tcallnode(hp.left).procdefinition)._class) and
  337. is_object(tprocdef(tcallnode(hp.left).procdefinition)._class)) then
  338. CGMessagePos(hp.left.fileinfo,cg_e_illegal_expression);
  339. { the resulttype of the block is the last type that is
  340. returned. Normally this is a voidtype. But when the
  341. compiler inserts a block of multiple statements then the
  342. last entry can return a value }
  343. resulttype:=hp.left.resulttype;
  344. end;
  345. hp:=tstatementnode(hp.right);
  346. end;
  347. end;
  348. function tblocknode.pass_1 : tnode;
  349. var
  350. hp : tstatementnode;
  351. count : longint;
  352. begin
  353. result:=nil;
  354. expectloc:=LOC_VOID;
  355. count:=0;
  356. hp:=tstatementnode(left);
  357. while assigned(hp) do
  358. begin
  359. (*
  360. if cs_regvars in aktglobalswitches then
  361. begin
  362. { node transformations }
  363. { concat function result to exit }
  364. { this is wrong for string or other complex
  365. result types !!! }
  366. if {ret_in_acc(current_procinfo.procdef.rettype.def) and }
  367. (is_ordinal(current_procinfo.procdef.rettype.def) or
  368. is_smallset(current_procinfo.procdef.rettype.def)) and
  369. assigned(hp.right) and
  370. assigned(tstatementnode(hp.right).left) and
  371. (tstatementnode(hp.right).left.nodetype=exitn) and
  372. (hp.left.nodetype=assignn) and
  373. { !!!! this tbinarynode should be tassignmentnode }
  374. (tbinarynode(hp.left).left.nodetype=loadn) and
  375. (is_funcret_sym(tloadnode(tbinarynode(hp.left).left).symtableentry)) then
  376. begin
  377. if assigned(texitnode(tstatementnode(hp.right).left).left) then
  378. CGMessage(cg_n_inefficient_code)
  379. else
  380. begin
  381. texitnode(tstatementnode(hp.right).left).left:=tassignmentnode(hp.left).right;
  382. tassignmentnode(hp.left).right:=nil;
  383. hp.left.free;
  384. hp.left:=nil;
  385. end;
  386. end
  387. { warning if unreachable code occurs and elimate this }
  388. else if (hp.left.nodetype in
  389. [exitn,breakn,continuen,goton]) and
  390. { statement node (JM) }
  391. assigned(hp.right) and
  392. { kind of statement! (JM) }
  393. assigned(tstatementnode(hp.right).left) and
  394. (tstatementnode(hp.right).left.nodetype<>labeln) then
  395. begin
  396. { use correct line number }
  397. aktfilepos:=hp.right.fileinfo;
  398. hp.right.free;
  399. hp.right:=nil;
  400. CGMessage(cg_w_unreachable_code);
  401. { old lines }
  402. aktfilepos:=hp.left.fileinfo;
  403. end;
  404. end;
  405. *)
  406. if assigned(hp.left) then
  407. begin
  408. codegenerror:=false;
  409. firstpass(hp.left);
  410. hp.expectloc:=hp.left.expectloc;
  411. hp.registersint:=hp.left.registersint;
  412. hp.registersfpu:=hp.left.registersfpu;
  413. {$ifdef SUPPORT_MMX}
  414. hp.registersmmx:=hp.left.registersmmx;
  415. {$endif SUPPORT_MMX}
  416. end
  417. else
  418. hp.registersint:=0;
  419. if hp.registersint>registersint then
  420. registersint:=hp.registersint;
  421. if hp.registersfpu>registersfpu then
  422. registersfpu:=hp.registersfpu;
  423. {$ifdef SUPPORT_MMX}
  424. if hp.registersmmx>registersmmx then
  425. registersmmx:=hp.registersmmx;
  426. {$endif}
  427. expectloc:=hp.expectloc;
  428. inc(count);
  429. hp:=tstatementnode(hp.right);
  430. end;
  431. end;
  432. {$ifdef state_tracking}
  433. function Tblocknode.track_state_pass(exec_known:boolean):boolean;
  434. var hp:Tstatementnode;
  435. begin
  436. track_state_pass:=false;
  437. hp:=Tstatementnode(left);
  438. while assigned(hp) do
  439. begin
  440. if hp.left.track_state_pass(exec_known) then
  441. track_state_pass:=true;
  442. hp:=Tstatementnode(hp.right);
  443. end;
  444. end;
  445. {$endif state_tracking}
  446. {*****************************************************************************
  447. TASMNODE
  448. *****************************************************************************}
  449. constructor tasmnode.create(p : taasmoutput);
  450. begin
  451. inherited create(asmn);
  452. p_asm:=p;
  453. getposition:=false;
  454. currenttai:=nil;
  455. used_regs_int:=[];
  456. used_regs_fpu:=[];
  457. end;
  458. constructor tasmnode.create_get_position;
  459. begin
  460. inherited create(asmn);
  461. p_asm:=nil;
  462. getposition:=true;
  463. currenttai:=nil;
  464. end;
  465. destructor tasmnode.destroy;
  466. begin
  467. if assigned(p_asm) then
  468. p_asm.free;
  469. inherited destroy;
  470. end;
  471. constructor tasmnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  472. var
  473. hp : tai;
  474. begin
  475. inherited ppuload(t,ppufile);
  476. getposition:=boolean(ppufile.getbyte);
  477. if not getposition then
  478. begin
  479. p_asm:=taasmoutput.create;
  480. repeat
  481. hp:=ppuloadai(ppufile);
  482. if hp=nil then
  483. break;
  484. p_asm.concat(hp);
  485. until false;
  486. end
  487. else
  488. p_asm:=nil;
  489. currenttai:=nil;
  490. end;
  491. procedure tasmnode.ppuwrite(ppufile:tcompilerppufile);
  492. var
  493. hp : tai;
  494. begin
  495. inherited ppuwrite(ppufile);
  496. ppufile.putbyte(byte(getposition));
  497. {$warning FIXME Add saving of register sets}
  498. if not getposition then
  499. begin
  500. hp:=tai(p_asm.first);
  501. while assigned(hp) do
  502. begin
  503. ppuwriteai(ppufile,hp);
  504. hp:=tai(hp.next);
  505. end;
  506. { end is marked by a nil }
  507. ppuwriteai(ppufile,nil);
  508. end;
  509. end;
  510. procedure tasmnode.buildderefimpl;
  511. var
  512. hp : tai;
  513. begin
  514. inherited buildderefimpl;
  515. if not getposition then
  516. begin
  517. hp:=tai(p_asm.first);
  518. while assigned(hp) do
  519. begin
  520. hp.buildderefimpl;
  521. hp:=tai(hp.next);
  522. end;
  523. end;
  524. end;
  525. procedure tasmnode.derefimpl;
  526. var
  527. hp : tai;
  528. begin
  529. inherited derefimpl;
  530. if not getposition then
  531. begin
  532. hp:=tai(p_asm.first);
  533. while assigned(hp) do
  534. begin
  535. hp.derefimpl;
  536. hp:=tai(hp.next);
  537. end;
  538. end;
  539. end;
  540. function tasmnode.getcopy: tnode;
  541. var
  542. n: tasmnode;
  543. begin
  544. n := tasmnode(inherited getcopy);
  545. if assigned(p_asm) then
  546. begin
  547. n.p_asm:=taasmoutput.create;
  548. n.p_asm.concatlistcopy(p_asm);
  549. end
  550. else n.p_asm := nil;
  551. n.getposition:=getposition;
  552. n.currenttai:=currenttai;
  553. getcopy := n;
  554. end;
  555. function tasmnode.det_resulttype:tnode;
  556. begin
  557. result:=nil;
  558. resulttype:=voidtype;
  559. if not getposition then
  560. include(current_procinfo.flags,pi_uses_asm);
  561. end;
  562. function tasmnode.pass_1 : tnode;
  563. begin
  564. result:=nil;
  565. expectloc:=LOC_VOID;
  566. end;
  567. function tasmnode.docompare(p: tnode): boolean;
  568. begin
  569. { comparing of asmlists is not implemented (JM) }
  570. docompare := false;
  571. end;
  572. {*****************************************************************************
  573. TEMPCREATENODE
  574. *****************************************************************************}
  575. constructor ttempcreatenode.create_reg(const _restype: ttype; _size: longint; _temptype: ttemptype);
  576. begin
  577. create(_restype,_size,_temptype);
  578. may_be_in_reg:=true;
  579. end;
  580. constructor ttempcreatenode.create(const _restype: ttype; _size: longint; _temptype: ttemptype);
  581. begin
  582. inherited create(tempcreaten);
  583. size := _size;
  584. new(tempinfo);
  585. fillchar(tempinfo^,sizeof(tempinfo^),0);
  586. tempinfo^.restype := _restype;
  587. tempinfo^.temptype := _temptype;
  588. tempinfo^.owner:=self;
  589. may_be_in_reg:=false;
  590. end;
  591. function ttempcreatenode.getcopy: tnode;
  592. var
  593. n: ttempcreatenode;
  594. begin
  595. n := ttempcreatenode(inherited getcopy);
  596. n.size := size;
  597. n.may_be_in_reg := may_be_in_reg;
  598. new(n.tempinfo);
  599. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  600. n.tempinfo^.owner:=n;
  601. n.tempinfo^.restype := tempinfo^.restype;
  602. n.tempinfo^.temptype := tempinfo^.temptype;
  603. { when the tempinfo has already a hookoncopy then it is not
  604. reset by a tempdeletenode }
  605. if assigned(tempinfo^.hookoncopy) then
  606. internalerror(200211262);
  607. { signal the temprefs that the temp they point to has been copied, }
  608. { so that if the refs get copied as well, they can hook themselves }
  609. { to the copy of the temp }
  610. tempinfo^.hookoncopy := n.tempinfo;
  611. tempinfo^.nextref_set_hookoncopy_nil := false;
  612. result := n;
  613. end;
  614. constructor ttempcreatenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  615. begin
  616. inherited ppuload(t,ppufile);
  617. size:=ppufile.getlongint;
  618. may_be_in_reg:=boolean(ppufile.getbyte);
  619. new(tempinfo);
  620. fillchar(tempinfo^,sizeof(tempinfo^),0);
  621. ppufile.gettype(tempinfo^.restype);
  622. tempinfo^.temptype := ttemptype(ppufile.getbyte);
  623. tempinfo^.owner:=self;
  624. end;
  625. procedure ttempcreatenode.ppuwrite(ppufile:tcompilerppufile);
  626. begin
  627. inherited ppuwrite(ppufile);
  628. ppufile.putlongint(size);
  629. ppufile.putbyte(byte(may_be_in_reg));
  630. ppufile.puttype(tempinfo^.restype);
  631. ppufile.putbyte(byte(tempinfo^.temptype));
  632. end;
  633. procedure ttempcreatenode.buildderefimpl;
  634. begin
  635. tempinfo^.restype.buildderef;
  636. end;
  637. procedure ttempcreatenode.derefimpl;
  638. begin
  639. tempinfo^.restype.resolve;
  640. end;
  641. function ttempcreatenode.pass_1 : tnode;
  642. begin
  643. result := nil;
  644. expectloc:=LOC_VOID;
  645. end;
  646. function ttempcreatenode.det_resulttype: tnode;
  647. begin
  648. result := nil;
  649. { a tempcreatenode doesn't have a resulttype, only temprefnodes do }
  650. resulttype := voidtype;
  651. end;
  652. function ttempcreatenode.docompare(p: tnode): boolean;
  653. begin
  654. result :=
  655. inherited docompare(p) and
  656. (ttempcreatenode(p).size = size) and
  657. (ttempcreatenode(p).may_be_in_reg = may_be_in_reg) and
  658. equal_defs(ttempcreatenode(p).tempinfo^.restype.def,tempinfo^.restype.def);
  659. end;
  660. procedure ttempcreatenode.printnodedata(var t:text);
  661. begin
  662. inherited printnodedata(t);
  663. writeln(t,printnodeindention,'size = ',size);
  664. end;
  665. {*****************************************************************************
  666. TEMPREFNODE
  667. *****************************************************************************}
  668. constructor ttemprefnode.create(const temp: ttempcreatenode);
  669. begin
  670. inherited create(temprefn);
  671. tempinfo := temp.tempinfo;
  672. offset:=0;
  673. end;
  674. constructor ttemprefnode.create_offset(const temp: ttempcreatenode;aoffset:longint);
  675. begin
  676. self.create(temp);
  677. offset := aoffset;
  678. end;
  679. function ttemprefnode.getcopy: tnode;
  680. var
  681. n: ttemprefnode;
  682. begin
  683. n := ttemprefnode(inherited getcopy);
  684. n.offset := offset;
  685. if assigned(tempinfo^.hookoncopy) then
  686. { if the temp has been copied, assume it becomes a new }
  687. { temp which has to be hooked by the copied reference }
  688. begin
  689. { hook the ref to the copied temp }
  690. n.tempinfo := tempinfo^.hookoncopy;
  691. { if we passed a ttempdeletenode that changed the temp }
  692. { from a persistent one into a normal one, we must be }
  693. { the last reference (since our parent should free the }
  694. { temp (JM) }
  695. if (tempinfo^.nextref_set_hookoncopy_nil) then
  696. tempinfo^.hookoncopy := nil;
  697. end
  698. else
  699. { if the temp we refer to hasn't been copied, assume }
  700. { we're just a new reference to that temp }
  701. begin
  702. n.tempinfo := tempinfo;
  703. end;
  704. result := n;
  705. end;
  706. constructor ttemprefnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  707. begin
  708. inherited ppuload(t,ppufile);
  709. tempidx:=ppufile.getlongint;
  710. offset:=ppufile.getlongint;
  711. end;
  712. procedure ttemprefnode.ppuwrite(ppufile:tcompilerppufile);
  713. begin
  714. inherited ppuwrite(ppufile);
  715. ppufile.putlongint(tempinfo^.owner.ppuidx);
  716. ppufile.putlongint(offset);
  717. end;
  718. procedure ttemprefnode.derefnode;
  719. var
  720. temp : ttempcreatenode;
  721. begin
  722. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  723. if temp.nodetype<>tempcreaten then
  724. internalerror(200311075);
  725. tempinfo:=temp.tempinfo;
  726. end;
  727. function ttemprefnode.pass_1 : tnode;
  728. begin
  729. expectloc:=LOC_REFERENCE;
  730. result := nil;
  731. end;
  732. function ttemprefnode.det_resulttype: tnode;
  733. begin
  734. { check if the temp is already resulttype passed }
  735. if not assigned(tempinfo^.restype.def) then
  736. internalerror(200108233);
  737. result := nil;
  738. resulttype := tempinfo^.restype;
  739. end;
  740. function ttemprefnode.docompare(p: tnode): boolean;
  741. begin
  742. result :=
  743. inherited docompare(p) and
  744. (ttemprefnode(p).tempinfo = tempinfo) and
  745. (ttemprefnode(p).offset = offset);
  746. end;
  747. procedure Ttemprefnode.mark_write;
  748. begin
  749. include(flags,nf_write);
  750. end;
  751. {*****************************************************************************
  752. TEMPDELETENODE
  753. *****************************************************************************}
  754. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  755. begin
  756. inherited create(tempdeleten);
  757. tempinfo := temp.tempinfo;
  758. release_to_normal := false;
  759. end;
  760. constructor ttempdeletenode.create_normal_temp(const temp: ttempcreatenode);
  761. begin
  762. inherited create(tempdeleten);
  763. tempinfo := temp.tempinfo;
  764. release_to_normal := true;
  765. if tempinfo^.temptype <> tt_persistent then
  766. internalerror(200204211);
  767. end;
  768. function ttempdeletenode.getcopy: tnode;
  769. var
  770. n: ttempdeletenode;
  771. begin
  772. n := ttempdeletenode(inherited getcopy);
  773. n.release_to_normal := release_to_normal;
  774. if assigned(tempinfo^.hookoncopy) then
  775. { if the temp has been copied, assume it becomes a new }
  776. { temp which has to be hooked by the copied deletenode }
  777. begin
  778. { hook the tempdeletenode to the copied temp }
  779. n.tempinfo := tempinfo^.hookoncopy;
  780. { the temp shall not be used, reset hookoncopy }
  781. { Only if release_to_normal is false, otherwise }
  782. { the temp can still be referenced once more (JM) }
  783. if (not release_to_normal) then
  784. tempinfo^.hookoncopy:=nil
  785. else
  786. tempinfo^.nextref_set_hookoncopy_nil := true;
  787. end
  788. else
  789. { if the temp we refer to hasn't been copied, we have a }
  790. { problem since that means we now have two delete nodes }
  791. { for one temp }
  792. internalerror(200108234);
  793. result := n;
  794. end;
  795. constructor ttempdeletenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  796. begin
  797. inherited ppuload(t,ppufile);
  798. tempidx:=ppufile.getlongint;
  799. release_to_normal:=(ppufile.getbyte<>0);
  800. end;
  801. procedure ttempdeletenode.ppuwrite(ppufile:tcompilerppufile);
  802. begin
  803. inherited ppuwrite(ppufile);
  804. ppufile.putlongint(tempinfo^.owner.ppuidx);
  805. ppufile.putbyte(byte(release_to_normal));
  806. end;
  807. procedure ttempdeletenode.derefnode;
  808. var
  809. temp : ttempcreatenode;
  810. begin
  811. temp:=ttempcreatenode(nodeppuidxget(tempidx));
  812. if temp.nodetype<>tempcreaten then
  813. internalerror(200311075);
  814. tempinfo:=temp.tempinfo;
  815. end;
  816. function ttempdeletenode.pass_1 : tnode;
  817. begin
  818. expectloc:=LOC_VOID;
  819. result := nil;
  820. end;
  821. function ttempdeletenode.det_resulttype: tnode;
  822. begin
  823. result := nil;
  824. resulttype := voidtype;
  825. end;
  826. function ttempdeletenode.docompare(p: tnode): boolean;
  827. begin
  828. result :=
  829. inherited docompare(p) and
  830. (ttemprefnode(p).tempinfo = tempinfo);
  831. end;
  832. destructor ttempdeletenode.destroy;
  833. begin
  834. dispose(tempinfo);
  835. end;
  836. begin
  837. cnothingnode:=tnothingnode;
  838. cerrornode:=terrornode;
  839. casmnode:=tasmnode;
  840. cstatementnode:=tstatementnode;
  841. cblocknode:=tblocknode;
  842. ctempcreatenode:=ttempcreatenode;
  843. ctemprefnode:=ttemprefnode;
  844. ctempdeletenode:=ttempdeletenode;
  845. end.
  846. {
  847. $Log$
  848. Revision 1.79 2004-02-03 22:32:54 peter
  849. * renamed xNNbittype to xNNinttype
  850. * renamed registers32 to registersint
  851. * replace some s32bit,u32bit with torddef([su]inttype).def.typ
  852. Revision 1.78 2004/02/03 19:48:06 jonas
  853. * fixed and re-enabled temps in registers
  854. Revision 1.77 2004/02/03 17:56:14 jonas
  855. * disabled temps in registers for now, because the register allocator
  856. can't handle it yet in some cases
  857. Revision 1.76 2004/02/03 16:46:51 jonas
  858. + support to store ttempcreate/ref/deletenodes in registers
  859. * put temps for withnodes and some newnodes in registers
  860. Note: this currently only works because calling ungetregister()
  861. multiple times for the same register doesn't matter. We need again
  862. a way to specify that a register is currently a regvar and as such
  863. should not be freed when you call ungetregister() on it.
  864. Revision 1.75 2004/01/26 16:12:27 daniel
  865. * reginfo now also only allocated during register allocation
  866. * third round of gdb cleanups: kick out most of concatstabto
  867. Revision 1.74 2003/12/10 20:31:40 jonas
  868. * override tblocknode.destroy so all statements are freed sequentially
  869. instead of recusively.
  870. Revision 1.73 2003/11/10 22:02:52 peter
  871. * cross unit inlining fixed
  872. Revision 1.72 2003/11/04 15:35:13 peter
  873. * fix for referencecounted temps
  874. Revision 1.71 2003/10/31 15:51:47 peter
  875. * fix crashes in asmnode.deref when p_asm=nil
  876. Revision 1.70 2003/10/29 20:34:20 peter
  877. * move check for unused object constructor result to blocknode
  878. Revision 1.69 2003/10/23 14:44:07 peter
  879. * splitted buildderef and buildderefimpl to fix interface crc
  880. calculation
  881. Revision 1.68 2003/10/22 20:40:00 peter
  882. * write derefdata in a separate ppu entry
  883. Revision 1.67 2003/10/21 18:15:16 peter
  884. * fixed check for $X- result usage
  885. Revision 1.66 2003/10/19 01:34:30 florian
  886. * some ppc stuff fixed
  887. * memory leak fixed
  888. Revision 1.65 2003/10/17 14:38:32 peter
  889. * 64k registers supported
  890. * fixed some memory leaks
  891. Revision 1.64 2003/10/10 17:48:13 peter
  892. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  893. * tregisteralloctor renamed to trgobj
  894. * removed rgobj from a lot of units
  895. * moved location_* and reference_* to cgobj
  896. * first things for mmx register allocation
  897. Revision 1.63 2003/10/01 20:34:48 peter
  898. * procinfo unit contains tprocinfo
  899. * cginfo renamed to cgbase
  900. * moved cgmessage to verbose
  901. * fixed ppc and sparc compiles
  902. Revision 1.62 2003/09/23 17:56:05 peter
  903. * locals and paras are allocated in the code generation
  904. * tvarsym.localloc contains the location of para/local when
  905. generating code for the current procedure
  906. Revision 1.61 2003/09/07 22:09:35 peter
  907. * preparations for different default calling conventions
  908. * various RA fixes
  909. Revision 1.60 2003/09/03 15:55:00 peter
  910. * NEWRA branch merged
  911. Revision 1.59.2.1 2003/08/27 20:23:55 peter
  912. * remove old ra code
  913. Revision 1.59 2003/08/09 18:56:54 daniel
  914. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  915. allocator
  916. * Some preventive changes to i386 spillinh code
  917. Revision 1.58 2003/06/13 21:19:30 peter
  918. * current_procdef removed, use current_procinfo.procdef instead
  919. Revision 1.57 2003/06/10 09:10:47 jonas
  920. * patch from Peter to fix tempinfo copying
  921. Revision 1.56 2003/06/09 18:26:46 peter
  922. * remove temptype, use tempinfo.temptype instead
  923. Revision 1.55 2003/06/09 12:20:47 peter
  924. * getposition added to retrieve the the current tai item
  925. Revision 1.54 2003/06/08 18:27:15 jonas
  926. + ability to change the location of a ttempref node with changelocation()
  927. method. Useful to use instead of copying the contents from one temp to
  928. another
  929. + some shortstring optimizations in tassignmentnode that avoid some
  930. copying (required some shortstring optimizations to be moved from
  931. resulttype to firstpass, because they work on callnodes and string
  932. addnodes are only changed to callnodes in the firstpass)
  933. * allow setting/changing the funcretnode of callnodes after the
  934. resulttypepass has been done, funcretnode is now a property
  935. (all of the above should have a quite big effect on callparatemp)
  936. Revision 1.53 2003/05/30 21:01:44 jonas
  937. - disabled "result := value; exit;" -> exit(value) optimization because
  938. a) it was wrong
  939. b) exit(value) works now exactly the same as that
  940. (it was only activated with -Or)
  941. Revision 1.52 2003/05/23 14:27:35 peter
  942. * remove some unit dependencies
  943. * current_procinfo changes to store more info
  944. Revision 1.51 2003/05/17 13:30:08 jonas
  945. * changed tt_persistant to tt_persistent :)
  946. * tempcreatenode now doesn't accept a boolean anymore for persistent
  947. temps, but a ttemptype, so you can also create ansistring temps etc
  948. Revision 1.50 2003/05/13 19:14:41 peter
  949. * failn removed
  950. * inherited result code check moven to pexpr
  951. Revision 1.49 2003/05/11 14:45:12 peter
  952. * tloadnode does not support objectsymtable,withsymtable anymore
  953. * withnode cleanup
  954. * direct with rewritten to use temprefnode
  955. Revision 1.48 2003/04/27 11:21:33 peter
  956. * aktprocdef renamed to current_procinfo.procdef
  957. * procinfo renamed to current_procinfo
  958. * procinfo will now be stored in current_module so it can be
  959. cleaned up properly
  960. * gen_main_procsym changed to create_main_proc and release_main_proc
  961. to also generate a tprocinfo structure
  962. * fixed unit implicit initfinal
  963. Revision 1.47 2003/04/25 20:59:33 peter
  964. * removed funcretn,funcretsym, function result is now in varsym
  965. and aliases for result and function name are added using absolutesym
  966. * vs_hidden parameter for funcret passed in parameter
  967. * vs_hidden fixes
  968. * writenode changed to printnode and released from extdebug
  969. * -vp option added to generate a tree.log with the nodetree
  970. * nicer printnode for statements, callnode
  971. Revision 1.46 2002/04/25 20:15:39 florian
  972. * block nodes within expressions shouldn't release the used registers,
  973. fixed using a flag till the new rg is ready
  974. Revision 1.45 2003/04/23 08:41:34 jonas
  975. * fixed ttemprefnode.compare and .getcopy to take offset field into
  976. account
  977. Revision 1.44 2003/04/22 23:50:22 peter
  978. * firstpass uses expectloc
  979. * checks if there are differences between the expectloc and
  980. location.loc from secondpass in EXTDEBUG
  981. Revision 1.43 2003/04/21 15:00:22 jonas
  982. * fixed tstatementnode.det_resulttype and tststatementnode.pass_1
  983. * fixed some getcopy issues with ttemp*nodes
  984. Revision 1.42 2003/04/17 07:50:24 daniel
  985. * Some work on interference graph construction
  986. Revision 1.41 2003/04/12 14:53:59 jonas
  987. * ttempdeletenode.create now sets the nodetype to tempdeleten instead of
  988. temprefn
  989. Revision 1.40 2003/03/17 20:30:46 peter
  990. * errornode.mark_write added
  991. Revision 1.39 2003/01/03 12:15:55 daniel
  992. * Removed ifdefs around notifications
  993. ifdefs around for loop optimizations remain
  994. Revision 1.38 2002/11/27 02:37:12 peter
  995. * case statement inlining added
  996. * fixed inlining of write()
  997. * switched statementnode left and right parts so the statements are
  998. processed in the correct order when getcopy is used. This is
  999. required for tempnodes
  1000. Revision 1.37 2002/11/25 17:43:17 peter
  1001. * splitted defbase in defutil,symutil,defcmp
  1002. * merged isconvertable and is_equal into compare_defs(_ext)
  1003. * made operator search faster by walking the list only once
  1004. Revision 1.36 2002/10/05 15:15:19 peter
  1005. * don't complain in X- mode for internal generated function calls
  1006. with funcretrefnode set
  1007. * give statement error at the correct line position instead of the
  1008. block begin
  1009. Revision 1.35 2002/09/01 08:01:16 daniel
  1010. * Removed sets from Tcallnode.det_resulttype
  1011. + Added read/write notifications of variables. These will be usefull
  1012. for providing information for several optimizations. For example
  1013. the value of the loop variable of a for loop does matter is the
  1014. variable is read after the for loop, but if it's no longer used
  1015. or written, it doesn't matter and this can be used to optimize
  1016. the loop code generation.
  1017. Revision 1.34 2002/08/18 20:06:23 peter
  1018. * inlining is now also allowed in interface
  1019. * renamed write/load to ppuwrite/ppuload
  1020. * tnode storing in ppu
  1021. * nld,ncon,nbas are already updated for storing in ppu
  1022. Revision 1.33 2002/08/17 22:09:44 florian
  1023. * result type handling in tcgcal.pass_2 overhauled
  1024. * better tnode.printnodetree
  1025. * some ppc stuff fixed
  1026. Revision 1.32 2002/08/17 09:23:34 florian
  1027. * first part of procinfo rewrite
  1028. Revision 1.31 2002/08/15 19:10:35 peter
  1029. * first things tai,tnode storing in ppu
  1030. Revision 1.30 2002/07/20 11:57:53 florian
  1031. * types.pas renamed to defbase.pas because D6 contains a types
  1032. unit so this would conflicts if D6 programms are compiled
  1033. + Willamette/SSE2 instructions to assembler added
  1034. Revision 1.29 2002/07/19 11:41:35 daniel
  1035. * State tracker work
  1036. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1037. allows the state tracker to change while nodes automatically into
  1038. repeat nodes.
  1039. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1040. 'not(a>b)' is optimized into 'a<=b'.
  1041. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1042. by removing the notn and later switchting the true and falselabels. The
  1043. same is done with 'repeat until not a'.
  1044. Revision 1.28 2002/07/14 18:00:43 daniel
  1045. + Added the beginning of a state tracker. This will track the values of
  1046. variables through procedures and optimize things away.
  1047. Revision 1.27 2002/07/01 18:46:22 peter
  1048. * internal linker
  1049. * reorganized aasm layer
  1050. Revision 1.26 2002/06/24 12:43:00 jonas
  1051. * fixed errors found with new -CR code from Peter when cycling with -O2p3r
  1052. Revision 1.25 2002/05/18 13:34:09 peter
  1053. * readded missing revisions
  1054. Revision 1.24 2002/05/16 19:46:37 carl
  1055. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1056. + try to fix temp allocation (still in ifdef)
  1057. + generic constructor calls
  1058. + start of tassembler / tmodulebase class cleanup
  1059. Revision 1.22 2002/04/23 19:16:34 peter
  1060. * add pinline unit that inserts compiler supported functions using
  1061. one or more statements
  1062. * moved finalize and setlength from ninl to pinline
  1063. Revision 1.21 2002/04/21 19:02:03 peter
  1064. * removed newn and disposen nodes, the code is now directly
  1065. inlined from pexpr
  1066. * -an option that will write the secondpass nodes to the .s file, this
  1067. requires EXTDEBUG define to actually write the info
  1068. * fixed various internal errors and crashes due recent code changes
  1069. Revision 1.20 2002/04/04 19:05:57 peter
  1070. * removed unused units
  1071. * use tlocation.size in cg.a_*loc*() routines
  1072. Revision 1.19 2002/03/31 20:26:33 jonas
  1073. + a_loadfpu_* and a_loadmm_* methods in tcg
  1074. * register allocation is now handled by a class and is mostly processor
  1075. independent (+rgobj.pas and i386/rgcpu.pas)
  1076. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1077. * some small improvements and fixes to the optimizer
  1078. * some register allocation fixes
  1079. * some fpuvaroffset fixes in the unary minus node
  1080. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1081. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1082. also better optimizable)
  1083. * fixed and optimized register saving/restoring for new/dispose nodes
  1084. * LOC_FPU locations now also require their "register" field to be set to
  1085. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1086. - list field removed of the tnode class because it's not used currently
  1087. and can cause hard-to-find bugs
  1088. }