mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Use dynamic memory for argument encoding.
This commit is contained in:
parent
35ec81971a
commit
02d5716944
@ -50,6 +50,13 @@ void CompilerUtils::storeFreeMemoryPointer()
|
|||||||
m_context << u256(freeMemoryPointer) << eth::Instruction::MSTORE;
|
m_context << u256(freeMemoryPointer) << eth::Instruction::MSTORE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CompilerUtils::toSizeAfterFreeMemoryPointer()
|
||||||
|
{
|
||||||
|
fetchFreeMemoryPointer();
|
||||||
|
m_context << eth::Instruction::DUP1 << eth::Instruction::SWAP2 << eth::Instruction::SUB;
|
||||||
|
m_context << eth::Instruction::SWAP1;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned CompilerUtils::loadFromMemory(
|
unsigned CompilerUtils::loadFromMemory(
|
||||||
unsigned _offset,
|
unsigned _offset,
|
||||||
Type const& _type,
|
Type const& _type,
|
||||||
@ -204,6 +211,7 @@ unsigned CompilerUtils::getSizeOnStack(vector<shared_ptr<Type const>> const& _va
|
|||||||
void CompilerUtils::computeHashStatic(Type const& _type, bool _padToWordBoundaries)
|
void CompilerUtils::computeHashStatic(Type const& _type, bool _padToWordBoundaries)
|
||||||
{
|
{
|
||||||
unsigned length = storeInMemory(0, _type, _padToWordBoundaries);
|
unsigned length = storeInMemory(0, _type, _padToWordBoundaries);
|
||||||
|
solAssert(length <= CompilerUtils::freeMemoryPointer, "");
|
||||||
m_context << u256(length) << u256(0) << eth::Instruction::SHA3;
|
m_context << u256(length) << u256(0) << eth::Instruction::SHA3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,6 +41,8 @@ public:
|
|||||||
void fetchFreeMemoryPointer();
|
void fetchFreeMemoryPointer();
|
||||||
/// Stores the free memory pointer from the stack.
|
/// Stores the free memory pointer from the stack.
|
||||||
void storeFreeMemoryPointer();
|
void storeFreeMemoryPointer();
|
||||||
|
/// Appends code that transforms memptr to (memptr - free_memptr) memptr
|
||||||
|
void toSizeAfterFreeMemoryPointer();
|
||||||
|
|
||||||
/// 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)
|
||||||
@ -74,7 +76,7 @@ public:
|
|||||||
bool _padToWordBoundaries = false
|
bool _padToWordBoundaries = false
|
||||||
);
|
);
|
||||||
/// Dynamic version of @see storeInMemory, expects the memory offset below the value on the stack
|
/// Dynamic version of @see storeInMemory, expects the memory offset below the value on the stack
|
||||||
/// and also updates that.
|
/// and also updates that. For arrays, only copies the data part.
|
||||||
/// Stack pre: memory_offset value...
|
/// Stack pre: memory_offset value...
|
||||||
/// Stack post: (memory_offset+length)
|
/// Stack post: (memory_offset+length)
|
||||||
void storeInMemoryDynamic(Type const& _type, bool _padToWordBoundaries = true);
|
void storeInMemoryDynamic(Type const& _type, bool _padToWordBoundaries = true);
|
||||||
|
@ -73,6 +73,7 @@ void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const&
|
|||||||
{
|
{
|
||||||
if (auto mappingType = dynamic_cast<MappingType const*>(returnType.get()))
|
if (auto mappingType = dynamic_cast<MappingType const*>(returnType.get()))
|
||||||
{
|
{
|
||||||
|
solAssert(CompilerUtils::freeMemoryPointer >= 0x40, "");
|
||||||
// pop offset
|
// pop offset
|
||||||
m_context << eth::Instruction::POP;
|
m_context << eth::Instruction::POP;
|
||||||
// move storage offset to memory.
|
// move storage offset to memory.
|
||||||
@ -470,21 +471,28 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
|
|||||||
_functionCall.getExpression().accept(*this);
|
_functionCall.getExpression().accept(*this);
|
||||||
solAssert(!function.gasSet(), "Gas limit set for contract creation.");
|
solAssert(!function.gasSet(), "Gas limit set for contract creation.");
|
||||||
solAssert(function.getReturnParameterTypes().size() == 1, "");
|
solAssert(function.getReturnParameterTypes().size() == 1, "");
|
||||||
|
TypePointers argumentTypes;
|
||||||
|
for (auto const& arg: arguments)
|
||||||
|
{
|
||||||
|
arg->accept(*this);
|
||||||
|
argumentTypes.push_back(arg->getType());
|
||||||
|
}
|
||||||
ContractDefinition const& contract = dynamic_cast<ContractType const&>(
|
ContractDefinition const& contract = dynamic_cast<ContractType const&>(
|
||||||
*function.getReturnParameterTypes().front()).getContractDefinition();
|
*function.getReturnParameterTypes().front()).getContractDefinition();
|
||||||
// copy the contract's code into memory
|
// copy the contract's code into memory
|
||||||
bytes const& bytecode = m_context.getCompiledContract(contract);
|
bytes const& bytecode = m_context.getCompiledContract(contract);
|
||||||
m_context << u256(bytecode.size());
|
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
||||||
|
m_context << u256(bytecode.size()) << eth::Instruction::DUP1;
|
||||||
//@todo could be done by actually appending the Assembly, but then we probably need to compile
|
//@todo could be done by actually appending the Assembly, but then we probably need to compile
|
||||||
// multiple times. Will revisit once external fuctions are inlined.
|
// multiple times. Will revisit once external fuctions are inlined.
|
||||||
m_context.appendData(bytecode);
|
m_context.appendData(bytecode);
|
||||||
//@todo copy to memory position 0, shift as soon as we use memory
|
m_context << eth::Instruction::DUP4 << eth::Instruction::CODECOPY;
|
||||||
m_context << u256(0) << eth::Instruction::CODECOPY;
|
|
||||||
|
|
||||||
m_context << u256(bytecode.size());
|
m_context << eth::Instruction::ADD;
|
||||||
appendArgumentsCopyToMemory(arguments, function.getParameterTypes());
|
encodeToMemory(argumentTypes, function.getParameterTypes());
|
||||||
// size, offset, endowment
|
// now on stack: memory_end_ptr
|
||||||
m_context << u256(0);
|
// need: size, offset, endowment
|
||||||
|
CompilerUtils(m_context).toSizeAfterFreeMemoryPointer();
|
||||||
if (function.valueSet())
|
if (function.valueSet())
|
||||||
m_context << eth::dupInstruction(3);
|
m_context << eth::dupInstruction(3);
|
||||||
else
|
else
|
||||||
@ -546,12 +554,16 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
|
|||||||
break;
|
break;
|
||||||
case Location::SHA3:
|
case Location::SHA3:
|
||||||
{
|
{
|
||||||
// we might compute a sha as part of argumentsAppendCopyToMemory, this is only a hack
|
TypePointers argumentTypes;
|
||||||
// and should be removed once we have a real free memory pointer
|
for (auto const& arg: arguments)
|
||||||
m_context << u256(0x40);
|
{
|
||||||
appendArgumentsCopyToMemory(arguments, TypePointers(), function.padArguments(), false, true);
|
arg->accept(*this);
|
||||||
m_context << u256(0x40) << eth::Instruction::SWAP1 << eth::Instruction::SUB;
|
argumentTypes.push_back(arg->getType());
|
||||||
m_context << u256(0x40) << eth::Instruction::SHA3;
|
}
|
||||||
|
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
||||||
|
encodeToMemory(argumentTypes, TypePointers(), function.padArguments(), true);
|
||||||
|
CompilerUtils(m_context).toSizeAfterFreeMemoryPointer();
|
||||||
|
m_context << eth::Instruction::SHA3;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Location::Log0:
|
case Location::Log0:
|
||||||
@ -566,9 +578,15 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
|
|||||||
arguments[arg]->accept(*this);
|
arguments[arg]->accept(*this);
|
||||||
appendTypeConversion(*arguments[arg]->getType(), *function.getParameterTypes()[arg], true);
|
appendTypeConversion(*arguments[arg]->getType(), *function.getParameterTypes()[arg], true);
|
||||||
}
|
}
|
||||||
m_context << u256(0);
|
arguments.front()->accept(*this);
|
||||||
appendExpressionCopyToMemory(*function.getParameterTypes().front(), *arguments.front());
|
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
||||||
m_context << u256(0) << eth::logInstruction(logNumber);
|
encodeToMemory(
|
||||||
|
{arguments.front()->getType()},
|
||||||
|
{function.getParameterTypes().front()},
|
||||||
|
false,
|
||||||
|
true);
|
||||||
|
CompilerUtils(m_context).toSizeAfterFreeMemoryPointer();
|
||||||
|
m_context << eth::logInstruction(logNumber);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Location::Event:
|
case Location::Event:
|
||||||
@ -582,8 +600,11 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
|
|||||||
{
|
{
|
||||||
++numIndexed;
|
++numIndexed;
|
||||||
arguments[arg - 1]->accept(*this);
|
arguments[arg - 1]->accept(*this);
|
||||||
appendTypeConversion(*arguments[arg - 1]->getType(),
|
appendTypeConversion(
|
||||||
*function.getParameterTypes()[arg - 1], true);
|
*arguments[arg - 1]->getType(),
|
||||||
|
*function.getParameterTypes()[arg - 1],
|
||||||
|
true
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if (!event.isAnonymous())
|
if (!event.isAnonymous())
|
||||||
{
|
{
|
||||||
@ -593,18 +614,20 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
|
|||||||
solAssert(numIndexed <= 4, "Too many indexed arguments.");
|
solAssert(numIndexed <= 4, "Too many indexed arguments.");
|
||||||
// Copy all non-indexed arguments to memory (data)
|
// Copy all non-indexed arguments to memory (data)
|
||||||
// Memory position is only a hack and should be removed once we have free memory pointer.
|
// Memory position is only a hack and should be removed once we have free memory pointer.
|
||||||
m_context << u256(0x40);
|
TypePointers nonIndexedArgTypes;
|
||||||
vector<ASTPointer<Expression const>> nonIndexedArgs;
|
TypePointers nonIndexedParamTypes;
|
||||||
TypePointers nonIndexedTypes;
|
|
||||||
for (unsigned arg = 0; arg < arguments.size(); ++arg)
|
for (unsigned arg = 0; arg < arguments.size(); ++arg)
|
||||||
if (!event.getParameters()[arg]->isIndexed())
|
if (!event.getParameters()[arg]->isIndexed())
|
||||||
{
|
{
|
||||||
nonIndexedArgs.push_back(arguments[arg]);
|
arguments[arg]->accept(*this);
|
||||||
nonIndexedTypes.push_back(function.getParameterTypes()[arg]);
|
nonIndexedArgTypes.push_back(arguments[arg]->getType());
|
||||||
|
nonIndexedParamTypes.push_back(function.getParameterTypes()[arg]);
|
||||||
}
|
}
|
||||||
appendArgumentsCopyToMemory(nonIndexedArgs, nonIndexedTypes);
|
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
||||||
m_context << u256(0x40) << eth::Instruction::SWAP1 << eth::Instruction::SUB;
|
encodeToMemory(nonIndexedArgTypes, nonIndexedParamTypes);
|
||||||
m_context << u256(0x40) << eth::logInstruction(numIndexed);
|
// need: topic1 ... topicn memsize memstart
|
||||||
|
CompilerUtils(m_context).toSizeAfterFreeMemoryPointer();
|
||||||
|
m_context << eth::logInstruction(numIndexed);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Location::BlockHash:
|
case Location::BlockHash:
|
||||||
@ -804,8 +827,10 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
|
|||||||
Type const& keyType = *dynamic_cast<MappingType const&>(baseType).getKeyType();
|
Type const& keyType = *dynamic_cast<MappingType const&>(baseType).getKeyType();
|
||||||
m_context << u256(0); // memory position
|
m_context << u256(0); // memory position
|
||||||
solAssert(_indexAccess.getIndexExpression(), "Index expression expected.");
|
solAssert(_indexAccess.getIndexExpression(), "Index expression expected.");
|
||||||
|
solAssert(keyType.getCalldataEncodedSize() <= 0x20, "Dynamic keys not yet implemented.");
|
||||||
appendExpressionCopyToMemory(keyType, *_indexAccess.getIndexExpression());
|
appendExpressionCopyToMemory(keyType, *_indexAccess.getIndexExpression());
|
||||||
m_context << eth::Instruction::SWAP1;
|
m_context << eth::Instruction::SWAP1;
|
||||||
|
solAssert(CompilerUtils::freeMemoryPointer >= 0x40, "");
|
||||||
appendTypeMoveToMemory(IntegerType(256));
|
appendTypeMoveToMemory(IntegerType(256));
|
||||||
m_context << u256(0) << eth::Instruction::SHA3;
|
m_context << u256(0) << eth::Instruction::SHA3;
|
||||||
m_context << u256(0);
|
m_context << u256(0);
|
||||||
@ -1058,50 +1083,78 @@ void ExpressionCompiler::appendExternalFunctionCall(
|
|||||||
unsigned gasStackPos = m_context.currentToBaseStackOffset(gasValueSize);
|
unsigned gasStackPos = m_context.currentToBaseStackOffset(gasValueSize);
|
||||||
unsigned valueStackPos = m_context.currentToBaseStackOffset(1);
|
unsigned valueStackPos = m_context.currentToBaseStackOffset(1);
|
||||||
|
|
||||||
bool returnSuccessCondition =
|
using FunctionKind = FunctionType::Location;
|
||||||
_functionType.getLocation() == FunctionType::Location::Bare ||
|
FunctionKind funKind = _functionType.getLocation();
|
||||||
_functionType.getLocation() == FunctionType::Location::BareCallCode;
|
bool returnSuccessCondition = funKind == FunctionKind::Bare || funKind == FunctionKind::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* firstReturnType =
|
||||||
_functionType.getReturnParameterTypes().front().get();
|
_functionType.getReturnParameterTypes().empty() ?
|
||||||
unsigned retSize = firstType ? firstType->getCalldataEncodedSize() : 0;
|
nullptr :
|
||||||
|
_functionType.getReturnParameterTypes().front().get();
|
||||||
|
unsigned retSize = firstReturnType ? firstReturnType->getCalldataEncodedSize() : 0;
|
||||||
if (returnSuccessCondition)
|
if (returnSuccessCondition)
|
||||||
retSize = 0; // return value actually is success condition
|
retSize = 0; // return value actually is success condition
|
||||||
// put on stack: <size of output> <memory pos of output>
|
|
||||||
m_context << u256(retSize);
|
|
||||||
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
|
||||||
|
|
||||||
//@TODO CHECK ALL CALLS OF appendTypeMoveToMemory
|
// Evaluate arguments.
|
||||||
|
TypePointers argumentTypes;
|
||||||
// copy arguments to memory and
|
bool manualFunctionId =
|
||||||
// put on stack: <size of input> <memory pos of input>
|
(funKind == FunctionKind::Bare || funKind == FunctionKind::BareCallCode) &&
|
||||||
m_context << eth::Instruction::DUP1 << eth::Instruction::DUP1;
|
!_arguments.empty() &&
|
||||||
if (!_functionType.isBareCall())
|
_arguments.front()->getType()->getRealType()->getCalldataEncodedSize(false) ==
|
||||||
|
CompilerUtils::dataStartOffset;
|
||||||
|
if (manualFunctionId)
|
||||||
{
|
{
|
||||||
// copy function identifier
|
// If we have a BareCall or BareCallCode and the first type has exactly 4 bytes, use it as
|
||||||
m_context << eth::dupInstruction(gasValueSize + 3);
|
// function identifier.
|
||||||
CompilerUtils(m_context).storeInMemoryDynamic(
|
_arguments.front()->accept(*this);
|
||||||
IntegerType(CompilerUtils::dataStartOffset * 8),
|
appendTypeConversion(
|
||||||
false
|
*_arguments.front()->getType(),
|
||||||
|
IntegerType(8 * CompilerUtils::dataStartOffset),
|
||||||
|
true
|
||||||
);
|
);
|
||||||
|
for (unsigned i = 0; i < gasValueSize; ++i)
|
||||||
|
m_context << eth::swapInstruction(gasValueSize - i);
|
||||||
|
gasStackPos++;
|
||||||
|
valueStackPos++;
|
||||||
|
}
|
||||||
|
for (size_t i = manualFunctionId ? 1 : 0; i < _arguments.size(); ++i)
|
||||||
|
{
|
||||||
|
_arguments[i]->accept(*this);
|
||||||
|
argumentTypes.push_back(_arguments[i]->getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
// For bare call, activate "4 byte pad exception": If the first argument has exactly 4 bytes,
|
// Copy function identifier to memory.
|
||||||
// do not pad it to 32 bytes.
|
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
||||||
|
if (!_functionType.isBareCall() || manualFunctionId)
|
||||||
|
{
|
||||||
|
m_context << eth::dupInstruction(2 + gasValueSize + CompilerUtils::getSizeOnStack(argumentTypes));
|
||||||
|
appendTypeMoveToMemory(IntegerType(8 * CompilerUtils::dataStartOffset), false);
|
||||||
|
}
|
||||||
// If the function takes arbitrary parameters, copy dynamic length data in place.
|
// If the function takes arbitrary parameters, copy dynamic length data in place.
|
||||||
appendArgumentsCopyToMemory(
|
// Move argumenst to memory, will not update the free memory pointer (but will update the memory
|
||||||
_arguments,
|
// pointer on the stack).
|
||||||
|
encodeToMemory(
|
||||||
|
argumentTypes,
|
||||||
_functionType.getParameterTypes(),
|
_functionType.getParameterTypes(),
|
||||||
_functionType.padArguments(),
|
_functionType.padArguments(),
|
||||||
_functionType.getLocation() == FunctionType::Location::Bare ||
|
|
||||||
_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;
|
// Stack now:
|
||||||
|
// <stack top>
|
||||||
|
// input_memory_end
|
||||||
|
// value [if _functionType.valueSet()]
|
||||||
|
// gas [if _functionType.gasSet()]
|
||||||
|
// function identifier [unless bare]
|
||||||
|
// contract address
|
||||||
|
|
||||||
|
// Output data will replace input data.
|
||||||
|
// put on stack: <size of output> <memory pos of output> <size of input> <memory pos of input>
|
||||||
|
m_context << u256(retSize);
|
||||||
|
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
||||||
|
m_context << eth::Instruction::DUP1 << eth::Instruction::DUP4 << eth::Instruction::SUB;
|
||||||
|
m_context << eth::Instruction::DUP2;
|
||||||
|
|
||||||
// CALL arguments: outSize, outOff, inSize, inOff (already present up to here)
|
// CALL arguments: outSize, outOff, inSize, inOff (already present up to here)
|
||||||
// value, addr, gas (stack top)
|
// value, addr, gas (stack top)
|
||||||
@ -1120,19 +1173,16 @@ void ExpressionCompiler::appendExternalFunctionCall(
|
|||||||
u256(eth::c_callGas + 10 + (_functionType.valueSet() ? eth::c_callValueTransferGas : 0) + eth::c_callNewAccountGas) <<
|
u256(eth::c_callGas + 10 + (_functionType.valueSet() ? eth::c_callValueTransferGas : 0) + eth::c_callNewAccountGas) <<
|
||||||
eth::Instruction::GAS <<
|
eth::Instruction::GAS <<
|
||||||
eth::Instruction::SUB;
|
eth::Instruction::SUB;
|
||||||
if (
|
if (funKind == FunctionKind::CallCode || funKind == FunctionKind::BareCallCode)
|
||||||
_functionType.getLocation() == FunctionType::Location::CallCode ||
|
|
||||||
_functionType.getLocation() == FunctionType::Location::BareCallCode
|
|
||||||
)
|
|
||||||
m_context << eth::Instruction::CALLCODE;
|
m_context << eth::Instruction::CALLCODE;
|
||||||
else
|
else
|
||||||
m_context << eth::Instruction::CALL;
|
m_context << eth::Instruction::CALL;
|
||||||
|
|
||||||
unsigned remainsSize =
|
unsigned remainsSize =
|
||||||
1 + // contract address
|
2 + // contract address, input_memory_end
|
||||||
_functionType.valueSet() +
|
_functionType.valueSet() +
|
||||||
_functionType.gasSet() +
|
_functionType.gasSet() +
|
||||||
!_functionType.isBareCall();
|
(!_functionType.isBareCall() || manualFunctionId);
|
||||||
|
|
||||||
if (returnSuccessCondition)
|
if (returnSuccessCondition)
|
||||||
m_context << eth::swapInstruction(remainsSize);
|
m_context << eth::swapInstruction(remainsSize);
|
||||||
@ -1149,56 +1199,93 @@ void ExpressionCompiler::appendExternalFunctionCall(
|
|||||||
{
|
{
|
||||||
// already there
|
// already there
|
||||||
}
|
}
|
||||||
else if (_functionType.getLocation() == FunctionType::Location::RIPEMD160)
|
else if (funKind == FunctionKind::RIPEMD160)
|
||||||
{
|
{
|
||||||
// fix: built-in contract returns right-aligned data
|
// fix: built-in contract returns right-aligned data
|
||||||
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
||||||
CompilerUtils(m_context).loadFromMemoryDynamic(IntegerType(160), false, true, false);
|
CompilerUtils(m_context).loadFromMemoryDynamic(IntegerType(160), false, true, false);
|
||||||
appendTypeConversion(IntegerType(160), FixedBytesType(20));
|
appendTypeConversion(IntegerType(160), FixedBytesType(20));
|
||||||
}
|
}
|
||||||
else if (firstType)
|
else if (firstReturnType)
|
||||||
{
|
{
|
||||||
|
//@todo manually update free memory pointer if we accept returning memory-stored objects
|
||||||
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
||||||
CompilerUtils(m_context).loadFromMemoryDynamic(*firstType, false, true, false);
|
CompilerUtils(m_context).loadFromMemoryDynamic(*firstReturnType, false, true, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExpressionCompiler::appendArgumentsCopyToMemory(
|
void ExpressionCompiler::encodeToMemory(
|
||||||
vector<ASTPointer<Expression const>> const& _arguments,
|
TypePointers const& _givenTypes,
|
||||||
TypePointers const& _types,
|
TypePointers const& _targetTypes,
|
||||||
bool _padToWordBoundaries,
|
bool _padToWordBoundaries,
|
||||||
bool _padExceptionIfFourBytes,
|
|
||||||
bool _copyDynamicDataInPlace
|
bool _copyDynamicDataInPlace
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
solAssert(_types.empty() || _types.size() == _arguments.size(), "");
|
// stack: <v1> <v2> ... <vn> <mem>
|
||||||
TypePointers types = _types;
|
TypePointers targetTypes = _targetTypes.empty() ? _givenTypes : _targetTypes;
|
||||||
if (_types.empty())
|
solAssert(targetTypes.size() == _givenTypes.size(), "");
|
||||||
for (ASTPointer<Expression const> const& argument: _arguments)
|
for (TypePointer& t: targetTypes)
|
||||||
types.push_back(argument->getType()->getRealType());
|
t = t->getRealType()->externalType();
|
||||||
|
|
||||||
vector<size_t> dynamicArguments;
|
// Stack during operation:
|
||||||
unsigned stackSizeOfDynamicTypes = 0;
|
// <v1> <v2> ... <vn> <mem_start> <dyn_head_1> ... <dyn_head_r> <end_of_mem>
|
||||||
for (size_t i = 0; i < _arguments.size(); ++i)
|
// 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
|
||||||
|
m_context << eth::Instruction::DUP1;
|
||||||
|
|
||||||
|
unsigned argSize = CompilerUtils::getSizeOnStack(_givenTypes);
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
_arguments[i]->accept(*this);
|
TypePointer targetType = targetTypes[i];
|
||||||
TypePointer argType = types[i]->externalType();
|
solAssert(!!targetType, "Externalable type expected.");
|
||||||
solAssert(!!argType, "Externalable type expected.");
|
if (targetType->isDynamicallySized() && !_copyDynamicDataInPlace)
|
||||||
if (argType->isValueType())
|
|
||||||
appendTypeConversion(*_arguments[i]->getType(), *argType, true);
|
|
||||||
else
|
|
||||||
argType = _arguments[i]->getType()->getRealType()->externalType();
|
|
||||||
solAssert(!!argType, "Externalable type expected.");
|
|
||||||
bool pad = _padToWordBoundaries;
|
|
||||||
// Do not pad if the first argument has exactly four bytes
|
|
||||||
if (i == 0 && pad && _padExceptionIfFourBytes && argType->getCalldataEncodedSize(false) == 4)
|
|
||||||
pad = false;
|
|
||||||
if (!_copyDynamicDataInPlace && argType->isDynamicallySized())
|
|
||||||
{
|
{
|
||||||
solAssert(argType->getCategory() == Type::Category::Array, "Unknown dynamic type.");
|
// leave end_of_mem as dyn head pointer
|
||||||
auto const& arrayType = dynamic_cast<ArrayType const&>(*_arguments[i]->getType());
|
m_context << eth::Instruction::DUP1 << u256(32) << eth::Instruction::ADD;
|
||||||
// move memory reference to top of stack
|
dynPointers++;
|
||||||
CompilerUtils(m_context).moveToStackTop(arrayType.getSizeOnStack());
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CompilerUtils(m_context).copyToStackTop(
|
||||||
|
argSize - stackPos + dynPointers + 2,
|
||||||
|
_givenTypes[i]->getSizeOnStack()
|
||||||
|
);
|
||||||
|
if (targetType->isValueType())
|
||||||
|
appendTypeConversion(*_givenTypes[i], *targetType, true);
|
||||||
|
solAssert(!!targetType, "Externalable type expected.");
|
||||||
|
appendTypeMoveToMemory(*targetType, _padToWordBoundaries);
|
||||||
|
}
|
||||||
|
stackPos += _givenTypes[i]->getSizeOnStack();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
{
|
||||||
|
solAssert(_givenTypes[i]->getCategory() == Type::Category::Array, "Unknown dynamic type.");
|
||||||
|
auto const& arrayType = dynamic_cast<ArrayType const&>(*_givenTypes[i]);
|
||||||
|
// copy tail pointer (=mem_end - mem_start) to memory
|
||||||
|
m_context << eth::dupInstruction(2 + dynPointers) << eth::Instruction::DUP2;
|
||||||
|
m_context << eth::Instruction::SUB;
|
||||||
|
m_context << eth::dupInstruction(2 + dynPointers - thisDynPointer);
|
||||||
|
m_context << eth::Instruction::MSTORE;
|
||||||
|
// now copy the array
|
||||||
|
CompilerUtils(m_context).copyToStackTop(
|
||||||
|
argSize - stackPos + dynPointers + 2,
|
||||||
|
arrayType.getSizeOnStack()
|
||||||
|
);
|
||||||
|
// copy length to memory
|
||||||
|
m_context << eth::dupInstruction(1 + arrayType.getSizeOnStack());
|
||||||
if (arrayType.location() == ReferenceType::Location::CallData)
|
if (arrayType.location() == ReferenceType::Location::CallData)
|
||||||
m_context << eth::Instruction::DUP2; // length is on stack
|
m_context << eth::Instruction::DUP2; // length is on stack
|
||||||
else if (arrayType.location() == ReferenceType::Location::Storage)
|
else if (arrayType.location() == ReferenceType::Location::Storage)
|
||||||
@ -1209,31 +1296,19 @@ void ExpressionCompiler::appendArgumentsCopyToMemory(
|
|||||||
m_context << eth::Instruction::DUP2 << eth::Instruction::MLOAD;
|
m_context << eth::Instruction::DUP2 << eth::Instruction::MLOAD;
|
||||||
}
|
}
|
||||||
appendTypeMoveToMemory(IntegerType(256), true);
|
appendTypeMoveToMemory(IntegerType(256), true);
|
||||||
stackSizeOfDynamicTypes += arrayType.getSizeOnStack();
|
// copy the new memory pointer
|
||||||
dynamicArguments.push_back(i);
|
m_context << eth::swapInstruction(arrayType.getSizeOnStack() + 1) << eth::Instruction::POP;
|
||||||
|
// copy data part
|
||||||
|
appendTypeMoveToMemory(arrayType, true);
|
||||||
|
|
||||||
|
thisDynPointer++;
|
||||||
}
|
}
|
||||||
else
|
stackPos += _givenTypes[i]->getSizeOnStack();
|
||||||
appendTypeMoveToMemory(*argType, pad);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy dynamic values to memory
|
// remove unneeded stack elements (and retain memory pointer)
|
||||||
unsigned dynStackPointer = stackSizeOfDynamicTypes;
|
m_context << eth::swapInstruction(argSize + dynPointers + 1);
|
||||||
// stack layout: <dyn arg 1> ... <dyn arg m> <memory pointer>
|
CompilerUtils(m_context).popStackSlots(argSize + dynPointers + 1);
|
||||||
for (size_t i: dynamicArguments)
|
|
||||||
{
|
|
||||||
auto const& arrayType = dynamic_cast<ArrayType const&>(*_arguments[i]->getType());
|
|
||||||
CompilerUtils(m_context).copyToStackTop(1 + dynStackPointer, arrayType.getSizeOnStack());
|
|
||||||
dynStackPointer -= arrayType.getSizeOnStack();
|
|
||||||
appendTypeMoveToMemory(arrayType, true);
|
|
||||||
}
|
|
||||||
solAssert(dynStackPointer == 0, "");
|
|
||||||
|
|
||||||
// remove dynamic values (and retain memory pointer)
|
|
||||||
if (stackSizeOfDynamicTypes > 0)
|
|
||||||
{
|
|
||||||
m_context << eth::swapInstruction(stackSizeOfDynamicTypes);
|
|
||||||
CompilerUtils(m_context).popStackSlots(stackSizeOfDynamicTypes);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExpressionCompiler::appendTypeMoveToMemory(Type const& _type, bool _padToWordBoundaries)
|
void ExpressionCompiler::appendTypeMoveToMemory(Type const& _type, bool _padToWordBoundaries)
|
||||||
|
@ -98,21 +98,28 @@ private:
|
|||||||
void appendHighBitsCleanup(IntegerType const& _typeOnStack);
|
void appendHighBitsCleanup(IntegerType const& _typeOnStack);
|
||||||
|
|
||||||
/// Appends code to call a function of the given type with the given arguments.
|
/// Appends code to call a function of the given type with the given arguments.
|
||||||
void appendExternalFunctionCall(FunctionType const& _functionType, std::vector<ASTPointer<Expression const>> const& _arguments);
|
void appendExternalFunctionCall(
|
||||||
/// Appends code that evaluates the given arguments and moves the result to memory encoded as
|
FunctionType const& _functionType,
|
||||||
/// specified by the ABI. The memory offset is expected to be on the stack and is updated by
|
std::vector<ASTPointer<Expression const>> const& _arguments
|
||||||
/// this call. If @a _padToWordBoundaries is set to false, all values are concatenated without
|
);
|
||||||
/// padding. If @a _copyDynamicDataInPlace is set, dynamic types is stored (without length)
|
/// Copies values (of types @a _givenTypes) given on the stack to a location in memory given
|
||||||
|
/// at the stack top, encoding them according to the ABI as the given types @a _targetTypes.
|
||||||
|
/// Removes the values from the stack and leaves the updated memory pointer.
|
||||||
|
/// Stack pre: <v1> <v2> ... <vn> <memptr>
|
||||||
|
/// Stack post: <memptr_updated>
|
||||||
|
/// Does not touch the memory-free pointer.
|
||||||
|
/// @param _padToWordBoundaries if false, all values are concatenated without padding.
|
||||||
|
/// @param _copyDynamicDataInPlace if true, dynamic types is stored (without length)
|
||||||
/// together with fixed-length data.
|
/// together with fixed-length data.
|
||||||
void appendArgumentsCopyToMemory(
|
void encodeToMemory(
|
||||||
std::vector<ASTPointer<Expression const>> const& _arguments,
|
TypePointers const& _givenTypes = {},
|
||||||
TypePointers const& _types = {},
|
TypePointers const& _targetTypes = {},
|
||||||
bool _padToWordBoundaries = true,
|
bool _padToWordBoundaries = true,
|
||||||
bool _padExceptionIfFourBytes = false,
|
|
||||||
bool _copyDynamicDataInPlace = false
|
bool _copyDynamicDataInPlace = false
|
||||||
);
|
);
|
||||||
/// Appends code that moves a stack element of the given type to memory. The memory offset is
|
/// Appends code that moves a stack element of the given type to memory. The memory offset is
|
||||||
/// expected below the stack element and is updated by this call.
|
/// expected below the stack element and is updated by this call.
|
||||||
|
/// For arrays, this only copies the data part.
|
||||||
void appendTypeMoveToMemory(Type const& _type, bool _padToWordBoundaries = true);
|
void appendTypeMoveToMemory(Type const& _type, bool _padToWordBoundaries = true);
|
||||||
/// Appends code that evaluates a single expression and moves the result to memory. The memory offset is
|
/// Appends code that evaluates a single expression and moves the result to memory. The memory offset is
|
||||||
/// expected to be on the stack and is updated by this call.
|
/// expected to be on the stack and is updated by this call.
|
||||||
|
Loading…
Reference in New Issue
Block a user