nbas.pas 11 KB

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