Try having build_expr return Either

This commit is contained in:
Richard Feldman 2020-07-08 23:00:28 -04:00
parent 7aaf37f4d1
commit 70ceaac9ff
3 changed files with 16 additions and 9 deletions

1
Cargo.lock generated
View File

@ -1930,6 +1930,7 @@ name = "roc_gen"
version = "0.1.0"
dependencies = [
"bumpalo",
"either",
"im",
"im-rc",
"indoc",

View File

@ -40,6 +40,7 @@ inlinable_string = "0.1"
# This way, GitHub Actions works and nobody's builds get broken.
inkwell = { git = "https://github.com/rtfeldman/inkwell", tag = "llvm10-0.release1" }
target-lexicon = "0.10"
either = "1.5" # this should be the same version as the one Inkwell uses
[dev-dependencies]
roc_can = { path = "../can" }

View File

@ -5,6 +5,7 @@ use crate::llvm::convert::{
};
use bumpalo::collections::Vec;
use bumpalo::Bump;
use either::Either::{self, *};
use inkwell::builder::Builder;
use inkwell::context::Context;
use inkwell::memory_buffer::MemoryBuffer;
@ -13,7 +14,7 @@ use inkwell::passes::{PassManager, PassManagerBuilder};
use inkwell::types::{BasicTypeEnum, FunctionType, IntType, PointerType, StructType};
use inkwell::values::BasicValueEnum::{self, *};
use inkwell::values::{
FloatValue, FunctionValue, InstructionValue, IntValue, PointerValue, StructValue,
BasicValue, FloatValue, FunctionValue, InstructionValue, IntValue, PointerValue, StructValue,
};
use inkwell::AddressSpace;
use inkwell::{IntPredicate, OptimizationLevel};
@ -173,14 +174,14 @@ pub fn build_expr<'a, 'ctx, 'env>(
scope: &Scope<'a, 'ctx>,
parent: FunctionValue<'ctx>,
expr: &Expr<'a>,
) -> BasicValueEnum<'ctx> {
) -> Either<BasicValueEnum<'ctx>, InstructionValue<'ctx>> {
use roc_mono::expr::Expr::*;
match expr {
Int(num) => env.context.i64_type().const_int(*num as u64, true).into(),
Float(num) => env.context.f64_type().const_float(*num).into(),
Bool(b) => env.context.bool_type().const_int(*b as u64, false).into(),
Byte(b) => env.context.i8_type().const_int(*b as u64, false).into(),
Int(num) => Left(env.context.i64_type().const_int(*num as u64, true).into()),
Float(num) => Left(env.context.f64_type().const_float(*num).into()),
Bool(b) => Left(env.context.bool_type().const_int(*b as u64, false).into()),
Byte(b) => Left(env.context.i8_type().const_int(*b as u64, false).into()),
Cond {
branch_symbol,
pass: (pass_stores, pass_expr),
@ -212,7 +213,11 @@ pub fn build_expr<'a, 'ctx, 'env>(
// build then block
builder.position_at_end(then_block);
let then_val = build_expr(env, layout_ids, scope, parent, pass);
let then_val: &'a dyn BasicValue<'ctx> =
match build_expr(env, layout_ids, scope, parent, pass) {
Left(val) => env.arena.alloc(val),
Right(inst) => env.arena.alloc(inst),
};
builder.build_unconditional_branch(cont_block);
let then_block = builder.get_insert_block().unwrap();
@ -229,9 +234,9 @@ pub fn build_expr<'a, 'ctx, 'env>(
let phi = builder.build_phi(ret_type, "branch");
phi.add_incoming(&[(&then_val, then_block), (&else_val, else_block)]);
phi.add_incoming(&[(then_val, then_block), (&else_val, else_block)]);
phi.as_basic_value()
Left(phi.as_basic_value())
}
_ => panic!(
"Tried to make a branch out of an invalid condition: cond_expr = {:?}",