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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
#include <numeric>
|
2014-11-10 16:31:09 +00:00
|
|
|
#include <libdevcore/Common.h>
|
2014-10-30 00:20:32 +00:00
|
|
|
#include <libsolidity/AST.h>
|
|
|
|
#include <libsolidity/ExpressionCompiler.h>
|
2014-10-30 11:42:04 +00:00
|
|
|
#include <libsolidity/CompilerContext.h>
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace dev {
|
|
|
|
namespace solidity {
|
|
|
|
|
|
|
|
void ExpressionCompiler::compileExpression(CompilerContext& _context, Expression& _expression)
|
|
|
|
{
|
|
|
|
ExpressionCompiler compiler(_context);
|
|
|
|
_expression.accept(compiler);
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:13:03 +00:00
|
|
|
void ExpressionCompiler::appendTypeConversion(CompilerContext& _context,
|
|
|
|
Type const& _typeOnStack, Type const& _targetType)
|
|
|
|
{
|
|
|
|
ExpressionCompiler compiler(_context);
|
|
|
|
compiler.appendTypeConversion(_typeOnStack, _targetType);
|
|
|
|
}
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
bool ExpressionCompiler::visit(Assignment& _assignment)
|
|
|
|
{
|
2014-11-06 21:39:02 +00:00
|
|
|
_assignment.getRightHandSide().accept(*this);
|
|
|
|
appendTypeConversion(*_assignment.getRightHandSide().getType(), *_assignment.getType());
|
2014-10-30 00:20:32 +00:00
|
|
|
_assignment.getLeftHandSide().accept(*this);
|
2014-11-10 16:31:09 +00:00
|
|
|
if (asserts(m_currentLValue.isValid()))
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("LValue not retrieved."));
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
Token::Value op = _assignment.getAssignmentOperator();
|
2014-11-06 21:39:02 +00:00
|
|
|
if (op != Token::ASSIGN) // compound assignment
|
2014-11-10 16:31:09 +00:00
|
|
|
{
|
|
|
|
if (m_currentLValue.storesReferenceOnStack())
|
|
|
|
m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2;
|
|
|
|
m_currentLValue.retrieveValue(_assignment, true);
|
2014-11-06 21:39:02 +00:00
|
|
|
appendOrdinaryBinaryOperatorCode(Token::AssignmentToBinaryOp(op), *_assignment.getType());
|
2014-11-10 16:31:09 +00:00
|
|
|
}
|
|
|
|
m_currentLValue.storeValue(_assignment);
|
2014-11-13 00:12:57 +00:00
|
|
|
m_currentLValue.reset();
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionCompiler::endVisit(UnaryOperation& _unaryOperation)
|
|
|
|
{
|
|
|
|
//@todo type checking and creating code for an operator should be in the same place:
|
|
|
|
// the operator should know how to convert itself and to which types it applies, so
|
|
|
|
// put this code together with "Type::acceptsBinary/UnaryOperator" into a class that
|
|
|
|
// represents the operator
|
|
|
|
switch (_unaryOperation.getOperator())
|
|
|
|
{
|
|
|
|
case Token::NOT: // !
|
2014-10-31 16:20:27 +00:00
|
|
|
m_context << eth::Instruction::ISZERO;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
case Token::BIT_NOT: // ~
|
2014-10-31 16:20:27 +00:00
|
|
|
m_context << eth::Instruction::NOT;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
case Token::DELETE: // delete
|
|
|
|
// @todo semantics change for complex types
|
2014-11-10 16:31:09 +00:00
|
|
|
if (asserts(m_currentLValue.isValid()))
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("LValue not retrieved."));
|
|
|
|
|
|
|
|
m_context << u256(0);
|
|
|
|
if (m_currentLValue.storesReferenceOnStack())
|
|
|
|
m_context << eth::Instruction::SWAP1;
|
|
|
|
m_currentLValue.storeValue(_unaryOperation);
|
2014-11-13 00:12:57 +00:00
|
|
|
m_currentLValue.reset();
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
case Token::INC: // ++ (pre- or postfix)
|
|
|
|
case Token::DEC: // -- (pre- or postfix)
|
2014-11-10 16:31:09 +00:00
|
|
|
if (asserts(m_currentLValue.isValid()))
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("LValue not retrieved."));
|
|
|
|
m_currentLValue.retrieveValue(_unaryOperation);
|
2014-10-30 00:20:32 +00:00
|
|
|
if (!_unaryOperation.isPrefixOperation())
|
2014-11-10 16:31:09 +00:00
|
|
|
{
|
|
|
|
if (m_currentLValue.storesReferenceOnStack())
|
|
|
|
m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2;
|
|
|
|
else
|
|
|
|
m_context << eth::Instruction::DUP1;
|
|
|
|
}
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context << u256(1);
|
|
|
|
if (_unaryOperation.getOperator() == Token::INC)
|
|
|
|
m_context << eth::Instruction::ADD;
|
|
|
|
else
|
|
|
|
m_context << eth::Instruction::SWAP1 << eth::Instruction::SUB; // @todo avoid the swap
|
2014-11-10 16:31:09 +00:00
|
|
|
// Stack for prefix: [ref] (*ref)+-1
|
|
|
|
// Stack for postfix: *ref [ref] (*ref)+-1
|
|
|
|
if (m_currentLValue.storesReferenceOnStack())
|
|
|
|
m_context << eth::Instruction::SWAP1;
|
|
|
|
m_currentLValue.storeValue(_unaryOperation, !_unaryOperation.isPrefixOperation());
|
2014-11-13 00:12:57 +00:00
|
|
|
m_currentLValue.reset();
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
case Token::ADD: // +
|
|
|
|
// unary add, so basically no-op
|
|
|
|
break;
|
|
|
|
case Token::SUB: // -
|
|
|
|
m_context << u256(0) << eth::Instruction::SUB;
|
|
|
|
break;
|
|
|
|
default:
|
2014-11-05 13:20:56 +00:00
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid unary operator: " +
|
|
|
|
string(Token::toString(_unaryOperation.getOperator()))));
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExpressionCompiler::visit(BinaryOperation& _binaryOperation)
|
|
|
|
{
|
|
|
|
Expression& leftExpression = _binaryOperation.getLeftExpression();
|
|
|
|
Expression& rightExpression = _binaryOperation.getRightExpression();
|
2014-11-04 14:01:07 +00:00
|
|
|
Type const& commonType = _binaryOperation.getCommonType();
|
2014-10-30 00:20:32 +00:00
|
|
|
Token::Value const op = _binaryOperation.getOperator();
|
|
|
|
|
2014-11-06 21:39:02 +00:00
|
|
|
if (op == Token::AND || op == Token::OR) // special case: short-circuiting
|
2014-10-30 00:20:32 +00:00
|
|
|
appendAndOrOperatorCode(_binaryOperation);
|
|
|
|
else
|
|
|
|
{
|
2014-11-04 18:13:03 +00:00
|
|
|
bool cleanupNeeded = false;
|
|
|
|
if (commonType.getCategory() == Type::Category::INTEGER)
|
|
|
|
if (Token::isCompareOp(op) || op == Token::DIV || op == Token::MOD)
|
|
|
|
cleanupNeeded = true;
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
rightExpression.accept(*this);
|
2014-11-04 20:29:36 +00:00
|
|
|
appendTypeConversion(*rightExpression.getType(), commonType, cleanupNeeded);
|
2014-11-06 21:39:02 +00:00
|
|
|
leftExpression.accept(*this);
|
|
|
|
appendTypeConversion(*leftExpression.getType(), commonType, cleanupNeeded);
|
2014-11-04 14:01:07 +00:00
|
|
|
if (Token::isCompareOp(op))
|
|
|
|
appendCompareOperatorCode(op, commonType);
|
|
|
|
else
|
|
|
|
appendOrdinaryBinaryOperatorCode(op, commonType);
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// do not visit the child nodes, we already did that explicitly
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExpressionCompiler::visit(FunctionCall& _functionCall)
|
|
|
|
{
|
|
|
|
if (_functionCall.isTypeConversion())
|
|
|
|
{
|
|
|
|
//@todo we only have integers and bools for now which cannot be explicitly converted
|
2014-11-05 13:20:56 +00:00
|
|
|
if (asserts(_functionCall.getArguments().size() == 1))
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError());
|
2014-10-30 00:20:32 +00:00
|
|
|
Expression& firstArgument = *_functionCall.getArguments().front();
|
|
|
|
firstArgument.accept(*this);
|
2014-11-04 18:13:03 +00:00
|
|
|
appendTypeConversion(*firstArgument.getType(), *_functionCall.getType());
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Calling convention: Caller pushes return address and arguments
|
|
|
|
// Callee removes them and pushes return values
|
2014-11-07 01:06:37 +00:00
|
|
|
FunctionDefinition const& function = dynamic_cast<FunctionType const&>(*_functionCall.getExpression().getType()).getFunction();
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
eth::AssemblyItem returnLabel = m_context.pushNewTag();
|
|
|
|
std::vector<ASTPointer<Expression>> const& arguments = _functionCall.getArguments();
|
2014-11-05 13:20:56 +00:00
|
|
|
if (asserts(arguments.size() == function.getParameters().size()))
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError());
|
2014-10-30 00:20:32 +00:00
|
|
|
for (unsigned i = 0; i < arguments.size(); ++i)
|
|
|
|
{
|
|
|
|
arguments[i]->accept(*this);
|
2014-11-06 21:39:02 +00:00
|
|
|
appendTypeConversion(*arguments[i]->getType(), *function.getParameters()[i]->getType());
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
2014-11-10 16:31:09 +00:00
|
|
|
_functionCall.getExpression().accept(*this);
|
|
|
|
if (asserts(m_currentLValue.isInCode()))
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Code reference expected."));
|
2014-11-13 00:12:57 +00:00
|
|
|
m_currentLValue.reset();
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2014-11-10 16:31:09 +00:00
|
|
|
m_context.appendJump();
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context << returnLabel;
|
|
|
|
|
|
|
|
// callee adds return parameters, but removes arguments and return label
|
2014-11-05 13:20:56 +00:00
|
|
|
m_context.adjustStackOffset(function.getReturnParameters().size() - arguments.size() - 1);
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
// @todo for now, the return value of a function is its first return value, so remove
|
|
|
|
// all others
|
2014-11-05 13:20:56 +00:00
|
|
|
for (unsigned i = 1; i < function.getReturnParameters().size(); ++i)
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context << eth::Instruction::POP;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-13 00:12:57 +00:00
|
|
|
void ExpressionCompiler::endVisit(MemberAccess& _memberAccess)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2014-11-13 00:12:57 +00:00
|
|
|
if (asserts(m_currentLValue.isInStorage()))
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Member access to a non-storage value."));
|
|
|
|
StructType const& type = dynamic_cast<StructType const&>(*_memberAccess.getExpression().getType());
|
|
|
|
unsigned memberIndex = type.memberNameToIndex(_memberAccess.getMemberName());
|
|
|
|
if (asserts(memberIndex <= type.getMemberCount()))
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Member not found in struct during compilation."));
|
|
|
|
m_context << type.getStorageOffsetOfMember(memberIndex) << eth::Instruction::ADD;
|
|
|
|
m_currentLValue.retrieveValueIfLValueNotRequested(_memberAccess);
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
2014-11-10 16:31:09 +00:00
|
|
|
bool ExpressionCompiler::visit(IndexAccess& _indexAccess)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2014-11-10 16:31:09 +00:00
|
|
|
m_currentLValue.reset();
|
|
|
|
_indexAccess.getBaseExpression().accept(*this);
|
|
|
|
if (asserts(m_currentLValue.isInStorage()))
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Index access to a non-storage value."));
|
|
|
|
_indexAccess.getIndexExpression().accept(*this);
|
|
|
|
appendTypeConversion(*_indexAccess.getIndexExpression().getType(),
|
|
|
|
*dynamic_cast<MappingType const&>(*_indexAccess.getBaseExpression().getType()).getKeyType(),
|
|
|
|
true);
|
|
|
|
// @todo move this once we actually use memory
|
|
|
|
m_context << u256(32) << eth::Instruction::MSTORE << u256(0) << eth::Instruction::MSTORE;
|
|
|
|
m_context << u256(64) << u256(0) << eth::Instruction::SHA3;
|
|
|
|
|
|
|
|
m_currentLValue = LValue(m_context, LValue::STORAGE);
|
|
|
|
m_currentLValue.retrieveValueIfLValueNotRequested(_indexAccess);
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2014-11-10 16:31:09 +00:00
|
|
|
return false;
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionCompiler::endVisit(Identifier& _identifier)
|
|
|
|
{
|
2014-11-10 16:31:09 +00:00
|
|
|
m_currentLValue.fromDeclaration(_identifier, *_identifier.getReferencedDeclaration());
|
|
|
|
m_currentLValue.retrieveValueIfLValueNotRequested(_identifier);
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionCompiler::endVisit(Literal& _literal)
|
|
|
|
{
|
|
|
|
switch (_literal.getType()->getCategory())
|
|
|
|
{
|
|
|
|
case Type::Category::INTEGER:
|
|
|
|
case Type::Category::BOOL:
|
|
|
|
m_context << _literal.getType()->literalValue(_literal);
|
|
|
|
break;
|
|
|
|
default:
|
2014-11-05 13:20:56 +00:00
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Only integer and boolean literals implemented for now."));
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionCompiler::appendAndOrOperatorCode(BinaryOperation& _binaryOperation)
|
|
|
|
{
|
|
|
|
Token::Value const op = _binaryOperation.getOperator();
|
2014-11-05 13:20:56 +00:00
|
|
|
if (asserts(op == Token::OR || op == Token::AND))
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError());
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
_binaryOperation.getLeftExpression().accept(*this);
|
|
|
|
m_context << eth::Instruction::DUP1;
|
|
|
|
if (op == Token::AND)
|
2014-10-31 16:20:27 +00:00
|
|
|
m_context << eth::Instruction::ISZERO;
|
2014-10-30 00:20:32 +00:00
|
|
|
eth::AssemblyItem endLabel = m_context.appendConditionalJump();
|
2014-10-31 16:47:43 +00:00
|
|
|
m_context << eth::Instruction::POP;
|
2014-10-30 00:20:32 +00:00
|
|
|
_binaryOperation.getRightExpression().accept(*this);
|
|
|
|
m_context << endLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionCompiler::appendCompareOperatorCode(Token::Value _operator, Type const& _type)
|
|
|
|
{
|
|
|
|
if (_operator == Token::EQ || _operator == Token::NE)
|
|
|
|
{
|
|
|
|
m_context << eth::Instruction::EQ;
|
|
|
|
if (_operator == Token::NE)
|
2014-10-31 16:20:27 +00:00
|
|
|
m_context << eth::Instruction::ISZERO;
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-05 13:20:56 +00:00
|
|
|
IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
|
|
|
|
bool const isSigned = type.isSigned();
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
switch (_operator)
|
|
|
|
{
|
|
|
|
case Token::GTE:
|
2014-11-06 21:39:02 +00:00
|
|
|
m_context << (isSigned ? eth::Instruction::SLT : eth::Instruction::LT)
|
2014-10-31 16:20:27 +00:00
|
|
|
<< eth::Instruction::ISZERO;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
case Token::LTE:
|
2014-11-06 21:39:02 +00:00
|
|
|
m_context << (isSigned ? eth::Instruction::SGT : eth::Instruction::GT)
|
2014-10-31 16:20:27 +00:00
|
|
|
<< eth::Instruction::ISZERO;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
case Token::GT:
|
2014-11-06 21:39:02 +00:00
|
|
|
m_context << (isSigned ? eth::Instruction::SGT : eth::Instruction::GT);
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
case Token::LT:
|
2014-11-06 21:39:02 +00:00
|
|
|
m_context << (isSigned ? eth::Instruction::SLT : eth::Instruction::LT);
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-11-05 13:20:56 +00:00
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown comparison operator."));
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionCompiler::appendOrdinaryBinaryOperatorCode(Token::Value _operator, Type const& _type)
|
|
|
|
{
|
|
|
|
if (Token::isArithmeticOp(_operator))
|
|
|
|
appendArithmeticOperatorCode(_operator, _type);
|
|
|
|
else if (Token::isBitOp(_operator))
|
|
|
|
appendBitOperatorCode(_operator);
|
|
|
|
else if (Token::isShiftOp(_operator))
|
|
|
|
appendShiftOperatorCode(_operator);
|
|
|
|
else
|
2014-11-05 13:20:56 +00:00
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown binary operator."));
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionCompiler::appendArithmeticOperatorCode(Token::Value _operator, Type const& _type)
|
|
|
|
{
|
2014-11-05 13:20:56 +00:00
|
|
|
IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
|
|
|
|
bool const isSigned = type.isSigned();
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
switch (_operator)
|
|
|
|
{
|
|
|
|
case Token::ADD:
|
|
|
|
m_context << eth::Instruction::ADD;
|
|
|
|
break;
|
|
|
|
case Token::SUB:
|
2014-11-06 21:39:02 +00:00
|
|
|
m_context << eth::Instruction::SUB;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
case Token::MUL:
|
|
|
|
m_context << eth::Instruction::MUL;
|
|
|
|
break;
|
|
|
|
case Token::DIV:
|
2014-11-06 21:39:02 +00:00
|
|
|
m_context << (isSigned ? eth::Instruction::SDIV : eth::Instruction::DIV);
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
case Token::MOD:
|
2014-11-06 21:39:02 +00:00
|
|
|
m_context << (isSigned ? eth::Instruction::SMOD : eth::Instruction::MOD);
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-11-05 13:20:56 +00:00
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown arithmetic operator."));
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionCompiler::appendBitOperatorCode(Token::Value _operator)
|
|
|
|
{
|
|
|
|
switch (_operator)
|
|
|
|
{
|
|
|
|
case Token::BIT_OR:
|
|
|
|
m_context << eth::Instruction::OR;
|
|
|
|
break;
|
|
|
|
case Token::BIT_AND:
|
|
|
|
m_context << eth::Instruction::AND;
|
|
|
|
break;
|
|
|
|
case Token::BIT_XOR:
|
|
|
|
m_context << eth::Instruction::XOR;
|
|
|
|
break;
|
|
|
|
default:
|
2014-11-05 13:20:56 +00:00
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown bit operator."));
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionCompiler::appendShiftOperatorCode(Token::Value _operator)
|
|
|
|
{
|
2014-11-05 13:20:56 +00:00
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Shift operators not yet implemented."));
|
2014-10-30 00:20:32 +00:00
|
|
|
switch (_operator)
|
|
|
|
{
|
|
|
|
case Token::SHL:
|
|
|
|
break;
|
|
|
|
case Token::SAR:
|
|
|
|
break;
|
|
|
|
default:
|
2014-11-05 13:20:56 +00:00
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown shift operator."));
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-04 20:29:36 +00:00
|
|
|
void ExpressionCompiler::appendTypeConversion(Type const& _typeOnStack, Type const& _targetType, bool _cleanupNeeded)
|
2014-11-04 18:13:03 +00:00
|
|
|
{
|
2014-11-06 21:39:02 +00:00
|
|
|
// For a type extension, we need to remove all higher-order bits that we might have ignored in
|
|
|
|
// previous operations.
|
|
|
|
// @todo: store in the AST whether the operand might have "dirty" higher order bits
|
2014-11-04 18:13:03 +00:00
|
|
|
|
2014-11-04 20:29:36 +00:00
|
|
|
if (_typeOnStack == _targetType && !_cleanupNeeded)
|
2014-11-04 18:13:03 +00:00
|
|
|
return;
|
|
|
|
if (_typeOnStack.getCategory() == Type::Category::INTEGER)
|
|
|
|
appendHighBitsCleanup(dynamic_cast<IntegerType const&>(_typeOnStack));
|
2014-11-04 20:29:36 +00:00
|
|
|
else if (_typeOnStack != _targetType)
|
2014-11-04 18:13:03 +00:00
|
|
|
// All other types should not be convertible to non-equal types.
|
2014-11-06 21:04:10 +00:00
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid type conversion requested."));
|
2014-11-04 18:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionCompiler::appendHighBitsCleanup(IntegerType const& _typeOnStack)
|
|
|
|
{
|
|
|
|
if (_typeOnStack.getNumBits() == 256)
|
|
|
|
return;
|
|
|
|
else if (_typeOnStack.isSigned())
|
|
|
|
m_context << u256(_typeOnStack.getNumBits() / 8 - 1) << eth::Instruction::SIGNEXTEND;
|
|
|
|
else
|
|
|
|
m_context << ((u256(1) << _typeOnStack.getNumBits()) - 1) << eth::Instruction::AND;
|
|
|
|
}
|
|
|
|
|
2014-11-10 16:31:09 +00:00
|
|
|
void ExpressionCompiler::LValue::retrieveValue(Expression const& _expression, bool _remove) const
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2014-11-10 16:31:09 +00:00
|
|
|
switch (m_type)
|
2014-11-07 01:06:37 +00:00
|
|
|
{
|
2014-11-10 16:31:09 +00:00
|
|
|
case CODE:
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Tried to retrieve value of a function."));
|
2014-11-07 01:06:37 +00:00
|
|
|
break;
|
2014-11-10 16:31:09 +00:00
|
|
|
case STACK:
|
2014-11-07 01:06:37 +00:00
|
|
|
{
|
2014-11-10 16:31:09 +00:00
|
|
|
unsigned stackPos = m_context->baseToCurrentStackOffset(unsigned(m_baseStackOffset));
|
2014-11-07 01:06:37 +00:00
|
|
|
if (stackPos >= 15) //@todo correct this by fetching earlier or moving to memory
|
|
|
|
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_expression.getLocation())
|
|
|
|
<< errinfo_comment("Stack too deep."));
|
2014-11-10 16:31:09 +00:00
|
|
|
*m_context << eth::dupInstruction(stackPos + 1);
|
2014-11-07 01:06:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-11-10 16:31:09 +00:00
|
|
|
case STORAGE:
|
|
|
|
if (!_remove)
|
|
|
|
*m_context << eth::Instruction::DUP1;
|
|
|
|
*m_context << eth::Instruction::SLOAD;
|
2014-11-07 01:06:37 +00:00
|
|
|
break;
|
2014-11-10 16:31:09 +00:00
|
|
|
case MEMORY:
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation())
|
|
|
|
<< errinfo_comment("Location type not yet implemented."));
|
2014-11-07 01:06:37 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-11-10 16:31:09 +00:00
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation())
|
|
|
|
<< errinfo_comment("Unsupported location type."));
|
2014-11-07 01:06:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
2014-11-10 16:31:09 +00:00
|
|
|
void ExpressionCompiler::LValue::storeValue(Expression const& _expression, bool _move) const
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2014-11-10 16:31:09 +00:00
|
|
|
switch (m_type)
|
2014-11-07 01:06:37 +00:00
|
|
|
{
|
2014-11-10 16:31:09 +00:00
|
|
|
case STACK:
|
2014-11-07 01:06:37 +00:00
|
|
|
{
|
2014-11-10 16:31:09 +00:00
|
|
|
unsigned stackPos = m_context->baseToCurrentStackOffset(unsigned(m_baseStackOffset));
|
2014-11-07 01:06:37 +00:00
|
|
|
if (stackPos > 16)
|
|
|
|
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_expression.getLocation())
|
|
|
|
<< errinfo_comment("Stack too deep."));
|
|
|
|
else if (stackPos > 0)
|
2014-11-10 16:31:09 +00:00
|
|
|
*m_context << eth::swapInstruction(stackPos) << eth::Instruction::POP;
|
2014-11-07 01:06:37 +00:00
|
|
|
if (!_move)
|
2014-11-10 16:31:09 +00:00
|
|
|
retrieveValue(_expression);
|
2014-11-07 01:06:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-11-10 16:31:09 +00:00
|
|
|
case LValue::STORAGE:
|
2014-11-07 01:06:37 +00:00
|
|
|
if (!_move)
|
2014-11-10 16:31:09 +00:00
|
|
|
*m_context << eth::Instruction::DUP2 << eth::Instruction::SWAP1;
|
|
|
|
*m_context << eth::Instruction::SSTORE;
|
2014-11-07 01:06:37 +00:00
|
|
|
break;
|
2014-11-10 16:31:09 +00:00
|
|
|
case LValue::CODE:
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation())
|
|
|
|
<< errinfo_comment("Location type does not support assignment."));
|
2014-11-07 01:06:37 +00:00
|
|
|
break;
|
2014-11-10 16:31:09 +00:00
|
|
|
case LValue::MEMORY:
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation())
|
|
|
|
<< errinfo_comment("Location type not yet implemented."));
|
2014-11-07 01:06:37 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-11-10 16:31:09 +00:00
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation())
|
|
|
|
<< errinfo_comment("Unsupported location type."));
|
2014-11-07 01:06:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
2014-11-10 16:31:09 +00:00
|
|
|
void ExpressionCompiler::LValue::retrieveValueIfLValueNotRequested(const Expression& _expression)
|
|
|
|
{
|
|
|
|
if (!_expression.lvalueRequested())
|
|
|
|
{
|
|
|
|
retrieveValue(_expression, true);
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionCompiler::LValue::fromDeclaration( Expression const& _expression, Declaration const& _declaration)
|
|
|
|
{
|
|
|
|
if (m_context->isLocalVariable(&_declaration))
|
|
|
|
{
|
|
|
|
m_type = STACK;
|
|
|
|
m_baseStackOffset = m_context->getBaseStackOffsetOfVariable(_declaration);
|
|
|
|
}
|
|
|
|
else if (m_context->isStateVariable(&_declaration))
|
|
|
|
{
|
|
|
|
m_type = STORAGE;
|
|
|
|
*m_context << m_context->getStorageLocationOfVariable(_declaration);
|
|
|
|
}
|
|
|
|
else if (m_context->isFunctionDefinition(&_declaration))
|
|
|
|
{
|
|
|
|
m_type = CODE;
|
|
|
|
*m_context << m_context->getFunctionEntryLabel(dynamic_cast<FunctionDefinition const&>(_declaration)).pushTag();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation())
|
|
|
|
<< errinfo_comment("Identifier type not supported or identifier not found."));
|
|
|
|
}
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
}
|