Add AST -> MIR for bitwise-operations and or

This commit is contained in:
Sofia 2025-07-28 11:59:20 +03:00
parent 516833d26f
commit 49084ea0af
6 changed files with 56 additions and 42 deletions

View File

@ -6,5 +6,5 @@ fn main() -> bool {
let bwor = (0x0f | 0x00) << 4;
let bwxor = (0xf0 | 0x0f);
return (bwxor == 255) && ((value == 15) || false) && (bwor == 240);
return (bwxor == 255) && ((bwand == 15) || false) && (bwor == 240);
}

View File

@ -446,21 +446,21 @@ impl ast::BinaryOperator {
ast::BinaryOperator::Add => mir::BinaryOperator::Add,
ast::BinaryOperator::Minus => mir::BinaryOperator::Minus,
ast::BinaryOperator::Mult => mir::BinaryOperator::Mult,
ast::BinaryOperator::And => mir::BinaryOperator::And,
ast::BinaryOperator::Div => mir::BinaryOperator::Div,
ast::BinaryOperator::Mod => mir::BinaryOperator::Mod,
ast::BinaryOperator::And => mir::BinaryOperator::And,
ast::BinaryOperator::Or => mir::BinaryOperator::Or,
ast::BinaryOperator::LT => mir::BinaryOperator::Cmp(mir::CmpOperator::LT),
ast::BinaryOperator::LE => mir::BinaryOperator::Cmp(mir::CmpOperator::LE),
ast::BinaryOperator::GT => mir::BinaryOperator::Cmp(mir::CmpOperator::GT),
ast::BinaryOperator::GE => mir::BinaryOperator::Cmp(mir::CmpOperator::GE),
ast::BinaryOperator::EQ => mir::BinaryOperator::Cmp(mir::CmpOperator::EQ),
ast::BinaryOperator::NE => mir::BinaryOperator::Cmp(mir::CmpOperator::NE),
ast::BinaryOperator::BitshiftRight => todo!(),
ast::BinaryOperator::BitshiftLeft => todo!(),
ast::BinaryOperator::Or => todo!(),
ast::BinaryOperator::Xor => todo!(),
ast::BinaryOperator::BWAnd => todo!(),
ast::BinaryOperator::BWOr => todo!(),
ast::BinaryOperator::BitshiftRight => mir::BinaryOperator::BitshiftRight,
ast::BinaryOperator::BitshiftLeft => mir::BinaryOperator::BitshiftLeft,
ast::BinaryOperator::Xor => mir::BinaryOperator::Xor,
ast::BinaryOperator::BWAnd => mir::BinaryOperator::BitwiseAnd,
ast::BinaryOperator::BWOr => mir::BinaryOperator::BitwiseOr,
}
}
}

View File

