Skip to content

Commit d118f21

Browse files
authored
Merge pull request #12729 from ethereum/splitOutDisassemble
Split out disassemble to remove numeric from instruction includes.
2 parents e8520a6 + b0dcd7b commit d118f21

File tree

16 files changed

+136
-67
lines changed

16 files changed

+136
-67
lines changed

libevmasm/AssemblyItem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <libevmasm/Exceptions.h>
2727
#include <liblangutil/SourceLocation.h>
2828
#include <libsolutil/Common.h>
29+
#include <libsolutil/Numeric.h>
2930
#include <libsolutil/Assertions.h>
3031
#include <optional>
3132
#include <iostream>

libevmasm/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ set(sources
1111
ConstantOptimiser.h
1212
ControlFlowGraph.cpp
1313
ControlFlowGraph.h
14+
Disassemble.cpp
15+
Disassemble.h
1416
Exceptions.h
1517
ExpressionClasses.cpp
1618
ExpressionClasses.h

libevmasm/ConstantOptimiser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <liblangutil/EVMVersion.h>
2828

29+
#include <libsolutil/Numeric.h>
2930
#include <libsolutil/Assertions.h>
3031

3132
#include <vector>

libevmasm/Disassemble.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
This file is part of solidity.
3+
4+
solidity is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
solidity is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with solidity. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
// SPDX-License-Identifier: GPL-3.0
18+
19+
#include <libevmasm/Disassemble.h>
20+
21+
#include <libsolutil/Common.h>
22+
#include <libsolutil/CommonIO.h>
23+
#include <functional>
24+
25+
using namespace std;
26+
using namespace solidity;
27+
using namespace solidity::util;
28+
using namespace solidity::evmasm;
29+
30+
31+
void solidity::evmasm::eachInstruction(
32+
bytes const& _mem,
33+
function<void(Instruction,u256 const&)> const& _onInstruction
34+
)
35+
{
36+
for (auto it = _mem.begin(); it < _mem.end(); ++it)
37+
{
38+
Instruction const instr{*it};
39+
int additional = 0;
40+
if (isValidInstruction(instr))
41+
additional = instructionInfo(instr).additional;
42+
43+
u256 data{};
44+
45+
// fill the data with the additional data bytes from the instruction stream
46+
while (additional > 0 && std::next(it) < _mem.end())
47+
{
48+
data <<= 8;
49+
data |= *++it;
50+
--additional;
51+
}
52+
53+
// pad the remaining number of additional octets with zeros
54+
data <<= 8 * additional;
55+
56+
_onInstruction(instr, data);
57+
}
58+
}
59+
60+
string solidity::evmasm::disassemble(bytes const& _mem, string const& _delimiter)
61+
{
62+
stringstream ret;
63+
eachInstruction(_mem, [&](Instruction _instr, u256 const& _data) {
64+
if (!isValidInstruction(_instr))
65+
ret << "0x" << std::uppercase << std::hex << static_cast<int>(_instr) << _delimiter;
66+
else
67+
{
68+
InstructionInfo info = instructionInfo(_instr);
69+
ret << info.name;
70+
if (info.additional)
71+
ret << " 0x" << std::uppercase << std::hex << _data;
72+
ret << _delimiter;
73+
}
74+
});
75+
return ret.str();
76+
}

libevmasm/Disassemble.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
This file is part of solidity.
3+
4+
solidity is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
solidity is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with solidity. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
// SPDX-License-Identifier: GPL-3.0
18+
19+
#pragma once
20+
21+
#include <libsolutil/Common.h>
22+
#include <libsolutil/Numeric.h>
23+
24+
#include <libevmasm/Instruction.h>
25+
26+
#include <functional>
27+
#include <string>
28+
29+
namespace solidity::evmasm
30+
{
31+
32+
/// Iterate through EVM code and call a function on each instruction.
33+
void eachInstruction(bytes const& _mem, std::function<void(Instruction, u256 const&)> const& _onInstruction);
34+
35+
/// Convert from EVM code to simple EVM assembly language.
36+
std::string disassemble(bytes const& _mem, std::string const& _delimiter = " ");
37+
38+
}

libevmasm/Instruction.cpp

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222

2323
#include <libevmasm/Instruction.h>
2424

25-
#include <libsolutil/Common.h>
26-
#include <libsolutil/CommonIO.h>
27-
#include <functional>
28-
2925
using namespace std;
3026
using namespace solidity;
3127
using namespace solidity::util;
@@ -325,53 +321,6 @@ static std::map<Instruction, InstructionInfo> const c_instructionInfo =
325321
{ Instruction::SELFDESTRUCT, { "SELFDESTRUCT", 0, 1, 0, true, Tier::Special } }
326322
};
327323

