clang  11.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 LLVM_DUMP_METHOD void Function::dump() const { dump(llvm::errs()); }
25 
26 LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
27  if (F) {
28  if (auto *Cons = dyn_cast<CXXConstructorDecl>(F)) {
29  const std::string &Name = Cons->getParent()->getNameAsString();
30  OS << Name << "::" << Name << ":\n";
31  } else {
32  OS << F->getNameAsString() << ":\n";
33  }
34  } else {
35  OS << "<<expr>>\n";
36  }
37 
38  OS << "frame size: " << getFrameSize() << "\n";
39  OS << "arg size: " << getArgSize() << "\n";
40  OS << "rvo: " << hasRVO() << "\n";
41 
42  auto PrintName = [&OS](const char *Name) {
43  OS << Name;
44  for (long I = 0, N = strlen(Name); I < 30 - N; ++I) {
45  OS << ' ';
46  }
47  };
48 
49  for (CodePtr Start = getCodeBegin(), PC = Start; PC != getCodeEnd();) {
50  size_t Addr = PC - Start;
51  auto Op = PC.read<Opcode>();
52  OS << llvm::format("%8d", Addr) << " ";
53  switch (Op) {
54 #define GET_DISASM
55 #include "Opcodes.inc"
56 #undef GET_DISASM
57  }
58  }
59 }
60 
61 LLVM_DUMP_METHOD void Program::dump() const { dump(llvm::errs()); }
62 
63 LLVM_DUMP_METHOD void Program::dump(llvm::raw_ostream &OS) const {
64  for (auto &Func : Funcs) {
65  Func.second->dump();
66  }
67  for (auto &Anon : AnonFuncs) {
68  Anon->dump();
69  }
70 }
Pointer into the code segment.
Definition: Source.h:25
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition: Function.h:83
CodePtr getCodeEnd() const
Returns a pointer to the end of the code.
Definition: Function.cpp:26
void dump() const
Dumps the disassembled bytecode to llvm::errs().
Definition: Disasm.cpp:24
CodePtr getCodeBegin() const
Returns a pointer to the start of the code.
Definition: Function.cpp:24
std::string getNameAsString() const
Get a human-readable name for the declaration, even if it is one of the special kinds of names (C++ c...
Definition: Decl.h:266
unsigned getFrameSize() const
Returns the size of the function&#39;s local stack.
Definition: Function.h:64
Dataflow Directional Tag Classes.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
unsigned getArgSize() const
Returns the size of the argument stackx.
Definition: Function.h:66
void dump() const
Dumps the disassembled bytecode to llvm::errs().
Definition: Disasm.cpp:61