2014-12-08 17:52:30 +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 2014
|
|
|
|
* Routines used by both the compiler and the expression compiler.
|
|
|
|
*/
|
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/codegen/CompilerUtils.h>
|
|
|
|
#include <libsolidity/ast/AST.h>
|
2016-04-01 20:11:01 +00:00
|
|
|
#include <libevmasm/Instruction.h>
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/codegen/ArrayUtils.h>
|
|
|
|
#include <libsolidity/codegen/LValue.h>
|
2014-12-08 17:52:30 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
|
2015-06-05 15:32:13 +00:00
|
|
|
const unsigned CompilerUtils::dataStartOffset = 4;
|
|
|
|
const size_t CompilerUtils::freeMemoryPointer = 64;
|
2015-06-15 10:10:41 +00:00
|
|
|
const unsigned CompilerUtils::identityContractAddress = 4;
|
2015-06-05 15:32:13 +00:00
|
|
|
|
|
|
|
void CompilerUtils::initialiseFreeMemoryPointer()
|
|
|
|
{
|
|
|
|
m_context << u256(freeMemoryPointer + 32);
|
|
|
|
storeFreeMemoryPointer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerUtils::fetchFreeMemoryPointer()
|
|
|
|
{
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(freeMemoryPointer) << Instruction::MLOAD;
|
2015-06-05 15:32:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerUtils::storeFreeMemoryPointer()
|
|
|
|
{
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(freeMemoryPointer) << Instruction::MSTORE;
|
2015-06-05 15:32:13 +00:00
|
|
|
}
|
2015-01-08 23:27:26 +00:00
|
|
|
|
2015-06-25 15:51:01 +00:00
|
|
|
void CompilerUtils::allocateMemory()
|
|
|
|
{
|
|
|
|
fetchFreeMemoryPointer();
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::DUP2 << Instruction::ADD;
|
2015-06-25 15:51:01 +00:00
|
|
|
storeFreeMemoryPointer();
|
|
|
|
}
|
|
|
|
|
2015-06-05 22:57:51 +00:00
|
|
|
void CompilerUtils::toSizeAfterFreeMemoryPointer()
|
|
|
|
{
|
|
|
|
fetchFreeMemoryPointer();
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1 << Instruction::SWAP2 << Instruction::SUB;
|
|
|
|
m_context << Instruction::SWAP1;
|
2015-06-05 22:57:51 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 14:41:39 +00:00
|
|
|
unsigned CompilerUtils::loadFromMemory(
|
|
|
|
unsigned _offset,
|
|
|
|
Type const& _type,
|
|
|
|
bool _fromCalldata,
|
|
|
|
bool _padToWordBoundaries
|
|
|
|
)
|
2014-12-10 16:15:17 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
solAssert(_type.category() != Type::Category::Array, "Unable to statically load dynamic type.");
|
2015-02-15 00:02:38 +00:00
|
|
|
m_context << u256(_offset);
|
|
|
|
return loadFromMemoryHelper(_type, _fromCalldata, _padToWordBoundaries);
|
|
|
|
}
|
|
|
|
|
2015-03-03 16:55:28 +00:00
|
|
|
void CompilerUtils::loadFromMemoryDynamic(
|
2015-03-05 14:41:39 +00:00
|
|
|
Type const& _type,
|
|
|
|
bool _fromCalldata,
|
|
|
|
bool _padToWordBoundaries,
|
|
|
|
bool _keepUpdatedMemoryOffset
|
|
|
|
)
|
2015-09-23 15:31:37 +00:00
|
|
|
{
|
2015-03-03 16:55:28 +00:00
|
|
|
if (_keepUpdatedMemoryOffset)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1;
|
2015-09-23 15:31:37 +00:00
|
|
|
|
|
|
|
if (auto arrayType = dynamic_cast<ArrayType const*>(&_type))
|
2015-03-03 16:55:28 +00:00
|
|
|
{
|
2015-09-23 15:31:37 +00:00
|
|
|
solAssert(!arrayType->isDynamicallySized(), "");
|
|
|
|
solAssert(!_fromCalldata, "");
|
|
|
|
solAssert(_padToWordBoundaries, "");
|
|
|
|
if (_keepUpdatedMemoryOffset)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << arrayType->memorySize() << Instruction::ADD;
|
2015-09-23 15:31:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned numBytes = loadFromMemoryHelper(_type, _fromCalldata, _padToWordBoundaries);
|
|
|
|
if (_keepUpdatedMemoryOffset)
|
|
|
|
{
|
|
|
|
// update memory counter
|
|
|
|
moveToStackTop(_type.sizeOnStack());
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(numBytes) << Instruction::ADD;
|
2015-09-23 15:31:37 +00:00
|
|
|
}
|
2015-03-03 16:55:28 +00:00
|
|
|
}
|
2014-12-10 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 12:52:13 +00:00
|
|
|
void CompilerUtils::storeInMemory(unsigned _offset)
|
2014-12-10 16:15:17 +00:00
|
|
|
{
|
2015-06-25 12:52:13 +00:00
|
|
|
unsigned numBytes = prepareMemoryStore(IntegerType(256), true);
|
2015-02-10 16:53:43 +00:00
|
|
|
if (numBytes > 0)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(_offset) << Instruction::MSTORE;
|
2015-02-10 16:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerUtils::storeInMemoryDynamic(Type const& _type, bool _padToWordBoundaries)
|
|
|
|
{
|
2015-06-25 15:51:01 +00:00
|
|
|
if (auto ref = dynamic_cast<ReferenceType const*>(&_type))
|
|
|
|
{
|
|
|
|
solAssert(ref->location() == DataLocation::Memory, "");
|
|
|
|
storeInMemoryDynamic(IntegerType(256), _padToWordBoundaries);
|
|
|
|
}
|
2015-07-07 23:13:56 +00:00
|
|
|
else if (auto str = dynamic_cast<StringLiteralType const*>(&_type))
|
|
|
|
{
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1;
|
2015-07-07 23:13:56 +00:00
|
|
|
storeStringData(bytesConstRef(str->value()));
|
|
|
|
if (_padToWordBoundaries)
|
|
|
|
m_context << u256(((str->value().size() + 31) / 32) * 32);
|
|
|
|
else
|
|
|
|
m_context << u256(str->value().size());
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::ADD;
|
2015-07-07 23:13:56 +00:00
|
|
|
}
|
2015-02-11 13:32:46 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned numBytes = prepareMemoryStore(_type, _padToWordBoundaries);
|
|
|
|
if (numBytes > 0)
|
|
|
|
{
|
2015-06-25 15:51:01 +00:00
|
|
|
solAssert(
|
2015-08-31 16:44:29 +00:00
|
|
|
_type.sizeOnStack() == 1,
|
2015-06-25 15:51:01 +00:00
|
|
|
"Memory store of types with stack size != 1 not implemented."
|
|
|
|
);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2 << Instruction::MSTORE;
|
|
|
|
m_context << u256(numBytes) << Instruction::ADD;
|
2015-02-11 13:32:46 +00:00
|
|
|
}
|
2014-12-11 13:19:11 +00:00
|
|
|
}
|
2014-12-10 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 10:10:41 +00:00
|
|
|
void CompilerUtils::encodeToMemory(
|
|
|
|
TypePointers const& _givenTypes,
|
|
|
|
TypePointers const& _targetTypes,
|
|
|
|
bool _padToWordBoundaries,
|
2015-10-02 20:34:47 +00:00
|
|
|
bool _copyDynamicDataInPlace,
|
|
|
|
bool _encodeAsLibraryTypes
|
2015-06-15 10:10:41 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
// stack: <v1> <v2> ... <vn> <mem>
|
|
|
|
TypePointers targetTypes = _targetTypes.empty() ? _givenTypes : _targetTypes;
|
|
|
|
solAssert(targetTypes.size() == _givenTypes.size(), "");
|
|
|
|
for (TypePointer& t: targetTypes)
|
2015-10-02 20:34:47 +00:00
|
|
|
t = t->mobileType()->interfaceType(_encodeAsLibraryTypes)->encodingType();
|
2015-06-15 10:10:41 +00:00
|
|
|
|
|
|
|
// Stack during operation:
|
|
|
|
// <v1> <v2> ... <vn> <mem_start> <dyn_head_1> ... <dyn_head_r> <end_of_mem>
|
|
|
|
// The values dyn_head_i are added during the first loop and they point to the head part
|
|
|
|
// of the ith dynamic parameter, which is filled once the dynamic parts are processed.
|
|
|
|
|
|
|
|
// store memory start pointer
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1;
|
2015-06-15 10:10:41 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
unsigned argSize = CompilerUtils::sizeOnStack(_givenTypes);
|
2015-06-15 10:10:41 +00:00
|
|
|
unsigned stackPos = 0; // advances through the argument values
|
|
|
|
unsigned dynPointers = 0; // number of dynamic head pointers on the stack
|
|
|
|
for (size_t i = 0; i < _givenTypes.size(); ++i)
|
|
|
|
{
|
|
|
|
TypePointer targetType = targetTypes[i];
|
|
|
|
solAssert(!!targetType, "Externalable type expected.");
|
|
|
|
if (targetType->isDynamicallySized() && !_copyDynamicDataInPlace)
|
|
|
|
{
|
|
|
|
// leave end_of_mem as dyn head pointer
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1 << u256(32) << Instruction::ADD;
|
2015-06-15 10:10:41 +00:00
|
|
|
dynPointers++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
copyToStackTop(argSize - stackPos + dynPointers + 2, _givenTypes[i]->sizeOnStack());
|
2015-06-15 10:10:41 +00:00
|
|
|
solAssert(!!targetType, "Externalable type expected.");
|
2015-06-16 15:20:41 +00:00
|
|
|
TypePointer type = targetType;
|
2015-10-04 09:05:57 +00:00
|
|
|
if (_givenTypes[i]->dataStoredIn(DataLocation::Storage) && targetType->isValueType())
|
|
|
|
{
|
|
|
|
// special case: convert storage reference type to value type - this is only
|
|
|
|
// possible for library calls where we just forward the storage reference
|
|
|
|
solAssert(_encodeAsLibraryTypes, "");
|
|
|
|
solAssert(_givenTypes[i]->sizeOnStack() == 1, "");
|
|
|
|
}
|
|
|
|
else if (
|
2015-06-17 10:01:39 +00:00
|
|
|
_givenTypes[i]->dataStoredIn(DataLocation::Storage) ||
|
2015-07-07 23:13:56 +00:00
|
|
|
_givenTypes[i]->dataStoredIn(DataLocation::CallData) ||
|
2015-08-31 16:44:29 +00:00
|
|
|
_givenTypes[i]->category() == Type::Category::StringLiteral
|
2015-06-17 10:01:39 +00:00
|
|
|
)
|
2015-06-16 15:20:41 +00:00
|
|
|
type = _givenTypes[i]; // delay conversion
|
|
|
|
else
|
|
|
|
convertType(*_givenTypes[i], *targetType, true);
|
2015-06-25 15:51:01 +00:00
|
|
|
if (auto arrayType = dynamic_cast<ArrayType const*>(type.get()))
|
|
|
|
ArrayUtils(m_context).copyArrayToMemory(*arrayType, _padToWordBoundaries);
|
|
|
|
else
|
|
|
|
storeInMemoryDynamic(*type, _padToWordBoundaries);
|
2015-06-15 10:10:41 +00:00
|
|
|
}
|
2015-08-31 16:44:29 +00:00
|
|
|
stackPos += _givenTypes[i]->sizeOnStack();
|
2015-06-15 10:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now copy the dynamic part
|
|
|
|
// Stack: <v1> <v2> ... <vn> <mem_start> <dyn_head_1> ... <dyn_head_r> <end_of_mem>
|
|
|
|
stackPos = 0;
|
|
|
|
unsigned thisDynPointer = 0;
|
|
|
|
for (size_t i = 0; i < _givenTypes.size(); ++i)
|
|
|
|
{
|
|
|
|
TypePointer targetType = targetTypes[i];
|
|
|
|
solAssert(!!targetType, "Externalable type expected.");
|
|
|
|
if (targetType->isDynamicallySized() && !_copyDynamicDataInPlace)
|
|
|
|
{
|
|
|
|
// copy tail pointer (=mem_end - mem_start) to memory
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << dupInstruction(2 + dynPointers) << Instruction::DUP2;
|
|
|
|
m_context << Instruction::SUB;
|
|
|
|
m_context << dupInstruction(2 + dynPointers - thisDynPointer);
|
|
|
|
m_context << Instruction::MSTORE;
|
2015-07-07 23:13:56 +00:00
|
|
|
// stack: ... <end_of_mem>
|
2015-08-31 16:44:29 +00:00
|
|
|
if (_givenTypes[i]->category() == Type::Category::StringLiteral)
|
2015-07-07 23:13:56 +00:00
|
|
|
{
|
|
|
|
auto const& strType = dynamic_cast<StringLiteralType const&>(*_givenTypes[i]);
|
|
|
|
m_context << u256(strType.value().size());
|
|
|
|
storeInMemoryDynamic(IntegerType(256), true);
|
|
|
|
// stack: ... <end_of_mem'>
|
|
|
|
storeInMemoryDynamic(strType, _padToWordBoundaries);
|
|
|
|
}
|
2015-06-15 10:10:41 +00:00
|
|
|
else
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
solAssert(_givenTypes[i]->category() == Type::Category::Array, "Unknown dynamic type.");
|
2015-07-07 23:13:56 +00:00
|
|
|
auto const& arrayType = dynamic_cast<ArrayType const&>(*_givenTypes[i]);
|
|
|
|
// now copy the array
|
2015-08-31 16:44:29 +00:00
|
|
|
copyToStackTop(argSize - stackPos + dynPointers + 2, arrayType.sizeOnStack());
|
2015-07-07 23:13:56 +00:00
|
|
|
// stack: ... <end_of_mem> <value...>
|
|
|
|
// copy length to memory
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << dupInstruction(1 + arrayType.sizeOnStack());
|
2015-09-25 15:13:29 +00:00
|
|
|
ArrayUtils(m_context).retrieveLength(arrayType, 1);
|
2015-07-07 23:13:56 +00:00
|
|
|
// stack: ... <end_of_mem> <value...> <end_of_mem'> <length>
|
|
|
|
storeInMemoryDynamic(IntegerType(256), true);
|
|
|
|
// stack: ... <end_of_mem> <value...> <end_of_mem''>
|
|
|
|
// copy the new memory pointer
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << swapInstruction(arrayType.sizeOnStack() + 1) << Instruction::POP;
|
2015-07-07 23:13:56 +00:00
|
|
|
// stack: ... <end_of_mem''> <value...>
|
|
|
|
// copy data part
|
|
|
|
ArrayUtils(m_context).copyArrayToMemory(arrayType, _padToWordBoundaries);
|
|
|
|
// stack: ... <end_of_mem'''>
|
2015-06-15 10:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
thisDynPointer++;
|
|
|
|
}
|
2015-08-31 16:44:29 +00:00
|
|
|
stackPos += _givenTypes[i]->sizeOnStack();
|
2015-06-15 10:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove unneeded stack elements (and retain memory pointer)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << swapInstruction(argSize + dynPointers + 1);
|
2015-06-15 10:10:41 +00:00
|
|
|
popStackSlots(argSize + dynPointers + 1);
|
|
|
|
}
|
|
|
|
|
2015-11-17 14:15:00 +00:00
|
|
|
void CompilerUtils::zeroInitialiseMemoryArray(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
auto repeat = m_context.newTag();
|
|
|
|
m_context << repeat;
|
|
|
|
pushZeroValue(*_type.baseType());
|
|
|
|
storeInMemoryDynamic(*_type.baseType());
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << u256(1) << Instruction::SWAP1;
|
|
|
|
m_context << Instruction::SUB << Instruction::SWAP1;
|
|
|
|
m_context << Instruction::DUP2;
|
2015-11-17 14:15:00 +00:00
|
|
|
m_context.appendConditionalJumpTo(repeat);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::POP;
|
2015-11-17 14:15:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 12:55:33 +00:00
|
|
|
void CompilerUtils::memoryCopy()
|
|
|
|
{
|
|
|
|
// Stack here: size target source
|
|
|
|
// stack for call: outsize target size source value contract gas
|
|
|
|
//@TODO do not use ::CALL if less than 32 bytes?
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP3 << Instruction::SWAP1;
|
2015-06-23 12:55:33 +00:00
|
|
|
m_context << u256(0) << u256(identityContractAddress);
|
|
|
|
// compute gas costs
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(32) << Instruction::DUP5 << u256(31) << Instruction::ADD;
|
2016-08-05 23:18:21 +00:00
|
|
|
static unsigned c_identityGas = 15;
|
|
|
|
static unsigned c_identityWordGas = 3;
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DIV << u256(c_identityWordGas) << Instruction::MUL;
|
|
|
|
m_context << u256(c_identityGas) << Instruction::ADD;
|
|
|
|
m_context << Instruction::CALL;
|
|
|
|
m_context << Instruction::POP; // ignore return value
|
2015-06-23 12:55:33 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 10:10:41 +00:00
|
|
|
void CompilerUtils::convertType(Type const& _typeOnStack, Type const& _targetType, bool _cleanupNeeded)
|
|
|
|
{
|
|
|
|
// For a type extension, we need to remove all higher-order bits that we might have ignored in
|
|
|
|
// previous operations.
|
|
|
|
// @todo: store in the AST whether the operand might have "dirty" higher order bits
|
|
|
|
|
|
|
|
if (_typeOnStack == _targetType && !_cleanupNeeded)
|
|
|
|
return;
|
2015-08-31 16:44:29 +00:00
|
|
|
Type::Category stackTypeCategory = _typeOnStack.category();
|
|
|
|
Type::Category targetTypeCategory = _targetType.category();
|
2015-06-15 10:10:41 +00:00
|
|
|
|
|
|
|
switch (stackTypeCategory)
|
|
|
|
{
|
|
|
|
case Type::Category::FixedBytes:
|
|
|
|
{
|
|
|
|
FixedBytesType const& typeOnStack = dynamic_cast<FixedBytesType const&>(_typeOnStack);
|
|
|
|
if (targetTypeCategory == Type::Category::Integer)
|
|
|
|
{
|
|
|
|
// conversion from bytes to integer. no need to clean the high bit
|
|
|
|
// only to shift right because of opposite alignment
|
|
|
|
IntegerType const& targetIntegerType = dynamic_cast<IntegerType const&>(_targetType);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << (u256(1) << (256 - typeOnStack.numBytes() * 8)) << Instruction::SWAP1 << Instruction::DIV;
|
2015-08-31 16:44:29 +00:00
|
|
|
if (targetIntegerType.numBits() < typeOnStack.numBytes() * 8)
|
2015-07-07 23:13:56 +00:00
|
|
|
convertType(IntegerType(typeOnStack.numBytes() * 8), _targetType, _cleanupNeeded);
|
2015-06-15 10:10:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-20 14:22:31 +00:00
|
|
|
// clear for conversion to longer bytes
|
2015-06-15 10:10:41 +00:00
|
|
|
solAssert(targetTypeCategory == Type::Category::FixedBytes, "Invalid type conversion requested.");
|
|
|
|
FixedBytesType const& targetType = dynamic_cast<FixedBytesType const&>(_targetType);
|
2016-05-20 14:22:31 +00:00
|
|
|
if (targetType.numBytes() > typeOnStack.numBytes() || _cleanupNeeded)
|
2015-06-15 10:10:41 +00:00
|
|
|
{
|
2016-05-20 14:22:31 +00:00
|
|
|
if (typeOnStack.numBytes() == 0)
|
|
|
|
m_context << Instruction::POP << u256(0);
|
2015-06-15 10:10:41 +00:00
|
|
|
else
|
2015-06-16 09:28:35 +00:00
|
|
|
{
|
2016-05-20 14:22:31 +00:00
|
|
|
m_context << ((u256(1) << (256 - typeOnStack.numBytes() * 8)) - 1);
|
|
|
|
m_context << Instruction::NOT << Instruction::AND;
|
2015-06-16 09:28:35 +00:00
|
|
|
}
|
2015-06-15 10:10:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Type::Category::Enum:
|
|
|
|
solAssert(targetTypeCategory == Type::Category::Integer || targetTypeCategory == Type::Category::Enum, "");
|
|
|
|
break;
|
2016-05-10 12:57:29 +00:00
|
|
|
case Type::Category::FixedPoint:
|
|
|
|
solAssert(false, "Not yet implemented - FixedPointType.");
|
2015-06-15 10:10:41 +00:00
|
|
|
case Type::Category::Integer:
|
|
|
|
case Type::Category::Contract:
|
2016-03-29 21:13:00 +00:00
|
|
|
case Type::Category::RationalNumber:
|
2015-06-15 10:10:41 +00:00
|
|
|
if (targetTypeCategory == Type::Category::FixedBytes)
|
|
|
|
{
|
2016-03-29 20:08:51 +00:00
|
|
|
solAssert(stackTypeCategory == Type::Category::Integer || stackTypeCategory == Type::Category::RationalNumber,
|
2015-06-15 10:10:41 +00:00
|
|
|
"Invalid conversion to FixedBytesType requested.");
|
|
|
|
// conversion from bytes to string. no need to clean the high bit
|
|
|
|
// only to shift left because of opposite alignment
|
|
|
|
FixedBytesType const& targetBytesType = dynamic_cast<FixedBytesType const&>(_targetType);
|
|
|
|
if (auto typeOnStack = dynamic_cast<IntegerType const*>(&_typeOnStack))
|
2015-08-31 16:44:29 +00:00
|
|
|
if (targetBytesType.numBytes() * 8 > typeOnStack->numBits())
|
2015-06-15 10:10:41 +00:00
|
|
|
cleanHigherOrderBits(*typeOnStack);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << (u256(1) << (256 - targetBytesType.numBytes() * 8)) << Instruction::MUL;
|
2015-06-15 10:10:41 +00:00
|
|
|
}
|
|
|
|
else if (targetTypeCategory == Type::Category::Enum)
|
|
|
|
// just clean
|
|
|
|
convertType(_typeOnStack, *_typeOnStack.mobileType(), true);
|
2016-02-18 22:39:11 +00:00
|
|
|
else if (targetTypeCategory == Type::Category::FixedPoint)
|
|
|
|
{
|
|
|
|
solAssert(
|
|
|
|
stackTypeCategory == Type::Category::Integer ||
|
2016-03-29 20:08:51 +00:00
|
|
|
stackTypeCategory == Type::Category::RationalNumber ||
|
2016-02-18 22:39:11 +00:00
|
|
|
stackTypeCategory == Type::Category::FixedPoint,
|
|
|
|
"Invalid conversion to FixedMxNType requested."
|
|
|
|
);
|
|
|
|
//shift all integer bits onto the left side of the fixed type
|
|
|
|
FixedPointType const& targetFixedPointType = dynamic_cast<FixedPointType const&>(_targetType);
|
|
|
|
if (auto typeOnStack = dynamic_cast<IntegerType const*>(&_typeOnStack))
|
|
|
|
if (targetFixedPointType.integerBits() > typeOnStack->numBits())
|
|
|
|
cleanHigherOrderBits(*typeOnStack);
|
2016-05-10 12:57:29 +00:00
|
|
|
solAssert(false, "Not yet implemented - FixedPointType.");
|
2016-02-18 22:39:11 +00:00
|
|
|
}
|
2015-06-15 10:10:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(targetTypeCategory == Type::Category::Integer || targetTypeCategory == Type::Category::Contract, "");
|
|
|
|
IntegerType addressType(0, IntegerType::Modifier::Address);
|
|
|
|
IntegerType const& targetType = targetTypeCategory == Type::Category::Integer
|
|
|
|
? dynamic_cast<IntegerType const&>(_targetType) : addressType;
|
2016-03-29 20:08:51 +00:00
|
|
|
if (stackTypeCategory == Type::Category::RationalNumber)
|
2015-06-15 10:10:41 +00:00
|
|
|
{
|
2016-03-29 20:08:51 +00:00
|
|
|
RationalNumberType const& constType = dynamic_cast<RationalNumberType const&>(_typeOnStack);
|
2015-06-15 10:10:41 +00:00
|
|
|
// We know that the stack is clean, we only have to clean for a narrowing conversion
|
|
|
|
// where cleanup is forced.
|
2016-05-10 12:57:29 +00:00
|
|
|
solAssert(!constType.isFractional(), "Not yet implemented - FixedPointType.");
|
2015-08-31 16:44:29 +00:00
|
|
|
if (targetType.numBits() < constType.integerType()->numBits() && _cleanupNeeded)
|
2015-06-15 10:10:41 +00:00
|
|
|
cleanHigherOrderBits(targetType);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IntegerType const& typeOnStack = stackTypeCategory == Type::Category::Integer
|
|
|
|
? dynamic_cast<IntegerType const&>(_typeOnStack) : addressType;
|
|
|
|
// Widening: clean up according to source type width
|
|
|
|
// Non-widening and force: clean up according to target type bits
|
2015-08-31 16:44:29 +00:00
|
|
|
if (targetType.numBits() > typeOnStack.numBits())
|
2015-06-15 10:10:41 +00:00
|
|
|
cleanHigherOrderBits(typeOnStack);
|
|
|
|
else if (_cleanupNeeded)
|
|
|
|
cleanHigherOrderBits(targetType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2015-07-07 23:13:56 +00:00
|
|
|
case Type::Category::StringLiteral:
|
|
|
|
{
|
|
|
|
auto const& literalType = dynamic_cast<StringLiteralType const&>(_typeOnStack);
|
|
|
|
string const& value = literalType.value();
|
|
|
|
bytesConstRef data(value);
|
|
|
|
if (targetTypeCategory == Type::Category::FixedBytes)
|
|
|
|
{
|
|
|
|
solAssert(data.size() <= 32, "");
|
|
|
|
m_context << h256::Arith(h256(data, h256::AlignLeft));
|
|
|
|
}
|
|
|
|
else if (targetTypeCategory == Type::Category::Array)
|
|
|
|
{
|
|
|
|
auto const& arrayType = dynamic_cast<ArrayType const&>(_targetType);
|
|
|
|
solAssert(arrayType.isByteArray(), "");
|
|
|
|
u256 storageSize(32 + ((data.size() + 31) / 32) * 32);
|
|
|
|
m_context << storageSize;
|
|
|
|
allocateMemory();
|
|
|
|
// stack: mempos
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1 << u256(data.size());
|
2015-07-07 23:13:56 +00:00
|
|
|
storeInMemoryDynamic(IntegerType(256));
|
|
|
|
// stack: mempos datapos
|
|
|
|
storeStringData(data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
solAssert(
|
|
|
|
false,
|
|
|
|
"Invalid conversion from string literal to " + _targetType.toString(false) + " requested."
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2015-06-15 10:10:41 +00:00
|
|
|
case Type::Category::Array:
|
|
|
|
{
|
|
|
|
solAssert(targetTypeCategory == stackTypeCategory, "");
|
|
|
|
ArrayType const& typeOnStack = dynamic_cast<ArrayType const&>(_typeOnStack);
|
|
|
|
ArrayType const& targetType = dynamic_cast<ArrayType const&>(_targetType);
|
|
|
|
switch (targetType.location())
|
|
|
|
{
|
2015-06-17 10:01:39 +00:00
|
|
|
case DataLocation::Storage:
|
2015-06-15 10:10:41 +00:00
|
|
|
// Other cases are done explicitly in LValue::storeValue, and only possible by assignment.
|
|
|
|
solAssert(
|
2015-08-04 09:06:57 +00:00
|
|
|
(targetType.isPointer() || (typeOnStack.isByteArray() && targetType.isByteArray())) &&
|
2015-06-17 10:01:39 +00:00
|
|
|
typeOnStack.location() == DataLocation::Storage,
|
2015-06-15 10:10:41 +00:00
|
|
|
"Invalid conversion to storage type."
|
|
|
|
);
|
|
|
|
break;
|
2015-06-17 10:01:39 +00:00
|
|
|
case DataLocation::Memory:
|
2015-06-15 10:10:41 +00:00
|
|
|
{
|
|
|
|
// Copy the array to a free position in memory, unless it is already in memory.
|
2015-06-17 10:01:39 +00:00
|
|
|
if (typeOnStack.location() != DataLocation::Memory)
|
2015-06-15 10:10:41 +00:00
|
|
|
{
|
|
|
|
// stack: <source ref> (variably sized)
|
2015-08-31 16:44:29 +00:00
|
|
|
unsigned stackSize = typeOnStack.sizeOnStack();
|
2015-06-25 15:51:01 +00:00
|
|
|
ArrayUtils(m_context).retrieveLength(typeOnStack);
|
|
|
|
|
|
|
|
// allocate memory
|
|
|
|
// stack: <source ref> (variably sized) <length>
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1;
|
2015-06-25 15:51:01 +00:00
|
|
|
ArrayUtils(m_context).convertLengthToSize(targetType, true);
|
|
|
|
// stack: <source ref> (variably sized) <length> <size>
|
|
|
|
if (targetType.isDynamicallySized())
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(0x20) << Instruction::ADD;
|
2015-06-25 15:51:01 +00:00
|
|
|
allocateMemory();
|
|
|
|
// stack: <source ref> (variably sized) <length> <mem start>
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1;
|
2015-06-25 15:51:01 +00:00
|
|
|
moveIntoStack(2 + stackSize);
|
|
|
|
if (targetType.isDynamicallySized())
|
2015-06-15 10:10:41 +00:00
|
|
|
{
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP2;
|
2015-06-25 15:51:01 +00:00
|
|
|
storeInMemoryDynamic(IntegerType(256));
|
2015-06-15 10:10:41 +00:00
|
|
|
}
|
2015-06-25 15:51:01 +00:00
|
|
|
// stack: <mem start> <source ref> (variably sized) <length> <mem data pos>
|
2015-08-31 16:44:29 +00:00
|
|
|
if (targetType.baseType()->isValueType())
|
2015-06-17 10:01:39 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
solAssert(typeOnStack.baseType()->isValueType(), "");
|
2015-06-25 15:51:01 +00:00
|
|
|
copyToStackTop(2 + stackSize, stackSize);
|
|
|
|
ArrayUtils(m_context).copyArrayToMemory(typeOnStack);
|
2015-06-17 10:01:39 +00:00
|
|
|
}
|
|
|
|
else
|
2015-06-25 15:51:01 +00:00
|
|
|
{
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(0) << Instruction::SWAP1;
|
2015-06-25 15:51:01 +00:00
|
|
|
// stack: <mem start> <source ref> (variably sized) <length> <counter> <mem data pos>
|
|
|
|
auto repeat = m_context.newTag();
|
|
|
|
m_context << repeat;
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP3 << Instruction::DUP3;
|
|
|
|
m_context << Instruction::LT << Instruction::ISZERO;
|
2015-06-25 15:51:01 +00:00
|
|
|
auto loopEnd = m_context.appendConditionalJump();
|
|
|
|
copyToStackTop(3 + stackSize, stackSize);
|
|
|
|
copyToStackTop(2 + stackSize, 1);
|
2015-06-26 18:27:56 +00:00
|
|
|
ArrayUtils(m_context).accessIndex(typeOnStack, false);
|
2015-07-14 09:58:16 +00:00
|
|
|
if (typeOnStack.location() == DataLocation::Storage)
|
2015-08-31 16:44:29 +00:00
|
|
|
StorageItem(m_context, *typeOnStack.baseType()).retrieveValue(SourceLocation(), true);
|
|
|
|
convertType(*typeOnStack.baseType(), *targetType.baseType(), _cleanupNeeded);
|
|
|
|
storeInMemoryDynamic(*targetType.baseType(), true);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << u256(1) << Instruction::ADD;
|
|
|
|
m_context << Instruction::SWAP1;
|
2015-06-25 15:51:01 +00:00
|
|
|
m_context.appendJumpTo(repeat);
|
|
|
|
m_context << loopEnd;
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-06-25 15:51:01 +00:00
|
|
|
}
|
|
|
|
// stack: <mem start> <source ref> (variably sized) <length> <mem data pos updated>
|
|
|
|
popStackSlots(2 + stackSize);
|
|
|
|
// Stack: <mem start>
|
2015-06-15 10:10:41 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-08-04 09:06:57 +00:00
|
|
|
case DataLocation::CallData:
|
|
|
|
solAssert(
|
|
|
|
targetType.isByteArray() &&
|
|
|
|
typeOnStack.isByteArray() &&
|
|
|
|
typeOnStack.location() == DataLocation::CallData,
|
|
|
|
"Invalid conversion to calldata type.");
|
|
|
|
break;
|
2015-06-15 10:10:41 +00:00
|
|
|
default:
|
2015-06-25 15:22:28 +00:00
|
|
|
solAssert(
|
|
|
|
false,
|
2015-06-24 15:25:36 +00:00
|
|
|
"Invalid type conversion " +
|
|
|
|
_typeOnStack.toString(false) +
|
|
|
|
" to " +
|
|
|
|
_targetType.toString(false) +
|
|
|
|
" requested."
|
|
|
|
);
|
2015-06-15 10:10:41 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::Struct:
|
|
|
|
{
|
|
|
|
solAssert(targetTypeCategory == stackTypeCategory, "");
|
|
|
|
auto& targetType = dynamic_cast<StructType const&>(_targetType);
|
2015-06-30 09:54:51 +00:00
|
|
|
auto& typeOnStack = dynamic_cast<StructType const&>(_typeOnStack);
|
2015-06-15 10:10:41 +00:00
|
|
|
solAssert(
|
2015-06-30 09:54:51 +00:00
|
|
|
targetType.location() != DataLocation::CallData &&
|
|
|
|
typeOnStack.location() != DataLocation::CallData
|
|
|
|
, "");
|
|
|
|
switch (targetType.location())
|
|
|
|
{
|
|
|
|
case DataLocation::Storage:
|
|
|
|
// Other cases are done explicitly in LValue::storeValue, and only possible by assignment.
|
|
|
|
solAssert(
|
|
|
|
targetType.isPointer() &&
|
|
|
|
typeOnStack.location() == DataLocation::Storage,
|
|
|
|
"Invalid conversion to storage type."
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case DataLocation::Memory:
|
|
|
|
// Copy the array to a free position in memory, unless it is already in memory.
|
|
|
|
if (typeOnStack.location() != DataLocation::Memory)
|
|
|
|
{
|
|
|
|
solAssert(typeOnStack.location() == DataLocation::Storage, "");
|
2015-07-14 09:58:16 +00:00
|
|
|
// stack: <source ref>
|
2015-06-30 09:54:51 +00:00
|
|
|
m_context << typeOnStack.memorySize();
|
|
|
|
allocateMemory();
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::SWAP1 << Instruction::DUP2;
|
2015-06-30 09:54:51 +00:00
|
|
|
// stack: <memory ptr> <source ref> <memory ptr>
|
2015-11-19 17:02:04 +00:00
|
|
|
for (auto const& member: typeOnStack.members(nullptr))
|
2015-06-30 09:54:51 +00:00
|
|
|
{
|
|
|
|
if (!member.type->canLiveOutsideStorage())
|
|
|
|
continue;
|
2015-08-31 16:44:29 +00:00
|
|
|
pair<u256, unsigned> const& offsets = typeOnStack.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);
|
|
|
|
StorageItem(m_context, *member.type).retrieveValue(SourceLocation(), true);
|
2015-08-31 16:44:29 +00:00
|
|
|
TypePointer targetMemberType = targetType.memberType(member.name);
|
2015-06-30 09:54:51 +00:00
|
|
|
solAssert(!!targetMemberType, "Member not found in target type.");
|
|
|
|
convertType(*member.type, *targetMemberType, true);
|
|
|
|
storeInMemoryDynamic(*targetMemberType, true);
|
|
|
|
}
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP << Instruction::POP;
|
2015-06-30 09:54:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DataLocation::CallData:
|
2015-07-03 16:15:05 +00:00
|
|
|
solAssert(false, "Invalid type conversion target location CallData.");
|
2015-06-30 09:54:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-06-15 10:10:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-10-14 13:19:50 +00:00
|
|
|
case Type::Category::Tuple:
|
|
|
|
{
|
|
|
|
TupleType const& sourceTuple = dynamic_cast<TupleType const&>(_typeOnStack);
|
|
|
|
TupleType const& targetTuple = dynamic_cast<TupleType const&>(_targetType);
|
2015-10-14 22:42:36 +00:00
|
|
|
// fillRight: remove excess values at right side, !fillRight: remove eccess values at left side
|
|
|
|
bool fillRight = !targetTuple.components().empty() && (
|
|
|
|
!targetTuple.components().back() ||
|
|
|
|
targetTuple.components().front()
|
|
|
|
);
|
2015-10-14 13:19:50 +00:00
|
|
|
unsigned depth = sourceTuple.sizeOnStack();
|
|
|
|
for (size_t i = 0; i < sourceTuple.components().size(); ++i)
|
|
|
|
{
|
2015-10-14 22:42:36 +00:00
|
|
|
TypePointer sourceType = sourceTuple.components()[i];
|
|
|
|
TypePointer targetType;
|
|
|
|
if (fillRight && i < targetTuple.components().size())
|
|
|
|
targetType = targetTuple.components()[i];
|
|
|
|
else if (!fillRight && targetTuple.components().size() + i >= sourceTuple.components().size())
|
|
|
|
targetType = targetTuple.components()[targetTuple.components().size() - (sourceTuple.components().size() - i)];
|
2015-10-14 13:19:50 +00:00
|
|
|
if (!sourceType)
|
|
|
|
{
|
|
|
|
solAssert(!targetType, "");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
unsigned sourceSize = sourceType->sizeOnStack();
|
2015-10-14 22:42:36 +00:00
|
|
|
unsigned targetSize = targetType ? targetType->sizeOnStack() : 0;
|
|
|
|
if (!targetType || *sourceType != *targetType || _cleanupNeeded)
|
2015-10-14 13:19:50 +00:00
|
|
|
{
|
2015-10-14 22:42:36 +00:00
|
|
|
if (targetType)
|
|
|
|
{
|
|
|
|
if (sourceSize > 0)
|
|
|
|
copyToStackTop(depth, sourceSize);
|
|
|
|
convertType(*sourceType, *targetType, _cleanupNeeded);
|
|
|
|
}
|
2015-10-14 13:19:50 +00:00
|
|
|
if (sourceSize > 0 || targetSize > 0)
|
|
|
|
{
|
|
|
|
// Move it back into its place.
|
|
|
|
for (unsigned j = 0; j < min(sourceSize, targetSize); ++j)
|
|
|
|
m_context <<
|
2016-04-04 11:41:35 +00:00
|
|
|
swapInstruction(depth + targetSize - sourceSize) <<
|
|
|
|
Instruction::POP;
|
2015-10-14 13:19:50 +00:00
|
|
|
// Value shrank
|
|
|
|
for (unsigned j = targetSize; j < sourceSize; ++j)
|
|
|
|
{
|
|
|
|
moveToStackTop(depth - 1, 1);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-10-14 13:19:50 +00:00
|
|
|
}
|
|
|
|
// Value grew
|
|
|
|
if (targetSize > sourceSize)
|
2015-10-31 22:13:45 +00:00
|
|
|
moveIntoStack(depth + targetSize - sourceSize - 1, targetSize - sourceSize);
|
2015-10-14 13:19:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
depth -= sourceSize;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-05-20 14:22:31 +00:00
|
|
|
case Type::Category::Bool:
|
|
|
|
solAssert(_targetType == _typeOnStack, "Invalid conversion for bool.");
|
|
|
|
if (_cleanupNeeded)
|
|
|
|
m_context << Instruction::ISZERO << Instruction::ISZERO;
|
2015-06-15 10:10:41 +00:00
|
|
|
default:
|
|
|
|
// All other types should not be convertible to non-equal types.
|
|
|
|
solAssert(_typeOnStack == _targetType, "Invalid type conversion requested.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-24 13:54:37 +00:00
|
|
|
void CompilerUtils::pushZeroValue(Type const& _type)
|
2015-06-25 15:51:01 +00:00
|
|
|
{
|
|
|
|
auto const* referenceType = dynamic_cast<ReferenceType const*>(&_type);
|
|
|
|
if (!referenceType || referenceType->location() == DataLocation::Storage)
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
for (size_t i = 0; i < _type.sizeOnStack(); ++i)
|
2015-06-25 15:51:01 +00:00
|
|
|
m_context << u256(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
solAssert(referenceType->location() == DataLocation::Memory, "");
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
m_context << u256(max(32u, _type.calldataEncodedSize()));
|
2015-06-25 15:51:01 +00:00
|
|
|
allocateMemory();
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::DUP1;
|
2015-06-25 15:51:01 +00:00
|
|
|
|
|
|
|
if (auto structType = dynamic_cast<StructType const*>(&_type))
|
2015-11-19 17:02:04 +00:00
|
|
|
for (auto const& member: structType->members(nullptr))
|
2015-06-25 15:51:01 +00:00
|
|
|
{
|
|
|
|
pushZeroValue(*member.type);
|
|
|
|
storeInMemoryDynamic(*member.type);
|
|
|
|
}
|
|
|
|
else if (auto arrayType = dynamic_cast<ArrayType const*>(&_type))
|
|
|
|
{
|
|
|
|
if (arrayType->isDynamicallySized())
|
|
|
|
{
|
|
|
|
// zero length
|
|
|
|
m_context << u256(0);
|
|
|
|
storeInMemoryDynamic(IntegerType(256));
|
|
|
|
}
|
2015-08-31 16:44:29 +00:00
|
|
|
else if (arrayType->length() > 0)
|
2015-06-25 15:51:01 +00:00
|
|
|
{
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << arrayType->length() << Instruction::SWAP1;
|
2015-06-25 15:51:01 +00:00
|
|
|
// stack: items_to_do memory_pos
|
2015-11-17 14:15:00 +00:00
|
|
|
zeroInitialiseMemoryArray(*arrayType);
|
|
|
|
// stack: updated_memory_pos
|
2015-06-25 15:51:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
solAssert(false, "Requested initialisation for unknown type: " + _type.toString());
|
|
|
|
|
|
|
|
// remove the updated memory pointer
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-06-25 15:51:01 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 17:52:30 +00:00
|
|
|
void CompilerUtils::moveToStackVariable(VariableDeclaration const& _variable)
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
unsigned const stackPosition = m_context.baseToCurrentStackOffset(m_context.baseStackOffsetOfVariable(_variable));
|
2015-09-16 14:56:30 +00:00
|
|
|
unsigned const size = _variable.annotation().type->sizeOnStack();
|
2015-03-13 12:14:51 +00:00
|
|
|
solAssert(stackPosition >= size, "Variable size and position mismatch.");
|
2014-12-10 13:33:30 +00:00
|
|
|
// move variable starting from its top end in the stack
|
2014-12-08 21:18:19 +00:00
|
|
|
if (stackPosition - size + 1 > 16)
|
2015-06-05 23:04:55 +00:00
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
CompilerError() <<
|
2015-08-31 16:44:29 +00:00
|
|
|
errinfo_sourceLocation(_variable.location()) <<
|
2015-06-05 23:04:55 +00:00
|
|
|
errinfo_comment("Stack too deep, try removing local variables.")
|
|
|
|
);
|
2014-12-08 17:52:30 +00:00
|
|
|
for (unsigned i = 0; i < size; ++i)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << swapInstruction(stackPosition - size + 1) << Instruction::POP;
|
2014-12-08 17:52:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 16:55:28 +00:00
|
|
|
void CompilerUtils::copyToStackTop(unsigned _stackDepth, unsigned _itemSize)
|
2014-12-10 11:51:26 +00:00
|
|
|
{
|
2015-06-05 23:04:55 +00:00
|
|
|
solAssert(_stackDepth <= 16, "Stack too deep, try removing local variables.");
|
2015-03-03 16:55:28 +00:00
|
|
|
for (unsigned i = 0; i < _itemSize; ++i)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << dupInstruction(_stackDepth);
|
2014-12-10 11:51:26 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 13:19:50 +00:00
|
|
|
void CompilerUtils::moveToStackTop(unsigned _stackDepth, unsigned _itemSize)
|
2015-04-21 08:59:48 +00:00
|
|
|
{
|
2015-11-30 16:32:54 +00:00
|
|
|
moveIntoStack(_itemSize, _stackDepth);
|
2015-04-21 08:59:48 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 13:19:50 +00:00
|
|
|
void CompilerUtils::moveIntoStack(unsigned _stackDepth, unsigned _itemSize)
|
2015-06-15 10:10:41 +00:00
|
|
|
{
|
2015-11-30 16:32:54 +00:00
|
|
|
if (_stackDepth <= _itemSize)
|
|
|
|
for (unsigned i = 0; i < _stackDepth; ++i)
|
|
|
|
rotateStackDown(_stackDepth + _itemSize);
|
|
|
|
else
|
|
|
|
for (unsigned i = 0; i < _itemSize; ++i)
|
|
|
|
rotateStackUp(_stackDepth + _itemSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerUtils::rotateStackUp(unsigned _items)
|
|
|
|
{
|
|
|
|
solAssert(_items - 1 <= 16, "Stack too deep, try removing local variables.");
|
|
|
|
for (unsigned i = 1; i < _items; ++i)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << swapInstruction(_items - i);
|
2015-11-30 16:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerUtils::rotateStackDown(unsigned _items)
|
|
|
|
{
|
|
|
|
solAssert(_items - 1 <= 16, "Stack too deep, try removing local variables.");
|
|
|
|
for (unsigned i = 1; i < _items; ++i)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << swapInstruction(i);
|
2015-06-15 10:10:41 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 17:52:30 +00:00
|
|
|
void CompilerUtils::popStackElement(Type const& _type)
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
popStackSlots(_type.sizeOnStack());
|
2015-04-01 12:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerUtils::popStackSlots(size_t _amount)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < _amount; ++i)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2014-12-08 17:52:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
unsigned CompilerUtils::sizeOnStack(vector<shared_ptr<Type const>> const& _variableTypes)
|
2014-12-08 21:18:19 +00:00
|
|
|
{
|
|
|
|
unsigned size = 0;
|
|
|
|
for (shared_ptr<Type const> const& type: _variableTypes)
|
2015-08-31 16:44:29 +00:00
|
|
|
size += type->sizeOnStack();
|
2014-12-08 21:18:19 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2015-06-25 12:52:13 +00:00
|
|
|
void CompilerUtils::computeHashStatic()
|
2015-02-11 13:24:34 +00:00
|
|
|
{
|
2015-06-25 12:52:13 +00:00
|
|
|
storeInMemory(0);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(32) << u256(0) << Instruction::SHA3;
|
2015-02-11 13:24:34 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 23:13:56 +00:00
|
|
|
void CompilerUtils::storeStringData(bytesConstRef _data)
|
|
|
|
{
|
|
|
|
//@todo provide both alternatives to the optimiser
|
|
|
|
// stack: mempos
|
|
|
|
if (_data.size() <= 128)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < _data.size(); i += 32)
|
|
|
|
{
|
|
|
|
m_context << h256::Arith(h256(_data.cropped(i), h256::AlignLeft));
|
|
|
|
storeInMemoryDynamic(IntegerType(256));
|
|
|
|
}
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-07-07 23:13:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// stack: mempos mempos_data
|
|
|
|
m_context.appendData(_data.toBytes());
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(_data.size()) << Instruction::SWAP2;
|
|
|
|
m_context << Instruction::CODECOPY;
|
2015-07-07 23:13:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-15 00:02:38 +00:00
|
|
|
unsigned CompilerUtils::loadFromMemoryHelper(Type const& _type, bool _fromCalldata, bool _padToWordBoundaries)
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
unsigned numBytes = _type.calldataEncodedSize(_padToWordBoundaries);
|
|
|
|
bool leftAligned = _type.category() == Type::Category::FixedBytes;
|
2015-02-15 00:02:38 +00:00
|
|
|
if (numBytes == 0)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP << u256(0);
|
2015-02-15 00:02:38 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(numBytes <= 32, "Static memory load of more than 32 bytes requested.");
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << (_fromCalldata ? Instruction::CALLDATALOAD : Instruction::MLOAD);
|
2015-02-15 00:02:38 +00:00
|
|
|
if (numBytes != 32)
|
|
|
|
{
|
|
|
|
// add leading or trailing zeros by dividing/multiplying depending on alignment
|
|
|
|
u256 shiftFactor = u256(1) << ((32 - numBytes) * 8);
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << shiftFactor << Instruction::SWAP1 << Instruction::DIV;
|
2015-02-15 00:02:38 +00:00
|
|
|
if (leftAligned)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << shiftFactor << Instruction::MUL;
|
2015-02-15 00:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return numBytes;
|
|
|
|
}
|
|
|
|
|
2015-06-15 10:10:41 +00:00
|
|
|
void CompilerUtils::cleanHigherOrderBits(IntegerType const& _typeOnStack)
|
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
if (_typeOnStack.numBits() == 256)
|
2015-06-15 10:10:41 +00:00
|
|
|
return;
|
|
|
|
else if (_typeOnStack.isSigned())
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << u256(_typeOnStack.numBits() / 8 - 1) << Instruction::SIGNEXTEND;
|
2015-06-15 10:10:41 +00:00
|
|
|
else
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << ((u256(1) << _typeOnStack.numBits()) - 1) << Instruction::AND;
|
2015-06-15 10:10:41 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 13:32:46 +00:00
|
|
|
unsigned CompilerUtils::prepareMemoryStore(Type const& _type, bool _padToWordBoundaries) const
|
2015-02-10 16:53:43 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
unsigned numBytes = _type.calldataEncodedSize(_padToWordBoundaries);
|
|
|
|
bool leftAligned = _type.category() == Type::Category::FixedBytes;
|
2015-02-10 16:53:43 +00:00
|
|
|
if (numBytes == 0)
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << Instruction::POP;
|
2015-02-10 16:53:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(numBytes <= 32, "Memory store of more than 32 bytes requested.");
|
|
|
|
if (numBytes != 32 && !leftAligned && !_padToWordBoundaries)
|
|
|
|
// shift the value accordingly before storing
|
2016-04-04 11:41:35 +00:00
|
|
|
m_context << (u256(1) << ((32 - numBytes) * 8)) << Instruction::MUL;
|
2015-02-10 16:53:43 +00:00
|
|
|
}
|
|
|
|
return numBytes;
|
|
|
|
}
|
|
|
|
|
2014-12-08 17:52:30 +00:00
|
|
|
}
|
|
|
|
}
|