diff --git a/core/runtime/core.odin b/core/runtime/core.odin index ba8a550b8..36884950c 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -3,7 +3,6 @@ // The compiler relies upon this _exact_ order package runtime -import "core:os" import "intrinsics" _ :: intrinsics; @@ -510,15 +509,11 @@ __init_context :: proc "contextless" (c: ^Context) { c.temp_allocator.procedure = default_temp_allocator_proc; c.temp_allocator.data = &global_default_temp_allocator_data; - c.thread_id = os.current_thread_id(); // NOTE(bill): This is "contextless" so it is okay to call + c.thread_id = current_thread_id(); // NOTE(bill): This is "contextless" so it is okay to call c.assertion_failure_proc = default_assertion_failure_proc; c.logger.procedure = default_logger_proc; c.logger.data = nil; - - // c.stdin = os.stdin; - // c.stdout = os.stdout; - // c.stderr = os.stderr; } @builtin @@ -526,19 +521,6 @@ init_global_temporary_allocator :: proc(data: []byte, backup_allocator := contex default_temp_allocator_init(&global_default_temp_allocator_data, data, backup_allocator); } -default_assertion_failure_proc :: proc(prefix, message: string, loc: Source_Code_Location) { - fd := os.stderr; - print_caller_location(fd, loc); - os.write_string(fd, " "); - os.write_string(fd, prefix); - if len(message) > 0 { - os.write_string(fd, ": "); - os.write_string(fd, message); - } - os.write_byte(fd, '\n'); - debug_trap(); -} - @builtin diff --git a/core/runtime/default_allocators.odin b/core/runtime/default_allocators.odin index 14f4edcda..7e616d043 100644 --- a/core/runtime/default_allocators.odin +++ b/core/runtime/default_allocators.odin @@ -1,14 +1,32 @@ package runtime -import "core:os" +when ODIN_OS == "freestanding" { + // mem.nil_allocator reimplementation -default_allocator_proc :: os.heap_allocator_proc; + default_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, + size, alignment: int, + old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr { + return nil; + } -default_allocator :: proc() -> Allocator { - return os.heap_allocator(); + default_allocator :: proc() -> Allocator { + return Allocator{ + procedure = default_allocator_proc, + data = nil, + }; + } +} else { + import "core:os" + + default_allocator_proc :: os.heap_allocator_proc; + + default_allocator :: proc() -> Allocator { + return os.heap_allocator(); + } } + Default_Temp_Allocator :: struct { data: []byte, curr_offset: int, diff --git a/core/runtime/defaults.odin b/core/runtime/defaults.odin new file mode 100644 index 000000000..c2536d61a --- /dev/null +++ b/core/runtime/defaults.odin @@ -0,0 +1,21 @@ +package runtime + +import "core:os" + +current_thread_id :: proc "contextless" () -> int { + return os.current_thread_id(); +} + +default_assertion_failure_proc :: proc(prefix, message: string, loc: Source_Code_Location) { + fd := os.stderr; + print_caller_location(fd, loc); + os.write_string(fd, " "); + os.write_string(fd, prefix); + if len(message) > 0 { + os.write_string(fd, ": "); + os.write_string(fd, message); + } + os.write_byte(fd, '\n'); + debug_trap(); +} + diff --git a/core/runtime/error_checks.odin b/core/runtime/error_checks.odin new file mode 100644 index 000000000..1e1251c38 --- /dev/null +++ b/core/runtime/error_checks.odin @@ -0,0 +1,160 @@ +package runtime + +import "core:os" + + +bounds_trap :: proc "contextless" () -> ! { + when ODIN_OS == "windows" { + windows_trap_array_bounds(); + } else { + trap(); + } +} + +type_assertion_trap :: proc "contextless" () -> ! { + when ODIN_OS == "windows" { + windows_trap_type_assertion(); + } else { + trap(); + } +} + + +bounds_check_error :: proc "contextless" (file: string, line, column: int, index, count: int) { + if 0 <= index && index < count do return; + handle_error :: proc "contextless" (file: string, line, column: int, index, count: int) { + context = default_context(); + fd := os.stderr; + print_caller_location(fd, Source_Code_Location{file, line, column, "", 0}); + os.write_string(fd, " Index "); + print_i64(fd, i64(index)); + os.write_string(fd, " is out of bounds range 0:"); + print_i64(fd, i64(count)); + os.write_byte(fd, '\n'); + bounds_trap(); + } + handle_error(file, line, column, index, count); +} + +slice_handle_error :: proc "contextless" (file: string, line, column: int, lo, hi: int, len: int) { + context = default_context(); + fd := os.stderr; + print_caller_location(fd, Source_Code_Location{file, line, column, "", 0}); + os.write_string(fd, " Invalid slice indices: "); + print_i64(fd, i64(lo)); + os.write_string(fd, ":"); + print_i64(fd, i64(hi)); + os.write_string(fd, ":"); + print_i64(fd, i64(len)); + os.write_byte(fd, '\n'); + bounds_trap(); +} + +slice_expr_error_hi :: proc "contextless" (file: string, line, column: int, hi: int, len: int) { + if 0 <= hi && hi <= len do return; + slice_handle_error(file, line, column, 0, hi, len); +} + +slice_expr_error_lo_hi :: proc "contextless" (file: string, line, column: int, lo, hi: int, len: int) { + if 0 <= lo && lo <= len && lo <= hi && hi <= len do return; + slice_handle_error(file, line, column, lo, hi, len); +} + +dynamic_array_expr_error :: proc "contextless" (file: string, line, column: int, low, high, max: int) { + if 0 <= low && low <= high && high <= max do return; + handle_error :: proc "contextless" (file: string, line, column: int, low, high, max: int) { + context = default_context(); + fd := os.stderr; + print_caller_location(fd, Source_Code_Location{file, line, column, "", 0}); + os.write_string(fd, " Invalid dynamic array values: "); + print_i64(fd, i64(low)); + os.write_string(fd, ":"); + print_i64(fd, i64(high)); + os.write_string(fd, ":"); + print_i64(fd, i64(max)); + os.write_byte(fd, '\n'); + bounds_trap(); + } + handle_error(file, line, column, low, high, max); +} + + +type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column: int, from, to: typeid) { + if ok do return; + handle_error :: proc "contextless" (file: string, line, column: int, from, to: typeid) { + context = default_context(); + fd := os.stderr; + print_caller_location(fd, Source_Code_Location{file, line, column, "", 0}); + os.write_string(fd, " Invalid type assertion from "); + print_typeid(fd, from); + os.write_string(fd, " to "); + print_typeid(fd, to); + os.write_byte(fd, '\n'); + type_assertion_trap(); + } + handle_error(file, line, column, from, to); +} + +make_slice_error_loc :: inline proc "contextless" (loc := #caller_location, len: int) { + if 0 <= len do return; + handle_error :: proc "contextless" (loc: Source_Code_Location, len: int) { + context = default_context(); + fd := os.stderr; + print_caller_location(fd, loc); + os.write_string(fd, " Invalid slice length for make: "); + print_i64(fd, i64(len)); + os.write_byte(fd, '\n'); + bounds_trap(); + } + handle_error(loc, len); +} + +make_dynamic_array_error_loc :: inline proc "contextless" (using loc := #caller_location, len, cap: int) { + if 0 <= len && len <= cap do return; + handle_error :: proc "contextless" (loc: Source_Code_Location, len, cap: int) { + context = default_context(); + fd := os.stderr; + print_caller_location(fd, loc); + os.write_string(fd, " Invalid dynamic array parameters for make: "); + print_i64(fd, i64(len)); + os.write_byte(fd, ':'); + print_i64(fd, i64(cap)); + os.write_byte(fd, '\n'); + bounds_trap(); + } + handle_error(loc, len, cap); +} + +make_map_expr_error_loc :: inline proc "contextless" (loc := #caller_location, cap: int) { + if 0 <= cap do return; + handle_error :: proc "contextless" (loc: Source_Code_Location, cap: int) { + context = default_context(); + fd := os.stderr; + print_caller_location(fd, loc); + os.write_string(fd, " Invalid map capacity for make: "); + print_i64(fd, i64(cap)); + os.write_byte(fd, '\n'); + bounds_trap(); + } + handle_error(loc, cap); +} + + + + + +bounds_check_error_loc :: inline proc "contextless" (using loc := #caller_location, index, count: int) { + bounds_check_error(file_path, int(line), int(column), index, count); +} + +slice_expr_error_hi_loc :: inline proc "contextless" (using loc := #caller_location, hi: int, len: int) { + slice_expr_error_hi(file_path, int(line), int(column), hi, len); +} + +slice_expr_error_lo_hi_loc :: inline proc "contextless" (using loc := #caller_location, lo, hi: int, len: int) { + slice_expr_error_lo_hi(file_path, int(line), int(column), lo, hi, len); +} + +dynamic_array_expr_error_loc :: inline proc "contextless" (using loc := #caller_location, low, high, max: int) { + dynamic_array_expr_error(file_path, int(line), int(column), low, high, max); +} diff --git a/core/runtime/internal.odin b/core/runtime/internal.odin index 4a707f196..83c187449 100644 --- a/core/runtime/internal.odin +++ b/core/runtime/internal.odin @@ -1,7 +1,5 @@ package runtime -import "core:os" - bswap_16 :: proc "none" (x: u16) -> u16 { return x>>8 | x<<8; } @@ -160,303 +158,6 @@ mem_resize :: inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = } - - - - - - - -print_u64 :: proc(fd: os.Handle, x: u64) { - digits := "0123456789"; - - a: [129]byte; - i := len(a); - b := u64(10); - u := x; - for u >= b { - i -= 1; a[i] = digits[u % b]; - u /= b; - } - i -= 1; a[i] = digits[u % b]; - - os.write(fd, a[i:]); -} - -print_i64 :: proc(fd: os.Handle, x: i64) { - digits := "0123456789"; - b :: i64(10); - - u := x; - neg := u < 0; - u = abs(u); - - a: [129]byte; - i := len(a); - for u >= b { - i -= 1; a[i] = digits[u % b]; - u /= b; - } - i -= 1; a[i] = digits[u % b]; - if neg { - i -= 1; a[i] = '-'; - } - - os.write(fd, a[i:]); -} - -print_caller_location :: proc(fd: os.Handle, using loc: Source_Code_Location) { - os.write_string(fd, file_path); - os.write_byte(fd, '('); - print_u64(fd, u64(line)); - os.write_byte(fd, ':'); - print_u64(fd, u64(column)); - os.write_byte(fd, ')'); -} -print_typeid :: proc(fd: os.Handle, id: typeid) { - if id == nil { - os.write_string(fd, "nil"); - } else { - ti := type_info_of(id); - print_type(fd, ti); - } -} -print_type :: proc(fd: os.Handle, ti: ^Type_Info) { - if ti == nil { - os.write_string(fd, "nil"); - return; - } - - switch info in ti.variant { - case Type_Info_Named: - os.write_string(fd, info.name); - case Type_Info_Integer: - switch ti.id { - case int: os.write_string(fd, "int"); - case uint: os.write_string(fd, "uint"); - case uintptr: os.write_string(fd, "uintptr"); - case: - os.write_byte(fd, 'i' if info.signed else 'u'); - print_u64(fd, u64(8*ti.size)); - } - case Type_Info_Rune: - os.write_string(fd, "rune"); - case Type_Info_Float: - os.write_byte(fd, 'f'); - print_u64(fd, u64(8*ti.size)); - case Type_Info_Complex: - os.write_string(fd, "complex"); - print_u64(fd, u64(8*ti.size)); - case Type_Info_Quaternion: - os.write_string(fd, "quaternion"); - print_u64(fd, u64(8*ti.size)); - case Type_Info_String: - os.write_string(fd, "string"); - case Type_Info_Boolean: - switch ti.id { - case bool: os.write_string(fd, "bool"); - case: - os.write_byte(fd, 'b'); - print_u64(fd, u64(8*ti.size)); - } - case Type_Info_Any: - os.write_string(fd, "any"); - case Type_Info_Type_Id: - os.write_string(fd, "typeid"); - - case Type_Info_Pointer: - if info.elem == nil { - os.write_string(fd, "rawptr"); - } else { - os.write_string(fd, "^"); - print_type(fd, info.elem); - } - case Type_Info_Procedure: - os.write_string(fd, "proc"); - if info.params == nil { - os.write_string(fd, "()"); - } else { - t := info.params.variant.(Type_Info_Tuple); - os.write_byte(fd, '('); - for t, i in t.types { - if i > 0 do os.write_string(fd, ", "); - print_type(fd, t); - } - os.write_string(fd, ")"); - } - if info.results != nil { - os.write_string(fd, " -> "); - print_type(fd, info.results); - } - case Type_Info_Tuple: - count := len(info.names); - if count != 1 do os.write_byte(fd, '('); - for name, i in info.names { - if i > 0 do os.write_string(fd, ", "); - - t := info.types[i]; - - if len(name) > 0 { - os.write_string(fd, name); - os.write_string(fd, ": "); - } - print_type(fd, t); - } - if count != 1 do os.write_string(fd, ")"); - - case Type_Info_Array: - os.write_byte(fd, '['); - print_u64(fd, u64(info.count)); - os.write_byte(fd, ']'); - print_type(fd, info.elem); - - case Type_Info_Enumerated_Array: - os.write_byte(fd, '['); - print_type(fd, info.index); - os.write_byte(fd, ']'); - print_type(fd, info.elem); - - - case Type_Info_Dynamic_Array: - os.write_string(fd, "[dynamic]"); - print_type(fd, info.elem); - case Type_Info_Slice: - os.write_string(fd, "[]"); - print_type(fd, info.elem); - - case Type_Info_Map: - os.write_string(fd, "map["); - print_type(fd, info.key); - os.write_byte(fd, ']'); - print_type(fd, info.value); - - case Type_Info_Struct: - switch info.soa_kind { - case .None: // Ignore - case .Fixed: - os.write_string(fd, "#soa["); - print_u64(fd, u64(info.soa_len)); - os.write_byte(fd, ']'); - print_type(fd, info.soa_base_type); - return; - case .Slice: - os.write_string(fd, "#soa[]"); - print_type(fd, info.soa_base_type); - return; - case .Dynamic: - os.write_string(fd, "#soa[dynamic]"); - print_type(fd, info.soa_base_type); - return; - } - - os.write_string(fd, "struct "); - if info.is_packed do os.write_string(fd, "#packed "); - if info.is_raw_union do os.write_string(fd, "#raw_union "); - if info.custom_align { - os.write_string(fd, "#align "); - print_u64(fd, u64(ti.align)); - os.write_byte(fd, ' '); - } - os.write_byte(fd, '{'); - for name, i in info.names { - if i > 0 do os.write_string(fd, ", "); - os.write_string(fd, name); - os.write_string(fd, ": "); - print_type(fd, info.types[i]); - } - os.write_byte(fd, '}'); - - case Type_Info_Union: - os.write_string(fd, "union "); - if info.custom_align { - os.write_string(fd, "#align "); - print_u64(fd, u64(ti.align)); - } - if info.no_nil { - os.write_string(fd, "#no_nil "); - } - os.write_byte(fd, '{'); - for variant, i in info.variants { - if i > 0 do os.write_string(fd, ", "); - print_type(fd, variant); - } - os.write_string(fd, "}"); - - case Type_Info_Enum: - os.write_string(fd, "enum "); - print_type(fd, info.base); - os.write_string(fd, " {"); - for name, i in info.names { - if i > 0 do os.write_string(fd, ", "); - os.write_string(fd, name); - } - os.write_string(fd, "}"); - - case Type_Info_Bit_Field: - os.write_string(fd, "bit_field "); - if ti.align != 1 { - os.write_string(fd, "#align "); - print_u64(fd, u64(ti.align)); - os.write_byte(fd, ' '); - } - os.write_string(fd, " {"); - for name, i in info.names { - if i > 0 do os.write_string(fd, ", "); - os.write_string(fd, name); - os.write_string(fd, ": "); - print_u64(fd, u64(info.bits[i])); - } - os.write_string(fd, "}"); - - case Type_Info_Bit_Set: - os.write_string(fd, "bit_set["); - - #partial switch elem in type_info_base(info.elem).variant { - case Type_Info_Enum: - print_type(fd, info.elem); - case Type_Info_Rune: - os.write_encoded_rune(fd, rune(info.lower)); - os.write_string(fd, ".."); - os.write_encoded_rune(fd, rune(info.upper)); - case: - print_i64(fd, info.lower); - os.write_string(fd, ".."); - print_i64(fd, info.upper); - } - if info.underlying != nil { - os.write_string(fd, "; "); - print_type(fd, info.underlying); - } - os.write_byte(fd, ']'); - - case Type_Info_Opaque: - os.write_string(fd, "opaque "); - print_type(fd, info.elem); - - case Type_Info_Simd_Vector: - if info.is_x86_mmx { - os.write_string(fd, "intrinsics.x86_mmx"); - } else { - os.write_string(fd, "#simd["); - print_u64(fd, u64(info.count)); - os.write_byte(fd, ']'); - print_type(fd, info.elem); - } - - case Type_Info_Relative_Pointer: - os.write_string(fd, "#relative("); - print_type(fd, info.base_integer); - os.write_string(fd, ") "); - print_type(fd, info.pointer); - - case Type_Info_Relative_Slice: - os.write_string(fd, "#relative("); - print_type(fd, info.base_integer); - os.write_string(fd, ") "); - print_type(fd, info.slice); - } -} - memory_compare :: proc "contextless" (a, b: rawptr, n: int) -> int #no_bounds_check { x := uintptr(a); y := uintptr(b); @@ -583,99 +284,6 @@ quaternion256_eq :: inline proc "contextless" (a, b: quaternion256) -> bool { re quaternion256_ne :: inline proc "contextless" (a, b: quaternion256) -> bool { return real(a) != real(b) || imag(a) != imag(b) || jmag(a) != jmag(b) || kmag(a) != kmag(b); } -bounds_trap :: proc "contextless" () -> ! { - when ODIN_OS == "windows" { - windows_trap_array_bounds(); - } else { - trap(); - } -} - -type_assertion_trap :: proc "contextless" () -> ! { - when ODIN_OS == "windows" { - windows_trap_type_assertion(); - } else { - trap(); - } -} - - -bounds_check_error :: proc "contextless" (file: string, line, column: int, index, count: int) { - if 0 <= index && index < count do return; - handle_error :: proc "contextless" (file: string, line, column: int, index, count: int) { - context = default_context(); - fd := os.stderr; - print_caller_location(fd, Source_Code_Location{file, line, column, "", 0}); - os.write_string(fd, " Index "); - print_i64(fd, i64(index)); - os.write_string(fd, " is out of bounds range 0:"); - print_i64(fd, i64(count)); - os.write_byte(fd, '\n'); - bounds_trap(); - } - handle_error(file, line, column, index, count); -} - -slice_handle_error :: proc "contextless" (file: string, line, column: int, lo, hi: int, len: int) { - context = default_context(); - fd := os.stderr; - print_caller_location(fd, Source_Code_Location{file, line, column, "", 0}); - os.write_string(fd, " Invalid slice indices: "); - print_i64(fd, i64(lo)); - os.write_string(fd, ":"); - print_i64(fd, i64(hi)); - os.write_string(fd, ":"); - print_i64(fd, i64(len)); - os.write_byte(fd, '\n'); - bounds_trap(); -} - -slice_expr_error_hi :: proc "contextless" (file: string, line, column: int, hi: int, len: int) { - if 0 <= hi && hi <= len do return; - slice_handle_error(file, line, column, 0, hi, len); -} - -slice_expr_error_lo_hi :: proc "contextless" (file: string, line, column: int, lo, hi: int, len: int) { - if 0 <= lo && lo <= len && lo <= hi && hi <= len do return; - slice_handle_error(file, line, column, lo, hi, len); -} - -dynamic_array_expr_error :: proc "contextless" (file: string, line, column: int, low, high, max: int) { - if 0 <= low && low <= high && high <= max do return; - handle_error :: proc "contextless" (file: string, line, column: int, low, high, max: int) { - context = default_context(); - fd := os.stderr; - print_caller_location(fd, Source_Code_Location{file, line, column, "", 0}); - os.write_string(fd, " Invalid dynamic array values: "); - print_i64(fd, i64(low)); - os.write_string(fd, ":"); - print_i64(fd, i64(high)); - os.write_string(fd, ":"); - print_i64(fd, i64(max)); - os.write_byte(fd, '\n'); - bounds_trap(); - } - handle_error(file, line, column, low, high, max); -} - - -type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column: int, from, to: typeid) { - if ok do return; - handle_error :: proc "contextless" (file: string, line, column: int, from, to: typeid) { - context = default_context(); - fd := os.stderr; - print_caller_location(fd, Source_Code_Location{file, line, column, "", 0}); - os.write_string(fd, " Invalid type assertion from "); - print_typeid(fd, from); - os.write_string(fd, " to "); - print_typeid(fd, to); - os.write_byte(fd, '\n'); - type_assertion_trap(); - } - handle_error(file, line, column, from, to); -} - - string_decode_rune :: inline proc "contextless" (s: string) -> (rune, int) { // NOTE(bill): Duplicated here to remove dependency on package unicode/utf8 @@ -755,70 +363,6 @@ string_decode_rune :: inline proc "contextless" (s: string) -> (rune, int) { return rune(s0&MASK4)<<18 | rune(b1&MASKX)<<12 | rune(b2&MASKX)<<6 | rune(b3&MASKX), 4; } -bounds_check_error_loc :: inline proc "contextless" (using loc := #caller_location, index, count: int) { - bounds_check_error(file_path, int(line), int(column), index, count); -} - -slice_expr_error_hi_loc :: inline proc "contextless" (using loc := #caller_location, hi: int, len: int) { - slice_expr_error_hi(file_path, int(line), int(column), hi, len); -} - -slice_expr_error_lo_hi_loc :: inline proc "contextless" (using loc := #caller_location, lo, hi: int, len: int) { - slice_expr_error_lo_hi(file_path, int(line), int(column), lo, hi, len); -} - -dynamic_array_expr_error_loc :: inline proc "contextless" (using loc := #caller_location, low, high, max: int) { - dynamic_array_expr_error(file_path, int(line), int(column), low, high, max); -} - - -make_slice_error_loc :: inline proc "contextless" (loc := #caller_location, len: int) { - if 0 <= len do return; - handle_error :: proc "contextless" (loc: Source_Code_Location, len: int) { - context = default_context(); - fd := os.stderr; - print_caller_location(fd, loc); - os.write_string(fd, " Invalid slice length for make: "); - print_i64(fd, i64(len)); - os.write_byte(fd, '\n'); - bounds_trap(); - } - handle_error(loc, len); -} - -make_dynamic_array_error_loc :: inline proc "contextless" (using loc := #caller_location, len, cap: int) { - if 0 <= len && len <= cap do return; - handle_error :: proc "contextless" (loc: Source_Code_Location, len, cap: int) { - context = default_context(); - fd := os.stderr; - print_caller_location(fd, loc); - os.write_string(fd, " Invalid dynamic array parameters for make: "); - print_i64(fd, i64(len)); - os.write_byte(fd, ':'); - print_i64(fd, i64(cap)); - os.write_byte(fd, '\n'); - bounds_trap(); - } - handle_error(loc, len, cap); -} - -make_map_expr_error_loc :: inline proc "contextless" (loc := #caller_location, cap: int) { - if 0 <= cap do return; - handle_error :: proc "contextless" (loc: Source_Code_Location, cap: int) { - context = default_context(); - fd := os.stderr; - print_caller_location(fd, loc); - os.write_string(fd, " Invalid map capacity for make: "); - print_i64(fd, i64(cap)); - os.write_byte(fd, '\n'); - bounds_trap(); - } - handle_error(loc, cap); -} - - - - @(default_calling_convention = "c") foreign { @(link_name="llvm.sqrt.f32") _sqrt_f32 :: proc(x: f32) -> f32 --- diff --git a/core/runtime/print.odin b/core/runtime/print.odin new file mode 100644 index 000000000..b1d16c1fd --- /dev/null +++ b/core/runtime/print.odin @@ -0,0 +1,293 @@ +package runtime + +import "core:os" + +print_u64 :: proc(fd: os.Handle, x: u64) { + digits := "0123456789"; + + a: [129]byte; + i := len(a); + b := u64(10); + u := x; + for u >= b { + i -= 1; a[i] = digits[u % b]; + u /= b; + } + i -= 1; a[i] = digits[u % b]; + + os.write(fd, a[i:]); +} + +print_i64 :: proc(fd: os.Handle, x: i64) { + digits := "0123456789"; + b :: i64(10); + + u := x; + neg := u < 0; + u = abs(u); + + a: [129]byte; + i := len(a); + for u >= b { + i -= 1; a[i] = digits[u % b]; + u /= b; + } + i -= 1; a[i] = digits[u % b]; + if neg { + i -= 1; a[i] = '-'; + } + + os.write(fd, a[i:]); +} + +print_caller_location :: proc(fd: os.Handle, using loc: Source_Code_Location) { + os.write_string(fd, file_path); + os.write_byte(fd, '('); + print_u64(fd, u64(line)); + os.write_byte(fd, ':'); + print_u64(fd, u64(column)); + os.write_byte(fd, ')'); +} +print_typeid :: proc(fd: os.Handle, id: typeid) { + if id == nil { + os.write_string(fd, "nil"); + } else { + ti := type_info_of(id); + print_type(fd, ti); + } +} +print_type :: proc(fd: os.Handle, ti: ^Type_Info) { + if ti == nil { + os.write_string(fd, "nil"); + return; + } + + switch info in ti.variant { + case Type_Info_Named: + os.write_string(fd, info.name); + case Type_Info_Integer: + switch ti.id { + case int: os.write_string(fd, "int"); + case uint: os.write_string(fd, "uint"); + case uintptr: os.write_string(fd, "uintptr"); + case: + os.write_byte(fd, 'i' if info.signed else 'u'); + print_u64(fd, u64(8*ti.size)); + } + case Type_Info_Rune: + os.write_string(fd, "rune"); + case Type_Info_Float: + os.write_byte(fd, 'f'); + print_u64(fd, u64(8*ti.size)); + case Type_Info_Complex: + os.write_string(fd, "complex"); + print_u64(fd, u64(8*ti.size)); + case Type_Info_Quaternion: + os.write_string(fd, "quaternion"); + print_u64(fd, u64(8*ti.size)); + case Type_Info_String: + os.write_string(fd, "string"); + case Type_Info_Boolean: + switch ti.id { + case bool: os.write_string(fd, "bool"); + case: + os.write_byte(fd, 'b'); + print_u64(fd, u64(8*ti.size)); + } + case Type_Info_Any: + os.write_string(fd, "any"); + case Type_Info_Type_Id: + os.write_string(fd, "typeid"); + + case Type_Info_Pointer: + if info.elem == nil { + os.write_string(fd, "rawptr"); + } else { + os.write_string(fd, "^"); + print_type(fd, info.elem); + } + case Type_Info_Procedure: + os.write_string(fd, "proc"); + if info.params == nil { + os.write_string(fd, "()"); + } else { + t := info.params.variant.(Type_Info_Tuple); + os.write_byte(fd, '('); + for t, i in t.types { + if i > 0 do os.write_string(fd, ", "); + print_type(fd, t); + } + os.write_string(fd, ")"); + } + if info.results != nil { + os.write_string(fd, " -> "); + print_type(fd, info.results); + } + case Type_Info_Tuple: + count := len(info.names); + if count != 1 do os.write_byte(fd, '('); + for name, i in info.names { + if i > 0 do os.write_string(fd, ", "); + + t := info.types[i]; + + if len(name) > 0 { + os.write_string(fd, name); + os.write_string(fd, ": "); + } + print_type(fd, t); + } + if count != 1 do os.write_string(fd, ")"); + + case Type_Info_Array: + os.write_byte(fd, '['); + print_u64(fd, u64(info.count)); + os.write_byte(fd, ']'); + print_type(fd, info.elem); + + case Type_Info_Enumerated_Array: + os.write_byte(fd, '['); + print_type(fd, info.index); + os.write_byte(fd, ']'); + print_type(fd, info.elem); + + + case Type_Info_Dynamic_Array: + os.write_string(fd, "[dynamic]"); + print_type(fd, info.elem); + case Type_Info_Slice: + os.write_string(fd, "[]"); + print_type(fd, info.elem); + + case Type_Info_Map: + os.write_string(fd, "map["); + print_type(fd, info.key); + os.write_byte(fd, ']'); + print_type(fd, info.value); + + case Type_Info_Struct: + switch info.soa_kind { + case .None: // Ignore + case .Fixed: + os.write_string(fd, "#soa["); + print_u64(fd, u64(info.soa_len)); + os.write_byte(fd, ']'); + print_type(fd, info.soa_base_type); + return; + case .Slice: + os.write_string(fd, "#soa[]"); + print_type(fd, info.soa_base_type); + return; + case .Dynamic: + os.write_string(fd, "#soa[dynamic]"); + print_type(fd, info.soa_base_type); + return; + } + + os.write_string(fd, "struct "); + if info.is_packed do os.write_string(fd, "#packed "); + if info.is_raw_union do os.write_string(fd, "#raw_union "); + if info.custom_align { + os.write_string(fd, "#align "); + print_u64(fd, u64(ti.align)); + os.write_byte(fd, ' '); + } + os.write_byte(fd, '{'); + for name, i in info.names { + if i > 0 do os.write_string(fd, ", "); + os.write_string(fd, name); + os.write_string(fd, ": "); + print_type(fd, info.types[i]); + } + os.write_byte(fd, '}'); + + case Type_Info_Union: + os.write_string(fd, "union "); + if info.custom_align { + os.write_string(fd, "#align "); + print_u64(fd, u64(ti.align)); + } + if info.no_nil { + os.write_string(fd, "#no_nil "); + } + os.write_byte(fd, '{'); + for variant, i in info.variants { + if i > 0 do os.write_string(fd, ", "); + print_type(fd, variant); + } + os.write_string(fd, "}"); + + case Type_Info_Enum: + os.write_string(fd, "enum "); + print_type(fd, info.base); + os.write_string(fd, " {"); + for name, i in info.names { + if i > 0 do os.write_string(fd, ", "); + os.write_string(fd, name); + } + os.write_string(fd, "}"); + + case Type_Info_Bit_Field: + os.write_string(fd, "bit_field "); + if ti.align != 1 { + os.write_string(fd, "#align "); + print_u64(fd, u64(ti.align)); + os.write_byte(fd, ' '); + } + os.write_string(fd, " {"); + for name, i in info.names { + if i > 0 do os.write_string(fd, ", "); + os.write_string(fd, name); + os.write_string(fd, ": "); + print_u64(fd, u64(info.bits[i])); + } + os.write_string(fd, "}"); + + case Type_Info_Bit_Set: + os.write_string(fd, "bit_set["); + + #partial switch elem in type_info_base(info.elem).variant { + case Type_Info_Enum: + print_type(fd, info.elem); + case Type_Info_Rune: + os.write_encoded_rune(fd, rune(info.lower)); + os.write_string(fd, ".."); + os.write_encoded_rune(fd, rune(info.upper)); + case: + print_i64(fd, info.lower); + os.write_string(fd, ".."); + print_i64(fd, info.upper); + } + if info.underlying != nil { + os.write_string(fd, "; "); + print_type(fd, info.underlying); + } + os.write_byte(fd, ']'); + + case Type_Info_Opaque: + os.write_string(fd, "opaque "); + print_type(fd, info.elem); + + case Type_Info_Simd_Vector: + if info.is_x86_mmx { + os.write_string(fd, "intrinsics.x86_mmx"); + } else { + os.write_string(fd, "#simd["); + print_u64(fd, u64(info.count)); + os.write_byte(fd, ']'); + print_type(fd, info.elem); + } + + case Type_Info_Relative_Pointer: + os.write_string(fd, "#relative("); + print_type(fd, info.base_integer); + os.write_string(fd, ") "); + print_type(fd, info.pointer); + + case Type_Info_Relative_Slice: + os.write_string(fd, "#relative("); + print_type(fd, info.base_integer); + os.write_string(fd, ") "); + print_type(fd, info.slice); + } +}