nbas.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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,cgbase
  125. ;
  126. {*****************************************************************************
  127. TFIRSTNOTHING
  128. *****************************************************************************}
  129. constructor tnothingnode.create;
  130. begin
  131. inherited create(nothingn);
  132. end;
  133. function tnothingnode.det_resulttype:tnode;
  134. begin
  135. result:=nil;
  136. resulttype:=voidtype;
  137. end;
  138. function tnothingnode.pass_1 : tnode;
  139. begin
  140. result:=nil;
  141. end;
  142. {*****************************************************************************
  143. TFIRSTERROR
  144. *****************************************************************************}
  145. constructor terrornode.create;
  146. begin
  147. inherited create(errorn);
  148. end;
  149. function terrornode.det_resulttype:tnode;
  150. begin
  151. result:=nil;
  152. include(flags,nf_error);
  153. codegenerror:=true;
  154. resulttype:=generrortype;
  155. end;
  156. function terrornode.pass_1 : tnode;
  157. begin
  158. result:=nil;
  159. codegenerror:=true;
  160. end;
  161. {*****************************************************************************
  162. TSTATEMENTNODE
  163. *****************************************************************************}
  164. constructor tstatementnode.create(l,r : tnode);
  165. begin
  166. inherited create(statementn,l,r);
  167. end;
  168. function tstatementnode.det_resulttype:tnode;
  169. begin
  170. result:=nil;
  171. resulttype:=voidtype;
  172. { right is the statement itself calln assignn or a complex one }
  173. resulttypepass(right);
  174. if (not (cs_extsyntax in aktmoduleswitches)) and
  175. assigned(right.resulttype.def) and
  176. not((right.nodetype=calln) and
  177. (tcallnode(right).procdefinition.proctypeoption=potype_constructor)) and
  178. not(is_void(right.resulttype.def)) then
  179. CGMessage(cg_e_illegal_expression);
  180. if codegenerror then
  181. exit;
  182. { left is the next in the list }
  183. resulttypepass(left);
  184. if codegenerror then
  185. exit;
  186. end;
  187. function tstatementnode.pass_1 : tnode;
  188. begin
  189. result:=nil;
  190. { no temps over several statements }
  191. {$ifdef newcg}
  192. tg.cleartempgen;
  193. {$else newcg}
  194. cleartempgen;
  195. {$endif newcg}
  196. { right is the statement itself calln assignn or a complex one }
  197. firstpass(right);
  198. if codegenerror then
  199. exit;
  200. registers32:=right.registers32;
  201. registersfpu:=right.registersfpu;
  202. {$ifdef SUPPORT_MMX}
  203. registersmmx:=right.registersmmx;
  204. {$endif SUPPORT_MMX}
  205. { left is the next in the list }
  206. firstpass(left);
  207. if codegenerror then
  208. exit;
  209. if right.registers32>registers32 then
  210. registers32:=right.registers32;
  211. if right.registersfpu>registersfpu then
  212. registersfpu:=right.registersfpu;
  213. {$ifdef SUPPORT_MMX}
  214. if right.registersmmx>registersmmx then
  215. registersmmx:=right.registersmmx;
  216. {$endif}
  217. end;
  218. {$ifdef extdebug}
  219. procedure tstatementnode.dowrite;
  220. begin
  221. { can't use inherited dowrite, because that will use the
  222. binary which we don't want for statements }
  223. dowritenodetype;
  224. writeln(',');
  225. { write the statement }
  226. writenodeindention:=writenodeindention+' ';
  227. writenode(right);
  228. writeln(')');
  229. delete(writenodeindention,1,4);
  230. { go on with the next statement }
  231. writenode(left);
  232. end;
  233. {$endif}
  234. {*****************************************************************************
  235. TBLOCKNODE
  236. *****************************************************************************}
  237. constructor tblocknode.create(l : tnode);
  238. begin
  239. inherited create(blockn,l);
  240. end;
  241. function tblocknode.det_resulttype:tnode;
  242. var
  243. hp : tstatementnode;
  244. begin
  245. result:=nil;
  246. resulttype:=voidtype;
  247. hp:=tstatementnode(left);
  248. while assigned(hp) do
  249. begin
  250. if assigned(hp.right) then
  251. begin
  252. codegenerror:=false;
  253. resulttypepass(hp.right);
  254. if (not (cs_extsyntax in aktmoduleswitches)) and
  255. assigned(hp.right.resulttype.def) and
  256. not((hp.right.nodetype=calln) and
  257. (tcallnode(hp.right).procdefinition.proctypeoption=potype_constructor)) and
  258. not(is_void(hp.right.resulttype.def)) then
  259. CGMessage(cg_e_illegal_expression);
  260. end;
  261. hp:=tstatementnode(hp.left);
  262. end;
  263. end;
  264. function tblocknode.pass_1 : tnode;
  265. var
  266. hp : tstatementnode;
  267. count : longint;
  268. begin
  269. result:=nil;
  270. count:=0;
  271. hp:=tstatementnode(left);
  272. while assigned(hp) do
  273. begin
  274. if cs_regalloc in aktglobalswitches then
  275. begin
  276. { node transformations }
  277. { concat function result to exit }
  278. { this is wrong for string or other complex
  279. result types !!! }
  280. if {ret_in_acc(aktprocsym.definition.rettype.def) and }
  281. (is_ordinal(aktprocsym.definition.rettype.def) or
  282. is_smallset(aktprocsym.definition.rettype.def)) and
  283. assigned(hp.left) and
  284. assigned(tstatementnode(hp.left).right) and
  285. (tstatementnode(hp.left).right.nodetype=exitn) and
  286. (hp.right.nodetype=assignn) and
  287. { !!!! this tbinarynode should be tassignmentnode }
  288. (tbinarynode(hp.right).left.nodetype=funcretn) then
  289. begin
  290. if assigned(texitnode(tstatementnode(hp.left).right).left) then
  291. CGMessage(cg_n_inefficient_code)
  292. else
  293. begin
  294. texitnode(tstatementnode(hp.left).right).left:=tstatementnode(hp.right).right;
  295. tstatementnode(hp.right).right:=nil;
  296. hp.right.free;
  297. hp.right:=nil;
  298. end;
  299. end
  300. { warning if unreachable code occurs and elimate this }
  301. else if (hp.right.nodetype in
  302. [exitn,breakn,continuen,goton]) and
  303. { statement node (JM) }
  304. assigned(hp.left) and
  305. { kind of statement! (JM) }
  306. assigned(tstatementnode(hp.left).right) and
  307. (tstatementnode(hp.left).right.nodetype<>labeln) then
  308. begin
  309. { use correct line number }
  310. aktfilepos:=hp.left.fileinfo;
  311. hp.left.free;
  312. hp.left:=nil;
  313. CGMessage(cg_w_unreachable_code);
  314. { old lines }
  315. aktfilepos:=hp.right.fileinfo;
  316. end;
  317. end;
  318. if assigned(hp.right) then
  319. begin
  320. {$ifdef newcg}
  321. tg.cleartempgen;
  322. {$else newcg}
  323. cleartempgen;
  324. {$endif newcg}
  325. codegenerror:=false;
  326. firstpass(hp.right);
  327. hp.registers32:=hp.right.registers32;
  328. hp.registersfpu:=hp.right.registersfpu;
  329. {$ifdef SUPPORT_MMX}
  330. hp.registersmmx:=hp.right.registersmmx;
  331. {$endif SUPPORT_MMX}
  332. end
  333. else
  334. hp.registers32:=0;
  335. if hp.registers32>registers32 then
  336. registers32:=hp.registers32;
  337. if hp.registersfpu>registersfpu then
  338. registersfpu:=hp.registersfpu;
  339. {$ifdef SUPPORT_MMX}
  340. if hp.registersmmx>registersmmx then
  341. registersmmx:=hp.registersmmx;
  342. {$endif}
  343. inc(count);
  344. hp:=tstatementnode(hp.left);
  345. end;
  346. end;
  347. {*****************************************************************************
  348. TASMNODE
  349. *****************************************************************************}
  350. constructor tasmnode.create(p : taasmoutput);
  351. begin
  352. inherited create(asmn);
  353. p_asm:=p;
  354. end;
  355. destructor tasmnode.destroy;
  356. begin
  357. if assigned(p_asm) then
  358. p_asm.free;
  359. inherited destroy;
  360. end;
  361. function tasmnode.getcopy: tnode;
  362. var
  363. n: tasmnode;
  364. begin
  365. n := tasmnode(inherited getcopy);
  366. if assigned(p_asm) then
  367. begin
  368. n.p_asm:=taasmoutput.create;
  369. n.p_asm.concatlistcopy(p_asm);
  370. end
  371. else n.p_asm := nil;
  372. getcopy := n;
  373. end;
  374. function tasmnode.det_resulttype:tnode;
  375. begin
  376. result:=nil;
  377. resulttype:=voidtype;
  378. end;
  379. function tasmnode.pass_1 : tnode;
  380. begin
  381. result:=nil;
  382. procinfo^.flags:=procinfo^.flags or pi_uses_asm;
  383. end;
  384. function tasmnode.docompare(p: tnode): boolean;
  385. begin
  386. { comparing of asmlists is not implemented (JM) }
  387. docompare := false;
  388. end;
  389. {*****************************************************************************
  390. TEMPCREATENODE
  391. *****************************************************************************}
  392. constructor ttempcreatenode.create(const _restype: ttype; _size: longint; _persistent: boolean);
  393. begin
  394. inherited create(tempn);
  395. size := _size;
  396. new(tempinfo);
  397. fillchar(tempinfo^,sizeof(tempinfo^),0);
  398. tempinfo^.restype := _restype;
  399. persistent := _persistent;
  400. end;
  401. function ttempcreatenode.getcopy: tnode;
  402. var
  403. n: ttempcreatenode;
  404. begin
  405. n := ttempcreatenode(inherited getcopy);
  406. n.size := size;
  407. new(n.tempinfo);
  408. fillchar(n.tempinfo^,sizeof(n.tempinfo^),0);
  409. n.tempinfo^.restype := tempinfo^.restype;
  410. { signal the temprefs that the temp they point to has been copied, }
  411. { so that if the refs get copied as well, they can hook themselves }
  412. { to the copy of the temp }
  413. tempinfo^.hookoncopy := n.tempinfo;
  414. result := n;
  415. end;
  416. function ttempcreatenode.pass_1 : tnode;
  417. begin
  418. result := nil;
  419. end;
  420. function ttempcreatenode.det_resulttype: tnode;
  421. begin
  422. result := nil;
  423. { a tempcreatenode doesn't have a resulttype, only temprefnodes do }
  424. resulttype := voidtype;
  425. end;
  426. function ttempcreatenode.docompare(p: tnode): boolean;
  427. begin
  428. result :=
  429. inherited docompare(p) and
  430. (ttempcreatenode(p).size = size) and
  431. is_equal(ttempcreatenode(p).tempinfo^.restype.def,tempinfo^.restype.def);
  432. end;
  433. {*****************************************************************************
  434. TEMPREFNODE
  435. *****************************************************************************}
  436. constructor ttemprefnode.create(const temp: ttempcreatenode);
  437. begin
  438. inherited create(temprefn);
  439. tempinfo := temp.tempinfo;
  440. end;
  441. function ttemprefnode.getcopy: tnode;
  442. var
  443. n: ttemprefnode;
  444. begin
  445. n := ttemprefnode(inherited getcopy);
  446. if assigned(tempinfo^.hookoncopy) then
  447. { if the temp has been copied, assume it becomes a new }
  448. { temp which has to be hooked by the copied reference }
  449. begin
  450. { hook the ref to the copied temp }
  451. n.tempinfo := tempinfo^.hookoncopy;
  452. end
  453. else
  454. { if the temp we refer to hasn't been copied, assume }
  455. { we're just a new reference to that temp }
  456. begin
  457. n.tempinfo := tempinfo;
  458. end;
  459. result := n;
  460. end;
  461. function ttemprefnode.pass_1 : tnode;
  462. begin
  463. result := nil;
  464. end;
  465. function ttemprefnode.det_resulttype: tnode;
  466. begin
  467. { check if the temp is already resulttype passed }
  468. if not assigned(tempinfo^.restype.def) then
  469. internalerror(200108233);
  470. result := nil;
  471. resulttype := tempinfo^.restype;
  472. end;
  473. function ttemprefnode.docompare(p: tnode): boolean;
  474. begin
  475. result :=
  476. inherited docompare(p) and
  477. (ttemprefnode(p).tempinfo = tempinfo);
  478. end;
  479. {*****************************************************************************
  480. TEMPDELETENODE
  481. *****************************************************************************}
  482. constructor ttempdeletenode.create(const temp: ttempcreatenode);
  483. begin
  484. inherited create(temprefn);
  485. tempinfo := temp.tempinfo;
  486. end;
  487. function ttempdeletenode.getcopy: tnode;
  488. var
  489. n: ttempdeletenode;
  490. begin
  491. n := ttempdeletenode(inherited getcopy);
  492. if assigned(tempinfo^.hookoncopy) then
  493. { if the temp has been copied, assume it becomes a new }
  494. { temp which has to be hooked by the copied deletenode }
  495. begin
  496. { hook the tempdeletenode to the copied temp }
  497. n.tempinfo := tempinfo^.hookoncopy;
  498. end
  499. else
  500. { if the temp we refer to hasn't been copied, we have a }
  501. { problem since that means we now have two delete nodes }
  502. { for one temp }
  503. internalerror(200108234);
  504. result := n;
  505. end;
  506. function ttempdeletenode.pass_1 : tnode;
  507. begin
  508. result := nil;
  509. end;
  510. function ttempdeletenode.det_resulttype: tnode;
  511. begin
  512. result := nil;
  513. resulttype := voidtype;
  514. end;
  515. function ttempdeletenode.docompare(p: tnode): boolean;
  516. begin
  517. result :=
  518. inherited docompare(p) and
  519. (ttemprefnode(p).tempinfo = tempinfo);
  520. end;
  521. destructor ttempdeletenode.destroy;
  522. begin
  523. dispose(tempinfo);
  524. end;
  525. begin
  526. cnothingnode:=tnothingnode;
  527. cerrornode:=terrornode;
  528. casmnode:=tasmnode;
  529. cstatementnode:=tstatementnode;
  530. cblocknode:=tblocknode;
  531. ctempcreatenode:=ttempcreatenode;
  532. ctemprefnode:=ttemprefnode;
  533. ctempdeletenode:=ttempdeletenode;
  534. end.
  535. {
  536. $Log$
  537. Revision 1.16 2001-08-26 13:36:38 florian
  538. * some cg reorganisation
  539. * some PPC updates
  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. }