tcmat.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Type checking and register allocation for math 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 tcmat;
  19. interface
  20. uses
  21. tree;
  22. procedure firstmoddiv(var p : ptree);
  23. procedure firstshlshr(var p : ptree);
  24. procedure firstumminus(var p : ptree);
  25. procedure firstnot(var p : ptree);
  26. implementation
  27. uses
  28. cobjects,verbose,globals,systems,
  29. symtable,aasm,types,
  30. hcodegen,htypechk,pass_1
  31. {$ifdef i386}
  32. ,i386
  33. {$endif}
  34. {$ifdef m68k}
  35. ,m68k
  36. {$endif}
  37. ;
  38. {*****************************************************************************
  39. FirstModDiv
  40. *****************************************************************************}
  41. procedure firstmoddiv(var p : ptree);
  42. var
  43. t : ptree;
  44. rv,lv : longint;
  45. begin
  46. firstpass(p^.left);
  47. firstpass(p^.right);
  48. if codegenerror then
  49. exit;
  50. { check for division by zero }
  51. rv:=p^.right^.value;
  52. lv:=p^.left^.value;
  53. if is_constintnode(p^.right) and (rv=0) then
  54. begin
  55. Message(parser_e_division_by_zero);
  56. { recover }
  57. rv:=1;
  58. end;
  59. if is_constintnode(p^.left) and is_constintnode(p^.right) then
  60. begin
  61. case p^.treetype of
  62. modn : t:=genordinalconstnode(lv mod rv,s32bitdef);
  63. divn : t:=genordinalconstnode(lv div rv,s32bitdef);
  64. end;
  65. disposetree(p);
  66. firstpass(t);
  67. p:=t;
  68. exit;
  69. end;
  70. if not(p^.right^.resulttype^.deftype=orddef) or
  71. not(porddef(p^.right^.resulttype)^.typ in [s32bit,u32bit]) then
  72. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  73. if not(p^.left^.resulttype^.deftype=orddef) or
  74. not(porddef(p^.left^.resulttype)^.typ in [s32bit,u32bit]) then
  75. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  76. firstpass(p^.left);
  77. firstpass(p^.right);
  78. { the resulttype depends on the right side, because the left becomes }
  79. { always 64 bit }
  80. p^.resulttype:=p^.right^.resulttype;
  81. if codegenerror then
  82. exit;
  83. left_right_max(p);
  84. if p^.left^.registers32<=p^.right^.registers32 then
  85. inc(p^.registers32);
  86. p^.location.loc:=LOC_REGISTER;
  87. end;
  88. {*****************************************************************************
  89. FirstShlShr
  90. *****************************************************************************}
  91. procedure firstshlshr(var p : ptree);
  92. var
  93. t : ptree;
  94. begin
  95. firstpass(p^.left);
  96. firstpass(p^.right);
  97. if codegenerror then
  98. exit;
  99. if is_constintnode(p^.left) and is_constintnode(p^.right) then
  100. begin
  101. case p^.treetype of
  102. shrn : t:=genordinalconstnode(p^.left^.value shr p^.right^.value,s32bitdef);
  103. shln : t:=genordinalconstnode(p^.left^.value shl p^.right^.value,s32bitdef);
  104. end;
  105. disposetree(p);
  106. firstpass(t);
  107. p:=t;
  108. exit;
  109. end;
  110. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  111. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  112. firstpass(p^.left);
  113. firstpass(p^.right);
  114. if codegenerror then
  115. exit;
  116. calcregisters(p,2,0,0);
  117. p^.resulttype:=s32bitdef;
  118. p^.location.loc:=LOC_REGISTER;
  119. end;
  120. {*****************************************************************************
  121. FirstUmMinus
  122. *****************************************************************************}
  123. procedure firstumminus(var p : ptree);
  124. var
  125. t : ptree;
  126. minusdef : pprocdef;
  127. begin
  128. firstpass(p^.left);
  129. p^.registers32:=p^.left^.registers32;
  130. p^.registersfpu:=p^.left^.registersfpu;
  131. {$ifdef SUPPORT_MMX}
  132. p^.registersmmx:=p^.left^.registersmmx;
  133. {$endif SUPPORT_MMX}
  134. p^.resulttype:=p^.left^.resulttype;
  135. if codegenerror then
  136. exit;
  137. if is_constintnode(p^.left) then
  138. begin
  139. t:=genordinalconstnode(-p^.left^.value,s32bitdef);
  140. disposetree(p);
  141. firstpass(t);
  142. p:=t;
  143. exit;
  144. end;
  145. { nasm can not cope with negativ reals !! }
  146. if is_constrealnode(p^.left)
  147. {$ifdef i386}
  148. and not(aktoutputformat in [as_i386_nasmcoff,as_i386_nasmelf,as_i386_nasmobj])
  149. {$endif i386}
  150. then
  151. begin
  152. t:=genrealconstnode(-p^.left^.value_real);
  153. disposetree(p);
  154. firstpass(t);
  155. p:=t;
  156. exit;
  157. end;
  158. if (p^.left^.resulttype^.deftype=floatdef) then
  159. begin
  160. if pfloatdef(p^.left^.resulttype)^.typ=f32bit then
  161. begin
  162. if (p^.left^.location.loc<>LOC_REGISTER) and
  163. (p^.registers32<1) then
  164. p^.registers32:=1;
  165. p^.location.loc:=LOC_REGISTER;
  166. end
  167. else
  168. p^.location.loc:=LOC_FPU;
  169. end
  170. {$ifdef SUPPORT_MMX}
  171. else if (cs_mmx in aktlocalswitches) and
  172. is_mmx_able_array(p^.left^.resulttype) then
  173. begin
  174. if (p^.left^.location.loc<>LOC_MMXREGISTER) and
  175. (p^.registersmmx<1) then
  176. p^.registersmmx:=1;
  177. { if saturation is on, p^.left^.resulttype isn't
  178. "mmx able" (FK)
  179. if (cs_mmx_saturation in aktlocalswitches^) and
  180. (porddef(parraydef(p^.resulttype)^.definition)^.typ in
  181. [s32bit,u32bit]) then
  182. CGMessage(type_e_mismatch);
  183. }
  184. end
  185. {$endif SUPPORT_MMX}
  186. else if (p^.left^.resulttype^.deftype=orddef) then
  187. begin
  188. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  189. firstpass(p^.left);
  190. p^.registersfpu:=p^.left^.registersfpu;
  191. {$ifdef SUPPORT_MMX}
  192. p^.registersmmx:=p^.left^.registersmmx;
  193. {$endif SUPPORT_MMX}
  194. p^.registers32:=p^.left^.registers32;
  195. if codegenerror then
  196. exit;
  197. if (p^.left^.location.loc<>LOC_REGISTER) and
  198. (p^.registers32<1) then
  199. p^.registers32:=1;
  200. p^.location.loc:=LOC_REGISTER;
  201. p^.resulttype:=p^.left^.resulttype;
  202. end
  203. else
  204. begin
  205. if assigned(overloaded_operators[minus]) then
  206. minusdef:=overloaded_operators[minus]^.definition
  207. else
  208. minusdef:=nil;
  209. while assigned(minusdef) do
  210. begin
  211. if (minusdef^.para1^.data=p^.left^.resulttype) and
  212. (minusdef^.para1^.next=nil) then
  213. begin
  214. t:=gencallnode(overloaded_operators[minus],nil);
  215. t^.left:=gencallparanode(p^.left,nil);
  216. putnode(p);
  217. p:=t;
  218. firstpass(p);
  219. exit;
  220. end;
  221. minusdef:=minusdef^.nextoverloaded;
  222. end;
  223. CGMessage(type_e_mismatch);
  224. end;
  225. end;
  226. {*****************************************************************************
  227. FirstNot
  228. *****************************************************************************}
  229. procedure firstnot(var p : ptree);
  230. var
  231. t : ptree;
  232. begin
  233. firstpass(p^.left);
  234. if codegenerror then
  235. exit;
  236. if (p^.left^.treetype=ordconstn) then
  237. begin
  238. t:=genordinalconstnode(not(p^.left^.value),p^.left^.resulttype);
  239. disposetree(p);
  240. firstpass(t);
  241. p:=t;
  242. exit;
  243. end;
  244. p^.resulttype:=p^.left^.resulttype;
  245. p^.location.loc:=p^.left^.location.loc;
  246. {$ifdef SUPPORT_MMX}
  247. p^.registersmmx:=p^.left^.registersmmx;
  248. {$endif SUPPORT_MMX}
  249. if is_equal(p^.resulttype,booldef) then
  250. begin
  251. p^.registers32:=p^.left^.registers32;
  252. if ((p^.location.loc=LOC_REFERENCE) or
  253. (p^.location.loc=LOC_CREGISTER)) and
  254. (p^.registers32<1) then
  255. p^.registers32:=1;
  256. end
  257. else
  258. {$ifdef SUPPORT_MMX}
  259. if (cs_mmx in aktlocalswitches) and
  260. is_mmx_able_array(p^.left^.resulttype) then
  261. begin
  262. if (p^.left^.location.loc<>LOC_MMXREGISTER) and
  263. (p^.registersmmx<1) then
  264. p^.registersmmx:=1;
  265. end
  266. else
  267. {$endif SUPPORT_MMX}
  268. begin
  269. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  270. firstpass(p^.left);
  271. if codegenerror then
  272. exit;
  273. p^.resulttype:=p^.left^.resulttype;
  274. p^.registers32:=p^.left^.registers32;
  275. {$ifdef SUPPORT_MMX}
  276. p^.registersmmx:=p^.left^.registersmmx;
  277. {$endif SUPPORT_MMX}
  278. if (p^.left^.location.loc<>LOC_REGISTER) and
  279. (p^.registers32<1) then
  280. p^.registers32:=1;
  281. p^.location.loc:=LOC_REGISTER;
  282. end;
  283. p^.registersfpu:=p^.left^.registersfpu;
  284. end;
  285. end.
  286. {
  287. $Log$
  288. Revision 1.4 1998-10-13 16:50:25 pierre
  289. * undid some changes of Peter that made the compiler wrong
  290. for m68k (I had to reinsert some ifdefs)
  291. * removed several memory leaks under m68k
  292. * removed the meory leaks for assembler readers
  293. * cross compiling shoud work again better
  294. ( crosscompiling sysamiga works
  295. but as68k still complain about some code !)
  296. Revision 1.3 1998/10/13 13:10:33 peter
  297. * new style for m68k/i386 infos and enums
  298. Revision 1.2 1998/10/11 14:31:20 peter
  299. + checks for division by zero
  300. Revision 1.1 1998/09/23 20:42:24 peter
  301. * splitted pass_1
  302. }