mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-13 01:21:38 -07:00
Some global init cleanup; volatile types
This commit is contained in:
+24
-3
@@ -851,6 +851,7 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
|
||||
switch (operand.mode) {
|
||||
case Addressing_Type: {
|
||||
type = operand.type;
|
||||
type->flags |= e->type_flags;
|
||||
set_base_type(named_type, type);
|
||||
goto end;
|
||||
} break;
|
||||
@@ -881,12 +882,15 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
|
||||
|
||||
if (o.mode == Addressing_Type) {
|
||||
set_base_type(type, o.type);
|
||||
o.type->flags |= e->type_flags;
|
||||
return o.type;
|
||||
}
|
||||
case_end;
|
||||
|
||||
case_ast_node(pe, ParenExpr, e);
|
||||
return check_type(c, pe->expr, named_type, cycle_checker);
|
||||
type = check_type(c, pe->expr, named_type, cycle_checker);
|
||||
type->flags |= e->type_flags;
|
||||
return type;
|
||||
case_end;
|
||||
|
||||
case_ast_node(at, ArrayType, e);
|
||||
@@ -894,9 +898,11 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
|
||||
type = make_type_array(c->allocator,
|
||||
check_type(c, at->elem, NULL, cycle_checker),
|
||||
check_array_count(c, at->count));
|
||||
type->flags |= e->type_flags;
|
||||
set_base_type(named_type, type);
|
||||
} else {
|
||||
type = make_type_slice(c->allocator, check_type(c, at->elem));
|
||||
type->flags |= e->type_flags;
|
||||
set_base_type(named_type, type);
|
||||
}
|
||||
goto end;
|
||||
@@ -912,6 +918,7 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
|
||||
error(&c->error_collector, ast_node_token(vt->elem), "Vector element type must be numerical or a boolean. Got `%s`", err_str);
|
||||
}
|
||||
type = make_type_vector(c->allocator, elem, count);
|
||||
type->flags |= e->type_flags;
|
||||
set_base_type(named_type, type);
|
||||
goto end;
|
||||
case_end;
|
||||
@@ -923,6 +930,7 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
|
||||
check_struct_type(c, type, e, cycle_checker);
|
||||
check_close_scope(c);
|
||||
type->Record.node = e;
|
||||
type->flags |= e->type_flags;
|
||||
goto end;
|
||||
case_end;
|
||||
|
||||
@@ -933,6 +941,7 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
|
||||
check_union_type(c, type, e, cycle_checker);
|
||||
check_close_scope(c);
|
||||
type->Record.node = e;
|
||||
type->flags |= e->type_flags;
|
||||
goto end;
|
||||
case_end;
|
||||
|
||||
@@ -943,6 +952,7 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
|
||||
check_raw_union_type(c, type, e, cycle_checker);
|
||||
check_close_scope(c);
|
||||
type->Record.node = e;
|
||||
type->flags |= e->type_flags;
|
||||
goto end;
|
||||
case_end;
|
||||
|
||||
@@ -951,17 +961,20 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
|
||||
set_base_type(named_type, type);
|
||||
check_enum_type(c, type, named_type, e);
|
||||
type->Record.node = e;
|
||||
type->flags |= e->type_flags;
|
||||
goto end;
|
||||
case_end;
|
||||
|
||||
case_ast_node(pt, PointerType, e);
|
||||
type = make_type_pointer(c->allocator, check_type(c, pt->type));
|
||||
type->flags |= e->type_flags;
|
||||
set_base_type(named_type, type);
|
||||
goto end;
|
||||
case_end;
|
||||
|
||||
case_ast_node(pt, ProcType, e);
|
||||
type = alloc_type(c->allocator, Type_Proc);
|
||||
type->flags |= e->type_flags;
|
||||
set_base_type(named_type, type);
|
||||
check_procedure_type(c, type, e);
|
||||
goto end;
|
||||
@@ -973,6 +986,7 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
|
||||
check_expr_or_type(c, &o, e);
|
||||
if (o.mode == Addressing_Type) {
|
||||
type = o.type;
|
||||
type->flags |= e->type_flags;
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
@@ -987,6 +1001,7 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type, CycleChecker *cycle_c
|
||||
|
||||
end:
|
||||
GB_ASSERT(is_type_typed(type));
|
||||
type->flags |= e->type_flags;
|
||||
add_type_and_value(&c->info, e, Addressing_Type, type, null_value);
|
||||
return type;
|
||||
}
|
||||
@@ -3576,9 +3591,15 @@ gbString write_expr_to_string(gbString str, AstNode *node) {
|
||||
case_end;
|
||||
|
||||
case_ast_node(cl, CompoundLit, node);
|
||||
str = gb_string_appendc(str, "(");
|
||||
str = write_expr_to_string(str, cl->type);
|
||||
str = gb_string_appendc(str, " lit)");
|
||||
str = gb_string_appendc(str, "{");
|
||||
gb_for_array(i, cl->elems) {
|
||||
if (i > 0) {
|
||||
str = gb_string_appendc(str, ", ");
|
||||
}
|
||||
str = write_expr_to_string(str, cl->elems[i]);
|
||||
}
|
||||
str = gb_string_appendc(str, "}");
|
||||
case_end;
|
||||
|
||||
case_ast_node(te, TagExpr, node);
|
||||
|
||||
@@ -717,8 +717,9 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
||||
error(&c->error_collector, ast_node_token(node), "Is not an expression");
|
||||
break;
|
||||
default:
|
||||
if (kind == Expr_Stmt)
|
||||
if (kind == Expr_Stmt) {
|
||||
return;
|
||||
}
|
||||
error(&c->error_collector, ast_node_token(node), "Expression is not used");
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -84,11 +84,6 @@ String const type_strings[] = {
|
||||
#undef TYPE_KIND
|
||||
};
|
||||
|
||||
enum TypeFlag {
|
||||
TypeFlag_thread_local = GB_BIT(0),
|
||||
TypeFlag_volatile = GB_BIT(1),
|
||||
};
|
||||
|
||||
enum TypeRecordKind {
|
||||
TypeRecord_Invalid,
|
||||
|
||||
|
||||
+34
-27
@@ -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)));
|
||||
|
||||
@@ -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
@@ -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));
|
||||
|
||||
+49
-11
@@ -69,6 +69,13 @@ enum VarDeclTag {
|
||||
VarDeclTag_thread_local = GB_BIT(0),
|
||||
};
|
||||
|
||||
|
||||
enum TypeFlag : u32 {
|
||||
TypeFlag_thread_local = GB_BIT(0),
|
||||
TypeFlag_volatile = GB_BIT(1),
|
||||
TypeFlag_atomic = GB_BIT(1),
|
||||
};
|
||||
|
||||
enum CallExprKind {
|
||||
CallExpr_Prefix, // call(...)
|
||||
CallExpr_Postfix, // a'call
|
||||
@@ -280,6 +287,7 @@ String const ast_node_strings[] = {
|
||||
struct AstNode {
|
||||
AstNodeKind kind;
|
||||
// AstNode *prev, *next; // NOTE(bill): allow for Linked list
|
||||
u32 type_flags;
|
||||
union {
|
||||
#define AST_NODE_KIND(_kind_name_, name, ...) __VA_ARGS__ _kind_name_;
|
||||
AST_NODE_KINDS
|
||||
@@ -1082,7 +1090,7 @@ AstNode *parse_value(AstFile *f) {
|
||||
return value;
|
||||
}
|
||||
|
||||
AstNode *parse_identifier_or_type(AstFile *f);
|
||||
AstNode *parse_identifier_or_type(AstFile *f, u32 flags = 0);
|
||||
|
||||
|
||||
void check_proc_add_tag(AstFile *f, AstNode *tag_expr, u64 *tags, ProcTag tag, String tag_name) {
|
||||
@@ -1780,8 +1788,16 @@ AstNodeArray parse_struct_params(AstFile *f, isize *decl_count_, b32 using_allow
|
||||
return decls;
|
||||
}
|
||||
|
||||
AstNode *parse_identifier_or_type(AstFile *f) {
|
||||
AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
|
||||
switch (f->cursor[0].kind) {
|
||||
case Token_volatile:
|
||||
next_token(f);
|
||||
return parse_identifier_or_type(f, flags | TypeFlag_volatile);
|
||||
|
||||
case Token_atomic:
|
||||
next_token(f);
|
||||
return parse_identifier_or_type(f, flags | TypeFlag_atomic);
|
||||
|
||||
case Token_Identifier: {
|
||||
AstNode *e = parse_identifier(f);
|
||||
while (f->cursor[0].kind == Token_Period) {
|
||||
@@ -1794,11 +1810,15 @@ AstNode *parse_identifier_or_type(AstFile *f) {
|
||||
// HACK NOTE(bill): For type_of_val(expr)
|
||||
e = parse_call_expr(f, e);
|
||||
}
|
||||
e->type_flags = flags;
|
||||
return e;
|
||||
}
|
||||
|
||||
case Token_Pointer:
|
||||
return make_pointer_type(f, expect_token(f, Token_Pointer), parse_type(f));
|
||||
case Token_Pointer: {
|
||||
AstNode *e = make_pointer_type(f, expect_token(f, Token_Pointer), parse_type(f));
|
||||
e->type_flags = flags;
|
||||
return e;
|
||||
}
|
||||
|
||||
case Token_OpenBracket: {
|
||||
f->expr_level++;
|
||||
@@ -1813,7 +1833,9 @@ AstNode *parse_identifier_or_type(AstFile *f) {
|
||||
}
|
||||
expect_token(f, Token_CloseBracket);
|
||||
f->expr_level--;
|
||||
return make_array_type(f, token, count_expr, parse_type(f));
|
||||
AstNode *e = make_array_type(f, token, count_expr, parse_type(f));
|
||||
e->type_flags = flags;
|
||||
return e;
|
||||
}
|
||||
|
||||
case Token_OpenBrace: {
|
||||
@@ -1822,7 +1844,9 @@ AstNode *parse_identifier_or_type(AstFile *f) {
|
||||
AstNode *count_expr = parse_expr(f, false);
|
||||
expect_token(f, Token_CloseBrace);
|
||||
f->expr_level--;
|
||||
return make_vector_type(f, token, count_expr, parse_type(f));
|
||||
AstNode *e = make_vector_type(f, token, count_expr, parse_type(f));
|
||||
e->type_flags = flags;
|
||||
return e;
|
||||
}
|
||||
|
||||
case Token_struct: {
|
||||
@@ -1840,12 +1864,18 @@ AstNode *parse_identifier_or_type(AstFile *f) {
|
||||
}
|
||||
}
|
||||
|
||||
if (is_packed && is_ordered) {
|
||||
ast_file_err(f, token, "`#ordered` is not needed with `#packed` which implies ordering");
|
||||
}
|
||||
|
||||
Token open = expect_token(f, Token_OpenBrace);
|
||||
isize decl_count = 0;
|
||||
AstNodeArray decls = parse_struct_params(f, &decl_count, true);
|
||||
Token close = expect_token(f, Token_CloseBrace);
|
||||
|
||||
return make_struct_type(f, token, decls, decl_count, is_packed, is_ordered);
|
||||
AstNode *e = make_struct_type(f, token, decls, decl_count, is_packed, is_ordered);
|
||||
e->type_flags = flags;
|
||||
return e;
|
||||
} break;
|
||||
|
||||
case Token_union: {
|
||||
@@ -1855,7 +1885,9 @@ AstNode *parse_identifier_or_type(AstFile *f) {
|
||||
AstNodeArray decls = parse_struct_params(f, &decl_count, false);
|
||||
Token close = expect_token(f, Token_CloseBrace);
|
||||
|
||||
return make_union_type(f, token, decls, decl_count);
|
||||
AstNode *e = make_union_type(f, token, decls, decl_count);
|
||||
e->type_flags = flags;
|
||||
return e;
|
||||
}
|
||||
|
||||
case Token_raw_union: {
|
||||
@@ -1865,7 +1897,9 @@ AstNode *parse_identifier_or_type(AstFile *f) {
|
||||
AstNodeArray decls = parse_struct_params(f, &decl_count, true);
|
||||
Token close = expect_token(f, Token_CloseBrace);
|
||||
|
||||
return make_raw_union_type(f, token, decls, decl_count);
|
||||
AstNode *e = make_raw_union_type(f, token, decls, decl_count);
|
||||
e->type_flags = flags;
|
||||
return e;
|
||||
}
|
||||
|
||||
case Token_enum: {
|
||||
@@ -1900,7 +1934,9 @@ AstNode *parse_identifier_or_type(AstFile *f) {
|
||||
|
||||
close = expect_token(f, Token_CloseBrace);
|
||||
|
||||
return make_enum_type(f, token, base_type, fields);
|
||||
AstNode *e = make_enum_type(f, token, base_type, fields);
|
||||
e->type_flags = flags;
|
||||
return e;
|
||||
}
|
||||
|
||||
case Token_proc: {
|
||||
@@ -1919,7 +1955,9 @@ AstNode *parse_identifier_or_type(AstFile *f) {
|
||||
open = expect_token(f, Token_OpenParen);
|
||||
type = parse_type(f);
|
||||
close = expect_token(f, Token_CloseParen);
|
||||
return make_paren_expr(f, type, open, close);
|
||||
AstNode *e = make_paren_expr(f, type, open, close);
|
||||
e->type_flags = flags;
|
||||
return e;
|
||||
}
|
||||
|
||||
// TODO(bill): Why is this even allowed? Is this a parsing error?
|
||||
|
||||
@@ -104,6 +104,7 @@ TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
|
||||
TOKEN_KIND(Token_using, "using"), \
|
||||
TOKEN_KIND(Token_asm, "asm"), \
|
||||
TOKEN_KIND(Token_volatile, "volatile"), \
|
||||
TOKEN_KIND(Token_atomic, "atomic"), \
|
||||
TOKEN_KIND(Token__KeywordEnd, "_KeywordEnd"), \
|
||||
TOKEN_KIND(Token_Count, "")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user