diff options
author | Kevin Newton <kddnewton@gmail.com> | 2021-07-19 11:26:34 -0400 |
---|---|---|
committer | Alan Wu <XrXr@users.noreply.github.com> | 2021-10-20 18:19:38 -0400 |
commit | a0bb731f4f71ad3e57be57fb0a1a7347107dbf0d (patch) | |
tree | 6850972a16970ae3ba3a5865db672ced306a028f /yjit_asm_tests.c | |
parent | 71cef74432ef67edfd5635a9b9f8dffbbc33d392 (diff) |
Add some YJIT runtime tests
This is just for helping my own understanding, but could be useful for others as well.
Diffstat (limited to 'yjit_asm_tests.c')
-rw-r--r-- | yjit_asm_tests.c | 59 |
1 files changed, 57 insertions, 2 deletions
diff --git a/yjit_asm_tests.c b/yjit_asm_tests.c index 3d948d38f4..ccda979437 100644 --- a/yjit_asm_tests.c +++ b/yjit_asm_tests.c @@ -58,7 +58,7 @@ void check_bytes(codeblock_t* cb, const char* bytes) } } -void run_tests() +void run_assembler_tests() { printf("Running assembler tests\n"); @@ -353,9 +353,64 @@ void run_tests() printf("Assembler tests done\n"); } +void assert_equal(expected, actual) +{ + if (expected != actual) { + fprintf(stderr, "expected %d, got %d\n", expected, actual); + exit(-1); + } +} + +void run_runtime_tests() +{ + 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; + + #define TEST(BODY) cb_set_pos(cb, 0); BODY ret(cb); assert_equal(7, function()); + + // 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"); +} + int main(int argc, char** argv) { - run_tests(); + run_assembler_tests(); + run_runtime_tests(); return 0; } |