2017-05-23 17:11:14 +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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Common code generator for translating Julia / inline assembly to EVM and EVM1.5.
|
|
|
|
*/
|
|
|
|
|
2017-05-24 16:34:19 +00:00
|
|
|
#include <libjulia/backends/evm/EVMAssembly.h>
|
2017-05-23 17:21:14 +00:00
|
|
|
|
2017-12-01 21:58:05 +00:00
|
|
|
#include <libjulia/ASTDataForward.h>
|
2017-11-28 11:45:37 +00:00
|
|
|
|
2017-05-23 17:21:14 +00:00
|
|
|
#include <libsolidity/inlineasm/AsmScope.h>
|
|
|
|
|
|
|
|
#include <boost/variant.hpp>
|
2017-05-24 16:34:19 +00:00
|
|
|
#include <boost/optional.hpp>
|
2017-05-23 17:21:14 +00:00
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
class ErrorReporter;
|
2017-05-23 17:21:14 +00:00
|
|
|
namespace assembly
|
|
|
|
{
|
|
|
|
struct AsmAnalysisInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
namespace julia
|
|
|
|
{
|
2017-05-24 16:34:19 +00:00
|
|
|
class EVMAssembly;
|
2017-05-23 17:11:14 +00:00
|
|
|
|
|
|
|
class CodeTransform: public boost::static_visitor<>
|
|
|
|
{
|
|
|
|
public:
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Create the code transformer.
|
2017-05-23 17:11:14 +00:00
|
|
|
/// @param _identifierAccess used to resolve identifiers external to the inline assembly
|
2017-05-23 17:21:14 +00:00
|
|
|
CodeTransform(
|
2017-05-23 17:11:14 +00:00
|
|
|
julia::AbstractAssembly& _assembly,
|
2017-05-23 17:21:14 +00:00
|
|
|
solidity::assembly::AsmAnalysisInfo& _analysisInfo,
|
2017-06-09 13:38:23 +00:00
|
|
|
bool _julia = false,
|
2017-05-24 16:34:19 +00:00
|
|
|
bool _evm15 = false,
|
2017-08-25 15:04:31 +00:00
|
|
|
ExternalIdentifierAccess const& _identifierAccess = ExternalIdentifierAccess(),
|
|
|
|
bool _useNamedLabelsForFunctions = false
|
2017-06-13 19:58:25 +00:00
|
|
|
): CodeTransform(
|
|
|
|
_assembly,
|
|
|
|
_analysisInfo,
|
2017-06-09 13:38:23 +00:00
|
|
|
_julia,
|
2017-06-13 19:58:25 +00:00
|
|
|
_evm15,
|
|
|
|
_identifierAccess,
|
2017-08-25 15:04:31 +00:00
|
|
|
_useNamedLabelsForFunctions,
|
2017-06-13 19:58:25 +00:00
|
|
|
_assembly.stackHeight(),
|
|
|
|
std::make_shared<Context>()
|
|
|
|
)
|
2017-05-23 17:11:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-05-24 16:34:19 +00:00
|
|
|
protected:
|
2017-06-13 19:58:25 +00:00
|
|
|
struct Context
|
|
|
|
{
|
2017-06-13 22:01:06 +00:00
|
|
|
using Scope = solidity::assembly::Scope;
|
|
|
|
std::map<Scope::Label const*, AbstractAssembly::LabelID> labelIDs;
|
|
|
|
std::map<Scope::Function const*, AbstractAssembly::LabelID> functionEntryIDs;
|
|
|
|
std::map<Scope::Variable const*, int> variableStackHeights;
|
2017-06-13 19:58:25 +00:00
|
|
|
};
|
|
|
|
|
2017-05-23 17:11:14 +00:00
|
|
|
CodeTransform(
|
|
|
|
julia::AbstractAssembly& _assembly,
|
2017-05-23 17:21:14 +00:00
|
|
|
solidity::assembly::AsmAnalysisInfo& _analysisInfo,
|
2017-06-09 13:38:23 +00:00
|
|
|
bool _julia,
|
2017-05-24 16:34:19 +00:00
|
|
|
bool _evm15,
|
2017-05-23 17:21:14 +00:00
|
|
|
ExternalIdentifierAccess const& _identifierAccess,
|
2017-08-25 15:04:31 +00:00
|
|
|
bool _useNamedLabelsForFunctions,
|
2017-06-13 19:58:25 +00:00
|
|
|
int _stackAdjustment,
|
|
|
|
std::shared_ptr<Context> _context
|
2017-05-24 16:34:19 +00:00
|
|
|
):
|
|
|
|
m_assembly(_assembly),
|
|
|
|
m_info(_analysisInfo),
|
2017-06-09 13:38:23 +00:00
|
|
|
m_julia(_julia),
|
2017-05-24 16:34:19 +00:00
|
|
|
m_evm15(_evm15),
|
2017-08-25 15:04:31 +00:00
|
|
|
m_useNamedLabelsForFunctions(_useNamedLabelsForFunctions),
|
2017-05-24 16:34:19 +00:00
|
|
|
m_identifierAccess(_identifierAccess),
|
2017-06-13 19:58:25 +00:00
|
|
|
m_stackAdjustment(_stackAdjustment),
|
|
|
|
m_context(_context)
|
2017-05-24 16:34:19 +00:00
|
|
|
{}
|
2017-05-23 17:11:14 +00:00
|
|
|
|
|
|
|
public:
|
2017-11-28 11:45:37 +00:00
|
|
|
void operator()(Instruction const& _instruction);
|
|
|
|
void operator()(Literal const& _literal);
|
|
|
|
void operator()(Identifier const& _identifier);
|
|
|
|
void operator()(FunctionalInstruction const& _instr);
|
|
|
|
void operator()(FunctionCall const&);
|
2017-12-08 13:01:22 +00:00
|
|
|
void operator()(ExpressionStatement const& _statement);
|
2017-11-28 11:45:37 +00:00
|
|
|
void operator()(Label const& _label);
|
|
|
|
void operator()(StackAssignment const& _assignment);
|
|
|
|
void operator()(Assignment const& _assignment);
|
|
|
|
void operator()(VariableDeclaration const& _varDecl);
|
|
|
|
void operator()(If const& _if);
|
|
|
|
void operator()(Switch const& _switch);
|
|
|
|
void operator()(FunctionDefinition const&);
|
|
|
|
void operator()(ForLoop const&);
|
|
|
|
void operator()(Block const& _block);
|
2017-05-24 16:34:19 +00:00
|
|
|
|
2017-06-01 16:16:38 +00:00
|
|
|
private:
|
2017-11-28 11:45:37 +00:00
|
|
|
AbstractAssembly::LabelID labelFromIdentifier(Identifier const& _identifier);
|
2017-06-13 19:58:25 +00:00
|
|
|
/// @returns the label ID corresponding to the given label, allocating a new one if
|
|
|
|
/// necessary.
|
|
|
|
AbstractAssembly::LabelID labelID(solidity::assembly::Scope::Label const& _label);
|
2017-08-25 15:04:31 +00:00
|
|
|
AbstractAssembly::LabelID functionEntryID(std::string const& _name, solidity::assembly::Scope::Function const& _function);
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Generates code for an expression that is supposed to return a single value.
|
2017-12-08 13:01:22 +00:00
|
|
|
void visitExpression(Expression const& _expression);
|
2017-05-23 17:11:14 +00:00
|
|
|
|
2017-11-28 11:45:37 +00:00
|
|
|
void visitStatements(std::vector<Statement> const& _statements);
|
2017-06-14 13:35:51 +00:00
|
|
|
|
2017-06-09 11:59:16 +00:00
|
|
|
/// Pops all variables declared in the block and checks that the stack height is equal
|
|
|
|
/// to @a _blackStartStackHeight.
|
2017-11-28 11:45:37 +00:00
|
|
|
void finalizeBlock(Block const& _block, int _blockStartStackHeight);
|
2017-06-09 11:59:16 +00:00
|
|
|
|
2017-11-28 11:45:37 +00:00
|
|
|
void generateMultiAssignment(std::vector<Identifier> const& _variableNames);
|
|
|
|
void generateAssignment(Identifier const& _variableName);
|
2017-05-23 17:11:14 +00:00
|
|
|
|
2017-06-06 13:37:43 +00:00
|
|
|
/// Determines the stack height difference to the given variables. Throws
|
|
|
|
/// if it is not yet in scope or the height difference is too large. Returns
|
|
|
|
/// the (positive) stack height difference otherwise.
|
2017-09-20 07:52:10 +00:00
|
|
|
int variableHeightDiff(solidity::assembly::Scope::Variable const& _var, bool _forSwap) const;
|
2017-05-23 17:11:14 +00:00
|
|
|
|
2017-09-20 07:52:10 +00:00
|
|
|
void expectDeposit(int _deposit, int _oldHeight) const;
|
2017-05-23 17:11:14 +00:00
|
|
|
|
2017-09-20 07:52:10 +00:00
|
|
|
void checkStackHeight(void const* _astElement) const;
|
2017-05-23 17:11:14 +00:00
|
|
|
|
|
|
|
julia::AbstractAssembly& m_assembly;
|
2017-05-23 17:21:14 +00:00
|
|
|
solidity::assembly::AsmAnalysisInfo& m_info;
|
2017-05-24 16:34:19 +00:00
|
|
|
solidity::assembly::Scope* m_scope = nullptr;
|
2017-06-09 13:38:23 +00:00
|
|
|
bool m_julia = false;
|
2017-05-24 16:34:19 +00:00
|
|
|
bool m_evm15 = false;
|
2017-08-25 15:04:31 +00:00
|
|
|
bool m_useNamedLabelsForFunctions = false;
|
2017-05-23 17:11:14 +00:00
|
|
|
ExternalIdentifierAccess m_identifierAccess;
|
2017-05-31 10:15:43 +00:00
|
|
|
/// Adjustment between the stack height as determined during the analysis phase
|
|
|
|
/// and the stack height in the assembly. This is caused by an initial stack being present
|
|
|
|
/// for inline assembly and different stack heights depending on the EVM backend used
|
|
|
|
/// (EVM 1.0 or 1.5).
|
|
|
|
int m_stackAdjustment = 0;
|
2017-06-13 19:58:25 +00:00
|
|
|
std::shared_ptr<Context> m_context;
|
2017-05-23 17:11:14 +00:00
|
|
|
};
|
2017-05-23 17:21:14 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|