nmat.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. { nasm can not cope with negativ reals !! }
  265. if is_constrealnode(left)
  266. {$ifdef i386}
  267. and not(aktoutputformat in [as_i386_nasmcoff,as_i386_nasmelf,as_i386_nasmobj])
  268. {$endif i386}
  269. then
  270. begin
  271. t:=genrealconstnode(-trealconstnode(left).value_real,bestrealdef^);
  272. firstpass(t);
  273. pass_1:=t;
  274. exit;
  275. end;
  276. if (left.resulttype^.deftype=floatdef) then
  277. begin
  278. if pfloatdef(left.resulttype)^.typ=f32bit then
  279. begin
  280. if (left.location.loc<>LOC_REGISTER) and
  281. (registers32<1) then
  282. registers32:=1;
  283. location.loc:=LOC_REGISTER;
  284. end
  285. else
  286. location.loc:=LOC_FPU;
  287. end
  288. {$ifdef SUPPORT_MMX}
  289. else if (cs_mmx in aktlocalswitches) and
  290. is_mmx_able_array(left.resulttype) then
  291. begin
  292. if (left.location.loc<>LOC_MMXREGISTER) and
  293. (registersmmx<1) then
  294. registersmmx:=1;
  295. { if saturation is on, left.resulttype isn't
  296. "mmx able" (FK)
  297. if (cs_mmx_saturation in aktlocalswitches^) and
  298. (porddef(parraydef(resulttype)^.definition)^.typ in
  299. [s32bit,u32bit]) then
  300. CGMessage(type_e_mismatch);
  301. }
  302. end
  303. {$endif SUPPORT_MMX}
  304. else if is_64bitint(left.resulttype) then
  305. begin
  306. firstpass(left);
  307. registersfpu:=left.registersfpu;
  308. {$ifdef SUPPORT_MMX}
  309. registersmmx:=left.registersmmx;
  310. {$endif SUPPORT_MMX}
  311. registers32:=left.registers32;
  312. if codegenerror then
  313. exit;
  314. if (left.location.loc<>LOC_REGISTER) and
  315. (registers32<2) then
  316. registers32:=2;
  317. location.loc:=LOC_REGISTER;
  318. resulttype:=left.resulttype;
  319. end
  320. else if (left.resulttype^.deftype=orddef) then
  321. begin
  322. left:=gentypeconvnode(left,s32bitdef);
  323. firstpass(left);
  324. registersfpu:=left.registersfpu;
  325. {$ifdef SUPPORT_MMX}
  326. registersmmx:=left.registersmmx;
  327. {$endif SUPPORT_MMX}
  328. registers32:=left.registers32;
  329. if codegenerror then
  330. exit;
  331. if (left.location.loc<>LOC_REGISTER) and
  332. (registers32<1) then
  333. registers32:=1;
  334. location.loc:=LOC_REGISTER;
  335. resulttype:=left.resulttype;
  336. end
  337. else
  338. begin
  339. if assigned(overloaded_operators[_minus]) then
  340. minusdef:=overloaded_operators[_minus]^.definition
  341. else
  342. minusdef:=nil;
  343. while assigned(minusdef) do
  344. begin
  345. if is_equal(tparaitem(minusdef^.para.first).paratype.def,left.resulttype) and
  346. (tparaitem(minusdef^.para.first).next=nil) then
  347. begin
  348. t:=gencallnode(overloaded_operators[_minus],nil);
  349. tcallnode(t).left:=gencallparanode(left,nil);
  350. left:=nil;
  351. firstpass(t);
  352. pass_1:=t;
  353. exit;
  354. end;
  355. minusdef:=minusdef^.nextoverloaded;
  356. end;
  357. CGMessage(type_e_mismatch);
  358. end;
  359. end;
  360. {****************************************************************************
  361. TNOTNODE
  362. ****************************************************************************}
  363. constructor tnotnode.create(expr : tnode);
  364. begin
  365. inherited create(notn,expr);
  366. end;
  367. function tnotnode.pass_1 : tnode;
  368. var
  369. t : tnode;
  370. notdef : pprocdef;
  371. begin
  372. pass_1:=nil;
  373. firstpass(left);
  374. set_varstate(left,true);
  375. if codegenerror then
  376. exit;
  377. if (left.nodetype=ordconstn) then
  378. begin
  379. if is_boolean(left.resulttype) then
  380. { here we do a boolena(byte(..)) type cast because }
  381. { boolean(<int64>) is buggy in 1.00 }
  382. t:=genordinalconstnode(byte(not(boolean(byte(tordconstnode(left).value)))),left.resulttype)
  383. else
  384. t:=genordinalconstnode(not(tordconstnode(left).value),left.resulttype);
  385. firstpass(t);
  386. pass_1:=t;
  387. exit;
  388. end;
  389. resulttype:=left.resulttype;
  390. location.loc:=left.location.loc;
  391. {$ifdef SUPPORT_MMX}
  392. registersmmx:=left.registersmmx;
  393. {$endif SUPPORT_MMX}
  394. if is_boolean(resulttype) then
  395. begin
  396. registers32:=left.registers32;
  397. if (location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  398. begin
  399. location.loc:=LOC_REGISTER;
  400. if (registers32<1) then
  401. registers32:=1;
  402. end;
  403. { before loading it into flags we need to load it into
  404. a register thus 1 register is need PM }
  405. {$ifdef i386}
  406. if left.location.loc<>LOC_JUMP then
  407. location.loc:=LOC_FLAGS;
  408. {$endif def i386}
  409. end
  410. else
  411. {$ifdef SUPPORT_MMX}
  412. if (cs_mmx in aktlocalswitches) and
  413. is_mmx_able_array(left.resulttype) then
  414. begin
  415. if (left.location.loc<>LOC_MMXREGISTER) and
  416. (registersmmx<1) then
  417. registersmmx:=1;
  418. end
  419. else
  420. {$endif SUPPORT_MMX}
  421. if is_64bitint(left.resulttype) then
  422. begin
  423. registers32:=left.registers32;
  424. if (location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  425. begin
  426. location.loc:=LOC_REGISTER;
  427. if (registers32<2) then
  428. registers32:=2;
  429. end;
  430. end
  431. else if is_integer(left.resulttype) then
  432. begin
  433. left:=gentypeconvnode(left,s32bitdef);
  434. firstpass(left);
  435. if codegenerror then
  436. exit;
  437. resulttype:=left.resulttype;
  438. registers32:=left.registers32;
  439. {$ifdef SUPPORT_MMX}
  440. registersmmx:=left.registersmmx;
  441. {$endif SUPPORT_MMX}
  442. if (left.location.loc<>LOC_REGISTER) and
  443. (registers32<1) then
  444. registers32:=1;
  445. location.loc:=LOC_REGISTER;
  446. end
  447. else
  448. begin
  449. if assigned(overloaded_operators[_op_not]) then
  450. notdef:=overloaded_operators[_op_not]^.definition
  451. else
  452. notdef:=nil;
  453. while assigned(notdef) do
  454. begin
  455. if is_equal(tparaitem(notdef^.para.first).paratype.def,left.resulttype) and
  456. (tparaitem(notdef^.para.first).next=nil) then
  457. begin
  458. t:=gencallnode(overloaded_operators[_op_not],nil);
  459. tcallnode(t).left:=gencallparanode(left,nil);
  460. left:=nil;
  461. firstpass(t);
  462. pass_1:=t;
  463. exit;
  464. end;
  465. notdef:=notdef^.nextoverloaded;
  466. end;
  467. CGMessage(type_e_mismatch);
  468. end;
  469. registersfpu:=left.registersfpu;
  470. end;
  471. begin
  472. cmoddivnode:=tmoddivnode;
  473. cshlshrnode:=tshlshrnode;
  474. cunaryminusnode:=tunaryminusnode;
  475. cnotnode:=tnotnode;
  476. end.
  477. {
  478. $Log$
  479. Revision 1.13 2001-01-06 18:28:39 peter
  480. * fixed wrong notes about locals
  481. Revision 1.12 2001/01/05 17:36:57 florian
  482. * the info about exception frames is stored now on the stack
  483. instead on the heap
  484. Revision 1.11 2000/12/25 00:07:26 peter
  485. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  486. tlinkedlist objects)
  487. Revision 1.10 2000/12/16 15:54:01 jonas
  488. * 'resulttype of cardinal shl/shr x' is cardinal instead of longint
  489. Revision 1.9 2000/11/29 00:30:34 florian
  490. * unused units removed from uses clause
  491. * some changes for widestrings
  492. Revision 1.8 2000/10/31 22:02:49 peter
  493. * symtable splitted, no real code changes
  494. Revision 1.7 2000/10/01 19:48:24 peter
  495. * lot of compile updates for cg11
  496. Revision 1.6 2000/09/27 21:33:22 florian
  497. * finally nadd.pas compiles
  498. Revision 1.5 2000/09/27 20:25:44 florian
  499. * more stuff fixed
  500. Revision 1.4 2000/09/24 15:06:19 peter
  501. * use defines.inc
  502. Revision 1.3 2000/09/22 22:48:54 florian
  503. * some fixes
  504. Revision 1.2 2000/09/22 22:09:54 florian
  505. * more stuff converted
  506. Revision 1.1 2000/09/20 21:35:12 florian
  507. * initial revision
  508. }