nmat.pas 19 KB

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