File Library and TypeDecl syntax change

This commit is contained in:
gingerBill
2016-08-22 11:52:49 +01:00
parent 680274b6f1
commit a98e93f03f
15 changed files with 515 additions and 168 deletions
+208 -22
View File
@@ -1,17 +1,203 @@
putchar :: proc(c: i32) -> i32 #foreign
mem_compare :: proc(dst, src : rawptr, len: int) -> i32 #foreign "memcmp"
mem_copy :: proc(dst, src : rawptr, len: int) -> i32 #foreign "memcpy"
mem_move :: proc(dst, src : rawptr, len: int) -> i32 #foreign "memmove"
debug_trap :: proc() #foreign "llvm.debugtrap"
// TODO(bill): make custom heap procedures
heap_realloc :: proc(ptr: rawptr, sz: int) -> rawptr #foreign "realloc"
heap_alloc :: proc(sz: int) -> rawptr { return heap_realloc(null, sz); }
heap_free :: proc(ptr: rawptr) { _ = heap_realloc(ptr, 0); }
heap_alloc :: proc(len: int) -> rawptr {
return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
}
heap_free :: proc(ptr: rawptr) {
_ = HeapFree(GetProcessHeap(), 0, ptr);
}
memory_compare :: proc(dst, src: rawptr, len: int) -> int {
s1, s2: ^u8 = dst, src;
for i := 0; i < len; i++ {
if s1[i] != s2[i] {
return (s1[i] - s2[i]) as int;
}
}
return 0;
}
memory_copy :: proc(dst, src: rawptr, n: int) #inline {
if dst == src {
return;
}
v128b :: type {4}u32;
static_assert(align_of(v128b) == 16);
d, s: ^u8 = dst, src;
for ; s as uint % 16 != 0 && n != 0; n-- {
d[0] = s[0];
d, s = ^d[1], ^s[1];
}
if d as uint % 16 == 0 {
for ; n >= 16; d, s, n = ^d[16], ^s[16], n-16 {
(d as ^v128b)[0] = (s as ^v128b)[0];
}
if n&8 != 0 {
(d as ^u64)[0] = (s as ^u64)[0];
d, s = ^d[8], ^s[8];
}
if n&4 != 0 {
(d as ^u32)[0] = (s as ^u32)[0];
d, s = ^d[4], ^s[4];
}
if n&2 != 0 {
(d as ^u16)[0] = (s as ^u16)[0];
d, s = ^d[2], ^s[2];
}
if n&1 != 0 {
d[0] = s[0];
d, s = ^d[1], ^s[1];
}
return;
}
// IMPORTANT NOTE(bill): Little endian only
LS :: proc(a, b: u32) -> u32 #inline { return a << b; }
RS :: proc(a, b: u32) -> u32 #inline { return a >> b; }
/* NOTE(bill): Big endian version
LS :: proc(a, b: u32) -> u32 #inline { return a >> b; }
RS :: proc(a, b: u32) -> u32 #inline { return a << b; }
*/
w, x: u32;
if d as uint % 4 == 1 {
w = (s as ^u32)^;
d[0] = s[0];
d[1] = s[1];
d[2] = s[2];
d, s, n = ^d[3], ^s[3], n-3;
for n > 16 {
d32 := d as ^u32;
x = (^s[1] as ^u32)^; d32[0] = LS(w, 24) | RS(x, 8);
w = (^s[5] as ^u32)^; d32[1] = LS(x, 24) | RS(w, 8);
x = (^s[9] as ^u32)^; d32[2] = LS(w, 24) | RS(x, 8);
w = (^s[13] as ^u32)^; d32[3] = LS(x, 24) | RS(w, 8);
d, s, n = ^d[16], ^s[16], n-16;
}
} else if d as uint % 4 == 2 {
w = (s as ^u32)^;
d[0] = s[0];
d[1] = s[1];
d, s, n = ^d[2], ^s[2], n-2;
for n > 17 {
d32 := d as ^u32;
x = (^s[2] as ^u32)^; d32[0] = LS(w, 16) | RS(x, 16);
w = (^s[6] as ^u32)^; d32[1] = LS(x, 16) | RS(w, 16);
x = (^s[10] as ^u32)^; d32[2] = LS(w, 16) | RS(x, 16);
w = (^s[14] as ^u32)^; d32[3] = LS(x, 16) | RS(w, 16);
d, s, n = ^d[16], ^s[16], n-16;
}
} else if d as uint % 4 == 3 {
w = (s as ^u32)^;
d[0] = s[0];
d, s, n = ^d[1], ^s[1], n-1;
for n > 18 {
d32 := d as ^u32;
x = (^s[3] as ^u32)^; d32[0] = LS(w, 8) | RS(x, 24);
w = (^s[7] as ^u32)^; d32[1] = LS(x, 8) | RS(w, 24);
x = (^s[11] as ^u32)^; d32[2] = LS(w, 8) | RS(x, 24);
w = (^s[15] as ^u32)^; d32[3] = LS(x, 8) | RS(w, 24);
d, s, n = ^d[16], ^s[16], n-16;
}
}
if n&16 != 0 {
(d as ^v128b)[0] = (s as ^v128b)[0];
d, s = ^d[16], ^s[16];
}
if n&8 != 0 {
(d as ^u64)[0] = (s as ^u64)[0];
d, s = ^d[8], ^s[8];
}
if n&4 != 0 {
(d as ^u32)[0] = (s as ^u32)[0];
d, s = ^d[4], ^s[4];
}
if n&2 != 0 {
(d as ^u16)[0] = (s as ^u16)[0];
d, s = ^d[2], ^s[2];
}
if n&1 != 0 {
d[0] = s[0];
}
}
memory_move :: proc(dst, src: rawptr, n: int) #inline {
d, s: ^u8 = dst, src;
if d == s {
return;
}
if d >= ^s[n] || ^d[n] <= s {
memory_copy(d, s, n);
return;
}
// TODO(bill): Vectorize the shit out of this
if d < s {
if s as int % size_of(int) == d as int % size_of(int) {
for d as int % size_of(int) != 0 {
if n == 0 {
return;
}
n--;
d[0] = s[0];
d, s = ^d[1], ^s[1];
}
di, si := d as ^int, s as ^int;
for n >= size_of(int) {
di[0] = si[0];
di, si = ^di[1], ^si[1];
n -= size_of(int);
}
}
for ; n > 0; n-- {
d[0] = s[0];
d, s = ^d[1], ^s[1];
}
} else {
if s as int % size_of(int) == d as int % size_of(int) {
for ^d[n] as int % size_of(int) != 0 {
if n == 0 {
return;
}
n--;
d[0] = s[0];
d, s = ^d[1], ^s[1];
}
for n >= size_of(int) {
n -= size_of(int);
di, si := ^d[n] as ^int, ^s[n] as ^int;
di[0] = si[0];
}
for ; n > 0; n-- {
d[0] = s[0];
d, s = ^d[1], ^s[1];
}
}
for n > 0 {
n--;
d[n] = s[n];
}
}
}
__string_eq :: proc(a, b : string) -> bool {
if len(a) != len(b) {
return false;
@@ -19,10 +205,10 @@ __string_eq :: proc(a, b : string) -> bool {
if ^a[0] == ^b[0] {
return true;
}
return mem_compare(^a[0], ^b[0], len(a)) == 0;
return memory_compare(^a[0], ^b[0], len(a)) == 0;
}
__string_ne :: proc(a, b : string) -> bool {
__string_ne :: proc(a, b : string) -> bool #inline {
return !__string_eq(a, b);
}
@@ -48,30 +234,30 @@ __string_cmp :: proc(a, b : string) -> int {
return 0;
}
__string_lt :: proc(a, b : string) -> bool { return __string_cmp(a, b) < 0; }
__string_gt :: proc(a, b : string) -> bool { return __string_cmp(a, b) > 0; }
__string_le :: proc(a, b : string) -> bool { return __string_cmp(a, b) <= 0; }
__string_ge :: proc(a, b : string) -> bool { return __string_cmp(a, b) >= 0; }
__string_lt :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) < 0; }
__string_gt :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) > 0; }
__string_le :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) <= 0; }
__string_ge :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) >= 0; }
type AllocationMode: int;
AllocationMode :: type int;
ALLOCATION_ALLOC :: 0;
ALLOCATION_DEALLOC :: 1;
ALLOCATION_DEALLOC_ALL :: 2;
ALLOCATION_RESIZE :: 3;
type AllocatorProc: proc(allocator_data: rawptr, mode: AllocationMode,
size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64) -> rawptr;
AllocatorProc :: type proc(allocator_data: rawptr, mode: AllocationMode,
size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64) -> rawptr;
type Allocator: struct {
Allocator :: type struct {
procedure: AllocatorProc,
data: rawptr,
}
type Context: struct {
Context :: type struct {
thread_id: i32,
user_index: i32,
@@ -157,7 +343,7 @@ __default_allocator_proc :: proc(allocator_data: rawptr, mode: AllocationMode,
if mode == ALLOCATION_ALLOC {
return heap_alloc(size);
} else if mode == ALLOCATION_RESIZE {
return heap_realloc(old_memory, size);
return default_resize_align(old_memory, old_size, size, alignment);
} else if mode == ALLOCATION_DEALLOC {
heap_free(old_memory);
} else if mode == ALLOCATION_DEALLOC_ALL {