aboutsummaryrefslogtreecommitdiffstats
path: root/libevmasm/ConstantOptimiser.cpp
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2017-04-03 20:40:39 +0800
committerAlex Beregszaszi <alex@rtfs.hu>2017-04-27 21:06:28 +0800
commit54210ea89f1f115ee9dba9008ff5ea8ddb74c839 (patch)
tree6e8f60f6d35886abc409e107a4f0907ca841d45b /libevmasm/ConstantOptimiser.cpp
parent2c1fb46bc341d9e44074af23cd4eadd3a9f732c5 (diff)
downloaddexon-solidity-54210ea89f1f115ee9dba9008ff5ea8ddb74c839.tar.gz
dexon-solidity-54210ea89f1f115ee9dba9008ff5ea8ddb74c839.tar.zst
dexon-solidity-54210ea89f1f115ee9dba9008ff5ea8ddb74c839.zip
Add recomputation check for number representation.
Diffstat (limited to 'libevmasm/ConstantOptimiser.cpp')
-rw-r--r--libevmasm/ConstantOptimiser.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/libevmasm/ConstantOptimiser.cpp b/libevmasm/ConstantOptimiser.cpp
index a1dfd21c..c1d61b88 100644
--- a/libevmasm/ConstantOptimiser.cpp
+++ b/libevmasm/ConstantOptimiser.cpp
@@ -232,6 +232,54 @@ AssemblyItems ComputeMethod::findRepresentation(u256 const& _value)
}
}
+bool ComputeMethod::checkRepresentation(u256 const& _value)
+{
+ // This is a tiny EVM that can only evaluate some instructions.
+ vector<u256> stack;
+ for (AssemblyItem const& item: m_routine)
+ {
+ switch (item.type())
+ {
+ case Operation:
+ {
+ if (stack.size() < size_t(item.arguments()))
+ return false;
+ u256* sp = &stack.back();
+ switch (item.instruction())
+ {
+ case Instruction::MUL:
+ sp[-1] = sp[0] * sp[-1];
+ break;
+ case Instruction::EXP:
+ if (sp[-1] > 0xff)
+ return false;
+ sp[-1] = boost::multiprecision::pow(sp[0], unsigned(sp[-1]));
+ break;
+ case Instruction::ADD:
+ sp[-1] = sp[0] + sp[-1];
+ break;
+ case Instruction::SUB:
+ sp[-1] = sp[0] - sp[-1];
+ break;
+ case Instruction::NOT:
+ sp[0] = ~sp[0];
+ break;
+ default:
+ return false;
+ }
+ stack.resize(stack.size() + item.deposit());
+ break;
+ }
+ case Push:
+ stack.push_back(item.data());
+ break;
+ default:
+ return false;
+ }
+ }
+ return stack.size() == 1 && stack.front() == _value;
+}
+
bigint ComputeMethod::gasNeeded(AssemblyItems const& _routine)
{
size_t numExps = count(_routine.begin(), _routine.end(), Instruction::EXP);