diff options
author | chriseth <chris@ethereum.org> | 2017-05-27 01:30:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-27 01:30:42 +0800 |
commit | 788b64ea6181af6e7e364e86d64508c4809ca9b7 (patch) | |
tree | 2a136a342d6f8980f96043b0f6ce74828f89fdd6 /libsolidity/inlineasm | |
parent | e022f11cdb752a07b6c0f824f3e4f91233a19359 (diff) | |
parent | fe32531a16322c9528c34cfbd32924b4c24a1f13 (diff) | |
download | dexon-solidity-788b64ea6181af6e7e364e86d64508c4809ca9b7.tar dexon-solidity-788b64ea6181af6e7e364e86d64508c4809ca9b7.tar.gz dexon-solidity-788b64ea6181af6e7e364e86d64508c4809ca9b7.tar.bz2 dexon-solidity-788b64ea6181af6e7e364e86d64508c4809ca9b7.tar.lz dexon-solidity-788b64ea6181af6e7e364e86d64508c4809ca9b7.tar.xz dexon-solidity-788b64ea6181af6e7e364e86d64508c4809ca9b7.tar.zst dexon-solidity-788b64ea6181af6e7e364e86d64508c4809ca9b7.zip |
Merge pull request #2291 from ethereum/evm15
Allow different assembly types and target machines.
Diffstat (limited to 'libsolidity/inlineasm')
-rw-r--r-- | libsolidity/inlineasm/AsmAnalysis.cpp | 6 | ||||
-rw-r--r-- | libsolidity/inlineasm/AsmAnalysis.h | 4 | ||||
-rw-r--r-- | libsolidity/inlineasm/AsmCodeGen.cpp | 260 | ||||
-rw-r--r-- | libsolidity/inlineasm/AsmCodeGen.h | 4 | ||||
-rw-r--r-- | libsolidity/inlineasm/AsmStack.cpp | 4 | ||||
-rw-r--r-- | libsolidity/inlineasm/AsmStack.h | 23 |
6 files changed, 18 insertions, 283 deletions
diff --git a/libsolidity/inlineasm/AsmAnalysis.cpp b/libsolidity/inlineasm/AsmAnalysis.cpp index d022ecf6..60caaa1d 100644 --- a/libsolidity/inlineasm/AsmAnalysis.cpp +++ b/libsolidity/inlineasm/AsmAnalysis.cpp @@ -41,7 +41,7 @@ using namespace dev::solidity::assembly; AsmAnalyzer::AsmAnalyzer( AsmAnalysisInfo& _analysisInfo, ErrorList& _errors, - ExternalIdentifierAccess::Resolver const& _resolver + julia::ExternalIdentifierAccess::Resolver const& _resolver ): m_resolver(_resolver), m_info(_analysisInfo), m_errors(_errors) { @@ -123,7 +123,7 @@ bool AsmAnalyzer::operator()(assembly::Identifier const& _identifier) { size_t stackSize(-1); if (m_resolver) - stackSize = m_resolver(_identifier, IdentifierContext::RValue); + stackSize = m_resolver(_identifier, julia::IdentifierContext::RValue); if (stackSize == size_t(-1)) { // Only add an error message if the callback did not do it. @@ -393,7 +393,7 @@ bool AsmAnalyzer::checkAssignment(assembly::Identifier const& _variable, size_t variableSize = 1; } else if (m_resolver) - variableSize = m_resolver(_variable, IdentifierContext::LValue); + variableSize = m_resolver(_variable, julia::IdentifierContext::LValue); if (variableSize == size_t(-1)) { // Only add message if the callback did not. diff --git a/libsolidity/inlineasm/AsmAnalysis.h b/libsolidity/inlineasm/AsmAnalysis.h index 9f022b12..613abf38 100644 --- a/libsolidity/inlineasm/AsmAnalysis.h +++ b/libsolidity/inlineasm/AsmAnalysis.h @@ -64,7 +64,7 @@ public: AsmAnalyzer( AsmAnalysisInfo& _analysisInfo, ErrorList& _errors, - ExternalIdentifierAccess::Resolver const& _resolver = ExternalIdentifierAccess::Resolver() + julia::ExternalIdentifierAccess::Resolver const& _resolver = julia::ExternalIdentifierAccess::Resolver() ); bool analyze(assembly::Block const& _block); @@ -94,7 +94,7 @@ private: /// we enter the block. int m_virtualVariablesInNextBlock = 0; int m_stackHeight = 0; - ExternalIdentifierAccess::Resolver const& m_resolver; + julia::ExternalIdentifierAccess::Resolver const& m_resolver; Scope* m_currentScope = nullptr; AsmAnalysisInfo& m_info; ErrorList& m_errors; diff --git a/libsolidity/inlineasm/AsmCodeGen.cpp b/libsolidity/inlineasm/AsmCodeGen.cpp index 5c66b125..6a44faac 100644 --- a/libsolidity/inlineasm/AsmCodeGen.cpp +++ b/libsolidity/inlineasm/AsmCodeGen.cpp @@ -32,7 +32,8 @@ #include <libevmasm/SourceLocation.h> #include <libevmasm/Instruction.h> -#include <libjulia/backends/AbstractAssembly.h> +#include <libjulia/backends/evm/AbstractAssembly.h> +#include <libjulia/backends/evm/EVMCodeTransform.h> #include <libdevcore/CommonIO.h> @@ -48,15 +49,6 @@ using namespace dev; using namespace dev::solidity; using namespace dev::solidity::assembly; -struct GeneratorState -{ - GeneratorState(ErrorList& _errors, AsmAnalysisInfo& _analysisInfo): - errors(_errors), info(_analysisInfo) {} - - ErrorList& errors; - AsmAnalysisInfo info; -}; - class EthAssemblyAdapter: public julia::AbstractAssembly { public: @@ -107,254 +99,15 @@ private: eth::Assembly& m_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, - julia::AbstractAssembly& _assembly, - assembly::Block const& _block, - assembly::ExternalIdentifierAccess const& _identifierAccess = assembly::ExternalIdentifierAccess() - ): CodeTransform(_state, _assembly, _block, _identifierAccess, _assembly.stackHeight()) - { - } - -private: - CodeTransform( - GeneratorState& _state, - julia::AbstractAssembly& _assembly, - assembly::Block const& _block, - assembly::ExternalIdentifierAccess const& _identifierAccess, - int _initialStackHeight - ): - m_state(_state), - m_assembly(_assembly), - m_scope(*m_state.info.scopes.at(&_block)), - m_identifierAccess(_identifierAccess), - m_initialStackHeight(_initialStackHeight) - { - 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) - 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."); - } - -public: - void operator()(assembly::Instruction const& _instruction) - { - m_assembly.setSourceLocation(_instruction.location); - m_assembly.appendInstruction(_instruction.instruction); - checkStackHeight(&_instruction); - } - void 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 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 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 operator()(assembly::FunctionCall const&) - { - solAssert(false, "Function call not removed during desugaring phase."); - } - void 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 operator()(assembly::StackAssignment const& _assignment) - { - m_assembly.setSourceLocation(_assignment.location); - generateAssignment(_assignment.variableName, _assignment.location); - checkStackHeight(&_assignment); - } - void operator()(assembly::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 operator()(assembly::VariableDeclaration const& _varDecl) - { - int height = m_assembly.stackHeight(); - int expectedItems = _varDecl.variables.size(); - 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 operator()(assembly::Block const& _block) - { - CodeTransform(m_state, m_assembly, _block, m_identifierAccess, m_initialStackHeight); - checkStackHeight(&_block); - } - void operator()(assembly::Switch const&) - { - solAssert(false, "Switch not removed during desugaring phase."); - } - void operator()(assembly::FunctionDefinition const&) - { - solAssert(false, "Function definition not removed during desugaring phase."); - } - -private: - void generateAssignment(assembly::Identifier const& _variableName, SourceLocation const& _location) - { - auto var = m_scope.lookup(_variableName.name); - if (var) - { - Scope::Variable const& _var = boost::get<Scope::Variable>(*var); - if (int heightDiff = variableHeightDiff(_var, _location, true)) - m_assembly.appendInstruction(solidity::swapInstruction(heightDiff - 1)); - m_assembly.appendInstruction(solidity::Instruction::POP); - } - else - { - solAssert( - m_identifierAccess.generateCode, - "Identifier not found and no external access available." - ); - 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) - { - int heightDiff = m_assembly.stackHeight() - _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>( - Error::Type::TypeError, - "Variable inaccessible, too deep inside stack (" + boost::lexical_cast<string>(heightDiff) + ")", - _location - )); - return 0; - } - else - return heightDiff; - } - - void expectDeposit(int _deposit, int _oldHeight) - { - 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_assembly.stackHeight() - m_initialStackHeight, - "Stack height mismatch between analysis and code generation phase." - ); - } - - /// 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) - _label.id.reset(m_assembly.newLabelId()); - } - - - GeneratorState& m_state; - julia::AbstractAssembly& m_assembly; - Scope& m_scope; - ExternalIdentifierAccess m_identifierAccess; - int const m_initialStackHeight; -}; - eth::Assembly assembly::CodeGenerator::assemble( Block const& _parsedData, AsmAnalysisInfo& _analysisInfo, - ExternalIdentifierAccess const& _identifierAccess + julia::ExternalIdentifierAccess const& _identifierAccess ) { eth::Assembly assembly; - GeneratorState state(m_errors, _analysisInfo); EthAssemblyAdapter assemblyAdapter(assembly); - CodeTransform(state, assemblyAdapter, _parsedData, _identifierAccess); + julia::CodeTransform(m_errors, assemblyAdapter, _parsedData, _analysisInfo, _identifierAccess); return assembly; } @@ -362,10 +115,9 @@ void assembly::CodeGenerator::assemble( Block const& _parsedData, AsmAnalysisInfo& _analysisInfo, eth::Assembly& _assembly, - ExternalIdentifierAccess const& _identifierAccess + julia::ExternalIdentifierAccess const& _identifierAccess ) { - GeneratorState state(m_errors, _analysisInfo); EthAssemblyAdapter assemblyAdapter(_assembly); - CodeTransform(state, assemblyAdapter, _parsedData, _identifierAccess); + julia::CodeTransform(m_errors, assemblyAdapter, _parsedData, _analysisInfo, _identifierAccess); } diff --git a/libsolidity/inlineasm/AsmCodeGen.h b/libsolidity/inlineasm/AsmCodeGen.h index e830e047..1b43d2f6 100644 --- a/libsolidity/inlineasm/AsmCodeGen.h +++ b/libsolidity/inlineasm/AsmCodeGen.h @@ -48,14 +48,14 @@ public: eth::Assembly assemble( Block const& _parsedData, AsmAnalysisInfo& _analysisInfo, - ExternalIdentifierAccess const& _identifierAccess = ExternalIdentifierAccess() + julia::ExternalIdentifierAccess const& _identifierAccess = julia::ExternalIdentifierAccess() ); /// Performs code generation and appends generated to to _assembly. void assemble( Block const& _parsedData, AsmAnalysisInfo& _analysisInfo, eth::Assembly& _assembly, - ExternalIdentifierAccess const& _identifierAccess = ExternalIdentifierAccess() + julia::ExternalIdentifierAccess const& _identifierAccess = julia::ExternalIdentifierAccess() ); private: diff --git a/libsolidity/inlineasm/AsmStack.cpp b/libsolidity/inlineasm/AsmStack.cpp index c2a7d8ea..2e63465f 100644 --- a/libsolidity/inlineasm/AsmStack.cpp +++ b/libsolidity/inlineasm/AsmStack.cpp @@ -42,7 +42,7 @@ using namespace dev::solidity::assembly; bool InlineAssemblyStack::parse( shared_ptr<Scanner> const& _scanner, - ExternalIdentifierAccess::Resolver const& _resolver + julia::ExternalIdentifierAccess::Resolver const& _resolver ) { m_parserResult = make_shared<Block>(); @@ -73,7 +73,7 @@ eth::Assembly InlineAssemblyStack::assemble() bool InlineAssemblyStack::parseAndAssemble( string const& _input, eth::Assembly& _assembly, - ExternalIdentifierAccess const& _identifierAccess + julia::ExternalIdentifierAccess const& _identifierAccess ) { ErrorList errors; diff --git a/libsolidity/inlineasm/AsmStack.h b/libsolidity/inlineasm/AsmStack.h index e223ccc9..23072a88 100644 --- a/libsolidity/inlineasm/AsmStack.h +++ b/libsolidity/inlineasm/AsmStack.h @@ -24,7 +24,7 @@ #include <libsolidity/interface/Exceptions.h> -#include <libjulia/backends/AbstractAssembly.h> +#include <libjulia/backends/evm/AbstractAssembly.h> #include <string> #include <functional> @@ -43,23 +43,6 @@ namespace assembly struct Block; struct Identifier; -enum class IdentifierContext { LValue, RValue }; - -/// Object that is used to resolve references and generate code for access to identifiers external -/// to inline assembly (not used in standalone assembly mode). -struct ExternalIdentifierAccess -{ - using Resolver = std::function<size_t(assembly::Identifier const&, IdentifierContext)>; - /// 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; - using CodeGenerator = std::function<void(assembly::Identifier const&, IdentifierContext, julia::AbstractAssembly&)>; - /// Generate code for retrieving the value (rvalue context) or storing the value (lvalue context) - /// of an identifier. The code should be appended to the assembly. In rvalue context, the value is supposed - /// to be put onto the stack, in lvalue context, the value is assumed to be at the top of the stack. - CodeGenerator generateCode; -}; - class InlineAssemblyStack { public: @@ -67,7 +50,7 @@ public: /// @return false or error. bool parse( std::shared_ptr<Scanner> const& _scanner, - ExternalIdentifierAccess::Resolver const& _externalIdentifierResolver = ExternalIdentifierAccess::Resolver() + julia::ExternalIdentifierAccess::Resolver const& _externalIdentifierResolver = julia::ExternalIdentifierAccess::Resolver() ); /// Converts the parser result back into a string form (not necessarily the same form /// as the source form, but it should parse into the same parsed form again). @@ -79,7 +62,7 @@ public: bool parseAndAssemble( std::string const& _input, eth::Assembly& _assembly, - ExternalIdentifierAccess const& _identifierAccess = ExternalIdentifierAccess() + julia::ExternalIdentifierAccess const& _identifierAccess = julia::ExternalIdentifierAccess() ); ErrorList const& errors() const { return m_errors; } |