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" |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetInfo.h" |
Nate Begeman | ec9426c | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 22 | #include "llvm/CallingConv.h" |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 23 | #include "llvm/Constants.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "llvm/DerivedTypes.h" |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 25 | #include "llvm/Module.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | #include "llvm/Intrinsics.h" |
Christopher Lamb | ce39faa | 2007-12-02 08:49:54 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | using namespace CodeGen; |
| 30 | |
| 31 | |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 32 | CodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO, |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 33 | llvm::Module &M, const llvm::TargetData &TD, |
| 34 | Diagnostic &diags) |
| 35 | : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags), |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 36 | Types(C, M, TD), MemCpyFn(0), MemSetFn(0), CFConstantStringClassRef(0) { |
| 37 | //TODO: Make this selectable at runtime |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 38 | Runtime = CreateObjCRuntime(M, |
| 39 | getTypes().ConvertType(getContext().IntTy), |
| 40 | getTypes().ConvertType(getContext().LongTy)); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | CodeGenModule::~CodeGenModule() { |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 44 | llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction(); |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 45 | if (ObjCInitFunction) |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 46 | AddGlobalCtor(ObjCInitFunction); |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 47 | EmitStatics(); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 48 | EmitGlobalCtors(); |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 49 | EmitAnnotations(); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 50 | delete Runtime; |
| 51 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 53 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 54 | /// specified stmt yet. |
| 55 | void CodeGenModule::WarnUnsupported(const Stmt *S, const char *Type) { |
| 56 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning, |
| 57 | "cannot codegen this %0 yet"); |
| 58 | SourceRange Range = S->getSourceRange(); |
| 59 | std::string Msg = Type; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 60 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID, |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 61 | &Msg, 1, &Range, 1); |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 62 | } |
Chris Lattner | 58c3f9e | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 63 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 64 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 65 | /// specified decl yet. |
| 66 | void CodeGenModule::WarnUnsupported(const Decl *D, const char *Type) { |
| 67 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning, |
| 68 | "cannot codegen this %0 yet"); |
| 69 | std::string Msg = Type; |
| 70 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID, |
| 71 | &Msg, 1); |
| 72 | } |
| 73 | |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 74 | /// AddGlobalCtor - Add a function to the list that will be called before |
| 75 | /// main() runs. |
| 76 | void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor) { |
| 77 | // TODO: Type coercion of void()* types. |
| 78 | GlobalCtors.push_back(Ctor); |
| 79 | } |
| 80 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 81 | /// EmitGlobalCtors - Generates the array of contsturctor functions to be |
| 82 | /// called on module load, if any have been registered with AddGlobalCtor. |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 83 | void CodeGenModule::EmitGlobalCtors() { |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 84 | if (GlobalCtors.empty()) return; |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 86 | // Get the type of @llvm.global_ctors |
| 87 | std::vector<const llvm::Type*> CtorFields; |
| 88 | CtorFields.push_back(llvm::IntegerType::get(32)); |
| 89 | // Constructor function type |
| 90 | std::vector<const llvm::Type*> VoidArgs; |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 91 | llvm::FunctionType* CtorFuncTy = |
| 92 | llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false); |
| 93 | |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 94 | // i32, function type pair |
Chris Lattner | 572cf09 | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 95 | const llvm::Type *FPType = llvm::PointerType::getUnqual(CtorFuncTy); |
| 96 | llvm::StructType* CtorStructTy = |
| 97 | llvm::StructType::get(llvm::Type::Int32Ty, FPType, NULL); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 98 | // Array of fields |
Chris Lattner | 572cf09 | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 99 | llvm::ArrayType* GlobalCtorsTy = |
| 100 | llvm::ArrayType::get(CtorStructTy, GlobalCtors.size()); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 102 | // Define the global variable |
Chris Lattner | 572cf09 | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 103 | llvm::GlobalVariable *GlobalCtorsVal = |
| 104 | new llvm::GlobalVariable(GlobalCtorsTy, false, |
| 105 | llvm::GlobalValue::AppendingLinkage, |
| 106 | (llvm::Constant*)0, "llvm.global_ctors", |
| 107 | &TheModule); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 108 | |
| 109 | // Populate the array |
| 110 | std::vector<llvm::Constant*> CtorValues; |
Chris Lattner | 572cf09 | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 111 | llvm::Constant *MagicNumber = |
| 112 | llvm::ConstantInt::get(llvm::Type::Int32Ty, 65535, false); |
| 113 | std::vector<llvm::Constant*> StructValues; |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 114 | for (std::vector<llvm::Constant*>::iterator I = GlobalCtors.begin(), |
Chris Lattner | 572cf09 | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 115 | E = GlobalCtors.end(); I != E; ++I) { |
| 116 | StructValues.clear(); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 117 | StructValues.push_back(MagicNumber); |
| 118 | StructValues.push_back(*I); |
| 119 | |
Chris Lattner | 572cf09 | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 120 | CtorValues.push_back(llvm::ConstantStruct::get(CtorStructTy, StructValues)); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 121 | } |
Chris Lattner | 572cf09 | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 122 | |
| 123 | GlobalCtorsVal->setInitializer(llvm::ConstantArray::get(GlobalCtorsTy, |
| 124 | CtorValues)); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 127 | |
| 128 | |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 129 | void CodeGenModule::EmitAnnotations() { |
| 130 | if (Annotations.empty()) |
| 131 | return; |
| 132 | |
| 133 | // Create a new global variable for the ConstantStruct in the Module. |
| 134 | llvm::Constant *Array = |
| 135 | llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), |
| 136 | Annotations.size()), |
| 137 | Annotations); |
| 138 | llvm::GlobalValue *gv = |
| 139 | new llvm::GlobalVariable(Array->getType(), false, |
| 140 | llvm::GlobalValue::AppendingLinkage, Array, |
| 141 | "llvm.global.annotations", &TheModule); |
| 142 | gv->setSection("llvm.metadata"); |
| 143 | } |
| 144 | |
Chris Lattner | 58c3f9e | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 145 | /// ReplaceMapValuesWith - This is a really slow and bad function that |
| 146 | /// searches for any entries in GlobalDeclMap that point to OldVal, changing |
| 147 | /// them to point to NewVal. This is badbadbad, FIXME! |
| 148 | void CodeGenModule::ReplaceMapValuesWith(llvm::Constant *OldVal, |
| 149 | llvm::Constant *NewVal) { |
| 150 | for (llvm::DenseMap<const Decl*, llvm::Constant*>::iterator |
| 151 | I = GlobalDeclMap.begin(), E = GlobalDeclMap.end(); I != E; ++I) |
| 152 | if (I->second == OldVal) I->second = NewVal; |
| 153 | } |
| 154 | |
| 155 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 156 | llvm::Constant *CodeGenModule::GetAddrOfFunctionDecl(const FunctionDecl *D, |
| 157 | bool isDefinition) { |
| 158 | // 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] | 159 | llvm::Constant *&Entry = GlobalDeclMap[D]; |
| 160 | if (Entry) return Entry; |
| 161 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 162 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 163 | |
| 164 | // Check to see if the function already exists. |
| 165 | llvm::Function *F = getModule().getFunction(D->getName()); |
| 166 | const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty); |
| 167 | |
| 168 | // If it doesn't already exist, just create and return an entry. |
| 169 | if (F == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | // FIXME: param attributes for sext/zext etc. |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 171 | F = llvm::Function::Create(FTy, llvm::Function::ExternalLinkage, |
| 172 | D->getName(), &getModule()); |
Nate Begeman | ec9426c | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 173 | |
| 174 | // Set the appropriate calling convention for the Function. |
| 175 | if (D->getAttr<FastCallAttr>()) |
| 176 | F->setCallingConv(llvm::CallingConv::Fast); |
| 177 | return Entry = F; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 180 | // If the pointer type matches, just return it. |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 181 | llvm::Type *PFTy = llvm::PointerType::getUnqual(Ty); |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 182 | if (PFTy == F->getType()) return Entry = F; |
Chris Lattner | fafad83 | 2007-12-02 06:30:46 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 184 | // If this isn't a definition, just return it casted to the right type. |
| 185 | if (!isDefinition) |
| 186 | return Entry = llvm::ConstantExpr::getBitCast(F, PFTy); |
| 187 | |
| 188 | // Otherwise, we have a definition after a prototype with the wrong type. |
| 189 | // F is the Function* for the one with the wrong type, we must make a new |
| 190 | // Function* and update everything that used F (a declaration) with the new |
| 191 | // Function* (which will be a definition). |
| 192 | // |
| 193 | // This happens if there is a prototype for a function (e.g. "int f()") and |
| 194 | // then a definition of a different type (e.g. "int f(int x)"). Start by |
| 195 | // making a new function of the correct type, RAUW, then steal the name. |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 196 | llvm::Function *NewFn = llvm::Function::Create(FTy, |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 197 | llvm::Function::ExternalLinkage, |
| 198 | "", &getModule()); |
| 199 | NewFn->takeName(F); |
| 200 | |
| 201 | // Replace uses of F with the Function we will endow with a body. |
| 202 | llvm::Constant *NewPtrForOldDecl = |
| 203 | llvm::ConstantExpr::getBitCast(NewFn, F->getType()); |
| 204 | F->replaceAllUsesWith(NewPtrForOldDecl); |
| 205 | |
| 206 | // FIXME: Update the globaldeclmap for the previous decl of this name. We |
| 207 | // really want a way to walk all of these, but we don't have it yet. This |
| 208 | // is incredibly slow! |
| 209 | ReplaceMapValuesWith(F, NewPtrForOldDecl); |
| 210 | |
| 211 | // Ok, delete the old function now, which is dead. |
| 212 | assert(F->isDeclaration() && "Shouldn't replace non-declaration"); |
| 213 | F->eraseFromParent(); |
| 214 | |
| 215 | // Return the new function which has the right type. |
| 216 | return Entry = NewFn; |
| 217 | } |
| 218 | |
Chris Lattner | c4b23a5 | 2008-02-05 06:37:34 +0000 | [diff] [blame] | 219 | static bool IsZeroElementArray(const llvm::Type *Ty) { |
| 220 | if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(Ty)) |
| 221 | return ATy->getNumElements() == 0; |
| 222 | return false; |
| 223 | } |
| 224 | |
Chris Lattner | 2b9d2ca | 2007-12-18 08:16:44 +0000 | [diff] [blame] | 225 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, |
| 226 | bool isDefinition) { |
| 227 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 228 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 229 | // See if it is already in the map. |
| 230 | llvm::Constant *&Entry = GlobalDeclMap[D]; |
| 231 | if (Entry) return Entry; |
| 232 | |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 233 | QualType ASTTy = D->getType(); |
| 234 | const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 235 | |
| 236 | // Check to see if the global already exists. |
Chris Lattner | 4957378 | 2008-02-02 04:43:11 +0000 | [diff] [blame] | 237 | llvm::GlobalVariable *GV = getModule().getGlobalVariable(D->getName(), true); |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 238 | |
| 239 | // If it doesn't already exist, just create and return an entry. |
| 240 | if (GV == 0) { |
| 241 | return Entry = new llvm::GlobalVariable(Ty, false, |
| 242 | llvm::GlobalValue::ExternalLinkage, |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 243 | 0, D->getName(), &getModule(), 0, |
| 244 | ASTTy.getAddressSpace()); |
Chris Lattner | fafad83 | 2007-12-02 06:30:46 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 247 | // If the pointer type matches, just return it. |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 248 | llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 249 | if (PTy == GV->getType()) return Entry = GV; |
| 250 | |
| 251 | // If this isn't a definition, just return it casted to the right type. |
| 252 | if (!isDefinition) |
| 253 | return Entry = llvm::ConstantExpr::getBitCast(GV, PTy); |
| 254 | |
| 255 | |
| 256 | // Otherwise, we have a definition after a prototype with the wrong type. |
| 257 | // GV is the GlobalVariable* for the one with the wrong type, we must make a |
| 258 | /// new GlobalVariable* and update everything that used GV (a declaration) |
| 259 | // with the new GlobalVariable* (which will be a definition). |
| 260 | // |
| 261 | // This happens if there is a prototype for a global (e.g. "extern int x[];") |
| 262 | // and then a definition of a different type (e.g. "int x[10];"). Start by |
| 263 | // making a new global of the correct type, RAUW, then steal the name. |
| 264 | llvm::GlobalVariable *NewGV = |
| 265 | new llvm::GlobalVariable(Ty, false, llvm::GlobalValue::ExternalLinkage, |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 266 | 0, D->getName(), &getModule(), 0, |
| 267 | ASTTy.getAddressSpace()); |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 268 | NewGV->takeName(GV); |
| 269 | |
| 270 | // Replace uses of GV with the globalvalue we will endow with a body. |
| 271 | llvm::Constant *NewPtrForOldDecl = |
| 272 | llvm::ConstantExpr::getBitCast(NewGV, GV->getType()); |
| 273 | GV->replaceAllUsesWith(NewPtrForOldDecl); |
| 274 | |
| 275 | // FIXME: Update the globaldeclmap for the previous decl of this name. We |
| 276 | // really want a way to walk all of these, but we don't have it yet. This |
| 277 | // is incredibly slow! |
| 278 | ReplaceMapValuesWith(GV, NewPtrForOldDecl); |
| 279 | |
Chris Lattner | c4b23a5 | 2008-02-05 06:37:34 +0000 | [diff] [blame] | 280 | // Verify that GV was a declaration or something like x[] which turns into |
| 281 | // [0 x type]. |
| 282 | assert((GV->isDeclaration() || |
| 283 | IsZeroElementArray(GV->getType()->getElementType())) && |
| 284 | "Shouldn't replace non-declaration"); |
| 285 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 286 | // Ok, delete the old global now, which is dead. |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 287 | GV->eraseFromParent(); |
| 288 | |
| 289 | // Return the new global which has the right type. |
| 290 | return Entry = NewGV; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 294 | void CodeGenModule::EmitObjCMethod(const ObjCMethodDecl *OMD) { |
| 295 | // If this is not a prototype, emit the body. |
| 296 | if (OMD->getBody()) |
| 297 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 298 | } |
| 299 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 300 | void CodeGenModule::EmitFunction(const FunctionDecl *FD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 301 | // If this is not a prototype, emit the body. |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 302 | if (FD->getBody()) { |
| 303 | // If the function is a static, defer code generation until later so we can |
| 304 | // easily omit unused statics. |
| 305 | if (FD->getStorageClass() == FunctionDecl::Static) { |
Nate Begeman | 1a1d92a | 2008-04-20 20:38:08 +0000 | [diff] [blame^] | 306 | // We need to check the Module here to see if GetAddrOfFunctionDecl() has |
| 307 | // already added this function to the Module because the address of the |
| 308 | // function's prototype was taken. If this is the case, call |
| 309 | // GetAddrOfFunctionDecl to insert the static FunctionDecl into the used |
| 310 | // GlobalDeclsMap, so that EmitStatics will generate code for it later. |
| 311 | // |
| 312 | // Example: |
| 313 | // static int foo(); |
| 314 | // int bar() { return foo(); } |
| 315 | // static int foo() { return 5; } |
| 316 | if (getModule().getFunction(FD->getName())) |
| 317 | GetAddrOfFunctionDecl(FD, true); |
| 318 | |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 319 | StaticDecls.push_back(FD); |
| 320 | return; |
| 321 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 322 | CodeGenFunction(*this).GenerateCode(FD); |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | void CodeGenModule::EmitStatics() { |
| 327 | // Emit code for each used static decl encountered. Since a previously unused |
| 328 | // static decl may become used during the generation of code for a static |
| 329 | // function, iterate until no changes are made. |
| 330 | bool Changed; |
| 331 | do { |
| 332 | Changed = false; |
| 333 | for (unsigned i = 0, e = StaticDecls.size(); i != e; ++i) { |
| 334 | // Check the map of used decls for our static. If not found, continue. |
| 335 | const Decl *D = StaticDecls[i]; |
Nate Begeman | 1a1d92a | 2008-04-20 20:38:08 +0000 | [diff] [blame^] | 336 | if (!GlobalDeclMap.count(D)) |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 337 | continue; |
| 338 | |
| 339 | // If this is a function decl, generate code for the static function if it |
| 340 | // has a body. Otherwise, we must have a var decl for a static global |
| 341 | // variable. |
| 342 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 343 | if (FD->getBody()) |
| 344 | CodeGenFunction(*this).GenerateCode(FD); |
| 345 | } else { |
Nate Begeman | 1a1d92a | 2008-04-20 20:38:08 +0000 | [diff] [blame^] | 346 | EmitGlobalVarInit(cast<VarDecl>(D)); |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 347 | } |
| 348 | // Erase the used decl from the list. |
| 349 | StaticDecls[i] = StaticDecls.back(); |
| 350 | StaticDecls.pop_back(); |
| 351 | --i; |
| 352 | --e; |
| 353 | |
| 354 | // Remember that we made a change. |
| 355 | Changed = true; |
| 356 | } |
| 357 | } while (Changed); |
| 358 | |
| 359 | // Warn about all statics that are still unused at end of code generation. |
| 360 | for (unsigned i = 0, e = StaticDecls.size(); i != e; ++i) { |
Nate Begeman | 1a1d92a | 2008-04-20 20:38:08 +0000 | [diff] [blame^] | 361 | const NamedDecl *D = StaticDecls[i]; |
| 362 | std::string Msg = D->getName(); |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 363 | getDiags().Report(Context.getFullLoc(D->getLocation()), |
| 364 | diag::warn_unused_static, &Msg, 1); |
| 365 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 368 | llvm::Constant *CodeGenModule::EmitGlobalInit(const Expr *Expr) { |
| 369 | return EmitConstantExpr(Expr); |
Devang Patel | 9e32d4b | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 372 | /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the |
| 373 | /// annotation information for a given GlobalValue. The annotation struct is |
| 374 | /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the |
| 375 | /// GlobalValue being annotated. The second filed is thee constant string |
| 376 | /// created from the AnnotateAttr's annotation. The third field is a constant |
| 377 | /// string containing the name of the translation unit. The fourth field is |
| 378 | /// the line number in the file of the annotated value declaration. |
| 379 | /// |
| 380 | /// FIXME: this does not unique the annotation string constants, as llvm-gcc |
| 381 | /// appears to. |
| 382 | /// |
| 383 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
| 384 | const AnnotateAttr *AA, |
| 385 | unsigned LineNo) { |
| 386 | llvm::Module *M = &getModule(); |
| 387 | |
| 388 | // get [N x i8] constants for the annotation string, and the filename string |
| 389 | // which are the 2nd and 3rd elements of the global annotation structure. |
| 390 | const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 391 | llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); |
| 392 | llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), |
| 393 | true); |
| 394 | |
| 395 | // Get the two global values corresponding to the ConstantArrays we just |
| 396 | // created to hold the bytes of the strings. |
| 397 | llvm::GlobalValue *annoGV = |
| 398 | new llvm::GlobalVariable(anno->getType(), false, |
| 399 | llvm::GlobalValue::InternalLinkage, anno, |
| 400 | GV->getName() + ".str", M); |
| 401 | // translation unit name string, emitted into the llvm.metadata section. |
| 402 | llvm::GlobalValue *unitGV = |
| 403 | new llvm::GlobalVariable(unit->getType(), false, |
| 404 | llvm::GlobalValue::InternalLinkage, unit, ".str", M); |
| 405 | |
| 406 | // Create the ConstantStruct that is the global annotion. |
| 407 | llvm::Constant *Fields[4] = { |
| 408 | llvm::ConstantExpr::getBitCast(GV, SBP), |
| 409 | llvm::ConstantExpr::getBitCast(annoGV, SBP), |
| 410 | llvm::ConstantExpr::getBitCast(unitGV, SBP), |
| 411 | llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) |
| 412 | }; |
| 413 | return llvm::ConstantStruct::get(Fields, 4, false); |
| 414 | } |
| 415 | |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 416 | void CodeGenModule::EmitGlobalVar(const VarDecl *D) { |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 417 | // If the VarDecl is a static, defer code generation until later so we can |
| 418 | // easily omit unused statics. |
| 419 | if (D->getStorageClass() == VarDecl::Static) { |
| 420 | StaticDecls.push_back(D); |
| 421 | return; |
| 422 | } |
| 423 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 424 | // If this is just a forward declaration of the variable, don't emit it now, |
| 425 | // allow it to be emitted lazily on its first use. |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 426 | if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0) |
| 427 | return; |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 428 | |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 429 | EmitGlobalVarInit(D); |
| 430 | } |
| 431 | |
| 432 | void CodeGenModule::EmitGlobalVarInit(const VarDecl *D) { |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 433 | // Get the global, forcing it to be a direct reference. |
| 434 | llvm::GlobalVariable *GV = |
Chris Lattner | 2b9d2ca | 2007-12-18 08:16:44 +0000 | [diff] [blame] | 435 | cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, true)); |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 436 | |
| 437 | // Convert the initializer, or use zero if appropriate. |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 438 | llvm::Constant *Init = 0; |
| 439 | if (D->getInit() == 0) { |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 440 | Init = llvm::Constant::getNullValue(GV->getType()->getElementType()); |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 441 | } else if (D->getType()->isIntegerType()) { |
Hartmut Kaiser | 7b66000 | 2007-10-17 15:00:17 +0000 | [diff] [blame] | 442 | llvm::APSInt Value(static_cast<uint32_t>( |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 443 | getContext().getTypeSize(D->getInit()->getType()))); |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 444 | if (D->getInit()->isIntegerConstantExpr(Value, Context)) |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 445 | Init = llvm::ConstantInt::get(Value); |
| 446 | } |
Devang Patel | 8e53e72 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 447 | |
Devang Patel | 9e32d4b | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 448 | if (!Init) |
Oliver Hunt | 2824723 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 449 | Init = EmitGlobalInit(D->getInit()); |
Devang Patel | 8e53e72 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 450 | |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 451 | if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { |
| 452 | SourceManager &SM = Context.getSourceManager(); |
| 453 | AddAnnotation(EmitAnnotateAttr(GV, AA, |
| 454 | SM.getLogicalLineNumber(D->getLocation()))); |
| 455 | } |
| 456 | |
Chris Lattner | f89dfb2 | 2007-12-10 00:05:55 +0000 | [diff] [blame] | 457 | assert(GV->getType()->getElementType() == Init->getType() && |
| 458 | "Initializer codegen type mismatch!"); |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 459 | GV->setInitializer(Init); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 460 | |
| 461 | if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) |
| 462 | GV->setVisibility(attr->getVisibility()); |
| 463 | // FIXME: else handle -fvisibility |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 464 | |
| 465 | // Set the llvm linkage type as appropriate. |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 466 | if (D->getAttr<DLLImportAttr>()) |
| 467 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
| 468 | else if (D->getAttr<DLLExportAttr>()) |
| 469 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
| 470 | else if (D->getAttr<WeakAttr>()) { |
| 471 | GV->setLinkage(llvm::GlobalVariable::WeakLinkage); |
| 472 | |
| 473 | } else { |
| 474 | // FIXME: This isn't right. This should handle common linkage and other |
| 475 | // stuff. |
| 476 | switch (D->getStorageClass()) { |
| 477 | case VarDecl::Auto: |
| 478 | case VarDecl::Register: |
| 479 | assert(0 && "Can't have auto or register globals"); |
| 480 | case VarDecl::None: |
| 481 | if (!D->getInit()) |
| 482 | GV->setLinkage(llvm::GlobalVariable::WeakLinkage); |
| 483 | break; |
| 484 | case VarDecl::Extern: |
| 485 | case VarDecl::PrivateExtern: |
| 486 | // todo: common |
| 487 | break; |
| 488 | case VarDecl::Static: |
| 489 | GV->setLinkage(llvm::GlobalVariable::InternalLinkage); |
| 490 | break; |
| 491 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 494 | |
Chris Lattner | 32b266c | 2007-07-14 00:16:50 +0000 | [diff] [blame] | 495 | /// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified |
| 496 | /// declarator chain. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 497 | void CodeGenModule::EmitGlobalVarDeclarator(const VarDecl *D) { |
| 498 | for (; D; D = cast_or_null<VarDecl>(D->getNextDeclarator())) |
| 499 | if (D->isFileVarDecl()) |
| 500 | EmitGlobalVar(D); |
Chris Lattner | 32b266c | 2007-07-14 00:16:50 +0000 | [diff] [blame] | 501 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 502 | |
Chris Lattner | c5b8806 | 2008-02-06 05:08:19 +0000 | [diff] [blame] | 503 | void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { |
| 504 | // Make sure that this type is translated. |
| 505 | Types.UpdateCompletedType(TD); |
Chris Lattner | d86e6bc | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 509 | /// getBuiltinLibFunction |
| 510 | llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { |
Chris Lattner | 1426fec | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 511 | if (BuiltinID > BuiltinFunctions.size()) |
| 512 | BuiltinFunctions.resize(BuiltinID); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 513 | |
Chris Lattner | 1426fec | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 514 | // Cache looked up functions. Since builtin id #0 is invalid we don't reserve |
| 515 | // a slot for it. |
| 516 | assert(BuiltinID && "Invalid Builtin ID"); |
| 517 | llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 518 | if (FunctionSlot) |
| 519 | return FunctionSlot; |
| 520 | |
| 521 | assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn"); |
| 522 | |
| 523 | // Get the name, skip over the __builtin_ prefix. |
| 524 | const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10; |
| 525 | |
| 526 | // Get the type for the builtin. |
| 527 | QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context); |
| 528 | const llvm::FunctionType *Ty = |
| 529 | cast<llvm::FunctionType>(getTypes().ConvertType(Type)); |
| 530 | |
| 531 | // FIXME: This has a serious problem with code like this: |
| 532 | // void abs() {} |
| 533 | // ... __builtin_abs(x); |
| 534 | // The two versions of abs will collide. The fix is for the builtin to win, |
| 535 | // and for the existing one to be turned into a constantexpr cast of the |
| 536 | // builtin. In the case where the existing one is a static function, it |
| 537 | // should just be renamed. |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 538 | if (llvm::Function *Existing = getModule().getFunction(Name)) { |
| 539 | if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) |
| 540 | return FunctionSlot = Existing; |
| 541 | assert(Existing == 0 && "FIXME: Name collision"); |
| 542 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 543 | |
| 544 | // FIXME: param attributes for sext/zext etc. |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 545 | return FunctionSlot = |
| 546 | llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name, |
| 547 | &getModule()); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 550 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, |
| 551 | unsigned NumTys) { |
| 552 | return llvm::Intrinsic::getDeclaration(&getModule(), |
| 553 | (llvm::Intrinsic::ID)IID, Tys, NumTys); |
| 554 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 555 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 556 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 557 | if (MemCpyFn) return MemCpyFn; |
| 558 | llvm::Intrinsic::ID IID; |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 559 | switch (Context.Target.getPointerWidth(0)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 560 | default: assert(0 && "Unknown ptr width"); |
| 561 | case 32: IID = llvm::Intrinsic::memcpy_i32; break; |
| 562 | case 64: IID = llvm::Intrinsic::memcpy_i64; break; |
| 563 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 564 | return MemCpyFn = getIntrinsic(IID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 565 | } |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 566 | |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 567 | llvm::Function *CodeGenModule::getMemSetFn() { |
| 568 | if (MemSetFn) return MemSetFn; |
| 569 | llvm::Intrinsic::ID IID; |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 570 | switch (Context.Target.getPointerWidth(0)) { |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 571 | default: assert(0 && "Unknown ptr width"); |
| 572 | case 32: IID = llvm::Intrinsic::memset_i32; break; |
| 573 | case 64: IID = llvm::Intrinsic::memset_i64; break; |
| 574 | } |
| 575 | return MemSetFn = getIntrinsic(IID); |
| 576 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 577 | |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 578 | llvm::Constant *CodeGenModule:: |
| 579 | GetAddrOfConstantCFString(const std::string &str) { |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 580 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 581 | CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 582 | |
| 583 | if (Entry.getValue()) |
| 584 | return Entry.getValue(); |
| 585 | |
| 586 | std::vector<llvm::Constant*> Fields; |
| 587 | |
| 588 | if (!CFConstantStringClassRef) { |
| 589 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 590 | Ty = llvm::ArrayType::get(Ty, 0); |
| 591 | |
| 592 | CFConstantStringClassRef = |
| 593 | new llvm::GlobalVariable(Ty, false, |
| 594 | llvm::GlobalVariable::ExternalLinkage, 0, |
| 595 | "__CFConstantStringClassReference", |
| 596 | &getModule()); |
| 597 | } |
| 598 | |
| 599 | // Class pointer. |
| 600 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 601 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 602 | llvm::Constant *C = |
| 603 | llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2); |
| 604 | Fields.push_back(C); |
| 605 | |
| 606 | // Flags. |
| 607 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 608 | Fields.push_back(llvm::ConstantInt::get(Ty, 1992)); |
| 609 | |
| 610 | // String pointer. |
| 611 | C = llvm::ConstantArray::get(str); |
| 612 | C = new llvm::GlobalVariable(C->getType(), true, |
| 613 | llvm::GlobalValue::InternalLinkage, |
| 614 | C, ".str", &getModule()); |
| 615 | |
| 616 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 617 | Fields.push_back(C); |
| 618 | |
| 619 | // String length. |
| 620 | Ty = getTypes().ConvertType(getContext().LongTy); |
| 621 | Fields.push_back(llvm::ConstantInt::get(Ty, str.length())); |
| 622 | |
| 623 | // The struct. |
| 624 | Ty = getTypes().ConvertType(getContext().getCFConstantStringType()); |
| 625 | C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields); |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 626 | llvm::GlobalVariable *GV = |
| 627 | new llvm::GlobalVariable(C->getType(), true, |
| 628 | llvm::GlobalVariable::InternalLinkage, |
| 629 | C, "", &getModule()); |
| 630 | GV->setSection("__DATA,__cfstring"); |
| 631 | Entry.setValue(GV); |
| 632 | return GV; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 633 | } |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 634 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 635 | /// GenerateWritableString -- Creates storage for a string literal. |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 636 | static llvm::Constant *GenerateStringLiteral(const std::string &str, |
| 637 | bool constant, |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 638 | CodeGenModule &CGM) { |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 639 | // Create Constant for this string literal |
| 640 | llvm::Constant *C=llvm::ConstantArray::get(str); |
| 641 | |
| 642 | // Create a global variable for this string |
| 643 | C = new llvm::GlobalVariable(C->getType(), constant, |
| 644 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 645 | C, ".str", &CGM.getModule()); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 646 | return C; |
| 647 | } |
| 648 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 649 | /// CodeGenModule::GetAddrOfConstantString -- returns a pointer to the character |
| 650 | /// array containing the literal. The result is pointer to array type. |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 651 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str) { |
| 652 | // Don't share any string literals if writable-strings is turned on. |
| 653 | if (Features.WritableStrings) |
| 654 | return GenerateStringLiteral(str, false, *this); |
| 655 | |
| 656 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 657 | ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 658 | |
| 659 | if (Entry.getValue()) |
| 660 | return Entry.getValue(); |
| 661 | |
| 662 | // Create a global variable for this. |
| 663 | llvm::Constant *C = GenerateStringLiteral(str, true, *this); |
| 664 | Entry.setValue(C); |
| 665 | return C; |
| 666 | } |