Reorganize code to improve code generation

This commit is contained in:
gingerBill
2021-06-08 16:21:19 +01:00
parent 8ec2ca9dcd
commit f30e6f50bd
5 changed files with 132 additions and 123 deletions
+5 -5
View File
@@ -35,26 +35,26 @@ copy :: proc{copy_slice, copy_from_string};
@builtin @builtin
unordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) { unordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) #no_bounds_check {
bounds_check_error_loc(loc, index, len(array)); bounds_check_error_loc(loc, index, len(array));
n := len(array)-1; n := len(array)-1;
if index != n { if index != n {
array[index] = array[n]; array[index] = array[n];
} }
pop(array); (^Raw_Dynamic_Array)(array).len -= 1;
} }
@builtin @builtin
ordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) { ordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) #no_bounds_check {
bounds_check_error_loc(loc, index, len(array)); bounds_check_error_loc(loc, index, len(array));
if index+1 < len(array) { if index+1 < len(array) {
copy(array[index:], array[index+1:]); copy(array[index:], array[index+1:]);
} }
pop(array); (^Raw_Dynamic_Array)(array).len -= 1;
} }
@builtin @builtin
remove_range :: proc(array: ^$D/[dynamic]$T, lo, hi: int, loc := #caller_location) { remove_range :: proc(array: ^$D/[dynamic]$T, lo, hi: int, loc := #caller_location) #no_bounds_check {
slice_expr_error_lo_hi_loc(loc, lo, hi, len(array)); slice_expr_error_lo_hi_loc(loc, lo, hi, len(array));
n := max(hi-lo, 0); n := max(hi-lo, 0);
if n > 0 { if n > 0 {
+45 -28
View File
@@ -37,20 +37,9 @@ default_temp_allocator_destroy :: proc(s: ^Default_Temp_Allocator) {
s^ = {}; s^ = {};
} }
default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, @(private)
size, alignment: int, default_temp_allocator_alloc :: proc(s: ^Default_Temp_Allocator, size, alignment: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
s := (^Default_Temp_Allocator)(allocator_data);
if s.data == nil {
default_temp_allocator_init(s, DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE, default_allocator());
}
size := size; size := size;
switch mode {
case .Alloc:
size = align_forward_int(size, alignment); size = align_forward_int(size, alignment);
switch { switch {
@@ -98,10 +87,12 @@ default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
} }
return data, .None; return data, .None;
}
case .Free: @(private)
default_temp_allocator_free :: proc(s: ^Default_Temp_Allocator, old_memory: rawptr, loc := #caller_location) -> Allocator_Error {
if old_memory == nil { if old_memory == nil {
return nil, .None; return .None;
} }
start := uintptr(raw_data(s.data)); start := uintptr(raw_data(s.data));
@@ -111,12 +102,12 @@ default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
if s.prev_allocation == old_memory { if s.prev_allocation == old_memory {
s.curr_offset = int(uintptr(s.prev_allocation) - start); s.curr_offset = int(uintptr(s.prev_allocation) - start);
s.prev_allocation = nil; s.prev_allocation = nil;
return nil, .None; return .None;
} }
if start <= old_ptr && old_ptr < end { if start <= old_ptr && old_ptr < end {
// NOTE(bill): Cannot free this pointer but it is valid // NOTE(bill): Cannot free this pointer but it is valid
return nil, .None; return .None;
} }
if len(s.leaked_allocations) != 0 { if len(s.leaked_allocations) != 0 {
@@ -125,22 +116,26 @@ default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
if ptr == old_memory { if ptr == old_memory {
free(ptr, s.backup_allocator); free(ptr, s.backup_allocator);
ordered_remove(&s.leaked_allocations, i); ordered_remove(&s.leaked_allocations, i);
return nil, .None; return .None;
} }
} }
} }
return nil, .Invalid_Pointer; return .Invalid_Pointer;
// panic("invalid pointer passed to default_temp_allocator"); // panic("invalid pointer passed to default_temp_allocator");
}
case .Free_All: @(private)
default_temp_allocator_free_all :: proc(s: ^Default_Temp_Allocator, loc := #caller_location) {
s.curr_offset = 0; s.curr_offset = 0;
s.prev_allocation = nil; s.prev_allocation = nil;
for data in s.leaked_allocations { for data in s.leaked_allocations {
free(raw_data(data), s.backup_allocator); free(raw_data(data), s.backup_allocator);
} }
clear(&s.leaked_allocations); clear(&s.leaked_allocations);
}
case .Resize: @(private)
default_temp_allocator_resize :: proc(s: ^Default_Temp_Allocator, old_memory: rawptr, old_size, size, alignment: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
begin := uintptr(raw_data(s.data)); begin := uintptr(raw_data(s.data));
end := begin + uintptr(len(s.data)); end := begin + uintptr(len(s.data));
old_ptr := uintptr(old_memory); old_ptr := uintptr(old_memory);
@@ -150,25 +145,47 @@ default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
return byte_slice(old_memory, size), .None; return byte_slice(old_memory, size), .None;
} }
} }
ptr, err := default_temp_allocator_proc(allocator_data, .Alloc, size, alignment, old_memory, old_size, loc); data, err := default_temp_allocator_alloc(s, size, alignment, loc);
if err == .None { if err == .None {
copy(ptr, byte_slice(old_memory, old_size)); copy(data, byte_slice(old_memory, old_size));
_, err = default_temp_allocator_proc(allocator_data, .Free, 0, alignment, old_memory, old_size, loc); err = default_temp_allocator_free(s, old_memory, loc);
} }
return ptr, err; return data, err;
}
default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, loc := #caller_location) -> (data: []byte, err: Allocator_Error) {
s := (^Default_Temp_Allocator)(allocator_data);
if s.data == nil {
default_temp_allocator_init(s, DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE, default_allocator());
}
switch mode {
case .Alloc:
data, err = default_temp_allocator_alloc(s, size, alignment, loc);
case .Free:
err = default_temp_allocator_free(s, old_memory, loc);
case .Free_All:
default_temp_allocator_free_all(s, loc);
case .Resize:
data, err = default_temp_allocator_resize(s, old_memory, old_size, size, alignment, loc);
case .Query_Features: case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory); set := (^Allocator_Mode_Set)(old_memory);
if set != nil { if set != nil {
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features}; set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
} }
return nil, nil;
case .Query_Info: case .Query_Info:
return nil, .None; // Nothing to give
} }
return nil, .None; return;
} }
default_temp_allocator :: proc(allocator: ^Default_Temp_Allocator) -> Allocator { default_temp_allocator :: proc(allocator: ^Default_Temp_Allocator) -> Allocator {
-8
View File
@@ -22,7 +22,6 @@ bounds_check_error :: proc "contextless" (file: string, line, column: i32, index
return; return;
} }
handle_error :: proc "contextless" (file: string, line, column: i32, index, count: int) { handle_error :: proc "contextless" (file: string, line, column: i32, index, count: int) {
context = default_context();
print_caller_location(Source_Code_Location{file, line, column, ""}); print_caller_location(Source_Code_Location{file, line, column, ""});
print_string(" Index "); print_string(" Index ");
print_i64(i64(index)); print_i64(i64(index));
@@ -35,7 +34,6 @@ bounds_check_error :: proc "contextless" (file: string, line, column: i32, index
} }
slice_handle_error :: proc "contextless" (file: string, line, column: i32, lo, hi: int, len: int) -> ! { slice_handle_error :: proc "contextless" (file: string, line, column: i32, lo, hi: int, len: int) -> ! {
context = default_context();
print_caller_location(Source_Code_Location{file, line, column, ""}); print_caller_location(Source_Code_Location{file, line, column, ""});
print_string(" Invalid slice indices: "); print_string(" Invalid slice indices: ");
print_i64(i64(lo)); print_i64(i64(lo));
@@ -66,7 +64,6 @@ dynamic_array_expr_error :: proc "contextless" (file: string, line, column: i32,
return; return;
} }
handle_error :: proc "contextless" (file: string, line, column: i32, low, high, max: int) { handle_error :: proc "contextless" (file: string, line, column: i32, low, high, max: int) {
context = default_context();
print_caller_location(Source_Code_Location{file, line, column, ""}); print_caller_location(Source_Code_Location{file, line, column, ""});
print_string(" Invalid dynamic array values: "); print_string(" Invalid dynamic array values: ");
print_i64(i64(low)); print_i64(i64(low));
@@ -86,7 +83,6 @@ type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column
return; return;
} }
handle_error :: proc "contextless" (file: string, line, column: i32, from, to: typeid) { handle_error :: proc "contextless" (file: string, line, column: i32, from, to: typeid) {
context = default_context();
print_caller_location(Source_Code_Location{file, line, column, ""}); print_caller_location(Source_Code_Location{file, line, column, ""});
print_string(" Invalid type assertion from "); print_string(" Invalid type assertion from ");
print_typeid(from); print_typeid(from);
@@ -131,7 +127,6 @@ type_assertion_check2 :: proc "contextless" (ok: bool, file: string, line, colum
} }
handle_error :: proc "contextless" (file: string, line, column: i32, from, to: typeid, from_data: rawptr) { handle_error :: proc "contextless" (file: string, line, column: i32, from, to: typeid, from_data: rawptr) {
context = default_context();
actual := variant_type(from, from_data); actual := variant_type(from, from_data);
@@ -156,7 +151,6 @@ make_slice_error_loc :: #force_inline proc "contextless" (loc := #caller_locatio
return; return;
} }
handle_error :: proc "contextless" (loc: Source_Code_Location, len: int) { handle_error :: proc "contextless" (loc: Source_Code_Location, len: int) {
context = default_context();
print_caller_location(loc); print_caller_location(loc);
print_string(" Invalid slice length for make: "); print_string(" Invalid slice length for make: ");
print_i64(i64(len)); print_i64(i64(len));
@@ -171,7 +165,6 @@ make_dynamic_array_error_loc :: #force_inline proc "contextless" (using loc := #
return; return;
} }
handle_error :: proc "contextless" (loc: Source_Code_Location, len, cap: int) { handle_error :: proc "contextless" (loc: Source_Code_Location, len, cap: int) {
context = default_context();
print_caller_location(loc); print_caller_location(loc);
print_string(" Invalid dynamic array parameters for make: "); print_string(" Invalid dynamic array parameters for make: ");
print_i64(i64(len)); print_i64(i64(len));
@@ -188,7 +181,6 @@ make_map_expr_error_loc :: #force_inline proc "contextless" (loc := #caller_loca
return; return;
} }
handle_error :: proc "contextless" (loc: Source_Code_Location, cap: int) { handle_error :: proc "contextless" (loc: Source_Code_Location, cap: int) {
context = default_context();
print_caller_location(loc); print_caller_location(loc);
print_string(" Invalid map capacity for make: "); print_string(" Invalid map capacity for make: ");
print_i64(i64(cap)); print_i64(i64(cap));
+1 -1
View File
@@ -24,7 +24,7 @@ foreign kernel32 {
HeapFree :: proc(hHeap: rawptr, dwFlags: u32, lpMem: rawptr) -> b32 --- HeapFree :: proc(hHeap: rawptr, dwFlags: u32, lpMem: rawptr) -> b32 ---
} }
_os_write :: proc "contextless" (data: []byte) -> (n: int, err: _OS_Errno) { _os_write :: proc "contextless" (data: []byte) -> (n: int, err: _OS_Errno) #no_bounds_check {
if len(data) == 0 { if len(data) == 0 {
return 0, 0; return 0, 0;
} }
+3 -3
View File
@@ -86,7 +86,7 @@ print_encoded_rune :: proc "contextless" (r: rune) {
print_byte('\''); print_byte('\'');
} }
print_rune :: proc "contextless" (r: rune) -> (int, _OS_Errno) { print_rune :: proc "contextless" (r: rune) -> (int, _OS_Errno) #no_bounds_check {
RUNE_SELF :: 0x80; RUNE_SELF :: 0x80;
if r < RUNE_SELF { if r < RUNE_SELF {
@@ -98,7 +98,7 @@ print_rune :: proc "contextless" (r: rune) -> (int, _OS_Errno) {
} }
print_u64 :: proc "contextless" (x: u64) { print_u64 :: proc "contextless" (x: u64) #no_bounds_check {
digits := _INTEGER_DIGITS; digits := _INTEGER_DIGITS;
a: [129]byte; a: [129]byte;
@@ -115,7 +115,7 @@ print_u64 :: proc "contextless" (x: u64) {
} }
print_i64 :: proc "contextless" (x: i64) { print_i64 :: proc "contextless" (x: i64) #no_bounds_check {
digits := _INTEGER_DIGITS; digits := _INTEGER_DIGITS;
b :: i64(10); b :: i64(10);