Remove duplicates in type info data.

This commit is contained in:
Ginger Bill
2016-09-09 19:48:28 +01:00
parent 56dd12c54c
commit 1ca752ce04
9 changed files with 388 additions and 140 deletions

View File

@@ -3,11 +3,21 @@
#load "file.odin"
print_string_to_buffer :: proc(buf: ^[]byte, s: string) {
for i := 0; i < len(s); i++ {
if !append(buf, s[i]) {
// Buffer is full
return
}
// NOTE(bill): This is quite a hack
// TODO(bill): Should I allow the raw editing of a slice by exposing its
// internal members?
Raw_Bytes :: struct {
data: ^byte
len: int
cap: int
}
slice := buf as ^Raw_Bytes
if slice.len < slice.cap {
n := min(slice.cap-slice.len, len(s))
offset := ((slice.data as int) + slice.len) as ^byte
memory_move(offset, ^s[0], n)
slice.len += n
}
}
@@ -79,7 +89,7 @@ print_int_base_to_buffer :: proc(buffer: ^[]byte, i, base: int) {
}
for i > 0 {
buf[len] = PRINT__NUM_TO_CHAR_TABLE[i % base]
len++;
len++
i /= base
}
@@ -171,12 +181,32 @@ print__f64 :: proc(buffer: ^[]byte, f: f64, decimal_places: int) {
print_any_to_buffer :: proc(buf: ^[]byte, arg: any) {
using Type_Info
match type arg.type_info -> info {
match type info : arg.type_info {
case Named:
a: any
a.type_info = info.base
a.data = arg.data
print_any_to_buffer(buf, a)
match type b : info.base {
case Struct:
print_string_to_buffer(buf, info.name)
print_string_to_buffer(buf, "{")
for i := 0; i < len(b.fields); i++ {
f := b.fields[i];
if i > 0 {
print_string_to_buffer(buf, ", ")
}
print_any_to_buffer(buf, f.name)
print_string_to_buffer(buf, " = ")
v: any
v.type_info = f.type_info
v.data = ptr_offset(arg.data as ^u8, f.offset)
print_any_to_buffer(buf, v)
}
print_string_to_buffer(buf, "}")
default:
print_any_to_buffer(buf, a)
}
case Integer:
if info.signed {
@@ -243,11 +273,59 @@ print_any_to_buffer :: proc(buf: ^[]byte, arg: any) {
print_any_to_buffer(buf, v)
case Array: print_string_to_buffer(buf, "(array)")
case Slice: print_string_to_buffer(buf, "(slice)")
case Vector: print_string_to_buffer(buf, "(vector)")
case Array:
print_string_to_buffer(buf, "[")
for i := 0; i < info.len; i++ {
if i > 0 {
print_string_to_buffer(buf, ", ")
}
case Struct: print_string_to_buffer(buf, "(struct)")
elem: any
elem.data = (arg.data as int + i*info.elem_size) as rawptr
elem.type_info = info.elem
print_any_to_buffer(buf, elem)
}
print_string_to_buffer(buf, "]")
case Slice:
slice := arg.data as ^struct { data: rawptr; len, cap: int }
print_string_to_buffer(buf, "[")
for i := 0; i < slice.len; i++ {
if i > 0 {
print_string_to_buffer(buf, ", ")
}
elem: any
elem.data = (slice.data as int + i*info.elem_size) as rawptr
elem.type_info = info.elem
print_any_to_buffer(buf, elem)
}
print_string_to_buffer(buf, "]")
case Vector:
print_string_to_buffer(buf, "<")
for i := 0; i < info.len; i++ {
if i > 0 {
print_string_to_buffer(buf, ", ")
}
elem: any
elem.data = (arg.data as int + i*info.elem_size) as rawptr
elem.type_info = info.elem
print_any_to_buffer(buf, elem)
}
print_string_to_buffer(buf, ">")
case Struct:
print_string_to_buffer(buf, "(struct ")
for i := 0; i < len(info.fields); i++ {
if i > 0 {
print_string_to_buffer(buf, ", ")
}
print_any_to_buffer(buf, info.fields[i].name)
}
print_string_to_buffer(buf, ")")
case Union: print_string_to_buffer(buf, "(union)")
case Raw_Union: print_string_to_buffer(buf, "(raw_union)")
case Procedure:
@@ -259,12 +337,34 @@ print_any_to_buffer :: proc(buf: ^[]byte, arg: any) {
}
}
type_info_is_string :: proc(info: ^Type_Info) -> bool {
using Type_Info
if info == null {
return false
}
for {
match type i : info {
case Named:
info = i.base
continue
case String:
return true
default:
return false
}
}
return false
}
print_to_buffer :: proc(buf: ^[]byte, args: ..any) {
prev_string := false
for i := 0; i < len(args); i++ {
arg := args[i]
if i > 0 {
print_space_to_buffer(buf)
}
print_any_to_buffer(buf, args[i])
print_any_to_buffer(buf, arg)
}
}

View File

@@ -1,5 +1,10 @@
#load "basic.odin"
Vector3 :: struct { x, y, z: f32 }
main :: proc() {
println(137, "Hello", 1.25, true)
v := Vector3{1, 4, 9}
println(123, "Hello", true, 6.28)
println([4]int{1, 2, 3, 4})
}

View File

@@ -4,9 +4,9 @@
// The compiler relies upon this _exact_ order
Type_Info :: union {
Member :: struct {
name: string
type_: ^Type_Info
offset: int
name: string
type_info: ^Type_Info
offset: int
}
Record :: struct {
fields: []Member // NOTE(bill): This will need to be allocated on the heap
@@ -32,15 +32,19 @@ Type_Info :: union {
Procedure: struct{}
Array: struct {
elem: ^Type_Info
count: int
elem_size: int
len: int
}
Slice: struct {
elem: ^Type_Info
elem_size: int
}
Vector: struct {
elem: ^Type_Info
count: int
elem_size: int
len: int
}
Tuple: Record
Struct: Record
Union: Record
Raw_Union: Record

View File

@@ -208,7 +208,8 @@ struct CheckerInfo {
Map<ExpressionInfo> untyped; // Key: AstNode * | Expression -> ExpressionInfo
Map<DeclInfo *> entities; // Key: Entity *
Map<Entity *> foreign_procs; // Key: String
Map<Type *> type_info_types; // Key: Type *
Map<isize> type_info_map; // Key: Type *
isize type_info_index;
};
struct Checker {
@@ -441,7 +442,8 @@ void init_checker_info(CheckerInfo *i) {
map_init(&i->entities, a);
map_init(&i->untyped, a);
map_init(&i->foreign_procs, a);
map_init(&i->type_info_types, a);
map_init(&i->type_info_map, a);
i->type_info_index = 0;
}
@@ -453,7 +455,7 @@ void destroy_checker_info(CheckerInfo *i) {
map_destroy(&i->entities);
map_destroy(&i->untyped);
map_destroy(&i->foreign_procs);
map_destroy(&i->type_info_types);
map_destroy(&i->type_info_map);
}
@@ -670,6 +672,82 @@ void add_file_entity(Checker *c, AstNode *identifier, Entity *e, DeclInfo *d) {
map_set(&c->info.entities, hash_pointer(e), d);
}
void add_type_info_type(Checker *c, Type *t) {
if (t == NULL) {
return;
}
t = default_type(t);
if (map_get(&c->info.type_info_map, hash_pointer(t)) != NULL) {
// Types have already been added
return;
}
isize found = -1;
gb_for_array(i, c->info.type_info_map.entries) {
auto *e = &c->info.type_info_map.entries[i];
Type *prev_type = cast(Type *)cast(uintptr)e->key.key;
if (are_types_identical(t, prev_type)) {
found = i;
break;
}
}
if (found >= 0) {
// Duplicate entry
map_set(&c->info.type_info_map, hash_pointer(t), found);
} else {
// Unique entry
isize index = c->info.type_info_index;
c->info.type_info_index++;
map_set(&c->info.type_info_map, hash_pointer(t), index);
}
if (t->kind == Type_Named) {
// NOTE(bill): Just in case
add_type_info_type(c, t->Named.base);
return;
}
Type *bt = get_base_type(t);
switch (bt->kind) {
case Type_Basic: {
if (bt->Basic.kind == Basic_string) {
add_type_info_type(c, make_type_pointer(c->allocator, t_u8));
add_type_info_type(c, t_int);
}
} break;
case Type_Array:
add_type_info_type(c, bt->Array.elem);
add_type_info_type(c, make_type_pointer(c->allocator, bt->Array.elem));
add_type_info_type(c, t_int);
break;
case Type_Slice:
add_type_info_type(c, bt->Slice.elem);
add_type_info_type(c, make_type_pointer(c->allocator, bt->Slice.elem));
add_type_info_type(c, t_int);
break;
case Type_Vector: add_type_info_type(c, bt->Vector.elem); break;
case Type_Pointer: add_type_info_type(c, bt->Pointer.elem); break;
case Type_Record: {
switch (bt->Record.kind) {
case TypeRecord_Enum:
add_type_info_type(c, bt->Record.enum_base);
break;
default:
for (isize i = 0; i < bt->Record.field_count; i++) {
Entity *f = bt->Record.fields[i];
add_type_info_type(c, f->type);
}
break;
}
} break;
}
// TODO(bill): Type info for procedures and tuples
// TODO(bill): Remove duplicate identical types efficiently
}
void check_procedure_later(Checker *c, AstFile *file, Token token, DeclInfo *decl, Type *type, AstNode *body) {
ProcedureInfo info = {};
@@ -866,7 +944,11 @@ void check_parsed_files(Checker *c) {
t_type_info_ptr = make_type_pointer(c->allocator, t_type_info);
auto *record = &get_base_type(e->type)->Record;
GB_ASSERT_MSG(record->field_count == 15, "Internal Compiler Error: Invalid `Type_Info` layout");
t_type_info_member = record->other_fields[0]->type;
t_type_info_member_ptr = make_type_pointer(c->allocator, t_type_info_member);
GB_ASSERT_MSG(record->field_count == 16, "Internal Compiler Error: Invalid `Type_Info` layout");
t_type_info_named = record->fields[ 1]->type;
t_type_info_integer = record->fields[ 2]->type;
t_type_info_float = record->fields[ 3]->type;
@@ -877,11 +959,11 @@ void check_parsed_files(Checker *c) {
t_type_info_array = record->fields[ 8]->type;
t_type_info_slice = record->fields[ 9]->type;
t_type_info_vector = record->fields[10]->type;
t_type_info_struct = record->fields[11]->type;
t_type_info_union = record->fields[12]->type;
t_type_info_raw_union = record->fields[13]->type;
t_type_info_enum = record->fields[14]->type;
// t_type_info_any = record->fields[15]->type;
t_type_info_tuple = record->fields[11]->type;
t_type_info_struct = record->fields[12]->type;
t_type_info_union = record->fields[13]->type;
t_type_info_raw_union = record->fields[14]->type;
t_type_info_enum = record->fields[15]->type;
}
check_global_entity(c, Entity_Constant);

View File

@@ -46,63 +46,6 @@ b32 check_is_assignable_to_using_subtype(Type *dst, Type *src) {
}
void add_type_info_type(Checker *c, Type *t) {
if (t == NULL) {
return;
}
t = default_type(t);
if (map_get(&c->info.type_info_types, hash_pointer(t)) != NULL) {
// Types have already been added
return;
}
map_set(&c->info.type_info_types, hash_pointer(t), t);
if (t->kind == Type_Named) {
// NOTE(bill): Just in case
add_type_info_type(c, t->Named.base);
return;
}
Type *bt = get_base_type(t);
switch (bt->kind) {
case Type_Basic: {
if (bt->Basic.kind == Basic_string) {
add_type_info_type(c, make_type_pointer(c->allocator, t_u8));
add_type_info_type(c, t_int);
}
} break;
case Type_Array:
add_type_info_type(c, bt->Array.elem);
add_type_info_type(c, make_type_pointer(c->allocator, bt->Array.elem));
add_type_info_type(c, t_int);
break;
case Type_Slice:
add_type_info_type(c, bt->Slice.elem);
add_type_info_type(c, make_type_pointer(c->allocator, bt->Slice.elem));
add_type_info_type(c, t_int);
break;
case Type_Vector: add_type_info_type(c, bt->Vector.elem); break;
case Type_Pointer: add_type_info_type(c, bt->Pointer.elem); break;
case Type_Record: {
switch (bt->Record.kind) {
case TypeRecord_Enum:
add_type_info_type(c, bt->Record.enum_base);
break;
default:
for (isize i = 0; i < bt->Record.field_count; i++) {
Entity *f = bt->Record.fields[i];
add_type_info_type(c, f->type);
}
break;
}
} break;
}
// TODO(bill): Type info for procedures and tuples
// TODO(bill): Remove duplicate identical types efficiently
}
b32 check_is_assignable_to(Checker *c, Operand *operand, Type *type, b32 is_argument = false) {
if (operand->mode == Addressing_Invalid ||
type == t_invalid) {

View File

@@ -352,24 +352,27 @@ gb_global Type *t_untyped_rune = &basic_types[Basic_UntypedRune];
gb_global Type *t_byte = &basic_type_aliases[Basic_byte];
gb_global Type *t_rune = &basic_type_aliases[Basic_rune];
gb_global Type *t_type_info = NULL;
gb_global Type *t_type_info_ptr = NULL;
gb_global Type *t_type_info = NULL;
gb_global Type *t_type_info_ptr = NULL;
gb_global Type *t_type_info_member = NULL;
gb_global Type *t_type_info_member_ptr = NULL;
gb_global Type *t_type_info_named = NULL;
gb_global Type *t_type_info_integer = NULL;
gb_global Type *t_type_info_float = NULL;
gb_global Type *t_type_info_string = NULL;
gb_global Type *t_type_info_boolean = NULL;
gb_global Type *t_type_info_pointer = NULL;
gb_global Type *t_type_info_procedure = NULL;
gb_global Type *t_type_info_array = NULL;
gb_global Type *t_type_info_slice = NULL;
gb_global Type *t_type_info_vector = NULL;
gb_global Type *t_type_info_struct = NULL;
gb_global Type *t_type_info_union = NULL;
gb_global Type *t_type_info_raw_union = NULL;
gb_global Type *t_type_info_enum = NULL;
gb_global Type *t_type_info_any = NULL;
gb_global Type *t_type_info_named = NULL;
gb_global Type *t_type_info_integer = NULL;
gb_global Type *t_type_info_float = NULL;
gb_global Type *t_type_info_string = NULL;
gb_global Type *t_type_info_boolean = NULL;
gb_global Type *t_type_info_pointer = NULL;
gb_global Type *t_type_info_procedure = NULL;
gb_global Type *t_type_info_array = NULL;
gb_global Type *t_type_info_slice = NULL;
gb_global Type *t_type_info_vector = NULL;
gb_global Type *t_type_info_tuple = NULL;
gb_global Type *t_type_info_struct = NULL;
gb_global Type *t_type_info_union = NULL;
gb_global Type *t_type_info_raw_union = NULL;
gb_global Type *t_type_info_enum = NULL;
gb_global Type *t_type_info_any = NULL;

View File

@@ -190,17 +190,16 @@ void ssa_gen_tree(ssaGen *s) {
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 * {
auto *info = proc->module->info;
MapFindResult fr = map__find(&info->type_info_types, hash_pointer(type));
GB_ASSERT(fr.entry_index >= 0);
return ssa_emit_struct_gep(proc, type_info_data, fr.entry_index, t_type_info_ptr);
return ssa_emit_struct_gep(proc, type_info_data,
ssa_type_info_index(proc->module->info, type),
t_type_info_ptr);
};
gb_for_array(entry_index, info->type_info_types.entries) {
auto *entry = &info->type_info_types.entries[entry_index];
Type *t = entry->value;
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;
ssaValue *tag = NULL;
@@ -270,29 +269,120 @@ void ssa_gen_tree(ssaGen *s) {
tag = ssa_add_local_generated(proc, t_type_info_array);
ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Array.elem);
ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, v_zero32, t_type_info_ptr_ptr), gep);
isize ez = type_size_of(m->sizes, a, t->Array.elem);
ssaValue *elem_size = ssa_emit_struct_gep(proc, tag, v_one32, t_int_ptr);
ssa_emit_store(proc, elem_size, ssa_make_value_constant(a, t_int, make_exact_value_integer(ez)));
ssaValue *count = ssa_emit_struct_gep(proc, tag, v_two32, t_int_ptr);
ssa_emit_store(proc, count, ssa_make_value_constant(a, t_int, make_exact_value_integer(t->Array.count)));
} break;
case Type_Slice: {
tag = ssa_add_local_generated(proc, t_type_info_slice);
ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Slice.elem);
ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, v_zero32, t_type_info_ptr_ptr), gep);
isize ez = type_size_of(m->sizes, a, t->Slice.elem);
ssaValue *elem_size = ssa_emit_struct_gep(proc, tag, v_one32, t_int_ptr);
ssa_emit_store(proc, elem_size, ssa_make_value_constant(a, t_int, make_exact_value_integer(ez)));
} break;
case Type_Vector: {
tag = ssa_add_local_generated(proc, t_type_info_vector);
ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Vector.elem);
ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, v_zero32, t_type_info_ptr_ptr), gep);
isize ez = type_size_of(m->sizes, a, t->Vector.elem);
ssaValue *elem_size = ssa_emit_struct_gep(proc, tag, v_one32, t_int_ptr);
ssa_emit_store(proc, elem_size, ssa_make_value_constant(a, t_int, make_exact_value_integer(ez)));
ssaValue *count = ssa_emit_struct_gep(proc, tag, v_two32, t_int_ptr);
ssa_emit_store(proc, count, ssa_make_value_constant(a, t_int, make_exact_value_integer(t->Vector.count)));
} break;
case Type_Record: {
switch (t->Record.kind) {
// TODO(bill): Record members for `Type_Info`
case TypeRecord_Struct:
case TypeRecord_Struct: {
tag = ssa_add_local_generated(proc, t_type_info_struct);
break;
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);
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);
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];
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)));
}
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 *field_count = ssa_make_value_constant(a, t_int, make_exact_value_integer(t->Record.field_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, field_count);
ssa_emit_store(proc, cap, field_count);
} break;
case TypeRecord_Union:
tag = ssa_add_local_generated(proc, t_type_info_union);
break;
case TypeRecord_RawUnion:
case TypeRecord_RawUnion: {
tag = ssa_add_local_generated(proc, t_type_info_raw_union);
break;
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);
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);
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)));
ssa_emit_store(proc, type_info, tip);
ssa_emit_store(proc, offset, ssa_make_value_constant(a, t_int, make_exact_value_integer(0)));
}
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 *field_count = ssa_make_value_constant(a, t_int, make_exact_value_integer(t->Record.field_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, field_count);
ssa_emit_store(proc, cap, field_count);
} break;
case TypeRecord_Enum: {
tag = ssa_add_local_generated(proc, t_type_info_enum);
Type *enum_base = t->Record.enum_base;

View File

@@ -319,7 +319,7 @@ void ssa_module_init(ssaModule *m, Checker *c) {
token.string = name;
isize count = gb_array_count(c->info.type_info_types.entries);
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;
@@ -881,6 +881,29 @@ 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;
}
@@ -1540,20 +1563,8 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t, b32 is_arg
data = ssa_emit_conv(proc, data, t_rawptr);
MapFindResult fr = map__find(&info->type_info_types, hash_pointer(src_type));
isize entry_index = fr.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_types.entries){
auto *e = &info->type_info_types.entries[i];
Type *t = e->value;
if (are_types_identical(t, src_type)) {
entry_index = i;
break;
}
}
}
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 *gep0 = ssa_emit_struct_gep(proc, result, v_zero32, make_type_pointer(proc->module->allocator, t_type_info_ptr));
@@ -2251,12 +2262,9 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
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]));
MapFindResult fr = map__find(&proc->module->info->type_info_types, hash_pointer(t));
GB_ASSERT(fr.entry_index >= 0);
// Zero is null and void
isize offset = fr.entry_index;
isize entry_index = ssa_type_info_index(proc->module->info, t);
ssaValue *gep = ssa_emit_struct_gep(proc, type_info_data, offset, t_type_info_ptr);
ssaValue *gep = ssa_emit_struct_gep(proc, type_info_data, entry_index, t_type_info_ptr);
return gep;
} break;
}
@@ -2369,6 +2377,14 @@ 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 *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);
}
ssaValue *ssa_build_expr(ssaProcedure *proc, AstNode *expr) {
expr = unparen_expr(expr);
@@ -2381,10 +2397,7 @@ ssaValue *ssa_build_expr(ssaProcedure *proc, AstNode *expr) {
// TODO(bill): Optimize by not allocating everytime
if (tv->value.value_string.len > 0) {
ssaValue *global_array = ssa_add_global_string_array(proc, tv->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);
return ssa_emit_global_string(proc, tv->value);
} else {
ssaValue *null_string = ssa_add_local_generated(proc, t_string);
return ssa_emit_load(proc, null_string);

View File

@@ -2261,10 +2261,17 @@ AstNode *parse_match_stmt(AstFile *f) {
AstNode *body = NULL;
Token open, close;
if (allow_token(f, Token_type)) {
tag = parse_expr(f, true);
expect_token(f, Token_ArrowRight);
isize prev_level = f->expr_level;
f->expr_level = -1;
AstNode *var = parse_identifier(f);
expect_token(f, Token_Colon);
tag = parse_simple_stmt(f);
f->expr_level = prev_level;
open = expect_token(f, Token_OpenBrace);
AstNodeArray list = make_ast_node_array(f);
@@ -2277,6 +2284,7 @@ AstNode *parse_match_stmt(AstFile *f) {
close = expect_token(f, Token_CloseBrace);
body = make_block_stmt(f, list, open, close);
tag = convert_stmt_to_expr(f, tag, make_string("type match expression"));
return make_type_match_stmt(f, token, tag, var, body);
} else {
if (f->cursor[0].kind != Token_OpenBrace) {