reduced number of foreign imports
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 6 Sep 2020 15:08:50 +0000 (10:08 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 6 Sep 2020 15:08:50 +0000 (10:08 -0500)
core/random.onyx
docs/plan
include/onyxastnodes.h
include/onyxwasm.h
onyx
progs/poly_test.onyx
src/onyx.c
src/onyxchecker.c
src/onyxsymres.c
src/onyxwasm.c

index fd4a557dad4b86c95b24b195846835607dbbed86..3ff7aae221eec776d50cb217f6380c5fd1639569 100644 (file)
@@ -9,4 +9,8 @@ random :: proc (s := ^seed) -> u32 {
        return *s;
 }
 
-random_between :: proc (lo: i32, hi: i32) -> i32 do return random() % (hi + 1 - lo) + lo;
\ No newline at end of file
+random_between :: proc (lo: i32, hi: i32) -> i32 do return random() % (hi + 1 - lo) + lo;
+
+random_float :: proc (lo := 0.0f, hi := 1.0f) -> f32 {
+    return (cast(f32) (random() % (1 << 20)) / cast(f32) (1 << 20)) * (hi - lo) + lo;
+}
index d11718c28dd0ff41991a8937368312683c83af5f..19b7f2e5fce05992bd0efc6e9205f7636743a365 100644 (file)
--- a/docs/plan
+++ b/docs/plan
@@ -241,7 +241,7 @@ HOW:
                 * slice
                 * dynamic array
 
-        [ ] Don't include foreign functions unless they're used
+        [X] Don't include foreign functions unless they're used
             - Do multiple passes if needed
             - Some APIs like WebGL have a ton of foreigns, and most of them aren't even used
 
index 4ec59f57f2887457f2bb9a7d0a89254e6f8114ea..f32097e87909096dc517efd696b2321bbc0a40bf 100644 (file)
@@ -528,6 +528,7 @@ typedef enum EntityType {
     Entity_Type_Type_Alias,
     Entity_Type_Memory_Reservation,
     Entity_Type_Polymorphic_Proc,
+    Entity_Type_Foreign_Function_Header,
     Entity_Type_Function_Header,
     Entity_Type_Global_Header,
     Entity_Type_Expression,
@@ -572,7 +573,6 @@ typedef struct ProgramInfo {
     bh_table(Package *)   packages;
     bh_arr(Entity)        entities;
 
-    u32 foreign_func_count;
     u32 foreign_global_count;
 } ProgramInfo;
 
index 8c803d7d96f10879d5fa3f18b8d419fd646798a6..4c3e6ea71aa92e5b58e38437445b30651381f22f 100644 (file)
@@ -343,7 +343,6 @@ typedef struct OnyxWasmModule {
     u32 export_count;
     u32 next_type_idx;
     u32 next_func_idx;
-    u32 next_foreign_func_idx;
     u32 next_global_idx;
     u32 next_foreign_global_idx;
     u32 next_datum_offset;
diff --git a/onyx b/onyx
index 63595b46e8f882a0dceb690f5844b2561bdc3630..a9c8ea999d3a663641035153495ea0d2a0a5c435 100755 (executable)
Binary files a/onyx and b/onyx differ
index 217268ed8965323de0c790850ca4430446e195ba..cf256a0a72d3a1f709b02fd4d0e102fa19be4a70 100644 (file)
@@ -68,15 +68,6 @@ get_range :: proc (arr: ^[..] $T) -> range {
     return 0 .. arr.count;
 }
 
-print_range :: proc (r: range) #add_overload print {
-    print(r.low);
-    print(" to ");
-    print(r.high);
-    print(" by ");
-    print(r.step);
-    print("\n");
-}
-
 // NOTE: This function will be very useful for for loops. i.e.
 //        for i: 0 .. 100 |> by(2) {
 //            ...
index 59b6acf36ce9cdc98dce2540872486e9eb1f4638..40608c76a82b26039536c34bd293793b347a14c6 100644 (file)
@@ -229,13 +229,20 @@ static void merge_parse_results(CompilerState* compiler_state, ParseResults* res
 
         switch (nkind) {
             case Ast_Kind_Function: {
-                ent.type     = Entity_Type_Function_Header;
-                ent.function = (AstFunction *) node;
-                bh_arr_push(compiler_state->prog_info.entities, ent);
-
-                ent.type     = Entity_Type_Function;
-                ent.function = (AstFunction *) node;
-                bh_arr_push(compiler_state->prog_info.entities, ent);
+                if ((node->flags & Ast_Flag_Foreign) != 0) {
+                    ent.type     = Entity_Type_Foreign_Function_Header;
+                    ent.function = (AstFunction *) node;
+                    bh_arr_push(compiler_state->prog_info.entities, ent);
+
+                } else {
+                    ent.type     = Entity_Type_Function_Header;
+                    ent.function = (AstFunction *) node;
+                    bh_arr_push(compiler_state->prog_info.entities, ent);
+
+                    ent.type     = Entity_Type_Function;
+                    ent.function = (AstFunction *) node;
+                    bh_arr_push(compiler_state->prog_info.entities, ent);
+                }
                 break;
             }
 
index 56e63db0ec2c66a00191776371731eab22eaade1..5b697a9eefdaeb504a8d39175ebac3389e17ed70 100644 (file)
@@ -1319,10 +1319,8 @@ b32 check_node(AstNode* node) {
 void onyx_type_check() {
     bh_arr_each(Entity, entity, semstate.program->entities) {
         switch (entity->type) {
+            case Entity_Type_Foreign_Function_Header:
             case Entity_Type_Function_Header:
-                if (entity->function->flags & Ast_Flag_Foreign)
-                    semstate.program->foreign_func_count++;
-
                 if (check_function_header(entity->function)) return;
                 break;
 
index 86afa631cc0b146cb67b4bdd9a6014a9ed0f8af0..f023d71c5f1d3e39494ab6af7f4517ebf54d5d40 100644 (file)
@@ -686,8 +686,10 @@ void onyx_resolve_symbols() {
         }
 
         switch (entity->type) {
-            case Entity_Type_Use_Package:         symres_use_package(entity->use_package); break;
+            case Entity_Type_Foreign_Function_Header:
             case Entity_Type_Function:            symres_function(entity->function); break;
+
+            case Entity_Type_Use_Package:         symres_use_package(entity->use_package); break;
             case Entity_Type_Overloaded_Function: symres_overloaded_function(entity->overloaded_function); break;
             case Entity_Type_Global:              symres_global(entity->global); break;
             case Entity_Type_Expression:          symres_expression(&entity->expr); break;
index 4e8fb945b5a6bd2b6d0b6942a6abf89c4f1abdcf..f47dd4aa9e63e31223d18b1d0a34dccd0c807cc6 100644 (file)
@@ -2169,8 +2169,6 @@ static inline b32 should_emit_function(AstFunction* fd) {
     // NOTE: Don't output intrinsic functions
     if (fd->flags & Ast_Flag_Intrinsic) return 0;
 
-    if (fd->flags & Ast_Flag_Foreign) return 1;
-
     // NOTE: Don't output functions that are not used, only if
     // they are also not exported.
     if ((fd->flags & Ast_Flag_Function_Used) == 0) {
@@ -2190,18 +2188,6 @@ static void emit_function(OnyxWasmModule* mod, AstFunction* fd) {
 
     i32 type_idx = generate_type_idx(mod, fd->type);
 
-    if (fd->flags & Ast_Flag_Foreign) {
-        WasmImport import = {
-            .kind = WASM_FOREIGN_FUNCTION,
-            .idx  = type_idx,
-            .mod  = fd->foreign_module,
-            .name = fd->foreign_name,
-        };
-
-        bh_arr_push(mod->imports, import);
-        return;
-    }
-
     WasmFunc wasm_func = {
         .type_idx = type_idx,
         .locals = {
@@ -2281,6 +2267,22 @@ static void emit_function(OnyxWasmModule* mod, AstFunction* fd) {
     bh_imap_clear(&mod->local_map);
 }
 
+static void emit_foreign_function(OnyxWasmModule* mod, AstFunction* fd) {
+    if (!should_emit_function(fd)) return;
+
+    i32 type_idx = generate_type_idx(mod, fd->type);
+
+    WasmImport import = {
+        .kind = WASM_FOREIGN_FUNCTION,
+        .idx  = type_idx,
+        .mod  = fd->foreign_module,
+        .name = fd->foreign_name,
+    };
+
+    bh_arr_push(mod->imports, import);
+    return;
+}
+
 static void emit_global(OnyxWasmModule* module, AstGlobal* global) {
     WasmType global_type = onyx_type_to_wasm_type(global->type);
 
@@ -2519,7 +2521,6 @@ OnyxWasmModule onyx_wasm_module_create(bh_allocator alloc) {
 
         .funcs = NULL,
         .next_func_idx = 0,
-        .next_foreign_func_idx = 0,
 
         .exports = NULL,
         .export_count = 0,
@@ -2568,7 +2569,6 @@ OnyxWasmModule onyx_wasm_module_create(bh_allocator alloc) {
 }
 
 void onyx_wasm_module_compile(OnyxWasmModule* module, ProgramInfo* program) {
-    module->next_func_idx   = program->foreign_func_count;
     module->next_global_idx = program->foreign_global_count;
 
     WasmExport mem_export = {
@@ -2594,18 +2594,15 @@ void onyx_wasm_module_compile(OnyxWasmModule* module, ProgramInfo* program) {
         }
 
         switch (entity->type) {
-            case Entity_Type_Function_Header: {
-                if (!should_emit_function(entity->function)) break;
+            case Entity_Type_Foreign_Function_Header:
+                emit_foreign_function(module, entity->function);
+                // fallthrough
 
-                u64 func_idx;
-                if ((entity->function->flags & Ast_Flag_Foreign) != 0)
-                    func_idx = module->next_foreign_func_idx++;
-                else
-                    func_idx = module->next_func_idx++;
+            case Entity_Type_Function_Header:
+                if (!should_emit_function(entity->function)) break;
 
-                bh_imap_put(&module->index_map, (u64) entity->function, func_idx);
+                bh_imap_put(&module->index_map, (u64) entity->function, module->next_func_idx++);
                 break;
-            }
 
             case Entity_Type_Global_Header: {
                 u64 global_idx;