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