mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
@@ -29,15 +29,19 @@ namespace solidity
|
||||
|
||||
enum class ExperimentalFeature
|
||||
{
|
||||
ABIEncoderV2, // new ABI encoder that makes use of JULIA
|
||||
Test,
|
||||
TestOnlyAnalysis
|
||||
};
|
||||
|
||||
static const std::map<ExperimentalFeature, bool> ExperimentalFeatureOnlyAnalysis = {
|
||||
static const std::map<ExperimentalFeature, bool> ExperimentalFeatureOnlyAnalysis =
|
||||
{
|
||||
{ ExperimentalFeature::TestOnlyAnalysis, true },
|
||||
};
|
||||
|
||||
static const std::map<std::string, ExperimentalFeature> ExperimentalFeatureNames = {
|
||||
static const std::map<std::string, ExperimentalFeature> ExperimentalFeatureNames =
|
||||
{
|
||||
{ "ABIEncoderV2", ExperimentalFeature::ABIEncoderV2 },
|
||||
{ "__test", ExperimentalFeature::Test },
|
||||
{ "__testOnlyAnalysis", ExperimentalFeature::TestOnlyAnalysis },
|
||||
};
|
||||
|
||||
@@ -1529,8 +1529,6 @@ TypePointer ArrayType::interfaceType(bool _inLibrary) const
|
||||
TypePointer baseExt = m_baseType->interfaceType(_inLibrary);
|
||||
if (!baseExt)
|
||||
return TypePointer();
|
||||
if (m_baseType->category() == Category::Array && m_baseType->isDynamicallySized())
|
||||
return TypePointer();
|
||||
|
||||
if (isDynamicallySized())
|
||||
return make_shared<ArrayType>(DataLocation::Memory, baseExt);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* @author Christian <chris@ethereum.org>
|
||||
* @date 2017
|
||||
* Routines that generate JULIA code related to ABI encoding, decoding and type conversions.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libsolidity/ast/ASTForward.h>
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
|
||||
namespace dev {
|
||||
namespace solidity {
|
||||
|
||||
class Type;
|
||||
class ArrayType;
|
||||
class StructType;
|
||||
class FunctionType;
|
||||
using TypePointer = std::shared_ptr<Type const>;
|
||||
using TypePointers = std::vector<TypePointer>;
|
||||
|
||||
///
|
||||
/// Class to generate encoding and decoding functions. Also maintains a collection
|
||||
/// of "functions to be generated" in order to avoid generating the same function
|
||||
/// multiple times.
|
||||
///
|
||||
/// Make sure to include the result of ``requestedFunctions()`` to a block that
|
||||
/// is visible from the code that was generated here.
|
||||
class ABIFunctions
|
||||
{
|
||||
public:
|
||||
~ABIFunctions();
|
||||
|
||||
/// @returns assembly code block to ABI-encode values of @a _givenTypes residing on the stack
|
||||
/// into memory, converting the types to @a _targetTypes on the fly.
|
||||
/// Assumed variables to be present: <$value0> <$value1> ... <$value(n-1)> <$headStart>
|
||||
/// Does not allocate memory (does not change the memory head pointer), but writes
|
||||
/// to memory starting at $headStart and an unrestricted amount after that.
|
||||
/// Assigns the end of encoded memory either to $value0 or (if that is not present)
|
||||
/// to $headStart.
|
||||
std::string tupleEncoder(
|
||||
TypePointers const& _givenTypes,
|
||||
TypePointers const& _targetTypes,
|
||||
bool _encodeAsLibraryTypes = false
|
||||
);
|
||||
|
||||
/// @returns auxiliary functions referenced from the block generated in @a tupleEncoder
|
||||
std::string requestedFunctions();
|
||||
|
||||
private:
|
||||
/// @returns the name of the cleanup function for the given type and
|
||||
/// adds its implementation to the requested functions.
|
||||
/// @param _revertOnFailure if true, causes revert on invalid data,
|
||||
/// otherwise an assertion failure.
|
||||
std::string cleanupFunction(Type const& _type, bool _revertOnFailure = false);
|
||||
|
||||
/// @returns the name of the function that converts a value of type @a _from
|
||||
/// to a value of type @a _to. The resulting vale is guaranteed to be in range
|
||||
/// (i.e. "clean"). Asserts on failure.
|
||||
std::string conversionFunction(Type const& _from, Type const& _to);
|
||||
|
||||
std::string cleanupCombinedExternalFunctionIdFunction();
|
||||
|
||||
/// @returns a function that combines the address and selector to a single value
|
||||
/// for use in the ABI.
|
||||
std::string combineExternalFunctionIdFunction();
|
||||
|
||||
/// @returns the name of the ABI encoding function with the given type
|
||||
/// and queues the generation of the function to the requested functions.
|
||||
/// @param _compacted if true, the input value was just loaded from storage
|
||||
/// or memory and thus might be compacted into a single slot (depending on the type).
|
||||
std::string abiEncodingFunction(
|
||||
Type const& _givenType,
|
||||
Type const& _targetType,
|
||||
bool _encodeAsLibraryTypes,
|
||||
bool _compacted
|
||||
);
|
||||
/// Part of @a abiEncodingFunction for array target type and given calldata array.
|
||||
std::string abiEncodingFunctionCalldataArray(
|
||||
Type const& _givenType,
|
||||
Type const& _targetType,
|
||||
bool _encodeAsLibraryTypes
|
||||
);
|
||||
/// Part of @a abiEncodingFunction for array target type and given memory array or
|
||||
/// a given storage array with one item per slot.
|
||||
std::string abiEncodingFunctionSimpleArray(
|
||||
ArrayType const& _givenType,
|
||||
ArrayType const& _targetType,
|
||||
bool _encodeAsLibraryTypes
|
||||
);
|
||||
std::string abiEncodingFunctionMemoryByteArray(
|
||||
ArrayType const& _givenType,
|
||||
ArrayType const& _targetType,
|
||||
bool _encodeAsLibraryTypes
|
||||
);
|
||||
/// Part of @a abiEncodingFunction for array target type and given storage array
|
||||
/// where multiple items are packed into the same storage slot.
|
||||
std::string abiEncodingFunctionCompactStorageArray(
|
||||
ArrayType const& _givenType,
|
||||
ArrayType const& _targetType,
|
||||
bool _encodeAsLibraryTypes
|
||||
);
|
||||
|
||||
// @returns the name of the ABI encoding function with the given type
|
||||
// and queues the generation of the function to the requested functions.
|
||||
// Case for _givenType being a string literal
|
||||
std::string abiEncodingFunctionStringLiteral(
|
||||
Type const& _givenType,
|
||||
Type const& _targetType,
|
||||
bool _encodeAsLibraryTypes
|
||||
);
|
||||
|
||||
std::string abiEncodingFunctionFunctionType(
|
||||
FunctionType const& _from,
|
||||
Type const& _to,
|
||||
bool _encodeAsLibraryTypes,
|
||||
bool _compacted
|
||||
);
|
||||
|
||||
/// @returns a function that copies raw bytes of dynamic length from calldata
|
||||
/// or memory to memory.
|
||||
/// Pads with zeros and might write more than exactly length.
|
||||
std::string copyToMemoryFunction(bool _fromCalldata);
|
||||
|
||||
std::string shiftLeftFunction(size_t _numBits);
|
||||
std::string shiftRightFunction(size_t _numBits, bool _signed);
|
||||
/// @returns the name of a function that rounds its input to the next multiple
|
||||
/// of 32 or the input if it is a multiple of 32.
|
||||
std::string roundUpFunction();
|
||||
|
||||
std::string arrayLengthFunction(ArrayType const& _type);
|
||||
/// @returns the name of a function that converts a storage slot number
|
||||
/// or a memory pointer to the slot number / memory pointer for the data position of an array
|
||||
/// which is stored in that slot / memory area.
|
||||
std::string arrayDataAreaFunction(ArrayType const& _type);
|
||||
/// @returns the name of a function that advances an array data pointer to the next element.
|
||||
/// Only works for memory arrays and storage arrays that store one item per slot.
|
||||
std::string nextArrayElementFunction(ArrayType const& _type);
|
||||
|
||||
/// Helper function that uses @a _creator to create a function and add it to
|
||||
/// @a m_requestedFunctions if it has not been created yet and returns @a _name in both
|
||||
/// cases.
|
||||
std::string createFunction(std::string const& _name, std::function<std::string()> const& _creator);
|
||||
|
||||
/// @returns the size of the static part of the encoding of the given types.
|
||||
size_t headSize(TypePointers const& _targetTypes);
|
||||
|
||||
/// Map from function name to code for a multi-use function.
|
||||
std::map<std::string, std::string> m_requestedFunctions;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
@@ -44,11 +44,6 @@ namespace dev
|
||||
namespace solidity
|
||||
{
|
||||
|
||||
void CompilerContext::addMagicGlobal(MagicVariableDeclaration const& _declaration)
|
||||
{
|
||||
m_magicGlobals.insert(&_declaration);
|
||||
}
|
||||
|
||||
void CompilerContext::addStateVariable(
|
||||
VariableDeclaration const& _declaration,
|
||||
u256 const& _storageOffset,
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace solidity {
|
||||
class CompilerContext
|
||||
{
|
||||
public:
|
||||
CompilerContext(CompilerContext* _runtimeContext = nullptr):
|
||||
explicit CompilerContext(CompilerContext* _runtimeContext = nullptr):
|
||||
m_asm(std::make_shared<eth::Assembly>()),
|
||||
m_runtimeContext(_runtimeContext)
|
||||
{
|
||||
@@ -56,7 +56,9 @@ public:
|
||||
m_runtimeSub = size_t(m_asm->newSub(m_runtimeContext->m_asm).data());
|
||||
}
|
||||
|
||||
void addMagicGlobal(MagicVariableDeclaration const& _declaration);
|
||||
void setExperimentalFeatures(std::set<ExperimentalFeature> const& _features) { m_experimentalFeatures = _features; }
|
||||
bool experimentalFeatureActive(ExperimentalFeature _feature) const { return m_experimentalFeatures.count(_feature); }
|
||||
|
||||
void addStateVariable(VariableDeclaration const& _declaration, u256 const& _storageOffset, unsigned _byteOffset);
|
||||
void addVariable(VariableDeclaration const& _declaration, unsigned _offsetToCurrent = 0);
|
||||
void removeVariable(VariableDeclaration const& _declaration);
|
||||
@@ -68,7 +70,6 @@ public:
|
||||
void adjustStackOffset(int _adjustment) { m_asm->adjustDeposit(_adjustment); }
|
||||
unsigned stackHeight() const { solAssert(m_asm->deposit() >= 0, ""); return unsigned(m_asm->deposit()); }
|
||||
|
||||
bool isMagicGlobal(Declaration const* _declaration) const { return m_magicGlobals.count(_declaration) != 0; }
|
||||
bool isLocalVariable(Declaration const* _declaration) const;
|
||||
bool isStateVariable(Declaration const* _declaration) const { return m_stateVariables.count(_declaration) != 0; }
|
||||
|
||||
@@ -265,8 +266,8 @@ private:
|
||||
} m_functionCompilationQueue;
|
||||
|
||||
eth::AssemblyPointer m_asm;
|
||||
/// Magic global variables like msg, tx or this, distinguished by type.
|
||||
std::set<Declaration const*> m_magicGlobals;
|
||||
/// Activated experimental features.
|
||||
std::set<ExperimentalFeature> m_experimentalFeatures;
|
||||
/// Other already compiled contracts to be used in contract creation calls.
|
||||
std::map<ContractDefinition const*, eth::Assembly const*> m_compiledContracts;
|
||||
/// Storage offsets of state variables
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <libevmasm/Instruction.h>
|
||||
#include <libsolidity/codegen/ArrayUtils.h>
|
||||
#include <libsolidity/codegen/LValue.h>
|
||||
#include <libsolidity/codegen/ABIFunctions.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -182,6 +183,18 @@ void CompilerUtils::encodeToMemory(
|
||||
|
||||
if (_givenTypes.empty())
|
||||
return;
|
||||
else if (
|
||||
_padToWordBoundaries &&
|
||||
!_copyDynamicDataInPlace &&
|
||||
m_context.experimentalFeatureActive(ExperimentalFeature::ABIEncoderV2)
|
||||
)
|
||||
{
|
||||
// Use the new JULIA-based encoding function
|
||||
auto stackHeightBefore = m_context.stackHeight();
|
||||
abiEncode(_givenTypes, targetTypes, _encodeAsLibraryTypes);
|
||||
solAssert(stackHeightBefore - m_context.stackHeight() == sizeOnStack(_givenTypes), "");
|
||||
return;
|
||||
}
|
||||
|
||||
// Stack during operation:
|
||||
// <v1> <v2> ... <vn> <mem_start> <dyn_head_1> ... <dyn_head_r> <end_of_mem>
|
||||
@@ -289,6 +302,28 @@ void CompilerUtils::encodeToMemory(
|
||||
popStackSlots(argSize + dynPointers + 1);
|
||||
}
|
||||
|
||||
void CompilerUtils::abiEncode(
|
||||
TypePointers const& _givenTypes,
|
||||
TypePointers const& _targetTypes,
|
||||
bool _encodeAsLibraryTypes
|
||||
)
|
||||
{
|
||||
// stack: <$value0> <$value1> ... <$value(n-1)> <$headStart>
|
||||
|
||||
vector<string> variables;
|
||||
size_t numValues = sizeOnStack(_givenTypes);
|
||||
for (size_t i = 0; i < numValues; ++i)
|
||||
variables.push_back("$value" + to_string(i));
|
||||
variables.push_back("$headStart");
|
||||
|
||||
ABIFunctions funs;
|
||||
string routine = funs.tupleEncoder(_givenTypes, _targetTypes, _encodeAsLibraryTypes);
|
||||
routine += funs.requestedFunctions();
|
||||
m_context.appendInlineAssembly("{" + routine + "}", variables);
|
||||
// Remove everyhing except for "value0" / the final memory pointer.
|
||||
popStackSlots(numValues);
|
||||
}
|
||||
|
||||
void CompilerUtils::zeroInitialiseMemoryArray(ArrayType const& _type)
|
||||
{
|
||||
auto repeat = m_context.newTag();
|
||||
|
||||
@@ -103,6 +103,14 @@ public:
|
||||
bool _encodeAsLibraryTypes = false
|
||||
);
|
||||
|
||||
/// 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).
|
||||
void abiEncode(
|
||||
TypePointers const& _givenTypes,
|
||||
TypePointers const& _targetTypes,
|
||||
bool _encodeAsLibraryTypes = false
|
||||
);
|
||||
|
||||
/// Zero-initialises (the data part of) an already allocated memory array.
|
||||
/// Length has to be nonzero!
|
||||
/// Stack pre: <length> <memptr>
|
||||
|
||||
@@ -100,6 +100,7 @@ void ContractCompiler::initializeContext(
|
||||
map<ContractDefinition const*, eth::Assembly const*> const& _compiledContracts
|
||||
)
|
||||
{
|
||||
m_context.setExperimentalFeatures(_contract.sourceUnit().annotation().experimentalFeatures);
|
||||
m_context.setCompiledContracts(_compiledContracts);
|
||||
m_context.setInheritanceHierarchy(_contract.annotation().linearizedBaseContracts);
|
||||
CompilerUtils(m_context).initialiseFreeMemoryPointer();
|
||||
|
||||
Reference in New Issue
Block a user