nmat.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. {
  2. $Id$
  3. Copyright (c) 2000 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 nmat;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. node;
  23. type
  24. tmoddivnode = class(tbinopnode)
  25. function pass_1 : tnode;override;
  26. function det_resulttype:tnode;override;
  27. end;
  28. tshlshrnode = class(tbinopnode)
  29. function pass_1 : tnode;override;
  30. function det_resulttype:tnode;override;
  31. end;
  32. tunaryminusnode = class(tunarynode)
  33. constructor create(expr : tnode);virtual;
  34. function pass_1 : tnode;override;
  35. function det_resulttype:tnode;override;
  36. end;
  37. tnotnode = class(tunarynode)
  38. constructor create(expr : tnode);virtual;
  39. function pass_1 : tnode;override;
  40. function det_resulttype:tnode;override;
  41. end;
  42. var
  43. cmoddivnode : class of tmoddivnode;
  44. cshlshrnode : class of tshlshrnode;
  45. cunaryminusnode : class of tunaryminusnode;
  46. cnotnode : class of tnotnode;
  47. implementation
  48. uses
  49. systems,tokens,
  50. verbose,globals,
  51. {$ifdef support_mmx}
  52. globtype,
  53. {$endif}
  54. symconst,symtype,symtable,symdef,types,
  55. htypechk,pass_1,cpubase,cpuinfo,
  56. {$ifdef newcg}
  57. cgbase,
  58. {$endif newcg}
  59. hcodegen,
  60. ncon,ncnv,ncal;
  61. {****************************************************************************
  62. TMODDIVNODE
  63. ****************************************************************************}
  64. function tmoddivnode.det_resulttype:tnode;
  65. var
  66. t : tnode;
  67. rd,ld : pdef;
  68. begin
  69. result:=nil;
  70. resulttypepass(left);
  71. resulttypepass(right);
  72. set_varstate(left,true);
  73. set_varstate(right,true);
  74. if codegenerror then
  75. exit;
  76. { allow operator overloading }
  77. t:=self;
  78. if isbinaryoverloaded(t) then
  79. begin
  80. resulttypepass(t);
  81. result:=t;
  82. exit;
  83. end;
  84. { if one operand is a cardinal and the other is a positive constant, convert the }
  85. { constant to a cardinal as well so we don't have to do a 64bit division (JM) }
  86. { Do the same for qwords and positive constants as well, otherwise things like }
  87. { "qword mod 10" are evaluated with int64 as result, which is wrong if the }
  88. { "qword" was > high(int64) (JM) }
  89. if (left.resulttype.def^.deftype=orddef) and (right.resulttype.def^.deftype=orddef) then
  90. if (porddef(right.resulttype.def)^.typ in [u32bit,u64bit]) and
  91. is_constintnode(left) and
  92. (tordconstnode(left).value >= 0) then
  93. inserttypeconv(left,right.resulttype)
  94. else if (porddef(left.resulttype.def)^.typ in [u32bit,u64bit]) and
  95. is_constintnode(right) and
  96. (tordconstnode(right).value >= 0) then
  97. inserttypeconv(right,left.resulttype);
  98. if (left.resulttype.def^.deftype=orddef) and (right.resulttype.def^.deftype=orddef) and
  99. (is_64bitint(left.resulttype.def) or is_64bitint(right.resulttype.def) or
  100. { when mixing cardinals and signed numbers, convert everythign to 64bit (JM) }
  101. ((porddef(right.resulttype.def)^.typ = u32bit) and
  102. is_signed(left.resulttype.def)) or
  103. ((porddef(left.resulttype.def)^.typ = u32bit) and
  104. is_signed(right.resulttype.def))) then
  105. begin
  106. rd:=right.resulttype.def;
  107. ld:=left.resulttype.def;
  108. { issue warning if necessary }
  109. if not (is_64bitint(left.resulttype.def) or is_64bitint(right.resulttype.def)) then
  110. CGMessage(type_w_mixed_signed_unsigned);
  111. if is_signed(rd) or is_signed(ld) then
  112. begin
  113. if (porddef(ld)^.typ<>s64bit) then
  114. inserttypeconv(left,cs64bittype);
  115. if (porddef(rd)^.typ<>s64bit) then
  116. inserttypeconv(right,cs64bittype);
  117. end
  118. else
  119. begin
  120. if (porddef(ld)^.typ<>u64bit) then
  121. inserttypeconv(left,cu64bittype);
  122. if (porddef(rd)^.typ<>u64bit) then
  123. inserttypeconv(right,cu64bittype);
  124. end;
  125. resulttype:=left.resulttype;
  126. end
  127. else
  128. begin
  129. if not(right.resulttype.def^.deftype=orddef) or
  130. not(porddef(right.resulttype.def)^.typ in [s32bit,u32bit]) then
  131. inserttypeconv(right,s32bittype);
  132. if not(left.resulttype.def^.deftype=orddef) or
  133. not(porddef(left.resulttype.def)^.typ in [s32bit,u32bit]) then
  134. inserttypeconv(left,s32bittype);
  135. { the resulttype.def depends on the right side, because the left becomes }
  136. { always 64 bit }
  137. resulttype:=right.resulttype;
  138. end;
  139. end;
  140. function tmoddivnode.pass_1 : tnode;
  141. var
  142. t : tnode;
  143. rv,lv : tconstexprint;
  144. begin
  145. result:=nil;
  146. firstpass(left);
  147. firstpass(right);
  148. if codegenerror then
  149. exit;
  150. if is_constintnode(left) and is_constintnode(right) then
  151. begin
  152. rv:=tordconstnode(right).value;
  153. lv:=tordconstnode(left).value;
  154. { check for division by zero }
  155. if (rv=0) then
  156. begin
  157. Message(parser_e_division_by_zero);
  158. { recover }
  159. rv:=1;
  160. end;
  161. case nodetype of
  162. modn:
  163. t:=genintconstnode(lv mod rv);
  164. divn:
  165. t:=genintconstnode(lv div rv);
  166. end;
  167. firstpass(t);
  168. result:=t;
  169. exit;
  170. end;
  171. { 64bit }
  172. if (left.resulttype.def^.deftype=orddef) and (right.resulttype.def^.deftype=orddef) and
  173. (is_64bitint(left.resulttype.def) or is_64bitint(right.resulttype.def)) then
  174. begin
  175. calcregisters(self,2,0,0);
  176. end
  177. else
  178. begin
  179. left_right_max;
  180. if left.registers32<=right.registers32 then
  181. inc(registers32);
  182. end;
  183. location.loc:=LOC_REGISTER;
  184. end;
  185. {****************************************************************************
  186. TSHLSHRNODE
  187. ****************************************************************************}
  188. function tshlshrnode.det_resulttype:tnode;
  189. var
  190. t : tnode;
  191. begin
  192. result:=nil;
  193. resulttypepass(left);
  194. resulttypepass(right);
  195. set_varstate(right,true);
  196. set_varstate(left,true);
  197. if codegenerror then
  198. exit;
  199. { allow operator overloading }
  200. t:=self;
  201. if isbinaryoverloaded(t) then
  202. begin
  203. resulttypepass(t);
  204. result:=t;
  205. exit;
  206. end;
  207. { 64 bit ints have their own shift handling }
  208. if not(is_64bitint(left.resulttype.def)) then
  209. begin
  210. if porddef(left.resulttype.def)^.typ <> u32bit then
  211. inserttypeconv(left,s32bittype);
  212. end;
  213. inserttypeconv(right,s32bittype);
  214. resulttype:=left.resulttype;
  215. end;
  216. function tshlshrnode.pass_1 : tnode;
  217. var
  218. t : tnode;
  219. regs : longint;
  220. begin
  221. result:=nil;
  222. firstpass(left);
  223. firstpass(right);
  224. if codegenerror then
  225. exit;
  226. if is_constintnode(left) and is_constintnode(right) then
  227. begin
  228. case nodetype of
  229. shrn:
  230. t:=genintconstnode(tordconstnode(left).value shr tordconstnode(right).value);
  231. shln:
  232. t:=genintconstnode(tordconstnode(left).value shl tordconstnode(right).value);
  233. end;
  234. firstpass(t);
  235. result:=t;
  236. exit;
  237. end;
  238. { 64 bit ints have their own shift handling }
  239. if not(is_64bitint(left.resulttype.def)) then
  240. regs:=1
  241. else
  242. regs:=2;
  243. if (right.nodetype<>ordconstn) then
  244. inc(regs);
  245. calcregisters(self,regs,0,0);
  246. location.loc:=LOC_REGISTER;
  247. end;
  248. {****************************************************************************
  249. TUNARYMINUSNODE
  250. ****************************************************************************}
  251. constructor tunaryminusnode.create(expr : tnode);
  252. begin
  253. inherited create(unaryminusn,expr);
  254. end;
  255. function tunaryminusnode.det_resulttype : tnode;
  256. var
  257. t : tnode;
  258. minusdef : pprocdef;
  259. begin
  260. result:=nil;
  261. resulttypepass(left);
  262. set_varstate(left,true);
  263. if codegenerror then
  264. exit;
  265. resulttype:=left.resulttype;
  266. if (left.resulttype.def^.deftype=floatdef) then
  267. begin
  268. end
  269. {$ifdef SUPPORT_MMX}
  270. else if (cs_mmx in aktlocalswitches) and
  271. is_mmx_able_array(left.resulttype.def) then
  272. begin
  273. { if saturation is on, left.resulttype.def isn't
  274. "mmx able" (FK)
  275. if (cs_mmx_saturation in aktlocalswitches^) and
  276. (porddef(parraydef(resulttype.def)^.definition)^.typ in
  277. [s32bit,u32bit]) then
  278. CGMessage(type_e_mismatch);
  279. }
  280. end
  281. {$endif SUPPORT_MMX}
  282. else if is_64bitint(left.resulttype.def) then
  283. begin
  284. end
  285. else if (left.resulttype.def^.deftype=orddef) then
  286. begin
  287. inserttypeconv(left,s32bittype);
  288. resulttype:=left.resulttype;
  289. end
  290. else
  291. begin
  292. if assigned(overloaded_operators[_minus]) then
  293. minusdef:=overloaded_operators[_minus]^.definition
  294. else
  295. minusdef:=nil;
  296. while assigned(minusdef) do
  297. begin
  298. if is_equal(tparaitem(minusdef^.para.first).paratype.def,left.resulttype.def) and
  299. (tparaitem(minusdef^.para.first).next=nil) then
  300. begin
  301. t:=ccallnode.create(ccallparanode.create(left,nil),
  302. overloaded_operators[_minus],nil,nil);
  303. left:=nil;
  304. resulttypepass(t);
  305. result:=t;
  306. exit;
  307. end;
  308. minusdef:=minusdef^.nextoverloaded;
  309. end;
  310. CGMessage(type_e_mismatch);
  311. end;
  312. end;
  313. function tunaryminusnode.pass_1 : tnode;
  314. var
  315. t : tnode;
  316. begin
  317. result:=nil;
  318. firstpass(left);
  319. if codegenerror then
  320. exit;
  321. if is_constintnode(left) then
  322. begin
  323. t:=cordconstnode.create(-tordconstnode(left).value,resulttype);
  324. firstpass(t);
  325. result:=t;
  326. exit;
  327. end;
  328. if is_constrealnode(left) then
  329. begin
  330. t:=crealconstnode.create(-trealconstnode(left).value_real,resulttype);
  331. firstpass(t);
  332. result:=t;
  333. exit;
  334. end;
  335. registers32:=left.registers32;
  336. registersfpu:=left.registersfpu;
  337. {$ifdef SUPPORT_MMX}
  338. registersmmx:=left.registersmmx;
  339. {$endif SUPPORT_MMX}
  340. if (left.resulttype.def^.deftype=floatdef) then
  341. begin
  342. location.loc:=LOC_FPU;
  343. end
  344. {$ifdef SUPPORT_MMX}
  345. else if (cs_mmx in aktlocalswitches) and
  346. is_mmx_able_array(left.resulttype.def) then
  347. begin
  348. if (left.location.loc<>LOC_MMXREGISTER) and
  349. (registersmmx<1) then
  350. registersmmx:=1;
  351. end
  352. {$endif SUPPORT_MMX}
  353. else if is_64bitint(left.resulttype.def) then
  354. begin
  355. if (left.location.loc<>LOC_REGISTER) and
  356. (registers32<2) then
  357. registers32:=2;
  358. location.loc:=LOC_REGISTER;
  359. end
  360. else if (left.resulttype.def^.deftype=orddef) then
  361. begin
  362. if (left.location.loc<>LOC_REGISTER) and
  363. (registers32<1) then
  364. registers32:=1;
  365. location.loc:=LOC_REGISTER;
  366. end;
  367. end;
  368. {****************************************************************************
  369. TNOTNODE
  370. ****************************************************************************}
  371. constructor tnotnode.create(expr : tnode);
  372. begin
  373. inherited create(notn,expr);
  374. end;
  375. function tnotnode.det_resulttype : tnode;
  376. var
  377. t : tnode;
  378. notdef : pprocdef;
  379. begin
  380. result:=nil;
  381. resulttypepass(left);
  382. set_varstate(left,true);
  383. if codegenerror then
  384. exit;
  385. resulttype:=left.resulttype;
  386. if is_boolean(resulttype.def) then
  387. begin
  388. end
  389. else
  390. {$ifdef SUPPORT_MMX}
  391. if (cs_mmx in aktlocalswitches) and
  392. is_mmx_able_array(left.resulttype.def) then
  393. begin
  394. end
  395. else
  396. {$endif SUPPORT_MMX}
  397. if is_64bitint(left.resulttype.def) then
  398. begin
  399. end
  400. else if is_integer(left.resulttype.def) then
  401. begin
  402. if (porddef(left.resulttype.def)^.typ <> u32bit) then
  403. inserttypeconv(left,s32bittype);
  404. end
  405. else
  406. begin
  407. if assigned(overloaded_operators[_op_not]) then
  408. notdef:=overloaded_operators[_op_not]^.definition
  409. else
  410. notdef:=nil;
  411. while assigned(notdef) do
  412. begin
  413. if is_equal(tparaitem(notdef^.para.first).paratype.def,left.resulttype.def) and
  414. (tparaitem(notdef^.para.first).next=nil) then
  415. begin
  416. t:=ccallnode.create(ccallparanode.create(left,nil),
  417. overloaded_operators[_op_not],nil,nil);
  418. left:=nil;
  419. resulttypepass(t);
  420. result:=t;
  421. exit;
  422. end;
  423. notdef:=notdef^.nextoverloaded;
  424. end;
  425. CGMessage(type_e_mismatch);
  426. end;
  427. end;
  428. function tnotnode.pass_1 : tnode;
  429. var
  430. t : tnode;
  431. begin
  432. result:=nil;
  433. firstpass(left);
  434. set_varstate(left,true);
  435. if codegenerror then
  436. exit;
  437. if (left.nodetype=ordconstn) then
  438. begin
  439. if is_boolean(left.resulttype.def) then
  440. { here we do a boolena(byte(..)) type cast because }
  441. { boolean(<int64>) is buggy in 1.00 }
  442. t:=cordconstnode.create(byte(not(boolean(byte(tordconstnode(left).value)))),left.resulttype)
  443. else
  444. t:=cordconstnode.create(not(tordconstnode(left).value),left.resulttype);
  445. firstpass(t);
  446. result:=t;
  447. exit;
  448. end;
  449. location.loc:=left.location.loc;
  450. resulttype:=left.resulttype;
  451. registers32:=left.registers32;
  452. {$ifdef SUPPORT_MMX}
  453. registersmmx:=left.registersmmx;
  454. {$endif SUPPORT_MMX}
  455. if is_boolean(resulttype.def) then
  456. begin
  457. if (location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  458. begin
  459. location.loc:=LOC_REGISTER;
  460. if (registers32<1) then
  461. registers32:=1;
  462. end;
  463. { before loading it into flags we need to load it into
  464. a register thus 1 register is need PM }
  465. {$ifdef i386}
  466. if left.location.loc<>LOC_JUMP then
  467. location.loc:=LOC_FLAGS;
  468. {$endif def i386}
  469. end
  470. else
  471. {$ifdef SUPPORT_MMX}
  472. if (cs_mmx in aktlocalswitches) and
  473. is_mmx_able_array(left.resulttype.def) then
  474. begin
  475. if (left.location.loc<>LOC_MMXREGISTER) and
  476. (registersmmx<1) then
  477. registersmmx:=1;
  478. end
  479. else
  480. {$endif SUPPORT_MMX}
  481. if is_64bitint(left.resulttype.def) then
  482. begin
  483. if (location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  484. begin
  485. location.loc:=LOC_REGISTER;
  486. if (registers32<2) then
  487. registers32:=2;
  488. end;
  489. end
  490. else if is_integer(left.resulttype.def) then
  491. begin
  492. if (left.location.loc<>LOC_REGISTER) and
  493. (registers32<1) then
  494. registers32:=1;
  495. location.loc:=LOC_REGISTER;
  496. end
  497. end;
  498. begin
  499. cmoddivnode:=tmoddivnode;
  500. cshlshrnode:=tshlshrnode;
  501. cunaryminusnode:=tunaryminusnode;
  502. cnotnode:=tnotnode;
  503. end.
  504. {
  505. $Log$
  506. Revision 1.17 2001-04-02 21:20:31 peter
  507. * resulttype rewrite
  508. Revision 1.16 2001/03/20 18:11:03 jonas
  509. * not (cardinal) now has cardinal instead of longint result (bug reported
  510. in mailinglist) ("merged")
  511. Revision 1.15 2001/03/04 10:38:55 jonas
  512. * fixed 'qword mod/div pos_const' to have qword result
  513. Revision 1.14 2001/02/20 21:48:17 peter
  514. * remove nasm hack
  515. Revision 1.13 2001/01/06 18:28:39 peter
  516. * fixed wrong notes about locals
  517. Revision 1.12 2001/01/05 17:36:57 florian
  518. * the info about exception frames is stored now on the stack
  519. instead on the heap
  520. Revision 1.11 2000/12/25 00:07:26 peter
  521. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  522. tlinkedlist objects)
  523. Revision 1.10 2000/12/16 15:54:01 jonas
  524. * 'resulttype.def of cardinal shl/shr x' is cardinal instead of longint
  525. Revision 1.9 2000/11/29 00:30:34 florian
  526. * unused units removed from uses clause
  527. * some changes for widestrings
  528. Revision 1.8 2000/10/31 22:02:49 peter
  529. * symtable splitted, no real code changes
  530. Revision 1.7 2000/10/01 19:48:24 peter
  531. * lot of compile updates for cg11
  532. Revision 1.6 2000/09/27 21:33:22 florian
  533. * finally nadd.pas compiles
  534. Revision 1.5 2000/09/27 20:25:44 florian
  535. * more stuff fixed
  536. Revision 1.4 2000/09/24 15:06:19 peter
  537. * use defines.inc
  538. Revision 1.3 2000/09/22 22:48:54 florian
  539. * some fixes
  540. Revision 1.2 2000/09/22 22:09:54 florian
  541. * more stuff converted
  542. Revision 1.1 2000/09/20 21:35:12 florian
  543. * initial revision
  544. }