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 22 using namespace clang; 23 using namespace clang::interp; 24 25 template <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 34 LLVM_DUMP_METHOD void Function::dump() const { dump(llvm::errs()); } 35 36 LLVM_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 long N = 30 - strlen(Name); 46 if (N > 0) 47 OS.indent(N); 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 62 LLVM_DUMP_METHOD void Program::dump() const { dump(llvm::errs()); } 63 64 LLVM_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 } 76