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