nbas.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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,node;
  23. type
  24. tnothingnode = class(tnode)
  25. constructor create;virtual;
  26. function pass_1 : tnode;override;
  27. function det_resulttype:tnode;override;
  28. procedure pass_2;override;
  29. end;
  30. terrornode = class(tnode)
  31. constructor create;virtual;
  32. function pass_1 : tnode;override;
  33. function det_resulttype:tnode;override;
  34. end;
  35. tasmnode = class(tnode)
  36. p_asm : taasmoutput;
  37. constructor create(p : taasmoutput);virtual;
  38. destructor destroy;override;
  39. function getcopy : tnode;override;
  40. function pass_1 : tnode;override;
  41. function det_resulttype:tnode;override;
  42. function docompare(p: tnode): boolean; override;
  43. end;
  44. tstatementnode = class(tbinarynode)
  45. constructor create(l,r : tnode);virtual;
  46. function pass_1 : tnode;override;
  47. function det_resulttype:tnode;override;
  48. {$ifdef extdebug}
  49. procedure dowrite;override;
  50. {$endif extdebug}
  51. end;
  52. tblocknode = class(tunarynode)
  53. constructor create(l : tnode);virtual;
  54. function pass_1 : tnode;override;
  55. function det_resulttype:tnode;override;
  56. end;
  57. var
  58. cnothingnode : class of tnothingnode;
  59. cerrornode : class of terrornode;
  60. casmnode : class of tasmnode;
  61. cstatementnode : class of tstatementnode;
  62. cblocknode : class of tblocknode;
  63. implementation
  64. uses
  65. cutils,
  66. verbose,globals,globtype,systems,
  67. symconst,symdef,types,
  68. pass_1,
  69. ncal,nflw,tgcpu,hcodegen
  70. {$ifdef newcg}
  71. ,cgbase
  72. {$endif}
  73. ;
  74. {*****************************************************************************
  75. TFIRSTNOTHING
  76. *****************************************************************************}
  77. constructor tnothingnode.create;
  78. begin
  79. inherited create(nothingn);
  80. end;
  81. function tnothingnode.det_resulttype:tnode;
  82. begin
  83. result:=nil;
  84. resulttype:=voidtype;
  85. end;
  86. function tnothingnode.pass_1 : tnode;
  87. begin
  88. result:=nil;
  89. end;
  90. procedure tnothingnode.pass_2;
  91. begin
  92. { avoid an abstract rte }
  93. end;
  94. {*****************************************************************************
  95. TFIRSTERROR
  96. *****************************************************************************}
  97. constructor terrornode.create;
  98. begin
  99. inherited create(errorn);
  100. end;
  101. function terrornode.det_resulttype:tnode;
  102. begin
  103. result:=nil;
  104. include(flags,nf_error);
  105. codegenerror:=true;
  106. resulttype:=generrortype;
  107. end;
  108. function terrornode.pass_1 : tnode;
  109. begin
  110. result:=nil;
  111. codegenerror:=true;
  112. end;
  113. {*****************************************************************************
  114. TSTATEMENTNODE
  115. *****************************************************************************}
  116. constructor tstatementnode.create(l,r : tnode);
  117. begin
  118. inherited create(statementn,l,r);
  119. end;
  120. function tstatementnode.det_resulttype:tnode;
  121. begin
  122. result:=nil;
  123. resulttype:=voidtype;
  124. { right is the statement itself calln assignn or a complex one }
  125. resulttypepass(right);
  126. if (not (cs_extsyntax in aktmoduleswitches)) and
  127. assigned(right.resulttype.def) and
  128. not((right.nodetype=calln) and
  129. (tcallnode(right).procdefinition.proctypeoption=potype_constructor)) and
  130. not(is_void(right.resulttype.def)) then
  131. CGMessage(cg_e_illegal_expression);
  132. if codegenerror then
  133. exit;
  134. { left is the next in the list }
  135. resulttypepass(left);
  136. if codegenerror then
  137. exit;
  138. end;
  139. function tstatementnode.pass_1 : tnode;
  140. begin
  141. result:=nil;
  142. { no temps over several statements }
  143. {$ifdef newcg}
  144. tg.cleartempgen;
  145. {$else newcg}
  146. cleartempgen;
  147. {$endif newcg}
  148. { right is the statement itself calln assignn or a complex one }
  149. firstpass(right);
  150. if codegenerror then
  151. exit;
  152. registers32:=right.registers32;
  153. registersfpu:=right.registersfpu;
  154. {$ifdef SUPPORT_MMX}
  155. registersmmx:=right.registersmmx;
  156. {$endif SUPPORT_MMX}
  157. { left is the next in the list }
  158. firstpass(left);
  159. if codegenerror then
  160. exit;
  161. if right.registers32>registers32 then
  162. registers32:=right.registers32;
  163. if right.registersfpu>registersfpu then
  164. registersfpu:=right.registersfpu;
  165. {$ifdef SUPPORT_MMX}
  166. if right.registersmmx>registersmmx then
  167. registersmmx:=right.registersmmx;
  168. {$endif}
  169. end;
  170. {$ifdef extdebug}
  171. procedure tstatementnode.dowrite;
  172. begin
  173. { can't use inherited dowrite, because that will use the
  174. binary which we don't want for statements }
  175. dowritenodetype;
  176. writeln(',');
  177. { write the statement }
  178. writenodeindention:=writenodeindention+' ';
  179. writenode(right);
  180. writeln(')');
  181. delete(writenodeindention,1,4);
  182. { go on with the next statement }
  183. writenode(left);
  184. end;
  185. {$endif}
  186. {*****************************************************************************
  187. TBLOCKNODE
  188. *****************************************************************************}
  189. constructor tblocknode.create(l : tnode);
  190. begin
  191. inherited create(blockn,l);
  192. end;
  193. function tblocknode.det_resulttype:tnode;
  194. var
  195. hp : tstatementnode;
  196. begin
  197. result:=nil;
  198. resulttype:=voidtype;
  199. hp:=tstatementnode(left);
  200. while assigned(hp) do
  201. begin
  202. if assigned(hp.right) then
  203. begin
  204. codegenerror:=false;
  205. resulttypepass(hp.right);
  206. if (not (cs_extsyntax in aktmoduleswitches)) and
  207. assigned(hp.right.resulttype.def) and
  208. not((hp.right.nodetype=calln) and
  209. (tcallnode(hp.right).procdefinition.proctypeoption=potype_constructor)) and
  210. not(is_void(hp.right.resulttype.def)) then
  211. CGMessage(cg_e_illegal_expression);
  212. end;
  213. hp:=tstatementnode(hp.left);
  214. end;
  215. end;
  216. function tblocknode.pass_1 : tnode;
  217. var
  218. hp : tstatementnode;
  219. count : longint;
  220. begin
  221. result:=nil;
  222. count:=0;
  223. hp:=tstatementnode(left);
  224. while assigned(hp) do
  225. begin
  226. if cs_regalloc in aktglobalswitches then
  227. begin
  228. { node transformations }
  229. { concat function result to exit }
  230. { this is wrong for string or other complex
  231. result types !!! }
  232. if ret_in_acc(procinfo^.returntype.def) and
  233. assigned(hp.left) and
  234. assigned(tstatementnode(hp.left).right) and
  235. (tstatementnode(hp.left).right.nodetype=exitn) and
  236. (hp.right.nodetype=assignn) and
  237. { !!!! this tbinarynode should be tassignmentnode }
  238. (tbinarynode(hp.right).left.nodetype=funcretn) then
  239. begin
  240. if assigned(texitnode(tstatementnode(hp.left).right).left) then
  241. CGMessage(cg_n_inefficient_code)
  242. else
  243. begin
  244. texitnode(tstatementnode(hp.left).right).left:=tstatementnode(hp.right).right;
  245. tstatementnode(hp.right).right:=nil;
  246. hp.right.free;
  247. hp.right:=nil;
  248. end;
  249. end
  250. { warning if unreachable code occurs and elimate this }
  251. else if (hp.right.nodetype in
  252. [exitn,breakn,continuen,goton]) and
  253. { statement node (JM) }
  254. assigned(hp.left) and
  255. { kind of statement! (JM) }
  256. assigned(tstatementnode(hp.left).right) and
  257. (tstatementnode(hp.left).right.nodetype<>labeln) then
  258. begin
  259. { use correct line number }
  260. aktfilepos:=hp.left.fileinfo;
  261. hp.left.free;
  262. hp.left:=nil;
  263. CGMessage(cg_w_unreachable_code);
  264. { old lines }
  265. aktfilepos:=hp.right.fileinfo;
  266. end;
  267. end;
  268. if assigned(hp.right) then
  269. begin
  270. {$ifdef newcg}
  271. tg.cleartempgen;
  272. {$else newcg}
  273. cleartempgen;
  274. {$endif newcg}
  275. codegenerror:=false;
  276. firstpass(hp.right);
  277. hp.registers32:=hp.right.registers32;
  278. hp.registersfpu:=hp.right.registersfpu;
  279. {$ifdef SUPPORT_MMX}
  280. hp.registersmmx:=hp.right.registersmmx;
  281. {$endif SUPPORT_MMX}
  282. end
  283. else
  284. hp.registers32:=0;
  285. if hp.registers32>registers32 then
  286. registers32:=hp.registers32;
  287. if hp.registersfpu>registersfpu then
  288. registersfpu:=hp.registersfpu;
  289. {$ifdef SUPPORT_MMX}
  290. if hp.registersmmx>registersmmx then
  291. registersmmx:=hp.registersmmx;
  292. {$endif}
  293. inc(count);
  294. hp:=tstatementnode(hp.left);
  295. end;
  296. end;
  297. {*****************************************************************************
  298. TASMNODE
  299. *****************************************************************************}
  300. constructor tasmnode.create(p : taasmoutput);
  301. begin
  302. inherited create(asmn);
  303. p_asm:=p;
  304. end;
  305. destructor tasmnode.destroy;
  306. begin
  307. if assigned(p_asm) then
  308. p_asm.free;
  309. inherited destroy;
  310. end;
  311. function tasmnode.getcopy: tnode;
  312. var
  313. n: tasmnode;
  314. begin
  315. n := tasmnode(inherited getcopy);
  316. if assigned(p_asm) then
  317. begin
  318. n.p_asm:=taasmoutput.create;
  319. n.p_asm.concatlistcopy(p_asm);
  320. end
  321. else n.p_asm := nil;
  322. getcopy := n;
  323. end;
  324. function tasmnode.det_resulttype:tnode;
  325. begin
  326. result:=nil;
  327. resulttype:=voidtype;
  328. end;
  329. function tasmnode.pass_1 : tnode;
  330. begin
  331. result:=nil;
  332. procinfo^.flags:=procinfo^.flags or pi_uses_asm;
  333. end;
  334. function tasmnode.docompare(p: tnode): boolean;
  335. begin
  336. { comparing of asmlists is not implemented (JM) }
  337. docompare := false;
  338. end;
  339. begin
  340. cnothingnode:=tnothingnode;
  341. cerrornode:=terrornode;
  342. casmnode:=tasmnode;
  343. cstatementnode:=tstatementnode;
  344. cblocknode:=tblocknode;
  345. end.
  346. {
  347. $Log$
  348. Revision 1.10 2001-04-13 01:22:08 peter
  349. * symtable change to classes
  350. * range check generation and errors fixed, make cycle DEBUG=1 works
  351. * memory leaks fixed
  352. Revision 1.9 2001/04/02 21:20:30 peter
  353. * resulttype rewrite
  354. Revision 1.8 2001/02/05 20:45:49 peter
  355. * fixed buf 1364
  356. Revision 1.7 2000/12/31 11:14:10 jonas
  357. + implemented/fixed docompare() mathods for all nodes (not tested)
  358. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  359. and constant strings/chars together
  360. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  361. when adding
  362. Revision 1.6 2000/12/25 00:07:26 peter
  363. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  364. tlinkedlist objects)
  365. Revision 1.5 2000/11/29 00:30:31 florian
  366. * unused units removed from uses clause
  367. * some changes for widestrings
  368. Revision 1.4 2000/10/31 22:02:47 peter
  369. * symtable splitted, no real code changes
  370. Revision 1.3 2000/10/27 14:57:16 jonas
  371. + implementation for tasmnode.getcopy
  372. Revision 1.2 2000/10/14 21:52:54 peter
  373. * fixed memory leaks
  374. Revision 1.1 2000/10/14 10:14:50 peter
  375. * moehrendorf oct 2000 rewrite
  376. }