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>
|
2015-02-02 16:52:50 +00:00
|
|
|
#include <boost/range/adaptor/reversed.hpp>
|
2014-11-10 16:31:09 +00:00
|
|
|
#include <libdevcore/Common.h>
|
2015-05-19 17:51:38 +00:00
|
|
|
#include <libdevcore/SHA3.h>
|
2015-11-21 13:34:21 +00:00
|
|
|
#include <libethcore/ChainOperationParams.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>
|
2014-10-30 00:20:32 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2015-11-21 13:34:21 +00:00
|
|
|
// TODO: FIXME: HOMESTEAD: XXX: @chriseth Params deprecated - use EVMSchedule instead.
|
|
|
|
|
2014-12-17 15:23:18 +00:00
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2015-02-25 14:14:22 +00:00
|
|
|
void ExpressionCompiler::compile(Expression const& _expression)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2015-02-25 14:14:22 +00:00
|
|
|
_expression.accept(*this);
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2015-09-16 14:56:30 +00:00
|
|
|
TypePointer type = _varDecl.value()->annotation().type;
|
2015-07-15 17:13:42 +00:00
|
|
|
solAssert(!!type, "Type information not available.");
|
2015-03-07 22:08:39 +00:00
|
|
|
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
|
|
|
|
2015-09-16 14:56:30 +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.
|
|
|
|
utils().convertType(*type, *type->mobileType());
|
|
|
|
type = type->mobileType();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-16 14:56:30 +00:00
|
|
|
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);
|
2015-01-27 15:55:06 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 15:57:57 +00:00
|
|
|
void ExpressionCompiler::appendConstStateVariableAccessor(VariableDeclaration const& _varDecl)
|
|
|
|
{
|
|
|
|
solAssert(_varDecl.isConstant(), "");
|
2015-08-31 16:44:29 +00:00
|
|
|
_varDecl.value()->accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
utils().convertType(*_varDecl.value()->annotation().type, *_varDecl.annotation().type);
|
2015-08-21 15:57:57 +00:00
|
|
|
|
|
|
|
// append return
|
2015-09-16 14:56:30 +00:00
|
|
|
m_context << eth::dupInstruction(_varDecl.annotation().type->sizeOnStack() + 1);
|
2015-08-21 15:57:57 +00:00
|
|
|
m_context.appendJump(eth::AssemblyItem::JumpType::OutOfFunction);
|
|
|
|
}
|
|
|
|
|
2015-02-25 14:14:22 +00:00
|
|
|
void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const& _varDecl)
|
2015-02-17 15:21:38 +00:00
|
|
|
{
|
2015-08-21 15:57:57 +00:00
|
|
|
solAssert(!_varDecl.isConstant(), "");
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _varDecl);
|
2015-02-25 14:14:22 +00:00
|
|
|
FunctionType accessorType(_varDecl);
|
|
|
|
|
2015-12-09 16:53:15 +00:00
|
|
|
TypePointers paramTypes = accessorType.parameterTypes();
|
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
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
TypePointer returnType = _varDecl.annotation().type;
|
2015-02-25 14:14:22 +00:00
|
|
|
|
2015-04-01 12:22:42 +00:00
|
|
|
for (size_t i = 0; i < paramTypes.size(); ++i)
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2015-04-01 12:22:42 +00:00
|
|
|
if (auto mappingType = dynamic_cast<MappingType const*>(returnType.get()))
|
|
|
|
{
|
2015-06-05 22:57:51 +00:00
|
|
|
solAssert(CompilerUtils::freeMemoryPointer >= 0x40, "");
|
2015-08-03 16:09:39 +00:00
|
|
|
solAssert(
|
|
|
|
!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 << eth::Instruction::POP;
|
2015-04-01 12:22:42 +00:00
|
|
|
// move storage offset to memory.
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().storeInMemory(32);
|
2015-04-02 15:03:02 +00:00
|
|
|
// move key to memory.
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().copyToStackTop(paramTypes.size() - i, 1);
|
|
|
|
utils().storeInMemory(0);
|
2015-04-01 12:22:42 +00:00
|
|
|
m_context << u256(64) << u256(0) << eth::Instruction::SHA3;
|
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();
|
2015-04-01 12:22:42 +00:00
|
|
|
}
|
|
|
|
else if (auto arrayType = dynamic_cast<ArrayType const*>(returnType.get()))
|
|
|
|
{
|
2015-04-02 15:03:02 +00:00
|
|
|
// pop offset
|
|
|
|
m_context << eth::Instruction::POP;
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().copyToStackTop(paramTypes.size() - i + 1, 1);
|
2015-04-01 12:22:42 +00:00
|
|
|
ArrayUtils(m_context).accessIndex(*arrayType);
|
2015-08-31 16:44:29 +00:00
|
|
|
returnType = arrayType->baseType();
|
2015-04-01 12:22:42 +00:00
|
|
|
}
|
|
|
|
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 << eth::Instruction::SWAP2 << eth::Instruction::POP << eth::Instruction::SWAP1;
|
|
|
|
else if (paramTypes.size() >= 2)
|
|
|
|
{
|
|
|
|
m_context << eth::swapInstruction(paramTypes.size());
|
|
|
|
m_context << eth::Instruction::POP;
|
|
|
|
m_context << eth::swapInstruction(paramTypes.size());
|
2015-06-15 10:10:41 +00:00
|
|
|
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 << eth::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)
|
2015-03-11 17:09:35 +00:00
|
|
|
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]);
|
2015-03-13 18:48:24 +00:00
|
|
|
m_context << eth::Instruction::DUP1 << u256(offsets.first) << eth::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
|
2015-02-25 14:14:22 +00:00
|
|
|
m_context << eth::Instruction::POP;
|
|
|
|
}
|
|
|
|
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), "");
|
2015-03-31 09:07:10 +00:00
|
|
|
solAssert(retSizeOnStack <= 15, "Stack is too deep.");
|
2015-03-09 18:22:24 +00:00
|
|
|
m_context << eth::dupInstruction(retSizeOnStack + 1);
|
|
|
|
m_context.appendJump(eth::AssemblyItem::JumpType::OutOfFunction);
|
2015-02-17 15:21:38 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:06:24 +00:00
|
|
|
bool ExpressionCompiler::visit(Assignment const& _assignment)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _assignment);
|
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 type = _assignment.rightHandSide().annotation().type->closestTemporaryType(
|
|
|
|
_assignment.leftHandSide().annotation().type
|
|
|
|
);
|
|
|
|
utils().convertType(*_assignment.rightHandSide().annotation().type, *type);
|
|
|
|
|
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.");
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
Token::Value op = _assignment.assignmentOperator();
|
2015-02-09 13:00:12 +00:00
|
|
|
if (op != Token::Assign) // compound assignment
|
2014-11-10 16:31:09 +00:00
|
|
|
{
|
2015-09-16 14:56:30 +00:00
|
|
|
solAssert(_assignment.annotation().type->isValueType(), "Compound operators not implemented for non-value types.");
|
2015-03-03 16:55:28 +00:00
|
|
|
unsigned lvalueSize = m_currentLValue->sizeOnStack();
|
2015-09-16 14:56:30 +00:00
|
|
|
unsigned itemSize = _assignment.annotation().type->sizeOnStack();
|
2015-03-03 16:55:28 +00:00
|
|
|
if (lvalueSize > 0)
|
|
|
|
{
|
2015-06-15 10:10:41 +00:00
|
|
|
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);
|
2015-09-16 14:56:30 +00:00
|
|
|
appendOrdinaryBinaryOperatorCode(Token::AssignmentToBinaryOp(op), *_assignment.annotation().type);
|
2015-03-03 16:55:28 +00:00
|
|
|
if (lvalueSize > 0)
|
2015-03-13 12:14:51 +00:00
|
|
|
{
|
2015-06-05 23:04:55 +00:00
|
|
|
solAssert(itemSize + lvalueSize <= 16, "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 << eth::swapInstruction(itemSize + lvalueSize) << eth::Instruction::POP;
|
2015-03-13 12:14:51 +00:00
|
|
|
}
|
2014-11-10 16:31:09 +00:00
|
|
|
}
|
2015-08-31 16:44:29 +00:00
|
|
|
m_currentLValue->storeValue(*type, _assignment.location());
|
2014-11-13 00:12:57 +00:00
|
|
|
m_currentLValue.reset();
|
2014-10-30 00:20:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-14 13:19:50 +00:00
|
|
|
bool ExpressionCompiler::visit(TupleExpression const& _tuple)
|
|
|
|
{
|
|
|
|
vector<unique_ptr<LValue>> lvalues;
|
|
|
|
for (auto const& component: _tuple.components())
|
|
|
|
if (component)
|
|
|
|
{
|
|
|
|
component->accept(*this);
|
|
|
|
if (_tuple.annotation().lValueRequested)
|
|
|
|
{
|
|
|
|
solAssert(!!m_currentLValue, "");
|
|
|
|
lvalues.push_back(move(m_currentLValue));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_tuple.annotation().lValueRequested)
|
|
|
|
lvalues.push_back(unique_ptr<LValue>());
|
|
|
|
if (_tuple.annotation().lValueRequested)
|
|
|
|
m_currentLValue.reset(new TupleObject(m_context, move(lvalues)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-19 10:31:17 +00:00
|
|
|
bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _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
|
2015-09-16 14:56:30 +00:00
|
|
|
if (_unaryOperation.annotation().type->category() == Type::Category::IntegerConstant)
|
2014-12-19 10:31:17 +00:00
|
|
|
{
|
2015-09-16 14:56:30 +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
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
switch (_unaryOperation.getOperator())
|
|
|
|
{
|
2015-02-09 13:00:12 +00:00
|
|
|
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;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::BitNot: // ~
|
2014-10-31 16:20:27 +00:00
|
|
|
m_context << eth::Instruction::NOT;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-03-04 16:35:23 +00:00
|
|
|
case Token::After: // after
|
|
|
|
m_context << eth::Instruction::TIMESTAMP << eth::Instruction::ADD;
|
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
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();
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
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());
|
2014-10-30 00:20:32 +00:00
|
|
|
if (!_unaryOperation.isPrefixOperation())
|
2014-11-10 16:31:09 +00:00
|
|
|
{
|
2015-03-11 17:09:35 +00:00
|
|
|
// store value for later
|
2015-09-16 14:56:30 +00:00
|
|
|
solAssert(_unaryOperation.annotation().type->sizeOnStack() == 1, "Stack size != 1 not implemented.");
|
2015-03-11 17:09:35 +00:00
|
|
|
m_context << eth::Instruction::DUP1;
|
|
|
|
if (m_currentLValue->sizeOnStack() > 0)
|
|
|
|
for (unsigned i = 1 + m_currentLValue->sizeOnStack(); i > 0; --i)
|
|
|
|
m_context << eth::swapInstruction(i);
|
2014-11-10 16:31:09 +00:00
|
|
|
}
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context << u256(1);
|
2015-02-09 13:00:12 +00:00
|
|
|
if (_unaryOperation.getOperator() == Token::Inc)
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context << eth::Instruction::ADD;
|
|
|
|
else
|
2015-03-11 17:09:35 +00:00
|
|
|
m_context << eth::Instruction::SWAP1 << eth::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 << eth::swapInstruction(i);
|
2015-02-25 14:14:22 +00:00
|
|
|
m_currentLValue->storeValue(
|
2015-09-16 14:56:30 +00:00
|
|
|
*_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();
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::Add: // +
|
2014-10-30 00:20:32 +00:00
|
|
|
// unary add, so basically no-op
|
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::Sub: // -
|
2014-10-30 00:20:32 +00:00
|
|
|
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
|
|
|
{
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _binaryOperation);
|
2015-08-31 16:44:29 +00:00
|
|
|
Expression const& leftExpression = _binaryOperation.leftExpression();
|
|
|
|
Expression const& rightExpression = _binaryOperation.rightExpression();
|
2015-09-16 14:56:30 +00:00
|
|
|
solAssert(!!_binaryOperation.annotation().commonType, "");
|
|
|
|
Type const& commonType = *_binaryOperation.annotation().commonType;
|
2015-01-09 20:36:25 +00:00
|
|
|
Token::Value const c_op = _binaryOperation.getOperator();
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2015-02-09 13:00:12 +00:00
|
|
|
if (c_op == Token::And || c_op == Token::Or) // special case: short-circuiting
|
2014-10-30 00:20:32 +00:00
|
|
|
appendAndOrOperatorCode(_binaryOperation);
|
2015-08-31 16:44:29 +00:00
|
|
|
else if (commonType.category() == Type::Category::IntegerConstant)
|
2014-12-19 10:31:17 +00:00
|
|
|
m_context << commonType.literalValue(nullptr);
|
2014-10-30 00:20:32 +00:00
|
|
|
else
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
bool cleanupNeeded = commonType.category() == Type::Category::Integer &&
|
2015-02-25 14:14:22 +00:00
|
|
|
(Token::isCompareOp(c_op) || c_op == Token::Div || c_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)
|
|
|
|
{
|
2015-09-16 14:56:30 +00:00
|
|
|
return dynamic_cast<Literal const*>(&_e) || _e.annotation().type->category() == Type::Category::IntegerConstant;
|
2014-12-19 10:31:17 +00:00
|
|
|
};
|
2015-01-09 20:36:25 +00:00
|
|
|
bool swap = m_optimize && Token::isCommutativeOp(c_op) && isLiteral(rightExpression) && !isLiteral(leftExpression);
|
2014-12-11 16:35:23 +00:00
|
|
|
if (swap)
|
|
|
|
{
|
|
|
|
leftExpression.accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
utils().convertType(*leftExpression.annotation().type, commonType, cleanupNeeded);
|
2014-12-11 16:35:23 +00:00
|
|
|
rightExpression.accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
utils().convertType(*rightExpression.annotation().type, commonType, cleanupNeeded);
|
2014-12-11 16:35:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rightExpression.accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
utils().convertType(*rightExpression.annotation().type, commonType, cleanupNeeded);
|
2014-12-11 16:35:23 +00:00
|
|
|
leftExpression.accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
utils().convertType(*leftExpression.annotation().type, commonType, cleanupNeeded);
|
2014-12-11 16:35:23 +00:00
|
|
|
}
|
2015-01-09 20:36:25 +00:00
|
|
|
if (Token::isCompareOp(c_op))
|
|
|
|
appendCompareOperatorCode(c_op, commonType);
|
2014-11-04 14:01:07 +00:00
|
|
|
else
|
2015-01-09 20:36:25 +00:00
|
|
|
appendOrdinaryBinaryOperatorCode(c_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
|
|
|
{
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _functionCall);
|
2014-11-26 12:19:17 +00:00
|
|
|
using Location = FunctionType::Location;
|
2015-09-16 14:56:30 +00:00
|
|
|
if (_functionCall.annotation().isTypeConversion)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
solAssert(_functionCall.arguments().size() == 1, "");
|
|
|
|
solAssert(_functionCall.names().empty(), "");
|
|
|
|
Expression const& firstArgument = *_functionCall.arguments().front();
|
2014-10-30 00:20:32 +00:00
|
|
|
firstArgument.accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
utils().convertType(*firstArgument.annotation().type, *_functionCall.annotation().type);
|
2015-06-30 19:08:34 +00:00
|
|
|
return false;
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
2015-06-30 19:08:34 +00:00
|
|
|
|
|
|
|
FunctionTypePointer functionType;
|
2015-09-16 14:56:30 +00:00
|
|
|
if (_functionCall.annotation().isStructConstructorCall)
|
2015-06-30 19:08:34 +00:00
|
|
|
{
|
2015-09-16 14:56:30 +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
|
2015-09-16 14:56:30 +00:00
|
|
|
functionType = dynamic_pointer_cast<FunctionType const>(_functionCall.expression().annotation().type);
|
2015-06-30 19:08:34 +00:00
|
|
|
|
2015-12-09 16:53:15 +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;
|
2014-10-30 00:20:32 +00:00
|
|
|
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, "");
|
|
|
|
}
|
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
if (_functionCall.annotation().isStructConstructorCall)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2015-09-16 14:56:30 +00:00
|
|
|
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
|
|
|
|
2015-09-17 13:04:44 +00:00
|
|
|
m_context << max(u256(32u), structType.memorySize());
|
2015-06-30 19:08:34 +00:00
|
|
|
utils().allocateMemory();
|
|
|
|
m_context << eth::Instruction::DUP1;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < arguments.size(); ++i)
|
|
|
|
{
|
|
|
|
arguments[i]->accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
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 << eth::Instruction::POP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FunctionType const& function = *functionType;
|
2015-11-27 13:08:09 +00:00
|
|
|
if (function.bound())
|
|
|
|
// Only callcode functions can be bound, this might be lifted later.
|
|
|
|
solAssert(function.location() == Location::CallCode, "");
|
2015-08-31 16:44:29 +00:00
|
|
|
switch (function.location())
|
2014-12-04 18:38:24 +00:00
|
|
|
{
|
2015-02-09 13:08:48 +00:00
|
|
|
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);
|
2015-09-16 14:56:30 +00:00
|
|
|
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);
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2015-03-09 18:22:24 +00:00
|
|
|
m_context.appendJump(eth::AssemblyItem::JumpType::IntoFunction);
|
2014-11-25 17:23:39 +00:00
|
|
|
m_context << returnLabel;
|
2014-10-30 00:20:32 +00:00
|
|
|
|
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
|
2015-08-31 16:44:29 +00:00
|
|
|
m_context.adjustStackOffset(returnParametersSize - CompilerUtils::sizeOnStack(function.parameterTypes()) - 1);
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
2014-11-25 17:23:39 +00:00
|
|
|
}
|
2015-02-09 13:08:48 +00:00
|
|
|
case Location::External:
|
2015-05-15 16:02:09 +00:00
|
|
|
case Location::CallCode:
|
2015-02-09 13:08:48 +00:00
|
|
|
case Location::Bare:
|
2015-05-15 16:02:09 +00:00
|
|
|
case Location::BareCallCode:
|
2015-08-31 16:44:29 +00:00
|
|
|
_functionCall.expression().accept(*this);
|
2015-05-15 16:02:09 +00:00
|
|
|
appendExternalFunctionCall(function, arguments);
|
2015-01-12 11:47:37 +00:00
|
|
|
break;
|
2015-02-09 13:08:48 +00:00
|
|
|
case Location::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, "");
|
2015-06-05 22:57:51 +00:00
|
|
|
TypePointers argumentTypes;
|
|
|
|
for (auto const& arg: arguments)
|
|
|
|
{
|
|
|
|
arg->accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
argumentTypes.push_back(arg->annotation().type);
|
2015-06-05 22:57:51 +00:00
|
|
|
}
|
2015-09-01 09:19:02 +00:00
|
|
|
ContractDefinition const& contract =
|
|
|
|
dynamic_cast<ContractType const&>(*function.returnParameterTypes().front()).contractDefinition();
|
2015-01-13 17:12:19 +00:00
|
|
|
// copy the contract's code into memory
|
2015-09-10 10:01:05 +00:00
|
|
|
eth::Assembly const& assembly = m_context.compiledContract(contract);
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().fetchFreeMemoryPointer();
|
2015-09-10 10:01:05 +00:00
|
|
|
// pushes size
|
|
|
|
eth::AssemblyItem subroutine = m_context.addSubroutine(assembly);
|
|
|
|
m_context << eth::Instruction::DUP1 << subroutine;
|
2015-06-05 22:57:51 +00:00
|
|
|
m_context << eth::Instruction::DUP4 << eth::Instruction::CODECOPY;
|
2015-01-13 17:12:19 +00:00
|
|
|
|
2015-06-05 22:57:51 +00:00
|
|
|
m_context << eth::Instruction::ADD;
|
2015-08-31 16:44:29 +00:00
|
|
|
utils().encodeToMemory(argumentTypes, function.parameterTypes());
|
2015-06-05 22:57:51 +00:00
|
|
|
// now on stack: memory_end_ptr
|
|
|
|
// need: size, offset, endowment
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().toSizeAfterFreeMemoryPointer();
|
2015-01-13 17:12:19 +00:00
|
|
|
if (function.valueSet())
|
|
|
|
m_context << eth::dupInstruction(3);
|
|
|
|
else
|
|
|
|
m_context << u256(0);
|
|
|
|
m_context << eth::Instruction::CREATE;
|
|
|
|
if (function.valueSet())
|
|
|
|
m_context << eth::swapInstruction(1) << eth::Instruction::POP;
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-02-09 13:08:48 +00:00
|
|
|
case Location::SetGas:
|
2014-12-10 22:01:40 +00:00
|
|
|
{
|
2015-01-12 11:47:37 +00:00
|
|
|
// stack layout: contract_address function_id [gas] [value]
|
2015-08-31 16:44:29 +00:00
|
|
|
_functionCall.expression().accept(*this);
|
2015-06-03 16:06:49 +00:00
|
|
|
|
2015-01-12 11:47:37 +00:00
|
|
|
arguments.front()->accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
utils().convertType(*arguments.front()->annotation().type, IntegerType(256), true);
|
2015-01-12 11:47:37 +00:00
|
|
|
// 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 << eth::swapInstruction(stackDepth);
|
|
|
|
if (function.gasSet())
|
|
|
|
m_context << eth::Instruction::POP;
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
2014-12-10 22:01:40 +00:00
|
|
|
}
|
2015-02-09 13:08:48 +00:00
|
|
|
case Location::SetValue:
|
2015-01-12 11:47:37 +00:00
|
|
|
// stack layout: contract_address function_id [gas] [value]
|
2015-08-31 16:44:29 +00:00
|
|
|
_functionCall.expression().accept(*this);
|
2015-01-12 11:47:37 +00:00
|
|
|
// 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 << eth::Instruction::POP;
|
|
|
|
arguments.front()->accept(*this);
|
|
|
|
break;
|
2015-02-09 13:08:48 +00:00
|
|
|
case Location::Send:
|
2015-08-31 16:44:29 +00:00
|
|
|
_functionCall.expression().accept(*this);
|
2015-06-05 10:25:10 +00:00
|
|
|
m_context << u256(0); // do not send gas (there still is the stipend)
|
2015-01-12 11:47:37 +00:00
|
|
|
arguments.front()->accept(*this);
|
2015-06-16 09:28:35 +00:00
|
|
|
utils().convertType(
|
2015-09-16 14:56:30 +00:00
|
|
|
*arguments.front()->annotation().type,
|
2015-08-31 16:44:29 +00:00
|
|
|
*function.parameterTypes().front(), true
|
2015-06-16 09:28:35 +00:00
|
|
|
);
|
2015-04-22 16:53:58 +00:00
|
|
|
appendExternalFunctionCall(
|
|
|
|
FunctionType(
|
|
|
|
TypePointers{},
|
|
|
|
TypePointers{},
|
|
|
|
strings(),
|
2015-04-27 11:07:25 +00:00
|
|
|
strings(),
|
2015-05-15 16:02:09 +00:00
|
|
|
Location::Bare,
|
2015-04-22 16:53:58 +00:00
|
|
|
false,
|
2015-06-22 16:05:13 +00:00
|
|
|
nullptr,
|
2015-06-05 10:25:10 +00:00
|
|
|
true,
|
2015-04-22 16:53:58 +00:00
|
|
|
true
|
|
|
|
),
|
2015-05-15 16:02:09 +00:00
|
|
|
{}
|
2015-04-22 16:53:58 +00:00
|
|
|
);
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
2015-11-28 23:09:15 +00:00
|
|
|
case Location::Selfdestruct:
|
2014-12-04 18:38:24 +00:00
|
|
|
arguments.front()->accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
utils().convertType(*arguments.front()->annotation().type, *function.parameterTypes().front(), true);
|
2014-12-04 18:38:24 +00:00
|
|
|
m_context << eth::Instruction::SUICIDE;
|
|
|
|
break;
|
|
|
|
case Location::SHA3:
|
2015-02-03 13:02:58 +00:00
|
|
|
{
|
2015-06-05 22:57:51 +00:00
|
|
|
TypePointers argumentTypes;
|
|
|
|
for (auto const& arg: arguments)
|
|
|
|
{
|
|
|
|
arg->accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
argumentTypes.push_back(arg->annotation().type);
|
2015-06-05 22:57:51 +00:00
|
|
|
}
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().fetchFreeMemoryPointer();
|
|
|
|
utils().encodeToMemory(argumentTypes, TypePointers(), function.padArguments(), true);
|
|
|
|
utils().toSizeAfterFreeMemoryPointer();
|
2015-06-05 22:57:51 +00:00
|
|
|
m_context << eth::Instruction::SHA3;
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
2015-02-03 13:02:58 +00:00
|
|
|
}
|
2015-02-09 13:08:48 +00:00
|
|
|
case Location::Log0:
|
|
|
|
case Location::Log1:
|
|
|
|
case Location::Log2:
|
|
|
|
case Location::Log3:
|
|
|
|
case Location::Log4:
|
2015-01-09 14:00:47 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
unsigned logNumber = int(function.location()) - int(Location::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);
|
2015-09-16 14:56:30 +00:00
|
|
|
utils().convertType(*arguments[arg]->annotation().type, *function.parameterTypes()[arg], true);
|
2015-01-09 14:00:47 +00:00
|
|
|
}
|
2015-06-05 22:57:51 +00:00
|
|
|
arguments.front()->accept(*this);
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().fetchFreeMemoryPointer();
|
|
|
|
utils().encodeToMemory(
|
2015-09-16 14:56:30 +00:00
|
|
|
{arguments.front()->annotation().type},
|
2015-08-31 16:44:29 +00:00
|
|
|
{function.parameterTypes().front()},
|
2015-06-05 22:57:51 +00:00
|
|
|
false,
|
|
|
|
true);
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().toSizeAfterFreeMemoryPointer();
|
2015-06-05 22:57:51 +00:00
|
|
|
m_context << eth::logInstruction(logNumber);
|
2015-01-29 15:42:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-02-09 13:08:48 +00:00
|
|
|
case Location::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);
|
2015-11-16 16:09:09 +00:00
|
|
|
if (auto const& arrayType = dynamic_pointer_cast<ArrayType const>(function.parameterTypes()[arg - 1]))
|
|
|
|
{
|
|
|
|
utils().fetchFreeMemoryPointer();
|
|
|
|
utils().encodeToMemory(
|
|
|
|
{arguments[arg - 1]->annotation().type},
|
|
|
|
{arrayType},
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
utils().toSizeAfterFreeMemoryPointer();
|
|
|
|
m_context << eth::Instruction::SHA3;
|
|
|
|
}
|
|
|
|
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())
|
2015-03-16 18:19:34 +00:00
|
|
|
{
|
2015-10-05 15:19:23 +00:00
|
|
|
m_context << u256(h256::Arith(dev::sha3(function.externalSignature())));
|
2015-03-16 18:19:34 +00:00
|
|
|
++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)
|
2015-04-21 18:09:20 +00:00
|
|
|
// Memory position is only a hack and should be removed once we have free memory pointer.
|
2015-06-05 22:57:51 +00:00
|
|
|
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
|
|
|
{
|
2015-06-05 22:57:51 +00:00
|
|
|
arguments[arg]->accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
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
|
|
|
}
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().fetchFreeMemoryPointer();
|
|
|
|
utils().encodeToMemory(nonIndexedArgTypes, nonIndexedParamTypes);
|
2015-06-05 22:57:51 +00:00
|
|
|
// need: topic1 ... topicn memsize memstart
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().toSizeAfterFreeMemoryPointer();
|
2015-06-05 22:57:51 +00:00
|
|
|
m_context << eth::logInstruction(numIndexed);
|
2015-01-08 23:22:06 +00:00
|
|
|
break;
|
2015-01-09 14:00:47 +00:00
|
|
|
}
|
2015-02-09 13:08:48 +00:00
|
|
|
case Location::BlockHash:
|
2015-01-15 18:59:35 +00:00
|
|
|
{
|
|
|
|
arguments[0]->accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
utils().convertType(*arguments[0]->annotation().type, *function.parameterTypes()[0], true);
|
2015-01-15 18:59:35 +00:00
|
|
|
m_context << eth::Instruction::BLOCKHASH;
|
|
|
|
break;
|
|
|
|
}
|
2015-11-18 16:12:39 +00:00
|
|
|
case Location::AddMod:
|
|
|
|
case Location::MulMod:
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < 3; i ++)
|
|
|
|
{
|
|
|
|
arguments[2 - i]->accept(*this);
|
|
|
|
utils().convertType(*arguments[2 - i]->annotation().type, IntegerType(256));
|
|
|
|
}
|
|
|
|
if (function.location() == Location::AddMod)
|
|
|
|
m_context << eth::Instruction::ADDMOD;
|
|
|
|
else
|
|
|
|
m_context << eth::Instruction::MULMOD;
|
|
|
|
break;
|
|
|
|
}
|
2015-02-09 13:08:48 +00:00
|
|
|
case Location::ECRecover:
|
2014-12-04 18:38:24 +00:00
|
|
|
case Location::SHA256:
|
|
|
|
case Location::RIPEMD160:
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
_functionCall.expression().accept(*this);
|
2015-02-09 13:08:48 +00:00
|
|
|
static const map<Location, u256> contractAddresses{{Location::ECRecover, 1},
|
2014-12-04 18:38:24 +00:00
|
|
|
{Location::SHA256, 2},
|
|
|
|
{Location::RIPEMD160, 3}};
|
2015-08-31 16:44:29 +00:00
|
|
|
m_context << contractAddresses.find(function.location())->second;
|
|
|
|
for (unsigned i = function.sizeOnStack(); i > 0; --i)
|
2015-03-13 12:14:51 +00:00
|
|
|
m_context << eth::swapInstruction(i);
|
2015-05-15 16:02:09 +00:00
|
|
|
appendExternalFunctionCall(function, arguments);
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-10-15 11:54:59 +00:00
|
|
|
case Location::ByteArrayPush:
|
2015-10-13 14:55:59 +00:00
|
|
|
case Location::ArrayPush:
|
|
|
|
{
|
2015-10-15 11:54:59 +00:00
|
|
|
_functionCall.expression().accept(*this);
|
2015-10-13 14:55:59 +00:00
|
|
|
solAssert(function.parameterTypes().size() == 1, "");
|
|
|
|
solAssert(!!function.parameterTypes()[0], "");
|
2015-11-25 13:23:35 +00:00
|
|
|
TypePointer paramType = function.parameterTypes()[0];
|
2015-10-15 11:54:59 +00:00
|
|
|
shared_ptr<ArrayType> arrayType =
|
|
|
|
function.location() == Location::ArrayPush ?
|
|
|
|
make_shared<ArrayType>(DataLocation::Storage, paramType) :
|
|
|
|
make_shared<ArrayType>(DataLocation::Storage);
|
2015-10-13 14:55:59 +00:00
|
|
|
// get the current length
|
2015-10-15 11:54:59 +00:00
|
|
|
ArrayUtils(m_context).retrieveLength(*arrayType);
|
2015-10-13 14:55:59 +00:00
|
|
|
m_context << eth::Instruction::DUP1;
|
|
|
|
// stack: ArrayReference currentLength currentLength
|
|
|
|
m_context << u256(1) << eth::Instruction::ADD;
|
|
|
|
// stack: ArrayReference currentLength newLength
|
|
|
|
m_context << eth::Instruction::DUP3 << eth::Instruction::DUP2;
|
2015-10-15 11:54:59 +00:00
|
|
|
ArrayUtils(m_context).resizeDynamicArray(*arrayType);
|
2015-10-13 14:55:59 +00:00
|
|
|
m_context << eth::Instruction::SWAP2 << eth::Instruction::SWAP1;
|
|
|
|
// stack: newLength ArrayReference oldLength
|
2015-10-15 11:54:59 +00:00
|
|
|
ArrayUtils(m_context).accessIndex(*arrayType, false);
|
2015-10-13 14:55:59 +00:00
|
|
|
|
|
|
|
// stack: newLength storageSlot slotOffset
|
|
|
|
arguments[0]->accept(*this);
|
|
|
|
// stack: newLength storageSlot slotOffset argValue
|
2015-10-15 16:14:14 +00:00
|
|
|
TypePointer type = arguments[0]->annotation().type->closestTemporaryType(arrayType->baseType());
|
|
|
|
utils().convertType(*arguments[0]->annotation().type, *type);
|
2015-10-13 14:55:59 +00:00
|
|
|
utils().moveToStackTop(1 + type->sizeOnStack());
|
|
|
|
utils().moveToStackTop(1 + type->sizeOnStack());
|
|
|
|
// stack: newLength argValue storageSlot slotOffset
|
2015-10-15 12:37:11 +00:00
|
|
|
if (function.location() == Location::ArrayPush)
|
|
|
|
StorageItem(m_context, *paramType).storeValue(*type, _functionCall.location(), true);
|
|
|
|
else
|
|
|
|
StorageByteArrayElement(m_context).storeValue(*type, _functionCall.location(), true);
|
2015-10-13 14:55:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-11-17 14:15:00 +00:00
|
|
|
case Location::ObjectCreation:
|
|
|
|
{
|
|
|
|
// 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 << eth::Instruction::DUP1 << eth::Instruction::MSIZE;
|
|
|
|
m_context << eth::Instruction::LT;
|
|
|
|
auto initialise = m_context.appendConditionalJump();
|
|
|
|
// Free memory pointer does not point to empty memory, use MSIZE.
|
|
|
|
m_context << eth::Instruction::POP;
|
|
|
|
m_context << eth::Instruction::MSIZE;
|
|
|
|
m_context << initialise;
|
|
|
|
|
|
|
|
// Stack: requested_length memptr
|
|
|
|
m_context << eth::Instruction::SWAP1;
|
|
|
|
// Stack: memptr requested_length
|
|
|
|
// store length
|
|
|
|
m_context << eth::Instruction::DUP1 << eth::Instruction::DUP3 << eth::Instruction::MSTORE;
|
|
|
|
// Stack: memptr requested_length
|
|
|
|
// update free memory pointer
|
|
|
|
m_context << eth::Instruction::DUP1 << arrayType.baseType()->memoryHeadSize();
|
|
|
|
m_context << eth::Instruction::MUL << u256(32) << eth::Instruction::ADD;
|
|
|
|
m_context << eth::Instruction::DUP3 << eth::Instruction::ADD;
|
|
|
|
utils().storeFreeMemoryPointer();
|
|
|
|
// Stack: memptr requested_length
|
|
|
|
|
|
|
|
// 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 << eth::Instruction::DUP2 << u256(32) << eth::Instruction::ADD;
|
|
|
|
utils().zeroInitialiseMemoryArray(arrayType);
|
|
|
|
m_context << eth::Instruction::POP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_context << eth::Instruction::POP;
|
|
|
|
break;
|
|
|
|
}
|
2014-12-04 18:38:24 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-06 00:06:24 +00:00
|
|
|
void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _memberAccess);
|
2015-11-27 13:08:09 +00:00
|
|
|
|
|
|
|
// Check whether the member is a bound function.
|
2015-08-31 16:44:29 +00:00
|
|
|
ASTString const& member = _memberAccess.memberName();
|
2015-11-27 13:08:09 +00:00
|
|
|
if (auto funType = dynamic_cast<FunctionType const*>(_memberAccess.annotation().type.get()))
|
|
|
|
if (funType->bound())
|
|
|
|
{
|
|
|
|
utils().convertType(
|
|
|
|
*_memberAccess.expression().annotation().type,
|
|
|
|
*funType->selfType(),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
auto contract = dynamic_cast<ContractDefinition const*>(funType->declaration().scope());
|
|
|
|
solAssert(contract && contract->isLibrary(), "");
|
|
|
|
//@TODO library name might not be unique
|
|
|
|
m_context.appendLibraryAddress(contract->name());
|
|
|
|
m_context << funType->externalIdentifier();
|
|
|
|
utils().moveIntoStack(funType->selfType()->sizeOnStack(), 2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
switch (_memberAccess.expression().annotation().type->category())
|
2014-11-21 18:14:56 +00:00
|
|
|
{
|
2015-02-09 13:00:12 +00:00
|
|
|
case Type::Category::Contract:
|
2015-01-07 21:54:56 +00:00
|
|
|
{
|
2015-01-27 15:42:28 +00:00
|
|
|
bool alsoSearchInteger = false;
|
2015-09-16 14:56:30 +00:00
|
|
|
ContractType const& type = dynamic_cast<ContractType const&>(*_memberAccess.expression().annotation().type);
|
2015-01-27 13:32:59 +00:00
|
|
|
if (type.isSuper())
|
2015-04-15 15:40:50 +00:00
|
|
|
{
|
2015-09-16 14:56:30 +00:00
|
|
|
solAssert(!!_memberAccess.annotation().referencedDeclaration, "Referenced declaration not resolved.");
|
2015-08-31 16:44:29 +00:00
|
|
|
m_context << m_context.superFunctionEntryLabel(
|
2015-09-16 14:56:30 +00:00
|
|
|
dynamic_cast<FunctionDefinition const&>(*_memberAccess.annotation().referencedDeclaration),
|
2015-08-31 16:44:29 +00:00
|
|
|
type.contractDefinition()
|
2015-04-15 15:40:50 +00:00
|
|
|
).pushTag();
|
|
|
|
}
|
2015-01-27 13:32:59 +00:00
|
|
|
else
|
|
|
|
{
|
2015-01-27 15:42:28 +00:00
|
|
|
// ordinary contract type
|
2015-09-16 14:56:30 +00:00
|
|
|
if (Declaration const* declaration = _memberAccess.annotation().referencedDeclaration)
|
2015-01-27 13:32:59 +00:00
|
|
|
{
|
2015-04-15 15:40:50 +00:00
|
|
|
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.");
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().convertType(type, IntegerType(0, IntegerType::Modifier::Address), true);
|
2015-01-27 13:32:59 +00:00
|
|
|
m_context << identifier;
|
|
|
|
}
|
2015-01-27 15:42:28 +00:00
|
|
|
else
|
|
|
|
// not found in contract, search in members inherited from address
|
|
|
|
alsoSearchInteger = true;
|
2015-01-27 13:32:59 +00:00
|
|
|
}
|
2015-01-27 15:42:28 +00:00
|
|
|
if (!alsoSearchInteger)
|
|
|
|
break;
|
2015-01-07 21:54:56 +00:00
|
|
|
}
|
2015-02-09 13:00:12 +00:00
|
|
|
case Type::Category::Integer:
|
2014-11-25 17:23:39 +00:00
|
|
|
if (member == "balance")
|
2014-11-26 12:19:17 +00:00
|
|
|
{
|
2015-06-16 09:28:35 +00:00
|
|
|
utils().convertType(
|
2015-09-16 14:56:30 +00:00
|
|
|
*_memberAccess.expression().annotation().type,
|
2015-06-16 09:28:35 +00:00
|
|
|
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
|
|
|
}
|
2015-05-15 16:02:09 +00:00
|
|
|
else if ((set<string>{"send", "call", "callcode"}).count(member))
|
2015-06-16 09:28:35 +00:00
|
|
|
utils().convertType(
|
2015-09-16 14:56:30 +00:00
|
|
|
*_memberAccess.expression().annotation().type,
|
2015-06-16 09:28:35 +00:00
|
|
|
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;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Type::Category::Function:
|
2015-09-16 14:56:30 +00:00
|
|
|
solAssert(!!_memberAccess.expression().annotation().type->memberType(member),
|
2015-01-12 11:47:37 +00:00
|
|
|
"Invalid member access to function.");
|
|
|
|
break;
|
2015-02-09 13:00:12 +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-19 14:34:15 +00:00
|
|
|
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;
|
2015-02-10 13:57:01 +00:00
|
|
|
else if (member == "data")
|
2015-02-16 16:33:13 +00:00
|
|
|
m_context << u256(0) << eth::Instruction::CALLDATASIZE;
|
2015-03-13 16:36:00 +00:00
|
|
|
else if (member == "sig")
|
2015-03-16 14:46:04 +00:00
|
|
|
m_context << u256(0) << eth::Instruction::CALLDATALOAD
|
|
|
|
<< (u256(0xffffffff) << (256 - 32)) << eth::Instruction::AND;
|
2014-11-24 12:23:58 +00:00
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown magic member."));
|
2014-11-21 18:14:56 +00:00
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Type::Category::Struct:
|
2014-11-21 18:14:56 +00:00
|
|
|
{
|
2015-09-16 14:56:30 +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);
|
2015-06-26 16:35:43 +00:00
|
|
|
m_context << offsets.first << eth::Instruction::ADD << u256(offsets.second);
|
|
|
|
setLValueToStorageItem(_memberAccess);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DataLocation::Memory:
|
|
|
|
{
|
2015-06-30 09:54:51 +00:00
|
|
|
m_context << type.memoryOffsetOfMember(member) << eth::Instruction::ADD;
|
2015-09-16 14:56:30 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-02-12 16:59:52 +00:00
|
|
|
case Type::Category::Enum:
|
|
|
|
{
|
2015-09-16 14:56:30 +00:00
|
|
|
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());
|
2015-02-13 12:32:18 +00:00
|
|
|
break;
|
2015-02-12 16:59:52 +00:00
|
|
|
}
|
2015-02-10 08:52:19 +00:00
|
|
|
case Type::Category::TypeType:
|
2015-01-19 18:18:34 +00:00
|
|
|
{
|
2015-09-16 14:56:30 +00:00
|
|
|
TypeType const& type = dynamic_cast<TypeType const&>(*_memberAccess.expression().annotation().type);
|
2015-02-13 21:52:04 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
if (dynamic_cast<ContractType const*>(type.actualType().get()))
|
2015-01-19 18:18:34 +00:00
|
|
|
{
|
2015-12-18 14:56:26 +00:00
|
|
|
if (auto funType = dynamic_cast<FunctionType const*>(_memberAccess.annotation().type.get()))
|
2015-09-10 17:40:07 +00:00
|
|
|
{
|
2015-12-18 14:56:26 +00:00
|
|
|
if (funType->location() != FunctionType::Location::Internal)
|
|
|
|
m_context << funType->externalIdentifier();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto const* function = dynamic_cast<FunctionDefinition const*>(_memberAccess.annotation().referencedDeclaration);
|
|
|
|
solAssert(!!function, "Function not found in member access");
|
|
|
|
m_context << m_context.functionEntryLabel(*function).pushTag();
|
|
|
|
}
|
2015-09-10 17:40:07 +00:00
|
|
|
}
|
2015-01-19 18:18:34 +00:00
|
|
|
}
|
2015-08-31 16:44:29 +00:00
|
|
|
else if (auto enumType = dynamic_cast<EnumType const*>(type.actualType().get()))
|
|
|
|
m_context << enumType->memberValue(_memberAccess.memberName());
|
2015-02-14 02:06:50 +00:00
|
|
|
break;
|
2015-01-19 18:18:34 +00:00
|
|
|
}
|
2015-02-20 14:52:30 +00:00
|
|
|
case Type::Category::Array:
|
2015-02-12 14:44:35 +00:00
|
|
|
{
|
2015-09-16 14:56:30 +00:00
|
|
|
auto const& type = dynamic_cast<ArrayType const&>(*_memberAccess.expression().annotation().type);
|
2015-10-12 08:11:58 +00:00
|
|
|
if (member == "length")
|
2015-02-16 16:33:13 +00:00
|
|
|
{
|
2015-10-12 08:11:58 +00:00
|
|
|
if (!type.isDynamicallySized())
|
|
|
|
{
|
|
|
|
utils().popStackElement(type);
|
|
|
|
m_context << type.length();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
switch (type.location())
|
|
|
|
{
|
|
|
|
case DataLocation::CallData:
|
|
|
|
m_context << eth::Instruction::SWAP1 << eth::Instruction::POP;
|
|
|
|
break;
|
|
|
|
case DataLocation::Storage:
|
|
|
|
setLValue<StorageArrayLength>(_memberAccess, type);
|
|
|
|
break;
|
|
|
|
case DataLocation::Memory:
|
|
|
|
m_context << eth::Instruction::MLOAD;
|
|
|
|
break;
|
|
|
|
}
|
2015-02-16 16:33:13 +00:00
|
|
|
}
|
2015-10-13 14:55:59 +00:00
|
|
|
else if (member == "push")
|
2015-10-12 08:11:58 +00:00
|
|
|
{
|
2015-10-13 14:55:59 +00:00
|
|
|
solAssert(
|
|
|
|
type.isDynamicallySized() && type.location() == DataLocation::Storage,
|
|
|
|
"Tried to use .push() on a non-dynamically sized array"
|
|
|
|
);
|
2015-10-12 08:11:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
solAssert(false, "Illegal array member.");
|
2015-02-12 14:44:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-11-21 18:14:56 +00:00
|
|
|
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
|
|
|
{
|
2015-03-07 22:08:39 +00:00
|
|
|
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
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
Type const& baseType = *_indexAccess.baseExpression().annotation().type;
|
2015-09-09 15:35:27 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
if (baseType.category() == Type::Category::Mapping)
|
2015-02-22 18:15:41 +00:00
|
|
|
{
|
2015-03-11 17:09:35 +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!
|
2015-08-04 14:58:31 +00:00
|
|
|
utils().encodeToMemory(
|
2015-09-16 14:56:30 +00:00
|
|
|
TypePointers{_indexAccess.indexExpression()->annotation().type},
|
2015-08-04 14:58:31 +00:00
|
|
|
TypePointers{keyType},
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
2015-08-03 16:09:39 +00:00
|
|
|
m_context << eth::Instruction::SWAP1;
|
|
|
|
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());
|
2015-08-03 16:09:39 +00:00
|
|
|
m_context << eth::Instruction::SWAP1;
|
|
|
|
solAssert(CompilerUtils::freeMemoryPointer >= 0x40, "");
|
|
|
|
utils().storeInMemoryDynamic(IntegerType(256));
|
|
|
|
m_context << u256(0);
|
|
|
|
}
|
|
|
|
m_context << eth::Instruction::SHA3;
|
2015-03-11 17:09:35 +00:00
|
|
|
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);
|
2015-03-30 17:31:57 +00:00
|
|
|
// stack layout: <base_ref> [<length>] <index>
|
|
|
|
ArrayUtils(m_context).accessIndex(arrayType);
|
2015-06-25 15:51:01 +00:00
|
|
|
switch (arrayType.location())
|
2015-02-23 17:21:17 +00:00
|
|
|
{
|
2015-06-25 15:51:01 +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);
|
2015-06-25 15:51:01 +00:00
|
|
|
break;
|
|
|
|
case DataLocation::Memory:
|
2015-09-16 14:56:30 +00:00
|
|
|
setLValue<MemoryItem>(_indexAccess, *_indexAccess.annotation().type, !arrayType.isByteArray());
|
2015-06-25 15:51:01 +00:00
|
|
|
break;
|
|
|
|
case DataLocation::CallData:
|
|
|
|
//@todo if we implement this, the value in calldata has to be added to the base offset
|
2015-08-31 16:44:29 +00:00
|
|
|
solAssert(!arrayType.baseType()->isDynamicallySized(), "Nested arrays not yet implemented.");
|
|
|
|
if (arrayType.baseType()->isValueType())
|
2015-06-25 15:51:01 +00:00
|
|
|
CompilerUtils(m_context).loadFromMemoryDynamic(
|
2015-08-31 16:44:29 +00:00
|
|
|
*arrayType.baseType(),
|
2015-06-25 15:51:01 +00:00
|
|
|
true,
|
|
|
|
!arrayType.isByteArray(),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
break;
|
2015-02-23 17:21:17 +00:00
|
|
|
}
|
2015-02-22 18:15:41 +00:00
|
|
|
}
|
2015-10-16 12:34:43 +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-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
|
|
|
{
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _identifier);
|
2015-09-16 14:56:30 +00:00
|
|
|
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())
|
2015-03-09 18:22:43 +00:00
|
|
|
{
|
|
|
|
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())
|
2015-01-27 13:32:59 +00:00
|
|
|
m_context << eth::Instruction::ADDRESS;
|
2015-03-09 18:22:43 +00:00
|
|
|
break;
|
|
|
|
case Type::Category::Integer:
|
|
|
|
// "now"
|
|
|
|
m_context << eth::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))
|
2015-08-31 16:44:29 +00:00
|
|
|
m_context << m_context.virtualFunctionEntryLabel(*functionDef).pushTag();
|
2015-03-03 11:58:01 +00:00
|
|
|
else if (auto variable = dynamic_cast<VariableDeclaration const*>(declaration))
|
|
|
|
{
|
|
|
|
if (!variable->isConstant())
|
|
|
|
setLValueFromDeclaration(*declaration, _identifier);
|
2015-03-12 13:15:04 +00:00
|
|
|
else
|
2015-09-08 10:57:27 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
variable->value()->accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
utils().convertType(*variable->value()->annotation().type, *variable->annotation().type);
|
2015-09-08 10:57:27 +00:00
|
|
|
}
|
2015-03-03 11:58:01 +00:00
|
|
|
}
|
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())
|
|
|
|
//@todo name should be unique, change once we have module management
|
|
|
|
m_context.appendLibraryAddress(contract->name());
|
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
|
|
|
|
}
|
2015-02-12 16:59:52 +00:00
|
|
|
else if (dynamic_cast<EnumDefinition const*>(declaration))
|
|
|
|
{
|
|
|
|
// no-op
|
|
|
|
}
|
2015-10-16 12:34:43 +00:00
|
|
|
else if (dynamic_cast<StructDefinition const*>(declaration))
|
|
|
|
{
|
|
|
|
// no-op
|
|
|
|
}
|
2015-01-19 18:18:34 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _literal);
|
2015-09-16 14:56:30 +00:00
|
|
|
TypePointer type = _literal.annotation().type;
|
2015-08-31 16:44:29 +00:00
|
|
|
switch (type->category())
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2015-02-09 13:00:12 +00:00
|
|
|
case Type::Category::IntegerConstant:
|
|
|
|
case Type::Category::Bool:
|
2015-07-07 23:13:56 +00:00
|
|
|
m_context << type->literalValue(&_literal);
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-07-07 23:13:56 +00:00
|
|
|
case Type::Category::StringLiteral:
|
|
|
|
break; // will be done during conversion
|
2014-10-30 00:20:32 +00:00
|
|
|
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
|
|
|
{
|
2015-01-09 20:36:25 +00:00
|
|
|
Token::Value const c_op = _binaryOperation.getOperator();
|
2015-02-09 13:00:12 +00:00
|
|
|
solAssert(c_op == Token::Or || c_op == Token::And, "");
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
_binaryOperation.leftExpression().accept(*this);
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context << eth::Instruction::DUP1;
|
2015-02-09 13:00:12 +00:00
|
|
|
if (c_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;
|
2015-08-31 16:44:29 +00:00
|
|
|
_binaryOperation.rightExpression().accept(*this);
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context << endLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionCompiler::appendCompareOperatorCode(Token::Value _operator, Type const& _type)
|
|
|
|
{
|
2015-02-10 08:52:19 +00:00
|
|
|
if (_operator == Token::Equal || _operator == Token::NotEqual)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
|
|
|
m_context << eth::Instruction::EQ;
|
2015-02-10 08:52:19 +00:00
|
|
|
if (_operator == Token::NotEqual)
|
2014-10-31 16:20:27 +00:00
|
|
|
m_context << eth::Instruction::ISZERO;
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-14 14:37:11 +00:00
|
|
|
bool isSigned = false;
|
|
|
|
if (auto type = dynamic_cast<IntegerType const*>(&_type))
|
|
|
|
isSigned = type->isSigned();
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
switch (_operator)
|
|
|
|
{
|
2015-02-10 08:52:19 +00:00
|
|
|
case Token::GreaterThanOrEqual:
|
2015-07-14 14:37:11 +00:00
|
|
|
m_context <<
|
|
|
|
(isSigned ? eth::Instruction::SLT : eth::Instruction::LT) <<
|
|
|
|
eth::Instruction::ISZERO;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-02-10 08:52:19 +00:00
|
|
|
case Token::LessThanOrEqual:
|
2015-07-14 14:37:11 +00:00
|
|
|
m_context <<
|
|
|
|
(isSigned ? eth::Instruction::SGT : eth::Instruction::GT) <<
|
|
|
|
eth::Instruction::ISZERO;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::GreaterThan:
|
2015-07-14 14:37:11 +00:00
|
|
|
m_context << (isSigned ? eth::Instruction::SGT : eth::Instruction::GT);
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::LessThan:
|
2015-07-14 14:37:11 +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);
|
2015-01-09 20:36:25 +00:00
|
|
|
bool const c_isSigned = type.isSigned();
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
switch (_operator)
|
|
|
|
{
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::Add:
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context << eth::Instruction::ADD;
|
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
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;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::Mul:
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context << eth::Instruction::MUL;
|
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::Div:
|
2015-01-09 20:36:25 +00:00
|
|
|
m_context << (c_isSigned ? eth::Instruction::SDIV : eth::Instruction::DIV);
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::Mod:
|
2015-01-09 20:36:25 +00:00
|
|
|
m_context << (c_isSigned ? eth::Instruction::SMOD : eth::Instruction::MOD);
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-02-08 11:23:17 +00:00
|
|
|
case Token::Exp:
|
|
|
|
m_context << eth::Instruction::EXP;
|
|
|
|
break;
|
2014-10-30 00:20:32 +00:00
|
|
|
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)
|
|
|
|
{
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::BitOr:
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context << eth::Instruction::OR;
|
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::BitAnd:
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context << eth::Instruction::AND;
|
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::BitXor:
|
2014-10-30 00:20:32 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 16:02:09 +00:00
|
|
|
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
|
|
|
|
2015-01-12 11:47:37 +00:00
|
|
|
// Assumed stack content here:
|
|
|
|
// <stack top>
|
|
|
|
// value [if _functionType.valueSet()]
|
|
|
|
// gas [if _functionType.gasSet()]
|
2015-11-27 13:08:09 +00:00
|
|
|
// self object [if bound - moved to top right away]
|
2015-01-14 10:57:22 +00:00
|
|
|
// function identifier [unless bare]
|
2015-01-12 11:47:37 +00:00
|
|
|
// contract address
|
2015-01-08 16:18:31 +00:00
|
|
|
|
2015-11-27 13:08:09 +00:00
|
|
|
unsigned selfSize = _functionType.bound() ? _functionType.selfType()->sizeOnStack() : 0;
|
2015-01-12 11:47:37 +00:00
|
|
|
unsigned gasValueSize = (_functionType.gasSet() ? 1 : 0) + (_functionType.valueSet() ? 1 : 0);
|
2015-11-27 13:08:09 +00:00
|
|
|
unsigned contractStackPos = m_context.currentToBaseStackOffset(1 + gasValueSize + selfSize + (_functionType.isBareCall() ? 0 : 1));
|
2015-01-14 10:57:22 +00:00
|
|
|
unsigned gasStackPos = m_context.currentToBaseStackOffset(gasValueSize);
|
|
|
|
unsigned valueStackPos = m_context.currentToBaseStackOffset(1);
|
|
|
|
|
2015-11-27 13:08:09 +00:00
|
|
|
// move self object to top
|
|
|
|
if (_functionType.bound())
|
|
|
|
utils().moveToStackTop(gasValueSize, _functionType.selfType()->sizeOnStack());
|
|
|
|
|
2015-06-05 22:57:51 +00:00
|
|
|
using FunctionKind = FunctionType::Location;
|
2015-08-31 16:44:29 +00:00
|
|
|
FunctionKind funKind = _functionType.location();
|
2015-06-05 22:57:51 +00:00
|
|
|
bool returnSuccessCondition = funKind == FunctionKind::Bare || funKind == FunctionKind::BareCallCode;
|
2015-07-28 11:37:38 +00:00
|
|
|
bool isCallCode = funKind == FunctionKind::BareCallCode || funKind == FunctionKind::CallCode;
|
2015-06-05 15:32:13 +00:00
|
|
|
|
2015-06-22 16:05:13 +00:00
|
|
|
unsigned retSize = 0;
|
2015-06-05 12:34:10 +00:00
|
|
|
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())
|
|
|
|
{
|
2015-11-24 13:54:37 +00:00
|
|
|
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
|
|
|
|
2015-06-05 22:57:51 +00:00
|
|
|
// Evaluate arguments.
|
|
|
|
TypePointers argumentTypes;
|
2015-11-27 13:08:09 +00:00
|
|
|
TypePointers parameterTypes = _functionType.parameterTypes();
|
2015-06-05 22:57:51 +00:00
|
|
|
bool manualFunctionId =
|
|
|
|
(funKind == FunctionKind::Bare || funKind == FunctionKind::BareCallCode) &&
|
|
|
|
!_arguments.empty() &&
|
2015-09-16 14:56:30 +00:00
|
|
|
_arguments.front()->annotation().type->mobileType()->calldataEncodedSize(false) ==
|
2015-06-05 22:57:51 +00:00
|
|
|
CompilerUtils::dataStartOffset;
|
|
|
|
if (manualFunctionId)
|
2014-12-10 22:01:40 +00:00
|
|
|
{
|
2015-06-05 22:57:51 +00:00
|
|
|
// If we have a BareCall or BareCallCode and the first type has exactly 4 bytes, use it as
|
|
|
|
// function identifier.
|
|
|
|
_arguments.front()->accept(*this);
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().convertType(
|
2015-09-16 14:56:30 +00:00
|
|
|
*_arguments.front()->annotation().type,
|
2015-06-05 22:57:51 +00:00
|
|
|
IntegerType(8 * CompilerUtils::dataStartOffset),
|
|
|
|
true
|
2015-06-05 15:32:13 +00:00
|
|
|
);
|
2015-06-05 22:57:51 +00:00
|
|
|
for (unsigned i = 0; i < gasValueSize; ++i)
|
|
|
|
m_context << eth::swapInstruction(gasValueSize - i);
|
|
|
|
gasStackPos++;
|
|
|
|
valueStackPos++;
|
|
|
|
}
|
2015-11-27 13:08:09 +00:00
|
|
|
if (_functionType.bound())
|
|
|
|
{
|
|
|
|
argumentTypes.push_back(_functionType.selfType());
|
|
|
|
parameterTypes.insert(parameterTypes.begin(), _functionType.selfType());
|
|
|
|
}
|
2015-06-05 22:57:51 +00:00
|
|
|
for (size_t i = manualFunctionId ? 1 : 0; i < _arguments.size(); ++i)
|
|
|
|
{
|
|
|
|
_arguments[i]->accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
argumentTypes.push_back(_arguments[i]->annotation().type);
|
2014-12-10 22:01:40 +00:00
|
|
|
}
|
2015-01-08 16:18:31 +00:00
|
|
|
|
2015-06-05 22:57:51 +00:00
|
|
|
// Copy function identifier to memory.
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().fetchFreeMemoryPointer();
|
2015-06-05 22:57:51 +00:00
|
|
|
if (!_functionType.isBareCall() || manualFunctionId)
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
m_context << eth::dupInstruction(2 + gasValueSize + CompilerUtils::sizeOnStack(argumentTypes));
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().storeInMemoryDynamic(IntegerType(8 * CompilerUtils::dataStartOffset), false);
|
2015-06-05 22:57:51 +00:00
|
|
|
}
|
2015-04-21 08:59:48 +00:00
|
|
|
// If the function takes arbitrary parameters, copy dynamic length data in place.
|
2015-06-05 22:57:51 +00:00
|
|
|
// Move argumenst to memory, will not update the free memory pointer (but will update the memory
|
|
|
|
// pointer on the stack).
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().encodeToMemory(
|
2015-06-05 22:57:51 +00:00
|
|
|
argumentTypes,
|
2015-11-27 13:08:09 +00:00
|
|
|
parameterTypes,
|
2015-04-21 08:59:48 +00:00
|
|
|
_functionType.padArguments(),
|
2015-10-04 09:05:57 +00:00
|
|
|
_functionType.takesArbitraryParameters(),
|
|
|
|
isCallCode
|
2015-04-21 08:59:48 +00:00
|
|
|
);
|
2015-06-05 22:57:51 +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.
|
|
|
|
// put on stack: <size of output> <memory pos of output> <size of input> <memory pos of input>
|
|
|
|
m_context << u256(retSize);
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().fetchFreeMemoryPointer();
|
2015-06-05 22:57:51 +00:00
|
|
|
m_context << eth::Instruction::DUP1 << eth::Instruction::DUP4 << eth::Instruction::SUB;
|
|
|
|
m_context << eth::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)
|
2015-01-12 11:47:37 +00:00
|
|
|
if (_functionType.valueSet())
|
2015-01-14 10:57:22 +00:00
|
|
|
m_context << eth::dupInstruction(m_context.baseToCurrentStackOffset(valueStackPos));
|
2014-12-10 22:01:40 +00:00
|
|
|
else
|
|
|
|
m_context << u256(0);
|
2015-01-14 10:57:22 +00:00
|
|
|
m_context << eth::dupInstruction(m_context.baseToCurrentStackOffset(contractStackPos));
|
2015-01-08 16:18:31 +00:00
|
|
|
|
2015-01-12 11:47:37 +00:00
|
|
|
if (_functionType.gasSet())
|
2015-01-14 10:57:22 +00:00
|
|
|
m_context << eth::dupInstruction(m_context.baseToCurrentStackOffset(gasStackPos));
|
2015-01-12 11:47:37 +00:00
|
|
|
else
|
2015-07-28 11:37:38 +00:00
|
|
|
{
|
2015-11-27 13:08:09 +00:00
|
|
|
eth::EVMSchedule schedule;// TODO: Make relevant to current suppose context.
|
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.
|
2015-11-21 13:34:21 +00:00
|
|
|
u256 gasNeededByCaller = schedule.callGas + 10;
|
2015-07-28 11:37:38 +00:00
|
|
|
if (_functionType.valueSet())
|
2015-11-21 13:34:21 +00:00
|
|
|
gasNeededByCaller += schedule.callValueTransferGas;
|
2015-07-28 11:37:38 +00:00
|
|
|
if (!isCallCode)
|
2015-11-21 13:34:21 +00:00
|
|
|
gasNeededByCaller += schedule.callNewAccountGas; // we never know
|
2015-06-03 16:06:49 +00:00
|
|
|
m_context <<
|
2015-07-28 11:37:38 +00:00
|
|
|
gasNeededByCaller <<
|
2015-06-03 16:06:49 +00:00
|
|
|
eth::Instruction::GAS <<
|
|
|
|
eth::Instruction::SUB;
|
2015-07-28 11:37:38 +00:00
|
|
|
}
|
|
|
|
if (isCallCode)
|
2015-05-15 16:02:09 +00:00
|
|
|
m_context << eth::Instruction::CALLCODE;
|
|
|
|
else
|
|
|
|
m_context << eth::Instruction::CALL;
|
2015-05-19 10:58:12 +00:00
|
|
|
|
2015-06-05 15:38:06 +00:00
|
|
|
unsigned remainsSize =
|
2015-06-05 22:57:51 +00:00
|
|
|
2 + // contract address, input_memory_end
|
2015-06-05 15:38:06 +00:00
|
|
|
_functionType.valueSet() +
|
|
|
|
_functionType.gasSet() +
|
2015-06-05 22:57:51 +00:00
|
|
|
(!_functionType.isBareCall() || manualFunctionId);
|
2015-05-19 10:58:12 +00:00
|
|
|
|
2015-06-05 12:34:10 +00:00
|
|
|
if (returnSuccessCondition)
|
|
|
|
m_context << eth::swapInstruction(remainsSize);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Propagate error condition (if CALL pushes 0 on stack).
|
|
|
|
m_context << eth::Instruction::ISZERO;
|
|
|
|
m_context.appendConditionalJumpTo(m_context.errorTag());
|
|
|
|
}
|
2015-01-08 16:18:31 +00:00
|
|
|
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().popStackSlots(remainsSize);
|
2015-06-05 12:34:10 +00:00
|
|
|
|
|
|
|
if (returnSuccessCondition)
|
|
|
|
{
|
|
|
|
// already there
|
|
|
|
}
|
2015-06-05 22:57:51 +00:00
|
|
|
else if (funKind == FunctionKind::RIPEMD160)
|
2015-04-20 12:09:41 +00:00
|
|
|
{
|
|
|
|
// fix: built-in contract returns right-aligned data
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().fetchFreeMemoryPointer();
|
|
|
|
utils().loadFromMemoryDynamic(IntegerType(160), false, true, false);
|
|
|
|
utils().convertType(IntegerType(160), FixedBytesType(20));
|
2015-04-20 12:09:41 +00:00
|
|
|
}
|
2015-10-09 18:44:56 +00:00
|
|
|
else if (!_functionType.returnParameterTypes().empty())
|
2015-06-05 15:32:13 +00:00
|
|
|
{
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().fetchFreeMemoryPointer();
|
2015-10-09 18:44:56 +00:00
|
|
|
bool memoryNeeded = false;
|
|
|
|
for (auto const& retType: _functionType.returnParameterTypes())
|
2015-09-23 15:31:37 +00:00
|
|
|
{
|
2015-10-09 18:44:56 +00:00
|
|
|
utils().loadFromMemoryDynamic(*retType, false, true, true);
|
|
|
|
if (dynamic_cast<ReferenceType const*>(retType.get()))
|
|
|
|
memoryNeeded = true;
|
2015-09-23 15:31:37 +00:00
|
|
|
}
|
2015-10-09 18:44:56 +00:00
|
|
|
if (memoryNeeded)
|
|
|
|
utils().storeFreeMemoryPointer();
|
2015-09-23 15:31:37 +00:00
|
|
|
else
|
2015-10-09 18:44:56 +00:00
|
|
|
m_context << eth::Instruction::POP;
|
2015-06-05 22:57:51 +00:00
|
|
|
}
|
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)
|
2015-02-01 01:41:14 +00:00
|
|
|
{
|
2015-06-16 15:20:41 +00:00
|
|
|
solAssert(_expectedType.isValueType(), "Not implemented for non-value types.");
|
2015-02-01 01:41:14 +00:00
|
|
|
_expression.accept(*this);
|
2015-09-16 14:56:30 +00:00
|
|
|
utils().convertType(*_expression.annotation().type, _expectedType, true);
|
2015-06-16 15:20:41 +00:00
|
|
|
utils().storeInMemoryDynamic(_expectedType);
|
2015-02-01 01:41:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 14:14:22 +00:00
|
|
|
void ExpressionCompiler::setLValueFromDeclaration(Declaration const& _declaration, Expression const& _expression)
|
2014-12-08 21:18:19 +00:00
|
|
|
{
|
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));
|
2015-01-14 12:52:03 +00:00
|
|
|
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."));
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 14:14:22 +00:00
|
|
|
void ExpressionCompiler::setLValueToStorageItem(Expression const& _expression)
|
2015-01-26 17:16:47 +00:00
|
|
|
{
|
2015-09-16 14:56:30 +00:00
|
|
|
setLValue<StorageItem>(_expression, *_expression.annotation().type);
|
2014-11-10 16:31:09 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 10:10:41 +00:00
|
|
|
CompilerUtils ExpressionCompiler::utils()
|
|
|
|
{
|
|
|
|
return CompilerUtils(m_context);
|
|
|
|
}
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
}
|