Merge branch 'trunk' into access-record-fields

This commit is contained in:
Dan Bruder 2020-03-13 09:07:41 -04:00
commit 099145e4b4
14 changed files with 1022 additions and 158 deletions

View file

@ -120,9 +120,7 @@ pub fn build_expr<'a, B: Backend>(
let fn_id = match scope.get(name) {
Some(ScopeEntry::Func{ func_id, .. }) => *func_id,
other => panic!(
"FunctionPointer could not find function named {:?} in scope; instead, found {:?} in scope {:?}",
name, other, scope
),
"FunctionPointer could not find function named {:?} declared in scope (and it was not special-cased in crane::build as a builtin); instead, found {:?} in scope {:?}", name, other, scope),
};
let func_ref = module.declare_func_in_func(fn_id, &mut builder.func);
@ -161,7 +159,10 @@ pub fn build_expr<'a, B: Backend>(
Some(ScopeEntry::Func { .. }) => {
panic!("TODO I don't yet know how to return fn pointers")
}
None => panic!("Could not find a var for {:?} in scope {:?}", name, scope),
None => panic!(
"Could not resolve lookup for {:?} because no ScopeEntry was found for {:?} in scope {:?}",
name, name, scope
),
},
Struct { layout, fields } => {
let cfg = env.cfg;
@ -590,20 +591,34 @@ fn call_by_name<'a, B: Backend>(
procs: &Procs<'a>,
) -> Value {
match symbol {
Symbol::NUM_ADD => {
Symbol::INT_ADD | Symbol::NUM_ADD => {
debug_assert!(args.len() == 2);
let a = build_arg(&args[0], env, scope, module, builder, procs);
let b = build_arg(&args[1], env, scope, module, builder, procs);
builder.ins().iadd(a, b)
}
Symbol::NUM_SUB => {
Symbol::FLOAT_ADD => {
debug_assert!(args.len() == 2);
let a = build_arg(&args[0], env, scope, module, builder, procs);
let b = build_arg(&args[1], env, scope, module, builder, procs);
builder.ins().fadd(a, b)
}
Symbol::INT_SUB | Symbol::NUM_SUB => {
debug_assert!(args.len() == 2);
let a = build_arg(&args[0], env, scope, module, builder, procs);
let b = build_arg(&args[1], env, scope, module, builder, procs);
builder.ins().isub(a, b)
}
Symbol::FLOAT_SUB => {
debug_assert!(args.len() == 2);
let a = build_arg(&args[0], env, scope, module, builder, procs);
let b = build_arg(&args[1], env, scope, module, builder, procs);
builder.ins().fsub(a, b)
}
Symbol::NUM_MUL => {
debug_assert!(args.len() == 2);
let a = build_arg(&args[0], env, scope, module, builder, procs);
@ -708,12 +723,9 @@ fn call_by_name<'a, B: Backend>(
}
_ => {
let fn_id = match scope.get(&symbol) {
Some(ScopeEntry::Func{ func_id, .. }) => *func_id,
other => panic!(
"CallByName could not find function named {:?} in scope; instead, found {:?} in scope {:?}",
symbol, other, scope
),
};
Some(ScopeEntry::Func { func_id, .. }) => *func_id,
other => panic!("CallByName could not find function named {:?} declared in scope (and it was not special-cased in crane::build as a builtin); instead, found {:?} in scope {:?}", symbol, other, scope),
};
let local_func = module.declare_func_in_func(fn_id, &mut builder.func);
let mut arg_vals = Vec::with_capacity_in(args.len(), env.arena);

View file

@ -536,35 +536,57 @@ fn call_with_args<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
) -> BasicValueEnum<'ctx> {
match symbol {
Symbol::NUM_ADD => {
Symbol::INT_ADD | Symbol::NUM_ADD => {
debug_assert!(args.len() == 2);
let int_val = env.builder.build_int_add(
args[0].into_int_value(),
args[1].into_int_value(),
"ADD_I64",
"add_i64",
);
BasicValueEnum::IntValue(int_val)
}
Symbol::NUM_SUB => {
Symbol::FLOAT_ADD => {
debug_assert!(args.len() == 2);
let float_val = env.builder.build_float_add(
args[0].into_float_value(),
args[1].into_float_value(),
"add_f64",
);
BasicValueEnum::FloatValue(float_val)
}
Symbol::INT_SUB | Symbol::NUM_SUB => {
debug_assert!(args.len() == 2);
let int_val = env.builder.build_int_sub(
args[0].into_int_value(),
args[1].into_int_value(),
"SUB_I64",
"sub_I64",
);
BasicValueEnum::IntValue(int_val)
}
Symbol::FLOAT_SUB => {
debug_assert!(args.len() == 2);
let float_val = env.builder.build_float_sub(
args[0].into_float_value(),
args[1].into_float_value(),
"sub_f64",
);
BasicValueEnum::FloatValue(float_val)
}
Symbol::NUM_MUL => {
debug_assert!(args.len() == 2);
let int_val = env.builder.build_int_mul(
args[0].into_int_value(),
args[1].into_int_value(),
"MUL_I64",
"mul_i64",
);
BasicValueEnum::IntValue(int_val)
@ -574,7 +596,7 @@ fn call_with_args<'a, 'ctx, 'env>(
let int_val = env
.builder
.build_int_neg(args[0].into_int_value(), "NEGATE_I64");
.build_int_neg(args[0].into_int_value(), "negate_i64");
BasicValueEnum::IntValue(int_val)
}
@ -587,7 +609,7 @@ fn call_with_args<'a, 'ctx, 'env>(
let builder = env.builder;
let elem_bytes = 8; // TODO Look this up instead of hardcoding it!
let elem_size = env.context.i64_type().const_int(elem_bytes, false);
let offset = builder.build_int_mul(elem_index, elem_size, "MUL_OFFSET");
let offset = builder.build_int_mul(elem_index, elem_size, "mul_offset");
let elem_ptr = unsafe { builder.build_gep(list_ptr, &[offset], "elem") };