From 0ed259a80dc367a014d4ef0733e699f8108abefe Mon Sep 17 00:00:00 2001 From: Folkert Date: Wed, 26 Jan 2022 14:37:32 +0100 Subject: [PATCH] phase 3 --- compiler/mono/src/code_gen_help/equality.rs | 4 +++- compiler/mono/src/code_gen_help/mod.rs | 10 +++++----- compiler/mono/src/code_gen_help/refcount.rs | 20 +++++++++++--------- compiler/mono/src/ir.rs | 16 ++++++++-------- compiler/mono/src/layout.rs | 7 +++++++ 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/compiler/mono/src/code_gen_help/equality.rs b/compiler/mono/src/code_gen_help/equality.rs index db14a1e3b9..d58d095274 100644 --- a/compiler/mono/src/code_gen_help/equality.rs +++ b/compiler/mono/src/code_gen_help/equality.rs @@ -590,7 +590,9 @@ fn eq_list<'a>( // let size = literal int let size = root.create_symbol(ident_ids, "size"); - let size_expr = Expr::Literal(Literal::Int(elem_layout.stack_size(root.ptr_size) as i128)); + let size_expr = Expr::Literal(Literal::Int( + elem_layout.stack_size(root.target_info) as i128 + )); let size_stmt = |next| Stmt::Let(size, size_expr, layout_isize, next); // let list_size = len_1 * size diff --git a/compiler/mono/src/code_gen_help/mod.rs b/compiler/mono/src/code_gen_help/mod.rs index 61c4c32c9e..e74e4058ed 100644 --- a/compiler/mono/src/code_gen_help/mod.rs +++ b/compiler/mono/src/code_gen_help/mod.rs @@ -1,9 +1,9 @@ use bumpalo::collections::vec::Vec; use bumpalo::Bump; -use roc_builtins::bitcode::IntWidth; use roc_module::ident::Ident; use roc_module::low_level::LowLevel; use roc_module::symbol::{IdentIds, ModuleId, Symbol}; +use roc_target::TargetInfo; use crate::ir::{ Call, CallSpecId, CallType, Expr, HostExposedLayouts, JoinPointId, ModifyRc, Proc, ProcLayout, @@ -74,19 +74,19 @@ pub struct Context<'a> { pub struct CodeGenHelp<'a> { arena: &'a Bump, home: ModuleId, - ptr_size: u32, + target_info: TargetInfo, layout_isize: Layout<'a>, specializations: Vec<'a, Specialization<'a>>, debug_recursion_depth: usize, } impl<'a> CodeGenHelp<'a> { - pub fn new(arena: &'a Bump, intwidth_isize: IntWidth, home: ModuleId) -> Self { + pub fn new(arena: &'a Bump, target_info: TargetInfo, home: ModuleId) -> Self { CodeGenHelp { arena, home, - ptr_size: intwidth_isize.stack_size(), - layout_isize: Layout::Builtin(Builtin::Int(intwidth_isize)), + target_info, + layout_isize: Layout::usize(target_info), specializations: Vec::with_capacity_in(16, arena), debug_recursion_depth: 0, } diff --git a/compiler/mono/src/code_gen_help/refcount.rs b/compiler/mono/src/code_gen_help/refcount.rs index 2bc4aeff92..272e503d18 100644 --- a/compiler/mono/src/code_gen_help/refcount.rs +++ b/compiler/mono/src/code_gen_help/refcount.rs @@ -207,7 +207,7 @@ pub fn rc_ptr_from_data_ptr<'a>( // Mask for lower bits (for tag union id) let mask_sym = root.create_symbol(ident_ids, "mask"); - let mask_expr = Expr::Literal(Literal::Int(-(root.ptr_size as i128))); + let mask_expr = Expr::Literal(Literal::Int(-(root.target_info.ptr_width() as i128))); let mask_stmt = |next| Stmt::Let(mask_sym, mask_expr, root.layout_isize, next); let masked_sym = root.create_symbol(ident_ids, "masked"); @@ -222,7 +222,7 @@ pub fn rc_ptr_from_data_ptr<'a>( // Pointer size constant let ptr_size_sym = root.create_symbol(ident_ids, "ptr_size"); - let ptr_size_expr = Expr::Literal(Literal::Int(root.ptr_size as i128)); + let ptr_size_expr = Expr::Literal(Literal::Int(root.target_info.ptr_width() as i128)); let ptr_size_stmt = |next| Stmt::Let(ptr_size_sym, ptr_size_expr, root.layout_isize, next); // Refcount address @@ -382,7 +382,7 @@ fn refcount_str<'a>( // A pointer to the refcount value itself let rc_ptr = root.create_symbol(ident_ids, "rc_ptr"); - let alignment = root.ptr_size; + let alignment = root.target_info.ptr_width() as u32; let ret_unit_stmt = rc_return_stmt(root, ident_ids, ctx); let mod_rc_stmt = modify_refcount( @@ -487,7 +487,7 @@ fn refcount_list<'a>( // let rc_ptr = root.create_symbol(ident_ids, "rc_ptr"); - let alignment = layout.alignment_bytes(root.ptr_size); + let alignment = layout.alignment_bytes(root.target_info); let ret_stmt = rc_return_stmt(root, ident_ids, ctx); let modify_list = modify_refcount( @@ -584,7 +584,9 @@ fn refcount_list_elems<'a>( // let size = literal int let elem_size = root.create_symbol(ident_ids, "elem_size"); - let elem_size_expr = Expr::Literal(Literal::Int(elem_layout.stack_size(root.ptr_size) as i128)); + let elem_size_expr = Expr::Literal(Literal::Int( + elem_layout.stack_size(root.target_info) as i128 + )); let elem_size_stmt = |next| Stmt::Let(elem_size, elem_size_expr, layout_isize, next); // let list_size = len * size @@ -972,7 +974,7 @@ fn refcount_union_rec<'a>( let rc_structure_stmt = { let rc_ptr = root.create_symbol(ident_ids, "rc_ptr"); - let alignment = Layout::Union(union_layout).alignment_bytes(root.ptr_size); + let alignment = Layout::Union(union_layout).alignment_bytes(root.target_info); let ret_stmt = rc_return_stmt(root, ident_ids, ctx); let modify_structure_stmt = modify_refcount( root, @@ -988,7 +990,7 @@ fn refcount_union_rec<'a>( ident_ids, structure, rc_ptr, - union_layout.stores_tag_id_in_pointer(root.ptr_size), + union_layout.stores_tag_id_in_pointer(root.target_info), root.arena.alloc(modify_structure_stmt), ) }; @@ -1080,7 +1082,7 @@ fn refcount_union_tailrec<'a>( ) }; - let alignment = layout.alignment_bytes(root.ptr_size); + let alignment = layout.alignment_bytes(root.target_info); let modify_structure_stmt = modify_refcount( root, ident_ids, @@ -1095,7 +1097,7 @@ fn refcount_union_tailrec<'a>( ident_ids, current, rc_ptr, - union_layout.stores_tag_id_in_pointer(root.ptr_size), + union_layout.stores_tag_id_in_pointer(root.target_info), root.arena.alloc(modify_structure_stmt), ) }; diff --git a/compiler/mono/src/ir.rs b/compiler/mono/src/ir.rs index a2517b05f5..d048a0f4c2 100644 --- a/compiler/mono/src/ir.rs +++ b/compiler/mono/src/ir.rs @@ -16,6 +16,7 @@ use roc_module::symbol::{IdentIds, ModuleId, Symbol}; use roc_problem::can::RuntimeError; use roc_region::all::{Loc, Region}; use roc_std::RocDec; +use roc_target::TargetInfo; use roc_types::subs::{Content, FlatType, StorageSubs, Subs, Variable, VariableSubsSlice}; use std::collections::HashMap; use ven_pretty::{BoxAllocator, DocAllocator, DocBuilder}; @@ -1071,7 +1072,7 @@ pub struct Env<'a, 'i> { pub problems: &'i mut std::vec::Vec, pub home: ModuleId, pub ident_ids: &'i mut IdentIds, - pub target_info: u32, + pub target_info: TargetInfo, pub update_mode_ids: &'i mut UpdateModeIds, pub call_specialization_counter: u32, } @@ -8259,7 +8260,7 @@ pub enum IntOrFloat { /// Given the `a` in `Num a`, determines whether it's an int or a float pub fn num_argument_to_int_or_float( subs: &Subs, - ptr_bytes: u32, + target_info: TargetInfo, var: Variable, known_to_be_float: bool, ) -> IntOrFloat { @@ -8274,7 +8275,7 @@ pub fn num_argument_to_int_or_float( // Recurse on the second argument let var = subs[args.variables().into_iter().next().unwrap()]; - num_argument_to_int_or_float(subs, ptr_bytes, var, false) + num_argument_to_int_or_float(subs, target_info, var, false) } other @ Content::Alias(symbol, args, _) => { @@ -8292,16 +8293,15 @@ pub fn num_argument_to_int_or_float( // Recurse on the second argument let var = subs[args.variables().into_iter().next().unwrap()]; - num_argument_to_int_or_float(subs, ptr_bytes, var, true) + num_argument_to_int_or_float(subs, target_info, var, true) } Symbol::NUM_DECIMAL | Symbol::NUM_AT_DECIMAL => IntOrFloat::DecimalFloatType, Symbol::NUM_NAT | Symbol::NUM_NATURAL | Symbol::NUM_AT_NATURAL => { - let int_width = match ptr_bytes { - 4 => IntWidth::U32, - 8 => IntWidth::U64, - _ => panic!("unsupported word size"), + let int_width = match target_info.ptr_width() { + roc_target::PtrWidth::Bytes4 => IntWidth::U32, + roc_target::PtrWidth::Bytes8 => IntWidth::U64, }; IntOrFloat::Int(int_width) diff --git a/compiler/mono/src/layout.rs b/compiler/mono/src/layout.rs index c56a10cb4d..12d759f8b8 100644 --- a/compiler/mono/src/layout.rs +++ b/compiler/mono/src/layout.rs @@ -1237,6 +1237,13 @@ impl<'a> Layout<'a> { } } + pub fn isize(target_info: TargetInfo) -> Layout<'a> { + match target_info.ptr_width() { + roc_target::PtrWidth::Bytes4 => Self::i32(), + roc_target::PtrWidth::Bytes8 => Self::i64(), + } + } + pub fn bool() -> Layout<'a> { Layout::Builtin(Builtin::Bool) }