diff --git a/Makefile b/Makefile index 8369954..4c8477a 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,8 @@ OBJS_COMMON = $(BUILD_DIR)/ir.o $(BUILD_DIR)/ir_strtab.o $(BUILD_DIR)/ir_cfg.o \ $(BUILD_DIR)/ir_cpuinfo.o OBJS_IR = $(BUILD_DIR)/ir_main.o OBJS_IR_TEST = $(BUILD_DIR)/ir_test.o -EXAMPLE_EXES = $(EXAMPLES_BUILD_DIR)/0001-basic $(EXAMPLES_BUILD_DIR)/0001-while $(EXAMPLES_BUILD_DIR)/0005-basic-runner-func +EXAMPLE_EXES = $(EXAMPLES_BUILD_DIR)/0001-basic $(EXAMPLES_BUILD_DIR)/0001-while $(EXAMPLES_BUILD_DIR)/0005-basic-runner-func \ + $(EXAMPLES_BUILD_DIR)/0001-pointer all: $(BUILD_DIR) $(BUILD_DIR)/ir $(BUILD_DIR)/ir_test diff --git a/examples/0001-basic.c b/examples/0001-basic.c index 558d5b1..c90f271 100644 --- a/examples/0001-basic.c +++ b/examples/0001-basic.c @@ -18,10 +18,14 @@ typedef int32_t (*myfunc_t)(int32_t, int32_t); void gen_myfunc(ir_ctx *ctx) { + /* Function entry start */ ir_START(); + /* Declare function parameters */ ir_ref x = ir_PARAM(IR_I32, "x", 1); ir_ref y = ir_PARAM(IR_I32, "y", 2); + /* Subtract y from x and save it into a new ref. */ ir_ref cr = ir_SUB_I32(x, y); + /* Function end */ ir_RETURN(cr); } diff --git a/examples/0001-pointer.c b/examples/0001-pointer.c new file mode 100644 index 0000000..0a14c80 --- /dev/null +++ b/examples/0001-pointer.c @@ -0,0 +1,57 @@ +/* + * IR - Lightweight JIT Compilation Framework + * (Exmaples package) + * Copyright (C) 2023 by IR project. + * Authors: Anatol Belski + */ + +#include "ir.h" +#include "ir_builder.h" +#include + +/* + * void myfunc(uint32_t *i) { + * (*i)++; + * } + */ +typedef uint32_t (*myfunc_t)(uint32_t*); + +void gen_myfunc(ir_ctx *ctx) +{ + /* Function entry start */ + ir_START(); + /* Declare function parameters */ + ir_ref i_ptr = ir_PARAM(IR_U32, "i", 1); + + /* Dereference the argument pointer value, increment and store back. */ + ir_ref i_val = ir_LOAD_U32(i_ptr); + ir_ref i_inc_val = ir_ADD_U32(i_val, ir_CONST_U32(1)); + ir_STORE(i_ptr, i_inc_val); + + /* Function end, declared void */ + ir_RETURN(IR_UNUSED); +} + +int main(int argc, char **argv) +{ + ir_ctx ctx = {0}; + + ir_consistency_check(); + + ir_init(&ctx, IR_FUNCTION | IR_OPT_FOLDING, IR_CONSTS_LIMIT_MIN, IR_INSNS_LIMIT_MIN); + + gen_myfunc(&ctx); + + size_t size; + void *entry = ir_jit_compile(&ctx, 2, &size); + if (entry) { + uint32_t i = 42; + printf("i=%u ", i); + ((myfunc_t)entry)(&i); + printf("i=%u\n", i); + } + + ir_free(&ctx); + + return 0; +} diff --git a/examples/0001-while.c b/examples/0001-while.c index 302fb86..8de7ecc 100644 --- a/examples/0001-while.c +++ b/examples/0001-while.c @@ -20,7 +20,9 @@ typedef int32_t (*myfunc_t)(void); void gen_myfunc(ir_ctx *ctx) { + /* Function entry start */ ir_START(); + /* Declare loop counter. */ ir_ref i = ir_COPY_I32(ir_CONST_I32(0)); ir_ref loop = ir_LOOP_BEGIN(ir_END()); ir_ref phi_i_1 = ir_PHI_2(i, IR_UNUSED); @@ -32,6 +34,7 @@ void gen_myfunc(ir_ctx *ctx) ir_PHI_SET_OP(phi_i_1, 2, i_2); ir_IF_FALSE(cond); + /* Function end */ ir_RETURN(i_2); } diff --git a/examples/0005-basic-runner-func.c b/examples/0005-basic-runner-func.c index c6e0e3f..291eb85 100644 --- a/examples/0005-basic-runner-func.c +++ b/examples/0005-basic-runner-func.c @@ -18,14 +18,17 @@ typedef double (*myfunc_t)(double, double); void gen_myfunc(ir_ctx *ctx) { + /* Function entry start */ ir_START(); + /* Declare function parameters */ ir_ref x = ir_PARAM(IR_DOUBLE, "x", 1); ir_ref y = ir_PARAM(IR_DOUBLE, "y", 2); - ir_ref cr0 = ir_SUB_D(x, y); + ir_ref cr0 = ir_SUB_D(x, y); ir_ref cd = ir_CONST_DOUBLE(.3); cr0 = ir_ADD_D(cr0, cd); + /* Function end */ ir_RETURN(cr0); } diff --git a/win32/Makefile b/win32/Makefile index 88dd326..40c412d 100644 --- a/win32/Makefile +++ b/win32/Makefile @@ -71,7 +71,8 @@ OBJS_COMMON=$(BUILD_DIR)\ir.obj $(BUILD_DIR)\ir_strtab.obj $(BUILD_DIR)\ir_cfg.o $(BUILD_DIR)\ir_disasm.obj $(BUILD_DIR)\ir_check.obj $(BUILD_DIR)\ir_cpuinfo.obj OBJS_IR = $(BUILD_DIR)\ir_main.obj OBJS_IR_TEST = $(BUILD_DIR)\ir_test.obj -EXAMPLE_EXES = $(EXAMPLES_BUILD_DIR)\0001-basic.exe $(EXAMPLES_BUILD_DIR)\0001-while.exe $(EXAMPLES_BUILD_DIR)\0005-basic-runner-func.exe +EXAMPLE_EXES = $(EXAMPLES_BUILD_DIR)\0001-basic.exe $(EXAMPLES_BUILD_DIR)\0001-while.exe $(EXAMPLES_BUILD_DIR)\0005-basic-runner-func.exe \ + $(EXAMPLES_BUILD_DIR)\0001-pointer.exe all: $(BUILD_DIR) $(BUILD_DIR)\ir.exe $(BUILD_DIR)\ir_test.exe