17330f729Sjoerg //===--- Disasm.cpp - Disassembler for bytecode functions -------*- C++ -*-===// 27330f729Sjoerg // 37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information. 57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 67330f729Sjoerg // 77330f729Sjoerg //===----------------------------------------------------------------------===// 87330f729Sjoerg // 97330f729Sjoerg // Dump method for Function which disassembles the bytecode. 107330f729Sjoerg // 117330f729Sjoerg //===----------------------------------------------------------------------===// 127330f729Sjoerg 137330f729Sjoerg #include "Function.h" 147330f729Sjoerg #include "Opcode.h" 157330f729Sjoerg #include "PrimType.h" 167330f729Sjoerg #include "Program.h" 177330f729Sjoerg #include "clang/AST/DeclCXX.h" 187330f729Sjoerg #include "llvm/Support/Compiler.h" 19*e038c9c4Sjoerg #include "llvm/Support/Format.h" 207330f729Sjoerg 217330f729Sjoerg using namespace clang; 227330f729Sjoerg using namespace clang::interp; 237330f729Sjoerg dump() const247330f729SjoergLLVM_DUMP_METHOD void Function::dump() const { dump(llvm::errs()); } 257330f729Sjoerg dump(llvm::raw_ostream & OS) const267330f729SjoergLLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const { 277330f729Sjoerg if (F) { 287330f729Sjoerg if (auto *Cons = dyn_cast<CXXConstructorDecl>(F)) { 29*e038c9c4Sjoerg DeclarationName Name = Cons->getParent()->getDeclName(); 307330f729Sjoerg OS << Name << "::" << Name << ":\n"; 317330f729Sjoerg } else { 32*e038c9c4Sjoerg OS << F->getDeclName() << ":\n"; 337330f729Sjoerg } 347330f729Sjoerg } else { 357330f729Sjoerg OS << "<<expr>>\n"; 367330f729Sjoerg } 377330f729Sjoerg 387330f729Sjoerg OS << "frame size: " << getFrameSize() << "\n"; 397330f729Sjoerg OS << "arg size: " << getArgSize() << "\n"; 407330f729Sjoerg OS << "rvo: " << hasRVO() << "\n"; 417330f729Sjoerg 427330f729Sjoerg auto PrintName = [&OS](const char *Name) { 437330f729Sjoerg OS << Name; 447330f729Sjoerg for (long I = 0, N = strlen(Name); I < 30 - N; ++I) { 457330f729Sjoerg OS << ' '; 467330f729Sjoerg } 477330f729Sjoerg }; 487330f729Sjoerg 497330f729Sjoerg for (CodePtr Start = getCodeBegin(), PC = Start; PC != getCodeEnd();) { 507330f729Sjoerg size_t Addr = PC - Start; 517330f729Sjoerg auto Op = PC.read<Opcode>(); 527330f729Sjoerg OS << llvm::format("%8d", Addr) << " "; 537330f729Sjoerg switch (Op) { 547330f729Sjoerg #define GET_DISASM 557330f729Sjoerg #include "Opcodes.inc" 567330f729Sjoerg #undef GET_DISASM 577330f729Sjoerg } 587330f729Sjoerg } 597330f729Sjoerg } 607330f729Sjoerg dump() const617330f729SjoergLLVM_DUMP_METHOD void Program::dump() const { dump(llvm::errs()); } 627330f729Sjoerg dump(llvm::raw_ostream & OS) const637330f729SjoergLLVM_DUMP_METHOD void Program::dump(llvm::raw_ostream &OS) const { 647330f729Sjoerg for (auto &Func : Funcs) { 657330f729Sjoerg Func.second->dump(); 667330f729Sjoerg } 677330f729Sjoerg for (auto &Anon : AnonFuncs) { 687330f729Sjoerg Anon->dump(); 697330f729Sjoerg } 707330f729Sjoerg } 71