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-12-08 21:18:19 +00:00
|
|
|
#include <libsolidity/CompilerUtils.h>
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2014-12-17 15:23:18 +00:00
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2014-12-11 16:35:23 +00:00
|
|
|
void ExpressionCompiler::compileExpression(CompilerContext& _context, Expression const& _expression, bool _optimize)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2014-12-11 16:35:23 +00:00
|
|
|
ExpressionCompiler compiler(_context, _optimize);
|
2014-10-30 00:20:32 +00:00
|
|
|
_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-12-06 00:06:24 +00:00
|
|
|
bool ExpressionCompiler::visit(Assignment const& _assignment)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
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-12-17 15:23:18 +00:00
|
|
|
solAssert(m_currentLValue.isValid(), "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-12-18 21:15:11 +00:00
|
|
|
if (m_currentLValue.storesReferenceOnStack())
|
|
|
|
m_context << eth::Instruction::SWAP1;
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-19 10:31:17 +00:00
|
|
|
bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
|
|
|
//@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
|
2014-12-19 10:31:17 +00:00
|
|
|
if (_unaryOperation.getType()->getCategory() == Type::Category::INTEGER_CONSTANT)
|
|
|
|
{
|
|
|
|
m_context << _unaryOperation.getType()->literalValue(nullptr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_unaryOperation.getSubExpression().accept(*this);
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
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-12-17 15:23:18 +00:00
|
|
|
solAssert(m_currentLValue.isValid(), "LValue not retrieved.");
|
2014-11-10 16:31:09 +00:00
|
|
|
|
|
|
|
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-12-17 15:23:18 +00:00
|
|
|
solAssert(m_currentLValue.isValid(), "LValue not retrieved.");
|
2014-11-10 16:31:09 +00:00
|
|
|
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
|
|
|
}
|
2014-12-19 10:31:17 +00:00
|
|
|
return false;
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:06:24 +00:00
|
|
|
bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2014-12-06 00:06:24 +00:00
|
|
|
Expression const& leftExpression = _binaryOperation.getLeftExpression();
|
|
|
|
Expression const& 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);
|
2014-12-19 10:31:17 +00:00
|
|
|
else if (commonType.getCategory() == Type::Category::INTEGER_CONSTANT)
|
|
|
|
m_context << commonType.literalValue(nullptr);
|
2014-10-30 00:20:32 +00:00
|
|
|
else
|
|
|
|
{
|
2014-12-19 10:31:17 +00:00
|
|
|
bool cleanupNeeded = commonType.getCategory() == Type::Category::INTEGER &&
|
|
|
|
(Token::isCompareOp(op) || op == Token::DIV || op == Token::MOD);
|
2014-11-04 18:13:03 +00:00
|
|
|
|
2014-12-11 16:35:23 +00:00
|
|
|
// for commutative operators, push the literal as late as possible to allow improved optimization
|
2014-12-19 10:31:17 +00:00
|
|
|
auto isLiteral = [](Expression const& _e)
|
|
|
|
{
|
|
|
|
return dynamic_cast<Literal const*>(&_e) || _e.getType()->getCategory() == Type::Category::INTEGER_CONSTANT;
|
|
|
|
};
|
|
|
|
bool swap = m_optimize && Token::isCommutativeOp(op) && isLiteral(rightExpression) && !isLiteral(leftExpression);
|
2014-12-11 16:35:23 +00:00
|
|
|
if (swap)
|
|
|
|
{
|
|
|
|
leftExpression.accept(*this);
|
|
|
|
appendTypeConversion(*leftExpression.getType(), commonType, cleanupNeeded);
|
|
|
|
rightExpression.accept(*this);
|
|
|
|
appendTypeConversion(*rightExpression.getType(), commonType, cleanupNeeded);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rightExpression.accept(*this);
|
|
|
|
appendTypeConversion(*rightExpression.getType(), commonType, cleanupNeeded);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-06 00:06:24 +00:00
|
|
|
bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2014-11-26 12:19:17 +00:00
|
|
|
using Location = FunctionType::Location;
|
2014-10-30 00:20:32 +00:00
|
|
|
if (_functionCall.isTypeConversion())
|
|
|
|
{
|
2014-11-21 18:14:56 +00:00
|
|
|
//@todo struct construction
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(_functionCall.getArguments().size() == 1, "");
|
2014-12-06 00:06:24 +00:00
|
|
|
Expression const& firstArgument = *_functionCall.getArguments().front();
|
2014-10-30 00:20:32 +00:00
|
|
|
firstArgument.accept(*this);
|
2014-11-20 17:33:23 +00:00
|
|
|
if (firstArgument.getType()->getCategory() == Type::Category::CONTRACT &&
|
|
|
|
_functionCall.getType()->getCategory() == Type::Category::INTEGER)
|
|
|
|
{
|
|
|
|
// explicit type conversion contract -> address, nothing to do.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
appendTypeConversion(*firstArgument.getType(), *_functionCall.getType());
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-25 13:43:23 +00:00
|
|
|
FunctionType const& function = dynamic_cast<FunctionType const&>(*_functionCall.getExpression().getType());
|
2014-12-10 22:01:40 +00:00
|
|
|
vector<ASTPointer<Expression const>> arguments = _functionCall.getArguments();
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(arguments.size() == function.getParameterTypes().size(), "");
|
2014-11-25 17:23:39 +00:00
|
|
|
|
2014-12-04 18:38:24 +00:00
|
|
|
switch (function.getLocation())
|
|
|
|
{
|
|
|
|
case Location::INTERNAL:
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2014-11-25 17:23:39 +00:00
|
|
|
// Calling convention: Caller pushes return address and arguments
|
|
|
|
// Callee removes them and pushes return values
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2014-11-25 17:23:39 +00:00
|
|
|
eth::AssemblyItem returnLabel = m_context.pushNewTag();
|
|
|
|
for (unsigned i = 0; i < arguments.size(); ++i)
|
|
|
|
{
|
|
|
|
arguments[i]->accept(*this);
|
|
|
|
appendTypeConversion(*arguments[i]->getType(), *function.getParameterTypes()[i]);
|
|
|
|
}
|
|
|
|
_functionCall.getExpression().accept(*this);
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2014-11-25 17:23:39 +00:00
|
|
|
m_context.appendJump();
|
|
|
|
m_context << returnLabel;
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2014-12-08 21:18:19 +00:00
|
|
|
unsigned returnParametersSize = CompilerUtils::getSizeOnStack(function.getReturnParameterTypes());
|
2014-11-25 17:23:39 +00:00
|
|
|
// callee adds return parameters, but removes arguments and return label
|
2015-01-07 11:50:23 +00:00
|
|
|
m_context.adjustStackOffset(returnParametersSize - CompilerUtils::getSizeOnStack(function.getParameterTypes()) - 1);
|
2014-11-25 17:23:39 +00:00
|
|
|
|
|
|
|
// @todo for now, the return value of a function is its first return value, so remove
|
|
|
|
// all others
|
|
|
|
for (unsigned i = 1; i < function.getReturnParameterTypes().size(); ++i)
|
2014-12-08 21:18:19 +00:00
|
|
|
CompilerUtils(m_context).popStackElement(*function.getReturnParameterTypes()[i]);
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
2014-11-25 17:23:39 +00:00
|
|
|
}
|
2014-12-04 18:38:24 +00:00
|
|
|
case Location::EXTERNAL:
|
2014-12-10 22:01:40 +00:00
|
|
|
case Location::BARE:
|
2014-11-25 17:23:39 +00:00
|
|
|
{
|
2014-12-10 22:01:40 +00:00
|
|
|
FunctionCallOptions options;
|
|
|
|
options.bare = function.getLocation() == Location::BARE;
|
|
|
|
options.obtainAddress = [&]() { _functionCall.getExpression().accept(*this); };
|
|
|
|
appendExternalFunctionCall(function, arguments, options);
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Location::SEND:
|
2014-12-10 22:01:40 +00:00
|
|
|
{
|
|
|
|
FunctionCallOptions options;
|
|
|
|
options.bare = true;
|
|
|
|
options.obtainAddress = [&]() { _functionCall.getExpression().accept(*this); };
|
|
|
|
options.obtainValue = [&]() { arguments.front()->accept(*this); };
|
|
|
|
appendExternalFunctionCall(FunctionType({}, {}, Location::EXTERNAL), {}, options);
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
2014-12-10 22:01:40 +00:00
|
|
|
}
|
2014-12-04 18:38:24 +00:00
|
|
|
case Location::SUICIDE:
|
|
|
|
arguments.front()->accept(*this);
|
|
|
|
//@todo might not be necessary
|
|
|
|
appendTypeConversion(*arguments.front()->getType(), *function.getParameterTypes().front(), true);
|
|
|
|
m_context << eth::Instruction::SUICIDE;
|
|
|
|
break;
|
|
|
|
case Location::SHA3:
|
|
|
|
arguments.front()->accept(*this);
|
|
|
|
appendTypeConversion(*arguments.front()->getType(), *function.getParameterTypes().front(), true);
|
|
|
|
// @todo move this once we actually use memory
|
2014-12-10 16:15:17 +00:00
|
|
|
CompilerUtils(m_context).storeInMemory(0);
|
|
|
|
m_context << u256(32) << u256(0) << eth::Instruction::SHA3;
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
2015-01-08 23:22:06 +00:00
|
|
|
case Location::LOG0:
|
|
|
|
arguments.front()->accept(*this);
|
|
|
|
appendTypeConversion(*arguments.front()->getType(), *function.getParameterTypes().front(), true);
|
|
|
|
// @todo move this once we actually use memory
|
|
|
|
CompilerUtils(m_context).storeInMemory(0);
|
|
|
|
m_context << u256(32) << u256(0) << eth::Instruction::LOG0;
|
|
|
|
break;
|
|
|
|
case Location::LOG1:
|
|
|
|
arguments[1]->accept(*this);
|
|
|
|
arguments[0]->accept(*this);
|
|
|
|
appendTypeConversion(*arguments[1]->getType(), *function.getParameterTypes()[1], true);
|
|
|
|
appendTypeConversion(*arguments[0]->getType(), *function.getParameterTypes()[0], true);
|
|
|
|
// @todo move this once we actually use memory
|
|
|
|
CompilerUtils(m_context).storeInMemory(0);
|
|
|
|
m_context << u256(32) << u256(0) << eth::Instruction::LOG1;
|
|
|
|
break;
|
|
|
|
case Location::LOG2:
|
|
|
|
arguments[2]->accept(*this);
|
|
|
|
arguments[1]->accept(*this);
|
|
|
|
arguments[0]->accept(*this);
|
|
|
|
appendTypeConversion(*arguments[2]->getType(), *function.getParameterTypes()[2], true);
|
|
|
|
appendTypeConversion(*arguments[1]->getType(), *function.getParameterTypes()[1], true);
|
|
|
|
appendTypeConversion(*arguments[0]->getType(), *function.getParameterTypes()[0], true);
|
|
|
|
// @todo move this once we actually use memory
|
|
|
|
CompilerUtils(m_context).storeInMemory(0);
|
|
|
|
m_context << u256(32) << u256(0) << eth::Instruction::LOG2;
|
|
|
|
break;
|
|
|
|
case Location::LOG3:
|
|
|
|
arguments[3]->accept(*this);
|
|
|
|
arguments[2]->accept(*this);
|
|
|
|
arguments[1]->accept(*this);
|
|
|
|
arguments[0]->accept(*this);
|
|
|
|
appendTypeConversion(*arguments[3]->getType(), *function.getParameterTypes()[3], true);
|
|
|
|
appendTypeConversion(*arguments[2]->getType(), *function.getParameterTypes()[2], true);
|
|
|
|
appendTypeConversion(*arguments[1]->getType(), *function.getParameterTypes()[1], true);
|
|
|
|
appendTypeConversion(*arguments[0]->getType(), *function.getParameterTypes()[0], true);
|
|
|
|
// @todo move this once we actually use memory
|
|
|
|
CompilerUtils(m_context).storeInMemory(0);
|
|
|
|
m_context << u256(32) << u256(0) << eth::Instruction::LOG3;
|
|
|
|
break;
|
|
|
|
case Location::LOG4:
|
|
|
|
arguments[4]->accept(*this);
|
|
|
|
arguments[3]->accept(*this);
|
|
|
|
arguments[2]->accept(*this);
|
|
|
|
arguments[1]->accept(*this);
|
|
|
|
arguments[0]->accept(*this);
|
|
|
|
appendTypeConversion(*arguments[4]->getType(), *function.getParameterTypes()[4], true);
|
|
|
|
appendTypeConversion(*arguments[3]->getType(), *function.getParameterTypes()[3], true);
|
|
|
|
appendTypeConversion(*arguments[2]->getType(), *function.getParameterTypes()[2], true);
|
|
|
|
appendTypeConversion(*arguments[1]->getType(), *function.getParameterTypes()[1], true);
|
|
|
|
appendTypeConversion(*arguments[0]->getType(), *function.getParameterTypes()[0], true);
|
|
|
|
// @todo move this once we actually use memory
|
|
|
|
CompilerUtils(m_context).storeInMemory(0);
|
|
|
|
m_context << u256(32) << u256(0) << eth::Instruction::LOG4;
|
|
|
|
break;
|
2014-12-04 18:38:24 +00:00
|
|
|
case Location::ECRECOVER:
|
|
|
|
case Location::SHA256:
|
|
|
|
case Location::RIPEMD160:
|
|
|
|
{
|
|
|
|
static const map<Location, u256> contractAddresses{{Location::ECRECOVER, 1},
|
|
|
|
{Location::SHA256, 2},
|
|
|
|
{Location::RIPEMD160, 3}};
|
|
|
|
u256 contractAddress = contractAddresses.find(function.getLocation())->second;
|
2014-12-10 22:01:40 +00:00
|
|
|
FunctionCallOptions options;
|
|
|
|
options.bare = true;
|
|
|
|
options.obtainAddress = [&]() { m_context << contractAddress; };
|
|
|
|
options.packDensely = false;
|
|
|
|
appendExternalFunctionCall(function, arguments, options);
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid function type."));
|
2014-11-25 17:23:39 +00:00
|
|
|
}
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-12 15:49:26 +00:00
|
|
|
bool ExpressionCompiler::visit(NewExpression const& _newExpression)
|
|
|
|
{
|
|
|
|
ContractType const* type = dynamic_cast<ContractType const*>(_newExpression.getType().get());
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(type, "");
|
2014-12-12 15:49:26 +00:00
|
|
|
TypePointers const& types = type->getConstructorType()->getParameterTypes();
|
|
|
|
vector<ASTPointer<Expression const>> arguments = _newExpression.getArguments();
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(arguments.size() == types.size(), "");
|
2014-12-12 15:49:26 +00:00
|
|
|
|
|
|
|
// copy the contracts code into memory
|
|
|
|
bytes const& bytecode = m_context.getCompiledContract(*_newExpression.getContract());
|
|
|
|
m_context << u256(bytecode.size());
|
|
|
|
//@todo could be done by actually appending the Assembly, but then we probably need to compile
|
|
|
|
// multiple times. Will revisit once external fuctions are inlined.
|
|
|
|
m_context.appendData(bytecode);
|
|
|
|
//@todo copy to memory position 0, shift as soon as we use memory
|
|
|
|
m_context << u256(0) << eth::Instruction::CODECOPY;
|
|
|
|
|
|
|
|
unsigned dataOffset = bytecode.size();
|
|
|
|
for (unsigned i = 0; i < arguments.size(); ++i)
|
|
|
|
{
|
|
|
|
arguments[i]->accept(*this);
|
|
|
|
appendTypeConversion(*arguments[i]->getType(), *types[i]);
|
|
|
|
unsigned const numBytes = types[i]->getCalldataEncodedSize();
|
|
|
|
if (numBytes > 32)
|
|
|
|
BOOST_THROW_EXCEPTION(CompilerError()
|
|
|
|
<< errinfo_sourceLocation(arguments[i]->getLocation())
|
|
|
|
<< errinfo_comment("Type " + types[i]->toString() + " not yet supported."));
|
|
|
|
bool const leftAligned = types[i]->getCategory() == Type::Category::STRING;
|
|
|
|
CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned);
|
|
|
|
dataOffset += numBytes;
|
|
|
|
}
|
|
|
|
// size, offset, endowment
|
|
|
|
m_context << u256(dataOffset) << u256(0) << u256(0) << eth::Instruction::CREATE;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-06 00:06:24 +00:00
|
|
|
void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2014-11-24 12:23:58 +00:00
|
|
|
ASTString const& member = _memberAccess.getMemberName();
|
2014-11-21 18:14:56 +00:00
|
|
|
switch (_memberAccess.getExpression().getType()->getCategory())
|
|
|
|
{
|
|
|
|
case Type::Category::INTEGER:
|
2014-11-25 17:23:39 +00:00
|
|
|
if (member == "balance")
|
2014-11-26 12:19:17 +00:00
|
|
|
{
|
|
|
|
appendTypeConversion(*_memberAccess.getExpression().getType(),
|
|
|
|
IntegerType(0, IntegerType::Modifier::ADDRESS), true);
|
2014-11-25 17:23:39 +00:00
|
|
|
m_context << eth::Instruction::BALANCE;
|
2014-11-26 12:19:17 +00:00
|
|
|
}
|
2014-12-15 00:02:33 +00:00
|
|
|
else if (member == "send" || member.substr(0, min<size_t>(member.size(), 4)) == "call")
|
2014-11-26 12:19:17 +00:00
|
|
|
appendTypeConversion(*_memberAccess.getExpression().getType(),
|
|
|
|
IntegerType(0, IntegerType::Modifier::ADDRESS), true);
|
2014-11-25 17:23:39 +00:00
|
|
|
else
|
2014-11-21 18:14:56 +00:00
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid member access to integer."));
|
|
|
|
break;
|
|
|
|
case Type::Category::CONTRACT:
|
2014-12-04 18:38:24 +00:00
|
|
|
{
|
|
|
|
ContractType const& type = dynamic_cast<ContractType const&>(*_memberAccess.getExpression().getType());
|
2015-01-08 16:18:31 +00:00
|
|
|
m_context << type.getFunctionIdentifier(member);
|
2014-11-21 18:14:56 +00:00
|
|
|
break;
|
2014-12-04 18:38:24 +00:00
|
|
|
}
|
2014-11-21 18:14:56 +00:00
|
|
|
case Type::Category::MAGIC:
|
2014-11-24 12:23:58 +00:00
|
|
|
// we can ignore the kind of magic and only look at the name of the member
|
|
|
|
if (member == "coinbase")
|
|
|
|
m_context << eth::Instruction::COINBASE;
|
|
|
|
else if (member == "timestamp")
|
|
|
|
m_context << eth::Instruction::TIMESTAMP;
|
2015-01-05 11:35:56 +00:00
|
|
|
/* else if (member == "blockhash")
|
|
|
|
m_context << eth::Instruction::BLOCKHASH;
|
|
|
|
*/ else if (member == "difficulty")
|
2014-11-24 12:23:58 +00:00
|
|
|
m_context << eth::Instruction::DIFFICULTY;
|
|
|
|
else if (member == "number")
|
|
|
|
m_context << eth::Instruction::NUMBER;
|
|
|
|
else if (member == "gaslimit")
|
|
|
|
m_context << eth::Instruction::GASLIMIT;
|
|
|
|
else if (member == "sender")
|
|
|
|
m_context << eth::Instruction::CALLER;
|
|
|
|
else if (member == "value")
|
|
|
|
m_context << eth::Instruction::CALLVALUE;
|
|
|
|
else if (member == "origin")
|
|
|
|
m_context << eth::Instruction::ORIGIN;
|
|
|
|
else if (member == "gas")
|
|
|
|
m_context << eth::Instruction::GAS;
|
|
|
|
else if (member == "gasprice")
|
|
|
|
m_context << eth::Instruction::GASPRICE;
|
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown magic member."));
|
2014-11-21 18:14:56 +00:00
|
|
|
break;
|
|
|
|
case Type::Category::STRUCT:
|
|
|
|
{
|
|
|
|
StructType const& type = dynamic_cast<StructType const&>(*_memberAccess.getExpression().getType());
|
2014-11-24 12:23:58 +00:00
|
|
|
m_context << type.getStorageOffsetOfMember(member) << eth::Instruction::ADD;
|
2014-12-08 21:18:19 +00:00
|
|
|
m_currentLValue = LValue(m_context, LValue::STORAGE, *_memberAccess.getType());
|
2014-11-21 18:14:56 +00:00
|
|
|
m_currentLValue.retrieveValueIfLValueNotRequested(_memberAccess);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Member access to unknown type."));
|
|
|
|
}
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:06:24 +00:00
|
|
|
bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2014-11-10 16:31:09 +00:00
|
|
|
_indexAccess.getBaseExpression().accept(*this);
|
|
|
|
_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
|
2014-12-10 16:15:17 +00:00
|
|
|
CompilerUtils(m_context).storeInMemory(0);
|
|
|
|
CompilerUtils(m_context).storeInMemory(32);
|
2014-11-10 16:31:09 +00:00
|
|
|
m_context << u256(64) << u256(0) << eth::Instruction::SHA3;
|
|
|
|
|
2014-12-08 21:18:19 +00:00
|
|
|
m_currentLValue = LValue(m_context, LValue::STORAGE, *_indexAccess.getType());
|
2014-11-10 16:31:09 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-12-06 00:06:24 +00:00
|
|
|
void ExpressionCompiler::endVisit(Identifier const& _identifier)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2014-12-06 00:06:24 +00:00
|
|
|
Declaration const* declaration = _identifier.getReferencedDeclaration();
|
|
|
|
if (MagicVariableDeclaration const* magicVar = dynamic_cast<MagicVariableDeclaration const*>(declaration))
|
2014-11-21 18:14:56 +00:00
|
|
|
{
|
2014-11-26 12:19:17 +00:00
|
|
|
if (magicVar->getType()->getCategory() == Type::Category::CONTRACT) // must be "this"
|
2014-11-21 18:14:56 +00:00
|
|
|
m_context << eth::Instruction::ADDRESS;
|
|
|
|
return;
|
|
|
|
}
|
2014-12-06 00:06:24 +00:00
|
|
|
if (FunctionDefinition const* functionDef = dynamic_cast<FunctionDefinition const*>(declaration))
|
2014-11-21 18:14:56 +00:00
|
|
|
{
|
|
|
|
m_context << m_context.getFunctionEntryLabel(*functionDef).pushTag();
|
|
|
|
return;
|
|
|
|
}
|
2014-12-06 00:06:24 +00:00
|
|
|
if (dynamic_cast<VariableDeclaration const*>(declaration))
|
2014-11-21 18:14:56 +00:00
|
|
|
{
|
2014-12-06 00:06:24 +00:00
|
|
|
m_currentLValue.fromIdentifier(_identifier, *declaration);
|
2014-11-21 18:14:56 +00:00
|
|
|
m_currentLValue.retrieveValueIfLValueNotRequested(_identifier);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Identifier type not expected in expression context."));
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:06:24 +00:00
|
|
|
void ExpressionCompiler::endVisit(Literal const& _literal)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
|
|
|
switch (_literal.getType()->getCategory())
|
|
|
|
{
|
2014-12-19 10:31:17 +00:00
|
|
|
case Type::Category::INTEGER_CONSTANT:
|
2014-10-30 00:20:32 +00:00
|
|
|
case Type::Category::BOOL:
|
2014-12-09 17:46:18 +00:00
|
|
|
case Type::Category::STRING:
|
2014-12-19 10:31:17 +00:00
|
|
|
m_context << _literal.getType()->literalValue(&_literal);
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-12-09 17:46:18 +00:00
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Only integer, boolean and string literals implemented for now."));
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-06 00:06:24 +00:00
|
|
|
void ExpressionCompiler::appendAndOrOperatorCode(BinaryOperation const& _binaryOperation)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
|
|
|
Token::Value const op = _binaryOperation.getOperator();
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(op == Token::OR || op == Token::AND, "");
|
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;
|
2014-12-19 10:31:17 +00:00
|
|
|
Type::Category stackTypeCategory = _typeOnStack.getCategory();
|
|
|
|
Type::Category targetTypeCategory = _targetType.getCategory();
|
|
|
|
if (stackTypeCategory == Type::Category::INTEGER)
|
|
|
|
{
|
|
|
|
solAssert(targetTypeCategory == Type::Category::INTEGER || targetTypeCategory == Type::Category::CONTRACT, "");
|
2014-11-04 18:13:03 +00:00
|
|
|
appendHighBitsCleanup(dynamic_cast<IntegerType const&>(_typeOnStack));
|
2014-12-19 10:31:17 +00:00
|
|
|
}
|
|
|
|
else if (stackTypeCategory == Type::Category::INTEGER_CONSTANT)
|
|
|
|
solAssert(targetTypeCategory == Type::Category::INTEGER || targetTypeCategory == Type::Category::CONTRACT, "");
|
|
|
|
else if (stackTypeCategory == Type::Category::STRING)
|
2014-12-09 17:46:18 +00:00
|
|
|
{
|
|
|
|
// nothing to do, strings are high-order-bit-aligned
|
|
|
|
//@todo clear lower-order bytes if we allow explicit conversion to shorter strings
|
|
|
|
}
|
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-12-10 22:01:40 +00:00
|
|
|
void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functionType,
|
|
|
|
vector<ASTPointer<Expression const>> const& _arguments,
|
|
|
|
FunctionCallOptions const& _options)
|
|
|
|
{
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(_arguments.size() == _functionType.getParameterTypes().size(), "");
|
2014-12-10 22:01:40 +00:00
|
|
|
|
2015-01-08 16:18:31 +00:00
|
|
|
_options.obtainAddress();
|
|
|
|
if (!_options.bare)
|
2015-01-08 23:27:26 +00:00
|
|
|
CompilerUtils(m_context).storeInMemory(0, CompilerUtils::dataStartOffset);
|
2015-01-08 16:18:31 +00:00
|
|
|
|
2015-01-08 23:27:26 +00:00
|
|
|
unsigned dataOffset = _options.bare ? 0 : CompilerUtils::dataStartOffset; // reserve 4 bytes for the function's hash identifier
|
2014-12-10 22:01:40 +00:00
|
|
|
for (unsigned i = 0; i < _arguments.size(); ++i)
|
|
|
|
{
|
|
|
|
_arguments[i]->accept(*this);
|
|
|
|
Type const& type = *_functionType.getParameterTypes()[i];
|
|
|
|
appendTypeConversion(*_arguments[i]->getType(), type);
|
|
|
|
unsigned const numBytes = _options.packDensely ? type.getCalldataEncodedSize() : 32;
|
|
|
|
if (numBytes == 0 || numBytes > 32)
|
|
|
|
BOOST_THROW_EXCEPTION(CompilerError()
|
|
|
|
<< errinfo_sourceLocation(_arguments[i]->getLocation())
|
|
|
|
<< errinfo_comment("Type " + type.toString() + " not yet supported."));
|
|
|
|
bool const leftAligned = type.getCategory() == Type::Category::STRING;
|
|
|
|
CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned);
|
|
|
|
dataOffset += numBytes;
|
|
|
|
}
|
|
|
|
//@todo only return the first return value for now
|
|
|
|
Type const* firstType = _functionType.getReturnParameterTypes().empty() ? nullptr :
|
|
|
|
_functionType.getReturnParameterTypes().front().get();
|
|
|
|
unsigned retSize = firstType ? firstType->getCalldataEncodedSize() : 0;
|
|
|
|
if (!_options.packDensely && retSize > 0)
|
|
|
|
retSize = 32;
|
|
|
|
// CALL arguments: outSize, outOff, inSize, inOff, value, addr, gas (stack top)
|
|
|
|
m_context << u256(retSize) << u256(0) << u256(dataOffset) << u256(0);
|
|
|
|
if (_options.obtainValue)
|
|
|
|
_options.obtainValue();
|
|
|
|
else
|
|
|
|
m_context << u256(0);
|
2015-01-08 16:18:31 +00:00
|
|
|
m_context << eth::dupInstruction(6); //copy contract address
|
|
|
|
|
2014-12-10 22:01:40 +00:00
|
|
|
m_context << u256(25) << eth::Instruction::GAS << eth::Instruction::SUB
|
|
|
|
<< eth::Instruction::CALL
|
2015-01-08 16:18:31 +00:00
|
|
|
<< eth::Instruction::POP // @todo do not ignore failure indicator
|
|
|
|
<< eth::Instruction::POP; // pop contract address
|
|
|
|
|
2014-12-10 22:01:40 +00:00
|
|
|
if (retSize > 0)
|
|
|
|
{
|
|
|
|
bool const leftAligned = firstType->getCategory() == Type::Category::STRING;
|
|
|
|
CompilerUtils(m_context).loadFromMemory(0, retSize, leftAligned);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 13:37:37 +00:00
|
|
|
ExpressionCompiler::LValue::LValue(CompilerContext& _compilerContext, LValueType _type, Type const& _dataType,
|
2014-12-08 21:18:19 +00:00
|
|
|
unsigned _baseStackOffset):
|
|
|
|
m_context(&_compilerContext), m_type(_type), m_baseStackOffset(_baseStackOffset),
|
|
|
|
m_stackSize(_dataType.getSizeOnStack())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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 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-12-08 21:18:19 +00:00
|
|
|
for (unsigned i = 0; i < m_stackSize; ++i)
|
|
|
|
*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:
|
2014-11-21 18:14:56 +00:00
|
|
|
if (!_expression.getType()->isValueType())
|
|
|
|
break; // no distinction between value and reference for non-value types
|
2014-11-10 16:31:09 +00:00
|
|
|
if (!_remove)
|
|
|
|
*m_context << eth::Instruction::DUP1;
|
2014-12-08 21:18:19 +00:00
|
|
|
if (m_stackSize == 1)
|
|
|
|
*m_context << eth::Instruction::SLOAD;
|
|
|
|
else
|
|
|
|
for (unsigned i = 0; i < m_stackSize; ++i)
|
|
|
|
{
|
|
|
|
*m_context << eth::Instruction::DUP1 << eth::Instruction::SLOAD << eth::Instruction::SWAP1;
|
|
|
|
if (i + 1 < m_stackSize)
|
|
|
|
*m_context << u256(1) << eth::Instruction::ADD;
|
|
|
|
else
|
|
|
|
*m_context << eth::Instruction::POP;
|
|
|
|
}
|
2014-11-07 01:06:37 +00:00
|
|
|
break;
|
2014-11-10 16:31:09 +00:00
|
|
|
case MEMORY:
|
2014-11-21 18:14:56 +00:00
|
|
|
if (!_expression.getType()->isValueType())
|
|
|
|
break; // no distinction between value and reference for non-value types
|
2014-11-10 16:31:09 +00:00
|
|
|
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-12-08 21:18:19 +00:00
|
|
|
unsigned stackDiff = m_context->baseToCurrentStackOffset(unsigned(m_baseStackOffset)) - m_stackSize + 1;
|
|
|
|
if (stackDiff > 16)
|
2014-11-07 01:06:37 +00:00
|
|
|
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_expression.getLocation())
|
|
|
|
<< errinfo_comment("Stack too deep."));
|
2014-12-08 21:18:19 +00:00
|
|
|
else if (stackDiff > 0)
|
|
|
|
for (unsigned i = 0; i < m_stackSize; ++i)
|
|
|
|
*m_context << eth::swapInstruction(stackDiff) << 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-21 18:14:56 +00:00
|
|
|
if (!_expression.getType()->isValueType())
|
|
|
|
break; // no distinction between value and reference for non-value types
|
2014-12-08 21:18:19 +00:00
|
|
|
// stack layout: value value ... value ref
|
|
|
|
if (!_move) // copy values
|
|
|
|
{
|
|
|
|
if (m_stackSize + 1 > 16)
|
|
|
|
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_expression.getLocation())
|
|
|
|
<< errinfo_comment("Stack too deep."));
|
|
|
|
for (unsigned i = 0; i < m_stackSize; ++i)
|
|
|
|
*m_context << eth::dupInstruction(m_stackSize + 1) << eth::Instruction::SWAP1;
|
|
|
|
}
|
|
|
|
if (m_stackSize > 0) // store high index value first
|
|
|
|
*m_context << u256(m_stackSize - 1) << eth::Instruction::ADD;
|
|
|
|
for (unsigned i = 0; i < m_stackSize; ++i)
|
|
|
|
{
|
|
|
|
if (i + 1 >= m_stackSize)
|
|
|
|
*m_context << eth::Instruction::SSTORE;
|
|
|
|
else
|
|
|
|
// v v ... v v r+x
|
|
|
|
*m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2
|
|
|
|
<< eth::Instruction::SSTORE
|
|
|
|
<< u256(1) << eth::Instruction::SWAP1 << eth::Instruction::SUB;
|
|
|
|
}
|
2014-11-07 01:06:37 +00:00
|
|
|
break;
|
2014-11-10 16:31:09 +00:00
|
|
|
case LValue::MEMORY:
|
2014-11-21 18:14:56 +00:00
|
|
|
if (!_expression.getType()->isValueType())
|
|
|
|
break; // no distinction between value and reference for non-value types
|
2014-11-10 16:31:09 +00:00
|
|
|
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-20 17:33:23 +00:00
|
|
|
void ExpressionCompiler::LValue::retrieveValueIfLValueNotRequested(Expression const& _expression)
|
2014-11-10 16:31:09 +00:00
|
|
|
{
|
|
|
|
if (!_expression.lvalueRequested())
|
|
|
|
{
|
|
|
|
retrieveValue(_expression, true);
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 18:14:56 +00:00
|
|
|
void ExpressionCompiler::LValue::fromIdentifier(Identifier const& _identifier, Declaration const& _declaration)
|
2014-11-10 16:31:09 +00:00
|
|
|
{
|
2014-12-08 21:18:19 +00:00
|
|
|
m_stackSize = _identifier.getType()->getSizeOnStack();
|
2014-11-10 16:31:09 +00:00
|
|
|
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
|
2014-11-21 18:14:56 +00:00
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_identifier.getLocation())
|
2014-11-10 16:31:09 +00:00
|
|
|
<< errinfo_comment("Identifier type not supported or identifier not found."));
|
|
|
|
}
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
}
|