xref: /freebsd-src/contrib/llvm-project/clang/lib/AST/Interp/Disasm.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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 
13*06c3fb27SDimitry Andric #include "Floating.h"
14a7dea167SDimitry Andric #include "Function.h"
15a7dea167SDimitry Andric #include "Opcode.h"
16a7dea167SDimitry Andric #include "PrimType.h"
17a7dea167SDimitry Andric #include "Program.h"
18a7dea167SDimitry Andric #include "clang/AST/DeclCXX.h"
19a7dea167SDimitry Andric #include "llvm/Support/Compiler.h"
205ffd83dbSDimitry Andric #include "llvm/Support/Format.h"
21a7dea167SDimitry Andric 
22a7dea167SDimitry Andric using namespace clang;
23a7dea167SDimitry Andric using namespace clang::interp;
24a7dea167SDimitry Andric 
25bdd1243dSDimitry Andric template <typename T> inline T ReadArg(Program &P, CodePtr &OpPC) {
26bdd1243dSDimitry Andric   if constexpr (std::is_pointer_v<T>) {
27349cc55cSDimitry Andric     uint32_t ID = OpPC.read<uint32_t>();
28349cc55cSDimitry Andric     return reinterpret_cast<T>(P.getNativePointer(ID));
29bdd1243dSDimitry Andric   } else {
30bdd1243dSDimitry Andric     return OpPC.read<T>();
31bdd1243dSDimitry Andric   }
32349cc55cSDimitry Andric }
33349cc55cSDimitry Andric 
34a7dea167SDimitry Andric LLVM_DUMP_METHOD void Function::dump() const { dump(llvm::errs()); }
35a7dea167SDimitry Andric 
36a7dea167SDimitry Andric LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
37*06c3fb27SDimitry Andric   OS << getName() << " " << (const void *)this << "\n";
38a7dea167SDimitry Andric   OS << "frame size: " << getFrameSize() << "\n";
39a7dea167SDimitry Andric   OS << "arg size:   " << getArgSize() << "\n";
40a7dea167SDimitry Andric   OS << "rvo:        " << hasRVO() << "\n";
41bdd1243dSDimitry Andric   OS << "this arg:   " << hasThisPointer() << "\n";
42a7dea167SDimitry Andric 
43a7dea167SDimitry Andric   auto PrintName = [&OS](const char *Name) {
44a7dea167SDimitry Andric     OS << Name;
45*06c3fb27SDimitry Andric     long N = 30 - strlen(Name);
46*06c3fb27SDimitry Andric     if (N > 0)
47*06c3fb27SDimitry Andric       OS.indent(N);
48a7dea167SDimitry Andric   };
49a7dea167SDimitry Andric 
50a7dea167SDimitry Andric   for (CodePtr Start = getCodeBegin(), PC = Start; PC != getCodeEnd();) {
51a7dea167SDimitry Andric     size_t Addr = PC - Start;
52a7dea167SDimitry Andric     auto Op = PC.read<Opcode>();
53a7dea167SDimitry Andric     OS << llvm::format("%8d", Addr) << " ";
54a7dea167SDimitry Andric     switch (Op) {
55a7dea167SDimitry Andric #define GET_DISASM
56a7dea167SDimitry Andric #include "Opcodes.inc"
57a7dea167SDimitry Andric #undef GET_DISASM
58a7dea167SDimitry Andric     }
59a7dea167SDimitry Andric   }
60a7dea167SDimitry Andric }
61a7dea167SDimitry Andric 
62a7dea167SDimitry Andric LLVM_DUMP_METHOD void Program::dump() const { dump(llvm::errs()); }
63a7dea167SDimitry Andric 
64a7dea167SDimitry Andric LLVM_DUMP_METHOD void Program::dump(llvm::raw_ostream &OS) const {
65bdd1243dSDimitry Andric   OS << ":: Program\n";
66bdd1243dSDimitry Andric   OS << "Global Variables: " << Globals.size() << "\n";
67bdd1243dSDimitry Andric   OS << "Functions: " << Funcs.size() << "\n";
68bdd1243dSDimitry Andric   OS << "\n";
69a7dea167SDimitry Andric   for (auto &Func : Funcs) {
70a7dea167SDimitry Andric     Func.second->dump();
71a7dea167SDimitry Andric   }
72a7dea167SDimitry Andric   for (auto &Anon : AnonFuncs) {
73a7dea167SDimitry Andric     Anon->dump();
74a7dea167SDimitry Andric   }
75a7dea167SDimitry Andric }
76