mirror of
https://github.com/danog/ir.git
synced 2024-11-29 20:29:46 +01:00
examples: Add examples and makefiles
Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
This commit is contained in:
parent
75edc8fec5
commit
764f5c3985
11
Makefile
11
Makefile
@ -4,6 +4,8 @@ TARGET = x86_64
|
||||
BUILD = debug
|
||||
BUILD_DIR = .
|
||||
SRC_DIR = .
|
||||
EXAMPLES_SRC_DIR = $(SRC_DIR)/examples
|
||||
EXAMPLES_BUILD_DIR = $(BUILD_DIR)/examples
|
||||
|
||||
CC = gcc
|
||||
CXX = g++
|
||||
@ -45,12 +47,16 @@ 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)/0005-basic-runner-func
|
||||
|
||||
all: $(BUILD_DIR) $(BUILD_DIR)/ir $(BUILD_DIR)/ir_test
|
||||
|
||||
$(BUILD_DIR):
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
|
||||
$(EXAMPLES_BUILD_DIR):
|
||||
@mkdir -p $(EXAMPLES_BUILD_DIR)
|
||||
|
||||
$(BUILD_DIR)/ir: $(OBJS_COMMON) $(OBJS_IR)
|
||||
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) -lcapstone
|
||||
|
||||
@ -84,6 +90,9 @@ $(BUILD_DIR)/ir_emit_$(DASM_ARCH).h: $(SRC_DIR)/ir_$(DASM_ARCH).dasc $(SRC_DIR)/
|
||||
$(OBJS_COMMON) $(OBJS_IR) $(OBJS_IR_TEST): $(BUILD_DIR)/$(notdir %.o): $(SRC_DIR)/$(notdir %.c)
|
||||
$(CC) $(CFLAGS) -I$(BUILD_DIR) -o $@ -c $<
|
||||
|
||||
$(EXAMPLE_EXES): $(EXAMPLES_BUILD_DIR)/$(notdir %): $(EXAMPLES_SRC_DIR)/$(notdir %.c)
|
||||
$(CC) $(CFLAGS) -I$(BUILD_DIR) $< -o $@ $(OBJS_COMMON) $(LDFLAGS) -lcapstone
|
||||
|
||||
$(BUILD_DIR)/ir-test: $(SRC_DIR)/ir-test.cxx
|
||||
$(CXX) -O3 -std=c++17 $(SRC_DIR)/ir-test.cxx -o $(BUILD_DIR)/ir-test
|
||||
|
||||
@ -96,6 +105,8 @@ test: $(BUILD_DIR)/ir $(BUILD_DIR)/ir-test
|
||||
test-ci: $(BUILD_DIR)/ir $(BUILD_DIR)/ir-test
|
||||
$(BUILD_DIR)/ir-test --show-diff $(SRC_DIR)/tests
|
||||
|
||||
examples: $(OBJS_COMMON) $(EXAMPLES_BUILD_DIR) $(EXAMPLE_EXES)
|
||||
|
||||
clean:
|
||||
rm -rf $(BUILD_DIR)/ir $(BUILD_DIR)/ir_test $(BUILD_DIR)/*.o \
|
||||
$(BUILD_DIR)/minilua $(BUILD_DIR)/ir_emit_$(DASM_ARCH).h \
|
||||
|
52
examples/0001-basic.c
Normal file
52
examples/0001-basic.c
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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>
|
||||
|
||||
/*
|
||||
* int32_t myfunc(int32_t x, int32_t y) {
|
||||
* return x - y;
|
||||
* }
|
||||
*/
|
||||
typedef int32_t (*myfunc_t)(int32_t, int32_t);
|
||||
|
||||
void gen_myfunc(ir_ctx *ctx)
|
||||
{
|
||||
ir_START();
|
||||
ir_ref x = ir_PARAM(IR_I32, "x", 1);
|
||||
ir_ref y = ir_PARAM(IR_I32, "y", 2);
|
||||
ir_ref cr = ir_SUB_I32(x, y);
|
||||
ir_RETURN(cr);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
ir_ctx ctx = {0};
|
||||
|
||||
ir_consistency_check();
|
||||
|
||||
ir_init(&ctx, IR_FUNCTION, IR_CONSTS_LIMIT_MIN, IR_INSNS_LIMIT_MIN);
|
||||
|
||||
gen_myfunc(&ctx);
|
||||
|
||||
ir_build_def_use_lists(&ctx);
|
||||
ir_build_cfg(&ctx);
|
||||
ir_match(&ctx);
|
||||
ir_assign_virtual_registers(&ctx);
|
||||
|
||||
size_t size;
|
||||
void *entry = ir_emit_code(&ctx, &size);
|
||||
if (entry) {
|
||||
printf("42 - 24 = %d\n", ((myfunc_t)entry)(42, 24));
|
||||
}
|
||||
|
||||
ir_free(&ctx);
|
||||
|
||||
return 0;
|
||||
}
|
68
examples/0005-basic-runner-func.c
Normal file
68
examples/0005-basic-runner-func.c
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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>
|
||||
|
||||
/*
|
||||
* double myfunc(double x, double y) {
|
||||
* return x - y + .3;
|
||||
* }
|
||||
*/
|
||||
typedef double (*myfunc_t)(double, double);
|
||||
|
||||
void gen_myfunc(ir_ctx *ctx)
|
||||
{
|
||||
ir_START();
|
||||
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 cd = ir_CONST_DOUBLE(.3);
|
||||
cr0 = ir_ADD_D(cr0, cd);
|
||||
|
||||
ir_RETURN(cr0);
|
||||
}
|
||||
|
||||
void run(myfunc_t func)
|
||||
{
|
||||
const double N = 2;
|
||||
double x, y;
|
||||
for (y = -N; y < N; y++) {
|
||||
for (x = -N; x < N; x++) {
|
||||
double ret = func(x, y);
|
||||
printf("%4.1f - %4.1f = %4.1f\n", x, y, ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
ir_ctx ctx = {0};
|
||||
|
||||
ir_consistency_check();
|
||||
|
||||
ir_init(&ctx, IR_FUNCTION, 256, 1024);
|
||||
|
||||
gen_myfunc(&ctx);
|
||||
|
||||
ir_build_def_use_lists(&ctx);
|
||||
ir_build_cfg(&ctx);
|
||||
ir_match(&ctx);
|
||||
ir_assign_virtual_registers(&ctx);
|
||||
|
||||
size_t size;
|
||||
void *entry = ir_emit_code(&ctx, &size);
|
||||
if (entry) {
|
||||
run(entry);
|
||||
}
|
||||
|
||||
ir_free(&ctx);
|
||||
|
||||
return 0;
|
||||
}
|
@ -12,12 +12,15 @@ BUILD_DIR=win32\build_$(VCPKG_TRIPLET)
|
||||
!endif
|
||||
SRC_DIR=.
|
||||
|
||||
EXAMPLES_SRC_DIR=$(SRC_DIR)\examples
|
||||
EXAMPLES_BUILD_DIR=$(BUILD_DIR)\examples
|
||||
|
||||
!if "$(VCPKG_DIR)" == ""
|
||||
VCPKG_DIR=win32\vcpkg
|
||||
!endif
|
||||
PATH=$(PATH);$(VCPKG_DIR)\installed\$(VCPKG_TRIPLET)\bin
|
||||
|
||||
CFLAGS=/nologo /utf-8 /W3 /EHsc /Zi /I$(BUILD_DIR) /I$(VCPKG_DIR)\installed\$(VCPKG_TRIPLET)\include
|
||||
CFLAGS=/nologo /utf-8 /W3 /EHsc /Zi /I$(SRC_DIR) /I$(BUILD_DIR) /I$(VCPKG_DIR)\installed\$(VCPKG_TRIPLET)\include
|
||||
LDFLAGS=/nologo
|
||||
!if "$(BUILD)" == "release"
|
||||
CFLAGS=$(CFLAGS) /MT /Ox
|
||||
@ -68,6 +71,7 @@ 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)\0005-basic-runner-func.exe
|
||||
|
||||
all: $(BUILD_DIR) $(BUILD_DIR)\ir.exe $(BUILD_DIR)\ir_test.exe
|
||||
|
||||
@ -101,6 +105,10 @@ $(BUILD_DIR)\ir_disasm.obj: $(VCPKG_CAPSTONE)
|
||||
{$(SRC_DIR)}.c{$(BUILD_DIR)}.obj:
|
||||
"$(CC)" $(CFLAGS) /Fo$@ /c $<
|
||||
|
||||
{$(EXAMPLES_SRC_DIR)}.c{$(EXAMPLES_BUILD_DIR)}.exe:
|
||||
"$(CC)" $(CFLAGS) /Fo$*.obj /c $<
|
||||
"$(LD)" $(LDFLAGS) $(OBJS_COMMON) $*.obj $(LIBS) /out:$@
|
||||
|
||||
# If the vcpkg dir exists, lets assume we're good with the deps.
|
||||
$(VCPKG_DIR)\vcpkg.exe:
|
||||
git clone https://github.com/Microsoft/vcpkg.git "$(VCPKG_DIR)"
|
||||
@ -112,6 +120,9 @@ $(VCPKG_CAPSTONE): $(VCPKG_DIR)\vcpkg.exe
|
||||
$(BUILD_DIR):
|
||||
md "$(BUILD_DIR)"
|
||||
|
||||
$(EXAMPLES_BUILD_DIR):
|
||||
md "$(EXAMPLES_BUILD_DIR)"
|
||||
|
||||
$(BUILD_DIR)\ir-test.exe: $(SRC_DIR)/ir-test.cxx
|
||||
"$(CXX)" /std:c++17 $(SRC_DIR)\ir-test.cxx /Fe:$@
|
||||
|
||||
@ -124,6 +135,8 @@ test-ci: $(BUILD_DIR)\ir.exe $(BUILD_DIR)\ir-test.exe
|
||||
set PATH=$(VCPKG_DIR)\installed\$(VCPKG_TRIPLET)\bin:%%PATH%%
|
||||
$(BUILD_DIR)\ir-test.exe --show-diff $(SRC_DIR)\tests
|
||||
|
||||
examples: $(OBJS_COMMON) $(EXAMPLES_BUILD_DIR) $(EXAMPLE_EXES)
|
||||
|
||||
clean:
|
||||
del /f /q $(BUILD_DIR)\*.obj $(BUILD_DIR)\*.exe $(BUILD_DIR)\*.pdb $(BUILD_DIR)\*.ilk $(BUILD_DIR)\*.h
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user