nppcmat.pas 21 KB

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