From e5f188241cd91086fd2af64da69c8a2b05144302 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 31 Dec 2018 11:41:56 +0000 Subject: [PATCH] Move error handling for bounds checking into separate procedures (eliminate caching issues) --- core/runtime/internal.odin | 146 ++++++++++++++++++++----------------- 1 file changed, 81 insertions(+), 65 deletions(-) diff --git a/core/runtime/internal.odin b/core/runtime/internal.odin index 5db1845f5..d6fa36167 100644 --- a/core/runtime/internal.odin +++ b/core/runtime/internal.odin @@ -268,61 +268,71 @@ complex128_eq :: inline proc "contextless" (a, b: complex128) -> bool { return r complex128_ne :: inline proc "contextless" (a, b: complex128) -> bool { return real(a) != real(b) || imag(a) != imag(b); } + + bounds_check_error :: proc "contextless" (file: string, line, column: int, index, count: int) { if 0 <= index && index < count do return; - - fd := os.stderr; - print_caller_location(fd, Source_Code_Location{file, line, column, ""}); - 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'); - debug_trap(); + handle_error :: proc "contextless" (file: string, line, column: int, index, count: int) { + fd := os.stderr; + print_caller_location(fd, Source_Code_Location{file, line, column, ""}); + 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'); + debug_trap(); + } + handle_error(file, line, column, index, count); } slice_expr_error :: proc "contextless" (file: string, line, column: int, lo, hi: int, len: int) { if 0 <= lo && lo <= hi && hi <= len do return; - - fd := os.stderr; - print_caller_location(fd, Source_Code_Location{file, line, column, ""}); - 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'); - debug_trap(); + handle_error :: proc "contextless" (file: string, line, column: int, lo, hi: int, len: int) { + fd := os.stderr; + print_caller_location(fd, Source_Code_Location{file, line, column, ""}); + 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'); + debug_trap(); + } + 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; - - fd := os.stderr; - print_caller_location(fd, Source_Code_Location{file, line, column, ""}); - 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'); - debug_trap(); + handle_error :: proc "contextless" (file: string, line, column: int, low, high, max: int) { + fd := os.stderr; + print_caller_location(fd, Source_Code_Location{file, line, column, ""}); + 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'); + debug_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; - - fd := os.stderr; - print_caller_location(fd, Source_Code_Location{file, line, column, ""}); - 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'); - debug_trap(); + handle_error :: proc "contextless" (file: string, line, column: int, from, to: typeid) { + fd := os.stderr; + print_caller_location(fd, Source_Code_Location{file, line, column, ""}); + 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'); + debug_trap(); + } + handle_error(file, line, column, from, to); } string_decode_rune :: inline proc "contextless" (s: string) -> (rune, int) { @@ -342,39 +352,45 @@ dynamic_array_expr_error_loc :: inline proc "contextless" (using loc := #caller_ } -make_slice_error_loc :: inline proc "contextless" (using loc := #caller_location, len: int) { +make_slice_error_loc :: inline proc "contextless" (loc := #caller_location, len: int) { if 0 <= len do return; - - 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'); - debug_trap(); + handle_error :: proc "contextless" (loc: Source_Code_Location, len: int) { + 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'); + debug_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; - - 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'); - debug_trap(); + handle_error :: proc "contextless" (loc: Source_Code_Location, len, cap: int) { + 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'); + debug_trap(); + } + handle_error(loc, len, cap); } -make_map_expr_error_loc :: inline proc "contextless" (using loc := #caller_location, cap: int) { +make_map_expr_error_loc :: inline proc "contextless" (loc := #caller_location, cap: int) { if 0 <= cap do return; - - 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'); - debug_trap(); + handle_error :: proc "contextless" (loc: Source_Code_Location, cap: int) { + 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'); + debug_trap(); + } + handle_error(loc, cap); }