From: Brendan Hansen Date: Tue, 30 Mar 2021 16:48:53 +0000 (-0500) Subject: random cleanups X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=b4920dcb5cf717e4614ff56eea62992c38bf7b5e;p=onyx.git random cleanups --- diff --git a/bin/onyx b/bin/onyx index cf48b2c3..2cd9c348 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/core/string.onyx b/core/string.onyx index ab8949d5..2c5997b6 100644 --- a/core/string.onyx +++ b/core/string.onyx @@ -38,7 +38,7 @@ copy :: (orig: str, dest: str) { len := orig.count; if dest.count < len do len = dest.count; - for i: 0 .. len do dest.data[i] = orig.data[i]; + memory.copy(dest.data, orig.data, len); } concat :: (s1: str, s2: str) -> str { @@ -46,8 +46,8 @@ concat :: (s1: str, s2: str) -> str { len2 := length(s2); data := cast(^u8) calloc(len1 + len2); - for i: 0 .. len1 do data[i] = s1[i]; - for i: 0 .. len2 do data[i + len1] = s2[i]; + memory.copy(data, s1.data, len1); + memory.copy(data + len1, s2.data, len2); return str.{ data, len1 + len2 }; } diff --git a/core/sys/wasi.onyx b/core/sys/wasi.onyx index ebfbf06e..2805a217 100644 --- a/core/sys/wasi.onyx +++ b/core/sys/wasi.onyx @@ -31,6 +31,17 @@ assert_handler :: (msg: str, file: str) { proc_exit(1); } +proc () #export "_initialize" { + alloc.init(); + + __initialize(^context); + context.allocator = alloc.heap_allocator; + context.temp_allocator = alloc.temp_allocator; + context.assert_handler = assert_handler; + + stdio_init(); +} + // The builtin _start proc. // Sets up everything needed for execution. proc () #export "_start" { @@ -41,6 +52,8 @@ proc () #export "_start" { context.temp_allocator = alloc.temp_allocator; context.assert_handler = assert_handler; + stdio_init(); + args : [] cstr; argv_buf_size : Size; args_sizes_get(^args.count, ^argv_buf_size); @@ -63,10 +76,6 @@ proc () #export "_start" { args[i] = cast(cstr) (cast(^u32) args.data)[i]; } - - - stdio_init(); - main.main(args); print_stream_flush(); diff --git a/core/wasi.onyx b/core/wasi.onyx index 1383f60d..fa0e3a1a 100644 --- a/core/wasi.onyx +++ b/core/wasi.onyx @@ -364,42 +364,42 @@ IOVecArray :: struct { // FUNCTIONS -args_get :: (argv: ^^u8, argv_buf: ^u8) -> Errno #foreign "wasi_unstable" "args_get"--- -args_sizes_get :: (argc: ^Size, argv_buf_size: ^Size) -> Errno #foreign "wasi_unstable" "args_sizes_get" --- - -environ_get :: (environ: ^^u8, environ_buf: ^u8) -> Errno #foreign "wasi_unstable" "environ_get" --- -environ_sizes_get :: (environc: ^Size, environ_buf_size: ^Size) -> Errno #foreign "wasi_unstable" "environ_sizes_get" --- - -clock_res_get :: (id: ClockID, resolution: ^Timestamp) -> Errno #foreign "wasi_unstable" "clock_res_get" --- -clock_time_get :: (id: ClockID, precision: Timestamp, time: ^Timestamp) -> Errno #foreign "wasi_unstable" "clock_time_get" --- - -fd_advise :: (fd: FileDescriptor, offset: Filesize, len: Filesize, advice: Advice) -> Errno #foreign "wasi_unstable" "fd_advise" --- -fd_allocate :: (fd: FileDescriptor, offset: Filesize, len: Filesize) -> Errno #foreign "wasi_unstable" "fd_allocate" --- -fd_close :: (fd: FileDescriptor) -> Errno #foreign "wasi_unstable" "fd_close" --- -fd_datasync :: (fd: FileDescriptor) -> Errno #foreign "wasi_unstable" "fd_datasync" --- -fd_fdstat_get :: (fd: FileDescriptor, stat: ^FDStat) -> Errno #foreign "wasi_unstable" "fd_fdstat_get" --- -fd_fdstat_set_flags :: (fd: FileDescriptor, flags: FDFlags) -> Errno #foreign "wasi_unstable" "fd_fdstat_set_flags" --- -fd_fdstat_set_rights :: (fd: FileDescriptor, rights_base: Rights, rights_inheriting: Rights) -> Errno #foreign "wasi_unstable" "fd_fdstat_set_rights" --- -fd_filestat_get :: (fd: FileDescriptor, buf: ^FileStat) -> Errno #foreign "wasi_unstable" "fd_filestat_get" --- -fd_filestat_set_size :: (fd: FileDescriptor, size: Filesize) -> Errno #foreign "wasi_unstable" "fd_filestat_set_size" --- -fd_filestat_set_times :: (fd: FileDescriptor, atim: Timestamp, mtim: Timestamp, fst_flags: FSTFlags) -> Errno #foreign "wasi_unstable" "fd_filestat_set_times" --- -fd_pread :: (fd: FileDescriptor, iovs: IOVecArray, offset: Filesize, nread: ^Size) -> Errno #foreign "wasi_unstable" "fd_pread" --- -fd_prestat_get :: (fd: FileDescriptor, buf: ^PrestatTagged) -> Errno #foreign "wasi_unstable" "fd_prestat_get" --- -fd_prestat_dir_name :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_unstable" "fd_prestat_dir_name" --- -fd_pwrite :: (fd: FileDescriptor, iovs: IOVecArray, offset: Filesize, nwritten: ^Size) -> Errno #foreign "wasi_unstable" "fd_pwrite" --- -fd_read :: (fd: FileDescriptor, iovs: IOVecArray, nread: ^Size) -> Errno #foreign "wasi_unstable" "fd_read" --- -fd_readdir :: (fd: FileDescriptor, buf: ^u8, buf_len: Size, cookie: DirCookie, bufused: ^Size) -> Errno #foreign "wasi_unstable" "fd_readdir" --- -fd_renumber :: (fd: FileDescriptor, to: FileDescriptor) -> Errno #foreign "wasi_unstable" "fd_renumber" --- -fd_seek :: (fd: FileDescriptor, offset: FileDelta, whence: Whence, newoffset: ^Filesize) -> Errno #foreign "wasi_unstable" "fd_seek" --- -fd_sync :: (fd: FileDescriptor) -> Errno #foreign "wasi_unstable" "fd_sync" --- -fd_tell :: (fd: FileDescriptor, offset: ^Filesize) -> Errno #foreign "wasi_unstable" "fd_tell" --- -fd_write :: (fd: FileDescriptor, iovs: IOVecArray, nwritten: ^Size) -> Errno #foreign "wasi_unstable" "fd_write" --- - -path_create_directory :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_unstable" "path_create_directory" --- -path_filestat_get :: (fd: FileDescriptor, flags: LookupFlags, path: str, buf: ^FileStat) -> Errno #foreign "wasi_unstable" "path_filestat_get" --- -path_filestat_set_times :: (fd: FileDescriptor, flags: LookupFlags, path: str, atim: Timestamp, mtim: Timestamp, fst_flags: FSTFlags) -> Errno #foreign "wasi_unstable" "path_filestat_set_times" --- - -path_link :: (fd: FileDescriptor, old_flags: LookupFlags, old_path: str, new_fd: FileDescriptor, new_path: str) -> Errno #foreign "wasi_unstable" "path_link" --- +args_get :: (argv: ^^u8, argv_buf: ^u8) -> Errno #foreign "wasi_snapshot_preview1" "args_get"--- +args_sizes_get :: (argc: ^Size, argv_buf_size: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "args_sizes_get" --- + +environ_get :: (environ: ^^u8, environ_buf: ^u8) -> Errno #foreign "wasi_snapshot_preview1" "environ_get" --- +environ_sizes_get :: (environc: ^Size, environ_buf_size: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "environ_sizes_get" --- + +clock_res_get :: (id: ClockID, resolution: ^Timestamp) -> Errno #foreign "wasi_snapshot_preview1" "clock_res_get" --- +clock_time_get :: (id: ClockID, precision: Timestamp, time: ^Timestamp) -> Errno #foreign "wasi_snapshot_preview1" "clock_time_get" --- + +fd_advise :: (fd: FileDescriptor, offset: Filesize, len: Filesize, advice: Advice) -> Errno #foreign "wasi_snapshot_preview1" "fd_advise" --- +fd_allocate :: (fd: FileDescriptor, offset: Filesize, len: Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_allocate" --- +fd_close :: (fd: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_close" --- +fd_datasync :: (fd: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_datasync" --- +fd_fdstat_get :: (fd: FileDescriptor, stat: ^FDStat) -> Errno #foreign "wasi_snapshot_preview1" "fd_fdstat_get" --- +fd_fdstat_set_flags :: (fd: FileDescriptor, flags: FDFlags) -> Errno #foreign "wasi_snapshot_preview1" "fd_fdstat_set_flags" --- +fd_fdstat_set_rights :: (fd: FileDescriptor, rights_base: Rights, rights_inheriting: Rights) -> Errno #foreign "wasi_snapshot_preview1" "fd_fdstat_set_rights" --- +fd_filestat_get :: (fd: FileDescriptor, buf: ^FileStat) -> Errno #foreign "wasi_snapshot_preview1" "fd_filestat_get" --- +fd_filestat_set_size :: (fd: FileDescriptor, size: Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_filestat_set_size" --- +fd_filestat_set_times :: (fd: FileDescriptor, atim: Timestamp, mtim: Timestamp, fst_flags: FSTFlags) -> Errno #foreign "wasi_snapshot_preview1" "fd_filestat_set_times" --- +fd_pread :: (fd: FileDescriptor, iovs: IOVecArray, offset: Filesize, nread: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_pread" --- +fd_prestat_get :: (fd: FileDescriptor, buf: ^PrestatTagged) -> Errno #foreign "wasi_snapshot_preview1" "fd_prestat_get" --- +fd_prestat_dir_name :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_snapshot_preview1" "fd_prestat_dir_name" --- +fd_pwrite :: (fd: FileDescriptor, iovs: IOVecArray, offset: Filesize, nwritten: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_pwrite" --- +fd_read :: (fd: FileDescriptor, iovs: IOVecArray, nread: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_read" --- +fd_readdir :: (fd: FileDescriptor, buf: ^u8, buf_len: Size, cookie: DirCookie, bufused: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_readdir" --- +fd_renumber :: (fd: FileDescriptor, to: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_renumber" --- +fd_seek :: (fd: FileDescriptor, offset: FileDelta, whence: Whence, newoffset: ^Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_seek" --- +fd_sync :: (fd: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_sync" --- +fd_tell :: (fd: FileDescriptor, offset: ^Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_tell" --- +fd_write :: (fd: FileDescriptor, iovs: IOVecArray, nwritten: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_write" --- + +path_create_directory :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_create_directory" --- +path_filestat_get :: (fd: FileDescriptor, flags: LookupFlags, path: str, buf: ^FileStat) -> Errno #foreign "wasi_snapshot_preview1" "path_filestat_get" --- +path_filestat_set_times :: (fd: FileDescriptor, flags: LookupFlags, path: str, atim: Timestamp, mtim: Timestamp, fst_flags: FSTFlags) -> Errno #foreign "wasi_snapshot_preview1" "path_filestat_set_times" --- + +path_link :: (fd: FileDescriptor, old_flags: LookupFlags, old_path: str, new_fd: FileDescriptor, new_path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_link" --- path_open :: (fd: FileDescriptor , dirflags: LookupFlags , path: str @@ -409,26 +409,26 @@ path_open :: (fd: FileDescriptor , fdflags: FDFlags , opened_fd: ^FileDescriptor ) -> Errno - #foreign "wasi_unstable" "path_open" --- + #foreign "wasi_snapshot_preview1" "path_open" --- -path_readlink :: (fd: FileDescriptor, path: str, buf: ^u8, buf_len: Size, bufused: ^Size) -> Errno #foreign "wasi_unstable" "path_readlink" --- -path_remove_directory :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_unstable" "path_remove_directory" --- -path_rename :: (fd: FileDescriptor, old_path: str, new_fd: FileDescriptor, new_path: str) -> Errno #foreign "wasi_unstable" "path_rename" --- -path_symlink :: (old_path: ^u8, old_path_len: Size, fd: FileDescriptor, new_path: str) -> Errno #foreign "wasi_unstable" "path_symlink" --- -path_unlink_file :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_unstable" "path_unlink_file" --- +path_readlink :: (fd: FileDescriptor, path: str, buf: ^u8, buf_len: Size, bufused: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "path_readlink" --- +path_remove_directory :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_remove_directory" --- +path_rename :: (fd: FileDescriptor, old_path: str, new_fd: FileDescriptor, new_path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_rename" --- +path_symlink :: (old_path: ^u8, old_path_len: Size, fd: FileDescriptor, new_path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_symlink" --- +path_unlink_file :: (fd: FileDescriptor, path: str) -> Errno #foreign "wasi_snapshot_preview1" "path_unlink_file" --- -poll_oneoff :: (in: ^Subscription, out: ^Event, nsubscriptions: Size, nevents: ^Size) -> Errno #foreign "wasi_unstable" "poll_oneoff" --- +poll_oneoff :: (in: ^Subscription, out: ^Event, nsubscriptions: Size, nevents: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "poll_oneoff" --- -proc_exit :: (rval: ExitCode) -> void #foreign "wasi_unstable" "proc_exit" --- -proc_raise :: (sig: Signal) -> Errno #foreign "wasi_unstable" "proc_raise" --- +proc_exit :: (rval: ExitCode) -> void #foreign "wasi_snapshot_preview1" "proc_exit" --- +proc_raise :: (sig: Signal) -> Errno #foreign "wasi_snapshot_preview1" "proc_raise" --- -sched_yield :: () -> Errno #foreign "wasi_unstable" "sched_yield" --- +sched_yield :: () -> Errno #foreign "wasi_snapshot_preview1" "sched_yield" --- -random_get :: (buf: ^u8, buf_len: Size) -> Errno #foreign "wasi_unstable" "random_get" --- +random_get :: (buf: ^u8, buf_len: Size) -> Errno #foreign "wasi_snapshot_preview1" "random_get" --- -sock_recv :: (fd: FileDescriptor, ri_data: IOVecArray, ri_flags: RIFlags, ro_datalen: ^Size, ro_flags: ^ROFlags) -> Errno #foreign "wasi_unstable" "sock_recv" --- -sock_send :: (fd: FileDescriptor, si_data: IOVecArray, si_flags: SIFlags, so_datalen: ^Size) -> Errno #foreign "wasi_unstable" "sock_send" --- -sock_shutdown :: (fd: FileDescriptor, how: SDFlags) -> Errno #foreign "wasi_unstable" "sock_shutdown" --- +sock_recv :: (fd: FileDescriptor, ri_data: IOVecArray, ri_flags: RIFlags, ro_datalen: ^Size, ro_flags: ^ROFlags) -> Errno #foreign "wasi_snapshot_preview1" "sock_recv" --- +sock_send :: (fd: FileDescriptor, si_data: IOVecArray, si_flags: SIFlags, so_datalen: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "sock_send" --- +sock_shutdown :: (fd: FileDescriptor, how: SDFlags) -> Errno #foreign "wasi_snapshot_preview1" "sock_shutdown" --- diff --git a/src/onyxsymres.c b/src/onyxsymres.c index efb5ec6e..beaa4806 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -1084,16 +1084,16 @@ void symres_entity(Entity* ent) { case Entity_Type_Function: ss = symres_function(ent->function); break; case Entity_Type_Foreign_Global_Header: - case Entity_Type_Global_Header: ss = symres_global(ent->global); break; + case Entity_Type_Global_Header: ss = symres_global(ent->global); break; - case Entity_Type_Use_Package: ss = symres_use_package(ent->use_package); - if (ent->use_package->package) package_track_use_package(ent->use_package->package, ent); - next_state = Entity_State_Finalized; - break; + case Entity_Type_Use_Package: ss = symres_use_package(ent->use_package); + if (ent->use_package->package) package_track_use_package(ent->use_package->package, ent); + next_state = Entity_State_Finalized; + break; - case Entity_Type_Use: ss = symres_use(ent->use); - next_state = Entity_State_Finalized; - break; + case Entity_Type_Use: ss = symres_use(ent->use); + next_state = Entity_State_Finalized; + break; case Entity_Type_Overloaded_Function: ss = symres_overloaded_function(ent->overloaded_function); break; case Entity_Type_Expression: ss = symres_expression(&ent->expr); break; diff --git a/src/onyxtypes.c b/src/onyxtypes.c index 993f97f2..42eab6b2 100644 --- a/src/onyxtypes.c +++ b/src/onyxtypes.c @@ -37,95 +37,7 @@ Type basic_types[] = { { Type_Kind_Basic, 0, (AstType *) &basic_type_v128, { Basic_Kind_V128, Basic_Flag_SIMD, 16, 16, "v128" } }, }; -b32 types_are_surface_compatible(Type* t1, Type* t2) { - // NOTE: If they are pointing to the same thing, - // it is safe to assume they are the same type - if (t1 == t2) return 1; - if (t1 == NULL || t2 == NULL) return 0; - - switch (t1->kind) { - case Type_Kind_Basic: - if (t2->kind == Type_Kind_Basic) { - // HACK: Not sure if this is right way to check this? - if (t1 == t2) return 1; - - if ((t1->Basic.flags & Basic_Flag_Integer) && (t2->Basic.flags & Basic_Flag_Integer)) { - return t1->Basic.size == t2->Basic.size; - } - - if (t1->Basic.kind == Basic_Kind_Rawptr && type_is_pointer(t2)) { - return 1; - } - } - break; - - case Type_Kind_Pointer: - if (t2->kind != Type_Kind_Pointer) return 0; - - if (t1->Pointer.elem->kind == Type_Kind_Basic && t2->Pointer.elem->kind == Type_Kind_Basic) - return types_are_compatible(t1->Pointer.elem, t2->Pointer.elem); - - return 1; - - case Type_Kind_Array: { - if (t2->kind != Type_Kind_Array) return 0; - - if (t1->Array.count != 0) - if (t1->Array.count != t2->Array.count) return 0; - - return types_are_compatible(t1->Array.elem, t2->Array.elem); - } - - case Type_Kind_Struct: { - if (t2->kind != Type_Kind_Struct) return 0; - if (t1->Struct.mem_count != t2->Struct.mem_count) return 0; - if (t1->Struct.name && t2->Struct.name) - if (strcmp(t1->Struct.name, t2->Struct.name) == 0) return 1; - - b32 works = 1; - bh_table_each_start(StructMember, t1->Struct.members); - if (!bh_table_has(StructMember, t2->Struct.members, (char *) key)) return 0; - StructMember other = bh_table_get(StructMember, t2->Struct.members, (char *) key); - if (other.offset != value.offset) return 0; - - if (!types_are_compatible(value.type, other.type)) { - works = 0; - break; - } - bh_table_each_end; - - return works; - } - - case Type_Kind_Enum: { - if (t2->kind != Type_Kind_Enum) return 0; - return t1 == t2; - } - - case Type_Kind_Slice: { - if (t2->kind != Type_Kind_Slice) return 0; - return types_are_compatible(t1->Slice.ptr_to_data->Pointer.elem, t2->Slice.ptr_to_data->Pointer.elem); - } - - case Type_Kind_VarArgs: { - if (t2->kind != Type_Kind_VarArgs) return 0; - return types_are_compatible(t1->VarArgs.ptr_to_data->Pointer.elem, t2->VarArgs.ptr_to_data->Pointer.elem); - } - - case Type_Kind_DynArray: { - if (t2->kind != Type_Kind_DynArray) return 0; - return types_are_compatible(t1->DynArray.ptr_to_data->Pointer.elem, t2->DynArray.ptr_to_data->Pointer.elem); - } - - default: - assert(("Invalid type", 0)); - break; - } - - return 0; -} - -b32 types_are_compatible(Type* t1, Type* t2) { +b32 types_are_compatible_(Type* t1, Type* t2, b32 recurse_pointers) { // NOTE: If they are pointing to the same thing, // it is safe to assume they are the same type if (t1 == t2) return 1; @@ -151,6 +63,8 @@ b32 types_are_compatible(Type* t1, Type* t2) { case Type_Kind_Pointer: { if (t2->kind == Type_Kind_Pointer) { + if (!recurse_pointers) return 1; + if (types_are_compatible(t1->Pointer.elem, t2->Pointer.elem)) return 1; if (t1->Pointer.elem->kind == Type_Kind_Struct && t2->Pointer.elem->kind == Type_Kind_Struct) { @@ -178,6 +92,7 @@ b32 types_are_compatible(Type* t1, Type* t2) { case Type_Kind_Struct: { if (t2->kind != Type_Kind_Struct) return 0; + if (t1->Struct.unique_id != t2->Struct.unique_id) return 0; if (t1->Struct.mem_count != t2->Struct.mem_count) return 0; b32 works = 1; @@ -186,7 +101,9 @@ b32 types_are_compatible(Type* t1, Type* t2) { StructMember other = bh_table_get(StructMember, t2->Struct.members, (char *) key); if (other.offset != value.offset) return 0; - if (!types_are_surface_compatible(value.type, other.type)) { + // NOTE: Don't recurse down pointers; This could be a problem, but it is a quick + // fix for the problem that occurs when using a linked-list style data structure. + if (!types_are_compatible_(value.type, other.type, 0)) { works = 0; break; } @@ -196,9 +113,8 @@ b32 types_are_compatible(Type* t1, Type* t2) { } case Type_Kind_Enum: { + // NOTE: The check above for t1 == t2 would already catch this. return 0; - // if (t2->kind != Type_Kind_Enum) return 0; - // return t1 == t2; } case Type_Kind_Function: { @@ -250,6 +166,10 @@ b32 types_are_compatible(Type* t1, Type* t2) { return 0; } +b32 types_are_compatible(Type* t1, Type* t2) { + return types_are_compatible_(t1, t2, 1); +} + u32 type_size_of(Type* type) { if (type == NULL) return 0;