2020-11-25 09:36:06 +08:00
|
|
|
//! Module implementing the [`AST`] optimizer.
|
2021-11-27 23:20:05 +08:00
|
|
|
#![cfg(not(feature = "no_optimize"))]
|
2020-07-23 18:40:42 +08:00
|
|
|
|
2022-07-18 13:40:41 +08:00
|
|
|
use crate::ast::{
|
|
|
|
ASTFlags, Expr, OpAssignment, Stmt, StmtBlock, StmtBlockContainer, SwitchCasesCollection,
|
|
|
|
};
|
2022-12-03 12:08:35 +08:00
|
|
|
use crate::engine::{KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_PRINT, KEYWORD_TYPE_OF};
|
2022-04-16 16:36:53 +08:00
|
|
|
use crate::eval::{Caches, GlobalRuntimeState};
|
2021-11-13 22:36:23 +08:00
|
|
|
use crate::func::builtin::get_builtin_binary_op_fn;
|
|
|
|
use crate::func::hashing::get_hasher;
|
2022-11-23 11:36:30 +08:00
|
|
|
use crate::module::ModuleFlags;
|
2022-10-29 12:09:18 +08:00
|
|
|
use crate::tokenizer::Token;
|
2021-11-13 22:36:23 +08:00
|
|
|
use crate::types::dynamic::AccessMode;
|
2021-12-27 22:28:11 +08:00
|
|
|
use crate::{
|
2022-12-23 14:26:06 +08:00
|
|
|
calc_fn_hash, calc_fn_hash_full, Dynamic, Engine, FlowControl, FnPtr, Identifier,
|
|
|
|
ImmutableString, Position, Scope, StaticVec, AST,
|
2021-12-27 22:28:11 +08:00
|
|
|
};
|
2021-04-17 15:15:54 +08:00
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
|
|
|
use std::{
|
2021-12-27 22:28:11 +08:00
|
|
|
any::TypeId,
|
2021-12-02 12:49:57 +08:00
|
|
|
convert::TryFrom,
|
2021-04-17 15:15:54 +08:00
|
|
|
hash::{Hash, Hasher},
|
|
|
|
mem,
|
|
|
|
};
|
2020-03-15 22:39:58 +08:00
|
|
|
|
2022-12-03 12:08:35 +08:00
|
|
|
/// Standard not operator.
|
|
|
|
const OP_NOT: &str = Token::Bang.literal_syntax();
|
|
|
|
|
2020-03-18 10:36:50 +08:00
|
|
|
/// Level of optimization performed.
|
2020-03-15 22:39:58 +08:00
|
|
|
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
|
2022-05-03 21:55:08 +08:00
|
|
|
#[non_exhaustive]
|
2020-03-15 22:39:58 +08:00
|
|
|
pub enum OptimizationLevel {
|
2020-03-18 10:36:50 +08:00
|
|
|
/// No optimization performed.
|
2020-03-15 22:39:58 +08:00
|
|
|
None,
|
2020-03-18 10:36:50 +08:00
|
|
|
/// Only perform simple optimizations without evaluating functions.
|
2020-03-15 22:39:58 +08:00
|
|
|
Simple,
|
|
|
|
/// Full optimizations performed, including evaluating functions.
|
2020-03-18 10:36:50 +08:00
|
|
|
/// Take care that this may cause side effects as it essentially assumes that all functions are pure.
|
2020-03-15 22:39:58 +08:00
|
|
|
Full,
|
|
|
|
}
|
|
|
|
|
2021-05-18 12:24:11 +08:00
|
|
|
impl Default for OptimizationLevel {
|
2020-10-08 22:25:50 +08:00
|
|
|
#[inline(always)]
|
2022-09-28 12:06:22 +08:00
|
|
|
#[must_use]
|
2021-05-18 12:24:11 +08:00
|
|
|
fn default() -> Self {
|
2021-11-29 10:17:04 +08:00
|
|
|
Self::Simple
|
2020-04-10 12:16:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 10:36:50 +08:00
|
|
|
/// Mutable state throughout an optimization pass.
|
2020-10-09 13:26:32 +08:00
|
|
|
#[derive(Debug, Clone)]
|
2021-07-04 16:40:15 +08:00
|
|
|
struct OptimizerState<'a> {
|
2020-11-25 09:36:06 +08:00
|
|
|
/// Has the [`AST`] been changed during this pass?
|
2020-03-13 18:12:41 +08:00
|
|
|
changed: bool,
|
2020-03-18 10:36:50 +08:00
|
|
|
/// Collection of constants to use for eager function evaluations.
|
2022-12-20 16:52:55 +08:00
|
|
|
variables: StaticVec<(Identifier, AccessMode, Option<Dynamic>)>,
|
2021-01-12 23:52:50 +08:00
|
|
|
/// Activate constants propagation?
|
|
|
|
propagate_constants: bool,
|
2020-11-25 09:36:06 +08:00
|
|
|
/// An [`Engine`] instance for eager function evaluation.
|
2020-04-16 23:31:48 +08:00
|
|
|
engine: &'a Engine,
|
2022-05-02 00:03:45 +08:00
|
|
|
/// The global runtime state.
|
2022-11-07 16:18:59 +08:00
|
|
|
global: GlobalRuntimeState,
|
2022-05-02 00:03:45 +08:00
|
|
|
/// Function resolution caches.
|
2022-11-07 16:18:59 +08:00
|
|
|
caches: Caches,
|
2020-04-08 09:30:50 +08:00
|
|
|
/// Optimization level.
|
|
|
|
optimization_level: OptimizationLevel,
|
2020-03-13 18:12:41 +08:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:40:15 +08:00
|
|
|
impl<'a> OptimizerState<'a> {
|
2022-11-25 20:42:16 +08:00
|
|
|
/// Create a new [`OptimizerState`].
|
2020-10-08 22:25:50 +08:00
|
|
|
#[inline(always)]
|
2022-05-02 00:03:45 +08:00
|
|
|
pub fn new(
|
2021-03-11 18:29:22 +08:00
|
|
|
engine: &'a Engine,
|
2022-11-28 23:24:22 +08:00
|
|
|
lib: &'a [crate::SharedModule],
|
2021-03-11 18:29:22 +08:00
|
|
|
optimization_level: OptimizationLevel,
|
|
|
|
) -> Self {
|
2022-11-10 12:16:23 +08:00
|
|
|
let mut _global = GlobalRuntimeState::new(engine);
|
2022-11-28 23:24:22 +08:00
|
|
|
let _lib = lib;
|
2022-11-10 12:16:23 +08:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
{
|
2022-11-28 23:24:22 +08:00
|
|
|
_global.lib = _lib.iter().cloned().collect();
|
2022-11-10 12:16:23 +08:00
|
|
|
}
|
2022-11-10 11:49:10 +08:00
|
|
|
|
2020-04-01 09:51:33 +08:00
|
|
|
Self {
|
|
|
|
changed: false,
|
2021-11-25 17:09:00 +08:00
|
|
|
variables: StaticVec::new_const(),
|
2021-01-12 23:52:50 +08:00
|
|
|
propagate_constants: true,
|
2020-04-01 09:51:33 +08:00
|
|
|
engine,
|
2022-11-10 12:16:23 +08:00
|
|
|
global: _global,
|
2022-05-02 00:03:45 +08:00
|
|
|
caches: Caches::new(),
|
2021-03-11 18:29:22 +08:00
|
|
|
optimization_level,
|
2020-04-01 09:51:33 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 09:36:06 +08:00
|
|
|
/// Set the [`AST`] state to be dirty (i.e. changed).
|
2020-10-08 22:25:50 +08:00
|
|
|
#[inline(always)]
|
2020-03-13 18:12:41 +08:00
|
|
|
pub fn set_dirty(&mut self) {
|
|
|
|
self.changed = true;
|
|
|
|
}
|
2021-03-11 18:29:22 +08:00
|
|
|
/// Set the [`AST`] state to be not dirty (i.e. unchanged).
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn clear_dirty(&mut self) {
|
|
|
|
self.changed = false;
|
|
|
|
}
|
2020-11-25 09:36:06 +08:00
|
|
|
/// Is the [`AST`] dirty (i.e. changed)?
|
2020-10-08 22:25:50 +08:00
|
|
|
#[inline(always)]
|
2021-06-28 18:06:05 +08:00
|
|
|
pub const fn is_dirty(&self) -> bool {
|
2020-03-13 18:12:41 +08:00
|
|
|
self.changed
|
|
|
|
}
|
2020-03-18 10:36:50 +08:00
|
|
|
/// Prune the list of constants back to a specified size.
|
2020-10-08 22:25:50 +08:00
|
|
|
#[inline(always)]
|
2020-12-08 22:20:29 +08:00
|
|
|
pub fn restore_var(&mut self, len: usize) {
|
2022-07-27 18:04:59 +08:00
|
|
|
self.variables.truncate(len);
|
2020-03-13 18:12:41 +08:00
|
|
|
}
|
2022-01-20 08:17:34 +08:00
|
|
|
/// Add a new variable to the list.
|
2020-10-08 22:25:50 +08:00
|
|
|
#[inline(always)]
|
2022-12-20 16:52:55 +08:00
|
|
|
pub fn push_var(
|
|
|
|
&mut self,
|
|
|
|
name: impl Into<Identifier>,
|
|
|
|
access: AccessMode,
|
|
|
|
value: Option<Dynamic>,
|
|
|
|
) {
|
2022-07-27 18:04:59 +08:00
|
|
|
self.variables.push((name.into(), access, value));
|
2020-03-13 18:12:41 +08:00
|
|
|
}
|
2020-03-18 10:36:50 +08:00
|
|
|
/// Look up a constant from the list.
|
2020-10-08 22:25:50 +08:00
|
|
|
#[inline]
|
2022-01-04 15:22:48 +08:00
|
|
|
pub fn find_constant(&self, name: &str) -> Option<&Dynamic> {
|
2021-01-12 23:52:50 +08:00
|
|
|
if !self.propagate_constants {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-11-08 22:16:28 +08:00
|
|
|
for (n, access, value) in self.variables.iter().rev() {
|
2020-03-13 18:12:41 +08:00
|
|
|
if n == name {
|
2021-11-08 22:16:28 +08:00
|
|
|
return match access {
|
2021-03-05 13:34:58 +08:00
|
|
|
AccessMode::ReadWrite => None,
|
2022-12-20 16:52:55 +08:00
|
|
|
AccessMode::ReadOnly => value.as_ref(),
|
2021-11-08 22:16:28 +08:00
|
|
|
};
|
2020-03-13 18:12:41 +08:00
|
|
|
}
|
2021-11-08 22:16:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
2020-03-13 18:12:41 +08:00
|
|
|
}
|
2021-07-14 13:57:58 +08:00
|
|
|
/// Call a registered function
|
2021-10-21 17:26:43 +08:00
|
|
|
#[inline]
|
2021-07-14 13:57:58 +08:00
|
|
|
pub fn call_fn_with_constant_arguments(
|
2022-05-02 00:03:45 +08:00
|
|
|
&mut self,
|
2022-01-04 15:22:48 +08:00
|
|
|
fn_name: &str,
|
2022-11-25 20:42:16 +08:00
|
|
|
op_token: Token,
|
2021-07-14 13:57:58 +08:00
|
|
|
arg_values: &mut [Dynamic],
|
2022-12-20 16:52:55 +08:00
|
|
|
) -> Option<Dynamic> {
|
2021-07-14 13:57:58 +08:00
|
|
|
self.engine
|
2022-10-30 18:43:18 +08:00
|
|
|
.exec_native_fn_call(
|
2022-05-02 00:03:45 +08:00
|
|
|
&mut self.global,
|
|
|
|
&mut self.caches,
|
2022-01-04 15:22:48 +08:00
|
|
|
fn_name,
|
2022-10-30 23:33:44 +08:00
|
|
|
op_token,
|
2022-09-21 11:46:23 +08:00
|
|
|
calc_fn_hash(None, fn_name, arg_values.len()),
|
2021-07-14 13:57:58 +08:00
|
|
|
&mut arg_values.iter_mut().collect::<StaticVec<_>>(),
|
|
|
|
false,
|
|
|
|
Position::NONE,
|
|
|
|
)
|
2022-12-20 16:52:55 +08:00
|
|
|
.ok()
|
|
|
|
.map(|(v, ..)| v)
|
2021-07-14 13:57:58 +08:00
|
|
|
}
|
2020-04-09 18:45:49 +08:00
|
|
|
}
|
|
|
|
|
2020-12-26 23:21:16 +08:00
|
|
|
/// Optimize a block of [statements][Stmt].
|
2020-11-12 12:37:42 +08:00
|
|
|
fn optimize_stmt_block(
|
2022-02-16 12:57:26 +08:00
|
|
|
mut statements: StmtBlockContainer,
|
2021-07-04 16:40:15 +08:00
|
|
|
state: &mut OptimizerState,
|
2020-11-12 12:37:42 +08:00
|
|
|
preserve_result: bool,
|
2021-03-13 23:43:05 +08:00
|
|
|
is_internal: bool,
|
|
|
|
reduce_return: bool,
|
2022-02-16 12:57:26 +08:00
|
|
|
) -> StmtBlockContainer {
|
2021-03-10 12:27:10 +08:00
|
|
|
if statements.is_empty() {
|
|
|
|
return statements;
|
|
|
|
}
|
|
|
|
|
2021-03-11 18:29:22 +08:00
|
|
|
let mut is_dirty = state.is_dirty();
|
2021-01-12 23:52:50 +08:00
|
|
|
|
2021-03-13 23:43:05 +08:00
|
|
|
let is_pure = if is_internal {
|
|
|
|
Stmt::is_internally_pure
|
|
|
|
} else {
|
|
|
|
Stmt::is_pure
|
|
|
|
};
|
|
|
|
|
2021-12-30 12:14:54 +08:00
|
|
|
// Flatten blocks
|
2022-07-27 16:04:24 +08:00
|
|
|
while let Some(n) = statements.iter().position(
|
|
|
|
|s| matches!(s, Stmt::Block(block, ..) if !block.iter().any(Stmt::is_block_dependent)),
|
|
|
|
) {
|
|
|
|
let (first, second) = statements.split_at_mut(n);
|
|
|
|
let stmt = mem::take(&mut second[0]);
|
|
|
|
let mut stmts = match stmt {
|
|
|
|
Stmt::Block(block, ..) => block,
|
|
|
|
stmt => unreachable!("Stmt::Block expected but gets {:?}", stmt),
|
|
|
|
};
|
|
|
|
statements = first
|
|
|
|
.iter_mut()
|
|
|
|
.map(mem::take)
|
|
|
|
.chain(stmts.iter_mut().map(mem::take))
|
|
|
|
.chain(second.iter_mut().skip(1).map(mem::take))
|
|
|
|
.collect();
|
2021-12-30 12:14:54 +08:00
|
|
|
|
|
|
|
is_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimize
|
2021-03-11 18:29:22 +08:00
|
|
|
loop {
|
|
|
|
state.clear_dirty();
|
2020-11-12 12:37:42 +08:00
|
|
|
|
2021-03-11 18:29:22 +08:00
|
|
|
let orig_constants_len = state.variables.len(); // Original number of constants in the state, for restore later
|
|
|
|
let orig_propagate_constants = state.propagate_constants;
|
2020-11-12 12:37:42 +08:00
|
|
|
|
2021-03-11 18:29:22 +08:00
|
|
|
// Remove everything following control flow breaking statements
|
|
|
|
let mut dead_code = false;
|
2020-11-12 12:37:42 +08:00
|
|
|
|
2021-03-11 18:29:22 +08:00
|
|
|
statements.retain(|stmt| {
|
|
|
|
if dead_code {
|
|
|
|
state.set_dirty();
|
|
|
|
false
|
|
|
|
} else if stmt.is_control_flow_break() {
|
|
|
|
dead_code = true;
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
});
|
2020-11-12 12:37:42 +08:00
|
|
|
|
2021-03-11 18:29:22 +08:00
|
|
|
// Optimize each statement in the block
|
2022-09-26 23:35:37 +08:00
|
|
|
for stmt in &mut statements {
|
2021-03-11 18:29:22 +08:00
|
|
|
match stmt {
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Var(x, options, ..) => {
|
2022-02-25 11:42:59 +08:00
|
|
|
if options.contains(ASTFlags::CONSTANT) {
|
2021-08-03 22:19:25 +08:00
|
|
|
// Add constant literals into the state
|
2022-02-16 17:51:14 +08:00
|
|
|
optimize_expr(&mut x.1, state, false);
|
2021-08-03 22:19:25 +08:00
|
|
|
|
2022-02-16 17:51:14 +08:00
|
|
|
if x.1.is_constant() {
|
2021-08-03 22:19:25 +08:00
|
|
|
state.push_var(
|
2022-03-05 17:57:23 +08:00
|
|
|
x.0.as_str(),
|
2021-08-03 22:19:25 +08:00
|
|
|
AccessMode::ReadOnly,
|
2022-12-20 16:52:55 +08:00
|
|
|
x.1.get_literal_value(),
|
2021-08-03 22:19:25 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Add variables into the state
|
2022-02-16 17:51:14 +08:00
|
|
|
optimize_expr(&mut x.1, state, false);
|
2022-12-20 16:52:55 +08:00
|
|
|
state.push_var(x.0.as_str(), AccessMode::ReadWrite, None);
|
2021-03-11 18:29:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Optimize the statement
|
|
|
|
_ => optimize_stmt(stmt, state, preserve_result),
|
2020-11-12 12:37:42 +08:00
|
|
|
}
|
2022-01-28 18:59:18 +08:00
|
|
|
}
|
2020-11-12 12:37:42 +08:00
|
|
|
|
2021-03-13 23:43:05 +08:00
|
|
|
// Remove all pure statements except the last one
|
|
|
|
let mut index = 0;
|
|
|
|
let mut first_non_constant = statements
|
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.enumerate()
|
|
|
|
.find_map(|(i, stmt)| match stmt {
|
|
|
|
stmt if !is_pure(stmt) => Some(i),
|
|
|
|
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Var(x, ..) if x.1.is_constant() => Some(i),
|
|
|
|
Stmt::Expr(e) if !e.is_constant() => Some(i),
|
2021-03-13 23:43:05 +08:00
|
|
|
|
2021-03-14 10:47:21 +08:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Import(x, ..) if !x.0.is_constant() => Some(i),
|
2021-03-14 10:47:21 +08:00
|
|
|
|
2021-04-24 13:42:45 +08:00
|
|
|
_ => None,
|
2021-03-13 23:43:05 +08:00
|
|
|
})
|
2021-04-24 13:42:45 +08:00
|
|
|
.map_or(0, |n| statements.len() - n - 1);
|
2021-03-13 23:43:05 +08:00
|
|
|
|
|
|
|
while index < statements.len() {
|
|
|
|
if preserve_result && index >= statements.len() - 1 {
|
|
|
|
break;
|
2022-08-27 16:26:41 +08:00
|
|
|
}
|
|
|
|
match statements[index] {
|
|
|
|
ref stmt if is_pure(stmt) && index >= first_non_constant => {
|
|
|
|
state.set_dirty();
|
|
|
|
statements.remove(index);
|
|
|
|
}
|
|
|
|
ref stmt if stmt.is_pure() => {
|
|
|
|
state.set_dirty();
|
|
|
|
if index < first_non_constant {
|
|
|
|
first_non_constant -= 1;
|
2021-03-13 23:43:05 +08:00
|
|
|
}
|
2022-08-27 16:26:41 +08:00
|
|
|
statements.remove(index);
|
2021-03-13 23:43:05 +08:00
|
|
|
}
|
2022-08-27 16:26:41 +08:00
|
|
|
_ => index += 1,
|
2021-03-13 23:43:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 18:29:22 +08:00
|
|
|
// Remove all pure statements that do not return values at the end of a block.
|
|
|
|
// We cannot remove anything for non-pure statements due to potential side-effects.
|
|
|
|
if preserve_result {
|
|
|
|
loop {
|
2021-06-14 12:02:22 +08:00
|
|
|
match statements[..] {
|
2021-03-13 23:43:05 +08:00
|
|
|
// { return; } -> {}
|
2022-02-16 17:51:14 +08:00
|
|
|
[Stmt::Return(None, options, ..)]
|
2022-02-25 11:42:59 +08:00
|
|
|
if reduce_return && !options.contains(ASTFlags::BREAK) =>
|
2021-09-10 20:23:59 +08:00
|
|
|
{
|
2021-03-13 23:43:05 +08:00
|
|
|
state.set_dirty();
|
|
|
|
statements.clear();
|
|
|
|
}
|
2021-06-14 12:02:22 +08:00
|
|
|
[ref stmt] if !stmt.returns_value() && is_pure(stmt) => {
|
2021-03-11 18:29:22 +08:00
|
|
|
state.set_dirty();
|
|
|
|
statements.clear();
|
|
|
|
}
|
2021-03-13 23:43:05 +08:00
|
|
|
// { ...; return; } -> { ... }
|
2022-02-16 17:51:14 +08:00
|
|
|
[.., ref last_stmt, Stmt::Return(None, options, ..)]
|
2021-09-10 20:23:59 +08:00
|
|
|
if reduce_return
|
2022-02-25 11:42:59 +08:00
|
|
|
&& !options.contains(ASTFlags::BREAK)
|
2021-09-10 20:23:59 +08:00
|
|
|
&& !last_stmt.returns_value() =>
|
2021-03-13 23:43:05 +08:00
|
|
|
{
|
|
|
|
state.set_dirty();
|
2022-01-06 11:07:52 +08:00
|
|
|
statements.pop().unwrap();
|
2021-03-13 23:43:05 +08:00
|
|
|
}
|
|
|
|
// { ...; return val; } -> { ...; val }
|
2022-02-16 17:51:14 +08:00
|
|
|
[.., Stmt::Return(ref mut expr, options, pos)]
|
2022-02-25 11:42:59 +08:00
|
|
|
if reduce_return && !options.contains(ASTFlags::BREAK) =>
|
2021-03-13 23:43:05 +08:00
|
|
|
{
|
|
|
|
state.set_dirty();
|
2022-01-06 11:07:52 +08:00
|
|
|
*statements.last_mut().unwrap() = expr
|
2021-12-12 12:33:22 +08:00
|
|
|
.as_mut()
|
|
|
|
.map_or_else(|| Stmt::Noop(pos), |e| Stmt::Expr(mem::take(e)));
|
2021-03-13 23:43:05 +08:00
|
|
|
}
|
2021-07-01 12:27:29 +08:00
|
|
|
// { ...; stmt; noop } -> done
|
2022-02-08 09:46:14 +08:00
|
|
|
[.., ref second_last_stmt, Stmt::Noop(..)]
|
2021-07-01 12:27:29 +08:00
|
|
|
if second_last_stmt.returns_value() =>
|
|
|
|
{
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// { ...; stmt_that_returns; pure_non_value_stmt } -> { ...; stmt_that_returns; noop }
|
|
|
|
// { ...; stmt; pure_non_value_stmt } -> { ...; stmt }
|
2021-06-14 12:02:22 +08:00
|
|
|
[.., ref second_last_stmt, ref last_stmt]
|
2021-03-13 23:43:05 +08:00
|
|
|
if !last_stmt.returns_value() && is_pure(last_stmt) =>
|
2021-03-11 18:29:22 +08:00
|
|
|
{
|
|
|
|
state.set_dirty();
|
|
|
|
if second_last_stmt.returns_value() {
|
2022-01-06 11:07:52 +08:00
|
|
|
*statements.last_mut().unwrap() = Stmt::Noop(last_stmt.position());
|
2021-03-11 18:29:22 +08:00
|
|
|
} else {
|
2022-01-06 11:07:52 +08:00
|
|
|
statements.pop().unwrap();
|
2021-03-11 18:29:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
loop {
|
2021-06-14 12:02:22 +08:00
|
|
|
match statements[..] {
|
|
|
|
[ref stmt] if is_pure(stmt) => {
|
2021-03-11 18:29:22 +08:00
|
|
|
state.set_dirty();
|
|
|
|
statements.clear();
|
|
|
|
}
|
2021-03-13 23:43:05 +08:00
|
|
|
// { ...; return; } -> { ... }
|
2022-02-16 17:51:14 +08:00
|
|
|
[.., Stmt::Return(None, options, ..)]
|
2022-02-25 11:42:59 +08:00
|
|
|
if reduce_return && !options.contains(ASTFlags::BREAK) =>
|
2021-03-13 23:43:05 +08:00
|
|
|
{
|
|
|
|
state.set_dirty();
|
2022-01-06 11:07:52 +08:00
|
|
|
statements.pop().unwrap();
|
2021-03-13 23:43:05 +08:00
|
|
|
}
|
|
|
|
// { ...; return pure_val; } -> { ... }
|
2022-02-16 17:51:14 +08:00
|
|
|
[.., Stmt::Return(Some(ref expr), options, ..)]
|
2021-09-10 20:23:59 +08:00
|
|
|
if reduce_return
|
2022-02-25 11:42:59 +08:00
|
|
|
&& !options.contains(ASTFlags::BREAK)
|
2021-09-10 20:23:59 +08:00
|
|
|
&& expr.is_pure() =>
|
2021-03-13 23:43:05 +08:00
|
|
|
{
|
|
|
|
state.set_dirty();
|
2022-01-06 11:07:52 +08:00
|
|
|
statements.pop().unwrap();
|
2021-03-13 23:43:05 +08:00
|
|
|
}
|
2021-06-14 12:02:22 +08:00
|
|
|
[.., ref last_stmt] if is_pure(last_stmt) => {
|
2021-03-11 18:29:22 +08:00
|
|
|
state.set_dirty();
|
2022-01-06 11:07:52 +08:00
|
|
|
statements.pop().unwrap();
|
2021-03-11 18:29:22 +08:00
|
|
|
}
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
2020-11-12 12:37:42 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 18:29:22 +08:00
|
|
|
// Pop the stack and remove all the local constants
|
|
|
|
state.restore_var(orig_constants_len);
|
|
|
|
state.propagate_constants = orig_propagate_constants;
|
2020-11-12 12:37:42 +08:00
|
|
|
|
2021-03-11 18:29:22 +08:00
|
|
|
if !state.is_dirty() {
|
|
|
|
break;
|
2020-11-12 12:37:42 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 18:29:22 +08:00
|
|
|
is_dirty = true;
|
|
|
|
}
|
2020-11-12 12:37:42 +08:00
|
|
|
|
2021-03-11 18:29:22 +08:00
|
|
|
if is_dirty {
|
2020-11-12 12:37:42 +08:00
|
|
|
state.set_dirty();
|
|
|
|
}
|
|
|
|
|
2021-03-13 23:43:05 +08:00
|
|
|
statements.shrink_to_fit();
|
2021-03-10 12:27:10 +08:00
|
|
|
statements
|
2020-11-12 12:37:42 +08:00
|
|
|
}
|
|
|
|
|
2020-12-26 23:21:16 +08:00
|
|
|
/// Optimize a [statement][Stmt].
|
2021-07-04 16:40:15 +08:00
|
|
|
fn optimize_stmt(stmt: &mut Stmt, state: &mut OptimizerState, preserve_result: bool) {
|
2020-03-09 21:57:07 +08:00
|
|
|
match stmt {
|
2021-04-23 23:37:10 +08:00
|
|
|
// var = var op expr => var op= expr
|
2022-02-08 09:02:15 +08:00
|
|
|
Stmt::Assignment(x, ..)
|
2022-04-18 23:12:47 +08:00
|
|
|
if !x.0.is_op_assignment()
|
2022-01-28 10:11:40 +08:00
|
|
|
&& x.1.lhs.is_variable_access(true)
|
2022-02-08 09:02:15 +08:00
|
|
|
&& matches!(&x.1.rhs, Expr::FnCall(x2, ..)
|
2022-10-30 22:16:09 +08:00
|
|
|
if Token::lookup_symbol_from_syntax(&x2.name).map_or(false, |t| t.has_op_assignment())
|
2021-06-08 14:46:49 +08:00
|
|
|
&& x2.args.len() == 2
|
2022-01-28 10:11:40 +08:00
|
|
|
&& x2.args[0].get_variable_name(true) == x.1.lhs.get_variable_name(true)
|
2021-04-23 23:37:10 +08:00
|
|
|
) =>
|
|
|
|
{
|
2022-01-28 10:11:40 +08:00
|
|
|
match x.1.rhs {
|
2022-10-30 18:43:18 +08:00
|
|
|
Expr::FnCall(ref mut x2, pos) => {
|
2021-04-23 23:37:10 +08:00
|
|
|
state.set_dirty();
|
2022-10-30 18:43:18 +08:00
|
|
|
x.0 = OpAssignment::new_op_assignment_from_base(&x2.name, pos);
|
2022-03-05 12:06:47 +08:00
|
|
|
x.1.rhs = mem::take(&mut x2.args[1]);
|
2021-04-23 23:37:10 +08:00
|
|
|
}
|
2021-12-30 12:14:54 +08:00
|
|
|
ref expr => unreachable!("Expr::FnCall expected but gets {:?}", expr),
|
2021-04-23 23:37:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 13:08:19 +08:00
|
|
|
// expr op= expr
|
2022-02-08 09:02:15 +08:00
|
|
|
Stmt::Assignment(x, ..) => {
|
2022-01-28 10:11:40 +08:00
|
|
|
if !x.1.lhs.is_variable_access(false) {
|
|
|
|
optimize_expr(&mut x.1.lhs, state, false);
|
2020-11-03 13:08:19 +08:00
|
|
|
}
|
2022-01-28 10:11:40 +08:00
|
|
|
optimize_expr(&mut x.1.rhs, state, false);
|
|
|
|
}
|
2020-11-14 23:43:36 +08:00
|
|
|
|
2021-03-10 12:27:10 +08:00
|
|
|
// if expr {}
|
2022-12-23 14:26:06 +08:00
|
|
|
Stmt::If(x, ..) if x.body.is_empty() && x.branch.is_empty() => {
|
|
|
|
let condition = &mut x.expr;
|
2020-03-13 18:12:41 +08:00
|
|
|
state.set_dirty();
|
2020-03-12 23:46:52 +08:00
|
|
|
|
2022-02-04 12:04:33 +08:00
|
|
|
let pos = condition.start_position();
|
2020-11-12 12:37:42 +08:00
|
|
|
let mut expr = mem::take(condition);
|
2021-06-02 14:29:18 +08:00
|
|
|
optimize_expr(&mut expr, state, false);
|
2020-03-12 23:46:52 +08:00
|
|
|
|
2020-11-12 12:37:42 +08:00
|
|
|
*stmt = if preserve_result {
|
2020-03-18 10:36:50 +08:00
|
|
|
// -> { expr, Noop }
|
2022-02-16 17:51:14 +08:00
|
|
|
(
|
|
|
|
[Stmt::Expr(expr.into()), Stmt::Noop(pos)],
|
|
|
|
pos,
|
|
|
|
Position::NONE,
|
2022-02-04 12:04:33 +08:00
|
|
|
)
|
2022-02-16 17:51:14 +08:00
|
|
|
.into()
|
2020-03-14 23:41:15 +08:00
|
|
|
} else {
|
2020-03-18 10:36:50 +08:00
|
|
|
// -> expr
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Expr(expr.into())
|
2020-11-12 12:37:42 +08:00
|
|
|
};
|
2020-03-12 23:46:52 +08:00
|
|
|
}
|
2021-03-10 12:27:10 +08:00
|
|
|
// if false { if_block } -> Noop
|
2022-12-23 14:26:06 +08:00
|
|
|
Stmt::If(x, ..)
|
|
|
|
if matches!(x.expr, Expr::BoolConstant(false, ..)) && x.branch.is_empty() =>
|
|
|
|
{
|
|
|
|
if let Expr::BoolConstant(false, pos) = x.expr {
|
2022-02-16 17:51:14 +08:00
|
|
|
state.set_dirty();
|
|
|
|
*stmt = Stmt::Noop(pos);
|
|
|
|
} else {
|
|
|
|
unreachable!("`Expr::BoolConstant`");
|
|
|
|
}
|
2020-11-12 12:37:42 +08:00
|
|
|
}
|
2020-10-27 18:18:19 +08:00
|
|
|
// if false { if_block } else { else_block } -> else_block
|
2022-12-23 14:26:06 +08:00
|
|
|
Stmt::If(x, ..) if matches!(x.expr, Expr::BoolConstant(false, ..)) => {
|
2021-03-10 12:27:10 +08:00
|
|
|
state.set_dirty();
|
2022-12-23 14:26:06 +08:00
|
|
|
let body = mem::take(&mut *x.branch);
|
|
|
|
*stmt = match optimize_stmt_block(body, state, preserve_result, true, false) {
|
|
|
|
statements if statements.is_empty() => Stmt::Noop(x.branch.position()),
|
|
|
|
statements => (statements, x.branch.span()).into(),
|
|
|
|
}
|
2020-10-27 18:18:19 +08:00
|
|
|
}
|
|
|
|
// if true { if_block } else { else_block } -> if_block
|
2022-12-23 14:26:06 +08:00
|
|
|
Stmt::If(x, ..) if matches!(x.expr, Expr::BoolConstant(true, ..)) => {
|
2021-03-10 12:27:10 +08:00
|
|
|
state.set_dirty();
|
2022-12-23 14:26:06 +08:00
|
|
|
let body = mem::take(&mut *x.body);
|
|
|
|
*stmt = match optimize_stmt_block(body, state, preserve_result, true, false) {
|
|
|
|
statements if statements.is_empty() => Stmt::Noop(x.body.position()),
|
|
|
|
statements => (statements, x.body.span()).into(),
|
|
|
|
}
|
2020-11-12 12:37:42 +08:00
|
|
|
}
|
2020-03-18 10:36:50 +08:00
|
|
|
// if expr { if_block } else { else_block }
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::If(x, ..) => {
|
2022-12-23 14:26:06 +08:00
|
|
|
let FlowControl { expr, body, branch } = &mut **x;
|
|
|
|
optimize_expr(expr, state, false);
|
|
|
|
let statements = mem::take(&mut **body);
|
|
|
|
**body = optimize_stmt_block(statements, state, preserve_result, true, false);
|
|
|
|
let statements = mem::take(&mut **branch);
|
|
|
|
**branch = optimize_stmt_block(statements, state, preserve_result, true, false);
|
2020-11-12 12:37:42 +08:00
|
|
|
}
|
2020-10-27 18:18:19 +08:00
|
|
|
|
2020-11-14 23:43:36 +08:00
|
|
|
// switch const { ... }
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Switch(x, pos) if x.0.is_constant() => {
|
|
|
|
let (
|
|
|
|
match_expr,
|
2022-07-18 13:40:41 +08:00
|
|
|
SwitchCasesCollection {
|
2022-07-19 13:33:53 +08:00
|
|
|
expressions,
|
2022-02-16 17:51:14 +08:00
|
|
|
cases,
|
|
|
|
ranges,
|
|
|
|
def_case,
|
|
|
|
},
|
2022-07-05 16:26:38 +08:00
|
|
|
) = &mut **x;
|
2022-02-16 17:51:14 +08:00
|
|
|
|
2022-01-06 11:07:52 +08:00
|
|
|
let value = match_expr.get_literal_value().unwrap();
|
2020-11-14 23:43:36 +08:00
|
|
|
let hasher = &mut get_hasher();
|
|
|
|
value.hash(hasher);
|
|
|
|
let hash = hasher.finish();
|
|
|
|
|
2021-12-15 12:06:17 +08:00
|
|
|
// First check hashes
|
2022-07-18 13:40:41 +08:00
|
|
|
if let Some(case_blocks_list) = cases.get(&hash) {
|
|
|
|
match &case_blocks_list[..] {
|
|
|
|
[] => (),
|
|
|
|
[index] => {
|
2022-07-19 13:33:53 +08:00
|
|
|
let mut b = mem::take(&mut expressions[*index]);
|
2022-07-18 13:40:41 +08:00
|
|
|
cases.clear();
|
2022-07-04 17:42:24 +08:00
|
|
|
|
2022-07-18 23:28:12 +08:00
|
|
|
if b.is_always_true() {
|
|
|
|
// Promote the matched case
|
2022-07-19 13:33:53 +08:00
|
|
|
let mut statements = Stmt::Expr(mem::take(&mut b.expr).into());
|
|
|
|
optimize_stmt(&mut statements, state, true);
|
|
|
|
*stmt = statements;
|
2022-07-18 23:28:12 +08:00
|
|
|
} else {
|
|
|
|
// switch const { case if condition => stmt, _ => def } => if condition { stmt } else { def }
|
|
|
|
optimize_expr(&mut b.condition, state, false);
|
|
|
|
|
2022-12-23 14:26:06 +08:00
|
|
|
let branch = match def_case {
|
2022-10-10 16:46:35 +08:00
|
|
|
Some(index) => {
|
|
|
|
let mut def_stmt =
|
|
|
|
Stmt::Expr(mem::take(&mut expressions[*index].expr).into());
|
|
|
|
optimize_stmt(&mut def_stmt, state, true);
|
|
|
|
def_stmt.into()
|
|
|
|
}
|
|
|
|
_ => StmtBlock::NONE,
|
2022-07-18 23:28:12 +08:00
|
|
|
};
|
2022-12-23 14:26:06 +08:00
|
|
|
let body = Stmt::Expr(mem::take(&mut b.expr).into()).into();
|
|
|
|
let expr = mem::take(&mut b.condition);
|
2022-07-18 23:28:12 +08:00
|
|
|
|
|
|
|
*stmt = Stmt::If(
|
2022-12-23 14:26:06 +08:00
|
|
|
FlowControl { expr, body, branch }.into(),
|
2022-07-18 23:28:12 +08:00
|
|
|
match_expr.start_position(),
|
|
|
|
);
|
2022-07-18 13:40:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
state.set_dirty();
|
|
|
|
return;
|
2022-04-19 16:20:43 +08:00
|
|
|
}
|
2022-07-18 13:40:41 +08:00
|
|
|
_ => {
|
|
|
|
for &index in case_blocks_list {
|
2022-07-19 13:33:53 +08:00
|
|
|
let mut b = mem::take(&mut expressions[index]);
|
2022-07-18 13:40:41 +08:00
|
|
|
|
2022-07-18 23:28:12 +08:00
|
|
|
if b.is_always_true() {
|
|
|
|
// Promote the matched case
|
2022-07-19 13:33:53 +08:00
|
|
|
let mut statements = Stmt::Expr(mem::take(&mut b.expr).into());
|
|
|
|
optimize_stmt(&mut statements, state, true);
|
|
|
|
*stmt = statements;
|
2022-07-18 23:28:12 +08:00
|
|
|
state.set_dirty();
|
|
|
|
return;
|
2022-07-18 13:40:41 +08:00
|
|
|
}
|
|
|
|
}
|
2022-04-19 16:20:43 +08:00
|
|
|
}
|
2021-04-16 12:04:33 +08:00
|
|
|
}
|
2021-12-15 12:06:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then check ranges
|
2022-11-09 12:44:57 +08:00
|
|
|
if value.is_int() && !ranges.is_empty() {
|
2022-06-26 18:09:15 +08:00
|
|
|
let value = value.as_int().unwrap();
|
2021-12-15 12:06:17 +08:00
|
|
|
|
|
|
|
// Only one range or all ranges without conditions
|
2022-04-19 16:20:43 +08:00
|
|
|
if ranges.len() == 1
|
2022-07-18 23:28:12 +08:00
|
|
|
|| ranges
|
|
|
|
.iter()
|
2022-07-19 13:33:53 +08:00
|
|
|
.all(|r| expressions[r.index()].is_always_true())
|
2022-04-19 16:20:43 +08:00
|
|
|
{
|
2022-07-27 16:04:24 +08:00
|
|
|
if let Some(r) = ranges.iter().find(|r| r.contains(value)) {
|
2022-07-19 13:33:53 +08:00
|
|
|
let range_block = &mut expressions[r.index()];
|
2022-07-18 23:28:12 +08:00
|
|
|
|
|
|
|
if range_block.is_always_true() {
|
|
|
|
// Promote the matched case
|
2022-07-19 13:33:53 +08:00
|
|
|
let block = &mut expressions[r.index()];
|
|
|
|
let mut statements = Stmt::Expr(mem::take(&mut block.expr).into());
|
|
|
|
optimize_stmt(&mut statements, state, true);
|
|
|
|
*stmt = statements;
|
2022-07-18 23:28:12 +08:00
|
|
|
} else {
|
2022-12-23 14:26:06 +08:00
|
|
|
let mut expr = mem::take(&mut range_block.condition);
|
2022-07-18 23:28:12 +08:00
|
|
|
|
|
|
|
// switch const { range if condition => stmt, _ => def } => if condition { stmt } else { def }
|
2022-12-23 14:26:06 +08:00
|
|
|
optimize_expr(&mut expr, state, false);
|
2022-07-18 23:28:12 +08:00
|
|
|
|
2022-12-23 14:26:06 +08:00
|
|
|
let branch = match def_case {
|
2022-10-10 16:46:35 +08:00
|
|
|
Some(index) => {
|
|
|
|
let mut def_stmt =
|
|
|
|
Stmt::Expr(mem::take(&mut expressions[*index].expr).into());
|
|
|
|
optimize_stmt(&mut def_stmt, state, true);
|
|
|
|
def_stmt.into()
|
|
|
|
}
|
|
|
|
_ => StmtBlock::NONE,
|
2022-07-18 23:28:12 +08:00
|
|
|
};
|
|
|
|
|
2022-12-23 14:26:06 +08:00
|
|
|
let body =
|
2022-07-19 13:33:53 +08:00
|
|
|
Stmt::Expr(mem::take(&mut expressions[r.index()].expr).into())
|
|
|
|
.into();
|
2022-07-18 23:28:12 +08:00
|
|
|
|
|
|
|
*stmt = Stmt::If(
|
2022-12-23 14:26:06 +08:00
|
|
|
FlowControl { expr, body, branch }.into(),
|
2022-07-18 23:28:12 +08:00
|
|
|
match_expr.start_position(),
|
|
|
|
);
|
2021-12-15 12:06:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
state.set_dirty();
|
|
|
|
return;
|
|
|
|
}
|
2021-04-16 13:15:11 +08:00
|
|
|
} else {
|
2021-12-15 12:06:17 +08:00
|
|
|
// Multiple ranges - clear the table and just keep the right ranges
|
2022-01-28 10:11:40 +08:00
|
|
|
if !cases.is_empty() {
|
2021-12-15 12:06:17 +08:00
|
|
|
state.set_dirty();
|
2022-01-28 10:11:40 +08:00
|
|
|
cases.clear();
|
2021-12-15 12:06:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let old_ranges_len = ranges.len();
|
|
|
|
|
2022-07-04 17:42:24 +08:00
|
|
|
ranges.retain(|r| r.contains(value));
|
2021-12-15 12:06:17 +08:00
|
|
|
|
|
|
|
if ranges.len() != old_ranges_len {
|
|
|
|
state.set_dirty();
|
|
|
|
}
|
|
|
|
|
2022-07-05 16:26:38 +08:00
|
|
|
for r in &*ranges {
|
2022-07-19 13:33:53 +08:00
|
|
|
let b = &mut expressions[r.index()];
|
2022-07-18 08:54:10 +08:00
|
|
|
optimize_expr(&mut b.condition, state, false);
|
2022-07-19 13:33:53 +08:00
|
|
|
optimize_expr(&mut b.expr, state, false);
|
2021-12-15 12:06:17 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2021-04-16 12:04:33 +08:00
|
|
|
}
|
2021-12-15 12:06:17 +08:00
|
|
|
|
|
|
|
// Promote the default case
|
|
|
|
state.set_dirty();
|
2022-07-18 22:30:09 +08:00
|
|
|
|
2022-10-10 16:46:35 +08:00
|
|
|
match def_case {
|
|
|
|
Some(index) => {
|
|
|
|
let mut def_stmt = Stmt::Expr(mem::take(&mut expressions[*index].expr).into());
|
|
|
|
optimize_stmt(&mut def_stmt, state, true);
|
|
|
|
*stmt = def_stmt;
|
|
|
|
}
|
|
|
|
_ => *stmt = StmtBlock::empty(*pos).into(),
|
2022-07-18 22:30:09 +08:00
|
|
|
}
|
2020-11-14 23:43:36 +08:00
|
|
|
}
|
|
|
|
// switch
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Switch(x, ..) => {
|
|
|
|
let (
|
|
|
|
match_expr,
|
2022-07-18 13:40:41 +08:00
|
|
|
SwitchCasesCollection {
|
2022-07-19 13:33:53 +08:00
|
|
|
expressions,
|
2022-02-16 17:51:14 +08:00
|
|
|
cases,
|
|
|
|
ranges,
|
|
|
|
def_case,
|
|
|
|
..
|
|
|
|
},
|
2022-07-05 16:26:38 +08:00
|
|
|
) = &mut **x;
|
2022-02-16 17:51:14 +08:00
|
|
|
|
2021-06-02 14:29:18 +08:00
|
|
|
optimize_expr(match_expr, state, false);
|
2022-02-16 17:51:14 +08:00
|
|
|
|
2022-07-04 17:42:24 +08:00
|
|
|
// Optimize blocks
|
2022-09-26 23:35:37 +08:00
|
|
|
for b in expressions.as_mut() {
|
2022-07-18 08:54:10 +08:00
|
|
|
optimize_expr(&mut b.condition, state, false);
|
2022-07-19 13:33:53 +08:00
|
|
|
optimize_expr(&mut b.expr, state, false);
|
2022-04-19 16:20:43 +08:00
|
|
|
|
2022-07-27 16:04:24 +08:00
|
|
|
if b.is_always_false() && !b.expr.is_unit() {
|
|
|
|
b.expr = Expr::Unit(b.expr.position());
|
|
|
|
state.set_dirty();
|
2021-04-16 12:04:33 +08:00
|
|
|
}
|
2022-01-28 18:59:18 +08:00
|
|
|
}
|
2021-04-16 12:04:33 +08:00
|
|
|
|
|
|
|
// Remove false cases
|
2022-07-18 13:40:41 +08:00
|
|
|
cases.retain(|_, list| {
|
|
|
|
// Remove all entries that have false conditions
|
2022-07-18 23:28:12 +08:00
|
|
|
list.retain(|index| {
|
2022-07-19 13:33:53 +08:00
|
|
|
if expressions[*index].is_always_false() {
|
2022-07-18 22:30:09 +08:00
|
|
|
state.set_dirty();
|
|
|
|
false
|
2022-07-18 23:28:12 +08:00
|
|
|
} else {
|
|
|
|
true
|
2022-07-18 22:30:09 +08:00
|
|
|
}
|
2022-07-18 13:40:41 +08:00
|
|
|
});
|
|
|
|
// Remove all entries after a `true` condition
|
|
|
|
if let Some(n) = list
|
|
|
|
.iter()
|
2022-07-20 21:06:36 +08:00
|
|
|
.position(|&index| expressions[index].is_always_true())
|
2022-07-18 13:40:41 +08:00
|
|
|
{
|
2022-07-18 22:30:09 +08:00
|
|
|
if n + 1 < list.len() {
|
|
|
|
state.set_dirty();
|
|
|
|
list.truncate(n + 1);
|
|
|
|
}
|
2022-02-16 17:51:14 +08:00
|
|
|
}
|
2022-07-18 13:40:41 +08:00
|
|
|
// Remove if no entry left
|
2022-07-27 18:04:59 +08:00
|
|
|
if list.is_empty() {
|
|
|
|
state.set_dirty();
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
2022-07-18 22:30:09 +08:00
|
|
|
}
|
2022-02-16 17:51:14 +08:00
|
|
|
});
|
2022-07-18 22:30:09 +08:00
|
|
|
|
2022-02-16 17:51:14 +08:00
|
|
|
// Remove false ranges
|
2022-07-18 23:28:12 +08:00
|
|
|
ranges.retain(|r| {
|
2022-07-19 13:33:53 +08:00
|
|
|
if expressions[r.index()].is_always_false() {
|
2022-02-16 17:51:14 +08:00
|
|
|
state.set_dirty();
|
|
|
|
false
|
2022-07-18 23:28:12 +08:00
|
|
|
} else {
|
|
|
|
true
|
2022-02-16 17:51:14 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-07-18 22:30:09 +08:00
|
|
|
if let Some(index) = def_case {
|
2022-07-19 13:33:53 +08:00
|
|
|
optimize_expr(&mut expressions[*index].expr, state, false);
|
2022-07-18 22:30:09 +08:00
|
|
|
}
|
2022-07-18 08:54:10 +08:00
|
|
|
|
|
|
|
// Remove unused block statements
|
2022-07-19 13:33:53 +08:00
|
|
|
for index in 0..expressions.len() {
|
2022-07-18 22:30:09 +08:00
|
|
|
if *def_case == Some(index)
|
2022-07-18 13:40:41 +08:00
|
|
|
|| cases.values().flat_map(|c| c.iter()).any(|&n| n == index)
|
2022-07-18 08:54:10 +08:00
|
|
|
|| ranges.iter().any(|r| r.index() == index)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-07-19 13:33:53 +08:00
|
|
|
let b = &mut expressions[index];
|
2022-07-18 08:54:10 +08:00
|
|
|
|
2022-07-19 13:33:53 +08:00
|
|
|
if !b.expr.is_unit() {
|
|
|
|
b.expr = Expr::Unit(b.expr.position());
|
2022-07-18 08:54:10 +08:00
|
|
|
state.set_dirty();
|
|
|
|
}
|
|
|
|
}
|
2020-11-14 23:43:36 +08:00
|
|
|
}
|
|
|
|
|
2020-10-27 18:18:19 +08:00
|
|
|
// while false { block } -> Noop
|
2022-12-23 14:26:06 +08:00
|
|
|
Stmt::While(x, ..) if matches!(x.expr, Expr::BoolConstant(false, ..)) => match x.expr {
|
2022-02-16 17:51:14 +08:00
|
|
|
Expr::BoolConstant(false, pos) => {
|
|
|
|
state.set_dirty();
|
2022-07-27 18:04:59 +08:00
|
|
|
*stmt = Stmt::Noop(pos);
|
2022-02-16 17:51:14 +08:00
|
|
|
}
|
|
|
|
_ => unreachable!("`Expr::BoolConstant"),
|
|
|
|
},
|
2020-03-18 10:36:50 +08:00
|
|
|
// while expr { block }
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::While(x, ..) => {
|
2022-12-23 14:26:06 +08:00
|
|
|
let FlowControl { expr, body, .. } = &mut **x;
|
|
|
|
optimize_expr(expr, state, false);
|
|
|
|
if let Expr::BoolConstant(true, pos) = expr {
|
|
|
|
*expr = Expr::Unit(*pos);
|
2021-08-04 17:40:26 +08:00
|
|
|
}
|
2022-02-16 17:51:14 +08:00
|
|
|
**body = optimize_stmt_block(mem::take(&mut **body), state, false, true, false);
|
2020-11-20 22:23:37 +08:00
|
|
|
}
|
|
|
|
// do { block } while|until expr
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Do(x, ..) => {
|
2022-12-23 14:26:06 +08:00
|
|
|
optimize_expr(&mut x.expr, state, false);
|
|
|
|
*x.body = optimize_stmt_block(mem::take(&mut *x.body), state, false, true, false);
|
2020-11-12 12:37:42 +08:00
|
|
|
}
|
2020-03-18 10:36:50 +08:00
|
|
|
// for id in expr { block }
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::For(x, ..) => {
|
2022-12-23 14:26:06 +08:00
|
|
|
optimize_expr(&mut x.2.expr, state, false);
|
|
|
|
*x.2.body = optimize_stmt_block(mem::take(&mut *x.2.body), state, false, true, false);
|
2020-10-27 19:23:43 +08:00
|
|
|
}
|
2020-03-18 10:36:50 +08:00
|
|
|
// let id = expr;
|
2022-02-25 11:42:59 +08:00
|
|
|
Stmt::Var(x, options, ..) if !options.contains(ASTFlags::CONSTANT) => {
|
2022-07-27 18:04:59 +08:00
|
|
|
optimize_expr(&mut x.1, state, false);
|
2021-08-03 22:19:25 +08:00
|
|
|
}
|
2020-10-20 23:16:03 +08:00
|
|
|
// import expr as var;
|
2020-07-26 15:53:22 +08:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Import(x, ..) => optimize_expr(&mut x.0, state, false),
|
2020-03-18 10:36:50 +08:00
|
|
|
// { block }
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Block(block) => {
|
|
|
|
let span = block.span();
|
|
|
|
let statements = block.take_statements().into_vec().into();
|
2021-05-05 18:38:52 +08:00
|
|
|
let mut block = optimize_stmt_block(statements, state, preserve_result, true, false);
|
2021-04-05 14:57:07 +08:00
|
|
|
|
|
|
|
match block.as_mut_slice() {
|
|
|
|
[] => {
|
2021-03-10 12:27:10 +08:00
|
|
|
state.set_dirty();
|
2022-02-08 23:01:47 +08:00
|
|
|
*stmt = Stmt::Noop(span.start());
|
2021-03-10 12:27:10 +08:00
|
|
|
}
|
2021-12-30 12:14:54 +08:00
|
|
|
// Only one statement which is not block-dependent - promote
|
|
|
|
[s] if !s.is_block_dependent() => {
|
2021-03-10 12:27:10 +08:00
|
|
|
state.set_dirty();
|
2021-04-05 14:57:07 +08:00
|
|
|
*stmt = mem::take(s);
|
2021-03-10 12:27:10 +08:00
|
|
|
}
|
2022-02-16 17:51:14 +08:00
|
|
|
_ => *stmt = (block, span).into(),
|
2021-04-05 14:57:07 +08:00
|
|
|
}
|
2020-03-09 21:57:07 +08:00
|
|
|
}
|
2021-03-13 23:43:05 +08:00
|
|
|
// try { pure try_block } catch ( var ) { catch_block } -> try_block
|
2022-12-23 14:26:06 +08:00
|
|
|
Stmt::TryCatch(x, ..) if x.body.iter().all(Stmt::is_pure) => {
|
2020-10-20 23:16:03 +08:00
|
|
|
// If try block is pure, there will never be any exceptions
|
|
|
|
state.set_dirty();
|
2022-02-16 17:51:14 +08:00
|
|
|
*stmt = (
|
2022-12-23 14:26:06 +08:00
|
|
|
optimize_stmt_block(mem::take(&mut *x.body), state, false, true, false),
|
|
|
|
x.body.span(),
|
2022-02-16 17:51:14 +08:00
|
|
|
)
|
|
|
|
.into();
|
2020-10-20 23:16:03 +08:00
|
|
|
}
|
2021-03-13 23:43:05 +08:00
|
|
|
// try { try_block } catch ( var ) { catch_block }
|
2022-02-08 09:02:15 +08:00
|
|
|
Stmt::TryCatch(x, ..) => {
|
2022-12-23 14:26:06 +08:00
|
|
|
*x.body = optimize_stmt_block(mem::take(&mut *x.body), state, false, true, false);
|
|
|
|
*x.branch = optimize_stmt_block(mem::take(&mut *x.branch), state, false, true, false);
|
2020-10-27 18:18:19 +08:00
|
|
|
}
|
2022-02-16 17:51:14 +08:00
|
|
|
|
2022-07-19 21:59:49 +08:00
|
|
|
// expr(stmt)
|
|
|
|
Stmt::Expr(expr) if matches!(**expr, Expr::Stmt(..)) => {
|
|
|
|
state.set_dirty();
|
|
|
|
match expr.as_mut() {
|
|
|
|
Expr::Stmt(block) if !block.is_empty() => {
|
|
|
|
let mut stmt_block = *mem::take(block);
|
|
|
|
*stmt_block =
|
|
|
|
optimize_stmt_block(mem::take(&mut *stmt_block), state, true, true, false);
|
|
|
|
*stmt = stmt_block.into();
|
|
|
|
}
|
|
|
|
Expr::Stmt(..) => *stmt = Stmt::Noop(expr.position()),
|
2022-07-23 21:00:58 +08:00
|
|
|
_ => unreachable!("`Expr::Stmt`"),
|
2022-07-19 21:59:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Expr(expr) => {
|
2021-06-02 14:29:18 +08:00
|
|
|
optimize_expr(expr, state, false);
|
2022-02-16 17:51:14 +08:00
|
|
|
|
2022-11-13 21:05:14 +08:00
|
|
|
// Do not promote until the expression is fully optimized
|
|
|
|
if !state.is_dirty() && matches!(**expr, Expr::FnCall(..) | Expr::Stmt(..)) {
|
2022-10-14 18:31:40 +08:00
|
|
|
*stmt = match *mem::take(expr) {
|
|
|
|
// func(...);
|
|
|
|
Expr::FnCall(x, pos) => Stmt::FnCall(x, pos),
|
|
|
|
// {};
|
|
|
|
Expr::Stmt(x) if x.is_empty() => Stmt::Noop(x.position()),
|
|
|
|
// {...};
|
|
|
|
Expr::Stmt(x) => (*x).into(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2022-11-13 21:05:14 +08:00
|
|
|
state.set_dirty();
|
2021-04-21 18:16:24 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-16 17:51:14 +08:00
|
|
|
|
2022-10-29 12:09:18 +08:00
|
|
|
// break expr;
|
|
|
|
Stmt::BreakLoop(Some(ref mut expr), ..) => optimize_expr(expr, state, false),
|
|
|
|
|
2020-03-18 10:36:50 +08:00
|
|
|
// return expr;
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Return(Some(ref mut expr), ..) => optimize_expr(expr, state, false),
|
2020-11-12 12:37:42 +08:00
|
|
|
|
2020-03-18 10:36:50 +08:00
|
|
|
// All other statements - skip
|
2020-11-12 12:37:42 +08:00
|
|
|
_ => (),
|
2020-03-09 21:57:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-26 23:21:16 +08:00
|
|
|
/// Optimize an [expression][Expr].
|
2022-04-11 16:29:16 +08:00
|
|
|
fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
|
2020-03-18 10:36:50 +08:00
|
|
|
// These keywords are handled specially
|
2020-10-05 12:14:34 +08:00
|
|
|
const DONT_EVAL_KEYWORDS: &[&str] = &[
|
2020-11-22 15:41:55 +08:00
|
|
|
KEYWORD_PRINT, // side effects
|
|
|
|
KEYWORD_DEBUG, // side effects
|
|
|
|
KEYWORD_EVAL, // arbitrary scripts
|
2020-10-05 12:14:34 +08:00
|
|
|
];
|
2020-03-17 10:27:43 +08:00
|
|
|
|
2020-03-09 21:57:07 +08:00
|
|
|
match expr {
|
2020-11-04 11:49:02 +08:00
|
|
|
// {}
|
2021-04-16 13:15:11 +08:00
|
|
|
Expr::Stmt(x) if x.is_empty() => { state.set_dirty(); *expr = Expr::Unit(x.position()) }
|
2022-07-19 21:59:49 +08:00
|
|
|
Expr::Stmt(x) if x.len() == 1 && matches!(x.statements()[0], Stmt::Expr(..)) => {
|
|
|
|
state.set_dirty();
|
|
|
|
match x.take_statements().remove(0) {
|
|
|
|
Stmt::Expr(mut e) => {
|
|
|
|
optimize_expr(&mut e, state, false);
|
|
|
|
*expr = *e;
|
|
|
|
}
|
2022-07-23 21:00:58 +08:00
|
|
|
_ => unreachable!("`Expr::Stmt`")
|
2022-07-19 21:59:49 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-05 14:57:07 +08:00
|
|
|
// { stmt; ... } - do not count promotion as dirty because it gets turned back into an array
|
|
|
|
Expr::Stmt(x) => {
|
2021-12-15 23:21:05 +08:00
|
|
|
***x = optimize_stmt_block(mem::take(&mut **x), state, true, true, false);
|
2021-04-05 14:57:07 +08:00
|
|
|
|
|
|
|
// { Stmt(Expr) } - promote
|
2022-07-27 16:04:24 +08:00
|
|
|
if let [ Stmt::Expr(e) ] = &mut ****x { state.set_dirty(); *expr = mem::take(e); }
|
2021-04-04 13:13:07 +08:00
|
|
|
}
|
2022-06-10 10:26:06 +08:00
|
|
|
// ()?.rhs
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
Expr::Dot(x, options, ..) if options.contains(ASTFlags::NEGATED) && matches!(x.lhs, Expr::Unit(..)) => {
|
|
|
|
state.set_dirty();
|
|
|
|
*expr = mem::take(&mut x.lhs);
|
|
|
|
}
|
2020-03-18 10:36:50 +08:00
|
|
|
// lhs.rhs
|
2020-03-29 17:15:12 +08:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-06-10 10:26:06 +08:00
|
|
|
Expr::Dot(x, ..) if !_chaining => match (&mut x.lhs, &mut x.rhs) {
|
2020-04-10 21:02:13 +08:00
|
|
|
// map.string
|
2022-02-08 09:02:15 +08:00
|
|
|
(Expr::Map(m, pos), Expr::Property(p, ..)) if m.0.iter().all(|(.., x)| x.is_pure()) => {
|
2022-01-28 10:11:40 +08:00
|
|
|
let prop = p.2.as_str();
|
2020-04-10 21:02:13 +08:00
|
|
|
// Map literal where everything is pure - promote the indexed item.
|
|
|
|
// All other items can be thrown away.
|
|
|
|
state.set_dirty();
|
2022-03-05 17:57:23 +08:00
|
|
|
*expr = mem::take(&mut m.0).into_iter().find(|(x, ..)| x.as_str() == prop)
|
2022-07-27 18:04:59 +08:00
|
|
|
.map_or_else(|| Expr::Unit(*pos), |(.., mut expr)| { expr.set_position(*pos); expr });
|
2020-04-10 21:02:13 +08:00
|
|
|
}
|
2020-11-03 13:08:19 +08:00
|
|
|
// var.rhs
|
2022-02-08 09:02:15 +08:00
|
|
|
(Expr::Variable(..), rhs) => optimize_expr(rhs, state, true),
|
2022-11-21 16:27:12 +08:00
|
|
|
// const.type_of()
|
|
|
|
(lhs, Expr::MethodCall(x, pos)) if lhs.is_constant() && x.name == KEYWORD_TYPE_OF && x.args.is_empty() => {
|
|
|
|
if let Some(value) = lhs.get_literal_value() {
|
|
|
|
state.set_dirty();
|
|
|
|
let typ = state.engine.map_type_name(value.type_name()).into();
|
|
|
|
*expr = Expr::from_dynamic(typ, *pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// const.is_shared()
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
(lhs, Expr::MethodCall(x, pos)) if lhs.is_constant() && x.name == crate::engine::KEYWORD_IS_SHARED && x.args.is_empty() => {
|
|
|
|
if let Some(..) = lhs.get_literal_value() {
|
|
|
|
state.set_dirty();
|
|
|
|
*expr = Expr::from_dynamic(Dynamic::FALSE, *pos);
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 21:02:13 +08:00
|
|
|
// lhs.rhs
|
2021-06-02 14:29:18 +08:00
|
|
|
(lhs, rhs) => { optimize_expr(lhs, state, false); optimize_expr(rhs, state, true); }
|
2020-04-10 21:02:13 +08:00
|
|
|
}
|
2021-06-02 14:29:18 +08:00
|
|
|
// ....lhs.rhs
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-06-10 10:26:06 +08:00
|
|
|
Expr::Dot(x,..) => { optimize_expr(&mut x.lhs, state, false); optimize_expr(&mut x.rhs, state, _chaining); }
|
2020-03-11 11:03:18 +08:00
|
|
|
|
2022-06-12 00:32:12 +08:00
|
|
|
// ()?[rhs]
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
Expr::Index(x, options, ..) if options.contains(ASTFlags::NEGATED) && matches!(x.lhs, Expr::Unit(..)) => {
|
|
|
|
state.set_dirty();
|
|
|
|
*expr = mem::take(&mut x.lhs);
|
|
|
|
}
|
2020-03-18 10:36:50 +08:00
|
|
|
// lhs[rhs]
|
2020-03-10 17:10:33 +08:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-12-22 17:34:58 +08:00
|
|
|
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::Index(x, ..) if !_chaining => match (&mut x.lhs, &mut x.rhs) {
|
2020-03-18 10:36:50 +08:00
|
|
|
// array[int]
|
2022-08-29 14:27:05 +08:00
|
|
|
(Expr::Array(a, pos), Expr::IntegerConstant(i, ..)) if *i >= 0 && *i <= crate::MAX_USIZE_INT && (*i as usize) < a.len() && a.iter().all(Expr::is_pure) => {
|
2020-03-16 12:41:52 +08:00
|
|
|
// Array literal where everything is pure - promote the indexed item.
|
2020-03-11 23:43:10 +08:00
|
|
|
// All other items can be thrown away.
|
2020-03-13 18:12:41 +08:00
|
|
|
state.set_dirty();
|
2021-06-16 18:36:33 +08:00
|
|
|
let mut result = mem::take(&mut a[*i as usize]);
|
2020-11-12 12:37:42 +08:00
|
|
|
result.set_position(*pos);
|
|
|
|
*expr = result;
|
2020-04-10 21:02:13 +08:00
|
|
|
}
|
2021-04-10 15:00:03 +08:00
|
|
|
// array[-int]
|
2022-08-29 14:27:05 +08:00
|
|
|
(Expr::Array(a, pos), Expr::IntegerConstant(i, ..)) if *i < 0 && i.unsigned_abs() as u64 <= a.len() as u64 && a.iter().all(Expr::is_pure) => {
|
2021-04-10 15:00:03 +08:00
|
|
|
// Array literal where everything is pure - promote the indexed item.
|
|
|
|
// All other items can be thrown away.
|
|
|
|
state.set_dirty();
|
2022-07-27 16:04:24 +08:00
|
|
|
let index = a.len() - i.unsigned_abs() as usize;
|
2021-06-16 18:36:33 +08:00
|
|
|
let mut result = mem::take(&mut a[index]);
|
2021-04-10 15:00:03 +08:00
|
|
|
result.set_position(*pos);
|
|
|
|
*expr = result;
|
|
|
|
}
|
2020-04-10 21:02:13 +08:00
|
|
|
// map[string]
|
2022-02-08 09:02:15 +08:00
|
|
|
(Expr::Map(m, pos), Expr::StringConstant(s, ..)) if m.0.iter().all(|(.., x)| x.is_pure()) => {
|
2020-04-10 21:02:13 +08:00
|
|
|
// Map literal where everything is pure - promote the indexed item.
|
|
|
|
// All other items can be thrown away.
|
|
|
|
state.set_dirty();
|
2022-03-05 17:57:23 +08:00
|
|
|
*expr = mem::take(&mut m.0).into_iter().find(|(x, ..)| x.as_str() == s.as_str())
|
2022-07-27 18:04:59 +08:00
|
|
|
.map_or_else(|| Expr::Unit(*pos), |(.., mut expr)| { expr.set_position(*pos); expr });
|
2020-03-10 17:10:33 +08:00
|
|
|
}
|
2021-06-02 14:29:18 +08:00
|
|
|
// int[int]
|
2022-08-29 14:27:05 +08:00
|
|
|
(Expr::IntegerConstant(n, pos), Expr::IntegerConstant(i, ..)) if *i >= 0 && *i <= crate::MAX_USIZE_INT && (*i as usize) < crate::INT_BITS => {
|
2021-06-02 14:29:18 +08:00
|
|
|
// Bit-field literal indexing - get the bit
|
|
|
|
state.set_dirty();
|
|
|
|
*expr = Expr::BoolConstant((*n & (1 << (*i as usize))) != 0, *pos);
|
|
|
|
}
|
|
|
|
// int[-int]
|
2022-08-29 14:27:05 +08:00
|
|
|
(Expr::IntegerConstant(n, pos), Expr::IntegerConstant(i, ..)) if *i < 0 && i.unsigned_abs() as u64 <= crate::INT_BITS as u64 => {
|
2021-06-02 14:29:18 +08:00
|
|
|
// Bit-field literal indexing - get the bit
|
|
|
|
state.set_dirty();
|
2022-07-27 16:04:24 +08:00
|
|
|
*expr = Expr::BoolConstant((*n & (1 << (crate::INT_BITS - i.unsigned_abs() as usize))) != 0, *pos);
|
2021-06-02 14:29:18 +08:00
|
|
|
}
|
2020-03-18 10:36:50 +08:00
|
|
|
// string[int]
|
2022-08-29 14:27:05 +08:00
|
|
|
(Expr::StringConstant(s, pos), Expr::IntegerConstant(i, ..)) if *i >= 0 && *i <= crate::MAX_USIZE_INT && (*i as usize) < s.chars().count() => {
|
2020-03-16 12:41:52 +08:00
|
|
|
// String literal indexing - get the character
|
|
|
|
state.set_dirty();
|
2022-01-06 11:07:52 +08:00
|
|
|
*expr = Expr::CharConstant(s.chars().nth(*i as usize).unwrap(), *pos);
|
2020-03-16 12:41:52 +08:00
|
|
|
}
|
2021-04-10 15:00:03 +08:00
|
|
|
// string[-int]
|
2022-08-29 14:27:05 +08:00
|
|
|
(Expr::StringConstant(s, pos), Expr::IntegerConstant(i, ..)) if *i < 0 && i.unsigned_abs() as u64 <= s.chars().count() as u64 => {
|
2021-04-10 15:00:03 +08:00
|
|
|
// String literal indexing - get the character
|
|
|
|
state.set_dirty();
|
2022-07-27 16:04:24 +08:00
|
|
|
*expr = Expr::CharConstant(s.chars().rev().nth(i.unsigned_abs() as usize - 1).unwrap(), *pos);
|
2021-04-10 15:00:03 +08:00
|
|
|
}
|
2020-11-03 13:08:19 +08:00
|
|
|
// var[rhs]
|
2022-02-08 09:02:15 +08:00
|
|
|
(Expr::Variable(..), rhs) => optimize_expr(rhs, state, true),
|
2020-03-18 10:36:50 +08:00
|
|
|
// lhs[rhs]
|
2021-06-02 14:29:18 +08:00
|
|
|
(lhs, rhs) => { optimize_expr(lhs, state, false); optimize_expr(rhs, state, true); }
|
2020-03-10 17:10:33 +08:00
|
|
|
},
|
2021-06-02 14:29:18 +08:00
|
|
|
// ...[lhs][rhs]
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::Index(x, ..) => { optimize_expr(&mut x.lhs, state, false); optimize_expr(&mut x.rhs, state, _chaining); }
|
2021-04-04 13:13:07 +08:00
|
|
|
// ``
|
2021-06-29 18:41:03 +08:00
|
|
|
Expr::InterpolatedString(x, pos) if x.is_empty() => {
|
2021-04-04 13:13:07 +08:00
|
|
|
state.set_dirty();
|
2022-11-28 23:24:22 +08:00
|
|
|
*expr = Expr::StringConstant(state.engine.const_empty_string(), *pos);
|
2021-04-04 13:13:07 +08:00
|
|
|
}
|
2022-07-29 10:49:03 +08:00
|
|
|
// `... ${const} ...`
|
|
|
|
Expr::InterpolatedString(..) if expr.is_constant() => {
|
2021-04-04 13:13:07 +08:00
|
|
|
state.set_dirty();
|
2022-07-29 10:49:03 +08:00
|
|
|
*expr = Expr::StringConstant(expr.get_literal_value().unwrap().cast::<ImmutableString>(), expr.position());
|
2021-04-04 13:13:07 +08:00
|
|
|
}
|
|
|
|
// `... ${ ... } ...`
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::InterpolatedString(x, ..) => {
|
2021-09-20 22:36:10 +08:00
|
|
|
x.iter_mut().for_each(|expr| optimize_expr(expr, state, false));
|
|
|
|
|
2021-06-16 18:36:33 +08:00
|
|
|
let mut n = 0;
|
2021-04-04 13:13:07 +08:00
|
|
|
|
|
|
|
// Merge consecutive strings
|
2021-12-15 23:21:05 +08:00
|
|
|
while n < x.len() - 1 {
|
2021-04-04 13:13:07 +08:00
|
|
|
match (mem::take(&mut x[n]), mem::take(&mut x[n+1])) {
|
2022-02-08 09:02:15 +08:00
|
|
|
(Expr::StringConstant(mut s1, pos), Expr::StringConstant(s2, ..)) => { s1 += s2; x[n] = Expr::StringConstant(s1, pos); x.remove(n+1); state.set_dirty(); }
|
2022-02-10 17:55:32 +08:00
|
|
|
(expr1, Expr::Unit(..)) => { x[n] = expr1; x.remove(n+1); state.set_dirty(); }
|
2022-02-08 09:46:14 +08:00
|
|
|
(Expr::Unit(..), expr2) => { x[n+1] = expr2; x.remove(n); state.set_dirty(); }
|
2022-02-08 09:02:15 +08:00
|
|
|
(expr1, Expr::StringConstant(s, ..)) if s.is_empty() => { x[n] = expr1; x.remove(n+1); state.set_dirty(); }
|
|
|
|
(Expr::StringConstant(s, ..), expr2) if s.is_empty()=> { x[n+1] = expr2; x.remove(n); state.set_dirty(); }
|
2021-12-15 23:21:05 +08:00
|
|
|
(expr1, expr2) => { x[n] = expr1; x[n+1] = expr2; n += 1; }
|
2021-04-04 13:13:07 +08:00
|
|
|
}
|
|
|
|
}
|
2021-05-05 18:38:52 +08:00
|
|
|
|
|
|
|
x.shrink_to_fit();
|
2021-04-04 13:13:07 +08:00
|
|
|
}
|
2020-11-14 19:04:49 +08:00
|
|
|
// [ constant .. ]
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::Array(..) if expr.is_constant() => {
|
2020-11-14 19:04:49 +08:00
|
|
|
state.set_dirty();
|
2022-01-06 11:07:52 +08:00
|
|
|
*expr = Expr::DynamicConstant(expr.get_literal_value().unwrap().into(), expr.position());
|
2020-11-14 19:04:49 +08:00
|
|
|
}
|
2020-03-18 10:36:50 +08:00
|
|
|
// [ items .. ]
|
2020-03-10 17:10:33 +08:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::Array(x, ..) => x.iter_mut().for_each(|expr| optimize_expr(expr, state, false)),
|
2020-11-14 19:04:49 +08:00
|
|
|
// #{ key:constant, .. }
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::Map(..) if expr.is_constant() => {
|
2020-11-14 19:04:49 +08:00
|
|
|
state.set_dirty();
|
2022-01-06 11:07:52 +08:00
|
|
|
*expr = Expr::DynamicConstant(expr.get_literal_value().unwrap().into(), expr.position());
|
2020-11-14 19:04:49 +08:00
|
|
|
}
|
2020-11-12 12:37:42 +08:00
|
|
|
// #{ key:value, .. }
|
2020-03-29 23:53:35 +08:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::Map(x, ..) => x.0.iter_mut().for_each(|(.., expr)| optimize_expr(expr, state, false)),
|
2020-03-18 10:36:50 +08:00
|
|
|
// lhs && rhs
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::And(x, ..) => match (&mut x.lhs, &mut x.rhs) {
|
2020-03-18 10:36:50 +08:00
|
|
|
// true && rhs -> rhs
|
2022-02-08 09:02:15 +08:00
|
|
|
(Expr::BoolConstant(true, ..), rhs) => { state.set_dirty(); optimize_expr(rhs, state, false); *expr = mem::take(rhs); }
|
2020-03-18 10:36:50 +08:00
|
|
|
// false && rhs -> false
|
2022-02-08 09:02:15 +08:00
|
|
|
(Expr::BoolConstant(false, pos), ..) => { state.set_dirty(); *expr = Expr::BoolConstant(false, *pos); }
|
2020-03-18 10:36:50 +08:00
|
|
|
// lhs && true -> lhs
|
2022-02-08 09:02:15 +08:00
|
|
|
(lhs, Expr::BoolConstant(true, ..)) => { state.set_dirty(); optimize_expr(lhs, state, false); *expr = mem::take(lhs); }
|
2020-03-18 10:36:50 +08:00
|
|
|
// lhs && rhs
|
2021-12-15 23:21:05 +08:00
|
|
|
(lhs, rhs) => { optimize_expr(lhs, state, false); optimize_expr(rhs, state, false); }
|
2020-03-09 21:57:07 +08:00
|
|
|
},
|
2020-03-18 10:36:50 +08:00
|
|
|
// lhs || rhs
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::Or(ref mut x, ..) => match (&mut x.lhs, &mut x.rhs) {
|
2020-03-18 10:36:50 +08:00
|
|
|
// false || rhs -> rhs
|
2022-02-08 09:02:15 +08:00
|
|
|
(Expr::BoolConstant(false, ..), rhs) => { state.set_dirty(); optimize_expr(rhs, state, false); *expr = mem::take(rhs); }
|
2020-03-18 10:36:50 +08:00
|
|
|
// true || rhs -> true
|
2022-02-08 09:02:15 +08:00
|
|
|
(Expr::BoolConstant(true, pos), ..) => { state.set_dirty(); *expr = Expr::BoolConstant(true, *pos); }
|
2020-03-18 10:36:50 +08:00
|
|
|
// lhs || false
|
2022-02-08 09:02:15 +08:00
|
|
|
(lhs, Expr::BoolConstant(false, ..)) => { state.set_dirty(); optimize_expr(lhs, state, false); *expr = mem::take(lhs); }
|
2020-03-18 10:36:50 +08:00
|
|
|
// lhs || rhs
|
2021-12-15 23:21:05 +08:00
|
|
|
(lhs, rhs) => { optimize_expr(lhs, state, false); optimize_expr(rhs, state, false); }
|
2020-03-09 21:57:07 +08:00
|
|
|
},
|
2022-06-10 11:22:33 +08:00
|
|
|
// () ?? rhs -> rhs
|
|
|
|
Expr::Coalesce(x, ..) if matches!(x.lhs, Expr::Unit(..)) => {
|
|
|
|
state.set_dirty();
|
|
|
|
*expr = mem::take(&mut x.rhs);
|
|
|
|
},
|
|
|
|
// lhs:constant ?? rhs -> lhs
|
|
|
|
Expr::Coalesce(x, ..) if x.lhs.is_constant() => {
|
|
|
|
state.set_dirty();
|
|
|
|
*expr = mem::take(&mut x.lhs);
|
|
|
|
},
|
2020-03-11 23:43:10 +08:00
|
|
|
|
2022-11-30 14:11:57 +08:00
|
|
|
// !true or !false
|
|
|
|
Expr::FnCall(x,..)
|
|
|
|
if x.name == OP_NOT
|
|
|
|
&& x.args.len() == 1
|
|
|
|
&& matches!(x.args[0], Expr::BoolConstant(..))
|
|
|
|
=> {
|
|
|
|
state.set_dirty();
|
|
|
|
if let Expr::BoolConstant(b, pos) = x.args[0] {
|
|
|
|
*expr = Expr::BoolConstant(!b, pos)
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:52:50 +08:00
|
|
|
// eval!
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::FnCall(x, ..) if x.name == KEYWORD_EVAL => {
|
2021-01-12 23:52:50 +08:00
|
|
|
state.propagate_constants = false;
|
|
|
|
}
|
2021-04-06 12:26:38 +08:00
|
|
|
// Fn
|
|
|
|
Expr::FnCall(x, pos)
|
2021-04-20 23:28:04 +08:00
|
|
|
if !x.is_qualified() // Non-qualified
|
2021-04-06 12:26:38 +08:00
|
|
|
&& state.optimization_level == OptimizationLevel::Simple // simple optimizations
|
2021-06-08 14:46:49 +08:00
|
|
|
&& x.args.len() == 1
|
2021-04-06 12:26:38 +08:00
|
|
|
&& x.name == KEYWORD_FN_PTR
|
2022-10-30 23:33:44 +08:00
|
|
|
&& x.constant_args()
|
2021-04-06 12:26:38 +08:00
|
|
|
=> {
|
2021-06-08 14:46:49 +08:00
|
|
|
let fn_name = match x.args[0] {
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::StringConstant(ref s, ..) => s.clone().into(),
|
2021-12-02 12:49:57 +08:00
|
|
|
_ => Dynamic::UNIT
|
2021-06-08 14:46:49 +08:00
|
|
|
};
|
|
|
|
|
2022-07-27 18:04:59 +08:00
|
|
|
if let Ok(fn_ptr) = fn_name.into_immutable_string().map_err(Into::into).and_then(FnPtr::try_from) {
|
2021-12-02 12:49:57 +08:00
|
|
|
state.set_dirty();
|
|
|
|
*expr = Expr::DynamicConstant(Box::new(fn_ptr.into()), *pos);
|
|
|
|
} else {
|
|
|
|
optimize_expr(&mut x.args[0], state, false);
|
2021-06-08 14:46:49 +08:00
|
|
|
}
|
2021-04-06 12:26:38 +08:00
|
|
|
}
|
|
|
|
|
2020-04-01 09:51:33 +08:00
|
|
|
// Do not call some special keywords
|
2022-03-05 17:57:23 +08:00
|
|
|
Expr::FnCall(x, ..) if DONT_EVAL_KEYWORDS.contains(&x.name.as_str()) => {
|
2021-06-02 14:29:18 +08:00
|
|
|
x.args.iter_mut().for_each(|a| optimize_expr(a, state, false));
|
2020-05-09 21:46:38 +08:00
|
|
|
}
|
2020-03-19 19:53:42 +08:00
|
|
|
|
2020-10-09 11:15:25 +08:00
|
|
|
// Call built-in operators
|
2020-11-12 12:37:42 +08:00
|
|
|
Expr::FnCall(x, pos)
|
2021-04-20 23:28:04 +08:00
|
|
|
if !x.is_qualified() // Non-qualified
|
2020-10-05 10:27:31 +08:00
|
|
|
&& state.optimization_level == OptimizationLevel::Simple // simple optimizations
|
2022-10-30 23:33:44 +08:00
|
|
|
&& x.constant_args() // all arguments are constants
|
2020-10-05 10:27:31 +08:00
|
|
|
=> {
|
2022-03-05 12:06:47 +08:00
|
|
|
let arg_values = &mut x.args.iter().map(|e| e.get_literal_value().unwrap()).collect::<StaticVec<_>>();
|
2020-10-05 10:27:31 +08:00
|
|
|
let arg_types: StaticVec<_> = arg_values.iter().map(Dynamic::type_id).collect();
|
|
|
|
|
2021-11-01 09:42:22 +08:00
|
|
|
match x.name.as_str() {
|
|
|
|
KEYWORD_TYPE_OF if arg_values.len() == 1 => {
|
|
|
|
state.set_dirty();
|
2022-03-14 08:50:17 +08:00
|
|
|
let typ = state.engine.map_type_name(arg_values[0].type_name()).into();
|
|
|
|
*expr = Expr::from_dynamic(typ, *pos);
|
2021-11-01 09:42:22 +08:00
|
|
|
return;
|
2021-11-01 10:07:45 +08:00
|
|
|
}
|
2021-07-14 22:33:47 +08:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
2021-12-02 12:49:46 +08:00
|
|
|
crate::engine::KEYWORD_IS_SHARED if arg_values.len() == 1 => {
|
2021-11-01 09:42:22 +08:00
|
|
|
state.set_dirty();
|
|
|
|
*expr = Expr::from_dynamic(Dynamic::FALSE, *pos);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-14 13:57:58 +08:00
|
|
|
// Overloaded operators can override built-in.
|
2022-11-28 23:24:22 +08:00
|
|
|
_ if x.args.len() == 2 && x.op_token != Token::NONE && (state.engine.fast_operators() || !state.engine.has_native_fn_override(x.hashes.native(), &arg_types)) => {
|
2022-11-25 20:42:16 +08:00
|
|
|
if let Some(result) = get_builtin_binary_op_fn(x.op_token.clone(), &arg_values[0], &arg_values[1])
|
2022-12-03 16:20:13 +08:00
|
|
|
.and_then(|(f, ctx)| {
|
|
|
|
let context = if ctx {
|
|
|
|
Some((state.engine, x.name.as_str(),None, &state.global, *pos).into())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2022-01-06 11:07:52 +08:00
|
|
|
let (first, second) = arg_values.split_first_mut().unwrap();
|
2021-11-05 19:35:33 +08:00
|
|
|
(f)(context, &mut [ first, &mut second[0] ]).ok()
|
2021-11-01 09:42:22 +08:00
|
|
|
}) {
|
|
|
|
state.set_dirty();
|
|
|
|
*expr = Expr::from_dynamic(result, *pos);
|
|
|
|
return;
|
|
|
|
}
|
2020-10-05 10:27:31 +08:00
|
|
|
}
|
2021-11-01 09:42:22 +08:00
|
|
|
_ => ()
|
2020-10-05 10:27:31 +08:00
|
|
|
}
|
|
|
|
|
2021-06-02 14:29:18 +08:00
|
|
|
x.args.iter_mut().for_each(|a| optimize_expr(a, state, false));
|
2021-03-28 19:04:25 +08:00
|
|
|
|
2021-06-08 14:46:49 +08:00
|
|
|
// Move constant arguments
|
2022-09-26 23:35:37 +08:00
|
|
|
for arg in &mut x.args {
|
2022-03-05 12:06:47 +08:00
|
|
|
match arg {
|
|
|
|
Expr::DynamicConstant(..) | Expr::Unit(..)
|
|
|
|
| Expr::StringConstant(..) | Expr::CharConstant(..)
|
|
|
|
| Expr::BoolConstant(..) | Expr::IntegerConstant(..) => (),
|
2022-03-05 17:57:23 +08:00
|
|
|
|
2022-03-05 12:06:47 +08:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
Expr:: FloatConstant(..) => (),
|
2022-03-05 17:57:23 +08:00
|
|
|
|
2022-03-05 12:06:47 +08:00
|
|
|
_ => if let Some(value) = arg.get_literal_value() {
|
|
|
|
state.set_dirty();
|
|
|
|
*arg = Expr::DynamicConstant(value.into(), arg.start_position());
|
|
|
|
},
|
2021-06-08 14:46:49 +08:00
|
|
|
}
|
2022-03-05 12:06:47 +08:00
|
|
|
}
|
2020-10-05 10:27:31 +08:00
|
|
|
}
|
|
|
|
|
2020-03-18 10:36:50 +08:00
|
|
|
// Eagerly call functions
|
2020-11-12 12:37:42 +08:00
|
|
|
Expr::FnCall(x, pos)
|
2022-03-04 12:22:44 +08:00
|
|
|
if !x.is_qualified() // non-qualified
|
2020-05-09 21:46:38 +08:00
|
|
|
&& state.optimization_level == OptimizationLevel::Full // full optimizations
|
2022-10-30 23:33:44 +08:00
|
|
|
&& x.constant_args() // all arguments are constants
|
2020-03-17 10:27:43 +08:00
|
|
|
=> {
|
2020-10-05 10:27:31 +08:00
|
|
|
// First search for script-defined functions (can override built-in)
|
2020-10-05 12:09:45 +08:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2022-11-10 11:49:10 +08:00
|
|
|
let has_script_fn = !x.hashes.is_native_only() && state.global.lib.iter().find_map(|m| m.get_script_fn(&x.name, x.args.len())).is_some();
|
2020-10-05 12:09:45 +08:00
|
|
|
#[cfg(feature = "no_function")]
|
|
|
|
let has_script_fn = false;
|
2020-03-19 22:29:02 +08:00
|
|
|
|
2020-10-05 10:27:31 +08:00
|
|
|
if !has_script_fn {
|
2022-09-25 18:22:48 +08:00
|
|
|
let arg_values = &mut x.args.iter().map(Expr::get_literal_value).collect::<Option<StaticVec<_>>>().unwrap();
|
2021-07-14 13:57:58 +08:00
|
|
|
|
|
|
|
let result = match x.name.as_str() {
|
2022-12-20 16:52:55 +08:00
|
|
|
KEYWORD_TYPE_OF if arg_values.len() == 1 => Some(state.engine.map_type_name(arg_values[0].type_name()).into()),
|
2021-07-14 22:33:47 +08:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
2022-12-20 16:52:55 +08:00
|
|
|
crate::engine::KEYWORD_IS_SHARED if arg_values.len() == 1 => Some(Dynamic::FALSE),
|
2022-11-25 20:42:16 +08:00
|
|
|
_ => state.call_fn_with_constant_arguments(&x.name, x.op_token.clone(), arg_values)
|
2020-10-05 10:27:31 +08:00
|
|
|
};
|
|
|
|
|
2022-12-20 16:52:55 +08:00
|
|
|
if let Some(r) = result {
|
2020-06-01 15:25:22 +08:00
|
|
|
state.set_dirty();
|
2022-12-20 16:52:55 +08:00
|
|
|
*expr = Expr::from_dynamic(r, *pos);
|
2020-11-12 12:37:42 +08:00
|
|
|
return;
|
2020-10-05 10:27:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 14:29:18 +08:00
|
|
|
x.args.iter_mut().for_each(|a| optimize_expr(a, state, false));
|
2020-03-15 22:39:58 +08:00
|
|
|
}
|
2020-03-19 19:53:42 +08:00
|
|
|
|
2022-03-04 12:22:44 +08:00
|
|
|
// id(args ..) or xxx.id(args ..) -> optimize function call arguments
|
2022-09-26 23:35:37 +08:00
|
|
|
Expr::FnCall(x, ..) | Expr::MethodCall(x, ..) => for arg in &mut x.args {
|
2021-06-08 14:46:49 +08:00
|
|
|
optimize_expr(arg, state, false);
|
2021-03-28 19:04:25 +08:00
|
|
|
|
2021-06-08 14:46:49 +08:00
|
|
|
// Move constant arguments
|
2022-03-05 12:06:47 +08:00
|
|
|
match arg {
|
|
|
|
Expr::DynamicConstant(..) | Expr::Unit(..)
|
|
|
|
| Expr::StringConstant(..) | Expr::CharConstant(..)
|
|
|
|
| Expr::BoolConstant(..) | Expr::IntegerConstant(..) => (),
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
Expr:: FloatConstant(..) => (),
|
|
|
|
|
|
|
|
_ => if let Some(value) = arg.get_literal_value() {
|
|
|
|
state.set_dirty();
|
|
|
|
*arg = Expr::DynamicConstant(value.into(), arg.start_position());
|
|
|
|
},
|
2021-03-28 19:04:25 +08:00
|
|
|
}
|
2021-06-08 14:46:49 +08:00
|
|
|
},
|
2020-03-19 19:53:42 +08:00
|
|
|
|
2020-03-18 10:36:50 +08:00
|
|
|
// constant-name
|
2022-01-29 11:09:43 +08:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-03-05 17:57:23 +08:00
|
|
|
Expr::Variable(x, ..) if !x.1.is_empty() => (),
|
|
|
|
Expr::Variable(x, .., pos) if state.find_constant(&x.3).is_some() => {
|
2020-03-13 18:12:41 +08:00
|
|
|
// Replace constant with value
|
2022-03-05 17:57:23 +08:00
|
|
|
*expr = Expr::from_dynamic(state.find_constant(&x.3).unwrap().clone(), *pos);
|
2021-05-16 21:21:13 +08:00
|
|
|
state.set_dirty();
|
2020-03-13 18:12:41 +08:00
|
|
|
}
|
2020-03-19 19:53:42 +08:00
|
|
|
|
2020-07-09 19:54:28 +08:00
|
|
|
// Custom syntax
|
2022-07-05 22:59:03 +08:00
|
|
|
#[cfg(not(feature = "no_custom_syntax"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::Custom(x, ..) => {
|
2021-07-04 16:31:01 +08:00
|
|
|
if x.scope_may_be_changed {
|
2021-01-12 23:52:50 +08:00
|
|
|
state.propagate_constants = false;
|
|
|
|
}
|
2022-07-06 12:56:15 +08:00
|
|
|
// Do not optimize custom syntax expressions as you won't know how they would be called
|
2021-01-12 23:52:50 +08:00
|
|
|
}
|
2020-07-09 19:54:28 +08:00
|
|
|
|
2020-03-18 10:36:50 +08:00
|
|
|
// All other expressions - skip
|
2020-11-12 12:37:42 +08:00
|
|
|
_ => (),
|
2020-03-09 21:57:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 20:42:16 +08:00
|
|
|
impl Engine {
|
|
|
|
/// Has a system function a Rust-native override?
|
|
|
|
fn has_native_fn_override(&self, hash_script: u64, arg_types: impl AsRef<[TypeId]>) -> bool {
|
|
|
|
let hash = calc_fn_hash_full(hash_script, arg_types.as_ref().iter().copied());
|
2021-08-13 13:42:39 +08:00
|
|
|
|
2022-11-25 20:42:16 +08:00
|
|
|
// First check the global namespace and packages, but skip modules that are standard because
|
|
|
|
// they should never conflict with system functions.
|
|
|
|
if self
|
|
|
|
.global_modules
|
|
|
|
.iter()
|
|
|
|
.filter(|m| !m.flags.contains(ModuleFlags::STANDARD_LIB))
|
|
|
|
.any(|m| m.contains_fn(hash))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2020-03-15 22:39:58 +08:00
|
|
|
|
2022-11-25 20:42:16 +08:00
|
|
|
// Then check sub-modules
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
if self
|
|
|
|
.global_sub_modules
|
2022-11-25 23:03:20 +08:00
|
|
|
.as_deref()
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
|
|
|
.any(|(_, m)| m.contains_qualified_fn(hash))
|
2022-11-25 20:42:16 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
2022-03-28 12:53:52 +08:00
|
|
|
}
|
|
|
|
|
2022-11-25 20:42:16 +08:00
|
|
|
/// Optimize a block of [statements][Stmt] at top level.
|
|
|
|
///
|
|
|
|
/// Constants and variables from the scope are added.
|
|
|
|
fn optimize_top_level(
|
|
|
|
&self,
|
|
|
|
statements: StmtBlockContainer,
|
|
|
|
scope: &Scope,
|
2022-11-28 23:24:22 +08:00
|
|
|
lib: &[crate::SharedModule],
|
2022-11-25 20:42:16 +08:00
|
|
|
optimization_level: OptimizationLevel,
|
|
|
|
) -> StmtBlockContainer {
|
|
|
|
let mut statements = statements;
|
|
|
|
|
|
|
|
// If optimization level is None then skip optimizing
|
|
|
|
if optimization_level == OptimizationLevel::None {
|
|
|
|
statements.shrink_to_fit();
|
|
|
|
return statements;
|
2020-12-08 22:20:29 +08:00
|
|
|
}
|
2020-03-15 22:39:58 +08:00
|
|
|
|
2022-11-25 20:42:16 +08:00
|
|
|
// Set up the state
|
2022-11-28 23:24:22 +08:00
|
|
|
let mut state = OptimizerState::new(self, lib, optimization_level);
|
2020-03-15 22:39:58 +08:00
|
|
|
|
2022-11-25 20:42:16 +08:00
|
|
|
// Add constants from global modules
|
|
|
|
for (name, value) in self.global_modules.iter().rev().flat_map(|m| m.iter_var()) {
|
2022-12-20 16:52:55 +08:00
|
|
|
state.push_var(name, AccessMode::ReadOnly, Some(value.clone()));
|
2022-11-25 20:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add constants and variables from the scope
|
|
|
|
for (name, constant, value) in scope.iter() {
|
|
|
|
if constant {
|
2022-12-20 16:52:55 +08:00
|
|
|
state.push_var(name, AccessMode::ReadOnly, Some(value));
|
2022-11-25 20:42:16 +08:00
|
|
|
} else {
|
2022-12-20 16:52:55 +08:00
|
|
|
state.push_var(name, AccessMode::ReadWrite, None);
|
2022-01-28 18:59:18 +08:00
|
|
|
}
|
2022-11-25 20:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
optimize_stmt_block(statements, &mut state, true, false, true)
|
|
|
|
}
|
2020-06-05 15:14:42 +08:00
|
|
|
|
2022-11-25 20:42:16 +08:00
|
|
|
/// Optimize a collection of statements and functions into an [`AST`].
|
|
|
|
pub(crate) fn optimize_into_ast(
|
|
|
|
&self,
|
|
|
|
scope: &Scope,
|
|
|
|
statements: StmtBlockContainer,
|
|
|
|
#[cfg(not(feature = "no_function"))] functions: StaticVec<
|
|
|
|
crate::Shared<crate::ast::ScriptFnDef>,
|
|
|
|
>,
|
|
|
|
optimization_level: OptimizationLevel,
|
|
|
|
) -> AST {
|
|
|
|
let mut statements = statements;
|
2021-03-11 18:29:22 +08:00
|
|
|
|
2022-11-25 20:42:16 +08:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
let lib: crate::Shared<_> = {
|
|
|
|
let mut module = crate::Module::new();
|
|
|
|
|
2022-12-22 17:34:58 +08:00
|
|
|
if optimization_level == OptimizationLevel::None {
|
|
|
|
for fn_def in functions {
|
|
|
|
module.set_script_fn(fn_def);
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-25 20:42:16 +08:00
|
|
|
// We only need the script library's signatures for optimization purposes
|
|
|
|
let mut lib2 = crate::Module::new();
|
|
|
|
|
|
|
|
for fn_def in &functions {
|
|
|
|
lib2.set_script_fn(crate::ast::ScriptFnDef {
|
|
|
|
name: fn_def.name.clone(),
|
|
|
|
access: fn_def.access,
|
|
|
|
body: crate::ast::StmtBlock::NONE,
|
|
|
|
params: fn_def.params.clone(),
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[cfg(feature = "metadata")]
|
|
|
|
comments: Box::default(),
|
|
|
|
});
|
|
|
|
}
|
2021-03-12 14:11:08 +08:00
|
|
|
|
2022-11-25 20:42:16 +08:00
|
|
|
let lib2 = &[lib2.into()];
|
2021-03-10 22:12:48 +08:00
|
|
|
|
2022-11-25 20:42:16 +08:00
|
|
|
for fn_def in functions {
|
|
|
|
let mut fn_def = crate::func::shared_take_or_clone(fn_def);
|
2021-03-10 22:12:48 +08:00
|
|
|
|
2022-11-25 20:42:16 +08:00
|
|
|
// Optimize the function body
|
|
|
|
let body = mem::take(&mut *fn_def.body);
|
|
|
|
|
|
|
|
*fn_def.body = self.optimize_top_level(body, scope, lib2, optimization_level);
|
|
|
|
|
|
|
|
module.set_script_fn(fn_def);
|
|
|
|
}
|
2022-01-28 18:59:18 +08:00
|
|
|
}
|
2020-06-05 15:14:42 +08:00
|
|
|
|
2022-11-25 20:42:16 +08:00
|
|
|
module.into()
|
|
|
|
};
|
2022-11-28 23:24:22 +08:00
|
|
|
#[cfg(feature = "no_function")]
|
|
|
|
let lib: crate::Shared<_> = crate::Module::new().into();
|
2020-04-04 22:00:44 +08:00
|
|
|
|
2022-11-25 20:42:16 +08:00
|
|
|
statements.shrink_to_fit();
|
2020-11-15 13:49:54 +08:00
|
|
|
|
2022-11-25 20:42:16 +08:00
|
|
|
AST::new(
|
|
|
|
match optimization_level {
|
|
|
|
OptimizationLevel::None => statements,
|
2022-11-28 23:24:22 +08:00
|
|
|
OptimizationLevel::Simple | OptimizationLevel::Full => {
|
|
|
|
self.optimize_top_level(statements, scope, &[lib.clone()], optimization_level)
|
|
|
|
}
|
2022-11-25 20:42:16 +08:00
|
|
|
},
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
lib,
|
|
|
|
)
|
|
|
|
}
|
2020-03-15 22:39:58 +08:00
|
|
|
}
|