mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-05 03:01:38 -07:00
Compare commits
91 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e86c990b75 | |||
| 31aacd5bf4 | |||
| 92453369c5 | |||
| 832009f33a | |||
| d3d3bfd455 | |||
| ce3582fd89 | |||
| e3e16f5d05 | |||
| f47f25f942 | |||
| e85458919c | |||
| b59a052e32 | |||
| 12498b2d39 | |||
| 6d93aa429f | |||
| 3f023509a7 | |||
| 563b1e2b28 | |||
| 4603d2525e | |||
| 2af9fb79dc | |||
| 367d307dc4 | |||
| cb59c1cf08 | |||
| 383f5b55ad | |||
| 6dc6b6f8aa | |||
| ac736aa4ec | |||
| 6fe25badf0 | |||
| c29d433e38 | |||
| ff473e8342 | |||
| 659e5359b2 | |||
| d9ce0b9da0 | |||
| 703e1aa2bc | |||
| b1e35b6da3 | |||
| fc1af0a04b | |||
| 4afb3f8fa4 | |||
| 207b252f23 | |||
| 1356dfeec2 | |||
| d025791462 | |||
| b07ee9ec23 | |||
| 915b5cdab7 | |||
| c8f99b360f | |||
| b76f6a8c27 | |||
| cff1b3dff6 | |||
| 883dd0642c | |||
| 40f5dd56f7 | |||
| 70d4ca00df | |||
| a86896e4d3 | |||
| a3883a178c | |||
| ce89a1428e | |||
| 9202bd1b06 | |||
| a48e0c7179 | |||
| 3f1195cd03 | |||
| 311b5cb6e2 | |||
| 6fef74317c | |||
| 0c6775ca14 | |||
| 6748f305db | |||
| 2ecafda1d3 | |||
| 23d32f34e5 | |||
| d714bece47 | |||
| 923b039cf6 | |||
| d0e1efe622 | |||
| 478d63424f | |||
| ac1566762b | |||
| f5eeecaca5 | |||
| 77e219d442 | |||
| 4c10fbdcd4 | |||
| e370337f97 | |||
| 5217eb55b4 | |||
| 062a2c63e1 | |||
| b957365b0d | |||
| 625b98eac4 | |||
| 0f809989e3 | |||
| d4457e9fa4 | |||
| 9634b28b07 | |||
| f567983260 | |||
| ad84314143 | |||
| a6f8c9d6e0 | |||
| de9016b7d0 | |||
| e8b4228833 | |||
| 0d69dfcde6 | |||
| fa89d2775a | |||
| 60b6538a7a | |||
| d9bd770992 | |||
| 517b34f798 | |||
| a16bdb215a | |||
| 88aa74bbb9 | |||
| 8ec9811d7a | |||
| c71b547cde | |||
| 76e724718c | |||
| 0b87313f08 | |||
| 4bb45700a5 | |||
| be8b9bda2f | |||
| ab2ca7cf59 | |||
| b76c8abe73 | |||
| d9c686b53d | |||
| b232b9d5ea |
@@ -30,31 +30,27 @@ Odin is fast, concise, readable, pragmatic and open sourced. It is designed with
|
||||
## Warnings
|
||||
|
||||
* This is still highly in development and the language's design is quite volatile.
|
||||
* Syntax is not fixed.
|
||||
* Syntax is definitely not fixed
|
||||
|
||||
## Roadmap
|
||||
|
||||
Not in any particular order
|
||||
|
||||
* Compile Time Execution (CTE)
|
||||
- More metaprogramming madness
|
||||
- Compiler as a library
|
||||
- AST inspection and modification
|
||||
* CTE-based build system
|
||||
* Replace LLVM backend with my own custom backend
|
||||
* Improve SSA design to accommodate for lowering to a "bytecode"
|
||||
* SSA optimizations
|
||||
* Parametric Polymorphism ("Generics")
|
||||
* Documentation Generator for "Entities"
|
||||
* Multiple Architecture support
|
||||
* Language level atomics and concurrency support
|
||||
* Linking Options
|
||||
* Custom backend to replace LLVM
|
||||
- Improve SSA design to accommodate for lowering to a "bytecode"
|
||||
- SSA optimizations
|
||||
- COFF generation
|
||||
- linker
|
||||
* Type safe "macros"
|
||||
* Documentation generator for "Entities"
|
||||
* Multiple architecture support
|
||||
* Inline assembly
|
||||
* Linking options
|
||||
- Executable
|
||||
- Static/Dynamic Library
|
||||
* Debug Information
|
||||
* Debug information
|
||||
- pdb format too
|
||||
* Command Line Tooling
|
||||
* Compiler Internals:
|
||||
* Command line tooling
|
||||
* Compiler internals:
|
||||
- Big numbers library
|
||||
- Cyclic Type Checking (at the moment will cause compiler to go into an infinite loop)
|
||||
- Multithreading for performance increase
|
||||
|
||||
|
||||
@@ -4,9 +4,8 @@
|
||||
set exe_name=odin.exe
|
||||
|
||||
:: Debug = 0, Release = 1
|
||||
set release_mode=1
|
||||
|
||||
set compiler_flags= -nologo -Oi -TC -W4 -fp:fast -fp:except- -Gm- -MP -FC -GS- -EHsc- -GR-
|
||||
set release_mode=0
|
||||
set compiler_flags= -nologo -Oi -TC -fp:fast -fp:except- -Gm- -MP -FC -GS- -EHsc- -GR-
|
||||
|
||||
if %release_mode% EQU 0 ( rem Debug
|
||||
set compiler_flags=%compiler_flags% -Od -MDd -Z7
|
||||
@@ -16,7 +15,7 @@ if %release_mode% EQU 0 ( rem Debug
|
||||
)
|
||||
|
||||
set compiler_warnings= ^
|
||||
-we4013 -we4706 -we4002 -we4133 ^
|
||||
-W4 -WX ^
|
||||
-wd4100 -wd4101 -wd4127 -wd4189 ^
|
||||
-wd4201 -wd4204 -wd4244 ^
|
||||
-wd4306 ^
|
||||
@@ -40,19 +39,20 @@ set compiler_settings=%compiler_includes% %compiler_flags% %compiler_warnings%
|
||||
set linker_settings=%libs% %linker_flags%
|
||||
|
||||
|
||||
rem set build_dir= "\"
|
||||
rem if not exist %build_dir% mkdir %build_dir%
|
||||
rem pushd %build_dir%
|
||||
del *.pdb > NUL 2> NUL
|
||||
del *.ilk > NUL 2> NUL
|
||||
del *.pdb > NUL 2> NUL
|
||||
del *.ilk > NUL 2> NUL
|
||||
|
||||
cl %compiler_settings% "src\main.c" ^
|
||||
/link %linker_settings% -OUT:%exe_name% ^
|
||||
cl %compiler_settings% "src\main.c" ^
|
||||
/link %linker_settings% -OUT:%exe_name% ^
|
||||
&& odin run code/demo.odin
|
||||
rem odin run code/demo.odin
|
||||
rem && odin build_dll code/example.odin ^
|
||||
rem odin run code/punity.odin
|
||||
|
||||
|
||||
:do_not_compile_exe
|
||||
rem pushd src\asm
|
||||
rem nasm hellope.asm -fwin64 -o hellope.obj ^
|
||||
rem && cl /nologo hellope.obj /link kernel32.lib /entry:main ^
|
||||
rem && hellope.exe
|
||||
rem popd
|
||||
|
||||
:end_of_build
|
||||
|
||||
|
||||
+280
-29
@@ -1,32 +1,283 @@
|
||||
#import "fmt.odin"
|
||||
#import "utf8.odin"
|
||||
#import "fmt.odin";
|
||||
#import "utf8.odin";
|
||||
// #import "atomic.odin";
|
||||
// #import "hash.odin";
|
||||
// #import "math.odin";
|
||||
// #import "mem.odin";
|
||||
// #import "opengl.odin";
|
||||
// #import "os.odin";
|
||||
// #import "sync.odin";
|
||||
// #import win32 "sys/windows.odin";
|
||||
|
||||
main :: proc() {
|
||||
MAX :: 64
|
||||
buf: [MAX]rune
|
||||
backing: [MAX]byte
|
||||
offset: int
|
||||
|
||||
msg := "Hello"
|
||||
count := utf8.rune_count(msg)
|
||||
assert(count <= MAX)
|
||||
runes := buf[:count]
|
||||
|
||||
offset = 0
|
||||
for i := 0; i < count; i++ {
|
||||
s := msg[offset:]
|
||||
r, len := utf8.decode_rune(s)
|
||||
runes[count-i-1] = r
|
||||
offset += len
|
||||
}
|
||||
|
||||
offset = 0
|
||||
for i := 0; i < count; i++ {
|
||||
data, len := utf8.encode_rune(runes[i])
|
||||
copy(backing[offset:], data[:len])
|
||||
offset += len
|
||||
}
|
||||
|
||||
reverse := backing[:offset] as string
|
||||
fmt.println(reverse) // olleH
|
||||
syntax();
|
||||
}
|
||||
|
||||
syntax :: proc() {
|
||||
// Cyclic type checking
|
||||
// Uncomment to see the error
|
||||
// A :: struct {b: B};
|
||||
// B :: struct {a: A};
|
||||
|
||||
x: int;
|
||||
y := cast(f32)x;
|
||||
z := transmute(u32)y;
|
||||
// down_cast, union_cast as similar too
|
||||
|
||||
|
||||
|
||||
// Basic directives
|
||||
fmt.printf("Basic directives = %s(%d): %s\n", #file, #line, #procedure);
|
||||
// NOTE: new and improved `printf`
|
||||
// TODO: It does need accurate float printing
|
||||
|
||||
|
||||
|
||||
// record fields use the same syntax a procedure signatures
|
||||
Thing1 :: struct {
|
||||
x: f32,
|
||||
y: int,
|
||||
z: ^[]int,
|
||||
};
|
||||
Thing2 :: struct {x: f32, y: int, z: ^[]int};
|
||||
|
||||
// Slice interals are now just a `ptr+count`
|
||||
slice: []int; compile_assert(size_of_val(slice) == 2*size_of(int));
|
||||
|
||||
// Helper type - Help the reader understand what it is quicker
|
||||
My_Int :: type int;
|
||||
My_Proc :: type proc(int) -> f32;
|
||||
|
||||
|
||||
// All declarations with : are either variable or constant
|
||||
// To make these declarations syntactically consistent
|
||||
v_variable := 123;
|
||||
c_constant :: 123;
|
||||
c_type1 :: int;
|
||||
c_type2 :: []int;
|
||||
c_proc :: proc() { /* code here */ };
|
||||
|
||||
|
||||
x += 1;
|
||||
x -= 1;
|
||||
// ++ and -- have been removed
|
||||
// x++;
|
||||
// x--;
|
||||
// Question: Should they be added again?
|
||||
// They were removed as they are redundant and statements, not expressions
|
||||
// like in C/C++
|
||||
|
||||
|
||||
// You can now build files as a `.dll`
|
||||
// `odin build_dll demo.odin`
|
||||
|
||||
|
||||
// New vector syntax
|
||||
v: [vector 3]f32;
|
||||
v[0] = 123;
|
||||
v.x = 123; // valid for all vectors with count 1 to 4
|
||||
|
||||
|
||||
// Next part
|
||||
prefixes();
|
||||
}
|
||||
|
||||
|
||||
Prefix_Type :: struct {x: int, y: f32, z: rawptr};
|
||||
|
||||
thread_local my_tls: Prefix_Type;
|
||||
|
||||
prefixes :: proc() {
|
||||
using var: Prefix_Type;
|
||||
immutable const := Prefix_Type{1, 2, nil};
|
||||
var.x = 123;
|
||||
x = 123;
|
||||
// const.x = 123; // const is immutable
|
||||
|
||||
|
||||
|
||||
foo :: proc(using immutable pt: Prefix_Type, immutable int_ptr: ^int) {
|
||||
// int_ptr = nil; // Not valid
|
||||
int_ptr^ = 123; // Is valid
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Same as C99's `restrict`
|
||||
bar :: proc(no_alias a, b: ^int) {
|
||||
// Assumes a never equals b so it can perform optimizations with that fact
|
||||
}
|
||||
|
||||
|
||||
when_statements();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
when_statements :: proc() {
|
||||
X :: 123 + 12;
|
||||
Y :: X/5;
|
||||
COND :: Y > 0;
|
||||
|
||||
when COND {
|
||||
fmt.println("Y > 0");
|
||||
} else {
|
||||
fmt.println("Y <= 0");
|
||||
}
|
||||
|
||||
|
||||
when false {
|
||||
this_code_does_not_exist(123, 321);
|
||||
but_its_syntax_is_valid();
|
||||
x :: ^^^^int;
|
||||
}
|
||||
|
||||
foreign_procedures();
|
||||
}
|
||||
|
||||
#foreign_system_library win32_user "user32.lib" when ODIN_OS == "windows";
|
||||
// NOTE: This is done on purpose for two reasons:
|
||||
// * Makes it clear where the platform specific stuff is
|
||||
// * Removes the need to solve the travelling salesman problem when importing files :P
|
||||
|
||||
foreign_procedures :: proc() {
|
||||
ShowWindow :: proc(hwnd: rawptr, cmd_show: i32) -> i32 #foreign win32_user;
|
||||
show_window :: proc(hwnd: rawptr, cmd_show: i32) -> i32 #foreign win32_user "ShowWindow";
|
||||
// NOTE: If that library doesn't get used, it doesn't get linked with
|
||||
// NOTE: There is not link checking yet to see if that procedure does come from that library
|
||||
|
||||
// See sys/windows.odin for more examples
|
||||
|
||||
special_expressions();
|
||||
}
|
||||
|
||||
special_expressions :: proc() {
|
||||
// Block expression
|
||||
x := {
|
||||
a: f32 = 123;
|
||||
b := a-123;
|
||||
c := b/a;
|
||||
give c;
|
||||
}; // semicolon is required as it's an expression
|
||||
|
||||
y := if x < 50 {
|
||||
give x;
|
||||
} else {
|
||||
// TODO: Type cohesion is not yet finished
|
||||
give 123;
|
||||
}; // semicolon is required as it's an expression
|
||||
|
||||
|
||||
// This is allows for inline blocks of code and will be a useful feature to have when
|
||||
// macros will be implemented into the language
|
||||
|
||||
loops();
|
||||
}
|
||||
|
||||
loops :: proc() {
|
||||
// The C-style for loop
|
||||
for i := 0; i < 123; i += 1 {
|
||||
break;
|
||||
}
|
||||
for i := 0; i < 123; {
|
||||
break;
|
||||
}
|
||||
for false {
|
||||
break;
|
||||
}
|
||||
for {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
for i in 0..<123 { // 123 exclusive
|
||||
}
|
||||
|
||||
for i in 0...122 { // 122 inclusive
|
||||
}
|
||||
|
||||
for val, idx in 12..<16 {
|
||||
fmt.println(val, idx);
|
||||
}
|
||||
|
||||
primes := [...]int{2, 3, 5, 7, 11, 13, 17, 19};
|
||||
|
||||
for p in primes {
|
||||
fmt.println(p);
|
||||
}
|
||||
|
||||
// Pointers to arrays, slices, or strings are allowed
|
||||
for _ in ^primes {
|
||||
// ignore the value and just iterate across it
|
||||
}
|
||||
|
||||
|
||||
|
||||
name := "你好,世界";
|
||||
fmt.println(name);
|
||||
for r in name {
|
||||
compile_assert(type_of_val(r) == rune);
|
||||
fmt.printf("%r\n", r);
|
||||
}
|
||||
|
||||
when false {
|
||||
for i, size := 0; i < name.count; i += size {
|
||||
r: rune;
|
||||
r, size = utf8.decode_rune(name[i:]);
|
||||
fmt.printf("%r\n", r);
|
||||
}
|
||||
}
|
||||
|
||||
procedure_overloading();
|
||||
}
|
||||
|
||||
|
||||
procedure_overloading :: proc() {
|
||||
THINGF :: 14451.1;
|
||||
THINGI :: 14451;
|
||||
|
||||
foo :: proc() {
|
||||
fmt.printf("Zero args\n");
|
||||
}
|
||||
foo :: proc(i: int) {
|
||||
fmt.printf("int arg, i=%d\n", i);
|
||||
}
|
||||
foo :: proc(f: f64) {
|
||||
i := cast(int)f;
|
||||
fmt.printf("f64 arg, f=%d\n", i);
|
||||
}
|
||||
|
||||
foo();
|
||||
foo(THINGF);
|
||||
// foo(THINGI); // 14451 is just a number so it could go to either procedures
|
||||
foo(cast(int)THINGI);
|
||||
|
||||
|
||||
|
||||
|
||||
foo :: proc(x: ^i32) -> (int, int) {
|
||||
fmt.println("^int");
|
||||
return 123, cast(int)(x^);
|
||||
}
|
||||
foo :: proc(x: rawptr) {
|
||||
fmt.println("rawptr");
|
||||
}
|
||||
|
||||
|
||||
a: i32 = 123;
|
||||
b: f32;
|
||||
c: rawptr;
|
||||
fmt.println(foo(^a));
|
||||
foo(^b);
|
||||
foo(c);
|
||||
// foo(nil); // nil could go to numerous types thus the ambiguity
|
||||
|
||||
f: proc();
|
||||
f = foo; // The correct `foo` to chosen
|
||||
f();
|
||||
|
||||
|
||||
// See math.odin and atomic.odin for more examples
|
||||
}
|
||||
|
||||
+94
-94
@@ -1,60 +1,60 @@
|
||||
#import "win32.odin"
|
||||
#import "fmt.odin"
|
||||
#import "math.odin"
|
||||
#import "os.odin"
|
||||
#import "opengl.odin" as gl
|
||||
#import "win32.odin" when ODIN_OS == "windows";
|
||||
#import "fmt.odin";
|
||||
#import "math.odin";
|
||||
#import "os.odin";
|
||||
#import gl "opengl.odin";
|
||||
|
||||
TWO_HEARTS :: '💕'
|
||||
TWO_HEARTS :: '💕';
|
||||
|
||||
win32_perf_count_freq := win32.GetQueryPerformanceFrequency()
|
||||
win32_perf_count_freq := win32.GetQueryPerformanceFrequency();
|
||||
time_now :: proc() -> f64 {
|
||||
assert(win32_perf_count_freq != 0)
|
||||
assert(win32_perf_count_freq != 0);
|
||||
|
||||
counter: i64
|
||||
win32.QueryPerformanceCounter(^counter)
|
||||
result := counter as f64 / win32_perf_count_freq as f64
|
||||
return result
|
||||
counter: i64;
|
||||
win32.QueryPerformanceCounter(^counter);
|
||||
result := counter as f64 / win32_perf_count_freq as f64;
|
||||
return result;
|
||||
}
|
||||
win32_print_last_error :: proc() {
|
||||
err_code := win32.GetLastError() as int
|
||||
err_code := win32.GetLastError() as int;
|
||||
if err_code != 0 {
|
||||
fmt.println("GetLastError: %", err_code)
|
||||
fmt.println("GetLastError: %", err_code);
|
||||
}
|
||||
}
|
||||
|
||||
// Yuk!
|
||||
to_c_string :: proc(s: string) -> []u8 {
|
||||
c_str := new_slice(u8, s.count+1)
|
||||
copy(c_str, s as []byte)
|
||||
c_str[s.count] = 0
|
||||
return c_str
|
||||
c_str := new_slice(u8, s.count+1);
|
||||
copy(c_str, s as []byte);
|
||||
c_str[s.count] = 0;
|
||||
return c_str;
|
||||
}
|
||||
|
||||
|
||||
Window :: struct {
|
||||
width, height: int
|
||||
wc: win32.WNDCLASSEXA
|
||||
dc: win32.HDC
|
||||
hwnd: win32.HWND
|
||||
opengl_context, rc: win32.HGLRC
|
||||
c_title: []u8
|
||||
width, height: int;
|
||||
wc: win32.WNDCLASSEXA;
|
||||
dc: win32.HDC;
|
||||
hwnd: win32.HWND;
|
||||
opengl_context, rc: win32.HGLRC;
|
||||
c_title: []u8;
|
||||
}
|
||||
|
||||
make_window :: proc(title: string, msg, height: int, window_proc: win32.WNDPROC) -> (Window, bool) {
|
||||
using win32
|
||||
using win32;
|
||||
|
||||
w: Window
|
||||
w.width, w.height = msg, height
|
||||
w: Window;
|
||||
w.width, w.height = msg, height;
|
||||
|
||||
class_name := "Win32-Odin-Window\x00"
|
||||
c_class_name := class_name.data
|
||||
class_name := "Win32-Odin-Window\x00";
|
||||
c_class_name := class_name.data;
|
||||
if title[title.count-1] != 0 {
|
||||
w.c_title = to_c_string(title)
|
||||
w.c_title = to_c_string(title);
|
||||
} else {
|
||||
w.c_title = title as []u8
|
||||
w.c_title = title as []u8;
|
||||
}
|
||||
|
||||
instance := GetModuleHandleA(nil)
|
||||
instance := GetModuleHandleA(nil);
|
||||
|
||||
w.wc = WNDCLASSEXA{
|
||||
size = size_of(WNDCLASSEXA) as u32,
|
||||
@@ -65,8 +65,8 @@ make_window :: proc(title: string, msg, height: int, window_proc: win32.WNDPROC)
|
||||
};
|
||||
|
||||
if RegisterClassExA(^w.wc) == 0 {
|
||||
win32_print_last_error()
|
||||
return w, false
|
||||
win32_print_last_error();
|
||||
return w, false;
|
||||
}
|
||||
|
||||
w.hwnd = CreateWindowExA(0,
|
||||
@@ -74,14 +74,14 @@ make_window :: proc(title: string, msg, height: int, window_proc: win32.WNDPROC)
|
||||
WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
|
||||
CW_USEDEFAULT, CW_USEDEFAULT,
|
||||
w.width as i32, w.height as i32,
|
||||
nil, nil, instance, nil)
|
||||
nil, nil, instance, nil);
|
||||
|
||||
if w.hwnd == nil {
|
||||
win32_print_last_error()
|
||||
return w, false
|
||||
win32_print_last_error();
|
||||
return w, false;
|
||||
}
|
||||
|
||||
w.dc = GetDC(w.hwnd)
|
||||
w.dc = GetDC(w.hwnd);
|
||||
|
||||
{
|
||||
pfd := PIXELFORMATDESCRIPTOR{
|
||||
@@ -94,122 +94,122 @@ make_window :: proc(title: string, msg, height: int, window_proc: win32.WNDPROC)
|
||||
depth_bits = 24,
|
||||
stencil_bits = 8,
|
||||
layer_type = PFD_MAIN_PLANE,
|
||||
}
|
||||
};
|
||||
|
||||
SetPixelFormat(w.dc, ChoosePixelFormat(w.dc, ^pfd), nil)
|
||||
w.opengl_context = wglCreateContext(w.dc)
|
||||
wglMakeCurrent(w.dc, w.opengl_context)
|
||||
SetPixelFormat(w.dc, ChoosePixelFormat(w.dc, ^pfd), nil);
|
||||
w.opengl_context = wglCreateContext(w.dc);
|
||||
wglMakeCurrent(w.dc, w.opengl_context);
|
||||
|
||||
attribs := [8]i32{
|
||||
WGL_CONTEXT_MAJOR_VERSION_ARB, 2,
|
||||
WGL_CONTEXT_MINOR_VERSION_ARB, 1,
|
||||
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
|
||||
0, // NOTE(bill): tells the proc that this is the end of attribs
|
||||
}
|
||||
};
|
||||
|
||||
wglCreateContextAttribsARB := wglGetProcAddress(("wglCreateContextAttribsARB\x00" as string).data) as wglCreateContextAttribsARBType
|
||||
w.rc = wglCreateContextAttribsARB(w.dc, 0, ^attribs[0])
|
||||
wglMakeCurrent(w.dc, w.rc)
|
||||
SwapBuffers(w.dc)
|
||||
wglCreateContextAttribsARB := wglGetProcAddress(("wglCreateContextAttribsARB\x00" as string).data) as wglCreateContextAttribsARBType;
|
||||
w.rc = wglCreateContextAttribsARB(w.dc, 0, ^attribs[0]);
|
||||
wglMakeCurrent(w.dc, w.rc);
|
||||
SwapBuffers(w.dc);
|
||||
}
|
||||
|
||||
return w, true
|
||||
return w, true;
|
||||
}
|
||||
|
||||
destroy_window :: proc(w: ^Window) {
|
||||
free(w.c_title.data)
|
||||
free(w.c_title.data);
|
||||
}
|
||||
|
||||
display_window :: proc(w: ^Window) {
|
||||
win32.SwapBuffers(w.dc)
|
||||
win32.SwapBuffers(w.dc);
|
||||
}
|
||||
|
||||
|
||||
run :: proc() {
|
||||
using win32
|
||||
using math
|
||||
using win32;
|
||||
using math;
|
||||
|
||||
win32_proc :: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT #no_inline {
|
||||
if msg == WM_DESTROY || msg == WM_CLOSE || msg == WM_QUIT {
|
||||
os.exit(0)
|
||||
return 0
|
||||
os.exit(0);
|
||||
return 0;
|
||||
}
|
||||
return DefWindowProcA(hwnd, msg, wparam, lparam)
|
||||
return DefWindowProcA(hwnd, msg, wparam, lparam);
|
||||
}
|
||||
|
||||
window, window_success := make_window("Odin Language Demo", 854, 480, win32_proc)
|
||||
window, window_success := make_window("Odin Language Demo", 854, 480, win32_proc);
|
||||
if !window_success {
|
||||
return
|
||||
return;
|
||||
}
|
||||
defer destroy_window(^window)
|
||||
defer destroy_window(^window);
|
||||
|
||||
gl.init()
|
||||
gl.init();
|
||||
|
||||
|
||||
prev_time := time_now()
|
||||
running := true
|
||||
prev_time := time_now();
|
||||
running := true;
|
||||
|
||||
pos := Vec2{100, 100}
|
||||
pos := Vec2{100, 100};
|
||||
|
||||
for running {
|
||||
curr_time := time_now()
|
||||
dt := (curr_time - prev_time) as f32
|
||||
prev_time = curr_time
|
||||
curr_time := time_now();
|
||||
dt := (curr_time - prev_time) as f32;
|
||||
prev_time = curr_time;
|
||||
|
||||
msg: MSG
|
||||
msg: MSG;
|
||||
for PeekMessageA(^msg, nil, 0, 0, PM_REMOVE) > 0 {
|
||||
if msg.message == WM_QUIT {
|
||||
running = false
|
||||
running = false;
|
||||
}
|
||||
TranslateMessage(^msg)
|
||||
DispatchMessageA(^msg)
|
||||
TranslateMessage(^msg);
|
||||
DispatchMessageA(^msg);
|
||||
}
|
||||
|
||||
if is_key_down(Key_Code.ESCAPE) {
|
||||
running = false
|
||||
running = false;
|
||||
}
|
||||
|
||||
{
|
||||
SPEED :: 500
|
||||
v: Vec2
|
||||
SPEED :: 500;
|
||||
v: Vec2;
|
||||
|
||||
if is_key_down(Key_Code.RIGHT) { v[0] += 1 }
|
||||
if is_key_down(Key_Code.LEFT) { v[0] -= 1 }
|
||||
if is_key_down(Key_Code.UP) { v[1] += 1 }
|
||||
if is_key_down(Key_Code.DOWN) { v[1] -= 1 }
|
||||
if is_key_down(Key_Code.RIGHT) { v[0] += 1; }
|
||||
if is_key_down(Key_Code.LEFT) { v[0] -= 1; }
|
||||
if is_key_down(Key_Code.UP) { v[1] += 1; }
|
||||
if is_key_down(Key_Code.DOWN) { v[1] -= 1; }
|
||||
|
||||
v = vec2_norm0(v)
|
||||
v = vec2_norm0(v);
|
||||
|
||||
pos += v * Vec2{SPEED * dt}
|
||||
pos += v * Vec2{SPEED * dt};
|
||||
}
|
||||
|
||||
|
||||
gl.ClearColor(0.5, 0.7, 1.0, 1.0)
|
||||
gl.Clear(gl.COLOR_BUFFER_BIT)
|
||||
gl.ClearColor(0.5, 0.7, 1.0, 1.0);
|
||||
gl.Clear(gl.COLOR_BUFFER_BIT);
|
||||
|
||||
gl.LoadIdentity()
|
||||
gl.LoadIdentity();
|
||||
gl.Ortho(0, window.width as f64,
|
||||
0, window.height as f64, 0, 1)
|
||||
0, window.height as f64, 0, 1);
|
||||
|
||||
draw_rect :: proc(x, y, w, h: f32) {
|
||||
gl.Begin(gl.TRIANGLES)
|
||||
defer gl.End()
|
||||
gl.Begin(gl.TRIANGLES);
|
||||
defer gl.End();
|
||||
|
||||
gl.Color3f(1, 0, 0); gl.Vertex3f(x, y, 0)
|
||||
gl.Color3f(0, 1, 0); gl.Vertex3f(x+w, y, 0)
|
||||
gl.Color3f(0, 0, 1); gl.Vertex3f(x+w, y+h, 0)
|
||||
gl.Color3f(1, 0, 0); gl.Vertex3f(x, y, 0);
|
||||
gl.Color3f(0, 1, 0); gl.Vertex3f(x+w, y, 0);
|
||||
gl.Color3f(0, 0, 1); gl.Vertex3f(x+w, y+h, 0);
|
||||
|
||||
gl.Color3f(0, 0, 1); gl.Vertex3f(x+w, y+h, 0)
|
||||
gl.Color3f(1, 1, 0); gl.Vertex3f(x, y+h, 0)
|
||||
gl.Color3f(1, 0, 0); gl.Vertex3f(x, y, 0)
|
||||
gl.Color3f(0, 0, 1); gl.Vertex3f(x+w, y+h, 0);
|
||||
gl.Color3f(1, 1, 0); gl.Vertex3f(x, y+h, 0);
|
||||
gl.Color3f(1, 0, 0); gl.Vertex3f(x, y, 0);
|
||||
}
|
||||
|
||||
draw_rect(pos.x, pos.y, 50, 50)
|
||||
draw_rect(pos.x, pos.y, 50, 50);
|
||||
|
||||
display_window(^window)
|
||||
ms_to_sleep := (16 - 1000*dt) as i32
|
||||
display_window(^window);
|
||||
ms_to_sleep := (16 - 1000*dt) as i32;
|
||||
if ms_to_sleep > 0 {
|
||||
win32.Sleep(ms_to_sleep)
|
||||
win32.Sleep(ms_to_sleep);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
-6
@@ -1,6 +1,6 @@
|
||||
#import "fmt.odin" as fmt
|
||||
#import "fmt.odin"
|
||||
|
||||
#foreign_system_library "Ws2_32"
|
||||
#foreign_system_library "Ws2_32" when ODIN_OS == "windows"
|
||||
|
||||
|
||||
SOCKET :: type uint
|
||||
@@ -95,7 +95,7 @@ WSAGetLastError :: proc() -> i32
|
||||
|
||||
to_c_string :: proc(s: string) -> ^byte {
|
||||
c_str := new_slice(byte, s.count+1)
|
||||
assert(c_str.data != null)
|
||||
assert(c_str.data != nil)
|
||||
copy(c_str, s as []byte)
|
||||
c_str[s.count] = 0
|
||||
return c_str.data
|
||||
@@ -103,7 +103,7 @@ to_c_string :: proc(s: string) -> ^byte {
|
||||
|
||||
run :: proc() {
|
||||
wsa: WSADATA
|
||||
res: ^addrinfo = null
|
||||
res: ^addrinfo = nil
|
||||
hints: addrinfo
|
||||
s, client: SOCKET
|
||||
|
||||
@@ -118,7 +118,7 @@ run :: proc() {
|
||||
hints.protocol = IPPROTO_TCP
|
||||
hints.flags = AI_PASSIVE
|
||||
|
||||
if getaddrinfo(null, to_c_string("8080"), ^hints, ^res) != 0 {
|
||||
if getaddrinfo(nil, to_c_string("8080"), ^hints, ^res) != 0 {
|
||||
fmt.println("getaddrinfo failed: ", WSAGetLastError())
|
||||
return
|
||||
}
|
||||
@@ -134,7 +134,7 @@ run :: proc() {
|
||||
bind(s, res.addr, res.addrlen as i32)
|
||||
listen(s, SOMAXCONN)
|
||||
|
||||
client = accept(s, null, null)
|
||||
client = accept(s, nil, 0)
|
||||
if client == INVALID_SOCKET {
|
||||
fmt.println("socket failed: ", WSAGetLastError())
|
||||
return
|
||||
|
||||
+87
-87
@@ -1,9 +1,9 @@
|
||||
#import "fmt.odin"
|
||||
#import "os.odin"
|
||||
#import "mem.odin"
|
||||
// #import "http_test.odin" as ht
|
||||
// #import "game.odin" as game
|
||||
// #import "punity.odin" as pn
|
||||
#import "fmt.odin";
|
||||
#import "os.odin";
|
||||
#import "mem.odin";
|
||||
// #import "http_test.odin" as ht;
|
||||
// #import "game.odin" as game;
|
||||
// #import "punity.odin" as pn;
|
||||
|
||||
main :: proc() {
|
||||
// struct_padding()
|
||||
@@ -26,58 +26,58 @@ main :: proc() {
|
||||
struct_padding :: proc() {
|
||||
{
|
||||
A :: struct {
|
||||
a: u8
|
||||
b: u32
|
||||
c: u16
|
||||
a: u8,
|
||||
b: u32,
|
||||
c: u16,
|
||||
}
|
||||
|
||||
B :: struct {
|
||||
a: [7]u8
|
||||
b: [3]u16
|
||||
c: u8
|
||||
d: u16
|
||||
a: [7]u8,
|
||||
b: [3]u16,
|
||||
c: u8,
|
||||
d: u16,
|
||||
}
|
||||
|
||||
fmt.println("size_of(A):", size_of(A))
|
||||
fmt.println("size_of(B):", size_of(B))
|
||||
fmt.println("size_of(A):", size_of(A));
|
||||
fmt.println("size_of(B):", size_of(B));
|
||||
|
||||
// n.b. http://cbloomrants.blogspot.co.uk/2012/07/07-23-12-structs-are-not-what-you-want.html
|
||||
}
|
||||
{
|
||||
A :: struct #ordered {
|
||||
a: u8
|
||||
b: u32
|
||||
c: u16
|
||||
a: u8,
|
||||
b: u32,
|
||||
c: u16,
|
||||
}
|
||||
|
||||
B :: struct #ordered {
|
||||
a: [7]u8
|
||||
b: [3]u16
|
||||
c: u8
|
||||
d: u16
|
||||
a: [7]u8,
|
||||
b: [3]u16,
|
||||
c: u8,
|
||||
d: u16,
|
||||
}
|
||||
|
||||
fmt.println("size_of(A):", size_of(A))
|
||||
fmt.println("size_of(B):", size_of(B))
|
||||
fmt.println("size_of(A):", size_of(A));
|
||||
fmt.println("size_of(B):", size_of(B));
|
||||
|
||||
// C-style structure layout
|
||||
}
|
||||
{
|
||||
A :: struct #packed {
|
||||
a: u8
|
||||
b: u32
|
||||
c: u16
|
||||
a: u8,
|
||||
b: u32,
|
||||
c: u16,
|
||||
}
|
||||
|
||||
B :: struct #packed {
|
||||
a: [7]u8
|
||||
b: [3]u16
|
||||
c: u8
|
||||
d: u16
|
||||
a: [7]u8,
|
||||
b: [3]u16,
|
||||
c: u8,
|
||||
d: u16,
|
||||
}
|
||||
|
||||
fmt.println("size_of(A):", size_of(A))
|
||||
fmt.println("size_of(B):", size_of(B))
|
||||
fmt.println("size_of(A):", size_of(A));
|
||||
fmt.println("size_of(B):", size_of(B));
|
||||
|
||||
// Useful for explicit layout
|
||||
}
|
||||
@@ -119,7 +119,7 @@ struct_padding :: proc() {
|
||||
}
|
||||
|
||||
bounds_checking :: proc() {
|
||||
x: [4]int
|
||||
x: [4]int;
|
||||
// x[-1] = 0; // Compile Time
|
||||
// x[4] = 0; // Compile Time
|
||||
|
||||
@@ -132,9 +132,9 @@ bounds_checking :: proc() {
|
||||
// Works for arrays, strings, slices, and related procedures & operations
|
||||
|
||||
{
|
||||
base: [10]int
|
||||
s := base[2:6]
|
||||
a, b := -1, 6
|
||||
base: [10]int;
|
||||
s := base[2:6];
|
||||
a, b := -1, 6;
|
||||
|
||||
#no_bounds_check {
|
||||
s[a] = 0;
|
||||
@@ -154,69 +154,69 @@ bounds_checking :: proc() {
|
||||
|
||||
type_introspection :: proc() {
|
||||
{
|
||||
info: ^Type_Info
|
||||
x: int
|
||||
info: ^Type_Info;
|
||||
x: int;
|
||||
|
||||
info = type_info(int) // by type
|
||||
info = type_info_of_val(x) // by value
|
||||
info = type_info(int); // by type
|
||||
info = type_info_of_val(x); // by value
|
||||
// See: runtime.odin
|
||||
|
||||
match type i : info {
|
||||
match type i in info {
|
||||
case Type_Info.Integer:
|
||||
fmt.println("integer!")
|
||||
fmt.println("integer!");
|
||||
case Type_Info.Float:
|
||||
fmt.println("float!")
|
||||
fmt.println("float!");
|
||||
default:
|
||||
fmt.println("potato!")
|
||||
fmt.println("potato!");
|
||||
}
|
||||
|
||||
// Unsafe cast
|
||||
integer_info := info as ^Type_Info.Integer
|
||||
integer_info := cast(^Type_Info.Integer)info;
|
||||
}
|
||||
|
||||
{
|
||||
Vector2 :: struct { x, y: f32 }
|
||||
Vector3 :: struct { x, y, z: f32 }
|
||||
|
||||
v1: Vector2
|
||||
v2: Vector3
|
||||
v3: Vector3
|
||||
v1: Vector2;
|
||||
v2: Vector3;
|
||||
v3: Vector3;
|
||||
|
||||
t1 := type_info_of_val(v1)
|
||||
t2 := type_info_of_val(v2)
|
||||
t3 := type_info_of_val(v3)
|
||||
t1 := type_info_of_val(v1);
|
||||
t2 := type_info_of_val(v2);
|
||||
t3 := type_info_of_val(v3);
|
||||
|
||||
fmt.println()
|
||||
fmt.print("Type of v1 is:\n\t", t1)
|
||||
fmt.println();
|
||||
fmt.print("Type of v1 is:\n\t", t1);
|
||||
|
||||
fmt.println()
|
||||
fmt.print("Type of v2 is:\n\t", t2)
|
||||
fmt.println();
|
||||
fmt.print("Type of v2 is:\n\t", t2);
|
||||
|
||||
fmt.println("\n")
|
||||
fmt.println("t1 == t2:", t1 == t2)
|
||||
fmt.println("t2 == t3:", t2 == t3)
|
||||
fmt.println("\n");
|
||||
fmt.println("t1 == t2:", t1 == t2);
|
||||
fmt.println("t2 == t3:", t2 == t3);
|
||||
}
|
||||
}
|
||||
|
||||
any_type :: proc() {
|
||||
a: any
|
||||
a: any;
|
||||
|
||||
x: int = 123
|
||||
y: f64 = 6.28
|
||||
z: string = "Yo-Yo Ma"
|
||||
x: int = 123;
|
||||
y: f64 = 6.28;
|
||||
z: string = "Yo-Yo Ma";
|
||||
// All types can be implicit cast to `any`
|
||||
a = x
|
||||
a = y
|
||||
a = z
|
||||
a = a // This the "identity" type, it doesn't get converted
|
||||
a = x;
|
||||
a = y;
|
||||
a = z;
|
||||
a = a; // This the "identity" type, it doesn't get converted
|
||||
|
||||
a = 123 // Literals are copied onto the stack first
|
||||
a = 123; // Literals are copied onto the stack first
|
||||
|
||||
// any has two members
|
||||
// data - rawptr to the data
|
||||
// type_info - pointer to the type info
|
||||
|
||||
fmt.println(x, y, z)
|
||||
fmt.println(x, y, z);
|
||||
// See: fmt.odin
|
||||
// For variadic any procedures in action
|
||||
}
|
||||
@@ -232,15 +232,15 @@ crazy_introspection :: proc() {
|
||||
TOMATO,
|
||||
}
|
||||
|
||||
s: string
|
||||
s = enum_to_string(Fruit.PEACH)
|
||||
fmt.println(s)
|
||||
s: string;
|
||||
// s = enum_to_string(Fruit.PEACH);
|
||||
fmt.println(s);
|
||||
|
||||
f := Fruit.GRAPE
|
||||
s = enum_to_string(f)
|
||||
fmt.println(s)
|
||||
f := Fruit.GRAPE;
|
||||
// s = enum_to_string(f);
|
||||
fmt.println(s);
|
||||
|
||||
fmt.println(f)
|
||||
fmt.println(f);
|
||||
// See: runtime.odin
|
||||
}
|
||||
|
||||
@@ -259,15 +259,15 @@ crazy_introspection :: proc() {
|
||||
TOMATO,
|
||||
}
|
||||
|
||||
fruit_ti := type_info(Fruit)
|
||||
name := (fruit_ti as ^Type_Info.Named).name // Unsafe casts
|
||||
info := type_info_base(fruit_ti) as ^Type_Info.Enum // Unsafe casts
|
||||
fruit_ti := type_info(Fruit);
|
||||
name := (cast(^Type_Info.Named)fruit_ti).name; // Unsafe casts
|
||||
info := cast(^Type_Info.Enum)type_info_base(fruit_ti); // Unsafe casts
|
||||
|
||||
fmt.printf("% :: enum % {\n", name, info.base);
|
||||
for i := 0; i < info.values.count; i++ {
|
||||
fmt.printf("\t%\t= %,\n", info.names[i], info.values[i])
|
||||
for i := 0; i < info.values.count; i += 1 {
|
||||
fmt.printf("\t%\t= %,\n", info.names[i], info.values[i]);
|
||||
}
|
||||
fmt.printf("}\n")
|
||||
fmt.printf("}\n");
|
||||
|
||||
// NOTE(bill): look at that type-safe printf!
|
||||
}
|
||||
@@ -275,10 +275,10 @@ crazy_introspection :: proc() {
|
||||
{
|
||||
Vector3 :: struct {x, y, z: f32}
|
||||
|
||||
a := Vector3{x = 1, y = 4, z = 9}
|
||||
fmt.println(a)
|
||||
b := Vector3{x = 9, y = 3, z = 1}
|
||||
fmt.println(b)
|
||||
a := Vector3{x = 1, y = 4, z = 9};
|
||||
fmt.println(a);
|
||||
b := Vector3{x = 9, y = 3, z = 1};
|
||||
fmt.println(b);
|
||||
|
||||
// NOTE(bill): See fmt.odin
|
||||
}
|
||||
@@ -301,7 +301,7 @@ namespaces_and_files :: proc() {
|
||||
#import "file.odin" as _
|
||||
|
||||
// Exporting import
|
||||
#load "file.odin"
|
||||
#include "file.odin"
|
||||
*/
|
||||
|
||||
// Talk about scope rules and diagram
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Demo 002
|
||||
#load "basic.odin"
|
||||
#load "math.odin"
|
||||
// #load "game.odin"
|
||||
#include "basic.odin"
|
||||
#include "math.odin"
|
||||
// #include "game.odin"
|
||||
|
||||
#thread_local tls_int: int
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#load "win32.odin"
|
||||
#include "win32.odin"
|
||||
|
||||
assume :: proc(cond: bool) #foreign "llvm.assume"
|
||||
|
||||
|
||||
+164
-159
@@ -1,34 +1,35 @@
|
||||
#import "win32.odin"
|
||||
#import "fmt.odin"
|
||||
#import "os.odin"
|
||||
#import win32 "sys/windows.odin";
|
||||
#import "fmt.odin";
|
||||
#import "os.odin";
|
||||
#import "mem.odin";
|
||||
|
||||
CANVAS_WIDTH :: 128
|
||||
CANVAS_HEIGHT :: 128
|
||||
CANVAS_SCALE :: 3
|
||||
FRAME_TIME :: 1.0/30.0
|
||||
WINDOW_TITLE : string : "Punity\x00"
|
||||
CANVAS_WIDTH :: 128;
|
||||
CANVAS_HEIGHT :: 128;
|
||||
CANVAS_SCALE :: 3;
|
||||
FRAME_TIME :: 1.0/30.0;
|
||||
WINDOW_TITLE :: "Punity\x00";
|
||||
|
||||
_ := compile_assert(CANVAS_WIDTH % 16 == 0)
|
||||
_ := compile_assert(CANVAS_WIDTH % 16 == 0);
|
||||
|
||||
WINDOW_WIDTH :: CANVAS_WIDTH * CANVAS_SCALE
|
||||
WINDOW_HEIGHT :: CANVAS_HEIGHT * CANVAS_SCALE
|
||||
WINDOW_WIDTH :: CANVAS_WIDTH * CANVAS_SCALE;
|
||||
WINDOW_HEIGHT :: CANVAS_HEIGHT * CANVAS_SCALE;
|
||||
|
||||
|
||||
STACK_CAPACITY :: 1<<20
|
||||
STORAGE_CAPACITY :: 1<<20
|
||||
STACK_CAPACITY :: 1<<20;
|
||||
STORAGE_CAPACITY :: 1<<20;
|
||||
|
||||
DRAW_LIST_RESERVE :: 128
|
||||
DRAW_LIST_RESERVE :: 128;
|
||||
|
||||
MAX_KEYS :: 256
|
||||
MAX_KEYS :: 256;
|
||||
|
||||
Core :: struct {
|
||||
stack: ^Bank
|
||||
storage: ^Bank
|
||||
stack: ^Bank,
|
||||
storage: ^Bank,
|
||||
|
||||
running: bool
|
||||
key_modifiers: u32
|
||||
key_states: [MAX_KEYS]byte
|
||||
key_deltas: [MAX_KEYS]byte
|
||||
running: bool,
|
||||
key_modifiers: u32,
|
||||
key_states: [MAX_KEYS]byte,
|
||||
key_deltas: [MAX_KEYS]byte,
|
||||
|
||||
perf_frame,
|
||||
perf_frame_inner,
|
||||
@@ -36,70 +37,66 @@ Core :: struct {
|
||||
perf_audio,
|
||||
perf_blit,
|
||||
perf_blit_cvt,
|
||||
perf_blit_gdi: Perf_Span
|
||||
perf_blit_gdi: Perf_Span,
|
||||
|
||||
frame: i64
|
||||
frame: i64,
|
||||
|
||||
canvas: Canvas
|
||||
draw_list: ^Draw_List
|
||||
canvas: Canvas,
|
||||
draw_list: ^Draw_List,
|
||||
}
|
||||
|
||||
Perf_Span :: struct {
|
||||
stamp: f64
|
||||
delta: f32
|
||||
stamp: f64,
|
||||
delta: f32,
|
||||
}
|
||||
|
||||
Bank :: struct {
|
||||
memory: []byte
|
||||
cursor: int
|
||||
memory: []byte,
|
||||
cursor: int,
|
||||
}
|
||||
|
||||
Bank_State :: struct {
|
||||
state: Bank
|
||||
bank: ^Bank
|
||||
state: Bank,
|
||||
bank: ^Bank,
|
||||
}
|
||||
|
||||
|
||||
Color :: raw_union {
|
||||
using channels: struct{ a, b, g, r: byte }
|
||||
rgba: u32
|
||||
using channels: struct{a, b, g, r: byte},
|
||||
rgba: u32,
|
||||
}
|
||||
|
||||
Palette :: struct {
|
||||
colors: [256]Color
|
||||
colors_count: byte
|
||||
colors: [256]Color,
|
||||
colors_count: byte,
|
||||
}
|
||||
|
||||
|
||||
Rect :: raw_union {
|
||||
using minmax: struct {
|
||||
min_x, min_y, max_x, max_y: int
|
||||
}
|
||||
using pos: struct {
|
||||
left, top, right, bottom: int
|
||||
}
|
||||
e: [4]int
|
||||
using minmax: struct {min_x, min_y, max_x, max_y: int},
|
||||
using pos: struct {left, top, right, bottom: int},
|
||||
e: [4]int,
|
||||
}
|
||||
|
||||
Bitmap :: struct {
|
||||
pixels: []byte
|
||||
width: int
|
||||
height: int
|
||||
pixels: []byte,
|
||||
width: int,
|
||||
height: int,
|
||||
}
|
||||
|
||||
Font :: struct {
|
||||
using bitmap: Bitmap
|
||||
char_width: int
|
||||
char_height: int
|
||||
using bitmap: Bitmap,
|
||||
char_width: int,
|
||||
char_height: int,
|
||||
}
|
||||
|
||||
Canvas :: struct {
|
||||
using bitmap: ^Bitmap
|
||||
palette: Palette
|
||||
translate_x: int
|
||||
translate_y: int
|
||||
clip: Rect
|
||||
font: ^Font
|
||||
using bitmap: ^Bitmap,
|
||||
palette: Palette,
|
||||
translate_x: int,
|
||||
translate_y: int,
|
||||
clip: Rect,
|
||||
font: ^Font,
|
||||
}
|
||||
|
||||
DrawFlag :: enum {
|
||||
@@ -109,12 +106,9 @@ DrawFlag :: enum {
|
||||
MASK = 1<<2,
|
||||
}
|
||||
|
||||
|
||||
Draw_Item :: struct {}
|
||||
Draw_List :: struct {
|
||||
Item :: struct {
|
||||
|
||||
}
|
||||
items: []Item
|
||||
items: []Draw_Item,
|
||||
}
|
||||
|
||||
Key :: enum {
|
||||
@@ -268,113 +262,112 @@ Key :: enum {
|
||||
BACKSLASH = 92, /* \ */
|
||||
RIGHT_BRACKET = 93, /* ] */
|
||||
GRAVE_ACCENT = 96, /* ` */
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
key_down :: proc(k: Key) -> bool {
|
||||
return _core.key_states[k] != 0
|
||||
return _core.key_states[k] != 0;
|
||||
}
|
||||
|
||||
key_pressed :: proc(k: Key) -> bool {
|
||||
return (_core.key_deltas[k] != 0) && key_down(k)
|
||||
return (_core.key_deltas[k] != 0) && key_down(k);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
win32_perf_count_freq := win32.GetQueryPerformanceFrequency()
|
||||
win32_perf_count_freq := win32.GetQueryPerformanceFrequency();
|
||||
time_now :: proc() -> f64 {
|
||||
assert(win32_perf_count_freq != 0)
|
||||
assert(win32_perf_count_freq != 0);
|
||||
|
||||
counter: i64
|
||||
win32.QueryPerformanceCounter(^counter)
|
||||
result := counter as f64 / win32_perf_count_freq as f64
|
||||
return result
|
||||
counter: i64;
|
||||
win32.QueryPerformanceCounter(^counter);
|
||||
result := cast(f64)counter / cast(f64)win32_perf_count_freq;
|
||||
return result;
|
||||
}
|
||||
|
||||
_core: Core
|
||||
_core: Core;
|
||||
|
||||
run :: proc(user_init, user_step: proc(c: ^Core)) {
|
||||
using win32
|
||||
using win32;
|
||||
|
||||
_core.running = true;
|
||||
|
||||
_core.running = true
|
||||
|
||||
win32_proc :: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT #no_inline #stdcall {
|
||||
win32_proc :: proc(hwnd: win32.HWND, msg: u32, wparam: win32.WPARAM, lparam: win32.LPARAM) -> win32.LRESULT #no_inline #cc_c {
|
||||
win32_app_key_mods :: proc() -> u32 {
|
||||
mods: u32 = 0
|
||||
mods: u32 = 0;
|
||||
|
||||
if is_key_down(Key_Code.SHIFT) {
|
||||
mods |= Key.MOD_SHIFT as u32;
|
||||
mods |= cast(u32)Key.MOD_SHIFT;
|
||||
}
|
||||
if is_key_down(Key_Code.CONTROL) {
|
||||
mods |= Key.MOD_CONTROL as u32;
|
||||
mods |= cast(u32)Key.MOD_CONTROL;
|
||||
}
|
||||
if is_key_down(Key_Code.MENU) {
|
||||
mods |= Key.MOD_ALT as u32;
|
||||
mods |= cast(u32)Key.MOD_ALT;
|
||||
}
|
||||
if is_key_down(Key_Code.LWIN) || is_key_down(Key_Code.RWIN) {
|
||||
mods |= Key.MOD_SUPER as u32;
|
||||
mods |= cast(u32)Key.MOD_SUPER;
|
||||
}
|
||||
|
||||
return mods
|
||||
return mods;
|
||||
}
|
||||
|
||||
match msg {
|
||||
case WM_KEYDOWN:
|
||||
_core.key_modifiers = win32_app_key_mods()
|
||||
_core.key_modifiers = win32_app_key_mods();
|
||||
if wparam < MAX_KEYS {
|
||||
_core.key_states[wparam] = 1
|
||||
_core.key_deltas[wparam] = 1
|
||||
_core.key_states[wparam] = 1;
|
||||
_core.key_deltas[wparam] = 1;
|
||||
}
|
||||
return 0
|
||||
return 0;
|
||||
|
||||
case WM_KEYUP:
|
||||
_core.key_modifiers = win32_app_key_mods()
|
||||
_core.key_modifiers = win32_app_key_mods();
|
||||
if wparam < MAX_KEYS {
|
||||
_core.key_states[wparam] = 0
|
||||
_core.key_deltas[wparam] = 1
|
||||
_core.key_states[wparam] = 0;
|
||||
_core.key_deltas[wparam] = 1;
|
||||
}
|
||||
return 0
|
||||
return 0;
|
||||
|
||||
case WM_CLOSE:
|
||||
PostQuitMessage(0)
|
||||
_core.running = false
|
||||
return 0
|
||||
PostQuitMessage(0);
|
||||
_core.running = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return DefWindowProcA(hwnd, msg, wparam, lparam)
|
||||
return DefWindowProcA(hwnd, msg, wparam, lparam);
|
||||
}
|
||||
|
||||
|
||||
window_class := WNDCLASSEXA{
|
||||
class_name = ("Punity\x00" as string).data, // C-style string
|
||||
size = size_of(WNDCLASSEXA) as u32,
|
||||
class_name = (cast(string)"Punity\x00").data, // C-style string
|
||||
size = size_of(WNDCLASSEXA),
|
||||
style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
|
||||
instance = GetModuleHandleA(null) as HINSTANCE,
|
||||
instance = cast(HINSTANCE)GetModuleHandleA(nil),
|
||||
wnd_proc = win32_proc,
|
||||
// wnd_proc = DefWindowProcA,
|
||||
background = GetStockObject(BLACK_BRUSH) as HBRUSH,
|
||||
}
|
||||
background = cast(HBRUSH)GetStockObject(BLACK_BRUSH),
|
||||
};
|
||||
|
||||
if RegisterClassExA(^window_class) == 0 {
|
||||
fmt.fprintln(os.stderr, "RegisterClassExA failed")
|
||||
return
|
||||
fmt.fprintln(os.stderr, "RegisterClassExA failed");
|
||||
return;
|
||||
}
|
||||
|
||||
screen_width := GetSystemMetrics(SM_CXSCREEN)
|
||||
screen_height := GetSystemMetrics(SM_CYSCREEN)
|
||||
screen_width := GetSystemMetrics(SM_CXSCREEN);
|
||||
screen_height := GetSystemMetrics(SM_CYSCREEN);
|
||||
|
||||
rc: RECT
|
||||
rc.left = (screen_width - WINDOW_WIDTH) / 2
|
||||
rc.top = (screen_height - WINDOW_HEIGHT) / 2
|
||||
rc.right = rc.left + WINDOW_WIDTH
|
||||
rc.bottom = rc.top + WINDOW_HEIGHT
|
||||
rc: RECT;
|
||||
rc.left = (screen_width - WINDOW_WIDTH) / 2;
|
||||
rc.top = (screen_height - WINDOW_HEIGHT) / 2;
|
||||
rc.right = rc.left + WINDOW_WIDTH;
|
||||
rc.bottom = rc.top + WINDOW_HEIGHT;
|
||||
|
||||
style: u32 = WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX
|
||||
assert(AdjustWindowRect(^rc, style, 0) != 0)
|
||||
style: u32 = WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
|
||||
assert(AdjustWindowRect(^rc, style, 0) != 0);
|
||||
|
||||
wt := WINDOW_TITLE
|
||||
wt := WINDOW_TITLE;
|
||||
|
||||
win32_window := CreateWindowExA(0,
|
||||
window_class.class_name,
|
||||
@@ -382,83 +375,82 @@ run :: proc(user_init, user_step: proc(c: ^Core)) {
|
||||
style,
|
||||
rc.left, rc.top,
|
||||
rc.right-rc.left, rc.bottom-rc.top,
|
||||
null, null, window_class.instance,
|
||||
null);
|
||||
nil, nil, window_class.instance,
|
||||
nil);
|
||||
|
||||
if win32_window == null {
|
||||
fmt.fprintln(os.stderr, "CreateWindowExA failed")
|
||||
return
|
||||
if win32_window == nil {
|
||||
fmt.fprintln(os.stderr, "CreateWindowExA failed");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
window_bmi: BITMAPINFO;
|
||||
window_bmi.size = size_of(BITMAPINFO.HEADER) as u32
|
||||
window_bmi.width = CANVAS_WIDTH
|
||||
window_bmi.height = CANVAS_HEIGHT
|
||||
window_bmi.planes = 1
|
||||
window_bmi.bit_count = 32
|
||||
window_bmi.compression = BI_RGB
|
||||
window_bmi.size = size_of(BITMAPINFOHEADER);
|
||||
window_bmi.width = CANVAS_WIDTH;
|
||||
window_bmi.height = CANVAS_HEIGHT;
|
||||
window_bmi.planes = 1;
|
||||
window_bmi.bit_count = 32;
|
||||
window_bmi.compression = BI_RGB;
|
||||
|
||||
|
||||
user_init(^_core)
|
||||
user_init(^_core);
|
||||
|
||||
|
||||
ShowWindow(win32_window, SW_SHOW)
|
||||
ShowWindow(win32_window, SW_SHOW);
|
||||
|
||||
window_buffer := new_slice(u32, CANVAS_WIDTH * CANVAS_HEIGHT);
|
||||
assert(window_buffer.data != null)
|
||||
defer free(window_buffer.data)
|
||||
defer free(window_buffer);
|
||||
|
||||
for i := 0; i < window_buffer.count; i++ {
|
||||
window_buffer[i] = 0xff00ff
|
||||
|
||||
for i := 0; i < window_buffer.count; i += 1 {
|
||||
window_buffer[i] = 0xff00ff;
|
||||
}
|
||||
|
||||
|
||||
prev_time, curr_time,dt: f64
|
||||
prev_time = time_now()
|
||||
curr_time = time_now()
|
||||
total_time : f64 = 0
|
||||
dt: f64;
|
||||
prev_time := time_now();
|
||||
curr_time := time_now();
|
||||
total_time : f64 = 0;
|
||||
offset_x := 0;
|
||||
offset_y := 0;
|
||||
|
||||
message: MSG
|
||||
message: MSG;
|
||||
for _core.running {
|
||||
curr_time = time_now()
|
||||
dt = curr_time - prev_time
|
||||
prev_time = curr_time
|
||||
total_time += dt
|
||||
curr_time = time_now();
|
||||
dt = curr_time - prev_time;
|
||||
prev_time = curr_time;
|
||||
total_time += dt;
|
||||
|
||||
offset_x += 1
|
||||
offset_y += 2
|
||||
offset_x += 1;
|
||||
offset_y += 2;
|
||||
|
||||
{
|
||||
data: [128]byte
|
||||
buf := data[:0]
|
||||
fmt.bprintf(^buf, "Punity: % ms\x00", dt*1000)
|
||||
win32.SetWindowTextA(win32_window, buf.data)
|
||||
data: [128]byte;
|
||||
buf: fmt.Buffer;
|
||||
buf.data = data[:];
|
||||
fmt.bprintf(^buf, "Punity: %.4f ms\x00", dt*1000);
|
||||
win32.SetWindowTextA(win32_window, ^buf[0]);
|
||||
}
|
||||
|
||||
|
||||
for y := 0; y < CANVAS_HEIGHT; y++ {
|
||||
for x := 0; x < CANVAS_WIDTH; x++ {
|
||||
g := (x % 32) * 8
|
||||
b := (y % 32) * 8
|
||||
window_buffer[x + y*CANVAS_WIDTH] = (g << 8 | b) as u32
|
||||
for y := 0; y < CANVAS_HEIGHT; y += 1 {
|
||||
for x := 0; x < CANVAS_WIDTH; x += 1 {
|
||||
g := (x % 32) * 8;
|
||||
b := (y % 32) * 8;
|
||||
window_buffer[x + y*CANVAS_WIDTH] = cast(u32)(g << 8 | b);
|
||||
}
|
||||
}
|
||||
|
||||
memory_zero(^_core.key_deltas[0], size_of_val(_core.key_deltas[0]))
|
||||
mem.zero(^_core.key_deltas[0], size_of_val(_core.key_deltas));
|
||||
|
||||
|
||||
for PeekMessageA(^message, null, 0, 0, PM_REMOVE) != 0 {
|
||||
for PeekMessageA(^message, nil, 0, 0, PM_REMOVE) != 0 {
|
||||
if message.message == WM_QUIT {
|
||||
_core.running = false
|
||||
_core.running = false;
|
||||
}
|
||||
TranslateMessage(^message)
|
||||
DispatchMessageA(^message)
|
||||
TranslateMessage(^message);
|
||||
DispatchMessageA(^message);
|
||||
}
|
||||
|
||||
user_step(^_core)
|
||||
user_step(^_core);
|
||||
|
||||
dc := GetDC(win32_window);
|
||||
StretchDIBits(dc,
|
||||
@@ -467,18 +459,31 @@ run :: proc(user_init, user_step: proc(c: ^Core)) {
|
||||
window_buffer.data,
|
||||
^window_bmi,
|
||||
DIB_RGB_COLORS,
|
||||
SRCCOPY)
|
||||
ReleaseDC(win32_window, dc)
|
||||
SRCCOPY);
|
||||
ReleaseDC(win32_window, dc);
|
||||
|
||||
|
||||
{
|
||||
delta := time_now() - prev_time
|
||||
ms := ((FRAME_TIME - delta) * 1000) as i32
|
||||
delta := time_now() - prev_time;
|
||||
ms := cast(i32)((FRAME_TIME - delta) * 1000);
|
||||
if ms > 0 {
|
||||
win32.Sleep(ms)
|
||||
win32.Sleep(ms);
|
||||
}
|
||||
}
|
||||
|
||||
_core.frame++
|
||||
_core.frame += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
main :: proc() {
|
||||
user_init :: proc(c: ^Core) {
|
||||
|
||||
}
|
||||
|
||||
user_step :: proc(c: ^Core) {
|
||||
|
||||
}
|
||||
|
||||
run(user_init, user_step);
|
||||
}
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ thing :: proc() {
|
||||
*/
|
||||
|
||||
/*
|
||||
#load "fmt.odin"
|
||||
#include "fmt.odin"
|
||||
|
||||
thing :: proc() {
|
||||
println("Hello5!")
|
||||
|
||||
+214
-243
@@ -1,288 +1,272 @@
|
||||
#shared_global_scope
|
||||
#shared_global_scope;
|
||||
|
||||
#import "os.odin"
|
||||
#import "fmt.odin"
|
||||
#import "mem.odin"
|
||||
#import "os.odin";
|
||||
#import "fmt.odin";
|
||||
#import "mem.odin";
|
||||
#import "utf8.odin";
|
||||
|
||||
/*
|
||||
Optimization_Level :: enum {
|
||||
DEBUG,
|
||||
RELEASE,
|
||||
}
|
||||
// IMPORTANT NOTE(bill): `type_info` & `type_info_val` cannot be used within a
|
||||
// #shared_global_scope due to the internals of the compiler.
|
||||
// This could change at a later date if the all these data structures are
|
||||
// implemented within the compiler rather than in this "preload" file
|
||||
|
||||
Bounds_Check_Mode :: enum {
|
||||
ON,
|
||||
OFF,
|
||||
}
|
||||
|
||||
Build_Options :: struct {
|
||||
optimization_level: Optimization_Level
|
||||
|
||||
bounds_check: Bounds_Check_Mode
|
||||
|
||||
output_name: string
|
||||
output_path: string
|
||||
}
|
||||
|
||||
build_options: Build_Options
|
||||
*/
|
||||
|
||||
// IMPORTANT NOTE(bill): Do not change the order of any of this data
|
||||
// The compiler relies upon this _exact_ order
|
||||
Type_Info :: union {
|
||||
Member :: struct #ordered {
|
||||
name: string // can be empty if tuple
|
||||
type_info: ^Type_Info
|
||||
offset: int // offsets are not used in tuples
|
||||
}
|
||||
Record :: struct #ordered {
|
||||
fields: []Member
|
||||
size: int // in bytes
|
||||
align: int // in bytes
|
||||
packed: bool
|
||||
ordered: bool
|
||||
}
|
||||
|
||||
Named: struct #ordered {
|
||||
name: string
|
||||
base: ^Type_Info // This will _not_ be a Type_Info.Named
|
||||
}
|
||||
Integer: struct #ordered {
|
||||
size: int // in bytes
|
||||
signed: bool
|
||||
}
|
||||
Float: struct #ordered {
|
||||
size: int // in bytes
|
||||
}
|
||||
Any: struct #ordered {}
|
||||
String: struct #ordered {}
|
||||
Boolean: struct #ordered {}
|
||||
Pointer: struct #ordered {
|
||||
elem: ^Type_Info // nil -> rawptr
|
||||
}
|
||||
Maybe: struct #ordered {
|
||||
elem: ^Type_Info
|
||||
}
|
||||
Procedure: struct #ordered {
|
||||
params: ^Type_Info // Type_Info.Tuple
|
||||
results: ^Type_Info // Type_Info.Tuple
|
||||
variadic: bool
|
||||
}
|
||||
Array: struct #ordered {
|
||||
elem: ^Type_Info
|
||||
elem_size: int
|
||||
count: int
|
||||
}
|
||||
Slice: struct #ordered {
|
||||
elem: ^Type_Info
|
||||
elem_size: int
|
||||
}
|
||||
Vector: struct #ordered {
|
||||
elem: ^Type_Info
|
||||
elem_size: int
|
||||
count: int
|
||||
align: int
|
||||
}
|
||||
Tuple: Record
|
||||
Struct: Record
|
||||
Union: Record
|
||||
Raw_Union: Record
|
||||
Enum: struct #ordered {
|
||||
base: ^Type_Info
|
||||
values: []i64
|
||||
names: []string
|
||||
}
|
||||
Type_Info_Member :: struct #ordered {
|
||||
name: string, // can be empty if tuple
|
||||
type_info: ^Type_Info,
|
||||
offset: int, // offsets are not used in tuples
|
||||
}
|
||||
Type_Info_Record :: struct #ordered {
|
||||
fields: []Type_Info_Member,
|
||||
size: int, // in bytes
|
||||
align: int, // in bytes
|
||||
packed: bool,
|
||||
ordered: bool,
|
||||
}
|
||||
Type_Info_Enum_Value :: raw_union {
|
||||
f: f64,
|
||||
i: i64,
|
||||
}
|
||||
|
||||
// NOTE(bill): This much the same as the compiler's
|
||||
Calling_Convention :: enum {
|
||||
ODIN = 0,
|
||||
C = 1,
|
||||
STD = 2,
|
||||
FAST = 3,
|
||||
}
|
||||
|
||||
Type_Info :: union {
|
||||
Named: struct #ordered {
|
||||
name: string,
|
||||
base: ^Type_Info, // This will _not_ be a Type_Info.Named
|
||||
},
|
||||
Integer: struct #ordered {
|
||||
size: int, // in bytes
|
||||
signed: bool,
|
||||
},
|
||||
Float: struct #ordered {
|
||||
size: int, // in bytes
|
||||
},
|
||||
Any: struct #ordered {},
|
||||
String: struct #ordered {},
|
||||
Boolean: struct #ordered {},
|
||||
Pointer: struct #ordered {
|
||||
elem: ^Type_Info, // nil -> rawptr
|
||||
},
|
||||
Maybe: struct #ordered {
|
||||
elem: ^Type_Info,
|
||||
},
|
||||
Procedure: struct #ordered {
|
||||
params: ^Type_Info, // Type_Info.Tuple
|
||||
results: ^Type_Info, // Type_Info.Tuple
|
||||
variadic: bool,
|
||||
convention: Calling_Convention,
|
||||
},
|
||||
Array: struct #ordered {
|
||||
elem: ^Type_Info,
|
||||
elem_size: int,
|
||||
count: int,
|
||||
},
|
||||
Slice: struct #ordered {
|
||||
elem: ^Type_Info,
|
||||
elem_size: int,
|
||||
},
|
||||
Vector: struct #ordered {
|
||||
elem: ^Type_Info,
|
||||
elem_size: int,
|
||||
count: int,
|
||||
align: int,
|
||||
},
|
||||
Tuple: Type_Info_Record,
|
||||
Struct: Type_Info_Record,
|
||||
Union: Type_Info_Record,
|
||||
Raw_Union: Type_Info_Record,
|
||||
Enum: struct #ordered {
|
||||
base: ^Type_Info,
|
||||
names: []string,
|
||||
values: []Type_Info_Enum_Value,
|
||||
},
|
||||
}
|
||||
|
||||
// // NOTE(bill): only the ones that are needed (not all types)
|
||||
// // This will be set by the compiler
|
||||
// immutable __type_infos: []Type_Info;
|
||||
|
||||
type_info_base :: proc(info: ^Type_Info) -> ^Type_Info {
|
||||
if info == nil {
|
||||
return nil
|
||||
return nil;
|
||||
}
|
||||
base := info
|
||||
match type i : base {
|
||||
base := info;
|
||||
match type i in base {
|
||||
case Type_Info.Named:
|
||||
base = i.base
|
||||
base = i.base;
|
||||
}
|
||||
return base
|
||||
return base;
|
||||
}
|
||||
|
||||
|
||||
|
||||
assume :: proc(cond: bool) #foreign "llvm.assume"
|
||||
|
||||
__debug_trap :: proc() #foreign "llvm.debugtrap"
|
||||
__trap :: proc() #foreign "llvm.trap"
|
||||
read_cycle_counter :: proc() -> u64 #foreign "llvm.readcyclecounter"
|
||||
|
||||
bit_reverse16 :: proc(b: u16) -> u16 #foreign "llvm.bitreverse.i16"
|
||||
bit_reverse32 :: proc(b: u32) -> u32 #foreign "llvm.bitreverse.i32"
|
||||
bit_reverse64 :: proc(b: u64) -> u64 #foreign "llvm.bitreverse.i64"
|
||||
|
||||
byte_swap16 :: proc(b: u16) -> u16 #foreign "llvm.bswap.i16"
|
||||
byte_swap32 :: proc(b: u32) -> u32 #foreign "llvm.bswap.i32"
|
||||
byte_swap64 :: proc(b: u64) -> u64 #foreign "llvm.bswap.i64"
|
||||
|
||||
fmuladd32 :: proc(a, b, c: f32) -> f32 #foreign "llvm.fmuladd.f32"
|
||||
fmuladd64 :: proc(a, b, c: f64) -> f64 #foreign "llvm.fmuladd.f64"
|
||||
|
||||
|
||||
|
||||
|
||||
assume :: proc(cond: bool) #foreign __llvm_core "llvm.assume";
|
||||
|
||||
__debug_trap :: proc() #foreign __llvm_core "llvm.debugtrap";
|
||||
__trap :: proc() #foreign __llvm_core "llvm.trap";
|
||||
read_cycle_counter :: proc() -> u64 #foreign __llvm_core "llvm.readcyclecounter";
|
||||
|
||||
|
||||
Allocator_Mode :: enum u8 {
|
||||
ALLOC,
|
||||
FREE,
|
||||
FREE_ALL,
|
||||
RESIZE,
|
||||
}
|
||||
Allocator_Proc :: type proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr;
|
||||
Allocator :: struct #ordered {
|
||||
Mode :: enum {
|
||||
ALLOC,
|
||||
FREE,
|
||||
FREE_ALL,
|
||||
RESIZE,
|
||||
}
|
||||
Proc :: type proc(allocator_data: rawptr, mode: Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr
|
||||
|
||||
|
||||
procedure: Proc;
|
||||
data: rawptr
|
||||
procedure: Allocator_Proc,
|
||||
data: rawptr,
|
||||
}
|
||||
|
||||
|
||||
Context :: struct #ordered {
|
||||
thread_id: int
|
||||
thread_id: int,
|
||||
|
||||
allocator: Allocator
|
||||
allocator: Allocator,
|
||||
|
||||
user_data: rawptr
|
||||
user_index: int
|
||||
user_data: rawptr,
|
||||
user_index: int,
|
||||
}
|
||||
|
||||
#thread_local __context: Context
|
||||
thread_local __context: Context;
|
||||
|
||||
|
||||
DEFAULT_ALIGNMENT :: align_of({4}f32)
|
||||
DEFAULT_ALIGNMENT :: align_of([vector 4]f32);
|
||||
|
||||
|
||||
__check_context :: proc() {
|
||||
c := ^__context
|
||||
c := ^__context;
|
||||
|
||||
if c.allocator.procedure == nil {
|
||||
c.allocator = default_allocator()
|
||||
c.allocator = default_allocator();
|
||||
}
|
||||
if c.thread_id == 0 {
|
||||
c.thread_id = os.current_thread_id()
|
||||
c.thread_id = os.current_thread_id();
|
||||
}
|
||||
}
|
||||
|
||||
alloc :: proc(size: int) -> rawptr #inline { return alloc_align(size, DEFAULT_ALIGNMENT) }
|
||||
alloc :: proc(size: int) -> rawptr #inline { return alloc_align(size, DEFAULT_ALIGNMENT); }
|
||||
|
||||
alloc_align :: proc(size, alignment: int) -> rawptr #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
return a.procedure(a.data, Allocator.Mode.ALLOC, size, alignment, nil, 0, 0)
|
||||
__check_context();
|
||||
a := context.allocator;
|
||||
return a.procedure(a.data, Allocator_Mode.ALLOC, size, alignment, nil, 0, 0);
|
||||
}
|
||||
|
||||
free :: proc(ptr: rawptr) #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
free_ptr :: proc(ptr: rawptr) #inline {
|
||||
__check_context();
|
||||
a := context.allocator;
|
||||
if ptr != nil {
|
||||
a.procedure(a.data, Allocator.Mode.FREE, 0, 0, ptr, 0, 0)
|
||||
a.procedure(a.data, Allocator_Mode.FREE, 0, 0, ptr, 0, 0);
|
||||
}
|
||||
}
|
||||
free_all :: proc() #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
a.procedure(a.data, Allocator.Mode.FREE_ALL, 0, 0, nil, 0, 0)
|
||||
__check_context();
|
||||
a := context.allocator;
|
||||
a.procedure(a.data, Allocator_Mode.FREE_ALL, 0, 0, nil, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
resize :: proc(ptr: rawptr, old_size, new_size: int) -> rawptr #inline { return resize_align(ptr, old_size, new_size, DEFAULT_ALIGNMENT) }
|
||||
resize :: proc(ptr: rawptr, old_size, new_size: int) -> rawptr #inline { return resize_align(ptr, old_size, new_size, DEFAULT_ALIGNMENT); }
|
||||
resize_align :: proc(ptr: rawptr, old_size, new_size, alignment: int) -> rawptr #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
return a.procedure(a.data, Allocator.Mode.RESIZE, new_size, alignment, ptr, old_size, 0)
|
||||
__check_context();
|
||||
a := context.allocator;
|
||||
return a.procedure(a.data, Allocator_Mode.RESIZE, new_size, alignment, ptr, old_size, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: int) -> rawptr {
|
||||
if old_memory == nil {
|
||||
return alloc_align(new_size, alignment)
|
||||
return alloc_align(new_size, alignment);
|
||||
}
|
||||
|
||||
if new_size == 0 {
|
||||
free(old_memory)
|
||||
return nil
|
||||
free(old_memory);
|
||||
return nil;
|
||||
}
|
||||
|
||||
if new_size == old_size {
|
||||
return old_memory
|
||||
return old_memory;
|
||||
}
|
||||
|
||||
new_memory := alloc_align(new_size, alignment)
|
||||
new_memory := alloc_align(new_size, alignment);
|
||||
if new_memory == nil {
|
||||
return nil
|
||||
return nil;
|
||||
}
|
||||
|
||||
mem.copy(new_memory, old_memory, min(old_size, new_size));
|
||||
free(old_memory)
|
||||
return new_memory
|
||||
mem.copy(new_memory, old_memory, min(old_size, new_size));;
|
||||
free(old_memory);
|
||||
return new_memory;
|
||||
}
|
||||
|
||||
|
||||
default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator.Mode,
|
||||
default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
|
||||
using Allocator.Mode
|
||||
/*
|
||||
match mode {
|
||||
case ALLOC:
|
||||
total_size := size + alignment + size_of(mem.AllocationHeader)
|
||||
ptr := os.heap_alloc(total_size)
|
||||
header := ptr as ^mem.AllocationHeader
|
||||
ptr = mem.align_forward(header+1, alignment)
|
||||
mem.allocation_header_fill(header, ptr, size)
|
||||
return mem.zero(ptr, size)
|
||||
using Allocator_Mode;
|
||||
|
||||
case FREE:
|
||||
os.heap_free(mem.allocation_header(old_memory))
|
||||
return nil
|
||||
when false {
|
||||
match mode {
|
||||
case ALLOC:
|
||||
total_size := size + alignment + size_of(mem.AllocationHeader);
|
||||
ptr := os.heap_alloc(total_size);
|
||||
header := (^mem.AllocationHeader)(ptr);
|
||||
ptr = mem.align_forward(header+1, alignment);
|
||||
mem.allocation_header_fill(header, ptr, size);
|
||||
return mem.zero(ptr, size);
|
||||
|
||||
case FREE_ALL:
|
||||
// NOTE(bill): Does nothing
|
||||
case FREE:
|
||||
os.heap_free(mem.allocation_header(old_memory));
|
||||
return nil;
|
||||
|
||||
case RESIZE:
|
||||
total_size := size + alignment + size_of(mem.AllocationHeader)
|
||||
ptr := os.heap_resize(mem.allocation_header(old_memory), total_size)
|
||||
header := ptr as ^mem.AllocationHeader
|
||||
ptr = mem.align_forward(header+1, alignment)
|
||||
mem.allocation_header_fill(header, ptr, size)
|
||||
return mem.zero(ptr, size)
|
||||
}
|
||||
*/
|
||||
match mode {
|
||||
case ALLOC:
|
||||
return os.heap_alloc(size)
|
||||
case FREE_ALL:
|
||||
// NOTE(bill): Does nothing
|
||||
|
||||
case FREE:
|
||||
os.heap_free(old_memory)
|
||||
return nil
|
||||
case RESIZE:
|
||||
total_size := size + alignment + size_of(mem.AllocationHeader);
|
||||
ptr := os.heap_resize(mem.allocation_header(old_memory), total_size);
|
||||
header := (^mem.AllocationHeader)(ptr);
|
||||
ptr = mem.align_forward(header+1, alignment);
|
||||
mem.allocation_header_fill(header, ptr, size);
|
||||
return mem.zero(ptr, size);
|
||||
}
|
||||
} else {
|
||||
match mode {
|
||||
case ALLOC:
|
||||
return os.heap_alloc(size);
|
||||
|
||||
case FREE_ALL:
|
||||
// NOTE(bill): Does nothing
|
||||
case FREE:
|
||||
os.heap_free(old_memory);
|
||||
return nil;
|
||||
|
||||
case RESIZE:
|
||||
return os.heap_resize(old_memory, size)
|
||||
case FREE_ALL:
|
||||
// NOTE(bill): Does nothing
|
||||
|
||||
case RESIZE:
|
||||
return os.heap_resize(old_memory, size);
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil;
|
||||
}
|
||||
|
||||
default_allocator :: proc() -> Allocator {
|
||||
return Allocator{
|
||||
procedure = default_allocator_proc,
|
||||
data = nil,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -297,71 +281,58 @@ default_allocator :: proc() -> Allocator {
|
||||
|
||||
__string_eq :: proc(a, b: string) -> bool {
|
||||
if a.count != b.count {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
if a.data == b.data {
|
||||
return true
|
||||
return true;
|
||||
}
|
||||
return mem.compare(a.data, b.data, a.count) == 0
|
||||
return mem.compare(cast(rawptr)a.data, cast(rawptr)b.data, a.count) == 0;
|
||||
}
|
||||
|
||||
__string_cmp :: proc(a, b : string) -> int {
|
||||
return mem.compare(a.data, b.data, min(a.count, b.count))
|
||||
__string_cmp :: proc(a, b: string) -> int {
|
||||
return mem.compare(cast(rawptr)a.data, cast(rawptr)b.data, min(a.count, b.count));
|
||||
}
|
||||
|
||||
__string_ne :: proc(a, b: string) -> bool #inline { return !__string_eq(a, b) }
|
||||
__string_lt :: proc(a, b: string) -> bool #inline { return __string_cmp(a, b) < 0 }
|
||||
__string_gt :: proc(a, b: string) -> bool #inline { return __string_cmp(a, b) > 0 }
|
||||
__string_le :: proc(a, b: string) -> bool #inline { return __string_cmp(a, b) <= 0 }
|
||||
__string_ge :: proc(a, b: string) -> bool #inline { return __string_cmp(a, b) >= 0 }
|
||||
__string_ne :: proc(a, b: string) -> bool #inline { return !__string_eq(a, b); }
|
||||
__string_lt :: proc(a, b: string) -> bool #inline { return __string_cmp(a, b) < 0; }
|
||||
__string_gt :: proc(a, b: string) -> bool #inline { return __string_cmp(a, b) > 0; }
|
||||
__string_le :: proc(a, b: string) -> bool #inline { return __string_cmp(a, b) <= 0; }
|
||||
__string_ge :: proc(a, b: string) -> bool #inline { return __string_cmp(a, b) >= 0; }
|
||||
|
||||
|
||||
__assert :: proc(file: string, line, column: int, msg: string) #inline {
|
||||
fmt.fprintf(os.stderr, "%(%:%) Runtime assertion: %\n",
|
||||
file, line, column, msg)
|
||||
__debug_trap()
|
||||
fmt.fprintf(os.stderr, "%s(%d:%d) Runtime assertion: %s\n",
|
||||
file, line, column, msg);
|
||||
__debug_trap();
|
||||
}
|
||||
|
||||
__bounds_check_error :: proc(file: string, line, column: int,
|
||||
index, count: int) {
|
||||
__bounds_check_error :: proc(file: string, line, column: int, index, count: int) {
|
||||
if 0 <= index && index < count {
|
||||
return
|
||||
return;
|
||||
}
|
||||
fmt.fprintf(os.stderr, "%(%:%) Index % is out of bounds range [0, %)\n",
|
||||
file, line, column, index, count)
|
||||
__debug_trap()
|
||||
fmt.fprintf(os.stderr, "%s(%d:%d) Index %d is out of bounds range 0..<%d\n",
|
||||
file, line, column, index, count);
|
||||
__debug_trap();
|
||||
}
|
||||
|
||||
__slice_expr_error :: proc(file: string, line, column: int,
|
||||
low, high, max: int) {
|
||||
if 0 <= low && low <= high && high <= max {
|
||||
return
|
||||
}
|
||||
fmt.fprintf(os.stderr, "%(%:%) Invalid slice indices: [%:%:%]\n",
|
||||
file, line, column, low, high, max)
|
||||
__debug_trap()
|
||||
}
|
||||
__substring_expr_error :: proc(file: string, line, column: int,
|
||||
low, high: int) {
|
||||
__slice_expr_error :: proc(file: string, line, column: int, low, high: int) {
|
||||
if 0 <= low && low <= high {
|
||||
return
|
||||
return;
|
||||
}
|
||||
fmt.fprintf(os.stderr, "%(%:%) Invalid substring indices: [%:%:%]\n",
|
||||
file, line, column, low, high)
|
||||
__debug_trap()
|
||||
fmt.fprintf(os.stderr, "%s(%d:%d) Invalid slice indices: [%d:%d]\n",
|
||||
file, line, column, low, high);
|
||||
__debug_trap();
|
||||
}
|
||||
__substring_expr_error :: proc(file: string, line, column: int, low, high: int) {
|
||||
if 0 <= low && low <= high {
|
||||
return;
|
||||
}
|
||||
fmt.fprintf(os.stderr, "%s(%d:%d) Invalid substring indices: [%d:%d]\n",
|
||||
file, line, column, low, high);
|
||||
__debug_trap();
|
||||
}
|
||||
|
||||
__enum_to_string :: proc(info: ^Type_Info, value: i64) -> string {
|
||||
match type ti : type_info_base(info) {
|
||||
case Type_Info.Enum:
|
||||
// TODO(bill): Search faster than linearly
|
||||
for i := 0; i < ti.values.count; i++ {
|
||||
if ti.values[i] == value {
|
||||
return ti.names[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
__string_decode_rune :: proc(s: string) -> (rune, int) #inline {
|
||||
return utf8.decode_rune(s);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+138
-137
@@ -1,157 +1,158 @@
|
||||
#shared_global_scope
|
||||
|
||||
#import "fmt.odin"
|
||||
#shared_global_scope;
|
||||
|
||||
|
||||
__u128_mod :: proc(a, b: u128) -> u128 #link_name "__umodti3" {
|
||||
_, r := __u128_quo_mod(a, b)
|
||||
return r
|
||||
}
|
||||
// import "fmt.odin";
|
||||
|
||||
__u128_quo :: proc(a, b: u128) -> u128 #link_name "__udivti3" {
|
||||
n, _ := __u128_quo_mod(a, b)
|
||||
return n
|
||||
}
|
||||
// proc __u128_mod(a, b: u128) -> u128 #link_name "__umodti3" {
|
||||
// var _, r := __u128_quo_mod(a, b)
|
||||
// return r
|
||||
// }
|
||||
|
||||
__i128_mod :: proc(a, b: i128) -> i128 #link_name "__modti3" {
|
||||
_, r := __i128_quo_mod(a, b)
|
||||
return r
|
||||
}
|
||||
// proc __u128_quo(a, b: u128) -> u128 #link_name "__udivti3" {
|
||||
// var n, _ := __u128_quo_mod(a, b)
|
||||
// return n
|
||||
// }
|
||||
|
||||
__i128_quo :: proc(a, b: i128) -> i128 #link_name "__divti3" {
|
||||
n, _ := __i128_quo_mod(a, b)
|
||||
return n
|
||||
}
|
||||
// proc __i128_mod(a, b: i128) -> i128 #link_name "__modti3" {
|
||||
// var _, r := __i128_quo_mod(a, b)
|
||||
// return r
|
||||
// }
|
||||
|
||||
__i128_quo_mod :: proc(a, b: i128) -> (i128, i128) #link_name "__divmodti4" {
|
||||
s := b >> 127
|
||||
b = (b ~ s) - s
|
||||
s = a >> 127
|
||||
a = (a ~ s) - s
|
||||
// proc __i128_quo(a, b: i128) -> i128 #link_name "__divti3" {
|
||||
// var n, _ := __i128_quo_mod(a, b)
|
||||
// return n
|
||||
// }
|
||||
|
||||
n, r := __u128_quo_mod(a as u128, b as u128)
|
||||
return (n as i128 ~ s) - s, (r as i128 ~ s) - s
|
||||
}
|
||||
// proc __i128_quo_mod(a, b: i128) -> (i128, i128) #link_name "__divmodti4" {
|
||||
// var s := b >> 127
|
||||
// b = (b ~ s) - s
|
||||
// s = a >> 127
|
||||
// a = (a ~ s) - s
|
||||
|
||||
// var n, r := __u128_quo_mod(a as u128, b as u128)
|
||||
// return (n as i128 ~ s) - s, (r as i128 ~ s) - s
|
||||
// }
|
||||
|
||||
|
||||
__u128_quo_mod :: proc(a, b: u128) -> (u128, u128) #link_name "__udivmodti4" {
|
||||
clz :: proc(x: u64) -> u64 {
|
||||
clz_u64 :: proc(x: u64, is_zero_undef: bool) -> u64 #foreign "llvm.ctlz.i64"
|
||||
return clz_u64(x, false)
|
||||
}
|
||||
ctz :: proc(x: u64) -> u64 {
|
||||
ctz_u64 :: proc(x: u64, is_zero_undef: bool) -> u64 #foreign "llvm.cttz.i64"
|
||||
return ctz_u64(x, false)
|
||||
}
|
||||
// proc __u128_quo_mod(a, b: u128) -> (u128, u128) #link_name "__udivmodti4" {
|
||||
// proc clz(x: u64) -> u64 {
|
||||
// proc clz_u64(x: u64, is_zero_undef: bool) -> u64 #foreign "llvm.ctlz.i64"
|
||||
// return clz_u64(x, false)
|
||||
// }
|
||||
// proc ctz(x: u64) -> u64 {
|
||||
// proc ctz_u64(x: u64, is_zero_undef: bool) -> u64 #foreign "llvm.cttz.i64"
|
||||
// return ctz_u64(x, false)
|
||||
// }
|
||||
|
||||
|
||||
u128_lo_hi :: raw_union {
|
||||
all: u128
|
||||
using _lohi: struct {lo, hi: u64}
|
||||
}
|
||||
// u128_lo_hi :: raw_union {
|
||||
// all: u128
|
||||
// using _lohi: struct {lo, hi: u64;}
|
||||
// }
|
||||
|
||||
n, d, q, r: u128_lo_hi
|
||||
sr: u64
|
||||
// n, d, q, r: u128_lo_hi
|
||||
// sr: u64
|
||||
|
||||
n.all = a
|
||||
d.all = b
|
||||
// n.all = a
|
||||
// d.all = b
|
||||
|
||||
if n.hi == 0 {
|
||||
if d.hi == 0 {
|
||||
return (n.lo / d.lo) as u128, (n.lo % d.lo) as u128
|
||||
}
|
||||
return 0, n.lo as u128
|
||||
}
|
||||
if d.lo == 0 {
|
||||
if d.hi == 0 {
|
||||
return (n.hi / d.lo) as u128, (n.hi % d.lo) as u128
|
||||
}
|
||||
if n.lo == 0 {
|
||||
r.hi = n.hi % d.hi
|
||||
r.lo = 0
|
||||
return (n.hi / d.hi) as u128, r.all
|
||||
}
|
||||
if (d.hi & (d.hi-1)) == 0 {
|
||||
r.lo = n.lo
|
||||
r.hi = n.hi & (d.hi-1)
|
||||
return (n.hi >> ctz(d.hi)) as u128, r.all
|
||||
}
|
||||
// if n.hi == 0 {
|
||||
// if d.hi == 0 {
|
||||
// return (n.lo / d.lo) as u128, (n.lo % d.lo) as u128
|
||||
// }
|
||||
// return 0, n.lo as u128
|
||||
// }
|
||||
// if d.lo == 0 {
|
||||
// if d.hi == 0 {
|
||||
// return (n.hi / d.lo) as u128, (n.hi % d.lo) as u128
|
||||
// }
|
||||
// if n.lo == 0 {
|
||||
// r.hi = n.hi % d.hi
|
||||
// r.lo = 0
|
||||
// return (n.hi / d.hi) as u128, r.all
|
||||
// }
|
||||
// if (d.hi & (d.hi-1)) == 0 {
|
||||
// r.lo = n.lo
|
||||
// r.hi = n.hi & (d.hi-1)
|
||||
// return (n.hi >> ctz(d.hi)) as u128, r.all
|
||||
// }
|
||||
|
||||
sr = clz(d.hi) - clz(n.hi)
|
||||
if sr > 64 - 2 {
|
||||
return 0, n.all
|
||||
}
|
||||
sr++
|
||||
q.lo = 0
|
||||
q.hi = n.lo << (64-sr)
|
||||
r.hi = n.hi >> sr
|
||||
r.lo = (n.hi << (64-sr)) | (n.lo >> sr)
|
||||
} else {
|
||||
if d.hi == 0 {
|
||||
if (d.lo & (d.lo - 1)) == 0 {
|
||||
rem := (n.lo % (d.lo - 1)) as u128
|
||||
if d.lo == 1 {
|
||||
return n.all, rem
|
||||
}
|
||||
sr = ctz(d.lo)
|
||||
q.hi = n.hi >> sr
|
||||
q.lo = (n.hi << (64-sr)) | (n.lo >> sr);
|
||||
return q.all, rem
|
||||
}
|
||||
// sr = clz(d.hi) - clz(n.hi)
|
||||
// if sr > 64 - 2 {
|
||||
// return 0, n.all
|
||||
// }
|
||||
// sr++
|
||||
// q.lo = 0
|
||||
// q.hi = n.lo << (64-sr)
|
||||
// r.hi = n.hi >> sr
|
||||
// r.lo = (n.hi << (64-sr)) | (n.lo >> sr)
|
||||
// } else {
|
||||
// if d.hi == 0 {
|
||||
// if (d.lo & (d.lo - 1)) == 0 {
|
||||
// var rem := (n.lo % (d.lo - 1)) as u128
|
||||
// if d.lo == 1 {
|
||||
// return n.all, rem
|
||||
// }
|
||||
// sr = ctz(d.lo)
|
||||
// q.hi = n.hi >> sr
|
||||
// q.lo = (n.hi << (64-sr)) | (n.lo >> sr)
|
||||
// return q.all, rem
|
||||
// }
|
||||
|
||||
sr = 1 + 64 + clz(d.lo) - clz(n.hi)
|
||||
// sr = 1 + 64 + clz(d.lo) - clz(n.hi)
|
||||
|
||||
q.all = n.all << (128-sr)
|
||||
r.all = n.all >> sr
|
||||
if sr == 64 {
|
||||
q.lo = 0
|
||||
q.hi = n.lo
|
||||
r.hi = 0
|
||||
r.lo = n.hi
|
||||
} else if sr < 64 {
|
||||
q.lo = 0
|
||||
q.hi = n.lo << (64-sr)
|
||||
r.hi = n.hi >> sr
|
||||
r.lo = (n.hi << (64-sr)) | (n.lo >> sr)
|
||||
} else {
|
||||
q.lo = n.lo << (128-sr)
|
||||
q.hi = (n.hi << (128-sr)) | (n.lo >> (sr-64))
|
||||
r.hi = 0
|
||||
r.lo = n.hi >> (sr-64)
|
||||
}
|
||||
} else {
|
||||
sr = clz(d.hi) - clz(n.hi)
|
||||
if sr > 64-1 {
|
||||
return 0, n.all
|
||||
}
|
||||
sr++
|
||||
q.lo = 0
|
||||
q.hi = n.lo << (64-sr)
|
||||
r.all = n.all >> sr
|
||||
if sr < 64 {
|
||||
r.hi = n.hi >> sr
|
||||
r.lo = (n.hi << (64-sr)) | (n.lo >> sr)
|
||||
} else {
|
||||
r.hi = 0
|
||||
r.lo = n.hi
|
||||
}
|
||||
}
|
||||
}
|
||||
// q.all = n.all << (128-sr)
|
||||
// r.all = n.all >> sr
|
||||
// if sr == 64 {
|
||||
// q.lo = 0
|
||||
// q.hi = n.lo
|
||||
// r.hi = 0
|
||||
// r.lo = n.hi
|
||||
// } else if sr < 64 {
|
||||
// q.lo = 0
|
||||
// q.hi = n.lo << (64-sr)
|
||||
// r.hi = n.hi >> sr
|
||||
// r.lo = (n.hi << (64-sr)) | (n.lo >> sr)
|
||||
// } else {
|
||||
// q.lo = n.lo << (128-sr)
|
||||
// q.hi = (n.hi << (128-sr)) | (n.lo >> (sr-64))
|
||||
// r.hi = 0
|
||||
// r.lo = n.hi >> (sr-64)
|
||||
// }
|
||||
// } else {
|
||||
// sr = clz(d.hi) - clz(n.hi)
|
||||
// if sr > 64-1 {
|
||||
// return 0, n.all
|
||||
// }
|
||||
// sr++
|
||||
// q.lo = 0
|
||||
// q.hi = n.lo << (64-sr)
|
||||
// r.all = n.all >> sr
|
||||
// if sr < 64 {
|
||||
// r.hi = n.hi >> sr
|
||||
// r.lo = (n.hi << (64-sr)) | (n.lo >> sr)
|
||||
// } else {
|
||||
// r.hi = 0
|
||||
// r.lo = n.hi
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
carry: u64
|
||||
for ; sr > 0; sr-- {
|
||||
r.hi = (r.hi << 1) | (r.lo >> (64-1))
|
||||
r.lo = (r.lo << 1) | (r.hi >> (64-1))
|
||||
q.hi = (q.hi << 1) | (q.lo >> (64-1))
|
||||
q.lo = (q.lo << 1) | carry
|
||||
// carry: u64
|
||||
// for ; sr > 0; sr-- {
|
||||
// r.hi = (r.hi << 1) | (r.lo >> (64-1))
|
||||
// r.lo = (r.lo << 1) | (r.hi >> (64-1))
|
||||
// q.hi = (q.hi << 1) | (q.lo >> (64-1))
|
||||
// q.lo = (q.lo << 1) | carry
|
||||
|
||||
carry = 0
|
||||
if r.all >= d.all {
|
||||
r.all -= d.all
|
||||
carry = 1
|
||||
}
|
||||
}
|
||||
// carry = 0
|
||||
// if r.all >= d.all {
|
||||
// r.all -= d.all
|
||||
// carry = 1
|
||||
// }
|
||||
// }
|
||||
|
||||
// q.all = (q.all << 1) | (carry as u128)
|
||||
// return q.all, r.all
|
||||
// }
|
||||
|
||||
q.all = (q.all << 1) | (carry as u128)
|
||||
return q.all, r.all
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
// TODO(bill): Use assembly instead here to implement atomics
|
||||
// Inline vs external file?
|
||||
|
||||
#import win32 "sys/windows.odin" when ODIN_OS == "windows";
|
||||
_ := compile_assert(ODIN_ARCH == "amd64"); // TODO(bill): x86 version
|
||||
|
||||
|
||||
yield_thread :: proc() { win32._mm_pause(); }
|
||||
mfence :: proc() { win32.ReadWriteBarrier(); }
|
||||
sfence :: proc() { win32.WriteBarrier(); }
|
||||
lfence :: proc() { win32.ReadBarrier(); }
|
||||
|
||||
|
||||
load :: proc(a: ^i32) -> i32 {
|
||||
return a^;
|
||||
}
|
||||
store :: proc(a: ^i32, value: i32) {
|
||||
a^ = value;
|
||||
}
|
||||
compare_exchange :: proc(a: ^i32, expected, desired: i32) -> i32 {
|
||||
return win32.InterlockedCompareExchange(a, desired, expected);
|
||||
}
|
||||
exchanged :: proc(a: ^i32, desired: i32) -> i32 {
|
||||
return win32.InterlockedExchange(a, desired);
|
||||
}
|
||||
fetch_add :: proc(a: ^i32, operand: i32) -> i32 {
|
||||
return win32.InterlockedExchangeAdd(a, operand);
|
||||
|
||||
}
|
||||
fetch_and :: proc(a: ^i32, operand: i32) -> i32 {
|
||||
return win32.InterlockedAnd(a, operand);
|
||||
}
|
||||
fetch_or :: proc(a: ^i32, operand: i32) -> i32 {
|
||||
return win32.InterlockedOr(a, operand);
|
||||
}
|
||||
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) {
|
||||
counter += 1;
|
||||
yield_thread();
|
||||
old_value = compare_exchange(a, 1, 0);
|
||||
mfence();
|
||||
}
|
||||
return old_value == 0;
|
||||
}
|
||||
spin_unlock :: proc(a: ^i32) {
|
||||
store(a, 0);
|
||||
mfence();
|
||||
}
|
||||
try_acquire_lock :: proc(a: ^i32) -> bool {
|
||||
yield_thread();
|
||||
old_value := compare_exchange(a, 1, 0);
|
||||
mfence();
|
||||
return old_value == 0;
|
||||
}
|
||||
|
||||
|
||||
load :: proc(a: ^i64) -> i64 {
|
||||
return a^;
|
||||
}
|
||||
store :: proc(a: ^i64, value: i64) {
|
||||
a^ = value;
|
||||
}
|
||||
compare_exchange :: proc(a: ^i64, expected, desired: i64) -> i64 {
|
||||
return win32.InterlockedCompareExchange64(a, desired, expected);
|
||||
}
|
||||
exchanged :: proc(a: ^i64, desired: i64) -> i64 {
|
||||
return win32.InterlockedExchange64(a, desired);
|
||||
}
|
||||
fetch_add :: proc(a: ^i64, operand: i64) -> i64 {
|
||||
return win32.InterlockedExchangeAdd64(a, operand);
|
||||
}
|
||||
fetch_and :: proc(a: ^i64, operand: i64) -> i64 {
|
||||
return win32.InterlockedAnd64(a, operand);
|
||||
}
|
||||
fetch_or :: proc(a: ^i64, operand: i64) -> i64 {
|
||||
return win32.InterlockedOr64(a, operand);
|
||||
}
|
||||
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) {
|
||||
counter += 1;
|
||||
yield_thread();
|
||||
old_value = compare_exchange(a, 1, 0);
|
||||
mfence();
|
||||
}
|
||||
return old_value == 0;
|
||||
}
|
||||
spin_unlock :: proc(a: ^i64) {
|
||||
store(a, 0);
|
||||
mfence();
|
||||
}
|
||||
try_acquire_lock :: proc(a: ^i64) -> bool {
|
||||
yield_thread();
|
||||
old_value := compare_exchange(a, 1, 0);
|
||||
mfence();
|
||||
return old_value == 0;
|
||||
}
|
||||
+878
-441
File diff suppressed because it is too large
Load Diff
+105
-105
@@ -1,164 +1,164 @@
|
||||
crc32 :: proc(data: rawptr, len: int) -> u32 {
|
||||
result := ~(0 as u32)
|
||||
s := slice_ptr(data as ^u8, len)
|
||||
for i := 0; i < len; i++ {
|
||||
b := s[i] as u32
|
||||
result = result>>8 ~ __CRC32_TABLE[(result ~ b) & 0xff]
|
||||
result := ~cast(u32)0;
|
||||
s := slice_ptr(cast(^u8)data, len);
|
||||
for i in 0..<len {
|
||||
b := cast(u32)s[i];
|
||||
result = result>>8 ~ __CRC32_TABLE[(result ~ b) & 0xff];
|
||||
}
|
||||
return ~result
|
||||
return ~result;
|
||||
}
|
||||
crc64 :: proc(data: rawptr, len: int) -> u64 {
|
||||
result := ~(0 as u64)
|
||||
s := slice_ptr(data as ^u8, len)
|
||||
for i := 0; i < len; i++ {
|
||||
b := s[i] as u64
|
||||
result = result>>8 ~ __CRC64_TABLE[(result ~ b) & 0xff]
|
||||
result := ~cast(u64)0;
|
||||
s := slice_ptr(cast(^u8)data, len);
|
||||
for i in 0..<len {
|
||||
b := cast(u64)s[i];
|
||||
result = result>>8 ~ __CRC64_TABLE[(result ~ b) & 0xff];
|
||||
}
|
||||
return ~result
|
||||
return ~result;
|
||||
}
|
||||
|
||||
fnv32 :: proc(data: rawptr, len: int) -> u32 {
|
||||
s := slice_ptr(data as ^u8, len)
|
||||
s := slice_ptr(cast(^u8)data, len);
|
||||
|
||||
h: u32 = 0x811c9dc5
|
||||
for i := 0; i < len; i++ {
|
||||
h = (h * 0x01000193) ~ s[i] as u32
|
||||
h: u32 = 0x811c9dc5;
|
||||
for i in 0..<len {
|
||||
h = (h * 0x01000193) ~ cast(u32)s[i];
|
||||
}
|
||||
return h
|
||||
return h;
|
||||
}
|
||||
|
||||
fnv64 :: proc(data: rawptr, len: int) -> u64 {
|
||||
s := slice_ptr(data as ^u8, len)
|
||||
s := slice_ptr(cast(^u8)data, len);
|
||||
|
||||
h: u64 = 0xcbf29ce484222325
|
||||
for i := 0; i < len; i++ {
|
||||
h = (h * 0x100000001b3) ~ s[i] as u64
|
||||
h: u64 = 0xcbf29ce484222325;
|
||||
for i in 0..<len {
|
||||
h = (h * 0x100000001b3) ~ cast(u64)s[i];
|
||||
}
|
||||
return h
|
||||
return h;
|
||||
}
|
||||
|
||||
fnv32a :: proc(data: rawptr, len: int) -> u32 {
|
||||
s := slice_ptr(data as ^u8, len)
|
||||
s := slice_ptr(cast(^u8)data, len);
|
||||
|
||||
h: u32 = 0x811c9dc5
|
||||
for i := 0; i < len; i++ {
|
||||
h = (h ~ s[i] as u32) * 0x01000193
|
||||
h: u32 = 0x811c9dc5;
|
||||
for i in 0..<len {
|
||||
h = (h ~ cast(u32)s[i]) * 0x01000193;
|
||||
}
|
||||
return h
|
||||
return h;
|
||||
}
|
||||
|
||||
fnv64a :: proc(data: rawptr, len: int) -> u64 {
|
||||
s := slice_ptr(data as ^u8, len)
|
||||
s := slice_ptr(cast(^u8)data, len);
|
||||
|
||||
h: u64 = 0xcbf29ce484222325
|
||||
for i := 0; i < len; i++ {
|
||||
h = (h ~ s[i] as u64) * 0x100000001b3
|
||||
h :u64 = 0xcbf29ce484222325;
|
||||
for i in 0..<len {
|
||||
h = (h ~ cast(u64)s[i]) * 0x100000001b3;
|
||||
}
|
||||
return h
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
murmur64 :: proc(data_: rawptr, len: int) -> u64 {
|
||||
SEED :: 0x9747b28c
|
||||
SEED :: 0x9747b28c;
|
||||
|
||||
if size_of(int) == 8 {
|
||||
m :: 0xc6a4a7935bd1e995
|
||||
r :: 47
|
||||
when size_of(int) == 8 {
|
||||
m :: 0xc6a4a7935bd1e995;
|
||||
r :: 47;
|
||||
|
||||
h: u64 = SEED ~ (len as u64 * m)
|
||||
h: u64 = SEED ~ (cast(u64)len * m);
|
||||
|
||||
data := slice_ptr(data_ as ^u64, len/size_of(u64))
|
||||
data2 := slice_ptr(data_ as ^u8, len)
|
||||
data := slice_ptr(cast(^u64)data_, len/size_of(u64));
|
||||
data2 := slice_ptr(cast(^u8)data_, len);
|
||||
|
||||
for i := 0; i < data.count; i++ {
|
||||
k := data[i]
|
||||
for i in 0 ..< data.count {
|
||||
k := data[i];
|
||||
|
||||
k *= m
|
||||
k ~= k>>r
|
||||
k *= m
|
||||
k *= m;
|
||||
k ~= k>>r;
|
||||
k *= m;
|
||||
|
||||
h ~= k
|
||||
h *= m
|
||||
h ~= k;
|
||||
h *= m;
|
||||
}
|
||||
|
||||
match len & 7 {
|
||||
case 7: h ~= data2[6] as u64 << 48; fallthrough
|
||||
case 6: h ~= data2[5] as u64 << 40; fallthrough
|
||||
case 5: h ~= data2[4] as u64 << 32; fallthrough
|
||||
case 4: h ~= data2[3] as u64 << 24; fallthrough
|
||||
case 3: h ~= data2[2] as u64 << 16; fallthrough
|
||||
case 2: h ~= data2[1] as u64 << 8; fallthrough
|
||||
case 7: h ~= cast(u64)data2[6] << 48; fallthrough;
|
||||
case 6: h ~= cast(u64)data2[5] << 40; fallthrough;
|
||||
case 5: h ~= cast(u64)data2[4] << 32; fallthrough;
|
||||
case 4: h ~= cast(u64)data2[3] << 24; fallthrough;
|
||||
case 3: h ~= cast(u64)data2[2] << 16; fallthrough;
|
||||
case 2: h ~= cast(u64)data2[1] << 8; fallthrough;
|
||||
case 1:
|
||||
h ~= data2[0] as u64
|
||||
h *= m
|
||||
h ~= cast(u64)data2[0];
|
||||
h *= m;
|
||||
}
|
||||
|
||||
h ~= h>>r
|
||||
h *= m
|
||||
h ~= h>>r
|
||||
h ~= h>>r;
|
||||
h *= m;
|
||||
h ~= h>>r;
|
||||
|
||||
return h
|
||||
return h;
|
||||
} else {
|
||||
m :: 0x5bd1e995
|
||||
r :: 24
|
||||
m :: 0x5bd1e995;
|
||||
r :: 24;
|
||||
|
||||
h1: u32 = SEED as u32 ~ len as u32
|
||||
h2: u32 = SEED >> 32
|
||||
h1: u32 = cast(u32)SEED ~ cast(u32)len;
|
||||
h2: u32 = SEED >> 32;
|
||||
|
||||
data := slice_ptr(data_ as ^u32, len/size_of(u32))
|
||||
data := slice_ptr(cast(^u32)data_, len/size_of(u32));
|
||||
|
||||
i := 0
|
||||
i := 0;
|
||||
for len >= 8 {
|
||||
k1, k2: u32
|
||||
k1 = data[i]; i++
|
||||
k1 *= m
|
||||
k1 ~= k1>>r
|
||||
k1 *= m
|
||||
h1 *= m
|
||||
h1 ~= k1
|
||||
len -= 4
|
||||
k1, k2: u32;
|
||||
k1 = data[i]; i += 1;
|
||||
k1 *= m;
|
||||
k1 ~= k1>>r;
|
||||
k1 *= m;
|
||||
h1 *= m;
|
||||
h1 ~= k1;
|
||||
len -= 4;
|
||||
|
||||
k2 = data[i]; i++
|
||||
k2 *= m
|
||||
k2 ~= k2>>r
|
||||
k2 *= m
|
||||
h2 *= m
|
||||
h2 ~= k2
|
||||
len -= 4
|
||||
k2 = data[i]; i += 1;
|
||||
k2 *= m;
|
||||
k2 ~= k2>>r;
|
||||
k2 *= m;
|
||||
h2 *= m;
|
||||
h2 ~= k2;
|
||||
len -= 4;
|
||||
}
|
||||
|
||||
if (len >= 4) {
|
||||
k1: u32
|
||||
k1 = data[i]; i++
|
||||
k1 *= m
|
||||
k1 ~= k1>>r
|
||||
k1 *= m
|
||||
h1 *= m
|
||||
h1 ~= k1
|
||||
len -= 4
|
||||
if len >= 4 {
|
||||
k1: u32;
|
||||
k1 = data[i]; i += 1;
|
||||
k1 *= m;
|
||||
k1 ~= k1>>r;
|
||||
k1 *= m;
|
||||
h1 *= m;
|
||||
h1 ~= k1;
|
||||
len -= 4;
|
||||
}
|
||||
|
||||
data8 := slice_ptr((data.data+i) as ^u8, 3) // NOTE(bill): This is unsafe
|
||||
data8 := slice_ptr(cast(^u8)(data.data+i), 3); // NOTE(bill): This is unsafe
|
||||
|
||||
match len {
|
||||
case 3: h2 ~= data8[2] as u32 << 16; fallthrough
|
||||
case 2: h2 ~= data8[1] as u32 << 8; fallthrough
|
||||
case 3: h2 ~= cast(u32)data8[2] << 16; fallthrough;
|
||||
case 2: h2 ~= cast(u32)data8[1] << 8; fallthrough;
|
||||
case 1:
|
||||
h2 ~= data8[0] as u32
|
||||
h2 *= m
|
||||
h2 ~= cast(u32)data8[0];
|
||||
h2 *= m;
|
||||
}
|
||||
|
||||
h1 ~= h2>>18
|
||||
h1 *= m
|
||||
h2 ~= h1>>22
|
||||
h2 *= m
|
||||
h1 ~= h2>>17
|
||||
h1 *= m
|
||||
h2 ~= h1>>19
|
||||
h2 *= m
|
||||
h1 ~= h2>>18;
|
||||
h1 *= m;
|
||||
h2 ~= h1>>22;
|
||||
h2 *= m;
|
||||
h1 ~= h2>>17;
|
||||
h1 *= m;
|
||||
h2 ~= h1>>19;
|
||||
h2 *= m;
|
||||
|
||||
h := (h1 as u64)<<32 | h2 as u64
|
||||
return h
|
||||
h := cast(u64)(h1)<<32 | cast(u64)(h2);
|
||||
return h;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,7 +229,7 @@ __CRC32_TABLE := [256]u32{
|
||||
0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
|
||||
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
|
||||
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
|
||||
}
|
||||
};
|
||||
__CRC64_TABLE := [256]u64{
|
||||
0x0000000000000000, 0x42f0e1eba9ea3693, 0x85e1c3d753d46d26, 0xc711223cfa3e5bb5,
|
||||
0x493366450e42ecdf, 0x0bc387aea7a8da4c, 0xccd2a5925d9681f9, 0x8e224479f47cb76a,
|
||||
@@ -295,4 +295,4 @@ __CRC64_TABLE := [256]u64{
|
||||
0xcf8b0890283e370c, 0x8d7be97b81d4019f, 0x4a6acb477bea5a2a, 0x089a2aacd2006cb9,
|
||||
0x14dea25f3af9026d, 0x562e43b4931334fe, 0x913f6188692d6f4b, 0xd3cf8063c0c759d8,
|
||||
0x5dedc41a34bbeeb2, 0x1f1d25f19d51d821, 0xd80c07cd676f8394, 0x9afce626ce85b507,
|
||||
}
|
||||
};
|
||||
|
||||
+255
-252
@@ -1,141 +1,146 @@
|
||||
TAU :: 6.28318530717958647692528676655900576
|
||||
PI :: 3.14159265358979323846264338327950288
|
||||
ONE_OVER_TAU :: 0.636619772367581343075535053490057448
|
||||
ONE_OVER_PI :: 0.159154943091895335768883763372514362
|
||||
TAU :: 6.28318530717958647692528676655900576;
|
||||
PI :: 3.14159265358979323846264338327950288;
|
||||
ONE_OVER_TAU :: 0.636619772367581343075535053490057448;
|
||||
ONE_OVER_PI :: 0.159154943091895335768883763372514362;
|
||||
|
||||
E :: 2.71828182845904523536
|
||||
SQRT_TWO :: 1.41421356237309504880168872420969808
|
||||
SQRT_THREE :: 1.73205080756887729352744634150587236
|
||||
SQRT_FIVE :: 2.23606797749978969640917366873127623
|
||||
E :: 2.71828182845904523536;
|
||||
SQRT_TWO :: 1.41421356237309504880168872420969808;
|
||||
SQRT_THREE :: 1.73205080756887729352744634150587236;
|
||||
SQRT_FIVE :: 2.23606797749978969640917366873127623;
|
||||
|
||||
LOG_TWO :: 0.693147180559945309417232121458176568
|
||||
LOG_TEN :: 2.30258509299404568401799145468436421
|
||||
LOG_TWO :: 0.693147180559945309417232121458176568;
|
||||
LOG_TEN :: 2.30258509299404568401799145468436421;
|
||||
|
||||
EPSILON :: 1.19209290e-7
|
||||
EPSILON :: 1.19209290e-7;
|
||||
|
||||
τ :: TAU
|
||||
π :: PI
|
||||
τ :: TAU;
|
||||
π :: PI;
|
||||
|
||||
Vec2 :: [vector 2]f32;
|
||||
Vec3 :: [vector 3]f32;
|
||||
Vec4 :: [vector 4]f32;
|
||||
|
||||
Mat2 :: [2]Vec2;
|
||||
Mat3 :: [3]Vec3;
|
||||
Mat4 :: [4]Vec4;
|
||||
|
||||
sqrt :: proc(x: f32) -> f32 #foreign __llvm_core "llvm.sqrt.f32";
|
||||
sqrt :: proc(x: f64) -> f64 #foreign __llvm_core "llvm.sqrt.f64";
|
||||
|
||||
sin :: proc(x: f32) -> f32 #foreign __llvm_core "llvm.sin.f32";
|
||||
sin :: proc(x: f64) -> f64 #foreign __llvm_core "llvm.sin.f64";
|
||||
|
||||
cos :: proc(x: f32) -> f32 #foreign __llvm_core "llvm.cos.f32";
|
||||
cos :: proc(x: f64) -> f64 #foreign __llvm_core "llvm.cos.f64";
|
||||
|
||||
tan :: proc(x: f32) -> f32 #inline { return sin(x)/cos(x); }
|
||||
tan :: proc(x: f64) -> f64 #inline { return sin(x)/cos(x); }
|
||||
|
||||
lerp :: proc(a, b, t: f32) -> f32 { return a*(1-t) + b*t; }
|
||||
lerp :: proc(a, b, t: f64) -> f64 { return a*(1-t) + b*t; }
|
||||
|
||||
sign :: proc(x: f32) -> f32 { if x >= 0 { return +1; } return -1; }
|
||||
sign :: proc(x: f64) -> f64 { if x >= 0 { return +1; } return -1; }
|
||||
|
||||
bit_reverse :: proc(b: u16) -> u16 #foreign __llvm_core "llvm.bitreverse.i16";
|
||||
bit_reverse :: proc(b: u32) -> u32 #foreign __llvm_core "llvm.bitreverse.i32";
|
||||
bit_reverse :: proc(b: u64) -> u64 #foreign __llvm_core "llvm.bitreverse.i64";
|
||||
|
||||
byte_swap :: proc(b: u16) -> u16 #foreign __llvm_core "llvm.bswap.i16";
|
||||
byte_swap :: proc(b: u32) -> u32 #foreign __llvm_core "llvm.bswap.i32";
|
||||
byte_swap :: proc(b: u64) -> u64 #foreign __llvm_core "llvm.bswap.i64";
|
||||
|
||||
fmuladd :: proc(a, b, c: f32) -> f32 #foreign __llvm_core "llvm.fmuladd.f32";
|
||||
fmuladd :: proc(a, b, c: f64) -> f64 #foreign __llvm_core "llvm.fmuladd.f64";
|
||||
|
||||
|
||||
Vec2 :: type {2}f32
|
||||
Vec3 :: type {3}f32
|
||||
Vec4 :: type {4}f32
|
||||
|
||||
Mat2 :: type [2]Vec2
|
||||
Mat3 :: type [3]Vec3
|
||||
Mat4 :: type [4]Vec4
|
||||
|
||||
|
||||
sqrt32 :: proc(x: f32) -> f32 #foreign "llvm.sqrt.f32"
|
||||
sqrt64 :: proc(x: f64) -> f64 #foreign "llvm.sqrt.f64"
|
||||
|
||||
sin32 :: proc(x: f32) -> f32 #foreign "llvm.sin.f32"
|
||||
sin64 :: proc(x: f64) -> f64 #foreign "llvm.sin.f64"
|
||||
|
||||
cos32 :: proc(x: f32) -> f32 #foreign "llvm.cos.f32"
|
||||
cos64 :: proc(x: f64) -> f64 #foreign "llvm.cos.f64"
|
||||
|
||||
tan32 :: proc(x: f32) -> f32 #inline { return sin32(x)/cos32(x) }
|
||||
tan64 :: proc(x: f64) -> f64 #inline { return sin64(x)/cos64(x) }
|
||||
|
||||
lerp32 :: proc(a, b, t: f32) -> f32 { return a*(1-t) + b*t }
|
||||
lerp64 :: proc(a, b, t: f64) -> f64 { return a*(1-t) + b*t }
|
||||
|
||||
clamp32 :: proc(x, lower, upper: f32) -> f32 { return min(max(x, lower), upper) }
|
||||
clamp64 :: proc(x, lower, upper: f64) -> f64 { return min(max(x, lower), upper) }
|
||||
|
||||
sign32 :: proc(x: f32) -> f32 { if x >= 0 { return +1 } return -1 }
|
||||
sign64 :: proc(x: f64) -> f64 { if x >= 0 { return +1 } return -1 }
|
||||
|
||||
|
||||
|
||||
copy_sign32 :: proc(x, y: f32) -> f32 {
|
||||
ix := x transmute u32
|
||||
iy := y transmute u32
|
||||
ix &= 0x7fffffff
|
||||
ix |= iy & 0x80000000
|
||||
return ix transmute f32
|
||||
copy_sign :: proc(x, y: f32) -> f32 {
|
||||
ix := transmute(u32)x;
|
||||
iy := transmute(u32)y;
|
||||
ix &= 0x7fffffff;
|
||||
ix |= iy & 0x80000000;
|
||||
return transmute(f32)ix;
|
||||
}
|
||||
round32 :: proc(x: f32) -> f32 {
|
||||
round :: proc(x: f32) -> f32 {
|
||||
if x >= 0 {
|
||||
return floor32(x + 0.5)
|
||||
return floor(x + 0.5);
|
||||
}
|
||||
return ceil32(x - 0.5)
|
||||
return ceil(x - 0.5);
|
||||
}
|
||||
floor32 :: proc(x: f32) -> f32 {
|
||||
floor :: proc(x: f32) -> f32 {
|
||||
if x >= 0 {
|
||||
return x as int as f32
|
||||
return cast(f32)cast(int)x;
|
||||
}
|
||||
return (x-0.5) as int as f32
|
||||
return cast(f32)cast(int)(x-0.5);
|
||||
}
|
||||
ceil32 :: proc(x: f32) -> f32 {
|
||||
ceil :: proc(x: f32) -> f32 {
|
||||
if x < 0 {
|
||||
return x as int as f32
|
||||
return cast(f32)cast(int)x;
|
||||
}
|
||||
return ((x as int)+1) as f32
|
||||
return cast(f32)cast(int)(x+1);
|
||||
}
|
||||
|
||||
remainder32 :: proc(x, y: f32) -> f32 {
|
||||
return x - round32(x/y) * y
|
||||
return x - round(x/y) * y;
|
||||
}
|
||||
|
||||
fmod32 :: proc(x, y: f32) -> f32 {
|
||||
y = abs(y)
|
||||
result := remainder32(abs(x), y)
|
||||
if sign32(result) < 0 {
|
||||
result += y
|
||||
y = abs(y);
|
||||
result := remainder32(abs(x), y);
|
||||
if sign(result) < 0 {
|
||||
result += y;
|
||||
}
|
||||
return copy_sign32(result, x)
|
||||
return copy_sign(result, x);
|
||||
}
|
||||
|
||||
|
||||
to_radians :: proc(degrees: f32) -> f32 { return degrees * TAU / 360 }
|
||||
to_degrees :: proc(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; }
|
||||
|
||||
|
||||
|
||||
|
||||
dot2 :: proc(a, b: Vec2) -> f32 { c := a*b; return c.x + c.y }
|
||||
dot3 :: proc(a, b: Vec3) -> f32 { c := a*b; return c.x + c.y + c.z }
|
||||
dot4 :: proc(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; }
|
||||
|
||||
cross3 :: 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
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
vec2_mag :: proc(v: Vec2) -> f32 { return sqrt32(dot2(v, v)) }
|
||||
vec3_mag :: proc(v: Vec3) -> f32 { return sqrt32(dot3(v, v)) }
|
||||
vec4_mag :: proc(v: Vec4) -> f32 { return sqrt32(dot4(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)); }
|
||||
|
||||
vec2_norm :: proc(v: Vec2) -> Vec2 { return v / Vec2{vec2_mag(v)} }
|
||||
vec3_norm :: proc(v: Vec3) -> Vec3 { return v / Vec3{vec3_mag(v)} }
|
||||
vec4_norm :: proc(v: Vec4) -> Vec4 { return v / Vec4{vec4_mag(v)} }
|
||||
norm :: proc(v: Vec2) -> Vec2 { return v / Vec2{mag(v)}; }
|
||||
norm :: proc(v: Vec3) -> Vec3 { return v / Vec3{mag(v)}; }
|
||||
norm :: proc(v: Vec4) -> Vec4 { return v / Vec4{mag(v)}; }
|
||||
|
||||
vec2_norm0 :: proc(v: Vec2) -> Vec2 {
|
||||
m := vec2_mag(v)
|
||||
norm0 :: proc(v: Vec2) -> Vec2 {
|
||||
m := mag(v);
|
||||
if m == 0 {
|
||||
return Vec2{0}
|
||||
return Vec2{0};
|
||||
}
|
||||
return v / Vec2{m}
|
||||
return v / Vec2{m};
|
||||
}
|
||||
|
||||
vec3_norm0 :: proc(v: Vec3) -> Vec3 {
|
||||
m := vec3_mag(v)
|
||||
norm0 :: proc(v: Vec3) -> Vec3 {
|
||||
m := mag(v);
|
||||
if m == 0 {
|
||||
return Vec3{0}
|
||||
return Vec3{0};
|
||||
}
|
||||
return v / Vec3{m}
|
||||
return v / Vec3{m};
|
||||
}
|
||||
|
||||
vec4_norm0 :: proc(v: Vec4) -> Vec4 {
|
||||
m := vec4_mag(v)
|
||||
norm0 :: proc(v: Vec4) -> Vec4 {
|
||||
m := mag(v);
|
||||
if m == 0 {
|
||||
return Vec4{0}
|
||||
return Vec4{0};
|
||||
}
|
||||
return v / Vec4{m}
|
||||
return v / Vec4{m};
|
||||
}
|
||||
|
||||
|
||||
@@ -146,228 +151,226 @@ mat4_identity :: proc() -> Mat4 {
|
||||
{0, 1, 0, 0},
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 0, 1},
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
mat4_transpose :: proc(m: Mat4) -> Mat4 {
|
||||
for j := 0; j < 4; j++ {
|
||||
for i := 0; i < 4; i++ {
|
||||
m[i][j], m[j][i] = m[j][i], m[i][j]
|
||||
for j in 0..<4 {
|
||||
for i in 0..<4 {
|
||||
m[i][j], m[j][i] = m[j][i], m[i][j];
|
||||
}
|
||||
}
|
||||
return m
|
||||
return m;
|
||||
}
|
||||
|
||||
mat4_mul :: proc(a, b: Mat4) -> Mat4 {
|
||||
c: Mat4
|
||||
for j := 0; j < 4; j++ {
|
||||
for i := 0; i < 4; i++ {
|
||||
c[j][i] = a[0][i]*b[j][0]
|
||||
+ a[1][i]*b[j][1]
|
||||
+ a[2][i]*b[j][2]
|
||||
+ a[3][i]*b[j][3]
|
||||
mul :: proc(a, b: Mat4) -> Mat4 {
|
||||
c: Mat4;
|
||||
for j in 0..<4 {
|
||||
for i in 0..<4 {
|
||||
c[j][i] = a[0][i]*b[j][0] +
|
||||
a[1][i]*b[j][1] +
|
||||
a[2][i]*b[j][2] +
|
||||
a[3][i]*b[j][3];
|
||||
}
|
||||
}
|
||||
return c
|
||||
return c;
|
||||
}
|
||||
|
||||
mat4_mul_vec4 :: proc(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,
|
||||
m[0][2]*v.x + m[1][2]*v.y + m[2][2]*v.z + m[3][2]*v.w,
|
||||
m[0][3]*v.x + m[1][3]*v.y + m[2][3]*v.z + m[3][3]*v.w,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
mat4_inverse :: proc(m: Mat4) -> Mat4 {
|
||||
o: Mat4
|
||||
inverse :: proc(m: Mat4) -> Mat4 {
|
||||
o: Mat4;
|
||||
|
||||
sf00 := m[2][2] * m[3][3] - m[3][2] * m[2][3]
|
||||
sf01 := m[2][1] * m[3][3] - m[3][1] * m[2][3]
|
||||
sf02 := m[2][1] * m[3][2] - m[3][1] * m[2][2]
|
||||
sf03 := m[2][0] * m[3][3] - m[3][0] * m[2][3]
|
||||
sf04 := m[2][0] * m[3][2] - m[3][0] * m[2][2]
|
||||
sf05 := m[2][0] * m[3][1] - m[3][0] * m[2][1]
|
||||
sf06 := m[1][2] * m[3][3] - m[3][2] * m[1][3]
|
||||
sf07 := m[1][1] * m[3][3] - m[3][1] * m[1][3]
|
||||
sf08 := m[1][1] * m[3][2] - m[3][1] * m[1][2]
|
||||
sf09 := m[1][0] * m[3][3] - m[3][0] * m[1][3]
|
||||
sf10 := m[1][0] * m[3][2] - m[3][0] * m[1][2]
|
||||
sf11 := m[1][1] * m[3][3] - m[3][1] * m[1][3]
|
||||
sf12 := m[1][0] * m[3][1] - m[3][0] * m[1][1]
|
||||
sf13 := m[1][2] * m[2][3] - m[2][2] * m[1][3]
|
||||
sf14 := m[1][1] * m[2][3] - m[2][1] * m[1][3]
|
||||
sf15 := m[1][1] * m[2][2] - m[2][1] * m[1][2]
|
||||
sf16 := m[1][0] * m[2][3] - m[2][0] * m[1][3]
|
||||
sf17 := m[1][0] * m[2][2] - m[2][0] * m[1][2]
|
||||
sf18 := m[1][0] * m[2][1] - m[2][0] * m[1][1]
|
||||
sf00 := m[2][2] * m[3][3] - m[3][2] * m[2][3];
|
||||
sf01 := m[2][1] * m[3][3] - m[3][1] * m[2][3];
|
||||
sf02 := m[2][1] * m[3][2] - m[3][1] * m[2][2];
|
||||
sf03 := m[2][0] * m[3][3] - m[3][0] * m[2][3];
|
||||
sf04 := m[2][0] * m[3][2] - m[3][0] * m[2][2];
|
||||
sf05 := m[2][0] * m[3][1] - m[3][0] * m[2][1];
|
||||
sf06 := m[1][2] * m[3][3] - m[3][2] * m[1][3];
|
||||
sf07 := m[1][1] * m[3][3] - m[3][1] * m[1][3];
|
||||
sf08 := m[1][1] * m[3][2] - m[3][1] * m[1][2];
|
||||
sf09 := m[1][0] * m[3][3] - m[3][0] * m[1][3];
|
||||
sf10 := m[1][0] * m[3][2] - m[3][0] * m[1][2];
|
||||
sf11 := m[1][1] * m[3][3] - m[3][1] * m[1][3];
|
||||
sf12 := m[1][0] * m[3][1] - m[3][0] * m[1][1];
|
||||
sf13 := m[1][2] * m[2][3] - m[2][2] * m[1][3];
|
||||
sf14 := m[1][1] * m[2][3] - m[2][1] * m[1][3];
|
||||
sf15 := m[1][1] * m[2][2] - m[2][1] * m[1][2];
|
||||
sf16 := m[1][0] * m[2][3] - m[2][0] * m[1][3];
|
||||
sf17 := m[1][0] * m[2][2] - m[2][0] * m[1][2];
|
||||
sf18 := m[1][0] * m[2][1] - m[2][0] * m[1][1];
|
||||
|
||||
o[0][0] = +(m[1][1] * sf00 - m[1][2] * sf01 + m[1][3] * sf02)
|
||||
o[0][1] = -(m[1][0] * sf00 - m[1][2] * sf03 + m[1][3] * sf04)
|
||||
o[0][2] = +(m[1][0] * sf01 - m[1][1] * sf03 + m[1][3] * sf05)
|
||||
o[0][3] = -(m[1][0] * sf02 - m[1][1] * sf04 + m[1][2] * sf05)
|
||||
o[0][0] = +(m[1][1] * sf00 - m[1][2] * sf01 + m[1][3] * sf02);
|
||||
o[0][1] = -(m[1][0] * sf00 - m[1][2] * sf03 + m[1][3] * sf04);
|
||||
o[0][2] = +(m[1][0] * sf01 - m[1][1] * sf03 + m[1][3] * sf05);
|
||||
o[0][3] = -(m[1][0] * sf02 - m[1][1] * sf04 + m[1][2] * sf05);
|
||||
|
||||
o[1][0] = -(m[0][1] * sf00 - m[0][2] * sf01 + m[0][3] * sf02)
|
||||
o[1][1] = +(m[0][0] * sf00 - m[0][2] * sf03 + m[0][3] * sf04)
|
||||
o[1][2] = -(m[0][0] * sf01 - m[0][1] * sf03 + m[0][3] * sf05)
|
||||
o[1][3] = +(m[0][0] * sf02 - m[0][1] * sf04 + m[0][2] * sf05)
|
||||
o[1][0] = -(m[0][1] * sf00 - m[0][2] * sf01 + m[0][3] * sf02);
|
||||
o[1][1] = +(m[0][0] * sf00 - m[0][2] * sf03 + m[0][3] * sf04);
|
||||
o[1][2] = -(m[0][0] * sf01 - m[0][1] * sf03 + m[0][3] * sf05);
|
||||
o[1][3] = +(m[0][0] * sf02 - m[0][1] * sf04 + m[0][2] * sf05);
|
||||
|
||||
o[2][0] = +(m[0][1] * sf06 - m[0][2] * sf07 + m[0][3] * sf08)
|
||||
o[2][1] = -(m[0][0] * sf06 - m[0][2] * sf09 + m[0][3] * sf10)
|
||||
o[2][2] = +(m[0][0] * sf11 - m[0][1] * sf09 + m[0][3] * sf12)
|
||||
o[2][3] = -(m[0][0] * sf08 - m[0][1] * sf10 + m[0][2] * sf12)
|
||||
o[2][0] = +(m[0][1] * sf06 - m[0][2] * sf07 + m[0][3] * sf08);
|
||||
o[2][1] = -(m[0][0] * sf06 - m[0][2] * sf09 + m[0][3] * sf10);
|
||||
o[2][2] = +(m[0][0] * sf11 - m[0][1] * sf09 + m[0][3] * sf12);
|
||||
o[2][3] = -(m[0][0] * sf08 - m[0][1] * sf10 + m[0][2] * sf12);
|
||||
|
||||
o[3][0] = -(m[0][1] * sf13 - m[0][2] * sf14 + m[0][3] * sf15)
|
||||
o[3][1] = +(m[0][0] * sf13 - m[0][2] * sf16 + m[0][3] * sf17)
|
||||
o[3][2] = -(m[0][0] * sf14 - m[0][1] * sf16 + m[0][3] * sf18)
|
||||
o[3][3] = +(m[0][0] * sf15 - m[0][1] * sf17 + m[0][2] * sf18)
|
||||
o[3][0] = -(m[0][1] * sf13 - m[0][2] * sf14 + m[0][3] * sf15);
|
||||
o[3][1] = +(m[0][0] * sf13 - m[0][2] * sf16 + m[0][3] * sf17);
|
||||
o[3][2] = -(m[0][0] * sf14 - m[0][1] * sf16 + m[0][3] * sf18);
|
||||
o[3][3] = +(m[0][0] * sf15 - m[0][1] * sf17 + m[0][2] * sf18);
|
||||
|
||||
ood := 1.0 / (m[0][0] * o[0][0] +
|
||||
m[0][1] * o[0][1] +
|
||||
m[0][2] * o[0][2] +
|
||||
m[0][3] * o[0][3])
|
||||
m[0][3] * o[0][3]);
|
||||
|
||||
o[0][0] *= ood
|
||||
o[0][1] *= ood
|
||||
o[0][2] *= ood
|
||||
o[0][3] *= ood
|
||||
o[1][0] *= ood
|
||||
o[1][1] *= ood
|
||||
o[1][2] *= ood
|
||||
o[1][3] *= ood
|
||||
o[2][0] *= ood
|
||||
o[2][1] *= ood
|
||||
o[2][2] *= ood
|
||||
o[2][3] *= ood
|
||||
o[3][0] *= ood
|
||||
o[3][1] *= ood
|
||||
o[3][2] *= ood
|
||||
o[3][3] *= ood
|
||||
o[0][0] *= ood;
|
||||
o[0][1] *= ood;
|
||||
o[0][2] *= ood;
|
||||
o[0][3] *= ood;
|
||||
o[1][0] *= ood;
|
||||
o[1][1] *= ood;
|
||||
o[1][2] *= ood;
|
||||
o[1][3] *= ood;
|
||||
o[2][0] *= ood;
|
||||
o[2][1] *= ood;
|
||||
o[2][2] *= ood;
|
||||
o[2][3] *= ood;
|
||||
o[3][0] *= ood;
|
||||
o[3][1] *= ood;
|
||||
o[3][2] *= ood;
|
||||
o[3][3] *= ood;
|
||||
|
||||
return o
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
mat4_translate :: proc(v: Vec3) -> Mat4 {
|
||||
m := mat4_identity()
|
||||
m[3][0] = v.x
|
||||
m[3][1] = v.y
|
||||
m[3][2] = v.z
|
||||
m[3][3] = 1
|
||||
return m
|
||||
m := mat4_identity();
|
||||
m[3][0] = v.x;
|
||||
m[3][1] = v.y;
|
||||
m[3][2] = v.z;
|
||||
m[3][3] = 1;
|
||||
return m;
|
||||
}
|
||||
|
||||
mat4_rotate :: proc(v: Vec3, angle_radians: f32) -> Mat4 {
|
||||
c := cos32(angle_radians)
|
||||
s := sin32(angle_radians)
|
||||
c := cos(angle_radians);
|
||||
s := sin(angle_radians);
|
||||
|
||||
a := vec3_norm(v)
|
||||
t := a * Vec3{1-c}
|
||||
a := norm(v);
|
||||
t := a * Vec3{1-c};
|
||||
|
||||
rot := mat4_identity()
|
||||
rot := mat4_identity();
|
||||
|
||||
rot[0][0] = c + t.x*a.x
|
||||
rot[0][1] = 0 + t.x*a.y + s*a.z
|
||||
rot[0][2] = 0 + t.x*a.z - s*a.y
|
||||
rot[0][3] = 0
|
||||
rot[0][0] = c + t.x*a.x;
|
||||
rot[0][1] = 0 + t.x*a.y + s*a.z;
|
||||
rot[0][2] = 0 + t.x*a.z - s*a.y;
|
||||
rot[0][3] = 0;
|
||||
|
||||
rot[1][0] = 0 + t.y*a.x - s*a.z
|
||||
rot[1][1] = c + t.y*a.y
|
||||
rot[1][2] = 0 + t.y*a.z + s*a.x
|
||||
rot[1][3] = 0
|
||||
rot[1][0] = 0 + t.y*a.x - s*a.z;
|
||||
rot[1][1] = c + t.y*a.y;
|
||||
rot[1][2] = 0 + t.y*a.z + s*a.x;
|
||||
rot[1][3] = 0;
|
||||
|
||||
rot[2][0] = 0 + t.z*a.x + s*a.y
|
||||
rot[2][1] = 0 + t.z*a.y - s*a.x
|
||||
rot[2][2] = c + t.z*a.z
|
||||
rot[2][3] = 0
|
||||
rot[2][0] = 0 + t.z*a.x + s*a.y;
|
||||
rot[2][1] = 0 + t.z*a.y - s*a.x;
|
||||
rot[2][2] = c + t.z*a.z;
|
||||
rot[2][3] = 0;
|
||||
|
||||
return rot
|
||||
return rot;
|
||||
}
|
||||
|
||||
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
|
||||
scale :: proc(m: Mat4, v: Vec3) -> Mat4 {
|
||||
m[0][0] *= v.x;
|
||||
m[1][1] *= v.y;
|
||||
m[2][2] *= v.z;
|
||||
return m;
|
||||
}
|
||||
|
||||
mat4_scalef :: proc(m: Mat4, s: f32) -> Mat4 {
|
||||
m[0][0] = s
|
||||
m[1][1] = s
|
||||
m[2][2] = s
|
||||
return m
|
||||
scale :: proc(m: Mat4, s: f32) -> Mat4 {
|
||||
m[0][0] *= s;
|
||||
m[1][1] *= s;
|
||||
m[2][2] *= s;
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
mat4_look_at :: proc(eye, centre, up: Vec3) -> Mat4 {
|
||||
f := vec3_norm(centre - eye)
|
||||
s := vec3_norm(cross3(f, up))
|
||||
u := cross3(s, f)
|
||||
look_at :: proc(eye, centre, up: Vec3) -> Mat4 {
|
||||
f := norm(centre - eye);
|
||||
s := norm(cross(f, up));
|
||||
u := cross(s, f);
|
||||
|
||||
m: Mat4
|
||||
m: Mat4;
|
||||
|
||||
m[0] = Vec4{+s.x, +s.y, +s.z, 0}
|
||||
m[1] = Vec4{+u.x, +u.y, +u.z, 0}
|
||||
m[2] = Vec4{-f.x, -f.y, -f.z, 0}
|
||||
m[3] = Vec4{dot3(s, eye), dot3(u, eye), dot3(f, eye), 1}
|
||||
m[0] = Vec4{+s.x, +s.y, +s.z, 0};
|
||||
m[1] = Vec4{+u.x, +u.y, +u.z, 0};
|
||||
m[2] = Vec4{-f.x, -f.y, -f.z, 0};
|
||||
m[3] = Vec4{dot(s, eye), dot(u, eye), dot(f, eye), 1};
|
||||
|
||||
return m
|
||||
return m;
|
||||
}
|
||||
mat4_perspective :: proc(fovy, aspect, near, far: f32) -> Mat4 {
|
||||
m: Mat4
|
||||
tan_half_fovy := tan32(0.5 * fovy)
|
||||
m[0][0] = 1.0 / (aspect*tan_half_fovy)
|
||||
m[1][1] = 1.0 / (tan_half_fovy)
|
||||
m[2][2] = -(far + near) / (far - near)
|
||||
m[2][3] = -1.0
|
||||
m[3][2] = -2.0*far*near / (far - near)
|
||||
return m
|
||||
perspective :: proc(fovy, aspect, near, far: f32) -> Mat4 {
|
||||
m: Mat4;
|
||||
tan_half_fovy := tan(0.5 * fovy);
|
||||
m[0][0] = 1.0 / (aspect*tan_half_fovy);
|
||||
m[1][1] = 1.0 / (tan_half_fovy);
|
||||
m[2][2] = -(far + near) / (far - near);
|
||||
m[2][3] = -1.0;
|
||||
m[3][2] = -2.0*far*near / (far - near);
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
m[2][2] = -2.0 / (far - near)
|
||||
m[3][0] = -(right + left) / (right - left)
|
||||
m[3][1] = -(top + bottom) / (top - bottom)
|
||||
m[3][2] = -(far + near) / (far - near)
|
||||
|
||||
return m
|
||||
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);
|
||||
m[2][2] = -2.0 / (far - near);
|
||||
m[3][0] = -(right + left) / (right - left);
|
||||
m[3][1] = -(top + bottom) / (top - bottom);
|
||||
m[3][2] = -(far + near) / (far - near);
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
F32_DIG :: 6
|
||||
F32_EPSILON :: 1.192092896e-07
|
||||
F32_GUARD :: 0
|
||||
F32_MANT_DIG :: 24
|
||||
F32_MAX :: 3.402823466e+38
|
||||
F32_MAX_10_EXP :: 38
|
||||
F32_MAX_EXP :: 128
|
||||
F32_MIN :: 1.175494351e-38
|
||||
F32_MIN_10_EXP :: -37
|
||||
F32_MIN_EXP :: -125
|
||||
F32_NORMALIZE :: 0
|
||||
F32_RADIX :: 2
|
||||
F32_ROUNDS :: 1
|
||||
F32_DIG :: 6;
|
||||
F32_EPSILON :: 1.192092896e-07;
|
||||
F32_GUARD :: 0;
|
||||
F32_MANT_DIG :: 24;
|
||||
F32_MAX :: 3.402823466e+38;
|
||||
F32_MAX_10_EXP :: 38;
|
||||
F32_MAX_EXP :: 128;
|
||||
F32_MIN :: 1.175494351e-38;
|
||||
F32_MIN_10_EXP :: -37;
|
||||
F32_MIN_EXP :: -125;
|
||||
F32_NORMALIZE :: 0;
|
||||
F32_RADIX :: 2;
|
||||
F32_ROUNDS :: 1;
|
||||
|
||||
F64_DIG :: 15 // # of decimal digits of precision
|
||||
F64_EPSILON :: 2.2204460492503131e-016 // smallest such that 1.0+F64_EPSILON != 1.0
|
||||
F64_MANT_DIG :: 53 // # of bits in mantissa
|
||||
F64_MAX :: 1.7976931348623158e+308 // max value
|
||||
F64_MAX_10_EXP :: 308 // max decimal exponent
|
||||
F64_MAX_EXP :: 1024 // max binary exponent
|
||||
F64_MIN :: 2.2250738585072014e-308 // min positive value
|
||||
F64_MIN_10_EXP :: -307 // min decimal exponent
|
||||
F64_MIN_EXP :: -1021 // min binary exponent
|
||||
F64_RADIX :: 2 // exponent radix
|
||||
F64_ROUNDS :: 1 // addition rounding: near
|
||||
F64_DIG :: 15; // # of decimal digits of precision
|
||||
F64_EPSILON :: 2.2204460492503131e-016; // smallest such that 1.0+F64_EPSILON != 1.0
|
||||
F64_MANT_DIG :: 53; // # of bits in mantissa
|
||||
F64_MAX :: 1.7976931348623158e+308; // max value
|
||||
F64_MAX_10_EXP :: 308; // max decimal exponent
|
||||
F64_MAX_EXP :: 1024; // max binary exponent
|
||||
F64_MIN :: 2.2250738585072014e-308; // min positive value
|
||||
F64_MIN_10_EXP :: -307; // min decimal exponent
|
||||
F64_MIN_EXP :: -1021; // min binary exponent
|
||||
F64_RADIX :: 2; // exponent radix
|
||||
F64_ROUNDS :: 1; // addition rounding: near
|
||||
|
||||
|
||||
|
||||
|
||||
+178
-186
@@ -1,111 +1,90 @@
|
||||
#import "fmt.odin"
|
||||
#import "os.odin"
|
||||
#import "fmt.odin";
|
||||
#import "os.odin";
|
||||
|
||||
set :: proc(data: rawptr, value: i32, len: int) -> rawptr #link_name "__mem_set" {
|
||||
llvm_memset_64bit :: proc(dst: rawptr, val: byte, len: int, align: i32, is_volatile: bool) #foreign "llvm.memset.p0i8.i64"
|
||||
llvm_memset_64bit(data, value as byte, len, 1, false)
|
||||
return data
|
||||
llvm_memset_64bit :: proc(dst: rawptr, val: byte, len: int, align: i32, is_volatile: bool) #foreign __llvm_core "llvm.memset.p0i8.i64";
|
||||
llvm_memset_64bit(data, cast(byte)value, len, 1, false);
|
||||
return data;
|
||||
}
|
||||
|
||||
zero :: proc(data: rawptr, len: int) -> rawptr {
|
||||
return set(data, 0, len)
|
||||
zero :: proc(data: rawptr, len: int) -> rawptr #link_name "__mem_zero" {
|
||||
return set(data, 0, len);
|
||||
}
|
||||
|
||||
copy :: proc(dst, src: rawptr, len: int) -> rawptr #link_name "__mem_copy" {
|
||||
// NOTE(bill): This _must_ implemented like C's memmove
|
||||
llvm_memmove_64bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #foreign "llvm.memmove.p0i8.p0i8.i64"
|
||||
llvm_memmove_64bit(dst, src, len, 1, false)
|
||||
return dst
|
||||
llvm_memmove_64bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #foreign __llvm_core "llvm.memmove.p0i8.p0i8.i64";
|
||||
llvm_memmove_64bit(dst, src, len, 1, false);
|
||||
return dst;
|
||||
}
|
||||
|
||||
copy_non_overlapping :: proc(dst, src: rawptr, len: int) -> rawptr #link_name "__mem_copy_non_overlapping" {
|
||||
// NOTE(bill): This _must_ implemented like C's memcpy
|
||||
llvm_memcpy_64bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #foreign "llvm.memcpy.p0i8.p0i8.i64"
|
||||
llvm_memcpy_64bit(dst, src, len, 1, false)
|
||||
return dst
|
||||
llvm_memcpy_64bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #foreign __llvm_core "llvm.memcpy.p0i8.p0i8.i64";
|
||||
llvm_memcpy_64bit(dst, src, len, 1, false);
|
||||
return dst;
|
||||
}
|
||||
|
||||
|
||||
compare :: proc(dst, src: rawptr, n: int) -> int #link_name "__mem_compare" {
|
||||
// Translation of http://mgronhol.github.io/fast-strcmp/
|
||||
a := slice_ptr(dst as ^byte, n)
|
||||
b := slice_ptr(src as ^byte, n)
|
||||
|
||||
fast := n/size_of(int) + 1
|
||||
offset := (fast-1)*size_of(int)
|
||||
curr_block := 0
|
||||
if n <= size_of(int) {
|
||||
fast = 0
|
||||
}
|
||||
|
||||
la := slice_ptr(^a[0] as ^int, fast)
|
||||
lb := slice_ptr(^b[0] as ^int, fast)
|
||||
|
||||
for ; curr_block < fast; curr_block++ {
|
||||
if (la[curr_block] ~ lb[curr_block]) != 0 {
|
||||
for pos := curr_block*size_of(int); pos < n; pos++ {
|
||||
if (a[pos] ~ b[pos]) != 0 {
|
||||
return a[pos] as int - b[pos] as int
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for ; offset < n; offset++ {
|
||||
if (a[offset] ~ b[offset]) != 0 {
|
||||
return a[offset] as int - b[offset] as int
|
||||
a := slice_ptr(cast(^byte)dst, n);
|
||||
b := slice_ptr(cast(^byte)src, n);
|
||||
for i in 0..<n {
|
||||
match {
|
||||
case a[i] < b[i]:
|
||||
return -1;
|
||||
case a[i] > b[i]:
|
||||
return +1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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 gigabytes(x) * 1024 }
|
||||
terabytes :: proc(x: int) -> int #inline { return terabytes(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 gigabytes(x) * 1024; }
|
||||
terabytes :: proc(x: int) -> int #inline { return terabytes(x) * 1024; }
|
||||
|
||||
is_power_of_two :: proc(x: int) -> bool {
|
||||
if x <= 0 {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
return (x & (x-1)) == 0
|
||||
return (x & (x-1)) == 0;
|
||||
}
|
||||
|
||||
align_forward :: proc(ptr: rawptr, align: int) -> rawptr {
|
||||
assert(is_power_of_two(align))
|
||||
assert(is_power_of_two(align));
|
||||
|
||||
a := align as uint
|
||||
p := ptr as uint
|
||||
modulo := p & (a-1)
|
||||
a := cast(uint)align;
|
||||
p := cast(uint)ptr;
|
||||
modulo := p & (a-1);
|
||||
if modulo != 0 {
|
||||
p += a - modulo
|
||||
p += a - modulo;
|
||||
}
|
||||
return p as rawptr
|
||||
return cast(rawptr)p;
|
||||
}
|
||||
|
||||
|
||||
|
||||
AllocationHeader :: struct {
|
||||
size: int
|
||||
Allocation_Header :: struct {
|
||||
size: int,
|
||||
}
|
||||
allocation_header_fill :: proc(header: ^AllocationHeader, data: rawptr, size: int) {
|
||||
header.size = size
|
||||
ptr := (header+1) as ^int
|
||||
|
||||
for i := 0; ptr as rawptr < data; i++ {
|
||||
(ptr+i)^ = -1
|
||||
allocation_header_fill :: proc(header: ^Allocation_Header, data: rawptr, size: int) {
|
||||
header.size = size;
|
||||
ptr := cast(^int)(header+1);
|
||||
|
||||
for i := 0; cast(rawptr)ptr < data; i += 1 {
|
||||
(ptr+i)^ = -1;
|
||||
}
|
||||
}
|
||||
allocation_header :: proc(data: rawptr) -> ^AllocationHeader {
|
||||
p := data as ^int
|
||||
allocation_header :: proc(data: rawptr) -> ^Allocation_Header {
|
||||
p := cast(^int)data;
|
||||
for (p-1)^ == -1 {
|
||||
p = (p-1)
|
||||
p = (p-1);
|
||||
}
|
||||
return (p as ^AllocationHeader)-1
|
||||
return cast(^Allocation_Header)p-1;
|
||||
}
|
||||
|
||||
|
||||
@@ -113,16 +92,16 @@ allocation_header :: proc(data: rawptr) -> ^AllocationHeader {
|
||||
|
||||
|
||||
// Custom allocators
|
||||
|
||||
Arena :: struct {
|
||||
backing: Allocator
|
||||
memory: []byte
|
||||
temp_count: int
|
||||
backing: Allocator,
|
||||
offset: int,
|
||||
memory: []byte,
|
||||
temp_count: int,
|
||||
}
|
||||
|
||||
Temp_Memory :: struct {
|
||||
arena: ^Arena
|
||||
original_count: int
|
||||
}
|
||||
Arena_Temp_Memory :: struct {
|
||||
arena: ^Arena,
|
||||
original_count: int,
|
||||
}
|
||||
|
||||
|
||||
@@ -130,22 +109,23 @@ Arena :: struct {
|
||||
|
||||
|
||||
init_arena_from_memory :: proc(using a: ^Arena, data: []byte) {
|
||||
backing = Allocator{}
|
||||
memory = data[:0]
|
||||
temp_count = 0
|
||||
backing = Allocator{};
|
||||
memory = data[:0];
|
||||
temp_count = 0;
|
||||
}
|
||||
|
||||
init_arena_from_context :: proc(using a: ^Arena, size: int) {
|
||||
backing = context.allocator
|
||||
memory = new_slice(byte, 0, size)
|
||||
temp_count = 0
|
||||
backing = context.allocator;
|
||||
memory = new_slice(byte, size);
|
||||
temp_count = 0;
|
||||
}
|
||||
|
||||
free_arena :: proc(using a: ^Arena) {
|
||||
if backing.procedure != nil {
|
||||
push_allocator backing {
|
||||
free(memory.data)
|
||||
memory = memory[0:0:0]
|
||||
free(memory);
|
||||
memory = nil;
|
||||
offset = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,57 +134,57 @@ arena_allocator :: proc(arena: ^Arena) -> Allocator {
|
||||
return Allocator{
|
||||
procedure = arena_allocator_proc,
|
||||
data = arena,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator.Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
|
||||
arena := allocator_data as ^Arena
|
||||
arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
|
||||
using Allocator_Mode;
|
||||
arena := cast(^Arena)allocator_data;
|
||||
|
||||
using Allocator.Mode
|
||||
match mode {
|
||||
case ALLOC:
|
||||
total_size := size + alignment
|
||||
total_size := size + alignment;
|
||||
|
||||
if arena.memory.count + total_size > arena.memory.capacity {
|
||||
fmt.fprintln(os.stderr, "Arena out of memory")
|
||||
return nil
|
||||
if arena.offset + total_size > arena.memory.count {
|
||||
fmt.fprintln(os.stderr, "Arena out of memory");
|
||||
return nil;
|
||||
}
|
||||
|
||||
#no_bounds_check end := ^arena.memory[arena.memory.count]
|
||||
#no_bounds_check end := ^arena.memory[arena.offset];
|
||||
|
||||
ptr := align_forward(end, alignment)
|
||||
arena.memory.count += total_size
|
||||
return zero(ptr, size)
|
||||
ptr := align_forward(end, alignment);
|
||||
arena.offset += total_size;
|
||||
return zero(ptr, size);
|
||||
|
||||
case FREE:
|
||||
// NOTE(bill): Free all at once
|
||||
// Use Arena.Temp_Memory if you want to free a block
|
||||
// Use Arena_Temp_Memory if you want to free a block
|
||||
|
||||
case FREE_ALL:
|
||||
arena.memory.count = 0
|
||||
arena.offset = 0;
|
||||
|
||||
case RESIZE:
|
||||
return default_resize_align(old_memory, old_size, size, alignment)
|
||||
return default_resize_align(old_memory, old_size, size, alignment);
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil;
|
||||
}
|
||||
|
||||
begin_arena_temp_memory :: proc(a: ^Arena) -> Arena.Temp_Memory {
|
||||
tmp: Arena.Temp_Memory
|
||||
tmp.arena = a
|
||||
tmp.original_count = a.memory.count
|
||||
a.temp_count++
|
||||
return tmp
|
||||
begin_arena_temp_memory :: proc(a: ^Arena) -> Arena_Temp_Memory {
|
||||
tmp: Arena_Temp_Memory;
|
||||
tmp.arena = a;
|
||||
tmp.original_count = a.memory.count;
|
||||
a.temp_count += 1;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
end_arena_temp_memory :: proc(using tmp: Arena.Temp_Memory) {
|
||||
assert(arena.memory.count >= original_count)
|
||||
assert(arena.temp_count > 0)
|
||||
arena.memory.count = original_count
|
||||
arena.temp_count--
|
||||
end_arena_temp_memory :: proc(using tmp: Arena_Temp_Memory) {
|
||||
assert(arena.memory.count >= original_count);
|
||||
assert(arena.temp_count > 0);
|
||||
arena.memory.count = original_count;
|
||||
arena.temp_count -= 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -214,119 +194,131 @@ end_arena_temp_memory :: proc(using tmp: Arena.Temp_Memory) {
|
||||
|
||||
|
||||
align_of_type_info :: proc(type_info: ^Type_Info) -> int {
|
||||
WORD_SIZE :: size_of(int)
|
||||
using Type_Info
|
||||
|
||||
match type info : type_info {
|
||||
case Named:
|
||||
return align_of_type_info(info.base)
|
||||
case Integer:
|
||||
return info.size
|
||||
case Float:
|
||||
return info.size
|
||||
case String:
|
||||
return WORD_SIZE
|
||||
case Boolean:
|
||||
return 1
|
||||
case Pointer:
|
||||
return WORD_SIZE
|
||||
case Maybe:
|
||||
return max(align_of_type_info(info.elem), 1)
|
||||
case Procedure:
|
||||
return WORD_SIZE
|
||||
case Array:
|
||||
return align_of_type_info(info.elem)
|
||||
case Slice:
|
||||
return WORD_SIZE
|
||||
case Vector:
|
||||
return align_of_type_info(info.elem)
|
||||
case Struct:
|
||||
return info.align
|
||||
case Union:
|
||||
return info.align
|
||||
case Raw_Union:
|
||||
return info.align
|
||||
case Enum:
|
||||
return align_of_type_info(info.base)
|
||||
prev_pow2 :: proc(n: i64) -> i64 {
|
||||
if n <= 0 {
|
||||
return 0;
|
||||
}
|
||||
n |= n >> 1;
|
||||
n |= n >> 2;
|
||||
n |= n >> 4;
|
||||
n |= n >> 8;
|
||||
n |= n >> 16;
|
||||
n |= n >> 32;
|
||||
return n - (n >> 1);
|
||||
}
|
||||
|
||||
return 0
|
||||
WORD_SIZE :: size_of(int);
|
||||
MAX_ALIGN :: size_of([vector 64]f64); // TODO(bill): Should these constants be builtin constants?
|
||||
using Type_Info;
|
||||
|
||||
match type info in type_info {
|
||||
case Named:
|
||||
return align_of_type_info(info.base);
|
||||
case Integer:
|
||||
return info.size;
|
||||
case Float:
|
||||
return info.size;
|
||||
case String:
|
||||
return WORD_SIZE;
|
||||
case Boolean:
|
||||
return 1;
|
||||
case Pointer:
|
||||
return WORD_SIZE;
|
||||
case Maybe:
|
||||
return max(align_of_type_info(info.elem), 1);
|
||||
case Procedure:
|
||||
return WORD_SIZE;
|
||||
case Array:
|
||||
return align_of_type_info(info.elem);
|
||||
case Slice:
|
||||
return WORD_SIZE;
|
||||
case Vector:
|
||||
size := size_of_type_info(info.elem);
|
||||
count := cast(int)max(prev_pow2(cast(i64)info.count), 1);
|
||||
total := size * count;
|
||||
return clamp(total, 1, MAX_ALIGN);
|
||||
case Struct:
|
||||
return info.align;
|
||||
case Union:
|
||||
return info.align;
|
||||
case Raw_Union:
|
||||
return info.align;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
align_formula :: proc(size, align: int) -> int {
|
||||
result := size + align-1
|
||||
return result - result%align
|
||||
result := size + align-1;
|
||||
return result - result%align;
|
||||
}
|
||||
|
||||
size_of_type_info :: proc(type_info: ^Type_Info) -> int {
|
||||
WORD_SIZE :: size_of(int)
|
||||
using Type_Info
|
||||
|
||||
match type info : type_info {
|
||||
WORD_SIZE :: size_of(int);
|
||||
using Type_Info;
|
||||
match type info in type_info {
|
||||
case Named:
|
||||
return size_of_type_info(info.base)
|
||||
return size_of_type_info(info.base);
|
||||
case Integer:
|
||||
return info.size
|
||||
return info.size;
|
||||
case Float:
|
||||
return info.size
|
||||
return info.size;
|
||||
case Any:
|
||||
return 2*WORD_SIZE
|
||||
return 2*WORD_SIZE;
|
||||
case String:
|
||||
return 2*WORD_SIZE
|
||||
return 2*WORD_SIZE;
|
||||
case Boolean:
|
||||
return 1
|
||||
return 1;
|
||||
case Pointer:
|
||||
return WORD_SIZE
|
||||
return WORD_SIZE;
|
||||
case Maybe:
|
||||
return size_of_type_info(info.elem) + 1
|
||||
return size_of_type_info(info.elem) + 1;
|
||||
case Procedure:
|
||||
return WORD_SIZE
|
||||
return WORD_SIZE;
|
||||
case Array:
|
||||
count := info.count
|
||||
count := info.count;
|
||||
if count == 0 {
|
||||
return 0
|
||||
return 0;
|
||||
}
|
||||
size := size_of_type_info(info.elem)
|
||||
align := align_of_type_info(info.elem)
|
||||
alignment := align_formula(size, align)
|
||||
return alignment*(count-1) + size
|
||||
size := size_of_type_info(info.elem);
|
||||
align := align_of_type_info(info.elem);
|
||||
alignment := align_formula(size, align);
|
||||
return alignment*(count-1) + size;
|
||||
case Slice:
|
||||
return 3*WORD_SIZE
|
||||
return 3*WORD_SIZE;
|
||||
case Vector:
|
||||
is_bool :: proc(type_info: ^Type_Info) -> bool {
|
||||
match type info : type_info {
|
||||
match type info in type_info {
|
||||
case Named:
|
||||
return is_bool(info.base)
|
||||
return is_bool(info.base);
|
||||
case Boolean:
|
||||
return true
|
||||
return true;
|
||||
}
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
|
||||
count := info.count
|
||||
count := info.count;
|
||||
if count == 0 {
|
||||
return 0
|
||||
return 0;
|
||||
}
|
||||
bit_size := 8*size_of_type_info(info.elem)
|
||||
bit_size := 8*size_of_type_info(info.elem);
|
||||
if is_bool(info.elem) {
|
||||
// NOTE(bill): LLVM can store booleans as 1 bit because a boolean _is_ an `i1`
|
||||
// Silly LLVM spec
|
||||
bit_size = 1
|
||||
bit_size = 1;
|
||||
}
|
||||
total_size_in_bits := bit_size * count
|
||||
total_size := (total_size_in_bits+7)/8
|
||||
return total_size
|
||||
total_size_in_bits := bit_size * count;
|
||||
total_size := (total_size_in_bits+7)/8;
|
||||
return total_size;
|
||||
|
||||
case Struct:
|
||||
return info.size
|
||||
return info.size;
|
||||
case Union:
|
||||
return info.size
|
||||
return info.size;
|
||||
case Raw_Union:
|
||||
return info.size
|
||||
case Enum:
|
||||
return size_of_type_info(info.base)
|
||||
return info.size;
|
||||
}
|
||||
|
||||
return 0
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
+119
-119
@@ -1,155 +1,155 @@
|
||||
#foreign_system_library "opengl32"
|
||||
#import "win32.odin"
|
||||
#load "opengl_constants.odin"
|
||||
#foreign_system_library lib "opengl32.lib" when ODIN_OS == "windows";
|
||||
#import win32 "sys/windows.odin" when ODIN_OS == "windows";
|
||||
#include "opengl_constants.odin";
|
||||
|
||||
Clear :: proc(mask: u32) #foreign "glClear"
|
||||
ClearColor :: proc(r, g, b, a: f32) #foreign "glClearColor"
|
||||
Begin :: proc(mode: i32) #foreign "glBegin"
|
||||
End :: proc() #foreign "glEnd"
|
||||
Finish :: proc() #foreign "glFinish"
|
||||
BlendFunc :: proc(sfactor, dfactor: i32) #foreign "glBlendFunc"
|
||||
Enable :: proc(cap: i32) #foreign "glEnable"
|
||||
Disable :: proc(cap: i32) #foreign "glDisable"
|
||||
GenTextures :: proc(count: i32, result: ^u32) #foreign "glGenTextures"
|
||||
DeleteTextures :: proc(count: i32, result: ^u32) #foreign "glDeleteTextures"
|
||||
TexParameteri :: proc(target, pname, param: i32) #foreign "glTexParameteri"
|
||||
TexParameterf :: proc(target: i32, pname: i32, param: f32) #foreign "glTexParameterf"
|
||||
BindTexture :: proc(target: i32, texture: u32) #foreign "glBindTexture"
|
||||
LoadIdentity :: proc() #foreign "glLoadIdentity"
|
||||
Viewport :: proc(x, y, width, height: i32) #foreign "glViewport"
|
||||
Ortho :: proc(left, right, bottom, top, near, far: f64) #foreign "glOrtho"
|
||||
Color3f :: proc(r, g, b: f32) #foreign "glColor3f"
|
||||
Vertex3f :: proc(x, y, z: f32) #foreign "glVertex3f"
|
||||
TexImage2D :: proc(target, level, internal_format,
|
||||
width, height, border,
|
||||
format, _type: i32, pixels: rawptr) #foreign "glTexImage2D"
|
||||
Clear :: proc(mask: u32) #foreign lib "glClear";
|
||||
ClearColor :: proc(r, g, b, a: f32) #foreign lib "glClearColor";
|
||||
Begin :: proc(mode: i32) #foreign lib "glBegin";
|
||||
End :: proc() #foreign lib "glEnd";
|
||||
Finish :: proc() #foreign lib "glFinish";
|
||||
BlendFunc :: proc(sfactor, dfactor: i32) #foreign lib "glBlendFunc";
|
||||
Enable :: proc(cap: i32) #foreign lib "glEnable";
|
||||
Disable :: proc(cap: i32) #foreign lib "glDisable";
|
||||
GenTextures :: proc(count: i32, result: ^u32) #foreign lib "glGenTextures";
|
||||
DeleteTextures:: proc(count: i32, result: ^u32) #foreign lib "glDeleteTextures";
|
||||
TexParameteri :: proc(target, pname, param: i32) #foreign lib "glTexParameteri";
|
||||
TexParameterf :: proc(target: i32, pname: i32, param: f32) #foreign lib "glTexParameterf";
|
||||
BindTexture :: proc(target: i32, texture: u32) #foreign lib "glBindTexture";
|
||||
LoadIdentity :: proc() #foreign lib "glLoadIdentity";
|
||||
Viewport :: proc(x, y, width, height: i32) #foreign lib "glViewport";
|
||||
Ortho :: proc(left, right, bottom, top, near, far: f64) #foreign lib "glOrtho";
|
||||
Color3f :: proc(r, g, b: f32) #foreign lib "glColor3f";
|
||||
Vertex3f :: proc(x, y, z: f32) #foreign lib "glVertex3f";
|
||||
TexImage2D :: proc(target, level, internal_format,
|
||||
width, height, border,
|
||||
format, _type: i32, pixels: rawptr) #foreign lib "glTexImage2D";
|
||||
|
||||
GetError :: proc() -> i32 #foreign "glGetError"
|
||||
GetString :: proc(name: i32) -> ^byte #foreign "glGetString"
|
||||
GetIntegerv :: proc(name: i32, v: ^i32) #foreign "glGetIntegerv"
|
||||
GetError :: proc() -> i32 #foreign lib "glGetError";
|
||||
GetString :: proc(name: i32) -> ^byte #foreign lib "glGetString";
|
||||
GetIntegerv :: proc(name: i32, v: ^i32) #foreign lib "glGetIntegerv";
|
||||
|
||||
|
||||
string_data :: proc(s: string) -> ^u8 #inline { return ^s[0]; }
|
||||
|
||||
_libgl := win32.LoadLibraryA(("opengl32.dll\x00" as string).data)
|
||||
_libgl := win32.LoadLibraryA(string_data("opengl32.dll\x00"));
|
||||
|
||||
GetProcAddress :: proc(name: string) -> proc() {
|
||||
assert(name[name.count-1] == 0)
|
||||
res := win32.wglGetProcAddress(name.data)
|
||||
GetProcAddress :: proc(name: string) -> proc() #cc_c {
|
||||
assert(name[name.count-1] == 0);
|
||||
res := win32.wglGetProcAddress(name.data);
|
||||
if res == nil {
|
||||
res = win32.GetProcAddress(_libgl, name.data);
|
||||
}
|
||||
return res
|
||||
return res;
|
||||
}
|
||||
|
||||
GenBuffers: proc(count: i32, buffers: ^u32) #cc_c;
|
||||
GenVertexArrays: proc(count: i32, buffers: ^u32) #cc_c;
|
||||
GenSamplers: proc(count: i32, buffers: ^u32) #cc_c;
|
||||
BindBuffer: proc(target: i32, buffer: u32) #cc_c;
|
||||
BindVertexArray: proc(buffer: u32) #cc_c;
|
||||
BindSampler: proc(position: i32, sampler: u32) #cc_c;
|
||||
BufferData: proc(target: i32, size: int, data: rawptr, usage: i32) #cc_c;
|
||||
BufferSubData: proc(target: i32, offset, size: int, data: rawptr) #cc_c;
|
||||
|
||||
GenBuffers: proc(count: i32, buffers: ^u32)
|
||||
GenVertexArrays: proc(count: i32, buffers: ^u32)
|
||||
GenSamplers: proc(count: i32, buffers: ^u32)
|
||||
BindBuffer: proc(target: i32, buffer: u32)
|
||||
BindVertexArray: proc(buffer: u32)
|
||||
BindSampler: proc(position: i32, sampler: u32)
|
||||
BufferData: proc(target: i32, size: int, data: rawptr, usage: i32)
|
||||
BufferSubData: proc(target: i32, offset, size: int, data: rawptr)
|
||||
DrawArrays: proc(mode, first: i32, count: u32) #cc_c;
|
||||
DrawElements: proc(mode: i32, count: u32, type_: i32, indices: rawptr) #cc_c;
|
||||
|
||||
DrawArrays: proc(mode, first: i32, count: u32)
|
||||
DrawElements: proc(mode: i32, count: u32, type_: i32, indices: rawptr)
|
||||
MapBuffer: proc(target, access: i32) -> rawptr #cc_c;
|
||||
UnmapBuffer: proc(target: i32) #cc_c;
|
||||
|
||||
MapBuffer: proc(target, access: i32) -> rawptr
|
||||
UnmapBuffer: proc(target: i32)
|
||||
VertexAttribPointer: proc(index: u32, size, type_: i32, normalized: i32, stride: u32, pointer: rawptr) #cc_c;
|
||||
EnableVertexAttribArray: proc(index: u32) #cc_c;
|
||||
|
||||
VertexAttribPointer: proc(index: u32, size, type_: i32, normalized: i32, stride: u32, pointer: rawptr)
|
||||
EnableVertexAttribArray: proc(index: u32)
|
||||
|
||||
CreateShader: proc(shader_type: i32) -> u32
|
||||
ShaderSource: proc(shader: u32, count: u32, string: ^^byte, length: ^i32)
|
||||
CompileShader: proc(shader: u32)
|
||||
CreateProgram: proc() -> u32
|
||||
AttachShader: proc(program, shader: u32)
|
||||
DetachShader: proc(program, shader: u32)
|
||||
DeleteShader: proc(shader: u32)
|
||||
LinkProgram: proc(program: u32)
|
||||
UseProgram: proc(program: u32)
|
||||
DeleteProgram: proc(program: u32)
|
||||
CreateShader: proc(shader_type: i32) -> u32 #cc_c;
|
||||
ShaderSource: proc(shader: u32, count: u32, str: ^^byte, length: ^i32) #cc_c;
|
||||
CompileShader: proc(shader: u32) #cc_c;
|
||||
CreateProgram: proc() -> u32 #cc_c;
|
||||
AttachShader: proc(program, shader: u32) #cc_c;
|
||||
DetachShader: proc(program, shader: u32) #cc_c;
|
||||
DeleteShader: proc(shader: u32) #cc_c;
|
||||
LinkProgram: proc(program: u32) #cc_c;
|
||||
UseProgram: proc(program: u32) #cc_c;
|
||||
DeleteProgram: proc(program: u32) #cc_c;
|
||||
|
||||
|
||||
GetShaderiv: proc(shader: u32, pname: i32, params: ^i32)
|
||||
GetProgramiv: proc(program: u32, pname: i32, params: ^i32)
|
||||
GetShaderInfoLog: proc(shader: u32, max_length: u32, length: ^u32, info_long: ^byte)
|
||||
GetProgramInfoLog: proc(program: u32, max_length: u32, length: ^u32, info_long: ^byte)
|
||||
GetShaderiv: proc(shader: u32, pname: i32, params: ^i32) #cc_c;
|
||||
GetProgramiv: proc(program: u32, pname: i32, params: ^i32) #cc_c;
|
||||
GetShaderInfoLog: proc(shader: u32, max_length: u32, length: ^u32, info_long: ^byte) #cc_c;
|
||||
GetProgramInfoLog: proc(program: u32, max_length: u32, length: ^u32, info_long: ^byte) #cc_c;
|
||||
|
||||
ActiveTexture: proc(texture: i32)
|
||||
GenerateMipmap: proc(target: i32)
|
||||
ActiveTexture: proc(texture: i32) #cc_c;
|
||||
GenerateMipmap: proc(target: i32) #cc_c;
|
||||
|
||||
SamplerParameteri: proc(sampler: u32, pname: i32, param: i32)
|
||||
SamplerParameterf: proc(sampler: u32, pname: i32, param: f32)
|
||||
SamplerParameteriv: proc(sampler: u32, pname: i32, params: ^i32)
|
||||
SamplerParameterfv: proc(sampler: u32, pname: i32, params: ^f32)
|
||||
SamplerParameterIiv: proc(sampler: u32, pname: i32, params: ^i32)
|
||||
SamplerParameterIuiv: proc(sampler: u32, pname: i32, params: ^u32)
|
||||
SamplerParameteri: proc(sampler: u32, pname: i32, param: i32) #cc_c;
|
||||
SamplerParameterf: proc(sampler: u32, pname: i32, param: f32) #cc_c;
|
||||
SamplerParameteriv: proc(sampler: u32, pname: i32, params: ^i32) #cc_c;
|
||||
SamplerParameterfv: proc(sampler: u32, pname: i32, params: ^f32) #cc_c;
|
||||
SamplerParameterIiv: proc(sampler: u32, pname: i32, params: ^i32) #cc_c;
|
||||
SamplerParameterIuiv: proc(sampler: u32, pname: i32, params: ^u32) #cc_c;
|
||||
|
||||
|
||||
Uniform1i: proc(loc: i32, v0: i32)
|
||||
Uniform2i: proc(loc: i32, v0, v1: i32)
|
||||
Uniform3i: proc(loc: i32, v0, v1, v2: i32)
|
||||
Uniform4i: proc(loc: i32, v0, v1, v2, v3: i32)
|
||||
Uniform1f: proc(loc: i32, v0: f32)
|
||||
Uniform2f: proc(loc: i32, v0, v1: f32)
|
||||
Uniform3f: proc(loc: i32, v0, v1, v2: f32)
|
||||
Uniform4f: proc(loc: i32, v0, v1, v2, v3: f32)
|
||||
UniformMatrix4fv: proc(loc: i32, count: u32, transpose: i32, value: ^f32)
|
||||
Uniform1i: proc(loc: i32, v0: i32) #cc_c;
|
||||
Uniform2i: proc(loc: i32, v0, v1: i32) #cc_c;
|
||||
Uniform3i: proc(loc: i32, v0, v1, v2: i32) #cc_c;
|
||||
Uniform4i: proc(loc: i32, v0, v1, v2, v3: i32) #cc_c;
|
||||
Uniform1f: proc(loc: i32, v0: f32) #cc_c;
|
||||
Uniform2f: proc(loc: i32, v0, v1: f32) #cc_c;
|
||||
Uniform3f: proc(loc: i32, v0, v1, v2: f32) #cc_c;
|
||||
Uniform4f: proc(loc: i32, v0, v1, v2, v3: f32) #cc_c;
|
||||
UniformMatrix4fv: proc(loc: i32, count: u32, transpose: i32, value: ^f32) #cc_c;
|
||||
|
||||
GetUniformLocation: proc(program: u32, name: ^byte) -> i32
|
||||
GetUniformLocation: proc(program: u32, name: ^byte) -> i32 #cc_c;
|
||||
|
||||
init :: proc() {
|
||||
set_proc_address :: proc(p: rawptr, name: string) #inline { (p as ^proc())^ = GetProcAddress(name) }
|
||||
set_proc_address :: proc(p: rawptr, name: string) #inline { (cast(^(proc() #cc_c))p)^ = GetProcAddress(name); }
|
||||
|
||||
set_proc_address(^GenBuffers, "glGenBuffers\x00")
|
||||
set_proc_address(^GenVertexArrays, "glGenVertexArrays\x00")
|
||||
set_proc_address(^GenSamplers, "glGenSamplers\x00")
|
||||
set_proc_address(^BindBuffer, "glBindBuffer\x00")
|
||||
set_proc_address(^BindSampler, "glBindSampler\x00")
|
||||
set_proc_address(^BindVertexArray, "glBindVertexArray\x00")
|
||||
set_proc_address(^BufferData, "glBufferData\x00")
|
||||
set_proc_address(^BufferSubData, "glBufferSubData\x00")
|
||||
set_proc_address(^GenBuffers, "glGenBuffers\x00");
|
||||
set_proc_address(^GenVertexArrays, "glGenVertexArrays\x00");
|
||||
set_proc_address(^GenSamplers, "glGenSamplers\x00");
|
||||
set_proc_address(^BindBuffer, "glBindBuffer\x00");
|
||||
set_proc_address(^BindSampler, "glBindSampler\x00");
|
||||
set_proc_address(^BindVertexArray, "glBindVertexArray\x00");
|
||||
set_proc_address(^BufferData, "glBufferData\x00");
|
||||
set_proc_address(^BufferSubData, "glBufferSubData\x00");
|
||||
|
||||
set_proc_address(^DrawArrays, "glDrawArrays\x00")
|
||||
set_proc_address(^DrawElements, "glDrawElements\x00")
|
||||
set_proc_address(^DrawArrays, "glDrawArrays\x00");
|
||||
set_proc_address(^DrawElements, "glDrawElements\x00");
|
||||
|
||||
set_proc_address(^MapBuffer, "glMapBuffer\x00")
|
||||
set_proc_address(^UnmapBuffer, "glUnmapBuffer\x00")
|
||||
set_proc_address(^MapBuffer, "glMapBuffer\x00");
|
||||
set_proc_address(^UnmapBuffer, "glUnmapBuffer\x00");
|
||||
|
||||
set_proc_address(^VertexAttribPointer, "glVertexAttribPointer\x00")
|
||||
set_proc_address(^EnableVertexAttribArray, "glEnableVertexAttribArray\x00")
|
||||
set_proc_address(^VertexAttribPointer, "glVertexAttribPointer\x00");
|
||||
set_proc_address(^EnableVertexAttribArray, "glEnableVertexAttribArray\x00");
|
||||
|
||||
set_proc_address(^CreateShader, "glCreateShader\x00")
|
||||
set_proc_address(^ShaderSource, "glShaderSource\x00")
|
||||
set_proc_address(^CompileShader, "glCompileShader\x00")
|
||||
set_proc_address(^CreateProgram, "glCreateProgram\x00")
|
||||
set_proc_address(^AttachShader, "glAttachShader\x00")
|
||||
set_proc_address(^DetachShader, "glDetachShader\x00")
|
||||
set_proc_address(^DeleteShader, "glDeleteShader\x00")
|
||||
set_proc_address(^LinkProgram, "glLinkProgram\x00")
|
||||
set_proc_address(^UseProgram, "glUseProgram\x00")
|
||||
set_proc_address(^DeleteProgram, "glDeleteProgram\x00")
|
||||
set_proc_address(^CreateShader, "glCreateShader\x00");
|
||||
set_proc_address(^ShaderSource, "glShaderSource\x00");
|
||||
set_proc_address(^CompileShader, "glCompileShader\x00");
|
||||
set_proc_address(^CreateProgram, "glCreateProgram\x00");
|
||||
set_proc_address(^AttachShader, "glAttachShader\x00");
|
||||
set_proc_address(^DetachShader, "glDetachShader\x00");
|
||||
set_proc_address(^DeleteShader, "glDeleteShader\x00");
|
||||
set_proc_address(^LinkProgram, "glLinkProgram\x00");
|
||||
set_proc_address(^UseProgram, "glUseProgram\x00");
|
||||
set_proc_address(^DeleteProgram, "glDeleteProgram\x00");
|
||||
|
||||
set_proc_address(^GetShaderiv, "glGetShaderiv\x00")
|
||||
set_proc_address(^GetProgramiv, "glGetProgramiv\x00")
|
||||
set_proc_address(^GetShaderInfoLog, "glGetShaderInfoLog\x00")
|
||||
set_proc_address(^GetProgramInfoLog, "glGetProgramInfoLog\x00")
|
||||
set_proc_address(^GetShaderiv, "glGetShaderiv\x00");
|
||||
set_proc_address(^GetProgramiv, "glGetProgramiv\x00");
|
||||
set_proc_address(^GetShaderInfoLog, "glGetShaderInfoLog\x00");
|
||||
set_proc_address(^GetProgramInfoLog, "glGetProgramInfoLog\x00");
|
||||
|
||||
set_proc_address(^ActiveTexture, "glActiveTexture\x00")
|
||||
set_proc_address(^GenerateMipmap, "glGenerateMipmap\x00")
|
||||
set_proc_address(^ActiveTexture, "glActiveTexture\x00");
|
||||
set_proc_address(^GenerateMipmap, "glGenerateMipmap\x00");
|
||||
|
||||
set_proc_address(^Uniform1i, "glUniform1i\x00")
|
||||
set_proc_address(^UniformMatrix4fv, "glUniformMatrix4fv\x00")
|
||||
set_proc_address(^Uniform1i, "glUniform1i\x00");
|
||||
set_proc_address(^UniformMatrix4fv, "glUniformMatrix4fv\x00");
|
||||
|
||||
set_proc_address(^GetUniformLocation, "glGetUniformLocation\x00")
|
||||
set_proc_address(^GetUniformLocation, "glGetUniformLocation\x00");
|
||||
|
||||
set_proc_address(^SamplerParameteri, "glSamplerParameteri\x00")
|
||||
set_proc_address(^SamplerParameterf, "glSamplerParameterf\x00")
|
||||
set_proc_address(^SamplerParameteriv, "glSamplerParameteriv\x00")
|
||||
set_proc_address(^SamplerParameterfv, "glSamplerParameterfv\x00")
|
||||
set_proc_address(^SamplerParameterIiv, "glSamplerParameterIiv\x00")
|
||||
set_proc_address(^SamplerParameterIuiv, "glSamplerParameterIuiv\x00")
|
||||
set_proc_address(^SamplerParameteri, "glSamplerParameteri\x00");
|
||||
set_proc_address(^SamplerParameterf, "glSamplerParameterf\x00");
|
||||
set_proc_address(^SamplerParameteriv, "glSamplerParameteriv\x00");
|
||||
set_proc_address(^SamplerParameterfv, "glSamplerParameterfv\x00");
|
||||
set_proc_address(^SamplerParameterIiv, "glSamplerParameterIiv\x00");
|
||||
set_proc_address(^SamplerParameterIuiv, "glSamplerParameterIuiv\x00");
|
||||
}
|
||||
|
||||
|
||||
+1366
-1365
File diff suppressed because it is too large
Load Diff
+1
-171
@@ -1,172 +1,2 @@
|
||||
#import "win32.odin"
|
||||
#import "fmt.odin"
|
||||
|
||||
File_Time :: type u64
|
||||
|
||||
File :: struct {
|
||||
Handle :: type win32.HANDLE
|
||||
handle: Handle
|
||||
last_write_time: File_Time
|
||||
}
|
||||
|
||||
open :: proc(name: string) -> (File, bool) {
|
||||
using win32
|
||||
buf: [300]byte
|
||||
copy(buf[:], name as []byte)
|
||||
f := File{
|
||||
handle = CreateFileA(^buf[0], FILE_GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, nil),
|
||||
}
|
||||
success := f.handle != INVALID_HANDLE_VALUE as File.Handle
|
||||
f.last_write_time = last_write_time(^f)
|
||||
return f, success
|
||||
}
|
||||
|
||||
create :: proc(name: string) -> (File, bool) {
|
||||
using win32
|
||||
buf: [300]byte
|
||||
copy(buf[:], name as []byte)
|
||||
f := File{
|
||||
handle = CreateFileA(^buf[0], FILE_GENERIC_WRITE, FILE_SHARE_READ, nil, CREATE_ALWAYS, 0, nil),
|
||||
}
|
||||
success := f.handle != INVALID_HANDLE_VALUE as File.Handle
|
||||
f.last_write_time = last_write_time(^f)
|
||||
return f, success
|
||||
}
|
||||
|
||||
close :: proc(using f: ^File) {
|
||||
win32.CloseHandle(handle)
|
||||
}
|
||||
|
||||
write :: proc(using f: ^File, buf: []byte) -> bool {
|
||||
bytes_written: i32
|
||||
return win32.WriteFile(handle, buf.data, buf.count as i32, ^bytes_written, nil) != 0
|
||||
}
|
||||
|
||||
file_has_changed :: proc(f: ^File) -> bool {
|
||||
last_write_time := last_write_time(f)
|
||||
if f.last_write_time != last_write_time {
|
||||
f.last_write_time = last_write_time
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
|
||||
last_write_time :: proc(f: ^File) -> File_Time {
|
||||
file_info: win32.BY_HANDLE_FILE_INFORMATION
|
||||
win32.GetFileInformationByHandle(f.handle, ^file_info)
|
||||
l := file_info.last_write_time.low_date_time as File_Time
|
||||
h := file_info.last_write_time.high_date_time as File_Time
|
||||
return l | h << 32
|
||||
}
|
||||
|
||||
last_write_time_by_name :: proc(name: string) -> File_Time {
|
||||
last_write_time: win32.FILETIME
|
||||
data: win32.WIN32_FILE_ATTRIBUTE_DATA
|
||||
|
||||
buf: [1024]byte
|
||||
path := buf[:0]
|
||||
fmt.bprint(^path, name, "\x00")
|
||||
|
||||
if win32.GetFileAttributesExA(path.data, win32.GetFileExInfoStandard, ^data) != 0 {
|
||||
last_write_time = data.last_write_time
|
||||
}
|
||||
|
||||
l := last_write_time.low_date_time as File_Time
|
||||
h := last_write_time.high_date_time as File_Time
|
||||
return l | h << 32
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
File_Standard :: type enum {
|
||||
INPUT,
|
||||
OUTPUT,
|
||||
ERROR,
|
||||
}
|
||||
|
||||
// NOTE(bill): Uses startup to initialize it
|
||||
__std_files := [File_Standard.count]File{
|
||||
{handle = win32.GetStdHandle(win32.STD_INPUT_HANDLE)},
|
||||
{handle = win32.GetStdHandle(win32.STD_OUTPUT_HANDLE)},
|
||||
{handle = win32.GetStdHandle(win32.STD_ERROR_HANDLE)},
|
||||
}
|
||||
|
||||
stdin := ^__std_files[File_Standard.INPUT]
|
||||
stdout := ^__std_files[File_Standard.OUTPUT]
|
||||
stderr := ^__std_files[File_Standard.ERROR]
|
||||
|
||||
|
||||
|
||||
read_entire_file :: proc(name: string) -> ([]byte, bool) {
|
||||
buf: [300]byte
|
||||
copy(buf[:], name as []byte)
|
||||
|
||||
f, file_ok := open(name)
|
||||
if !file_ok {
|
||||
return nil, false
|
||||
}
|
||||
defer close(^f)
|
||||
|
||||
length: i64
|
||||
file_size_ok := win32.GetFileSizeEx(f.handle as win32.HANDLE, ^length) != 0
|
||||
if !file_size_ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
data := new_slice(u8, length)
|
||||
if data.data == nil {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
single_read_length: i32
|
||||
total_read: i64
|
||||
|
||||
for total_read < length {
|
||||
remaining := length - total_read
|
||||
to_read: u32
|
||||
MAX :: 1<<32-1
|
||||
if remaining <= MAX {
|
||||
to_read = remaining as u32
|
||||
} else {
|
||||
to_read = MAX
|
||||
}
|
||||
|
||||
win32.ReadFile(f.handle as win32.HANDLE, ^data[total_read], to_read, ^single_read_length, nil)
|
||||
if single_read_length <= 0 {
|
||||
free(data.data)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
total_read += single_read_length as i64
|
||||
}
|
||||
|
||||
return data, true
|
||||
}
|
||||
|
||||
|
||||
|
||||
heap_alloc :: proc(size: int) -> rawptr {
|
||||
return win32.HeapAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, size)
|
||||
}
|
||||
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
|
||||
return win32.HeapReAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, ptr, new_size)
|
||||
}
|
||||
heap_free :: proc(ptr: rawptr) {
|
||||
win32.HeapFree(win32.GetProcessHeap(), 0, ptr)
|
||||
}
|
||||
|
||||
|
||||
exit :: proc(code: int) {
|
||||
win32.ExitProcess(code as u32)
|
||||
}
|
||||
|
||||
|
||||
|
||||
current_thread_id :: proc() -> int {
|
||||
GetCurrentThreadId :: proc() -> u32 #foreign #dll_import
|
||||
return GetCurrentThreadId() as int
|
||||
}
|
||||
#include "os_windows.odin" when ODIN_OS == "windows"
|
||||
|
||||
|
||||
@@ -0,0 +1,272 @@
|
||||
#import win32 "sys/windows.odin";
|
||||
#import "fmt.odin";
|
||||
|
||||
|
||||
Handle :: int;
|
||||
File_Time :: u64;
|
||||
Errno :: int;
|
||||
|
||||
INVALID_HANDLE: Handle : -1;
|
||||
|
||||
|
||||
O_RDONLY :: 0x00000;
|
||||
O_WRONLY :: 0x00001;
|
||||
O_RDWR :: 0x00002;
|
||||
O_CREAT :: 0x00040;
|
||||
O_EXCL :: 0x00080;
|
||||
O_NOCTTY :: 0x00100;
|
||||
O_TRUNC :: 0x00200;
|
||||
O_NONBLOCK :: 0x00800;
|
||||
O_APPEND :: 0x00400;
|
||||
O_SYNC :: 0x01000;
|
||||
O_ASYNC :: 0x02000;
|
||||
O_CLOEXEC :: 0x80000;
|
||||
|
||||
ERROR_NONE: Errno : 0;
|
||||
ERROR_FILE_NOT_FOUND: Errno : 2;
|
||||
ERROR_PATH_NOT_FOUND: Errno : 3;
|
||||
ERROR_ACCESS_DENIED: Errno : 5;
|
||||
ERROR_NO_MORE_FILES: Errno : 18;
|
||||
ERROR_HANDLE_EOF: Errno : 38;
|
||||
ERROR_NETNAME_DELETED: Errno : 64;
|
||||
ERROR_FILE_EXISTS: Errno : 80;
|
||||
ERROR_BROKEN_PIPE: Errno : 109;
|
||||
ERROR_BUFFER_OVERFLOW: Errno : 111;
|
||||
ERROR_INSUFFICIENT_BUFFER: Errno : 122;
|
||||
ERROR_MOD_NOT_FOUND: Errno : 126;
|
||||
ERROR_PROC_NOT_FOUND: Errno : 127;
|
||||
ERROR_DIR_NOT_EMPTY: Errno : 145;
|
||||
ERROR_ALREADY_EXISTS: Errno : 183;
|
||||
ERROR_ENVVAR_NOT_FOUND: Errno : 203;
|
||||
ERROR_MORE_DATA: Errno : 234;
|
||||
ERROR_OPERATION_ABORTED: Errno : 995;
|
||||
ERROR_IO_PENDING: Errno : 997;
|
||||
ERROR_NOT_FOUND: Errno : 1168;
|
||||
ERROR_PRIVILEGE_NOT_HELD: Errno : 1314;
|
||||
WSAEACCES: Errno : 10013;
|
||||
WSAECONNRESET: Errno : 10054;
|
||||
|
||||
// Windows reserves errors >= 1<<29 for application use
|
||||
ERROR_FILE_IS_PIPE: Errno : 1<<29 + 0;
|
||||
|
||||
|
||||
|
||||
|
||||
open :: proc(path: string, mode: int, perm: u32) -> (Handle, Errno) {
|
||||
using win32;
|
||||
if path.count == 0 {
|
||||
return INVALID_HANDLE, ERROR_FILE_NOT_FOUND;
|
||||
}
|
||||
|
||||
access: u32;
|
||||
match mode & (O_RDONLY|O_WRONLY|O_RDWR) {
|
||||
case O_RDONLY: access = FILE_GENERIC_READ;
|
||||
case O_WRONLY: access = FILE_GENERIC_WRITE;
|
||||
case O_RDWR: access = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
|
||||
}
|
||||
|
||||
if mode&O_CREAT != 0 {
|
||||
access |= FILE_GENERIC_WRITE;
|
||||
}
|
||||
if mode&O_APPEND != 0 {
|
||||
access &~= FILE_GENERIC_WRITE;
|
||||
access |= FILE_APPEND_DATA;
|
||||
}
|
||||
|
||||
share_mode := cast(u32)(FILE_SHARE_READ|FILE_SHARE_WRITE);
|
||||
sa: ^SECURITY_ATTRIBUTES = nil;
|
||||
sa_inherit := SECURITY_ATTRIBUTES{length = size_of(SECURITY_ATTRIBUTES), inherit_handle = 1};
|
||||
if mode&O_CLOEXEC == 0 {
|
||||
sa = ^sa_inherit;
|
||||
}
|
||||
|
||||
create_mode: u32;
|
||||
match {
|
||||
case mode&(O_CREAT|O_EXCL) == (O_CREAT | O_EXCL):
|
||||
create_mode = CREATE_NEW;
|
||||
case mode&(O_CREAT|O_TRUNC) == (O_CREAT | O_TRUNC):
|
||||
create_mode = CREATE_ALWAYS;
|
||||
case mode&O_CREAT == O_CREAT:
|
||||
create_mode = OPEN_ALWAYS;
|
||||
case mode&O_TRUNC == O_TRUNC:
|
||||
create_mode = TRUNCATE_EXISTING;
|
||||
default:
|
||||
create_mode = OPEN_EXISTING;
|
||||
}
|
||||
|
||||
buf: [300]byte;
|
||||
copy(buf[:], cast([]byte)path);
|
||||
|
||||
handle := cast(Handle)CreateFileA(^buf[0], access, share_mode, sa, create_mode, FILE_ATTRIBUTE_NORMAL, nil);
|
||||
if handle != INVALID_HANDLE {
|
||||
return handle, ERROR_NONE;
|
||||
}
|
||||
err := GetLastError();
|
||||
return INVALID_HANDLE, cast(Errno)err;
|
||||
}
|
||||
|
||||
close :: proc(fd: Handle) {
|
||||
win32.CloseHandle(cast(win32.HANDLE)fd);
|
||||
}
|
||||
|
||||
write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
||||
bytes_written: i32;
|
||||
e := win32.WriteFile(cast(win32.HANDLE)fd, data.data, cast(i32)data.count, ^bytes_written, nil);
|
||||
if e == win32.FALSE {
|
||||
err := win32.GetLastError();
|
||||
return 0, cast(Errno)err;
|
||||
}
|
||||
return cast(int)bytes_written, ERROR_NONE;
|
||||
}
|
||||
|
||||
read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
||||
bytes_read: i32;
|
||||
e := win32.ReadFile(cast(win32.HANDLE)fd, data.data, cast(u32)data.count, ^bytes_read, nil);
|
||||
if e == win32.FALSE {
|
||||
err := win32.GetLastError();
|
||||
return 0, cast(Errno)err;
|
||||
}
|
||||
return cast(int)bytes_read, ERROR_NONE;
|
||||
}
|
||||
|
||||
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
|
||||
using win32;
|
||||
w: u32;
|
||||
match whence {
|
||||
case 0: w = FILE_BEGIN;
|
||||
case 1: w = FILE_CURRENT;
|
||||
case 2: w = FILE_END;
|
||||
}
|
||||
hi := cast(i32)(offset>>32);
|
||||
lo := cast(i32)(offset);
|
||||
ft := GetFileType(cast(HANDLE)fd);
|
||||
if ft == FILE_TYPE_PIPE {
|
||||
return 0, ERROR_FILE_IS_PIPE;
|
||||
}
|
||||
dw_ptr := SetFilePointer(cast(HANDLE)fd, lo, ^hi, w);
|
||||
if dw_ptr == INVALID_SET_FILE_POINTER {
|
||||
err := GetLastError();
|
||||
return 0, cast(Errno)err;
|
||||
}
|
||||
return cast(i64)hi<<32 + cast(i64)dw_ptr, ERROR_NONE;
|
||||
}
|
||||
|
||||
|
||||
// NOTE(bill): Uses startup to initialize it
|
||||
stdin := get_std_handle(win32.STD_INPUT_HANDLE);
|
||||
stdout := get_std_handle(win32.STD_OUTPUT_HANDLE);
|
||||
stderr := get_std_handle(win32.STD_ERROR_HANDLE);
|
||||
|
||||
|
||||
get_std_handle :: proc(h: int) -> Handle {
|
||||
fd := win32.GetStdHandle(cast(i32)h);
|
||||
win32.SetHandleInformation(fd, win32.HANDLE_FLAG_INHERIT, 0);
|
||||
return cast(Handle)fd;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
last_write_time :: proc(fd: Handle) -> File_Time {
|
||||
file_info: win32.BY_HANDLE_FILE_INFORMATION;
|
||||
win32.GetFileInformationByHandle(cast(win32.HANDLE)fd, ^file_info);
|
||||
lo := cast(File_Time)file_info.last_write_time.lo;
|
||||
hi := cast(File_Time)file_info.last_write_time.hi;
|
||||
return lo | hi << 32;
|
||||
}
|
||||
|
||||
last_write_time_by_name :: proc(name: string) -> File_Time {
|
||||
last_write_time: win32.FILETIME;
|
||||
data: win32.FILE_ATTRIBUTE_DATA;
|
||||
buf: [1024]byte;
|
||||
|
||||
assert(buf.count > name.count);
|
||||
|
||||
copy(buf[:], cast([]byte)name);
|
||||
|
||||
if win32.GetFileAttributesExA(^buf[0], win32.GetFileExInfoStandard, ^data) != 0 {
|
||||
last_write_time = data.last_write_time;
|
||||
}
|
||||
|
||||
l := cast(File_Time)last_write_time.lo;
|
||||
h := cast(File_Time)last_write_time.hi;
|
||||
return l | h << 32;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
read_entire_file :: proc(name: string) -> ([]byte, bool) {
|
||||
buf: [300]byte;
|
||||
copy(buf[:], cast([]byte)name);
|
||||
|
||||
fd, err := open(name, O_RDONLY, 0);
|
||||
if err != ERROR_NONE {
|
||||
return nil, false;
|
||||
}
|
||||
defer close(fd);
|
||||
|
||||
length: i64;
|
||||
file_size_ok := win32.GetFileSizeEx(cast(win32.HANDLE)fd, ^length) != 0;
|
||||
if !file_size_ok {
|
||||
return nil, false;
|
||||
}
|
||||
|
||||
data := new_slice(u8, length);
|
||||
if data.data == nil {
|
||||
return nil, false;
|
||||
}
|
||||
|
||||
single_read_length: i32;
|
||||
total_read: i64;
|
||||
|
||||
for total_read < length {
|
||||
remaining := length - total_read;
|
||||
to_read: u32;
|
||||
MAX :: 1<<32-1;
|
||||
if remaining <= MAX {
|
||||
to_read = cast(u32)remaining;
|
||||
} else {
|
||||
to_read = MAX;
|
||||
}
|
||||
|
||||
win32.ReadFile(cast(win32.HANDLE)fd, ^data[total_read], to_read, ^single_read_length, nil);
|
||||
if single_read_length <= 0 {
|
||||
free(data);
|
||||
return nil, false;
|
||||
}
|
||||
|
||||
total_read += cast(i64)single_read_length;
|
||||
}
|
||||
|
||||
return data, true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
heap_alloc :: proc(size: int) -> rawptr {
|
||||
return win32.HeapAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, size);
|
||||
}
|
||||
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
|
||||
return win32.HeapReAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, ptr, new_size);
|
||||
}
|
||||
heap_free :: proc(ptr: rawptr) {
|
||||
win32.HeapFree(win32.GetProcessHeap(), 0, ptr);
|
||||
}
|
||||
|
||||
|
||||
exit :: proc(code: int) {
|
||||
win32.ExitProcess(cast(u32)code);
|
||||
}
|
||||
|
||||
|
||||
|
||||
current_thread_id :: proc() -> int {
|
||||
return cast(int)win32.GetCurrentThreadId();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
#import win32 "sys/windows.odin" when ODIN_OS == "windows";
|
||||
#import "atomic.odin";
|
||||
|
||||
Semaphore :: struct {
|
||||
handle: win32.HANDLE,
|
||||
}
|
||||
|
||||
Mutex :: struct {
|
||||
semaphore: Semaphore,
|
||||
counter: i32,
|
||||
owner: i32,
|
||||
recursion: i32,
|
||||
}
|
||||
|
||||
current_thread_id :: proc() -> i32 {
|
||||
return cast(i32)win32.GetCurrentThreadId();
|
||||
}
|
||||
|
||||
semaphore_init :: proc(s: ^Semaphore) {
|
||||
s.handle = win32.CreateSemaphoreA(nil, 0, 1<<31-1, nil);
|
||||
}
|
||||
|
||||
semaphore_destroy :: proc(s: ^Semaphore) {
|
||||
win32.CloseHandle(s.handle);
|
||||
}
|
||||
|
||||
semaphore_post :: proc(s: ^Semaphore, count: int) {
|
||||
win32.ReleaseSemaphore(s.handle, cast(i32)count, nil);
|
||||
}
|
||||
|
||||
semaphore_release :: proc(s: ^Semaphore) #inline { semaphore_post(s, 1); }
|
||||
|
||||
semaphore_wait :: proc(s: ^Semaphore) {
|
||||
win32.WaitForSingleObject(s.handle, win32.INFINITE);
|
||||
}
|
||||
|
||||
|
||||
mutex_init :: proc(m: ^Mutex) {
|
||||
atomic.store(^m.counter, 0);
|
||||
atomic.store(^m.owner, current_thread_id());
|
||||
semaphore_init(^m.semaphore);
|
||||
m.recursion = 0;
|
||||
}
|
||||
mutex_destroy :: proc(m: ^Mutex) {
|
||||
semaphore_destroy(^m.semaphore);
|
||||
}
|
||||
mutex_lock :: proc(m: ^Mutex) {
|
||||
thread_id := current_thread_id();
|
||||
if atomic.fetch_add(^m.counter, 1) > 0 {
|
||||
if thread_id != atomic.load(^m.owner) {
|
||||
semaphore_wait(^m.semaphore);
|
||||
}
|
||||
}
|
||||
atomic.store(^m.owner, thread_id);
|
||||
m.recursion += 1;
|
||||
}
|
||||
mutex_try_lock :: proc(m: ^Mutex) -> bool {
|
||||
thread_id := current_thread_id();
|
||||
if atomic.load(^m.owner) == thread_id {
|
||||
atomic.fetch_add(^m.counter, 1);
|
||||
} else {
|
||||
expected: i32 = 0;
|
||||
if atomic.load(^m.counter) != 0 {
|
||||
return false;
|
||||
}
|
||||
if atomic.compare_exchange(^m.counter, expected, 1) == 0 {
|
||||
return false;
|
||||
}
|
||||
atomic.store(^m.owner, thread_id);
|
||||
}
|
||||
m.recursion += 1;
|
||||
return true;
|
||||
}
|
||||
mutex_unlock :: proc(m: ^Mutex) {
|
||||
recursion: i32;
|
||||
thread_id := current_thread_id();
|
||||
assert(thread_id == atomic.load(^m.owner));
|
||||
|
||||
m.recursion -= 1;
|
||||
recursion = m.recursion;
|
||||
if recursion == 0 {
|
||||
atomic.store(^m.owner, thread_id);
|
||||
}
|
||||
|
||||
if atomic.fetch_add(^m.counter, -1) > 1 {
|
||||
if recursion == 0 {
|
||||
semaphore_release(^m.semaphore);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,566 @@
|
||||
#foreign_system_library "kernel32.lib";
|
||||
#foreign_system_library "user32.lib";
|
||||
#foreign_system_library "gdi32.lib";
|
||||
#foreign_system_library "winmm.lib";
|
||||
#foreign_system_library "opengl32.lib";
|
||||
|
||||
HANDLE :: rawptr;
|
||||
HWND :: HANDLE;
|
||||
HDC :: HANDLE;
|
||||
HINSTANCE :: HANDLE;
|
||||
HICON :: HANDLE;
|
||||
HCURSOR :: HANDLE;
|
||||
HMENU :: HANDLE;
|
||||
HBRUSH :: HANDLE;
|
||||
HGDIOBJ :: HANDLE;
|
||||
HMODULE :: HANDLE;
|
||||
WPARAM :: uint;
|
||||
LPARAM :: int;
|
||||
LRESULT :: int;
|
||||
ATOM :: i16;
|
||||
BOOL :: i32;
|
||||
WNDPROC :: type proc(HWND, u32, WPARAM, LPARAM) -> LRESULT #cc_c;
|
||||
|
||||
|
||||
INVALID_HANDLE_VALUE :: cast(HANDLE)(~cast(int)0);
|
||||
|
||||
FALSE: BOOL : 0;
|
||||
TRUE: BOOL : 1;
|
||||
|
||||
CS_VREDRAW :: 0x0001;
|
||||
CS_HREDRAW :: 0x0002;
|
||||
CS_OWNDC :: 0x0020;
|
||||
CW_USEDEFAULT :: -0x80000000;
|
||||
|
||||
WS_OVERLAPPED :: 0;
|
||||
WS_MAXIMIZEBOX :: 0x00010000;
|
||||
WS_MINIMIZEBOX :: 0x00020000;
|
||||
WS_THICKFRAME :: 0x00040000;
|
||||
WS_SYSMENU :: 0x00080000;
|
||||
WS_CAPTION :: 0x00C00000;
|
||||
WS_VISIBLE :: 0x10000000;
|
||||
WS_OVERLAPPEDWINDOW :: WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX;
|
||||
|
||||
WM_DESTROY :: 0x0002;
|
||||
WM_CLOSE :: 0x0010;
|
||||
WM_QUIT :: 0x0012;
|
||||
WM_KEYDOWN :: 0x0100;
|
||||
WM_KEYUP :: 0x0101;
|
||||
|
||||
PM_REMOVE :: 1;
|
||||
|
||||
COLOR_BACKGROUND :: cast(HBRUSH)(cast(int)1);
|
||||
BLACK_BRUSH :: 4;
|
||||
|
||||
SM_CXSCREEN :: 0;
|
||||
SM_CYSCREEN :: 1;
|
||||
|
||||
SW_SHOW :: 5;
|
||||
|
||||
|
||||
POINT :: struct #ordered {
|
||||
x, y: i32,
|
||||
}
|
||||
|
||||
WNDCLASSEXA :: struct #ordered {
|
||||
size, style: u32,
|
||||
wnd_proc: WNDPROC,
|
||||
cls_extra, wnd_extra: i32,
|
||||
instance: HINSTANCE,
|
||||
icon: HICON,
|
||||
cursor: HCURSOR,
|
||||
background: HBRUSH,
|
||||
menu_name, class_name: ^u8,
|
||||
sm: HICON,
|
||||
}
|
||||
|
||||
MSG :: struct #ordered {
|
||||
hwnd: HWND,
|
||||
message: u32,
|
||||
wparam: WPARAM,
|
||||
lparam: LPARAM,
|
||||
time: u32,
|
||||
pt: POINT,
|
||||
}
|
||||
|
||||
RECT :: struct #ordered {
|
||||
left: i32,
|
||||
top: i32,
|
||||
right: i32,
|
||||
bottom: i32,
|
||||
}
|
||||
|
||||
FILETIME :: struct #ordered {
|
||||
lo, hi: u32,
|
||||
}
|
||||
|
||||
SYSTEMTIME :: struct #ordered {
|
||||
year, month: u16,
|
||||
day_of_week, day: u16,
|
||||
hour, minute, second, millisecond: u16,
|
||||
}
|
||||
|
||||
BY_HANDLE_FILE_INFORMATION :: struct #ordered {
|
||||
file_attributes: u32,
|
||||
creation_time,
|
||||
last_access_time,
|
||||
last_write_time: FILETIME,
|
||||
volume_serial_number,
|
||||
file_size_high,
|
||||
file_size_low,
|
||||
number_of_links,
|
||||
file_index_high,
|
||||
file_index_low: u32,
|
||||
}
|
||||
|
||||
FILE_ATTRIBUTE_DATA :: struct #ordered {
|
||||
file_attributes: u32,
|
||||
creation_time,
|
||||
last_access_time,
|
||||
last_write_time: FILETIME,
|
||||
file_size_high,
|
||||
file_size_low: u32,
|
||||
}
|
||||
|
||||
GET_FILEEX_INFO_LEVELS :: i32;
|
||||
|
||||
GetFileExInfoStandard: GET_FILEEX_INFO_LEVELS : 0;
|
||||
GetFileExMaxInfoLevel: GET_FILEEX_INFO_LEVELS : 1;
|
||||
|
||||
GetLastError :: proc() -> i32 #foreign kernel32;
|
||||
ExitProcess :: proc(exit_code: u32) #foreign kernel32;
|
||||
GetDesktopWindow :: proc() -> HWND #foreign user32;
|
||||
GetCursorPos :: proc(p: ^POINT) -> i32 #foreign user32;
|
||||
ScreenToClient :: proc(h: HWND, p: ^POINT) -> i32 #foreign user32;
|
||||
GetModuleHandleA :: proc(module_name: ^u8) -> HINSTANCE #foreign kernel32;
|
||||
GetStockObject :: proc(fn_object: i32) -> HGDIOBJ #foreign gdi32;
|
||||
PostQuitMessage :: proc(exit_code: i32) #foreign user32;
|
||||
SetWindowTextA :: proc(hwnd: HWND, c_string: ^u8) -> BOOL #foreign user32;
|
||||
|
||||
QueryPerformanceFrequency :: proc(result: ^i64) -> i32 #foreign kernel32;
|
||||
QueryPerformanceCounter :: proc(result: ^i64) -> i32 #foreign kernel32;
|
||||
|
||||
Sleep :: proc(ms: i32) -> i32 #foreign kernel32;
|
||||
|
||||
OutputDebugStringA :: proc(c_str: ^u8) #foreign kernel32;
|
||||
|
||||
|
||||
RegisterClassExA :: proc(wc: ^WNDCLASSEXA) -> ATOM #foreign user32;
|
||||
CreateWindowExA :: proc(ex_style: u32,
|
||||
class_name, title: ^u8,
|
||||
style: u32,
|
||||
x, y, w, h: i32,
|
||||
parent: HWND, menu: HMENU, instance: HINSTANCE,
|
||||
param: rawptr) -> HWND #foreign user32;
|
||||
|
||||
ShowWindow :: proc(hwnd: HWND, cmd_show: i32) -> BOOL #foreign user32;
|
||||
TranslateMessage :: proc(msg: ^MSG) -> BOOL #foreign user32;
|
||||
DispatchMessageA :: proc(msg: ^MSG) -> LRESULT #foreign user32;
|
||||
UpdateWindow :: proc(hwnd: HWND) -> BOOL #foreign user32;
|
||||
PeekMessageA :: proc(msg: ^MSG, hwnd: HWND,
|
||||
msg_filter_min, msg_filter_max, remove_msg: u32) -> BOOL #foreign user32;
|
||||
|
||||
DefWindowProcA :: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT #foreign user32;
|
||||
|
||||
AdjustWindowRect :: proc(rect: ^RECT, style: u32, menu: BOOL) -> BOOL #foreign user32;
|
||||
GetActiveWindow :: proc() -> HWND #foreign user32;
|
||||
|
||||
|
||||
GetQueryPerformanceFrequency :: proc() -> i64 {
|
||||
r: i64;
|
||||
QueryPerformanceFrequency(^r);
|
||||
return r;
|
||||
}
|
||||
|
||||
GetCommandLineA :: proc() -> ^u8 #foreign kernel32;
|
||||
GetSystemMetrics :: proc(index: i32) -> i32 #foreign kernel32;
|
||||
GetCurrentThreadId :: proc() -> u32 #foreign kernel32;
|
||||
|
||||
timeGetTime :: proc() -> u32 #foreign winmm;
|
||||
GetSystemTimeAsFileTime :: proc(system_time_as_file_time: ^FILETIME) #foreign kernel32;
|
||||
FileTimeToLocalFileTime :: proc(file_time: ^FILETIME, local_file_time: ^FILETIME) -> BOOL #foreign kernel32;
|
||||
FileTimeToSystemTime :: proc(file_time: ^FILETIME, system_time: ^SYSTEMTIME) -> BOOL #foreign kernel32;
|
||||
SystemTimeToFileTime :: proc(system_time: ^SYSTEMTIME, file_time: ^FILETIME) -> BOOL #foreign kernel32;
|
||||
|
||||
// File Stuff
|
||||
|
||||
CloseHandle :: proc(h: HANDLE) -> i32 #foreign kernel32;
|
||||
GetStdHandle :: proc(h: i32) -> HANDLE #foreign kernel32;
|
||||
CreateFileA :: proc(filename: ^u8, desired_access, share_mode: u32,
|
||||
security: rawptr,
|
||||
creation, flags_and_attribs: u32, template_file: HANDLE) -> HANDLE #foreign kernel32;
|
||||
ReadFile :: proc(h: HANDLE, buf: rawptr, to_read: u32, bytes_read: ^i32, overlapped: rawptr) -> BOOL #foreign kernel32;
|
||||
WriteFile :: proc(h: HANDLE, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> BOOL #foreign kernel32;
|
||||
|
||||
GetFileSizeEx :: proc(file_handle: HANDLE, file_size: ^i64) -> BOOL #foreign kernel32;
|
||||
GetFileAttributesExA :: proc(filename: ^u8, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: rawptr) -> BOOL #foreign kernel32;
|
||||
GetFileInformationByHandle :: proc(file_handle: HANDLE, file_info: ^BY_HANDLE_FILE_INFORMATION) -> BOOL #foreign kernel32;
|
||||
|
||||
GetFileType :: proc(file_handle: HANDLE) -> u32 #foreign kernel32;
|
||||
SetFilePointer :: proc(file_handle: HANDLE, distance_to_move: i32, distance_to_move_high: ^i32, move_method: u32) -> u32 #foreign kernel32;
|
||||
|
||||
SetHandleInformation :: proc(obj: HANDLE, mask, flags: u32) -> BOOL #foreign kernel32;
|
||||
|
||||
HANDLE_FLAG_INHERIT :: 1;
|
||||
HANDLE_FLAG_PROTECT_FROM_CLOSE :: 2;
|
||||
|
||||
|
||||
FILE_BEGIN :: 0;
|
||||
FILE_CURRENT :: 1;
|
||||
FILE_END :: 2;
|
||||
|
||||
FILE_SHARE_READ :: 0x00000001;
|
||||
FILE_SHARE_WRITE :: 0x00000002;
|
||||
FILE_SHARE_DELETE :: 0x00000004;
|
||||
FILE_GENERIC_ALL :: 0x10000000;
|
||||
FILE_GENERIC_EXECUTE :: 0x20000000;
|
||||
FILE_GENERIC_WRITE :: 0x40000000;
|
||||
FILE_GENERIC_READ :: 0x80000000;
|
||||
|
||||
FILE_APPEND_DATA :: 0x0004;
|
||||
|
||||
STD_INPUT_HANDLE :: -10;
|
||||
STD_OUTPUT_HANDLE :: -11;
|
||||
STD_ERROR_HANDLE :: -12;
|
||||
|
||||
CREATE_NEW :: 1;
|
||||
CREATE_ALWAYS :: 2;
|
||||
OPEN_EXISTING :: 3;
|
||||
OPEN_ALWAYS :: 4;
|
||||
TRUNCATE_EXISTING :: 5;
|
||||
|
||||
FILE_ATTRIBUTE_READONLY :: 0x00000001;
|
||||
FILE_ATTRIBUTE_HIDDEN :: 0x00000002;
|
||||
FILE_ATTRIBUTE_SYSTEM :: 0x00000004;
|
||||
FILE_ATTRIBUTE_DIRECTORY :: 0x00000010;
|
||||
FILE_ATTRIBUTE_ARCHIVE :: 0x00000020;
|
||||
FILE_ATTRIBUTE_DEVICE :: 0x00000040;
|
||||
FILE_ATTRIBUTE_NORMAL :: 0x00000080;
|
||||
FILE_ATTRIBUTE_TEMPORARY :: 0x00000100;
|
||||
FILE_ATTRIBUTE_SPARSE_FILE :: 0x00000200;
|
||||
FILE_ATTRIBUTE_REPARSE_POINT :: 0x00000400;
|
||||
FILE_ATTRIBUTE_COMPRESSED :: 0x00000800;
|
||||
FILE_ATTRIBUTE_OFFLINE :: 0x00001000;
|
||||
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED :: 0x00002000;
|
||||
FILE_ATTRIBUTE_ENCRYPTED :: 0x00004000;
|
||||
|
||||
FILE_TYPE_DISK :: 0x0001;
|
||||
FILE_TYPE_CHAR :: 0x0002;
|
||||
FILE_TYPE_PIPE :: 0x0003;
|
||||
|
||||
INVALID_SET_FILE_POINTER :: ~cast(u32)0;
|
||||
|
||||
|
||||
|
||||
|
||||
HeapAlloc :: proc (h: HANDLE, flags: u32, bytes: int) -> rawptr #foreign kernel32;
|
||||
HeapReAlloc :: proc (h: HANDLE, flags: u32, memory: rawptr, bytes: int) -> rawptr #foreign kernel32;
|
||||
HeapFree :: proc (h: HANDLE, flags: u32, memory: rawptr) -> BOOL #foreign kernel32;
|
||||
GetProcessHeap :: proc () -> HANDLE #foreign kernel32;
|
||||
|
||||
|
||||
HEAP_ZERO_MEMORY :: 0x00000008;
|
||||
|
||||
// Synchronization
|
||||
|
||||
SECURITY_ATTRIBUTES :: struct #ordered {
|
||||
length: u32,
|
||||
security_descriptor: rawptr,
|
||||
inherit_handle: BOOL,
|
||||
}
|
||||
|
||||
INFINITE :: 0xffffffff;
|
||||
|
||||
CreateSemaphoreA :: proc(attributes: ^SECURITY_ATTRIBUTES, initial_count, maximum_count: i32, name: ^byte) -> HANDLE #foreign kernel32;
|
||||
ReleaseSemaphore :: proc(semaphore: HANDLE, release_count: i32, previous_count: ^i32) -> BOOL #foreign kernel32;
|
||||
WaitForSingleObject :: proc(handle: HANDLE, milliseconds: u32) -> u32 #foreign kernel32;
|
||||
|
||||
|
||||
InterlockedCompareExchange :: proc(dst: ^i32, exchange, comparand: i32) -> i32 #foreign kernel32;
|
||||
InterlockedExchange :: proc(dst: ^i32, desired: i32) -> i32 #foreign kernel32;
|
||||
InterlockedExchangeAdd :: proc(dst: ^i32, desired: i32) -> i32 #foreign kernel32;
|
||||
InterlockedAnd :: proc(dst: ^i32, desired: i32) -> i32 #foreign kernel32;
|
||||
InterlockedOr :: proc(dst: ^i32, desired: i32) -> i32 #foreign kernel32;
|
||||
|
||||
InterlockedCompareExchange64 :: proc(dst: ^i64, exchange, comparand: i64) -> i64 #foreign kernel32;
|
||||
InterlockedExchange64 :: proc(dst: ^i64, desired: i64) -> i64 #foreign kernel32;
|
||||
InterlockedExchangeAdd64 :: proc(dst: ^i64, desired: i64) -> i64 #foreign kernel32;
|
||||
InterlockedAnd64 :: proc(dst: ^i64, desired: i64) -> i64 #foreign kernel32;
|
||||
InterlockedOr64 :: proc(dst: ^i64, desired: i64) -> i64 #foreign kernel32;
|
||||
|
||||
_mm_pause :: proc() #foreign kernel32;
|
||||
ReadWriteBarrier :: proc() #foreign kernel32;
|
||||
WriteBarrier :: proc() #foreign kernel32;
|
||||
ReadBarrier :: proc() #foreign kernel32;
|
||||
|
||||
|
||||
|
||||
BITMAPINFOHEADER :: struct #ordered {
|
||||
size: u32,
|
||||
width, height: i32,
|
||||
planes, bit_count: i16,
|
||||
compression: u32,
|
||||
size_image: u32,
|
||||
x_pels_per_meter: i32,
|
||||
y_pels_per_meter: i32,
|
||||
clr_used: u32,
|
||||
clr_important: u32,
|
||||
}
|
||||
BITMAPINFO :: struct #ordered {
|
||||
using header: BITMAPINFOHEADER,
|
||||
colors: [1]RGBQUAD,
|
||||
}
|
||||
|
||||
|
||||
RGBQUAD :: struct #ordered { blue, green, red, reserved: byte }
|
||||
|
||||
BI_RGB :: 0;
|
||||
DIB_RGB_COLORS :: 0x00;
|
||||
SRCCOPY: u32 : 0x00cc0020;
|
||||
|
||||
|
||||
StretchDIBits :: 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 #foreign gdi32;
|
||||
|
||||
|
||||
|
||||
LoadLibraryA :: proc (c_str: ^u8) -> HMODULE #foreign kernel32;
|
||||
FreeLibrary :: proc (h: HMODULE) #foreign kernel32;
|
||||
GetProcAddress :: proc (h: HMODULE, c_str: ^u8) -> PROC #foreign kernel32;
|
||||
|
||||
GetClientRect :: proc(hwnd: HWND, rect: ^RECT) -> BOOL #foreign user32;
|
||||
|
||||
// Windows OpenGL
|
||||
PFD_TYPE_RGBA :: 0;
|
||||
PFD_TYPE_COLORINDEX :: 1;
|
||||
PFD_MAIN_PLANE :: 0;
|
||||
PFD_OVERLAY_PLANE :: 1;
|
||||
PFD_UNDERLAY_PLANE :: -1;
|
||||
PFD_DOUBLEBUFFER :: 1;
|
||||
PFD_STEREO :: 2;
|
||||
PFD_DRAW_TO_WINDOW :: 4;
|
||||
PFD_DRAW_TO_BITMAP :: 8;
|
||||
PFD_SUPPORT_GDI :: 16;
|
||||
PFD_SUPPORT_OPENGL :: 32;
|
||||
PFD_GENERIC_FORMAT :: 64;
|
||||
PFD_NEED_PALETTE :: 128;
|
||||
PFD_NEED_SYSTEM_PALETTE :: 0x00000100;
|
||||
PFD_SWAP_EXCHANGE :: 0x00000200;
|
||||
PFD_SWAP_COPY :: 0x00000400;
|
||||
PFD_SWAP_LAYER_BUFFERS :: 0x00000800;
|
||||
PFD_GENERIC_ACCELERATED :: 0x00001000;
|
||||
PFD_DEPTH_DONTCARE :: 0x20000000;
|
||||
PFD_DOUBLEBUFFER_DONTCARE :: 0x40000000;
|
||||
PFD_STEREO_DONTCARE :: 0x80000000;
|
||||
|
||||
HGLRC :: HANDLE;
|
||||
PROC :: type proc() #cc_c;
|
||||
wglCreateContextAttribsARBType :: type proc(hdc: HDC, hshareContext: rawptr, attribList: ^i32) -> HGLRC;
|
||||
|
||||
|
||||
PIXELFORMATDESCRIPTOR :: struct #ordered {
|
||||
size,
|
||||
version,
|
||||
flags: u32,
|
||||
|
||||
pixel_type,
|
||||
color_bits,
|
||||
red_bits,
|
||||
red_shift,
|
||||
green_bits,
|
||||
green_shift,
|
||||
blue_bits,
|
||||
blue_shift,
|
||||
alpha_bits,
|
||||
alpha_shift,
|
||||
accum_bits,
|
||||
accum_red_bits,
|
||||
accum_green_bits,
|
||||
accum_blue_bits,
|
||||
accum_alpha_bits,
|
||||
depth_bits,
|
||||
stencil_bits,
|
||||
aux_buffers,
|
||||
layer_type,
|
||||
reserved: byte,
|
||||
|
||||
layer_mask,
|
||||
visible_mask,
|
||||
damage_mask: u32,
|
||||
}
|
||||
|
||||
GetDC :: proc(h: HANDLE) -> HDC #foreign user32;
|
||||
SetPixelFormat :: proc(hdc: HDC, pixel_format: i32, pfd: ^PIXELFORMATDESCRIPTOR ) -> BOOL #foreign user32;
|
||||
ChoosePixelFormat :: proc(hdc: HDC, pfd: ^PIXELFORMATDESCRIPTOR) -> i32 #foreign user32;
|
||||
SwapBuffers :: proc(hdc: HDC) -> BOOL #foreign user32;
|
||||
ReleaseDC :: proc(wnd: HWND, hdc: HDC) -> i32 #foreign user32;
|
||||
|
||||
WGL_CONTEXT_MAJOR_VERSION_ARB :: 0x2091;
|
||||
WGL_CONTEXT_MINOR_VERSION_ARB :: 0x2092;
|
||||
WGL_CONTEXT_PROFILE_MASK_ARB :: 0x9126;
|
||||
WGL_CONTEXT_CORE_PROFILE_BIT_ARB :: 0x0001;
|
||||
WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB :: 0x0002;
|
||||
|
||||
wglCreateContext :: proc(hdc: HDC) -> HGLRC #foreign opengl32;
|
||||
wglMakeCurrent :: proc(hdc: HDC, hglrc: HGLRC) -> BOOL #foreign opengl32;
|
||||
wglGetProcAddress :: proc(c_str: ^u8) -> PROC #foreign opengl32;
|
||||
wglDeleteContext :: proc(hglrc: HGLRC) -> BOOL #foreign opengl32;
|
||||
|
||||
|
||||
|
||||
GetKeyState :: proc(v_key: i32) -> i16 #foreign user32;
|
||||
GetAsyncKeyState :: proc(v_key: i32) -> i16 #foreign user32;
|
||||
|
||||
is_key_down :: proc(key: Key_Code) -> bool #inline { return GetAsyncKeyState(cast(i32)key) < 0; }
|
||||
|
||||
Key_Code :: enum i32 {
|
||||
LBUTTON = 0x01,
|
||||
RBUTTON = 0x02,
|
||||
CANCEL = 0x03,
|
||||
MBUTTON = 0x04,
|
||||
BACK = 0x08,
|
||||
TAB = 0x09,
|
||||
CLEAR = 0x0C,
|
||||
RETURN = 0x0D,
|
||||
|
||||
SHIFT = 0x10,
|
||||
CONTROL = 0x11,
|
||||
MENU = 0x12,
|
||||
PAUSE = 0x13,
|
||||
CAPITAL = 0x14,
|
||||
KANA = 0x15,
|
||||
HANGEUL = 0x15,
|
||||
HANGUL = 0x15,
|
||||
JUNJA = 0x17,
|
||||
FINAL = 0x18,
|
||||
HANJA = 0x19,
|
||||
KANJI = 0x19,
|
||||
ESCAPE = 0x1B,
|
||||
CONVERT = 0x1C,
|
||||
NONCONVERT = 0x1D,
|
||||
ACCEPT = 0x1E,
|
||||
MODECHANGE = 0x1F,
|
||||
SPACE = 0x20,
|
||||
PRIOR = 0x21,
|
||||
NEXT = 0x22,
|
||||
END = 0x23,
|
||||
HOME = 0x24,
|
||||
LEFT = 0x25,
|
||||
UP = 0x26,
|
||||
RIGHT = 0x27,
|
||||
DOWN = 0x28,
|
||||
SELECT = 0x29,
|
||||
PRINT = 0x2A,
|
||||
EXECUTE = 0x2B,
|
||||
SNAPSHOT = 0x2C,
|
||||
INSERT = 0x2D,
|
||||
DELETE = 0x2E,
|
||||
HELP = 0x2F,
|
||||
|
||||
NUM0 = '0',
|
||||
NUM1 = '1',
|
||||
NUM2 = '2',
|
||||
NUM3 = '3',
|
||||
NUM4 = '4',
|
||||
NUM5 = '5',
|
||||
NUM6 = '6',
|
||||
NUM7 = '7',
|
||||
NUM8 = '8',
|
||||
NUM9 = '9',
|
||||
A = 'A',
|
||||
B = 'B',
|
||||
C = 'C',
|
||||
D = 'D',
|
||||
E = 'E',
|
||||
F = 'F',
|
||||
G = 'G',
|
||||
H = 'H',
|
||||
I = 'I',
|
||||
J = 'J',
|
||||
K = 'K',
|
||||
L = 'L',
|
||||
M = 'M',
|
||||
N = 'N',
|
||||
O = 'O',
|
||||
P = 'P',
|
||||
Q = 'Q',
|
||||
R = 'R',
|
||||
S = 'S',
|
||||
T = 'T',
|
||||
U = 'U',
|
||||
V = 'V',
|
||||
W = 'W',
|
||||
X = 'X',
|
||||
Y = 'Y',
|
||||
Z = 'Z',
|
||||
|
||||
LWIN = 0x5B,
|
||||
RWIN = 0x5C,
|
||||
APPS = 0x5D,
|
||||
|
||||
NUMPAD0 = 0x60,
|
||||
NUMPAD1 = 0x61,
|
||||
NUMPAD2 = 0x62,
|
||||
NUMPAD3 = 0x63,
|
||||
NUMPAD4 = 0x64,
|
||||
NUMPAD5 = 0x65,
|
||||
NUMPAD6 = 0x66,
|
||||
NUMPAD7 = 0x67,
|
||||
NUMPAD8 = 0x68,
|
||||
NUMPAD9 = 0x69,
|
||||
MULTIPLY = 0x6A,
|
||||
ADD = 0x6B,
|
||||
SEPARATOR = 0x6C,
|
||||
SUBTRACT = 0x6D,
|
||||
DECIMAL = 0x6E,
|
||||
DIVIDE = 0x6F,
|
||||
|
||||
F1 = 0x70,
|
||||
F2 = 0x71,
|
||||
F3 = 0x72,
|
||||
F4 = 0x73,
|
||||
F5 = 0x74,
|
||||
F6 = 0x75,
|
||||
F7 = 0x76,
|
||||
F8 = 0x77,
|
||||
F9 = 0x78,
|
||||
F10 = 0x79,
|
||||
F11 = 0x7A,
|
||||
F12 = 0x7B,
|
||||
F13 = 0x7C,
|
||||
F14 = 0x7D,
|
||||
F15 = 0x7E,
|
||||
F16 = 0x7F,
|
||||
F17 = 0x80,
|
||||
F18 = 0x81,
|
||||
F19 = 0x82,
|
||||
F20 = 0x83,
|
||||
F21 = 0x84,
|
||||
F22 = 0x85,
|
||||
F23 = 0x86,
|
||||
F24 = 0x87,
|
||||
|
||||
NUMLOCK = 0x90,
|
||||
SCROLL = 0x91,
|
||||
LSHIFT = 0xA0,
|
||||
RSHIFT = 0xA1,
|
||||
LCONTROL = 0xA2,
|
||||
RCONTROL = 0xA3,
|
||||
LMENU = 0xA4,
|
||||
RMENU = 0xA5,
|
||||
PROCESSKEY = 0xE5,
|
||||
ATTN = 0xF6,
|
||||
CRSEL = 0xF7,
|
||||
EXSEL = 0xF8,
|
||||
EREOF = 0xF9,
|
||||
PLAY = 0xFA,
|
||||
ZOOM = 0xFB,
|
||||
NONAME = 0xFC,
|
||||
PA1 = 0xFD,
|
||||
OEM_CLEAR = 0xFE,
|
||||
}
|
||||
|
||||
+120
-110
@@ -1,28 +1,44 @@
|
||||
RUNE_ERROR :: '\ufffd'
|
||||
RUNE_SELF :: 0x80
|
||||
RUNE_BOM :: 0xfeff
|
||||
RUNE_EOF :: ~(0 as rune)
|
||||
MAX_RUNE :: '\U0010ffff'
|
||||
UTF_MAX :: 4
|
||||
RUNE_ERROR :: '\ufffd';
|
||||
RUNE_SELF :: 0x80;
|
||||
RUNE_BOM :: 0xfeff;
|
||||
RUNE_EOF :: ~cast(rune)0;
|
||||
MAX_RUNE :: '\U0010ffff';
|
||||
UTF_MAX :: 4;
|
||||
|
||||
SURROGATE_MIN :: 0xd800;
|
||||
SURROGATE_MAX :: 0xdfff;
|
||||
|
||||
SURROGATE_MIN :: 0xd800
|
||||
SURROGATE_MAX :: 0xdfff
|
||||
T1 :: 0b0000_0000;
|
||||
TX :: 0b1000_0000;
|
||||
T2 :: 0b1100_0000;
|
||||
T3 :: 0b1110_0000;
|
||||
T4 :: 0b1111_0000;
|
||||
T5 :: 0b1111_1000;
|
||||
|
||||
MASKX :: 0b0011_1111;
|
||||
MASK2 :: 0b0001_1111;
|
||||
MASK3 :: 0b0000_1111;
|
||||
MASK4 :: 0b0000_0111;
|
||||
|
||||
Accept_Range :: struct {
|
||||
lo, hi: u8
|
||||
}
|
||||
RUNE1_MAX :: 1<<7 - 1;
|
||||
RUNE2_MAX :: 1<<11 - 1;
|
||||
RUNE3_MAX :: 1<<16 - 1;
|
||||
|
||||
accept_ranges := [5]Accept_Range{
|
||||
// The default lowest and highest continuation byte.
|
||||
LOCB :: 0b1000_0000;
|
||||
HICB :: 0b1011_1111;
|
||||
|
||||
Accept_Range :: struct { lo, hi: u8 }
|
||||
|
||||
immutable accept_ranges := [5]Accept_Range{
|
||||
{0x80, 0xbf},
|
||||
{0xa0, 0xbf},
|
||||
{0x80, 0x9f},
|
||||
{0x90, 0xbf},
|
||||
{0x80, 0x8f},
|
||||
}
|
||||
};
|
||||
|
||||
accept_sizes := [256]byte{
|
||||
immutable accept_sizes := [256]byte{
|
||||
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x00-0x0f
|
||||
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x10-0x1f
|
||||
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x20-0x2f
|
||||
@@ -40,177 +56,171 @@ accept_sizes := [256]byte{
|
||||
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, // 0xd0-0xdf
|
||||
0x13, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x23, 0x03, 0x03, // 0xe0-0xef
|
||||
0x34, 0x04, 0x04, 0x04, 0x44, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xf0-0xff
|
||||
}
|
||||
};
|
||||
|
||||
encode_rune :: proc(r_: rune) -> ([4]byte, int) {
|
||||
r := r_
|
||||
buf: [4]byte
|
||||
i := r as u32
|
||||
mask: byte : 0x3f
|
||||
encode_rune :: proc(r: rune) -> ([4]byte, int) {
|
||||
buf: [4]byte;
|
||||
i := cast(u32)r;
|
||||
mask: byte : 0x3f;
|
||||
if i <= 1<<7-1 {
|
||||
buf[0] = r as byte
|
||||
return buf, 1
|
||||
buf[0] = cast(byte)r;
|
||||
return buf, 1;
|
||||
}
|
||||
if i <= 1<<11-1 {
|
||||
buf[0] = 0xc0 | (r>>6) as byte
|
||||
buf[1] = 0x80 | (r) as byte & mask
|
||||
return buf, 2
|
||||
buf[0] = 0xc0 | cast(byte)(r>>6);
|
||||
buf[1] = 0x80 | cast(byte)r & mask;
|
||||
return buf, 2;
|
||||
}
|
||||
|
||||
// Invalid or Surrogate range
|
||||
if i > 0x0010ffff ||
|
||||
(0xd800 <= i && i <= 0xdfff) {
|
||||
r = 0xfffd
|
||||
r = 0xfffd;
|
||||
}
|
||||
|
||||
if i <= 1<<16-1 {
|
||||
buf[0] = 0xe0 | (r>>12) as byte
|
||||
buf[1] = 0x80 | (r>>6) as byte & mask
|
||||
buf[2] = 0x80 | (r) as byte & mask
|
||||
return buf, 3
|
||||
buf[0] = 0xe0 | cast(byte)(r>>12);
|
||||
buf[1] = 0x80 | cast(byte)(r>>6) & mask;
|
||||
buf[2] = 0x80 | cast(byte)r & mask;
|
||||
return buf, 3;
|
||||
}
|
||||
|
||||
buf[0] = 0xf0 | (r>>18) as byte
|
||||
buf[1] = 0x80 | (r>>12) as byte & mask
|
||||
buf[2] = 0x80 | (r>>6) as byte & mask
|
||||
buf[3] = 0x80 | (r) as byte & mask
|
||||
return buf, 4
|
||||
buf[0] = 0xf0 | cast(byte)(r>>18);
|
||||
buf[1] = 0x80 | cast(byte)(r>>12) & mask;
|
||||
buf[2] = 0x80 | cast(byte)(r>>6) & mask;
|
||||
buf[3] = 0x80 | cast(byte)r & mask;
|
||||
return buf, 4;
|
||||
}
|
||||
|
||||
decode_rune :: proc(s: string) -> (rune, int) {
|
||||
n := s.count
|
||||
n := s.count;
|
||||
if n < 1 {
|
||||
return RUNE_ERROR, 0
|
||||
return RUNE_ERROR, 0;
|
||||
}
|
||||
b0 := s[0]
|
||||
x := accept_sizes[b0]
|
||||
if x >= 0xf0 {
|
||||
mask := (x as rune << 31) >> 31 // all zeros or all ones
|
||||
return (b0 as rune) &~ mask | RUNE_ERROR&mask, 1
|
||||
s0 := s[0];
|
||||
x := accept_sizes[s0];
|
||||
if x >= 0xF0 {
|
||||
mask := cast(rune)(x) << 31 >> 31; // NOTE(bill): Create 0x0000 or 0xffff.
|
||||
return cast(rune)(s[0])&~mask | RUNE_ERROR&mask, 1;
|
||||
}
|
||||
size := x & 7
|
||||
ar := accept_ranges[x>>4]
|
||||
if n < size as int {
|
||||
return RUNE_ERROR, 1
|
||||
sz := x & 7;
|
||||
accept := accept_ranges[x>>4];
|
||||
if n < cast(int)sz {
|
||||
return RUNE_ERROR, 1;
|
||||
}
|
||||
b1 := s[1]
|
||||
if b1 < ar.lo || ar.hi < b1 {
|
||||
return RUNE_ERROR, 1
|
||||
b1 := s[1];
|
||||
if b1 < accept.lo || accept.hi < b1 {
|
||||
return RUNE_ERROR, 1;
|
||||
}
|
||||
|
||||
MASK_X :: 0b00111111
|
||||
MASK_2 :: 0b00011111
|
||||
MASK_3 :: 0b00001111
|
||||
MASK_4 :: 0b00000111
|
||||
|
||||
if size == 2 {
|
||||
return (b0&MASK_2) as rune <<6 | (b1&MASK_X) as rune, 2
|
||||
if sz == 2 {
|
||||
return cast(rune)(s0&MASK2)<<6 | cast(rune)(b1&MASKX), 2;
|
||||
}
|
||||
b2 := s[2]
|
||||
if b2 < 0x80 || 0xbf < b2 {
|
||||
return RUNE_ERROR, 1
|
||||
b2 := s[2];
|
||||
if b2 < LOCB || HICB < b2 {
|
||||
return RUNE_ERROR, 1;
|
||||
}
|
||||
if size == 3 {
|
||||
return (b0&MASK_3) as rune <<12 | (b1&MASK_X) as rune <<6 | (b2&MASK_X) as rune, 3
|
||||
if sz == 3 {
|
||||
return cast(rune)(s0&MASK3)<<12 | cast(rune)(b1&MASKX)<<6 | cast(rune)(b2&MASKX), 3;
|
||||
}
|
||||
b3 := s[3]
|
||||
if b3 < 0x80 || 0xbf < b3 {
|
||||
return RUNE_ERROR, 1
|
||||
b3 := s[3];
|
||||
if b3 < LOCB || HICB < b3 {
|
||||
return RUNE_ERROR, 1;
|
||||
}
|
||||
return (b0&MASK_4) as rune <<18 | (b1&MASK_X) as rune <<12 | (b3&MASK_X) as rune <<6 | (b3&MASK_X) as rune, 4
|
||||
|
||||
return cast(rune)(s0&MASK4)<<18 | cast(rune)(b1&MASKX)<<12 | cast(rune)(b2&MASKX)<<6 | cast(rune)(b3&MASKX), 4;
|
||||
}
|
||||
|
||||
|
||||
valid_rune :: proc(r: rune) -> bool {
|
||||
if r < 0 {
|
||||
return false
|
||||
return false;
|
||||
} else if SURROGATE_MIN <= r && r <= SURROGATE_MAX {
|
||||
return false
|
||||
return false;
|
||||
} else if r > MAX_RUNE {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
return true
|
||||
return true;
|
||||
}
|
||||
|
||||
valid_string :: proc(s: string) -> bool {
|
||||
n := s.count
|
||||
n := s.count;
|
||||
for i := 0; i < n; {
|
||||
si := s[i]
|
||||
si := s[i];
|
||||
if si < RUNE_SELF { // ascii
|
||||
i++
|
||||
continue
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
x := accept_sizes[si]
|
||||
x := accept_sizes[si];
|
||||
if x == 0xf1 {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
size := (x & 7) as int
|
||||
size := cast(int)(x & 7);
|
||||
if i+size > n {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
ar := accept_ranges[x>>4]
|
||||
ar := accept_ranges[x>>4];
|
||||
if b := s[i+1]; b < ar.lo || ar.hi < b {
|
||||
return false
|
||||
return false;
|
||||
} else if size == 2 {
|
||||
// Okay
|
||||
} else if b := s[i+2]; b < 0x80 || 0xbf < b {
|
||||
return false
|
||||
return false;
|
||||
} else if size == 3 {
|
||||
// Okay
|
||||
} else if b := s[i+3]; b < 0x80 || 0xbf < b {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
i += size
|
||||
i += size;
|
||||
}
|
||||
return true
|
||||
return true;
|
||||
}
|
||||
|
||||
rune_count :: proc(s: string) -> int {
|
||||
count := 0
|
||||
n := s.count
|
||||
for i := 0; i < n; count++ {
|
||||
si := s[i]
|
||||
count := 0;
|
||||
n := s.count;
|
||||
|
||||
for i := 0; i < n; {
|
||||
defer count += 1;
|
||||
si := s[i];
|
||||
if si < RUNE_SELF { // ascii
|
||||
i++
|
||||
continue
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
x := accept_sizes[si]
|
||||
x := accept_sizes[si];
|
||||
if x == 0xf1 {
|
||||
i++
|
||||
continue
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
size := (x & 7) as int
|
||||
size := cast(int)(x & 7);
|
||||
if i+size > n {
|
||||
i++
|
||||
continue
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
ar := accept_ranges[x>>4]
|
||||
ar := accept_ranges[x>>4];
|
||||
if b := s[i+1]; b < ar.lo || ar.hi < b {
|
||||
size = 1
|
||||
size = 1;
|
||||
} else if size == 2 {
|
||||
// Okay
|
||||
} else if b := s[i+2]; b < 0x80 || 0xbf < b {
|
||||
size = 1
|
||||
size = 1;
|
||||
} else if size == 3 {
|
||||
// Okay
|
||||
} else if b := s[i+3]; b < 0x80 || 0xbf < b {
|
||||
size = 1
|
||||
size = 1;
|
||||
}
|
||||
i += size
|
||||
i += size;
|
||||
}
|
||||
return count
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
rune_size :: proc(r: rune) -> int {
|
||||
match {
|
||||
case r < 0: return -1
|
||||
case r <= 1<<7 - 1: return 1
|
||||
case r <= 1<<11 - 1: return 2
|
||||
case SURROGATE_MIN <= r && r <= SURROGATE_MAX: return -1
|
||||
case r <= 1<<16 - 1: return 3
|
||||
case r <= MAX_RUNE: return 4
|
||||
case r < 0: return -1;
|
||||
case r <= 1<<7 - 1: return 1;
|
||||
case r <= 1<<11 - 1: return 2;
|
||||
case SURROGATE_MIN <= r && r <= SURROGATE_MAX: return -1;
|
||||
case r <= 1<<16 - 1: return 3;
|
||||
case r <= MAX_RUNE: return 4;
|
||||
}
|
||||
return -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
-495
@@ -1,495 +0,0 @@
|
||||
#foreign_system_library "user32"
|
||||
#foreign_system_library "gdi32"
|
||||
|
||||
_:= compile_assert(ODIN_OS == "windows")
|
||||
|
||||
HANDLE :: type rawptr
|
||||
HWND :: type HANDLE
|
||||
HDC :: type HANDLE
|
||||
HINSTANCE :: type HANDLE
|
||||
HICON :: type HANDLE
|
||||
HCURSOR :: type HANDLE
|
||||
HMENU :: type HANDLE
|
||||
HBRUSH :: type HANDLE
|
||||
HGDIOBJ :: type HANDLE
|
||||
HMODULE :: type HANDLE
|
||||
WPARAM :: type uint
|
||||
LPARAM :: type int
|
||||
LRESULT :: type int
|
||||
ATOM :: type i16
|
||||
BOOL :: type i32
|
||||
WNDPROC :: type proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT
|
||||
|
||||
INVALID_HANDLE_VALUE :: (-1 as int) as HANDLE
|
||||
|
||||
CS_VREDRAW :: 0x0001
|
||||
CS_HREDRAW :: 0x0002
|
||||
CS_OWNDC :: 0x0020
|
||||
CW_USEDEFAULT :: -0x80000000
|
||||
|
||||
WS_OVERLAPPED :: 0
|
||||
WS_MAXIMIZEBOX :: 0x00010000
|
||||
WS_MINIMIZEBOX :: 0x00020000
|
||||
WS_THICKFRAME :: 0x00040000
|
||||
WS_SYSMENU :: 0x00080000
|
||||
WS_CAPTION :: 0x00C00000
|
||||
WS_VISIBLE :: 0x10000000
|
||||
WS_OVERLAPPEDWINDOW :: WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX
|
||||
|
||||
WM_DESTROY :: 0x0002
|
||||
WM_CLOSE :: 0x0010
|
||||
WM_QUIT :: 0x0012
|
||||
WM_KEYDOWN :: 0x0100
|
||||
WM_KEYUP :: 0x0101
|
||||
|
||||
PM_REMOVE :: 1
|
||||
|
||||
COLOR_BACKGROUND :: 1 as HBRUSH
|
||||
BLACK_BRUSH :: 4
|
||||
|
||||
SM_CXSCREEN :: 0
|
||||
SM_CYSCREEN :: 1
|
||||
|
||||
SW_SHOW :: 5
|
||||
|
||||
POINT :: struct #ordered {
|
||||
x, y: i32
|
||||
}
|
||||
|
||||
|
||||
WNDCLASSEXA :: struct #ordered {
|
||||
size, style: u32
|
||||
wnd_proc: WNDPROC
|
||||
cls_extra, wnd_extra: i32
|
||||
instance: HINSTANCE
|
||||
icon: HICON
|
||||
cursor: HCURSOR
|
||||
background: HBRUSH
|
||||
menu_name, class_name: ^u8
|
||||
sm: HICON
|
||||
}
|
||||
|
||||
MSG :: struct #ordered {
|
||||
hwnd: HWND
|
||||
message: u32
|
||||
wparam: WPARAM
|
||||
lparam: LPARAM
|
||||
time: u32
|
||||
pt: POINT
|
||||
}
|
||||
|
||||
RECT :: struct #ordered {
|
||||
left: i32
|
||||
top: i32
|
||||
right: i32
|
||||
bottom: i32
|
||||
}
|
||||
|
||||
FILETIME :: struct #ordered {
|
||||
low_date_time, high_date_time: u32
|
||||
}
|
||||
|
||||
BY_HANDLE_FILE_INFORMATION :: struct #ordered {
|
||||
file_attributes: u32
|
||||
creation_time,
|
||||
last_access_time,
|
||||
last_write_time: FILETIME
|
||||
volume_serial_number,
|
||||
file_size_high,
|
||||
file_size_low,
|
||||
number_of_links,
|
||||
file_index_high,
|
||||
file_index_low: u32
|
||||
}
|
||||
|
||||
WIN32_FILE_ATTRIBUTE_DATA :: struct #ordered {
|
||||
file_attributes: u32
|
||||
creation_time,
|
||||
last_access_time,
|
||||
last_write_time: FILETIME
|
||||
file_size_high,
|
||||
file_size_low: u32
|
||||
}
|
||||
|
||||
GET_FILEEX_INFO_LEVELS :: type i32
|
||||
GetFileExInfoStandard : GET_FILEEX_INFO_LEVELS : 0
|
||||
GetFileExMaxInfoLevel : GET_FILEEX_INFO_LEVELS : 1
|
||||
|
||||
GetLastError :: proc() -> i32 #foreign #dll_import
|
||||
ExitProcess :: proc(exit_code: u32) #foreign #dll_import
|
||||
GetDesktopWindow :: proc() -> HWND #foreign #dll_import
|
||||
GetCursorPos :: proc(p: ^POINT) -> i32 #foreign #dll_import
|
||||
ScreenToClient :: proc(h: HWND, p: ^POINT) -> i32 #foreign #dll_import
|
||||
GetModuleHandleA :: proc(module_name: ^u8) -> HINSTANCE #foreign #dll_import
|
||||
GetStockObject :: proc(fn_object: i32) -> HGDIOBJ #foreign #dll_import
|
||||
PostQuitMessage :: proc(exit_code: i32) #foreign #dll_import
|
||||
SetWindowTextA :: proc(hwnd: HWND, c_string: ^u8) -> BOOL #foreign #dll_import
|
||||
|
||||
QueryPerformanceFrequency :: proc(result: ^i64) -> i32 #foreign #dll_import
|
||||
QueryPerformanceCounter :: proc(result: ^i64) -> i32 #foreign #dll_import
|
||||
|
||||
Sleep :: proc(ms: i32) -> i32 #foreign #dll_import
|
||||
|
||||
OutputDebugStringA :: proc(c_str: ^u8) #foreign #dll_import
|
||||
|
||||
|
||||
RegisterClassExA :: proc(wc: ^WNDCLASSEXA) -> ATOM #foreign #dll_import
|
||||
CreateWindowExA :: proc(ex_style: u32,
|
||||
class_name, title: ^u8,
|
||||
style: u32,
|
||||
x, y, w, h: i32,
|
||||
parent: HWND, menu: HMENU, instance: HINSTANCE,
|
||||
param: rawptr) -> HWND #foreign #dll_import
|
||||
|
||||
ShowWindow :: proc(hwnd: HWND, cmd_show: i32) -> BOOL #foreign #dll_import
|
||||
TranslateMessage :: proc(msg: ^MSG) -> BOOL #foreign #dll_import
|
||||
DispatchMessageA :: proc(msg: ^MSG) -> LRESULT #foreign #dll_import
|
||||
UpdateWindow :: proc(hwnd: HWND) -> BOOL #foreign #dll_import
|
||||
PeekMessageA :: proc(msg: ^MSG, hwnd: HWND,
|
||||
msg_filter_min, msg_filter_max, remove_msg: u32) -> BOOL #foreign #dll_import
|
||||
|
||||
DefWindowProcA :: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT #foreign #dll_import
|
||||
|
||||
AdjustWindowRect :: proc(rect: ^RECT, style: u32, menu: BOOL) -> BOOL #foreign #dll_import
|
||||
|
||||
|
||||
GetQueryPerformanceFrequency :: proc() -> i64 {
|
||||
r: i64
|
||||
QueryPerformanceFrequency(^r)
|
||||
return r
|
||||
}
|
||||
|
||||
GetCommandLineA :: proc() -> ^u8 #foreign #dll_import
|
||||
GetSystemMetrics :: proc(index: i32) -> i32 #foreign #dll_import
|
||||
GetCurrentThreadId :: proc() -> u32 #foreign #dll_import
|
||||
|
||||
// File Stuff
|
||||
|
||||
CloseHandle :: proc(h: HANDLE) -> i32 #foreign #dll_import
|
||||
GetStdHandle :: proc(h: i32) -> HANDLE #foreign #dll_import
|
||||
CreateFileA :: proc(filename: ^u8, desired_access, share_mode: u32,
|
||||
security: rawptr,
|
||||
creation, flags_and_attribs: u32, template_file: HANDLE) -> HANDLE #foreign #dll_import
|
||||
ReadFile :: proc(h: HANDLE, buf: rawptr, to_read: u32, bytes_read: ^i32, overlapped: rawptr) -> BOOL #foreign #dll_import
|
||||
WriteFile :: proc(h: HANDLE, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> i32 #foreign #dll_import
|
||||
|
||||
GetFileSizeEx :: proc(file_handle: HANDLE, file_size: ^i64) -> BOOL #foreign #dll_import
|
||||
GetFileAttributesExA :: proc(filename: ^u8, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: rawptr) -> BOOL #foreign #dll_import
|
||||
GetFileInformationByHandle :: proc(file_handle: HANDLE, file_info: ^BY_HANDLE_FILE_INFORMATION) -> BOOL #foreign #dll_import
|
||||
|
||||
FILE_SHARE_READ :: 0x00000001
|
||||
FILE_SHARE_WRITE :: 0x00000002
|
||||
FILE_SHARE_DELETE :: 0x00000004
|
||||
FILE_GENERIC_ALL :: 0x10000000
|
||||
FILE_GENERIC_EXECUTE :: 0x20000000
|
||||
FILE_GENERIC_WRITE :: 0x40000000
|
||||
FILE_GENERIC_READ :: 0x80000000
|
||||
|
||||
STD_INPUT_HANDLE :: -10
|
||||
STD_OUTPUT_HANDLE :: -11
|
||||
STD_ERROR_HANDLE :: -12
|
||||
|
||||
CREATE_NEW :: 1
|
||||
CREATE_ALWAYS :: 2
|
||||
OPEN_EXISTING :: 3
|
||||
OPEN_ALWAYS :: 4
|
||||
TRUNCATE_EXISTING :: 5
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
HeapAlloc :: proc(h: HANDLE, flags: u32, bytes: int) -> rawptr #foreign #dll_import
|
||||
HeapReAlloc :: proc(h: HANDLE, flags: u32, memory: rawptr, bytes: int) -> rawptr #foreign #dll_import
|
||||
HeapFree :: proc(h: HANDLE, flags: u32, memory: rawptr) -> BOOL #foreign #dll_import
|
||||
GetProcessHeap :: proc() -> HANDLE #foreign #dll_import
|
||||
|
||||
|
||||
HEAP_ZERO_MEMORY :: 0x00000008
|
||||
|
||||
|
||||
|
||||
// GDI
|
||||
|
||||
BITMAPINFO :: struct #ordered {
|
||||
HEADER :: struct #ordered {
|
||||
size: u32
|
||||
width, height: i32
|
||||
planes, bit_count: i16
|
||||
compression: u32
|
||||
size_image: u32
|
||||
x_pels_per_meter: i32
|
||||
y_pels_per_meter: i32
|
||||
clr_used: u32
|
||||
clr_important: u32
|
||||
}
|
||||
using header: HEADER
|
||||
colors: [1]RGBQUAD
|
||||
}
|
||||
|
||||
|
||||
RGBQUAD :: struct #ordered {
|
||||
blue, green, red, reserved: byte
|
||||
}
|
||||
|
||||
BI_RGB :: 0
|
||||
DIB_RGB_COLORS :: 0x00
|
||||
SRCCOPY : u32 : 0x00cc0020
|
||||
|
||||
StretchDIBits :: 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 #foreign #dll_import
|
||||
|
||||
|
||||
|
||||
LoadLibraryA :: proc(c_str: ^u8) -> HMODULE #foreign
|
||||
FreeLibrary :: proc(h: HMODULE) #foreign
|
||||
GetProcAddress :: proc(h: HMODULE, c_str: ^u8) -> proc() #foreign
|
||||
|
||||
GetClientRect :: proc(hwnd: HWND, rect: ^RECT) -> BOOL #foreign
|
||||
|
||||
|
||||
|
||||
// Windows OpenGL
|
||||
|
||||
PFD_TYPE_RGBA :: 0
|
||||
PFD_TYPE_COLORINDEX :: 1
|
||||
PFD_MAIN_PLANE :: 0
|
||||
PFD_OVERLAY_PLANE :: 1
|
||||
PFD_UNDERLAY_PLANE :: -1
|
||||
PFD_DOUBLEBUFFER :: 1
|
||||
PFD_STEREO :: 2
|
||||
PFD_DRAW_TO_WINDOW :: 4
|
||||
PFD_DRAW_TO_BITMAP :: 8
|
||||
PFD_SUPPORT_GDI :: 16
|
||||
PFD_SUPPORT_OPENGL :: 32
|
||||
PFD_GENERIC_FORMAT :: 64
|
||||
PFD_NEED_PALETTE :: 128
|
||||
PFD_NEED_SYSTEM_PALETTE :: 0x00000100
|
||||
PFD_SWAP_EXCHANGE :: 0x00000200
|
||||
PFD_SWAP_COPY :: 0x00000400
|
||||
PFD_SWAP_LAYER_BUFFERS :: 0x00000800
|
||||
PFD_GENERIC_ACCELERATED :: 0x00001000
|
||||
PFD_DEPTH_DONTCARE :: 0x20000000
|
||||
PFD_DOUBLEBUFFER_DONTCARE :: 0x40000000
|
||||
PFD_STEREO_DONTCARE :: 0x80000000
|
||||
|
||||
HGLRC :: type HANDLE
|
||||
PROC :: type proc()
|
||||
wglCreateContextAttribsARBType :: type proc(hdc: HDC, hshareContext: rawptr, attribList: ^i32) -> HGLRC
|
||||
|
||||
|
||||
PIXELFORMATDESCRIPTOR :: struct #ordered {
|
||||
size,
|
||||
version,
|
||||
flags: u32
|
||||
|
||||
pixel_type,
|
||||
color_bits,
|
||||
red_bits,
|
||||
red_shift,
|
||||
green_bits,
|
||||
green_shift,
|
||||
blue_bits,
|
||||
blue_shift,
|
||||
alpha_bits,
|
||||
alpha_shift,
|
||||
accum_bits,
|
||||
accum_red_bits,
|
||||
accum_green_bits,
|
||||
accum_blue_bits,
|
||||
accum_alpha_bits,
|
||||
depth_bits,
|
||||
stencil_bits,
|
||||
aux_buffers,
|
||||
layer_type,
|
||||
reserved: byte
|
||||
|
||||
layer_mask,
|
||||
visible_mask,
|
||||
damage_mask: u32
|
||||
}
|
||||
|
||||
GetDC :: proc(h: HANDLE) -> HDC #foreign
|
||||
SetPixelFormat :: proc(hdc: HDC, pixel_format: i32, pfd: ^PIXELFORMATDESCRIPTOR ) -> BOOL #foreign #dll_import
|
||||
ChoosePixelFormat :: proc(hdc: HDC, pfd: ^PIXELFORMATDESCRIPTOR) -> i32 #foreign #dll_import
|
||||
SwapBuffers :: proc(hdc: HDC) -> BOOL #foreign #dll_import
|
||||
ReleaseDC :: proc(wnd: HWND, hdc: HDC) -> i32 #foreign #dll_import
|
||||
|
||||
WGL_CONTEXT_MAJOR_VERSION_ARB :: 0x2091
|
||||
WGL_CONTEXT_MINOR_VERSION_ARB :: 0x2092
|
||||
WGL_CONTEXT_PROFILE_MASK_ARB :: 0x9126
|
||||
WGL_CONTEXT_CORE_PROFILE_BIT_ARB :: 0x0001
|
||||
WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB :: 0x0002
|
||||
|
||||
wglCreateContext :: proc(hdc: HDC) -> HGLRC #foreign #dll_import
|
||||
wglMakeCurrent :: proc(hdc: HDC, hglrc: HGLRC) -> BOOL #foreign #dll_import
|
||||
wglGetProcAddress :: proc(c_str: ^u8) -> PROC #foreign #dll_import
|
||||
wglDeleteContext :: proc(hglrc: HGLRC) -> BOOL #foreign #dll_import
|
||||
|
||||
|
||||
|
||||
GetKeyState :: proc(v_key: i32) -> i16 #foreign #dll_import
|
||||
GetAsyncKeyState :: proc(v_key: i32) -> i16 #foreign #dll_import
|
||||
|
||||
is_key_down :: proc(key: Key_Code) -> bool {
|
||||
return GetAsyncKeyState(key as i32) < 0
|
||||
}
|
||||
|
||||
Key_Code :: enum i32 {
|
||||
LBUTTON = 0x01,
|
||||
RBUTTON = 0x02,
|
||||
CANCEL = 0x03,
|
||||
MBUTTON = 0x04,
|
||||
|
||||
BACK = 0x08,
|
||||
TAB = 0x09,
|
||||
|
||||
CLEAR = 0x0C,
|
||||
RETURN = 0x0D,
|
||||
|
||||
SHIFT = 0x10,
|
||||
CONTROL = 0x11,
|
||||
MENU = 0x12,
|
||||
PAUSE = 0x13,
|
||||
CAPITAL = 0x14,
|
||||
|
||||
KANA = 0x15,
|
||||
HANGEUL = 0x15,
|
||||
HANGUL = 0x15,
|
||||
JUNJA = 0x17,
|
||||
FINAL = 0x18,
|
||||
HANJA = 0x19,
|
||||
KANJI = 0x19,
|
||||
|
||||
ESCAPE = 0x1B,
|
||||
|
||||
CONVERT = 0x1C,
|
||||
NONCONVERT = 0x1D,
|
||||
ACCEPT = 0x1E,
|
||||
MODECHANGE = 0x1F,
|
||||
|
||||
SPACE = 0x20,
|
||||
PRIOR = 0x21,
|
||||
NEXT = 0x22,
|
||||
END = 0x23,
|
||||
HOME = 0x24,
|
||||
LEFT = 0x25,
|
||||
UP = 0x26,
|
||||
RIGHT = 0x27,
|
||||
DOWN = 0x28,
|
||||
SELECT = 0x29,
|
||||
PRINT = 0x2A,
|
||||
EXECUTE = 0x2B,
|
||||
SNAPSHOT = 0x2C,
|
||||
INSERT = 0x2D,
|
||||
DELETE = 0x2E,
|
||||
HELP = 0x2F,
|
||||
|
||||
NUM0 = '0',
|
||||
NUM1 = '1',
|
||||
NUM2 = '2',
|
||||
NUM3 = '3',
|
||||
NUM4 = '4',
|
||||
NUM5 = '5',
|
||||
NUM6 = '6',
|
||||
NUM7 = '7',
|
||||
NUM8 = '8',
|
||||
NUM9 = '9',
|
||||
|
||||
A = 'A',
|
||||
B = 'B',
|
||||
C = 'C',
|
||||
D = 'D',
|
||||
E = 'E',
|
||||
F = 'F',
|
||||
G = 'G',
|
||||
H = 'H',
|
||||
I = 'I',
|
||||
J = 'J',
|
||||
K = 'K',
|
||||
L = 'L',
|
||||
M = 'M',
|
||||
N = 'N',
|
||||
O = 'O',
|
||||
P = 'P',
|
||||
Q = 'Q',
|
||||
R = 'R',
|
||||
S = 'S',
|
||||
T = 'T',
|
||||
U = 'U',
|
||||
V = 'V',
|
||||
W = 'W',
|
||||
X = 'X',
|
||||
Y = 'Y',
|
||||
Z = 'Z',
|
||||
|
||||
LWIN = 0x5B,
|
||||
RWIN = 0x5C,
|
||||
APPS = 0x5D,
|
||||
|
||||
NUMPAD0 = 0x60,
|
||||
NUMPAD1 = 0x61,
|
||||
NUMPAD2 = 0x62,
|
||||
NUMPAD3 = 0x63,
|
||||
NUMPAD4 = 0x64,
|
||||
NUMPAD5 = 0x65,
|
||||
NUMPAD6 = 0x66,
|
||||
NUMPAD7 = 0x67,
|
||||
NUMPAD8 = 0x68,
|
||||
NUMPAD9 = 0x69,
|
||||
MULTIPLY = 0x6A,
|
||||
ADD = 0x6B,
|
||||
SEPARATOR = 0x6C,
|
||||
SUBTRACT = 0x6D,
|
||||
DECIMAL = 0x6E,
|
||||
DIVIDE = 0x6F,
|
||||
F1 = 0x70,
|
||||
F2 = 0x71,
|
||||
F3 = 0x72,
|
||||
F4 = 0x73,
|
||||
F5 = 0x74,
|
||||
F6 = 0x75,
|
||||
F7 = 0x76,
|
||||
F8 = 0x77,
|
||||
F9 = 0x78,
|
||||
F10 = 0x79,
|
||||
F11 = 0x7A,
|
||||
F12 = 0x7B,
|
||||
F13 = 0x7C,
|
||||
F14 = 0x7D,
|
||||
F15 = 0x7E,
|
||||
F16 = 0x7F,
|
||||
F17 = 0x80,
|
||||
F18 = 0x81,
|
||||
F19 = 0x82,
|
||||
F20 = 0x83,
|
||||
F21 = 0x84,
|
||||
F22 = 0x85,
|
||||
F23 = 0x86,
|
||||
F24 = 0x87,
|
||||
|
||||
NUMLOCK = 0x90,
|
||||
SCROLL = 0x91,
|
||||
|
||||
LSHIFT = 0xA0,
|
||||
RSHIFT = 0xA1,
|
||||
LCONTROL = 0xA2,
|
||||
RCONTROL = 0xA3,
|
||||
LMENU = 0xA4,
|
||||
RMENU = 0xA5,
|
||||
PROCESSKEY = 0xE5,
|
||||
ATTN = 0xF6,
|
||||
CRSEL = 0xF7,
|
||||
EXSEL = 0xF8,
|
||||
EREOF = 0xF9,
|
||||
PLAY = 0xFA,
|
||||
ZOOM = 0xFB,
|
||||
NONAME = 0xFC,
|
||||
PA1 = 0xFD,
|
||||
OEM_CLEAR = 0xFE,
|
||||
}
|
||||
|
||||
+13
-16
@@ -2,23 +2,20 @@
|
||||
|
||||
Not in any particular order
|
||||
|
||||
* Compile Time Execution (CTE)
|
||||
- More metaprogramming madness
|
||||
- Compiler as a library
|
||||
- AST inspection and modification
|
||||
* CTE-based build system
|
||||
* Replace LLVM backend with my own custom backend
|
||||
* Improve SSA design to accommodate for lowering to a "bytecode"
|
||||
* SSA optimizations
|
||||
* Parametric Polymorphism
|
||||
* Documentation Generator for "Entities"
|
||||
* Multiple Architecture support
|
||||
* Linking Options
|
||||
* Custom backend to replace LLVM
|
||||
- Improve SSA design to accommodate for lowering to a "bytecode"
|
||||
- SSA optimizations
|
||||
- COFF generation
|
||||
- linker
|
||||
* Type safe "macros"
|
||||
* Documentation generator for "Entities"
|
||||
* Multiple architecture support
|
||||
* Inline assembly
|
||||
* Linking options
|
||||
- Executable
|
||||
- Static/Dynamic Library
|
||||
* Debug Information
|
||||
* Debug information
|
||||
- pdb format too
|
||||
* Command Line Tooling
|
||||
* Compiler Internals:
|
||||
* Command line tooling
|
||||
* Compiler internals:
|
||||
- Big numbers library
|
||||
- Cyclic Type Checking (at the moment will cause compiler to go into an infinite loop)
|
||||
|
||||
+172
@@ -0,0 +1,172 @@
|
||||
typedef struct BuildContext {
|
||||
String ODIN_OS; // target operating system
|
||||
String ODIN_ARCH; // target architecture
|
||||
String ODIN_VENDOR; // compiler vendor
|
||||
String ODIN_VERSION; // compiler version
|
||||
String ODIN_ROOT; // Odin ROOT
|
||||
|
||||
i64 word_size;
|
||||
i64 max_align;
|
||||
String llc_flags;
|
||||
String link_flags;
|
||||
bool is_dll;
|
||||
} BuildContext;
|
||||
|
||||
// TODO(bill): OS dependent versions for the BuildContext
|
||||
// join_path
|
||||
// is_dir
|
||||
// is_file
|
||||
// is_abs_path
|
||||
// has_subdir
|
||||
|
||||
String const WIN32_SEPARATOR_STRING = {cast(u8 *)"\\", 1};
|
||||
String const NIX_SEPARATOR_STRING = {cast(u8 *)"/", 1};
|
||||
|
||||
String odin_root_dir(void) {
|
||||
String path = global_module_path;
|
||||
Array(wchar_t) path_buf;
|
||||
isize len, i;
|
||||
gbTempArenaMemory tmp;
|
||||
wchar_t *text;
|
||||
|
||||
if (global_module_path_set) {
|
||||
return global_module_path;
|
||||
}
|
||||
|
||||
array_init_count(&path_buf, heap_allocator(), 300);
|
||||
|
||||
len = 0;
|
||||
for (;;) {
|
||||
len = GetModuleFileNameW(NULL, &path_buf.e[0], path_buf.count);
|
||||
if (len == 0) {
|
||||
return make_string(NULL, 0);
|
||||
}
|
||||
if (len < path_buf.count) {
|
||||
break;
|
||||
}
|
||||
array_resize(&path_buf, 2*path_buf.count + 300);
|
||||
}
|
||||
|
||||
tmp = gb_temp_arena_memory_begin(&string_buffer_arena);
|
||||
|
||||
text = gb_alloc_array(string_buffer_allocator, wchar_t, len+1);
|
||||
|
||||
GetModuleFileNameW(NULL, text, len);
|
||||
path = string16_to_string(heap_allocator(), make_string16(text, len));
|
||||
for (i = path.len-1; i >= 0; i--) {
|
||||
u8 c = path.text[i];
|
||||
if (c == '/' || c == '\\') {
|
||||
break;
|
||||
}
|
||||
path.len--;
|
||||
}
|
||||
|
||||
global_module_path = path;
|
||||
global_module_path_set = true;
|
||||
|
||||
gb_temp_arena_memory_end(tmp);
|
||||
|
||||
array_free(&path_buf);
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
String path_to_fullpath(gbAllocator a, String s) {
|
||||
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&string_buffer_arena);
|
||||
String16 string16 = string_to_string16(string_buffer_allocator, s);
|
||||
String result = {0};
|
||||
|
||||
DWORD len = GetFullPathNameW(string16.text, 0, NULL, NULL);
|
||||
if (len != 0) {
|
||||
wchar_t *text = gb_alloc_array(string_buffer_allocator, wchar_t, len+1);
|
||||
GetFullPathNameW(string16.text, len, text, NULL);
|
||||
text[len] = 0;
|
||||
result = string16_to_string(a, make_string16(text, len));
|
||||
}
|
||||
gb_temp_arena_memory_end(tmp);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
String get_fullpath_relative(gbAllocator a, String base_dir, String path) {
|
||||
String res = {0};
|
||||
isize str_len = base_dir.len+path.len;
|
||||
|
||||
u8 *str = gb_alloc_array(heap_allocator(), u8, str_len+1);
|
||||
|
||||
isize i = 0;
|
||||
gb_memmove(str+i, base_dir.text, base_dir.len); i += base_dir.len;
|
||||
gb_memmove(str+i, path.text, path.len);
|
||||
str[str_len] = '\0';
|
||||
res = path_to_fullpath(a, make_string(str, str_len));
|
||||
gb_free(heap_allocator(), str);
|
||||
return res;
|
||||
}
|
||||
|
||||
String get_fullpath_core(gbAllocator a, String path) {
|
||||
String module_dir = odin_root_dir();
|
||||
String res = {0};
|
||||
|
||||
char core[] = "core/";
|
||||
isize core_len = gb_size_of(core)-1;
|
||||
|
||||
isize str_len = module_dir.len + core_len + path.len;
|
||||
u8 *str = gb_alloc_array(heap_allocator(), u8, str_len+1);
|
||||
|
||||
gb_memmove(str, module_dir.text, module_dir.len);
|
||||
gb_memmove(str+module_dir.len, core, core_len);
|
||||
gb_memmove(str+module_dir.len+core_len, path.text, path.len);
|
||||
str[str_len] = '\0';
|
||||
|
||||
res = path_to_fullpath(a, make_string(str, str_len));
|
||||
gb_free(heap_allocator(), str);
|
||||
return res;
|
||||
}
|
||||
|
||||
String get_filepath_extension(String path) {
|
||||
isize dot = 0;
|
||||
bool seen_slash = false;
|
||||
for (isize i = path.len-1; i >= 0; i--) {
|
||||
u8 c = path.text[i];
|
||||
if (c == '/' || c == '\\') {
|
||||
seen_slash = true;
|
||||
}
|
||||
|
||||
if (c == '.') {
|
||||
if (seen_slash) {
|
||||
return str_lit("");
|
||||
}
|
||||
|
||||
dot = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return make_string(path.text, dot);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void init_build_context(BuildContext *bc) {
|
||||
bc->ODIN_VENDOR = str_lit("odin");
|
||||
bc->ODIN_VERSION = str_lit("0.0.5e");
|
||||
bc->ODIN_ROOT = odin_root_dir();
|
||||
|
||||
#if defined(GB_SYSTEM_WINDOWS)
|
||||
bc->ODIN_OS = str_lit("windows");
|
||||
bc->ODIN_ARCH = str_lit("amd64");
|
||||
#else
|
||||
#error Implement system
|
||||
#endif
|
||||
|
||||
if (str_eq(bc->ODIN_ARCH, str_lit("amd64"))) {
|
||||
bc->word_size = 8;
|
||||
bc->max_align = 16;
|
||||
bc->llc_flags = str_lit("-march=x86-64 ");
|
||||
bc->link_flags = str_lit("/machine:x64 ");
|
||||
} else if (str_eq(bc->ODIN_ARCH, str_lit("x86"))) {
|
||||
bc->word_size = 4;
|
||||
bc->max_align = 8;
|
||||
bc->llc_flags = str_lit("-march=x86 ");
|
||||
bc->link_flags = str_lit("/machine:x86 ");
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,6 @@
|
||||
bool check_is_terminating(AstNode *node);
|
||||
void check_stmt (Checker *c, AstNode *node, u32 flags);
|
||||
void check_stmt_list (Checker *c, AstNodeArray stmts, u32 flags);
|
||||
void check_type_decl (Checker *c, Entity *e, AstNode *type_expr, Type *def, CycleChecker *cycle_checker);
|
||||
void check_const_decl (Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr);
|
||||
void check_proc_decl (Checker *c, Entity *e, DeclInfo *d);
|
||||
void check_var_decl (Checker *c, Entity *e, Entity **entities, isize entity_count, AstNode *type_expr, AstNode *init_expr);
|
||||
void check_stmt (Checker *c, AstNode *node, u32 flags);
|
||||
void check_stmt_list (Checker *c, AstNodeArray stmts, u32 flags);
|
||||
|
||||
// NOTE(bill): `content_name` is for debugging and error messages
|
||||
Type *check_init_variable(Checker *c, Entity *e, Operand *operand, String context_name) {
|
||||
@@ -16,7 +12,7 @@ Type *check_init_variable(Checker *c, Entity *e, Operand *operand, String contex
|
||||
gbString expr_str = expr_to_string(operand->expr);
|
||||
|
||||
// TODO(bill): is this a good enough error message?
|
||||
error(ast_node_token(operand->expr),
|
||||
error_node(operand->expr,
|
||||
"Cannot assign builtin procedure `%s` in %.*s",
|
||||
expr_str,
|
||||
LIT(context_name));
|
||||
@@ -44,6 +40,7 @@ Type *check_init_variable(Checker *c, Entity *e, Operand *operand, String contex
|
||||
}
|
||||
t = default_type(t);
|
||||
}
|
||||
GB_ASSERT(is_type_typed(t));
|
||||
e->type = t;
|
||||
}
|
||||
|
||||
@@ -67,6 +64,7 @@ void check_init_variables(Checker *c, Entity **lhs, isize lhs_count, AstNodeArra
|
||||
Array(Operand) operands;
|
||||
array_init_reserve(&operands, c->tmp_allocator, 2*lhs_count);
|
||||
|
||||
// TODO(bill): Allow for type hints from the entities
|
||||
for_array(i, inits) {
|
||||
AstNode *rhs = inits.e[i];
|
||||
Operand o = {0};
|
||||
@@ -96,125 +94,21 @@ void check_init_variables(Checker *c, Entity **lhs, isize lhs_count, AstNodeArra
|
||||
}
|
||||
|
||||
if (rhs_count > 0 && lhs_count != rhs_count) {
|
||||
error(lhs[0]->token, "Assignment count mismatch `%td` := `%td`", lhs_count, rhs_count);
|
||||
error(lhs[0]->token, "Assignment count mismatch `%td` = `%td`", lhs_count, rhs_count);
|
||||
}
|
||||
|
||||
#if 0
|
||||
if (lhs[0]->kind == Entity_Variable &&
|
||||
lhs[0]->Variable.is_let) {
|
||||
if (lhs_count != rhs_count) {
|
||||
error(lhs[0]->token, "`let` variables must be initialized, `%td` = `%td`", lhs_count, rhs_count);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
gb_temp_arena_memory_end(tmp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void check_entity_decl(Checker *c, Entity *e, DeclInfo *d, Type *named_type, CycleChecker *cycle_checker) {
|
||||
if (e->type != NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (d == NULL) {
|
||||
DeclInfo **found = map_decl_info_get(&c->info.entities, hash_pointer(e));
|
||||
if (found) {
|
||||
d = *found;
|
||||
} else {
|
||||
e->type = t_invalid;
|
||||
set_base_type(named_type, t_invalid);
|
||||
return;
|
||||
// GB_PANIC("`%.*s` should been declared!", LIT(e->token.string));
|
||||
}
|
||||
}
|
||||
|
||||
if (e->kind == Entity_Procedure) {
|
||||
check_proc_decl(c, e, d);
|
||||
return;
|
||||
}
|
||||
CheckerContext prev = c->context;
|
||||
c->context.scope = d->scope;
|
||||
c->context.decl = d;
|
||||
|
||||
switch (e->kind) {
|
||||
case Entity_Constant:
|
||||
check_const_decl(c, e, d->type_expr, d->init_expr);
|
||||
break;
|
||||
case Entity_Variable:
|
||||
check_var_decl(c, e, d->entities, d->entity_count, d->type_expr, d->init_expr);
|
||||
break;
|
||||
case Entity_TypeName:
|
||||
check_type_decl(c, e, d->type_expr, named_type, cycle_checker);
|
||||
break;
|
||||
}
|
||||
|
||||
c->context = prev;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void check_var_decl_node(Checker *c, AstNode *node) {
|
||||
ast_node(vd, VarDecl, node);
|
||||
isize entity_count = vd->names.count;
|
||||
isize entity_index = 0;
|
||||
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
|
||||
|
||||
for_array(i, vd->names) {
|
||||
AstNode *name = vd->names.e[i];
|
||||
Entity *entity = NULL;
|
||||
if (name->kind == AstNode_Ident) {
|
||||
Token token = name->Ident;
|
||||
String str = token.string;
|
||||
Entity *found = NULL;
|
||||
// NOTE(bill): Ignore assignments to `_`
|
||||
if (str_ne(str, str_lit("_"))) {
|
||||
found = current_scope_lookup_entity(c->context.scope, str);
|
||||
}
|
||||
if (found == NULL) {
|
||||
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL);
|
||||
add_entity_definition(&c->info, name, entity);
|
||||
} 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;
|
||||
}
|
||||
} else {
|
||||
error(ast_node_token(name), "A variable declaration must be an identifier");
|
||||
}
|
||||
if (entity == NULL) {
|
||||
entity = make_entity_dummy_variable(c->allocator, c->global_scope, ast_node_token(name));
|
||||
}
|
||||
entities[entity_index++] = entity;
|
||||
}
|
||||
|
||||
Type *init_type = NULL;
|
||||
if (vd->type) {
|
||||
init_type = check_type_extra(c, vd->type, NULL, 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_init_variables(c, entities, entity_count, vd->values, str_lit("variable declaration"));
|
||||
|
||||
for_array(i, vd->names) {
|
||||
if (entities[i] != NULL) {
|
||||
add_entity(c, c->context.scope, vd->names.e[i], entities[i]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void check_init_constant(Checker *c, Entity *e, Operand *operand) {
|
||||
if (operand->mode == Addressing_Invalid ||
|
||||
operand->type == t_invalid ||
|
||||
@@ -227,23 +121,23 @@ void check_init_constant(Checker *c, Entity *e, Operand *operand) {
|
||||
|
||||
if (operand->mode != Addressing_Constant) {
|
||||
// TODO(bill): better error
|
||||
error(ast_node_token(operand->expr),
|
||||
"`%.*s` is not a constant", LIT(ast_node_token(operand->expr).string));
|
||||
gbString str = expr_to_string(operand->expr);
|
||||
error_node(operand->expr, "`%s` is not a constant", str);
|
||||
gb_string_free(str);
|
||||
if (e->type == NULL) {
|
||||
e->type = t_invalid;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!is_type_constant_type(operand->type)) {
|
||||
gbString type_str = type_to_string(operand->type);
|
||||
error_node(operand->expr, "Invalid constant type: `%s`", type_str);
|
||||
gb_string_free(type_str);
|
||||
if (e->type == NULL) {
|
||||
e->type = t_invalid;
|
||||
}
|
||||
return;
|
||||
}
|
||||
// if (!is_type_constant_type(operand->type)) {
|
||||
// gbString type_str = type_to_string(operand->type);
|
||||
// defer (gb_string_free(type_str));
|
||||
// error(ast_node_token(operand->expr),
|
||||
// "Invalid constant type: `%s`", type_str);
|
||||
// if (e->type == NULL) {
|
||||
// e->type = t_invalid;
|
||||
// }
|
||||
// return;
|
||||
// }
|
||||
|
||||
if (e->type == NULL) { // NOTE(bill): type inference
|
||||
e->type = operand->type;
|
||||
@@ -257,9 +151,27 @@ void check_init_constant(Checker *c, Entity *e, Operand *operand) {
|
||||
e->Constant.value = operand->value;
|
||||
}
|
||||
|
||||
|
||||
void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr) {
|
||||
void check_type_decl(Checker *c, Entity *e, AstNode *type_expr, Type *def) {
|
||||
GB_ASSERT(e->type == NULL);
|
||||
Type *named = make_type_named(c->allocator, e->token.string, NULL, e);
|
||||
named->Named.type_name = e;
|
||||
if (def != NULL && def->kind == Type_Named) {
|
||||
def->Named.base = named;
|
||||
}
|
||||
e->type = named;
|
||||
|
||||
// gb_printf_err("%.*s %p\n", LIT(e->token.string), e);
|
||||
|
||||
Type *bt = check_type_extra(c, type_expr, named);
|
||||
named->Named.base = base_type(bt);
|
||||
if (named->Named.base == t_invalid) {
|
||||
// gb_printf("check_type_decl: %s\n", type_to_string(named));
|
||||
}
|
||||
}
|
||||
|
||||
void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init, Type *named_type) {
|
||||
GB_ASSERT(e->type == NULL);
|
||||
GB_ASSERT(e->kind == Entity_Constant);
|
||||
|
||||
if (e->flags & EntityFlag_Visited) {
|
||||
e->type = t_invalid;
|
||||
@@ -269,47 +181,37 @@ void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_e
|
||||
|
||||
if (type_expr) {
|
||||
Type *t = check_type(c, type_expr);
|
||||
// if (!is_type_constant_type(t)) {
|
||||
// gbString str = type_to_string(t);
|
||||
// defer (gb_string_free(str));
|
||||
// error(ast_node_token(type_expr),
|
||||
// "Invalid constant type `%s`", str);
|
||||
// e->type = t_invalid;
|
||||
// return;
|
||||
// }
|
||||
if (!is_type_constant_type(t)) {
|
||||
gbString str = type_to_string(t);
|
||||
error_node(type_expr, "Invalid constant type `%s`", str);
|
||||
gb_string_free(str);
|
||||
e->type = t_invalid;
|
||||
return;
|
||||
}
|
||||
e->type = t;
|
||||
}
|
||||
|
||||
Operand operand = {0};
|
||||
if (init_expr) {
|
||||
check_expr(c, &operand, init_expr);
|
||||
if (init != NULL) {
|
||||
check_expr_or_type(c, &operand, init);
|
||||
}
|
||||
if (operand.mode == Addressing_Type) {
|
||||
e->kind = Entity_TypeName;
|
||||
|
||||
DeclInfo *d = c->context.decl;
|
||||
d->type_expr = d->init_expr;
|
||||
check_type_decl(c, e, d->type_expr, named_type);
|
||||
return;
|
||||
}
|
||||
|
||||
check_init_constant(c, e, &operand);
|
||||
|
||||
if (operand.mode == Addressing_Invalid ||
|
||||
base_type(operand.type) == t_invalid) {
|
||||
error(e->token, "Invalid declaration type");
|
||||
}
|
||||
}
|
||||
|
||||
void check_type_decl(Checker *c, Entity *e, AstNode *type_expr, Type *def, CycleChecker *cycle_checker) {
|
||||
GB_ASSERT(e->type == NULL);
|
||||
Type *named = make_type_named(c->allocator, e->token.string, NULL, e);
|
||||
named->Named.type_name = e;
|
||||
if (def != NULL && def->kind == Type_Named) {
|
||||
def->Named.base = named;
|
||||
}
|
||||
e->type = named;
|
||||
|
||||
CycleChecker local_cycle_checker = {0};
|
||||
if (cycle_checker == NULL) {
|
||||
cycle_checker = &local_cycle_checker;
|
||||
}
|
||||
|
||||
Type *bt = check_type_extra(c, type_expr, named, cycle_checker_add(cycle_checker, e));
|
||||
named->Named.base = bt;
|
||||
named->Named.base = base_type(named->Named.base);
|
||||
if (named->Named.base == t_invalid) {
|
||||
gb_printf("check_type_decl: %s\n", type_to_string(named));
|
||||
}
|
||||
|
||||
cycle_checker_destroy(&local_cycle_checker);
|
||||
}
|
||||
|
||||
|
||||
bool are_signatures_similar_enough(Type *a_, Type *b_) {
|
||||
@@ -350,18 +252,24 @@ bool are_signatures_similar_enough(Type *a_, Type *b_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
|
||||
void check_proc_lit(Checker *c, Entity *e, DeclInfo *d) {
|
||||
GB_ASSERT(e->type == NULL);
|
||||
if (d->proc_lit->kind != AstNode_ProcLit) {
|
||||
// TOOD(bill): Better error message
|
||||
error_node(d->proc_lit, "Expected a procedure to check");
|
||||
return;
|
||||
}
|
||||
|
||||
Type *proc_type = make_type_proc(c->allocator, e->scope, NULL, 0, NULL, 0, false);
|
||||
Type *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(pd, ProcLit, d->proc_lit);
|
||||
|
||||
check_open_scope(c, pd->type);
|
||||
check_procedure_type(c, proc_type, pd->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;
|
||||
|
||||
@@ -370,31 +278,36 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
|
||||
if (proc_type != NULL) {
|
||||
TypeProc *pt = &proc_type->Proc;
|
||||
if (pt->param_count != 0 ||
|
||||
pt->result_count) {
|
||||
pt->result_count != 0) {
|
||||
gbString str = type_to_string(proc_type);
|
||||
error(e->token,
|
||||
"Procedure type of `main` was expected to be `proc()`, got %s", str);
|
||||
error(e->token, "Procedure type of `main` was expected to be `proc()`, got %s", str);
|
||||
gb_string_free(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_inline && is_no_inline) {
|
||||
error(ast_node_token(pd->type),
|
||||
"You cannot apply both `inline` and `no_inline` to a procedure");
|
||||
error_node(pd->type, "You cannot apply both `inline` and `no_inline` to a procedure");
|
||||
}
|
||||
|
||||
if (is_foreign && is_link_name) {
|
||||
error(ast_node_token(pd->type),
|
||||
"You cannot apply both `foreign` and `link_name` to a procedure");
|
||||
error_node(pd->type, "You cannot apply both `foreign` and `link_name` to a procedure");
|
||||
} else if (is_foreign && is_export) {
|
||||
error_node(pd->type, "You cannot apply both `foreign` and `export` to a procedure");
|
||||
}
|
||||
|
||||
|
||||
if (pd->body != NULL) {
|
||||
if (is_foreign) {
|
||||
error(ast_node_token(pd->body),
|
||||
"A procedure tagged as `#foreign` cannot have a body");
|
||||
error_node(pd->body, "A procedure tagged as `#foreign` cannot have a body");
|
||||
}
|
||||
|
||||
// TODO(bill): Is this the best option? What about passing to external shit?!
|
||||
// if (proc_type->Proc.calling_convention != ProcCC_Odin) {
|
||||
// error_node(d->proc_lit, "An internal procedure may only have the Odin calling convention");
|
||||
// proc_type->Proc.calling_convention = ProcCC_Odin;
|
||||
// }
|
||||
|
||||
d->scope = c->context.scope;
|
||||
|
||||
GB_ASSERT(pd->body->kind == AstNode_BlockStmt);
|
||||
@@ -403,11 +316,37 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
|
||||
|
||||
if (is_foreign) {
|
||||
MapEntity *fp = &c->info.foreign_procs;
|
||||
AstNodeProcDecl *proc_decl = &d->proc_decl->ProcDecl;
|
||||
String name = proc_decl->name->Ident.string;
|
||||
if (proc_decl->foreign_name.len > 0) {
|
||||
name = proc_decl->foreign_name;
|
||||
String name = e->token.string;
|
||||
if (pd->foreign_name.len > 0) {
|
||||
name = pd->foreign_name;
|
||||
}
|
||||
|
||||
AstNode *foreign_library = d->proc_lit->ProcLit.foreign_library;
|
||||
if (foreign_library == NULL) {
|
||||
error(e->token, "#foreign procedures must declare which library they are from");
|
||||
} else if (foreign_library->kind != AstNode_Ident) {
|
||||
error_node(foreign_library, "#foreign library names must be an identifier");
|
||||
} else {
|
||||
String name = foreign_library->Ident.string;
|
||||
Entity *found = scope_lookup_entity(c->context.scope, name);
|
||||
if (found == NULL) {
|
||||
if (str_eq(name, str_lit("_"))) {
|
||||
error_node(foreign_library, "`_` cannot be used as a value type");
|
||||
} else {
|
||||
error_node(foreign_library, "Undeclared name: %.*s", LIT(name));
|
||||
}
|
||||
} else if (found->kind != Entity_LibraryName) {
|
||||
error_node(foreign_library, "`_` cannot be used as a library name");
|
||||
} else {
|
||||
// TODO(bill): Extra stuff to do with library names?
|
||||
e->Procedure.foreign_library = found;
|
||||
add_entity_use(c, foreign_library, found);
|
||||
}
|
||||
}
|
||||
|
||||
e->Procedure.is_foreign = true;
|
||||
e->Procedure.foreign_name = name;
|
||||
|
||||
HashKey key = hash_string(name);
|
||||
Entity **found = map_entity_get(fp, key);
|
||||
if (found) {
|
||||
@@ -416,30 +355,38 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
|
||||
Type *this_type = base_type(e->type);
|
||||
Type *other_type = base_type(f->type);
|
||||
if (!are_signatures_similar_enough(this_type, other_type)) {
|
||||
error(ast_node_token(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_node(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 {
|
||||
map_entity_set(fp, key, e);
|
||||
}
|
||||
} else if (is_link_name) {
|
||||
MapEntity *fp = &c->info.foreign_procs;
|
||||
AstNodeProcDecl *proc_decl = &d->proc_decl->ProcDecl;
|
||||
String name = proc_decl->link_name;
|
||||
} else {
|
||||
String name = e->token.string;
|
||||
if (is_link_name) {
|
||||
name = pd->link_name;
|
||||
}
|
||||
|
||||
HashKey key = hash_string(name);
|
||||
Entity **found = map_entity_get(fp, key);
|
||||
if (found) {
|
||||
Entity *f = *found;
|
||||
TokenPos pos = f->token.pos;
|
||||
error(ast_node_token(d->proc_decl),
|
||||
"Non unique #link_name for procedure `%.*s`\n"
|
||||
"\tother at %.*s(%td:%td)",
|
||||
LIT(name), LIT(pos.file), pos.line, pos.column);
|
||||
} else {
|
||||
map_entity_set(fp, key, e);
|
||||
if (is_link_name || is_export) {
|
||||
MapEntity *fp = &c->info.foreign_procs;
|
||||
|
||||
e->Procedure.link_name = name;
|
||||
|
||||
HashKey key = hash_string(name);
|
||||
Entity **found = map_entity_get(fp, key);
|
||||
if (found) {
|
||||
Entity *f = *found;
|
||||
TokenPos pos = f->token.pos;
|
||||
// TODO(bill): Better error message?
|
||||
error_node(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_entity_set(fp, key, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -456,12 +403,14 @@ void check_var_decl(Checker *c, Entity *e, Entity **entities, isize entity_count
|
||||
}
|
||||
e->flags |= EntityFlag_Visited;
|
||||
|
||||
if (type_expr != NULL)
|
||||
e->type = check_type_extra(c, type_expr, NULL, NULL);
|
||||
if (type_expr != NULL) {
|
||||
e->type = check_type_extra(c, type_expr, NULL);
|
||||
}
|
||||
|
||||
if (init_expr == NULL) {
|
||||
if (type_expr == NULL)
|
||||
if (type_expr == NULL) {
|
||||
e->type = t_invalid;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -473,8 +422,9 @@ void check_var_decl(Checker *c, Entity *e, Entity **entities, isize entity_count
|
||||
}
|
||||
|
||||
if (type_expr != NULL) {
|
||||
for (isize i = 0; i < entity_count; i++)
|
||||
for (isize i = 0; i < entity_count; i++) {
|
||||
entities[i]->type = e->type;
|
||||
}
|
||||
}
|
||||
|
||||
AstNodeArray inits;
|
||||
@@ -483,12 +433,65 @@ void check_var_decl(Checker *c, Entity *e, Entity **entities, isize entity_count
|
||||
check_init_variables(c, entities, entity_count, inits, str_lit("variable declaration"));
|
||||
}
|
||||
|
||||
|
||||
void check_entity_decl(Checker *c, Entity *e, DeclInfo *d, Type *named_type) {
|
||||
if (e->type != NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (d == NULL) {
|
||||
DeclInfo **found = map_decl_info_get(&c->info.entities, hash_pointer(e));
|
||||
if (found) {
|
||||
d = *found;
|
||||
} else {
|
||||
// TODO(bill): Err here?
|
||||
e->type = t_invalid;
|
||||
set_base_type(named_type, t_invalid);
|
||||
return;
|
||||
// GB_PANIC("`%.*s` should been declared!", LIT(e->token.string));
|
||||
}
|
||||
}
|
||||
|
||||
CheckerContext prev = c->context;
|
||||
c->context.scope = d->scope;
|
||||
c->context.decl = d;
|
||||
|
||||
switch (e->kind) {
|
||||
case Entity_Variable:
|
||||
check_var_decl(c, e, d->entities, d->entity_count, d->type_expr, d->init_expr);
|
||||
break;
|
||||
case Entity_Constant:
|
||||
check_const_decl(c, e, d->type_expr, d->init_expr, named_type);
|
||||
break;
|
||||
case Entity_TypeName:
|
||||
check_type_decl(c, e, d->type_expr, named_type);
|
||||
break;
|
||||
case Entity_Procedure:
|
||||
check_proc_lit(c, e, d);
|
||||
break;
|
||||
}
|
||||
|
||||
c->context = prev;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void check_proc_body(Checker *c, Token token, DeclInfo *decl, Type *type, AstNode *body) {
|
||||
GB_ASSERT(body->kind == AstNode_BlockStmt);
|
||||
|
||||
String proc_name = {0};
|
||||
if (token.kind == Token_Ident) {
|
||||
proc_name = token.string;
|
||||
} else {
|
||||
// TODO(bill): Better name
|
||||
proc_name = str_lit("(anonymous-procedure)");
|
||||
}
|
||||
|
||||
CheckerContext old_context = c->context;
|
||||
c->context.scope = decl->scope;
|
||||
c->context.decl = decl;
|
||||
c->context.proc_name = proc_name;
|
||||
|
||||
|
||||
GB_ASSERT(type->kind == Type_Proc);
|
||||
if (type->Proc.param_count > 0) {
|
||||
@@ -499,6 +502,7 @@ void check_proc_body(Checker *c, Token token, DeclInfo *decl, Type *type, AstNod
|
||||
if (!(e->flags & EntityFlag_Anonymous)) {
|
||||
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)) {
|
||||
@@ -508,6 +512,7 @@ void check_proc_body(Checker *c, Token token, DeclInfo *decl, Type *type, AstNod
|
||||
Entity *f = (*found)->elements.entries.e[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(e->token, "Namespace collision while `using` `%.*s` of: %.*s", LIT(name), LIT(prev->token.string));
|
||||
@@ -525,11 +530,14 @@ void check_proc_body(Checker *c, Token token, DeclInfo *decl, Type *type, AstNod
|
||||
push_procedure(c, type);
|
||||
{
|
||||
ast_node(bs, BlockStmt, body);
|
||||
// TODO(bill): Check declarations first (except mutable variable declarations)
|
||||
check_stmt_list(c, bs->stmts, 0);
|
||||
if (type->Proc.result_count > 0) {
|
||||
if (!check_is_terminating(body)) {
|
||||
error(bs->close, "Missing return statement at the end of the procedure");
|
||||
if (token.kind == Token_Ident) {
|
||||
error(bs->close, "Missing return statement at the end of the procedure `%.*s`", LIT(token.string));
|
||||
} else {
|
||||
error(bs->close, "Missing return statement at the end of the procedure");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+1794
-1162
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+7
-61
@@ -12,72 +12,18 @@ gbAllocator heap_allocator(void) {
|
||||
gb_global String global_module_path = {0};
|
||||
gb_global bool global_module_path_set = false;
|
||||
|
||||
gb_global gbScratchMemory scratch_memory = {0};
|
||||
|
||||
String get_module_dir() {
|
||||
String path = global_module_path;
|
||||
Array(wchar_t) path_buf;
|
||||
isize len, i;
|
||||
gbTempArenaMemory tmp;
|
||||
wchar_t *text;
|
||||
|
||||
if (global_module_path_set) {
|
||||
return global_module_path;
|
||||
}
|
||||
|
||||
array_init_count(&path_buf, heap_allocator(), 300);
|
||||
|
||||
len = 0;
|
||||
for (;;) {
|
||||
len = GetModuleFileNameW(NULL, &path_buf.e[0], path_buf.count);
|
||||
if (len == 0) {
|
||||
return make_string(NULL, 0);
|
||||
}
|
||||
if (len < path_buf.count) {
|
||||
break;
|
||||
}
|
||||
array_resize(&path_buf, 2*path_buf.count + 300);
|
||||
}
|
||||
|
||||
tmp = gb_temp_arena_memory_begin(&string_buffer_arena);
|
||||
|
||||
text = gb_alloc_array(string_buffer_allocator, wchar_t, len+1);
|
||||
|
||||
GetModuleFileNameW(NULL, text, len);
|
||||
path = string16_to_string(heap_allocator(), make_string16(text, len));
|
||||
for (i = path.len-1; i >= 0; i--) {
|
||||
u8 c = path.text[i];
|
||||
if (c == '/' || c == '\\') {
|
||||
break;
|
||||
}
|
||||
path.len--;
|
||||
}
|
||||
|
||||
global_module_path = path;
|
||||
global_module_path_set = true;
|
||||
|
||||
gb_temp_arena_memory_end(tmp);
|
||||
|
||||
array_free(&path_buf);
|
||||
|
||||
return path;
|
||||
void init_scratch_memory(isize size) {
|
||||
void *memory = gb_alloc(heap_allocator(), size);
|
||||
gb_scratch_memory_init(&scratch_memory, memory, size);
|
||||
}
|
||||
|
||||
String path_to_fullpath(gbAllocator a, String s) {
|
||||
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&string_buffer_arena);
|
||||
String16 string16 = string_to_string16(string_buffer_allocator, s);
|
||||
String result = {0};
|
||||
|
||||
DWORD len = GetFullPathNameW(string16.text, 0, NULL, NULL);
|
||||
if (len != 0) {
|
||||
wchar_t *text = gb_alloc_array(string_buffer_allocator, wchar_t, len+1);
|
||||
GetFullPathNameW(string16.text, len, text, NULL);
|
||||
text[len] = 0;
|
||||
result = string16_to_string(a, make_string16(text, len));
|
||||
}
|
||||
gb_temp_arena_memory_end(tmp);
|
||||
return result;
|
||||
gbAllocator scratch_allocator(void) {
|
||||
return gb_scratch_allocator(&scratch_memory);
|
||||
}
|
||||
|
||||
|
||||
i64 next_pow2(i64 n) {
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
|
||||
@@ -12,6 +12,7 @@ typedef enum ImplicitValueId ImplicitValueId;
|
||||
ENTITY_KIND(Procedure) \
|
||||
ENTITY_KIND(Builtin) \
|
||||
ENTITY_KIND(ImportName) \
|
||||
ENTITY_KIND(LibraryName) \
|
||||
ENTITY_KIND(Nil) \
|
||||
ENTITY_KIND(ImplicitValue) \
|
||||
ENTITY_KIND(Count)
|
||||
@@ -35,8 +36,16 @@ typedef enum EntityFlag {
|
||||
EntityFlag_Field = 1<<3,
|
||||
EntityFlag_Param = 1<<4,
|
||||
EntityFlag_VectorElem = 1<<5,
|
||||
EntityFlag_Ellipsis = 1<<6,
|
||||
EntityFlag_NoAlias = 1<<7,
|
||||
} EntityFlag;
|
||||
|
||||
typedef enum OverloadKind {
|
||||
Overload_No = -1,
|
||||
Overload_Unknown = 0,
|
||||
Overload_Yes = +1,
|
||||
} OverloadKind;
|
||||
|
||||
typedef struct Entity Entity;
|
||||
struct Entity {
|
||||
EntityKind kind;
|
||||
@@ -55,11 +64,20 @@ struct Entity {
|
||||
ExactValue value;
|
||||
} Constant;
|
||||
struct {
|
||||
i32 field_index;
|
||||
i32 field_src_index;
|
||||
i32 field_index;
|
||||
i32 field_src_index;
|
||||
bool is_immutable;
|
||||
bool is_thread_local;
|
||||
} Variable;
|
||||
i32 TypeName;
|
||||
i32 Procedure;
|
||||
struct {
|
||||
bool is_foreign;
|
||||
String foreign_name;
|
||||
Entity * foreign_library;
|
||||
String link_name;
|
||||
u64 tags;
|
||||
OverloadKind overload_kind;
|
||||
} Procedure;
|
||||
struct {
|
||||
BuiltinProcId id;
|
||||
} Builtin;
|
||||
@@ -67,8 +85,13 @@ struct Entity {
|
||||
String path;
|
||||
String name;
|
||||
Scope *scope;
|
||||
bool used;
|
||||
bool used;
|
||||
} ImportName;
|
||||
struct {
|
||||
String path;
|
||||
String name;
|
||||
bool used;
|
||||
} LibraryName;
|
||||
i32 Nil;
|
||||
struct {
|
||||
// TODO(bill): Should this be a user-level construct rather than compiler-level?
|
||||
@@ -78,6 +101,7 @@ struct Entity {
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Entity *alloc_entity(gbAllocator a, EntityKind kind, Scope *scope, Token token, Type *type) {
|
||||
Entity *entity = gb_alloc_item(a, Entity);
|
||||
entity->kind = kind;
|
||||
@@ -87,13 +111,15 @@ Entity *alloc_entity(gbAllocator a, EntityKind kind, Scope *scope, Token token,
|
||||
return entity;
|
||||
}
|
||||
|
||||
Entity *make_entity_variable(gbAllocator a, Scope *scope, Token token, Type *type) {
|
||||
Entity *make_entity_variable(gbAllocator a, Scope *scope, Token token, Type *type, bool is_immutable) {
|
||||
Entity *entity = alloc_entity(a, Entity_Variable, scope, token, type);
|
||||
entity->Variable.is_immutable = is_immutable;
|
||||
return entity;
|
||||
}
|
||||
|
||||
Entity *make_entity_using_variable(gbAllocator a, Entity *parent, Token token, Type *type) {
|
||||
GB_ASSERT(parent != NULL);
|
||||
token.pos = parent->token.pos;
|
||||
Entity *entity = alloc_entity(a, Entity_Variable, parent->scope, token, type);
|
||||
entity->using_parent = parent;
|
||||
entity->flags |= EntityFlag_Anonymous;
|
||||
@@ -112,16 +138,16 @@ Entity *make_entity_type_name(gbAllocator a, Scope *scope, Token token, Type *ty
|
||||
return entity;
|
||||
}
|
||||
|
||||
Entity *make_entity_param(gbAllocator a, Scope *scope, Token token, Type *type, bool anonymous) {
|
||||
Entity *entity = make_entity_variable(a, scope, token, type);
|
||||
Entity *make_entity_param(gbAllocator a, Scope *scope, Token token, Type *type, bool anonymous, bool is_immutable) {
|
||||
Entity *entity = make_entity_variable(a, scope, token, type, is_immutable);
|
||||
entity->flags |= EntityFlag_Used;
|
||||
entity->flags |= EntityFlag_Anonymous*(anonymous != 0);
|
||||
if (anonymous) entity->flags |= EntityFlag_Anonymous;
|
||||
entity->flags |= EntityFlag_Param;
|
||||
return entity;
|
||||
}
|
||||
|
||||
Entity *make_entity_field(gbAllocator a, Scope *scope, Token token, Type *type, bool anonymous, i32 field_src_index) {
|
||||
Entity *entity = make_entity_variable(a, scope, token, type);
|
||||
Entity *entity = make_entity_variable(a, scope, token, type, false);
|
||||
entity->Variable.field_src_index = field_src_index;
|
||||
entity->Variable.field_index = field_src_index;
|
||||
entity->flags |= EntityFlag_Field;
|
||||
@@ -130,7 +156,7 @@ Entity *make_entity_field(gbAllocator a, Scope *scope, Token token, Type *type,
|
||||
}
|
||||
|
||||
Entity *make_entity_vector_elem(gbAllocator a, Scope *scope, Token token, Type *type, i32 field_src_index) {
|
||||
Entity *entity = make_entity_variable(a, scope, token, type);
|
||||
Entity *entity = make_entity_variable(a, scope, token, type, false);
|
||||
entity->Variable.field_src_index = field_src_index;
|
||||
entity->Variable.field_index = field_src_index;
|
||||
entity->flags |= EntityFlag_Field;
|
||||
@@ -138,8 +164,9 @@ Entity *make_entity_vector_elem(gbAllocator a, Scope *scope, Token token, Type *
|
||||
return entity;
|
||||
}
|
||||
|
||||
Entity *make_entity_procedure(gbAllocator a, Scope *scope, Token token, Type *signature_type) {
|
||||
Entity *make_entity_procedure(gbAllocator a, Scope *scope, Token token, Type *signature_type, u64 tags) {
|
||||
Entity *entity = alloc_entity(a, Entity_Procedure, scope, token, signature_type);
|
||||
entity->Procedure.tags = tags;
|
||||
return entity;
|
||||
}
|
||||
|
||||
@@ -158,6 +185,14 @@ Entity *make_entity_import_name(gbAllocator a, Scope *scope, Token token, Type *
|
||||
return entity;
|
||||
}
|
||||
|
||||
Entity *make_entity_library_name(gbAllocator a, Scope *scope, Token token, Type *type,
|
||||
String path, String name) {
|
||||
Entity *entity = alloc_entity(a, Entity_LibraryName, scope, token, type);
|
||||
entity->LibraryName.path = path;
|
||||
entity->LibraryName.name = name;
|
||||
return entity;
|
||||
}
|
||||
|
||||
Entity *make_entity_nil(gbAllocator a, String name, Type *type) {
|
||||
Token token = make_token_ident(name);
|
||||
Entity *entity = alloc_entity(a, Entity_Nil, NULL, token, type);
|
||||
@@ -172,8 +207,8 @@ Entity *make_entity_implicit_value(gbAllocator a, String name, Type *type, Impli
|
||||
}
|
||||
|
||||
|
||||
Entity *make_entity_dummy_variable(gbAllocator a, Scope *file_scope, Token token) {
|
||||
Entity *make_entity_dummy_variable(gbAllocator a, Scope *scope, Token token) {
|
||||
token.string = str_lit("_");
|
||||
return make_entity_variable(a, file_scope, token, NULL);
|
||||
return make_entity_variable(a, scope, token, NULL, false);
|
||||
}
|
||||
|
||||
+47
-19
@@ -21,7 +21,7 @@ typedef enum ExactValueKind {
|
||||
typedef struct ExactValue {
|
||||
ExactValueKind kind;
|
||||
union {
|
||||
bool value_bool;
|
||||
bool value_bool;
|
||||
String value_string;
|
||||
i64 value_integer; // NOTE(bill): This must be an integer and not a pointer
|
||||
f64 value_float;
|
||||
@@ -54,30 +54,57 @@ ExactValue make_exact_value_string(String string) {
|
||||
return result;
|
||||
}
|
||||
|
||||
ExactValue make_exact_value_integer_from_string(String string) {
|
||||
// TODO(bill): Allow for numbers with underscores in them
|
||||
ExactValue result = {ExactValue_Integer};
|
||||
i32 base = 10;
|
||||
if (string.text[0] == '0') {
|
||||
switch (string.text[1]) {
|
||||
case 'b': base = 2; break;
|
||||
case 'o': base = 8; break;
|
||||
case 'd': base = 10; break;
|
||||
case 'x': base = 16; break;
|
||||
}
|
||||
}
|
||||
|
||||
result.value_integer = gb_str_to_i64(cast(char *)string.text, NULL, base);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ExactValue make_exact_value_integer(i64 i) {
|
||||
ExactValue result = {ExactValue_Integer};
|
||||
result.value_integer = i;
|
||||
return result;
|
||||
}
|
||||
|
||||
ExactValue make_exact_value_integer_from_string(String string) {
|
||||
// TODO(bill): Allow for numbers with underscores in them
|
||||
i32 base = 10;
|
||||
bool has_prefix = false;
|
||||
if (string.len > 2 && string.text[0] == '0') {
|
||||
switch (string.text[1]) {
|
||||
case 'b': base = 2; has_prefix = true; break;
|
||||
case 'o': base = 8; has_prefix = true; break;
|
||||
case 'd': base = 10; has_prefix = true; break;
|
||||
case 'x': base = 16; has_prefix = true; break;
|
||||
}
|
||||
}
|
||||
|
||||
u8 *text = string.text;
|
||||
isize len = string.len;
|
||||
if (has_prefix) {
|
||||
text += 2;
|
||||
len -= 2;
|
||||
}
|
||||
|
||||
i64 result = 0;
|
||||
for (isize i = 0; i < len; i++) {
|
||||
Rune r = cast(Rune)text[i];
|
||||
if (r == '_') {
|
||||
continue;
|
||||
}
|
||||
i64 v = 0;
|
||||
if (gb_char_is_digit(r)) {
|
||||
v = r - '0';
|
||||
} else if (gb_char_is_hex_digit(r)) {
|
||||
v = gb_hex_digit_to_int(r);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
result *= base;
|
||||
result += v;
|
||||
}
|
||||
|
||||
|
||||
return make_exact_value_integer(result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
ExactValue make_exact_value_float_from_string(String string) {
|
||||
// TODO(bill): Allow for numbers with underscores in them
|
||||
ExactValue result = {ExactValue_Float};
|
||||
@@ -306,6 +333,7 @@ ExactValue exact_binary_operator_value(TokenKind op, ExactValue x, ExactValue y)
|
||||
case Token_Shr: c = a >> b; break;
|
||||
default: goto error;
|
||||
}
|
||||
|
||||
return make_exact_value_integer(c);
|
||||
} break;
|
||||
|
||||
|
||||
+4
-8
@@ -4112,7 +4112,7 @@ gb_inline i64 gb_atomic64_fetch_and(gbAtomic64 volatile *a, i64 operand) {
|
||||
|
||||
gb_inline i64 gb_atomic64_fetch_or(gbAtomic64 volatile *a, i64 operand) {
|
||||
#if defined(GB_ARCH_64_BIT)
|
||||
return _InterlockedAnd64(cast(i64 volatile *)a, operand);
|
||||
return _InterlockedOr64(cast(i64 volatile *)a, operand);
|
||||
#elif GB_CPU_X86
|
||||
i64 expected = a->value;
|
||||
for (;;) {
|
||||
@@ -4819,14 +4819,10 @@ GB_ALLOCATOR_PROC(gb_heap_allocator_proc) {
|
||||
#else
|
||||
// TODO(bill): *nix version that's decent
|
||||
case gbAllocation_Alloc: {
|
||||
gbAllocationHeader *header;
|
||||
isize total_size = size + alignment + gb_size_of(gbAllocationHeader);
|
||||
ptr = malloc(total_size);
|
||||
header = cast(gbAllocationHeader *)ptr;
|
||||
ptr = gb_align_forward(header+1, alignment);
|
||||
gb_allocation_header_fill(header, ptr, size);
|
||||
if (flags & gbAllocatorFlag_ClearToZero)
|
||||
ptr = aligned_alloc(alignment, size);
|
||||
if (flags & gbAllocatorFlag_ClearToZero) {
|
||||
gb_zero_size(ptr, size);
|
||||
}
|
||||
} break;
|
||||
|
||||
case gbAllocation_Free: {
|
||||
|
||||
+117
-119
@@ -1,95 +1,93 @@
|
||||
// Optimizations for the SSA code
|
||||
// Optimizations for the IR code
|
||||
|
||||
void ssa_opt_add_operands(ssaValueArray *ops, ssaInstr *i) {
|
||||
void ir_opt_add_operands(irValueArray *ops, irInstr *i) {
|
||||
switch (i->kind) {
|
||||
case ssaInstr_Comment:
|
||||
case irInstr_Comment:
|
||||
break;
|
||||
case ssaInstr_Local:
|
||||
case irInstr_Local:
|
||||
break;
|
||||
case ssaInstr_ZeroInit:
|
||||
case irInstr_ZeroInit:
|
||||
array_add(ops, i->ZeroInit.address);
|
||||
break;
|
||||
case ssaInstr_Store:
|
||||
case irInstr_Store:
|
||||
array_add(ops, i->Store.address);
|
||||
array_add(ops, i->Store.value);
|
||||
break;
|
||||
case ssaInstr_Load:
|
||||
case irInstr_Load:
|
||||
array_add(ops, i->Load.address);
|
||||
break;
|
||||
case ssaInstr_ArrayElementPtr:
|
||||
case irInstr_ArrayElementPtr:
|
||||
array_add(ops, i->ArrayElementPtr.address);
|
||||
array_add(ops, i->ArrayElementPtr.elem_index);
|
||||
break;
|
||||
case ssaInstr_StructElementPtr:
|
||||
case irInstr_StructElementPtr:
|
||||
array_add(ops, i->StructElementPtr.address);
|
||||
break;
|
||||
case ssaInstr_PtrOffset:
|
||||
case irInstr_PtrOffset:
|
||||
array_add(ops, i->PtrOffset.address);
|
||||
array_add(ops, i->PtrOffset.offset);
|
||||
break;
|
||||
case ssaInstr_ArrayExtractValue:
|
||||
array_add(ops, i->ArrayExtractValue.address);
|
||||
break;
|
||||
case ssaInstr_StructExtractValue:
|
||||
case irInstr_StructExtractValue:
|
||||
array_add(ops, i->StructExtractValue.address);
|
||||
break;
|
||||
case ssaInstr_Conv:
|
||||
case irInstr_Conv:
|
||||
array_add(ops, i->Conv.value);
|
||||
break;
|
||||
case ssaInstr_Jump:
|
||||
case irInstr_Jump:
|
||||
break;
|
||||
case ssaInstr_If:
|
||||
case irInstr_If:
|
||||
array_add(ops, i->If.cond);
|
||||
break;
|
||||
case ssaInstr_Return:
|
||||
case irInstr_Return:
|
||||
if (i->Return.value != NULL) {
|
||||
array_add(ops, i->Return.value);
|
||||
}
|
||||
break;
|
||||
case ssaInstr_Select:
|
||||
case irInstr_Select:
|
||||
array_add(ops, i->Select.cond);
|
||||
break;
|
||||
case ssaInstr_Phi:
|
||||
case irInstr_Phi:
|
||||
for_array(j, i->Phi.edges) {
|
||||
array_add(ops, i->Phi.edges.e[j]);
|
||||
}
|
||||
break;
|
||||
case ssaInstr_Unreachable: break;
|
||||
case ssaInstr_BinaryOp:
|
||||
case irInstr_Unreachable:
|
||||
break;
|
||||
case irInstr_UnaryOp:
|
||||
array_add(ops, i->UnaryOp.expr);
|
||||
break;
|
||||
case irInstr_BinaryOp:
|
||||
array_add(ops, i->BinaryOp.left);
|
||||
array_add(ops, i->BinaryOp.right);
|
||||
break;
|
||||
case ssaInstr_Call:
|
||||
case irInstr_Call:
|
||||
array_add(ops, i->Call.value);
|
||||
for (isize j = 0; j < i->Call.arg_count; j++) {
|
||||
array_add(ops, i->Call.args[j]);
|
||||
}
|
||||
break;
|
||||
case ssaInstr_VectorExtractElement:
|
||||
case irInstr_VectorExtractElement:
|
||||
array_add(ops, i->VectorExtractElement.vector);
|
||||
array_add(ops, i->VectorExtractElement.index);
|
||||
break;
|
||||
case ssaInstr_VectorInsertElement:
|
||||
case irInstr_VectorInsertElement:
|
||||
array_add(ops, i->VectorInsertElement.vector);
|
||||
array_add(ops, i->VectorInsertElement.elem);
|
||||
array_add(ops, i->VectorInsertElement.index);
|
||||
break;
|
||||
case ssaInstr_VectorShuffle:
|
||||
case irInstr_VectorShuffle:
|
||||
array_add(ops, i->VectorShuffle.vector);
|
||||
break;
|
||||
case ssaInstr_StartupRuntime:
|
||||
case irInstr_StartupRuntime:
|
||||
break;
|
||||
case ssaInstr_BoundsCheck:
|
||||
case irInstr_BoundsCheck:
|
||||
array_add(ops, i->BoundsCheck.index);
|
||||
array_add(ops, i->BoundsCheck.len);
|
||||
break;
|
||||
case ssaInstr_SliceBoundsCheck:
|
||||
case irInstr_SliceBoundsCheck:
|
||||
array_add(ops, i->SliceBoundsCheck.low);
|
||||
array_add(ops, i->SliceBoundsCheck.high);
|
||||
array_add(ops, i->SliceBoundsCheck.max);
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,26 +95,26 @@ void ssa_opt_add_operands(ssaValueArray *ops, ssaInstr *i) {
|
||||
|
||||
|
||||
|
||||
void ssa_opt_block_replace_pred(ssaBlock *b, ssaBlock *from, ssaBlock *to) {
|
||||
void ir_opt_block_replace_pred(irBlock *b, irBlock *from, irBlock *to) {
|
||||
for_array(i, b->preds) {
|
||||
ssaBlock *pred = b->preds.e[i];
|
||||
irBlock *pred = b->preds.e[i];
|
||||
if (pred == from) {
|
||||
b->preds.e[i] = to;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ssa_opt_block_replace_succ(ssaBlock *b, ssaBlock *from, ssaBlock *to) {
|
||||
void ir_opt_block_replace_succ(irBlock *b, irBlock *from, irBlock *to) {
|
||||
for_array(i, b->succs) {
|
||||
ssaBlock *succ = b->succs.e[i];
|
||||
irBlock *succ = b->succs.e[i];
|
||||
if (succ == from) {
|
||||
b->succs.e[i] = to;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ssa_opt_block_has_phi(ssaBlock *b) {
|
||||
return b->instrs.e[0]->Instr.kind == ssaInstr_Phi;
|
||||
bool ir_opt_block_has_phi(irBlock *b) {
|
||||
return b->instrs.e[0]->Instr.kind == irInstr_Phi;
|
||||
}
|
||||
|
||||
|
||||
@@ -128,11 +126,11 @@ bool ssa_opt_block_has_phi(ssaBlock *b) {
|
||||
|
||||
|
||||
|
||||
ssaValueArray ssa_get_block_phi_nodes(ssaBlock *b) {
|
||||
ssaValueArray phis = {0};
|
||||
irValueArray ir_get_block_phi_nodes(irBlock *b) {
|
||||
irValueArray phis = {0};
|
||||
for_array(i, b->instrs) {
|
||||
ssaInstr *instr = &b->instrs.e[i]->Instr;
|
||||
if (instr->kind != ssaInstr_Phi) {
|
||||
irInstr *instr = &b->instrs.e[i]->Instr;
|
||||
if (instr->kind != irInstr_Phi) {
|
||||
phis = b->instrs;
|
||||
phis.count = i;
|
||||
return phis;
|
||||
@@ -141,15 +139,15 @@ ssaValueArray ssa_get_block_phi_nodes(ssaBlock *b) {
|
||||
return phis;
|
||||
}
|
||||
|
||||
void ssa_remove_pred(ssaBlock *b, ssaBlock *p) {
|
||||
ssaValueArray phis = ssa_get_block_phi_nodes(b);
|
||||
void ir_remove_pred(irBlock *b, irBlock *p) {
|
||||
irValueArray phis = ir_get_block_phi_nodes(b);
|
||||
isize i = 0;
|
||||
for_array(j, b->preds) {
|
||||
ssaBlock *pred = b->preds.e[j];
|
||||
irBlock *pred = b->preds.e[j];
|
||||
if (pred != p) {
|
||||
b->preds.e[i] = b->preds.e[j];
|
||||
for_array(k, phis) {
|
||||
ssaInstrPhi *phi = &phis.e[k]->Instr.Phi;
|
||||
irInstrPhi *phi = &phis.e[k]->Instr.Phi;
|
||||
phi->edges.e[i] = phi->edges.e[j];
|
||||
}
|
||||
i++;
|
||||
@@ -157,16 +155,16 @@ void ssa_remove_pred(ssaBlock *b, ssaBlock *p) {
|
||||
}
|
||||
b->preds.count = i;
|
||||
for_array(k, phis) {
|
||||
ssaInstrPhi *phi = &phis.e[k]->Instr.Phi;
|
||||
irInstrPhi *phi = &phis.e[k]->Instr.Phi;
|
||||
phi->edges.count = i;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ssa_remove_dead_blocks(ssaProcedure *proc) {
|
||||
void ir_remove_dead_blocks(irProcedure *proc) {
|
||||
isize j = 0;
|
||||
for_array(i, proc->blocks) {
|
||||
ssaBlock *b = proc->blocks.e[i];
|
||||
irBlock *b = proc->blocks.e[i];
|
||||
if (b == NULL) {
|
||||
continue;
|
||||
}
|
||||
@@ -177,34 +175,34 @@ void ssa_remove_dead_blocks(ssaProcedure *proc) {
|
||||
proc->blocks.count = j;
|
||||
}
|
||||
|
||||
void ssa_mark_reachable(ssaBlock *b) {
|
||||
void ir_mark_reachable(irBlock *b) {
|
||||
isize const WHITE = 0;
|
||||
isize const BLACK = -1;
|
||||
b->index = BLACK;
|
||||
for_array(i, b->succs) {
|
||||
ssaBlock *succ = b->succs.e[i];
|
||||
irBlock *succ = b->succs.e[i];
|
||||
if (succ->index == WHITE) {
|
||||
ssa_mark_reachable(succ);
|
||||
ir_mark_reachable(succ);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ssa_remove_unreachable_blocks(ssaProcedure *proc) {
|
||||
void ir_remove_unreachable_blocks(irProcedure *proc) {
|
||||
isize const WHITE = 0;
|
||||
isize const BLACK = -1;
|
||||
for_array(i, proc->blocks) {
|
||||
proc->blocks.e[i]->index = WHITE;
|
||||
}
|
||||
|
||||
ssa_mark_reachable(proc->blocks.e[0]);
|
||||
ir_mark_reachable(proc->blocks.e[0]);
|
||||
|
||||
for_array(i, proc->blocks) {
|
||||
ssaBlock *b = proc->blocks.e[i];
|
||||
irBlock *b = proc->blocks.e[i];
|
||||
if (b->index == WHITE) {
|
||||
for_array(j, b->succs) {
|
||||
ssaBlock *c = b->succs.e[j];
|
||||
irBlock *c = b->succs.e[j];
|
||||
if (c->index == BLACK) {
|
||||
ssa_remove_pred(c, b);
|
||||
ir_remove_pred(c, b);
|
||||
}
|
||||
}
|
||||
// NOTE(bill): Mark as empty but don't actually free it
|
||||
@@ -212,26 +210,26 @@ void ssa_remove_unreachable_blocks(ssaProcedure *proc) {
|
||||
proc->blocks.e[i] = NULL;
|
||||
}
|
||||
}
|
||||
ssa_remove_dead_blocks(proc);
|
||||
ir_remove_dead_blocks(proc);
|
||||
}
|
||||
|
||||
bool ssa_opt_block_fusion(ssaProcedure *proc, ssaBlock *a) {
|
||||
bool ir_opt_block_fusion(irProcedure *proc, irBlock *a) {
|
||||
if (a->succs.count != 1) {
|
||||
return false;
|
||||
}
|
||||
ssaBlock *b = a->succs.e[0];
|
||||
irBlock *b = a->succs.e[0];
|
||||
if (b->preds.count != 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ssa_opt_block_has_phi(b)) {
|
||||
if (ir_opt_block_has_phi(b)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
array_pop(&a->instrs); // Remove branch at end
|
||||
for_array(i, b->instrs) {
|
||||
array_add(&a->instrs, b->instrs.e[i]);
|
||||
ssa_set_instr_parent(b->instrs.e[i], a);
|
||||
ir_set_instr_parent(b->instrs.e[i], a);
|
||||
}
|
||||
|
||||
array_clear(&a->succs);
|
||||
@@ -241,28 +239,28 @@ bool ssa_opt_block_fusion(ssaProcedure *proc, ssaBlock *a) {
|
||||
|
||||
// Fix preds links
|
||||
for_array(i, b->succs) {
|
||||
ssa_opt_block_replace_pred(b->succs.e[i], b, a);
|
||||
ir_opt_block_replace_pred(b->succs.e[i], b, a);
|
||||
}
|
||||
|
||||
proc->blocks.e[b->index] = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
void ssa_opt_blocks(ssaProcedure *proc) {
|
||||
ssa_remove_unreachable_blocks(proc);
|
||||
void ir_opt_blocks(irProcedure *proc) {
|
||||
ir_remove_unreachable_blocks(proc);
|
||||
|
||||
#if 1
|
||||
bool changed = true;
|
||||
while (changed) {
|
||||
changed = false;
|
||||
for_array(i, proc->blocks) {
|
||||
ssaBlock *b = proc->blocks.e[i];
|
||||
irBlock *b = proc->blocks.e[i];
|
||||
if (b == NULL) {
|
||||
continue;
|
||||
}
|
||||
GB_ASSERT(b->index == i);
|
||||
|
||||
if (ssa_opt_block_fusion(proc, b)) {
|
||||
if (ir_opt_block_fusion(proc, b)) {
|
||||
changed = true;
|
||||
}
|
||||
// TODO(bill): other simple block optimizations
|
||||
@@ -270,25 +268,25 @@ void ssa_opt_blocks(ssaProcedure *proc) {
|
||||
}
|
||||
#endif
|
||||
|
||||
ssa_remove_dead_blocks(proc);
|
||||
ir_remove_dead_blocks(proc);
|
||||
}
|
||||
void ssa_opt_build_referrers(ssaProcedure *proc) {
|
||||
void ir_opt_build_referrers(irProcedure *proc) {
|
||||
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&proc->module->tmp_arena);
|
||||
|
||||
ssaValueArray ops = {0}; // NOTE(bill): Act as a buffer
|
||||
irValueArray ops = {0}; // NOTE(bill): Act as a buffer
|
||||
array_init_reserve(&ops, proc->module->tmp_allocator, 64); // HACK(bill): This _could_ overflow the temp arena
|
||||
for_array(i, proc->blocks) {
|
||||
ssaBlock *b = proc->blocks.e[i];
|
||||
irBlock *b = proc->blocks.e[i];
|
||||
for_array(j, b->instrs) {
|
||||
ssaValue *instr = b->instrs.e[j];
|
||||
irValue *instr = b->instrs.e[j];
|
||||
array_clear(&ops);
|
||||
ssa_opt_add_operands(&ops, &instr->Instr);
|
||||
ir_opt_add_operands(&ops, &instr->Instr);
|
||||
for_array(k, ops) {
|
||||
ssaValue *op = ops.e[k];
|
||||
irValue *op = ops.e[k];
|
||||
if (op == NULL) {
|
||||
continue;
|
||||
}
|
||||
ssaValueArray *refs = ssa_value_referrers(op);
|
||||
irValueArray *refs = ir_value_referrers(op);
|
||||
if (refs != NULL) {
|
||||
array_add(refs, instr);
|
||||
}
|
||||
@@ -307,36 +305,36 @@ void ssa_opt_build_referrers(ssaProcedure *proc) {
|
||||
|
||||
// State of Lengauer-Tarjan algorithm
|
||||
// Based on this paper: http://jgaa.info/accepted/2006/GeorgiadisTarjanWerneck2006.10.1.pdf
|
||||
typedef struct ssaLTState {
|
||||
typedef struct irLTState {
|
||||
isize count;
|
||||
// NOTE(bill): These are arrays
|
||||
ssaBlock **sdom; // Semidominator
|
||||
ssaBlock **parent; // Parent in DFS traversal of CFG
|
||||
ssaBlock **ancestor;
|
||||
} ssaLTState;
|
||||
irBlock **sdom; // Semidominator
|
||||
irBlock **parent; // Parent in DFS traversal of CFG
|
||||
irBlock **ancestor;
|
||||
} irLTState;
|
||||
|
||||
// §2.2 - bottom of page
|
||||
void ssa_lt_link(ssaLTState *lt, ssaBlock *p, ssaBlock *q) {
|
||||
void ir_lt_link(irLTState *lt, irBlock *p, irBlock *q) {
|
||||
lt->ancestor[q->index] = p;
|
||||
}
|
||||
|
||||
i32 ssa_lt_depth_first_search(ssaLTState *lt, ssaBlock *p, i32 i, ssaBlock **preorder) {
|
||||
i32 ir_lt_depth_first_search(irLTState *lt, irBlock *p, i32 i, irBlock **preorder) {
|
||||
preorder[i] = p;
|
||||
p->dom.pre = i++;
|
||||
lt->sdom[p->index] = p;
|
||||
ssa_lt_link(lt, NULL, p);
|
||||
ir_lt_link(lt, NULL, p);
|
||||
for_array(index, p->succs) {
|
||||
ssaBlock *q = p->succs.e[index];
|
||||
irBlock *q = p->succs.e[index];
|
||||
if (lt->sdom[q->index] == NULL) {
|
||||
lt->parent[q->index] = p;
|
||||
i = ssa_lt_depth_first_search(lt, q, i, preorder);
|
||||
i = ir_lt_depth_first_search(lt, q, i, preorder);
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
ssaBlock *ssa_lt_eval(ssaLTState *lt, ssaBlock *v) {
|
||||
ssaBlock *u = v;
|
||||
irBlock *ir_lt_eval(irLTState *lt, irBlock *v) {
|
||||
irBlock *u = v;
|
||||
for (;
|
||||
lt->ancestor[v->index] != NULL;
|
||||
v = lt->ancestor[v->index]) {
|
||||
@@ -347,16 +345,16 @@ ssaBlock *ssa_lt_eval(ssaLTState *lt, ssaBlock *v) {
|
||||
return u;
|
||||
}
|
||||
|
||||
typedef struct ssaDomPrePost {
|
||||
typedef struct irDomPrePost {
|
||||
i32 pre, post;
|
||||
} ssaDomPrePost;
|
||||
} irDomPrePost;
|
||||
|
||||
ssaDomPrePost ssa_opt_number_dom_tree(ssaBlock *v, i32 pre, i32 post) {
|
||||
ssaDomPrePost result = {pre, post};
|
||||
irDomPrePost ir_opt_number_dom_tree(irBlock *v, i32 pre, i32 post) {
|
||||
irDomPrePost result = {pre, post};
|
||||
|
||||
v->dom.pre = pre++;
|
||||
for_array(i, v->dom.children) {
|
||||
result = ssa_opt_number_dom_tree(v->dom.children.e[i], result.pre, result.post);
|
||||
result = ir_opt_number_dom_tree(v->dom.children.e[i], result.pre, result.post);
|
||||
}
|
||||
v->dom.post = post++;
|
||||
|
||||
@@ -366,35 +364,35 @@ ssaDomPrePost ssa_opt_number_dom_tree(ssaBlock *v, i32 pre, i32 post) {
|
||||
}
|
||||
|
||||
|
||||
// NOTE(bill): Requires `ssa_opt_blocks` to be called before this
|
||||
void ssa_opt_build_dom_tree(ssaProcedure *proc) {
|
||||
// NOTE(bill): Requires `ir_opt_blocks` to be called before this
|
||||
void ir_opt_build_dom_tree(irProcedure *proc) {
|
||||
// Based on this paper: http://jgaa.info/accepted/2006/GeorgiadisTarjanWerneck2006.10.1.pdf
|
||||
|
||||
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&proc->module->tmp_arena);
|
||||
|
||||
isize n = proc->blocks.count;
|
||||
ssaBlock **buf = gb_alloc_array(proc->module->tmp_allocator, ssaBlock *, 5*n);
|
||||
irBlock **buf = gb_alloc_array(proc->module->tmp_allocator, irBlock *, 5*n);
|
||||
|
||||
ssaLTState lt = {0};
|
||||
irLTState lt = {0};
|
||||
lt.count = n;
|
||||
lt.sdom = &buf[0*n];
|
||||
lt.parent = &buf[1*n];
|
||||
lt.ancestor = &buf[2*n];
|
||||
|
||||
ssaBlock **preorder = &buf[3*n];
|
||||
ssaBlock **buckets = &buf[4*n];
|
||||
ssaBlock *root = proc->blocks.e[0];
|
||||
irBlock **preorder = &buf[3*n];
|
||||
irBlock **buckets = &buf[4*n];
|
||||
irBlock *root = proc->blocks.e[0];
|
||||
|
||||
// Step 1 - number vertices
|
||||
i32 pre_num = ssa_lt_depth_first_search(<, root, 0, preorder);
|
||||
i32 pre_num = ir_lt_depth_first_search(<, root, 0, preorder);
|
||||
gb_memmove(buckets, preorder, n*gb_size_of(preorder[0]));
|
||||
|
||||
for (i32 i = n-1; i > 0; i--) {
|
||||
ssaBlock *w = preorder[i];
|
||||
irBlock *w = preorder[i];
|
||||
|
||||
// Step 3 - Implicitly define idom for nodes
|
||||
for (ssaBlock *v = buckets[i]; v != w; v = buckets[v->dom.pre]) {
|
||||
ssaBlock *u = ssa_lt_eval(<, v);
|
||||
for (irBlock *v = buckets[i]; v != w; v = buckets[v->dom.pre]) {
|
||||
irBlock *u = ir_lt_eval(<, v);
|
||||
if (lt.sdom[u->index]->dom.pre < i) {
|
||||
v->dom.idom = u;
|
||||
} else {
|
||||
@@ -405,14 +403,14 @@ void ssa_opt_build_dom_tree(ssaProcedure *proc) {
|
||||
// Step 2 - Compute all sdoms
|
||||
lt.sdom[w->index] = lt.parent[w->index];
|
||||
for_array(pred_index, w->preds) {
|
||||
ssaBlock *v = w->preds.e[pred_index];
|
||||
ssaBlock *u = ssa_lt_eval(<, v);
|
||||
irBlock *v = w->preds.e[pred_index];
|
||||
irBlock *u = ir_lt_eval(<, v);
|
||||
if (lt.sdom[u->index]->dom.pre < lt.sdom[w->index]->dom.pre) {
|
||||
lt.sdom[w->index] = lt.sdom[u->index];
|
||||
}
|
||||
}
|
||||
|
||||
ssa_lt_link(<, lt.parent[w->index], w);
|
||||
ir_lt_link(<, lt.parent[w->index], w);
|
||||
|
||||
if (lt.parent[w->index] == lt.sdom[w->index]) {
|
||||
w->dom.idom = lt.parent[w->index];
|
||||
@@ -423,13 +421,13 @@ void ssa_opt_build_dom_tree(ssaProcedure *proc) {
|
||||
}
|
||||
|
||||
// The rest of Step 3
|
||||
for (ssaBlock *v = buckets[0]; v != root; v = buckets[v->dom.pre]) {
|
||||
for (irBlock *v = buckets[0]; v != root; v = buckets[v->dom.pre]) {
|
||||
v->dom.idom = root;
|
||||
}
|
||||
|
||||
// Step 4 - Explicitly define idom for nodes (in preorder)
|
||||
for (isize i = 1; i < n; i++) {
|
||||
ssaBlock *w = preorder[i];
|
||||
irBlock *w = preorder[i];
|
||||
if (w == root) {
|
||||
w->dom.idom = NULL;
|
||||
} else {
|
||||
@@ -448,32 +446,32 @@ void ssa_opt_build_dom_tree(ssaProcedure *proc) {
|
||||
}
|
||||
}
|
||||
|
||||
ssa_opt_number_dom_tree(root, 0, 0);
|
||||
ir_opt_number_dom_tree(root, 0, 0);
|
||||
|
||||
gb_temp_arena_memory_end(tmp);
|
||||
}
|
||||
|
||||
void ssa_opt_mem2reg(ssaProcedure *proc) {
|
||||
// TODO(bill): ssa_opt_mem2reg
|
||||
void ir_opt_mem2reg(irProcedure *proc) {
|
||||
// TODO(bill): ir_opt_mem2reg
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ssa_opt_tree(ssaGen *s) {
|
||||
void ir_opt_tree(irGen *s) {
|
||||
s->opt_called = true;
|
||||
|
||||
for_array(member_index, s->module.procs) {
|
||||
ssaProcedure *proc = s->module.procs.e[member_index];
|
||||
irProcedure *proc = s->module.procs.e[member_index];
|
||||
if (proc->blocks.count == 0) { // Prototype/external procedure
|
||||
continue;
|
||||
}
|
||||
|
||||
ssa_opt_blocks(proc);
|
||||
ir_opt_blocks(proc);
|
||||
#if 1
|
||||
ssa_opt_build_referrers(proc);
|
||||
ssa_opt_build_dom_tree(proc);
|
||||
ir_opt_build_referrers(proc);
|
||||
ir_opt_build_dom_tree(proc);
|
||||
|
||||
// TODO(bill): ssa optimization
|
||||
// TODO(bill): ir optimization
|
||||
// [ ] cse (common-subexpression) elim
|
||||
// [ ] copy elim
|
||||
// [ ] dead code elim
|
||||
@@ -484,10 +482,10 @@ void ssa_opt_tree(ssaGen *s) {
|
||||
// [ ] lift/mem2reg
|
||||
// [ ] lift/mem2reg
|
||||
|
||||
ssa_opt_mem2reg(proc);
|
||||
ir_opt_mem2reg(proc);
|
||||
#endif
|
||||
|
||||
GB_ASSERT(proc->blocks.count > 0);
|
||||
ssa_number_proc_registers(proc);
|
||||
ir_number_proc_registers(proc);
|
||||
}
|
||||
}
|
||||
+1513
File diff suppressed because it is too large
Load Diff
+100
-83
@@ -1,22 +1,23 @@
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
#define VERSION_STRING "v0.0.3d"
|
||||
|
||||
#include "common.c"
|
||||
#include "timings.c"
|
||||
#include "unicode.c"
|
||||
#include "build.c"
|
||||
#include "tokenizer.c"
|
||||
#include "parser.c"
|
||||
// #include "printer.c"
|
||||
#include "checker/checker.c"
|
||||
#include "ssa.c"
|
||||
#include "ssa_opt.c"
|
||||
#include "ssa_print.c"
|
||||
#include "checker.c"
|
||||
// #include "ssa.c"
|
||||
#include "ir.c"
|
||||
#include "ir_opt.c"
|
||||
#include "ir_print.c"
|
||||
// #include "vm.c"
|
||||
|
||||
// NOTE(bill): `name` is used in debugging and profiling modes
|
||||
i32 win32_exec_command_line_app(char *name, char *fmt, ...) {
|
||||
i32 win32_exec_command_line_app(char *name, bool is_silent, char *fmt, ...) {
|
||||
STARTUPINFOW start_info = {gb_size_of(STARTUPINFOW)};
|
||||
PROCESS_INFORMATION pi = {0};
|
||||
char cmd_line[4096] = {0};
|
||||
@@ -59,49 +60,26 @@ i32 win32_exec_command_line_app(char *name, char *fmt, ...) {
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
typedef enum ArchKind {
|
||||
ArchKind_x64,
|
||||
ArchKind_x86,
|
||||
} ArchKind;
|
||||
|
||||
typedef struct ArchData {
|
||||
BaseTypeSizes sizes;
|
||||
String llc_flags;
|
||||
String link_flags;
|
||||
} ArchData;
|
||||
|
||||
ArchData make_arch_data(ArchKind kind) {
|
||||
ArchData data = {0};
|
||||
|
||||
switch (kind) {
|
||||
case ArchKind_x64:
|
||||
default:
|
||||
data.sizes.word_size = 8;
|
||||
data.sizes.max_align = 16;
|
||||
data.llc_flags = str_lit("-march=x86-64 ");
|
||||
data.link_flags = str_lit("/machine:x64 ");
|
||||
break;
|
||||
|
||||
case ArchKind_x86:
|
||||
data.sizes.word_size = 4;
|
||||
data.sizes.max_align = 8;
|
||||
data.llc_flags = str_lit("-march=x86 ");
|
||||
data.link_flags = str_lit("/machine:x86 ");
|
||||
break;
|
||||
void print_usage_line(i32 indent, char *fmt, ...) {
|
||||
while (indent --> 0) {
|
||||
gb_printf_err("\t");
|
||||
}
|
||||
|
||||
return data;
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
gb_printf_err_va(fmt, va);
|
||||
va_end(va);
|
||||
gb_printf_err("\n");
|
||||
}
|
||||
|
||||
void usage(char *argv0) {
|
||||
gb_printf_err("%s is a tool for managing Odin source code\n", argv0);
|
||||
gb_printf_err("Usage:");
|
||||
gb_printf_err("\n\t%s command [arguments]\n", argv0);
|
||||
gb_printf_err("Commands:");
|
||||
gb_printf_err("\n\tbuild compile .odin file");
|
||||
gb_printf_err("\n\trun compile and run .odin file");
|
||||
gb_printf_err("\n\tversion print Odin version");
|
||||
gb_printf_err("\n\n");
|
||||
print_usage_line(0, "%s is a tool for managing Odin source code", argv0);
|
||||
print_usage_line(0, "Usage:");
|
||||
print_usage_line(1, "%s command [arguments]", argv0);
|
||||
print_usage_line(0, "Commands:");
|
||||
print_usage_line(1, "build compile .odin file as executable");
|
||||
print_usage_line(1, "build_dll compile .odin file as dll");
|
||||
print_usage_line(1, "run compile and run .odin file");
|
||||
print_usage_line(1, "version print version");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
@@ -113,25 +91,42 @@ int main(int argc, char **argv) {
|
||||
Timings timings = {0};
|
||||
timings_init(&timings, str_lit("Total Time"), 128);
|
||||
// defer (timings_destroy(&timings));
|
||||
|
||||
#if 1
|
||||
init_string_buffer_memory();
|
||||
init_scratch_memory(gb_megabytes(10));
|
||||
init_global_error_collector();
|
||||
|
||||
String module_dir = get_module_dir();
|
||||
#if 1
|
||||
|
||||
init_universal_scope();
|
||||
BuildContext build_context = {0};
|
||||
init_build_context(&build_context);
|
||||
|
||||
init_universal_scope(&build_context);
|
||||
|
||||
char *init_filename = NULL;
|
||||
bool run_output = false;
|
||||
String arg1 = make_string_c(argv[1]);
|
||||
if (str_eq(arg1, str_lit("run"))) {
|
||||
run_output = true;
|
||||
if (argc != 3) {
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
init_filename = argv[2];
|
||||
run_output = true;
|
||||
} else if (str_eq(arg1, str_lit("build_dll"))) {
|
||||
if (argc != 3) {
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
init_filename = argv[2];
|
||||
build_context.is_dll = true;
|
||||
} else if (str_eq(arg1, str_lit("build"))) {
|
||||
if (argc != 3) {
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
init_filename = argv[2];
|
||||
} else if (str_eq(arg1, str_lit("version"))) {
|
||||
gb_printf("%s version %s", argv[0], VERSION_STRING);
|
||||
gb_printf("%s version %.*s", argv[0], LIT(build_context.ODIN_VERSION));
|
||||
return 0;
|
||||
} else {
|
||||
usage(argv[0]);
|
||||
@@ -157,38 +152,48 @@ int main(int argc, char **argv) {
|
||||
timings_start_section(&timings, str_lit("type check"));
|
||||
|
||||
Checker checker = {0};
|
||||
ArchData arch_data = make_arch_data(ArchKind_x64);
|
||||
|
||||
init_checker(&checker, &parser, arch_data.sizes);
|
||||
init_checker(&checker, &parser, &build_context);
|
||||
// defer (destroy_checker(&checker));
|
||||
|
||||
check_parsed_files(&checker);
|
||||
|
||||
|
||||
#endif
|
||||
#if 1
|
||||
|
||||
ssaGen ssa = {0};
|
||||
if (!ssa_gen_init(&ssa, &checker)) {
|
||||
#if 0
|
||||
if (global_error_collector.count != 0) {
|
||||
return 1;
|
||||
}
|
||||
// defer (ssa_gen_destroy(&ssa));
|
||||
|
||||
timings_start_section(&timings, str_lit("ssa gen"));
|
||||
ssa_gen_tree(&ssa);
|
||||
if (checker.parser->total_token_count < 2) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
timings_start_section(&timings, str_lit("ssa opt"));
|
||||
ssa_opt_tree(&ssa);
|
||||
ssa_generate(&checker.info, &build_context);
|
||||
#endif
|
||||
#if 1
|
||||
|
||||
timings_start_section(&timings, str_lit("ssa print"));
|
||||
ssa_print_llvm_ir(&ssa);
|
||||
irGen ir_gen = {0};
|
||||
if (!ir_gen_init(&ir_gen, &checker, &build_context)) {
|
||||
return 1;
|
||||
}
|
||||
// defer (ssa_gen_destroy(&ir_gen));
|
||||
|
||||
timings_start_section(&timings, str_lit("llvm ir gen"));
|
||||
ir_gen_tree(&ir_gen);
|
||||
|
||||
timings_start_section(&timings, str_lit("llvm ir opt tree"));
|
||||
ir_opt_tree(&ir_gen);
|
||||
|
||||
timings_start_section(&timings, str_lit("llvm ir print"));
|
||||
print_llvm_ir(&ir_gen);
|
||||
|
||||
// prof_print_all();
|
||||
|
||||
#if 1
|
||||
timings_start_section(&timings, str_lit("llvm-opt"));
|
||||
|
||||
char const *output_name = ssa.output_file.filename;
|
||||
char const *output_name = ir_gen.output_file.filename;
|
||||
isize base_name_len = gb_path_extension(output_name)-1 - output_name;
|
||||
String output = make_string(cast(u8 *)output_name, base_name_len);
|
||||
|
||||
@@ -197,8 +202,8 @@ int main(int argc, char **argv) {
|
||||
|
||||
i32 exit_code = 0;
|
||||
// For more passes arguments: http://llvm.org/docs/Passes.html
|
||||
exit_code = win32_exec_command_line_app("llvm-opt",
|
||||
"%.*sbin/opt %s -o %.*s.bc "
|
||||
exit_code = win32_exec_command_line_app("llvm-opt", false,
|
||||
"\"%.*sbin/opt\" \"%s\" -o \"%.*s\".bc "
|
||||
"-mem2reg "
|
||||
"-memcpyopt "
|
||||
"-die "
|
||||
@@ -206,7 +211,7 @@ int main(int argc, char **argv) {
|
||||
// "-dce "
|
||||
// "-S "
|
||||
"",
|
||||
LIT(module_dir),
|
||||
LIT(build_context.ODIN_ROOT),
|
||||
output_name, LIT(output));
|
||||
if (exit_code != 0) {
|
||||
return exit_code;
|
||||
@@ -215,39 +220,52 @@ int main(int argc, char **argv) {
|
||||
#if 1
|
||||
timings_start_section(&timings, str_lit("llvm-llc"));
|
||||
// For more arguments: http://llvm.org/docs/CommandGuide/llc.html
|
||||
exit_code = win32_exec_command_line_app("llvm-llc",
|
||||
"%.*sbin/llc %.*s.bc -filetype=obj -O%d "
|
||||
exit_code = win32_exec_command_line_app("llvm-llc", false,
|
||||
"\"%.*sbin/llc\" \"%.*s.bc\" -filetype=obj -O%d "
|
||||
"%.*s "
|
||||
// "-debug-pass=Arguments "
|
||||
"",
|
||||
LIT(module_dir),
|
||||
LIT(build_context.ODIN_ROOT),
|
||||
LIT(output),
|
||||
optimization_level,
|
||||
LIT(arch_data.llc_flags));
|
||||
LIT(build_context.llc_flags));
|
||||
if (exit_code != 0) {
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
timings_start_section(&timings, str_lit("msvc-link"));
|
||||
|
||||
gbString lib_str = gb_string_make(heap_allocator(), "Kernel32.lib");
|
||||
gbString lib_str = gb_string_make(heap_allocator(), "");
|
||||
// defer (gb_string_free(lib_str));
|
||||
char lib_str_buf[1024] = {0};
|
||||
for_array(i, parser.foreign_libraries) {
|
||||
String lib = parser.foreign_libraries.e[i];
|
||||
for_array(i, ir_gen.module.foreign_library_paths) {
|
||||
String lib = ir_gen.module.foreign_library_paths.e[i];
|
||||
// gb_printf_err("Linking lib: %.*s\n", LIT(lib));
|
||||
isize len = gb_snprintf(lib_str_buf, gb_size_of(lib_str_buf),
|
||||
" %.*s.lib", LIT(lib));
|
||||
" \"%.*s\"", LIT(lib));
|
||||
lib_str = gb_string_appendc(lib_str, lib_str_buf);
|
||||
}
|
||||
|
||||
exit_code = win32_exec_command_line_app("msvc-link",
|
||||
"link %.*s.obj -OUT:%.*s.exe %s "
|
||||
char *output_ext = "exe";
|
||||
char *link_settings = "";
|
||||
if (build_context.is_dll) {
|
||||
output_ext = "dll";
|
||||
link_settings = "/DLL";
|
||||
} else {
|
||||
link_settings = "/ENTRY:mainCRTStartup";
|
||||
}
|
||||
|
||||
exit_code = win32_exec_command_line_app("msvc-link", true,
|
||||
"link \"%.*s\".obj -OUT:\"%.*s.%s\" %s "
|
||||
"/defaultlib:libcmt "
|
||||
"/nologo /incremental:no /opt:ref /subsystem:console "
|
||||
"/nologo /incremental:no /opt:ref /subsystem:CONSOLE "
|
||||
" %.*s "
|
||||
" %s "
|
||||
"",
|
||||
LIT(output), LIT(output),
|
||||
lib_str, LIT(arch_data.link_flags));
|
||||
LIT(output), LIT(output), output_ext,
|
||||
lib_str, LIT(build_context.link_flags),
|
||||
link_settings
|
||||
);
|
||||
if (exit_code != 0) {
|
||||
return exit_code;
|
||||
}
|
||||
@@ -255,8 +273,7 @@ int main(int argc, char **argv) {
|
||||
// timings_print_all(&timings);
|
||||
|
||||
if (run_output) {
|
||||
win32_exec_command_line_app("odin run",
|
||||
"%.*s.exe", cast(int)base_name_len, output_name);
|
||||
win32_exec_command_line_app("odin run", false, "%.*s.exe", cast(int)base_name_len, output_name);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -320,6 +320,7 @@ void _J2(MAP_PROC,multi_insert)(MAP_NAME *h, HashKey key, MAP_TYPE value) {
|
||||
if (h->hashes.count == 0) {
|
||||
_J2(MAP_PROC,grow)(h);
|
||||
}
|
||||
// Make
|
||||
fr = _J2(MAP_PROC,_find)(h, key);
|
||||
i = _J2(MAP_PROC,_add_entry)(h, key);
|
||||
if (fr.entry_prev < 0) {
|
||||
@@ -329,6 +330,7 @@ void _J2(MAP_PROC,multi_insert)(MAP_NAME *h, HashKey key, MAP_TYPE value) {
|
||||
}
|
||||
h->entries.e[i].next = fr.entry_index;
|
||||
h->entries.e[i].value = value;
|
||||
// Grow if needed
|
||||
if (_J2(MAP_PROC,_full)(h)) {
|
||||
_J2(MAP_PROC,grow)(h);
|
||||
}
|
||||
|
||||
+1599
-986
File diff suppressed because it is too large
Load Diff
-1439
File diff suppressed because it is too large
Load Diff
+1
-14
@@ -46,7 +46,7 @@ gb_inline String make_string_c(char *text) {
|
||||
return make_string(cast(u8 *)cast(void *)text, gb_strlen(text));
|
||||
}
|
||||
|
||||
#define str_lit(c_str) make_string(cast(u8 *)c_str, gb_size_of(c_str)-1)
|
||||
#define str_lit(c_str) (String){cast(u8 *)c_str, gb_size_of(c_str)-1}
|
||||
|
||||
|
||||
|
||||
@@ -108,19 +108,6 @@ GB_COMPARE_PROC(string_cmp_proc) {
|
||||
return string_compare(x, y);
|
||||
}
|
||||
|
||||
|
||||
// gb_inline bool operator ==(String a, String b) { return are_strings_equal(a, b) != 0; }
|
||||
// gb_inline bool operator !=(String a, String b) { return !operator==(a, b); }
|
||||
// gb_inline bool operator < (String a, String b) { return string_compare(a, b) < 0; }
|
||||
// gb_inline bool operator > (String a, String b) { return string_compare(a, b) > 0; }
|
||||
// gb_inline bool operator <=(String a, String b) { return string_compare(a, b) <= 0; }
|
||||
// gb_inline bool operator >=(String a, String b) { return string_compare(a, b) >= 0; }
|
||||
|
||||
// template <size_t N> gb_inline bool operator ==(String a, char const (&b)[N]) { return a == make_string(cast(u8 *)b, N-1); }
|
||||
// template <size_t N> gb_inline bool operator !=(String a, char const (&b)[N]) { return a != make_string(cast(u8 *)b, N-1); }
|
||||
// template <size_t N> gb_inline bool operator ==(char const (&a)[N], String b) { return make_string(cast(u8 *)a, N-1) == b; }
|
||||
// template <size_t N> gb_inline bool operator !=(char const (&a)[N], String b) { return make_string(cast(u8 *)a, N-1) != b; }
|
||||
|
||||
gb_inline bool str_eq(String a, String b) { return a.len == b.len ? gb_memcompare(a.text, b.text, a.len) == 0 : false; }
|
||||
gb_inline bool str_ne(String a, String b) { return !str_eq(a, b); }
|
||||
gb_inline bool str_lt(String a, String b) { return string_compare(a, b) < 0; }
|
||||
|
||||
+276
-142
@@ -1,89 +1,88 @@
|
||||
#define TOKEN_KINDS \
|
||||
TOKEN_KIND(Token_Invalid, "Invalid"), \
|
||||
TOKEN_KIND(Token_EOF, "EOF"), \
|
||||
TOKEN_KIND(Token_EOF, "EOF"), \
|
||||
TOKEN_KIND(Token_Comment, "Comment"), \
|
||||
\
|
||||
TOKEN_KIND(Token__LiteralBegin, "_LiteralBegin"), \
|
||||
TOKEN_KIND(Token_Identifier, "Identifier"), \
|
||||
TOKEN_KIND(Token_Integer, "Integer"), \
|
||||
TOKEN_KIND(Token_Float, "Float"), \
|
||||
TOKEN_KIND(Token_Rune, "Rune"), \
|
||||
TOKEN_KIND(Token_String, "String"), \
|
||||
TOKEN_KIND(Token__LiteralEnd, "_LiteralEnd"), \
|
||||
TOKEN_KIND(Token_Ident, "identifier"), \
|
||||
TOKEN_KIND(Token_Integer, "integer"), \
|
||||
TOKEN_KIND(Token_Float, "float"), \
|
||||
TOKEN_KIND(Token_Rune, "rune"), \
|
||||
TOKEN_KIND(Token_String, "string"), \
|
||||
TOKEN_KIND(Token__LiteralEnd, "_LiteralEnd"), \
|
||||
\
|
||||
TOKEN_KIND(Token__OperatorBegin, "_OperatorBegin"), \
|
||||
TOKEN_KIND(Token_Eq, "="), \
|
||||
TOKEN_KIND(Token_Not, "!"), \
|
||||
TOKEN_KIND(Token_Hash, "#"), \
|
||||
TOKEN_KIND(Token_At, "@"), \
|
||||
TOKEN_KIND(Token_Eq, "="), \
|
||||
TOKEN_KIND(Token_Not, "!"), \
|
||||
TOKEN_KIND(Token_Hash, "#"), \
|
||||
TOKEN_KIND(Token_At, "@"), \
|
||||
TOKEN_KIND(Token_Pointer, "^"), \
|
||||
TOKEN_KIND(Token_Maybe, "?"), \
|
||||
TOKEN_KIND(Token_Add, "+"), \
|
||||
TOKEN_KIND(Token_Sub, "-"), \
|
||||
TOKEN_KIND(Token_Mul, "*"), \
|
||||
TOKEN_KIND(Token_Quo, "/"), \
|
||||
TOKEN_KIND(Token_Mod, "%"), \
|
||||
TOKEN_KIND(Token_And, "&"), \
|
||||
TOKEN_KIND(Token_Or, "|"), \
|
||||
TOKEN_KIND(Token_Xor, "~"), \
|
||||
TOKEN_KIND(Token_AndNot, "&~"), \
|
||||
TOKEN_KIND(Token_Shl, "<<"), \
|
||||
TOKEN_KIND(Token_Shr, ">>"), \
|
||||
TOKEN_KIND(Token_Maybe, "?"), \
|
||||
TOKEN_KIND(Token_Add, "+"), \
|
||||
TOKEN_KIND(Token_Sub, "-"), \
|
||||
TOKEN_KIND(Token_Mul, "*"), \
|
||||
TOKEN_KIND(Token_Quo, "/"), \
|
||||
TOKEN_KIND(Token_Mod, "%"), \
|
||||
TOKEN_KIND(Token_And, "&"), \
|
||||
TOKEN_KIND(Token_Or, "|"), \
|
||||
TOKEN_KIND(Token_Xor, "~"), \
|
||||
TOKEN_KIND(Token_AndNot, "&~"), \
|
||||
TOKEN_KIND(Token_Shl, "<<"), \
|
||||
TOKEN_KIND(Token_Shr, ">>"), \
|
||||
\
|
||||
TOKEN_KIND(Token_as, "as"), \
|
||||
TOKEN_KIND(Token_transmute, "transmute"), \
|
||||
TOKEN_KIND(Token_down_cast, "down_cast"), \
|
||||
TOKEN_KIND(Token_union_cast, "union_cast"), \
|
||||
/*TOKEN_KIND(Token_as, "as"), */\
|
||||
/*TOKEN_KIND(Token_transmute, "transmute"), */\
|
||||
/*TOKEN_KIND(Token_down_cast, "down_cast"), */\
|
||||
/*TOKEN_KIND(Token_union_cast, "union_cast"), */\
|
||||
\
|
||||
TOKEN_KIND(Token_CmpAnd, "&&"), \
|
||||
TOKEN_KIND(Token_CmpOr, "||"), \
|
||||
TOKEN_KIND(Token_CmpOr, "||"), \
|
||||
\
|
||||
TOKEN_KIND(Token__AssignOpBegin, "_AssignOpBegin"), \
|
||||
TOKEN_KIND(Token_AddEq, "+="), \
|
||||
TOKEN_KIND(Token_SubEq, "-="), \
|
||||
TOKEN_KIND(Token_MulEq, "*="), \
|
||||
TOKEN_KIND(Token_QuoEq, "/="), \
|
||||
TOKEN_KIND(Token_ModEq, "%="), \
|
||||
TOKEN_KIND(Token_AndEq, "&="), \
|
||||
TOKEN_KIND(Token_OrEq, "|="), \
|
||||
TOKEN_KIND(Token_XorEq, "~="), \
|
||||
TOKEN_KIND(Token_AndNotEq, "&~="), \
|
||||
TOKEN_KIND(Token_ShlEq, "<<="), \
|
||||
TOKEN_KIND(Token_ShrEq, ">>="), \
|
||||
TOKEN_KIND(Token_CmpAndEq, "&&="), \
|
||||
TOKEN_KIND(Token_CmpOrEq, "||="), \
|
||||
TOKEN_KIND(Token__AssignOpEnd, "_AssignOpEnd"), \
|
||||
TOKEN_KIND(Token_Increment, "++"), \
|
||||
TOKEN_KIND(Token_Decrement, "--"), \
|
||||
TOKEN_KIND(Token_AddEq, "+="), \
|
||||
TOKEN_KIND(Token_SubEq, "-="), \
|
||||
TOKEN_KIND(Token_MulEq, "*="), \
|
||||
TOKEN_KIND(Token_QuoEq, "/="), \
|
||||
TOKEN_KIND(Token_ModEq, "%="), \
|
||||
TOKEN_KIND(Token_AndEq, "&="), \
|
||||
TOKEN_KIND(Token_OrEq, "|="), \
|
||||
TOKEN_KIND(Token_XorEq, "~="), \
|
||||
TOKEN_KIND(Token_AndNotEq, "&~="), \
|
||||
TOKEN_KIND(Token_ShlEq, "<<="), \
|
||||
TOKEN_KIND(Token_ShrEq, ">>="), \
|
||||
TOKEN_KIND(Token_CmpAndEq, "&&="), \
|
||||
TOKEN_KIND(Token_CmpOrEq, "||="), \
|
||||
TOKEN_KIND(Token__AssignOpEnd, "_AssignOpEnd"), \
|
||||
TOKEN_KIND(Token_ArrowRight, "->"), \
|
||||
TOKEN_KIND(Token_ArrowLeft, "<-"), \
|
||||
TOKEN_KIND(Token_ArrowLeft, "<-"), \
|
||||
\
|
||||
TOKEN_KIND(Token__ComparisonBegin, "_ComparisonBegin"), \
|
||||
TOKEN_KIND(Token_CmpEq, "=="), \
|
||||
TOKEN_KIND(Token_NotEq, "!="), \
|
||||
TOKEN_KIND(Token_Lt, "<"), \
|
||||
TOKEN_KIND(Token_Gt, ">"), \
|
||||
TOKEN_KIND(Token_LtEq, "<="), \
|
||||
TOKEN_KIND(Token_GtEq, ">="), \
|
||||
TOKEN_KIND(Token_Lt, "<"), \
|
||||
TOKEN_KIND(Token_Gt, ">"), \
|
||||
TOKEN_KIND(Token_LtEq, "<="), \
|
||||
TOKEN_KIND(Token_GtEq, ">="), \
|
||||
TOKEN_KIND(Token__ComparisonEnd, "_ComparisonEnd"), \
|
||||
\
|
||||
TOKEN_KIND(Token_OpenParen, "("), \
|
||||
TOKEN_KIND(Token_CloseParen, ")"), \
|
||||
TOKEN_KIND(Token_OpenBracket, "["), \
|
||||
TOKEN_KIND(Token_CloseBracket, "]"), \
|
||||
TOKEN_KIND(Token_OpenBrace, "{"), \
|
||||
TOKEN_KIND(Token_CloseBrace, "}"), \
|
||||
TOKEN_KIND(Token_Colon, ":"), \
|
||||
TOKEN_KIND(Token_Semicolon, ";"), \
|
||||
TOKEN_KIND(Token_Period, "."), \
|
||||
TOKEN_KIND(Token_Comma, ","), \
|
||||
TOKEN_KIND(Token_Ellipsis, ".."), \
|
||||
TOKEN_KIND(Token_RangeExclusive, "..<"), \
|
||||
TOKEN_KIND(Token_OpenParen, "("), \
|
||||
TOKEN_KIND(Token_CloseParen, ")"), \
|
||||
TOKEN_KIND(Token_OpenBracket, "["), \
|
||||
TOKEN_KIND(Token_CloseBracket, "]"), \
|
||||
TOKEN_KIND(Token_OpenBrace, "{"), \
|
||||
TOKEN_KIND(Token_CloseBrace, "}"), \
|
||||
TOKEN_KIND(Token_Colon, ":"), \
|
||||
TOKEN_KIND(Token_Semicolon, ";"), \
|
||||
TOKEN_KIND(Token_Period, "."), \
|
||||
TOKEN_KIND(Token_Comma, ","), \
|
||||
TOKEN_KIND(Token_Ellipsis, "..."), \
|
||||
TOKEN_KIND(Token_HalfOpenRange, "..<"), \
|
||||
TOKEN_KIND(Token__OperatorEnd, "_OperatorEnd"), \
|
||||
\
|
||||
TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
|
||||
TOKEN_KIND(Token_type, "type"), \
|
||||
TOKEN_KIND(Token_proc, "proc"), \
|
||||
TOKEN_KIND(Token_macro, "macro"), \
|
||||
TOKEN_KIND(Token_match, "match"), \
|
||||
TOKEN_KIND(Token_break, "break"), \
|
||||
TOKEN_KIND(Token_continue, "continue"), \
|
||||
@@ -94,19 +93,28 @@ TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
|
||||
TOKEN_KIND(Token_if, "if"), \
|
||||
TOKEN_KIND(Token_else, "else"), \
|
||||
TOKEN_KIND(Token_for, "for"), \
|
||||
TOKEN_KIND(Token_in, "in"), \
|
||||
TOKEN_KIND(Token_when, "when"), \
|
||||
TOKEN_KIND(Token_range, "range"), \
|
||||
TOKEN_KIND(Token_defer, "defer"), \
|
||||
TOKEN_KIND(Token_return, "return"), \
|
||||
TOKEN_KIND(Token_give, "give"), \
|
||||
TOKEN_KIND(Token_struct, "struct"), \
|
||||
TOKEN_KIND(Token_union, "union"), \
|
||||
TOKEN_KIND(Token_raw_union, "raw_union"), \
|
||||
TOKEN_KIND(Token_enum, "enum"), \
|
||||
TOKEN_KIND(Token_vector, "vector"), \
|
||||
TOKEN_KIND(Token_using, "using"), \
|
||||
TOKEN_KIND(Token_no_alias, "no_alias"), \
|
||||
TOKEN_KIND(Token_immutable, "immutable"), \
|
||||
TOKEN_KIND(Token_thread_local, "thread_local"), \
|
||||
TOKEN_KIND(Token_asm, "asm"), \
|
||||
TOKEN_KIND(Token_volatile, "volatile"), \
|
||||
TOKEN_KIND(Token_atomic, "atomic"), \
|
||||
TOKEN_KIND(Token_push_allocator, "push_allocator"), \
|
||||
TOKEN_KIND(Token_push_context, "push_context"), \
|
||||
TOKEN_KIND(Token_cast, "cast"), \
|
||||
TOKEN_KIND(Token_transmute, "transmute"), \
|
||||
TOKEN_KIND(Token_down_cast, "down_cast"), \
|
||||
TOKEN_KIND(Token_union_cast, "union_cast"), \
|
||||
TOKEN_KIND(Token__KeywordEnd, "_KeywordEnd"), \
|
||||
TOKEN_KIND(Token_Count, "")
|
||||
|
||||
@@ -141,11 +149,10 @@ i32 token_pos_cmp(TokenPos a, TokenPos b) {
|
||||
return (a.line < b.line) ? -1 : +1;
|
||||
}
|
||||
|
||||
bool token_pos_are_equal(TokenPos a, TokenPos b) {
|
||||
bool token_pos_eq(TokenPos a, TokenPos b) {
|
||||
return token_pos_cmp(a, b) == 0;
|
||||
}
|
||||
|
||||
// NOTE(bill): Text is UTF-8, thus why u8 and not char
|
||||
typedef struct Token {
|
||||
TokenKind kind;
|
||||
String string;
|
||||
@@ -153,10 +160,10 @@ typedef struct Token {
|
||||
} Token;
|
||||
|
||||
Token empty_token = {Token_Invalid};
|
||||
Token blank_token = {Token_Identifier, {cast(u8 *)"_", 1}};
|
||||
Token blank_token = {Token_Ident, {cast(u8 *)"_", 1}};
|
||||
|
||||
Token make_token_ident(String s) {
|
||||
Token t = {Token_Identifier, s};
|
||||
Token t = {Token_Ident, s};
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -174,68 +181,75 @@ void init_global_error_collector(void) {
|
||||
gb_mutex_init(&global_error_collector.mutex);
|
||||
}
|
||||
|
||||
|
||||
void warning(Token token, char *fmt, ...) {
|
||||
void warning_va(Token token, char *fmt, va_list va) {
|
||||
gb_mutex_lock(&global_error_collector.mutex);
|
||||
|
||||
global_error_collector.warning_count++;
|
||||
// NOTE(bill): Duplicate error, skip it
|
||||
if (!token_pos_are_equal(global_error_collector.prev, token.pos)) {
|
||||
va_list va;
|
||||
|
||||
if (!token_pos_eq(global_error_collector.prev, token.pos)) {
|
||||
global_error_collector.prev = token.pos;
|
||||
|
||||
va_start(va, fmt);
|
||||
gb_printf_err("%.*s(%td:%td) Warning: %s\n",
|
||||
LIT(token.pos.file), token.pos.line, token.pos.column,
|
||||
gb_bprintf_va(fmt, va));
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
gb_mutex_unlock(&global_error_collector.mutex);
|
||||
}
|
||||
|
||||
void error(Token token, char *fmt, ...) {
|
||||
void error_va(Token token, char *fmt, va_list va) {
|
||||
gb_mutex_lock(&global_error_collector.mutex);
|
||||
|
||||
global_error_collector.count++;
|
||||
// NOTE(bill): Duplicate error, skip it
|
||||
if (!token_pos_are_equal(global_error_collector.prev, token.pos)) {
|
||||
va_list va;
|
||||
|
||||
if (!token_pos_eq(global_error_collector.prev, token.pos)) {
|
||||
global_error_collector.prev = token.pos;
|
||||
|
||||
va_start(va, fmt);
|
||||
gb_printf_err("%.*s(%td:%td) %s\n",
|
||||
LIT(token.pos.file), token.pos.line, token.pos.column,
|
||||
gb_bprintf_va(fmt, va));
|
||||
va_end(va);
|
||||
} else if (token.pos.line == 0) {
|
||||
gb_printf_err("Error: %s\n", gb_bprintf_va(fmt, va));
|
||||
}
|
||||
|
||||
gb_mutex_unlock(&global_error_collector.mutex);
|
||||
}
|
||||
|
||||
void syntax_error(Token token, char *fmt, ...) {
|
||||
void syntax_error_va(Token token, char *fmt, va_list va) {
|
||||
gb_mutex_lock(&global_error_collector.mutex);
|
||||
|
||||
global_error_collector.count++;
|
||||
// NOTE(bill): Duplicate error, skip it
|
||||
if (!token_pos_are_equal(global_error_collector.prev, token.pos)) {
|
||||
va_list va;
|
||||
|
||||
if (!token_pos_eq(global_error_collector.prev, token.pos)) {
|
||||
global_error_collector.prev = token.pos;
|
||||
|
||||
va_start(va, fmt);
|
||||
gb_printf_err("%.*s(%td:%td) Syntax Error: %s\n",
|
||||
LIT(token.pos.file), token.pos.line, token.pos.column,
|
||||
gb_bprintf_va(fmt, va));
|
||||
va_end(va);
|
||||
} else if (token.pos.line == 0) {
|
||||
gb_printf_err("Error: %s\n", gb_bprintf_va(fmt, va));
|
||||
}
|
||||
|
||||
gb_mutex_unlock(&global_error_collector.mutex);
|
||||
}
|
||||
|
||||
|
||||
void warning(Token token, char *fmt, ...) {
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
warning_va(token, fmt, va);
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
void error(Token token, char *fmt, ...) {
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
error_va(token, fmt, va);
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
void syntax_error(Token token, char *fmt, ...) {
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
syntax_error_va(token, fmt, va);
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
|
||||
void compiler_error(char *fmt, ...) {
|
||||
va_list va;
|
||||
|
||||
@@ -281,6 +295,14 @@ typedef enum TokenizerInitError {
|
||||
} TokenizerInitError;
|
||||
|
||||
|
||||
typedef struct TokenizerState {
|
||||
Rune curr_rune; // current character
|
||||
u8 * curr; // character pos
|
||||
u8 * read_curr; // pos from start
|
||||
u8 * line; // current line pos
|
||||
isize line_count;
|
||||
} TokenizerState;
|
||||
|
||||
typedef struct Tokenizer {
|
||||
String fullpath;
|
||||
u8 *start;
|
||||
@@ -297,11 +319,31 @@ typedef struct Tokenizer {
|
||||
} Tokenizer;
|
||||
|
||||
|
||||
TokenizerState save_tokenizer_state(Tokenizer *t) {
|
||||
TokenizerState state = {0};
|
||||
state.curr_rune = t->curr_rune;
|
||||
state.curr = t->curr;
|
||||
state.read_curr = t->read_curr;
|
||||
state.line = t->line;
|
||||
state.line_count = t->line_count;
|
||||
return state;
|
||||
}
|
||||
|
||||
void restore_tokenizer_state(Tokenizer *t, TokenizerState *state) {
|
||||
t->curr_rune = state->curr_rune;
|
||||
t->curr = state->curr;
|
||||
t->read_curr = state->read_curr;
|
||||
t->line = state->line;
|
||||
t->line_count = state->line_count;
|
||||
}
|
||||
|
||||
|
||||
void tokenizer_err(Tokenizer *t, char *msg, ...) {
|
||||
va_list va;
|
||||
isize column = t->read_curr - t->line+1;
|
||||
if (column < 1)
|
||||
if (column < 1) {
|
||||
column = 1;
|
||||
}
|
||||
|
||||
gb_printf_err("%.*s(%td:%td) Syntax error: ", LIT(t->fullpath), t->line_count, column);
|
||||
|
||||
@@ -401,7 +443,10 @@ gb_inline void destroy_tokenizer(Tokenizer *t) {
|
||||
}
|
||||
|
||||
void tokenizer_skip_whitespace(Tokenizer *t) {
|
||||
while (rune_is_whitespace(t->curr_rune)) {
|
||||
while (t->curr_rune == ' ' ||
|
||||
t->curr_rune == '\t' ||
|
||||
t->curr_rune == '\n' ||
|
||||
t->curr_rune == '\r') {
|
||||
advance_to_next_rune(t);
|
||||
}
|
||||
}
|
||||
@@ -417,12 +462,15 @@ gb_inline i32 digit_value(Rune r) {
|
||||
return 16; // NOTE(bill): Larger than highest possible
|
||||
}
|
||||
|
||||
gb_inline void scan_mantissa(Tokenizer *t, i32 base) {
|
||||
// TODO(bill): Allow for underscores in numbers as a number separator
|
||||
// TODO(bill): Is this a good idea?
|
||||
// while (digit_value(t->curr_rune) < base || t->curr_rune == '_')
|
||||
while (digit_value(t->curr_rune) < base) {
|
||||
advance_to_next_rune(t);
|
||||
gb_inline void scan_mantissa(Tokenizer *t, i32 base, bool allow_underscore) {
|
||||
if (allow_underscore) {
|
||||
while (digit_value(t->curr_rune) < base || t->curr_rune == '_') {
|
||||
advance_to_next_rune(t);
|
||||
}
|
||||
} else {
|
||||
while (digit_value(t->curr_rune) < base) {
|
||||
advance_to_next_rune(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -437,7 +485,7 @@ Token scan_number_to_token(Tokenizer *t, bool seen_decimal_point) {
|
||||
|
||||
if (seen_decimal_point) {
|
||||
token.kind = Token_Float;
|
||||
scan_mantissa(t, 10);
|
||||
scan_mantissa(t, 10, true);
|
||||
goto exponent;
|
||||
}
|
||||
|
||||
@@ -446,27 +494,31 @@ Token scan_number_to_token(Tokenizer *t, bool seen_decimal_point) {
|
||||
advance_to_next_rune(t);
|
||||
if (t->curr_rune == 'b') { // Binary
|
||||
advance_to_next_rune(t);
|
||||
scan_mantissa(t, 2);
|
||||
if (t->curr - prev <= 2)
|
||||
scan_mantissa(t, 2, true);
|
||||
if (t->curr - prev <= 2) {
|
||||
token.kind = Token_Invalid;
|
||||
}
|
||||
} else if (t->curr_rune == 'o') { // Octal
|
||||
advance_to_next_rune(t);
|
||||
scan_mantissa(t, 8);
|
||||
if (t->curr - prev <= 2)
|
||||
scan_mantissa(t, 8, true);
|
||||
if (t->curr - prev <= 2) {
|
||||
token.kind = Token_Invalid;
|
||||
}
|
||||
} else if (t->curr_rune == 'd') { // Decimal
|
||||
advance_to_next_rune(t);
|
||||
scan_mantissa(t, 10);
|
||||
if (t->curr - prev <= 2)
|
||||
scan_mantissa(t, 10, true);
|
||||
if (t->curr - prev <= 2) {
|
||||
token.kind = Token_Invalid;
|
||||
}
|
||||
} else if (t->curr_rune == 'x') { // Hexadecimal
|
||||
advance_to_next_rune(t);
|
||||
scan_mantissa(t, 16);
|
||||
if (t->curr - prev <= 2)
|
||||
scan_mantissa(t, 16, true);
|
||||
if (t->curr - prev <= 2) {
|
||||
token.kind = Token_Invalid;
|
||||
}
|
||||
} else {
|
||||
seen_decimal_point = false;
|
||||
scan_mantissa(t, 10);
|
||||
scan_mantissa(t, 10, true);
|
||||
|
||||
if (t->curr_rune == '.' || t->curr_rune == 'e' || t->curr_rune == 'E') {
|
||||
seen_decimal_point = true;
|
||||
@@ -478,13 +530,20 @@ Token scan_number_to_token(Tokenizer *t, bool seen_decimal_point) {
|
||||
return token;
|
||||
}
|
||||
|
||||
scan_mantissa(t, 10);
|
||||
scan_mantissa(t, 10, true);
|
||||
|
||||
fraction:
|
||||
if (t->curr_rune == '.') {
|
||||
token.kind = Token_Float;
|
||||
// HACK(bill): This may be inefficient
|
||||
TokenizerState state = save_tokenizer_state(t);
|
||||
advance_to_next_rune(t);
|
||||
scan_mantissa(t, 10);
|
||||
if (digit_value(t->curr_rune) >= 10) {
|
||||
// TODO(bill): Clean up this shit
|
||||
restore_tokenizer_state(t, &state);
|
||||
goto end;
|
||||
}
|
||||
token.kind = Token_Float;
|
||||
scan_mantissa(t, 10, true);
|
||||
}
|
||||
|
||||
exponent:
|
||||
@@ -494,9 +553,10 @@ exponent:
|
||||
if (t->curr_rune == '-' || t->curr_rune == '+') {
|
||||
advance_to_next_rune(t);
|
||||
}
|
||||
scan_mantissa(t, 10);
|
||||
scan_mantissa(t, 10, false);
|
||||
}
|
||||
|
||||
end:
|
||||
token.string.len = t->curr - token.string.text;
|
||||
return token;
|
||||
}
|
||||
@@ -605,19 +665,63 @@ gb_inline TokenKind token_kind_dub_eq(Tokenizer *t, Rune sing_rune, TokenKind si
|
||||
return sing;
|
||||
}
|
||||
|
||||
Token tokenizer_get_token(Tokenizer *t) {
|
||||
Token token = {0};
|
||||
Rune curr_rune;
|
||||
void tokenizer__fle_update(Tokenizer *t) {
|
||||
t->curr_rune = '/';
|
||||
t->curr = t->curr-1;
|
||||
t->read_curr = t->curr+1;
|
||||
advance_to_next_rune(t);
|
||||
}
|
||||
|
||||
// NOTE(bill): needed if comment is straight after a "semicolon"
|
||||
bool tokenizer_find_line_end(Tokenizer *t) {
|
||||
while (t->curr_rune == '/' || t->curr_rune == '*') {
|
||||
if (t->curr_rune == '/') {
|
||||
tokenizer__fle_update(t);
|
||||
return true;
|
||||
}
|
||||
|
||||
advance_to_next_rune(t);
|
||||
while (t->curr_rune >= 0) {
|
||||
Rune r = t->curr_rune;
|
||||
if (r == '\n') {
|
||||
tokenizer__fle_update(t);
|
||||
return true;
|
||||
}
|
||||
advance_to_next_rune(t);
|
||||
if (r == '*' && t->curr_rune == '/') {
|
||||
advance_to_next_rune(t);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
tokenizer_skip_whitespace(t);
|
||||
if (t->curr_rune < 0 || t->curr_rune == '\n') {
|
||||
tokenizer__fle_update(t);
|
||||
return true;
|
||||
}
|
||||
if (t->curr_rune != '/') {
|
||||
tokenizer__fle_update(t);
|
||||
return false;
|
||||
}
|
||||
advance_to_next_rune(t);
|
||||
}
|
||||
|
||||
tokenizer__fle_update(t);
|
||||
return false;
|
||||
}
|
||||
|
||||
Token tokenizer_get_token(Tokenizer *t) {
|
||||
tokenizer_skip_whitespace(t);
|
||||
|
||||
Token token = {0};
|
||||
token.string = make_string(t->curr, 1);
|
||||
token.pos.file = t->fullpath;
|
||||
token.pos.line = t->line_count;
|
||||
token.pos.column = t->curr - t->line + 1;
|
||||
|
||||
curr_rune = t->curr_rune;
|
||||
Rune curr_rune = t->curr_rune;
|
||||
if (rune_is_letter(curr_rune)) {
|
||||
token.kind = Token_Identifier;
|
||||
token.kind = Token_Ident;
|
||||
while (rune_is_letter(t->curr_rune) || rune_is_digit(t->curr_rune)) {
|
||||
advance_to_next_rune(t);
|
||||
}
|
||||
@@ -626,7 +730,7 @@ Token tokenizer_get_token(Tokenizer *t) {
|
||||
|
||||
// NOTE(bill): All keywords are > 1
|
||||
if (token.string.len > 1) {
|
||||
if (str_eq(token.string, token_strings[Token_as])) {
|
||||
/* if (str_eq(token.string, token_strings[Token_as])) {
|
||||
token.kind = Token_as;
|
||||
} else if (str_eq(token.string, token_strings[Token_transmute])) {
|
||||
token.kind = Token_transmute;
|
||||
@@ -634,7 +738,7 @@ Token tokenizer_get_token(Tokenizer *t) {
|
||||
token.kind = Token_down_cast;
|
||||
} else if (str_eq(token.string, token_strings[Token_union_cast])) {
|
||||
token.kind = Token_union_cast;
|
||||
} else {
|
||||
} else */{
|
||||
for (i32 k = Token__KeywordBegin+1; k < Token__KeywordEnd; k++) {
|
||||
if (str_eq(token.string, token_strings[k])) {
|
||||
token.kind = cast(TokenKind)k;
|
||||
@@ -741,42 +845,72 @@ Token tokenizer_get_token(Tokenizer *t) {
|
||||
|
||||
case '.':
|
||||
token.kind = Token_Period; // Default
|
||||
if (gb_is_between(t->curr_rune, '0', '9')) { // Might be a number
|
||||
token = scan_number_to_token(t, true);
|
||||
} else if (t->curr_rune == '.') { // Could be an ellipsis
|
||||
if (t->curr_rune == '.') { // Could be an ellipsis
|
||||
advance_to_next_rune(t);
|
||||
token.kind = Token_Ellipsis;
|
||||
if (t->curr_rune == '<') {
|
||||
advance_to_next_rune(t);
|
||||
token.kind = Token_RangeExclusive;
|
||||
token.kind = Token_HalfOpenRange;
|
||||
} else if (t->curr_rune == '.') {
|
||||
advance_to_next_rune(t);
|
||||
token.kind = Token_Ellipsis;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case '#': token.kind = Token_Hash; break;
|
||||
case '@': token.kind = Token_At; break;
|
||||
case '^': token.kind = Token_Pointer; break;
|
||||
case '?': token.kind = Token_Maybe; break;
|
||||
case ';': token.kind = Token_Semicolon; break;
|
||||
case ',': token.kind = Token_Comma; break;
|
||||
case '(': token.kind = Token_OpenParen; break;
|
||||
case ')': token.kind = Token_CloseParen; break;
|
||||
case '[': token.kind = Token_OpenBracket; break;
|
||||
case ']': token.kind = Token_CloseBracket; break;
|
||||
case '{': token.kind = Token_OpenBrace; break;
|
||||
case '}': token.kind = Token_CloseBrace; break;
|
||||
case ':': token.kind = Token_Colon; break;
|
||||
case '#':
|
||||
token.kind = Token_Hash;
|
||||
break;
|
||||
case '@':
|
||||
token.kind = Token_At;
|
||||
break;
|
||||
case '^':
|
||||
token.kind = Token_Pointer;
|
||||
break;
|
||||
case '?':
|
||||
token.kind = Token_Maybe;
|
||||
break;
|
||||
case ';':
|
||||
token.kind = Token_Semicolon;
|
||||
break;
|
||||
case ',':
|
||||
token.kind = Token_Comma;
|
||||
break;
|
||||
case ':':
|
||||
token.kind = Token_Colon;
|
||||
break;
|
||||
case '(':
|
||||
token.kind = Token_OpenParen;
|
||||
break;
|
||||
case ')':
|
||||
token.kind = Token_CloseParen;
|
||||
break;
|
||||
case '[':
|
||||
token.kind = Token_OpenBracket;
|
||||
break;
|
||||
case ']':
|
||||
token.kind = Token_CloseBracket;
|
||||
break;
|
||||
case '{':
|
||||
token.kind = Token_OpenBrace;
|
||||
break;
|
||||
case '}':
|
||||
token.kind = Token_CloseBrace;
|
||||
break;
|
||||
|
||||
case '*': token.kind = token_kind_variant2(t, Token_Mul, Token_MulEq); break;
|
||||
case '%': token.kind = token_kind_variant2(t, Token_Mod, Token_ModEq); break;
|
||||
case '=': token.kind = token_kind_variant2(t, Token_Eq, Token_CmpEq); break;
|
||||
case '~': token.kind = token_kind_variant2(t, Token_Xor, Token_XorEq); break;
|
||||
case '!': token.kind = token_kind_variant2(t, Token_Not, Token_NotEq); break;
|
||||
case '+': token.kind = token_kind_variant3(t, Token_Add, Token_AddEq, '+', Token_Increment); break;
|
||||
case '-': token.kind = token_kind_variant4(t, Token_Sub, Token_SubEq, '-', Token_Decrement, '>', Token_ArrowRight); break;
|
||||
case '+':
|
||||
token.kind = token_kind_variant2(t, Token_Add, Token_AddEq);
|
||||
break;
|
||||
case '-':
|
||||
token.kind = token_kind_variant3(t, Token_Sub, Token_SubEq, '>', Token_ArrowRight);
|
||||
break;
|
||||
case '/': {
|
||||
if (t->curr_rune == '/') {
|
||||
while (t->curr_rune != '\n') {
|
||||
while (t->curr_rune != '\n' && t->curr_rune != GB_RUNE_EOF) {
|
||||
advance_to_next_rune(t);
|
||||
}
|
||||
token.kind = Token_Comment;
|
||||
|
||||
+551
-179
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user