nmat.pas 20 KB

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