From 4269b6d3d0aeff1cf1bae3eac5f40cceb75576e0 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sat, 26 Aug 2023 17:13:56 +0300 Subject: apply fixes to make jit-comparison easier --- examples/main.c | 2 +- lib/imm.h | 6 ++++++ lib/imm_decls.h | 1 + lib/imm_defns.h | 8 ++++++++ lib/op_decls.h | 3 +++ lib/op_defns.h | 21 +++++++++++++++++++++ ops/branchi/bani.c | 8 ++++++++ ops/branchi/source.mk | 2 ++ ops/peeko.c | 9 +++++++++ ops/popo.c | 9 +++++++++ ops/pusha.c | 9 +++++++++ ops/source.mk | 1 + src/copyjit.c | 8 +++++--- src/copyjit.h | 4 +++- tests/check.c | 22 ++++++++++++---------- 15 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 ops/branchi/bani.c create mode 100644 ops/branchi/source.mk create mode 100644 ops/peeko.c create mode 100644 ops/popo.c create mode 100644 ops/pusha.c diff --git a/examples/main.c b/examples/main.c index 90fb21d..1da1be0 100644 --- a/examples/main.c +++ b/examples/main.c @@ -31,6 +31,6 @@ int main() compile_finish(&ctx); - printf("%lu\n", run(&ctx)); + printf("%lu\n", run(&ctx, 0, 0, 0, 0)); compile_destroy(&ctx); } diff --git a/lib/imm.h b/lib/imm.h index 1ee7e1e..97580f4 100644 --- a/lib/imm.h +++ b/lib/imm.h @@ -40,4 +40,10 @@ unsigned char gen_liy[] = { 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 }; unsigned int gen_liy_len = 20; +unsigned char gen_bani[] = { + 0x48, 0x85, 0xf6, 0x48, 0x8d, 0x05, 0x12, 0x00, 0x00, 0x00, 0x48, 0x0f, + 0x45, 0x05, 0x02, 0x00, 0x00, 0x00, 0xff, 0xe0, 0x0f, 0x1f, 0x84, 0x00, + 0x00, 0x00, 0x00, 0x00 +}; +unsigned int gen_bani_len = 28; #endif /* COPYJIT_OPS_H */ diff --git a/lib/imm_decls.h b/lib/imm_decls.h index 5a2f840..f520e1f 100644 --- a/lib/imm_decls.h +++ b/lib/imm_decls.h @@ -8,4 +8,5 @@ void *compile_lia(ctx_t *ctx, unsigned long i); void *compile_lio(ctx_t *ctx, unsigned long i); void *compile_lix(ctx_t *ctx, unsigned long i); void *compile_liy(ctx_t *ctx, unsigned long i); +void *compile_bani(ctx_t *ctx, unsigned long i); #endif /* COPYJIT_DECLS_H */ diff --git a/lib/imm_defns.h b/lib/imm_defns.h index 6b9c52a..f3b8baa 100644 --- a/lib/imm_defns.h +++ b/lib/imm_defns.h @@ -64,4 +64,12 @@ void *compile_liy(ctx_t *ctx, unsigned long i) ctx->pc += gen_liy_len; return pc; } +void *compile_bani(ctx_t *ctx, unsigned long i) + { + void *pc = ctx->pc; + memcpy(ctx->pc, gen_bani, gen_bani_len); + *(unsigned long *)(ctx->pc + gen_bani_len - 8) = i; + ctx->pc += gen_bani_len; + return pc; + } #endif /* COPYJIT_DEFNS_H */ diff --git a/lib/op_decls.h b/lib/op_decls.h index 147a6ac..c7eb3fb 100644 --- a/lib/op_decls.h +++ b/lib/op_decls.h @@ -14,6 +14,9 @@ void *compile_movxa(ctx_t *ctx); void *compile_movxo(ctx_t *ctx); void *compile_movya(ctx_t *ctx); void *compile_movyo(ctx_t *ctx); +void *compile_peeko(ctx_t *ctx); +void *compile_popo(ctx_t *ctx); +void *compile_pusha(ctx_t *ctx); void *compile_sub(ctx_t *ctx); void *compile_subxo(ctx_t *ctx); void *compile_subyo(ctx_t *ctx); diff --git a/lib/op_defns.h b/lib/op_defns.h index 5b9cdb2..f930abe 100644 --- a/lib/op_defns.h +++ b/lib/op_defns.h @@ -98,6 +98,27 @@ void *compile_movyo(ctx_t *ctx) ctx->pc += gen_movyo_len; return pc; } +void *compile_peeko(ctx_t *ctx) + { + void *pc = ctx->pc; + memcpy(pc, gen_peeko, gen_peeko_len); + ctx->pc += gen_peeko_len; + return pc; + } +void *compile_popo(ctx_t *ctx) + { + void *pc = ctx->pc; + memcpy(pc, gen_popo, gen_popo_len); + ctx->pc += gen_popo_len; + return pc; + } +void *compile_pusha(ctx_t *ctx) + { + void *pc = ctx->pc; + memcpy(pc, gen_pusha, gen_pusha_len); + ctx->pc += gen_pusha_len; + return pc; + } void *compile_sub(ctx_t *ctx) { void *pc = ctx->pc; diff --git a/ops/branchi/bani.c b/ops/branchi/bani.c new file mode 100644 index 0000000..15708eb --- /dev/null +++ b/ops/branchi/bani.c @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ + +#include "../common.h" + +DEFINE_OP(bani) +{ + BRANCH(IMM(), a != 0, sp, a, x, y, o); +} diff --git a/ops/branchi/source.mk b/ops/branchi/source.mk new file mode 100644 index 0000000..1c5424e --- /dev/null +++ b/ops/branchi/source.mk @@ -0,0 +1,2 @@ +IMM_SRC != echo ops/branchi/*.c +IMM_SOURCES += $(IMM_SRC) diff --git a/ops/peeko.c b/ops/peeko.c new file mode 100644 index 0000000..06e6f2c --- /dev/null +++ b/ops/peeko.c @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ + +#include "common.h" + +DEFINE_OP(peeko) +{ + UNUSED(o); + NEXT_OP(sp, a, x, y, *(sp - 1)); +} diff --git a/ops/popo.c b/ops/popo.c new file mode 100644 index 0000000..5a90524 --- /dev/null +++ b/ops/popo.c @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ + +#include "common.h" + +DEFINE_OP(popo) +{ + UNUSED(o); + NEXT_OP(sp - 1, a, x, y, *(sp - 1)); +} diff --git a/ops/pusha.c b/ops/pusha.c new file mode 100644 index 0000000..8898a77 --- /dev/null +++ b/ops/pusha.c @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ + +#include "common.h" + +DEFINE_OP(pusha) +{ + *sp = a; + NEXT_OP(sp + 1, a, x, y, o); +} diff --git a/ops/source.mk b/ops/source.mk index 0a90169..8db98bc 100644 --- a/ops/source.mk +++ b/ops/source.mk @@ -6,3 +6,4 @@ include ops/bs/source.mk include ops/li/source.mk include ops/sl/source.mk include ops/sr/source.mk +include ops/branchi/source.mk diff --git a/src/copyjit.c b/src/copyjit.c index 96616e3..8ca35eb 100644 --- a/src/copyjit.c +++ b/src/copyjit.c @@ -498,10 +498,12 @@ void compile_finish(ctx_t *ctx) __builtin___clear_cache(ctx->buf, ctx->buf + ctx->size); } -unsigned long run(ctx_t *ctx) +unsigned long run(ctx_t *ctx, + unsigned long a, unsigned long x, unsigned long y, + unsigned long o) { void *sp = malloc(4096); - unsigned long a = CALL(ctx->buf, sp, 0, 0, 0, 0); + unsigned long r = CALL(ctx->buf, sp, a, x, y, o); free(sp); - return a; + return r; } diff --git a/src/copyjit.h b/src/copyjit.h index 6104800..3faac44 100644 --- a/src/copyjit.h +++ b/src/copyjit.h @@ -46,6 +46,8 @@ void compile_start(ctx_t *ctx); void compile_finish(ctx_t *ctx); void compile_destroy(ctx_t *ctx); -unsigned long run(ctx_t *ctx); +unsigned long run(ctx_t *ctx, + unsigned long a, unsigned long x, unsigned long y, + unsigned long o); #endif /* HMMJIT_H */ diff --git a/tests/check.c b/tests/check.c index 168f02f..e302aed 100644 --- a/tests/check.c +++ b/tests/check.c @@ -1,3 +1,5 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ + #include #include @@ -15,7 +17,7 @@ static void check_li() compile_end(&ctx); compile_finish(&ctx); - assert(run(&ctx) == i); + assert(run(&ctx, 0, 0, 0, 0) == i); compile_destroy(&ctx); } @@ -34,7 +36,7 @@ static void check_li() compile_end(&ctx); compile_finish(&ctx); - assert(run(&ctx) == i); + assert(run(&ctx, 0, 0, 0, 0) == i); compile_destroy(&ctx); } @@ -51,7 +53,7 @@ static void check_li() compile_end(&ctx); compile_finish(&ctx); - assert(run(&ctx) == i); + assert(run(&ctx, 0, 0, 0, 0) == i); compile_destroy(&ctx); } @@ -68,7 +70,7 @@ static void check_li() compile_end(&ctx); compile_finish(&ctx); - assert(run(&ctx) == i); + assert(run(&ctx, 0, 0, 0, 0) == i); compile_destroy(&ctx); } } @@ -86,7 +88,7 @@ static void check_add() compile_end(&ctx); compile_finish(&ctx); - assert(run(&ctx) == i); + assert(run(&ctx, 0, 0, 0, 0) == i); compile_destroy(&ctx); } @@ -105,7 +107,7 @@ static void check_add() compile_end(&ctx); compile_finish(&ctx); - assert(run(&ctx) == i); + assert(run(&ctx, 0, 0, 0, 0) == i); compile_destroy(&ctx); } @@ -122,7 +124,7 @@ static void check_add() compile_end(&ctx); compile_finish(&ctx); - assert(run(&ctx) == i); + assert(run(&ctx, 0, 0, 0, 0) == i); compile_destroy(&ctx); } @@ -139,7 +141,7 @@ static void check_add() compile_end(&ctx); compile_finish(&ctx); - assert(run(&ctx) == i); + assert(run(&ctx, 0, 0, 0, 0) == i); compile_destroy(&ctx); } } @@ -163,7 +165,7 @@ static void check_branch() compile_finish(&ctx); - assert(run(&ctx) == (i != 0)); + assert(run(&ctx, 0, 0, 0, 0) == (i != 0)); compile_destroy(&ctx); } @@ -184,7 +186,7 @@ static void check_branch() compile_finish(&ctx); - assert(run(&ctx) == (i == 0)); + assert(run(&ctx, 0, 0, 0, 0) == (i == 0)); compile_destroy(&ctx); } } -- cgit v1.3