sljitNativeX86_32.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * Stack-less Just-In-Time compiler
  3. *
  4. * Copyright 2009-2012 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. /* x86 32-bit arch dependent functions. */
  27. static sljit_si emit_do_imm(struct sljit_compiler *compiler, sljit_ub opcode, sljit_sw imm)
  28. {
  29. sljit_ub *inst;
  30. inst = (sljit_ub*)ensure_buf(compiler, 1 + 1 + sizeof(sljit_sw));
  31. FAIL_IF(!inst);
  32. INC_SIZE(1 + sizeof(sljit_sw));
  33. *inst++ = opcode;
  34. *(sljit_sw*)inst = imm;
  35. return SLJIT_SUCCESS;
  36. }
  37. static sljit_ub* generate_far_jump_code(struct sljit_jump *jump, sljit_ub *code_ptr, sljit_si type)
  38. {
  39. if (type == SLJIT_JUMP) {
  40. *code_ptr++ = JMP_i32;
  41. jump->addr++;
  42. }
  43. else if (type >= SLJIT_FAST_CALL) {
  44. *code_ptr++ = CALL_i32;
  45. jump->addr++;
  46. }
  47. else {
  48. *code_ptr++ = GROUP_0F;
  49. *code_ptr++ = get_jump_code(type);
  50. jump->addr += 2;
  51. }
  52. if (jump->flags & JUMP_LABEL)
  53. jump->flags |= PATCH_MW;
  54. else
  55. *(sljit_sw*)code_ptr = jump->u.target - (jump->addr + 4);
  56. code_ptr += 4;
  57. return code_ptr;
  58. }
  59. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_enter(struct sljit_compiler *compiler,
  60. sljit_si options, sljit_si args, sljit_si scratches, sljit_si saveds,
  61. sljit_si fscratches, sljit_si fsaveds, sljit_si local_size)
  62. {
  63. sljit_si size;
  64. sljit_ub *inst;
  65. CHECK_ERROR();
  66. CHECK(check_sljit_emit_enter(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size));
  67. set_emit_enter(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size);
  68. compiler->args = args;
  69. compiler->flags_saved = 0;
  70. size = 1 + (scratches > 7 ? (scratches - 7) : 0) + (saveds <= 3 ? saveds : 3);
  71. #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  72. size += (args > 0 ? (args * 2) : 0) + (args > 2 ? 2 : 0);
  73. #else
  74. size += (args > 0 ? (2 + args * 3) : 0);
  75. #endif
  76. inst = (sljit_ub*)ensure_buf(compiler, 1 + size);
  77. FAIL_IF(!inst);
  78. INC_SIZE(size);
  79. PUSH_REG(reg_map[TMP_REG1]);
  80. #if !(defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  81. if (args > 0) {
  82. *inst++ = MOV_r_rm;
  83. *inst++ = MOD_REG | (reg_map[TMP_REG1] << 3) | 0x4 /* esp */;
  84. }
  85. #endif
  86. if (saveds > 2 || scratches > 7)
  87. PUSH_REG(reg_map[SLJIT_S2]);
  88. if (saveds > 1 || scratches > 8)
  89. PUSH_REG(reg_map[SLJIT_S1]);
  90. if (saveds > 0 || scratches > 9)
  91. PUSH_REG(reg_map[SLJIT_S0]);
  92. #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  93. if (args > 0) {
  94. *inst++ = MOV_r_rm;
  95. *inst++ = MOD_REG | (reg_map[SLJIT_S0] << 3) | reg_map[SLJIT_R2];
  96. }
  97. if (args > 1) {
  98. *inst++ = MOV_r_rm;
  99. *inst++ = MOD_REG | (reg_map[SLJIT_S1] << 3) | reg_map[SLJIT_R1];
  100. }
  101. if (args > 2) {
  102. *inst++ = MOV_r_rm;
  103. *inst++ = MOD_DISP8 | (reg_map[SLJIT_S2] << 3) | 0x4 /* esp */;
  104. *inst++ = 0x24;
  105. *inst++ = sizeof(sljit_sw) * (3 + 2); /* saveds >= 3 as well. */
  106. }
  107. #else
  108. if (args > 0) {
  109. *inst++ = MOV_r_rm;
  110. *inst++ = MOD_DISP8 | (reg_map[SLJIT_S0] << 3) | reg_map[TMP_REG1];
  111. *inst++ = sizeof(sljit_sw) * 2;
  112. }
  113. if (args > 1) {
  114. *inst++ = MOV_r_rm;
  115. *inst++ = MOD_DISP8 | (reg_map[SLJIT_S1] << 3) | reg_map[TMP_REG1];
  116. *inst++ = sizeof(sljit_sw) * 3;
  117. }
  118. if (args > 2) {
  119. *inst++ = MOV_r_rm;
  120. *inst++ = MOD_DISP8 | (reg_map[SLJIT_S2] << 3) | reg_map[TMP_REG1];
  121. *inst++ = sizeof(sljit_sw) * 4;
  122. }
  123. #endif
  124. SLJIT_COMPILE_ASSERT(SLJIT_LOCALS_OFFSET >= (2 + 4) * sizeof(sljit_uw), require_at_least_two_words);
  125. #if defined(__APPLE__)
  126. /* Ignore pushed registers and SLJIT_LOCALS_OFFSET when computing the aligned local size. */
  127. saveds = (2 + (scratches > 7 ? (scratches - 7) : 0) + (saveds <= 3 ? saveds : 3)) * sizeof(sljit_uw);
  128. local_size = ((SLJIT_LOCALS_OFFSET + saveds + local_size + 15) & ~15) - saveds;
  129. #else
  130. if (options & SLJIT_DOUBLE_ALIGNMENT) {
  131. local_size = SLJIT_LOCALS_OFFSET + ((local_size + 7) & ~7);
  132. inst = (sljit_ub*)ensure_buf(compiler, 1 + 17);
  133. FAIL_IF(!inst);
  134. INC_SIZE(17);
  135. inst[0] = MOV_r_rm;
  136. inst[1] = MOD_REG | (reg_map[TMP_REG1] << 3) | reg_map[SLJIT_SP];
  137. inst[2] = GROUP_F7;
  138. inst[3] = MOD_REG | (0 << 3) | reg_map[SLJIT_SP];
  139. *(sljit_sw*)(inst + 4) = 0x4;
  140. inst[8] = JNE_i8;
  141. inst[9] = 6;
  142. inst[10] = GROUP_BINARY_81;
  143. inst[11] = MOD_REG | (5 << 3) | reg_map[SLJIT_SP];
  144. *(sljit_sw*)(inst + 12) = 0x4;
  145. inst[16] = PUSH_r + reg_map[TMP_REG1];
  146. }
  147. else
  148. local_size = SLJIT_LOCALS_OFFSET + ((local_size + 3) & ~3);
  149. #endif
  150. compiler->local_size = local_size;
  151. #ifdef _WIN32
  152. if (local_size > 1024) {
  153. #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  154. FAIL_IF(emit_do_imm(compiler, MOV_r_i32 + reg_map[SLJIT_R0], local_size));
  155. #else
  156. local_size -= SLJIT_LOCALS_OFFSET;
  157. FAIL_IF(emit_do_imm(compiler, MOV_r_i32 + reg_map[SLJIT_R0], local_size));
  158. FAIL_IF(emit_non_cum_binary(compiler, SUB_r_rm, SUB_rm_r, SUB, SUB_EAX_i32,
  159. SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, SLJIT_LOCALS_OFFSET));
  160. #endif
  161. FAIL_IF(sljit_emit_ijump(compiler, SLJIT_CALL1, SLJIT_IMM, SLJIT_FUNC_OFFSET(sljit_grow_stack)));
  162. }
  163. #endif
  164. SLJIT_ASSERT(local_size > 0);
  165. return emit_non_cum_binary(compiler, SUB_r_rm, SUB_rm_r, SUB, SUB_EAX_i32,
  166. SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, local_size);
  167. }
  168. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_set_context(struct sljit_compiler *compiler,
  169. sljit_si options, sljit_si args, sljit_si scratches, sljit_si saveds,
  170. sljit_si fscratches, sljit_si fsaveds, sljit_si local_size)
  171. {
  172. CHECK_ERROR();
  173. CHECK(check_sljit_set_context(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size));
  174. set_set_context(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size);
  175. compiler->args = args;
  176. #if defined(__APPLE__)
  177. saveds = (2 + (scratches > 7 ? (scratches - 7) : 0) + (saveds <= 3 ? saveds : 3)) * sizeof(sljit_uw);
  178. compiler->local_size = ((SLJIT_LOCALS_OFFSET + saveds + local_size + 15) & ~15) - saveds;
  179. #else
  180. if (options & SLJIT_DOUBLE_ALIGNMENT)
  181. compiler->local_size = SLJIT_LOCALS_OFFSET + ((local_size + 7) & ~7);
  182. else
  183. compiler->local_size = SLJIT_LOCALS_OFFSET + ((local_size + 3) & ~3);
  184. #endif
  185. return SLJIT_SUCCESS;
  186. }
  187. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
  188. {
  189. sljit_si size;
  190. sljit_ub *inst;
  191. CHECK_ERROR();
  192. CHECK(check_sljit_emit_return(compiler, op, src, srcw));
  193. SLJIT_ASSERT(compiler->args >= 0);
  194. compiler->flags_saved = 0;
  195. FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
  196. SLJIT_ASSERT(compiler->local_size > 0);
  197. FAIL_IF(emit_cum_binary(compiler, ADD_r_rm, ADD_rm_r, ADD, ADD_EAX_i32,
  198. SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, compiler->local_size));
  199. #if !defined(__APPLE__)
  200. if (compiler->options & SLJIT_DOUBLE_ALIGNMENT) {
  201. inst = (sljit_ub*)ensure_buf(compiler, 1 + 3);
  202. FAIL_IF(!inst);
  203. INC_SIZE(3);
  204. inst[0] = MOV_r_rm;
  205. inst[1] = (reg_map[SLJIT_SP] << 3) | 0x4 /* SIB */;
  206. inst[2] = (4 << 3) | reg_map[SLJIT_SP];
  207. }
  208. #endif
  209. size = 2 + (compiler->scratches > 7 ? (compiler->scratches - 7) : 0) +
  210. (compiler->saveds <= 3 ? compiler->saveds : 3);
  211. #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  212. if (compiler->args > 2)
  213. size += 2;
  214. #else
  215. if (compiler->args > 0)
  216. size += 2;
  217. #endif
  218. inst = (sljit_ub*)ensure_buf(compiler, 1 + size);
  219. FAIL_IF(!inst);
  220. INC_SIZE(size);
  221. if (compiler->saveds > 0 || compiler->scratches > 9)
  222. POP_REG(reg_map[SLJIT_S0]);
  223. if (compiler->saveds > 1 || compiler->scratches > 8)
  224. POP_REG(reg_map[SLJIT_S1]);
  225. if (compiler->saveds > 2 || compiler->scratches > 7)
  226. POP_REG(reg_map[SLJIT_S2]);
  227. POP_REG(reg_map[TMP_REG1]);
  228. #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  229. if (compiler->args > 2)
  230. RET_I16(sizeof(sljit_sw));
  231. else
  232. RET();
  233. #else
  234. RET();
  235. #endif
  236. return SLJIT_SUCCESS;
  237. }
  238. /* --------------------------------------------------------------------- */
  239. /* Operators */
  240. /* --------------------------------------------------------------------- */
  241. /* Size contains the flags as well. */
  242. static sljit_ub* emit_x86_instruction(struct sljit_compiler *compiler, sljit_si size,
  243. /* The register or immediate operand. */
  244. sljit_si a, sljit_sw imma,
  245. /* The general operand (not immediate). */
  246. sljit_si b, sljit_sw immb)
  247. {
  248. sljit_ub *inst;
  249. sljit_ub *buf_ptr;
  250. sljit_si flags = size & ~0xf;
  251. sljit_si inst_size;
  252. /* Both cannot be switched on. */
  253. SLJIT_ASSERT((flags & (EX86_BIN_INS | EX86_SHIFT_INS)) != (EX86_BIN_INS | EX86_SHIFT_INS));
  254. /* Size flags not allowed for typed instructions. */
  255. SLJIT_ASSERT(!(flags & (EX86_BIN_INS | EX86_SHIFT_INS)) || (flags & (EX86_BYTE_ARG | EX86_HALF_ARG)) == 0);
  256. /* Both size flags cannot be switched on. */
  257. SLJIT_ASSERT((flags & (EX86_BYTE_ARG | EX86_HALF_ARG)) != (EX86_BYTE_ARG | EX86_HALF_ARG));
  258. /* SSE2 and immediate is not possible. */
  259. SLJIT_ASSERT(!(a & SLJIT_IMM) || !(flags & EX86_SSE2));
  260. SLJIT_ASSERT((flags & (EX86_PREF_F2 | EX86_PREF_F3)) != (EX86_PREF_F2 | EX86_PREF_F3)
  261. && (flags & (EX86_PREF_F2 | EX86_PREF_66)) != (EX86_PREF_F2 | EX86_PREF_66)
  262. && (flags & (EX86_PREF_F3 | EX86_PREF_66)) != (EX86_PREF_F3 | EX86_PREF_66));
  263. size &= 0xf;
  264. inst_size = size;
  265. if (flags & (EX86_PREF_F2 | EX86_PREF_F3))
  266. inst_size++;
  267. if (flags & EX86_PREF_66)
  268. inst_size++;
  269. /* Calculate size of b. */
  270. inst_size += 1; /* mod r/m byte. */
  271. if (b & SLJIT_MEM) {
  272. if ((b & REG_MASK) == SLJIT_UNUSED)
  273. inst_size += sizeof(sljit_sw);
  274. else if (immb != 0 && !(b & OFFS_REG_MASK)) {
  275. /* Immediate operand. */
  276. if (immb <= 127 && immb >= -128)
  277. inst_size += sizeof(sljit_sb);
  278. else
  279. inst_size += sizeof(sljit_sw);
  280. }
  281. if ((b & REG_MASK) == SLJIT_SP && !(b & OFFS_REG_MASK))
  282. b |= TO_OFFS_REG(SLJIT_SP);
  283. if ((b & OFFS_REG_MASK) != SLJIT_UNUSED)
  284. inst_size += 1; /* SIB byte. */
  285. }
  286. /* Calculate size of a. */
  287. if (a & SLJIT_IMM) {
  288. if (flags & EX86_BIN_INS) {
  289. if (imma <= 127 && imma >= -128) {
  290. inst_size += 1;
  291. flags |= EX86_BYTE_ARG;
  292. } else
  293. inst_size += 4;
  294. }
  295. else if (flags & EX86_SHIFT_INS) {
  296. imma &= 0x1f;
  297. if (imma != 1) {
  298. inst_size ++;
  299. flags |= EX86_BYTE_ARG;
  300. }
  301. } else if (flags & EX86_BYTE_ARG)
  302. inst_size++;
  303. else if (flags & EX86_HALF_ARG)
  304. inst_size += sizeof(short);
  305. else
  306. inst_size += sizeof(sljit_sw);
  307. }
  308. else
  309. SLJIT_ASSERT(!(flags & EX86_SHIFT_INS) || a == SLJIT_PREF_SHIFT_REG);
  310. inst = (sljit_ub*)ensure_buf(compiler, 1 + inst_size);
  311. PTR_FAIL_IF(!inst);
  312. /* Encoding the byte. */
  313. INC_SIZE(inst_size);
  314. if (flags & EX86_PREF_F2)
  315. *inst++ = 0xf2;
  316. if (flags & EX86_PREF_F3)
  317. *inst++ = 0xf3;
  318. if (flags & EX86_PREF_66)
  319. *inst++ = 0x66;
  320. buf_ptr = inst + size;
  321. /* Encode mod/rm byte. */
  322. if (!(flags & EX86_SHIFT_INS)) {
  323. if ((flags & EX86_BIN_INS) && (a & SLJIT_IMM))
  324. *inst = (flags & EX86_BYTE_ARG) ? GROUP_BINARY_83 : GROUP_BINARY_81;
  325. if ((a & SLJIT_IMM) || (a == 0))
  326. *buf_ptr = 0;
  327. else if (!(flags & EX86_SSE2_OP1))
  328. *buf_ptr = reg_map[a] << 3;
  329. else
  330. *buf_ptr = a << 3;
  331. }
  332. else {
  333. if (a & SLJIT_IMM) {
  334. if (imma == 1)
  335. *inst = GROUP_SHIFT_1;
  336. else
  337. *inst = GROUP_SHIFT_N;
  338. } else
  339. *inst = GROUP_SHIFT_CL;
  340. *buf_ptr = 0;
  341. }
  342. if (!(b & SLJIT_MEM))
  343. *buf_ptr++ |= MOD_REG + ((!(flags & EX86_SSE2_OP2)) ? reg_map[b] : b);
  344. else if ((b & REG_MASK) != SLJIT_UNUSED) {
  345. if ((b & OFFS_REG_MASK) == SLJIT_UNUSED || (b & OFFS_REG_MASK) == TO_OFFS_REG(SLJIT_SP)) {
  346. if (immb != 0) {
  347. if (immb <= 127 && immb >= -128)
  348. *buf_ptr |= 0x40;
  349. else
  350. *buf_ptr |= 0x80;
  351. }
  352. if ((b & OFFS_REG_MASK) == SLJIT_UNUSED)
  353. *buf_ptr++ |= reg_map[b & REG_MASK];
  354. else {
  355. *buf_ptr++ |= 0x04;
  356. *buf_ptr++ = reg_map[b & REG_MASK] | (reg_map[OFFS_REG(b)] << 3);
  357. }
  358. if (immb != 0) {
  359. if (immb <= 127 && immb >= -128)
  360. *buf_ptr++ = immb; /* 8 bit displacement. */
  361. else {
  362. *(sljit_sw*)buf_ptr = immb; /* 32 bit displacement. */
  363. buf_ptr += sizeof(sljit_sw);
  364. }
  365. }
  366. }
  367. else {
  368. *buf_ptr++ |= 0x04;
  369. *buf_ptr++ = reg_map[b & REG_MASK] | (reg_map[OFFS_REG(b)] << 3) | (immb << 6);
  370. }
  371. }
  372. else {
  373. *buf_ptr++ |= 0x05;
  374. *(sljit_sw*)buf_ptr = immb; /* 32 bit displacement. */
  375. buf_ptr += sizeof(sljit_sw);
  376. }
  377. if (a & SLJIT_IMM) {
  378. if (flags & EX86_BYTE_ARG)
  379. *buf_ptr = imma;
  380. else if (flags & EX86_HALF_ARG)
  381. *(short*)buf_ptr = imma;
  382. else if (!(flags & EX86_SHIFT_INS))
  383. *(sljit_sw*)buf_ptr = imma;
  384. }
  385. return !(flags & EX86_SHIFT_INS) ? inst : (inst + 1);
  386. }
  387. /* --------------------------------------------------------------------- */
  388. /* Call / return instructions */
  389. /* --------------------------------------------------------------------- */
  390. static SLJIT_INLINE sljit_si call_with_args(struct sljit_compiler *compiler, sljit_si type)
  391. {
  392. sljit_ub *inst;
  393. #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  394. inst = (sljit_ub*)ensure_buf(compiler, type >= SLJIT_CALL3 ? 1 + 2 + 1 : 1 + 2);
  395. FAIL_IF(!inst);
  396. INC_SIZE(type >= SLJIT_CALL3 ? 2 + 1 : 2);
  397. if (type >= SLJIT_CALL3)
  398. PUSH_REG(reg_map[SLJIT_R2]);
  399. *inst++ = MOV_r_rm;
  400. *inst++ = MOD_REG | (reg_map[SLJIT_R2] << 3) | reg_map[SLJIT_R0];
  401. #else
  402. inst = (sljit_ub*)ensure_buf(compiler, 1 + 4 * (type - SLJIT_CALL0));
  403. FAIL_IF(!inst);
  404. INC_SIZE(4 * (type - SLJIT_CALL0));
  405. *inst++ = MOV_rm_r;
  406. *inst++ = MOD_DISP8 | (reg_map[SLJIT_R0] << 3) | 0x4 /* SIB */;
  407. *inst++ = (0x4 /* none*/ << 3) | reg_map[SLJIT_SP];
  408. *inst++ = 0;
  409. if (type >= SLJIT_CALL2) {
  410. *inst++ = MOV_rm_r;
  411. *inst++ = MOD_DISP8 | (reg_map[SLJIT_R1] << 3) | 0x4 /* SIB */;
  412. *inst++ = (0x4 /* none*/ << 3) | reg_map[SLJIT_SP];
  413. *inst++ = sizeof(sljit_sw);
  414. }
  415. if (type >= SLJIT_CALL3) {
  416. *inst++ = MOV_rm_r;
  417. *inst++ = MOD_DISP8 | (reg_map[SLJIT_R2] << 3) | 0x4 /* SIB */;
  418. *inst++ = (0x4 /* none*/ << 3) | reg_map[SLJIT_SP];
  419. *inst++ = 2 * sizeof(sljit_sw);
  420. }
  421. #endif
  422. return SLJIT_SUCCESS;
  423. }
  424. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
  425. {
  426. sljit_ub *inst;
  427. CHECK_ERROR();
  428. CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
  429. ADJUST_LOCAL_OFFSET(dst, dstw);
  430. CHECK_EXTRA_REGS(dst, dstw, (void)0);
  431. /* For UNUSED dst. Uncommon, but possible. */
  432. if (dst == SLJIT_UNUSED)
  433. dst = TMP_REG1;
  434. if (FAST_IS_REG(dst)) {
  435. /* Unused dest is possible here. */
  436. inst = (sljit_ub*)ensure_buf(compiler, 1 + 1);
  437. FAIL_IF(!inst);
  438. INC_SIZE(1);
  439. POP_REG(reg_map[dst]);
  440. return SLJIT_SUCCESS;
  441. }
  442. /* Memory. */
  443. inst = emit_x86_instruction(compiler, 1, 0, 0, dst, dstw);
  444. FAIL_IF(!inst);
  445. *inst++ = POP_rm;
  446. return SLJIT_SUCCESS;
  447. }
  448. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
  449. {
  450. sljit_ub *inst;
  451. CHECK_ERROR();
  452. CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
  453. ADJUST_LOCAL_OFFSET(src, srcw);
  454. CHECK_EXTRA_REGS(src, srcw, (void)0);
  455. if (FAST_IS_REG(src)) {
  456. inst = (sljit_ub*)ensure_buf(compiler, 1 + 1 + 1);
  457. FAIL_IF(!inst);
  458. INC_SIZE(1 + 1);
  459. PUSH_REG(reg_map[src]);
  460. }
  461. else if (src & SLJIT_MEM) {
  462. inst = emit_x86_instruction(compiler, 1, 0, 0, src, srcw);
  463. FAIL_IF(!inst);
  464. *inst++ = GROUP_FF;
  465. *inst |= PUSH_rm;
  466. inst = (sljit_ub*)ensure_buf(compiler, 1 + 1);
  467. FAIL_IF(!inst);
  468. INC_SIZE(1);
  469. }
  470. else {
  471. /* SLJIT_IMM. */
  472. inst = (sljit_ub*)ensure_buf(compiler, 1 + 5 + 1);
  473. FAIL_IF(!inst);
  474. INC_SIZE(5 + 1);
  475. *inst++ = PUSH_i32;
  476. *(sljit_sw*)inst = srcw;
  477. inst += sizeof(sljit_sw);
  478. }
  479. RET();
  480. return SLJIT_SUCCESS;
  481. }