nbas.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. symconst,symtype,symdef,types,
  63. pass_1,
  64. ncal,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. not((right.nodetype=calln) and
  123. (tcallnode(right).procdefinition^.proctypeoption=potype_constructor)) and
  124. (right.resulttype<>pdef(voiddef)) then
  125. CGMessage(cg_e_illegal_expression);
  126. if codegenerror then
  127. exit;
  128. registers32:=right.registers32;
  129. registersfpu:=right.registersfpu;
  130. {$ifdef SUPPORT_MMX}
  131. registersmmx:=right.registersmmx;
  132. {$endif SUPPORT_MMX}
  133. { left is the next in the list }
  134. firstpass(left);
  135. if codegenerror then
  136. exit;
  137. if right.registers32>registers32 then
  138. registers32:=right.registers32;
  139. if right.registersfpu>registersfpu then
  140. registersfpu:=right.registersfpu;
  141. {$ifdef SUPPORT_MMX}
  142. if right.registersmmx>registersmmx then
  143. registersmmx:=right.registersmmx;
  144. {$endif}
  145. end;
  146. {$ifdef extdebug}
  147. procedure tstatementnode.dowrite;
  148. begin
  149. { can't use inherited dowrite, because that will use the
  150. binary which we don't want for statements }
  151. dowritenodetype;
  152. writeln(',');
  153. { write the statement }
  154. writenodeindention:=writenodeindention+' ';
  155. writenode(right);
  156. writeln(')');
  157. delete(writenodeindention,1,4);
  158. { go on with the next statement }
  159. writenode(left);
  160. end;
  161. {$endif}
  162. {*****************************************************************************
  163. TBLOCKNODE
  164. *****************************************************************************}
  165. constructor tblocknode.create(l : tnode);
  166. begin
  167. inherited create(blockn,l);
  168. end;
  169. function tblocknode.pass_1 : tnode;
  170. var
  171. hp : tstatementnode;
  172. count : longint;
  173. begin
  174. pass_1:=nil;
  175. count:=0;
  176. hp:=tstatementnode(left);
  177. while assigned(hp) do
  178. begin
  179. if cs_regalloc in aktglobalswitches then
  180. begin
  181. { node transformations }
  182. { concat function result to exit }
  183. { this is wrong for string or other complex
  184. result types !!! }
  185. if ret_in_acc(procinfo^.returntype.def) and
  186. assigned(hp.left) and
  187. assigned(tstatementnode(hp.left).right) and
  188. (tstatementnode(hp.left).right.nodetype=exitn) and
  189. (hp.right.nodetype=assignn) and
  190. { !!!! this tbinarynode should be tassignmentnode }
  191. (tbinarynode(hp.right).left.nodetype=funcretn) then
  192. begin
  193. if assigned(texitnode(tstatementnode(hp.left).right).left) then
  194. CGMessage(cg_n_inefficient_code)
  195. else
  196. begin
  197. texitnode(tstatementnode(hp.left).right).left:=tstatementnode(hp.right).right;
  198. tstatementnode(hp.right).right:=nil;
  199. hp.right.free;
  200. hp.right:=nil;
  201. end;
  202. end
  203. { warning if unreachable code occurs and elimate this }
  204. else if (hp.right.nodetype in
  205. [exitn,breakn,continuen,goton]) and
  206. { statement node (JM) }
  207. assigned(hp.left) and
  208. { kind of statement! (JM) }
  209. assigned(tstatementnode(hp.left).right) and
  210. (tstatementnode(hp.left).right.nodetype<>labeln) then
  211. begin
  212. { use correct line number }
  213. aktfilepos:=hp.left.fileinfo;
  214. hp.left.free;
  215. hp.left:=nil;
  216. CGMessage(cg_w_unreachable_code);
  217. { old lines }
  218. aktfilepos:=hp.right.fileinfo;
  219. end;
  220. end;
  221. if assigned(hp.right) then
  222. begin
  223. {$ifdef newcg}
  224. tg.cleartempgen;
  225. {$else newcg}
  226. cleartempgen;
  227. {$endif newcg}
  228. codegenerror:=false;
  229. firstpass(hp.right);
  230. if (not (cs_extsyntax in aktmoduleswitches)) and
  231. assigned(hp.right.resulttype) and
  232. not((hp.right.nodetype=calln) and
  233. (tcallnode(hp.right).procdefinition^.proctypeoption=potype_constructor)) and
  234. (hp.right.resulttype<>pdef(voiddef)) then
  235. CGMessage(cg_e_illegal_expression);
  236. {if codegenerror then
  237. exit;}
  238. hp.registers32:=hp.right.registers32;
  239. hp.registersfpu:=hp.right.registersfpu;
  240. {$ifdef SUPPORT_MMX}
  241. hp.registersmmx:=hp.right.registersmmx;
  242. {$endif SUPPORT_MMX}
  243. end
  244. else
  245. hp.registers32:=0;
  246. if hp.registers32>registers32 then
  247. registers32:=hp.registers32;
  248. if hp.registersfpu>registersfpu then
  249. registersfpu:=hp.registersfpu;
  250. {$ifdef SUPPORT_MMX}
  251. if hp.registersmmx>registersmmx then
  252. registersmmx:=hp.registersmmx;
  253. {$endif}
  254. inc(count);
  255. hp:=tstatementnode(hp.left);
  256. end;
  257. end;
  258. {*****************************************************************************
  259. TASMNODE
  260. *****************************************************************************}
  261. constructor tasmnode.create(p : taasmoutput);
  262. begin
  263. inherited create(asmn);
  264. p_asm:=p;
  265. end;
  266. destructor tasmnode.destroy;
  267. begin
  268. if assigned(p_asm) then
  269. p_asm.free;
  270. inherited destroy;
  271. end;
  272. function tasmnode.getcopy: tnode;
  273. var
  274. n: tasmnode;
  275. begin
  276. n := tasmnode(inherited getcopy);
  277. if assigned(p_asm) then
  278. begin
  279. n.p_asm:=taasmoutput.create;
  280. n.p_asm.concatlistcopy(p_asm);
  281. end
  282. else n.p_asm := nil;
  283. getcopy := n;
  284. end;
  285. function tasmnode.pass_1 : tnode;
  286. begin
  287. pass_1:=nil;
  288. procinfo^.flags:=procinfo^.flags or pi_uses_asm;
  289. end;
  290. function tasmnode.docompare(p: tnode): boolean;
  291. begin
  292. { comparing of asmlists is not implemented (JM) }
  293. docompare := false;
  294. end;
  295. begin
  296. cnothingnode:=tnothingnode;
  297. cerrornode:=terrornode;
  298. casmnode:=tasmnode;
  299. cstatementnode:=tstatementnode;
  300. cblocknode:=tblocknode;
  301. end.
  302. {
  303. $Log$
  304. Revision 1.8 2001-02-05 20:45:49 peter
  305. * fixed buf 1364
  306. Revision 1.7 2000/12/31 11:14:10 jonas
  307. + implemented/fixed docompare() mathods for all nodes (not tested)
  308. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  309. and constant strings/chars together
  310. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  311. when adding
  312. Revision 1.6 2000/12/25 00:07:26 peter
  313. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  314. tlinkedlist objects)
  315. Revision 1.5 2000/11/29 00:30:31 florian
  316. * unused units removed from uses clause
  317. * some changes for widestrings
  318. Revision 1.4 2000/10/31 22:02:47 peter
  319. * symtable splitted, no real code changes
  320. Revision 1.3 2000/10/27 14:57:16 jonas
  321. + implementation for tasmnode.getcopy
  322. Revision 1.2 2000/10/14 21:52:54 peter
  323. * fixed memory leaks
  324. Revision 1.1 2000/10/14 10:14:50 peter
  325. * moehrendorf oct 2000 rewrite
  326. }