nbas.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. {
  2. $Id$
  3. Copyright (c) 2000 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 defines.inc}
  20. interface
  21. uses
  22. aasm,symtype,node,cpubase;
  23. type
  24. tnothingnode = class(tnode)
  25. constructor create;virtual;
  26. function pass_1 : tnode;override;
  27. function det_resulttype:tnode;override;
  28. end;
  29. tnothingnodeclass = class of tnothingnode;
  30. terrornode = class(tnode)
  31. constructor create;virtual;
  32. function pass_1 : tnode;override;
  33. function det_resulttype:tnode;override;
  34. end;
  35. terrornodeclass = class of terrornode;
  36. tasmnode = class(tnode)
  37. p_asm : taasmoutput;
  38. constructor create(p : taasmoutput);virtual;
  39. destructor destroy;override;
  40. function getcopy : tnode;override;
  41. function pass_1 : tnode;override;
  42. function det_resulttype:tnode;override;
  43. function docompare(p: tnode): boolean; override;
  44. end;
  45. tasmnodeclass = class of tasmnode;
  46. tstatementnode = class(tbinarynode)
  47. constructor create(l,r : tnode);virtual;
  48. function pass_1 : tnode;override;
  49. function det_resulttype:tnode;override;
  50. {$ifdef extdebug}
  51. procedure dowrite;override;
  52. {$endif extdebug}
  53. end;
  54. tstatementnodeclass = class of tstatementnode;
  55. tblocknode = class(tunarynode)
  56. constructor create(l : tnode);virtual;
  57. function pass_1 : tnode;override;
  58. function det_resulttype:tnode;override;
  59. end;
  60. tblocknodeclass = class of tblocknode;
  61. { to allow access to the location by temp references even after the temp has }
  62. { already been disposed and to make sure the coherency between temps and }
  63. { temp references is kept after a getcopy }
  64. ptempinfo = ^ttempinfo;
  65. ttempinfo = record
  66. { set to the copy of a tempcreate pnode (if it gets copied) so that the }
  67. { refs and deletenode can hook to this copy once they get copied too }
  68. hookoncopy: ptempinfo;
  69. ref: treference;
  70. restype: ttype;
  71. valid: boolean;
  72. end;
  73. { a node which will create a (non)persistent temp of a given type with a given }
  74. { size (the size is separate to allow creating "void" temps with a custom size) }
  75. ttempcreatenode = class(tnode)
  76. size: longint;
  77. tempinfo: ptempinfo;
  78. { * persistent temps are used in manually written code where the temp }
  79. { be usable among different statements and where you can manually say }
  80. { when the temp has to be freed (using a ttempdeletenode) }
  81. { * non-persistent temps are mostly used in typeconversion helpers, }
  82. { where the node that receives the temp becomes responsible for }
  83. { freeing it. In this last case, you should use only one reference }
  84. { to it and *not* generate a ttempdeletenode }
  85. constructor create(const _restype: ttype; _size: longint; _persistent: boolean); virtual;
  86. function getcopy: tnode; override;
  87. function pass_1 : tnode; override;
  88. function det_resulttype: tnode; override;
  89. function docompare(p: tnode): boolean; override;
  90. protected
  91. persistent: boolean;
  92. end;
  93. ttempcreatenodeclass = class of ttempcreatenode;
  94. { a node which is a reference to a certain temp }
  95. ttemprefnode = class(tnode)
  96. constructor create(const temp: ttempcreatenode); virtual;
  97. function getcopy: tnode; override;
  98. function pass_1 : tnode; override;
  99. function det_resulttype : tnode; override;
  100. function docompare(p: tnode): boolean; override;
  101. protected
  102. tempinfo: ptempinfo;
  103. end;
  104. ttemprefnodeclass = class of ttemprefnode;
  105. { a node which removes a temp }
  106. ttempdeletenode = class(tnode)
  107. constructor create(const temp: ttempcreatenode);
  108. function getcopy: tnode; override;
  109. function pass_1: tnode; override;
  110. function det_resulttype: tnode; override;
  111. function docompare(p: tnode): boolean; override;
  112. destructor destroy; override;
  113. protected
  114. tempinfo: ptempinfo;
  115. end;
  116. ttempdeletenodeclass = class of ttempdeletenode;
  117. var
  118. cnothingnode : tnothingnodeclass;
  119. cerrornode : terrornodeclass;
  120. casmnode : tasmnodeclass;
  121. cstatementnode : tstatementnodeclass;
  122. cblocknode : tblocknodeclass;
  123. ctempcreatenode : ttempcreatenodeclass;
  124. ctemprefnode : ttemprefnodeclass;
  125. ctempdeletenode : ttempdeletenodeclass;
  126. implementation
  127. uses
  128. cutils,
  129. verbose,globals,globtype,systems,
  130. symconst,symdef,symsym,types,
  131. pass_1,
  132. ncal,nflw,tgcpu,cgbase
  133. ;
  134. {*****************************************************************************
  135. TFIRSTNOTHING
  136. *****************************************************************************}
  137. constructor tnothingnode.create;
  138. begin
  139. inherited create(nothingn);
  140. end;
  141. function tnothingnode.det_resulttype:tnode;
  142. begin
  143. result:=nil;
  144. resulttype:=voidtype;
  145. end;
  146. function tnothingnode.pass_1 : tnode;
  147. begin
  148. result:=nil;
  149. end;
  150. {*****************************************************************************
  151. TFIRSTERROR
  152. *****************************************************************************}
  153. constructor terrornode.create;
  154. begin
  155. inherited create(errorn);
  156. end;
  157. function terrornode.det_resulttype:tnode;
  158. begin
  159. result:=nil;
  160. include(flags,nf_error);
  161. codegenerror:=true;
  162. resulttype:=generrortype;
  163. end;
  164. function terrornode.pass_1 : tnode;
  165. begin
  166. result:=nil;
  167. codegenerror:=true;
  168. end;
  169. {*****************************************************************************
  170. TSTATEMENTNODE
  171. *****************************************************************************}
  172. constructor tstatementnode.create(l,r : tnode);
  173. begin
  174. inherited create(statementn,l,r);
  175. end;
  176. function tstatementnode.det_resulttype:tnode;
  177. begin
  178. result:=nil;
  179. resulttype:=voidtype;
  180. { right is the statement itself calln assignn or a complex one }
  181. resulttypepass(right);
  182. if (not (cs_extsyntax in aktmoduleswitches)) and
  183. assigned(right.resulttype.def) and
  184. not((right.nodetype=calln) and
  185. (tcallnode(right).procdefinition.proctypeoption=potype_constructor)) and
  186. not(is_void(right.resulttype.def)) then
  187. CGMessage(cg_e_illegal_expression);
  188. if codegenerror then
  189. exit;
  190. { left is the next in the list }
  191. resulttypepass(left);
  192. if codegenerror then
  193. exit;
  194. end;
  195. function tstatementnode.pass_1 : tnode;
  196. begin
  197. result:=nil;
  198. { no temps over several statements }
  199. {$ifdef newcg}
  200. tg.cleartempgen;
  201. {$else newcg}
  202. cleartempgen;
  203. {$endif newcg}
  204. { right is the statement itself calln assignn or a complex one }
  205. firstpass(right);
  206. if codegenerror then
  207. exit;
  208. registers32:=right.registers32;
  209. registersfpu:=right.registersfpu;
  210. {$ifdef SUPPORT_MMX}
  211. registersmmx:=right.registersmmx;
  212. {$endif SUPPORT_MMX}
  213. { left is the next in the list }
  214. firstpass(left);
  215. if codegenerror then
  216. exit;
  217. if right.registers32>registers32 then
  218. registers32:=right.registers32;
  219. if right.registersfpu>registersfpu then
  220. registersfpu:=right.registersfpu;
  221. {$ifdef SUPPORT_MMX}
  222. if right.registersmmx>registersmmx then
  223. registersmmx:=right.registersmmx;
  224. {$endif}
  225. end;
  226. {$ifdef extdebug}
  227. procedure tstatementnode.dowrite;
  228. begin
  229. { can't use inherited dowrite, because that will use the
  230. binary which we don't want for statements }
  231. dowritenodetype;
  232. writeln(',');
  233. { write the statement }
  234. writenodeindention:=writenodeindention+' ';
  235. writenode(right);
  236. writeln(')');
  237. delete(writenodeindention,1,4);
  238. { go on with the next statement }
  239. writenode(left);
  240. end;
  241. {$endif}
  242. {*****************************************************************************
  243. TBLOCKNODE
  244. *****************************************************************************}
  245. constructor tblocknode.create(l : tnode);
  246. begin
  247. inherited create(blockn,l);
  248. end;
  249. function tblocknode.det_resulttype:tnode;
  250. var
  251. hp : tstatementnode;
  252. begin
  253. result:=nil;
  254. resulttype:=voidtype;
  255. hp:=tstatementnode(left);
  256. while assigned(hp) do
  257. begin
  258. if assigned(hp.right) then
  259. begin
  260. codegenerror:=false;
  261. resulttypepass(hp.right);
  262. if (not (cs_extsyntax in aktmoduleswitches)) and
  263. assigned(hp.right.resulttype.def) and
  264. not((hp.right.nodetype=calln) and
  265. (tcallnode(hp.right).procdefinition.proctypeoption=potype_constructor)) and
  266. not(is_void(hp.right.resulttype.def)) then
  267. CGMessage(cg_e_illegal_expression);
  268. end;
  269. hp:=tstatementnode(hp.left);
  270. end;
  271. end;
  272. function tblocknode.pass_1 : tnode;
  273. var
  274. hp : tstatementnode;
  275. count : longint;
  276. begin
  277. result:=nil;
  278. count:=0;
  279. hp:=tstatementnode(left);
  280. while assigned(hp) do
  281. begin
  282. if cs_regalloc in aktglobalswitches then
  283. begin
  284. { node transformations }
  285. { concat function result to exit }
  286. { this is wrong for string or other complex
  287. result types !!! }
  288. if {ret_in_acc(aktprocdef.rettype.def) and }
  289. (is_ordinal(aktprocdef.rettype.def) or
  290. is_smallset(aktprocdef.rettype.def)) and
  291. assigned(hp.left) and
  292. assigned(tstatementnode(hp.left).right) and
  293. (tstatementnode(hp.left).right.nodetype=exitn) and
  294. (hp.right.nodetype=assignn) and
  295. { !!!! this tbinarynode should be tassignmentnode }
  296. (tbinarynode(hp.right).left.nodetype=funcretn) then
  297. begin
  298. if assigned(texitnode(tstatementnode(hp.left).right).left) then
  299. CGMessage(cg_n_inefficient_code)
  300. else
  301. begin
  302. texitnode(tstatementnode(hp.left).right).left:=tstatementnode(hp.right).right;
  303. tstatementnode(hp.right).right:=nil;
  304. hp.right.free;
  305. hp.right:=nil;
  306. end;
  307. end
  308. { warning if unreachable code occurs and elimate this }
  309. else if (hp.right.nodetype in
  310. [exitn,breakn,continuen,goton]) and
  311. { statement node (JM) }
  312. assigned(hp.left) and
  313. { kind of statement! (JM) }
  314. assigned(tstatementnode(hp.left).right) and
  315. (tstatementnode(hp.left).right.nodetype<>labeln) then
  316. begin
  317. { use correct line number }
  318. aktfilepos:=hp.left.fileinfo;
  319. hp.left.free;
  320. hp.left:=nil;
  321. CGMessage(cg_w_unreachable_code);
  322. { old lines }
  323. aktfilepos:=hp.right.fileinfo;
  324. end;
  325. end;
  326. if assigned(hp.right) then
  327. begin
  328. {$ifdef newcg}
  329. tg.cleartempgen;
  330. {$else newcg}
  331. cleartempgen;
  332. {$endif newcg}
  333. codegenerror:=false;
  334. firstpass(hp.right);
  335. hp.registers32:=hp.right.registers32;
  336. hp.registersfpu:=hp.right.registersfpu;
  337. {$ifdef SUPPORT_MMX}
  338. hp.registersmmx:=hp.right.registersmmx;
  339. {$endif SUPPORT_MMX}
  340. end
  341. else
  342. hp.registers32:=0;
  343. if hp.registers32>registers32 then
  344. registers32:=hp.registers32;
  345. if hp.registersfpu>registersfpu then
  346. registersfpu:=hp.registersfpu;
  347. {$ifdef SUPPORT_MMX}
  348. if hp.registersmmx>registersmmx then
  349. registersmmx:=hp.registersmmx;
  350. {$endif}
  351. inc(count);
  352. hp:=tstatementnode(hp.left);
  353. end;
  354. end;
  355. {*****************************************************************************
  356. TASMNODE
  357. *****************************************************************************}
  358. constructor tasmnode.create(p : taasmoutput);
  359. begin
  360. inherited create(asmn);
  361. p_asm:=p;
  362. end;
  363. destructor tasmnode.destroy;
  364. begin
  365. if assigned(p_asm) then
  366. p_asm.free;
  367. inherited destroy;
  368. end;
  369. function tasmnode.getcopy: tnode;
  370. var
  371. n: tasmnode;
  372. begin
  373. n := tasmnode(inherited getcopy);
  374. if assigned(p_asm) then
  375. begin
  376. n.p_asm:=taasmoutput.create;
  377. n.p_asm.concatlistcopy(p_asm);
  378. end
  379. else n.p_asm := nil;
  380. getcopy := n;
  381. end;
  382. function tasmnode.det_resulttype:tnode;
  383. begin
  384. result:=nil;
  385. resulttype:=voidtype;
  386. end;
  387. function tasmnode.pass_1 : tnode;
  388. begin
  389. result:=nil;
  390. procinfo^.flags:=procinfo^.flags or pi_uses_asm;
  391. end;
  392. function tasmnode.docompare(p: tnode): boolean;
  393. begin
  394. { comparing of asmlists is not implemented (JM) }
  395. docompare := false;
  396. end;
  397. {*****************************************************************************
  398. TEMPCREATENODE
  399. *****************************************************************************}
  400. constructor ttempcreatenode.create(const _restype: ttype; _size: longint; _persistent: boolean);
  401. begin
  402. inherited create(tempn);
  403. size := _size;
  404. new(tempinfo);
  405. fillchar(tempinfo^,sizeof(tempinfo^),0);
  406. tempinfo^.restype := _restype;
  407. persistent := _persistent;
  408. end;
  409. function ttempcreatenode.getcopy: tnode;
  410. var
  411. n: ttempcreatenode;
  412. begin
  413. n := ttempcreatenode(inherited getcopy);
  414. n.size := size;
  415. new(n.tempinfo);
  416. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  417. n.tempinfo^.restype := tempinfo^.restype;
  418. { signal the temprefs that the temp they point to has been copied, }
  419. { so that if the refs get copied as well, they can hook themselves }
  420. { to the copy of the temp }
  421. tempinfo^.hookoncopy := n.tempinfo;
  422. result := n;
  423. end;
  424. function ttempcreatenode.pass_1 : tnode;
  425. begin
  426. result := nil;
  427. end;
  428. function ttempcreatenode.det_resulttype: tnode;
  429. begin
  430. result := nil;
  431. { a tempcreatenode doesn't have a resulttype, only temprefnodes do }
  432. resulttype := voidtype;
  433. end;
  434. function ttempcreatenode.docompare(p: tnode): boolean;
  435. begin
  436. result :=
  437. inherited docompare(p) and
  438. (ttempcreatenode(p).size = size) and
  439. is_equal(ttempcreatenode(p).tempinfo^.restype.def,tempinfo^.restype.def);
  440. end;
  441. {*****************************************************************************
  442. TEMPREFNODE
  443. *****************************************************************************}
  444. constructor ttemprefnode.create(const temp: ttempcreatenode);
  445. begin
  446. inherited create(temprefn);
  447. tempinfo := temp.tempinfo;
  448. end;
  449. function ttemprefnode.getcopy: tnode;
  450. var
  451. n: ttemprefnode;
  452. begin
  453. n := ttemprefnode(inherited getcopy);
  454. if assigned(tempinfo^.hookoncopy) then
  455. { if the temp has been copied, assume it becomes a new }
  456. { temp which has to be hooked by the copied reference }
  457. begin
  458. { hook the ref to the copied temp }
  459. n.tempinfo := tempinfo^.hookoncopy;
  460. end
  461. else
  462. { if the temp we refer to hasn't been copied, assume }
  463. { we're just a new reference to that temp }
  464. begin
  465. n.tempinfo := tempinfo;
  466. end;
  467. result := n;
  468. end;
  469. function ttemprefnode.pass_1 : tnode;
  470. begin
  471. result := nil;
  472. end;
  473. function ttemprefnode.det_resulttype: tnode;
  474. begin
  475. { check if the temp is already resulttype passed }
  476. if not assigned(tempinfo^.restype.def) then
  477. internalerror(200108233);
  478. result := nil;
  479. resulttype := tempinfo^.restype;
  480. end;
  481. function ttemprefnode.docompare(p: tnode): boolean;
  482. begin
  483. result :=
  484. inherited docompare(p) and
  485. (ttemprefnode(p).tempinfo = tempinfo);
  486. end;
  487. {*****************************************************************************
  488. TEMPDELETENODE
  489. *****************************************************************************}
  490. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  491. begin
  492. inherited create(temprefn);
  493. tempinfo := temp.tempinfo;
  494. end;
  495. function ttempdeletenode.getcopy: tnode;
  496. var
  497. n: ttempdeletenode;
  498. begin
  499. n := ttempdeletenode(inherited getcopy);
  500. if assigned(tempinfo^.hookoncopy) then
  501. { if the temp has been copied, assume it becomes a new }
  502. { temp which has to be hooked by the copied deletenode }
  503. begin
  504. { hook the tempdeletenode to the copied temp }
  505. n.tempinfo := tempinfo^.hookoncopy;
  506. end
  507. else
  508. { if the temp we refer to hasn't been copied, we have a }
  509. { problem since that means we now have two delete nodes }
  510. { for one temp }
  511. internalerror(200108234);
  512. result := n;
  513. end;
  514. function ttempdeletenode.pass_1 : tnode;
  515. begin
  516. result := nil;
  517. end;
  518. function ttempdeletenode.det_resulttype: tnode;
  519. begin
  520. result := nil;
  521. resulttype := voidtype;
  522. end;
  523. function ttempdeletenode.docompare(p: tnode): boolean;
  524. begin
  525. result :=
  526. inherited docompare(p) and
  527. (ttemprefnode(p).tempinfo = tempinfo);
  528. end;
  529. destructor ttempdeletenode.destroy;
  530. begin
  531. dispose(tempinfo);
  532. end;
  533. begin
  534. cnothingnode:=tnothingnode;
  535. cerrornode:=terrornode;
  536. casmnode:=tasmnode;
  537. cstatementnode:=tstatementnode;
  538. cblocknode:=tblocknode;
  539. ctempcreatenode:=ttempcreatenode;
  540. ctemprefnode:=ttemprefnode;
  541. ctempdeletenode:=ttempdeletenode;
  542. end.
  543. {
  544. $Log$
  545. Revision 1.18 2001-11-02 22:58:01 peter
  546. * procsym definition rewrite
  547. Revision 1.17 2001/09/02 21:12:06 peter
  548. * move class of definitions into type section for delphi
  549. Revision 1.16 2001/08/26 13:36:38 florian
  550. * some cg reorganisation
  551. * some PPC updates
  552. Revision 1.15 2001/08/24 13:47:26 jonas
  553. * moved "reverseparameters" from ninl.pas to ncal.pas
  554. + support for non-persistent temps in ttempcreatenode.create, for use
  555. with typeconversion nodes
  556. Revision 1.14 2001/08/23 14:28:35 jonas
  557. + tempcreate/ref/delete nodes (allows the use of temps in the
  558. resulttype and first pass)
  559. * made handling of read(ln)/write(ln) processor independent
  560. * moved processor independent handling for str and reset/rewrite-typed
  561. from firstpass to resulttype pass
  562. * changed names of helpers in text.inc to be generic for use as
  563. compilerprocs + added "iocheck" directive for most of them
  564. * reading of ordinals is done by procedures instead of functions
  565. because otherwise FPC_IOCHECK overwrote the result before it could
  566. be stored elsewhere (range checking still works)
  567. * compilerprocs can now be used in the system unit before they are
  568. implemented
  569. * added note to errore.msg that booleans can't be read using read/readln
  570. Revision 1.13 2001/08/06 21:40:46 peter
  571. * funcret moved from tprocinfo to tprocdef
  572. Revision 1.12 2001/06/11 17:41:12 jonas
  573. * fixed web bug 1501 in conjunction with -Or
  574. Revision 1.11 2001/05/18 22:31:06 peter
  575. * tasmnode.pass_2 is independent of cpu, moved to ncgbas
  576. * include ncgbas for independent nodes
  577. Revision 1.10 2001/04/13 01:22:08 peter
  578. * symtable change to classes
  579. * range check generation and errors fixed, make cycle DEBUG=1 works
  580. * memory leaks fixed
  581. Revision 1.9 2001/04/02 21:20:30 peter
  582. * resulttype rewrite
  583. Revision 1.8 2001/02/05 20:45:49 peter
  584. * fixed buf 1364
  585. Revision 1.7 2000/12/31 11:14:10 jonas
  586. + implemented/fixed docompare() mathods for all nodes (not tested)
  587. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  588. and constant strings/chars together
  589. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  590. when adding
  591. Revision 1.6 2000/12/25 00:07:26 peter
  592. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  593. tlinkedlist objects)
  594. Revision 1.5 2000/11/29 00:30:31 florian
  595. * unused units removed from uses clause
  596. * some changes for widestrings
  597. Revision 1.4 2000/10/31 22:02:47 peter
  598. * symtable splitted, no real code changes
  599. Revision 1.3 2000/10/27 14:57:16 jonas
  600. + implementation for tasmnode.getcopy
  601. Revision 1.2 2000/10/14 21:52:54 peter
  602. * fixed memory leaks
  603. Revision 1.1 2000/10/14 10:14:50 peter
  604. * moehrendorf oct 2000 rewrite
  605. }