2021-10-01 18:38:39 -04:00
|
|
|
// For MAP_ANONYMOUS on GNU/Linux
|
|
|
|
#define _GNU_SOURCE 1
|
|
|
|
|
2021-10-27 10:57:08 -04:00
|
|
|
#include <stdarg.h>
|
2020-09-04 15:56:00 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-09-08 16:45:35 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2021-10-01 18:38:39 -04:00
|
|
|
|
2021-10-19 16:43:20 -04:00
|
|
|
// This test executable doesn't compile with the rest of Ruby
|
|
|
|
// so we need to define a rb_bug().
|
|
|
|
_Noreturn
|
2021-10-27 10:57:08 -04:00
|
|
|
static void rb_bug(const char *message, ...)
|
2021-10-19 16:43:20 -04:00
|
|
|
{
|
2021-10-27 10:57:08 -04:00
|
|
|
va_list args;
|
|
|
|
va_start(args, message);
|
|
|
|
vfprintf(stderr, message, args);
|
|
|
|
va_end(args);
|
2021-10-19 16:43:20 -04:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2021-10-01 18:38:39 -04:00
|
|
|
#include "yjit_asm.c"
|
2020-09-04 15:56:00 -04:00
|
|
|
|
2020-09-16 12:37:59 -04:00
|
|
|
// Print the bytes in a code block
|
|
|
|
void print_bytes(codeblock_t* cb)
|
|
|
|
{
|
2021-01-12 14:56:43 -05:00
|
|
|
for (uint32_t i = 0; i < cb->write_pos; ++i)
|
2020-09-16 12:37:59 -04:00
|
|
|
{
|
2021-12-01 14:15:23 -05:00
|
|
|
printf("%02X", (int)*cb_get_ptr(cb, i));
|
2020-09-16 12:37:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2020-09-08 16:45:35 -04:00
|
|
|
// Check that the code block contains the given sequence of bytes
|
|
|
|
void check_bytes(codeblock_t* cb, const char* bytes)
|
|
|
|
{
|
|
|
|
printf("checking encoding: %s\n", bytes);
|
|
|
|
|
|
|
|
size_t len = strlen(bytes);
|
|
|
|
assert (len % 2 == 0);
|
|
|
|
size_t num_bytes = len / 2;
|
|
|
|
|
|
|
|
if (cb->write_pos != num_bytes)
|
|
|
|
{
|
2021-01-12 14:56:43 -05:00
|
|
|
fprintf(stderr, "incorrect encoding length, expected %ld, got %d\n",
|
2020-09-16 12:37:59 -04:00
|
|
|
num_bytes,
|
|
|
|
cb->write_pos
|
|
|
|
);
|
|
|
|
printf("%s\n", bytes);
|
|
|
|
print_bytes(cb);
|
2020-09-08 16:45:35 -04:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:56:43 -05:00
|
|
|
for (uint32_t i = 0; i < num_bytes; ++i)
|
2020-09-08 16:45:35 -04:00
|
|
|
{
|
|
|
|
char byte_str[] = {0, 0, 0, 0};
|
|
|
|
strncpy(byte_str, bytes + (2 * i), 2);
|
|
|
|
char* endptr;
|
|
|
|
long int byte = strtol(byte_str, &endptr, 16);
|
|
|
|
|
2021-12-01 14:15:23 -05:00
|
|
|
uint8_t cb_byte = *cb_get_ptr(cb, i);
|
2020-09-08 16:45:35 -04:00
|
|
|
|
|
|
|
if (cb_byte != byte)
|
|
|
|
{
|
2021-01-12 14:56:43 -05:00
|
|
|
fprintf(stderr, "incorrect encoding at position %d, expected %02X, got %02X\n",
|
2020-09-09 16:45:28 -04:00
|
|
|
i,
|
2020-09-16 12:37:59 -04:00
|
|
|
(int)byte,
|
|
|
|
(int)cb_byte
|
2020-09-09 16:45:28 -04:00
|
|
|
);
|
2020-09-16 12:37:59 -04:00
|
|
|
printf("%s\n", bytes);
|
|
|
|
print_bytes(cb);
|
2020-09-08 16:45:35 -04:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-04 17:10:11 -04:00
|
|
|
|
2021-10-27 10:57:08 -04:00
|
|
|
void run_assembler_tests(void)
|
2020-09-04 15:56:00 -04:00
|
|
|
{
|
|
|
|
printf("Running assembler tests\n");
|
|
|
|
|
2020-09-08 16:45:35 -04:00
|
|
|
codeblock_t cb_obj;
|
|
|
|
codeblock_t* cb = &cb_obj;
|
2020-09-28 15:50:41 -04:00
|
|
|
uint8_t* mem_block = alloc_exec_mem(4096);
|
|
|
|
cb_init(cb, mem_block, 4096);
|
2020-09-08 16:45:35 -04:00
|
|
|
|
2020-09-09 16:45:28 -04:00
|
|
|
// add
|
2020-09-22 11:39:04 -04:00
|
|
|
cb_set_pos(cb, 0); add(cb, CL, imm_opnd(3)); check_bytes(cb, "80C103");
|
|
|
|
cb_set_pos(cb, 0); add(cb, CL, BL); check_bytes(cb, "00D9");
|
|
|
|
cb_set_pos(cb, 0); add(cb, CL, SPL); check_bytes(cb, "4000E1");
|
|
|
|
cb_set_pos(cb, 0); add(cb, CX, BX); check_bytes(cb, "6601D9");
|
2020-09-09 16:45:28 -04:00
|
|
|
cb_set_pos(cb, 0); add(cb, RAX, RBX); check_bytes(cb, "4801D8");
|
2020-09-10 10:57:29 -04:00
|
|
|
cb_set_pos(cb, 0); add(cb, ECX, EDX); check_bytes(cb, "01D1");
|
2020-09-09 16:45:28 -04:00
|
|
|
cb_set_pos(cb, 0); add(cb, RDX, R14); check_bytes(cb, "4C01F2");
|
|
|
|
cb_set_pos(cb, 0); add(cb, mem_opnd(64, RAX, 0), RDX); check_bytes(cb, "480110");
|
|
|
|
cb_set_pos(cb, 0); add(cb, RDX, mem_opnd(64, RAX, 0)); check_bytes(cb, "480310");
|
|
|
|
cb_set_pos(cb, 0); add(cb, RDX, mem_opnd(64, RAX, 8)); check_bytes(cb, "48035008");
|
|
|
|
cb_set_pos(cb, 0); add(cb, RDX, mem_opnd(64, RAX, 255)); check_bytes(cb, "480390FF000000");
|
|
|
|
cb_set_pos(cb, 0); add(cb, mem_opnd(64, RAX, 127), imm_opnd(255)); check_bytes(cb, "4881407FFF000000");
|
2020-09-10 10:57:29 -04:00
|
|
|
cb_set_pos(cb, 0); add(cb, mem_opnd(32, RAX, 0), EDX); check_bytes(cb, "0110");
|
2020-09-09 16:45:28 -04:00
|
|
|
cb_set_pos(cb, 0); add(cb, RSP, imm_opnd(8)); check_bytes(cb, "4883C408");
|
2020-09-10 10:57:29 -04:00
|
|
|
cb_set_pos(cb, 0); add(cb, ECX, imm_opnd(8)); check_bytes(cb, "83C108");
|
|
|
|
cb_set_pos(cb, 0); add(cb, ECX, imm_opnd(255)); check_bytes(cb, "81C1FF000000");
|
2020-09-09 16:45:28 -04:00
|
|
|
|
2020-09-14 16:59:39 -04:00
|
|
|
// and
|
|
|
|
cb_set_pos(cb, 0); and(cb, EBP, R12D); check_bytes(cb, "4421E5");
|
2020-09-24 15:57:53 -04:00
|
|
|
cb_set_pos(cb, 0); and(cb, mem_opnd(64, RAX, 0), imm_opnd(0x08)); check_bytes(cb, "48832008");
|
2020-09-14 16:59:39 -04:00
|
|
|
|
2020-09-09 16:45:28 -04:00
|
|
|
// call
|
2020-09-11 15:36:40 -04:00
|
|
|
{
|
|
|
|
cb_set_pos(cb, 0);
|
2021-01-12 14:56:43 -05:00
|
|
|
uint32_t fn_label = cb_new_label(cb, "foo");
|
2020-09-11 15:36:40 -04:00
|
|
|
call_label(cb, fn_label);
|
|
|
|
cb_link_labels(cb);
|
|
|
|
check_bytes(cb, "E8FBFFFFFF");
|
|
|
|
}
|
2020-09-09 16:45:28 -04:00
|
|
|
cb_set_pos(cb, 0); call(cb, RAX); check_bytes(cb, "FFD0");
|
|
|
|
cb_set_pos(cb, 0); call(cb, mem_opnd(64, RSP, 8)); check_bytes(cb, "FF542408");
|
|
|
|
|
2020-09-14 11:54:25 -04:00
|
|
|
// cmovcc
|
2020-09-15 10:44:46 -04:00
|
|
|
cb_set_pos(cb, 0); cmovg(cb, ESI, EDI); check_bytes(cb, "0F4FF7");
|
|
|
|
cb_set_pos(cb, 0); cmovg(cb, ESI, mem_opnd(32, RBP, 12)); check_bytes(cb, "0F4F750C");
|
|
|
|
cb_set_pos(cb, 0); cmovl(cb, EAX, ECX); check_bytes(cb, "0F4CC1");
|
|
|
|
cb_set_pos(cb, 0); cmovl(cb, RBX, RBP); check_bytes(cb, "480F4CDD");
|
|
|
|
cb_set_pos(cb, 0); cmovle(cb, ESI, mem_opnd(32, RSP, 4)); check_bytes(cb, "0F4E742404");
|
2020-09-14 11:54:25 -04:00
|
|
|
|
|
|
|
// cmp
|
2020-09-22 11:39:04 -04:00
|
|
|
cb_set_pos(cb, 0); cmp(cb, CL, DL); check_bytes(cb, "38D1");
|
2020-09-14 11:54:25 -04:00
|
|
|
cb_set_pos(cb, 0); cmp(cb, ECX, EDI); check_bytes(cb, "39F9");
|
|
|
|
cb_set_pos(cb, 0); cmp(cb, RDX, mem_opnd(64, R12, 0)); check_bytes(cb, "493B1424");
|
|
|
|
cb_set_pos(cb, 0); cmp(cb, RAX, imm_opnd(2)); check_bytes(cb, "4883F802");
|
|
|
|
|
|
|
|
// cqo
|
|
|
|
cb_set_pos(cb, 0); cqo(cb); check_bytes(cb, "4899");
|
|
|
|
|
2020-09-11 16:42:51 -04:00
|
|
|
// div
|
|
|
|
/*
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.div(X86Opnd(EDX)); },
|
|
|
|
"F7F2"
|
|
|
|
);
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.div(X86Opnd(32, RSP, -12)); },
|
|
|
|
"F77424F4"
|
|
|
|
);
|
|
|
|
*/
|
|
|
|
|
2021-02-25 17:01:52 -05:00
|
|
|
// jcc to label
|
2020-09-11 15:10:16 -04:00
|
|
|
{
|
|
|
|
cb_set_pos(cb, 0);
|
2021-01-12 14:56:43 -05:00
|
|
|
uint32_t loop_label = cb_new_label(cb, "loop");
|
2021-02-25 17:01:52 -05:00
|
|
|
jge_label(cb, loop_label);
|
2020-09-11 15:10:16 -04:00
|
|
|
cb_link_labels(cb);
|
|
|
|
check_bytes(cb, "0F8DFAFFFFFF");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
cb_set_pos(cb, 0);
|
2021-01-12 14:56:43 -05:00
|
|
|
uint32_t loop_label = cb_new_label(cb, "loop");
|
2021-02-25 17:01:52 -05:00
|
|
|
jo_label(cb, loop_label);
|
2020-09-11 15:10:16 -04:00
|
|
|
cb_link_labels(cb);
|
|
|
|
check_bytes(cb, "0F80FAFFFFFF");
|
|
|
|
}
|
2020-09-09 17:16:21 -04:00
|
|
|
|
2021-02-25 17:01:52 -05:00
|
|
|
// jmp to label
|
|
|
|
{
|
|
|
|
cb_set_pos(cb, 0);
|
|
|
|
uint32_t loop_label = cb_new_label(cb, "loop");
|
|
|
|
jmp_label(cb, loop_label);
|
|
|
|
cb_link_labels(cb);
|
|
|
|
check_bytes(cb, "E9FBFFFFFF");
|
|
|
|
}
|
|
|
|
|
2020-09-11 15:10:16 -04:00
|
|
|
// jmp with RM operand
|
|
|
|
cb_set_pos(cb, 0); jmp_rm(cb, R12); check_bytes(cb, "41FFE4");
|
2020-09-09 17:16:21 -04:00
|
|
|
|
|
|
|
// lea
|
2020-09-10 11:20:30 -04:00
|
|
|
cb_set_pos(cb, 0); lea(cb, RDX, mem_opnd(64, RCX, 8)); check_bytes(cb, "488D5108");
|
2020-09-16 12:37:59 -04:00
|
|
|
cb_set_pos(cb, 0); lea(cb, RAX, mem_opnd(8, RIP, 0)); check_bytes(cb, "488D0500000000");
|
|
|
|
cb_set_pos(cb, 0); lea(cb, RAX, mem_opnd(8, RIP, 5)); check_bytes(cb, "488D0505000000");
|
|
|
|
cb_set_pos(cb, 0); lea(cb, RDI, mem_opnd(8, RIP, 5)); check_bytes(cb, "488D3D05000000");
|
2020-09-09 17:16:21 -04:00
|
|
|
|
2020-09-09 16:45:28 -04:00
|
|
|
// mov
|
2020-09-10 10:57:29 -04:00
|
|
|
cb_set_pos(cb, 0); mov(cb, EAX, imm_opnd(7)); check_bytes(cb, "B807000000");
|
|
|
|
cb_set_pos(cb, 0); mov(cb, EAX, imm_opnd(-3)); check_bytes(cb, "B8FDFFFFFF");
|
YJIT: use shorter encoding for mov(r64,imm) when unambiguous (#5081)
* YJIT: use shorter encoding for mov(r64,imm) when unambiguous
Previously, for small constants such as `mov(RAX, imm_opnd(Qundef))`,
we emit an instruction with an 8-byte immediate. This form commonly
gets the `movabs` mnemonic.
In 64-bit mode, 32-bit operands get zero extended to 64-bit to fill the
register, so when the immediate is small enough, we can save 4 bytes by
using the `mov` variant that takes a 32-bit immediate and does a zero
extension.
Not implement with this change, there is an imm32 variant of `mov` that
does sign extension we could use. When the constant is negative, we
fallback to the `movabs` form.
In railsbench, this change yields roughly a 12% code size reduction for
the outlined block.
Co-authored-by: Jemma Issroff <jemmaissroff@gmail.com>
* [ci skip] comment edit. Please squash.
Co-authored-by: Jemma Issroff <jemmaissroff@gmail.com>
2021-11-05 15:44:29 -04:00
|
|
|
cb_set_pos(cb, 0); mov(cb, R15, imm_opnd(3)); check_bytes(cb, "41BF03000000");
|
2020-09-10 10:57:29 -04:00
|
|
|
cb_set_pos(cb, 0); mov(cb, EAX, EBX); check_bytes(cb, "89D8");
|
|
|
|
cb_set_pos(cb, 0); mov(cb, EAX, ECX); check_bytes(cb, "89C8");
|
|
|
|
cb_set_pos(cb, 0); mov(cb, EDX, mem_opnd(32, RBX, 128)); check_bytes(cb, "8B9380000000");
|
YJIT: use shorter encoding for mov(r64,imm) when unambiguous (#5081)
* YJIT: use shorter encoding for mov(r64,imm) when unambiguous
Previously, for small constants such as `mov(RAX, imm_opnd(Qundef))`,
we emit an instruction with an 8-byte immediate. This form commonly
gets the `movabs` mnemonic.
In 64-bit mode, 32-bit operands get zero extended to 64-bit to fill the
register, so when the immediate is small enough, we can save 4 bytes by
using the `mov` variant that takes a 32-bit immediate and does a zero
extension.
Not implement with this change, there is an imm32 variant of `mov` that
does sign extension we could use. When the constant is negative, we
fallback to the `movabs` form.
In railsbench, this change yields roughly a 12% code size reduction for
the outlined block.
Co-authored-by: Jemma Issroff <jemmaissroff@gmail.com>
* [ci skip] comment edit. Please squash.
Co-authored-by: Jemma Issroff <jemmaissroff@gmail.com>
2021-11-05 15:44:29 -04:00
|
|
|
|
|
|
|
// Test `mov rax, 3` => `mov eax, 3` optimization
|
|
|
|
cb_set_pos(cb, 0); mov(cb, R8, imm_opnd(0x34)); check_bytes(cb, "41B834000000");
|
|
|
|
cb_set_pos(cb, 0); mov(cb, R8, imm_opnd(0x80000000)); check_bytes(cb, "49B80000008000000000");
|
|
|
|
cb_set_pos(cb, 0); mov(cb, R8, imm_opnd(-1)); check_bytes(cb, "49B8FFFFFFFFFFFFFFFF");
|
|
|
|
|
|
|
|
cb_set_pos(cb, 0); mov(cb, RAX, imm_opnd(0x34)); check_bytes(cb, "B834000000");
|
|
|
|
cb_set_pos(cb, 0); mov(cb, RAX, imm_opnd(0x80000000)); check_bytes(cb, "48B80000008000000000");
|
|
|
|
cb_set_pos(cb, 0); mov(cb, RAX, imm_opnd(-52)); check_bytes(cb, "48B8CCFFFFFFFFFFFFFF");
|
|
|
|
cb_set_pos(cb, 0); mov(cb, RAX, imm_opnd(-1)); check_bytes(cb, "48B8FFFFFFFFFFFFFFFF");
|
2020-09-09 16:45:28 -04:00
|
|
|
/*
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.mov(X86Opnd(AL), X86Opnd(8, RCX, 0, 1, RDX)); },
|
|
|
|
"8A0411"
|
|
|
|
);
|
|
|
|
*/
|
2020-09-22 11:39:04 -04:00
|
|
|
cb_set_pos(cb, 0); mov(cb, CL, R9B); check_bytes(cb, "4488C9");
|
2020-09-09 16:45:28 -04:00
|
|
|
cb_set_pos(cb, 0); mov(cb, RBX, RAX); check_bytes(cb, "4889C3");
|
|
|
|
cb_set_pos(cb, 0); mov(cb, RDI, RBX); check_bytes(cb, "4889DF");
|
2020-09-22 11:39:04 -04:00
|
|
|
cb_set_pos(cb, 0); mov(cb, SIL, imm_opnd(11)); check_bytes(cb, "40B60B");
|
2020-09-09 16:45:28 -04:00
|
|
|
cb_set_pos(cb, 0); mov(cb, mem_opnd(8, RSP, 0), imm_opnd(-3)); check_bytes(cb, "C60424FD");
|
2020-09-14 16:59:39 -04:00
|
|
|
cb_set_pos(cb, 0); mov(cb, mem_opnd(64, RDI, 8), imm_opnd(1)); check_bytes(cb, "48C7470801000000");
|
|
|
|
|
2020-09-18 12:20:43 -04:00
|
|
|
// movsx
|
2020-09-22 11:39:04 -04:00
|
|
|
cb_set_pos(cb, 0); movsx(cb, AX, AL); check_bytes(cb, "660FBEC0");
|
|
|
|
cb_set_pos(cb, 0); movsx(cb, EDX, AL); check_bytes(cb, "0FBED0");
|
|
|
|
cb_set_pos(cb, 0); movsx(cb, RAX, BL); check_bytes(cb, "480FBEC3");
|
|
|
|
cb_set_pos(cb, 0); movsx(cb, ECX, AX); check_bytes(cb, "0FBFC8");
|
|
|
|
cb_set_pos(cb, 0); movsx(cb, R11, CL); check_bytes(cb, "4C0FBED9");
|
2020-09-18 12:20:43 -04:00
|
|
|
cb_set_pos(cb, 0); movsx(cb, R10, mem_opnd(32, RSP, 12)); check_bytes(cb, "4C6354240C");
|
|
|
|
cb_set_pos(cb, 0); movsx(cb, RAX, mem_opnd(8, RSP, 0)); check_bytes(cb, "480FBE0424");
|
|
|
|
|
2020-09-14 16:59:39 -04:00
|
|
|
// neg
|
|
|
|
cb_set_pos(cb, 0); neg(cb, RAX); check_bytes(cb, "48F7D8");
|
2020-09-09 16:45:28 -04:00
|
|
|
|
|
|
|
// nop
|
|
|
|
cb_set_pos(cb, 0); nop(cb, 1); check_bytes(cb, "90");
|
|
|
|
|
2020-09-14 11:54:25 -04:00
|
|
|
// not
|
2020-09-22 11:39:04 -04:00
|
|
|
cb_set_pos(cb, 0); not(cb, AX); check_bytes(cb, "66F7D0");
|
2020-09-14 11:54:25 -04:00
|
|
|
cb_set_pos(cb, 0); not(cb, EAX); check_bytes(cb, "F7D0");
|
|
|
|
cb_set_pos(cb, 0); not(cb, mem_opnd(64, R12, 0)); check_bytes(cb, "49F71424");
|
|
|
|
cb_set_pos(cb, 0); not(cb, mem_opnd(32, RSP, 301)); check_bytes(cb, "F794242D010000");
|
|
|
|
cb_set_pos(cb, 0); not(cb, mem_opnd(32, RSP, 0)); check_bytes(cb, "F71424");
|
|
|
|
cb_set_pos(cb, 0); not(cb, mem_opnd(32, RSP, 3)); check_bytes(cb, "F7542403");
|
|
|
|
cb_set_pos(cb, 0); not(cb, mem_opnd(32, RBP, 0)); check_bytes(cb, "F75500");
|
|
|
|
cb_set_pos(cb, 0); not(cb, mem_opnd(32, RBP, 13)); check_bytes(cb, "F7550D");
|
|
|
|
cb_set_pos(cb, 0); not(cb, RAX); check_bytes(cb, "48F7D0");
|
|
|
|
cb_set_pos(cb, 0); not(cb, R11); check_bytes(cb, "49F7D3");
|
|
|
|
cb_set_pos(cb, 0); not(cb, mem_opnd(32, RAX, 0)); check_bytes(cb, "F710");
|
|
|
|
cb_set_pos(cb, 0); not(cb, mem_opnd(32, RSI, 0)); check_bytes(cb, "F716");
|
|
|
|
cb_set_pos(cb, 0); not(cb, mem_opnd(32, RDI, 0)); check_bytes(cb, "F717");
|
|
|
|
cb_set_pos(cb, 0); not(cb, mem_opnd(32, RDX, 55)); check_bytes(cb, "F75237");
|
|
|
|
cb_set_pos(cb, 0); not(cb, mem_opnd(32, RDX, 1337)); check_bytes(cb, "F79239050000");
|
|
|
|
cb_set_pos(cb, 0); not(cb, mem_opnd(32, RDX, -55)); check_bytes(cb, "F752C9");
|
|
|
|
cb_set_pos(cb, 0); not(cb, mem_opnd(32, RDX, -555)); check_bytes(cb, "F792D5FDFFFF");
|
|
|
|
/*
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.not(X86Opnd(32, RAX, 0, 1, RBX)); },
|
|
|
|
"F71418"
|
|
|
|
);
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.not(X86Opnd(32, RAX, 0, 1, R12)); },
|
|
|
|
"42F71420"
|
|
|
|
);
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.not(X86Opnd(32, R15, 0, 1, R12)); },
|
|
|
|
"43F71427"
|
|
|
|
);
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.not(X86Opnd(32, R15, 5, 1, R12)); },
|
|
|
|
"43F7542705"
|
|
|
|
);
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.not(X86Opnd(32, R15, 5, 8, R12)); },
|
|
|
|
"43F754E705"
|
|
|
|
);
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.not(X86Opnd(32, R15, 5, 8, R13)); },
|
|
|
|
"43F754EF05"
|
|
|
|
);
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.not(X86Opnd(32, R12, 5, 4, R9)); },
|
|
|
|
"43F7548C05"
|
|
|
|
);
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.not(X86Opnd(32, R12, 301, 4, R9)); },
|
|
|
|
"43F7948C2D010000"
|
|
|
|
);
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.not(X86Opnd(32, RAX, 5, 4, RDX)); },
|
|
|
|
"F7549005"
|
|
|
|
);
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.not(X86Opnd(64, RAX, 0, 2, RDX)); },
|
|
|
|
"48F71450"
|
|
|
|
);
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.not(X86Opnd(32, RSP, 0, 1, RBX)); },
|
|
|
|
"F7141C"
|
|
|
|
);
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.not(X86Opnd(32, RSP, 3, 1, RBX)); },
|
|
|
|
"F7541C03"
|
|
|
|
);
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.not(X86Opnd(32, RBP, 13, 1, RDX)); },
|
|
|
|
"F754150D"
|
|
|
|
);
|
|
|
|
*/
|
|
|
|
|
|
|
|
// or
|
2020-09-14 16:59:39 -04:00
|
|
|
cb_set_pos(cb, 0); or(cb, EDX, ESI); check_bytes(cb, "09F2");
|
2020-09-14 11:54:25 -04:00
|
|
|
|
2020-09-08 16:45:35 -04:00
|
|
|
// pop
|
|
|
|
cb_set_pos(cb, 0); pop(cb, RAX); check_bytes(cb, "58");
|
|
|
|
cb_set_pos(cb, 0); pop(cb, RBX); check_bytes(cb, "5B");
|
|
|
|
cb_set_pos(cb, 0); pop(cb, RSP); check_bytes(cb, "5C");
|
|
|
|
cb_set_pos(cb, 0); pop(cb, RBP); check_bytes(cb, "5D");
|
|
|
|
cb_set_pos(cb, 0); pop(cb, R12); check_bytes(cb, "415C");
|
2021-02-26 22:11:27 -05:00
|
|
|
cb_set_pos(cb, 0); pop(cb, mem_opnd(64, RAX, 0)); check_bytes(cb, "8F00");
|
|
|
|
cb_set_pos(cb, 0); pop(cb, mem_opnd(64, R8, 0)); check_bytes(cb, "418F00");
|
|
|
|
cb_set_pos(cb, 0); pop(cb, mem_opnd(64, R8, 3)); check_bytes(cb, "418F4003");
|
|
|
|
cb_set_pos(cb, 0); pop(cb, mem_opnd_sib(64, RAX, RCX, 8, 3)); check_bytes(cb, "8F44C803");
|
|
|
|
cb_set_pos(cb, 0); pop(cb, mem_opnd_sib(64, R8, RCX, 8, 3)); check_bytes(cb, "418F44C803");
|
2020-09-08 16:45:35 -04:00
|
|
|
|
|
|
|
// push
|
|
|
|
cb_set_pos(cb, 0); push(cb, RAX); check_bytes(cb, "50");
|
|
|
|
cb_set_pos(cb, 0); push(cb, RBX); check_bytes(cb, "53");
|
|
|
|
cb_set_pos(cb, 0); push(cb, R12); check_bytes(cb, "4154");
|
2021-02-26 02:22:01 -05:00
|
|
|
cb_set_pos(cb, 0); push(cb, mem_opnd(64, RAX, 0)); check_bytes(cb, "FF30");
|
|
|
|
cb_set_pos(cb, 0); push(cb, mem_opnd(64, R8, 0)); check_bytes(cb, "41FF30");
|
|
|
|
cb_set_pos(cb, 0); push(cb, mem_opnd(64, R8, 3)); check_bytes(cb, "41FF7003");
|
|
|
|
cb_set_pos(cb, 0); push(cb, mem_opnd_sib(64, RAX, RCX, 8, 3)); check_bytes(cb, "FF74C803");
|
|
|
|
cb_set_pos(cb, 0); push(cb, mem_opnd_sib(64, R8, RCX, 8, 3)); check_bytes(cb, "41FF74C803");
|
2020-09-08 16:45:35 -04:00
|
|
|
|
|
|
|
// ret
|
|
|
|
cb_set_pos(cb, 0); ret(cb); check_bytes(cb, "C3");
|
2020-09-04 15:56:00 -04:00
|
|
|
|
2020-09-10 14:31:45 -04:00
|
|
|
// sal
|
2020-09-22 11:39:04 -04:00
|
|
|
cb_set_pos(cb, 0); sal(cb, CX, imm_opnd(1)); check_bytes(cb, "66D1E1");
|
2020-09-10 14:31:45 -04:00
|
|
|
cb_set_pos(cb, 0); sal(cb, ECX, imm_opnd(1)); check_bytes(cb, "D1E1");
|
|
|
|
cb_set_pos(cb, 0); sal(cb, EBP, imm_opnd(5)); check_bytes(cb, "C1E505");
|
|
|
|
cb_set_pos(cb, 0); sal(cb, mem_opnd(32, RSP, 68), imm_opnd(1)); check_bytes(cb, "D1642444");
|
2020-09-04 15:56:00 -04:00
|
|
|
|
2020-09-10 14:31:45 -04:00
|
|
|
// sar
|
|
|
|
cb_set_pos(cb, 0); sar(cb, EDX, imm_opnd(1)); check_bytes(cb, "D1FA");
|
2020-09-04 15:56:00 -04:00
|
|
|
|
2020-09-10 14:31:45 -04:00
|
|
|
// shr
|
|
|
|
cb_set_pos(cb, 0); shr(cb, R14, imm_opnd(7)); check_bytes(cb, "49C1EE07");
|
2020-09-04 15:56:00 -04:00
|
|
|
|
2020-09-10 14:31:45 -04:00
|
|
|
/*
|
|
|
|
// sqrtsd
|
|
|
|
test(
|
|
|
|
delegate void (CodeBlock cb) { cb.sqrtsd(X86Opnd(XMM2), X86Opnd(XMM6)); },
|
|
|
|
"F20F51D6"
|
|
|
|
);
|
|
|
|
*/
|
2020-09-04 15:56:00 -04:00
|
|
|
|
2020-09-10 14:31:45 -04:00
|
|
|
// sub
|
|
|
|
cb_set_pos(cb, 0); sub(cb, EAX, imm_opnd(1)); check_bytes(cb, "83E801");
|
|
|
|
cb_set_pos(cb, 0); sub(cb, RAX, imm_opnd(2)); check_bytes(cb, "4883E802");
|
2020-09-04 15:56:00 -04:00
|
|
|
|
2020-09-17 11:49:53 -04:00
|
|
|
// test
|
2021-05-11 16:33:28 -04:00
|
|
|
cb_set_pos(cb, 0); test(cb, AL, AL); check_bytes(cb, "84C0");
|
|
|
|
cb_set_pos(cb, 0); test(cb, AX, AX); check_bytes(cb, "6685C0");
|
2020-09-28 15:50:41 -04:00
|
|
|
cb_set_pos(cb, 0); test(cb, CL, imm_opnd(8)); check_bytes(cb, "F6C108");
|
|
|
|
cb_set_pos(cb, 0); test(cb, DL, imm_opnd(7)); check_bytes(cb, "F6C207");
|
|
|
|
cb_set_pos(cb, 0); test(cb, RCX, imm_opnd(8)); check_bytes(cb, "F6C108");
|
|
|
|
cb_set_pos(cb, 0); test(cb, mem_opnd(8, RDX, 8), imm_opnd(8)); check_bytes(cb, "F6420808");
|
|
|
|
cb_set_pos(cb, 0); test(cb, mem_opnd(8, RDX, 8), imm_opnd(255)); check_bytes(cb, "F64208FF");
|
|
|
|
cb_set_pos(cb, 0); test(cb, DX, imm_opnd(0xFFFF)); check_bytes(cb, "66F7C2FFFF");
|
|
|
|
cb_set_pos(cb, 0); test(cb, mem_opnd(16, RDX, 8), imm_opnd(0xFFFF)); check_bytes(cb, "66F74208FFFF");
|
2020-10-02 12:12:28 -04:00
|
|
|
cb_set_pos(cb, 0); test(cb, mem_opnd(8, RSI, 0), imm_opnd(1)); check_bytes(cb, "F60601");
|
|
|
|
cb_set_pos(cb, 0); test(cb, mem_opnd(8, RSI, 16), imm_opnd(1)); check_bytes(cb, "F6461001");
|
|
|
|
cb_set_pos(cb, 0); test(cb, mem_opnd(8, RSI, -16), imm_opnd(1)); check_bytes(cb, "F646F001");
|
2020-10-27 11:10:19 -04:00
|
|
|
cb_set_pos(cb, 0); test(cb, mem_opnd(32, RSI, 64), EAX); check_bytes(cb, "854640");
|
2021-03-03 18:06:49 -05:00
|
|
|
cb_set_pos(cb, 0); test(cb, mem_opnd(64, RDI, 42), RAX); check_bytes(cb, "4885472A");
|
|
|
|
cb_set_pos(cb, 0); test(cb, RAX, RAX); check_bytes(cb, "4885C0");
|
|
|
|
cb_set_pos(cb, 0); test(cb, RAX, RSI); check_bytes(cb, "4885F0");
|
2020-12-14 13:26:33 -05:00
|
|
|
cb_set_pos(cb, 0); test(cb, mem_opnd(64, RSI, 64), imm_opnd(~0x08)); check_bytes(cb, "48F74640F7FFFFFF");
|
2020-09-17 11:49:53 -04:00
|
|
|
|
2021-05-12 10:18:46 -04:00
|
|
|
// xchg
|
|
|
|
cb_set_pos(cb, 0); xchg(cb, RAX, RCX); check_bytes(cb, "4891");
|
|
|
|
cb_set_pos(cb, 0); xchg(cb, RAX, R13); check_bytes(cb, "4995");
|
|
|
|
cb_set_pos(cb, 0); xchg(cb, RCX, RBX); check_bytes(cb, "4887D9");
|
|
|
|
cb_set_pos(cb, 0); xchg(cb, R9, R15); check_bytes(cb, "4D87F9");
|
|
|
|
|
2020-09-14 16:59:39 -04:00
|
|
|
// xor
|
2020-09-15 10:44:46 -04:00
|
|
|
cb_set_pos(cb, 0); xor(cb, EAX, EAX); check_bytes(cb, "31C0");
|
2020-09-14 16:59:39 -04:00
|
|
|
|
2020-09-04 15:56:00 -04:00
|
|
|
printf("Assembler tests done\n");
|
|
|
|
}
|
|
|
|
|
2021-10-27 10:57:08 -04:00
|
|
|
void assert_equal(int expected, int actual)
|
2021-07-19 11:26:34 -04:00
|
|
|
{
|
|
|
|
if (expected != actual) {
|
|
|
|
fprintf(stderr, "expected %d, got %d\n", expected, actual);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 10:57:08 -04:00
|
|
|
void run_runtime_tests(void)
|
2021-07-19 11:26:34 -04:00
|
|
|
{
|
|
|
|
printf("Running runtime tests\n");
|
|
|
|
|
|
|
|
codeblock_t codeblock;
|
|
|
|
codeblock_t* cb = &codeblock;
|
|
|
|
|
|
|
|
uint8_t* mem_block = alloc_exec_mem(4096);
|
|
|
|
cb_init(cb, mem_block, 4096);
|
|
|
|
|
|
|
|
int (*function)(void);
|
|
|
|
function = (int (*)(void))mem_block;
|
|
|
|
|
2021-10-26 19:57:30 -04:00
|
|
|
#define TEST(BODY) cb_set_pos(cb, 0); BODY ret(cb); cb_mark_all_executable(cb); assert_equal(7, function());
|
2021-07-19 11:26:34 -04:00
|
|
|
|
|
|
|
// add
|
|
|
|
TEST({ mov(cb, RAX, imm_opnd(0)); add(cb, RAX, imm_opnd(7)); })
|
|
|
|
TEST({ mov(cb, RAX, imm_opnd(0)); mov(cb, RCX, imm_opnd(7)); add(cb, RAX, RCX); })
|
|
|
|
|
|
|
|
// and
|
|
|
|
TEST({ mov(cb, RAX, imm_opnd(31)); and(cb, RAX, imm_opnd(7)); })
|
|
|
|
TEST({ mov(cb, RAX, imm_opnd(31)); mov(cb, RCX, imm_opnd(7)); and(cb, RAX, RCX); })
|
|
|
|
|
|
|
|
// or
|
|
|
|
TEST({ mov(cb, RAX, imm_opnd(3)); or(cb, RAX, imm_opnd(4)); })
|
|
|
|
TEST({ mov(cb, RAX, imm_opnd(3)); mov(cb, RCX, imm_opnd(4)); or(cb, RAX, RCX); })
|
|
|
|
|
|
|
|
// push/pop
|
|
|
|
TEST({ mov(cb, RCX, imm_opnd(7)); push(cb, RCX); pop(cb, RAX); })
|
|
|
|
|
|
|
|
// shr
|
|
|
|
TEST({ mov(cb, RAX, imm_opnd(31)); shr(cb, RAX, imm_opnd(2)); })
|
|
|
|
|
|
|
|
// sub
|
|
|
|
TEST({ mov(cb, RAX, imm_opnd(12)); sub(cb, RAX, imm_opnd(5)); })
|
|
|
|
TEST({ mov(cb, RAX, imm_opnd(12)); mov(cb, RCX, imm_opnd(5)); sub(cb, RAX, RCX); })
|
|
|
|
|
|
|
|
// xor
|
|
|
|
TEST({ mov(cb, RAX, imm_opnd(13)); xor(cb, RAX, imm_opnd(10)); })
|
|
|
|
TEST({ mov(cb, RAX, imm_opnd(13)); mov(cb, RCX, imm_opnd(10)); xor(cb, RAX, RCX); })
|
|
|
|
|
|
|
|
#undef TEST
|
|
|
|
|
|
|
|
printf("Runtime tests done\n");
|
|
|
|
}
|
|
|
|
|
2020-09-04 15:56:00 -04:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2021-07-19 11:26:34 -04:00
|
|
|
run_assembler_tests();
|
|
|
|
run_runtime_tests();
|
2020-09-04 15:56:00 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|