sljitNativeX86_32.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. /* x86 32-bit arch dependent functions. */
  27. static sljit_s32 emit_do_imm(struct sljit_compiler *compiler, sljit_u8 opcode, sljit_sw imm)
  28. {
  29. sljit_u8 *inst;
  30. inst = (sljit_u8*)ensure_buf(compiler, 1 + 1 + sizeof(sljit_sw));
  31. FAIL_IF(!inst);
  32. INC_SIZE(1 + sizeof(sljit_sw));
  33. *inst++ = opcode;
  34. sljit_unaligned_store_sw(inst, imm);
  35. return SLJIT_SUCCESS;
  36. }
  37. static sljit_u8* generate_far_jump_code(struct sljit_jump *jump, sljit_u8 *code_ptr, sljit_s32 type, sljit_sw executable_offset)
  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_unaligned_store_sw(code_ptr, jump->u.target - (jump->addr + 4) - (sljit_uw)executable_offset);
  56. code_ptr += 4;
  57. return code_ptr;
  58. }
  59. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
  60. sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
  61. sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
  62. {
  63. sljit_s32 args, size;
  64. sljit_u8 *inst;
  65. CHECK_ERROR();
  66. CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
  67. set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
  68. args = get_arg_count(arg_types);
  69. compiler->args = args;
  70. /* [esp+0] for saving temporaries and function calls. */
  71. compiler->stack_tmp_size = 2 * sizeof(sljit_sw);
  72. #if !(defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  73. if (scratches > 3)
  74. compiler->stack_tmp_size = 3 * sizeof(sljit_sw);
  75. #endif
  76. compiler->saveds_offset = compiler->stack_tmp_size;
  77. if (scratches > 3)
  78. compiler->saveds_offset += ((scratches > (3 + 6)) ? 6 : (scratches - 3)) * sizeof(sljit_sw);
  79. compiler->locals_offset = compiler->saveds_offset;
  80. if (saveds > 3)
  81. compiler->locals_offset += (saveds - 3) * sizeof(sljit_sw);
  82. if (options & SLJIT_F64_ALIGNMENT)
  83. compiler->locals_offset = (compiler->locals_offset + sizeof(sljit_f64) - 1) & ~(sizeof(sljit_f64) - 1);
  84. size = 1 + (scratches > 9 ? (scratches - 9) : 0) + (saveds <= 3 ? saveds : 3);
  85. #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  86. size += (args > 0 ? (args * 2) : 0) + (args > 2 ? 2 : 0);
  87. #else
  88. size += (args > 0 ? (2 + args * 3) : 0);
  89. #endif
  90. inst = (sljit_u8*)ensure_buf(compiler, 1 + size);
  91. FAIL_IF(!inst);
  92. INC_SIZE(size);
  93. PUSH_REG(reg_map[TMP_REG1]);
  94. #if !(defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  95. if (args > 0) {
  96. *inst++ = MOV_r_rm;
  97. *inst++ = MOD_REG | (reg_map[TMP_REG1] << 3) | 0x4 /* esp */;
  98. }
  99. #endif
  100. if (saveds > 2 || scratches > 9)
  101. PUSH_REG(reg_map[SLJIT_S2]);
  102. if (saveds > 1 || scratches > 10)
  103. PUSH_REG(reg_map[SLJIT_S1]);
  104. if (saveds > 0 || scratches > 11)
  105. PUSH_REG(reg_map[SLJIT_S0]);
  106. #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  107. if (args > 0) {
  108. inst[0] = MOV_r_rm;
  109. inst[1] = MOD_REG | (reg_map[SLJIT_S0] << 3) | reg_map[SLJIT_R2];
  110. inst += 2;
  111. }
  112. if (args > 1) {
  113. inst[0] = MOV_r_rm;
  114. inst[1] = MOD_REG | (reg_map[SLJIT_S1] << 3) | reg_map[SLJIT_R1];
  115. inst += 2;
  116. }
  117. if (args > 2) {
  118. inst[0] = MOV_r_rm;
  119. inst[1] = MOD_DISP8 | (reg_map[SLJIT_S2] << 3) | 0x4 /* esp */;
  120. inst[2] = 0x24;
  121. inst[3] = sizeof(sljit_sw) * (3 + 2); /* saveds >= 3 as well. */
  122. }
  123. #else
  124. if (args > 0) {
  125. inst[0] = MOV_r_rm;
  126. inst[1] = MOD_DISP8 | (reg_map[SLJIT_S0] << 3) | reg_map[TMP_REG1];
  127. inst[2] = sizeof(sljit_sw) * 2;
  128. inst += 3;
  129. }
  130. if (args > 1) {
  131. inst[0] = MOV_r_rm;
  132. inst[1] = MOD_DISP8 | (reg_map[SLJIT_S1] << 3) | reg_map[TMP_REG1];
  133. inst[2] = sizeof(sljit_sw) * 3;
  134. inst += 3;
  135. }
  136. if (args > 2) {
  137. inst[0] = MOV_r_rm;
  138. inst[1] = MOD_DISP8 | (reg_map[SLJIT_S2] << 3) | reg_map[TMP_REG1];
  139. inst[2] = sizeof(sljit_sw) * 4;
  140. }
  141. #endif
  142. SLJIT_ASSERT(SLJIT_LOCALS_OFFSET > 0);
  143. #if defined(__APPLE__)
  144. /* Ignore pushed registers and SLJIT_LOCALS_OFFSET when computing the aligned local size. */
  145. saveds = (2 + (scratches > 9 ? (scratches - 9) : 0) + (saveds <= 3 ? saveds : 3)) * sizeof(sljit_uw);
  146. local_size = ((SLJIT_LOCALS_OFFSET + saveds + local_size + 15) & ~15) - saveds;
  147. #else
  148. if (options & SLJIT_F64_ALIGNMENT)
  149. local_size = SLJIT_LOCALS_OFFSET + ((local_size + sizeof(sljit_f64) - 1) & ~(sizeof(sljit_f64) - 1));
  150. else
  151. local_size = SLJIT_LOCALS_OFFSET + ((local_size + sizeof(sljit_sw) - 1) & ~(sizeof(sljit_sw) - 1));
  152. #endif
  153. compiler->local_size = local_size;
  154. #ifdef _WIN32
  155. if (local_size > 0) {
  156. if (local_size <= 4 * 4096) {
  157. if (local_size > 4096)
  158. EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), -4096);
  159. if (local_size > 2 * 4096)
  160. EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), -4096 * 2);
  161. if (local_size > 3 * 4096)
  162. EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), -4096 * 3);
  163. }
  164. else {
  165. EMIT_MOV(compiler, SLJIT_R0, 0, SLJIT_SP, 0);
  166. EMIT_MOV(compiler, SLJIT_R1, 0, SLJIT_IMM, (local_size - 1) >> 12);
  167. SLJIT_ASSERT (reg_map[SLJIT_R0] == 0);
  168. EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_R0), -4096);
  169. FAIL_IF(emit_non_cum_binary(compiler, BINARY_OPCODE(SUB),
  170. SLJIT_R0, 0, SLJIT_R0, 0, SLJIT_IMM, 4096));
  171. FAIL_IF(emit_non_cum_binary(compiler, BINARY_OPCODE(SUB),
  172. SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1));
  173. inst = (sljit_u8*)ensure_buf(compiler, 1 + 2);
  174. FAIL_IF(!inst);
  175. INC_SIZE(2);
  176. inst[0] = JNE_i8;
  177. inst[1] = (sljit_s8) -16;
  178. }
  179. EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), -local_size);
  180. }
  181. #endif
  182. SLJIT_ASSERT(local_size > 0);
  183. #if !defined(__APPLE__)
  184. if (options & SLJIT_F64_ALIGNMENT) {
  185. EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_SP, 0);
  186. /* Some space might allocated during sljit_grow_stack() above on WIN32. */
  187. FAIL_IF(emit_non_cum_binary(compiler, BINARY_OPCODE(SUB),
  188. SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, local_size + sizeof(sljit_sw)));
  189. #if defined _WIN32 && !(defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  190. if (compiler->local_size > 1024)
  191. FAIL_IF(emit_cum_binary(compiler, BINARY_OPCODE(ADD),
  192. TMP_REG1, 0, TMP_REG1, 0, SLJIT_IMM, sizeof(sljit_sw)));
  193. #endif
  194. inst = (sljit_u8*)ensure_buf(compiler, 1 + 6);
  195. FAIL_IF(!inst);
  196. INC_SIZE(6);
  197. inst[0] = GROUP_BINARY_81;
  198. inst[1] = MOD_REG | AND | reg_map[SLJIT_SP];
  199. sljit_unaligned_store_sw(inst + 2, ~(sizeof(sljit_f64) - 1));
  200. /* The real local size must be used. */
  201. return emit_mov(compiler, SLJIT_MEM1(SLJIT_SP), compiler->local_size, TMP_REG1, 0);
  202. }
  203. #endif
  204. return emit_non_cum_binary(compiler, BINARY_OPCODE(SUB),
  205. SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, local_size);
  206. }
  207. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
  208. sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
  209. sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
  210. {
  211. CHECK_ERROR();
  212. CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
  213. set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
  214. compiler->args = get_arg_count(arg_types);
  215. /* [esp+0] for saving temporaries and function calls. */
  216. compiler->stack_tmp_size = 2 * sizeof(sljit_sw);
  217. #if !(defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  218. if (scratches > 3)
  219. compiler->stack_tmp_size = 3 * sizeof(sljit_sw);
  220. #endif
  221. compiler->saveds_offset = compiler->stack_tmp_size;
  222. if (scratches > 3)
  223. compiler->saveds_offset += ((scratches > (3 + 6)) ? 6 : (scratches - 3)) * sizeof(sljit_sw);
  224. compiler->locals_offset = compiler->saveds_offset;
  225. if (saveds > 3)
  226. compiler->locals_offset += (saveds - 3) * sizeof(sljit_sw);
  227. if (options & SLJIT_F64_ALIGNMENT)
  228. compiler->locals_offset = (compiler->locals_offset + sizeof(sljit_f64) - 1) & ~(sizeof(sljit_f64) - 1);
  229. #if defined(__APPLE__)
  230. saveds = (2 + (scratches > 9 ? (scratches - 9) : 0) + (saveds <= 3 ? saveds : 3)) * sizeof(sljit_uw);
  231. compiler->local_size = ((SLJIT_LOCALS_OFFSET + saveds + local_size + 15) & ~15) - saveds;
  232. #else
  233. if (options & SLJIT_F64_ALIGNMENT)
  234. compiler->local_size = SLJIT_LOCALS_OFFSET + ((local_size + sizeof(sljit_f64) - 1) & ~(sizeof(sljit_f64) - 1));
  235. else
  236. compiler->local_size = SLJIT_LOCALS_OFFSET + ((local_size + sizeof(sljit_sw) - 1) & ~(sizeof(sljit_sw) - 1));
  237. #endif
  238. return SLJIT_SUCCESS;
  239. }
  240. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
  241. {
  242. sljit_s32 size;
  243. sljit_u8 *inst;
  244. CHECK_ERROR();
  245. CHECK(check_sljit_emit_return(compiler, op, src, srcw));
  246. SLJIT_ASSERT(compiler->args >= 0);
  247. FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
  248. SLJIT_ASSERT(compiler->local_size > 0);
  249. #if !defined(__APPLE__)
  250. if (compiler->options & SLJIT_F64_ALIGNMENT)
  251. EMIT_MOV(compiler, SLJIT_SP, 0, SLJIT_MEM1(SLJIT_SP), compiler->local_size)
  252. else
  253. FAIL_IF(emit_cum_binary(compiler, BINARY_OPCODE(ADD),
  254. SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, compiler->local_size));
  255. #else
  256. FAIL_IF(emit_cum_binary(compiler, BINARY_OPCODE(ADD),
  257. SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, compiler->local_size));
  258. #endif
  259. size = 2 + (compiler->scratches > 7 ? (compiler->scratches - 7) : 0) +
  260. (compiler->saveds <= 3 ? compiler->saveds : 3);
  261. #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  262. if (compiler->args > 2)
  263. size += 2;
  264. #else
  265. if (compiler->args > 0)
  266. size += 2;
  267. #endif
  268. inst = (sljit_u8*)ensure_buf(compiler, 1 + size);
  269. FAIL_IF(!inst);
  270. INC_SIZE(size);
  271. if (compiler->saveds > 0 || compiler->scratches > 11)
  272. POP_REG(reg_map[SLJIT_S0]);
  273. if (compiler->saveds > 1 || compiler->scratches > 10)
  274. POP_REG(reg_map[SLJIT_S1]);
  275. if (compiler->saveds > 2 || compiler->scratches > 9)
  276. POP_REG(reg_map[SLJIT_S2]);
  277. POP_REG(reg_map[TMP_REG1]);
  278. #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  279. if (compiler->args > 2)
  280. RET_I16(sizeof(sljit_sw));
  281. else
  282. RET();
  283. #else
  284. RET();
  285. #endif
  286. return SLJIT_SUCCESS;
  287. }
  288. /* --------------------------------------------------------------------- */
  289. /* Operators */
  290. /* --------------------------------------------------------------------- */
  291. /* Size contains the flags as well. */
  292. static sljit_u8* emit_x86_instruction(struct sljit_compiler *compiler, sljit_s32 size,
  293. /* The register or immediate operand. */
  294. sljit_s32 a, sljit_sw imma,
  295. /* The general operand (not immediate). */
  296. sljit_s32 b, sljit_sw immb)
  297. {
  298. sljit_u8 *inst;
  299. sljit_u8 *buf_ptr;
  300. sljit_s32 flags = size & ~0xf;
  301. sljit_s32 inst_size;
  302. /* Both cannot be switched on. */
  303. SLJIT_ASSERT((flags & (EX86_BIN_INS | EX86_SHIFT_INS)) != (EX86_BIN_INS | EX86_SHIFT_INS));
  304. /* Size flags not allowed for typed instructions. */
  305. SLJIT_ASSERT(!(flags & (EX86_BIN_INS | EX86_SHIFT_INS)) || (flags & (EX86_BYTE_ARG | EX86_HALF_ARG)) == 0);
  306. /* Both size flags cannot be switched on. */
  307. SLJIT_ASSERT((flags & (EX86_BYTE_ARG | EX86_HALF_ARG)) != (EX86_BYTE_ARG | EX86_HALF_ARG));
  308. /* SSE2 and immediate is not possible. */
  309. SLJIT_ASSERT(!(a & SLJIT_IMM) || !(flags & EX86_SSE2));
  310. SLJIT_ASSERT((flags & (EX86_PREF_F2 | EX86_PREF_F3)) != (EX86_PREF_F2 | EX86_PREF_F3)
  311. && (flags & (EX86_PREF_F2 | EX86_PREF_66)) != (EX86_PREF_F2 | EX86_PREF_66)
  312. && (flags & (EX86_PREF_F3 | EX86_PREF_66)) != (EX86_PREF_F3 | EX86_PREF_66));
  313. size &= 0xf;
  314. inst_size = size;
  315. if (flags & (EX86_PREF_F2 | EX86_PREF_F3))
  316. inst_size++;
  317. if (flags & EX86_PREF_66)
  318. inst_size++;
  319. /* Calculate size of b. */
  320. inst_size += 1; /* mod r/m byte. */
  321. if (b & SLJIT_MEM) {
  322. if ((b & REG_MASK) == SLJIT_UNUSED)
  323. inst_size += sizeof(sljit_sw);
  324. else if (immb != 0 && !(b & OFFS_REG_MASK)) {
  325. /* Immediate operand. */
  326. if (immb <= 127 && immb >= -128)
  327. inst_size += sizeof(sljit_s8);
  328. else
  329. inst_size += sizeof(sljit_sw);
  330. }
  331. if ((b & REG_MASK) == SLJIT_SP && !(b & OFFS_REG_MASK))
  332. b |= TO_OFFS_REG(SLJIT_SP);
  333. if ((b & OFFS_REG_MASK) != SLJIT_UNUSED)
  334. inst_size += 1; /* SIB byte. */
  335. }
  336. /* Calculate size of a. */
  337. if (a & SLJIT_IMM) {
  338. if (flags & EX86_BIN_INS) {
  339. if (imma <= 127 && imma >= -128) {
  340. inst_size += 1;
  341. flags |= EX86_BYTE_ARG;
  342. } else
  343. inst_size += 4;
  344. }
  345. else if (flags & EX86_SHIFT_INS) {
  346. imma &= 0x1f;
  347. if (imma != 1) {
  348. inst_size ++;
  349. flags |= EX86_BYTE_ARG;
  350. }
  351. } else if (flags & EX86_BYTE_ARG)
  352. inst_size++;
  353. else if (flags & EX86_HALF_ARG)
  354. inst_size += sizeof(short);
  355. else
  356. inst_size += sizeof(sljit_sw);
  357. }
  358. else
  359. SLJIT_ASSERT(!(flags & EX86_SHIFT_INS) || a == SLJIT_PREF_SHIFT_REG);
  360. inst = (sljit_u8*)ensure_buf(compiler, 1 + inst_size);
  361. PTR_FAIL_IF(!inst);
  362. /* Encoding the byte. */
  363. INC_SIZE(inst_size);
  364. if (flags & EX86_PREF_F2)
  365. *inst++ = 0xf2;
  366. if (flags & EX86_PREF_F3)
  367. *inst++ = 0xf3;
  368. if (flags & EX86_PREF_66)
  369. *inst++ = 0x66;
  370. buf_ptr = inst + size;
  371. /* Encode mod/rm byte. */
  372. if (!(flags & EX86_SHIFT_INS)) {
  373. if ((flags & EX86_BIN_INS) && (a & SLJIT_IMM))
  374. *inst = (flags & EX86_BYTE_ARG) ? GROUP_BINARY_83 : GROUP_BINARY_81;
  375. if (a & SLJIT_IMM)
  376. *buf_ptr = 0;
  377. else if (!(flags & EX86_SSE2_OP1))
  378. *buf_ptr = reg_map[a] << 3;
  379. else
  380. *buf_ptr = a << 3;
  381. }
  382. else {
  383. if (a & SLJIT_IMM) {
  384. if (imma == 1)
  385. *inst = GROUP_SHIFT_1;
  386. else
  387. *inst = GROUP_SHIFT_N;
  388. } else
  389. *inst = GROUP_SHIFT_CL;
  390. *buf_ptr = 0;
  391. }
  392. if (!(b & SLJIT_MEM))
  393. *buf_ptr++ |= MOD_REG + ((!(flags & EX86_SSE2_OP2)) ? reg_map[b] : b);
  394. else if ((b & REG_MASK) != SLJIT_UNUSED) {
  395. if ((b & OFFS_REG_MASK) == SLJIT_UNUSED || (b & OFFS_REG_MASK) == TO_OFFS_REG(SLJIT_SP)) {
  396. if (immb != 0) {
  397. if (immb <= 127 && immb >= -128)
  398. *buf_ptr |= 0x40;
  399. else
  400. *buf_ptr |= 0x80;
  401. }
  402. if ((b & OFFS_REG_MASK) == SLJIT_UNUSED)
  403. *buf_ptr++ |= reg_map[b & REG_MASK];
  404. else {
  405. *buf_ptr++ |= 0x04;
  406. *buf_ptr++ = reg_map[b & REG_MASK] | (reg_map[OFFS_REG(b)] << 3);
  407. }
  408. if (immb != 0) {
  409. if (immb <= 127 && immb >= -128)
  410. *buf_ptr++ = immb; /* 8 bit displacement. */
  411. else {
  412. sljit_unaligned_store_sw(buf_ptr, immb); /* 32 bit displacement. */
  413. buf_ptr += sizeof(sljit_sw);
  414. }
  415. }
  416. }
  417. else {
  418. *buf_ptr++ |= 0x04;
  419. *buf_ptr++ = reg_map[b & REG_MASK] | (reg_map[OFFS_REG(b)] << 3) | (immb << 6);
  420. }
  421. }
  422. else {
  423. *buf_ptr++ |= 0x05;
  424. sljit_unaligned_store_sw(buf_ptr, immb); /* 32 bit displacement. */
  425. buf_ptr += sizeof(sljit_sw);
  426. }
  427. if (a & SLJIT_IMM) {
  428. if (flags & EX86_BYTE_ARG)
  429. *buf_ptr = imma;
  430. else if (flags & EX86_HALF_ARG)
  431. sljit_unaligned_store_s16(buf_ptr, imma);
  432. else if (!(flags & EX86_SHIFT_INS))
  433. sljit_unaligned_store_sw(buf_ptr, imma);
  434. }
  435. return !(flags & EX86_SHIFT_INS) ? inst : (inst + 1);
  436. }
  437. /* --------------------------------------------------------------------- */
  438. /* Call / return instructions */
  439. /* --------------------------------------------------------------------- */
  440. #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  441. static sljit_s32 c_fast_call_get_stack_size(sljit_s32 arg_types, sljit_s32 *word_arg_count_ptr)
  442. {
  443. sljit_s32 stack_size = 0;
  444. sljit_s32 word_arg_count = 0;
  445. arg_types >>= SLJIT_DEF_SHIFT;
  446. while (arg_types) {
  447. switch (arg_types & SLJIT_DEF_MASK) {
  448. case SLJIT_ARG_TYPE_F32:
  449. stack_size += sizeof(sljit_f32);
  450. break;
  451. case SLJIT_ARG_TYPE_F64:
  452. stack_size += sizeof(sljit_f64);
  453. break;
  454. default:
  455. word_arg_count++;
  456. if (word_arg_count > 2)
  457. stack_size += sizeof(sljit_sw);
  458. break;
  459. }
  460. arg_types >>= SLJIT_DEF_SHIFT;
  461. }
  462. if (word_arg_count_ptr)
  463. *word_arg_count_ptr = word_arg_count;
  464. return stack_size;
  465. }
  466. static sljit_s32 c_fast_call_with_args(struct sljit_compiler *compiler,
  467. sljit_s32 arg_types, sljit_s32 stack_size, sljit_s32 word_arg_count, sljit_s32 swap_args)
  468. {
  469. sljit_u8 *inst;
  470. sljit_s32 float_arg_count;
  471. if (stack_size == sizeof(sljit_sw) && word_arg_count == 3) {
  472. inst = (sljit_u8*)ensure_buf(compiler, 1 + 1);
  473. FAIL_IF(!inst);
  474. INC_SIZE(1);
  475. PUSH_REG(reg_map[SLJIT_R2]);
  476. }
  477. else if (stack_size > 0) {
  478. if (word_arg_count >= 4)
  479. EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), compiler->saveds_offset - sizeof(sljit_sw));
  480. FAIL_IF(emit_non_cum_binary(compiler, BINARY_OPCODE(SUB),
  481. SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, stack_size));
  482. stack_size = 0;
  483. arg_types >>= SLJIT_DEF_SHIFT;
  484. word_arg_count = 0;
  485. float_arg_count = 0;
  486. while (arg_types) {
  487. switch (arg_types & SLJIT_DEF_MASK) {
  488. case SLJIT_ARG_TYPE_F32:
  489. float_arg_count++;
  490. FAIL_IF(emit_sse2_store(compiler, 1, SLJIT_MEM1(SLJIT_SP), stack_size, float_arg_count));
  491. stack_size += sizeof(sljit_f32);
  492. break;
  493. case SLJIT_ARG_TYPE_F64:
  494. float_arg_count++;
  495. FAIL_IF(emit_sse2_store(compiler, 0, SLJIT_MEM1(SLJIT_SP), stack_size, float_arg_count));
  496. stack_size += sizeof(sljit_f64);
  497. break;
  498. default:
  499. word_arg_count++;
  500. if (word_arg_count == 3) {
  501. EMIT_MOV(compiler, SLJIT_MEM1(SLJIT_SP), stack_size, SLJIT_R2, 0);
  502. stack_size += sizeof(sljit_sw);
  503. }
  504. else if (word_arg_count == 4) {
  505. EMIT_MOV(compiler, SLJIT_MEM1(SLJIT_SP), stack_size, TMP_REG1, 0);
  506. stack_size += sizeof(sljit_sw);
  507. }
  508. break;
  509. }
  510. arg_types >>= SLJIT_DEF_SHIFT;
  511. }
  512. }
  513. if (word_arg_count > 0) {
  514. if (swap_args) {
  515. inst = (sljit_u8*)ensure_buf(compiler, 1 + 1);
  516. FAIL_IF(!inst);
  517. INC_SIZE(1);
  518. *inst++ = XCHG_EAX_r | reg_map[SLJIT_R2];
  519. }
  520. else {
  521. inst = (sljit_u8*)ensure_buf(compiler, 1 + 2);
  522. FAIL_IF(!inst);
  523. INC_SIZE(2);
  524. *inst++ = MOV_r_rm;
  525. *inst++ = MOD_REG | (reg_map[SLJIT_R2] << 3) | reg_map[SLJIT_R0];
  526. }
  527. }
  528. return SLJIT_SUCCESS;
  529. }
  530. #endif
  531. static sljit_s32 cdecl_call_get_stack_size(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *word_arg_count_ptr)
  532. {
  533. sljit_s32 stack_size = 0;
  534. sljit_s32 word_arg_count = 0;
  535. arg_types >>= SLJIT_DEF_SHIFT;
  536. while (arg_types) {
  537. switch (arg_types & SLJIT_DEF_MASK) {
  538. case SLJIT_ARG_TYPE_F32:
  539. stack_size += sizeof(sljit_f32);
  540. break;
  541. case SLJIT_ARG_TYPE_F64:
  542. stack_size += sizeof(sljit_f64);
  543. break;
  544. default:
  545. word_arg_count++;
  546. stack_size += sizeof(sljit_sw);
  547. break;
  548. }
  549. arg_types >>= SLJIT_DEF_SHIFT;
  550. }
  551. if (word_arg_count_ptr)
  552. *word_arg_count_ptr = word_arg_count;
  553. if (stack_size <= compiler->stack_tmp_size)
  554. return 0;
  555. #if defined(__APPLE__)
  556. return ((stack_size - compiler->stack_tmp_size + 15) & ~15);
  557. #else
  558. return stack_size - compiler->stack_tmp_size;
  559. #endif
  560. }
  561. static sljit_s32 cdecl_call_with_args(struct sljit_compiler *compiler,
  562. sljit_s32 arg_types, sljit_s32 stack_size, sljit_s32 word_arg_count)
  563. {
  564. sljit_s32 float_arg_count = 0;
  565. if (word_arg_count >= 4)
  566. EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), compiler->saveds_offset - sizeof(sljit_sw));
  567. if (stack_size > 0)
  568. FAIL_IF(emit_non_cum_binary(compiler, BINARY_OPCODE(SUB),
  569. SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, stack_size));
  570. stack_size = 0;
  571. word_arg_count = 0;
  572. arg_types >>= SLJIT_DEF_SHIFT;
  573. while (arg_types) {
  574. switch (arg_types & SLJIT_DEF_MASK) {
  575. case SLJIT_ARG_TYPE_F32:
  576. float_arg_count++;
  577. FAIL_IF(emit_sse2_store(compiler, 1, SLJIT_MEM1(SLJIT_SP), stack_size, float_arg_count));
  578. stack_size += sizeof(sljit_f32);
  579. break;
  580. case SLJIT_ARG_TYPE_F64:
  581. float_arg_count++;
  582. FAIL_IF(emit_sse2_store(compiler, 0, SLJIT_MEM1(SLJIT_SP), stack_size, float_arg_count));
  583. stack_size += sizeof(sljit_f64);
  584. break;
  585. default:
  586. word_arg_count++;
  587. EMIT_MOV(compiler, SLJIT_MEM1(SLJIT_SP), stack_size, (word_arg_count >= 4) ? TMP_REG1 : word_arg_count, 0);
  588. stack_size += sizeof(sljit_sw);
  589. break;
  590. }
  591. arg_types >>= SLJIT_DEF_SHIFT;
  592. }
  593. return SLJIT_SUCCESS;
  594. }
  595. static sljit_s32 post_call_with_args(struct sljit_compiler *compiler,
  596. sljit_s32 arg_types, sljit_s32 stack_size)
  597. {
  598. sljit_u8 *inst;
  599. sljit_s32 single;
  600. if (stack_size > 0)
  601. FAIL_IF(emit_cum_binary(compiler, BINARY_OPCODE(ADD),
  602. SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, stack_size));
  603. if ((arg_types & SLJIT_DEF_MASK) < SLJIT_ARG_TYPE_F32)
  604. return SLJIT_SUCCESS;
  605. single = ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32);
  606. inst = (sljit_u8*)ensure_buf(compiler, 1 + 3);
  607. FAIL_IF(!inst);
  608. INC_SIZE(3);
  609. inst[0] = single ? FSTPS : FSTPD;
  610. inst[1] = (0x03 << 3) | 0x04;
  611. inst[2] = (0x04 << 3) | reg_map[SLJIT_SP];
  612. return emit_sse2_load(compiler, single, SLJIT_FR0, SLJIT_MEM1(SLJIT_SP), 0);
  613. }
  614. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
  615. sljit_s32 arg_types)
  616. {
  617. struct sljit_jump *jump;
  618. sljit_s32 stack_size = 0;
  619. sljit_s32 word_arg_count;
  620. CHECK_ERROR_PTR();
  621. CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
  622. #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  623. if ((type & 0xff) == SLJIT_CALL) {
  624. stack_size = c_fast_call_get_stack_size(arg_types, &word_arg_count);
  625. PTR_FAIL_IF(c_fast_call_with_args(compiler, arg_types, stack_size, word_arg_count, 0));
  626. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  627. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  628. compiler->skip_checks = 1;
  629. #endif
  630. jump = sljit_emit_jump(compiler, type);
  631. PTR_FAIL_IF(jump == NULL);
  632. PTR_FAIL_IF(post_call_with_args(compiler, arg_types, 0));
  633. return jump;
  634. }
  635. #endif
  636. stack_size = cdecl_call_get_stack_size(compiler, arg_types, &word_arg_count);
  637. PTR_FAIL_IF(cdecl_call_with_args(compiler, arg_types, stack_size, word_arg_count));
  638. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  639. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  640. compiler->skip_checks = 1;
  641. #endif
  642. jump = sljit_emit_jump(compiler, type);
  643. PTR_FAIL_IF(jump == NULL);
  644. PTR_FAIL_IF(post_call_with_args(compiler, arg_types, stack_size));
  645. return jump;
  646. }
  647. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
  648. sljit_s32 arg_types,
  649. sljit_s32 src, sljit_sw srcw)
  650. {
  651. sljit_s32 stack_size = 0;
  652. sljit_s32 word_arg_count;
  653. #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  654. sljit_s32 swap_args;
  655. #endif
  656. CHECK_ERROR();
  657. CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
  658. #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  659. SLJIT_ASSERT(reg_map[SLJIT_R0] == 0 && reg_map[SLJIT_R2] == 1 && SLJIT_R0 == 1 && SLJIT_R2 == 3);
  660. if ((type & 0xff) == SLJIT_CALL) {
  661. stack_size = c_fast_call_get_stack_size(arg_types, &word_arg_count);
  662. swap_args = 0;
  663. if (word_arg_count > 0) {
  664. if ((src & REG_MASK) == SLJIT_R2 || OFFS_REG(src) == SLJIT_R2) {
  665. swap_args = 1;
  666. if (((src & REG_MASK) | 0x2) == SLJIT_R2)
  667. src ^= 0x2;
  668. if ((OFFS_REG(src) | 0x2) == SLJIT_R2)
  669. src ^= TO_OFFS_REG(0x2);
  670. }
  671. }
  672. FAIL_IF(c_fast_call_with_args(compiler, arg_types, stack_size, word_arg_count, swap_args));
  673. compiler->saveds_offset += stack_size;
  674. compiler->locals_offset += stack_size;
  675. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  676. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  677. compiler->skip_checks = 1;
  678. #endif
  679. FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
  680. compiler->saveds_offset -= stack_size;
  681. compiler->locals_offset -= stack_size;
  682. return post_call_with_args(compiler, arg_types, 0);
  683. }
  684. #endif
  685. stack_size = cdecl_call_get_stack_size(compiler, arg_types, &word_arg_count);
  686. FAIL_IF(cdecl_call_with_args(compiler, arg_types, stack_size, word_arg_count));
  687. compiler->saveds_offset += stack_size;
  688. compiler->locals_offset += stack_size;
  689. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  690. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  691. compiler->skip_checks = 1;
  692. #endif
  693. FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
  694. compiler->saveds_offset -= stack_size;
  695. compiler->locals_offset -= stack_size;
  696. return post_call_with_args(compiler, arg_types, stack_size);
  697. }
  698. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
  699. {
  700. sljit_u8 *inst;
  701. CHECK_ERROR();
  702. CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
  703. ADJUST_LOCAL_OFFSET(dst, dstw);
  704. CHECK_EXTRA_REGS(dst, dstw, (void)0);
  705. /* For UNUSED dst. Uncommon, but possible. */
  706. if (dst == SLJIT_UNUSED)
  707. dst = TMP_REG1;
  708. if (FAST_IS_REG(dst)) {
  709. /* Unused dest is possible here. */
  710. inst = (sljit_u8*)ensure_buf(compiler, 1 + 1);
  711. FAIL_IF(!inst);
  712. INC_SIZE(1);
  713. POP_REG(reg_map[dst]);
  714. return SLJIT_SUCCESS;
  715. }
  716. /* Memory. */
  717. inst = emit_x86_instruction(compiler, 1, 0, 0, dst, dstw);
  718. FAIL_IF(!inst);
  719. *inst++ = POP_rm;
  720. return SLJIT_SUCCESS;
  721. }
  722. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw)
  723. {
  724. sljit_u8 *inst;
  725. CHECK_ERROR();
  726. CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
  727. ADJUST_LOCAL_OFFSET(src, srcw);
  728. CHECK_EXTRA_REGS(src, srcw, (void)0);
  729. if (FAST_IS_REG(src)) {
  730. inst = (sljit_u8*)ensure_buf(compiler, 1 + 1 + 1);
  731. FAIL_IF(!inst);
  732. INC_SIZE(1 + 1);
  733. PUSH_REG(reg_map[src]);
  734. }
  735. else {
  736. inst = emit_x86_instruction(compiler, 1, 0, 0, src, srcw);
  737. FAIL_IF(!inst);
  738. *inst++ = GROUP_FF;
  739. *inst |= PUSH_rm;
  740. inst = (sljit_u8*)ensure_buf(compiler, 1 + 1);
  741. FAIL_IF(!inst);
  742. INC_SIZE(1);
  743. }
  744. RET();
  745. return SLJIT_SUCCESS;
  746. }