Skip to content

Commit

Permalink
Add Program#alignment_byte_width
Browse files Browse the repository at this point in the history
Don't hardcode the alignment to 4 or 8 when the actual value depends on
the actual target (e.g. it's always 1 for AVR targets).
  • Loading branch information
ysbaddaden committed Apr 8, 2024
1 parent 07ab1c6 commit f1b5feb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/primitives.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ class Crystal::CodeGenVisitor
when CharType
inst.alignment = 4
else
inst.alignment = @program.bits64? ? 8 : 4
inst.alignment = @program.alignment_byte_width
end
end

Expand Down
13 changes: 13 additions & 0 deletions src/compiler/crystal/codegen/target.cr
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ class Crystal::Codegen::Target
end
end

def alignment_byte_width
case @architecture
when "aarch64", "x86_64"
8
when "arm", "i386", "wasm32"
4
when "avr"
1
else
raise "BUG: unknown Target#alignment_byte_width for #{@architecture} target architecture"
end
end

def os_name
case self
when .macos?
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/crystal/semantic/flags.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class Crystal::Program
codegen_target.size_bit_width
end

def alignment_byte_width
codegen_target.alignment_byte_width
end

private def flags_for_target(target)
flags = Set(String).new

Expand Down

0 comments on commit f1b5feb

Please sign in to comment.