tcmat.pas 12 KB

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