diff options
author | Kimplul <kimi.h.kuparinen@gmail.com> | 2024-06-29 14:20:07 +0300 |
---|---|---|
committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2024-06-29 14:20:07 +0300 |
commit | 49aa680ccdac46d1d2a7f9f250999b7ff7099548 (patch) | |
tree | 1de3bd5209feadfd147f7a05d1ac925f98b747b1 /src/compile | |
parent | 29718f2e84478b296c3198ae6d35cfd5d79efb14 (diff) | |
download | ejit-49aa680ccdac46d1d2a7f9f250999b7ff7099548.tar.gz ejit-49aa680ccdac46d1d2a7f9f250999b7ff7099548.zip |
start adding tests
Diffstat (limited to 'src/compile')
-rw-r--r-- | src/compile/compile.c | 127 |
1 files changed, 78 insertions, 49 deletions
diff --git a/src/compile/compile.c b/src/compile/compile.c index c07a46a..c9a92a2 100644 --- a/src/compile/compile.c +++ b/src/compile/compile.c @@ -5,8 +5,8 @@ static void *alloc_arena(size_t size) { return mmap(NULL, size, - PROT_EXEC | PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + PROT_EXEC | PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); } static void free_arena(void *arena, size_t size) @@ -27,7 +27,7 @@ static size_t frploc_count(struct ejit_func *f) static size_t stack_size(struct ejit_func *f) { return grploc_count(f) * sizeof(jit_uword_t) - + frploc_count(f) * sizeof(jit_float64_t); + + frploc_count(f) * sizeof(jit_float64_t); } static jit_off_t stack_loc(size_t l) @@ -40,7 +40,7 @@ static jit_off_t stack_loc_f(struct ejit_func *f, size_t l) { assert(l >= jit_vf_num()); return grploc_count(f) * sizeof(jit_uword_t) - + (l - jit_vf_num()) * sizeof(jit_float64_t); + + (l - jit_vf_num()) * sizeof(jit_float64_t); } @@ -84,14 +84,16 @@ static void compile_label(jit_state_t *j, size_t ii, struct vec *labels) vect_at(jit_addr_t, *labels, ii) = jit_address(j); } -static void compile_movi(struct ejit_func *f, jit_state_t *j, struct ejit_insn i) +static void compile_movi(struct ejit_func *f, jit_state_t *j, + struct ejit_insn i) { jit_gpr_t r = getreg(f, i.r0, 0); jit_movi(j, r, i.o); putloc(f, j, i.r0, r); } -static void compile_movr(struct ejit_func *f, jit_state_t *j, struct ejit_insn i) +static void compile_movr(struct ejit_func *f, jit_state_t *j, + struct ejit_insn i) { jit_gpr_t to = getreg(f, i.r0, 0); jit_gpr_t from = getreg(f, i.r1, 1); @@ -99,7 +101,8 @@ static void compile_movr(struct ejit_func *f, jit_state_t *j, struct ejit_insn i putloc(f, j, i.r0, to); } -static void compile_addr(struct ejit_func *f, jit_state_t *j, struct ejit_insn i) +static void compile_addr(struct ejit_func *f, jit_state_t *j, + struct ejit_insn i) { jit_gpr_t dst = getreg(f, i.r0, 0); jit_gpr_t src0 = getloc(f, j, i.r1, 1); @@ -108,7 +111,8 @@ static void compile_addr(struct ejit_func *f, jit_state_t *j, struct ejit_insn i putloc(f, j, i.r0, dst); } -static void compile_addi(struct ejit_func *f, jit_state_t *j, struct ejit_insn i) +static void compile_addi(struct ejit_func *f, jit_state_t *j, + struct ejit_insn i) { jit_gpr_t dst = getreg(f, i.r0, 0); jit_gpr_t src0 = getloc(f, j, i.r1, 1); @@ -116,7 +120,8 @@ static void compile_addi(struct ejit_func *f, jit_state_t *j, struct ejit_insn i putloc(f, j, i.r0, dst); } -static void compile_subr(struct ejit_func *f, jit_state_t *j, struct ejit_insn i) +static void compile_subr(struct ejit_func *f, jit_state_t *j, + struct ejit_insn i) { jit_gpr_t dst = getreg(f, i.r0, 0); jit_gpr_t src0 = getloc(f, j, i.r1, 1); @@ -125,7 +130,8 @@ static void compile_subr(struct ejit_func *f, jit_state_t *j, struct ejit_insn i putloc(f, j, i.r0, dst); } -static void compile_subi(struct ejit_func *f, jit_state_t *j, struct ejit_insn i) +static void compile_subi(struct ejit_func *f, jit_state_t *j, + struct ejit_insn i) { jit_gpr_t dst = getreg(f, i.r0, 0); jit_gpr_t src0 = getloc(f, j, i.r1, 1); @@ -133,14 +139,16 @@ static void compile_subi(struct ejit_func *f, jit_state_t *j, struct ejit_insn i putloc(f, j, i.r0, dst); } -static void compile_stxi64(struct ejit_func *f, jit_state_t *j, struct ejit_insn i) +static void compile_stxi64(struct ejit_func *f, jit_state_t *j, + struct ejit_insn i) { jit_gpr_t r0 = getloc(f, j, i.r0, 0); jit_gpr_t r1 = getloc(f, j, i.r1, 1); jit_stxi_l(j, i.o, r1, r0); } -static void compile_ldxiu64(struct ejit_func *f, jit_state_t *j, struct ejit_insn i) +static void compile_ldxiu64(struct ejit_func *f, jit_state_t *j, + struct ejit_insn i) { jit_gpr_t r0 = getreg(f, i.r0, 0); jit_gpr_t r1 = getloc(f, j, i.r1, 1); @@ -148,8 +156,10 @@ static void compile_ldxiu64(struct ejit_func *f, jit_state_t *j, struct ejit_ins putloc(f, j, i.r0, r0); } -static void compile_reg_cmp(struct ejit_func *f, jit_state_t *j, struct ejit_insn i, - jit_reloc_t (*bcomp)(jit_state_t *, jit_gpr_t, jit_gpr_t), long same) +static void compile_reg_cmp(struct ejit_func *f, jit_state_t *j, + struct ejit_insn i, + jit_reloc_t (*bcomp)(jit_state_t *, jit_gpr_t, + jit_gpr_t), long same) { jit_gpr_t r0 = getreg(f, i.r0, 0); if (i.r1 == i.r2) { @@ -185,7 +195,8 @@ static void compile_ltr(struct ejit_func *f, jit_state_t *j, struct ejit_insn i) compile_reg_cmp(f, j, i, jit_bltr, 0); } -static void compile_bltr(struct ejit_func *f, jit_state_t *j, struct ejit_insn i, struct vec *relocs) +static void compile_bltr(struct ejit_func *f, jit_state_t *j, + struct ejit_insn i, struct vec *relocs) { jit_gpr_t c0 = getloc(f, j, i.r1, 0); jit_gpr_t c1 = getloc(f, j, i.r2, 1); @@ -194,7 +205,8 @@ static void compile_bltr(struct ejit_func *f, jit_state_t *j, struct ejit_insn i vect_append(struct reloc_helper, *relocs, &h); } -static void compile_beqi(struct ejit_func *f, jit_state_t *j, struct ejit_insn i, struct vec *relocs) +static void compile_beqi(struct ejit_func *f, jit_state_t *j, + struct ejit_insn i, struct vec *relocs) { jit_gpr_t r1 = getloc(f, j, i.r1, 0); jit_reloc_t r = jit_beqi(j, r1, i.o); @@ -202,7 +214,8 @@ static void compile_beqi(struct ejit_func *f, jit_state_t *j, struct ejit_insn i vect_append(struct reloc_helper, *relocs, &h); } -static void compile_bnei(struct ejit_func *f, jit_state_t *j, struct ejit_insn i, struct vec *relocs) +static void compile_bnei(struct ejit_func *f, jit_state_t *j, + struct ejit_insn i, struct vec *relocs) { jit_gpr_t r1 = getloc(f, j, i.r1, 0); jit_reloc_t r = jit_bnei(j, r1, i.o); @@ -210,7 +223,8 @@ static void compile_bnei(struct ejit_func *f, jit_state_t *j, struct ejit_insn i vect_append(struct reloc_helper, *relocs, &h); } -static void compile_bgti(struct ejit_func *f, jit_state_t *j, struct ejit_insn i, struct vec *relocs) +static void compile_bgti(struct ejit_func *f, jit_state_t *j, + struct ejit_insn i, struct vec *relocs) { jit_gpr_t r1 = getloc(f, j, i.r1, 0); jit_reloc_t r = jit_bgti(j, r1, i.o); @@ -218,7 +232,8 @@ static void compile_bgti(struct ejit_func *f, jit_state_t *j, struct ejit_insn i vect_append(struct reloc_helper, *relocs, &h); } -static void compile_jmp(struct ejit_func *f, jit_state_t *j, struct ejit_insn i, struct vec *relocs) +static void compile_jmp(struct ejit_func *f, jit_state_t *j, struct ejit_insn i, + struct vec *relocs) { (void)(f); jit_reloc_t r = jit_jmp(j); @@ -226,7 +241,8 @@ static void compile_jmp(struct ejit_func *f, jit_state_t *j, struct ejit_insn i, vect_append(struct reloc_helper, *relocs, &h); } -static void compile_retval(struct ejit_func *f, jit_state_t *j, struct ejit_insn i) +static void compile_retval(struct ejit_func *f, jit_state_t *j, + struct ejit_insn i) { jit_gpr_t r0 = getreg(f, i.r0, 0); jit_retval(j, r0); @@ -256,14 +272,14 @@ static enum jit_operand_abi jit_abi_from(enum ejit_type t) static size_t arg_offsetof(enum ejit_type t) { switch (t) { - case EJIT_INT8: return offsetof(struct ejit_arg, c); - case EJIT_INT16: return offsetof(struct ejit_arg, s); - case EJIT_INT32: return offsetof(struct ejit_arg, i); - case EJIT_INT64: return offsetof(struct ejit_arg, l); - case EJIT_UINT8: return offsetof(struct ejit_arg, uc); - case EJIT_UINT16: return offsetof(struct ejit_arg, us); - case EJIT_UINT32: return offsetof(struct ejit_arg, ui); - case EJIT_UINT64: return offsetof(struct ejit_arg, ul); + case EJIT_INT8: return offsetof(struct ejit_arg, i8); + case EJIT_INT16: return offsetof(struct ejit_arg, i16); + case EJIT_INT32: return offsetof(struct ejit_arg, i32); + case EJIT_INT64: return offsetof(struct ejit_arg, i64); + case EJIT_UINT8: return offsetof(struct ejit_arg, u8); + case EJIT_UINT16: return offsetof(struct ejit_arg, u16); + case EJIT_UINT32: return offsetof(struct ejit_arg, u32); + case EJIT_UINT64: return offsetof(struct ejit_arg, u64); case EJIT_POINTER: return offsetof(struct ejit_arg, p); case EJIT_FLOAT: return offsetof(struct ejit_arg, f); case EJIT_DOUBLE: return offsetof(struct ejit_arg, d); @@ -282,7 +298,8 @@ static jit_off_t arg_offset(struct ejit_insn i) static jit_off_t type_offset(struct ejit_insn i) { - return (sizeof(struct ejit_arg) * i.r0) + offsetof(struct ejit_arg, type); + return (sizeof(struct ejit_arg) * i.r0) + offsetof(struct ejit_arg, + type); } static void fixup_operands(struct vec *operands, size_t fixup) @@ -297,7 +314,8 @@ static void fixup_operands(struct vec *operands, size_t fixup) } } -static void compile_imm_call(jit_state_t *j, struct vec *src, struct vec *dst, void *addr, size_t argc, jit_operand_t args[argc]) +static void compile_imm_call(jit_state_t *j, struct vec *src, struct vec *dst, + void *addr, size_t argc, jit_operand_t args[argc]) { /* each move is type + arg, so twofold */ size_t movec = vec_len(src) / 2; @@ -313,7 +331,8 @@ static void compile_imm_call(jit_state_t *j, struct vec *src, struct vec *dst, v vec_reset(dst); } -static size_t compile_fn_body(struct ejit_func *f, jit_state_t *j, void *arena, size_t size) +static size_t compile_fn_body(struct ejit_func *f, jit_state_t *j, void *arena, + size_t size) { jit_begin(j, arena, size); size_t gprs = f->gpr >= jit_v_num() ? jit_v_num() : f->gpr; @@ -323,8 +342,8 @@ static size_t compile_fn_body(struct ejit_func *f, jit_state_t *j, void *arena, /* very important, argc we don't really do anything with but JIR_R1 * contains the argument stack! */ jit_load_args_2(j, - jit_operand_gpr(JIT_OPERAND_ABI_WORD, JIT_R0), - jit_operand_gpr(JIT_OPERAND_ABI_POINTER, JIT_R1)); + jit_operand_gpr(JIT_OPERAND_ABI_WORD, JIT_R0), + jit_operand_gpr(JIT_OPERAND_ABI_POINTER, JIT_R1)); size_t stack = jit_align_stack(j, stack_size(f)); @@ -367,24 +386,29 @@ static size_t compile_fn_body(struct ejit_func *f, jit_state_t *j, void *arena, case JMP: compile_jmp(f, j, i, &relocs); break; case ARG: { - jit_operand_t type = jit_operand_imm(JIT_OPERAND_ABI_WORD, i.r1); + jit_operand_t type = + jit_operand_imm(JIT_OPERAND_ABI_WORD, i.r1); jit_operand_t arg; if (i.r0 < jit_v_num()) { /* regular register */ - arg = jit_operand_gpr(jit_abi_from(i.r1), jit_v(i.r2)); + arg = jit_operand_gpr(jit_abi_from(i.r1), + jit_v(i.r2)); } else { /* stack location, note that we'll fix up the SP * offset before doing the actual call */ - arg = jit_operand_mem(jit_abi_from(i.r1), JIT_SP, stack_loc(i.r0)); + arg = jit_operand_mem(jit_abi_from(i.r1), + JIT_SP, stack_loc(i.r0)); } vec_append(&src, &type); vec_append(&src, &arg); jit_operand_t to[2] = { - jit_operand_mem(JIT_OPERAND_ABI_WORD, JIT_SP, type_offset(i)), - jit_operand_mem(jit_abi_from(i.r1), JIT_SP, arg_offset(i)) + jit_operand_mem(JIT_OPERAND_ABI_WORD, JIT_SP, + type_offset(i)), + jit_operand_mem(jit_abi_from(i.r1), JIT_SP, + arg_offset(i)) }; vec_append(&dst, &to[0]); @@ -394,7 +418,8 @@ static size_t compile_fn_body(struct ejit_func *f, jit_state_t *j, void *arena, case ESCAPEI: { jit_operand_t args[2] = { - jit_operand_imm(JIT_OPERAND_ABI_WORD, vec_len(&src) / 2), + jit_operand_imm(JIT_OPERAND_ABI_WORD, + vec_len(&src) / 2), jit_operand_gpr(JIT_OPERAND_ABI_POINTER, JIT_SP) }; compile_imm_call(j, &src, &dst, (void *)i.o, 2, args); @@ -404,7 +429,8 @@ static size_t compile_fn_body(struct ejit_func *f, jit_state_t *j, void *arena, case CALLI: { jit_operand_t args[3] = { jit_operand_imm(JIT_OPERAND_ABI_POINTER, i.o), - jit_operand_imm(JIT_OPERAND_ABI_WORD, vec_len(&src) / 2), + jit_operand_imm(JIT_OPERAND_ABI_WORD, + vec_len(&src) / 2), jit_operand_gpr(JIT_OPERAND_ABI_POINTER, JIT_SP) }; compile_imm_call(j, &src, &dst, ejit_run_func, 3, args); @@ -412,7 +438,7 @@ static size_t compile_fn_body(struct ejit_func *f, jit_state_t *j, void *arena, } case RETVAL: compile_retval(f, j, i); break; - case RET: { + case RETR: { jit_gpr_t r = getloc(f, j, i.r0, 0); /* R0 won't get overwritten by jit_leave_jit_abi */ jit_movr(j, JIT_R0, r); @@ -422,7 +448,7 @@ static size_t compile_fn_body(struct ejit_func *f, jit_state_t *j, void *arena, break; } - case RET_I: { + case RETI: { jit_shrink_stack(j, stack); jit_leave_jit_abi(j, gprs, fprs, frame); jit_reti(j, i.o); @@ -440,19 +466,21 @@ static size_t compile_fn_body(struct ejit_func *f, jit_state_t *j, void *arena, case PARAM: { /* move from argument stack to location */ jit_operand_t from = jit_operand_mem( - jit_abi_from(i.r1), - JIT_R1, - arg_offset(i) - ); + jit_abi_from(i.r1), + JIT_R1, + arg_offset(i) + ); jit_operand_t to; if (i.r0 < jit_v_num()) { /* regular register */ - to = jit_operand_gpr(jit_abi_from(i.r1), jit_v(i.r2)); + to = jit_operand_gpr(jit_abi_from(i.r1), + jit_v(i.r2)); } else { /* stack location */ - to = jit_operand_mem(jit_abi_from(i.r1), JIT_SP, stack_loc(i.r2)); + to = jit_operand_mem(jit_abi_from(i.r1), JIT_SP, + stack_loc(i.r2)); } vec_append(&src, &from); @@ -474,7 +502,8 @@ static size_t compile_fn_body(struct ejit_func *f, jit_state_t *j, void *arena, } foreach_vec(ri, relocs) { - struct reloc_helper h = vect_at(struct reloc_helper, relocs, ri); + struct reloc_helper h = vect_at(struct reloc_helper, relocs, + ri); jit_addr_t a = vect_at(jit_addr_t, labels, h.to); jit_reloc_t r = h.r; |