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