Assembler.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Platform.h>
  8. #include <AK/Vector.h>
  9. #if ARCH(X86_64)
  10. namespace JIT {
  11. struct Assembler {
  12. Assembler(Vector<u8>& output)
  13. : m_output(output)
  14. {
  15. }
  16. Vector<u8>& m_output;
  17. enum class Reg {
  18. RAX = 0,
  19. RCX = 1,
  20. RDX = 2,
  21. RBX = 3,
  22. RSP = 4,
  23. RBP = 5,
  24. RSI = 6,
  25. RDI = 7,
  26. R8 = 8,
  27. R9 = 9,
  28. R10 = 10,
  29. R11 = 11,
  30. R12 = 12,
  31. R13 = 13,
  32. R14 = 14,
  33. R15 = 15,
  34. };
  35. struct Operand {
  36. enum class Type {
  37. Reg,
  38. Imm,
  39. Mem64BaseAndOffset,
  40. };
  41. Type type {};
  42. Reg reg {};
  43. u64 offset_or_immediate { 0 };
  44. static Operand Register(Reg reg)
  45. {
  46. Operand operand;
  47. operand.type = Type::Reg;
  48. operand.reg = reg;
  49. return operand;
  50. }
  51. static Operand Imm(u64 imm)
  52. {
  53. Operand operand;
  54. operand.type = Type::Imm;
  55. operand.offset_or_immediate = imm;
  56. return operand;
  57. }
  58. static Operand Mem64BaseAndOffset(Reg base, u64 offset)
  59. {
  60. Operand operand;
  61. operand.type = Type::Mem64BaseAndOffset;
  62. operand.reg = base;
  63. operand.offset_or_immediate = offset;
  64. return operand;
  65. }
  66. bool fits_in_u8() const
  67. {
  68. VERIFY(type == Type::Imm);
  69. return offset_or_immediate <= NumericLimits<u8>::max();
  70. }
  71. bool fits_in_u32() const
  72. {
  73. VERIFY(type == Type::Imm);
  74. return offset_or_immediate <= NumericLimits<u32>::max();
  75. }
  76. bool fits_in_i8() const
  77. {
  78. VERIFY(type == Type::Imm);
  79. return (offset_or_immediate <= NumericLimits<i8>::max()) || (((~offset_or_immediate) & NumericLimits<i8>::min()) == 0);
  80. }
  81. bool fits_in_i32() const
  82. {
  83. VERIFY(type == Type::Imm);
  84. return (offset_or_immediate <= NumericLimits<i32>::max()) || (((~offset_or_immediate) & NumericLimits<i32>::min()) == 0);
  85. }
  86. };
  87. enum class Condition {
  88. EqualTo = 0x4,
  89. NotEqualTo = 0x5,
  90. UnsignedGreaterThan = 0x7,
  91. UnsignedGreaterThanOrEqualTo = 0x3,
  92. UnsignedLessThan = 0x2,
  93. UnsignedLessThanOrEqualTo = 0x6,
  94. SignedGreaterThan = 0xF,
  95. SignedGreaterThanOrEqualTo = 0xD,
  96. SignedLessThan = 0xC,
  97. SignedLessThanOrEqualTo = 0xE,
  98. };
  99. static constexpr u8 encode_reg(Reg reg)
  100. {
  101. return to_underlying(reg) & 0x7;
  102. }
  103. enum class Patchable {
  104. Yes,
  105. No,
  106. };
  107. union ModRM {
  108. static constexpr u8 Mem = 0b00;
  109. static constexpr u8 MemDisp8 = 0b01;
  110. static constexpr u8 MemDisp32 = 0b10;
  111. static constexpr u8 Reg = 0b11;
  112. struct {
  113. u8 rm : 3;
  114. u8 reg : 3;
  115. u8 mode : 2;
  116. };
  117. u8 raw;
  118. };
  119. void emit_modrm_slash(u8 slash, Operand rm, Patchable patchable = Patchable::No)
  120. {
  121. ModRM raw;
  122. raw.rm = encode_reg(rm.reg);
  123. raw.reg = slash;
  124. emit_modrm(raw, rm, patchable);
  125. }
  126. void emit_modrm_rm(Operand dst, Operand src, Patchable patchable = Patchable::No)
  127. {
  128. VERIFY(dst.type == Operand::Type::Reg);
  129. ModRM raw {};
  130. raw.reg = encode_reg(dst.reg);
  131. raw.rm = encode_reg(src.reg);
  132. emit_modrm(raw, src, patchable);
  133. }
  134. void emit_modrm_mr(Operand dst, Operand src, Patchable patchable = Patchable::No)
  135. {
  136. VERIFY(src.type == Operand::Type::Reg);
  137. ModRM raw {};
  138. raw.reg = encode_reg(src.reg);
  139. raw.rm = encode_reg(dst.reg);
  140. emit_modrm(raw, dst, patchable);
  141. }
  142. void emit_modrm(ModRM raw, Operand rm, Patchable patchable)
  143. {
  144. // FIXME: rm:100 (RSP) is reserved as the SIB marker
  145. VERIFY(rm.type != Operand::Type::Imm);
  146. switch (rm.type) {
  147. case Operand::Type::Reg:
  148. // FIXME: There is mod:00,rm:101(EBP?) -> disp32, that might be something else
  149. raw.mode = ModRM::Reg;
  150. emit8(raw.raw);
  151. break;
  152. case Operand::Type::Mem64BaseAndOffset: {
  153. auto disp = rm.offset_or_immediate;
  154. if (patchable == Patchable::Yes) {
  155. raw.mode = ModRM::MemDisp32;
  156. emit8(raw.raw);
  157. emit32(disp);
  158. } else if (disp == 0) {
  159. raw.mode = ModRM::Mem;
  160. emit8(raw.raw);
  161. } else if (static_cast<i64>(disp) >= -128 && disp <= 127) {
  162. raw.mode = ModRM::MemDisp8;
  163. emit8(raw.raw);
  164. emit8(disp & 0xff);
  165. } else {
  166. raw.mode = ModRM::MemDisp32;
  167. emit8(raw.raw);
  168. emit32(disp);
  169. }
  170. break;
  171. }
  172. case Operand::Type::Imm:
  173. VERIFY_NOT_REACHED();
  174. }
  175. }
  176. void shift_right(Operand dst, Operand count)
  177. {
  178. VERIFY(dst.type == Operand::Type::Reg);
  179. VERIFY(count.type == Operand::Type::Imm);
  180. VERIFY(count.fits_in_u8());
  181. emit8(0x48 | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  182. emit8(0xc1);
  183. emit8(0xe8 | encode_reg(dst.reg));
  184. emit8(count.offset_or_immediate);
  185. }
  186. void mov(Operand dst, Operand src, Patchable patchable = Patchable::No)
  187. {
  188. if (dst.type == Operand::Type::Reg && src.type == Operand::Type::Reg) {
  189. if (src.reg == dst.reg)
  190. return;
  191. emit8(0x48
  192. | ((to_underlying(src.reg) >= 8) ? 1 << 2 : 0)
  193. | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  194. emit8(0x89);
  195. emit8(0xc0 | (encode_reg(src.reg) << 3) | encode_reg(dst.reg));
  196. return;
  197. }
  198. if (dst.type == Operand::Type::Reg && src.type == Operand::Type::Imm) {
  199. if (patchable == Patchable::No) {
  200. if (src.offset_or_immediate == 0) {
  201. // xor dst, dst
  202. emit8(0x48 | ((to_underlying(dst.reg) >= 8) ? (1 << 0 | 1 << 2) : 0));
  203. emit8(0x31);
  204. emit8(0xc0 | (encode_reg(dst.reg) << 3) | encode_reg(dst.reg));
  205. return;
  206. }
  207. if (src.fits_in_u32()) {
  208. if (dst.reg > Reg::RDI)
  209. emit8(0x41);
  210. emit8(0xb8 | encode_reg(dst.reg));
  211. emit32(src.offset_or_immediate);
  212. return;
  213. }
  214. }
  215. emit8(0x48 | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  216. emit8(0xb8 | encode_reg(dst.reg));
  217. emit64(src.offset_or_immediate);
  218. return;
  219. }
  220. if (dst.type == Operand::Type::Mem64BaseAndOffset && src.type == Operand::Type::Reg) {
  221. emit8(0x48
  222. | ((to_underlying(src.reg) >= 8) ? 1 << 2 : 0)
  223. | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  224. emit8(0x89);
  225. if (dst.reg <= Reg::RDI && dst.offset_or_immediate == 0) {
  226. emit8(0x00 | (encode_reg(src.reg) << 3) | encode_reg(dst.reg));
  227. } else if (dst.offset_or_immediate <= 127) {
  228. emit8(0x40 | (encode_reg(src.reg) << 3) | encode_reg(dst.reg));
  229. emit8(dst.offset_or_immediate);
  230. } else {
  231. emit8(0x80 | (encode_reg(src.reg) << 3) | encode_reg(dst.reg));
  232. emit32(dst.offset_or_immediate);
  233. }
  234. return;
  235. }
  236. if (dst.type == Operand::Type::Reg && src.type == Operand::Type::Mem64BaseAndOffset) {
  237. emit8(0x48
  238. | ((to_underlying(dst.reg) >= 8) ? 1 << 2 : 0)
  239. | ((to_underlying(src.reg) >= 8) ? 1 << 0 : 0));
  240. emit8(0x8b);
  241. if (src.reg <= Reg::RDI && src.offset_or_immediate == 0) {
  242. emit8(0x00 | (encode_reg(dst.reg) << 3) | encode_reg(src.reg));
  243. } else if (src.offset_or_immediate <= 127) {
  244. emit8(0x40 | (encode_reg(dst.reg) << 3) | encode_reg(src.reg));
  245. emit8(src.offset_or_immediate);
  246. } else {
  247. emit8(0x80 | (encode_reg(dst.reg) << 3) | encode_reg(src.reg));
  248. emit32(src.offset_or_immediate);
  249. }
  250. return;
  251. }
  252. VERIFY_NOT_REACHED();
  253. }
  254. void emit8(u8 value)
  255. {
  256. m_output.append(value);
  257. }
  258. void emit32(u32 value)
  259. {
  260. m_output.append((value >> 0) & 0xff);
  261. m_output.append((value >> 8) & 0xff);
  262. m_output.append((value >> 16) & 0xff);
  263. m_output.append((value >> 24) & 0xff);
  264. }
  265. void emit64(u64 value)
  266. {
  267. m_output.append((value >> 0) & 0xff);
  268. m_output.append((value >> 8) & 0xff);
  269. m_output.append((value >> 16) & 0xff);
  270. m_output.append((value >> 24) & 0xff);
  271. m_output.append((value >> 32) & 0xff);
  272. m_output.append((value >> 40) & 0xff);
  273. m_output.append((value >> 48) & 0xff);
  274. m_output.append((value >> 56) & 0xff);
  275. }
  276. struct Label {
  277. Optional<size_t> offset_of_label_in_instruction_stream;
  278. Vector<size_t> jump_slot_offsets_in_instruction_stream;
  279. void add_jump(Assembler& assembler, size_t offset)
  280. {
  281. jump_slot_offsets_in_instruction_stream.append(offset);
  282. if (offset_of_label_in_instruction_stream.has_value())
  283. link_jump(assembler, offset);
  284. }
  285. void link(Assembler& assembler)
  286. {
  287. link_to(assembler, assembler.m_output.size());
  288. }
  289. void link_to(Assembler& assembler, size_t link_offset)
  290. {
  291. VERIFY(!offset_of_label_in_instruction_stream.has_value());
  292. offset_of_label_in_instruction_stream = link_offset;
  293. for (auto offset_in_instruction_stream : jump_slot_offsets_in_instruction_stream)
  294. link_jump(assembler, offset_in_instruction_stream);
  295. }
  296. private:
  297. void link_jump(Assembler& assembler, size_t offset_in_instruction_stream)
  298. {
  299. auto offset = offset_of_label_in_instruction_stream.value() - offset_in_instruction_stream;
  300. auto jump_slot = offset_in_instruction_stream - 4;
  301. assembler.m_output[jump_slot + 0] = (offset >> 0) & 0xff;
  302. assembler.m_output[jump_slot + 1] = (offset >> 8) & 0xff;
  303. assembler.m_output[jump_slot + 2] = (offset >> 16) & 0xff;
  304. assembler.m_output[jump_slot + 3] = (offset >> 24) & 0xff;
  305. }
  306. };
  307. [[nodiscard]] Label jump()
  308. {
  309. // jmp target (RIP-relative 32-bit offset)
  310. emit8(0xe9);
  311. emit32(0xdeadbeef);
  312. Assembler::Label label {};
  313. label.add_jump(*this, m_output.size());
  314. return label;
  315. }
  316. void jump(Label& label)
  317. {
  318. // jmp target (RIP-relative 32-bit offset)
  319. emit8(0xe9);
  320. emit32(0xdeadbeef);
  321. label.add_jump(*this, m_output.size());
  322. }
  323. void jump(Operand op)
  324. {
  325. if (op.type == Operand::Type::Reg) {
  326. if (to_underlying(op.reg) >= 8)
  327. emit8(0x41);
  328. emit8(0xff);
  329. emit8(0xe0 | encode_reg(op.reg));
  330. } else {
  331. VERIFY_NOT_REACHED();
  332. }
  333. }
  334. void verify_not_reached()
  335. {
  336. // ud2
  337. emit8(0x0f);
  338. emit8(0x0b);
  339. }
  340. void cmp(Operand lhs, Operand rhs)
  341. {
  342. if (rhs.type == Operand::Type::Imm && rhs.offset_or_immediate == 0) {
  343. test(lhs, lhs);
  344. } else if (lhs.type == Operand::Type::Reg && rhs.type == Operand::Type::Reg) {
  345. emit8(0x48
  346. | ((to_underlying(rhs.reg) >= 8) ? 1 << 2 : 0)
  347. | ((to_underlying(lhs.reg) >= 8) ? 1 << 0 : 0));
  348. emit8(0x39);
  349. emit8(0xc0 | (encode_reg(rhs.reg) << 3) | encode_reg(lhs.reg));
  350. } else if (lhs.type == Operand::Type::Reg && rhs.type == Operand::Type::Imm && rhs.fits_in_i8()) {
  351. emit8(0x48 | ((to_underlying(lhs.reg) >= 8) ? 1 << 0 : 0));
  352. emit8(0x83);
  353. emit8(0xf8 | encode_reg(lhs.reg));
  354. emit8(rhs.offset_or_immediate);
  355. } else if (lhs.type == Operand::Type::Reg && rhs.type == Operand::Type::Imm && rhs.fits_in_i32()) {
  356. emit8(0x48 | ((to_underlying(lhs.reg) >= 8) ? 1 << 0 : 0));
  357. emit8(0x81);
  358. emit8(0xf8 | encode_reg(lhs.reg));
  359. emit32(rhs.offset_or_immediate);
  360. } else {
  361. VERIFY_NOT_REACHED();
  362. }
  363. }
  364. void test(Operand lhs, Operand rhs)
  365. {
  366. if (lhs.type == Operand::Type::Reg && rhs.type == Operand::Type::Reg) {
  367. emit8(0x48
  368. | ((to_underlying(rhs.reg) >= 8) ? 1 << 2 : 0)
  369. | ((to_underlying(lhs.reg) >= 8) ? 1 << 0 : 0));
  370. emit8(0x85);
  371. emit8(0xc0 | (encode_reg(rhs.reg) << 3) | encode_reg(lhs.reg));
  372. } else if (lhs.type == Operand::Type::Reg && rhs.type == Operand::Type::Imm) {
  373. VERIFY(rhs.fits_in_i32());
  374. emit8(0x48 | ((to_underlying(lhs.reg) >= 8) ? 1 << 0 : 0));
  375. emit8(0xf7);
  376. emit8(0xc0 | encode_reg(lhs.reg));
  377. emit32(rhs.offset_or_immediate);
  378. } else {
  379. VERIFY_NOT_REACHED();
  380. }
  381. }
  382. void jump_if(Operand lhs, Condition condition, Operand rhs, Label& label)
  383. {
  384. cmp(lhs, rhs);
  385. emit8(0x0F);
  386. emit8(0x80 | to_underlying(condition));
  387. emit32(0xdeadbeef);
  388. label.add_jump(*this, m_output.size());
  389. }
  390. void sign_extend_32_to_64_bits(Reg reg)
  391. {
  392. // movsxd (reg as 64-bit), (reg as 32-bit)
  393. emit8(0x48 | ((to_underlying(reg) >= 8) ? 1 << 0 : 0));
  394. emit8(0x63);
  395. emit8(0xc0 | (encode_reg(reg) << 3) | encode_reg(reg));
  396. }
  397. void bitwise_and(Operand dst, Operand src)
  398. {
  399. // and dst,src
  400. if (dst.type == Operand::Type::Reg && src.type == Operand::Type::Reg) {
  401. emit8(0x48
  402. | ((to_underlying(src.reg) >= 8) ? 1 << 2 : 0)
  403. | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  404. emit8(0x21);
  405. emit8(0xc0 | (encode_reg(src.reg) << 3) | encode_reg(dst.reg));
  406. } else if (dst.type == Operand::Type::Reg && src.type == Operand::Type::Imm && src.fits_in_i8()) {
  407. emit8(0x48 | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  408. emit8(0x83);
  409. emit8(0xe0 | encode_reg(dst.reg));
  410. emit8(src.offset_or_immediate);
  411. } else if (dst.type == Operand::Type::Reg && src.type == Operand::Type::Imm && src.fits_in_i32()) {
  412. emit8(0x48 | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  413. emit8(0x81);
  414. emit8(0xe0 | encode_reg(dst.reg));
  415. emit32(src.offset_or_immediate);
  416. } else {
  417. VERIFY_NOT_REACHED();
  418. }
  419. }
  420. void bitwise_or(Operand dst, Operand src)
  421. {
  422. // or dst,src
  423. if (dst.type == Operand::Type::Reg && src.type == Operand::Type::Reg) {
  424. emit8(0x48
  425. | ((to_underlying(src.reg) >= 8) ? 1 << 2 : 0)
  426. | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  427. emit8(0x09);
  428. emit8(0xc0 | (encode_reg(src.reg) << 3) | encode_reg(dst.reg));
  429. } else if (dst.type == Operand::Type::Reg && src.type == Operand::Type::Imm && src.fits_in_i8()) {
  430. emit8(0x48 | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  431. emit8(0x83);
  432. emit8(0xc8 | encode_reg(dst.reg));
  433. emit8(src.offset_or_immediate);
  434. } else if (dst.type == Operand::Type::Reg && src.type == Operand::Type::Imm && src.fits_in_i32()) {
  435. emit8(0x48 | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  436. emit8(0x81);
  437. emit8(0xc8 | encode_reg(dst.reg));
  438. emit32(src.offset_or_immediate);
  439. } else {
  440. VERIFY_NOT_REACHED();
  441. }
  442. }
  443. void enter()
  444. {
  445. push_callee_saved_registers();
  446. push(Operand::Register(Reg::RBP));
  447. mov(Operand::Register(Reg::RBP), Operand::Register(Reg::RSP));
  448. }
  449. void exit()
  450. {
  451. // leave
  452. emit8(0xc9);
  453. pop_callee_saved_registers();
  454. // ret
  455. emit8(0xc3);
  456. }
  457. void push_callee_saved_registers()
  458. {
  459. // FIXME: Don't push RBX twice :^)
  460. push(Operand::Register(Reg::RBX));
  461. push(Operand::Register(Reg::RBX));
  462. push(Operand::Register(Reg::R12));
  463. push(Operand::Register(Reg::R13));
  464. push(Operand::Register(Reg::R14));
  465. push(Operand::Register(Reg::R15));
  466. }
  467. void pop_callee_saved_registers()
  468. {
  469. pop(Operand::Register(Reg::R15));
  470. pop(Operand::Register(Reg::R14));
  471. pop(Operand::Register(Reg::R13));
  472. pop(Operand::Register(Reg::R12));
  473. // FIXME: Don't pop RBX twice :^)
  474. pop(Operand::Register(Reg::RBX));
  475. pop(Operand::Register(Reg::RBX));
  476. }
  477. void push(Operand op)
  478. {
  479. if (op.type == Operand::Type::Reg) {
  480. if (to_underlying(op.reg) >= 8)
  481. emit8(0x49);
  482. emit8(0x50 | encode_reg(op.reg));
  483. } else if (op.type == Operand::Type::Imm) {
  484. if (op.fits_in_i8()) {
  485. emit8(0x6a);
  486. emit8(op.offset_or_immediate);
  487. } else if (op.fits_in_i32()) {
  488. emit8(0x68);
  489. emit32(op.offset_or_immediate);
  490. } else {
  491. VERIFY_NOT_REACHED();
  492. }
  493. } else {
  494. VERIFY_NOT_REACHED();
  495. }
  496. }
  497. void pop(Operand op)
  498. {
  499. if (op.type == Operand::Type::Reg) {
  500. if (to_underlying(op.reg) >= 8)
  501. emit8(0x49);
  502. emit8(0x58 | encode_reg(op.reg));
  503. } else {
  504. VERIFY_NOT_REACHED();
  505. }
  506. }
  507. void add(Operand dst, Operand src)
  508. {
  509. if (dst.type == Operand::Type::Reg && src.type == Operand::Type::Reg) {
  510. emit8(0x48
  511. | ((to_underlying(src.reg) >= 8) ? 1 << 2 : 0)
  512. | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  513. emit8(0x01);
  514. emit8(0xc0 | (encode_reg(src.reg) << 3) | encode_reg(dst.reg));
  515. } else if (dst.type == Operand::Type::Reg && src.type == Operand::Type::Imm && src.fits_in_i8()) {
  516. emit8(0x48 | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  517. emit8(0x83);
  518. emit8(0xc0 | encode_reg(dst.reg));
  519. emit8(src.offset_or_immediate);
  520. } else if (dst.type == Operand::Type::Reg && src.type == Operand::Type::Imm && src.fits_in_i32()) {
  521. emit8(0x48 | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  522. emit8(0x81);
  523. emit8(0xc0 | encode_reg(dst.reg));
  524. emit32(src.offset_or_immediate);
  525. } else {
  526. VERIFY_NOT_REACHED();
  527. }
  528. }
  529. void add32(Operand dst, Operand src, Optional<Label&> label)
  530. {
  531. if (dst.type == Operand::Type::Reg && to_underlying(dst.reg) < 8 && src.type == Operand::Type::Reg && to_underlying(src.reg) < 8) {
  532. emit8(0x01);
  533. emit8(0xc0 | (encode_reg(src.reg) << 3) | encode_reg(dst.reg));
  534. } else if (dst.type == Operand::Type::Reg && to_underlying(dst.reg) < 8 && src.type == Operand::Type::Imm && src.fits_in_i8()) {
  535. emit8(0x83);
  536. emit8(0xc0 | encode_reg(dst.reg));
  537. emit8(src.offset_or_immediate);
  538. } else if (dst.type == Operand::Type::Reg && to_underlying(dst.reg) < 8 && src.type == Operand::Type::Imm && src.fits_in_i32()) {
  539. emit8(0x81);
  540. emit8(0xc0 | encode_reg(dst.reg));
  541. emit32(src.offset_or_immediate);
  542. } else {
  543. VERIFY_NOT_REACHED();
  544. }
  545. if (label.has_value()) {
  546. // jo label (RIP-relative 32-bit offset)
  547. emit8(0x0f);
  548. emit8(0x80);
  549. emit32(0xdeadbeef);
  550. label->add_jump(*this, m_output.size());
  551. }
  552. }
  553. void sub(Operand dst, Operand src)
  554. {
  555. if (dst.type == Operand::Type::Reg && src.type == Operand::Type::Reg) {
  556. emit8(0x48
  557. | ((to_underlying(src.reg) >= 8) ? 1 << 2 : 0)
  558. | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  559. emit8(0x29);
  560. emit8(0xc0 | (encode_reg(src.reg) << 3) | encode_reg(dst.reg));
  561. } else if (dst.type == Operand::Type::Reg && src.type == Operand::Type::Imm && src.fits_in_i8()) {
  562. emit8(0x48 | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  563. emit8(0x83);
  564. emit8(0xe8 | encode_reg(dst.reg));
  565. emit8(src.offset_or_immediate);
  566. } else if (dst.type == Operand::Type::Reg && src.type == Operand::Type::Imm && src.fits_in_i32()) {
  567. emit8(0x48 | ((to_underlying(dst.reg) >= 8) ? 1 << 0 : 0));
  568. emit8(0x81);
  569. emit8(0xe8 | encode_reg(dst.reg));
  570. emit32(src.offset_or_immediate);
  571. } else {
  572. VERIFY_NOT_REACHED();
  573. }
  574. }
  575. // NOTE: It's up to the caller of this function to preserve registers as needed.
  576. void native_call(void* callee, Vector<Operand> const& stack_arguments = {})
  577. {
  578. // Preserve 16-byte stack alignment for non-even amount of stack-passed arguments
  579. if ((stack_arguments.size() % 2) == 1)
  580. push(Operand::Imm(0));
  581. for (auto const& stack_argument : stack_arguments.in_reverse())
  582. push(stack_argument);
  583. // load callee into RAX
  584. mov(Operand::Register(Reg::RAX), Operand::Imm(bit_cast<u64>(callee)));
  585. // call RAX
  586. emit8(0xff);
  587. emit8(0xd0);
  588. if (!stack_arguments.is_empty())
  589. add(Operand::Register(Reg::RSP), Operand::Imm(align_up_to(stack_arguments.size(), 2) * sizeof(void*)));
  590. }
  591. void trap()
  592. {
  593. // int3
  594. emit8(0xcc);
  595. }
  596. };
  597. }
  598. #endif