Applied code review and some CI failure fixes.

This commit is contained in:
Christian Parpart 2021-09-29 13:22:50 +02:00 committed by Marenz
parent bff68fd469
commit 694c431d74
7 changed files with 55 additions and 38 deletions

View File

@ -65,5 +65,6 @@ This means the following source mappings represent the same information:
``1:2:1;:9;2:1:2;;`` ``1:2:1;:9;2:1:2;;``
Important to note is that :ref:`verbatim <yul-verbatim>` verbatim instructions Important to note is that when the :ref:`verbatim <yul-verbatim>` builtin is used,
do not have individual but only one source mapping. the source mappings will be invalid: The builtin is considered a single
instruction instead of potentially multiple.

View File

@ -87,7 +87,9 @@ size_t AssemblyItem::bytesRequired(size_t _addressLength) const
case PushImmutable: case PushImmutable:
return 1 + 32; return 1 + 32;
case AssignImmutable: case AssignImmutable:
if (m_immutableOccurrences) if (!m_immutableOccurrences)
return 0;
else if (m_immutableOccurrences.value() != 0)
// (DUP DUP PUSH <n> ADD MSTORE)* (PUSH <n> ADD MSTORE) // (DUP DUP PUSH <n> ADD MSTORE)* (PUSH <n> ADD MSTORE)
return (*m_immutableOccurrences - 1) * (5 + 32) + (3 + 32); return (*m_immutableOccurrences - 1) * (5 + 32) + (3 + 32);
else else
@ -324,6 +326,27 @@ ostream& solidity::evmasm::operator<<(ostream& _out, AssemblyItem const& _item)
return _out; return _out;
} }
size_t AssemblyItem::opcodeCount() const noexcept
{
switch (m_type)
{
case AssemblyItemType::AssignImmutable:
// Append empty items if this AssignImmutable was referenced more than once.
// For n immutable occurrences the first (n - 1) occurrences will
// generate 5 opcodes and the last will generate 3 opcodes,
// because it is reusing the 2 top-most elements on the stack.
if (!m_immutableOccurrences)
// AssignImmutable without any uses does not need to be assigned to at all.
return 0;
else if (m_immutableOccurrences.value() != 0)
return (*m_immutableOccurrences - 1) * 5 + 3;
else
return 2; // two POP's
default:
return 1;
}
}
std::string AssemblyItem::computeSourceMapping( std::string AssemblyItem::computeSourceMapping(
AssemblyItems const& _items, AssemblyItems const& _items,
map<string, unsigned> const& _sourceIndicesMap map<string, unsigned> const& _sourceIndicesMap
@ -405,17 +428,8 @@ std::string AssemblyItem::computeSourceMapping(
} }
} }
// Append empty items if this AssignImmutable was referenced more than once. if (item.opcodeCount() > 1)
// For n immutable occurrences the first (n - 1) occurrences will ret += string(item.opcodeCount() - 1, ';');
// generate 5 opcodes and the last will generate 3 opcodes,
// because it is reusing the 2 top-most elements on the stack.
if (item.immutableOccurrences())
{
auto const n = (item.immutableOccurrences() - 1) * 5 + 3;
ret += string(n - 1, ';');
}
else if (item.type() == AssemblyItemType::AssignImmutable)
ret += ';'; // two POP's
prevStart = location.start; prevStart = location.start;
prevLength = length; prevLength = length;

View File

@ -171,7 +171,8 @@ public:
size_t m_modifierDepth = 0; size_t m_modifierDepth = 0;
void setImmutableOccurrences(size_t _n) const { m_immutableOccurrences = _n; } void setImmutableOccurrences(size_t _n) const { m_immutableOccurrences = _n; }
size_t immutableOccurrences() const noexcept { return m_immutableOccurrences.value_or(0); }
size_t opcodeCount() const noexcept;
private: private:
AssemblyItemType m_type; AssemblyItemType m_type;

View File

