Some global init cleanup; volatile types

This commit is contained in:
Ginger Bill
2016-09-12 12:05:38 +01:00
parent 7509cdceb8
commit 687e78d5dd
8 changed files with 203 additions and 114 deletions
+34 -27
View File
@@ -39,10 +39,6 @@ void ssa_gen_destroy(ssaGen *s) {
gb_file_close(&s->output_file);
}
struct ssaGlobalVariable {
ssaValue *var, *init;
DeclInfo *decl;
};
void ssa_gen_tree(ssaGen *s) {
if (v_zero == NULL) {
@@ -55,6 +51,11 @@ void ssa_gen_tree(ssaGen *s) {
v_true = ssa_make_value_constant(gb_heap_allocator(), t_bool, make_exact_value_bool(true));
}
struct ssaGlobalVariable {
ssaValue *var, *init;
DeclInfo *decl;
};
ssaModule *m = &s->module;
CheckerInfo *info = m->info;
gbAllocator a = m->allocator;
@@ -82,7 +83,24 @@ void ssa_gen_tree(ssaGen *s) {
ssaGlobalVariable var = {};
var.var = g;
var.decl = decl;
gb_array_append(global_variables, var);
if (decl->init_expr != NULL) {
TypeAndValue *tav = map_get(&info->types, hash_pointer(decl->init_expr));
if (tav != NULL && tav->value.kind != ExactValue_Invalid) {
ExactValue v = tav->value;
if (v.kind == ExactValue_String) {
// NOTE(bill): The printer will fix the value correctly
g->Global.value = ssa_add_global_string_array(m, v);
} else {
g->Global.value = ssa_make_value_constant(a, tav->type, v);
}
}
}
if (g->Global.value == NULL) {
gb_array_append(global_variables, var);
}
map_set(&m->values, hash_pointer(e), g);
map_set(&m->members, hash_string(name), g);
} break;
@@ -96,20 +114,6 @@ void ssa_gen_tree(ssaGen *s) {
name = pd->foreign_name;
}
if (are_strings_equal(name, original_name)) {
#if 0
Scope *scope = *map_get(&info->scopes, hash_pointer(pd->type));
isize count = multi_map_count(&scope->elements, hash_string(original_name));
if (count > 1) {
gb_printf("%.*s\n", LIT(name));
isize name_len = name.len + 1 + 10 + 1;
u8 *name_text = gb_alloc_array(m->allocator, u8, name_len);
name_len = gb_snprintf(cast(char *)name_text, name_len, "%.*s$%d", LIT(name), e->guid);
name = make_string(name_text, name_len-1);
}
#endif
}
ssaValue *p = ssa_make_value_procedure(a, m, e->type, decl->type_expr, body, name);
p->Proc.tags = pd->tags;
@@ -183,11 +187,11 @@ void ssa_gen_tree(ssaGen *s) {
ssaValue *type_info_member_data = NULL;
ssaValue **found = NULL;
found = map_get(&proc->module->members, hash_string(make_string("__type_info_data")));
found = map_get(&proc->module->members, hash_string(make_string(SSA_TYPE_INFO_DATA_NAME)));
GB_ASSERT(found != NULL);
type_info_data = *found;
found = map_get(&proc->module->members, hash_string(make_string("__type_info_member_data")));
found = map_get(&proc->module->members, hash_string(make_string(SSA_TYPE_INFO_DATA_MEMBER_NAME)));
GB_ASSERT(found != NULL);
type_info_member_data = *found;
@@ -227,7 +231,7 @@ void ssa_gen_tree(ssaGen *s) {
// TODO(bill): Which is better? The mangled name or actual name?
// ssaValue *gsa = ssa_add_global_string_array(proc, make_exact_value_string(t->Named.name));
ssaValue *gsa = ssa_add_global_string_array(proc, make_exact_value_string(t->Named.type_name->token.string));
ssaValue *gsa = ssa_add_global_string_array(m, make_exact_value_string(t->Named.type_name->token.string));
ssaValue *elem = ssa_array_elem(proc, gsa);
ssaValue *len = ssa_array_len(proc, ssa_emit_load(proc, gsa));
ssaValue *name = ssa_emit_string(proc, elem, len);
@@ -330,14 +334,17 @@ void ssa_gen_tree(ssaGen *s) {
type_set_offsets(m->sizes, a, t); // NOTE(bill): Just incase the offsets have not been set yet
for (isize i = 0; i < t->Record.field_count; i++) {
ssaValue *field = ssa_emit_ptr_offset(proc, memory, ssa_make_value_constant(a, t_int, make_exact_value_integer(i)));
ssaValue *name = ssa_emit_struct_gep(proc, field, v_zero32, t_string_ptr);
ssaValue *type_info = ssa_emit_struct_gep(proc, field, v_one32, t_type_info_ptr_ptr);
ssaValue *offset = ssa_emit_struct_gep(proc, field, v_two32, t_int_ptr);
// NOTE(bill): Order fields in source order not layout order
Entity *f = t->Record.fields[i];
ssaValue *tip = get_type_info_ptr(proc, type_info_data, f->type);
i64 foffset = t->Record.struct_offsets[i];
GB_ASSERT(f->kind == Entity_Variable && f->Variable.is_field);
isize source_index = f->Variable.field_index;
ssaValue *field = ssa_emit_ptr_offset(proc, memory, ssa_make_value_constant(a, t_int, make_exact_value_integer(source_index)));
ssaValue *name = ssa_emit_struct_gep(proc, field, v_zero32, t_string_ptr);
ssaValue *type_info = ssa_emit_struct_gep(proc, field, v_one32, t_type_info_ptr_ptr);
ssaValue *offset = ssa_emit_struct_gep(proc, field, v_two32, t_int_ptr);
if (f->token.string.len > 0) {
ssa_emit_store(proc, name, ssa_emit_global_string(proc, make_exact_value_string(f->token.string)));
+23 -3
View File
@@ -336,9 +336,23 @@ void ssa_print_value(ssaFileBuffer *f, ssaModule *m, ssaValue *value, Type *type
case ssaValue_TypeName:
ssa_print_encoded_local(f, value->TypeName.name);
break;
case ssaValue_Global:
ssa_print_encoded_global(f, value->Global.entity->token.string);
break;
case ssaValue_Global: {
if (type_hint != NULL && is_type_string(type_hint)) {
ssa_fprintf(f, "{i8* getelementptr inbounds (");
ssa_print_type(f, m->sizes, value->Global.entity->type);
ssa_fprintf(f, ", ");
ssa_print_type(f, m->sizes, value->Global.entity->type);
ssa_fprintf(f, "* ");
ssa_print_encoded_global(f, value->Global.entity->token.string);
ssa_fprintf(f, ", ");
ssa_print_type(f, m->sizes, t_int);
ssa_fprintf(f, " 0, i32 0), ");
ssa_print_type(f, m->sizes, t_int);
ssa_fprintf(f, " %lld}", 0);
} else {
ssa_print_encoded_global(f, value->Global.entity->token.string);
}
} break;
case ssaValue_Param:
ssa_print_encoded_local(f, value->Param.entity->token.string);
break;
@@ -385,6 +399,9 @@ void ssa_print_instr(ssaFileBuffer *f, ssaModule *m, ssaValue *value) {
case ssaInstr_Store: {
Type *type = ssa_type(instr);
ssa_fprintf(f, "store ");
if ((type->flags & TypeFlag_volatile) != 0) {
ssa_fprintf(f, "volatile ");
}
ssa_print_type(f, m->sizes, type);
ssa_fprintf(f, " ");
ssa_print_value(f, m, instr->Store.value, type);
@@ -398,6 +415,9 @@ void ssa_print_instr(ssaFileBuffer *f, ssaModule *m, ssaValue *value) {
case ssaInstr_Load: {
Type *type = instr->Load.type;
ssa_fprintf(f, "%%%d = load ", value->id);
if ((type->flags & TypeFlag_volatile) != 0) {
ssa_fprintf(f, "volatile ");
}
ssa_print_type(f, m->sizes, type);
ssa_fprintf(f, ", ");
ssa_print_type(f, m->sizes, type);
+70 -64
View File
@@ -67,7 +67,9 @@ struct ssaProcedure {
ssaTargetList * target_list;
};
#define SSA_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime"
#define SSA_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime"
#define SSA_TYPE_INFO_DATA_NAME "__$type_info_data"
#define SSA_TYPE_INFO_DATA_MEMBER_NAME "__$type_info_data_member"
#define SSA_INSTR_KINDS \
@@ -225,7 +227,6 @@ enum ssaValueKind {
ssaValue_TypeName,
ssaValue_Global,
ssaValue_Param,
ssaValue_GlobalString,
ssaValue_Proc,
ssaValue_Block,
@@ -312,7 +313,7 @@ void ssa_module_init(ssaModule *m, Checker *c) {
{
// Add type info data
{
String name = make_string("__type_info_data");
String name = make_string(SSA_TYPE_INFO_DATA_NAME);
Token token = {Token_Identifier};
token.string = name;
@@ -348,7 +349,7 @@ void ssa_module_init(ssaModule *m, Checker *c) {
}
}
String name = make_string("__type_info_member_data");
String name = make_string(SSA_TYPE_INFO_DATA_MEMBER_NAME);
Token token = {Token_Identifier};
token.string = name;
@@ -914,33 +915,6 @@ ssaValue *ssa_lvalue_load(ssaProcedure *proc, ssaAddr lval) {
}
isize ssa_type_info_index(CheckerInfo *info, Type *type) {
isize entry_index = -1;
HashKey key = hash_pointer(type);
auto *found_entry_index = map_get(&info->type_info_map, key);
if (found_entry_index) {
entry_index = *found_entry_index;
}
if (entry_index < 0) {
// NOTE(bill): Do manual search
// TODO(bill): This is O(n) and can be very slow
gb_for_array(i, info->type_info_map.entries){
auto *e = &info->type_info_map.entries[i];
Type *prev_type = cast(Type *)cast(uintptr)e->key.key;
if (are_types_identical(prev_type, type)) {
entry_index = e->value;
map_set(&info->type_info_map, key, entry_index);
break;
}
}
}
GB_ASSERT(entry_index >= 0);
return entry_index;
}
void ssa_begin_procedure_body(ssaProcedure *proc) {
@@ -1180,6 +1154,45 @@ ssaValue *ssa_emit_deep_field_ev(ssaProcedure *proc, Type *type, ssaValue *e, Se
isize ssa_type_info_index(CheckerInfo *info, Type *type) {
isize entry_index = -1;
HashKey key = hash_pointer(type);
auto *found_entry_index = map_get(&info->type_info_map, key);
if (found_entry_index) {
entry_index = *found_entry_index;
}
if (entry_index < 0) {
// NOTE(bill): Do manual search
// TODO(bill): This is O(n) and can be very slow
gb_for_array(i, info->type_info_map.entries){
auto *e = &info->type_info_map.entries[i];
Type *prev_type = cast(Type *)cast(uintptr)e->key.key;
if (are_types_identical(prev_type, type)) {
entry_index = e->value;
// NOTE(bill): Add it to the search map
map_set(&info->type_info_map, key, entry_index);
break;
}
}
}
GB_ASSERT(entry_index >= 0);
return entry_index;
}
ssaValue *ssa_type_info(ssaProcedure *proc, Type *type) {
ssaValue **found = map_get(&proc->module->members, hash_string(make_string(SSA_TYPE_INFO_DATA_NAME)));
GB_ASSERT(found != NULL);
ssaValue *type_info_data = *found;
CheckerInfo *info = proc->module->info;
isize entry_index = ssa_type_info_index(info, type);
return ssa_emit_struct_gep(proc, type_info_data, entry_index, t_type_info_ptr);
}
ssaValue *ssa_array_elem(ssaProcedure *proc, ssaValue *array) {
@@ -1311,14 +1324,14 @@ ssaValue *ssa_emit_substring(ssaProcedure *proc, ssaValue *base, ssaValue *low,
}
ssaValue *ssa_add_global_string_array(ssaProcedure *proc, ExactValue value) {
ssaValue *ssa_add_global_string_array(ssaModule *m, ExactValue value) {
GB_ASSERT(value.kind == ExactValue_String);
gbAllocator a = gb_heap_allocator();
isize max_len = 4+8+1;
u8 *str = cast(u8 *)gb_alloc_array(a, u8, max_len);
isize len = gb_snprintf(cast(char *)str, max_len, "__str$%x", proc->module->global_string_index);
proc->module->global_string_index++;
isize len = gb_snprintf(cast(char *)str, max_len, "__str$%x", m->global_string_index);
m->global_string_index++;
String name = make_string(str, len-1);
Token token = {Token_String};
@@ -1327,10 +1340,10 @@ ssaValue *ssa_add_global_string_array(ssaProcedure *proc, ExactValue value) {
Entity *entity = make_entity_constant(a, NULL, token, type, value);
ssaValue *g = ssa_make_value_global(a, entity, ssa_make_value_constant(a, type, value));
g->Global.is_private = true;
g->Global.is_constant = true;
// g->Global.is_constant = true;
map_set(&proc->module->values, hash_pointer(entity), g);
map_set(&proc->module->members, hash_string(name), g);
map_set(&m->values, hash_pointer(entity), g);
map_set(&m->members, hash_string(name), g);
return g;
}
@@ -1583,22 +1596,22 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t, b32 is_arg
}
if (is_type_any(dst)) {
ssaValue **found = map_get(&proc->module->members, hash_string(make_string("__type_info_data")));
GB_ASSERT(found != NULL);
ssaValue *type_info_data = *found;
CheckerInfo *info = proc->module->info;
ssaValue *result = ssa_add_local_generated(proc, t_any);
// NOTE(bill): Make copy on stack so I can reference it later
ssaValue *data = ssa_add_local_generated(proc, src_type);
ssa_emit_store(proc, data, value);
ssaValue *data = NULL;
if (value->kind == ssaValue_Instr &&
value->Instr.kind == ssaInstr_Load) {
// NOTE(bill): Addressable value
data = value->Instr.Load.address;
} else {
// NOTE(bill): Non-addressable value
data = ssa_add_local_generated(proc, src_type);
ssa_emit_store(proc, data, value);
}
data = ssa_emit_conv(proc, data, t_rawptr);
isize entry_index = ssa_type_info_index(info, src_type);
ssaValue *ti = ssa_emit_struct_gep(proc, type_info_data, entry_index, t_type_info_ptr);
ssaValue *ti = ssa_type_info(proc, src_type);
ssaValue *gep0 = ssa_emit_struct_gep(proc, result, v_zero32, make_type_pointer(proc->module->allocator, t_type_info_ptr));
ssaValue *gep1 = ssa_emit_struct_gep(proc, result, v_one32, make_type_pointer(proc->module->allocator, t_rawptr));
@@ -1951,6 +1964,12 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
if (found && (*found)->kind == Entity_Builtin) {
Entity *e = *found;
switch (e->Builtin.id) {
case BuiltinProc_type_info: {
ssaValue *x = ssa_build_expr(proc, ce->args[0]);
Type *t = default_type(type_of_expr(proc->module->info, ce->args[0]));
return ssa_type_info(proc, t);
} break;
case BuiltinProc_new: {
ssa_emit_comment(proc, make_string("new"));
// new :: proc(Type) -> ^Type
@@ -2056,7 +2075,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
LIT(pos.file), pos.line, pos.column, expr);
err_len--;
ssaValue *array = ssa_add_global_string_array(proc, make_exact_value_string(make_string(err_str, err_len)));
ssaValue *array = ssa_add_global_string_array(proc->module, make_exact_value_string(make_string(err_str, err_len)));
ssaValue *elem = ssa_array_elem(proc, array);
ssaValue *len = ssa_make_value_constant(proc->module->allocator, t_int, make_exact_value_integer(err_len));
ssaValue *string = ssa_emit_string(proc, elem, len);
@@ -2290,19 +2309,6 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
ssaValue *cond = ssa_emit_comp(proc, lt, x, v_zero);
return ssa_emit_select(proc, cond, neg_x, x);
} break;
case BuiltinProc_type_info: {
ssaValue **found = map_get(&proc->module->members, hash_string(make_string("__type_info_data")));
GB_ASSERT(found != NULL);
ssaValue *type_info_data = *found;
ssaValue *x = ssa_build_expr(proc, ce->args[0]);
Type *t = default_type(type_of_expr(proc->module->info, ce->args[0]));
isize entry_index = ssa_type_info_index(proc->module->info, t);
ssaValue *gep = ssa_emit_struct_gep(proc, type_info_data, entry_index, t_type_info_ptr);
return gep;
} break;
}
}
}
@@ -2415,7 +2421,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
ssaValue *ssa_emit_global_string(ssaProcedure *proc, ExactValue value) {
GB_ASSERT(value.kind == ExactValue_String);
ssaValue *global_array = ssa_add_global_string_array(proc, value);
ssaValue *global_array = ssa_add_global_string_array(proc->module, value);
ssaValue *elem = ssa_array_elem(proc, global_array);
ssaValue *len = ssa_array_len(proc, ssa_emit_load(proc, global_array));
return ssa_emit_string(proc, elem, len);
@@ -2582,7 +2588,7 @@ ssaAddr ssa_build_addr(ssaProcedure *proc, AstNode *expr) {
case Type_Basic: { // Basic_string
TypeAndValue *tv = map_get(&proc->module->info->types, hash_pointer(ie->expr));
if (tv->mode == Addressing_Constant) {
ssaValue *array = ssa_add_global_string_array(proc, tv->value);
ssaValue *array = ssa_add_global_string_array(proc->module, tv->value);
elem = ssa_array_elem(proc, array);
} else {
elem = ssa_string_elem(proc, ssa_build_expr(proc, ie->expr));