tcmat.pas 17 KB

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