Move error handling for bounds checking into separate procedures (eliminate caching issues)

This commit is contained in:
gingerBill
2018-12-31 11:41:56 +00:00
parent 6d3203c11b
commit e5f188241c
+81 -65
View File
@@ -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); } 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) { bounds_check_error :: proc "contextless" (file: string, line, column: int, index, count: int) {
if 0 <= index && index < count do return; if 0 <= index && index < count do return;
handle_error :: proc "contextless" (file: string, line, column: int, index, count: int) {
fd := os.stderr; fd := os.stderr;
print_caller_location(fd, Source_Code_Location{file, line, column, ""}); print_caller_location(fd, Source_Code_Location{file, line, column, ""});
os.write_string(fd, " Index "); os.write_string(fd, " Index ");
print_i64(fd, i64(index)); print_i64(fd, i64(index));
os.write_string(fd, " is out of bounds range 0:"); os.write_string(fd, " is out of bounds range 0:");
print_i64(fd, i64(count)); print_i64(fd, i64(count));
os.write_byte(fd, '\n'); os.write_byte(fd, '\n');
debug_trap(); debug_trap();
}
handle_error(file, line, column, index, count);
} }
slice_expr_error :: proc "contextless" (file: string, line, column: int, lo, hi: int, len: int) { slice_expr_error :: proc "contextless" (file: string, line, column: int, lo, hi: int, len: int) {
if 0 <= lo && lo <= hi && hi <= len do return; if 0 <= lo && lo <= hi && hi <= len do return;
handle_error :: proc "contextless" (file: string, line, column: int, lo, hi: int, len: int) {
fd := os.stderr; fd := os.stderr;
print_caller_location(fd, Source_Code_Location{file, line, column, ""}); print_caller_location(fd, Source_Code_Location{file, line, column, ""});
os.write_string(fd, " Invalid slice indices: "); os.write_string(fd, " Invalid slice indices: ");
print_i64(fd, i64(lo)); print_i64(fd, i64(lo));
os.write_string(fd, ":"); os.write_string(fd, ":");
print_i64(fd, i64(hi)); print_i64(fd, i64(hi));
os.write_string(fd, ":"); os.write_string(fd, ":");
print_i64(fd, i64(len)); print_i64(fd, i64(len));
os.write_byte(fd, '\n'); os.write_byte(fd, '\n');
debug_trap(); 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) { dynamic_array_expr_error :: proc "contextless" (file: string, line, column: int, low, high, max: int) {
if 0 <= low && low <= high && high <= max do return; if 0 <= low && low <= high && high <= max do return;
handle_error :: proc "contextless" (file: string, line, column: int, low, high, max: int) {
fd := os.stderr; fd := os.stderr;
print_caller_location(fd, Source_Code_Location{file, line, column, ""}); print_caller_location(fd, Source_Code_Location{file, line, column, ""});
os.write_string(fd, " Invalid dynamic array values: "); os.write_string(fd, " Invalid dynamic array values: ");
print_i64(fd, i64(low)); print_i64(fd, i64(low));
os.write_string(fd, ":"); os.write_string(fd, ":");
print_i64(fd, i64(high)); print_i64(fd, i64(high));
os.write_string(fd, ":"); os.write_string(fd, ":");
print_i64(fd, i64(max)); print_i64(fd, i64(max));
os.write_byte(fd, '\n'); os.write_byte(fd, '\n');
debug_trap(); 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) { type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column: int, from, to: typeid) {
if ok do return; if ok do return;
handle_error :: proc "contextless" (file: string, line, column: int, from, to: typeid) {
fd := os.stderr; fd := os.stderr;
print_caller_location(fd, Source_Code_Location{file, line, column, ""}); print_caller_location(fd, Source_Code_Location{file, line, column, ""});
os.write_string(fd, " Invalid type assertion from "); os.write_string(fd, " Invalid type assertion from ");
print_typeid(fd, from); print_typeid(fd, from);
os.write_string(fd, " to "); os.write_string(fd, " to ");
print_typeid(fd, to); print_typeid(fd, to);
os.write_byte(fd, '\n'); os.write_byte(fd, '\n');
debug_trap(); debug_trap();
}
handle_error(file, line, column, from, to);
} }
string_decode_rune :: inline proc "contextless" (s: string) -> (rune, int) { 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; if 0 <= len do return;
handle_error :: proc "contextless" (loc: Source_Code_Location, len: int) {
fd := os.stderr; fd := os.stderr;
print_caller_location(fd, loc); print_caller_location(fd, loc);
os.write_string(fd, " Invalid slice length for make: "); os.write_string(fd, " Invalid slice length for make: ");
print_i64(fd, i64(len)); print_i64(fd, i64(len));
os.write_byte(fd, '\n'); os.write_byte(fd, '\n');
debug_trap(); debug_trap();
}
handle_error(loc, len);
} }
make_dynamic_array_error_loc :: inline proc "contextless" (using loc := #caller_location, len, cap: int) { make_dynamic_array_error_loc :: inline proc "contextless" (using loc := #caller_location, len, cap: int) {
if 0 <= len && len <= cap do return; if 0 <= len && len <= cap do return;
handle_error :: proc "contextless" (loc: Source_Code_Location, len, cap: int) {
fd := os.stderr; fd := os.stderr;
print_caller_location(fd, loc); print_caller_location(fd, loc);
os.write_string(fd, " Invalid dynamic array parameters for make: "); os.write_string(fd, " Invalid dynamic array parameters for make: ");
print_i64(fd, i64(len)); print_i64(fd, i64(len));
os.write_byte(fd, ':'); os.write_byte(fd, ':');
print_i64(fd, i64(cap)); print_i64(fd, i64(cap));
os.write_byte(fd, '\n'); os.write_byte(fd, '\n');
debug_trap(); 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; if 0 <= cap do return;
handle_error :: proc "contextless" (loc: Source_Code_Location, cap: int) {
fd := os.stderr; fd := os.stderr;
print_caller_location(fd, loc); print_caller_location(fd, loc);
os.write_string(fd, " Invalid map capacity for make: "); os.write_string(fd, " Invalid map capacity for make: ");
print_i64(fd, i64(cap)); print_i64(fd, i64(cap));
os.write_byte(fd, '\n'); os.write_byte(fd, '\n');
debug_trap(); debug_trap();
}
handle_error(loc, cap);
} }