Skip to content

Commit 91bf0ae

Browse files
committed
wip
1 parent 1447010 commit 91bf0ae

File tree

6 files changed

+321
-167
lines changed

6 files changed

+321
-167
lines changed

libyul/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ add_library(yul
6969
backends/evm/OptimizedEVMCodeTransform.cpp
7070
backends/evm/OptimizedEVMCodeTransform.h
7171
backends/evm/SSACFGStackShuffler.h
72-
backends/evm/SSACFGStack.cpp
73-
backends/evm/SSACFGStack.h
7472
backends/evm/SSACFGStackLayout.cpp
7573
backends/evm/SSACFGStackLayout.h
7674
backends/evm/SSACFGEVMCodeTransform.cpp
@@ -93,6 +91,7 @@ add_library(yul
9391
backends/evm/ssa/SSACFG.h
9492
backends/evm/ssa/SSACFGBuilder.cpp
9593
backends/evm/ssa/Stack.h
94+
backends/evm/ssa/Stack.cpp
9695
backends/evm/ssa/SSACFGBuilder.h
9796
backends/evm/ssa/SSACFGJsonExporter.h
9897
backends/evm/ssa/SSACFGJsonExporter.cpp

libyul/backends/evm/SSACFGStackLayout.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#pragma once
2020

2121
#include <libyul/backends/evm/AbstractAssembly.h>
22-
#include <libyul/backends/evm/SSACFGStack.h>
2322
#include <libyul/backends/evm/ssa/SSACFG.h>
23+
#include <libyul/backends/evm/ssa/Stack.h>
2424

2525
#include <libyul/Exceptions.h>
2626

@@ -36,20 +36,15 @@ namespace solidity::yul::ssa
3636

3737
struct SSACFGStackLayout
3838
{
39-
// each operation has a current stack
40-
using Stack = StackData;
41-
// a slot can be some valueId or a labelId
42-
using Slot = Stack::value_type;
43-
4439
// Each block has its own layout
4540
struct BlockLayout
4641
{
4742
// stack layout required to enter the block
48-
Stack stackIn;
43+
StackData stackIn;
4944
// stack layout required to execute the i-th operation in the block
50-
std::vector<Stack> operationIn;
45+
std::vector<StackSlot> operationIn;
5146
// stack after the block was executed
52-
Stack stackOut;
47+
StackData stackOut;
5348
};
5449

5550
// each block has a fixed list of operations

libyul/backends/evm/ssa/Stack.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "fmt/ranges.h"
2+
#include "range/v3/view/drop.hpp"
3+
4+
5+
#include <libyul/backends/evm/ssa/Stack.h>
6+
7+
namespace
8+
{
9+
size_t junkTailSize(solidity::yul::ssa::Stack<>::Data const& _stackData)
10+
{
11+
std::size_t numJunk = 0;
12+
auto it = _stackData.begin();
13+
while (it != _stackData.end() && it->isJunk())
14+
{
15+
++numJunk;
16+
++it;
17+
}
18+
return numJunk;
19+
}
20+
}
21+
22+
namespace solidity::yul::ssa
23+
{
24+
25+
std::string slotToString(StackSlot const& _slot)
26+
{
27+
switch (_slot.kind())
28+
{
29+
case StackSlot::Kind::ValueID:
30+
return fmt::format("v{}", _slot.valueID().value);
31+
case StackSlot::Kind::Junk:
32+
return "JUNK";
33+
case StackSlot::Kind::AssemblyLabelID:
34+
return fmt::format("LABEL[{}]", _slot.assemblyLabelID());
35+
case StackSlot::Kind::FunctionReturnLabel:
36+
return fmt::format("ReturnLabel[ix={}]", _slot.functionReturnLabelIndex());
37+
}
38+
}
39+
40+
std::string slotToString(StackSlot const& _slot, SSACFG const& _cfg)
41+
{
42+
if (_slot.kind() == StackSlot::Kind::ValueID)
43+
return _cfg.valueDescription(_slot.valueID());
44+
45+
return slotToString(_slot);
46+
}
47+
48+
std::string stackToString(Stack<>::Data const& _stackData)
49+
{
50+
auto const numJunk = junkTailSize(_stackData);
51+
if (numJunk > 0)
52+
return fmt::format(
53+
"[JUNK x {}, {}]",
54+
numJunk,
55+
fmt::join(_stackData | ranges::views::drop(numJunk) | ranges::views::transform([&](auto const& _slot) { return slotToString(_slot); }), ", ")
56+
);
57+
58+
return format(
59+
"[{}]",
60+
fmt::join(_stackData | ranges::views::transform([&](auto const& _slot) { return slotToString(_slot); }), ", ")
61+
);
62+
}
63+
64+
std::string stackToString(Stack<>::Data const& _stackData, SSACFG const& _cfg)
65+
{
66+
auto const numJunk = junkTailSize(_stackData);
67+
if (numJunk > 0)
68+
return format(
69+
"[JUNK x {}, {}]",
70+
numJunk,
71+
fmt::join(_stackData | ranges::views::drop(numJunk) | ranges::views::transform([&](auto const& _slot) { return slotToString(_slot, _cfg); }), ", ")
72+
);
73+
74+
return format(
75+
"[{}]",
76+
fmt::join(_stackData | ranges::views::transform([&](auto const& _slot) { return slotToString(_slot, _cfg); }), ", ")
77+
);
78+
}
79+
80+
}

0 commit comments

Comments
 (0)