codegen.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. #include <assert.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include "../const.h"
  5. #include "../cpu.h"
  6. #include "../global_pointers.h"
  7. #include "../log.h"
  8. #include "codegen.h"
  9. #include "wasmgen.h"
  10. static void jit_add_seg_offset(int32_t default_segment);
  11. static void jit_resolve_modrm32_(int32_t modrm_byte);
  12. static void jit_resolve_modrm16_(int32_t modrm_byte);
  13. PackedStr pack_str(char const* fn_name, uint8_t fn_len);
  14. void gen_reset(void)
  15. {
  16. wg_reset();
  17. cs = wg_get_code_section();
  18. instruction_body = wg_get_instruction_body();
  19. add_get_seg_import();
  20. }
  21. void add_get_seg_import(void)
  22. {
  23. uint16_t _fn_get_seg_idx = get_fn_idx("get_seg", 7, FN1_RET_TYPE_INDEX);
  24. assert(_fn_get_seg_idx == fn_get_seg_idx);
  25. UNUSED(_fn_get_seg_idx);
  26. }
  27. PackedStr pack_str(char const* fn_name, uint8_t fn_len)
  28. {
  29. assert(fn_len <= 24);
  30. union {
  31. PackedStr pstr;
  32. uint8_t u8s[24];
  33. } ret = { { 0, 0, 0 } };
  34. for(int i = 0; i < fn_len; i++)
  35. {
  36. ret.u8s[i] = fn_name[i];
  37. }
  38. return ret.pstr;
  39. }
  40. uint16_t get_fn_idx(char const* fn, uint8_t fn_len, uint8_t fn_type)
  41. {
  42. PackedStr pstr = pack_str(fn, fn_len);
  43. return wg_get_fn_idx(pstr.a, pstr.b, pstr.c, fn_type);
  44. }
  45. void gen_increment_mem32(int32_t addr)
  46. {
  47. wg_increment_mem32(cs, addr);
  48. }
  49. void gen_increment_variable(int32_t variable_address, int32_t n)
  50. {
  51. wg_increment_variable(cs, variable_address, n);
  52. }
  53. void gen_increment_instruction_pointer(int32_t n)
  54. {
  55. wg_push_i32(cs, (int32_t)instruction_pointer); // store address of ip
  56. wg_load_aligned_i32(cs, (int32_t)instruction_pointer); // load ip
  57. wg_push_i32(cs, n);
  58. wg_add_i32(cs);
  59. wg_store_aligned_i32(cs); // store it back in
  60. }
  61. void gen_relative_jump(int32_t n)
  62. {
  63. // add n to instruction_pointer (without setting the offset as above)
  64. wg_push_i32(instruction_body, (int32_t)instruction_pointer);
  65. wg_load_aligned_i32(instruction_body, (int32_t)instruction_pointer);
  66. wg_push_i32(instruction_body, n);
  67. wg_add_i32(instruction_body);
  68. wg_store_aligned_i32(instruction_body);
  69. }
  70. void gen_increment_timestamp_counter(uint32_t n)
  71. {
  72. gen_increment_variable((int32_t)timestamp_counter, n);
  73. }
  74. void gen_set_previous_eip_offset_from_eip(int32_t n)
  75. {
  76. wg_push_i32(cs, (int32_t)previous_ip); // store address of previous ip
  77. wg_load_aligned_i32(cs, (int32_t)instruction_pointer); // load ip
  78. if(n != 0)
  79. {
  80. wg_push_i32(cs, n);
  81. wg_add_i32(cs); // add constant to ip value
  82. }
  83. wg_store_aligned_i32(cs); // store it as previous ip
  84. }
  85. void gen_set_previous_eip(void)
  86. {
  87. wg_push_i32(cs, (int32_t)previous_ip); // store address of previous ip
  88. wg_load_aligned_i32(cs, (int32_t)instruction_pointer); // load ip
  89. wg_store_aligned_i32(cs); // store it as previous ip
  90. }
  91. void gen_clear_prefixes(void)
  92. {
  93. wg_push_i32(instruction_body, (int32_t)prefixes); // load address of prefixes
  94. wg_push_i32(instruction_body, 0);
  95. wg_store_aligned_i32(instruction_body);
  96. }
  97. void gen_add_prefix_bits(int32_t mask)
  98. {
  99. assert(mask >= 0 && mask < 0x100);
  100. wg_push_i32(instruction_body, (int32_t)prefixes); // load address of prefixes
  101. wg_load_aligned_i32(instruction_body, (int32_t)prefixes); // load old value
  102. wg_push_i32(instruction_body, mask);
  103. wg_or_i32(instruction_body);
  104. wg_store_aligned_i32(instruction_body);
  105. }
  106. void gen_fn0_const_ret(char const* fn, uint8_t fn_len)
  107. {
  108. int32_t fn_idx = get_fn_idx(fn, fn_len, FN0_RET_TYPE_INDEX);
  109. wg_call_fn(instruction_body, fn_idx);
  110. }
  111. void gen_fn0_const(char const* fn, uint8_t fn_len)
  112. {
  113. int32_t fn_idx = get_fn_idx(fn, fn_len, FN0_TYPE_INDEX);
  114. wg_call_fn(instruction_body, fn_idx);
  115. }
  116. void gen_set_reg16_fn0(char const* fn, uint8_t fn_len, int32_t reg)
  117. {
  118. // generates: reg16[reg] = fn()
  119. int32_t fn_idx = get_fn_idx(fn, fn_len, FN0_RET_TYPE_INDEX);
  120. wg_push_i32(instruction_body, (int32_t) &reg16[reg]);
  121. wg_call_fn(instruction_body, fn_idx);
  122. wg_store_aligned_u16(instruction_body);
  123. }
  124. void gen_set_reg32s_fn0(char const* fn, uint8_t fn_len, int32_t reg)
  125. {
  126. // generates: reg32s[reg] = fn()
  127. int32_t fn_idx = get_fn_idx(fn, fn_len, FN0_RET_TYPE_INDEX);
  128. wg_push_i32(instruction_body, (int32_t) &reg32s[reg]);
  129. wg_call_fn(instruction_body, fn_idx);
  130. wg_store_aligned_i32(instruction_body);
  131. }
  132. void gen_fn1_const_ret(char const* fn, uint8_t fn_len, int32_t arg0)
  133. {
  134. int32_t fn_idx = get_fn_idx(fn, fn_len, FN1_RET_TYPE_INDEX);
  135. wg_push_i32(instruction_body, arg0);
  136. wg_call_fn(instruction_body, fn_idx);
  137. }
  138. void gen_call_fn1_ret(char const* fn, uint8_t fn_len)
  139. {
  140. // generates: fn( _ ) where _ must be left on the stack before calling this, and fn returns a value
  141. int32_t fn_idx = get_fn_idx(fn, fn_len, FN1_RET_TYPE_INDEX);
  142. wg_call_fn(instruction_body, fn_idx);
  143. }
  144. void gen_call_fn1(char const* fn, uint8_t fn_len)
  145. {
  146. // generates: fn( _ ) where _ must be left on the stack before calling this
  147. int32_t fn_idx = get_fn_idx(fn, fn_len, FN1_TYPE_INDEX);
  148. wg_call_fn(instruction_body, fn_idx);
  149. }
  150. void gen_fn1_const(char const* fn, uint8_t fn_len, int32_t arg0)
  151. {
  152. int32_t fn_idx = get_fn_idx(fn, fn_len, FN1_TYPE_INDEX);
  153. wg_push_i32(instruction_body, arg0);
  154. wg_call_fn(instruction_body, fn_idx);
  155. }
  156. void gen_set_reg16_r(int32_t r_dest, int32_t r_src)
  157. {
  158. // generates: reg16[r_dest] = reg16[r_src]
  159. wg_push_i32(instruction_body, (int32_t) &reg16[r_dest]);
  160. wg_load_aligned_u16(instruction_body, (int32_t) &reg16[r_src]);
  161. wg_store_aligned_u16(instruction_body);
  162. }
  163. void gen_set_reg32_r(int32_t r_dest, int32_t r_src)
  164. {
  165. // generates: reg32s[r_dest] = reg32s[r_src]
  166. wg_push_i32(instruction_body, (int32_t) &reg32s[r_dest]);
  167. wg_load_aligned_i32(instruction_body, (int32_t) &reg32s[r_src]);
  168. wg_store_aligned_i32(instruction_body);
  169. }
  170. void gen_fn1_reg16(char const* fn, uint8_t fn_len, int32_t reg)
  171. {
  172. // generates: fn(reg16[reg])
  173. int32_t fn_idx = get_fn_idx(fn, fn_len, FN1_TYPE_INDEX);
  174. wg_load_aligned_u16(instruction_body, (int32_t) &reg16[reg]);
  175. wg_call_fn(instruction_body, fn_idx);
  176. }
  177. void gen_fn1_reg32s(char const* fn, uint8_t fn_len, int32_t reg)
  178. {
  179. // generates: fn(reg32s[reg])
  180. int32_t fn_idx = get_fn_idx(fn, fn_len, FN1_TYPE_INDEX);
  181. wg_load_aligned_i32(instruction_body, (int32_t) &reg32s[reg]);
  182. wg_call_fn(instruction_body, fn_idx);
  183. }
  184. void gen_call_fn2(char const* fn, uint8_t fn_len)
  185. {
  186. // generates: fn( _, _ ) where _ must be left on the stack before calling this
  187. int32_t fn_idx = get_fn_idx(fn, fn_len, FN2_TYPE_INDEX);
  188. wg_call_fn(instruction_body, fn_idx);
  189. }
  190. void gen_fn2_const(char const* fn, uint8_t fn_len, int32_t arg0, int32_t arg1)
  191. {
  192. int32_t fn_idx = get_fn_idx(fn, fn_len, FN2_TYPE_INDEX);
  193. wg_push_i32(instruction_body, arg0);
  194. wg_push_i32(instruction_body, arg1);
  195. wg_call_fn(instruction_body, fn_idx);
  196. }
  197. void gen_fn3_const(char const* fn, uint8_t fn_len, int32_t arg0, int32_t arg1, int32_t arg2)
  198. {
  199. int32_t fn_idx = get_fn_idx(fn, fn_len, FN3_TYPE_INDEX);
  200. wg_push_i32(instruction_body, arg0);
  201. wg_push_i32(instruction_body, arg1);
  202. wg_push_i32(instruction_body, arg2);
  203. wg_call_fn(instruction_body, fn_idx);
  204. }
  205. void gen_safe_read32(void)
  206. {
  207. // Assumes virtual address has been pushed to the stack, and generates safe_read32s' fast-path
  208. // inline, bailing to safe_read32s_slow if necessary
  209. const int32_t address_local = GEN_LOCAL_SCRATCH0;
  210. wg_tee_local(instruction_body, address_local);
  211. // Pseudo: base_on_stack = (uint32_t)address >> 12;
  212. wg_push_i32(instruction_body, 12);
  213. wg_shr_u32(instruction_body);
  214. SCALE_INDEX_FOR_ARRAY32(tlb_data);
  215. // Pseudo: entry = tlb_data[base_on_stack];
  216. const int32_t entry_local = GEN_LOCAL_SCRATCH1;
  217. wg_load_aligned_i32_from_stack(instruction_body, (uint32_t) tlb_data);
  218. wg_tee_local(instruction_body, entry_local);
  219. // Pseudo: bool can_use_fast_path = (entry & 0xFFF & ~TLB_READONLY & ~TLB_GLOBAL & ~(cpl == 3 ? 0 : TLB_NO_USER) == TLB_VALID &&
  220. // (address & 0xFFF) <= (0x1000 - 4));
  221. wg_push_i32(instruction_body, 0xFFF & ~TLB_READONLY & ~TLB_GLOBAL & ~(*cpl == 3 ? 0 : TLB_NO_USER));
  222. wg_and_i32(instruction_body);
  223. wg_push_i32(instruction_body, TLB_VALID);
  224. wg_eq_i32(instruction_body);
  225. wg_get_local(instruction_body, address_local);
  226. wg_push_i32(instruction_body, 0xFFF);
  227. wg_and_i32(instruction_body);
  228. wg_push_i32(instruction_body, 0x1000 - 4);
  229. wg_le_i32(instruction_body);
  230. wg_and_i32(instruction_body);
  231. // Pseudo:
  232. // if(can_use_fast_path) leave_on_stack(mem8[entry & ~0xFFF ^ address]);
  233. wg_if_i32(instruction_body);
  234. wg_get_local(instruction_body, entry_local);
  235. wg_push_i32(instruction_body, ~0xFFF);
  236. wg_and_i32(instruction_body);
  237. wg_get_local(instruction_body, address_local);
  238. wg_xor_i32(instruction_body);
  239. wg_load_unaligned_i32_from_stack(instruction_body, (uint32_t) mem8);
  240. // Pseudo:
  241. // else { leave_on_stack(safe_read32s_slow(address)); }
  242. wg_else(instruction_body);
  243. wg_get_local(instruction_body, address_local);
  244. gen_call_fn1_ret("safe_read32s_slow", 17);
  245. wg_block_end(instruction_body);
  246. }
  247. void gen_safe_write32(int32_t local_for_address, int32_t local_for_value)
  248. {
  249. // Generates safe_write32' fast-path inline, bailing to safe_write32_slow if necessary.
  250. // local_for_{address,value} are the numbers of the local variables which contain the virtual
  251. // address and value for safe_write32
  252. // Usage:
  253. // set_local(0, value);
  254. // set_local(1, v_addr);
  255. // gen_safe_write32();
  256. // Since this function clobbers other variables, we confirm that the caller uses the local
  257. // variables we expect them to
  258. assert(local_for_address == GEN_LOCAL_SCRATCH0);
  259. assert(local_for_value == GEN_LOCAL_SCRATCH1);
  260. wg_get_local(instruction_body, local_for_address);
  261. // Pseudo: base_on_stack = (uint32_t)address >> 12;
  262. wg_push_i32(instruction_body, 12);
  263. wg_shr_u32(instruction_body);
  264. SCALE_INDEX_FOR_ARRAY32(tlb_data);
  265. // entry_local is only used in the following block, so the scratch variable can be reused later
  266. {
  267. // Pseudo: entry = tlb_data[base_on_stack];
  268. const int32_t entry_local = GEN_LOCAL_SCRATCH2;
  269. wg_load_aligned_i32_from_stack(instruction_body, (uint32_t) tlb_data);
  270. wg_tee_local(instruction_body, entry_local);
  271. // Pseudo: bool can_use_fast_path = (entry & 0xFFF & ~TLB_GLOBAL & ~(cpl == 3 ? 0 : TLB_NO_USER) == TLB_VALID &&
  272. // (address & 0xFFF) <= (0x1000 - 4));
  273. wg_push_i32(instruction_body, 0xFFF & ~TLB_GLOBAL & ~(*cpl == 3 ? 0 : TLB_NO_USER));
  274. wg_and_i32(instruction_body);
  275. wg_push_i32(instruction_body, TLB_VALID);
  276. wg_eq_i32(instruction_body);
  277. wg_get_local(instruction_body, local_for_address);
  278. wg_push_i32(instruction_body, 0xFFF);
  279. wg_and_i32(instruction_body);
  280. wg_push_i32(instruction_body, 0x1000 - 4);
  281. wg_le_i32(instruction_body);
  282. wg_and_i32(instruction_body);
  283. // Pseudo:
  284. // if(can_use_fast_path)
  285. // {
  286. // phys_addr = entry & ~0xFFF ^ address;
  287. wg_if_void(instruction_body);
  288. wg_get_local(instruction_body, entry_local);
  289. wg_push_i32(instruction_body, ~0xFFF);
  290. wg_and_i32(instruction_body);
  291. wg_get_local(instruction_body, local_for_address);
  292. wg_xor_i32(instruction_body);
  293. }
  294. // entry_local isn't needed anymore, so we overwrite it
  295. const int32_t phys_addr_local = GEN_LOCAL_SCRATCH2;
  296. // Pseudo:
  297. // /* continued within can_use_fast_path branch */
  298. // mem8[phys_addr] = value;
  299. wg_tee_local(instruction_body, phys_addr_local);
  300. wg_get_local(instruction_body, local_for_value);
  301. wg_store_unaligned_i32(instruction_body, (uint32_t) mem8);
  302. // Pseudo:
  303. // else { safe_read32_slow(address, value); }
  304. wg_else(instruction_body);
  305. wg_get_local(instruction_body, local_for_address);
  306. wg_get_local(instruction_body, local_for_value);
  307. gen_call_fn2("safe_write32_slow", 17);
  308. wg_block_end(instruction_body);
  309. }
  310. #define MODRM_ENTRY(n, work)\
  311. case (n) | 0 << 3:\
  312. case (n) | 1 << 3:\
  313. case (n) | 2 << 3:\
  314. case (n) | 3 << 3:\
  315. case (n) | 4 << 3:\
  316. case (n) | 5 << 3:\
  317. case (n) | 6 << 3:\
  318. case (n) | 7 << 3:\
  319. (work); break;
  320. #define MODRM_ENTRY16_0(row, seg, reg1, reg2)\
  321. MODRM_ENTRY(0x00 | (row), gen_modrm_entry_0((seg), (reg1), (reg2), 0))\
  322. MODRM_ENTRY(0x40 | (row), gen_modrm_entry_0((seg), (reg1), (reg2), read_imm8s()))\
  323. MODRM_ENTRY(0x80 | (row), gen_modrm_entry_0((seg), (reg1), (reg2), read_imm16()))
  324. #define MODRM_ENTRY16_1(row, seg, reg)\
  325. MODRM_ENTRY(0x00 | (row), gen_modrm_entry_1(seg, reg, 0))\
  326. MODRM_ENTRY(0x40 | (row), gen_modrm_entry_1(seg, reg, read_imm8s()))\
  327. MODRM_ENTRY(0x80 | (row), gen_modrm_entry_1(seg, reg, read_imm16()))
  328. static void inline gen_modrm_entry_0(int32_t segment, int32_t reg16_idx_1, int32_t reg16_idx_2, int32_t imm)
  329. {
  330. // generates: fn( ( reg1 + reg2 + imm ) & 0xFFFF )
  331. wg_load_aligned_u16(instruction_body, reg16_idx_1);
  332. wg_load_aligned_u16(instruction_body, reg16_idx_2);
  333. wg_add_i32(instruction_body);
  334. if(imm)
  335. {
  336. wg_push_i32(instruction_body, imm);
  337. wg_add_i32(instruction_body);
  338. }
  339. wg_push_i32(instruction_body, 0xFFFF);
  340. wg_and_i32(instruction_body);
  341. jit_add_seg_offset(segment);
  342. }
  343. static void gen_modrm_entry_1(int32_t segment, int32_t reg16_idx, int32_t imm)
  344. {
  345. // generates: fn ( ( reg + imm ) & 0xFFFF )
  346. wg_load_aligned_u16(instruction_body, reg16_idx);
  347. if(imm)
  348. {
  349. wg_push_i32(instruction_body, imm);
  350. wg_add_i32(instruction_body);
  351. }
  352. wg_push_i32(instruction_body, 0xFFFF);
  353. wg_and_i32(instruction_body);
  354. jit_add_seg_offset(segment);
  355. }
  356. static bool can_optimize_get_seg(int32_t segment)
  357. {
  358. return (segment == DS || segment == SS) && has_flat_segmentation();
  359. }
  360. /*
  361. * Note: Requires an existing value to be on the WASM stack! Based on optimization possibilities,
  362. * the value will be consumed and added to get_seg(segment), or it'll be left as-is
  363. */
  364. static void jit_add_seg_offset(int32_t default_segment)
  365. {
  366. int32_t prefix = *prefixes & PREFIX_MASK_SEGMENT;
  367. int32_t seg = prefix ? prefix - 1 : default_segment;
  368. if(can_optimize_get_seg(seg) || prefix == SEG_PREFIX_ZERO)
  369. {
  370. return;
  371. }
  372. wg_push_i32(instruction_body, seg);
  373. wg_call_fn(instruction_body, fn_get_seg_idx);
  374. wg_add_i32(instruction_body);
  375. }
  376. static void gen_modrm_entry_2()
  377. {
  378. wg_push_i32(instruction_body, read_imm16());
  379. jit_add_seg_offset(DS);
  380. }
  381. static void jit_resolve_modrm16_(int32_t modrm_byte)
  382. {
  383. switch(modrm_byte)
  384. {
  385. // The following casts cause some weird issue with emscripten and cause
  386. // a performance hit. XXX: look into this later.
  387. MODRM_ENTRY16_0(0, DS, (int32_t)(reg16 + BX), (int32_t)(reg16 + SI))
  388. MODRM_ENTRY16_0(1, DS, (int32_t)(reg16 + BX), (int32_t)(reg16 + DI))
  389. MODRM_ENTRY16_0(2, SS, (int32_t)(reg16 + BP), (int32_t)(reg16 + SI))
  390. MODRM_ENTRY16_0(3, SS, (int32_t)(reg16 + BP), (int32_t)(reg16 + DI))
  391. MODRM_ENTRY16_1(4, DS, (int32_t)(reg16 + SI))
  392. MODRM_ENTRY16_1(5, DS, (int32_t)(reg16 + DI))
  393. // special case
  394. MODRM_ENTRY(0x00 | 6, gen_modrm_entry_2())
  395. MODRM_ENTRY(0x40 | 6, gen_modrm_entry_1(SS, (int32_t)(reg16 + BP), read_imm8s()))
  396. MODRM_ENTRY(0x80 | 6, gen_modrm_entry_1(SS, (int32_t)(reg16 + BP), read_imm16()))
  397. MODRM_ENTRY16_1(7, DS, (int32_t)(reg16 + BX))
  398. default:
  399. assert(false);
  400. }
  401. }
  402. #define MODRM_ENTRY32_0(row, seg, reg)\
  403. MODRM_ENTRY(0x00 | (row), gen_modrm32_entry(seg, reg, 0))\
  404. MODRM_ENTRY(0x40 | (row), gen_modrm32_entry(seg, reg, read_imm8s()))\
  405. MODRM_ENTRY(0x80 | (row), gen_modrm32_entry(seg, reg, read_imm32s()))
  406. static void gen_modrm32_entry(int32_t segment, int32_t reg32s_idx, int32_t imm)
  407. {
  408. // generates: fn ( reg + imm )
  409. wg_load_aligned_i32(instruction_body, reg32s_idx);
  410. if(imm)
  411. {
  412. wg_push_i32(instruction_body, imm);
  413. wg_add_i32(instruction_body);
  414. }
  415. jit_add_seg_offset(segment);
  416. }
  417. static void jit_resolve_sib(bool mod)
  418. {
  419. uint8_t sib_byte = read_imm8();
  420. uint8_t r = sib_byte & 7;
  421. uint8_t m = sib_byte >> 3 & 7;
  422. int32_t base_addr;
  423. int32_t base;
  424. uint8_t seg;
  425. bool base_is_mem_access = true;
  426. if(r == 4)
  427. {
  428. base_addr = (int32_t)(reg32s + ESP);
  429. seg = SS;
  430. }
  431. else if(r == 5)
  432. {
  433. if(mod)
  434. {
  435. base_addr = (int32_t)(reg32s + EBP);
  436. seg = SS;
  437. }
  438. else
  439. {
  440. base = read_imm32s();
  441. seg = DS;
  442. base_is_mem_access = false;
  443. }
  444. }
  445. else
  446. {
  447. base_addr = (int32_t)(reg32s + r);
  448. seg = DS;
  449. }
  450. // generate: get_seg_prefix(seg) + base
  451. // Where base is accessed from memory if base_is_mem_access or written as a constant otherwise
  452. if(base_is_mem_access)
  453. {
  454. wg_load_aligned_i32(instruction_body, base_addr);
  455. }
  456. else
  457. {
  458. wg_push_i32(instruction_body, base);
  459. }
  460. jit_add_seg_offset(seg);
  461. // We now have to generate an offset value to add
  462. if(m == 4)
  463. {
  464. // offset is 0, we don't need to add anything
  465. return;
  466. }
  467. // Offset is reg32s[m] << s, where s is:
  468. uint8_t s = sib_byte >> 6 & 3;
  469. wg_load_aligned_i32(instruction_body, (int32_t)(reg32s + m));
  470. wg_push_i32(instruction_body, s);
  471. wg_shl_i32(instruction_body);
  472. wg_add_i32(instruction_body);
  473. }
  474. static void modrm32_special_case_1(void)
  475. {
  476. jit_resolve_sib(true);
  477. int32_t imm = read_imm8s();
  478. if(imm)
  479. {
  480. wg_push_i32(instruction_body, imm);
  481. wg_add_i32(instruction_body);
  482. }
  483. }
  484. static void modrm32_special_case_2(void)
  485. {
  486. jit_resolve_sib(true);
  487. int32_t imm = read_imm32s();
  488. if(imm)
  489. {
  490. wg_push_i32(instruction_body, imm);
  491. wg_add_i32(instruction_body);
  492. }
  493. }
  494. static void gen_modrm32_entry_1()
  495. {
  496. int32_t imm = read_imm32s();
  497. wg_push_i32(instruction_body, imm);
  498. jit_add_seg_offset(DS);
  499. }
  500. static void jit_resolve_modrm32_(int32_t modrm_byte)
  501. {
  502. switch(modrm_byte)
  503. {
  504. MODRM_ENTRY32_0(0, DS, (int32_t)(reg32s + EAX))
  505. MODRM_ENTRY32_0(1, DS, (int32_t)(reg32s + ECX))
  506. MODRM_ENTRY32_0(2, DS, (int32_t)(reg32s + EDX))
  507. MODRM_ENTRY32_0(3, DS, (int32_t)(reg32s + EBX))
  508. // special cases
  509. MODRM_ENTRY(0x00 | 4, jit_resolve_sib(false))
  510. MODRM_ENTRY(0x40 | 4, modrm32_special_case_1())
  511. MODRM_ENTRY(0x80 | 4, modrm32_special_case_2())
  512. MODRM_ENTRY(0x00 | 5, gen_modrm32_entry_1())
  513. MODRM_ENTRY(0x40 | 5, gen_modrm32_entry(SS, (int32_t)(reg32s + EBP), read_imm8s()))
  514. MODRM_ENTRY(0x80 | 5, gen_modrm32_entry(SS, (int32_t)(reg32s + EBP), read_imm32s()))
  515. MODRM_ENTRY32_0(6, DS, (int32_t)(reg32s + ESI))
  516. MODRM_ENTRY32_0(7, DS, (int32_t)(reg32s + EDI))
  517. default:
  518. assert(false);
  519. }
  520. }
  521. #undef MODRM_ENTRY
  522. // This function leaves a value on the wasm stack, to be consumed by one of the
  523. // gen_modrm_fn* functions below
  524. void gen_modrm_resolve(int32_t modrm_byte)
  525. {
  526. if(is_asize_32())
  527. {
  528. jit_resolve_modrm32_(modrm_byte);
  529. }
  530. else
  531. {
  532. jit_resolve_modrm16_(modrm_byte);
  533. }
  534. }
  535. void gen_modrm_fn2(char const* fn, uint8_t fn_len, int32_t arg0, int32_t arg1)
  536. {
  537. // generates: fn( _, arg0, arg1 )
  538. wg_push_i32(instruction_body, arg0);
  539. wg_push_i32(instruction_body, arg1);
  540. int32_t fn_idx = get_fn_idx(fn, fn_len, FN3_TYPE_INDEX);
  541. wg_call_fn(instruction_body, fn_idx);
  542. }
  543. void gen_modrm_fn1(char const* fn, uint8_t fn_len, int32_t arg0)
  544. {
  545. // generates: fn( _, arg0 )
  546. wg_push_i32(instruction_body, arg0);
  547. int32_t fn_idx = get_fn_idx(fn, fn_len, FN2_TYPE_INDEX);
  548. wg_call_fn(instruction_body, fn_idx);
  549. }
  550. void gen_modrm_fn0(char const* fn, uint8_t fn_len)
  551. {
  552. // generates: fn( _ )
  553. int32_t fn_idx = get_fn_idx(fn, fn_len, FN1_TYPE_INDEX);
  554. wg_call_fn(instruction_body, fn_idx);
  555. }