|
| 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 | +/** |
| 18 | + * @author Christian <[email protected]> |
| 19 | + * @date 2017 |
| 20 | + * Converts a parsed assembly into its textual form. |
| 21 | + */ |
| 22 | + |
| 23 | +#include <libsolidity/inlineasm/AsmPrinter.h> |
| 24 | + |
| 25 | +#include <libsolidity/inlineasm/AsmData.h> |
| 26 | + |
| 27 | +#include <boost/algorithm/string.hpp> |
| 28 | +#include <boost/algorithm/string/replace.hpp> |
| 29 | +#include <boost/range/adaptor/transformed.hpp> |
| 30 | + |
| 31 | +#include <memory> |
| 32 | +#include <functional> |
| 33 | + |
| 34 | +using namespace std; |
| 35 | +using namespace dev; |
| 36 | +using namespace dev::solidity; |
| 37 | +using namespace dev::solidity::assembly; |
| 38 | + |
| 39 | +//@TODO source locations |
| 40 | + |
| 41 | +string AsmPrinter::operator()(assembly::Instruction const& _instruction) |
| 42 | +{ |
| 43 | + return boost::to_lower_copy(instructionInfo(_instruction.instruction).name); |
| 44 | +} |
| 45 | + |
| 46 | +string AsmPrinter::operator()(assembly::Literal const& _literal) |
| 47 | +{ |
| 48 | + if (_literal.isNumber) |
| 49 | + return _literal.value; |
| 50 | + string out; |
| 51 | + for (char c: _literal.value) |
| 52 | + if (c == '\\') |
| 53 | + out += "\\\\"; |
| 54 | + else if (c == '"') |
| 55 | + out += "\\\""; |
| 56 | + else if (c == '\b') |
| 57 | + out += "\\b"; |
| 58 | + else if (c == '\f') |
| 59 | + out += "\\f"; |
| 60 | + else if (c == '\n') |
| 61 | + out += "\\n"; |
| 62 | + else if (c == '\r') |
| 63 | + out += "\\r"; |
| 64 | + else if (c == '\t') |
| 65 | + out += "\\t"; |
| 66 | + else if (c == '\v') |
| 67 | + out += "\\v"; |
| 68 | + else if (!isprint(c, locale::classic())) |
| 69 | + { |
| 70 | + ostringstream o; |
| 71 | + o << std::hex << setfill('0') << setw(2) << (unsigned)(unsigned char)(c); |
| 72 | + out += "\\x" + o.str(); |
| 73 | + } |
| 74 | + else |
| 75 | + out += c; |
| 76 | + return "\"" + out + "\""; |
| 77 | +} |
| 78 | + |
| 79 | +string AsmPrinter::operator()(assembly::Identifier const& _identifier) |
| 80 | +{ |
| 81 | + return _identifier.name; |
| 82 | +} |
| 83 | + |
| 84 | +string AsmPrinter::operator()(assembly::FunctionalInstruction const& _functionalInstruction) |
| 85 | +{ |
| 86 | + return |
| 87 | + (*this)(_functionalInstruction.instruction) + |
| 88 | + "(" + |
| 89 | + boost::algorithm::join( |
| 90 | + _functionalInstruction.arguments | boost::adaptors::transformed(boost::apply_visitor(*this)), |
| 91 | + ", " ) + |
| 92 | + ")"; |
| 93 | +} |
| 94 | + |
| 95 | +string AsmPrinter::operator()(assembly::Label const& _label) |
| 96 | +{ |
| 97 | + return _label.name + ":"; |
| 98 | +} |
| 99 | + |
| 100 | +string AsmPrinter::operator()(assembly::Assignment const& _assignment) |
| 101 | +{ |
| 102 | + return "=: " + (*this)(_assignment.variableName); |
| 103 | +} |
| 104 | + |
| 105 | +string AsmPrinter::operator()(assembly::FunctionalAssignment const& _functionalAssignment) |
| 106 | +{ |
| 107 | + return (*this)(_functionalAssignment.variableName) + " := " + boost::apply_visitor(*this, *_functionalAssignment.value); |
| 108 | +} |
| 109 | + |
| 110 | +string AsmPrinter::operator()(assembly::VariableDeclaration const& _variableDeclaration) |
| 111 | +{ |
| 112 | + return "let " + _variableDeclaration.name + " := " + boost::apply_visitor(*this, *_variableDeclaration.value); |
| 113 | +} |
| 114 | + |
| 115 | +string AsmPrinter::operator()(Block const& _block) |
| 116 | +{ |
| 117 | + if (_block.statements.empty()) |
| 118 | + return "{\n}"; |
| 119 | + string body = boost::algorithm::join( |
| 120 | + _block.statements | boost::adaptors::transformed(boost::apply_visitor(*this)), |
| 121 | + "\n" |
| 122 | + ); |
| 123 | + boost::replace_all(body, "\n", "\n "); |
| 124 | + return "{\n " + body + "\n}"; |
| 125 | +} |
0 commit comments