From e2b21e1c9611fd94f8e7b91b0ecbb0ec39145758 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 28 Apr 2017 18:15:18 +0200 Subject: Refactor to abstract assembly to JULIA. --- libsolidity/inlineasm/AsmCodeGen.cpp | 143 +++++++++++++++++++++++------------ 1 file changed, 94 insertions(+), 49 deletions(-) (limited to 'libsolidity/inlineasm/AsmCodeGen.cpp') diff --git a/libsolidity/inlineasm/AsmCodeGen.cpp b/libsolidity/inlineasm/AsmCodeGen.cpp index 44e12b3e..a6d7e76b 100644 --- a/libsolidity/inlineasm/AsmCodeGen.cpp +++ b/libsolidity/inlineasm/AsmCodeGen.cpp @@ -32,6 +32,8 @@ #include #include +#include + #include #include @@ -48,24 +50,61 @@ using namespace dev::solidity::assembly; struct GeneratorState { - GeneratorState(ErrorList& _errors, AsmAnalysisInfo& _analysisInfo, eth::Assembly& _assembly): - errors(_errors), info(_analysisInfo), assembly(_assembly) {} + GeneratorState(ErrorList& _errors, AsmAnalysisInfo& _analysisInfo): + errors(_errors), info(_analysisInfo) {} + + ErrorList& errors; + AsmAnalysisInfo info; +}; - size_t newLabelId() +class EthAssemblyAdapter: public julia::AbstractAssembly +{ +public: + EthAssemblyAdapter(eth::Assembly& _assembly): + m_assembly(_assembly) + { + } + virtual void setSourceLocation(SourceLocation const& _location) override + { + m_assembly.setSourceLocation(_location); + } + virtual int stackHeight() const override { return m_assembly.deposit(); } + virtual void appendInstruction(solidity::Instruction _instruction) override + { + m_assembly.append(_instruction); + } + virtual void appendConstant(u256 const& _constant) override + { + m_assembly.append(_constant); + } + /// Append a label. + virtual void appendLabel(size_t _labelId) override + { + m_assembly.append(eth::AssemblyItem(eth::Tag, _labelId)); + } + /// Append a label reference. + virtual void appendLabelReference(size_t _labelId) override + { + m_assembly.append(eth::AssemblyItem(eth::PushTag, _labelId)); + } + virtual size_t newLabel() override + { + return assemblyTagToIdentifier(m_assembly.newTag()); + } + virtual void appendLinkerSymbol(std::string const& _linkerSymbol) override { - return assemblyTagToIdentifier(assembly.newTag()); + m_assembly.appendLibraryAddress(_linkerSymbol); } - size_t assemblyTagToIdentifier(eth::AssemblyItem const& _tag) const +private: + size_t assemblyTagToIdentifier(eth::AssemblyItem const& _tag) { u256 id = _tag.data(); solAssert(id <= std::numeric_limits::max(), "Tag id too large."); return size_t(id); } - ErrorList& errors; - AsmAnalysisInfo info; - eth::Assembly& assembly; + eth::Assembly& m_assembly; }; class CodeTransform: public boost::static_visitor<> @@ -76,81 +115,84 @@ public: /// @param _identifierAccess used to resolve identifiers external to the inline assembly explicit CodeTransform( GeneratorState& _state, + julia::AbstractAssembly& _assembly, assembly::Block const& _block, assembly::ExternalIdentifierAccess const& _identifierAccess = assembly::ExternalIdentifierAccess() - ): CodeTransform(_state, _block, _identifierAccess, _state.assembly.deposit()) + ): CodeTransform(_state, _assembly, _block, _identifierAccess, _assembly.stackHeight()) { } private: CodeTransform( GeneratorState& _state, + julia::AbstractAssembly& _assembly, assembly::Block const& _block, assembly::ExternalIdentifierAccess const& _identifierAccess, - int _initialDeposit + int _initialStackHeight ): m_state(_state), + m_assembly(_assembly), m_scope(*m_state.info.scopes.at(&_block)), m_identifierAccess(_identifierAccess), - m_initialDeposit(_initialDeposit) + m_initialStackHeight(_initialStackHeight) { - int blockStartDeposit = m_state.assembly.deposit(); + int blockStartStackHeight = m_assembly.stackHeight(); std::for_each(_block.statements.begin(), _block.statements.end(), boost::apply_visitor(*this)); - m_state.assembly.setSourceLocation(_block.location); + m_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); + m_assembly.appendInstruction(solidity::Instruction::POP); - int deposit = m_state.assembly.deposit() - blockStartDeposit; + int deposit = m_assembly.stackHeight() - blockStartStackHeight; solAssert(deposit == 0, "Invalid stack height at end of block."); } public: void operator()(assembly::Instruction const& _instruction) { - m_state.assembly.setSourceLocation(_instruction.location); - m_state.assembly.append(_instruction.instruction); + m_assembly.setSourceLocation(_instruction.location); + m_assembly.appendInstruction(_instruction.instruction); checkStackHeight(&_instruction); } void operator()(assembly::Literal const& _literal) { - m_state.assembly.setSourceLocation(_literal.location); + m_assembly.setSourceLocation(_literal.location); if (_literal.kind == assembly::LiteralKind::Number) - m_state.assembly.append(u256(_literal.value)); + m_assembly.appendConstant(u256(_literal.value)); else if (_literal.kind == assembly::LiteralKind::Boolean) { if (_literal.value == "true") - m_state.assembly.append(u256(1)); + m_assembly.appendConstant(u256(1)); else - m_state.assembly.append(u256(0)); + m_assembly.appendConstant(u256(0)); } else { solAssert(_literal.value.size() <= 32, ""); - m_state.assembly.append(u256(h256(_literal.value, h256::FromBinary, h256::AlignLeft))); + m_assembly.appendConstant(u256(h256(_literal.value, h256::FromBinary, h256::AlignLeft))); } checkStackHeight(&_literal); } void operator()(assembly::Identifier const& _identifier) { - m_state.assembly.setSourceLocation(_identifier.location); + 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_state.assembly.append(solidity::dupInstruction(heightDiff)); + m_assembly.appendInstruction(solidity::dupInstruction(heightDiff)); else // Store something to balance the stack - m_state.assembly.append(u256(0)); + m_assembly.appendConstant(u256(0)); }, [=](Scope::Label& _label) { assignLabelIdIfUnset(_label); - m_state.assembly.append(eth::AssemblyItem(eth::PushTag, _label.id)); + m_assembly.appendLabelReference(*_label.id); }, [=](Scope::Function&) { @@ -164,14 +206,14 @@ public: m_identifierAccess.generateCode, "Identifier not found and no external access available." ); - m_identifierAccess.generateCode(_identifier, IdentifierContext::RValue, m_state.assembly); + m_identifierAccess.generateCode(_identifier, IdentifierContext::RValue, m_assembly); checkStackHeight(&_identifier); } void operator()(FunctionalInstruction const& _instr) { for (auto it = _instr.arguments.rbegin(); it != _instr.arguments.rend(); ++it) { - int height = m_state.assembly.deposit(); + int height = m_assembly.stackHeight(); boost::apply_visitor(*this, *it); expectDeposit(1, height); } @@ -184,31 +226,31 @@ public: } void operator()(Label const& _label) { - m_state.assembly.setSourceLocation(_label.location); + m_assembly.setSourceLocation(_label.location); solAssert(m_scope.identifiers.count(_label.name), ""); Scope::Label& label = boost::get(m_scope.identifiers.at(_label.name)); assignLabelIdIfUnset(label); - m_state.assembly.append(eth::AssemblyItem(eth::Tag, label.id)); + m_assembly.appendLabel(*label.id); checkStackHeight(&_label); } void operator()(assembly::Assignment const& _assignment) { - m_state.assembly.setSourceLocation(_assignment.location); + m_assembly.setSourceLocation(_assignment.location); generateAssignment(_assignment.variableName, _assignment.location); checkStackHeight(&_assignment); } void operator()(FunctionalAssignment const& _assignment) { - int height = m_state.assembly.deposit(); + size_t height = m_assembly.stackHeight(); boost::apply_visitor(*this, *_assignment.value); expectDeposit(1, height); - m_state.assembly.setSourceLocation(_assignment.location); + m_assembly.setSourceLocation(_assignment.location); generateAssignment(_assignment.variableName, _assignment.location); checkStackHeight(&_assignment); } void operator()(assembly::VariableDeclaration const& _varDecl) { - int height = m_state.assembly.deposit(); + size_t height = m_assembly.stackHeight(); boost::apply_visitor(*this, *_varDecl.value); expectDeposit(1, height); auto& var = boost::get(m_scope.identifiers.at(_varDecl.variable.name)); @@ -217,7 +259,7 @@ public: } void operator()(assembly::Block const& _block) { - CodeTransform(m_state, _block, m_identifierAccess, m_initialDeposit); + CodeTransform(m_state, m_assembly, _block, m_identifierAccess, m_initialStackHeight); checkStackHeight(&_block); } void operator()(assembly::FunctionDefinition const&) @@ -233,8 +275,8 @@ private: { Scope::Variable const& _var = boost::get(*var); if (int heightDiff = variableHeightDiff(_var, _location, true)) - m_state.assembly.append(solidity::swapInstruction(heightDiff - 1)); - m_state.assembly.append(solidity::Instruction::POP); + m_assembly.appendInstruction(solidity::swapInstruction(heightDiff - 1)); + m_assembly.appendInstruction(solidity::Instruction::POP); } else { @@ -242,16 +284,16 @@ private: m_identifierAccess.generateCode, "Identifier not found and no external access available." ); - m_identifierAccess.generateCode(_variableName, IdentifierContext::LValue, m_state.assembly); + m_identifierAccess.generateCode(_variableName, IdentifierContext::LValue, m_assembly); } } /// 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) + size_t variableHeightDiff(Scope::Variable const& _var, SourceLocation const& _location, bool _forSwap) { - int heightDiff = m_state.assembly.deposit() - _var.stackHeight; + size_t heightDiff = m_assembly.stackHeight() - _var.stackHeight; if (heightDiff <= (_forSwap ? 1 : 0) || heightDiff > (_forSwap ? 17 : 16)) { //@TODO move this to analysis phase. @@ -268,14 +310,14 @@ private: void expectDeposit(int _deposit, int _oldHeight) { - solAssert(m_state.assembly.deposit() == _oldHeight + _deposit, "Invalid stack deposit."); + solAssert(m_assembly.stackHeight() == _oldHeight + _deposit, "Invalid stack deposit."); } void checkStackHeight(void const* _astElement) { solAssert(m_state.info.stackHeightInfo.count(_astElement), "Stack height for AST element not found."); solAssert( - m_state.info.stackHeightInfo.at(_astElement) == m_state.assembly.deposit() - m_initialDeposit, + m_state.info.stackHeightInfo.at(_astElement) == m_assembly.stackHeight() - m_initialStackHeight, "Stack height mismatch between analysis and code generation phase." ); } @@ -283,15 +325,16 @@ private: /// 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(); + if (!_label.id) + _label.id.reset(m_assembly.newLabel()); } GeneratorState& m_state; + julia::AbstractAssembly& m_assembly; Scope& m_scope; ExternalIdentifierAccess m_identifierAccess; - int const m_initialDeposit; + int const m_initialStackHeight; }; eth::Assembly assembly::CodeGenerator::assemble( @@ -301,8 +344,9 @@ eth::Assembly assembly::CodeGenerator::assemble( ) { eth::Assembly assembly; - GeneratorState state(m_errors, _analysisInfo, assembly); - CodeTransform(state, _parsedData, _identifierAccess); + GeneratorState state(m_errors, _analysisInfo); + EthAssemblyAdapter assemblyAdapter(assembly); + CodeTransform(state, assemblyAdapter, _parsedData, _identifierAccess); return assembly; } @@ -313,6 +357,7 @@ void assembly::CodeGenerator::assemble( ExternalIdentifierAccess const& _identifierAccess ) { - GeneratorState state(m_errors, _analysisInfo, _assembly); - CodeTransform(state, _parsedData, _identifierAccess); + GeneratorState state(m_errors, _analysisInfo); + EthAssemblyAdapter assemblyAdapter(_assembly); + CodeTransform(state, assemblyAdapter, _parsedData, _identifierAccess); } -- cgit v1.2.3