Function.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //===--- Function.h - Bytecode function for the VM --------------*- 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. // Defines the Function class which holds all bytecode function-specific data.
  10. //
  11. // The scope class which describes local variables is also defined here.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CLANG_AST_INTERP_FUNCTION_H
  15. #define LLVM_CLANG_AST_INTERP_FUNCTION_H
  16. #include "Pointer.h"
  17. #include "Source.h"
  18. #include "clang/AST/Decl.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. namespace clang {
  21. namespace interp {
  22. class Program;
  23. class ByteCodeEmitter;
  24. enum PrimType : uint32_t;
  25. /// Describes a scope block.
  26. ///
  27. /// The block gathers all the descriptors of the locals defined in this block.
  28. class Scope {
  29. public:
  30. /// Information about a local's storage.
  31. struct Local {
  32. /// Offset of the local in frame.
  33. unsigned Offset;
  34. /// Descriptor of the local.
  35. Descriptor *Desc;
  36. };
  37. using LocalVectorTy = llvm::SmallVector<Local, 8>;
  38. Scope(LocalVectorTy &&Descriptors) : Descriptors(std::move(Descriptors)) {}
  39. llvm::iterator_range<LocalVectorTy::iterator> locals() {
  40. return llvm::make_range(Descriptors.begin(), Descriptors.end());
  41. }
  42. private:
  43. /// Object descriptors in this block.
  44. LocalVectorTy Descriptors;
  45. };
  46. /// Bytecode function.
  47. ///
  48. /// Contains links to the bytecode of the function, as well as metadata
  49. /// describing all arguments and stack-local variables.
  50. class Function {
  51. public:
  52. using ParamDescriptor = std::pair<PrimType, Descriptor *>;
  53. /// Returns the size of the function's local stack.
  54. unsigned getFrameSize() const { return FrameSize; }
  55. /// Returns the size of the argument stackx
  56. unsigned getArgSize() const { return ArgSize; }
  57. /// Returns a pointer to the start of the code.
  58. CodePtr getCodeBegin() const;
  59. /// Returns a pointer to the end of the code.
  60. CodePtr getCodeEnd() const;
  61. /// Returns the original FunctionDecl.
  62. const FunctionDecl *getDecl() const { return F; }
  63. /// Returns the location.
  64. SourceLocation getLoc() const { return Loc; }
  65. /// Returns a parameter descriptor.
  66. ParamDescriptor getParamDescriptor(unsigned Offset) const;
  67. /// Checks if the first argument is a RVO pointer.
  68. bool hasRVO() const { return ParamTypes.size() != Params.size(); }
  69. /// Range over the scope blocks.
  70. llvm::iterator_range<llvm::SmallVector<Scope, 2>::iterator> scopes() {
  71. return llvm::make_range(Scopes.begin(), Scopes.end());
  72. }
  73. /// Range over argument types.
  74. using arg_reverse_iterator = SmallVectorImpl<PrimType>::reverse_iterator;
  75. llvm::iterator_range<arg_reverse_iterator> args_reverse() {
  76. return llvm::make_range(ParamTypes.rbegin(), ParamTypes.rend());
  77. }
  78. /// Returns a specific scope.
  79. Scope &getScope(unsigned Idx) { return Scopes[Idx]; }
  80. /// Returns the source information at a given PC.
  81. SourceInfo getSource(CodePtr PC) const;
  82. /// Checks if the function is valid to call in constexpr.
  83. bool isConstexpr() const { return IsValid; }
  84. /// Checks if the function is virtual.
  85. bool isVirtual() const;
  86. /// Checks if the function is a constructor.
  87. bool isConstructor() const { return isa<CXXConstructorDecl>(F); }
  88. private:
  89. /// Construct a function representing an actual function.
  90. Function(Program &P, const FunctionDecl *F, unsigned ArgSize,
  91. llvm::SmallVector<PrimType, 8> &&ParamTypes,
  92. llvm::DenseMap<unsigned, ParamDescriptor> &&Params);
  93. /// Sets the code of a function.
  94. void setCode(unsigned NewFrameSize, std::vector<char> &&NewCode, SourceMap &&NewSrcMap,
  95. llvm::SmallVector<Scope, 2> &&NewScopes) {
  96. FrameSize = NewFrameSize;
  97. Code = std::move(NewCode);
  98. SrcMap = std::move(NewSrcMap);
  99. Scopes = std::move(NewScopes);
  100. IsValid = true;
  101. }
  102. private:
  103. friend class Program;
  104. friend class ByteCodeEmitter;
  105. /// Program reference.
  106. Program &P;
  107. /// Location of the executed code.
  108. SourceLocation Loc;
  109. /// Declaration this function was compiled from.
  110. const FunctionDecl *F;
  111. /// Local area size: storage + metadata.
  112. unsigned FrameSize;
  113. /// Size of the argument stack.
  114. unsigned ArgSize;
  115. /// Program code.
  116. std::vector<char> Code;
  117. /// Opcode-to-expression mapping.
  118. SourceMap SrcMap;
  119. /// List of block descriptors.
  120. llvm::SmallVector<Scope, 2> Scopes;
  121. /// List of argument types.
  122. llvm::SmallVector<PrimType, 8> ParamTypes;
  123. /// Map from byte offset to parameter descriptor.
  124. llvm::DenseMap<unsigned, ParamDescriptor> Params;
  125. /// Flag to indicate if the function is valid.
  126. bool IsValid = false;
  127. public:
  128. /// Dumps the disassembled bytecode to \c llvm::errs().
  129. void dump() const;
  130. void dump(llvm::raw_ostream &OS) const;
  131. };
  132. } // namespace interp
  133. } // namespace clang
  134. #endif