nbas.pas 11 KB

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