ncpumat.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. {
  2. David Zhang 2007/01/15
  3. $Id: ncpumat.pas,v 1.23 2005/02/14 17:13:10 peter Exp $
  4. Copyright (c) 1998-2002 by Florian Klaempfl
  5. Generate MIPSel assembler for math nodes
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit ncpumat;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. symtype,
  24. node, nmat, ncgmat, cgbase;
  25. type
  26. tMIPSELmoddivnode = class(tmoddivnode)
  27. procedure pass_generate_code;override;
  28. end;
  29. tMIPSELshlshrnode = class(tcgshlshrnode)
  30. procedure second_64bit;override;
  31. { everything will be handled in pass_2 }
  32. function first_shlshr64bitint: tnode; override;
  33. end;
  34. tMIPSELnotnode = class(tcgnotnode)
  35. procedure second_boolean; override;
  36. end;
  37. TMIPSunaryminusnode = class(tcgunaryminusnode)
  38. procedure second_float; override;
  39. end;
  40. implementation
  41. uses
  42. globtype, systems,
  43. cutils, verbose, globals,
  44. symconst, symdef,
  45. aasmbase, aasmcpu, aasmtai, aasmdata,
  46. defutil,
  47. procinfo,
  48. cgobj, hlcgobj, pass_2,
  49. ncon,
  50. cpubase,
  51. ncgutil, cgcpu, cgutils;
  52. {*****************************************************************************
  53. TMipselMODDIVNODE
  54. *****************************************************************************}
  55. const
  56. ops_div: array[boolean] of tasmop = (A_DIVU, A_DIV);
  57. procedure tMIPSELmoddivnode.pass_generate_code;
  58. var
  59. power: longint;
  60. tmpreg, numerator, divider: tregister;
  61. hl,hl2: tasmlabel;
  62. begin
  63. secondpass(left);
  64. secondpass(right);
  65. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  66. location.register:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  67. { put numerator in register }
  68. hlcg.location_force_reg(current_asmdata.CurrAsmList, left.location, left.resultdef, left.resultdef, True);
  69. numerator := left.location.Register;
  70. if (nodetype = divn) and
  71. (right.nodetype = ordconstn) and
  72. ispowerof2(tordconstnode(right).Value.svalue, power) then
  73. begin
  74. tmpreg := cg.GetIntRegister(current_asmdata.CurrAsmList, OS_INT);
  75. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, 31, numerator, tmpreg);
  76. { if signed, tmpreg=right value-1, otherwise 0 }
  77. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_AND, OS_INT, tordconstnode(right).Value.svalue - 1, tmpreg);
  78. { add left value }
  79. cg.a_op_reg_reg(current_asmdata.CurrAsmList, OP_ADD, OS_INT, numerator, tmpreg);
  80. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, aword(power), tmpreg, location.register);
  81. end
  82. else
  83. begin
  84. { load divider in a register if necessary }
  85. hlcg.location_force_reg(current_asmdata.CurrAsmList, right.location,
  86. right.resultdef, right.resultdef, True);
  87. divider := right.location.Register;
  88. { GAS performs division in delay slot:
  89. bne denom,$zero,.L1
  90. div $zero,numerator,denom
  91. break 7
  92. .L1:
  93. mflo result
  94. We can't yet do the same without prior fixing the spilling code:
  95. if registers require spilling, loads can be inserted before 'div',
  96. resulting in invalid code.
  97. }
  98. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(ops_div[is_signed(resultdef)],NR_R0,numerator,divider));
  99. { Check for zero denominator, omit if dividing by constant (constants are checked earlier) }
  100. if (right.nodetype<>ordconstn) then
  101. begin
  102. current_asmdata.getjumplabel(hl);
  103. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,divider,NR_R0,hl);
  104. current_asmdata.CurrAsmList.Concat(taicpu.op_const(A_BREAK,7));
  105. cg.a_label(current_asmdata.CurrAsmList,hl);
  106. end;
  107. { Dividing low(longint) by -1 will overflow }
  108. if is_signed(right.resultdef) and (cs_check_overflow in current_settings.localswitches) then
  109. begin
  110. current_asmdata.getjumplabel(hl2);
  111. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_ADDIU,NR_R1,NR_R0,-1));
  112. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,divider,NR_R1,hl2);
  113. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_LUI,NR_R1,$8000));
  114. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,numerator,NR_R1,hl2);
  115. current_asmdata.CurrAsmList.concat(taicpu.op_const(A_BREAK,6));
  116. cg.a_label(current_asmdata.CurrAsmList,hl2);
  117. end;
  118. if (nodetype=modn) then
  119. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_MFHI,location.register))
  120. else
  121. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_MFLO,location.register));
  122. end;
  123. end;
  124. {*****************************************************************************
  125. TMIPSelSHLRSHRNODE
  126. *****************************************************************************}
  127. function TMIPSELShlShrNode.first_shlshr64bitint: TNode;
  128. begin
  129. { 64bit without constants need a helper }
  130. if is_64bit(left.resultdef) and
  131. (right.nodetype <> ordconstn) then
  132. begin
  133. Result := inherited first_shlshr64bitint;
  134. exit;
  135. end;
  136. Result := nil;
  137. end;
  138. procedure tMIPSELshlshrnode.second_64bit;
  139. var
  140. hregister, hreg64hi, hreg64lo: tregister;
  141. op: topcg;
  142. shiftval: aword;
  143. const
  144. ops: array [boolean] of topcg = (OP_SHR,OP_SHL);
  145. begin
  146. { 64bit without constants need a helper, and is
  147. already replaced in pass1 }
  148. if (right.nodetype <> ordconstn) then
  149. internalerror(200405301);
  150. location_reset(location, LOC_REGISTER, def_cgsize(resultdef));
  151. { load left operator in a register }
  152. hlcg.location_force_reg(current_asmdata.CurrAsmList, left.location, left.resultdef, resultdef, true);
  153. hreg64hi := left.location.register64.reghi;
  154. hreg64lo := left.location.register64.reglo;
  155. shiftval := tordconstnode(right).Value.svalue and 63;
  156. op := ops[nodetype=shln];
  157. if shiftval > 31 then
  158. begin
  159. if nodetype = shln then
  160. begin
  161. location.register64.reglo:=NR_R0;
  162. location.register64.reghi:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  163. { if shiftval and 31 = 0, it will optimize to MOVE }
  164. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, shiftval and 31, hreg64lo, location.register64.reghi);
  165. end
  166. else
  167. begin
  168. location.register64.reghi:=NR_R0;
  169. location.register64.reglo:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  170. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, shiftval and 31, hreg64hi, location.register64.reglo);
  171. end;
  172. end
  173. else
  174. begin
  175. location.register64.reglo:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  176. location.register64.reghi:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  177. hregister := cg.getintregister(current_asmdata.CurrAsmList, OS_32);
  178. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, op, OS_32, shiftval, hreg64hi, location.register64.reghi);
  179. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, op, OS_32, shiftval, hreg64lo, location.register64.reglo);
  180. if shiftval <> 0 then
  181. begin
  182. if nodetype = shln then
  183. begin
  184. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, 32-shiftval, hreg64lo, hregister);
  185. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_32, hregister, location.register64.reghi, location.register64.reghi);
  186. end
  187. else
  188. begin
  189. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, 32-shiftval, hreg64hi, hregister);
  190. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_32, hregister, location.register64.reglo, location.register64.reglo);
  191. end;
  192. end;
  193. end;
  194. end;
  195. {*****************************************************************************
  196. TMIPSelNOTNODE
  197. *****************************************************************************}
  198. procedure tMIPSELnotnode.second_boolean;
  199. var
  200. tmpreg : TRegister;
  201. begin
  202. if not handle_locjump then
  203. begin
  204. secondpass(left);
  205. case left.location.loc of
  206. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE,
  207. LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF:
  208. begin
  209. hlcg.location_force_reg(current_asmdata.CurrAsmList, left.location, left.resultdef, left.resultdef, True);
  210. location_reset(location,LOC_FLAGS,OS_NO);
  211. location.resflags.reg2:=NR_R0;
  212. location.resflags.cond:=OC_EQ;
  213. if is_64bit(resultdef) then
  214. begin
  215. tmpreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  216. { OR low and high parts together }
  217. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_OR,tmpreg,left.location.register64.reglo,left.location.register64.reghi));
  218. location.resflags.reg1:=tmpreg;
  219. end
  220. else
  221. location.resflags.reg1:=left.location.register;
  222. end;
  223. else
  224. internalerror(2003042401);
  225. end;
  226. end;
  227. end;
  228. {*****************************************************************************
  229. TMIPSunaryminusnode
  230. *****************************************************************************}
  231. procedure TMIPSunaryminusnode.second_float;
  232. begin
  233. secondpass(left);
  234. location_force_fpureg(current_asmdata.CurrAsmList,left.location,true);
  235. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  236. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  237. case location.size of
  238. OS_F32:
  239. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_NEG_s,location.register,left.location.register));
  240. OS_F64:
  241. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_NEG_d,location.register,left.location.register));
  242. else
  243. internalerror(2013030501);
  244. end;
  245. end;
  246. begin
  247. cmoddivnode := tMIPSELmoddivnode;
  248. cshlshrnode := tMIPSELshlshrnode;
  249. cnotnode := tMIPSELnotnode;
  250. cunaryminusnode := TMIPSunaryminusnode;
  251. end.