This commit is contained in:
gingerBill
2021-03-01 12:15:38 +00:00
16 changed files with 1116 additions and 721 deletions
+70
View File
@@ -0,0 +1,70 @@
package os
import "core:strings"
import "core:mem"
read_dir :: proc(fd: Handle, n: int, allocator := context.allocator) -> (fi: []File_Info, err: Errno) {
dirp: Dir;
dirp, err = _fdopendir(fd);
if err != ERROR_NONE {
return;
}
defer _closedir(dirp);
dirpath: string;
dirpath, err = absolute_path_from_handle(fd);
if err != ERROR_NONE {
return;
}
defer delete(dirpath);
n := n;
size := n;
if n <= 0 {
n = -1;
size = 100;
}
dfi := make([dynamic]File_Info, 0, size, allocator);
for {
entry: Dirent;
end_of_stream: bool;
entry, err, end_of_stream = _readdir(dirp);
if err != ERROR_NONE {
for fi_ in dfi {
file_info_delete(fi_, allocator);
}
delete(dfi);
return;
} else if end_of_stream {
break;
}
fi_: File_Info;
filename := cast(string)(transmute(cstring)mem.Raw_Cstring{ data = &entry.name[0] });
if filename == "." || filename == ".." {
continue;
}
fullpath := strings.join( []string{ dirpath, filename }, "/", context.temp_allocator);
defer delete(fullpath, context.temp_allocator);
fi_, err = stat(fullpath, allocator);
if err != ERROR_NONE {
for fi__ in dfi {
file_info_delete(fi__, allocator);
}
delete(dfi);
return;
}
append(&dfi, fi_);
}
return dfi[:], ERROR_NONE;
}
+170 -32
View File
@@ -6,6 +6,7 @@ foreign import pthread "System.framework"
import "core:runtime"
import "core:strings"
import "core:strconv"
import "core:c"
Handle :: distinct i32;
@@ -191,7 +192,7 @@ Unix_File_Time :: struct {
nanoseconds: i64,
}
Stat :: struct {
OS_Stat :: struct {
device_id: i32, // ID of device containing file
mode: u16, // Mode of the file
nlink: u16, // Number of hard links
@@ -215,6 +216,17 @@ Stat :: struct {
_reserve2: i64, // RESERVED
};
// NOTE(laleksic, 2021-01-21): Comment and rename these to match OS_Stat above
Dirent :: struct {
ino: u64,
off: u64,
reclen: u16,
type: u8,
name: [256]byte,
};
Dir :: distinct rawptr; // DIR*
// File type
S_IFMT :: 0o170000; // Type of file mask
S_IFIFO :: 0o010000; // Named pipe (fifo)
@@ -264,25 +276,33 @@ F_OK :: 0; // Test for file existance
foreign libc {
@(link_name="__error") __error :: proc() -> ^int ---;
@(link_name="open") _unix_open :: proc(path: cstring, flags: i32, mode: u16) -> Handle ---;
@(link_name="close") _unix_close :: proc(handle: Handle) ---;
@(link_name="read") _unix_read :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
@(link_name="write") _unix_write :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
@(link_name="lseek") _unix_lseek :: proc(fs: Handle, offset: int, whence: int) -> int ---;
@(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
@(link_name="getpagesize") _unix_getpagesize :: proc() -> i32 ---;
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^Stat) -> int ---;
@(link_name="access") _unix_access :: proc(path: cstring, mask: int) -> int ---;
@(link_name="open") _unix_open :: proc(path: cstring, flags: i32, mode: u16) -> Handle ---;
@(link_name="close") _unix_close :: proc(handle: Handle) ---;
@(link_name="read") _unix_read :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
@(link_name="write") _unix_write :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
@(link_name="lseek") _unix_lseek :: proc(fs: Handle, offset: int, whence: int) -> int ---;
@(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
@(link_name="getpagesize") _unix_getpagesize :: proc() -> i32 ---;
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
@(link_name="lstat") _unix_lstat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
@(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---;
@(link_name="readlink") _unix_readlink :: proc(path: cstring, buf: ^byte, bufsiz: c.size_t) -> c.ssize_t ---;
@(link_name="access") _unix_access :: proc(path: cstring, mask: int) -> int ---;
@(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---;
@(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---;
@(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---;
@(link_name="readdir_r") _unix_readdir_r :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---;
@(link_name="malloc") _unix_malloc :: proc(size: int) -> rawptr ---;
@(link_name="calloc") _unix_calloc :: proc(num, size: int) -> rawptr ---;
@(link_name="free") _unix_free :: proc(ptr: rawptr) ---;
@(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---;
@(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---;
@(link_name="getcwd") _unix_getcwd :: proc(buf: cstring, len: c.size_t) -> cstring ---;
@(link_name="chdir") _unix_chdir :: proc(buf: cstring) -> c.int ---;
@(link_name="malloc") _unix_malloc :: proc(size: int) -> rawptr ---;
@(link_name="calloc") _unix_calloc :: proc(num, size: int) -> rawptr ---;
@(link_name="free") _unix_free :: proc(ptr: rawptr) ---;
@(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---;
@(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---;
@(link_name="getcwd") _unix_getcwd :: proc(buf: cstring, len: c.size_t) -> cstring ---;
@(link_name="chdir") _unix_chdir :: proc(buf: cstring) -> c.int ---;
@(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---;
@(link_name="exit") _unix_exit :: proc(status: int) ---;
@(link_name="exit") _unix_exit :: proc(status: int) ---;
}
foreign dl {
@@ -366,17 +386,138 @@ is_path_separator :: proc(r: rune) -> bool {
return r == '/';
}
stat :: proc(path: string) -> (Stat, Errno) {
s: Stat;
cstr := strings.clone_to_cstring(path);
defer delete(cstr);
ret_int := _unix_stat(cstr, &s);
return s, Errno(ret_int);
@private
_stat :: proc(path: string) -> (OS_Stat, Errno) {
cstr := strings.clone_to_cstring(path, context.temp_allocator);
s: OS_Stat;
result := _unix_stat(cstr, &s);
if result == -1 {
return s, Errno(get_last_error());
}
return s, ERROR_NONE;
}
@private
_lstat :: proc(path: string) -> (OS_Stat, Errno) {
cstr := strings.clone_to_cstring(path, context.temp_allocator);
s: OS_Stat;
result := _unix_lstat(cstr, &s);
if result == -1 {
return s, Errno(get_last_error());
}
return s, ERROR_NONE;
}
@private
_fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
s: OS_Stat;
result := _unix_fstat(fd, &s);
if result == -1 {
return s, Errno(get_last_error());
}
return s, ERROR_NONE;
}
@private
_fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
dirp := _unix_fdopendir(fd);
if dirp == cast(Dir)nil {
return nil, Errno(get_last_error());
}
return dirp, ERROR_NONE;
}
@private
_closedir :: proc(dirp: Dir) -> Errno {
rc := _unix_closedir(dirp);
if rc != 0 {
return Errno(get_last_error());
}
return ERROR_NONE;
}
@private
_rewinddir :: proc(dirp: Dir) {
_unix_rewinddir(dirp);
}
@private
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
result: ^Dirent;
rc := _unix_readdir_r(dirp, &entry, &result);
if rc != 0 {
err = Errno(get_last_error());
return;
}
err = ERROR_NONE;
if result == nil {
end_of_stream = true;
return;
}
end_of_stream = false;
return;
}
@private
_readlink :: proc(path: string) -> (string, Errno) {
path_cstr := strings.clone_to_cstring(path, context.temp_allocator);
bufsz : uint = 256;
buf := make([]byte, bufsz);
for {
rc := _unix_readlink(path_cstr, &(buf[0]), bufsz);
if rc == -1 {
delete(buf);
return "", Errno(get_last_error());
} else if rc == int(bufsz) {
// NOTE(laleksic, 2021-01-21): Any cleaner way to resize the slice?
bufsz *= 2;
delete(buf);
buf = make([]byte, bufsz);
} else {
return strings.string_from_ptr(&buf[0], rc), ERROR_NONE;
}
}
}
absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
buf : [256]byte;
fd_str := strconv.itoa( buf[:], cast(int)fd );
procfs_path := strings.concatenate( []string{ "/proc/self/fd/", fd_str } );
defer delete(procfs_path);
return _readlink(procfs_path);
}
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
rel := rel;
if rel == "" {
rel = ".";
}
rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator);
path_ptr := _unix_realpath(rel_cstr, nil);
if path_ptr == nil {
return "", Errno(get_last_error());
}
defer _unix_free(path_ptr);
path_cstr := transmute(cstring)path_ptr;
path = strings.clone( string(path_cstr) );
return path, ERROR_NONE;
}
access :: proc(path: string, mask: int) -> bool {
cstr := strings.clone_to_cstring(path);
defer delete(cstr);
cstr := strings.clone_to_cstring(path, context.temp_allocator);
return _unix_access(cstr, mask) == 0;
}
@@ -392,8 +533,7 @@ heap_free :: proc(ptr: rawptr) {
}
getenv :: proc(name: string) -> (string, bool) {
path_str := strings.clone_to_cstring(name);
defer delete(path_str);
path_str := strings.clone_to_cstring(name, context.temp_allocator);
cstr := _unix_getenv(path_str);
if cstr == nil {
return "", false;
@@ -441,15 +581,13 @@ current_thread_id :: proc "contextless" () -> int {
}
dlopen :: proc(filename: string, flags: int) -> rawptr {
cstr := strings.clone_to_cstring(filename);
defer delete(cstr);
cstr := strings.clone_to_cstring(filename, context.temp_allocator);
handle := _unix_dlopen(cstr, flags);
return handle;
}
dlsym :: proc(handle: rawptr, symbol: string) -> rawptr {
assert(handle != nil);
cstr := strings.clone_to_cstring(symbol);
defer delete(cstr);
cstr := strings.clone_to_cstring(symbol, context.temp_allocator);
proc_handle := _unix_dlsym(handle, cstr);
return proc_handle;
}
+3
View File
@@ -50,3 +50,6 @@ write :: proc(fd: Handle, data: []u8) -> (int, Errno) {
return (int) (0), (Errno) (1);
}
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
return (i64) (0), (Errno) (1);
}
+7 -7
View File
@@ -151,7 +151,7 @@ Unix_File_Time :: struct {
pid_t :: u32;
Stat :: struct {
OS_Stat :: struct {
device_id: u64,
serial: u64,
nlink: u64,
@@ -235,8 +235,8 @@ foreign libc {
@(link_name="lseek64") _unix_seek :: proc(fd: Handle, offset: i64, whence: c.int) -> i64 ---;
@(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
@(link_name="getpagesize") _unix_getpagesize :: proc() -> c.int ---;
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^Stat) -> c.int ---;
@(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^Stat) -> c.int ---;
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
@(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---;
@(link_name="access") _unix_access :: proc(path: cstring, mask: c.int) -> c.int ---;
@(link_name="malloc") _unix_malloc :: proc(size: c.size_t) -> rawptr ---;
@@ -341,11 +341,11 @@ last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
return File_Time(modified), ERROR_NONE;
}
stat :: proc(path: string) -> (Stat, Errno) {
stat :: proc(path: string) -> (OS_Stat, Errno) {
cstr := strings.clone_to_cstring(path);
defer delete(cstr);
s: Stat;
s: OS_Stat;
result := _unix_stat(cstr, &s);
if result == -1 {
return s, Errno(get_last_error());
@@ -353,8 +353,8 @@ stat :: proc(path: string) -> (Stat, Errno) {
return s, ERROR_NONE;
}
fstat :: proc(fd: Handle) -> (Stat, Errno) {
s: Stat;
fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
s: OS_Stat;
result := _unix_fstat(fd, &s);
if result == -1 {
return s, Errno(get_last_error());
+11 -11
View File
@@ -185,7 +185,7 @@ Unix_File_Time :: struct {
nanoseconds: i64,
}
Stat :: struct {
OS_Stat :: struct {
device_id: u64, // ID of device containing file
serial: u64, // File serial number
nlink: u64, // Number of hard links
@@ -207,7 +207,7 @@ Stat :: struct {
_reserve3: i64,
};
// NOTE(laleksic, 2021-01-21): Comment and rename these to match Stat above
// NOTE(laleksic, 2021-01-21): Comment and rename these to match OS_Stat above
Dirent :: struct {
ino: u64,
off: u64,
@@ -278,9 +278,9 @@ foreign libc {
@(link_name="lseek64") _unix_seek :: proc(fd: Handle, offset: i64, whence: c.int) -> i64 ---;
@(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
@(link_name="getpagesize") _unix_getpagesize :: proc() -> c.int ---;
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^Stat) -> c.int ---;
@(link_name="lstat") _unix_lstat :: proc(path: cstring, stat: ^Stat) -> c.int ---;
@(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^Stat) -> c.int ---;
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
@(link_name="lstat") _unix_lstat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
@(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---;
@(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---;
@(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---;
@(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---;
@@ -397,11 +397,11 @@ last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
}
@private
_stat :: proc(path: string) -> (Stat, Errno) {
_stat :: proc(path: string) -> (OS_Stat, Errno) {
cstr := strings.clone_to_cstring(path);
defer delete(cstr);
s: Stat;
s: OS_Stat;
result := _unix_stat(cstr, &s);
if result == -1 {
return s, Errno(get_last_error());
@@ -410,11 +410,11 @@ _stat :: proc(path: string) -> (Stat, Errno) {
}
@private
_lstat :: proc(path: string) -> (Stat, Errno) {
_lstat :: proc(path: string) -> (OS_Stat, Errno) {
cstr := strings.clone_to_cstring(path);
defer delete(cstr);
s: Stat;
s: OS_Stat;
result := _unix_lstat(cstr, &s);
if result == -1 {
return s, Errno(get_last_error());
@@ -423,8 +423,8 @@ _lstat :: proc(path: string) -> (Stat, Errno) {
}
@private
_fstat :: proc(fd: Handle) -> (Stat, Errno) {
s: Stat;
_fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
s: OS_Stat;
result := _unix_fstat(fd, &s);
if result == -1 {
return s, Errno(get_last_error());
@@ -1,3 +1,4 @@
//+build linux, darwin, freebsd
package os
import "core:time"
@@ -58,10 +59,10 @@ _make_time_from_unix_file_time :: proc(uft: Unix_File_Time) -> time.Time {
}
@private
_fill_file_info_from_stat :: proc(fi: ^File_Info, s: Stat) {
_fill_file_info_from_stat :: proc(fi: ^File_Info, s: OS_Stat) {
fi.size = s.size;
fi.mode = cast(File_Mode)s.mode;
fi.is_dir = S_ISDIR(s.mode);
fi.is_dir = S_ISDIR(auto_cast s.mode);
// NOTE(laleksic, 2021-01-21): Not really creation time, but closest we can get (maybe better to leave it 0?)
fi.creation_time = _make_time_from_unix_file_time(s.status_change);
@@ -74,7 +75,7 @@ lstat :: proc(name: string, allocator := context.allocator) -> (fi: File_Info, e
context.allocator = allocator;
s: Stat;
s: OS_Stat;
s, err = _lstat(name);
if err != ERROR_NONE {
return fi, err;
@@ -92,7 +93,7 @@ stat :: proc(name: string, allocator := context.allocator) -> (fi: File_Info, er
context.allocator = allocator;
s: Stat;
s: OS_Stat;
s, err = _stat(name);
if err != ERROR_NONE {
return fi, err;
@@ -110,7 +111,7 @@ fstat :: proc(fd: Handle, allocator := context.allocator) -> (fi: File_Info, err
context.allocator = allocator;
s: Stat;
s: OS_Stat;
s, err = _fstat(fd);
if err != ERROR_NONE {
return fi, err;
+81 -28
View File
@@ -32,36 +32,38 @@ volume_name :: proc(path: string) -> string {
}
volume_name_len :: proc(path: string) -> int {
if len(path) < 2 {
return 0;
}
c := path[0];
if path[1] == ':' {
switch c {
case 'a'..'z', 'A'..'Z':
return 2;
if ODIN_OS == "windows" {
if len(path) < 2 {
return 0;
}
}
// URL: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
if l := len(path); l >= 5 && is_slash(path[0]) && is_slash(path[1]) &&
!is_slash(path[2]) && path[2] != '.' {
for n := 3; n < l-1; n += 1 {
if is_slash(path[n]) {
n += 1;
if !is_slash(path[n]) {
if path[n] == '.' {
break;
}
}
for ; n < l; n += 1 {
if is_slash(path[n]) {
break;
}
}
return n;
c := path[0];
if path[1] == ':' {
switch c {
case 'a'..'z', 'A'..'Z':
return 2;
}
}
// URL: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
if l := len(path); l >= 5 && is_slash(path[0]) && is_slash(path[1]) &&
!is_slash(path[2]) && path[2] != '.' {
for n := 3; n < l-1; n += 1 {
if is_slash(path[n]) {
n += 1;
if !is_slash(path[n]) {
if path[n] == '.' {
break;
}
}
for ; n < l; n += 1 {
if is_slash(path[n]) {
break;
}
}
return n;
}
break;
}
break;
}
}
return 0;
@@ -296,6 +298,57 @@ dir :: proc(path: string, allocator := context.allocator) -> string {
split_list :: proc(path: string, allocator := context.allocator) -> []string {
if path == "" {
return nil;
}
start: int;
quote: bool;
start, quote = 0, false;
count := 0;
for i := 0; i < len(path); i += 1 {
c := path[i];
switch {
case c == '"':
quote = !quote;
case c == LIST_SEPARATOR && !quote:
count += 1;
}
}
start, quote = 0, false;
list := make([]string, count, allocator);
index := 0;
for i := 0; i < len(path); i += 1 {
c := path[i];
switch {
case c == '"':
quote = !quote;
case c == LIST_SEPARATOR && !quote:
list[index] = path[start:i];
index += 1;
start = i + 1;
}
}
assert(index == count);
for s0, i in list {
s, new := strings.replace_all(s0, `"`, ``, allocator);
if !new {
s = strings.clone(s, allocator);
}
list[i] = s;
}
return list;
}
/*
Lazy_Buffer is a lazily made path buffer
When it does allocate, it uses the context.allocator
+16 -7
View File
@@ -8,6 +8,14 @@ SEPARATOR :: '/';
SEPARATOR_STRING :: `/`;
LIST_SEPARATOR :: ':';
is_reserved_name :: proc(path: string) -> bool {
return false;
}
is_abs :: proc(path: string) -> bool {
return strings.has_prefix(path, "/");
}
abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
full_path, err := os.absolute_path_from_relative(path);
if err != os.ERROR_NONE {
@@ -17,10 +25,11 @@ abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
}
join :: proc(elems: ..string, allocator := context.allocator) -> string {
s := strings.join(elems, SEPARATOR_STRING);
return s;
}
is_abs :: proc(path: string) -> bool {
return (path[0] == '/');
}
for e, i in elems {
if e != "" {
p := strings.join(elems[i:], SEPARATOR_STRING, context.temp_allocator);
return clean(p, allocator);
}
}
return "";
}
-50
View File
@@ -87,56 +87,6 @@ abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
return p, true;
}
split_list :: proc(path: string, allocator := context.allocator) -> []string {
if path == "" {
return nil;
}
start: int;
quote: bool;
start, quote = 0, false;
count := 0;
for i := 0; i < len(path); i += 1 {
c := path[i];
switch {
case c == '"':
quote = !quote;
case c == LIST_SEPARATOR && !quote:
count += 1;
}
}
start, quote = 0, false;
list := make([]string, count, allocator);
index := 0;
for i := 0; i < len(path); i += 1 {
c := path[i];
switch {
case c == '"':
quote = !quote;
case c == LIST_SEPARATOR && !quote:
list[index] = path[start:i];
index += 1;
start = i + 1;
}
}
assert(index == count);
for s0, i in list {
s, new := strings.replace_all(s0, `"`, ``, allocator);
if !new {
s = strings.clone(s, allocator);
}
list[i] = s;
}
return list;
}
join :: proc(elems: ..string, allocator := context.allocator) -> string {
for e, i in elems {
+1 -1
View File
@@ -167,7 +167,7 @@ concatenate :: proc(a: []$T/[]$E, allocator := context.allocator) -> (res: T) {
res = make(T, n, allocator);
i := 0;
for s in a {
i += copy(b[i:], s);
i += copy(res[i:], s);
}
return;
}
+649 -521
View File
File diff suppressed because it is too large Load Diff
+17 -1
View File
@@ -1,2 +1,18 @@
package time
IS_SUPPORTED :: false;
import "core:sys/es"
IS_SUPPORTED :: true;
now :: proc() -> Time {
// TODO Replace once there's a proper time API.
return Time{_nsec = i64(es.TimeStampMs() * 1e6)};
}
sleep :: proc(d: Duration) {
es.Sleep(u64(d/Millisecond));
}
_tick_now :: proc() -> Tick {
return Tick{_nsec = i64(es.TimeStampMs() * 1e6)};
}
+4 -6
View File
@@ -1724,6 +1724,10 @@ void add_dependency_to_set(Checker *c, Entity *entity) {
void force_add_dependency_entity(Checker *c, Scope *scope, String const &name) {
Entity *e = scope_lookup(scope, name);
if (e == nullptr) {
return;
}
GB_ASSERT_MSG(e != nullptr, "unable to find %.*s", LIT(name));
e->flags |= EntityFlag_Used;
add_dependency_to_set(c, e);
}
@@ -4381,12 +4385,6 @@ GB_THREAD_PROC(check_proc_info_worker_proc) {
}
void check_unchecked_bodies(Checker *c) {
#if !defined(GB_SYSTEM_WINDOWS)
// HACK TODO(2021-02-26, bill): THIS IS A FUCKING HACK
if (true) {
return;
}
#endif
// NOTE(2021-02-26, bill): Sanity checker
// This is a partial hack to make sure all procedure bodies have been checked
// even ones which should not exist, due to the multithreaded nature of the parser
+79 -51
View File
@@ -429,6 +429,14 @@ void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) {
GB_ASSERT(value.value != nullptr);
value = lb_emit_conv(p, value, lb_addr_type(addr));
if (lb_is_const_or_global(value)) {
// NOTE(bill): Just bypass the actual storage and set the initializer
if (LLVMGetValueKind(addr.addr.value) == LLVMGlobalVariableValueKind) {
LLVMSetInitializer(addr.addr.value, value.value);
return;
}
}
lb_emit_store(p, addr.addr, value);
}
@@ -882,10 +890,6 @@ String lb_get_entity_name(lbModule *m, Entity *e, String default_name) {
}
if (e->kind == Entity_TypeName) {
if ((e->scope->flags & ScopeFlag_File) == 0) {
gb_printf_err("<<< %.*s %.*s %p\n", LIT(e->token.string), LIT(name), e);
}
e->TypeName.ir_mangled_name = name;
} else if (e->kind == Entity_Procedure) {
e->Procedure.link_name = name;
@@ -1200,7 +1204,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
for_array(i, type->Struct.fields) {
Entity *field = type->Struct.fields[i];
fields[i+offset] = lb_type(m, field->type);
fields[i+offset] = lb_type(m, field->type);
}
@@ -1271,7 +1275,8 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
}
case Type_Proc:
if (m->internal_type_level > 256) { // TODO HACK(bill): is this really enough?
// if (m->internal_type_level > 256) { // TODO HACK(bill): is this really enough?
if (m->internal_type_level > 1) { // TODO HACK(bill): is this really enough?
return LLVMPointerType(LLVMIntTypeInContext(m->ctx, 8), 0);
} else {
unsigned param_count = 0;
@@ -8511,6 +8516,13 @@ bool lb_is_const(lbValue value) {
}
return false;
}
bool lb_is_const_or_global(lbValue value) {
return (LLVMGetValueKind(value.value) == LLVMGlobalVariableValueKind) || lb_is_const(value);
}
bool lb_is_const_nil(lbValue value) {
LLVMValueRef v = value.value;
if (LLVMIsConstant(v)) {
@@ -10048,7 +10060,7 @@ lbValue lb_const_hash(lbModule *m, lbValue key, Type *key_type) {
LLVMValueRef len = LLVMConstExtractValue(key.value, len_indices, gb_count_of(len_indices));
isize length = LLVMConstIntGetSExtValue(len);
char const *text = nullptr;
if (length != 0) {
if (false && length != 0) {
if (LLVMGetConstOpcode(data) != LLVMGetElementPtr) {
return {};
}
@@ -11523,8 +11535,7 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da
for_array(type_info_type_index, info->type_info_types) {
Type *t = info->type_info_types[type_info_type_index];
t = default_type(t);
if (t == t_invalid) {
if (t == nullptr || t == t_invalid) {
continue;
}
@@ -12452,6 +12463,7 @@ void lb_generate_code(lbGenerator *gen) {
}
if (is_foreign) {
LLVMSetExternallyInitialized(g.value, true);
lb_add_foreign_library_path(m, e->Variable.foreign_library);
} else {
LLVMSetInitializer(g.value, LLVMConstNull(lb_type(m, e->type)));
}
@@ -12460,6 +12472,10 @@ void lb_generate_code(lbGenerator *gen) {
LLVMSetDLLStorageClass(g.value, LLVMDLLExportStorageClass);
}
if (e->flags & EntityFlag_Static) {
LLVMSetLinkage(g.value, LLVMInternalLinkage);
}
GlobalVariable var = {};
var.var = g;
var.decl = decl;
@@ -12551,6 +12567,8 @@ void lb_generate_code(lbGenerator *gen) {
LLVMPassManagerRef default_function_pass_manager = LLVMCreateFunctionPassManagerForModule(mod);
defer (LLVMDisposePassManager(default_function_pass_manager));
LLVMInitializeFunctionPassManager(default_function_pass_manager);
{
auto dfpm = default_function_pass_manager;
@@ -12615,10 +12633,12 @@ void lb_generate_code(lbGenerator *gen) {
} while (repeat_count --> 0);
}
}
LLVMFinalizeFunctionPassManager(default_function_pass_manager);
LLVMPassManagerRef default_function_pass_manager_without_memcpy = LLVMCreateFunctionPassManagerForModule(mod);
defer (LLVMDisposePassManager(default_function_pass_manager_without_memcpy));
LLVMInitializeFunctionPassManager(default_function_pass_manager_without_memcpy);
{
auto dfpm = default_function_pass_manager_without_memcpy;
LLVMAddPromoteMemoryToRegisterPass(dfpm);
@@ -12631,8 +12651,9 @@ void lb_generate_code(lbGenerator *gen) {
LLVMAddCFGSimplificationPass(dfpm);
// LLVMAddUnifyFunctionExitNodesPass(dfpm);
}
LLVMFinalizeFunctionPassManager(default_function_pass_manager_without_memcpy);
TIME_SECTION("LLVM Runtime Creation");
TIME_SECTION("LLVM Runtime Type Information Creation");
lbProcedure *startup_type_info = nullptr;
lbProcedure *startup_runtime = nullptr;
@@ -12661,6 +12682,7 @@ void lb_generate_code(lbGenerator *gen) {
LLVMRunFunctionPassManager(default_function_pass_manager, p->value);
}
TIME_SECTION("LLVM Runtime Startup Creation (Global Variables)");
{ // Startup Runtime
Type *params = alloc_type_tuple();
Type *results = alloc_type_tuple();
@@ -12678,28 +12700,28 @@ void lb_generate_code(lbGenerator *gen) {
for_array(i, global_variables) {
auto *var = &global_variables[i];
if (var->decl->init_expr != nullptr) {
lbValue init = lb_build_expr(p, var->decl->init_expr);
if (lb_is_const(init)) {
if (!var->is_initialized) {
LLVMSetInitializer(var->var.value, init.value);
var->is_initialized = true;
}
} else {
var->init = init;
}
if (var->is_initialized) {
continue;
}
Entity *e = var->decl->entity;
GB_ASSERT(e->kind == Entity_Variable);
if (e->Variable.is_foreign) {
Entity *fl = e->Procedure.foreign_library;
lb_add_foreign_library_path(m, fl);
}
if (var->decl->init_expr != nullptr) {
// gb_printf_err("%s\n", expr_to_string(var->decl->init_expr));
lbValue init = lb_build_expr(p, var->decl->init_expr);
LLVMValueKind value_kind = LLVMGetValueKind(init.value);
// gb_printf_err("%s %d\n", LLVMPrintValueToString(init.value));
if (e->flags & EntityFlag_Static) {
LLVMSetLinkage(var->var.value, LLVMInternalLinkage);
if (lb_is_const_or_global(init)) {
if (!var->is_initialized) {
LLVMSetInitializer(var->var.value, init.value);
var->is_initialized = true;
continue;
}
} else {
var->init = init;
}
}
if (var->init.value != nullptr) {
@@ -12718,7 +12740,12 @@ void lb_generate_code(lbGenerator *gen) {
lb_emit_store(p, data, lb_emit_conv(p, gp, t_rawptr));
lb_emit_store(p, ti, lb_type_info(m, var_type));
} else {
lb_emit_store(p, var->var, lb_emit_conv(p, var->init, t));
LLVMTypeRef pvt = LLVMTypeOf(var->var.value);
LLVMTypeRef vt = LLVMGetElementType(pvt);
lbValue src0 = lb_emit_conv(p, var->init, t);
LLVMValueRef src = OdinLLVMBuildTransmute(p, src0.value, vt);
LLVMValueRef dst = var->var.value;
LLVMBuildStore(p->builder, src, dst);
}
var->is_initialized = true;
@@ -12758,6 +12785,7 @@ void lb_generate_code(lbGenerator *gen) {
}
if (!(build_context.build_mode == BuildMode_DynamicLibrary && !has_dll_main)) {
TIME_SECTION("LLVM DLL main");
Type *params = alloc_type_tuple();
@@ -12858,29 +12886,29 @@ void lb_generate_code(lbGenerator *gen) {
TIME_SECTION("LLVM Function Pass");
for_array(i, m->procedures_to_generate) {
lbProcedure *p = m->procedures_to_generate[i];
if (p->body != nullptr) { // Build Procedure
for (i32 i = 0; i <= build_context.optimization_level; i++) {
if (p->flags & lbProcedureFlag_WithoutMemcpyPass) {
LLVMRunFunctionPassManager(default_function_pass_manager_without_memcpy, p->value);
} else {
LLVMRunFunctionPassManager(default_function_pass_manager, p->value);
{
for_array(i, m->procedures_to_generate) {
lbProcedure *p = m->procedures_to_generate[i];
if (p->body != nullptr) { // Build Procedure
for (i32 i = 0; i <= build_context.optimization_level; i++) {
if (p->flags & lbProcedureFlag_WithoutMemcpyPass) {
LLVMRunFunctionPassManager(default_function_pass_manager_without_memcpy, p->value);
} else {
LLVMRunFunctionPassManager(default_function_pass_manager, p->value);
}
}
}
}
}
for_array(i, m->equal_procs.entries) {
lbProcedure *p = m->equal_procs.entries[i].value;
LLVMRunFunctionPassManager(default_function_pass_manager, p->value);
for_array(i, m->equal_procs.entries) {
lbProcedure *p = m->equal_procs.entries[i].value;
LLVMRunFunctionPassManager(default_function_pass_manager, p->value);
}
for_array(i, m->hasher_procs.entries) {
lbProcedure *p = m->hasher_procs.entries[i].value;
LLVMRunFunctionPassManager(default_function_pass_manager, p->value);
}
}
for_array(i, m->hasher_procs.entries) {
lbProcedure *p = m->hasher_procs.entries[i].value;
LLVMRunFunctionPassManager(default_function_pass_manager, p->value);
}
TIME_SECTION("LLVM Module Pass");
@@ -12889,12 +12917,12 @@ void lb_generate_code(lbGenerator *gen) {
LLVMAddAlwaysInlinerPass(module_pass_manager);
LLVMAddStripDeadPrototypesPass(module_pass_manager);
LLVMAddAnalysisPasses(target_machine, module_pass_manager);
// if (build_context.optimization_level >= 2) {
// LLVMAddArgumentPromotionPass(module_pass_manager);
// LLVMAddConstantMergePass(module_pass_manager);
// LLVMAddGlobalDCEPass(module_pass_manager);
// LLVMAddDeadArgEliminationPass(module_pass_manager);
// }
if (build_context.optimization_level >= 2) {
LLVMAddArgumentPromotionPass(module_pass_manager);
LLVMAddConstantMergePass(module_pass_manager);
LLVMAddGlobalDCEPass(module_pass_manager);
LLVMAddDeadArgEliminationPass(module_pass_manager);
}
LLVMPassManagerBuilderRef pass_manager_builder = LLVMPassManagerBuilderCreate();
defer (LLVMPassManagerBuilderDispose(pass_manager_builder));
+1
View File
@@ -363,6 +363,7 @@ lbValue lb_find_or_add_entity_string(lbModule *m, String const &str);
lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent = nullptr);
bool lb_is_const(lbValue value);
bool lb_is_const_or_global(lbValue value);
bool lb_is_const_nil(lbValue value);
String lb_get_const_string(lbModule *m, lbValue value);
+1 -1
View File
@@ -2216,7 +2216,7 @@ int main(int arg_count, char const **arg_ptr) {
if (build_context.cross_compiling && selected_target_metrics->metrics == &target_essence_amd64) {
#ifdef GB_SYSTEM_UNIX
system_exec_command_line_app("linker", "x86_64-essence-gcc \"%.*s.o\" -o \"%.*s\" %.*s %.*s", // -ffreestanding -nostdlib
system_exec_command_line_app("linker", "x86_64-essence-gcc -ffreestanding -nostdlib \"%.*s.o\" -o \"%.*s\" %.*s %.*s",
LIT(output_base), LIT(output_base), LIT(build_context.link_flags), LIT(build_context.extra_linker_flags));
#else
gb_printf_err("Linking for cross compilation for this platform is not yet supported (%.*s %.*s)\n",