[clang][Interp][NFC] Add a helper function for local variables
authorTimm Bäder <tbaeder@redhat.com>
Mon, 26 Dec 2022 10:49:58 +0000 (11:49 +0100)
committerTimm Bäder <tbaeder@redhat.com>
Wed, 25 Jan 2023 15:57:59 +0000 (16:57 +0100)
... in EvalEmitter.

clang/lib/AST/Interp/EvalEmitter.cpp
clang/lib/AST/Interp/EvalEmitter.h

index 72fd3b45254b1d2ef1de3c3a345ebe3b75ee9020..6d89bc407aad8d0fe7af1185de5b3e5ceab6bb54 100644 (file)
@@ -208,9 +208,7 @@ bool EvalEmitter::emitGetPtrLocal(uint32_t I, const SourceInfo &Info) {
   if (!isActive())
     return true;
 
-  auto It = Locals.find(I);
-  assert(It != Locals.end() && "Missing local variable");
-  Block *B = reinterpret_cast<Block *>(It->second.get());
+  Block *B = getLocal(I);
   S.Stk.push<Pointer>(B, sizeof(InlineDescriptor));
   return true;
 }
@@ -222,9 +220,7 @@ bool EvalEmitter::emitGetLocal(uint32_t I, const SourceInfo &Info) {
 
   using T = typename PrimConv<OpType>::T;
 
-  auto It = Locals.find(I);
-  assert(It != Locals.end() && "Missing local variable");
-  auto *B = reinterpret_cast<Block *>(It->second.get());
+  Block *B = getLocal(I);
   S.Stk.push<T>(*reinterpret_cast<T *>(B->data()));
   return true;
 }
@@ -236,9 +232,7 @@ bool EvalEmitter::emitSetLocal(uint32_t I, const SourceInfo &Info) {
 
   using T = typename PrimConv<OpType>::T;
 
-  auto It = Locals.find(I);
-  assert(It != Locals.end() && "Missing local variable");
-  auto *B = reinterpret_cast<Block *>(It->second.get());
+  Block *B = getLocal(I);
   *reinterpret_cast<T *>(B->data()) = S.Stk.pop<T>();
   InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
   Desc.IsInitialized = true;
@@ -251,9 +245,8 @@ bool EvalEmitter::emitDestroy(uint32_t I, const SourceInfo &Info) {
     return true;
 
   for (auto &Local : Descriptors[I]) {
-    auto It = Locals.find(Local.Offset);
-    assert(It != Locals.end() && "Missing local variable");
-    S.deallocate(reinterpret_cast<Block *>(It->second.get()));
+    Block *B = getLocal(Local.Offset);
+    S.deallocate(B);
   }
 
   return true;
index 6b6d0d621901f6f5c1ec7e4772341845aae21218..22d7b7c68d10476d144deb3bec36f11ed6bdb19d 100644 (file)
@@ -92,6 +92,12 @@ private:
   /// Temporaries which require storage.
   llvm::DenseMap<unsigned, std::unique_ptr<char[]>> Locals;
 
+  Block *getLocal(unsigned Index) const {
+    auto It = Locals.find(Index);
+    assert(It != Locals.end() && "Missing local variable");
+    return reinterpret_cast<Block *>(It->second.get());
+  }
+
   // The emitter always tracks the current instruction and sets OpPC to a token
   // value which is mapped to the location of the opcode being evaluated.
   CodePtr OpPC;