clang 20.0.0git
Disasm.cpp
Go to the documentation of this file.
1//===--- Disasm.cpp - Disassembler for bytecode functions -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Dump method for Function which disassembles the bytecode.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Boolean.h"
14#include "Context.h"
15#include "EvaluationResult.h"
16#include "Floating.h"
17#include "Function.h"
18#include "FunctionPointer.h"
19#include "Integral.h"
20#include "IntegralAP.h"
21#include "InterpFrame.h"
22#include "MemberPointer.h"
23#include "Opcode.h"
24#include "PrimType.h"
25#include "Program.h"
27#include "clang/AST/DeclCXX.h"
28#include "clang/AST/ExprCXX.h"
29#include "llvm/Support/Compiler.h"
30#include "llvm/Support/Format.h"
31
32using namespace clang;
33using namespace clang::interp;
34
35template <typename T> inline T ReadArg(Program &P, CodePtr &OpPC) {
36 if constexpr (std::is_pointer_v<T>) {
37 uint32_t ID = OpPC.read<uint32_t>();
38 return reinterpret_cast<T>(P.getNativePointer(ID));
39 } else {
40 return OpPC.read<T>();
41 }
42}
43
44template <> inline Floating ReadArg<Floating>(Program &P, CodePtr &OpPC) {
46 OpPC += align(F.bytesToSerialize());
47 return F;
48}
49
50template <>
51inline IntegralAP<false> ReadArg<IntegralAP<false>>(Program &P, CodePtr &OpPC) {
53 OpPC += align(I.bytesToSerialize());
54 return I;
55}
56
57template <>
58inline IntegralAP<true> ReadArg<IntegralAP<true>>(Program &P, CodePtr &OpPC) {
60 OpPC += align(I.bytesToSerialize());
61 return I;
62}
63
64LLVM_DUMP_METHOD void Function::dump() const { dump(llvm::errs()); }
65
66LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
67 {
68 ColorScope SC(OS, true, {llvm::raw_ostream::BRIGHT_GREEN, true});
69 OS << getName() << " " << (const void *)this << "\n";
70 }
71 OS << "frame size: " << getFrameSize() << "\n";
72 OS << "arg size: " << getArgSize() << "\n";
73 OS << "rvo: " << hasRVO() << "\n";
74 OS << "this arg: " << hasThisPointer() << "\n";
75
76 auto PrintName = [&OS](const char *Name) {
77 OS << Name;
78 long N = 30 - strlen(Name);
79 if (N > 0)
80 OS.indent(N);
81 };
82
83 for (CodePtr Start = getCodeBegin(), PC = Start; PC != getCodeEnd();) {
84 size_t Addr = PC - Start;
85 auto Op = PC.read<Opcode>();
86 OS << llvm::format("%8d", Addr) << " ";
87 switch (Op) {
88#define GET_DISASM
89#include "Opcodes.inc"
90#undef GET_DISASM
91 }
92 }
93}
94
95LLVM_DUMP_METHOD void Program::dump() const { dump(llvm::errs()); }
96
97static const char *primTypeToString(PrimType T) {
98 switch (T) {
99 case PT_Sint8:
100 return "Sint8";
101 case PT_Uint8:
102 return "Uint8";
103 case PT_Sint16:
104 return "Sint16";
105 case PT_Uint16:
106 return "Uint16";
107 case PT_Sint32:
108 return "Sint32";
109 case PT_Uint32:
110 return "Uint32";
111 case PT_Sint64:
112 return "Sint64";
113 case PT_Uint64:
114 return "Uint64";
115 case PT_IntAP:
116 return "IntAP";
117 case PT_IntAPS:
118 return "IntAPS";
119 case PT_Bool:
120 return "Bool";
121 case PT_Float:
122 return "Float";
123 case PT_Ptr:
124 return "Ptr";
125 case PT_FnPtr:
126 return "FnPtr";
127 case PT_MemberPtr:
128 return "MemberPtr";
129 }
130 llvm_unreachable("Unhandled PrimType");
131}
132
133LLVM_DUMP_METHOD void Program::dump(llvm::raw_ostream &OS) const {
134 {
135 ColorScope SC(OS, true, {llvm::raw_ostream::BRIGHT_RED, true});
136 OS << "\n:: Program\n";
137 }
138
139 {
140 ColorScope SC(OS, true, {llvm::raw_ostream::WHITE, true});
141 OS << "Total memory : " << Allocator.getTotalMemory() << " bytes\n";
142 OS << "Global Variables: " << Globals.size() << "\n";
143 }
144 unsigned GI = 0;
145 for (const Global *G : Globals) {
146 const Descriptor *Desc = G->block()->getDescriptor();
147 Pointer GP = getPtrGlobal(GI);
148
149 OS << GI << ": " << (const void *)G->block() << " ";
150 {
151 ColorScope SC(OS, true,
152 GP.isInitialized()
153 ? TerminalColor{llvm::raw_ostream::GREEN, false}
154 : TerminalColor{llvm::raw_ostream::RED, false});
155 OS << (GP.isInitialized() ? "initialized " : "uninitialized ");
156 }
157 Desc->dump(OS);
158
159 if (GP.isInitialized() && Desc->IsTemporary) {
160 if (const auto *MTE =
161 dyn_cast_if_present<MaterializeTemporaryExpr>(Desc->asExpr());
162 MTE && MTE->getLifetimeExtendedTemporaryDecl()) {
163 if (const APValue *V =
164 MTE->getLifetimeExtendedTemporaryDecl()->getValue()) {
165 OS << " (global temporary value: ";
166 {
167 ColorScope SC(OS, true, {llvm::raw_ostream::BRIGHT_MAGENTA, true});
168 std::string VStr;
169 llvm::raw_string_ostream SS(VStr);
170 V->dump(SS, Ctx.getASTContext());
171
172 for (unsigned I = 0; I != VStr.size(); ++I) {
173 if (VStr[I] == '\n')
174 VStr[I] = ' ';
175 }
176 VStr.pop_back(); // Remove the newline (or now space) at the end.
177 OS << VStr;
178 }
179 OS << ')';
180 }
181 }
182 }
183
184 OS << "\n";
185 if (GP.isInitialized() && Desc->isPrimitive() && !Desc->isDummy()) {
186 OS << " ";
187 {
188 ColorScope SC(OS, true, {llvm::raw_ostream::BRIGHT_CYAN, false});
189 OS << primTypeToString(Desc->getPrimType()) << " ";
190 }
191 TYPE_SWITCH(Desc->getPrimType(), { GP.deref<T>().print(OS); });
192 OS << "\n";
193 }
194 ++GI;
195 }
196
197 {
198 ColorScope SC(OS, true, {llvm::raw_ostream::WHITE, true});
199 OS << "Functions: " << Funcs.size() << "\n";
200 }
201 for (const auto &Func : Funcs) {
202 Func.second->dump();
203 }
204 for (const auto &Anon : AnonFuncs) {
205 Anon->dump();
206 }
207}
208
209LLVM_DUMP_METHOD void Descriptor::dump() const {
210 dump(llvm::errs());
211 llvm::errs() << '\n';
212}
213
214LLVM_DUMP_METHOD void Descriptor::dump(llvm::raw_ostream &OS) const {
215 // Source
216 {
217 ColorScope SC(OS, true, {llvm::raw_ostream::BLUE, true});
218 if (const auto *ND = dyn_cast_if_present<NamedDecl>(asDecl()))
219 ND->printQualifiedName(OS);
220 else if (asExpr())
221 OS << "Expr " << (const void *)asExpr();
222 }
223
224 // Print a few interesting bits about the descriptor.
225 if (isPrimitiveArray())
226 OS << " primitive-array";
227 else if (isCompositeArray())
228 OS << " composite-array";
229 else if (isUnion())
230 OS << " union";
231 else if (isRecord())
232 OS << " record";
233 else if (isPrimitive())
234 OS << " primitive";
235
236 if (isZeroSizeArray())
237 OS << " zero-size-array";
238 else if (isUnknownSizeArray())
239 OS << " unknown-size-array";
240
241 if (isDummy())
242 OS << " dummy";
243}
244
245LLVM_DUMP_METHOD void InlineDescriptor::dump(llvm::raw_ostream &OS) const {
246 {
247 ColorScope SC(OS, true, {llvm::raw_ostream::BLUE, true});
248 OS << "InlineDescriptor " << (const void *)this << "\n";
249 }
250 OS << "Offset: " << Offset << "\n";
251 OS << "IsConst: " << IsConst << "\n";
252 OS << "IsInitialized: " << IsInitialized << "\n";
253 OS << "IsBase: " << IsBase << "\n";
254 OS << "IsActive: " << IsActive << "\n";
255 OS << "InUnion: " << InUnion << "\n";
256 OS << "IsFieldMutable: " << IsFieldMutable << "\n";
257 OS << "Desc: ";
258 if (Desc)
259 Desc->dump(OS);
260 else
261 OS << "nullptr";
262 OS << "\n";
263}
264
265LLVM_DUMP_METHOD void InterpFrame::dump(llvm::raw_ostream &OS,
266 unsigned Indent) const {
267 unsigned Spaces = Indent * 2;
268 {
269 ColorScope SC(OS, true, {llvm::raw_ostream::BLUE, true});
270 OS.indent(Spaces);
271 if (getCallee())
272 describe(OS);
273 else
274 OS << "Frame (Depth: " << getDepth() << ")";
275 OS << "\n";
276 }
277 OS.indent(Spaces) << "Function: " << getFunction();
278 if (const Function *F = getFunction()) {
279 OS << " (" << F->getName() << ")";
280 }
281 OS << "\n";
282 OS.indent(Spaces) << "This: " << getThis() << "\n";
283 OS.indent(Spaces) << "RVO: " << getRVOPtr() << "\n";
284 OS.indent(Spaces) << "Depth: " << Depth << "\n";
285 OS.indent(Spaces) << "ArgSize: " << ArgSize << "\n";
286 OS.indent(Spaces) << "Args: " << (void *)Args << "\n";
287 OS.indent(Spaces) << "FrameOffset: " << FrameOffset << "\n";
288 OS.indent(Spaces) << "FrameSize: " << (Func ? Func->getFrameSize() : 0)
289 << "\n";
290
291 for (const InterpFrame *F = this->Caller; F; F = F->Caller) {
292 F->dump(OS, Indent + 1);
293 }
294}
295
296LLVM_DUMP_METHOD void Record::dump(llvm::raw_ostream &OS, unsigned Indentation,
297 unsigned Offset) const {
298 unsigned Indent = Indentation * 2;
299 OS.indent(Indent);
300 {
301 ColorScope SC(OS, true, {llvm::raw_ostream::BLUE, true});
302 OS << getName() << "\n";
303 }
304
305 unsigned I = 0;
306 for (const Record::Base &B : bases()) {
307 OS.indent(Indent) << "- Base " << I << ". Offset " << (Offset + B.Offset)
308 << "\n";
309 B.R->dump(OS, Indentation + 1, Offset + B.Offset);
310 ++I;
311 }
312
313 I = 0;
314 for (const Record::Field &F : fields()) {
315 OS.indent(Indent) << "- Field " << I << ": ";
316 {
317 ColorScope SC(OS, true, {llvm::raw_ostream::BRIGHT_RED, true});
318 OS << F.Decl->getName();
319 }
320 OS << ". Offset " << (Offset + F.Offset) << "\n";
321 ++I;
322 }
323
324 I = 0;
325 for (const Record::Base &B : virtual_bases()) {
326 OS.indent(Indent) << "- Virtual Base " << I << ". Offset "
327 << (Offset + B.Offset) << "\n";
328 B.R->dump(OS, Indentation + 1, Offset + B.Offset);
329 ++I;
330 }
331}
332
333LLVM_DUMP_METHOD void Block::dump(llvm::raw_ostream &OS) const {
334 {
335 ColorScope SC(OS, true, {llvm::raw_ostream::BRIGHT_BLUE, true});
336 OS << "Block " << (const void *)this;
337 }
338 OS << " (";
339 Desc->dump(OS);
340 OS << ")\n";
341 unsigned NPointers = 0;
342 for (const Pointer *P = Pointers; P; P = P->Next) {
343 ++NPointers;
344 }
345 OS << " Pointers: " << NPointers << "\n";
346 OS << " Dead: " << IsDead << "\n";
347 OS << " Static: " << IsStatic << "\n";
348 OS << " Extern: " << IsExtern << "\n";
349 OS << " Initialized: " << IsInitialized << "\n";
350}
351
352LLVM_DUMP_METHOD void EvaluationResult::dump() const {
353 assert(Ctx);
354 auto &OS = llvm::errs();
355 const ASTContext &ASTCtx = Ctx->getASTContext();
356
357 switch (Kind) {
358 case Empty:
359 OS << "Empty\n";
360 break;
361 case RValue:
362 OS << "RValue: ";
363 std::get<APValue>(Value).dump(OS, ASTCtx);
364 break;
365 case LValue: {
366 assert(Source);
367 QualType SourceType;
368 if (const auto *D = Source.dyn_cast<const Decl *>()) {
369 if (const auto *VD = dyn_cast<ValueDecl>(D))
370 SourceType = VD->getType();
371 } else if (const auto *E = Source.dyn_cast<const Expr *>()) {
372 SourceType = E->getType();
373 }
374
375 OS << "LValue: ";
376 if (const auto *P = std::get_if<Pointer>(&Value))
377 P->toAPValue(ASTCtx).printPretty(OS, ASTCtx, SourceType);
378 else if (const auto *FP = std::get_if<FunctionPointer>(&Value)) // Nope
379 FP->toAPValue(ASTCtx).printPretty(OS, ASTCtx, SourceType);
380 OS << "\n";
381 break;
382 }
383 case Invalid:
384 OS << "Invalid\n";
385 break;
386 case Valid:
387 OS << "Valid\n";
388 break;
389 }
390}
#define V(N, I)
Definition: ASTContext.h:3341
StringRef P
const Decl * D
Expr * E
static void dump(llvm::raw_ostream &OS, StringRef FunctionName, ArrayRef< CounterExpression > Expressions, ArrayRef< CounterMappingRegion > Regions)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
static const char * primTypeToString(PrimType T)
Definition: Disasm.cpp:97
Floating ReadArg< Floating >(Program &P, CodePtr &OpPC)
Definition: Disasm.cpp:44
T ReadArg(Program &P, CodePtr &OpPC)
Definition: Disasm.cpp:35
Defines the clang::Expr interface and subclasses for C++ expressions.
#define TYPE_SWITCH(Expr, B)
Definition: PrimType.h:148
static std::string getName(const CallEvent &Call)
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:187
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
This represents one expression.
Definition: Expr.h:110
QualType getType() const
Definition: Expr.h:142
A (possibly-)qualified type.
Definition: Type.h:941
void dump() const
Definition: InterpBlock.h:128
Pointer into the code segment.
Definition: Source.h:30
std::enable_if_t<!std::is_pointer< T >::value, T > read()
Reads data and advances the pointer.
Definition: Source.h:60
ASTContext & getASTContext() const
Returns the AST context.
Definition: Context.h:61
void dump() const
Dump to stderr.
Definition: Disasm.cpp:352
static Floating deserialize(const std::byte *Buff)
Definition: Floating.h:153
size_t bytesToSerialize() const
Definition: Floating.h:139
Bytecode function.
Definition: Function.h:81
unsigned getFrameSize() const
Returns the size of the function's local stack.
Definition: Function.h:86
static IntegralAP< Signed > deserialize(const std::byte *Buff)
Definition: IntegralAP.h:284
size_t bytesToSerialize() const
Definition: IntegralAP.h:270
Frame storing local variables.
Definition: InterpFrame.h:26
InterpFrame * Caller
The frame of the previous function.
Definition: InterpFrame.h:29
const Pointer & getThis() const
Returns the 'this' pointer.
Definition: InterpFrame.h:101
const Function * getFunction() const
Returns the current function.
Definition: InterpFrame.h:65
unsigned getDepth() const
Definition: InterpFrame.h:121
const Pointer & getRVOPtr() const
Returns the RVO pointer, if the Function has one.
Definition: InterpFrame.h:104
const FunctionDecl * getCallee() const override
Returns the caller.
void describe(llvm::raw_ostream &OS) const override
Describes the frame with arguments for diagnostic purposes.
A pointer to a memory block, live or dead.
Definition: Pointer.h:82
bool isInitialized() const
Checks if an object was initialized.
Definition: Pointer.cpp:311
The program contains and links the bytecode for all functions.
Definition: Program.h:39
Pointer getPtrGlobal(unsigned Idx) const
Returns a pointer to a global.
Definition: Program.cpp:104
void dump() const
Dumps the disassembled bytecode to llvm::errs().
Definition: Disasm.cpp:95
const std::string getName() const
Returns the name of the underlying declaration.
Definition: Record.cpp:31
llvm::iterator_range< const_virtual_iter > virtual_bases() const
Definition: Record.h:96
llvm::iterator_range< const_base_iter > bases() const
Definition: Record.h:85
llvm::iterator_range< const_field_iter > fields() const
Definition: Record.h:77
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:126
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:33
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
Describes a memory block created by an allocation site.
Definition: Descriptor.h:111
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition: Descriptor.h:251
bool isCompositeArray() const
Checks if the descriptor is of an array of composites.
Definition: Descriptor.h:244
const Decl * asDecl() const
Definition: Descriptor.h:198
bool isDummy() const
Checks if this is a dummy descriptor.
Definition: Descriptor.h:260
bool isUnknownSizeArray() const
Checks if the descriptor is of an array of unknown size.
Definition: Descriptor.h:248
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition: Descriptor.h:242
bool isZeroSizeArray() const
Checks if the descriptor is of an array of zero size.
Definition: Descriptor.h:246
PrimType getPrimType() const
Definition: Descriptor.h:224
bool isRecord() const
Checks if the descriptor is of a record.
Definition: Descriptor.h:256
const bool IsTemporary
Flag indicating if the block is a temporary.
Definition: Descriptor.h:153
bool isUnion() const
Checks if the descriptor is of a union.
Definition: Descriptor.cpp:421
const Expr * asExpr() const
Definition: Descriptor.h:199
unsigned IsActive
Flag indicating if the field is the active member of a union.
Definition: Descriptor.h:91
unsigned IsBase
Flag indicating if the field is an embedded base class.
Definition: Descriptor.h:85
const Descriptor * Desc
Definition: Descriptor.h:99
unsigned InUnion
Flat indicating if this field is in a union (even if nested).
Definition: Descriptor.h:93
unsigned Offset
Offset inside the structure/array.
Definition: Descriptor.h:71
unsigned IsInitialized
For primitive fields, it indicates if the field was initialized.
Definition: Descriptor.h:82
unsigned IsConst
Flag indicating if the storage is constant or not.
Definition: Descriptor.h:76
unsigned IsFieldMutable
Flag indicating if the field is mutable (if in a record).
Definition: Descriptor.h:97