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>
|
|
|
|
* @date 2014
|
|
|
|
* Solidity AST to EVM bytecode compiler for expressions.
|
|
|
|
*/
|
|
|
|
|
2014-11-07 01:06:37 +00:00
|
|
|
#include <libdevcore/Common.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-10-30 11:42:04 +00:00
|
|
|
class CompilerContext; // forward
|
2014-11-04 18:13:03 +00:00
|
|
|
class Type; // forward
|
|
|
|
class IntegerType; // forward
|
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-10-30 00:20:32 +00:00
|
|
|
class ExpressionCompiler: private ASTVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Compile the given @a _expression into the @a _context.
|
|
|
|
static void compileExpression(CompilerContext& _context, Expression& _expression);
|
|
|
|
|
|
|
|
/// Appends code to remove dirty higher order bits in case of an implicit promotion to a wider type.
|
2014-11-04 18:13:03 +00:00
|
|
|
static void appendTypeConversion(CompilerContext& _context, Type const& _typeOnStack, Type const& _targetType);
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
private:
|
2014-11-07 01:06:37 +00:00
|
|
|
ExpressionCompiler(CompilerContext& _compilerContext): m_context(_compilerContext) {}
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
virtual bool visit(Assignment& _assignment) override;
|
|
|
|
virtual void endVisit(UnaryOperation& _unaryOperation) override;
|
|
|
|
virtual bool visit(BinaryOperation& _binaryOperation) override;
|
|
|
|
virtual bool visit(FunctionCall& _functionCall) override;
|
|
|
|
virtual void endVisit(MemberAccess& _memberAccess) override;
|
|
|
|
virtual void endVisit(IndexAccess& _indexAccess) override;
|
|
|
|
virtual void endVisit(Identifier& _identifier) override;
|
|
|
|
virtual void endVisit(Literal& _literal) override;
|
|
|
|
|
|
|
|
///@{
|
|
|
|
///@name Append code for various operator types
|
|
|
|
void appendAndOrOperatorCode(BinaryOperation& _binaryOperation);
|
|
|
|
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
|
|
|
|
/// higher-order bits (@see appendHighBitCleanup) when widening integer types.
|
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-11-07 01:06:37 +00:00
|
|
|
/// Copies the value of the current lvalue to the top of the stack.
|
|
|
|
void retrieveLValueValue(Expression const& _expression);
|
|
|
|
/// Stores the value on top of the stack in the current lvalue. Removes it from the stack if
|
|
|
|
/// @a _move is true.
|
|
|
|
void storeInLValue(Expression const& _expression, bool _move = false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Location of an lvalue, either in code (for a function) on the stack, in the storage or memory.
|
|
|
|
*/
|
|
|
|
struct LValueLocation
|
|
|
|
{
|
|
|
|
enum LocationType { INVALID, CODE, STACK, MEMORY, STORAGE };
|
|
|
|
|
|
|
|
LValueLocation() { reset(); }
|
|
|
|
LValueLocation(LocationType _type, u256 const& _location): locationType(_type), location(_location) {}
|
|
|
|
void reset() { locationType = INVALID; location = 0; }
|
|
|
|
bool isValid() const { return locationType != INVALID; }
|
|
|
|
bool isInCode() const { return locationType == CODE; }
|
|
|
|
bool isInOnStack() const { return locationType == STACK; }
|
|
|
|
bool isInMemory() const { return locationType == MEMORY; }
|
|
|
|
bool isInStorage() const { return locationType == STORAGE; }
|
|
|
|
|
|
|
|
LocationType locationType;
|
|
|
|
/// Depending on the type, this is the id of a tag (code), the base offset of a stack
|
|
|
|
/// variable (@see CompilerContext::getBaseStackOffsetOfVariable) or the offset in
|
|
|
|
/// storage or memory.
|
|
|
|
u256 location;
|
|
|
|
};
|
|
|
|
|
|
|
|
LValueLocation m_currentLValue;
|
2014-10-30 00:20:32 +00:00
|
|
|
CompilerContext& m_context;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|