Disasm.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #include "llvm/Support/Format.h"
  19. using namespace clang;
  20. using namespace clang::interp;
  21. template <typename T>
  22. inline std::enable_if_t<!std::is_pointer<T>::value, T> ReadArg(Program &P,
  23. CodePtr OpPC) {
  24. return OpPC.read<T>();
  25. }
  26. template <typename T>
  27. inline std::enable_if_t<std::is_pointer<T>::value, T> ReadArg(Program &P,
  28. CodePtr OpPC) {
  29. uint32_t ID = OpPC.read<uint32_t>();
  30. return reinterpret_cast<T>(P.getNativePointer(ID));
  31. }
  32. LLVM_DUMP_METHOD void Function::dump() const { dump(llvm::errs()); }
  33. LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
  34. if (F) {
  35. if (auto *Cons = dyn_cast<CXXConstructorDecl>(F)) {
  36. DeclarationName Name = Cons->getParent()->getDeclName();
  37. OS << Name << "::" << Name << ":\n";
  38. } else {
  39. OS << F->getDeclName() << ":\n";
  40. }
  41. } else {
  42. OS << "<<expr>>\n";
  43. }
  44. OS << "frame size: " << getFrameSize() << "\n";
  45. OS << "arg size: " << getArgSize() << "\n";
  46. OS << "rvo: " << hasRVO() << "\n";
  47. auto PrintName = [&OS](const char *Name) {
  48. OS << Name;
  49. for (long I = 0, N = strlen(Name); I < 30 - N; ++I) {
  50. OS << ' ';
  51. }
  52. };
  53. for (CodePtr Start = getCodeBegin(), PC = Start; PC != getCodeEnd();) {
  54. size_t Addr = PC - Start;
  55. auto Op = PC.read<Opcode>();
  56. OS << llvm::format("%8d", Addr) << " ";
  57. switch (Op) {
  58. #define GET_DISASM
  59. #include "Opcodes.inc"
  60. #undef GET_DISASM
  61. }
  62. }
  63. }
  64. LLVM_DUMP_METHOD void Program::dump() const { dump(llvm::errs()); }
  65. LLVM_DUMP_METHOD void Program::dump(llvm::raw_ostream &OS) const {
  66. for (auto &Func : Funcs) {
  67. Func.second->dump();
  68. }
  69. for (auto &Anon : AnonFuncs) {
  70. Anon->dump();
  71. }
  72. }