summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ujit_asm_tests.c31
-rw-r--r--ujit_compile.c6
2 files changed, 29 insertions, 8 deletions
diff --git a/ujit_asm_tests.c b/ujit_asm_tests.c
index 650762ff46..6b8c7c695d 100644
--- a/ujit_asm_tests.c
+++ b/ujit_asm_tests.c
@@ -4,6 +4,17 @@
#include <assert.h>
#include "ujit_asm.h"
+// Print the bytes in a code block
+void print_bytes(codeblock_t* cb)
+{
+ for (size_t i = 0; i < cb->write_pos; ++i)
+ {
+ printf("%02X", (int)cb->mem_block[i]);
+ }
+
+ printf("\n");
+}
+
// Check that the code block contains the given sequence of bytes
void check_bytes(codeblock_t* cb, const char* bytes)
{
@@ -15,7 +26,12 @@ void check_bytes(codeblock_t* cb, const char* bytes)
if (cb->write_pos != num_bytes)
{
- fprintf(stderr, "incorrect encoding length %ld, expected %ld\n", cb->write_pos, num_bytes);
+ fprintf(stderr, "incorrect encoding length, expected %ld, got %ld\n",
+ num_bytes,
+ cb->write_pos
+ );
+ printf("%s\n", bytes);
+ print_bytes(cb);
exit(-1);
}
@@ -30,11 +46,13 @@ void check_bytes(codeblock_t* cb, const char* bytes)
if (cb_byte != byte)
{
- fprintf(stderr, "incorrect encoding at position %ld, got %02X, expected %02X\n",
+ fprintf(stderr, "incorrect encoding at position %ld, expected %02X, got %02X\n",
i,
- (int)cb_byte,
- (int)byte
+ (int)byte,
+ (int)cb_byte
);
+ printf("%s\n", bytes);
+ print_bytes(cb);
exit(-1);
}
}
@@ -167,9 +185,10 @@ void run_tests()
cb_set_pos(cb, 0); jmp_rm(cb, R12); check_bytes(cb, "41FFE4");
// lea
- //cb_set_pos(cb, 0); lea(cb, EBX, mem_opnd(32, RSP, 4)); check_bytes(cb, "8D5C2404");
cb_set_pos(cb, 0); lea(cb, RDX, mem_opnd(64, RCX, 8)); check_bytes(cb, "488D5108");
- //cb_set_pos(cb, 0); lea(cb, RAX, mem_opnd(8, RIP, 5)); check_bytes(cb, "488D042505000000");
+ 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");
// mov
cb_set_pos(cb, 0); mov(cb, EAX, imm_opnd(7)); check_bytes(cb, "B807000000");
diff --git a/ujit_compile.c b/ujit_compile.c
index 2a99a85e45..bd2d8697eb 100644
--- a/ujit_compile.c
+++ b/ujit_compile.c
@@ -85,8 +85,11 @@ VALUE ctx_get_arg(ctx_t* ctx, size_t arg_idx)
Generate a chunk of machine code for one individual bytecode instruction
Eventually, this will handle multiple instructions in a sequence
-MicroJIT code gets a pointer to the cfp as the first argument in RSI
+MicroJIT code gets a pointer to the cfp as the first argument in RDI
See rb_ujit_empty_func(rb_control_frame_t *cfp) in iseq.c
+
+System V ABI reference:
+https://wiki.osdev.org/System_V_ABI#x86-64
*/
uint8_t *
ujit_compile_insn(rb_iseq_t *iseq, size_t insn_idx)
@@ -186,7 +189,6 @@ void gen_getlocal_wc0(codeblock_t* cb, ctx_t* ctx)
// Load block pointer from CFP
mov(cb, RDX, mem_opnd(64, RDI, 32));
- // TODO: we may want a macro or helper function to get insn operands
// Compute the offset from BP to the local
int32_t local_idx = (int32_t)ctx_get_arg(ctx, 0);
const int32_t offs = -8 * local_idx;