From 6aae381e83dddf8808feefe4a5a2470320f27342 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 27 May 2018 11:03:46 +0100 Subject: [PATCH] Move ODIN_* platform constants to `core:os` --- core/bits/bits.odin | 34 +++++++++--------- core/{runtime => builtin}/_preload.odin | 2 +- core/{runtime => builtin}/_soft_numbers.odin | 2 +- core/os/os_amd64.odin | 4 +++ core/os/os_essence.odin | 2 ++ core/os/os_linux.odin | 2 ++ core/os/os_osx.odin | 2 ++ core/os/os_windows.odin | 2 ++ core/os/os_x86.odin | 4 +++ examples/demo/demo.odin | 2 +- src/build_settings.cpp | 6 ++-- src/check_decl.cpp | 9 ++--- src/checker.cpp | 38 +++++++++++--------- src/checker.hpp | 2 +- src/entity.cpp | 17 +-------- src/ir.cpp | 14 ++------ src/parser.cpp | 6 ++-- src/parser.hpp | 2 +- 18 files changed, 72 insertions(+), 78 deletions(-) rename core/{runtime => builtin}/_preload.odin (99%) rename core/{runtime => builtin}/_soft_numbers.odin (99%) create mode 100644 core/os/os_amd64.odin create mode 100644 core/os/os_x86.odin diff --git a/core/bits/bits.odin b/core/bits/bits.odin index 0e0f21db2..57e2ce7ae 100644 --- a/core/bits/bits.odin +++ b/core/bits/bits.odin @@ -1,5 +1,7 @@ package bits +import "core:os" + U8_MIN :: u8(0); U16_MIN :: u16(0); U32_MIN :: u32(0); @@ -95,29 +97,29 @@ reverse_bits32 :: proc(i: u32) -> u32 { return __llvm_bitreverse32(i); } reverse_bits64 :: proc(i: u64) -> u64 { return __llvm_bitreverse64(i); } from_be_u8 :: proc(i: u8) -> u8 { return i; } -from_be_u16 :: proc(i: u16) -> u16 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } } -from_be_u32 :: proc(i: u32) -> u32 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } } -from_be_u64 :: proc(i: u64) -> u64 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } } -from_be_uint :: proc(i: uint) -> uint { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } } +from_be_u16 :: proc(i: u16) -> u16 { when os.ENDIAN == "big" { return i; } else { return byte_swap(i); } } +from_be_u32 :: proc(i: u32) -> u32 { when os.ENDIAN == "big" { return i; } else { return byte_swap(i); } } +from_be_u64 :: proc(i: u64) -> u64 { when os.ENDIAN == "big" { return i; } else { return byte_swap(i); } } +from_be_uint :: proc(i: uint) -> uint { when os.ENDIAN == "big" { return i; } else { return byte_swap(i); } } from_le_u8 :: proc(i: u8) -> u8 { return i; } -from_le_u16 :: proc(i: u16) -> u16 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } } -from_le_u32 :: proc(i: u32) -> u32 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } } -from_le_u64 :: proc(i: u64) -> u64 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } } -from_le_uint :: proc(i: uint) -> uint { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } } +from_le_u16 :: proc(i: u16) -> u16 { when os.ENDIAN == "little" { return i; } else { return byte_swap(i); } } +from_le_u32 :: proc(i: u32) -> u32 { when os.ENDIAN == "little" { return i; } else { return byte_swap(i); } } +from_le_u64 :: proc(i: u64) -> u64 { when os.ENDIAN == "little" { return i; } else { return byte_swap(i); } } +from_le_uint :: proc(i: uint) -> uint { when os.ENDIAN == "little" { return i; } else { return byte_swap(i); } } to_be_u8 :: proc(i: u8) -> u8 { return i; } -to_be_u16 :: proc(i: u16) -> u16 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } } -to_be_u32 :: proc(i: u32) -> u32 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } } -to_be_u64 :: proc(i: u64) -> u64 { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } } -to_be_uint :: proc(i: uint) -> uint { when ODIN_ENDIAN == "big" { return i; } else { return byte_swap(i); } } +to_be_u16 :: proc(i: u16) -> u16 { when os.ENDIAN == "big" { return i; } else { return byte_swap(i); } } +to_be_u32 :: proc(i: u32) -> u32 { when os.ENDIAN == "big" { return i; } else { return byte_swap(i); } } +to_be_u64 :: proc(i: u64) -> u64 { when os.ENDIAN == "big" { return i; } else { return byte_swap(i); } } +to_be_uint :: proc(i: uint) -> uint { when os.ENDIAN == "big" { return i; } else { return byte_swap(i); } } to_le_u8 :: proc(i: u8) -> u8 { return i; } -to_le_u16 :: proc(i: u16) -> u16 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } } -to_le_u32 :: proc(i: u32) -> u32 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } } -to_le_u64 :: proc(i: u64) -> u64 { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } } -to_le_uint :: proc(i: uint) -> uint { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } } +to_le_u16 :: proc(i: u16) -> u16 { when os.ENDIAN == "little" { return i; } else { return byte_swap(i); } } +to_le_u32 :: proc(i: u32) -> u32 { when os.ENDIAN == "little" { return i; } else { return byte_swap(i); } } +to_le_u64 :: proc(i: u64) -> u64 { when os.ENDIAN == "little" { return i; } else { return byte_swap(i); } } +to_le_uint :: proc(i: uint) -> uint { when os.ENDIAN == "little" { return i; } else { return byte_swap(i); } } overflowing_add_u8 :: proc(lhs, rhs: u8) -> (u8, bool) { foreign __llvm_core @(link_name="llvm.uadd.with.overflow.i8") op :: proc(u8, u8) -> (u8, bool) ---; return op(lhs, rhs); } diff --git a/core/runtime/_preload.odin b/core/builtin/_preload.odin similarity index 99% rename from core/runtime/_preload.odin rename to core/builtin/_preload.odin index 5f80594b9..f6174d383 100644 --- a/core/runtime/_preload.odin +++ b/core/builtin/_preload.odin @@ -1,4 +1,4 @@ -package runtime +package builtin import "core:os" import "core:unicode/utf8" diff --git a/core/runtime/_soft_numbers.odin b/core/builtin/_soft_numbers.odin similarity index 99% rename from core/runtime/_soft_numbers.odin rename to core/builtin/_soft_numbers.odin index 92ec2bfec..b925eddea 100644 --- a/core/runtime/_soft_numbers.odin +++ b/core/builtin/_soft_numbers.odin @@ -1,4 +1,4 @@ -package runtime +package builtin /* @(link_name="__multi3") diff --git a/core/os/os_amd64.odin b/core/os/os_amd64.odin new file mode 100644 index 000000000..aab98b12c --- /dev/null +++ b/core/os/os_amd64.odin @@ -0,0 +1,4 @@ +package os; + +ARCH :: "amd64"; +ENDIAN :: "little"; diff --git a/core/os/os_essence.odin b/core/os/os_essence.odin index 6993e7c8f..525233dfd 100644 --- a/core/os/os_essence.odin +++ b/core/os/os_essence.odin @@ -1,5 +1,7 @@ package os +OS :: "essence"; + // foreign import api "system:api" // Handle :: distinct int; diff --git a/core/os/os_linux.odin b/core/os/os_linux.odin index e3c9de55b..efe9160d3 100644 --- a/core/os/os_linux.odin +++ b/core/os/os_linux.odin @@ -6,6 +6,8 @@ foreign import libc "system:c" import "core:strings" import "core:mem" +OS :: "linux"; + Handle :: distinct i32; File_Time :: distinct u64; Errno :: distinct i32; diff --git a/core/os/os_osx.odin b/core/os/os_osx.odin index e45cfb264..1292bfd95 100644 --- a/core/os/os_osx.odin +++ b/core/os/os_osx.odin @@ -6,6 +6,8 @@ foreign import libc "system:c" import "core:strings" import "core:mem" +OS :: "osx"; + Handle :: distinct i32; File_Time :: distinct u64; Errno :: distinct int; diff --git a/core/os/os_windows.odin b/core/os/os_windows.odin index 4b776c7f4..3c1a6d627 100644 --- a/core/os/os_windows.odin +++ b/core/os/os_windows.odin @@ -3,6 +3,8 @@ package os import "core:sys/win32" import "core:mem" +OS :: "windows"; + Handle :: distinct uintptr; File_Time :: distinct u64; Errno :: distinct int; diff --git a/core/os/os_x86.odin b/core/os/os_x86.odin new file mode 100644 index 000000000..c05858f45 --- /dev/null +++ b/core/os/os_x86.odin @@ -0,0 +1,4 @@ +package os; + +ARCH :: "x86"; +ENDIAN :: "little"; diff --git a/examples/demo/demo.odin b/examples/demo/demo.odin index ee96560d3..abd788120 100644 --- a/examples/demo/demo.odin +++ b/examples/demo/demo.odin @@ -488,7 +488,7 @@ prefix_table := [?]string{ }; threading_example :: proc() { - when ODIN_OS == "windows" { + when os.OS == "windows" { fmt.println("# threading_example"); unordered_remove :: proc(array: ^[dynamic]$T, index: int, loc := #caller_location) { diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 13776dedb..798b94bb8 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -13,7 +13,7 @@ enum TargetArchKind { TargetArch_Invalid, TargetArch_amd64, - TargetArch_x64, + TargetArch_x86, TargetArch_COUNT, }; @@ -77,7 +77,7 @@ gb_global BuildContext build_context = {0}; TargetOsKind get_target_os_from_string(String str) { for (isize i = 0; i < TargetOs_COUNT; i++) { - if (target_os_names[i] == str) { + if (str_eq_ignore_case(target_os_names[i], str)) { return cast(TargetOsKind)i; } } @@ -86,7 +86,7 @@ TargetOsKind get_target_os_from_string(String str) { TargetArchKind get_target_arch_from_string(String str) { for (isize i = 0; i < TargetArch_COUNT; i++) { - if (target_os_names[i] == str) { + if (str_eq_ignore_case(target_arch_names[i], str)) { return cast(TargetArchKind)i; } } diff --git a/src/check_decl.cpp b/src/check_decl.cpp index db666da46..f4545e794 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -534,12 +534,7 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) { e->deprecated_message = ac.deprecated_message; ac.link_name = handle_link_name(c, e->token, ac.link_name, ac.link_prefix); - AstPackage *package = nullptr; - if (d->scope->parent && d->scope->parent->is_package) { - package = d->scope->parent->package; - } - - if (package != nullptr && e->token.string == "main") { + if (e->package != nullptr && e->token.string == "main") { if (pt->param_count != 0 || pt->result_count != 0) { gbString str = type_to_string(proc_type); @@ -551,7 +546,7 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) { error(e->token, "Procedure 'main' cannot have a custom calling convention"); } pt->calling_convention = ProcCC_Contextless; - if (package->kind == ImportedPackage_Init) { + if (e->package->kind == ImportedPackage_Init) { if (c->info.entry_point != nullptr) { error(e->token, "Redeclaration of the entry pointer procedure 'main'"); } else { diff --git a/src/checker.cpp b/src/checker.cpp index 470cfe185..f1ec0d182 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -258,8 +258,8 @@ Scope *create_scope_from_package(Checker *c, AstPackage *p) { s->is_init = p->kind == ImportedPackage_Init; } - s->is_global = p->kind == ImportedPackage_Runtime; - if (p->kind == ImportedPackage_Runtime) { + s->is_global = p->kind == ImportedPackage_Builtin; + if (p->kind == ImportedPackage_Builtin) { universal_scope->shared = s; } @@ -481,7 +481,7 @@ void add_type_info_dependency(DeclInfo *d, Type *type) { void add_preload_dependency(Checker *c, char *name) { String n = make_string_c(name); - Entity *e = scope_lookup_entity(c->runtime_package->scope, n); + Entity *e = scope_lookup_entity(c->builtin_package->scope, n); GB_ASSERT(e != nullptr); ptr_set_add(&c->context.decl->deps, e); // add_type_info_type(c, e->type); @@ -551,9 +551,9 @@ void init_universal_scope(void) { str_lit(""), str_lit("__llvm_core"))); // TODO(bill): Set through flags in the compiler - add_global_string_constant(str_lit("ODIN_OS"), bc->ODIN_OS); - add_global_string_constant(str_lit("ODIN_ARCH"), bc->ODIN_ARCH); - add_global_string_constant(str_lit("ODIN_ENDIAN"), bc->ODIN_ENDIAN); + // add_global_string_constant(str_lit("ODIN_OS"), bc->ODIN_OS); + // add_global_string_constant(str_lit("ODIN_ARCH"), bc->ODIN_ARCH); + // add_global_string_constant(str_lit("ODIN_ENDIAN"), bc->ODIN_ENDIAN); add_global_string_constant(str_lit("ODIN_VENDOR"), bc->ODIN_VENDOR); add_global_string_constant(str_lit("ODIN_VERSION"), bc->ODIN_VERSION); add_global_string_constant(str_lit("ODIN_ROOT"), bc->ODIN_ROOT); @@ -921,11 +921,17 @@ void add_entity_and_decl_info(Checker *c, AstNode *identifier, Entity *e, DeclIn case Entity_LibraryName: // NOTE(bill): Entities local to file rather than package break; - default: - GB_ASSERT(scope->file->package->scope == scope->parent); - scope = scope->file->package->scope; + default: { + AstPackage *p = scope->file->package; + GB_ASSERT(p->scope == scope->parent); + scope = p->scope; + if (e->package != nullptr) { + GB_ASSERT(e->package == p); + } + e->package = p; break; } + } } add_entity(c, scope, identifier, e); } @@ -1343,7 +1349,7 @@ void generate_minimum_dependency_set(Checker *c, Entity *start) { str_lit("Context"), }; for (isize i = 0; i < gb_count_of(required_entities); i++) { - add_dependency_to_set(c, scope_lookup_entity(c->runtime_package->scope, required_entities[i])); + add_dependency_to_set(c, scope_lookup_entity(c->builtin_package->scope, required_entities[i])); } if (!build_context.no_bounds_check) { @@ -1353,7 +1359,7 @@ void generate_minimum_dependency_set(Checker *c, Entity *start) { str_lit("__dynamic_array_expr_error"), }; for (isize i = 0; i < gb_count_of(bounds_check_entities); i++) { - add_dependency_to_set(c, scope_lookup_entity(c->runtime_package->scope, bounds_check_entities[i])); + add_dependency_to_set(c, scope_lookup_entity(c->builtin_package->scope, bounds_check_entities[i])); } } @@ -1473,7 +1479,7 @@ Array generate_entity_dependency_graph(CheckerInfo *info) { Entity *find_core_entity(Checker *c, String name) { - Entity *e = current_scope_lookup_entity(c->runtime_package->scope, name); + Entity *e = current_scope_lookup_entity(c->builtin_package->scope, name); if (e == nullptr) { compiler_error("Could not find type declaration for '%.*s'\n" "Is '_preload.odin' missing from the 'core' directory relative to odin.exe?", LIT(name)); @@ -1483,7 +1489,7 @@ Entity *find_core_entity(Checker *c, String name) { } Type *find_core_type(Checker *c, String name) { - Entity *e = current_scope_lookup_entity(c->runtime_package->scope, name); + Entity *e = current_scope_lookup_entity(c->builtin_package->scope, name); if (e == nullptr) { compiler_error("Could not find type declaration for '%.*s'\n" "Is '_preload.odin' missing from the 'core' directory relative to odin.exe?", LIT(name)); @@ -2948,9 +2954,9 @@ void check_parsed_files(Checker *c) { if (scope->is_init) { c->info.init_scope = scope; } - if (p->kind == ImportedPackage_Runtime) { - GB_ASSERT(c->runtime_package == nullptr); - c->runtime_package = p; + if (p->kind == ImportedPackage_Builtin) { + GB_ASSERT(c->builtin_package == nullptr); + c->builtin_package = p; } } diff --git a/src/checker.hpp b/src/checker.hpp index 4265a535d..f4ea5b45f 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -326,7 +326,7 @@ struct Checker { AstFile * curr_ast_file; - AstPackage * runtime_package; + AstPackage * builtin_package; // NOTE(bill): Procedures to check Array procs; Map package_scopes; // Key: String (fullpath) diff --git a/src/entity.cpp b/src/entity.cpp index d7f7dc2df..31081e5ce 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -6,7 +6,6 @@ struct DeclInfo; #define ENTITY_KINDS \ ENTITY_KIND(Invalid) \ - ENTITY_KIND(Package) \ ENTITY_KIND(Constant) \ ENTITY_KIND(Variable) \ ENTITY_KIND(TypeName) \ @@ -77,6 +76,7 @@ struct Entity { AstNode * identifier; // Can be nullptr DeclInfo * decl_info; DeclInfo * parent_proc_decl; // nullptr if in file/global scope + AstPackage *package; // TODO(bill): Cleanup how `using` works for entities Entity * using_parent; @@ -86,12 +86,6 @@ struct Entity { String deprecated_message; union { - struct { - String fullpath; - String name; - Scope *scope; - // Array imports; // Entity_Package - } Package; struct { ExactValue value; } Constant; @@ -196,15 +190,6 @@ Entity *alloc_entity(EntityKind kind, Scope *scope, Token token, Type *type) { return entity; } -Entity *alloc_entity_package(Scope *scope, Type *type, String fullpath, String name) { - Token token = empty_token; - token.string = name; - Entity *entity = alloc_entity(Entity_Package, scope, token, type); - entity->Package.fullpath = fullpath; - entity->Package.name = name; - return entity; -} - Entity *alloc_entity_variable(Scope *scope, Token token, Type *type, bool is_immutable, EntityState state = EntityState_Unresolved) { Entity *entity = alloc_entity(Entity_Variable, scope, token, type); entity->Variable.is_immutable = is_immutable; diff --git a/src/ir.cpp b/src/ir.cpp index f20d2cafb..ff364602b 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -8257,12 +8257,7 @@ void ir_gen_tree(irGen *s) { Entity *e = info->entities[i]; String name = e->token.string; - bool is_global = false; - if (e->scope->is_package) { - is_global = true; - } else if (e->scope->parent && e->scope->parent->is_package) { - is_global = true; - } + bool is_global = e->package != nullptr; if (e->kind == Entity_Variable) { global_variable_max_count++; @@ -8315,12 +8310,7 @@ void ir_gen_tree(irGen *s) { GB_ASSERT(e->kind == Entity_Variable); - bool is_global = false; - if (e->scope->is_package) { - is_global = true; - } else if (e->scope->parent && e->scope->parent->is_package) { - is_global = true; - } + bool is_global = e->package != nullptr; bool is_foreign = e->Variable.is_foreign; bool is_export = e->Variable.is_export; diff --git a/src/parser.cpp b/src/parser.cpp index 1a7ba68a3..3945a9415 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -4150,7 +4150,7 @@ void parse_file(Parser *p, AstFile *f) { if (package_name.kind == Token_Ident) { if (package_name.string == "_") { error(package_name, "Invalid package name '_'"); - } else if (f->package->kind != ImportedPackage_Runtime && package_name.string == "runtime") { + } else if (f->package->kind != ImportedPackage_Builtin && package_name.string == "builtin") { error(package_name, "Use of reserved package name '%.*s'", LIT(package_name.string)); } } @@ -4338,8 +4338,8 @@ ParseFileError parse_packages(Parser *p, String init_filename) { isize shared_package_count = 0; if (!build_context.generate_docs) { - String s = get_fullpath_core(heap_allocator(), str_lit("runtime")); - try_add_import_path(p, s, s, init_pos, ImportedPackage_Runtime); + String s = get_fullpath_core(heap_allocator(), str_lit("builtin")); + try_add_import_path(p, s, s, init_pos, ImportedPackage_Builtin); shared_package_count++; } diff --git a/src/parser.hpp b/src/parser.hpp index ed122baea..4f33f44d2 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -27,7 +27,7 @@ struct CommentGroup { enum ImportedPackageKind { ImportedPackage_Normal, - ImportedPackage_Runtime, + ImportedPackage_Builtin, ImportedPackage_Init, };