diff options
Diffstat (limited to 'src/codegen')
| -rw-r--r-- | src/codegen/codegen.cpp | 17 | ||||
| -rw-r--r-- | src/codegen/print_llvm.cpp | 131 | ||||
| -rw-r--r-- | src/codegen/ssa.cpp | 98 |
3 files changed, 138 insertions, 108 deletions
diff --git a/src/codegen/codegen.cpp b/src/codegen/codegen.cpp index cde7951df..678013385 100644 --- a/src/codegen/codegen.cpp +++ b/src/codegen/codegen.cpp @@ -36,11 +36,13 @@ void ssa_gen_destroy(ssaGen *s) { 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)); - v_one = ssa_make_value_constant(gb_heap_allocator(), t_int, make_exact_value_integer(1)); - v_zero32 = ssa_make_value_constant(gb_heap_allocator(), t_i32, make_exact_value_integer(0)); - v_one32 = ssa_make_value_constant(gb_heap_allocator(), t_i32, make_exact_value_integer(1)); - v_two32 = ssa_make_value_constant(gb_heap_allocator(), t_i32, make_exact_value_integer(2)); + v_zero = ssa_make_value_constant(gb_heap_allocator(), t_int, make_exact_value_integer(0)); + v_one = ssa_make_value_constant(gb_heap_allocator(), t_int, make_exact_value_integer(1)); + v_zero32 = ssa_make_value_constant(gb_heap_allocator(), t_i32, make_exact_value_integer(0)); + v_one32 = ssa_make_value_constant(gb_heap_allocator(), t_i32, make_exact_value_integer(1)); + v_two32 = ssa_make_value_constant(gb_heap_allocator(), t_i32, make_exact_value_integer(2)); + v_false = ssa_make_value_constant(gb_heap_allocator(), t_bool, make_exact_value_bool(false)); + v_true = ssa_make_value_constant(gb_heap_allocator(), t_bool, make_exact_value_bool(true)); } ssaModule *m = &s->module; @@ -76,7 +78,8 @@ void ssa_gen_code(ssaGen *s) { } break; case Entity_Procedure: { - ssaValue *p = ssa_make_value_procedure(a, e, decl, m); + AstNode *body = decl->proc_decl->ProcDecl.body; + ssaValue *p = ssa_make_value_procedure(a, m, e->type, decl->type_expr, body, e->token.string); map_set(&m->values, hash_pointer(e), p); map_set(&m->members, hash_string(name), p); } break; @@ -87,7 +90,7 @@ void ssa_gen_code(ssaGen *s) { auto *entry = &m->members.entries[i]; ssaValue *v = entry->value; if (v->kind == ssaValue_Proc) - ssa_build_proc(v); + ssa_build_proc(v, NULL); } // m->layout = make_string("e-p:64:64:64"); diff --git a/src/codegen/print_llvm.cpp b/src/codegen/print_llvm.cpp index ce84cb45a..a9645cca1 100644 --- a/src/codegen/print_llvm.cpp +++ b/src/codegen/print_llvm.cpp @@ -102,10 +102,8 @@ void ssa_print_type(gbFile *f, BaseTypeSizes s, Type *t) { case Basic_u32: ssa_fprintf(f, "i32"); break; case Basic_u64: ssa_fprintf(f, "i64"); break; case Basic_u128: ssa_fprintf(f, "u128"); break; - case Basic_f16: ssa_fprintf(f, "half"); break; case Basic_f32: ssa_fprintf(f, "float"); break; case Basic_f64: ssa_fprintf(f, "double"); break; - case Basic_f128: ssa_fprintf(f, "fp128"); break; case Basic_rawptr: ssa_fprintf(f, "%%.rawptr"); break; case Basic_string: ssa_fprintf(f, "%%.string"); break; case Basic_uint: ssa_fprintf(f, "i%lld", word_bits); break; @@ -199,20 +197,16 @@ void ssa_print_exact_value(gbFile *f, ssaModule *m, ExactValue value, Type *type ssa_fprintf(f, "%lld", value.value_integer); break; case ExactValue_Float: { - u64 u = 0; + u64 u = *cast(u64*)&value.value_float; if (is_type_float(type) && type->basic.kind == Basic_f32) { // IMPORTANT NOTE(bill): LLVM requires all floating point constants to be // a 64 bit number if bits_of(float type) <= 64. - // To overcome this problem, fill the "bottom" 32 bits with zeros + // For some bizarre reason, you need to clear the bottom 28 bits // https://groups.google.com/forum/#!topic/llvm-dev/IlqV3TbSk6M - f32 fp = cast(f32)value.value_float; - u = *cast(u32 *)&fp; - u <<= 32; - - } else { - u = *cast(u64 *)&value.value_float; + u >>= 28; + u <<= 28; } - ssa_fprintf(f, "0x%llx", u); + ssa_fprintf(f, "0x%016llx", u); } break; case ExactValue_Pointer: if (value.value_float == NULL) { @@ -253,7 +247,7 @@ void ssa_print_value(gbFile *f, ssaModule *m, ssaValue *value, Type *type_hint) ssa_print_encoded_local(f, value->param.entity->token.string); break; case ssaValue_Proc: - ssa_print_encoded_global(f, value->proc.entity->token.string); + ssa_print_encoded_global(f, value->proc.name); break; case ssaValue_Instr: ssa_fprintf(f, "%%%d", value->id); @@ -545,8 +539,66 @@ void ssa_print_instr(gbFile *f, ssaModule *m, ssaValue *value) { } } +void ssa_print_proc(gbFile *f, ssaModule *m, ssaProcedure *proc) { + if (proc->body == NULL) { + ssa_fprintf(f, "declare "); + } else { + ssa_fprintf(f, "define "); + } + + auto *proc_type = &proc->type->proc; + + if (proc_type->result_count == 0) { + ssa_fprintf(f, "void"); + } else { + ssa_print_type(f, m->sizes, proc_type->results); + } + + ssa_fprintf(f, " "); + + ssa_print_encoded_global(f, proc->name); + ssa_fprintf(f, "("); + + if (proc_type->param_count > 0) { + auto *params = &proc_type->params->tuple; + for (isize i = 0; i < params->variable_count; i++) { + Entity *e = params->variables[i]; + if (i > 0) + + ssa_fprintf(f, ", "); + ssa_print_type(f, m->sizes, e->type); + ssa_fprintf(f, " %%%.*s", LIT(e->token.string)); + } + } + + ssa_fprintf(f, ") "); + + if (proc->body == NULL) { + ssa_fprintf(f, "\t; foreign procedure\n\n"); + } else { + ssa_fprintf(f, "{\n"); + gb_for_array(i, proc->blocks) { + ssaBlock *block = proc->blocks[i]; + + if (i > 0) ssa_fprintf(f, "\n"); + ssa_print_block_name(f, block); + ssa_fprintf(f, ":\n"); + + gb_for_array(j, block->instrs) { + ssaValue *value = block->instrs[j]; + ssa_print_instr(f, m, value); + } + } + ssa_fprintf(f, "}\n\n"); + } + + gb_for_array(i, proc->anon_procs) { + ssa_print_proc(f, m, proc->anon_procs[i]); + } +} + void ssa_print_llvm_ir(gbFile *f, ssaModule *m) { - if (m->layout.len > 0) { +if (m->layout.len > 0) { ssa_fprintf(f, "target datalayout = %.*s\n", LIT(m->layout)); } @@ -591,58 +643,7 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) { } break; case ssaValue_Proc: { - ssaProcedure *proc = &v->proc; - if (proc->body == NULL) { - ssa_fprintf(f, "declare "); - } else { - ssa_fprintf(f, "define "); - } - - auto *proc_type = &proc->entity->type->proc; - - if (proc_type->result_count == 0) { - ssa_fprintf(f, "void"); - } else { - ssa_print_type(f, m->sizes, proc_type->results); - } - - ssa_fprintf(f, " "); - - ssa_print_encoded_global(f, proc->name); - ssa_fprintf(f, "("); - - if (proc_type->param_count > 0) { - auto *params = &proc_type->params->tuple; - for (isize i = 0; i < params->variable_count; i++) { - Entity *e = params->variables[i]; - if (i > 0) - ssa_fprintf(f, ", "); - ssa_print_type(f, m->sizes, e->type); - ssa_fprintf(f, " %%%.*s", LIT(e->token.string)); - } - } - - ssa_fprintf(f, ") "); - - if (proc->body == NULL) { - ssa_fprintf(f, "\t; foreign procedure\n\n"); - } else { - ssa_fprintf(f, "{\n"); - gb_for_array(i, proc->blocks) { - ssaBlock *block = proc->blocks[i]; - - if (i > 0) ssa_fprintf(f, "\n"); - ssa_print_block_name(f, block); - ssa_fprintf(f, ":\n"); - - gb_for_array(j, block->instrs) { - ssaValue *value = block->instrs[j]; - ssa_print_instr(f, m, value); - } - } - ssa_fprintf(f, "}\n\n"); - } - + ssa_print_proc(f, m, &v->proc); } break; } } diff --git a/src/codegen/ssa.cpp b/src/codegen/ssa.cpp index 90adf2c7d..a126ae5f2 100644 --- a/src/codegen/ssa.cpp +++ b/src/codegen/ssa.cpp @@ -5,10 +5,10 @@ struct ssaValue; struct ssaModule { - CheckerInfo *info; + CheckerInfo * info; BaseTypeSizes sizes; - gbArena arena; - gbAllocator allocator; + gbArena arena; + gbAllocator allocator; String layout; @@ -31,25 +31,25 @@ struct ssaBlock { struct ssaTargetList { ssaTargetList *prev; - ssaBlock *break_; - ssaBlock *continue_; - ssaBlock *fallthrough_; + ssaBlock * break_; + ssaBlock * continue_; + ssaBlock * fallthrough_; }; struct ssaProcedure { - ssaModule *module; - String name; - Entity *entity; - Type *type; - DeclInfo *decl; - AstNode *type_expr; - AstNode *body; + ssaProcedure *parent; + ssaModule * module; + String name; + Entity * entity; + Type * type; + AstNode * type_expr; + AstNode * body; gbArray(ssaBlock *) blocks; - ssaBlock *curr_block; - ssaTargetList *target_list; + ssaBlock * curr_block; + ssaTargetList * target_list; - gbArray(ssaValue *) anonymous_procedures; + gbArray(ssaProcedure *) anon_procs; }; @@ -233,6 +233,8 @@ gb_global ssaValue *v_one = NULL; gb_global ssaValue *v_zero32 = NULL; gb_global ssaValue *v_one32 = NULL; gb_global ssaValue *v_two32 = NULL; +gb_global ssaValue *v_false = NULL; +gb_global ssaValue *v_true = NULL; enum ssaLvalueKind { ssaLvalue_Blank, @@ -397,6 +399,7 @@ ssaValue *ssa_build_expr(ssaProcedure *proc, AstNode *expr); ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv); ssaLvalue ssa_build_addr(ssaProcedure *proc, AstNode *expr); ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *a_type); +void ssa_build_proc(ssaValue *value, ssaProcedure *parent); @@ -603,13 +606,13 @@ ssaValue *ssa_make_value_constant(gbAllocator a, Type *type, ExactValue value) { return v; } -ssaValue *ssa_make_value_procedure(gbAllocator a, Entity *e, DeclInfo *decl, ssaModule *m) { +ssaValue *ssa_make_value_procedure(gbAllocator a, ssaModule *m, Type *type, AstNode *type_expr, AstNode *body, String name) { ssaValue *v = ssa_alloc_value(a, ssaValue_Proc); v->proc.module = m; - v->proc.entity = e; - v->proc.type = e->type; - v->proc.decl = decl; - v->proc.name = e->token.string; + v->proc.type = type; + v->proc.type_expr = type_expr; + v->proc.body = body; + v->proc.name = name; return v; } @@ -752,9 +755,10 @@ void ssa_emit_defer_stmts(ssaProcedure *proc, ssaBlock *block) { // IMPORTANT TODO(bill): ssa defer - Place where needed!!! +#if 0 Scope *curr_scope = block->scope; if (curr_scope == NULL) { - GB_PANIC("No scope found for deferred statements"); + // GB_PANIC("No scope found for deferred statements"); } for (Scope *s = curr_scope; s != NULL; s = s->parent) { @@ -763,6 +767,7 @@ void ssa_emit_defer_stmts(ssaProcedure *proc, ssaBlock *block) { ssa_build_stmt(proc, s->deferred_stmts[i]); } } +#endif } void ssa_emit_unreachable(ssaProcedure *proc) { @@ -1183,7 +1188,7 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) { } if (is_type_integer(src) && is_type_float(dst)) { ssaConvKind kind = ssaConv_sitofp; - if (is_type_unsigned(dst)) { + if (is_type_unsigned(src)) { kind = ssaConv_uitofp; } return ssa_emit(proc, ssa_make_instr_conv(proc, kind, value, src, dst)); @@ -1331,7 +1336,29 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue case_end; case_ast_node(pl, ProcLit, expr); - GB_PANIC("TODO(bill): ssa_build_single_expr ProcLit"); + if (proc->anon_procs == NULL) { + // TODO(bill): Cleanup + gb_array_init(proc->anon_procs, gb_heap_allocator()); + } + // NOTE(bill): Generate a new name + // parent$count + isize name_len = proc->name.len + 1 + 8 + 1; + u8 *name_text = gb_alloc_array(proc->module->allocator, u8, name_len); + name_len = gb_snprintf(cast(char *)name_text, name_len, "%.*s$%d", LIT(proc->name), cast(i32)gb_array_count(proc->anon_procs)); + String name = make_string(name_text, name_len-1); + + + // auto **found = map_get(&proc->module->info->definitions, + // hash_pointer(expr)) + Type *type = type_of_expr(proc->module->info, expr); + ssaValue *value = ssa_make_value_procedure(proc->module->allocator, + proc->module, type, pl->type, pl->body, name); + ssaProcedure *np = &value->proc; + + gb_array_append(proc->anon_procs, np); + ssa_build_proc(value, proc); + + return value; // TODO(bill): Is this correct? case_end; @@ -1673,6 +1700,7 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { if (!ssa_is_blank_ident(name)) { ssa_add_local_for_identifier(proc, name); lval = ssa_build_addr(proc, name); + GB_ASSERT(lval.address.value != NULL); } gb_array_append(lvals, lval); @@ -1685,8 +1713,10 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { gb_for_array(i, inits) { - ssaValue *v = ssa_emit_conv(proc, inits[i], ssa_lvalue_type(lvals[i])); - ssa_lvalue_store(lvals[i], proc, v); + if (lvals[i].kind != ssaLvalue_Blank) { + ssaValue *v = ssa_emit_conv(proc, inits[i], ssa_lvalue_type(lvals[i])); + ssa_lvalue_store(lvals[i], proc, v); + } } } else if (vd->value_count == 0) { // declared and zero-initialized @@ -1736,6 +1766,10 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { } case_end; + case_ast_node(pd, ProcDecl, node); + + case_end; + case_ast_node(ids, IncDecStmt, node); Token op = ids->op; if (op.kind == Token_Increment) { @@ -1957,18 +1991,10 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { } } -void ssa_build_proc(ssaValue *value) { +void ssa_build_proc(ssaValue *value, ssaProcedure *parent) { ssaProcedure *proc = &value->proc; - AstNode *proc_decl = proc->decl->proc_decl; - switch (proc_decl->kind) { - case_ast_node(pd, ProcDecl, proc_decl); - proc->type_expr = pd->type; - proc->body = pd->body; - case_end; - default: - return; - } + proc->parent = parent; if (proc->body != NULL) { ssa_begin_procedure_body(proc); |