Merge pull request #42 from weltling/examples2

examples: Add new example and some comments
This commit is contained in:
Dmitry Stogov 2023-06-05 19:15:24 +03:00 committed by GitHub
commit d2d4e275a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 3 deletions

View File

@ -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

View File

@ -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
View 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;
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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