clang 17.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 "Floating.h"
14#include "Function.h"
15#include "Opcode.h"
16#include "PrimType.h"
17#include "Program.h"
18#include "clang/AST/DeclCXX.h"
19#include "llvm/Support/Compiler.h"
20#include "llvm/Support/Format.h"
21
22using namespace clang;
23using namespace clang::interp;
24
25template <typename T> inline T ReadArg(Program &P, CodePtr &OpPC) {
26 if constexpr (std::is_pointer_v<T>) {
27 uint32_t ID = OpPC.read<uint32_t>();
28 return reinterpret_cast<T>(P.getNativePointer(ID));
29 } else {
30 return OpPC.read<T>();
31 }
32}
33
34LLVM_DUMP_METHOD void Function::dump() const { dump(llvm::errs()); }
35
36LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
37 OS << getName() << " " << (const void *)this << "\n";
38 OS << "frame size: " << getFrameSize() << "\n";
39 OS << "arg size: " << getArgSize() << "\n";
40 OS << "rvo: " << hasRVO() << "\n";
41 OS << "this arg: " << hasThisPointer() << "\n";
42
43 auto PrintName = [&OS](const char *Name) {
44 OS << Name;
45 for (long I = 0, N = strlen(Name); I < 30 - N; ++I) {
46 OS << ' ';
47 }
48 };
49
50 for (CodePtr Start = getCodeBegin(), PC = Start; PC != getCodeEnd();) {
51 size_t Addr = PC - Start;
52 auto Op = PC.read<Opcode>();
53 OS << llvm::format("%8d", Addr) << " ";
54 switch (Op) {
55#define GET_DISASM
56#include "Opcodes.inc"
57#undef GET_DISASM
58 }
59 }
60}
61
62LLVM_DUMP_METHOD void Program::dump() const { dump(llvm::errs()); }
63
64LLVM_DUMP_METHOD void Program::dump(llvm::raw_ostream &OS) const {
65 OS << ":: Program\n";
66 OS << "Global Variables: " << Globals.size() << "\n";
67 OS << "Functions: " << Funcs.size() << "\n";
68 OS << "\n";
69 for (auto &Func : Funcs) {
70 Func.second->dump();
71 }
72 for (auto &Anon : AnonFuncs) {
73 Anon->dump();
74 }
75}
StringRef P
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....
T ReadArg(Program &P, CodePtr &OpPC)
Definition: Disasm.cpp:25
llvm::raw_ostream & OS
Definition: Logger.cpp:24
static std::string getName(const CallEvent &Call)
Pointer into the code segment.
Definition: Source.h:26
std::enable_if_t<!std::is_pointer< T >::value, T > read()
Reads data and advances the pointer.
Definition: Source.h:50
The program contains and links the bytecode for all functions.
Definition: Program.h:40
void dump() const
Dumps the disassembled bytecode to llvm::errs().
Definition: Disasm.cpp:62