diff --git a/reid-llvm-lib/src/builder.rs b/reid-llvm-lib/src/builder.rs index 7956bd4..9c99b7a 100644 --- a/reid-llvm-lib/src/builder.rs +++ b/reid-llvm-lib/src/builder.rs @@ -200,6 +200,8 @@ impl Builder { Constant(_) => Ok(()), Add(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), Sub(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), + Mult(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), + And(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), ICmp(_, lhs, rhs) => { let t = match_types(&lhs, &rhs, self)?; if t.comparable() { @@ -248,6 +250,8 @@ impl InstructionValue { Constant(c) => Ok(c.get_type()), Add(lhs, rhs) => match_types(lhs, rhs, &builder), Sub(lhs, rhs) => match_types(lhs, rhs, &builder), + Mult(lhs, rhs) => match_types(lhs, rhs, &builder), + And(lhs, rhs) => match_types(lhs, rhs, &builder), ICmp(_, _, _) => Ok(Type::Bool), FunctionCall(function_value, _) => Ok(builder.function_data(function_value).ret), Phi(values) => values.first().ok_or(()).and_then(|v| v.get_type(&builder)), diff --git a/reid-llvm-lib/src/compile.rs b/reid-llvm-lib/src/compile.rs index 0a38ff4..6058913 100644 --- a/reid-llvm-lib/src/compile.rs +++ b/reid-llvm-lib/src/compile.rs @@ -263,6 +263,16 @@ impl InstructionHolder { let rhs_val = module.values.get(&rhs).unwrap().value_ref; LLVMBuildSub(module.builder_ref, lhs_val, rhs_val, c"sub".as_ptr()) } + Mult(lhs, rhs) => { + let lhs_val = module.values.get(&lhs).unwrap().value_ref; + let rhs_val = module.values.get(&rhs).unwrap().value_ref; + LLVMBuildMul(module.builder_ref, lhs_val, rhs_val, c"mul".as_ptr()) + } + And(lhs, rhs) => { + let lhs_val = module.values.get(&lhs).unwrap().value_ref; + let rhs_val = module.values.get(&rhs).unwrap().value_ref; + LLVMBuildAnd(module.builder_ref, lhs_val, rhs_val, c"and".as_ptr()) + } ICmp(pred, lhs, rhs) => { let lhs = module.values.get(&lhs).unwrap(); let rhs_val = module.values.get(&rhs).unwrap().value_ref; diff --git a/reid-llvm-lib/src/debug.rs b/reid-llvm-lib/src/debug.rs index 9a9c421..9eb1aa2 100644 --- a/reid-llvm-lib/src/debug.rs +++ b/reid-llvm-lib/src/debug.rs @@ -86,6 +86,8 @@ impl Debug for InstructionKind { Self::Constant(c) => c.fmt(f), Self::Add(lhs, rhs) => fmt_binop(f, lhs, &"+", rhs), Self::Sub(lhs, rhs) => fmt_binop(f, lhs, &"-", rhs), + Self::Mult(lhs, rhs) => fmt_binop(f, lhs, &"*", rhs), + Self::And(lhs, rhs) => fmt_binop(f, lhs, &"&&", rhs), Self::Phi(val) => fmt_call(f, &"Phi", &val), Self::ICmp(cmp, lhs, rhs) => fmt_binop(f, lhs, cmp, rhs), Self::FunctionCall(fun, params) => fmt_call(f, fun, params), diff --git a/reid-llvm-lib/src/lib.rs b/reid-llvm-lib/src/lib.rs index fb611f4..d07f4f3 100644 --- a/reid-llvm-lib/src/lib.rs +++ b/reid-llvm-lib/src/lib.rs @@ -152,6 +152,8 @@ pub enum InstructionKind { Constant(ConstValue), Add(InstructionValue, InstructionValue), Sub(InstructionValue, InstructionValue), + Mult(InstructionValue, InstructionValue), + And(InstructionValue, InstructionValue), Phi(Vec), /// Integer Comparison diff --git a/reid/examples/reid/fibonacci.reid b/reid/examples/reid/fibonacci.reid index 30a9b66..e092fb4 100644 --- a/reid/examples/reid/fibonacci.reid +++ b/reid/examples/reid/fibonacci.reid @@ -1,6 +1,6 @@ // Main fn main() -> bool { - return 2 == fibonacci(3); + return (5 == fibonacci(5)) && (2 == fibonacci(3)); } // Fibonacci diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 0ca3b03..717809f 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -240,8 +240,12 @@ impl mir::Expression { mir::BinaryOperator::Minus => { scope.block.build(InstructionKind::Sub(lhs, rhs)).unwrap() } - mir::BinaryOperator::Mult => todo!(), - mir::BinaryOperator::And => todo!(), + mir::BinaryOperator::Mult => { + scope.block.build(InstructionKind::Mult(lhs, rhs)).unwrap() + } + mir::BinaryOperator::And => { + scope.block.build(InstructionKind::And(lhs, rhs)).unwrap() + } mir::BinaryOperator::Logic(l) => scope .block .build(InstructionKind::ICmp(l.int_predicate(), lhs, rhs)) diff --git a/reid/src/mir/display.rs b/reid/src/mir/display.rs index 78848ae..62c1c6a 100644 --- a/reid/src/mir/display.rs +++ b/reid/src/mir/display.rs @@ -103,7 +103,10 @@ impl Display for StmtKind { impl Display for Expression { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - Display::fmt(&self.0, f) + f.write_char('(')?; + Display::fmt(&self.0, f)?; + f.write_char(')')?; + Ok(()) } }