From: Brendan Hansen Date: Sun, 6 Sep 2020 15:08:50 +0000 (-0500) Subject: reduced number of foreign imports X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=65103b831fd97be78871602182f7d5ec7ad7917c;p=onyx.git reduced number of foreign imports --- diff --git a/core/random.onyx b/core/random.onyx index fd4a557d..3ff7aae2 100644 --- a/core/random.onyx +++ b/core/random.onyx @@ -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; +} diff --git a/docs/plan b/docs/plan index d11718c2..19b7f2e5 100644 --- 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 diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 4ec59f57..f32097e8 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -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; diff --git a/include/onyxwasm.h b/include/onyxwasm.h index 8c803d7d..4c3e6ea7 100644 --- a/include/onyxwasm.h +++ b/include/onyxwasm.h @@ -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 63595b46..a9c8ea99 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/poly_test.onyx b/progs/poly_test.onyx index 217268ed..cf256a0a 100644 --- a/progs/poly_test.onyx +++ b/progs/poly_test.onyx @@ -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) { // ... diff --git a/src/onyx.c b/src/onyx.c index 59b6acf3..40608c76 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -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; } diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 56e63db0..5b697a9e 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -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; diff --git a/src/onyxsymres.c b/src/onyxsymres.c index 86afa631..f023d71c 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -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; diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 4e8fb945..f47dd4aa 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -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;