Basic Linux Build!

This commit is contained in:
Ginger Bill
2017-04-09 22:33:32 +01:00
parent 0683d2b4f4
commit aaf355e750
17 changed files with 212 additions and 274 deletions
+5 -4
View File
@@ -29,8 +29,8 @@ Type_Info_Record :: struct #ordered {
types: []^Type_Info,
names: []string,
offsets: []int, // offsets may not be used in tuples
size: int, // in bytes
align: int, // in bytes
// size: int,
// align: int,
packed: bool,
ordered: bool,
custom_align: bool,
@@ -39,7 +39,6 @@ Type_Info_Record :: struct #ordered {
Type_Info :: union {
size: int,
align: int,
Named{name: string, base: ^Type_Info},
Integer{signed: bool},
Float{},
@@ -95,6 +94,9 @@ Type_Info :: union {
// This will be set by the compiler
__type_table: []Type_Info;
__argv__: ^^byte;
__argc__: i32;
type_info_base :: proc(info: ^Type_Info) -> ^Type_Info {
if info == nil {
return nil;
@@ -422,7 +424,6 @@ __abs_quaternion256 :: proc(x: quaternion256) -> f64 #inline {
__dynamic_array_make :: proc(array_: rawptr, elem_size, elem_align: int, len, cap: int) {
array := cast(^raw.Dynamic_Array)array_;
__check_context();
+9 -10
View File
@@ -107,19 +107,19 @@ write_type :: proc(buf: ^[]byte, ti: ^Type_Info) {
}
case Float:
match 8*info.size {
case 32: write_string(buf, "f32");
case 64: write_string(buf, "f64");
match info.size {
case 4: write_string(buf, "f32");
case 8: write_string(buf, "f64");
}
case Complex:
match 8*info.size {
case 64: write_string(buf, "complex64");
case 128: write_string(buf, "complex128");
match info.size {
case 8: write_string(buf, "complex64");
case 16: write_string(buf, "complex128");
}
case Quaternion:
match 8*info.size {
case 128: write_string(buf, "quaternion128");
case 256: write_string(buf, "quaternion256");
match info.size {
case 16: write_string(buf, "quaternion128");
case 32: write_string(buf, "quaternion");
}
case String: write_string(buf, "string");
case Boolean: write_string(buf, "bool");
@@ -389,7 +389,6 @@ int_from_arg :: proc(args: []any, arg_index: int) -> (int, int, bool) {
arg.type_info = type_info_base(arg.type_info);
match i in arg {
case int: num = i;
case uint: num = cast(int)i;
case i8: num = cast(int)i;
case i16: num = cast(int)i;
case i32: num = cast(int)i;
+1
View File
@@ -1,4 +1,5 @@
#foreign_system_library lib "opengl32.lib" when ODIN_OS == "windows";
#foreign_system_library lib "gl" when ODIN_OS == "linux";
#import win32 "sys/windows.odin" when ODIN_OS == "windows";
#import "sys/wgl.odin" when ODIN_OS == "windows";
#load "opengl_constants.odin";
+82 -92
View File
@@ -3,10 +3,7 @@
Handle :: i32;
File_Time :: u64;
Errno :: int;
// TODO(zangent): Find out how to make this work on x64 and x32.
AddressSize :: i64;
Errno :: i32;
// INVALID_HANDLE: Handle : -1;
@@ -36,12 +33,13 @@ RTLD_NOW :: 0x002;
RTLD_BINDING_MASK :: 0x3;
RTLD_GLOBAL :: 0x100;
args: [dynamic]string;
// "Argv" arguments converted to Odin strings
immutable args := _alloc_command_line_arguments();
FileTime :: struct #ordered {
seconds: i64,
_File_Time :: struct #ordered {
seconds: i64,
nanoseconds: i32,
reserved: i32
reserved: i32,
}
// Translated from
@@ -49,27 +47,27 @@ FileTime :: struct #ordered {
// Validity is not guaranteed.
Stat :: struct #ordered {
device_id : u64, // ID of device containing file
serial : u64, // File serial number
nlink : u32, // Number of hard links
mode : u32, // Mode of the file
uid : u32, // User ID of the file's owner
gid : u32, // Group ID of the file's group
_padding : i32, // 32 bits of padding
rdev : u64, // Device ID, if device
size : i64, // Size of the file, in bytes
block_size : i64, // Optimal bllocksize for I/O
blocks : i64, // Number of 512-byte blocks allocated
device_id: u64, // ID of device containing file
serial: u64, // File serial number
nlink: u32, // Number of hard links
mode: u32, // Mode of the file
uid: u32, // User ID of the file's owner
gid: u32, // Group ID of the file's group
_padding: i32, // 32 bits of padding
rdev: u64, // Device ID, if device
size: i64, // Size of the file, in bytes
block_size: i64, // Optimal bllocksize for I/O
blocks: i64, // Number of 512-byte blocks allocated
last_access : FileTime, // Time of last access
modified : FileTime, // Time of last modification
status_change : FileTime, // Time of last status change
last_access: _File_Time, // Time of last access
modified: _File_Time, // Time of last modification
status_change: _File_Time, // Time of last status change
_reserve1,
_reserve2,
_reserve3 : i64,
serial : u64, // File serial number...? Maybe.
_reserve4 : i64
_reserve3: i64,
serial_numbe: u64, // File serial number...? Maybe.
_reserve4: i64,
};
// File type
@@ -125,32 +123,32 @@ F_OK :: 0; // Test for file existance
#foreign_system_library dl "dl";
#foreign_system_library libc "c";
unix_open :: proc(path: ^u8, mode: int) -> Handle #foreign libc "open";
unix_close :: proc(handle: Handle) #foreign libc "close";
unix_read :: proc(handle: Handle, buffer: rawptr, count: int) -> AddressSize #foreign libc "read";
unix_write :: proc(handle: Handle, buffer: rawptr, count: int) -> AddressSize #foreign libc "write";
unix_lseek :: proc(fs: Handle, offset: AddressSize, whence: int) -> AddressSize #foreign libc "lseek";
unix_gettid :: proc() -> u64 #foreign libc "gettid";
unix_stat :: proc(path: ^u8, stat: ^Stat) -> int #foreign libc "stat";
unix_access :: proc(path: ^u8, mask: int) -> int #foreign libc "access";
_unix_open :: proc(path: ^u8, mode: int) -> Handle #foreign libc "open";
_unix_close :: proc(fd: Handle) -> i32 #foreign libc "close";
_unix_read :: proc(fd: Handle, buf: rawptr, size: int) -> int #foreign libc "read";
_unix_write :: proc(fd: Handle, buf: rawptr, size: int) -> int #foreign libc "write";
_unix_seek :: proc(fd: Handle, offset: i64, whence: i32) -> i64 #foreign libc "lseek64";
_unix_gettid :: proc() -> u64 #foreign libc "gettid";
_unix_stat :: proc(path: ^u8, stat: ^Stat) -> i32 #foreign libc "stat";
_unix_access :: proc(path: ^u8, mask: int) -> i32 #foreign libc "access";
unix_malloc :: proc(size: int) -> rawptr #foreign libc "malloc";
unix_free :: proc(ptr: rawptr) #foreign libc "free";
unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr #foreign libc "realloc";
unix_getenv :: proc(^u8) -> ^u8 #foreign libc "getenv";
_unix_malloc :: proc(size: int) -> rawptr #foreign libc "malloc";
_unix_free :: proc(ptr: rawptr) #foreign libc "free";
_unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr #foreign libc "realloc";
_unix_getenv :: proc(^u8) -> ^u8 #foreign libc "getenv";
unix_exit :: proc(status: int) #foreign libc "exit";
_unix_exit :: proc(status: int) #foreign libc "exit";
unix_dlopen :: proc(filename: ^u8, flags: int) -> rawptr #foreign dl "dlopen";
unix_dlsym :: proc(handle: rawptr, symbol: ^u8) -> (proc() #cc_c) #foreign dl "dlsym";
unix_dlclose :: proc(handle: rawptr) -> int #foreign dl "dlclose";
unix_dlerror :: proc() -> ^u8 #foreign dl "dlerror";
_unix_dlopen :: proc(filename: ^u8, flags: int) -> rawptr #foreign dl "dlopen";
_unix_dlsym :: proc(handle: rawptr, symbol: ^u8) -> (proc() #cc_c) #foreign dl "dlsym";
_unix_dlclose :: proc(handle: rawptr) -> int #foreign dl "dlclose";
_unix_dlerror :: proc() -> ^u8 #foreign dl "dlerror";
// TODO(zangent): Change this to just `open` when Bill fixes overloading.
open_simple :: proc(path: string, mode: int) -> (Handle, Errno) {
cstr := strings.new_c_string(path);
handle := unix_open(cstr, mode);
handle := _unix_open(cstr, mode);
free(cstr);
if(handle == -1) {
return 0, 1;
@@ -163,44 +161,29 @@ open :: proc(path: string, mode: int, perm: u32) -> (Handle, Errno) {
}
close :: proc(fd: Handle) {
unix_close(fd);
_unix_close(fd);
}
write :: proc(fd: Handle, data: []byte) -> (AddressSize, Errno) {
assert(fd != -1);
bytes_written := unix_write(fd, ^data[0], len(data));
if(bytes_written == -1) {
return 0, 1;
}
return bytes_written, 0;
read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
sz := _unix_read(fd, ^data[0], len(data));
return sz, 0;
}
read :: proc(fd: Handle, data: []byte) -> (AddressSize, Errno) {
assert(fd != -1);
bytes_read := unix_read(fd, ^data[0], len(data));
if(bytes_read == -1) {
return 0, 1;
}
return bytes_read, 0;
write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
sz := _unix_write(fd, ^data[0], len(data));
return sz, 0;
}
seek :: proc(fd: Handle, offset: AddressSize, whence: int) -> (AddressSize, Errno) {
assert(fd != -1);
final_offset := unix_lseek(fd, offset, whence);
if(final_offset == -1) {
return 0, 1;
}
return final_offset, 0;
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
res := _unix_seek(fd, offset, cast(i32)whence);
return res, 0;
}
// NOTE(bill): Uses startup to initialize it
stdin: Handle = 0; // get_std_handle(win32.STD_INPUT_HANDLE);
stdout: Handle = 1; // get_std_handle(win32.STD_OUTPUT_HANDLE);
stderr: Handle = 2; // get_std_handle(win32.STD_ERROR_HANDLE);
stdin: Handle = 0;
stdout: Handle = 1;
stderr: Handle = 2;
/* TODO(zangent): Implement these!
last_write_time :: proc(fd: Handle) -> File_Time {}
@@ -211,35 +194,36 @@ stat :: proc(path: string) -> (Stat, int) #inline {
s: Stat;
cstr := strings.new_c_string(path);
defer free(cstr);
ret_int := unix_stat(cstr, ^s);
return s, ret_int;
ret_int := _unix_stat(cstr, ^s);
return s, cast(int)ret_int;
}
access :: proc(path: string, mask: int) -> bool #inline {
cstr := strings.new_c_string(path);
defer free(cstr);
return unix_access(cstr, mask) == 0;
return _unix_access(cstr, mask) == 0;
}
read_entire_file :: proc(name: string) -> ([]byte, bool) {
fd: Handle;
err: Errno;
size: i64;
handle, err := open_simple(name, O_RDONLY);
fd, err = open_simple(name, O_RDONLY);
if(err != 0) {
fmt.println("Failed to open file.");
return nil, false;
}
defer(close(handle));
defer close(fd);
// We have a file!
size: AddressSize;
size, err = seek(handle, 0, SEEK_END);
// We have a file
size, err = seek(fd, 0, SEEK_END);
if(err != 0) {
fmt.println("Failed to seek to end of file.");
return nil, false;
}
_, err = seek(handle, 0, SEEK_SET);
_, err = seek(fd, 0, SEEK_SET);
if(err != 0) {
fmt.println("Failed to seek to beginning of file.");
return nil, false;
@@ -253,7 +237,7 @@ read_entire_file :: proc(name: string) -> ([]byte, bool) {
return nil, false;
}
read(handle, data);
read(fd, data);
data[size] = 0;
return data, true;
@@ -261,20 +245,20 @@ read_entire_file :: proc(name: string) -> ([]byte, bool) {
heap_alloc :: proc(size: int) -> rawptr {
assert(size > 0);
return unix_malloc(size);
return _unix_malloc(size);
}
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
return unix_realloc(ptr, new_size);
return _unix_realloc(ptr, new_size);
}
heap_free :: proc(ptr: rawptr) {
unix_free(ptr);
_unix_free(ptr);
}
getenv :: proc(name: string) -> (string, bool) {
path_str := strings.new_c_string(name);
cstr: ^u8 = unix_getenv(path_str);
cstr: ^u8 = _unix_getenv(path_str);
free(path_str);
if(cstr == nil) {
return "", false;
@@ -283,31 +267,37 @@ getenv :: proc(name: string) -> (string, bool) {
}
exit :: proc(code: int) {
unix_exit(code);
_unix_exit(code);
}
current_thread_id :: proc() -> int {
// return cast(int) unix_gettid();
// return cast(int) _unix_gettid();
return 0;
}
dlopen :: proc(filename: string, flags: int) -> rawptr #inline {
cstr := strings.new_c_string(filename);
handle := unix_dlopen(cstr, flags);
handle := _unix_dlopen(cstr, flags);
free(cstr);
return handle;
}
dlsym :: proc(handle: rawptr, symbol: string) -> (proc() #cc_c) #inline {
assert(handle != nil);
cstr := strings.new_c_string(symbol);
proc_handle := unix_dlsym(handle, cstr);
proc_handle := _unix_dlsym(handle, cstr);
free(cstr);
return proc_handle;
}
dlclose :: proc(handle: rawptr) -> bool #inline {
assert(handle != nil);
return unix_dlclose(handle) == 0;
return _unix_dlclose(handle) == 0;
}
dlerror :: proc() -> string {
return strings.to_odin_string(unix_dlerror());
return strings.to_odin_string(_unix_dlerror());
}
_alloc_command_line_arguments :: proc() -> []string {
// TODO(bill):
return nil;
}
+2 -91
View File
@@ -1,91 +1,2 @@
#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++;
}
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++;
return true;
}
mutex_unlock :: proc(m: ^Mutex) {
recursion: i32;
thread_id := current_thread_id();
assert(thread_id == atomic.load(^m._owner));
m._recursion--;
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);
}
}
}
#load "sync_windows.odin" when ODIN_OS == "windows";
#load "sync_linux.odin" when ODIN_OS == "linux";