nppcmat.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. end;
  30. tppcunaryminusnode = class(tunaryminusnode)
  31. procedure pass_2; override;
  32. end;
  33. tppcnotnode = class(tnotnode)
  34. procedure pass_2; override;
  35. end;
  36. implementation
  37. uses
  38. globtype, systems,
  39. cutils, verbose, globals,
  40. symconst, symdef,
  41. aasmbase, aasmcpu, aasmtai,
  42. defutil,
  43. cgbase, cgutils, cgobj, pass_1, pass_2,
  44. ncon, procinfo,
  45. cpubase, cpuinfo,
  46. ncgutil, cgcpu, rgobj;
  47. {*****************************************************************************
  48. TPPCMODDIVNODE
  49. *****************************************************************************}
  50. function tppcmoddivnode.pass_1: tnode;
  51. begin
  52. result := inherited pass_1;
  53. if not assigned(result) then
  54. include(current_procinfo.flags, pi_do_call);
  55. end;
  56. procedure tppcmoddivnode.pass_2;
  57. const
  58. // ts: todo, use 32 bit operations if possible (much faster!)
  59. { signed overflow }
  60. divops: array[boolean, boolean] of tasmop =
  61. ((A_DIVDU, A_DIVDUO_), (A_DIVD, A_DIVDO_));
  62. zerocond: tasmcond = (dirhint: DH_Plus; simple: true; cond: C_NE; cr: RS_CR1);
  63. var
  64. power: longint;
  65. op: tasmop;
  66. numerator,
  67. divider,
  68. resultreg: tregister;
  69. size: Tcgsize;
  70. hl: tasmlabel;
  71. begin
  72. secondpass(left);
  73. secondpass(right);
  74. location_copy(location, left.location);
  75. { put numerator in register }
  76. size := def_cgsize(left.resulttype.def);
  77. location_force_reg(exprasmlist, left.location,
  78. size, true);
  79. location_copy(location, left.location);
  80. numerator := location.register;
  81. resultreg := location.register;
  82. if (location.loc = LOC_CREGISTER) then
  83. begin
  84. location.loc := LOC_REGISTER;
  85. location.register := cg.getintregister(exprasmlist, size);
  86. resultreg := location.register;
  87. end;
  88. if (nodetype = modn) then
  89. begin
  90. resultreg := cg.getintregister(exprasmlist, size);
  91. end;
  92. if (nodetype = divn) and
  93. (right.nodetype = ordconstn) and
  94. ispowerof2(tordconstnode(right).value, power) then
  95. begin
  96. if (is_signed(right.resulttype.def)) then 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_64, power,
  108. numerator, resultreg);
  109. exprasmlist.concat(taicpu.op_reg_reg(A_ADDZE, resultreg, resultreg));
  110. end else begin
  111. cg.a_op_const_reg_reg(exprasmlist, OP_SHR, OS_INT, power, numerator, resultreg);
  112. end;
  113. end
  114. else
  115. begin
  116. { load divider in a register if necessary }
  117. location_force_reg(exprasmlist, right.location,
  118. def_cgsize(right.resulttype.def), true);
  119. if (right.nodetype <> ordconstn) then
  120. {$NOTE ts: testme}
  121. exprasmlist.concat(taicpu.op_reg_reg_const(A_CMPDI, NR_CR1,
  122. right.location.register, 0));
  123. divider := right.location.register;
  124. { needs overflow checking, (-maxlongint-1) div (-1) overflows! }
  125. { And on PPC, the only way to catch a div-by-0 is by checking }
  126. { the overflow flag (JM) }
  127. op := divops[is_signed(right.resulttype.def),
  128. cs_check_overflow in aktlocalswitches];
  129. exprasmlist.concat(taicpu.op_reg_reg_reg(op, resultreg, numerator,
  130. divider));
  131. if (nodetype = modn) then
  132. begin
  133. {$NOTE ts:testme}
  134. exprasmlist.concat(taicpu.op_reg_reg_reg(A_MULLD, resultreg,
  135. divider, resultreg));
  136. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB, location.register,
  137. numerator, resultreg));
  138. resultreg := location.register;
  139. end;
  140. end;
  141. { set result location }
  142. location.loc := LOC_REGISTER;
  143. location.register := resultreg;
  144. if right.nodetype <> ordconstn then
  145. begin
  146. objectlibrary.getjumplabel(hl);
  147. exprasmlist.concat(taicpu.op_cond_sym(A_BC, zerocond, hl));
  148. cg.a_call_name(exprasmlist, 'FPC_DIVBYZERO');
  149. cg.a_label(exprasmlist, hl);
  150. end;
  151. cg.g_overflowcheck(exprasmlist, location, resulttype.def);
  152. end;
  153. {*****************************************************************************
  154. TPPCSHLRSHRNODE
  155. *****************************************************************************}
  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. { load left operators in a register }
  167. location_force_reg(exprasmlist, left.location,
  168. def_cgsize(left.resulttype.def), true);
  169. location_copy(location, left.location);
  170. resultreg := location.register;
  171. hregister1 := location.register;
  172. if (location.loc = LOC_CREGISTER) then begin
  173. location.loc := LOC_REGISTER;
  174. resultreg := cg.getintregister(exprasmlist, OS_64);
  175. location.register := resultreg;
  176. end;
  177. { determine operator }
  178. if nodetype = shln then
  179. op := OP_SHL
  180. else
  181. op := OP_SHR;
  182. { shifting by a constant directly coded: }
  183. if (right.nodetype = ordconstn) then begin
  184. // result types with size < 32 bits have their shift values masked
  185. // differently... :/
  186. shiftval := tordconstnode(right).value and (tcgsize2size[def_cgsize(resulttype.def)] * 8 -1);
  187. cg.a_op_const_reg_reg(exprasmlist, op, def_cgsize(resulttype.def),
  188. shiftval, hregister1, resultreg)
  189. end else begin
  190. { load shift count in a register if necessary }
  191. location_force_reg(exprasmlist, right.location,
  192. def_cgsize(right.resulttype.def), true);
  193. hregister2 := right.location.register;
  194. cg.a_op_reg_reg_reg(exprasmlist, op, def_cgsize(resulttype.def), hregister2,
  195. hregister1, resultreg);
  196. end;
  197. end;
  198. {*****************************************************************************
  199. TPPCUNARYMINUSNODE
  200. *****************************************************************************}
  201. procedure tppcunaryminusnode.pass_2;
  202. var
  203. src1: tregister;
  204. op: tasmop;
  205. begin
  206. secondpass(left);
  207. begin
  208. location_copy(location, left.location);
  209. location.loc := LOC_REGISTER;
  210. case left.location.loc of
  211. LOC_FPUREGISTER, LOC_REGISTER:
  212. begin
  213. src1 := left.location.register;
  214. location.register := src1;
  215. end;
  216. LOC_CFPUREGISTER, LOC_CREGISTER:
  217. begin
  218. src1 := left.location.register;
  219. if left.location.loc = LOC_CREGISTER then
  220. location.register := cg.getintregister(exprasmlist, OS_INT)
  221. else
  222. location.register := cg.getfpuregister(exprasmlist, location.size);
  223. end;
  224. LOC_REFERENCE, LOC_CREFERENCE:
  225. begin
  226. if (left.resulttype.def.deftype = floatdef) then
  227. begin
  228. src1 := cg.getfpuregister(exprasmlist,
  229. def_cgsize(left.resulttype.def));
  230. location.register := src1;
  231. cg.a_loadfpu_ref_reg(exprasmlist,
  232. def_cgsize(left.resulttype.def),
  233. left.location.reference, src1);
  234. end
  235. else
  236. begin
  237. src1 := cg.getintregister(exprasmlist, OS_64);
  238. location.register := src1;
  239. cg.a_load_ref_reg(exprasmlist, OS_64, OS_64,
  240. left.location.reference, src1);
  241. end;
  242. end;
  243. end;
  244. { choose appropriate operand }
  245. if left.resulttype.def.deftype <> floatdef then
  246. begin
  247. if not (cs_check_overflow in aktlocalswitches) then
  248. op := A_NEG
  249. else
  250. op := A_NEGO_;
  251. location.loc := LOC_REGISTER;
  252. end
  253. else
  254. begin
  255. op := A_FNEG;
  256. location.loc := LOC_FPUREGISTER;
  257. end;
  258. { emit operation }
  259. exprasmlist.concat(taicpu.op_reg_reg(op, location.register, src1));
  260. end;
  261. { Here was a problem... }
  262. { Operand to be negated always }
  263. { seems to be converted to signed }
  264. { 32-bit before doing neg!! }
  265. { So this is useless... }
  266. { that's not true: -2^31 gives an overflow error if it is negated (FK) }
  267. cg.g_overflowcheck(exprasmlist, location, resulttype.def);
  268. end;
  269. {*****************************************************************************
  270. TPPCNOTNODE
  271. *****************************************************************************}
  272. procedure tppcnotnode.pass_2;
  273. var
  274. hl: tasmlabel;
  275. begin
  276. if is_boolean(resulttype.def) then
  277. begin
  278. { if the location is LOC_JUMP, we do the secondpass after the
  279. labels are allocated
  280. }
  281. if left.expectloc = LOC_JUMP then
  282. begin
  283. hl := truelabel;
  284. truelabel := falselabel;
  285. falselabel := hl;
  286. secondpass(left);
  287. maketojumpbool(exprasmlist, left, lr_load_regvars);
  288. hl := truelabel;
  289. truelabel := falselabel;
  290. falselabel := hl;
  291. location.loc := LOC_JUMP;
  292. end
  293. else
  294. begin
  295. secondpass(left);
  296. case left.location.loc of
  297. LOC_FLAGS:
  298. begin
  299. location_copy(location, left.location);
  300. inverse_flags(location.resflags);
  301. end;
  302. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE:
  303. begin
  304. location_force_reg(exprasmlist, left.location,
  305. def_cgsize(left.resulttype.def), true);
  306. exprasmlist.concat(taicpu.op_reg_const(A_CMPDI,
  307. left.location.register, 0));
  308. location_reset(location, LOC_FLAGS, OS_NO);
  309. location.resflags.cr := RS_CR0;
  310. location.resflags.flag := F_EQ;
  311. end;
  312. else
  313. internalerror(2003042401);
  314. end;
  315. end;
  316. end
  317. else
  318. begin
  319. secondpass(left);
  320. location_force_reg(exprasmlist, left.location,
  321. def_cgsize(left.resulttype.def), true);
  322. location_copy(location, left.location);
  323. location.loc := LOC_REGISTER;
  324. location.register := cg.getintregister(exprasmlist, OS_INT);
  325. { perform the NOT operation }
  326. cg.a_op_reg_reg(exprasmlist, OP_NOT, def_cgsize(resulttype.def),
  327. left.location.register,
  328. location.register);
  329. end;
  330. end;
  331. begin
  332. cmoddivnode := tppcmoddivnode;
  333. cshlshrnode := tppcshlshrnode;
  334. cunaryminusnode := tppcunaryminusnode;
  335. cnotnode := tppcnotnode;
  336. end.