Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This coordinates the per-module state used while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenModule.h" |
| 15 | #include "CodeGenFunction.h" |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "llvm/DerivedTypes.h" |
| 21 | #include "llvm/Function.h" |
| 22 | #include "llvm/GlobalVariable.h" |
| 23 | #include "llvm/Intrinsics.h" |
| 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
| 27 | |
| 28 | CodeGenModule::CodeGenModule(ASTContext &C, llvm::Module &M) |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 29 | : Context(C), TheModule(M), Types(C, M), CFConstantStringClassRef(0) {} |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | |
| 31 | llvm::Constant *CodeGenModule::GetAddrOfGlobalDecl(const Decl *D) { |
| 32 | // See if it is already in the map. |
| 33 | llvm::Constant *&Entry = GlobalDeclMap[D]; |
| 34 | if (Entry) return Entry; |
| 35 | |
| 36 | QualType ASTTy = cast<ValueDecl>(D)->getType(); |
| 37 | const llvm::Type *Ty = getTypes().ConvertType(ASTTy); |
| 38 | if (isa<FunctionDecl>(D)) { |
| 39 | const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty); |
| 40 | // FIXME: param attributes for sext/zext etc. |
| 41 | return Entry = new llvm::Function(FTy, llvm::Function::ExternalLinkage, |
| 42 | D->getName(), &getModule()); |
| 43 | } |
| 44 | |
| 45 | assert(isa<FileVarDecl>(D) && "Unknown global decl!"); |
| 46 | |
| 47 | return Entry = new llvm::GlobalVariable(Ty, false, |
| 48 | llvm::GlobalValue::ExternalLinkage, |
| 49 | 0, D->getName(), &getModule()); |
| 50 | } |
| 51 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 52 | void CodeGenModule::EmitFunction(const FunctionDecl *FD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | // If this is not a prototype, emit the body. |
| 54 | if (FD->getBody()) |
| 55 | CodeGenFunction(*this).GenerateCode(FD); |
| 56 | } |
| 57 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 58 | void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) { |
| 59 | llvm::GlobalVariable *GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalDecl(D)); |
| 60 | |
| 61 | // If the storage class is external and there is no initializer, just leave it |
| 62 | // as a declaration. |
| 63 | if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0) |
| 64 | return; |
| 65 | |
| 66 | // Otherwise, convert the initializer, or use zero if appropriate. |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 67 | llvm::Constant *Init = 0; |
| 68 | if (D->getInit() == 0) { |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 69 | Init = llvm::Constant::getNullValue(GV->getType()->getElementType()); |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 70 | } else if (D->getType()->isIntegerType()) { |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 71 | llvm::APSInt Value(getContext().getTypeSize(D->getInit()->getType(), |
| 72 | SourceLocation())); |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 73 | if (D->getInit()->isIntegerConstantExpr(Value, Context)) |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 74 | Init = llvm::ConstantInt::get(Value); |
| 75 | } |
| 76 | assert(Init && "FIXME: Global variable initializers unimp!"); |
| 77 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 78 | GV->setInitializer(Init); |
| 79 | |
| 80 | // Set the llvm linkage type as appropriate. |
| 81 | // FIXME: This isn't right. This should handle common linkage and other |
| 82 | // stuff. |
| 83 | switch (D->getStorageClass()) { |
| 84 | case VarDecl::Auto: |
| 85 | case VarDecl::Register: |
| 86 | assert(0 && "Can't have auto or register globals"); |
| 87 | case VarDecl::None: |
| 88 | case VarDecl::Extern: |
| 89 | // todo: common |
| 90 | break; |
| 91 | case VarDecl::Static: |
| 92 | GV->setLinkage(llvm::GlobalVariable::InternalLinkage); |
| 93 | break; |
| 94 | } |
| 95 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 32b266c | 2007-07-14 00:16:50 +0000 | [diff] [blame] | 97 | /// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified |
| 98 | /// declarator chain. |
| 99 | void CodeGenModule::EmitGlobalVarDeclarator(const FileVarDecl *D) { |
| 100 | for (; D; D = cast_or_null<FileVarDecl>(D->getNextDeclarator())) |
| 101 | EmitGlobalVar(D); |
| 102 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | |
| 104 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 105 | if (MemCpyFn) return MemCpyFn; |
| 106 | llvm::Intrinsic::ID IID; |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 107 | uint64_t Size; unsigned Align; |
| 108 | Context.Target.getPointerInfo(Size, Align, SourceLocation()); |
| 109 | switch (Size) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | default: assert(0 && "Unknown ptr width"); |
| 111 | case 32: IID = llvm::Intrinsic::memcpy_i32; break; |
| 112 | case 64: IID = llvm::Intrinsic::memcpy_i64; break; |
| 113 | } |
| 114 | return MemCpyFn = llvm::Intrinsic::getDeclaration(&TheModule, IID); |
| 115 | } |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 116 | |
| 117 | llvm::Constant *CodeGenModule::GetAddrOfConstantCFString(const std::string &str) |
| 118 | { |
| 119 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 120 | CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 121 | |
| 122 | if (Entry.getValue()) |
| 123 | return Entry.getValue(); |
| 124 | |
| 125 | std::vector<llvm::Constant*> Fields; |
| 126 | |
| 127 | if (!CFConstantStringClassRef) { |
| 128 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 129 | Ty = llvm::ArrayType::get(Ty, 0); |
| 130 | |
| 131 | CFConstantStringClassRef = |
| 132 | new llvm::GlobalVariable(Ty, false, |
| 133 | llvm::GlobalVariable::ExternalLinkage, 0, |
| 134 | "__CFConstantStringClassReference", |
| 135 | &getModule()); |
| 136 | } |
| 137 | |
| 138 | // Class pointer. |
| 139 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 140 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 141 | llvm::Constant *C = |
| 142 | llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2); |
| 143 | Fields.push_back(C); |
| 144 | |
| 145 | // Flags. |
| 146 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 147 | Fields.push_back(llvm::ConstantInt::get(Ty, 1992)); |
| 148 | |
| 149 | // String pointer. |
| 150 | C = llvm::ConstantArray::get(str); |
| 151 | C = new llvm::GlobalVariable(C->getType(), true, |
| 152 | llvm::GlobalValue::InternalLinkage, |
| 153 | C, ".str", &getModule()); |
| 154 | |
| 155 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 156 | Fields.push_back(C); |
| 157 | |
| 158 | // String length. |
| 159 | Ty = getTypes().ConvertType(getContext().LongTy); |
| 160 | Fields.push_back(llvm::ConstantInt::get(Ty, str.length())); |
| 161 | |
| 162 | // The struct. |
| 163 | Ty = getTypes().ConvertType(getContext().getCFConstantStringType()); |
| 164 | C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields); |
| 165 | C = new llvm::GlobalVariable(C->getType(), true, |
| 166 | llvm::GlobalVariable::InternalLinkage, |
| 167 | C, "", &getModule()); |
| 168 | |
| 169 | Entry.setValue(C); |
| 170 | return C; |
| 171 | } |