sljitNativeX86_32.c 27 KB

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