nbas.pas 41 KB

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