diff --git a/examples/or_bitwise.reid b/examples/or_bitwise.reid index fb802f5..731892b 100644 --- a/examples/or_bitwise.reid +++ b/examples/or_bitwise.reid @@ -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); } \ No newline at end of file diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 8d57463..72a0fc2 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -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, } } } diff --git a/reid/src/codegen/mod.rs b/reid/src/codegen/mod.rs index f924656..cdba78d 100644 --- a/reid/src/codegen/mod.rs +++ b/reid/src/codegen/mod.rs @@ -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( diff --git a/reid/src/mir/fmt.rs b/reid/src/mir/fmt.rs index 7d478a4..ba0de0b 100644 --- a/reid/src/mir/fmt.rs +++ b/reid/src/mir/fmt.rs @@ -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, "<<"), } } } diff --git a/reid/src/mir/implement.rs b/reid/src/mir/implement.rs index a004fba..081755b 100644 --- a/reid/src/mir/implement.rs +++ b/reid/src/mir/implement.rs @@ -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 { - 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 { - 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, diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 4b3ff62..87c463c 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -222,6 +222,12 @@ pub enum BinaryOperator { Div, Mod, And, + Or, + Xor, + BitwiseOr, + BitwiseAnd, + BitshiftRight, + BitshiftLeft, Cmp(CmpOperator), }