nmat.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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,symtable;
  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. cobjects,verbose,globals,
  47. symconst,aasm,types,
  48. htypechk,pass_1,cpubase,cpuinfo,
  49. {$ifdef newcg}
  50. cgbase,
  51. {$else newcg}
  52. hcodegen,
  53. {$endif newcg}
  54. { for isbinaryoverloaded function }
  55. nadd,
  56. ncon,ncnv,ncal;
  57. {****************************************************************************
  58. TMODDIVNODE
  59. ****************************************************************************}
  60. function tmoddivnode.pass_1 : tnode;
  61. var
  62. t : tnode;
  63. rv,lv : tconstexprint;
  64. rd,ld : pdef;
  65. begin
  66. pass_1:=nil;
  67. firstpass(left);
  68. set_varstate(right,true);
  69. firstpass(right);
  70. set_varstate(right,true);
  71. if codegenerror then
  72. exit;
  73. t:=self;
  74. if isbinaryoverloaded(t) then
  75. begin
  76. pass_1:=t;
  77. exit;
  78. end;
  79. { check for division by zero }
  80. rv:=tordconstnode(right).value;
  81. lv:=tordconstnode(left).value;
  82. if is_constintnode(right) and (rv=0) then
  83. begin
  84. Message(parser_e_division_by_zero);
  85. { recover }
  86. rv:=1;
  87. end;
  88. if is_constintnode(left) and is_constintnode(right) then
  89. begin
  90. case nodetype of
  91. modn:
  92. t:=genintconstnode(lv mod rv);
  93. divn:
  94. t:=genintconstnode(lv div rv);
  95. end;
  96. firstpass(t);
  97. pass_1:=t;
  98. exit;
  99. end;
  100. if (left.resulttype^.deftype=orddef) and (right.resulttype^.deftype=orddef) and
  101. (is_64bitint(left.resulttype) or is_64bitint(right.resulttype)) then
  102. begin
  103. rd:=right.resulttype;
  104. ld:=left.resulttype;
  105. if (porddef(rd)^.typ=s64bit) or (porddef(ld)^.typ=s64bit) then
  106. begin
  107. if (porddef(ld)^.typ<>s64bit) then
  108. begin
  109. left:=gentypeconvnode(left,cs64bitdef);
  110. firstpass(left);
  111. end;
  112. if (porddef(rd)^.typ<>s64bit) then
  113. begin
  114. right:=gentypeconvnode(right,cs64bitdef);
  115. firstpass(right);
  116. end;
  117. calcregisters(self,2,0,0);
  118. end
  119. else if (porddef(rd)^.typ=u64bit) or (porddef(ld)^.typ=u64bit) then
  120. begin
  121. if (porddef(ld)^.typ<>u64bit) then
  122. begin
  123. left:=gentypeconvnode(left,cu64bitdef);
  124. firstpass(left);
  125. end;
  126. if (porddef(rd)^.typ<>u64bit) then
  127. begin
  128. right:=gentypeconvnode(right,cu64bitdef);
  129. firstpass(right);
  130. end;
  131. calcregisters(self,2,0,0);
  132. end;
  133. resulttype:=left.resulttype;
  134. end
  135. else
  136. begin
  137. if not(right.resulttype^.deftype=orddef) or
  138. not(porddef(right.resulttype)^.typ in [s32bit,u32bit]) then
  139. right:=gentypeconvnode(right,s32bitdef);
  140. if not(left.resulttype^.deftype=orddef) or
  141. not(porddef(left.resulttype)^.typ in [s32bit,u32bit]) then
  142. left:=gentypeconvnode(left,s32bitdef);
  143. firstpass(left);
  144. firstpass(right);
  145. {$ifdef cardinalmulfix}
  146. { if we divide a u32bit by a positive constant, the result is also u32bit (JM) }
  147. if (left.resulttype^.deftype = orddef) and
  148. (left.resulttype^.deftype = orddef) then
  149. begin
  150. if (porddef(left.resulttype)^.typ = u32bit) and
  151. is_constintnode(right) and
  152. { (porddef(right.resulttype)^.typ <> u32bit) and}
  153. (right.value > 0) then
  154. begin
  155. right := gentypeconvnode(right,u32bitdef);
  156. firstpass(right);
  157. end;
  158. { adjust also the left resulttype if necessary }
  159. if (porddef(right.resulttype)^.typ = u32bit) and
  160. is_constintnode(left) and
  161. { (porddef(left.resulttype)^.typ <> u32bit) and}
  162. (left.value > 0) then
  163. begin
  164. left := gentypeconvnode(left,u32bitdef);
  165. firstpass(left);
  166. end;
  167. end;
  168. {$endif cardinalmulfix}
  169. { the resulttype depends on the right side, because the left becomes }
  170. { always 64 bit }
  171. resulttype:=right.resulttype;
  172. if codegenerror then
  173. exit;
  174. left_right_max;
  175. if left.registers32<=right.registers32 then
  176. inc(registers32);
  177. end;
  178. location.loc:=LOC_REGISTER;
  179. end;
  180. {****************************************************************************
  181. TSHLSHRNODE
  182. ****************************************************************************}
  183. function tshlshrnode.pass_1 : tnode;
  184. var
  185. t : tnode;
  186. regs : longint;
  187. begin
  188. pass_1:=nil;
  189. firstpass(left);
  190. set_varstate(left,true);
  191. firstpass(right);
  192. set_varstate(right,true);
  193. if codegenerror then
  194. exit;
  195. t:=self;
  196. if isbinaryoverloaded(t) then
  197. begin
  198. pass_1:=t;
  199. exit;
  200. end;
  201. if is_constintnode(left) and is_constintnode(right) then
  202. begin
  203. case nodetype of
  204. shrn:
  205. t:=genintconstnode(tordconstnode(left).value shr tordconstnode(right).value);
  206. shln:
  207. t:=genintconstnode(tordconstnode(left).value shl tordconstnode(right).value);
  208. end;
  209. firstpass(t);
  210. pass_1:=t;
  211. exit;
  212. end;
  213. { 64 bit ints have their own shift handling }
  214. if not(is_64bitint(left.resulttype)) then
  215. begin
  216. left:=gentypeconvnode(left,s32bitdef);
  217. firstpass(left);
  218. regs:=1;
  219. resulttype:=s32bitdef;
  220. end
  221. else
  222. begin
  223. resulttype:=left.resulttype;
  224. regs:=2;
  225. end;
  226. right:=gentypeconvnode(right,s32bitdef);
  227. firstpass(right);
  228. if codegenerror then
  229. exit;
  230. if (right.nodetype<>ordconstn) then
  231. inc(regs);
  232. calcregisters(self,regs,0,0);
  233. location.loc:=LOC_REGISTER;
  234. end;
  235. {****************************************************************************
  236. TUNARYMINUSNODE
  237. ****************************************************************************}
  238. constructor tunaryminusnode.create(expr : tnode);
  239. begin
  240. inherited create(unaryminusn,expr);
  241. end;
  242. function tunaryminusnode.pass_1 : tnode;
  243. var
  244. t : tnode;
  245. minusdef : pprocdef;
  246. begin
  247. pass_1:=nil;
  248. firstpass(left);
  249. set_varstate(left,true);
  250. registers32:=left.registers32;
  251. registersfpu:=left.registersfpu;
  252. {$ifdef SUPPORT_MMX}
  253. registersmmx:=left.registersmmx;
  254. {$endif SUPPORT_MMX}
  255. resulttype:=left.resulttype;
  256. if codegenerror then
  257. exit;
  258. if is_constintnode(left) then
  259. begin
  260. t:=genintconstnode(-tordconstnode(left).value);
  261. firstpass(t);
  262. pass_1:=t;
  263. exit;
  264. end;
  265. { nasm can not cope with negativ reals !! }
  266. if is_constrealnode(left)
  267. {$ifdef i386}
  268. and not(aktoutputformat in [as_i386_nasmcoff,as_i386_nasmelf,as_i386_nasmobj])
  269. {$endif i386}
  270. then
  271. begin
  272. t:=genrealconstnode(-trealconstnode(left).value_real,bestrealdef^);
  273. firstpass(t);
  274. pass_1:=t;
  275. exit;
  276. end;
  277. if (left.resulttype^.deftype=floatdef) then
  278. begin
  279. if pfloatdef(left.resulttype)^.typ=f32bit then
  280. begin
  281. if (left.location.loc<>LOC_REGISTER) and
  282. (registers32<1) then
  283. registers32:=1;
  284. location.loc:=LOC_REGISTER;
  285. end
  286. else
  287. location.loc:=LOC_FPU;
  288. end
  289. {$ifdef SUPPORT_MMX}
  290. else if (cs_mmx in aktlocalswitches) and
  291. is_mmx_able_array(left.resulttype) then
  292. begin
  293. if (left.location.loc<>LOC_MMXREGISTER) and
  294. (registersmmx<1) then
  295. registersmmx:=1;
  296. { if saturation is on, left.resulttype isn't
  297. "mmx able" (FK)
  298. if (cs_mmx_saturation in aktlocalswitches^) and
  299. (porddef(parraydef(resulttype)^.definition)^.typ in
  300. [s32bit,u32bit]) then
  301. CGMessage(type_e_mismatch);
  302. }
  303. end
  304. {$endif SUPPORT_MMX}
  305. else if is_64bitint(left.resulttype) then
  306. begin
  307. firstpass(left);
  308. registersfpu:=left.registersfpu;
  309. {$ifdef SUPPORT_MMX}
  310. registersmmx:=left.registersmmx;
  311. {$endif SUPPORT_MMX}
  312. registers32:=left.registers32;
  313. if codegenerror then
  314. exit;
  315. if (left.location.loc<>LOC_REGISTER) and
  316. (registers32<2) then
  317. registers32:=2;
  318. location.loc:=LOC_REGISTER;
  319. resulttype:=left.resulttype;
  320. end
  321. else if (left.resulttype^.deftype=orddef) then
  322. begin
  323. left:=gentypeconvnode(left,s32bitdef);
  324. firstpass(left);
  325. registersfpu:=left.registersfpu;
  326. {$ifdef SUPPORT_MMX}
  327. registersmmx:=left.registersmmx;
  328. {$endif SUPPORT_MMX}
  329. registers32:=left.registers32;
  330. if codegenerror then
  331. exit;
  332. if (left.location.loc<>LOC_REGISTER) and
  333. (registers32<1) then
  334. registers32:=1;
  335. location.loc:=LOC_REGISTER;
  336. resulttype:=left.resulttype;
  337. end
  338. else
  339. begin
  340. if assigned(overloaded_operators[_minus]) then
  341. minusdef:=overloaded_operators[_minus]^.definition
  342. else
  343. minusdef:=nil;
  344. while assigned(minusdef) do
  345. begin
  346. if is_equal(pparaitem(minusdef^.para^.first)^.paratype.def,left.resulttype) and
  347. (pparaitem(minusdef^.para^.first)^.next=nil) then
  348. begin
  349. t:=gencallnode(overloaded_operators[_minus],nil);
  350. tcallnode(t).left:=gencallparanode(left,nil);
  351. left:=nil;
  352. firstpass(t);
  353. pass_1:=t;
  354. exit;
  355. end;
  356. minusdef:=minusdef^.nextoverloaded;
  357. end;
  358. CGMessage(type_e_mismatch);
  359. end;
  360. end;
  361. {****************************************************************************
  362. TNOTNODE
  363. ****************************************************************************}
  364. constructor tnotnode.create(expr : tnode);
  365. begin
  366. inherited create(notn,expr);
  367. end;
  368. function tnotnode.pass_1 : tnode;
  369. var
  370. t : tnode;
  371. notdef : pprocdef;
  372. begin
  373. pass_1:=nil;
  374. firstpass(left);
  375. set_varstate(left,true);
  376. if codegenerror then
  377. exit;
  378. if (left.nodetype=ordconstn) then
  379. begin
  380. if is_boolean(left.resulttype) then
  381. { here we do a boolena(byte(..)) type cast because }
  382. { boolean(<int64>) is buggy in 1.00 }
  383. t:=genordinalconstnode(byte(not(boolean(byte(tordconstnode(left).value)))),left.resulttype)
  384. else
  385. t:=genordinalconstnode(not(tordconstnode(left).value),left.resulttype);
  386. firstpass(t);
  387. pass_1:=t;
  388. exit;
  389. end;
  390. resulttype:=left.resulttype;
  391. location.loc:=left.location.loc;
  392. {$ifdef SUPPORT_MMX}
  393. registersmmx:=left.registersmmx;
  394. {$endif SUPPORT_MMX}
  395. if is_boolean(resulttype) then
  396. begin
  397. registers32:=left.registers32;
  398. if (location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  399. begin
  400. location.loc:=LOC_REGISTER;
  401. if (registers32<1) then
  402. registers32:=1;
  403. end;
  404. { before loading it into flags we need to load it into
  405. a register thus 1 register is need PM }
  406. {$ifdef i386}
  407. if left.location.loc<>LOC_JUMP then
  408. location.loc:=LOC_FLAGS;
  409. {$endif def i386}
  410. end
  411. else
  412. {$ifdef SUPPORT_MMX}
  413. if (cs_mmx in aktlocalswitches) and
  414. is_mmx_able_array(left.resulttype) then
  415. begin
  416. if (left.location.loc<>LOC_MMXREGISTER) and
  417. (registersmmx<1) then
  418. registersmmx:=1;
  419. end
  420. else
  421. {$endif SUPPORT_MMX}
  422. if is_64bitint(left.resulttype) then
  423. begin
  424. registers32:=left.registers32;
  425. if (location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  426. begin
  427. location.loc:=LOC_REGISTER;
  428. if (registers32<2) then
  429. registers32:=2;
  430. end;
  431. end
  432. else if is_integer(left.resulttype) then
  433. begin
  434. left:=gentypeconvnode(left,s32bitdef);
  435. firstpass(left);
  436. if codegenerror then
  437. exit;
  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(pparaitem(notdef^.para^.first)^.paratype.def,left.resulttype) and
  457. (pparaitem(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.7 2000-10-01 19:48:24 peter
  481. * lot of compile updates for cg11
  482. Revision 1.6 2000/09/27 21:33:22 florian
  483. * finally nadd.pas compiles
  484. Revision 1.5 2000/09/27 20:25:44 florian
  485. * more stuff fixed
  486. Revision 1.4 2000/09/24 15:06:19 peter
  487. * use defines.inc
  488. Revision 1.3 2000/09/22 22:48:54 florian
  489. * some fixes
  490. Revision 1.2 2000/09/22 22:09:54 florian
  491. * more stuff converted
  492. Revision 1.1 2000/09/20 21:35:12 florian
  493. * initial revision
  494. }