nmat.pas 23 KB

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