mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-15 10:22:23 -07:00
Begin reording of struct members by default.
This commit is contained in:
+85
-30
@@ -179,16 +179,25 @@ void ssa_gen_tree(ssaGen *s) {
|
||||
}
|
||||
|
||||
{ // NOTE(bill): Setup type_info data
|
||||
ssaValue **found = map_get(&proc->module->members, hash_string(make_string("__type_info_data")));
|
||||
ssaValue *type_info_data = NULL;
|
||||
ssaValue *type_info_member_data = NULL;
|
||||
|
||||
ssaValue **found = NULL;
|
||||
found = map_get(&proc->module->members, hash_string(make_string("__type_info_data")));
|
||||
GB_ASSERT(found != NULL);
|
||||
ssaValue *type_info_data = *found;
|
||||
type_info_data = *found;
|
||||
|
||||
found = map_get(&proc->module->members, hash_string(make_string("__type_info_member_data")));
|
||||
GB_ASSERT(found != NULL);
|
||||
type_info_member_data = *found;
|
||||
|
||||
CheckerInfo *info = proc->module->info;
|
||||
|
||||
|
||||
Type *t_int_ptr = make_type_pointer(a, t_int);
|
||||
Type *t_bool_ptr = make_type_pointer(a, t_bool);
|
||||
Type *t_string_ptr = make_type_pointer(a, t_string);
|
||||
Type *t_type_info_ptr_ptr = make_type_pointer(a, t_type_info_ptr);
|
||||
// Useful types
|
||||
Type *t_int_ptr = make_type_pointer(a, t_int);
|
||||
Type *t_bool_ptr = make_type_pointer(a, t_bool);
|
||||
Type *t_string_ptr = make_type_pointer(a, t_string);
|
||||
Type *t_type_info_ptr_ptr = make_type_pointer(a, t_type_info_ptr);
|
||||
|
||||
|
||||
auto get_type_info_ptr = [](ssaProcedure *proc, ssaValue *type_info_data, Type *type) -> ssaValue * {
|
||||
@@ -197,6 +206,15 @@ void ssa_gen_tree(ssaGen *s) {
|
||||
t_type_info_ptr);
|
||||
};
|
||||
|
||||
isize type_info_member_index = 0;
|
||||
|
||||
auto type_info_member_offset = [](ssaProcedure *proc, ssaValue *data, isize count, isize *index) -> ssaValue * {
|
||||
ssaValue *offset = ssa_emit_struct_gep(proc, data, *index, t_type_info_member_ptr);
|
||||
*index += count;
|
||||
return offset;
|
||||
};
|
||||
|
||||
|
||||
gb_for_array(entry_index, info->type_info_map.entries) {
|
||||
auto *entry = &info->type_info_map.entries[entry_index];
|
||||
Type *t = cast(Type *)cast(uintptr)entry->key.key;
|
||||
@@ -207,7 +225,9 @@ void ssa_gen_tree(ssaGen *s) {
|
||||
case Type_Named: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_named);
|
||||
|
||||
ssaValue *gsa = ssa_add_global_string_array(proc, make_exact_value_string(t->Named.name));
|
||||
// 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 *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);
|
||||
@@ -303,16 +323,10 @@ void ssa_gen_tree(ssaGen *s) {
|
||||
} break;
|
||||
case Type_Record: {
|
||||
switch (t->Record.kind) {
|
||||
// TODO(bill): Record members for `Type_Info`
|
||||
case TypeRecord_Struct: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_struct);
|
||||
ssaValue **args = gb_alloc_array(a, ssaValue *, 1);
|
||||
isize element_size = type_size_of(m->sizes, a, t_type_info_member);
|
||||
isize allocation_size = t->Record.field_count * element_size;
|
||||
ssaValue *size = ssa_make_value_constant(a, t_int, make_exact_value_integer(allocation_size));
|
||||
args[0] = size;
|
||||
ssaValue *memory = ssa_emit_global_call(proc, "alloc", args, 1);
|
||||
memory = ssa_emit_conv(proc, memory, t_type_info_member_ptr);
|
||||
|
||||
ssaValue *memory = type_info_member_offset(proc, type_info_member_data, t->Record.field_count, &type_info_member_index);
|
||||
|
||||
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++) {
|
||||
@@ -325,7 +339,9 @@ void ssa_gen_tree(ssaGen *s) {
|
||||
ssaValue *tip = get_type_info_ptr(proc, type_info_data, f->type);
|
||||
i64 foffset = t->Record.struct_offsets[i];
|
||||
|
||||
ssa_emit_store(proc, name, ssa_emit_global_string(proc, make_exact_value_string(f->token.string)));
|
||||
if (f->token.string.len > 0) {
|
||||
ssa_emit_store(proc, name, ssa_emit_global_string(proc, make_exact_value_string(f->token.string)));
|
||||
}
|
||||
ssa_emit_store(proc, type_info, tip);
|
||||
ssa_emit_store(proc, offset, ssa_make_value_constant(a, t_int, make_exact_value_integer(foffset)));
|
||||
}
|
||||
@@ -348,13 +364,8 @@ void ssa_gen_tree(ssaGen *s) {
|
||||
break;
|
||||
case TypeRecord_RawUnion: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_raw_union);
|
||||
ssaValue **args = gb_alloc_array(a, ssaValue *, 1);
|
||||
isize element_size = type_size_of(m->sizes, a, t_type_info_member);
|
||||
isize allocation_size = t->Record.field_count * element_size;
|
||||
ssaValue *size = ssa_make_value_constant(a, t_int, make_exact_value_integer(allocation_size));
|
||||
args[0] = size;
|
||||
ssaValue *memory = ssa_emit_global_call(proc, "alloc", args, 1);
|
||||
memory = ssa_emit_conv(proc, memory, t_type_info_member_ptr);
|
||||
|
||||
ssaValue *memory = type_info_member_offset(proc, type_info_member_data, t->Record.field_count, &type_info_member_index);
|
||||
|
||||
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)));
|
||||
@@ -365,7 +376,9 @@ void ssa_gen_tree(ssaGen *s) {
|
||||
Entity *f = t->Record.fields[i];
|
||||
ssaValue *tip = get_type_info_ptr(proc, type_info_data, f->type);
|
||||
|
||||
ssa_emit_store(proc, name, ssa_emit_global_string(proc, make_exact_value_string(f->token.string)));
|
||||
if (f->token.string.len > 0) {
|
||||
ssa_emit_store(proc, name, ssa_emit_global_string(proc, make_exact_value_string(f->token.string)));
|
||||
}
|
||||
ssa_emit_store(proc, type_info, tip);
|
||||
ssa_emit_store(proc, offset, ssa_make_value_constant(a, t_int, make_exact_value_integer(0)));
|
||||
}
|
||||
@@ -395,12 +408,54 @@ void ssa_gen_tree(ssaGen *s) {
|
||||
}
|
||||
} break;
|
||||
|
||||
case Type_Tuple:
|
||||
// TODO(bill): Type_Info for tuples
|
||||
break;
|
||||
case Type_Proc:
|
||||
case Type_Tuple: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_tuple);
|
||||
|
||||
ssaValue *memory = type_info_member_offset(proc, type_info_member_data, t->Tuple.variable_count, &type_info_member_index);
|
||||
|
||||
for (isize i = 0; i < t->Tuple.variable_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);
|
||||
// NOTE(bill): offset is not used for tuples
|
||||
|
||||
Entity *f = t->Tuple.variables[i];
|
||||
ssaValue *tip = get_type_info_ptr(proc, type_info_data, f->type);
|
||||
|
||||
if (f->token.string.len > 0) {
|
||||
ssa_emit_store(proc, name, ssa_emit_global_string(proc, make_exact_value_string(f->token.string)));
|
||||
}
|
||||
ssa_emit_store(proc, type_info, tip);
|
||||
}
|
||||
|
||||
Type *slice_type = make_type_slice(a, t_type_info_member);
|
||||
Type *slice_type_ptr = make_type_pointer(a, slice_type);
|
||||
ssaValue *slice = ssa_emit_struct_gep(proc, tag, v_zero32, slice_type_ptr);
|
||||
ssaValue *variable_count = ssa_make_value_constant(a, t_int, make_exact_value_integer(t->Tuple.variable_count));
|
||||
|
||||
ssaValue *elem = ssa_emit_struct_gep(proc, slice, v_zero32, make_type_pointer(a, t_type_info_member_ptr));
|
||||
ssaValue *len = ssa_emit_struct_gep(proc, slice, v_one32, make_type_pointer(a, t_int_ptr));
|
||||
ssaValue *cap = ssa_emit_struct_gep(proc, slice, v_two32, make_type_pointer(a, t_int_ptr));
|
||||
|
||||
ssa_emit_store(proc, elem, memory);
|
||||
ssa_emit_store(proc, len, variable_count);
|
||||
ssa_emit_store(proc, cap, variable_count);
|
||||
} break;
|
||||
|
||||
case Type_Proc: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_procedure);
|
||||
|
||||
ssaValue *params = ssa_emit_struct_gep(proc, tag, v_zero32, t_type_info_ptr_ptr);
|
||||
ssaValue *results = ssa_emit_struct_gep(proc, tag, v_one32, t_type_info_ptr_ptr);
|
||||
ssaValue *variadic = ssa_emit_struct_gep(proc, tag, v_two32, t_bool_ptr);
|
||||
|
||||
|
||||
ssa_emit_store(proc, params, get_type_info_ptr(proc, type_info_data, t->Proc.params));
|
||||
ssa_emit_store(proc, results, get_type_info_ptr(proc, type_info_data, t->Proc.results));
|
||||
ssa_emit_store(proc, variadic, ssa_make_value_constant(a, t_bool, make_exact_value_bool(t->Proc.variadic)));
|
||||
|
||||
// TODO(bill): Type_Info for procedures
|
||||
break;
|
||||
} break;
|
||||
}
|
||||
|
||||
if (tag != NULL) {
|
||||
|
||||
+46
-13
@@ -294,6 +294,7 @@ ssaAddr ssa_make_addr_vector(ssaValue *addr, ssaValue *index, AstNode *expr) {
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_make_value_global(gbAllocator a, Entity *e, ssaValue *value);
|
||||
|
||||
|
||||
void ssa_module_init(ssaModule *m, Checker *c) {
|
||||
@@ -310,21 +311,53 @@ void ssa_module_init(ssaModule *m, Checker *c) {
|
||||
|
||||
{
|
||||
// Add type info data
|
||||
ssaValue *ssa_make_value_global(gbAllocator a, Entity *e, ssaValue *value);
|
||||
{
|
||||
String name = make_string("__type_info_data");
|
||||
Token token = {Token_Identifier};
|
||||
token.string = name;
|
||||
|
||||
|
||||
String name = make_string("__type_info_data");
|
||||
Token token = {};
|
||||
token.kind = Token_Identifier;
|
||||
token.string = name;
|
||||
isize count = gb_array_count(c->info.type_info_map.entries);
|
||||
Entity *e = make_entity_variable(m->allocator, NULL, token, make_type_array(m->allocator, t_type_info, count));
|
||||
ssaValue *g = ssa_make_value_global(m->allocator, e, NULL);
|
||||
g->Global.is_private = true;
|
||||
map_set(&m->values, hash_pointer(e), g);
|
||||
map_set(&m->members, hash_string(name), g);
|
||||
}
|
||||
|
||||
// Type info member buffer
|
||||
{
|
||||
// NOTE(bill): Removes need for heap allocation by making it global memory
|
||||
isize count = 0;
|
||||
|
||||
isize count = gb_array_count(c->info.type_info_map.entries);
|
||||
Entity *e = make_entity_variable(m->allocator, NULL, token, make_type_array(m->allocator, t_type_info, count));
|
||||
ssaValue *g = ssa_make_value_global(m->allocator, e, NULL);
|
||||
g->Global.is_private = true;
|
||||
map_set(&m->values, hash_pointer(e), g);
|
||||
map_set(&m->members, hash_string(name), g);
|
||||
gb_for_array(entry_index, m->info->type_info_map.entries) {
|
||||
auto *entry = &m->info->type_info_map.entries[entry_index];
|
||||
Type *t = cast(Type *)cast(uintptr)entry->key.key;
|
||||
|
||||
switch (t->kind) {
|
||||
case Type_Record:
|
||||
switch (t->Record.kind) {
|
||||
case TypeRecord_Struct:
|
||||
case TypeRecord_RawUnion:
|
||||
count += t->Record.field_count;
|
||||
}
|
||||
break;
|
||||
case Type_Tuple:
|
||||
count += t->Tuple.variable_count;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String name = make_string("__type_info_member_data");
|
||||
Token token = {Token_Identifier};
|
||||
token.string = name;
|
||||
|
||||
Entity *e = make_entity_variable(m->allocator, NULL, token,
|
||||
make_type_array(m->allocator, t_type_info_member, count));
|
||||
ssaValue *g = ssa_make_value_global(m->allocator, e, NULL);
|
||||
map_set(&m->values, hash_pointer(e), g);
|
||||
map_set(&m->members, hash_string(name), g);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2087,7 +2120,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
args[1] = src;
|
||||
args[2] = byte_count;
|
||||
|
||||
ssa_emit_global_call(proc, "memory_move", args, 3);
|
||||
ssa_emit_global_call(proc, "memory_copy", args, 3);
|
||||
|
||||
return len;
|
||||
} break;
|
||||
@@ -2137,7 +2170,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
args[1] = item;
|
||||
args[2] = byte_count;
|
||||
|
||||
ssa_emit_global_call(proc, "memory_move", args, 3);
|
||||
ssa_emit_global_call(proc, "memory_copy", args, 3);
|
||||
|
||||
// Increment slice length
|
||||
Token add = {Token_Add};
|
||||
|
||||
Reference in New Issue
Block a user