Disasm.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include "Function.h"
  13. #include "Opcode.h"
  14. #include "PrimType.h"
  15. #include "Program.h"
  16. #include "clang/AST/DeclCXX.h"
  17. #include "llvm/Support/Compiler.h"
  18. using namespace clang;
  19. using namespace clang::interp;
  20. LLVM_DUMP_METHOD void Function::dump() const { dump(llvm::errs()); }
  21. LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
  22. if (F) {
  23. if (auto *Cons = dyn_cast<CXXConstructorDecl>(F)) {
  24. const std::string &Name = Cons->getParent()->getNameAsString();
  25. OS << Name << "::" << Name << ":\n";
  26. } else {
  27. OS << F->getNameAsString() << ":\n";
  28. }
  29. } else {
  30. OS << "<<expr>>\n";
  31. }
  32. OS << "frame size: " << getFrameSize() << "\n";
  33. OS << "arg size: " << getArgSize() << "\n";
  34. OS << "rvo: " << hasRVO() << "\n";
  35. auto PrintName = [&OS](const char *Name) {
  36. OS << Name;
  37. for (long I = 0, N = strlen(Name); I < 30 - N; ++I) {
  38. OS << ' ';
  39. }
  40. };
  41. for (CodePtr Start = getCodeBegin(), PC = Start; PC != getCodeEnd();) {
  42. size_t Addr = PC - Start;
  43. auto Op = PC.read<Opcode>();
  44. OS << llvm::format("%8d", Addr) << " ";
  45. switch (Op) {
  46. #define GET_DISASM
  47. #include "Opcodes.inc"
  48. #undef GET_DISASM
  49. }
  50. }
  51. }
  52. LLVM_DUMP_METHOD void Program::dump() const { dump(llvm::errs()); }
  53. LLVM_DUMP_METHOD void Program::dump(llvm::raw_ostream &OS) const {
  54. for (auto &Func : Funcs) {
  55. Func.second->dump();
  56. }
  57. for (auto &Anon : AnonFuncs) {
  58. Anon->dump();
  59. }
  60. }