diff --git a/compiler/builtins/src/bitcode.rs b/compiler/builtins/src/bitcode.rs index e1a2a9f8f4..9321d08637 100644 --- a/compiler/builtins/src/bitcode.rs +++ b/compiler/builtins/src/bitcode.rs @@ -1,4 +1,5 @@ use roc_module::symbol::Symbol; +use roc_target::TargetInfo; use std::ops::Index; pub const BUILTINS_HOST_OBJ_PATH: &str = env!( @@ -46,7 +47,7 @@ impl FloatWidth { } } - pub const fn alignment_bytes(&self) -> u32 { + pub const fn alignment_bytes(&self, target_info: TargetInfo) -> u32 { use std::mem::align_of; use FloatWidth::*; @@ -106,11 +107,10 @@ impl IntWidth { } } - pub const fn alignment_bytes(&self) -> u32 { + pub const fn alignment_bytes(&self, target_info: TargetInfo) -> u32 { use std::mem::align_of; use IntWidth::*; - // TODO actually alignment is architecture-specific match self { U8 | I8 => align_of::() as u32, U16 | I16 => align_of::() as u32, diff --git a/compiler/mono/src/layout.rs b/compiler/mono/src/layout.rs index 12d759f8b8..2055b03fd2 100644 --- a/compiler/mono/src/layout.rs +++ b/compiler/mono/src/layout.rs @@ -1341,10 +1341,10 @@ impl<'a> Builtin<'a> { // since both of those are one pointer size, the alignment of that structure is a pointer // size match self { - Int(int_width) => int_width.alignment_bytes(), - Float(float_width) => float_width.alignment_bytes(), + Int(int_width) => int_width.alignment_bytes(target_info), + Float(float_width) => float_width.alignment_bytes(target_info), Bool => align_of::() as u32, - Decimal => IntWidth::I128.alignment_bytes(), + Decimal => IntWidth::I128.alignment_bytes(target_info), Dict(_, _) => ptr_width, Set(_) => ptr_width, // we often treat these as i128 (64-bit systems) diff --git a/compiler/mono/src/layout_soa.rs b/compiler/mono/src/layout_soa.rs index 1481b38723..fe37cc385d 100644 --- a/compiler/mono/src/layout_soa.rs +++ b/compiler/mono/src/layout_soa.rs @@ -3,6 +3,7 @@ use roc_builtins::bitcode::{FloatWidth, IntWidth}; use roc_collections::all::MutMap; use roc_module::ident::TagName; use roc_module::symbol::Symbol; +use roc_target::TargetInfo; use roc_types::subs::{Content, FlatType, Subs, Variable}; use roc_types::types::RecordField; use std::collections::hash_map::Entry; @@ -99,7 +100,7 @@ pub struct Layouts { lambda_sets: Vec, symbols: Vec, recursion_variable_to_structure_variable_map: MutMap>, - usize_int_width: IntWidth, + target_info: TargetInfo, } pub struct FunctionLayout { @@ -402,7 +403,7 @@ impl Layouts { const VOID_TUPLE: Index<(Layout, Layout)> = Index::new(0); const UNIT_INDEX: Index = Index::new(2); - pub fn new(usize_int_width: IntWidth) -> Self { + pub fn new(target_info: TargetInfo) -> Self { let mut layouts = Vec::with_capacity(64); layouts.push(Layout::VOID); @@ -420,7 +421,7 @@ impl Layouts { lambda_sets: Vec::default(), symbols: Vec::default(), recursion_variable_to_structure_variable_map: MutMap::default(), - usize_int_width, + target_info, } } @@ -443,7 +444,12 @@ impl Layouts { } fn usize(&self) -> Layout { - Layout::Int(self.usize_int_width) + let usize_int_width = match self.target_info.ptr_width() { + roc_target::PtrWidth::Bytes4 => IntWidth::U32, + roc_target::PtrWidth::Bytes8 => IntWidth::U64, + }; + + Layout::Int(usize_int_width) } fn align_of_layout_index(&self, index: Index) -> u16 { @@ -453,18 +459,23 @@ impl Layouts { } fn align_of_layout(&self, layout: Layout) -> u16 { - let ptr_alignment = self.usize_int_width.alignment_bytes() as u16; + let usize_int_width = match self.target_info.ptr_width() { + roc_target::PtrWidth::Bytes4 => IntWidth::U32, + roc_target::PtrWidth::Bytes8 => IntWidth::U64, + }; + + let ptr_alignment = usize_int_width.alignment_bytes(self.target_info) as u16; match layout { Layout::Reserved => unreachable!(), - Layout::Int(int_width) => int_width.alignment_bytes() as u16, - Layout::Float(float_width) => float_width.alignment_bytes() as u16, - Layout::Decimal => IntWidth::U128.alignment_bytes() as u16, + Layout::Int(int_width) => int_width.alignment_bytes(self.target_info) as u16, + Layout::Float(float_width) => float_width.alignment_bytes(self.target_info) as u16, + Layout::Decimal => IntWidth::U128.alignment_bytes(self.target_info) as u16, Layout::Str | Layout::Dict(_) | Layout::Set(_) | Layout::List(_) => ptr_alignment, Layout::Struct(slice) => self.align_of_layout_slice(slice), Layout::Boxed(_) | Layout::UnionRecursive(_) => ptr_alignment, Layout::UnionNonRecursive(slices) => { - let tag_id_align = IntWidth::I64.alignment_bytes() as u16; + let tag_id_align = IntWidth::I64.alignment_bytes(self.target_info) as u16; self.align_of_layout_slices(slices).max(tag_id_align) } @@ -518,7 +529,12 @@ impl Layouts { } pub fn size_of_layout(&self, layout: Layout) -> u16 { - let ptr_width = self.usize_int_width.stack_size() as u16; + let usize_int_width = match self.target_info.ptr_width() { + roc_target::PtrWidth::Bytes4 => IntWidth::U32, + roc_target::PtrWidth::Bytes8 => IntWidth::U64, + }; + + let ptr_width = usize_int_width.stack_size() as u16; match layout { Layout::Reserved => unreachable!(),