solidity/libsolidity/codegen/ExpressionCompiler.cpp

1841 lines
64 KiB
C++
Raw Normal View History

/*
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/>.
*/
/**
* @author Christian <c@ethdev.com>
* @date 2014
* Solidity AST to EVM bytecode compiler for expressions.
*/
#include <utility>
#include <numeric>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/algorithm/string/replace.hpp>
2014-11-10 16:31:09 +00:00
#include <libdevcore/Common.h>
#include <libdevcore/SHA3.h>
2015-10-20 22:21:52 +00:00
#include <libsolidity/ast/AST.h>
#include <libsolidity/codegen/ExpressionCompiler.h>
#include <libsolidity/codegen/CompilerContext.h>
#include <libsolidity/codegen/CompilerUtils.h>
#include <libsolidity/codegen/LValue.h>
#include <libevmasm/GasMeter.h>
using namespace std;
namespace dev
{
namespace solidity
{
2015-02-25 14:14:22 +00:00
void ExpressionCompiler::compile(Expression const& _expression)
{
2015-02-25 14:14:22 +00:00
_expression.accept(*this);
}
2015-02-25 14:14:22 +00:00
void ExpressionCompiler::appendStateVariableInitialization(VariableDeclaration const& _varDecl)
2014-11-04 18:13:03 +00:00
{
2015-08-31 16:44:29 +00:00
if (!_varDecl.value())
2015-02-25 14:14:22 +00:00
return;
TypePointer type = _varDecl.value()->annotation().type;
2015-07-15 17:13:42 +00:00
solAssert(!!type, "Type information not available.");
CompilerContext::LocationSetter locationSetter(m_context, _varDecl);
2015-08-31 16:44:29 +00:00
_varDecl.value()->accept(*this);
2014-11-04 18:13:03 +00:00
if (_varDecl.annotation().type->dataStoredIn(DataLocation::Storage))
2015-07-15 17:13:42 +00:00
{
// reference type, only convert value to mobile type and do final conversion in storeValue.
2016-10-21 10:30:58 +00:00
auto mt = type->mobileType();
solAssert(mt, "");
utils().convertType(*type, *mt);
type = mt;
2015-07-15 17:13:42 +00:00
}
else
{
utils().convertType(*type, *_varDecl.annotation().type);
type = _varDecl.annotation().type;
2015-07-15 17:13:42 +00:00
}
2015-08-31 16:44:29 +00:00
StorageItem(m_context, _varDecl).storeValue(*type, _varDecl.location(), true);
}
void ExpressionCompiler::appendConstStateVariableAccessor(VariableDeclaration const& _varDecl)
{
solAssert(_varDecl.isConstant(), "");
2015-08-31 16:44:29 +00:00
_varDecl.value()->accept(*this);
utils().convertType(*_varDecl.value()->annotation().type, *_varDecl.annotation().type);
// append return
m_context << dupInstruction(_varDecl.annotation().type->sizeOnStack() + 1);
m_context.appendJump(eth::AssemblyItem::JumpType::OutOfFunction);
}
2015-02-25 14:14:22 +00:00
void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const& _varDecl)
{
solAssert(!_varDecl.isConstant(), "");
CompilerContext::LocationSetter locationSetter(m_context, _varDecl);
2015-02-25 14:14:22 +00:00
FunctionType accessorType(_varDecl);
TypePointers paramTypes = accessorType.parameterTypes();
2017-06-23 15:20:07 +00:00
m_context.adjustStackOffset(1 + CompilerUtils::sizeOnStack(paramTypes));
2015-02-25 14:14:22 +00:00
// retrieve the position of the variable
2015-08-31 16:44:29 +00:00
auto const& location = m_context.storageLocationOfVariable(_varDecl);
2015-04-02 15:03:02 +00:00
m_context << location.first << u256(location.second);
2015-02-25 14:14:22 +00:00
TypePointer returnType = _varDecl.annotation().type;
2015-02-25 14:14:22 +00:00
for (size_t i = 0; i < paramTypes.size(); ++i)
2015-02-25 14:14:22 +00:00
{
if (auto mappingType = dynamic_cast<MappingType const*>(returnType.get()))
{
solAssert(CompilerUtils::freeMemoryPointer >= 0x40, "");
solUnimplementedAssert(
2015-08-03 16:09:39 +00:00
!paramTypes[i]->isDynamicallySized(),
"Accessors for mapping with dynamically-sized keys not yet implemented."
);
2015-04-02 15:03:02 +00:00
// pop offset
m_context << Instruction::POP;
// move storage offset to memory.
utils().storeInMemory(32);
2015-04-02 15:03:02 +00:00
// move key to memory.
utils().copyToStackTop(paramTypes.size() - i, 1);
utils().storeInMemory(0);
m_context << u256(64) << u256(0) << Instruction::KECCAK256;
2015-04-02 15:03:02 +00:00
// push offset
m_context << u256(0);
2015-08-31 16:44:29 +00:00
returnType = mappingType->valueType();
}
else if (auto arrayType = dynamic_cast<ArrayType const*>(returnType.get()))
{
2015-04-02 15:03:02 +00:00
// pop offset
m_context << Instruction::POP;
utils().copyToStackTop(paramTypes.size() - i + 1, 1);
ArrayUtils(m_context).accessIndex(*arrayType);
2015-08-31 16:44:29 +00:00
returnType = arrayType->baseType();
}
else
solAssert(false, "Index access is allowed only for \"mapping\" and \"array\" types.");
2015-03-31 09:07:10 +00:00
}
2015-04-02 15:03:02 +00:00
// remove index arguments.
if (paramTypes.size() == 1)
m_context << Instruction::SWAP2 << Instruction::POP << Instruction::SWAP1;
2015-04-02 15:03:02 +00:00
else if (paramTypes.size() >= 2)
{
m_context << swapInstruction(paramTypes.size());
m_context << Instruction::POP;
m_context << swapInstruction(paramTypes.size());
utils().popStackSlots(paramTypes.size() - 1);
2015-02-25 14:14:22 +00:00
}
unsigned retSizeOnStack = 0;
2015-08-31 16:44:29 +00:00
solAssert(accessorType.returnParameterTypes().size() >= 1, "");
auto const& returnTypes = accessorType.returnParameterTypes();
2015-02-25 14:14:22 +00:00
if (StructType const* structType = dynamic_cast<StructType const*>(returnType.get()))
{
2015-04-02 15:03:02 +00:00
// remove offset
m_context << Instruction::POP;
2015-08-31 16:44:29 +00:00
auto const& names = accessorType.returnParameterNames();
2015-02-25 14:14:22 +00:00
// struct
for (size_t i = 0; i < names.size(); ++i)
{
2015-08-31 16:44:29 +00:00
if (returnTypes[i]->category() == Type::Category::Mapping)
continue;
2015-06-17 10:01:39 +00:00
if (auto arrayType = dynamic_cast<ArrayType const*>(returnTypes[i].get()))
if (!arrayType->isByteArray())
continue;
2015-08-31 16:44:29 +00:00
pair<u256, unsigned> const& offsets = structType->storageOffsetsOfMember(names[i]);
m_context << Instruction::DUP1 << u256(offsets.first) << Instruction::ADD << u256(offsets.second);
2015-08-31 16:44:29 +00:00
TypePointer memberType = structType->memberType(names[i]);
2015-06-17 10:01:39 +00:00
StorageItem(m_context, *memberType).retrieveValue(SourceLocation(), true);
utils().convertType(*memberType, *returnTypes[i]);
2015-08-31 16:44:29 +00:00
utils().moveToStackTop(returnTypes[i]->sizeOnStack());
retSizeOnStack += returnTypes[i]->sizeOnStack();
2015-02-25 14:14:22 +00:00
}
2015-04-02 15:03:02 +00:00
// remove slot
m_context << Instruction::POP;
2015-02-25 14:14:22 +00:00
}
else
{
2015-06-17 10:01:39 +00:00
// simple value or array
solAssert(returnTypes.size() == 1, "");
2015-02-25 19:27:55 +00:00
StorageItem(m_context, *returnType).retrieveValue(SourceLocation(), true);
2015-06-17 10:01:39 +00:00
utils().convertType(*returnType, *returnTypes.front());
2015-08-31 16:44:29 +00:00
retSizeOnStack = returnTypes.front()->sizeOnStack();
2015-02-25 14:14:22 +00:00
}
2015-08-31 16:44:29 +00:00
solAssert(retSizeOnStack == utils().sizeOnStack(returnTypes), "");
if (retSizeOnStack > 15)
BOOST_THROW_EXCEPTION(
CompilerError() <<
errinfo_sourceLocation(_varDecl.location()) <<
errinfo_comment("Stack too deep.")
);
m_context << dupInstruction(retSizeOnStack + 1);
m_context.appendJump(eth::AssemblyItem::JumpType::OutOfFunction);
}
bool ExpressionCompiler::visit(Conditional const& _condition)
{
CompilerContext::LocationSetter locationSetter(m_context, _condition);
_condition.condition().accept(*this);
eth::AssemblyItem trueTag = m_context.appendConditionalJump();
_condition.falseExpression().accept(*this);
utils().convertType(*_condition.falseExpression().annotation().type, *_condition.annotation().type);
eth::AssemblyItem endTag = m_context.appendJumpToNew();
m_context << trueTag;
2016-03-18 08:22:15 +00:00
int offset = _condition.annotation().type->sizeOnStack();
m_context.adjustStackOffset(-offset);
_condition.trueExpression().accept(*this);
utils().convertType(*_condition.trueExpression().annotation().type, *_condition.annotation().type);
m_context << endTag;
return false;
}
2014-12-06 00:06:24 +00:00
bool ExpressionCompiler::visit(Assignment const& _assignment)
{
CompilerContext::LocationSetter locationSetter(m_context, _assignment);
Token::Value op = _assignment.assignmentOperator();
Token::Value binOp = op == Token::Assign ? op : Token::AssignmentToBinaryOp(op);
Type const& leftType = *_assignment.leftHandSide().annotation().type;
if (leftType.category() == Type::Category::Tuple)
{
solAssert(*_assignment.annotation().type == TupleType(), "");
solAssert(op == Token::Assign, "");
}
else
solAssert(*_assignment.annotation().type == leftType, "");
bool cleanupNeeded = false;
if (op != Token::Assign)
cleanupNeeded = cleanupNeededForOp(leftType.category(), binOp);
2015-08-31 16:44:29 +00:00
_assignment.rightHandSide().accept(*this);
2015-10-14 13:19:50 +00:00
// Perform some conversion already. This will convert storage types to memory and literals
// to their actual type, but will not convert e.g. memory to storage.
TypePointer rightIntermediateType;
if (op != Token::Assign && Token::isShiftOp(binOp))
rightIntermediateType = _assignment.rightHandSide().annotation().type->mobileType();
else
rightIntermediateType = _assignment.rightHandSide().annotation().type->closestTemporaryType(
_assignment.leftHandSide().annotation().type
);
solAssert(rightIntermediateType, "");
utils().convertType(*_assignment.rightHandSide().annotation().type, *rightIntermediateType, cleanupNeeded);
2015-08-31 16:44:29 +00:00
_assignment.leftHandSide().accept(*this);
2015-02-25 14:14:22 +00:00
solAssert(!!m_currentLValue, "LValue not retrieved.");
2016-12-13 15:50:50 +00:00
if (op == Token::Assign)
m_currentLValue->storeValue(*rightIntermediateType, _assignment.location());
else // compound assignment
2014-11-10 16:31:09 +00:00
{
solAssert(leftType.isValueType(), "Compound operators only available for value types.");
2015-03-03 16:55:28 +00:00
unsigned lvalueSize = m_currentLValue->sizeOnStack();
unsigned itemSize = _assignment.annotation().type->sizeOnStack();
2015-03-03 16:55:28 +00:00
if (lvalueSize > 0)
{
utils().copyToStackTop(lvalueSize + itemSize, itemSize);
utils().copyToStackTop(itemSize + lvalueSize, lvalueSize);
2015-03-03 16:55:28 +00:00
// value lvalue_ref value lvalue_ref
}
2015-08-31 16:44:29 +00:00
m_currentLValue->retrieveValue(_assignment.location(), true);
utils().convertType(leftType, leftType, cleanupNeeded);
2016-12-13 15:50:50 +00:00
if (Token::isShiftOp(binOp))
appendShiftOperatorCode(binOp, leftType, *rightIntermediateType);
2016-04-29 23:15:22 +00:00
else
{
solAssert(leftType == *rightIntermediateType, "");
2016-12-13 15:50:50 +00:00
appendOrdinaryBinaryOperatorCode(binOp, leftType);
}
2015-03-03 16:55:28 +00:00
if (lvalueSize > 0)
2015-03-13 12:14:51 +00:00
{
if (itemSize + lvalueSize > 16)
BOOST_THROW_EXCEPTION(
CompilerError() <<
errinfo_sourceLocation(_assignment.location()) <<
errinfo_comment("Stack too deep, try removing local variables.")
);
2015-03-03 16:55:28 +00:00
// value [lvalue_ref] updated_value
for (unsigned i = 0; i < itemSize; ++i)
m_context << swapInstruction(itemSize + lvalueSize) << Instruction::POP;
2015-03-13 12:14:51 +00:00
}
2016-12-13 15:50:50 +00:00
m_currentLValue->storeValue(*_assignment.annotation().type, _assignment.location());
2014-11-10 16:31:09 +00:00
}
2014-11-13 00:12:57 +00:00
m_currentLValue.reset();
return false;
}
2015-10-14 13:19:50 +00:00
bool ExpressionCompiler::visit(TupleExpression const& _tuple)
{
2016-01-10 07:12:17 +00:00
if (_tuple.isInlineArray())
2016-01-11 03:49:36 +00:00
{
ArrayType const& arrayType = dynamic_cast<ArrayType const&>(*_tuple.annotation().type);
solAssert(!arrayType.isDynamicallySized(), "Cannot create dynamically sized inline array.");
2016-01-11 03:49:36 +00:00
m_context << max(u256(32u), arrayType.memorySize());
utils().allocateMemory();
m_context << Instruction::DUP1;
2016-01-11 03:49:36 +00:00
for (auto const& component: _tuple.components())
2016-01-11 03:49:36 +00:00
{
component->accept(*this);
utils().convertType(*component->annotation().type, *arrayType.baseType(), true);
utils().storeInMemoryDynamic(*arrayType.baseType(), true);
2016-01-11 03:49:36 +00:00
}
m_context << Instruction::POP;
2016-01-11 03:49:36 +00:00
}
2016-01-10 07:12:17 +00:00
else
{
vector<unique_ptr<LValue>> lvalues;
for (auto const& component: _tuple.components())
if (component)
2015-10-14 13:19:50 +00:00
{
2016-01-10 07:12:17 +00:00
component->accept(*this);
if (_tuple.annotation().lValueRequested)
{
solAssert(!!m_currentLValue, "");
lvalues.push_back(move(m_currentLValue));
}
2015-10-14 13:19:50 +00:00
}
2016-01-10 07:12:17 +00:00
else if (_tuple.annotation().lValueRequested)
lvalues.push_back(unique_ptr<LValue>());
if (_tuple.annotation().lValueRequested)
{
if (_tuple.components().size() == 1)
m_currentLValue = move(lvalues[0]);
else
m_currentLValue.reset(new TupleObject(m_context, move(lvalues)));
2015-10-14 13:19:50 +00:00
}
}
2015-10-14 13:19:50 +00:00
return false;
}
2014-12-19 10:31:17 +00:00
bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation)
{
CompilerContext::LocationSetter locationSetter(m_context, _unaryOperation);
if (_unaryOperation.annotation().type->category() == Type::Category::RationalNumber)
2014-12-19 10:31:17 +00:00
{
m_context << _unaryOperation.annotation().type->literalValue(nullptr);
2014-12-19 10:31:17 +00:00
return false;
}
2015-08-31 16:44:29 +00:00
_unaryOperation.subExpression().accept(*this);
2014-12-19 10:31:17 +00:00
switch (_unaryOperation.getOperator())
{
case Token::Not: // !
m_context << Instruction::ISZERO;
break;
case Token::BitNot: // ~
m_context << Instruction::NOT;
break;
case Token::Delete: // delete
2015-02-25 14:14:22 +00:00
solAssert(!!m_currentLValue, "LValue not retrieved.");
2015-08-31 16:44:29 +00:00
m_currentLValue->setToZero(_unaryOperation.location());
2014-11-13 00:12:57 +00:00
m_currentLValue.reset();
break;
case Token::Inc: // ++ (pre- or postfix)
case Token::Dec: // -- (pre- or postfix)
2015-02-25 14:14:22 +00:00
solAssert(!!m_currentLValue, "LValue not retrieved.");
2015-08-31 16:44:29 +00:00
m_currentLValue->retrieveValue(_unaryOperation.location());
if (!_unaryOperation.isPrefixOperation())
2014-11-10 16:31:09 +00:00
{
// store value for later
solUnimplementedAssert(_unaryOperation.annotation().type->sizeOnStack() == 1, "Stack size != 1 not implemented.");
m_context << Instruction::DUP1;
if (m_currentLValue->sizeOnStack() > 0)
for (unsigned i = 1 + m_currentLValue->sizeOnStack(); i > 0; --i)
m_context << swapInstruction(i);
2014-11-10 16:31:09 +00:00
}
m_context << u256(1);
if (_unaryOperation.getOperator() == Token::Inc)
m_context << Instruction::ADD;
else
m_context << Instruction::SWAP1 << Instruction::SUB;
// Stack for prefix: [ref...] (*ref)+-1
// Stack for postfix: *ref [ref...] (*ref)+-1
for (unsigned i = m_currentLValue->sizeOnStack(); i > 0; --i)
m_context << swapInstruction(i);
2015-02-25 14:14:22 +00:00
m_currentLValue->storeValue(
*_unaryOperation.annotation().type, _unaryOperation.location(),
2015-02-25 14:14:22 +00:00
!_unaryOperation.isPrefixOperation());
2014-11-13 00:12:57 +00:00
m_currentLValue.reset();
break;
case Token::Add: // +
// unary add, so basically no-op
break;
case Token::Sub: // -
m_context << u256(0) << Instruction::SUB;
break;
default:
solAssert(false, "Invalid unary operator: " + string(Token::toString(_unaryOperation.getOperator())));
}
2014-12-19 10:31:17 +00:00
return false;
}
2014-12-06 00:06:24 +00:00
bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
{
CompilerContext::LocationSetter locationSetter(m_context, _binaryOperation);
2015-08-31 16:44:29 +00:00
Expression const& leftExpression = _binaryOperation.leftExpression();
Expression const& rightExpression = _binaryOperation.rightExpression();
solAssert(!!_binaryOperation.annotation().commonType, "");
TypePointer const& commonType = _binaryOperation.annotation().commonType;
Token::Value const c_op = _binaryOperation.getOperator();
if (c_op == Token::And || c_op == Token::Or) // special case: short-circuiting
appendAndOrOperatorCode(_binaryOperation);
else if (commonType->category() == Type::Category::RationalNumber)
m_context << commonType->literalValue(nullptr);
else
{
bool cleanupNeeded = cleanupNeededForOp(commonType->category(), c_op);
TypePointer leftTargetType = commonType;
TypePointer rightTargetType = Token::isShiftOp(c_op) ? rightExpression.annotation().type->mobileType() : commonType;
solAssert(rightTargetType, "");
2014-11-04 18:13:03 +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.annotation().type->category() == Type::Category::RationalNumber;
2014-12-19 10:31:17 +00:00
};
bool swap = m_optimize && Token::isCommutativeOp(c_op) && isLiteral(rightExpression) && !isLiteral(leftExpression);
if (swap)
{
leftExpression.accept(*this);
utils().convertType(*leftExpression.annotation().type, *leftTargetType, cleanupNeeded);
rightExpression.accept(*this);
utils().convertType(*rightExpression.annotation().type, *rightTargetType, cleanupNeeded);
}
else
{
rightExpression.accept(*this);
utils().convertType(*rightExpression.annotation().type, *rightTargetType, cleanupNeeded);
leftExpression.accept(*this);
utils().convertType(*leftExpression.annotation().type, *leftTargetType, cleanupNeeded);
}
2016-04-29 23:15:22 +00:00
if (Token::isShiftOp(c_op))
// shift only cares about the signedness of both sides
appendShiftOperatorCode(c_op, *leftTargetType, *rightTargetType);
2016-04-29 23:15:22 +00:00
else if (Token::isCompareOp(c_op))
appendCompareOperatorCode(c_op, *commonType);
else
appendOrdinaryBinaryOperatorCode(c_op, *commonType);
}
// 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)
{
CompilerContext::LocationSetter locationSetter(m_context, _functionCall);
2017-05-19 13:45:01 +00:00
if (_functionCall.annotation().kind == FunctionCallKind::TypeConversion)
{
2015-08-31 16:44:29 +00:00
solAssert(_functionCall.arguments().size() == 1, "");
solAssert(_functionCall.names().empty(), "");
Expression const& firstArgument = *_functionCall.arguments().front();
firstArgument.accept(*this);
utils().convertType(*firstArgument.annotation().type, *_functionCall.annotation().type);
2015-06-30 19:08:34 +00:00
return false;
}
2015-06-30 19:08:34 +00:00
FunctionTypePointer functionType;
2017-05-19 13:45:01 +00:00
if (_functionCall.annotation().kind == FunctionCallKind::StructConstructorCall)
2015-06-30 19:08:34 +00:00
{
auto const& type = dynamic_cast<TypeType const&>(*_functionCall.expression().annotation().type);
2015-08-31 16:44:29 +00:00
auto const& structType = dynamic_cast<StructType const&>(*type.actualType());
2015-06-30 19:08:34 +00:00
functionType = structType.constructorType();
}
else
functionType = dynamic_pointer_cast<FunctionType const>(_functionCall.expression().annotation().type);
2015-06-30 19:08:34 +00:00
TypePointers parameterTypes = functionType->parameterTypes();
2015-08-31 16:44:29 +00:00
vector<ASTPointer<Expression const>> const& callArguments = _functionCall.arguments();
vector<ASTPointer<ASTString>> const& callArgumentNames = _functionCall.names();
2015-06-30 19:08:34 +00:00
if (!functionType->takesArbitraryParameters())
solAssert(callArguments.size() == parameterTypes.size(), "");
vector<ASTPointer<Expression const>> arguments;
if (callArgumentNames.empty())
// normal arguments
arguments = callArguments;
else
2015-06-30 19:08:34 +00:00
// named arguments
2015-08-31 16:44:29 +00:00
for (auto const& parameterName: functionType->parameterNames())
2015-06-30 19:08:34 +00:00
{
bool found = false;
for (size_t j = 0; j < callArgumentNames.size() && !found; j++)
if ((found = (parameterName == *callArgumentNames[j])))
// we found the actual parameter position
arguments.push_back(callArguments[j]);
solAssert(found, "");
}
2017-05-19 13:45:01 +00:00
if (_functionCall.annotation().kind == FunctionCallKind::StructConstructorCall)
{
TypeType const& type = dynamic_cast<TypeType const&>(*_functionCall.expression().annotation().type);
2015-08-31 16:44:29 +00:00
auto const& structType = dynamic_cast<StructType const&>(*type.actualType());
2014-11-25 17:23:39 +00:00
m_context << max(u256(32u), structType.memorySize());
2015-06-30 19:08:34 +00:00
utils().allocateMemory();
m_context << Instruction::DUP1;
2015-06-30 19:08:34 +00:00
for (unsigned i = 0; i < arguments.size(); ++i)
{
arguments[i]->accept(*this);
utils().convertType(*arguments[i]->annotation().type, *functionType->parameterTypes()[i]);
2015-08-31 16:44:29 +00:00
utils().storeInMemoryDynamic(*functionType->parameterTypes()[i]);
2015-06-30 19:08:34 +00:00
}
m_context << Instruction::POP;
2015-06-30 19:08:34 +00:00
}
else
{
FunctionType const& function = *functionType;
if (function.bound())
// Only delegatecall and internal functions can be bound, this might be lifted later.
solAssert(function.kind() == FunctionType::Kind::DelegateCall || function.kind() == FunctionType::Kind::Internal, "");
switch (function.kind())
2014-12-04 18:38:24 +00:00
{
case FunctionType::Kind::Internal:
{
2014-11-25 17:23:39 +00:00
// Calling convention: Caller pushes return address and arguments
// Callee removes them and pushes return values
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);
utils().convertType(*arguments[i]->annotation().type, *function.parameterTypes()[i]);
2014-11-25 17:23:39 +00:00
}
2015-08-31 16:44:29 +00:00
_functionCall.expression().accept(*this);
unsigned parameterSize = CompilerUtils::sizeOnStack(function.parameterTypes());
if (function.bound())
{
// stack: arg2, ..., argn, label, arg1
unsigned depth = parameterSize + 1;
utils().moveIntoStack(depth, function.selfType()->sizeOnStack());
parameterSize += function.selfType()->sizeOnStack();
}
if (m_context.runtimeContext())
// We have a runtime context, so we need the creation part.
2016-11-30 10:03:26 +00:00
utils().rightShiftNumberOnStack(32, false);
else
// Extract the runtime part.
m_context << ((u256(1) << 32) - 1) << Instruction::AND;
m_context.appendJump(eth::AssemblyItem::JumpType::IntoFunction);
2014-11-25 17:23:39 +00:00
m_context << returnLabel;
2015-08-31 16:44:29 +00:00
unsigned returnParametersSize = CompilerUtils::sizeOnStack(function.returnParameterTypes());
2014-11-25 17:23:39 +00:00
// callee adds return parameters, but removes arguments and return label
m_context.adjustStackOffset(returnParametersSize - parameterSize - 1);
2014-12-04 18:38:24 +00:00
break;
2014-11-25 17:23:39 +00:00
}
case FunctionType::Kind::External:
case FunctionType::Kind::CallCode:
case FunctionType::Kind::DelegateCall:
2017-08-01 09:32:23 +00:00
case FunctionType::Kind::BareCall:
case FunctionType::Kind::BareCallCode:
case FunctionType::Kind::BareDelegateCall:
2015-08-31 16:44:29 +00:00
_functionCall.expression().accept(*this);
appendExternalFunctionCall(function, arguments);
break;
case FunctionType::Kind::Creation:
2014-11-25 17:23:39 +00:00
{
2015-08-31 16:44:29 +00:00
_functionCall.expression().accept(*this);
2015-01-13 17:12:19 +00:00
solAssert(!function.gasSet(), "Gas limit set for contract creation.");
2015-08-31 16:44:29 +00:00
solAssert(function.returnParameterTypes().size() == 1, "");
TypePointers argumentTypes;
for (auto const& arg: arguments)
{
arg->accept(*this);
argumentTypes.push_back(arg->annotation().type);
}
2017-01-20 19:16:18 +00:00
ContractDefinition const* contract =
&dynamic_cast<ContractType const&>(*function.returnParameterTypes().front()).contractDefinition();
m_context.callLowLevelFunction(
"$copyContractCreationCodeToMemory_" + contract->type()->identifier(),
0,
1,
[contract](CompilerContext& _context)
{
// copy the contract's code into memory
eth::Assembly const& assembly = _context.compiledContract(*contract);
CompilerUtils(_context).fetchFreeMemoryPointer();
// pushes size
auto subroutine = _context.addSubroutine(make_shared<eth::Assembly>(assembly));
_context << Instruction::DUP1 << subroutine;
_context << Instruction::DUP4 << Instruction::CODECOPY;
_context << Instruction::ADD;
}
);
utils().abiEncode(argumentTypes, function.parameterTypes());
// now on stack: memory_end_ptr
// need: size, offset, endowment
utils().toSizeAfterFreeMemoryPointer();
2015-01-13 17:12:19 +00:00
if (function.valueSet())
m_context << dupInstruction(3);
2015-01-13 17:12:19 +00:00
else
m_context << u256(0);
m_context << Instruction::CREATE;
2016-07-11 13:04:33 +00:00
// Check if zero (out of stack or not enough balance).
m_context << Instruction::DUP1 << Instruction::ISZERO;
m_context.appendConditionalRevert();
2015-01-13 17:12:19 +00:00
if (function.valueSet())
m_context << swapInstruction(1) << Instruction::POP;
2014-12-04 18:38:24 +00:00
break;
}
case FunctionType::Kind::SetGas:
2014-12-10 22:01:40 +00:00
{
// stack layout: contract_address function_id [gas] [value]
2015-08-31 16:44:29 +00:00
_functionCall.expression().accept(*this);
arguments.front()->accept(*this);
utils().convertType(*arguments.front()->annotation().type, IntegerType(256), true);
// Note that function is not the original function, but the ".gas" function.
// Its values of gasSet and valueSet is equal to the original function's though.
unsigned stackDepth = (function.gasSet() ? 1 : 0) + (function.valueSet() ? 1 : 0);
if (stackDepth > 0)
m_context << swapInstruction(stackDepth);
if (function.gasSet())
m_context << Instruction::POP;
2014-12-04 18:38:24 +00:00
break;
2014-12-10 22:01:40 +00:00
}
case FunctionType::Kind::SetValue:
// stack layout: contract_address function_id [gas] [value]
2015-08-31 16:44:29 +00:00
_functionCall.expression().accept(*this);
// Note that function is not the original function, but the ".value" function.
// Its values of gasSet and valueSet is equal to the original function's though.
if (function.valueSet())
m_context << Instruction::POP;
arguments.front()->accept(*this);
break;
case FunctionType::Kind::Send:
case FunctionType::Kind::Transfer:
2015-08-31 16:44:29 +00:00
_functionCall.expression().accept(*this);
// Provide the gas stipend manually at first because we may send zero ether.
// Will be zeroed if we send more than zero ether.
2017-02-24 00:27:36 +00:00
m_context << u256(eth::GasCosts::callStipend);
arguments.front()->accept(*this);
2015-06-16 09:28:35 +00:00
utils().convertType(
*arguments.front()->annotation().type,
2015-08-31 16:44:29 +00:00
*function.parameterTypes().front(), true
2015-06-16 09:28:35 +00:00
);
2017-02-24 00:27:36 +00:00
// gas <- gas * !value
m_context << Instruction::SWAP1 << Instruction::DUP2;
m_context << Instruction::ISZERO << Instruction::MUL << Instruction::SWAP1;
appendExternalFunctionCall(
FunctionType(
TypePointers{},
TypePointers{},
strings(),
strings(),
2017-08-01 09:32:23 +00:00
FunctionType::Kind::BareCall,
false,
StateMutability::NonPayable,
nullptr,
true,
true
),
{}
);
if (function.kind() == FunctionType::Kind::Transfer)
2017-02-05 20:21:14 +00:00
{
// Check if zero (out of stack or not enough balance).
m_context << Instruction::ISZERO;
m_context.appendConditionalRevert();
2017-02-05 20:21:14 +00:00
}
2014-12-04 18:38:24 +00:00
break;
case FunctionType::Kind::Selfdestruct:
2014-12-04 18:38:24 +00:00
arguments.front()->accept(*this);
utils().convertType(*arguments.front()->annotation().type, *function.parameterTypes().front(), true);
m_context << Instruction::SELFDESTRUCT;
2014-12-04 18:38:24 +00:00
break;
case FunctionType::Kind::Revert:
m_context.appendRevert();
2017-02-06 22:11:19 +00:00
break;
case FunctionType::Kind::SHA3:
{
TypePointers argumentTypes;
for (auto const& arg: arguments)
{
arg->accept(*this);
argumentTypes.push_back(arg->annotation().type);
}
utils().fetchFreeMemoryPointer();
solAssert(!function.padArguments(), "");
utils().packedEncode(argumentTypes, TypePointers());
utils().toSizeAfterFreeMemoryPointer();
m_context << Instruction::KECCAK256;
2014-12-04 18:38:24 +00:00
break;
}
case FunctionType::Kind::Log0:
case FunctionType::Kind::Log1:
case FunctionType::Kind::Log2:
case FunctionType::Kind::Log3:
case FunctionType::Kind::Log4:
2015-01-09 14:00:47 +00:00
{
unsigned logNumber = int(function.kind()) - int(FunctionType::Kind::Log0);
2015-01-29 15:42:59 +00:00
for (unsigned arg = logNumber; arg > 0; --arg)
2015-01-09 14:00:47 +00:00
{
arguments[arg]->accept(*this);
utils().convertType(*arguments[arg]->annotation().type, *function.parameterTypes()[arg], true);
2015-01-09 14:00:47 +00:00
}
arguments.front()->accept(*this);
utils().fetchFreeMemoryPointer();
utils().packedEncode(
{arguments.front()->annotation().type},
{function.parameterTypes().front()}
);
utils().toSizeAfterFreeMemoryPointer();
m_context << logInstruction(logNumber);
2015-01-29 15:42:59 +00:00
break;
}
case FunctionType::Kind::Event:
2015-01-29 15:42:59 +00:00
{
2015-08-31 16:44:29 +00:00
_functionCall.expression().accept(*this);
auto const& event = dynamic_cast<EventDefinition const&>(function.declaration());
2015-01-29 15:42:59 +00:00
unsigned numIndexed = 0;
// All indexed arguments go to the stack
for (unsigned arg = arguments.size(); arg > 0; --arg)
2015-08-31 16:44:29 +00:00
if (event.parameters()[arg - 1]->isIndexed())
2015-01-29 15:42:59 +00:00
{
++numIndexed;
arguments[arg - 1]->accept(*this);
if (auto const& arrayType = dynamic_pointer_cast<ArrayType const>(function.parameterTypes()[arg - 1]))
{
utils().fetchFreeMemoryPointer();
utils().packedEncode(
{arguments[arg - 1]->annotation().type},
{arrayType}
);
utils().toSizeAfterFreeMemoryPointer();
m_context << Instruction::KECCAK256;
}
else
utils().convertType(
*arguments[arg - 1]->annotation().type,
*function.parameterTypes()[arg - 1],
true
);
2015-01-29 15:42:59 +00:00
}
2015-03-17 10:34:56 +00:00
if (!event.isAnonymous())
{
2016-10-05 10:30:28 +00:00
m_context << u256(h256::Arith(dev::keccak256(function.externalSignature())));
++numIndexed;
}
2015-01-29 15:42:59 +00:00
solAssert(numIndexed <= 4, "Too many indexed arguments.");
2015-02-10 16:53:43 +00:00
// Copy all non-indexed arguments to memory (data)
// Memory position is only a hack and should be removed once we have free memory pointer.
TypePointers nonIndexedArgTypes;
TypePointers nonIndexedParamTypes;
2015-02-10 16:53:43 +00:00
for (unsigned arg = 0; arg < arguments.size(); ++arg)
2015-08-31 16:44:29 +00:00
if (!event.parameters()[arg]->isIndexed())
2015-04-21 08:59:48 +00:00
{
arguments[arg]->accept(*this);
nonIndexedArgTypes.push_back(arguments[arg]->annotation().type);
2015-08-31 16:44:29 +00:00
nonIndexedParamTypes.push_back(function.parameterTypes()[arg]);
2015-04-21 08:59:48 +00:00
}
utils().fetchFreeMemoryPointer();
utils().abiEncode(nonIndexedArgTypes, nonIndexedParamTypes);
// need: topic1 ... topicn memsize memstart
utils().toSizeAfterFreeMemoryPointer();
m_context << logInstruction(numIndexed);
break;
2015-01-09 14:00:47 +00:00
}
case FunctionType::Kind::BlockHash:
{
arguments[0]->accept(*this);
utils().convertType(*arguments[0]->annotation().type, *function.parameterTypes()[0], true);
m_context << Instruction::BLOCKHASH;
break;
}
case FunctionType::Kind::AddMod:
case FunctionType::Kind::MulMod:
2015-11-18 16:12:39 +00:00
{
for (unsigned i = 0; i < 3; i ++)
{
arguments[2 - i]->accept(*this);
utils().convertType(*arguments[2 - i]->annotation().type, IntegerType(256));
}
if (function.kind() == FunctionType::Kind::AddMod)
m_context << Instruction::ADDMOD;
2015-11-18 16:12:39 +00:00
else
m_context << Instruction::MULMOD;
2015-11-18 16:12:39 +00:00
break;
}
case FunctionType::Kind::ECRecover:
case FunctionType::Kind::SHA256:
case FunctionType::Kind::RIPEMD160:
2014-12-04 18:38:24 +00:00
{
2015-08-31 16:44:29 +00:00
_functionCall.expression().accept(*this);
static const map<FunctionType::Kind, u256> contractAddresses{{FunctionType::Kind::ECRecover, 1},
{FunctionType::Kind::SHA256, 2},
{FunctionType::Kind::RIPEMD160, 3}};
m_context << contractAddresses.find(function.kind())->second;
2015-08-31 16:44:29 +00:00
for (unsigned i = function.sizeOnStack(); i > 0; --i)
m_context << swapInstruction(i);
appendExternalFunctionCall(function, arguments);
2014-12-04 18:38:24 +00:00
break;
}
case FunctionType::Kind::ByteArrayPush:
case FunctionType::Kind::ArrayPush:
{
_functionCall.expression().accept(*this);
solAssert(function.parameterTypes().size() == 1, "");
solAssert(!!function.parameterTypes()[0], "");
2015-11-25 13:23:35 +00:00
TypePointer paramType = function.parameterTypes()[0];
shared_ptr<ArrayType> arrayType =
function.kind() == FunctionType::Kind::ArrayPush ?
make_shared<ArrayType>(DataLocation::Storage, paramType) :
make_shared<ArrayType>(DataLocation::Storage);
// get the current length
ArrayUtils(m_context).retrieveLength(*arrayType);
m_context << Instruction::DUP1;
// stack: ArrayReference currentLength currentLength
m_context << u256(1) << Instruction::ADD;
// stack: ArrayReference currentLength newLength
m_context << Instruction::DUP3 << Instruction::DUP2;
ArrayUtils(m_context).resizeDynamicArray(*arrayType);
m_context << Instruction::SWAP2 << Instruction::SWAP1;
// stack: newLength ArrayReference oldLength
ArrayUtils(m_context).accessIndex(*arrayType, false);
// stack: newLength storageSlot slotOffset
arguments[0]->accept(*this);
// stack: newLength storageSlot slotOffset argValue
TypePointer type = arguments[0]->annotation().type->closestTemporaryType(arrayType->baseType());
solAssert(type, "");
utils().convertType(*arguments[0]->annotation().type, *type);
utils().moveToStackTop(1 + type->sizeOnStack());
utils().moveToStackTop(1 + type->sizeOnStack());
// stack: newLength argValue storageSlot slotOffset
if (function.kind() == FunctionType::Kind::ArrayPush)
StorageItem(m_context, *paramType).storeValue(*type, _functionCall.location(), true);
else
StorageByteArrayElement(m_context).storeValue(*type, _functionCall.location(), true);
break;
}
case FunctionType::Kind::ObjectCreation:
2015-11-17 14:15:00 +00:00
{
// Will allocate at the end of memory (MSIZE) and not write at all unless the base
// type is dynamically sized.
ArrayType const& arrayType = dynamic_cast<ArrayType const&>(*_functionCall.annotation().type);
_functionCall.expression().accept(*this);
solAssert(arguments.size() == 1, "");
// Fetch requested length.
arguments[0]->accept(*this);
utils().convertType(*arguments[0]->annotation().type, IntegerType(256));
// Stack: requested_length
// Allocate at max(MSIZE, freeMemoryPointer)
utils().fetchFreeMemoryPointer();
m_context << Instruction::DUP1 << Instruction::MSIZE;
m_context << Instruction::LT;
2015-11-17 14:15:00 +00:00
auto initialise = m_context.appendConditionalJump();
// Free memory pointer does not point to empty memory, use MSIZE.
m_context << Instruction::POP;
m_context << Instruction::MSIZE;
2015-11-17 14:15:00 +00:00
m_context << initialise;
// Stack: requested_length memptr
m_context << Instruction::SWAP1;
2015-11-17 14:15:00 +00:00
// Stack: memptr requested_length
// store length
m_context << Instruction::DUP1 << Instruction::DUP3 << Instruction::MSTORE;
2015-11-17 14:15:00 +00:00
// Stack: memptr requested_length
// update free memory pointer
m_context << Instruction::DUP1 << arrayType.baseType()->memoryHeadSize();
m_context << Instruction::MUL << u256(32) << Instruction::ADD;
m_context << Instruction::DUP3 << Instruction::ADD;
2015-11-17 14:15:00 +00:00
utils().storeFreeMemoryPointer();
// Stack: memptr requested_length
2016-07-28 15:19:17 +00:00
// Check if length is zero
m_context << Instruction::DUP1 << Instruction::ISZERO;
auto skipInit = m_context.appendConditionalJump();
2015-11-17 14:15:00 +00:00
// We only have to initialise if the base type is a not a value type.
if (dynamic_cast<ReferenceType const*>(arrayType.baseType().get()))
{
m_context << Instruction::DUP2 << u256(32) << Instruction::ADD;
2015-11-17 14:15:00 +00:00
utils().zeroInitialiseMemoryArray(arrayType);
}
2016-07-28 15:19:17 +00:00
m_context << skipInit;
m_context << Instruction::POP;
2015-11-17 14:15:00 +00:00
break;
}
case FunctionType::Kind::Assert:
case FunctionType::Kind::Require:
2017-02-09 01:37:53 +00:00
{
arguments.front()->accept(*this);
2017-02-10 13:32:36 +00:00
utils().convertType(*arguments.front()->annotation().type, *function.parameterTypes().front(), false);
2017-02-10 22:53:32 +00:00
// jump if condition was met
m_context << Instruction::ISZERO << Instruction::ISZERO;
auto success = m_context.appendConditionalJump();
if (function.kind() == FunctionType::Kind::Assert)
2017-03-09 16:03:53 +00:00
// condition was not met, flag an error
2017-06-16 13:52:56 +00:00
m_context.appendInvalid();
2017-03-09 16:03:53 +00:00
else
m_context.appendRevert();
2017-02-10 22:53:32 +00:00
// the success branch
m_context << success;
2017-02-09 01:37:53 +00:00
break;
}
2014-12-04 18:38:24 +00:00
default:
solAssert(false, "Invalid function type.");
2014-11-25 17:23:39 +00:00
}
}
return false;
}
2015-01-15 12:33:58 +00:00
bool ExpressionCompiler::visit(NewExpression const&)
2014-12-12 15:49:26 +00:00
{
2015-01-13 17:12:19 +00:00
// code is created for the function call (CREATION) only
2014-12-12 15:49:26 +00:00
return false;
}
bool ExpressionCompiler::visit(MemberAccess const& _memberAccess)
{
CompilerContext::LocationSetter locationSetter(m_context, _memberAccess);
// Check whether the member is a bound function.
2015-08-31 16:44:29 +00:00
ASTString const& member = _memberAccess.memberName();
if (auto funType = dynamic_cast<FunctionType const*>(_memberAccess.annotation().type.get()))
if (funType->bound())
{
_memberAccess.expression().accept(*this);
utils().convertType(
*_memberAccess.expression().annotation().type,
*funType->selfType(),
true
);
if (funType->kind() == FunctionType::Kind::Internal)
{
FunctionDefinition const& funDef = dynamic_cast<decltype(funDef)>(funType->declaration());
utils().pushCombinedFunctionEntryLabel(funDef);
utils().moveIntoStack(funType->selfType()->sizeOnStack(), 1);
}
else
{
solAssert(funType->kind() == FunctionType::Kind::DelegateCall, "");
auto contract = dynamic_cast<ContractDefinition const*>(funType->declaration().scope());
solAssert(contract && contract->isLibrary(), "");
m_context.appendLibraryAddress(contract->fullyQualifiedName());
m_context << funType->externalIdentifier();
utils().moveIntoStack(funType->selfType()->sizeOnStack(), 2);
}
return false;
}
// Special processing for TypeType because we do not want to visit the library itself
// for internal functions, or enum/struct definitions.
if (TypeType const* type = dynamic_cast<TypeType const*>(_memberAccess.expression().annotation().type.get()))
{
if (dynamic_cast<ContractType const*>(type->actualType().get()))
{
solAssert(_memberAccess.annotation().type, "_memberAccess has no type");
if (auto funType = dynamic_cast<FunctionType const*>(_memberAccess.annotation().type.get()))
{
switch (funType->kind())
{
case FunctionType::Kind::Internal:
// We do not visit the expression here on purpose, because in the case of an
// internal library function call, this would push the library address forcing
// us to link against it although we actually do not need it.
if (auto const* function = dynamic_cast<FunctionDefinition const*>(_memberAccess.annotation().referencedDeclaration))
utils().pushCombinedFunctionEntryLabel(*function);
else
solAssert(false, "Function not found in member access");
break;
case FunctionType::Kind::Event:
if (!dynamic_cast<EventDefinition const*>(_memberAccess.annotation().referencedDeclaration))
solAssert(false, "event not found");
// no-op, because the parent node will do the job
break;
case FunctionType::Kind::External:
case FunctionType::Kind::Creation:
case FunctionType::Kind::DelegateCall:
case FunctionType::Kind::CallCode:
case FunctionType::Kind::Send:
2017-08-01 09:32:23 +00:00
case FunctionType::Kind::BareCall:
case FunctionType::Kind::BareCallCode:
case FunctionType::Kind::BareDelegateCall:
case FunctionType::Kind::Transfer:
_memberAccess.expression().accept(*this);
m_context << funType->externalIdentifier();
break;
case FunctionType::Kind::Log0:
case FunctionType::Kind::Log1:
case FunctionType::Kind::Log2:
case FunctionType::Kind::Log3:
case FunctionType::Kind::Log4:
case FunctionType::Kind::ECRecover:
case FunctionType::Kind::SHA256:
case FunctionType::Kind::RIPEMD160:
default:
solAssert(false, "unsupported member function");
}
}
else if (dynamic_cast<TypeType const*>(_memberAccess.annotation().type.get()))
{
// no-op
}
else if (auto variable = dynamic_cast<VariableDeclaration const*>(_memberAccess.annotation().referencedDeclaration))
2016-10-21 18:16:21 +00:00
appendVariable(*variable, static_cast<Expression const&>(_memberAccess));
else
_memberAccess.expression().accept(*this);
}
else if (auto enumType = dynamic_cast<EnumType const*>(type->actualType().get()))
{
_memberAccess.expression().accept(*this);
m_context << enumType->memberValue(_memberAccess.memberName());
}
else
_memberAccess.expression().accept(*this);
return false;
}
_memberAccess.expression().accept(*this);
switch (_memberAccess.expression().annotation().type->category())
2014-11-21 18:14:56 +00:00
{
case Type::Category::Contract:
case Type::Category::Integer:
2015-01-07 21:54:56 +00:00
{
2015-01-27 15:42:28 +00:00
bool alsoSearchInteger = false;
if (_memberAccess.expression().annotation().type->category() == Type::Category::Contract)
{
ContractType const& type = dynamic_cast<ContractType const&>(*_memberAccess.expression().annotation().type);
if (type.isSuper())
2015-01-27 13:32:59 +00:00
{
solAssert(!!_memberAccess.annotation().referencedDeclaration, "Referenced declaration not resolved.");
utils().pushCombinedFunctionEntryLabel(m_context.superFunction(
dynamic_cast<FunctionDefinition const&>(*_memberAccess.annotation().referencedDeclaration),
type.contractDefinition()
));
2015-01-27 13:32:59 +00:00
}
2015-01-27 15:42:28 +00:00
else
{
// ordinary contract type
if (Declaration const* declaration = _memberAccess.annotation().referencedDeclaration)
{
u256 identifier;
if (auto const* variable = dynamic_cast<VariableDeclaration const*>(declaration))
identifier = FunctionType(*variable).externalIdentifier();
else if (auto const* function = dynamic_cast<FunctionDefinition const*>(declaration))
identifier = FunctionType(*function).externalIdentifier();
else
solAssert(false, "Contract member is neither variable nor function.");
utils().convertType(type, IntegerType(160, IntegerType::Modifier::Address), true);
m_context << identifier;
}
else
// not found in contract, search in members inherited from address
alsoSearchInteger = true;
}
2015-01-27 13:32:59 +00:00
}
else
alsoSearchInteger = true;
if (alsoSearchInteger)
2014-11-26 12:19:17 +00:00
{
if (member == "balance")
{
utils().convertType(
*_memberAccess.expression().annotation().type,
IntegerType(160, IntegerType::Modifier::Address),
true
);
m_context << Instruction::BALANCE;
}
else if ((set<string>{"send", "transfer", "call", "callcode", "delegatecall"}).count(member))
utils().convertType(
*_memberAccess.expression().annotation().type,
IntegerType(160, IntegerType::Modifier::Address),
true
);
else
solAssert(false, "Invalid member access to integer");
2014-11-26 12:19:17 +00:00
}
2014-11-21 18:14:56 +00:00
break;
}
case Type::Category::Function:
if (member == "selector")
2017-06-27 09:25:36 +00:00
{
m_context << Instruction::SWAP1 << Instruction::POP;
/// need to store store it as bytes4
utils().leftShiftNumberOnStack(224);
}
else
solAssert(!!_memberAccess.expression().annotation().type->memberType(member),
"Invalid member access to function.");
break;
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 << Instruction::COINBASE;
2014-11-24 12:23:58 +00:00
else if (member == "timestamp")
m_context << Instruction::TIMESTAMP;
else if (member == "difficulty")
m_context << Instruction::DIFFICULTY;
2014-11-24 12:23:58 +00:00
else if (member == "number")
m_context << Instruction::NUMBER;
2014-11-24 12:23:58 +00:00
else if (member == "gaslimit")
m_context << Instruction::GASLIMIT;
2014-11-24 12:23:58 +00:00
else if (member == "sender")
m_context << Instruction::CALLER;
2014-11-24 12:23:58 +00:00
else if (member == "value")
m_context << Instruction::CALLVALUE;
2014-11-24 12:23:58 +00:00
else if (member == "origin")
m_context << Instruction::ORIGIN;
2014-11-24 12:23:58 +00:00
else if (member == "gas")
m_context << Instruction::GAS;
2014-11-24 12:23:58 +00:00
else if (member == "gasprice")
m_context << Instruction::GASPRICE;
2015-02-10 13:57:01 +00:00
else if (member == "data")
m_context << u256(0) << Instruction::CALLDATASIZE;
else if (member == "sig")
m_context << u256(0) << Instruction::CALLDATALOAD
<< (u256(0xffffffff) << (256 - 32)) << Instruction::AND;
2014-11-24 12:23:58 +00:00
else
solAssert(false, "Unknown magic member.");
2014-11-21 18:14:56 +00:00
break;
case Type::Category::Struct:
2014-11-21 18:14:56 +00:00
{
StructType const& type = dynamic_cast<StructType const&>(*_memberAccess.expression().annotation().type);
2015-06-26 16:35:43 +00:00
switch (type.location())
{
case DataLocation::Storage:
{
2015-08-31 16:44:29 +00:00
pair<u256, unsigned> const& offsets = type.storageOffsetsOfMember(member);
m_context << offsets.first << Instruction::ADD << u256(offsets.second);
2015-06-26 16:35:43 +00:00
setLValueToStorageItem(_memberAccess);
break;
}
case DataLocation::Memory:
{
m_context << type.memoryOffsetOfMember(member) << Instruction::ADD;
setLValue<MemoryItem>(_memberAccess, *_memberAccess.annotation().type);
2015-06-26 16:35:43 +00:00
break;
}
default:
solAssert(false, "Illegal data location for struct.");
}
2014-11-21 18:14:56 +00:00
break;
}
case Type::Category::Enum:
{
EnumType const& type = dynamic_cast<EnumType const&>(*_memberAccess.expression().annotation().type);
2015-08-31 16:44:29 +00:00
m_context << type.memberValue(_memberAccess.memberName());
break;
}
case Type::Category::Array:
2015-02-12 14:44:35 +00:00
{
auto const& type = dynamic_cast<ArrayType const&>(*_memberAccess.expression().annotation().type);
if (member == "length")
{
if (!type.isDynamicallySized())
{
utils().popStackElement(type);
m_context << type.length();
}
else
switch (type.location())
{
case DataLocation::CallData:
m_context << Instruction::SWAP1 << Instruction::POP;
break;
case DataLocation::Storage:
setLValue<StorageArrayLength>(_memberAccess, type);
break;
case DataLocation::Memory:
m_context << Instruction::MLOAD;
break;
}
}
else if (member == "push")
{
solAssert(
type.isDynamicallySized() && type.location() == DataLocation::Storage,
"Tried to use .push() on a non-dynamically sized array"
);
}
else
solAssert(false, "Illegal array member.");
2015-02-12 14:44:35 +00:00
break;
}
2016-02-03 20:34:24 +00:00
case Type::Category::FixedBytes:
{
auto const& type = dynamic_cast<FixedBytesType const&>(*_memberAccess.expression().annotation().type);
utils().popStackElement(type);
if (member == "length")
m_context << u256(type.numBytes());
else
solAssert(false, "Illegal fixed bytes member.");
break;
}
2014-11-21 18:14:56 +00:00
default:
solAssert(false, "Member access to unknown type.");
2014-11-21 18:14:56 +00:00
}
return false;
}
2014-12-06 00:06:24 +00:00
bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
{
CompilerContext::LocationSetter locationSetter(m_context, _indexAccess);
2015-08-31 16:44:29 +00:00
_indexAccess.baseExpression().accept(*this);
2015-01-29 15:42:59 +00:00
Type const& baseType = *_indexAccess.baseExpression().annotation().type;
2015-08-31 16:44:29 +00:00
if (baseType.category() == Type::Category::Mapping)
2015-02-22 18:15:41 +00:00
{
// stack: storage_base_ref
2015-08-31 16:44:29 +00:00
TypePointer keyType = dynamic_cast<MappingType const&>(baseType).keyType();
solAssert(_indexAccess.indexExpression(), "Index expression expected.");
2015-08-04 14:58:31 +00:00
if (keyType->isDynamicallySized())
2015-08-03 16:09:39 +00:00
{
2015-08-31 16:44:29 +00:00
_indexAccess.indexExpression()->accept(*this);
2015-08-03 16:09:39 +00:00
utils().fetchFreeMemoryPointer();
// stack: base index mem
// note: the following operations must not allocate memory!
utils().packedEncode(
TypePointers{_indexAccess.indexExpression()->annotation().type},
TypePointers{keyType}
2015-08-04 14:58:31 +00:00
);
m_context << Instruction::SWAP1;
2015-08-03 16:09:39 +00:00
utils().storeInMemoryDynamic(IntegerType(256));
utils().toSizeAfterFreeMemoryPointer();
}
else
{
m_context << u256(0); // memory position
2015-08-31 16:44:29 +00:00
appendExpressionCopyToMemory(*keyType, *_indexAccess.indexExpression());
m_context << Instruction::SWAP1;
2015-08-03 16:09:39 +00:00
solAssert(CompilerUtils::freeMemoryPointer >= 0x40, "");
utils().storeInMemoryDynamic(IntegerType(256));
m_context << u256(0);
}
m_context << Instruction::KECCAK256;
m_context << u256(0);
2015-03-31 09:07:10 +00:00
setLValueToStorageItem(_indexAccess);
2015-02-22 18:15:41 +00:00
}
2015-08-31 16:44:29 +00:00
else if (baseType.category() == Type::Category::Array)
2015-02-22 18:15:41 +00:00
{
ArrayType const& arrayType = dynamic_cast<ArrayType const&>(baseType);
2015-08-31 16:44:29 +00:00
solAssert(_indexAccess.indexExpression(), "Index expression expected.");
2015-02-23 17:21:17 +00:00
2015-08-31 16:44:29 +00:00
_indexAccess.indexExpression()->accept(*this);
utils().convertType(*_indexAccess.indexExpression()->annotation().type, IntegerType(256), true);
2015-03-30 17:31:57 +00:00
// stack layout: <base_ref> [<length>] <index>
ArrayUtils(m_context).accessIndex(arrayType);
switch (arrayType.location())
2015-02-23 17:21:17 +00:00
{
case DataLocation::Storage:
2015-03-30 17:31:57 +00:00
if (arrayType.isByteArray())
2015-05-28 14:20:50 +00:00
{
solAssert(!arrayType.isString(), "Index access to string is not allowed.");
2015-03-30 17:31:57 +00:00
setLValue<StorageByteArrayElement>(_indexAccess);
2015-05-28 14:20:50 +00:00
}
2015-03-30 17:31:57 +00:00
else
2015-03-03 16:55:28 +00:00
setLValueToStorageItem(_indexAccess);
break;
case DataLocation::Memory:
setLValue<MemoryItem>(_indexAccess, *_indexAccess.annotation().type, !arrayType.isByteArray());
break;
case DataLocation::CallData:
//@todo if we implement this, the value in calldata has to be added to the base offset
solUnimplementedAssert(!arrayType.baseType()->isDynamicallySized(), "Nested arrays not yet implemented.");
2015-08-31 16:44:29 +00:00
if (arrayType.baseType()->isValueType())
CompilerUtils(m_context).loadFromMemoryDynamic(
2015-08-31 16:44:29 +00:00
*arrayType.baseType(),
true,
!arrayType.isByteArray(),
false
);
break;
2015-02-23 17:21:17 +00:00
}
2015-02-22 18:15:41 +00:00
}
2016-02-03 20:34:24 +00:00
else if (baseType.category() == Type::Category::FixedBytes)
{
FixedBytesType const& fixedBytesType = dynamic_cast<FixedBytesType const&>(baseType);
solAssert(_indexAccess.indexExpression(), "Index expression expected.");
_indexAccess.indexExpression()->accept(*this);
utils().convertType(*_indexAccess.indexExpression()->annotation().type, IntegerType(256), true);
2016-02-03 20:34:24 +00:00
// stack layout: <value> <index>
// check out-of-bounds access
m_context << u256(fixedBytesType.numBytes());
m_context << Instruction::DUP2 << Instruction::LT << Instruction::ISZERO;
2016-02-03 20:34:24 +00:00
// out-of-bounds access throws exception
m_context.appendConditionalInvalid();
2016-02-03 20:34:24 +00:00
m_context << Instruction::BYTE;
2016-11-30 10:03:26 +00:00
utils().leftShiftNumberOnStack(256 - 8);
2016-02-03 20:34:24 +00:00
}
else if (baseType.category() == Type::Category::TypeType)
{
solAssert(baseType.sizeOnStack() == 0, "");
solAssert(_indexAccess.annotation().type->sizeOnStack() == 0, "");
// no-op - this seems to be a lone array type (`structType[];`)
}
2015-02-22 18:15:41 +00:00
else
solAssert(false, "Index access only allowed for mappings or arrays.");
2014-11-10 16:31:09 +00:00
return false;
}
2014-12-06 00:06:24 +00:00
void ExpressionCompiler::endVisit(Identifier const& _identifier)
{
CompilerContext::LocationSetter locationSetter(m_context, _identifier);
Declaration const* declaration = _identifier.annotation().referencedDeclaration;
2015-03-06 05:02:35 +00:00
if (MagicVariableDeclaration const* magicVar = dynamic_cast<MagicVariableDeclaration const*>(declaration))
2014-11-21 18:14:56 +00:00
{
2015-11-19 17:02:04 +00:00
switch (magicVar->type()->category())
{
case Type::Category::Contract:
2015-01-27 13:32:59 +00:00
// "this" or "super"
2015-11-19 17:02:04 +00:00
if (!dynamic_cast<ContractType const&>(*magicVar->type()).isSuper())
m_context << Instruction::ADDRESS;
break;
case Type::Category::Integer:
// "now"
m_context << Instruction::TIMESTAMP;
break;
default:
break;
}
2014-11-21 18:14:56 +00:00
}
2015-01-19 18:18:34 +00:00
else if (FunctionDefinition const* functionDef = dynamic_cast<FunctionDefinition const*>(declaration))
utils().pushCombinedFunctionEntryLabel(m_context.resolveVirtualFunction(*functionDef));
2015-03-03 11:58:01 +00:00
else if (auto variable = dynamic_cast<VariableDeclaration const*>(declaration))
2016-10-21 18:16:21 +00:00
appendVariable(*variable, static_cast<Expression const&>(_identifier));
2015-09-10 17:40:07 +00:00
else if (auto contract = dynamic_cast<ContractDefinition const*>(declaration))
2015-01-19 18:18:34 +00:00
{
2015-09-10 17:40:07 +00:00
if (contract->isLibrary())
m_context.appendLibraryAddress(contract->fullyQualifiedName());
2015-01-19 18:18:34 +00:00
}
2015-01-29 15:42:59 +00:00
else if (dynamic_cast<EventDefinition const*>(declaration))
{
// no-op
}
else if (dynamic_cast<EnumDefinition const*>(declaration))
{
// no-op
}
else if (dynamic_cast<StructDefinition const*>(declaration))
{
// no-op
}
2015-01-19 18:18:34 +00:00
else
{
solAssert(false, "Identifier type not expected in expression context.");
2015-01-19 18:18:34 +00:00
}
}
2014-12-06 00:06:24 +00:00
void ExpressionCompiler::endVisit(Literal const& _literal)
{
CompilerContext::LocationSetter locationSetter(m_context, _literal);
TypePointer type = _literal.annotation().type;
2016-01-11 03:37:47 +00:00
2015-08-31 16:44:29 +00:00
switch (type->category())
{
case Type::Category::RationalNumber:
case Type::Category::Bool:
case Type::Category::Integer:
m_context << type->literalValue(&_literal);
break;
case Type::Category::StringLiteral:
break; // will be done during conversion
default:
2016-11-17 23:22:09 +00:00
solUnimplemented("Only integer, boolean and string literals implemented for now.");
}
}
2014-12-06 00:06:24 +00:00
void ExpressionCompiler::appendAndOrOperatorCode(BinaryOperation const& _binaryOperation)
{
Token::Value const c_op = _binaryOperation.getOperator();
solAssert(c_op == Token::Or || c_op == Token::And, "");
2015-08-31 16:44:29 +00:00
_binaryOperation.leftExpression().accept(*this);
m_context << Instruction::DUP1;
if (c_op == Token::And)
m_context << Instruction::ISZERO;
eth::AssemblyItem endLabel = m_context.appendConditionalJump();
m_context << Instruction::POP;
2015-08-31 16:44:29 +00:00
_binaryOperation.rightExpression().accept(*this);
m_context << endLabel;
}
void ExpressionCompiler::appendCompareOperatorCode(Token::Value _operator, Type const& _type)
{
solAssert(_type.sizeOnStack() == 1, "Comparison of multi-slot types.");
2015-02-10 08:52:19 +00:00
if (_operator == Token::Equal || _operator == Token::NotEqual)
{
if (FunctionType const* funType = dynamic_cast<decltype(funType)>(&_type))
{
if (funType->kind() == FunctionType::Kind::Internal)
{
// We have to remove the upper bits (construction time value) because they might
// be "unknown" in one of the operands and not in the other.
m_context << ((u256(1) << 32) - 1) << Instruction::AND;
m_context << Instruction::SWAP1;
m_context << ((u256(1) << 32) - 1) << Instruction::AND;
}
}
m_context << Instruction::EQ;
2015-02-10 08:52:19 +00:00
if (_operator == Token::NotEqual)
m_context << Instruction::ISZERO;
}
else
{
bool isSigned = false;
if (auto type = dynamic_cast<IntegerType const*>(&_type))
isSigned = type->isSigned();
switch (_operator)
{
2015-02-10 08:52:19 +00:00
case Token::GreaterThanOrEqual:
m_context <<
(isSigned ? Instruction::SLT : Instruction::LT) <<
Instruction::ISZERO;
break;
2015-02-10 08:52:19 +00:00
case Token::LessThanOrEqual:
m_context <<
(isSigned ? Instruction::SGT : Instruction::GT) <<
Instruction::ISZERO;
break;
case Token::GreaterThan:
m_context << (isSigned ? Instruction::SGT : Instruction::GT);
break;
case Token::LessThan:
m_context << (isSigned ? Instruction::SLT : Instruction::LT);
break;
default:
solAssert(false, "Unknown comparison operator.");
}
}
}
void ExpressionCompiler::appendOrdinaryBinaryOperatorCode(Token::Value _operator, Type const& _type)
{
if (Token::isArithmeticOp(_operator))
appendArithmeticOperatorCode(_operator, _type);
else if (Token::isBitOp(_operator))
appendBitOperatorCode(_operator);
else
solAssert(false, "Unknown binary operator.");
}
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 c_isSigned = type.isSigned();
if (_type.category() == Type::Category::FixedPoint)
solUnimplemented("Not yet implemented - FixedPointType.");
switch (_operator)
{
case Token::Add:
m_context << Instruction::ADD;
break;
case Token::Sub:
m_context << Instruction::SUB;
break;
case Token::Mul:
m_context << Instruction::MUL;
break;
case Token::Div:
case Token::Mod:
2016-08-12 14:50:37 +00:00
{
// Test for division by zero
m_context << Instruction::DUP2 << Instruction::ISZERO;
m_context.appendConditionalInvalid();
2016-08-12 14:50:37 +00:00
if (_operator == Token::Div)
m_context << (c_isSigned ? Instruction::SDIV : Instruction::DIV);
else
m_context << (c_isSigned ? Instruction::SMOD : Instruction::MOD);
break;
2016-08-12 14:50:37 +00:00
}
case Token::Exp:
m_context << Instruction::EXP;
break;
default:
solAssert(false, "Unknown arithmetic operator.");
}
}
void ExpressionCompiler::appendBitOperatorCode(Token::Value _operator)
{
switch (_operator)
{
case Token::BitOr:
m_context << Instruction::OR;
break;
case Token::BitAnd:
m_context << Instruction::AND;
break;
case Token::BitXor:
m_context << Instruction::XOR;
break;
default:
solAssert(false, "Unknown bit operator.");
}
}
void ExpressionCompiler::appendShiftOperatorCode(Token::Value _operator, Type const& _valueType, Type const& _shiftAmountType)
{
// stack: shift_amount value_to_shift
2016-04-29 23:15:22 +00:00
bool c_valueSigned = false;
if (auto valueType = dynamic_cast<IntegerType const*>(&_valueType))
c_valueSigned = valueType->isSigned();
2016-04-29 23:15:22 +00:00
else
solAssert(dynamic_cast<FixedBytesType const*>(&_valueType), "Only integer and fixed bytes type supported for shifts.");
2016-04-29 23:15:22 +00:00
// The amount can be a RationalNumberType too.
bool c_amountSigned = false;
if (auto amountType = dynamic_cast<RationalNumberType const*>(&_shiftAmountType))
2016-04-29 23:15:22 +00:00
{
// This should be handled by the type checker.
solAssert(amountType->integerType(), "");
solAssert(!amountType->integerType()->isSigned(), "");
2016-04-29 23:15:22 +00:00
}
else if (auto amountType = dynamic_cast<IntegerType const*>(&_shiftAmountType))
c_amountSigned = amountType->isSigned();
2016-04-29 23:15:22 +00:00
else
solAssert(false, "Invalid shift amount type.");
2016-04-29 23:15:22 +00:00
// shift by negative amount throws exception
if (c_amountSigned)
2016-04-29 23:15:22 +00:00
{
m_context << u256(0) << Instruction::DUP3 << Instruction::SLT;
m_context.appendConditionalInvalid();
2016-04-29 23:15:22 +00:00
}
switch (_operator)
{
case Token::SHL:
2016-04-29 23:15:22 +00:00
m_context << Instruction::SWAP1 << u256(2) << Instruction::EXP << Instruction::MUL;
break;
case Token::SAR:
m_context << Instruction::SWAP1 << u256(2) << Instruction::EXP << Instruction::SWAP1 << (c_valueSigned ? Instruction::SDIV : Instruction::DIV);
break;
case Token::SHR:
default:
solAssert(false, "Unknown shift operator.");
}
}
void ExpressionCompiler::appendExternalFunctionCall(
FunctionType const& _functionType,
vector<ASTPointer<Expression const>> const& _arguments
)
2014-12-10 22:01:40 +00:00
{
2015-09-01 09:19:02 +00:00
solAssert(
_functionType.takesArbitraryParameters() ||
_arguments.size() == _functionType.parameterTypes().size(), ""
);
2014-12-10 22:01:40 +00:00
// Assumed stack content here:
// <stack top>
// value [if _functionType.valueSet()]
// gas [if _functionType.gasSet()]
// self object [if bound - moved to top right away]
// function identifier [unless bare]
// contract address
unsigned selfSize = _functionType.bound() ? _functionType.selfType()->sizeOnStack() : 0;
unsigned gasValueSize = (_functionType.gasSet() ? 1 : 0) + (_functionType.valueSet() ? 1 : 0);
unsigned contractStackPos = m_context.currentToBaseStackOffset(1 + gasValueSize + selfSize + (_functionType.isBareCall() ? 0 : 1));
unsigned gasStackPos = m_context.currentToBaseStackOffset(gasValueSize);
unsigned valueStackPos = m_context.currentToBaseStackOffset(1);
// move self object to top
if (_functionType.bound())
utils().moveToStackTop(gasValueSize, _functionType.selfType()->sizeOnStack());
auto funKind = _functionType.kind();
bool returnSuccessCondition = funKind == FunctionType::Kind::BareCall || funKind == FunctionType::Kind::BareCallCode || funKind == FunctionType::Kind::BareDelegateCall;
bool isCallCode = funKind == FunctionType::Kind::BareCallCode || funKind == FunctionType::Kind::CallCode;
bool isDelegateCall = funKind == FunctionType::Kind::BareDelegateCall || funKind == FunctionType::Kind::DelegateCall;
2015-06-05 15:32:13 +00:00
2015-06-22 16:05:13 +00:00
unsigned retSize = 0;
if (returnSuccessCondition)
retSize = 0; // return value actually is success condition
2015-10-09 18:44:56 +00:00
else
for (auto const& retType: _functionType.returnParameterTypes())
{
solAssert(!retType->isDynamicallySized(), "Unable to return dynamic type from external call.");
2015-10-09 18:44:56 +00:00
retSize += retType->calldataEncodedSize();
}
2015-06-05 15:32:13 +00:00
// Evaluate arguments.
TypePointers argumentTypes;
TypePointers parameterTypes = _functionType.parameterTypes();
2016-10-21 10:30:58 +00:00
bool manualFunctionId = false;
if (
2017-08-01 09:32:23 +00:00
(funKind == FunctionType::Kind::BareCall || funKind == FunctionType::Kind::BareCallCode || funKind == FunctionType::Kind::BareDelegateCall) &&
2016-10-21 10:30:58 +00:00
!_arguments.empty()
)
{
solAssert(_arguments.front()->annotation().type->mobileType(), "");
manualFunctionId =
_arguments.front()->annotation().type->mobileType()->calldataEncodedSize(false) ==
CompilerUtils::dataStartOffset;
2016-10-21 10:30:58 +00:00
}
if (manualFunctionId)
2014-12-10 22:01:40 +00:00
{
// If we have a Bare* and the first type has exactly 4 bytes, use it as
// function identifier.
_arguments.front()->accept(*this);
utils().convertType(
*_arguments.front()->annotation().type,
IntegerType(8 * CompilerUtils::dataStartOffset),
true
2015-06-05 15:32:13 +00:00
);
for (unsigned i = 0; i < gasValueSize; ++i)
m_context << swapInstruction(gasValueSize - i);
gasStackPos++;
valueStackPos++;
}
if (_functionType.bound())
{
argumentTypes.push_back(_functionType.selfType());
parameterTypes.insert(parameterTypes.begin(), _functionType.selfType());
}
for (size_t i = manualFunctionId ? 1 : 0; i < _arguments.size(); ++i)
{
_arguments[i]->accept(*this);
argumentTypes.push_back(_arguments[i]->annotation().type);
2014-12-10 22:01:40 +00:00
}
if (funKind == FunctionType::Kind::ECRecover)
{
// Clears 32 bytes of currently free memory and advances free memory pointer.
// Output area will be "start of input area" - 32.
// The reason is that a failing ECRecover cannot be detected, it will just return
// zero bytes (which we cannot detect).
solAssert(0 < retSize && retSize <= 32, "");
utils().fetchFreeMemoryPointer();
m_context << u256(0) << Instruction::DUP2 << Instruction::MSTORE;
m_context << u256(32) << Instruction::ADD;
utils().storeFreeMemoryPointer();
}
// Touch the end of the output area so that we do not pay for memory resize during the call
// (which we would have to subtract from the gas left)
// We could also just use MLOAD; POP right before the gas calculation, but the optimizer
// would remove that, so we use MSTORE here.
if (!_functionType.gasSet() && retSize > 0)
{
m_context << u256(0);
utils().fetchFreeMemoryPointer();
// This touches too much, but that way we save some rounding arithmetics
m_context << u256(retSize) << Instruction::ADD << Instruction::MSTORE;
}
// Copy function identifier to memory.
utils().fetchFreeMemoryPointer();
if (!_functionType.isBareCall() || manualFunctionId)
{
m_context << dupInstruction(2 + gasValueSize + CompilerUtils::sizeOnStack(argumentTypes));
utils().storeInMemoryDynamic(IntegerType(8 * CompilerUtils::dataStartOffset), false);
}
2015-04-21 08:59:48 +00:00
// If the function takes arbitrary parameters, copy dynamic length data in place.
// Move arguments to memory, will not update the free memory pointer (but will update the memory
// pointer on the stack).
utils().encodeToMemory(
argumentTypes,
parameterTypes,
2015-04-21 08:59:48 +00:00
_functionType.padArguments(),
_functionType.takesArbitraryParameters(),
isCallCode || isDelegateCall
2015-04-21 08:59:48 +00:00
);
// Stack now:
// <stack top>
// input_memory_end
// value [if _functionType.valueSet()]
// gas [if _functionType.gasSet()]
// function identifier [unless bare]
// contract address
// Output data will replace input data, unless we have ECRecover (then, output
// area will be 32 bytes just before input area).
// put on stack: <size of output> <memory pos of output> <size of input> <memory pos of input>
m_context << u256(retSize);
utils().fetchFreeMemoryPointer(); // This is the start of input
if (funKind == FunctionType::Kind::ECRecover)
{
// In this case, output is 32 bytes before input and has already been cleared.
m_context << u256(32) << Instruction::DUP2 << Instruction::SUB << Instruction::SWAP1;
// Here: <input end> <output size> <outpos> <input pos>
m_context << Instruction::DUP1 << Instruction::DUP5 << Instruction::SUB;
m_context << Instruction::SWAP1;
}
else
{
m_context << Instruction::DUP1 << Instruction::DUP4 << Instruction::SUB;
m_context << Instruction::DUP2;
}
2015-01-13 17:12:19 +00:00
2015-06-05 15:32:13 +00:00
// CALL arguments: outSize, outOff, inSize, inOff (already present up to here)
// [value,] addr, gas (stack top)
if (isDelegateCall)
solAssert(!_functionType.valueSet(), "Value set for delegatecall");
else if (_functionType.valueSet())
m_context << dupInstruction(m_context.baseToCurrentStackOffset(valueStackPos));
2014-12-10 22:01:40 +00:00
else
m_context << u256(0);
m_context << dupInstruction(m_context.baseToCurrentStackOffset(contractStackPos));
bool existenceChecked = false;
// Check the the target contract exists (has code) for non-low-level calls.
if (funKind == FunctionType::Kind::External || funKind == FunctionType::Kind::CallCode || funKind == FunctionType::Kind::DelegateCall)
{
m_context << Instruction::DUP1 << Instruction::EXTCODESIZE << Instruction::ISZERO;
m_context.appendConditionalRevert();
existenceChecked = true;
}
if (_functionType.gasSet())
m_context << dupInstruction(m_context.baseToCurrentStackOffset(gasStackPos));
else
2015-07-28 11:37:38 +00:00
{
2015-03-06 15:23:39 +00:00
// send all gas except the amount needed to execute "SUB" and "CALL"
// @todo this retains too much gas for now, needs to be fine-tuned.
u256 gasNeededByCaller = eth::GasCosts::callGas + 10;
2015-07-28 11:37:38 +00:00
if (_functionType.valueSet())
gasNeededByCaller += eth::GasCosts::callValueTransferGas;
if (!existenceChecked)
gasNeededByCaller += eth::GasCosts::callNewAccountGas; // we never know
m_context << gasNeededByCaller << Instruction::GAS << Instruction::SUB;
2015-07-28 11:37:38 +00:00
}
if (isDelegateCall)
m_context << Instruction::DELEGATECALL;
else if (isCallCode)
m_context << Instruction::CALLCODE;
else
m_context << Instruction::CALL;
2015-06-05 15:38:06 +00:00
unsigned remainsSize =
2 + // contract address, input_memory_end
2015-06-05 15:38:06 +00:00
_functionType.valueSet() +
_functionType.gasSet() +
(!_functionType.isBareCall() || manualFunctionId);
if (returnSuccessCondition)
m_context << swapInstruction(remainsSize);
else
{
//Propagate error condition (if CALL pushes 0 on stack).
m_context << Instruction::ISZERO;
m_context.appendConditionalRevert();
}
utils().popStackSlots(remainsSize);
if (returnSuccessCondition)
{
// already there
}
else if (funKind == FunctionType::Kind::RIPEMD160)
{
// fix: built-in contract returns right-aligned data
utils().fetchFreeMemoryPointer();
utils().loadFromMemoryDynamic(IntegerType(160), false, true, false);
utils().convertType(IntegerType(160), FixedBytesType(20));
}
else if (funKind == FunctionType::Kind::ECRecover)
{
// Output is 32 bytes before input / free mem pointer.
// Failing ecrecover cannot be detected, so we clear output before the call.
m_context << u256(32);
utils().fetchFreeMemoryPointer();
m_context << Instruction::SUB << Instruction::MLOAD;
}
2015-10-09 18:44:56 +00:00
else if (!_functionType.returnParameterTypes().empty())
2015-06-05 15:32:13 +00:00
{
utils().fetchFreeMemoryPointer();
2015-10-09 18:44:56 +00:00
bool memoryNeeded = false;
for (auto const& retType: _functionType.returnParameterTypes())
{
2015-10-09 18:44:56 +00:00
utils().loadFromMemoryDynamic(*retType, false, true, true);
if (dynamic_cast<ReferenceType const*>(retType.get()))
memoryNeeded = true;
}
2015-10-09 18:44:56 +00:00
if (memoryNeeded)
utils().storeFreeMemoryPointer();
else
m_context << Instruction::POP;
}
2015-01-29 15:42:59 +00:00
}
2015-02-10 16:53:43 +00:00
void ExpressionCompiler::appendExpressionCopyToMemory(Type const& _expectedType, Expression const& _expression)
{
solUnimplementedAssert(_expectedType.isValueType(), "Not implemented for non-value types.");
_expression.accept(*this);
utils().convertType(*_expression.annotation().type, _expectedType, true);
utils().storeInMemoryDynamic(_expectedType);
}
2016-10-21 18:16:21 +00:00
void ExpressionCompiler::appendVariable(VariableDeclaration const& _variable, Expression const& _expression)
{
if (!_variable.isConstant())
setLValueFromDeclaration(_variable, _expression);
else
{
_variable.value()->accept(*this);
utils().convertType(*_variable.value()->annotation().type, *_variable.annotation().type);
}
}
2015-02-25 14:14:22 +00:00
void ExpressionCompiler::setLValueFromDeclaration(Declaration const& _declaration, Expression const& _expression)
{
2015-02-25 14:14:22 +00:00
if (m_context.isLocalVariable(&_declaration))
2015-09-21 16:55:58 +00:00
setLValue<StackVariable>(_expression, dynamic_cast<VariableDeclaration const&>(_declaration));
2015-02-25 14:14:22 +00:00
else if (m_context.isStateVariable(&_declaration))
2015-09-21 16:55:58 +00:00
setLValue<StorageItem>(_expression, dynamic_cast<VariableDeclaration const&>(_declaration));
else
2015-02-25 14:14:22 +00:00
BOOST_THROW_EXCEPTION(InternalCompilerError()
2015-08-31 16:44:29 +00:00
<< errinfo_sourceLocation(_expression.location())
2015-02-25 14:14:22 +00:00
<< errinfo_comment("Identifier type not supported or identifier not found."));
}
2015-02-25 14:14:22 +00:00
void ExpressionCompiler::setLValueToStorageItem(Expression const& _expression)
{
setLValue<StorageItem>(_expression, *_expression.annotation().type);
2014-11-10 16:31:09 +00:00
}
2017-08-30 21:44:46 +00:00
bool ExpressionCompiler::cleanupNeededForOp(Type::Category _type, Token::Value _op)
{
if (Token::isCompareOp(_op) || Token::isShiftOp(_op))
return true;
else if (_type == Type::Category::Integer && (_op == Token::Div || _op == Token::Mod))
return true;
else
return false;
}
CompilerUtils ExpressionCompiler::utils()
{
return CompilerUtils(m_context);
}
}
}