general bugfixes; strings are now a pointer and a length
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 22 Aug 2020 22:18:27 +0000 (17:18 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 22 Aug 2020 22:18:27 +0000 (17:18 -0500)
16 files changed:
Makefile
core/builtin.onyx [new file with mode: 0644]
core/string.onyx
core/wasi.onyx
include/onyxastnodes.h
include/onyxutils.h
include/onyxwasm.h
onyx
progs/wasi_test.onyx
src/onyx.c
src/onyxbuiltins.c
src/onyxchecker.c
src/onyxparser.c
src/onyxsymres.c
src/onyxutils.c
src/onyxwasm.c

index 740b434b32fd974a9aa5534e59995f57eb0a3365..f92c3aa4281e6993512ef183bc0a6e7552e7d3e8 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -21,7 +21,7 @@ TARGET=./onyx
 ifeq ($(RELEASE), 1)
        FLAGS=-O2
 else
-       FLAGS=-g -O2
+       FLAGS=-g
 endif
 
 build/%.o: src/%.c include/bh.h
diff --git a/core/builtin.onyx b/core/builtin.onyx
new file mode 100644 (file)
index 0000000..6a7d9b7
--- /dev/null
@@ -0,0 +1,8 @@
+package builtin
+
+string :: struct {
+       data : ^u8;
+       len  : u32;     
+}
+
+cstring :: #type ^u8;
\ No newline at end of file
index 50a21994ef35a420c46ef523264fe9d4ac6fed8e..8f58d66b321de4011fe00ba90a2e0d56ccc1a69e 100644 (file)
@@ -1,5 +1,6 @@
 package core
 
+use package builtin { string }
 use package memory
 
 Buffer :: struct {
@@ -7,18 +8,13 @@ Buffer :: struct {
     length : u32    = 0;
 }
 
-string :: struct {
-    length : u32 = 0;
-    data   : ^u8 = null;
-}
-
 string_make :: proc #overloaded { string_make_from_u8 }
 
 #private
 string_make_from_u8 :: proc (s: ^u8) -> string {
     len :: string_length(s);
 
-    return string.{ length = len, data = s };
+    return string.{ len = len, data = s };
 }
 
 
@@ -35,12 +31,12 @@ string_length :: proc #overloaded {
     },
 
     proc (s: string) -> u32 {
-        return s.length;
+        return s.len;
     },
 }
 
 #private
