sljitNativeMIPS_32.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. /* mips 32-bit arch dependent functions. */
  27. static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst_ar, sljit_sw imm)
  28. {
  29. if (!(imm & ~0xffff))
  30. return push_inst(compiler, ORI | SA(0) | TA(dst_ar) | IMM(imm), dst_ar);
  31. if (imm < 0 && imm >= SIMM_MIN)
  32. return push_inst(compiler, ADDIU | SA(0) | TA(dst_ar) | IMM(imm), dst_ar);
  33. FAIL_IF(push_inst(compiler, LUI | TA(dst_ar) | IMM(imm >> 16), dst_ar));
  34. return (imm & 0xffff) ? push_inst(compiler, ORI | SA(dst_ar) | TA(dst_ar) | IMM(imm), dst_ar) : SLJIT_SUCCESS;
  35. }
  36. #define EMIT_LOGICAL(op_imm, op_norm) \
  37. if (flags & SRC2_IMM) { \
  38. if (op & SLJIT_SET_Z) \
  39. FAIL_IF(push_inst(compiler, op_imm | S(src1) | TA(EQUAL_FLAG) | IMM(src2), EQUAL_FLAG)); \
  40. if (!(flags & UNUSED_DEST)) \
  41. FAIL_IF(push_inst(compiler, op_imm | S(src1) | T(dst) | IMM(src2), DR(dst))); \
  42. } \
  43. else { \
  44. if (op & SLJIT_SET_Z) \
  45. FAIL_IF(push_inst(compiler, op_norm | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG)); \
  46. if (!(flags & UNUSED_DEST)) \
  47. FAIL_IF(push_inst(compiler, op_norm | S(src1) | T(src2) | D(dst), DR(dst))); \
  48. }
  49. #define EMIT_SHIFT(op_imm, op_v) \
  50. if (flags & SRC2_IMM) { \
  51. if (op & SLJIT_SET_Z) \
  52. FAIL_IF(push_inst(compiler, op_imm | T(src1) | DA(EQUAL_FLAG) | SH_IMM(src2), EQUAL_FLAG)); \
  53. if (!(flags & UNUSED_DEST)) \
  54. FAIL_IF(push_inst(compiler, op_imm | T(src1) | D(dst) | SH_IMM(src2), DR(dst))); \
  55. } \
  56. else { \
  57. if (op & SLJIT_SET_Z) \
  58. FAIL_IF(push_inst(compiler, op_v | S(src2) | T(src1) | DA(EQUAL_FLAG), EQUAL_FLAG)); \
  59. if (!(flags & UNUSED_DEST)) \
  60. FAIL_IF(push_inst(compiler, op_v | S(src2) | T(src1) | D(dst), DR(dst))); \
  61. }
  62. static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
  63. sljit_s32 dst, sljit_s32 src1, sljit_sw src2)
  64. {
  65. sljit_s32 is_overflow, is_carry, is_handled;
  66. switch (GET_OPCODE(op)) {
  67. case SLJIT_MOV:
  68. case SLJIT_MOV_U32:
  69. case SLJIT_MOV_S32:
  70. case SLJIT_MOV_P:
  71. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
  72. if (dst != src2)
  73. return push_inst(compiler, ADDU | S(src2) | TA(0) | D(dst), DR(dst));
  74. return SLJIT_SUCCESS;
  75. case SLJIT_MOV_U8:
  76. case SLJIT_MOV_S8:
  77. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
  78. if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
  79. if (op == SLJIT_MOV_S8) {
  80. #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
  81. return push_inst(compiler, SEB | T(src2) | D(dst), DR(dst));
  82. #else
  83. FAIL_IF(push_inst(compiler, SLL | T(src2) | D(dst) | SH_IMM(24), DR(dst)));
  84. return push_inst(compiler, SRA | T(dst) | D(dst) | SH_IMM(24), DR(dst));
  85. #endif
  86. }
  87. return push_inst(compiler, ANDI | S(src2) | T(dst) | IMM(0xff), DR(dst));
  88. }
  89. else {
  90. SLJIT_ASSERT(dst == src2);
  91. }
  92. return SLJIT_SUCCESS;
  93. case SLJIT_MOV_U16:
  94. case SLJIT_MOV_S16:
  95. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
  96. if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
  97. if (op == SLJIT_MOV_S16) {
  98. #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
  99. return push_inst(compiler, SEH | T(src2) | D(dst), DR(dst));
  100. #else
  101. FAIL_IF(push_inst(compiler, SLL | T(src2) | D(dst) | SH_IMM(16), DR(dst)));
  102. return push_inst(compiler, SRA | T(dst) | D(dst) | SH_IMM(16), DR(dst));
  103. #endif
  104. }
  105. return push_inst(compiler, ANDI | S(src2) | T(dst) | IMM(0xffff), DR(dst));
  106. }
  107. else {
  108. SLJIT_ASSERT(dst == src2);
  109. }
  110. return SLJIT_SUCCESS;
  111. case SLJIT_NOT:
  112. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
  113. if (op & SLJIT_SET_Z)
  114. FAIL_IF(push_inst(compiler, NOR | S(src2) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
  115. if (!(flags & UNUSED_DEST))
  116. FAIL_IF(push_inst(compiler, NOR | S(src2) | T(src2) | D(dst), DR(dst)));
  117. return SLJIT_SUCCESS;
  118. case SLJIT_CLZ:
  119. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
  120. #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
  121. if (op & SLJIT_SET_Z)
  122. FAIL_IF(push_inst(compiler, CLZ | S(src2) | TA(EQUAL_FLAG) | DA(EQUAL_FLAG), EQUAL_FLAG));
  123. if (!(flags & UNUSED_DEST))
  124. FAIL_IF(push_inst(compiler, CLZ | S(src2) | T(dst) | D(dst), DR(dst)));
  125. #else
  126. if (SLJIT_UNLIKELY(flags & UNUSED_DEST)) {
  127. FAIL_IF(push_inst(compiler, SRL | T(src2) | DA(EQUAL_FLAG) | SH_IMM(31), EQUAL_FLAG));
  128. return push_inst(compiler, XORI | SA(EQUAL_FLAG) | TA(EQUAL_FLAG) | IMM(1), EQUAL_FLAG);
  129. }
  130. /* Nearly all instructions are unmovable in the following sequence. */
  131. FAIL_IF(push_inst(compiler, ADDU | S(src2) | TA(0) | D(TMP_REG1), DR(TMP_REG1)));
  132. /* Check zero. */
  133. FAIL_IF(push_inst(compiler, BEQ | S(TMP_REG1) | TA(0) | IMM(5), UNMOVABLE_INS));
  134. FAIL_IF(push_inst(compiler, ORI | SA(0) | T(dst) | IMM(32), UNMOVABLE_INS));
  135. FAIL_IF(push_inst(compiler, ADDIU | SA(0) | T(dst) | IMM(-1), DR(dst)));
  136. /* Loop for searching the highest bit. */
  137. FAIL_IF(push_inst(compiler, ADDIU | S(dst) | T(dst) | IMM(1), DR(dst)));
  138. FAIL_IF(push_inst(compiler, BGEZ | S(TMP_REG1) | IMM(-2), UNMOVABLE_INS));
  139. FAIL_IF(push_inst(compiler, SLL | T(TMP_REG1) | D(TMP_REG1) | SH_IMM(1), UNMOVABLE_INS));
  140. #endif
  141. return SLJIT_SUCCESS;
  142. case SLJIT_ADD:
  143. is_overflow = GET_FLAG_TYPE(op) == SLJIT_OVERFLOW;
  144. is_carry = GET_FLAG_TYPE(op) == GET_FLAG_TYPE(SLJIT_SET_CARRY);
  145. if (flags & SRC2_IMM) {
  146. if (is_overflow) {
  147. if (src2 >= 0)
  148. FAIL_IF(push_inst(compiler, OR | S(src1) | T(src1) | DA(EQUAL_FLAG), EQUAL_FLAG));
  149. else
  150. FAIL_IF(push_inst(compiler, NOR | S(src1) | T(src1) | DA(EQUAL_FLAG), EQUAL_FLAG));
  151. }
  152. else if (op & SLJIT_SET_Z)
  153. FAIL_IF(push_inst(compiler, ADDIU | S(src1) | TA(EQUAL_FLAG) | IMM(src2), EQUAL_FLAG));
  154. if (is_overflow || is_carry) {
  155. if (src2 >= 0)
  156. FAIL_IF(push_inst(compiler, ORI | S(src1) | TA(OTHER_FLAG) | IMM(src2), OTHER_FLAG));
  157. else {
  158. FAIL_IF(push_inst(compiler, ADDIU | SA(0) | TA(OTHER_FLAG) | IMM(src2), OTHER_FLAG));
  159. FAIL_IF(push_inst(compiler, OR | S(src1) | TA(OTHER_FLAG) | DA(OTHER_FLAG), OTHER_FLAG));
  160. }
  161. }
  162. /* dst may be the same as src1 or src2. */
  163. if (!(flags & UNUSED_DEST) || (op & VARIABLE_FLAG_MASK))
  164. FAIL_IF(push_inst(compiler, ADDIU | S(src1) | T(dst) | IMM(src2), DR(dst)));
  165. }
  166. else {
  167. if (is_overflow)
  168. FAIL_IF(push_inst(compiler, XOR | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
  169. else if (op & SLJIT_SET_Z)
  170. FAIL_IF(push_inst(compiler, ADDU | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
  171. if (is_overflow || is_carry)
  172. FAIL_IF(push_inst(compiler, OR | S(src1) | T(src2) | DA(OTHER_FLAG), OTHER_FLAG));
  173. /* dst may be the same as src1 or src2. */
  174. if (!(flags & UNUSED_DEST) || (op & VARIABLE_FLAG_MASK))
  175. FAIL_IF(push_inst(compiler, ADDU | S(src1) | T(src2) | D(dst), DR(dst)));
  176. }
  177. /* a + b >= a | b (otherwise, the carry should be set to 1). */
  178. if (is_overflow || is_carry)
  179. FAIL_IF(push_inst(compiler, SLTU | S(dst) | TA(OTHER_FLAG) | DA(OTHER_FLAG), OTHER_FLAG));
  180. if (!is_overflow)
  181. return SLJIT_SUCCESS;
  182. FAIL_IF(push_inst(compiler, SLL | TA(OTHER_FLAG) | D(TMP_REG1) | SH_IMM(31), DR(TMP_REG1)));
  183. FAIL_IF(push_inst(compiler, XOR | S(TMP_REG1) | TA(EQUAL_FLAG) | DA(EQUAL_FLAG), EQUAL_FLAG));
  184. FAIL_IF(push_inst(compiler, XOR | S(dst) | TA(EQUAL_FLAG) | DA(OTHER_FLAG), OTHER_FLAG));
  185. if (op & SLJIT_SET_Z)
  186. FAIL_IF(push_inst(compiler, ADDU | S(dst) | TA(0) | DA(EQUAL_FLAG), EQUAL_FLAG));
  187. return push_inst(compiler, SRL | TA(OTHER_FLAG) | DA(OTHER_FLAG) | SH_IMM(31), OTHER_FLAG);
  188. case SLJIT_ADDC:
  189. is_carry = GET_FLAG_TYPE(op) == GET_FLAG_TYPE(SLJIT_SET_CARRY);
  190. if (flags & SRC2_IMM) {
  191. if (is_carry) {
  192. if (src2 >= 0)
  193. FAIL_IF(push_inst(compiler, ORI | S(src1) | TA(EQUAL_FLAG) | IMM(src2), EQUAL_FLAG));
  194. else {
  195. FAIL_IF(push_inst(compiler, ADDIU | SA(0) | TA(EQUAL_FLAG) | IMM(src2), EQUAL_FLAG));
  196. FAIL_IF(push_inst(compiler, OR | S(src1) | TA(EQUAL_FLAG) | DA(EQUAL_FLAG), EQUAL_FLAG));
  197. }
  198. }
  199. FAIL_IF(push_inst(compiler, ADDIU | S(src1) | T(dst) | IMM(src2), DR(dst)));
  200. } else {
  201. if (is_carry)
  202. FAIL_IF(push_inst(compiler, OR | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
  203. /* dst may be the same as src1 or src2. */
  204. FAIL_IF(push_inst(compiler, ADDU | S(src1) | T(src2) | D(dst), DR(dst)));
  205. }
  206. if (is_carry)
  207. FAIL_IF(push_inst(compiler, SLTU | S(dst) | TA(EQUAL_FLAG) | DA(EQUAL_FLAG), EQUAL_FLAG));
  208. FAIL_IF(push_inst(compiler, ADDU | S(dst) | TA(OTHER_FLAG) | D(dst), DR(dst)));
  209. if (!is_carry)
  210. return SLJIT_SUCCESS;
  211. /* Set ULESS_FLAG (dst == 0) && (OTHER_FLAG == 1). */
  212. FAIL_IF(push_inst(compiler, SLTU | S(dst) | TA(OTHER_FLAG) | DA(OTHER_FLAG), OTHER_FLAG));
  213. /* Set carry flag. */
  214. return push_inst(compiler, OR | SA(OTHER_FLAG) | TA(EQUAL_FLAG) | DA(OTHER_FLAG), OTHER_FLAG);
  215. case SLJIT_SUB:
  216. if ((flags & SRC2_IMM) && src2 == SIMM_MIN) {
  217. FAIL_IF(push_inst(compiler, ADDIU | SA(0) | T(TMP_REG2) | IMM(src2), DR(TMP_REG2)));
  218. src2 = TMP_REG2;
  219. flags &= ~SRC2_IMM;
  220. }
  221. is_handled = 0;
  222. if (flags & SRC2_IMM) {
  223. if (GET_FLAG_TYPE(op) == SLJIT_LESS || GET_FLAG_TYPE(op) == SLJIT_GREATER_EQUAL) {
  224. FAIL_IF(push_inst(compiler, SLTIU | S(src1) | TA(OTHER_FLAG) | IMM(src2), OTHER_FLAG));
  225. is_handled = 1;
  226. }
  227. else if (GET_FLAG_TYPE(op) == SLJIT_SIG_LESS || GET_FLAG_TYPE(op) == SLJIT_SIG_GREATER_EQUAL) {
  228. FAIL_IF(push_inst(compiler, SLTI | S(src1) | TA(OTHER_FLAG) | IMM(src2), OTHER_FLAG));
  229. is_handled = 1;
  230. }
  231. }
  232. if (!is_handled && GET_FLAG_TYPE(op) >= SLJIT_LESS && GET_FLAG_TYPE(op) <= SLJIT_SIG_LESS_EQUAL) {
  233. is_handled = 1;
  234. if (flags & SRC2_IMM) {
  235. FAIL_IF(push_inst(compiler, ADDIU | SA(0) | T(TMP_REG2) | IMM(src2), DR(TMP_REG2)));
  236. src2 = TMP_REG2;
  237. flags &= ~SRC2_IMM;
  238. }
  239. if (GET_FLAG_TYPE(op) == SLJIT_LESS || GET_FLAG_TYPE(op) == SLJIT_GREATER_EQUAL) {
  240. FAIL_IF(push_inst(compiler, SLTU | S(src1) | T(src2) | DA(OTHER_FLAG), OTHER_FLAG));
  241. }
  242. else if (GET_FLAG_TYPE(op) == SLJIT_GREATER || GET_FLAG_TYPE(op) == SLJIT_LESS_EQUAL)
  243. {
  244. FAIL_IF(push_inst(compiler, SLTU | S(src2) | T(src1) | DA(OTHER_FLAG), OTHER_FLAG));
  245. }
  246. else if (GET_FLAG_TYPE(op) == SLJIT_SIG_LESS || GET_FLAG_TYPE(op) == SLJIT_SIG_GREATER_EQUAL) {
  247. FAIL_IF(push_inst(compiler, SLT | S(src1) | T(src2) | DA(OTHER_FLAG), OTHER_FLAG));
  248. }
  249. else if (GET_FLAG_TYPE(op) == SLJIT_SIG_GREATER || GET_FLAG_TYPE(op) == SLJIT_SIG_LESS_EQUAL)
  250. {
  251. FAIL_IF(push_inst(compiler, SLT | S(src2) | T(src1) | DA(OTHER_FLAG), OTHER_FLAG));
  252. }
  253. }
  254. if (is_handled) {
  255. if (flags & SRC2_IMM) {
  256. if (op & SLJIT_SET_Z)
  257. FAIL_IF(push_inst(compiler, ADDIU | S(src1) | TA(EQUAL_FLAG) | IMM(-src2), EQUAL_FLAG));
  258. if (!(flags & UNUSED_DEST))
  259. return push_inst(compiler, ADDIU | S(src1) | T(dst) | IMM(-src2), DR(dst));
  260. }
  261. else {
  262. if (op & SLJIT_SET_Z)
  263. FAIL_IF(push_inst(compiler, SUBU | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
  264. if (!(flags & UNUSED_DEST))
  265. return push_inst(compiler, SUBU | S(src1) | T(src2) | D(dst), DR(dst));
  266. }
  267. return SLJIT_SUCCESS;
  268. }
  269. is_overflow = GET_FLAG_TYPE(op) == SLJIT_OVERFLOW;
  270. is_carry = GET_FLAG_TYPE(op) == GET_FLAG_TYPE(SLJIT_SET_CARRY);
  271. if (flags & SRC2_IMM) {
  272. if (is_overflow) {
  273. if (src2 >= 0)
  274. FAIL_IF(push_inst(compiler, OR | S(src1) | T(src1) | DA(EQUAL_FLAG), EQUAL_FLAG));
  275. else
  276. FAIL_IF(push_inst(compiler, NOR | S(src1) | T(src1) | DA(EQUAL_FLAG), EQUAL_FLAG));
  277. }
  278. else if (op & SLJIT_SET_Z)
  279. FAIL_IF(push_inst(compiler, ADDIU | S(src1) | TA(EQUAL_FLAG) | IMM(-src2), EQUAL_FLAG));
  280. if (is_overflow || is_carry)
  281. FAIL_IF(push_inst(compiler, SLTIU | S(src1) | TA(OTHER_FLAG) | IMM(src2), OTHER_FLAG));
  282. /* dst may be the same as src1 or src2. */
  283. if (!(flags & UNUSED_DEST) || (op & VARIABLE_FLAG_MASK))
  284. FAIL_IF(push_inst(compiler, ADDIU | S(src1) | T(dst) | IMM(-src2), DR(dst)));
  285. }
  286. else {
  287. if (is_overflow)
  288. FAIL_IF(push_inst(compiler, XOR | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
  289. else if (op & SLJIT_SET_Z)
  290. FAIL_IF(push_inst(compiler, SUBU | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
  291. if (is_overflow || is_carry)
  292. FAIL_IF(push_inst(compiler, SLTU | S(src1) | T(src2) | DA(OTHER_FLAG), OTHER_FLAG));
  293. /* dst may be the same as src1 or src2. */
  294. if (!(flags & UNUSED_DEST) || (op & VARIABLE_FLAG_MASK))
  295. FAIL_IF(push_inst(compiler, SUBU | S(src1) | T(src2) | D(dst), DR(dst)));
  296. }
  297. if (!is_overflow)
  298. return SLJIT_SUCCESS;
  299. FAIL_IF(push_inst(compiler, SLL | TA(OTHER_FLAG) | D(TMP_REG1) | SH_IMM(31), DR(TMP_REG1)));
  300. FAIL_IF(push_inst(compiler, XOR | S(TMP_REG1) | TA(EQUAL_FLAG) | DA(EQUAL_FLAG), EQUAL_FLAG));
  301. FAIL_IF(push_inst(compiler, XOR | S(dst) | TA(EQUAL_FLAG) | DA(OTHER_FLAG), OTHER_FLAG));
  302. if (op & SLJIT_SET_Z)
  303. FAIL_IF(push_inst(compiler, ADDU | S(dst) | TA(0) | DA(EQUAL_FLAG), EQUAL_FLAG));
  304. return push_inst(compiler, SRL | TA(OTHER_FLAG) | DA(OTHER_FLAG) | SH_IMM(31), OTHER_FLAG);
  305. case SLJIT_SUBC:
  306. if ((flags & SRC2_IMM) && src2 == SIMM_MIN) {
  307. FAIL_IF(push_inst(compiler, ADDIU | SA(0) | T(TMP_REG2) | IMM(src2), DR(TMP_REG2)));
  308. src2 = TMP_REG2;
  309. flags &= ~SRC2_IMM;
  310. }
  311. is_carry = GET_FLAG_TYPE(op) == GET_FLAG_TYPE(SLJIT_SET_CARRY);
  312. if (flags & SRC2_IMM) {
  313. if (is_carry)
  314. FAIL_IF(push_inst(compiler, SLTIU | S(src1) | TA(EQUAL_FLAG) | IMM(src2), EQUAL_FLAG));
  315. /* dst may be the same as src1 or src2. */
  316. FAIL_IF(push_inst(compiler, ADDIU | S(src1) | T(dst) | IMM(-src2), DR(dst)));
  317. }
  318. else {
  319. if (is_carry)
  320. FAIL_IF(push_inst(compiler, SLTU | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
  321. /* dst may be the same as src1 or src2. */
  322. FAIL_IF(push_inst(compiler, SUBU | S(src1) | T(src2) | D(dst), DR(dst)));
  323. }
  324. if (is_carry)
  325. FAIL_IF(push_inst(compiler, SLTU | S(dst) | TA(OTHER_FLAG) | D(TMP_REG1), DR(TMP_REG1)));
  326. FAIL_IF(push_inst(compiler, SUBU | S(dst) | TA(OTHER_FLAG) | D(dst), DR(dst)));
  327. return (is_carry) ? push_inst(compiler, OR | SA(EQUAL_FLAG) | T(TMP_REG1) | DA(OTHER_FLAG), OTHER_FLAG) : SLJIT_SUCCESS;
  328. case SLJIT_MUL:
  329. SLJIT_ASSERT(!(flags & SRC2_IMM));
  330. if (GET_FLAG_TYPE(op) != SLJIT_MUL_OVERFLOW) {
  331. #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1) || (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  332. return push_inst(compiler, MUL | S(src1) | T(src2) | D(dst), DR(dst));
  333. #else /* !SLJIT_MIPS_R1 && !SLJIT_MIPS_R6 */
  334. FAIL_IF(push_inst(compiler, MULT | S(src1) | T(src2), MOVABLE_INS));
  335. return push_inst(compiler, MFLO | D(dst), DR(dst));
  336. #endif /* SLJIT_MIPS_R1 || SLJIT_MIPS_R6 */
  337. }
  338. #if (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  339. FAIL_IF(push_inst(compiler, MUL | S(src1) | T(src2) | D(dst), DR(dst)));
  340. FAIL_IF(push_inst(compiler, MUH | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
  341. #else /* !SLJIT_MIPS_R6 */
  342. FAIL_IF(push_inst(compiler, MULT | S(src1) | T(src2), MOVABLE_INS));
  343. FAIL_IF(push_inst(compiler, MFHI | DA(EQUAL_FLAG), EQUAL_FLAG));
  344. FAIL_IF(push_inst(compiler, MFLO | D(dst), DR(dst)));
  345. #endif /* SLJIT_MIPS_R6 */
  346. FAIL_IF(push_inst(compiler, SRA | T(dst) | DA(OTHER_FLAG) | SH_IMM(31), OTHER_FLAG));
  347. return push_inst(compiler, SUBU | SA(EQUAL_FLAG) | TA(OTHER_FLAG) | DA(OTHER_FLAG), OTHER_FLAG);
  348. case SLJIT_AND:
  349. EMIT_LOGICAL(ANDI, AND);
  350. return SLJIT_SUCCESS;
  351. case SLJIT_OR:
  352. EMIT_LOGICAL(ORI, OR);
  353. return SLJIT_SUCCESS;
  354. case SLJIT_XOR:
  355. EMIT_LOGICAL(XORI, XOR);
  356. return SLJIT_SUCCESS;
  357. case SLJIT_SHL:
  358. EMIT_SHIFT(SLL, SLLV);
  359. return SLJIT_SUCCESS;
  360. case SLJIT_LSHR:
  361. EMIT_SHIFT(SRL, SRLV);
  362. return SLJIT_SUCCESS;
  363. case SLJIT_ASHR:
  364. EMIT_SHIFT(SRA, SRAV);
  365. return SLJIT_SUCCESS;
  366. }
  367. SLJIT_UNREACHABLE();
  368. return SLJIT_SUCCESS;
  369. }
  370. static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw init_value)
  371. {
  372. FAIL_IF(push_inst(compiler, LUI | T(dst) | IMM(init_value >> 16), DR(dst)));
  373. return push_inst(compiler, ORI | S(dst) | T(dst) | IMM(init_value), DR(dst));
  374. }
  375. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
  376. {
  377. sljit_ins *inst = (sljit_ins *)addr;
  378. SLJIT_ASSERT((inst[0] & 0xffe00000) == LUI && (inst[1] & 0xfc000000) == ORI);
  379. inst[0] = (inst[0] & 0xffff0000) | ((new_target >> 16) & 0xffff);
  380. inst[1] = (inst[1] & 0xffff0000) | (new_target & 0xffff);
  381. inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  382. SLJIT_CACHE_FLUSH(inst, inst + 2);
  383. }
  384. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
  385. {
  386. sljit_ins *inst = (sljit_ins *)addr;
  387. SLJIT_ASSERT((inst[0] & 0xffe00000) == LUI && (inst[1] & 0xfc000000) == ORI);
  388. inst[0] = (inst[0] & 0xffff0000) | ((new_constant >> 16) & 0xffff);
  389. inst[1] = (inst[1] & 0xffff0000) | (new_constant & 0xffff);
  390. inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  391. SLJIT_CACHE_FLUSH(inst, inst + 2);
  392. }
  393. static sljit_s32 call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_ins *ins_ptr)
  394. {
  395. sljit_s32 stack_offset = 0;
  396. sljit_s32 arg_count = 0;
  397. sljit_s32 float_arg_count = 0;
  398. sljit_s32 word_arg_count = 0;
  399. sljit_s32 types = 0;
  400. sljit_s32 arg_count_save, types_save;
  401. sljit_ins prev_ins = NOP;
  402. sljit_ins ins = NOP;
  403. sljit_u8 offsets[4];
  404. SLJIT_ASSERT(reg_map[TMP_REG1] == 4 && freg_map[TMP_FREG1] == 12);
  405. arg_types >>= SLJIT_DEF_SHIFT;
  406. while (arg_types) {
  407. types = (types << SLJIT_DEF_SHIFT) | (arg_types & SLJIT_DEF_MASK);
  408. switch (arg_types & SLJIT_DEF_MASK) {
  409. case SLJIT_ARG_TYPE_F32:
  410. offsets[arg_count] = (sljit_u8)stack_offset;
  411. if (word_arg_count == 0 && arg_count <= 1)
  412. offsets[arg_count] = 254 + arg_count;
  413. stack_offset += sizeof(sljit_f32);
  414. arg_count++;
  415. float_arg_count++;
  416. break;
  417. case SLJIT_ARG_TYPE_F64:
  418. if (stack_offset & 0x7)
  419. stack_offset += sizeof(sljit_sw);
  420. offsets[arg_count] = (sljit_u8)stack_offset;
  421. if (word_arg_count == 0 && arg_count <= 1)
  422. offsets[arg_count] = 254 + arg_count;
  423. stack_offset += sizeof(sljit_f64);
  424. arg_count++;
  425. float_arg_count++;
  426. break;
  427. default:
  428. offsets[arg_count] = (sljit_u8)stack_offset;
  429. stack_offset += sizeof(sljit_sw);
  430. arg_count++;
  431. word_arg_count++;
  432. break;
  433. }
  434. arg_types >>= SLJIT_DEF_SHIFT;
  435. }
  436. /* Stack is aligned to 16 bytes, max two doubles can be placed on the stack. */
  437. if (stack_offset > 16)
  438. FAIL_IF(push_inst(compiler, ADDIU | S(SLJIT_SP) | T(SLJIT_SP) | IMM(-16), DR(SLJIT_SP)));
  439. types_save = types;
  440. arg_count_save = arg_count;
  441. while (types) {
  442. switch (types & SLJIT_DEF_MASK) {
  443. case SLJIT_ARG_TYPE_F32:
  444. arg_count--;
  445. if (offsets[arg_count] < 254)
  446. ins = SWC1 | S(SLJIT_SP) | FT(float_arg_count) | IMM(offsets[arg_count]);
  447. float_arg_count--;
  448. break;
  449. case SLJIT_ARG_TYPE_F64:
  450. arg_count--;
  451. if (offsets[arg_count] < 254)
  452. ins = SDC1 | S(SLJIT_SP) | FT(float_arg_count) | IMM(offsets[arg_count]);
  453. float_arg_count--;
  454. break;
  455. default:
  456. if (offsets[arg_count - 1] >= 16)
  457. ins = SW | S(SLJIT_SP) | T(word_arg_count) | IMM(offsets[arg_count - 1]);
  458. else if (arg_count != word_arg_count)
  459. ins = ADDU | S(word_arg_count) | TA(0) | DA(4 + (offsets[arg_count - 1] >> 2));
  460. else if (arg_count == 1)
  461. ins = ADDU | S(SLJIT_R0) | TA(0) | DA(4);
  462. arg_count--;
  463. word_arg_count--;
  464. break;
  465. }
  466. if (ins != NOP) {
  467. if (prev_ins != NOP)
  468. FAIL_IF(push_inst(compiler, prev_ins, MOVABLE_INS));
  469. prev_ins = ins;
  470. ins = NOP;
  471. }
  472. types >>= SLJIT_DEF_SHIFT;
  473. }
  474. types = types_save;
  475. arg_count = arg_count_save;
  476. while (types) {
  477. switch (types & SLJIT_DEF_MASK) {
  478. case SLJIT_ARG_TYPE_F32:
  479. arg_count--;
  480. if (offsets[arg_count] == 254)
  481. ins = MOV_S | FMT_S | FS(SLJIT_FR0) | FD(TMP_FREG1);
  482. else if (offsets[arg_count] < 16)
  483. ins = LW | S(SLJIT_SP) | TA(4 + (offsets[arg_count] >> 2)) | IMM(offsets[arg_count]);
  484. break;
  485. case SLJIT_ARG_TYPE_F64:
  486. arg_count--;
  487. if (offsets[arg_count] == 254)
  488. ins = MOV_S | FMT_D | FS(SLJIT_FR0) | FD(TMP_FREG1);
  489. else if (offsets[arg_count] < 16) {
  490. if (prev_ins != NOP)
  491. FAIL_IF(push_inst(compiler, prev_ins, MOVABLE_INS));
  492. prev_ins = LW | S(SLJIT_SP) | TA(4 + (offsets[arg_count] >> 2)) | IMM(offsets[arg_count]);
  493. ins = LW | S(SLJIT_SP) | TA(5 + (offsets[arg_count] >> 2)) | IMM(offsets[arg_count] + sizeof(sljit_sw));
  494. }
  495. break;
  496. default:
  497. arg_count--;
  498. break;
  499. }
  500. if (ins != NOP) {
  501. if (prev_ins != NOP)
  502. FAIL_IF(push_inst(compiler, prev_ins, MOVABLE_INS));
  503. prev_ins = ins;
  504. ins = NOP;
  505. }
  506. types >>= SLJIT_DEF_SHIFT;
  507. }
  508. *ins_ptr = prev_ins;
  509. return SLJIT_SUCCESS;
  510. }
  511. static sljit_s32 post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
  512. {
  513. sljit_s32 stack_offset = 0;
  514. arg_types >>= SLJIT_DEF_SHIFT;
  515. while (arg_types) {
  516. switch (arg_types & SLJIT_DEF_MASK) {
  517. case SLJIT_ARG_TYPE_F32:
  518. stack_offset += sizeof(sljit_f32);
  519. break;
  520. case SLJIT_ARG_TYPE_F64:
  521. if (stack_offset & 0x7)
  522. stack_offset += sizeof(sljit_sw);
  523. stack_offset += sizeof(sljit_f64);
  524. break;
  525. default:
  526. stack_offset += sizeof(sljit_sw);
  527. break;
  528. }
  529. arg_types >>= SLJIT_DEF_SHIFT;
  530. }
  531. /* Stack is aligned to 16 bytes, max two doubles can be placed on the stack. */
  532. if (stack_offset > 16)
  533. return push_inst(compiler, ADDIU | S(SLJIT_SP) | T(SLJIT_SP) | IMM(16), DR(SLJIT_SP));
  534. return SLJIT_SUCCESS;
  535. }
  536. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
  537. sljit_s32 arg_types)
  538. {
  539. struct sljit_jump *jump;
  540. sljit_ins ins;
  541. CHECK_ERROR_PTR();
  542. CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
  543. jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
  544. PTR_FAIL_IF(!jump);
  545. set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
  546. type &= 0xff;
  547. PTR_FAIL_IF(call_with_args(compiler, arg_types, &ins));
  548. SLJIT_ASSERT(DR(PIC_ADDR_REG) == 25 && PIC_ADDR_REG == TMP_REG2);
  549. PTR_FAIL_IF(emit_const(compiler, PIC_ADDR_REG, 0));
  550. jump->flags |= IS_JAL | IS_CALL;
  551. PTR_FAIL_IF(push_inst(compiler, JALR | S(PIC_ADDR_REG) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
  552. jump->addr = compiler->size;
  553. PTR_FAIL_IF(push_inst(compiler, ins, UNMOVABLE_INS));
  554. PTR_FAIL_IF(post_call_with_args(compiler, arg_types));
  555. return jump;
  556. }
  557. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
  558. sljit_s32 arg_types,
  559. sljit_s32 src, sljit_sw srcw)
  560. {
  561. sljit_ins ins;
  562. CHECK_ERROR();
  563. CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
  564. SLJIT_ASSERT(DR(PIC_ADDR_REG) == 25 && PIC_ADDR_REG == TMP_REG2);
  565. if (src & SLJIT_IMM)
  566. FAIL_IF(load_immediate(compiler, DR(PIC_ADDR_REG), srcw));
  567. else if (FAST_IS_REG(src))
  568. FAIL_IF(push_inst(compiler, ADDU | S(src) | TA(0) | D(PIC_ADDR_REG), DR(PIC_ADDR_REG)));
  569. else if (src & SLJIT_MEM) {
  570. ADJUST_LOCAL_OFFSET(src, srcw);
  571. FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, DR(PIC_ADDR_REG), src, srcw));
  572. }
  573. FAIL_IF(call_with_args(compiler, arg_types, &ins));
  574. /* Register input. */
  575. FAIL_IF(push_inst(compiler, JALR | S(PIC_ADDR_REG) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
  576. FAIL_IF(push_inst(compiler, ins, UNMOVABLE_INS));
  577. return post_call_with_args(compiler, arg_types);
  578. }