2013-04-03 18:31:38 +00:00
|
|
|
//===-- StreamWriter.h ----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 16:26:38 +00:00
|
|
|
#ifndef LLVM_TOOLS_LLVM_READOBJ_STREAMWRITER_H
|
|
|
|
#define LLVM_TOOLS_LLVM_READOBJ_STREAMWRITER_H
|
2013-04-03 18:31:38 +00:00
|
|
|
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2014-01-13 08:04:33 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2013-04-03 18:31:38 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::support;
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct EnumEntry {
|
|
|
|
StringRef Name;
|
|
|
|
T Value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HexNumber {
|
|
|
|
// To avoid sign-extension we have to explicitly cast to the appropriate
|
|
|
|
// unsigned type. The overloads are here so that every type that is implicitly
|
|
|
|
// convertible to an integer (including enums and endian helpers) can be used
|
|
|
|
// without requiring type traits or call-site changes.
|
|
|
|
HexNumber(int8_t Value) : Value(static_cast<uint8_t >(Value)) { }
|
|
|
|
HexNumber(int16_t Value) : Value(static_cast<uint16_t>(Value)) { }
|
|
|
|
HexNumber(int32_t Value) : Value(static_cast<uint32_t>(Value)) { }
|
|
|
|
HexNumber(int64_t Value) : Value(static_cast<uint64_t>(Value)) { }
|
|
|
|
HexNumber(uint8_t Value) : Value(Value) { }
|
|
|
|
HexNumber(uint16_t Value) : Value(Value) { }
|
|
|
|
HexNumber(uint32_t Value) : Value(Value) { }
|
|
|
|
HexNumber(uint64_t Value) : Value(Value) { }
|
|
|
|
uint64_t Value;
|
|
|
|
};
|
|
|
|
|
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const HexNumber& Value);
|
|
|
|
|
|
|
|
class StreamWriter {
|
|
|
|
public:
|
|
|
|
StreamWriter(raw_ostream &OS)
|
|
|
|
: OS(OS)
|
|
|
|
, IndentLevel(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush() {
|
|
|
|
OS.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void indent(int Levels = 1) {
|
|
|
|
IndentLevel += Levels;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unindent(int Levels = 1) {
|
|
|
|
IndentLevel = std::max(0, IndentLevel - Levels);
|
|
|
|
}
|
|
|
|
|
|
|
|
void printIndent() {
|
|
|
|
for (int i = 0; i < IndentLevel; ++i)
|
|
|
|
OS << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
HexNumber hex(T Value) {
|
|
|
|
return HexNumber(Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename TEnum>
|
|
|
|
void printEnum(StringRef Label, T Value,
|
|
|
|
ArrayRef<EnumEntry<TEnum> > EnumValues) {
|
|
|
|
StringRef Name;
|
|
|
|
bool Found = false;
|
2014-04-20 21:05:30 +00:00
|
|
|
for (const auto &EnumItem : EnumValues) {
|
|
|
|
if (EnumItem.Value == Value) {
|
|
|
|
Name = EnumItem.Name;
|
2013-04-03 18:31:38 +00:00
|
|
|
Found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Found) {
|
|
|
|
startLine() << Label << ": " << Name << " (" << hex(Value) << ")\n";
|
|
|
|
} else {
|
|
|
|
startLine() << Label << ": " << hex(Value) << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 14:04:44 +00:00
|
|
|
template <typename T, typename TFlag>
|
|
|
|
void printFlags(StringRef Label, T Value, ArrayRef<EnumEntry<TFlag>> Flags,
|
2015-05-08 07:04:59 +00:00
|
|
|
TFlag EnumMask1 = {}, TFlag EnumMask2 = {},
|
|
|
|
TFlag EnumMask3 = {}) {
|
2013-04-03 18:31:38 +00:00
|
|
|
typedef EnumEntry<TFlag> FlagEntry;
|
|
|
|
typedef SmallVector<FlagEntry, 10> FlagVector;
|
|
|
|
FlagVector SetFlags;
|
|
|
|
|
2014-04-20 21:05:30 +00:00
|
|
|
for (const auto &Flag : Flags) {
|
|
|
|
if (Flag.Value == 0)
|
2013-04-03 18:31:38 +00:00
|
|
|
continue;
|
|
|
|
|
2015-05-07 14:04:44 +00:00
|
|
|
TFlag EnumMask{};
|
|
|
|
if (Flag.Value & EnumMask1)
|
|
|
|
EnumMask = EnumMask1;
|
|
|
|
else if (Flag.Value & EnumMask2)
|
|
|
|
EnumMask = EnumMask2;
|
2015-05-08 07:04:59 +00:00
|
|
|
else if (Flag.Value & EnumMask3)
|
|
|
|
EnumMask = EnumMask3;
|
2014-04-20 21:05:30 +00:00
|
|
|
bool IsEnum = (Flag.Value & EnumMask) != 0;
|
|
|
|
if ((!IsEnum && (Value & Flag.Value) == Flag.Value) ||
|
|
|
|
(IsEnum && (Value & EnumMask) == Flag.Value)) {
|
|
|
|
SetFlags.push_back(Flag);
|
2013-04-03 18:31:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(SetFlags.begin(), SetFlags.end(), &flagName<TFlag>);
|
|
|
|
|
|
|
|
startLine() << Label << " [ (" << hex(Value) << ")\n";
|
2014-04-20 21:05:30 +00:00
|
|
|
for (const auto &Flag : SetFlags) {
|
|
|
|
startLine() << " " << Flag.Name << " (" << hex(Flag.Value) << ")\n";
|
2013-04-03 18:31:38 +00:00
|
|
|
}
|
|
|
|
startLine() << "]\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void printFlags(StringRef Label, T Value) {
|
|
|
|
startLine() << Label << " [ (" << hex(Value) << ")\n";
|
|
|
|
uint64_t Flag = 1;
|
|
|
|
uint64_t Curr = Value;
|
|
|
|
while (Curr > 0) {
|
|
|
|
if (Curr & 1)
|
|
|
|
startLine() << " " << hex(Flag) << "\n";
|
|
|
|
Curr >>= 1;
|
|
|
|
Flag <<= 1;
|
|
|
|
}
|
|
|
|
startLine() << "]\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void printNumber(StringRef Label, uint64_t Value) {
|
|
|
|
startLine() << Label << ": " << Value << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void printNumber(StringRef Label, uint32_t Value) {
|
|
|
|
startLine() << Label << ": " << Value << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void printNumber(StringRef Label, uint16_t Value) {
|
|
|
|
startLine() << Label << ": " << Value << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void printNumber(StringRef Label, uint8_t Value) {
|
|
|
|
startLine() << Label << ": " << unsigned(Value) << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void printNumber(StringRef Label, int64_t Value) {
|
|
|
|
startLine() << Label << ": " << Value << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void printNumber(StringRef Label, int32_t Value) {
|
|
|
|
startLine() << Label << ": " << Value << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void printNumber(StringRef Label, int16_t Value) {
|
|
|
|
startLine() << Label << ": " << Value << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void printNumber(StringRef Label, int8_t Value) {
|
|
|
|
startLine() << Label << ": " << int(Value) << "\n";
|
|
|
|
}
|
|
|
|
|
2014-06-04 15:47:11 +00:00
|
|
|
void printBoolean(StringRef Label, bool Value) {
|
|
|
|
startLine() << Label << ": " << (Value ? "Yes" : "No") << '\n';
|
|
|
|
}
|
|
|
|
|
2014-01-30 04:46:33 +00:00
|
|
|
template <typename T_>
|
|
|
|
void printList(StringRef Label, const SmallVectorImpl<T_> &List) {
|
|
|
|
startLine() << Label << ": [";
|
|
|
|
bool Comma = false;
|
2014-04-20 21:05:30 +00:00
|
|
|
for (const auto &Item : List) {
|
2014-01-30 04:46:33 +00:00
|
|
|
if (Comma)
|
|
|
|
OS << ", ";
|
2014-04-20 21:05:30 +00:00
|
|
|
OS << Item;
|
2014-01-30 04:46:33 +00:00
|
|
|
Comma = true;
|
|
|
|
}
|
|
|
|
OS << "]\n";
|
|
|
|
}
|
|
|
|
|
2013-04-03 18:31:38 +00:00
|
|
|
template<typename T>
|
|
|
|
void printHex(StringRef Label, T Value) {
|
|
|
|
startLine() << Label << ": " << hex(Value) << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void printHex(StringRef Label, StringRef Str, T Value) {
|
|
|
|
startLine() << Label << ": " << Str << " (" << hex(Value) << ")\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void printString(StringRef Label, StringRef Value) {
|
|
|
|
startLine() << Label << ": " << Value << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void printString(StringRef Label, const std::string &Value) {
|
|
|
|
startLine() << Label << ": " << Value << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void printNumber(StringRef Label, StringRef Str, T Value) {
|
|
|
|
startLine() << Label << ": " << Str << " (" << Value << ")\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void printBinary(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value) {
|
|
|
|
printBinaryImpl(Label, Str, Value, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void printBinary(StringRef Label, StringRef Str, ArrayRef<char> Value) {
|
2014-08-27 05:25:25 +00:00
|
|
|
auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()),
|
|
|
|
Value.size());
|
2013-04-03 18:31:38 +00:00
|
|
|
printBinaryImpl(Label, Str, V, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void printBinary(StringRef Label, ArrayRef<uint8_t> Value) {
|
|
|
|
printBinaryImpl(Label, StringRef(), Value, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void printBinary(StringRef Label, ArrayRef<char> Value) {
|
2014-08-27 05:25:25 +00:00
|
|
|
auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()),
|
|
|
|
Value.size());
|
2013-04-03 18:31:38 +00:00
|
|
|
printBinaryImpl(Label, StringRef(), V, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void printBinary(StringRef Label, StringRef Value) {
|
2014-08-27 05:25:25 +00:00
|
|
|
auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()),
|
|
|
|
Value.size());
|
2013-04-03 18:31:38 +00:00
|
|
|
printBinaryImpl(Label, StringRef(), V, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void printBinaryBlock(StringRef Label, StringRef Value) {
|
2014-08-27 05:25:25 +00:00
|
|
|
auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()),
|
|
|
|
Value.size());
|
2013-04-03 18:31:38 +00:00
|
|
|
printBinaryImpl(Label, StringRef(), V, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
raw_ostream& startLine() {
|
|
|
|
printIndent();
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
raw_ostream& getOStream() {
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template<typename T>
|
|
|
|
static bool flagName(const EnumEntry<T>& lhs, const EnumEntry<T>& rhs) {
|
|
|
|
return lhs.Name < rhs.Name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void printBinaryImpl(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value,
|
|
|
|
bool Block);
|
|
|
|
|
|
|
|
raw_ostream &OS;
|
|
|
|
int IndentLevel;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DictScope {
|
|
|
|
DictScope(StreamWriter& W, StringRef N) : W(W) {
|
|
|
|
W.startLine() << N << " {\n";
|
|
|
|
W.indent();
|
|
|
|
}
|
|
|
|
|
|
|
|
~DictScope() {
|
|
|
|
W.unindent();
|
|
|
|
W.startLine() << "}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamWriter& W;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ListScope {
|
|
|
|
ListScope(StreamWriter& W, StringRef N) : W(W) {
|
|
|
|
W.startLine() << N << " [\n";
|
|
|
|
W.indent();
|
|
|
|
}
|
|
|
|
|
|
|
|
~ListScope() {
|
|
|
|
W.unindent();
|
|
|
|
W.startLine() << "]\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamWriter& W;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace llvm
|
|
|
|
|
|
|
|
#endif
|