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" |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 21 | #include "llvm/Module.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "llvm/Intrinsics.h" |
| 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
| 26 | |
| 27 | CodeGenModule::CodeGenModule(ASTContext &C, llvm::Module &M) |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 28 | : Context(C), TheModule(M), Types(C, M), CFConstantStringClassRef(0) {} |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame^] | 30 | llvm::Constant *CodeGenModule::GetAddrOfGlobalDecl(const ValueDecl *D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | // See if it is already in the map. |
| 32 | llvm::Constant *&Entry = GlobalDeclMap[D]; |
| 33 | if (Entry) return Entry; |
| 34 | |
| 35 | QualType ASTTy = cast<ValueDecl>(D)->getType(); |
| 36 | const llvm::Type *Ty = getTypes().ConvertType(ASTTy); |
| 37 | if (isa<FunctionDecl>(D)) { |
| 38 | const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty); |
| 39 | // FIXME: param attributes for sext/zext etc. |
| 40 | return Entry = new llvm::Function(FTy, llvm::Function::ExternalLinkage, |
| 41 | D->getName(), &getModule()); |
| 42 | } |
| 43 | |
| 44 | assert(isa<FileVarDecl>(D) && "Unknown global decl!"); |
| 45 | |
| 46 | return Entry = new llvm::GlobalVariable(Ty, false, |
| 47 | llvm::GlobalValue::ExternalLinkage, |
| 48 | 0, D->getName(), &getModule()); |
| 49 | } |
| 50 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 51 | void CodeGenModule::EmitFunction(const FunctionDecl *FD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | // If this is not a prototype, emit the body. |
| 53 | if (FD->getBody()) |
| 54 | CodeGenFunction(*this).GenerateCode(FD); |
| 55 | } |
| 56 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 57 | void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) { |
| 58 | llvm::GlobalVariable *GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalDecl(D)); |
| 59 | |
| 60 | // If the storage class is external and there is no initializer, just leave it |
| 61 | // as a declaration. |
| 62 | if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0) |
| 63 | return; |
| 64 | |
| 65 | // Otherwise, convert the initializer, or use zero if appropriate. |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 66 | llvm::Constant *Init = 0; |
| 67 | if (D->getInit() == 0) { |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 68 | Init = llvm::Constant::getNullValue(GV->getType()->getElementType()); |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 69 | } else if (D->getType()->isIntegerType()) { |
Chris Lattner | 47f7dbf | 2007-09-04 02:34:27 +0000 | [diff] [blame] | 70 | llvm::APSInt Value(static_cast<unsigned>( |
| 71 | getContext().getTypeSize(D->getInit()->getType(), SourceLocation()))); |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 72 | if (D->getInit()->isIntegerConstantExpr(Value, Context)) |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 73 | Init = llvm::ConstantInt::get(Value); |
| 74 | } |
| 75 | assert(Init && "FIXME: Global variable initializers unimp!"); |
| 76 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 77 | GV->setInitializer(Init); |
| 78 | |
| 79 | // Set the llvm linkage type as appropriate. |
| 80 | // FIXME: This isn't right. This should handle common linkage and other |
| 81 | // stuff. |
| 82 | switch (D->getStorageClass()) { |
| 83 | case VarDecl::Auto: |
| 84 | case VarDecl::Register: |
| 85 | assert(0 && "Can't have auto or register globals"); |
| 86 | case VarDecl::None: |
| 87 | case VarDecl::Extern: |
| 88 | // todo: common |
| 89 | break; |
| 90 | case VarDecl::Static: |
| 91 | GV->setLinkage(llvm::GlobalVariable::InternalLinkage); |
| 92 | break; |
| 93 | } |
| 94 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 32b266c | 2007-07-14 00:16:50 +0000 | [diff] [blame] | 96 | /// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified |
| 97 | /// declarator chain. |
| 98 | void CodeGenModule::EmitGlobalVarDeclarator(const FileVarDecl *D) { |
| 99 | for (; D; D = cast_or_null<FileVarDecl>(D->getNextDeclarator())) |
| 100 | EmitGlobalVar(D); |
| 101 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 102 | |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 103 | /// getBuiltinLibFunction |
| 104 | llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { |
| 105 | if (BuiltinFunctions.size() <= BuiltinID) |
| 106 | BuiltinFunctions.resize(BuiltinID); |
| 107 | |
| 108 | // Already available? |
| 109 | llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID]; |
| 110 | if (FunctionSlot) |
| 111 | return FunctionSlot; |
| 112 | |
| 113 | assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn"); |
| 114 | |
| 115 | // Get the name, skip over the __builtin_ prefix. |
| 116 | const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10; |
| 117 | |
| 118 | // Get the type for the builtin. |
| 119 | QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context); |
| 120 | const llvm::FunctionType *Ty = |
| 121 | cast<llvm::FunctionType>(getTypes().ConvertType(Type)); |
| 122 | |
| 123 | // FIXME: This has a serious problem with code like this: |
| 124 | // void abs() {} |
| 125 | // ... __builtin_abs(x); |
| 126 | // The two versions of abs will collide. The fix is for the builtin to win, |
| 127 | // and for the existing one to be turned into a constantexpr cast of the |
| 128 | // builtin. In the case where the existing one is a static function, it |
| 129 | // should just be renamed. |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 130 | if (llvm::Function *Existing = getModule().getFunction(Name)) { |
| 131 | if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) |
| 132 | return FunctionSlot = Existing; |
| 133 | assert(Existing == 0 && "FIXME: Name collision"); |
| 134 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 135 | |
| 136 | // FIXME: param attributes for sext/zext etc. |
| 137 | return FunctionSlot = new llvm::Function(Ty, llvm::Function::ExternalLinkage, |
| 138 | Name, &getModule()); |
| 139 | } |
| 140 | |
| 141 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 142 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 143 | if (MemCpyFn) return MemCpyFn; |
| 144 | llvm::Intrinsic::ID IID; |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 145 | uint64_t Size; unsigned Align; |
| 146 | Context.Target.getPointerInfo(Size, Align, SourceLocation()); |
| 147 | switch (Size) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 148 | default: assert(0 && "Unknown ptr width"); |
| 149 | case 32: IID = llvm::Intrinsic::memcpy_i32; break; |
| 150 | case 64: IID = llvm::Intrinsic::memcpy_i64; break; |
| 151 | } |
| 152 | return MemCpyFn = llvm::Intrinsic::getDeclaration(&TheModule, IID); |
| 153 | } |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 154 | |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 155 | llvm::Constant *CodeGenModule:: |
| 156 | GetAddrOfConstantCFString(const std::string &str) { |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 157 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 158 | CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 159 | |
| 160 | if (Entry.getValue()) |
| 161 | return Entry.getValue(); |
| 162 | |
| 163 | std::vector<llvm::Constant*> Fields; |
| 164 | |
| 165 | if (!CFConstantStringClassRef) { |
| 166 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 167 | Ty = llvm::ArrayType::get(Ty, 0); |
| 168 | |
| 169 | CFConstantStringClassRef = |
| 170 | new llvm::GlobalVariable(Ty, false, |
| 171 | llvm::GlobalVariable::ExternalLinkage, 0, |
| 172 | "__CFConstantStringClassReference", |
| 173 | &getModule()); |
| 174 | } |
| 175 | |
| 176 | // Class pointer. |
| 177 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 178 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 179 | llvm::Constant *C = |
| 180 | llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2); |
| 181 | Fields.push_back(C); |
| 182 | |
| 183 | // Flags. |
| 184 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 185 | Fields.push_back(llvm::ConstantInt::get(Ty, 1992)); |
| 186 | |
| 187 | // String pointer. |
| 188 | C = llvm::ConstantArray::get(str); |
| 189 | C = new llvm::GlobalVariable(C->getType(), true, |
| 190 | llvm::GlobalValue::InternalLinkage, |
| 191 | C, ".str", &getModule()); |
| 192 | |
| 193 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 194 | Fields.push_back(C); |
| 195 | |
| 196 | // String length. |
| 197 | Ty = getTypes().ConvertType(getContext().LongTy); |
| 198 | Fields.push_back(llvm::ConstantInt::get(Ty, str.length())); |
| 199 | |
| 200 | // The struct. |
| 201 | Ty = getTypes().ConvertType(getContext().getCFConstantStringType()); |
| 202 | C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields); |
| 203 | C = new llvm::GlobalVariable(C->getType(), true, |
| 204 | llvm::GlobalVariable::InternalLinkage, |
| 205 | C, "", &getModule()); |
| 206 | |
| 207 | Entry.setValue(C); |
| 208 | return C; |
| 209 | } |