tcmat.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 is_64bitint(p^.left^.resulttype) then
  201. begin
  202. firstpass(p^.left);
  203. p^.registersfpu:=p^.left^.registersfpu;
  204. {$ifdef SUPPORT_MMX}
  205. p^.registersmmx:=p^.left^.registersmmx;
  206. {$endif SUPPORT_MMX}
  207. p^.registers32:=p^.left^.registers32;
  208. if codegenerror then
  209. exit;
  210. if (p^.left^.location.loc<>LOC_REGISTER) and
  211. (p^.registers32<2) then
  212. p^.registers32:=2;
  213. p^.location.loc:=LOC_REGISTER;
  214. p^.resulttype:=p^.left^.resulttype;
  215. end
  216. else if (p^.left^.resulttype^.deftype=orddef) then
  217. begin
  218. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  219. firstpass(p^.left);
  220. p^.registersfpu:=p^.left^.registersfpu;
  221. {$ifdef SUPPORT_MMX}
  222. p^.registersmmx:=p^.left^.registersmmx;
  223. {$endif SUPPORT_MMX}
  224. p^.registers32:=p^.left^.registers32;
  225. if codegenerror then
  226. exit;
  227. if (p^.left^.location.loc<>LOC_REGISTER) and
  228. (p^.registers32<1) then
  229. p^.registers32:=1;
  230. p^.location.loc:=LOC_REGISTER;
  231. p^.resulttype:=p^.left^.resulttype;
  232. end
  233. else
  234. begin
  235. if assigned(overloaded_operators[minus]) then
  236. minusdef:=overloaded_operators[minus]^.definition
  237. else
  238. minusdef:=nil;
  239. while assigned(minusdef) do
  240. begin
  241. if (minusdef^.para1^.data=p^.left^.resulttype) and
  242. (minusdef^.para1^.next=nil) then
  243. begin
  244. t:=gencallnode(overloaded_operators[minus],nil);
  245. t^.left:=gencallparanode(p^.left,nil);
  246. putnode(p);
  247. p:=t;
  248. firstpass(p);
  249. exit;
  250. end;
  251. minusdef:=minusdef^.nextoverloaded;
  252. end;
  253. CGMessage(type_e_mismatch);
  254. end;
  255. end;
  256. {*****************************************************************************
  257. FirstNot
  258. *****************************************************************************}
  259. procedure firstnot(var p : ptree);
  260. var
  261. t : ptree;
  262. begin
  263. firstpass(p^.left);
  264. if codegenerror then
  265. exit;
  266. if (p^.left^.treetype=ordconstn) then
  267. begin
  268. if is_boolean(p^.left^.resulttype) then
  269. t:=genordinalconstnode(byte(not(boolean(p^.left^.value))),p^.left^.resulttype)
  270. else
  271. t:=genordinalconstnode(not(p^.left^.value),p^.left^.resulttype);
  272. disposetree(p);
  273. firstpass(t);
  274. p:=t;
  275. exit;
  276. end;
  277. p^.resulttype:=p^.left^.resulttype;
  278. p^.location.loc:=p^.left^.location.loc;
  279. {$ifdef SUPPORT_MMX}
  280. p^.registersmmx:=p^.left^.registersmmx;
  281. {$endif SUPPORT_MMX}
  282. if is_boolean(p^.resulttype) then
  283. begin
  284. p^.registers32:=p^.left^.registers32;
  285. if (p^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  286. begin
  287. p^.location.loc:=LOC_REGISTER;
  288. if (p^.registers32<1) then
  289. p^.registers32:=1;
  290. end;
  291. end
  292. else
  293. {$ifdef SUPPORT_MMX}
  294. if (cs_mmx in aktlocalswitches) and
  295. is_mmx_able_array(p^.left^.resulttype) then
  296. begin
  297. if (p^.left^.location.loc<>LOC_MMXREGISTER) and
  298. (p^.registersmmx<1) then
  299. p^.registersmmx:=1;
  300. end
  301. else
  302. {$endif SUPPORT_MMX}
  303. if is_64bitint(p^.left^.resulttype) then
  304. begin
  305. p^.registers32:=p^.left^.registers32;
  306. if (p^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  307. begin
  308. p^.location.loc:=LOC_REGISTER;
  309. if (p^.registers32<2) then
  310. p^.registers32:=2;
  311. end;
  312. end
  313. else
  314. begin
  315. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  316. firstpass(p^.left);
  317. if codegenerror then
  318. exit;
  319. p^.resulttype:=p^.left^.resulttype;
  320. p^.registers32:=p^.left^.registers32;
  321. {$ifdef SUPPORT_MMX}
  322. p^.registersmmx:=p^.left^.registersmmx;
  323. {$endif SUPPORT_MMX}
  324. if (p^.left^.location.loc<>LOC_REGISTER) and
  325. (p^.registers32<1) then
  326. p^.registers32:=1;
  327. p^.location.loc:=LOC_REGISTER;
  328. end;
  329. p^.registersfpu:=p^.left^.registersfpu;
  330. end;
  331. end.
  332. {
  333. $Log$
  334. Revision 1.10 1998-12-11 16:50:24 florian
  335. + typed const int64 and qword
  336. + unary minus-operator q1:=-q2;
  337. + not-operator
  338. Revision 1.9 1998/12/11 16:10:12 florian
  339. + shifting for 64 bit ints added
  340. * bug in getexplicitregister32 fixed: usableregs wasn't decremented !!
  341. Revision 1.8 1998/12/11 00:03:56 peter
  342. + globtype,tokens,version unit splitted from globals
  343. Revision 1.7 1998/11/13 10:16:38 peter
  344. * fixed constant not(boolean)
  345. Revision 1.6 1998/11/05 14:26:01 peter
  346. * fixed shlshr which would push ecx when not needed
  347. Revision 1.5 1998/10/20 13:12:39 peter
  348. * fixed 'not not boolean', the location was not set to register
  349. Revision 1.4 1998/10/13 16:50:25 pierre
  350. * undid some changes of Peter that made the compiler wrong
  351. for m68k (I had to reinsert some ifdefs)
  352. * removed several memory leaks under m68k
  353. * removed the meory leaks for assembler readers
  354. * cross compiling shoud work again better
  355. ( crosscompiling sysamiga works
  356. but as68k still complain about some code !)
  357. Revision 1.3 1998/10/13 13:10:33 peter
  358. * new style for m68k/i386 infos and enums
  359. Revision 1.2 1998/10/11 14:31:20 peter
  360. + checks for division by zero
  361. Revision 1.1 1998/09/23 20:42:24 peter
  362. * splitted pass_1
  363. }