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" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 17 | #include "CGCall.h" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 18 | #include "CGObjCRuntime.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Nate Begeman | ec9426c | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 25 | #include "llvm/CallingConv.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" |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
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, |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 35 | Diagnostic &diags, bool GenerateDebugInfo) |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 36 | : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags), |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 37 | Types(C, M, TD), Runtime(0), MemCpyFn(0), MemMoveFn(0), MemSetFn(0), |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 38 | CFConstantStringClassRef(0) { |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 39 | |
| 40 | if (Features.ObjC1) { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 41 | if (Features.NeXTRuntime) { |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 42 | Runtime = CreateMacObjCRuntime(*this); |
| 43 | } else { |
| 44 | Runtime = CreateGNUObjCRuntime(*this); |
| 45 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 46 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 47 | |
| 48 | // If debug info generation is enabled, create the CGDebugInfo object. |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 49 | DebugInfo = GenerateDebugInfo ? new CGDebugInfo(this) : 0; |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | CodeGenModule::~CodeGenModule() { |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 53 | delete Runtime; |
| 54 | delete DebugInfo; |
| 55 | } |
| 56 | |
| 57 | void CodeGenModule::Release() { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 58 | EmitStatics(); |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 59 | EmitAliases(); |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 60 | if (Runtime) |
| 61 | if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) |
| 62 | AddGlobalCtor(ObjCInitFunction); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 63 | EmitCtorList(GlobalCtors, "llvm.global_ctors"); |
| 64 | EmitCtorList(GlobalDtors, "llvm.global_dtors"); |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 65 | EmitAnnotations(); |
Daniel Dunbar | f1968f2 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 66 | BindRuntimeFunctions(); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 67 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | |
Daniel Dunbar | f1968f2 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 69 | void CodeGenModule::BindRuntimeFunctions() { |
| 70 | // Deal with protecting runtime function names. |
| 71 | for (unsigned i = 0, e = RuntimeFunctions.size(); i < e; ++i) { |
| 72 | llvm::Function *Fn = RuntimeFunctions[i].first; |
| 73 | const std::string &Name = RuntimeFunctions[i].second; |
| 74 | |
| 75 | // See if there is a conflict against a function. |
| 76 | llvm::Function *Conflict = TheModule.getFunction(Name); |
| 77 | if (Conflict) { |
| 78 | // Decide which version to take. If the conflict is a definition |
| 79 | // we are forced to take that, otherwise assume the runtime |
| 80 | // knows best. |
| 81 | if (!Conflict->isDeclaration()) { |
| 82 | llvm::Value *Casted = |
| 83 | llvm::ConstantExpr::getBitCast(Conflict, Fn->getType()); |
| 84 | Fn->replaceAllUsesWith(Casted); |
| 85 | Fn->eraseFromParent(); |
| 86 | } else { |
| 87 | Fn->takeName(Conflict); |
| 88 | llvm::Value *Casted = |
| 89 | llvm::ConstantExpr::getBitCast(Fn, Conflict->getType()); |
| 90 | Conflict->replaceAllUsesWith(Casted); |
| 91 | Conflict->eraseFromParent(); |
| 92 | } |
| 93 | } else { |
| 94 | // FIXME: There still may be conflicts with aliases and |
| 95 | // variables. |
| 96 | Fn->setName(Name); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 101 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 102 | /// specified stmt yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 103 | void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, |
| 104 | bool OmitOnError) { |
| 105 | if (OmitOnError && getDiags().hasErrorOccurred()) |
| 106 | return; |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 107 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 108 | "cannot codegen this %0 yet"); |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 109 | std::string Msg = Type; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 110 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) |
| 111 | << Msg << S->getSourceRange(); |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 112 | } |
Chris Lattner | 58c3f9e | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 113 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 114 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 115 | /// specified decl yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 116 | void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, |
| 117 | bool OmitOnError) { |
| 118 | if (OmitOnError && getDiags().hasErrorOccurred()) |
| 119 | return; |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 120 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 121 | "cannot codegen this %0 yet"); |
| 122 | std::string Msg = Type; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 123 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 126 | /// setGlobalVisibility - Set the visibility for the given LLVM |
| 127 | /// GlobalValue according to the given clang AST visibility value. |
| 128 | static void setGlobalVisibility(llvm::GlobalValue *GV, |
| 129 | VisibilityAttr::VisibilityTypes Vis) { |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 130 | switch (Vis) { |
| 131 | default: assert(0 && "Unknown visibility!"); |
| 132 | case VisibilityAttr::DefaultVisibility: |
| 133 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
| 134 | break; |
| 135 | case VisibilityAttr::HiddenVisibility: |
| 136 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 137 | break; |
| 138 | case VisibilityAttr::ProtectedVisibility: |
| 139 | GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 144 | /// AddGlobalCtor - Add a function to the list that will be called before |
| 145 | /// main() runs. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 146 | void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 147 | // TODO: Type coercion of void()* types. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 148 | GlobalCtors.push_back(std::make_pair(Ctor, Priority)); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 151 | /// AddGlobalDtor - Add a function to the list that will be called |
| 152 | /// when the module is unloaded. |
| 153 | void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { |
| 154 | // TODO: Type coercion of void()* types. |
| 155 | GlobalDtors.push_back(std::make_pair(Dtor, Priority)); |
| 156 | } |
| 157 | |
| 158 | void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { |
| 159 | // Ctor function type is void()*. |
| 160 | llvm::FunctionType* CtorFTy = |
| 161 | llvm::FunctionType::get(llvm::Type::VoidTy, |
| 162 | std::vector<const llvm::Type*>(), |
| 163 | false); |
| 164 | llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); |
| 165 | |
| 166 | // Get the type of a ctor entry, { i32, void ()* }. |
Chris Lattner | 572cf09 | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 167 | llvm::StructType* CtorStructTy = |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 168 | llvm::StructType::get(llvm::Type::Int32Ty, |
| 169 | llvm::PointerType::getUnqual(CtorFTy), NULL); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 170 | |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 171 | // Construct the constructor and destructor arrays. |
| 172 | std::vector<llvm::Constant*> Ctors; |
| 173 | for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
| 174 | std::vector<llvm::Constant*> S; |
| 175 | S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false)); |
| 176 | S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); |
| 177 | Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 178 | } |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 179 | |
| 180 | if (!Ctors.empty()) { |
| 181 | llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); |
| 182 | new llvm::GlobalVariable(AT, false, |
| 183 | llvm::GlobalValue::AppendingLinkage, |
| 184 | llvm::ConstantArray::get(AT, Ctors), |
| 185 | GlobalName, |
| 186 | &TheModule); |
| 187 | } |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 190 | void CodeGenModule::EmitAnnotations() { |
| 191 | if (Annotations.empty()) |
| 192 | return; |
| 193 | |
| 194 | // Create a new global variable for the ConstantStruct in the Module. |
| 195 | llvm::Constant *Array = |
| 196 | llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), |
| 197 | Annotations.size()), |
| 198 | Annotations); |
| 199 | llvm::GlobalValue *gv = |
| 200 | new llvm::GlobalVariable(Array->getType(), false, |
| 201 | llvm::GlobalValue::AppendingLinkage, Array, |
| 202 | "llvm.global.annotations", &TheModule); |
| 203 | gv->setSection("llvm.metadata"); |
| 204 | } |
| 205 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 206 | static void SetGlobalValueAttributes(const Decl *D, |
| 207 | bool IsInternal, |
| 208 | bool IsInline, |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 209 | llvm::GlobalValue *GV, |
| 210 | bool ForDefinition) { |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 211 | // TODO: Set up linkage and many other things. Note, this is a simple |
| 212 | // approximation of what we really want. |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 213 | if (!ForDefinition) { |
| 214 | // Only a few attributes are set on declarations. |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 215 | if (D->getAttr<DLLImportAttr>()) |
| 216 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 217 | } else { |
| 218 | if (IsInternal) { |
| 219 | GV->setLinkage(llvm::Function::InternalLinkage); |
| 220 | } else { |
| 221 | if (D->getAttr<DLLImportAttr>()) |
| 222 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
| 223 | else if (D->getAttr<DLLExportAttr>()) |
| 224 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
| 225 | else if (D->getAttr<WeakAttr>() || IsInline) |
| 226 | GV->setLinkage(llvm::Function::WeakLinkage); |
| 227 | } |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 228 | } |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 229 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 230 | if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 231 | setGlobalVisibility(GV, attr->getVisibility()); |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 232 | // FIXME: else handle -fvisibility |
Daniel Dunbar | a735ad8 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 233 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 234 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
Daniel Dunbar | a735ad8 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 235 | // Prefaced with special LLVM marker to indicate that the name |
| 236 | // should not be munged. |
| 237 | GV->setName("\01" + ALA->getLabel()); |
| 238 | } |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 241 | void CodeGenModule::SetFunctionAttributes(const Decl *D, |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 242 | const CGFunctionInfo &Info, |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 243 | llvm::Function *F) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 244 | AttributeListType AttributeList; |
| 245 | ConstructAttributeList(D, Info.argtypes_begin(), Info.argtypes_end(), |
| 246 | AttributeList); |
Eli Friedman | c134fcb | 2008-06-04 19:41:28 +0000 | [diff] [blame] | 247 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 248 | F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), |
| 249 | AttributeList.size())); |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 250 | |
| 251 | // Set the appropriate calling convention for the Function. |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 252 | if (D->getAttr<FastCallAttr>()) |
Anton Korobeynikov | f1c9c09 | 2008-11-11 20:21:14 +0000 | [diff] [blame] | 253 | F->setCallingConv(llvm::CallingConv::X86_FastCall); |
| 254 | |
| 255 | if (D->getAttr<StdCallAttr>()) |
| 256 | F->setCallingConv(llvm::CallingConv::X86_StdCall); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /// SetFunctionAttributesForDefinition - Set function attributes |
| 260 | /// specific to a function definition. |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 261 | void CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D, |
| 262 | llvm::Function *F) { |
| 263 | if (isa<ObjCMethodDecl>(D)) { |
| 264 | SetGlobalValueAttributes(D, true, false, F, true); |
| 265 | } else { |
| 266 | const FunctionDecl *FD = cast<FunctionDecl>(D); |
| 267 | SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, |
| 268 | FD->isInline(), F, true); |
| 269 | } |
| 270 | |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 271 | if (!Features.Exceptions) |
Daniel Dunbar | f93349f | 2008-09-27 07:16:42 +0000 | [diff] [blame] | 272 | F->addFnAttr(llvm::Attribute::NoUnwind); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 273 | |
| 274 | if (D->getAttr<AlwaysInlineAttr>()) |
| 275 | F->addFnAttr(llvm::Attribute::AlwaysInline); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | void CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD, |
| 279 | llvm::Function *F) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 280 | SetFunctionAttributes(MD, CGFunctionInfo(MD, Context), F); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 281 | |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 282 | SetFunctionAttributesForDefinition(MD, F); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, |
| 286 | llvm::Function *F) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 287 | SetFunctionAttributes(FD, CGFunctionInfo(FD), F); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 288 | |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 289 | SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, |
| 290 | FD->isInline(), F, false); |
| 291 | } |
| 292 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 293 | |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 294 | void CodeGenModule::EmitAliases() { |
| 295 | for (unsigned i = 0, e = Aliases.size(); i != e; ++i) { |
| 296 | const FunctionDecl *D = Aliases[i]; |
| 297 | const AliasAttr *AA = D->getAttr<AliasAttr>(); |
| 298 | |
| 299 | // This is something of a hack, if the FunctionDecl got overridden |
| 300 | // then its attributes will be moved to the new declaration. In |
| 301 | // this case the current decl has no alias attribute, but we will |
| 302 | // eventually see it. |
| 303 | if (!AA) |
| 304 | continue; |
| 305 | |
| 306 | const std::string& aliaseeName = AA->getAliasee(); |
| 307 | llvm::Function *aliasee = getModule().getFunction(aliaseeName); |
| 308 | if (!aliasee) { |
| 309 | // FIXME: This isn't unsupported, this is just an error, which |
| 310 | // sema should catch, but... |
| 311 | ErrorUnsupported(D, "alias referencing a missing function"); |
| 312 | continue; |
| 313 | } |
| 314 | |
| 315 | llvm::GlobalValue *GA = |
| 316 | new llvm::GlobalAlias(aliasee->getType(), |
| 317 | llvm::Function::ExternalLinkage, |
| 318 | D->getName(), |
| 319 | aliasee, |
| 320 | &getModule()); |
| 321 | |
| 322 | llvm::GlobalValue *&Entry = GlobalDeclMap[D->getIdentifier()]; |
| 323 | if (Entry) { |
| 324 | // If we created a dummy function for this then replace it. |
| 325 | GA->takeName(Entry); |
| 326 | |
| 327 | llvm::Value *Casted = |
| 328 | llvm::ConstantExpr::getBitCast(GA, Entry->getType()); |
| 329 | Entry->replaceAllUsesWith(Casted); |
| 330 | Entry->eraseFromParent(); |
| 331 | |
| 332 | Entry = GA; |
| 333 | } |
| 334 | |
| 335 | // Alias should never be internal or inline. |
| 336 | SetGlobalValueAttributes(D, false, false, GA, true); |
| 337 | } |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 340 | void CodeGenModule::EmitStatics() { |
| 341 | // Emit code for each used static decl encountered. Since a previously unused |
| 342 | // static decl may become used during the generation of code for a static |
| 343 | // function, iterate until no changes are made. |
| 344 | bool Changed; |
| 345 | do { |
| 346 | Changed = false; |
| 347 | for (unsigned i = 0, e = StaticDecls.size(); i != e; ++i) { |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 348 | const ValueDecl *D = StaticDecls[i]; |
Eli Friedman | 6f7e2ee | 2008-05-27 04:58:01 +0000 | [diff] [blame] | 349 | |
| 350 | // Check if we have used a decl with the same name |
| 351 | // FIXME: The AST should have some sort of aggregate decls or |
| 352 | // global symbol map. |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 353 | // FIXME: This is missing some important cases. For example, we |
| 354 | // need to check for uses in an alias and in a constructor. |
Daniel Dunbar | 90db882 | 2008-08-25 06:18:57 +0000 | [diff] [blame] | 355 | if (!GlobalDeclMap.count(D->getIdentifier())) |
Daniel Dunbar | a735ad8 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 356 | continue; |
Eli Friedman | 6f7e2ee | 2008-05-27 04:58:01 +0000 | [diff] [blame] | 357 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 358 | // Emit the definition. |
| 359 | EmitGlobalDefinition(D); |
| 360 | |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 361 | // Erase the used decl from the list. |
| 362 | StaticDecls[i] = StaticDecls.back(); |
| 363 | StaticDecls.pop_back(); |
| 364 | --i; |
| 365 | --e; |
| 366 | |
| 367 | // Remember that we made a change. |
| 368 | Changed = true; |
| 369 | } |
| 370 | } while (Changed); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 373 | /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the |
| 374 | /// annotation information for a given GlobalValue. The annotation struct is |
| 375 | /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 376 | /// GlobalValue being annotated. The second field is the constant string |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 377 | /// created from the AnnotateAttr's annotation. The third field is a constant |
| 378 | /// string containing the name of the translation unit. The fourth field is |
| 379 | /// the line number in the file of the annotated value declaration. |
| 380 | /// |
| 381 | /// FIXME: this does not unique the annotation string constants, as llvm-gcc |
| 382 | /// appears to. |
| 383 | /// |
| 384 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
| 385 | const AnnotateAttr *AA, |
| 386 | unsigned LineNo) { |
| 387 | llvm::Module *M = &getModule(); |
| 388 | |
| 389 | // get [N x i8] constants for the annotation string, and the filename string |
| 390 | // which are the 2nd and 3rd elements of the global annotation structure. |
| 391 | const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 392 | llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); |
| 393 | llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), |
| 394 | true); |
| 395 | |
| 396 | // Get the two global values corresponding to the ConstantArrays we just |
| 397 | // created to hold the bytes of the strings. |
| 398 | llvm::GlobalValue *annoGV = |
| 399 | new llvm::GlobalVariable(anno->getType(), false, |
| 400 | llvm::GlobalValue::InternalLinkage, anno, |
| 401 | GV->getName() + ".str", M); |
| 402 | // translation unit name string, emitted into the llvm.metadata section. |
| 403 | llvm::GlobalValue *unitGV = |
| 404 | new llvm::GlobalVariable(unit->getType(), false, |
| 405 | llvm::GlobalValue::InternalLinkage, unit, ".str", M); |
| 406 | |
| 407 | // Create the ConstantStruct that is the global annotion. |
| 408 | llvm::Constant *Fields[4] = { |
| 409 | llvm::ConstantExpr::getBitCast(GV, SBP), |
| 410 | llvm::ConstantExpr::getBitCast(annoGV, SBP), |
| 411 | llvm::ConstantExpr::getBitCast(unitGV, SBP), |
| 412 | llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) |
| 413 | }; |
| 414 | return llvm::ConstantStruct::get(Fields, 4, false); |
| 415 | } |
| 416 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 417 | void CodeGenModule::EmitGlobal(const ValueDecl *Global) { |
| 418 | bool isDef, isStatic; |
| 419 | |
| 420 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 421 | // Aliases are deferred until code for everything else has been |
| 422 | // emitted. |
| 423 | if (FD->getAttr<AliasAttr>()) { |
| 424 | assert(!FD->isThisDeclarationADefinition() && |
| 425 | "Function alias cannot have a definition!"); |
| 426 | Aliases.push_back(FD); |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | isDef = FD->isThisDeclarationADefinition(); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 431 | isStatic = FD->getStorageClass() == FunctionDecl::Static; |
| 432 | } else if (const VarDecl *VD = cast<VarDecl>(Global)) { |
| 433 | assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); |
| 434 | |
| 435 | isDef = !(VD->getStorageClass() == VarDecl::Extern && VD->getInit() == 0); |
| 436 | isStatic = VD->getStorageClass() == VarDecl::Static; |
| 437 | } else { |
| 438 | assert(0 && "Invalid argument to EmitGlobal"); |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 439 | return; |
| 440 | } |
| 441 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 442 | // Forward declarations are emitted lazily on first use. |
| 443 | if (!isDef) |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 444 | return; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 445 | |
| 446 | // If the global is a static, defer code generation until later so |
| 447 | // we can easily omit unused statics. |
| 448 | if (isStatic) { |
| 449 | StaticDecls.push_back(Global); |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | // Otherwise emit the definition. |
| 454 | EmitGlobalDefinition(Global); |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 457 | void CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { |
| 458 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 459 | EmitGlobalFunctionDefinition(FD); |
| 460 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 461 | EmitGlobalVarDefinition(VD); |
| 462 | } else { |
| 463 | assert(0 && "Invalid argument to EmitGlobalDefinition()"); |
| 464 | } |
| 465 | } |
| 466 | |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 467 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D) { |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 468 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 469 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 470 | QualType ASTTy = D->getType(); |
| 471 | const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 472 | const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 473 | |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 474 | // Lookup the entry, lazily creating it if necessary. |
Daniel Dunbar | 90db882 | 2008-08-25 06:18:57 +0000 | [diff] [blame] | 475 | llvm::GlobalValue *&Entry = GlobalDeclMap[D->getIdentifier()]; |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 476 | if (!Entry) |
| 477 | Entry = new llvm::GlobalVariable(Ty, false, |
| 478 | llvm::GlobalValue::ExternalLinkage, |
| 479 | 0, D->getName(), &getModule(), 0, |
| 480 | ASTTy.getAddressSpace()); |
| 481 | |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 482 | // Make sure the result is of the correct type. |
| 483 | return llvm::ConstantExpr::getBitCast(Entry, PTy); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 487 | llvm::Constant *Init = 0; |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 488 | QualType ASTTy = D->getType(); |
| 489 | const llvm::Type *VarTy = getTypes().ConvertTypeForMem(ASTTy); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 490 | |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 491 | if (D->getInit() == 0) { |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 492 | // This is a tentative definition; tentative definitions are |
| 493 | // implicitly initialized with { 0 } |
| 494 | const llvm::Type* InitTy; |
| 495 | if (ASTTy->isIncompleteArrayType()) { |
| 496 | // An incomplete array is normally [ TYPE x 0 ], but we need |
| 497 | // to fix it to [ TYPE x 1 ]. |
| 498 | const llvm::ArrayType* ATy = cast<llvm::ArrayType>(VarTy); |
| 499 | InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); |
| 500 | } else { |
| 501 | InitTy = VarTy; |
| 502 | } |
| 503 | Init = llvm::Constant::getNullValue(InitTy); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 504 | } else { |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 505 | Init = EmitConstantExpr(D->getInit()); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 506 | } |
| 507 | const llvm::Type* InitType = Init->getType(); |
| 508 | |
Daniel Dunbar | 90db882 | 2008-08-25 06:18:57 +0000 | [diff] [blame] | 509 | llvm::GlobalValue *&Entry = GlobalDeclMap[D->getIdentifier()]; |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 510 | llvm::GlobalVariable *GV = cast_or_null<llvm::GlobalVariable>(Entry); |
| 511 | |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 512 | if (!GV) { |
| 513 | GV = new llvm::GlobalVariable(InitType, false, |
| 514 | llvm::GlobalValue::ExternalLinkage, |
| 515 | 0, D->getName(), &getModule(), 0, |
| 516 | ASTTy.getAddressSpace()); |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 517 | } else if (GV->getType() != |
| 518 | llvm::PointerType::get(InitType, ASTTy.getAddressSpace())) { |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 519 | // We have a definition after a prototype with the wrong type. |
| 520 | // We must make a new GlobalVariable* and update everything that used OldGV |
| 521 | // (a declaration or tentative definition) with the new GlobalVariable* |
| 522 | // (which will be a definition). |
| 523 | // |
| 524 | // This happens if there is a prototype for a global (e.g. "extern int x[];") |
| 525 | // and then a definition of a different type (e.g. "int x[10];"). This also |
| 526 | // happens when an initializer has a different type from the type of the |
| 527 | // global (this happens with unions). |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 528 | // |
| 529 | // FIXME: This also ends up happening if there's a definition followed by |
| 530 | // a tentative definition! (Although Sema rejects that construct |
| 531 | // at the moment.) |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 532 | |
| 533 | // Save the old global |
| 534 | llvm::GlobalVariable *OldGV = GV; |
| 535 | |
| 536 | // Make a new global with the correct type |
| 537 | GV = new llvm::GlobalVariable(InitType, false, |
| 538 | llvm::GlobalValue::ExternalLinkage, |
| 539 | 0, D->getName(), &getModule(), 0, |
| 540 | ASTTy.getAddressSpace()); |
| 541 | // Steal the name of the old global |
| 542 | GV->takeName(OldGV); |
| 543 | |
| 544 | // Replace all uses of the old global with the new global |
| 545 | llvm::Constant *NewPtrForOldDecl = |
| 546 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 547 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 548 | |
| 549 | // Erase the old global, since it is no longer used. |
| 550 | OldGV->eraseFromParent(); |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 551 | } |
Devang Patel | 8e53e72 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 552 | |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 553 | Entry = GV; |
Devang Patel | 8e53e72 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 554 | |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 555 | if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { |
| 556 | SourceManager &SM = Context.getSourceManager(); |
| 557 | AddAnnotation(EmitAnnotateAttr(GV, AA, |
| 558 | SM.getLogicalLineNumber(D->getLocation()))); |
| 559 | } |
| 560 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 561 | GV->setInitializer(Init); |
Nuno Lopes | b381aac | 2008-09-01 11:33:04 +0000 | [diff] [blame] | 562 | GV->setConstant(D->getType().isConstant(Context)); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 563 | |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 564 | // FIXME: This is silly; getTypeAlign should just work for incomplete arrays |
| 565 | unsigned Align; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 566 | if (const IncompleteArrayType* IAT = |
| 567 | Context.getAsIncompleteArrayType(D->getType())) |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 568 | Align = Context.getTypeAlign(IAT->getElementType()); |
| 569 | else |
| 570 | Align = Context.getTypeAlign(D->getType()); |
Eli Friedman | 08d7802 | 2008-05-29 11:10:27 +0000 | [diff] [blame] | 571 | if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) { |
| 572 | Align = std::max(Align, AA->getAlignment()); |
| 573 | } |
| 574 | GV->setAlignment(Align / 8); |
| 575 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 576 | if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 577 | setGlobalVisibility(GV, attr->getVisibility()); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 578 | // FIXME: else handle -fvisibility |
Daniel Dunbar | a735ad8 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 579 | |
| 580 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
| 581 | // Prefaced with special LLVM marker to indicate that the name |
| 582 | // should not be munged. |
| 583 | GV->setName("\01" + ALA->getLabel()); |
| 584 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 585 | |
| 586 | // Set the llvm linkage type as appropriate. |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 587 | if (D->getStorageClass() == VarDecl::Static) |
| 588 | GV->setLinkage(llvm::Function::InternalLinkage); |
| 589 | else if (D->getAttr<DLLImportAttr>()) |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 590 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
| 591 | else if (D->getAttr<DLLExportAttr>()) |
| 592 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 593 | else if (D->getAttr<WeakAttr>()) |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 594 | GV->setLinkage(llvm::GlobalVariable::WeakLinkage); |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 595 | else { |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 596 | // FIXME: This isn't right. This should handle common linkage and other |
| 597 | // stuff. |
| 598 | switch (D->getStorageClass()) { |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 599 | case VarDecl::Static: assert(0 && "This case handled above"); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 600 | case VarDecl::Auto: |
| 601 | case VarDecl::Register: |
| 602 | assert(0 && "Can't have auto or register globals"); |
| 603 | case VarDecl::None: |
| 604 | if (!D->getInit()) |
Eli Friedman | a07b764 | 2008-05-29 11:03:17 +0000 | [diff] [blame] | 605 | GV->setLinkage(llvm::GlobalVariable::CommonLinkage); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 606 | break; |
| 607 | case VarDecl::Extern: |
| 608 | case VarDecl::PrivateExtern: |
| 609 | // todo: common |
| 610 | break; |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 611 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 612 | } |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 613 | |
| 614 | // Emit global variable debug information. |
| 615 | CGDebugInfo *DI = getDebugInfo(); |
| 616 | if(DI) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 617 | DI->setLocation(D->getLocation()); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 618 | DI->EmitGlobalVariable(GV, D); |
| 619 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 620 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 621 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 622 | llvm::GlobalValue * |
| 623 | CodeGenModule::EmitForwardFunctionDefinition(const FunctionDecl *D) { |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 624 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 625 | llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), |
| 626 | llvm::Function::ExternalLinkage, |
| 627 | D->getName(), &getModule()); |
| 628 | SetFunctionAttributes(D, F); |
| 629 | return F; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | llvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D) { |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 633 | QualType ASTTy = D->getType(); |
| 634 | const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); |
| 635 | const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 636 | |
| 637 | // Lookup the entry, lazily creating it if necessary. |
Daniel Dunbar | 90db882 | 2008-08-25 06:18:57 +0000 | [diff] [blame] | 638 | llvm::GlobalValue *&Entry = GlobalDeclMap[D->getIdentifier()]; |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 639 | if (!Entry) |
| 640 | Entry = EmitForwardFunctionDefinition(D); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 641 | |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 642 | return llvm::ConstantExpr::getBitCast(Entry, PTy); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | void CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { |
Daniel Dunbar | 90db882 | 2008-08-25 06:18:57 +0000 | [diff] [blame] | 646 | llvm::GlobalValue *&Entry = GlobalDeclMap[D->getIdentifier()]; |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 647 | if (!Entry) { |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 648 | Entry = EmitForwardFunctionDefinition(D); |
| 649 | } else { |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 650 | // If the types mismatch then we have to rewrite the definition. |
| 651 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 652 | if (Entry->getType() != llvm::PointerType::getUnqual(Ty)) { |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 653 | // Otherwise, we have a definition after a prototype with the wrong type. |
| 654 | // F is the Function* for the one with the wrong type, we must make a new |
| 655 | // Function* and update everything that used F (a declaration) with the new |
| 656 | // Function* (which will be a definition). |
| 657 | // |
| 658 | // This happens if there is a prototype for a function (e.g. "int f()") and |
| 659 | // then a definition of a different type (e.g. "int f(int x)"). Start by |
| 660 | // making a new function of the correct type, RAUW, then steal the name. |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 661 | llvm::GlobalValue *NewFn = EmitForwardFunctionDefinition(D); |
| 662 | NewFn->takeName(Entry); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 663 | |
| 664 | // Replace uses of F with the Function we will endow with a body. |
| 665 | llvm::Constant *NewPtrForOldDecl = |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 666 | llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); |
| 667 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
| 668 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 669 | // Ok, delete the old function now, which is dead. |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 670 | assert(Entry->isDeclaration() && "Shouldn't replace non-declaration"); |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 671 | Entry->eraseFromParent(); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 672 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 673 | Entry = NewFn; |
| 674 | } |
| 675 | } |
| 676 | |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 677 | llvm::Function *Fn = cast<llvm::Function>(Entry); |
| 678 | CodeGenFunction(*this).GenerateCode(D, Fn); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 679 | |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 680 | SetFunctionAttributesForDefinition(D, Fn); |
| 681 | |
| 682 | if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) { |
| 683 | AddGlobalCtor(Fn, CA->getPriority()); |
| 684 | } else if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) { |
| 685 | AddGlobalDtor(Fn, DA->getPriority()); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Daniel Dunbar | f1968f2 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 689 | llvm::Function * |
| 690 | CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, |
| 691 | const std::string &Name) { |
| 692 | llvm::Function *Fn = llvm::Function::Create(FTy, |
| 693 | llvm::Function::ExternalLinkage, |
| 694 | "", &TheModule); |
| 695 | RuntimeFunctions.push_back(std::make_pair(Fn, Name)); |
| 696 | return Fn; |
| 697 | } |
| 698 | |
Chris Lattner | c5b8806 | 2008-02-06 05:08:19 +0000 | [diff] [blame] | 699 | void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { |
| 700 | // Make sure that this type is translated. |
| 701 | Types.UpdateCompletedType(TD); |
Chris Lattner | d86e6bc | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 705 | /// getBuiltinLibFunction |
| 706 | llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { |
Chris Lattner | 1426fec | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 707 | if (BuiltinID > BuiltinFunctions.size()) |
| 708 | BuiltinFunctions.resize(BuiltinID); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 709 | |
Chris Lattner | 1426fec | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 710 | // Cache looked up functions. Since builtin id #0 is invalid we don't reserve |
| 711 | // a slot for it. |
| 712 | assert(BuiltinID && "Invalid Builtin ID"); |
| 713 | llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 714 | if (FunctionSlot) |
| 715 | return FunctionSlot; |
| 716 | |
| 717 | assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn"); |
| 718 | |
| 719 | // Get the name, skip over the __builtin_ prefix. |
| 720 | const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10; |
| 721 | |
| 722 | // Get the type for the builtin. |
| 723 | QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context); |
| 724 | const llvm::FunctionType *Ty = |
| 725 | cast<llvm::FunctionType>(getTypes().ConvertType(Type)); |
| 726 | |
| 727 | // FIXME: This has a serious problem with code like this: |
| 728 | // void abs() {} |
| 729 | // ... __builtin_abs(x); |
| 730 | // The two versions of abs will collide. The fix is for the builtin to win, |
| 731 | // and for the existing one to be turned into a constantexpr cast of the |
| 732 | // builtin. In the case where the existing one is a static function, it |
| 733 | // should just be renamed. |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 734 | if (llvm::Function *Existing = getModule().getFunction(Name)) { |
| 735 | if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) |
| 736 | return FunctionSlot = Existing; |
| 737 | assert(Existing == 0 && "FIXME: Name collision"); |
| 738 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 739 | |
| 740 | // FIXME: param attributes for sext/zext etc. |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 741 | return FunctionSlot = |
| 742 | llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name, |
| 743 | &getModule()); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 746 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, |
| 747 | unsigned NumTys) { |
| 748 | return llvm::Intrinsic::getDeclaration(&getModule(), |
| 749 | (llvm::Intrinsic::ID)IID, Tys, NumTys); |
| 750 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 751 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 752 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 753 | if (MemCpyFn) return MemCpyFn; |
| 754 | llvm::Intrinsic::ID IID; |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 755 | switch (Context.Target.getPointerWidth(0)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 756 | default: assert(0 && "Unknown ptr width"); |
| 757 | case 32: IID = llvm::Intrinsic::memcpy_i32; break; |
| 758 | case 64: IID = llvm::Intrinsic::memcpy_i64; break; |
| 759 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 760 | return MemCpyFn = getIntrinsic(IID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 761 | } |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 762 | |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 763 | llvm::Function *CodeGenModule::getMemMoveFn() { |
| 764 | if (MemMoveFn) return MemMoveFn; |
| 765 | llvm::Intrinsic::ID IID; |
| 766 | switch (Context.Target.getPointerWidth(0)) { |
| 767 | default: assert(0 && "Unknown ptr width"); |
| 768 | case 32: IID = llvm::Intrinsic::memmove_i32; break; |
| 769 | case 64: IID = llvm::Intrinsic::memmove_i64; break; |
| 770 | } |
| 771 | return MemMoveFn = getIntrinsic(IID); |
| 772 | } |
| 773 | |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 774 | llvm::Function *CodeGenModule::getMemSetFn() { |
| 775 | if (MemSetFn) return MemSetFn; |
| 776 | llvm::Intrinsic::ID IID; |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 777 | switch (Context.Target.getPointerWidth(0)) { |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 778 | default: assert(0 && "Unknown ptr width"); |
| 779 | case 32: IID = llvm::Intrinsic::memset_i32; break; |
| 780 | case 64: IID = llvm::Intrinsic::memset_i64; break; |
| 781 | } |
| 782 | return MemSetFn = getIntrinsic(IID); |
| 783 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 784 | |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 785 | static void appendFieldAndPadding(CodeGenModule &CGM, |
| 786 | std::vector<llvm::Constant*>& Fields, |
| 787 | int FieldNo, llvm::Constant* Field, |
| 788 | RecordDecl* RD, const llvm::StructType *STy) |
| 789 | { |
| 790 | // Append the field. |
| 791 | Fields.push_back(Field); |
| 792 | |
| 793 | int StructFieldNo = |
| 794 | CGM.getTypes().getLLVMFieldNo(RD->getMember(FieldNo)); |
| 795 | |
| 796 | int NextStructFieldNo; |
| 797 | if (FieldNo + 1 == RD->getNumMembers()) { |
| 798 | NextStructFieldNo = STy->getNumElements(); |
| 799 | } else { |
| 800 | NextStructFieldNo = |
| 801 | CGM.getTypes().getLLVMFieldNo(RD->getMember(FieldNo + 1)); |
| 802 | } |
| 803 | |
| 804 | // Append padding |
| 805 | for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) { |
| 806 | llvm::Constant *C = |
| 807 | llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1)); |
| 808 | |
| 809 | Fields.push_back(C); |
| 810 | } |
| 811 | } |
| 812 | |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 813 | // We still need to work out the details of handling UTF-16. |
| 814 | // See: <rdr://2996215> |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 815 | llvm::Constant *CodeGenModule:: |
| 816 | GetAddrOfConstantCFString(const std::string &str) { |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 817 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 818 | CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 819 | |
| 820 | if (Entry.getValue()) |
| 821 | return Entry.getValue(); |
| 822 | |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 823 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 824 | llvm::Constant *Zeros[] = { Zero, Zero }; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 825 | |
| 826 | if (!CFConstantStringClassRef) { |
| 827 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 828 | Ty = llvm::ArrayType::get(Ty, 0); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 829 | |
| 830 | // FIXME: This is fairly broken if |
| 831 | // __CFConstantStringClassReference is already defined, in that it |
| 832 | // will get renamed and the user will most likely see an opaque |
| 833 | // error message. This is a general issue with relying on |
| 834 | // particular names. |
| 835 | llvm::GlobalVariable *GV = |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 836 | new llvm::GlobalVariable(Ty, false, |
| 837 | llvm::GlobalVariable::ExternalLinkage, 0, |
| 838 | "__CFConstantStringClassReference", |
| 839 | &getModule()); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 840 | |
| 841 | // Decay array -> ptr |
| 842 | CFConstantStringClassRef = |
| 843 | llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 846 | QualType CFTy = getContext().getCFConstantStringType(); |
| 847 | RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl(); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 848 | |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 849 | const llvm::StructType *STy = |
| 850 | cast<llvm::StructType>(getTypes().ConvertType(CFTy)); |
| 851 | |
| 852 | std::vector<llvm::Constant*> Fields; |
| 853 | |
| 854 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 855 | // Class pointer. |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 856 | appendFieldAndPadding(*this, Fields, 0, CFConstantStringClassRef, CFRD, STy); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 857 | |
| 858 | // Flags. |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 859 | const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 860 | appendFieldAndPadding(*this, Fields, 1, llvm::ConstantInt::get(Ty, 0x07C8), |
| 861 | CFRD, STy); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 862 | |
| 863 | // String pointer. |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 864 | llvm::Constant *C = llvm::ConstantArray::get(str); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 865 | C = new llvm::GlobalVariable(C->getType(), true, |
| 866 | llvm::GlobalValue::InternalLinkage, |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 867 | C, ".str", &getModule()); |
| 868 | appendFieldAndPadding(*this, Fields, 2, |
| 869 | llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2), |
| 870 | CFRD, STy); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 871 | |
| 872 | // String length. |
| 873 | Ty = getTypes().ConvertType(getContext().LongTy); |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 874 | appendFieldAndPadding(*this, Fields, 3, llvm::ConstantInt::get(Ty, str.length()), |
| 875 | CFRD, STy); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 876 | |
| 877 | // The struct. |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 878 | C = llvm::ConstantStruct::get(STy, Fields); |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 879 | llvm::GlobalVariable *GV = |
| 880 | new llvm::GlobalVariable(C->getType(), true, |
| 881 | llvm::GlobalVariable::InternalLinkage, |
| 882 | C, "", &getModule()); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 883 | |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 884 | GV->setSection("__DATA,__cfstring"); |
| 885 | Entry.setValue(GV); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 886 | |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 887 | return GV; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 888 | } |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 889 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 890 | /// GetStringForStringLiteral - Return the appropriate bytes for a |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 891 | /// string literal, properly padded to match the literal type. |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 892 | std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 893 | if (E->isWide()) { |
| 894 | ErrorUnsupported(E, "wide string"); |
| 895 | return "FIXME"; |
| 896 | } |
| 897 | |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 898 | const char *StrData = E->getStrData(); |
| 899 | unsigned Len = E->getByteLength(); |
| 900 | |
| 901 | const ConstantArrayType *CAT = |
| 902 | getContext().getAsConstantArrayType(E->getType()); |
| 903 | assert(CAT && "String isn't pointer or array!"); |
| 904 | |
| 905 | // Resize the string to the right size |
| 906 | // FIXME: What about wchar_t strings? |
| 907 | std::string Str(StrData, StrData+Len); |
| 908 | uint64_t RealLen = CAT->getSize().getZExtValue(); |
| 909 | Str.resize(RealLen, '\0'); |
| 910 | |
| 911 | return Str; |
| 912 | } |
| 913 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 914 | /// GetAddrOfConstantStringFromLiteral - Return a pointer to a |
| 915 | /// constant array for the given string literal. |
| 916 | llvm::Constant * |
| 917 | CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { |
| 918 | // FIXME: This can be more efficient. |
| 919 | return GetAddrOfConstantString(GetStringForStringLiteral(S)); |
| 920 | } |
| 921 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 922 | /// GenerateWritableString -- Creates storage for a string literal. |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 923 | static llvm::Constant *GenerateStringLiteral(const std::string &str, |
| 924 | bool constant, |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 925 | CodeGenModule &CGM, |
| 926 | const char *GlobalName) { |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 927 | // Create Constant for this string literal. Don't add a '\0'. |
| 928 | llvm::Constant *C = llvm::ConstantArray::get(str, false); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 929 | |
| 930 | // Create a global variable for this string |
| 931 | C = new llvm::GlobalVariable(C->getType(), constant, |
| 932 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 933 | C, |
| 934 | GlobalName ? GlobalName : ".str", |
| 935 | &CGM.getModule()); |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 936 | |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 937 | return C; |
| 938 | } |
| 939 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 940 | /// GetAddrOfConstantString - Returns a pointer to a character array |
| 941 | /// containing the literal. This contents are exactly that of the |
| 942 | /// given string, i.e. it will not be null terminated automatically; |
| 943 | /// see GetAddrOfConstantCString. Note that whether the result is |
| 944 | /// actually a pointer to an LLVM constant depends on |
| 945 | /// Feature.WriteableStrings. |
| 946 | /// |
| 947 | /// The result has pointer to array type. |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 948 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, |
| 949 | const char *GlobalName) { |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 950 | // Don't share any string literals if writable-strings is turned on. |
| 951 | if (Features.WritableStrings) |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 952 | return GenerateStringLiteral(str, false, *this, GlobalName); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 953 | |
| 954 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 955 | ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 956 | |
| 957 | if (Entry.getValue()) |
| 958 | return Entry.getValue(); |
| 959 | |
| 960 | // Create a global variable for this. |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 961 | llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 962 | Entry.setValue(C); |
| 963 | return C; |
| 964 | } |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 965 | |
| 966 | /// GetAddrOfConstantCString - Returns a pointer to a character |
| 967 | /// array containing the literal and a terminating '\-' |
| 968 | /// character. The result has pointer to array type. |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 969 | llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, |
| 970 | const char *GlobalName){ |
| 971 | return GetAddrOfConstantString(str + "\0", GlobalName); |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 972 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 973 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 974 | /// EmitObjCPropertyImplementations - Emit information for synthesized |
| 975 | /// properties for an implementation. |
| 976 | void CodeGenModule::EmitObjCPropertyImplementations(const |
| 977 | ObjCImplementationDecl *D) { |
| 978 | for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(), |
| 979 | e = D->propimpl_end(); i != e; ++i) { |
| 980 | ObjCPropertyImplDecl *PID = *i; |
| 981 | |
| 982 | // Dynamic is just for type-checking. |
| 983 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 984 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 985 | |
| 986 | // Determine which methods need to be implemented, some may have |
| 987 | // been overridden. Note that ::isSynthesized is not the method |
| 988 | // we want, that just indicates if the decl came from a |
| 989 | // property. What we want to know is if the method is defined in |
| 990 | // this implementation. |
| 991 | if (!D->getInstanceMethod(PD->getGetterName())) |
| 992 | CodeGenFunction(*this).GenerateObjCGetter(PID); |
| 993 | if (!PD->isReadOnly() && |
| 994 | !D->getInstanceMethod(PD->getSetterName())) |
| 995 | CodeGenFunction(*this).GenerateObjCSetter(PID); |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1000 | /// EmitTopLevelDecl - Emit code for a single top level declaration. |
| 1001 | void CodeGenModule::EmitTopLevelDecl(Decl *D) { |
| 1002 | // If an error has occurred, stop code generation, but continue |
| 1003 | // parsing and semantic analysis (to ensure all warnings and errors |
| 1004 | // are emitted). |
| 1005 | if (Diags.hasErrorOccurred()) |
| 1006 | return; |
| 1007 | |
| 1008 | switch (D->getKind()) { |
| 1009 | case Decl::Function: |
| 1010 | case Decl::Var: |
| 1011 | EmitGlobal(cast<ValueDecl>(D)); |
| 1012 | break; |
| 1013 | |
| 1014 | case Decl::Namespace: |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 1015 | ErrorUnsupported(D, "namespace"); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1016 | break; |
| 1017 | |
| 1018 | // Objective-C Decls |
| 1019 | |
| 1020 | // Forward declarations, no (immediate) code generation. |
| 1021 | case Decl::ObjCClass: |
| 1022 | case Decl::ObjCCategory: |
| 1023 | case Decl::ObjCForwardProtocol: |
| 1024 | case Decl::ObjCInterface: |
| 1025 | break; |
| 1026 | |
| 1027 | case Decl::ObjCProtocol: |
| 1028 | Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); |
| 1029 | break; |
| 1030 | |
| 1031 | case Decl::ObjCCategoryImpl: |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1032 | // Categories have properties but don't support synthesize so we |
| 1033 | // can ignore them here. |
| 1034 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1035 | Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); |
| 1036 | break; |
| 1037 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1038 | case Decl::ObjCImplementation: { |
| 1039 | ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); |
| 1040 | EmitObjCPropertyImplementations(OMD); |
| 1041 | Runtime->GenerateClass(OMD); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1042 | break; |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1043 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1044 | case Decl::ObjCMethod: { |
| 1045 | ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); |
| 1046 | // If this is not a prototype, emit the body. |
| 1047 | if (OMD->getBody()) |
| 1048 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 1049 | break; |
| 1050 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1051 | case Decl::ObjCCompatibleAlias: |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 1052 | ErrorUnsupported(D, "Objective-C compatible alias"); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1053 | break; |
| 1054 | |
| 1055 | case Decl::LinkageSpec: { |
| 1056 | LinkageSpecDecl *LSD = cast<LinkageSpecDecl>(D); |
| 1057 | if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx) |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 1058 | ErrorUnsupported(LSD, "linkage spec"); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1059 | // FIXME: implement C++ linkage, C linkage works mostly by C |
| 1060 | // language reuse already. |
| 1061 | break; |
| 1062 | } |
| 1063 | |
| 1064 | case Decl::FileScopeAsm: { |
| 1065 | FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); |
| 1066 | std::string AsmString(AD->getAsmString()->getStrData(), |
| 1067 | AD->getAsmString()->getByteLength()); |
| 1068 | |
| 1069 | const std::string &S = getModule().getModuleInlineAsm(); |
| 1070 | if (S.empty()) |
| 1071 | getModule().setModuleInlineAsm(AsmString); |
| 1072 | else |
| 1073 | getModule().setModuleInlineAsm(S + '\n' + AsmString); |
| 1074 | break; |
| 1075 | } |
| 1076 | |
| 1077 | default: |
| 1078 | // Make sure we handled everything we should, every other kind is |
| 1079 | // a non-top-level decl. FIXME: Would be nice to have an |
| 1080 | // isTopLevelDeclKind function. Need to recode Decl::Kind to do |
| 1081 | // that easily. |
| 1082 | assert(isa<TypeDecl>(D) && "Unsupported decl kind"); |
| 1083 | } |
| 1084 | } |
| 1085 | |