mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Initial EVM1.5 assembly implementation.
This commit is contained in:
@@ -41,6 +41,9 @@ struct Identifier;
|
||||
namespace julia
|
||||
{
|
||||
|
||||
///
|
||||
/// Assembly class that abstracts both the libevmasm assembly and the new julia evm assembly.
|
||||
///
|
||||
class AbstractAssembly
|
||||
{
|
||||
public:
|
||||
@@ -66,6 +69,21 @@ public:
|
||||
/// Append a reference to a to-be-linked symobl.
|
||||
/// Currently, we assume that the value is always a 20 byte number.
|
||||
virtual void appendLinkerSymbol(std::string const& _name) = 0;
|
||||
|
||||
/// Append a jump instruction.
|
||||
/// @param _stackDiffAfter the stack adjustment after this instruction.
|
||||
virtual void appendJump(int _stackDiffAfter) = 0;
|
||||
|
||||
/// Append a jump-to-immediate operation.
|
||||
virtual void appendJumpTo(LabelID _label, int _stackDiffAfter = 0) = 0;
|
||||
/// Append a jump-to-if-immediate operation.
|
||||
virtual void appendJumpToIf(LabelID _label) = 0;
|
||||
/// Start a subroutine.
|
||||
virtual void appendBeginsub(LabelID _label, int _arguments) = 0;
|
||||
/// Call a subroutine.
|
||||
virtual void appendJumpsub(LabelID _label, int _arguments, int _returns) = 0;
|
||||
/// Return from a subroutine.
|
||||
virtual void appendReturnsub(int _returns) = 0;
|
||||
};
|
||||
|
||||
enum class IdentifierContext { LValue, RValue };
|
||||
@@ -74,7 +92,7 @@ enum class IdentifierContext { LValue, RValue };
|
||||
/// to inline assembly (not used in standalone assembly mode).
|
||||
struct ExternalIdentifierAccess
|
||||
{
|
||||
using Resolver = std::function<size_t(solidity::assembly::Identifier const&, IdentifierContext)>;
|
||||
using Resolver = std::function<size_t(solidity::assembly::Identifier const&, IdentifierContext, bool /*_crossesFunctionBoundary*/)>;
|
||||
/// Resolve a an external reference given by the identifier in the given context.
|
||||
/// @returns the size of the value (number of stack slots) or size_t(-1) if not found.
|
||||
Resolver resolve;
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
/**
|
||||
* Assembly interface for EVM and EVM1.5.
|
||||
*/
|
||||
|
||||
#include <libjulia/backends/evm/EVMAssembly.h>
|
||||
|
||||
#include <libevmasm/Instruction.h>
|
||||
|
||||
#include <libsolidity/interface/Utils.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace dev;
|
||||
using namespace julia;
|
||||
|
||||
namespace
|
||||
{
|
||||
size_t constexpr labelReferenceSize = 4;
|
||||
}
|
||||
|
||||
|
||||
void EVMAssembly::setSourceLocation(SourceLocation const&)
|
||||
{
|
||||
// Ignored for now;
|
||||
}
|
||||
|
||||
void EVMAssembly::appendInstruction(solidity::Instruction _instr)
|
||||
{
|
||||
m_bytecode.push_back(byte(_instr));
|
||||
m_stackHeight += solidity::instructionInfo(_instr).ret - solidity::instructionInfo(_instr).args;
|
||||
}
|
||||
|
||||
void EVMAssembly::appendConstant(u256 const& _constant)
|
||||
{
|
||||
bytes data = toCompactBigEndian(_constant, 1);
|
||||
appendInstruction(solidity::pushInstruction(data.size()));
|
||||
m_bytecode += data;
|
||||
}
|
||||
|
||||
void EVMAssembly::appendLabel(LabelID _labelId)
|
||||
{
|
||||
setLabelToCurrentPosition(_labelId);
|
||||
appendInstruction(solidity::Instruction::JUMPDEST);
|
||||
}
|
||||
|
||||
void EVMAssembly::appendLabelReference(LabelID _labelId)
|
||||
{
|
||||
solAssert(!m_evm15, "Cannot use plain label references in EMV1.5 mode.");
|
||||
// @TODO we now always use labelReferenceSize for all labels, it could be shortened
|
||||
// for some of them.
|
||||
appendInstruction(solidity::pushInstruction(labelReferenceSize));
|
||||
m_labelReferences[m_bytecode.size()] = _labelId;
|
||||
m_bytecode += bytes(labelReferenceSize);
|
||||
}
|
||||
|
||||
EVMAssembly::LabelID EVMAssembly::newLabelId()
|
||||
{
|
||||
m_labelPositions[m_nextLabelID] = size_t(-1);
|
||||
return m_nextLabelID++;
|
||||
}
|
||||
|
||||
void EVMAssembly::appendLinkerSymbol(string const&)
|
||||
{
|
||||
solAssert(false, "Linker symbols not yet implemented.");
|
||||
}
|
||||
|
||||
void EVMAssembly::appendJump(int _stackDiffAfter)
|
||||
{
|
||||
solAssert(!m_evm15, "Plain JUMP used for EVM 1.5");
|
||||
appendInstruction(solidity::Instruction::JUMP);
|
||||
m_stackHeight += _stackDiffAfter;
|
||||
}
|
||||
|
||||
void EVMAssembly::appendJumpTo(AbstractAssembly::LabelID _labelId, int _stackDiffAfter)
|
||||
{
|
||||
if (m_evm15)
|
||||
{
|
||||
m_bytecode.push_back(byte(solidity::Instruction::JUMPTO));
|
||||
appendLabelReferenceInternal(_labelId);
|
||||
m_stackHeight += _stackDiffAfter;
|
||||
}
|
||||
else
|
||||
{
|
||||
appendLabelReference(_labelId);
|
||||
appendJump(_stackDiffAfter);
|
||||
}
|
||||
}
|
||||
|
||||
void EVMAssembly::appendJumpToIf(AbstractAssembly::LabelID _labelId)
|
||||
{
|
||||
if (m_evm15)
|
||||
{
|
||||
m_bytecode.push_back(byte(solidity::Instruction::JUMPIF));
|
||||
appendLabelReferenceInternal(_labelId);
|
||||
m_stackHeight--;
|
||||
}
|
||||
else
|
||||
{
|
||||
appendLabelReference(_labelId);
|
||||
appendInstruction(solidity::Instruction::JUMPI);
|
||||
}
|
||||
}
|
||||
|
||||
void EVMAssembly::appendBeginsub(AbstractAssembly::LabelID _labelId, int _arguments)
|
||||
{
|
||||
solAssert(m_evm15, "BEGINSUB used for EVM 1.0");
|
||||
solAssert(_arguments >= 0, "");
|
||||
setLabelToCurrentPosition(_labelId);
|
||||
m_bytecode.push_back(byte(solidity::Instruction::BEGINSUB));
|
||||
m_stackHeight += _arguments;
|
||||
}
|
||||
|
||||
void EVMAssembly::appendJumpsub(AbstractAssembly::LabelID _labelId, int _arguments, int _returns)
|
||||
{
|
||||
solAssert(m_evm15, "JUMPSUB used for EVM 1.0");
|
||||
solAssert(_arguments >= 0 && _returns >= 0, "");
|
||||
m_bytecode.push_back(byte(solidity::Instruction::JUMPSUB));
|
||||
appendLabelReferenceInternal(_labelId);
|
||||
m_stackHeight += _returns - _arguments;
|
||||
}
|
||||
|
||||
void EVMAssembly::appendReturnsub(int _returns)
|
||||
{
|
||||
solAssert(m_evm15, "RETURNSUB used for EVM 1.0");
|
||||
solAssert(_returns >= 0, "");
|
||||
m_bytecode.push_back(byte(solidity::Instruction::RETURNSUB));
|
||||
m_stackHeight -= _returns;
|
||||
}
|
||||
|
||||
eth::LinkerObject EVMAssembly::finalize()
|
||||
{
|
||||
for (auto const& ref: m_labelReferences)
|
||||
{
|
||||
size_t referencePos = ref.first;
|
||||
solAssert(m_labelPositions.count(ref.second), "");
|
||||
size_t labelPos = m_labelPositions.at(ref.second);
|
||||
solAssert(labelPos != size_t(-1), "Undefined but allocated label used.");
|
||||
solAssert(m_bytecode.size() >= 4 && referencePos <= m_bytecode.size() - 4, "");
|
||||
solAssert(labelPos < (uint64_t(1) << (8 * labelReferenceSize)), "");
|
||||
for (size_t i = 0; i < labelReferenceSize; i++)
|
||||
m_bytecode[referencePos + i] = byte((labelPos >> (8 * (labelReferenceSize - i - 1))) & 0xff);
|
||||
}
|
||||
eth::LinkerObject obj;
|
||||
obj.bytecode = m_bytecode;
|
||||
return obj;
|
||||
}
|
||||
|
||||
void EVMAssembly::setLabelToCurrentPosition(AbstractAssembly::LabelID _labelId)
|
||||
{
|
||||
solAssert(m_labelPositions.count(_labelId), "Label not found.");
|
||||
solAssert(m_labelPositions[_labelId] == size_t(-1), "Label already set.");
|
||||
m_labelPositions[_labelId] = m_bytecode.size();
|
||||
}
|
||||
|
||||
void EVMAssembly::appendLabelReferenceInternal(AbstractAssembly::LabelID _labelId)
|
||||
{
|
||||
m_labelReferences[m_bytecode.size()] = _labelId;
|
||||
m_bytecode += bytes(labelReferenceSize);
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
/**
|
||||
* Assembly interface for EVM and EVM1.5.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libjulia/backends/evm/AbstractAssembly.h>
|
||||
|
||||
#include <libevmasm/LinkerObject.h>
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace dev
|
||||
{
|
||||
namespace julia
|
||||
{
|
||||
|
||||
class EVMAssembly: public AbstractAssembly
|
||||
{
|
||||
public:
|
||||
explicit EVMAssembly(bool _evm15 = false): m_evm15(_evm15) { }
|
||||
virtual ~EVMAssembly() {}
|
||||
|
||||
/// Set a new source location valid starting from the next instruction.
|
||||
virtual void setSourceLocation(SourceLocation const& _location) override;
|
||||
/// Retrieve the current height of the stack. This does not have to be zero
|
||||
/// at the beginning.
|
||||
virtual int stackHeight() const override { return m_stackHeight; }
|
||||
/// Append an EVM instruction.
|
||||
virtual void appendInstruction(solidity::Instruction _instruction) override;
|
||||
/// Append a constant.
|
||||
virtual void appendConstant(u256 const& _constant) override;
|
||||
/// Append a label.
|
||||
virtual void appendLabel(LabelID _labelId) override;
|
||||
/// Append a label reference.
|
||||
virtual void appendLabelReference(LabelID _labelId) override;
|
||||
/// Generate a new unique label.
|
||||
virtual LabelID newLabelId() override;
|
||||
/// Append a reference to a to-be-linked symobl.
|
||||
/// Currently, we assume that the value is always a 20 byte number.
|
||||
virtual void appendLinkerSymbol(std::string const& _name) override;
|
||||
|
||||
/// Append a jump instruction.
|
||||
/// @param _stackDiffAfter the stack adjustment after this instruction.
|
||||
virtual void appendJump(int _stackDiffAfter) override;
|
||||
/// Append a jump-to-immediate operation.
|
||||
virtual void appendJumpTo(LabelID _label, int _stackDiffAfter) override;
|
||||
/// Append a jump-to-if-immediate operation.
|
||||
virtual void appendJumpToIf(LabelID _label) override;
|
||||
/// Start a subroutine.
|
||||
virtual void appendBeginsub(LabelID _label, int _arguments) override;
|
||||
/// Call a subroutine.
|
||||
virtual void appendJumpsub(LabelID _label, int _arguments, int _returns) override;
|
||||
/// Return from a subroutine.
|
||||
virtual void appendReturnsub(int _returns) override;
|
||||
|
||||
|
||||
/// Resolves references inside the bytecode and returns the linker object.
|
||||
eth::LinkerObject finalize();
|
||||
|
||||
private:
|
||||
void setLabelToCurrentPosition(AbstractAssembly::LabelID _labelId);
|
||||
void appendLabelReferenceInternal(AbstractAssembly::LabelID _labelId);
|
||||
|
||||
bool m_evm15 = false; ///< if true, switch to evm1.5 mode
|
||||
LabelID m_nextLabelID = 0;
|
||||
int m_stackHeight = 0;
|
||||
bytes m_bytecode;
|
||||
std::map<LabelID, size_t> m_labelPositions;
|
||||
std::map<size_t, LabelID> m_labelReferences;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
@@ -25,49 +25,359 @@
|
||||
|
||||
#include <libsolidity/interface/Utils.h>
|
||||
|
||||
#include <boost/range/adaptor/reversed.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace dev;
|
||||
using namespace dev::julia;
|
||||
using namespace dev::solidity;
|
||||
using namespace dev::solidity::assembly;
|
||||
|
||||
CodeTransform::CodeTransform(
|
||||
ErrorReporter& _errorReporter,
|
||||
AbstractAssembly& _assembly,
|
||||
Block const& _block,
|
||||
AsmAnalysisInfo& _analysisInfo,
|
||||
ExternalIdentifierAccess const& _identifierAccess,
|
||||
int _initialStackHeight
|
||||
):
|
||||
m_errorReporter(_errorReporter),
|
||||
m_assembly(_assembly),
|
||||
m_info(_analysisInfo),
|
||||
m_scope(*_analysisInfo.scopes.at(&_block)),
|
||||
m_identifierAccess(_identifierAccess),
|
||||
m_initialStackHeight(_initialStackHeight)
|
||||
void CodeTransform::run(Block const& _block)
|
||||
{
|
||||
m_scope = m_info.scopes.at(&_block).get();
|
||||
|
||||
int blockStartStackHeight = m_assembly.stackHeight();
|
||||
std::for_each(_block.statements.begin(), _block.statements.end(), boost::apply_visitor(*this));
|
||||
|
||||
m_assembly.setSourceLocation(_block.location);
|
||||
|
||||
// pop variables
|
||||
for (auto const& identifier: m_scope.identifiers)
|
||||
for (auto const& identifier: m_scope->identifiers)
|
||||
if (identifier.second.type() == typeid(Scope::Variable))
|
||||
m_assembly.appendInstruction(solidity::Instruction::POP);
|
||||
|
||||
int deposit = m_assembly.stackHeight() - blockStartStackHeight;
|
||||
solAssert(deposit == 0, "Invalid stack height at end of block.");
|
||||
checkStackHeight(&_block);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(const FunctionDefinition&)
|
||||
|
||||
void CodeTransform::operator()(VariableDeclaration const& _varDecl)
|
||||
{
|
||||
solAssert(false, "Function definition not removed during desugaring phase.");
|
||||
solAssert(m_scope, "");
|
||||
|
||||
int expectedItems = _varDecl.variables.size();
|
||||
int height = m_assembly.stackHeight();
|
||||
boost::apply_visitor(*this, *_varDecl.value);
|
||||
expectDeposit(expectedItems, height);
|
||||
for (auto const& variable: _varDecl.variables)
|
||||
{
|
||||
auto& var = boost::get<Scope::Variable>(m_scope->identifiers.at(variable.name));
|
||||
var.stackHeight = height++;
|
||||
var.active = true;
|
||||
}
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(Assignment const& _assignment)
|
||||
{
|
||||
visitExpression(*_assignment.value);
|
||||
m_assembly.setSourceLocation(_assignment.location);
|
||||
generateAssignment(_assignment.variableName, _assignment.location);
|
||||
checkStackHeight(&_assignment);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(StackAssignment const& _assignment)
|
||||
{
|
||||
m_assembly.setSourceLocation(_assignment.location);
|
||||
generateAssignment(_assignment.variableName, _assignment.location);
|
||||
checkStackHeight(&_assignment);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(Label const& _label)
|
||||
{
|
||||
m_assembly.setSourceLocation(_label.location);
|
||||
solAssert(m_scope, "");
|
||||
solAssert(m_scope->identifiers.count(_label.name), "");
|
||||
Scope::Label& label = boost::get<Scope::Label>(m_scope->identifiers.at(_label.name));
|
||||
assignLabelIdIfUnset(label.id);
|
||||
m_assembly.appendLabel(*label.id);
|
||||
checkStackHeight(&_label);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(FunctionCall const& _call)
|
||||
{
|
||||
solAssert(m_scope, "");
|
||||
|
||||
m_assembly.setSourceLocation(_call.location);
|
||||
EVMAssembly::LabelID returnLabel(-1); // only used for evm 1.0
|
||||
if (!m_evm15)
|
||||
{
|
||||
returnLabel = m_assembly.newLabelId();
|
||||
m_assembly.appendLabelReference(returnLabel);
|
||||
}
|
||||
|
||||
Scope::Function* function = nullptr;
|
||||
solAssert(m_scope->lookup(_call.functionName.name, Scope::NonconstVisitor(
|
||||
[=](Scope::Variable&) { solAssert(false, "Expected function name."); },
|
||||
[=](Scope::Label&) { solAssert(false, "Expected function name."); },
|
||||
[&](Scope::Function& _function) { function = &_function; }
|
||||
)), "Function name not found.");
|
||||
solAssert(function, "");
|
||||
solAssert(function->arguments.size() == _call.arguments.size(), "");
|
||||
for (auto const& arg: _call.arguments | boost::adaptors::reversed)
|
||||
visitExpression(arg);
|
||||
m_assembly.setSourceLocation(_call.location);
|
||||
assignLabelIdIfUnset(function->id);
|
||||
if (m_evm15)
|
||||
m_assembly.appendJumpsub(*function->id, function->arguments.size(), function->returns.size());
|
||||
else
|
||||
{
|
||||
m_assembly.appendJumpTo(*function->id, function->returns.size() - function->arguments.size() - 1);
|
||||
m_assembly.appendLabel(returnLabel);
|
||||
}
|
||||
checkStackHeight(&_call);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(FunctionalInstruction const& _instruction)
|
||||
{
|
||||
if (m_evm15 && (
|
||||
_instruction.instruction.instruction == solidity::Instruction::JUMP ||
|
||||
_instruction.instruction.instruction == solidity::Instruction::JUMPI
|
||||
))
|
||||
{
|
||||
bool const isJumpI = _instruction.instruction.instruction == solidity::Instruction::JUMPI;
|
||||
if (isJumpI)
|
||||
{
|
||||
solAssert(_instruction.arguments.size() == 2, "");
|
||||
visitExpression(_instruction.arguments.at(1));
|
||||
}
|
||||
else
|
||||
{
|
||||
solAssert(_instruction.arguments.size() == 1, "");
|
||||
}
|
||||
m_assembly.setSourceLocation(_instruction.location);
|
||||
auto label = labelFromIdentifier(boost::get<assembly::Identifier>(_instruction.arguments.at(0)));
|
||||
if (isJumpI)
|
||||
m_assembly.appendJumpToIf(label);
|
||||
else
|
||||
m_assembly.appendJumpTo(label);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (auto const& arg: _instruction.arguments | boost::adaptors::reversed)
|
||||
visitExpression(arg);
|
||||
(*this)(_instruction.instruction);
|
||||
}
|
||||
checkStackHeight(&_instruction);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(assembly::Identifier const& _identifier)
|
||||
{
|
||||
m_assembly.setSourceLocation(_identifier.location);
|
||||
// First search internals, then externals.
|
||||
solAssert(m_scope, "");
|
||||
if (m_scope->lookup(_identifier.name, Scope::NonconstVisitor(
|
||||
[=](Scope::Variable& _var)
|
||||
{
|
||||
if (int heightDiff = variableHeightDiff(_var, _identifier.location, false))
|
||||
m_assembly.appendInstruction(solidity::dupInstruction(heightDiff));
|
||||
else
|
||||
// Store something to balance the stack
|
||||
m_assembly.appendConstant(u256(0));
|
||||
},
|
||||
[=](Scope::Label& _label)
|
||||
{
|
||||
assignLabelIdIfUnset(_label.id);
|
||||
m_assembly.appendLabelReference(*_label.id);
|
||||
},
|
||||
[=](Scope::Function&)
|
||||
{
|
||||
solAssert(false, "Function not removed during desugaring.");
|
||||
}
|
||||
)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
solAssert(
|
||||
m_identifierAccess.generateCode,
|
||||
"Identifier not found and no external access available."
|
||||
);
|
||||
m_identifierAccess.generateCode(_identifier, IdentifierContext::RValue, m_assembly);
|
||||
checkStackHeight(&_identifier);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(assembly::Literal const& _literal)
|
||||
{
|
||||
m_assembly.setSourceLocation(_literal.location);
|
||||
if (_literal.kind == assembly::LiteralKind::Number)
|
||||
m_assembly.appendConstant(u256(_literal.value));
|
||||
else if (_literal.kind == assembly::LiteralKind::Boolean)
|
||||
{
|
||||
if (_literal.value == "true")
|
||||
m_assembly.appendConstant(u256(1));
|
||||
else
|
||||
m_assembly.appendConstant(u256(0));
|
||||
}
|
||||
else
|
||||
{
|
||||
solAssert(_literal.value.size() <= 32, "");
|
||||
m_assembly.appendConstant(u256(h256(_literal.value, h256::FromBinary, h256::AlignLeft)));
|
||||
}
|
||||
checkStackHeight(&_literal);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(assembly::Instruction const& _instruction)
|
||||
{
|
||||
solAssert(!m_evm15 || _instruction.instruction != solidity::Instruction::JUMP, "Bare JUMP instruction used for EVM1.5");
|
||||
solAssert(!m_evm15 || _instruction.instruction != solidity::Instruction::JUMPI, "Bare JUMPI instruction used for EVM1.5");
|
||||
m_assembly.setSourceLocation(_instruction.location);
|
||||
m_assembly.appendInstruction(_instruction.instruction);
|
||||
checkStackHeight(&_instruction);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(Switch const& _switch)
|
||||
{
|
||||
//@TODO use JUMPV in EVM1.5?
|
||||
|
||||
visitExpression(*_switch.expression);
|
||||
int expressionHeight = m_assembly.stackHeight();
|
||||
map<Case const*, AbstractAssembly::LabelID> caseBodies;
|
||||
AbstractAssembly::LabelID end = m_assembly.newLabelId();
|
||||
for (Case const& c: _switch.cases)
|
||||
{
|
||||
if (c.value)
|
||||
{
|
||||
(*this)(*c.value);
|
||||
m_assembly.setSourceLocation(c.location);
|
||||
AbstractAssembly::LabelID bodyLabel = m_assembly.newLabelId();
|
||||
caseBodies[&c] = bodyLabel;
|
||||
solAssert(m_assembly.stackHeight() == expressionHeight + 1, "");
|
||||
m_assembly.appendInstruction(solidity::dupInstruction(2));
|
||||
m_assembly.appendInstruction(solidity::Instruction::EQ);
|
||||
m_assembly.appendJumpToIf(bodyLabel);
|
||||
}
|
||||
else
|
||||
// default case
|
||||
(*this)(c.body);
|
||||
}
|
||||
m_assembly.setSourceLocation(_switch.location);
|
||||
m_assembly.appendJumpTo(end);
|
||||
|
||||
size_t numCases = caseBodies.size();
|
||||
for (auto const& c: caseBodies)
|
||||
{
|
||||
m_assembly.setSourceLocation(c.first->location);
|
||||
m_assembly.appendLabel(c.second);
|
||||
(*this)(c.first->body);
|
||||
if (--numCases > 0)
|
||||
{
|
||||
m_assembly.setSourceLocation(c.first->location);
|
||||
m_assembly.appendJumpTo(end);
|
||||
}
|
||||
}
|
||||
|
||||
m_assembly.setSourceLocation(_switch.location);
|
||||
m_assembly.appendLabel(end);
|
||||
m_assembly.appendInstruction(solidity::Instruction::POP);
|
||||
checkStackHeight(&_switch);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(FunctionDefinition const& _function)
|
||||
{
|
||||
solAssert(m_scope, "");
|
||||
solAssert(m_scope->identifiers.count(_function.name), "");
|
||||
Scope::Function& function = boost::get<Scope::Function>(m_scope->identifiers.at(_function.name));
|
||||
assignLabelIdIfUnset(function.id);
|
||||
|
||||
int height = m_evm15 ? 0 : 1;
|
||||
solAssert(m_info.scopes.at(&_function.body), "");
|
||||
Scope* varScope = m_info.scopes.at(m_info.virtualBlocks.at(&_function).get()).get();
|
||||
solAssert(varScope, "");
|
||||
for (auto const& v: _function.arguments | boost::adaptors::reversed)
|
||||
{
|
||||
auto& var = boost::get<Scope::Variable>(varScope->identifiers.at(v.name));
|
||||
var.stackHeight = height++;
|
||||
var.active = true;
|
||||
}
|
||||
|
||||
m_assembly.setSourceLocation(_function.location);
|
||||
int stackHeightBefore = m_assembly.stackHeight();
|
||||
AbstractAssembly::LabelID afterFunction = m_assembly.newLabelId();
|
||||
m_assembly.appendJumpTo(afterFunction, -stackHeightBefore + height);
|
||||
|
||||
if (m_evm15)
|
||||
m_assembly.appendBeginsub(*function.id, _function.arguments.size());
|
||||
else
|
||||
m_assembly.appendLabel(*function.id);
|
||||
|
||||
for (auto const& v: _function.returns)
|
||||
{
|
||||
auto& var = boost::get<Scope::Variable>(varScope->identifiers.at(v.name));
|
||||
var.stackHeight = height++;
|
||||
var.active = true;
|
||||
m_assembly.appendConstant(u256(0));
|
||||
}
|
||||
|
||||
CodeTransform(m_errorReporter, m_assembly, m_info, m_evm15, m_identifierAccess, 0).run(_function.body);
|
||||
|
||||
if (_function.arguments.size() > 0)
|
||||
{
|
||||
vector<int> stackLayout;
|
||||
if (!m_evm15)
|
||||
stackLayout.push_back(_function.returns.size()); // Move return label to the top
|
||||
stackLayout += vector<int>(_function.arguments.size(), -1); // discard all arguments
|
||||
for (size_t i = 0; i < _function.returns.size(); ++i)
|
||||
stackLayout.push_back(i);
|
||||
|
||||
solAssert(stackLayout.size() <= 17, "Stack too deep");
|
||||
while (stackLayout.back() != int(stackLayout.size() - 1))
|
||||
if (stackLayout.back() < 0)
|
||||
{
|
||||
m_assembly.appendInstruction(solidity::Instruction::POP);
|
||||
stackLayout.pop_back();
|
||||
}
|
||||
else
|
||||
{
|
||||
m_assembly.appendInstruction(swapInstruction(stackLayout.size() - stackLayout.back() - 1));
|
||||
swap(stackLayout[stackLayout.back()], stackLayout.back());
|
||||
}
|
||||
for (int i = 0; size_t(i) < stackLayout.size(); ++i)
|
||||
solAssert(i == stackLayout[i], "Error reshuffling stack.");
|
||||
}
|
||||
|
||||
if (m_evm15)
|
||||
m_assembly.appendReturnsub(_function.returns.size());
|
||||
else
|
||||
m_assembly.appendJump(stackHeightBefore - _function.returns.size());
|
||||
m_assembly.appendLabel(afterFunction);
|
||||
checkStackHeight(&_function);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(Block const& _block)
|
||||
{
|
||||
CodeTransform(m_errorReporter, m_assembly, m_info, m_evm15, m_identifierAccess, m_initialStackHeight).run(_block);
|
||||
}
|
||||
|
||||
AbstractAssembly::LabelID CodeTransform::labelFromIdentifier(Identifier const& _identifier)
|
||||
{
|
||||
AbstractAssembly::LabelID label = AbstractAssembly::LabelID(-1);
|
||||
if (!m_scope->lookup(_identifier.name, Scope::NonconstVisitor(
|
||||
[=](Scope::Variable&) { solAssert(false, "Expected label"); },
|
||||
[&](Scope::Label& _label)
|
||||
{
|
||||
assignLabelIdIfUnset(_label.id);
|
||||
label = *_label.id;
|
||||
},
|
||||
[=](Scope::Function&) { solAssert(false, "Expected label"); }
|
||||
)))
|
||||
{
|
||||
solAssert(false, "Identifier not found.");
|
||||
}
|
||||
return label;
|
||||
}
|
||||
|
||||
void CodeTransform::visitExpression(Statement const& _expression)
|
||||
{
|
||||
int height = m_assembly.stackHeight();
|
||||
boost::apply_visitor(*this, _expression);
|
||||
expectDeposit(1, height);
|
||||
}
|
||||
|
||||
void CodeTransform::generateAssignment(Identifier const& _variableName, SourceLocation const& _location)
|
||||
{
|
||||
auto var = m_scope.lookup(_variableName.name);
|
||||
solAssert(m_scope, "");
|
||||
auto var = m_scope->lookup(_variableName.name);
|
||||
if (var)
|
||||
{
|
||||
Scope::Variable const& _var = boost::get<Scope::Variable>(*var);
|
||||
@@ -111,142 +421,15 @@ void CodeTransform::checkStackHeight(void const* _astElement)
|
||||
solAssert(m_info.stackHeightInfo.count(_astElement), "Stack height for AST element not found.");
|
||||
solAssert(
|
||||
m_info.stackHeightInfo.at(_astElement) == m_assembly.stackHeight() - m_initialStackHeight,
|
||||
"Stack height mismatch between analysis and code generation phase."
|
||||
"Stack height mismatch between analysis and code generation phase: Analysis: " +
|
||||
to_string(m_info.stackHeightInfo.at(_astElement)) +
|
||||
" code gen: " +
|
||||
to_string(m_assembly.stackHeight() - m_initialStackHeight)
|
||||
);
|
||||
}
|
||||
|
||||
void CodeTransform::assignLabelIdIfUnset(Scope::Label& _label)
|
||||
void CodeTransform::assignLabelIdIfUnset(boost::optional<AbstractAssembly::LabelID>& _labelId)
|
||||
{
|
||||
if (!_label.id)
|
||||
_label.id.reset(m_assembly.newLabelId());
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(Block const& _block)
|
||||
{
|
||||
CodeTransform(m_errorReporter, m_assembly, _block, m_info, m_identifierAccess, m_initialStackHeight);
|
||||
checkStackHeight(&_block);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(Switch const&)
|
||||
{
|
||||
solAssert(false, "Switch not removed during desugaring phase.");
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(VariableDeclaration const& _varDecl)
|
||||
{
|
||||
int expectedItems = _varDecl.variables.size();
|
||||
int height = m_assembly.stackHeight();
|
||||
boost::apply_visitor(*this, *_varDecl.value);
|
||||
expectDeposit(expectedItems, height);
|
||||
for (auto const& variable: _varDecl.variables)
|
||||
{
|
||||
auto& var = boost::get<Scope::Variable>(m_scope.identifiers.at(variable.name));
|
||||
var.stackHeight = height++;
|
||||
var.active = true;
|
||||
}
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(Assignment const& _assignment)
|
||||
{
|
||||
int height = m_assembly.stackHeight();
|
||||
boost::apply_visitor(*this, *_assignment.value);
|
||||
expectDeposit(1, height);
|
||||
m_assembly.setSourceLocation(_assignment.location);
|
||||
generateAssignment(_assignment.variableName, _assignment.location);
|
||||
checkStackHeight(&_assignment);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(StackAssignment const& _assignment)
|
||||
{
|
||||
m_assembly.setSourceLocation(_assignment.location);
|
||||
generateAssignment(_assignment.variableName, _assignment.location);
|
||||
checkStackHeight(&_assignment);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(Label const& _label)
|
||||
{
|
||||
m_assembly.setSourceLocation(_label.location);
|
||||
solAssert(m_scope.identifiers.count(_label.name), "");
|
||||
Scope::Label& label = boost::get<Scope::Label>(m_scope.identifiers.at(_label.name));
|
||||
assignLabelIdIfUnset(label);
|
||||
m_assembly.appendLabel(*label.id);
|
||||
checkStackHeight(&_label);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(FunctionCall const&)
|
||||
{
|
||||
solAssert(false, "Function call not removed during desugaring phase.");
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(FunctionalInstruction const& _instr)
|
||||
{
|
||||
for (auto it = _instr.arguments.rbegin(); it != _instr.arguments.rend(); ++it)
|
||||
{
|
||||
int height = m_assembly.stackHeight();
|
||||
boost::apply_visitor(*this, *it);
|
||||
expectDeposit(1, height);
|
||||
}
|
||||
(*this)(_instr.instruction);
|
||||
checkStackHeight(&_instr);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(assembly::Identifier const& _identifier)
|
||||
{
|
||||
m_assembly.setSourceLocation(_identifier.location);
|
||||
// First search internals, then externals.
|
||||
if (m_scope.lookup(_identifier.name, Scope::NonconstVisitor(
|
||||
[=](Scope::Variable& _var)
|
||||
{
|
||||
if (int heightDiff = variableHeightDiff(_var, _identifier.location, false))
|
||||
m_assembly.appendInstruction(solidity::dupInstruction(heightDiff));
|
||||
else
|
||||
// Store something to balance the stack
|
||||
m_assembly.appendConstant(u256(0));
|
||||
},
|
||||
[=](Scope::Label& _label)
|
||||
{
|
||||
assignLabelIdIfUnset(_label);
|
||||
m_assembly.appendLabelReference(*_label.id);
|
||||
},
|
||||
[=](Scope::Function&)
|
||||
{
|
||||
solAssert(false, "Function not removed during desugaring.");
|
||||
}
|
||||
)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
solAssert(
|
||||
m_identifierAccess.generateCode,
|
||||
"Identifier not found and no external access available."
|
||||
);
|
||||
m_identifierAccess.generateCode(_identifier, IdentifierContext::RValue, m_assembly);
|
||||
checkStackHeight(&_identifier);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(assembly::Literal const& _literal)
|
||||
{
|
||||
m_assembly.setSourceLocation(_literal.location);
|
||||
if (_literal.kind == assembly::LiteralKind::Number)
|
||||
m_assembly.appendConstant(u256(_literal.value));
|
||||
else if (_literal.kind == assembly::LiteralKind::Boolean)
|
||||
{
|
||||
if (_literal.value == "true")
|
||||
m_assembly.appendConstant(u256(1));
|
||||
else
|
||||
m_assembly.appendConstant(u256(0));
|
||||
}
|
||||
else
|
||||
{
|
||||
solAssert(_literal.value.size() <= 32, "");
|
||||
m_assembly.appendConstant(u256(h256(_literal.value, h256::FromBinary, h256::AlignLeft)));
|
||||
}
|
||||
checkStackHeight(&_literal);
|
||||
}
|
||||
|
||||
void CodeTransform::operator()(assembly::Instruction const& _instruction)
|
||||
{
|
||||
m_assembly.setSourceLocation(_instruction.location);
|
||||
m_assembly.appendInstruction(_instruction.instruction);
|
||||
checkStackHeight(&_instruction);
|
||||
if (!_labelId)
|
||||
_labelId.reset(m_assembly.newLabelId());
|
||||
}
|
||||
|
||||
@@ -18,12 +18,13 @@
|
||||
* Common code generator for translating Julia / inline assembly to EVM and EVM1.5.
|
||||
*/
|
||||
|
||||
#include <libjulia/backends/evm/AbstractAssembly.h>
|
||||
#include <libjulia/backends/evm/EVMAssembly.h>
|
||||
|
||||
#include <libsolidity/inlineasm/AsmStack.h>
|
||||
#include <libsolidity/inlineasm/AsmScope.h>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
|
||||
namespace dev
|
||||
{
|
||||
@@ -45,37 +46,49 @@ struct StackAssignment;
|
||||
struct FunctionDefinition;
|
||||
struct FunctionCall;
|
||||
|
||||
using Statement = boost::variant<Instruction, Literal, Label, StackAssignment, Identifier, Assignment, FunctionCall, FunctionalInstruction, VariableDeclaration, FunctionDefinition, Switch, Block>;
|
||||
|
||||
struct AsmAnalysisInfo;
|
||||
}
|
||||
}
|
||||
namespace julia
|
||||
{
|
||||
class EVMAssembly;
|
||||
|
||||
class CodeTransform: public boost::static_visitor<>
|
||||
{
|
||||
public:
|
||||
/// Create the code transformer which appends assembly to _assembly as a side-effect
|
||||
/// of its creation.
|
||||
/// Create the code transformer.
|
||||
/// @param _identifierAccess used to resolve identifiers external to the inline assembly
|
||||
CodeTransform(
|
||||
solidity::ErrorReporter& _errorReporter,
|
||||
julia::AbstractAssembly& _assembly,
|
||||
solidity::assembly::Block const& _block,
|
||||
solidity::assembly::AsmAnalysisInfo& _analysisInfo,
|
||||
bool _evm15 = false,
|
||||
ExternalIdentifierAccess const& _identifierAccess = ExternalIdentifierAccess()
|
||||
): CodeTransform(_errorReporter, _assembly, _block, _analysisInfo, _identifierAccess, _assembly.stackHeight())
|
||||
): CodeTransform(_errorReporter, _assembly, _analysisInfo, _evm15, _identifierAccess, _assembly.stackHeight())
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
/// Processes the block and appends the resulting code to the assembly.
|
||||
void run(solidity::assembly::Block const& _block);
|
||||
|
||||
protected:
|
||||
CodeTransform(
|
||||
solidity::ErrorReporter& _errorReporter,
|
||||
julia::AbstractAssembly& _assembly,
|
||||
solidity::assembly::Block const& _block,
|
||||
solidity::assembly::AsmAnalysisInfo& _analysisInfo,
|
||||
bool _evm15,
|
||||
ExternalIdentifierAccess const& _identifierAccess,
|
||||
int _initialStackHeight
|
||||
);
|
||||
):
|
||||
m_errorReporter(_errorReporter),
|
||||
m_assembly(_assembly),
|
||||
m_info(_analysisInfo),
|
||||
m_evm15(_evm15),
|
||||
m_identifierAccess(_identifierAccess),
|
||||
m_initialStackHeight(_initialStackHeight)
|
||||
{}
|
||||
|
||||
public:
|
||||
void operator()(solidity::assembly::Instruction const& _instruction);
|
||||
@@ -87,11 +100,14 @@ public:
|
||||
void operator()(solidity::assembly::StackAssignment const& _assignment);
|
||||
void operator()(solidity::assembly::Assignment const& _assignment);
|
||||
void operator()(solidity::assembly::VariableDeclaration const& _varDecl);
|
||||
void operator()(solidity::assembly::Block const& _block);
|
||||
void operator()(solidity::assembly::Switch const& _switch);
|
||||
void operator()(solidity::assembly::FunctionDefinition const&);
|
||||
void operator()(solidity::assembly::Block const& _block);
|
||||
|
||||
AbstractAssembly::LabelID labelFromIdentifier(solidity::assembly::Identifier const& _identifier);
|
||||
/// Generates code for an expression that is supposed to return a single value.
|
||||
void visitExpression(solidity::assembly::Statement const& _expression);
|
||||
|
||||
private:
|
||||
void generateAssignment(solidity::assembly::Identifier const& _variableName, SourceLocation const& _location);
|
||||
|
||||
/// Determines the stack height difference to the given variables. Automatically generates
|
||||
@@ -103,13 +119,14 @@ private:
|
||||
|
||||
void checkStackHeight(void const* _astElement);
|
||||
|
||||
/// Assigns the label's id to a value taken from eth::Assembly if it has not yet been set.
|
||||
void assignLabelIdIfUnset(solidity::assembly::Scope::Label& _label);
|
||||
/// Assigns the label's or function's id to a value taken from eth::Assembly if it has not yet been set.
|
||||
void assignLabelIdIfUnset(boost::optional<AbstractAssembly::LabelID>& _labelId);
|
||||
|
||||
solidity::ErrorReporter& m_errorReporter;
|
||||
julia::AbstractAssembly& m_assembly;
|
||||
solidity::assembly::AsmAnalysisInfo& m_info;
|
||||
solidity::assembly::Scope& m_scope;
|
||||
solidity::assembly::Scope* m_scope = nullptr;
|
||||
bool m_evm15 = false;
|
||||
ExternalIdentifierAccess m_identifierAccess;
|
||||
int const m_initialStackHeight;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user