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