:: style procedure declarations; remove old parsing code

This commit is contained in:
Ginger Bill
2017-06-28 23:47:06 +01:00
parent 4f28e9e1fb
commit 94afcec757
32 changed files with 956 additions and 1557 deletions

View File

@@ -18,7 +18,7 @@ import (
"utf16.odin";
)
proc general_stuff() {
general_stuff :: proc() {
// Complex numbers
a := 3 + 4i;
b: complex64 = 3 + 4i;
@@ -33,8 +33,7 @@ proc general_stuff() {
// C-style variadic procedures
foreign __llvm_core {
// The variadic part allows for extra type checking too which C does not provide
proc c_printf(fmt: ^u8, #c_vararg args: ..any) -> i32
#link_name "printf";
c_printf :: proc(fmt: ^u8, #c_vararg args: ..any) -> i32 #link_name "printf" ---;
}
@@ -69,12 +68,12 @@ proc general_stuff() {
}
proc foreign_blocks() {
foreign_blocks :: proc() {
// See sys/windows.odin
}
proc default_arguments() {
proc hello(a: int = 9, b: int = 9) {
default_arguments :: proc() {
hello :: proc(a: int = 9, b: int = 9) {
fmt.printf("a is %d; b is %d\n", a, b);
}
fmt.println("\nTesting default arguments:");
@@ -83,7 +82,7 @@ proc default_arguments() {
hello();
}
proc named_arguments() {
named_arguments :: proc() {
Colour :: enum {
Red,
Orange,
@@ -94,7 +93,7 @@ proc named_arguments() {
};
using Colour;
proc make_character(name, catch_phrase: string, favourite_colour, least_favourite_colour: Colour) {
make_character :: proc(name, catch_phrase: string, favourite_colour, least_favourite_colour: Colour) {
fmt.println();
fmt.printf("My name is %v and I like %v. %v\n", name, favourite_colour, catch_phrase);
}
@@ -126,7 +125,7 @@ proc named_arguments() {
// Named arguments can also aid with default arguments
proc numerous_things(s: string, a = 1, b = 2, c = 3.14,
numerous_things :: proc(s: string, a = 1, b = 2, c = 3.14,
d = "The Best String!", e = false, f = 10.3/3.1, g = false) {
g_str := g ? "true" : "false";
fmt.printf("How many?! %s: %v\n", s, g_str);
@@ -137,7 +136,7 @@ proc named_arguments() {
// Default values can be placed anywhere, not just at the end like in other languages
proc weird(pre: string, mid: int = 0, post: string) {
weird :: proc(pre: string, mid: int = 0, post: string) {
fmt.println(pre, mid, post);
}
@@ -147,8 +146,8 @@ proc named_arguments() {
}
proc default_return_values() {
proc foo(x: int) -> (first: string = "Hellope", second = "world!") {
default_return_values :: proc() {
foo :: proc(x: int) -> (first: string = "Hellope", second = "world!") {
match x {
case 0: return;
case 1: return "Goodbye";
@@ -179,7 +178,7 @@ proc default_return_values() {
id: u32,
}
proc some_thing(input: int) -> (result: ^Entity = nil, err = Error.None) {
some_thing :: proc(input: int) -> (result: ^Entity = nil, err = Error.None) {
match {
case input == 3: return err = Error.WhyTheNumberThree;
case input >= 10: return err = Error.TenIsTooBig;
@@ -192,8 +191,8 @@ proc default_return_values() {
}
}
proc call_location() {
proc amazing(n: int, using loc = #caller_location) {
call_location :: proc() {
amazing :: proc(n: int, using loc = #caller_location) {
fmt.printf("%s(%d:%d) just asked to do something amazing.\n",
fully_pathed_filename, line, column);
fmt.printf("Normal -> %d\n", n);
@@ -215,9 +214,9 @@ proc call_location() {
}
proc explicit_parametric_polymorphic_procedures() {
explicit_parametric_polymorphic_procedures :: proc() {
// This is how `new` is actually implemented, see _preload.odin
proc alloc_type(T: type) -> ^T {
alloc_type :: proc(T: type) -> ^T {
return ^T(alloc(size_of(T), align_of(T)));
}
@@ -231,7 +230,7 @@ proc explicit_parametric_polymorphic_procedures() {
defer free(another_ptr);
proc add(T: type, args: ..T) -> T {
add :: proc(T: type, args: ..T) -> T {
res: T;
for arg in args {
res += arg;
@@ -241,7 +240,7 @@ proc explicit_parametric_polymorphic_procedures() {
fmt.println("add =", add(int, 1, 2, 3, 4, 5, 6));
proc swap(T: type, a, b: ^T) {
swap :: proc(T: type, a, b: ^T) {
tmp := a^;
a^ = b^;
b^ = tmp;
@@ -298,7 +297,7 @@ proc explicit_parametric_polymorphic_procedures() {
batch_index: u32,
}
proc use_empty_slot(manager: ^EntityManager, batch: ^EntityBatch) -> ^Entity {
use_empty_slot :: proc(manager: ^EntityManager, batch: ^EntityBatch) -> ^Entity {
for ok, i in batch.occupied {
if ok -> continue;
batch.occupied[i] = true;
@@ -313,7 +312,7 @@ proc explicit_parametric_polymorphic_procedures() {
return nil;
}
proc gen_new_entity(manager: ^EntityManager) -> ^Entity {
gen_new_entity :: proc(manager: ^EntityManager) -> ^Entity {
for b in manager.batches {
e := use_empty_slot(manager, b);
if e != nil -> return e;
@@ -328,7 +327,7 @@ proc explicit_parametric_polymorphic_procedures() {
proc new_entity(manager: ^EntityManager, Type: type, x, y: int) -> ^Type {
new_entity :: proc(manager: ^EntityManager, Type: type, x, y: int) -> ^Type {
result := new(Type);
result.entity = gen_new_entity(manager);
result.derived.data = result;
@@ -369,7 +368,7 @@ proc explicit_parametric_polymorphic_procedures() {
}
proc main() {
main :: proc() {
general_stuff();
// foreign_blocks();
// default_arguments();
@@ -423,18 +422,18 @@ proc main() {
type MyInt int;
type BarType proc();
proc bar() {
bar :: proc() {
}
foreign lib {
proc foreign_bar();
foreign_bar :: proc();
}
*/
}
/*
proc main() {
main :: proc() {
program := "+ + * - /";
accumulator := 0;

View File

@@ -117,7 +117,7 @@ __argv__: ^^u8;
__argc__: i32;
proc type_info_base(info: ^TypeInfo) -> ^TypeInfo {
type_info_base :: proc(info: ^TypeInfo) -> ^TypeInfo {
if info == nil -> return nil;
base := info;
@@ -129,7 +129,7 @@ proc type_info_base(info: ^TypeInfo) -> ^TypeInfo {
}
proc type_info_base_without_enum(info: ^TypeInfo) -> ^TypeInfo {
type_info_base_without_enum :: proc(info: ^TypeInfo) -> ^TypeInfo {
if info == nil -> return nil;
base := info;
@@ -145,10 +145,10 @@ proc type_info_base_without_enum(info: ^TypeInfo) -> ^TypeInfo {
foreign __llvm_core {
proc assume (cond: bool) #link_name "llvm.assume";
proc __debug_trap () #link_name "llvm.debugtrap";
proc __trap () #link_name "llvm.trap";
proc read_cycle_counter() -> u64 #link_name "llvm.readcyclecounter";
assume :: proc(cond: bool) #link_name "llvm.assume" ---;
__debug_trap :: proc() #link_name "llvm.debugtrap" ---;
__trap :: proc() #link_name "llvm.trap" ---;
read_cycle_counter :: proc() -> u64 #link_name "llvm.readcyclecounter" ---;
}
// IMPORTANT NOTE(bill): Must be in this order (as the compiler relies upon it)
@@ -186,7 +186,7 @@ SourceCodeLocation :: struct {
procedure: string,
}
proc make_source_code_location(file: string, line, column: i64, procedure: string) -> SourceCodeLocation #cc_contextless #inline {
make_source_code_location :: proc(file: string, line, column: i64, procedure: string) -> SourceCodeLocation #cc_contextless #inline {
return SourceCodeLocation{file, line, column, procedure};
}
@@ -194,7 +194,7 @@ proc make_source_code_location(file: string, line, column: i64, procedure: strin
DEFAULT_ALIGNMENT :: align_of([vector 4]f32);
proc __init_context_from_ptr(c: ^Context, other: ^Context) #cc_contextless {
__init_context_from_ptr :: proc(c: ^Context, other: ^Context) #cc_contextless {
if c == nil -> return;
c^ = other^;
@@ -206,7 +206,7 @@ proc __init_context_from_ptr(c: ^Context, other: ^Context) #cc_contextless {
}
}
proc __init_context(c: ^Context) #cc_contextless {
__init_context :: proc(c: ^Context) #cc_contextless {
if c == nil -> return;
if c.allocator.procedure == nil {
@@ -219,18 +219,18 @@ proc __init_context(c: ^Context) #cc_contextless {
/*
proc __check_context() {
__check_context :: proc() {
__init_context(&__context);
}
*/
proc alloc(size: int, alignment: int = DEFAULT_ALIGNMENT) -> rawptr #inline {
alloc :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT) -> rawptr #inline {
// __check_context();
a := context.allocator;
return a.procedure(a.data, AllocatorMode.Alloc, size, alignment, nil, 0, 0);
}
proc free_ptr_with_allocator(a: Allocator, ptr: rawptr) #inline {
free_ptr_with_allocator :: proc(a: Allocator, ptr: rawptr) #inline {
if ptr == nil {
return;
}
@@ -240,32 +240,32 @@ proc free_ptr_with_allocator(a: Allocator, ptr: rawptr) #inline {
a.procedure(a.data, AllocatorMode.Free, 0, 0, ptr, 0, 0);
}
proc free_ptr(ptr: rawptr) #inline {
free_ptr :: proc(ptr: rawptr) #inline {
// __check_context();
free_ptr_with_allocator(context.allocator, ptr);
}
proc free_all() #inline {
free_all :: proc() #inline {
// __check_context();
a := context.allocator;
a.procedure(a.data, AllocatorMode.FreeAll, 0, 0, nil, 0, 0);
}
proc resize(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT) -> rawptr #inline {
resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT) -> rawptr #inline {
// __check_context();
a := context.allocator;
return a.procedure(a.data, AllocatorMode.Resize, new_size, alignment, ptr, old_size, 0);
}
proc new(T: type) -> ^T #inline {
new :: proc(T: type) -> ^T #inline {
return ^T(alloc(size_of(T), align_of(T)));
}
proc default_resize_align(old_memory: rawptr, old_size, new_size, alignment: int) -> rawptr {
default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: int) -> rawptr {
if old_memory == nil {
return alloc(new_size, alignment);
}
@@ -290,7 +290,7 @@ proc default_resize_align(old_memory: rawptr, old_size, new_size, alignment: int
}
proc default_allocator_proc(allocator_data: rawptr, mode: AllocatorMode,
default_allocator_proc :: proc(allocator_data: rawptr, mode: AllocatorMode,
size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
using AllocatorMode;
@@ -315,7 +315,7 @@ proc default_allocator_proc(allocator_data: rawptr, mode: AllocatorMode,
return nil;
}
proc default_allocator() -> Allocator {
default_allocator :: proc() -> Allocator {
return Allocator{
procedure = default_allocator_proc,
data = nil,
@@ -323,7 +323,7 @@ proc default_allocator() -> Allocator {
}
proc assert(condition: bool, message = "", using location = #caller_location) -> bool #cc_contextless {
assert :: proc(condition: bool, message = "", using location = #caller_location) -> bool #cc_contextless {
if !condition {
if len(message) > 0 {
fmt.printf("%s(%d:%d) Runtime assertion: %s\n", fully_pathed_filename, line, column, message);
@@ -335,7 +335,7 @@ proc assert(condition: bool, message = "", using location = #caller_location) ->
return condition;
}
proc panic(message = "", using location = #caller_location) #cc_contextless {
panic :: proc(message = "", using location = #caller_location) #cc_contextless {
if len(message) > 0 {
fmt.printf("%s(%d:%d) Panic: %s\n", fully_pathed_filename, line, column, message);
} else {
@@ -347,7 +347,7 @@ proc panic(message = "", using location = #caller_location) #cc_contextless {
proc __string_eq(a, b: string) -> bool #cc_contextless {
__string_eq :: proc(a, b: string) -> bool #cc_contextless {
if len(a) != len(b) {
return false;
}
@@ -360,25 +360,25 @@ proc __string_eq(a, b: string) -> bool #cc_contextless {
return __string_cmp(a, b) == 0;
}
proc __string_cmp(a, b: string) -> int #cc_contextless {
__string_cmp :: proc(a, b: string) -> int #cc_contextless {
return __mem_compare(&a[0], &b[0], min(len(a), len(b)));
}
proc __string_ne(a, b: string) -> bool #cc_contextless #inline { return !__string_eq(a, b); }
proc __string_lt(a, b: string) -> bool #cc_contextless #inline { return __string_cmp(a, b) < 0; }
proc __string_gt(a, b: string) -> bool #cc_contextless #inline { return __string_cmp(a, b) > 0; }
proc __string_le(a, b: string) -> bool #cc_contextless #inline { return __string_cmp(a, b) <= 0; }
proc __string_ge(a, b: string) -> bool #cc_contextless #inline { return __string_cmp(a, b) >= 0; }
__string_ne :: proc(a, b: string) -> bool #cc_contextless #inline { return !__string_eq(a, b); }
__string_lt :: proc(a, b: string) -> bool #cc_contextless #inline { return __string_cmp(a, b) < 0; }
__string_gt :: proc(a, b: string) -> bool #cc_contextless #inline { return __string_cmp(a, b) > 0; }
__string_le :: proc(a, b: string) -> bool #cc_contextless #inline { return __string_cmp(a, b) <= 0; }
__string_ge :: proc(a, b: string) -> bool #cc_contextless #inline { return __string_cmp(a, b) >= 0; }
proc __complex64_eq (a, b: complex64) -> bool #cc_contextless #inline { return real(a) == real(b) && imag(a) == imag(b); }
proc __complex64_ne (a, b: complex64) -> bool #cc_contextless #inline { return real(a) != real(b) || imag(a) != imag(b); }
__complex64_eq :: proc (a, b: complex64) -> bool #cc_contextless #inline { return real(a) == real(b) && imag(a) == imag(b); }
__complex64_ne :: proc (a, b: complex64) -> bool #cc_contextless #inline { return real(a) != real(b) || imag(a) != imag(b); }
proc __complex128_eq(a, b: complex128) -> bool #cc_contextless #inline { return real(a) == real(b) && imag(a) == imag(b); }
proc __complex128_ne(a, b: complex128) -> bool #cc_contextless #inline { return real(a) != real(b) || imag(a) != imag(b); }
__complex128_eq :: proc(a, b: complex128) -> bool #cc_contextless #inline { return real(a) == real(b) && imag(a) == imag(b); }
__complex128_ne :: proc(a, b: complex128) -> bool #cc_contextless #inline { return real(a) != real(b) || imag(a) != imag(b); }
proc __bounds_check_error(file: string, line, column: int, index, count: int) #cc_contextless {
__bounds_check_error :: proc(file: string, line, column: int, index, count: int) #cc_contextless {
if 0 <= index && index < count {
return;
}
@@ -387,7 +387,7 @@ proc __bounds_check_error(file: string, line, column: int, index, count: int) #c
__debug_trap();
}
proc __slice_expr_error(file: string, line, column: int, low, high, max: int) #cc_contextless {
__slice_expr_error :: proc(file: string, line, column: int, low, high, max: int) #cc_contextless {
if 0 <= low && low <= high && high <= max {
return;
}
@@ -396,7 +396,7 @@ proc __slice_expr_error(file: string, line, column: int, low, high, max: int) #c
__debug_trap();
}
proc __substring_expr_error(file: string, line, column: int, low, high: int) #cc_contextless {
__substring_expr_error :: proc(file: string, line, column: int, low, high: int) #cc_contextless {
if 0 <= low && low <= high {
return;
}
@@ -404,7 +404,7 @@ proc __substring_expr_error(file: string, line, column: int, low, high: int) #cc
file, line, column, low, high);
__debug_trap();
}
proc __type_assertion_check(ok: bool, file: string, line, column: int, from, to: ^TypeInfo) #cc_contextless {
__type_assertion_check :: proc(ok: bool, file: string, line, column: int, from, to: ^TypeInfo) #cc_contextless {
if !ok {
fmt.fprintf(os.stderr, "%s(%d:%d) Invalid type_assertion from %T to %T\n",
file, line, column, from, to);
@@ -412,51 +412,51 @@ proc __type_assertion_check(ok: bool, file: string, line, column: int, from, to:
}
}
proc __string_decode_rune(s: string) -> (rune, int) #cc_contextless #inline {
__string_decode_rune :: proc(s: string) -> (rune, int) #cc_contextless #inline {
return utf8.decode_rune(s);
}
proc __mem_set(data: rawptr, value: i32, len: int) -> rawptr #cc_contextless {
__mem_set :: proc(data: rawptr, value: i32, len: int) -> rawptr #cc_contextless {
when size_of(rawptr) == 8 {
foreign __llvm_core proc llvm_memset_64bit(dst: rawptr, val: u8, len: int, align: i32, is_volatile: bool) #link_name "llvm.memset.p0i8.i64";
foreign __llvm_core llvm_memset_64bit :: proc(dst: rawptr, val: u8, len: int, align: i32, is_volatile: bool) #link_name "llvm.memset.p0i8.i64" ---;
llvm_memset_64bit(data, u8(value), len, 1, false);
return data;
} else {
foreign __llvm_core proc llvm_memset_32bit(dst: rawptr, val: u8, len: int, align: i32, is_volatile: bool) #link_name "llvm.memset.p0i8.i32";
foreign __llvm_core llvm_memset_32bit :: proc(dst: rawptr, val: u8, len: int, align: i32, is_volatile: bool) #link_name "llvm.memset.p0i8.i32" ---;
llvm_memset_32bit(data, u8(value), len, 1, false);
return data;
}
}
proc __mem_zero(data: rawptr, len: int) -> rawptr #cc_contextless {
__mem_zero :: proc(data: rawptr, len: int) -> rawptr #cc_contextless {
return __mem_set(data, 0, len);
}
proc __mem_copy(dst, src: rawptr, len: int) -> rawptr #cc_contextless {
__mem_copy :: proc(dst, src: rawptr, len: int) -> rawptr #cc_contextless {
// NOTE(bill): This _must_ be implemented like C's memmove
when size_of(rawptr) == 8 {
foreign __llvm_core proc llvm_memmove_64bit(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #link_name "llvm.memmove.p0i8.p0i8.i64";
foreign __llvm_core llvm_memmove_64bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #link_name "llvm.memmove.p0i8.p0i8.i64" ---;
llvm_memmove_64bit(dst, src, len, 1, false);
return dst;
} else {
foreign __llvm_core proc llvm_memmove_32bit(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #link_name "llvm.memmove.p0i8.p0i8.i32";
foreign __llvm_core llvm_memmove_32bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #link_name "llvm.memmove.p0i8.p0i8.i32" ---;
llvm_memmove_32bit(dst, src, len, 1, false);
return dst;
}
}
proc __mem_copy_non_overlapping(dst, src: rawptr, len: int) -> rawptr #cc_contextless {
__mem_copy_non_overlapping :: proc(dst, src: rawptr, len: int) -> rawptr #cc_contextless {
// NOTE(bill): This _must_ be implemented like C's memcpy
when size_of(rawptr) == 8 {
foreign __llvm_core proc llvm_memcpy_64bit(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #link_name "llvm.memcpy.p0i8.p0i8.i64";
foreign __llvm_core llvm_memcpy_64bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #link_name "llvm.memcpy.p0i8.p0i8.i64" ---;
llvm_memcpy_64bit(dst, src, len, 1, false);
return dst;
} else {
foreign __llvm_core proc llvm_memcpy_32bit(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #link_name "llvm.memcpy.p0i8.p0i8.i32";
foreign __llvm_core llvm_memcpy_32bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #link_name "llvm.memcpy.p0i8.p0i8.i32";
llvm_memcpy_32bit(dst, src, len, 1, false);
return dst;
}
}
proc __mem_compare(a, b: ^u8, n: int) -> int #cc_contextless {
__mem_compare :: proc(a, b: ^u8, n: int) -> int #cc_contextless {
for i in 0..<n {
match {
case (a+i)^ < (b+i)^:
@@ -469,14 +469,14 @@ proc __mem_compare(a, b: ^u8, n: int) -> int #cc_contextless {
}
foreign __llvm_core {
proc __sqrt_f32(x: f32) -> f32 #link_name "llvm.sqrt.f32";
proc __sqrt_f64(x: f64) -> f64 #link_name "llvm.sqrt.f64";
__sqrt_f32 :: proc(x: f32) -> f32 #link_name "llvm.sqrt.f32" ---;
__sqrt_f64 :: proc(x: f64) -> f64 #link_name "llvm.sqrt.f64" ---;
}
proc __abs_complex64(x: complex64) -> f32 #inline #cc_contextless {
__abs_complex64 :: proc(x: complex64) -> f32 #inline #cc_contextless {
r, i := real(x), imag(x);
return __sqrt_f32(r*r + i*i);
}
proc __abs_complex128(x: complex128) -> f64 #inline #cc_contextless {
__abs_complex128 :: proc(x: complex128) -> f64 #inline #cc_contextless {
r, i := real(x), imag(x);
return __sqrt_f64(r*r + i*i);
}
@@ -484,7 +484,7 @@ proc __abs_complex128(x: complex128) -> f64 #inline #cc_contextless {
proc __dynamic_array_make(array_: rawptr, elem_size, elem_align: int, len, cap: int) {
__dynamic_array_make :: proc(array_: rawptr, elem_size, elem_align: int, len, cap: int) {
array := ^raw.DynamicArray(array_);
// __check_context();
array.allocator = context.allocator;
@@ -496,7 +496,7 @@ proc __dynamic_array_make(array_: rawptr, elem_size, elem_align: int, len, cap:
}
}
proc __dynamic_array_reserve(array_: rawptr, elem_size, elem_align: int, cap: int) -> bool {
__dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, cap: int) -> bool {
array := ^raw.DynamicArray(array_);
if cap <= array.cap -> return true;
@@ -519,7 +519,7 @@ proc __dynamic_array_reserve(array_: rawptr, elem_size, elem_align: int, cap: in
return true;
}
proc __dynamic_array_resize(array_: rawptr, elem_size, elem_align: int, len: int) -> bool {
__dynamic_array_resize :: proc(array_: rawptr, elem_size, elem_align: int, len: int) -> bool {
array := ^raw.DynamicArray(array_);
ok := __dynamic_array_reserve(array_, elem_size, elem_align, len);
@@ -528,7 +528,7 @@ proc __dynamic_array_resize(array_: rawptr, elem_size, elem_align: int, len: int
}
proc __dynamic_array_append(array_: rawptr, elem_size, elem_align: int,
__dynamic_array_append :: proc(array_: rawptr, elem_size, elem_align: int,
items: rawptr, item_count: int) -> int {
array := ^raw.DynamicArray(array_);
@@ -552,7 +552,7 @@ proc __dynamic_array_append(array_: rawptr, elem_size, elem_align: int,
return array.len;
}
proc __dynamic_array_append_nothing(array_: rawptr, elem_size, elem_align: int) -> int {
__dynamic_array_append_nothing :: proc(array_: rawptr, elem_size, elem_align: int) -> int {
array := ^raw.DynamicArray(array_);
ok := true;
@@ -570,7 +570,7 @@ proc __dynamic_array_append_nothing(array_: rawptr, elem_size, elem_align: int)
return array.len;
}
proc __slice_append(slice_: rawptr, elem_size, elem_align: int,
__slice_append :: proc(slice_: rawptr, elem_size, elem_align: int,
items: rawptr, item_count: int) -> int {
slice := ^raw.Slice(slice_);
@@ -591,8 +591,8 @@ proc __slice_append(slice_: rawptr, elem_size, elem_align: int,
// Map stuff
proc __default_hash(data: []u8) -> u128 {
proc fnv128a(data: []u8) -> u128 {
__default_hash :: proc(data: []u8) -> u128 {
fnv128a :: proc(data: []u8) -> u128 {
h: u128 = 0x6c62272e07bb014262b821756295c58d;
for b in data {
h = (h ~ u128(b)) * 0x1000000000000000000013b;
@@ -601,7 +601,7 @@ proc __default_hash(data: []u8) -> u128 {
}
return fnv128a(data);
}
proc __default_hash_string(s: string) -> u128 {
__default_hash_string :: proc(s: string) -> u128 {
return __default_hash([]u8(s));
}
@@ -635,12 +635,12 @@ __MapHeader :: struct #ordered {
value_size: int,
}
proc __dynamic_map_reserve(using header: __MapHeader, cap: int) {
__dynamic_map_reserve :: proc(using header: __MapHeader, cap: int) {
__dynamic_array_reserve(&m.hashes, size_of(int), align_of(int), cap);
__dynamic_array_reserve(&m.entries, entry_size, entry_align, cap);
}
proc __dynamic_map_rehash(using header: __MapHeader, new_count: int) {
__dynamic_map_rehash :: proc(using header: __MapHeader, new_count: int) {
new_header: __MapHeader = header;
nm: raw.DynamicMap;
new_header.m = &nm;
@@ -683,7 +683,7 @@ proc __dynamic_map_rehash(using header: __MapHeader, new_count: int) {
header.m^ = nm;
}
proc __dynamic_map_get(h: __MapHeader, key: __MapKey) -> rawptr {
__dynamic_map_get :: proc(h: __MapHeader, key: __MapKey) -> rawptr {
index := __dynamic_map_find(h, key).entry_index;
if index >= 0 {
data := ^u8(__dynamic_map_get_entry(h, index));
@@ -693,7 +693,7 @@ proc __dynamic_map_get(h: __MapHeader, key: __MapKey) -> rawptr {
return nil;
}
proc __dynamic_map_set(using h: __MapHeader, key: __MapKey, value: rawptr) {
__dynamic_map_set :: proc(using h: __MapHeader, key: __MapKey, value: rawptr) {
index: int;
assert(value != nil);
@@ -728,17 +728,17 @@ proc __dynamic_map_set(using h: __MapHeader, key: __MapKey, value: rawptr) {
}
proc __dynamic_map_grow(using h: __MapHeader) {
__dynamic_map_grow :: proc(using h: __MapHeader) {
new_count := max(2*m.entries.cap + 8, __INITIAL_MAP_CAP);
__dynamic_map_rehash(h, new_count);
}
proc __dynamic_map_full(using h: __MapHeader) -> bool {
__dynamic_map_full :: proc(using h: __MapHeader) -> bool {
return int(0.75 * f64(len(m.hashes))) <= m.entries.cap;
}
proc __dynamic_map_hash_equal(h: __MapHeader, a, b: __MapKey) -> bool {
__dynamic_map_hash_equal :: proc(h: __MapHeader, a, b: __MapKey) -> bool {
if a.hash == b.hash {
if h.is_key_string -> return a.str == b.str;
return true;
@@ -746,7 +746,7 @@ proc __dynamic_map_hash_equal(h: __MapHeader, a, b: __MapKey) -> bool {
return false;
}
proc __dynamic_map_find(using h: __MapHeader, key: __MapKey) -> __MapFindResult {
__dynamic_map_find :: proc(using h: __MapHeader, key: __MapKey) -> __MapFindResult {
fr := __MapFindResult{-1, -1, -1};
if len(m.hashes) > 0 {
fr.hash_index = int(key.hash % u128(len(m.hashes)));
@@ -763,7 +763,7 @@ proc __dynamic_map_find(using h: __MapHeader, key: __MapKey) -> __MapFindResult
return fr;
}
proc __dynamic_map_add_entry(using h: __MapHeader, key: __MapKey) -> int {
__dynamic_map_add_entry :: proc(using h: __MapHeader, key: __MapKey) -> int {
prev := m.entries.len;
c := __dynamic_array_append_nothing(&m.entries, entry_size, entry_align);
if c != prev {
@@ -775,19 +775,19 @@ proc __dynamic_map_add_entry(using h: __MapHeader, key: __MapKey) -> int {
}
proc __dynamic_map_delete(using h: __MapHeader, key: __MapKey) {
__dynamic_map_delete :: proc(using h: __MapHeader, key: __MapKey) {
fr := __dynamic_map_find(h, key);
if fr.entry_index >= 0 {
__dynamic_map_erase(h, fr);
}
}
proc __dynamic_map_get_entry(using h: __MapHeader, index: int) -> ^__MapEntryHeader {
__dynamic_map_get_entry :: proc(using h: __MapHeader, index: int) -> ^__MapEntryHeader {
data := ^u8(m.entries.data) + index*entry_size;
return ^__MapEntryHeader(data);
}
proc __dynamic_map_erase(using h: __MapHeader, fr: __MapFindResult) {
__dynamic_map_erase :: proc(using h: __MapHeader, fr: __MapFindResult) {
if fr.entry_prev < 0 {
m.hashes[fr.hash_index] = __dynamic_map_get_entry(h, fr.entry_index).next;
} else {

View File

@@ -1,6 +1,6 @@
#shared_global_scope;
proc __multi3(a, b: u128) -> u128 #cc_c #link_name "__multi3" {
__multi3 :: proc(a, b: u128) -> u128 #cc_c #link_name "__multi3" {
bits_in_dword_2 :: size_of(i64) * 4;
lower_mask :: u128(~u64(0) >> bits_in_dword_2);
@@ -35,27 +35,27 @@ proc __multi3(a, b: u128) -> u128 #cc_c #link_name "__multi3" {
return r.all;
}
proc __u128_mod(a, b: u128) -> u128 #cc_c #link_name "__umodti3" {
__u128_mod :: proc(a, b: u128) -> u128 #cc_c #link_name "__umodti3" {
r: u128;
__u128_quo_mod(a, b, &r);
return r;
}
proc __u128_quo(a, b: u128) -> u128 #cc_c #link_name "__udivti3" {
__u128_quo :: proc(a, b: u128) -> u128 #cc_c #link_name "__udivti3" {
return __u128_quo_mod(a, b, nil);
}
proc __i128_mod(a, b: i128) -> i128 #cc_c #link_name "__modti3" {
__i128_mod :: proc(a, b: i128) -> i128 #cc_c #link_name "__modti3" {
r: i128;
__i128_quo_mod(a, b, &r);
return r;
}
proc __i128_quo(a, b: i128) -> i128 #cc_c #link_name "__divti3" {
__i128_quo :: proc(a, b: i128) -> i128 #cc_c #link_name "__divti3" {
return __i128_quo_mod(a, b, nil);
}
proc __i128_quo_mod(a, b: i128, rem: ^i128) -> (quo: i128) #cc_c #link_name "__divmodti4" {
__i128_quo_mod :: proc(a, b: i128, rem: ^i128) -> (quo: i128) #cc_c #link_name "__divmodti4" {
s: i128;
s = b >> 127;
b = (b~s) - s;
@@ -74,7 +74,7 @@ proc __i128_quo_mod(a, b: i128, rem: ^i128) -> (quo: i128) #cc_c #link_name "__d
}
proc __u128_quo_mod(a, b: u128, rem: ^u128) -> (quo: u128) #cc_c #link_name "__udivmodti4" {
__u128_quo_mod :: proc(a, b: u128, rem: ^u128) -> (quo: u128) #cc_c #link_name "__udivmodti4" {
alo, ahi := u64(a), u64(a>>64);
blo, bhi := u64(b), u64(b>>64);
if b == 0 {
@@ -103,7 +103,7 @@ proc __u128_quo_mod(a, b: u128, rem: ^u128) -> (quo: u128) #cc_c #link_name "__u
}
/*
proc __f16_to_f32(f: f16) -> f32 #cc_c #no_inline #link_name "__gnu_h2f_ieee" {
__f16_to_f32 :: proc(f: f16) -> f32 #cc_c #no_inline #link_name "__gnu_h2f_ieee" {
when true {
// Source: https://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/
FP32 :: raw_union {u: u32, f: f32};
@@ -127,7 +127,7 @@ proc __f16_to_f32(f: f16) -> f32 #cc_c #no_inline #link_name "__gnu_h2f_ieee" {
return 0;
}
}
proc __f32_to_f16(f_: f32) -> f16 #cc_c #no_inline #link_name "__gnu_f2h_ieee" {
__f32_to_f16 :: proc(f_: f32) -> f16 #cc_c #no_inline #link_name "__gnu_f2h_ieee" {
when false {
// Source: https://gist.github.com/rygorous/2156668
FP16 :: raw_union {u: u16, f: f16};
@@ -217,11 +217,11 @@ proc __f32_to_f16(f_: f32) -> f16 #cc_c #no_inline #link_name "__gnu_f2h_ieee" {
}
}
proc __f64_to_f16(f: f64) -> f16 #cc_c #no_inline #link_name "__truncdfhf2" {
__f64_to_f16 :: proc(f: f64) -> f16 #cc_c #no_inline #link_name "__truncdfhf2" {
return __f32_to_f16(f32(f));
}
proc __f16_to_f64(f: f16) -> f64 #cc_c #no_inline {
__f16_to_f64 :: proc(f: f16) -> f64 #cc_c #no_inline {
return f64(__f16_to_f32(f));
}
*/

View File

@@ -5,35 +5,35 @@ import win32 "sys/windows.odin" when ODIN_OS == "windows";
_ := compile_assert(ODIN_ARCH == "amd64"); // TODO(bill): x86 version
proc yield_thread() { win32.mm_pause(); }
proc mfence () { win32.read_write_barrier(); }
proc sfence () { win32.write_barrier(); }
proc lfence () { win32.read_barrier(); }
yield_thread :: proc() { win32.mm_pause(); }
mfence :: proc() { win32.read_write_barrier(); }
sfence :: proc() { win32.write_barrier(); }
lfence :: proc() { win32.read_barrier(); }
proc load(a: ^i32) -> i32 {
load :: proc(a: ^i32) -> i32 {
return a^;
}
proc store(a: ^i32, value: i32) {
store :: proc(a: ^i32, value: i32) {
a^ = value;
}
proc compare_exchange(a: ^i32, expected, desired: i32) -> i32 {
compare_exchange :: proc(a: ^i32, expected, desired: i32) -> i32 {
return win32.interlocked_compare_exchange(a, desired, expected);
}
proc exchanged(a: ^i32, desired: i32) -> i32 {
exchanged :: proc(a: ^i32, desired: i32) -> i32 {
return win32.interlocked_exchange(a, desired);
}
proc fetch_add(a: ^i32, operand: i32) -> i32 {
fetch_add :: proc(a: ^i32, operand: i32) -> i32 {
return win32.interlocked_exchange_add(a, operand);
}
proc fetch_and(a: ^i32, operand: i32) -> i32 {
fetch_and :: proc(a: ^i32, operand: i32) -> i32 {
return win32.interlocked_and(a, operand);
}
proc fetch_or(a: ^i32, operand: i32) -> i32 {
fetch_or :: proc(a: ^i32, operand: i32) -> i32 {
return win32.interlocked_or(a, operand);
}
proc spin_lock(a: ^i32, time_out: int) -> bool { // NOTE(bill) time_out = -1 as default
spin_lock :: proc(a: ^i32, time_out: int) -> bool { // NOTE(bill) time_out = -1 as default
old_value := compare_exchange(a, 1, 0);
counter := 0;
for old_value != 0 && (time_out < 0 || counter < time_out) {
@@ -44,11 +44,11 @@ proc spin_lock(a: ^i32, time_out: int) -> bool { // NOTE(bill) time_out = -1 as
}
return old_value == 0;
}
proc spin_unlock(a: ^i32) {
spin_unlock :: proc(a: ^i32) {
store(a, 0);
mfence();
}
proc try_acquire_lock(a: ^i32) -> bool {
try_acquire_lock :: proc(a: ^i32) -> bool {
yield_thread();
old_value := compare_exchange(a, 1, 0);
mfence();
@@ -56,28 +56,28 @@ proc try_acquire_lock(a: ^i32) -> bool {
}
proc load(a: ^i64) -> i64 {
load :: proc(a: ^i64) -> i64 {
return a^;
}
proc store(a: ^i64, value: i64) {
store :: proc(a: ^i64, value: i64) {
a^ = value;
}
proc compare_exchange(a: ^i64, expected, desired: i64) -> i64 {
compare_exchange :: proc(a: ^i64, expected, desired: i64) -> i64 {
return win32.interlocked_compare_exchange64(a, desired, expected);
}
proc exchanged(a: ^i64, desired: i64) -> i64 {
exchanged :: proc(a: ^i64, desired: i64) -> i64 {
return win32.interlocked_exchange64(a, desired);
}
proc fetch_add(a: ^i64, operand: i64) -> i64 {
fetch_add :: proc(a: ^i64, operand: i64) -> i64 {
return win32.interlocked_exchange_add64(a, operand);
}
proc fetch_and(a: ^i64, operand: i64) -> i64 {
fetch_and :: proc(a: ^i64, operand: i64) -> i64 {
return win32.interlocked_and64(a, operand);
}
proc fetch_or(a: ^i64, operand: i64) -> i64 {
fetch_or :: proc(a: ^i64, operand: i64) -> i64 {
return win32.interlocked_or64(a, operand);
}
proc spin_lock(a: ^i64, time_out: int) -> bool { // NOTE(bill) time_out = -1 as default
spin_lock :: proc(a: ^i64, time_out: int) -> bool { // NOTE(bill) time_out = -1 as default
old_value := compare_exchange(a, 1, 0);
counter := 0;
for old_value != 0 && (time_out < 0 || counter < time_out) {
@@ -88,11 +88,11 @@ proc spin_lock(a: ^i64, time_out: int) -> bool { // NOTE(bill) time_out = -1 as
}
return old_value == 0;
}
proc spin_unlock(a: ^i64) {
spin_unlock :: proc(a: ^i64) {
store(a, 0);
mfence();
}
proc try_acquire_lock(a: ^i64) -> bool {
try_acquire_lock :: proc(a: ^i64) -> bool {
yield_thread();
old_value := compare_exchange(a, 1, 0);
mfence();

View File

@@ -22,179 +22,179 @@ I32_MAX :: i32(0x7fff_ffff);
I64_MAX :: i64(0x7fff_ffff_ffff_ffff);
I128_MAX :: i128(0x7fff_ffff_ffff_ffff_ffff_ffff_ffff_ffff);
proc count_ones(i: u8) -> u8 { foreign __llvm_core proc __llvm_ctpop(u8) -> u8 #link_name "llvm.ctpop.i8"; return __llvm_ctpop(i); }
proc count_ones(i: i8) -> i8 { foreign __llvm_core proc __llvm_ctpop(i8) -> i8 #link_name "llvm.ctpop.i8"; return __llvm_ctpop(i); }
proc count_ones(i: u16) -> u16 { foreign __llvm_core proc __llvm_ctpop(u16) -> u16 #link_name "llvm.ctpop.i16"; return __llvm_ctpop(i); }
proc count_ones(i: i16) -> i16 { foreign __llvm_core proc __llvm_ctpop(i16) -> i16 #link_name "llvm.ctpop.i16"; return __llvm_ctpop(i); }
proc count_ones(i: u32) -> u32 { foreign __llvm_core proc __llvm_ctpop(u32) -> u32 #link_name "llvm.ctpop.i32"; return __llvm_ctpop(i); }
proc count_ones(i: i32) -> i32 { foreign __llvm_core proc __llvm_ctpop(i32) -> i32 #link_name "llvm.ctpop.i32"; return __llvm_ctpop(i); }
proc count_ones(i: u64) -> u64 { foreign __llvm_core proc __llvm_ctpop(u64) -> u64 #link_name "llvm.ctpop.i64"; return __llvm_ctpop(i); }
proc count_ones(i: i64) -> i64 { foreign __llvm_core proc __llvm_ctpop(i64) -> i64 #link_name "llvm.ctpop.i64"; return __llvm_ctpop(i); }
proc count_ones(i: u128) -> u128 { foreign __llvm_core proc __llvm_ctpop(u128) -> u128 #link_name "llvm.ctpop.i128";return __llvm_ctpop(i); }
proc count_ones(i: i128) -> i128 { foreign __llvm_core proc __llvm_ctpop(i128) -> i128 #link_name "llvm.ctpop.i128";return __llvm_ctpop(i); }
proc count_ones(i: uint) -> uint { when size_of(uint) == size_of(u32) { return uint(count_ones(u32(i))); } else { return uint(count_ones(u64(i))); } }
proc count_ones(i: int) -> int { when size_of(int) == size_of(i32) { return int(count_ones(i32(i))); } else { return int(count_ones(i64(i))); } }
count_ones :: proc(i: u8) -> u8 { foreign __llvm_core __llvm_ctpop :: proc(u8) -> u8 #link_name "llvm.ctpop.i8" ---; return __llvm_ctpop(i); }
count_ones :: proc(i: i8) -> i8 { foreign __llvm_core __llvm_ctpop :: proc(i8) -> i8 #link_name "llvm.ctpop.i8" ---; return __llvm_ctpop(i); }
count_ones :: proc(i: u16) -> u16 { foreign __llvm_core __llvm_ctpop :: proc(u16) -> u16 #link_name "llvm.ctpop.i16" ---; return __llvm_ctpop(i); }
count_ones :: proc(i: i16) -> i16 { foreign __llvm_core __llvm_ctpop :: proc(i16) -> i16 #link_name "llvm.ctpop.i16" ---; return __llvm_ctpop(i); }
count_ones :: proc(i: u32) -> u32 { foreign __llvm_core __llvm_ctpop :: proc(u32) -> u32 #link_name "llvm.ctpop.i32" ---; return __llvm_ctpop(i); }
count_ones :: proc(i: i32) -> i32 { foreign __llvm_core __llvm_ctpop :: proc(i32) -> i32 #link_name "llvm.ctpop.i32" ---; return __llvm_ctpop(i); }
count_ones :: proc(i: u64) -> u64 { foreign __llvm_core __llvm_ctpop :: proc(u64) -> u64 #link_name "llvm.ctpop.i64" ---; return __llvm_ctpop(i); }
count_ones :: proc(i: i64) -> i64 { foreign __llvm_core __llvm_ctpop :: proc(i64) -> i64 #link_name "llvm.ctpop.i64" ---; return __llvm_ctpop(i); }
count_ones :: proc(i: u128) -> u128 { foreign __llvm_core __llvm_ctpop :: proc(u128) -> u128 #link_name "llvm.ctpop.i128" ---;return __llvm_ctpop(i); }
count_ones :: proc(i: i128) -> i128 { foreign __llvm_core __llvm_ctpop :: proc(i128) -> i128 #link_name "llvm.ctpop.i128" ---;return __llvm_ctpop(i); }
count_ones :: proc(i: uint) -> uint { when size_of(uint) == size_of(u32) { return uint(count_ones(u32(i))); } else { return uint(count_ones(u64(i))); } }
count_ones :: proc(i: int) -> int { when size_of(int) == size_of(i32) { return int(count_ones(i32(i))); } else { return int(count_ones(i64(i))); } }
proc count_zeros(i: u8) -> u8 { return 8 - count_ones(i); }
proc count_zeros(i: i8) -> i8 { return 8 - count_ones(i); }
proc count_zeros(i: u16) -> u16 { return 16 - count_ones(i); }
proc count_zeros(i: i16) -> i16 { return 16 - count_ones(i); }
proc count_zeros(i: u32) -> u32 { return 32 - count_ones(i); }
proc count_zeros(i: i32) -> i32 { return 32 - count_ones(i); }
proc count_zeros(i: u64) -> u64 { return 64 - count_ones(i); }
proc count_zeros(i: i64) -> i64 { return 64 - count_ones(i); }
proc count_zeros(i: u128) -> u128 { return 128 - count_ones(i); }
proc count_zeros(i: i128) -> i128 { return 128 - count_ones(i); }
proc count_zeros(i: uint) -> uint { return 8*size_of(uint) - count_ones(i); }
proc count_zeros(i: int) -> int { return 8*size_of(int) - count_ones(i); }
count_zeros :: proc(i: u8) -> u8 { return 8 - count_ones(i); }
count_zeros :: proc(i: i8) -> i8 { return 8 - count_ones(i); }
count_zeros :: proc(i: u16) -> u16 { return 16 - count_ones(i); }
count_zeros :: proc(i: i16) -> i16 { return 16 - count_ones(i); }
count_zeros :: proc(i: u32) -> u32 { return 32 - count_ones(i); }
count_zeros :: proc(i: i32) -> i32 { return 32 - count_ones(i); }
count_zeros :: proc(i: u64) -> u64 { return 64 - count_ones(i); }
count_zeros :: proc(i: i64) -> i64 { return 64 - count_ones(i); }
count_zeros :: proc(i: u128) -> u128 { return 128 - count_ones(i); }
count_zeros :: proc(i: i128) -> i128 { return 128 - count_ones(i); }
count_zeros :: proc(i: uint) -> uint { return 8*size_of(uint) - count_ones(i); }
count_zeros :: proc(i: int) -> int { return 8*size_of(int) - count_ones(i); }
proc rotate_left(i: u8, s: uint) -> u8 { return (i << s)|(i >> (8*size_of(u8) - s)); }
proc rotate_left(i: i8, s: uint) -> i8 { return (i << s)|(i >> (8*size_of(i8) - s)); }
proc rotate_left(i: u16, s: uint) -> u16 { return (i << s)|(i >> (8*size_of(u16) - s)); }
proc rotate_left(i: i16, s: uint) -> i16 { return (i << s)|(i >> (8*size_of(i16) - s)); }
proc rotate_left(i: u32, s: uint) -> u32 { return (i << s)|(i >> (8*size_of(u32) - s)); }
proc rotate_left(i: i32, s: uint) -> i32 { return (i << s)|(i >> (8*size_of(i32) - s)); }
proc rotate_left(i: u64, s: uint) -> u64 { return (i << s)|(i >> (8*size_of(u64) - s)); }
proc rotate_left(i: i64, s: uint) -> i64 { return (i << s)|(i >> (8*size_of(i64) - s)); }
proc rotate_left(i: u128, s: uint) -> u128 { return (i << s)|(i >> (8*size_of(u128) - s)); }
proc rotate_left(i: i128, s: uint) -> i128 { return (i << s)|(i >> (8*size_of(i128) - s)); }
proc rotate_left(i: uint, s: uint) -> uint { when size_of(uint) == size_of(u32) { return uint(rotate_left(u32(i), s)); } else { return uint(rotate_left(u64(i), s)); } }
proc rotate_left(i: int, s: uint) -> int { when size_of(int) == size_of(i32) { return int(rotate_left(i32(i), s)); } else { return int(rotate_left(i64(i), s)); } }
rotate_left :: proc(i: u8, s: uint) -> u8 { return (i << s)|(i >> (8*size_of(u8) - s)); }
rotate_left :: proc(i: i8, s: uint) -> i8 { return (i << s)|(i >> (8*size_of(i8) - s)); }
rotate_left :: proc(i: u16, s: uint) -> u16 { return (i << s)|(i >> (8*size_of(u16) - s)); }
rotate_left :: proc(i: i16, s: uint) -> i16 { return (i << s)|(i >> (8*size_of(i16) - s)); }
rotate_left :: proc(i: u32, s: uint) -> u32 { return (i << s)|(i >> (8*size_of(u32) - s)); }
rotate_left :: proc(i: i32, s: uint) -> i32 { return (i << s)|(i >> (8*size_of(i32) - s)); }
rotate_left :: proc(i: u64, s: uint) -> u64 { return (i << s)|(i >> (8*size_of(u64) - s)); }
rotate_left :: proc(i: i64, s: uint) -> i64 { return (i << s)|(i >> (8*size_of(i64) - s)); }
rotate_left :: proc(i: u128, s: uint) -> u128 { return (i << s)|(i >> (8*size_of(u128) - s)); }
rotate_left :: proc(i: i128, s: uint) -> i128 { return (i << s)|(i >> (8*size_of(i128) - s)); }
rotate_left :: proc(i: uint, s: uint) -> uint { when size_of(uint) == size_of(u32) { return uint(rotate_left(u32(i), s)); } else { return uint(rotate_left(u64(i), s)); } }
rotate_left :: proc(i: int, s: uint) -> int { when size_of(int) == size_of(i32) { return int(rotate_left(i32(i), s)); } else { return int(rotate_left(i64(i), s)); } }
proc rotate_right(i: u8, s: uint) -> u8 { return (i >> s)|(i << (8*size_of(u8) - s)); }
proc rotate_right(i: i8, s: uint) -> i8 { return (i >> s)|(i << (8*size_of(i8) - s)); }
proc rotate_right(i: u16, s: uint) -> u16 { return (i >> s)|(i << (8*size_of(u16) - s)); }
proc rotate_right(i: i16, s: uint) -> i16 { return (i >> s)|(i << (8*size_of(i16) - s)); }
proc rotate_right(i: u32, s: uint) -> u32 { return (i >> s)|(i << (8*size_of(u32) - s)); }
proc rotate_right(i: i32, s: uint) -> i32 { return (i >> s)|(i << (8*size_of(i32) - s)); }
proc rotate_right(i: u64, s: uint) -> u64 { return (i >> s)|(i << (8*size_of(u64) - s)); }
proc rotate_right(i: i64, s: uint) -> i64 { return (i >> s)|(i << (8*size_of(i64) - s)); }
proc rotate_right(i: u128, s: uint) -> u128 { return (i >> s)|(i << (8*size_of(u128) - s)); }
proc rotate_right(i: i128, s: uint) -> i128 { return (i >> s)|(i << (8*size_of(i128) - s)); }
proc rotate_right(i: uint, s: uint) -> uint { when size_of(uint) == size_of(u32) { return uint(rotate_right(u32(i), s)); } else { return uint(rotate_right(u64(i), s)); } }
proc rotate_right(i: int, s: uint) -> int { when size_of(int) == size_of(i32) { return int(rotate_right(i32(i), s)); } else { return int(rotate_right(i64(i), s)); } }
rotate_right :: proc(i: u8, s: uint) -> u8 { return (i >> s)|(i << (8*size_of(u8) - s)); }
rotate_right :: proc(i: i8, s: uint) -> i8 { return (i >> s)|(i << (8*size_of(i8) - s)); }
rotate_right :: proc(i: u16, s: uint) -> u16 { return (i >> s)|(i << (8*size_of(u16) - s)); }
rotate_right :: proc(i: i16, s: uint) -> i16 { return (i >> s)|(i << (8*size_of(i16) - s)); }
rotate_right :: proc(i: u32, s: uint) -> u32 { return (i >> s)|(i << (8*size_of(u32) - s)); }
rotate_right :: proc(i: i32, s: uint) -> i32 { return (i >> s)|(i << (8*size_of(i32) - s)); }
rotate_right :: proc(i: u64, s: uint) -> u64 { return (i >> s)|(i << (8*size_of(u64) - s)); }
rotate_right :: proc(i: i64, s: uint) -> i64 { return (i >> s)|(i << (8*size_of(i64) - s)); }
rotate_right :: proc(i: u128, s: uint) -> u128 { return (i >> s)|(i << (8*size_of(u128) - s)); }
rotate_right :: proc(i: i128, s: uint) -> i128 { return (i >> s)|(i << (8*size_of(i128) - s)); }
rotate_right :: proc(i: uint, s: uint) -> uint { when size_of(uint) == size_of(u32) { return uint(rotate_right(u32(i), s)); } else { return uint(rotate_right(u64(i), s)); } }
rotate_right :: proc(i: int, s: uint) -> int { when size_of(int) == size_of(i32) { return int(rotate_right(i32(i), s)); } else { return int(rotate_right(i64(i), s)); } }
proc leading_zeros(i: u8) -> u8 { foreign __llvm_core proc __llvm_ctlz(u8, bool) -> u8 #link_name "llvm.ctlz.i8"; return __llvm_ctlz(i, false); }
proc leading_zeros(i: i8) -> i8 { foreign __llvm_core proc __llvm_ctlz(i8, bool) -> i8 #link_name "llvm.ctlz.i8"; return __llvm_ctlz(i, false); }
proc leading_zeros(i: u16) -> u16 { foreign __llvm_core proc __llvm_ctlz(u16, bool) -> u16 #link_name "llvm.ctlz.i16"; return __llvm_ctlz(i, false); }
proc leading_zeros(i: i16) -> i16 { foreign __llvm_core proc __llvm_ctlz(i16, bool) -> i16 #link_name "llvm.ctlz.i16"; return __llvm_ctlz(i, false); }
proc leading_zeros(i: u32) -> u32 { foreign __llvm_core proc __llvm_ctlz(u32, bool) -> u32 #link_name "llvm.ctlz.i32"; return __llvm_ctlz(i, false); }
proc leading_zeros(i: i32) -> i32 { foreign __llvm_core proc __llvm_ctlz(i32, bool) -> i32 #link_name "llvm.ctlz.i32"; return __llvm_ctlz(i, false); }
proc leading_zeros(i: u64) -> u64 { foreign __llvm_core proc __llvm_ctlz(u64, bool) -> u64 #link_name "llvm.ctlz.i64"; return __llvm_ctlz(i, false); }
proc leading_zeros(i: i64) -> i64 { foreign __llvm_core proc __llvm_ctlz(i64, bool) -> i64 #link_name "llvm.ctlz.i64"; return __llvm_ctlz(i, false); }
proc leading_zeros(i: u128) -> u128 { foreign __llvm_core proc __llvm_ctlz(u128, bool) -> u128 #link_name "llvm.ctlz.i128";return __llvm_ctlz(i, false); }
proc leading_zeros(i: i128) -> i128 { foreign __llvm_core proc __llvm_ctlz(i128, bool) -> i128 #link_name "llvm.ctlz.i128";return __llvm_ctlz(i, false); }
proc leading_zeros(i: uint) -> uint { when size_of(uint) == size_of(u32) { return uint(leading_zeros(u32(i))); } else { return uint(leading_zeros(u64(i))); } }
proc leading_zeros(i: int) -> int { when size_of(int) == size_of(i32) { return int(leading_zeros(i32(i))); } else { return int(leading_zeros(i64(i))); } }
leading_zeros :: proc(i: u8) -> u8 { foreign __llvm_core __llvm_ctlz :: proc(u8, bool) -> u8 #link_name "llvm.ctlz.i8" ---; return __llvm_ctlz(i, false); }
leading_zeros :: proc(i: i8) -> i8 { foreign __llvm_core __llvm_ctlz :: proc(i8, bool) -> i8 #link_name "llvm.ctlz.i8" ---; return __llvm_ctlz(i, false); }
leading_zeros :: proc(i: u16) -> u16 { foreign __llvm_core __llvm_ctlz :: proc(u16, bool) -> u16 #link_name "llvm.ctlz.i16" ---; return __llvm_ctlz(i, false); }
leading_zeros :: proc(i: i16) -> i16 { foreign __llvm_core __llvm_ctlz :: proc(i16, bool) -> i16 #link_name "llvm.ctlz.i16" ---; return __llvm_ctlz(i, false); }
leading_zeros :: proc(i: u32) -> u32 { foreign __llvm_core __llvm_ctlz :: proc(u32, bool) -> u32 #link_name "llvm.ctlz.i32" ---; return __llvm_ctlz(i, false); }
leading_zeros :: proc(i: i32) -> i32 { foreign __llvm_core __llvm_ctlz :: proc(i32, bool) -> i32 #link_name "llvm.ctlz.i32" ---; return __llvm_ctlz(i, false); }
leading_zeros :: proc(i: u64) -> u64 { foreign __llvm_core __llvm_ctlz :: proc(u64, bool) -> u64 #link_name "llvm.ctlz.i64" ---; return __llvm_ctlz(i, false); }
leading_zeros :: proc(i: i64) -> i64 { foreign __llvm_core __llvm_ctlz :: proc(i64, bool) -> i64 #link_name "llvm.ctlz.i64" ---; return __llvm_ctlz(i, false); }
leading_zeros :: proc(i: u128) -> u128 { foreign __llvm_core __llvm_ctlz :: proc(u128, bool) -> u128 #link_name "llvm.ctlz.i128" ---;return __llvm_ctlz(i, false); }
leading_zeros :: proc(i: i128) -> i128 { foreign __llvm_core __llvm_ctlz :: proc(i128, bool) -> i128 #link_name "llvm.ctlz.i128" ---;return __llvm_ctlz(i, false); }
leading_zeros :: proc(i: uint) -> uint { when size_of(uint) == size_of(u32) { return uint(leading_zeros(u32(i))); } else { return uint(leading_zeros(u64(i))); } }
leading_zeros :: proc(i: int) -> int { when size_of(int) == size_of(i32) { return int(leading_zeros(i32(i))); } else { return int(leading_zeros(i64(i))); } }
proc trailing_zeros(i: u8) -> u8 { foreign __llvm_core proc __llvm_cttz(u8, bool) -> u8 #link_name "llvm.cttz.i8"; return __llvm_cttz(i, false); }
proc trailing_zeros(i: i8) -> i8 { foreign __llvm_core proc __llvm_cttz(i8, bool) -> i8 #link_name "llvm.cttz.i8"; return __llvm_cttz(i, false); }
proc trailing_zeros(i: u16) -> u16 { foreign __llvm_core proc __llvm_cttz(u16, bool) -> u16 #link_name "llvm.cttz.i16"; return __llvm_cttz(i, false); }
proc trailing_zeros(i: i16) -> i16 { foreign __llvm_core proc __llvm_cttz(i16, bool) -> i16 #link_name "llvm.cttz.i16"; return __llvm_cttz(i, false); }
proc trailing_zeros(i: u32) -> u32 { foreign __llvm_core proc __llvm_cttz(u32, bool) -> u32 #link_name "llvm.cttz.i32"; return __llvm_cttz(i, false); }
proc trailing_zeros(i: i32) -> i32 { foreign __llvm_core proc __llvm_cttz(i32, bool) -> i32 #link_name "llvm.cttz.i32"; return __llvm_cttz(i, false); }
proc trailing_zeros(i: u64) -> u64 { foreign __llvm_core proc __llvm_cttz(u64, bool) -> u64 #link_name "llvm.cttz.i64"; return __llvm_cttz(i, false); }
proc trailing_zeros(i: i64) -> i64 { foreign __llvm_core proc __llvm_cttz(i64, bool) -> i64 #link_name "llvm.cttz.i64"; return __llvm_cttz(i, false); }
proc trailing_zeros(i: u128) -> u128 { foreign __llvm_core proc __llvm_cttz(u128, bool) -> u128 #link_name "llvm.cttz.i128";return __llvm_cttz(i, false); }
proc trailing_zeros(i: i128) -> i128 { foreign __llvm_core proc __llvm_cttz(i128, bool) -> i128 #link_name "llvm.cttz.i128";return __llvm_cttz(i, false); }
proc trailing_zeros(i: uint) -> uint { when size_of(uint) == size_of(u32) { return uint(trailing_zeros(u32(i))); } else { return uint(trailing_zeros(u64(i))); } }
proc trailing_zeros(i: int) -> int { when size_of(int) == size_of(i32) { return int(trailing_zeros(i32(i))); } else { return int(trailing_zeros(i64(i))); } }
trailing_zeros :: proc(i: u8) -> u8 { foreign __llvm_core __llvm_cttz :: proc(u8, bool) -> u8 #link_name "llvm.cttz.i8" ---; return __llvm_cttz(i, false); }
trailing_zeros :: proc(i: i8) -> i8 { foreign __llvm_core __llvm_cttz :: proc(i8, bool) -> i8 #link_name "llvm.cttz.i8" ---; return __llvm_cttz(i, false); }
trailing_zeros :: proc(i: u16) -> u16 { foreign __llvm_core __llvm_cttz :: proc(u16, bool) -> u16 #link_name "llvm.cttz.i16" ---; return __llvm_cttz(i, false); }
trailing_zeros :: proc(i: i16) -> i16 { foreign __llvm_core __llvm_cttz :: proc(i16, bool) -> i16 #link_name "llvm.cttz.i16" ---; return __llvm_cttz(i, false); }
trailing_zeros :: proc(i: u32) -> u32 { foreign __llvm_core __llvm_cttz :: proc(u32, bool) -> u32 #link_name "llvm.cttz.i32" ---; return __llvm_cttz(i, false); }
trailing_zeros :: proc(i: i32) -> i32 { foreign __llvm_core __llvm_cttz :: proc(i32, bool) -> i32 #link_name "llvm.cttz.i32" ---; return __llvm_cttz(i, false); }
trailing_zeros :: proc(i: u64) -> u64 { foreign __llvm_core __llvm_cttz :: proc(u64, bool) -> u64 #link_name "llvm.cttz.i64" ---; return __llvm_cttz(i, false); }
trailing_zeros :: proc(i: i64) -> i64 { foreign __llvm_core __llvm_cttz :: proc(i64, bool) -> i64 #link_name "llvm.cttz.i64" ---; return __llvm_cttz(i, false); }
trailing_zeros :: proc(i: u128) -> u128 { foreign __llvm_core __llvm_cttz :: proc(u128, bool) -> u128 #link_name "llvm.cttz.i128" ---;return __llvm_cttz(i, false); }
trailing_zeros :: proc(i: i128) -> i128 { foreign __llvm_core __llvm_cttz :: proc(i128, bool) -> i128 #link_name "llvm.cttz.i128" ---;return __llvm_cttz(i, false); }
trailing_zeros :: proc(i: uint) -> uint { when size_of(uint) == size_of(u32) { return uint(trailing_zeros(u32(i))); } else { return uint(trailing_zeros(u64(i))); } }
trailing_zeros :: proc(i: int) -> int { when size_of(int) == size_of(i32) { return int(trailing_zeros(i32(i))); } else { return int(trailing_zeros(i64(i))); } }
proc reverse_bits(i: u8) -> u8 { foreign __llvm_core proc __llvm_bitreverse(u8) -> u8 #link_name "llvm.bitreverse.i8"; return __llvm_bitreverse(i); }
proc reverse_bits(i: i8) -> i8 { foreign __llvm_core proc __llvm_bitreverse(i8) -> i8 #link_name "llvm.bitreverse.i8"; return __llvm_bitreverse(i); }
proc reverse_bits(i: u16) -> u16 { foreign __llvm_core proc __llvm_bitreverse(u16) -> u16 #link_name "llvm.bitreverse.i16"; return __llvm_bitreverse(i); }
proc reverse_bits(i: i16) -> i16 { foreign __llvm_core proc __llvm_bitreverse(i16) -> i16 #link_name "llvm.bitreverse.i16"; return __llvm_bitreverse(i); }
proc reverse_bits(i: u32) -> u32 { foreign __llvm_core proc __llvm_bitreverse(u32) -> u32 #link_name "llvm.bitreverse.i32"; return __llvm_bitreverse(i); }
proc reverse_bits(i: i32) -> i32 { foreign __llvm_core proc __llvm_bitreverse(i32) -> i32 #link_name "llvm.bitreverse.i32"; return __llvm_bitreverse(i); }
proc reverse_bits(i: u64) -> u64 { foreign __llvm_core proc __llvm_bitreverse(u64) -> u64 #link_name "llvm.bitreverse.i64"; return __llvm_bitreverse(i); }
proc reverse_bits(i: i64) -> i64 { foreign __llvm_core proc __llvm_bitreverse(i64) -> i64 #link_name "llvm.bitreverse.i64"; return __llvm_bitreverse(i); }
proc reverse_bits(i: u128) -> u128 { foreign __llvm_core proc __llvm_bitreverse(u128) -> u128 #link_name "llvm.bitreverse.i128";return __llvm_bitreverse(i); }
proc reverse_bits(i: i128) -> i128 { foreign __llvm_core proc __llvm_bitreverse(i128) -> i128 #link_name "llvm.bitreverse.i128";return __llvm_bitreverse(i); }
proc reverse_bits(i: uint) -> uint { when size_of(uint) == size_of(u32) { return uint(reverse_bits(u32(i))); } else { return uint(reverse_bits(u64(i))); } }
proc reverse_bits(i: int) -> int { when size_of(int) == size_of(i32) { return int(reverse_bits(i32(i))); } else { return int(reverse_bits(i64(i))); } }
reverse_bits :: proc(i: u8) -> u8 { foreign __llvm_core __llvm_bitreverse :: proc(u8) -> u8 #link_name "llvm.bitreverse.i8" ---; return __llvm_bitreverse(i); }
reverse_bits :: proc(i: i8) -> i8 { foreign __llvm_core __llvm_bitreverse :: proc(i8) -> i8 #link_name "llvm.bitreverse.i8" ---; return __llvm_bitreverse(i); }
reverse_bits :: proc(i: u16) -> u16 { foreign __llvm_core __llvm_bitreverse :: proc(u16) -> u16 #link_name "llvm.bitreverse.i16" ---; return __llvm_bitreverse(i); }
reverse_bits :: proc(i: i16) -> i16 { foreign __llvm_core __llvm_bitreverse :: proc(i16) -> i16 #link_name "llvm.bitreverse.i16" ---; return __llvm_bitreverse(i); }
reverse_bits :: proc(i: u32) -> u32 { foreign __llvm_core __llvm_bitreverse :: proc(u32) -> u32 #link_name "llvm.bitreverse.i32" ---; return __llvm_bitreverse(i); }
reverse_bits :: proc(i: i32) -> i32 { foreign __llvm_core __llvm_bitreverse :: proc(i32) -> i32 #link_name "llvm.bitreverse.i32" ---; return __llvm_bitreverse(i); }
reverse_bits :: proc(i: u64) -> u64 { foreign __llvm_core __llvm_bitreverse :: proc(u64) -> u64 #link_name "llvm.bitreverse.i64" ---; return __llvm_bitreverse(i); }
reverse_bits :: proc(i: i64) -> i64 { foreign __llvm_core __llvm_bitreverse :: proc(i64) -> i64 #link_name "llvm.bitreverse.i64" ---; return __llvm_bitreverse(i); }
reverse_bits :: proc(i: u128) -> u128 { foreign __llvm_core __llvm_bitreverse :: proc(u128) -> u128 #link_name "llvm.bitreverse.i128" ---;return __llvm_bitreverse(i); }
reverse_bits :: proc(i: i128) -> i128 { foreign __llvm_core __llvm_bitreverse :: proc(i128) -> i128 #link_name "llvm.bitreverse.i128" ---;return __llvm_bitreverse(i); }
reverse_bits :: proc(i: uint) -> uint { when size_of(uint) == size_of(u32) { return uint(reverse_bits(u32(i))); } else { return uint(reverse_bits(u64(i))); } }
reverse_bits :: proc(i: int) -> int { when size_of(int) == size_of(i32) { return int(reverse_bits(i32(i))); } else { return int(reverse_bits(i64(i))); } }
foreign __llvm_core {
proc byte_swap(u16) -> u16 #link_name "llvm.bswap.i16";
proc byte_swap(i16) -> i16 #link_name "llvm.bswap.i16";
proc byte_swap(u32) -> u32 #link_name "llvm.bswap.i32";
proc byte_swap(i32) -> i32 #link_name "llvm.bswap.i32";
proc byte_swap(u64) -> u64 #link_name "llvm.bswap.i64";
proc byte_swap(i64) -> i64 #link_name "llvm.bswap.i64";
proc byte_swap(u128) -> u128 #link_name "llvm.bswap.i128";
proc byte_swap(i128) -> i128 #link_name "llvm.bswap.i128";
byte_swap :: proc(u16) -> u16 #link_name "llvm.bswap.i16" ---;
byte_swap :: proc(i16) -> i16 #link_name "llvm.bswap.i16" ---;
byte_swap :: proc(u32) -> u32 #link_name "llvm.bswap.i32" ---;
byte_swap :: proc(i32) -> i32 #link_name "llvm.bswap.i32" ---;
byte_swap :: proc(u64) -> u64 #link_name "llvm.bswap.i64" ---;
byte_swap :: proc(i64) -> i64 #link_name "llvm.bswap.i64" ---;
byte_swap :: proc(u128) -> u128 #link_name "llvm.bswap.i128" ---;
byte_swap :: proc(i128) -> i128 #link_name "llvm.bswap.i128" ---;
}
proc byte_swap(i: uint) -> uint { when size_of(uint) == size_of(u32) { return uint(byte_swap(u32(i))); } else { return uint(byte_swap(u64(i))); } }
proc byte_swap(i: int) -> int { when size_of(int) == size_of(i32) { return int(byte_swap(i32(i))); } else { return int(byte_swap(i64(i))); } }
byte_swap :: proc(i: uint) -> uint { when size_of(uint) == size_of(u32) { return uint(byte_swap(u32(i))); } else { return uint(byte_swap(u64(i))); } }
byte_swap :: proc(i: int) -> int { when size_of(int) == size_of(i32) { return int(byte_swap(i32(i))); } else { return int(byte_swap(i64(i))); } }
proc from_be(i: u8) -> u8 { return i; }
proc from_be(i: i8) -> i8 { return i; }
proc from_be(i: u16) -> u16 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc from_be(i: i16) -> i16 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc from_be(i: u32) -> u32 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc from_be(i: i32) -> i32 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc from_be(i: u64) -> u64 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc from_be(i: i64) -> i64 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc from_be(i: u128) -> u128 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc from_be(i: i128) -> i128 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc from_be(i: uint) -> uint { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc from_be(i: int) -> int { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
from_be :: proc(i: u8) -> u8 { return i; }
from_be :: proc(i: i8) -> i8 { return i; }
from_be :: proc(i: u16) -> u16 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
from_be :: proc(i: i16) -> i16 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
from_be :: proc(i: u32) -> u32 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
from_be :: proc(i: i32) -> i32 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
from_be :: proc(i: u64) -> u64 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
from_be :: proc(i: i64) -> i64 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
from_be :: proc(i: u128) -> u128 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
from_be :: proc(i: i128) -> i128 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
from_be :: proc(i: uint) -> uint { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
from_be :: proc(i: int) -> int { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc from_le(i: u8) -> u8 { return i; }
proc from_le(i: i8) -> i8 { return i; }
proc from_le(i: u16) -> u16 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc from_le(i: i16) -> i16 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc from_le(i: u32) -> u32 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc from_le(i: i32) -> i32 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc from_le(i: u64) -> u64 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc from_le(i: i64) -> i64 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc from_le(i: u128) -> u128 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc from_le(i: i128) -> i128 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc from_le(i: uint) -> uint { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc from_le(i: int) -> int { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
from_le :: proc(i: u8) -> u8 { return i; }
from_le :: proc(i: i8) -> i8 { return i; }
from_le :: proc(i: u16) -> u16 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
from_le :: proc(i: i16) -> i16 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
from_le :: proc(i: u32) -> u32 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
from_le :: proc(i: i32) -> i32 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
from_le :: proc(i: u64) -> u64 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
from_le :: proc(i: i64) -> i64 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
from_le :: proc(i: u128) -> u128 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
from_le :: proc(i: i128) -> i128 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
from_le :: proc(i: uint) -> uint { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
from_le :: proc(i: int) -> int { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc to_be(i: u8) -> u8 { return i; }
proc to_be(i: i8) -> i8 { return i; }
proc to_be(i: u16) -> u16 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc to_be(i: i16) -> i16 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc to_be(i: u32) -> u32 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc to_be(i: i32) -> i32 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc to_be(i: u64) -> u64 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc to_be(i: i64) -> i64 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc to_be(i: u128) -> u128 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc to_be(i: i128) -> i128 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc to_be(i: uint) -> uint { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc to_be(i: int) -> int { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
to_be :: proc(i: u8) -> u8 { return i; }
to_be :: proc(i: i8) -> i8 { return i; }
to_be :: proc(i: u16) -> u16 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
to_be :: proc(i: i16) -> i16 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
to_be :: proc(i: u32) -> u32 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
to_be :: proc(i: i32) -> i32 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
to_be :: proc(i: u64) -> u64 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
to_be :: proc(i: i64) -> i64 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
to_be :: proc(i: u128) -> u128 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
to_be :: proc(i: i128) -> i128 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
to_be :: proc(i: uint) -> uint { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
to_be :: proc(i: int) -> int { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } }
proc to_le(i: u8) -> u8 { return i; }
proc to_le(i: i8) -> i8 { return i; }
proc to_le(i: u16) -> u16 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc to_le(i: i16) -> i16 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc to_le(i: u32) -> u32 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc to_le(i: i32) -> i32 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc to_le(i: u64) -> u64 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc to_le(i: i64) -> i64 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc to_le(i: u128) -> u128 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc to_le(i: i128) -> i128 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc to_le(i: uint) -> uint { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc to_le(i: int) -> int { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
to_le :: proc(i: u8) -> u8 { return i; }
to_le :: proc(i: i8) -> i8 { return i; }
to_le :: proc(i: u16) -> u16 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
to_le :: proc(i: i16) -> i16 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
to_le :: proc(i: u32) -> u32 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
to_le :: proc(i: i32) -> i32 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
to_le :: proc(i: u64) -> u64 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
to_le :: proc(i: i64) -> i64 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
to_le :: proc(i: u128) -> u128 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
to_le :: proc(i: i128) -> i128 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
to_le :: proc(i: uint) -> uint { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
to_le :: proc(i: int) -> int { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } }
proc overflowing_add(lhs, rhs: u8) -> (u8, bool) { foreign __llvm_core proc op(u8, u8) -> (u8, bool) #link_name "llvm.uadd.with.overflow.i8"; return op(lhs, rhs); }
proc overflowing_add(lhs, rhs: i8) -> (i8, bool) { foreign __llvm_core proc op(i8, i8) -> (i8, bool) #link_name "llvm.sadd.with.overflow.i8"; return op(lhs, rhs); }
proc overflowing_add(lhs, rhs: u16) -> (u16, bool) { foreign __llvm_core proc op(u16, u16) -> (u16, bool) #link_name "llvm.uadd.with.overflow.i16"; return op(lhs, rhs); }
proc overflowing_add(lhs, rhs: i16) -> (i16, bool) { foreign __llvm_core proc op(i16, i16) -> (i16, bool) #link_name "llvm.sadd.with.overflow.i16"; return op(lhs, rhs); }
proc overflowing_add(lhs, rhs: u32) -> (u32, bool) { foreign __llvm_core proc op(u32, u32) -> (u32, bool) #link_name "llvm.uadd.with.overflow.i32"; return op(lhs, rhs); }
proc overflowing_add(lhs, rhs: i32) -> (i32, bool) { foreign __llvm_core proc op(i32, i32) -> (i32, bool) #link_name "llvm.sadd.with.overflow.i32"; return op(lhs, rhs); }
proc overflowing_add(lhs, rhs: u64) -> (u64, bool) { foreign __llvm_core proc op(u64, u64) -> (u64, bool) #link_name "llvm.uadd.with.overflow.i64"; return op(lhs, rhs); }
proc overflowing_add(lhs, rhs: i64) -> (i64, bool) { foreign __llvm_core proc op(i64, i64) -> (i64, bool) #link_name "llvm.sadd.with.overflow.i64"; return op(lhs, rhs); }
proc overflowing_add(lhs, rhs: u128) -> (u128, bool) { foreign __llvm_core proc op(u128, u128) -> (u128, bool) #link_name "llvm.uadd.with.overflow.i128"; return op(lhs, rhs); }
proc overflowing_add(lhs, rhs: i128) -> (i128, bool) { foreign __llvm_core proc op(i128, i128) -> (i128, bool) #link_name "llvm.sadd.with.overflow.i128"; return op(lhs, rhs); }
proc overflowing_add(lhs, rhs: uint) -> (uint, bool) {
overflowing_add :: proc(lhs, rhs: u8) -> (u8, bool) { foreign __llvm_core op :: proc(u8, u8) -> (u8, bool) #link_name "llvm.uadd.with.overflow.i8" ---; return op(lhs, rhs); }
overflowing_add :: proc(lhs, rhs: i8) -> (i8, bool) { foreign __llvm_core op :: proc(i8, i8) -> (i8, bool) #link_name "llvm.sadd.with.overflow.i8" ---; return op(lhs, rhs); }
overflowing_add :: proc(lhs, rhs: u16) -> (u16, bool) { foreign __llvm_core op :: proc(u16, u16) -> (u16, bool) #link_name "llvm.uadd.with.overflow.i16" ---; return op(lhs, rhs); }
overflowing_add :: proc(lhs, rhs: i16) -> (i16, bool) { foreign __llvm_core op :: proc(i16, i16) -> (i16, bool) #link_name "llvm.sadd.with.overflow.i16" ---; return op(lhs, rhs); }
overflowing_add :: proc(lhs, rhs: u32) -> (u32, bool) { foreign __llvm_core op :: proc(u32, u32) -> (u32, bool) #link_name "llvm.uadd.with.overflow.i32" ---; return op(lhs, rhs); }
overflowing_add :: proc(lhs, rhs: i32) -> (i32, bool) { foreign __llvm_core op :: proc(i32, i32) -> (i32, bool) #link_name "llvm.sadd.with.overflow.i32" ---; return op(lhs, rhs); }
overflowing_add :: proc(lhs, rhs: u64) -> (u64, bool) { foreign __llvm_core op :: proc(u64, u64) -> (u64, bool) #link_name "llvm.uadd.with.overflow.i64" ---; return op(lhs, rhs); }
overflowing_add :: proc(lhs, rhs: i64) -> (i64, bool) { foreign __llvm_core op :: proc(i64, i64) -> (i64, bool) #link_name "llvm.sadd.with.overflow.i64" ---; return op(lhs, rhs); }
overflowing_add :: proc(lhs, rhs: u128) -> (u128, bool) { foreign __llvm_core op :: proc(u128, u128) -> (u128, bool) #link_name "llvm.uadd.with.overflow.i128" ---; return op(lhs, rhs); }
overflowing_add :: proc(lhs, rhs: i128) -> (i128, bool) { foreign __llvm_core op :: proc(i128, i128) -> (i128, bool) #link_name "llvm.sadd.with.overflow.i128" ---; return op(lhs, rhs); }
overflowing_add :: proc(lhs, rhs: uint) -> (uint, bool) {
when size_of(uint) == size_of(u32) {
x, ok := overflowing_add(u32(lhs), u32(rhs));
return uint(x), ok;
@@ -203,7 +203,7 @@ proc overflowing_add(lhs, rhs: uint) -> (uint, bool) {
return uint(x), ok;
}
}
proc overflowing_add(lhs, rhs: int) -> (int, bool) {
overflowing_add :: proc(lhs, rhs: int) -> (int, bool) {
when size_of(int) == size_of(i32) {
x, ok := overflowing_add(i32(lhs), i32(rhs));
return int(x), ok;
@@ -213,17 +213,17 @@ proc overflowing_add(lhs, rhs: int) -> (int, bool) {
}
}
proc overflowing_sub(lhs, rhs: u8) -> (u8, bool) { foreign __llvm_core proc op(u8, u8) -> (u8, bool) #link_name "llvm.usub.with.overflow.i8"; return op(lhs, rhs); }
proc overflowing_sub(lhs, rhs: i8) -> (i8, bool) { foreign __llvm_core proc op(i8, i8) -> (i8, bool) #link_name "llvm.ssub.with.overflow.i8"; return op(lhs, rhs); }
proc overflowing_sub(lhs, rhs: u16) -> (u16, bool) { foreign __llvm_core proc op(u16, u16) -> (u16, bool) #link_name "llvm.usub.with.overflow.i16"; return op(lhs, rhs); }
proc overflowing_sub(lhs, rhs: i16) -> (i16, bool) { foreign __llvm_core proc op(i16, i16) -> (i16, bool) #link_name "llvm.ssub.with.overflow.i16"; return op(lhs, rhs); }
proc overflowing_sub(lhs, rhs: u32) -> (u32, bool) { foreign __llvm_core proc op(u32, u32) -> (u32, bool) #link_name "llvm.usub.with.overflow.i32"; return op(lhs, rhs); }
proc overflowing_sub(lhs, rhs: i32) -> (i32, bool) { foreign __llvm_core proc op(i32, i32) -> (i32, bool) #link_name "llvm.ssub.with.overflow.i32"; return op(lhs, rhs); }
proc overflowing_sub(lhs, rhs: u64) -> (u64, bool) { foreign __llvm_core proc op(u64, u64) -> (u64, bool) #link_name "llvm.usub.with.overflow.i64"; return op(lhs, rhs); }
proc overflowing_sub(lhs, rhs: i64) -> (i64, bool) { foreign __llvm_core proc op(i64, i64) -> (i64, bool) #link_name "llvm.ssub.with.overflow.i64"; return op(lhs, rhs); }
proc overflowing_sub(lhs, rhs: u128) -> (u128, bool) { foreign __llvm_core proc op(u128, u128) -> (u128, bool) #link_name "llvm.usub.with.overflow.i128"; return op(lhs, rhs); }
proc overflowing_sub(lhs, rhs: i128) -> (i128, bool) { foreign __llvm_core proc op(i128, i128) -> (i128, bool) #link_name "llvm.ssub.with.overflow.i128"; return op(lhs, rhs); }
proc overflowing_sub(lhs, rhs: uint) -> (uint, bool) {
overflowing_sub :: proc(lhs, rhs: u8) -> (u8, bool) { foreign __llvm_core op :: proc(u8, u8) -> (u8, bool) #link_name "llvm.usub.with.overflow.i8" ---; return op(lhs, rhs); }
overflowing_sub :: proc(lhs, rhs: i8) -> (i8, bool) { foreign __llvm_core op :: proc(i8, i8) -> (i8, bool) #link_name "llvm.ssub.with.overflow.i8" ---; return op(lhs, rhs); }
overflowing_sub :: proc(lhs, rhs: u16) -> (u16, bool) { foreign __llvm_core op :: proc(u16, u16) -> (u16, bool) #link_name "llvm.usub.with.overflow.i16" ---; return op(lhs, rhs); }
overflowing_sub :: proc(lhs, rhs: i16) -> (i16, bool) { foreign __llvm_core op :: proc(i16, i16) -> (i16, bool) #link_name "llvm.ssub.with.overflow.i16" ---; return op(lhs, rhs); }
overflowing_sub :: proc(lhs, rhs: u32) -> (u32, bool) { foreign __llvm_core op :: proc(u32, u32) -> (u32, bool) #link_name "llvm.usub.with.overflow.i32" ---; return op(lhs, rhs); }
overflowing_sub :: proc(lhs, rhs: i32) -> (i32, bool) { foreign __llvm_core op :: proc(i32, i32) -> (i32, bool) #link_name "llvm.ssub.with.overflow.i32" ---; return op(lhs, rhs); }
overflowing_sub :: proc(lhs, rhs: u64) -> (u64, bool) { foreign __llvm_core op :: proc(u64, u64) -> (u64, bool) #link_name "llvm.usub.with.overflow.i64" ---; return op(lhs, rhs); }
overflowing_sub :: proc(lhs, rhs: i64) -> (i64, bool) { foreign __llvm_core op :: proc(i64, i64) -> (i64, bool) #link_name "llvm.ssub.with.overflow.i64" ---; return op(lhs, rhs); }
overflowing_sub :: proc(lhs, rhs: u128) -> (u128, bool) { foreign __llvm_core op :: proc(u128, u128) -> (u128, bool) #link_name "llvm.usub.with.overflow.i128" ---; return op(lhs, rhs); }
overflowing_sub :: proc(lhs, rhs: i128) -> (i128, bool) { foreign __llvm_core op :: proc(i128, i128) -> (i128, bool) #link_name "llvm.ssub.with.overflow.i128" ---; return op(lhs, rhs); }
overflowing_sub :: proc(lhs, rhs: uint) -> (uint, bool) {
when size_of(uint) == size_of(u32) {
x, ok := overflowing_sub(u32(lhs), u32(rhs));
return uint(x), ok;
@@ -232,7 +232,7 @@ proc overflowing_sub(lhs, rhs: uint) -> (uint, bool) {
return uint(x), ok;
}
}
proc overflowing_sub(lhs, rhs: int) -> (int, bool) {
overflowing_sub :: proc(lhs, rhs: int) -> (int, bool) {
when size_of(int) == size_of(i32) {
x, ok := overflowing_sub(i32(lhs), i32(rhs));
return int(x), ok;
@@ -242,17 +242,17 @@ proc overflowing_sub(lhs, rhs: int) -> (int, bool) {
}
}
proc overflowing_mul(lhs, rhs: u8) -> (u8, bool) { foreign __llvm_core proc op(u8, u8) -> (u8, bool) #link_name "llvm.umul.with.overflow.i8"; return op(lhs, rhs); }
proc overflowing_mul(lhs, rhs: i8) -> (i8, bool) { foreign __llvm_core proc op(i8, i8) -> (i8, bool) #link_name "llvm.smul.with.overflow.i8"; return op(lhs, rhs); }
proc overflowing_mul(lhs, rhs: u16) -> (u16, bool) { foreign __llvm_core proc op(u16, u16) -> (u16, bool) #link_name "llvm.umul.with.overflow.i16"; return op(lhs, rhs); }
proc overflowing_mul(lhs, rhs: i16) -> (i16, bool) { foreign __llvm_core proc op(i16, i16) -> (i16, bool) #link_name "llvm.smul.with.overflow.i16"; return op(lhs, rhs); }
proc overflowing_mul(lhs, rhs: u32) -> (u32, bool) { foreign __llvm_core proc op(u32, u32) -> (u32, bool) #link_name "llvm.umul.with.overflow.i32"; return op(lhs, rhs); }
proc overflowing_mul(lhs, rhs: i32) -> (i32, bool) { foreign __llvm_core proc op(i32, i32) -> (i32, bool) #link_name "llvm.smul.with.overflow.i32"; return op(lhs, rhs); }
proc overflowing_mul(lhs, rhs: u64) -> (u64, bool) { foreign __llvm_core proc op(u64, u64) -> (u64, bool) #link_name "llvm.umul.with.overflow.i64"; return op(lhs, rhs); }
proc overflowing_mul(lhs, rhs: i64) -> (i64, bool) { foreign __llvm_core proc op(i64, i64) -> (i64, bool) #link_name "llvm.smul.with.overflow.i64"; return op(lhs, rhs); }
proc overflowing_mul(lhs, rhs: u128) -> (u128, bool) { foreign __llvm_core proc op(u128, u128) -> (u128, bool) #link_name "llvm.umul.with.overflow.i128"; return op(lhs, rhs); }
proc overflowing_mul(lhs, rhs: i128) -> (i128, bool) { foreign __llvm_core proc op(i128, i128) -> (i128, bool) #link_name "llvm.smul.with.overflow.i128"; return op(lhs, rhs); }
proc overflowing_mul(lhs, rhs: uint) -> (uint, bool) {
overflowing_mul :: proc(lhs, rhs: u8) -> (u8, bool) { foreign __llvm_core op :: proc(u8, u8) -> (u8, bool) #link_name "llvm.umul.with.overflow.i8" ---; return op(lhs, rhs); }
overflowing_mul :: proc(lhs, rhs: i8) -> (i8, bool) { foreign __llvm_core op :: proc(i8, i8) -> (i8, bool) #link_name "llvm.smul.with.overflow.i8" ---; return op(lhs, rhs); }
overflowing_mul :: proc(lhs, rhs: u16) -> (u16, bool) { foreign __llvm_core op :: proc(u16, u16) -> (u16, bool) #link_name "llvm.umul.with.overflow.i16" ---; return op(lhs, rhs); }
overflowing_mul :: proc(lhs, rhs: i16) -> (i16, bool) { foreign __llvm_core op :: proc(i16, i16) -> (i16, bool) #link_name "llvm.smul.with.overflow.i16" ---; return op(lhs, rhs); }
overflowing_mul :: proc(lhs, rhs: u32) -> (u32, bool) { foreign __llvm_core op :: proc(u32, u32) -> (u32, bool) #link_name "llvm.umul.with.overflow.i32" ---; return op(lhs, rhs); }
overflowing_mul :: proc(lhs, rhs: i32) -> (i32, bool) { foreign __llvm_core op :: proc(i32, i32) -> (i32, bool) #link_name "llvm.smul.with.overflow.i32" ---; return op(lhs, rhs); }
overflowing_mul :: proc(lhs, rhs: u64) -> (u64, bool) { foreign __llvm_core op :: proc(u64, u64) -> (u64, bool) #link_name "llvm.umul.with.overflow.i64" ---; return op(lhs, rhs); }
overflowing_mul :: proc(lhs, rhs: i64) -> (i64, bool) { foreign __llvm_core op :: proc(i64, i64) -> (i64, bool) #link_name "llvm.smul.with.overflow.i64" ---; return op(lhs, rhs); }
overflowing_mul :: proc(lhs, rhs: u128) -> (u128, bool) { foreign __llvm_core op :: proc(u128, u128) -> (u128, bool) #link_name "llvm.umul.with.overflow.i128" ---; return op(lhs, rhs); }
overflowing_mul :: proc(lhs, rhs: i128) -> (i128, bool) { foreign __llvm_core op :: proc(i128, i128) -> (i128, bool) #link_name "llvm.smul.with.overflow.i128" ---; return op(lhs, rhs); }
overflowing_mul :: proc(lhs, rhs: uint) -> (uint, bool) {
when size_of(uint) == size_of(u32) {
x, ok := overflowing_mul(u32(lhs), u32(rhs));
return uint(x), ok;
@@ -261,7 +261,7 @@ proc overflowing_mul(lhs, rhs: uint) -> (uint, bool) {
return uint(x), ok;
}
}
proc overflowing_mul(lhs, rhs: int) -> (int, bool) {
overflowing_mul :: proc(lhs, rhs: int) -> (int, bool) {
when size_of(int) == size_of(i32) {
x, ok := overflowing_mul(i32(lhs), i32(rhs));
return int(x), ok;
@@ -271,15 +271,15 @@ proc overflowing_mul(lhs, rhs: int) -> (int, bool) {
}
}
proc is_power_of_two(i: u8) -> bool { return i > 0 && (i & (i-1)) == 0; }
proc is_power_of_two(i: i8) -> bool { return i > 0 && (i & (i-1)) == 0; }
proc is_power_of_two(i: u16) -> bool { return i > 0 && (i & (i-1)) == 0; }
proc is_power_of_two(i: i16) -> bool { return i > 0 && (i & (i-1)) == 0; }
proc is_power_of_two(i: u32) -> bool { return i > 0 && (i & (i-1)) == 0; }
proc is_power_of_two(i: i32) -> bool { return i > 0 && (i & (i-1)) == 0; }
proc is_power_of_two(i: u64) -> bool { return i > 0 && (i & (i-1)) == 0; }
proc is_power_of_two(i: i64) -> bool { return i > 0 && (i & (i-1)) == 0; }
proc is_power_of_two(i: u128) -> bool { return i > 0 && (i & (i-1)) == 0; }
proc is_power_of_two(i: i128) -> bool { return i > 0 && (i & (i-1)) == 0; }
proc is_power_of_two(i: uint) -> bool { return i > 0 && (i & (i-1)) == 0; }
proc is_power_of_two(i: int) -> bool { return i > 0 && (i & (i-1)) == 0; }
is_power_of_two :: proc(i: u8) -> bool { return i > 0 && (i & (i-1)) == 0; }
is_power_of_two :: proc(i: i8) -> bool { return i > 0 && (i & (i-1)) == 0; }
is_power_of_two :: proc(i: u16) -> bool { return i > 0 && (i & (i-1)) == 0; }
is_power_of_two :: proc(i: i16) -> bool { return i > 0 && (i & (i-1)) == 0; }
is_power_of_two :: proc(i: u32) -> bool { return i > 0 && (i & (i-1)) == 0; }
is_power_of_two :: proc(i: i32) -> bool { return i > 0 && (i & (i-1)) == 0; }
is_power_of_two :: proc(i: u64) -> bool { return i > 0 && (i & (i-1)) == 0; }
is_power_of_two :: proc(i: i64) -> bool { return i > 0 && (i & (i-1)) == 0; }
is_power_of_two :: proc(i: u128) -> bool { return i > 0 && (i & (i-1)) == 0; }
is_power_of_two :: proc(i: i128) -> bool { return i > 0 && (i & (i-1)) == 0; }
is_power_of_two :: proc(i: uint) -> bool { return i > 0 && (i & (i-1)) == 0; }
is_power_of_two :: proc(i: int) -> bool { return i > 0 && (i & (i-1)) == 0; }

View File

@@ -9,8 +9,8 @@ Decimal :: struct {
neg, trunc: bool,
}
proc decimal_to_string(buf: []u8, a: ^Decimal) -> string {
proc digit_zero(buf: []u8) -> int {
decimal_to_string :: proc(buf: []u8, a: ^Decimal) -> string {
digit_zero :: proc(buf: []u8) -> int {
for _, i in buf -> buf[i] = '0';
return len(buf);
}
@@ -46,7 +46,7 @@ proc decimal_to_string(buf: []u8, a: ^Decimal) -> string {
}
// trim trailing zeros
proc trim(a: ^Decimal) {
trim :: proc(a: ^Decimal) {
for a.count > 0 && a.digits[a.count-1] == '0' {
a.count--;
}
@@ -56,7 +56,7 @@ proc trim(a: ^Decimal) {
}
proc assign(a: ^Decimal, i: u64) {
assign :: proc(a: ^Decimal, i: u64) {
buf: [32]u8;
n := 0;
for i > 0 {
@@ -78,7 +78,7 @@ proc assign(a: ^Decimal, i: u64) {
proc shift_right(a: ^Decimal, k: uint) {
shift_right :: proc(a: ^Decimal, k: uint) {
r := 0; // read index
w := 0; // write index
@@ -129,7 +129,7 @@ proc shift_right(a: ^Decimal, k: uint) {
trim(a);
}
proc shift_left(a: ^Decimal, k: uint) {
shift_left :: proc(a: ^Decimal, k: uint) {
delta := int(k/4);
r := a.count; // read index
@@ -167,7 +167,7 @@ proc shift_left(a: ^Decimal, k: uint) {
trim(a);
}
proc shift(a: ^Decimal, k: int) {
shift :: proc(a: ^Decimal, k: int) {
uint_size :: 8*size_of(uint);
max_shift :: uint_size-4;
@@ -191,7 +191,7 @@ proc shift(a: ^Decimal, k: int) {
}
}
proc can_round_up(a: ^Decimal, nd: int) -> bool {
can_round_up :: proc(a: ^Decimal, nd: int) -> bool {
if nd < 0 || nd >= a.count { return false ; }
if a.digits[nd] == '5' && nd+1 == a.count {
if a.trunc -> return true;
@@ -201,7 +201,7 @@ proc can_round_up(a: ^Decimal, nd: int) -> bool {
return a.digits[nd] >= '5';
}
proc round(a: ^Decimal, nd: int) {
round :: proc(a: ^Decimal, nd: int) {
if nd < 0 || nd >= a.count { return; }
if can_round_up(a, nd) {
round_up(a, nd);
@@ -210,7 +210,7 @@ proc round(a: ^Decimal, nd: int) {
}
}
proc round_up(a: ^Decimal, nd: int) {
round_up :: proc(a: ^Decimal, nd: int) {
if nd < 0 || nd >= a.count { return; }
for i := nd-1; i >= 0; i-- {
@@ -227,7 +227,7 @@ proc round_up(a: ^Decimal, nd: int) {
a.decimal_point++;
}
proc round_down(a: ^Decimal, nd: int) {
round_down :: proc(a: ^Decimal, nd: int) {
if nd < 0 || nd >= a.count { return; }
a.count = nd;
trim(a);
@@ -235,7 +235,7 @@ proc round_down(a: ^Decimal, nd: int) {
// Extract integer part, rounded appropriately. There are no guarantees about overflow.
proc rounded_integer(a: ^Decimal) -> u64 {
rounded_integer :: proc(a: ^Decimal) -> u64 {
if a.decimal_point > 20 {
return 0xffff_ffff_ffff_ffff;
}

View File

@@ -34,14 +34,14 @@ FmtInfo :: struct {
}
proc make_string_buffer_from_slice(b: []u8) -> StringBuffer {
make_string_buffer_from_slice :: proc(b: []u8) -> StringBuffer {
return StringBuffer.Static{b};
}
proc make_string_dynamic_buffer() -> StringBuffer {
make_string_dynamic_buffer :: proc() -> StringBuffer {
return StringBuffer.Dynamic{make([dynamic]u8)};
}
proc string_buffer_data(buf: ^StringBuffer) -> []u8 {
string_buffer_data :: proc(buf: ^StringBuffer) -> []u8 {
match b in buf {
case StringBuffer.Static:
return b.buf[..];
@@ -50,7 +50,7 @@ proc string_buffer_data(buf: ^StringBuffer) -> []u8 {
}
return nil;
}
proc string_buffer_data(buf: StringBuffer) -> []u8 {
string_buffer_data :: proc(buf: StringBuffer) -> []u8 {
match b in buf {
case StringBuffer.Static:
return b.buf[..];
@@ -59,15 +59,15 @@ proc string_buffer_data(buf: StringBuffer) -> []u8 {
}
return nil;
}
proc to_string(buf: StringBuffer) -> string {
to_string :: proc(buf: StringBuffer) -> string {
return string(string_buffer_data(buf));
}
proc write_string(buf: ^StringBuffer, s: string) {
write_string :: proc(buf: ^StringBuffer, s: string) {
write_bytes(buf, []u8(s));
}
proc write_bytes(buf: ^StringBuffer, data: []u8) {
write_bytes :: proc(buf: ^StringBuffer, data: []u8) {
match b in buf {
case StringBuffer.Static:
append(b.buf, ..data);
@@ -75,7 +75,7 @@ proc write_bytes(buf: ^StringBuffer, data: []u8) {
append(b.buf, ..data);
}
}
proc write_byte(buf: ^StringBuffer, data: u8) {
write_byte :: proc(buf: ^StringBuffer, data: u8) {
match b in buf {
case StringBuffer.Static:
append(b.buf, data);
@@ -83,7 +83,7 @@ proc write_byte(buf: ^StringBuffer, data: u8) {
append(b.buf, data);
}
}
proc write_rune(buf: ^StringBuffer, r: rune) {
write_rune :: proc(buf: ^StringBuffer, r: rune) {
if r < utf8.RUNE_SELF {
write_byte(buf, u8(r));
return;
@@ -93,12 +93,12 @@ proc write_rune(buf: ^StringBuffer, r: rune) {
write_bytes(buf, b[0..<n]);
}
proc write_int(buf: ^StringBuffer, i: i128, base: int) {
write_int :: proc(buf: ^StringBuffer, i: i128, base: int) {
b: [129]u8;
s := strconv.append_bits(b[0..<0], u128(i), base, true, 128, strconv.digits, 0);
write_string(buf, s);
}
proc write_int(buf: ^StringBuffer, i: i64, base: int) {
write_int :: proc(buf: ^StringBuffer, i: i64, base: int) {
b: [129]u8;
s := strconv.append_bits(b[0..<0], u128(i), base, true, 64, strconv.digits, 0);
write_string(buf, s);
@@ -106,7 +106,7 @@ proc write_int(buf: ^StringBuffer, i: i64, base: int) {
proc fprint(fd: os.Handle, args: ..any) -> int {
fprint :: proc(fd: os.Handle, args: ..any) -> int {
data: [_BUFFER_SIZE]u8;
buf := make_string_buffer_from_slice(data[0..<0]);
sbprint(&buf, ..args);
@@ -115,7 +115,7 @@ proc fprint(fd: os.Handle, args: ..any) -> int {
return len(res);
}
proc fprintln(fd: os.Handle, args: ..any) -> int {
fprintln :: proc(fd: os.Handle, args: ..any) -> int {
data: [_BUFFER_SIZE]u8;
buf := make_string_buffer_from_slice(data[0..<0]);
sbprintln(&buf, ..args);
@@ -123,7 +123,7 @@ proc fprintln(fd: os.Handle, args: ..any) -> int {
os.write(fd, res);
return len(res);
}
proc fprintf(fd: os.Handle, fmt: string, args: ..any) -> int {
fprintf :: proc(fd: os.Handle, fmt: string, args: ..any) -> int {
data: [_BUFFER_SIZE]u8;
buf := make_string_buffer_from_slice(data[0..<0]);
sbprintf(&buf, fmt, ..args);
@@ -134,27 +134,27 @@ proc fprintf(fd: os.Handle, fmt: string, args: ..any) -> int {
// print* procedures return the number of bytes written
proc print (args: ..any) -> int { return fprint(os.stdout, ..args); }
proc print_err (args: ..any) -> int { return fprint(os.stderr, ..args); }
proc println (args: ..any) -> int { return fprintln(os.stdout, ..args); }
proc println_err (args: ..any) -> int { return fprintln(os.stderr, ..args); }
proc printf (fmt: string, args: ..any) -> int { return fprintf(os.stdout, fmt, ..args); }
proc printf_err (fmt: string, args: ..any) -> int { return fprintf(os.stderr, fmt, ..args); }
print :: proc(args: ..any) -> int { return fprint(os.stdout, ..args); }
print_err :: proc(args: ..any) -> int { return fprint(os.stderr, ..args); }
println :: proc(args: ..any) -> int { return fprintln(os.stdout, ..args); }
println_err :: proc(args: ..any) -> int { return fprintln(os.stderr, ..args); }
printf :: proc(fmt: string, args: ..any) -> int { return fprintf(os.stdout, fmt, ..args); }
printf_err :: proc(fmt: string, args: ..any) -> int { return fprintf(os.stderr, fmt, ..args); }
// aprint* procedures return a string that was allocated with the current context
// They must be freed accordingly
proc aprint(args: ..any) -> string {
aprint :: proc(args: ..any) -> string {
buf := make_string_dynamic_buffer();
sbprint(&buf, ..args);
return to_string(buf);
}
proc aprintln(args: ..any) -> string {
aprintln :: proc(args: ..any) -> string {
buf := make_string_dynamic_buffer();
sbprintln(&buf, ..args);
return to_string(buf);
}
proc aprintf(fmt: string, args: ..any) -> string {
aprintf :: proc(fmt: string, args: ..any) -> string {
buf := make_string_dynamic_buffer();
sbprintf(&buf, fmt, ..args);
return to_string(buf);
@@ -163,15 +163,15 @@ proc aprintf(fmt: string, args: ..any) -> string {
// bprint* procedures return a string that was allocated with the current context
// They must be freed accordingly
proc bprint(buf: []u8, args: ..any) -> string {
bprint :: proc(buf: []u8, args: ..any) -> string {
sb := make_string_buffer_from_slice(buf[0..<0..<len(buf)]);
return sbprint(&sb, ..args);
}
proc bprintln(buf: []u8, args: ..any) -> string {
bprintln :: proc(buf: []u8, args: ..any) -> string {
sb := make_string_buffer_from_slice(buf[0..<0..<len(buf)]);
return sbprintln(&sb, ..args);
}
proc bprintf(buf: []u8, fmt: string, args: ..any) -> string {
bprintf :: proc(buf: []u8, fmt: string, args: ..any) -> string {
sb := make_string_buffer_from_slice(buf[0..<0..<len(buf)]);
return sbprintf(&sb, fmt, ..args);
}
@@ -181,14 +181,14 @@ proc bprintf(buf: []u8, fmt: string, args: ..any) -> string {
proc fprint_type(fd: os.Handle, info: ^TypeInfo) {
fprint_type :: proc(fd: os.Handle, info: ^TypeInfo) {
data: [_BUFFER_SIZE]u8;
buf := make_string_buffer_from_slice(data[0..<0]);
write_type(&buf, info);
os.write(fd, string_buffer_data(buf));
}
proc write_type(buf: ^StringBuffer, ti: ^TypeInfo) {
write_type :: proc(buf: ^StringBuffer, ti: ^TypeInfo) {
if ti == nil -> return;
using TypeInfo;
@@ -377,8 +377,8 @@ proc write_type(buf: ^StringBuffer, ti: ^TypeInfo) {
}
proc _parse_int(s: string, offset: int) -> (result: int, offset: int, ok: bool) {
proc is_digit(r: rune) -> bool #inline {
_parse_int :: proc(s: string, offset: int) -> (result: int, offset: int, ok: bool) {
is_digit :: proc(r: rune) -> bool #inline {
return '0' <= r && r <= '9';
}
@@ -398,8 +398,8 @@ proc _parse_int(s: string, offset: int) -> (result: int, offset: int, ok: bool)
return result, offset+i, i != 0;
}
proc _arg_number(fi: ^FmtInfo, arg_index: int, format: string, offset, arg_count: int) -> (index, offset: int, ok: bool) {
proc parse_arg_number(format: string) -> (int, int, bool) {
_arg_number :: proc(fi: ^FmtInfo, arg_index: int, format: string, offset, arg_count: int) -> (index, offset: int, ok: bool) {
parse_arg_number :: proc(format: string) -> (int, int, bool) {
if len(format) < 3 {
return 0, 1, false;
}
@@ -430,7 +430,7 @@ proc _arg_number(fi: ^FmtInfo, arg_index: int, format: string, offset, arg_count
return arg_index, offset+width, false;
}
proc int_from_arg(args: []any, arg_index: int) -> (int, int, bool) {
int_from_arg :: proc(args: []any, arg_index: int) -> (int, int, bool) {
num := 0;
new_arg_index := arg_index;
ok := true;
@@ -456,7 +456,7 @@ proc int_from_arg(args: []any, arg_index: int) -> (int, int, bool) {
}
proc fmt_bad_verb(using fi: ^FmtInfo, verb: rune) {
fmt_bad_verb :: proc(using fi: ^FmtInfo, verb: rune) {
assert(verb != 'v');
write_string(buf, "%!");
write_rune(buf, verb);
@@ -471,7 +471,7 @@ proc fmt_bad_verb(using fi: ^FmtInfo, verb: rune) {
write_byte(buf, ')');
}
proc fmt_bool(using fi: ^FmtInfo, b: bool, verb: rune) {
fmt_bool :: proc(using fi: ^FmtInfo, b: bool, verb: rune) {
match verb {
case 't', 'v':
write_string(buf, b ? "true" : "false");
@@ -481,7 +481,7 @@ proc fmt_bool(using fi: ^FmtInfo, b: bool, verb: rune) {
}
proc fmt_write_padding(fi: ^FmtInfo, width: int) {
fmt_write_padding :: proc(fi: ^FmtInfo, width: int) {
if width <= 0 -> return;
pad_byte: u8 = fi.space ? ' ' : '0';
@@ -491,7 +491,7 @@ proc fmt_write_padding(fi: ^FmtInfo, width: int) {
for _ in 0..<count -> write_byte(fi.buf, pad_byte);
}
proc _fmt_int(fi: ^FmtInfo, u: u128, base: int, is_signed: bool, bit_size: int, digits: string) {
_fmt_int :: proc(fi: ^FmtInfo, u: u128, base: int, is_signed: bool, bit_size: int, digits: string) {
_, neg := strconv.is_integer_negative(u128(u), is_signed, bit_size);
BUF_SIZE :: 256;
@@ -563,7 +563,7 @@ proc _fmt_int(fi: ^FmtInfo, u: u128, base: int, is_signed: bool, bit_size: int,
__DIGITS_LOWER := "0123456789abcdefx";
__DIGITS_UPPER := "0123456789ABCDEFX";
proc fmt_rune(fi: ^FmtInfo, r: rune, verb: rune) {
fmt_rune :: proc(fi: ^FmtInfo, r: rune, verb: rune) {
match verb {
case 'c', 'r', 'v':
write_rune(fi.buf, r);
@@ -572,7 +572,7 @@ proc fmt_rune(fi: ^FmtInfo, r: rune, verb: rune) {
}
}
proc fmt_int(fi: ^FmtInfo, u: u128, is_signed: bool, bit_size: int, verb: rune) {
fmt_int :: proc(fi: ^FmtInfo, u: u128, is_signed: bool, bit_size: int, verb: rune) {
match verb {
case 'v': _fmt_int(fi, u, 10, is_signed, bit_size, __DIGITS_LOWER);
case 'b': _fmt_int(fi, u, 2, is_signed, bit_size, __DIGITS_LOWER);
@@ -596,7 +596,7 @@ proc fmt_int(fi: ^FmtInfo, u: u128, is_signed: bool, bit_size: int, verb: rune)
}
}
proc _pad(fi: ^FmtInfo, s: string) {
_pad :: proc(fi: ^FmtInfo, s: string) {
if !fi.width_set {
write_string(fi.buf, s);
return;
@@ -611,7 +611,7 @@ proc _pad(fi: ^FmtInfo, s: string) {
}
}
proc fmt_float(fi: ^FmtInfo, v: f64, bit_size: int, verb: rune) {
fmt_float :: proc(fi: ^FmtInfo, v: f64, bit_size: int, verb: rune) {
match verb {
// case 'e', 'E', 'f', 'F', 'g', 'G', 'v':
// case 'f', 'F', 'v':
@@ -653,7 +653,7 @@ proc fmt_float(fi: ^FmtInfo, v: f64, bit_size: int, verb: rune) {
fmt_bad_verb(fi, verb);
}
}
proc fmt_string(fi: ^FmtInfo, s: string, verb: rune) {
fmt_string :: proc(fi: ^FmtInfo, s: string, verb: rune) {
match verb {
case 's', 'v':
write_string(fi.buf, s);
@@ -673,7 +673,7 @@ proc fmt_string(fi: ^FmtInfo, s: string, verb: rune) {
}
}
proc fmt_pointer(fi: ^FmtInfo, p: rawptr, verb: rune) {
fmt_pointer :: proc(fi: ^FmtInfo, p: rawptr, verb: rune) {
match verb {
case 'p', 'v':
// Okay
@@ -688,7 +688,7 @@ proc fmt_pointer(fi: ^FmtInfo, p: rawptr, verb: rune) {
_fmt_int(fi, u, 16, false, 8*size_of(rawptr), __DIGITS_UPPER);
}
proc fmt_enum(fi: ^FmtInfo, v: any, verb: rune) {
fmt_enum :: proc(fi: ^FmtInfo, v: any, verb: rune) {
if v.type_info == nil || v.data == nil {
write_string(fi.buf, "<nil>");
return;
@@ -759,7 +759,7 @@ proc fmt_enum(fi: ^FmtInfo, v: any, verb: rune) {
}
proc fmt_value(fi: ^FmtInfo, v: any, verb: rune) {
fmt_value :: proc(fi: ^FmtInfo, v: any, verb: rune) {
if v.data == nil || v.type_info == nil {
write_string(fi.buf, "<nil>");
return;
@@ -926,7 +926,7 @@ proc fmt_value(fi: ^FmtInfo, v: any, verb: rune) {
}
}
proc fmt_complex(fi: ^FmtInfo, c: complex128, bits: int, verb: rune) {
fmt_complex :: proc(fi: ^FmtInfo, c: complex128, bits: int, verb: rune) {
match verb {
case 'f', 'F', 'v':
r, i := real(c), imag(c);
@@ -943,15 +943,15 @@ proc fmt_complex(fi: ^FmtInfo, c: complex128, bits: int, verb: rune) {
}
}
proc _u128_to_lo_hi(a: u128) -> (lo, hi: u64) { return u64(a), u64(a>>64); }
proc _i128_to_lo_hi(a: u128) -> (lo: u64 hi: i64) { return u64(a), i64(a>>64); }
_u128_to_lo_hi :: proc(a: u128) -> (lo, hi: u64) { return u64(a), u64(a>>64); }
_i128_to_lo_hi :: proc(a: u128) -> (lo: u64 hi: i64) { return u64(a), i64(a>>64); }
proc do_foo(fi: ^FmtInfo, f: f64) {
do_foo :: proc(fi: ^FmtInfo, f: f64) {
fmt_string(fi, "Hellope$%!", 'v');
}
proc fmt_arg(fi: ^FmtInfo, arg: any, verb: rune) {
fmt_arg :: proc(fi: ^FmtInfo, arg: any, verb: rune) {
if arg == nil {
write_string(fi.buf, "<nil>");
return;
@@ -1006,7 +1006,7 @@ proc fmt_arg(fi: ^FmtInfo, arg: any, verb: rune) {
proc sbprint(buf: ^StringBuffer, args: ..any) -> string {
sbprint :: proc(buf: ^StringBuffer, args: ..any) -> string {
fi: FmtInfo;
prev_string := false;
@@ -1023,7 +1023,7 @@ proc sbprint(buf: ^StringBuffer, args: ..any) -> string {
return to_string(buf^);
}
proc sbprintln(buf: ^StringBuffer, args: ..any) -> string {
sbprintln :: proc(buf: ^StringBuffer, args: ..any) -> string {
fi: FmtInfo;
fi.buf = buf;
@@ -1036,7 +1036,7 @@ proc sbprintln(buf: ^StringBuffer, args: ..any) -> string {
return to_string(buf^);
}
proc sbprintf(b: ^StringBuffer, fmt: string, args: ..any) -> string {
sbprintf :: proc(b: ^StringBuffer, fmt: string, args: ..any) -> string {
fi: FmtInfo;
arg_index: int = 0;
end := len(fmt);

View File

@@ -1,11 +1,11 @@
proc crc32(data: []u8) -> u32 {
crc32 :: proc(data: []u8) -> u32 {
result := ~u32(0);
for b in data {
result = result>>8 ~ _crc32_table[(result ~ u32(b)) & 0xff];
}
return ~result;
}
proc crc64(data: []u8) -> u64 {
crc64 :: proc(data: []u8) -> u64 {
result := ~u64(0);
for b in data {
result = result>>8 ~ _crc64_table[(result ~ u64(b)) & 0xff];
@@ -13,7 +13,7 @@ proc crc64(data: []u8) -> u64 {
return ~result;
}
proc fnv32(data: []u8) -> u32 {
fnv32 :: proc(data: []u8) -> u32 {
h: u32 = 0x811c9dc5;
for b in data {
h = (h * 0x01000193) ~ u32(b);
@@ -21,7 +21,7 @@ proc fnv32(data: []u8) -> u32 {
return h;
}
proc fnv64(data: []u8) -> u64 {
fnv64 :: proc(data: []u8) -> u64 {
h: u64 = 0xcbf29ce484222325;
for b in data {
h = (h * 0x100000001b3) ~ u64(b);
@@ -29,7 +29,7 @@ proc fnv64(data: []u8) -> u64 {
return h;
}
proc fnv32a(data: []u8) -> u32 {
fnv32a :: proc(data: []u8) -> u32 {
h: u32 = 0x811c9dc5;
for b in data {
h = (h ~ u32(b)) * 0x01000193;
@@ -37,7 +37,7 @@ proc fnv32a(data: []u8) -> u32 {
return h;
}
proc fnv64a(data: []u8) -> u64 {
fnv64a :: proc(data: []u8) -> u64 {
h: u64 = 0xcbf29ce484222325;
for b in data {
h = (h ~ u64(b)) * 0x100000001b3;
@@ -45,7 +45,7 @@ proc fnv64a(data: []u8) -> u64 {
return h;
}
proc murmur32(data: []u8) -> u32 {
murmur32 :: proc(data: []u8) -> u32 {
c1_32: u32 : 0xcc9e2d51;
c2_32: u32 : 0x1b873593;
@@ -94,7 +94,7 @@ proc murmur32(data: []u8) -> u32 {
return h1;
}
proc murmur64(data: []u8) -> u64 {
murmur64 :: proc(data: []u8) -> u64 {
SEED :: 0x9747b28c;
when size_of(int) == 8 {

View File

@@ -28,38 +28,38 @@ Mat4 :: [4][4]f32;
Complex :: complex64;
foreign __llvm_core {
proc sqrt(x: f32) -> f32 #link_name "llvm.sqrt.f32";
proc sqrt(x: f64) -> f64 #link_name "llvm.sqrt.f64";
sqrt :: proc(x: f32) -> f32 #link_name "llvm.sqrt.f32" ---;
sqrt :: proc(x: f64) -> f64 #link_name "llvm.sqrt.f64" ---;
proc sin (θ: f32) -> f32 #link_name "llvm.sin.f32";
proc sin (θ: f64) -> f64 #link_name "llvm.sin.f64";
sin :: proc(θ: f32) -> f32 #link_name "llvm.sin.f32" ---;
sin :: proc(θ: f64) -> f64 #link_name "llvm.sin.f64" ---;
proc cos (θ: f32) -> f32 #link_name "llvm.cos.f32";
proc cos (θ: f64) -> f64 #link_name "llvm.cos.f64";
cos :: proc(θ: f32) -> f32 #link_name "llvm.cos.f32" ---;
cos :: proc(θ: f64) -> f64 #link_name "llvm.cos.f64" ---;
proc pow (x, power: f32) -> f32 #link_name "llvm.pow.f32";
proc pow (x, power: f64) -> f64 #link_name "llvm.pow.f64";
pow :: proc(x, power: f32) -> f32 #link_name "llvm.pow.f32" ---;
pow :: proc(x, power: f64) -> f64 #link_name "llvm.pow.f64" ---;
proc fmuladd(a, b, c: f32) -> f32 #link_name "llvm.fmuladd.f32";
proc fmuladd(a, b, c: f64) -> f64 #link_name "llvm.fmuladd.f64";
fmuladd :: proc(a, b, c: f32) -> f32 #link_name "llvm.fmuladd.f32" ---;
fmuladd :: proc(a, b, c: f64) -> f64 #link_name "llvm.fmuladd.f64" ---;
}
proc tan (θ: f32) -> f32 #inline { return sin(θ)/cos(θ); }
proc tan (θ: f64) -> f64 #inline { return sin(θ)/cos(θ); }
tan :: proc(θ: f32) -> f32 #inline { return sin(θ)/cos(θ); }
tan :: proc(θ: f64) -> f64 #inline { return sin(θ)/cos(θ); }
proc lerp (a, b, t: f32) -> (x: f32) { return a*(1-t) + b*t; }
proc lerp (a, b, t: f64) -> (x: f64) { return a*(1-t) + b*t; }
proc unlerp(a, b, x: f32) -> (t: f32) { return (x-a)/(b-a); }
proc unlerp(a, b, x: f64) -> (t: f64) { return (x-a)/(b-a); }
lerp :: proc(a, b, t: f32) -> (x: f32) { return a*(1-t) + b*t; }
lerp :: proc(a, b, t: f64) -> (x: f64) { return a*(1-t) + b*t; }
unlerp :: proc(a, b, x: f32) -> (t: f32) { return (x-a)/(b-a); }
unlerp :: proc(a, b, x: f64) -> (t: f64) { return (x-a)/(b-a); }
proc sign(x: f32) -> f32 { return x >= 0 ? +1 : -1; }
proc sign(x: f64) -> f64 { return x >= 0 ? +1 : -1; }
sign :: proc(x: f32) -> f32 { return x >= 0 ? +1 : -1; }
sign :: proc(x: f64) -> f64 { return x >= 0 ? +1 : -1; }
proc copy_sign(x, y: f32) -> f32 {
copy_sign :: proc(x, y: f32) -> f32 {
ix := transmute(u32, x);
iy := transmute(u32, y);
ix &= 0x7fff_ffff;
@@ -67,7 +67,7 @@ proc copy_sign(x, y: f32) -> f32 {
return transmute(f32, ix);
}
proc copy_sign(x, y: f64) -> f64 {
copy_sign :: proc(x, y: f64) -> f64 {
ix := transmute(u64, x);
iy := transmute(u64, y);
ix &= 0x7fff_ffff_ffff_ff;
@@ -75,19 +75,19 @@ proc copy_sign(x, y: f64) -> f64 {
return transmute(f64, ix);
}
proc round (x: f32) -> f32 { return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5); }
proc round (x: f64) -> f64 { return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5); }
round :: proc(x: f32) -> f32 { return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5); }
round :: proc(x: f64) -> f64 { return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5); }
proc floor (x: f32) -> f32 { return x >= 0 ? f32(i64(x)) : f32(i64(x-0.5)); } // TODO: Get accurate versions
proc floor (x: f64) -> f64 { return x >= 0 ? f64(i64(x)) : f64(i64(x-0.5)); } // TODO: Get accurate versions
floor :: proc(x: f32) -> f32 { return x >= 0 ? f32(i64(x)) : f32(i64(x-0.5)); } // TODO: Get accurate versions
floor :: proc(x: f64) -> f64 { return x >= 0 ? f64(i64(x)) : f64(i64(x-0.5)); } // TODO: Get accurate versions
proc ceil (x: f32) -> f32 { return x < 0 ? f32(i64(x)) : f32(i64(x+1)); } // TODO: Get accurate versions
proc ceil (x: f64) -> f64 { return x < 0 ? f64(i64(x)) : f64(i64(x+1)); } // TODO: Get accurate versions
ceil :: proc(x: f32) -> f32 { return x < 0 ? f32(i64(x)) : f32(i64(x+1)); } // TODO: Get accurate versions
ceil :: proc(x: f64) -> f64 { return x < 0 ? f64(i64(x)) : f64(i64(x+1)); } // TODO: Get accurate versions
proc remainder(x, y: f32) -> f32 { return x - round(x/y) * y; }
proc remainder(x, y: f64) -> f64 { return x - round(x/y) * y; }
remainder :: proc(x, y: f32) -> f32 { return x - round(x/y) * y; }
remainder :: proc(x, y: f64) -> f64 { return x - round(x/y) * y; }
proc mod(x, y: f32) -> f32 {
mod :: proc(x, y: f32) -> f32 {
result: f32;
y = abs(y);
result = remainder(abs(x), y);
@@ -96,7 +96,7 @@ proc mod(x, y: f32) -> f32 {
}
return copy_sign(result, x);
}
proc mod(x, y: f64) -> f64 {
mod :: proc(x, y: f64) -> f64 {
result: f64;
y = abs(y);
result = remainder(abs(x), y);
@@ -107,31 +107,31 @@ proc mod(x, y: f64) -> f64 {
}
proc to_radians(degrees: f32) -> f32 { return degrees * TAU / 360; }
proc to_degrees(radians: f32) -> f32 { return radians * 360 / TAU; }
to_radians :: proc(degrees: f32) -> f32 { return degrees * TAU / 360; }
to_degrees :: proc(radians: f32) -> f32 { return radians * 360 / TAU; }
proc dot(a, b: Vec2) -> f32 { c := a*b; return c.x + c.y; }
proc dot(a, b: Vec3) -> f32 { c := a*b; return c.x + c.y + c.z; }
proc dot(a, b: Vec4) -> f32 { c := a*b; return c.x + c.y + c.z + c.w; }
dot :: proc(a, b: Vec2) -> f32 { c := a*b; return c.x + c.y; }
dot :: proc(a, b: Vec3) -> f32 { c := a*b; return c.x + c.y + c.z; }
dot :: proc(a, b: Vec4) -> f32 { c := a*b; return c.x + c.y + c.z + c.w; }
proc cross(x, y: Vec3) -> Vec3 {
cross :: proc(x, y: Vec3) -> Vec3 {
a := swizzle(x, 1, 2, 0) * swizzle(y, 2, 0, 1);
b := swizzle(x, 2, 0, 1) * swizzle(y, 1, 2, 0);
return a - b;
}
proc mag(v: Vec2) -> f32 { return sqrt(dot(v, v)); }
proc mag(v: Vec3) -> f32 { return sqrt(dot(v, v)); }
proc mag(v: Vec4) -> f32 { return sqrt(dot(v, v)); }
mag :: proc(v: Vec2) -> f32 { return sqrt(dot(v, v)); }
mag :: proc(v: Vec3) -> f32 { return sqrt(dot(v, v)); }
mag :: proc(v: Vec4) -> f32 { return sqrt(dot(v, v)); }
proc norm(v: Vec2) -> Vec2 { return v / mag(v); }
proc norm(v: Vec3) -> Vec3 { return v / mag(v); }
proc norm(v: Vec4) -> Vec4 { return v / mag(v); }
norm :: proc(v: Vec2) -> Vec2 { return v / mag(v); }
norm :: proc(v: Vec3) -> Vec3 { return v / mag(v); }
norm :: proc(v: Vec4) -> Vec4 { return v / mag(v); }
proc norm0(v: Vec2) -> Vec2 {
norm0 :: proc(v: Vec2) -> Vec2 {
m := mag(v);
if m == 0 {
return 0;
@@ -139,7 +139,7 @@ proc norm0(v: Vec2) -> Vec2 {
return v / m;
}
proc norm0(v: Vec3) -> Vec3 {
norm0 :: proc(v: Vec3) -> Vec3 {
m := mag(v);
if m == 0 {
return 0;
@@ -147,7 +147,7 @@ proc norm0(v: Vec3) -> Vec3 {
return v / m;
}
proc norm0(v: Vec4) -> Vec4 {
norm0 :: proc(v: Vec4) -> Vec4 {
m := mag(v);
if m == 0 {
return 0;
@@ -157,7 +157,7 @@ proc norm0(v: Vec4) -> Vec4 {
proc mat4_identity() -> Mat4 {
mat4_identity :: proc() -> Mat4 {
return Mat4{
{1, 0, 0, 0},
{0, 1, 0, 0},
@@ -166,7 +166,7 @@ proc mat4_identity() -> Mat4 {
};
}
proc mat4_transpose(m: Mat4) -> Mat4 {
mat4_transpose :: proc(m: Mat4) -> Mat4 {
for j in 0..<4 {
for i in 0..<4 {
m[i][j], m[j][i] = m[j][i], m[i][j];
@@ -175,7 +175,7 @@ proc mat4_transpose(m: Mat4) -> Mat4 {
return m;
}
proc mul(a, b: Mat4) -> Mat4 {
mul :: proc(a, b: Mat4) -> Mat4 {
c: Mat4;
for j in 0..<4 {
for i in 0..<4 {
@@ -188,7 +188,7 @@ proc mul(a, b: Mat4) -> Mat4 {
return c;
}
proc mul(m: Mat4, v: Vec4) -> Vec4 {
mul :: proc(m: Mat4, v: Vec4) -> Vec4 {
return Vec4{
m[0][0]*v.x + m[1][0]*v.y + m[2][0]*v.z + m[3][0]*v.w,
m[0][1]*v.x + m[1][1]*v.y + m[2][1]*v.z + m[3][1]*v.w,
@@ -197,7 +197,7 @@ proc mul(m: Mat4, v: Vec4) -> Vec4 {
};
}
proc inverse(m: Mat4) -> Mat4 {
inverse :: proc(m: Mat4) -> Mat4 {
o: Mat4;
sf00 := m[2][2] * m[3][3] - m[3][2] * m[2][3];
@@ -267,7 +267,7 @@ proc inverse(m: Mat4) -> Mat4 {
}
proc mat4_translate(v: Vec3) -> Mat4 {
mat4_translate :: proc(v: Vec3) -> Mat4 {
m := mat4_identity();
m[3][0] = v.x;
m[3][1] = v.y;
@@ -276,7 +276,7 @@ proc mat4_translate(v: Vec3) -> Mat4 {
return m;
}
proc mat4_rotate(v: Vec3, angle_radians: f32) -> Mat4 {
mat4_rotate :: proc(v: Vec3, angle_radians: f32) -> Mat4 {
c := cos(angle_radians);
s := sin(angle_radians);
@@ -303,14 +303,14 @@ proc mat4_rotate(v: Vec3, angle_radians: f32) -> Mat4 {
return rot;
}
proc scale(m: Mat4, v: Vec3) -> Mat4 {
scale :: proc(m: Mat4, v: Vec3) -> Mat4 {
m[0][0] *= v.x;
m[1][1] *= v.y;
m[2][2] *= v.z;
return m;
}
proc scale(m: Mat4, s: f32) -> Mat4 {
scale :: proc(m: Mat4, s: f32) -> Mat4 {
m[0][0] *= s;
m[1][1] *= s;
m[2][2] *= s;
@@ -318,7 +318,7 @@ proc scale(m: Mat4, s: f32) -> Mat4 {
}
proc look_at(eye, centre, up: Vec3) -> Mat4 {
look_at :: proc(eye, centre, up: Vec3) -> Mat4 {
f := norm(centre - eye);
s := norm(cross(f, up));
u := cross(s, f);
@@ -331,7 +331,7 @@ proc look_at(eye, centre, up: Vec3) -> Mat4 {
};
}
proc perspective(fovy, aspect, near, far: f32) -> Mat4 {
perspective :: proc(fovy, aspect, near, far: f32) -> Mat4 {
m: Mat4;
tan_half_fovy := tan(0.5 * fovy);
@@ -344,7 +344,7 @@ proc perspective(fovy, aspect, near, far: f32) -> Mat4 {
}
proc ortho3d(left, right, bottom, top, near, far: f32) -> Mat4 {
ortho3d :: proc(left, right, bottom, top, near, far: f32) -> Mat4 {
m := mat4_identity();
m[0][0] = +2.0 / (right - left);
m[1][1] = +2.0 / (top - bottom);

View File

@@ -3,42 +3,42 @@ import (
"os.odin";
)
foreign __llvm_core {
proc swap(b: u16) -> u16 #link_name "llvm.bswap.i16";
proc swap(b: u32) -> u32 #link_name "llvm.bswap.i32";
proc swap(b: u64) -> u64 #link_name "llvm.bswap.i64";
swap :: proc(b: u16) -> u16 #link_name "llvm.bswap.i16" ---;
swap :: proc(b: u32) -> u32 #link_name "llvm.bswap.i32" ---;
swap :: proc(b: u64) -> u64 #link_name "llvm.bswap.i64" ---;
}
proc set(data: rawptr, value: i32, len: int) -> rawptr {
set :: proc(data: rawptr, value: i32, len: int) -> rawptr {
return __mem_set(data, value, len);
}
proc zero(data: rawptr, len: int) -> rawptr {
zero :: proc(data: rawptr, len: int) -> rawptr {
return __mem_zero(data, len);
}
proc copy(dst, src: rawptr, len: int) -> rawptr {
copy :: proc(dst, src: rawptr, len: int) -> rawptr {
return __mem_copy(dst, src, len);
}
proc copy_non_overlapping(dst, src: rawptr, len: int) -> rawptr {
copy_non_overlapping :: proc(dst, src: rawptr, len: int) -> rawptr {
return __mem_copy_non_overlapping(dst, src, len);
}
proc compare(a, b: []u8) -> int {
compare :: proc(a, b: []u8) -> int {
return __mem_compare(&a[0], &b[0], min(len(a), len(b)));
}
proc kilobytes(x: int) -> int #inline { return (x) * 1024; }
proc megabytes(x: int) -> int #inline { return kilobytes(x) * 1024; }
proc gigabytes(x: int) -> int #inline { return megabytes(x) * 1024; }
proc terabytes(x: int) -> int #inline { return gigabytes(x) * 1024; }
kilobytes :: proc(x: int) -> int #inline { return (x) * 1024; }
megabytes :: proc(x: int) -> int #inline { return kilobytes(x) * 1024; }
gigabytes :: proc(x: int) -> int #inline { return megabytes(x) * 1024; }
terabytes :: proc(x: int) -> int #inline { return gigabytes(x) * 1024; }
proc is_power_of_two(x: int) -> bool {
is_power_of_two :: proc(x: int) -> bool {
if x <= 0 {
return false;
}
return (x & (x-1)) == 0;
}
proc align_forward(ptr: rawptr, align: int) -> rawptr {
align_forward :: proc(ptr: rawptr, align: int) -> rawptr {
assert(is_power_of_two(align));
a := uint(align);
@@ -56,7 +56,7 @@ AllocationHeader :: struct {
size: int,
}
proc allocation_header_fill(header: ^AllocationHeader, data: rawptr, size: int) {
allocation_header_fill :: proc(header: ^AllocationHeader, data: rawptr, size: int) {
header.size = size;
ptr := ^int(header+1);
@@ -64,7 +64,7 @@ proc allocation_header_fill(header: ^AllocationHeader, data: rawptr, size: int)
(ptr+i)^ = -1;
}
}
proc allocation_header(data: rawptr) -> ^AllocationHeader {
allocation_header :: proc(data: rawptr) -> ^AllocationHeader {
if data == nil {
return nil;
}
@@ -97,19 +97,19 @@ ArenaTempMemory :: struct {
proc init_arena_from_memory(using a: ^Arena, data: []u8) {
init_arena_from_memory :: proc(using a: ^Arena, data: []u8) {
backing = Allocator{};
memory = data[0..<0];
temp_count = 0;
}
proc init_arena_from_context(using a: ^Arena, size: int) {
init_arena_from_context :: proc(using a: ^Arena, size: int) {
backing = context.allocator;
memory = make([]u8, size);
temp_count = 0;
}
proc free_arena(using a: ^Arena) {
free_arena :: proc(using a: ^Arena) {
if backing.procedure != nil {
push_allocator backing {
free(memory);
@@ -119,14 +119,14 @@ proc free_arena(using a: ^Arena) {
}
}
proc arena_allocator(arena: ^Arena) -> Allocator {
arena_allocator :: proc(arena: ^Arena) -> Allocator {
return Allocator{
procedure = arena_allocator_proc,
data = arena,
};
}
proc arena_allocator_proc(allocator_data: rawptr, mode: AllocatorMode,
arena_allocator_proc :: proc(allocator_data: rawptr, mode: AllocatorMode,
size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
using AllocatorMode;
@@ -161,7 +161,7 @@ proc arena_allocator_proc(allocator_data: rawptr, mode: AllocatorMode,
return nil;
}
proc begin_arena_temp_memory(a: ^Arena) -> ArenaTempMemory {
begin_arena_temp_memory :: proc(a: ^Arena) -> ArenaTempMemory {
tmp: ArenaTempMemory;
tmp.arena = a;
tmp.original_count = len(a.memory);
@@ -169,7 +169,7 @@ proc begin_arena_temp_memory(a: ^Arena) -> ArenaTempMemory {
return tmp;
}
proc end_arena_temp_memory(using tmp: ArenaTempMemory) {
end_arena_temp_memory :: proc(using tmp: ArenaTempMemory) {
assert(len(arena.memory) >= original_count);
assert(arena.temp_count > 0);
arena.memory = arena.memory[0..<original_count];
@@ -182,8 +182,8 @@ proc end_arena_temp_memory(using tmp: ArenaTempMemory) {
proc align_of_type_info(type_info: ^TypeInfo) -> int {
proc prev_pow2(n: i64) -> i64 {
align_of_type_info :: proc(type_info: ^TypeInfo) -> int {
prev_pow2 :: proc(n: i64) -> i64 {
if n <= 0 {
return 0;
}
@@ -244,12 +244,12 @@ proc align_of_type_info(type_info: ^TypeInfo) -> int {
return 0;
}
proc align_formula(size, align: int) -> int {
align_formula :: proc(size, align: int) -> int {
result := size + align-1;
return result - result%align;
}
proc size_of_type_info(type_info: ^TypeInfo) -> int {
size_of_type_info :: proc(type_info: ^TypeInfo) -> int {
WORD_SIZE :: size_of(int);
using TypeInfo;
match info in type_info {

View File

@@ -9,39 +9,39 @@ import (
import_load "opengl_constants.odin";
foreign lib {
proc Clear (mask: u32) #link_name "glClear";
proc ClearColor (r, g, b, a: f32) #link_name "glClearColor";
proc Begin (mode: i32) #link_name "glBegin";
proc End () #link_name "glEnd";
proc Finish () #link_name "glFinish";
proc BlendFunc (sfactor, dfactor: i32) #link_name "glBlendFunc";
proc Enable (cap: i32) #link_name "glEnable";
proc Disable (cap: i32) #link_name "glDisable";
proc GenTextures (count: i32, result: ^u32) #link_name "glGenTextures";
proc DeleteTextures(count: i32, result: ^u32) #link_name "glDeleteTextures";
proc TexParameteri (target, pname, param: i32) #link_name "glTexParameteri";
proc TexParameterf (target: i32, pname: i32, param: f32) #link_name "glTexParameterf";
proc BindTexture (target: i32, texture: u32) #link_name "glBindTexture";
proc LoadIdentity () #link_name "glLoadIdentity";
proc Viewport (x, y, width, height: i32) #link_name "glViewport";
proc Ortho (left, right, bottom, top, near, far: f64) #link_name "glOrtho";
proc Color3f (r, g, b: f32) #link_name "glColor3f";
proc Vertex3f (x, y, z: f32) #link_name "glVertex3f";
proc GetError () -> i32 #link_name "glGetError";
proc GetString (name: i32) -> ^u8 #link_name "glGetString";
proc GetIntegerv (name: i32, v: ^i32) #link_name "glGetIntegerv";
proc TexCoord2f (x, y: f32) #link_name "glTexCoord2f";
proc TexImage2D (target, level, internal_format,
width, height, border,
format, type_: i32, pixels: rawptr) #link_name "glTexImage2D";
Clear :: proc(mask: u32) #link_name "glClear" ---;
ClearColor :: proc(r, g, b, a: f32) #link_name "glClearColor" ---;
Begin :: proc(mode: i32) #link_name "glBegin" ---;
End :: proc() #link_name "glEnd" ---;
Finish :: proc() #link_name "glFinish" ---;
BlendFunc :: proc(sfactor, dfactor: i32) #link_name "glBlendFunc" ---;
Enable :: proc(cap: i32) #link_name "glEnable" ---;
Disable :: proc(cap: i32) #link_name "glDisable" ---;
GenTextures :: proc(count: i32, result: ^u32) #link_name "glGenTextures" ---;
DeleteTextures :: proc(count: i32, result: ^u32) #link_name "glDeleteTextures"---;
TexParameteri :: proc(target, pname, param: i32) #link_name "glTexParameteri" ---;
TexParameterf :: proc(target: i32, pname: i32, param: f32) #link_name "glTexParameterf" ---;
BindTexture :: proc(target: i32, texture: u32) #link_name "glBindTexture" ---;
LoadIdentity :: proc() #link_name "glLoadIdentity" ---;
Viewport :: proc(x, y, width, height: i32) #link_name "glViewport" ---;
Ortho :: proc(left, right, bottom, top, near, far: f64) #link_name "glOrtho" ---;
Color3f :: proc(r, g, b: f32) #link_name "glColor3f" ---;
Vertex3f :: proc(x, y, z: f32) #link_name "glVertex3f" ---;
GetError :: proc() -> i32 #link_name "glGetError" ---;
GetString :: proc(name: i32) -> ^u8 #link_name "glGetString" ---;
GetIntegerv :: proc(name: i32, v: ^i32) #link_name "glGetIntegerv" ---;
TexCoord2f :: proc(x, y: f32) #link_name "glTexCoord2f" ---;
TexImage2D :: proc(target, level, internal_format,
width, height, border,
format, type_: i32, pixels: rawptr) #link_name "glTexImage2D" ---;
}
proc _string_data(s: string) -> ^u8 #inline { return &s[0]; }
_string_data :: proc(s: string) -> ^u8 #inline { return &s[0]; }
_libgl := win32.load_library_a(_string_data("opengl32.dll\x00"));
proc get_proc_address(name: string) -> rawptr {
get_proc_address :: proc(name: string) -> rawptr {
if name[len(name)-1] == 0 {
name = name[0..<len(name)-1];
}
@@ -116,8 +116,8 @@ proc get_proc_address(name: string) -> rawptr {
GetUniformLocation: proc(program: u32, name: ^u8) -> i32 #cc_c;
proc init() {
proc set_proc_address(p: rawptr, name: string) #inline {
init :: proc() {
set_proc_address :: proc(p: rawptr, name: string) #inline {
x := ^rawptr(p);
x^ = get_proc_address(name);
}

View File

@@ -4,11 +4,11 @@ import_load (
"os_linux.odin" when ODIN_OS == "linux";
)
proc write_string(fd: Handle, str: string) -> (int, Errno) {
write_string :: proc(fd: Handle, str: string) -> (int, Errno) {
return write(fd, []u8(str));
}
proc read_entire_file(name: string) -> ([]u8, bool) {
read_entire_file :: proc(name: string) -> ([]u8, bool) {
fd, err := open(name, O_RDONLY, 0);
if err != 0 {
return nil, false;
@@ -37,7 +37,7 @@ proc read_entire_file(name: string) -> ([]u8, bool) {
return data[0..<bytes_read], true;
}
proc write_entire_file(name: string, data: []u8) -> bool {
write_entire_file :: proc(name: string, data: []u8) -> bool {
fd, err := open(name, O_WRONLY, 0);
if err != 0 {
return false;

View File

@@ -108,13 +108,13 @@ S_ISGID :: 0002000; // Set group id on execution
S_ISVTX :: 0001000; // Directory restrcted delete
proc S_ISLNK (m: u32) -> bool #inline {return (m & S_IFMT) == S_IFLNK; }
proc S_ISREG (m: u32) -> bool #inline {return (m & S_IFMT) == S_IFREG; }
proc S_ISDIR (m: u32) -> bool #inline {return (m & S_IFMT) == S_IFDIR; }
proc S_ISCHR (m: u32) -> bool #inline {return (m & S_IFMT) == S_IFCHR; }
proc S_ISBLK (m: u32) -> bool #inline {return (m & S_IFMT) == S_IFBLK; }
proc S_ISFIFO(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFIFO; }
proc S_ISSOCK(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFSOCK;}
S_ISLNK :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFLNK; }
S_ISREG :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFREG; }
S_ISDIR :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFDIR; }
S_ISCHR :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFCHR; }
S_ISBLK :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFBLK; }
S_ISFIFO :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFIFO; }
S_ISSOCK :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFSOCK;}
F_OK :: 0; // Test for file existance
X_OK :: 1; // Test for execute permission
@@ -122,31 +122,31 @@ W_OK :: 2; // Test for write permission
R_OK :: 4; // Test for read permission
foreign libc {
proc _unix_open (path: ^u8, mode: int) -> Handle #link_name "open";
proc _unix_close (fd: Handle) -> i32 #link_name "close";
proc _unix_read (fd: Handle, buf: rawptr, size: int) -> int #link_name "read";
proc _unix_write (fd: Handle, buf: rawptr, size: int) -> int #link_name "write";
proc _unix_seek (fd: Handle, offset: i64, whence: i32) -> i64 #link_name "lseek64";
proc _unix_gettid() -> u64 #link_name "gettid";
proc _unix_stat (path: ^u8, stat: ^Stat) -> i32 #link_name "stat";
proc _unix_access(path: ^u8, mask: int) -> i32 #link_name "access";
_unix_open :: proc(path: ^u8, mode: int) -> Handle #link_name "open" ---;
_unix_close :: proc(fd: Handle) -> i32 #link_name "close" ---;
_unix_read :: proc(fd: Handle, buf: rawptr, size: int) -> int #link_name "read" ---;
_unix_write :: proc(fd: Handle, buf: rawptr, size: int) -> int #link_name "write" ---;
_unix_seek :: proc(fd: Handle, offset: i64, whence: i32) -> i64 #link_name "lseek64" ---;
_unix_gettid :: proc() -> u64 #link_name "gettid" ---;
_unix_stat :: proc(path: ^u8, stat: ^Stat) -> i32 #link_name "stat" ---;
_unix_access :: proc(path: ^u8, mask: int) -> i32 #link_name "access" ---;
proc _unix_malloc (size: int) -> rawptr #link_name "malloc";
proc _unix_free (ptr: rawptr) #link_name "free";
proc _unix_realloc(ptr: rawptr, size: int) -> rawptr #link_name "realloc";
proc _unix_getenv (^u8) -> ^u8 #link_name "getenv";
_unix_malloc :: proc(size: int) -> rawptr #link_name "malloc" ---;
_unix_free :: proc(ptr: rawptr) #link_name "free" ---;
_unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr #link_name "realloc" ---;
_unix_getenv :: proc(^u8) -> ^u8 #link_name "getenv" ---;
proc _unix_exit(status: int) #link_name "exit";
_unix_exit :: proc(status: int) #link_name "exit" ---;
}
foreign dl {
proc _unix_dlopen (filename: ^u8, flags: int) -> rawptr #link_name "dlopen";
proc _unix_dlsym (handle: rawptr, symbol: ^u8) -> (proc() #cc_c) #link_name "dlsym";
proc _unix_dlclose(handle: rawptr) -> int #link_name "dlclose";
proc _unix_dlerror() -> ^u8 #link_name "dlerror";
_unix_dlopen :: proc(filename: ^u8, flags: int) -> rawptr #link_name "dlopen" ---;
_unix_dlsym :: proc(handle: rawptr, symbol: ^u8) -> (proc() #cc_c) #link_name "dlsym" ---;
_unix_dlclose :: proc(handle: rawptr) -> int #link_name "dlclose" ---;
_unix_dlerror :: proc() -> ^u8 #link_name "dlerror" ---;
}
// TODO(zangent): Change this to just `open` when Bill fixes overloading.
proc open_simple(path: string, mode: int) -> (Handle, Errno) {
open_simple :: proc(path: string, mode: int) -> (Handle, Errno) {
cstr := strings.new_c_string(path);
handle := _unix_open(cstr, mode);
@@ -157,30 +157,30 @@ proc open_simple(path: string, mode: int) -> (Handle, Errno) {
return handle, 0;
}
// NOTE(zangent): This is here for compatability reasons. Should this be here?
proc open(path: string, mode: int = O_RDONLY, perm: u32 = 0) -> (Handle, Errno) {
open :: proc(path: string, mode: int = O_RDONLY, perm: u32 = 0) -> (Handle, Errno) {
return open_simple(path, mode);
}
proc close(fd: Handle) {
close :: proc(fd: Handle) {
_unix_close(fd);
}
proc read(fd: Handle, data: []u8) -> (int, Errno) {
read :: proc(fd: Handle, data: []u8) -> (int, Errno) {
sz := _unix_read(fd, &data[0], len(data));
return sz, 0;
}
proc write(fd: Handle, data: []u8) -> (int, Errno) {
write :: proc(fd: Handle, data: []u8) -> (int, Errno) {
sz := _unix_write(fd, &data[0], len(data));
return sz, 0;
}
proc seek(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
res := _unix_seek(fd, offset, i32(whence));
return res, 0;
}
proc file_size(fd: Handle) -> (i64, Errno) {
file_size :: proc(fd: Handle) -> (i64, Errno) {
prev, _ := seek(fd, 0, SEEK_CUR);
size, err := seek(fd, 0, SEEK_END);
seek(fd, prev, SEEK_SET);
@@ -195,11 +195,11 @@ stdout: Handle = 1;
stderr: Handle = 2;
/* TODO(zangent): Implement these!
proc last_write_time(fd: Handle) -> FileTime {}
proc last_write_time_by_name(name: string) -> FileTime {}
last_write_time :: proc(fd: Handle) -> FileTime {}
last_write_time_by_name :: proc(name: string) -> FileTime {}
*/
proc stat(path: string) -> (Stat, int) #inline {
stat :: proc(path: string) -> (Stat, int) #inline {
s: Stat;
cstr := strings.new_c_string(path);
defer free(cstr);
@@ -207,26 +207,26 @@ proc stat(path: string) -> (Stat, int) #inline {
return s, int(ret_int);
}
proc access(path: string, mask: int) -> bool #inline {
access :: proc(path: string, mask: int) -> bool #inline {
cstr := strings.new_c_string(path);
defer free(cstr);
return _unix_access(cstr, mask) == 0;
}
proc heap_alloc(size: int) -> rawptr {
heap_alloc :: proc(size: int) -> rawptr {
assert(size > 0);
return _unix_malloc(size);
}
proc heap_resize(ptr: rawptr, new_size: int) -> rawptr {
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
return _unix_realloc(ptr, new_size);
}
proc heap_free(ptr: rawptr) {
heap_free :: proc(ptr: rawptr) {
_unix_free(ptr);
}
proc getenv(name: string) -> (string, bool) {
getenv :: proc(name: string) -> (string, bool) {
path_str := strings.new_c_string(name);
cstr: ^u8 = _unix_getenv(path_str);
free(path_str);
@@ -236,38 +236,38 @@ proc getenv(name: string) -> (string, bool) {
return strings.to_odin_string(cstr), true;
}
proc exit(code: int) {
exit :: proc(code: int) {
_unix_exit(code);
}
proc current_thread_id() -> int {
current_thread_id :: proc() -> int {
// return int(_unix_gettid());
return 0;
}
proc dlopen(filename: string, flags: int) -> rawptr #inline {
dlopen :: proc(filename: string, flags: int) -> rawptr #inline {
cstr := strings.new_c_string(filename);
handle := _unix_dlopen(cstr, flags);
free(cstr);
return handle;
}
proc dlsym(handle: rawptr, symbol: string) -> (proc() #cc_c) #inline {
dlsym :: proc(handle: rawptr, symbol: string) -> (proc() #cc_c) #inline {
assert(handle != nil);
cstr := strings.new_c_string(symbol);
proc_handle := _unix_dlsym(handle, cstr);
free(cstr);
return proc_handle;
}
proc dlclose(handle: rawptr) -> bool #inline {
dlclose :: proc(handle: rawptr) -> bool #inline {
assert(handle != nil);
return _unix_dlclose(handle) == 0;
}
proc dlerror() -> string {
dlerror :: proc() -> string {
return strings.to_odin_string(_unix_dlerror());
}
proc _alloc_command_line_arguments() -> []string {
_alloc_command_line_arguments :: proc() -> []string {
// TODO(bill):
return nil;
}

View File

@@ -55,7 +55,7 @@ ERROR_FILE_IS_PIPE: Errno : 1<<29 + 0;
args := _alloc_command_line_arguments();
proc open(path: string, mode: int = O_RDONLY, perm: u32 = 0) -> (Handle, Errno) {
open :: proc(path: string, mode: int = O_RDONLY, perm: u32 = 0) -> (Handle, Errno) {
if len(path) == 0 {
return INVALID_HANDLE, ERROR_FILE_NOT_FOUND;
}
@@ -107,12 +107,12 @@ proc open(path: string, mode: int = O_RDONLY, perm: u32 = 0) -> (Handle, Errno)
return INVALID_HANDLE, Errno(err);
}
proc close(fd: Handle) {
close :: proc(fd: Handle) {
win32.close_handle(win32.Handle(fd));
}
proc write(fd: Handle, data: []u8) -> (int, Errno) {
write :: proc(fd: Handle, data: []u8) -> (int, Errno) {
if len(data) == 0 {
return 0, ERROR_NONE;
}
@@ -139,7 +139,7 @@ proc write(fd: Handle, data: []u8) -> (int, Errno) {
return int(total_write), ERROR_NONE;
}
proc read(fd: Handle, data: []u8) -> (int, Errno) {
read :: proc(fd: Handle, data: []u8) -> (int, Errno) {
if len(data) == 0 {
return 0, ERROR_NONE;
}
@@ -168,7 +168,7 @@ proc read(fd: Handle, data: []u8) -> (int, Errno) {
return int(total_read), ERROR_NONE;
}
proc seek(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
w: u32;
match whence {
case 0: w = win32.FILE_BEGIN;
@@ -189,7 +189,7 @@ proc seek(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
return i64(hi)<<32 + i64(dw_ptr), ERROR_NONE;
}
proc file_size(fd: Handle) -> (i64, Errno) {
file_size :: proc(fd: Handle) -> (i64, Errno) {
length: i64;
err: Errno;
if win32.get_file_size_ex(win32.Handle(fd), &length) == 0 {
@@ -206,7 +206,7 @@ stdout := get_std_handle(win32.STD_OUTPUT_HANDLE);
stderr := get_std_handle(win32.STD_ERROR_HANDLE);
proc get_std_handle(h: int) -> Handle {
get_std_handle :: proc(h: int) -> Handle {
fd := win32.get_std_handle(i32(h));
win32.set_handle_information(fd, win32.HANDLE_FLAG_INHERIT, 0);
return Handle(fd);
@@ -217,7 +217,7 @@ proc get_std_handle(h: int) -> Handle {
proc last_write_time(fd: Handle) -> FileTime {
last_write_time :: proc(fd: Handle) -> FileTime {
file_info: win32.ByHandleFileInformation;
win32.get_file_information_by_handle(win32.Handle(fd), &file_info);
lo := FileTime(file_info.last_write_time.lo);
@@ -225,7 +225,7 @@ proc last_write_time(fd: Handle) -> FileTime {
return lo | hi << 32;
}
proc last_write_time_by_name(name: string) -> FileTime {
last_write_time_by_name :: proc(name: string) -> FileTime {
last_write_time: win32.Filetime;
data: win32.FileAttributeData;
buf: [1024]u8;
@@ -245,10 +245,10 @@ proc last_write_time_by_name(name: string) -> FileTime {
proc heap_alloc(size: int) -> rawptr {
heap_alloc :: proc(size: int) -> rawptr {
return win32.heap_alloc(win32.get_process_heap(), win32.HEAP_ZERO_MEMORY, size);
}
proc heap_resize(ptr: rawptr, new_size: int) -> rawptr {
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
if new_size == 0 {
heap_free(ptr);
return nil;
@@ -258,7 +258,7 @@ proc heap_resize(ptr: rawptr, new_size: int) -> rawptr {
}
return win32.heap_realloc(win32.get_process_heap(), win32.HEAP_ZERO_MEMORY, ptr, new_size);
}
proc heap_free(ptr: rawptr) {
heap_free :: proc(ptr: rawptr) {
if ptr == nil {
return;
}
@@ -266,21 +266,21 @@ proc heap_free(ptr: rawptr) {
}
proc exit(code: int) {
exit :: proc(code: int) {
win32.exit_process(u32(code));
}
proc current_thread_id() -> int {
current_thread_id :: proc() -> int {
return int(win32.get_current_thread_id());
}
proc _alloc_command_line_arguments() -> []string {
proc alloc_ucs2_to_utf8(wstr: ^u16) -> string {
_alloc_command_line_arguments :: proc() -> []string {
alloc_ucs2_to_utf8 :: proc(wstr: ^u16) -> string {
wstr_len := 0;
for (wstr+wstr_len)^ != 0 {
wstr_len++;

View File

@@ -109,13 +109,13 @@ S_ISUID :: 0004000; // Set user id on execution
S_ISGID :: 0002000; // Set group id on execution
S_ISVTX :: 0001000; // Directory restrcted delete
proc S_ISLNK (m: u32) -> bool #inline {return (m & S_IFMT) == S_IFLNK; }
proc S_ISREG (m: u32) -> bool #inline {return (m & S_IFMT) == S_IFREG; }
proc S_ISDIR (m: u32) -> bool #inline {return (m & S_IFMT) == S_IFDIR; }
proc S_ISCHR (m: u32) -> bool #inline {return (m & S_IFMT) == S_IFCHR; }
proc S_ISBLK (m: u32) -> bool #inline {return (m & S_IFMT) == S_IFBLK; }
proc S_ISFIFO(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFIFO; }
proc S_ISSOCK(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFSOCK;}
S_ISLNK :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFLNK; }
S_ISREG :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFREG; }
S_ISDIR :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFDIR; }
S_ISCHR :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFCHR; }
S_ISBLK :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFBLK; }
S_ISFIFO :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFIFO; }
S_ISSOCK :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFSOCK;}
R_OK :: 4; // Test for read permission
W_OK :: 2; // Test for write permission
@@ -123,32 +123,32 @@ X_OK :: 1; // Test for execute permission
F_OK :: 0; // Test for file existance
foreign libc {
proc unix_open (path: ^u8, mode: int) -> Handle #link_name "open";
proc unix_close (handle: Handle) #link_name "close";
proc unix_read (handle: Handle, buffer: rawptr, count: int) -> AddressSize #link_name "read";
proc unix_write (handle: Handle, buffer: rawptr, count: int) -> AddressSize #link_name "write";
proc unix_lseek (fs: Handle, offset: AddressSize, whence: int) -> AddressSize #link_name "lseek";
proc unix_gettid() -> u64 #link_name "gettid";
proc unix_stat (path: ^u8, stat: ^Stat) -> int #link_name "stat";
proc unix_access(path: ^u8, mask: int) -> int #link_name "access";
unix_open :: proc(path: ^u8, mode: int) -> Handle #link_name "open" ---;
unix_close :: proc(handle: Handle) #link_name "close" ---;
unix_read :: proc(handle: Handle, buffer: rawptr, count: int) -> AddressSize #link_name "read" ---;
unix_write :: proc(handle: Handle, buffer: rawptr, count: int) -> AddressSize #link_name "write" ---;
unix_lseek :: proc(fs: Handle, offset: AddressSize, whence: int) -> AddressSize #link_name "lseek" ---;
unix_gettid :: proc() -> u64 #link_name "gettid" ---;
unix_stat :: proc(path: ^u8, stat: ^Stat) -> int #link_name "stat" ---;
unix_access :: proc(path: ^u8, mask: int) -> int #link_name "access" ---;
proc unix_malloc (size: int) -> rawptr #link_name "malloc";
proc unix_free (ptr: rawptr) #link_name "free";
proc unix_realloc(ptr: rawptr, size: int) -> rawptr #link_name "realloc";
proc unix_getenv (^u8) -> ^u8 #link_name "getenv";
unix_malloc :: proc(size: int) -> rawptr #link_name "malloc" ---;
unix_free :: proc(ptr: rawptr) #link_name "free" ---;
unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr #link_name "realloc" ---;
unix_getenv :: proc(^u8) -> ^u8 #link_name "getenv" ---;
proc unix_exit(status: int) #link_name "exit";
unix_exit :: proc(status: int) #link_name "exit" ---;
}
foreign dl {
proc unix_dlopen (filename: ^u8, flags: int) -> rawptr #link_name "dlopen";
proc unix_dlsym (handle: rawptr, symbol: ^u8) -> (proc() #cc_c) #link_name "dlsym";
proc unix_dlclose(handle: rawptr) -> int #link_name "dlclose";
proc unix_dlerror() -> ^u8 #link_name "dlerror";
unix_dlopen :: proc(filename: ^u8, flags: int) -> rawptr #link_name "dlopen" ---;
unix_dlsym :: proc(handle: rawptr, symbol: ^u8) -> (proc() #cc_c) #link_name "dlsym" ---;
unix_dlclose :: proc(handle: rawptr) -> int #link_name "dlclose" ---;
unix_dlerror :: proc() -> ^u8 #link_name "dlerror" ---;
}
// TODO(zangent): Change this to just `open` when Bill fixes overloading.
proc open_simple(path: string, mode: int) -> (Handle, Errno) {
open_simple :: proc(path: string, mode: int) -> (Handle, Errno) {
cstr := strings.new_c_string(path);
handle := unix_open(cstr, mode);
@@ -160,15 +160,15 @@ proc open_simple(path: string, mode: int) -> (Handle, Errno) {
}
// NOTE(zangent): This is here for compatability reasons. Should this be here?
proc open(path: string, mode: int = O_RDONLY, perm: u32 = 0) -> (Handle, Errno) {
open :: proc(path: string, mode: int = O_RDONLY, perm: u32 = 0) -> (Handle, Errno) {
return open_simple(path, mode);
}
proc close(fd: Handle) {
close :: proc(fd: Handle) {
unix_close(fd);
}
proc write(fd: Handle, data: []u8) -> (AddressSize, Errno) {
write :: proc(fd: Handle, data: []u8) -> (AddressSize, Errno) {
assert(fd != -1);
bytes_written := unix_write(fd, &data[0], len(data));
@@ -178,7 +178,7 @@ proc write(fd: Handle, data: []u8) -> (AddressSize, Errno) {
return bytes_written, 0;
}
proc read(fd: Handle, data: []u8) -> (AddressSize, Errno) {
read :: proc(fd: Handle, data: []u8) -> (AddressSize, Errno) {
assert(fd != -1);
bytes_read := unix_read(fd, &data[0], len(data));
@@ -188,7 +188,7 @@ proc read(fd: Handle, data: []u8) -> (AddressSize, Errno) {
return bytes_read, 0;
}
proc seek(fd: Handle, offset: AddressSize, whence: int) -> (AddressSize, Errno) {
seek :: proc(fd: Handle, offset: AddressSize, whence: int) -> (AddressSize, Errno) {
assert(fd != -1);
final_offset := unix_lseek(fd, offset, whence);
@@ -198,7 +198,7 @@ proc seek(fd: Handle, offset: AddressSize, whence: int) -> (AddressSize, Errno)
return final_offset, 0;
}
proc file_size(fd: Handle) -> (i64, Errno) {
file_size :: proc(fd: Handle) -> (i64, Errno) {
prev, _ := seek(fd, 0, SEEK_CUR);
size, err := seek(fd, 0, SEEK_END);
seek(fd, prev, SEEK_SET);
@@ -213,11 +213,11 @@ stdout: Handle = 1; // get_std_handle(win32.STD_OUTPUT_HANDLE);
stderr: Handle = 2; // get_std_handle(win32.STD_ERROR_HANDLE);
/* TODO(zangent): Implement these!
proc last_write_time(fd: Handle) -> FileTime {}
proc last_write_time_by_name(name: string) -> FileTime {}
last_write_time :: proc(fd: Handle) -> FileTime {}
last_write_time_by_name :: proc(name: string) -> FileTime {}
*/
proc stat(path: string) -> (Stat, bool) #inline {
stat :: proc(path: string) -> (Stat, bool) #inline {
s: Stat;
cstr := strings.new_c_string(path);
defer free(cstr);
@@ -225,24 +225,24 @@ proc stat(path: string) -> (Stat, bool) #inline {
return s, ret_int==0;
}
proc access(path: string, mask: int) -> bool #inline {
access :: proc(path: string, mask: int) -> bool #inline {
cstr := strings.new_c_string(path);
defer free(cstr);
return unix_access(cstr, mask) == 0;
}
proc heap_alloc(size: int) -> rawptr #inline {
heap_alloc :: proc(size: int) -> rawptr #inline {
assert(size > 0);
return unix_malloc(size);
}
proc heap_resize(ptr: rawptr, new_size: int) -> rawptr #inline {
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr #inline {
return unix_realloc(ptr, new_size);
}
proc heap_free(ptr: rawptr) #inline {
heap_free :: proc(ptr: rawptr) #inline {
unix_free(ptr);
}
proc getenv(name: string) -> (string, bool) {
getenv :: proc(name: string) -> (string, bool) {
path_str := strings.new_c_string(name);
cstr: ^u8 = unix_getenv(path_str);
free(path_str);
@@ -252,33 +252,33 @@ proc getenv(name: string) -> (string, bool) {
return strings.to_odin_string(cstr), true;
}
proc exit(code: int) #inline {
exit :: proc(code: int) #inline {
unix_exit(code);
}
proc current_thread_id() -> int {
current_thread_id :: proc() -> int {
// return cast(int) unix_gettid();
return 0;
}
proc dlopen(filename: string, flags: int) -> rawptr #inline {
dlopen :: proc(filename: string, flags: int) -> rawptr #inline {
cstr := strings.new_c_string(filename);
handle := unix_dlopen(cstr, flags);
free(cstr);
return handle;
}
proc dlsym(handle: rawptr, symbol: string) -> (proc() #cc_c) #inline {
dlsym :: proc(handle: rawptr, symbol: string) -> (proc() #cc_c) #inline {
assert(handle != nil);
cstr := strings.new_c_string(symbol);
proc_handle := unix_dlsym(handle, cstr);
free(cstr);
return proc_handle;
}
proc dlclose(handle: rawptr) -> bool #inline {
dlclose :: proc(handle: rawptr) -> bool #inline {
assert(handle != nil);
return unix_dlclose(handle) == 0;
}
proc dlerror() -> string {
dlerror :: proc() -> string {
return strings.to_odin_string(unix_dlerror());
}

View File

@@ -7,7 +7,7 @@ IntFlag :: enum {
}
proc parse_bool(s: string) -> (result: bool, ok: bool) {
parse_bool :: proc(s: string) -> (result: bool, ok: bool) {
match s {
case "1", "t", "T", "true", "TRUE", "True":
return true, true;
@@ -17,7 +17,7 @@ proc parse_bool(s: string) -> (result: bool, ok: bool) {
return false, false;
}
proc _digit_value(r: rune) -> int {
_digit_value :: proc(r: rune) -> int {
ri := int(r);
v: int = 16;
match r {
@@ -28,7 +28,7 @@ proc _digit_value(r: rune) -> int {
return v;
}
proc parse_i128(s: string) -> i128 {
parse_i128 :: proc(s: string) -> i128 {
neg := false;
if len(s) > 1 {
match s[0] {
@@ -70,7 +70,7 @@ proc parse_i128(s: string) -> i128 {
return neg ? -value : value;
}
proc parse_u128(s: string) -> u128 {
parse_u128 :: proc(s: string) -> u128 {
neg := false;
if len(s) > 1 && s[0] == '+' {
s = s[1..];
@@ -107,14 +107,14 @@ proc parse_u128(s: string) -> u128 {
}
proc parse_int(s: string) -> int {
parse_int :: proc(s: string) -> int {
return int(parse_i128(s));
}
proc parse_uint(s: string, base: int) -> uint {
parse_uint :: proc(s: string, base: int) -> uint {
return uint(parse_u128(s));
}
proc parse_f64(s: string) -> f64 {
parse_f64 :: proc(s: string) -> f64 {
i := 0;
sign: f64 = 1;
@@ -189,21 +189,21 @@ proc parse_f64(s: string) -> f64 {
}
proc append_bool(buf: []u8, b: bool) -> string {
append_bool :: proc(buf: []u8, b: bool) -> string {
s := b ? "true" : "false";
append(buf, ..[]u8(s));
return string(buf);
}
proc append_uint(buf: []u8, u: u64, base: int) -> string {
append_uint :: proc(buf: []u8, u: u64, base: int) -> string {
return append_bits(buf, u128(u), base, false, 8*size_of(uint), digits, 0);
}
proc append_int(buf: []u8, i: i64, base: int) -> string {
append_int :: proc(buf: []u8, i: i64, base: int) -> string {
return append_bits(buf, u128(i), base, true, 8*size_of(int), digits, 0);
}
proc itoa(buf: []u8, i: int) -> string { return append_int(buf, i64(i), 10); }
itoa :: proc(buf: []u8, i: int) -> string { return append_int(buf, i64(i), 10); }
proc append_float(buf: []u8, f: f64, fmt: u8, prec, bit_size: int) -> string {
append_float :: proc(buf: []u8, f: f64, fmt: u8, prec, bit_size: int) -> string {
return string(generic_ftoa(buf, f, fmt, prec, bit_size));
}
@@ -229,7 +229,7 @@ _f32_info := Float_Info{23, 8, -127};
_f64_info := Float_Info{52, 11, -1023};
proc generic_ftoa(buf: []u8, val: f64, fmt: u8, prec, bit_size: int) -> []u8 {
generic_ftoa :: proc(buf: []u8, val: f64, fmt: u8, prec, bit_size: int) -> []u8 {
bits: u64;
flt: ^Float_Info;
match bit_size {
@@ -301,7 +301,7 @@ proc generic_ftoa(buf: []u8, val: f64, fmt: u8, prec, bit_size: int) -> []u8 {
proc format_digits(buf: []u8, shortest: bool, neg: bool, digs: DecimalSlice, prec: int, fmt: u8) -> []u8 {
format_digits :: proc(buf: []u8, shortest: bool, neg: bool, digs: DecimalSlice, prec: int, fmt: u8) -> []u8 {
match fmt {
case 'f', 'F':
append(buf, neg ? '-' : '+');
@@ -346,7 +346,7 @@ proc format_digits(buf: []u8, shortest: bool, neg: bool, digs: DecimalSlice, pre
return buf;
}
proc round_shortest(d: ^Decimal, mant: u64, exp: int, flt: ^Float_Info) {
round_shortest :: proc(d: ^Decimal, mant: u64, exp: int, flt: ^Float_Info) {
if mant == 0 { // If mantissa is zero, the number is zero
d.count = 0;
return;
@@ -417,7 +417,7 @@ MAX_BASE :: 32;
digits := "0123456789abcdefghijklmnopqrstuvwxyz";
proc is_integer_negative(u: u128, is_signed: bool, bit_size: int) -> (unsigned: u128, neg: bool) {
is_integer_negative :: proc(u: u128, is_signed: bool, bit_size: int) -> (unsigned: u128, neg: bool) {
neg := false;
if is_signed {
match bit_size {
@@ -453,7 +453,7 @@ proc is_integer_negative(u: u128, is_signed: bool, bit_size: int) -> (unsigned:
return u, neg;
}
proc append_bits(buf: []u8, u: u128, base: int, is_signed: bool, bit_size: int, digits: string, flags: IntFlag) -> string {
append_bits :: proc(buf: []u8, u: u128, base: int, is_signed: bool, bit_size: int, digits: string, flags: IntFlag) -> string {
if base < 2 || base > MAX_BASE {
panic("strconv: illegal base passed to append_bits");
}

View File

@@ -1,18 +1,18 @@
proc new_string(s: string) -> string {
new_string :: proc(s: string) -> string {
c := make([]u8, len(s)+1);
copy(c, []u8(s));
c[len(s)] = 0;
return string(c[0..<len(s)]);
}
proc new_c_string(s: string) -> ^u8 {
new_c_string :: proc(s: string) -> ^u8 {
c := make([]u8, len(s)+1);
copy(c, []u8(s));
c[len(s)] = 0;
return &c[0];
}
proc to_odin_string(c: ^u8) -> string {
to_odin_string :: proc(c: ^u8) -> string {
len := 0;
for (c+len)^ != 0 {
len++;

View File

@@ -14,41 +14,41 @@ Mutex :: struct {
_recursion: i32,
}
proc current_thread_id() -> i32 {
current_thread_id :: proc() -> i32 {
return i32(os.current_thread_id());
}
proc semaphore_init(s: ^Semaphore) {
semaphore_init :: proc(s: ^Semaphore) {
// s._handle = win32.CreateSemaphoreA(nil, 0, 1<<31-1, nil);
}
proc semaphore_destroy(s: ^Semaphore) {
semaphore_destroy :: proc(s: ^Semaphore) {
// win32.CloseHandle(s._handle);
}
proc semaphore_post(s: ^Semaphore, count: int) {
semaphore_post :: proc(s: ^Semaphore, count: int) {
// win32.ReleaseSemaphore(s._handle, cast(i32)count, nil);
}
proc semaphore_release(s: ^Semaphore) #inline {
semaphore_release :: proc(s: ^Semaphore) #inline {
semaphore_post(s, 1);
}
proc semaphore_wait(s: ^Semaphore) {
semaphore_wait :: proc(s: ^Semaphore) {
// win32.WaitForSingleObject(s._handle, win32.INFINITE);
}
proc mutex_init(m: ^Mutex) {
mutex_init :: proc(m: ^Mutex) {
atomics.store(&m._counter, 0);
atomics.store(&m._owner, current_thread_id());
semaphore_init(&m._semaphore);
m._recursion = 0;
}
proc mutex_destroy(m: ^Mutex) {
mutex_destroy :: proc(m: ^Mutex) {
semaphore_destroy(&m._semaphore);
}
proc mutex_lock(m: ^Mutex) {
mutex_lock :: proc(m: ^Mutex) {
thread_id := current_thread_id();
if atomics.fetch_add(&m._counter, 1) > 0 {
if thread_id != atomics.load(&m._owner) {
@@ -58,7 +58,7 @@ proc mutex_lock(m: ^Mutex) {
atomics.store(&m._owner, thread_id);
m._recursion++;
}
proc mutex_try_lock(m: ^Mutex) -> bool {
mutex_try_lock :: proc(m: ^Mutex) -> bool {
thread_id := current_thread_id();
if atomics.load(&m._owner) == thread_id {
atomics.fetch_add(&m._counter, 1);
@@ -75,7 +75,7 @@ proc mutex_try_lock(m: ^Mutex) -> bool {
m._recursion++;
return true;
}
proc mutex_unlock(m: ^Mutex) {
mutex_unlock :: proc(m: ^Mutex) {
recursion: i32;
thread_id := current_thread_id();
assert(thread_id == atomics.load(&m._owner));

View File

@@ -14,39 +14,39 @@ Mutex :: struct {
_recursion: i32,
}
proc current_thread_id() -> i32 {
current_thread_id :: proc() -> i32 {
return i32(win32.get_current_thread_id());
}
proc semaphore_init(s: ^Semaphore) {
semaphore_init :: proc(s: ^Semaphore) {
s._handle = win32.create_semaphore_a(nil, 0, 1<<31-1, nil);
}
proc semaphore_destroy(s: ^Semaphore) {
semaphore_destroy :: proc(s: ^Semaphore) {
win32.close_handle(s._handle);
}
proc semaphore_post(s: ^Semaphore, count: int) {
semaphore_post :: proc(s: ^Semaphore, count: int) {
win32.release_semaphore(s._handle, i32(count), nil);
}
proc semaphore_release(s: ^Semaphore) #inline { semaphore_post(s, 1); }
semaphore_release :: proc(s: ^Semaphore) #inline { semaphore_post(s, 1); }
proc semaphore_wait(s: ^Semaphore) {
semaphore_wait :: proc(s: ^Semaphore) {
win32.wait_for_single_object(s._handle, win32.INFINITE);
}
proc mutex_init(m: ^Mutex) {
mutex_init :: proc(m: ^Mutex) {
atomics.store(&m._counter, 0);
atomics.store(&m._owner, current_thread_id());
semaphore_init(&m._semaphore);
m._recursion = 0;
}
proc mutex_destroy(m: ^Mutex) {
mutex_destroy :: proc(m: ^Mutex) {
semaphore_destroy(&m._semaphore);
}
proc mutex_lock(m: ^Mutex) {
mutex_lock :: proc(m: ^Mutex) {
thread_id := current_thread_id();
if atomics.fetch_add(&m._counter, 1) > 0 {
if thread_id != atomics.load(&m._owner) {
@@ -56,7 +56,7 @@ proc mutex_lock(m: ^Mutex) {
atomics.store(&m._owner, thread_id);
m._recursion++;
}
proc mutex_try_lock(m: ^Mutex) -> bool {
mutex_try_lock :: proc(m: ^Mutex) -> bool {
thread_id := current_thread_id();
if atomics.load(&m._owner) == thread_id {
atomics.fetch_add(&m._counter, 1);
@@ -73,7 +73,7 @@ proc mutex_try_lock(m: ^Mutex) -> bool {
m._recursion++;
return true;
}
proc mutex_unlock(m: ^Mutex) {
mutex_unlock :: proc(m: ^Mutex) {
recursion: i32;
thread_id := current_thread_id();
assert(thread_id == atomics.load(&m._owner));

View File

@@ -66,20 +66,20 @@ GetExtensionsStringARBType :: proc(Hdc) -> ^u8 #cc_c;
foreign opengl32 {
proc create_context (hdc: Hdc) -> Hglrc #link_name "wglCreateContext";
proc make_current (hdc: Hdc, hglrc: Hglrc) -> Bool #link_name "wglMakeCurrent";
proc get_proc_address (c_str: ^u8) -> Proc #link_name "wglGetProcAddress";
proc delete_context (hglrc: Hglrc) -> Bool #link_name "wglDeleteContext";
proc copy_context (src, dst: Hglrc, mask: u32) -> Bool #link_name "wglCopyContext";
proc create_layer_context (hdc: Hdc, layer_plane: i32) -> Hglrc #link_name "wglCreateLayerContext";
proc describe_layer_plane (hdc: Hdc, pixel_format, layer_plane: i32, bytes: u32, pd: ^LayerPlaneDescriptor) -> Bool #link_name "wglDescribeLayerPlane";
proc get_current_context () -> Hglrc #link_name "wglGetCurrentContext";
proc get_current_dc () -> Hdc #link_name "wglGetCurrentDC";
proc get_layer_palette_entries(hdc: Hdc, layer_plane, start, entries: i32, cr: ^ColorRef) -> i32 #link_name "wglGetLayerPaletteEntries";
proc realize_layer_palette (hdc: Hdc, layer_plane: i32, realize: Bool) -> Bool #link_name "wglRealizeLayerPalette";
proc set_layer_palette_entries(hdc: Hdc, layer_plane, start, entries: i32, cr: ^ColorRef) -> i32 #link_name "wglSetLayerPaletteEntries";
proc share_lists (hglrc1, hglrc2: Hglrc) -> Bool #link_name "wglShareLists";
proc swap_layer_buffers (hdc: Hdc, planes: u32) -> Bool #link_name "wglSwapLayerBuffers";
proc use_font_bitmaps (hdc: Hdc, first, count, list_base: u32) -> Bool #link_name "wglUseFontBitmaps";
proc use_font_outlines (hdc: Hdc, first, count, list_base: u32, deviation, extrusion: f32, format: i32, gmf: ^Glyph_MetricsFloat) -> Bool #link_name "wglUseFontOutlines";
create_context :: proc(hdc: Hdc) -> Hglrc #link_name "wglCreateContext" ---;
make_current :: proc(hdc: Hdc, hglrc: Hglrc) -> Bool #link_name "wglMakeCurrent" ---;
get_proc_address :: proc(c_str: ^u8) -> Proc #link_name "wglGetProcAddress" ---;
delete_context :: proc(hglrc: Hglrc) -> Bool #link_name "wglDeleteContext" ---;
copy_context :: proc(src, dst: Hglrc, mask: u32) -> Bool #link_name "wglCopyContext" ---;
create_layer_context :: proc(hdc: Hdc, layer_plane: i32) -> Hglrc #link_name "wglCreateLayerContext" ---;
describe_layer_plane :: proc(hdc: Hdc, pixel_format, layer_plane: i32, bytes: u32, pd: ^LayerPlaneDescriptor) -> Bool #link_name "wglDescribeLayerPlane" ---;
get_current_context :: proc() -> Hglrc #link_name "wglGetCurrentContext" ---;
get_current_dc :: proc() -> Hdc #link_name "wglGetCurrentDC" ---;
get_layer_palette_entries :: proc(hdc: Hdc, layer_plane, start, entries: i32, cr: ^ColorRef) -> i32 #link_name "wglGetLayerPaletteEntries" ---;
realize_layer_palette :: proc(hdc: Hdc, layer_plane: i32, realize: Bool) -> Bool #link_name "wglRealizeLayerPalette" ---;
set_layer_palette_entries :: proc(hdc: Hdc, layer_plane, start, entries: i32, cr: ^ColorRef) -> i32 #link_name "wglSetLayerPaletteEntries" ---;
share_lists :: proc(hglrc1, hglrc2: Hglrc) -> Bool #link_name "wglShareLists" ---;
swap_layer_buffers :: proc(hdc: Hdc, planes: u32) -> Bool #link_name "wglSwapLayerBuffers" ---;
use_font_bitmaps :: proc(hdc: Hdc, first, count, list_base: u32) -> Bool #link_name "wglUseFontBitmaps" ---;
use_font_outlines :: proc(hdc: Hdc, first, count, list_base: u32, deviation, extrusion: f32, format: i32, gmf: ^Glyph_MetricsFloat) -> Bool #link_name "wglUseFontOutlines" ---;
}

View File

@@ -272,180 +272,180 @@ GetFileExMaxInfoLevel: GET_FILEEX_INFO_LEVELS : 1;
foreign kernel32 {
proc get_last_error () -> i32 #cc_std #link_name "GetLastError";
proc exit_process (exit_code: u32) #cc_std #link_name "ExitProcess";
proc get_module_handle_a(module_name: ^u8) -> Hinstance #cc_std #link_name "GetModuleHandleA";
proc sleep(ms: i32) -> i32 #cc_std #link_name "Sleep";
proc query_performance_frequency(result: ^i64) -> i32 #cc_std #link_name "QueryPerformanceFrequency";
proc query_performance_counter (result: ^i64) -> i32 #cc_std #link_name "QueryPerformanceCounter";
proc output_debug_string_a(c_str: ^u8) #cc_std #link_name "OutputDebugStringA";
get_last_error :: proc() -> i32 #cc_std #link_name "GetLastError" ---;
exit_process :: proc(exit_code: u32) #cc_std #link_name "ExitProcess" ---;
get_module_handle_a :: proc(module_name: ^u8) -> Hinstance #cc_std #link_name "GetModuleHandleA" ---;
sleep :: proc(ms: i32) -> i32 #cc_std #link_name "Sleep" ---;
query_performance_frequency :: proc(result: ^i64) -> i32 #cc_std #link_name "QueryPerformanceFrequency" ---;
query_performance_counter :: proc(result: ^i64) -> i32 #cc_std #link_name "QueryPerformanceCounter" ---;
output_debug_string_a :: proc(c_str: ^u8) #cc_std #link_name "OutputDebugStringA" ---;
proc get_command_line_a () -> ^u8 #cc_std #link_name "GetCommandLineA";
proc get_command_line_w () -> ^u16 #cc_std #link_name "GetCommandLineW";
proc get_system_metrics (index: i32) -> i32 #cc_std #link_name "GetSystemMetrics";
proc get_current_thread_id () -> u32 #cc_std #link_name "GetCurrentThreadId";
get_command_line_a :: proc() -> ^u8 #cc_std #link_name "GetCommandLineA" ---;
get_command_line_w :: proc() -> ^u16 #cc_std #link_name "GetCommandLineW" ---;
get_system_metrics :: proc(index: i32) -> i32 #cc_std #link_name "GetSystemMetrics" ---;
get_current_thread_id :: proc() -> u32 #cc_std #link_name "GetCurrentThreadId" ---;
proc get_system_time_as_file_time(system_time_as_file_time: ^Filetime) #cc_std #link_name "GetSystemTimeAsFileTime";
proc file_time_to_local_file_time(file_time: ^Filetime, local_file_time: ^Filetime) -> Bool #cc_std #link_name "FileTimeToLocalFileTime";
proc file_time_to_system_time (file_time: ^Filetime, system_time: ^Systemtime) -> Bool #cc_std #link_name "FileTimeToSystemTime";
proc system_time_to_file_time (system_time: ^Systemtime, file_time: ^Filetime) -> Bool #cc_std #link_name "SystemTimeToFileTime";
get_system_time_as_file_time :: proc(system_time_as_file_time: ^Filetime) #cc_std #link_name "GetSystemTimeAsFileTime" ---;
file_time_to_local_file_time :: proc(file_time: ^Filetime, local_file_time: ^Filetime) -> Bool #cc_std #link_name "FileTimeToLocalFileTime" ---;
file_time_to_system_time :: proc(file_time: ^Filetime, system_time: ^Systemtime) -> Bool #cc_std #link_name "FileTimeToSystemTime" ---;
system_time_to_file_time :: proc(system_time: ^Systemtime, file_time: ^Filetime) -> Bool #cc_std #link_name "SystemTimeToFileTime" ---;
proc close_handle (h: Handle) -> i32 #cc_std #link_name "CloseHandle";
proc get_std_handle(h: i32) -> Handle #cc_std #link_name "GetStdHandle";
proc create_file_a (filename: ^u8, desired_access, share_mode: u32,
security: rawptr,
creation, flags_and_attribs: u32, template_file: Handle) -> Handle #cc_std #link_name "CreateFileA";
proc read_file (h: Handle, buf: rawptr, to_read: u32, bytes_read: ^i32, overlapped: rawptr) -> Bool #cc_std #link_name "ReadFile";
proc write_file(h: Handle, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> Bool #cc_std #link_name "WriteFile";
close_handle :: proc(h: Handle) -> i32 #cc_std #link_name "CloseHandle" ---;
get_std_handle :: proc(h: i32) -> Handle #cc_std #link_name "GetStdHandle" ---;
create_file_a :: proc(filename: ^u8, desired_access, share_mode: u32,
security: rawptr,
creation, flags_and_attribs: u32, template_file: Handle) -> Handle #cc_std #link_name "CreateFileA" ---;
read_file :: proc(h: Handle, buf: rawptr, to_read: u32, bytes_read: ^i32, overlapped: rawptr) -> Bool #cc_std #link_name "ReadFile" ---;
write_file :: proc(h: Handle, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> Bool #cc_std #link_name "WriteFile" ---;
proc get_file_size_ex (file_handle: Handle, file_size: ^i64) -> Bool #cc_std #link_name "GetFileSizeEx";
proc get_file_attributes_a (filename: ^u8) -> u32 #cc_std #link_name "GetFileAttributesA";
proc get_file_attributes_ex_a (filename: ^u8, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: rawptr) -> Bool #cc_std #link_name "GetFileAttributesExA";
proc get_file_information_by_handle(file_handle: Handle, file_info: ^ByHandleFileInformation) -> Bool #cc_std #link_name "GetFileInformationByHandle";
get_file_size_ex :: proc(file_handle: Handle, file_size: ^i64) -> Bool #cc_std #link_name "GetFileSizeEx" ---;
get_file_attributes_a :: proc(filename: ^u8) -> u32 #cc_std #link_name "GetFileAttributesA" ---;
get_file_attributes_ex_a :: proc(filename: ^u8, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: rawptr) -> Bool #cc_std #link_name "GetFileAttributesExA" ---;
get_file_information_by_handle :: proc(file_handle: Handle, file_info: ^ByHandleFileInformation) -> Bool #cc_std #link_name "GetFileInformationByHandle" ---;
proc get_file_type (file_handle: Handle) -> u32 #cc_std #link_name "GetFileType";
proc set_file_pointer(file_handle: Handle, distance_to_move: i32, distance_to_move_high: ^i32, move_method: u32) -> u32 #cc_std #link_name "SetFilePointer";
get_file_type :: proc(file_handle: Handle) -> u32 #cc_std #link_name "GetFileType" ---;
set_file_pointer :: proc(file_handle: Handle, distance_to_move: i32, distance_to_move_high: ^i32, move_method: u32) -> u32 #cc_std #link_name "SetFilePointer" ---;
proc set_handle_information(obj: Handle, mask, flags: u32) -> Bool #cc_std #link_name "SetHandleInformation";
set_handle_information :: proc(obj: Handle, mask, flags: u32) -> Bool #cc_std #link_name "SetHandleInformation" ---;
proc find_first_file_a(file_name : ^u8, data : ^FindData) -> Handle #cc_std #link_name "FindFirstFileA";
proc find_next_file_a (file : Handle, data : ^FindData) -> Bool #cc_std #link_name "FindNextFileA";
proc find_close (file : Handle) -> Bool #cc_std #link_name "FindClose";
find_first_file_a :: proc(file_name : ^u8, data : ^FindData) -> Handle #cc_std #link_name "FindFirstFileA" ---;
find_next_file_a :: proc(file : Handle, data : ^FindData) -> Bool #cc_std #link_name "FindNextFileA" ---;
find_close :: proc(file : Handle) -> Bool #cc_std #link_name "FindClose" ---;
proc heap_alloc (h: Handle, flags: u32, bytes: int) -> rawptr #cc_std #link_name "HeapAlloc";
proc heap_realloc (h: Handle, flags: u32, memory: rawptr, bytes: int) -> rawptr #cc_std #link_name "HeapReAlloc";
proc heap_free (h: Handle, flags: u32, memory: rawptr) -> Bool #cc_std #link_name "HeapFree";
proc get_process_heap() -> Handle #cc_std #link_name "GetProcessHeap";
heap_alloc :: proc(h: Handle, flags: u32, bytes: int) -> rawptr #cc_std #link_name "HeapAlloc" ---;
heap_realloc :: proc(h: Handle, flags: u32, memory: rawptr, bytes: int) -> rawptr #cc_std #link_name "HeapReAlloc" ---;
heap_free :: proc(h: Handle, flags: u32, memory: rawptr) -> Bool #cc_std #link_name "HeapFree" ---;
get_process_heap :: proc() -> Handle #cc_std #link_name "GetProcessHeap" ---;
proc create_semaphore_a (attributes: ^Security_Attributes, initial_count, maximum_count: i32, name: ^u8) -> Handle #cc_std #link_name "CreateSemaphoreA";
proc release_semaphore (semaphore: Handle, release_count: i32, previous_count: ^i32) -> Bool #cc_std #link_name "ReleaseSemaphore";
proc wait_for_single_object(handle: Handle, milliseconds: u32) -> u32 #cc_std #link_name "WaitForSingleObject";
create_semaphore_a :: proc(attributes: ^Security_Attributes, initial_count, maximum_count: i32, name: ^u8) -> Handle #cc_std #link_name "CreateSemaphoreA" ---;
release_semaphore :: proc(semaphore: Handle, release_count: i32, previous_count: ^i32) -> Bool #cc_std #link_name "ReleaseSemaphore" ---;
wait_for_single_object :: proc(handle: Handle, milliseconds: u32) -> u32 #cc_std #link_name "WaitForSingleObject" ---;
proc interlocked_compare_exchange (dst: ^i32, exchange, comparand: i32) -> i32 #cc_std #link_name "InterlockedCompareExchange";
proc interlocked_exchange (dst: ^i32, desired: i32) -> i32 #cc_std #link_name "InterlockedExchange";
proc interlocked_exchange_add (dst: ^i32, desired: i32) -> i32 #cc_std #link_name "InterlockedExchangeAdd";
proc interlocked_and (dst: ^i32, desired: i32) -> i32 #cc_std #link_name "InterlockedAnd";
proc interlocked_or (dst: ^i32, desired: i32) -> i32 #cc_std #link_name "InterlockedOr";
interlocked_compare_exchange :: proc(dst: ^i32, exchange, comparand: i32) -> i32 #cc_std #link_name "InterlockedCompareExchange" ---;
interlocked_exchange :: proc(dst: ^i32, desired: i32) -> i32 #cc_std #link_name "InterlockedExchange" ---;
interlocked_exchange_add :: proc(dst: ^i32, desired: i32) -> i32 #cc_std #link_name "InterlockedExchangeAdd" ---;
interlocked_and :: proc(dst: ^i32, desired: i32) -> i32 #cc_std #link_name "InterlockedAnd" ---;
interlocked_or :: proc(dst: ^i32, desired: i32) -> i32 #cc_std #link_name "InterlockedOr" ---;
proc interlocked_compare_exchange64(dst: ^i64, exchange, comparand: i64) -> i64 #cc_std #link_name "InterlockedCompareExchange64";
proc interlocked_exchange64 (dst: ^i64, desired: i64) -> i64 #cc_std #link_name "InterlockedExchange64";
proc interlocked_exchange_add64 (dst: ^i64, desired: i64) -> i64 #cc_std #link_name "InterlockedExchangeAdd64";
proc interlocked_and64 (dst: ^i64, desired: i64) -> i64 #cc_std #link_name "InterlockedAnd64";
proc interlocked_or64 (dst: ^i64, desired: i64) -> i64 #cc_std #link_name "InterlockedOr64";
interlocked_compare_exchange64 :: proc(dst: ^i64, exchange, comparand: i64) -> i64 #cc_std #link_name "InterlockedCompareExchange64" ---;
interlocked_exchange64 :: proc(dst: ^i64, desired: i64) -> i64 #cc_std #link_name "InterlockedExchange64" ---;
interlocked_exchange_add64 :: proc(dst: ^i64, desired: i64) -> i64 #cc_std #link_name "InterlockedExchangeAdd64" ---;
interlocked_and64 :: proc(dst: ^i64, desired: i64) -> i64 #cc_std #link_name "InterlockedAnd64" ---;
interlocked_or64 :: proc(dst: ^i64, desired: i64) -> i64 #cc_std #link_name "InterlockedOr64" ---;
proc mm_pause () #cc_std #link_name "_mm_pause";
proc read_write_barrier() #cc_std #link_name "ReadWriteBarrier";
proc write_barrier () #cc_std #link_name "WriteBarrier";
proc read_barrier () #cc_std #link_name "ReadBarrier";
mm_pause :: proc() #cc_std #link_name "_mm_pause" ---;
read_write_barrier :: proc() #cc_std #link_name "ReadWriteBarrier" ---;
write_barrier :: proc() #cc_std #link_name "WriteBarrier" ---;
read_barrier :: proc() #cc_std #link_name "ReadBarrier" ---;
proc load_library_a (c_str: ^u8) -> Hmodule #cc_std #link_name "LoadLibraryA";
proc free_library (h: Hmodule) #cc_std #link_name "FreeLibrary";
proc get_proc_address(h: Hmodule, c_str: ^u8) -> Proc #cc_std #link_name "GetProcAddress";
load_library_a :: proc(c_str: ^u8) -> Hmodule #cc_std #link_name "LoadLibraryA" ---;
free_library :: proc(h: Hmodule) #cc_std #link_name "FreeLibrary" ---;
get_proc_address :: proc(h: Hmodule, c_str: ^u8) -> Proc #cc_std #link_name "GetProcAddress" ---;
}
foreign user32 {
proc get_desktop_window () -> Hwnd #cc_std #link_name "GetDesktopWindow";
proc show_cursor (show : Bool) #cc_std #link_name "ShowCursor";
proc get_cursor_pos (p: ^Point) -> i32 #cc_std #link_name "GetCursorPos";
proc screen_to_client (h: Hwnd, p: ^Point) -> i32 #cc_std #link_name "ScreenToClient";
proc post_quit_message (exit_code: i32) #cc_std #link_name "PostQuitMessage";
proc set_window_text_a (hwnd: Hwnd, c_string: ^u8) -> Bool #cc_std #link_name "SetWindowTextA";
proc register_class_ex_a (wc: ^WndClassExA) -> i16 #cc_std #link_name "RegisterClassExA";
get_desktop_window :: proc() -> Hwnd #cc_std #link_name "GetDesktopWindow" ---;
show_cursor :: proc(show : Bool) #cc_std #link_name "ShowCursor" ---;
get_cursor_pos :: proc(p: ^Point) -> i32 #cc_std #link_name "GetCursorPos" ---;
screen_to_client :: proc(h: Hwnd, p: ^Point) -> i32 #cc_std #link_name "ScreenToClient" ---;
post_quit_message :: proc(exit_code: i32) #cc_std #link_name "PostQuitMessage" ---;
set_window_text_a :: proc(hwnd: Hwnd, c_string: ^u8) -> Bool #cc_std #link_name "SetWindowTextA" ---;
register_class_ex_a :: proc(wc: ^WndClassExA) -> i16 #cc_std #link_name "RegisterClassExA" ---;
proc create_window_ex_a (ex_style: u32,
class_name, title: ^u8,
style: u32,
x, y, w, h: i32,
parent: Hwnd, menu: Hmenu, instance: Hinstance,
param: rawptr) -> Hwnd #cc_std #link_name "CreateWindowExA";
create_window_ex_a :: proc(ex_style: u32,
class_name, title: ^u8,
style: u32,
x, y, w, h: i32,
parent: Hwnd, menu: Hmenu, instance: Hinstance,
param: rawptr) -> Hwnd #cc_std #link_name "CreateWindowExA" ---;
proc show_window (hwnd: Hwnd, cmd_show: i32) -> Bool #cc_std #link_name "ShowWindow";
proc translate_message (msg: ^Msg) -> Bool #cc_std #link_name "TranslateMessage";
proc dispatch_message_a (msg: ^Msg) -> Lresult #cc_std #link_name "DispatchMessageA";
proc update_window (hwnd: Hwnd) -> Bool #cc_std #link_name "UpdateWindow";
proc get_message_a (msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max : u32) -> Bool #cc_std #link_name "GetMessageA";
proc peek_message_a (msg: ^Msg, hwnd: Hwnd,
msg_filter_min, msg_filter_max, remove_msg: u32) -> Bool #cc_std #link_name "PeekMessageA";
show_window :: proc(hwnd: Hwnd, cmd_show: i32) -> Bool #cc_std #link_name "ShowWindow" ---;
translate_message :: proc(msg: ^Msg) -> Bool #cc_std #link_name "TranslateMessage" ---;
dispatch_message_a :: proc(msg: ^Msg) -> Lresult #cc_std #link_name "DispatchMessageA" ---;
update_window :: proc(hwnd: Hwnd) -> Bool #cc_std #link_name "UpdateWindow" ---;
get_message_a :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max : u32) -> Bool #cc_std #link_name "GetMessageA" ---;
peek_message_a :: proc(msg: ^Msg, hwnd: Hwnd,
msg_filter_min, msg_filter_max, remove_msg: u32) -> Bool #cc_std #link_name "PeekMessageA" ---;
proc post_message (hwnd: Hwnd, msg, wparam, lparam : u32) -> Bool #cc_std #link_name "PostMessageA";
post_message :: proc(hwnd: Hwnd, msg, wparam, lparam : u32) -> Bool #cc_std #link_name "PostMessageA" ---;
proc def_window_proc_a (hwnd: Hwnd, msg: u32, wparam: Wparam, lparam: Lparam) -> Lresult #cc_std #link_name "DefWindowProcA";
def_window_proc_a :: proc(hwnd: Hwnd, msg: u32, wparam: Wparam, lparam: Lparam) -> Lresult #cc_std #link_name "DefWindowProcA" ---;
proc adjust_window_rect (rect: ^Rect, style: u32, menu: Bool) -> Bool #cc_std #link_name "AdjustWindowRect";
proc get_active_window () -> Hwnd #cc_std #link_name "GetActiveWindow";
adjust_window_rect :: proc(rect: ^Rect, style: u32, menu: Bool) -> Bool #cc_std #link_name "AdjustWindowRect" ---;
get_active_window :: proc() -> Hwnd #cc_std #link_name "GetActiveWindow" ---;
proc destroy_window (wnd: Hwnd) -> Bool #cc_std #link_name "DestroyWindow";
proc describe_pixel_format(dc: Hdc, pixel_format: i32, bytes : u32, pfd: ^PixelFormatDescriptor) -> i32 #cc_std #link_name "DescribePixelFormat";
destroy_window :: proc(wnd: Hwnd) -> Bool #cc_std #link_name "DestroyWindow" ---;
describe_pixel_format :: proc(dc: Hdc, pixel_format: i32, bytes : u32, pfd: ^PixelFormatDescriptor) -> i32 #cc_std #link_name "DescribePixelFormat" ---;
proc get_monitor_info_a (monitor: Hmonitor, mi: ^MonitorInfo) -> Bool #cc_std #link_name "GetMonitorInfoA";
proc monitor_from_window (wnd: Hwnd, flags : u32) -> Hmonitor #cc_std #link_name "MonitorFromWindow";
get_monitor_info_a :: proc(monitor: Hmonitor, mi: ^MonitorInfo) -> Bool #cc_std #link_name "GetMonitorInfoA" ---;
monitor_from_window :: proc(wnd: Hwnd, flags : u32) -> Hmonitor #cc_std #link_name "MonitorFromWindow" ---;
proc set_window_pos (wnd: Hwnd, wndInsertAfter: Hwnd, x, y, width, height: i32, flags: u32) #cc_std #link_name "SetWindowPos";
set_window_pos :: proc(wnd: Hwnd, wndInsertAfter: Hwnd, x, y, width, height: i32, flags: u32) #cc_std #link_name "SetWindowPos" ---;
proc get_window_placement (wnd: Hwnd, wndpl: ^WindowPlacement) -> Bool #cc_std #link_name "GetWindowPlacement";
proc set_window_placement (wnd: Hwnd, wndpl: ^WindowPlacement) -> Bool #cc_std #link_name "SetWindowPlacement";
proc get_window_rect (wnd: Hwnd, rect: ^Rect) -> Bool #cc_std #link_name "GetWindowRect";
get_window_placement :: proc(wnd: Hwnd, wndpl: ^WindowPlacement) -> Bool #cc_std #link_name "GetWindowPlacement" ---;
set_window_placement :: proc(wnd: Hwnd, wndpl: ^WindowPlacement) -> Bool #cc_std #link_name "SetWindowPlacement" ---;
get_window_rect :: proc(wnd: Hwnd, rect: ^Rect) -> Bool #cc_std #link_name "GetWindowRect" ---;
proc get_window_long_ptr_a(wnd: Hwnd, index: i32) -> i64 #cc_std #link_name "GetWindowLongPtrA";
proc set_window_long_ptr_a(wnd: Hwnd, index: i32, new: i64) -> i64 #cc_std #link_name "SetWindowLongPtrA";
get_window_long_ptr_a :: proc(wnd: Hwnd, index: i32) -> i64 #cc_std #link_name "GetWindowLongPtrA" ---;
set_window_long_ptr_a :: proc(wnd: Hwnd, index: i32, new: i64) -> i64 #cc_std #link_name "SetWindowLongPtrA" ---;
proc get_window_text (wnd: Hwnd, str: ^u8, maxCount: i32) -> i32 #cc_std #link_name "GetWindowText";
get_window_text :: proc(wnd: Hwnd, str: ^u8, maxCount: i32) -> i32 #cc_std #link_name "GetWindowText" ---;
proc get_client_rect (hwnd: Hwnd, rect: ^Rect) -> Bool #cc_std #link_name "GetClientRect";
get_client_rect :: proc(hwnd: Hwnd, rect: ^Rect) -> Bool #cc_std #link_name "GetClientRect" ---;
proc get_dc (h: Hwnd) -> Hdc #cc_std #link_name "GetDC";
proc release_dc (wnd: Hwnd, hdc: Hdc) -> i32 #cc_std #link_name "ReleaseDC";
get_dc :: proc(h: Hwnd) -> Hdc #cc_std #link_name "GetDC" ---;
release_dc :: proc(wnd: Hwnd, hdc: Hdc) -> i32 #cc_std #link_name "ReleaseDC" ---;
proc map_virtual_key(scancode : u32, map_type : u32) -> u32 #cc_std #link_name "MapVirtualKeyA";
map_virtual_key :: proc(scancode : u32, map_type : u32) -> u32 #cc_std #link_name "MapVirtualKeyA" ---;
proc get_key_state (v_key: i32) -> i16 #cc_std #link_name "GetKeyState";
proc get_async_key_state(v_key: i32) -> i16 #cc_std #link_name "GetAsyncKeyState";
get_key_state :: proc(v_key: i32) -> i16 #cc_std #link_name "GetKeyState" ---;
get_async_key_state :: proc(v_key: i32) -> i16 #cc_std #link_name "GetAsyncKeyState" ---;
}
foreign gdi32 {
proc get_stock_object(fn_object: i32) -> Hgdiobj #cc_std #link_name "GetStockObject";
get_stock_object :: proc(fn_object: i32) -> Hgdiobj #cc_std #link_name "GetStockObject" ---;
proc stretch_dibits( hdc: Hdc,
stretch_dibits :: proc(hdc: Hdc,
x_dst, y_dst, width_dst, height_dst: i32,
x_src, y_src, width_src, header_src: i32,
bits: rawptr, bits_info: ^BitmapInfo,
usage: u32,
rop: u32) -> i32 #cc_std #link_name "StretchDIBits";
rop: u32) -> i32 #cc_std #link_name "StretchDIBits" ---;
proc set_pixel_format (hdc: Hdc, pixel_format: i32, pfd: ^PixelFormatDescriptor) -> Bool #cc_std #link_name "SetPixelFormat";
proc choose_pixel_format(hdc: Hdc, pfd: ^PixelFormatDescriptor) -> i32 #cc_std #link_name "ChoosePixelFormat";
proc swap_buffers (hdc: Hdc) -> Bool #cc_std #link_name "SwapBuffers";
set_pixel_format :: proc(hdc: Hdc, pixel_format: i32, pfd: ^PixelFormatDescriptor) -> Bool #cc_std #link_name "SetPixelFormat" ---;
choose_pixel_format :: proc(hdc: Hdc, pfd: ^PixelFormatDescriptor) -> i32 #cc_std #link_name "ChoosePixelFormat" ---;
swap_buffers :: proc(hdc: Hdc) -> Bool #cc_std #link_name "SwapBuffers" ---;
}
foreign shell32 {
proc command_line_to_argv_w(cmd_list: ^u16, num_args: ^i32) -> ^^u16 #cc_std #link_name "CommandLineToArgvW";
command_line_to_argv_w :: proc(cmd_list: ^u16, num_args: ^i32) -> ^^u16 #cc_std #link_name "CommandLineToArgvW" ---;
}
foreign winmm {
proc time_get_time() -> u32 #cc_std #link_name "timeGetTime";
time_get_time :: proc() -> u32 #cc_std #link_name "timeGetTime" ---;
}
proc get_query_performance_frequency() -> i64 {
get_query_performance_frequency :: proc() -> i64 {
r: i64;
query_performance_frequency(&r);
return r;
}
proc HIWORD(wParam: Wparam) -> u16 { return u16((u32(wParam) >> 16) & 0xffff); }
proc HIWORD(lParam: Lparam) -> u16 { return u16((u32(lParam) >> 16) & 0xffff); }
proc LOWORD(wParam: Wparam) -> u16 { return u16(wParam); }
proc LOWORD(lParam: Lparam) -> u16 { return u16(lParam); }
HIWORD :: proc(wParam: Wparam) -> u16 { return u16((u32(wParam) >> 16) & 0xffff); }
HIWORD :: proc(lParam: Lparam) -> u16 { return u16((u32(lParam) >> 16) & 0xffff); }
LOWORD :: proc(wParam: Wparam) -> u16 { return u16(wParam); }
LOWORD :: proc(lParam: Lparam) -> u16 { return u16(lParam); }
proc is_key_down(key: KeyCode) -> bool #inline { return get_async_key_state(i32(key)) < 0; }
is_key_down :: proc(key: KeyCode) -> bool #inline { return get_async_key_state(i32(key)) < 0; }

View File

@@ -1,4 +1,4 @@
proc is_signed(info: ^TypeInfo) -> bool {
is_signed :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
match i in type_info_base(info) {
case TypeInfo.Integer: return i.signed;
@@ -6,92 +6,92 @@ proc is_signed(info: ^TypeInfo) -> bool {
}
return false;
}
proc is_integer(info: ^TypeInfo) -> bool {
is_integer :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.Integer);
return ok;
}
proc is_float(info: ^TypeInfo) -> bool {
is_float :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.Float);
return ok;
}
proc is_complex(info: ^TypeInfo) -> bool {
is_complex :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.Complex);
return ok;
}
proc is_any(info: ^TypeInfo) -> bool {
is_any :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.Any);
return ok;
}
proc is_string(info: ^TypeInfo) -> bool {
is_string :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.String);
return ok;
}
proc is_boolean(info: ^TypeInfo) -> bool {
is_boolean :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.Boolean);
return ok;
}
proc is_pointer(info: ^TypeInfo) -> bool {
is_pointer :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.Pointer);
return ok;
}
proc is_procedure(info: ^TypeInfo) -> bool {
is_procedure :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.Procedure);
return ok;
}
proc is_array(info: ^TypeInfo) -> bool {
is_array :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.Array);
return ok;
}
proc is_dynamic_array(info: ^TypeInfo) -> bool {
is_dynamic_array :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.DynamicArray);
return ok;
}
proc is_dynamic_map(info: ^TypeInfo) -> bool {
is_dynamic_map :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.Map);
return ok;
}
proc is_slice(info: ^TypeInfo) -> bool {
is_slice :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.Slice);
return ok;
}
proc is_vector(info: ^TypeInfo) -> bool {
is_vector :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.Vector);
return ok;
}
proc is_tuple(info: ^TypeInfo) -> bool {
is_tuple :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.Tuple);
return ok;
}
proc is_struct(info: ^TypeInfo) -> bool {
is_struct :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.Struct);
return ok;
}
proc is_union(info: ^TypeInfo) -> bool {
is_union :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.Union);
return ok;
}
proc is_raw_union(info: ^TypeInfo) -> bool {
is_raw_union :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.RawUnion);
return ok;
}
proc is_enum(info: ^TypeInfo) -> bool {
is_enum :: proc(info: ^TypeInfo) -> bool {
if info == nil { return false; }
_, ok := type_info_base(info).(^TypeInfo.Enum);
return ok;

View File

@@ -7,11 +7,11 @@ _surr3 :: 0xe000;
_surr_self :: 0x10000;
proc is_surrogate(r: rune) -> bool {
is_surrogate :: proc(r: rune) -> bool {
return _surr1 <= r && r < _surr3;
}
proc decode_surrogate_pair(r1, r2: rune) -> rune {
decode_surrogate_pair :: proc(r1, r2: rune) -> rune {
if _surr1 <= r1 && r1 < _surr2 && _surr2 <= r2 && r2 < _surr3 {
return (r1-_surr1)<<10 | (r2 - _surr2) + _surr_self;
}
@@ -19,7 +19,7 @@ proc decode_surrogate_pair(r1, r2: rune) -> rune {
}
proc encode_surrogate_pair(r: rune) -> (r1, r2: rune) {
encode_surrogate_pair :: proc(r: rune) -> (r1, r2: rune) {
if r < _surr_self || r > MAX_RUNE {
return REPLACEMENT_CHAR, REPLACEMENT_CHAR;
}
@@ -27,7 +27,7 @@ proc encode_surrogate_pair(r: rune) -> (r1, r2: rune) {
return _surr1 + (r>>10)&0x3ff, _surr2 + r&0x3ff;
}
proc encode(d: []u16, s: []rune) {
encode :: proc(d: []u16, s: []rune) {
n := len(s);
for r in s {
if r >= _surr_self {

View File

@@ -58,7 +58,7 @@ accept_sizes := [256]u8{
0x34, 0x04, 0x04, 0x04, 0x44, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xf0-0xff
};
proc encode_rune(r: rune) -> ([4]u8, int) {
encode_rune :: proc(r: rune) -> ([4]u8, int) {
buf: [4]u8;
i := u32(r);
mask: u8 : 0x3f;
@@ -92,8 +92,8 @@ proc encode_rune(r: rune) -> ([4]u8, int) {
return buf, 4;
}
proc decode_rune(s: string) -> (rune, int) #inline { return decode_rune([]u8(s)); }
proc decode_rune(s: []u8) -> (rune, int) {
decode_rune :: proc(s: string) -> (rune, int) #inline { return decode_rune([]u8(s)); }
decode_rune :: proc(s: []u8) -> (rune, int) {
n := len(s);
if n < 1 {
return RUNE_ERROR, 0;
@@ -132,8 +132,8 @@ proc decode_rune(s: []u8) -> (rune, int) {
proc decode_last_rune(s: string) -> (rune, int) #inline { return decode_last_rune([]u8(s)); }
proc decode_last_rune(s: []u8) -> (rune, int) {
decode_last_rune :: proc(s: string) -> (rune, int) #inline { return decode_last_rune([]u8(s)); }
decode_last_rune :: proc(s: []u8) -> (rune, int) {
r: rune;
size: int;
start, end, limit: int;
@@ -171,7 +171,7 @@ proc decode_last_rune(s: []u8) -> (rune, int) {
proc valid_rune(r: rune) -> bool {
valid_rune :: proc(r: rune) -> bool {
if r < 0 {
return false;
} else if SURROGATE_MIN <= r && r <= SURROGATE_MAX {
@@ -182,7 +182,7 @@ proc valid_rune(r: rune) -> bool {
return true;
}
proc valid_string(s: string) -> bool {
valid_string :: proc(s: string) -> bool {
n := len(s);
for i := 0; i < n; {
si := s[i];
@@ -215,10 +215,10 @@ proc valid_string(s: string) -> bool {
return true;
}
proc rune_start(b: u8) -> bool #inline { return b&0xc0 != 0x80; }
rune_start :: proc(b: u8) -> bool #inline { return b&0xc0 != 0x80; }
proc rune_count(s: string) -> int #inline { return rune_count([]u8(s)); }
proc rune_count(s: []u8) -> int {
rune_count :: proc(s: string) -> int #inline { return rune_count([]u8(s)); }
rune_count :: proc(s: []u8) -> int {
count := 0;
n := len(s);
@@ -257,7 +257,7 @@ proc rune_count(s: []u8) -> int {
}
proc rune_size(r: rune) -> int {
rune_size :: proc(r: rune) -> int {
match {
case r < 0: return -1;
case r <= 1<<7 - 1: return 1;

View File

@@ -311,9 +311,9 @@ void init_entity_foreign_library(Checker *c, Entity *e) {
void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
GB_ASSERT(e->type == NULL);
if (d->proc_decl->kind != AstNode_ProcDecl) {
if (d->proc_lit->kind != AstNode_ProcLit) {
// TOOD(bill): Better error message
error(d->proc_decl, "Expected a procedure to check");
error(d->proc_lit, "Expected a procedure to check");
return;
}
@@ -324,19 +324,19 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
proc_type = make_type_proc(c->allocator, e->scope, NULL, 0, NULL, 0, false, ProcCC_Odin);
}
e->type = proc_type;
ast_node(pd, ProcDecl, d->proc_decl);
ast_node(pl, ProcLit, d->proc_lit);
check_open_scope(c, pd->type);
check_open_scope(c, pl->type);
defer (check_close_scope(c));
check_procedure_type(c, proc_type, pd->type);
check_procedure_type(c, proc_type, pl->type);
bool is_foreign = (pd->tags & ProcTag_foreign) != 0;
bool is_link_name = (pd->tags & ProcTag_link_name) != 0;
bool is_export = (pd->tags & ProcTag_export) != 0;
bool is_inline = (pd->tags & ProcTag_inline) != 0;
bool is_no_inline = (pd->tags & ProcTag_no_inline) != 0;
bool is_require_results = (pd->tags & ProcTag_require_results) != 0;
bool is_foreign = (pl->tags & ProcTag_foreign) != 0;
bool is_link_name = (pl->tags & ProcTag_link_name) != 0;
bool is_export = (pl->tags & ProcTag_export) != 0;
bool is_inline = (pl->tags & ProcTag_inline) != 0;
bool is_no_inline = (pl->tags & ProcTag_no_inline) != 0;
bool is_require_results = (pl->tags & ProcTag_require_results) != 0;
TypeProc *pt = &proc_type->Proc;
@@ -356,16 +356,16 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
}
if (is_inline && is_no_inline) {
error(pd->type, "You cannot apply both `inline` and `no_inline` to a procedure");
error(pl->type, "You cannot apply both `inline` and `no_inline` to a procedure");
}
if (is_foreign && is_export) {
error(pd->type, "A foreign procedure cannot have an `export` tag");
error(pl->type, "A foreign procedure cannot have an `export` tag");
}
if (pt->is_generic) {
if (pd->body == NULL) {
if (pl->body == NULL) {
error(e->token, "Polymorphic procedures must have a body");
}
@@ -375,24 +375,24 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
}
}
if (pd->body != NULL) {
if (pl->body != NULL) {
if (is_foreign) {
error(pd->body, "A foreign procedure cannot have a body");
error(pl->body, "A foreign procedure cannot have a body");
}
if (proc_type->Proc.c_vararg) {
error(pd->body, "A procedure with a `#c_vararg` field cannot have a body");
error(pl->body, "A procedure with a `#c_vararg` field cannot have a body");
}
d->scope = c->context.scope;
GB_ASSERT(pd->body->kind == AstNode_BlockStmt);
check_procedure_later(c, c->curr_ast_file, e->token, d, proc_type, pd->body, pd->tags);
GB_ASSERT(pl->body->kind == AstNode_BlockStmt);
check_procedure_later(c, c->curr_ast_file, e->token, d, proc_type, pl->body, pl->tags);
} else if (!is_foreign) {
error(e->token, "Only a foreign procedure cannot have a body");
}
if (pt->result_count == 0 && is_require_results) {
error(pd->type, "`#require_results` is not needed on a procedure with no results");
error(pl->type, "`#require_results` is not needed on a procedure with no results");
} else {
pt->require_results = is_require_results;
}
@@ -401,8 +401,8 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
if (is_foreign) {
String name = e->token.string;
if (pd->link_name.len > 0) {
name = pd->link_name;
if (pl->link_name.len > 0) {
name = pl->link_name;
}
e->Procedure.is_foreign = true;
e->Procedure.link_name = name;
@@ -420,16 +420,16 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
Type *other_type = base_type(f->type);
if (is_type_proc(this_type) && is_type_proc(other_type)) {
if (!are_signatures_similar_enough(this_type, other_type)) {
error(d->proc_decl,
"Redeclaration of foreign procedure `%.*s` with different type signatures\n"
"\tat %.*s(%td:%td)",
LIT(name), LIT(pos.file), pos.line, pos.column);
error(d->proc_lit,
"Redeclaration of foreign procedure `%.*s` with different type signatures\n"
"\tat %.*s(%td:%td)",
LIT(name), LIT(pos.file), pos.line, pos.column);
}
} else if (!are_types_identical(this_type, other_type)) {
error(d->proc_decl,
"Foreign entity `%.*s` previously declared elsewhere with a different type\n"
"\tat %.*s(%td:%td)",
LIT(name), LIT(pos.file), pos.line, pos.column);
error(d->proc_lit,
"Foreign entity `%.*s` previously declared elsewhere with a different type\n"
"\tat %.*s(%td:%td)",
LIT(name), LIT(pos.file), pos.line, pos.column);
}
} else {
map_set(fp, key, e);
@@ -437,7 +437,7 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
} else {
String name = e->token.string;
if (is_link_name) {
name = pd->link_name;
name = pl->link_name;
}
if (is_link_name || is_export) {
@@ -451,10 +451,10 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
Entity *f = *found;
TokenPos pos = f->token.pos;
// TODO(bill): Better error message?
error(d->proc_decl,
"Non unique linking name for procedure `%.*s`\n"
"\tother at %.*s(%td:%td)",
LIT(name), LIT(pos.file), pos.line, pos.column);
error(d->proc_lit,
"Non unique linking name for procedure `%.*s`\n"
"\tother at %.*s(%td:%td)",
LIT(name), LIT(pos.file), pos.line, pos.column);
} else {
map_set(fp, key, e);
}

View File

@@ -5034,10 +5034,10 @@ Entity *find_or_generate_polymorphic_procedure(Checker *c, Entity *base_entity,
}
AstNode *proc_decl = clone_ast_node(a, old_decl->proc_decl);
ast_node(pd, ProcDecl, proc_decl);
AstNode *proc_lit = clone_ast_node(a, old_decl->proc_lit);
ast_node(pl, ProcLit, proc_lit);
// NOTE(bill): Associate the scope declared above with this procedure declaration's type
add_scope(c, pd->type, final_proc_type->Proc.scope);
add_scope(c, pl->type, final_proc_type->Proc.scope);
final_proc_type->Proc.is_generic_specialized = true;
u64 tags = base_entity->Procedure.tags;
@@ -5045,8 +5045,8 @@ Entity *find_or_generate_polymorphic_procedure(Checker *c, Entity *base_entity,
Token token = ident->Ident;
DeclInfo *d = make_declaration_info(c->allocator, c->context.scope, old_decl->parent);
d->gen_proc_type = final_proc_type;
d->type_expr = pd->type;
d->proc_decl = proc_decl;
d->type_expr = pl->type;
d->proc_lit = proc_lit;
Entity *entity = make_entity_procedure(c->allocator, NULL, token, final_proc_type, tags);
@@ -5061,7 +5061,7 @@ Entity *find_or_generate_polymorphic_procedure(Checker *c, Entity *base_entity,
proc_info.token = token;
proc_info.decl = d;
proc_info.type = final_proc_type;
proc_info.body = pd->body;
proc_info.body = pl->body;
proc_info.tags = tags;
}
@@ -5926,7 +5926,7 @@ ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *t
check_open_scope(c, pl->type);
{
decl = make_declaration_info(c->allocator, c->context.scope, c->context.decl);
decl->proc_decl = node;
decl->proc_lit = node;
c->context.decl = decl;
if (pl->tags != 0) {

View File

@@ -1812,161 +1812,5 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
}
}
case_end;
case_ast_node(gd, GenDecl, node);
GB_ASSERT(!c->context.scope->is_file);
for_array(i, gd->specs) {
AstNode *spec = gd->specs[i];
switch (gd->token.kind) {
case Token_var: {
ast_node(vd, ValueSpec, spec);
Entity **entities = gb_alloc_array(c->allocator, Entity *, vd->names.count);
isize entity_count = 0;
if (gd->flags & VarDeclFlag_thread_local) {
gd->flags &= ~VarDeclFlag_thread_local;
error(node, "`thread_local` may only be applied to a variable declaration");
}
for_array(i, vd->names) {
AstNode *name = vd->names[i];
Entity *entity = NULL;
if (name->kind != AstNode_Ident) {
error(name, "A variable declaration must be an identifier");
} else {
Token token = name->Ident;
String str = token.string;
Entity *found = NULL;
// NOTE(bill): Ignore assignments to `_`
if (str != "_") {
found = current_scope_lookup_entity(c->context.scope, str);
}
if (found == NULL) {
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL, false);
entity->identifier = name;
AstNode *fl = c->context.curr_foreign_library;
if (fl != NULL) {
GB_ASSERT(fl->kind == AstNode_Ident);
entity->Variable.is_foreign = true;
entity->Variable.foreign_library_ident = fl;
}
} else {
TokenPos pos = found->token.pos;
error(token,
"Redeclaration of `%.*s` in this scope\n"
"\tat %.*s(%td:%td)",
LIT(str), LIT(pos.file), pos.line, pos.column);
entity = found;
}
}
if (entity == NULL) {
entity = make_entity_dummy_variable(c->allocator, c->global_scope, ast_node_token(name));
}
entity->parent_proc_decl = c->context.curr_proc_decl;
entities[entity_count++] = entity;
}
Type *init_type = NULL;
if (vd->type) {
init_type = check_type(c, vd->type, NULL);
if (init_type == NULL) {
init_type = t_invalid;
}
}
for (isize i = 0; i < entity_count; i++) {
Entity *e = entities[i];
GB_ASSERT(e != NULL);
if (e->flags & EntityFlag_Visited) {
e->type = t_invalid;
continue;
}
e->flags |= EntityFlag_Visited;
if (e->type == NULL) {
e->type = init_type;
}
}
check_arity_match(c, vd);
check_init_variables(c, entities, entity_count, vd->values, str_lit("variable declaration"));
for (isize i = 0; i < entity_count; i++) {
Entity *e = entities[i];
if (e->Variable.is_foreign) {
if (vd->values.count > 0) {
error(e->token, "A foreign variable declaration cannot have a default value");
}
init_entity_foreign_library(c, e);
String name = e->token.string;
auto *fp = &c->info.foreigns;
HashKey key = hash_string(name);
Entity **found = map_get(fp, key);
if (found) {
Entity *f = *found;
TokenPos pos = f->token.pos;
Type *this_type = base_type(e->type);
Type *other_type = base_type(f->type);
if (!are_types_identical(this_type, other_type)) {
error(e->token,
"Foreign entity `%.*s` previously declared elsewhere with a different type\n"
"\tat %.*s(%td:%td)",
LIT(name), LIT(pos.file), pos.line, pos.column);
}
} else {
map_set(fp, key, e);
}
}
add_entity(c, c->context.scope, e->identifier, e);
}
if ((gd->flags & VarDeclFlag_using) != 0) {
Token token = ast_node_token(node);
if (vd->type != NULL && entity_count > 1) {
error(token, "`using` can only be applied to one variable of the same type");
// TODO(bill): Should a `continue` happen here?
}
for (isize entity_index = 0; entity_index < entity_count; entity_index++) {
Entity *e = entities[entity_index];
if (e == NULL) {
continue;
}
if (e->kind != Entity_Variable) {
continue;
}
bool is_immutable = e->Variable.is_immutable;
String name = e->token.string;
Type *t = base_type(type_deref(e->type));
if (is_type_struct(t) || is_type_raw_union(t)) {
Scope *scope = scope_of_node(&c->info, t->Record.node);
for_array(i, scope->elements.entries) {
Entity *f = scope->elements.entries[i].value;
if (f->kind == Entity_Variable) {
Entity *uvar = make_entity_using_variable(c->allocator, e, f->token, f->type);
uvar->Variable.is_immutable = is_immutable;
Entity *prev = scope_insert_entity(c->context.scope, uvar);
if (prev != NULL) {
error(token, "Namespace collision while `using` `%.*s` of: %.*s", LIT(name), LIT(prev->token.string));
return;
}
}
}
} else {
// NOTE(bill): skip the rest to remove extra errors
error(token, "`using` can only be applied to variables of type struct or raw_union");
return;
}
}
}
} break;
}
}
case_end;
}
}

View File

@@ -187,7 +187,7 @@ struct DeclInfo {
AstNode * type_expr;
AstNode * init_expr;
AstNode * proc_decl; // AstNode_ProcDecl
AstNode * proc_lit; // AstNode_ProcLit
Type * gen_proc_type; // Precalculated
Map<bool> deps; // Key: Entity *
@@ -366,10 +366,10 @@ bool decl_info_has_init(DeclInfo *d) {
if (d->init_expr != NULL) {
return true;
}
if (d->proc_decl != NULL) {
switch (d->proc_decl->kind) {
case_ast_node(pd, ProcDecl, d->proc_decl);
if (pd->body != NULL) {
if (d->proc_lit != NULL) {
switch (d->proc_lit->kind) {
case_ast_node(pl, ProcLit, d->proc_lit);
if (pl->body != NULL) {
return true;
}
case_end;
@@ -1386,7 +1386,6 @@ void init_preload(Checker *c) {
bool check_arity_match(Checker *c, AstNodeValueSpec *s);
bool check_arity_match(Checker *c, AstNodeValueDecl *vd);
void check_collect_entities(Checker *c, Array<AstNode *> nodes, bool is_file_scope);
void check_collect_entities_from_when_stmt(Checker *c, AstNodeWhenStmt *ws, bool is_file_scope);
@@ -1507,38 +1506,6 @@ void check_procedure_overloading(Checker *c, Entity *e) {
bool check_arity_match(Checker *c, AstNodeValueSpec *spec) {
isize lhs = spec->names.count;
isize rhs = spec->values.count;
if (rhs == 0) {
if (spec->type == NULL) {
error(spec->names[0], "Missing type or initial expression");
return false;
}
} else if (lhs < rhs) {
if (lhs < spec->values.count) {
AstNode *n = spec->values[lhs];
gbString str = expr_to_string(n);
error(n, "Extra initial expression `%s`", str);
gb_string_free(str);
} else {
error(spec->names[0], "Extra initial expression");
}
return false;
} else if (lhs > rhs && rhs != 1) {
AstNode *n = spec->names[rhs];
gbString str = expr_to_string(n);
error(n, "Missing expression for `%s`", str);
gb_string_free(str);
return false;
}
return true;
}
bool check_arity_match(Checker *c, AstNodeValueDecl *vd) {
isize lhs = vd->names.count;
isize rhs = vd->values.count;
@@ -1705,6 +1672,10 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes, bool is_file_sco
}
AstNode *init = unparen_expr(vd->values[i]);
if (init == NULL) {
error(name, "Expected a value for this constant value declaration");
continue;
}
AstNode *fl = c->context.curr_foreign_library;
DeclInfo *d = make_declaration_info(c->allocator, c->context.scope, c->context.decl);
@@ -1722,8 +1693,7 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes, bool is_file_sco
e->Procedure.foreign_library_ident = fl;
pl->tags |= ProcTag_foreign;
}
GB_PANIC("TODO(bill): Constant procedure literals");
d->proc_decl = init;
d->proc_lit = init;
d->type_expr = pl->type;
} else {
e = make_entity_constant(c->allocator, d->scope, name->Ident, NULL, empty_exact_value);
@@ -1733,7 +1703,7 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes, bool is_file_sco
e->identifier = name;
if (fl != NULL && e->kind != Entity_Procedure) {
error(name, "Only procedures and variables are allowed to be in a foreign block");
error(name, "Only procedures and variables are allowed to be in a foreign block, got %.*s", LIT(ast_node_strings[init->kind]));
// continue;
}
@@ -1746,137 +1716,9 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes, bool is_file_sco
case_end;
case_ast_node(gd, GenDecl, decl);
AstNodeValueSpec empty_spec = {};
AstNodeValueSpec *last_spec = NULL;
for_array(i, gd->specs) {
AstNode *spec = gd->specs[i];
switch (gd->token.kind) {
case Token_const: {
ast_node(vs, ValueSpec, spec);
if (vs->type != NULL || vs->values.count > 0) {
last_spec = vs;
} else if (last_spec == NULL) {
last_spec = &empty_spec;
}
for_array(i, vs->names) {
AstNode *name = vs->names[i];
if (name->kind != AstNode_Ident) {
error(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[name->kind]));
continue;
}
AstNode *init = NULL;
if (i < vs->values.count) {
init = vs->values[i];
}
DeclInfo *d = make_declaration_info(c->allocator, c->context.scope, c->context.decl);
Entity *e = make_entity_constant(c->allocator, d->scope, name->Ident, NULL, empty_exact_value);
d->type_expr = last_spec->type;
d->init_expr = init;
e->identifier = name;
add_entity_and_decl_info(c, name, e, d);
}
check_arity_match(c, vs);
} break;
case Token_var: {
if (!c->context.scope->is_file) {
// NOTE(bill): local scope -> handle later and in order
break;
}
ast_node(vs, ValueSpec, spec);
// NOTE(bill): You need to store the entity information here unline a constant declaration
isize entity_cap = vs->names.count;
isize entity_count = 0;
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_cap);
DeclInfo *di = NULL;
if (vs->values.count > 0) {
di = make_declaration_info(heap_allocator(), c->context.scope, c->context.decl);
di->entities = entities;
di->type_expr = vs->type;
di->init_expr = vs->values[0];
if (gd->flags & VarDeclFlag_thread_local) {
error(decl, "#thread_local variable declarations cannot have initialization values");
}
}
for_array(i, vs->names) {
AstNode *name = vs->names[i];
AstNode *value = NULL;
if (i < vs->values.count) {
value = vs->values[i];
}
if (name->kind != AstNode_Ident) {
error(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[name->kind]));
continue;
}
Entity *e = make_entity_variable(c->allocator, c->context.scope, name->Ident, NULL, false);
e->Variable.is_thread_local = (gd->flags & VarDeclFlag_thread_local) != 0;
e->identifier = name;
if (gd->flags & VarDeclFlag_using) {
gd->flags &= ~VarDeclFlag_using; // NOTE(bill): This error will be only caught once
error(name, "`using` is not allowed at the file scope");
}
AstNode *fl = c->context.curr_foreign_library;
if (fl != NULL) {
GB_ASSERT(fl->kind == AstNode_Ident);
e->Variable.is_foreign = true;
e->Variable.foreign_library_ident = fl;
}
entities[entity_count++] = e;
DeclInfo *d = di;
if (d == NULL) {
AstNode *init_expr = value;
d = make_declaration_info(heap_allocator(), e->scope, c->context.decl);
d->type_expr = vs->type;
d->init_expr = init_expr;
}
add_entity_and_decl_info(c, name, e, d);
}
if (di != NULL) {
di->entity_count = entity_count;
}
check_arity_match(c, vs);
} break;
case Token_type: {
ast_node(ts, TypeSpec, spec);
AstNode *name = ts->name;
if (name->kind != AstNode_Ident) {
error(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[name->kind]));
break;
}
DeclInfo *d = make_declaration_info(c->allocator, c->context.scope, c->context.decl);
Entity *e = NULL;
AstNode *type = unparen_expr(ts->type);
e = make_entity_type_name(c->allocator, d->scope, name->Ident, NULL);
d->type_expr = type;
d->init_expr = type;
e->identifier = name;
add_entity_and_decl_info(c, name, e, d);
} break;
case Token_import:
case Token_import_load: {
ast_node(ts, ImportSpec, spec);
@@ -1941,29 +1783,29 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes, bool is_file_sco
c->context = prev_context;
case_end;
case_ast_node(pd, ProcDecl, decl);
AstNode *name = pd->name;
if (name->kind != AstNode_Ident) {
error(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[name->kind]));
break;
}
// case_ast_node(pd, ProcDecl, decl);
// AstNode *name = pd->name;
// if (name->kind != AstNode_Ident) {
// error(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[name->kind]));
// break;
// }
DeclInfo *d = make_declaration_info(c->allocator, c->context.scope, c->context.decl);
Entity *e = NULL;
// DeclInfo *d = make_declaration_info(c->allocator, c->context.scope, c->context.decl);
// Entity *e = NULL;
e = make_entity_procedure(c->allocator, d->scope, name->Ident, NULL, pd->tags);
AstNode *fl = c->context.curr_foreign_library;
if (fl != NULL) {
GB_ASSERT(fl->kind == AstNode_Ident);
e->Procedure.foreign_library_ident = fl;
pd->tags |= ProcTag_foreign;
}
d->proc_decl = decl;
d->type_expr = pd->type;
e->identifier = name;
add_entity_and_decl_info(c, name, e, d);
case_end;
// e = make_entity_procedure(c->allocator, d->scope, name->Ident, NULL, pd->tags);
// AstNode *fl = c->context.curr_foreign_library;
// if (fl != NULL) {
// GB_ASSERT(fl->kind == AstNode_Ident);
// e->Procedure.foreign_library_ident = fl;
// pd->tags |= ProcTag_foreign;
// }
// d->proc_decl = decl;
// d->type_expr = pd->type;
// e->identifier = name;
// add_entity_and_decl_info(c, name, e, d);
// case_end;
default:
if (c->context.scope->is_file) {

View File

@@ -32,6 +32,7 @@ String alloc_comment_group_string(gbAllocator a, CommentGroup g) {
return make_string(text, len);
}
#if 0
void print_type_spec(AstNode *spec) {
ast_node(ts, TypeSpec, spec);
GB_ASSERT(ts->name->kind == AstNode_Ident);
@@ -87,20 +88,13 @@ void print_proc_decl(AstNodeProcDecl *pd) {
}
gb_printf("\n\n");
}
#endif
void print_declaration(AstNode *decl) {
switch (decl->kind) {
case_ast_node(gd, GenDecl, decl);
for_array(spec_index, gd->specs) {
AstNode *spec = gd->specs[spec_index];
switch(gd->token.kind) {
case Token_var:
break;
case Token_const:
break;
case Token_type:
// print_type_spec(spec);
break;
case Token_import:
case Token_import_load:
break;
@@ -111,9 +105,9 @@ void print_declaration(AstNode *decl) {
}
case_end;
case_ast_node(pd, ProcDecl, decl);
print_proc_decl(pd);
case_end;
// case_ast_node(pd, ProcDecl, decl);
// print_proc_decl(pd);
// case_end;
case_ast_node(fb, ForeignBlockDecl, decl);
// TODO(bill)

View File

@@ -5881,7 +5881,7 @@ void ir_type_case_body(irProcedure *proc, AstNode *label, AstNode *clause, irBlo
}
void ir_build_poly_proc(irProcedure *proc, AstNodeProcDecl *pd, Entity *e) {
void ir_build_poly_proc(irProcedure *proc, AstNodeProcLit *pd, Entity *e) {
GB_ASSERT(pd->body != NULL);
if (is_entity_in_dependency_map(&proc->module->min_dep_map, e) == false) {
@@ -6020,139 +6020,49 @@ void ir_build_stmt_internal(irProcedure *proc, AstNode *node) {
map_set(&proc->module->entity_names, hash_entity(e), name);
ir_gen_global_type_name(proc->module, e, name);
} else if (e->kind == Entity_Procedure) {
GB_PANIC("TODO(bill): Procedure values");
}
}
}
case_end;
case_ast_node(gd, GenDecl, node);
for_array(i, gd->specs) {
AstNode *spec = gd->specs[i];
switch (gd->token.kind) {
case Token_var: {
ast_node(vd, ValueSpec, spec);
irModule *m = proc->module;
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&m->tmp_arena);
if (vd->values.count == 0) { // declared and zero-initialized
for_array(i, vd->names) {
AstNode *name = vd->names[i];
if (!ir_is_blank_ident(name)) {
ir_add_local_for_identifier(proc, name, true);
}
}
} else { // Tuple(s)
Array<irAddr> lvals = {};
Array<irValue *> inits = {};
array_init(&lvals, m->tmp_allocator, vd->names.count);
array_init(&inits, m->tmp_allocator, vd->names.count);
for_array(i, vd->names) {
AstNode *name = vd->names[i];
irAddr lval = ir_addr(NULL);
if (!ir_is_blank_ident(name)) {
ir_add_local_for_identifier(proc, name, false);
lval = ir_build_addr(proc, name);
}
array_add(&lvals, lval);
}
for_array(i, vd->values) {
irValue *init = ir_build_expr(proc, vd->values[i]);
Type *t = ir_type(init);
if (t->kind == Type_Tuple) {
for (isize i = 0; i < t->Tuple.variable_count; i++) {
Entity *e = t->Tuple.variables[i];
irValue *v = ir_emit_struct_ev(proc, init, i);
array_add(&inits, v);
CheckerInfo *info = proc->module->info;
DeclInfo *decl = decl_info_of_entity(info, e);
ast_node(pl, ProcLit, decl->proc_lit);
if (pl->body != NULL) {
if (is_type_gen_proc(e->type)) {
auto found = *map_get(&info->gen_procs, hash_pointer(ident));
for_array(i, found) {
Entity *e = found[i];
DeclInfo *d = decl_info_of_entity(info, e);
ir_build_poly_proc(proc, &d->proc_lit->ProcLit, e);
}
} else {
array_add(&inits, init);
ir_build_poly_proc(proc, pl, e);
}
} else {
// FFI - Foreign function interace
String original_name = e->token.string;
String name = original_name;
if (pl->link_name.len > 0) {
name = pl->link_name;
}
irValue *value = ir_value_procedure(proc->module->allocator,
proc->module, e, e->type, pl->type, pl->body, name);
value->Proc.tags = pl->tags;
ir_module_add_value(proc->module, e, value);
ir_build_proc(value, proc);
if (value->Proc.tags & ProcTag_foreign) {
HashKey key = hash_string(name);
irValue **prev_value = map_get(&proc->module->members, key);
if (prev_value == NULL) {
// NOTE(bill): Don't do mutliple declarations in the IR
map_set(&proc->module->members, key, value);
}
} else {
array_add(&proc->children, &value->Proc);
}
}
for_array(i, inits) {
ir_addr_store(proc, lvals[i], inits[i]);
}
}
gb_temp_arena_memory_end(tmp);
} break;
case Token_type: {
ast_node(td, TypeSpec, spec);
AstNode *ident = td->name;
GB_ASSERT(ident->kind == AstNode_Ident);
Entity *e = entity_of_ident(proc->module->info, ident);
GB_ASSERT(e != NULL);
if (e->kind == Entity_TypeName) {
// NOTE(bill): Generate a new name
// parent_proc.name-guid
String ts_name = e->token.string;
isize name_len = proc->name.len + 1 + ts_name.len + 1 + 10 + 1;
u8 *name_text = gb_alloc_array(proc->module->allocator, u8, name_len);
i32 guid = cast(i32)proc->module->members.entries.count;
name_len = gb_snprintf(cast(char *)name_text, name_len, "%.*s.%.*s-%d", LIT(proc->name), LIT(ts_name), guid);
String name = make_string(name_text, name_len-1);
irValue *value = ir_value_type_name(proc->module->allocator,
name, e->type);
map_set(&proc->module->entity_names, hash_entity(e), name);
ir_gen_global_type_name(proc->module, e, name);
}
} break;
}
}
case_end;
case_ast_node(pd, ProcDecl, node);
AstNode *ident = pd->name;
GB_ASSERT(ident->kind == AstNode_Ident);
CheckerInfo *info = proc->module->info;
Entity *e = entity_of_ident(info, ident);
if (pd->body != NULL) {
if (is_type_gen_proc(e->type)) {
auto found = *map_get(&info->gen_procs, hash_pointer(ident));
for_array(i, found) {
Entity *e = found[i];
DeclInfo *d = decl_info_of_entity(info, e);
ir_build_poly_proc(proc, &d->proc_decl->ProcDecl, e);
}
} else {
ir_build_poly_proc(proc, pd, e);
}
} else {
// FFI - Foreign function interace
String original_name = e->token.string;
String name = original_name;
if (pd->link_name.len > 0) {
name = pd->link_name;
}
irValue *value = ir_value_procedure(proc->module->allocator,
proc->module, e, e->type, pd->type, pd->body, name);
value->Proc.tags = pd->tags;
ir_module_add_value(proc->module, e, value);
ir_build_proc(value, proc);
if (value->Proc.tags & ProcTag_foreign) {
HashKey key = hash_string(name);
irValue **prev_value = map_get(&proc->module->members, key);
if (prev_value == NULL) {
// NOTE(bill): Don't do mutliple declarations in the IR
map_set(&proc->module->members, key, value);
}
} else {
array_add(&proc->children, &value->Proc);
}
}
case_end;
@@ -7514,22 +7424,22 @@ void ir_gen_tree(irGen *s) {
} break;
case Entity_Procedure: {
ast_node(pd, ProcDecl, decl->proc_decl);
ast_node(pl, ProcLit, decl->proc_lit);
String original_name = name;
AstNode *body = pd->body;
AstNode *body = pl->body;
if (e->Procedure.is_foreign) {
name = e->token.string; // NOTE(bill): Don't use the mangled name
ir_add_foreign_library_path(m, e->Procedure.foreign_library);
}
if (pd->link_name.len > 0) {
name = pd->link_name;
if (pl->link_name.len > 0) {
name = pl->link_name;
}
AstNode *type_expr = pd->type;
AstNode *type_expr = pl->type;
irValue *p = ir_value_procedure(a, m, e, e->type, type_expr, body, name);
p->Proc.tags = pd->tags;
p->Proc.tags = pl->tags;
ir_module_add_value(m, e, p);
HashKey hash_name = hash_string(name);

View File

@@ -34,7 +34,6 @@ struct AstFile {
// NOTE(bill): Used to prevent type literals in control clauses
isize expr_level;
bool allow_range; // NOTE(bill): Ranges are only allowed in certain cases
bool allow_gen_proc_type;
bool in_foreign_block;
Array<AstNode *> decls;
@@ -309,15 +308,6 @@ AST_NODE_KIND(_ComplexStmtEnd, "", i32) \
AST_NODE_KIND(_StmtEnd, "", i32) \
AST_NODE_KIND(_DeclBegin, "", i32) \
AST_NODE_KIND(BadDecl, "bad declaration", struct { Token begin, end; }) \
AST_NODE_KIND(ProcDecl, "procedure declaration", struct { \
Token token; \
AstNode *name; \
AstNode *type; \
AstNode *body; \
u64 tags; \
String link_name; \
CommentGroup docs; \
}) \
AST_NODE_KIND(ForeignBlockDecl, "foreign block declaration", struct { \
Token token; \
AstNode * foreign_library; \
@@ -346,19 +336,6 @@ AST_NODE_KIND(_DeclBegin, "", i32) \
CommentGroup docs; \
CommentGroup comment; \
}) \
AST_NODE_KIND(ValueSpec, "value specification", struct { \
Array<AstNode *> names; \
AstNode * type; \
Array<AstNode *> values; \
CommentGroup docs; \
CommentGroup comment; \
}) \
AST_NODE_KIND(TypeSpec, "type specification", struct { \
AstNode *name; \
AstNode *type; \
CommentGroup docs; \
CommentGroup comment; \
}) \
AST_NODE_KIND(ImportSpec, "import specification", struct { \
bool is_import; \
Token relpath; \
@@ -581,15 +558,12 @@ Token ast_node_token(AstNode *node) {
case AstNode_PushContext: return node->PushContext.token;
case AstNode_BadDecl: return node->BadDecl.begin;
case AstNode_ProcDecl: return node->ProcDecl.token;
case AstNode_ForeignLibrarySpec: return node->ForeignLibrarySpec.filepath;
case AstNode_Label: return node->Label.token;
case AstNode_GenDecl: return node->GenDecl.token;
case AstNode_ValueDecl: return ast_node_token(node->ValueDecl.names[0]);
case AstNode_ValueSpec: return ast_node_token(node->ValueSpec.names[0]);
case AstNode_ImportSpec: return node->ImportSpec.import_name;
case AstNode_TypeSpec: return ast_node_token(node->TypeSpec.name);
case AstNode_ForeignBlockDecl: return node->ForeignBlockDecl.token;
@@ -811,12 +785,7 @@ AstNode *clone_ast_node(gbAllocator a, AstNode *node) {
break;
case AstNode_BadDecl: break;
case AstNode_ProcDecl:
n->ProcDecl.name = clone_ast_node(a, n->ProcDecl.name);
n->ProcDecl.type = clone_ast_node(a, n->ProcDecl.type);
n->ProcDecl.body = clone_ast_node(a, n->ProcDecl.body);
// TODO(bill): Clone the comment group too?
break;
case AstNode_ForeignBlockDecl:
n->ForeignBlockDecl.foreign_library = clone_ast_node(a, n->ForeignBlockDecl.foreign_library);
n->ForeignBlockDecl.decls = clone_ast_node_array(a, n->ForeignBlockDecl.decls);
@@ -832,15 +801,6 @@ AstNode *clone_ast_node(gbAllocator a, AstNode *node) {
n->ValueDecl.type = clone_ast_node(a, n->ValueDecl.type);
n->ValueDecl.values = clone_ast_node_array(a, n->ValueDecl.values);
break;
case AstNode_ValueSpec:
n->ValueSpec.names = clone_ast_node_array(a, n->ValueSpec.names);
n->ValueSpec.type = clone_ast_node(a, n->ValueSpec.type);
n->ValueSpec.values = clone_ast_node_array(a, n->ValueSpec.values);
break;
case AstNode_TypeSpec:
n->TypeSpec.name = clone_ast_node(a, n->TypeSpec.name);
n->TypeSpec.type = clone_ast_node(a, n->TypeSpec.type);
break;
case AstNode_ForeignLibrarySpec:
n->ForeignLibrarySpec.cond = clone_ast_node(a, n->ForeignLibrarySpec.cond);
break;
@@ -1504,19 +1464,6 @@ AstNode *ast_map_type(AstFile *f, Token token, AstNode *count, AstNode *key, Ast
}
AstNode *ast_proc_decl(AstFile *f, Token token, AstNode *name, AstNode *type, AstNode *body,
u64 tags, String link_name, CommentGroup docs) {
AstNode *result = make_ast_node(f, AstNode_ProcDecl);
result->ProcDecl.token = token;
result->ProcDecl.name = name;
result->ProcDecl.type = type;
result->ProcDecl.body = body;
result->ProcDecl.tags = tags;
result->ProcDecl.link_name = link_name;
result->ProcDecl.docs = docs;
return result;
}
AstNode *ast_foreign_block_decl(AstFile *f, Token token, AstNode *foreign_library, Token open, Token close, Array<AstNode *> decls,
CommentGroup docs) {
AstNode *result = make_ast_node(f, AstNode_ForeignBlockDecl);
@@ -1558,27 +1505,6 @@ AstNode *ast_value_decl(AstFile *f, Array<AstNode *> names, AstNode *type, Array
return result;
}
AstNode *ast_value_spec(AstFile *f, Array<AstNode *> names, AstNode *type, Array<AstNode *> values,
CommentGroup docs, CommentGroup comment) {
AstNode *result = make_ast_node(f, AstNode_ValueSpec);
result->ValueSpec.names = names;
result->ValueSpec.type = type;
result->ValueSpec.values = values;
result->ValueSpec.docs = docs;
result->ValueSpec.comment = comment;
return result;
}
AstNode *ast_type_spec(AstFile *f, AstNode *name, AstNode *type,
CommentGroup docs, CommentGroup comment) {
AstNode *result = make_ast_node(f, AstNode_TypeSpec);
result->TypeSpec.name = name;
result->TypeSpec.type = type;
result->TypeSpec.docs = docs;
result->TypeSpec.comment = comment;
return result;
}
AstNode *ast_import_spec(AstFile *f, bool is_import, Token relpath, Token import_name, AstNode *cond,
CommentGroup docs, CommentGroup comment) {
AstNode *result = make_ast_node(f, AstNode_ImportSpec);
@@ -1868,8 +1794,6 @@ bool is_semicolon_optional_for_node(AstFile *f, AstNode *s) {
return true;
case AstNode_ProcLit:
return s->ProcLit.body != NULL;
case AstNode_ProcDecl:
return s->ProcDecl.body != NULL;
case AstNode_ValueDecl:
if (s->ValueDecl.is_mutable) {
@@ -1897,9 +1821,6 @@ bool is_semicolon_optional_for_node(AstFile *f, AstNode *s) {
return is_semicolon_optional_for_node(f, s->ForeignBlockDecl.decls[0]);
}
break;
case AstNode_TypeSpec:
return is_semicolon_optional_for_node(f, s->TypeSpec.type);
}
return false;
@@ -2764,45 +2685,6 @@ AstNode *parse_type(AstFile *f) {
return type;
}
AstNode *parse_proc_decl(AstFile *f) {
CommentGroup docs = f->lead_comment;
TokenKind look_ahead = look_ahead_token_kind(f, 1);
if (look_ahead != Token_Ident) {
return ast_expr_stmt(f, parse_expr(f, true));
}
Token token = expect_token(f, Token_proc);
AstNode *body = NULL;
String link_name = {};
bool prev_allow_gen_proc_type = f->allow_gen_proc_type;
f->allow_gen_proc_type = true;
AstNode *name = parse_ident(f);
AstNode *type = parse_proc_type(f, token, &link_name);
u64 tags = type->ProcType.tags;
f->allow_gen_proc_type = prev_allow_gen_proc_type;
if (allow_token(f, Token_Undef)) {
body = NULL;
} else if (f->curr_token.kind == Token_OpenBrace) {
if ((tags & ProcTag_foreign) != 0) {
syntax_error(token, "A procedure tagged as `#foreign` cannot have a body");
}
AstNode *curr_proc = f->curr_proc;
f->curr_proc = type;
body = parse_body(f);
f->curr_proc = curr_proc;
}
AstNode *decl = ast_proc_decl(f, token, name, type, body, tags, link_name, docs);
expect_semicolon(f, decl);
return decl;
}
#define PARSE_SPEC_FUNC(name) AstNode *name(AstFile *f, CommentGroup docs, Token token)
typedef PARSE_SPEC_FUNC(ParseSpecFunc);
@@ -2841,62 +2723,6 @@ AstNode *parse_gen_decl(AstFile *f, Token token, ParseSpecFunc *func) {
return ast_gen_decl(f, token, open, close, specs, docs);
}
PARSE_SPEC_FUNC(parse_value_spec) {
bool is_mutable = token.kind != Token_const;
Array<AstNode *> names = parse_ident_list(f);
AstNode *type = NULL;
Array<AstNode *> values = {};
if (allow_token(f, Token_Colon)) {
type = parse_type(f);
} else if (f->curr_token.kind != Token_Eq &&
f->curr_token.kind != Token_Semicolon) {
syntax_error(f->curr_token, "Expected separator `:` or `=`");
}
if (allow_token(f, Token_Eq)) {
values = parse_rhs_expr_list(f);
if (values.count > names.count) {
syntax_error(f->curr_token, "Too many values on the right hand side of the declaration");
} else if (values.count < names.count && !is_mutable) {
syntax_error(f->curr_token, "All constant declarations must be defined");
} else if (values.count == 0) {
syntax_error(f->curr_token, "Expected an expression for this declaration");
}
}
if (is_mutable) {
if (type == NULL && values.count == 0) {
syntax_error(f->curr_token, "Missing variable type or initialization");
return ast_bad_decl(f, f->curr_token, f->curr_token);
}
} else {
if (type == NULL && values.count == 0 && names.count > 0) {
syntax_error(f->curr_token, "Missing constant value");
return ast_bad_decl(f, f->curr_token, f->curr_token);
}
}
if (values.data == NULL) {
values = make_ast_node_array(f);
}
if (f->expr_level >= 0) {
expect_semicolon(f, NULL);
}
return ast_value_spec(f, names, type, values, docs, f->line_comment);
}
PARSE_SPEC_FUNC(parse_type_spec) {
AstNode *name = parse_ident(f);
AstNode *type = parse_type(f);
expect_semicolon(f, type);
return ast_type_spec(f, name, type, docs, f->line_comment);
}
PARSE_SPEC_FUNC(parse_import_spec) {
AstNode *spec = NULL;
if (token.kind == Token_import) {
@@ -3034,7 +2860,6 @@ void parse_foreign_block_decl(AstFile *f, Array<AstNode *> *decls) {
return;
case AstNode_ValueDecl:
case AstNode_ProcDecl:
array_add(decls, decl);
return;
@@ -3056,15 +2881,6 @@ void parse_foreign_block_decl(AstFile *f, Array<AstNode *> *decls) {
AstNode *parse_decl(AstFile *f) {
ParseSpecFunc *func = NULL;
switch (f->curr_token.kind) {
case Token_var:
case Token_const:
func = parse_value_spec;
break;
case Token_type:
func = parse_type_spec;
break;
case Token_import:
case Token_import_load:
func = parse_import_spec;
@@ -3103,9 +2919,6 @@ AstNode *parse_decl(AstFile *f) {
return ast_foreign_block_decl(f, token, foreign_library, open, close, decls, docs);
} break;
case Token_proc:
return parse_proc_decl(f);
default: {
Token tok = f->curr_token;
fix_advance_to_next_stmt(f);
@@ -3501,7 +3314,7 @@ AstNode *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_flags, Tok
isize total_name_count = 0;
bool allow_ellipsis = allowed_flags&FieldFlag_ellipsis;
bool allow_type_token = f->allow_gen_proc_type && allow_default_parameters;
bool allow_type_token = allow_default_parameters;
while (f->curr_token.kind != follow &&
f->curr_token.kind != Token_Colon &&
@@ -3886,10 +3699,7 @@ AstNode *parse_type_or_ident(AstFile *f) {
case Token_proc: {
Token token = f->curr_token; next_token(f);
bool prev_allow_gen_proc_type = f->allow_gen_proc_type;
f->allow_gen_proc_type = false;
AstNode *pt = parse_proc_type(f, token, NULL);
f->allow_gen_proc_type = prev_allow_gen_proc_type;
if (pt->ProcType.tags != 0) {
syntax_error(token, "A procedure type cannot have tags");
}
@@ -4328,7 +4138,7 @@ AstNode *parse_stmt(AstFile *f) {
// case Token_var:
// case Token_const:
case Token_proc:
// case Token_proc:
// case Token_type:
case Token_import:
case Token_import_load:

View File

@@ -2377,12 +2377,12 @@ void ssa_build_proc(ssaModule *m, ssaProc *p) {
p->module = m;
m->proc = p;
if (p->decl_info->proc_decl == NULL ||
p->decl_info->proc_decl->kind != AstNode_ProcDecl) {
if (p->decl_info->proc_lit == NULL ||
p->decl_info->proc_lit->kind != AstNode_ProcLit) {
return;
}
ast_node(pl, ProcLit, p->decl_info->proc_decl);
ast_node(pl, ProcLit, p->decl_info->proc_lit);
if (pl->body == NULL) {
return;
}
@@ -2495,14 +2495,14 @@ bool ssa_generate(Parser *parser, CheckerInfo *info) {
} break;
case Entity_Procedure: {
ast_node(pd, ProcDecl, decl->proc_decl);
ast_node(pl, ProcLit, decl->proc_lit);
String original_name = name;
AstNode *body = pd->body;
AstNode *body = pl->body;
if (e->Procedure.is_foreign) {
name = e->token.string; // NOTE(bill): Don't use the mangled name
}
if (pd->link_name.len > 0) {
name = pd->link_name;
if (pl->link_name.len > 0) {
name = pl->link_name;
}
if (e == entry_point) {
@@ -2511,7 +2511,7 @@ bool ssa_generate(Parser *parser, CheckerInfo *info) {
}
// ssaValue *p = ssa_make_value_procedure(a, m, e, e->type, decl->type_expr, body, name);
// p->Proc.tags = pd->tags;
// p->Proc.tags = pl->tags;
// ssa_module_add_value(m, e, p);
// HashKey hash_name = hash_string(name);