provide target info to number alignment function

This commit is contained in:
Folkert 2022-01-26 17:19:53 +01:00
parent 0298013346
commit fbd26c598e
3 changed files with 32 additions and 16 deletions

View file

@ -1,4 +1,5 @@
use roc_module::symbol::Symbol; use roc_module::symbol::Symbol;
use roc_target::TargetInfo;
use std::ops::Index; use std::ops::Index;
pub const BUILTINS_HOST_OBJ_PATH: &str = env!( 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 std::mem::align_of;
use FloatWidth::*; 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 std::mem::align_of;
use IntWidth::*; use IntWidth::*;
// TODO actually alignment is architecture-specific
match self { match self {
U8 | I8 => align_of::<i8>() as u32, U8 | I8 => align_of::<i8>() as u32,
U16 | I16 => align_of::<i16>() as u32, U16 | I16 => align_of::<i16>() as u32,

View file

@ -1341,10 +1341,10 @@ impl<'a> Builtin<'a> {
// since both of those are one pointer size, the alignment of that structure is a pointer // since both of those are one pointer size, the alignment of that structure is a pointer
// size // size
match self { match self {
Int(int_width) => int_width.alignment_bytes(), Int(int_width) => int_width.alignment_bytes(target_info),
Float(float_width) => float_width.alignment_bytes(), Float(float_width) => float_width.alignment_bytes(target_info),
Bool => align_of::<bool>() as u32, Bool => align_of::<bool>() as u32,
Decimal => IntWidth::I128.alignment_bytes(), Decimal => IntWidth::I128.alignment_bytes(target_info),
Dict(_, _) => ptr_width, Dict(_, _) => ptr_width,
Set(_) => ptr_width, Set(_) => ptr_width,
// we often treat these as i128 (64-bit systems) // we often treat these as i128 (64-bit systems)

View file

@ -3,6 +3,7 @@ use roc_builtins::bitcode::{FloatWidth, IntWidth};
use roc_collections::all::MutMap; use roc_collections::all::MutMap;
use roc_module::ident::TagName; use roc_module::ident::TagName;
use roc_module::symbol::Symbol; use roc_module::symbol::Symbol;
use roc_target::TargetInfo;
use roc_types::subs::{Content, FlatType, Subs, Variable}; use roc_types::subs::{Content, FlatType, Subs, Variable};
use roc_types::types::RecordField; use roc_types::types::RecordField;
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
@ -99,7 +100,7 @@ pub struct Layouts {
lambda_sets: Vec<LambdaSet>, lambda_sets: Vec<LambdaSet>,
symbols: Vec<Symbol>, symbols: Vec<Symbol>,
recursion_variable_to_structure_variable_map: MutMap<Variable, Index<Layout>>, recursion_variable_to_structure_variable_map: MutMap<Variable, Index<Layout>>,
usize_int_width: IntWidth, target_info: TargetInfo,
} }
pub struct FunctionLayout { pub struct FunctionLayout {
@ -402,7 +403,7 @@ impl Layouts {
const VOID_TUPLE: Index<(Layout, Layout)> = Index::new(0); const VOID_TUPLE: Index<(Layout, Layout)> = Index::new(0);
const UNIT_INDEX: Index<Layout> = Index::new(2); const UNIT_INDEX: Index<Layout> = 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); let mut layouts = Vec::with_capacity(64);
layouts.push(Layout::VOID); layouts.push(Layout::VOID);
@ -420,7 +421,7 @@ impl Layouts {
lambda_sets: Vec::default(), lambda_sets: Vec::default(),
symbols: Vec::default(), symbols: Vec::default(),
recursion_variable_to_structure_variable_map: MutMap::default(), recursion_variable_to_structure_variable_map: MutMap::default(),
usize_int_width, target_info,
} }
} }
@ -443,7 +444,12 @@ impl Layouts {
} }
fn usize(&self) -> Layout { 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<Layout>) -> u16 { fn align_of_layout_index(&self, index: Index<Layout>) -> u16 {
@ -453,18 +459,23 @@ impl Layouts {
} }
fn align_of_layout(&self, layout: Layout) -> u16 { 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 { match layout {
Layout::Reserved => unreachable!(), Layout::Reserved => unreachable!(),
Layout::Int(int_width) => int_width.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() as u16, Layout::Float(float_width) => float_width.alignment_bytes(self.target_info) as u16,
Layout::Decimal => IntWidth::U128.alignment_bytes() as u16, Layout::Decimal => IntWidth::U128.alignment_bytes(self.target_info) as u16,
Layout::Str | Layout::Dict(_) | Layout::Set(_) | Layout::List(_) => ptr_alignment, Layout::Str | Layout::Dict(_) | Layout::Set(_) | Layout::List(_) => ptr_alignment,
Layout::Struct(slice) => self.align_of_layout_slice(slice), Layout::Struct(slice) => self.align_of_layout_slice(slice),
Layout::Boxed(_) | Layout::UnionRecursive(_) => ptr_alignment, Layout::Boxed(_) | Layout::UnionRecursive(_) => ptr_alignment,
Layout::UnionNonRecursive(slices) => { 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) 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 { 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 { match layout {
Layout::Reserved => unreachable!(), Layout::Reserved => unreachable!(),