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 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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" |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 19 | #include "clang/Basic/LangOptions.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "llvm/Intrinsics.h" |
Christopher Lamb | ce39faa | 2007-12-02 08:49:54 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using namespace CodeGen; |
| 28 | |
| 29 | |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 30 | CodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO, |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 31 | llvm::Module &M, const llvm::TargetData &TD, |
| 32 | Diagnostic &diags) |
| 33 | : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags), |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 34 | Types(C, M, TD), MemCpyFn(0), CFConstantStringClassRef(0) {} |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 36 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 37 | /// specified stmt yet. |
| 38 | void CodeGenModule::WarnUnsupported(const Stmt *S, const char *Type) { |
| 39 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning, |
| 40 | "cannot codegen this %0 yet"); |
| 41 | SourceRange Range = S->getSourceRange(); |
| 42 | std::string Msg = Type; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 43 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID, |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 44 | &Msg, 1, &Range, 1); |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 45 | } |
Chris Lattner | 58c3f9e | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 46 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 47 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 48 | /// specified decl yet. |
| 49 | void CodeGenModule::WarnUnsupported(const Decl *D, const char *Type) { |
| 50 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning, |
| 51 | "cannot codegen this %0 yet"); |
| 52 | std::string Msg = Type; |
| 53 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID, |
| 54 | &Msg, 1); |
| 55 | } |
| 56 | |
Chris Lattner | 58c3f9e | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 57 | /// ReplaceMapValuesWith - This is a really slow and bad function that |
| 58 | /// searches for any entries in GlobalDeclMap that point to OldVal, changing |
| 59 | /// them to point to NewVal. This is badbadbad, FIXME! |
| 60 | void CodeGenModule::ReplaceMapValuesWith(llvm::Constant *OldVal, |
| 61 | llvm::Constant *NewVal) { |
| 62 | for (llvm::DenseMap<const Decl*, llvm::Constant*>::iterator |
| 63 | I = GlobalDeclMap.begin(), E = GlobalDeclMap.end(); I != E; ++I) |
| 64 | if (I->second == OldVal) I->second = NewVal; |
| 65 | } |
| 66 | |
| 67 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 68 | llvm::Constant *CodeGenModule::GetAddrOfFunctionDecl(const FunctionDecl *D, |
| 69 | bool isDefinition) { |
| 70 | // See if it is already in the map. If so, just return it. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 71 | llvm::Constant *&Entry = GlobalDeclMap[D]; |
| 72 | if (Entry) return Entry; |
| 73 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 74 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 75 | |
| 76 | // Check to see if the function already exists. |
| 77 | llvm::Function *F = getModule().getFunction(D->getName()); |
| 78 | const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty); |
| 79 | |
| 80 | // If it doesn't already exist, just create and return an entry. |
| 81 | if (F == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | // FIXME: param attributes for sext/zext etc. |
| 83 | return Entry = new llvm::Function(FTy, llvm::Function::ExternalLinkage, |
| 84 | D->getName(), &getModule()); |
| 85 | } |
| 86 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 87 | // If the pointer type matches, just return it. |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 88 | llvm::Type *PFTy = llvm::PointerType::getUnqual(Ty); |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 89 | if (PFTy == F->getType()) return Entry = F; |
Chris Lattner | fafad83 | 2007-12-02 06:30:46 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 91 | // If this isn't a definition, just return it casted to the right type. |
| 92 | if (!isDefinition) |
| 93 | return Entry = llvm::ConstantExpr::getBitCast(F, PFTy); |
| 94 | |
| 95 | // Otherwise, we have a definition after a prototype with the wrong type. |
| 96 | // F is the Function* for the one with the wrong type, we must make a new |
| 97 | // Function* and update everything that used F (a declaration) with the new |
| 98 | // Function* (which will be a definition). |
| 99 | // |
| 100 | // This happens if there is a prototype for a function (e.g. "int f()") and |
| 101 | // then a definition of a different type (e.g. "int f(int x)"). Start by |
| 102 | // making a new function of the correct type, RAUW, then steal the name. |
| 103 | llvm::Function *NewFn = new llvm::Function(FTy, |
| 104 | llvm::Function::ExternalLinkage, |
| 105 | "", &getModule()); |
| 106 | NewFn->takeName(F); |
| 107 | |
| 108 | // Replace uses of F with the Function we will endow with a body. |
| 109 | llvm::Constant *NewPtrForOldDecl = |
| 110 | llvm::ConstantExpr::getBitCast(NewFn, F->getType()); |
| 111 | F->replaceAllUsesWith(NewPtrForOldDecl); |
| 112 | |
| 113 | // FIXME: Update the globaldeclmap for the previous decl of this name. We |
| 114 | // really want a way to walk all of these, but we don't have it yet. This |
| 115 | // is incredibly slow! |
| 116 | ReplaceMapValuesWith(F, NewPtrForOldDecl); |
| 117 | |
| 118 | // Ok, delete the old function now, which is dead. |
| 119 | assert(F->isDeclaration() && "Shouldn't replace non-declaration"); |
| 120 | F->eraseFromParent(); |
| 121 | |
| 122 | // Return the new function which has the right type. |
| 123 | return Entry = NewFn; |
| 124 | } |
| 125 | |
Chris Lattner | c4b23a5 | 2008-02-05 06:37:34 +0000 | [diff] [blame^] | 126 | static bool IsZeroElementArray(const llvm::Type *Ty) { |
| 127 | if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(Ty)) |
| 128 | return ATy->getNumElements() == 0; |
| 129 | return false; |
| 130 | } |
| 131 | |
Chris Lattner | 2b9d2ca | 2007-12-18 08:16:44 +0000 | [diff] [blame] | 132 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, |
| 133 | bool isDefinition) { |
| 134 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 135 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 136 | // See if it is already in the map. |
| 137 | llvm::Constant *&Entry = GlobalDeclMap[D]; |
| 138 | if (Entry) return Entry; |
| 139 | |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 140 | QualType ASTTy = D->getType(); |
| 141 | const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 142 | |
| 143 | // Check to see if the global already exists. |
Chris Lattner | 4957378 | 2008-02-02 04:43:11 +0000 | [diff] [blame] | 144 | llvm::GlobalVariable *GV = getModule().getGlobalVariable(D->getName(), true); |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 145 | |
| 146 | // If it doesn't already exist, just create and return an entry. |
| 147 | if (GV == 0) { |
| 148 | return Entry = new llvm::GlobalVariable(Ty, false, |
| 149 | llvm::GlobalValue::ExternalLinkage, |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 150 | 0, D->getName(), &getModule(), 0, |
| 151 | ASTTy.getAddressSpace()); |
Chris Lattner | fafad83 | 2007-12-02 06:30:46 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 154 | // If the pointer type matches, just return it. |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 155 | llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 156 | if (PTy == GV->getType()) return Entry = GV; |
| 157 | |
| 158 | // If this isn't a definition, just return it casted to the right type. |
| 159 | if (!isDefinition) |
| 160 | return Entry = llvm::ConstantExpr::getBitCast(GV, PTy); |
| 161 | |
| 162 | |
| 163 | // Otherwise, we have a definition after a prototype with the wrong type. |
| 164 | // GV is the GlobalVariable* for the one with the wrong type, we must make a |
| 165 | /// new GlobalVariable* and update everything that used GV (a declaration) |
| 166 | // with the new GlobalVariable* (which will be a definition). |
| 167 | // |
| 168 | // This happens if there is a prototype for a global (e.g. "extern int x[];") |
| 169 | // and then a definition of a different type (e.g. "int x[10];"). Start by |
| 170 | // making a new global of the correct type, RAUW, then steal the name. |
| 171 | llvm::GlobalVariable *NewGV = |
| 172 | new llvm::GlobalVariable(Ty, false, llvm::GlobalValue::ExternalLinkage, |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 173 | 0, D->getName(), &getModule(), 0, |
| 174 | ASTTy.getAddressSpace()); |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 175 | NewGV->takeName(GV); |
| 176 | |
| 177 | // Replace uses of GV with the globalvalue we will endow with a body. |
| 178 | llvm::Constant *NewPtrForOldDecl = |
| 179 | llvm::ConstantExpr::getBitCast(NewGV, GV->getType()); |
| 180 | GV->replaceAllUsesWith(NewPtrForOldDecl); |
| 181 | |
| 182 | // FIXME: Update the globaldeclmap for the previous decl of this name. We |
| 183 | // really want a way to walk all of these, but we don't have it yet. This |
| 184 | // is incredibly slow! |
| 185 | ReplaceMapValuesWith(GV, NewPtrForOldDecl); |
| 186 | |
Chris Lattner | c4b23a5 | 2008-02-05 06:37:34 +0000 | [diff] [blame^] | 187 | // Verify that GV was a declaration or something like x[] which turns into |
| 188 | // [0 x type]. |
| 189 | assert((GV->isDeclaration() || |
| 190 | IsZeroElementArray(GV->getType()->getElementType())) && |
| 191 | "Shouldn't replace non-declaration"); |
| 192 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 193 | // Ok, delete the old global now, which is dead. |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 194 | GV->eraseFromParent(); |
| 195 | |
| 196 | // Return the new global which has the right type. |
| 197 | return Entry = NewGV; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 200 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 201 | void CodeGenModule::EmitFunction(const FunctionDecl *FD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | // If this is not a prototype, emit the body. |
| 203 | if (FD->getBody()) |
| 204 | CodeGenFunction(*this).GenerateCode(FD); |
| 205 | } |
| 206 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 207 | llvm::Constant *CodeGenModule::EmitGlobalInit(const Expr *Expr) { |
| 208 | return EmitConstantExpr(Expr); |
Devang Patel | 9e32d4b | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 211 | void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) { |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 212 | // If this is just a forward declaration of the variable, don't emit it now, |
| 213 | // allow it to be emitted lazily on its first use. |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 214 | if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0) |
| 215 | return; |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 216 | |
| 217 | // Get the global, forcing it to be a direct reference. |
| 218 | llvm::GlobalVariable *GV = |
Chris Lattner | 2b9d2ca | 2007-12-18 08:16:44 +0000 | [diff] [blame] | 219 | cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, true)); |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 220 | |
| 221 | // Convert the initializer, or use zero if appropriate. |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 222 | llvm::Constant *Init = 0; |
| 223 | if (D->getInit() == 0) { |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 224 | Init = llvm::Constant::getNullValue(GV->getType()->getElementType()); |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 225 | } else if (D->getType()->isIntegerType()) { |
Hartmut Kaiser | 7b66000 | 2007-10-17 15:00:17 +0000 | [diff] [blame] | 226 | llvm::APSInt Value(static_cast<uint32_t>( |
Chris Lattner | 47f7dbf | 2007-09-04 02:34:27 +0000 | [diff] [blame] | 227 | getContext().getTypeSize(D->getInit()->getType(), SourceLocation()))); |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 228 | if (D->getInit()->isIntegerConstantExpr(Value, Context)) |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 229 | Init = llvm::ConstantInt::get(Value); |
| 230 | } |
Devang Patel | 8e53e72 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 231 | |
Devang Patel | 9e32d4b | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 232 | if (!Init) |
Oliver Hunt | 2824723 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 233 | Init = EmitGlobalInit(D->getInit()); |
Devang Patel | 8e53e72 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 234 | |
Chris Lattner | f89dfb2 | 2007-12-10 00:05:55 +0000 | [diff] [blame] | 235 | assert(GV->getType()->getElementType() == Init->getType() && |
| 236 | "Initializer codegen type mismatch!"); |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 237 | GV->setInitializer(Init); |
| 238 | |
| 239 | // Set the llvm linkage type as appropriate. |
| 240 | // FIXME: This isn't right. This should handle common linkage and other |
| 241 | // stuff. |
| 242 | switch (D->getStorageClass()) { |
| 243 | case VarDecl::Auto: |
| 244 | case VarDecl::Register: |
| 245 | assert(0 && "Can't have auto or register globals"); |
| 246 | case VarDecl::None: |
| 247 | case VarDecl::Extern: |
Steve Naroff | d6326c6 | 2008-01-25 22:14:40 +0000 | [diff] [blame] | 248 | case VarDecl::PrivateExtern: |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 249 | // todo: common |
| 250 | break; |
| 251 | case VarDecl::Static: |
| 252 | GV->setLinkage(llvm::GlobalVariable::InternalLinkage); |
| 253 | break; |
| 254 | } |
| 255 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 256 | |
Chris Lattner | 32b266c | 2007-07-14 00:16:50 +0000 | [diff] [blame] | 257 | /// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified |
| 258 | /// declarator chain. |
| 259 | void CodeGenModule::EmitGlobalVarDeclarator(const FileVarDecl *D) { |
| 260 | for (; D; D = cast_or_null<FileVarDecl>(D->getNextDeclarator())) |
| 261 | EmitGlobalVar(D); |
| 262 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 264 | /// getBuiltinLibFunction |
| 265 | llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { |
Chris Lattner | 1426fec | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 266 | if (BuiltinID > BuiltinFunctions.size()) |
| 267 | BuiltinFunctions.resize(BuiltinID); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 268 | |
Chris Lattner | 1426fec | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 269 | // Cache looked up functions. Since builtin id #0 is invalid we don't reserve |
| 270 | // a slot for it. |
| 271 | assert(BuiltinID && "Invalid Builtin ID"); |
| 272 | llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 273 | if (FunctionSlot) |
| 274 | return FunctionSlot; |
| 275 | |
| 276 | assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn"); |
| 277 | |
| 278 | // Get the name, skip over the __builtin_ prefix. |
| 279 | const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10; |
| 280 | |
| 281 | // Get the type for the builtin. |
| 282 | QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context); |
| 283 | const llvm::FunctionType *Ty = |
| 284 | cast<llvm::FunctionType>(getTypes().ConvertType(Type)); |
| 285 | |
| 286 | // FIXME: This has a serious problem with code like this: |
| 287 | // void abs() {} |
| 288 | // ... __builtin_abs(x); |
| 289 | // The two versions of abs will collide. The fix is for the builtin to win, |
| 290 | // and for the existing one to be turned into a constantexpr cast of the |
| 291 | // builtin. In the case where the existing one is a static function, it |
| 292 | // should just be renamed. |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 293 | if (llvm::Function *Existing = getModule().getFunction(Name)) { |
| 294 | if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) |
| 295 | return FunctionSlot = Existing; |
| 296 | assert(Existing == 0 && "FIXME: Name collision"); |
| 297 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 298 | |
| 299 | // FIXME: param attributes for sext/zext etc. |
| 300 | return FunctionSlot = new llvm::Function(Ty, llvm::Function::ExternalLinkage, |
| 301 | Name, &getModule()); |
| 302 | } |
| 303 | |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 304 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, |
| 305 | unsigned NumTys) { |
| 306 | return llvm::Intrinsic::getDeclaration(&getModule(), |
| 307 | (llvm::Intrinsic::ID)IID, Tys, NumTys); |
| 308 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 309 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 310 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 311 | if (MemCpyFn) return MemCpyFn; |
| 312 | llvm::Intrinsic::ID IID; |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 313 | uint64_t Size; unsigned Align; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 314 | Context.Target.getPointerInfo(Size, Align, FullSourceLoc()); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 315 | switch (Size) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 316 | default: assert(0 && "Unknown ptr width"); |
| 317 | case 32: IID = llvm::Intrinsic::memcpy_i32; break; |
| 318 | case 64: IID = llvm::Intrinsic::memcpy_i64; break; |
| 319 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 320 | return MemCpyFn = getIntrinsic(IID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 321 | } |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 322 | |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 323 | |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 324 | llvm::Constant *CodeGenModule:: |
| 325 | GetAddrOfConstantCFString(const std::string &str) { |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 326 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 327 | CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 328 | |
| 329 | if (Entry.getValue()) |
| 330 | return Entry.getValue(); |
| 331 | |
| 332 | std::vector<llvm::Constant*> Fields; |
| 333 | |
| 334 | if (!CFConstantStringClassRef) { |
| 335 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 336 | Ty = llvm::ArrayType::get(Ty, 0); |
| 337 | |
| 338 | CFConstantStringClassRef = |
| 339 | new llvm::GlobalVariable(Ty, false, |
| 340 | llvm::GlobalVariable::ExternalLinkage, 0, |
| 341 | "__CFConstantStringClassReference", |
| 342 | &getModule()); |
| 343 | } |
| 344 | |
| 345 | // Class pointer. |
| 346 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 347 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 348 | llvm::Constant *C = |
| 349 | llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2); |
| 350 | Fields.push_back(C); |
| 351 | |
| 352 | // Flags. |
| 353 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 354 | Fields.push_back(llvm::ConstantInt::get(Ty, 1992)); |
| 355 | |
| 356 | // String pointer. |
| 357 | C = llvm::ConstantArray::get(str); |
| 358 | C = new llvm::GlobalVariable(C->getType(), true, |
| 359 | llvm::GlobalValue::InternalLinkage, |
| 360 | C, ".str", &getModule()); |
| 361 | |
| 362 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 363 | Fields.push_back(C); |
| 364 | |
| 365 | // String length. |
| 366 | Ty = getTypes().ConvertType(getContext().LongTy); |
| 367 | Fields.push_back(llvm::ConstantInt::get(Ty, str.length())); |
| 368 | |
| 369 | // The struct. |
| 370 | Ty = getTypes().ConvertType(getContext().getCFConstantStringType()); |
| 371 | C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields); |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 372 | llvm::GlobalVariable *GV = |
| 373 | new llvm::GlobalVariable(C->getType(), true, |
| 374 | llvm::GlobalVariable::InternalLinkage, |
| 375 | C, "", &getModule()); |
| 376 | GV->setSection("__DATA,__cfstring"); |
| 377 | Entry.setValue(GV); |
| 378 | return GV; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 379 | } |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 380 | |
| 381 | /// GenerateWritableString -- Creates storage for a string literal |
| 382 | static llvm::Constant *GenerateStringLiteral(const std::string &str, |
| 383 | bool constant, |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 384 | CodeGenModule &CGM) { |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 385 | // Create Constant for this string literal |
| 386 | llvm::Constant *C=llvm::ConstantArray::get(str); |
| 387 | |
| 388 | // Create a global variable for this string |
| 389 | C = new llvm::GlobalVariable(C->getType(), constant, |
| 390 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 391 | C, ".str", &CGM.getModule()); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 392 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 393 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 394 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 395 | return C; |
| 396 | } |
| 397 | |
| 398 | /// CodeGenModule::GetAddrOfConstantString -- returns a pointer to the first |
| 399 | /// element of a character array containing the literal. |
| 400 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str) { |
| 401 | // Don't share any string literals if writable-strings is turned on. |
| 402 | if (Features.WritableStrings) |
| 403 | return GenerateStringLiteral(str, false, *this); |
| 404 | |
| 405 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 406 | ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 407 | |
| 408 | if (Entry.getValue()) |
| 409 | return Entry.getValue(); |
| 410 | |
| 411 | // Create a global variable for this. |
| 412 | llvm::Constant *C = GenerateStringLiteral(str, true, *this); |
| 413 | Entry.setValue(C); |
| 414 | return C; |
| 415 | } |