2015-02-25 14:14:22 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-02-25 14:14:22 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-02-25 14:14:22 +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,
|
2015-02-25 14:14:22 +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/>.
|
2015-02-25 14:14:22 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
2016-07-14 20:56:45 +00:00
|
|
|
* LValues for use in the expression compiler.
|
2015-02-25 14:14:22 +00:00
|
|
|
*/
|
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/codegen/LValue.h>
|
2016-04-01 20:11:01 +00:00
|
|
|
#include <libevmasm/Instruction.h>
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/ast/Types.h>
|
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
#include <libsolidity/codegen/CompilerUtils.h>
|
2015-02-25 14:14:22 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace solidity;
|
|
|
|
|
|
|
|
|
2015-09-21 16:55:58 +00:00
|
|
|
StackVariable::StackVariable(CompilerContext& _compilerContext, VariableDeclaration const& _declaration):
|
2015-10-14 13:19:50 +00:00
|
|
|
LValue(_compilerContext, _declaration.annotation().type.get()),
|
2015-08-31 16:44:29 +00:00
|
|
|
m_baseStackOffset(m_context.baseStackOffsetOfVariable(_declaration)),
|
2015-10-14 13:19:50 +00:00
|
|
|
m_size(m_dataType->sizeOnStack())
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-02-25 19:27:55 +00:00
|
|
|
void StackVariable::retrieveValue(SourceLocation const& _location, bool) const
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
|
|
|
unsigned stackPos = m_context.baseToCurrentStackOffset(m_baseStackOffset);
|
2015-07-15 14:32:47 +00:00
|
|
|
if (stackPos + 1 > 16) //@todo correct this by fetching earlier or moving to memory
|
2015-06-05 23:04:55 +00:00
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
CompilerError() <<
|
|
|
|
errinfo_sourceLocation(_location) <<
|
|
|
|
errinfo_comment("Stack too deep, try removing local variables.")
|
|
|
|
);
|
2015-06-30 09:54:51 +00:00
|
|
|
solAssert(stackPos + 1 >= m_size, "Size and stack pos mismatch.");
|
2015-02-25 14:14:22 +00:00
|
|
|
for (unsigned i = 0; i < m_size; ++i)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << dupInstruction(stackPos + 1);
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 19:27:55 +00:00
|
|
|
void StackVariable::storeValue(Type const&, SourceLocation const& _location, bool _move) const
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
|
|
|
unsigned stackDiff = m_context.baseToCurrentStackOffset(m_baseStackOffset) - m_size + 1;
|
|
|
|
if (stackDiff > 16)
|
2015-06-05 23:04:55 +00:00
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
CompilerError() <<
|
|
|
|
errinfo_sourceLocation(_location) <<
|
|
|
|
errinfo_comment("Stack too deep, try removing local variables.")
|
|
|
|
);
|
2015-02-25 14:14:22 +00:00
|
|
|
else if (stackDiff > 0)
|
|
|
|
for (unsigned i = 0; i < m_size; ++i)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << swapInstruction(stackDiff) << Instruction::POP;
|
2015-02-25 14:14:22 +00:00
|
|
|
if (!_move)
|
|
|
|
retrieveValue(_location);
|
|
|
|
}
|
|
|
|
|
2015-02-25 19:27:55 +00:00
|
|
|
void StackVariable::setToZero(SourceLocation const& _location, bool) const
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2015-10-14 13:19:50 +00:00
|
|
|
CompilerUtils(m_context).pushZeroValue(*m_dataType);
|
|
|
|
storeValue(*m_dataType, _location, true);
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 15:51:01 +00:00
|
|
|
MemoryItem::MemoryItem(CompilerContext& _compilerContext, Type const& _type, bool _padded):
|
2015-10-14 13:19:50 +00:00
|
|
|
LValue(_compilerContext, &_type),
|
2015-06-25 15:51:01 +00:00
|
|
|
m_padded(_padded)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryItem::retrieveValue(SourceLocation const&, bool _remove) const
|
|
|
|
{
|
2015-10-14 13:19:50 +00:00
|
|
|
if (m_dataType->isValueType())
|
2015-06-25 15:51:01 +00:00
|
|
|
{
|
|
|
|
if (!_remove)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1;
|
2015-10-14 13:19:50 +00:00
|
|
|
CompilerUtils(m_context).loadFromMemoryDynamic(*m_dataType, false, m_padded, false);
|
2015-06-25 15:51:01 +00:00
|
|
|
}
|
|
|
|
else
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::MLOAD;
|
2015-06-25 15:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryItem::storeValue(Type const& _sourceType, SourceLocation const&, bool _move) const
|
|
|
|
{
|
|
|
|
CompilerUtils utils(m_context);
|
2015-10-14 13:19:50 +00:00
|
|
|
if (m_dataType->isValueType())
|
2015-06-25 15:51:01 +00:00
|
|
|
{
|
|
|
|
solAssert(_sourceType.isValueType(), "");
|
2015-08-31 16:44:29 +00:00
|
|
|
utils.moveIntoStack(_sourceType.sizeOnStack());
|
2015-10-14 13:19:50 +00:00
|
|
|
utils.convertType(_sourceType, *m_dataType, true);
|
2015-06-25 15:51:01 +00:00
|
|
|
if (!_move)
|
|
|
|
{
|
2015-10-14 13:19:50 +00:00
|
|
|
utils.moveToStackTop(m_dataType->sizeOnStack());
|
2015-11-16 17:32:21 +00:00
|
|
|
utils.copyToStackTop(1 + m_dataType->sizeOnStack(), m_dataType->sizeOnStack());
|
|
|
|
}
|
|
|
|
if (!m_padded)
|
|
|
|
{
|
|
|
|
solAssert(m_dataType->calldataEncodedSize(false) == 1, "Invalid non-padded type.");
|
|
|
|
if (m_dataType->category() == Type::Category::FixedBytes)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(0) << Instruction::BYTE;
|
|
|
|
m_context << Instruction::SWAP1 << Instruction::MSTORE8;
|
2015-11-16 17:32:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
utils.storeInMemoryDynamic(*m_dataType, m_padded);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-06-25 15:51:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-14 20:41:58 +00:00
|
|
|
solUnimplementedAssert(_sourceType == *m_dataType, "Conversion not implemented for assignment to memory.");
|
2015-06-25 15:51:01 +00:00
|
|
|
|
2015-10-14 13:19:50 +00:00
|
|
|
solAssert(m_dataType->sizeOnStack() == 1, "");
|
2015-06-25 15:51:01 +00:00
|
|
|
if (!_move)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2 << Instruction::SWAP1;
|
2015-06-25 15:51:01 +00:00
|
|
|
// stack: [value] value lvalue
|
|
|
|
// only store the reference
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::MSTORE;
|
2015-06-25 15:51:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryItem::setToZero(SourceLocation const&, bool _removeReference) const
|
|
|
|
{
|
|
|
|
CompilerUtils utils(m_context);
|
|
|
|
if (!_removeReference)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1;
|
2015-10-14 13:19:50 +00:00
|
|
|
utils.pushZeroValue(*m_dataType);
|
|
|
|
utils.storeInMemoryDynamic(*m_dataType, m_padded);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-06-25 15:51:01 +00:00
|
|
|
}
|
2015-02-25 14:14:22 +00:00
|
|
|
|
2015-09-21 16:55:58 +00:00
|
|
|
StorageItem::StorageItem(CompilerContext& _compilerContext, VariableDeclaration const& _declaration):
|
2015-09-16 14:56:30 +00:00
|
|
|
StorageItem(_compilerContext, *_declaration.annotation().type)
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
auto const& location = m_context.storageLocationOfVariable(_declaration);
|
2015-03-13 18:48:24 +00:00
|
|
|
m_context << location.first << u256(location.second);
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 19:27:55 +00:00
|
|
|
StorageItem::StorageItem(CompilerContext& _compilerContext, Type const& _type):
|
2015-10-14 13:19:50 +00:00
|
|
|
LValue(_compilerContext, &_type)
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2015-10-14 13:19:50 +00:00
|
|
|
if (m_dataType->isValueType())
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2016-10-14 10:27:46 +00:00
|
|
|
if (m_dataType->category() != Type::Category::Function)
|
|
|
|
solAssert(m_dataType->storageSize() == m_dataType->sizeOnStack(), "");
|
2015-10-14 13:19:50 +00:00
|
|
|
solAssert(m_dataType->storageSize() == 1, "Invalid storage size.");
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StorageItem::retrieveValue(SourceLocation const&, bool _remove) const
|
|
|
|
{
|
2015-03-11 17:09:35 +00:00
|
|
|
// stack: storage_key storage_offset
|
2015-10-14 13:19:50 +00:00
|
|
|
if (!m_dataType->isValueType())
|
2015-07-14 09:58:16 +00:00
|
|
|
{
|
2015-10-14 13:19:50 +00:00
|
|
|
solAssert(m_dataType->sizeOnStack() == 1, "Invalid storage ref size.");
|
2015-07-14 09:58:16 +00:00
|
|
|
if (_remove)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP; // remove byte offset
|
2015-07-14 09:58:16 +00:00
|
|
|
else
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2;
|
2015-07-14 09:58:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-02-25 14:14:22 +00:00
|
|
|
if (!_remove)
|
2015-03-11 17:09:35 +00:00
|
|
|
CompilerUtils(m_context).copyToStackTop(sizeOnStack(), sizeOnStack());
|
2015-10-14 13:19:50 +00:00
|
|
|
if (m_dataType->storageBytes() == 32)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP << Instruction::SLOAD;
|
2015-03-13 18:48:24 +00:00
|
|
|
else
|
|
|
|
{
|
2016-11-09 12:34:51 +00:00
|
|
|
bool cleaned = false;
|
2015-03-13 18:48:24 +00:00
|
|
|
m_context
|
2016-04-04 11:41:35 +00:00
|
|
|
<< Instruction::SWAP1 << Instruction::SLOAD << Instruction::SWAP1
|
|
|
|
<< u256(0x100) << Instruction::EXP << Instruction::SWAP1 << Instruction::DIV;
|
2016-05-10 12:57:29 +00:00
|
|
|
if (m_dataType->category() == Type::Category::FixedPoint)
|
|
|
|
// implementation should be very similar to the integer case.
|
2016-11-14 20:41:58 +00:00
|
|
|
solUnimplemented("Not yet implemented - FixedPointType.");
|
2015-10-14 13:19:50 +00:00
|
|
|
if (m_dataType->category() == Type::Category::FixedBytes)
|
2016-11-09 12:34:51 +00:00
|
|
|
{
|
2017-06-15 12:02:05 +00:00
|
|
|
CompilerUtils(m_context).leftShiftNumberOnStack(256 - 8 * m_dataType->storageBytes());
|
2016-11-09 12:34:51 +00:00
|
|
|
cleaned = true;
|
|
|
|
}
|
2015-04-16 15:43:40 +00:00
|
|
|
else if (
|
2015-10-14 13:19:50 +00:00
|
|
|
m_dataType->category() == Type::Category::Integer &&
|
|
|
|
dynamic_cast<IntegerType const&>(*m_dataType).isSigned()
|
2015-04-16 15:43:40 +00:00
|
|
|
)
|
2016-11-09 12:34:51 +00:00
|
|
|
{
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(m_dataType->storageBytes() - 1) << Instruction::SIGNEXTEND;
|
2016-11-09 12:34:51 +00:00
|
|
|
cleaned = true;
|
|
|
|
}
|
2016-11-07 19:08:05 +00:00
|
|
|
else if (FunctionType const* fun = dynamic_cast<decltype(fun)>(m_dataType))
|
|
|
|
{
|
2017-03-16 11:58:17 +00:00
|
|
|
if (fun->kind() == FunctionType::Kind::External)
|
2016-11-09 12:34:51 +00:00
|
|
|
{
|
2016-11-07 19:08:05 +00:00
|
|
|
CompilerUtils(m_context).splitExternalFunctionType(false);
|
2016-11-09 12:34:51 +00:00
|
|
|
cleaned = true;
|
|
|
|
}
|
2016-11-07 19:08:05 +00:00
|
|
|
}
|
2016-11-09 12:34:51 +00:00
|
|
|
if (!cleaned)
|
2016-10-14 10:27:46 +00:00
|
|
|
{
|
|
|
|
solAssert(m_dataType->sizeOnStack() == 1, "");
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << ((u256(0x1) << (8 * m_dataType->storageBytes())) - 1) << Instruction::AND;
|
2016-10-14 10:27:46 +00:00
|
|
|
}
|
2015-03-13 18:48:24 +00:00
|
|
|
}
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StorageItem::storeValue(Type const& _sourceType, SourceLocation const& _location, bool _move) const
|
|
|
|
{
|
2015-06-30 09:54:51 +00:00
|
|
|
CompilerUtils utils(m_context);
|
2016-11-21 15:41:02 +00:00
|
|
|
solAssert(m_dataType, "");
|
|
|
|
|
2015-03-11 17:09:35 +00:00
|
|
|
// stack: value storage_key storage_offset
|
2015-10-14 13:19:50 +00:00
|
|
|
if (m_dataType->isValueType())
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2015-10-14 13:19:50 +00:00
|
|
|
solAssert(m_dataType->storageBytes() <= 32, "Invalid storage bytes size.");
|
|
|
|
solAssert(m_dataType->storageBytes() > 0, "Invalid storage bytes size.");
|
|
|
|
if (m_dataType->storageBytes() == 32)
|
2015-03-13 18:48:24 +00:00
|
|
|
{
|
2016-10-14 10:27:46 +00:00
|
|
|
solAssert(m_dataType->sizeOnStack() == 1, "Invalid stack size.");
|
2015-03-13 18:48:24 +00:00
|
|
|
// offset should be zero
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-03-13 18:48:24 +00:00
|
|
|
if (!_move)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2 << Instruction::SWAP1;
|
2016-11-21 15:41:02 +00:00
|
|
|
|
|
|
|
m_context << Instruction::SWAP1;
|
2016-11-24 15:49:54 +00:00
|
|
|
utils.convertType(_sourceType, *m_dataType, true);
|
2016-11-21 15:41:02 +00:00
|
|
|
m_context << Instruction::SWAP1;
|
|
|
|
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SSTORE;
|
2015-03-13 18:48:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// OR the value into the other values in the storage slot
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(0x100) << Instruction::EXP;
|
2015-03-13 18:48:24 +00:00
|
|
|
// stack: value storage_ref multiplier
|
|
|
|
// fetch old value
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2 << Instruction::SLOAD;
|
2015-03-13 18:48:24 +00:00
|
|
|
// stack: value storege_ref multiplier old_full_value
|
|
|
|
// clear bytes in old value
|
|
|
|
m_context
|
2016-04-04 11:41:35 +00:00
|
|
|
<< Instruction::DUP2 << ((u256(1) << (8 * m_dataType->storageBytes())) - 1)
|
|
|
|
<< Instruction::MUL;
|
2016-10-14 10:27:46 +00:00
|
|
|
m_context << Instruction::NOT << Instruction::AND << Instruction::SWAP1;
|
|
|
|
// stack: value storage_ref cleared_value multiplier
|
2016-10-28 15:30:56 +00:00
|
|
|
utils.copyToStackTop(3 + m_dataType->sizeOnStack(), m_dataType->sizeOnStack());
|
2015-03-13 18:48:24 +00:00
|
|
|
// stack: value storage_ref cleared_value multiplier value
|
2016-11-07 19:08:05 +00:00
|
|
|
if (FunctionType const* fun = dynamic_cast<decltype(fun)>(m_dataType))
|
|
|
|
{
|
2016-11-25 14:38:02 +00:00
|
|
|
solAssert(_sourceType == *m_dataType, "function item stored but target is not equal to source");
|
2017-03-16 11:58:17 +00:00
|
|
|
if (fun->kind() == FunctionType::Kind::External)
|
2016-11-07 19:08:05 +00:00
|
|
|
// Combine the two-item function type into a single stack slot.
|
|
|
|
utils.combineExternalFunctionType(false);
|
|
|
|
else
|
|
|
|
m_context <<
|
|
|
|
((u256(1) << (8 * m_dataType->storageBytes())) - 1) <<
|
|
|
|
Instruction::AND;
|
|
|
|
}
|
2016-10-14 10:27:46 +00:00
|
|
|
else if (m_dataType->category() == Type::Category::FixedBytes)
|
2016-11-25 14:38:02 +00:00
|
|
|
{
|
|
|
|
solAssert(_sourceType.category() == Type::Category::FixedBytes, "source not fixed bytes");
|
2017-06-15 12:02:05 +00:00
|
|
|
CompilerUtils(m_context).rightShiftNumberOnStack(256 - 8 * dynamic_cast<FixedBytesType const&>(*m_dataType).numBytes(), false);
|
2016-11-25 14:38:02 +00:00
|
|
|
}
|
2016-10-31 15:40:30 +00:00
|
|
|
else
|
2016-10-14 10:27:46 +00:00
|
|
|
{
|
|
|
|
solAssert(m_dataType->sizeOnStack() == 1, "Invalid stack size for opaque type.");
|
2015-04-16 15:43:40 +00:00
|
|
|
// remove the higher order bits
|
2016-11-25 14:38:02 +00:00
|
|
|
utils.convertType(_sourceType, *m_dataType, true, true);
|
2016-10-14 10:27:46 +00:00
|
|
|
}
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::MUL << Instruction::OR;
|
2015-03-13 18:48:24 +00:00
|
|
|
// stack: value storage_ref updated_value
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::SSTORE;
|
2015-03-13 18:48:24 +00:00
|
|
|
if (_move)
|
2016-10-14 10:27:46 +00:00
|
|
|
utils.popStackElement(*m_dataType);
|
2015-03-13 18:48:24 +00:00
|
|
|
}
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-11 17:09:35 +00:00
|
|
|
solAssert(
|
2015-10-14 13:19:50 +00:00
|
|
|
_sourceType.category() == m_dataType->category(),
|
2015-02-25 14:14:22 +00:00
|
|
|
"Wrong type conversation for assignment.");
|
2015-10-14 13:19:50 +00:00
|
|
|
if (m_dataType->category() == Type::Category::Array)
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP; // remove byte offset
|
2015-02-25 19:27:55 +00:00
|
|
|
ArrayUtils(m_context).copyArrayToStorage(
|
2015-10-16 08:01:22 +00:00
|
|
|
dynamic_cast<ArrayType const&>(*m_dataType),
|
|
|
|
dynamic_cast<ArrayType const&>(_sourceType)
|
|
|
|
);
|
2015-02-25 14:14:22 +00:00
|
|
|
if (_move)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
2015-10-14 13:19:50 +00:00
|
|
|
else if (m_dataType->category() == Type::Category::Struct)
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2015-07-14 09:58:16 +00:00
|
|
|
// stack layout: source_ref target_ref target_offset
|
|
|
|
// note that we have structs, so offset should be zero and are ignored
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-10-14 13:19:50 +00:00
|
|
|
auto const& structType = dynamic_cast<StructType const&>(*m_dataType);
|
2015-06-30 09:54:51 +00:00
|
|
|
auto const& sourceType = dynamic_cast<StructType const&>(_sourceType);
|
2015-06-09 12:26:08 +00:00
|
|
|
solAssert(
|
2015-06-30 09:54:51 +00:00
|
|
|
structType.structDefinition() == sourceType.structDefinition(),
|
2015-06-09 12:26:08 +00:00
|
|
|
"Struct assignment with conversion."
|
|
|
|
);
|
2015-06-30 09:54:51 +00:00
|
|
|
solAssert(sourceType.location() != DataLocation::CallData, "Structs in calldata not supported.");
|
2015-11-19 17:02:04 +00:00
|
|
|
for (auto const& member: structType.members(nullptr))
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
|
|
|
// assign each member that is not a mapping
|
2015-04-15 15:40:50 +00:00
|
|
|
TypePointer const& memberType = member.type;
|
2015-08-31 16:44:29 +00:00
|
|
|
if (memberType->category() == Type::Category::Mapping)
|
2015-02-25 14:14:22 +00:00
|
|
|
continue;
|
2015-08-31 16:44:29 +00:00
|
|
|
TypePointer sourceMemberType = sourceType.memberType(member.name);
|
2015-06-30 09:54:51 +00:00
|
|
|
if (sourceType.location() == DataLocation::Storage)
|
|
|
|
{
|
2015-07-14 09:58:16 +00:00
|
|
|
// stack layout: source_ref target_ref
|
2015-08-31 16:44:29 +00:00
|
|
|
pair<u256, unsigned> const& offsets = sourceType.storageOffsetsOfMember(member.name);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << offsets.first << Instruction::DUP3 << Instruction::ADD;
|
2015-06-30 09:54:51 +00:00
|
|
|
m_context << u256(offsets.second);
|
2015-07-14 09:58:16 +00:00
|
|
|
// stack: source_ref target_ref source_member_ref source_member_off
|
2015-06-30 09:54:51 +00:00
|
|
|
StorageItem(m_context, *sourceMemberType).retrieveValue(_location, true);
|
2015-07-14 09:58:16 +00:00
|
|
|
// stack: source_ref target_ref source_value...
|
2015-06-30 09:54:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(sourceType.location() == DataLocation::Memory, "");
|
2015-07-14 09:58:16 +00:00
|
|
|
// stack layout: source_ref target_ref
|
2015-08-31 16:44:29 +00:00
|
|
|
TypePointer sourceMemberType = sourceType.memberType(member.name);
|
2015-06-30 09:54:51 +00:00
|
|
|
m_context << sourceType.memoryOffsetOfMember(member.name);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP3 << Instruction::ADD;
|
2015-06-30 09:54:51 +00:00
|
|
|
MemoryItem(m_context, *sourceMemberType).retrieveValue(_location, true);
|
2015-07-14 09:58:16 +00:00
|
|
|
// stack layout: source_ref target_ref source_value...
|
2015-06-30 09:54:51 +00:00
|
|
|
}
|
2015-08-31 16:44:29 +00:00
|
|
|
unsigned stackSize = sourceMemberType->sizeOnStack();
|
|
|
|
pair<u256, unsigned> const& offsets = structType.storageOffsetsOfMember(member.name);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << dupInstruction(1 + stackSize) << offsets.first << Instruction::ADD;
|
2015-06-30 09:54:51 +00:00
|
|
|
m_context << u256(offsets.second);
|
2015-07-14 09:58:16 +00:00
|
|
|
// stack: source_ref target_ref target_off source_value... target_member_ref target_member_byte_off
|
2015-06-30 09:54:51 +00:00
|
|
|
StorageItem(m_context, *memberType).storeValue(*sourceMemberType, _location, true);
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
2015-07-14 09:58:16 +00:00
|
|
|
// stack layout: source_ref target_ref
|
2015-08-31 16:44:29 +00:00
|
|
|
solAssert(sourceType.sizeOnStack() == 1, "Unexpected source size.");
|
2015-02-25 14:14:22 +00:00
|
|
|
if (_move)
|
2015-07-14 09:58:16 +00:00
|
|
|
utils.popStackSlots(2);
|
|
|
|
else
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::POP;
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
|
|
|
else
|
2015-03-11 17:09:35 +00:00
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
InternalCompilerError()
|
|
|
|
<< errinfo_sourceLocation(_location)
|
|
|
|
<< errinfo_comment("Invalid non-value type for assignment."));
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 19:27:55 +00:00
|
|
|
void StorageItem::setToZero(SourceLocation const&, bool _removeReference) const
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2015-10-14 13:19:50 +00:00
|
|
|
if (m_dataType->category() == Type::Category::Array)
|
2015-02-25 19:27:55 +00:00
|
|
|
{
|
|
|
|
if (!_removeReference)
|
2015-03-11 17:09:35 +00:00
|
|
|
CompilerUtils(m_context).copyToStackTop(sizeOnStack(), sizeOnStack());
|
2015-10-14 13:19:50 +00:00
|
|
|
ArrayUtils(m_context).clearArray(dynamic_cast<ArrayType const&>(*m_dataType));
|
2015-02-25 19:27:55 +00:00
|
|
|
}
|
2015-10-14 13:19:50 +00:00
|
|
|
else if (m_dataType->category() == Type::Category::Struct)
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2015-03-11 17:09:35 +00:00
|
|
|
// stack layout: storage_key storage_offset
|
2015-03-31 12:59:38 +00:00
|
|
|
// @todo this can be improved: use StorageItem for non-value types, and just store 0 in
|
|
|
|
// all slots that contain value types later.
|
2015-10-14 13:19:50 +00:00
|
|
|
auto const& structType = dynamic_cast<StructType const&>(*m_dataType);
|
2015-11-19 17:02:04 +00:00
|
|
|
for (auto const& member: structType.members(nullptr))
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
|
|
|
// zero each member that is not a mapping
|
2015-04-15 15:40:50 +00:00
|
|
|
TypePointer const& memberType = member.type;
|
2015-08-31 16:44:29 +00:00
|
|
|
if (memberType->category() == Type::Category::Mapping)
|
2015-02-25 14:14:22 +00:00
|
|
|
continue;
|
2015-08-31 16:44:29 +00:00
|
|
|
pair<u256, unsigned> const& offsets = structType.storageOffsetsOfMember(member.name);
|
2015-03-11 17:09:35 +00:00
|
|
|
m_context
|
2016-04-04 11:41:35 +00:00
|
|
|
<< offsets.first << Instruction::DUP3 << Instruction::ADD
|
2015-03-13 18:48:24 +00:00
|
|
|
<< u256(offsets.second);
|
2015-02-25 19:27:55 +00:00
|
|
|
StorageItem(m_context, *memberType).setToZero();
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
2015-02-25 19:27:55 +00:00
|
|
|
if (_removeReference)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP << Instruction::POP;
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-14 13:19:50 +00:00
|
|
|
solAssert(m_dataType->isValueType(), "Clearing of unsupported type requested: " + m_dataType->toString());
|
2015-03-11 17:09:35 +00:00
|
|
|
if (!_removeReference)
|
|
|
|
CompilerUtils(m_context).copyToStackTop(sizeOnStack(), sizeOnStack());
|
2015-10-14 13:19:50 +00:00
|
|
|
if (m_dataType->storageBytes() == 32)
|
2015-03-13 18:48:24 +00:00
|
|
|
{
|
|
|
|
// offset should be zero
|
|
|
|
m_context
|
2016-04-04 11:41:35 +00:00
|
|
|
<< Instruction::POP << u256(0)
|
|
|
|
<< Instruction::SWAP1 << Instruction::SSTORE;
|
2015-03-13 18:48:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(0x100) << Instruction::EXP;
|
2015-03-13 18:48:24 +00:00
|
|
|
// stack: storage_ref multiplier
|
|
|
|
// fetch old value
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2 << Instruction::SLOAD;
|
2015-03-13 18:48:24 +00:00
|
|
|
// stack: storege_ref multiplier old_full_value
|
|
|
|
// clear bytes in old value
|
|
|
|
m_context
|
2016-04-04 11:41:35 +00:00
|
|
|
<< Instruction::SWAP1 << ((u256(1) << (8 * m_dataType->storageBytes())) - 1)
|
|
|
|
<< Instruction::MUL;
|
|
|
|
m_context << Instruction::NOT << Instruction::AND;
|
2015-03-13 18:48:24 +00:00
|
|
|
// stack: storage_ref cleared_value
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::SSTORE;
|
2015-03-13 18:48:24 +00:00
|
|
|
}
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-25 19:27:55 +00:00
|
|
|
|
2015-03-03 16:55:28 +00:00
|
|
|
/// Used in StorageByteArrayElement
|
2015-03-06 16:20:00 +00:00
|
|
|
static FixedBytesType byteType(1);
|
2015-03-03 16:55:28 +00:00
|
|
|
|
|
|
|
StorageByteArrayElement::StorageByteArrayElement(CompilerContext& _compilerContext):
|
2015-10-14 13:19:50 +00:00
|
|
|
LValue(_compilerContext, &byteType)
|
2015-03-03 16:55:28 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void StorageByteArrayElement::retrieveValue(SourceLocation const&, bool _remove) const
|
|
|
|
{
|
2015-03-05 14:41:39 +00:00
|
|
|
// stack: ref byte_number
|
2015-03-03 16:55:28 +00:00
|
|
|
if (_remove)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::SLOAD
|
|
|
|
<< Instruction::SWAP1 << Instruction::BYTE;
|
2015-03-03 16:55:28 +00:00
|
|
|
else
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2 << Instruction::SLOAD
|
|
|
|
<< Instruction::DUP2 << Instruction::BYTE;
|
|
|
|
m_context << (u256(1) << (256 - 8)) << Instruction::MUL;
|
2015-03-03 16:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StorageByteArrayElement::storeValue(Type const&, SourceLocation const&, bool _move) const
|
|
|
|
{
|
2015-03-05 14:41:39 +00:00
|
|
|
// stack: value ref byte_number
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(31) << Instruction::SUB << u256(0x100) << Instruction::EXP;
|
2015-03-05 14:41:39 +00:00
|
|
|
// stack: value ref (1<<(8*(31-byte_number)))
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2 << Instruction::SLOAD;
|
2015-03-05 14:41:39 +00:00
|
|
|
// stack: value ref (1<<(8*(31-byte_number))) old_full_value
|
2015-03-03 16:55:28 +00:00
|
|
|
// clear byte in old value
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2 << u256(0xff) << Instruction::MUL
|
|
|
|
<< Instruction::NOT << Instruction::AND;
|
2015-03-05 14:41:39 +00:00
|
|
|
// stack: value ref (1<<(32-byte_number)) old_full_value_with_cleared_byte
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1;
|
|
|
|
m_context << (u256(1) << (256 - 8)) << Instruction::DUP5 << Instruction::DIV
|
|
|
|
<< Instruction::MUL << Instruction::OR;
|
2015-03-03 16:55:28 +00:00
|
|
|
// stack: value ref new_full_value
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::SSTORE;
|
2015-03-03 16:55:28 +00:00
|
|
|
if (_move)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-03-03 16:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StorageByteArrayElement::setToZero(SourceLocation const&, bool _removeReference) const
|
|
|
|
{
|
2015-03-05 14:41:39 +00:00
|
|
|
// stack: ref byte_number
|
2015-03-03 16:55:28 +00:00
|
|
|
if (!_removeReference)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2 << Instruction::DUP2;
|
|
|
|
m_context << u256(31) << Instruction::SUB << u256(0x100) << Instruction::EXP;
|
2015-03-05 14:41:39 +00:00
|
|
|
// stack: ref (1<<(8*(31-byte_number)))
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2 << Instruction::SLOAD;
|
2015-03-05 14:41:39 +00:00
|
|
|
// stack: ref (1<<(8*(31-byte_number))) old_full_value
|
2015-03-03 16:55:28 +00:00
|
|
|
// clear byte in old value
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << u256(0xff) << Instruction::MUL;
|
|
|
|
m_context << Instruction::NOT << Instruction::AND;
|
2015-03-03 16:55:28 +00:00
|
|
|
// stack: ref old_full_value_with_cleared_byte
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::SSTORE;
|
2015-03-03 16:55:28 +00:00
|
|
|
}
|
2015-02-25 19:27:55 +00:00
|
|
|
|
|
|
|
StorageArrayLength::StorageArrayLength(CompilerContext& _compilerContext, const ArrayType& _arrayType):
|
2015-10-14 13:19:50 +00:00
|
|
|
LValue(_compilerContext, _arrayType.memberType("length").get()),
|
2015-02-25 19:27:55 +00:00
|
|
|
m_arrayType(_arrayType)
|
|
|
|
{
|
|
|
|
solAssert(m_arrayType.isDynamicallySized(), "");
|
|
|
|
}
|
|
|
|
|
2015-03-02 14:12:54 +00:00
|
|
|
void StorageArrayLength::retrieveValue(SourceLocation const&, bool _remove) const
|
2015-02-25 19:27:55 +00:00
|
|
|
{
|
2015-09-25 15:13:29 +00:00
|
|
|
ArrayUtils(m_context).retrieveLength(m_arrayType);
|
|
|
|
if (_remove)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::POP;
|
2015-02-25 19:27:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 14:12:54 +00:00
|
|
|
void StorageArrayLength::storeValue(Type const&, SourceLocation const&, bool _move) const
|
2015-02-25 19:27:55 +00:00
|
|
|
{
|
|
|
|
if (_move)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1;
|
2015-02-25 19:27:55 +00:00
|
|
|
else
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2;
|
2015-02-25 19:27:55 +00:00
|
|
|
ArrayUtils(m_context).resizeDynamicArray(m_arrayType);
|
|
|
|
}
|
|
|
|
|
2015-03-02 14:12:54 +00:00
|
|
|
void StorageArrayLength::setToZero(SourceLocation const&, bool _removeReference) const
|
2015-02-25 19:27:55 +00:00
|
|
|
{
|
|
|
|
if (!_removeReference)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1;
|
2015-02-25 19:27:55 +00:00
|
|
|
ArrayUtils(m_context).clearDynamicArray(m_arrayType);
|
|
|
|
}
|
2015-10-14 13:19:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
TupleObject::TupleObject(
|
|
|
|
CompilerContext& _compilerContext,
|
|
|
|
std::vector<std::unique_ptr<LValue>>&& _lvalues
|
|
|
|
):
|
|
|
|
LValue(_compilerContext), m_lvalues(move(_lvalues))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned TupleObject::sizeOnStack() const
|
|
|
|
{
|
|
|
|
unsigned size = 0;
|
|
|
|
for (auto const& lv: m_lvalues)
|
|
|
|
if (lv)
|
|
|
|
size += lv->sizeOnStack();
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TupleObject::retrieveValue(SourceLocation const& _location, bool _remove) const
|
|
|
|
{
|
|
|
|
unsigned initialDepth = sizeOnStack();
|
|
|
|
unsigned initialStack = m_context.stackHeight();
|
|
|
|
for (auto const& lv: m_lvalues)
|
|
|
|
if (lv)
|
|
|
|
{
|
|
|
|
solAssert(initialDepth + m_context.stackHeight() >= initialStack, "");
|
|
|
|
unsigned depth = initialDepth + m_context.stackHeight() - initialStack;
|
|
|
|
if (lv->sizeOnStack() > 0)
|
2015-10-15 12:03:56 +00:00
|
|
|
{
|
2015-10-14 13:19:50 +00:00
|
|
|
if (_remove && depth > lv->sizeOnStack())
|
|
|
|
CompilerUtils(m_context).moveToStackTop(depth, depth - lv->sizeOnStack());
|
|
|
|
else if (!_remove && depth > 0)
|
|
|
|
CompilerUtils(m_context).copyToStackTop(depth, lv->sizeOnStack());
|
2015-10-15 12:03:56 +00:00
|
|
|
}
|
2015-10-14 13:19:50 +00:00
|
|
|
lv->retrieveValue(_location, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 12:03:56 +00:00
|
|
|
void TupleObject::storeValue(Type const& _sourceType, SourceLocation const& _location, bool) const
|
2015-10-14 13:19:50 +00:00
|
|
|
{
|
|
|
|
// values are below the lvalue references
|
|
|
|
unsigned valuePos = sizeOnStack();
|
|
|
|
TypePointers const& valueTypes = dynamic_cast<TupleType const&>(_sourceType).components();
|
2015-10-14 22:42:36 +00:00
|
|
|
solAssert(valueTypes.size() == m_lvalues.size(), "");
|
2015-10-14 13:19:50 +00:00
|
|
|
// valuePos .... refPos ...
|
|
|
|
// We will assign from right to left to optimize stack layout.
|
|
|
|
for (size_t i = 0; i < m_lvalues.size(); ++i)
|
|
|
|
{
|
|
|
|
unique_ptr<LValue> const& lvalue = m_lvalues[m_lvalues.size() - i - 1];
|
|
|
|
TypePointer const& valType = valueTypes[valueTypes.size() - i - 1];
|
|
|
|
unsigned stackHeight = m_context.stackHeight();
|
2015-10-14 22:42:36 +00:00
|
|
|
solAssert(!valType == !lvalue, "");
|
|
|
|
if (!lvalue)
|
|
|
|
continue;
|
2015-10-14 13:19:50 +00:00
|
|
|
valuePos += valType->sizeOnStack();
|
2015-10-14 22:42:36 +00:00
|
|
|
// copy value to top
|
|
|
|
CompilerUtils(m_context).copyToStackTop(valuePos, valType->sizeOnStack());
|
|
|
|
// move lvalue ref above value
|
|
|
|
CompilerUtils(m_context).moveToStackTop(valType->sizeOnStack(), lvalue->sizeOnStack());
|
|
|
|
lvalue->storeValue(*valType, _location, true);
|
2015-10-14 13:19:50 +00:00
|
|
|
valuePos += m_context.stackHeight() - stackHeight;
|
|
|
|
}
|
|
|
|
// As the type of an assignment to a tuple type is the empty tuple, we always move.
|
|
|
|
CompilerUtils(m_context).popStackElement(_sourceType);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TupleObject::setToZero(SourceLocation const& _location, bool _removeReference) const
|
|
|
|
{
|
|
|
|
if (_removeReference)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_lvalues.size(); ++i)
|
|
|
|
if (m_lvalues[m_lvalues.size() - i])
|
|
|
|
m_lvalues[m_lvalues.size() - i]->setToZero(_location, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned depth = sizeOnStack();
|
|
|
|
for (auto const& val: m_lvalues)
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
if (val->sizeOnStack() > 0)
|
|
|
|
CompilerUtils(m_context).copyToStackTop(depth, val->sizeOnStack());
|
|
|
|
val->setToZero(_location, false);
|
|
|
|
depth -= val->sizeOnStack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|