solidity/test/tools/yulInterpreter/Interpreter.h

206 lines
5.6 KiB
C
Raw Normal View History

2018-02-06 09:57:16 +00:00
/*
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/>.
*/
/**
* Yul interpreter.
*/
#pragma once
#include <libyul/AsmDataForward.h>
#include <libyul/optimiser/ASTWalker.h>
#include <libdevcore/FixedHash.h>
#include <libdevcore/CommonData.h>
#include <libdevcore/Exceptions.h>
#include <map>
namespace solidity::yul
2018-02-06 09:57:16 +00:00
{
struct Dialect;
}
namespace solidity::yul::test
2018-02-06 09:57:16 +00:00
{
class InterpreterTerminatedGeneric: public util::Exception
{
};
class ExplicitlyTerminated: public InterpreterTerminatedGeneric
{
};
class StepLimitReached: public InterpreterTerminatedGeneric
{
};
class TraceLimitReached: public InterpreterTerminatedGeneric
2018-02-06 09:57:16 +00:00
{
};
2019-10-28 14:25:02 +00:00
enum class ControlFlowState
{
Default,
Continue,
Break,
2019-10-28 14:25:02 +00:00
Leave
};
2018-02-06 09:57:16 +00:00
struct InterpreterState
{
bytes calldata;
bytes returndata;
std::map<u256, uint8_t> memory;
2018-02-06 09:57:16 +00:00
/// This is different than memory.size() because we ignore gas.
u256 msize;
std::map<util::h256, util::h256> storage;
u160 address = 0x11111111;
u256 balance = 0x22222222;
u256 selfbalance = 0x22223333;
u160 origin = 0x33333333;
u160 caller = 0x44444444;
u256 callvalue = 0x55555555;
2018-02-06 09:57:16 +00:00
/// Deployed code
bytes code = util::asBytes("codecodecodecodecode");
u256 gasprice = 0x66666666;
u160 coinbase = 0x77777777;
u256 timestamp = 0x88888888;
u256 blockNumber = 1024;
u256 difficulty = 0x9999999;
u256 gaslimit = 4000000;
u256 chainid = 0x01;
2018-02-06 09:57:16 +00:00
/// Log of changes / effects. Sholud be structured data in the future.
std::vector<std::string> trace;
/// This is actually an input parameter that more or less limits the runtime.
size_t maxTraceSize = 0;
size_t maxSteps = 0;
size_t numSteps = 0;
2019-10-28 14:25:02 +00:00
ControlFlowState controlFlowState = ControlFlowState::Default;
void dumpTraceAndState(std::ostream& _out) const;
2018-02-06 09:57:16 +00:00
};
/**
* Yul interpreter.
*/
class Interpreter: public ASTWalker
{
public:
Interpreter(
InterpreterState& _state,
Dialect const& _dialect,
std::map<YulString, u256> _variables = {},
std::vector<std::map<YulString, FunctionDefinition const*>> _scopes = {}
2018-02-06 09:57:16 +00:00
):
m_dialect(_dialect),
2018-02-06 09:57:16 +00:00
m_state(_state),
m_variables(std::move(_variables)),
m_scopes(std::move(_scopes))
2018-02-06 09:57:16 +00:00
{}
2019-02-25 10:46:53 +00:00
void operator()(ExpressionStatement const& _statement) override;
void operator()(Assignment const& _assignment) override;
void operator()(VariableDeclaration const& _varDecl) override;
void operator()(If const& _if) override;
void operator()(Switch const& _switch) override;
void operator()(FunctionDefinition const&) override;
void operator()(ForLoop const&) override;
void operator()(Break const&) override;
void operator()(Continue const&) override;
2019-10-28 14:25:02 +00:00
void operator()(Leave const&) override;
2019-02-25 10:46:53 +00:00
void operator()(Block const& _block) override;
2018-02-06 09:57:16 +00:00
std::vector<std::string> const& trace() const { return m_state.trace; }
u256 valueOfVariable(YulString _name) const { return m_variables.at(_name); }
2018-02-06 09:57:16 +00:00
private:
/// Asserts that the expression evaluates to exactly one value and returns it.
u256 evaluate(Expression const& _expression);
2018-02-06 09:57:16 +00:00
/// Evaluates the expression and returns its value.
std::vector<u256> evaluateMulti(Expression const& _expression);
2018-02-06 09:57:16 +00:00
void openScope() { m_scopes.push_back({}); }
/// Unregisters variables and functions.
2018-02-06 09:57:16 +00:00
void closeScope();
Dialect const& m_dialect;
2018-02-06 09:57:16 +00:00
InterpreterState& m_state;
/// Values of variables.
std::map<YulString, u256> m_variables;
/// Scopes of variables and functions. Used for lookup, clearing at end of blocks
/// and passing over the visible functions across function calls.
/// The pointer is nullptr if and only if the key is a variable.
std::vector<std::map<YulString, FunctionDefinition const*>> m_scopes;
2018-02-06 09:57:16 +00:00
};
/**
* Yul expression evaluator.
*/
class ExpressionEvaluator: public ASTWalker
{
public:
ExpressionEvaluator(
InterpreterState& _state,
Dialect const& _dialect,
std::map<YulString, u256> const& _variables,
std::vector<std::map<YulString, FunctionDefinition const*>> const& _scopes
2018-02-06 09:57:16 +00:00
):
m_state(_state),
m_dialect(_dialect),
2018-02-06 09:57:16 +00:00
m_variables(_variables),
m_scopes(_scopes)
2018-02-06 09:57:16 +00:00
{}
2019-02-25 10:46:53 +00:00
void operator()(Literal const&) override;
void operator()(Identifier const&) override;
void operator()(FunctionCall const& _funCall) override;
2018-02-06 09:57:16 +00:00
/// Asserts that the expression has exactly one value and returns it.
u256 value() const;
2018-02-06 09:57:16 +00:00
/// Returns the list of values of the expression.
std::vector<u256> values() const { return m_values; }
2018-02-06 09:57:16 +00:00
private:
void setValue(u256 _value);
2018-02-06 09:57:16 +00:00
/// Evaluates the given expression from right to left and
/// stores it in m_value.
void evaluateArgs(std::vector<Expression> const& _expr);
/// Finds the function called @a _functionName in the current scope stack and returns
/// the function's scope stack (with variables removed) and definition.
std::pair<
std::vector<std::map<YulString, FunctionDefinition const*>>,
FunctionDefinition const*
> findFunctionAndScope(YulString _functionName) const;
2018-02-06 09:57:16 +00:00
InterpreterState& m_state;
Dialect const& m_dialect;
2018-02-06 09:57:16 +00:00
/// Values of variables.
std::map<YulString, u256> const& m_variables;
/// Stack of scopes in the current context.
std::vector<std::map<YulString, FunctionDefinition const*>> const& m_scopes;
2018-02-06 09:57:16 +00:00
/// Current value of the expression
std::vector<u256> m_values;
2018-02-06 09:57:16 +00:00
};
}