blob: 390b05e358d377b34ad2922cdb6fc2fb49517db9 [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
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +000022const 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 Christopher9cad53c2013-04-03 18:31:38 +000027}
28
29void 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