blob: 391c12691da5466c10ed7b3738bf7f50271e6ac5 [file] [log] [blame]
Eric Christopher9cad53c2013-04-03 18:31:38 +00001#include "StreamWriter.h"
2#include "llvm/ADT/StringExtras.h"
3#include "llvm/Support/Format.h"
4#include <cctype>
5
6using namespace llvm::support;
7
8namespace llvm {
9
10raw_ostream &operator<<(raw_ostream &OS, const HexNumber& Value) {
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +000011 OS << "0x" << to_hexString(Value.Value);
12 return OS;
13}
Eric Christopher9cad53c2013-04-03 18:31:38 +000014
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +000015const 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 Christopher9cad53c2013-04-03 18:31:38 +000021
Eric Christopher9cad53c2013-04-03 18:31:38 +000022void StreamWriter::printBinaryImpl(StringRef Label, StringRef Str,
23 ArrayRef<uint8_t> Data, bool Block) {
24 if (Data.size() > 16)
25 Block = true;
26
27 if (Block) {
28 startLine() << Label;
29 if (Str.size() > 0)
30 OS << ": " << Str;
31 OS << " (\n";
32 for (size_t addr = 0, end = Data.size(); addr < end; addr += 16) {
33 startLine() << format(" %04" PRIX64 ": ", uint64_t(addr));
34 // Dump line of hex.
35 for (size_t i = 0; i < 16; ++i) {
36 if (i != 0 && i % 4 == 0)
37 OS << ' ';
38 if (addr + i < end)
39 OS << hexdigit((Data[addr + i] >> 4) & 0xF, false)
40 << hexdigit(Data[addr + i] & 0xF, false);
41 else
42 OS << " ";
43 }
44 // Print ascii.
45 OS << " |";
46 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
47 if (std::isprint(Data[addr + i] & 0xFF))
48 OS << Data[addr + i];
49 else
50 OS << ".";
51 }
52 OS << "|\n";
53 }
54
55 startLine() << ")\n";
56 } else {
57 startLine() << Label << ":";
58 if (Str.size() > 0)
59 OS << " " << Str;
60 OS << " (";
61 for (size_t i = 0; i < Data.size(); ++i) {
62 if (i > 0)
63 OS << " ";
64
65 OS << format("%02X", static_cast<int>(Data[i]));
66 }
67 OS << ")\n";
68 }
69}
70
71} // namespace llvm