328-
void solidity::evmasm::eachInstruction(
329-
bytes const& _mem,
330-
function<void(Instruction,u256 const&)> const& _onInstruction
331-
)
332-
{
333-
for (auto it = _mem.begin(); it < _mem.end(); ++it)
334-
{
335-
auto instr = Instruction(*it);
336-
int additional = 0;
337-
if (isValidInstruction(instr))
338-
additional = instructionInfo(instr).additional;
339-
340-
u256 data;
341-
342-
// fill the data with the additional data bytes from the instruction stream
343-
while (additional > 0 && std::next(it) < _mem.end())
344-
{
345-
data <<= 8;
346-
data |= *++it;
347-
--additional;
348-
}
349-
350-
// pad the remaining number of additional octets with zeros
351-
data <<= 8 * additional;
352-
353-
_onInstruction(instr, data);
354-
}
355-
}
356-
357-
string solidity::evmasm::disassemble(bytes const& _mem, string const& _delimiter)
358-
{
359-
stringstream ret;
360-
eachInstruction(_mem, [&](Instruction _instr, u256 const& _data) {
361-
if (!isValidInstruction(_instr))
362-
ret << "0x" << std::uppercase << std::hex << static_cast<int>(_instr) << _delimiter;
363-
else
364-
{
365-
InstructionInfo info = instructionInfo(_instr);
366-
ret << info.name;
367-
if (info.additional)
368-
ret << " 0x" << std::uppercase << std::hex << _data;
369-
ret << _delimiter;
370-
}
371-
});
372-
return ret.str();
373-
}
374-
375324
InstructionInfo solidity::evmasm::instructionInfo(Instruction _inst)
376325
{
377326
try
@@ -380,7 +329,7 @@ InstructionInfo solidity::evmasm::instructionInfo(Instruction _inst)
380329
}
381330
catch (...)
382331
{
383-
return InstructionInfo({"<INVALID_INSTRUCTION: " + toString((unsigned)_inst) + ">", 0, 0, 0, false, Tier::Invalid});
332+
return InstructionInfo({"<INVALID_INSTRUCTION: " + to_string(static_cast<unsigned>(_inst)) + ">", 0, 0, 0, false, Tier::Invalid});
384333
}
385334
}
386335

libevmasm/Instruction.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
#include <libevmasm/Exceptions.h>
2626
#include <libsolutil/Common.h>
2727
#include <libsolutil/Assertions.h>
28-
#include <libsolutil/Numeric.h>
29-
#include <functional>
3028

3129
namespace solidity::evmasm
3230
{
@@ -217,25 +215,25 @@ inline bool isLogInstruction(Instruction _inst)
217215
/// @returns the number of PUSH Instruction _inst
218216
inline unsigned getPushNumber(Instruction _inst)
219217
{
220-
return (uint8_t)_inst - unsigned(Instruction::PUSH1) + 1;
218+
return static_cast<uint8_t>(_inst) - unsigned(Instruction::PUSH1) + 1;
221219
}
222220

223221
/// @returns the number of DUP Instruction _inst
224222
inline unsigned getDupNumber(Instruction _inst)
225223
{
226-
return (uint8_t)_inst - unsigned(Instruction::DUP1) + 1;
224+
return static_cast<uint8_t>(_inst) - unsigned(Instruction::DUP1) + 1;
227225
}
228226

229227
/// @returns the number of SWAP Instruction _inst
230228
inline unsigned getSwapNumber(Instruction _inst)
231229
{
232-
return (uint8_t)_inst - unsigned(Instruction::SWAP1) + 1;
230+
return static_cast<uint8_t>(_inst) - unsigned(Instruction::SWAP1) + 1;
233231
}
234232

235233
/// @returns the number of LOG Instruction _inst
236234
inline unsigned getLogNumber(Instruction _inst)
237235
{
238-
return (uint8_t)_inst - unsigned(Instruction::LOG0);
236+
return static_cast<uint8_t>(_inst) - unsigned(Instruction::LOG0);
239237
}
240238

241239
/// @returns the PUSH<_number> instruction
@@ -266,7 +264,7 @@ inline Instruction logInstruction(unsigned _number)
266264
return Instruction(unsigned(Instruction::LOG0) + _number);
267265
}
268266

269-
enum class Tier : unsigned
267+
enum class Tier
270268
{
271269
Zero = 0, // 0, Zero
272270
Base, // 2, Quick
@@ -301,10 +299,4 @@ bool isValidInstruction(Instruction _inst);
301299
/// Convert from string mnemonic to Instruction type.
302300
extern const std::map<std::string, Instruction> c_instructions;
303301

304-
/// Iterate through EVM code and call a function on each instruction.
305-
void eachInstruction(bytes const& _mem, std::function<void(Instruction, u256 const&)> const& _onInstruction);
306-
307-
/// Convert from EVM code to simple EVM assembly language.
308-
std::string disassemble(bytes const& _mem, std::string const& _delimiter = " ");
309-
310302
}

libevmasm/SemanticInformation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626

2727
#include <libevmasm/Instruction.h>
2828

29+
#include <optional>
30+
#include <vector>
31+
2932
namespace solidity::evmasm
3033
{
3134

libsolidity/interface/StandardCompiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <libyul/Exceptions.h>
3030
#include <libyul/optimiser/Suite.h>
3131

32-
#include <libevmasm/Instruction.h>
32+
#include <libevmasm/Disassemble.h>
3333

3434
#include <libsmtutil/Exceptions.h>
3535

libyul/backends/evm/EVMMetrics.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <libyul/optimiser/ASTWalker.h>
2525
#include <liblangutil/EVMVersion.h>
26+
#include <libsolutil/Numeric.h>
2627
#include <libevmasm/Instruction.h>
2728

2829
namespace solidity::yul

0 commit comments

Comments
 (0)