added simple optimization for single element structures
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 9 Mar 2022 04:12:13 +0000 (22:12 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 9 Mar 2022 04:12:13 +0000 (22:12 -0600)
src/types.c
src/wasm_emit.c

index af04627b643c38843eb2f53fae8ce67e0b2bb161..cd913e5e0523f49921815309b39640e6d91c6748 100644 (file)
@@ -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
index 281840aa31b8275632ca98b6d9cb0445d2b67a26..38f88518adf5dd81086d741f3726484258378c4a 100644 (file)
@@ -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';