solidity/libsolidity/inlineasm/AsmCodeGen.cpp

286 lines
8.9 KiB
C++
Raw Normal View History

/*
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/>.
*/
/**
* @author Christian <c@ethdev.com>
* @date 2016
* Code-generating part of inline assembly.
*/
#include <libsolidity/inlineasm/AsmCodeGen.h>
2017-02-15 13:52:53 +00:00
#include <libsolidity/inlineasm/AsmParser.h>
#include <libsolidity/inlineasm/AsmData.h>
#include <libsolidity/inlineasm/AsmScope.h>
2017-02-15 13:52:53 +00:00
#include <libsolidity/inlineasm/AsmAnalysis.h>
#include <libsolidity/inlineasm/AsmAnalysisInfo.h>
2017-02-15 13:52:53 +00:00
#include <libevmasm/Assembly.h>
#include <libevmasm/SourceLocation.h>
#include <libevmasm/Instruction.h>
2017-02-15 13:52:53 +00:00
#include <libdevcore/CommonIO.h>
#include <boost/range/adaptor/reversed.hpp>
2017-02-15 15:47:54 +00:00
#include <boost/range/adaptor/map.hpp>
#include <boost/range/algorithm/count_if.hpp>
2017-02-15 13:52:53 +00:00
#include <memory>
#include <functional>
using namespace std;
using namespace dev;
using namespace dev::solidity;
using namespace dev::solidity::assembly;
struct GeneratorState
{
GeneratorState(ErrorList& _errors, AsmAnalysisInfo& _analysisInfo, eth::Assembly& _assembly):
errors(_errors), info(_analysisInfo), assembly(_assembly) {}
2017-02-15 13:52:53 +00:00
size_t newLabelId()
{
2017-02-15 13:52:53 +00:00
return assemblyTagToIdentifier(assembly.newTag());
}
2017-02-15 13:52:53 +00:00
size_t assemblyTagToIdentifier(eth::AssemblyItem const& _tag) const
{
2017-02-15 13:52:53 +00:00
u256 id = _tag.data();
solAssert(id <= std::numeric_limits<size_t>::max(), "Tag id too large.");
return size_t(id);
}
ErrorList& errors;
AsmAnalysisInfo info;
eth::Assembly& assembly;
};
class CodeTransform: public boost::static_visitor<>
{
public:
/// Create the code transformer which appends assembly to _state.assembly when called
/// with parsed assembly data.
/// @param _identifierAccess used to resolve identifiers external to the inline assembly
explicit CodeTransform(
GeneratorState& _state,
2017-02-15 15:47:54 +00:00
assembly::Block const& _block,
assembly::ExternalIdentifierAccess const& _identifierAccess = assembly::ExternalIdentifierAccess()
):
2017-02-15 15:47:54 +00:00
m_state(_state),
m_scope(*m_state.info.scopes.at(&_block)),
2017-02-15 15:47:54 +00:00
m_initialDeposit(m_state.assembly.deposit()),
m_identifierAccess(_identifierAccess)
{
2017-02-15 15:47:54 +00:00
std::for_each(_block.statements.begin(), _block.statements.end(), boost::apply_visitor(*this));
m_state.assembly.setSourceLocation(_block.location);
// pop variables
for (auto const& identifier: m_scope.identifiers)
if (identifier.second.type() == typeid(Scope::Variable))
m_state.assembly.append(solidity::Instruction::POP);
int deposit = m_state.assembly.deposit() - m_initialDeposit;
solAssert(deposit == 0, "Invalid stack height at end of block.");
}
2016-04-18 11:47:40 +00:00
void operator()(assembly::Instruction const& _instruction)
{
2016-04-18 11:47:40 +00:00
m_state.assembly.setSourceLocation(_instruction.location);
m_state.assembly.append(_instruction.instruction);
}
void operator()(assembly::Literal const& _literal)
{
2016-04-18 11:47:40 +00:00
m_state.assembly.setSourceLocation(_literal.location);
if (_literal.isNumber)
m_state.assembly.append(u256(_literal.value));
else
2017-02-15 13:52:53 +00:00
{
solAssert(_literal.value.size() <= 32, "");
m_state.assembly.append(u256(h256(_literal.value, h256::FromBinary, h256::AlignLeft)));
2017-02-15 13:52:53 +00:00
}
}
void operator()(assembly::Identifier const& _identifier)
{
2016-04-18 11:47:40 +00:00
m_state.assembly.setSourceLocation(_identifier.location);
2017-02-15 13:52:53 +00:00
// First search internals, then externals.
2017-02-15 15:47:54 +00:00
if (m_scope.lookup(_identifier.name, Scope::NonconstVisitor(
2017-02-15 13:52:53 +00:00
[=](Scope::Variable& _var)
2016-04-18 11:47:40 +00:00
{
2017-02-16 09:12:54 +00:00
if (int heightDiff = variableHeightDiff(_var, _identifier.location, false))
2017-02-15 13:52:53 +00:00
m_state.assembly.append(solidity::dupInstruction(heightDiff));
2017-02-16 09:12:54 +00:00
else
// Store something to balance the stack
m_state.assembly.append(u256(0));
2017-02-15 13:52:53 +00:00
},
[=](Scope::Label& _label)
{
2017-02-23 11:56:37 +00:00
assignLabelIdIfUnset(_label);
2017-02-15 13:52:53 +00:00
m_state.assembly.append(eth::AssemblyItem(eth::PushTag, _label.id));
2017-02-16 22:44:22 +00:00
},
[=](Scope::Function&)
{
solAssert(false, "Function not removed during desugaring.");
2016-04-18 11:47:40 +00:00
}
2017-02-15 13:52:53 +00:00
)))
2017-02-15 15:47:54 +00:00
{
return;
2017-02-15 15:47:54 +00:00
}
solAssert(
m_identifierAccess.generateCode,
"Identifier not found and no external access available."
);
m_identifierAccess.generateCode(_identifier, IdentifierContext::RValue, m_state.assembly);
}
void operator()(FunctionalInstruction const& _instr)
{
for (auto it = _instr.arguments.rbegin(); it != _instr.arguments.rend(); ++it)
{
int height = m_state.assembly.deposit();
boost::apply_visitor(*this, *it);
expectDeposit(1, height);
}
(*this)(_instr.instruction);
}
2017-02-01 20:20:21 +00:00
void operator()(assembly::FunctionCall const&)
{
solAssert(false, "Function call not removed during desugaring phase.");
}
void operator()(Label const& _label)
{
2016-04-18 11:47:40 +00:00
m_state.assembly.setSourceLocation(_label.location);
2017-02-15 15:47:54 +00:00
solAssert(m_scope.identifiers.count(_label.name), "");
Scope::Label& label = boost::get<Scope::Label>(m_scope.identifiers.at(_label.name));
2017-02-23 11:56:37 +00:00
assignLabelIdIfUnset(label);
2017-02-15 15:47:54 +00:00
m_state.assembly.append(eth::AssemblyItem(eth::Tag, label.id));
}
void operator()(assembly::Assignment const& _assignment)
{
2016-04-18 11:47:40 +00:00
m_state.assembly.setSourceLocation(_assignment.location);
generateAssignment(_assignment.variableName, _assignment.location);
}
void operator()(FunctionalAssignment const& _assignment)
{
int height = m_state.assembly.deposit();
boost::apply_visitor(*this, *_assignment.value);
expectDeposit(1, height);
2016-04-18 11:47:40 +00:00
m_state.assembly.setSourceLocation(_assignment.location);
generateAssignment(_assignment.variableName, _assignment.location);
}
void operator()(assembly::VariableDeclaration const& _varDecl)
{
int height = m_state.assembly.deposit();
boost::apply_visitor(*this, *_varDecl.value);
expectDeposit(1, height);
auto& var = boost::get<Scope::Variable>(m_scope.identifiers.at(_varDecl.name));
2017-02-15 13:52:53 +00:00
var.stackHeight = height;
var.active = true;
}
void operator()(assembly::Block const& _block)
{
2017-02-15 15:47:54 +00:00
CodeTransform(m_state, _block, m_identifierAccess);
2017-01-31 22:59:41 +00:00
}
void operator()(assembly::FunctionDefinition const&)
{
solAssert(false, "Function definition not removed during desugaring phase.");
}
private:
2016-04-18 11:47:40 +00:00
void generateAssignment(assembly::Identifier const& _variableName, SourceLocation const& _location)
{
auto var = m_scope.lookup(_variableName.name);
if (var)
2017-02-15 15:47:54 +00:00
{
Scope::Variable const& _var = boost::get<Scope::Variable>(*var);
if (int heightDiff = variableHeightDiff(_var, _location, true))
m_state.assembly.append(solidity::swapInstruction(heightDiff - 1));
m_state.assembly.append(solidity::Instruction::POP);
2017-02-15 15:47:54 +00:00
}
else
{
solAssert(
m_identifierAccess.generateCode,
"Identifier not found and no external access available."
);
m_identifierAccess.generateCode(_variableName, IdentifierContext::LValue, m_state.assembly);
}
}
2017-02-16 09:12:54 +00:00
/// Determines the stack height difference to the given variables. Automatically generates
/// errors if it is not yet in scope or the height difference is too large. Returns 0 on
/// errors and the (positive) stack height difference otherwise.
int variableHeightDiff(Scope::Variable const& _var, SourceLocation const& _location, bool _forSwap)
{
int heightDiff = m_state.assembly.deposit() - _var.stackHeight;
if (heightDiff <= (_forSwap ? 1 : 0) || heightDiff > (_forSwap ? 17 : 16))
{
//@TODO move this to analysis phase.
m_state.errors.push_back(make_shared<Error>(
2017-02-16 09:12:54 +00:00
Error::Type::TypeError,
"Variable inaccessible, too deep inside stack (" + boost::lexical_cast<string>(heightDiff) + ")",
_location
));
2017-02-16 09:12:54 +00:00
return 0;
}
else
return heightDiff;
}
void expectDeposit(int _deposit, int _oldHeight)
{
solAssert(m_state.assembly.deposit() == _oldHeight + _deposit, "Invalid stack deposit.");
}
2017-02-23 11:56:37 +00:00
/// Assigns the label's id to a value taken from eth::Assembly if it has not yet been set.
void assignLabelIdIfUnset(Scope::Label& _label)
{
if (_label.id == Scope::Label::unassignedLabelId)
_label.id = m_state.newLabelId();
else if (_label.id == Scope::Label::errorLabelId)
_label.id = size_t(m_state.assembly.errorTag().data());
}
GeneratorState& m_state;
2017-02-15 15:47:54 +00:00
Scope& m_scope;
int const m_initialDeposit;
ExternalIdentifierAccess m_identifierAccess;
};
2017-04-12 16:32:25 +00:00
eth::Assembly assembly::CodeGenerator::assemble(
Block const& _parsedData,
AsmAnalysisInfo& _analysisInfo,
2017-04-12 16:32:25 +00:00
ExternalIdentifierAccess const& _identifierAccess
)
{
eth::Assembly assembly;
GeneratorState state(m_errors, _analysisInfo, assembly);
2017-04-12 16:32:25 +00:00
CodeTransform(state, _parsedData, _identifierAccess);
return assembly;
}
2017-04-12 16:32:25 +00:00
void assembly::CodeGenerator::assemble(
Block const& _parsedData,
AsmAnalysisInfo& _analysisInfo,
2017-04-12 16:32:25 +00:00
eth::Assembly& _assembly,
ExternalIdentifierAccess const& _identifierAccess
)
{
GeneratorState state(m_errors, _analysisInfo, _assembly);
2017-04-12 16:32:25 +00:00
CodeTransform(state, _parsedData, _identifierAccess);
}