From 58d1c95241b4f44ad7673aed2973fa5ec784fea9 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 8 Mar 2022 22:12:13 -0600 Subject: [PATCH] added simple optimization for single element structures --- src/types.c | 12 ++++++++++++ src/wasm_emit.c | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/types.c b/src/types.c index af04627b..cd913e5e 100644 --- a/src/types.c +++ b/src/types.c @@ -1284,6 +1284,18 @@ b32 type_is_numeric(Type* type) { b32 type_is_compound(Type* type) { if (type == NULL) return 0; + + if (type->kind == Type_Kind_Struct) { + // + // This is for the kind of common case where a structure simply wraps a + // single non-compound value; in this situation, the structure can be + // "dissolved" at compile-time and turn into the underlying type. + // + + if (bh_arr_length(type->Struct.linear_members) != 1) return 1; + return type_is_compound(type->Struct.linear_members[0].type); + } + return type->kind != Type_Kind_Basic && type->kind != Type_Kind_Pointer && type->kind != Type_Kind_Enum diff --git a/src/wasm_emit.c b/src/wasm_emit.c index 281840aa..38f88518 100644 --- a/src/wasm_emit.c +++ b/src/wasm_emit.c @@ -633,6 +633,11 @@ EMIT_FUNC(store_instruction, Type* type, u32 offset) { return; } + if (type->kind == Type_Kind_Struct) { + assert(bh_arr_length(type->Struct.linear_members) == 1); + type = type->Struct.linear_members[0].type; + } + if (type->kind == Type_Kind_Array) { emit_array_store(mod, pcode, type, offset); return; @@ -678,6 +683,11 @@ EMIT_FUNC(load_instruction, Type* type, u32 offset) { return; } + if (type->kind == Type_Kind_Struct) { + assert(bh_arr_length(type->Struct.linear_members) == 1); + type = type->Struct.linear_members[0].type; + } + if (type->kind == Type_Kind_Array) { if (offset != 0) { WID(WI_PTR_CONST, offset); @@ -3205,6 +3215,10 @@ static i32 generate_type_idx(OnyxWasmModule* mod, Type* ft) { // HACK: Slightly: the wasm type for structs has to be 0x00 WasmType return_type = onyx_type_to_wasm_type(ft->Function.return_type); + if (ft->Function.return_type->kind == Type_Kind_Struct && !type_is_compound(ft->Function.return_type)) { + return_type = onyx_type_to_wasm_type(ft->Function.return_type->Struct.linear_members[0].type); + } + *(t++) = (char) return_type; *t = '\0'; -- 2.25.1