diff options
| author | gingerBill <ginger.bill.22@gmail.com> | 2016-08-15 21:22:44 +0100 |
|---|---|---|
| committer | gingerBill <ginger.bill.22@gmail.com> | 2016-08-16 10:56:44 +0100 |
| commit | 0dd17fe84308bba09c6d2ba15f8b8504bbcf5c89 (patch) | |
| tree | 720ab8306146f4febc14c72ae7601fb90bc487dc /src/codegen | |
| parent | dcbb2fcfbdbd3b35ddc44a4c542b7c6375e47214 (diff) | |
Win32 Window Test
Diffstat (limited to 'src/codegen')
| -rw-r--r-- | src/codegen/codegen.cpp | 19 | ||||
| -rw-r--r-- | src/codegen/print_llvm.cpp | 92 | ||||
| -rw-r--r-- | src/codegen/ssa.cpp | 35 |
3 files changed, 94 insertions, 52 deletions
diff --git a/src/codegen/codegen.cpp b/src/codegen/codegen.cpp index 43957ba76..4b9d943c4 100644 --- a/src/codegen/codegen.cpp +++ b/src/codegen/codegen.cpp @@ -71,14 +71,15 @@ 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; - } - } - ssaValue *g = ssa_make_value_global(a, e, value); + // 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); map_set(&m->values, hash_pointer(e), g); map_set(&m->members, hash_string(name), g); } break; @@ -91,6 +92,8 @@ void ssa_gen_code(ssaGen *s) { name = pd->foreign_name; } ssaValue *p = ssa_make_value_procedure(a, m, e->type, decl->type_expr, body, name); + p->proc.tags = pd->tags; + map_set(&m->values, hash_pointer(e), p); map_set(&m->members, hash_string(name), p); } break; diff --git a/src/codegen/print_llvm.cpp b/src/codegen/print_llvm.cpp index 97cf168b9..ae2ef5fb1 100644 --- a/src/codegen/print_llvm.cpp +++ b/src/codegen/print_llvm.cpp @@ -174,21 +174,22 @@ void ssa_print_type(gbFile *f, BaseTypeSizes s, Type *t) { ssa_fprintf(f, "}"); } break; - case Type_Proc: + case Type_Proc: { if (t->proc.result_count == 0) { ssa_fprintf(f, "void"); } else { ssa_print_type(f, s, t->proc.results); } ssa_fprintf(f, " ("); + auto *params = &t->proc.params->tuple; for (isize i = 0; i < t->proc.param_count; i++) { if (i > 0) { ssa_fprintf(f, ", "); } - ssa_print_type(f, s, &t->proc.params[i]); + ssa_print_type(f, s, params->variables[i]->type); } ssa_fprintf(f, ")*"); - break; + } break; } } @@ -208,9 +209,9 @@ void ssa_print_exact_value(gbFile *f, ssaModule *m, ExactValue value, Type *type ssa_print_escape_string(f, value.value_string, false); ssa_fprintf(f, "\""); } break; - case ExactValue_Integer: + case ExactValue_Integer: { ssa_fprintf(f, "%lld", value.value_integer); - break; + } break; case ExactValue_Float: { u64 u = *cast(u64*)&value.value_float; if (is_type_float(type) && type->basic.kind == Basic_f32) { @@ -506,7 +507,7 @@ void ssa_print_instr(gbFile *f, ssaModule *m, ssaValue *value) { case ssaInstr_Call: { auto *call = &instr->call; - Type *result_type = call->type->proc.results; + Type *result_type = call->type; if (result_type) { ssa_fprintf(f, "%%%d = ", value->id); } @@ -521,18 +522,22 @@ void ssa_print_instr(gbFile *f, ssaModule *m, ssaValue *value) { ssa_fprintf(f, "("); - auto *params = &call->type->proc.params->tuple; - for (isize i = 0; i < call->arg_count; i++) { - Entity *e = params->variables[i]; - GB_ASSERT(e != NULL); - Type *t = e->type; - if (i > 0) { - ssa_fprintf(f, ", "); + if (call->arg_count > 0) { + Type *proc_type = get_base_type(ssa_value_type(call->value)); + GB_ASSERT(proc_type->kind == Type_Proc); + auto *params = &proc_type->proc.params->tuple; + for (isize i = 0; i < call->arg_count; i++) { + Entity *e = params->variables[i]; + GB_ASSERT(e != NULL); + Type *t = e->type; + if (i > 0) { + ssa_fprintf(f, ", "); + } + ssa_print_type(f, m->sizes, t); + ssa_fprintf(f, " "); + ssaValue *arg = call->args[i]; + ssa_print_value(f, m, arg, t); } - ssa_print_type(f, m->sizes, t); - ssa_fprintf(f, " "); - ssaValue *arg = call->args[i]; - ssa_print_value(f, m, arg, t); } ssa_fprintf(f, ")\n"); @@ -650,9 +655,18 @@ void ssa_print_proc(gbFile *f, ssaModule *m, ssaProcedure *proc) { ssa_fprintf(f, ") "); - if (proc->body == NULL) { - ssa_fprintf(f, "; foreign procedure\n\n"); - } else { + if (proc->tags != 0) { + if (proc->tags & ProcTag_inline) + ssa_fprintf(f, "alwaysinline "); + if (proc->tags & ProcTag_no_inline) + ssa_fprintf(f, "noinline "); + + + if (proc->tags & ProcTag_foreign) + ssa_fprintf(f, "; foreign\n"); + } + + if (proc->body != NULL) { ssa_fprintf(f, "{\n"); gb_for_array(i, proc->blocks) { ssaBlock *block = proc->blocks[i]; @@ -682,6 +696,7 @@ void ssa_print_type_name(gbFile *f, ssaModule *m, ssaValue *v) { ssa_fprintf(f, "\n"); } + void ssa_print_llvm_ir(gbFile *f, ssaModule *m) { if (m->layout.len > 0) { ssa_fprintf(f, "target datalayout = \"%.*s\"\n", LIT(m->layout)); @@ -695,17 +710,6 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) { ssa_print_encoded_local(f, make_string(".rawptr")); ssa_fprintf(f, " = type i8* ; Basic_rawptr\n\n"); - ssa_fprintf(f, "declare void @llvm.memmove.p0i8.p0i8."); - ssa_print_type(f, m->sizes, t_int); - ssa_fprintf(f, "(i8*, i8*, "); - ssa_print_type(f, m->sizes, t_int); - ssa_fprintf(f, ", i32, i1) argmemonly nounwind \n\n"); - - gb_for_array(i, m->nested_type_names) { - ssaValue *v = m->nested_type_names[i]; - ssa_print_type_name(f, m, v); - } - gb_for_array(member_index, m->members.entries) { auto *entry = &m->members.entries[member_index]; ssaValue *v = entry->value; @@ -716,7 +720,27 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) { ssa_print_type(f, m->sizes, get_base_type(v->type_name.type)); ssa_fprintf(f, "\n"); } break; + } + } + gb_for_array(i, m->nested_type_names) { + ssaValue *v = m->nested_type_names[i]; + ssa_print_type_name(f, m, v); + } + + + ssa_fprintf(f, "declare void @llvm.memmove.p0i8.p0i8."); + ssa_print_type(f, m->sizes, t_int); + ssa_fprintf(f, "(i8*, i8*, "); + ssa_print_type(f, m->sizes, t_int); + ssa_fprintf(f, ", i32, i1) argmemonly nounwind \n\n"); + + + + gb_for_array(member_index, m->members.entries) { + auto *entry = &m->members.entries[member_index]; + ssaValue *v = entry->value; + switch (v->kind) { case ssaValue_Global: { auto *g = &v->global; ssa_print_encoded_global(f, g->entity->token.string); @@ -729,7 +753,11 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) { ssa_print_type(f, m->sizes, get_base_type(g->entity->type)); ssa_fprintf(f, " "); - ssa_print_value(f, m, g->value, g->entity->type); + if (g->value != NULL) { + ssa_print_value(f, m, g->value, g->entity->type); + } else { + ssa_fprintf(f, "zeroinitializer"); + } ssa_fprintf(f, "\n"); } break; diff --git a/src/codegen/ssa.cpp b/src/codegen/ssa.cpp index 1ccbcf5d6..3df8cbd81 100644 --- a/src/codegen/ssa.cpp +++ b/src/codegen/ssa.cpp @@ -46,6 +46,7 @@ struct ssaProcedure { Type * type; AstNode * type_expr; AstNode * body; + u64 tags; gbArray(ssaBlock *) blocks; ssaBlock * curr_block; @@ -318,13 +319,13 @@ Type *ssa_instr_type(ssaInstr *instr) { case ssaInstr_Select: return ssa_value_type(instr->select.true_value); case ssaInstr_Call: { - Type *pt = instr->call.type; - GB_ASSERT(pt->kind == Type_Proc); - auto *tuple = &pt->proc.results->tuple; - if (tuple->variable_count != 1) - return pt->proc.results; - else - return tuple->variables[0]->type; + Type *pt = get_base_type(instr->call.type); + if (pt != NULL) { + if (pt->kind == Type_Tuple && pt->tuple.variable_count == 1) + return pt->tuple.variables[0]->type; + return pt; + } + return NULL; } case ssaInstr_CopyMemory: return t_int; @@ -891,7 +892,7 @@ void ssa_end_procedure_body(ssaProcedure *proc) { case ssaInstr_CopyMemory: continue; case ssaInstr_Call: - if (instr->call.type->proc.results == NULL) { + if (instr->call.type == NULL) { continue; } break; @@ -1194,6 +1195,7 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) { return value; } + Type *src = get_base_type(src_type); Type *dst = get_base_type(t); if (are_types_identical(t, src_type)) @@ -1208,6 +1210,10 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) { // } else if (is_type_integer(dst)) { ev = exact_value_to_integer(ev); + } else if (is_type_pointer(dst)) { + // IMPORTANT NOTE(bill): LLVM doesn't support pointer constants expect `null` + ssaValue *i = ssa_make_value_constant(proc->module->allocator, t_uint, ev); + return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_inttoptr, i, t_uint, dst)); } return ssa_make_value_constant(proc->module->allocator, t, ev); } @@ -1459,6 +1465,8 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue ssaValue *value = ssa_make_value_procedure(proc->module->allocator, proc->module, type, pl->type, pl->body, name); + value->proc.tags = pl->tags; + gb_array_append(proc->children, &value->proc); ssa_build_proc(value, proc); @@ -1693,8 +1701,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue } } - ssaValue *call = ssa_make_instr_call(proc, value, args, arg_count, tv->type); - ssa_value_set_type(call, proc_type_); + ssaValue *call = ssa_make_instr_call(proc, value, args, arg_count, type->results); return ssa_emit(proc, call); case_end; @@ -1725,6 +1732,7 @@ ssaValue *ssa_build_expr(ssaProcedure *proc, AstNode *expr) { ssaValue *elem = ssa_array_elem(proc, array); return ssa_emit_load(proc, ssa_emit_string(proc, elem, ssa_array_len(proc, array))); } + return ssa_make_value_constant(proc->module->allocator, tv->type, tv->value); } @@ -2043,6 +2051,8 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { if (proc->children == NULL) { gb_array_init(proc->children, gb_heap_allocator()); } + + if (pd->body != NULL) { // NOTE(bill): Generate a new name // parent$name-guid @@ -2059,6 +2069,8 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { ssaValue *value = ssa_make_value_procedure(proc->module->allocator, proc->module, e->type, pd->type, pd->body, name); + value->proc.tags = pd->tags; + ssa_module_add_value(proc->module, e, value); gb_array_append(proc->children, &value->proc); ssa_build_proc(value, proc); @@ -2273,7 +2285,7 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { proc->curr_block = init; ssa_build_stmt(proc, fs->init); } - ssaBlock *body = ssa__make_block(proc, node, make_string("for.body")); + ssaBlock *body = ssa_add_block(proc, node, make_string("for.body")); ssaBlock *done = ssa__make_block(proc, node, make_string("for.done")); // NOTE(bill): Append later ssaBlock *loop = body; @@ -2289,7 +2301,6 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { proc->curr_block = loop; if (loop != body) { ssa_build_cond(proc, fs->cond, body, done); - gb_array_append(proc->blocks, body); proc->curr_block = body; } |