[cfe-commits] r60943 - in /cfe/trunk/lib/CodeGen: CGDecl.cpp CGExprScalar.cpp
Anders Carlsson
andersca at mac.com
Thu Dec 11 23:38:44 PST 2008
Author: andersca
Date: Fri Dec 12 01:38:43 2008
New Revision: 60943
URL: http://llvm.org/viewvc/llvm-project?rev=60943&view=rev
Log:
Implement allocation and sizeof VLAs. This is very basic for now.
Modified:
cfe/trunk/lib/CodeGen/CGDecl.cpp
cfe/trunk/lib/CodeGen/CGExprScalar.cpp
Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=60943&r1=60942&r2=60943&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Fri Dec 12 01:38:43 2008
@@ -20,6 +20,7 @@
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/TargetInfo.h"
#include "llvm/GlobalVariable.h"
+#include "llvm/Intrinsics.h"
#include "llvm/Type.h"
using namespace clang;
using namespace CodeGen;
@@ -160,13 +161,37 @@
DeclPtr = GenerateStaticBlockVarDecl(D, true, Class);
}
} else {
- CGM.ErrorUnsupported(&D, "variable-length array");
+ const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty);
+
+ if (!StackSaveValues.back()) {
+ // Save the stack.
+ const llvm::Type *LTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
+ llvm::Value *Stack = CreateTempAlloca(LTy, "saved_stack");
+
+ llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
+ llvm::Value *V = Builder.CreateCall(F);
+
+ Builder.CreateStore(V, Stack);
+
+ StackSaveValues.back() = Stack;
+ }
+ // Get the element type.
+ const llvm::Type *LElemTy = ConvertType(Ty);
+ const llvm::Type *LElemPtrTy =
+ llvm::PointerType::get(LElemTy, D.getType().getAddressSpace());
+
+ llvm::Value *VLASize = GetVLASize(VAT);
+
+ // Allocate memory for the array.
+ llvm::Value *VLA = Builder.CreateAlloca(llvm::Type::Int8Ty, VLASize, "vla");
+ VLA = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
- // FIXME: VLA: Add VLA support. For now just make up enough to let
- // the compile go through.
- const llvm::Type *LTy = ConvertType(Ty);
llvm::AllocaInst *Alloc =
- CreateTempAlloca(LTy, D.getIdentifier()->getName());
+ CreateTempAlloca(LElemPtrTy, D.getIdentifier()->getName());
+
+ // FIXME: Volatile?
+ Builder.CreateStore(VLA, Alloc);
+
DeclPtr = Alloc;
}
Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=60943&r1=60942&r2=60943&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Fri Dec 12 01:38:43 2008
@@ -663,7 +663,14 @@
if (TypeToSize->isVoidType() || TypeToSize->isFunctionType())
return llvm::ConstantInt::get(llvm::APInt(ResultWidth, 1));
- /// FIXME: This doesn't handle VLAs yet!
+ if (const VariableArrayType *VAT =
+ CGF.getContext().getAsVariableArrayType(TypeToSize)) {
+ if (E->isSizeOf())
+ return CGF.GetVLASize(VAT);
+ else
+ assert(0 && "alignof VLAs not implemented yet");
+ }
+
std::pair<uint64_t, unsigned> Info = CGF.getContext().getTypeInfo(TypeToSize);
uint64_t Val = E->isSizeOf() ? Info.first : Info.second;
More information about the cfe-commits
mailing list