1 //===--- Function.h - Bytecode function for the VM --------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Defines the Function class which holds all bytecode function-specific data.
11 // The scope class which describes local variables is also defined here.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_AST_INTERP_FUNCTION_H
16 #define LLVM_CLANG_AST_INTERP_FUNCTION_H
20 #include "clang/AST/Decl.h"
21 #include "llvm/Support/raw_ostream.h"
26 class ByteCodeEmitter;
27 enum PrimType : uint32_t;
29 /// Describes a scope block.
31 /// The block gathers all the descriptors of the locals defined in this block.
34 /// Information about a local's storage.
36 /// Offset of the local in frame.
38 /// Descriptor of the local.
42 using LocalVectorTy = llvm::SmallVector<Local, 8>;
44 Scope(LocalVectorTy &&Descriptors) : Descriptors(std::move(Descriptors)) {}
46 llvm::iterator_range<LocalVectorTy::iterator> locals() {
47 return llvm::make_range(Descriptors.begin(), Descriptors.end());
51 /// Object descriptors in this block.
52 LocalVectorTy Descriptors;
55 /// Bytecode function.
57 /// Contains links to the bytecode of the function, as well as metadata
58 /// describing all arguments and stack-local variables.
61 using ParamDescriptor = std::pair<PrimType, Descriptor *>;
63 /// Returns the size of the function's local stack.
64 unsigned getFrameSize() const { return FrameSize; }
65 /// Returns the size of the argument stackx
66 unsigned getArgSize() const { return ArgSize; }
68 /// Returns a pointer to the start of the code.
69 CodePtr getCodeBegin() const;
70 /// Returns a pointer to the end of the code.
71 CodePtr getCodeEnd() const;
73 /// Returns the original FunctionDecl.
74 const FunctionDecl *getDecl() const { return F; }
76 /// Returns the lcoation.
77 SourceLocation getLoc() const { return Loc; }
79 /// Returns a parameter descriptor.
80 ParamDescriptor getParamDescriptor(unsigned Offset) const;
82 /// Checks if the first argument is a RVO pointer.
83 bool hasRVO() const { return ParamTypes.size() != Params.size(); }
85 /// Range over the scope blocks.
86 llvm::iterator_range<llvm::SmallVector<Scope, 2>::iterator> scopes() {
87 return llvm::make_range(Scopes.begin(), Scopes.end());
90 /// Range over argument types.
91 using arg_reverse_iterator = SmallVectorImpl<PrimType>::reverse_iterator;
92 llvm::iterator_range<arg_reverse_iterator> args_reverse() {
93 return llvm::make_range(ParamTypes.rbegin(), ParamTypes.rend());
96 /// Returns a specific scope.
97 Scope &getScope(unsigned Idx) { return Scopes[Idx]; }
99 /// Returns the source information at a given PC.
100 SourceInfo getSource(CodePtr PC) const;
102 /// Checks if the function is valid to call in constexpr.
103 bool isConstexpr() const { return IsValid; }
105 /// Checks if the function is virtual.
106 bool isVirtual() const;
108 /// Checks if the function is a constructor.
109 bool isConstructor() const { return isa<CXXConstructorDecl>(F); }
112 /// Construct a function representing an actual function.
113 Function(Program &P, const FunctionDecl *F, unsigned ArgSize,
114 llvm::SmallVector<PrimType, 8> &&ParamTypes,
115 llvm::DenseMap<unsigned, ParamDescriptor> &&Params);
117 /// Sets the code of a function.
118 void setCode(unsigned NewFrameSize, std::vector<char> &&NewCode, SourceMap &&NewSrcMap,
119 llvm::SmallVector<Scope, 2> &&NewScopes) {
120 FrameSize = NewFrameSize;
121 Code = std::move(NewCode);
122 SrcMap = std::move(NewSrcMap);
123 Scopes = std::move(NewScopes);
128 friend class Program;
129 friend class ByteCodeEmitter;
131 /// Program reference.
133 /// Location of the executed code.
135 /// Declaration this function was compiled from.
136 const FunctionDecl *F;
137 /// Local area size: storage + metadata.
139 /// Size of the argument stack.
142 std::vector<char> Code;
143 /// Opcode-to-expression mapping.
145 /// List of block descriptors.
146 llvm::SmallVector<Scope, 2> Scopes;
147 /// List of argument types.
148 llvm::SmallVector<PrimType, 8> ParamTypes;
149 /// Map from byte offset to parameter descriptor.
150 llvm::DenseMap<unsigned, ParamDescriptor> Params;
151 /// Flag to indicate if the function is valid.
152 bool IsValid = false;
155 /// Dumps the disassembled bytecode to \c llvm::errs().
157 void dump(llvm::raw_ostream &OS) const;
160 } // namespace interp