sljitNativePPC_32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. /* ppc 32-bit arch dependent functions. */
  27. static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 reg, sljit_sw imm)
  28. {
  29. if (imm <= SIMM_MAX && imm >= SIMM_MIN)
  30. return push_inst(compiler, ADDI | D(reg) | A(0) | IMM(imm));
  31. if (!(imm & ~0xffff))
  32. return push_inst(compiler, ORI | S(TMP_ZERO) | A(reg) | IMM(imm));
  33. FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(imm >> 16)));
  34. return (imm & 0xffff) ? push_inst(compiler, ORI | S(reg) | A(reg) | IMM(imm)) : SLJIT_SUCCESS;
  35. }
  36. #define INS_CLEAR_LEFT(dst, src, from) \
  37. (RLWINM | S(src) | A(dst) | ((from) << 6) | (31 << 1))
  38. static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
  39. sljit_s32 dst, sljit_s32 src1, sljit_s32 src2)
  40. {
  41. switch (op) {
  42. case SLJIT_MOV:
  43. case SLJIT_MOV_U32:
  44. case SLJIT_MOV_S32:
  45. case SLJIT_MOV_P:
  46. SLJIT_ASSERT(src1 == TMP_REG1);
  47. if (dst != src2)
  48. return push_inst(compiler, OR | S(src2) | A(dst) | B(src2));
  49. return SLJIT_SUCCESS;
  50. case SLJIT_MOV_U8:
  51. case SLJIT_MOV_S8:
  52. SLJIT_ASSERT(src1 == TMP_REG1);
  53. if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
  54. if (op == SLJIT_MOV_S8)
  55. return push_inst(compiler, EXTSB | S(src2) | A(dst));
  56. return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 24));
  57. }
  58. else if ((flags & REG_DEST) && op == SLJIT_MOV_S8)
  59. return push_inst(compiler, EXTSB | S(src2) | A(dst));
  60. else {
  61. SLJIT_ASSERT(dst == src2);
  62. }
  63. return SLJIT_SUCCESS;
  64. case SLJIT_MOV_U16:
  65. case SLJIT_MOV_S16:
  66. SLJIT_ASSERT(src1 == TMP_REG1);
  67. if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
  68. if (op == SLJIT_MOV_S16)
  69. return push_inst(compiler, EXTSH | S(src2) | A(dst));
  70. return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 16));
  71. }
  72. else {
  73. SLJIT_ASSERT(dst == src2);
  74. }
  75. return SLJIT_SUCCESS;
  76. case SLJIT_NOT:
  77. SLJIT_ASSERT(src1 == TMP_REG1);
  78. return push_inst(compiler, NOR | RC(flags) | S(src2) | A(dst) | B(src2));
  79. case SLJIT_NEG:
  80. SLJIT_ASSERT(src1 == TMP_REG1);
  81. /* Setting XER SO is not enough, CR SO is also needed. */
  82. return push_inst(compiler, NEG | OE((flags & ALT_FORM1) ? ALT_SET_FLAGS : 0) | RC(flags) | D(dst) | A(src2));
  83. case SLJIT_CLZ:
  84. SLJIT_ASSERT(src1 == TMP_REG1);
  85. return push_inst(compiler, CNTLZW | S(src2) | A(dst));
  86. case SLJIT_ADD:
  87. if (flags & ALT_FORM1) {
  88. /* Setting XER SO is not enough, CR SO is also needed. */
  89. return push_inst(compiler, ADD | OE(ALT_SET_FLAGS) | RC(ALT_SET_FLAGS) | D(dst) | A(src1) | B(src2));
  90. }
  91. if (flags & ALT_FORM2) {
  92. /* Flags does not set: BIN_IMM_EXTS unnecessary. */
  93. SLJIT_ASSERT(src2 == TMP_REG2);
  94. if (flags & ALT_FORM3)
  95. return push_inst(compiler, ADDIS | D(dst) | A(src1) | compiler->imm);
  96. if (flags & ALT_FORM4) {
  97. FAIL_IF(push_inst(compiler, ADDIS | D(dst) | A(src1) | (((compiler->imm >> 16) & 0xffff) + ((compiler->imm >> 15) & 0x1))));
  98. src1 = dst;
  99. }
  100. return push_inst(compiler, ADDI | D(dst) | A(src1) | (compiler->imm & 0xffff));
  101. }
  102. if (flags & ALT_FORM3) {
  103. SLJIT_ASSERT(src2 == TMP_REG2);
  104. return push_inst(compiler, ADDIC | D(dst) | A(src1) | compiler->imm);
  105. }
  106. if (!(flags & ALT_SET_FLAGS))
  107. return push_inst(compiler, ADD | D(dst) | A(src1) | B(src2));
  108. if (flags & ALT_FORM4)
  109. return push_inst(compiler, ADDC | RC(ALT_SET_FLAGS) | D(dst) | A(src1) | B(src2));
  110. return push_inst(compiler, ADD | RC(flags) | D(dst) | A(src1) | B(src2));
  111. case SLJIT_ADDC:
  112. return push_inst(compiler, ADDE | D(dst) | A(src1) | B(src2));
  113. case SLJIT_SUB:
  114. if (flags & ALT_FORM1) {
  115. if (flags & ALT_FORM2) {
  116. FAIL_IF(push_inst(compiler, CMPLI | CRD(0) | A(src1) | compiler->imm));
  117. if (!(flags & ALT_FORM3))
  118. return SLJIT_SUCCESS;
  119. return push_inst(compiler, ADDI | D(dst) | A(src1) | (-compiler->imm & 0xffff));
  120. }
  121. FAIL_IF(push_inst(compiler, CMPL | CRD(0) | A(src1) | B(src2)));
  122. if (!(flags & ALT_FORM3))
  123. return SLJIT_SUCCESS;
  124. return push_inst(compiler, SUBF | D(dst) | A(src2) | B(src1));
  125. }
  126. if (flags & ALT_FORM2) {
  127. /* Setting XER SO is not enough, CR SO is also needed. */
  128. return push_inst(compiler, SUBF | OE(ALT_SET_FLAGS) | RC(ALT_SET_FLAGS) | D(dst) | A(src2) | B(src1));
  129. }
  130. if (flags & ALT_FORM3) {
  131. /* Flags does not set: BIN_IMM_EXTS unnecessary. */
  132. SLJIT_ASSERT(src2 == TMP_REG2);
  133. return push_inst(compiler, SUBFIC | D(dst) | A(src1) | compiler->imm);
  134. }
  135. if (flags & ALT_FORM4) {
  136. if (flags & ALT_FORM5) {
  137. SLJIT_ASSERT(src2 == TMP_REG2);
  138. return push_inst(compiler, CMPI | CRD(0) | A(src1) | compiler->imm);
  139. }
  140. return push_inst(compiler, CMP | CRD(0) | A(src1) | B(src2));
  141. }
  142. if (!(flags & ALT_SET_FLAGS))
  143. return push_inst(compiler, SUBF | D(dst) | A(src2) | B(src1));
  144. if (flags & ALT_FORM5)
  145. return push_inst(compiler, SUBFC | RC(ALT_SET_FLAGS) | D(dst) | A(src2) | B(src1));
  146. return push_inst(compiler, SUBF | RC(flags) | D(dst) | A(src2) | B(src1));
  147. case SLJIT_SUBC:
  148. return push_inst(compiler, SUBFE | D(dst) | A(src2) | B(src1));
  149. case SLJIT_MUL:
  150. if (flags & ALT_FORM1) {
  151. SLJIT_ASSERT(src2 == TMP_REG2);
  152. return push_inst(compiler, MULLI | D(dst) | A(src1) | compiler->imm);
  153. }
  154. return push_inst(compiler, MULLW | OE(flags) | RC(flags) | D(dst) | A(src2) | B(src1));
  155. case SLJIT_AND:
  156. if (flags & ALT_FORM1) {
  157. SLJIT_ASSERT(src2 == TMP_REG2);
  158. return push_inst(compiler, ANDI | S(src1) | A(dst) | compiler->imm);
  159. }
  160. if (flags & ALT_FORM2) {
  161. SLJIT_ASSERT(src2 == TMP_REG2);
  162. return push_inst(compiler, ANDIS | S(src1) | A(dst) | compiler->imm);
  163. }
  164. return push_inst(compiler, AND | RC(flags) | S(src1) | A(dst) | B(src2));
  165. case SLJIT_OR:
  166. if (flags & ALT_FORM1) {
  167. SLJIT_ASSERT(src2 == TMP_REG2);
  168. return push_inst(compiler, ORI | S(src1) | A(dst) | compiler->imm);
  169. }
  170. if (flags & ALT_FORM2) {
  171. SLJIT_ASSERT(src2 == TMP_REG2);
  172. return push_inst(compiler, ORIS | S(src1) | A(dst) | compiler->imm);
  173. }
  174. if (flags & ALT_FORM3) {
  175. SLJIT_ASSERT(src2 == TMP_REG2);
  176. FAIL_IF(push_inst(compiler, ORI | S(src1) | A(dst) | IMM(compiler->imm)));
  177. return push_inst(compiler, ORIS | S(dst) | A(dst) | IMM(compiler->imm >> 16));
  178. }
  179. return push_inst(compiler, OR | RC(flags) | S(src1) | A(dst) | B(src2));
  180. case SLJIT_XOR:
  181. if (flags & ALT_FORM1) {
  182. SLJIT_ASSERT(src2 == TMP_REG2);
  183. return push_inst(compiler, XORI | S(src1) | A(dst) | compiler->imm);
  184. }
  185. if (flags & ALT_FORM2) {
  186. SLJIT_ASSERT(src2 == TMP_REG2);
  187. return push_inst(compiler, XORIS | S(src1) | A(dst) | compiler->imm);
  188. }
  189. if (flags & ALT_FORM3) {
  190. SLJIT_ASSERT(src2 == TMP_REG2);
  191. FAIL_IF(push_inst(compiler, XORI | S(src1) | A(dst) | IMM(compiler->imm)));
  192. return push_inst(compiler, XORIS | S(dst) | A(dst) | IMM(compiler->imm >> 16));
  193. }
  194. return push_inst(compiler, XOR | RC(flags) | S(src1) | A(dst) | B(src2));
  195. case SLJIT_SHL:
  196. if (flags & ALT_FORM1) {
  197. SLJIT_ASSERT(src2 == TMP_REG2);
  198. compiler->imm &= 0x1f;
  199. return push_inst(compiler, RLWINM | RC(flags) | S(src1) | A(dst) | (compiler->imm << 11) | ((31 - compiler->imm) << 1));
  200. }
  201. return push_inst(compiler, SLW | RC(flags) | S(src1) | A(dst) | B(src2));
  202. case SLJIT_LSHR:
  203. if (flags & ALT_FORM1) {
  204. SLJIT_ASSERT(src2 == TMP_REG2);
  205. compiler->imm &= 0x1f;
  206. return push_inst(compiler, RLWINM | RC(flags) | S(src1) | A(dst) | (((32 - compiler->imm) & 0x1f) << 11) | (compiler->imm << 6) | (31 << 1));
  207. }
  208. return push_inst(compiler, SRW | RC(flags) | S(src1) | A(dst) | B(src2));
  209. case SLJIT_ASHR:
  210. if (flags & ALT_FORM1) {
  211. SLJIT_ASSERT(src2 == TMP_REG2);
  212. compiler->imm &= 0x1f;
  213. return push_inst(compiler, SRAWI | RC(flags) | S(src1) | A(dst) | (compiler->imm << 11));
  214. }
  215. return push_inst(compiler, SRAW | RC(flags) | S(src1) | A(dst) | B(src2));
  216. }
  217. SLJIT_UNREACHABLE();
  218. return SLJIT_SUCCESS;
  219. }
  220. static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 reg, sljit_sw init_value)
  221. {
  222. FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(init_value >> 16)));
  223. return push_inst(compiler, ORI | S(reg) | A(reg) | IMM(init_value));
  224. }
  225. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
  226. {
  227. sljit_ins *inst = (sljit_ins *)addr;
  228. SLJIT_ASSERT((inst[0] & 0xfc1f0000) == ADDIS && (inst[1] & 0xfc000000) == ORI);
  229. inst[0] = (inst[0] & 0xffff0000) | ((new_target >> 16) & 0xffff);
  230. inst[1] = (inst[1] & 0xffff0000) | (new_target & 0xffff);
  231. inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  232. SLJIT_CACHE_FLUSH(inst, inst + 2);
  233. }
  234. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
  235. {
  236. sljit_ins *inst = (sljit_ins *)addr;
  237. SLJIT_ASSERT((inst[0] & 0xfc1f0000) == ADDIS && (inst[1] & 0xfc000000) == ORI);
  238. inst[0] = (inst[0] & 0xffff0000) | ((new_constant >> 16) & 0xffff);
  239. inst[1] = (inst[1] & 0xffff0000) | (new_constant & 0xffff);
  240. inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  241. SLJIT_CACHE_FLUSH(inst, inst + 2);
  242. }