From 83ebb24015f8f1e1f03e7a7ea2737187bf2c176c Mon Sep 17 00:00:00 2001 From: Ginger Bill Date: Fri, 7 Apr 2017 14:05:28 +0100 Subject: [PATCH] Move to `Raw_*` types to raw.odin; Add size and align members to Type_Info --- core/_preload.odin | 61 +++++++++++------------------------ core/fmt.odin | 24 +++++++------- src/checker.c | 6 ---- src/ir.c | 80 ++++++++++++++++++++-------------------------- src/ir_print.c | 2 +- src/types.c | 2 -- 6 files changed, 66 insertions(+), 109 deletions(-) diff --git a/core/_preload.odin b/core/_preload.odin index c938dcefa..4bac34ed8 100644 --- a/core/_preload.odin +++ b/core/_preload.odin @@ -3,6 +3,7 @@ #import "os.odin"; #import "fmt.odin"; #import "utf8.odin"; +#import "raw.odin"; // IMPORTANT NOTE(bill): `type_info` & `type_info_val` cannot be used within a // #shared_global_scope due to the internals of the compiler. @@ -36,11 +37,14 @@ Type_Info_Record :: struct #ordered { } Type_Info :: union { + size: int, + align: int, + Named{name: string, base: ^Type_Info}, - Integer{size: int, signed: bool}, - Float{size: int}, - Complex{size: int}, - Quaternion{size: int}, + Integer{signed: bool}, + Float{}, + Complex{}, + Quaternion{}, String{}, Boolean{}, Any{}, @@ -60,7 +64,7 @@ Type_Info :: union { }, Dynamic_Array{elem: ^Type_Info, elem_size: int}, Slice {elem: ^Type_Info, elem_size: int}, - Vector {elem: ^Type_Info, elem_size, count, align: int}, + Vector {elem: ^Type_Info, elem_size, count: int}, Tuple {using record: Type_Info_Record}, // Only really used for procedures Struct {using record: Type_Info_Record}, Raw_Union {using record: Type_Info_Record}, @@ -72,8 +76,6 @@ Type_Info :: union { }, variant_names: []string, variant_types: []^Type_Info, - size: int, - align: int, }, Enum{ base: ^Type_Info, @@ -419,37 +421,10 @@ __abs_quaternion256 :: proc(x: quaternion256) -> f64 #inline { -Raw_Any :: struct #ordered { - type_info: ^Type_Info, - data: rawptr, -} - -Raw_String :: struct #ordered { - data: ^byte, - len: int, -}; - -Raw_Slice :: struct #ordered { - data: rawptr, - len: int, - cap: int, -}; - -Raw_Dynamic_Array :: struct #ordered { - data: rawptr, - len: int, - cap: int, - allocator: Allocator, -}; - -Raw_Dynamic_Map :: struct #ordered { - hashes: [dynamic]int, - entries: Raw_Dynamic_Array, -}; __dynamic_array_make :: proc(array_: rawptr, elem_size, elem_align: int, len, cap: int) { - array := cast(^Raw_Dynamic_Array)array_; + array := cast(^raw.Dynamic_Array)array_; __check_context(); array.allocator = context.allocator; assert(array.allocator.procedure != nil); @@ -461,7 +436,7 @@ __dynamic_array_make :: proc(array_: rawptr, elem_size, elem_align: int, len, ca } __dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, cap: int) -> bool { - array := cast(^Raw_Dynamic_Array)array_; + array := cast(^raw.Dynamic_Array)array_; if cap <= array.cap { return true; @@ -490,7 +465,7 @@ __dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, cap: __dynamic_array_append :: proc(array_: rawptr, elem_size, elem_align: int, items: rawptr, item_count: int) -> int { - array := cast(^Raw_Dynamic_Array)array_; + array := cast(^raw.Dynamic_Array)array_; if item_count <= 0 || items == nil { return array.len; @@ -514,7 +489,7 @@ __dynamic_array_append :: proc(array_: rawptr, elem_size, elem_align: int, } __dynamic_array_append_nothing :: proc(array_: rawptr, elem_size, elem_align: int) -> int { - array := cast(^Raw_Dynamic_Array)array_; + array := cast(^raw.Dynamic_Array)array_; ok := true; if array.cap <= array.len+1 { @@ -534,7 +509,7 @@ __dynamic_array_append_nothing :: proc(array_: rawptr, elem_size, elem_align: in __slice_append :: proc(slice_: rawptr, elem_size, elem_align: int, items: rawptr, item_count: int) -> int { - slice := cast(^Raw_Slice)slice_; + slice := cast(^raw.Slice)slice_; if item_count <= 0 || items == nil { return slice.len; @@ -587,7 +562,7 @@ __Map_Entry_Header :: struct #ordered { } __Map_Header :: struct #ordered { - m: ^Raw_Dynamic_Map, + m: ^raw.Dynamic_Map, is_key_string: bool, entry_size: int, entry_align: int, @@ -602,11 +577,11 @@ __dynamic_map_reserve :: proc(using header: __Map_Header, cap: int) -> bool { __dynamic_map_rehash :: proc(using header: __Map_Header, new_count: int) { new_header := header; - nm: Raw_Dynamic_Map; + nm: raw.Dynamic_Map; new_header.m = ^nm; - header_hashes := cast(^Raw_Dynamic_Array)^header.m.hashes; - nm_hashes := cast(^Raw_Dynamic_Array)^nm.hashes; + header_hashes := cast(^raw.Dynamic_Array)^header.m.hashes; + nm_hashes := cast(^raw.Dynamic_Array)^nm.hashes; reserve(nm.hashes, new_count); diff --git a/core/fmt.odin b/core/fmt.odin index a39e5db2e..f1b6413d2 100644 --- a/core/fmt.odin +++ b/core/fmt.odin @@ -3,6 +3,7 @@ #import "utf8.odin"; #import "types.odin"; #import "strconv.odin"; +#import "raw.odin"; _BUFFER_SIZE :: 1<<12; @@ -106,19 +107,19 @@ write_type :: proc(buf: ^[]byte, ti: ^Type_Info) { } case Float: - match info.size { - case 4: write_string(buf, "f32"); - case 8: write_string(buf, "f64"); + match 8*info.size { + case 32: write_string(buf, "f32"); + case 64: write_string(buf, "f64"); } case Complex: - match info.size { - case 8: write_string(buf, "complex64"); - case 16: write_string(buf, "complex128"); + match 8*info.size { + case 64: write_string(buf, "complex64"); + case 128: write_string(buf, "complex128"); } case Quaternion: - match info.size { - case 16: write_string(buf, "quaternion128"); - case 32: write_string(buf, "quaternion"); + match 8*info.size { + case 128: write_string(buf, "quaternion128"); + case 256: write_string(buf, "quaternion256"); } case String: write_string(buf, "string"); case Boolean: write_string(buf, "bool"); @@ -388,6 +389,7 @@ int_from_arg :: proc(args: []any, arg_index: int) -> (int, int, bool) { arg.type_info = type_info_base(arg.type_info); match i in arg { case int: num = i; + case uint: num = cast(int)i; case i8: num = cast(int)i; case i16: num = cast(int)i; case i32: num = cast(int)i; @@ -779,7 +781,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) { write_byte(fi.buf, '['); defer write_byte(fi.buf, ']'); - array := cast(^Raw_Dynamic_Array)v.data; + array := cast(^raw.Dynamic_Array)v.data; for i in 0..array.len { if i > 0 { write_string(fi.buf, ", "); @@ -796,7 +798,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) { write_string(fi.buf, "map["); defer write_byte(fi.buf, ']'); - entries := ^(cast(^Raw_Dynamic_Map)v.data).entries; + entries := ^(cast(^raw.Dynamic_Map)v.data).entries; gs := union_cast(^Struct)type_info_base(info.generated_struct); ed := union_cast(^Dynamic_Array)type_info_base(gs.types[1]); diff --git a/src/checker.c b/src/checker.c index 30b1857b5..99d2b790f 100644 --- a/src/checker.c +++ b/src/checker.c @@ -1229,12 +1229,6 @@ void init_preload(Checker *c) { t_context_ptr = make_type_pointer(c->allocator, t_context); } - if (t_raw_dynamic_array == NULL) { - Entity *e = find_core_entity(c, str_lit("Raw_Dynamic_Array")); - t_raw_dynamic_array = e->type; - t_raw_dynamic_array = make_type_pointer(c->allocator, t_raw_dynamic_array); - } - if (t_map_key == NULL) { Entity *e = find_core_entity(c, str_lit("__Map_Key")); t_map_key = e->type; diff --git a/src/ir.c b/src/ir.c index 2ba5e254d..bd43fb33c 100644 --- a/src/ir.c +++ b/src/ir.c @@ -7077,6 +7077,9 @@ void ir_gen_tree(irGen *s) { irValue *tag = NULL; irValue *ti_ptr = ir_emit_array_epi(proc, ir_global_type_info_data, entry_index); + ir_emit_store(proc, ir_emit_struct_ep(proc, ti_ptr, 0), ir_const_int(a, type_size_of(a, t))); + ir_emit_store(proc, ir_emit_struct_ep(proc, ti_ptr, 1), ir_const_int(a, type_align_of(a, t))); + switch (t->kind) { case Type_Named: { @@ -7087,8 +7090,8 @@ void ir_gen_tree(irGen *s) { irValue *name = ir_const_string(a, t->Named.type_name->token.string); irValue *gtip = ir_get_type_info_ptr(proc, t->Named.base); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), name); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 1), gtip); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), name); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 3), gtip); } break; case Type_Basic: @@ -7109,31 +7112,23 @@ void ir_gen_tree(irGen *s) { case Basic_uint: { tag = ir_emit_conv(proc, ti_ptr, t_type_info_integer_ptr); bool is_unsigned = (t->Basic.flags & BasicFlag_Unsigned) != 0; - irValue *bits = ir_const_int(a, type_size_of(a, t)); irValue *is_signed = ir_const_bool(a, !is_unsigned); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), bits); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 1), is_signed); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), is_signed); } break; case Basic_f32: case Basic_f64: { tag = ir_emit_conv(proc, ti_ptr, t_type_info_float_ptr); - irValue *bits = ir_const_int(a, type_size_of(a, t)); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), bits); } break; case Basic_complex64: case Basic_complex128: { tag = ir_emit_conv(proc, ti_ptr, t_type_info_complex_ptr); - irValue *bits = ir_const_int(a, type_size_of(a, t)); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), bits); } break; case Basic_quaternion128: case Basic_quaternion256: { tag = ir_emit_conv(proc, ti_ptr, t_type_info_quaternion_ptr); - irValue *bits = ir_const_int(a, type_size_of(a, t)); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), bits); } break; case Basic_rawptr: @@ -7154,19 +7149,19 @@ void ir_gen_tree(irGen *s) { 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, t->Pointer.elem); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), gep); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), 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, t->Array.elem); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), gep); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), gep); isize ez = type_size_of(a, t->Array.elem); - irValue *elem_size = ir_emit_struct_ep(proc, tag, 1); + irValue *elem_size = ir_emit_struct_ep(proc, tag, 3); ir_emit_store(proc, elem_size, ir_const_int(a, ez)); - irValue *count = ir_emit_struct_ep(proc, tag, 2); + irValue *count = ir_emit_struct_ep(proc, tag, 4); ir_emit_store(proc, count, ir_const_int(a, t->Array.count)); } break; @@ -7174,42 +7169,41 @@ void ir_gen_tree(irGen *s) { 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, t->DynamicArray.elem); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), gep); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), gep); isize ez = type_size_of(a, t->DynamicArray.elem); - irValue *elem_size = ir_emit_struct_ep(proc, tag, 1); + irValue *elem_size = ir_emit_struct_ep(proc, tag, 3); ir_emit_store(proc, elem_size, ir_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, t->Slice.elem); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), gep); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), gep); isize ez = type_size_of(a, t->Slice.elem); - irValue *elem_size = ir_emit_struct_ep(proc, tag, 1); + irValue *elem_size = ir_emit_struct_ep(proc, tag, 3); ir_emit_store(proc, elem_size, ir_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, t->Vector.elem); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), gep); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), gep); isize ez = type_size_of(a, t->Vector.elem); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 1), ir_const_int(a, ez)); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), ir_const_int(a, t->Vector.count)); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 3), ir_const_int(a, type_align_of(a, t))); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 3), ir_const_int(a, ez)); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 4), ir_const_int(a, t->Vector.count)); } 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); + irValue *params = ir_emit_struct_ep(proc, tag, 2); + irValue *results = ir_emit_struct_ep(proc, tag, 3); + irValue *variadic = ir_emit_struct_ep(proc, tag, 4); + irValue *convention = ir_emit_struct_ep(proc, tag, 5); if (t->Proc.params != NULL) { ir_emit_store(proc, params, ir_get_type_info_ptr(proc, t->Proc.params)); @@ -7225,7 +7219,7 @@ void ir_gen_tree(irGen *s) { 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 *record = ir_emit_struct_ep(proc, tag, 0); + irValue *record = ir_emit_struct_ep(proc, tag, 2); { irValue *align = ir_const_int(a, type_align_of(a, t)); @@ -7258,7 +7252,7 @@ void ir_gen_tree(irGen *s) { case TypeRecord_Struct: { ir_emit_comment(proc, str_lit("Type_Info_Struct")); tag = ir_emit_conv(proc, ti_ptr, t_type_info_struct_ptr); - irValue *record = ir_emit_struct_ep(proc, tag, 0); + irValue *record = ir_emit_struct_ep(proc, tag, 2); { irValue *size = ir_const_int(a, type_size_of(a, t)); @@ -7305,15 +7299,9 @@ void ir_gen_tree(irGen *s) { 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_const_int(a, type_size_of(a, t)); - irValue *align = ir_const_int(a, type_align_of(a, t)); - 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 *common_fields = ir_emit_struct_ep(proc, tag, 0); + irValue *common_fields = ir_emit_struct_ep(proc, tag, 2); isize field_count = t->Record.field_count; irValue *memory_types = ir_type_info_member_types_offset(proc, field_count); @@ -7348,8 +7336,8 @@ void ir_gen_tree(irGen *s) { } { - irValue *variant_names = ir_emit_struct_ep(proc, tag, 1); - irValue *variant_types = ir_emit_struct_ep(proc, tag, 2); + irValue *variant_names = ir_emit_struct_ep(proc, tag, 3); + irValue *variant_types = ir_emit_struct_ep(proc, tag, 4); isize variant_count = gb_max(0, t->Record.variant_count-1); irValue *memory_names = ir_type_info_member_names_offset(proc, variant_count); @@ -7379,7 +7367,7 @@ void ir_gen_tree(irGen *s) { 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 *record = ir_emit_struct_ep(proc, tag, 0); + irValue *record = ir_emit_struct_ep(proc, tag, 2); { irValue *size = ir_const_int(a, type_size_of(a, t)); @@ -7416,7 +7404,7 @@ void ir_gen_tree(irGen *s) { { GB_ASSERT(t->Record.enum_base_type != NULL); irValue *base = ir_type_info(proc, t->Record.enum_base_type); - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), base); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), base); if (t->Record.field_count > 0) { Entity **fields = t->Record.fields; @@ -7450,13 +7438,13 @@ void ir_gen_tree(irGen *s) { irValue *v_count = ir_const_int(a, count); - irValue *names = ir_emit_struct_ep(proc, tag, 1); + irValue *names = ir_emit_struct_ep(proc, tag, 3); irValue *name_array_elem = ir_array_elem(proc, name_array); ir_emit_store(proc, ir_emit_struct_ep(proc, names, 0), name_array_elem); ir_emit_store(proc, ir_emit_struct_ep(proc, names, 1), v_count); - irValue *values = ir_emit_struct_ep(proc, tag, 2); + irValue *values = ir_emit_struct_ep(proc, tag, 4); irValue *value_array_elem = ir_array_elem(proc, value_array); ir_emit_store(proc, ir_emit_struct_ep(proc, values, 0), value_array_elem); @@ -7470,10 +7458,10 @@ void ir_gen_tree(irGen *s) { 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); - irValue *value = ir_emit_struct_ep(proc, tag, 1); - irValue *generated_struct = ir_emit_struct_ep(proc, tag, 2); - irValue *count = ir_emit_struct_ep(proc, tag, 3); + irValue *key = ir_emit_struct_ep(proc, tag, 2); + irValue *value = ir_emit_struct_ep(proc, tag, 3); + irValue *generated_struct = ir_emit_struct_ep(proc, tag, 4); + irValue *count = ir_emit_struct_ep(proc, tag, 5); ir_emit_store(proc, key, ir_get_type_info_ptr(proc, t->Map.key)); ir_emit_store(proc, value, ir_get_type_info_ptr(proc, t->Map.value)); diff --git a/src/ir_print.c b/src/ir_print.c index a17c7d109..9663f7042 100644 --- a/src/ir_print.c +++ b/src/ir_print.c @@ -643,7 +643,7 @@ void ir_print_value(irFileBuffer *f, irModule *m, irValue *value, Type *type_hin ir_print_type(f, m, t_int); ir_fprintf(f, " 0, i32 0), "); ir_print_type(f, m, t_int); - ir_fprintf(f, " %lld}", cs->count); + ir_fprintf(f, " %lld, %lld}", cs->count, cs->count); } } break; diff --git a/src/types.c b/src/types.c index c99aeeb18..22bcbae83 100644 --- a/src/types.c +++ b/src/types.c @@ -349,8 +349,6 @@ gb_global Type *t_allocator_ptr = NULL; gb_global Type *t_context = NULL; gb_global Type *t_context_ptr = NULL; -gb_global Type *t_raw_dynamic_array = NULL; -gb_global Type *t_raw_dynamic_array_ptr = NULL; gb_global Type *t_map_key = NULL; gb_global Type *t_map_header = NULL;