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