From 6979678ff947cecc8e6e0d0e8ceea7e0304d3f4e Mon Sep 17 00:00:00 2001 From: Ginger Bill Date: Fri, 9 Sep 2016 23:33:54 +0100 Subject: [PATCH] Begin reording of struct members by default. --- examples/basic.odin | 2 +- examples/demo.odin | 9 ++- examples/old_demos/demo002.odin | 4 +- examples/old_runtime.odin | 2 +- examples/runtime.odin | 50 +++++++------- examples/win32.odin | 6 +- src/checker/checker.cpp | 54 +++++++++++---- src/checker/expr.cpp | 76 ++++++++++++++++----- src/checker/type.cpp | 1 + src/codegen/codegen.cpp | 115 +++++++++++++++++++++++--------- src/codegen/ssa.cpp | 59 ++++++++++++---- src/parser.cpp | 15 +++-- src/string.cpp | 40 +++++++++++ 13 files changed, 322 insertions(+), 111 deletions(-) diff --git a/examples/basic.odin b/examples/basic.odin index ad4b224c2..4d41f937d 100644 --- a/examples/basic.odin +++ b/examples/basic.odin @@ -16,7 +16,7 @@ print_string_to_buffer :: proc(buf: ^[]byte, s: string) { 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) + memory_copy(offset, ^s[0], n) slice.len += n } } diff --git a/examples/demo.odin b/examples/demo.odin index 6d14320b9..b418693fa 100644 --- a/examples/demo.odin +++ b/examples/demo.odin @@ -1,10 +1,17 @@ #load "basic.odin" -Vector3 :: struct { x, y, z: f32 } main :: proc() { + Vector3 :: struct { + x: i8 + y: i32 + z: i16 + } v := Vector3{1, 4, 9} + t := type_info(v) + println(123, "Hello", true, 6.28) println([4]int{1, 2, 3, 4}) + println(v) } diff --git a/examples/old_demos/demo002.odin b/examples/old_demos/demo002.odin index 899f9fc32..b9923f37a 100644 --- a/examples/old_demos/demo002.odin +++ b/examples/old_demos/demo002.odin @@ -180,8 +180,8 @@ new_builtins :: proc() { { // Compile time assert COND :: true - assert(COND) - // assert(!COND) + compile_assert(COND) + // compile_assert(!COND) // Runtime assert x := true diff --git a/examples/old_runtime.odin b/examples/old_runtime.odin index 44ae3d64a..af788f11d 100644 --- a/examples/old_runtime.odin +++ b/examples/old_runtime.odin @@ -46,7 +46,7 @@ memory_copy :: proc(dst, src: rawptr, n: int) #inline { } v128b :: type {4}u32 - assert(align_of(v128b) == 16) + compile_assert(align_of(v128b) == 16) d, s: ^byte = dst, src diff --git a/examples/runtime.odin b/examples/runtime.odin index 6a074eb91..dd787e0aa 100644 --- a/examples/runtime.odin +++ b/examples/runtime.odin @@ -3,43 +3,47 @@ // IMPORTANT NOTE(bill): Do not change the order of any of this data // The compiler relies upon this _exact_ order Type_Info :: union { - Member :: struct { - name: string + Member :: struct #ordered { + name: string // can be empty if tuple type_info: ^Type_Info - offset: int + offset: int // offsets are not used in tuples } - Record :: struct { - fields: []Member // NOTE(bill): This will need to be allocated on the heap + Record :: struct #ordered { + fields: []Member // IMPORTANT: This will need to be allocated on the heap } - Named: struct { + Named: struct #ordered { name: string base: ^Type_Info } - Integer: struct { + Integer: struct #ordered { size: int // in bytes signed: bool } - Float: struct { + Float: struct #ordered { size: int // in bytes } - String: struct {} - Boolean: struct {} - Pointer: struct { + String: struct #ordered {} + Boolean: struct #ordered {} + Pointer: struct #ordered { elem: ^Type_Info } - Procedure: struct{} - Array: struct { + Procedure: struct #ordered { + params: ^Type_Info // Type_Info.Tuple + results: ^Type_Info // Type_Info.Tuple + variadic: bool + } + Array: struct #ordered { elem: ^Type_Info elem_size: int len: int } - Slice: struct { + Slice: struct #ordered { elem: ^Type_Info elem_size: int } - Vector: struct { + Vector: struct #ordered { elem: ^Type_Info elem_size: int len: int @@ -48,7 +52,7 @@ Type_Info :: union { Struct: Record Union: Record Raw_Union: Record - Enum: struct { + Enum: struct #ordered { base: ^Type_Info } } @@ -57,9 +61,9 @@ Type_Info :: union { assume :: proc(cond: bool) #foreign "llvm.assume" -__debug_trap :: proc() #foreign "llvm.debugtrap" -__trap :: proc() #foreign "llvm.trap" -read_cycle_counter :: proc() -> u64 #foreign "llvm.readcyclecounter" +__debug_trap :: proc() #foreign "llvm.debugtrap" +__trap :: proc() #foreign "llvm.trap" +read_cycle_counter :: proc() -> u64 #foreign "llvm.readcyclecounter" bit_reverse16 :: proc(b: u16) -> u16 #foreign "llvm.bitreverse.i16" bit_reverse32 :: proc(b: u32) -> u32 #foreign "llvm.bitreverse.i32" @@ -87,7 +91,8 @@ memory_zero :: proc(data: rawptr, len: int) { memory_compare :: proc(dst, src: rawptr, len: int) -> int { // TODO(bill): make a faster `memory_compare` - a, b := slice_ptr(dst as ^byte, len), slice_ptr(src as ^byte, len) + a := slice_ptr(dst as ^byte, len) + b := slice_ptr(src as ^byte, len) for i := 0; i < len; i++ { if a[i] != b[i] { return (a[i] - b[i]) as int @@ -97,11 +102,6 @@ memory_compare :: proc(dst, src: rawptr, len: int) -> int { } memory_copy :: proc(dst, src: rawptr, len: int) #inline { - llvm_memcpy_64bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #foreign "llvm.memcpy.p0i8.p0i8.i64" - llvm_memcpy_64bit(dst, src, len, 1, false) -} - -memory_move :: proc(dst, src: rawptr, len: int) #inline { llvm_memmove_64bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #foreign "llvm.memmove.p0i8.p0i8.i64" llvm_memmove_64bit(dst, src, len, 1, false) } diff --git a/examples/win32.odin b/examples/win32.odin index 35c47ccba..f693a4def 100644 --- a/examples/win32.odin +++ b/examples/win32.odin @@ -42,7 +42,7 @@ INVALID_HANDLE_VALUE :: (-1 as int) as HANDLE WNDPROC :: type proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT -WNDCLASSEXA :: struct { +WNDCLASSEXA :: struct #ordered { size, style: u32 wnd_proc: WNDPROC cls_extra, wnd_extra: i32 @@ -54,7 +54,7 @@ WNDCLASSEXA :: struct { sm: HICON } -MSG :: struct { +MSG :: struct #ordered { hwnd: HWND message: u32 wparam: WPARAM @@ -191,7 +191,7 @@ PROC :: type proc() wglCreateContextAttribsARBType :: type proc(hdc: HDC, hshareContext: rawptr, attribList: ^i32) -> HGLRC -PIXELFORMATDESCRIPTOR :: struct { +PIXELFORMATDESCRIPTOR :: struct #ordered { size, version, flags: u32 diff --git a/src/checker/checker.cpp b/src/checker/checker.cpp index 218f3517b..735ed827c 100644 --- a/src/checker/checker.cpp +++ b/src/checker/checker.cpp @@ -133,6 +133,10 @@ enum BuiltinProcId { BuiltinProc_offset_of, BuiltinProc_offset_of_val, BuiltinProc_type_of_val, + + BuiltinProc_type_info, + + BuiltinProc_compile_assert, BuiltinProc_assert, BuiltinProc_len, @@ -150,7 +154,6 @@ enum BuiltinProcId { BuiltinProc_max, BuiltinProc_abs, - BuiltinProc_type_info, BuiltinProc_Count, }; @@ -174,6 +177,10 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_Count] = { {STR_LIT("offset_of"), 2, false, Expr_Expr}, {STR_LIT("offset_of_val"), 1, false, Expr_Expr}, {STR_LIT("type_of_val"), 1, false, Expr_Expr}, + + {STR_LIT("type_info"), 1, false, Expr_Expr}, + + {STR_LIT("compile_assert"), 1, false, Expr_Stmt}, {STR_LIT("assert"), 1, false, Expr_Stmt}, {STR_LIT("len"), 1, false, Expr_Expr}, @@ -191,7 +198,6 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_Count] = { {STR_LIT("max"), 2, false, Expr_Expr}, {STR_LIT("abs"), 1, false, Expr_Expr}, - {STR_LIT("type_info"), 1, false, Expr_Expr}, }; struct CheckerContext { @@ -682,26 +688,27 @@ void add_type_info_type(Checker *c, Type *t) { return; } - isize found = -1; + isize ti_index = -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; + // Duplicate entry + ti_index = i; break; } } - if (found >= 0) { - // Duplicate entry - map_set(&c->info.type_info_map, hash_pointer(t), found); - } else { + if (ti_index < 0) { // Unique entry - isize index = c->info.type_info_index; + // NOTE(bill): map entries grow linearly and in order + ti_index = c->info.type_info_index; c->info.type_info_index++; - map_set(&c->info.type_info_map, hash_pointer(t), index); } + map_set(&c->info.type_info_map, hash_pointer(t), ti_index); + // Add nested types + if (t->kind == Type_Named) { // NOTE(bill): Just in case add_type_info_type(c, t->Named.base); @@ -716,6 +723,11 @@ void add_type_info_type(Checker *c, Type *t) { add_type_info_type(c, t_int); } } break; + + case Type_Pointer: + add_type_info_type(c, bt->Pointer.elem); + 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)); @@ -726,8 +738,11 @@ void add_type_info_type(Checker *c, Type *t) { 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_Vector: + add_type_info_type(c, bt->Vector.elem); + add_type_info_type(c, t_int); + break; + case Type_Record: { switch (bt->Record.kind) { case TypeRecord_Enum: @@ -741,9 +756,20 @@ void add_type_info_type(Checker *c, Type *t) { break; } } break; + + case Type_Tuple: + for (isize i = 0; i < bt->Tuple.variable_count; i++) { + Entity *var = bt->Tuple.variables[i]; + add_type_info_type(c, var->type); + } + break; + + case Type_Proc: + add_type_info_type(c, bt->Proc.params); + add_type_info_type(c, bt->Proc.results); + break; } - // TODO(bill): Type info for procedures and tuples - // TODO(bill): Remove duplicate identical types efficiently + } diff --git a/src/checker/expr.cpp b/src/checker/expr.cpp index 8ec744016..3dfdb8e98 100644 --- a/src/checker/expr.cpp +++ b/src/checker/expr.cpp @@ -386,6 +386,22 @@ void check_fields(Checker *c, AstNode *node, AstNodeArray decls, } +// TODO(bill): Cleanup struct field reordering +gb_global BaseTypeSizes __checker_sizes = {}; +gb_global gbAllocator __checker_allocator = {}; + +GB_COMPARE_PROC(cmp_struct_entity_size) { + // NOTE(bill): Biggest to smallest + Entity *x = *(Entity **)a; + Entity *y = *(Entity **)b; + i64 xs = type_size_of(__checker_sizes, __checker_allocator, x->type); + i64 ys = type_size_of(__checker_sizes, __checker_allocator, y->type); + if (xs == ys) { + return string_cmp(&x->token.string, &y->token.string); + } + return xs > ys ? -1 : xs < ys; +} + void check_struct_type(Checker *c, Type *struct_type, AstNode *node, CycleChecker *cycle_checker) { GB_ASSERT(is_type_struct(struct_type)); ast_node(st, StructType, node); @@ -414,7 +430,20 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node, CycleChecke check_fields(c, node, st->decls, fields, field_count, other_fields, other_field_count, cycle_checker, make_string("struct")); + if (!st->is_packed && !st->is_ordered) { + // NOTE(bill): Reorder fields for reduced size/performance + // NOTE(bill): Hacky thing + __checker_sizes = c->sizes; + __checker_allocator = c->allocator; + + // TODO(bill): Figure out rules more + // sorting rules + // compound literal order must match source not layout + // gb_sort_array(fields, field_count, cmp_struct_entity_size); + } + struct_type->Record.struct_is_packed = st->is_packed; + struct_type->Record.struct_is_ordered = st->is_ordered; struct_type->Record.fields = fields; struct_type->Record.field_count = field_count; struct_type->Record.other_fields = other_fields; @@ -1959,7 +1988,8 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id) case BuiltinProc_size_of: case BuiltinProc_align_of: case BuiltinProc_offset_of: - // NOTE(bill): The first arg is a Type, this will be checked case by case + case BuiltinProc_type_info: + // NOTE(bill): The first arg may be a Type, this will be checked case by case break; default: check_multi_expr(c, operand, ce->args[0]); @@ -2178,6 +2208,34 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id) operand->mode = Addressing_Type; break; + + case BuiltinProc_type_info: { + // type_info :: proc(val_or_type) -> ^Type_Info + Operand op = {}; + check_expr_or_type(c, &op, ce->args[0]); + add_type_info_type(c, op.type); + operand->mode = Addressing_Value; + operand->type = t_type_info_ptr; + } break; + + case BuiltinProc_compile_assert: + // compile_assert :: proc(cond: bool) + + if (!is_type_boolean(operand->type) && operand->mode != Addressing_Constant) { + gbString str = expr_to_string(ce->args[0]); + defer (gb_string_free(str)); + error(&c->error_collector, ast_node_token(call), + "`%s` is not a constant boolean", str); + return false; + } + if (!operand->value.value_bool) { + gbString str = expr_to_string(ce->args[0]); + defer (gb_string_free(str)); + error(&c->error_collector, ast_node_token(call), + "Compile time assertion: `%s`", str); + } + break; + case BuiltinProc_assert: // assert :: proc(cond: bool) @@ -2188,14 +2246,7 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id) "`%s` is not a boolean", str); return false; } - if (operand->mode == Addressing_Constant && - !operand->value.value_bool) { - gbString str = expr_to_string(ce->args[0]); - defer (gb_string_free(str)); - error(&c->error_collector, ast_node_token(call), - "Compile time assertion: `%s`", str); - return true; - } + if (operand->mode != Addressing_Constant) { operand->mode = Addressing_NoValue; } @@ -2693,13 +2744,6 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id) operand->type = type; } break; - - - case BuiltinProc_type_info: { - add_type_info_type(c, operand->type); - operand->mode = Addressing_Value; - operand->type = t_type_info_ptr; - } break; } return true; diff --git a/src/checker/type.cpp b/src/checker/type.cpp index 9a1241a55..ea7bb9873 100644 --- a/src/checker/type.cpp +++ b/src/checker/type.cpp @@ -132,6 +132,7 @@ struct Type { i64 * struct_offsets; b32 struct_are_offsets_set; b32 struct_is_packed; + b32 struct_is_ordered; // Entity_Constant or Entity_TypeName Entity **other_fields; diff --git a/src/codegen/codegen.cpp b/src/codegen/codegen.cpp index c556e9631..758cf398e 100644 --- a/src/codegen/codegen.cpp +++ b/src/codegen/codegen.cpp @@ -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) { diff --git a/src/codegen/ssa.cpp b/src/codegen/ssa.cpp index 018a908e1..3568504e4 100644 --- a/src/codegen/ssa.cpp +++ b/src/codegen/ssa.cpp @@ -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}; diff --git a/src/parser.cpp b/src/parser.cpp index 17c15c890..bd98e8c1e 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -245,6 +245,7 @@ AST_NODE_KIND(_TypeBegin, "", struct{}) \ AstNodeArray decls; \ isize decl_count; \ b32 is_packed; \ + b32 is_ordered; \ }) \ AST_NODE_KIND(UnionType, "union type", struct { \ Token token; \ @@ -815,12 +816,13 @@ gb_inline AstNode *make_vector_type(AstFile *f, Token token, AstNode *count, Ast return result; } -gb_inline AstNode *make_struct_type(AstFile *f, Token token, AstNodeArray decls, isize decl_count, b32 is_packed) { +gb_inline AstNode *make_struct_type(AstFile *f, Token token, AstNodeArray decls, isize decl_count, b32 is_packed, b32 is_ordered) { AstNode *result = make_node(f, AstNode_StructType); result->StructType.token = token; result->StructType.decls = decls; result->StructType.decl_count = decl_count; result->StructType.is_packed = is_packed; + result->StructType.is_ordered = is_ordered; return result; } @@ -1826,12 +1828,15 @@ AstNode *parse_identifier_or_type(AstFile *f) { case Token_struct: { Token token = expect_token(f, Token_struct); b32 is_packed = false; - if (allow_token(f, Token_Hash)) { + b32 is_ordered = false; + while (allow_token(f, Token_Hash)) { Token tag = expect_token(f, Token_Identifier); if (are_strings_equal(tag.string, make_string("packed"))) { is_packed = true; - } else { - ast_file_err(f, tag, "Expected a `#packed` tag"); + } else if (are_strings_equal(tag.string, make_string("ordered"))) { + is_ordered = true; + } else { + ast_file_err(f, tag, "Expected a `#packed` or `#ordered` tag"); } } @@ -1840,7 +1845,7 @@ AstNode *parse_identifier_or_type(AstFile *f) { AstNodeArray decls = parse_struct_params(f, &decl_count, true); Token close = expect_token(f, Token_CloseBrace); - return make_struct_type(f, token, decls, decl_count, is_packed); + return make_struct_type(f, token, decls, decl_count, is_packed, is_ordered); } break; case Token_union: { diff --git a/src/string.cpp b/src/string.cpp index 8b5e3aa4b..743d5dcd4 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -43,6 +43,46 @@ gb_inline b32 are_strings_equal_ignore_case(String a, String b) { return false; } +GB_COMPARE_PROC(string_cmp) { + String x = *cast(String *)a; + String y = *cast(String *)b; + + if (x.len == y.len && + x.text == y.text) { + return 0; + } + + isize n = gb_min(x.len, y.len); + + isize fast = n/gb_size_of(isize) + 1; + isize offset = (fast-1)*gb_size_of(isize); + isize curr_block = 0; + if (n <= gb_size_of(isize)) { + fast = 0; + } + + isize *la = cast(isize *)x.text; + isize *lb = cast(isize *)y.text; + + for (; curr_block < fast; curr_block++) { + if ((la[curr_block] ^ lb[curr_block]) != 0) { + for (isize pos = curr_block*gb_size_of(isize); pos < n; pos++) { + if ((x.text[pos] ^ y.text[pos]) != 0) { + return cast(int)x.text[pos] - cast(int)y.text[pos]; + } + } + } + } + + for (; offset < n; offset++) { + if ((x.text[offset] ^ y.text[offset]) != 0) { + return cast(int)x.text[offset] - cast(int)y.text[offset]; + } + } + + return 0; +} + gb_inline isize string_extension_position(String str) { isize dot_pos = -1;