|
| 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