Implement codegen for the following static var init.

void g() {
  static char a[10];
  static char *b = a;
}

Now we can compile wget!



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@47627 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Lauro Ramos Venancio 2008-02-26 21:41:45 +00:00
Родитель d87a321a3c
Коммит 813733577d
6 изменённых файлов: 27 добавлений и 9 удалений

Просмотреть файл

@ -77,7 +77,7 @@ void CodeGenFunction::EmitStaticBlockVarDecl(const BlockVarDecl &D) {
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");

Просмотреть файл

@ -25,9 +25,10 @@ namespace {
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 @@ public:
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 @@ public:
} // 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 @@ llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E)
}
}
return ConstExprEmitter(*this).Visit(const_cast<Expr*>(E));
return ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
}

Просмотреть файл

@ -40,6 +40,10 @@ llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
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);

Просмотреть файл

@ -335,6 +335,9 @@ public:
/// 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
//===--------------------------------------------------------------------===//

Просмотреть файл

@ -41,6 +41,8 @@ namespace clang {
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 @@ public:
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.

Просмотреть файл

@ -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 @@ void f() {
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;
}