nbas.pas 22 KB

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