diff --git a/src/tilde.cpp b/src/tilde.cpp index 9550374e8..a8c398a69 100644 --- a/src/tilde.cpp +++ b/src/tilde.cpp @@ -250,7 +250,9 @@ gb_internal TB_Symbol *cg_find_symbol_from_entity(cgModule *m, Entity *e) { String link_name = cg_get_entity_name(m, e); cgProcedure **proc_found = string_map_get(&m->procedures, link_name); if (proc_found) { - return (*proc_found)->symbol; + TB_Symbol *symbol = (*proc_found)->symbol; + map_set(&m->symbols, e, symbol); + return symbol; } GB_PANIC("could not find entity's symbol %.*s", LIT(e->token.string)); return nullptr; diff --git a/src/tilde_const.cpp b/src/tilde_const.cpp index 05b57a97a..d37edb89a 100644 --- a/src/tilde_const.cpp +++ b/src/tilde_const.cpp @@ -347,22 +347,23 @@ gb_internal isize cg_global_const_calculate_region_count(ExactValue const &value case ExactValue_Bool: case ExactValue_Integer: case ExactValue_Float: - case ExactValue_Pointer: case ExactValue_Typeid: case ExactValue_Complex: case ExactValue_Quaternion: return 1; + case ExactValue_Pointer: + return 2; case ExactValue_Procedure: return 1; case ExactValue_String: if (is_type_string(type)) { - return 2; + return 3; } else if (is_type_cstring(type) || is_type_array_like(type)) { - return 1; + return 2; } - return 2; + return 3; case ExactValue_Compound: { ast_node(cl, CompoundLit, value.value_compound); @@ -455,7 +456,7 @@ gb_internal isize cg_global_const_calculate_region_count(ExactValue const &value return 1; case Type_Slice: - return 2; + return 3; default: GB_PANIC("TODO(bill): %s", type_to_string(type)); diff --git a/src/tilde_stmt.cpp b/src/tilde_stmt.cpp index 794061335..caa622b4d 100644 --- a/src/tilde_stmt.cpp +++ b/src/tilde_stmt.cpp @@ -2240,7 +2240,65 @@ gb_internal void cg_build_stmt(cgProcedure *p, Ast *node) { } } if (is_static) { - GB_PANIC("TODO(bill): build static variables"); + for_array(i, vd->names) { + Ast *ident = vd->names[i]; + GB_ASSERT(!is_blank_ident(ident)); + Entity *e = entity_of_node(ident); + GB_ASSERT(e->flags & EntityFlag_Static); + String name = e->token.string; + + String mangled_name = {}; + { + gbString str = gb_string_make_length(permanent_allocator(), p->name.text, p->name.len); + str = gb_string_appendc(str, "-"); + str = gb_string_append_fmt(str, ".%.*s-%llu", LIT(name), cast(long long)e->id); + mangled_name.text = cast(u8 *)str; + mangled_name.len = gb_string_length(str); + } + + cgModule *m = p->module; + + TB_DebugType *debug_type = cg_debug_type(m, e->type); + TB_Global *global = tb_global_create(m->mod, mangled_name.len, cast(char const *)mangled_name.text, debug_type, TB_LINKAGE_PRIVATE); + + TB_ModuleSection *section = tb_module_get_data(m->mod); + if (e->Variable.thread_local_model != "") { + section = tb_module_get_tls(m->mod); + String model = e->Variable.thread_local_model; + if (model == "default") { + // TODO(bill): Thread Local Storage models + } else if (model == "localdynamic") { + // TODO(bill): Thread Local Storage models + } else if (model == "initialexec") { + // TODO(bill): Thread Local Storage models + } else if (model == "localexec") { + // TODO(bill): Thread Local Storage models + } else { + GB_PANIC("Unhandled thread local mode %.*s", LIT(model)); + } + } + + i64 max_objects = 0; + ExactValue value = {}; + + if (vd->values.count > 0) { + GB_ASSERT(vd->names.count == vd->values.count); + Ast *ast_value = vd->values[i]; + GB_ASSERT(ast_value->tav.mode == Addressing_Constant || + ast_value->tav.mode == Addressing_Invalid); + + value = ast_value->tav.value; + max_objects = cg_global_const_calculate_region_count(value, e->type); + } + tb_global_set_storage(m->mod, section, global, type_size_of(e->type), type_align_of(e->type), max_objects); + + cg_global_const_add_region(m, value, e->type, global, 0); + + TB_Node *node = tb_inst_get_symbol_address(p->func, cast(TB_Symbol *)global); + cgValue global_val = cg_value(node, alloc_type_pointer(e->type)); + cg_add_entity(p->module, e, global_val); + cg_add_member(p->module, mangled_name, global_val); + } return; }