aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Compiler.cpp10
-rw-r--r--CompilerUtils.h3
-rw-r--r--ExpressionCompiler.cpp17
-rw-r--r--Types.cpp9
-rw-r--r--Types.h2
5 files changed, 23 insertions, 18 deletions
diff --git a/Compiler.cpp b/Compiler.cpp
index 514e1a27..d49807be 100644
--- a/Compiler.cpp
+++ b/Compiler.cpp
@@ -100,7 +100,7 @@ void Compiler::appendConstructorCall(FunctionDefinition const& _constructor)
{
m_context << u256(argumentSize);
m_context.appendProgramSize();
- m_context << u256(1); // copy it to byte one as expected for ABI calls
+ m_context << u256(g_functionIdentifierOffset); // copy it to byte four as expected for ABI calls
m_context << eth::Instruction::CODECOPY;
appendCalldataUnpacker(_constructor, true);
}
@@ -125,12 +125,12 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("More than 4294967295 public functions for contract."));
// retrieve the function signature hash from the calldata
- m_context << u256(1) << u256(0) << u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296)// some constants
+ m_context << u256(1) << u256(0) << (u256(1) << 224) // some constants
<< eth::dupInstruction(2) << eth::Instruction::CALLDATALOAD
<< eth::Instruction::DIV;
// stack now is: 1 0 <funhash>
- for (auto it = interfaceFunctions.begin(); it != interfaceFunctions.end(); ++it)
+ for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it)
{
callDataUnpackerEntryPoints.push_back(m_context.newTag());
m_context << eth::dupInstruction(1) << u256(FixedHash<4>::Arith(it->first)) << eth::Instruction::EQ;
@@ -139,7 +139,7 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract)
m_context << eth::Instruction::STOP; // function not found
unsigned funid = 0;
- for (auto it = interfaceFunctions.begin(); it != interfaceFunctions.end(); ++it, ++funid)
+ for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it, ++funid)
{
FunctionDefinition const& function = *it->second;
m_context << callDataUnpackerEntryPoints[funid];
@@ -154,7 +154,7 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract)
unsigned Compiler::appendCalldataUnpacker(FunctionDefinition const& _function, bool _fromMemory)
{
// We do not check the calldata size, everything is zero-padded.
- unsigned dataOffset = 1;
+ unsigned dataOffset = g_functionIdentifierOffset; // the 4 bytes of the function hash signature
//@todo this can be done more efficiently, saving some CALLDATALOAD calls
for (ASTPointer<VariableDeclaration> const& var: _function.getParameters())
{
diff --git a/CompilerUtils.h b/CompilerUtils.h
index 928f0e2d..bffd6f49 100644
--- a/CompilerUtils.h
+++ b/CompilerUtils.h
@@ -30,6 +30,9 @@ namespace solidity {
class Type; // forward
+/// The size in bytes of the function (hash) identifier
+static const unsigned int g_functionIdentifierOffset = 4;
+
class CompilerUtils
{
public:
diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp
index 6bf14f55..5fefd528 100644
--- a/ExpressionCompiler.cpp
+++ b/ExpressionCompiler.cpp
@@ -335,7 +335,7 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
case Type::Category::CONTRACT:
{
ContractType const& type = dynamic_cast<ContractType const&>(*_memberAccess.getExpression().getType());
- m_context << type.getFunctionIndex(member);
+ m_context << type.getFunctionIdentifier(member);
break;
}
case Type::Category::MAGIC:
@@ -590,7 +590,11 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio
{
solAssert(_arguments.size() == _functionType.getParameterTypes().size(), "");
- unsigned dataOffset = _options.bare ? 0 : 1; // reserve one byte for the function index
+ _options.obtainAddress();
+ if (!_options.bare)
+ CompilerUtils(m_context).storeInMemory(0, g_functionIdentifierOffset);
+
+ unsigned dataOffset = _options.bare ? 0 : g_functionIdentifierOffset; // reserve 4 bytes for the function's hash identifier
for (unsigned i = 0; i < _arguments.size(); ++i)
{
_arguments[i]->accept(*this);
@@ -617,12 +621,13 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio
_options.obtainValue();
else
m_context << u256(0);
- _options.obtainAddress();
- if (!_options.bare)
- m_context << u256(0) << eth::Instruction::MSTORE8;
+ m_context << eth::dupInstruction(6); //copy contract address
+
m_context << u256(25) << eth::Instruction::GAS << eth::Instruction::SUB
<< eth::Instruction::CALL
- << eth::Instruction::POP; // @todo do not ignore failure indicator
+ << eth::Instruction::POP // @todo do not ignore failure indicator
+ << eth::Instruction::POP; // pop contract address
+
if (retSize > 0)
{
bool const leftAligned = firstType->getCategory() == Type::Category::STRING;
diff --git a/Types.cpp b/Types.cpp
index 1a04fe61..e5bfa30f 100644
--- a/Types.cpp
+++ b/Types.cpp
@@ -323,16 +323,13 @@ shared_ptr<FunctionType const> const& ContractType::getConstructorType() const
return m_constructorType;
}
-unsigned ContractType::getFunctionIndex(string const& _functionName) const
+u256 ContractType::getFunctionIdentifier(string const& _functionName) const
{
- unsigned index = 0;
auto interfaceFunctions = m_contract.getInterfaceFunctions();
for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it)
- {
if (it->second->getName() == _functionName)
- return index;
- ++index;
- }
+ return FixedHash<4>::Arith(it->first);
+
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Index of non-existing contract function requested."));
}
diff --git a/Types.h b/Types.h
index 0fe685b8..9d34e19a 100644
--- a/Types.h
+++ b/Types.h
@@ -254,7 +254,7 @@ public:
/// is not used, as this type cannot be the type of a variable or expression.
std::shared_ptr<FunctionType const> const& getConstructorType() const;
- unsigned getFunctionIndex(std::string const& _functionName) const;
+ u256 getFunctionIdentifier(std::string const& _functionName) const;
private:
ContractDefinition const& m_contract;