xref: /freebsd-src/contrib/llvm-project/clang/lib/AST/Interp/Disasm.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
1a7dea167SDimitry Andric //===--- Disasm.cpp - Disassembler for bytecode functions -------*- C++ -*-===//
2a7dea167SDimitry Andric //
3a7dea167SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a7dea167SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5a7dea167SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a7dea167SDimitry Andric //
7a7dea167SDimitry Andric //===----------------------------------------------------------------------===//
8a7dea167SDimitry Andric //
9a7dea167SDimitry Andric // Dump method for Function which disassembles the bytecode.
10a7dea167SDimitry Andric //
11a7dea167SDimitry Andric //===----------------------------------------------------------------------===//
12a7dea167SDimitry Andric 
13a7dea167SDimitry Andric #include "Function.h"
14a7dea167SDimitry Andric #include "Opcode.h"
15a7dea167SDimitry Andric #include "PrimType.h"
16a7dea167SDimitry Andric #include "Program.h"
17a7dea167SDimitry Andric #include "clang/AST/DeclCXX.h"
18a7dea167SDimitry Andric #include "llvm/Support/Compiler.h"
19*5ffd83dbSDimitry Andric #include "llvm/Support/Format.h"
20a7dea167SDimitry Andric 
21a7dea167SDimitry Andric using namespace clang;
22a7dea167SDimitry Andric using namespace clang::interp;
23a7dea167SDimitry Andric 
24a7dea167SDimitry Andric LLVM_DUMP_METHOD void Function::dump() const { dump(llvm::errs()); }
25a7dea167SDimitry Andric 
26a7dea167SDimitry Andric LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
27a7dea167SDimitry Andric   if (F) {
28a7dea167SDimitry Andric     if (auto *Cons = dyn_cast<CXXConstructorDecl>(F)) {
29a7dea167SDimitry Andric       const std::string &Name = Cons->getParent()->getNameAsString();
30a7dea167SDimitry Andric       OS << Name << "::" << Name << ":\n";
31a7dea167SDimitry Andric     } else {
32a7dea167SDimitry Andric       OS << F->getNameAsString() << ":\n";
33a7dea167SDimitry Andric     }
34a7dea167SDimitry Andric   } else {
35a7dea167SDimitry Andric     OS << "<<expr>>\n";
36a7dea167SDimitry Andric   }
37a7dea167SDimitry Andric 
38a7dea167SDimitry Andric   OS << "frame size: " << getFrameSize() << "\n";
39a7dea167SDimitry Andric   OS << "arg size:   " << getArgSize() << "\n";
40a7dea167SDimitry Andric   OS << "rvo:        " << hasRVO() << "\n";
41a7dea167SDimitry Andric 
42a7dea167SDimitry Andric   auto PrintName = [&OS](const char *Name) {
43a7dea167SDimitry Andric     OS << Name;
44a7dea167SDimitry Andric     for (long I = 0, N = strlen(Name); I < 30 - N; ++I) {
45a7dea167SDimitry Andric       OS << ' ';
46a7dea167SDimitry Andric     }
47a7dea167SDimitry Andric   };
48a7dea167SDimitry Andric 
49a7dea167SDimitry Andric   for (CodePtr Start = getCodeBegin(), PC = Start; PC != getCodeEnd();) {
50a7dea167SDimitry Andric     size_t Addr = PC - Start;
51a7dea167SDimitry Andric     auto Op = PC.read<Opcode>();
52a7dea167SDimitry Andric     OS << llvm::format("%8d", Addr) << " ";
53a7dea167SDimitry Andric     switch (Op) {
54a7dea167SDimitry Andric #define GET_DISASM
55a7dea167SDimitry Andric #include "Opcodes.inc"
56a7dea167SDimitry Andric #undef GET_DISASM
57a7dea167SDimitry Andric     }
58a7dea167SDimitry Andric   }
59a7dea167SDimitry Andric }
60a7dea167SDimitry Andric 
61a7dea167SDimitry Andric LLVM_DUMP_METHOD void Program::dump() const { dump(llvm::errs()); }
62a7dea167SDimitry Andric 
63a7dea167SDimitry Andric LLVM_DUMP_METHOD void Program::dump(llvm::raw_ostream &OS) const {
64a7dea167SDimitry Andric   for (auto &Func : Funcs) {
65a7dea167SDimitry Andric     Func.second->dump();
66a7dea167SDimitry Andric   }
67a7dea167SDimitry Andric   for (auto &Anon : AnonFuncs) {
68a7dea167SDimitry Andric     Anon->dump();
69a7dea167SDimitry Andric   }
70a7dea167SDimitry Andric }
71