2020-04-12 17:00:06 +02:00
|
|
|
use crate::any::Dynamic;
|
2020-04-21 17:01:10 +02:00
|
|
|
use crate::calc_fn_hash;
|
2020-03-17 03:27:43 +01:00
|
|
|
use crate::engine::{
|
2020-04-21 17:01:10 +02:00
|
|
|
Engine, FnAny, FnCallArgs, FunctionsLib, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_PRINT,
|
|
|
|
KEYWORD_TYPE_OF,
|
2020-03-17 03:27:43 +01:00
|
|
|
};
|
2020-04-20 18:11:25 +02:00
|
|
|
use crate::packages::PackageLibrary;
|
2020-04-15 16:21:23 +02:00
|
|
|
use crate::parser::{map_dynamic_to_expr, Expr, FnDef, ReturnType, Stmt, AST};
|
2020-04-09 12:45:49 +02:00
|
|
|
use crate::result::EvalAltResult;
|
2020-03-25 04:27:18 +01:00
|
|
|
use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope};
|
2020-04-15 16:21:23 +02:00
|
|
|
use crate::token::Position;
|
2020-03-09 14:57:07 +01:00
|
|
|
|
2020-03-17 19:26:11 +01:00
|
|
|
use crate::stdlib::{
|
2020-03-18 11:41:18 +01:00
|
|
|
boxed::Box,
|
2020-04-09 12:45:49 +02:00
|
|
|
collections::HashMap,
|
2020-04-04 16:00:44 +02:00
|
|
|
rc::Rc,
|
2020-03-18 11:41:18 +01:00
|
|
|
string::{String, ToString},
|
2020-03-17 19:26:11 +01:00
|
|
|
sync::Arc,
|
2020-03-18 11:41:18 +01:00
|
|
|
vec,
|
|
|
|
vec::Vec,
|
2020-03-17 19:26:11 +01:00
|
|
|
};
|
2020-03-15 15:39:58 +01:00
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Level of optimization performed.
|
2020-04-03 13:42:01 +02:00
|
|
|
///
|
|
|
|
/// Not available under the `no_optimize` feature.
|
2020-03-15 15:39:58 +01:00
|
|
|
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
|
|
|
|
pub enum OptimizationLevel {
|
2020-03-18 03:36:50 +01:00
|
|
|
/// No optimization performed.
|
2020-03-15 15:39:58 +01:00
|
|
|
None,
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Only perform simple optimizations without evaluating functions.
|
2020-03-15 15:39:58 +01:00
|
|
|
Simple,
|
|
|
|
/// Full optimizations performed, including evaluating functions.
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Take care that this may cause side effects as it essentially assumes that all functions are pure.
|
2020-03-15 15:39:58 +01:00
|
|
|
Full,
|
|
|
|
}
|
|
|
|
|
2020-04-10 06:16:39 +02:00
|
|
|
impl OptimizationLevel {
|
|
|
|
/// Is the `OptimizationLevel` None.
|
|
|
|
pub fn is_none(self) -> bool {
|
|
|
|
self == Self::None
|
|
|
|
}
|
|
|
|
/// Is the `OptimizationLevel` Full.
|
|
|
|
pub fn is_full(self) -> bool {
|
|
|
|
self == Self::Full
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Mutable state throughout an optimization pass.
|
2020-03-15 15:39:58 +01:00
|
|
|
struct State<'a> {
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Has the AST been changed during this pass?
|
2020-03-13 11:12:41 +01:00
|
|
|
changed: bool,
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Collection of constants to use for eager function evaluations.
|
2020-03-13 11:12:41 +01:00
|
|
|
constants: Vec<(String, Expr)>,
|
2020-03-18 03:36:50 +01:00
|
|
|
/// An `Engine` instance for eager function evaluation.
|
2020-04-16 17:31:48 +02:00
|
|
|
engine: &'a Engine,
|
2020-04-07 07:23:06 +02:00
|
|
|
/// Library of script-defined functions.
|
|
|
|
fn_lib: &'a [(&'a str, usize)],
|
2020-04-08 03:30:50 +02:00
|
|
|
/// Optimization level.
|
|
|
|
optimization_level: OptimizationLevel,
|
2020-03-13 11:12:41 +01:00
|
|
|
}
|
|
|
|
|
2020-04-01 03:51:33 +02:00
|
|
|
impl<'a> State<'a> {
|
|
|
|
/// Create a new State.
|
2020-04-08 03:30:50 +02:00
|
|
|
pub fn new(
|
2020-04-16 17:31:48 +02:00
|
|
|
engine: &'a Engine,
|
2020-04-08 03:30:50 +02:00
|
|
|
fn_lib: &'a [(&'a str, usize)],
|
|
|
|
level: OptimizationLevel,
|
|
|
|
) -> Self {
|
2020-04-01 03:51:33 +02:00
|
|
|
Self {
|
|
|
|
changed: false,
|
|
|
|
constants: vec![],
|
|
|
|
engine,
|
2020-04-07 07:23:06 +02:00
|
|
|
fn_lib,
|
2020-04-08 03:30:50 +02:00
|
|
|
optimization_level: level,
|
2020-04-01 03:51:33 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Reset the state from dirty to clean.
|
2020-03-15 15:39:58 +01:00
|
|
|
pub fn reset(&mut self) {
|
|
|
|
self.changed = false;
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Set the AST state to be dirty (i.e. changed).
|
2020-03-13 11:12:41 +01:00
|
|
|
pub fn set_dirty(&mut self) {
|
|
|
|
self.changed = true;
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Is the AST dirty (i.e. changed)?
|
2020-03-13 11:12:41 +01:00
|
|
|
pub fn is_dirty(&self) -> bool {
|
|
|
|
self.changed
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Does a constant exist?
|
2020-03-13 11:12:41 +01:00
|
|
|
pub fn contains_constant(&self, name: &str) -> bool {
|
|
|
|
self.constants.iter().any(|(n, _)| n == name)
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Prune the list of constants back to a specified size.
|
2020-03-13 11:12:41 +01:00
|
|
|
pub fn restore_constants(&mut self, len: usize) {
|
|
|
|
self.constants.truncate(len)
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Add a new constant to the list.
|
2020-03-13 11:12:41 +01:00
|
|
|
pub fn push_constant(&mut self, name: &str, value: Expr) {
|
|
|
|
self.constants.push((name.to_string(), value))
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Look up a constant from the list.
|
2020-03-13 11:12:41 +01:00
|
|
|
pub fn find_constant(&self, name: &str) -> Option<&Expr> {
|
|
|
|
for (n, expr) in self.constants.iter().rev() {
|
|
|
|
if n == name {
|
|
|
|
return Some(expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 12:45:49 +02:00
|
|
|
/// Call a registered function
|
|
|
|
fn call_fn(
|
2020-04-20 18:11:25 +02:00
|
|
|
packages: &Vec<PackageLibrary>,
|
2020-04-19 07:40:11 +02:00
|
|
|
functions: &HashMap<u64, Box<FnAny>>,
|
2020-04-09 12:45:49 +02:00
|
|
|
fn_name: &str,
|
|
|
|
args: &mut FnCallArgs,
|
|
|
|
pos: Position,
|
2020-04-17 18:14:33 +02:00
|
|
|
) -> Result<Option<Dynamic>, Box<EvalAltResult>> {
|
2020-04-09 12:45:49 +02:00
|
|
|
// Search built-in's and external functions
|
2020-04-21 17:01:10 +02:00
|
|
|
let hash = calc_fn_hash(fn_name, args.iter().map(|a| a.type_id()));
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-09 12:45:49 +02:00
|
|
|
functions
|
2020-04-20 18:11:25 +02:00
|
|
|
.get(&hash)
|
|
|
|
.or_else(|| {
|
|
|
|
packages
|
|
|
|
.iter()
|
2020-04-21 17:01:10 +02:00
|
|
|
.find(|p| p.functions.contains_key(&hash))
|
|
|
|
.and_then(|p| p.functions.get(&hash))
|
2020-04-20 18:11:25 +02:00
|
|
|
})
|
2020-04-09 12:45:49 +02:00
|
|
|
.map(|func| func(args, pos))
|
|
|
|
.transpose()
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Optimize a statement.
|
2020-03-15 15:39:58 +01:00
|
|
|
fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) -> Stmt {
|
2020-03-09 14:57:07 +01:00
|
|
|
match stmt {
|
2020-03-18 03:36:50 +01:00
|
|
|
// if expr { Noop }
|
2020-03-22 03:18:16 +01:00
|
|
|
Stmt::IfThenElse(expr, if_block, None) if matches!(*if_block, Stmt::Noop(_)) => {
|
2020-03-13 11:12:41 +01:00
|
|
|
state.set_dirty();
|
2020-03-12 16:46:52 +01:00
|
|
|
|
|
|
|
let pos = expr.position();
|
2020-03-13 11:12:41 +01:00
|
|
|
let expr = optimize_expr(*expr, state);
|
2020-03-12 16:46:52 +01:00
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
if preserve_result {
|
|
|
|
// -> { expr, Noop }
|
|
|
|
Stmt::Block(vec![Stmt::Expr(Box::new(expr)), *if_block], pos)
|
2020-03-14 16:41:15 +01:00
|
|
|
} else {
|
2020-03-18 03:36:50 +01:00
|
|
|
// -> expr
|
|
|
|
Stmt::Expr(Box::new(expr))
|
2020-03-12 16:46:52 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// if expr { if_block }
|
2020-03-22 03:18:16 +01:00
|
|
|
Stmt::IfThenElse(expr, if_block, None) => match *expr {
|
2020-03-18 03:36:50 +01:00
|
|
|
// if false { if_block } -> Noop
|
2020-03-09 14:57:07 +01:00
|
|
|
Expr::False(pos) => {
|
2020-03-13 11:12:41 +01:00
|
|
|
state.set_dirty();
|
2020-03-09 14:57:07 +01:00
|
|
|
Stmt::Noop(pos)
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// if true { if_block } -> if_block
|
|
|
|
Expr::True(_) => optimize_stmt(*if_block, state, true),
|
|
|
|
// if expr { if_block }
|
2020-03-22 03:18:16 +01:00
|
|
|
expr => Stmt::IfThenElse(
|
2020-03-13 11:12:41 +01:00
|
|
|
Box::new(optimize_expr(expr, state)),
|
2020-03-18 03:36:50 +01:00
|
|
|
Box::new(optimize_stmt(*if_block, state, true)),
|
2020-03-09 14:57:07 +01:00
|
|
|
None,
|
|
|
|
),
|
|
|
|
},
|
2020-03-18 03:36:50 +01:00
|
|
|
// if expr { if_block } else { else_block }
|
2020-03-22 03:18:16 +01:00
|
|
|
Stmt::IfThenElse(expr, if_block, Some(else_block)) => match *expr {
|
2020-03-18 03:36:50 +01:00
|
|
|
// if false { if_block } else { else_block } -> else_block
|
|
|
|
Expr::False(_) => optimize_stmt(*else_block, state, true),
|
|
|
|
// if true { if_block } else { else_block } -> if_block
|
|
|
|
Expr::True(_) => optimize_stmt(*if_block, state, true),
|
|
|
|
// if expr { if_block } else { else_block }
|
2020-03-22 03:18:16 +01:00
|
|
|
expr => Stmt::IfThenElse(
|
2020-03-13 11:12:41 +01:00
|
|
|
Box::new(optimize_expr(expr, state)),
|
2020-03-18 03:36:50 +01:00
|
|
|
Box::new(optimize_stmt(*if_block, state, true)),
|
|
|
|
match optimize_stmt(*else_block, state, true) {
|
2020-04-23 07:24:24 +02:00
|
|
|
Stmt::Noop(_) => None, // Noop -> no else block
|
2020-03-12 16:46:52 +01:00
|
|
|
stmt => Some(Box::new(stmt)),
|
|
|
|
},
|
2020-03-09 14:57:07 +01:00
|
|
|
),
|
|
|
|
},
|
2020-03-18 03:36:50 +01:00
|
|
|
// while expr { block }
|
|
|
|
Stmt::While(expr, block) => match *expr {
|
|
|
|
// while false { block } -> Noop
|
2020-03-09 14:57:07 +01:00
|
|
|
Expr::False(pos) => {
|
2020-03-13 11:12:41 +01:00
|
|
|
state.set_dirty();
|
2020-03-09 14:57:07 +01:00
|
|
|
Stmt::Noop(pos)
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// while true { block } -> loop { block }
|
|
|
|
Expr::True(_) => Stmt::Loop(Box::new(optimize_stmt(*block, state, false))),
|
|
|
|
// while expr { block }
|
|
|
|
expr => match optimize_stmt(*block, state, false) {
|
|
|
|
// while expr { break; } -> { expr; }
|
2020-03-17 10:33:37 +01:00
|
|
|
Stmt::Break(pos) => {
|
|
|
|
// Only a single break statement - turn into running the guard expression once
|
|
|
|
state.set_dirty();
|
|
|
|
let mut statements = vec![Stmt::Expr(Box::new(optimize_expr(expr, state)))];
|
|
|
|
if preserve_result {
|
|
|
|
statements.push(Stmt::Noop(pos))
|
|
|
|
}
|
|
|
|
Stmt::Block(statements, pos)
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// while expr { block }
|
2020-03-17 10:33:37 +01:00
|
|
|
stmt => Stmt::While(Box::new(optimize_expr(expr, state)), Box::new(stmt)),
|
|
|
|
},
|
|
|
|
},
|
2020-03-18 03:36:50 +01:00
|
|
|
// loop { block }
|
|
|
|
Stmt::Loop(block) => match optimize_stmt(*block, state, false) {
|
|
|
|
// loop { break; } -> Noop
|
2020-03-17 10:33:37 +01:00
|
|
|
Stmt::Break(pos) => {
|
|
|
|
// Only a single break statement
|
|
|
|
state.set_dirty();
|
|
|
|
Stmt::Noop(pos)
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// loop { block }
|
2020-03-17 10:33:37 +01:00
|
|
|
stmt => Stmt::Loop(Box::new(stmt)),
|
2020-03-09 14:57:07 +01:00
|
|
|
},
|
2020-03-18 03:36:50 +01:00
|
|
|
// for id in expr { block }
|
|
|
|
Stmt::For(id, expr, block) => Stmt::For(
|
2020-03-09 14:57:07 +01:00
|
|
|
id,
|
2020-03-13 11:12:41 +01:00
|
|
|
Box::new(optimize_expr(*expr, state)),
|
2020-03-18 03:36:50 +01:00
|
|
|
Box::new(optimize_stmt(*block, state, false)),
|
2020-03-09 14:57:07 +01:00
|
|
|
),
|
2020-03-18 03:36:50 +01:00
|
|
|
// let id = expr;
|
2020-03-09 14:57:07 +01:00
|
|
|
Stmt::Let(id, Some(expr), pos) => {
|
2020-03-13 11:12:41 +01:00
|
|
|
Stmt::Let(id, Some(Box::new(optimize_expr(*expr, state))), pos)
|
2020-03-09 14:57:07 +01:00
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// let id;
|
2020-03-09 14:57:07 +01:00
|
|
|
Stmt::Let(_, None, _) => stmt,
|
2020-03-18 03:36:50 +01:00
|
|
|
// { block }
|
|
|
|
Stmt::Block(block, pos) => {
|
|
|
|
let orig_len = block.len(); // Original number of statements in the block, for change detection
|
|
|
|
let orig_constants_len = state.constants.len(); // Original number of constants in the state, for restore later
|
2020-03-09 14:57:07 +01:00
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
// Optimize each statement in the block
|
|
|
|
let mut result: Vec<_> = block
|
|
|
|
.into_iter()
|
2020-03-26 13:26:05 +01:00
|
|
|
.map(|stmt| match stmt {
|
|
|
|
// Add constant into the state
|
|
|
|
Stmt::Const(name, value, pos) => {
|
2020-03-13 11:12:41 +01:00
|
|
|
state.push_constant(&name, *value);
|
|
|
|
state.set_dirty();
|
|
|
|
Stmt::Noop(pos) // No need to keep constants
|
|
|
|
}
|
2020-03-26 13:26:05 +01:00
|
|
|
// Optimize the statement
|
|
|
|
_ => optimize_stmt(stmt, state, preserve_result),
|
2020-03-13 11:12:41 +01:00
|
|
|
})
|
2020-03-09 14:57:07 +01:00
|
|
|
.collect();
|
|
|
|
|
2020-03-11 16:43:10 +01:00
|
|
|
// Remove all raw expression statements that are pure except for the very last statement
|
|
|
|
let last_stmt = if preserve_result { result.pop() } else { None };
|
2020-03-10 04:22:41 +01:00
|
|
|
|
2020-03-17 10:33:37 +01:00
|
|
|
result.retain(|stmt| !stmt.is_pure());
|
2020-03-11 16:43:10 +01:00
|
|
|
|
|
|
|
if let Some(stmt) = last_stmt {
|
|
|
|
result.push(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all let statements at the end of a block - the new variables will go away anyway.
|
|
|
|
// But be careful only remove ones that have no initial values or have values that are pure expressions,
|
|
|
|
// otherwise there may be side effects.
|
|
|
|
let mut removed = false;
|
|
|
|
|
|
|
|
while let Some(expr) = result.pop() {
|
|
|
|
match expr {
|
|
|
|
Stmt::Let(_, None, _) => removed = true,
|
|
|
|
Stmt::Let(_, Some(val_expr), _) if val_expr.is_pure() => removed = true,
|
|
|
|
_ => {
|
|
|
|
result.push(expr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if preserve_result {
|
|
|
|
if removed {
|
|
|
|
result.push(Stmt::Noop(pos))
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
// Optimize all the statements again
|
2020-03-11 16:43:10 +01:00
|
|
|
result = result
|
|
|
|
.into_iter()
|
|
|
|
.rev()
|
|
|
|
.enumerate()
|
2020-03-18 03:36:50 +01:00
|
|
|
.map(|(i, s)| optimize_stmt(s, state, i == 0))
|
2020-03-11 16:43:10 +01:00
|
|
|
.rev()
|
|
|
|
.collect();
|
2020-03-10 04:22:41 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 10:33:37 +01:00
|
|
|
// Remove everything following the the first return/throw
|
|
|
|
let mut dead_code = false;
|
|
|
|
|
|
|
|
result.retain(|stmt| {
|
|
|
|
if dead_code {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
match stmt {
|
|
|
|
Stmt::ReturnWithVal(_, _, _) | Stmt::Break(_) => {
|
|
|
|
dead_code = true;
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
});
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
// Change detection
|
2020-03-13 11:12:41 +01:00
|
|
|
if orig_len != result.len() {
|
|
|
|
state.set_dirty();
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
// Pop the stack and remove all the local constants
|
2020-03-15 15:39:58 +01:00
|
|
|
state.restore_constants(orig_constants_len);
|
2020-03-09 14:57:07 +01:00
|
|
|
|
|
|
|
match result[..] {
|
2020-03-11 16:43:10 +01:00
|
|
|
// No statements in block - change to No-op
|
2020-03-09 14:57:07 +01:00
|
|
|
[] => {
|
2020-03-13 11:12:41 +01:00
|
|
|
state.set_dirty();
|
2020-03-10 04:22:41 +01:00
|
|
|
Stmt::Noop(pos)
|
|
|
|
}
|
2020-03-11 16:43:10 +01:00
|
|
|
// Only one statement - promote
|
2020-03-09 14:57:07 +01:00
|
|
|
[_] => {
|
2020-03-13 11:12:41 +01:00
|
|
|
state.set_dirty();
|
2020-03-09 14:57:07 +01:00
|
|
|
result.remove(0)
|
|
|
|
}
|
|
|
|
_ => Stmt::Block(result, pos),
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// expr;
|
2020-03-13 11:12:41 +01:00
|
|
|
Stmt::Expr(expr) => Stmt::Expr(Box::new(optimize_expr(*expr, state))),
|
2020-03-18 03:36:50 +01:00
|
|
|
// return expr;
|
2020-03-13 11:12:41 +01:00
|
|
|
Stmt::ReturnWithVal(Some(expr), is_return, pos) => {
|
|
|
|
Stmt::ReturnWithVal(Some(Box::new(optimize_expr(*expr, state))), is_return, pos)
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// All other statements - skip
|
2020-03-11 16:43:10 +01:00
|
|
|
stmt => stmt,
|
2020-03-09 14:57:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Optimize an expression.
|
2020-03-15 15:39:58 +01:00
|
|
|
fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr {
|
2020-03-18 03:36:50 +01:00
|
|
|
// These keywords are handled specially
|
2020-03-19 12:53:42 +01:00
|
|
|
const DONT_EVAL_KEYWORDS: [&str; 3] = [KEYWORD_PRINT, KEYWORD_DEBUG, KEYWORD_EVAL];
|
2020-03-17 03:27:43 +01:00
|
|
|
|
2020-03-09 14:57:07 +01:00
|
|
|
match expr {
|
2020-03-18 03:36:50 +01:00
|
|
|
// ( stmt )
|
2020-03-13 11:12:41 +01:00
|
|
|
Expr::Stmt(stmt, pos) => match optimize_stmt(*stmt, state, true) {
|
2020-03-18 03:36:50 +01:00
|
|
|
// ( Noop ) -> ()
|
2020-03-09 14:57:07 +01:00
|
|
|
Stmt::Noop(_) => {
|
2020-03-13 11:12:41 +01:00
|
|
|
state.set_dirty();
|
2020-03-09 14:57:07 +01:00
|
|
|
Expr::Unit(pos)
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// ( expr ) -> expr
|
2020-03-09 14:57:07 +01:00
|
|
|
Stmt::Expr(expr) => {
|
2020-03-13 11:12:41 +01:00
|
|
|
state.set_dirty();
|
2020-03-09 14:57:07 +01:00
|
|
|
*expr
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// ( stmt )
|
2020-03-09 14:57:07 +01:00
|
|
|
stmt => Expr::Stmt(Box::new(stmt), pos),
|
|
|
|
},
|
2020-03-18 03:36:50 +01:00
|
|
|
// id = expr
|
|
|
|
Expr::Assignment(id, expr, pos) => match *expr {
|
|
|
|
//id = id2 = expr2
|
|
|
|
Expr::Assignment(id2, expr2, pos2) => match (*id, *id2) {
|
|
|
|
// var = var = expr2 -> var = expr2
|
|
|
|
(Expr::Variable(var, _), Expr::Variable(var2, _)) if var == var2 => {
|
2020-03-14 04:51:45 +01:00
|
|
|
// Assignment to the same variable - fold
|
|
|
|
state.set_dirty();
|
|
|
|
|
|
|
|
Expr::Assignment(
|
2020-03-18 03:36:50 +01:00
|
|
|
Box::new(Expr::Variable(var, pos)),
|
2020-03-14 04:51:45 +01:00
|
|
|
Box::new(optimize_expr(*expr2, state)),
|
2020-03-18 03:36:50 +01:00
|
|
|
pos,
|
2020-03-14 04:51:45 +01:00
|
|
|
)
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// id1 = id2 = expr2
|
2020-03-14 04:51:45 +01:00
|
|
|
(id1, id2) => Expr::Assignment(
|
|
|
|
Box::new(id1),
|
|
|
|
Box::new(Expr::Assignment(
|
|
|
|
Box::new(id2),
|
|
|
|
Box::new(optimize_expr(*expr2, state)),
|
|
|
|
pos2,
|
|
|
|
)),
|
2020-03-18 03:36:50 +01:00
|
|
|
pos,
|
2020-03-14 04:51:45 +01:00
|
|
|
),
|
|
|
|
},
|
2020-03-18 03:36:50 +01:00
|
|
|
// id = expr
|
|
|
|
expr => Expr::Assignment(id, Box::new(optimize_expr(expr, state)), pos),
|
2020-03-14 04:51:45 +01:00
|
|
|
},
|
2020-04-10 15:02:13 +02:00
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
// lhs.rhs
|
2020-03-29 11:15:12 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-04-10 15:02:13 +02:00
|
|
|
Expr::Dot(lhs, rhs, pos) => match (*lhs, *rhs) {
|
|
|
|
// map.string
|
|
|
|
(Expr::Map(items, pos), Expr::Property(s, _))
|
|
|
|
if items.iter().all(|(_, x, _)| x.is_pure()) =>
|
|
|
|
{
|
|
|
|
// Map literal where everything is pure - promote the indexed item.
|
|
|
|
// All other items can be thrown away.
|
|
|
|
state.set_dirty();
|
|
|
|
items.into_iter().find(|(name, _, _)| name == s.as_ref())
|
|
|
|
.map(|(_, expr, _)| expr.set_position(pos))
|
|
|
|
.unwrap_or_else(|| Expr::Unit(pos))
|
|
|
|
}
|
|
|
|
// lhs.rhs
|
|
|
|
(lhs, rhs) => Expr::Dot(
|
|
|
|
Box::new(optimize_expr(lhs, state)),
|
|
|
|
Box::new(optimize_expr(rhs, state)),
|
|
|
|
pos,
|
|
|
|
)
|
|
|
|
}
|
2020-03-11 04:03:18 +01:00
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
// lhs[rhs]
|
2020-03-10 10:10:33 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
Expr::Index(lhs, rhs, pos) => match (*lhs, *rhs) {
|
2020-03-18 03:36:50 +01:00
|
|
|
// array[int]
|
2020-04-10 15:02:13 +02:00
|
|
|
(Expr::Array(mut items, pos), Expr::IntegerConstant(i, _))
|
|
|
|
if i >= 0 && (i as usize) < items.len() && items.iter().all(Expr::is_pure) =>
|
2020-03-10 10:10:33 +01:00
|
|
|
{
|
2020-03-16 05:41:52 +01:00
|
|
|
// Array literal where everything is pure - promote the indexed item.
|
2020-03-11 16:43:10 +01:00
|
|
|
// All other items can be thrown away.
|
2020-03-13 11:12:41 +01:00
|
|
|
state.set_dirty();
|
2020-04-10 15:02:13 +02:00
|
|
|
items.remove(i as usize).set_position(pos)
|
|
|
|
}
|
|
|
|
// map[string]
|
|
|
|
(Expr::Map(items, pos), Expr::StringConstant(s, _))
|
|
|
|
if items.iter().all(|(_, x, _)| x.is_pure()) =>
|
|
|
|
{
|
|
|
|
// Map literal where everything is pure - promote the indexed item.
|
|
|
|
// All other items can be thrown away.
|
|
|
|
state.set_dirty();
|
|
|
|
items.into_iter().find(|(name, _, _)| name == s.as_ref())
|
|
|
|
.map(|(_, expr, _)| expr.set_position(pos))
|
|
|
|
.unwrap_or_else(|| Expr::Unit(pos))
|
2020-03-10 10:10:33 +01:00
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// string[int]
|
2020-03-17 03:27:43 +01:00
|
|
|
(Expr::StringConstant(s, pos), Expr::IntegerConstant(i, _))
|
2020-03-16 05:41:52 +01:00
|
|
|
if i >= 0 && (i as usize) < s.chars().count() =>
|
|
|
|
{
|
|
|
|
// String literal indexing - get the character
|
|
|
|
state.set_dirty();
|
|
|
|
Expr::CharConstant(s.chars().nth(i as usize).expect("should get char"), pos)
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// lhs[rhs]
|
2020-03-10 10:10:33 +01:00
|
|
|
(lhs, rhs) => Expr::Index(
|
2020-03-13 11:12:41 +01:00
|
|
|
Box::new(optimize_expr(lhs, state)),
|
|
|
|
Box::new(optimize_expr(rhs, state)),
|
2020-03-10 10:10:33 +01:00
|
|
|
pos,
|
|
|
|
),
|
|
|
|
},
|
2020-03-18 03:36:50 +01:00
|
|
|
// [ items .. ]
|
2020-03-10 10:10:33 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-04-01 03:51:33 +02:00
|
|
|
Expr::Array(items, pos) => Expr::Array(items
|
2020-03-09 14:57:07 +01:00
|
|
|
.into_iter()
|
2020-03-13 11:12:41 +01:00
|
|
|
.map(|expr| optimize_expr(expr, state))
|
2020-04-01 03:51:33 +02:00
|
|
|
.collect(), pos),
|
2020-03-29 17:53:35 +02:00
|
|
|
// [ items .. ]
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-04-01 03:51:33 +02:00
|
|
|
Expr::Map(items, pos) => Expr::Map(items
|
2020-03-29 17:53:35 +02:00
|
|
|
.into_iter()
|
|
|
|
.map(|(key, expr, pos)| (key, optimize_expr(expr, state), pos))
|
2020-04-01 03:51:33 +02:00
|
|
|
.collect(), pos),
|
2020-04-06 11:47:34 +02:00
|
|
|
// lhs in rhs
|
|
|
|
Expr::In(lhs, rhs, pos) => match (*lhs, *rhs) {
|
|
|
|
// "xxx" in "xxxxx"
|
|
|
|
(Expr::StringConstant(lhs, pos), Expr::StringConstant(rhs, _)) => {
|
|
|
|
state.set_dirty();
|
|
|
|
if rhs.contains(lhs.as_ref()) {
|
|
|
|
Expr::True(pos)
|
|
|
|
} else {
|
|
|
|
Expr::False(pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 'x' in "xxxxx"
|
|
|
|
(Expr::CharConstant(lhs, pos), Expr::StringConstant(rhs, _)) => {
|
|
|
|
state.set_dirty();
|
|
|
|
if rhs.contains(&lhs.to_string()) {
|
|
|
|
Expr::True(pos)
|
|
|
|
} else {
|
|
|
|
Expr::False(pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// "xxx" in #{...}
|
|
|
|
(Expr::StringConstant(lhs, pos), Expr::Map(items, _)) => {
|
|
|
|
state.set_dirty();
|
|
|
|
if items.iter().find(|(name, _, _)| name == &lhs).is_some() {
|
|
|
|
Expr::True(pos)
|
|
|
|
} else {
|
|
|
|
Expr::False(pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 'x' in #{...}
|
|
|
|
(Expr::CharConstant(lhs, pos), Expr::Map(items, _)) => {
|
|
|
|
state.set_dirty();
|
|
|
|
let lhs = lhs.to_string();
|
|
|
|
|
|
|
|
if items.iter().find(|(name, _, _)| name == &lhs).is_some() {
|
|
|
|
Expr::True(pos)
|
|
|
|
} else {
|
|
|
|
Expr::False(pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// lhs in rhs
|
|
|
|
(lhs, rhs) => Expr::In(
|
|
|
|
Box::new(optimize_expr(lhs, state)),
|
|
|
|
Box::new(optimize_expr(rhs, state)),
|
|
|
|
pos
|
|
|
|
),
|
|
|
|
},
|
2020-03-18 03:36:50 +01:00
|
|
|
// lhs && rhs
|
2020-04-06 11:47:34 +02:00
|
|
|
Expr::And(lhs, rhs, pos) => match (*lhs, *rhs) {
|
2020-03-18 03:36:50 +01:00
|
|
|
// true && rhs -> rhs
|
2020-03-09 14:57:07 +01:00
|
|
|
(Expr::True(_), rhs) => {
|
2020-03-13 11:12:41 +01:00
|
|
|
state.set_dirty();
|
2020-03-09 14:57:07 +01:00
|
|
|
rhs
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// false && rhs -> false
|
2020-03-09 14:57:07 +01:00
|
|
|
(Expr::False(pos), _) => {
|
2020-03-13 11:12:41 +01:00
|
|
|
state.set_dirty();
|
2020-03-09 14:57:07 +01:00
|
|
|
Expr::False(pos)
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// lhs && true -> lhs
|
2020-03-09 14:57:07 +01:00
|
|
|
(lhs, Expr::True(_)) => {
|
2020-03-13 11:12:41 +01:00
|
|
|
state.set_dirty();
|
2020-03-18 03:36:50 +01:00
|
|
|
optimize_expr(lhs, state)
|
2020-03-09 14:57:07 +01:00
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// lhs && rhs
|
2020-03-09 14:57:07 +01:00
|
|
|
(lhs, rhs) => Expr::And(
|
2020-03-13 11:12:41 +01:00
|
|
|
Box::new(optimize_expr(lhs, state)),
|
|
|
|
Box::new(optimize_expr(rhs, state)),
|
2020-04-06 11:47:34 +02:00
|
|
|
pos
|
2020-03-09 14:57:07 +01:00
|
|
|
),
|
|
|
|
},
|
2020-03-18 03:36:50 +01:00
|
|
|
// lhs || rhs
|
2020-04-06 11:47:34 +02:00
|
|
|
Expr::Or(lhs, rhs, pos) => match (*lhs, *rhs) {
|
2020-03-18 03:36:50 +01:00
|
|
|
// false || rhs -> rhs
|
2020-03-09 14:57:07 +01:00
|
|
|
(Expr::False(_), rhs) => {
|
2020-03-13 11:12:41 +01:00
|
|
|
state.set_dirty();
|
2020-03-09 14:57:07 +01:00
|
|
|
rhs
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// true || rhs -> true
|
2020-03-09 14:57:07 +01:00
|
|
|
(Expr::True(pos), _) => {
|
2020-03-13 11:12:41 +01:00
|
|
|
state.set_dirty();
|
2020-03-09 14:57:07 +01:00
|
|
|
Expr::True(pos)
|
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// lhs || false
|
2020-03-09 14:57:07 +01:00
|
|
|
(lhs, Expr::False(_)) => {
|
2020-03-13 11:12:41 +01:00
|
|
|
state.set_dirty();
|
2020-03-18 03:36:50 +01:00
|
|
|
optimize_expr(lhs, state)
|
2020-03-09 14:57:07 +01:00
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// lhs || rhs
|
2020-03-09 14:57:07 +01:00
|
|
|
(lhs, rhs) => Expr::Or(
|
2020-03-13 11:12:41 +01:00
|
|
|
Box::new(optimize_expr(lhs, state)),
|
|
|
|
Box::new(optimize_expr(rhs, state)),
|
2020-04-06 11:47:34 +02:00
|
|
|
pos
|
2020-03-09 14:57:07 +01:00
|
|
|
),
|
|
|
|
},
|
2020-03-11 16:43:10 +01:00
|
|
|
|
2020-04-01 03:51:33 +02:00
|
|
|
// Do not call some special keywords
|
2020-04-06 06:29:01 +02:00
|
|
|
Expr::FunctionCall(id, args, def_value, pos) if DONT_EVAL_KEYWORDS.contains(&id.as_ref())=>
|
2020-03-19 15:29:02 +01:00
|
|
|
Expr::FunctionCall(id, args.into_iter().map(|a| optimize_expr(a, state)).collect(), def_value, pos),
|
2020-03-19 12:53:42 +01:00
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
// Eagerly call functions
|
2020-03-15 15:39:58 +01:00
|
|
|
Expr::FunctionCall(id, args, def_value, pos)
|
2020-04-08 03:30:50 +02:00
|
|
|
if state.optimization_level == OptimizationLevel::Full // full optimizations
|
2020-03-15 15:39:58 +01:00
|
|
|
&& args.iter().all(|expr| expr.is_constant()) // all arguments are constants
|
2020-03-17 03:27:43 +01:00
|
|
|
=> {
|
2020-03-19 15:29:02 +01:00
|
|
|
// First search in script-defined functions (can override built-in)
|
2020-04-07 07:23:06 +02:00
|
|
|
if state.fn_lib.iter().find(|(name, len)| name == &id && *len == args.len()).is_some() {
|
|
|
|
// A script-defined function overrides the built-in function - do not make the call
|
|
|
|
return Expr::FunctionCall(id, args.into_iter().map(|a| optimize_expr(a, state)).collect(), def_value, pos);
|
2020-03-19 15:29:02 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 15:39:58 +01:00
|
|
|
let mut arg_values: Vec<_> = args.iter().map(Expr::get_constant_value).collect();
|
2020-04-12 17:00:06 +02:00
|
|
|
let mut call_args: Vec<_> = arg_values.iter_mut().collect();
|
2020-03-17 03:27:43 +01:00
|
|
|
|
|
|
|
// Save the typename of the first argument if it is `type_of()`
|
|
|
|
// This is to avoid `call_args` being passed into the closure
|
|
|
|
let arg_for_type_of = if id == KEYWORD_TYPE_OF && call_args.len() == 1 {
|
2020-03-18 03:36:50 +01:00
|
|
|
state.engine.map_type_name(call_args[0].type_name())
|
2020-03-17 03:27:43 +01:00
|
|
|
} else {
|
|
|
|
""
|
|
|
|
};
|
|
|
|
|
2020-04-20 18:11:25 +02:00
|
|
|
call_fn(&state.engine.packages, &state.engine.functions, &id, &mut call_args, pos).ok()
|
2020-04-09 12:45:49 +02:00
|
|
|
.and_then(|result|
|
|
|
|
result.or_else(|| {
|
|
|
|
if !arg_for_type_of.is_empty() {
|
|
|
|
// Handle `type_of()`
|
2020-04-22 06:12:13 +02:00
|
|
|
Some(arg_for_type_of.to_string().into())
|
2020-04-09 12:45:49 +02:00
|
|
|
} else {
|
|
|
|
// Otherwise use the default value, if any
|
|
|
|
def_value.clone()
|
|
|
|
}
|
|
|
|
}).and_then(|result| map_dynamic_to_expr(result, pos))
|
2020-03-15 15:39:58 +01:00
|
|
|
.map(|expr| {
|
|
|
|
state.set_dirty();
|
|
|
|
expr
|
2020-03-17 03:27:43 +01:00
|
|
|
})
|
2020-04-09 12:45:49 +02:00
|
|
|
).unwrap_or_else(||
|
|
|
|
// Optimize function call arguments
|
|
|
|
Expr::FunctionCall(id, args.into_iter().map(|a| optimize_expr(a, state)).collect(), def_value, pos)
|
|
|
|
)
|
2020-03-15 15:39:58 +01:00
|
|
|
}
|
2020-03-19 12:53:42 +01:00
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
// id(args ..) -> optimize function call arguments
|
2020-03-17 03:27:43 +01:00
|
|
|
Expr::FunctionCall(id, args, def_value, pos) =>
|
2020-03-19 15:29:02 +01:00
|
|
|
Expr::FunctionCall(id, args.into_iter().map(|a| optimize_expr(a, state)).collect(), def_value, pos),
|
2020-03-19 12:53:42 +01:00
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
// constant-name
|
2020-04-10 15:02:13 +02:00
|
|
|
Expr::Variable(name, pos) if state.contains_constant(&name) => {
|
2020-03-13 11:12:41 +01:00
|
|
|
state.set_dirty();
|
|
|
|
|
|
|
|
// Replace constant with value
|
2020-04-10 15:02:13 +02:00
|
|
|
state.find_constant(&name).expect("should find constant in scope!").clone().set_position(pos)
|
2020-03-13 11:12:41 +01:00
|
|
|
}
|
2020-03-19 12:53:42 +01:00
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
// All other expressions - skip
|
2020-03-11 16:43:10 +01:00
|
|
|
expr => expr,
|
2020-03-09 14:57:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 06:16:39 +02:00
|
|
|
fn optimize<'a>(
|
2020-04-07 07:23:06 +02:00
|
|
|
statements: Vec<Stmt>,
|
2020-04-16 17:31:48 +02:00
|
|
|
engine: &Engine,
|
2020-04-07 07:23:06 +02:00
|
|
|
scope: &Scope,
|
|
|
|
fn_lib: &'a [(&'a str, usize)],
|
2020-04-08 03:30:50 +02:00
|
|
|
level: OptimizationLevel,
|
2020-04-07 07:23:06 +02:00
|
|
|
) -> Vec<Stmt> {
|
2020-03-15 15:39:58 +01:00
|
|
|
// If optimization level is None then skip optimizing
|
2020-04-08 03:30:50 +02:00
|
|
|
if level == OptimizationLevel::None {
|
2020-03-15 15:39:58 +01:00
|
|
|
return statements;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the state
|
2020-04-08 03:30:50 +02:00
|
|
|
let mut state = State::new(engine, fn_lib, level);
|
2020-03-15 15:39:58 +01:00
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
// Add constants from the scope into the state
|
2020-03-15 15:39:58 +01:00
|
|
|
scope
|
|
|
|
.iter()
|
2020-03-25 04:27:18 +01:00
|
|
|
.filter(|ScopeEntry { typ, expr, .. }| {
|
2020-03-15 15:39:58 +01:00
|
|
|
// Get all the constants with definite constant expressions
|
2020-03-25 04:27:18 +01:00
|
|
|
*typ == ScopeEntryType::Constant
|
2020-04-27 15:14:34 +02:00
|
|
|
&& expr.as_ref().map(|v| v.is_constant()).unwrap_or(false)
|
2020-03-15 15:39:58 +01:00
|
|
|
})
|
|
|
|
.for_each(|ScopeEntry { name, expr, .. }| {
|
|
|
|
state.push_constant(
|
|
|
|
name.as_ref(),
|
2020-04-27 15:14:34 +02:00
|
|
|
(**expr.as_ref().expect("should be Some(expr)")).clone(),
|
2020-03-15 15:39:58 +01:00
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
let orig_constants_len = state.constants.len();
|
|
|
|
|
2020-03-11 16:43:10 +01:00
|
|
|
let mut result = statements;
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
// Optimization loop
|
2020-03-09 14:57:07 +01:00
|
|
|
loop {
|
2020-03-15 15:39:58 +01:00
|
|
|
state.reset();
|
|
|
|
state.restore_constants(orig_constants_len);
|
2020-03-09 14:57:07 +01:00
|
|
|
|
2020-03-15 15:39:58 +01:00
|
|
|
let num_statements = result.len();
|
2020-03-14 07:30:44 +01:00
|
|
|
|
2020-03-11 16:43:10 +01:00
|
|
|
result = result
|
2020-03-09 14:57:07 +01:00
|
|
|
.into_iter()
|
2020-03-11 16:43:10 +01:00
|
|
|
.enumerate()
|
|
|
|
.map(|(i, stmt)| {
|
2020-04-11 12:09:03 +02:00
|
|
|
match stmt {
|
|
|
|
Stmt::Const(ref name, ref value, _) => {
|
|
|
|
// Load constants
|
|
|
|
state.push_constant(name.as_ref(), value.as_ref().clone());
|
|
|
|
stmt // Keep it in the global scope
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Keep all variable declarations at this level
|
|
|
|
// and always keep the last return value
|
|
|
|
let keep = matches!(stmt, Stmt::Let(_, _, _)) || i == num_statements - 1;
|
|
|
|
optimize_stmt(stmt, &mut state, keep)
|
|
|
|
}
|
2020-03-13 11:12:41 +01:00
|
|
|
}
|
2020-03-11 16:43:10 +01:00
|
|
|
})
|
2020-03-09 14:57:07 +01:00
|
|
|
.collect();
|
|
|
|
|
2020-03-13 11:12:41 +01:00
|
|
|
if !state.is_dirty() {
|
2020-03-09 14:57:07 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:46:52 +01:00
|
|
|
// Eliminate code that is pure but always keep the last statement
|
2020-03-11 16:43:10 +01:00
|
|
|
let last_stmt = result.pop();
|
|
|
|
|
2020-03-17 07:29:22 +01:00
|
|
|
// Remove all pure statements at global level
|
2020-03-18 11:41:18 +01:00
|
|
|
result.retain(|stmt| !stmt.is_pure());
|
2020-03-11 16:43:10 +01:00
|
|
|
|
2020-03-18 11:41:18 +01:00
|
|
|
// Add back the last statement unless it is a lone No-op
|
2020-03-11 16:43:10 +01:00
|
|
|
if let Some(stmt) = last_stmt {
|
2020-03-24 09:57:35 +01:00
|
|
|
if !result.is_empty() || !matches!(stmt, Stmt::Noop(_)) {
|
2020-03-18 11:41:18 +01:00
|
|
|
result.push(stmt);
|
|
|
|
}
|
2020-03-11 16:43:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
result
|
2020-03-09 14:57:07 +01:00
|
|
|
}
|
2020-03-15 15:39:58 +01:00
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Optimize an AST.
|
2020-03-18 11:41:18 +01:00
|
|
|
pub fn optimize_into_ast(
|
2020-03-15 15:39:58 +01:00
|
|
|
engine: &Engine,
|
|
|
|
scope: &Scope,
|
|
|
|
statements: Vec<Stmt>,
|
|
|
|
functions: Vec<FnDef>,
|
2020-04-08 03:30:50 +02:00
|
|
|
level: OptimizationLevel,
|
2020-03-15 15:39:58 +01:00
|
|
|
) -> AST {
|
2020-04-10 06:16:39 +02:00
|
|
|
#[cfg(feature = "no_optimize")]
|
|
|
|
const level: OptimizationLevel = OptimizationLevel::None;
|
|
|
|
|
2020-04-07 07:23:06 +02:00
|
|
|
let fn_lib: Vec<_> = functions
|
|
|
|
.iter()
|
|
|
|
.map(|fn_def| (fn_def.name.as_str(), fn_def.params.len()))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let lib = FunctionsLib::from_vec(
|
2020-03-15 15:39:58 +01:00
|
|
|
functions
|
2020-04-04 16:00:44 +02:00
|
|
|
.iter()
|
|
|
|
.cloned()
|
2020-03-15 15:39:58 +01:00
|
|
|
.map(|mut fn_def| {
|
2020-04-10 06:16:39 +02:00
|
|
|
if !level.is_none() {
|
2020-03-24 02:49:37 +01:00
|
|
|
let pos = fn_def.body.position();
|
|
|
|
|
|
|
|
// Optimize the function body
|
2020-04-08 03:30:50 +02:00
|
|
|
let mut body =
|
|
|
|
optimize(vec![fn_def.body], engine, &Scope::new(), &fn_lib, level);
|
2020-03-24 02:49:37 +01:00
|
|
|
|
|
|
|
// {} -> Noop
|
|
|
|
fn_def.body = match body.pop().unwrap_or_else(|| Stmt::Noop(pos)) {
|
|
|
|
// { return val; } -> val
|
|
|
|
Stmt::ReturnWithVal(Some(val), ReturnType::Return, _) => Stmt::Expr(val),
|
|
|
|
// { return; } -> ()
|
|
|
|
Stmt::ReturnWithVal(None, ReturnType::Return, pos) => {
|
|
|
|
Stmt::Expr(Box::new(Expr::Unit(pos)))
|
|
|
|
}
|
|
|
|
// All others
|
|
|
|
stmt => stmt,
|
|
|
|
};
|
2020-03-15 15:39:58 +01:00
|
|
|
}
|
2020-04-04 16:00:44 +02:00
|
|
|
fn_def
|
2020-03-15 15:39:58 +01:00
|
|
|
})
|
|
|
|
.collect(),
|
2020-04-04 16:00:44 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
AST(
|
2020-04-08 03:30:50 +02:00
|
|
|
match level {
|
2020-04-04 16:00:44 +02:00
|
|
|
OptimizationLevel::None => statements,
|
|
|
|
OptimizationLevel::Simple | OptimizationLevel::Full => {
|
2020-04-08 03:30:50 +02:00
|
|
|
optimize(statements, engine, &scope, &fn_lib, level)
|
2020-04-04 16:00:44 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
#[cfg(feature = "sync")]
|
2020-04-07 07:23:06 +02:00
|
|
|
Arc::new(lib),
|
2020-04-04 16:00:44 +02:00
|
|
|
#[cfg(not(feature = "sync"))]
|
2020-04-07 07:23:06 +02:00
|
|
|
Rc::new(lib),
|
2020-03-15 15:39:58 +01:00
|
|
|
)
|
|
|
|
}
|