nbas.pas 13 KB

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