blob: 0225f01156a9a7c3e761331ea846f7076f955824 [file] [log] [blame]
Zachary Turner88bb1632016-05-03 00:28:04 +00001#include "llvm/Support/ScopedPrinter.h"
2
Eric Christopher9cad53c2013-04-03 18:31:38 +00003#include "llvm/ADT/StringExtras.h"
4#include "llvm/Support/Format.h"
5#include <cctype>
6
7using namespace llvm::support;
8
9namespace llvm {
10
Zachary Turner88bb1632016-05-03 00:28:04 +000011raw_ostream &operator<<(raw_ostream &OS, const HexNumber &Value) {
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +000012 OS << "0x" << to_hexString(Value.Value);
13 return OS;
14}
Eric Christopher9cad53c2013-04-03 18:31:38 +000015
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +000016const 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 Christopher9cad53c2013-04-03 18:31:38 +000022
Zachary Turner88bb1632016-05-03 00:28:04 +000023void ScopedPrinter::printBinaryImpl(StringRef Label, StringRef Str,
24 ArrayRef<uint8_t> Data, bool Block) {
Eric Christopher9cad53c2013-04-03 18:31:38 +000025 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