mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-24 12:32:29 +00:00
Move load_symbol_and_layout to scope
This commit is contained in:
parent
6e23365551
commit
bbef63f28f
5 changed files with 54 additions and 59 deletions
|
@ -1083,7 +1083,7 @@ pub(crate) fn build_exp_expr<'a, 'ctx>(
|
|||
),
|
||||
|
||||
ExprBox { symbol } => {
|
||||
let (value, layout) = load_symbol_and_layout(scope, symbol);
|
||||
let (value, layout) = scope.load_symbol_and_layout(symbol);
|
||||
let basic_type = basic_type_from_layout(env, layout_interner, layout);
|
||||
let allocation = reserve_with_refcount_help(
|
||||
env,
|
||||
|
@ -1121,7 +1121,7 @@ pub(crate) fn build_exp_expr<'a, 'ctx>(
|
|||
.update_mode(update_var)
|
||||
.unwrap_or(UpdateMode::Immutable);
|
||||
|
||||
let (tag_ptr, layout) = load_symbol_and_layout(scope, symbol);
|
||||
let (tag_ptr, layout) = scope.load_symbol_and_layout(symbol);
|
||||
let tag_ptr = tag_ptr.into_pointer_value();
|
||||
|
||||
// reset is only generated for union values
|
||||
|
@ -1208,7 +1208,7 @@ pub(crate) fn build_exp_expr<'a, 'ctx>(
|
|||
.update_mode(update_var)
|
||||
.unwrap_or(UpdateMode::Immutable);
|
||||
|
||||
let (tag_ptr, layout) = load_symbol_and_layout(scope, symbol);
|
||||
let (tag_ptr, layout) = scope.load_symbol_and_layout(symbol);
|
||||
let tag_ptr = tag_ptr.into_pointer_value();
|
||||
|
||||
let ctx = env.context;
|
||||
|
@ -1268,7 +1268,7 @@ pub(crate) fn build_exp_expr<'a, 'ctx>(
|
|||
StructAtIndex {
|
||||
index, structure, ..
|
||||
} => {
|
||||
let (value, layout) = load_symbol_and_layout(scope, structure);
|
||||
let (value, layout) = scope.load_symbol_and_layout(structure);
|
||||
|
||||
struct_::load_at_index(env, layout_interner, layout, value, *index)
|
||||
}
|
||||
|
@ -1286,7 +1286,7 @@ pub(crate) fn build_exp_expr<'a, 'ctx>(
|
|||
union_layout,
|
||||
} => {
|
||||
// cast the argument bytes into the desired shape for this tag
|
||||
let (argument, structure_layout) = load_symbol_and_layout(scope, structure);
|
||||
let (argument, structure_layout) = scope.load_symbol_and_layout(structure);
|
||||
|
||||
match union_layout {
|
||||
UnionLayout::NonRecursive(tag_layouts) => {
|
||||
|
@ -1427,7 +1427,7 @@ pub(crate) fn build_exp_expr<'a, 'ctx>(
|
|||
union_layout,
|
||||
} => {
|
||||
// cast the argument bytes into the desired shape for this tag
|
||||
let (argument, _structure_layout) = load_symbol_and_layout(scope, structure);
|
||||
let (argument, _structure_layout) = scope.load_symbol_and_layout(structure);
|
||||
|
||||
get_tag_id(env, layout_interner, parent, union_layout, argument).into()
|
||||
}
|
||||
|
@ -2456,7 +2456,7 @@ pub(crate) fn build_exp_stmt<'a, 'ctx>(
|
|||
result
|
||||
}
|
||||
Ret(symbol) => {
|
||||
let (value, layout) = load_symbol_and_layout(scope, symbol);
|
||||
let (value, layout) = scope.load_symbol_and_layout(symbol);
|
||||
|
||||
match RocReturn::from_layout(env, layout_interner, layout) {
|
||||
RocReturn::Return => {
|
||||
|
@ -2653,7 +2653,7 @@ pub(crate) fn build_exp_stmt<'a, 'ctx>(
|
|||
let current_block = builder.get_insert_block().unwrap();
|
||||
|
||||
for (phi_value, argument) in argument_phi_values.iter().zip(arguments.iter()) {
|
||||
let (value, _) = load_symbol_and_layout(scope, argument);
|
||||
let (value, _) = scope.load_symbol_and_layout(argument);
|
||||
|
||||
phi_value.add_incoming(&[(&value, current_block)]);
|
||||
}
|
||||
|
@ -2669,7 +2669,7 @@ pub(crate) fn build_exp_stmt<'a, 'ctx>(
|
|||
|
||||
match modify {
|
||||
Inc(symbol, inc_amount) => {
|
||||
let (value, layout) = load_symbol_and_layout(scope, symbol);
|
||||
let (value, layout) = scope.load_symbol_and_layout(symbol);
|
||||
if layout_interner.contains_refcounted(layout) {
|
||||
increment_refcount_layout(
|
||||
env,
|
||||
|
@ -2692,7 +2692,7 @@ pub(crate) fn build_exp_stmt<'a, 'ctx>(
|
|||
)
|
||||
}
|
||||
Dec(symbol) => {
|
||||
let (value, layout) = load_symbol_and_layout(scope, symbol);
|
||||
let (value, layout) = scope.load_symbol_and_layout(symbol);
|
||||
|
||||
if layout_interner.contains_refcounted(layout) {
|
||||
decrement_refcount_layout(env, layout_interner, layout_ids, value, layout);
|
||||
|
@ -2709,7 +2709,7 @@ pub(crate) fn build_exp_stmt<'a, 'ctx>(
|
|||
)
|
||||
}
|
||||
DecRef(symbol) => {
|
||||
let (value, layout) = load_symbol_and_layout(scope, symbol);
|
||||
let (value, layout) = scope.load_symbol_and_layout(symbol);
|
||||
|
||||
let lay = layout_interner.get_repr(layout);
|
||||
match lay {
|
||||
|
@ -2820,7 +2820,7 @@ pub(crate) fn build_exp_stmt<'a, 'ctx>(
|
|||
let bd = env.builder;
|
||||
let context = env.context;
|
||||
|
||||
let (cond, _cond_layout) = load_symbol_and_layout(scope, cond_symbol);
|
||||
let (cond, _cond_layout) = scope.load_symbol_and_layout(cond_symbol);
|
||||
|
||||
let condition = bd.build_int_compare(
|
||||
IntPredicate::EQ,
|
||||
|
@ -2892,7 +2892,7 @@ pub(crate) fn build_exp_stmt<'a, 'ctx>(
|
|||
let bd = env.builder;
|
||||
let context = env.context;
|
||||
|
||||
let (cond, _cond_layout) = load_symbol_and_layout(scope, cond_symbol);
|
||||
let (cond, _cond_layout) = scope.load_symbol_and_layout(cond_symbol);
|
||||
|
||||
let condition = bd.build_int_compare(
|
||||
IntPredicate::EQ,
|
||||
|
@ -2960,16 +2960,6 @@ pub(crate) fn build_exp_stmt<'a, 'ctx>(
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn load_symbol_and_layout<'a, 'ctx>(
|
||||
scope: &Scope<'a, 'ctx>,
|
||||
symbol: &Symbol,
|
||||
) -> (BasicValueEnum<'ctx>, InLayout<'a>) {
|
||||
match scope.get(symbol) {
|
||||
Some((layout, ptr)) => (*ptr, *layout),
|
||||
None => panic!("There was no entry for {:?} in scope {:?}", symbol, scope),
|
||||
}
|
||||
}
|
||||
|
||||
fn equivalent_type_constructors(t1: &BasicTypeEnum, t2: &BasicTypeEnum) -> bool {
|
||||
use BasicTypeEnum::*;
|
||||
match (t1, t2) {
|
||||
|
@ -3278,7 +3268,7 @@ fn build_switch_ir<'a, 'ctx>(
|
|||
let scope = &mut copy;
|
||||
|
||||
let cond_symbol = &cond_symbol;
|
||||
let (cond_value, stored_layout) = load_symbol_and_layout(scope, cond_symbol);
|
||||
let (cond_value, stored_layout) = scope.load_symbol_and_layout(cond_symbol);
|
||||
|
||||
debug_assert_eq!(
|
||||
basic_type_from_layout(env, layout_interner, cond_layout),
|
||||
|
@ -5795,7 +5785,7 @@ fn build_foreign_symbol<'a, 'ctx>(
|
|||
let mut arguments = Vec::with_capacity_in(argument_symbols.len(), env.arena);
|
||||
|
||||
for symbol in argument_symbols {
|
||||
let (value, _) = load_symbol_and_layout(scope, symbol);
|
||||
let (value, _) = scope.load_symbol_and_layout(symbol);
|
||||
|
||||
arguments.push(value);
|
||||
}
|
||||
|
@ -5821,7 +5811,7 @@ fn build_foreign_symbol<'a, 'ctx>(
|
|||
let mut arguments = Vec::with_capacity_in(argument_symbols.len(), env.arena);
|
||||
|
||||
for symbol in argument_symbols {
|
||||
let (value, layout) = load_symbol_and_layout(scope, symbol);
|
||||
let (value, layout) = scope.load_symbol_and_layout(symbol);
|
||||
|
||||
cc_argument_types.push(to_cc_type(env, layout_interner, layout));
|
||||
|
||||
|
|
|
@ -18,9 +18,7 @@ use roc_mono::layout::{
|
|||
use roc_region::all::Region;
|
||||
|
||||
use super::build::BuilderExt;
|
||||
use super::build::{
|
||||
add_func, load_roc_value, load_symbol_and_layout, use_roc_value, FunctionSpec, LlvmBackendMode,
|
||||
};
|
||||
use super::build::{add_func, load_roc_value, use_roc_value, FunctionSpec, LlvmBackendMode};
|
||||
use super::convert::struct_type_from_union_layout;
|
||||
use super::scope::Scope;
|
||||
|
||||
|
@ -209,7 +207,7 @@ pub(crate) fn clone_to_shared_memory<'a, 'ctx>(
|
|||
for lookup in lookups.iter() {
|
||||
lookup_starts.push(offset);
|
||||
|
||||
let (value, layout) = load_symbol_and_layout(scope, lookup);
|
||||
let (value, layout) = scope.load_symbol_and_layout(lookup);
|
||||
|
||||
let stack_size = env
|
||||
.ptr_int()
|
||||
|
|
|
@ -54,10 +54,7 @@ use crate::llvm::{
|
|||
};
|
||||
|
||||
use super::{build::throw_internal_exception, convert::zig_with_overflow_roc_dec, scope::Scope};
|
||||
use super::{
|
||||
build::{load_symbol_and_layout, Env},
|
||||
convert::zig_dec_type,
|
||||
};
|
||||
use super::{build::Env, convert::zig_dec_type};
|
||||
|
||||
pub(crate) fn run_low_level<'a, 'ctx>(
|
||||
env: &Env<'a, 'ctx, '_>,
|
||||
|
@ -97,7 +94,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
|
|||
// look at that, a usage for if let ... else
|
||||
let [$(($x, $y)),+] = match &args {
|
||||
[$($x),+] => {
|
||||
[ $(load_symbol_and_layout(scope, $x)),+ ]
|
||||
[ $(scope.load_symbol_and_layout($x)),+ ]
|
||||
}
|
||||
_ => {
|
||||
// we could get fancier with reporting here, but this macro is used a bunch
|
||||
|
@ -323,7 +320,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
|
|||
// Str.fromInt : Int -> Str
|
||||
debug_assert_eq!(args.len(), 1);
|
||||
|
||||
let (int, int_layout) = load_symbol_and_layout(scope, &args[0]);
|
||||
let (int, int_layout) = scope.load_symbol_and_layout(&args[0]);
|
||||
let int = int.into_int_value();
|
||||
|
||||
let int_width = match layout_interner.get_repr(int_layout) {
|
||||
|
@ -343,7 +340,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
|
|||
// Str.fromFloat : Float * -> Str
|
||||
debug_assert_eq!(args.len(), 1);
|
||||
|
||||
let (float, float_layout) = load_symbol_and_layout(scope, &args[0]);
|
||||
let (float, float_layout) = scope.load_symbol_and_layout(&args[0]);
|
||||
|
||||
let float_width = match layout_interner.get_repr(float_layout) {
|
||||
LayoutRepr::Builtin(Builtin::Float(float_width)) => float_width,
|
||||
|
@ -683,7 +680,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
|
|||
ListConcat => {
|
||||
debug_assert_eq!(args.len(), 2);
|
||||
|
||||
let (first_list, list_layout) = load_symbol_and_layout(scope, &args[0]);
|
||||
let (first_list, list_layout) = scope.load_symbol_and_layout(&args[0]);
|
||||
|
||||
let second_list = scope.load_symbol(&args[1]);
|
||||
|
||||
|
@ -702,7 +699,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
|
|||
debug_assert_eq!(args.len(), 2);
|
||||
|
||||
let original_wrapper = scope.load_symbol(&args[0]).into_struct_value();
|
||||
let (elem, elem_layout) = load_symbol_and_layout(scope, &args[1]);
|
||||
let (elem, elem_layout) = scope.load_symbol_and_layout(&args[1]);
|
||||
|
||||
list_append_unsafe(env, layout_interner, original_wrapper, elem, elem_layout)
|
||||
}
|
||||
|
@ -711,7 +708,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
|
|||
debug_assert_eq!(args.len(), 2);
|
||||
|
||||
let original_wrapper = scope.load_symbol(&args[0]).into_struct_value();
|
||||
let (elem, elem_layout) = load_symbol_and_layout(scope, &args[1]);
|
||||
let (elem, elem_layout) = scope.load_symbol_and_layout(&args[1]);
|
||||
|
||||
list_prepend(env, layout_interner, original_wrapper, elem, elem_layout)
|
||||
}
|
||||
|
@ -719,7 +716,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
|
|||
// List.reserve : List elem, Nat -> List elem
|
||||
debug_assert_eq!(args.len(), 2);
|
||||
|
||||
let (list, list_layout) = load_symbol_and_layout(scope, &args[0]);
|
||||
let (list, list_layout) = scope.load_symbol_and_layout(&args[0]);
|
||||
let element_layout = list_element_layout!(layout_interner, list_layout);
|
||||
let spare = scope.load_symbol(&args[1]);
|
||||
|
||||
|
@ -736,7 +733,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
|
|||
// List.releaseExcessCapacity: List elem -> List elem
|
||||
debug_assert_eq!(args.len(), 1);
|
||||
|
||||
let (list, list_layout) = load_symbol_and_layout(scope, &args[0]);
|
||||
let (list, list_layout) = scope.load_symbol_and_layout(&args[0]);
|
||||
let element_layout = list_element_layout!(layout_interner, list_layout);
|
||||
|
||||
list_release_excess_capacity(env, layout_interner, list, element_layout, update_mode)
|
||||
|
@ -745,7 +742,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
|
|||
// List.swap : List elem, Nat, Nat -> List elem
|
||||
debug_assert_eq!(args.len(), 3);
|
||||
|
||||
let (list, list_layout) = load_symbol_and_layout(scope, &args[0]);
|
||||
let (list, list_layout) = scope.load_symbol_and_layout(&args[0]);
|
||||
let original_wrapper = list.into_struct_value();
|
||||
|
||||
let index_1 = scope.load_symbol(&args[1]);
|
||||
|
@ -765,7 +762,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
|
|||
ListSublist => {
|
||||
debug_assert_eq!(args.len(), 3);
|
||||
|
||||
let (list, list_layout) = load_symbol_and_layout(scope, &args[0]);
|
||||
let (list, list_layout) = scope.load_symbol_and_layout(&args[0]);
|
||||
let original_wrapper = list.into_struct_value();
|
||||
|
||||
let start = scope.load_symbol(&args[1]);
|
||||
|
@ -786,7 +783,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
|
|||
// List.dropAt : List elem, Nat -> List elem
|
||||
debug_assert_eq!(args.len(), 2);
|
||||
|
||||
let (list, list_layout) = load_symbol_and_layout(scope, &args[0]);
|
||||
let (list, list_layout) = scope.load_symbol_and_layout(&args[0]);
|
||||
let original_wrapper = list.into_struct_value();
|
||||
|
||||
let count = scope.load_symbol(&args[1]);
|
||||
|
@ -868,7 +865,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
|
|||
)
|
||||
}
|
||||
LayoutRepr::Builtin(Builtin::Float(_float_width)) => {
|
||||
let (float, float_layout) = load_symbol_and_layout(scope, &args[0]);
|
||||
let (float, float_layout) = scope.load_symbol_and_layout(&args[0]);
|
||||
|
||||
let float_width = match layout_interner.get_repr(float_layout) {
|
||||
LayoutRepr::Builtin(Builtin::Float(float_width)) => float_width,
|
||||
|
@ -2491,7 +2488,7 @@ pub(crate) fn run_higher_order_low_level<'a, 'ctx>(
|
|||
match op {
|
||||
ListMap { xs } => {
|
||||
// List.map : List before, (before -> after) -> List after
|
||||
let (list, list_layout) = load_symbol_and_layout(scope, xs);
|
||||
let (list, list_layout) = scope.load_symbol_and_layout(xs);
|
||||
|
||||
let (function, closure, closure_layout) = function_details!();
|
||||
|
||||
|
@ -2530,8 +2527,8 @@ pub(crate) fn run_higher_order_low_level<'a, 'ctx>(
|
|||
}
|
||||
}
|
||||
ListMap2 { xs, ys } => {
|
||||
let (list1, list1_layout) = load_symbol_and_layout(scope, xs);
|
||||
let (list2, list2_layout) = load_symbol_and_layout(scope, ys);
|
||||
let (list1, list1_layout) = scope.load_symbol_and_layout(xs);
|
||||
let (list2, list2_layout) = scope.load_symbol_and_layout(ys);
|
||||
|
||||
let (function, closure, closure_layout) = function_details!();
|
||||
|
||||
|
@ -2575,9 +2572,9 @@ pub(crate) fn run_higher_order_low_level<'a, 'ctx>(
|
|||
}
|
||||
}
|
||||
ListMap3 { xs, ys, zs } => {
|
||||
let (list1, list1_layout) = load_symbol_and_layout(scope, xs);
|
||||
let (list2, list2_layout) = load_symbol_and_layout(scope, ys);
|
||||
let (list3, list3_layout) = load_symbol_and_layout(scope, zs);
|
||||
let (list1, list1_layout) = scope.load_symbol_and_layout(xs);
|
||||
let (list2, list2_layout) = scope.load_symbol_and_layout(ys);
|
||||
let (list3, list3_layout) = scope.load_symbol_and_layout(zs);
|
||||
|
||||
let (function, closure, closure_layout) = function_details!();
|
||||
|
||||
|
@ -2625,10 +2622,10 @@ pub(crate) fn run_higher_order_low_level<'a, 'ctx>(
|
|||
}
|
||||
}
|
||||
ListMap4 { xs, ys, zs, ws } => {
|
||||
let (list1, list1_layout) = load_symbol_and_layout(scope, xs);
|
||||
let (list2, list2_layout) = load_symbol_and_layout(scope, ys);
|
||||
let (list3, list3_layout) = load_symbol_and_layout(scope, zs);
|
||||
let (list4, list4_layout) = load_symbol_and_layout(scope, ws);
|
||||
let (list1, list1_layout) = scope.load_symbol_and_layout(xs);
|
||||
let (list2, list2_layout) = scope.load_symbol_and_layout(ys);
|
||||
let (list3, list3_layout) = scope.load_symbol_and_layout(zs);
|
||||
let (list4, list4_layout) = scope.load_symbol_and_layout(ws);
|
||||
|
||||
let (function, closure, closure_layout) = function_details!();
|
||||
|
||||
|
@ -2686,7 +2683,7 @@ pub(crate) fn run_higher_order_low_level<'a, 'ctx>(
|
|||
}
|
||||
ListSortWith { xs } => {
|
||||
// List.sortWith : List a, (a, a -> Ordering) -> List a
|
||||
let (list, list_layout) = load_symbol_and_layout(scope, xs);
|
||||
let (list, list_layout) = scope.load_symbol_and_layout(xs);
|
||||
|
||||
let (function, closure, closure_layout) = function_details!();
|
||||
|
||||
|
|
|
@ -35,6 +35,16 @@ impl<'a, 'ctx> Scope<'a, 'ctx> {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn load_symbol_and_layout(
|
||||
&self,
|
||||
symbol: &Symbol,
|
||||
) -> (BasicValueEnum<'ctx>, InLayout<'a>) {
|
||||
match self.get(symbol) {
|
||||
Some((layout, ptr)) => (*ptr, *layout),
|
||||
None => panic!("There was no entry for {:?} in scope {:?}", symbol, self),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, symbol: Symbol, value: (InLayout<'a>, BasicValueEnum<'ctx>)) {
|
||||
self.symbols.insert(symbol, value);
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ use roc_mono::layout::{InLayout, LayoutInterner, LayoutRepr, STLayoutInterner};
|
|||
use crate::llvm::build::use_roc_value;
|
||||
|
||||
use super::{
|
||||
build::{load_symbol_and_layout, BuilderExt, Env},
|
||||
build::{BuilderExt, Env},
|
||||
convert::basic_type_from_layout,
|
||||
scope::Scope,
|
||||
};
|
||||
|
@ -106,7 +106,7 @@ fn build_struct_value<'a, 'ctx>(
|
|||
for symbol in sorted_fields.iter() {
|
||||
// Zero-sized fields have no runtime representation.
|
||||
// The layout of the struct expects them to be dropped!
|
||||
let (field_expr, field_layout) = load_symbol_and_layout(scope, symbol);
|
||||
let (field_expr, field_layout) = scope.load_symbol_and_layout(symbol);
|
||||
if !layout_interner
|
||||
.get_repr(field_layout)
|
||||
.is_dropped_because_empty()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue