From: Brendan Hansen Date: Sat, 22 Aug 2020 22:18:27 +0000 (-0500) Subject: general bugfixes; strings are now a pointer and a length X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=21c741e2ccc026060c661a3b889f1f7bff27e349;p=onyx.git general bugfixes; strings are now a pointer and a length --- diff --git a/Makefile b/Makefile index 740b434b..f92c3aa4 100644 --- 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 index 00000000..6a7d9b7a --- /dev/null +++ b/core/builtin.onyx @@ -0,0 +1,8 @@ +package builtin + +string :: struct { + data : ^u8; + len : u32; +} + +cstring :: #type ^u8; \ No newline at end of file diff --git a/core/string.onyx b/core/string.onyx index 50a21994..8f58d66b 100644 --- a/core/string.onyx +++ b/core/string.onyx @@ -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); diff --git a/core/wasi.onyx b/core/wasi.onyx index 96908f0f..d26d9e96 100644 --- a/core/wasi.onyx +++ b/core/wasi.onyx @@ -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 --- diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index b965794c..aefd7c78 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -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 diff --git a/include/onyxutils.h b/include/onyxutils.h index 70bdb32d..4e2b1f43 100644 --- a/include/onyxutils.h +++ b/include/onyxutils.h @@ -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); diff --git a/include/onyxwasm.h b/include/onyxwasm.h index c448a303..2b6d9761 100644 --- a/include/onyxwasm.h +++ b/include/onyxwasm.h @@ -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 7f0459b0..54e07f57 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/wasi_test.onyx b/progs/wasi_test.onyx index 92f35ba3..9f4f7c36 100644 --- a/progs/wasi_test.onyx +++ b/progs/wasi_test.onyx @@ -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 diff --git a/src/onyx.c b/src/onyx.c index 9608963a..62a8cc7e 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -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) { diff --git a/src/onyxbuiltins.c b/src/onyxbuiltins.c index e4a8d423..77e1972e 100644 --- a/src/onyxbuiltins.c +++ b/src/onyxbuiltins.c @@ -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 diff --git a/src/onyxchecker.c b/src/onyxchecker.c index b3d2f744..789f2405 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -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; } diff --git a/src/onyxparser.c b/src/onyxparser.c index e61e4b52..2ae55a91 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -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); diff --git a/src/onyxsymres.c b/src/onyxsymres.c index 69a2fd46..66206ac5 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -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); diff --git a/src/onyxutils.c b/src/onyxutils.c index 766d0c46..9733d625 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -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; } diff --git a/src/onyxwasm.c b/src/onyxwasm.c index e33b3789..0eec5286 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -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); }