Skip to content

Commit

Permalink
Work on #14, can now return small (<= 4 bytes) structs from functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DontBelieveMe committed Jan 11, 2022
1 parent 518fc16 commit 57fd952
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/regalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ static bool CanTypeFitInNativeRegister(const Type* type)
return true;
}

if (type->IsStruct()) {
const size_t structSize = ARMv7::TypeSize(type);
return structSize <= 4;
}

return false;
}

Expand Down Expand Up @@ -77,6 +82,10 @@ static const Type* LowerVirtualRegisterType(const Type* type) {
}
else if (type->IsIntegral()) {
return type;
} else if (type->IsStruct()) {
if (ARMv7::TypeSize(type) <= 4) {
return BuiltinTypes::GetInt32();
}
}

helix_unreachable("type cannot be lowered into a single physical register");
Expand Down Expand Up @@ -303,7 +312,7 @@ void RegisterAllocator::Execute(Function* fn)
//where = head->InsertAfter(where, Helix::CreateIntToPtr(BuiltinTypes::GetInt32(), temp, output_ptr));
//
//
Instruction* lastInsn = output_ptr->GetUse(0).GetInstruction();
//Instruction* lastInsn = output_ptr->GetUse(0).GetInstruction();
int c = 0;

struct T { Use use; PhysicalRegisterName* reg; };
Expand Down
10 changes: 10 additions & 0 deletions testsuite/f5/f5013-cconv-return-small-struct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
struct MyStruct
{
int a;
};

struct MyStruct get_small_struct()
{
struct MyStruct a = { 33 };
return a;
}
32 changes: 32 additions & 0 deletions testsuite/f5/f5013-cconv-return-small-struct.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Test>
<Flags>--emit-ir-post=cconv -c</Flags>

<TestFlags>
<TestFlag name="regex" value="false"></TestFlag>
</TestFlags>

<ExpectedOutput>
MyStruct = struct { i32 }

@ci0:ptr = global i32 0:i32
@ci1:ptr = global i32 33:i32

function get_small_struct(): void {
.0:
stack_alloc [MyStruct x 1], %0:ptr
stack_alloc [MyStruct x 1], %1:ptr
ptrtoint [ptr -> i32], %1:ptr, %2:i32
load @ci0:ptr, %3:i32
iadd %2:i32, %3:i32, %4:i32
inttoptr [i32 -> ptr], %4:i32, %5:ptr
load @ci1:ptr, %6:i32
store %6:i32, %5:ptr
load %1:ptr, %7:MyStruct
store %7:MyStruct, %0:ptr
br .1
.1:
load %0:ptr, r0:i32
ret
}
</ExpectedOutput>
</Test>

0 comments on commit 57fd952

Please sign in to comment.