aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Beregszaszi <alex@rtfs.hu>2017-09-27 20:04:37 +0800
committerGitHub <noreply@github.com>2017-09-27 20:04:37 +0800
commit3f783c8dad7a143d364f75ddac839aac2eeece1a (patch)
tree9df24003560a05a73f1cdb1f2c6a2c28105fb669
parenta27b23bacb60a6902547fb99b9f7a2df8bb014cd (diff)
parent204214f0700179e3d8fa97c77d4f92acd349f015 (diff)
downloaddexon-solidity-3f783c8dad7a143d364f75ddac839aac2eeece1a.tar
dexon-solidity-3f783c8dad7a143d364f75ddac839aac2eeece1a.tar.gz
dexon-solidity-3f783c8dad7a143d364f75ddac839aac2eeece1a.tar.bz2
dexon-solidity-3f783c8dad7a143d364f75ddac839aac2eeece1a.tar.lz
dexon-solidity-3f783c8dad7a143d364f75ddac839aac2eeece1a.tar.xz
dexon-solidity-3f783c8dad7a143d364f75ddac839aac2eeece1a.tar.zst
dexon-solidity-3f783c8dad7a143d364f75ddac839aac2eeece1a.zip
Merge pull request #2975 from ethereum/encode-memory
Split encodeToMemory into packedEncode and abiEncode
-rw-r--r--libsolidity/codegen/CompilerUtils.cpp4
-rw-r--r--libsolidity/codegen/CompilerUtils.h35
-rw-r--r--libsolidity/codegen/ContractCompiler.cpp2
-rw-r--r--libsolidity/codegen/ExpressionCompiler.cpp26
4 files changed, 45 insertions, 22 deletions
diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp
index ce97ed61..f9b181ae 100644
--- a/libsolidity/codegen/CompilerUtils.cpp
+++ b/libsolidity/codegen/CompilerUtils.cpp
@@ -191,7 +191,7 @@ void CompilerUtils::encodeToMemory(
{
// Use the new JULIA-based encoding function
auto stackHeightBefore = m_context.stackHeight();
- abiEncode(_givenTypes, targetTypes, _encodeAsLibraryTypes);
+ abiEncodeV2(_givenTypes, targetTypes, _encodeAsLibraryTypes);
solAssert(stackHeightBefore - m_context.stackHeight() == sizeOnStack(_givenTypes), "");
return;
}
@@ -302,7 +302,7 @@ void CompilerUtils::encodeToMemory(
popStackSlots(argSize + dynPointers + 1);
}
-void CompilerUtils::abiEncode(
+void CompilerUtils::abiEncodeV2(
TypePointers const& _givenTypes,
TypePointers const& _targetTypes,
bool _encodeAsLibraryTypes
diff --git a/libsolidity/codegen/CompilerUtils.h b/libsolidity/codegen/CompilerUtils.h
index 5e45699b..ad3989ad 100644
--- a/libsolidity/codegen/CompilerUtils.h
+++ b/libsolidity/codegen/CompilerUtils.h
@@ -102,13 +102,26 @@ public:
/// @note the locations of target reference types are ignored, because it will always be
/// memory.
void encodeToMemory(
- TypePointers const& _givenTypes = {},
- TypePointers const& _targetTypes = {},
- bool _padToWords = true,
- bool _copyDynamicDataInPlace = false,
+ TypePointers const& _givenTypes,
+ TypePointers const& _targetTypes,
+ bool _padToWords,
+ bool _copyDynamicDataInPlace,
bool _encodeAsLibraryTypes = false
);
+ /// Special case of @a encodeToMemory which assumes tight packing, e.g. no zero padding
+ /// and dynamic data is encoded in-place.
+ /// Stack pre: <value0> <value1> ... <valueN-1> <head_start>
+ /// Stack post: <mem_ptr>
+ void packedEncode(
+ TypePointers const& _givenTypes,
+ TypePointers const& _targetTypes,
+ bool _encodeAsLibraryTypes = false
+ )
+ {
+ encodeToMemory(_givenTypes, _targetTypes, false, true, _encodeAsLibraryTypes);
+ }
+
/// Special case of @a encodeToMemory which assumes that everything is padded to words
/// and dynamic data is not copied in place (i.e. a proper ABI encoding).
/// Stack pre: <value0> <value1> ... <valueN-1> <head_start>
@@ -117,6 +130,20 @@ public:
TypePointers const& _givenTypes,
TypePointers const& _targetTypes,
bool _encodeAsLibraryTypes = false
+ )
+ {
+ encodeToMemory(_givenTypes, _targetTypes, true, false, _encodeAsLibraryTypes);
+ }
+
+ /// Special case of @a encodeToMemory which assumes that everything is padded to words
+ /// and dynamic data is not copied in place (i.e. a proper ABI encoding).
+ /// Uses a new, less tested encoder implementation.
+ /// Stack pre: <value0> <value1> ... <valueN-1> <head_start>
+ /// Stack post: <mem_ptr>
+ void abiEncodeV2(
+ TypePointers const& _givenTypes,
+ TypePointers const& _targetTypes,
+ bool _encodeAsLibraryTypes = false
);
/// Zero-initialises (the data part of) an already allocated memory array.
diff --git a/libsolidity/codegen/ContractCompiler.cpp b/libsolidity/codegen/ContractCompiler.cpp
index 92782b8d..429db532 100644
--- a/libsolidity/codegen/ContractCompiler.cpp
+++ b/libsolidity/codegen/ContractCompiler.cpp
@@ -421,7 +421,7 @@ void ContractCompiler::appendReturnValuePacker(TypePointers const& _typeParamete
utils.fetchFreeMemoryPointer();
//@todo optimization: if we return a single memory array, there should be enough space before
// its data to add the needed parts and we avoid a memory copy.
- utils.encodeToMemory(_typeParameters, _typeParameters, true, false, _isLibrary);
+ utils.abiEncode(_typeParameters, _typeParameters, _isLibrary);
utils.toSizeAfterFreeMemoryPointer();
m_context << Instruction::RETURN;
}
diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp
index 58c64bce..c2bf0f5c 100644
--- a/libsolidity/codegen/ExpressionCompiler.cpp
+++ b/libsolidity/codegen/ExpressionCompiler.cpp
@@ -581,7 +581,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
_context << Instruction::ADD;
}
);
- utils().encodeToMemory(argumentTypes, function.parameterTypes());
+ utils().abiEncode(argumentTypes, function.parameterTypes());
// now on stack: memory_end_ptr
// need: size, offset, endowment
utils().toSizeAfterFreeMemoryPointer();
@@ -675,7 +675,8 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
argumentTypes.push_back(arg->annotation().type);
}
utils().fetchFreeMemoryPointer();
- utils().encodeToMemory(argumentTypes, TypePointers(), function.padArguments(), true);
+ solAssert(!function.padArguments(), "");
+ utils().packedEncode(argumentTypes, TypePointers());
utils().toSizeAfterFreeMemoryPointer();
m_context << Instruction::KECCAK256;
break;
@@ -694,11 +695,10 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
}
arguments.front()->accept(*this);
utils().fetchFreeMemoryPointer();
- utils().encodeToMemory(
+ utils().packedEncode(
{arguments.front()->annotation().type},
- {function.parameterTypes().front()},
- false,
- true);
+ {function.parameterTypes().front()}
+ );
utils().toSizeAfterFreeMemoryPointer();
m_context << logInstruction(logNumber);
break;
@@ -717,11 +717,9 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
if (auto const& arrayType = dynamic_pointer_cast<ArrayType const>(function.parameterTypes()[arg - 1]))
{
utils().fetchFreeMemoryPointer();
- utils().encodeToMemory(
+ utils().packedEncode(
{arguments[arg - 1]->annotation().type},
- {arrayType},
- false,
- true
+ {arrayType}
);
utils().toSizeAfterFreeMemoryPointer();
m_context << Instruction::KECCAK256;
@@ -751,7 +749,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
nonIndexedParamTypes.push_back(function.parameterTypes()[arg]);
}
utils().fetchFreeMemoryPointer();
- utils().encodeToMemory(nonIndexedArgTypes, nonIndexedParamTypes);
+ utils().abiEncode(nonIndexedArgTypes, nonIndexedParamTypes);
// need: topic1 ... topicn memsize memstart
utils().toSizeAfterFreeMemoryPointer();
m_context << logInstruction(numIndexed);
@@ -1212,11 +1210,9 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
utils().fetchFreeMemoryPointer();
// stack: base index mem
// note: the following operations must not allocate memory!
- utils().encodeToMemory(
+ utils().packedEncode(
TypePointers{_indexAccess.indexExpression()->annotation().type},
- TypePointers{keyType},
- false,
- true
+ TypePointers{keyType}
);
m_context << Instruction::SWAP1;
utils().storeInMemoryDynamic(IntegerType(256));