clang  14.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 "Function.h"
14 #include "Opcode.h"
15 #include "PrimType.h"
16 #include "Program.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/Format.h"
20 
21 using namespace clang;
22 using namespace clang::interp;
23 
24 template <typename T>
25 inline std::enable_if_t<!std::is_pointer<T>::value, T> ReadArg(Program &P,
26  CodePtr OpPC) {
27  return OpPC.read<T>();
28 }
29 
30 template <typename T>
31 inline std::enable_if_t<std::is_pointer<T>::value, T> ReadArg(Program &P,
32  CodePtr OpPC) {
33  uint32_t ID = OpPC.read<uint32_t>();
34  return reinterpret_cast<T>(P.getNativePointer(ID));
35 }
36 
37 LLVM_DUMP_METHOD void Function::dump() const { dump(llvm::errs()); }
38 
39 LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
40  if (F) {
41  if (auto *Cons = dyn_cast<CXXConstructorDecl>(F)) {
42  DeclarationName Name = Cons->getParent()->getDeclName();
43  OS << Name << "::" << Name << ":\n";
44  } else {
45  OS << F->getDeclName() << ":\n";
46  }
47  } else {
48  OS << "<<expr>>\n";
49  }
50 
51  OS << "frame size: " << getFrameSize() << "\n";
52  OS << "arg size: " << getArgSize() << "\n";
53  OS << "rvo: " << hasRVO() << "\n";
54 
55  auto PrintName = [&OS](const char *Name) {
56  OS << Name;
57  for (long I = 0, N = strlen(Name); I < 30 - N; ++I) {
58  OS << ' ';
59  }
60  };
61 
62  for (CodePtr Start = getCodeBegin(), PC = Start; PC != getCodeEnd();) {
63  size_t Addr = PC - Start;
64  auto Op = PC.read<Opcode>();
65  OS << llvm::format("%8d", Addr) << " ";
66  switch (Op) {
67 #define GET_DISASM
68 #include "Opcodes.inc"
69 #undef GET_DISASM
70  }
71  }
72 }
73 
74 LLVM_DUMP_METHOD void Program::dump() const { dump(llvm::errs()); }
75 
76 LLVM_DUMP_METHOD void Program::dump(llvm::raw_ostream &OS) const {
77  for (auto &Func : Funcs) {
78  Func.second->dump();
79  }
80  for (auto &Anon : AnonFuncs) {
81  Anon->dump();
82  }
83 }
clang::interp::Function::getCodeBegin
CodePtr getCodeBegin() const
Returns a pointer to the start of the code.
Definition: Function.cpp:24
clang::interp::Function::getArgSize
unsigned getArgSize() const
Returns the size of the argument stackx.
Definition: Function.h:66
Opcode.h
DeclCXX.h
clang::interp::Function::getCodeEnd
CodePtr getCodeEnd() const
Returns a pointer to the end of the code.
Definition: Function.cpp:26
clang::DeclarationName
The name of a declaration.
Definition: DeclarationName.h:145
clang::interp::Function::hasRVO
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition: Function.h:83
clang::interp
Definition: ASTContext.h:158
clang::interp::Opcode
Opcode
Definition: Opcode.h:21
PrimType.h
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::interp::CodePtr::read
std::enable_if_t<!std::is_pointer< T >::value, T > read()
Reads data and advances the pointer.
Definition: Source.h:47
clang::interp::CodePtr
Pointer into the code segment.
Definition: Source.h:25
clang::interp::Program::dump
void dump() const
Dumps the disassembled bytecode to llvm::errs().
Definition: Disasm.cpp:74
clang::interp::Function::dump
void dump() const
Dumps the disassembled bytecode to llvm::errs().
Definition: Disasm.cpp:37
clang::Builtin::ID
ID
Definition: Builtins.h:48
clang
Definition: CalledOnceCheck.h:17
Function.h
clang::NamedDecl::getDeclName
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition: Decl.h:313
Program.h
clang::interp::Program
The program contains and links the bytecode for all functions.
Definition: Program.h:43
clang::interp::Function::getFrameSize
unsigned getFrameSize() const
Returns the size of the function's local stack.
Definition: Function.h:64
clang::interp::ReadArg
std::enable_if_t<!std::is_pointer< T >::value, T > ReadArg(InterpState &S, CodePtr OpPC)
Definition: Interp.h:958