mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 23:28:48 +00:00
Better name mangler for SSA generation
TODO: Define better name mangling rules and allow for explicit name overload
This commit is contained in:
+14
-13
@@ -354,12 +354,13 @@ void scope_lookup_parent_entity(Scope *scope, String name, Scope **scope_, Entit
|
||||
continue;
|
||||
}
|
||||
|
||||
if (e->scope == shared) {
|
||||
// Do not return imported entities
|
||||
if (entity_) *entity_ = e;
|
||||
if (scope_) *scope_ = shared;
|
||||
return;
|
||||
if (e->scope != shared) {
|
||||
// Do not return imported entities even #load ones
|
||||
continue;
|
||||
}
|
||||
if (entity_) *entity_ = e;
|
||||
if (scope_) *scope_ = shared;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -529,13 +530,12 @@ void destroy_checker_info(CheckerInfo *i) {
|
||||
}
|
||||
|
||||
|
||||
void init_checker(Checker *c, Parser *parser) {
|
||||
void init_checker(Checker *c, Parser *parser, BaseTypeSizes sizes) {
|
||||
gbAllocator a = gb_heap_allocator();
|
||||
|
||||
c->parser = parser;
|
||||
init_checker_info(&c->info);
|
||||
c->sizes.word_size = 8;
|
||||
c->sizes.max_align = 8;
|
||||
c->sizes = sizes;
|
||||
|
||||
gb_array_init(c->proc_stack, a);
|
||||
gb_array_init(c->procs, a);
|
||||
@@ -832,6 +832,7 @@ void check_type_name_cycles(Checker *c, CycleCheck *cc, Entity *e) {
|
||||
// if (t->kind == Type_Named) {
|
||||
// if (t->Named.type_name == e) {
|
||||
// gb_printf("Illegal cycle %.*s!!!\n", LIT(e->token.string));
|
||||
// GB_PANIC("!!!");
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -841,9 +842,8 @@ void init_type_info_types(Checker *c) {
|
||||
String type_info_str = make_string("Type_Info");
|
||||
Entity *e = current_scope_lookup_entity(c->global_scope, type_info_str);
|
||||
if (e == NULL) {
|
||||
gb_printf_err("Internal Compiler Error: Could not find type declaration for `Type_Info`\n");
|
||||
gb_printf_err("Is `runtime.odin` missing from the `core` directory?\n");
|
||||
gb_exit(1);
|
||||
compiler_error("Could not find type declaration for `Type_Info`\n"
|
||||
"Is `runtime.odin` missing from the `core` directory relative to odin.exe?");
|
||||
}
|
||||
t_type_info = e->type;
|
||||
t_type_info_ptr = make_type_pointer(c->allocator, t_type_info);
|
||||
@@ -854,8 +854,7 @@ void init_type_info_types(Checker *c) {
|
||||
t_type_info_member_ptr = make_type_pointer(c->allocator, t_type_info_member);
|
||||
|
||||
if (record->field_count != 16) {
|
||||
gb_printf_err("Internal Compiler Error: Invalid `Type_Info` layout\n");
|
||||
gb_exit(1);
|
||||
compiler_error("Invalid `Type_Info` layout");
|
||||
}
|
||||
t_type_info_named = record->fields[ 1]->type;
|
||||
t_type_info_integer = record->fields[ 2]->type;
|
||||
@@ -1042,6 +1041,8 @@ void check_parsed_files(Checker *c) {
|
||||
}
|
||||
if (!previously_added) {
|
||||
gb_array_append(file_scope->imported, scope);
|
||||
} else {
|
||||
warning(id->token, "Multiple #import of the same file within this scope");
|
||||
}
|
||||
|
||||
if (are_strings_equal(id->import_name.string, make_string("_"))) {
|
||||
|
||||
+15
-21
@@ -26,37 +26,37 @@ String const entity_strings[] = {
|
||||
};
|
||||
|
||||
|
||||
typedef i64 EntityGuid;
|
||||
typedef struct Type Type;
|
||||
|
||||
struct Entity {
|
||||
EntityKind kind;
|
||||
EntityGuid guid;
|
||||
|
||||
Scope *scope;
|
||||
Token token;
|
||||
Type *type;
|
||||
Entity *using_parent;
|
||||
AstNode *using_expr;
|
||||
Scope * scope;
|
||||
Token token;
|
||||
Type * type;
|
||||
Entity * using_parent;
|
||||
AstNode * using_expr;
|
||||
|
||||
union {
|
||||
struct { ExactValue value; } Constant;
|
||||
struct {
|
||||
b8 visited; // Cycle detection
|
||||
b8 used; // Variable is used
|
||||
b8 anonymous; // Variable is an anonymous
|
||||
b8 is_using; // `using` variable
|
||||
ExactValue value;
|
||||
} Constant;
|
||||
struct {
|
||||
b8 visited; // Cycle detection
|
||||
b8 used; // Variable is used
|
||||
b8 anonymous; // Variable is an anonymous
|
||||
b8 is_using; // `using` variable
|
||||
|
||||
i32 field_index; // Order in source
|
||||
b8 is_field; // Is struct field
|
||||
} Variable;
|
||||
struct {
|
||||
// struct DeclInfo *decl; // Usually NULL
|
||||
} TypeName;
|
||||
struct {
|
||||
b8 pure;
|
||||
} Procedure;
|
||||
struct { BuiltinProcId id; } Builtin;
|
||||
struct {
|
||||
BuiltinProcId id;
|
||||
} Builtin;
|
||||
struct {
|
||||
String path;
|
||||
String name;
|
||||
@@ -78,16 +78,10 @@ b32 is_entity_exported(Entity *e) {
|
||||
return true;
|
||||
}
|
||||
|
||||
gb_global gbAtomic64 entity_guid_counter = {0};
|
||||
|
||||
EntityGuid next_entity_guid(void) {
|
||||
return cast(EntityGuid)gb_atomic64_fetch_add(&entity_guid_counter, 1);
|
||||
}
|
||||
|
||||
Entity *alloc_entity(gbAllocator a, EntityKind kind, Scope *scope, Token token, Type *type) {
|
||||
Entity *entity = gb_alloc_item(a, Entity);
|
||||
entity->kind = kind;
|
||||
entity->guid = next_entity_guid();
|
||||
entity->scope = scope;
|
||||
entity->token = token;
|
||||
entity->type = type;
|
||||
|
||||
@@ -668,7 +668,7 @@ void check_entity_decl(Checker *c, Entity *e, DeclInfo *d, Type *named_type, Cyc
|
||||
|
||||
|
||||
|
||||
void check_var_decl(Checker *c, AstNode *node) {
|
||||
void check_var_decl_node(Checker *c, AstNode *node) {
|
||||
ast_node(vd, VarDecl, node);
|
||||
isize entity_count = gb_array_count(vd->names);
|
||||
isize entity_index = 0;
|
||||
@@ -736,7 +736,7 @@ void check_var_decl(Checker *c, AstNode *node) {
|
||||
}
|
||||
|
||||
|
||||
void check_const_decl(Checker *c, AstNode *node) {
|
||||
void check_const_decl_node(Checker *c, AstNode *node) {
|
||||
ast_node(vd, ConstDecl, node);
|
||||
isize entity_count = gb_array_count(vd->names);
|
||||
isize entity_index = 0;
|
||||
@@ -1425,7 +1425,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
||||
if (gb_array_count(vd->names) > 1 && vd->type != NULL) {
|
||||
error(us->token, "`using` can only be applied to one variable of the same type");
|
||||
}
|
||||
check_var_decl(c, us->node);
|
||||
check_var_decl_node(c, us->node);
|
||||
|
||||
gb_for_array(name_index, vd->names) {
|
||||
AstNode *item = vd->names[name_index];
|
||||
@@ -1467,11 +1467,11 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
||||
|
||||
|
||||
case_ast_node(vd, VarDecl, node);
|
||||
check_var_decl(c, node);
|
||||
check_var_decl_node(c, node);
|
||||
case_end;
|
||||
|
||||
case_ast_node(cd, ConstDecl, node);
|
||||
check_const_decl(c, node);
|
||||
check_const_decl_node(c, node);
|
||||
case_end;
|
||||
|
||||
case_ast_node(pd, ProcDecl, node);
|
||||
|
||||
+11
-8
@@ -96,7 +96,7 @@ enum TypeRecordKind {
|
||||
};
|
||||
|
||||
struct Type {
|
||||
u32 flags;
|
||||
u32 flags; // See parser.cpp `enum TypeFlag`
|
||||
TypeKind kind;
|
||||
union {
|
||||
BasicType Basic;
|
||||
@@ -908,9 +908,9 @@ i64 type_align_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
case Type_Vector: {
|
||||
i64 size = type_size_of(s, allocator, t->Vector.elem);
|
||||
size *= t->Vector.count;
|
||||
size = next_pow2(size);
|
||||
size = prev_pow2(size);
|
||||
// TODO(bill): Type_Vector type_align_of
|
||||
return gb_clamp(size, s.max_align, 4*s.max_align);
|
||||
return gb_clamp(size, 1, s.max_align);
|
||||
} break;
|
||||
|
||||
case Type_Record: {
|
||||
@@ -920,8 +920,9 @@ i64 type_align_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
i64 max = 1;
|
||||
for (isize i = 0; i < t->Record.field_count; i++) {
|
||||
i64 align = type_align_of(s, allocator, t->Record.fields[i]->type);
|
||||
if (max < align)
|
||||
if (max < align) {
|
||||
max = align;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
@@ -931,8 +932,9 @@ i64 type_align_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
for (isize i = 1; i < t->Record.field_count; i++) {
|
||||
// NOTE(bill): field zero is null
|
||||
i64 align = type_align_of(s, allocator, t->Record.fields[i]->type);
|
||||
if (max < align)
|
||||
if (max < align) {
|
||||
max = align;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
} break;
|
||||
@@ -940,8 +942,9 @@ i64 type_align_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
i64 max = 1;
|
||||
for (isize i = 0; i < t->Record.field_count; i++) {
|
||||
i64 align = type_align_of(s, allocator, t->Record.fields[i]->type);
|
||||
if (max < align)
|
||||
if (max < align) {
|
||||
max = align;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
} break;
|
||||
@@ -955,7 +958,6 @@ i64 type_align_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
}
|
||||
|
||||
i64 *type_set_offsets_of(BaseTypeSizes s, gbAllocator allocator, Entity **fields, isize field_count, b32 is_packed) {
|
||||
// TODO(bill): use arena allocation
|
||||
i64 *offsets = gb_alloc_array(allocator, i64, field_count);
|
||||
i64 curr_offset = 0;
|
||||
if (is_packed) {
|
||||
@@ -1040,8 +1042,9 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
switch (t->Record.kind) {
|
||||
case TypeRecord_Struct: {
|
||||
i64 count = t->Record.field_count;
|
||||
if (count == 0)
|
||||
if (count == 0) {
|
||||
return 0;
|
||||
}
|
||||
type_set_offsets(s, allocator, t);
|
||||
return t->Record.struct_offsets[count-1] + type_size_of(s, allocator, t->Record.fields[count-1]->type);
|
||||
} break;
|
||||
|
||||
Reference in New Issue
Block a user