StreamWriter.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "StreamWriter.h"
  2. #include "llvm/ADT/StringExtras.h"
  3. #include "llvm/Support/Format.h"
  4. #include <cctype>
  5. using namespace llvm::support;
  6. namespace llvm {
  7. raw_ostream &operator<<(raw_ostream &OS, const HexNumber& Value) {
  8. uint64_t N = Value.Value;
  9. // Zero is a special case.
  10. if (N == 0)
  11. return OS << "0x0";
  12. char NumberBuffer[20];
  13. char *EndPtr = NumberBuffer + sizeof(NumberBuffer);
  14. char *CurPtr = EndPtr;
  15. while (N) {
  16. uintptr_t X = N % 16;
  17. *--CurPtr = (X < 10 ? '0' + X : 'A' + X - 10);
  18. N /= 16;
  19. }
  20. OS << "0x";
  21. return OS.write(CurPtr, EndPtr - CurPtr);
  22. }
  23. void StreamWriter::printBinaryImpl(StringRef Label, StringRef Str,
  24. ArrayRef<uint8_t> Data, bool Block) {
  25. if (Data.size() > 16)
  26. Block = true;
  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. startLine() << ")\n";
  55. } else {
  56. startLine() << Label << ":";
  57. if (Str.size() > 0)
  58. OS << " " << Str;
  59. OS << " (";
  60. for (size_t i = 0; i < Data.size(); ++i) {
  61. if (i > 0)
  62. OS << " ";
  63. OS << format("%02X", static_cast<int>(Data[i]));
  64. }
  65. OS << ")\n";
  66. }
  67. }
  68. } // namespace llvm