|
| 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 <libyul/backends/evm/ssa/Stack.h> |
| 20 | + |
| 21 | +#include <fmt/ranges.h> |
| 22 | + |
| 23 | +#include <range/v3/view/drop.hpp> |
| 24 | +#include <range/v3/view/take_while.hpp> |
| 25 | + |
| 26 | +namespace |
| 27 | +{ |
| 28 | +size_t junkTailSize(solidity::yul::ssa::StackData const& _stackData) |
| 29 | +{ |
| 30 | + auto junkTail = _stackData | ranges::views::take_while([](auto const& slot) { return slot.isJunk(); }); |
| 31 | + return static_cast<size_t>(ranges::distance(junkTail)); |
| 32 | +} |
| 33 | +} |
| 34 | + |
| 35 | +namespace solidity::yul::ssa |
| 36 | +{ |
| 37 | + |
| 38 | +std::string slotToString(StackSlot const& _slot) |
| 39 | +{ |
| 40 | + switch (_slot.kind()) |
| 41 | + { |
| 42 | + case StackSlot::Kind::ValueID: |
| 43 | + if (_slot.isLiteralValueID()) |
| 44 | + return fmt::format("lit{}", _slot.valueID().value()); |
| 45 | + else |
| 46 | + return fmt::format("v{}", _slot.valueID().value()); |
| 47 | + case StackSlot::Kind::Junk: |
| 48 | + return "JUNK"; |
| 49 | + case StackSlot::Kind::FunctionCallReturnLabel: |
| 50 | + return fmt::format("FunctionCallReturnLabel[{}]", _slot.functionCallReturnLabel()); |
| 51 | + case StackSlot::Kind::FunctionReturnLabel: |
| 52 | + return fmt::format("ReturnLabel[{}]", _slot.functionReturnLabel()); |
| 53 | + } |
| 54 | + util::unreachable(); |
| 55 | +} |
| 56 | + |
| 57 | +std::string slotToString(StackSlot const& _slot, SSACFG const& _cfg) |
| 58 | +{ |
| 59 | + if (_slot.kind() == StackSlot::Kind::ValueID) |
| 60 | + return _slot.valueID().str(_cfg); |
| 61 | + |
| 62 | + return slotToString(_slot); |
| 63 | +} |
| 64 | + |
| 65 | +std::string stackToString(StackData const& _stackData) |
| 66 | +{ |
| 67 | + auto const numJunk = junkTailSize(_stackData); |
| 68 | + if (numJunk > 0) |
| 69 | + return fmt::format( |
| 70 | + "[JUNK x {}, {}]", |
| 71 | + numJunk, |
| 72 | + fmt::join(_stackData | ranges::views::drop(numJunk) | ranges::views::transform([&](auto const& _slot) { return slotToString(_slot); }), ", ") |
| 73 | + ); |
| 74 | + |
| 75 | + return fmt::format( |
| 76 | + "[{}]", |
| 77 | + fmt::join(_stackData | ranges::views::transform([&](auto const& _slot) { return slotToString(_slot); }), ", ") |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +std::string stackToString(StackData const& _stackData, SSACFG const& _cfg) |
| 82 | +{ |
| 83 | + auto const numJunk = junkTailSize(_stackData); |
| 84 | + if (numJunk > 0) |
| 85 | + return fmt::format( |
| 86 | + "[JUNK x {}, {}]", |
| 87 | + numJunk, |
| 88 | + fmt::join(_stackData | ranges::views::drop(numJunk) | ranges::views::transform([&](auto const& _slot) { return slotToString(_slot, _cfg); }), ", ") |
| 89 | + ); |
| 90 | + |
| 91 | + return fmt::format( |
| 92 | + "[{}]", |
| 93 | + fmt::join(_stackData | ranges::views::transform([&](auto const& _slot) { return slotToString(_slot, _cfg); }), ", ") |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +} |
0 commit comments