summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxime Chevalier-Boisvert <maxime.chevalierboisvert@shopify.com>2020-09-21 16:46:57 -0400
committerAlan Wu <XrXr@users.noreply.github.com>2021-10-20 18:19:23 -0400
commit304adba717b30df17b4db3c76993a649c3efec0e (patch)
treea2563a18f25a919b4fa6f1e6af6754f92a1ee3d7
parentc20066b24cf1c1c235d1f9402b7986b5085cad53 (diff)
Add location hint to code block mmap call
-rw-r--r--ujit_asm.c10
-rw-r--r--ujit_compile.c30
2 files changed, 32 insertions, 8 deletions
diff --git a/ujit_asm.c b/ujit_asm.c
index 6e12abe513..c5e508fddb 100644
--- a/ujit_asm.c
+++ b/ujit_asm.c
@@ -117,7 +117,7 @@ void cb_init(codeblock_t* cb, size_t mem_size)
{
// Map the memory as executable
cb->mem_block = (uint8_t*)mmap(
- NULL,
+ &cb_init,
mem_size,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANON,
@@ -1084,14 +1084,12 @@ void jmp_rm(codeblock_t* cb, x86opnd_t opnd)
}
/*
-/// Opcode for direct jump with relative 8-bit offset
-const ubyte JMP_REL8_OPCODE = 0xEB;
-*/
-
-/*
/// jmp - Jump with relative 8-bit offset
void jmp8(CodeBlock cb, int8_t offset)
{
+ /// Opcode for direct jump with relative 8-bit offset
+ const ubyte JMP_REL8_OPCODE = 0xEB;
+
cb.writeASM("jmp", ((offset > 0)? "+":"-") ~ to!string(offset));
cb.writeByte(JMP_REL8_OPCODE);
cb.writeByte(offset);
diff --git a/ujit_compile.c b/ujit_compile.c
index 367fbf8ba9..bfe75d1b7b 100644
--- a/ujit_compile.c
+++ b/ujit_compile.c
@@ -170,6 +170,7 @@ ujit_compile_insn(rb_iseq_t *iseq, unsigned int insn_idx, unsigned int* next_uji
st_data_t st_gen_fn;
if (!rb_st_lookup(gen_fns, opcode, &st_gen_fn))
{
+ //print_int(cb, imm_opnd(num_instrs));
//print_str(cb, insn_name(opcode));
break;
}
@@ -200,8 +201,6 @@ ujit_compile_insn(rb_iseq_t *iseq, unsigned int insn_idx, unsigned int* next_uji
return NULL;
}
- //print_int(cb, imm_opnd(num_instrs));
-
// Write the adjusted SP back into the CFP
if (ctx.stack_diff != 0)
{
@@ -217,6 +216,33 @@ ujit_compile_insn(rb_iseq_t *iseq, unsigned int insn_idx, unsigned int* next_uji
// Write the post call bytes
ujit_instr_exit(cb);
+ /*
+ // Hack to patch a relative 32-bit jump to the instruction handler
+ int next_opcode = (int)*ctx.pc;
+ const void * const *table = rb_vm_get_insns_address_table();
+ VALUE encoded = (VALUE)table[next_opcode];
+ uint8_t* p_handler = (uint8_t*)encoded;
+
+ uint8_t* p_code = &cb->mem_block[cb->write_pos];
+ int64_t rel64 = ((int64_t)p_handler) - ((int64_t)p_code - 2 + 5);
+
+ //printf("p_handler: %lld\n", (int64_t)p_handler);
+ //printf("rel64: %lld\n", rel64);
+
+ uint8_t byte0 = cb->mem_block[cb->write_pos - 2];
+ uint8_t byte1 = cb->mem_block[cb->write_pos - 1];
+
+ //printf("cb_init: %lld\n", (int64_t)&cb_init);
+ //printf("%lld\n", rel64);
+
+ if (byte0 == 0xFF && byte1 == 0x20 && rel64 >= -2147483648 && rel64 <= 2147483647)
+ {
+ //printf("%02X %02X\n", (int)byte0, (int)byte1);
+ cb->write_pos -= 2;
+ jmp32(cb, (int32_t)rel64);
+ }
+ */
+
addr2insn_bookkeeping(code_ptr, first_opcode);
return code_ptr;