nppcmat.pas 22 KB

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