mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-15 18:32:22 -07:00
Merge branch 'master' of http://git.handmadedev.org/gingerbill/Odin
# Conflicts: # examples/main.ll # examples/main.odin # examples/win32.odin # src/codegen/print_llvm.cpp
This commit is contained in:
+64
-9
@@ -39,6 +39,11 @@ void ssa_gen_destroy(ssaGen *s) {
|
||||
gb_file_close(&s->output_file);
|
||||
}
|
||||
|
||||
struct ssaGlobalVariable {
|
||||
ssaValue *var, *init;
|
||||
DeclInfo *decl;
|
||||
};
|
||||
|
||||
void ssa_gen_code(ssaGen *s) {
|
||||
if (v_zero == NULL) {
|
||||
v_zero = ssa_make_value_constant(gb_heap_allocator(), t_int, make_exact_value_integer(0));
|
||||
@@ -53,8 +58,9 @@ void ssa_gen_code(ssaGen *s) {
|
||||
ssaModule *m = &s->module;
|
||||
CheckerInfo *info = m->info;
|
||||
gbAllocator a = m->allocator;
|
||||
ssaProcedure dummy_proc = {};
|
||||
dummy_proc.module = m;
|
||||
gbArray(ssaGlobalVariable) global_variables;
|
||||
gb_array_init(global_variables, gb_heap_allocator());
|
||||
defer (gb_array_free(global_variables));
|
||||
|
||||
gb_for_array(i, info->entities.entries) {
|
||||
auto *entry = &info->entities.entries[i];
|
||||
@@ -71,15 +77,12 @@ void ssa_gen_code(ssaGen *s) {
|
||||
} break;
|
||||
|
||||
case Entity_Variable: {
|
||||
// ssaValue *value = ssa_build_expr(&dummy_proc, decl->init_expr);
|
||||
// if (value->kind == ssaValue_Instr) {
|
||||
// ssaInstr *i = &value->instr;
|
||||
// if (i->kind == ssaInstr_Load) {
|
||||
// value = i->load.address;
|
||||
// }
|
||||
// }
|
||||
// TODO(bill): global runtime initialization
|
||||
ssaValue *g = ssa_make_value_global(a, e, NULL);
|
||||
ssaGlobalVariable var = {};
|
||||
var.var = g;
|
||||
var.decl = decl;
|
||||
gb_array_append(global_variables, var);
|
||||
map_set(&m->values, hash_pointer(e), g);
|
||||
map_set(&m->members, hash_string(name), g);
|
||||
} break;
|
||||
@@ -107,6 +110,58 @@ void ssa_gen_code(ssaGen *s) {
|
||||
ssa_build_proc(v, NULL);
|
||||
}
|
||||
|
||||
|
||||
|
||||
{ // Startup Runtime
|
||||
// Cleanup(bill): probably better way of doing code insertion
|
||||
String name = make_string(SSA_STARTUP_RUNTIME_PROC_NAME);
|
||||
Type *proc_type = make_type_proc(a, gb_alloc_item(a, Scope),
|
||||
NULL, 0,
|
||||
NULL, 0);
|
||||
AstNode *body = gb_alloc_item(a, AstNode);
|
||||
ssaValue *p = ssa_make_value_procedure(a, m, proc_type, NULL, body, name);
|
||||
Token token = {};
|
||||
token.string = name;
|
||||
Entity *e = make_entity_procedure(a, NULL, token, proc_type);
|
||||
|
||||
map_set(&m->values, hash_pointer(e), p);
|
||||
map_set(&m->members, hash_string(name), p);
|
||||
|
||||
ssaProcedure *proc = &p->proc;
|
||||
|
||||
ssa_begin_procedure_body(proc);
|
||||
|
||||
// TODO(bill): Should do a dependency graph do check which order to initialize them in?
|
||||
gb_for_array(i, global_variables) {
|
||||
ssaGlobalVariable *var = &global_variables[i];
|
||||
if (var->decl->init_expr != NULL) {
|
||||
var->init = ssa_build_expr(proc, var->decl->init_expr);
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE(bill): Initialize constants first
|
||||
gb_for_array(i, global_variables) {
|
||||
ssaGlobalVariable *var = &global_variables[i];
|
||||
if (var->init != NULL) {
|
||||
if (var->init->kind == ssaValue_Constant) {
|
||||
ssa_emit_store(proc, var->var, var->init);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gb_for_array(i, global_variables) {
|
||||
ssaGlobalVariable *var = &global_variables[i];
|
||||
if (var->init != NULL) {
|
||||
if (var->init->kind != ssaValue_Constant) {
|
||||
ssa_emit_store(proc, var->var, var->init);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ssa_end_procedure_body(proc);
|
||||
}
|
||||
|
||||
|
||||
// m->layout = make_string("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64");
|
||||
|
||||
ssa_print_llvm_ir(&s->output_file, &s->module);
|
||||
|
||||
@@ -275,6 +275,10 @@ void ssa_print_instr(gbFile *f, ssaModule *m, ssaValue *value) {
|
||||
|
||||
ssa_fprintf(f, "\t");
|
||||
switch (instr->kind) {
|
||||
case ssaInstr_StartupRuntime: {
|
||||
ssa_fprintf(f, "call void @" SSA_STARTUP_RUNTIME_PROC_NAME "()\n");
|
||||
} break;
|
||||
|
||||
case ssaInstr_Local: {
|
||||
Type *type = instr->local.entity->type;
|
||||
ssa_fprintf(f, "%%%d = alloca ", value->id);
|
||||
|
||||
@@ -53,6 +53,7 @@ struct ssaProcedure {
|
||||
ssaTargetList * target_list;
|
||||
};
|
||||
|
||||
#define SSA_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime"
|
||||
|
||||
|
||||
#define SSA_INSTR_KINDS \
|
||||
@@ -73,6 +74,7 @@ struct ssaProcedure {
|
||||
SSA_INSTR_KIND(ExtractElement), \
|
||||
SSA_INSTR_KIND(InsertElement), \
|
||||
SSA_INSTR_KIND(ShuffleVector), \
|
||||
SSA_INSTR_KIND(StartupRuntime), \
|
||||
SSA_INSTR_KIND(Count),
|
||||
|
||||
enum ssaInstrKind {
|
||||
@@ -192,6 +194,8 @@ struct ssaInstr {
|
||||
ssaValue *elem;
|
||||
ssaValue *index;
|
||||
} insert_element;
|
||||
|
||||
struct {} startup_runtime;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -890,6 +894,7 @@ void ssa_end_procedure_body(ssaProcedure *proc) {
|
||||
case ssaInstr_Ret:
|
||||
case ssaInstr_Unreachable:
|
||||
case ssaInstr_CopyMemory:
|
||||
case ssaInstr_StartupRuntime:
|
||||
continue;
|
||||
case ssaInstr_Call:
|
||||
if (instr->call.type == NULL) {
|
||||
@@ -2340,6 +2345,26 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ssa_emit_startup_runtime(ssaProcedure *proc) {
|
||||
GB_ASSERT(proc->parent == NULL && are_strings_equal(proc->name, make_string("main")));
|
||||
|
||||
ssaValue *v = ssa_alloc_instr(proc->module->allocator, ssaInstr_StartupRuntime);
|
||||
if (proc->curr_block) {
|
||||
gb_array_append(proc->curr_block->values, v);
|
||||
}
|
||||
ssa_emit(proc, v);
|
||||
}
|
||||
|
||||
void ssa_insert_code_before_proc(ssaProcedure* proc, ssaProcedure *parent) {
|
||||
if (parent == NULL) {
|
||||
if (are_strings_equal(proc->name, make_string("main"))) {
|
||||
ssa_emit_startup_runtime(proc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ssa_build_proc(ssaValue *value, ssaProcedure *parent) {
|
||||
ssaProcedure *proc = &value->proc;
|
||||
|
||||
@@ -2347,6 +2372,7 @@ void ssa_build_proc(ssaValue *value, ssaProcedure *parent) {
|
||||
|
||||
if (proc->body != NULL) {
|
||||
ssa_begin_procedure_body(proc);
|
||||
ssa_insert_code_before_proc(proc, parent);
|
||||
ssa_build_stmt(proc, proc->body);
|
||||
ssa_end_procedure_body(proc);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user