nbas.pas 11 KB

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