nmat.pas 18 KB

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