nmat.pas 21 KB

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