nppcmat.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. r : Tregister;
  164. begin
  165. secondpass(left);
  166. secondpass(right);
  167. if is_64bitint(left.resulttype.def) then
  168. begin
  169. location_force_reg(exprasmlist,left.location,
  170. def_cgsize(left.resulttype.def),true);
  171. location_copy(location,left.location);
  172. hreg64hi := location.register64.reghi;
  173. hreg64lo := location.register64.reglo;
  174. if (location.loc = LOC_CREGISTER) then
  175. begin
  176. location.loc := LOC_REGISTER;
  177. location.register64.reghi := cg.getintregister(exprasmlist,OS_32);
  178. location.register64.reglo := cg.getintregister(exprasmlist,OS_32);
  179. end;
  180. if (right.nodetype = ordconstn) then
  181. begin
  182. shiftval := tordconstnode(right).value;
  183. if shiftval > 63 then
  184. begin
  185. cg.a_load_const_reg(exprasmlist,OS_32,0,location.register64.reglo);
  186. cg.a_load_const_reg(exprasmlist,OS_32,0,location.register64.reglo);
  187. end
  188. else if shiftval > 31 then
  189. begin
  190. if nodetype = shln then
  191. begin
  192. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,
  193. shiftval and 31,hreg64lo,location.register64.reghi);
  194. cg.a_load_const_reg(exprasmlist,OS_32,0,location.register64.reglo);
  195. end
  196. else
  197. begin
  198. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,
  199. shiftval and 31,hreg64hi,location.register64.reglo);
  200. cg.a_load_const_reg(exprasmlist,OS_32,0,location.register64.reghi);
  201. end;
  202. end
  203. else
  204. begin
  205. if nodetype = shln then
  206. begin
  207. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  208. A_RLWINM,location.register64.reghi,hreg64hi,shiftval,
  209. 0,31-shiftval));
  210. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  211. A_RLWIMI,location.register64.reghi,hreg64lo,shiftval,
  212. 32-shiftval,31));
  213. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  214. A_RLWINM,location.register64.reglo,hreg64lo,shiftval,
  215. 0,31-shiftval));
  216. end
  217. else
  218. begin
  219. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  220. A_RLWINM,location.register64.reglo,hreg64lo,32-shiftval,
  221. shiftval,31));
  222. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  223. A_RLWIMI,location.register64.reglo,hreg64hi,32-shiftval,
  224. 0,shiftval-1));
  225. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  226. A_RLWINM,location.register64.reghi,hreg64hi,32-shiftval,
  227. shiftval,31));
  228. end;
  229. end;
  230. end
  231. else
  232. { no constant shiftcount }
  233. begin
  234. location_force_reg(exprasmlist,right.location,OS_S32,true);
  235. hregister1 := right.location.register;
  236. if nodetype = shln then
  237. begin
  238. asmop1 := A_SLW;
  239. asmop2 := A_SRW;
  240. end
  241. else
  242. begin
  243. asmop1 := A_SRW;
  244. asmop2 := A_SLW;
  245. resultreg := hreg64hi;
  246. hreg64hi := hreg64lo;
  247. hreg64lo := resultreg;
  248. resultreg := location.register64.reghi;
  249. location.register64.reghi := location.register64.reglo;
  250. location.register64.reglo := resultreg;
  251. end;
  252. cg.getcpuregister(exprasmlist,NR_R0);
  253. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  254. NR_R0,hregister1,32));
  255. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  256. location.register64.reghi,hreg64hi,hregister1));
  257. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop2,
  258. NR_R0,hreg64lo,NR_R0));
  259. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  260. location.register64.reghi,location.register64.reghi,NR_R0));
  261. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBI,
  262. NR_R0,hregister1,32));
  263. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  264. NR_R0,hreg64lo,NR_R0));
  265. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  266. location.register64.reghi,location.register64.reghi,NR_R0));
  267. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  268. location.register64.reglo,hreg64lo,hregister1));
  269. cg.ungetcpuregister(exprasmlist,NR_R0);
  270. if nodetype = shrn then
  271. begin
  272. resultreg := location.register64.reghi;
  273. location.register64.reghi := location.register64.reglo;
  274. location.register64.reglo := resultreg;
  275. end;
  276. end
  277. end
  278. else
  279. begin
  280. { load left operators in a register }
  281. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  282. location_copy(location,left.location);
  283. resultreg := location.register;
  284. hregister1 := location.register;
  285. if (location.loc = LOC_CREGISTER) then
  286. begin
  287. location.loc := LOC_REGISTER;
  288. resultreg := cg.getintregister(exprasmlist,OS_32);
  289. location.register := resultreg;
  290. end;
  291. { determine operator }
  292. if nodetype=shln then
  293. op:=OP_SHL
  294. else
  295. op:=OP_SHR;
  296. { shifting by a constant directly coded: }
  297. if (right.nodetype=ordconstn) then
  298. cg.a_op_const_reg_reg(exprasmlist,op,OS_32,
  299. tordconstnode(right).value and 31,hregister1,resultreg)
  300. else
  301. begin
  302. { load shift count in a register if necessary }
  303. location_force_reg(exprasmlist,right.location,def_cgsize(right.resulttype.def),true);
  304. hregister2 := right.location.register;
  305. cg.a_op_reg_reg_reg(exprasmlist,op,OS_32,hregister2,
  306. hregister1,resultreg);
  307. end;
  308. end;
  309. end;
  310. {*****************************************************************************
  311. TPPCUNARYMINUSNODE
  312. *****************************************************************************}
  313. procedure tppcunaryminusnode.pass_2;
  314. var
  315. src1, src2, tmp: tregister;
  316. op: tasmop;
  317. begin
  318. secondpass(left);
  319. if is_64bitint(left.resulttype.def) then
  320. begin
  321. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  322. location_copy(location,left.location);
  323. if (location.loc = LOC_CREGISTER) then
  324. begin
  325. location.register64.reglo := cg.getintregister(exprasmlist,OS_INT);
  326. location.register64.reghi := cg.getintregister(exprasmlist,OS_INT);
  327. location.loc := LOC_REGISTER;
  328. end;
  329. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  330. location.register64.reglo,left.location.register64.reglo,0));
  331. if not(cs_check_overflow in aktlocalswitches) then
  332. exprasmlist.concat(taicpu.op_reg_reg(A_SUBFZE,
  333. location.register64.reghi,left.location.register64.reghi))
  334. else
  335. exprasmlist.concat(taicpu.op_reg_reg(A_SUBFZEO_,
  336. location.register64.reghi,left.location.register64.reghi));
  337. end
  338. else
  339. begin
  340. location_copy(location,left.location);
  341. location.loc:=LOC_REGISTER;
  342. case left.location.loc of
  343. LOC_FPUREGISTER, LOC_REGISTER:
  344. begin
  345. src1 := left.location.register;
  346. location.register := src1;
  347. end;
  348. LOC_CFPUREGISTER, LOC_CREGISTER:
  349. begin
  350. src1 := left.location.register;
  351. if left.location.loc = LOC_CREGISTER then
  352. location.register := cg.getintregister(exprasmlist,OS_INT)
  353. else
  354. location.register := cg.getfpuregister(exprasmlist,location.size);
  355. end;
  356. LOC_REFERENCE,LOC_CREFERENCE:
  357. begin
  358. if (left.resulttype.def.deftype=floatdef) then
  359. begin
  360. src1 := cg.getfpuregister(exprasmlist,def_cgsize(left.resulttype.def));
  361. location.register := src1;
  362. cg.a_loadfpu_ref_reg(exprasmlist,
  363. def_cgsize(left.resulttype.def),
  364. left.location.reference,src1);
  365. end
  366. else
  367. begin
  368. src1 := cg.getintregister(exprasmlist,OS_32);
  369. location.register:= src1;
  370. cg.a_load_ref_reg(exprasmlist,OS_32,OS_32,
  371. left.location.reference,src1);
  372. end;
  373. end;
  374. end;
  375. { choose appropriate operand }
  376. if left.resulttype.def.deftype <> floatdef then
  377. begin
  378. if not(cs_check_overflow in aktlocalswitches) then
  379. op := A_NEG
  380. else
  381. op := A_NEGO_;
  382. location.loc := LOC_REGISTER;
  383. end
  384. else
  385. begin
  386. op := A_FNEG;
  387. location.loc := LOC_FPUREGISTER;
  388. end;
  389. { emit operation }
  390. exprasmlist.concat(taicpu.op_reg_reg(op,location.register,src1));
  391. end;
  392. { Here was a problem... }
  393. { Operand to be negated always }
  394. { seems to be converted to signed }
  395. { 32-bit before doing neg!! }
  396. { So this is useless... }
  397. { that's not true: -2^31 gives an overflow error if it is negated (FK) }
  398. cg.g_overflowcheck(exprasmlist,location,resulttype.def);
  399. end;
  400. {*****************************************************************************
  401. TPPCNOTNODE
  402. *****************************************************************************}
  403. procedure tppcnotnode.pass_2;
  404. var
  405. hl : tasmlabel;
  406. regl, regh: tregister;
  407. begin
  408. if is_boolean(resulttype.def) then
  409. begin
  410. { if the location is LOC_JUMP, we do the secondpass after the
  411. labels are allocated
  412. }
  413. if left.expectloc=LOC_JUMP then
  414. begin
  415. hl:=truelabel;
  416. truelabel:=falselabel;
  417. falselabel:=hl;
  418. secondpass(left);
  419. maketojumpbool(exprasmlist,left,lr_load_regvars);
  420. hl:=truelabel;
  421. truelabel:=falselabel;
  422. falselabel:=hl;
  423. location.loc:=LOC_JUMP;
  424. end
  425. else
  426. begin
  427. secondpass(left);
  428. case left.location.loc of
  429. LOC_FLAGS :
  430. begin
  431. location_copy(location,left.location);
  432. inverse_flags(location.resflags);
  433. end;
  434. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE :
  435. begin
  436. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  437. exprasmlist.concat(taicpu.op_reg_const(A_CMPWI,left.location.register,0));
  438. location_reset(location,LOC_FLAGS,OS_NO);
  439. location.resflags.cr:=RS_CR0;
  440. location.resflags.flag:=F_EQ;
  441. end;
  442. else
  443. internalerror(2003042401);
  444. end;
  445. end;
  446. end
  447. else if is_64bitint(left.resulttype.def) then
  448. begin
  449. secondpass(left);
  450. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  451. location_copy(location,left.location);
  452. { perform the NOT operation }
  453. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.register64.reghi,
  454. location.register64.reghi));
  455. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.register64.reglo,
  456. location.register64.reglo));
  457. end
  458. else
  459. begin
  460. secondpass(left);
  461. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  462. location_copy(location,left.location);
  463. location.loc := LOC_REGISTER;
  464. location.register := cg.getintregister(exprasmlist,OS_INT);
  465. { perform the NOT operation }
  466. cg.a_op_reg_reg(exprasmlist,OP_NOT,def_cgsize(resulttype.def),left.location.register,
  467. location.register);
  468. end;
  469. end;
  470. begin
  471. cmoddivnode:=tppcmoddivnode;
  472. cshlshrnode:=tppcshlshrnode;
  473. cunaryminusnode:=tppcunaryminusnode;
  474. cnotnode:=tppcnotnode;
  475. end.
  476. {
  477. $Log$
  478. Revision 1.42 2004-10-31 21:45:03 peter
  479. * generic tlocation
  480. * move tlocation to cgutils
  481. Revision 1.41 2004/10/25 15:36:47 peter
  482. * save standard registers moved to tcgobj
  483. Revision 1.40 2004/09/25 14:23:55 peter
  484. * ungetregister is now only used for cpuregisters, renamed to
  485. ungetcpuregister
  486. * renamed (get|unget)explicitregister(s) to ..cpuregister
  487. * removed location-release/reference_release
  488. Revision 1.39 2004/06/20 08:55:32 florian
  489. * logs truncated
  490. Revision 1.38 2004/01/01 17:58:16 jonas
  491. + integer division-by-zero detection support for ppc
  492. + compilerproc FPC_DIVBYZERO
  493. }