mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-30 11:20:08 +00:00
Fix zero value initialization in IR
This commit is contained in:
+1
-1
@@ -305,7 +305,7 @@ void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init,
|
||||
return;
|
||||
}
|
||||
|
||||
// NOTE(bill): Check to see if the expression it to be aliases
|
||||
// NOTE(bill): Check to see if the expression it to be aliases
|
||||
case Addressing_Builtin:
|
||||
if (e->type != nullptr) {
|
||||
error(type_expr, "A constant alias of a built-in procedure may not have a type initializer");
|
||||
|
||||
+23
-69
@@ -94,7 +94,6 @@ bool check_has_break(AstNode *stmt, bool implicit) {
|
||||
|
||||
// NOTE(bill): The last expression has to be a 'return' statement
|
||||
// TODO(bill): This is a mild hack and should be probably handled properly
|
||||
// TODO(bill): Warn/err against code after 'return' that it won't be executed
|
||||
bool check_is_terminating(AstNode *node) {
|
||||
switch (node->kind) {
|
||||
case_ast_node(rs, ReturnStmt, node);
|
||||
@@ -344,22 +343,6 @@ Type *check_assignment_variable(Checker *c, Operand *lhs, Operand *rhs) {
|
||||
return rhs->type;
|
||||
}
|
||||
|
||||
enum SwitchKind {
|
||||
Switch_Invalid,
|
||||
Switch_Union,
|
||||
Switch_Any,
|
||||
};
|
||||
|
||||
SwitchKind check_valid_type_switch_type(Type *type) {
|
||||
type = type_deref(type);
|
||||
if (is_type_union(type)) {
|
||||
return Switch_Union;
|
||||
}
|
||||
if (is_type_any(type)) {
|
||||
return Switch_Any;
|
||||
}
|
||||
return Switch_Invalid;
|
||||
}
|
||||
|
||||
void check_stmt_internal(Checker *c, AstNode *node, u32 flags);
|
||||
void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
||||
@@ -771,12 +754,32 @@ void check_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
enum SwitchKind {
|
||||
Switch_Invalid,
|
||||
Switch_Union,
|
||||
Switch_Any,
|
||||
};
|
||||
|
||||
SwitchKind check_valid_type_switch_type(Type *type) {
|
||||
type = type_deref(type);
|
||||
if (is_type_union(type)) {
|
||||
return Switch_Union;
|
||||
}
|
||||
if (is_type_any(type)) {
|
||||
return Switch_Any;
|
||||
}
|
||||
return Switch_Invalid;
|
||||
}
|
||||
|
||||
void check_type_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) {
|
||||
ast_node(ss, TypeSwitchStmt, node);
|
||||
Operand x = {};
|
||||
|
||||
mod_flags |= Stmt_BreakAllowed;
|
||||
check_open_scope(c, node);
|
||||
defer (check_close_scope(c));
|
||||
|
||||
check_label(c, ss->label); // TODO(bill): What should the label's "scope" be?
|
||||
|
||||
SwitchKind switch_kind = Switch_Invalid;
|
||||
@@ -802,7 +805,7 @@ void check_type_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) {
|
||||
check_expr(c, &x, rhs);
|
||||
check_assignment(c, &x, nullptr, str_lit("type switch expression"));
|
||||
switch_kind = check_valid_type_switch_type(x.type);
|
||||
if (check_valid_type_switch_type(x.type) == Switch_Invalid) {
|
||||
if (switch_kind == Switch_Invalid) {
|
||||
gbString str = type_to_string(x.type);
|
||||
error(x.expr, "Invalid type for this type switch expression, got '%s'", str);
|
||||
gb_string_free(str);
|
||||
@@ -838,15 +841,14 @@ void check_type_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (lhs->kind != AstNode_Ident) {
|
||||
error(rhs, "Expected an identifier, got '%.*s'", LIT(ast_node_strings[rhs->kind]));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Map<bool> seen = {}; // Multimap, Key: Type *
|
||||
Map<bool> seen = {}; // Key: Type *
|
||||
map_init(&seen, heap_allocator());
|
||||
defer (map_destroy(&seen));
|
||||
|
||||
for_array(i, bs->stmts) {
|
||||
AstNode *stmt = bs->stmts[i];
|
||||
@@ -933,9 +935,6 @@ void check_type_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) {
|
||||
check_stmt_list(c, cc->stmts, mod_flags);
|
||||
check_close_scope(c);
|
||||
}
|
||||
map_destroy(&seen);
|
||||
|
||||
check_close_scope(c);
|
||||
}
|
||||
|
||||
void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
|
||||
@@ -989,51 +988,6 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
|
||||
check_stmt(c, ts->stmt, flags);
|
||||
case_end;
|
||||
|
||||
#if 0
|
||||
case_ast_node(s, IncDecStmt, node);
|
||||
TokenKind op = s->op.kind;
|
||||
switch (op) {
|
||||
case Token_Inc: op = Token_Add; break;
|
||||
case Token_Dec: op = Token_Sub; break;
|
||||
default:
|
||||
error(node, "Invalid inc/dec operation");
|
||||
return;
|
||||
}
|
||||
|
||||
Operand x = {};
|
||||
check_expr(c, &x, s->expr);
|
||||
if (x.mode == Addressing_Invalid) {
|
||||
return;
|
||||
}
|
||||
if (!is_type_integer(x.type) && !is_type_float(x.type)) {
|
||||
gbString e = expr_to_string(s->expr);
|
||||
gbString t = type_to_string(x.type);
|
||||
error(node, "%s%.*s used on non-numeric type %s", e, LIT(s->op.string), t);
|
||||
gb_string_free(t);
|
||||
gb_string_free(e);
|
||||
return;
|
||||
}
|
||||
AstNode *left = s->expr;
|
||||
AstNode *right = gb_alloc_item(c->allocator, AstNode);
|
||||
right->kind = AstNode_BasicLit;
|
||||
right->BasicLit.pos = s->op.pos;
|
||||
right->BasicLit.kind = Token_Integer;
|
||||
right->BasicLit.string = str_lit("1");
|
||||
|
||||
AstNode *be = gb_alloc_item(c->allocator, AstNode);
|
||||
be->kind = AstNode_BinaryExpr;
|
||||
be->BinaryExpr.op = s->op;
|
||||
be->BinaryExpr.op.kind = op;
|
||||
be->BinaryExpr.left = left;
|
||||
be->BinaryExpr.right = right;
|
||||
check_binary_expr(c, &x, be);
|
||||
if (x.mode == Addressing_Invalid) {
|
||||
return;
|
||||
}
|
||||
check_assignment_variable(c, &x, left);
|
||||
case_end;
|
||||
#endif
|
||||
|
||||
case_ast_node(as, AssignStmt, node);
|
||||
switch (as->op.kind) {
|
||||
case Token_Eq: {
|
||||
|
||||
+11
-5
@@ -2008,6 +2008,7 @@ bool check_type_internal(Checker *c, AstNode *e, Type **type, Type *named_type)
|
||||
return false;
|
||||
}
|
||||
*type = t;
|
||||
set_base_type(named_type, *type);
|
||||
return true;
|
||||
case_end;
|
||||
|
||||
@@ -2038,21 +2039,22 @@ bool check_type_internal(Checker *c, AstNode *e, Type **type, Type *named_type)
|
||||
|
||||
case_ast_node(pe, ParenExpr, e);
|
||||
*type = check_type(c, pe->expr, named_type);
|
||||
set_base_type(named_type, *type);
|
||||
return true;
|
||||
case_end;
|
||||
|
||||
case_ast_node(ue, UnaryExpr, e);
|
||||
if (ue->op.kind == Token_Pointer) {
|
||||
switch (ue->op.kind) {
|
||||
case Token_Pointer:
|
||||
*type = make_type_pointer(c->allocator, check_type(c, ue->expr));
|
||||
set_base_type(named_type, *type);
|
||||
return true;
|
||||
} /* else if (ue->op.kind == Token_Maybe) {
|
||||
*type = make_type_maybe(c->allocator, check_type(c, ue->expr));
|
||||
return true;
|
||||
} */
|
||||
}
|
||||
case_end;
|
||||
|
||||
case_ast_node(pt, PointerType, e);
|
||||
*type = make_type_pointer(c->allocator, check_type(c, pt->type));
|
||||
set_base_type(named_type, *type);
|
||||
return true;
|
||||
case_end;
|
||||
|
||||
@@ -2074,12 +2076,14 @@ bool check_type_internal(Checker *c, AstNode *e, Type **type, Type *named_type)
|
||||
Type *elem = check_type(c, at->elem);
|
||||
*type = make_type_slice(c->allocator, elem);
|
||||
}
|
||||
set_base_type(named_type, *type);
|
||||
return true;
|
||||
case_end;
|
||||
|
||||
case_ast_node(dat, DynamicArrayType, e);
|
||||
Type *elem = check_type(c, dat->elem);
|
||||
*type = make_type_dynamic_array(c->allocator, elem);
|
||||
set_base_type(named_type, *type);
|
||||
return true;
|
||||
case_end;
|
||||
|
||||
@@ -2143,6 +2147,7 @@ bool check_type_internal(Checker *c, AstNode *e, Type **type, Type *named_type)
|
||||
check_expr_or_type(c, &o, e);
|
||||
if (o.mode == Addressing_Type) {
|
||||
*type = o.type;
|
||||
set_base_type(named_type, *type);
|
||||
return true;
|
||||
}
|
||||
case_end;
|
||||
@@ -2152,6 +2157,7 @@ bool check_type_internal(Checker *c, AstNode *e, Type **type, Type *named_type)
|
||||
check_expr_or_type(c, &o, e);
|
||||
if (o.mode == Addressing_Type) {
|
||||
*type = o.type;
|
||||
set_base_type(named_type, *type);
|
||||
return true;
|
||||
}
|
||||
case_end;
|
||||
|
||||
+4
-4
@@ -782,7 +782,7 @@ void add_untyped(CheckerInfo *i, AstNode *expression, bool lhs, AddressingMode m
|
||||
if (mode == Addressing_Constant && type == t_invalid) {
|
||||
compiler_error("add_untyped - invalid type: %s", type_to_string(type));
|
||||
}
|
||||
map_set(&i->untyped, hash_node(expression), make_expr_info(lhs, mode, type, value));
|
||||
map_set(&i->untyped, hash_node(expression), make_expr_info(mode, type, value, lhs));
|
||||
}
|
||||
|
||||
void add_type_and_value(CheckerInfo *i, AstNode *expression, AddressingMode mode, Type *type, ExactValue value) {
|
||||
@@ -880,14 +880,14 @@ void add_entity_and_decl_info(Checker *c, AstNode *identifier, Entity *e, DeclIn
|
||||
GB_ASSERT(identifier->kind == AstNode_Ident);
|
||||
GB_ASSERT(e != nullptr && d != nullptr);
|
||||
GB_ASSERT(identifier->Ident.token.string == e->token.string);
|
||||
if (e->scope != nullptr) add_entity(c, e->scope, identifier, e);
|
||||
if (e->scope != nullptr) {
|
||||
add_entity(c, e->scope, identifier, e);
|
||||
}
|
||||
add_entity_definition(&c->info, identifier, e);
|
||||
GB_ASSERT(e->decl_info == nullptr);
|
||||
e->decl_info = d;
|
||||
array_add(&c->info.entities, e);
|
||||
e->order_in_src = c->info.entities.count;
|
||||
// map_set(&c->info.entities, hash_entity(e), d);
|
||||
// e->order_in_src = c->info.entities.entries.count;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+7
-3
@@ -31,14 +31,18 @@ struct TypeAndValue {
|
||||
|
||||
// ExprInfo stores information used for "untyped" expressions
|
||||
struct ExprInfo {
|
||||
bool is_lhs; // Debug info
|
||||
AddressingMode mode;
|
||||
Type * type; // Type_Basic
|
||||
ExactValue value;
|
||||
bool is_lhs; // Debug info
|
||||
};
|
||||
|
||||
gb_inline ExprInfo make_expr_info(bool is_lhs, AddressingMode mode, Type *type, ExactValue value) {
|
||||
ExprInfo ei = {is_lhs, mode, type, value};
|
||||
gb_inline ExprInfo make_expr_info(AddressingMode mode, Type *type, ExactValue value, bool is_lhs) {
|
||||
ExprInfo ei = {};
|
||||
ei.is_lhs = is_lhs;
|
||||
ei.mode = mode;
|
||||
ei.type = type;
|
||||
ei.value = value;
|
||||
return ei;
|
||||
}
|
||||
|
||||
|
||||
+11
-13
@@ -1603,10 +1603,10 @@ void ir_emit_zero_init(irProcedure *p, irValue *address, AstNode *expr) {
|
||||
irValue **args = gb_alloc_array(a, irValue *, 2);
|
||||
args[0] = ir_emit_conv(p, address, t_rawptr);
|
||||
args[1] = ir_const_int(a, type_size_of(a, t));
|
||||
ir_emit(p, ir_instr_zero_init(p, address));
|
||||
if (p->entity->token.string != "__mem_zero") {
|
||||
ir_emit_global_call(p, "__mem_zero", args, 2, expr);
|
||||
}
|
||||
ir_emit(p, ir_instr_zero_init(p, address));
|
||||
}
|
||||
|
||||
irValue *ir_emit_comment(irProcedure *p, String text) {
|
||||
@@ -8033,6 +8033,9 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info
|
||||
irValue *base = ir_type_info(proc, t->Enum.base_type);
|
||||
ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), base);
|
||||
|
||||
// is_export
|
||||
ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 3), ir_const_bool(a, t->Enum.is_export));
|
||||
|
||||
if (t->Enum.field_count > 0) {
|
||||
Entity **fields = t->Enum.fields;
|
||||
isize count = t->Enum.field_count;
|
||||
@@ -8342,22 +8345,17 @@ void ir_gen_tree(irGen *s) {
|
||||
}
|
||||
}
|
||||
|
||||
if (decl->init_expr != nullptr) {
|
||||
if (is_type_any(e->type)) {
|
||||
} else {
|
||||
TypeAndValue tav = type_and_value_of_expr(info, decl->init_expr);
|
||||
if (tav.mode != Addressing_Invalid) {
|
||||
if (tav.value.kind != ExactValue_Invalid) {
|
||||
ExactValue v = tav.value;
|
||||
g->Global.value = ir_add_module_constant(m, tav.type, v);
|
||||
}
|
||||
if (decl->init_expr != nullptr && !is_type_any(e->type)) {
|
||||
TypeAndValue tav = type_and_value_of_expr(info, decl->init_expr);
|
||||
if (tav.mode != Addressing_Invalid) {
|
||||
if (tav.value.kind != ExactValue_Invalid) {
|
||||
ExactValue v = tav.value;
|
||||
g->Global.value = ir_add_module_constant(m, tav.type, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if (g->Global.value == nullptr) {
|
||||
array_add(&global_variables, var);
|
||||
// }
|
||||
array_add(&global_variables, var);
|
||||
|
||||
ir_module_add_value(m, e, g);
|
||||
map_set(&m->members, hash_string(name), g);
|
||||
|
||||
Reference in New Issue
Block a user