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