mirror of
https://github.com/danog/ir.git
synced 2024-12-03 10:08:29 +01:00
Merge pull request #42 from weltling/examples2
examples: Add new example and some comments
This commit is contained in:
commit
d2d4e275a7
3
Makefile
3
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
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
57
examples/0001-pointer.c
Normal file
57
examples/0001-pointer.c
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* IR - Lightweight JIT Compilation Framework
|
||||
* (Exmaples package)
|
||||
* Copyright (C) 2023 by IR project.
|
||||
* Authors: Anatol Belski <anbelski@linux.microsoft.com>
|
||||
*/
|
||||
|
||||
#include "ir.h"
|
||||
#include "ir_builder.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user