tcmat.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. ,i386base
  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. rd,ld : pdef;
  47. begin
  48. firstpass(p^.left);
  49. firstpass(p^.right);
  50. if codegenerror then
  51. exit;
  52. { check for division by zero }
  53. rv:=p^.right^.value;
  54. lv:=p^.left^.value;
  55. if is_constintnode(p^.right) and (rv=0) then
  56. begin
  57. Message(parser_e_division_by_zero);
  58. { recover }
  59. rv:=1;
  60. end;
  61. if is_constintnode(p^.left) and is_constintnode(p^.right) then
  62. begin
  63. case p^.treetype of
  64. modn : t:=genordinalconstnode(lv mod rv,s32bitdef);
  65. divn : t:=genordinalconstnode(lv div rv,s32bitdef);
  66. end;
  67. disposetree(p);
  68. firstpass(t);
  69. p:=t;
  70. exit;
  71. end;
  72. if (p^.left^.resulttype^.deftype=orddef) and (p^.right^.resulttype^.deftype=orddef) and
  73. (is_64bitint(p^.left^.resulttype) or is_64bitint(p^.right^.resulttype)) then
  74. begin
  75. rd:=p^.right^.resulttype;
  76. ld:=p^.left^.resulttype;
  77. if (porddef(rd)^.typ=s64bitint) or (porddef(ld)^.typ=s64bitint) then
  78. begin
  79. if (porddef(ld)^.typ<>s64bitint) then
  80. begin
  81. p^.left:=gentypeconvnode(p^.left,cs64bitintdef);
  82. firstpass(p^.left);
  83. end;
  84. if (porddef(rd)^.typ<>s64bitint) then
  85. begin
  86. p^.right:=gentypeconvnode(p^.right,cs64bitintdef);
  87. firstpass(p^.right);
  88. end;
  89. calcregisters(p,2,0,0);
  90. end
  91. else if (porddef(rd)^.typ=u64bit) or (porddef(ld)^.typ=u64bit) then
  92. begin
  93. if (porddef(ld)^.typ<>u64bit) then
  94. begin
  95. p^.left:=gentypeconvnode(p^.left,cu64bitdef);
  96. firstpass(p^.left);
  97. end;
  98. if (porddef(rd)^.typ<>u64bit) then
  99. begin
  100. p^.right:=gentypeconvnode(p^.right,cu64bitdef);
  101. firstpass(p^.right);
  102. end;
  103. calcregisters(p,2,0,0);
  104. end;
  105. p^.resulttype:=p^.left^.resulttype;
  106. end
  107. else
  108. begin
  109. if not(p^.right^.resulttype^.deftype=orddef) or
  110. not(porddef(p^.right^.resulttype)^.typ in [s32bit,u32bit]) then
  111. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  112. if not(p^.left^.resulttype^.deftype=orddef) or
  113. not(porddef(p^.left^.resulttype)^.typ in [s32bit,u32bit]) then
  114. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  115. firstpass(p^.left);
  116. firstpass(p^.right);
  117. { the resulttype depends on the right side, because the left becomes }
  118. { always 64 bit }
  119. p^.resulttype:=p^.right^.resulttype;
  120. if codegenerror then
  121. exit;
  122. left_right_max(p);
  123. if p^.left^.registers32<=p^.right^.registers32 then
  124. inc(p^.registers32);
  125. end;
  126. p^.location.loc:=LOC_REGISTER;
  127. end;
  128. {*****************************************************************************
  129. FirstShlShr
  130. *****************************************************************************}
  131. procedure firstshlshr(var p : ptree);
  132. var
  133. t : ptree;
  134. regs : longint;
  135. begin
  136. firstpass(p^.left);
  137. firstpass(p^.right);
  138. if codegenerror then
  139. exit;
  140. if is_constintnode(p^.left) and is_constintnode(p^.right) then
  141. begin
  142. case p^.treetype of
  143. shrn : t:=genordinalconstnode(p^.left^.value shr p^.right^.value,s32bitdef);
  144. shln : t:=genordinalconstnode(p^.left^.value shl p^.right^.value,s32bitdef);
  145. end;
  146. disposetree(p);
  147. firstpass(t);
  148. p:=t;
  149. exit;
  150. end;
  151. { 64 bit ints have their own shift handling }
  152. if not(is_64bitint(p^.left^.resulttype)) then
  153. begin
  154. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  155. firstpass(p^.left);
  156. regs:=1;
  157. p^.resulttype:=s32bitdef;
  158. end
  159. else
  160. begin
  161. p^.resulttype:=p^.left^.resulttype;
  162. regs:=2;
  163. end;
  164. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  165. firstpass(p^.right);
  166. if codegenerror then
  167. exit;
  168. if (p^.right^.treetype<>ordconstn) then
  169. inc(regs);
  170. calcregisters(p,regs,0,0);
  171. p^.location.loc:=LOC_REGISTER;
  172. end;
  173. {*****************************************************************************
  174. FirstUmMinus
  175. *****************************************************************************}
  176. procedure firstumminus(var p : ptree);
  177. var
  178. t : ptree;
  179. minusdef : pprocdef;
  180. begin
  181. firstpass(p^.left);
  182. p^.registers32:=p^.left^.registers32;
  183. p^.registersfpu:=p^.left^.registersfpu;
  184. {$ifdef SUPPORT_MMX}
  185. p^.registersmmx:=p^.left^.registersmmx;
  186. {$endif SUPPORT_MMX}
  187. p^.resulttype:=p^.left^.resulttype;
  188. if codegenerror then
  189. exit;
  190. if is_constintnode(p^.left) then
  191. begin
  192. t:=genordinalconstnode(-p^.left^.value,s32bitdef);
  193. disposetree(p);
  194. firstpass(t);
  195. p:=t;
  196. exit;
  197. end;
  198. { nasm can not cope with negativ reals !! }
  199. if is_constrealnode(p^.left)
  200. {$ifdef i386}
  201. and not(aktoutputformat in [as_i386_nasmcoff,as_i386_nasmelf,as_i386_nasmobj])
  202. {$endif i386}
  203. then
  204. begin
  205. t:=genrealconstnode(-p^.left^.value_real,bestrealdef^);
  206. disposetree(p);
  207. firstpass(t);
  208. p:=t;
  209. exit;
  210. end;
  211. if (p^.left^.resulttype^.deftype=floatdef) then
  212. begin
  213. if pfloatdef(p^.left^.resulttype)^.typ=f32bit then
  214. begin
  215. if (p^.left^.location.loc<>LOC_REGISTER) and
  216. (p^.registers32<1) then
  217. p^.registers32:=1;
  218. p^.location.loc:=LOC_REGISTER;
  219. end
  220. else
  221. p^.location.loc:=LOC_FPU;
  222. end
  223. {$ifdef SUPPORT_MMX}
  224. else if (cs_mmx in aktlocalswitches) and
  225. is_mmx_able_array(p^.left^.resulttype) then
  226. begin
  227. if (p^.left^.location.loc<>LOC_MMXREGISTER) and
  228. (p^.registersmmx<1) then
  229. p^.registersmmx:=1;
  230. { if saturation is on, p^.left^.resulttype isn't
  231. "mmx able" (FK)
  232. if (cs_mmx_saturation in aktlocalswitches^) and
  233. (porddef(parraydef(p^.resulttype)^.definition)^.typ in
  234. [s32bit,u32bit]) then
  235. CGMessage(type_e_mismatch);
  236. }
  237. end
  238. {$endif SUPPORT_MMX}
  239. else if is_64bitint(p^.left^.resulttype) then
  240. begin
  241. firstpass(p^.left);
  242. p^.registersfpu:=p^.left^.registersfpu;
  243. {$ifdef SUPPORT_MMX}
  244. p^.registersmmx:=p^.left^.registersmmx;
  245. {$endif SUPPORT_MMX}
  246. p^.registers32:=p^.left^.registers32;
  247. if codegenerror then
  248. exit;
  249. if (p^.left^.location.loc<>LOC_REGISTER) and
  250. (p^.registers32<2) then
  251. p^.registers32:=2;
  252. p^.location.loc:=LOC_REGISTER;
  253. p^.resulttype:=p^.left^.resulttype;
  254. end
  255. else if (p^.left^.resulttype^.deftype=orddef) then
  256. begin
  257. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  258. firstpass(p^.left);
  259. p^.registersfpu:=p^.left^.registersfpu;
  260. {$ifdef SUPPORT_MMX}
  261. p^.registersmmx:=p^.left^.registersmmx;
  262. {$endif SUPPORT_MMX}
  263. p^.registers32:=p^.left^.registers32;
  264. if codegenerror then
  265. exit;
  266. if (p^.left^.location.loc<>LOC_REGISTER) and
  267. (p^.registers32<1) then
  268. p^.registers32:=1;
  269. p^.location.loc:=LOC_REGISTER;
  270. p^.resulttype:=p^.left^.resulttype;
  271. end
  272. else
  273. begin
  274. if assigned(overloaded_operators[minus]) then
  275. minusdef:=overloaded_operators[minus]^.definition
  276. else
  277. minusdef:=nil;
  278. while assigned(minusdef) do
  279. begin
  280. if (minusdef^.para1^.data=p^.left^.resulttype) and
  281. (minusdef^.para1^.next=nil) then
  282. begin
  283. t:=gencallnode(overloaded_operators[minus],nil);
  284. t^.left:=gencallparanode(p^.left,nil);
  285. putnode(p);
  286. p:=t;
  287. firstpass(p);
  288. exit;
  289. end;
  290. minusdef:=minusdef^.nextoverloaded;
  291. end;
  292. CGMessage(type_e_mismatch);
  293. end;
  294. end;
  295. {*****************************************************************************
  296. FirstNot
  297. *****************************************************************************}
  298. procedure firstnot(var p : ptree);
  299. var
  300. t : ptree;
  301. begin
  302. firstpass(p^.left);
  303. if codegenerror then
  304. exit;
  305. if (p^.left^.treetype=ordconstn) then
  306. begin
  307. if is_boolean(p^.left^.resulttype) then
  308. t:=genordinalconstnode(byte(not(boolean(p^.left^.value))),p^.left^.resulttype)
  309. else
  310. t:=genordinalconstnode(not(p^.left^.value),p^.left^.resulttype);
  311. disposetree(p);
  312. firstpass(t);
  313. p:=t;
  314. exit;
  315. end;
  316. p^.resulttype:=p^.left^.resulttype;
  317. p^.location.loc:=p^.left^.location.loc;
  318. {$ifdef SUPPORT_MMX}
  319. p^.registersmmx:=p^.left^.registersmmx;
  320. {$endif SUPPORT_MMX}
  321. if is_boolean(p^.resulttype) then
  322. begin
  323. p^.registers32:=p^.left^.registers32;
  324. {$ifdef i386}
  325. if p^.left^.location.loc<>LOC_JUMP then
  326. p^.location.loc:=LOC_FLAGS;
  327. {$endif def i386}
  328. if (p^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  329. begin
  330. p^.location.loc:=LOC_REGISTER;
  331. if (p^.registers32<1) then
  332. p^.registers32:=1;
  333. end;
  334. end
  335. else
  336. {$ifdef SUPPORT_MMX}
  337. if (cs_mmx in aktlocalswitches) and
  338. is_mmx_able_array(p^.left^.resulttype) then
  339. begin
  340. if (p^.left^.location.loc<>LOC_MMXREGISTER) and
  341. (p^.registersmmx<1) then
  342. p^.registersmmx:=1;
  343. end
  344. else
  345. {$endif SUPPORT_MMX}
  346. if is_64bitint(p^.left^.resulttype) then
  347. begin
  348. p^.registers32:=p^.left^.registers32;
  349. if (p^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  350. begin
  351. p^.location.loc:=LOC_REGISTER;
  352. if (p^.registers32<2) then
  353. p^.registers32:=2;
  354. end;
  355. end
  356. else
  357. begin
  358. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  359. firstpass(p^.left);
  360. if codegenerror then
  361. exit;
  362. p^.resulttype:=p^.left^.resulttype;
  363. p^.registers32:=p^.left^.registers32;
  364. {$ifdef SUPPORT_MMX}
  365. p^.registersmmx:=p^.left^.registersmmx;
  366. {$endif SUPPORT_MMX}
  367. if (p^.left^.location.loc<>LOC_REGISTER) and
  368. (p^.registers32<1) then
  369. p^.registers32:=1;
  370. p^.location.loc:=LOC_REGISTER;
  371. end;
  372. p^.registersfpu:=p^.left^.registersfpu;
  373. end;
  374. end.
  375. {
  376. $Log$
  377. Revision 1.16 1999-06-02 10:11:54 florian
  378. * make cycle fixed i.e. compilation with 0.99.10
  379. * some fixes for qword
  380. * start of register calling conventions
  381. Revision 1.15 1999/05/27 19:45:22 peter
  382. * removed oldasm
  383. * plabel -> pasmlabel
  384. * -a switches to source writing automaticly
  385. * assembler readers OOPed
  386. * asmsymbol automaticly external
  387. * jumptables and other label fixes for asm readers
  388. Revision 1.14 1999/05/06 09:05:38 peter
  389. * generic write_float and str_float
  390. * fixed constant float conversions
  391. Revision 1.13 1999/05/01 13:24:55 peter
  392. * merged nasm compiler
  393. * old asm moved to oldasm/
  394. Revision 1.12 1999/02/22 02:15:53 peter
  395. * updates for ag386bin
  396. Revision 1.11 1999/02/03 10:11:11 pierre
  397. * fix for bug0211 for i386
  398. Revision 1.10 1998/12/11 16:50:24 florian
  399. + typed const int64 and qword
  400. + unary minus-operator q1:=-q2;
  401. + not-operator
  402. Revision 1.9 1998/12/11 16:10:12 florian
  403. + shifting for 64 bit ints added
  404. * bug in getexplicitregister32 fixed: usableregs wasn't decremented !!
  405. Revision 1.8 1998/12/11 00:03:56 peter
  406. + globtype,tokens,version unit splitted from globals
  407. Revision 1.7 1998/11/13 10:16:38 peter
  408. * fixed constant not(boolean)
  409. Revision 1.6 1998/11/05 14:26:01 peter
  410. * fixed shlshr which would push ecx when not needed
  411. Revision 1.5 1998/10/20 13:12:39 peter
  412. * fixed 'not not boolean', the location was not set to register
  413. Revision 1.4 1998/10/13 16:50:25 pierre
  414. * undid some changes of Peter that made the compiler wrong
  415. for m68k (I had to reinsert some ifdefs)
  416. * removed several memory leaks under m68k
  417. * removed the meory leaks for assembler readers
  418. * cross compiling shoud work again better
  419. ( crosscompiling sysamiga works
  420. but as68k still complain about some code !)
  421. Revision 1.3 1998/10/13 13:10:33 peter
  422. * new style for m68k/i386 infos and enums
  423. Revision 1.2 1998/10/11 14:31:20 peter
  424. + checks for division by zero
  425. Revision 1.1 1998/09/23 20:42:24 peter
  426. * splitted pass_1
  427. }