-string_length_string :: proc (s: string) -> u32 do return s.length;
+string_length_string :: proc (s: string) -> u32 do return s.len;
 
 string_concat :: proc (a: Allocator, s1: string, s2: string) -> string {
     len1 :: string_length(s1);
@@ -50,7 +46,7 @@ string_concat :: proc (a: Allocator, s1: string, s2: string) -> string {
     for i: 0, len1 do data[i]        = s1.data[i];
     for i: 0, len2 do data[i + len1] = s2.data[i];
 
-    return string.{ len1 + len2, data };
+    return string.{ data, len1 + len2 };
 }
 
 string_free :: proc (a: Allocator, s: string) do free(a, s.data);
index 96908f0f1f5ebca82e6893aaee834f7f96ab4778..d26d9e967eefad8ac0e368a472bf5f4f90f75540 100644 (file)
@@ -349,65 +349,65 @@ IOVecArray :: struct {
 
 
 // FUNCTIONS
-args_get       :: proc #foreign "wasi_unstable" "args_get" (argv: ^^u8, argv_buf: ^u8) -> Errno ---
-args_sizes_get :: proc #foreign "wasi_unstable" "args_sizes_get" (argc: ^Size, argv_buf_size: ^Size) -> Errno ---
-
-environ_get       :: proc #foreign "wasi_unstable" "environ_get" (environ: ^^u8, environ_buf: ^u8) -> Errno ---
-environ_sizes_get :: proc #foreign "wasi_unstable" "environ_sizes_get" (environc: ^Size, environ_buf_size: ^Size) -> Errno ---
-
-clock_res_get  :: proc #foreign "wasi_unstable" "clock_res_get" (id: ClockID, resolution: ^Timestamp) -> Errno ---
-clock_time_get :: proc #foreign "wasi_unstable" "clock_time_get" (id: ClockID, precision: Timestamp, time: ^Timestamp) -> Errno ---
-
-fd_advise             :: proc #foreign "wasi_unstable" "fd_advise" (fd: FileDescriptor, offset: Filesize, len: Filesize, advice: Advice) -> Errno ---
-fd_allocate           :: proc #foreign "wasi_unstable" "fd_allocate" (fd: FileDescriptor, offset: Filesize, len: Filesize) -> Errno ---
-fd_close              :: proc #foreign "wasi_unstable" "fd_close" (fd: FileDescriptor) -> Errno ---
-fd_datasync           :: proc #foreign "wasi_unstable" "fd_datasync" (fd: FileDescriptor) -> Errno ---
-fd_fdstat_get         :: proc #foreign "wasi_unstable" "fd_fdstat_get" (fd: FileDescriptor, stat: ^FDStat) -> Errno ---
-fd_fdstat_set_flags   :: proc #foreign "wasi_unstable" "fd_fdstat_set_flags" (fd: FileDescriptor, flags: FDFlags) -> Errno ---
-fd_fdstat_set_rights  :: proc #foreign "wasi_unstable" "fd_fdstat_set_rights" (fd: FileDescriptor, rights_base: Rights, rights_inheriting: Rights) -> Errno ---
-fd_filestat_get       :: proc #foreign "wasi_unstable" "fd_filestat_get" (fd: FileDescriptor, buf: ^FileStat) -> Errno ---
-fd_filestat_set_size  :: proc #foreign "wasi_unstable" "fd_filestat_set_size" (fd: FileDescriptor, size: Filesize) -> Errno ---
-fd_filestat_set_times :: proc #foreign "wasi_unstable" "fd_filestat_set_times" (fd: FileDescriptor, atim: Timestamp, mtim: Timestamp, fst_flags: FSTFlags) -> Errno ---
+args_get       :: proc #foreign "wasi_snapshot_preview1" "args_get" (argv: ^^u8, argv_buf: ^u8) -> Errno ---
+args_sizes_get :: proc #foreign "wasi_snapshot_preview1" "args_sizes_get" (argc: ^Size, argv_buf_size: ^Size) -> Errno ---
+
+environ_get       :: proc #foreign "wasi_snapshot_preview1" "environ_get" (environ: ^^u8, environ_buf: ^u8) -> Errno ---
+environ_sizes_get :: proc #foreign "wasi_snapshot_preview1" "environ_sizes_get" (environc: ^Size, environ_buf_size: ^Size) -> Errno ---
+
+clock_res_get  :: proc #foreign "wasi_snapshot_preview1" "clock_res_get" (id: ClockID, resolution: ^Timestamp) -> Errno ---
+clock_time_get :: proc #foreign "wasi_snapshot_preview1" "clock_time_get" (id: ClockID, precision: Timestamp, time: ^Timestamp) -> Errno ---
+
+fd_advise             :: proc #foreign "wasi_snapshot_preview1" "fd_advise" (fd: FileDescriptor, offset: Filesize, len: Filesize, advice: Advice) -> Errno ---
+fd_allocate           :: proc #foreign "wasi_snapshot_preview1" "fd_allocate" (fd: FileDescriptor, offset: Filesize, len: Filesize) -> Errno ---
+fd_close              :: proc #foreign "wasi_snapshot_preview1" "fd_close" (fd: FileDescriptor) -> Errno ---
+fd_datasync           :: proc #foreign "wasi_snapshot_preview1" "fd_datasync" (fd: FileDescriptor) -> Errno ---
+fd_fdstat_get         :: proc #foreign "wasi_snapshot_preview1" "fd_fdstat_get" (fd: FileDescriptor, stat: ^FDStat) -> Errno ---
+fd_fdstat_set_flags   :: proc #foreign "wasi_snapshot_preview1" "fd_fdstat_set_flags" (fd: FileDescriptor, flags: FDFlags) -> Errno ---
+fd_fdstat_set_rights  :: proc #foreign "wasi_snapshot_preview1" "fd_fdstat_set_rights" (fd: FileDescriptor, rights_base: Rights, rights_inheriting: Rights) -> Errno ---
+fd_filestat_get       :: proc #foreign "wasi_snapshot_preview1" "fd_filestat_get" (fd: FileDescriptor, buf: ^FileStat) -> Errno ---
+fd_filestat_set_size  :: proc #foreign "wasi_snapshot_preview1" "fd_filestat_set_size" (fd: FileDescriptor, size: Filesize) -> Errno ---
+fd_filestat_set_times :: proc #foreign "wasi_snapshot_preview1" "fd_filestat_set_times" (fd: FileDescriptor, atim: Timestamp, mtim: Timestamp, fst_flags: FSTFlags) -> Errno ---
 fd_pread ::
-       proc #foreign "wasi_unstable" "fd_pread"
+       proc #foreign "wasi_snapshot_preview1" "fd_pread"
        (fd: FileDescriptor, iovs: IOVecArray, offset: Filesize, nread: ^Size) -> Errno ---
-fd_prestat_get :: proc #foreign "wasi_unstable" "fd_prestat_get" (fd: FileDescriptor, buf: ^PrestatTagged) -> Errno ---
-fd_prestat_dir_name :: proc #foreign "wasi_unstable" "fd_prestat_dir_name" (fd: FileDescriptor, path: ^u8, path_len: Size) -> Errno ---
+fd_prestat_get :: proc #foreign "wasi_snapshot_preview1" "fd_prestat_get" (fd: FileDescriptor, buf: ^PrestatTagged) -> Errno ---
+fd_prestat_dir_name :: proc #foreign "wasi_snapshot_preview1" "fd_prestat_dir_name" (fd: FileDescriptor, path: ^u8, path_len: Size) -> Errno ---
 fd_pwrite ::
-       proc #foreign "wasi_unstable" "fd_pwrite"
+       proc #foreign "wasi_snapshot_preview1" "fd_pwrite"
        (fd: FileDescriptor, iovs: IOVecArray, offset: Filesize, nwritten: ^Size) -> Errno ---
 fd_read ::
-       proc #foreign "wasi_unstable" "fd_read"
+       proc #foreign "wasi_snapshot_preview1" "fd_read"
        (fd: FileDescriptor, iovs: IOVecArray, nread: ^Size) -> Errno ---
 fd_readdir ::
-       proc #foreign "wasi_unstable" "fd_readdir"
+       proc #foreign "wasi_snapshot_preview1" "fd_readdir"
        (fd: FileDescriptor, buf: ^u8, buf_len: Size, cookie: DirCookie, bufused: ^Size) -> Errno ---
 fd_renumber ::
-       proc #foreign "wasi_unstable" "fd_renumber"
+       proc #foreign "wasi_snapshot_preview1" "fd_renumber"
        (fd: FileDescriptor, to: FileDescriptor) -> Errno ---
 fd_seek ::
-       proc #foreign "wasi_unstable" "fd_seek"
+       proc #foreign "wasi_snapshot_preview1" "fd_seek"
        (fd: FileDescriptor, offset: FileDelta, whence: Whence, newoffset: ^Filesize) -> Errno ---
-fd_sync :: proc #foreign "wasi_unstable" "fd_sync" (fd: FileDescriptor) -> Errno ---
-fd_tell :: proc #foreign "wasi_unstable" "fd_tell" (fd: FileDescriptor, offset: ^Filesize) -> Errno ---
+fd_sync :: proc #foreign "wasi_snapshot_preview1" "fd_sync" (fd: FileDescriptor) -> Errno ---
+fd_tell :: proc #foreign "wasi_snapshot_preview1" "fd_tell" (fd: FileDescriptor, offset: ^Filesize) -> Errno ---
 fd_write ::
-       proc #foreign "wasi_unstable" "fd_write"
+       proc #foreign "wasi_snapshot_preview1" "fd_write"
        (fd: FileDescriptor, iovs: IOVecArray, nwritten: ^Size) -> Errno ---
 
 path_create_directory ::
-       proc #foreign "wasi_unstable" "path_create_directory"
+       proc #foreign "wasi_snapshot_preview1" "path_create_directory"
        (fd: FileDescriptor, path: ^u8, path_len: Size) -> Errno ---
 path_filestat_get ::
-       proc #foreign "wasi_unstable" "path_filestat_get"
+       proc #foreign "wasi_snapshot_preview1" "path_filestat_get"
        (fd: FileDescriptor, flags: LookupFlags, path: ^u8, path_len: Size, buf: ^FileStat) -> Errno ---
 path_filestat_set_times ::
-       proc #foreign "wasi_unstable" "path_filestat_set_times"
+       proc #foreign "wasi_snapshot_preview1" "path_filestat_set_times"
        (fd: FileDescriptor, flags: LookupFlags, path: ^u8, path_len: Size, atim: Timestamp, mtim: Timestamp, fst_flags: FSTFlags) -> Errno ---
 path_link ::
-       proc #foreign "wasi_unstable" "path_link"
+       proc #foreign "wasi_snapshot_preview1" "path_link"
        (fd: FileDescriptor, old_flags: LookupFlags, old_path: ^u8, old_path_len: Size, new_fd: FileDescriptor, new_path: ^u8, new_path_len: Size) -> Errno --- 
 path_open ::
-       proc #foreign "wasi_unstable" "path_open"
+       proc #foreign "wasi_snapshot_preview1" "path_open"
        ( fd: FileDescriptor
        , dirflags: LookupFlags
        , path: ^u8
@@ -419,38 +419,38 @@ path_open ::
        , opened_fd: ^FileDescriptor
        ) -> Errno ---
 path_readlink ::
-       proc #foreign "wasi_unstable" "path_readlink"
+       proc #foreign "wasi_snapshot_preview1" "path_readlink"
        (fd: FileDescriptor, path: ^u8, path_len: Size, buf: ^u8, buf_len: Size, bufused: ^Size) -> Errno ---
 path_remove_directory ::
-       proc #foreign "wasi_unstable" "path_remove_directory"
+       proc #foreign "wasi_snapshot_preview1" "path_remove_directory"
        (fd: FileDescriptor, path: ^u8, path_len: Size) -> Errno ---
 path_rename ::
-       proc #foreign "wasi_unstable" "path_rename"
+       proc #foreign "wasi_snapshot_preview1" "path_rename"
        (fd: FileDescriptor, old_path: ^u8, old_path_len: Size, new_fd: FileDescriptor, new_path: ^u8, new_path_len: Size) -> Errno ---
 path_symlink ::
-       proc #foreign "wasi_unstable" "path_symlink"
+       proc #foreign "wasi_snapshot_preview1" "path_symlink"
        (old_path: ^u8, old_path_len: Size, fd: FileDescriptor, new_path: ^u8, new_path_len: Size) -> Errno ---
 path_unlink_file ::
-       proc #foreign "wasi_unstable" "path_unlink_file"
+       proc #foreign "wasi_snapshot_preview1" "path_unlink_file"
        (fd: FileDescriptor, path: ^u8, path_len: Size) -> Errno ---
 
 poll_oneoff ::
-       proc #foreign "wasi_unstable" "poll_oneoff"
+       proc #foreign "wasi_snapshot_preview1" "poll_oneoff"
        (in: ^Subscription, out: ^Event, nsubscriptions: Size, nevents: ^Size) -> Errno ---
 
-proc_exit :: proc #foreign "wasi_unstable" "proc_exit" (rval: ExitCode) ---
-proc_raise :: proc #foreign "wasi_unstable" "proc_raise" (sig: Signal) -> Errno ---
+proc_exit  :: proc #foreign "wasi_snapshot_preview1" "proc_exit" (rval: ExitCode) ---
+proc_raise :: proc #foreign "wasi_snapshot_preview1" "proc_raise" (sig: Signal) -> Errno ---
 
-sched_yield :: proc #foreign "wasi_unstable" "sched_yield" -> Errno ---
+sched_yield :: proc #foreign "wasi_snapshot_preview1" "sched_yield" -> Errno ---
 
-random_get :: proc #foreign "wasi_unstable" "random_get" (buf: ^u8, buf_len: Size) -> Errno ---
+random_get :: proc #foreign "wasi_snapshot_preview1" "random_get" (buf: ^u8, buf_len: Size) -> Errno ---
 
 sock_recv ::
-       proc #foreign "wasi_unstable" "sock_recv"
+       proc #foreign "wasi_snapshot_preview1" "sock_recv"
        (fd: FileDescriptor, ri_data: IOVecArray, ri_flags: RIFlags, ro_datalen: ^Size, ro_flags: ^ROFlags) -> Errno ---
 sock_send ::
-       proc #foreign "wasi_unstable" "sock_send"
+       proc #foreign "wasi_snapshot_preview1" "sock_send"
        (fd: FileDescriptor, si_data: IOVecArray, si_flags: SIFlags, so_datalen: ^Size) -> Errno ---
 sock_shutdown ::
-       proc #foreign "wasi_unstable" "sock_shutdown"
+       proc #foreign "wasi_snapshot_preview1" "sock_shutdown"
        (fd: FileDescriptor, how: SDFlags) -> Errno ---
index b965794c73452c44f29511fbf48829b84e692b59..aefd7c78e2915a85c0247049e76da709f84b4781 100644 (file)
@@ -280,7 +280,7 @@ struct AstTyped AstTyped_members;
 struct AstBinOp         { AstTyped_base; BinaryOp operation; AstTyped *left, *right; };
 struct AstUnaryOp       { AstTyped_base; UnaryOp operation; AstTyped *expr; };
 struct AstNumLit        { AstTyped_base; union { i32 i; i64 l; f32 f; f64 d; } value; };
-struct AstStrLit        { AstTyped_base; u64 addr; };
+struct AstStrLit        { AstTyped_base; u64 addr; u64 length; };
 struct AstLocal         { AstTyped_base; AstLocal *prev_local; };
 struct AstCall          { AstTyped_base; AstArgument *arguments; u64 arg_count; AstTyped *callee; };
 struct AstIntrinsicCall { AstTyped_base; AstArgument *arguments; u64 arg_count; OnyxIntrinsic intrinsic; };
@@ -509,6 +509,7 @@ extern AstBasicType basic_type_rawptr;
 
 extern AstNumLit builtin_heap_start;
 extern AstGlobal builtin_stack_top;
+extern AstType  *builtin_string_type;
 
 typedef struct BuiltinSymbol {
     char*    package;
@@ -518,7 +519,7 @@ typedef struct BuiltinSymbol {
 
 extern const BuiltinSymbol builtin_symbols[];
 
-void initialize_builtins();
+void initialize_builtins(bh_allocator a, ProgramInfo* prog);
 
 
 // NOTE: Useful not inlined functions
index 70bdb32d3fb90b0a9e3ccee3d74ceda273b53c18..4e2b1f4316f409a81c1be8e68b57c890b0a57312 100644 (file)
@@ -19,6 +19,7 @@ void scope_include(Scope* target, Scope* source);
 b32 symbol_introduce(Scope* scope, OnyxToken* tkn, AstNode* symbol);
 b32 symbol_raw_introduce(Scope* scope, char* tkn, OnyxFilePos pos, AstNode* symbol);
 void symbol_builtin_introduce(Scope* scope, char* sym, AstNode *node);
+AstNode* symbol_raw_resolve(Scope* start_scope, char* sym);
 AstNode* symbol_resolve(Scope* start_scope, OnyxToken* tkn);
 
 void onyx_ast_print(AstNode* program, i32 indent);
index c448a30330fb5ed4abe30acd909c94a565bd51e4..2b6d97610e0af2e64063e94c06e1ed1246e93824 100644 (file)
@@ -294,6 +294,11 @@ typedef struct DeferredStmt {
     AstNode *stmt;
 } DeferredStmt;
 
+typedef struct StrLitInfo {
+    u32 addr;
+    u32 len;
+} StrLitInfo;
+
 typedef struct OnyxWasmModule {
     bh_allocator allocator;
 
@@ -316,7 +321,7 @@ typedef struct OnyxWasmModule {
     bh_table(i32) type_map;
 
     bh_table(u32) loaded_file_offsets;
-    bh_table(u32) string_literals;
+    bh_table(StrLitInfo) string_literals;
 
     bh_arr(u8) structured_jump_target;
 
diff --git a/onyx b/onyx
index 7f0459b0fc8b914e0ae629d58ef4b629bb8f8178..54e07f57d9affbbe60a5d45bcb1976b995835fe2 100755 (executable)
Binary files a/onyx and b/onyx differ
index 92f35ba30d30ed06c9e785d99ac64f60121fa9b0..9f4f7c3645faa3c7e0f252b279679097c97c93c8 100644 (file)
@@ -1,11 +1,8 @@
 package main
 
-// WASI is still crap and I cannot get file opening to work.
-// Will try again in 5 years or so when they can finally make
-// that basic and necessary feature work.
-
 #include_folder "/usr/share/onyx/core"
 
+#include_file "builtin"
 #include_file "wasi"
 #include_file "alloc"
 #include_file "intrinsics"
@@ -16,6 +13,7 @@ use package builtin
 use package core
 use package memory
 use package wasi
+use package intrinsics
 
 print_u64_with_base :: proc (n_: u64, base: u64) {
     n := n_;
@@ -26,6 +24,8 @@ print_u64_with_base :: proc (n_: u64, base: u64) {
     *c = #char "\0";
     c -= 1;
 
+    s :: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
+
     if n == 0l {
         *c = #char "0";
         c -= 1;
@@ -33,27 +33,7 @@ print_u64_with_base :: proc (n_: u64, base: u64) {
         while n > 0l {
             m :: n % base;
 
-            ch := cast(u8) 0;
-
-            // TODO: Replace with array lookup when array literals are added
-            if     m == 0l  do ch = #char "0";
-            elseif m == 1l  do ch = #char "1";
-            elseif m == 2l  do ch = #char "2";
-            elseif m == 3l  do ch = #char "3";
-            elseif m == 4l  do ch = #char "4";
-            elseif m == 5l  do ch = #char "5";
-            elseif m == 6l  do ch = #char "6";
-            elseif m == 7l  do ch = #char "7";
-            elseif m == 8l  do ch = #char "8";
-            elseif m == 9l  do ch = #char "9";
-            elseif m == 10l do ch = #char "A";
-            elseif m == 11l do ch = #char "B";
-            elseif m == 12l do ch = #char "C";
-            elseif m == 13l do ch = #char "D";
-            elseif m == 14l do ch = #char "E";
-            elseif m == 15l do ch = #char "F";
-
-            *c = ch;
+            *c = s.data[cast(u32) m];
             c -= 1;
 
             n /= base;
@@ -78,13 +58,13 @@ print_u64_with_base :: proc (n_: u64, base: u64) {
 }
 
 print_string :: proc (s: string) {
-       vec := IOVec.{ buf = s.data, len = s.length };
+       vec := IOVec.{ buf = s.data, len = s.len };
        tmp : Size;     
        fd_write(1, IOVecArray.{ ^vec, 1 }, ^tmp);
        fd_datasync(1);
 }
 
-print_u8 :: proc (s: ^u8) {
+print_u8 :: proc (s: cstring) {
        string_make(s) |> print_string();
 }
 
@@ -92,6 +72,7 @@ print :: proc #overloaded { print_string, print_u8 }
 
 print_rights :: proc (rights: Rights) {
        print_u64_with_base(cast(u64) rights, 2l);
+       print("\n");
 
        if rights & Rights.DataSync != cast(Rights) 0 do print("DataSync\n");
        if rights & Rights.Read != cast(Rights) 0 do print("Read\n");
@@ -132,9 +113,42 @@ readline :: proc (buf: ^u8, bufsize: u32) -> u32 {
        return nread;
 }
 
+readdir :: proc (fd: FileDescriptor) {
+       buf : [1024] u8;
+       bufused : Size;
+
+       if fd_readdir(fd, cast(^u8) buf, 1024, cast(DirCookie) 0, ^bufused) != Errno.Success {
+               print("Failed to readdir\n");
+               return;
+       }
+
+       dirent := cast(^DirEnt) buf;
+       while true {
+               print(string.{ cast(^u8) (cast(u32) dirent + sizeof DirEnt), dirent.d_namlen });
+               print("\n");
+
+               print("\td_namlen: ");
+               print_u64_with_base(cast(u64) dirent.d_namlen, 16l);
+               print("\n");
+               print("\td_type: ");
+               print_u64_with_base(cast(u64) dirent.d_type, 16l);
+               print("\n");
+
+               bufused -= sizeof DirEnt + dirent.d_namlen;
+               dirent = cast(^DirEnt) (cast(u32) dirent + sizeof DirEnt + dirent.d_namlen);
+
+               if bufused <= 0 do break;
+       }
+}
+
 main :: proc #export "_start" {
        memory_init();
 
+       curr_time: Timestamp;
+       clock_time_get(ClockID.Realtime, cast(Timestamp) 1, ^curr_time);
+       print_u64_with_base(cast(u64) curr_time, 10l);
+       print("\n");
+
        print("Hello World!\n");
        print_u64_with_base(cast(u64) 0x624abd, 16l);
        print("\n");
@@ -145,90 +159,44 @@ main :: proc #export "_start" {
 
        fd : FileDescriptor;
        err := path_open(3,
-               cast(LookupFlags) 0,
-               "./foo.txt", 9,
-               OFlags.Creat,
-               Rights.DataSync | Rights.Write | Rights.Read | Rights.Seek | Rights.Tell | Rights.Advise | Rights.PathOpen | Rights.PathCreateFile | Rights.Seek,
-               Rights.DataSync | Rights.Write | Rights.Read | Rights.Seek | Rights.Tell | Rights.Advise | Rights.PathOpen | Rights.PathCreateFile | Rights.Seek,
-               FDFlags.Sync | FDFlags.Append,
+               LookupFlags.SymLinkFollow,
+               "./src/onyxmsgs.c".data, 16,
+               cast(OFlags) 0,
+               Rights.DataSync | Rights.Write | Rights.Read | Rights.Tell | Rights.Seek | Rights.Advise | Rights.PathOpen | Rights.PathCreateFile,
+               Rights.DataSync | Rights.Write | Rights.Read | Rights.Tell | Rights.Seek | Rights.Advise | Rights.PathOpen | Rights.PathCreateFile,
+               FDFlags.Sync,
                ^fd);
 
        if err != Errno.Success {
                print("Failed to open file\n");
-               proc_exit(cast(u32) err);
+               print("Error code: ");
+               print_u64_with_base(cast(u64) err, 16l);
+               proc_exit(1);
        }
 
        print_u64_with_base(cast(u64) fd, 16l);
        print("\n");
 
-       hello_vec := IOVec.{ buf = "Hello World!\n", len = 13 };
-       nwritten : Size;
-       if fd_write(fd, IOVecArray.{ ^hello_vec, 1 }, ^nwritten) != Errno.Success {
-               print("Failed to write to file\n");
-               proc_exit(cast(u32) 123);
-               return;
+       filelen : Filesize;
+       if fd_seek(fd, 0l, Whence.End, ^filelen) != Errno.Success {
+               print("Failed to seek in file\n");
+               proc_exit(1);
        }
+       print("the size is: ");
+       print_u64_with_base(cast(u64) filelen, 10l);
+
+       //buf : [128] u8;
+       //hello_vec := IOVec.{ buf = cast(^u8) buf, len = 128 };
+       //nread : Size;
+       //if fd_read(fd, IOVecArray.{ ^hello_vec, 1 }, ^nread) != Errno.Success {
+       //      print("Failed to read from file\n");
+       //      print("Error code: ");
+       //      print_u64_with_base(cast(u64) err, 10l);
+       //      proc_exit(1);
+       //      return;
+       //}
+//
+       //print(string.{ nread, cast(^u8) buf });
 
        fd_close(fd);
-//
-//     if false {
-//             argc_size : Size;
-//             argv_size : Size;
-//             args_sizes_get(^argc_size, ^argv_size);
-//             print_u64_with_base(cast(u64) argc_size, 10l);
-//             print("\n");
-//             print_u64_with_base(cast(u64) argv_size, 10l);
-//             print("\n");
-//
-//             argv : ^u8;
-//             argv_buf := cast(^u8) malloc(argv_size);
-//             args_get(^argv, argv_buf);
-//             print(argv);
-//
-//             fs : FDStat;
-//             new_fd : FileDescriptor;
-//             print_u64_with_base(cast(u64) new_fd, 16l);
-//
-//             err := path_open(3,
-//                             cast(LookupFlags) 0,
-//                             "./Makefile", 10,
-//                             cast(OFlags) 0,
-//                             Rights.Read | Rights.Sync,
-//                             Rights.Read | Rights.Sync,
-//                             cast(FDFlags) 0,
-//                             ^new_fd);
-//
-//             print_u64_with_base(cast(u64) new_fd, 16l);
-//
-//             if err != Errno.Success {
-//                     print("Failed to open file\n");
-//                     proc_exit(cast(u32) err);
-//                     return;
-//             }
-//
-//             fd_fdstat_get(new_fd, ^fs);
-//             print("Rights base:\n");
-//             print_rights(fs.fs_rights_base);
-//             print("Rights inheriting:\n");
-//             print_rights(fs.fs_rights_inheriting);
-//
-//             buf : [128] u8;
-//             read_iov := IOVec.{ cast(^u8) buf, 128 };
-//             nread : Size;
-//             fd_read(new_fd, IOVecArray.{ ^read_iov, 1 }, ^nread);
-//
-//             print(string.{nread, cast(^u8) buf});
-//
-//             //hello_vec := IOVec.{ buf = "Hello World!\n", len = 13 };
-//             //nwritten : Size;
-//             //if fd_write(new_fd, IOVecArray.{ ^hello_vec, 1 }, ^nwritten) != Errno.Success {
-//             //      print("Failed to write to file\n");
-//             //      proc_exit(cast(u32) 123);
-//             //      return;
-//             //}
-//             //print_u64_with_base(cast(u64) nwritten, 10l);
-//             //fd_datasync(1);
-//             //fd_datasync(new_fd);
-//             //fd_close(new_fd);
-//     }
 }
\ No newline at end of file
index 9608963a406dce11fe59c451ab4e4971d0803f7f..62a8cc7e9f5afbc67b6935cb0ec74fff84a75639 100644 (file)
@@ -350,7 +350,10 @@ static i32 onyx_compile(CompilerState* compiler_state) {
         bh_arr_fastdelete(compiler_state->queued_files, 0);
     }
 
-    initialize_builtins();
+    initialize_builtins(compiler_state->ast_alloc, &compiler_state->prog_info);
+    if (onyx_message_has_errors()) {
+        return ONYX_COMPILER_PROGRESS_FAILED_SEMPASS;
+    }
 
     // Add builtin one-time entities
     bh_arr_push(compiler_state->prog_info.entities, ((Entity) {
index e4a8d423d6c02eb272e6430e55d3d8cccb4d62f1..77e1972e1682b86ff782cf61e633d538722c1491 100644 (file)
@@ -1,5 +1,7 @@
 #include "onyxastnodes.h"
 #include "onyxtypes.h"
+#include "onyxmsgs.h"
+#include "onyxutils.h"
 
 AstBasicType basic_type_void   = { { Ast_Kind_Basic_Type, 0, NULL, "void"   }, &basic_types[Basic_Kind_Void]  };
 AstBasicType basic_type_bool   = { { Ast_Kind_Basic_Type, 0, NULL, "bool"   }, &basic_types[Basic_Kind_Bool]  };
@@ -17,8 +19,9 @@ AstBasicType basic_type_rawptr = { { Ast_Kind_Basic_Type, 0, NULL, "rawptr" }, &
 
 static OnyxToken builtin_heap_start_token = { Token_Type_Symbol, 12, "__heap_start ", { 0 } };
 static OnyxToken builtin_stack_top_token  = { Token_Type_Symbol, 11, "__stack_top ",  { 0 } };
-AstNumLit builtin_heap_start = { Ast_Kind_NumLit, Ast_Flag_Const, &builtin_heap_start_token, NULL, (AstType *) &basic_type_rawptr, NULL, 0 };
-AstGlobal builtin_stack_top  = { Ast_Kind_Global, Ast_Flag_Const | Ast_Flag_Global_Stack_Top,  &builtin_stack_top_token,  NULL, (AstType *) &basic_type_rawptr, NULL };
+AstNumLit builtin_heap_start  = { Ast_Kind_NumLit, Ast_Flag_Const, &builtin_heap_start_token, NULL, (AstType *) &basic_type_rawptr, NULL, 0 };
+AstGlobal builtin_stack_top   = { Ast_Kind_Global, Ast_Flag_Const | Ast_Flag_Global_Stack_Top,  &builtin_stack_top_token,  NULL, (AstType *) &basic_type_rawptr, NULL };
+AstType  *builtin_string_type;
 
 const BuiltinSymbol builtin_symbols[] = {
     { NULL, "void",       (AstNode *) &basic_type_void },
@@ -41,5 +44,30 @@ const BuiltinSymbol builtin_symbols[] = {
     { NULL, NULL, NULL },
 };
 
-void initialize_builtins() {
+void initialize_builtins(bh_allocator a, ProgramInfo* prog) {
+    BuiltinSymbol* bsym = (BuiltinSymbol *) &builtin_symbols[0];
+    while (bsym->sym != NULL) {
+        if (bsym->package == NULL)
+            symbol_builtin_introduce(prog->global_scope, bsym->sym, bsym->node);
+        else {
+            Package* p = program_info_package_lookup_or_create(
+                    prog,
+                    bsym->package,
+                    prog->global_scope,
+                    a);
+            assert(p);
+
+            symbol_builtin_introduce(p->scope, bsym->sym, bsym->node);
+        }
+        bsym++;
+    }
+
+    Package* p = program_info_package_lookup_or_create(prog, "builtin", prog->global_scope, a);
+    builtin_string_type = (AstType *) symbol_raw_resolve(p->scope, "string");
+    if (builtin_string_type == NULL) {
+        onyx_message_add(Msg_Type_Literal,
+                (OnyxFilePos) { 0 },
+                "'string' struct not found in builtin package");
+        return;
+    }
 }
\ No newline at end of file
index b3d2f7448422b580369eb6857646fc4d25801c4d..789f2405a95c7cff5e9048a6a0732c7b4a411e0f 100644 (file)
@@ -718,10 +718,10 @@ CHECK(array_access, AstArrayAccess* aa) {
     }
 
     if (aa->expr->type->kind != Type_Kind_Basic
-            || (aa->expr->type->Basic.flags & Basic_Flag_Integer) == 0) {
+            || (aa->expr->type->Basic.kind != Basic_Kind_I32 && aa->expr->type->Basic.kind != Basic_Kind_U32)) {
         onyx_message_add(Msg_Type_Literal,
                 aa->token->pos,
-                "expected integer type for index");
+                "expected 32-bit integer type for index");
         return 1;
     }
 
index e61e4b52f862ef08d7361ef923c99abc68bf66a4..2ae55a9162f16dbd08559e97c756c416831250e4 100644 (file)
@@ -366,13 +366,8 @@ static AstTyped* parse_factor(OnyxParser* parser) {
             break;
 
         case Token_Type_Literal_String: {
-            AstPointerType* str_type = make_node(AstPointerType, Ast_Kind_Pointer_Type);
-            str_type->flags |= Basic_Flag_Pointer;
-            str_type->elem = (AstType *) &basic_type_u8;
-
             AstStrLit* str_node = make_node(AstStrLit, Ast_Kind_StrLit);
             str_node->token     = expect_token(parser, Token_Type_Literal_String);
-            str_node->type_node = (AstType *) str_type;
             str_node->addr      = 0;
 
             add_node_to_process(parser, (AstNode *) str_node);
index 69a2fd46f9ea2b899680746052c2a7e7454141bd..66206ac5a14c243df629d5ff46507a364b8ecdbc 100644 (file)
@@ -300,9 +300,12 @@ static void symres_expression(AstTyped** expr) {
 
         case Ast_Kind_Function:
         case Ast_Kind_NumLit:
-        case Ast_Kind_StrLit:
             (*expr)->type_node = symres_type((*expr)->type_node);
             break;
+            
+        case Ast_Kind_StrLit:
+            (*expr)->type_node = symres_type(builtin_string_type);
+            break;
 
         case Ast_Kind_Array_Access:
             symres_expression(&((AstArrayAccess *)(*expr))->addr);
@@ -568,24 +571,6 @@ void onyx_resolve_symbols() {
 
     semstate.curr_scope = semstate.program->global_scope;
 
-    // NOTE: Add types to global scope
-    BuiltinSymbol* bsym = (BuiltinSymbol *) &builtin_symbols[0];
-    while (bsym->sym != NULL) {
-        if (bsym->package == NULL)
-            symbol_builtin_introduce(semstate.curr_scope, bsym->sym, bsym->node);
-        else {
-            Package* p = program_info_package_lookup_or_create(
-                    semstate.program,
-                    bsym->package,
-                    semstate.curr_scope,
-                    semstate.node_allocator);
-            assert(p);
-
-            symbol_builtin_introduce(p->scope, bsym->sym, bsym->node);
-        }
-        bsym++;
-    }
-
     bh_arr_each(Entity, entity, semstate.program->entities) {
         if (entity->package) {
             scope_enter(entity->package->private_scope);
index 766d0c46273f1466564b54213fe34d840df181cf..9733d625e570f644f64feda5276ca26e178fd73c 100644 (file)
@@ -157,34 +157,36 @@ void symbol_builtin_introduce(Scope* scope, char* sym, AstNode *node) {
     bh_table_put(AstNode *, scope->symbols, sym, node);
 }
 
-AstNode* symbol_resolve(Scope* start_scope, OnyxToken* tkn) {
-    token_toggle_end(tkn);
-
+AstNode* symbol_raw_resolve(Scope* start_scope, char* sym) {
     AstNode* res = NULL;
     Scope* scope = start_scope;
 
     while (res == NULL && scope != NULL) {
-        if (bh_table_has(AstNode *, scope->symbols, tkn->text)) {
-            res = bh_table_get(AstNode *, scope->symbols, tkn->text);
+        if (bh_table_has(AstNode *, scope->symbols, sym)) {
+            res = bh_table_get(AstNode *, scope->symbols, sym);
         } else {
             scope = scope->parent;
         }
     }
 
-    if (res == NULL) {
-        onyx_message_add(Msg_Type_Unknown_Symbol,
-                tkn->pos,
-                tkn->text);
-
-        token_toggle_end(tkn);
-        return &empty_node;
-    }
+    if (res == NULL) return NULL;
 
     if (res->kind == Ast_Kind_Symbol) {
-        token_toggle_end(tkn);
         return symbol_resolve(start_scope, res->token);
     }
 
+    return res;
+}
+
+AstNode* symbol_resolve(Scope* start_scope, OnyxToken* tkn) {
+    token_toggle_end(tkn);
+    AstNode* res = symbol_raw_resolve(start_scope, tkn->text);
+
+    if (res == NULL) {
+        onyx_message_add(Msg_Type_Unknown_Symbol, tkn->pos, tkn->text);
+        return &empty_node;
+    }
+
     token_toggle_end(tkn);
     return res;
 }
index e33b3789f829c1994f710a5a3fe07a5178f38526..0eec5286f66cd93b66962cdcc0aa5bc1606ed47d 100644 (file)
@@ -300,8 +300,11 @@ static u64 local_allocate(LocalAllocator* la, AstLocal* local) {
         u32 size = type_size_of(local->type);
         u32 alignment = type_alignment_of(local->type);
 
-        if (size % alignment != 0)
-            size += alignment - (size % alignment);
+        if (la->curr_stack % alignment != 0)
+            la->curr_stack += alignment - (la->curr_stack % alignment);
+
+        if (la->max_stack < la->curr_stack)
+            la->max_stack = la->curr_stack;
 
         if (la->max_stack - la->curr_stack >= size) {
             la->curr_stack += size;
@@ -1288,6 +1291,7 @@ COMPILE_FUNC(expression, AstTyped* expr) {
 
         case Ast_Kind_StrLit: {
             WID(WI_I32_CONST, ((AstStrLit *) expr)->addr);
+            WID(WI_I32_CONST, ((AstStrLit *) expr)->length);
             break;
         }
 
@@ -1395,6 +1399,22 @@ COMPILE_FUNC(expression, AstTyped* expr) {
                 break;
             }
 
+            if (field->expr->kind == Ast_Kind_StrLit) {
+                StructMember smem;
+
+                token_toggle_end(field->token);
+                type_struct_lookup_member(field->expr->type, field->token->text, &smem);
+                token_toggle_end(field->token);
+
+                if (smem.idx == 0)
+                    WID(WI_I32_CONST, ((AstStrLit *) field->expr)->addr);
+
+                if (smem.idx == 1)
+                    WID(WI_I32_CONST, ((AstStrLit *) field->expr)->length);
+                
+                break;
+            }
+
             u64 offset = 0;
             compile_field_access_location(mod, &code, field, &offset);
             compile_load_instruction(mod, &code, field->type, offset);
@@ -1903,10 +1923,11 @@ static void compile_string_literal(OnyxWasmModule* mod, AstStrLit* strlit) {
             *des++ = src[i];
         }
     }
-    *des++ = '\0';
 
-    if (bh_table_has(u32, mod->string_literals, (char *) strdata)) {
-        strlit->addr = bh_table_get(u32, mod->string_literals, (char *) strdata);
+    if (bh_table_has(StrLitInfo, mod->string_literals, (char *) strdata)) {
+        StrLitInfo sti = bh_table_get(StrLitInfo, mod->string_literals, (char *) strdata);
+        strlit->addr   = sti.addr;
+        strlit->length = sti.len;
         return;
     }
 
@@ -1919,9 +1940,10 @@ static void compile_string_literal(OnyxWasmModule* mod, AstStrLit* strlit) {
     };
 
     strlit->addr = (u32) mod->next_datum_offset,
+    strlit->length = length;
     mod->next_datum_offset += length;
 
-    bh_table_put(u32, mod->string_literals, (char *) strdata, strlit->addr);
+    bh_table_put(StrLitInfo, mod->string_literals, (char *) strdata, ((StrLitInfo) { strlit->addr, strlit->length }));
 
     bh_arr_push(mod->data, datum);
 }