mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Dynamic memory.
This commit is contained in:
parent
c2a9419e49
commit
35ec81971a
3
AST.cpp
3
AST.cpp
@ -462,9 +462,6 @@ void FunctionDefinition::checkTypeRequirements()
|
|||||||
{
|
{
|
||||||
if (!var->getType()->canLiveOutsideStorage())
|
if (!var->getType()->canLiveOutsideStorage())
|
||||||
BOOST_THROW_EXCEPTION(var->createTypeError("Type is required to live outside storage."));
|
BOOST_THROW_EXCEPTION(var->createTypeError("Type is required to live outside storage."));
|
||||||
// todo delete when will be implemented arrays as parameter type in internal functions
|
|
||||||
if (getVisibility() == Visibility::Public && var->getType()->getCategory() == Type::Category::Array)
|
|
||||||
BOOST_THROW_EXCEPTION(var->createTypeError("Arrays only implemented for external functions."));
|
|
||||||
if (getVisibility() >= Visibility::Public && !(var->getType()->externalType()))
|
if (getVisibility() >= Visibility::Public && !(var->getType()->externalType()))
|
||||||
BOOST_THROW_EXCEPTION(var->createTypeError("Internal type is not allowed for public and external functions."));
|
BOOST_THROW_EXCEPTION(var->createTypeError("Internal type is not allowed for public and external functions."));
|
||||||
}
|
}
|
||||||
|
51
Compiler.cpp
51
Compiler.cpp
@ -52,6 +52,7 @@ void Compiler::compileContract(ContractDefinition const& _contract,
|
|||||||
map<ContractDefinition const*, bytes const*> const& _contracts)
|
map<ContractDefinition const*, bytes const*> const& _contracts)
|
||||||
{
|
{
|
||||||
m_context = CompilerContext(); // clear it just in case
|
m_context = CompilerContext(); // clear it just in case
|
||||||
|
CompilerUtils(m_context).initialiseFreeMemoryPointer();
|
||||||
initializeContext(_contract, _contracts);
|
initializeContext(_contract, _contracts);
|
||||||
appendFunctionSelector(_contract);
|
appendFunctionSelector(_contract);
|
||||||
set<Declaration const*> functions = m_context.getFunctionsWithoutCode();
|
set<Declaration const*> functions = m_context.getFunctionsWithoutCode();
|
||||||
@ -67,6 +68,7 @@ void Compiler::compileContract(ContractDefinition const& _contract,
|
|||||||
|
|
||||||
// Swap the runtime context with the creation-time context
|
// Swap the runtime context with the creation-time context
|
||||||
swap(m_context, m_runtimeContext);
|
swap(m_context, m_runtimeContext);
|
||||||
|
CompilerUtils(m_context).initialiseFreeMemoryPointer();
|
||||||
initializeContext(_contract, _contracts);
|
initializeContext(_contract, _contracts);
|
||||||
packIntoContractCreator(_contract, m_runtimeContext);
|
packIntoContractCreator(_contract, m_runtimeContext);
|
||||||
if (m_optimize)
|
if (m_optimize)
|
||||||
@ -233,31 +235,42 @@ void Compiler::appendCalldataUnpacker(TypePointers const& _typeParameters, bool
|
|||||||
m_context << u256(CompilerUtils::dataStartOffset);
|
m_context << u256(CompilerUtils::dataStartOffset);
|
||||||
for (TypePointer const& type: _typeParameters)
|
for (TypePointer const& type: _typeParameters)
|
||||||
{
|
{
|
||||||
switch (type->getCategory())
|
if (type->getCategory() == Type::Category::Array)
|
||||||
{
|
{
|
||||||
case Type::Category::Array:
|
auto const& arrayType = dynamic_cast<ArrayType const&>(*type);
|
||||||
if (type->isDynamicallySized())
|
if (arrayType.location() == ReferenceType::Location::CallData)
|
||||||
{
|
{
|
||||||
// put on stack: data_pointer length
|
if (type->isDynamicallySized())
|
||||||
CompilerUtils(m_context).loadFromMemoryDynamic(IntegerType(256), !_fromMemory);
|
{
|
||||||
// stack: data_offset next_pointer
|
// put on stack: data_pointer length
|
||||||
//@todo once we support nested arrays, this offset needs to be dynamic.
|
CompilerUtils(m_context).loadFromMemoryDynamic(IntegerType(256), !_fromMemory);
|
||||||
m_context << eth::Instruction::SWAP1 << u256(CompilerUtils::dataStartOffset);
|
// stack: data_offset next_pointer
|
||||||
m_context << eth::Instruction::ADD;
|
//@todo once we support nested arrays, this offset needs to be dynamic.
|
||||||
// stack: next_pointer data_pointer
|
m_context << eth::Instruction::SWAP1 << u256(CompilerUtils::dataStartOffset);
|
||||||
// retrieve length
|
m_context << eth::Instruction::ADD;
|
||||||
CompilerUtils(m_context).loadFromMemoryDynamic(IntegerType(256), !_fromMemory, true);
|
// stack: next_pointer data_pointer
|
||||||
// stack: next_pointer length data_pointer
|
// retrieve length
|
||||||
m_context << eth::Instruction::SWAP2;
|
CompilerUtils(m_context).loadFromMemoryDynamic(IntegerType(256), !_fromMemory, true);
|
||||||
|
// stack: next_pointer length data_pointer
|
||||||
|
m_context << eth::Instruction::SWAP2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// leave the pointer on the stack
|
||||||
|
m_context << eth::Instruction::DUP1;
|
||||||
|
m_context << u256(type->getCalldataEncodedSize()) << eth::Instruction::ADD;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// leave the pointer on the stack
|
solAssert(arrayType.location() == ReferenceType::Location::Memory, "");
|
||||||
m_context << eth::Instruction::DUP1;
|
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
||||||
m_context << u256(type->getCalldataEncodedSize()) << eth::Instruction::ADD;
|
CompilerUtils(m_context).storeInMemoryDynamic(*type);
|
||||||
|
CompilerUtils(m_context).storeFreeMemoryPointer();
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
default:
|
else
|
||||||
|
{
|
||||||
solAssert(!type->isDynamicallySized(), "Unknown dynamically sized type: " + type->toString());
|
solAssert(!type->isDynamicallySized(), "Unknown dynamically sized type: " + type->toString());
|
||||||
CompilerUtils(m_context).loadFromMemoryDynamic(*type, !_fromMemory, true);
|
CompilerUtils(m_context).loadFromMemoryDynamic(*type, !_fromMemory, true);
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,24 @@ namespace dev
|
|||||||
namespace solidity
|
namespace solidity
|
||||||
{
|
{
|
||||||
|
|
||||||
const unsigned int CompilerUtils::dataStartOffset = 4;
|
const unsigned CompilerUtils::dataStartOffset = 4;
|
||||||
|
const size_t CompilerUtils::freeMemoryPointer = 64;
|
||||||
|
|
||||||
|
void CompilerUtils::initialiseFreeMemoryPointer()
|
||||||
|
{
|
||||||
|
m_context << u256(freeMemoryPointer + 32);
|
||||||
|
storeFreeMemoryPointer();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CompilerUtils::fetchFreeMemoryPointer()
|
||||||
|
{
|
||||||
|
m_context << u256(freeMemoryPointer) << eth::Instruction::MLOAD;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CompilerUtils::storeFreeMemoryPointer()
|
||||||
|
{
|
||||||
|
m_context << u256(freeMemoryPointer) << eth::Instruction::MSTORE;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned CompilerUtils::loadFromMemory(
|
unsigned CompilerUtils::loadFromMemory(
|
||||||
unsigned _offset,
|
unsigned _offset,
|
||||||
|
@ -35,6 +35,13 @@ class CompilerUtils
|
|||||||
public:
|
public:
|
||||||
CompilerUtils(CompilerContext& _context): m_context(_context) {}
|
CompilerUtils(CompilerContext& _context): m_context(_context) {}
|
||||||
|
|
||||||
|
/// Stores the initial value of the free-memory-pointer at its position;
|
||||||
|
void initialiseFreeMemoryPointer();
|
||||||
|
/// Copies the free memory pointer to the stack.
|
||||||
|
void fetchFreeMemoryPointer();
|
||||||
|
/// Stores the free memory pointer from the stack.
|
||||||
|
void storeFreeMemoryPointer();
|
||||||
|
|
||||||
/// Loads data from memory to the stack.
|
/// Loads data from memory to the stack.
|
||||||
/// @param _offset offset in memory (or calldata)
|
/// @param _offset offset in memory (or calldata)
|
||||||
/// @param _type data type to load
|
/// @param _type data type to load
|
||||||
@ -95,7 +102,10 @@ public:
|
|||||||
|
|
||||||
/// Bytes we need to the start of call data.
|
/// Bytes we need to the start of call data.
|
||||||
/// - The size in bytes of the function (hash) identifier.
|
/// - The size in bytes of the function (hash) identifier.
|
||||||
static const unsigned int dataStartOffset;
|
static const unsigned dataStartOffset;
|
||||||
|
|
||||||
|
/// Position of the free-memory-pointer in memory;
|
||||||
|
static const size_t freeMemoryPointer;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Prepares the given type for storing in memory by shifting it if necessary.
|
/// Prepares the given type for storing in memory by shifting it if necessary.
|
||||||
|
@ -1061,22 +1061,32 @@ void ExpressionCompiler::appendExternalFunctionCall(
|
|||||||
bool returnSuccessCondition =
|
bool returnSuccessCondition =
|
||||||
_functionType.getLocation() == FunctionType::Location::Bare ||
|
_functionType.getLocation() == FunctionType::Location::Bare ||
|
||||||
_functionType.getLocation() == FunctionType::Location::BareCallCode;
|
_functionType.getLocation() == FunctionType::Location::BareCallCode;
|
||||||
|
|
||||||
|
// Output data will be at FreeMemPtr, replacing input data.
|
||||||
|
|
||||||
//@todo only return the first return value for now
|
//@todo only return the first return value for now
|
||||||
Type const* firstType = _functionType.getReturnParameterTypes().empty() ? nullptr :
|
Type const* firstType = _functionType.getReturnParameterTypes().empty() ? nullptr :
|
||||||
_functionType.getReturnParameterTypes().front().get();
|
_functionType.getReturnParameterTypes().front().get();
|
||||||
unsigned retSize = firstType ? firstType->getCalldataEncodedSize() : 0;
|
unsigned retSize = firstType ? firstType->getCalldataEncodedSize() : 0;
|
||||||
if (returnSuccessCondition)
|
if (returnSuccessCondition)
|
||||||
retSize = 0; // return value actually is success condition
|
retSize = 0; // return value actually is success condition
|
||||||
m_context << u256(retSize) << u256(0);
|
// put on stack: <size of output> <memory pos of output>
|
||||||
|
m_context << u256(retSize);
|
||||||
|
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
||||||
|
|
||||||
if (_functionType.isBareCall())
|
//@TODO CHECK ALL CALLS OF appendTypeMoveToMemory
|
||||||
m_context << u256(0);
|
|
||||||
else
|
// copy arguments to memory and
|
||||||
|
// put on stack: <size of input> <memory pos of input>
|
||||||
|
m_context << eth::Instruction::DUP1 << eth::Instruction::DUP1;
|
||||||
|
if (!_functionType.isBareCall())
|
||||||
{
|
{
|
||||||
// copy function identifier
|
// copy function identifier
|
||||||
m_context << eth::dupInstruction(gasValueSize + 3);
|
m_context << eth::dupInstruction(gasValueSize + 3);
|
||||||
CompilerUtils(m_context).storeInMemory(0, IntegerType(CompilerUtils::dataStartOffset * 8));
|
CompilerUtils(m_context).storeInMemoryDynamic(
|
||||||
m_context << u256(CompilerUtils::dataStartOffset);
|
IntegerType(CompilerUtils::dataStartOffset * 8),
|
||||||
|
false
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// For bare call, activate "4 byte pad exception": If the first argument has exactly 4 bytes,
|
// For bare call, activate "4 byte pad exception": If the first argument has exactly 4 bytes,
|
||||||
@ -1090,10 +1100,11 @@ void ExpressionCompiler::appendExternalFunctionCall(
|
|||||||
_functionType.getLocation() == FunctionType::Location::BareCallCode,
|
_functionType.getLocation() == FunctionType::Location::BareCallCode,
|
||||||
_functionType.takesArbitraryParameters()
|
_functionType.takesArbitraryParameters()
|
||||||
);
|
);
|
||||||
|
// now on stack: ... <pos of output = pos of input> <pos of input> <end of input>
|
||||||
|
m_context << eth::Instruction::SUB << eth::Instruction::DUP2;
|
||||||
|
|
||||||
// CALL arguments: outSize, outOff, inSize, (already present up to here)
|
// CALL arguments: outSize, outOff, inSize, inOff (already present up to here)
|
||||||
// inOff, value, addr, gas (stack top)
|
// value, addr, gas (stack top)
|
||||||
m_context << u256(0);
|
|
||||||
if (_functionType.valueSet())
|
if (_functionType.valueSet())
|
||||||
m_context << eth::dupInstruction(m_context.baseToCurrentStackOffset(valueStackPos));
|
m_context << eth::dupInstruction(m_context.baseToCurrentStackOffset(valueStackPos));
|
||||||
else
|
else
|
||||||
@ -1141,11 +1152,15 @@ void ExpressionCompiler::appendExternalFunctionCall(
|
|||||||
else if (_functionType.getLocation() == FunctionType::Location::RIPEMD160)
|
else if (_functionType.getLocation() == FunctionType::Location::RIPEMD160)
|
||||||
{
|
{
|
||||||
// fix: built-in contract returns right-aligned data
|
// fix: built-in contract returns right-aligned data
|
||||||
CompilerUtils(m_context).loadFromMemory(0, IntegerType(160), false, true);
|
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
||||||
|
CompilerUtils(m_context).loadFromMemoryDynamic(IntegerType(160), false, true, false);
|
||||||
appendTypeConversion(IntegerType(160), FixedBytesType(20));
|
appendTypeConversion(IntegerType(160), FixedBytesType(20));
|
||||||
}
|
}
|
||||||
else if (firstType)
|
else if (firstType)
|
||||||
CompilerUtils(m_context).loadFromMemory(0, *firstType, false, true);
|
{
|
||||||
|
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
||||||
|
CompilerUtils(m_context).loadFromMemoryDynamic(*firstType, false, true, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExpressionCompiler::appendArgumentsCopyToMemory(
|
void ExpressionCompiler::appendArgumentsCopyToMemory(
|
||||||
|
Loading…
Reference in New Issue
Block a user