2014-10-30 00:20:32 +00:00
|
|
|
/*
|
|
|
|
This file is part of cpp-ethereum.
|
|
|
|
|
|
|
|
cpp-ethereum 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.
|
|
|
|
|
|
|
|
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
2015-01-09 06:39:30 +00:00
|
|
|
* @author Gav Wood <g@ethdev.com>
|
2014-10-30 00:20:32 +00:00
|
|
|
* @date 2014
|
|
|
|
* Solidity AST to EVM bytecode compiler for expressions.
|
|
|
|
*/
|
|
|
|
|
2014-12-10 22:01:40 +00:00
|
|
|
#include <functional>
|
2015-02-12 17:38:07 +00:00
|
|
|
#include <memory>
|
2014-11-10 16:31:09 +00:00
|
|
|
#include <boost/noncopyable.hpp>
|
2014-11-07 01:06:37 +00:00
|
|
|
#include <libdevcore/Common.h>
|
2015-02-12 17:38:07 +00:00
|
|
|
#include <libsolidity/BaseTypes.h>
|
2014-10-30 00:20:32 +00:00
|
|
|
#include <libsolidity/ASTVisitor.h>
|
|
|
|
|
|
|
|
namespace dev {
|
2014-11-07 01:06:37 +00:00
|
|
|
namespace eth
|
|
|
|
{
|
|
|
|
class AssemblyItem; // forward
|
|
|
|
}
|
2014-10-30 00:20:32 +00:00
|
|
|
namespace solidity {
|
|
|
|
|
2014-12-04 18:38:24 +00:00
|
|
|
// forward declarations
|
|
|
|
class CompilerContext;
|
|
|
|
class Type;
|
|
|
|
class IntegerType;
|
2015-02-12 17:38:07 +00:00
|
|
|
class ByteArrayType;
|
2014-12-09 17:46:18 +00:00
|
|
|
class StaticStringType;
|
2014-10-30 11:42:04 +00:00
|
|
|
|
2014-11-07 01:06:37 +00:00
|
|
|
/**
|
|
|
|
* Compiler for expressions, i.e. converts an AST tree whose root is an Expression into a stream
|
|
|
|
* of EVM instructions. It needs a compiler context that is the same for the whole compilation
|
|
|
|
* unit.
|
|
|
|
*/
|
2014-12-06 01:19:10 +00:00
|
|
|
class ExpressionCompiler: private ASTConstVisitor
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Compile the given @a _expression into the @a _context.
|
2014-12-11 16:35:23 +00:00
|
|
|
static void compileExpression(CompilerContext& _context, Expression const& _expression, bool _optimize = false);
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
/// Appends code to remove dirty higher order bits in case of an implicit promotion to a wider type.
|
2015-01-08 23:58:32 +00:00
|
|
|
static void appendTypeConversion(CompilerContext& _context, Type const& _typeOnStack,
|
|
|
|
Type const& _targetType, bool _cleanupNeeded = false);
|
2015-01-27 15:55:06 +00:00
|
|
|
/// Appends code for a State Variable accessor function
|
2015-01-28 17:06:45 +00:00
|
|
|
static void appendStateVariableAccessor(CompilerContext& _context, VariableDeclaration const& _varDecl, bool _optimize = false);
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2015-02-17 15:21:38 +00:00
|
|
|
/// Appends code for a State Variable Initialization function
|
|
|
|
static void appendStateVariableInitialization(CompilerContext& _context, VariableDeclaration const& _varDecl, bool _optimize = false);
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
private:
|
2014-12-11 16:35:23 +00:00
|
|
|
explicit ExpressionCompiler(CompilerContext& _compilerContext, bool _optimize = false):
|
|
|
|
m_optimize(_optimize), m_context(_compilerContext), m_currentLValue(m_context) {}
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2014-12-06 00:06:24 +00:00
|
|
|
virtual bool visit(Assignment const& _assignment) override;
|
2014-12-19 10:31:17 +00:00
|
|
|
virtual bool visit(UnaryOperation const& _unaryOperation) override;
|
2014-12-06 00:06:24 +00:00
|
|
|
virtual bool visit(BinaryOperation const& _binaryOperation) override;
|
|
|
|
virtual bool visit(FunctionCall const& _functionCall) override;
|
2014-12-12 15:49:26 +00:00
|
|
|
virtual bool visit(NewExpression const& _newExpression) override;
|
2014-12-06 00:06:24 +00:00
|
|
|
virtual void endVisit(MemberAccess const& _memberAccess) override;
|
|
|
|
virtual bool visit(IndexAccess const& _indexAccess) override;
|
|
|
|
virtual void endVisit(Identifier const& _identifier) override;
|
|
|
|
virtual void endVisit(Literal const& _literal) override;
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
///@{
|
|
|
|
///@name Append code for various operator types
|
2014-12-06 00:06:24 +00:00
|
|
|
void appendAndOrOperatorCode(BinaryOperation const& _binaryOperation);
|
2014-10-30 00:20:32 +00:00
|
|
|
void appendCompareOperatorCode(Token::Value _operator, Type const& _type);
|
|
|
|
void appendOrdinaryBinaryOperatorCode(Token::Value _operator, Type const& _type);
|
|
|
|
|
|
|
|
void appendArithmeticOperatorCode(Token::Value _operator, Type const& _type);
|
|
|
|
void appendBitOperatorCode(Token::Value _operator);
|
|
|
|
void appendShiftOperatorCode(Token::Value _operator);
|
|
|
|
/// @}
|
|
|
|
|
2014-11-04 18:13:03 +00:00
|
|
|
/// Appends an implicit or explicit type conversion. For now this comprises only erasing
|
2014-12-09 17:46:18 +00:00
|
|
|
/// higher-order bits (@see appendHighBitCleanup) when widening integer.
|
2014-11-04 20:29:36 +00:00
|
|
|
/// If @a _cleanupNeeded, high order bits cleanup is also done if no type conversion would be
|
|
|
|
/// necessary.
|
|
|
|
void appendTypeConversion(Type const& _typeOnStack, Type const& _targetType, bool _cleanupNeeded = false);
|
2014-11-04 18:13:03 +00:00
|
|
|
//// Appends code that cleans higher-order bits for integer types.
|
|
|
|
void appendHighBitsCleanup(IntegerType const& _typeOnStack);
|
|
|
|
|
2014-12-10 22:01:40 +00:00
|
|
|
/// Appends code to call a function of the given type with the given arguments.
|
|
|
|
void appendExternalFunctionCall(FunctionType const& _functionType, std::vector<ASTPointer<Expression const>> const& _arguments,
|
2015-01-12 11:47:37 +00:00
|
|
|
bool bare = false);
|
2015-02-10 16:53:43 +00:00
|
|
|
/// Appends code that evaluates the given arguments and moves the result to memory. The memory offset is
|
|
|
|
/// expected to be on the stack and is updated by this call.
|
|
|
|
void appendArgumentsCopyToMemory(std::vector<ASTPointer<Expression const>> const& _arguments,
|
|
|
|
TypePointers const& _types = {},
|
|
|
|
bool _padToWordBoundaries = true,
|
|
|
|
bool _padExceptionIfFourBytes = false);
|
|
|
|
/// Appends code that moves a stack element of the given type to memory. The memory offset is
|
|
|
|
/// expected below the stack element and is updated by this call.
|
|
|
|
void appendTypeMoveToMemory(Type const& _type, bool _padToWordBoundaries = true);
|
|
|
|
/// Appends code that evaluates a single expression and moves the result to memory. The memory offset is
|
|
|
|
/// expected to be on the stack and is updated by this call.
|
|
|
|
void appendExpressionCopyToMemory(Type const& _expectedType, Expression const& _expression);
|
2014-12-10 22:01:40 +00:00
|
|
|
|
2015-01-26 17:16:47 +00:00
|
|
|
/// Appends code for a State Variable accessor function
|
2015-01-28 17:06:45 +00:00
|
|
|
void appendStateVariableAccessor(VariableDeclaration const& _varDecl);
|
2015-01-26 17:16:47 +00:00
|
|
|
|
2015-02-17 15:21:38 +00:00
|
|
|
/// Appends code for a State Variable initialization
|
|
|
|
void appendStateVariableInitialization(VariableDeclaration const& _varDecl);
|
|
|
|
|
2014-11-07 01:06:37 +00:00
|
|
|
/**
|
2014-11-10 16:31:09 +00:00
|
|
|
* Helper class to store and retrieve lvalues to and from various locations.
|
2014-11-21 18:14:56 +00:00
|
|
|
* All types except STACK store a reference in a slot on the stack, STACK just
|
|
|
|
* stores the base stack offset of the variable in @a m_baseStackOffset.
|
2014-11-07 01:06:37 +00:00
|
|
|
*/
|
2014-11-10 16:31:09 +00:00
|
|
|
class LValue
|
2014-11-07 01:06:37 +00:00
|
|
|
{
|
2014-11-10 16:31:09 +00:00
|
|
|
public:
|
2015-02-09 13:41:01 +00:00
|
|
|
enum class LValueType { None, Stack, Memory, Storage };
|
2014-11-10 16:31:09 +00:00
|
|
|
|
|
|
|
explicit LValue(CompilerContext& _compilerContext): m_context(&_compilerContext) { reset(); }
|
2015-02-12 17:38:07 +00:00
|
|
|
LValue(CompilerContext& _compilerContext, LValueType _type,
|
|
|
|
std::shared_ptr<Type const> const& _dataType, unsigned _baseStackOffset = 0);
|
2014-11-10 16:31:09 +00:00
|
|
|
|
|
|
|
/// Set type according to the declaration and retrieve the reference.
|
2015-02-17 15:21:38 +00:00
|
|
|
/// @a _identifier is the current identifier
|
2014-11-21 18:14:56 +00:00
|
|
|
void fromIdentifier(Identifier const& _identifier, Declaration const& _declaration);
|
2015-02-17 15:21:38 +00:00
|
|
|
|
|
|
|
/// Set type according to the declaration and retrieve the reference.
|
|
|
|
/// @a _declaration is the variable declaration
|
2015-02-20 10:57:42 +00:00
|
|
|
void fromVariableDeclaration(const Declaration &_declaration);
|
2015-02-17 15:21:38 +00:00
|
|
|
|
2015-02-12 17:38:07 +00:00
|
|
|
void reset() { m_type = LValueType::None; m_dataType.reset(); m_baseStackOffset = 0; m_size = 0; }
|
2014-11-10 16:31:09 +00:00
|
|
|
|
2015-02-09 13:41:01 +00:00
|
|
|
bool isValid() const { return m_type != LValueType::None; }
|
|
|
|
bool isInOnStack() const { return m_type == LValueType::Stack; }
|
|
|
|
bool isInMemory() const { return m_type == LValueType::Memory; }
|
|
|
|
bool isInStorage() const { return m_type == LValueType::Storage; }
|
2014-11-10 16:31:09 +00:00
|
|
|
|
|
|
|
/// @returns true if this lvalue reference type occupies a slot on the stack.
|
2015-02-09 13:41:01 +00:00
|
|
|
bool storesReferenceOnStack() const { return m_type == LValueType::Storage || m_type == LValueType::Memory; }
|
2014-11-10 16:31:09 +00:00
|
|
|
|
|
|
|
/// Copies the value of the current lvalue to the top of the stack and, if @a _remove is true,
|
|
|
|
/// also removes the reference from the stack (note that is does not reset the type to @a NONE).
|
2015-02-12 17:38:07 +00:00
|
|
|
/// @a _location source location of the current expression, used for error reporting.
|
|
|
|
void retrieveValue(Location const& _location, bool _remove = false) const;
|
|
|
|
/// Moves a value from the stack to the lvalue. Removes the value if @a _move is true.
|
|
|
|
/// @a _location is the source location of the expression that caused this operation.
|
2015-02-17 23:15:08 +00:00
|
|
|
/// Stack pre: value [lvalue_ref]
|
2015-02-12 17:38:07 +00:00
|
|
|
/// Stack post if !_move: value_of(lvalue_ref)
|
|
|
|
void storeValue(Type const& _sourceType, Location const& _location = Location(), bool _move = false) const;
|
2015-01-14 12:52:03 +00:00
|
|
|
/// Stores zero in the lvalue.
|
2015-02-12 17:38:07 +00:00
|
|
|
/// @a _location is the source location of the requested operation
|
|
|
|
void setToZero(Location const& _location = Location()) const;
|
2014-11-10 16:31:09 +00:00
|
|
|
/// Convenience function to convert the stored reference to a value and reset type to NONE if
|
|
|
|
/// the reference was not requested by @a _expression.
|
|
|
|
void retrieveValueIfLValueNotRequested(Expression const& _expression);
|
|
|
|
|
|
|
|
private:
|
2015-01-29 09:44:29 +00:00
|
|
|
/// Convenience function to retrieve Value from Storage. Specific version of @ref retrieveValue
|
2015-02-12 17:38:07 +00:00
|
|
|
void retrieveValueFromStorage(bool _remove = false) const;
|
2015-02-10 13:57:01 +00:00
|
|
|
/// Copies from a byte array to a byte array in storage, both references on the stack.
|
|
|
|
void copyByteArrayToStorage(ByteArrayType const& _targetType, ByteArrayType const& _sourceType) const;
|
2015-01-29 09:44:29 +00:00
|
|
|
|
2014-11-10 16:31:09 +00:00
|
|
|
CompilerContext* m_context;
|
2015-02-09 13:41:01 +00:00
|
|
|
LValueType m_type = LValueType::None;
|
2015-02-12 17:38:07 +00:00
|
|
|
std::shared_ptr<Type const> m_dataType;
|
2014-11-10 16:31:09 +00:00
|
|
|
/// If m_type is STACK, this is base stack offset (@see
|
|
|
|
/// CompilerContext::getBaseStackOffsetOfVariable) of a local variable.
|
2015-01-07 15:46:15 +00:00
|
|
|
unsigned m_baseStackOffset = 0;
|
2015-01-14 12:52:03 +00:00
|
|
|
/// Size of the value of this lvalue on the stack or the storage.
|
|
|
|
unsigned m_size = 0;
|
2014-11-07 01:06:37 +00:00
|
|
|
};
|
|
|
|
|
2014-12-11 16:35:23 +00:00
|
|
|
bool m_optimize;
|
2014-10-30 00:20:32 +00:00
|
|
|
CompilerContext& m_context;
|
2014-11-10 16:31:09 +00:00
|
|
|
LValue m_currentLValue;
|
2014-10-30 00:20:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|