Skip to content
Open
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
26 changes: 26 additions & 0 deletions spec/compiler/codegen/abi/x86_64_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,32 @@ class Crystal::ABI
info.arg_types[0].should eq(ArgType.indirect(str, LLVM::Attribute::ByVal))
info.return_type.should eq(ArgType.indirect(str, LLVM::Attribute::StructRet))
end

test "does with non-packed struct containing packed struct with unaligned fields" do |abi, ctx|
inner = ctx.struct([ctx.int16, ctx.int8], packed: true)
str = ctx.struct([ctx.int8, inner])
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, LLVM::Attribute::ByVal))
info.return_type.should eq(ArgType.indirect(str, LLVM::Attribute::StructRet))
end

test "does with non-packed struct containing single-element array of packed struct with unaligned fields" do |abi, ctx|
inner = ctx.struct([ctx.int16, ctx.int8], packed: true).array(1)
str = ctx.struct([ctx.int8, inner])
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, LLVM::Attribute::ByVal))
info.return_type.should eq(ArgType.indirect(str, LLVM::Attribute::StructRet))
end
end
{% end %}
end
Expand Down
7 changes: 4 additions & 3 deletions src/compiler/crystal/codegen/abi/x86_64.cr
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Crystal::ABI::X86_64 < Crystal::ABI
i = off // 8
e = (off + t_size + 7) // 8
while i < e
unify(cls, ix + 1, RegClass::Memory)
unify(cls, ix + i, RegClass::Memory)
i += 1
end
return
Expand Down Expand Up @@ -278,9 +278,10 @@ class Crystal::ABI::X86_64 < Crystal::ABI
def has_misaligned_fields?(type : LLVM::Type, offset : Int = 0) : Bool
case type.kind
when LLVM::Type::Kind::Struct
return false unless type.packed_struct?
type.struct_element_types.each do |elem|
offset = align_offset(offset, elem) unless type.packed_struct?
return true unless offset.divisible_by?(align(elem))
return true if has_misaligned_fields?(elem, offset)
offset += size(elem)
end
false
Expand All @@ -301,7 +302,7 @@ class Crystal::ABI::X86_64 < Crystal::ABI
# misaligned fields, then `size(elem) % align(elem) == 0` must be true,
# meaning array indices have no effect on element alignment.
elem = type.element_type
has_misaligned_fields?(elem) || type.array_size > 1 && has_misaligned_fields?(elem, size(elem))
has_misaligned_fields?(elem, offset) || type.array_size > 1 && has_misaligned_fields?(elem, offset + size(elem))
else
false
end
Expand Down
7 changes: 4 additions & 3 deletions src/llvm/abi/x86_64.cr
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class LLVM::ABI::X86_64 < LLVM::ABI
i = off // 8
e = (off + t_size + 7) // 8
while i < e
unify(cls, ix + 1, RegClass::Memory)
unify(cls, ix + i, RegClass::Memory)
i += 1
end
return
Expand Down Expand Up @@ -279,9 +279,10 @@ class LLVM::ABI::X86_64 < LLVM::ABI
def has_misaligned_fields?(type : Type, offset : Int = 0) : Bool
case type.kind
when Type::Kind::Struct
return false unless type.packed_struct?
type.struct_element_types.each do |elem|
offset = align_offset(offset, elem) unless type.packed_struct?
return true unless offset.divisible_by?(align(elem))
return true if has_misaligned_fields?(elem, offset)
offset += size(elem)
end
false
Expand All @@ -302,7 +303,7 @@ class LLVM::ABI::X86_64 < LLVM::ABI
# misaligned fields, then `size(elem) % align(elem) == 0` must be true,
# meaning array indices have no effect on element alignment.
elem = type.element_type
has_misaligned_fields?(elem) || type.array_size > 1 && has_misaligned_fields?(elem, size(elem))
has_misaligned_fields?(elem, offset) || type.array_size > 1 && has_misaligned_fields?(elem, offset + size(elem))
else
false
end
Expand Down
Loading