@ -885,6 +885,30 @@ impl mir::Expression {
.maybe_location(&mut scope.block, location);
Instr::Sub(lhs, mul)
}
(mir::BinaryOperator::Or, true, true) => todo!(),
(mir::BinaryOperator::Or, true, false) => todo!(),
(mir::BinaryOperator::Or, false, true) => todo!(),
(mir::BinaryOperator::Or, false, false) => todo!(),
(mir::BinaryOperator::Xor, true, true) => todo!(),
(mir::BinaryOperator::Xor, true, false) => todo!(),
(mir::BinaryOperator::Xor, false, true) => todo!(),
(mir::BinaryOperator::Xor, false, false) => todo!(),
(mir::BinaryOperator::BitwiseOr, true, true) => todo!(),
(mir::BinaryOperator::BitwiseOr, true, false) => todo!(),
(mir::BinaryOperator::BitwiseOr, false, true) => todo!(),
(mir::BinaryOperator::BitwiseOr, false, false) => todo!(),
(mir::BinaryOperator::BitwiseAnd, true, true) => todo!(),
(mir::BinaryOperator::BitwiseAnd, true, false) => todo!(),
(mir::BinaryOperator::BitwiseAnd, false, true) => todo!(),
(mir::BinaryOperator::BitwiseAnd, false, false) => todo!(),
(mir::BinaryOperator::BitshiftRight, true, true) => todo!(),
(mir::BinaryOperator::BitshiftRight, true, false) => todo!(),
(mir::BinaryOperator::BitshiftRight, false, true) => todo!(),
(mir::BinaryOperator::BitshiftRight, false, false) => todo!(),
(mir::BinaryOperator::BitshiftLeft, true, true) => todo!(),
(mir::BinaryOperator::BitshiftLeft, true, false) => todo!(),
(mir::BinaryOperator::BitshiftLeft, false, true) => todo!(),
(mir::BinaryOperator::BitshiftLeft, false, false) => todo!(),
};
Some(StackValue(
StackValueKind::Immutable(

View File

@ -352,6 +352,12 @@ impl Display for BinaryOperator {
BinaryOperator::Cmp(op) => Display::fmt(op, f),
BinaryOperator::Div => write!(f, "/"),
BinaryOperator::Mod => write!(f, "%"),
BinaryOperator::Or => write!(f, "||"),
BinaryOperator::Xor => write!(f, "^"),
BinaryOperator::BitwiseOr => write!(f, "|"),
BinaryOperator::BitwiseAnd => write!(f, "&"),
BinaryOperator::BitshiftRight => write!(f, ">>"),
BinaryOperator::BitshiftLeft => write!(f, "<<"),
}
}
}

View File

@ -26,40 +26,6 @@ enum BlockReturn<'b> {
}
impl TypeKind {
/// Return the type that is the result of a binary operator between two
/// values of this type
pub fn simple_binop_type(&self, op: &BinaryOperator) -> Option<TypeKind> {
if !self.category().is_simple_maths() {
return None;
}
Some(match op {
BinaryOperator::Add => self.clone(),
BinaryOperator::Minus => self.clone(),
BinaryOperator::Mult => self.clone(),
BinaryOperator::And => TypeKind::Bool,
BinaryOperator::Cmp(_) => TypeKind::Bool,
BinaryOperator::Div => self.clone(),
BinaryOperator::Mod => self.clone(),
})
}
/// Reverse of binop_type, where the given hint is the known required output
/// type of the binop, and the output is the hint for the lhs/rhs type.
pub fn simple_binop_hint(&self, op: &BinaryOperator) -> Option<TypeKind> {
if !self.category().is_simple_maths() {
return None;
}
match op {
BinaryOperator::Add
| BinaryOperator::Minus
| BinaryOperator::Mult
| BinaryOperator::Div
| BinaryOperator::Mod => Some(self.clone()),
BinaryOperator::And => None,
BinaryOperator::Cmp(_) => None,
}
}
pub fn signed(&self) -> bool {
match self {
TypeKind::Bool => false,
@ -240,6 +206,12 @@ impl BinaryOperator {
CmpOperator::EQ => true,
CmpOperator::NE => true,
},
BinaryOperator::Or => true,
BinaryOperator::Xor => true,
BinaryOperator::BitwiseOr => true,
BinaryOperator::BitwiseAnd => true,
BinaryOperator::BitshiftRight => false,
BinaryOperator::BitshiftLeft => false,
}
}
}
@ -507,6 +479,12 @@ impl Expression {
}
maybe(lhs.num_value()?, rhs.num_value()?, |a, b| a % b)
}
BinaryOperator::Or => None,
BinaryOperator::Xor => maybe(lhs.num_value()?, rhs.num_value()?, |a, b| a ^ b),
BinaryOperator::BitwiseOr => maybe(lhs.num_value()?, rhs.num_value()?, |a, b| a | b),
BinaryOperator::BitwiseAnd => maybe(lhs.num_value()?, rhs.num_value()?, |a, b| a & b),
BinaryOperator::BitshiftRight => maybe(lhs.num_value()?, rhs.num_value()?, |a, b| a >> b),
BinaryOperator::BitshiftLeft => maybe(lhs.num_value()?, rhs.num_value()?, |a, b| a << b),
},
ExprKind::FunctionCall(..) => None,
ExprKind::If(_) => None,

View File

@ -222,6 +222,12 @@ pub enum BinaryOperator {
Div,
Mod,
And,
Or,
Xor,
BitwiseOr,
BitwiseAnd,
BitshiftRight,
BitshiftLeft,
Cmp(CmpOperator),
}