2015-02-25 14:14:22 +00:00
|
|
|
/*
|
|
|
|
This file is part of cpp-ethereum.
|
|
|
|
|
|
|
|
cpp-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
cpp-ethereum is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* LValues for use in the expresison compiler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/LValue.h>
|
|
|
|
#include <libevmcore/Instruction.h>
|
|
|
|
#include <libsolidity/Types.h>
|
|
|
|
#include <libsolidity/AST.h>
|
|
|
|
#include <libsolidity/CompilerUtils.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace solidity;
|
|
|
|
|
|
|
|
|
|
|
|
StackVariable::StackVariable(CompilerContext& _compilerContext, Declaration const& _declaration):
|
2015-02-25 19:27:55 +00:00
|
|
|
LValue(_compilerContext, *_declaration.getType()),
|
2015-02-25 14:14:22 +00:00
|
|
|
m_baseStackOffset(m_context.getBaseStackOffsetOfVariable(_declaration)),
|
2015-02-25 19:27:55 +00:00
|
|
|
m_size(m_dataType.getSizeOnStack())
|
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);
|
|
|
|
if (stackPos >= 15) //@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-02-25 14:14:22 +00:00
|
|
|
for (unsigned i = 0; i < m_size; ++i)
|
|
|
|
m_context << eth::dupInstruction(stackPos + 1);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
m_context << eth::swapInstruction(stackDiff) << eth::Instruction::POP;
|
|
|
|
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-06-26 16:35:43 +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):
|
|
|
|
LValue(_compilerContext, _type),
|
|
|
|
m_padded(_padded)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryItem::retrieveValue(SourceLocation const&, bool _remove) const
|
|
|
|
{
|
|
|
|
if (m_dataType.isValueType())
|
|
|
|
{
|
|
|
|
if (!_remove)
|
|
|
|
m_context << eth::Instruction::DUP1;
|
|
|
|
CompilerUtils(m_context).loadFromMemoryDynamic(m_dataType, false, m_padded, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_context << eth::Instruction::MLOAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryItem::storeValue(Type const& _sourceType, SourceLocation const&, bool _move) const
|
|
|
|
{
|
|
|
|
CompilerUtils utils(m_context);
|
|
|
|
if (m_dataType.isValueType())
|
|
|
|
{
|
|
|
|
solAssert(_sourceType.isValueType(), "");
|
|
|
|
utils.moveIntoStack(_sourceType.getSizeOnStack());
|
|
|
|
utils.convertType(_sourceType, m_dataType, true);
|
|
|
|
if (!_move)
|
|
|
|
{
|
|
|
|
utils.moveToStackTop(m_dataType.getSizeOnStack());
|
|
|
|
utils.copyToStackTop(2, m_dataType.getSizeOnStack());
|
|
|
|
}
|
|
|
|
utils.storeInMemoryDynamic(m_dataType, m_padded);
|
|
|
|
m_context << eth::Instruction::POP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(_sourceType == m_dataType, "Conversion not implemented for assignment to memory.");
|
|
|
|
|
|
|
|
solAssert(m_dataType.getSizeOnStack() == 1, "");
|
|
|
|
if (!_move)
|
|
|
|
m_context << eth::Instruction::DUP2 << eth::Instruction::SWAP1;
|
|
|
|
// stack: [value] value lvalue
|
|
|
|
// only store the reference
|
|
|
|
m_context << eth::Instruction::MSTORE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryItem::setToZero(SourceLocation const&, bool _removeReference) const
|
|
|
|
{
|
|
|
|
CompilerUtils utils(m_context);
|
|
|
|
if (!_removeReference)
|
|
|
|
m_context << eth::Instruction::DUP1;
|
|
|
|
utils.pushZeroValue(m_dataType);
|
|
|
|
utils.storeInMemoryDynamic(m_dataType, m_padded);
|
|
|
|
m_context << eth::Instruction::POP;
|
|
|
|
}
|
2015-02-25 14:14:22 +00:00
|
|
|
|
|
|
|
StorageItem::StorageItem(CompilerContext& _compilerContext, Declaration const& _declaration):
|
2015-02-25 19:27:55 +00:00
|
|
|
StorageItem(_compilerContext, *_declaration.getType())
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2015-03-13 18:48:24 +00:00
|
|
|
auto const& location = m_context.getStorageLocationOfVariable(_declaration);
|
|
|
|
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-02-25 14:14:22 +00:00
|
|
|
LValue(_compilerContext, _type)
|
|
|
|
{
|
2015-02-25 19:27:55 +00:00
|
|
|
if (m_dataType.isValueType())
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2015-02-25 19:27:55 +00:00
|
|
|
solAssert(m_dataType.getStorageSize() == m_dataType.getSizeOnStack(), "");
|
2015-03-11 17:09:35 +00:00
|
|
|
solAssert(m_dataType.getStorageSize() == 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-02-25 19:27:55 +00:00
|
|
|
if (!m_dataType.isValueType())
|
2015-02-25 14:14:22 +00:00
|
|
|
return; // no distinction between value and reference for non-value types
|
|
|
|
if (!_remove)
|
2015-03-11 17:09:35 +00:00
|
|
|
CompilerUtils(m_context).copyToStackTop(sizeOnStack(), sizeOnStack());
|
2015-03-13 18:48:24 +00:00
|
|
|
if (m_dataType.getStorageBytes() == 32)
|
|
|
|
m_context << eth::Instruction::POP << eth::Instruction::SLOAD;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_context
|
|
|
|
<< eth::Instruction::SWAP1 << eth::Instruction::SLOAD << eth::Instruction::SWAP1
|
|
|
|
<< u256(0x100) << eth::Instruction::EXP << eth::Instruction::SWAP1 << eth::Instruction::DIV;
|
|
|
|
if (m_dataType.getCategory() == Type::Category::FixedBytes)
|
|
|
|
m_context << (u256(0x1) << (256 - 8 * m_dataType.getStorageBytes())) << eth::Instruction::MUL;
|
2015-04-16 15:43:40 +00:00
|
|
|
else if (
|
|
|
|
m_dataType.getCategory() == Type::Category::Integer &&
|
|
|
|
dynamic_cast<IntegerType const&>(m_dataType).isSigned()
|
|
|
|
)
|
|
|
|
m_context << u256(m_dataType.getStorageBytes() - 1) << eth::Instruction::SIGNEXTEND;
|
2015-03-13 18:48:24 +00:00
|
|
|
else
|
|
|
|
m_context << ((u256(0x1) << (8 * m_dataType.getStorageBytes())) - 1) << eth::Instruction::AND;
|
|
|
|
}
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StorageItem::storeValue(Type const& _sourceType, SourceLocation const& _location, bool _move) const
|
|
|
|
{
|
2015-03-11 17:09:35 +00:00
|
|
|
// stack: value storage_key storage_offset
|
2015-02-25 19:27:55 +00:00
|
|
|
if (m_dataType.isValueType())
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2015-03-13 18:48:24 +00:00
|
|
|
solAssert(m_dataType.getStorageBytes() <= 32, "Invalid storage bytes size.");
|
|
|
|
solAssert(m_dataType.getStorageBytes() > 0, "Invalid storage bytes size.");
|
|
|
|
if (m_dataType.getStorageBytes() == 32)
|
|
|
|
{
|
|
|
|
// offset should be zero
|
2015-03-11 17:09:35 +00:00
|
|
|
m_context << eth::Instruction::POP;
|
2015-03-13 18:48:24 +00:00
|
|
|
if (!_move)
|
|
|
|
m_context << eth::Instruction::DUP2 << eth::Instruction::SWAP1;
|
|
|
|
m_context << eth::Instruction::SSTORE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// OR the value into the other values in the storage slot
|
|
|
|
m_context << u256(0x100) << eth::Instruction::EXP;
|
|
|
|
// stack: value storage_ref multiplier
|
|
|
|
// fetch old value
|
|
|
|
m_context << eth::Instruction::DUP2 << eth::Instruction::SLOAD;
|
|
|
|
// stack: value storege_ref multiplier old_full_value
|
|
|
|
// clear bytes in old value
|
|
|
|
m_context
|
|
|
|
<< eth::Instruction::DUP2 << ((u256(1) << (8 * m_dataType.getStorageBytes())) - 1)
|
|
|
|
<< eth::Instruction::MUL;
|
|
|
|
m_context << eth::Instruction::NOT << eth::Instruction::AND;
|
|
|
|
// stack: value storage_ref multiplier cleared_value
|
|
|
|
m_context
|
|
|
|
<< eth::Instruction::SWAP1 << eth::Instruction::DUP4;
|
|
|
|
// stack: value storage_ref cleared_value multiplier value
|
|
|
|
if (m_dataType.getCategory() == Type::Category::FixedBytes)
|
|
|
|
m_context
|
|
|
|
<< (u256(0x1) << (256 - 8 * dynamic_cast<FixedBytesType const&>(m_dataType).getNumBytes()))
|
|
|
|
<< eth::Instruction::SWAP1 << eth::Instruction::DIV;
|
2015-04-16 15:43:40 +00:00
|
|
|
else if (
|
|
|
|
m_dataType.getCategory() == Type::Category::Integer &&
|
|
|
|
dynamic_cast<IntegerType const&>(m_dataType).isSigned()
|
|
|
|
)
|
|
|
|
// remove the higher order bits
|
|
|
|
m_context
|
|
|
|
<< (u256(1) << (8 * (32 - m_dataType.getStorageBytes())))
|
|
|
|
<< eth::Instruction::SWAP1
|
|
|
|
<< eth::Instruction::DUP2
|
|
|
|
<< eth::Instruction::MUL
|
|
|
|
<< eth::Instruction::DIV;
|
2015-03-13 18:48:24 +00:00
|
|
|
m_context << eth::Instruction::MUL << eth::Instruction::OR;
|
|
|
|
// stack: value storage_ref updated_value
|
|
|
|
m_context << eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
|
|
|
|
if (_move)
|
|
|
|
m_context << eth::Instruction::POP;
|
|
|
|
}
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-11 17:09:35 +00:00
|
|
|
solAssert(
|
|
|
|
_sourceType.getCategory() == m_dataType.getCategory(),
|
2015-02-25 14:14:22 +00:00
|
|
|
"Wrong type conversation for assignment.");
|
2015-02-25 19:27:55 +00:00
|
|
|
if (m_dataType.getCategory() == Type::Category::Array)
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2015-02-25 19:27:55 +00:00
|
|
|
ArrayUtils(m_context).copyArrayToStorage(
|
|
|
|
dynamic_cast<ArrayType const&>(m_dataType),
|
2015-02-25 14:14:22 +00:00
|
|
|
dynamic_cast<ArrayType const&>(_sourceType));
|
|
|
|
if (_move)
|
2015-03-11 17:09:35 +00:00
|
|
|
CompilerUtils(m_context).popStackElement(_sourceType);
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
2015-02-25 19:27:55 +00:00
|
|
|
else if (m_dataType.getCategory() == Type::Category::Struct)
|
2015-02-25 14:14:22 +00:00
|
|
|
{
|
2015-03-11 17:09:35 +00:00
|
|
|
// stack layout: source_ref source_offset target_ref target_offset
|
2015-03-13 18:48:24 +00:00
|
|
|
// note that we have structs, so offsets should be zero and are ignored
|
2015-02-25 19:27:55 +00:00
|
|
|
auto const& structType = dynamic_cast<StructType const&>(m_dataType);
|
2015-06-09 12:26:08 +00:00
|
|
|
solAssert(
|
|
|
|
structType.structDefinition() ==
|
|
|
|
dynamic_cast<StructType const&>(_sourceType).structDefinition(),
|
|
|
|
"Struct assignment with conversion."
|
|
|
|
);
|
2015-02-25 14:14:22 +00:00
|
|
|
for (auto const& member: structType.getMembers())
|
|
|
|
{
|
|
|
|
// assign each member that is not a mapping
|
2015-04-15 15:40:50 +00:00
|
|
|
TypePointer const& memberType = member.type;
|
2015-02-25 14:14:22 +00:00
|
|
|
if (memberType->getCategory() == Type::Category::Mapping)
|
|
|
|
continue;
|
2015-04-15 15:40:50 +00:00
|
|
|
pair<u256, unsigned> const& offsets = structType.getStorageOffsetsOfMember(member.name);
|
2015-03-13 18:48:24 +00:00
|
|
|
m_context
|
|
|
|
<< offsets.first << u256(offsets.second)
|
|
|
|
<< eth::Instruction::DUP6 << eth::Instruction::DUP3
|
|
|
|
<< eth::Instruction::ADD << eth::Instruction::DUP2;
|
|
|
|
// stack: source_ref source_off target_ref target_off member_slot_offset member_byte_offset source_member_ref source_member_off
|
2015-02-25 19:27:55 +00:00
|
|
|
StorageItem(m_context, *memberType).retrieveValue(_location, true);
|
2015-03-11 17:09:35 +00:00
|
|
|
// stack: source_ref source_off target_ref target_off member_offset source_value...
|
2015-06-05 23:04:55 +00:00
|
|
|
solAssert(
|
|
|
|
4 + memberType->getSizeOnStack() <= 16,
|
|
|
|
"Stack too deep, try removing local varibales."
|
|
|
|
);
|
2015-03-13 18:48:24 +00:00
|
|
|
m_context
|
|
|
|
<< eth::dupInstruction(4 + memberType->getSizeOnStack())
|
|
|
|
<< eth::dupInstruction(3 + memberType->getSizeOnStack()) << eth::Instruction::ADD
|
|
|
|
<< eth::dupInstruction(2 + memberType->getSizeOnStack());
|
|
|
|
// stack: source_ref source_off target_ref target_off member_slot_offset member_byte_offset source_value... target_member_ref target_member_byte_off
|
2015-02-25 19:27:55 +00:00
|
|
|
StorageItem(m_context, *memberType).storeValue(*memberType, _location, true);
|
2015-03-13 18:48:24 +00:00
|
|
|
m_context << eth::Instruction::POP << eth::Instruction::POP;
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
|
|
|
if (_move)
|
2015-03-11 17:09:35 +00:00
|
|
|
m_context
|
|
|
|
<< eth::Instruction::POP << eth::Instruction::POP
|
|
|
|
<< eth::Instruction::POP << eth::Instruction::POP;
|
2015-02-25 14:14:22 +00:00
|
|
|
else
|
2015-03-11 17:09:35 +00:00
|
|
|
m_context
|
|
|
|
<< eth::Instruction::SWAP2 << eth::Instruction::POP
|
|
|
|
<< eth::Instruction::SWAP2 << eth::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-02-25 19:27:55 +00:00
|
|
|
if (m_dataType.getCategory() == Type::Category::Array)
|
|
|
|
{
|
|
|
|
if (!_removeReference)
|
2015-03-11 17:09:35 +00:00
|
|
|
CompilerUtils(m_context).copyToStackTop(sizeOnStack(), sizeOnStack());
|
2015-02-25 19:27:55 +00:00
|
|
|
ArrayUtils(m_context).clearArray(dynamic_cast<ArrayType const&>(m_dataType));
|
|
|
|
}
|
|
|
|
else if (m_dataType.getCategory() == 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-02-25 19:27:55 +00:00
|
|
|
auto const& structType = dynamic_cast<StructType const&>(m_dataType);
|
2015-02-25 14:14:22 +00:00
|
|
|
for (auto const& member: structType.getMembers())
|
|
|
|
{
|
|
|
|
// zero each member that is not a mapping
|
2015-04-15 15:40:50 +00:00
|
|
|
TypePointer const& memberType = member.type;
|
2015-02-25 14:14:22 +00:00
|
|
|
if (memberType->getCategory() == Type::Category::Mapping)
|
|
|
|
continue;
|
2015-04-15 15:40:50 +00:00
|
|
|
pair<u256, unsigned> const& offsets = structType.getStorageOffsetsOfMember(member.name);
|
2015-03-11 17:09:35 +00:00
|
|
|
m_context
|
2015-03-13 18:48:24 +00:00
|
|
|
<< offsets.first << eth::Instruction::DUP3 << eth::Instruction::ADD
|
|
|
|
<< 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)
|
2015-03-11 17:09:35 +00:00
|
|
|
m_context << eth::Instruction::POP << eth::Instruction::POP;
|
2015-02-25 14:14:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-06 12:00:54 +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-03-13 18:48:24 +00:00
|
|
|
if (m_dataType.getStorageBytes() == 32)
|
|
|
|
{
|
|
|
|
// offset should be zero
|
|
|
|
m_context
|
|
|
|
<< eth::Instruction::POP << u256(0)
|
|
|
|
<< eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_context << u256(0x100) << eth::Instruction::EXP;
|
|
|
|
// stack: storage_ref multiplier
|
|
|
|
// fetch old value
|
|
|
|
m_context << eth::Instruction::DUP2 << eth::Instruction::SLOAD;
|
|
|
|
// stack: storege_ref multiplier old_full_value
|
|
|
|
// clear bytes in old value
|
|
|
|
m_context
|
|
|
|
<< eth::Instruction::SWAP1 << ((u256(1) << (8 * m_dataType.getStorageBytes())) - 1)
|
|
|
|
<< eth::Instruction::MUL;
|
|
|
|
m_context << eth::Instruction::NOT << eth::Instruction::AND;
|
|
|
|
// stack: storage_ref cleared_value
|
|
|
|
m_context << eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
|
|
|
|
}
|
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):
|
|
|
|
LValue(_compilerContext, byteType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
m_context << eth::Instruction::SWAP1 << eth::Instruction::SLOAD
|
2015-03-11 15:58:25 +00:00
|
|
|
<< eth::Instruction::SWAP1 << eth::Instruction::BYTE;
|
2015-03-03 16:55:28 +00:00
|
|
|
else
|
|
|
|
m_context << eth::Instruction::DUP2 << eth::Instruction::SLOAD
|
|
|
|
<< eth::Instruction::DUP2 << eth::Instruction::BYTE;
|
2015-03-10 17:22:19 +00:00
|
|
|
m_context << (u256(1) << (256 - 8)) << eth::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
|
2015-03-03 16:55:28 +00:00
|
|
|
m_context << u256(31) << eth::Instruction::SUB << u256(0x100) << eth::Instruction::EXP;
|
2015-03-05 14:41:39 +00:00
|
|
|
// stack: value ref (1<<(8*(31-byte_number)))
|
2015-03-03 16:55:28 +00:00
|
|
|
m_context << eth::Instruction::DUP2 << eth::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
|
|
|
|
m_context << eth::Instruction::DUP2 << u256(0xff) << eth::Instruction::MUL
|
|
|
|
<< eth::Instruction::NOT << eth::Instruction::AND;
|
2015-03-05 14:41:39 +00:00
|
|
|
// stack: value ref (1<<(32-byte_number)) old_full_value_with_cleared_byte
|
2015-03-10 17:22:19 +00:00
|
|
|
m_context << eth::Instruction::SWAP1;
|
|
|
|
m_context << (u256(1) << (256 - 8)) << eth::Instruction::DUP5 << eth::Instruction::DIV
|
2015-03-12 11:25:07 +00:00
|
|
|
<< eth::Instruction::MUL << eth::Instruction::OR;
|
2015-03-03 16:55:28 +00:00
|
|
|
// stack: value ref new_full_value
|
|
|
|
m_context << eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
|
|
|
|
if (_move)
|
|
|
|
m_context << eth::Instruction::POP;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2015-04-28 09:31:40 +00:00
|
|
|
m_context << eth::Instruction::DUP2 << eth::Instruction::DUP2;
|
2015-03-03 16:55:28 +00:00
|
|
|
m_context << u256(31) << eth::Instruction::SUB << u256(0x100) << eth::Instruction::EXP;
|
2015-03-05 14:41:39 +00:00
|
|
|
// stack: ref (1<<(8*(31-byte_number)))
|
2015-03-03 16:55:28 +00:00
|
|
|
m_context << eth::Instruction::DUP2 << eth::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
|
2015-04-28 09:31:40 +00:00
|
|
|
m_context << eth::Instruction::SWAP1 << u256(0xff) << eth::Instruction::MUL;
|
|
|
|
m_context << eth::Instruction::NOT << eth::Instruction::AND;
|
2015-03-03 16:55:28 +00:00
|
|
|
// stack: ref old_full_value_with_cleared_byte
|
|
|
|
m_context << eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
|
|
|
|
}
|
2015-02-25 19:27:55 +00:00
|
|
|
|
|
|
|
StorageArrayLength::StorageArrayLength(CompilerContext& _compilerContext, const ArrayType& _arrayType):
|
|
|
|
LValue(_compilerContext, *_arrayType.getMemberType("length")),
|
|
|
|
m_arrayType(_arrayType)
|
|
|
|
{
|
|
|
|
solAssert(m_arrayType.isDynamicallySized(), "");
|
2015-03-11 17:09:35 +00:00
|
|
|
// storage byte offset must be zero
|
|
|
|
m_context << eth::Instruction::POP;
|
2015-02-25 19:27:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 14:12:54 +00:00
|
|
|
void StorageArrayLength::retrieveValue(SourceLocation const&, bool _remove) const
|
2015-02-25 19:27:55 +00:00
|
|
|
{
|
|
|
|
if (!_remove)
|
|
|
|
m_context << eth::Instruction::DUP1;
|
|
|
|
m_context << eth::Instruction::SLOAD;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
m_context << eth::Instruction::SWAP1;
|
|
|
|
else
|
|
|
|
m_context << eth::Instruction::DUP2;
|
|
|
|
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)
|
|
|
|
m_context << eth::Instruction::DUP1;
|
|
|
|
ArrayUtils(m_context).clearDynamicArray(m_arrayType);
|
|
|
|
}
|