]> granicus.if.org Git - clang/commitdiff
Add map of VLA types and their sizes
authorAnders Carlsson <andersca@mac.com>
Fri, 12 Dec 2008 07:19:02 +0000 (07:19 +0000)
committerAnders Carlsson <andersca@mac.com>
Fri, 12 Dec 2008 07:19:02 +0000 (07:19 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@60939 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CodeGenFunction.cpp
lib/CodeGen/CodeGenFunction.h

index fe88bc9127276570ae50c4c7cff64e6cce8a332d..bcb51a3efc65708fae95e215bd7797a4fa4fe8dd 100644 (file)
@@ -401,3 +401,30 @@ llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty)
   return AddrTyped;
 }
 
+llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT)
+{
+  llvm::Value *&SizeEntry = VLASizeMap[VAT];
+
+  if (!SizeEntry) {
+    // Get the element size;
+    llvm::Value *ElemSize;
+  
+    QualType ElemTy = VAT->getElementType();
+
+    if (const VariableArrayType *ElemVAT = 
+        getContext().getAsVariableArrayType(ElemTy))
+      ElemSize = GetVLASize(ElemVAT);
+    else {
+      // FIXME: We use Int32Ty here because the alloca instruction takes a
+      // 32-bit integer. What should we do about overflow?
+      ElemSize = llvm::ConstantInt::get(llvm::Type::Int32Ty, 
+                                        getContext().getTypeSize(ElemTy) / 8);
+    }
+
+    llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
+
+    SizeEntry = Builder.CreateMul(ElemSize, NumElements);
+  }
+
+  return SizeEntry;
+}
index 952c3b3c1c71f4bc21d275a7282bb079441a0c43..5c4246f7c315ca7801a23a0ebd3071f7f3009c40 100644 (file)
@@ -166,6 +166,11 @@ private:
   /// statement range in current switch instruction.
   llvm::BasicBlock *CaseRangeBlock;
 
+  // VLASizeMap - This keeps track of the associated size for each VLA type
+  // FIXME: Maybe this could be a stack of maps that is pushed/popped as
+  // we enter/leave scopes.
+  llvm::DenseMap<const VariableArrayType*, llvm::Value*> VLASizeMap;
+  
   /// StackSaveValues - A stack(!) of stack save values. When a new scope is
   /// entered, a null is pushed on this stack. If a VLA is emitted, then 
   /// the return value of llvm.stacksave() is stored at the top of this stack.
@@ -344,6 +349,10 @@ public:
   // instruction in LLVM instead once it works well enough.  
   llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty);
   
+  // GetVLASize - Returns an LLVM value that corresponds to the size in bytes
+  // of a variable length array type.
+  llvm::Value *GetVLASize(const VariableArrayType *);
+
   //===--------------------------------------------------------------------===//
   //                            Declaration Emission
   //===--------------------------------------------------------------------===//