codegen.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 "codegen.h"
  8. #include "cstring.h"
  9. #include "module_init.h"
  10. #include "util.h"
  11. #include "wasm_opcodes.h"
  12. #include "wasm_util.h"
  13. static Buffer op = { .start = codegen_buffer_op, .ptr = codegen_buffer_op, .len = 0x10000 };
  14. static Buffer cs = { .start = codegen_buffer_cs, .ptr = codegen_buffer_cs, .len = 0x10000 };
  15. Buffer instruction_body = {
  16. .start = codegen_buffer_instruction_body,
  17. .ptr = codegen_buffer_instruction_body,
  18. .len = 0x10000,
  19. };
  20. static uint8_t* op_ptr_reset_location;
  21. static uint32_t import_table_size_reset_value;
  22. static uint32_t initial_import_count;
  23. static void jit_add_seg_offset(int32_t default_segment);
  24. static void jit_resolve_modrm32_(int32_t modrm_byte);
  25. static void jit_resolve_modrm16_(int32_t modrm_byte);
  26. void gen_init(void)
  27. {
  28. // wasm magic header
  29. write_raw_u8(&op, 0); write_raw_u8(&op, 'a'); write_raw_u8(&op, 's'); write_raw_u8(&op, 'm');
  30. // wasm version in leb128, 4 bytes
  31. write_raw_u8(&op, WASM_VERSION); write_raw_u8(&op, 0); write_raw_u8(&op, 0); write_raw_u8(&op, 0);
  32. write_type_section();
  33. write_import_section_preamble();
  34. // add initial imports
  35. uint8_t _fn_get_seg_idx = write_import_entry("get_seg", 7, FN1_RET_TYPE_INDEX);
  36. assert(_fn_get_seg_idx == fn_get_seg_idx);
  37. UNUSED(_fn_get_seg_idx);
  38. // store state of current pointers etc. so we can reset them later
  39. op_ptr_reset_location = op.ptr;
  40. initial_import_count = *ptr_import_count;
  41. import_table_size_reset_value = import_table_size;
  42. }
  43. void gen_reset(void)
  44. {
  45. op.ptr = op_ptr_reset_location;
  46. cs.ptr = cs.start;
  47. *ptr_import_count = initial_import_count;
  48. import_table_size = import_table_size_reset_value;
  49. }
  50. uintptr_t gen_finish(int32_t no_of_locals_i32)
  51. {
  52. write_memory_import();
  53. write_function_section(1);
  54. write_export_section();
  55. uint8_t* ptr_code_section_size = (uint8_t*) 0; // initialized below
  56. uint8_t* ptr_fn_body_size = (uint8_t*) 0; // this as well
  57. // write code section preamble
  58. write_raw_u8(&op, SC_CODE);
  59. ptr_code_section_size = op.ptr; // we will write to this location later
  60. write_raw_u8(&op, 0); write_raw_u8(&op, 0); // write temp val for now using 4 bytes
  61. write_raw_u8(&op, 0); write_raw_u8(&op, 0);
  62. write_raw_u8(&op, 1); // number of function bodies: just 1
  63. // same as above but for body size of the function
  64. ptr_fn_body_size = op.ptr;
  65. write_raw_u8(&op, 0); write_raw_u8(&op, 0);
  66. write_raw_u8(&op, 0); write_raw_u8(&op, 0);
  67. write_raw_u8(&op, 1); // count of local blocks
  68. write_raw_u8(&op, no_of_locals_i32); write_raw_u8(&op, TYPE_I32); // 2 locals of type i32
  69. copy_code_section();
  70. // write code section epilogue
  71. write_raw_u8(&op, OP_END);
  72. // write the actual sizes to the pointer locations stored above. We subtract 1 from the actual
  73. // value because the ptr itself points to two bytes
  74. write_fixed_leb32_to_ptr(ptr_fn_body_size, ((op.ptr - 1) - ptr_fn_body_size) - 3);
  75. write_fixed_leb32_to_ptr(ptr_code_section_size, ((op.ptr - 1) - ptr_code_section_size) - 3);
  76. return (uintptr_t) op.ptr;
  77. }
  78. uintptr_t gen_get_final_offset(void)
  79. {
  80. return (uintptr_t) op.ptr;
  81. }
  82. void gen_increment_variable(int32_t variable_address, int32_t n)
  83. {
  84. push_i32(&cs, variable_address);
  85. load_aligned_i32(&cs, variable_address);
  86. push_i32(&cs, n);
  87. add_i32(&cs);
  88. store_aligned_i32(&cs);
  89. }
  90. void gen_increment_instruction_pointer(int32_t n)
  91. {
  92. push_i32(&cs, (int32_t)instruction_pointer); // store address of ip
  93. load_aligned_i32(&cs, (int32_t)instruction_pointer); // load ip
  94. push_i32(&cs, n);
  95. add_i32(&cs);
  96. store_aligned_i32(&cs); // store it back in
  97. }
  98. void gen_relative_jump(int32_t n)
  99. {
  100. // add n to instruction_pointer (without setting the offset as above)
  101. push_i32(&instruction_body, (int32_t)instruction_pointer);
  102. load_aligned_i32(&instruction_body, (int32_t)instruction_pointer);
  103. push_i32(&instruction_body, n);
  104. add_i32(&instruction_body);
  105. store_aligned_i32(&instruction_body);
  106. }
  107. void gen_increment_timestamp_counter(uint32_t n)
  108. {
  109. gen_increment_variable((int32_t)timestamp_counter, n);
  110. }
  111. void gen_set_previous_eip_offset_from_eip(int32_t n)
  112. {
  113. push_i32(&cs, (int32_t)previous_ip); // store address of previous ip
  114. load_aligned_i32(&cs, (int32_t)instruction_pointer); // load ip
  115. if(n != 0)
  116. {
  117. push_i32(&cs, n);
  118. add_i32(&cs); // add constant to ip value
  119. }
  120. store_aligned_i32(&cs); // store it as previous ip
  121. }
  122. void gen_set_previous_eip(void)
  123. {
  124. push_i32(&cs, (int32_t)previous_ip); // store address of previous ip
  125. load_aligned_i32(&cs, (int32_t)instruction_pointer); // load ip
  126. store_aligned_i32(&cs); // store it as previous ip
  127. }
  128. void gen_clear_prefixes(void)
  129. {
  130. push_i32(&instruction_body, (int32_t)prefixes); // load address of prefixes
  131. push_i32(&instruction_body, 0);
  132. store_aligned_i32(&instruction_body);
  133. }
  134. void gen_add_prefix_bits(int32_t mask)
  135. {
  136. assert(mask >= 0 && mask < 0x100);
  137. push_i32(&instruction_body, (int32_t)prefixes); // load address of prefixes
  138. load_aligned_i32(&instruction_body, (int32_t)prefixes); // load old value
  139. push_i32(&instruction_body, mask);
  140. or_i32(&instruction_body);
  141. store_aligned_i32(&instruction_body);
  142. }
  143. void gen_fn0_ret(char const* fn, uint8_t fn_len)
  144. {
  145. int32_t fn_idx = get_fn_index(fn, fn_len, FN0_RET_TYPE_INDEX);
  146. call_fn(&instruction_body, fn_idx);
  147. }
  148. void gen_fn0(char const* fn, uint8_t fn_len)
  149. {
  150. int32_t fn_idx = get_fn_index(fn, fn_len, FN0_TYPE_INDEX);
  151. call_fn(&instruction_body, fn_idx);
  152. }
  153. void gen_reg16_eq_fn0(char const* fn, uint8_t fn_len, int32_t reg)
  154. {
  155. // generates: reg16[reg] = fn()
  156. int32_t fn_idx = get_fn_index(fn, fn_len, FN0_RET_TYPE_INDEX);
  157. push_i32(&instruction_body, (int32_t) &reg16[reg]);
  158. call_fn(&instruction_body, fn_idx);
  159. store_aligned_u16(&instruction_body);
  160. }
  161. void gen_reg32s_eq_fn0(char const* fn, uint8_t fn_len, int32_t reg)
  162. {
  163. // generates: reg32s[reg] = fn()
  164. int32_t fn_idx = get_fn_index(fn, fn_len, FN0_RET_TYPE_INDEX);
  165. push_i32(&instruction_body, (int32_t) &reg32s[reg]);
  166. call_fn(&instruction_body, fn_idx);
  167. store_aligned_i32(&instruction_body);
  168. }
  169. void gen_fn1(char const* fn, uint8_t fn_len, int32_t arg0)
  170. {
  171. int32_t fn_idx = get_fn_index(fn, fn_len, FN1_TYPE_INDEX);
  172. push_i32(&instruction_body, arg0);
  173. call_fn(&instruction_body, fn_idx);
  174. }
  175. void gen_fn1_reg16(char const* fn, uint8_t fn_len, int32_t reg)
  176. {
  177. // generates: fn(reg16[reg])
  178. int32_t fn_idx = get_fn_index(fn, fn_len, FN1_TYPE_INDEX);
  179. load_aligned_u16(&instruction_body, (int32_t) &reg16[reg]);
  180. call_fn(&instruction_body, fn_idx);
  181. }
  182. void gen_fn1_reg32s(char const* fn, uint8_t fn_len, int32_t reg)
  183. {
  184. // generates: fn(reg32s[reg])
  185. int32_t fn_idx = get_fn_index(fn, fn_len, FN1_TYPE_INDEX);
  186. load_aligned_i32(&instruction_body, (int32_t) &reg32s[reg]);
  187. call_fn(&instruction_body, fn_idx);
  188. }
  189. void gen_fn2(char const* fn, uint8_t fn_len, int32_t arg0, int32_t arg1)
  190. {
  191. int32_t fn_idx = get_fn_index(fn, fn_len, FN2_TYPE_INDEX);
  192. push_i32(&instruction_body, arg0);
  193. push_i32(&instruction_body, arg1);
  194. call_fn(&instruction_body, fn_idx);
  195. }
  196. void gen_fn3(char const* fn, uint8_t fn_len, int32_t arg0, int32_t arg1, int32_t arg2)
  197. {
  198. int32_t fn_idx = get_fn_index(fn, fn_len, FN3_TYPE_INDEX);
  199. push_i32(&instruction_body, arg0);
  200. push_i32(&instruction_body, arg1);
  201. push_i32(&instruction_body, arg2);
  202. call_fn(&instruction_body, fn_idx);
  203. }
  204. void gen_add_i32(void)
  205. {
  206. add_i32(&instruction_body);
  207. }
  208. void gen_eqz_i32(void)
  209. {
  210. write_raw_u8(&instruction_body, OP_I32EQZ);
  211. }
  212. void gen_if_void(void)
  213. {
  214. write_raw_u8(&instruction_body, OP_IF);
  215. write_raw_u8(&instruction_body, TYPE_VOID_BLOCK);
  216. }
  217. void gen_else(void)
  218. {
  219. write_raw_u8(&instruction_body, OP_ELSE);
  220. }
  221. void gen_loop_void(void)
  222. {
  223. write_raw_u8(&instruction_body, OP_LOOP);
  224. write_raw_u8(&instruction_body, TYPE_VOID_BLOCK);
  225. }
  226. void gen_block_void(void)
  227. {
  228. write_raw_u8(&instruction_body, OP_BLOCK);
  229. write_raw_u8(&instruction_body, TYPE_VOID_BLOCK);
  230. }
  231. void gen_block_end(void)
  232. {
  233. write_raw_u8(&instruction_body, OP_END);
  234. }
  235. void gen_return(void)
  236. {
  237. write_raw_u8(&instruction_body, OP_RETURN);
  238. }
  239. // Generate a br_table where an input of [i] will branch [i]th outer block,
  240. // where [i] is passed on the wasm stack
  241. void gen_switch(int32_t cases_count)
  242. {
  243. write_raw_u8(&instruction_body, OP_BRTABLE);
  244. write_leb_u32(&instruction_body, cases_count);
  245. for(int32_t i = 0; i < cases_count + 1; i++)
  246. {
  247. write_leb_u32(&instruction_body, i);
  248. }
  249. }
  250. void gen_br(int32_t depth)
  251. {
  252. write_raw_u8(&instruction_body, OP_BR);
  253. write_leb_i32(&instruction_body, depth);
  254. }
  255. void gen_get_local(int32_t idx)
  256. {
  257. write_raw_u8(&instruction_body, OP_GETLOCAL);
  258. write_leb_i32(&instruction_body, idx);
  259. }
  260. void gen_set_local(int32_t idx)
  261. {
  262. write_raw_u8(&instruction_body, OP_SETLOCAL);
  263. write_leb_i32(&instruction_body, idx);
  264. }
  265. void gen_const_i32(int32_t v)
  266. {
  267. push_i32(&instruction_body, v);
  268. }
  269. void gen_unreachable(void)
  270. {
  271. write_raw_u8(&instruction_body, OP_UNREACHABLE);
  272. }
  273. #define MODRM_ENTRY(n, work)\
  274. case (n) | 0 << 3:\
  275. case (n) | 1 << 3:\
  276. case (n) | 2 << 3:\
  277. case (n) | 3 << 3:\
  278. case (n) | 4 << 3:\
  279. case (n) | 5 << 3:\
  280. case (n) | 6 << 3:\
  281. case (n) | 7 << 3:\
  282. (work); break;
  283. #define MODRM_ENTRY16_0(row, seg, reg1, reg2)\
  284. MODRM_ENTRY(0x00 | (row), gen_modrm_entry_0((seg), (reg1), (reg2), 0))\
  285. MODRM_ENTRY(0x40 | (row), gen_modrm_entry_0((seg), (reg1), (reg2), read_imm8s()))\
  286. MODRM_ENTRY(0x80 | (row), gen_modrm_entry_0((seg), (reg1), (reg2), read_imm16()))
  287. #define MODRM_ENTRY16_1(row, seg, reg)\
  288. MODRM_ENTRY(0x00 | (row), gen_modrm_entry_1(seg, reg, 0))\
  289. MODRM_ENTRY(0x40 | (row), gen_modrm_entry_1(seg, reg, read_imm8s()))\
  290. MODRM_ENTRY(0x80 | (row), gen_modrm_entry_1(seg, reg, read_imm16()))
  291. static void inline gen_modrm_entry_0(int32_t segment, int32_t reg16_idx_1, int32_t reg16_idx_2, int32_t imm)
  292. {
  293. // generates: fn( ( reg1 + reg2 + imm ) & 0xFFFF )
  294. load_aligned_u16(&instruction_body, reg16_idx_1);
  295. load_aligned_u16(&instruction_body, reg16_idx_2);
  296. add_i32(&instruction_body);
  297. if(imm)
  298. {
  299. push_i32(&instruction_body, imm);
  300. add_i32(&instruction_body);
  301. }
  302. push_i32(&instruction_body, 0xFFFF);
  303. and_i32(&instruction_body);
  304. jit_add_seg_offset(segment);
  305. }
  306. static void gen_modrm_entry_1(int32_t segment, int32_t reg16_idx, int32_t imm)
  307. {
  308. // generates: fn ( ( reg + imm ) & 0xFFFF )
  309. load_aligned_u16(&instruction_body, reg16_idx);
  310. if(imm)
  311. {
  312. push_i32(&instruction_body, imm);
  313. add_i32(&instruction_body);
  314. }
  315. push_i32(&instruction_body, 0xFFFF);
  316. and_i32(&instruction_body);
  317. jit_add_seg_offset(segment);
  318. }
  319. static bool can_optimize_get_seg(int32_t segment)
  320. {
  321. return (segment == DS || segment == SS) && has_flat_segmentation();
  322. }
  323. /*
  324. * Note: Requires an existing value to be on the WASM stack! Based on optimization possibilities,
  325. * the value will be consumed and added to get_seg(segment), or it'll be left as-is
  326. */
  327. static void jit_add_seg_offset(int32_t default_segment)
  328. {
  329. int32_t prefix = *prefixes & PREFIX_MASK_SEGMENT;
  330. int32_t seg = prefix ? prefix - 1 : default_segment;
  331. if(can_optimize_get_seg(seg) || prefix == SEG_PREFIX_ZERO)
  332. {
  333. return;
  334. }
  335. push_i32(&instruction_body, seg);
  336. call_fn(&instruction_body, fn_get_seg_idx);
  337. add_i32(&instruction_body);
  338. }
  339. static void gen_modrm_entry_2()
  340. {
  341. push_i32(&instruction_body, read_imm16());
  342. jit_add_seg_offset(DS);
  343. }
  344. static void jit_resolve_modrm16_(int32_t modrm_byte)
  345. {
  346. switch(modrm_byte)
  347. {
  348. // The following casts cause some weird issue with emscripten and cause
  349. // a performance hit. XXX: look into this later.
  350. MODRM_ENTRY16_0(0, DS, (int32_t)(reg16 + BX), (int32_t)(reg16 + SI))
  351. MODRM_ENTRY16_0(1, DS, (int32_t)(reg16 + BX), (int32_t)(reg16 + DI))
  352. MODRM_ENTRY16_0(2, SS, (int32_t)(reg16 + BP), (int32_t)(reg16 + SI))
  353. MODRM_ENTRY16_0(3, SS, (int32_t)(reg16 + BP), (int32_t)(reg16 + DI))
  354. MODRM_ENTRY16_1(4, DS, (int32_t)(reg16 + SI))
  355. MODRM_ENTRY16_1(5, DS, (int32_t)(reg16 + DI))
  356. // special case
  357. MODRM_ENTRY(0x00 | 6, gen_modrm_entry_2())
  358. MODRM_ENTRY(0x40 | 6, gen_modrm_entry_1(SS, (int32_t)(reg16 + BP), read_imm8s()))
  359. MODRM_ENTRY(0x80 | 6, gen_modrm_entry_1(SS, (int32_t)(reg16 + BP), read_imm16()))
  360. MODRM_ENTRY16_1(7, DS, (int32_t)(reg16 + BX))
  361. default:
  362. assert(false);
  363. }
  364. }
  365. #define MODRM_ENTRY32_0(row, seg, reg)\
  366. MODRM_ENTRY(0x00 | (row), gen_modrm32_entry(seg, reg, 0))\
  367. MODRM_ENTRY(0x40 | (row), gen_modrm32_entry(seg, reg, read_imm8s()))\
  368. MODRM_ENTRY(0x80 | (row), gen_modrm32_entry(seg, reg, read_imm32s()))
  369. static void gen_modrm32_entry(int32_t segment, int32_t reg32s_idx, int32_t imm)
  370. {
  371. // generates: fn ( reg + imm )
  372. load_aligned_i32(&instruction_body, reg32s_idx);
  373. if(imm)
  374. {
  375. push_i32(&instruction_body, imm);
  376. add_i32(&instruction_body);
  377. }
  378. jit_add_seg_offset(segment);
  379. }
  380. static void jit_resolve_sib(bool mod)
  381. {
  382. uint8_t sib_byte = read_imm8();
  383. uint8_t r = sib_byte & 7;
  384. uint8_t m = sib_byte >> 3 & 7;
  385. int32_t base_addr;
  386. int32_t base;
  387. uint8_t seg;
  388. bool base_is_mem_access = true;
  389. if(r == 4)
  390. {
  391. base_addr = (int32_t)(reg32s + ESP);
  392. seg = SS;
  393. }
  394. else if(r == 5)
  395. {
  396. if(mod)
  397. {
  398. base_addr = (int32_t)(reg32s + EBP);
  399. seg = SS;
  400. }
  401. else
  402. {
  403. base = read_imm32s();
  404. seg = DS;
  405. base_is_mem_access = false;
  406. }
  407. }
  408. else
  409. {
  410. base_addr = (int32_t)(reg32s + r);
  411. seg = DS;
  412. }
  413. // generate: get_seg_prefix(seg) + base
  414. // Where base is accessed from memory if base_is_mem_access or written as a constant otherwise
  415. if(base_is_mem_access)
  416. {
  417. load_aligned_i32(&instruction_body, base_addr);
  418. }
  419. else
  420. {
  421. push_i32(&instruction_body, base);
  422. }
  423. jit_add_seg_offset(seg);
  424. // We now have to generate an offset value to add
  425. if(m == 4)
  426. {
  427. // offset is 0, we don't need to add anything
  428. return;
  429. }
  430. // Offset is reg32s[m] << s, where s is:
  431. uint8_t s = sib_byte >> 6 & 3;
  432. load_aligned_i32(&instruction_body, (int32_t)(reg32s + m));
  433. // We don't use push_u32 here either since s will fit in 1 byte
  434. write_raw_u8(&instruction_body, OP_I32CONST);
  435. write_raw_u8(&instruction_body, s);
  436. shl_i32(&instruction_body);
  437. add_i32(&instruction_body);
  438. }
  439. static void modrm32_special_case_1(void)
  440. {
  441. jit_resolve_sib(true);
  442. int32_t imm = read_imm8s();
  443. if(imm)
  444. {
  445. push_i32(&instruction_body, imm);
  446. add_i32(&instruction_body);
  447. }
  448. }
  449. static void modrm32_special_case_2(void)
  450. {
  451. jit_resolve_sib(true);
  452. int32_t imm = read_imm32s();
  453. if(imm)
  454. {
  455. push_i32(&instruction_body, imm);
  456. add_i32(&instruction_body);
  457. }
  458. }
  459. static void gen_modrm32_entry_1()
  460. {
  461. int32_t imm = read_imm32s();
  462. push_i32(&instruction_body, imm);
  463. jit_add_seg_offset(DS);
  464. }
  465. static void jit_resolve_modrm32_(int32_t modrm_byte)
  466. {
  467. switch(modrm_byte)
  468. {
  469. MODRM_ENTRY32_0(0, DS, (int32_t)(reg32s + EAX))
  470. MODRM_ENTRY32_0(1, DS, (int32_t)(reg32s + ECX))
  471. MODRM_ENTRY32_0(2, DS, (int32_t)(reg32s + EDX))
  472. MODRM_ENTRY32_0(3, DS, (int32_t)(reg32s + EBX))
  473. // special cases
  474. MODRM_ENTRY(0x00 | 4, jit_resolve_sib(false))
  475. MODRM_ENTRY(0x40 | 4, modrm32_special_case_1())
  476. MODRM_ENTRY(0x80 | 4, modrm32_special_case_2())
  477. MODRM_ENTRY(0x00 | 5, gen_modrm32_entry_1())
  478. MODRM_ENTRY(0x40 | 5, gen_modrm32_entry(SS, (int32_t)(reg32s + EBP), read_imm8s()))
  479. MODRM_ENTRY(0x80 | 5, gen_modrm32_entry(SS, (int32_t)(reg32s + EBP), read_imm32s()))
  480. MODRM_ENTRY32_0(6, DS, (int32_t)(reg32s + ESI))
  481. MODRM_ENTRY32_0(7, DS, (int32_t)(reg32s + EDI))
  482. default:
  483. assert(false);
  484. }
  485. }
  486. #undef MODRM_ENTRY
  487. // This function leaves a value on the wasm stack, to be consumed by one of the
  488. // gen_modrm_fn* functions below
  489. void gen_modrm_resolve(int32_t modrm_byte)
  490. {
  491. if(is_asize_32())
  492. {
  493. jit_resolve_modrm32_(modrm_byte);
  494. }
  495. else
  496. {
  497. jit_resolve_modrm16_(modrm_byte);
  498. }
  499. }
  500. void gen_modrm_fn2(char const* fn, uint8_t fn_len, int32_t arg0, int32_t arg1)
  501. {
  502. // generates: fn( _, arg0, arg1 )
  503. push_i32(&instruction_body, arg0);
  504. push_i32(&instruction_body, arg1);
  505. int32_t fn_idx = get_fn_index(fn, fn_len, FN3_TYPE_INDEX);
  506. call_fn(&instruction_body, fn_idx);
  507. }
  508. void gen_modrm_fn1(char const* fn, uint8_t fn_len, int32_t arg0)
  509. {
  510. // generates: fn( _, arg0 )
  511. push_i32(&instruction_body, arg0);
  512. int32_t fn_idx = get_fn_index(fn, fn_len, FN2_TYPE_INDEX);
  513. call_fn(&instruction_body, fn_idx);
  514. }
  515. void gen_modrm_fn0(char const* fn, uint8_t fn_len)
  516. {
  517. // generates: fn( _ )
  518. int32_t fn_idx = get_fn_index(fn, fn_len, FN1_TYPE_INDEX);
  519. call_fn(&instruction_body, fn_idx);
  520. }
  521. void gen_commit_instruction_body_to_cs(void)
  522. {
  523. append_buffer(&cs, &instruction_body);
  524. instruction_body.ptr = instruction_body.start;
  525. }