tcmat.pas 17 KB

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