nppcmat.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 { signed overflow }
  58. divops: array[boolean, boolean] of tasmop =
  59. ((A_DIVDU, A_DIVDU_),(A_DIVD, A_DIVDO_));
  60. divcgops : array[boolean] of TOpCG = (OP_DIV, OP_IDIV);
  61. zerocond: tasmcond = (dirhint: DH_Plus; simple: true; cond:C_NE; cr: RS_CR7);
  62. tcgsize2native : array[OS_8..OS_S128] of tcgsize = (
  63. OS_64, OS_64, OS_64, OS_64, OS_NO,
  64. OS_S64, OS_S64, OS_S64, OS_S64, OS_NO
  65. );
  66. var
  67. power : longint;
  68. op : tasmop;
  69. numerator, divider,
  70. resultreg : tregister;
  71. size : TCgSize;
  72. hl : tasmlabel;
  73. done: boolean;
  74. procedure genOrdConstNodeMod;
  75. var
  76. modreg, maskreg, tempreg : tregister;
  77. isNegPower : boolean;
  78. begin
  79. if (tordconstnode(right).value = 0) then begin
  80. internalerror(2005061702);
  81. end else if (abs(tordconstnode(right).value) = 1) then begin
  82. { x mod +/-1 is always zero }
  83. cg.a_load_const_reg(exprasmlist, OS_INT, 0, resultreg);
  84. end else if (ispowerof2(tordconstnode(right).value, power)) then begin
  85. if (is_signed(right.resulttype.def)) then begin
  86. tempreg := cg.getintregister(exprasmlist, OS_INT);
  87. maskreg := cg.getintregister(exprasmlist, OS_INT);
  88. modreg := cg.getintregister(exprasmlist, OS_INT);
  89. cg.a_load_const_reg(exprasmlist, OS_INT, abs(tordconstnode(right).value)-1, modreg);
  90. cg.a_op_const_reg_reg(exprasmlist, OP_SAR, OS_INT, 63, numerator, maskreg);
  91. cg.a_op_reg_reg_reg(exprasmlist, OP_AND, OS_INT, numerator, modreg, tempreg);
  92. exprasmlist.concat(taicpu.op_reg_reg_reg(A_ANDC, maskreg, maskreg, modreg));
  93. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC, modreg, tempreg, 0));
  94. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUBFE, modreg, modreg, modreg));
  95. cg.a_op_reg_reg_reg(exprasmlist, OP_AND, OS_INT, modreg, maskreg, maskreg);
  96. cg.a_op_reg_reg_reg(exprasmlist, OP_OR, OS_INT, maskreg, tempreg, resultreg);
  97. end else begin
  98. cg.a_op_const_reg_reg(exprasmlist, OP_AND, OS_INT, tordconstnode(right).value-1, numerator,
  99. resultreg);
  100. end;
  101. end else begin
  102. cg.a_op_const_reg_reg(exprasmlist, divCgOps[is_signed(right.resulttype.def)], OS_INT,
  103. tordconstnode(right).value, numerator, resultreg);
  104. cg.a_op_const_reg_reg(exprasmlist, OP_MUL, OS_INT, tordconstnode(right).value, resultreg,
  105. resultreg);
  106. cg.a_op_reg_reg_reg(exprasmlist, OP_SUB, OS_INT, resultreg, numerator, resultreg);
  107. end;
  108. end;
  109. begin
  110. secondpass(left);
  111. secondpass(right);
  112. location_copy(location,left.location);
  113. { put numerator in register }
  114. size:=def_cgsize(left.resulttype.def);
  115. location_force_reg(exprasmlist,left.location,
  116. size,true);
  117. location_copy(location,left.location);
  118. numerator := location.register;
  119. resultreg := location.register;
  120. if (location.loc = LOC_CREGISTER) then begin
  121. location.loc := LOC_REGISTER;
  122. location.register := cg.getintregister(exprasmlist,size);
  123. resultreg := location.register;
  124. end else if (nodetype = modn) or (right.nodetype = ordconstn) then begin
  125. { for a modulus op, and for const nodes we need the result register
  126. to be an extra register }
  127. resultreg := cg.getintregister(exprasmlist,size);
  128. end;
  129. done := false;
  130. if (cs_slowoptimize in aktglobalswitches) and (right.nodetype = ordconstn) then begin
  131. if (nodetype = divn) then
  132. cg.a_op_const_reg_reg(exprasmlist, divCgOps[is_signed(right.resulttype.def)],
  133. size, tordconstnode(right).value, numerator, resultreg)
  134. else
  135. genOrdConstNodeMod;
  136. done := true;
  137. end;
  138. if (not done) then begin
  139. { load divider in a register if necessary }
  140. location_force_reg(exprasmlist,right.location,def_cgsize(right.resulttype.def),true);
  141. if (right.nodetype <> ordconstn) then
  142. exprasmlist.concat(taicpu.op_reg_reg_const(A_CMPDI, NR_CR7,
  143. right.location.register, 0))
  144. else begin
  145. if (tordconstnode(right).value = 0) then
  146. internalerror(2005100301);
  147. end;
  148. divider := right.location.register;
  149. { select the correct opcode according to the sign of the result, whether we need
  150. overflow checking }
  151. op := divops[is_signed(right.resulttype.def), cs_check_overflow in aktlocalswitches];
  152. exprasmlist.concat(taicpu.op_reg_reg_reg(op, resultreg, numerator,
  153. divider));
  154. if (nodetype = modn) then begin
  155. { multiply with the divisor again, taking care of the correct size }
  156. exprasmlist.concat(taicpu.op_reg_reg_reg(A_MULLD,resultreg,
  157. divider,resultreg));
  158. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB,location.register,
  159. numerator,resultreg));
  160. resultreg := location.register;
  161. end;
  162. end;
  163. { set result location }
  164. location.loc:=LOC_REGISTER;
  165. location.register:=resultreg;
  166. if right.nodetype <> ordconstn then begin
  167. objectlibrary.getjumplabel(hl);
  168. exprasmlist.concat(taicpu.op_cond_sym(A_BC,zerocond,hl));
  169. cg.a_call_name(exprasmlist,'FPC_DIVBYZERO');
  170. cg.a_label(exprasmlist,hl);
  171. end;
  172. { unsigned division/module can only overflow in case of division by zero
  173. (but checking this overflow flag is more convoluted than performing a
  174. simple comparison with 0) }
  175. if is_signed(right.resulttype.def) then
  176. cg.g_overflowcheck(exprasmlist,location,resulttype.def);
  177. end;
  178. {*****************************************************************************
  179. TPPCSHLRSHRNODE
  180. *****************************************************************************}
  181. procedure tppcshlshrnode.pass_2;
  182. var
  183. resultreg, hregister1, hregister2 : tregister;
  184. op: topcg;
  185. asmop1, asmop2: tasmop;
  186. shiftval: aint;
  187. begin
  188. secondpass(left);
  189. secondpass(right);
  190. { load left operators in a register }
  191. location_force_reg(exprasmlist, left.location,
  192. def_cgsize(left.resulttype.def), true);
  193. location_copy(location, left.location);
  194. resultreg := location.register;
  195. hregister1 := location.register;
  196. if (location.loc = LOC_CREGISTER) then begin
  197. location.loc := LOC_REGISTER;
  198. resultreg := cg.getintregister(exprasmlist, OS_INT);
  199. location.register := resultreg;
  200. end;
  201. { determine operator }
  202. if nodetype = shln then
  203. op := OP_SHL
  204. else
  205. op := OP_SHR;
  206. { shifting by a constant directly coded: }
  207. if (right.nodetype = ordconstn) then begin
  208. // result types with size < 32 bits have their shift values masked
  209. // differently... :/
  210. shiftval := tordconstnode(right).value and (tcgsize2size[def_cgsize(resulttype.def)] * 8 -1);
  211. cg.a_op_const_reg_reg(exprasmlist, op, def_cgsize(resulttype.def),
  212. shiftval, hregister1, resultreg)
  213. end else begin
  214. { load shift count in a register if necessary }
  215. location_force_reg(exprasmlist, right.location,
  216. def_cgsize(right.resulttype.def), true);
  217. hregister2 := right.location.register;
  218. cg.a_op_reg_reg_reg(exprasmlist, op, def_cgsize(resulttype.def), hregister2,
  219. hregister1, resultreg);
  220. end;
  221. end;
  222. {*****************************************************************************
  223. TPPCUNARYMINUSNODE
  224. *****************************************************************************}
  225. procedure tppcunaryminusnode.pass_2;
  226. var
  227. src1: tregister;
  228. op: tasmop;
  229. begin
  230. secondpass(left);
  231. begin
  232. location_copy(location, left.location);
  233. location.loc := LOC_REGISTER;
  234. case left.location.loc of
  235. LOC_FPUREGISTER, LOC_REGISTER:
  236. begin
  237. src1 := left.location.register;
  238. location.register := src1;
  239. end;
  240. LOC_CFPUREGISTER, LOC_CREGISTER:
  241. begin
  242. src1 := left.location.register;
  243. if left.location.loc = LOC_CREGISTER then
  244. location.register := cg.getintregister(exprasmlist, OS_INT)
  245. else
  246. location.register := cg.getfpuregister(exprasmlist, location.size);
  247. end;
  248. LOC_REFERENCE, LOC_CREFERENCE:
  249. begin
  250. if (left.resulttype.def.deftype = floatdef) then begin
  251. src1 := cg.getfpuregister(exprasmlist,
  252. def_cgsize(left.resulttype.def));
  253. location.register := src1;
  254. cg.a_loadfpu_ref_reg(exprasmlist,
  255. def_cgsize(left.resulttype.def),
  256. left.location.reference, src1);
  257. end else begin
  258. src1 := cg.getintregister(exprasmlist, OS_64);
  259. location.register := src1;
  260. cg.a_load_ref_reg(exprasmlist, OS_64, OS_64,
  261. left.location.reference, src1);
  262. end;
  263. end;
  264. end;
  265. { choose appropriate operand }
  266. if left.resulttype.def.deftype <> floatdef then begin
  267. if not (cs_check_overflow in aktlocalswitches) then
  268. op := A_NEG
  269. else
  270. op := A_NEGO_;
  271. location.loc := LOC_REGISTER;
  272. end else begin
  273. op := A_FNEG;
  274. location.loc := LOC_FPUREGISTER;
  275. end;
  276. { emit operation }
  277. exprasmlist.concat(taicpu.op_reg_reg(op, location.register, src1));
  278. end;
  279. cg.g_overflowcheck(exprasmlist, location, resulttype.def);
  280. end;
  281. {*****************************************************************************
  282. TPPCNOTNODE
  283. *****************************************************************************}
  284. procedure tppcnotnode.pass_2;
  285. var
  286. hl: tasmlabel;
  287. begin
  288. if is_boolean(resulttype.def) then
  289. begin
  290. { if the location is LOC_JUMP, we do the secondpass after the
  291. labels are allocated
  292. }
  293. if left.expectloc = LOC_JUMP then
  294. begin
  295. hl := truelabel;
  296. truelabel := falselabel;
  297. falselabel := hl;
  298. secondpass(left);
  299. maketojumpbool(exprasmlist, left, lr_load_regvars);
  300. hl := truelabel;
  301. truelabel := falselabel;
  302. falselabel := hl;
  303. location.loc := LOC_JUMP;
  304. end
  305. else
  306. begin
  307. secondpass(left);
  308. case left.location.loc of
  309. LOC_FLAGS:
  310. begin
  311. location_copy(location, left.location);
  312. inverse_flags(location.resflags);
  313. end;
  314. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE:
  315. begin
  316. location_force_reg(exprasmlist, left.location,
  317. def_cgsize(left.resulttype.def), true);
  318. exprasmlist.concat(taicpu.op_reg_const(A_CMPDI,
  319. left.location.register, 0));
  320. location_reset(location, LOC_FLAGS, OS_NO);
  321. location.resflags.cr := RS_CR0;
  322. location.resflags.flag := F_EQ;
  323. end;
  324. else
  325. internalerror(2003042401);
  326. end;
  327. end;
  328. end
  329. else
  330. begin
  331. secondpass(left);
  332. location_force_reg(exprasmlist, left.location,
  333. def_cgsize(left.resulttype.def), true);
  334. location_copy(location, left.location);
  335. location.loc := LOC_REGISTER;
  336. location.register := cg.getintregister(exprasmlist, OS_INT);
  337. { perform the NOT operation }
  338. cg.a_op_reg_reg(exprasmlist, OP_NOT, def_cgsize(resulttype.def),
  339. left.location.register,
  340. location.register);
  341. end;
  342. end;
  343. begin
  344. cmoddivnode := tppcmoddivnode;
  345. cshlshrnode := tppcshlshrnode;
  346. cunaryminusnode := tppcunaryminusnode;
  347. cnotnode := tppcnotnode;
  348. end.