Add Mult and And -operators to the whole chain

This commit is contained in:
Sofia 2025-07-09 19:08:21 +03:00
parent 8afb2c2572
commit b84672ef8c
7 changed files with 29 additions and 4 deletions

View File

@ -200,6 +200,8 @@ impl Builder {
Constant(_) => Ok(()), Constant(_) => Ok(()),
Add(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), Add(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()),
Sub(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) => { ICmp(_, lhs, rhs) => {
let t = match_types(&lhs, &rhs, self)?; let t = match_types(&lhs, &rhs, self)?;
if t.comparable() { if t.comparable() {
@ -248,6 +250,8 @@ impl InstructionValue {
Constant(c) => Ok(c.get_type()), Constant(c) => Ok(c.get_type()),
Add(lhs, rhs) => match_types(lhs, rhs, &builder), Add(lhs, rhs) => match_types(lhs, rhs, &builder),
Sub(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), ICmp(_, _, _) => Ok(Type::Bool),
FunctionCall(function_value, _) => Ok(builder.function_data(function_value).ret), FunctionCall(function_value, _) => Ok(builder.function_data(function_value).ret),
Phi(values) => values.first().ok_or(()).and_then(|v| v.get_type(&builder)), Phi(values) => values.first().ok_or(()).and_then(|v| v.get_type(&builder)),

View File

@ -263,6 +263,16 @@ impl InstructionHolder {
let rhs_val = module.values.get(&rhs).unwrap().value_ref; let rhs_val = module.values.get(&rhs).unwrap().value_ref;
LLVMBuildSub(module.builder_ref, lhs_val, rhs_val, c"sub".as_ptr()) 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) => { ICmp(pred, lhs, rhs) => {
let lhs = module.values.get(&lhs).unwrap(); let lhs = module.values.get(&lhs).unwrap();
let rhs_val = module.values.get(&rhs).unwrap().value_ref; let rhs_val = module.values.get(&rhs).unwrap().value_ref;

View File

@ -86,6 +86,8 @@ impl Debug for InstructionKind {
Self::Constant(c) => c.fmt(f), Self::Constant(c) => c.fmt(f),
Self::Add(lhs, rhs) => fmt_binop(f, lhs, &"+", rhs), Self::Add(lhs, rhs) => fmt_binop(f, lhs, &"+", rhs),
Self::Sub(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::Phi(val) => fmt_call(f, &"Phi", &val),
Self::ICmp(cmp, lhs, rhs) => fmt_binop(f, lhs, cmp, rhs), Self::ICmp(cmp, lhs, rhs) => fmt_binop(f, lhs, cmp, rhs),
Self::FunctionCall(fun, params) => fmt_call(f, fun, params), Self::FunctionCall(fun, params) => fmt_call(f, fun, params),

View File

@ -152,6 +152,8 @@ pub enum InstructionKind {
Constant(ConstValue), Constant(ConstValue),
Add(InstructionValue, InstructionValue), Add(InstructionValue, InstructionValue),
Sub(InstructionValue, InstructionValue), Sub(InstructionValue, InstructionValue),
Mult(InstructionValue, InstructionValue),
And(InstructionValue, InstructionValue),
Phi(Vec<InstructionValue>), Phi(Vec<InstructionValue>),
/// Integer Comparison /// Integer Comparison

View File

@ -1,6 +1,6 @@
// Main // Main
fn main() -> bool { fn main() -> bool {
return 2 == fibonacci(3); return (5 == fibonacci(5)) && (2 == fibonacci(3));
} }
// Fibonacci // Fibonacci

View File

@ -240,8 +240,12 @@ impl mir::Expression {
mir::BinaryOperator::Minus => { mir::BinaryOperator::Minus => {
scope.block.build(InstructionKind::Sub(lhs, rhs)).unwrap() scope.block.build(InstructionKind::Sub(lhs, rhs)).unwrap()
} }
mir::BinaryOperator::Mult => todo!(), mir::BinaryOperator::Mult => {
mir::BinaryOperator::And => todo!(), scope.block.build(InstructionKind::Mult(lhs, rhs)).unwrap()
}
mir::BinaryOperator::And => {
scope.block.build(InstructionKind::And(lhs, rhs)).unwrap()
}
mir::BinaryOperator::Logic(l) => scope mir::BinaryOperator::Logic(l) => scope
.block .block
.build(InstructionKind::ICmp(l.int_predicate(), lhs, rhs)) .build(InstructionKind::ICmp(l.int_predicate(), lhs, rhs))

View File

@ -103,7 +103,10 @@ impl Display for StmtKind {
impl Display for Expression { impl Display for Expression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 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(())
} }
} }