nppcmat.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate PowerPC assembler 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 nppcmat;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nmat;
  23. type
  24. tppcmoddivnode = class(tmoddivnode)
  25. function pass_1: tnode;override;
  26. procedure pass_2;override;
  27. end;
  28. tppcshlshrnode = class(tshlshrnode)
  29. procedure pass_2;override;
  30. { everything will be handled in pass_2 }
  31. function first_shlshr64bitint: tnode; override;
  32. end;
  33. tppcunaryminusnode = class(tunaryminusnode)
  34. procedure pass_2;override;
  35. end;
  36. tppcnotnode = class(tnotnode)
  37. procedure pass_2;override;
  38. end;
  39. implementation
  40. uses
  41. globtype,systems,
  42. cutils,verbose,globals,
  43. symconst,symdef,
  44. aasmbase,aasmcpu,aasmtai,
  45. defutil,
  46. cgbase,cgutils,cgobj,pass_1,pass_2,
  47. ncon,procinfo,
  48. cpubase,cpuinfo,
  49. ncgutil,cgcpu,cg64f32,rgobj;
  50. {*****************************************************************************
  51. TPPCMODDIVNODE
  52. *****************************************************************************}
  53. function tppcmoddivnode.pass_1: tnode;
  54. begin
  55. result := inherited pass_1;
  56. if not assigned(result) then
  57. include(current_procinfo.flags,pi_do_call);
  58. end;
  59. procedure tppcmoddivnode.pass_2;
  60. const
  61. { signed overflow }
  62. divops: array[boolean, boolean] of tasmop =
  63. ((A_DIVWU,A_DIVWUO_),(A_DIVW,A_DIVWO_));
  64. zerocond: tasmcond = (dirhint: DH_Plus; simple: true; cond:C_NE; cr: RS_CR1);
  65. var
  66. power : longint;
  67. op : tasmop;
  68. numerator,
  69. divider,
  70. resultreg : tregister;
  71. size : Tcgsize;
  72. hl : tasmlabel;
  73. begin
  74. secondpass(left);
  75. secondpass(right);
  76. location_copy(location,left.location);
  77. { put numerator in register }
  78. size:=def_cgsize(left.resulttype.def);
  79. location_force_reg(exprasmlist,left.location,
  80. size,true);
  81. location_copy(location,left.location);
  82. numerator := location.register;
  83. resultreg := location.register;
  84. if (location.loc = LOC_CREGISTER) then
  85. begin
  86. location.loc := LOC_REGISTER;
  87. location.register := cg.getintregister(exprasmlist,size);
  88. resultreg := location.register;
  89. end;
  90. if (nodetype = modn) then
  91. begin
  92. resultreg := cg.getintregister(exprasmlist,size);
  93. end;
  94. if (nodetype = divn) and
  95. (right.nodetype = ordconstn) and
  96. ispowerof2(tordconstnode(right).value,power) then
  97. begin
  98. { From "The PowerPC Compiler Writer's Guide": }
  99. { This code uses the fact that, in the PowerPC architecture, }
  100. { the shift right algebraic instructions set the Carry bit if }
  101. { the source register contains a negative number and one or }
  102. { more 1-bits are shifted out. Otherwise, the carry bit is }
  103. { cleared. The addze instruction corrects the quotient, if }
  104. { necessary, when the dividend is negative. For example, if }
  105. { n = -13, (0xFFFF_FFF3), and k = 2, after executing the srawi }
  106. { instruction, q = -4 (0xFFFF_FFFC) and CA = 1. After executing }
  107. { the addze instruction, q = -3, the correct quotient. }
  108. cg.a_op_const_reg_reg(exprasmlist,OP_SAR,OS_32,power,
  109. numerator,resultreg);
  110. exprasmlist.concat(taicpu.op_reg_reg(A_ADDZE,resultreg,resultreg));
  111. end
  112. else
  113. begin
  114. { load divider in a register if necessary }
  115. location_force_reg(exprasmlist,right.location,
  116. def_cgsize(right.resulttype.def),true);
  117. if (right.nodetype <> ordconstn) then
  118. exprasmlist.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR1,
  119. right.location.register,0));
  120. divider := right.location.register;
  121. { needs overflow checking, (-maxlongint-1) div (-1) overflows! }
  122. { And on PPC, the only way to catch a div-by-0 is by checking }
  123. { the overflow flag (JM) }
  124. op := divops[is_signed(right.resulttype.def),
  125. cs_check_overflow in aktlocalswitches];
  126. exprasmlist.concat(taicpu.op_reg_reg_reg(op,resultreg,numerator,
  127. divider));
  128. if (nodetype = modn) then
  129. begin
  130. exprasmlist.concat(taicpu.op_reg_reg_reg(A_MULLW,resultreg,
  131. divider,resultreg));
  132. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB,location.register,
  133. numerator,resultreg));
  134. resultreg := location.register;
  135. end;
  136. end;
  137. { set result location }
  138. location.loc:=LOC_REGISTER;
  139. location.register:=resultreg;
  140. if right.nodetype <> ordconstn then
  141. begin
  142. objectlibrary.getlabel(hl);
  143. exprasmlist.concat(taicpu.op_cond_sym(A_BC,zerocond,hl));
  144. cg.a_call_name(exprasmlist,'FPC_DIVBYZERO');
  145. cg.a_label(exprasmlist,hl);
  146. end;
  147. cg.g_overflowcheck(exprasmlist,location,resulttype.def);
  148. end;
  149. {*****************************************************************************
  150. TPPCSHLRSHRNODE
  151. *****************************************************************************}
  152. function tppcshlshrnode.first_shlshr64bitint: tnode;
  153. begin
  154. result := nil;
  155. end;
  156. procedure tppcshlshrnode.pass_2;
  157. var
  158. resultreg, hregister1,hregister2,
  159. hreg64hi,hreg64lo : tregister;
  160. op : topcg;
  161. asmop1, asmop2: tasmop;
  162. shiftval: aint;
  163. begin
  164. secondpass(left);
  165. secondpass(right);
  166. if is_64bitint(left.resulttype.def) then
  167. begin
  168. location_force_reg(exprasmlist,left.location,
  169. def_cgsize(left.resulttype.def),true);
  170. location_copy(location,left.location);
  171. hreg64hi := location.register64.reghi;
  172. hreg64lo := location.register64.reglo;
  173. if (location.loc = LOC_CREGISTER) then
  174. begin
  175. location.loc := LOC_REGISTER;
  176. location.register64.reghi := cg.getintregister(exprasmlist,OS_32);
  177. location.register64.reglo := cg.getintregister(exprasmlist,OS_32);
  178. end;
  179. if (right.nodetype = ordconstn) then
  180. begin
  181. shiftval := tordconstnode(right).value;
  182. shiftval := shiftval and 63;
  183. {
  184. I think the statements below is much more correct instead of the hack above,
  185. but then we fail tshlshr.pp :/
  186. if shiftval > 63 then
  187. begin
  188. cg.a_load_const_reg(exprasmlist,OS_32,0,location.register64.reglo);
  189. cg.a_load_const_reg(exprasmlist,OS_32,0,location.register64.reglo);
  190. end
  191. else } if shiftval > 31 then
  192. begin
  193. if nodetype = shln then
  194. begin
  195. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,
  196. shiftval and 31,hreg64lo,location.register64.reghi);
  197. cg.a_load_const_reg(exprasmlist,OS_32,0,location.register64.reglo);
  198. end
  199. else
  200. begin
  201. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,
  202. shiftval and 31,hreg64hi,location.register64.reglo);
  203. cg.a_load_const_reg(exprasmlist,OS_32,0,location.register64.reghi);
  204. end;
  205. end
  206. else
  207. begin
  208. if nodetype = shln then
  209. begin
  210. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  211. A_RLWINM,location.register64.reghi,hreg64hi,shiftval,
  212. 0,31-shiftval));
  213. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  214. A_RLWIMI,location.register64.reghi,hreg64lo,shiftval,
  215. 32-shiftval,31));
  216. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  217. A_RLWINM,location.register64.reglo,hreg64lo,shiftval,
  218. 0,31-shiftval));
  219. end
  220. else
  221. begin
  222. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  223. A_RLWINM,location.register64.reglo,hreg64lo,32-shiftval,
  224. shiftval,31));
  225. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  226. A_RLWIMI,location.register64.reglo,hreg64hi,32-shiftval,
  227. 0,shiftval-1));
  228. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  229. A_RLWINM,location.register64.reghi,hreg64hi,32-shiftval,
  230. shiftval,31));
  231. end;
  232. end;
  233. end
  234. else
  235. { no constant shiftcount }
  236. begin
  237. location_force_reg(exprasmlist,right.location,OS_S32,true);
  238. hregister1 := right.location.register;
  239. if nodetype = shln then
  240. begin
  241. asmop1 := A_SLW;
  242. asmop2 := A_SRW;
  243. end
  244. else
  245. begin
  246. asmop1 := A_SRW;
  247. asmop2 := A_SLW;
  248. resultreg := hreg64hi;
  249. hreg64hi := hreg64lo;
  250. hreg64lo := resultreg;
  251. resultreg := location.register64.reghi;
  252. location.register64.reghi := location.register64.reglo;
  253. location.register64.reglo := resultreg;
  254. end;
  255. cg.getcpuregister(exprasmlist,NR_R0);
  256. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  257. NR_R0,hregister1,32));
  258. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  259. location.register64.reghi,hreg64hi,hregister1));
  260. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop2,
  261. NR_R0,hreg64lo,NR_R0));
  262. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  263. location.register64.reghi,location.register64.reghi,NR_R0));
  264. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBI,
  265. NR_R0,hregister1,32));
  266. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  267. NR_R0,hreg64lo,NR_R0));
  268. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  269. location.register64.reghi,location.register64.reghi,NR_R0));
  270. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  271. location.register64.reglo,hreg64lo,hregister1));
  272. cg.ungetcpuregister(exprasmlist,NR_R0);
  273. if nodetype = shrn then
  274. begin
  275. resultreg := location.register64.reghi;
  276. location.register64.reghi := location.register64.reglo;
  277. location.register64.reglo := resultreg;
  278. end;
  279. end
  280. end
  281. else
  282. begin
  283. { load left operators in a register }
  284. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  285. location_copy(location,left.location);
  286. resultreg := location.register;
  287. hregister1 := location.register;
  288. if (location.loc = LOC_CREGISTER) then
  289. begin
  290. location.loc := LOC_REGISTER;
  291. resultreg := cg.getintregister(exprasmlist,OS_32);
  292. location.register := resultreg;
  293. end;
  294. { determine operator }
  295. if nodetype=shln then
  296. op:=OP_SHL
  297. else
  298. op:=OP_SHR;
  299. { shifting by a constant directly coded: }
  300. if (right.nodetype=ordconstn) then
  301. cg.a_op_const_reg_reg(exprasmlist,op,OS_32,
  302. tordconstnode(right).value and 31,hregister1,resultreg)
  303. else
  304. begin
  305. { load shift count in a register if necessary }
  306. location_force_reg(exprasmlist,right.location,def_cgsize(right.resulttype.def),true);
  307. hregister2 := right.location.register;
  308. cg.a_op_reg_reg_reg(exprasmlist,op,OS_32,hregister2,
  309. hregister1,resultreg);
  310. end;
  311. end;
  312. end;
  313. {*****************************************************************************
  314. TPPCUNARYMINUSNODE
  315. *****************************************************************************}
  316. procedure tppcunaryminusnode.pass_2;
  317. var
  318. src1: tregister;
  319. op: tasmop;
  320. begin
  321. secondpass(left);
  322. if is_64bitint(left.resulttype.def) then
  323. begin
  324. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  325. location_copy(location,left.location);
  326. if (location.loc = LOC_CREGISTER) then
  327. begin
  328. location.register64.reglo := cg.getintregister(exprasmlist,OS_INT);
  329. location.register64.reghi := cg.getintregister(exprasmlist,OS_INT);
  330. location.loc := LOC_REGISTER;
  331. end;
  332. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  333. location.register64.reglo,left.location.register64.reglo,0));
  334. if not(cs_check_overflow in aktlocalswitches) then
  335. exprasmlist.concat(taicpu.op_reg_reg(A_SUBFZE,
  336. location.register64.reghi,left.location.register64.reghi))
  337. else
  338. exprasmlist.concat(taicpu.op_reg_reg(A_SUBFZEO_,
  339. location.register64.reghi,left.location.register64.reghi));
  340. end
  341. else
  342. begin
  343. location_copy(location,left.location);
  344. location.loc:=LOC_REGISTER;
  345. case left.location.loc of
  346. LOC_FPUREGISTER, LOC_REGISTER:
  347. begin
  348. src1 := left.location.register;
  349. location.register := src1;
  350. end;
  351. LOC_CFPUREGISTER, LOC_CREGISTER:
  352. begin
  353. src1 := left.location.register;
  354. if left.location.loc = LOC_CREGISTER then
  355. location.register := cg.getintregister(exprasmlist,OS_INT)
  356. else
  357. location.register := cg.getfpuregister(exprasmlist,location.size);
  358. end;
  359. LOC_REFERENCE,LOC_CREFERENCE:
  360. begin
  361. if (left.resulttype.def.deftype=floatdef) then
  362. begin
  363. src1 := cg.getfpuregister(exprasmlist,def_cgsize(left.resulttype.def));
  364. location.register := src1;
  365. cg.a_loadfpu_ref_reg(exprasmlist,
  366. def_cgsize(left.resulttype.def),
  367. left.location.reference,src1);
  368. end
  369. else
  370. begin
  371. src1 := cg.getintregister(exprasmlist,OS_32);
  372. location.register:= src1;
  373. cg.a_load_ref_reg(exprasmlist,OS_32,OS_32,
  374. left.location.reference,src1);
  375. end;
  376. end;
  377. end;
  378. { choose appropriate operand }
  379. if left.resulttype.def.deftype <> floatdef then
  380. begin
  381. if not(cs_check_overflow in aktlocalswitches) then
  382. op := A_NEG
  383. else
  384. op := A_NEGO_;
  385. location.loc := LOC_REGISTER;
  386. end
  387. else
  388. begin
  389. op := A_FNEG;
  390. location.loc := LOC_FPUREGISTER;
  391. end;
  392. { emit operation }
  393. exprasmlist.concat(taicpu.op_reg_reg(op,location.register,src1));
  394. end;
  395. { Here was a problem... }
  396. { Operand to be negated always }
  397. { seems to be converted to signed }
  398. { 32-bit before doing neg!! }
  399. { So this is useless... }
  400. { that's not true: -2^31 gives an overflow error if it is negated (FK) }
  401. cg.g_overflowcheck(exprasmlist,location,resulttype.def);
  402. end;
  403. {*****************************************************************************
  404. TPPCNOTNODE
  405. *****************************************************************************}
  406. procedure tppcnotnode.pass_2;
  407. var
  408. hl : tasmlabel;
  409. begin
  410. if is_boolean(resulttype.def) then
  411. begin
  412. { if the location is LOC_JUMP, we do the secondpass after the
  413. labels are allocated
  414. }
  415. if left.expectloc=LOC_JUMP then
  416. begin
  417. hl:=truelabel;
  418. truelabel:=falselabel;
  419. falselabel:=hl;
  420. secondpass(left);
  421. maketojumpbool(exprasmlist,left,lr_load_regvars);
  422. hl:=truelabel;
  423. truelabel:=falselabel;
  424. falselabel:=hl;
  425. location.loc:=LOC_JUMP;
  426. end
  427. else
  428. begin
  429. secondpass(left);
  430. case left.location.loc of
  431. LOC_FLAGS :
  432. begin
  433. location_copy(location,left.location);
  434. inverse_flags(location.resflags);
  435. end;
  436. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE :
  437. begin
  438. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  439. exprasmlist.concat(taicpu.op_reg_const(A_CMPWI,left.location.register,0));
  440. location_reset(location,LOC_FLAGS,OS_NO);
  441. location.resflags.cr:=RS_CR0;
  442. location.resflags.flag:=F_EQ;
  443. end;
  444. else
  445. internalerror(2003042401);
  446. end;
  447. end;
  448. end
  449. else if is_64bitint(left.resulttype.def) then
  450. begin
  451. secondpass(left);
  452. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  453. location_copy(location,left.location);
  454. { perform the NOT operation }
  455. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.register64.reghi,
  456. location.register64.reghi));
  457. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.register64.reglo,
  458. location.register64.reglo));
  459. end
  460. else
  461. begin
  462. secondpass(left);
  463. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  464. location_copy(location,left.location);
  465. location.loc := LOC_REGISTER;
  466. location.register := cg.getintregister(exprasmlist,OS_INT);
  467. { perform the NOT operation }
  468. cg.a_op_reg_reg(exprasmlist,OP_NOT,def_cgsize(resulttype.def),left.location.register,
  469. location.register);
  470. end;
  471. end;
  472. begin
  473. cmoddivnode:=tppcmoddivnode;
  474. cshlshrnode:=tppcshlshrnode;
  475. cunaryminusnode:=tppcunaryminusnode;
  476. cnotnode:=tppcnotnode;
  477. end.
  478. {
  479. $Log$
  480. Revision 1.45 2005-03-25 21:55:43 jonas
  481. * removed some unused variables
  482. Revision 1.44 2005/02/14 17:13:10 peter
  483. * truncate log
  484. }