-
-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement constant folding optmization
- Loading branch information
Showing
13 changed files
with
382 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
use boa_ast::{ | ||
expression::{ | ||
literal::Literal, | ||
operator::{ | ||
binary::{ArithmeticOp, BinaryOp, BitwiseOp, LogicalOp, RelationalOp}, | ||
unary::UnaryOp, | ||
Binary, Unary, | ||
}, | ||
}, | ||
Expression, | ||
}; | ||
use boa_interner::Interner; | ||
|
||
use super::{Pass, PassResult}; | ||
|
||
#[derive(Debug, Default)] | ||
pub(crate) struct ConstantFoldingOptimizer {} | ||
|
||
impl ConstantFoldingOptimizer { | ||
pub(crate) fn constant_fold_unary_expr( | ||
unary: &mut Unary, | ||
interner: &mut Interner, | ||
) -> PassResult<Expression> { | ||
let value = if let Expression::Literal(Literal::Int(integer)) = unary.target() { | ||
*integer | ||
} else { | ||
return PassResult::Leave; | ||
}; | ||
let literal = match unary.op() { | ||
UnaryOp::Minus => Literal::Int(-value), | ||
UnaryOp::Plus => Literal::Int(value), | ||
UnaryOp::Not => Literal::Bool(value == 0), | ||
UnaryOp::Tilde => Literal::Int(!value), | ||
UnaryOp::TypeOf => Literal::String(interner.get_or_intern("number")), | ||
UnaryOp::Delete => Literal::Bool(true), | ||
UnaryOp::Void => Literal::Undefined, | ||
}; | ||
|
||
PassResult::Replace(Expression::Literal(literal)) | ||
} | ||
pub(crate) fn constant_fold_binary_expr( | ||
binary: &mut Binary, | ||
_interner: &mut Interner, | ||
) -> PassResult<Expression> { | ||
let lhs = if let Expression::Literal(Literal::Int(integer)) = binary.lhs() { | ||
*integer | ||
} else { | ||
return PassResult::Leave; | ||
}; | ||
let rhs = if let Expression::Literal(Literal::Int(integer)) = binary.rhs() { | ||
*integer | ||
} else { | ||
return PassResult::Leave; | ||
}; | ||
|
||
let literal = match binary.op() { | ||
BinaryOp::Arithmetic(op) => match op { | ||
ArithmeticOp::Add => { | ||
if let Some(result) = lhs.checked_add(rhs) { | ||
Literal::Int(result) | ||
} else { | ||
Literal::Num(f64::from(lhs) + f64::from(rhs)) | ||
} | ||
} | ||
ArithmeticOp::Sub => { | ||
if let Some(result) = lhs.checked_sub(rhs) { | ||
Literal::Int(result) | ||
} else { | ||
Literal::Num(f64::from(lhs) - f64::from(rhs)) | ||
} | ||
} | ||
ArithmeticOp::Div => { | ||
if let Some(result) = lhs.checked_div(rhs).filter(|div| rhs * div == lhs) { | ||
Literal::Int(result) | ||
} else { | ||
Literal::Num(f64::from(lhs) / f64::from(rhs)) | ||
} | ||
} | ||
ArithmeticOp::Mul => { | ||
if let Some(result) = lhs.checked_mul(rhs) { | ||
Literal::Int(result) | ||
} else { | ||
Literal::Num(f64::from(lhs) - f64::from(rhs)) | ||
} | ||
} | ||
ArithmeticOp::Exp => { | ||
if rhs.is_positive() { | ||
if let Some(result) = lhs.checked_pow(rhs as u32) { | ||
Literal::Int(result) | ||
} else { | ||
Literal::Num(f64::from(lhs).powf(f64::from(rhs))) | ||
} | ||
} else { | ||
Literal::Num(f64::from(lhs).powf(f64::from(rhs))) | ||
} | ||
} | ||
ArithmeticOp::Mod => { | ||
if let Some(result) = lhs.checked_rem(rhs) { | ||
Literal::Int(result) | ||
} else { | ||
Literal::Num(f64::from(lhs) % f64::from(rhs)) | ||
} | ||
} | ||
}, | ||
BinaryOp::Bitwise(op) => match op { | ||
BitwiseOp::And => Literal::Int(lhs & rhs), | ||
BitwiseOp::Or => Literal::Int(lhs | rhs), | ||
BitwiseOp::Xor => Literal::Int(lhs ^ rhs), | ||
BitwiseOp::Shl => Literal::Int(lhs.wrapping_shl(rhs as u32)), | ||
BitwiseOp::Shr => Literal::Int(lhs.wrapping_shr(rhs as u32)), | ||
BitwiseOp::UShr => { | ||
let result = (lhs as u32).wrapping_shr(rhs as u32); | ||
if let Ok(result) = result.try_into() { | ||
Literal::Int(result) | ||
} else { | ||
Literal::Num(f64::from(result)) | ||
} | ||
} | ||
}, | ||
BinaryOp::Relational(op) => match op { | ||
RelationalOp::Equal | RelationalOp::StrictEqual => Literal::Bool(lhs == rhs), | ||
RelationalOp::NotEqual | RelationalOp::StrictNotEqual => Literal::Bool(lhs != rhs), | ||
RelationalOp::GreaterThan => Literal::Bool(lhs > rhs), | ||
RelationalOp::GreaterThanOrEqual => Literal::Bool(lhs >= rhs), | ||
RelationalOp::LessThan => Literal::Bool(lhs < rhs), | ||
RelationalOp::LessThanOrEqual => Literal::Bool(lhs <= rhs), | ||
RelationalOp::In | RelationalOp::InstanceOf => return PassResult::Leave, | ||
}, | ||
BinaryOp::Logical(op) => match op { | ||
LogicalOp::And => Literal::Int(if lhs == 0 { lhs } else { rhs }), | ||
LogicalOp::Or => Literal::Int(if lhs == 0 { rhs } else { lhs }), | ||
LogicalOp::Coalesce => Literal::Int(rhs), | ||
}, | ||
BinaryOp::Comma => return PassResult::Leave, | ||
}; | ||
PassResult::Replace(Expression::Literal(literal)) | ||
} | ||
} | ||
|
||
impl Pass for ConstantFoldingOptimizer { | ||
fn pass_expression( | ||
&mut self, | ||
expr: &mut Expression, | ||
interner: &mut Interner, | ||
) -> PassResult<Expression> { | ||
match expr { | ||
Expression::Unary(unary) => Self::constant_fold_unary_expr(unary, interner), | ||
Expression::Binary(binary) => Self::constant_fold_binary_expr(binary, interner), | ||
_ => PassResult::Leave, | ||
} | ||
} | ||
} |
Oops, something went wrong.