solidity/LValue.cpp

266 lines
9.1 KiB
C++
Raw Normal View History

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
BOOST_THROW_EXCEPTION(CompilerError()
<< errinfo_sourceLocation(_location) << errinfo_comment("Stack too deep."));
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)
BOOST_THROW_EXCEPTION(CompilerError()
<< errinfo_sourceLocation(_location) << errinfo_comment("Stack too deep."));
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
{
unsigned stackDiff = m_context.baseToCurrentStackOffset(m_baseStackOffset);
if (stackDiff > 16)
BOOST_THROW_EXCEPTION(CompilerError()
<< errinfo_sourceLocation(_location) << errinfo_comment("Stack too deep."));
solAssert(stackDiff >= m_size - 1, "");
for (unsigned i = 0; i < m_size; ++i)
m_context << u256(0) << eth::swapInstruction(stackDiff + 1 - i)
<< eth::Instruction::POP;
}
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
{
m_context << m_context.getStorageLocationOfVariable(_declaration);
}
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(), "");
solAssert(m_dataType.getStorageSize() <= numeric_limits<unsigned>::max(),
"The storage size of " + m_dataType.toString() + " should fit in an unsigned");
m_size = unsigned(m_dataType.getStorageSize());
2015-02-25 14:14:22 +00:00
}
else
m_size = 0; // unused
}
void StorageItem::retrieveValue(SourceLocation const&, bool _remove) const
{
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)
m_context << eth::Instruction::DUP1;
if (m_size == 1)
m_context << eth::Instruction::SLOAD;
else
for (unsigned i = 0; i < m_size; ++i)
{
m_context << eth::Instruction::DUP1 << eth::Instruction::SLOAD << eth::Instruction::SWAP1;
if (i + 1 < m_size)
m_context << u256(1) << eth::Instruction::ADD;
else
m_context << eth::Instruction::POP;
}
}
void StorageItem::storeValue(Type const& _sourceType, SourceLocation const& _location, bool _move) const
{
// stack layout: value value ... value target_ref
2015-02-25 19:27:55 +00:00
if (m_dataType.isValueType())
2015-02-25 14:14:22 +00:00
{
if (!_move) // copy values
{
if (m_size + 1 > 16)
BOOST_THROW_EXCEPTION(CompilerError()
<< errinfo_sourceLocation(_location) << errinfo_comment("Stack too deep."));
for (unsigned i = 0; i < m_size; ++i)
m_context << eth::dupInstruction(m_size + 1) << eth::Instruction::SWAP1;
}
if (m_size > 1) // store high index value first
m_context << u256(m_size - 1) << eth::Instruction::ADD;
for (unsigned i = 0; i < m_size; ++i)
{
if (i + 1 >= m_size)
m_context << eth::Instruction::SSTORE;
else
// stack here: value value ... value value (target_ref+offset)
m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2
<< eth::Instruction::SSTORE
<< u256(1) << eth::Instruction::SWAP1 << eth::Instruction::SUB;
}
}
else
{
2015-02-25 19:27:55 +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)
m_context << eth::Instruction::POP;
}
2015-02-25 19:27:55 +00:00
else if (m_dataType.getCategory() == Type::Category::Struct)
2015-02-25 14:14:22 +00:00
{
// stack layout: source_ref target_ref
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
solAssert(structType == _sourceType, "Struct assignment with conversion.");
for (auto const& member: structType.getMembers())
{
// assign each member that is not a mapping
TypePointer const& memberType = member.second;
if (memberType->getCategory() == Type::Category::Mapping)
continue;
m_context << structType.getStorageOffsetOfMember(member.first)
<< eth::Instruction::DUP3 << eth::Instruction::DUP2 << eth::Instruction::ADD;
// stack: source_ref target_ref member_offset source_member_ref
2015-02-25 19:27:55 +00:00
StorageItem(m_context, *memberType).retrieveValue(_location, true);
2015-02-25 14:14:22 +00:00
// stack: source_ref target_ref member_offset source_value...
m_context << eth::dupInstruction(2 + memberType->getSizeOnStack())
<< eth::dupInstruction(2 + memberType->getSizeOnStack()) << eth::Instruction::ADD;
// stack: source_ref target_ref member_offset source_value... target_member_ref
2015-02-25 19:27:55 +00:00
StorageItem(m_context, *memberType).storeValue(*memberType, _location, true);
2015-02-25 14:14:22 +00:00
m_context << eth::Instruction::POP;
}
if (_move)
m_context << eth::Instruction::POP;
else
m_context << eth::Instruction::SWAP1;
m_context << eth::Instruction::POP;
}
else
BOOST_THROW_EXCEPTION(InternalCompilerError()
<< errinfo_sourceLocation(_location) << errinfo_comment("Invalid non-value type for assignment."));
}
}
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)
m_context << eth::Instruction::DUP1;
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
{
// stack layout: ref
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
TypePointer const& memberType = member.second;
if (memberType->getCategory() == Type::Category::Mapping)
continue;
m_context << structType.getStorageOffsetOfMember(member.first)
<< eth::Instruction::DUP2 << eth::Instruction::ADD;
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)
m_context << eth::Instruction::POP;
2015-02-25 14:14:22 +00:00
}
else
{
2015-02-25 19:27:55 +00:00
if (m_size == 0 && _removeReference)
2015-02-25 14:14:22 +00:00
m_context << eth::Instruction::POP;
2015-02-25 19:27:55 +00:00
else if (m_size == 1)
m_context
<< u256(0) << (_removeReference ? eth::Instruction::SWAP1 : eth::Instruction::DUP2)
<< eth::Instruction::SSTORE;
else
{
if (!_removeReference)
m_context << eth::Instruction::DUP1;
for (unsigned i = 0; i < m_size; ++i)
if (i + 1 >= m_size)
m_context << u256(0) << eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
else
m_context << u256(0) << eth::Instruction::DUP2 << eth::Instruction::SSTORE
<< u256(1) << eth::Instruction::ADD;
}
2015-02-25 14:14:22 +00:00
}
}
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-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);
}