nppcmat.pas 21 KB

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