Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame^] | 1 | #include "llvm/Support/ScopedPrinter.h" |
| 2 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 3 | #include "llvm/ADT/StringExtras.h" |
| 4 | #include "llvm/Support/Format.h" |
| 5 | #include <cctype> |
| 6 | |
| 7 | using namespace llvm::support; |
| 8 | |
| 9 | namespace llvm { |
| 10 | |
Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame^] | 11 | raw_ostream &operator<<(raw_ostream &OS, const HexNumber &Value) { |
Hemant Kulkarni | d8a985e | 2016-02-10 20:40:55 +0000 | [diff] [blame] | 12 | OS << "0x" << to_hexString(Value.Value); |
| 13 | return OS; |
| 14 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 15 | |
Hemant Kulkarni | d8a985e | 2016-02-10 20:40:55 +0000 | [diff] [blame] | 16 | const std::string to_hexString(uint64_t Value, bool UpperCase) { |
| 17 | std::string number; |
| 18 | llvm::raw_string_ostream stream(number); |
| 19 | stream << format_hex_no_prefix(Value, 1, UpperCase); |
| 20 | return stream.str(); |
| 21 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 22 | |
Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame^] | 23 | void ScopedPrinter::printBinaryImpl(StringRef Label, StringRef Str, |
| 24 | ArrayRef<uint8_t> Data, bool Block) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 25 | if (Data.size() > 16) |
| 26 | Block = true; |
| 27 | |
| 28 | if (Block) { |
| 29 | startLine() << Label; |
| 30 | if (Str.size() > 0) |
| 31 | OS << ": " << Str; |
| 32 | OS << " (\n"; |
| 33 | for (size_t addr = 0, end = Data.size(); addr < end; addr += 16) { |
| 34 | startLine() << format(" %04" PRIX64 ": ", uint64_t(addr)); |
| 35 | // Dump line of hex. |
| 36 | for (size_t i = 0; i < 16; ++i) { |
| 37 | if (i != 0 && i % 4 == 0) |
| 38 | OS << ' '; |
| 39 | if (addr + i < end) |
| 40 | OS << hexdigit((Data[addr + i] >> 4) & 0xF, false) |
| 41 | << hexdigit(Data[addr + i] & 0xF, false); |
| 42 | else |
| 43 | OS << " "; |
| 44 | } |
| 45 | // Print ascii. |
| 46 | OS << " |"; |
| 47 | for (std::size_t i = 0; i < 16 && addr + i < end; ++i) { |
| 48 | if (std::isprint(Data[addr + i] & 0xFF)) |
| 49 | OS << Data[addr + i]; |
| 50 | else |
| 51 | OS << "."; |
| 52 | } |
| 53 | OS << "|\n"; |
| 54 | } |
| 55 | |
| 56 | startLine() << ")\n"; |
| 57 | } else { |
| 58 | startLine() << Label << ":"; |
| 59 | if (Str.size() > 0) |
| 60 | OS << " " << Str; |
| 61 | OS << " ("; |
| 62 | for (size_t i = 0; i < Data.size(); ++i) { |
| 63 | if (i > 0) |
| 64 | OS << " "; |
| 65 | |
| 66 | OS << format("%02X", static_cast<int>(Data[i])); |
| 67 | } |
| 68 | OS << ")\n"; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | } // namespace llvm |