@ -21,15 +21,16 @@
* Tests for the assembler. * Tests for the assembler.
*/ */
#include <libsolutil/JSON.h>
#include <libevmasm/Assembly.h> #include <libevmasm/Assembly.h>
#include <libsolutil/JSON.h>
#include <libyul/Exceptions.h>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <algorithm>
#include <memory>
#include <string> #include <string>
#include <tuple> #include <tuple>
#include <memory>
#include <libyul/Exceptions.h>
using namespace std; using namespace std;
using namespace solidity::langutil; using namespace solidity::langutil;
@ -196,22 +197,22 @@ BOOST_AUTO_TEST_CASE(immutables_and_its_source_maps)
}; };
auto subAsm = make_shared<Assembly>(); auto subAsm = make_shared<Assembly>();
for (int8_t i = 0; i < numImmutables; ++i) for (char i = 0; i < numImmutables; ++i)
{ {
for (int r = 0; r < numActualRefs; ++r) for (int r = 0; r < numActualRefs; ++r)
{ {
subAsm->setSourceLocation(SourceLocation{10*i, 10*i + 6 + r, subName}); subAsm->setSourceLocation(SourceLocation{10*i, 10*i + 6 + r, subName});
subAsm->appendImmutable(string(1, 'a' + i)); // "a", "b", ... subAsm->appendImmutable(string(1, char('a' + i))); // "a", "b", ...
} }
} }
Assembly assembly; Assembly assembly;
for (int8_t i = 1; i <= numImmutables; ++i) for (char i = 1; i <= numImmutables; ++i)
{ {
assembly.setSourceLocation({10*i, 10*i + 3+i, assemblyName}); assembly.setSourceLocation({10*i, 10*i + 3+i, assemblyName});
assembly.append(u256(0x71)); // immutble value assembly.append(u256(0x71)); // immutble value
assembly.append(u256(0)); // target... modules? assembly.append(u256(0)); // target... modules?
assembly.appendImmutableAssignment(string(1, 'a' + i - 1)); assembly.appendImmutableAssignment(string(1, char('a' + i - 1)));
} }
assembly.appendSubroutine(subAsm); assembly.appendSubroutine(subAsm);

View File

@ -11,15 +11,15 @@ object "Contract" {
// ---- // ----
// Assembly: // Assembly:
// /* "source":33:48 */ // /* "source":33:48 */
// jump(tag_1) // jump(tag_3)
// tag_2: // tag_1:
// tag_3: // tag_4:
// jump // out // jump // out
// /* "source":53:68 */ // /* "source":53:68 */
// tag_4: // tag_2:
// tag_5: // tag_5:
// jump // out // jump // out
// tag_1: // tag_3:
// /* "source":83:84 */ // /* "source":83:84 */
// 0x01 // 0x01
// /* "source":80:81 */ // /* "source":80:81 */

View File

@ -17,4 +17,4 @@ object "a" {
// assignImmutable("0x85a5b1db611c82c46f5fa18e39ae218397536256c451e5de155a86de843a9ad6") // assignImmutable("0x85a5b1db611c82c46f5fa18e39ae218397536256c451e5de155a86de843a9ad6")
// Bytecode: 73123456789012345678901234567890123456789060005050 // Bytecode: 73123456789012345678901234567890123456789060005050
// Opcodes: PUSH20 0x1234567890123456789012345678901234567890 PUSH1 0x0 POP POP // Opcodes: PUSH20 0x1234567890123456789012345678901234567890 PUSH1 0x0 POP POP
// SourceMappings: 167:42:0:-:0;58:1;32:187 // SourceMappings: 167:42:0:-:0;58:1;32:187;

View File

@ -11,21 +11,21 @@ object "Contract" {
// ---- // ----
// Assembly: // Assembly:
// /* "source":33:54 */ // /* "source":33:54 */
// jump(tag_1) // jump(tag_3)
// tag_2: // tag_1:
// /* "source":48:52 */ // /* "source":48:52 */
// tag_4 // tag_5
// /* "source":50:51 */ // /* "source":50:51 */
// 0x01 // 0x01
// /* "source":48:52 */ // /* "source":48:52 */
// tag_5 // tag_2
// jump // in // jump // in
// tag_4: // tag_5:
// /* "source":33:54 */ // /* "source":33:54 */
// tag_3: // tag_4:
// jump // out // jump // out
// /* "source":59:104 */ // /* "source":59:104 */
// tag_5: // tag_2:
// /* "source":78:79 */ // /* "source":78:79 */
// dup1 // dup1
// /* "source":75:89 */ // /* "source":75:89 */
@ -46,20 +46,20 @@ object "Contract" {
// /* "source":92:101 */ // /* "source":92:101 */
// add // add
// /* "source":90:102 */ // /* "source":90:102 */
// tag_5 // tag_2
// jump // in // jump // in
// tag_8: // tag_8:
// /* "source":59:104 */ // /* "source":59:104 */
// pop // pop
// tag_6: // tag_6:
// jump // out // jump // out
// tag_1: // tag_3:
// /* "source":109:113 */ // /* "source":109:113 */
// tag_9 // tag_9
// /* "source":111:112 */ // /* "source":111:112 */
// 0x01 // 0x01
// /* "source":109:113 */ // /* "source":109:113 */
// tag_5 // tag_2
// jump // in // jump // in
// tag_9: // tag_9:
// Bytecode: 6026565b600b6001600e565b5b565b8015601857506024565b602260028201600e565b505b565b602e6001600e565b // Bytecode: 6026565b600b6001600e565b5b565b8015601857506024565b602260028201600e565b505b565b602e6001600e565b