Skip to content

Commit 010c89a

Browse files
authored
Merge pull request ruby#133 from Shopify/runtime-tests
Add some YJIT runtime tests
2 parents de2246b + befcf51 commit 010c89a

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

yjit_asm_tests.c

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void check_bytes(codeblock_t* cb, const char* bytes)
5858
}
5959
}
6060

61-
void run_tests()
61+
void run_assembler_tests()
6262
{
6363
printf("Running assembler tests\n");
6464

@@ -353,9 +353,64 @@ void run_tests()
353353
printf("Assembler tests done\n");
354354
}
355355

356+
void assert_equal(expected, actual)
357+
{
358+
if (expected != actual) {
359+
fprintf(stderr, "expected %d, got %d\n", expected, actual);
360+
exit(-1);
361+
}
362+
}
363+
364+
void run_runtime_tests()
365+
{
366+
printf("Running runtime tests\n");
367+
368+
codeblock_t codeblock;
369+
codeblock_t* cb = &codeblock;
370+
371+
uint8_t* mem_block = alloc_exec_mem(4096);
372+
cb_init(cb, mem_block, 4096);
373+
374+
int (*function)(void);
375+
function = (int (*)(void))mem_block;
376+
377+
#define TEST(BODY) cb_set_pos(cb, 0); BODY ret(cb); assert_equal(7, function());
378+
379+
// add
380+
TEST({ mov(cb, RAX, imm_opnd(0)); add(cb, RAX, imm_opnd(7)); })
381+
TEST({ mov(cb, RAX, imm_opnd(0)); mov(cb, RCX, imm_opnd(7)); add(cb, RAX, RCX); })
382+
383+
// and
384+
TEST({ mov(cb, RAX, imm_opnd(31)); and(cb, RAX, imm_opnd(7)); })
385+
TEST({ mov(cb, RAX, imm_opnd(31)); mov(cb, RCX, imm_opnd(7)); and(cb, RAX, RCX); })
386+
387+
// or
388+
TEST({ mov(cb, RAX, imm_opnd(3)); or(cb, RAX, imm_opnd(4)); })
389+
TEST({ mov(cb, RAX, imm_opnd(3)); mov(cb, RCX, imm_opnd(4)); or(cb, RAX, RCX); })
390+
391+
// push/pop
392+
TEST({ mov(cb, RCX, imm_opnd(7)); push(cb, RCX); pop(cb, RAX); })
393+
394+
// shr
395+
TEST({ mov(cb, RAX, imm_opnd(31)); shr(cb, RAX, imm_opnd(2)); })
396+
397+
// sub
398+
TEST({ mov(cb, RAX, imm_opnd(12)); sub(cb, RAX, imm_opnd(5)); })
399+
TEST({ mov(cb, RAX, imm_opnd(12)); mov(cb, RCX, imm_opnd(5)); sub(cb, RAX, RCX); })
400+
401+
// xor
402+
TEST({ mov(cb, RAX, imm_opnd(13)); xor(cb, RAX, imm_opnd(10)); })
403+
TEST({ mov(cb, RAX, imm_opnd(13)); mov(cb, RCX, imm_opnd(10)); xor(cb, RAX, RCX); })
404+
405+
#undef TEST
406+
407+
printf("Runtime tests done\n");
408+
}
409+
356410
int main(int argc, char** argv)
357411
{
358-
run_tests();
412+
run_assembler_tests();
413+
run_runtime_tests();
359414

360415
return 0;
361416
}

0 commit comments

Comments
 (0)