tbaeder updated this revision to Diff 460654.
tbaeder marked 2 inline comments as done.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133941/new/

https://reviews.llvm.org/D133941

Files:
  clang/lib/AST/Interp/InterpStack.h


Index: clang/lib/AST/Interp/InterpStack.h
===================================================================
--- clang/lib/AST/Interp/InterpStack.h
+++ clang/lib/AST/Interp/InterpStack.h
@@ -13,7 +13,9 @@
 #ifndef LLVM_CLANG_AST_INTERP_INTERPSTACK_H
 #define LLVM_CLANG_AST_INTERP_INTERPSTACK_H
 
+#include "PrimType.h"
 #include <memory>
+#include <vector>
 
 namespace clang {
 namespace interp {
@@ -29,10 +31,17 @@
   /// Constructs a value in place on the top of the stack.
   template <typename T, typename... Tys> void push(Tys &&... Args) {
     new (grow(aligned_size<T>())) T(std::forward<Tys>(Args)...);
+#ifndef NDEBUG
+    ItemTypes.push_back(toPrimType<T>());
+#endif
   }
 
   /// Returns the value from the top of the stack and removes it.
   template <typename T> T pop() {
+#ifndef NDEBUG
+    assert(ItemTypes.back() == toPrimType<T>());
+    ItemTypes.pop_back();
+#endif
     auto *Ptr = &peek<T>();
     auto Value = std::move(*Ptr);
     Ptr->~T();
@@ -42,6 +51,10 @@
 
   /// Discards the top value from the stack.
   template <typename T> void discard() {
+#ifndef NDEBUG
+    assert(ItemTypes.back() == toPrimType<T>());
+    ItemTypes.pop_back();
+#endif
     auto *Ptr = &peek<T>();
     Ptr->~T();
     shrink(aligned_size<T>());
@@ -111,6 +124,45 @@
   StackChunk *Chunk = nullptr;
   /// Total size of the stack.
   size_t StackSize = 0;
+
+#ifndef NDEBUG
+  /// vector recording the type of data we pushed into the stack.
+  std::vector<PrimType> ItemTypes;
+
+  template <typename T> static constexpr PrimType toPrimType() {
+    if constexpr (std::is_same<T, Pointer>::value)
+      return PT_Ptr;
+    else if constexpr (std::is_same<T, bool>::value ||
+                       std::is_same<T, Boolean>::value)
+      return PT_Bool;
+    else if constexpr (std::is_same<T, int8_t>::value ||
+                       std::is_same<T, Integral<8, true>>::value)
+      return PT_Sint8;
+    else if constexpr (std::is_same<T, uint8_t>::value ||
+                       std::is_same<T, Integral<8, false>>::value)
+      return PT_Uint8;
+    else if constexpr (std::is_same<T, int16_t>::value ||
+                       std::is_same<T, Integral<16, true>>::value)
+      return PT_Sint16;
+    else if constexpr (std::is_same<T, uint16_t>::value ||
+                       std::is_same<T, Integral<16, false>>::value)
+      return PT_Uint16;
+    else if constexpr (std::is_same<T, int32_t>::value ||
+                       std::is_same<T, Integral<32, true>>::value)
+      return PT_Sint32;
+    else if constexpr (std::is_same<T, uint32_t>::value ||
+                       std::is_same<T, Integral<32, false>>::value)
+      return PT_Uint32;
+    else if constexpr (std::is_same<T, int64_t>::value ||
+                       std::is_same<T, Integral<64, true>>::value)
+      return PT_Sint64;
+    else if constexpr (std::is_same<T, uint64_t>::value ||
+                       std::is_same<T, Integral<64, false>>::value)
+      return PT_Uint64;
+
+    llvm_unreachable("unknown type push()'ed into InterpStack");
+  }
+#endif
 };
 
 } // namespace interp


Index: clang/lib/AST/Interp/InterpStack.h
===================================================================
--- clang/lib/AST/Interp/InterpStack.h
+++ clang/lib/AST/Interp/InterpStack.h
@@ -13,7 +13,9 @@
 #ifndef LLVM_CLANG_AST_INTERP_INTERPSTACK_H
 #define LLVM_CLANG_AST_INTERP_INTERPSTACK_H
 
+#include "PrimType.h"
 #include <memory>
+#include <vector>
 
 namespace clang {
 namespace interp {
@@ -29,10 +31,17 @@
   /// Constructs a value in place on the top of the stack.
   template <typename T, typename... Tys> void push(Tys &&... Args) {
     new (grow(aligned_size<T>())) T(std::forward<Tys>(Args)...);
+#ifndef NDEBUG
+    ItemTypes.push_back(toPrimType<T>());
+#endif
   }
 
   /// Returns the value from the top of the stack and removes it.
   template <typename T> T pop() {
+#ifndef NDEBUG
+    assert(ItemTypes.back() == toPrimType<T>());
+    ItemTypes.pop_back();
+#endif
     auto *Ptr = &peek<T>();
     auto Value = std::move(*Ptr);
     Ptr->~T();
@@ -42,6 +51,10 @@
 
   /// Discards the top value from the stack.
   template <typename T> void discard() {
+#ifndef NDEBUG
+    assert(ItemTypes.back() == toPrimType<T>());
+    ItemTypes.pop_back();
+#endif
     auto *Ptr = &peek<T>();
     Ptr->~T();
     shrink(aligned_size<T>());
@@ -111,6 +124,45 @@
   StackChunk *Chunk = nullptr;
   /// Total size of the stack.
   size_t StackSize = 0;
+
+#ifndef NDEBUG
+  /// vector recording the type of data we pushed into the stack.
+  std::vector<PrimType> ItemTypes;
+
+  template <typename T> static constexpr PrimType toPrimType() {
+    if constexpr (std::is_same<T, Pointer>::value)
+      return PT_Ptr;
+    else if constexpr (std::is_same<T, bool>::value ||
+                       std::is_same<T, Boolean>::value)
+      return PT_Bool;
+    else if constexpr (std::is_same<T, int8_t>::value ||
+                       std::is_same<T, Integral<8, true>>::value)
+      return PT_Sint8;
+    else if constexpr (std::is_same<T, uint8_t>::value ||
+                       std::is_same<T, Integral<8, false>>::value)
+      return PT_Uint8;
+    else if constexpr (std::is_same<T, int16_t>::value ||
+                       std::is_same<T, Integral<16, true>>::value)
+      return PT_Sint16;
+    else if constexpr (std::is_same<T, uint16_t>::value ||
+                       std::is_same<T, Integral<16, false>>::value)
+      return PT_Uint16;
+    else if constexpr (std::is_same<T, int32_t>::value ||
+                       std::is_same<T, Integral<32, true>>::value)
+      return PT_Sint32;
+    else if constexpr (std::is_same<T, uint32_t>::value ||
+                       std::is_same<T, Integral<32, false>>::value)
+      return PT_Uint32;
+    else if constexpr (std::is_same<T, int64_t>::value ||
+                       std::is_same<T, Integral<64, true>>::value)
+      return PT_Sint64;
+    else if constexpr (std::is_same<T, uint64_t>::value ||
+                       std::is_same<T, Integral<64, false>>::value)
+      return PT_Uint64;
+
+    llvm_unreachable("unknown type push()'ed into InterpStack");
+  }
+#endif
 };
 
 } // namespace interp
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to