sljitNativeX86_32.c 26 KB

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