Reorganize runtime package

This commit is contained in:
gingerBill
2018-05-27 21:22:25 +01:00
parent a5763d6fee
commit 5c52ffe24e
21 changed files with 908 additions and 856 deletions
+1 -1
View File
@@ -38,7 +38,7 @@ string_buffer_from_slice :: proc(backing: []byte) -> String_Buffer {
data = s.data,
len = 0,
cap = s.len,
allocator = nil_allocator(),
allocator = mem.nil_allocator(),
};
return transmute(String_Buffer)d;
}
+1 -1
View File
@@ -64,7 +64,7 @@ murmur32 :: proc(data: []byte) -> u32 {
c2_32: u32 : 0x1b873593;
h1: u32 = 0;
nblocks := len(data)/4;
nblocks := uintptr(len(data)/4);
p := &data[0];
p1 := mem.ptr_offset(p, 4*nblocks);
+115
View File
@@ -0,0 +1,115 @@
package mem
import "core:raw"
import "core:os"
alloc :: inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr {
a := context.allocator;
return a.procedure(a.data, Allocator_Mode.Alloc, size, alignment, nil, 0, 0, loc);
}
free_ptr_with_allocator :: inline proc(a: Allocator, ptr: rawptr, loc := #caller_location) {
if ptr == nil do return;
if a.procedure == nil do return;
a.procedure(a.data, Allocator_Mode.Free, 0, 0, ptr, 0, 0, loc);
}
free_ptr :: inline proc(ptr: rawptr, loc := #caller_location) do free_ptr_with_allocator(context.allocator, ptr);
free_all :: inline proc(loc := #caller_location) {
a := context.allocator;
a.procedure(a.data, Allocator_Mode.Free_All, 0, 0, nil, 0, 0, loc);
}
resize :: inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr {
a := context.allocator;
return a.procedure(a.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, 0, loc);
}
free_string :: proc(str: string, loc := #caller_location) {
free_ptr(raw.data(str), loc);
}
free_cstring :: proc(str: cstring, loc := #caller_location) {
free_ptr((^byte)(str), loc);
}
free_dynamic_array :: proc(array: $T/[dynamic]$E, loc := #caller_location) {
free_ptr(raw.data(array), loc);
}
free_slice :: proc(array: $T/[]$E, loc := #caller_location) {
free_ptr(raw.data(array), loc);
}
free_map :: proc(m: $T/map[$K]$V, loc := #caller_location) {
raw := transmute(raw.Map)m;
free_dynamic_array(raw.hashes, loc);
free_ptr(raw.entries.data, loc);
}
default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: int, loc := #caller_location) -> rawptr {
if old_memory == nil do return alloc(new_size, alignment, loc);
if new_size == 0 {
free(old_memory, loc);
return nil;
}
if new_size == old_size do return old_memory;
new_memory := alloc(new_size, alignment, loc);
if new_memory == nil do return nil;
__mem_copy(new_memory, old_memory, min(old_size, new_size));;
free(old_memory, loc);
return new_memory;
}
default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
using Allocator_Mode;
switch mode {
case Alloc:
return os.heap_alloc(size);
case Free:
os.heap_free(old_memory);
return nil;
case Free_All:
// NOTE(bill): Does nothing
case Resize:
ptr := os.heap_resize(old_memory, size);
assert(ptr != nil);
return ptr;
}
return nil;
}
default_allocator :: proc() -> Allocator {
return Allocator{
procedure = default_allocator_proc,
data = nil,
};
}
nil_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
return nil;
}
nil_allocator :: proc() -> Allocator {
return Allocator{
procedure = nil_allocator_proc,
data = nil,
};
}
+24 -5
View File
@@ -23,12 +23,19 @@ copy_non_overlapping :: proc "contextless" (dst, src: rawptr, len: int) -> rawpt
return __mem_copy_non_overlapping(dst, src, len);
}
compare :: proc "contextless" (a, b: []byte) -> int {
return __mem_compare(&a[0], &b[0], min(len(a), len(b)));
return compare_byte_ptrs(&a[0], &b[0], min(len(a), len(b)));
}
compare_byte_ptrs :: proc "contextless" (a, b: ^byte, n: int) -> int {
pa :: ptr_offset;
for i in 0..uintptr(n) do switch {
case pa(a, i)^ < pa(b, i)^: return -1;
case pa(a, i)^ > pa(b, i)^: return +1;
}
return 0;
}
ptr_offset :: proc "contextless" (ptr: $P/^$T, n: int) -> P {
new := uintptr(ptr) + uintptr(size_of(T)*n);
ptr_offset :: proc "contextless" (ptr: $P/^$T, n: uintptr) -> P {
new := uintptr(ptr) + size_of(T)*n;
return P(new);
}
@@ -48,6 +55,18 @@ slice_to_bytes :: proc "contextless" (slice: $E/[]$T) -> []byte {
return transmute([]byte)s;
}
buffer_from_slice :: proc(backing: $T/[]$E) -> [dynamic]E {
s := transmute(raw.Slice)backing;
d := raw.Dynamic_Array{
data = s.data,
len = 0,
cap = s.len,
allocator = nil_allocator(),
};
return transmute([dynamic]E)d;
}
ptr_to_bytes :: proc "contextless" (ptr: ^$T, len := 1) -> []byte {
assert(len >= 0);
return transmute([]byte)raw.Slice{ptr, len*size_of(T)};
@@ -89,7 +108,7 @@ allocation_header_fill :: proc(header: ^AllocationHeader, data: rawptr, size: in
ptr := cast(^uint)(ptr_offset(header, 1));
n := ptr_sub(cast(^uint)data, ptr);
for i in 0..n {
for i in 0..uintptr(n) {
ptr_offset(ptr, i)^ = ~uint(0);
}
}
+5 -3
View File
@@ -1,6 +1,6 @@
package os
import "core:mem"
import "core:raw"
write_string :: proc(fd: Handle, str: string) -> (int, Errno) {
return write(fd, cast([]byte)str);
@@ -56,9 +56,11 @@ write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (succ
}
write_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) {
return write(fd, mem.slice_ptr(cast(^byte)data, len));
s := transmute([]byte)raw.Slice{data, len};
return write(fd, s);
}
read_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) {
return read(fd, mem.slice_ptr(cast(^byte)data, len));
s := transmute([]byte)raw.Slice{data, len};
return read(fd, s);
}
-1
View File
@@ -4,7 +4,6 @@ foreign import dl "system:dl"
foreign import libc "system:c"
import "core:strings"
import "core:mem"
OS :: "linux";
-1
View File
@@ -4,7 +4,6 @@ foreign import dl "system:dl"
foreign import libc "system:c"
import "core:strings"
import "core:mem"
OS :: "osx";
+1 -2
View File
@@ -1,7 +1,6 @@
package os
import "core:sys/win32"
import "core:mem"
OS :: "windows";
@@ -269,7 +268,7 @@ _alloc_command_line_arguments :: proc() -> []string {
arg_list_ptr := win32.command_line_to_argv_w(win32.get_command_line_w(), &arg_count);
arg_list := make([]string, int(arg_count));
for _, i in arg_list {
wc_str := mem.ptr_offset(arg_list_ptr, i)^;
wc_str := (^win32.Wstring)(uintptr(arg_list_ptr) + size_of(win32.Wstring)*uintptr(i))^;
olen := win32.wide_char_to_multi_byte(win32.CP_UTF8, 0, wc_str, -1,
nil, 0, nil, nil);
@@ -1,4 +1,4 @@
package builtin
package runtime
import "core:os"
import "core:unicode/utf8"
@@ -146,8 +146,6 @@ Source_Code_Location :: struct {
procedure: string,
}
Allocator_Mode :: enum byte {
Alloc,
Free,
@@ -167,8 +165,9 @@ Allocator :: struct {
}
Context :: struct {
allocator: Allocator,
allocator: mem.Allocator,
thread_id: int,
user_data: any,
@@ -180,6 +179,9 @@ Context :: struct {
DEFAULT_ALIGNMENT :: 2*align_of(rawptr);
__INITIAL_MAP_CAP :: 16;
__Map_Key :: struct {
@@ -212,6 +214,9 @@ __Map_Header :: struct {
type_info_base :: proc "contextless" (info: ^Type_Info) -> ^Type_Info {
if info == nil do return nil;
@@ -293,7 +298,7 @@ __init_context :: proc "contextless" (c: ^Context) {
if c == nil do return;
if c.allocator.procedure == nil {
c.allocator = default_allocator();
c.allocator = mem.default_allocator();
}
if c.thread_id == 0 {
c.thread_id = os.current_thread_id();
@@ -302,30 +307,6 @@ __init_context :: proc "contextless" (c: ^Context) {
alloc :: inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr {
a := context.allocator;
return a.procedure(a.data, Allocator_Mode.Alloc, size, alignment, nil, 0, 0, loc);
}
free_ptr_with_allocator :: inline proc(a: Allocator, ptr: rawptr, loc := #caller_location) {
if ptr == nil do return;
if a.procedure == nil do return;
a.procedure(a.data, Allocator_Mode.Free, 0, 0, ptr, 0, 0, loc);
}
free_ptr :: inline proc(ptr: rawptr, loc := #caller_location) do free_ptr_with_allocator(context.allocator, ptr);
free_all :: inline proc(loc := #caller_location) {
a := context.allocator;
a.procedure(a.data, Allocator_Mode.Free_All, 0, 0, nil, 0, 0, loc);
}
resize :: inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr {
a := context.allocator;
return a.procedure(a.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, 0, loc);
}
copy :: proc "contextless" (dst, src: $T/[]$E) -> int {
n := max(0, min(len(dst), len(src)));
@@ -334,6 +315,65 @@ copy :: proc "contextless" (dst, src: $T/[]$E) -> int {
}
pop :: proc "contextless" (array: ^$T/[dynamic]$E) -> E {
if array == nil do return E{};
assert(len(array) > 0);
res := array[len(array)-1];
(^raw.Dynamic_Array)(array).len -= 1;
return res;
}
clear :: proc[clear_dynamic_array, clear_map];
reserve :: proc[reserve_dynamic_array, reserve_map];
new :: inline proc(T: type, loc := #caller_location) -> ^T {
ptr := (^T)(mem.alloc(size_of(T), align_of(T), loc));
ptr^ = T{};
return ptr;
}
new_clone :: inline proc(data: $T, loc := #caller_location) -> ^T {
ptr := (^T)(mem.alloc(size_of(T), align_of(T), loc));
ptr^ = data;
return ptr;
}
free :: proc[
mem.free_ptr,
mem.free_string,
mem.free_cstring,
mem.free_dynamic_array,
mem.free_slice,
mem.free_map,
];
clear_map :: inline proc "contextless" (m: ^$T/map[$K]$V) {
if m == nil do return;
raw_map := (^raw.Map)(m);
hashes := (^raw.Dynamic_Array)(&raw_map.hashes);
entries := (^raw.Dynamic_Array)(&raw_map.entries);
hashes.len = 0;
entries.len = 0;
}
reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int) {
if m != nil do __dynamic_map_reserve(__get_map_header(m), capacity);
}
delete :: proc(m: ^$T/map[$K]$V, key: K) {
if m != nil do __dynamic_map_delete(__get_map_header(m), __get_map_key(key));
}
append :: proc(array: ^$T/[dynamic]$E, args: ...E, loc := #caller_location) -> int {
if array == nil do return 0;
@@ -350,7 +390,7 @@ append :: proc(array: ^$T/[dynamic]$E, args: ...E, loc := #caller_location) -> i
a := (^raw.Dynamic_Array)(array);
data := (^E)(a.data);
assert(data != nil);
__mem_copy(mem.ptr_offset(data, a.len), &args[0], size_of(E) * arg_len);
__mem_copy(mem.ptr_offset(data, uintptr(a.len)), &args[0], size_of(E) * arg_len);
a.len += arg_len;
}
return len(array);
@@ -363,27 +403,9 @@ append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ...string, loc := #caller
return len(array);
}
pop :: proc "contextless" (array: ^$T/[dynamic]$E) -> E {
if array == nil do return E{};
assert(len(array) > 0);
res := array[len(array)-1];
(^raw.Dynamic_Array)(array).len -= 1;
return res;
}
clear_dynamic_array :: inline proc "contextless" (array: ^$T/[dynamic]$E) {
if array != nil do (^raw.Dynamic_Array)(array).len = 0;
}
clear_map :: inline proc "contextless" (m: ^$T/map[$K]$V) {
if m == nil do return;
raw_map := (^raw.Map)(m);
hashes := (^raw.Dynamic_Array)(&raw_map.hashes);
entries := (^raw.Dynamic_Array)(&raw_map.entries);
hashes.len = 0;
entries.len = 0;
}
clear :: proc[clear_dynamic_array, clear_map];
reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #caller_location) -> bool {
if array == nil do return false;
@@ -412,409 +434,9 @@ reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #cal
}
__get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> __Map_Header {
header := __Map_Header{m = (^raw.Map)(m)};
Entry :: struct {
key: __Map_Key,
next: int,
value: V,
}
_, is_string := type_info_base(type_info_of(K)).variant.(Type_Info_String);
header.is_key_string = is_string;
header.entry_size = int(size_of(Entry));
header.entry_align = int(align_of(Entry));
header.value_offset = uintptr(offset_of(Entry, value));
header.value_size = int(size_of(V));
return header;
}
__get_map_key :: proc "contextless" (key: $K) -> __Map_Key {
map_key: __Map_Key;
ti := type_info_base_without_enum(type_info_of(K));
switch _ in ti.variant {
case Type_Info_Integer:
switch 8*size_of(key) {
case 8: map_key.hash = u64(( ^u8)(&key)^);
case 16: map_key.hash = u64(( ^u16)(&key)^);
case 32: map_key.hash = u64(( ^u32)(&key)^);
case 64: map_key.hash = u64(( ^u64)(&key)^);
case: panic("Unhandled integer size");
}
case Type_Info_Rune:
map_key.hash = u64((^rune)(&key)^);
case Type_Info_Pointer:
map_key.hash = u64(uintptr((^rawptr)(&key)^));
case Type_Info_Float:
switch 8*size_of(key) {
case 32: map_key.hash = u64((^u32)(&key)^);
case 64: map_key.hash = u64((^u64)(&key)^);
case: panic("Unhandled float size");
}
case Type_Info_String:
str := (^string)(&key)^;
map_key.hash = __default_hash_string(str);
map_key.str = str;
case:
panic("Unhandled map key type");
}
return map_key;
}
reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int) {
if m != nil do __dynamic_map_reserve(__get_map_header(m), capacity);
}
delete :: proc(m: ^$T/map[$K]$V, key: K) {
if m != nil do __dynamic_map_delete(__get_map_header(m), __get_map_key(key));
}
reserve :: proc[reserve_dynamic_array, reserve_map];
new :: inline proc(T: type, loc := #caller_location) -> ^T {
ptr := (^T)(alloc(size_of(T), align_of(T), loc));
ptr^ = T{};
return ptr;
}
new_clone :: inline proc(data: $T, loc := #caller_location) -> ^T {
ptr := (^T)(alloc(size_of(T), align_of(T), loc));
ptr^ = data;
return ptr;
}
free_string :: proc(str: string, loc := #caller_location) {
free_ptr(raw.data(str), loc);
}
free_cstring :: proc(str: cstring, loc := #caller_location) {
free_ptr((^byte)(str), loc);
}
free_dynamic_array :: proc(array: $T/[dynamic]$E, loc := #caller_location) {
free_ptr(raw.data(array), loc);
}
free_slice :: proc(array: $T/[]$E, loc := #caller_location) {
free_ptr(raw.data(array), loc);
}
free_map :: proc(m: $T/map[$K]$V, loc := #caller_location) {
raw := transmute(raw.Map)m;
free_dynamic_array(raw.hashes, loc);
free_ptr(raw.entries.data, loc);
}
free :: proc[
free_ptr,
free_string, free_cstring,
free_dynamic_array, free_slice, free_map,
];
// NOTE(bill): This code works but I will prefer having `make` a built-in procedure
// to have better error messages
/*
make :: proc(T: type/[]$E, len: int, using loc := #caller_location) -> T {
cap := len;
__slice_expr_error(file_path, int(line), int(column), 0, len, cap);
data := cast(^E)alloc(len * size_of(E), align_of(E));
for i in 0..len do (data+i)^ = E{};
s := raw.Slice{data = data, len = len};
return (cast(^T)&s)^;
}
make :: proc(T: type/[dynamic]$E, len: int = 8, using loc := #caller_location) -> T {
cap := len;
__slice_expr_error(file_path, int(line), int(column), 0, len, cap);
data := cast(^E)alloc(cap * size_of(E), align_of(E));
for i in 0..len do (data+i)^ = E{};
s := raw.Dynamic_Array{data = data, len = len, cap = cap, allocator = context.allocator};
return (cast(^T)&s)^;
}
make :: proc(T: type/[dynamic]$E, len, cap: int, using loc := #caller_location) -> T {
__slice_expr_error(file_path, int(line), int(column), 0, len, cap);
data := cast(^E)alloc(cap * size_of(E), align_of(E));
for i in 0..len do (data+i)^ = E{};
s := raw.Dynamic_Array{data = data, len = len, cap = cap, allocator = context.allocator};
return (cast(^T)&s)^;
}
make :: proc(T: type/map[$K]$V, cap: int = 16, using loc := #caller_location) -> T {
if cap < 0 do cap = 16;
m: T;
header := __get_map_header(&m);
__dynamic_map_reserve(header, cap);
return m;
}
*/
default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: int, loc := #caller_location) -> rawptr {
if old_memory == nil do return alloc(new_size, alignment, loc);
if new_size == 0 {
free(old_memory, loc);
return nil;
}
if new_size == old_size do return old_memory;
new_memory := alloc(new_size, alignment, loc);
if new_memory == nil do return nil;
__mem_copy(new_memory, old_memory, min(old_size, new_size));;
free(old_memory, loc);
return new_memory;
}
default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
using Allocator_Mode;
switch mode {
case Alloc:
return os.heap_alloc(size);
case Free:
os.heap_free(old_memory);
return nil;
case Free_All:
// NOTE(bill): Does nothing
case Resize:
ptr := os.heap_resize(old_memory, size);
assert(ptr != nil);
return ptr;
}
return nil;
}
default_allocator :: proc() -> Allocator {
return Allocator{
procedure = default_allocator_proc,
data = nil,
};
}
nil_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
return nil;
}
nil_allocator :: proc() -> Allocator {
return Allocator{
procedure = nil_allocator_proc,
data = nil,
};
}
__print_u64 :: proc(fd: os.Handle, u: u64) {
digits := "0123456789";
a: [129]byte;
i := len(a);
b := u64(10);
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, u: i64) {
digits := "0123456789";
neg := u < 0;
u = abs(u);
a: [129]byte;
i := len(a);
b := i64(10);
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) {
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:
a := any{typeid = typeid_of(ti)};
switch _ in a {
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, info.signed ? 'i' : '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_String:
os.write_string(fd, "string");
case Type_Info_Boolean:
a := any{typeid = typeid_of(ti)};
switch _ in a {
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_string(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_string(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_string(fd, "[");
__print_u64(fd, u64(info.count));
os.write_string(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:
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 {");
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, "}");
}
}
assert :: proc "contextless" (condition: bool, message := "", using loc := #caller_location) -> bool {
if !condition {
@@ -844,262 +466,8 @@ panic :: proc "contextless" (message := "", using loc := #caller_location) {
}
buffer_from_slice :: proc(backing: $T/[]$E) -> [dynamic]E {
s := transmute(raw.Slice)backing;
d := raw.Dynamic_Array{
data = s.data,
len = 0,
cap = s.len,
allocator = nil_allocator(),
};
return transmute([dynamic]E)d;
}
__string_eq :: proc "contextless" (a, b: string) -> bool {
switch {
case len(a) != len(b): return false;
case len(a) == 0: return true;
case &a[0] == &b[0]: return true;
}
return __string_cmp(a, b) == 0;
}
__string_cmp :: proc "contextless" (a, b: string) -> int {
return __mem_compare(&a[0], &b[0], min(len(a), len(b)));
}
__string_ne :: inline proc "contextless" (a, b: string) -> bool { return !__string_eq(a, b); }
__string_lt :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) < 0; }
__string_gt :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) > 0; }
__string_le :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) <= 0; }
__string_ge :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) >= 0; }
__cstring_len :: proc "contextless" (s: cstring) -> int {
n := 0;
for p := (^byte)(s); p != nil && p^ != 0; p = mem.ptr_offset(p, 1) {
n += 1;
}
return n;
}
__cstring_to_string :: proc "contextless" (s: cstring) -> string {
if s == nil do return "";
ptr := (^byte)(s);
n := __cstring_len(s);
return transmute(string)raw.String{ptr, n};
}
__complex64_eq :: inline proc "contextless" (a, b: complex64) -> bool { return real(a) == real(b) && imag(a) == imag(b); }
__complex64_ne :: inline proc "contextless" (a, b: complex64) -> bool { return real(a) != real(b) || imag(a) != imag(b); }
__complex128_eq :: 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) {
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();
}
__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();
}
__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();
}
__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();
}
__string_decode_rune :: inline proc "contextless" (s: string) -> (rune, int) {
return utf8.decode_rune_from_string(s);
}
__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_loc :: inline proc "contextless" (using loc := #caller_location, lo, hi: int, len: int) {
__slice_expr_error(file_path, int(line), int(column), lo, hi, len);
}
__mem_set :: proc "contextless" (data: rawptr, value: i32, len: int) -> rawptr {
if data == nil do return nil;
foreign __llvm_core {
when size_of(rawptr) == 8 {
@(link_name="llvm.memset.p0i8.i64")
llvm_memset :: proc(dst: rawptr, val: byte, len: int, align: i32, is_volatile: bool) ---;
} else {
@(link_name="llvm.memset.p0i8.i32")
llvm_memset :: proc(dst: rawptr, val: byte, len: int, align: i32, is_volatile: bool) ---;
}
}
llvm_memset(data, byte(value), len, 1, false);
return data;
}
__mem_zero :: proc "contextless" (data: rawptr, len: int) -> rawptr {
return __mem_set(data, 0, len);
}
__mem_copy :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
if src == nil do return dst;
// NOTE(bill): This _must_ be implemented like C's memmove
foreign __llvm_core {
when size_of(rawptr) == 8 {
@(link_name="llvm.memmove.p0i8.p0i8.i64")
llvm_memmove :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) ---;
} else {
@(link_name="llvm.memmove.p0i8.p0i8.i32")
llvm_memmove :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) ---;
}
}
llvm_memmove(dst, src, len, 1, false);
return dst;
}
__mem_copy_non_overlapping :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
if src == nil do return dst;
// NOTE(bill): This _must_ be implemented like C's memcpy
foreign __llvm_core {
when size_of(rawptr) == 8 {
@(link_name="llvm.memcpy.p0i8.p0i8.i64")
llvm_memcpy :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) ---;
} else {
@(link_name="llvm.memcpy.p0i8.p0i8.i32")
llvm_memcpy :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) ---;
}
}
llvm_memcpy(dst, src, len, 1, false);
return dst;
}
__mem_compare :: proc "contextless" (a, b: ^byte, n: int) -> int {
pa :: mem.ptr_offset;
for i in 0..n do switch {
case pa(a, i)^ < pa(b, i)^: return -1;
case pa(a, i)^ > pa(b, i)^: return +1;
}
return 0;
}
@(default_calling_convention = "c")
foreign __llvm_core {
@(link_name="llvm.sqrt.f32") __sqrt_f32 :: proc(x: f32) -> f32 ---;
@(link_name="llvm.sqrt.f64") __sqrt_f64 :: proc(x: f64) -> f64 ---;
@(link_name="llvm.sin.f32") __sin_f32 :: proc(θ: f32) -> f32 ---;
@(link_name="llvm.sin.f64") __sin_f64 :: proc(θ: f64) -> f64 ---;
@(link_name="llvm.cos.f32") __cos_f32 :: proc(θ: f32) -> f32 ---;
@(link_name="llvm.cos.f64") __cos_f64 :: proc(θ: f64) -> f64 ---;
@(link_name="llvm.pow.f32") __pow_f32 :: proc(x, power: f32) -> f32 ---;
@(link_name="llvm.pow.f64") __pow_f64 :: proc(x, power: f64) -> f64 ---;
@(link_name="llvm.fmuladd.f32") fmuladd32 :: proc(a, b, c: f32) -> f32 ---;
@(link_name="llvm.fmuladd.f64") fmuladd64 :: proc(a, b, c: f64) -> f64 ---;
}
__abs_f32 :: inline proc "contextless" (x: f32) -> f32 {
foreign __llvm_core {
@(link_name="llvm.fabs.f32") _abs :: proc "c" (x: f32) -> f32 ---;
}
return _abs(x);
}
__abs_f64 :: inline proc "contextless" (x: f64) -> f64 {
foreign __llvm_core {
@(link_name="llvm.fabs.f64") _abs :: proc "c" (x: f64) -> f64 ---;
}
return _abs(x);
}
__min_f32 :: proc(a, b: f32) -> f32 {
foreign __llvm_core {
@(link_name="llvm.minnum.f32") _min :: proc "c" (a, b: f32) -> f32 ---;
}
return _min(a, b);
}
__min_f64 :: proc(a, b: f64) -> f64 {
foreign __llvm_core {
@(link_name="llvm.minnum.f64") _min :: proc "c" (a, b: f64) -> f64 ---;
}
return _min(a, b);
}
__max_f32 :: proc(a, b: f32) -> f32 {
foreign __llvm_core {
@(link_name="llvm.maxnum.f32") _max :: proc "c" (a, b: f32) -> f32 ---;
}
return _max(a, b);
}
__max_f64 :: proc(a, b: f64) -> f64 {
foreign __llvm_core {
@(link_name="llvm.maxnum.f64") _max :: proc "c" (a, b: f64) -> f64 ---;
}
return _max(a, b);
}
__abs_complex64 :: inline proc "contextless" (x: complex64) -> f32 {
r, i := real(x), imag(x);
return __sqrt_f32(r*r + i*i);
}
__abs_complex128 :: inline proc "contextless" (x: complex128) -> f64 {
r, i := real(x), imag(x);
return __sqrt_f64(r*r + i*i);
}
// Dynamic Array
__dynamic_array_make :: proc(array_: rawptr, elem_size, elem_align: int, len, cap: int, loc := #caller_location) {
@@ -1127,7 +495,7 @@ __dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, cap:
new_size := cap * elem_size;
allocator := array.allocator;
new_data := allocator.procedure(allocator.data, Allocator_Mode.Resize, new_size, elem_align, array.data, old_size, 0, loc);
new_data := allocator.procedure(allocator.data, mem.Allocator_Mode.Resize, new_size, elem_align, array.data, old_size, 0, loc);
if new_data == nil do return false;
array.data = new_data;
@@ -1186,7 +554,60 @@ __dynamic_array_append_nothing :: proc(array_: rawptr, elem_size, elem_align: in
return array.len;
}
// Map stuff
// Map
__get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> __Map_Header {
header := __Map_Header{m = (^raw.Map)(m)};
Entry :: struct {
key: __Map_Key,
next: int,
value: V,
}
_, is_string := type_info_base(type_info_of(K)).variant.(Type_Info_String);
header.is_key_string = is_string;
header.entry_size = int(size_of(Entry));
header.entry_align = int(align_of(Entry));
header.value_offset = uintptr(offset_of(Entry, value));
header.value_size = int(size_of(V));
return header;
}
__get_map_key :: proc "contextless" (key: $K) -> __Map_Key {
map_key: __Map_Key;
ti := type_info_base_without_enum(type_info_of(K));
switch _ in ti.variant {
case Type_Info_Integer:
switch 8*size_of(key) {
case 8: map_key.hash = u64(( ^u8)(&key)^);
case 16: map_key.hash = u64(( ^u16)(&key)^);
case 32: map_key.hash = u64(( ^u32)(&key)^);
case 64: map_key.hash = u64(( ^u64)(&key)^);
case: panic("Unhandled integer size");
}
case Type_Info_Rune:
map_key.hash = u64((^rune)(&key)^);
case Type_Info_Pointer:
map_key.hash = u64(uintptr((^rawptr)(&key)^));
case Type_Info_Float:
switch 8*size_of(key) {
case 32: map_key.hash = u64((^u32)(&key)^);
case 64: map_key.hash = u64((^u64)(&key)^);
case: panic("Unhandled float size");
}
case Type_Info_String:
str := (^string)(&key)^;
map_key.hash = __default_hash_string(str);
map_key.str = str;
case:
panic("Unhandled map key type");
}
return map_key;
}
__default_hash :: proc(data: []byte) -> u64 {
fnv64a :: proc(data: []byte) -> u64 {
@@ -1238,8 +659,8 @@ __dynamic_map_rehash :: proc(using header: __Map_Header, new_count: int, loc :=
if __dynamic_map_full(new_header) do __dynamic_map_grow(new_header, loc);
}
free_ptr_with_allocator(header_hashes.allocator, header_hashes.data, loc);
free_ptr_with_allocator(header.m.entries.allocator, header.m.entries.data, loc);
mem.free_ptr_with_allocator(header_hashes.allocator, header_hashes.data, loc);
mem.free_ptr_with_allocator(header.m.entries.allocator, header.m.entries.data, loc);
header.m^ = nm;
}
+432
View File
@@ -0,0 +1,432 @@
package runtime
import "core:raw"
import "core:mem"
import "core:os"
import "core:unicode/utf8"
__print_u64 :: proc(fd: os.Handle, u: u64) {
digits := "0123456789";
a: [129]byte;
i := len(a);
b := u64(10);
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, u: i64) {
digits := "0123456789";
neg := u < 0;
u = abs(u);
a: [129]byte;
i := len(a);
b := i64(10);
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) {
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:
a := any{typeid = typeid_of(ti)};
switch _ in a {
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, info.signed ? 'i' : '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_String:
os.write_string(fd, "string");
case Type_Info_Boolean:
a := any{typeid = typeid_of(ti)};
switch _ in a {
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_string(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_string(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_string(fd, "[");
__print_u64(fd, u64(info.count));
os.write_string(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:
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 {");
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, "}");
}
}
__string_eq :: proc "contextless" (a, b: string) -> bool {
switch {
case len(a) != len(b): return false;
case len(a) == 0: return true;
case &a[0] == &b[0]: return true;
}
return __string_cmp(a, b) == 0;
}
__string_cmp :: proc "contextless" (a, b: string) -> int {
return mem.compare_byte_ptrs(&a[0], &b[0], min(len(a), len(b)));
}
__string_ne :: inline proc "contextless" (a, b: string) -> bool { return !__string_eq(a, b); }
__string_lt :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) < 0; }
__string_gt :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) > 0; }
__string_le :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) <= 0; }
__string_ge :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) >= 0; }
__cstring_len :: proc "contextless" (s: cstring) -> int {
n := 0;
for p := (^byte)(s); p != nil && p^ != 0; p = mem.ptr_offset(p, 1) {
n += 1;
}
return n;
}
__cstring_to_string :: proc "contextless" (s: cstring) -> string {
if s == nil do return "";
ptr := (^byte)(s);
n := __cstring_len(s);
return transmute(string)raw.String{ptr, n};
}
__complex64_eq :: inline proc "contextless" (a, b: complex64) -> bool { return real(a) == real(b) && imag(a) == imag(b); }
__complex64_ne :: inline proc "contextless" (a, b: complex64) -> bool { return real(a) != real(b) || imag(a) != imag(b); }
__complex128_eq :: 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) {
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();
}
__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();
}
__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();
}
__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();
}
__string_decode_rune :: inline proc "contextless" (s: string) -> (rune, int) {
return utf8.decode_rune_from_string(s);
}
__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_loc :: inline proc "contextless" (using loc := #caller_location, lo, hi: int, len: int) {
__slice_expr_error(file_path, int(line), int(column), lo, hi, len);
}
__mem_set :: proc "contextless" (data: rawptr, value: i32, len: int) -> rawptr {
if data == nil do return nil;
foreign __llvm_core {
when size_of(rawptr) == 8 {
@(link_name="llvm.memset.p0i8.i64")
llvm_memset :: proc(dst: rawptr, val: byte, len: int, align: i32, is_volatile: bool) ---;
} else {
@(link_name="llvm.memset.p0i8.i32")
llvm_memset :: proc(dst: rawptr, val: byte, len: int, align: i32, is_volatile: bool) ---;
}
}
llvm_memset(data, byte(value), len, 1, false);
return data;
}
__mem_zero :: proc "contextless" (data: rawptr, len: int) -> rawptr {
return __mem_set(data, 0, len);
}
__mem_copy :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
if src == nil do return dst;
// NOTE(bill): This _must_ be implemented like C's memmove
foreign __llvm_core {
when size_of(rawptr) == 8 {
@(link_name="llvm.memmove.p0i8.p0i8.i64")
llvm_memmove :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) ---;
} else {
@(link_name="llvm.memmove.p0i8.p0i8.i32")
llvm_memmove :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) ---;
}
}
llvm_memmove(dst, src, len, 1, false);
return dst;
}
__mem_copy_non_overlapping :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
if src == nil do return dst;
// NOTE(bill): This _must_ be implemented like C's memcpy
foreign __llvm_core {
when size_of(rawptr) == 8 {
@(link_name="llvm.memcpy.p0i8.p0i8.i64")
llvm_memcpy :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) ---;
} else {
@(link_name="llvm.memcpy.p0i8.p0i8.i32")
llvm_memcpy :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) ---;
}
}
llvm_memcpy(dst, src, len, 1, false);
return dst;
}
@(default_calling_convention = "c")
foreign __llvm_core {
@(link_name="llvm.sqrt.f32") __sqrt_f32 :: proc(x: f32) -> f32 ---;
@(link_name="llvm.sqrt.f64") __sqrt_f64 :: proc(x: f64) -> f64 ---;
@(link_name="llvm.sin.f32") __sin_f32 :: proc(θ: f32) -> f32 ---;
@(link_name="llvm.sin.f64") __sin_f64 :: proc(θ: f64) -> f64 ---;
@(link_name="llvm.cos.f32") __cos_f32 :: proc(θ: f32) -> f32 ---;
@(link_name="llvm.cos.f64") __cos_f64 :: proc(θ: f64) -> f64 ---;
@(link_name="llvm.pow.f32") __pow_f32 :: proc(x, power: f32) -> f32 ---;
@(link_name="llvm.pow.f64") __pow_f64 :: proc(x, power: f64) -> f64 ---;
@(link_name="llvm.fmuladd.f32") fmuladd32 :: proc(a, b, c: f32) -> f32 ---;
@(link_name="llvm.fmuladd.f64") fmuladd64 :: proc(a, b, c: f64) -> f64 ---;
}
__abs_f32 :: inline proc "contextless" (x: f32) -> f32 {
foreign __llvm_core {
@(link_name="llvm.fabs.f32") _abs :: proc "c" (x: f32) -> f32 ---;
}
return _abs(x);
}
__abs_f64 :: inline proc "contextless" (x: f64) -> f64 {
foreign __llvm_core {
@(link_name="llvm.fabs.f64") _abs :: proc "c" (x: f64) -> f64 ---;
}
return _abs(x);
}
__min_f32 :: proc(a, b: f32) -> f32 {
foreign __llvm_core {
@(link_name="llvm.minnum.f32") _min :: proc "c" (a, b: f32) -> f32 ---;
}
return _min(a, b);
}
__min_f64 :: proc(a, b: f64) -> f64 {
foreign __llvm_core {
@(link_name="llvm.minnum.f64") _min :: proc "c" (a, b: f64) -> f64 ---;
}
return _min(a, b);
}
__max_f32 :: proc(a, b: f32) -> f32 {
foreign __llvm_core {
@(link_name="llvm.maxnum.f32") _max :: proc "c" (a, b: f32) -> f32 ---;
}
return _max(a, b);
}
__max_f64 :: proc(a, b: f64) -> f64 {
foreign __llvm_core {
@(link_name="llvm.maxnum.f64") _max :: proc "c" (a, b: f64) -> f64 ---;
}
return _max(a, b);
}
__abs_complex64 :: inline proc "contextless" (x: complex64) -> f32 {
r, i := real(x), imag(x);
return __sqrt_f32(r*r + i*i);
}
__abs_complex128 :: inline proc "contextless" (x: complex128) -> f64 {
r, i := real(x), imag(x);
return __sqrt_f64(r*r + i*i);
}
@@ -1,4 +1,4 @@
package builtin
package runtime
/*
@(link_name="__multi3")