clang  10.0.0svn
InterpFrame.cpp
Go to the documentation of this file.
1 //===--- InterpFrame.cpp - Call Frame implementation 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 #include "InterpFrame.h"
10 #include "Function.h"
11 #include "Interp.h"
12 #include "InterpStack.h"
13 #include "PrimType.h"
14 #include "Program.h"
15 #include "clang/AST/DeclCXX.h"
16 
17 using namespace clang;
18 using namespace clang::interp;
19 
21  CodePtr RetPC, Pointer &&This)
22  : Caller(Caller), S(S), Func(Func), This(std::move(This)), RetPC(RetPC),
23  ArgSize(Func ? Func->getArgSize() : 0),
24  Args(static_cast<char *>(S.Stk.top())), FrameOffset(S.Stk.size()) {
25  if (Func) {
26  if (unsigned FrameSize = Func->getFrameSize()) {
27  Locals = std::make_unique<char[]>(FrameSize);
28  for (auto &Scope : Func->scopes()) {
29  for (auto &Local : Scope.locals()) {
30  Block *B = new (localBlock(Local.Offset)) Block(Local.Desc);
31  B->invokeCtor();
32  }
33  }
34  }
35  }
36 }
37 
39  if (Func && Func->isConstructor() && This.isBaseClass())
40  This.initialize();
41  for (auto &Param : Params)
42  S.deallocate(reinterpret_cast<Block *>(Param.second.get()));
43 }
44 
45 void InterpFrame::destroy(unsigned Idx) {
46  for (auto &Local : Func->getScope(Idx).locals()) {
47  S.deallocate(reinterpret_cast<Block *>(localBlock(Local.Offset)));
48  }
49 }
50 
52  for (PrimType Ty : Func->args_reverse())
53  TYPE_SWITCH(Ty, S.Stk.discard<T>());
54 }
55 
56 template <typename T>
57 static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType) {
58  OS << V;
59 }
60 
61 template <>
62 void print(llvm::raw_ostream &OS, const Pointer &P, ASTContext &Ctx,
63  QualType Ty) {
64  if (P.isZero()) {
65  OS << "nullptr";
66  return;
67  }
68 
69  auto printDesc = [&OS, &Ctx](Descriptor *Desc) {
70  if (auto *D = Desc->asDecl()) {
71  // Subfields or named values.
72  if (auto *VD = dyn_cast<ValueDecl>(D)) {
73  OS << *VD;
74  return;
75  }
76  // Base classes.
77  if (isa<RecordDecl>(D)) {
78  return;
79  }
80  }
81  // Temporary expression.
82  if (auto *E = Desc->asExpr()) {
83  E->printPretty(OS, nullptr, Ctx.getPrintingPolicy());
84  return;
85  }
86  llvm_unreachable("Invalid descriptor type");
87  };
88 
89  if (!Ty->isReferenceType())
90  OS << "&";
92  for (Pointer F = P; !F.isRoot(); ) {
93  Levels.push_back(F);
94  F = F.isArrayElement() ? F.getArray().expand() : F.getBase();
95  }
96 
97  printDesc(P.getDeclDesc());
98  for (auto It = Levels.rbegin(); It != Levels.rend(); ++It) {
99  if (It->inArray()) {
100  OS << "[" << It->expand().getIndex() << "]";
101  continue;
102  }
103  if (auto Index = It->getIndex()) {
104  OS << " + " << Index;
105  continue;
106  }
107  OS << ".";
108  printDesc(It->getFieldDesc());
109  }
110 }
111 
112 void InterpFrame::describe(llvm::raw_ostream &OS) {
113  const FunctionDecl *F = getCallee();
114  auto *M = dyn_cast<CXXMethodDecl>(F);
115  if (M && M->isInstance() && !isa<CXXConstructorDecl>(F)) {
116  print(OS, This, S.getCtx(), S.getCtx().getRecordType(M->getParent()));
117  OS << "->";
118  }
119  OS << *F << "(";
120  unsigned Off = Func->hasRVO() ? primSize(PT_Ptr) : 0;
121  for (unsigned I = 0, N = F->getNumParams(); I < N; ++I) {
122  QualType Ty = F->getParamDecl(I)->getType();
123 
124  PrimType PrimTy;
125  if (llvm::Optional<PrimType> T = S.Ctx.classify(Ty)) {
126  PrimTy = *T;
127  } else {
128  PrimTy = PT_Ptr;
129  }
130 
131  TYPE_SWITCH(PrimTy, print(OS, stackRef<T>(Off), S.getCtx(), Ty));
132  Off += align(primSize(PrimTy));
133  if (I + 1 != N)
134  OS << ", ";
135  }
136  OS << ")";
137 }
138 
140  if (Caller->Caller)
141  return Caller;
142  return S.getSplitFrame();
143 }
144 
146  if (!Caller->Func)
147  return S.getLocation(nullptr, {});
148  return S.getLocation(Caller->Func, RetPC - sizeof(uintptr_t));
149 }
150 
152  return Func->getDecl();
153 }
154 
156  assert(Offset < Func->getFrameSize() && "Invalid local offset.");
157  return Pointer(
158  reinterpret_cast<Block *>(Locals.get() + Offset - sizeof(Block)));
159 }
160 
162  // Return the block if it was created previously.
163  auto Pt = Params.find(Off);
164  if (Pt != Params.end()) {
165  return Pointer(reinterpret_cast<Block *>(Pt->second.get()));
166  }
167 
168  // Allocate memory to store the parameter and the block metadata.
169  const auto &Desc = Func->getParamDescriptor(Off);
170  size_t BlockSize = sizeof(Block) + Desc.second->getAllocSize();
171  auto Memory = std::make_unique<char[]>(BlockSize);
172  auto *B = new (Memory.get()) Block(Desc.second);
173 
174  // Copy the initial value.
175  TYPE_SWITCH(Desc.first, new (B->data()) T(stackRef<T>(Off)));
176 
177  // Record the param.
178  Params.insert({Off, std::move(Memory)});
179  return Pointer(B);
180 }
181 
183  return S.getSource(Func, PC);
184 }
185 
187  return S.getExpr(Func, PC);
188 }
189 
191  return S.getLocation(Func, PC);
192 }
193 
llvm::iterator_range< llvm::SmallVector< Scope, 2 >::iterator > scopes()
Range over the scope blocks.
Definition: Function.h:86
Represents a function declaration or definition.
Definition: Decl.h:1784
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:17
A (possibly-)qualified type.
Definition: Type.h:643
Pointer getParamPointer(unsigned Offset)
Returns a pointer to an argument - lazily creates a block.
void initialize() const
Initializes a field.
Definition: Pointer.cpp:154
Pointer into the code segment.
Definition: Source.h:25
SourceLocation getLocation(CodePtr PC) const
Frame storing local variables.
Definition: InterpFrame.h:29
StringRef P
Pointer getLocalPointer(unsigned Offset)
Returns a pointer to a local variables.
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:57
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition: Function.h:74
bool This(InterpState &S, CodePtr OpPC)
Definition: Interp.h:830
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition: Function.h:83
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:81
llvm::Optional< PrimType > classify(QualType T)
Classifies an expression.
Definition: Context.cpp:68
A pointer to a memory block, live or dead.
Definition: Pointer.h:39
InterpFrame * Caller
The frame of the previous function.
Definition: InterpFrame.h:32
Scope & getScope(unsigned Idx)
Returns a specific scope.
Definition: Function.h:97
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:160
bool isConstructor() const
Checks if the function is a constructor.
Definition: Function.h:109
ParamDescriptor getParamDescriptor(unsigned Offset) const
Returns a parameter descriptor.
Definition: Function.cpp:28
Definition: Format.h:2392
InterpStack & Stk
Temporary stack.
Definition: InterpState.h:100
A memory block, either on the stack or in the heap.
Definition: Block.h:37
bool isReferenceType() const
Definition: Type.h:6403
void destroy(unsigned Idx)
Invokes the destructors for a scope.
Definition: InterpFrame.cpp:45
ASTContext & getCtx() const override
Definition: InterpState.h:53
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:27
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:670
bool isRoot() const
Pointer points directly to a block.
Definition: Pointer.h:216
Context & Ctx
Interpreter Context.
Definition: InterpState.h:102
Describes a memory block created by an allocation site.
Definition: Descriptor.h:51
void popArgs()
Pops the arguments off the stack.
Definition: InterpFrame.cpp:51
unsigned Offset
Definition: Format.cpp:1809
This represents one expression.
Definition: Expr.h:108
#define V(N, I)
Definition: ASTContext.h:2921
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Definition: opencl-c-base.h:62
void invokeCtor()
Invokes the constructor.
Definition: Block.h:71
virtual SourceInfo getSource(CodePtr PC) const
Map a location to a source.
~InterpFrame()
Destroys the frame, killing all live pointers to stack slots.
Definition: InterpFrame.cpp:38
QualType getRecordType(const RecordDecl *Decl) const
Base class for stack frames, shared between VM and walker.
Definition: Frame.h:25
Encodes a location in the source.
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1905
Bytecode function.
Definition: Function.h:59
unsigned getFrameSize() const
Returns the size of the function&#39;s local stack.
Definition: Function.h:64
bool isBaseClass() const
Checks if a structure is a base class.
Definition: Pointer.h:244
const Expr * getExpr(CodePtr PC) const
InterpFrame(InterpState &S, Function *Func, InterpFrame *Caller, CodePtr RetPC, Pointer &&This)
Creates a new frame for a method call.
Definition: InterpFrame.cpp:20
SourceInfo getSource(Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Definition: InterpState.h:84
Dataflow Directional Tag Classes.
const FunctionDecl * getCallee() const
Returns the caller.
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:57
Interpreter context.
Definition: InterpState.h:34
llvm::iterator_range< LocalVectorTy::iterator > locals()
Definition: Function.h:46
bool isZero() const
Checks if the pointer is null.
Definition: Pointer.h:140
Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition: Pointer.h:147
Frame * getCaller() const
Returns the parent frame object.
const Expr * getExpr(Function *F, CodePtr PC) const
Returns the expression if an opcode belongs to one, null otherwise.
Definition: Source.cpp:31
#define TYPE_SWITCH(Expr, B)
Definition: PrimType.h:85
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
SourceLocation getCallLocation() const
Returns the location of the call to the frame.
void describe(llvm::raw_ostream &OS)
Describes the frame with arguments for diagnostic purposes.
SourceLocation getLocation(Function *F, CodePtr PC) const
Returns the location from which an opcode originates.
Definition: Source.cpp:37
Describes a scope block.
Definition: Function.h:32
static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType)
Definition: InterpFrame.cpp:57
void discard()
Discards the top value from the stack.
Definition: InterpStack.h:44
llvm::iterator_range< arg_reverse_iterator > args_reverse()
Definition: Function.h:92