sljitNativeSPARC_32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Stack-less Just-In-Time compiler
  3. *
  4. * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification, are
  7. * permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this list of
  10. * conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  13. * of conditions and the following disclaimer in the documentation and/or other materials
  14. * provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  19. * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  21. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  22. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  24. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw imm)
  27. {
  28. if (imm <= SIMM_MAX && imm >= SIMM_MIN)
  29. return push_inst(compiler, OR | D(dst) | S1(0) | IMM(imm), DR(dst));
  30. FAIL_IF(push_inst(compiler, SETHI | D(dst) | ((imm >> 10) & 0x3fffff), DR(dst)));
  31. return (imm & 0x3ff) ? push_inst(compiler, OR | D(dst) | S1(dst) | IMM_ARG | (imm & 0x3ff), DR(dst)) : SLJIT_SUCCESS;
  32. }
  33. #define ARG2(flags, src2) ((flags & SRC2_IMM) ? IMM(src2) : S2(src2))
  34. static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
  35. sljit_s32 dst, sljit_s32 src1, sljit_sw src2)
  36. {
  37. SLJIT_COMPILE_ASSERT(ICC_IS_SET == SET_FLAGS, icc_is_set_and_set_flags_must_be_the_same);
  38. switch (op) {
  39. case SLJIT_MOV:
  40. case SLJIT_MOV_U32:
  41. case SLJIT_MOV_S32:
  42. case SLJIT_MOV_P:
  43. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
  44. if (dst != src2)
  45. return push_inst(compiler, OR | D(dst) | S1(0) | S2(src2), DR(dst));
  46. return SLJIT_SUCCESS;
  47. case SLJIT_MOV_U8:
  48. case SLJIT_MOV_S8:
  49. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
  50. if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
  51. if (op == SLJIT_MOV_U8)
  52. return push_inst(compiler, AND | D(dst) | S1(src2) | IMM(0xff), DR(dst));
  53. FAIL_IF(push_inst(compiler, SLL | D(dst) | S1(src2) | IMM(24), DR(dst)));
  54. return push_inst(compiler, SRA | D(dst) | S1(dst) | IMM(24), DR(dst));
  55. }
  56. else if (dst != src2)
  57. SLJIT_UNREACHABLE();
  58. return SLJIT_SUCCESS;
  59. case SLJIT_MOV_U16:
  60. case SLJIT_MOV_S16:
  61. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
  62. if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
  63. FAIL_IF(push_inst(compiler, SLL | D(dst) | S1(src2) | IMM(16), DR(dst)));
  64. return push_inst(compiler, (op == SLJIT_MOV_S16 ? SRA : SRL) | D(dst) | S1(dst) | IMM(16), DR(dst));
  65. }
  66. else if (dst != src2)
  67. SLJIT_UNREACHABLE();
  68. return SLJIT_SUCCESS;
  69. case SLJIT_NOT:
  70. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
  71. return push_inst(compiler, XNOR | (flags & SET_FLAGS) | D(dst) | S1(0) | S2(src2), DR(dst) | (flags & SET_FLAGS));
  72. case SLJIT_CLZ:
  73. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
  74. FAIL_IF(push_inst(compiler, SUB | SET_FLAGS | D(0) | S1(src2) | S2(0), SET_FLAGS));
  75. FAIL_IF(push_inst(compiler, OR | D(TMP_REG1) | S1(0) | S2(src2), DR(TMP_REG1)));
  76. FAIL_IF(push_inst(compiler, BICC | DA(0x1) | (7 & DISP_MASK), UNMOVABLE_INS));
  77. FAIL_IF(push_inst(compiler, OR | D(dst) | S1(0) | IMM(32), UNMOVABLE_INS));
  78. FAIL_IF(push_inst(compiler, OR | D(dst) | S1(0) | IMM(-1), DR(dst)));
  79. /* Loop. */
  80. FAIL_IF(push_inst(compiler, SUB | SET_FLAGS | D(0) | S1(TMP_REG1) | S2(0), SET_FLAGS));
  81. FAIL_IF(push_inst(compiler, SLL | D(TMP_REG1) | S1(TMP_REG1) | IMM(1), DR(TMP_REG1)));
  82. FAIL_IF(push_inst(compiler, BICC | DA(0xe) | (-2 & DISP_MASK), UNMOVABLE_INS));
  83. return push_inst(compiler, ADD | D(dst) | S1(dst) | IMM(1), UNMOVABLE_INS);
  84. case SLJIT_ADD:
  85. return push_inst(compiler, ADD | (flags & SET_FLAGS) | D(dst) | S1(src1) | ARG2(flags, src2), DR(dst) | (flags & SET_FLAGS));
  86. case SLJIT_ADDC:
  87. return push_inst(compiler, ADDC | (flags & SET_FLAGS) | D(dst) | S1(src1) | ARG2(flags, src2), DR(dst) | (flags & SET_FLAGS));
  88. case SLJIT_SUB:
  89. return push_inst(compiler, SUB | (flags & SET_FLAGS) | D(dst) | S1(src1) | ARG2(flags, src2), DR(dst) | (flags & SET_FLAGS));
  90. case SLJIT_SUBC:
  91. return push_inst(compiler, SUBC | (flags & SET_FLAGS) | D(dst) | S1(src1) | ARG2(flags, src2), DR(dst) | (flags & SET_FLAGS));
  92. case SLJIT_MUL:
  93. FAIL_IF(push_inst(compiler, SMUL | D(dst) | S1(src1) | ARG2(flags, src2), DR(dst)));
  94. if (!(flags & SET_FLAGS))
  95. return SLJIT_SUCCESS;
  96. FAIL_IF(push_inst(compiler, SRA | D(TMP_REG1) | S1(dst) | IMM(31), DR(TMP_REG1)));
  97. FAIL_IF(push_inst(compiler, RDY | D(TMP_LINK), DR(TMP_LINK)));
  98. return push_inst(compiler, SUB | SET_FLAGS | D(0) | S1(TMP_REG1) | S2(TMP_LINK), MOVABLE_INS | SET_FLAGS);
  99. case SLJIT_AND:
  100. return push_inst(compiler, AND | (flags & SET_FLAGS) | D(dst) | S1(src1) | ARG2(flags, src2), DR(dst) | (flags & SET_FLAGS));
  101. case SLJIT_OR:
  102. return push_inst(compiler, OR | (flags & SET_FLAGS) | D(dst) | S1(src1) | ARG2(flags, src2), DR(dst) | (flags & SET_FLAGS));
  103. case SLJIT_XOR:
  104. return push_inst(compiler, XOR | (flags & SET_FLAGS) | D(dst) | S1(src1) | ARG2(flags, src2), DR(dst) | (flags & SET_FLAGS));
  105. case SLJIT_SHL:
  106. FAIL_IF(push_inst(compiler, SLL | D(dst) | S1(src1) | ARG2(flags, src2), DR(dst)));
  107. return !(flags & SET_FLAGS) ? SLJIT_SUCCESS : push_inst(compiler, SUB | SET_FLAGS | D(0) | S1(dst) | S2(0), SET_FLAGS);
  108. case SLJIT_LSHR:
  109. FAIL_IF(push_inst(compiler, SRL | D(dst) | S1(src1) | ARG2(flags, src2), DR(dst)));
  110. return !(flags & SET_FLAGS) ? SLJIT_SUCCESS : push_inst(compiler, SUB | SET_FLAGS | D(0) | S1(dst) | S2(0), SET_FLAGS);
  111. case SLJIT_ASHR:
  112. FAIL_IF(push_inst(compiler, SRA | D(dst) | S1(src1) | ARG2(flags, src2), DR(dst)));
  113. return !(flags & SET_FLAGS) ? SLJIT_SUCCESS : push_inst(compiler, SUB | SET_FLAGS | D(0) | S1(dst) | S2(0), SET_FLAGS);
  114. }
  115. SLJIT_UNREACHABLE();
  116. return SLJIT_SUCCESS;
  117. }
  118. static sljit_s32 call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src)
  119. {
  120. sljit_s32 reg_index = 8;
  121. sljit_s32 word_reg_index = 8;
  122. sljit_s32 float_arg_index = 1;
  123. sljit_s32 double_arg_count = 0;
  124. sljit_s32 float_offset = (16 + 6) * sizeof(sljit_sw);
  125. sljit_s32 types = 0;
  126. sljit_s32 reg = 0;
  127. sljit_s32 move_to_tmp2 = 0;
  128. if (src)
  129. reg = reg_map[*src & REG_MASK];
  130. arg_types >>= SLJIT_DEF_SHIFT;
  131. while (arg_types) {
  132. types = (types << SLJIT_DEF_SHIFT) | (arg_types & SLJIT_DEF_MASK);
  133. switch (arg_types & SLJIT_DEF_MASK) {
  134. case SLJIT_ARG_TYPE_F32:
  135. float_arg_index++;
  136. if (reg_index == reg)
  137. move_to_tmp2 = 1;
  138. reg_index++;
  139. break;
  140. case SLJIT_ARG_TYPE_F64:
  141. float_arg_index++;
  142. double_arg_count++;
  143. if (reg_index == reg || reg_index + 1 == reg)
  144. move_to_tmp2 = 1;
  145. reg_index += 2;
  146. break;
  147. default:
  148. if (reg_index != word_reg_index && reg_index < 14 && reg_index == reg)
  149. move_to_tmp2 = 1;
  150. reg_index++;
  151. word_reg_index++;
  152. break;
  153. }
  154. if (move_to_tmp2) {
  155. move_to_tmp2 = 0;
  156. if (reg < 14)
  157. FAIL_IF(push_inst(compiler, OR | D(TMP_REG1) | S1(0) | S2A(reg), DR(TMP_REG1)));
  158. *src = TMP_REG1;
  159. }
  160. arg_types >>= SLJIT_DEF_SHIFT;
  161. }
  162. arg_types = types;
  163. while (arg_types) {
  164. switch (arg_types & SLJIT_DEF_MASK) {
  165. case SLJIT_ARG_TYPE_F32:
  166. float_arg_index--;
  167. FAIL_IF(push_inst(compiler, STF | FD(float_arg_index) | S1(SLJIT_SP) | IMM(float_offset), MOVABLE_INS));
  168. float_offset -= sizeof(sljit_f64);
  169. break;
  170. case SLJIT_ARG_TYPE_F64:
  171. float_arg_index--;
  172. if (float_arg_index == 4 && double_arg_count == 4) {
  173. FAIL_IF(push_inst(compiler, STF | FD(float_arg_index) | S1(SLJIT_SP) | IMM((16 + 7) * sizeof(sljit_sw)), MOVABLE_INS));
  174. FAIL_IF(push_inst(compiler, STF | FD(float_arg_index) | (1 << 25) | S1(SLJIT_SP) | IMM((16 + 8) * sizeof(sljit_sw)), MOVABLE_INS));
  175. }
  176. else
  177. FAIL_IF(push_inst(compiler, STDF | FD(float_arg_index) | S1(SLJIT_SP) | IMM(float_offset), MOVABLE_INS));
  178. float_offset -= sizeof(sljit_f64);
  179. break;
  180. default:
  181. break;
  182. }
  183. arg_types >>= SLJIT_DEF_SHIFT;
  184. }
  185. float_offset = (16 + 6) * sizeof(sljit_sw);
  186. while (types) {
  187. switch (types & SLJIT_DEF_MASK) {
  188. case SLJIT_ARG_TYPE_F32:
  189. reg_index--;
  190. if (reg_index < 14)
  191. FAIL_IF(push_inst(compiler, LDUW | DA(reg_index) | S1(SLJIT_SP) | IMM(float_offset), reg_index));
  192. float_offset -= sizeof(sljit_f64);
  193. break;
  194. case SLJIT_ARG_TYPE_F64:
  195. reg_index -= 2;
  196. if (reg_index < 14) {
  197. if ((reg_index & 0x1) != 0) {
  198. FAIL_IF(push_inst(compiler, LDUW | DA(reg_index) | S1(SLJIT_SP) | IMM(float_offset), reg_index));
  199. if (reg_index < 13)
  200. FAIL_IF(push_inst(compiler, LDUW | DA(reg_index + 1) | S1(SLJIT_SP) | IMM(float_offset + sizeof(sljit_sw)), reg_index + 1));
  201. }
  202. else
  203. FAIL_IF(push_inst(compiler, LDD | DA(reg_index) | S1(SLJIT_SP) | IMM(float_offset), reg_index));
  204. }
  205. float_offset -= sizeof(sljit_f64);
  206. break;
  207. default:
  208. reg_index--;
  209. word_reg_index--;
  210. if (reg_index != word_reg_index) {
  211. if (reg_index < 14)
  212. FAIL_IF(push_inst(compiler, OR | DA(reg_index) | S1(0) | S2A(word_reg_index), reg_index));
  213. else
  214. FAIL_IF(push_inst(compiler, STW | DA(word_reg_index) | S1(SLJIT_SP) | IMM(92), word_reg_index));
  215. }
  216. break;
  217. }
  218. types >>= SLJIT_DEF_SHIFT;
  219. }
  220. return SLJIT_SUCCESS;
  221. }
  222. static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw init_value)
  223. {
  224. FAIL_IF(push_inst(compiler, SETHI | D(dst) | ((init_value >> 10) & 0x3fffff), DR(dst)));
  225. return push_inst(compiler, OR | D(dst) | S1(dst) | IMM_ARG | (init_value & 0x3ff), DR(dst));
  226. }
  227. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
  228. {
  229. sljit_ins *inst = (sljit_ins *)addr;
  230. SLJIT_ASSERT(((inst[0] & 0xc1c00000) == 0x01000000) && ((inst[1] & 0xc1f82000) == 0x80102000));
  231. inst[0] = (inst[0] & 0xffc00000) | ((new_target >> 10) & 0x3fffff);
  232. inst[1] = (inst[1] & 0xfffffc00) | (new_target & 0x3ff);
  233. inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  234. SLJIT_CACHE_FLUSH(inst, inst + 2);
  235. }
  236. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
  237. {
  238. sljit_ins *inst = (sljit_ins *)addr;
  239. SLJIT_ASSERT(((inst[0] & 0xc1c00000) == 0x01000000) && ((inst[1] & 0xc1f82000) == 0x80102000));
  240. inst[0] = (inst[0] & 0xffc00000) | ((new_constant >> 10) & 0x3fffff);
  241. inst[1] = (inst[1] & 0xfffffc00) | (new_constant & 0x3ff);
  242. inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  243. SLJIT_CACHE_FLUSH(inst, inst + 2);
  244. }