From: Brendan Hansen Date: Fri, 18 Sep 2020 19:17:40 +0000 (-0500) Subject: code cleanup X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=cdbb34e661bbc9c96e25e09194d80f8200392092;p=onyx.git code cleanup --- diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 02646b6b..603b5a26 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -429,7 +429,7 @@ struct AstBinOp { AstTyped_base; BinaryOp operation; AstTyped *left, *ri struct AstUnaryOp { AstTyped_base; UnaryOp operation; AstTyped *expr; }; struct AstNumLit { AstTyped_base; union { i32 i; i64 l; f32 f; f64 d; } value; }; struct AstStrLit { AstTyped_base; u64 addr; u64 length; }; -struct AstLocal { AstTyped_base; ParamPassType ppt; }; +struct AstLocal { AstTyped_base; }; struct AstCall { AstTyped_base; AstArgument *arguments; u64 arg_count; AstTyped *callee; }; struct AstIntrinsicCall { AstTyped_base; AstArgument *arguments; u64 arg_count; OnyxIntrinsic intrinsic; }; struct AstArgument { AstTyped_base; AstTyped *value; }; @@ -810,4 +810,9 @@ static inline CallingConvention type_function_get_cc(Type* type) { return CC_Return_Wasm; } +static inline ParamPassType type_get_param_pass(Type* type) { + if (type_is_structlike_strict(type) && !type_structlike_is_simple(type)) return Param_Pass_By_Implicit_Pointer; + return Param_Pass_By_Value; +} + #endif // #ifndef ONYXASTNODES_H diff --git a/onyx b/onyx index c41cd073..ead1c3ce 100755 Binary files a/onyx and b/onyx differ diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 6e4510d6..7c238aff 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -1268,14 +1268,6 @@ b32 check_function_header(AstFunction* func) { return 1; } - local->ppt = Param_Pass_By_Value; - - if (type_is_structlike_strict(local->type) - && !type_structlike_is_simple(local->type)) { - // local->type = type_make_pointer(semstate.node_allocator, local->type); - local->ppt = Param_Pass_By_Implicit_Pointer; - } - if (param->is_vararg) has_had_varargs = 1; if (local->type->kind != Type_Kind_Array diff --git a/src/onyxwasm.c b/src/onyxwasm.c index df11a30f..2ea7e1bc 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -1306,29 +1306,27 @@ EMIT_FUNC(call, AstCall* call) { for (AstArgument *arg = call->arguments; arg != NULL; arg = (AstArgument *) arg->next) { - if ((arg->flags & Ast_Flag_Arg_Is_VarArg) && !type_is_structlike(arg->type)) { - WID(WI_GLOBAL_GET, stack_top_idx); - } - emit_expression(mod, &code, arg->value); + b32 place_on_stack = 0; + b32 arg_is_struct = type_is_structlike(arg->type); - if (arg->flags & Ast_Flag_Arg_Is_VarArg) { - if (type_is_structlike(arg->type)) WID(WI_GLOBAL_GET, stack_top_idx); + if (arg->flags & Ast_Flag_Arg_Is_VarArg) place_on_stack = 1; + if (type_get_param_pass(arg->type) == Param_Pass_By_Implicit_Pointer) place_on_stack = 1; - emit_store_instruction(mod, &code, arg->type, stack_grow_amm); + if (place_on_stack && !arg_is_struct) WID(WI_GLOBAL_GET, stack_top_idx); - stack_grow_amm += type_size_of(arg->type); - vararg_count += 1; - } + emit_expression(mod, &code, arg->value); - // CLEANUP structs-by-value - else if (type_is_structlike_strict(arg->type) && !type_structlike_is_simple(arg->type)) { - WID(WI_GLOBAL_GET, stack_top_idx); + if (place_on_stack) { + if (arg_is_struct) WID(WI_GLOBAL_GET, stack_top_idx); emit_store_instruction(mod, &code, arg->type, stack_grow_amm); - WID(WI_GLOBAL_GET, stack_top_idx); - WID(WI_I32_CONST, stack_grow_amm); - WI(WI_I32_ADD); + if (arg->flags & Ast_Flag_Arg_Is_VarArg) vararg_count += 1; + else { + WID(WI_GLOBAL_GET, stack_top_idx); + WID(WI_I32_CONST, stack_grow_amm); + WI(WI_I32_ADD); + } stack_grow_amm += type_size_of(arg->type); } @@ -2040,7 +2038,7 @@ EMIT_FUNC(expression, AstTyped* expr) { AstLocal* param = (AstLocal *) expr; u64 localidx = bh_imap_get(&mod->local_map, (u64) param); - switch (param->ppt) { + switch (type_get_param_pass(param->type)) { case Param_Pass_By_Value: { if (type_is_structlike_strict(expr->type)) { u32 mem_count = type_structlike_mem_count(expr->type); @@ -2170,7 +2168,7 @@ EMIT_FUNC(expression, AstTyped* expr) { if (field->expr->kind == Ast_Kind_Param) { AstLocal* param = (AstLocal *) field->expr; - if (param->ppt == Param_Pass_By_Value && !type_is_pointer(param->type)) { + if (type_get_param_pass(param->type) == Param_Pass_By_Value && !type_is_pointer(param->type)) { u64 localidx = bh_imap_get(&mod->local_map, (u64) field->expr) + field->idx; WIL(WI_LOCAL_GET, localidx); break; @@ -2483,19 +2481,18 @@ static i32 generate_type_idx(OnyxWasmModule* mod, Type* ft) { if (ft->kind != Type_Kind_Function) return -1; static char type_repr_buf[128]; - char* t = type_repr_buf; + Type** param_type = ft->Function.params; i32 param_count = ft->Function.param_count; i32 params_left = param_count; + while (params_left-- > 0) { - if (type_is_structlike_strict(*param_type)) { - if (!type_structlike_is_simple(*param_type)) { - // HACK!! - // CLEANUP structs-by-value - *(t++) = WASM_TYPE_INT32; + if (type_get_param_pass(*param_type) == Param_Pass_By_Implicit_Pointer) { + *(t++) = (char) onyx_type_to_wasm_type(&basic_types[Basic_Kind_Rawptr]); - } else { + } else { + if (type_is_structlike_strict(*param_type)) { u32 mem_count = type_structlike_mem_count(*param_type); StructMember smem; @@ -2505,10 +2502,10 @@ static i32 generate_type_idx(OnyxWasmModule* mod, Type* ft) { } param_count += mem_count - 1; - } - } else { - *(t++) = (char) onyx_type_to_wasm_type(*param_type); + } else { + *(t++) = (char) onyx_type_to_wasm_type(*param_type); + } } param_type++; @@ -2618,7 +2615,7 @@ static void emit_function(OnyxWasmModule* mod, AstFunction* fd) { // NOTE: Generate the local map u64 localidx = 0; bh_arr_each(AstParam, param, fd->params) { - switch (param->local->ppt) { + switch (type_get_param_pass(param->local->type)) { case Param_Pass_By_Value: { if (type_is_structlike_strict(param->local->type)) { bh_imap_put(&mod->local_map, (u64) param->local, localidx | LOCAL_IS_WASM);