aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Changelog.md7
-rw-r--r--README.md6
-rw-r--r--docs/introduction-to-smart-contracts.rst8
-rw-r--r--docs/solidity-by-example.rst14
-rw-r--r--libjulia/backends/evm/AbstractAssembly.h (renamed from libjulia/backends/AbstractAssembly.h)0
-rw-r--r--libjulia/backends/evm/EVMCodeTransform.cpp253
-rw-r--r--libjulia/backends/evm/EVMCodeTransform.h119
-rw-r--r--libsolidity/CMakeLists.txt4
-rw-r--r--libsolidity/analysis/ReferencesResolver.cpp6
-rw-r--r--libsolidity/analysis/StaticAnalyzer.cpp22
-rw-r--r--libsolidity/analysis/StaticAnalyzer.h1
-rw-r--r--libsolidity/analysis/TypeChecker.cpp11
-rw-r--r--libsolidity/ast/AST.h2
-rw-r--r--libsolidity/ast/ASTJsonConverter.cpp6
-rw-r--r--libsolidity/codegen/CompilerContext.cpp10
-rw-r--r--libsolidity/codegen/ContractCompiler.cpp8
-rw-r--r--libsolidity/inlineasm/AsmAnalysis.cpp102
-rw-r--r--libsolidity/inlineasm/AsmAnalysis.h21
-rw-r--r--libsolidity/inlineasm/AsmAnalysisInfo.h7
-rw-r--r--libsolidity/inlineasm/AsmCodeGen.cpp252
-rw-r--r--libsolidity/inlineasm/AsmCodeGen.h4
-rw-r--r--libsolidity/inlineasm/AsmData.h36
-rw-r--r--libsolidity/inlineasm/AsmParser.cpp145
-rw-r--r--libsolidity/inlineasm/AsmParser.h3
-rw-r--r--libsolidity/inlineasm/AsmPrinter.cpp31
-rw-r--r--libsolidity/inlineasm/AsmPrinter.h6
-rw-r--r--libsolidity/inlineasm/AsmScopeFiller.cpp14
-rw-r--r--libsolidity/inlineasm/AsmScopeFiller.h8
-rw-r--r--libsolidity/inlineasm/AsmStack.cpp8
-rw-r--r--libsolidity/inlineasm/AsmStack.h23
-rw-r--r--libsolidity/interface/AssemblyStack.cpp85
-rw-r--r--libsolidity/interface/AssemblyStack.h85
-rw-r--r--libsolidity/interface/CompilerStack.cpp4
-rw-r--r--libsolidity/parsing/Parser.cpp44
-rw-r--r--libsolidity/parsing/Parser.h5
-rw-r--r--libsolidity/parsing/ParserBase.cpp56
-rw-r--r--libsolidity/parsing/ParserBase.h10
-rwxr-xr-xscripts/bytecodecompare/storebytecode.sh10
-rw-r--r--solc/CommandLineInterface.cpp111
-rw-r--r--solc/CommandLineInterface.h9
-rw-r--r--test/libjulia/Parser.cpp45
-rw-r--r--test/libsolidity/InlineAssembly.cpp89
-rw-r--r--test/libsolidity/Metadata.cpp63
-rw-r--r--test/libsolidity/SolidityABIJSON.cpp20
-rw-r--r--test/libsolidity/SolidityNameAndTypeResolution.cpp14
45 files changed, 1254 insertions, 533 deletions
diff --git a/Changelog.md b/Changelog.md
index 650d75d8..5ba712f5 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -1,5 +1,12 @@
### 0.4.12 (unreleased)
+
+Features:
* AST: export all attributes to Json format
+ * Inline Assembly: Present proper error message when not supplying enough arguments to a functional
+ instruction.
+
+Bugfixes:
+ * Unused variable warnings no longer issued for variables used inside inline assembly
### 0.4.11 (2017-05-03)
diff --git a/README.md b/README.md
index bda66996..1fed49fb 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
[![Join the chat at https://gitter.im/ethereum/solidity](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/ethereum/solidity?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
## Useful links
-To get started you can find an introduction to the language in the [Solidity documentation](https://solidity.readthedocs.org). In the documentation, you can find [code examples](http://solidity.readthedocs.io/en/latest/solidity-by-example.html) as well as [a reference](http://solidity.readthedocs.io/en/latest/solidity-in-depth.html) of the syntax and details on how to write smart contracts.
+To get started you can find an introduction to the language in the [Solidity documentation](https://solidity.readthedocs.org). In the documentation, you can find [code examples](https://solidity.readthedocs.io/en/latest/solidity-by-example.html) as well as [a reference](https://solidity.readthedocs.io/en/latest/solidity-in-depth.html) of the syntax and details on how to write smart contracts.
You can start using [Solidity in your browser](https://ethereum.github.io/browser-solidity/) with no need to download or compile anything.
@@ -11,9 +11,9 @@ The changelog for this project can be found [here](https://github.com/ethereum/s
Solidity is still under development. So please do not hesitate and open an [issue in GitHub](https://github.com/ethereum/solidity/issues) if you encounter anything strange.
## Building
-See the [Solidity documentation](http://solidity.readthedocs.io/en/latest/installing-solidity.html#building-from-source) for build instructions.
+See the [Solidity documentation](https://solidity.readthedocs.io/en/latest/installing-solidity.html#building-from-source) for build instructions.
## How to Contribute
-Please see our contribution guidelines in [the Solidity documentation](http://solidity.readthedocs.io/en/latest/contributing.html).
+Please see our contribution guidelines in [the Solidity documentation](https://solidity.readthedocs.io/en/latest/contributing.html).
Any contributions are welcome!
diff --git a/docs/introduction-to-smart-contracts.rst b/docs/introduction-to-smart-contracts.rst
index d6c30db8..dc7c6cc9 100644
--- a/docs/introduction-to-smart-contracts.rst
+++ b/docs/introduction-to-smart-contracts.rst
@@ -35,7 +35,7 @@ Solidity version 0.4.0 or anything newer that does not break functionality
(up to, but not including, version 0.5.0). This is to ensure that the
contract does not suddenly behave differently with a new compiler version. The keyword ``pragma`` is called that way because, in general,
pragmas are instructions for the compiler about how to treat the
-source code (e.g. [pragma once](https://en.wikipedia.org/wiki/Pragma_once)). .
+source code (e.g. `pragma once <https://en.wikipedia.org/wiki/Pragma_once>`_). .
A contract in the sense of Solidity is a collection of code (its *functions*) and
data (its *state*) that resides at a specific address on the Ethereum
@@ -126,7 +126,7 @@ get the idea - the compiler figures that out for you.
The next line, ``mapping (address => uint) public balances;`` also
creates a public state variable, but it is a more complex datatype.
The type maps addresses to unsigned integers.
-Mappings can be seen as [hash tables](https://en.wikipedia.org/wiki/Hash_table) which are
+Mappings can be seen as `hash tables <https://en.wikipedia.org/wiki/Hash_table>`_ which are
virtually initialized such that every possible key exists and is mapped to a
value whose byte-representation is all zeros. This analogy does not go
too far, though, as it is neither possible to obtain a list of all keys of
@@ -195,7 +195,7 @@ Blockchain Basics
*****************
Blockchains as a concept are not too hard to understand for programmers. The reason is that
-most of the complications (mining, [hashing](https://en.wikipedia.org/wiki/Cryptographic_hash_function), [elliptic-curve cryptography](https://en.wikipedia.org/wiki/Elliptic_curve_cryptography), [peer-to-peer networks](https://en.wikipedia.org/wiki/Peer-to-peer), etc.)
+most of the complications (mining, `hashing <https://en.wikipedia.org/wiki/Cryptographic_hash_function>`_, `elliptic-curve cryptography <https://en.wikipedia.org/wiki/Elliptic_curve_cryptography>`_, `peer-to-peer networks <https://en.wikipedia.org/wiki/Peer-to-peer>`_, etc.)
are just there to provide a certain set of features and promises. Once you accept these
features as given, you do not have to worry about the underlying technology - or do you have
to know how Amazon's AWS works internally in order to use it?
@@ -438,7 +438,7 @@ that maps all the way up to the block level. This feature called **logs**
is used by Solidity in order to implement **events**.
Contracts cannot access log data after it has been created, but they
can be efficiently accessed from outside the blockchain.
-Since some part of the log data is stored in [bloom filters](https://en.wikipedia.org/wiki/Bloom_filter), it is
+Since some part of the log data is stored in `bloom filters <https://en.wikipedia.org/wiki/Bloom_filter>`_, it is
possible to search for this data in an efficient and cryptographically
secure way, so network peers that do not download the whole blockchain
("light clients") can still find these logs.
diff --git a/docs/solidity-by-example.rst b/docs/solidity-by-example.rst
index ab3f375e..993e2c18 100644
--- a/docs/solidity-by-example.rst
+++ b/docs/solidity-by-example.rst
@@ -316,7 +316,7 @@ activate themselves.
// 3. interacting with other contracts
// If these phases are mixed up, the other contract could call
// back into the current contract and modify the state or cause
- // effects (ether payout) to be perfromed multiple times.
+ // effects (ether payout) to be performed multiple times.
// If functions called internally include interaction with external
// contracts, they also have to be considered interaction with
// external contracts.
@@ -566,9 +566,9 @@ Safe Remote Purchase
_;
}
- event aborted();
- event purchaseConfirmed();
- event itemReceived();
+ event Aborted();
+ event PurchaseConfirmed();
+ event ItemReceived();
/// Abort the purchase and reclaim the ether.
/// Can only be called by the seller before
@@ -577,7 +577,7 @@ Safe Remote Purchase
onlySeller
inState(State.Created)
{
- aborted();
+ Aborted();
state = State.Inactive;
seller.transfer(this.balance);
}
@@ -591,7 +591,7 @@ Safe Remote Purchase
condition(msg.value == (2 * value))
payable
{
- purchaseConfirmed();
+ PurchaseConfirmed();
buyer = msg.sender;
state = State.Locked;
}
@@ -602,7 +602,7 @@ Safe Remote Purchase
onlyBuyer
inState(State.Locked)
{
- itemReceived();
+ ItemReceived();
// It is important to change the state first because
// otherwise, the contracts called using `send` below
// can call in again here.
diff --git a/libjulia/backends/AbstractAssembly.h b/libjulia/backends/evm/AbstractAssembly.h
index e3afa2b6..e3afa2b6 100644
--- a/libjulia/backends/AbstractAssembly.h
+++ b/libjulia/backends/evm/AbstractAssembly.h
diff --git a/libjulia/backends/evm/EVMCodeTransform.cpp b/libjulia/backends/evm/EVMCodeTransform.cpp
new file mode 100644
index 00000000..18c3e63c
--- /dev/null
+++ b/libjulia/backends/evm/EVMCodeTransform.cpp
@@ -0,0 +1,253 @@
+/*
+ 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.
+ */
+
+#include <libjulia/backends/evm/EVMCodeTransform.h>
+
+#include <libsolidity/inlineasm/AsmAnalysisInfo.h>
+#include <libsolidity/inlineasm/AsmData.h>
+
+#include <libsolidity/interface/Utils.h>
+
+using namespace std;
+using namespace dev;
+using namespace dev::julia;
+using namespace dev::solidity;
+using namespace dev::solidity::assembly;
+
+CodeTransform::CodeTransform(
+ ErrorList& _errors,
+ AbstractAssembly& _assembly,
+ Block const& _block,
+ AsmAnalysisInfo& _analysisInfo,
+ ExternalIdentifierAccess const& _identifierAccess,
+ int _initialStackHeight
+):
+ m_errors(_errors),
+ m_assembly(_assembly),
+ m_info(_analysisInfo),
+ m_scope(*_analysisInfo.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.");
+}
+
+void CodeTransform::operator()(const FunctionDefinition&)
+{
+ solAssert(false, "Function definition not removed during desugaring phase.");
+}
+
+void CodeTransform::generateAssignment(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);
+ }
+}
+
+int CodeTransform::variableHeightDiff(solidity::assembly::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_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 CodeTransform::expectDeposit(int _deposit, int _oldHeight)
+{
+ solAssert(m_assembly.stackHeight() == _oldHeight + _deposit, "Invalid stack deposit.");
+}
+
+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."
+ );
+}
+
+void CodeTransform::assignLabelIdIfUnset(Scope::Label& _label)
+{
+ if (!_label.id)
+ _label.id.reset(m_assembly.newLabelId());
+}
+
+void CodeTransform::operator()(Block const& _block)
+{
+ CodeTransform(m_errors, 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);
+}
diff --git a/libjulia/backends/evm/EVMCodeTransform.h b/libjulia/backends/evm/EVMCodeTransform.h
new file mode 100644
index 00000000..5408a3aa
--- /dev/null
+++ b/libjulia/backends/evm/EVMCodeTransform.h
@@ -0,0 +1,119 @@
+/*
+ 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.
+ */
+
+#include <libjulia/backends/evm/AbstractAssembly.h>
+
+#include <libsolidity/interface/Exceptions.h>
+
+#include <libsolidity/inlineasm/AsmStack.h>
+#include <libsolidity/inlineasm/AsmScope.h>
+
+#include <boost/variant.hpp>
+
+namespace dev
+{
+namespace solidity
+{
+namespace assembly
+{
+struct Literal;
+struct Block;
+struct Switch;
+struct Label;
+struct FunctionalInstruction;
+struct Assignment;
+struct VariableDeclaration;
+struct Instruction;
+struct Identifier;
+struct StackAssignment;
+struct FunctionDefinition;
+struct FunctionCall;
+
+struct AsmAnalysisInfo;
+}
+}
+namespace julia
+{
+
+class CodeTransform: public boost::static_visitor<>
+{
+public:
+ /// Create the code transformer which appends assembly to _assembly as a side-effect
+ /// of its creation.
+ /// @param _identifierAccess used to resolve identifiers external to the inline assembly
+ CodeTransform(
+ solidity::ErrorList& _errors,
+ julia::AbstractAssembly& _assembly,
+ solidity::assembly::Block const& _block,
+ solidity::assembly::AsmAnalysisInfo& _analysisInfo,
+ ExternalIdentifierAccess const& _identifierAccess = ExternalIdentifierAccess()
+ ): CodeTransform(_errors, _assembly, _block, _analysisInfo, _identifierAccess, _assembly.stackHeight())
+ {
+ }
+
+private:
+ CodeTransform(
+ solidity::ErrorList& _errors,
+ julia::AbstractAssembly& _assembly,
+ solidity::assembly::Block const& _block,
+ solidity::assembly::AsmAnalysisInfo& _analysisInfo,
+ ExternalIdentifierAccess const& _identifierAccess,
+ int _initialStackHeight
+ );
+
+public:
+ void operator()(solidity::assembly::Instruction const& _instruction);
+ void operator()(solidity::assembly::Literal const& _literal);
+ void operator()(solidity::assembly::Identifier const& _identifier);
+ void operator()(solidity::assembly::FunctionalInstruction const& _instr);
+ void operator()(solidity::assembly::FunctionCall const&);
+ void operator()(solidity::assembly::Label const& _label);
+ 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&);
+
+private:
+ void generateAssignment(solidity::assembly::Identifier const& _variableName, SourceLocation const& _location);
+
+ /// 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(solidity::assembly::Scope::Variable const& _var, SourceLocation const& _location, bool _forSwap);
+
+ void expectDeposit(int _deposit, int _oldHeight);
+
+ 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);
+
+ solidity::ErrorList& m_errors;
+ julia::AbstractAssembly& m_assembly;
+ solidity::assembly::AsmAnalysisInfo& m_info;
+ solidity::assembly::Scope& m_scope;
+ ExternalIdentifierAccess m_identifierAccess;
+ int const m_initialStackHeight;
+};
+
+}
+}
diff --git a/libsolidity/CMakeLists.txt b/libsolidity/CMakeLists.txt
index bcc47e5a..2342f0f9 100644
--- a/libsolidity/CMakeLists.txt
+++ b/libsolidity/CMakeLists.txt
@@ -7,10 +7,12 @@ aux_source_directory(formal SRC_LIST)
aux_source_directory(interface SRC_LIST)
aux_source_directory(parsing SRC_LIST)
aux_source_directory(inlineasm SRC_LIST)
+# Until we have a clear separation, libjulia has to be included here
+aux_source_directory(../libjulia SRC_LIST)
set(EXECUTABLE solidity)
-file(GLOB HEADERS "*/*.h")
+file(GLOB HEADERS "*/*.h" "../libjulia/backends/evm/*")
include_directories(BEFORE ..)
add_library(${EXECUTABLE} ${SRC_LIST} ${HEADERS})
diff --git a/libsolidity/analysis/ReferencesResolver.cpp b/libsolidity/analysis/ReferencesResolver.cpp
index 9433976a..f5fffb57 100644
--- a/libsolidity/analysis/ReferencesResolver.cpp
+++ b/libsolidity/analysis/ReferencesResolver.cpp
@@ -166,8 +166,8 @@ bool ReferencesResolver::visit(InlineAssembly const& _inlineAssembly)
// The only purpose of this step is to fill the inline assembly annotation with
// external references.
ErrorList errorsIgnored;
- assembly::ExternalIdentifierAccess::Resolver resolver =
- [&](assembly::Identifier const& _identifier, assembly::IdentifierContext) {
+ julia::ExternalIdentifierAccess::Resolver resolver =
+ [&](assembly::Identifier const& _identifier, julia::IdentifierContext) {
auto declarations = m_resolver.nameFromCurrentScope(_identifier.name);
bool isSlot = boost::algorithm::ends_with(_identifier.name, "_slot");
bool isOffset = boost::algorithm::ends_with(_identifier.name, "_offset");
@@ -194,7 +194,7 @@ bool ReferencesResolver::visit(InlineAssembly const& _inlineAssembly)
// Will be re-generated later with correct information
assembly::AsmAnalysisInfo analysisInfo;
- assembly::AsmAnalyzer(analysisInfo, errorsIgnored, resolver).analyze(_inlineAssembly.operations());
+ assembly::AsmAnalyzer(analysisInfo, errorsIgnored, false, resolver).analyze(_inlineAssembly.operations());
return false;
}
diff --git a/libsolidity/analysis/StaticAnalyzer.cpp b/libsolidity/analysis/StaticAnalyzer.cpp
index 369376fa..cfc863e0 100644
--- a/libsolidity/analysis/StaticAnalyzer.cpp
+++ b/libsolidity/analysis/StaticAnalyzer.cpp
@@ -21,14 +21,13 @@
*/
#include <libsolidity/analysis/StaticAnalyzer.h>
-#include <memory>
#include <libsolidity/ast/AST.h>
+#include <memory>
using namespace std;
using namespace dev;
using namespace dev::solidity;
-
bool StaticAnalyzer::analyze(SourceUnit const& _sourceUnit)
{
_sourceUnit.accept(*this);
@@ -128,3 +127,22 @@ void StaticAnalyzer::warning(SourceLocation const& _location, string const& _des
m_errors.push_back(err);
}
+
+bool StaticAnalyzer::visit(InlineAssembly const& _inlineAssembly)
+{
+ if (!m_currentFunction)
+ return true;
+
+ for (auto const& ref: _inlineAssembly.annotation().externalReferences)
+ {
+ if (auto var = dynamic_cast<VariableDeclaration const*>(ref.second.declaration))
+ {
+ solAssert(!var->name().empty(), "");
+ if (var->isLocalVariable())
+ m_localVarUseCount[var] += 1;
+ }
+ }
+
+ return true;
+}
+
diff --git a/libsolidity/analysis/StaticAnalyzer.h b/libsolidity/analysis/StaticAnalyzer.h
index ab72e7d9..458bab2a 100644
--- a/libsolidity/analysis/StaticAnalyzer.h
+++ b/libsolidity/analysis/StaticAnalyzer.h
@@ -65,6 +65,7 @@ private:
virtual bool visit(Identifier const& _identifier) override;
virtual bool visit(Return const& _return) override;
virtual bool visit(MemberAccess const& _memberAccess) override;
+ virtual bool visit(InlineAssembly const& _inlineAssembly) override;
ErrorList& m_errors;
diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp
index 8161a3a1..528a23da 100644
--- a/libsolidity/analysis/TypeChecker.cpp
+++ b/libsolidity/analysis/TypeChecker.cpp
@@ -631,9 +631,9 @@ bool TypeChecker::visit(InlineAssembly const& _inlineAssembly)
{
// External references have already been resolved in a prior stage and stored in the annotation.
// We run the resolve step again regardless.
- assembly::ExternalIdentifierAccess::Resolver identifierAccess = [&](
+ julia::ExternalIdentifierAccess::Resolver identifierAccess = [&](
assembly::Identifier const& _identifier,
- assembly::IdentifierContext _context
+ julia::IdentifierContext _context
)
{
auto ref = _inlineAssembly.annotation().externalReferences.find(&_identifier);
@@ -650,7 +650,7 @@ bool TypeChecker::visit(InlineAssembly const& _inlineAssembly)
typeError(_identifier.location, "The suffixes _offset and _slot can only be used on storage variables.");
return size_t(-1);
}
- else if (_context != assembly::IdentifierContext::RValue)
+ else if (_context != julia::IdentifierContext::RValue)
{
typeError(_identifier.location, "Storage variables cannot be assigned to.");
return size_t(-1);
@@ -677,13 +677,13 @@ bool TypeChecker::visit(InlineAssembly const& _inlineAssembly)
return size_t(-1);
}
}
- else if (_context == assembly::IdentifierContext::LValue)
+ else if (_context == julia::IdentifierContext::LValue)
{
typeError(_identifier.location, "Only local variables can be assigned to in inline assembly.");
return size_t(-1);
}
- if (_context == assembly::IdentifierContext::RValue)
+ if (_context == julia::IdentifierContext::RValue)
{
solAssert(!!declaration->type(), "Type of declaration required but not yet determined.");
if (dynamic_cast<FunctionDefinition const*>(declaration))
@@ -711,6 +711,7 @@ bool TypeChecker::visit(InlineAssembly const& _inlineAssembly)
assembly::AsmAnalyzer analyzer(
*_inlineAssembly.annotation().analysisInfo,
m_errors,
+ false,
identifierAccess
);
if (!analyzer.analyze(_inlineAssembly.operations()))
diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h
index 71b81a69..ba1d0589 100644
--- a/libsolidity/ast/AST.h
+++ b/libsolidity/ast/AST.h
@@ -873,6 +873,8 @@ public:
std::vector<ASTPointer<VariableDeclaration>> const& parameterTypes() const { return m_parameterTypes->parameters(); }
std::vector<ASTPointer<VariableDeclaration>> const& returnParameterTypes() const { return m_returnTypes->parameters(); }
+ ASTPointer<ParameterList> const& parameterTypeList() const { return m_parameterTypes; }
+ ASTPointer<ParameterList> const& returnParameterTypeList() const { return m_returnTypes; }
Declaration::Visibility visibility() const
{
diff --git a/libsolidity/ast/ASTJsonConverter.cpp b/libsolidity/ast/ASTJsonConverter.cpp
index 40c552a3..cdf70cf9 100644
--- a/libsolidity/ast/ASTJsonConverter.cpp
+++ b/libsolidity/ast/ASTJsonConverter.cpp
@@ -178,7 +178,7 @@ Json::Value ASTJsonConverter::inlineAssemblyIdentifierToJson(pair<assembly::Iden
tuple["declaration"] = idOrNull(_info.second.declaration);
tuple["isSlot"] = Json::Value(_info.second.isSlot);
tuple["isOffset"] = Json::Value(_info.second.isOffset);
- tuple["valueSize"] = _info.second.valueSize;
+ tuple["valueSize"] = Json::Value(Json::LargestUInt(_info.second.valueSize));
return tuple;
}
@@ -420,8 +420,8 @@ bool ASTJsonConverter::visit(FunctionTypeName const& _node)
make_pair("payable", _node.isPayable()),
make_pair("visibility", visibility(_node.visibility())),
make_pair(m_legacy ? "constant" : "isDeclaredConst", _node.isDeclaredConst()),
- make_pair("parameterTypes", toJson(_node.parameterTypes())),
- make_pair("returnParameterTypes", toJson(_node.returnParameterTypes())),
+ make_pair("parameterTypes", toJson(*_node.parameterTypeList())),
+ make_pair("returnParameterTypes", toJson(*_node.returnParameterTypeList())),
make_pair("typeDescriptions", typePointerToJson(_node.annotation().type))
});
return false;
diff --git a/libsolidity/codegen/CompilerContext.cpp b/libsolidity/codegen/CompilerContext.cpp
index 5c4f88c4..404a3af6 100644
--- a/libsolidity/codegen/CompilerContext.cpp
+++ b/libsolidity/codegen/CompilerContext.cpp
@@ -266,10 +266,10 @@ void CompilerContext::appendInlineAssembly(
int startStackHeight = stackHeight();
- assembly::ExternalIdentifierAccess identifierAccess;
+ julia::ExternalIdentifierAccess identifierAccess;
identifierAccess.resolve = [&](
assembly::Identifier const& _identifier,
- assembly::IdentifierContext
+ julia::IdentifierContext
)
{
auto it = std::find(_localVariables.begin(), _localVariables.end(), _identifier.name);
@@ -277,7 +277,7 @@ void CompilerContext::appendInlineAssembly(
};
identifierAccess.generateCode = [&](
assembly::Identifier const& _identifier,
- assembly::IdentifierContext _context,
+ julia::IdentifierContext _context,
julia::AbstractAssembly& _assembly
)
{
@@ -285,14 +285,14 @@ void CompilerContext::appendInlineAssembly(
solAssert(it != _localVariables.end(), "");
int stackDepth = _localVariables.end() - it;
int stackDiff = _assembly.stackHeight() - startStackHeight + stackDepth;
- if (_context == assembly::IdentifierContext::LValue)
+ if (_context == julia::IdentifierContext::LValue)
stackDiff -= 1;
if (stackDiff < 1 || stackDiff > 16)
BOOST_THROW_EXCEPTION(
CompilerError() <<
errinfo_comment("Stack too deep (" + to_string(stackDiff) + "), try removing local variables.")
);
- if (_context == assembly::IdentifierContext::RValue)
+ if (_context == julia::IdentifierContext::RValue)
_assembly.appendInstruction(dupInstruction(stackDiff));
else
{
diff --git a/libsolidity/codegen/ContractCompiler.cpp b/libsolidity/codegen/ContractCompiler.cpp
index 41940e1a..e79bb6dc 100644
--- a/libsolidity/codegen/ContractCompiler.cpp
+++ b/libsolidity/codegen/ContractCompiler.cpp
@@ -522,21 +522,21 @@ bool ContractCompiler::visit(InlineAssembly const& _inlineAssembly)
ErrorList errors;
assembly::CodeGenerator codeGen(errors);
unsigned startStackHeight = m_context.stackHeight();
- assembly::ExternalIdentifierAccess identifierAccess;
- identifierAccess.resolve = [&](assembly::Identifier const& _identifier, assembly::IdentifierContext)
+ julia::ExternalIdentifierAccess identifierAccess;
+ identifierAccess.resolve = [&](assembly::Identifier const& _identifier, julia::IdentifierContext)
{
auto ref = _inlineAssembly.annotation().externalReferences.find(&_identifier);
if (ref == _inlineAssembly.annotation().externalReferences.end())
return size_t(-1);
return ref->second.valueSize;
};
- identifierAccess.generateCode = [&](assembly::Identifier const& _identifier, assembly::IdentifierContext _context, julia::AbstractAssembly& _assembly)
+ identifierAccess.generateCode = [&](assembly::Identifier const& _identifier, julia::IdentifierContext _context, julia::AbstractAssembly& _assembly)
{
auto ref = _inlineAssembly.annotation().externalReferences.find(&_identifier);
solAssert(ref != _inlineAssembly.annotation().externalReferences.end(), "");
Declaration const* decl = ref->second.declaration;
solAssert(!!decl, "");
- if (_context == assembly::IdentifierContext::RValue)
+ if (_context == julia::IdentifierContext::RValue)
{
int const depositBefore = _assembly.stackHeight();
solAssert(!!decl->type(), "Type of declaration required but not yet determined.");
diff --git a/libsolidity/inlineasm/AsmAnalysis.cpp b/libsolidity/inlineasm/AsmAnalysis.cpp
index e03eea2e..eeb7d0a6 100644
--- a/libsolidity/inlineasm/AsmAnalysis.cpp
+++ b/libsolidity/inlineasm/AsmAnalysis.cpp
@@ -38,13 +38,10 @@ using namespace dev;
using namespace dev::solidity;
using namespace dev::solidity::assembly;
-AsmAnalyzer::AsmAnalyzer(
- AsmAnalysisInfo& _analysisInfo,
- ErrorList& _errors,
- ExternalIdentifierAccess::Resolver const& _resolver
-):
- m_resolver(_resolver), m_info(_analysisInfo), m_errors(_errors)
-{
+namespace {
+
+set<string> const builtinTypes{"bool", "u8", "s8", "u32", "s32", "u64", "s64", "u128", "s128", "u256", "s256"};
+
}
bool AsmAnalyzer::analyze(Block const& _block)
@@ -57,12 +54,14 @@ bool AsmAnalyzer::analyze(Block const& _block)
bool AsmAnalyzer::operator()(Label const& _label)
{
+ solAssert(!m_julia, "");
m_info.stackHeightInfo[&_label] = m_stackHeight;
return true;
}
bool AsmAnalyzer::operator()(assembly::Instruction const& _instruction)
{
+ solAssert(!m_julia, "");
auto const& info = instructionInfo(_instruction.instruction);
m_stackHeight += info.ret - info.args;
m_info.stackHeightInfo[&_instruction] = m_stackHeight;
@@ -71,6 +70,7 @@ bool AsmAnalyzer::operator()(assembly::Instruction const& _instruction)
bool AsmAnalyzer::operator()(assembly::Literal const& _literal)
{
+ expectValidType(_literal.type, _literal.location);
++m_stackHeight;
if (_literal.kind == assembly::LiteralKind::String && _literal.value.size() > 32)
{
@@ -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.
@@ -143,6 +143,7 @@ bool AsmAnalyzer::operator()(assembly::Identifier const& _identifier)
bool AsmAnalyzer::operator()(FunctionalInstruction const& _instr)
{
+ solAssert(!m_julia, "");
bool success = true;
for (auto const& arg: _instr.arguments | boost::adaptors::reversed)
{
@@ -160,15 +161,15 @@ bool AsmAnalyzer::operator()(FunctionalInstruction const& _instr)
return success;
}
-bool AsmAnalyzer::operator()(assembly::Assignment const& _assignment)
+bool AsmAnalyzer::operator()(assembly::StackAssignment const& _assignment)
{
+ solAssert(!m_julia, "");
bool success = checkAssignment(_assignment.variableName, size_t(-1));
m_info.stackHeightInfo[&_assignment] = m_stackHeight;
return success;
}
-
-bool AsmAnalyzer::operator()(FunctionalAssignment const& _assignment)
+bool AsmAnalyzer::operator()(assembly::Assignment const& _assignment)
{
int const stackHeight = m_stackHeight;
bool success = boost::apply_visitor(*this, *_assignment.value);
@@ -181,10 +182,24 @@ bool AsmAnalyzer::operator()(FunctionalAssignment const& _assignment)
bool AsmAnalyzer::operator()(assembly::VariableDeclaration const& _varDecl)
{
+ int const expectedItems = _varDecl.variables.size();
int const stackHeight = m_stackHeight;
bool success = boost::apply_visitor(*this, *_varDecl.value);
- solAssert(m_stackHeight - stackHeight == 1, "Invalid value size.");
- boost::get<Scope::Variable>(m_currentScope->identifiers.at(_varDecl.variable.name)).active = true;
+ if ((m_stackHeight - stackHeight) != expectedItems)
+ {
+ m_errors.push_back(make_shared<Error>(
+ Error::Type::DeclarationError,
+ "Variable count mismatch.",
+ _varDecl.location
+ ));
+ return false;
+ }
+
+ for (auto const& variable: _varDecl.variables)
+ {
+ expectValidType(variable.type, variable.location);
+ boost::get<Scope::Variable>(m_currentScope->identifiers.at(variable.name)).active = true;
+ }
m_info.stackHeightInfo[&_varDecl] = m_stackHeight;
return success;
}
@@ -193,7 +208,10 @@ bool AsmAnalyzer::operator()(assembly::FunctionDefinition const& _funDef)
{
Scope& bodyScope = scope(&_funDef.body);
for (auto const& var: _funDef.arguments + _funDef.returns)
+ {
+ expectValidType(var.type, var.location);
boost::get<Scope::Variable>(bodyScope.identifiers.at(var.name)).active = true;
+ }
int const stackHeight = m_stackHeight;
m_stackHeight = _funDef.arguments.size() + _funDef.returns.size();
@@ -274,6 +292,48 @@ bool AsmAnalyzer::operator()(assembly::FunctionCall const& _funCall)
return success;
}
+bool AsmAnalyzer::operator()(Switch const& _switch)
+{
+ bool success = true;
+
+ int const initialStackHeight = m_stackHeight;
+ if (!boost::apply_visitor(*this, *_switch.expression))
+ success = false;
+ expectDeposit(1, initialStackHeight, locationOf(*_switch.expression));
+
+ set<tuple<LiteralKind, string>> cases;
+ for (auto const& _case: _switch.cases)
+ {
+ if (_case.value)
+ {
+ int const initialStackHeight = m_stackHeight;
+ if (!(*this)(*_case.value))
+ success = false;
+ expectDeposit(1, initialStackHeight, _case.value->location);
+ m_stackHeight--;
+
+ /// Note: the parser ensures there is only one default case
+ auto val = make_tuple(_case.value->kind, _case.value->value);
+ if (!cases.insert(val).second)
+ {
+ m_errors.push_back(make_shared<Error>(
+ Error::Type::DeclarationError,
+ "Duplicate case defined",
+ _case.location
+ ));
+ success = false;
+ }
+ }
+
+ if (!(*this)(_case.body))
+ success = false;
+ }
+
+ m_stackHeight--;
+
+ return success;
+}
+
bool AsmAnalyzer::operator()(Block const& _block)
{
bool success = true;
@@ -340,7 +400,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.
@@ -395,7 +455,21 @@ bool AsmAnalyzer::expectDeposit(int const _deposit, int const _oldHeight, Source
Scope& AsmAnalyzer::scope(Block const* _block)
{
+ solAssert(m_info.scopes.count(_block) == 1, "Scope requested but not present.");
auto scopePtr = m_info.scopes.at(_block);
solAssert(scopePtr, "Scope requested but not present.");
return *scopePtr;
}
+
+void AsmAnalyzer::expectValidType(string const& type, SourceLocation const& _location)
+{
+ if (!m_julia)
+ return;
+
+ if (!builtinTypes.count(type))
+ m_errors.push_back(make_shared<Error>(
+ Error::Type::TypeError,
+ "\"" + type + "\" is not a valid type (user defined types are not yet supported).",
+ _location
+ ));
+}
diff --git a/libsolidity/inlineasm/AsmAnalysis.h b/libsolidity/inlineasm/AsmAnalysis.h
index 426ee0d2..87d41e11 100644
--- a/libsolidity/inlineasm/AsmAnalysis.h
+++ b/libsolidity/inlineasm/AsmAnalysis.h
@@ -40,13 +40,14 @@ struct Literal;
struct Block;
struct Label;
struct FunctionalInstruction;
-struct FunctionalAssignment;
+struct Assignment;
struct VariableDeclaration;
struct Instruction;
struct Identifier;
-struct Assignment;
+struct StackAssignment;
struct FunctionDefinition;
struct FunctionCall;
+struct Switch;
struct Scope;
@@ -60,11 +61,12 @@ struct AsmAnalysisInfo;
class AsmAnalyzer: public boost::static_visitor<bool>
{
public:
- AsmAnalyzer(
+ explicit AsmAnalyzer(
AsmAnalysisInfo& _analysisInfo,
ErrorList& _errors,
- ExternalIdentifierAccess::Resolver const& _resolver = ExternalIdentifierAccess::Resolver()
- );
+ bool _julia = false,
+ julia::ExternalIdentifierAccess::Resolver const& _resolver = julia::ExternalIdentifierAccess::Resolver()
+ ): m_resolver(_resolver), m_info(_analysisInfo), m_errors(_errors), m_julia(_julia) {}
bool analyze(assembly::Block const& _block);
@@ -73,11 +75,12 @@ public:
bool operator()(assembly::Identifier const&);
bool operator()(assembly::FunctionalInstruction const& _functionalInstruction);
bool operator()(assembly::Label const& _label);
- bool operator()(assembly::Assignment const&);
- bool operator()(assembly::FunctionalAssignment const& _functionalAssignment);
+ bool operator()(assembly::StackAssignment const&);
+ bool operator()(assembly::Assignment const& _assignment);
bool operator()(assembly::VariableDeclaration const& _variableDeclaration);
bool operator()(assembly::FunctionDefinition const& _functionDefinition);
bool operator()(assembly::FunctionCall const& _functionCall);
+ bool operator()(assembly::Switch const& _switch);
bool operator()(assembly::Block const& _block);
private:
@@ -86,16 +89,18 @@ private:
bool checkAssignment(assembly::Identifier const& _assignment, size_t _valueSize = size_t(-1));
bool expectDeposit(int _deposit, int _oldHeight, SourceLocation const& _location);
Scope& scope(assembly::Block const* _block);
+ void expectValidType(std::string const& type, SourceLocation const& _location);
/// This is used when we enter the body of a function definition. There, the parameters
/// and return parameters appear as variables which are already on the stack before
/// 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;
+ bool m_julia = false;
};
}
diff --git a/libsolidity/inlineasm/AsmAnalysisInfo.h b/libsolidity/inlineasm/AsmAnalysisInfo.h
index e21eb2c5..18382db0 100644
--- a/libsolidity/inlineasm/AsmAnalysisInfo.h
+++ b/libsolidity/inlineasm/AsmAnalysisInfo.h
@@ -36,17 +36,18 @@ struct Literal;
struct Block;
struct Label;
struct FunctionalInstruction;
-struct FunctionalAssignment;
+struct Assignment;
struct VariableDeclaration;
struct Instruction;
struct Identifier;
-struct Assignment;
+struct StackAssignment;
struct FunctionDefinition;
struct FunctionCall;
+struct Switch;
struct Scope;
-using Statement = boost::variant<Instruction, Literal, Label, Assignment, Identifier, FunctionalAssignment, FunctionCall, FunctionalInstruction, VariableDeclaration, FunctionDefinition, Block>;
+using Statement = boost::variant<Instruction, Literal, Label, StackAssignment, Identifier, Assignment, FunctionCall, FunctionalInstruction, VariableDeclaration, FunctionDefinition, Switch, Block>;
struct AsmAnalysisInfo
{
diff --git a/libsolidity/inlineasm/AsmCodeGen.cpp b/libsolidity/inlineasm/AsmCodeGen.cpp
index b8af9dc6..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,246 +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::Assignment const& _assignment)
- {
- m_assembly.setSourceLocation(_assignment.location);
- generateAssignment(_assignment.variableName, _assignment.location);
- checkStackHeight(&_assignment);
- }
- void operator()(FunctionalAssignment 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();
- boost::apply_visitor(*this, *_varDecl.value);
- expectDeposit(1, height);
- auto& var = boost::get<Scope::Variable>(m_scope.identifiers.at(_varDecl.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::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;
}
@@ -354,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/AsmData.h b/libsolidity/inlineasm/AsmData.h
index 8efe1f07..72afeef1 100644
--- a/libsolidity/inlineasm/AsmData.h
+++ b/libsolidity/inlineasm/AsmData.h
@@ -40,6 +40,21 @@ using TypedNameList = std::vector<TypedName>;
/// What follows are the AST nodes for assembly.
+struct Instruction;
+struct Literal;
+struct Label;
+struct StackAssignment;
+struct Identifier;
+struct Assignment;
+struct VariableDeclaration;
+struct FunctionalInstruction;
+struct FunctionDefinition;
+struct FunctionCall;
+struct Switch;
+struct Block;
+
+using Statement = boost::variant<Instruction, Literal, Label, StackAssignment, Identifier, Assignment, FunctionCall, FunctionalInstruction, VariableDeclaration, FunctionDefinition, Switch, Block>;
+
/// Direct EVM instruction (except PUSHi and JUMPDEST)
struct Instruction { SourceLocation location; solidity::Instruction instruction; };
/// Literal number or string (up to 32 bytes)
@@ -47,29 +62,26 @@ enum class LiteralKind { Number, Boolean, String };
struct Literal { SourceLocation location; LiteralKind kind; std::string value; Type type; };
/// External / internal identifier or label reference
struct Identifier { SourceLocation location; std::string name; };
-struct FunctionalInstruction;
/// Jump label ("name:")
struct Label { SourceLocation location; std::string name; };
-/// Assignemnt (":= x", moves stack top into x, potentially multiple slots)
-struct Assignment { SourceLocation location; Identifier variableName; };
-struct FunctionalAssignment;
-struct VariableDeclaration;
-struct FunctionDefinition;
-struct FunctionCall;
-struct Block;
-using Statement = boost::variant<Instruction, Literal, Label, Assignment, Identifier, FunctionalAssignment, FunctionCall, FunctionalInstruction, VariableDeclaration, FunctionDefinition, Block>;
-/// Functional assignment ("x := mload(20:u256)", expects push-1-expression on the right hand
+/// Assignment from stack (":= x", moves stack top into x, potentially multiple slots)
+struct StackAssignment { SourceLocation location; Identifier variableName; };
+/// Assignment ("x := mload(20:u256)", expects push-1-expression on the right hand
/// side and requires x to occupy exactly one stack slot.
-struct FunctionalAssignment { SourceLocation location; Identifier variableName; std::shared_ptr<Statement> value; };
+struct Assignment { SourceLocation location; Identifier variableName; std::shared_ptr<Statement> value; };
/// Functional instruction, e.g. "mul(mload(20:u256), add(2:u256, x))"
struct FunctionalInstruction { SourceLocation location; Instruction instruction; std::vector<Statement> arguments; };
struct FunctionCall { SourceLocation location; Identifier functionName; std::vector<Statement> arguments; };
/// Block-scope variable declaration ("let x:u256 := mload(20:u256)"), non-hoisted
-struct VariableDeclaration { SourceLocation location; TypedName variable; std::shared_ptr<Statement> value; };
+struct VariableDeclaration { SourceLocation location; TypedNameList variables; std::shared_ptr<Statement> value; };
/// Block that creates a scope (frees declared stack variables)
struct Block { SourceLocation location; std::vector<Statement> statements; };
/// Function definition ("function f(a, b) -> (d, e) { ... }")
struct FunctionDefinition { SourceLocation location; std::string name; TypedNameList arguments; TypedNameList returns; Block body; };
+/// Switch case or default case
+struct Case { SourceLocation location; std::shared_ptr<Literal> value; Block body; };
+/// Switch statement
+struct Switch { SourceLocation location; std::shared_ptr<Statement> expression; std::vector<Case> cases; };
struct LocationExtractor: boost::static_visitor<SourceLocation>
{
diff --git a/libsolidity/inlineasm/AsmParser.cpp b/libsolidity/inlineasm/AsmParser.cpp
index a96984f5..80409c63 100644
--- a/libsolidity/inlineasm/AsmParser.cpp
+++ b/libsolidity/inlineasm/AsmParser.cpp
@@ -50,16 +50,16 @@ assembly::Block Parser::parseBlock()
{
assembly::Block block = createWithLocation<Block>();
expectToken(Token::LBrace);
- while (m_scanner->currentToken() != Token::RBrace)
+ while (currentToken() != Token::RBrace)
block.statements.emplace_back(parseStatement());
block.location.end = endPosition();
- m_scanner->next();
+ advance();
return block;
}
assembly::Statement Parser::parseStatement()
{
- switch (m_scanner->currentToken())
+ switch (currentToken())
{
case Token::Let:
return parseVariableDeclaration();
@@ -67,24 +67,41 @@ assembly::Statement Parser::parseStatement()
return parseFunctionDefinition();
case Token::LBrace:
return parseBlock();
+ case Token::Switch:
+ {
+ assembly::Switch _switch = createWithLocation<assembly::Switch>();
+ m_scanner->next();
+ _switch.expression = make_shared<Statement>(parseExpression());
+ if (_switch.expression->type() == typeid(assembly::Instruction))
+ fatalParserError("Instructions are not supported as expressions for switch.");
+ while (m_scanner->currentToken() == Token::Case)
+ _switch.cases.emplace_back(parseCase());
+ if (m_scanner->currentToken() == Token::Default)
+ _switch.cases.emplace_back(parseCase());
+ if (m_scanner->currentToken() == Token::Default)
+ fatalParserError("Only one default case allowed.");
+ else if (m_scanner->currentToken() == Token::Case)
+ fatalParserError("Case not allowed after default case.");
+ if (_switch.cases.size() == 0)
+ fatalParserError("Switch statement without any cases.");
+ _switch.location.end = _switch.cases.back().body.location.end;
+ return _switch;
+ }
case Token::Assign:
{
if (m_julia)
break;
- assembly::Assignment assignment = createWithLocation<assembly::Assignment>();
- m_scanner->next();
+ assembly::StackAssignment assignment = createWithLocation<assembly::StackAssignment>();
+ advance();
expectToken(Token::Colon);
assignment.variableName.location = location();
- assignment.variableName.name = m_scanner->currentLiteral();
+ assignment.variableName.name = currentLiteral();
if (!m_julia && instructions().count(assignment.variableName.name))
fatalParserError("Identifier expected, got instruction name.");
assignment.location.end = endPosition();
expectToken(Token::Identifier);
return assignment;
}
- case Token::Return: // opcode
- case Token::Byte: // opcode
- case Token::Address: // opcode
default:
break;
}
@@ -93,29 +110,28 @@ assembly::Statement Parser::parseStatement()
// literal,
// identifier (might turn into label or functional assignment)
Statement statement(parseElementaryOperation(false));
- switch (m_scanner->currentToken())
+ switch (currentToken())
{
case Token::LParen:
- return parseFunctionalInstruction(std::move(statement));
+ return parseCall(std::move(statement));
case Token::Colon:
{
if (statement.type() != typeid(assembly::Identifier))
fatalParserError("Label name / variable name must precede \":\".");
assembly::Identifier const& identifier = boost::get<assembly::Identifier>(statement);
- m_scanner->next();
+ advance();
// identifier:=: should be parsed as identifier: =: (i.e. a label),
// while identifier:= (being followed by a non-colon) as identifier := (assignment).
- if (m_scanner->currentToken() == Token::Assign && m_scanner->peekNextToken() != Token::Colon)
+ if (currentToken() == Token::Assign && peekNextToken() != Token::Colon)
{
- // functional assignment
- FunctionalAssignment funAss = createWithLocation<FunctionalAssignment>(identifier.location);
+ assembly::Assignment assignment = createWithLocation<assembly::Assignment>(identifier.location);
if (!m_julia && instructions().count(identifier.name))
fatalParserError("Cannot use instruction names for identifier names.");
- m_scanner->next();
- funAss.variableName = identifier;
- funAss.value.reset(new Statement(parseExpression()));
- funAss.location.end = locationOf(*funAss.value).end;
- return funAss;
+ advance();
+ assignment.variableName = identifier;
+ assignment.value.reset(new Statement(parseExpression()));
+ assignment.location.end = locationOf(*assignment.value).end;
+ return assignment;
}
else
{
@@ -135,11 +151,31 @@ assembly::Statement Parser::parseStatement()
return statement;
}
+assembly::Case Parser::parseCase()
+{
+ assembly::Case _case = createWithLocation<assembly::Case>();
+ if (m_scanner->currentToken() == Token::Default)
+ m_scanner->next();
+ else if (m_scanner->currentToken() == Token::Case)
+ {
+ m_scanner->next();
+ assembly::Statement statement = parseElementaryOperation();
+ if (statement.type() != typeid(assembly::Literal))
+ fatalParserError("Literal expected.");
+ _case.value = make_shared<Literal>(std::move(boost::get<assembly::Literal>(statement)));
+ }
+ else
+ fatalParserError("Case or default case expected.");
+ _case.body = parseBlock();
+ _case.location.end = _case.body.location.end;
+ return _case;
+}
+
assembly::Statement Parser::parseExpression()
{
Statement operation = parseElementaryOperation(true);
- if (m_scanner->currentToken() == Token::LParen)
- return parseFunctionalInstruction(std::move(operation));
+ if (currentToken() == Token::LParen)
+ return parseCall(std::move(operation));
else
return operation;
}
@@ -171,7 +207,7 @@ std::map<string, dev::solidity::Instruction> const& Parser::instructions()
assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher)
{
Statement ret;
- switch (m_scanner->currentToken())
+ switch (currentToken())
{
case Token::Identifier:
case Token::Return:
@@ -179,14 +215,14 @@ assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher)
case Token::Address:
{
string literal;
- if (m_scanner->currentToken() == Token::Return)
+ if (currentToken() == Token::Return)
literal = "return";
- else if (m_scanner->currentToken() == Token::Byte)
+ else if (currentToken() == Token::Byte)
literal = "byte";
- else if (m_scanner->currentToken() == Token::Address)
+ else if (currentToken() == Token::Address)
literal = "address";
else
- literal = m_scanner->currentLiteral();
+ literal = currentLiteral();
// first search the set of instructions.
if (!m_julia && instructions().count(literal))
{
@@ -201,7 +237,7 @@ assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher)
}
else
ret = Identifier{location(), literal};
- m_scanner->next();
+ advance();
break;
}
case Token::StringLiteral:
@@ -210,7 +246,7 @@ assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher)
case Token::FalseLiteral:
{
LiteralKind kind = LiteralKind::Number;
- switch (m_scanner->currentToken())
+ switch (currentToken())
{
case Token::StringLiteral:
kind = LiteralKind::String;
@@ -229,10 +265,10 @@ assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher)
Literal literal{
location(),
kind,
- m_scanner->currentLiteral(),
+ currentLiteral(),
""
};
- m_scanner->next();
+ advance();
if (m_julia)
{
expectToken(Token::Colon);
@@ -248,7 +284,7 @@ assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher)
fatalParserError(
m_julia ?
"Literal or identifier expected." :
- "Expected elementary inline assembly operation."
+ "Literal, identifier or instruction expected."
);
}
return ret;
@@ -258,7 +294,14 @@ assembly::VariableDeclaration Parser::parseVariableDeclaration()
{
VariableDeclaration varDecl = createWithLocation<VariableDeclaration>();
expectToken(Token::Let);
- varDecl.variable = parseTypedName();
+ while (true)
+ {
+ varDecl.variables.emplace_back(parseTypedName());
+ if (currentToken() == Token::Comma)
+ expectToken(Token::Comma);
+ else
+ break;
+ }
expectToken(Token::Colon);
expectToken(Token::Assign);
varDecl.value.reset(new Statement(parseExpression()));
@@ -272,22 +315,22 @@ assembly::FunctionDefinition Parser::parseFunctionDefinition()
expectToken(Token::Function);
funDef.name = expectAsmIdentifier();
expectToken(Token::LParen);
- while (m_scanner->currentToken() != Token::RParen)
+ while (currentToken() != Token::RParen)
{
funDef.arguments.emplace_back(parseTypedName());
- if (m_scanner->currentToken() == Token::RParen)
+ if (currentToken() == Token::RParen)
break;
expectToken(Token::Comma);
}
expectToken(Token::RParen);
- if (m_scanner->currentToken() == Token::Sub)
+ if (currentToken() == Token::Sub)
{
expectToken(Token::Sub);
expectToken(Token::GreaterThan);
while (true)
{
funDef.returns.emplace_back(parseTypedName());
- if (m_scanner->currentToken() == Token::LBrace)
+ if (currentToken() == Token::LBrace)
break;
expectToken(Token::Comma);
}
@@ -297,7 +340,7 @@ assembly::FunctionDefinition Parser::parseFunctionDefinition()
return funDef;
}
-assembly::Statement Parser::parseFunctionalInstruction(assembly::Statement&& _instruction)
+assembly::Statement Parser::parseCall(assembly::Statement&& _instruction)
{
if (_instruction.type() == typeid(Instruction))
{
@@ -315,10 +358,20 @@ assembly::Statement Parser::parseFunctionalInstruction(assembly::Statement&& _in
unsigned args = unsigned(instrInfo.args);
for (unsigned i = 0; i < args; ++i)
{
+ /// check for premature closing parentheses
+ if (currentToken() == Token::RParen)
+ fatalParserError(string(
+ "Expected expression (" +
+ instrInfo.name +
+ " expects " +
+ boost::lexical_cast<string>(args) +
+ " arguments)"
+ ));
+
ret.arguments.emplace_back(parseExpression());
if (i != args - 1)
{
- if (m_scanner->currentToken() != Token::Comma)
+ if (currentToken() != Token::Comma)
fatalParserError(string(
"Expected comma (" +
instrInfo.name +
@@ -327,11 +380,11 @@ assembly::Statement Parser::parseFunctionalInstruction(assembly::Statement&& _in
" arguments)"
));
else
- m_scanner->next();
+ advance();
}
}
ret.location.end = endPosition();
- if (m_scanner->currentToken() == Token::Comma)
+ if (currentToken() == Token::Comma)
fatalParserError(
string("Expected ')' (" + instrInfo.name + " expects " + boost::lexical_cast<string>(args) + " arguments)")
);
@@ -344,10 +397,10 @@ assembly::Statement Parser::parseFunctionalInstruction(assembly::Statement&& _in
ret.functionName = std::move(boost::get<Identifier>(_instruction));
ret.location = ret.functionName.location;
expectToken(Token::LParen);
- while (m_scanner->currentToken() != Token::RParen)
+ while (currentToken() != Token::RParen)
{
ret.arguments.emplace_back(parseExpression());
- if (m_scanner->currentToken() == Token::RParen)
+ if (currentToken() == Token::RParen)
break;
expectToken(Token::Comma);
}
@@ -380,12 +433,12 @@ TypedName Parser::parseTypedName()
string Parser::expectAsmIdentifier()
{
- string name = m_scanner->currentLiteral();
+ string name = currentLiteral();
if (m_julia)
{
- if (m_scanner->currentToken() == Token::Bool)
+ if (currentToken() == Token::Bool)
{
- m_scanner->next();
+ advance();
return name;
}
}
diff --git a/libsolidity/inlineasm/AsmParser.h b/libsolidity/inlineasm/AsmParser.h
index addc1725..138af337 100644
--- a/libsolidity/inlineasm/AsmParser.h
+++ b/libsolidity/inlineasm/AsmParser.h
@@ -62,13 +62,14 @@ protected:
Block parseBlock();
Statement parseStatement();
+ Case parseCase();
/// Parses a functional expression that has to push exactly one stack element
Statement parseExpression();
std::map<std::string, dev::solidity::Instruction> const& instructions();
Statement parseElementaryOperation(bool _onlySinglePusher = false);
VariableDeclaration parseVariableDeclaration();
FunctionDefinition parseFunctionDefinition();
- Statement parseFunctionalInstruction(Statement&& _instruction);
+ Statement parseCall(Statement&& _instruction);
TypedName parseTypedName();
std::string expectAsmIdentifier();
diff --git a/libsolidity/inlineasm/AsmPrinter.cpp b/libsolidity/inlineasm/AsmPrinter.cpp
index 636e61b8..e282e5e8 100644
--- a/libsolidity/inlineasm/AsmPrinter.cpp
+++ b/libsolidity/inlineasm/AsmPrinter.cpp
@@ -108,20 +108,29 @@ string AsmPrinter::operator()(assembly::Label const& _label)
return _label.name + ":";
}
-string AsmPrinter::operator()(assembly::Assignment const& _assignment)
+string AsmPrinter::operator()(assembly::StackAssignment const& _assignment)
{
solAssert(!m_julia, "");
return "=: " + (*this)(_assignment.variableName);
}
-string AsmPrinter::operator()(assembly::FunctionalAssignment const& _functionalAssignment)
+string AsmPrinter::operator()(assembly::Assignment const& _assignment)
{
- return (*this)(_functionalAssignment.variableName) + " := " + boost::apply_visitor(*this, *_functionalAssignment.value);
+ return (*this)(_assignment.variableName) + " := " + boost::apply_visitor(*this, *_assignment.value);
}
string AsmPrinter::operator()(assembly::VariableDeclaration const& _variableDeclaration)
{
- return "let " + _variableDeclaration.variable.name + appendTypeName(_variableDeclaration.variable.type) + " := " + boost::apply_visitor(*this, *_variableDeclaration.value);
+ string out = "let ";
+ out += boost::algorithm::join(
+ _variableDeclaration.variables | boost::adaptors::transformed(
+ [this](TypedName variable) { return variable.name + appendTypeName(variable.type); }
+ ),
+ ", "
+ );
+ out += " := ";
+ out += boost::apply_visitor(*this, *_variableDeclaration.value);
+ return out;
}
string AsmPrinter::operator()(assembly::FunctionDefinition const& _functionDefinition)
@@ -158,6 +167,20 @@ string AsmPrinter::operator()(assembly::FunctionCall const& _functionCall)
")";
}
+string AsmPrinter::operator()(Switch const& _switch)
+{
+ string out = "switch " + boost::apply_visitor(*this, *_switch.expression);
+ for (auto const& _case: _switch.cases)
+ {
+ if (!_case.value)
+ out += "\ndefault ";
+ else
+ out += "\ncase " + (*this)(*_case.value) + " ";
+ out += (*this)(_case.body);
+ }
+ return out;
+}
+
string AsmPrinter::operator()(Block const& _block)
{
if (_block.statements.empty())
diff --git a/libsolidity/inlineasm/AsmPrinter.h b/libsolidity/inlineasm/AsmPrinter.h
index 282fd7e3..b0d7fc09 100644
--- a/libsolidity/inlineasm/AsmPrinter.h
+++ b/libsolidity/inlineasm/AsmPrinter.h
@@ -35,11 +35,12 @@ struct Literal;
struct Identifier;
struct FunctionalInstruction;
struct Label;
+struct StackAssignment;
struct Assignment;
-struct FunctionalAssignment;
struct VariableDeclaration;
struct FunctionDefinition;
struct FunctionCall;
+struct Switch;
struct Block;
class AsmPrinter: public boost::static_visitor<std::string>
@@ -52,11 +53,12 @@ public:
std::string operator()(assembly::Identifier const& _identifier);
std::string operator()(assembly::FunctionalInstruction const& _functionalInstruction);
std::string operator()(assembly::Label const& _label);
+ std::string operator()(assembly::StackAssignment const& _assignment);
std::string operator()(assembly::Assignment const& _assignment);
- std::string operator()(assembly::FunctionalAssignment const& _functionalAssignment);
std::string operator()(assembly::VariableDeclaration const& _variableDeclaration);
std::string operator()(assembly::FunctionDefinition const& _functionDefinition);
std::string operator()(assembly::FunctionCall const& _functionCall);
+ std::string operator()(assembly::Switch const& _switch);
std::string operator()(assembly::Block const& _block);
private:
diff --git a/libsolidity/inlineasm/AsmScopeFiller.cpp b/libsolidity/inlineasm/AsmScopeFiller.cpp
index eb10dbb3..7eb6a9ed 100644
--- a/libsolidity/inlineasm/AsmScopeFiller.cpp
+++ b/libsolidity/inlineasm/AsmScopeFiller.cpp
@@ -59,7 +59,10 @@ bool ScopeFiller::operator()(Label const& _item)
bool ScopeFiller::operator()(assembly::VariableDeclaration const& _varDecl)
{
- return registerVariable(_varDecl.variable, _varDecl.location, *m_currentScope);
+ for (auto const& variable: _varDecl.variables)
+ if (!registerVariable(variable, _varDecl.location, *m_currentScope))
+ return false;
+ return true;
}
bool ScopeFiller::operator()(assembly::FunctionDefinition const& _funDef)
@@ -94,6 +97,15 @@ bool ScopeFiller::operator()(assembly::FunctionDefinition const& _funDef)
return success;
}
+bool ScopeFiller::operator()(Switch const& _switch)
+{
+ bool success = true;
+ for (auto const& _case: _switch.cases)
+ if (!(*this)(_case.body))
+ success = false;
+ return success;
+}
+
bool ScopeFiller::operator()(Block const& _block)
{
bool success = true;
diff --git a/libsolidity/inlineasm/AsmScopeFiller.h b/libsolidity/inlineasm/AsmScopeFiller.h
index 61428eea..c7179b3b 100644
--- a/libsolidity/inlineasm/AsmScopeFiller.h
+++ b/libsolidity/inlineasm/AsmScopeFiller.h
@@ -39,13 +39,14 @@ struct Literal;
struct Block;
struct Label;
struct FunctionalInstruction;
-struct FunctionalAssignment;
+struct Assignment;
struct VariableDeclaration;
struct Instruction;
struct Identifier;
-struct Assignment;
+struct StackAssignment;
struct FunctionDefinition;
struct FunctionCall;
+struct Switch;
struct Scope;
@@ -64,11 +65,12 @@ public:
bool operator()(assembly::Identifier const&) { return true; }
bool operator()(assembly::FunctionalInstruction const&) { return true; }
bool operator()(assembly::Label const& _label);
+ bool operator()(assembly::StackAssignment const&) { return true; }
bool operator()(assembly::Assignment const&) { return true; }
- bool operator()(assembly::FunctionalAssignment const&) { return true; }
bool operator()(assembly::VariableDeclaration const& _variableDeclaration);
bool operator()(assembly::FunctionDefinition const& _functionDefinition);
bool operator()(assembly::FunctionCall const&) { return true; }
+ bool operator()(assembly::Switch const& _switch);
bool operator()(assembly::Block const& _block);
private:
diff --git a/libsolidity/inlineasm/AsmStack.cpp b/libsolidity/inlineasm/AsmStack.cpp
index c2a7d8ea..fe443c08 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>();
@@ -53,7 +53,7 @@ bool InlineAssemblyStack::parse(
*m_parserResult = std::move(*result);
AsmAnalysisInfo analysisInfo;
- return (AsmAnalyzer(analysisInfo, m_errors, _resolver)).analyze(*m_parserResult);
+ return (AsmAnalyzer(analysisInfo, m_errors, false, _resolver)).analyze(*m_parserResult);
}
string InlineAssemblyStack::toString()
@@ -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;
@@ -84,7 +84,7 @@ bool InlineAssemblyStack::parseAndAssemble(
solAssert(parserResult, "");
AsmAnalysisInfo analysisInfo;
- AsmAnalyzer analyzer(analysisInfo, errors, _identifierAccess.resolve);
+ AsmAnalyzer analyzer(analysisInfo, errors, false, _identifierAccess.resolve);
solAssert(analyzer.analyze(*parserResult), "");
CodeGenerator(errors).assemble(*parserResult, analysisInfo, _assembly, _identifierAccess);
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; }
diff --git a/libsolidity/interface/AssemblyStack.cpp b/libsolidity/interface/AssemblyStack.cpp
new file mode 100644
index 00000000..c4bd63c4
--- /dev/null
+++ b/libsolidity/interface/AssemblyStack.cpp
@@ -0,0 +1,85 @@
+/*
+ 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/>.
+*/
+/**
+ * Full assembly stack that can support EVM-assembly and JULIA as input and EVM, EVM1.5 and
+ * eWasm as output.
+ */
+
+
+#include <libsolidity/interface/AssemblyStack.h>
+
+#include <libsolidity/parsing/Scanner.h>
+#include <libsolidity/inlineasm/AsmPrinter.h>
+#include <libsolidity/inlineasm/AsmParser.h>
+#include <libsolidity/inlineasm/AsmAnalysis.h>
+#include <libsolidity/inlineasm/AsmCodeGen.h>
+
+#include <libevmasm/Assembly.h>
+
+using namespace std;
+using namespace dev;
+using namespace dev::solidity;
+
+
+Scanner const& AssemblyStack::scanner() const
+{
+ solAssert(m_scanner, "");
+ return *m_scanner;
+}
+
+bool AssemblyStack::parseAndAnalyze(std::string const& _sourceName, std::string const& _source)
+{
+ m_analysisSuccessful = false;
+ m_scanner = make_shared<Scanner>(CharStream(_source), _sourceName);
+ m_parserResult = assembly::Parser(m_errors, m_language == Language::JULIA).parse(m_scanner);
+ if (!m_errors.empty())
+ return false;
+ solAssert(m_parserResult, "");
+
+ m_analysisInfo = make_shared<assembly::AsmAnalysisInfo>();
+ assembly::AsmAnalyzer analyzer(*m_analysisInfo, m_errors);
+ m_analysisSuccessful = analyzer.analyze(*m_parserResult);
+ return m_analysisSuccessful;
+}
+
+eth::LinkerObject AssemblyStack::assemble(Machine _machine)
+{
+ solAssert(m_analysisSuccessful, "");
+ solAssert(m_parserResult, "");
+ solAssert(m_analysisInfo, "");
+
+ switch (_machine)
+ {
+ case Machine::EVM:
+ {
+ auto assembly = assembly::CodeGenerator(m_errors).assemble(*m_parserResult, *m_analysisInfo);
+ return assembly.assemble();
+ }
+ case Machine::EVM15:
+ solUnimplemented("EVM 1.5 backend is not yet implemented.");
+ case Machine::eWasm:
+ solUnimplemented("eWasm backend is not yet implemented.");
+ }
+ // unreachable
+ return eth::LinkerObject();
+}
+
+string AssemblyStack::print()
+{
+ solAssert(m_parserResult, "");
+ return assembly::AsmPrinter(m_language == Language::JULIA)(*m_parserResult);
+}
diff --git a/libsolidity/interface/AssemblyStack.h b/libsolidity/interface/AssemblyStack.h
new file mode 100644
index 00000000..40662ac3
--- /dev/null
+++ b/libsolidity/interface/AssemblyStack.h
@@ -0,0 +1,85 @@
+/*
+ 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/>.
+*/
+/**
+ * Full assembly stack that can support EVM-assembly and JULIA as input and EVM, EVM1.5 and
+ * eWasm as output.
+ */
+
+#pragma once
+
+#include <libsolidity/interface/Exceptions.h>
+#include <libsolidity/inlineasm/AsmAnalysisInfo.h>
+#include <libevmasm/LinkerObject.h>
+
+#include <string>
+#include <memory>
+
+namespace dev
+{
+namespace solidity
+{
+class Scanner;
+namespace assembly
+{
+struct AsmAnalysisInfo;
+struct Block;
+}
+
+/*
+ * Full assembly stack that can support EVM-assembly and JULIA as input and EVM, EVM1.5 and
+ * eWasm as output.
+ */
+class AssemblyStack
+{
+public:
+ enum class Language { JULIA, Assembly };
+ enum class Machine { EVM, EVM15, eWasm };
+
+ explicit AssemblyStack(Language _language = Language::Assembly):
+ m_language(_language)
+ {}
+
+ /// @returns the scanner used during parsing
+ Scanner const& scanner() const;
+
+ /// Runs parsing and analysis steps, returns false if input cannot be assembled.
+ /// Multiple calls overwrite the previous state.
+ bool parseAndAnalyze(std::string const& _sourceName, std::string const& _source);
+
+ /// Run the assembly step (should only be called after parseAndAnalyze).
+ eth::LinkerObject assemble(Machine _machine);
+
+ /// @returns the errors generated during parsing, analysis (and potentially assembly).
+ ErrorList const& errors() const { return m_errors; }
+
+ /// Pretty-print the input after having parsed it.
+ std::string print();
+
+private:
+
+ Language m_language = Language::Assembly;
+
+ std::shared_ptr<Scanner> m_scanner;
+
+ bool m_analysisSuccessful = false;
+ std::shared_ptr<assembly::Block> m_parserResult;
+ std::shared_ptr<assembly::AsmAnalysisInfo> m_analysisInfo;
+ ErrorList m_errors;
+};
+
+}
+}
diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp
index 72712298..328df91f 100644
--- a/libsolidity/interface/CompilerStack.cpp
+++ b/libsolidity/interface/CompilerStack.cpp
@@ -507,8 +507,8 @@ string const& CompilerStack::onChainMetadata(string const& _contractName) const
Scanner const& CompilerStack::scanner(string const& _sourceName) const
{
- if (m_stackState < ParsingSuccessful)
- BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
+ if (m_stackState < SourcesSet)
+ BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("No sources set."));
return *source(_sourceName).scanner;
}
diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp
index 72150a3e..5b318b2b 100644
--- a/libsolidity/parsing/Parser.cpp
+++ b/libsolidity/parsing/Parser.cpp
@@ -1437,5 +1437,49 @@ ASTPointer<ParameterList> Parser::createEmptyParameterList()
return nodeFactory.createNode<ParameterList>(vector<ASTPointer<VariableDeclaration>>());
}
+string Parser::currentTokenName()
+{
+ Token::Value token = m_scanner->currentToken();
+ if (Token::isElementaryTypeName(token)) //for the sake of accuracy in reporting
+ {
+ ElementaryTypeNameToken elemTypeName = m_scanner->currentElementaryTypeNameToken();
+ return elemTypeName.toString();
+ }
+ else
+ return Token::name(token);
+}
+
+Token::Value Parser::expectAssignmentOperator()
+{
+ Token::Value op = m_scanner->currentToken();
+ if (!Token::isAssignmentOp(op))
+ fatalParserError(
+ string("Expected assignment operator, got '") +
+ currentTokenName() +
+ string("'")
+ );
+ m_scanner->next();
+ return op;
+}
+
+ASTPointer<ASTString> Parser::expectIdentifierToken()
+{
+ Token::Value id = m_scanner->currentToken();
+ if (id != Token::Identifier)
+ fatalParserError(
+ string("Expected identifier, got '") +
+ currentTokenName() +
+ string("'")
+ );
+ return getLiteralAndAdvance();
+}
+
+ASTPointer<ASTString> Parser::getLiteralAndAdvance()
+{
+ ASTPointer<ASTString> identifier = make_shared<ASTString>(m_scanner->currentLiteral());
+ m_scanner->next();
+ return identifier;
+}
+
}
}
diff --git a/libsolidity/parsing/Parser.h b/libsolidity/parsing/Parser.h
index 282617ab..5ec3dbed 100644
--- a/libsolidity/parsing/Parser.h
+++ b/libsolidity/parsing/Parser.h
@@ -154,6 +154,11 @@ private:
std::vector<ASTPointer<PrimaryExpression>> const& _path,
std::vector<std::pair<ASTPointer<Expression>, SourceLocation>> const& _indices
);
+
+ std::string currentTokenName();
+ Token::Value expectAssignmentOperator();
+ ASTPointer<ASTString> expectIdentifierToken();
+ ASTPointer<ASTString> getLiteralAndAdvance();
///@}
/// Creates an empty ParameterList at the current location (used if parameters can be omitted).
diff --git a/libsolidity/parsing/ParserBase.cpp b/libsolidity/parsing/ParserBase.cpp
index 87d47f4b..ac103bda 100644
--- a/libsolidity/parsing/ParserBase.cpp
+++ b/libsolidity/parsing/ParserBase.cpp
@@ -80,62 +80,6 @@ void ParserBase::expectToken(Token::Value _value)
m_scanner->next();
}
-Token::Value ParserBase::expectAssignmentOperator()
-{
- Token::Value op = m_scanner->currentToken();
- if (!Token::isAssignmentOp(op))
- {
- if (Token::isElementaryTypeName(op)) //for the sake of accuracy in reporting
- {
- ElementaryTypeNameToken elemTypeName = m_scanner->currentElementaryTypeNameToken();
- fatalParserError(
- string("Expected assignment operator, got '") +
- elemTypeName.toString() +
- string("'")
- );
- }
- else
- fatalParserError(
- string("Expected assignment operator, got '") +
- string(Token::name(m_scanner->currentToken())) +
- string("'")
- );
- }
- m_scanner->next();
- return op;
-}
-
-ASTPointer<ASTString> ParserBase::expectIdentifierToken()
-{
- Token::Value id = m_scanner->currentToken();
- if (id != Token::Identifier)
- {
- if (Token::isElementaryTypeName(id)) //for the sake of accuracy in reporting
- {
- ElementaryTypeNameToken elemTypeName = m_scanner->currentElementaryTypeNameToken();
- fatalParserError(
- string("Expected identifier, got '") +
- elemTypeName.toString() +
- string("'")
- );
- }
- else
- fatalParserError(
- string("Expected identifier, got '") +
- string(Token::name(id)) +
- string("'")
- );
- }
- return getLiteralAndAdvance();
-}
-
-ASTPointer<ASTString> ParserBase::getLiteralAndAdvance()
-{
- ASTPointer<ASTString> identifier = make_shared<ASTString>(m_scanner->currentLiteral());
- m_scanner->next();
- return identifier;
-}
-
void ParserBase::parserError(string const& _description)
{
auto err = make_shared<Error>(Error::Type::ParserError);
diff --git a/libsolidity/parsing/ParserBase.h b/libsolidity/parsing/ParserBase.h
index dfb7cab7..18b39a5e 100644
--- a/libsolidity/parsing/ParserBase.h
+++ b/libsolidity/parsing/ParserBase.h
@@ -24,8 +24,8 @@
#include <memory>
#include <libsolidity/interface/Exceptions.h>
+#include <libsolidity/parsing/Scanner.h>
#include <libsolidity/parsing/Token.h>
-#include <libsolidity/ast/ASTForward.h>
namespace dev
{
@@ -47,14 +47,14 @@ protected:
/// End position of the current token
int endPosition() const;
-
///@{
///@name Helper functions
/// If current token value is not _value, throw exception otherwise advance token.
void expectToken(Token::Value _value);
- Token::Value expectAssignmentOperator();
- ASTPointer<ASTString> expectIdentifierToken();
- ASTPointer<ASTString> getLiteralAndAdvance();
+ Token::Value currentToken() const { return m_scanner->currentToken(); }
+ Token::Value peekNextToken() const { return m_scanner->peekNextToken(); }
+ std::string currentLiteral() const { return m_scanner->currentLiteral(); }
+ Token::Value advance() { return m_scanner->next(); }
///@}
/// Creates a @ref ParserError and annotates it with the current position and the
diff --git a/scripts/bytecodecompare/storebytecode.sh b/scripts/bytecodecompare/storebytecode.sh
index b3491cfa..653c86ba 100755
--- a/scripts/bytecodecompare/storebytecode.sh
+++ b/scripts/bytecodecompare/storebytecode.sh
@@ -87,17 +87,15 @@ EOF
eval `ssh-agent -s`
ssh-add deploy_key
- COMMIT_DATE=$(cd "$REPO_ROOT" && git show -s --format="%cd" --date=short "$TRAVIS_COMMIT")
-
git clone --depth 2 git@github.com:ethereum/solidity-test-bytecode.git
cd solidity-test-bytecode
git config user.name "travis"
git config user.email "chris@ethereum.org"
git clean -f -d -x
- DIRECTORY=$COMMIT_DATE"_"$TRAVIS_COMMIT
- mkdir -p "$DIRECTORY"
- REPORT="$DIRECTORY/$ZIP_SUFFIX.txt"
+ DIRNAME=$(cd "$REPO_ROOT" && git show -s --format="%cd-H" --date=short)
+ mkdir -p "$DIRNAME"
+ REPORT="$DIRNAME/$ZIP_SUFFIX.txt"
cp ../report.txt "$REPORT"
# Only push if adding actually worked, i.e. there were changes.
if git add "$REPORT"
@@ -110,4 +108,4 @@ EOF
fi
fi
)
-rm -rf "$TMPDIR" \ No newline at end of file
+rm -rf "$TMPDIR"
diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp
index d21fccaa..b2e257ee 100644
--- a/solc/CommandLineInterface.cpp
+++ b/solc/CommandLineInterface.cpp
@@ -35,6 +35,7 @@
#include <libsolidity/interface/StandardCompiler.h>
#include <libsolidity/interface/SourceReferenceFormatter.h>
#include <libsolidity/interface/GasEstimator.h>
+#include <libsolidity/interface/AssemblyStack.h>
#include <libsolidity/formal/Why3Translator.h>
#include <libevmasm/Instruction.h>
@@ -84,11 +85,15 @@ static string const g_strCloneBinary = "clone-bin";
static string const g_strCombinedJson = "combined-json";
static string const g_strCompactJSON = "compact-format";
static string const g_strContracts = "contracts";
+static string const g_strEVM = "evm";
+static string const g_strEVM15 = "evm15";
+static string const g_streWasm = "ewasm";
static string const g_strFormal = "formal";
static string const g_strGas = "gas";
static string const g_strHelp = "help";
static string const g_strInputFile = "input-file";
static string const g_strInterface = "interface";
+static string const g_strJulia = "julia";
static string const g_strLibraries = "libraries";
static string const g_strLink = "link";
static string const g_strMetadata = "metadata";
@@ -126,8 +131,10 @@ static string const g_argFormal = g_strFormal;
static string const g_argGas = g_strGas;
static string const g_argHelp = g_strHelp;
static string const g_argInputFile = g_strInputFile;
+static string const g_argJulia = "julia";
static string const g_argLibraries = g_strLibraries;
static string const g_argLink = g_strLink;
+static string const g_argMachine = "machine";
static string const g_argMetadata = g_strMetadata;
static string const g_argMetadataLiteral = g_strMetadataLiteral;
static string const g_argNatspecDev = g_strNatspecDev;
@@ -160,6 +167,14 @@ static set<string> const g_combinedJsonArgs
g_strSrcMapRuntime
};
+/// Possible arguments to for --machine
+static set<string> const g_machineArgs
+{
+ g_strEVM,
+ g_strEVM15,
+ g_streWasm
+};
+
static void version()
{
cout <<
@@ -545,7 +560,16 @@ Allowed options)",
)
(
g_argAssemble.c_str(),
- "Switch to assembly mode, ignoring all options and assumes input is assembly."
+ "Switch to assembly mode, ignoring all options except --machine and assumes input is assembly."
+ )
+ (
+ g_argJulia.c_str(),
+ "Switch to JULIA mode, ignoring all options except --machine and assumes input is JULIA."
+ )
+ (
+ g_argMachine.c_str(),
+ po::value<string>()->value_name(boost::join(g_machineArgs, ",")),
+ "Target machine in assembly or JULIA mode."
)
(
g_argLink.c_str(),
@@ -696,11 +720,30 @@ bool CommandLineInterface::processInput()
if (!parseLibraryOption(library))
return false;
- if (m_args.count(g_argAssemble))
+ if (m_args.count(g_argAssemble) || m_args.count(g_argJulia))
{
// switch to assembly mode
m_onlyAssemble = true;
- return assemble();
+ using Input = AssemblyStack::Language;
+ using Machine = AssemblyStack::Machine;
+ Input inputLanguage = m_args.count(g_argJulia) ? Input::JULIA : Input::Assembly;
+ Machine targetMachine = Machine::EVM;
+ if (m_args.count(g_argMachine))
+ {
+ string machine = m_args[g_argMachine].as<string>();
+ if (machine == g_strEVM)
+ targetMachine = Machine::EVM;
+ else if (machine == g_strEVM15)
+ targetMachine = Machine::EVM15;
+ else if (machine == g_streWasm)
+ targetMachine = Machine::eWasm;
+ else
+ {
+ cerr << "Invalid option for --machine: " << machine << endl;
+ return false;
+ }
+ }
+ return assemble(inputLanguage, targetMachine);
}
if (m_args.count(g_argLink))
{
@@ -925,10 +968,9 @@ void CommandLineInterface::handleAst(string const& _argStr)
bool CommandLineInterface::actOnInput()
{
- if (m_args.count(g_argStandardJSON))
+ if (m_args.count(g_argStandardJSON) || m_onlyAssemble)
+ // Already done in "processInput" phase.
return true;
- else if (m_onlyAssemble)
- outputAssembly();
else if (m_onlyLink)
writeLinkedFiles();
else
@@ -989,21 +1031,20 @@ void CommandLineInterface::writeLinkedFiles()
writeFile(src.first, src.second);
}
-bool CommandLineInterface::assemble()
+bool CommandLineInterface::assemble(
+ AssemblyStack::Language _language,
+ AssemblyStack::Machine _targetMachine
+)
{
bool successful = true;
- map<string, shared_ptr<Scanner>> scanners;
+ map<string, AssemblyStack> assemblyStacks;
for (auto const& src: m_sourceCodes)
{
+ auto& stack = assemblyStacks[src.first] = AssemblyStack(_language);
try
{
- auto scanner = make_shared<Scanner>(CharStream(src.second), src.first);
- scanners[src.first] = scanner;
- if (!m_assemblyStacks[src.first].parse(scanner))
+ if (!stack.parseAndAnalyze(src.first, src.second))
successful = false;
- else
- //@TODO we should not just throw away the result here
- m_assemblyStacks[src.first].assemble();
}
catch (Exception const& _exception)
{
@@ -1016,31 +1057,49 @@ bool CommandLineInterface::assemble()
return false;
}
}
- for (auto const& stack: m_assemblyStacks)
+ for (auto const& sourceAndStack: assemblyStacks)
{
- for (auto const& error: stack.second.errors())
+ auto const& stack = sourceAndStack.second;
+ for (auto const& error: stack.errors())
SourceReferenceFormatter::printExceptionInformation(
cerr,
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error",
- [&](string const& _source) -> Scanner const& { return *scanners.at(_source); }
+ [&](string const&) -> Scanner const& { return stack.scanner(); }
);
- if (!Error::containsOnlyWarnings(stack.second.errors()))
+ if (!Error::containsOnlyWarnings(stack.errors()))
successful = false;
}
- return successful;
-}
+ if (!successful)
+ return false;
-void CommandLineInterface::outputAssembly()
-{
for (auto const& src: m_sourceCodes)
{
- cout << endl << "======= " << src.first << " =======" << endl;
- eth::Assembly assembly = m_assemblyStacks[src.first].assemble();
- cout << assembly.assemble().toHex() << endl;
- assembly.stream(cout, "", m_sourceCodes);
+ string machine =
+ _targetMachine == AssemblyStack::Machine::EVM ? "EVM" :
+ _targetMachine == AssemblyStack::Machine::EVM15 ? "EVM 1.5" :
+ "eWasm";
+ cout << endl << "======= " << src.first << " (" << machine << ") =======" << endl;
+ AssemblyStack& stack = assemblyStacks[src.first];
+ try
+ {
+ cout << stack.assemble(_targetMachine).toHex() << endl;
+ }
+ catch (Exception const& _exception)
+ {
+ cerr << "Exception while assembling: " << boost::diagnostic_information(_exception) << endl;
+ return false;
+ }
+ catch (...)
+ {
+ cerr << "Unknown exception while assembling." << endl;
+ return false;
+ }
+ cout << stack.print() << endl;
}
+
+ return true;
}
void CommandLineInterface::outputCompilationResults()
diff --git a/solc/CommandLineInterface.h b/solc/CommandLineInterface.h
index ebf52625..b482c20b 100644
--- a/solc/CommandLineInterface.h
+++ b/solc/CommandLineInterface.h
@@ -22,7 +22,7 @@
#pragma once
#include <libsolidity/interface/CompilerStack.h>
-#include <libsolidity/inlineasm/AsmStack.h>
+#include <libsolidity/interface/AssemblyStack.h>
#include <boost/program_options.hpp>
#include <boost/filesystem/path.hpp>
@@ -54,9 +54,7 @@ private:
bool link();
void writeLinkedFiles();
- /// Parse assembly input.
- bool assemble();
- void outputAssembly();
+ bool assemble(AssemblyStack::Language _language, AssemblyStack::Machine _targetMachine);
void outputCompilationResults();
@@ -86,6 +84,7 @@ private:
bool m_error = false; ///< If true, some error occurred.
bool m_onlyAssemble = false;
+
bool m_onlyLink = false;
/// Compiler arguments variable map
@@ -98,8 +97,6 @@ private:
std::map<std::string, h160> m_libraries;
/// Solidity compiler stack
std::unique_ptr<dev::solidity::CompilerStack> m_compiler;
- /// Assembly stacks for assembly-only mode
- std::map<std::string, assembly::InlineAssemblyStack> m_assemblyStacks;
};
}
diff --git a/test/libjulia/Parser.cpp b/test/libjulia/Parser.cpp
index d1081067..b82c446a 100644
--- a/test/libjulia/Parser.cpp
+++ b/test/libjulia/Parser.cpp
@@ -52,7 +52,10 @@ bool parse(string const& _source, ErrorList& errors)
auto scanner = make_shared<Scanner>(CharStream(_source));
auto parserResult = assembly::Parser(errors, true).parse(scanner);
if (parserResult)
- return true;
+ {
+ assembly::AsmAnalysisInfo analysisInfo;
+ return (assembly::AsmAnalyzer(analysisInfo, errors, true)).analyze(*parserResult);
+ }
}
catch (FatalError const&)
{
@@ -107,7 +110,6 @@ do \
BOOST_CHECK(searchErrorMessage(err, (substring))); \
} while(0)
-
BOOST_AUTO_TEST_SUITE(JuliaParser)
BOOST_AUTO_TEST_CASE(smoke_test)
@@ -131,14 +133,9 @@ BOOST_AUTO_TEST_CASE(assignment)
BOOST_CHECK(successParse("{ let x:u256 := 2:u256 let y:u256 := x }"));
}
-BOOST_AUTO_TEST_CASE(function_call)
-{
- BOOST_CHECK(successParse("{ fun() fun(fun()) }"));
-}
-
BOOST_AUTO_TEST_CASE(vardecl_complex)
{
- BOOST_CHECK(successParse("{ let y:u256 := 2:u256 let x:u256 := add(7:u256, mul(6:u256, y)) }"));
+ BOOST_CHECK(successParse("{ function add(a:u256, b:u256) -> c:u256 {} let y:u256 := 2:u256 let x:u256 := add(7:u256, add(6:u256, y)) }"));
}
BOOST_AUTO_TEST_CASE(blocks)
@@ -158,7 +155,12 @@ BOOST_AUTO_TEST_CASE(function_definitions_multiple_args)
BOOST_AUTO_TEST_CASE(function_calls)
{
- BOOST_CHECK(successParse("{ function f(a:u256) -> b:u256 {} function g(a:u256, b:u256, c:u256) {} function x() { g(1:u256, 2:u256, f(mul(2:u256, 3:u256))) x() } }"));
+ BOOST_CHECK(successParse("{ function f(a:u256) -> b:u256 {} function g(a:u256, b:u256, c:u256) {} function x() { g(1:u256, 2:u256, f(3:u256)) x() } }"));
+}
+
+BOOST_AUTO_TEST_CASE(tuple_assignment)
+{
+ BOOST_CHECK(successParse("{ function f() -> a:u256, b:u256, c:u256 {} let x:u256, y:u256, z:u256 := f() }"));
}
BOOST_AUTO_TEST_CASE(label)
@@ -194,6 +196,31 @@ BOOST_AUTO_TEST_CASE(lacking_types)
CHECK_ERROR("{ function f(a:u256) -> b {} }", ParserError, "Expected token Colon got 'LBrace'");
}
+BOOST_AUTO_TEST_CASE(invalid_types)
+{
+ /// testing invalid literal
+ /// NOTE: these will need to change when types are compared
+ CHECK_ERROR("{ let x:bool := 1:invalid }", TypeError, "\"invalid\" is not a valid type (user defined types are not yet supported).");
+ /// testing invalid variable declaration
+ CHECK_ERROR("{ let x:invalid := 1:bool }", TypeError, "\"invalid\" is not a valid type (user defined types are not yet supported).");
+ CHECK_ERROR("{ function f(a:invalid) {} }", TypeError, "\"invalid\" is not a valid type (user defined types are not yet supported).");
+}
+
+BOOST_AUTO_TEST_CASE(builtin_types)
+{
+ BOOST_CHECK(successParse("{ let x:bool := true:bool }"));
+ BOOST_CHECK(successParse("{ let x:u8 := 1:u8 }"));
+ BOOST_CHECK(successParse("{ let x:s8 := 1:u8 }"));
+ BOOST_CHECK(successParse("{ let x:u32 := 1:u32 }"));
+ BOOST_CHECK(successParse("{ let x:s32 := 1:s32 }"));
+ BOOST_CHECK(successParse("{ let x:u64 := 1:u64 }"));
+ BOOST_CHECK(successParse("{ let x:s64 := 1:s64 }"));
+ BOOST_CHECK(successParse("{ let x:u128 := 1:u128 }"));
+ BOOST_CHECK(successParse("{ let x:s128 := 1:s128 }"));
+ BOOST_CHECK(successParse("{ let x:u256 := 1:u256 }"));
+ BOOST_CHECK(successParse("{ let x:s256 := 1:s256 }"));
+}
+
BOOST_AUTO_TEST_SUITE_END()
}
diff --git a/test/libsolidity/InlineAssembly.cpp b/test/libsolidity/InlineAssembly.cpp
index 0729c8db..b390a40b 100644
--- a/test/libsolidity/InlineAssembly.cpp
+++ b/test/libsolidity/InlineAssembly.cpp
@@ -161,6 +161,21 @@ BOOST_AUTO_TEST_CASE(vardecl)
BOOST_CHECK(successParse("{ let x := 7 }"));
}
+BOOST_AUTO_TEST_CASE(vardecl_name_clashes)
+{
+ CHECK_PARSE_ERROR("{ let x := 1 let x := 2 }", DeclarationError, "Variable name x already taken in this scope.");
+}
+
+BOOST_AUTO_TEST_CASE(vardecl_multi)
+{
+ BOOST_CHECK(successParse("{ function f() -> x, y {} let x, y := f() }"));
+}
+
+BOOST_AUTO_TEST_CASE(vardecl_multi_conflict)
+{
+ CHECK_PARSE_ERROR("{ function f() -> x, y {} let x, x := f() }", DeclarationError, "Variable name x already taken in this scope.");
+}
+
BOOST_AUTO_TEST_CASE(vardecl_bool)
{
CHECK_PARSE_ERROR("{ let x := true }", ParserError, "True and false are not valid literals.");
@@ -208,6 +223,53 @@ BOOST_AUTO_TEST_CASE(variable_use_before_decl)
CHECK_PARSE_ERROR("{ let x := mul(2, x) }", DeclarationError, "Variable x used before it was declared.");
}
+BOOST_AUTO_TEST_CASE(switch_statement)
+{
+ BOOST_CHECK(successParse("{ switch 42 default {} }"));
+ BOOST_CHECK(successParse("{ switch 42 case 1 {} }"));
+ BOOST_CHECK(successParse("{ switch 42 case 1 {} case 2 {} }"));
+ BOOST_CHECK(successParse("{ switch 42 case 1 {} default {} }"));
+ BOOST_CHECK(successParse("{ switch 42 case 1 {} case 2 {} default {} }"));
+ BOOST_CHECK(successParse("{ switch mul(1, 2) case 1 {} case 2 {} default {} }"));
+ BOOST_CHECK(successParse("{ function f() -> x {} switch f() case 1 {} case 2 {} default {} }"));
+}
+
+BOOST_AUTO_TEST_CASE(switch_no_cases)
+{
+ CHECK_PARSE_ERROR("{ switch 42 }", ParserError, "Switch statement without any cases.");
+}
+
+BOOST_AUTO_TEST_CASE(switch_duplicate_case)
+{
+ CHECK_PARSE_ERROR("{ switch 42 case 1 {} case 1 {} default {} }", DeclarationError, "Duplicate case defined");
+}
+
+BOOST_AUTO_TEST_CASE(switch_invalid_expression)
+{
+ CHECK_PARSE_ERROR("{ switch {} default {} }", ParserError, "Literal, identifier or instruction expected.");
+ CHECK_PARSE_ERROR("{ 1 2 switch mul default {} }", ParserError, "Instructions are not supported as expressions for switch.");
+}
+
+BOOST_AUTO_TEST_CASE(switch_default_before_case)
+{
+ CHECK_PARSE_ERROR("{ switch 42 default {} case 1 {} }", ParserError, "Case not allowed after default case.");
+}
+
+BOOST_AUTO_TEST_CASE(switch_duplicate_default_case)
+{
+ CHECK_PARSE_ERROR("{ switch 42 default {} default {} }", ParserError, "Only one default case allowed.");
+}
+
+BOOST_AUTO_TEST_CASE(switch_invalid_case)
+{
+ CHECK_PARSE_ERROR("{ switch 42 case mul(1, 2) {} case 2 {} default {} }", ParserError, "Literal expected.");
+}
+
+BOOST_AUTO_TEST_CASE(switch_invalid_body)
+{
+ CHECK_PARSE_ERROR("{ switch 42 case 1 mul case 2 {} default {} }", ParserError, "Expected token LBrace got 'Identifier'");
+}
+
BOOST_AUTO_TEST_CASE(blocks)
{
BOOST_CHECK(successParse("{ let x := 7 { let y := 3 } { let z := 2 } }"));
@@ -249,6 +311,23 @@ BOOST_AUTO_TEST_CASE(variable_access_cross_functions)
CHECK_PARSE_ERROR("{ let x := 2 function g() { x pop } }", DeclarationError, "Identifier not found.");
}
+BOOST_AUTO_TEST_CASE(invalid_tuple_assignment)
+{
+ /// The push(42) is added here to silence the unbalanced stack error, so that there's only one error reported.
+ CHECK_PARSE_ERROR("{ 42 let x, y := 1 }", DeclarationError, "Variable count mismatch.");
+}
+
+BOOST_AUTO_TEST_CASE(instruction_too_few_arguments)
+{
+ CHECK_PARSE_ERROR("{ mul() }", ParserError, "Expected expression (MUL expects 2 arguments)");
+ CHECK_PARSE_ERROR("{ mul(1) }", ParserError, "Expected comma (MUL expects 2 arguments)");
+}
+
+BOOST_AUTO_TEST_CASE(instruction_too_many_arguments)
+{
+ CHECK_PARSE_ERROR("{ mul(1, 2, 3) }", ParserError, "Expected ')' (MUL expects 2 arguments)");
+}
+
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE(Printing)
@@ -283,6 +362,11 @@ BOOST_AUTO_TEST_CASE(print_assignments)
parsePrintCompare("{\n let x := mul(2, 3)\n 7\n =: x\n x := add(1, 2)\n}");
}
+BOOST_AUTO_TEST_CASE(print_multi_assignments)
+{
+ parsePrintCompare("{\n function f() -> x, y\n {\n }\n let x, y := f()\n}");
+}
+
BOOST_AUTO_TEST_CASE(print_string_literals)
{
parsePrintCompare("{\n \"\\n'\\xab\\x95\\\"\"\n pop\n}");
@@ -299,6 +383,11 @@ BOOST_AUTO_TEST_CASE(print_string_literal_unicode)
parsePrintCompare(parsed);
}
+BOOST_AUTO_TEST_CASE(print_switch)
+{
+ parsePrintCompare("{\n switch 42\n case 1 {\n }\n case 2 {\n }\n default {\n }\n}");
+}
+
BOOST_AUTO_TEST_CASE(function_definitions_multiple_args)
{
parsePrintCompare("{\n function f(a, d)\n {\n mstore(a, d)\n }\n function g(a, d) -> x, y\n {\n }\n}");
diff --git a/test/libsolidity/Metadata.cpp b/test/libsolidity/Metadata.cpp
new file mode 100644
index 00000000..60bb2e4e
--- /dev/null
+++ b/test/libsolidity/Metadata.cpp
@@ -0,0 +1,63 @@
+/*
+ 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/>.
+ */
+/**
+ * @date 2017
+ * Unit tests for the metadata output.
+ */
+
+#include "../Metadata.h"
+#include "../TestHelper.h"
+#include <libsolidity/interface/CompilerStack.h>
+#include <libdevcore/SwarmHash.h>
+
+namespace dev
+{
+namespace solidity
+{
+namespace test
+{
+
+BOOST_AUTO_TEST_SUITE(Metadata)
+
+BOOST_AUTO_TEST_CASE(metadata_stamp)
+{
+ // Check that the metadata stamp is at the end of the runtime bytecode.
+ char const* sourceCode = R"(
+ pragma solidity >=0.0;
+ contract test {
+ function g(function(uint) external returns (uint) x) {}
+ }
+ )";
+ CompilerStack compilerStack;
+ BOOST_REQUIRE(compilerStack.compile(std::string(sourceCode)));
+ bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
+ std::string const& metadata = compilerStack.onChainMetadata("test");
+ BOOST_CHECK(dev::test::isValidMetadata(metadata));
+ bytes hash = dev::swarmHash(metadata).asBytes();
+ BOOST_REQUIRE(hash.size() == 32);
+ BOOST_REQUIRE(bytecode.size() >= 2);
+ size_t metadataCBORSize = (size_t(bytecode.end()[-2]) << 8) + size_t(bytecode.end()[-1]);
+ BOOST_REQUIRE(metadataCBORSize < bytecode.size() - 2);
+ bytes expectation = bytes{0xa1, 0x65, 'b', 'z', 'z', 'r', '0', 0x58, 0x20} + hash;
+ BOOST_CHECK(std::equal(expectation.begin(), expectation.end(), bytecode.end() - metadataCBORSize - 2));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}
+}
diff --git a/test/libsolidity/SolidityABIJSON.cpp b/test/libsolidity/SolidityABIJSON.cpp
index 1ebccc13..f87390e1 100644
--- a/test/libsolidity/SolidityABIJSON.cpp
+++ b/test/libsolidity/SolidityABIJSON.cpp
@@ -752,26 +752,6 @@ BOOST_AUTO_TEST_CASE(function_type)
checkInterface(sourceCode, interface);
}
-BOOST_AUTO_TEST_CASE(metadata_stamp)
-{
- // Check that the metadata stamp is at the end of the runtime bytecode.
- char const* sourceCode = R"(
- pragma solidity >=0.0;
- contract test {
- function g(function(uint) external returns (uint) x) {}
- }
- )";
- BOOST_REQUIRE(m_compilerStack.compile(std::string(sourceCode)));
- bytes const& bytecode = m_compilerStack.runtimeObject("test").bytecode;
- bytes hash = dev::swarmHash(m_compilerStack.onChainMetadata("test")).asBytes();
- BOOST_REQUIRE(hash.size() == 32);
- BOOST_REQUIRE(bytecode.size() >= 2);
- size_t metadataCBORSize = (size_t(bytecode.end()[-2]) << 8) + size_t(bytecode.end()[-1]);
- BOOST_REQUIRE(metadataCBORSize < bytecode.size() - 2);
- bytes expectation = bytes{0xa1, 0x65, 'b', 'z', 'z', 'r', '0', 0x58, 0x20} + hash;
- BOOST_CHECK(std::equal(expectation.begin(), expectation.end(), bytecode.end() - metadataCBORSize - 2));
-}
-
BOOST_AUTO_TEST_SUITE_END()
}
diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp
index 3a9f7295..97c4303f 100644
--- a/test/libsolidity/SolidityNameAndTypeResolution.cpp
+++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp
@@ -5718,6 +5718,20 @@ BOOST_AUTO_TEST_CASE(no_unused_dec_after_use)
CHECK_SUCCESS_NO_WARNINGS(text);
}
+BOOST_AUTO_TEST_CASE(no_unused_inline_asm)
+{
+ char const* text = R"(
+ contract C {
+ function f() {
+ uint a;
+ assembly {
+ a := 1
+ }
+ }
+ }
+ )";
+ CHECK_SUCCESS_NO_WARNINGS(text);
+}