[cfe-commits] r47627 - in /cfe/trunk: CodeGen/CGDecl.cpp CodeGen/CGExprConstant.cpp CodeGen/CodeGenFunction.cpp CodeGen/CodeGenFunction.h CodeGen/CodeGenModule.h test/CodeGen/staticinit.c
Lauro Ramos Venancio
lauro.venancio at gmail.com
Tue Feb 26 13:41:45 PST 2008
Author: laurov
Date: Tue Feb 26 15:41:45 2008
New Revision: 47627
URL: http://llvm.org/viewvc/llvm-project?rev=47627&view=rev
Log:
Implement codegen for the following static var init.
void g() {
static char a[10];
static char *b = a;
}
Now we can compile wget!
Modified:
cfe/trunk/CodeGen/CGDecl.cpp
cfe/trunk/CodeGen/CGExprConstant.cpp
cfe/trunk/CodeGen/CodeGenFunction.cpp
cfe/trunk/CodeGen/CodeGenFunction.h
cfe/trunk/CodeGen/CodeGenModule.h
cfe/trunk/test/CodeGen/staticinit.c
Modified: cfe/trunk/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGDecl.cpp?rev=47627&r1=47626&r2=47627&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/CodeGen/CGDecl.cpp Tue Feb 26 15:41:45 2008
@@ -77,7 +77,7 @@
if (D.getInit() == 0) {
Init = llvm::Constant::getNullValue(LTy);
} else {
- Init = CGM.EmitGlobalInit(D.getInit());
+ Init = CGM.EmitConstantExpr(D.getInit(), this);
}
assert(Init && "Unable to create initialiser for static decl");
Modified: cfe/trunk/CodeGen/CGExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGExprConstant.cpp?rev=47627&r1=47626&r2=47627&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/CodeGen/CGExprConstant.cpp Tue Feb 26 15:41:45 2008
@@ -25,9 +25,10 @@
class VISIBILITY_HIDDEN ConstExprEmitter :
public StmtVisitor<ConstExprEmitter, llvm::Constant*> {
CodeGenModule &CGM;
+ CodeGenFunction *CGF;
public:
- ConstExprEmitter(CodeGenModule &cgm)
- : CGM(cgm) {
+ ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf)
+ : CGM(cgm), CGF(cgf) {
}
//===--------------------------------------------------------------------===//
@@ -534,8 +535,10 @@
return CGM.GetAddrOfFunctionDecl(FD, false);
if (const FileVarDecl* VD = dyn_cast<FileVarDecl>(Decl))
return CGM.GetAddrOfGlobalVar(VD, false);
- // We can end up here with static block-scope variables (and others?)
- // FIXME: How do we implement block-scope variables?!
+ if (const BlockVarDecl* BVD = dyn_cast<BlockVarDecl>(Decl)) {
+ assert(CGF && "Can't access static local vars without CGF");
+ return CGF->GetAddrOfStaticLocalVar(BVD);
+ }
break;
}
case Expr::MemberExprClass: {
@@ -604,7 +607,8 @@
} // end anonymous namespace.
-llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E)
+llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
+ CodeGenFunction *CGF)
{
QualType type = E->getType().getCanonicalType();
@@ -616,5 +620,5 @@
}
}
- return ConstExprEmitter(*this).Visit(const_cast<Expr*>(E));
+ return ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
}
Modified: cfe/trunk/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenFunction.cpp?rev=47627&r1=47626&r2=47627&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/CodeGen/CodeGenFunction.cpp Tue Feb 26 15:41:45 2008
@@ -40,6 +40,10 @@
return BB = new llvm::BasicBlock(S->getName());
}
+llvm::Constant *
+CodeGenFunction::GetAddrOfStaticLocalVar(const BlockVarDecl *BVD) {
+ return cast<llvm::Constant>(LocalDeclMap[BVD]);
+}
const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
return CGM.getTypes().ConvertType(T);
Modified: cfe/trunk/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenFunction.h?rev=47627&r1=47626&r2=47627&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/CodeGen/CodeGenFunction.h Tue Feb 26 15:41:45 2008
@@ -335,6 +335,9 @@
/// getCGRecordLayout - Return record layout info.
const CGRecordLayout *getCGRecordLayout(CodeGenTypes &CGT, QualType RTy);
+
+ /// GetAddrOfStaticLocalVar - Return the address of a static local variable.
+ llvm::Constant *GetAddrOfStaticLocalVar(const BlockVarDecl *BVD);
//===--------------------------------------------------------------------===//
// Declaration Emission
//===--------------------------------------------------------------------===//
Modified: cfe/trunk/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenModule.h?rev=47627&r1=47626&r2=47627&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/CodeGen/CodeGenModule.h Tue Feb 26 15:41:45 2008
@@ -41,6 +41,8 @@
namespace CodeGen {
+ class CodeGenFunction;
+
/// CodeGenModule - This class organizes the cross-module state that is used
/// while generating LLVM code.
class CodeGenModule {
@@ -95,7 +97,7 @@
void EmitGlobalVarDeclarator(const FileVarDecl *D);
void UpdateCompletedType(const TagDecl *D);
llvm::Constant *EmitGlobalInit(const Expr *E);
- llvm::Constant *EmitConstantExpr(const Expr *E);
+ llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
/// WarnUnsupported - Print out a warning that codegen doesn't support the
/// specified stmt yet.
Modified: cfe/trunk/test/CodeGen/staticinit.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/staticinit.c?rev=47627&r1=47626&r2=47627&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/staticinit.c (original)
+++ cfe/trunk/test/CodeGen/staticinit.c Tue Feb 26 15:41:45 2008
@@ -1,4 +1,4 @@
-// RUN: clang -emit-llvm %s
+// RUN: clang -emit-llvm < %s | grep "g.b = internal global i8. getelementptr"
struct AStruct {
int i;
@@ -13,3 +13,8 @@
static char* strs[] = { "one", "two", "three", "four" };
static struct AStruct myStruct = { 1, "two", 3.0 };
}
+
+void g() {
+ static char a[10];
+ static char *b = a;
+}
More information about the cfe-commits
mailing list