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;
+}
* 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
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,
bh_table(Package *) packages;
bh_arr(Entity) entities;
- u32 foreign_func_count;
u32 foreign_global_count;
} ProgramInfo;
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;
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) {
// ...
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;
}
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;
}
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;
// 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) {
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 = {
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);
.funcs = NULL,
.next_func_idx = 0,
- .next_foreign_func_idx = 0,
.exports = NULL,
.export_count = 0,
}
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 = {
}
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;