Skip to content

Commit

Permalink
Initial implementation of LLVM export
Browse files Browse the repository at this point in the history
  • Loading branch information
dstogov committed Sep 28, 2023
1 parent 09829a9 commit 51a37f1
Show file tree
Hide file tree
Showing 100 changed files with 2,670 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ OBJS_COMMON = $(BUILD_DIR)/ir.o $(BUILD_DIR)/ir_strtab.o $(BUILD_DIR)/ir_cfg.o \
$(BUILD_DIR)/ir_sccp.o $(BUILD_DIR)/ir_gcm.o $(BUILD_DIR)/ir_ra.o $(BUILD_DIR)/ir_emit.o \
$(BUILD_DIR)/ir_load.o $(BUILD_DIR)/ir_save.o $(BUILD_DIR)/ir_emit_c.o $(BUILD_DIR)/ir_dump.o \
$(BUILD_DIR)/ir_disasm.o $(BUILD_DIR)/ir_gdb.o $(BUILD_DIR)/ir_perf.o $(BUILD_DIR)/ir_check.o \
$(BUILD_DIR)/ir_cpuinfo.o
$(BUILD_DIR)/ir_cpuinfo.o $(BUILD_DIR)/ir_emit_llvm.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 \
Expand Down
13 changes: 11 additions & 2 deletions ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -2205,20 +2205,23 @@ ir_type ir_get_return_type(ir_ctx *ctx)
ir_ref ref;
ir_insn *insn;
uint8_t ret_type = 255;
ir_type type;

/* Check all RETURN nodes */
ref = ctx->ir_base[1].op1;
while (ref) {
insn = &ctx->ir_base[ref];
if (insn->op == IR_RETURN) {
type = ctx->ir_base[insn->op2].type;
check_type:
if (ret_type == 255) {
if (insn->op2) {
ret_type = ctx->ir_base[insn->op2].type;
ret_type = type;
} else {
ret_type = IR_VOID;
}
} else if (insn->op2) {
if (ret_type != ctx->ir_base[insn->op2].type) {
if (ret_type != type) {
IR_ASSERT(0 && "conflicting return types");
return IR_VOID;
}
Expand All @@ -2228,6 +2231,12 @@ ir_type ir_get_return_type(ir_ctx *ctx)
return IR_VOID;
}
}
} else if (insn->op == IR_UNREACHABLE) {
insn = &ctx->ir_base[insn->op1];
if (insn->op == IR_TAILCALL) {
type = insn->type;
goto check_type;
}
}
ref = ctx->ir_base[ref].op3;
}
Expand Down
7 changes: 5 additions & 2 deletions ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ void ir_strtab_free(ir_strtab *strtab);
#define IR_OPT_IN_SCCP (1<<19)
#define IR_LINEAR (1<<20)
#define IR_GEN_NATIVE (1<<21)
#define IR_GEN_C (1<<22)
#define IR_GEN_CODE (1<<22) /* C or LLVM */

/* Temporary: SCCP -> CFG */
#define IR_SCCP_DONE (1<<25)
Expand Down Expand Up @@ -763,7 +763,10 @@ void ir_dump_live_ranges(const ir_ctx *ctx, FILE *f);
void ir_dump_codegen(const ir_ctx *ctx, FILE *f);

/* IR to C conversion (implementation in ir_emit_c.c) */
int ir_emit_c(ir_ctx *ctx, FILE *f);
int ir_emit_c(ir_ctx *ctx, const char *name, FILE *f);

/* IR to LLVM conversion (implementation in ir_emit_llvm.c) */
int ir_emit_llvm(ir_ctx *ctx, const char *name, FILE *f);

/* IR verification API (implementation in ir_check.c) */
bool ir_check(const ir_ctx *ctx);
Expand Down
4 changes: 4 additions & 0 deletions ir_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ bool ir_check(const ir_ctx *ctx)
/* second argument of SHIFT may be incompatible with result */
break;
}
if (insn->op == IR_NOT && insn->type == IR_BOOL) {
/* bolean not */
break;
}
if (sizeof(void*) == 8) {
if (insn->type == IR_ADDR && use_insn->type == IR_U64) {
break;
Expand Down
12 changes: 6 additions & 6 deletions ir_emit_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ static void ir_emit_tailcall(ir_ctx *ctx, FILE *f, ir_insn *insn)
}
fprintf(f, ");\n");
if (insn->type == IR_VOID) {
fprintf(f, "\treturn;");
fprintf(f, "\treturn;\n");
}
}

Expand Down Expand Up @@ -613,7 +613,7 @@ static void ir_emit_store(ir_ctx *ctx, FILE *f, ir_insn *insn)
fprintf(f, ";\n");
}

static int ir_emit_func(ir_ctx *ctx, FILE *f)
static int ir_emit_func(ir_ctx *ctx, const char *name, FILE *f)
{
ir_ref i, n, *p;
ir_insn *insn;
Expand Down Expand Up @@ -642,7 +642,7 @@ static int ir_emit_func(ir_ctx *ctx, FILE *f)

/* Emit function prototype */
fprintf(f, "%s", ir_type_cname[ret_type]);
fprintf(f, " test(");
fprintf(f, " %s(", name);
if (has_params) {
use_list = &ctx->use_lists[1];
n = use_list->count;
Expand Down Expand Up @@ -849,7 +849,7 @@ static int ir_emit_func(ir_ctx *ctx, FILE *f)
IR_ASSERT(bb->successors_count == 0);
fprintf(f, "\treturn");
if (!insn->op2) {
fprintf(f, ";");
fprintf(f, ";\n");
} else {
fprintf(f, " ");
ir_emit_ref(ctx, f, insn->op2);
Expand Down Expand Up @@ -914,7 +914,7 @@ static int ir_emit_func(ir_ctx *ctx, FILE *f)
return 1;
}

int ir_emit_c(ir_ctx *ctx, FILE *f)
int ir_emit_c(ir_ctx *ctx, const char *name, FILE *f)
{
return ir_emit_func(ctx, f);
return ir_emit_func(ctx, name, f);
}
Loading

0 comments on commit 51a37f1

Please sign in to comment.