nbas.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. procedure pass_2;override;
  28. end;
  29. terrornode = class(tnode)
  30. constructor create;virtual;
  31. function pass_1 : tnode;override;
  32. end;
  33. tasmnode = class(tnode)
  34. p_asm : paasmoutput;
  35. constructor create(p : paasmoutput);virtual;
  36. destructor destroy;override;
  37. function pass_1 : tnode;override;
  38. end;
  39. tstatementnode = class(tbinarynode)
  40. constructor create(l,r : tnode);virtual;
  41. function pass_1 : tnode;override;
  42. {$ifdef extdebug}
  43. procedure dowrite;override;
  44. {$endif extdebug}
  45. end;
  46. tblocknode = class(tunarynode)
  47. constructor create(l : tnode);virtual;
  48. function pass_1 : tnode;override;
  49. end;
  50. var
  51. cnothingnode : class of tnothingnode;
  52. cerrornode : class of terrornode;
  53. casmnode : class of tasmnode;
  54. cstatementnode : class of tstatementnode;
  55. cblocknode : class of tblocknode;
  56. implementation
  57. uses
  58. globtype,systems,
  59. cutils,cobjects,verbose,globals,
  60. symtable,types,
  61. htypechk,
  62. cpubase,cpuasm,
  63. pass_1,
  64. nflw
  65. {$ifdef newcg}
  66. ,cgbase
  67. ,tgcpu
  68. {$else newcg}
  69. ,hcodegen
  70. {$ifdef i386}
  71. ,tgeni386
  72. {$endif}
  73. {$ifdef m68k}
  74. ,tgen68k
  75. {$endif}
  76. {$endif}
  77. ;
  78. {*****************************************************************************
  79. TFIRSTNOTHING
  80. *****************************************************************************}
  81. constructor tnothingnode.create;
  82. begin
  83. inherited create(nothingn);
  84. end;
  85. function tnothingnode.pass_1 : tnode;
  86. begin
  87. pass_1:=nil;
  88. resulttype:=voiddef;
  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.pass_1 : tnode;
  102. begin
  103. pass_1:=nil;
  104. include(flags,nf_error);
  105. codegenerror:=true;
  106. resulttype:=generrordef;
  107. end;
  108. {*****************************************************************************
  109. TSTATEMENTNODE
  110. *****************************************************************************}
  111. constructor tstatementnode.create(l,r : tnode);
  112. begin
  113. inherited create(statementn,l,r);
  114. end;
  115. function tstatementnode.pass_1 : tnode;
  116. begin
  117. pass_1:=nil;
  118. { left is the next statement in the list }
  119. resulttype:=voiddef;
  120. { no temps over several statements }
  121. {$ifdef newcg}
  122. tg.cleartempgen;
  123. {$else newcg}
  124. cleartempgen;
  125. {$endif newcg}
  126. { right is the statement itself calln assignn or a complex one }
  127. {must_be_valid:=true; obsolete PM }
  128. firstpass(right);
  129. if (not (cs_extsyntax in aktmoduleswitches)) and
  130. assigned(right.resulttype) and
  131. (right.resulttype<>pdef(voiddef)) then
  132. CGMessage(cg_e_illegal_expression);
  133. if codegenerror then
  134. exit;
  135. registers32:=right.registers32;
  136. registersfpu:=right.registersfpu;
  137. {$ifdef SUPPORT_MMX}
  138. registersmmx:=right.registersmmx;
  139. {$endif SUPPORT_MMX}
  140. { left is the next in the list }
  141. firstpass(left);
  142. if codegenerror then
  143. exit;
  144. if right.registers32>registers32 then
  145. registers32:=right.registers32;
  146. if right.registersfpu>registersfpu then
  147. registersfpu:=right.registersfpu;
  148. {$ifdef SUPPORT_MMX}
  149. if right.registersmmx>registersmmx then
  150. registersmmx:=right.registersmmx;
  151. {$endif}
  152. end;
  153. {$ifdef extdebug}
  154. procedure tstatementnode.dowrite;
  155. begin
  156. { can't use inherited dowrite, because that will use the
  157. binary which we don't want for statements }
  158. dowritenodetype;
  159. writeln(',');
  160. { write the statement }
  161. writenodeindention:=writenodeindention+' ';
  162. writenode(right);
  163. writeln(')');
  164. delete(writenodeindention,1,4);
  165. { go on with the next statement }
  166. writenode(left);
  167. end;
  168. {$endif}
  169. {*****************************************************************************
  170. TBLOCKNODE
  171. *****************************************************************************}
  172. constructor tblocknode.create(l : tnode);
  173. begin
  174. inherited create(blockn,l);
  175. end;
  176. function tblocknode.pass_1 : tnode;
  177. var
  178. hp : tstatementnode;
  179. count : longint;
  180. begin
  181. pass_1:=nil;
  182. count:=0;
  183. hp:=tstatementnode(left);
  184. while assigned(hp) do
  185. begin
  186. if cs_regalloc in aktglobalswitches then
  187. begin
  188. { node transformations }
  189. { concat function result to exit }
  190. { this is wrong for string or other complex
  191. result types !!! }
  192. if ret_in_acc(procinfo^.returntype.def) and
  193. assigned(hp.left) and
  194. assigned(tstatementnode(hp.left).right) and
  195. (tstatementnode(hp.left).right.nodetype=exitn) and
  196. (hp.right.nodetype=assignn) and
  197. { !!!! this tbinarynode should be tassignmentnode }
  198. (tbinarynode(hp.right).left.nodetype=funcretn) then
  199. begin
  200. if assigned(texitnode(tstatementnode(hp.left).right).left) then
  201. CGMessage(cg_n_inefficient_code)
  202. else
  203. begin
  204. texitnode(tstatementnode(hp.left).right).left:=tstatementnode(hp.right).right;
  205. tstatementnode(hp.right).right:=nil;
  206. hp.right.free;
  207. hp.right:=nil;
  208. end;
  209. end
  210. { warning if unreachable code occurs and elimate this }
  211. else if (hp.right.nodetype in
  212. [exitn,breakn,continuen,goton]) and
  213. { statement node (JM) }
  214. assigned(hp.left) and
  215. { kind of statement! (JM) }
  216. assigned(tstatementnode(hp.left).right) and
  217. (tstatementnode(hp.left).right.nodetype<>labeln) then
  218. begin
  219. { use correct line number }
  220. aktfilepos:=hp.left.fileinfo;
  221. hp.left.free;
  222. hp.left:=nil;
  223. CGMessage(cg_w_unreachable_code);
  224. { old lines }
  225. aktfilepos:=hp.right.fileinfo;
  226. end;
  227. end;
  228. if assigned(hp.right) then
  229. begin
  230. {$ifdef newcg}
  231. tg.cleartempgen;
  232. {$else newcg}
  233. cleartempgen;
  234. {$endif newcg}
  235. codegenerror:=false;
  236. firstpass(hp.right);
  237. if (not (cs_extsyntax in aktmoduleswitches)) and
  238. assigned(hp.right.resulttype) and
  239. (hp.right.resulttype<>pdef(voiddef)) then
  240. CGMessage(cg_e_illegal_expression);
  241. {if codegenerror then
  242. exit;}
  243. hp.registers32:=hp.right.registers32;
  244. hp.registersfpu:=hp.right.registersfpu;
  245. {$ifdef SUPPORT_MMX}
  246. hp.registersmmx:=hp.right.registersmmx;
  247. {$endif SUPPORT_MMX}
  248. end
  249. else
  250. hp.registers32:=0;
  251. if hp.registers32>registers32 then
  252. registers32:=hp.registers32;
  253. if hp.registersfpu>registersfpu then
  254. registersfpu:=hp.registersfpu;
  255. {$ifdef SUPPORT_MMX}
  256. if hp.registersmmx>registersmmx then
  257. registersmmx:=hp.registersmmx;
  258. {$endif}
  259. inc(count);
  260. hp:=tstatementnode(hp.left);
  261. end;
  262. end;
  263. {*****************************************************************************
  264. TASMNODE
  265. *****************************************************************************}
  266. constructor tasmnode.create(p : paasmoutput);
  267. begin
  268. inherited create(asmn);
  269. p_asm:=p;
  270. end;
  271. destructor tasmnode.destroy;
  272. begin
  273. if assigned(p_asm) then
  274. dispose(p_asm,done);
  275. inherited destroy;
  276. end;
  277. function tasmnode.pass_1 : tnode;
  278. begin
  279. pass_1:=nil;
  280. procinfo^.flags:=procinfo^.flags or pi_uses_asm;
  281. end;
  282. begin
  283. cnothingnode:=tnothingnode;
  284. cerrornode:=terrornode;
  285. casmnode:=tasmnode;
  286. cstatementnode:=tstatementnode;
  287. cblocknode:=tblocknode;
  288. end.
  289. {
  290. $Log$
  291. Revision 1.2 2000-10-14 21:52:54 peter
  292. * fixed memory leaks
  293. Revision 1.1 2000/10/14 10:14:50 peter
  294. * moehrendorf oct 2000 rewrite
  295. }