nmat.pas 18 KB

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