sljitNativePPC_64.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 64-bit arch dependent functions. */
  27. #if defined(__GNUC__) || (defined(__IBM_GCC_ASM) && __IBM_GCC_ASM)
  28. #define ASM_SLJIT_CLZ(src, dst) \
  29. __asm__ volatile ( "cntlzd %0, %1" : "=r"(dst) : "r"(src) )
  30. #elif defined(__xlc__)
  31. #error "Please enable GCC syntax for inline assembly statements"
  32. #else
  33. #error "Must implement count leading zeroes"
  34. #endif
  35. #define PUSH_RLDICR(reg, shift) \
  36. push_inst(compiler, RLDI(reg, reg, 63 - shift, shift, 1))
  37. static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 reg, sljit_sw imm)
  38. {
  39. sljit_uw tmp;
  40. sljit_uw shift;
  41. sljit_uw tmp2;
  42. sljit_uw shift2;
  43. if (imm <= SIMM_MAX && imm >= SIMM_MIN)
  44. return push_inst(compiler, ADDI | D(reg) | A(0) | IMM(imm));
  45. if (!(imm & ~0xffff))
  46. return push_inst(compiler, ORI | S(TMP_ZERO) | A(reg) | IMM(imm));
  47. if (imm <= 0x7fffffffl && imm >= -0x80000000l) {
  48. FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(imm >> 16)));
  49. return (imm & 0xffff) ? push_inst(compiler, ORI | S(reg) | A(reg) | IMM(imm)) : SLJIT_SUCCESS;
  50. }
  51. /* Count leading zeroes. */
  52. tmp = (imm >= 0) ? imm : ~imm;
  53. ASM_SLJIT_CLZ(tmp, shift);
  54. SLJIT_ASSERT(shift > 0);
  55. shift--;
  56. tmp = (imm << shift);
  57. if ((tmp & ~0xffff000000000000ul) == 0) {
  58. FAIL_IF(push_inst(compiler, ADDI | D(reg) | A(0) | IMM(tmp >> 48)));
  59. shift += 15;
  60. return PUSH_RLDICR(reg, shift);
  61. }
  62. if ((tmp & ~0xffffffff00000000ul) == 0) {
  63. FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(tmp >> 48)));
  64. FAIL_IF(push_inst(compiler, ORI | S(reg) | A(reg) | IMM(tmp >> 32)));
  65. shift += 31;
  66. return PUSH_RLDICR(reg, shift);
  67. }
  68. /* Cut out the 16 bit from immediate. */
  69. shift += 15;
  70. tmp2 = imm & ((1ul << (63 - shift)) - 1);
  71. if (tmp2 <= 0xffff) {
  72. FAIL_IF(push_inst(compiler, ADDI | D(reg) | A(0) | IMM(tmp >> 48)));
  73. FAIL_IF(PUSH_RLDICR(reg, shift));
  74. return push_inst(compiler, ORI | S(reg) | A(reg) | tmp2);
  75. }
  76. if (tmp2 <= 0xffffffff) {
  77. FAIL_IF(push_inst(compiler, ADDI | D(reg) | A(0) | IMM(tmp >> 48)));
  78. FAIL_IF(PUSH_RLDICR(reg, shift));
  79. FAIL_IF(push_inst(compiler, ORIS | S(reg) | A(reg) | (tmp2 >> 16)));
  80. return (imm & 0xffff) ? push_inst(compiler, ORI | S(reg) | A(reg) | IMM(tmp2)) : SLJIT_SUCCESS;
  81. }
  82. ASM_SLJIT_CLZ(tmp2, shift2);
  83. tmp2 <<= shift2;
  84. if ((tmp2 & ~0xffff000000000000ul) == 0) {
  85. FAIL_IF(push_inst(compiler, ADDI | D(reg) | A(0) | IMM(tmp >> 48)));
  86. shift2 += 15;
  87. shift += (63 - shift2);
  88. FAIL_IF(PUSH_RLDICR(reg, shift));
  89. FAIL_IF(push_inst(compiler, ORI | S(reg) | A(reg) | (tmp2 >> 48)));
  90. return PUSH_RLDICR(reg, shift2);
  91. }
  92. /* The general version. */
  93. FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(imm >> 48)));
  94. FAIL_IF(push_inst(compiler, ORI | S(reg) | A(reg) | IMM(imm >> 32)));
  95. FAIL_IF(PUSH_RLDICR(reg, 31));
  96. FAIL_IF(push_inst(compiler, ORIS | S(reg) | A(reg) | IMM(imm >> 16)));
  97. return push_inst(compiler, ORI | S(reg) | A(reg) | IMM(imm));
  98. }
  99. /* Simplified mnemonics: clrldi. */
  100. #define INS_CLEAR_LEFT(dst, src, from) \
  101. (RLDICL | S(src) | A(dst) | ((from) << 6) | (1 << 5))
  102. /* Sign extension for integer operations. */
  103. #define UN_EXTS() \
  104. if ((flags & (ALT_SIGN_EXT | REG2_SOURCE)) == (ALT_SIGN_EXT | REG2_SOURCE)) { \
  105. FAIL_IF(push_inst(compiler, EXTSW | S(src2) | A(TMP_REG2))); \
  106. src2 = TMP_REG2; \
  107. }
  108. #define BIN_EXTS() \
  109. if (flags & ALT_SIGN_EXT) { \
  110. if (flags & REG1_SOURCE) { \
  111. FAIL_IF(push_inst(compiler, EXTSW | S(src1) | A(TMP_REG1))); \
  112. src1 = TMP_REG1; \
  113. } \
  114. if (flags & REG2_SOURCE) { \
  115. FAIL_IF(push_inst(compiler, EXTSW | S(src2) | A(TMP_REG2))); \
  116. src2 = TMP_REG2; \
  117. } \
  118. }
  119. #define BIN_IMM_EXTS() \
  120. if ((flags & (ALT_SIGN_EXT | REG1_SOURCE)) == (ALT_SIGN_EXT | REG1_SOURCE)) { \
  121. FAIL_IF(push_inst(compiler, EXTSW | S(src1) | A(TMP_REG1))); \
  122. src1 = TMP_REG1; \
  123. }
  124. static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
  125. sljit_s32 dst, sljit_s32 src1, sljit_s32 src2)
  126. {
  127. switch (op) {
  128. case SLJIT_MOV:
  129. case SLJIT_MOV_P:
  130. SLJIT_ASSERT(src1 == TMP_REG1);
  131. if (dst != src2)
  132. return push_inst(compiler, OR | S(src2) | A(dst) | B(src2));
  133. return SLJIT_SUCCESS;
  134. case SLJIT_MOV_U32:
  135. case SLJIT_MOV_S32:
  136. SLJIT_ASSERT(src1 == TMP_REG1);
  137. if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
  138. if (op == SLJIT_MOV_S32)
  139. return push_inst(compiler, EXTSW | S(src2) | A(dst));
  140. return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 0));
  141. }
  142. else {
  143. SLJIT_ASSERT(dst == src2);
  144. }
  145. return SLJIT_SUCCESS;
  146. case SLJIT_MOV_U8:
  147. case SLJIT_MOV_S8:
  148. SLJIT_ASSERT(src1 == TMP_REG1);
  149. if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
  150. if (op == SLJIT_MOV_S8)
  151. return push_inst(compiler, EXTSB | S(src2) | A(dst));
  152. return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 24));
  153. }
  154. else if ((flags & REG_DEST) && op == SLJIT_MOV_S8)
  155. return push_inst(compiler, EXTSB | S(src2) | A(dst));
  156. else {
  157. SLJIT_ASSERT(dst == src2);
  158. }
  159. return SLJIT_SUCCESS;
  160. case SLJIT_MOV_U16:
  161. case SLJIT_MOV_S16:
  162. SLJIT_ASSERT(src1 == TMP_REG1);
  163. if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
  164. if (op == SLJIT_MOV_S16)
  165. return push_inst(compiler, EXTSH | S(src2) | A(dst));
  166. return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 16));
  167. }
  168. else {
  169. SLJIT_ASSERT(dst == src2);
  170. }
  171. return SLJIT_SUCCESS;
  172. case SLJIT_NOT:
  173. SLJIT_ASSERT(src1 == TMP_REG1);
  174. UN_EXTS();
  175. return push_inst(compiler, NOR | RC(flags) | S(src2) | A(dst) | B(src2));
  176. case SLJIT_NEG:
  177. SLJIT_ASSERT(src1 == TMP_REG1);
  178. if ((flags & (ALT_FORM1 | ALT_SIGN_EXT)) == (ALT_FORM1 | ALT_SIGN_EXT)) {
  179. FAIL_IF(push_inst(compiler, RLDI(TMP_REG2, src2, 32, 31, 1)));
  180. FAIL_IF(push_inst(compiler, NEG | OE(ALT_SET_FLAGS) | RC(ALT_SET_FLAGS) | D(dst) | A(TMP_REG2)));
  181. return push_inst(compiler, RLDI(dst, dst, 32, 32, 0));
  182. }
  183. UN_EXTS();
  184. /* Setting XER SO is not enough, CR SO is also needed. */
  185. return push_inst(compiler, NEG | OE((flags & ALT_FORM1) ? ALT_SET_FLAGS : 0) | RC(flags) | D(dst) | A(src2));
  186. case SLJIT_CLZ:
  187. SLJIT_ASSERT(src1 == TMP_REG1);
  188. if (flags & ALT_FORM1)
  189. return push_inst(compiler, CNTLZW | S(src2) | A(dst));
  190. return push_inst(compiler, CNTLZD | S(src2) | A(dst));
  191. case SLJIT_ADD:
  192. if (flags & ALT_FORM1) {
  193. if (flags & ALT_SIGN_EXT) {
  194. FAIL_IF(push_inst(compiler, RLDI(TMP_REG1, src1, 32, 31, 1)));
  195. src1 = TMP_REG1;
  196. FAIL_IF(push_inst(compiler, RLDI(TMP_REG2, src2, 32, 31, 1)));
  197. src2 = TMP_REG2;
  198. }
  199. /* Setting XER SO is not enough, CR SO is also needed. */
  200. FAIL_IF(push_inst(compiler, ADD | OE(ALT_SET_FLAGS) | RC(ALT_SET_FLAGS) | D(dst) | A(src1) | B(src2)));
  201. if (flags & ALT_SIGN_EXT)
  202. return push_inst(compiler, RLDI(dst, dst, 32, 32, 0));
  203. return SLJIT_SUCCESS;
  204. }
  205. if (flags & ALT_FORM2) {
  206. /* Flags does not set: BIN_IMM_EXTS unnecessary. */
  207. SLJIT_ASSERT(src2 == TMP_REG2);
  208. if (flags & ALT_FORM3)
  209. return push_inst(compiler, ADDIS | D(dst) | A(src1) | compiler->imm);
  210. if (flags & ALT_FORM4) {
  211. FAIL_IF(push_inst(compiler, ADDIS | D(dst) | A(src1) | (((compiler->imm >> 16) & 0xffff) + ((compiler->imm >> 15) & 0x1))));
  212. src1 = dst;
  213. }
  214. return push_inst(compiler, ADDI | D(dst) | A(src1) | (compiler->imm & 0xffff));
  215. }
  216. if (flags & ALT_FORM3) {
  217. SLJIT_ASSERT(src2 == TMP_REG2);
  218. BIN_IMM_EXTS();
  219. return push_inst(compiler, ADDIC | D(dst) | A(src1) | compiler->imm);
  220. }
  221. if (!(flags & ALT_SET_FLAGS))
  222. return push_inst(compiler, ADD | D(dst) | A(src1) | B(src2));
  223. BIN_EXTS();
  224. if (flags & ALT_FORM4)
  225. return push_inst(compiler, ADDC | RC(ALT_SET_FLAGS) | D(dst) | A(src1) | B(src2));
  226. return push_inst(compiler, ADD | RC(flags) | D(dst) | A(src1) | B(src2));
  227. case SLJIT_ADDC:
  228. BIN_EXTS();
  229. return push_inst(compiler, ADDE | D(dst) | A(src1) | B(src2));
  230. case SLJIT_SUB:
  231. if (flags & ALT_FORM1) {
  232. if (flags & ALT_FORM2) {
  233. FAIL_IF(push_inst(compiler, CMPLI | CRD(0 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | compiler->imm));
  234. if (!(flags & ALT_FORM3))
  235. return SLJIT_SUCCESS;
  236. return push_inst(compiler, ADDI | D(dst) | A(src1) | (-compiler->imm & 0xffff));
  237. }
  238. FAIL_IF(push_inst(compiler, CMPL | CRD(0 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | B(src2)));
  239. if (!(flags & ALT_FORM3))
  240. return SLJIT_SUCCESS;
  241. return push_inst(compiler, SUBF | D(dst) | A(src2) | B(src1));
  242. }
  243. if (flags & ALT_FORM2) {
  244. if (flags & ALT_SIGN_EXT) {
  245. FAIL_IF(push_inst(compiler, RLDI(TMP_REG1, src1, 32, 31, 1)));
  246. src1 = TMP_REG1;
  247. FAIL_IF(push_inst(compiler, RLDI(TMP_REG2, src2, 32, 31, 1)));
  248. src2 = TMP_REG2;
  249. }
  250. /* Setting XER SO is not enough, CR SO is also needed. */
  251. FAIL_IF(push_inst(compiler, SUBF | OE(ALT_SET_FLAGS) | RC(ALT_SET_FLAGS) | D(dst) | A(src2) | B(src1)));
  252. if (flags & ALT_SIGN_EXT)
  253. return push_inst(compiler, RLDI(dst, dst, 32, 32, 0));
  254. return SLJIT_SUCCESS;
  255. }
  256. if (flags & ALT_FORM3) {
  257. /* Flags does not set: BIN_IMM_EXTS unnecessary. */
  258. SLJIT_ASSERT(src2 == TMP_REG2);
  259. return push_inst(compiler, SUBFIC | D(dst) | A(src1) | compiler->imm);
  260. }
  261. if (flags & ALT_FORM4) {
  262. if (flags & ALT_FORM5) {
  263. SLJIT_ASSERT(src2 == TMP_REG2);
  264. return push_inst(compiler, CMPI | CRD(0 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | compiler->imm);
  265. }
  266. return push_inst(compiler, CMP | CRD(0 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | B(src2));
  267. }
  268. if (!(flags & ALT_SET_FLAGS))
  269. return push_inst(compiler, SUBF | D(dst) | A(src2) | B(src1));
  270. BIN_EXTS();
  271. if (flags & ALT_FORM5)
  272. return push_inst(compiler, SUBFC | RC(ALT_SET_FLAGS) | D(dst) | A(src2) | B(src1));
  273. return push_inst(compiler, SUBF | RC(flags) | D(dst) | A(src2) | B(src1));
  274. case SLJIT_SUBC:
  275. BIN_EXTS();
  276. return push_inst(compiler, SUBFE | D(dst) | A(src2) | B(src1));
  277. case SLJIT_MUL:
  278. if (flags & ALT_FORM1) {
  279. SLJIT_ASSERT(src2 == TMP_REG2);
  280. return push_inst(compiler, MULLI | D(dst) | A(src1) | compiler->imm);
  281. }
  282. BIN_EXTS();
  283. if (flags & ALT_FORM2)
  284. return push_inst(compiler, MULLW | OE(flags) | RC(flags) | D(dst) | A(src2) | B(src1));
  285. return push_inst(compiler, MULLD | OE(flags) | RC(flags) | D(dst) | A(src2) | B(src1));
  286. case SLJIT_AND:
  287. if (flags & ALT_FORM1) {
  288. SLJIT_ASSERT(src2 == TMP_REG2);
  289. return push_inst(compiler, ANDI | S(src1) | A(dst) | compiler->imm);
  290. }
  291. if (flags & ALT_FORM2) {
  292. SLJIT_ASSERT(src2 == TMP_REG2);
  293. return push_inst(compiler, ANDIS | S(src1) | A(dst) | compiler->imm);
  294. }
  295. return push_inst(compiler, AND | RC(flags) | S(src1) | A(dst) | B(src2));
  296. case SLJIT_OR:
  297. if (flags & ALT_FORM1) {
  298. SLJIT_ASSERT(src2 == TMP_REG2);
  299. return push_inst(compiler, ORI | S(src1) | A(dst) | compiler->imm);
  300. }
  301. if (flags & ALT_FORM2) {
  302. SLJIT_ASSERT(src2 == TMP_REG2);
  303. return push_inst(compiler, ORIS | S(src1) | A(dst) | compiler->imm);
  304. }
  305. if (flags & ALT_FORM3) {
  306. SLJIT_ASSERT(src2 == TMP_REG2);
  307. FAIL_IF(push_inst(compiler, ORI | S(src1) | A(dst) | IMM(compiler->imm)));
  308. return push_inst(compiler, ORIS | S(dst) | A(dst) | IMM(compiler->imm >> 16));
  309. }
  310. return push_inst(compiler, OR | RC(flags) | S(src1) | A(dst) | B(src2));
  311. case SLJIT_XOR:
  312. if (flags & ALT_FORM1) {
  313. SLJIT_ASSERT(src2 == TMP_REG2);
  314. return push_inst(compiler, XORI | S(src1) | A(dst) | compiler->imm);
  315. }
  316. if (flags & ALT_FORM2) {
  317. SLJIT_ASSERT(src2 == TMP_REG2);
  318. return push_inst(compiler, XORIS | S(src1) | A(dst) | compiler->imm);
  319. }
  320. if (flags & ALT_FORM3) {
  321. SLJIT_ASSERT(src2 == TMP_REG2);
  322. FAIL_IF(push_inst(compiler, XORI | S(src1) | A(dst) | IMM(compiler->imm)));
  323. return push_inst(compiler, XORIS | S(dst) | A(dst) | IMM(compiler->imm >> 16));
  324. }
  325. return push_inst(compiler, XOR | RC(flags) | S(src1) | A(dst) | B(src2));
  326. case SLJIT_SHL:
  327. if (flags & ALT_FORM1) {
  328. SLJIT_ASSERT(src2 == TMP_REG2);
  329. if (flags & ALT_FORM2) {
  330. compiler->imm &= 0x1f;
  331. return push_inst(compiler, RLWINM | RC(flags) | S(src1) | A(dst) | (compiler->imm << 11) | ((31 - compiler->imm) << 1));
  332. }
  333. compiler->imm &= 0x3f;
  334. return push_inst(compiler, RLDI(dst, src1, compiler->imm, 63 - compiler->imm, 1) | RC(flags));
  335. }
  336. return push_inst(compiler, ((flags & ALT_FORM2) ? SLW : SLD) | RC(flags) | S(src1) | A(dst) | B(src2));
  337. case SLJIT_LSHR:
  338. if (flags & ALT_FORM1) {
  339. SLJIT_ASSERT(src2 == TMP_REG2);
  340. if (flags & ALT_FORM2) {
  341. compiler->imm &= 0x1f;
  342. return push_inst(compiler, RLWINM | RC(flags) | S(src1) | A(dst) | (((32 - compiler->imm) & 0x1f) << 11) | (compiler->imm << 6) | (31 << 1));
  343. }
  344. compiler->imm &= 0x3f;
  345. return push_inst(compiler, RLDI(dst, src1, 64 - compiler->imm, compiler->imm, 0) | RC(flags));
  346. }
  347. return push_inst(compiler, ((flags & ALT_FORM2) ? SRW : SRD) | RC(flags) | S(src1) | A(dst) | B(src2));
  348. case SLJIT_ASHR:
  349. if (flags & ALT_FORM1) {
  350. SLJIT_ASSERT(src2 == TMP_REG2);
  351. if (flags & ALT_FORM2) {
  352. compiler->imm &= 0x1f;
  353. return push_inst(compiler, SRAWI | RC(flags) | S(src1) | A(dst) | (compiler->imm << 11));
  354. }
  355. compiler->imm &= 0x3f;
  356. return push_inst(compiler, SRADI | RC(flags) | S(src1) | A(dst) | ((compiler->imm & 0x1f) << 11) | ((compiler->imm & 0x20) >> 4));
  357. }
  358. return push_inst(compiler, ((flags & ALT_FORM2) ? SRAW : SRAD) | RC(flags) | S(src1) | A(dst) | B(src2));
  359. }
  360. SLJIT_UNREACHABLE();
  361. return SLJIT_SUCCESS;
  362. }
  363. static sljit_s32 call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src)
  364. {
  365. sljit_s32 arg_count = 0;
  366. sljit_s32 word_arg_count = 0;
  367. sljit_s32 types = 0;
  368. sljit_s32 reg = 0;
  369. if (src)
  370. reg = *src & REG_MASK;
  371. arg_types >>= SLJIT_DEF_SHIFT;
  372. while (arg_types) {
  373. types = (types << SLJIT_DEF_SHIFT) | (arg_types & SLJIT_DEF_MASK);
  374. switch (arg_types & SLJIT_DEF_MASK) {
  375. case SLJIT_ARG_TYPE_F32:
  376. case SLJIT_ARG_TYPE_F64:
  377. arg_count++;
  378. break;
  379. default:
  380. arg_count++;
  381. word_arg_count++;
  382. if (arg_count != word_arg_count && arg_count == reg) {
  383. FAIL_IF(push_inst(compiler, OR | S(reg) | A(TMP_CALL_REG) | B(reg)));
  384. *src = TMP_CALL_REG;
  385. }
  386. break;
  387. }
  388. arg_types >>= SLJIT_DEF_SHIFT;
  389. }
  390. while (types) {
  391. switch (types & SLJIT_DEF_MASK) {
  392. case SLJIT_ARG_TYPE_F32:
  393. case SLJIT_ARG_TYPE_F64:
  394. arg_count--;
  395. break;
  396. default:
  397. if (arg_count != word_arg_count)
  398. FAIL_IF(push_inst(compiler, OR | S(word_arg_count) | A(arg_count) | B(word_arg_count)));
  399. arg_count--;
  400. word_arg_count--;
  401. break;
  402. }
  403. types >>= SLJIT_DEF_SHIFT;
  404. }
  405. return SLJIT_SUCCESS;
  406. }
  407. static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 reg, sljit_sw init_value)
  408. {
  409. FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(init_value >> 48)));
  410. FAIL_IF(push_inst(compiler, ORI | S(reg) | A(reg) | IMM(init_value >> 32)));
  411. FAIL_IF(PUSH_RLDICR(reg, 31));
  412. FAIL_IF(push_inst(compiler, ORIS | S(reg) | A(reg) | IMM(init_value >> 16)));
  413. return push_inst(compiler, ORI | S(reg) | A(reg) | IMM(init_value));
  414. }
  415. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
  416. {
  417. sljit_ins *inst = (sljit_ins*)addr;
  418. inst[0] = (inst[0] & 0xffff0000) | ((new_target >> 48) & 0xffff);
  419. inst[1] = (inst[1] & 0xffff0000) | ((new_target >> 32) & 0xffff);
  420. inst[3] = (inst[3] & 0xffff0000) | ((new_target >> 16) & 0xffff);
  421. inst[4] = (inst[4] & 0xffff0000) | (new_target & 0xffff);
  422. inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  423. SLJIT_CACHE_FLUSH(inst, inst + 5);
  424. }
  425. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
  426. {
  427. sljit_ins *inst = (sljit_ins*)addr;
  428. inst[0] = (inst[0] & 0xffff0000) | ((new_constant >> 48) & 0xffff);
  429. inst[1] = (inst[1] & 0xffff0000) | ((new_constant >> 32) & 0xffff);
  430. inst[3] = (inst[3] & 0xffff0000) | ((new_constant >> 16) & 0xffff);
  431. inst[4] = (inst[4] & 0xffff0000) | (new_constant & 0xffff);
  432. inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  433. SLJIT_CACHE_FLUSH(inst, inst + 5);
  434. }