aboutsummaryrefslogtreecommitdiffstats
path: root/libevmasm
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2018-02-06 19:20:00 +0800
committerchriseth <chris@ethereum.org>2018-02-07 05:51:30 +0800
commitc961a3079dda8735363872cdb84c489d61846003 (patch)
tree8048fd3fbdd4f14031255344bfa1375a18d83e0f /libevmasm
parent5523296eaa68a591a331d9b75dc19cf11d1c538e (diff)
downloaddexon-solidity-c961a3079dda8735363872cdb84c489d61846003.tar.gz
dexon-solidity-c961a3079dda8735363872cdb84c489d61846003.tar.zst
dexon-solidity-c961a3079dda8735363872cdb84c489d61846003.zip
Turn simplification rule tuple into struct.
Diffstat (limited to 'libevmasm')
-rw-r--r--libevmasm/ExpressionClasses.cpp2
-rw-r--r--libevmasm/RuleList.h15
-rw-r--r--libevmasm/SimplificationRule.h45
-rw-r--r--libevmasm/SimplificationRules.cpp11
-rw-r--r--libevmasm/SimplificationRules.h9
5 files changed, 63 insertions, 19 deletions
diff --git a/libevmasm/ExpressionClasses.cpp b/libevmasm/ExpressionClasses.cpp
index 3825f5ed..69b33ec5 100644
--- a/libevmasm/ExpressionClasses.cpp
+++ b/libevmasm/ExpressionClasses.cpp
@@ -202,7 +202,7 @@ ExpressionClasses::Id ExpressionClasses::tryToSimplify(Expression const& _expr)
//cout << "with rule " << match->first.toString() << endl;
//ExpressionTemplate t(match->second());
//cout << "to " << match->second().toString() << endl;
- return rebuildExpression(ExpressionTemplate(std::get<1>(*match)(), _expr.item->location()));
+ return rebuildExpression(ExpressionTemplate(match->action(), _expr.item->location()));
}
return -1;
diff --git a/libevmasm/RuleList.h b/libevmasm/RuleList.h
index f8a6ce73..2312d673 100644
--- a/libevmasm/RuleList.h
+++ b/libevmasm/RuleList.h
@@ -25,6 +25,7 @@
#include <functional>
#include <libevmasm/Instruction.h>
+#include <libevmasm/SimplificationRule.h>
#include <libdevcore/CommonData.h>
@@ -45,12 +46,10 @@ template <class S> S modWorkaround(S const& _a, S const& _b)
/// @returns a list of simplification rules given certain match placeholders.
/// A, B and C should represent constants, X and Y arbitrary expressions.
-/// The third element in the tuple is a boolean flag that indicates whether
-/// any non-constant elements in the pattern are removed by applying it.
/// The simplifications should neven change the order of evaluation of
-/// arbitrary operations, though.
+/// arbitrary operations.
template <class Pattern>
-std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> simplificationRuleList(
+std::vector<SimplificationRule<Pattern>> simplificationRuleList(
Pattern A,
Pattern B,
Pattern C,
@@ -58,8 +57,8 @@ std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> simplificationR
Pattern Y
)
{
- std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> rules;
- rules += std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>>{
+ std::vector<SimplificationRule<Pattern>> rules;
+ rules += std::vector<SimplificationRule<Pattern>>{
// arithmetics on constants
{{Instruction::ADD, {A, B}}, [=]{ return A.d() + B.d(); }, false},
{{Instruction::MUL, {A, B}}, [=]{ return A.d() * B.d(); }, false},
@@ -196,7 +195,7 @@ std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> simplificationR
// xa can be (X, A) or (A, X)
for (auto xa: {std::vector<Pattern>{X, A}, std::vector<Pattern>{A, X}})
{
- rules += std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>>{{
+ rules += std::vector<SimplificationRule<Pattern>>{{
// (X+A)+B -> X+(A+B)
{op, {{op, xa}, B}},
[=]() -> Pattern { return {op, {X, fun(A.d(), B.d())}}; },
@@ -221,7 +220,7 @@ std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> simplificationR
}
// move constants across subtractions
- rules += std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>>{
+ rules += std::vector<SimplificationRule<Pattern>>{
{
// X - A -> X + (-A)
{Instruction::SUB, {X, A}},
diff --git a/libevmasm/SimplificationRule.h b/libevmasm/SimplificationRule.h
new file mode 100644
index 00000000..7b4dea68
--- /dev/null
+++ b/libevmasm/SimplificationRule.h
@@ -0,0 +1,45 @@
+/*
+ This file is part of solidity.
+
+ solidity is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ solidity is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with solidity. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * Expression simplification pattern.
+ */
+
+#pragma once
+
+#include <functional>
+
+namespace dev
+{
+namespace solidity
+{
+
+/**
+ * Rule that contains a pattern, an action that can be applied
+ * after the pattern has matched and a bool that indicates
+ * whether the action would remove something from the expression
+ * than is not a constant literal.
+ */
+template <class Pattern>
+struct SimplificationRule
+{
+ Pattern pattern;
+ std::function<Pattern()> action;
+ bool removesNonConstants;
+};
+
+}
+}
diff --git a/libevmasm/SimplificationRules.cpp b/libevmasm/SimplificationRules.cpp
index d81a993f..53a5f9fc 100644
--- a/libevmasm/SimplificationRules.cpp
+++ b/libevmasm/SimplificationRules.cpp
@@ -23,7 +23,6 @@
#include <libevmasm/ExpressionClasses.h>
#include <utility>
-#include <tuple>
#include <functional>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/noncopyable.hpp>
@@ -38,7 +37,7 @@ using namespace dev;
using namespace dev::eth;
-tuple<Pattern, function<Pattern()>, bool> const* Rules::findFirstMatch(
+SimplificationRule<Pattern> const* Rules::findFirstMatch(
Expression const& _expr,
ExpressionClasses const& _classes
)
@@ -48,22 +47,22 @@ tuple<Pattern, function<Pattern()>, bool> const* Rules::findFirstMatch(
assertThrow(_expr.item, OptimizerException, "");
for (auto const& rule: m_rules[byte(_expr.item->instruction())])
{
- if (std::get<0>(rule).matches(_expr, _classes))
+ if (rule.pattern.matches(_expr, _classes))
return &rule;
resetMatchGroups();
}
return nullptr;
}
-void Rules::addRules(std::vector<std::tuple<Pattern, std::function<Pattern ()>, bool>> const& _rules)
+void Rules::addRules(std::vector<SimplificationRule<Pattern>> const& _rules)
{
for (auto const& r: _rules)
addRule(r);
}
-void Rules::addRule(std::tuple<Pattern, std::function<Pattern()>, bool> const& _rule)
+void Rules::addRule(SimplificationRule<Pattern> const& _rule)
{
- m_rules[byte(std::get<0>(_rule).instruction())].push_back(_rule);
+ m_rules[byte(_rule.pattern.instruction())].push_back(_rule);
}
Rules::Rules()
diff --git a/libevmasm/SimplificationRules.h b/libevmasm/SimplificationRules.h
index 6040fd76..53f7e595 100644
--- a/libevmasm/SimplificationRules.h
+++ b/libevmasm/SimplificationRules.h
@@ -24,6 +24,7 @@
#pragma once
#include <libevmasm/ExpressionClasses.h>
+#include <libevmasm/SimplificationRule.h>
#include <functional>
#include <vector>
@@ -47,21 +48,21 @@ public:
/// @returns a pointer to the first matching pattern and sets the match
/// groups accordingly.
- std::tuple<Pattern, std::function<Pattern()>, bool> const* findFirstMatch(
+ SimplificationRule<Pattern> const* findFirstMatch(
Expression const& _expr,
ExpressionClasses const& _classes
);
private:
- void addRules(std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> const& _rules);
- void addRule(std::tuple<Pattern, std::function<Pattern()>, bool> const& _rule);
+ void addRules(std::vector<SimplificationRule<Pattern>> const& _rules);
+ void addRule(SimplificationRule<Pattern> const& _rule);
void resetMatchGroups() { m_matchGroups.clear(); }
std::map<unsigned, Expression const*> m_matchGroups;
/// Pattern to match, replacement to be applied and flag indicating whether
/// the replacement might remove some elements (except constants).
- std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> m_rules[256];
+ std::vector<SimplificationRule<Pattern>> m_rules[256];
};
/**