aboutsummaryrefslogtreecommitdiffstats
path: root/libjulia/backends/evm
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2017-06-23 17:30:02 +0800
committerGitHub <noreply@github.com>2017-06-23 17:30:02 +0800
commit50e8a887a4647e6f2981d43a85f3a7f9763e4146 (patch)
tree9c826c0fc5c70d6d67604d39d4b98a8ae32386d9 /libjulia/backends/evm
parent793f05fa3f573f60e7a460245cd7b9cca37d67cc (diff)
parent58fca7215c2735a7697278ca7f0dd52393b2b301 (diff)
downloaddexon-solidity-50e8a887a4647e6f2981d43a85f3a7f9763e4146.tar.gz
dexon-solidity-50e8a887a4647e6f2981d43a85f3a7f9763e4146.tar.zst
dexon-solidity-50e8a887a4647e6f2981d43a85f3a7f9763e4146.zip
Merge pull request #2443 from ethereum/julia-asm-size
Support appendAssemblySize() in AbstractAssembly
Diffstat (limited to 'libjulia/backends/evm')
-rw-r--r--libjulia/backends/evm/AbstractAssembly.h3
-rw-r--r--libjulia/backends/evm/EVMAssembly.cpp27
-rw-r--r--libjulia/backends/evm/EVMAssembly.h4
3 files changed, 30 insertions, 4 deletions
diff --git a/libjulia/backends/evm/AbstractAssembly.h b/libjulia/backends/evm/AbstractAssembly.h
index f667c1a7..cfc9b8a5 100644
--- a/libjulia/backends/evm/AbstractAssembly.h
+++ b/libjulia/backends/evm/AbstractAssembly.h
@@ -89,6 +89,9 @@ public:
/// Return from a subroutine.
/// @param _stackDiffAfter the stack adjustment after this instruction.
virtual void appendReturnsub(int _returns, int _stackDiffAfter = 0) = 0;
+
+ /// Append the assembled size as a constant.
+ virtual void appendAssemblySize() = 0;
};
enum class IdentifierContext { LValue, RValue };
diff --git a/libjulia/backends/evm/EVMAssembly.cpp b/libjulia/backends/evm/EVMAssembly.cpp
index 7ec26957..173d5e93 100644
--- a/libjulia/backends/evm/EVMAssembly.cpp
+++ b/libjulia/backends/evm/EVMAssembly.cpp
@@ -32,6 +32,8 @@ namespace
{
/// Size of labels in bytes. Four-byte labels are required by some EVM1.5 instructions.
size_t constexpr labelReferenceSize = 4;
+
+size_t constexpr assemblySizeReferenceSize = 4;
}
@@ -145,17 +147,19 @@ void EVMAssembly::appendReturnsub(int _returns, int _stackDiffAfter)
eth::LinkerObject EVMAssembly::finalize()
{
+ size_t bytecodeSize = m_bytecode.size();
+ for (auto const& ref: m_assemblySizePositions)
+ updateReference(ref, assemblySizeReferenceSize, u256(bytecodeSize));
+
for (auto const& ref: m_labelReferences)
{
size_t referencePos = ref.first;
solAssert(m_labelPositions.count(ref.second), "");
size_t labelPos = m_labelPositions.at(ref.second);
solAssert(labelPos != size_t(-1), "Undefined but allocated label used.");
- solAssert(m_bytecode.size() >= 4 && referencePos <= m_bytecode.size() - 4, "");
- solAssert(uint64_t(labelPos) < (uint64_t(1) << (8 * labelReferenceSize)), "");
- for (size_t i = 0; i < labelReferenceSize; i++)
- m_bytecode[referencePos + i] = byte((labelPos >> (8 * (labelReferenceSize - i - 1))) & 0xff);
+ updateReference(referencePos, labelReferenceSize, u256(labelPos));
}
+
eth::LinkerObject obj;
obj.bytecode = m_bytecode;
return obj;
@@ -173,3 +177,18 @@ void EVMAssembly::appendLabelReferenceInternal(LabelID _labelId)
m_labelReferences[m_bytecode.size()] = _labelId;
m_bytecode += bytes(labelReferenceSize);
}
+
+void EVMAssembly::appendAssemblySize()
+{
+ appendInstruction(solidity::pushInstruction(assemblySizeReferenceSize));
+ m_assemblySizePositions.push_back(m_bytecode.size());
+ m_bytecode += bytes(assemblySizeReferenceSize);
+}
+
+void EVMAssembly::updateReference(size_t pos, size_t size, u256 value)
+{
+ solAssert(m_bytecode.size() >= size && pos <= m_bytecode.size() - size, "");
+ solAssert(value < (u256(1) << (8 * size)), "");
+ for (size_t i = 0; i < size; i++)
+ m_bytecode[pos + i] = byte((value >> (8 * (size - i - 1))) & 0xff);
+}
diff --git a/libjulia/backends/evm/EVMAssembly.h b/libjulia/backends/evm/EVMAssembly.h
index a2df0cdc..69585822 100644
--- a/libjulia/backends/evm/EVMAssembly.h
+++ b/libjulia/backends/evm/EVMAssembly.h
@@ -70,6 +70,8 @@ public:
/// Return from a subroutine.
virtual void appendReturnsub(int _returns, int _stackDiffAfter) override;
+ /// Append the assembled size as a constant.
+ virtual void appendAssemblySize() override;
/// Resolves references inside the bytecode and returns the linker object.
eth::LinkerObject finalize();
@@ -77,6 +79,7 @@ public:
private:
void setLabelToCurrentPosition(AbstractAssembly::LabelID _labelId);
void appendLabelReferenceInternal(AbstractAssembly::LabelID _labelId);
+ void updateReference(size_t pos, size_t size, u256 value);
bool m_evm15 = false; ///< if true, switch to evm1.5 mode
LabelID m_nextLabelId = 0;
@@ -84,6 +87,7 @@ private:
bytes m_bytecode;
std::map<LabelID, size_t> m_labelPositions;
std::map<size_t, LabelID> m_labelReferences;
+ std::vector<size_t> m_assemblySizePositions;
};
}