1//===- PDBExtras.cpp - helper functions and classes for PDBs -----*- C++-*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/DebugInfo/PDB/PDBExtras.h"
11
12#include "llvm/ADT/ArrayRef.h"
13
14using namespace llvm;
15
16#define CASE_OUTPUT_ENUM_CLASS_STR(Class, Value, Str, Stream)                  \
17  case Class::Value:                                                           \
18    Stream << Str;                                                             \
19    break;
20
21#define CASE_OUTPUT_ENUM_CLASS_NAME(Class, Value, Stream)                      \
22  CASE_OUTPUT_ENUM_CLASS_STR(Class, Value, #Value, Stream)
23
24raw_ostream &llvm::operator<<(raw_ostream &OS, const PDB_VariantType &Type) {
25  switch (Type) {
26    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_VariantType, Bool, OS)
27    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_VariantType, Single, OS)
28    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_VariantType, Double, OS)
29    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_VariantType, Int8, OS)
30    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_VariantType, Int16, OS)
31    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_VariantType, Int32, OS)
32    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_VariantType, Int64, OS)
33    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_VariantType, UInt8, OS)
34    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_VariantType, UInt16, OS)
35    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_VariantType, UInt32, OS)
36    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_VariantType, UInt64, OS)
37    default:
38      OS << "Unknown";
39  }
40  return OS;
41}
42
43raw_ostream &llvm::operator<<(raw_ostream &OS, const PDB_CallingConv &Conv) {
44  OS << "__";
45  switch (Conv) {
46    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, NearCdecl, "cdecl", OS)
47    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, FarCdecl, "cdecl", OS)
48    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, NearPascal, "pascal", OS)
49    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, FarPascal, "pascal", OS)
50    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, NearFastcall, "fastcall", OS)
51    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, FarFastcall, "fastcall", OS)
52    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, Skipped, "skippedcall", OS)
53    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, NearStdcall, "stdcall", OS)
54    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, FarStdcall, "stdcall", OS)
55    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, NearSyscall, "syscall", OS)
56    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, FarSyscall, "syscall", OS)
57    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, Thiscall, "thiscall", OS)
58    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, MipsCall, "mipscall", OS)
59    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, Generic, "genericcall", OS)
60    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, Alphacall, "alphacall", OS)
61    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, Ppccall, "ppccall", OS)
62    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, SuperHCall, "superhcall", OS)
63    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, Armcall, "armcall", OS)
64    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, AM33call, "am33call", OS)
65    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, Tricall, "tricall", OS)
66    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, Sh5call, "sh5call", OS)
67    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, M32R, "m32rcall", OS)
68    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, Clrcall, "clrcall", OS)
69    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, Inline, "inlinecall", OS)
70    CASE_OUTPUT_ENUM_CLASS_STR(PDB_CallingConv, NearVectorcall, "vectorcall",
71                               OS)
72  default:
73    OS << "unknowncall";
74  }
75  return OS;
76}
77
78raw_ostream &llvm::operator<<(raw_ostream &OS, const PDB_DataKind &Data) {
79  switch (Data) {
80    CASE_OUTPUT_ENUM_CLASS_STR(PDB_DataKind, Unknown, "unknown", OS)
81    CASE_OUTPUT_ENUM_CLASS_STR(PDB_DataKind, Local, "local", OS)
82    CASE_OUTPUT_ENUM_CLASS_STR(PDB_DataKind, StaticLocal, "static local", OS)
83    CASE_OUTPUT_ENUM_CLASS_STR(PDB_DataKind, Param, "param", OS)
84    CASE_OUTPUT_ENUM_CLASS_STR(PDB_DataKind, ObjectPtr, "this ptr", OS)
85    CASE_OUTPUT_ENUM_CLASS_STR(PDB_DataKind, FileStatic, "static global", OS)
86    CASE_OUTPUT_ENUM_CLASS_STR(PDB_DataKind, Global, "global", OS)
87    CASE_OUTPUT_ENUM_CLASS_STR(PDB_DataKind, Member, "member", OS)
88    CASE_OUTPUT_ENUM_CLASS_STR(PDB_DataKind, StaticMember, "static member", OS)
89    CASE_OUTPUT_ENUM_CLASS_STR(PDB_DataKind, Constant, "const", OS)
90  }
91  return OS;
92}
93
94raw_ostream &llvm::operator<<(raw_ostream &OS, const PDB_RegisterId &Reg) {
95  switch (Reg) {
96    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, AL, OS)
97    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, CL, OS)
98    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, DL, OS)
99    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, BL, OS)
100    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, AH, OS)
101    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, CH, OS)
102    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, DH, OS)
103    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, BH, OS)
104    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, AX, OS)
105    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, CX, OS)
106    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, DX, OS)
107    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, BX, OS)
108    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, SP, OS)
109    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, BP, OS)
110    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, SI, OS)
111    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, DI, OS)
112    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, EAX, OS)
113    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, ECX, OS)
114    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, EDX, OS)
115    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, EBX, OS)
116    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, ESP, OS)
117    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, EBP, OS)
118    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, ESI, OS)
119    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, EDI, OS)
120    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, ES, OS)
121    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, CS, OS)
122    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, SS, OS)
123    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, DS, OS)
124    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, FS, OS)
125    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, GS, OS)
126    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, IP, OS)
127    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, RAX, OS)
128    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, RBX, OS)
129    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, RCX, OS)
130    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, RDX, OS)
131    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, RSI, OS)
132    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, RDI, OS)
133    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, RBP, OS)
134    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, RSP, OS)
135    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, R8, OS)
136    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, R9, OS)
137    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, R10, OS)
138    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, R11, OS)
139    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, R12, OS)
140    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, R13, OS)
141    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, R14, OS)
142    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_RegisterId, R15, OS)
143  default:
144    OS << static_cast<int>(Reg);
145  }
146  return OS;
147}
148
149raw_ostream &llvm::operator<<(raw_ostream &OS, const PDB_LocType &Loc) {
150  switch (Loc) {
151    CASE_OUTPUT_ENUM_CLASS_STR(PDB_LocType, Static, "static", OS)
152    CASE_OUTPUT_ENUM_CLASS_STR(PDB_LocType, TLS, "tls", OS)
153    CASE_OUTPUT_ENUM_CLASS_STR(PDB_LocType, RegRel, "regrel", OS)
154    CASE_OUTPUT_ENUM_CLASS_STR(PDB_LocType, ThisRel, "thisrel", OS)
155    CASE_OUTPUT_ENUM_CLASS_STR(PDB_LocType, Enregistered, "register", OS)
156    CASE_OUTPUT_ENUM_CLASS_STR(PDB_LocType, BitField, "bitfield", OS)
157    CASE_OUTPUT_ENUM_CLASS_STR(PDB_LocType, Slot, "slot", OS)
158    CASE_OUTPUT_ENUM_CLASS_STR(PDB_LocType, IlRel, "IL rel", OS)
159    CASE_OUTPUT_ENUM_CLASS_STR(PDB_LocType, MetaData, "metadata", OS)
160    CASE_OUTPUT_ENUM_CLASS_STR(PDB_LocType, Constant, "constant", OS)
161  default:
162    OS << "Unknown";
163  }
164  return OS;
165}
166
167raw_ostream &llvm::operator<<(raw_ostream &OS, const PDB_ThunkOrdinal &Thunk) {
168  switch (Thunk) {
169    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_ThunkOrdinal, BranchIsland, OS)
170    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_ThunkOrdinal, Pcode, OS)
171    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_ThunkOrdinal, Standard, OS)
172    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_ThunkOrdinal, ThisAdjustor, OS)
173    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_ThunkOrdinal, TrampIncremental, OS)
174    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_ThunkOrdinal, UnknownLoad, OS)
175    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_ThunkOrdinal, Vcall, OS)
176  }
177  return OS;
178}
179
180raw_ostream &llvm::operator<<(raw_ostream &OS, const PDB_Checksum &Checksum) {
181  switch (Checksum) {
182    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Checksum, None, OS)
183    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Checksum, MD5, OS)
184    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Checksum, SHA1, OS)
185  }
186  return OS;
187}
188
189raw_ostream &llvm::operator<<(raw_ostream &OS, const PDB_Lang &Lang) {
190  switch (Lang) {
191    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, C, OS)
192    CASE_OUTPUT_ENUM_CLASS_STR(PDB_Lang, Cpp, "C++", OS)
193    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, Fortran, OS)
194    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, Masm, OS)
195    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, Pascal, OS)
196    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, Basic, OS)
197    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, Cobol, OS)
198    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, Link, OS)
199    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, Cvtres, OS)
200    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, Cvtpgd, OS)
201    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, CSharp, OS)
202    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, VB, OS)
203    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, ILAsm, OS)
204    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, Java, OS)
205    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, JScript, OS)
206    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, MSIL, OS)
207    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_Lang, HLSL, OS)
208  }
209  return OS;
210}
211
212raw_ostream &llvm::operator<<(raw_ostream &OS, const PDB_SymType &Tag) {
213  switch (Tag) {
214    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, Exe, OS)
215    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, Compiland, OS)
216    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, CompilandDetails, OS)
217    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, CompilandEnv, OS)
218    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, Function, OS)
219    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, Block, OS)
220    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, Data, OS)
221    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, Annotation, OS)
222    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, Label, OS)
223    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, PublicSymbol, OS)
224    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, UDT, OS)
225    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, Enum, OS)
226    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, FunctionSig, OS)
227    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, PointerType, OS)
228    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, ArrayType, OS)
229    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, BuiltinType, OS)
230    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, Typedef, OS)
231    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, BaseClass, OS)
232    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, Friend, OS)
233    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, FunctionArg, OS)
234    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, FuncDebugStart, OS)
235    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, FuncDebugEnd, OS)
236    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, UsingNamespace, OS)
237    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, VTableShape, OS)
238    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, VTable, OS)
239    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, Custom, OS)
240    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, Thunk, OS)
241    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, CustomType, OS)
242    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, ManagedType, OS)
243    CASE_OUTPUT_ENUM_CLASS_NAME(PDB_SymType, Dimension, OS)
244  default:
245    OS << "Unknown";
246  }
247  return OS;
248}
249
250raw_ostream &llvm::operator<<(raw_ostream &OS, const PDB_MemberAccess &Access) {
251  switch (Access) {
252    CASE_OUTPUT_ENUM_CLASS_STR(PDB_MemberAccess, Public, "public", OS)
253    CASE_OUTPUT_ENUM_CLASS_STR(PDB_MemberAccess, Protected, "protected", OS)
254    CASE_OUTPUT_ENUM_CLASS_STR(PDB_MemberAccess, Private, "private", OS)
255  }
256  return OS;
257}
258
259raw_ostream &llvm::operator<<(raw_ostream &OS, const PDB_UdtType &Type) {
260  switch (Type) {
261    CASE_OUTPUT_ENUM_CLASS_STR(PDB_UdtType, Class, "class", OS)
262    CASE_OUTPUT_ENUM_CLASS_STR(PDB_UdtType, Struct, "struct", OS)
263    CASE_OUTPUT_ENUM_CLASS_STR(PDB_UdtType, Interface, "interface", OS)
264    CASE_OUTPUT_ENUM_CLASS_STR(PDB_UdtType, Union, "union", OS)
265  }
266  return OS;
267}
268
269raw_ostream &llvm::operator<<(raw_ostream &OS, const PDB_UniqueId &Id) {
270  static const char *Lookup = "0123456789ABCDEF";
271
272  static_assert(sizeof(PDB_UniqueId) == 16, "Expected 16-byte GUID");
273  ArrayRef<uint8_t> GuidBytes(reinterpret_cast<const uint8_t*>(&Id), 16);
274  OS << "{";
275  for (int i=0; i < 16;) {
276    uint8_t Byte = GuidBytes[i];
277    uint8_t HighNibble = (Byte >> 4) & 0xF;
278    uint8_t LowNibble = Byte & 0xF;
279    OS << Lookup[HighNibble] << Lookup[LowNibble];
280    ++i;
281    if (i>=4 && i<=10 && i%2==0)
282      OS << "-";
283  }
284  OS << "}";
285  return OS;
286}
287
288raw_ostream &llvm::operator<<(raw_ostream &OS, const Variant &Value) {
289  switch (Value.Type) {
290    case PDB_VariantType::Bool:
291      OS << (Value.Bool ? "true" : "false");
292      break;
293    case PDB_VariantType::Double:
294      OS << Value.Double;
295      break;
296    case PDB_VariantType::Int16:
297      OS << Value.Int16;
298      break;
299    case PDB_VariantType::Int32:
300      OS << Value.Int32;
301      break;
302    case PDB_VariantType::Int64:
303      OS << Value.Int64;
304      break;
305    case PDB_VariantType::Int8:
306      OS << static_cast<int>(Value.Int8);
307      break;
308    case PDB_VariantType::Single:
309      OS << Value.Single;
310      break;
311    case PDB_VariantType::UInt16:
312      OS << Value.Double;
313      break;
314    case PDB_VariantType::UInt32:
315      OS << Value.UInt32;
316      break;
317    case PDB_VariantType::UInt64:
318      OS << Value.UInt64;
319      break;
320    case PDB_VariantType::UInt8:
321      OS << static_cast<unsigned>(Value.UInt8);
322      break;
323    default:
324      OS << Value.Type;
325  }
326  return OS;
327}
328
329raw_ostream &llvm::operator<<(raw_ostream &OS, const VersionInfo &Version) {
330  OS << Version.Major << "." << Version.Minor << "." << Version.Build;
331  return OS;
332}
333
334raw_ostream &llvm::operator<<(raw_ostream &OS, const TagStats &Stats) {
335  for (auto Tag : Stats) {
336    OS << Tag.first << ":" << Tag.second << " ";
337  }
338  return OS;
339}
340