2014-10-30 00:20:32 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2014-10-30 00:20:32 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2014-10-30 00:20:32 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2014-10-30 00:20:32 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2014-10-30 00:20:32 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2014
|
|
|
|
* Solidity AST to EVM bytecode compiler for expressions.
|
|
|
|
*/
|
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/codegen/ExpressionCompiler.h>
|
2018-12-17 16:06:11 +00:00
|
|
|
|
2020-02-10 10:58:36 +00:00
|
|
|
#include <libsolidity/codegen/ReturnInfo.h>
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/codegen/CompilerContext.h>
|
|
|
|
#include <libsolidity/codegen/CompilerUtils.h>
|
|
|
|
#include <libsolidity/codegen/LValue.h>
|
2017-06-21 20:50:57 +00:00
|
|
|
|
2020-02-10 10:58:36 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
|
|
|
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <libevmasm/GasMeter.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Common.h>
|
|
|
|
#include <libsolutil/Keccak256.h>
|
|
|
|
#include <libsolutil/Whiskers.h>
|
2017-07-31 23:55:13 +00:00
|
|
|
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
#include <numeric>
|
|
|
|
#include <utility>
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::evmasm;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::util;
|
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.
|
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
|
|
|
|
{
|
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
|
|
|
}
|
2020-04-02 06:34:20 +00:00
|
|
|
if (_varDecl.immutable())
|
|
|
|
ImmutableItem(m_context, _varDecl).storeValue(*type, _varDecl.location(), true);
|
|
|
|
else
|
|
|
|
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(), "");
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(*_varDecl.value(), *_varDecl.annotation().type);
|
2015-08-21 15:57:57 +00:00
|
|
|
|
|
|
|
// append return
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << dupInstruction(_varDecl.annotation().type->sizeOnStack() + 1);
|
2019-12-11 16:31:36 +00:00
|
|
|
m_context.appendJump(evmasm::AssemblyItem::JumpType::OutOfFunction);
|
2015-08-21 15:57:57 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 14:14:22 +00:00
|
|
|
void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const& _varDecl)
|
2015-02-17 15:21:38 +00:00
|
|
|
{
|
2020-04-02 15:13:31 +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();
|
2020-04-02 15:13:31 +00:00
|
|
|
if (_varDecl.immutable())
|
|
|
|
solAssert(paramTypes.empty(), "");
|
|
|
|
|
2020-06-02 13:42:46 +00:00
|
|
|
m_context.adjustStackOffset(static_cast<int>(1 + CompilerUtils::sizeOnStack(paramTypes)));
|
2015-02-25 14:14:22 +00:00
|
|
|
|
2020-04-02 15:13:31 +00:00
|
|
|
if (!_varDecl.immutable())
|
|
|
|
{
|
|
|
|
// retrieve the position of the variable
|
|
|
|
auto const& location = m_context.storageLocationOfVariable(_varDecl);
|
|
|
|
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
|
|
|
{
|
2019-04-15 13:33:39 +00:00
|
|
|
if (auto mappingType = dynamic_cast<MappingType const*>(returnType))
|
2015-04-01 12:22:42 +00:00
|
|
|
{
|
2015-06-05 22:57:51 +00:00
|
|
|
solAssert(CompilerUtils::freeMemoryPointer >= 0x40, "");
|
2019-02-21 17:46:05 +00:00
|
|
|
|
2015-04-02 15:03:02 +00:00
|
|
|
// pop offset
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2019-02-21 17:46:05 +00:00
|
|
|
if (paramTypes[i]->isDynamicallySized())
|
|
|
|
{
|
|
|
|
solAssert(
|
|
|
|
dynamic_cast<ArrayType const&>(*paramTypes[i]).isByteArray(),
|
|
|
|
"Expected string or byte array for mapping key type"
|
|
|
|
);
|
|
|
|
|
|
|
|
// stack: <keys..> <slot position>
|
|
|
|
|
|
|
|
// copy key[i] to top.
|
|
|
|
utils().copyToStackTop(paramTypes.size() - i + 1, 1);
|
|
|
|
|
|
|
|
m_context.appendInlineAssembly(R"({
|
|
|
|
let key_len := mload(key_ptr)
|
|
|
|
// Temp. use the memory after the array data for the slot
|
|
|
|
// position
|
|
|
|
let post_data_ptr := add(key_ptr, add(key_len, 0x20))
|
|
|
|
let orig_data := mload(post_data_ptr)
|
|
|
|
mstore(post_data_ptr, slot_pos)
|
|
|
|
let hash := keccak256(add(key_ptr, 0x20), add(key_len, 0x20))
|
|
|
|
mstore(post_data_ptr, orig_data)
|
|
|
|
slot_pos := hash
|
|
|
|
})", {"slot_pos", "key_ptr"});
|
|
|
|
|
|
|
|
m_context << Instruction::POP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(paramTypes[i]->isValueType(), "Expected value type for mapping key");
|
|
|
|
|
|
|
|
// move storage offset to memory.
|
|
|
|
utils().storeInMemory(32);
|
|
|
|
|
|
|
|
// move key to memory.
|
|
|
|
utils().copyToStackTop(paramTypes.size() - i, 1);
|
|
|
|
utils().storeInMemory(0);
|
|
|
|
m_context << u256(64) << u256(0);
|
|
|
|
m_context << 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();
|
2015-04-01 12:22:42 +00:00
|
|
|
}
|
2019-04-15 13:33:39 +00:00
|
|
|
else if (auto arrayType = dynamic_cast<ArrayType const*>(returnType))
|
2015-04-01 12:22:42 +00:00
|
|
|
{
|
2015-04-02 15:03:02 +00:00
|
|
|
// pop offset
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << 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)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP2 << Instruction::POP << Instruction::SWAP1;
|
2015-04-02 15:03:02 +00:00
|
|
|
else if (paramTypes.size() >= 2)
|
|
|
|
{
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << swapInstruction(paramTypes.size());
|
|
|
|
m_context << Instruction::POP;
|
|
|
|
m_context << 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;
|
2018-02-20 23:40:38 +00:00
|
|
|
auto returnTypes = accessorType.returnParameterTypes();
|
|
|
|
solAssert(returnTypes.size() >= 1, "");
|
2019-04-15 13:33:39 +00:00
|
|
|
if (StructType const* structType = dynamic_cast<StructType const*>(returnType))
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2020-04-02 15:13:31 +00:00
|
|
|
solAssert(!_varDecl.immutable(), "");
|
2015-04-02 15:03:02 +00:00
|
|
|
// remove offset
|
2016-04-04 11:41:35 +00:00
|
|
|
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)
|
2015-03-11 17:09:35 +00:00
|
|
|
continue;
|
2019-04-15 13:33:39 +00:00
|
|
|
if (auto arrayType = dynamic_cast<ArrayType const*>(returnTypes[i]))
|
2015-06-17 10:01:39 +00:00
|
|
|
if (!arrayType->isByteArray())
|
|
|
|
continue;
|
2015-08-31 16:44:29 +00:00
|
|
|
pair<u256, unsigned> const& offsets = structType->storageOffsetsOfMember(names[i]);
|
2016-04-04 11:41:35 +00:00
|
|
|
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
|
2016-04-04 11:41:35 +00:00
|
|
|
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, "");
|
2020-04-02 15:13:31 +00:00
|
|
|
if (_varDecl.immutable())
|
|
|
|
ImmutableItem(m_context, _varDecl).retrieveValue(SourceLocation());
|
|
|
|
else
|
|
|
|
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), "");
|
2017-07-19 01:26:41 +00:00
|
|
|
if (retSizeOnStack > 15)
|
|
|
|
BOOST_THROW_EXCEPTION(
|
2020-07-01 07:46:06 +00:00
|
|
|
StackTooDeepError() <<
|
2017-07-19 01:26:41 +00:00
|
|
|
errinfo_sourceLocation(_varDecl.location()) <<
|
|
|
|
errinfo_comment("Stack too deep.")
|
|
|
|
);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << dupInstruction(retSizeOnStack + 1);
|
2019-12-11 16:31:36 +00:00
|
|
|
m_context.appendJump(evmasm::AssemblyItem::JumpType::OutOfFunction);
|
2015-02-17 15:21:38 +00:00
|
|
|
}
|
|
|
|
|
2015-12-22 16:50:24 +00:00
|
|
|
bool ExpressionCompiler::visit(Conditional const& _condition)
|
|
|
|
{
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _condition);
|
|
|
|
_condition.condition().accept(*this);
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem trueTag = m_context.appendConditionalJump();
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(_condition.falseExpression(), *_condition.annotation().type);
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem endTag = m_context.appendJumpToNew();
|
2015-12-23 16:09:29 +00:00
|
|
|
m_context << trueTag;
|
2020-06-02 13:42:46 +00:00
|
|
|
int offset = static_cast<int>(_condition.annotation().type->sizeOnStack());
|
2016-03-18 08:22:15 +00:00
|
|
|
m_context.adjustStackOffset(-offset);
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(_condition.trueExpression(), *_condition.annotation().type);
|
2015-12-22 16:50:24 +00:00
|
|
|
m_context << endTag;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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);
|
2018-10-22 14:48:21 +00:00
|
|
|
Token op = _assignment.assignmentOperator();
|
|
|
|
Token binOp = op == Token::Assign ? op : TokenTraits::AssignmentToBinaryOp(op);
|
2016-12-06 22:45:17 +00:00
|
|
|
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.
|
2016-12-06 22:45:17 +00:00
|
|
|
TypePointer rightIntermediateType;
|
2018-10-22 14:48:21 +00:00
|
|
|
if (op != Token::Assign && TokenTraits::isShiftOp(binOp))
|
2016-12-06 22:45:17 +00:00
|
|
|
rightIntermediateType = _assignment.rightHandSide().annotation().type->mobileType();
|
|
|
|
else
|
|
|
|
rightIntermediateType = _assignment.rightHandSide().annotation().type->closestTemporaryType(
|
|
|
|
_assignment.leftHandSide().annotation().type
|
|
|
|
);
|
2017-02-16 10:45:06 +00:00
|
|
|
solAssert(rightIntermediateType, "");
|
2016-12-06 22:45:17 +00:00
|
|
|
utils().convertType(*_assignment.rightHandSide().annotation().type, *rightIntermediateType, cleanupNeeded);
|
2016-01-11 20:25:59 +00:00
|
|
|
|
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
|
|
|
|
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
|
|
|
{
|
2020-07-23 14:16:39 +00:00
|
|
|
solAssert(binOp != Token::Exp, "Compound exp is not possible.");
|
2016-12-06 22:45:17 +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();
|
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);
|
2016-12-06 22:45:17 +00:00
|
|
|
utils().convertType(leftType, leftType, cleanupNeeded);
|
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
if (TokenTraits::isShiftOp(binOp))
|
2016-12-13 15:50:50 +00:00
|
|
|
appendShiftOperatorCode(binOp, leftType, *rightIntermediateType);
|
2016-04-29 23:15:22 +00:00
|
|
|
else
|
2016-12-06 22:45:17 +00:00
|
|
|
{
|
|
|
|
solAssert(leftType == *rightIntermediateType, "");
|
2016-12-13 15:50:50 +00:00
|
|
|
appendOrdinaryBinaryOperatorCode(binOp, leftType);
|
2016-12-06 22:45:17 +00:00
|
|
|
}
|
2015-03-03 16:55:28 +00:00
|
|
|
if (lvalueSize > 0)
|
2015-03-13 12:14:51 +00:00
|
|
|
{
|
2017-01-20 18:01:19 +00:00
|
|
|
if (itemSize + lvalueSize > 16)
|
|
|
|
BOOST_THROW_EXCEPTION(
|
2020-07-01 07:46:06 +00:00
|
|
|
StackTooDeepError() <<
|
2017-01-20 18:01:19 +00:00
|
|
|
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)
|
2016-04-04 11:41:35 +00:00
|
|
|
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();
|
2014-10-30 00:20:32 +00:00
|
|
|
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);
|
2018-09-03 13:01:15 +00:00
|
|
|
|
2016-01-11 20:25:59 +00:00
|
|
|
solAssert(!arrayType.isDynamicallySized(), "Cannot create dynamically sized inline array.");
|
2019-07-09 14:10:38 +00:00
|
|
|
utils().allocateMemory(max(u256(32u), arrayType.memoryDataSize()));
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1;
|
2018-09-03 13:01:15 +00:00
|
|
|
|
2016-01-11 20:25:59 +00:00
|
|
|
for (auto const& component: _tuple.components())
|
2016-01-11 03:49:36 +00:00
|
|
|
{
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(*component, *arrayType.baseType(), true);
|
2018-09-03 13:01:15 +00:00
|
|
|
utils().storeInMemoryDynamic(*arrayType.baseType(), true);
|
2016-01-11 03:49:36 +00:00
|
|
|
}
|
2018-09-03 13:01:15 +00:00
|
|
|
|
2016-04-04 11:41:35 +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);
|
2020-04-09 10:48:57 +00:00
|
|
|
if (_tuple.annotation().willBeWrittenTo)
|
2016-01-10 07:12:17 +00:00
|
|
|
{
|
|
|
|
solAssert(!!m_currentLValue, "");
|
|
|
|
lvalues.push_back(move(m_currentLValue));
|
|
|
|
}
|
2015-10-14 13:19:50 +00:00
|
|
|
}
|
2020-04-09 10:48:57 +00:00
|
|
|
else if (_tuple.annotation().willBeWrittenTo)
|
2016-01-10 07:12:17 +00:00
|
|
|
lvalues.push_back(unique_ptr<LValue>());
|
2020-04-09 10:48:57 +00:00
|
|
|
if (_tuple.annotation().willBeWrittenTo)
|
2016-01-10 07:12:17 +00:00
|
|
|
{
|
|
|
|
if (_tuple.components().size() == 1)
|
|
|
|
m_currentLValue = move(lvalues[0]);
|
|
|
|
else
|
2019-11-27 16:24:21 +00:00
|
|
|
m_currentLValue = make_unique<TupleObject>(m_context, move(lvalues));
|
2015-10-14 13:19:50 +00:00
|
|
|
}
|
2016-01-04 08:11:04 +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)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _unaryOperation);
|
2016-03-29 20:08:51 +00:00
|
|
|
if (_unaryOperation.annotation().type->category() == Type::Category::RationalNumber)
|
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: // !
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::ISZERO;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::BitNot: // ~
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::NOT;
|
2014-10-30 00:20:32 +00:00
|
|
|
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.");
|
2018-07-11 12:53:34 +00:00
|
|
|
solUnimplementedAssert(
|
|
|
|
_unaryOperation.annotation().type->category() != Type::Category::FixedPoint,
|
|
|
|
"Not yet implemented - FixedPointType."
|
|
|
|
);
|
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
|
2016-11-14 20:41:58 +00:00
|
|
|
solUnimplementedAssert(_unaryOperation.annotation().type->sizeOnStack() == 1, "Stack size != 1 not implemented.");
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1;
|
2015-03-11 17:09:35 +00:00
|
|
|
if (m_currentLValue->sizeOnStack() > 0)
|
|
|
|
for (unsigned i = 1 + m_currentLValue->sizeOnStack(); i > 0; --i)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << 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)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::ADD;
|
2014-10-30 00:20:32 +00:00
|
|
|
else
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::SUB;
|
2015-03-11 17:09:35 +00:00
|
|
|
// Stack for prefix: [ref...] (*ref)+-1
|
|
|
|
// Stack for postfix: *ref [ref...] (*ref)+-1
|
|
|
|
for (unsigned i = m_currentLValue->sizeOnStack(); i > 0; --i)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << 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: // -
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(0) << Instruction::SUB;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-10-22 14:48:21 +00:00
|
|
|
solAssert(false, "Invalid unary operator: " + string(TokenTraits::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, "");
|
2016-12-06 22:45:17 +00:00
|
|
|
TypePointer const& commonType = _binaryOperation.annotation().commonType;
|
2018-10-22 14:48:21 +00:00
|
|
|
Token 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);
|
2016-12-06 22:45:17 +00:00
|
|
|
else if (commonType->category() == Type::Category::RationalNumber)
|
|
|
|
m_context << commonType->literalValue(nullptr);
|
2014-10-30 00:20:32 +00:00
|
|
|
else
|
|
|
|
{
|
2016-12-06 22:45:17 +00:00
|
|
|
bool cleanupNeeded = cleanupNeededForOp(commonType->category(), c_op);
|
|
|
|
|
|
|
|
TypePointer leftTargetType = commonType;
|
2020-07-23 14:16:39 +00:00
|
|
|
TypePointer rightTargetType =
|
|
|
|
TokenTraits::isShiftOp(c_op) || c_op == Token::Exp ?
|
|
|
|
rightExpression.annotation().type->mobileType() :
|
|
|
|
commonType;
|
2017-02-16 10:45:06 +00:00
|
|
|
solAssert(rightTargetType, "");
|
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)
|
|
|
|
{
|
2016-03-29 20:08:51 +00:00
|
|
|
return dynamic_cast<Literal const*>(&_e) || _e.annotation().type->category() == Type::Category::RationalNumber;
|
2014-12-19 10:31:17 +00:00
|
|
|
};
|
2017-07-17 11:12:00 +00:00
|
|
|
bool swap = m_optimiseOrderLiterals && TokenTraits::isCommutativeOp(c_op) && isLiteral(rightExpression) && !isLiteral(leftExpression);
|
2014-12-11 16:35:23 +00:00
|
|
|
if (swap)
|
|
|
|
{
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(leftExpression, *leftTargetType, cleanupNeeded);
|
|
|
|
acceptAndConvert(rightExpression, *rightTargetType, cleanupNeeded);
|
2014-12-11 16:35:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(rightExpression, *rightTargetType, cleanupNeeded);
|
|
|
|
acceptAndConvert(leftExpression, *leftTargetType, cleanupNeeded);
|
2014-12-11 16:35:23 +00:00
|
|
|
}
|
2018-10-22 14:48:21 +00:00
|
|
|
if (TokenTraits::isShiftOp(c_op))
|
2016-04-29 23:15:22 +00:00
|
|
|
// shift only cares about the signedness of both sides
|
2016-12-06 22:45:17 +00:00
|
|
|
appendShiftOperatorCode(c_op, *leftTargetType, *rightTargetType);
|
2020-07-23 14:16:39 +00:00
|
|
|
else if (c_op == Token::Exp)
|
|
|
|
appendExpOperatorCode(*leftTargetType, *rightTargetType);
|
2018-10-22 14:48:21 +00:00
|
|
|
else if (TokenTraits::isCompareOp(c_op))
|
2016-12-06 22:45:17 +00:00
|
|
|
appendCompareOperatorCode(c_op, *commonType);
|
2014-11-04 14:01:07 +00:00
|
|
|
else
|
2016-12-06 22:45:17 +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
|
|
|
{
|
2020-04-08 17:38:30 +00:00
|
|
|
auto functionCallKind = *_functionCall.annotation().kind;
|
|
|
|
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _functionCall);
|
2020-04-08 17:38:30 +00:00
|
|
|
if (functionCallKind == FunctionCallKind::TypeConversion)
|
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(), "");
|
2019-11-12 15:20:03 +00:00
|
|
|
auto const& expression = *_functionCall.arguments().front();
|
|
|
|
auto const& targetType = *_functionCall.annotation().type;
|
|
|
|
if (auto const* typeType = dynamic_cast<TypeType const*>(expression.annotation().type))
|
|
|
|
if (auto const* addressType = dynamic_cast<AddressType const*>(&targetType))
|
|
|
|
{
|
|
|
|
auto const* contractType = dynamic_cast<ContractType const*>(typeType->actualType());
|
|
|
|
solAssert(
|
|
|
|
contractType &&
|
|
|
|
contractType->contractDefinition().isLibrary() &&
|
|
|
|
addressType->stateMutability() == StateMutability::NonPayable,
|
|
|
|
""
|
|
|
|
);
|
|
|
|
m_context.appendLibraryAddress(contractType->contractDefinition().fullyQualifiedName());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
acceptAndConvert(expression, targetType);
|
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;
|
2020-04-08 17:38:30 +00:00
|
|
|
if (functionCallKind == FunctionCallKind::StructConstructorCall)
|
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
|
2019-04-15 13:33:39 +00:00
|
|
|
functionType = dynamic_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, "");
|
|
|
|
}
|
|
|
|
|
2020-04-08 17:38:30 +00:00
|
|
|
if (functionCallKind == FunctionCallKind::StructConstructorCall)
|
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
|
|
|
|
2019-07-09 14:10:38 +00:00
|
|
|
utils().allocateMemory(max(u256(32u), structType.memoryDataSize()));
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1;
|
2015-06-30 19:08:34 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < arguments.size(); ++i)
|
|
|
|
{
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(*arguments[i], *functionType->parameterTypes()[i]);
|
2015-08-31 16:44:29 +00:00
|
|
|
utils().storeInMemoryDynamic(*functionType->parameterTypes()[i]);
|
2015-06-30 19:08:34 +00:00
|
|
|
}
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-06-30 19:08:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FunctionType const& function = *functionType;
|
2015-11-27 13:08:09 +00:00
|
|
|
if (function.bound())
|
2016-04-26 23:02:10 +00:00
|
|
|
// Only delegatecall and internal functions can be bound, this might be lifted later.
|
2017-03-16 11:58:17 +00:00
|
|
|
solAssert(function.kind() == FunctionType::Kind::DelegateCall || function.kind() == FunctionType::Kind::Internal, "");
|
|
|
|
switch (function.kind())
|
2014-12-04 18:38:24 +00:00
|
|
|
{
|
2020-01-07 10:42:37 +00:00
|
|
|
case FunctionType::Kind::Declaration:
|
|
|
|
solAssert(false, "Attempted to generate code for calling a function definition.");
|
|
|
|
break;
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::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
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem returnLabel = m_context.pushNewTag();
|
2014-11-25 17:23:39 +00:00
|
|
|
for (unsigned i = 0; i < arguments.size(); ++i)
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(*arguments[i], *function.parameterTypes()[i]);
|
2018-03-08 10:17:08 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
bool shortcutTaken = false;
|
|
|
|
if (auto identifier = dynamic_cast<Identifier const*>(&_functionCall.expression()))
|
2018-11-19 14:17:39 +00:00
|
|
|
{
|
|
|
|
solAssert(!function.bound(), "");
|
2018-03-08 10:17:08 +00:00
|
|
|
if (auto functionDef = dynamic_cast<FunctionDefinition const*>(identifier->annotation().referencedDeclaration))
|
|
|
|
{
|
|
|
|
// Do not directly visit the identifier, because this way, we can avoid
|
|
|
|
// the runtime entry label to be created at the creation time context.
|
|
|
|
CompilerContext::LocationSetter locationSetter2(m_context, *identifier);
|
2020-09-16 09:00:07 +00:00
|
|
|
solAssert(*identifier->annotation().requiredLookup == VirtualLookup::Virtual, "");
|
2020-03-24 17:25:59 +00:00
|
|
|
utils().pushCombinedFunctionEntryLabel(
|
|
|
|
functionDef->resolveVirtual(m_context.mostDerivedContract()),
|
|
|
|
false
|
|
|
|
);
|
2018-03-08 10:17:08 +00:00
|
|
|
shortcutTaken = true;
|
|
|
|
}
|
2018-11-19 14:17:39 +00:00
|
|
|
}
|
2018-03-08 10:17:08 +00:00
|
|
|
|
|
|
|
if (!shortcutTaken)
|
|
|
|
_functionCall.expression().accept(*this);
|
|
|
|
}
|
|
|
|
|
2016-04-26 23:02:10 +00:00
|
|
|
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();
|
|
|
|
}
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2016-11-10 17:16:21 +00:00
|
|
|
if (m_context.runtimeContext())
|
|
|
|
// We have a runtime context, so we need the creation part.
|
2018-04-06 14:25:13 +00:00
|
|
|
utils().rightShiftNumberOnStack(32);
|
2016-11-10 17:16:21 +00:00
|
|
|
else
|
|
|
|
// Extract the runtime part.
|
|
|
|
m_context << ((u256(1) << 32) - 1) << Instruction::AND;
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
m_context.appendJump(evmasm::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
|
2020-06-02 13:42:46 +00:00
|
|
|
m_context.adjustStackOffset(static_cast<int>(returnParametersSize - parameterSize) - 1);
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
2014-11-25 17:23:39 +00:00
|
|
|
}
|
2017-08-01 09:32:23 +00:00
|
|
|
case FunctionType::Kind::BareCall:
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::BareDelegateCall:
|
2018-08-15 12:40:20 +00:00
|
|
|
case FunctionType::Kind::BareStaticCall:
|
2019-09-05 18:02:58 +00:00
|
|
|
solAssert(!_functionCall.annotation().tryCall, "");
|
|
|
|
[[fallthrough]];
|
|
|
|
case FunctionType::Kind::External:
|
|
|
|
case FunctionType::Kind::DelegateCall:
|
2015-08-31 16:44:29 +00:00
|
|
|
_functionCall.expression().accept(*this);
|
2019-09-05 18:02:58 +00:00
|
|
|
appendExternalFunctionCall(function, arguments, _functionCall.annotation().tryCall);
|
2015-01-12 11:47:37 +00:00
|
|
|
break;
|
2018-10-17 20:26:10 +00:00
|
|
|
case FunctionType::Kind::BareCallCode:
|
|
|
|
solAssert(false, "Callcode has been removed.");
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::Creation:
|
2014-11-25 17:23:39 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
_functionCall.expression().accept(*this);
|
2020-01-22 14:42:50 +00:00
|
|
|
// Stack: [salt], [value]
|
|
|
|
|
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
|
|
|
}
|
2017-01-20 19:16:18 +00:00
|
|
|
ContractDefinition const* contract =
|
|
|
|
&dynamic_cast<ContractType const&>(*function.returnParameterTypes().front()).contractDefinition();
|
2019-01-10 15:44:31 +00:00
|
|
|
utils().fetchFreeMemoryPointer();
|
|
|
|
utils().copyContractCodeToMemory(*contract, true);
|
2017-09-26 21:10:30 +00:00
|
|
|
utils().abiEncode(argumentTypes, function.parameterTypes());
|
2020-01-22 14:42:50 +00:00
|
|
|
// now on stack: [salt], [value], memory_end_ptr
|
|
|
|
// need: [salt], size, offset, value
|
|
|
|
|
|
|
|
if (function.saltSet())
|
|
|
|
{
|
|
|
|
m_context << dupInstruction(2 + (function.valueSet() ? 1 : 0));
|
|
|
|
m_context << Instruction::SWAP1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now: [salt], [value], [salt], memory_end_ptr
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().toSizeAfterFreeMemoryPointer();
|
2020-01-22 14:42:50 +00:00
|
|
|
|
|
|
|
// now: [salt], [value], [salt], size, offset
|
2015-01-13 17:12:19 +00:00
|
|
|
if (function.valueSet())
|
2020-01-22 14:42:50 +00:00
|
|
|
m_context << dupInstruction(3 + (function.saltSet() ? 1 : 0));
|
2015-01-13 17:12:19 +00:00
|
|
|
else
|
|
|
|
m_context << u256(0);
|
2020-01-22 14:42:50 +00:00
|
|
|
|
|
|
|
// now: [salt], [value], [salt], size, offset, value
|
|
|
|
if (function.saltSet())
|
|
|
|
m_context << Instruction::CREATE2;
|
|
|
|
else
|
|
|
|
m_context << Instruction::CREATE;
|
|
|
|
|
|
|
|
// now: [salt], [value], address
|
|
|
|
|
2015-01-13 17:12:19 +00:00
|
|
|
if (function.valueSet())
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << swapInstruction(1) << Instruction::POP;
|
2020-01-22 14:42:50 +00:00
|
|
|
if (function.saltSet())
|
|
|
|
m_context << swapInstruction(1) << Instruction::POP;
|
|
|
|
|
2019-09-09 21:10:36 +00:00
|
|
|
// Check if zero (reverted)
|
|
|
|
m_context << Instruction::DUP1 << Instruction::ISZERO;
|
|
|
|
if (_functionCall.annotation().tryCall)
|
|
|
|
{
|
|
|
|
// If this is a try call, return "<address> 1" in the success case and
|
|
|
|
// "0" in the error case.
|
|
|
|
AssemblyItem errorCase = m_context.appendConditionalJump();
|
|
|
|
m_context << u256(1);
|
|
|
|
m_context << errorCase;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_context.appendConditionalRevert(true);
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::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
|
|
|
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(*arguments.front(), *TypeProvider::uint256(), 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.
|
2020-06-02 13:42:46 +00:00
|
|
|
unsigned stackDepth = (function.gasSet() ? 1u : 0u) + (function.valueSet() ? 1u : 0u);
|
2015-01-12 11:47:37 +00:00
|
|
|
if (stackDepth > 0)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << swapInstruction(stackDepth);
|
2015-01-12 11:47:37 +00:00
|
|
|
if (function.gasSet())
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
2014-12-10 22:01:40 +00:00
|
|
|
}
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::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())
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-01-12 11:47:37 +00:00
|
|
|
arguments.front()->accept(*this);
|
|
|
|
break;
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::Send:
|
|
|
|
case FunctionType::Kind::Transfer:
|
2015-08-31 16:44:29 +00:00
|
|
|
_functionCall.expression().accept(*this);
|
2016-09-05 13:47:03 +00:00
|
|
|
// Provide the gas stipend manually at first because we may send zero ether.
|
|
|
|
// Will be zeroed if we send more than zero ether.
|
2019-12-11 16:31:36 +00:00
|
|
|
m_context << u256(evmasm::GasCosts::callStipend);
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(*arguments.front(), *function.parameterTypes().front(), true);
|
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;
|
2015-04-22 16:53:58 +00:00
|
|
|
appendExternalFunctionCall(
|
|
|
|
FunctionType(
|
|
|
|
TypePointers{},
|
|
|
|
TypePointers{},
|
|
|
|
strings(),
|
2015-04-27 11:07:25 +00:00
|
|
|
strings(),
|
2017-08-01 09:32:23 +00:00
|
|
|
FunctionType::Kind::BareCall,
|
2015-04-22 16:53:58 +00:00
|
|
|
false,
|
2017-08-09 20:52:25 +00:00
|
|
|
StateMutability::NonPayable,
|
2017-08-28 13:40:28 +00:00
|
|
|
nullptr,
|
2015-06-05 10:25:10 +00:00
|
|
|
true,
|
2015-04-22 16:53:58 +00:00
|
|
|
true
|
|
|
|
),
|
2019-09-05 18:02:58 +00:00
|
|
|
{},
|
|
|
|
false
|
2015-04-22 16:53:58 +00:00
|
|
|
);
|
2017-03-16 11:58:17 +00:00
|
|
|
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;
|
2020-01-22 14:48:56 +00:00
|
|
|
// Revert message bubbles up.
|
2017-12-30 19:13:41 +00:00
|
|
|
m_context.appendConditionalRevert(true);
|
2017-02-05 20:21:14 +00:00
|
|
|
}
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::Selfdestruct:
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(*arguments.front(), *function.parameterTypes().front(), true);
|
2017-02-06 18:35:18 +00:00
|
|
|
m_context << Instruction::SELFDESTRUCT;
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::Revert:
|
2017-12-30 12:47:51 +00:00
|
|
|
{
|
2019-09-18 14:44:36 +00:00
|
|
|
if (arguments.empty())
|
|
|
|
m_context.appendRevert();
|
|
|
|
else
|
2017-12-30 12:47:51 +00:00
|
|
|
{
|
2018-04-06 13:14:55 +00:00
|
|
|
// function-sel(Error(string)) + encoding
|
2017-12-30 12:47:51 +00:00
|
|
|
solAssert(arguments.size() == 1, "");
|
|
|
|
solAssert(function.parameterTypes().size() == 1, "");
|
2020-01-22 14:48:56 +00:00
|
|
|
if (m_context.revertStrings() == RevertStrings::Strip)
|
2019-09-18 14:44:36 +00:00
|
|
|
{
|
2020-09-10 10:01:23 +00:00
|
|
|
if (!*arguments.front()->annotation().isPure)
|
2019-09-18 14:44:36 +00:00
|
|
|
{
|
|
|
|
arguments.front()->accept(*this);
|
|
|
|
utils().popStackElement(*arguments.front()->annotation().type);
|
|
|
|
}
|
|
|
|
m_context.appendRevert();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
arguments.front()->accept(*this);
|
|
|
|
utils().revertWithStringData(*arguments.front()->annotation().type);
|
|
|
|
}
|
2017-12-30 12:47:51 +00:00
|
|
|
}
|
2017-02-06 22:11:19 +00:00
|
|
|
break;
|
2017-12-30 12:47:51 +00:00
|
|
|
}
|
2018-08-08 15:31:46 +00:00
|
|
|
case FunctionType::Kind::KECCAK256:
|
2015-02-03 13:02:58 +00:00
|
|
|
{
|
2018-05-23 14:55:58 +00:00
|
|
|
solAssert(arguments.size() == 1, "");
|
|
|
|
solAssert(!function.padArguments(), "");
|
|
|
|
TypePointer const& argType = arguments.front()->annotation().type;
|
|
|
|
solAssert(argType, "");
|
|
|
|
arguments.front()->accept(*this);
|
2020-07-07 13:10:21 +00:00
|
|
|
if (auto const* stringLiteral = dynamic_cast<StringLiteralType const*>(argType))
|
|
|
|
// Optimization: Compute keccak256 on string literals at compile-time.
|
|
|
|
m_context << u256(keccak256(stringLiteral->value()));
|
|
|
|
else if (*argType == *TypeProvider::bytesMemory() || *argType == *TypeProvider::stringMemory())
|
2015-06-05 22:57:51 +00:00
|
|
|
{
|
2020-07-07 13:10:21 +00:00
|
|
|
// Optimization: If type is bytes or string, then do not encode,
|
|
|
|
// but directly compute keccak256 on memory.
|
2019-04-17 11:40:50 +00:00
|
|
|
ArrayUtils(m_context).retrieveLength(*TypeProvider::bytesMemory());
|
2018-05-23 14:55:58 +00:00
|
|
|
m_context << Instruction::SWAP1 << u256(0x20) << Instruction::ADD;
|
2020-07-07 13:10:21 +00:00
|
|
|
m_context << Instruction::KECCAK256;
|
2018-05-23 14:55:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
utils().fetchFreeMemoryPointer();
|
|
|
|
utils().packedEncode({argType}, TypePointers());
|
|
|
|
utils().toSizeAfterFreeMemoryPointer();
|
2020-07-07 13:10:21 +00:00
|
|
|
m_context << Instruction::KECCAK256;
|
2015-06-05 22:57:51 +00:00
|
|
|
}
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
2015-02-03 13:02:58 +00:00
|
|
|
}
|
2017-03-16 11:58:17 +00:00
|
|
|
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
|
|
|
{
|
2020-06-02 13:42:46 +00:00
|
|
|
unsigned logNumber = static_cast<unsigned>(function.kind()) - static_cast<unsigned>(FunctionType::Kind::Log0);
|
2015-01-29 15:42:59 +00:00
|
|
|
for (unsigned arg = logNumber; arg > 0; --arg)
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(*arguments[arg], *function.parameterTypes()[arg], true);
|
2015-06-05 22:57:51 +00:00
|
|
|
arguments.front()->accept(*this);
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().fetchFreeMemoryPointer();
|
2019-01-24 13:40:22 +00:00
|
|
|
solAssert(function.parameterTypes().front()->isValueType(), "");
|
2017-09-26 21:10:30 +00:00
|
|
|
utils().packedEncode(
|
2015-09-16 14:56:30 +00:00
|
|
|
{arguments.front()->annotation().type},
|
2017-09-26 21:10:30 +00:00
|
|
|
{function.parameterTypes().front()}
|
|
|
|
);
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().toSizeAfterFreeMemoryPointer();
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << logInstruction(logNumber);
|
2015-01-29 15:42:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-03-16 11:58:17 +00:00
|
|
|
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;
|
2019-01-24 13:40:22 +00:00
|
|
|
TypePointers paramTypes = function.parameterTypes();
|
2015-01-29 15:42:59 +00:00
|
|
|
// 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);
|
2019-04-15 13:33:39 +00:00
|
|
|
if (auto const& referenceType = dynamic_cast<ReferenceType const*>(paramTypes[arg - 1]))
|
2015-11-16 16:09:09 +00:00
|
|
|
{
|
|
|
|
utils().fetchFreeMemoryPointer();
|
2017-09-26 21:10:30 +00:00
|
|
|
utils().packedEncode(
|
2015-11-16 16:09:09 +00:00
|
|
|
{arguments[arg - 1]->annotation().type},
|
2019-01-24 13:40:22 +00:00
|
|
|
{referenceType}
|
2015-11-16 16:09:09 +00:00
|
|
|
);
|
|
|
|
utils().toSizeAfterFreeMemoryPointer();
|
2017-05-10 07:48:00 +00:00
|
|
|
m_context << Instruction::KECCAK256;
|
2015-11-16 16:09:09 +00:00
|
|
|
}
|
|
|
|
else
|
2019-01-24 13:40:22 +00:00
|
|
|
{
|
|
|
|
solAssert(paramTypes[arg - 1]->isValueType(), "");
|
2020-09-21 15:19:32 +00:00
|
|
|
if (auto functionType = dynamic_cast<FunctionType const*>(paramTypes[arg - 1]))
|
|
|
|
{
|
|
|
|
auto argumentType =
|
|
|
|
dynamic_cast<FunctionType const*>(arguments[arg-1]->annotation().type);
|
|
|
|
solAssert(
|
|
|
|
argumentType &&
|
|
|
|
functionType->kind() == FunctionType::Kind::External &&
|
|
|
|
argumentType->kind() == FunctionType::Kind::External &&
|
|
|
|
!argumentType->bound(),
|
|
|
|
""
|
|
|
|
);
|
|
|
|
|
|
|
|
utils().combineExternalFunctionType(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
utils().convertType(
|
|
|
|
*arguments[arg - 1]->annotation().type,
|
|
|
|
*paramTypes[arg - 1],
|
|
|
|
true
|
|
|
|
);
|
2019-01-24 13:40:22 +00:00
|
|
|
}
|
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
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
m_context << u256(h256::Arith(keccak256(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);
|
2019-01-24 13:40:22 +00:00
|
|
|
nonIndexedParamTypes.push_back(paramTypes[arg]);
|
2015-04-21 08:59:48 +00:00
|
|
|
}
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().fetchFreeMemoryPointer();
|
2017-09-26 21:10:30 +00:00
|
|
|
utils().abiEncode(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();
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << logInstruction(numIndexed);
|
2015-01-08 23:22:06 +00:00
|
|
|
break;
|
2015-01-09 14:00:47 +00:00
|
|
|
}
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::BlockHash:
|
2015-01-15 18:59:35 +00:00
|
|
|
{
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(*arguments[0], *function.parameterTypes()[0], true);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::BLOCKHASH;
|
2015-01-15 18:59:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::AddMod:
|
|
|
|
case FunctionType::Kind::MulMod:
|
2015-11-18 16:12:39 +00:00
|
|
|
{
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(*arguments[2], *TypeProvider::uint256());
|
2018-02-15 12:04:40 +00:00
|
|
|
m_context << Instruction::DUP1 << Instruction::ISZERO;
|
|
|
|
m_context.appendConditionalInvalid();
|
|
|
|
for (unsigned i = 1; i < 3; i ++)
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(*arguments[2 - i], *TypeProvider::uint256());
|
2017-03-16 11:58:17 +00:00
|
|
|
if (function.kind() == FunctionType::Kind::AddMod)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::ADDMOD;
|
2015-11-18 16:12:39 +00:00
|
|
|
else
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::MULMOD;
|
2015-11-18 16:12:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-03-16 11:58:17 +00:00
|
|
|
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);
|
2018-12-18 22:50:26 +00:00
|
|
|
static map<FunctionType::Kind, u256> const contractAddresses{
|
|
|
|
{FunctionType::Kind::ECRecover, 1},
|
|
|
|
{FunctionType::Kind::SHA256, 2},
|
|
|
|
{FunctionType::Kind::RIPEMD160, 3}
|
|
|
|
};
|
|
|
|
m_context << contractAddresses.at(function.kind());
|
2015-08-31 16:44:29 +00:00
|
|
|
for (unsigned i = function.sizeOnStack(); i > 0; --i)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << swapInstruction(i);
|
2019-09-05 18:02:58 +00:00
|
|
|
solAssert(!_functionCall.annotation().tryCall, "");
|
|
|
|
appendExternalFunctionCall(function, arguments, false);
|
2014-12-04 18:38:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::ByteArrayPush:
|
|
|
|
case FunctionType::Kind::ArrayPush:
|
2015-10-13 14:55:59 +00:00
|
|
|
{
|
2015-10-15 11:54:59 +00:00
|
|
|
_functionCall.expression().accept(*this);
|
2019-09-13 22:54:51 +00:00
|
|
|
|
|
|
|
if (function.parameterTypes().size() == 0)
|
|
|
|
{
|
|
|
|
auto paramType = function.returnParameterTypes().at(0);
|
|
|
|
solAssert(paramType, "");
|
|
|
|
|
|
|
|
ArrayType const* arrayType =
|
|
|
|
function.kind() == FunctionType::Kind::ArrayPush ?
|
|
|
|
TypeProvider::array(DataLocation::Storage, paramType) :
|
|
|
|
TypeProvider::bytesStorage();
|
|
|
|
|
|
|
|
// stack: ArrayReference
|
|
|
|
m_context << u256(1) << Instruction::DUP2;
|
|
|
|
ArrayUtils(m_context).incrementDynamicArraySize(*arrayType);
|
|
|
|
// stack: ArrayReference 1 newLength
|
|
|
|
m_context << Instruction::SUB;
|
|
|
|
// stack: ArrayReference (newLength-1)
|
|
|
|
ArrayUtils(m_context).accessIndex(*arrayType, false);
|
|
|
|
|
|
|
|
if (arrayType->isByteArray())
|
|
|
|
setLValue<StorageByteArrayElement>(_functionCall);
|
|
|
|
else
|
|
|
|
setLValueToStorageItem(_functionCall);
|
|
|
|
}
|
2015-10-15 12:37:11 +00:00
|
|
|
else
|
2019-09-13 22:54:51 +00:00
|
|
|
{
|
|
|
|
solAssert(function.parameterTypes().size() == 1, "");
|
|
|
|
solAssert(!!function.parameterTypes()[0], "");
|
|
|
|
TypePointer paramType = function.parameterTypes()[0];
|
|
|
|
ArrayType const* arrayType =
|
|
|
|
function.kind() == FunctionType::Kind::ArrayPush ?
|
|
|
|
TypeProvider::array(DataLocation::Storage, paramType) :
|
|
|
|
TypeProvider::bytesStorage();
|
|
|
|
|
|
|
|
// stack: ArrayReference
|
|
|
|
arguments[0]->accept(*this);
|
|
|
|
TypePointer const& argType = arguments[0]->annotation().type;
|
|
|
|
// stack: ArrayReference argValue
|
|
|
|
utils().moveToStackTop(argType->sizeOnStack(), 1);
|
|
|
|
// stack: argValue ArrayReference
|
|
|
|
m_context << Instruction::DUP1;
|
|
|
|
ArrayUtils(m_context).incrementDynamicArraySize(*arrayType);
|
|
|
|
// stack: argValue ArrayReference newLength
|
|
|
|
m_context << u256(1) << Instruction::SWAP1 << Instruction::SUB;
|
|
|
|
// stack: argValue ArrayReference (newLength-1)
|
|
|
|
ArrayUtils(m_context).accessIndex(*arrayType, false);
|
|
|
|
// stack: argValue storageSlot slotOffset
|
|
|
|
utils().moveToStackTop(2, argType->sizeOnStack());
|
|
|
|
// stack: storageSlot slotOffset argValue
|
|
|
|
TypePointer type = arguments[0]->annotation().type->closestTemporaryType(arrayType->baseType());
|
|
|
|
solAssert(type, "");
|
|
|
|
utils().convertType(*argType, *type);
|
|
|
|
utils().moveToStackTop(1 + type->sizeOnStack());
|
|
|
|
utils().moveToStackTop(1 + type->sizeOnStack());
|
|
|
|
// stack: 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);
|
|
|
|
}
|
2015-10-13 14:55:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-03-09 16:46:24 +00:00
|
|
|
case FunctionType::Kind::ArrayPop:
|
|
|
|
{
|
|
|
|
_functionCall.expression().accept(*this);
|
|
|
|
solAssert(function.parameterTypes().empty(), "");
|
|
|
|
|
|
|
|
ArrayType const& arrayType = dynamic_cast<ArrayType const&>(
|
|
|
|
*dynamic_cast<MemberAccess const&>(_functionCall.expression()).expression().annotation().type
|
|
|
|
);
|
|
|
|
solAssert(arrayType.dataStoredIn(DataLocation::Storage), "");
|
|
|
|
|
|
|
|
ArrayUtils(m_context).popStorageArrayElement(arrayType);
|
|
|
|
break;
|
|
|
|
}
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::ObjectCreation:
|
2015-11-17 14:15:00 +00:00
|
|
|
{
|
|
|
|
ArrayType const& arrayType = dynamic_cast<ArrayType const&>(*_functionCall.annotation().type);
|
|
|
|
_functionCall.expression().accept(*this);
|
|
|
|
solAssert(arguments.size() == 1, "");
|
|
|
|
|
|
|
|
// Fetch requested length.
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(*arguments[0], *TypeProvider::uint256());
|
2015-11-17 14:15:00 +00:00
|
|
|
|
2020-04-01 12:02:30 +00:00
|
|
|
// Make sure we can allocate memory without overflow
|
|
|
|
m_context << u256(0xffffffffffffffff);
|
|
|
|
m_context << Instruction::DUP2;
|
|
|
|
m_context << Instruction::GT;
|
|
|
|
m_context.appendConditionalRevert();
|
|
|
|
|
2015-11-17 14:15:00 +00:00
|
|
|
// Stack: requested_length
|
|
|
|
utils().fetchFreeMemoryPointer();
|
|
|
|
|
|
|
|
// Stack: requested_length memptr
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1;
|
2015-11-17 14:15:00 +00:00
|
|
|
// Stack: memptr requested_length
|
|
|
|
// store length
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1 << Instruction::DUP3 << Instruction::MSTORE;
|
2015-11-17 14:15:00 +00:00
|
|
|
// Stack: memptr requested_length
|
|
|
|
// update free memory pointer
|
2017-10-11 10:28:21 +00:00
|
|
|
m_context << Instruction::DUP1;
|
|
|
|
// Stack: memptr requested_length requested_length
|
|
|
|
if (arrayType.isByteArray())
|
|
|
|
// Round up to multiple of 32
|
|
|
|
m_context << u256(31) << Instruction::ADD << u256(31) << Instruction::NOT << Instruction::AND;
|
|
|
|
else
|
|
|
|
m_context << arrayType.baseType()->memoryHeadSize() << Instruction::MUL;
|
|
|
|
// stacK: memptr requested_length data_size
|
|
|
|
m_context << u256(32) << Instruction::ADD;
|
2016-04-04 11:41:35 +00:00
|
|
|
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();
|
2018-03-08 18:41:29 +00:00
|
|
|
// Always initialize because the free memory pointer might point at
|
|
|
|
// a dirty memory area.
|
|
|
|
m_context << Instruction::DUP2 << u256(32) << Instruction::ADD;
|
|
|
|
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;
|
|
|
|
}
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::Assert:
|
|
|
|
case FunctionType::Kind::Require:
|
2017-02-09 01:37:53 +00:00
|
|
|
{
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(*arguments.front(), *function.parameterTypes().front(), false);
|
2019-09-18 14:44:36 +00:00
|
|
|
|
2020-01-22 14:48:56 +00:00
|
|
|
bool haveReasonString = arguments.size() > 1 && m_context.revertStrings() != RevertStrings::Strip;
|
2019-09-18 14:44:36 +00:00
|
|
|
|
2017-12-30 13:35:45 +00:00
|
|
|
if (arguments.size() > 1)
|
|
|
|
{
|
|
|
|
// Users probably expect the second argument to be evaluated
|
|
|
|
// even if the condition is false, as would be the case for an actual
|
|
|
|
// function call.
|
|
|
|
solAssert(arguments.size() == 2, "");
|
|
|
|
solAssert(function.kind() == FunctionType::Kind::Require, "");
|
2020-01-22 14:48:56 +00:00
|
|
|
if (m_context.revertStrings() == RevertStrings::Strip)
|
2019-09-18 14:44:36 +00:00
|
|
|
{
|
2020-09-10 10:01:23 +00:00
|
|
|
if (!*arguments.at(1)->annotation().isPure)
|
2019-09-18 14:44:36 +00:00
|
|
|
{
|
|
|
|
arguments.at(1)->accept(*this);
|
|
|
|
utils().popStackElement(*arguments.at(1)->annotation().type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
arguments.at(1)->accept(*this);
|
|
|
|
utils().moveIntoStack(1, arguments.at(1)->annotation().type->sizeOnStack());
|
|
|
|
}
|
2017-12-30 13:35:45 +00:00
|
|
|
}
|
|
|
|
// Stack: <error string (unconverted)> <condition>
|
2017-02-10 22:53:32 +00:00
|
|
|
// jump if condition was met
|
|
|
|
m_context << Instruction::ISZERO << Instruction::ISZERO;
|
|
|
|
auto success = m_context.appendConditionalJump();
|
2017-03-16 11:58:17 +00:00
|
|
|
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();
|
2019-09-18 14:44:36 +00:00
|
|
|
else if (haveReasonString)
|
2018-05-04 02:18:52 +00:00
|
|
|
{
|
2018-04-06 13:14:55 +00:00
|
|
|
utils().revertWithStringData(*arguments.at(1)->annotation().type);
|
2018-05-04 02:18:52 +00:00
|
|
|
// Here, the argument is consumed, but in the other branch, it is still there.
|
2020-06-02 13:42:46 +00:00
|
|
|
m_context.adjustStackOffset(static_cast<int>(arguments.at(1)->annotation().type->sizeOnStack()));
|
2018-05-04 02:18:52 +00:00
|
|
|
}
|
2017-03-09 16:03:53 +00:00
|
|
|
else
|
2017-05-24 09:47:43 +00:00
|
|
|
m_context.appendRevert();
|
2017-02-10 22:53:32 +00:00
|
|
|
// the success branch
|
|
|
|
m_context << success;
|
2019-09-18 14:44:36 +00:00
|
|
|
if (haveReasonString)
|
2017-12-30 13:35:45 +00:00
|
|
|
utils().popStackElement(*arguments.at(1)->annotation().type);
|
2017-02-09 01:37:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-07-31 23:55:13 +00:00
|
|
|
case FunctionType::Kind::ABIEncode:
|
|
|
|
case FunctionType::Kind::ABIEncodePacked:
|
|
|
|
case FunctionType::Kind::ABIEncodeWithSelector:
|
|
|
|
case FunctionType::Kind::ABIEncodeWithSignature:
|
|
|
|
{
|
|
|
|
bool const isPacked = function.kind() == FunctionType::Kind::ABIEncodePacked;
|
|
|
|
bool const hasSelectorOrSignature =
|
|
|
|
function.kind() == FunctionType::Kind::ABIEncodeWithSelector ||
|
|
|
|
function.kind() == FunctionType::Kind::ABIEncodeWithSignature;
|
|
|
|
|
|
|
|
TypePointers argumentTypes;
|
|
|
|
TypePointers targetTypes;
|
|
|
|
for (unsigned i = 0; i < arguments.size(); ++i)
|
|
|
|
{
|
|
|
|
arguments[i]->accept(*this);
|
|
|
|
// Do not keep the selector as part of the ABI encoded args
|
|
|
|
if (!hasSelectorOrSignature || i > 0)
|
|
|
|
argumentTypes.push_back(arguments[i]->annotation().type);
|
|
|
|
}
|
|
|
|
utils().fetchFreeMemoryPointer();
|
|
|
|
// stack now: [<selector>] <arg1> .. <argN> <free_mem>
|
|
|
|
|
|
|
|
// adjust by 32(+4) bytes to accommodate the length(+selector)
|
|
|
|
m_context << u256(32 + (hasSelectorOrSignature ? 4 : 0)) << Instruction::ADD;
|
|
|
|
// stack now: [<selector>] <arg1> .. <argN> <data_encoding_area_start>
|
|
|
|
|
|
|
|
if (isPacked)
|
|
|
|
{
|
|
|
|
solAssert(!function.padArguments(), "");
|
|
|
|
utils().packedEncode(argumentTypes, TypePointers());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(function.padArguments(), "");
|
|
|
|
utils().abiEncode(argumentTypes, TypePointers());
|
|
|
|
}
|
|
|
|
utils().fetchFreeMemoryPointer();
|
|
|
|
// stack: [<selector>] <data_encoding_area_end> <bytes_memory_ptr>
|
|
|
|
|
|
|
|
// size is end minus start minus length slot
|
|
|
|
m_context.appendInlineAssembly(R"({
|
|
|
|
mstore(mem_ptr, sub(sub(mem_end, mem_ptr), 0x20))
|
|
|
|
})", {"mem_end", "mem_ptr"});
|
|
|
|
m_context << Instruction::SWAP1;
|
|
|
|
utils().storeFreeMemoryPointer();
|
|
|
|
// stack: [<selector>] <memory ptr>
|
|
|
|
|
|
|
|
if (hasSelectorOrSignature)
|
|
|
|
{
|
|
|
|
// stack: <selector> <memory pointer>
|
|
|
|
solAssert(arguments.size() >= 1, "");
|
|
|
|
TypePointer const& selectorType = arguments[0]->annotation().type;
|
|
|
|
utils().moveIntoStack(selectorType->sizeOnStack());
|
|
|
|
TypePointer dataOnStack = selectorType;
|
|
|
|
// stack: <memory pointer> <selector>
|
|
|
|
if (function.kind() == FunctionType::Kind::ABIEncodeWithSignature)
|
|
|
|
{
|
|
|
|
// hash the signature
|
2019-04-15 13:33:39 +00:00
|
|
|
if (auto const* stringType = dynamic_cast<StringLiteralType const*>(selectorType))
|
2017-07-31 23:55:13 +00:00
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
FixedHash<4> hash(keccak256(stringType->value()));
|
2017-07-31 23:55:13 +00:00
|
|
|
m_context << (u256(FixedHash<4>::Arith(hash)) << (256 - 32));
|
2019-04-17 11:40:50 +00:00
|
|
|
dataOnStack = TypeProvider::fixedBytes(4);
|
2017-07-31 23:55:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
utils().fetchFreeMemoryPointer();
|
|
|
|
// stack: <memory pointer> <selector> <free mem ptr>
|
|
|
|
utils().packedEncode(TypePointers{selectorType}, TypePointers());
|
|
|
|
utils().toSizeAfterFreeMemoryPointer();
|
|
|
|
m_context << Instruction::KECCAK256;
|
|
|
|
// stack: <memory pointer> <hash>
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
dataOnStack = TypeProvider::fixedBytes(32);
|
2017-07-31 23:55:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(function.kind() == FunctionType::Kind::ABIEncodeWithSelector, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
utils().convertType(*dataOnStack, FixedBytesType(4), true);
|
|
|
|
|
|
|
|
// stack: <memory pointer> <selector>
|
|
|
|
|
|
|
|
// load current memory, mask and combine the selector
|
|
|
|
string mask = formatNumber((u256(-1) >> 32));
|
|
|
|
m_context.appendInlineAssembly(R"({
|
|
|
|
let data_start := add(mem_ptr, 0x20)
|
|
|
|
let data := mload(data_start)
|
|
|
|
let mask := )" + mask + R"(
|
2018-04-13 01:14:18 +00:00
|
|
|
mstore(data_start, or(and(data, mask), selector))
|
2017-07-31 23:55:13 +00:00
|
|
|
})", {"mem_ptr", "selector"});
|
|
|
|
m_context << Instruction::POP;
|
|
|
|
}
|
|
|
|
|
|
|
|
// stack now: <memory pointer>
|
|
|
|
break;
|
|
|
|
}
|
2018-06-30 16:09:13 +00:00
|
|
|
case FunctionType::Kind::ABIDecode:
|
|
|
|
{
|
|
|
|
arguments.front()->accept(*this);
|
|
|
|
TypePointer firstArgType = arguments.front()->annotation().type;
|
2018-09-04 14:24:21 +00:00
|
|
|
TypePointers targetTypes;
|
2019-04-15 13:33:39 +00:00
|
|
|
if (TupleType const* targetTupleType = dynamic_cast<TupleType const*>(_functionCall.annotation().type))
|
2018-09-04 14:24:21 +00:00
|
|
|
targetTypes = targetTupleType->components();
|
|
|
|
else
|
|
|
|
targetTypes = TypePointers{_functionCall.annotation().type};
|
2018-06-30 16:09:13 +00:00
|
|
|
if (
|
2019-09-03 16:30:00 +00:00
|
|
|
auto referenceType = dynamic_cast<ReferenceType const*>(firstArgType);
|
|
|
|
referenceType && referenceType->dataStoredIn(DataLocation::CallData)
|
2018-06-30 16:09:13 +00:00
|
|
|
)
|
2019-09-03 16:30:00 +00:00
|
|
|
{
|
|
|
|
solAssert(referenceType->isImplicitlyConvertibleTo(*TypeProvider::bytesCalldata()), "");
|
|
|
|
utils().convertType(*referenceType, *TypeProvider::bytesCalldata());
|
2018-06-30 16:09:13 +00:00
|
|
|
utils().abiDecode(targetTypes, false);
|
2019-09-03 16:30:00 +00:00
|
|
|
}
|
2018-06-30 16:09:13 +00:00
|
|
|
else
|
|
|
|
{
|
2019-04-17 11:40:50 +00:00
|
|
|
utils().convertType(*firstArgType, *TypeProvider::bytesMemory());
|
2018-06-30 16:09:13 +00:00
|
|
|
m_context << Instruction::DUP1 << u256(32) << Instruction::ADD;
|
|
|
|
m_context << Instruction::SWAP1 << Instruction::MLOAD;
|
|
|
|
// stack now: <mem_pos> <length>
|
|
|
|
|
|
|
|
utils().abiDecode(targetTypes, true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-03-02 16:58:27 +00:00
|
|
|
case FunctionType::Kind::GasLeft:
|
|
|
|
m_context << Instruction::GAS;
|
|
|
|
break;
|
2019-01-10 15:28:39 +00:00
|
|
|
case FunctionType::Kind::MetaType:
|
|
|
|
// No code to generate.
|
|
|
|
break;
|
2014-11-25 17:23:39 +00:00
|
|
|
}
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:42:50 +00:00
|
|
|
bool ExpressionCompiler::visit(FunctionCallOptions const& _functionCallOptions)
|
|
|
|
{
|
|
|
|
_functionCallOptions.expression().accept(*this);
|
|
|
|
|
|
|
|
// Desired Stack: [salt], [gas], [value]
|
|
|
|
enum Option { Salt, Gas, Value };
|
|
|
|
|
|
|
|
vector<Option> presentOptions;
|
|
|
|
FunctionType const& funType = dynamic_cast<FunctionType const&>(
|
|
|
|
*_functionCallOptions.expression().annotation().type
|
|
|
|
);
|
|
|
|
if (funType.saltSet()) presentOptions.emplace_back(Salt);
|
|
|
|
if (funType.gasSet()) presentOptions.emplace_back(Gas);
|
|
|
|
if (funType.valueSet()) presentOptions.emplace_back(Value);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < _functionCallOptions.options().size(); ++i)
|
|
|
|
{
|
|
|
|
string const& name = *_functionCallOptions.names()[i];
|
|
|
|
Type const* requiredType = TypeProvider::uint256();
|
|
|
|
Option newOption;
|
|
|
|
if (name == "salt")
|
|
|
|
{
|
|
|
|
newOption = Salt;
|
|
|
|
requiredType = TypeProvider::fixedBytes(32);
|
|
|
|
}
|
|
|
|
else if (name == "gas")
|
|
|
|
newOption = Gas;
|
|
|
|
else if (name == "value")
|
|
|
|
newOption = Value;
|
|
|
|
else
|
|
|
|
solAssert(false, "Unexpected option name!");
|
|
|
|
acceptAndConvert(*_functionCallOptions.options()[i], *requiredType);
|
|
|
|
|
|
|
|
solAssert(!contains(presentOptions, newOption), "");
|
2020-06-02 13:42:46 +00:00
|
|
|
ptrdiff_t insertPos = presentOptions.end() - lower_bound(presentOptions.begin(), presentOptions.end(), newOption);
|
2020-01-22 14:42:50 +00:00
|
|
|
|
2020-06-02 13:42:46 +00:00
|
|
|
utils().moveIntoStack(static_cast<size_t>(insertPos), 1);
|
2020-01-22 14:42:50 +00:00
|
|
|
presentOptions.insert(presentOptions.end() - insertPos, newOption);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-26 23:02:10 +00:00
|
|
|
bool ExpressionCompiler::visit(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();
|
2019-04-15 13:33:39 +00:00
|
|
|
if (auto funType = dynamic_cast<FunctionType const*>(_memberAccess.annotation().type))
|
2015-11-27 13:08:09 +00:00
|
|
|
if (funType->bound())
|
|
|
|
{
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(_memberAccess.expression(), *funType->selfType(), true);
|
2017-03-16 11:58:17 +00:00
|
|
|
if (funType->kind() == FunctionType::Kind::Internal)
|
2016-04-26 23:02:10 +00:00
|
|
|
{
|
2016-11-10 17:16:21 +00:00
|
|
|
FunctionDefinition const& funDef = dynamic_cast<decltype(funDef)>(funType->declaration());
|
2020-09-16 09:00:07 +00:00
|
|
|
solAssert(*_memberAccess.annotation().requiredLookup == VirtualLookup::Static, "");
|
2016-11-10 17:16:21 +00:00
|
|
|
utils().pushCombinedFunctionEntryLabel(funDef);
|
2016-04-26 23:02:10 +00:00
|
|
|
utils().moveIntoStack(funType->selfType()->sizeOnStack(), 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-16 11:58:17 +00:00
|
|
|
solAssert(funType->kind() == FunctionType::Kind::DelegateCall, "");
|
2016-04-26 23:02:10 +00:00
|
|
|
auto contract = dynamic_cast<ContractDefinition const*>(funType->declaration().scope());
|
|
|
|
solAssert(contract && contract->isLibrary(), "");
|
2016-12-21 19:26:22 +00:00
|
|
|
m_context.appendLibraryAddress(contract->fullyQualifiedName());
|
2016-04-26 23:02:10 +00:00
|
|
|
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
|
2016-10-21 08:05:36 +00:00
|
|
|
// for internal functions, or enum/struct definitions.
|
2019-04-15 13:33:39 +00:00
|
|
|
if (TypeType const* type = dynamic_cast<TypeType const*>(_memberAccess.expression().annotation().type))
|
2016-04-26 23:02:10 +00:00
|
|
|
{
|
2019-04-15 13:33:39 +00:00
|
|
|
if (dynamic_cast<ContractType const*>(type->actualType()))
|
2016-04-26 23:02:10 +00:00
|
|
|
{
|
2016-10-21 08:05:36 +00:00
|
|
|
solAssert(_memberAccess.annotation().type, "_memberAccess has no type");
|
2018-11-29 18:30:27 +00:00
|
|
|
if (auto variable = dynamic_cast<VariableDeclaration const*>(_memberAccess.annotation().referencedDeclaration))
|
|
|
|
appendVariable(*variable, static_cast<Expression const&>(_memberAccess));
|
2019-04-15 13:33:39 +00:00
|
|
|
else if (auto funType = dynamic_cast<FunctionType const*>(_memberAccess.annotation().type))
|
2016-04-26 23:02:10 +00:00
|
|
|
{
|
2017-03-16 11:58:17 +00:00
|
|
|
switch (funType->kind())
|
2016-04-26 23:02:10 +00:00
|
|
|
{
|
2020-02-07 11:44:52 +00:00
|
|
|
case FunctionType::Kind::Declaration:
|
|
|
|
break;
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::Internal:
|
2016-04-26 23:02:10 +00:00
|
|
|
// 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.
|
2016-10-26 11:08:00 +00:00
|
|
|
if (auto const* function = dynamic_cast<FunctionDefinition const*>(_memberAccess.annotation().referencedDeclaration))
|
2020-09-16 09:00:07 +00:00
|
|
|
{
|
|
|
|
solAssert(*_memberAccess.annotation().requiredLookup == VirtualLookup::Static, "");
|
2016-10-26 11:08:00 +00:00
|
|
|
utils().pushCombinedFunctionEntryLabel(*function);
|
2020-09-16 09:00:07 +00:00
|
|
|
}
|
2016-10-26 11:08:00 +00:00
|
|
|
else
|
|
|
|
solAssert(false, "Function not found in member access");
|
|
|
|
break;
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::Event:
|
2016-10-26 11:08:00 +00:00
|
|
|
if (!dynamic_cast<EventDefinition const*>(_memberAccess.annotation().referencedDeclaration))
|
|
|
|
solAssert(false, "event not found");
|
|
|
|
// no-op, because the parent node will do the job
|
|
|
|
break;
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::DelegateCall:
|
2018-08-15 15:11:43 +00:00
|
|
|
_memberAccess.expression().accept(*this);
|
|
|
|
m_context << funType->externalIdentifier();
|
|
|
|
break;
|
|
|
|
case FunctionType::Kind::External:
|
|
|
|
case FunctionType::Kind::Creation:
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::Send:
|
2017-08-01 09:32:23 +00:00
|
|
|
case FunctionType::Kind::BareCall:
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::BareCallCode:
|
|
|
|
case FunctionType::Kind::BareDelegateCall:
|
2018-08-15 12:40:20 +00:00
|
|
|
case FunctionType::Kind::BareStaticCall:
|
2017-03-16 11:58:17 +00:00
|
|
|
case FunctionType::Kind::Transfer:
|
|
|
|
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:
|
2016-10-26 11:08:00 +00:00
|
|
|
default:
|
|
|
|
solAssert(false, "unsupported member function");
|
2016-04-26 23:02:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-15 13:33:39 +00:00
|
|
|
else if (dynamic_cast<TypeType const*>(_memberAccess.annotation().type))
|
2016-10-21 08:05:36 +00:00
|
|
|
{
|
|
|
|
// no-op
|
|
|
|
}
|
2016-04-26 23:02:10 +00:00
|
|
|
else
|
|
|
|
_memberAccess.expression().accept(*this);
|
2015-11-27 13:08:09 +00:00
|
|
|
}
|
2019-04-15 13:33:39 +00:00
|
|
|
else if (auto enumType = dynamic_cast<EnumType const*>(type->actualType()))
|
2016-04-26 23:02:10 +00:00
|
|
|
{
|
|
|
|
_memberAccess.expression().accept(*this);
|
|
|
|
m_context << enumType->memberValue(_memberAccess.memberName());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_memberAccess.expression().accept(*this);
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-07 10:42:37 +00:00
|
|
|
// Another special case for `this.f.selector` and for ``C.f.selector`` which do not need the address.
|
|
|
|
// There are other uses of `.selector` which do need the address, but we want these
|
|
|
|
// specific uses to be pure expressions.
|
2018-02-13 08:49:50 +00:00
|
|
|
if (
|
2020-01-07 10:42:37 +00:00
|
|
|
auto const* functionType = dynamic_cast<FunctionType const*>(_memberAccess.expression().annotation().type);
|
|
|
|
functionType && member == "selector"
|
2018-02-13 08:49:50 +00:00
|
|
|
)
|
2020-01-07 10:42:37 +00:00
|
|
|
{
|
2020-01-14 13:00:51 +00:00
|
|
|
if (functionType->hasDeclaration())
|
2020-01-07 10:42:37 +00:00
|
|
|
{
|
|
|
|
m_context << functionType->externalIdentifier();
|
|
|
|
/// need to store it as bytes4
|
|
|
|
utils().leftShiftNumberOnStack(224);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (auto const* expr = dynamic_cast<MemberAccess const*>(&_memberAccess.expression()))
|
2018-02-13 08:49:50 +00:00
|
|
|
if (auto const* exprInt = dynamic_cast<Identifier const*>(&expr->expression()))
|
|
|
|
if (exprInt->name() == "this")
|
|
|
|
if (Declaration const* declaration = expr->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.");
|
|
|
|
m_context << identifier;
|
2018-09-20 14:54:57 +00:00
|
|
|
/// need to store it as bytes4
|
2018-02-13 08:49:50 +00:00
|
|
|
utils().leftShiftNumberOnStack(224);
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-07 10:42:37 +00:00
|
|
|
}
|
2019-10-14 04:25:42 +00:00
|
|
|
// Another special case for `address(this).balance`. Post-Istanbul, we can use the selfbalance
|
|
|
|
// opcode.
|
|
|
|
if (
|
|
|
|
m_context.evmVersion().hasSelfBalance() &&
|
|
|
|
member == "balance" &&
|
|
|
|
_memberAccess.expression().annotation().type->category() == Type::Category::Address
|
|
|
|
)
|
|
|
|
if (FunctionCall const* funCall = dynamic_cast<FunctionCall const*>(&_memberAccess.expression()))
|
|
|
|
if (auto const* addr = dynamic_cast<ElementaryTypeNameExpression const*>(&funCall->expression()))
|
|
|
|
if (
|
2019-10-23 11:08:17 +00:00
|
|
|
addr->type().typeName().token() == Token::Address &&
|
2019-10-14 04:25:42 +00:00
|
|
|
funCall->arguments().size() == 1
|
|
|
|
)
|
|
|
|
if (auto arg = dynamic_cast<Identifier const*>( funCall->arguments().front().get()))
|
|
|
|
if (
|
|
|
|
arg->name() == "this" &&
|
|
|
|
dynamic_cast<MagicVariableDeclaration const*>(arg->annotation().referencedDeclaration)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
m_context << Instruction::SELFBALANCE;
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-27 13:08:09 +00:00
|
|
|
|
2016-04-26 23:02:10 +00:00
|
|
|
_memberAccess.expression().accept(*this);
|
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
|
|
|
{
|
2018-07-18 17:51:24 +00:00
|
|
|
ContractType const& type = dynamic_cast<ContractType const&>(*_memberAccess.expression().annotation().type);
|
|
|
|
if (type.isSuper())
|
2015-04-15 15:40:50 +00:00
|
|
|
{
|
2018-07-18 17:51:24 +00:00
|
|
|
solAssert(!!_memberAccess.annotation().referencedDeclaration, "Referenced declaration not resolved.");
|
2020-09-16 09:00:07 +00:00
|
|
|
solAssert(*_memberAccess.annotation().requiredLookup == VirtualLookup::Super, "");
|
2018-07-18 17:51:24 +00:00
|
|
|
utils().pushCombinedFunctionEntryLabel(m_context.superFunction(
|
|
|
|
dynamic_cast<FunctionDefinition const&>(*_memberAccess.annotation().referencedDeclaration),
|
|
|
|
type.contractDefinition()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
// ordinary contract type
|
|
|
|
else 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();
|
2015-01-27 15:42:28 +00:00
|
|
|
else
|
2018-07-18 17:51:24 +00:00
|
|
|
solAssert(false, "Contract member is neither variable nor function.");
|
2019-04-17 11:40:50 +00:00
|
|
|
utils().convertType(type, type.isPayable() ? *TypeProvider::payableAddress() : *TypeProvider::address(), true);
|
2018-07-18 17:51:24 +00:00
|
|
|
m_context << identifier;
|
2015-01-27 13:32:59 +00:00
|
|
|
}
|
2017-09-19 18:02:16 +00:00
|
|
|
else
|
2018-07-18 17:51:24 +00:00
|
|
|
solAssert(false, "Invalid member access in contract");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::Integer:
|
2018-09-03 15:45:58 +00:00
|
|
|
{
|
|
|
|
solAssert(false, "Invalid member access to integer");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::Address:
|
2018-07-18 17:51:24 +00:00
|
|
|
{
|
|
|
|
if (member == "balance")
|
2014-11-26 12:19:17 +00:00
|
|
|
{
|
2018-07-18 17:51:24 +00:00
|
|
|
utils().convertType(
|
|
|
|
*_memberAccess.expression().annotation().type,
|
2019-04-17 11:40:50 +00:00
|
|
|
*TypeProvider::address(),
|
2018-07-18 17:51:24 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
m_context << Instruction::BALANCE;
|
2014-11-26 12:19:17 +00:00
|
|
|
}
|
2018-09-05 15:59:55 +00:00
|
|
|
else if ((set<string>{"send", "transfer"}).count(member))
|
|
|
|
{
|
|
|
|
solAssert(dynamic_cast<AddressType const&>(*_memberAccess.expression().annotation().type).stateMutability() == StateMutability::Payable, "");
|
|
|
|
utils().convertType(
|
|
|
|
*_memberAccess.expression().annotation().type,
|
|
|
|
AddressType(StateMutability::Payable),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if ((set<string>{"call", "callcode", "delegatecall", "staticcall"}).count(member))
|
2018-07-18 17:51:24 +00:00
|
|
|
utils().convertType(
|
|
|
|
*_memberAccess.expression().annotation().type,
|
2019-04-17 11:40:50 +00:00
|
|
|
*TypeProvider::address(),
|
2018-07-18 17:51:24 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
else
|
2018-09-03 15:45:58 +00:00
|
|
|
solAssert(false, "Invalid member access to address");
|
2014-11-21 18:14:56 +00:00
|
|
|
break;
|
2017-09-19 18:02:16 +00:00
|
|
|
}
|
2015-02-09 13:00:12 +00:00
|
|
|
case Type::Category::Function:
|
2017-06-28 17:15:41 +00:00
|
|
|
if (member == "selector")
|
2017-06-27 09:25:36 +00:00
|
|
|
{
|
|
|
|
m_context << Instruction::SWAP1 << Instruction::POP;
|
2018-09-20 14:54:57 +00:00
|
|
|
/// need to store it as bytes4
|
2017-06-27 09:25:36 +00:00
|
|
|
utils().leftShiftNumberOnStack(224);
|
|
|
|
}
|
2019-08-13 14:43:25 +00:00
|
|
|
else if (member == "address")
|
|
|
|
{
|
|
|
|
auto const& functionType = dynamic_cast<FunctionType const&>(*_memberAccess.expression().annotation().type);
|
|
|
|
solAssert(functionType.kind() == FunctionType::Kind::External, "");
|
|
|
|
// stack: <address> <function_id>
|
|
|
|
m_context << Instruction::POP;
|
|
|
|
}
|
2017-06-27 09:25:36 +00:00
|
|
|
else
|
2019-02-13 15:56:46 +00:00
|
|
|
solAssert(
|
|
|
|
!!_memberAccess.expression().annotation().type->memberType(member),
|
|
|
|
"Invalid member access to function."
|
|
|
|
);
|
2015-01-12 11:47:37 +00:00
|
|
|
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")
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::COINBASE;
|
2014-11-24 12:23:58 +00:00
|
|
|
else if (member == "timestamp")
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::TIMESTAMP;
|
2015-01-19 14:34:15 +00:00
|
|
|
else if (member == "difficulty")
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DIFFICULTY;
|
2014-11-24 12:23:58 +00:00
|
|
|
else if (member == "number")
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::NUMBER;
|
2014-11-24 12:23:58 +00:00
|
|
|
else if (member == "gaslimit")
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::GASLIMIT;
|
2014-11-24 12:23:58 +00:00
|
|
|
else if (member == "sender")
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::CALLER;
|
2014-11-24 12:23:58 +00:00
|
|
|
else if (member == "value")
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::CALLVALUE;
|
2014-11-24 12:23:58 +00:00
|
|
|
else if (member == "origin")
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::ORIGIN;
|
2014-11-24 12:23:58 +00:00
|
|
|
else if (member == "gasprice")
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::GASPRICE;
|
2015-02-10 13:57:01 +00:00
|
|
|
else if (member == "data")
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(0) << Instruction::CALLDATASIZE;
|
2015-03-13 16:36:00 +00:00
|
|
|
else if (member == "sig")
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(0) << Instruction::CALLDATALOAD
|
|
|
|
<< (u256(0xffffffff) << (256 - 32)) << Instruction::AND;
|
2018-10-17 20:26:10 +00:00
|
|
|
else if (member == "gas")
|
|
|
|
solAssert(false, "Gas has been removed.");
|
2018-03-13 16:28:58 +00:00
|
|
|
else if (member == "blockhash")
|
2018-10-17 20:26:10 +00:00
|
|
|
solAssert(false, "Blockhash has been removed.");
|
2019-01-10 15:44:31 +00:00
|
|
|
else if (member == "creationCode" || member == "runtimeCode")
|
|
|
|
{
|
|
|
|
TypePointer arg = dynamic_cast<MagicType const&>(*_memberAccess.expression().annotation().type).typeArgument();
|
|
|
|
ContractDefinition const& contract = dynamic_cast<ContractType const&>(*arg).contractDefinition();
|
|
|
|
utils().fetchFreeMemoryPointer();
|
|
|
|
m_context << Instruction::DUP1 << u256(32) << Instruction::ADD;
|
|
|
|
utils().copyContractCodeToMemory(contract, member == "creationCode");
|
|
|
|
// Stack: start end
|
|
|
|
m_context.appendInlineAssembly(
|
|
|
|
Whiskers(R"({
|
|
|
|
mstore(start, sub(end, add(start, 0x20)))
|
2019-01-28 10:59:18 +00:00
|
|
|
mstore(<free>, and(add(end, 31), not(31)))
|
2019-01-10 15:44:31 +00:00
|
|
|
})")("free", to_string(CompilerUtils::freeMemoryPointer)).render(),
|
|
|
|
{"start", "end"}
|
|
|
|
);
|
|
|
|
m_context << Instruction::POP;
|
|
|
|
}
|
2019-01-22 16:15:55 +00:00
|
|
|
else if (member == "name")
|
|
|
|
{
|
|
|
|
TypePointer arg = dynamic_cast<MagicType const&>(*_memberAccess.expression().annotation().type).typeArgument();
|
|
|
|
ContractDefinition const& contract = dynamic_cast<ContractType const&>(*arg).contractDefinition();
|
2019-03-13 13:27:25 +00:00
|
|
|
utils().allocateMemory(((contract.name().length() + 31) / 32) * 32 + 32);
|
2019-01-22 16:15:55 +00:00
|
|
|
// store string length
|
|
|
|
m_context << u256(contract.name().length()) << Instruction::DUP2 << Instruction::MSTORE;
|
|
|
|
// adjust pointer
|
|
|
|
m_context << Instruction::DUP1 << u256(32) << Instruction::ADD;
|
|
|
|
utils().storeStringData(contract.name());
|
|
|
|
}
|
2020-04-08 22:08:49 +00:00
|
|
|
else if (member == "interfaceId")
|
|
|
|
{
|
|
|
|
TypePointer arg = dynamic_cast<MagicType const&>(*_memberAccess.expression().annotation().type).typeArgument();
|
|
|
|
ContractDefinition const& contract = dynamic_cast<ContractType const&>(*arg).contractDefinition();
|
2020-09-14 10:39:38 +00:00
|
|
|
m_context << (u256{contract.interfaceId()} << (256 - 32));
|
2020-04-08 22:08:49 +00:00
|
|
|
}
|
2020-05-07 16:24:37 +00:00
|
|
|
else if (member == "min" || member == "max")
|
|
|
|
{
|
|
|
|
MagicType const* arg = dynamic_cast<MagicType const*>(_memberAccess.expression().annotation().type);
|
|
|
|
IntegerType const* integerType = dynamic_cast<IntegerType const*>(arg->typeArgument());
|
|
|
|
|
|
|
|
if (member == "min")
|
|
|
|
m_context << integerType->min();
|
|
|
|
else
|
|
|
|
m_context << integerType->max();
|
|
|
|
}
|
2019-04-29 12:27:10 +00:00
|
|
|
else if ((set<string>{"encode", "encodePacked", "encodeWithSelector", "encodeWithSignature", "decode"}).count(member))
|
2019-04-26 13:01:35 +00:00
|
|
|
{
|
|
|
|
// no-op
|
|
|
|
}
|
2014-11-24 12:23:58 +00:00
|
|
|
else
|
2017-07-19 01:19:00 +00:00
|
|
|
solAssert(false, "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);
|
2019-01-17 22:08:05 +00:00
|
|
|
TypePointer const& memberType = _memberAccess.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);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << offsets.first << Instruction::ADD << u256(offsets.second);
|
2015-06-26 16:35:43 +00:00
|
|
|
setLValueToStorageItem(_memberAccess);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DataLocation::Memory:
|
|
|
|
{
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << type.memoryOffsetOfMember(member) << Instruction::ADD;
|
2019-01-17 22:08:05 +00:00
|
|
|
setLValue<MemoryItem>(_memberAccess, *memberType);
|
2015-06-26 16:35:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-02-05 19:29:57 +00:00
|
|
|
case DataLocation::CallData:
|
|
|
|
{
|
2019-03-06 15:35:05 +00:00
|
|
|
if (_memberAccess.annotation().type->isDynamicallyEncoded())
|
2019-02-05 19:29:57 +00:00
|
|
|
{
|
2019-03-06 15:35:05 +00:00
|
|
|
m_context << Instruction::DUP1;
|
|
|
|
m_context << type.calldataOffsetOfMember(member) << Instruction::ADD;
|
2019-01-17 22:08:05 +00:00
|
|
|
CompilerUtils(m_context).accessCalldataTail(*memberType);
|
2019-02-05 19:29:57 +00:00
|
|
|
}
|
|
|
|
else
|
2019-03-06 15:35:05 +00:00
|
|
|
{
|
|
|
|
m_context << type.calldataOffsetOfMember(member) << Instruction::ADD;
|
|
|
|
// For non-value types the calldata offset is returned directly.
|
2019-01-17 22:08:05 +00:00
|
|
|
if (memberType->isValueType())
|
2019-03-06 15:35:05 +00:00
|
|
|
{
|
2019-01-17 22:08:05 +00:00
|
|
|
solAssert(memberType->calldataEncodedSize() > 0, "");
|
|
|
|
solAssert(memberType->storageBytes() <= 32, "");
|
|
|
|
if (memberType->storageBytes() < 32 && m_context.experimentalFeatureActive(ExperimentalFeature::ABIEncoderV2))
|
|
|
|
{
|
|
|
|
m_context << u256(32);
|
|
|
|
CompilerUtils(m_context).abiDecodeV2({memberType}, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
CompilerUtils(m_context).loadFromMemoryDynamic(*memberType, true, true, false);
|
2019-03-06 15:35:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
solAssert(
|
2019-01-17 22:08:05 +00:00
|
|
|
memberType->category() == Type::Category::Array ||
|
|
|
|
memberType->category() == Type::Category::Struct,
|
2019-03-06 15:35:05 +00:00
|
|
|
""
|
|
|
|
);
|
|
|
|
}
|
2019-02-05 19:29:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-06-26 16:35:43 +00:00
|
|
|
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-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:
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::POP;
|
2015-10-12 08:11:58 +00:00
|
|
|
break;
|
|
|
|
case DataLocation::Storage:
|
2019-09-18 12:58:20 +00:00
|
|
|
ArrayUtils(m_context).retrieveLength(type);
|
|
|
|
m_context << Instruction::SWAP1 << Instruction::POP;
|
2015-10-12 08:11:58 +00:00
|
|
|
break;
|
|
|
|
case DataLocation::Memory:
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::MLOAD;
|
2015-10-12 08:11:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-02-16 16:33:13 +00:00
|
|
|
}
|
2018-04-04 16:21:06 +00:00
|
|
|
else if (member == "push" || member == "pop")
|
2015-10-12 08:11:58 +00:00
|
|
|
{
|
2015-10-13 14:55:59 +00:00
|
|
|
solAssert(
|
2018-03-09 16:46:24 +00:00
|
|
|
type.isDynamicallySized() &&
|
|
|
|
type.location() == DataLocation::Storage &&
|
|
|
|
type.category() == Type::Category::Array,
|
2018-04-04 16:21:06 +00:00
|
|
|
"Tried to use ." + member + "() on a non-dynamically sized array"
|
2018-03-09 16:46:24 +00:00
|
|
|
);
|
|
|
|
}
|
2015-10-12 08:11:58 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-06-16 14:49:31 +00:00
|
|
|
case Type::Category::Module:
|
|
|
|
{
|
|
|
|
Type::Category category = _memberAccess.annotation().type->category();
|
|
|
|
solAssert(
|
|
|
|
category == Type::Category::TypeType ||
|
2020-05-04 16:38:00 +00:00
|
|
|
category == Type::Category::Module ||
|
|
|
|
category == Type::Category::Function,
|
2020-06-16 14:49:31 +00:00
|
|
|
""
|
|
|
|
);
|
2020-05-04 16:38:00 +00:00
|
|
|
if (auto funType = dynamic_cast<FunctionType const*>(_memberAccess.annotation().type))
|
|
|
|
{
|
|
|
|
auto const* funDef = dynamic_cast<FunctionDefinition const*>(_memberAccess.annotation().referencedDeclaration);
|
|
|
|
solAssert(funDef && funDef->isFree(), "");
|
|
|
|
solAssert(funType->kind() == FunctionType::Kind::Internal, "");
|
2020-09-16 09:00:07 +00:00
|
|
|
solAssert(*_memberAccess.annotation().requiredLookup == VirtualLookup::Static, "");
|
2020-05-04 16:38:00 +00:00
|
|
|
utils().pushCombinedFunctionEntryLabel(*funDef);
|
|
|
|
}
|
2020-06-16 14:49:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-11-21 18:14:56 +00:00
|
|
|
default:
|
2017-07-19 01:19:00 +00:00
|
|
|
solAssert(false, "Member access to unknown type.");
|
2014-11-21 18:14:56 +00:00
|
|
|
}
|
2016-04-26 23:02:10 +00:00
|
|
|
return false;
|
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
|
|
|
|
2019-09-03 16:30:00 +00:00
|
|
|
switch (baseType.category())
|
2015-02-22 18:15:41 +00:00
|
|
|
{
|
2019-09-03 16:30:00 +00:00
|
|
|
case Type::Category::Mapping:
|
2015-08-03 16:09:39 +00:00
|
|
|
{
|
2019-09-03 16:30:00 +00:00
|
|
|
// stack: storage_base_ref
|
|
|
|
TypePointer keyType = dynamic_cast<MappingType const&>(baseType).keyType();
|
|
|
|
solAssert(_indexAccess.indexExpression(), "Index expression expected.");
|
|
|
|
if (keyType->isDynamicallySized())
|
|
|
|
{
|
|
|
|
_indexAccess.indexExpression()->accept(*this);
|
|
|
|
utils().fetchFreeMemoryPointer();
|
|
|
|
// stack: base index mem
|
|
|
|
// note: the following operations must not allocate memory!
|
|
|
|
utils().packedEncode(
|
|
|
|
TypePointers{_indexAccess.indexExpression()->annotation().type},
|
|
|
|
TypePointers{keyType}
|
|
|
|
);
|
|
|
|
m_context << Instruction::SWAP1;
|
|
|
|
utils().storeInMemoryDynamic(*TypeProvider::uint256());
|
|
|
|
utils().toSizeAfterFreeMemoryPointer();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_context << u256(0); // memory position
|
|
|
|
appendExpressionCopyToMemory(*keyType, *_indexAccess.indexExpression());
|
|
|
|
m_context << Instruction::SWAP1;
|
|
|
|
solAssert(CompilerUtils::freeMemoryPointer >= 0x40, "");
|
|
|
|
utils().storeInMemoryDynamic(*TypeProvider::uint256());
|
|
|
|
m_context << u256(0);
|
|
|
|
}
|
|
|
|
m_context << Instruction::KECCAK256;
|
2015-08-03 16:09:39 +00:00
|
|
|
m_context << u256(0);
|
2019-09-03 16:30:00 +00:00
|
|
|
setLValueToStorageItem(_indexAccess);
|
|
|
|
break;
|
2015-08-03 16:09:39 +00:00
|
|
|
}
|
2019-09-03 16:30:00 +00:00
|
|
|
case Type::Category::ArraySlice:
|
|
|
|
{
|
|
|
|
auto const& arrayType = dynamic_cast<ArraySliceType const&>(baseType).arrayType();
|
2020-05-13 09:00:47 +00:00
|
|
|
solAssert(
|
|
|
|
arrayType.location() == DataLocation::CallData &&
|
|
|
|
arrayType.isDynamicallySized() &&
|
|
|
|
!arrayType.baseType()->isDynamicallyEncoded(),
|
|
|
|
""
|
|
|
|
);
|
2019-09-03 16:30:00 +00:00
|
|
|
solAssert(_indexAccess.indexExpression(), "Index expression expected.");
|
2015-02-23 17:21:17 +00:00
|
|
|
|
2019-09-03 16:30:00 +00:00
|
|
|
acceptAndConvert(*_indexAccess.indexExpression(), *TypeProvider::uint256(), true);
|
|
|
|
ArrayUtils(m_context).accessCallDataArrayElement(arrayType);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
case Type::Category::Array:
|
2015-02-23 17:21:17 +00:00
|
|
|
{
|
2019-09-03 16:30:00 +00:00
|
|
|
ArrayType const& arrayType = dynamic_cast<ArrayType const&>(baseType);
|
|
|
|
solAssert(_indexAccess.indexExpression(), "Index expression expected.");
|
|
|
|
|
|
|
|
acceptAndConvert(*_indexAccess.indexExpression(), *TypeProvider::uint256(), true);
|
|
|
|
// stack layout: <base_ref> [<length>] <index>
|
|
|
|
switch (arrayType.location())
|
2015-05-28 14:20:50 +00:00
|
|
|
{
|
2019-09-03 16:30:00 +00:00
|
|
|
case DataLocation::Storage:
|
|
|
|
ArrayUtils(m_context).accessIndex(arrayType);
|
|
|
|
if (arrayType.isByteArray())
|
|
|
|
{
|
|
|
|
solAssert(!arrayType.isString(), "Index access to string is not allowed.");
|
|
|
|
setLValue<StorageByteArrayElement>(_indexAccess);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
setLValueToStorageItem(_indexAccess);
|
|
|
|
break;
|
|
|
|
case DataLocation::Memory:
|
|
|
|
ArrayUtils(m_context).accessIndex(arrayType);
|
|
|
|
setLValue<MemoryItem>(_indexAccess, *_indexAccess.annotation().type, !arrayType.isByteArray());
|
|
|
|
break;
|
|
|
|
case DataLocation::CallData:
|
|
|
|
ArrayUtils(m_context).accessCallDataArrayElement(arrayType);
|
|
|
|
break;
|
2015-05-28 14:20:50 +00:00
|
|
|
}
|
2015-06-25 15:51:01 +00:00
|
|
|
break;
|
2019-09-03 16:30:00 +00:00
|
|
|
}
|
|
|
|
case Type::Category::FixedBytes:
|
|
|
|
{
|
|
|
|
FixedBytesType const& fixedBytesType = dynamic_cast<FixedBytesType const&>(baseType);
|
|
|
|
solAssert(_indexAccess.indexExpression(), "Index expression expected.");
|
|
|
|
|
|
|
|
acceptAndConvert(*_indexAccess.indexExpression(), *TypeProvider::uint256(), true);
|
|
|
|
// stack layout: <value> <index>
|
|
|
|
// check out-of-bounds access
|
|
|
|
m_context << u256(fixedBytesType.numBytes());
|
|
|
|
m_context << Instruction::DUP2 << Instruction::LT << Instruction::ISZERO;
|
|
|
|
// out-of-bounds access throws exception
|
|
|
|
m_context.appendConditionalInvalid();
|
|
|
|
|
|
|
|
m_context << Instruction::BYTE;
|
|
|
|
utils().leftShiftNumberOnStack(256 - 8);
|
2015-06-25 15:51:01 +00:00
|
|
|
break;
|
2019-09-03 16:30:00 +00:00
|
|
|
}
|
|
|
|
case 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-06-25 15:51:01 +00:00
|
|
|
break;
|
2015-02-23 17:21:17 +00:00
|
|
|
}
|
2019-09-03 16:30:00 +00:00
|
|
|
default:
|
|
|
|
solAssert(false, "Index access only allowed for mappings or arrays.");
|
|
|
|
break;
|
2015-02-22 18:15:41 +00:00
|
|
|
}
|
2019-09-03 16:30:00 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExpressionCompiler::visit(IndexRangeAccess const& _indexAccess)
|
|
|
|
{
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _indexAccess);
|
|
|
|
_indexAccess.baseExpression().accept(*this);
|
2020-03-06 19:59:34 +00:00
|
|
|
// stack: offset length
|
2019-09-03 16:30:00 +00:00
|
|
|
|
|
|
|
Type const& baseType = *_indexAccess.baseExpression().annotation().type;
|
|
|
|
|
|
|
|
ArrayType const *arrayType = dynamic_cast<ArrayType const*>(&baseType);
|
|
|
|
if (!arrayType)
|
|
|
|
if (ArraySliceType const* sliceType = dynamic_cast<ArraySliceType const*>(&baseType))
|
|
|
|
arrayType = &sliceType->arrayType();
|
|
|
|
|
|
|
|
solAssert(arrayType, "");
|
2020-05-13 09:00:47 +00:00
|
|
|
solUnimplementedAssert(
|
|
|
|
arrayType->location() == DataLocation::CallData &&
|
|
|
|
arrayType->isDynamicallySized() &&
|
|
|
|
!arrayType->baseType()->isDynamicallyEncoded(),
|
|
|
|
""
|
|
|
|
);
|
2019-09-03 16:30:00 +00:00
|
|
|
|
|
|
|
if (_indexAccess.startExpression())
|
|
|
|
acceptAndConvert(*_indexAccess.startExpression(), *TypeProvider::uint256());
|
2015-02-22 18:15:41 +00:00
|
|
|
else
|
2019-09-03 16:30:00 +00:00
|
|
|
m_context << u256(0);
|
2020-03-06 19:59:34 +00:00
|
|
|
// stack: offset length sliceStart
|
|
|
|
|
|
|
|
m_context << Instruction::SWAP1;
|
|
|
|
// stack: offset sliceStart length
|
|
|
|
|
2019-09-03 16:30:00 +00:00
|
|
|
if (_indexAccess.endExpression())
|
|
|
|
acceptAndConvert(*_indexAccess.endExpression(), *TypeProvider::uint256());
|
|
|
|
else
|
2020-03-06 19:59:34 +00:00
|
|
|
m_context << Instruction::DUP1;
|
|
|
|
// stack: offset sliceStart length sliceEnd
|
|
|
|
|
|
|
|
m_context << Instruction::SWAP3;
|
|
|
|
// stack: sliceEnd sliceStart length offset
|
2019-09-03 16:30:00 +00:00
|
|
|
|
2020-03-06 19:59:34 +00:00
|
|
|
m_context.callYulFunction(m_context.utilFunctions().calldataArrayIndexRangeAccess(*arrayType), 4, 2);
|
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())
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::ADDRESS;
|
2015-03-09 18:22:43 +00:00
|
|
|
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))
|
2020-09-16 09:00:07 +00:00
|
|
|
{
|
2018-03-08 10:17:08 +00:00
|
|
|
// If the identifier is called right away, this code is executed in visit(FunctionCall...), because
|
|
|
|
// we want to avoid having a reference to the runtime function entry point in the
|
|
|
|
// constructor context, since this would force the compiler to include unreferenced
|
2020-05-24 17:41:15 +00:00
|
|
|
// internal functions in the runtime context.
|
2020-09-16 09:00:07 +00:00
|
|
|
solAssert(*_identifier.annotation().requiredLookup == VirtualLookup::Virtual, "");
|
2020-03-24 17:25:59 +00:00
|
|
|
utils().pushCombinedFunctionEntryLabel(functionDef->resolveVirtual(m_context.mostDerivedContract()));
|
2020-09-16 09:00:07 +00:00
|
|
|
}
|
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())
|
2016-12-21 19:26:22 +00:00
|
|
|
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
|
|
|
|
}
|
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
|
|
|
|
}
|
2020-06-16 14:49:31 +00:00
|
|
|
else if (dynamic_cast<ImportDirective const*>(declaration))
|
|
|
|
{
|
|
|
|
// no-op
|
|
|
|
}
|
2015-01-19 18:18:34 +00:00
|
|
|
else
|
|
|
|
{
|
2017-07-19 01:19:00 +00:00
|
|
|
solAssert(false, "Identifier type not expected in expression context.");
|
2015-01-19 18:18:34 +00:00
|
|
|
}
|
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;
|
2018-09-03 13:01:15 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
switch (type->category())
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2016-03-29 20:08:51 +00:00
|
|
|
case Type::Category::RationalNumber:
|
2015-02-09 13:00:12 +00:00
|
|
|
case Type::Category::Bool:
|
2018-09-03 15:45:58 +00:00
|
|
|
case Type::Category::Address:
|
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:
|
2016-11-17 23:22:09 +00:00
|
|
|
solUnimplemented("Only integer, boolean and string literals implemented for now.");
|
2016-01-11 20:25:59 +00:00
|
|
|
}
|
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
|
|
|
{
|
2018-10-22 14:48:21 +00:00
|
|
|
Token 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);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1;
|
2015-02-09 13:00:12 +00:00
|
|
|
if (c_op == Token::And)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::ISZERO;
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem endLabel = m_context.appendConditionalJump();
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << 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;
|
|
|
|
}
|
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
void ExpressionCompiler::appendCompareOperatorCode(Token _operator, Type const& _type)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2017-06-23 16:37:15 +00:00
|
|
|
solAssert(_type.sizeOnStack() == 1, "Comparison of multi-slot types.");
|
2015-02-10 08:52:19 +00:00
|
|
|
if (_operator == Token::Equal || _operator == Token::NotEqual)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2016-11-10 17:16:21 +00:00
|
|
|
if (FunctionType const* funType = dynamic_cast<decltype(funType)>(&_type))
|
|
|
|
{
|
2017-03-16 11:58:17 +00:00
|
|
|
if (funType->kind() == FunctionType::Kind::Internal)
|
2016-11-10 17:16:21 +00:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::EQ;
|
2015-02-10 08:52:19 +00:00
|
|
|
if (_operator == Token::NotEqual)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << 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 <<
|
2016-04-04 11:41:35 +00:00
|
|
|
(isSigned ? Instruction::SLT : Instruction::LT) <<
|
|
|
|
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 <<
|
2016-04-04 11:41:35 +00:00
|
|
|
(isSigned ? Instruction::SGT : Instruction::GT) <<
|
|
|
|
Instruction::ISZERO;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::GreaterThan:
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << (isSigned ? Instruction::SGT : Instruction::GT);
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::LessThan:
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << (isSigned ? Instruction::SLT : Instruction::LT);
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-07-19 01:19:00 +00:00
|
|
|
solAssert(false, "Unknown comparison operator.");
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
void ExpressionCompiler::appendOrdinaryBinaryOperatorCode(Token _operator, Type const& _type)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2018-10-22 14:48:21 +00:00
|
|
|
if (TokenTraits::isArithmeticOp(_operator))
|
2014-10-30 00:20:32 +00:00
|
|
|
appendArithmeticOperatorCode(_operator, _type);
|
2018-10-22 14:48:21 +00:00
|
|
|
else if (TokenTraits::isBitOp(_operator))
|
2014-10-30 00:20:32 +00:00
|
|
|
appendBitOperatorCode(_operator);
|
|
|
|
else
|
2017-07-19 01:19:00 +00:00
|
|
|
solAssert(false, "Unknown binary operator.");
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
void ExpressionCompiler::appendArithmeticOperatorCode(Token _operator, Type const& _type)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2016-05-10 12:57:29 +00:00
|
|
|
if (_type.category() == Type::Category::FixedPoint)
|
2016-11-14 20:41:58 +00:00
|
|
|
solUnimplemented("Not yet implemented - FixedPointType.");
|
2016-05-10 12:57:29 +00:00
|
|
|
|
2018-07-11 12:53:34 +00:00
|
|
|
IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
|
|
|
|
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:
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::ADD;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::Sub:
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SUB;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::Mul:
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::MUL;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
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;
|
2017-01-22 19:49:12 +00:00
|
|
|
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);
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2016-08-12 14:50:37 +00:00
|
|
|
}
|
2014-10-30 00:20:32 +00:00
|
|
|
default:
|
2017-07-19 01:19:00 +00:00
|
|
|
solAssert(false, "Unknown arithmetic operator.");
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
void ExpressionCompiler::appendBitOperatorCode(Token _operator)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
|
|
|
switch (_operator)
|
|
|
|
{
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::BitOr:
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::OR;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::BitAnd:
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::AND;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2015-02-09 13:00:12 +00:00
|
|
|
case Token::BitXor:
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::XOR;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-07-19 01:19:00 +00:00
|
|
|
solAssert(false, "Unknown bit operator.");
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
void ExpressionCompiler::appendShiftOperatorCode(Token _operator, Type const& _valueType, Type const& _shiftAmountType)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2016-12-06 22:45:17 +00:00
|
|
|
// stack: shift_amount value_to_shift
|
2016-04-29 23:15:22 +00:00
|
|
|
|
2016-12-06 22:45:17 +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
|
2016-12-06 22:45:17 +00:00
|
|
|
solAssert(dynamic_cast<FixedBytesType const*>(&_valueType), "Only integer and fixed bytes type supported for shifts.");
|
2016-04-29 23:15:22 +00:00
|
|
|
|
2016-12-06 22:45:17 +00:00
|
|
|
// The amount can be a RationalNumberType too.
|
|
|
|
if (auto amountType = dynamic_cast<RationalNumberType const*>(&_shiftAmountType))
|
2016-04-29 23:15:22 +00:00
|
|
|
{
|
2016-12-06 22:45:17 +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
|
|
|
}
|
2016-12-06 22:45:17 +00:00
|
|
|
else if (auto amountType = dynamic_cast<IntegerType const*>(&_shiftAmountType))
|
2020-07-23 14:45:16 +00:00
|
|
|
solAssert(!amountType->isSigned(), "");
|
2016-04-29 23:15:22 +00:00
|
|
|
else
|
2016-12-06 22:45:17 +00:00
|
|
|
solAssert(false, "Invalid shift amount type.");
|
2016-04-29 23:15:22 +00:00
|
|
|
|
2018-03-29 08:56:51 +00:00
|
|
|
m_context << Instruction::SWAP1;
|
|
|
|
// stack: value_to_shift shift_amount
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
switch (_operator)
|
|
|
|
{
|
|
|
|
case Token::SHL:
|
2018-03-29 08:56:51 +00:00
|
|
|
if (m_context.evmVersion().hasBitwiseShifting())
|
|
|
|
m_context << Instruction::SHL;
|
|
|
|
else
|
|
|
|
m_context << u256(2) << Instruction::EXP << Instruction::MUL;
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
|
|
|
case Token::SAR:
|
2018-05-07 17:09:52 +00:00
|
|
|
if (m_context.evmVersion().hasBitwiseShifting())
|
|
|
|
m_context << (c_valueSigned ? Instruction::SAR : Instruction::SHR);
|
2018-03-29 08:56:51 +00:00
|
|
|
else
|
2018-05-07 17:09:52 +00:00
|
|
|
{
|
|
|
|
if (c_valueSigned)
|
2018-06-07 15:08:42 +00:00
|
|
|
// In the following assembly snippet, xor_mask will be zero, if value_to_shift is positive.
|
2018-07-10 07:18:59 +00:00
|
|
|
// Therefore xor'ing with xor_mask is the identity and the computation reduces to
|
2018-06-07 15:08:42 +00:00
|
|
|
// div(value_to_shift, exp(2, shift_amount)), which is correct, since for positive values
|
|
|
|
// arithmetic right shift is dividing by a power of two (which, as a bitwise operation, results
|
|
|
|
// in discarding bits on the right and filling with zeros from the left).
|
|
|
|
// For negative values arithmetic right shift, viewed as a bitwise operation, discards bits to the
|
|
|
|
// right and fills in ones from the left. This is achieved as follows:
|
|
|
|
// If value_to_shift is negative, then xor_mask will have all bits set, so xor'ing with xor_mask
|
|
|
|
// will flip all bits. First all bits in value_to_shift are flipped. As for the positive case,
|
|
|
|
// dividing by a power of two using integer arithmetic results in discarding bits to the right
|
|
|
|
// and filling with zeros from the left. Flipping all bits in the result again, turns all zeros
|
|
|
|
// on the left to ones and restores the non-discarded, shifted bits to their original value (they
|
|
|
|
// have now been flipped twice). In summary we now have discarded bits to the right and filled with
|
|
|
|
// ones from the left, i.e. we have performed an arithmetic right shift.
|
2018-05-07 17:09:52 +00:00
|
|
|
m_context.appendInlineAssembly(R"({
|
|
|
|
let xor_mask := sub(0, slt(value_to_shift, 0))
|
|
|
|
value_to_shift := xor(div(xor(value_to_shift, xor_mask), exp(2, shift_amount)), xor_mask)
|
|
|
|
})", {"value_to_shift", "shift_amount"});
|
|
|
|
else
|
|
|
|
m_context.appendInlineAssembly(R"({
|
|
|
|
value_to_shift := div(value_to_shift, exp(2, shift_amount))
|
|
|
|
})", {"value_to_shift", "shift_amount"});
|
|
|
|
m_context << Instruction::POP;
|
|
|
|
|
|
|
|
}
|
2014-10-30 00:20:32 +00:00
|
|
|
break;
|
2016-04-29 23:14:05 +00:00
|
|
|
case Token::SHR:
|
2014-10-30 00:20:32 +00:00
|
|
|
default:
|
2017-07-19 01:19:00 +00:00
|
|
|
solAssert(false, "Unknown shift operator.");
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 14:16:39 +00:00
|
|
|
void ExpressionCompiler::appendExpOperatorCode(Type const& _valueType, Type const& _exponentType)
|
|
|
|
{
|
|
|
|
solAssert(_valueType.category() == Type::Category::Integer, "");
|
|
|
|
solAssert(!dynamic_cast<IntegerType const&>(_exponentType).isSigned(), "");
|
|
|
|
|
|
|
|
m_context << Instruction::EXP;
|
|
|
|
}
|
|
|
|
|
2015-05-15 16:02:09 +00:00
|
|
|
void ExpressionCompiler::appendExternalFunctionCall(
|
|
|
|
FunctionType const& _functionType,
|
2019-09-05 18:02:58 +00:00
|
|
|
vector<ASTPointer<Expression const>> const& _arguments,
|
|
|
|
bool _tryCall
|
2015-05-15 16:02:09 +00:00
|
|
|
)
|
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;
|
2020-06-02 13:42:46 +00:00
|
|
|
unsigned gasValueSize = (_functionType.gasSet() ? 1u : 0u) + (_functionType.valueSet() ? 1u : 0u);
|
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());
|
|
|
|
|
2017-03-16 11:58:17 +00:00
|
|
|
auto funKind = _functionType.kind();
|
2018-08-15 12:40:20 +00:00
|
|
|
|
|
|
|
solAssert(funKind != FunctionType::Kind::BareStaticCall || m_context.evmVersion().hasStaticCall(), "");
|
2018-09-04 15:28:52 +00:00
|
|
|
|
2018-10-17 20:26:10 +00:00
|
|
|
solAssert(funKind != FunctionType::Kind::BareCallCode, "Callcode has been removed.");
|
|
|
|
|
|
|
|
bool returnSuccessConditionAndReturndata = funKind == FunctionType::Kind::BareCall || funKind == FunctionType::Kind::BareDelegateCall || funKind == FunctionType::Kind::BareStaticCall;
|
2017-03-16 11:58:17 +00:00
|
|
|
bool isDelegateCall = funKind == FunctionType::Kind::BareDelegateCall || funKind == FunctionType::Kind::DelegateCall;
|
2018-08-15 12:40:20 +00:00
|
|
|
bool useStaticCall = funKind == FunctionType::Kind::BareStaticCall || (_functionType.stateMutability() <= StateMutability::View && m_context.evmVersion().hasStaticCall());
|
2015-06-05 15:32:13 +00:00
|
|
|
|
2019-09-05 18:02:58 +00:00
|
|
|
if (_tryCall)
|
|
|
|
{
|
|
|
|
solAssert(!returnSuccessConditionAndReturndata, "");
|
|
|
|
solAssert(!_functionType.isBareCall(), "");
|
|
|
|
}
|
|
|
|
|
2020-02-10 10:58:36 +00:00
|
|
|
ReturnInfo const returnInfo{m_context.evmVersion(), _functionType};
|
|
|
|
bool const haveReturndatacopy = m_context.evmVersion().supportsReturndata();
|
|
|
|
unsigned const retSize = returnInfo.estimatedReturnSize;
|
|
|
|
bool const dynamicReturnSize = returnInfo.dynamicReturnSize;
|
|
|
|
TypePointers const& returnTypes = returnInfo.returnTypes;
|
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();
|
|
|
|
if (_functionType.bound())
|
|
|
|
{
|
|
|
|
argumentTypes.push_back(_functionType.selfType());
|
|
|
|
parameterTypes.insert(parameterTypes.begin(), _functionType.selfType());
|
|
|
|
}
|
2018-05-18 16:03:41 +00:00
|
|
|
for (size_t i = 0; i < _arguments.size(); ++i)
|
2015-06-05 22:57:51 +00:00
|
|
|
{
|
|
|
|
_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
|
|
|
|
2017-03-16 11:58:17 +00:00
|
|
|
if (funKind == FunctionType::Kind::ECRecover)
|
2016-08-07 17:40:19 +00:00
|
|
|
{
|
|
|
|
// 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();
|
2017-07-28 08:06:49 +00:00
|
|
|
m_context << u256(0) << Instruction::DUP2 << Instruction::MSTORE;
|
2016-08-07 17:40:19 +00:00
|
|
|
m_context << u256(32) << Instruction::ADD;
|
|
|
|
utils().storeFreeMemoryPointer();
|
|
|
|
}
|
|
|
|
|
2018-02-26 17:25:36 +00:00
|
|
|
if (!m_context.evmVersion().canOverchargeGasForCall())
|
2016-09-16 10:56:52 +00:00
|
|
|
{
|
2018-02-26 17:25:36 +00:00
|
|
|
// 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();
|
2018-07-10 07:18:59 +00:00
|
|
|
// This touches too much, but that way we save some rounding arithmetic
|
2018-02-26 17:25:36 +00:00
|
|
|
m_context << u256(retSize) << Instruction::ADD << Instruction::MSTORE;
|
|
|
|
}
|
2016-09-16 10:56:52 +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();
|
2018-05-18 16:03:41 +00:00
|
|
|
if (!_functionType.isBareCall())
|
2015-06-05 22:57:51 +00:00
|
|
|
{
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << 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
|
|
|
}
|
2018-05-07 20:34:31 +00:00
|
|
|
|
2018-05-18 16:03:41 +00:00
|
|
|
// If the function takes arbitrary parameters or is a bare call, 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).
|
2019-02-13 16:39:16 +00:00
|
|
|
bool encodeInPlace = _functionType.takesArbitraryParameters() || _functionType.isBareCall();
|
|
|
|
if (_functionType.kind() == FunctionType::Kind::ECRecover)
|
|
|
|
// This would be the only combination of padding and in-place encoding,
|
|
|
|
// but all parameters of ecrecover are value types anyway.
|
|
|
|
encodeInPlace = false;
|
2019-02-18 17:30:19 +00:00
|
|
|
bool encodeForLibraryCall = funKind == FunctionType::Kind::DelegateCall;
|
2018-05-18 16:03:41 +00:00
|
|
|
utils().encodeToMemory(
|
|
|
|
argumentTypes,
|
|
|
|
parameterTypes,
|
|
|
|
_functionType.padArguments(),
|
2019-02-13 16:39:16 +00:00
|
|
|
encodeInPlace,
|
2019-02-18 17:30:19 +00:00
|
|
|
encodeForLibraryCall
|
2018-05-18 16:03:41 +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
|
|
|
|
|
2016-08-07 17:40:19 +00:00
|
|
|
// Output data will replace input data, unless we have ECRecover (then, output
|
|
|
|
// area will be 32 bytes just before input area).
|
2015-06-05 22:57:51 +00:00
|
|
|
// put on stack: <size of output> <memory pos of output> <size of input> <memory pos of input>
|
|
|
|
m_context << u256(retSize);
|
2016-08-07 17:40:19 +00:00
|
|
|
utils().fetchFreeMemoryPointer(); // This is the start of input
|
2017-03-16 11:58:17 +00:00
|
|
|
if (funKind == FunctionType::Kind::ECRecover)
|
2016-08-07 17:40:19 +00:00
|
|
|
{
|
|
|
|
// 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)
|
2016-03-07 15:55:53 +00:00
|
|
|
// [value,] addr, gas (stack top)
|
|
|
|
if (isDelegateCall)
|
|
|
|
solAssert(!_functionType.valueSet(), "Value set for delegatecall");
|
2017-09-26 13:27:03 +00:00
|
|
|
else if (useStaticCall)
|
|
|
|
solAssert(!_functionType.valueSet(), "Value set for staticcall");
|
2016-03-07 15:55:53 +00:00
|
|
|
else if (_functionType.valueSet())
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << dupInstruction(m_context.baseToCurrentStackOffset(valueStackPos));
|
2014-12-10 22:01:40 +00:00
|
|
|
else
|
|
|
|
m_context << u256(0);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << dupInstruction(m_context.baseToCurrentStackOffset(contractStackPos));
|
2015-01-08 16:18:31 +00:00
|
|
|
|
2016-09-05 13:47:21 +00:00
|
|
|
bool existenceChecked = false;
|
2018-09-20 14:54:57 +00:00
|
|
|
// Check the target contract exists (has code) for non-low-level calls.
|
2018-08-23 07:39:00 +00:00
|
|
|
if (funKind == FunctionType::Kind::External || funKind == FunctionType::Kind::DelegateCall)
|
2016-08-07 17:46:11 +00:00
|
|
|
{
|
|
|
|
m_context << Instruction::DUP1 << Instruction::EXTCODESIZE << Instruction::ISZERO;
|
2020-01-22 14:48:56 +00:00
|
|
|
m_context.appendConditionalRevert(false, "Target contract does not contain code");
|
2016-09-05 13:47:21 +00:00
|
|
|
existenceChecked = true;
|
2016-08-07 17:46:11 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 11:47:37 +00:00
|
|
|
if (_functionType.gasSet())
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << dupInstruction(m_context.baseToCurrentStackOffset(gasStackPos));
|
2018-02-26 17:25:36 +00:00
|
|
|
else if (m_context.evmVersion().canOverchargeGasForCall())
|
2017-10-05 09:56:36 +00:00
|
|
|
// Send all gas (requires tangerine whistle EVM)
|
|
|
|
m_context << Instruction::GAS;
|
2015-01-12 11:47:37 +00:00
|
|
|
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.
|
2019-12-11 16:31:36 +00:00
|
|
|
u256 gasNeededByCaller = evmasm::GasCosts::callGas(m_context.evmVersion()) + 10;
|
2015-07-28 11:37:38 +00:00
|
|
|
if (_functionType.valueSet())
|
2019-12-11 16:31:36 +00:00
|
|
|
gasNeededByCaller += evmasm::GasCosts::callValueTransferGas;
|
2017-08-01 11:56:36 +00:00
|
|
|
if (!existenceChecked)
|
2019-12-11 16:31:36 +00:00
|
|
|
gasNeededByCaller += evmasm::GasCosts::callNewAccountGas; // we never know
|
2016-09-16 10:56:52 +00:00
|
|
|
m_context << gasNeededByCaller << Instruction::GAS << Instruction::SUB;
|
2015-07-28 11:37:38 +00:00
|
|
|
}
|
2017-09-26 13:27:03 +00:00
|
|
|
// Order is important here, STATICCALL might overlap with DELEGATECALL.
|
2016-03-07 15:55:53 +00:00
|
|
|
if (isDelegateCall)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DELEGATECALL;
|
2017-09-26 13:27:03 +00:00
|
|
|
else if (useStaticCall)
|
|
|
|
m_context << Instruction::STATICCALL;
|
2015-05-15 16:02:09 +00:00
|
|
|
else
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::CALL;
|
2015-05-19 10:58:12 +00:00
|
|
|
|
2015-06-05 15:38:06 +00:00
|
|
|
unsigned remainsSize =
|
2020-06-02 13:42:46 +00:00
|
|
|
2u + // contract address, input_memory_end
|
2018-05-18 16:03:41 +00:00
|
|
|
(_functionType.valueSet() ? 1 : 0) +
|
|
|
|
(_functionType.gasSet() ? 1 : 0) +
|
|
|
|
(!_functionType.isBareCall() ? 1 : 0);
|
2015-05-19 10:58:12 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem endTag = m_context.newTag();
|
2019-09-05 18:02:58 +00:00
|
|
|
|
|
|
|
if (!returnSuccessConditionAndReturndata && !_tryCall)
|
2015-06-05 12:34:10 +00:00
|
|
|
{
|
2019-09-05 18:02:58 +00:00
|
|
|
// Propagate error condition (if CALL pushes 0 on stack).
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::ISZERO;
|
2017-12-30 19:13:41 +00:00
|
|
|
m_context.appendConditionalRevert(true);
|
2015-06-05 12:34:10 +00:00
|
|
|
}
|
2019-09-05 18:02:58 +00:00
|
|
|
else
|
|
|
|
m_context << swapInstruction(remainsSize);
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().popStackSlots(remainsSize);
|
2015-06-05 12:34:10 +00:00
|
|
|
|
2019-09-05 18:02:58 +00:00
|
|
|
// Only success flag is remaining on stack.
|
|
|
|
|
|
|
|
if (_tryCall)
|
|
|
|
{
|
|
|
|
m_context << Instruction::DUP1 << Instruction::ISZERO;
|
|
|
|
m_context.appendConditionalJumpTo(endTag);
|
|
|
|
m_context << Instruction::POP;
|
|
|
|
}
|
|
|
|
|
2018-08-15 21:30:09 +00:00
|
|
|
if (returnSuccessConditionAndReturndata)
|
2015-06-05 12:34:10 +00:00
|
|
|
{
|
2018-08-15 21:30:09 +00:00
|
|
|
// success condition is already there
|
|
|
|
// The return parameter types can be empty, when this function is used as
|
|
|
|
// an internal helper function e.g. for ``send`` and ``transfer``. In that
|
|
|
|
// case we're only interested in the success condition, not the return data.
|
|
|
|
if (!_functionType.returnParameterTypes().empty())
|
2019-09-05 21:35:31 +00:00
|
|
|
utils().returnDataToArray();
|
2015-06-05 12:34:10 +00:00
|
|
|
}
|
2017-03-16 11:58:17 +00:00
|
|
|
else if (funKind == FunctionType::Kind::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
|
|
|
}
|
2017-03-16 11:58:17 +00:00
|
|
|
else if (funKind == FunctionType::Kind::ECRecover)
|
2016-08-07 17:40:19 +00:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
2018-02-20 23:40:38 +00:00
|
|
|
else if (!returnTypes.empty())
|
2015-06-05 15:32:13 +00:00
|
|
|
{
|
2015-06-15 10:10:41 +00:00
|
|
|
utils().fetchFreeMemoryPointer();
|
2018-02-21 10:07:30 +00:00
|
|
|
// Stack: return_data_start
|
|
|
|
|
|
|
|
// The old decoder did not allocate any memory (i.e. did not touch the free
|
|
|
|
// memory pointer), but kept references to the return data for
|
|
|
|
// (statically-sized) arrays
|
|
|
|
bool needToUpdateFreeMemoryPtr = false;
|
|
|
|
if (dynamicReturnSize || m_context.experimentalFeatureActive(ExperimentalFeature::ABIEncoderV2))
|
|
|
|
needToUpdateFreeMemoryPtr = true;
|
|
|
|
else
|
|
|
|
for (auto const& retType: returnTypes)
|
2019-04-15 13:33:39 +00:00
|
|
|
if (dynamic_cast<ReferenceType const*>(retType))
|
2018-02-21 10:07:30 +00:00
|
|
|
needToUpdateFreeMemoryPtr = true;
|
|
|
|
|
|
|
|
// Stack: return_data_start
|
|
|
|
if (dynamicReturnSize)
|
2015-09-23 15:31:37 +00:00
|
|
|
{
|
2018-02-21 10:07:30 +00:00
|
|
|
solAssert(haveReturndatacopy, "");
|
|
|
|
m_context.appendInlineAssembly("{ returndatacopy(return_data_start, 0, returndatasize()) }", {"return_data_start"});
|
2015-09-23 15:31:37 +00:00
|
|
|
}
|
|
|
|
else
|
2018-02-21 10:07:30 +00:00
|
|
|
solAssert(retSize > 0, "");
|
|
|
|
// Always use the actual return length, and not our calculated expected length, if returndatacopy is supported.
|
|
|
|
// This ensures it can catch badly formatted input from external calls.
|
2019-12-11 16:31:36 +00:00
|
|
|
m_context << (haveReturndatacopy ? evmasm::AssemblyItem(Instruction::RETURNDATASIZE) : u256(retSize));
|
2018-02-21 10:07:30 +00:00
|
|
|
// Stack: return_data_start return_data_size
|
|
|
|
if (needToUpdateFreeMemoryPtr)
|
|
|
|
m_context.appendInlineAssembly(R"({
|
|
|
|
// round size to the next multiple of 32
|
|
|
|
let newMem := add(start, and(add(size, 0x1f), not(0x1f)))
|
|
|
|
mstore(0x40, newMem)
|
|
|
|
})", {"start", "size"});
|
|
|
|
|
2018-06-04 10:31:18 +00:00
|
|
|
utils().abiDecode(returnTypes, true);
|
2015-06-05 22:57:51 +00:00
|
|
|
}
|
2019-09-05 18:02:58 +00:00
|
|
|
|
|
|
|
if (_tryCall)
|
|
|
|
{
|
|
|
|
// Success branch will reach this, failure branch will directly jump to endTag.
|
|
|
|
m_context << u256(1);
|
|
|
|
m_context << endTag;
|
|
|
|
}
|
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
|
|
|
{
|
2016-11-14 20:41:58 +00:00
|
|
|
solUnimplementedAssert(_expectedType.isValueType(), "Not implemented for non-value types.");
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(_expression, _expectedType, true);
|
2015-06-16 15:20:41 +00:00
|
|
|
utils().storeInMemoryDynamic(_expectedType);
|
2015-02-01 01:41:14 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 18:16:21 +00:00
|
|
|
void ExpressionCompiler::appendVariable(VariableDeclaration const& _variable, Expression const& _expression)
|
|
|
|
{
|
2020-02-27 15:13:55 +00:00
|
|
|
if (_variable.isConstant())
|
2019-09-05 11:00:07 +00:00
|
|
|
acceptAndConvert(*_variable.value(), *_variable.annotation().type);
|
2020-02-27 15:13:55 +00:00
|
|
|
else if (_variable.immutable())
|
2020-03-10 13:30:04 +00:00
|
|
|
setLValue<ImmutableItem>(_expression, _variable);
|
2020-02-27 15:13:55 +00:00
|
|
|
else
|
|
|
|
setLValueFromDeclaration(_variable, _expression);
|
2016-10-21 18:16:21 +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
|
|
|
}
|
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
bool ExpressionCompiler::cleanupNeededForOp(Type::Category _type, Token _op)
|
2016-12-06 22:45:17 +00:00
|
|
|
{
|
2018-10-22 14:48:21 +00:00
|
|
|
if (TokenTraits::isCompareOp(_op) || TokenTraits::isShiftOp(_op))
|
2016-12-06 22:45:17 +00:00
|
|
|
return true;
|
2018-09-04 14:51:22 +00:00
|
|
|
else if (_type == Type::Category::Integer && (_op == Token::Div || _op == Token::Mod || _op == Token::Exp))
|
|
|
|
// We need cleanup for EXP because 0**0 == 1, but 0**0x100 == 0
|
|
|
|
// It would suffice to clean the exponent, though.
|
2016-12-06 22:45:17 +00:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-05 11:00:07 +00:00
|
|
|
void ExpressionCompiler::acceptAndConvert(Expression const& _expression, Type const& _type, bool _cleanupNeeded)
|
|
|
|
{
|
|
|
|
_expression.accept(*this);
|
|
|
|
utils().convertType(*_expression.annotation().type, _type, _cleanupNeeded);
|
|
|
|
}
|
|
|
|
|
2015-06-15 10:10:41 +00:00
|
|
|
CompilerUtils ExpressionCompiler::utils()
|
|
|
|
{
|
|
|
|
return CompilerUtils(m_context);
|
|
|
|
}
|