2019-04-30 16:32:56 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity 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.
|
|
|
|
|
|
|
|
solidity 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 solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Generator for code that handles LValues.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/codegen/ir/IRLValue.h>
|
|
|
|
|
|
|
|
#include <libsolidity/codegen/ir/IRGenerationContext.h>
|
|
|
|
#include <libsolidity/codegen/YulUtilFunctions.h>
|
2019-05-22 10:25:00 +00:00
|
|
|
#include <libsolidity/codegen/CompilerUtils.h>
|
2019-04-30 16:32:56 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
|
|
|
|
#include <libdevcore/Whiskers.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::solidity;
|
|
|
|
|
|
|
|
IRLocalVariable::IRLocalVariable(
|
|
|
|
IRGenerationContext& _context,
|
|
|
|
VariableDeclaration const& _varDecl
|
|
|
|
):
|
2019-08-08 15:27:35 +00:00
|
|
|
IRLValue(_context.utils(), _varDecl.annotation().type),
|
2019-04-30 16:32:56 +00:00
|
|
|
m_variableName(_context.localVariableName(_varDecl))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-13 17:04:22 +00:00
|
|
|
string IRLocalVariable::storeValue(string const& _value, Type const& _type) const
|
2019-04-30 16:32:56 +00:00
|
|
|
{
|
|
|
|
solAssert(_type == *m_type, "Storing different types - not necessarily a problem.");
|
2019-05-13 17:04:22 +00:00
|
|
|
return m_variableName + " := " + _value + "\n";
|
2019-04-30 16:32:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 10:25:00 +00:00
|
|
|
string IRLocalVariable::setToZero() const
|
|
|
|
{
|
2019-08-08 15:27:35 +00:00
|
|
|
return storeValue(m_utils.zeroValueFunction(*m_type) + "()", *m_type);
|
2019-05-22 10:25:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 16:32:56 +00:00
|
|
|
IRStorageItem::IRStorageItem(
|
|
|
|
IRGenerationContext& _context,
|
|
|
|
VariableDeclaration const& _varDecl
|
2019-06-17 13:26:40 +00:00
|
|
|
):
|
|
|
|
IRStorageItem(
|
2019-08-08 15:27:35 +00:00
|
|
|
_context.utils(),
|
|
|
|
*_varDecl.annotation().type,
|
|
|
|
_context.storageLocationOfVariable(_varDecl)
|
|
|
|
)
|
2019-06-05 17:32:30 +00:00
|
|
|
{ }
|
|
|
|
|
|
|
|
IRStorageItem::IRStorageItem(
|
2019-08-08 15:27:35 +00:00
|
|
|
YulUtilFunctions _utils,
|
2019-06-05 17:32:30 +00:00
|
|
|
Type const& _type,
|
|
|
|
std::pair<u256, unsigned> slot_offset
|
2019-06-17 13:26:40 +00:00
|
|
|
):
|
2019-08-08 15:27:35 +00:00
|
|
|
IRLValue(std::move(_utils), &_type),
|
2019-06-05 17:32:30 +00:00
|
|
|
m_slot(toCompactHexWithPrefix(slot_offset.first)),
|
|
|
|
m_offset(slot_offset.second)
|
2019-04-30 16:32:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-02 10:48:34 +00:00
|
|
|
IRStorageItem::IRStorageItem(
|
2019-08-08 15:27:35 +00:00
|
|
|
YulUtilFunctions _utils,
|
2019-05-02 10:48:34 +00:00
|
|
|
string _slot,
|
2019-06-05 17:32:30 +00:00
|
|
|
boost::variant<string, unsigned> _offset,
|
2019-05-02 10:48:34 +00:00
|
|
|
Type const& _type
|
|
|
|
):
|
2019-08-08 15:27:35 +00:00
|
|
|
IRLValue(std::move(_utils), &_type),
|
|
|
|
m_slot(std::move(_slot)),
|
2019-06-05 17:32:30 +00:00
|
|
|
m_offset(std::move(_offset))
|
2019-05-02 10:48:34 +00:00
|
|
|
{
|
2019-06-05 17:32:30 +00:00
|
|
|
solAssert(!m_offset.empty(), "");
|
|
|
|
solAssert(!m_slot.empty(), "");
|
2019-05-02 10:48:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 16:32:56 +00:00
|
|
|
string IRStorageItem::retrieveValue() const
|
|
|
|
{
|
|
|
|
if (!m_type->isValueType())
|
|
|
|
return m_slot;
|
|
|
|
solUnimplementedAssert(m_type->category() != Type::Category::Function, "");
|
2019-06-05 17:32:30 +00:00
|
|
|
if (m_offset.type() == typeid(string))
|
|
|
|
return
|
2019-08-08 15:27:35 +00:00
|
|
|
m_utils.readFromStorageDynamic(*m_type, false) +
|
2019-06-05 17:32:30 +00:00
|
|
|
"(" +
|
|
|
|
m_slot +
|
|
|
|
", " +
|
|
|
|
boost::get<string>(m_offset) +
|
|
|
|
")";
|
|
|
|
else if (m_offset.type() == typeid(unsigned))
|
|
|
|
return
|
2019-08-08 15:27:35 +00:00
|
|
|
m_utils.readFromStorage(*m_type, boost::get<unsigned>(m_offset), false) +
|
2019-06-05 17:32:30 +00:00
|
|
|
"(" +
|
|
|
|
m_slot +
|
|
|
|
")";
|
|
|
|
|
|
|
|
solAssert(false, "");
|
2019-04-30 16:32:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 17:04:22 +00:00
|
|
|
string IRStorageItem::storeValue(string const& _value, Type const& _sourceType) const
|
2019-04-30 16:32:56 +00:00
|
|
|
{
|
|
|
|
if (m_type->isValueType())
|
|
|
|
solAssert(_sourceType == *m_type, "Different type, but might not be an error.");
|
|
|
|
|
2019-10-28 10:39:30 +00:00
|
|
|
std::optional<unsigned> offset;
|
2019-06-05 17:32:30 +00:00
|
|
|
|
|
|
|
if (m_offset.type() == typeid(unsigned))
|
|
|
|
offset = get<unsigned>(m_offset);
|
|
|
|
|
|
|
|
return
|
2019-08-08 15:27:35 +00:00
|
|
|
m_utils.updateStorageValueFunction(*m_type, offset) +
|
2019-06-05 17:32:30 +00:00
|
|
|
"(" +
|
|
|
|
m_slot +
|
|
|
|
(m_offset.type() == typeid(string) ?
|
|
|
|
(", " + get<string>(m_offset)) :
|
|
|
|
""
|
|
|
|
) +
|
|
|
|
", " +
|
|
|
|
_value +
|
|
|
|
")\n";
|
2019-04-30 16:32:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 10:25:00 +00:00
|
|
|
string IRStorageItem::setToZero() const
|
|
|
|
{
|
2019-07-10 10:41:31 +00:00
|
|
|
return
|
2019-08-08 15:27:35 +00:00
|
|
|
m_utils.storageSetToZeroFunction(*m_type) +
|
2019-07-10 10:41:31 +00:00
|
|
|
"(" +
|
|
|
|
m_slot +
|
|
|
|
", " +
|
|
|
|
(
|
|
|
|
m_offset.type() == typeid(unsigned) ?
|
|
|
|
to_string(get<unsigned>(m_offset)) :
|
|
|
|
get<string>(m_offset)
|
|
|
|
) +
|
|
|
|
")\n";
|
2019-05-22 10:25:00 +00:00
|
|
|
}
|
2019-06-06 12:07:40 +00:00
|
|
|
|
2019-08-08 15:27:35 +00:00
|
|
|
IRStorageArrayLength::IRStorageArrayLength(
|
|
|
|
YulUtilFunctions _utils,
|
|
|
|
string _slot,
|
|
|
|
Type const& _type,
|
|
|
|
ArrayType const& _arrayType
|
|
|
|
):
|
|
|
|
IRLValue(std::move(_utils), &_type), m_arrayType(_arrayType), m_slot(move(_slot))
|
2019-06-06 12:07:40 +00:00
|
|
|
{
|
|
|
|
solAssert(*m_type == *TypeProvider::uint256(), "Must be uint256!");
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRStorageArrayLength::retrieveValue() const
|
|
|
|
{
|
2019-08-08 15:27:35 +00:00
|
|
|
return m_utils.arrayLengthFunction(m_arrayType) + "(" + m_slot + ")";
|
2019-06-06 12:07:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string IRStorageArrayLength::storeValue(std::string const& _value, Type const& _type) const
|
|
|
|
{
|
|
|
|
solAssert(_type == *m_type, "Different type, but might not be an error.");
|
|
|
|
|
2019-08-08 15:27:35 +00:00
|
|
|
return m_utils.resizeDynamicArrayFunction(m_arrayType) +
|
2019-06-06 12:07:40 +00:00
|
|
|
"(" +
|
|
|
|
m_slot +
|
|
|
|
", " +
|
|
|
|
_value +
|
|
|
|
")\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRStorageArrayLength::setToZero() const
|
|
|
|
{
|
|
|
|
return storeValue("0", *TypeProvider::uint256());
|
|
|
|
}
|
2019-06-27 11:36:06 +00:00
|
|
|
|
|
|
|
IRMemoryItem::IRMemoryItem(
|
2019-08-08 15:27:35 +00:00
|
|
|
YulUtilFunctions _utils,
|
2019-06-27 11:36:06 +00:00
|
|
|
std::string _address,
|
|
|
|
bool _byteArrayElement,
|
|
|
|
Type const& _type
|
|
|
|
):
|
2019-08-08 15:27:35 +00:00
|
|
|
IRLValue(std::move(_utils), &_type),
|
2019-06-27 11:36:06 +00:00
|
|
|
m_address(move(_address)),
|
|
|
|
m_byteArrayElement(_byteArrayElement)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
string IRMemoryItem::retrieveValue() const
|
|
|
|
{
|
|
|
|
if (m_byteArrayElement)
|
2019-08-08 15:27:35 +00:00
|
|
|
return m_utils.cleanupFunction(*m_type) +
|
2019-06-27 11:36:06 +00:00
|
|
|
"(mload(" +
|
|
|
|
m_address +
|
|
|
|
"))";
|
|
|
|
|
|
|
|
if (m_type->isValueType())
|
2019-08-08 15:27:35 +00:00
|
|
|
return m_utils.readFromMemory(*m_type) +
|
2019-06-27 11:36:06 +00:00
|
|
|
"(" +
|
|
|
|
m_address +
|
|
|
|
")";
|
|
|
|
else
|
|
|
|
return "mload(" + m_address + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRMemoryItem::storeValue(string const& _value, Type const& _type) const
|
|
|
|
{
|
|
|
|
if (!m_type->isValueType())
|
|
|
|
{
|
|
|
|
solUnimplementedAssert(_type == *m_type, "Conversion not implemented for assignment to memory.");
|
|
|
|
|
|
|
|
solAssert(m_type->sizeOnStack() == 1, "");
|
|
|
|
solAssert(dynamic_cast<ReferenceType const*>(m_type), "");
|
|
|
|
|
|
|
|
return "mstore(" + m_address + ", " + _value + ")\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
solAssert(_type.isValueType(), "");
|
|
|
|
|
|
|
|
string prepared = _value;
|
|
|
|
|
|
|
|
// Exists to see if this case ever happens
|
|
|
|
solAssert(_type == *m_type, "");
|
|
|
|
|
|
|
|
if (_type != *m_type)
|
|
|
|
prepared =
|
2019-08-08 15:27:35 +00:00
|
|
|
m_utils.conversionFunction(_type, *m_type) +
|
2019-06-27 11:36:06 +00:00
|
|
|
"(" +
|
|
|
|
_value +
|
|
|
|
")";
|
|
|
|
else
|
|
|
|
prepared =
|
2019-08-08 15:27:35 +00:00
|
|
|
m_utils.cleanupFunction(*m_type) +
|
2019-06-27 11:36:06 +00:00
|
|
|
"(" +
|
|
|
|
_value +
|
|
|
|
")";
|
|
|
|
|
|
|
|
if (m_byteArrayElement)
|
|
|
|
{
|
|
|
|
solAssert(*m_type == *TypeProvider::byte(), "");
|
|
|
|
return "mstore8(" + m_address + ", byte(0, " + prepared + "))\n";
|
|
|
|
}
|
|
|
|
else
|
2019-08-08 15:27:35 +00:00
|
|
|
return m_utils.writeToMemoryFunction(*m_type) +
|
2019-06-27 11:36:06 +00:00
|
|
|
"(" +
|
|
|
|
m_address +
|
|
|
|
", " +
|
|
|
|
prepared +
|
|
|
|
")\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRMemoryItem::setToZero() const
|
|
|
|
{
|
2019-08-08 15:27:35 +00:00
|
|
|
return storeValue(m_utils.zeroValueFunction(*m_type) + "()", *m_type);
|
2019-06-27 11:36:06 +00:00
|
|
|
}
|