Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions spec/std/llvm/x86_64_abi_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,30 @@ class LLVM::ABI
info.arg_types[0].should eq(ArgType.indirect(str, Attribute::ByVal))
info.return_type.should eq(ArgType.indirect(str, Attribute::StructRet))
end

test "does with packed struct containing unaligned fields (#9873)" do |abi, ctx|
str = ctx.struct([ctx.int8, ctx.int16], packed: true)
arg_types = [str]
return_type = str

info = abi.abi_info(arg_types, return_type, true, ctx)
info.arg_types.size.should eq(1)

info.arg_types[0].should eq(ArgType.indirect(str, Attribute::ByVal))
info.return_type.should eq(ArgType.indirect(str, Attribute::StructRet))
end

test "does with packed struct not containing unaligned fields" do |abi, ctx|
str = ctx.struct([ctx.int16, ctx.int8], packed: true)
arg_types = [str]
return_type = str

info = abi.abi_info(arg_types, return_type, true, ctx)
info.arg_types.size.should eq(1)

info.arg_types[0].should eq(ArgType.direct(str, cast: ctx.struct([ctx.int64])))
info.return_type.should eq(ArgType.direct(str, cast: ctx.struct([ctx.int64])))
end
end
{% end %}
end
Expand Down
12 changes: 11 additions & 1 deletion src/llvm/abi/x86_64.cr
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class LLVM::ABI::X86_64 < LLVM::ABI
def classify(type)
words = (size(type) + 7) // 8
reg_classes = Array.new(words, RegClass::NoClass)
if words > 4
if words > 4 || has_misaligned_fields?(type)
all_mem(reg_classes)
else
classify(type, reg_classes, 0, 0)
Expand Down Expand Up @@ -275,6 +275,16 @@ class LLVM::ABI::X86_64 < LLVM::ABI
size(type, 8)
end

def has_misaligned_fields?(type : Type) : Bool
return false unless type.packed_struct?
offset = 0
type.struct_element_types.each do |elem|
return true unless offset.divisible_by?(align(elem))
offset += size(elem)
end
false
end

enum RegClass
NoClass
Int
Expand Down