void CodeGenFunction::EmitDecl(const Decl &D) {
-
switch (D.getKind()) {
default: assert(0 && "Unknown decl kind!");
case Decl::FileVariable:
0, D->getName(), &getModule());
}
-void CodeGenModule::EmitFunction(FunctionDecl *FD) {
+void CodeGenModule::EmitFunction(const FunctionDecl *FD) {
// If this is not a prototype, emit the body.
if (FD->getBody())
CodeGenFunction(*this).GenerateCode(FD);
}
+void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) {
+ llvm::GlobalVariable *GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalDecl(D));
+
+ // If the storage class is external and there is no initializer, just leave it
+ // as a declaration.
+ if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0)
+ return;
+
+ // Otherwise, convert the initializer, or use zero if appropriate.
+ llvm::Constant *Init;
+ if (D->getInit() == 0)
+ Init = llvm::Constant::getNullValue(GV->getType()->getElementType());
+ else
+ assert(D->getInit() == 0 && "FIXME: Global variable initializers unimp!");
+
+ GV->setInitializer(Init);
+
+ // Set the llvm linkage type as appropriate.
+ // FIXME: This isn't right. This should handle common linkage and other
+ // stuff.
+ switch (D->getStorageClass()) {
+ case VarDecl::Auto:
+ case VarDecl::Register:
+ assert(0 && "Can't have auto or register globals");
+ case VarDecl::None:
+ case VarDecl::Extern:
+ // todo: common
+ break;
+ case VarDecl::Static:
+ GV->setLinkage(llvm::GlobalVariable::InternalLinkage);
+ break;
+ }
+}
llvm::Function *CodeGenModule::getMemCpyFn() {
class ASTContext;
class FunctionDecl;
class Decl;
+ class FileVarDecl;
namespace CodeGen {
llvm::Function *getMemCpyFn();
- void EmitFunction(FunctionDecl *FD);
+ void EmitFunction(const FunctionDecl *FD);
+ void EmitGlobalVar(const FileVarDecl *D);
void PrintStats() {}
};
static_cast<CodeGenModule*>(B)->EmitFunction(D);
}
+/// CodeGenGlobalVar - Emit the specified global variable to LLVM.
+void clang::CodeGen::CodeGenGlobalVar(BuilderTy *Builder, FileVarDecl *D) {
+ static_cast<CodeGenModule*>(Builder)->EmitGlobalVar(D);
+}
+
+
/// PrintStats - Emit statistic information to stderr.
///
void clang::CodeGen::PrintStats(BuilderTy *B) {
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
CodeGen::CodeGenFunction(Builder, FD);
- } else if (isa<TypedefDecl>(D)) {
- std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n";
+ } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
+ CodeGen::CodeGenGlobalVar(Builder, FVD);
} else {
- std::cerr << "Read top-level variable decl: '" << D->getName() << "'\n";
+ assert(isa<TypedefDecl>(D) && "Only expected typedefs here");
+ // don't codegen for now, eventually pass down for debug info.
+ //std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n";
}
}
namespace clang {
class ASTContext;
class FunctionDecl;
+ class FileVarDecl;
namespace CodeGen {
/// BuilderTy - This is an opaque type used to reference ModuleBuilder
///
void CodeGenFunction(BuilderTy *Builder, FunctionDecl *D);
+ /// CodeGenGlobalVar - Emit the specified global variable to LLVM.
+ void CodeGenGlobalVar(BuilderTy *Builder, FileVarDecl *D);
+
/// PrintStats - Emit statistic information to stderr.
///
void PrintStats(BuilderTy *Builder);