diff options
| author | Ginger Bill <bill@gingerbill.org> | 2017-02-12 11:31:04 +0000 |
|---|---|---|
| committer | Ginger Bill <bill@gingerbill.org> | 2017-02-12 11:31:04 +0000 |
| commit | b9ed546ce009e0701388fd4338326557316ab2a7 (patch) | |
| tree | db0301b454f5080c4f202ddce84d403913632cd9 | |
| parent | e1fdd675cebc6e6dad50a359d8908c779a6d69b5 (diff) | |
Record type field `names`
| -rw-r--r-- | build.bat | 2 | ||||
| -rw-r--r-- | core/_preload.odin | 9 | ||||
| -rw-r--r-- | core/fmt.odin | 65 | ||||
| -rw-r--r-- | src/check_expr.c | 31 | ||||
| -rw-r--r-- | src/checker.c | 11 | ||||
| -rw-r--r-- | src/ir.c | 300 | ||||
| -rw-r--r-- | src/ir_print.c | 5 | ||||
| -rw-r--r-- | src/types.c | 8 |
8 files changed, 228 insertions, 203 deletions
@@ -4,7 +4,7 @@ set exe_name=odin.exe :: Debug = 0, Release = 1 -set release_mode=1 +set release_mode=0 set compiler_flags= -nologo -Oi -TC -fp:fast -fp:except- -Gm- -MP -FC -GS- -EHsc- -GR- if %release_mode% EQU 0 ( rem Debug diff --git a/core/_preload.odin b/core/_preload.odin index a2c1ebe3d..7ff3bc44f 100644 --- a/core/_preload.odin +++ b/core/_preload.odin @@ -14,13 +14,10 @@ // IMPORTANT NOTE(bill): Do not change the order of any of this data // The compiler relies upon this _exact_ order -Type_Info_Member :: struct #ordered { - name: string, // can be empty if tuple - type_info: ^Type_Info, - offset: int, // offsets may not be used in tuples -} Type_Info_Record :: struct #ordered { - fields: []Type_Info_Member, + types: []^Type_Info, + names: []string, + offsets: []int, // offsets may not be used in tuples size: int, // in bytes align: int, // in bytes packed: bool, diff --git a/core/fmt.odin b/core/fmt.odin index d990fcada..542f951e9 100644 --- a/core/fmt.odin +++ b/core/fmt.odin @@ -140,11 +140,11 @@ buffer_write_type :: proc(buf: ^Buffer, ti: ^Type_Info) { if info.params == nil { buffer_write_string(buf, "()"); } else { - fields := (cast(^Tuple)info.params).fields; + t := cast(^Tuple)info.params; buffer_write_string(buf, "("); - for f, i in fields { + for type, i in t.types { if i > 0 { buffer_write_string(buf, ", "); } - buffer_write_type(buf, f.type_info); + buffer_write_type(buf, type); } buffer_write_string(buf, ")"); } @@ -153,18 +153,19 @@ buffer_write_type :: proc(buf: ^Buffer, ti: ^Type_Info) { buffer_write_type(buf, info.results); } case Tuple: - count := info.fields.count; + count := info.names.count; if count != 1 { buffer_write_string(buf, "("); } for i in 0..<count { if i > 0 { buffer_write_string(buf, ", "); } - f := info.fields[i]; + name := info.names[i]; + type := info.types[i]; - if f.name.count > 0 { - buffer_write_string(buf, f.name); + if name.count > 0 { + buffer_write_string(buf, name); buffer_write_string(buf, ": "); } - buffer_write_type(buf, f.type_info); + buffer_write_type(buf, type); } if count != 1 { buffer_write_string(buf, ")"); } @@ -205,30 +206,30 @@ buffer_write_type :: proc(buf: ^Buffer, ti: ^Type_Info) { buffer_write_byte(buf, ' '); } buffer_write_byte(buf, '{'); - for field, i in info.fields { - buffer_write_string(buf, field.name); + for name, i in info.names { + buffer_write_string(buf, name); buffer_write_string(buf, ": "); - buffer_write_type(buf, field.type_info); + buffer_write_type(buf, info.types[i]); buffer_write_byte(buf, ','); } buffer_write_byte(buf, '}'); case Union: buffer_write_string(buf, "union {"); - for field, i in info.fields { - buffer_write_string(buf, field.name); + for name, i in info.names { + buffer_write_string(buf, name); buffer_write_string(buf, ": "); - buffer_write_type(buf, field.type_info); + buffer_write_type(buf, info.types[i]); buffer_write_byte(buf, ','); } buffer_write_string(buf, "}"); case Raw_Union: buffer_write_string(buf, "raw_union {"); - for field, i in info.fields { - buffer_write_string(buf, field.name); + for name, i in info.names { + buffer_write_string(buf, name); buffer_write_string(buf, ": "); - buffer_write_type(buf, field.type_info); + buffer_write_type(buf, info.types[i]); buffer_write_byte(buf, ','); } buffer_write_string(buf, "}"); @@ -417,9 +418,10 @@ fmt_write_padding :: proc(fi: ^Fmt_Info, width: int) { } fmt_integer :: proc(fi: ^Fmt_Info, u: u64, base: int, signed: bool, digits: string) { - s := cast(i64)u; - negative := signed && s < 0; - u = cast(u64)abs(s); + negative := signed && cast(i64)u < 0; + if signed { + u = cast(u64)abs(cast(i64)u); + } buf: [256]byte; if fi.width_set || fi.prec_set { width := fi.width + fi.prec + 3; @@ -719,14 +721,14 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) { } buffer_write_string(fi.buf, info.name); buffer_write_byte(fi.buf, '{'); - for f, i in b.fields { + for _, i in b.names { if i > 0 { buffer_write_string(fi.buf, ", "); } - buffer_write_string(fi.buf, f.name); + buffer_write_string(fi.buf, b.names[i]); buffer_write_string(fi.buf, " = "); - data := cast(^byte)v.data + f.offset; - fmt_arg(fi, any{f.type_info, cast(rawptr)data}, 'v'); + data := cast(^byte)v.data + b.offsets[i]; + fmt_arg(fi, any{b.types[i], cast(rawptr)data}, 'v'); } buffer_write_byte(fi.buf, '}'); @@ -788,8 +790,8 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) { buffer_write_string(fi.buf, "map["); defer buffer_write_byte(fi.buf, ']'); entries := ^(cast(^Raw_Dynamic_Map)v.data).entries; - gs, gs_ok := union_cast(^Struct)type_info_base(info.generated_struct); assert(gs_ok); - ed, ed_ok := union_cast(^Dynamic_Array)type_info_base(gs.fields[1].type_info); assert(ed_ok); + gs, gs_ok := union_cast(^Struct)type_info_base(info.generated_struct); assert(gs_ok); + ed, ed_ok := union_cast(^Dynamic_Array)type_info_base(gs.types[1]); assert(ed_ok); entry_type, et_ok := union_cast(^Struct)ed.elem; assert(et_ok); entry_size := ed.elem_size; @@ -809,7 +811,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) { buffer_write_string(fi.buf, "="); - value := data + entry_type.fields[2].offset; + value := data + entry_type.offsets[2]; fmt_arg(fi, any{info.value, cast(rawptr)value}, 'v'); } @@ -847,15 +849,14 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) { buffer_write_byte(fi.buf, '{'); defer buffer_write_byte(fi.buf, '}'); - for f, i in info.fields { + for _, i in info.names { if i > 0 { buffer_write_string(fi.buf, ", "); } - buffer_write_string(fi.buf, f.name); + buffer_write_string(fi.buf, info.names[i]); buffer_write_string(fi.buf, " = "); - data := cast(^byte)v.data + f.offset; - ti := f.type_info; - fmt_value(fi, any{ti, cast(rawptr)data}, 'v'); + data := cast(^byte)v.data + info.offsets[i]; + fmt_value(fi, any{info.types[i], cast(rawptr)data}, 'v'); } case Union: diff --git a/src/check_expr.c b/src/check_expr.c index de494a9cd..8689b70f0 100644 --- a/src/check_expr.c +++ b/src/check_expr.c @@ -502,6 +502,14 @@ GB_COMPARE_PROC(cmp_struct_entity_size) { return xa > ya ? -1 : xa < ya; } +Entity *make_names_field_for_record(Checker *c, Scope *scope) { + Entity *e = make_entity_field(c->allocator, scope, + make_token_ident(str_lit("names")), t_string_slice, false, 0); + e->Variable.is_immutable = true; + e->flags |= EntityFlag_TypeField; + return e; +} + void check_struct_type(Checker *c, Type *struct_type, AstNode *node) { GB_ASSERT(is_type_struct(struct_type)); ast_node(st, StructType, node); @@ -525,11 +533,7 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node) { struct_type->Record.fields = fields; struct_type->Record.fields_in_src_order = fields; struct_type->Record.field_count = field_count; - - // struct_type->Record.names = make_entity_field(c->allocator, c->context.scope, - // make_token_ident(str_lit("names")), t_string_slice, false, 0); - // struct_type->Record.names->Variable.is_immutable = true; - // struct_type->Record.names->flags |= EntityFlag_TypeField; + struct_type->Record.names = make_names_field_for_record(c, c->context.scope); if (!st->is_packed && !st->is_ordered) { // NOTE(bill): Reorder fields for reduced size/performance @@ -615,11 +619,7 @@ void check_union_type(Checker *c, Type *union_type, AstNode *node) { union_type->Record.fields = fields; union_type->Record.field_count = field_count; - - // union_type->Record.names = make_entity_field(c->allocator, c->context.scope, - // make_token_ident(str_lit("names")), t_string_slice, false, 0); - // union_type->Record.names->Variable.is_immutable = true; - // union_type->Record.names->flags |= EntityFlag_TypeField; + union_type->Record.names = make_names_field_for_record(c, c->context.scope); } void check_raw_union_type(Checker *c, Type *union_type, AstNode *node) { @@ -643,11 +643,7 @@ void check_raw_union_type(Checker *c, Type *union_type, AstNode *node) { union_type->Record.fields = fields; union_type->Record.field_count = field_count; - -// union_type->Record.names = make_entity_field(c->allocator, c->context.scope, -// make_token_ident(str_lit("names")), t_string_slice, false, 0); -// union_type->Record.names->Variable.is_immutable = true; -// union_type->Record.names->flags |= EntityFlag_TypeField; + union_type->Record.names = make_names_field_for_record(c, c->context.scope); } // GB_COMPARE_PROC(cmp_enum_order) { @@ -802,10 +798,7 @@ void check_enum_type(Checker *c, Type *enum_type, Type *named_type, AstNode *nod enum_type->Record.enum_max_value = make_entity_constant(c->allocator, c->context.scope, make_token_ident(str_lit("max_value")), constant_type, max_value); - enum_type->Record.names = make_entity_field(c->allocator, c->context.scope, - make_token_ident(str_lit("names")), t_string_slice, false, 0); - enum_type->Record.names->Variable.is_immutable = true; - enum_type->Record.names->flags |= EntityFlag_TypeField; + enum_type->Record.names = make_names_field_for_record(c, c->context.scope); } diff --git a/src/checker.c b/src/checker.c index 657df6f57..8c194fc7f 100644 --- a/src/checker.c +++ b/src/checker.c @@ -1079,19 +1079,16 @@ void init_preload(Checker *c) { } if (t_type_info == NULL) { - Entity *type_info_entity = find_core_entity(c, str_lit("Type_Info")); - Entity *type_info_member_entity = find_core_entity(c, str_lit("Type_Info_Member")); - Entity *type_info_enum_value_entity = find_core_entity(c, str_lit("Type_Info_Enum_Value")); + Entity *type_info_entity = find_core_entity(c, str_lit("Type_Info")); t_type_info = type_info_entity->type; t_type_info_ptr = make_type_pointer(c->allocator, t_type_info); GB_ASSERT(is_type_union(type_info_entity->type)); TypeRecord *record = &base_type(type_info_entity->type)->Record; - t_type_info_member = type_info_member_entity->type; - t_type_info_member_ptr = make_type_pointer(c->allocator, t_type_info_member); - - t_type_info_enum_value = type_info_enum_value_entity->type; + t_type_info_record = find_core_entity(c, str_lit("Type_Info_Record"))->type; + t_type_info_record_ptr = make_type_pointer(c->allocator, t_type_info_record); + t_type_info_enum_value = find_core_entity(c, str_lit("Type_Info_Enum_Value"))->type; t_type_info_enum_value_ptr = make_type_pointer(c->allocator, t_type_info_enum_value); @@ -128,9 +128,11 @@ struct irProcedure { i32 block_count; }; -#define IR_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime" -#define IR_TYPE_INFO_DATA_NAME "__$type_info_data" -#define IR_TYPE_INFO_DATA_MEMBER_NAME "__$type_info_data_member" +#define IR_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime" +#define IR_TYPE_INFO_DATA_NAME "__$type_info_data" +#define IR_TYPE_INFO_TYPES_NAME "__$type_info_types_data" +#define IR_TYPE_INFO_NAMES_NAME "__$type_info_names_data" +#define IR_TYPE_INFO_OFFSETS_NAME "__$type_info_offsets_data" #define IR_INSTR_KINDS \ @@ -1293,6 +1295,14 @@ irValue *ir_emit(irProcedure *proc, irValue *instr) { return instr; } irValue *ir_emit_store(irProcedure *p, irValue *address, irValue *value) { +#if 1 + // NOTE(bill): Sanity check + Type *a = base_type(base_enum_type(type_deref(ir_type(address)))); + Type *b = base_type(base_enum_type(ir_type(value))); + if (!is_type_untyped(b)) { + GB_ASSERT_MSG(are_types_identical(a, b), "%s %s", type_to_string(a), type_to_string(b)); + } +#endif return ir_emit(p, ir_make_instr_store(p, address, value)); } irValue *ir_emit_load(irProcedure *p, irValue *address) { @@ -1762,8 +1772,12 @@ irValue *ir_emit_union_tag_ptr(irProcedure *proc, irValue *u) { Type *t = ir_type(u); GB_ASSERT(is_type_pointer(t) && is_type_union(type_deref(t))); - GB_ASSERT(are_types_identical(t, ir_type(u))); - return ir_emit(proc, ir_make_instr_union_tag_ptr(proc, u)); + irValue *tag_ptr = ir_emit(proc, ir_make_instr_union_tag_ptr(proc, u)); + Type *tpt = ir_type(tag_ptr); + GB_ASSERT(is_type_pointer(tpt)); + tpt = base_type(type_deref(tpt)); + GB_ASSERT(tpt == t_int); + return tag_ptr; } irValue *ir_emit_union_tag_value(irProcedure *proc, irValue *u) { @@ -3746,20 +3760,15 @@ irAddr ir_build_addr(irProcedure *proc, AstNode *expr) { String name = e->token.string; if (str_eq(name, str_lit("names"))) { irValue *ti_ptr = ir_type_info(proc, type); - // { - // irValue **args = gb_alloc_array(proc->module->allocator, irValue *, 1); - // args[0] = ti_ptr; - // ti_ptr = ir_emit_global_call(proc, "type_info_base", args, 1); - // } + irValue *names_ptr = NULL; if (is_type_enum(type)) { irValue *enum_info = ir_emit_conv(proc, ti_ptr, t_type_info_enum_ptr); names_ptr = ir_emit_struct_ep(proc, enum_info, 1); - } else { - GB_PANIC("TODO(bill): `names` for records"); - // irValue *record_info = ir_emit_conv(proc, ti_ptr, t_type_info_record_ptr); - // names_ptr = ir_emit_struct_ep(proc, record_info, 1); + } else if (type->kind == Type_Record) { + irValue *record_info = ir_emit_conv(proc, ti_ptr, t_type_info_record_ptr); + names_ptr = ir_emit_struct_ep(proc, record_info, 1); } return ir_make_addr(names_ptr); } else { @@ -5721,12 +5730,30 @@ void ir_init_module(irModule *m, Checker *c, BuildContext *build_context) { } } - String name = str_lit(IR_TYPE_INFO_DATA_MEMBER_NAME); - Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name), - make_type_array(m->allocator, t_type_info_member, count), false); - irValue *g = ir_make_value_global(m->allocator, e, NULL); - ir_module_add_value(m, e, g); - map_ir_value_set(&m->members, hash_string(name), g); + { + String name = str_lit(IR_TYPE_INFO_TYPES_NAME); + Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name), + make_type_array(m->allocator, t_type_info_ptr, count), false); + irValue *g = ir_make_value_global(m->allocator, e, NULL); + ir_module_add_value(m, e, g); + map_ir_value_set(&m->members, hash_string(name), g); + } + { + String name = str_lit(IR_TYPE_INFO_NAMES_NAME); + Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name), + make_type_array(m->allocator, t_string, count), false); + irValue *g = ir_make_value_global(m->allocator, e, NULL); + ir_module_add_value(m, e, g); + map_ir_value_set(&m->members, hash_string(name), g); + } + { + String name = str_lit(IR_TYPE_INFO_OFFSETS_NAME); + Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name), + make_type_array(m->allocator, t_int, count), false); + irValue *g = ir_make_value_global(m->allocator, e, NULL); + ir_module_add_value(m, e, g); + map_ir_value_set(&m->members, hash_string(name), g); + } } } @@ -5865,6 +5892,17 @@ void ir_add_foreign_library_path(irModule *m, Entity *e) { array_add(&m->foreign_library_paths, library_path); } +void ir_fill_slice(irProcedure *proc, irValue *slice_ptr, irValue *data, irValue *count) { + Type *t = ir_type(slice_ptr); + GB_ASSERT(is_type_pointer(t)); + t = type_deref(t); + GB_ASSERT(is_type_slice(t)); + irValue *elem = ir_emit_struct_ep(proc, slice_ptr, 0); + irValue *len = ir_emit_struct_ep(proc, slice_ptr, 1); + ir_emit_store(proc, elem, data); + ir_emit_store(proc, len, count); +} + void ir_gen_tree(irGen *s) { irModule *m = &s->module; CheckerInfo *info = m->info; @@ -6193,17 +6231,16 @@ void ir_gen_tree(irGen *s) { { // NOTE(bill): Setup type_info data // TODO(bill): Try and make a lot of this constant aggregate literals in LLVM IR - irValue *type_info_data = NULL; - irValue *type_info_member_data = NULL; + irValue *type_info_data = NULL; + irValue *type_info_member_types = NULL; + irValue *type_info_member_names = NULL; + irValue *type_info_member_offsets = NULL; irValue **found = NULL; - found = map_ir_value_get(&proc->module->members, hash_string(str_lit(IR_TYPE_INFO_DATA_NAME))); - GB_ASSERT(found != NULL); - type_info_data = *found; - - found = map_ir_value_get(&proc->module->members, hash_string(str_lit(IR_TYPE_INFO_DATA_MEMBER_NAME))); - GB_ASSERT(found != NULL); - type_info_member_data = *found; + type_info_data = *map_ir_value_get(&proc->module->members, hash_string(str_lit(IR_TYPE_INFO_DATA_NAME))); + type_info_member_types = *map_ir_value_get(&proc->module->members, hash_string(str_lit(IR_TYPE_INFO_TYPES_NAME))); + type_info_member_names = *map_ir_value_get(&proc->module->members, hash_string(str_lit(IR_TYPE_INFO_NAMES_NAME))); + type_info_member_offsets = *map_ir_value_get(&proc->module->members, hash_string(str_lit(IR_TYPE_INFO_OFFSETS_NAME))); CheckerInfo *info = proc->module->info; @@ -6223,7 +6260,9 @@ void ir_gen_tree(irGen *s) { Type *t_i64_slice_ptr = make_type_pointer(a, make_type_slice(a, t_i64)); Type *t_string_slice_ptr = make_type_pointer(a, make_type_slice(a, t_string)); - i32 type_info_member_index = 0; + i32 type_info_member_types_index = 0; + i32 type_info_member_names_index = 0; + i32 type_info_member_offsets_index = 0; for_array(type_info_map_index, info->type_info_map.entries) { MapIsizeEntry *entry = &info->type_info_map.entries.e[type_info_map_index]; @@ -6234,8 +6273,10 @@ void ir_gen_tree(irGen *s) { irValue *tag = NULL; irValue *ti_ptr = ir_emit_array_epi(proc, type_info_data, entry_index); + switch (t->kind) { case Type_Named: { + ir_emit_comment(proc, str_lit("Type_Info_Named")); tag = ir_emit_conv(proc, ti_ptr, t_type_info_named_ptr); // TODO(bill): Which is better? The mangled name or actual name? @@ -6247,6 +6288,7 @@ void ir_gen_tree(irGen *s) { } break; case Type_Basic: + ir_emit_comment(proc, str_lit("Type_Info_Basic")); switch (t->Basic.kind) { case Basic_bool: tag = ir_emit_conv(proc, ti_ptr, t_type_info_boolean_ptr); @@ -6296,11 +6338,13 @@ void ir_gen_tree(irGen *s) { break; case Type_Pointer: { + ir_emit_comment(proc, str_lit("Type_Info_Pointer")); tag = ir_emit_conv(proc, ti_ptr, t_type_info_pointer_ptr); irValue *gep = ir_get_type_info_ptr(proc, type_info_data, t->Pointer.elem); ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), gep); } break; case Type_Array: { + ir_emit_comment(proc, str_lit("Type_Info_Array")); tag = ir_emit_conv(proc, ti_ptr, t_type_info_array_ptr); irValue *gep = ir_get_type_info_ptr(proc, type_info_data, t->Array.elem); ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), gep); @@ -6314,6 +6358,7 @@ void ir_gen_tree(irGen *s) { } break; case Type_DynamicArray: { + ir_emit_comment(proc, str_lit("Type_Info_DynamicArray")); tag = ir_emit_conv(proc, ti_ptr, t_type_info_dynamic_array_ptr); irValue *gep = ir_get_type_info_ptr(proc, type_info_data, t->DynamicArray.elem); ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), gep); @@ -6323,6 +6368,7 @@ void ir_gen_tree(irGen *s) { ir_emit_store(proc, elem_size, ir_make_const_int(a, ez)); } break; case Type_Slice: { + ir_emit_comment(proc, str_lit("Type_Info_Slice")); tag = ir_emit_conv(proc, ti_ptr, t_type_info_slice_ptr); irValue *gep = ir_get_type_info_ptr(proc, type_info_data, t->Slice.elem); ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), gep); @@ -6332,6 +6378,7 @@ void ir_gen_tree(irGen *s) { ir_emit_store(proc, elem_size, ir_make_const_int(a, ez)); } break; case Type_Vector: { + ir_emit_comment(proc, str_lit("Type_Info_Vector")); tag = ir_emit_conv(proc, ti_ptr, t_type_info_vector_ptr); irValue *gep = ir_get_type_info_ptr(proc, type_info_data, t->Vector.elem); ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), gep); @@ -6342,9 +6389,59 @@ void ir_gen_tree(irGen *s) { ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 3), ir_make_const_int(a, type_align_of(m->sizes, a, t))); } break; + case Type_Proc: { + ir_emit_comment(proc, str_lit("Type_Info_Proc")); + tag = ir_emit_conv(proc, ti_ptr, t_type_info_procedure_ptr); + + irValue *params = ir_emit_struct_ep(proc, tag, 0); + irValue *results = ir_emit_struct_ep(proc, tag, 1); + irValue *variadic = ir_emit_struct_ep(proc, tag, 2); + irValue *convention = ir_emit_struct_ep(proc, tag, 3); + + if (t->Proc.params) { + ir_emit_store(proc, params, ir_get_type_info_ptr(proc, type_info_data, t->Proc.params)); + } + if (t->Proc.results) { + ir_emit_store(proc, results, ir_get_type_info_ptr(proc, type_info_data, t->Proc.results)); + } + ir_emit_store(proc, variadic, ir_make_const_bool(a, t->Proc.variadic)); + ir_emit_store(proc, convention, ir_make_const_int(a, t->Proc.calling_convention)); + + // TODO(bill): Type_Info for procedures + } break; + case Type_Tuple: { + ir_emit_comment(proc, str_lit("Type_Info_Tuple")); + tag = ir_emit_conv(proc, ti_ptr, t_type_info_tuple_ptr); + + { + irValue *align = ir_make_const_int(a, type_align_of(m->sizes, a, t)); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 4), align); + } + + irValue *memory_types = ir_type_info_member_offset(proc, type_info_member_types, t->Record.field_count, &type_info_member_types_index); + irValue *memory_names = ir_type_info_member_offset(proc, type_info_member_names, t->Record.field_count, &type_info_member_names_index); + + for (isize i = 0; i < t->Tuple.variable_count; i++) { + // NOTE(bill): offset is not used for tuples + Entity *f = t->Tuple.variables[i]; + + irValue *index = ir_make_const_int(a, i); + irValue *type_info = ir_emit_ptr_offset(proc, memory_types, index); + + ir_emit_store(proc, type_info, ir_type_info(proc, f->type)); + if (f->token.string.len > 0) { + irValue *name = ir_emit_ptr_offset(proc, memory_names, index); + ir_emit_store(proc, name, ir_make_const_string(a, f->token.string)); + } + } + + ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 0), memory_types, ir_make_const_int(a, t->Record.field_count)); + ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 1), memory_names, ir_make_const_int(a, t->Record.field_count)); + } break; case Type_Record: { switch (t->Record.kind) { case TypeRecord_Struct: { + ir_emit_comment(proc, str_lit("Type_Info_Struct")); tag = ir_emit_conv(proc, ti_ptr, t_type_info_struct_ptr); { @@ -6353,14 +6450,16 @@ void ir_gen_tree(irGen *s) { irValue *packed = ir_make_const_bool(a, t->Record.struct_is_packed); irValue *ordered = ir_make_const_bool(a, t->Record.struct_is_ordered); irValue *custom_align = ir_make_const_bool(a, t->Record.custom_align); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 1), size); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), align); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 3), packed); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 4), ordered); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 5), custom_align); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 3), size); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 4), align); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 5), packed); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 6), ordered); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 7), custom_align); } - irValue *memory = ir_type_info_member_offset(proc, type_info_member_data, t->Record.field_count, &type_info_member_index); + irValue *memory_types = ir_type_info_member_offset(proc, type_info_member_types, t->Record.field_count, &type_info_member_types_index); + irValue *memory_names = ir_type_info_member_offset(proc, type_info_member_names, t->Record.field_count, &type_info_member_names_index); + irValue *memory_offsets = ir_type_info_member_offset(proc, type_info_member_offsets, t->Record.field_count, &type_info_member_offsets_index); type_set_offsets(m->sizes, a, t); // NOTE(bill): Just incase the offsets have not been set yet for (isize source_index = 0; source_index < t->Record.field_count; source_index++) { @@ -6370,77 +6469,65 @@ void ir_gen_tree(irGen *s) { i64 foffset = t->Record.struct_offsets[f->Variable.field_index]; GB_ASSERT(f->kind == Entity_Variable && f->flags & EntityFlag_Field); - irValue *field = ir_emit_ptr_offset(proc, memory, ir_make_const_int(a, source_index)); - irValue *name = ir_emit_struct_ep(proc, field, 0); - irValue *type_info = ir_emit_struct_ep(proc, field, 1); - irValue *offset = ir_emit_struct_ep(proc, field, 2); + irValue *index = ir_make_const_int(a, source_index); + irValue *type_info = ir_emit_ptr_offset(proc, memory_types, index); + irValue *offset = ir_emit_ptr_offset(proc, memory_offsets, index); + ir_emit_store(proc, type_info, ir_type_info(proc, f->type)); if (f->token.string.len > 0) { + irValue *name = ir_emit_ptr_offset(proc, memory_names, index); ir_emit_store(proc, name, ir_make_const_string(a, f->token.string)); } - ir_emit_store(proc, type_info, tip); ir_emit_store(proc, offset, ir_make_const_int(a, foffset)); } - Type *slice_type = make_type_slice(a, t_type_info_member); - Type *slice_type_ptr = make_type_pointer(a, slice_type); - irValue *slice = ir_emit_struct_ep(proc, tag, 0); - irValue *field_count = ir_make_const_int(a, t->Record.field_count); - - irValue *elem = ir_emit_struct_ep(proc, slice, 0); - irValue *len = ir_emit_struct_ep(proc, slice, 1); - - ir_emit_store(proc, elem, memory); - ir_emit_store(proc, len, field_count); + ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 0), memory_types, ir_make_const_int(a, t->Record.field_count)); + ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 1), memory_names, ir_make_const_int(a, t->Record.field_count)); + ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 2), memory_offsets, ir_make_const_int(a, t->Record.field_count)); } break; case TypeRecord_Union: + ir_emit_comment(proc, str_lit("Type_Info_Union")); tag = ir_emit_conv(proc, ti_ptr, t_type_info_union_ptr); { irValue *size = ir_make_const_int(a, type_size_of(m->sizes, a, t)); irValue *align = ir_make_const_int(a, type_align_of(m->sizes, a, t)); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 1), size); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), align); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 3), size); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 4), align); } break; case TypeRecord_RawUnion: { + ir_emit_comment(proc, str_lit("Type_Info_RawUnion")); tag = ir_emit_conv(proc, ti_ptr, t_type_info_raw_union_ptr); { irValue *size = ir_make_const_int(a, type_size_of(m->sizes, a, t)); irValue *align = ir_make_const_int(a, type_align_of(m->sizes, a, t)); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 1), size); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), align); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 3), size); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 4), align); } - irValue *memory = ir_type_info_member_offset(proc, type_info_member_data, t->Record.field_count, &type_info_member_index); + irValue *memory_types = ir_type_info_member_offset(proc, type_info_member_types, t->Record.field_count, &type_info_member_types_index); + irValue *memory_names = ir_type_info_member_offset(proc, type_info_member_names, t->Record.field_count, &type_info_member_names_index); + irValue *memory_offsets = ir_type_info_member_offset(proc, type_info_member_offsets, t->Record.field_count, &type_info_member_offsets_index); for (isize i = 0; i < t->Record.field_count; i++) { - irValue *field = ir_emit_ptr_offset(proc, memory, ir_make_const_int(a, i)); - irValue *name = ir_emit_struct_ep(proc, field, 0); - irValue *type_info = ir_emit_struct_ep(proc, field, 1); - irValue *offset = ir_emit_struct_ep(proc, field, 2); - Entity *f = t->Record.fields[i]; - irValue *tip = ir_get_type_info_ptr(proc, type_info_data, f->type); + irValue *index = ir_make_const_int(a, i); + irValue *type_info = ir_emit_ptr_offset(proc, memory_types, index); + // NOTE(bill): Offsets are always 0 + ir_emit_store(proc, type_info, ir_type_info(proc, f->type)); if (f->token.string.len > 0) { + irValue *name = ir_emit_ptr_offset(proc, memory_names, index); ir_emit_store(proc, name, ir_make_const_string(a, f->token.string)); } - ir_emit_store(proc, type_info, tip); - ir_emit_store(proc, offset, ir_make_const_int(a, 0)); } - Type *slice_type = make_type_slice(a, t_type_info_member); - Type *slice_type_ptr = make_type_pointer(a, slice_type); - irValue *slice = ir_emit_struct_ep(proc, tag, 0); - irValue *field_count = ir_make_const_int(a, t->Record.field_count); - - irValue *elem = ir_emit_struct_ep(proc, slice, 0); - irValue *len = ir_emit_struct_ep(proc, slice, 1); - - ir_emit_store(proc, elem, memory); - ir_emit_store(proc, len, field_count); + ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 0), memory_types, ir_make_const_int(a, t->Record.field_count)); + ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 1), memory_names, ir_make_const_int(a, t->Record.field_count)); + ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 2), memory_offsets, ir_make_const_int(a, t->Record.field_count)); } break; case TypeRecord_Enum: + ir_emit_comment(proc, str_lit("Type_Info_Enum")); tag = ir_emit_conv(proc, ti_ptr, t_type_info_enum_ptr); { GB_ASSERT(t->Record.enum_base_type != NULL); @@ -6495,65 +6582,8 @@ void ir_gen_tree(irGen *s) { break; } } break; - - case Type_Tuple: { - tag = ir_emit_conv(proc, ti_ptr, t_type_info_tuple_ptr); - - { - irValue *align = ir_make_const_int(a, type_align_of(m->sizes, a, t)); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), align); - } - - irValue *memory = ir_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++) { - irValue *field = ir_emit_ptr_offset(proc, memory, ir_make_const_int(a, i)); - irValue *name = ir_emit_struct_ep(proc, field, 0); - irValue *type_info = ir_emit_struct_ep(proc, field, 1); - // NOTE(bill): offset is not used for tuples - - Entity *f = t->Tuple.variables[i]; - irValue *tip = ir_get_type_info_ptr(proc, type_info_data, f->type); - - if (f->token.string.len > 0) { - ir_emit_store(proc, name, ir_make_const_string(a, f->token.string)); - } - ir_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); - irValue *slice = ir_emit_struct_ep(proc, tag, 0); - irValue *variable_count = ir_make_const_int(a, t->Tuple.variable_count); - - irValue *elem = ir_emit_struct_ep(proc, slice, 0); - irValue *len = ir_emit_struct_ep(proc, slice, 1); - - ir_emit_store(proc, elem, memory); - ir_emit_store(proc, len, variable_count); - } break; - - case Type_Proc: { - tag = ir_emit_conv(proc, ti_ptr, t_type_info_procedure_ptr); - - irValue *params = ir_emit_struct_ep(proc, tag, 0); - irValue *results = ir_emit_struct_ep(proc, tag, 1); - irValue *variadic = ir_emit_struct_ep(proc, tag, 2); - irValue *convention = ir_emit_struct_ep(proc, tag, 3); - - if (t->Proc.params) { - ir_emit_store(proc, params, ir_get_type_info_ptr(proc, type_info_data, t->Proc.params)); - } - if (t->Proc.results) { - ir_emit_store(proc, results, ir_get_type_info_ptr(proc, type_info_data, t->Proc.results)); - } - ir_emit_store(proc, variadic, ir_make_const_bool(a, t->Proc.variadic)); - ir_emit_store(proc, convention, ir_make_const_int(a, t->Proc.calling_convention)); - - // TODO(bill): Type_Info for procedures - } break; - case Type_Map: { + ir_emit_comment(proc, str_lit("Type_Info_Map")); tag = ir_emit_conv(proc, ti_ptr, t_type_info_map_ptr); irValue *key = ir_emit_struct_ep(proc, tag, 0); @@ -6568,6 +6598,7 @@ void ir_gen_tree(irGen *s) { } break; } + if (tag != NULL) { Type *tag_type = type_deref(ir_type(tag)); Type *ti = base_type(t_type_info); @@ -6576,8 +6607,9 @@ void ir_gen_tree(irGen *s) { Entity *f = ti->Record.fields[i]; if (are_types_identical(f->type, tag_type)) { found = true; - irValue *tag = ir_make_const_int(proc->module->allocator, i); - ir_emit_store(proc, ir_emit_union_tag_ptr(proc, ti_ptr), tag); + irValue *tag = ir_make_const_int(a, i); + irValue *ptr = ir_emit_union_tag_ptr(proc, ti_ptr); + ir_emit_store(proc, ptr, tag); break; } } diff --git a/src/ir_print.c b/src/ir_print.c index da411fdc6..2d92cef17 100644 --- a/src/ir_print.c +++ b/src/ir_print.c @@ -842,6 +842,7 @@ void ir_print_instr(irFileBuffer *f, irModule *m, irValue *value) { ir_fprintf(f, " 0, "); ir_print_type(f, m, t_i32); ir_fprintf(f, " %d", 2); + ir_fprintf(f, " ; UnionTagPtr"); ir_fprintf(f, "\n"); } break; @@ -852,7 +853,9 @@ void ir_print_instr(irFileBuffer *f, irModule *m, irValue *value) { ir_print_type(f, m, et); ir_fprintf(f, " "); ir_print_value(f, m, instr->UnionTagValue.address, et); - ir_fprintf(f, ", %d\n", 2); + ir_fprintf(f, ", %d", 2); + ir_fprintf(f, " ; UnionTagValue"); + ir_fprintf(f, "\n"); } break; case irInstr_Jump: {; diff --git a/src/types.c b/src/types.c index af8d4dd37..2f9138fb3 100644 --- a/src/types.c +++ b/src/types.c @@ -291,10 +291,10 @@ gb_global Type *t_string_slice = NULL; gb_global Type *t_type_info = NULL; -gb_global Type *t_type_info_member = NULL; +gb_global Type *t_type_info_record = NULL; gb_global Type *t_type_info_enum_value = NULL; gb_global Type *t_type_info_ptr = NULL; -gb_global Type *t_type_info_member_ptr = NULL; +gb_global Type *t_type_info_record_ptr = NULL; gb_global Type *t_type_info_enum_value_ptr = NULL; gb_global Type *t_type_info_named = NULL; @@ -867,7 +867,9 @@ bool are_types_identical(Type *x, Type *y) { case TypeRecord_Union: if (x->Record.field_count == y->Record.field_count && x->Record.struct_is_packed == y->Record.struct_is_packed && - x->Record.struct_is_ordered == y->Record.struct_is_ordered) { + x->Record.struct_is_ordered == y->Record.struct_is_ordered && + x->Record.custom_align == y->Record.custom_align) { + // TODO(bill); Fix the custom alignment rule for (isize i = 0; i < x->Record.field_count; i++) { if (!are_types_identical(x->Record.fields[i]->type, y->Record.fields[i]->type)) { return false; |