Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This coordinates the per-module state used while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Sanjiv Gupta | 40e56a1 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 14 | #include "CGDebugInfo.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 20 | #include "clang/Basic/LangOptions.h" |
Nate Begeman | 8a70417 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Nate Begeman | dc6262e | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 23 | #include "llvm/CallingConv.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 24 | #include "llvm/Constants.h" |
| 25 | #include "llvm/DerivedTypes.h" |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 26 | #include "llvm/Module.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 27 | #include "llvm/Intrinsics.h" |
Anton Korobeynikov | cd5d08d | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
Chris Lattner | b033c4a | 2008-04-30 16:05:42 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/Verifier.h" |
Christopher Lamb | 6db92f3 | 2007-12-02 08:49:54 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | using namespace CodeGen; |
| 33 | |
| 34 | |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 35 | CodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO, |
Chris Lattner | 22595b8 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 36 | llvm::Module &M, const llvm::TargetData &TD, |
Sanjiv Gupta | 40e56a1 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 37 | Diagnostic &diags, bool GenerateDebugInfo) |
Chris Lattner | 22595b8 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 38 | : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags), |
Eli Friedman | 8f08a25 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 39 | Types(C, M, TD), MemCpyFn(0), MemMoveFn(0), MemSetFn(0), |
| 40 | CFConstantStringClassRef(0) { |
Chris Lattner | cbfb551 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 41 | //TODO: Make this selectable at runtime |
Chris Lattner | b326b17 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 42 | Runtime = CreateObjCRuntime(M, |
| 43 | getTypes().ConvertType(getContext().IntTy), |
| 44 | getTypes().ConvertType(getContext().LongTy)); |
Sanjiv Gupta | 40e56a1 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 45 | |
| 46 | // If debug info generation is enabled, create the CGDebugInfo object. |
| 47 | if (GenerateDebugInfo) |
| 48 | DebugInfo = new CGDebugInfo(this); |
| 49 | else |
| 50 | DebugInfo = NULL; |
Chris Lattner | cbfb551 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | CodeGenModule::~CodeGenModule() { |
Anton Korobeynikov | cd5d08d | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 54 | EmitStatics(); |
Chris Lattner | b326b17 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 55 | llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction(); |
Chris Lattner | c61e9f8 | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 56 | if (ObjCInitFunction) |
Chris Lattner | b326b17 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 57 | AddGlobalCtor(ObjCInitFunction); |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 58 | EmitGlobalCtors(); |
Nate Begeman | 52da5c7 | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 59 | EmitAnnotations(); |
Chris Lattner | cbfb551 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 60 | delete Runtime; |
Sanjiv Gupta | 40e56a1 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 61 | delete DebugInfo; |
Chris Lattner | b033c4a | 2008-04-30 16:05:42 +0000 | [diff] [blame] | 62 | // Run the verifier to check that the generated code is consistent. |
| 63 | assert(!verifyModule(TheModule)); |
Chris Lattner | cbfb551 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 64 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 65 | |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 66 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 67 | /// specified stmt yet. |
| 68 | void CodeGenModule::WarnUnsupported(const Stmt *S, const char *Type) { |
| 69 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning, |
| 70 | "cannot codegen this %0 yet"); |
| 71 | SourceRange Range = S->getSourceRange(); |
| 72 | std::string Msg = Type; |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 73 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID, |
Ted Kremenek | b3ee193 | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 74 | &Msg, 1, &Range, 1); |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 75 | } |
Chris Lattner | 0e4755d | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 77 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 78 | /// specified decl yet. |
| 79 | void CodeGenModule::WarnUnsupported(const Decl *D, const char *Type) { |
| 80 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning, |
| 81 | "cannot codegen this %0 yet"); |
| 82 | std::string Msg = Type; |
| 83 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID, |
| 84 | &Msg, 1); |
| 85 | } |
| 86 | |
Dan Gohman | 4751a3a | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 87 | /// setVisibility - Set the visibility for the given LLVM GlobalValue |
| 88 | /// according to the given clang AST visibility value. |
| 89 | void CodeGenModule::setVisibility(llvm::GlobalValue *GV, |
| 90 | VisibilityAttr::VisibilityTypes Vis) { |
| 91 | switch (Vis) { |
| 92 | default: assert(0 && "Unknown visibility!"); |
| 93 | case VisibilityAttr::DefaultVisibility: |
| 94 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
| 95 | break; |
| 96 | case VisibilityAttr::HiddenVisibility: |
| 97 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 98 | break; |
| 99 | case VisibilityAttr::ProtectedVisibility: |
| 100 | GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 105 | /// AddGlobalCtor - Add a function to the list that will be called before |
| 106 | /// main() runs. |
| 107 | void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor) { |
| 108 | // TODO: Type coercion of void()* types. |
| 109 | GlobalCtors.push_back(Ctor); |
| 110 | } |
| 111 | |
Chris Lattner | b326b17 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 112 | /// EmitGlobalCtors - Generates the array of contsturctor functions to be |
| 113 | /// called on module load, if any have been registered with AddGlobalCtor. |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 114 | void CodeGenModule::EmitGlobalCtors() { |
Chris Lattner | b326b17 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 115 | if (GlobalCtors.empty()) return; |
Chris Lattner | c61e9f8 | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 117 | // Get the type of @llvm.global_ctors |
| 118 | std::vector<const llvm::Type*> CtorFields; |
| 119 | CtorFields.push_back(llvm::IntegerType::get(32)); |
| 120 | // Constructor function type |
| 121 | std::vector<const llvm::Type*> VoidArgs; |
Chris Lattner | c61e9f8 | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 122 | llvm::FunctionType* CtorFuncTy = |
| 123 | llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false); |
| 124 | |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 125 | // i32, function type pair |
Chris Lattner | a18c12e | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 126 | const llvm::Type *FPType = llvm::PointerType::getUnqual(CtorFuncTy); |
| 127 | llvm::StructType* CtorStructTy = |
| 128 | llvm::StructType::get(llvm::Type::Int32Ty, FPType, NULL); |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 129 | // Array of fields |
Chris Lattner | a18c12e | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 130 | llvm::ArrayType* GlobalCtorsTy = |
| 131 | llvm::ArrayType::get(CtorStructTy, GlobalCtors.size()); |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 133 | // Define the global variable |
Chris Lattner | a18c12e | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 134 | llvm::GlobalVariable *GlobalCtorsVal = |
| 135 | new llvm::GlobalVariable(GlobalCtorsTy, false, |
| 136 | llvm::GlobalValue::AppendingLinkage, |
| 137 | (llvm::Constant*)0, "llvm.global_ctors", |
| 138 | &TheModule); |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 139 | |
| 140 | // Populate the array |
| 141 | std::vector<llvm::Constant*> CtorValues; |
Chris Lattner | a18c12e | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 142 | llvm::Constant *MagicNumber = |
| 143 | llvm::ConstantInt::get(llvm::Type::Int32Ty, 65535, false); |
| 144 | std::vector<llvm::Constant*> StructValues; |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 145 | for (std::vector<llvm::Constant*>::iterator I = GlobalCtors.begin(), |
Chris Lattner | a18c12e | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 146 | E = GlobalCtors.end(); I != E; ++I) { |
| 147 | StructValues.clear(); |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 148 | StructValues.push_back(MagicNumber); |
| 149 | StructValues.push_back(*I); |
| 150 | |
Chris Lattner | a18c12e | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 151 | CtorValues.push_back(llvm::ConstantStruct::get(CtorStructTy, StructValues)); |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 152 | } |
Chris Lattner | a18c12e | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 153 | |
| 154 | GlobalCtorsVal->setInitializer(llvm::ConstantArray::get(GlobalCtorsTy, |
| 155 | CtorValues)); |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Chris Lattner | b326b17 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 158 | |
| 159 | |
Nate Begeman | 52da5c7 | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 160 | void CodeGenModule::EmitAnnotations() { |
| 161 | if (Annotations.empty()) |
| 162 | return; |
| 163 | |
| 164 | // Create a new global variable for the ConstantStruct in the Module. |
| 165 | llvm::Constant *Array = |
| 166 | llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), |
| 167 | Annotations.size()), |
| 168 | Annotations); |
| 169 | llvm::GlobalValue *gv = |
| 170 | new llvm::GlobalVariable(Array->getType(), false, |
| 171 | llvm::GlobalValue::AppendingLinkage, Array, |
| 172 | "llvm.global.annotations", &TheModule); |
| 173 | gv->setSection("llvm.metadata"); |
| 174 | } |
| 175 | |
Chris Lattner | 0e4755d | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 176 | /// ReplaceMapValuesWith - This is a really slow and bad function that |
| 177 | /// searches for any entries in GlobalDeclMap that point to OldVal, changing |
| 178 | /// them to point to NewVal. This is badbadbad, FIXME! |
| 179 | void CodeGenModule::ReplaceMapValuesWith(llvm::Constant *OldVal, |
| 180 | llvm::Constant *NewVal) { |
| 181 | for (llvm::DenseMap<const Decl*, llvm::Constant*>::iterator |
| 182 | I = GlobalDeclMap.begin(), E = GlobalDeclMap.end(); I != E; ++I) |
| 183 | if (I->second == OldVal) I->second = NewVal; |
| 184 | } |
| 185 | |
Eli Friedman | 9be4221 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 186 | bool hasAggregateLLVMType(QualType T) { |
| 187 | return !T->isRealType() && !T->isPointerLikeType() && |
| 188 | !T->isVoidType() && !T->isVectorType() && !T->isFunctionType(); |
| 189 | } |
| 190 | |
| 191 | void CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, |
| 192 | llvm::Function *F, |
| 193 | const llvm::FunctionType *FTy) { |
| 194 | unsigned FuncAttrs = 0; |
| 195 | if (FD->getAttr<NoThrowAttr>()) |
| 196 | FuncAttrs |= llvm::ParamAttr::NoUnwind; |
| 197 | if (FD->getAttr<NoReturnAttr>()) |
| 198 | FuncAttrs |= llvm::ParamAttr::NoReturn; |
| 199 | |
| 200 | llvm::SmallVector<llvm::ParamAttrsWithIndex, 8> ParamAttrList; |
| 201 | if (FuncAttrs) |
| 202 | ParamAttrList.push_back(llvm::ParamAttrsWithIndex::get(0, FuncAttrs)); |
| 203 | // Note that there is parallel code in CodeGenFunction::EmitCallExpr |
| 204 | bool AggregateReturn = hasAggregateLLVMType(FD->getResultType()); |
| 205 | if (AggregateReturn) |
| 206 | ParamAttrList.push_back( |
| 207 | llvm::ParamAttrsWithIndex::get(1, llvm::ParamAttr::StructRet)); |
| 208 | unsigned increment = AggregateReturn ? 2 : 1; |
| 209 | for (unsigned i = 0; i < FD->getNumParams(); i++) { |
| 210 | QualType ParamType = FD->getParamDecl(i)->getType(); |
| 211 | unsigned ParamAttrs = 0; |
| 212 | if (ParamType->isRecordType()) |
| 213 | ParamAttrs |= llvm::ParamAttr::ByVal; |
| 214 | if (ParamType->isSignedIntegerType() && ParamType->isPromotableIntegerType()) |
| 215 | ParamAttrs |= llvm::ParamAttr::SExt; |
| 216 | if (ParamType->isUnsignedIntegerType() && ParamType->isPromotableIntegerType()) |
| 217 | ParamAttrs |= llvm::ParamAttr::ZExt; |
| 218 | if (ParamAttrs) |
| 219 | ParamAttrList.push_back(llvm::ParamAttrsWithIndex::get(i + increment, |
| 220 | ParamAttrs)); |
| 221 | } |
| 222 | F->setParamAttrs(llvm::PAListPtr::get(ParamAttrList.begin(), |
| 223 | ParamAttrList.size())); |
| 224 | |
| 225 | // Set the appropriate calling convention for the Function. |
| 226 | if (FD->getAttr<FastCallAttr>()) |
| 227 | F->setCallingConv(llvm::CallingConv::Fast); |
| 228 | |
| 229 | // TODO: Set up linkage and many other things. Note, this is a simple |
| 230 | // approximation of what we really want. |
| 231 | if (FD->getStorageClass() == FunctionDecl::Static) |
| 232 | F->setLinkage(llvm::Function::InternalLinkage); |
| 233 | else if (FD->getAttr<DLLImportAttr>()) |
| 234 | F->setLinkage(llvm::Function::DLLImportLinkage); |
| 235 | else if (FD->getAttr<DLLExportAttr>()) |
| 236 | F->setLinkage(llvm::Function::DLLExportLinkage); |
| 237 | else if (FD->getAttr<WeakAttr>() || FD->isInline()) |
| 238 | F->setLinkage(llvm::Function::WeakLinkage); |
| 239 | |
| 240 | if (const VisibilityAttr *attr = FD->getAttr<VisibilityAttr>()) |
| 241 | CodeGenModule::setVisibility(F, attr->getVisibility()); |
| 242 | // FIXME: else handle -fvisibility |
| 243 | } |
| 244 | |
| 245 | |
Chris Lattner | 0e4755d | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 247 | llvm::Constant *CodeGenModule::GetAddrOfFunctionDecl(const FunctionDecl *D, |
| 248 | bool isDefinition) { |
| 249 | // See if it is already in the map. If so, just return it. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 250 | llvm::Constant *&Entry = GlobalDeclMap[D]; |
Eli Friedman | 5dcae52 | 2008-05-30 11:13:18 +0000 | [diff] [blame] | 251 | if (!isDefinition && Entry) return Entry; |
Eli Friedman | 725d2be | 2008-05-20 09:21:07 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 253 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 254 | |
| 255 | // Check to see if the function already exists. |
| 256 | llvm::Function *F = getModule().getFunction(D->getName()); |
| 257 | const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty); |
| 258 | |
| 259 | // If it doesn't already exist, just create and return an entry. |
| 260 | if (F == 0) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 261 | // FIXME: param attributes for sext/zext etc. |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 262 | F = llvm::Function::Create(FTy, llvm::Function::ExternalLinkage, |
| 263 | D->getName(), &getModule()); |
Nate Begeman | dc6262e | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 264 | |
Eli Friedman | 9be4221 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 265 | SetFunctionAttributes(D, F, FTy); |
Nate Begeman | dc6262e | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 266 | return Entry = F; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 269 | // If the pointer type matches, just return it. |
Christopher Lamb | 4fe5e70 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 270 | llvm::Type *PFTy = llvm::PointerType::getUnqual(Ty); |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 271 | if (PFTy == F->getType()) return Entry = F; |
Chris Lattner | 77ce67c | 2007-12-02 06:30:46 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 273 | // If this isn't a definition, just return it casted to the right type. |
| 274 | if (!isDefinition) |
| 275 | return Entry = llvm::ConstantExpr::getBitCast(F, PFTy); |
| 276 | |
| 277 | // Otherwise, we have a definition after a prototype with the wrong type. |
| 278 | // F is the Function* for the one with the wrong type, we must make a new |
| 279 | // Function* and update everything that used F (a declaration) with the new |
| 280 | // Function* (which will be a definition). |
| 281 | // |
| 282 | // This happens if there is a prototype for a function (e.g. "int f()") and |
| 283 | // then a definition of a different type (e.g. "int f(int x)"). Start by |
| 284 | // making a new function of the correct type, RAUW, then steal the name. |
Gabor Greif | 815e2c1 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 285 | llvm::Function *NewFn = llvm::Function::Create(FTy, |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 286 | llvm::Function::ExternalLinkage, |
| 287 | "", &getModule()); |
| 288 | NewFn->takeName(F); |
| 289 | |
| 290 | // Replace uses of F with the Function we will endow with a body. |
| 291 | llvm::Constant *NewPtrForOldDecl = |
| 292 | llvm::ConstantExpr::getBitCast(NewFn, F->getType()); |
| 293 | F->replaceAllUsesWith(NewPtrForOldDecl); |
| 294 | |
| 295 | // FIXME: Update the globaldeclmap for the previous decl of this name. We |
| 296 | // really want a way to walk all of these, but we don't have it yet. This |
| 297 | // is incredibly slow! |
| 298 | ReplaceMapValuesWith(F, NewPtrForOldDecl); |
| 299 | |
| 300 | // Ok, delete the old function now, which is dead. |
| 301 | assert(F->isDeclaration() && "Shouldn't replace non-declaration"); |
| 302 | F->eraseFromParent(); |
| 303 | |
Eli Friedman | 9be4221 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 304 | SetFunctionAttributes(D, NewFn, FTy); |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 305 | // Return the new function which has the right type. |
| 306 | return Entry = NewFn; |
| 307 | } |
| 308 | |
Chris Lattner | d2df2b5 | 2007-12-18 08:16:44 +0000 | [diff] [blame] | 309 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, |
| 310 | bool isDefinition) { |
| 311 | assert(D->hasGlobalStorage() && "Not a global variable"); |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 312 | assert(!isDefinition && "This shouldn't be called for definitions!"); |
| 313 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 314 | // See if it is already in the map. |
| 315 | llvm::Constant *&Entry = GlobalDeclMap[D]; |
| 316 | if (Entry) return Entry; |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 317 | |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 318 | QualType ASTTy = D->getType(); |
| 319 | const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 320 | |
| 321 | // Check to see if the global already exists. |
Chris Lattner | ed430e9 | 2008-02-02 04:43:11 +0000 | [diff] [blame] | 322 | llvm::GlobalVariable *GV = getModule().getGlobalVariable(D->getName(), true); |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 323 | |
| 324 | // If it doesn't already exist, just create and return an entry. |
| 325 | if (GV == 0) { |
| 326 | return Entry = new llvm::GlobalVariable(Ty, false, |
| 327 | llvm::GlobalValue::ExternalLinkage, |
Christopher Lamb | 2a72bb3 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 328 | 0, D->getName(), &getModule(), 0, |
| 329 | ASTTy.getAddressSpace()); |
Chris Lattner | 77ce67c | 2007-12-02 06:30:46 +0000 | [diff] [blame] | 330 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 331 | |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 332 | // Otherwise, it already exists; return the existing version |
| 333 | llvm::PointerType *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); |
| 334 | return Entry = llvm::ConstantExpr::getBitCast(GV, PTy); |
| 335 | } |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 336 | |
Chris Lattner | b326b17 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 337 | void CodeGenModule::EmitObjCMethod(const ObjCMethodDecl *OMD) { |
| 338 | // If this is not a prototype, emit the body. |
| 339 | if (OMD->getBody()) |
| 340 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 341 | } |
Anton Korobeynikov | cd5d08d | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 342 | void CodeGenModule::EmitObjCProtocolImplementation(const ObjCProtocolDecl *PD){ |
| 343 | llvm::SmallVector<std::string, 16> Protocols; |
| 344 | for (unsigned i = 0, e = PD->getNumReferencedProtocols() ; i < e ; i++) |
| 345 | Protocols.push_back(PD->getReferencedProtocols()[i]->getName()); |
| 346 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames; |
| 347 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
| 348 | for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(), |
| 349 | endIter = PD->instmeth_end() ; iter != endIter ; iter++) { |
| 350 | std::string TypeStr; |
| 351 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
| 352 | InstanceMethodNames.push_back( |
| 353 | GetAddrOfConstantString((*iter)->getSelector().getName())); |
| 354 | InstanceMethodTypes.push_back(GetAddrOfConstantString(TypeStr)); |
| 355 | } |
| 356 | // Collect information about class methods: |
| 357 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames; |
| 358 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
| 359 | for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(), |
| 360 | endIter = PD->classmeth_end() ; iter != endIter ; iter++) { |
| 361 | std::string TypeStr; |
| 362 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
| 363 | ClassMethodNames.push_back( |
| 364 | GetAddrOfConstantString((*iter)->getSelector().getName())); |
| 365 | ClassMethodTypes.push_back(GetAddrOfConstantString(TypeStr)); |
| 366 | } |
| 367 | Runtime->GenerateProtocol(PD->getName(), Protocols, InstanceMethodNames, |
| 368 | InstanceMethodTypes, ClassMethodNames, ClassMethodTypes); |
| 369 | } |
| 370 | |
| 371 | void CodeGenModule::EmitObjCCategoryImpl(const ObjCCategoryImplDecl *OCD) { |
| 372 | |
| 373 | // Collect information about instance methods |
| 374 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames; |
| 375 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
| 376 | for (ObjCCategoryDecl::instmeth_iterator iter = OCD->instmeth_begin(), |
| 377 | endIter = OCD->instmeth_end() ; iter != endIter ; iter++) { |
| 378 | std::string TypeStr; |
| 379 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
| 380 | InstanceMethodNames.push_back( |
| 381 | GetAddrOfConstantString((*iter)->getSelector().getName())); |
| 382 | InstanceMethodTypes.push_back(GetAddrOfConstantString(TypeStr)); |
| 383 | } |
| 384 | |
| 385 | // Collect information about class methods |
| 386 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames; |
| 387 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
| 388 | for (ObjCCategoryDecl::classmeth_iterator iter = OCD->classmeth_begin(), |
| 389 | endIter = OCD->classmeth_end() ; iter != endIter ; iter++) { |
| 390 | std::string TypeStr; |
| 391 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
| 392 | ClassMethodNames.push_back( |
| 393 | GetAddrOfConstantString((*iter)->getSelector().getName())); |
| 394 | ClassMethodTypes.push_back(GetAddrOfConstantString(TypeStr)); |
| 395 | } |
| 396 | |
| 397 | // Collect the names of referenced protocols |
| 398 | llvm::SmallVector<std::string, 16> Protocols; |
| 399 | ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OCD->getClassInterface(); |
| 400 | for (unsigned i=0 ; i<ClassDecl->getNumIntfRefProtocols() ; i++) |
| 401 | Protocols.push_back(ClassDecl->getReferencedProtocols()[i]->getName()); |
| 402 | |
| 403 | // Generate the category |
| 404 | Runtime->GenerateCategory(OCD->getClassInterface()->getName(), |
| 405 | OCD->getName(), InstanceMethodNames, InstanceMethodTypes, |
| 406 | ClassMethodNames, ClassMethodTypes, Protocols); |
| 407 | } |
| 408 | |
| 409 | void CodeGenModule::EmitObjCClassImplementation( |
| 410 | const ObjCImplementationDecl *OID) { |
| 411 | // Get the superclass name. |
| 412 | const ObjCInterfaceDecl * SCDecl = OID->getClassInterface()->getSuperClass(); |
| 413 | const char * SCName = NULL; |
| 414 | if (SCDecl) { |
| 415 | SCName = SCDecl->getName(); |
| 416 | } |
| 417 | |
| 418 | // Get the class name |
| 419 | ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface(); |
| 420 | const char * ClassName = ClassDecl->getName(); |
| 421 | |
| 422 | // Get the size of instances. For runtimes that support late-bound instances |
| 423 | // this should probably be something different (size just of instance |
| 424 | // varaibles in this class, not superclasses?). |
| 425 | int instanceSize = 0; |
| 426 | const llvm::Type *ObjTy; |
| 427 | if (!Runtime->LateBoundIVars()) { |
| 428 | ObjTy = getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl)); |
| 429 | instanceSize = TheTargetData.getABITypeSize(ObjTy); |
| 430 | } |
| 431 | |
| 432 | // Collect information about instance variables. |
| 433 | llvm::SmallVector<llvm::Constant*, 16> IvarNames; |
| 434 | llvm::SmallVector<llvm::Constant*, 16> IvarTypes; |
| 435 | llvm::SmallVector<llvm::Constant*, 16> IvarOffsets; |
| 436 | const llvm::StructLayout *Layout = |
| 437 | TheTargetData.getStructLayout(cast<llvm::StructType>(ObjTy)); |
| 438 | ObjTy = llvm::PointerType::getUnqual(ObjTy); |
| 439 | for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(), |
| 440 | endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) { |
| 441 | // Store the name |
| 442 | IvarNames.push_back(GetAddrOfConstantString((*iter)->getName())); |
| 443 | // Get the type encoding for this ivar |
| 444 | std::string TypeStr; |
| 445 | llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes; |
| 446 | Context.getObjCEncodingForType((*iter)->getType(), TypeStr, |
| 447 | EncodingRecordTypes); |
| 448 | IvarTypes.push_back(GetAddrOfConstantString(TypeStr)); |
| 449 | // Get the offset |
| 450 | int offset = |
| 451 | (int)Layout->getElementOffset(getTypes().getLLVMFieldNo(*iter)); |
| 452 | IvarOffsets.push_back( |
| 453 | llvm::ConstantInt::get(llvm::Type::Int32Ty, offset)); |
| 454 | } |
| 455 | |
| 456 | // Collect information about instance methods |
| 457 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames; |
| 458 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
| 459 | for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(), |
| 460 | endIter = OID->instmeth_end() ; iter != endIter ; iter++) { |
| 461 | std::string TypeStr; |
| 462 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
| 463 | InstanceMethodNames.push_back( |
| 464 | GetAddrOfConstantString((*iter)->getSelector().getName())); |
| 465 | InstanceMethodTypes.push_back(GetAddrOfConstantString(TypeStr)); |
| 466 | } |
| 467 | |
| 468 | // Collect information about class methods |
| 469 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames; |
| 470 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
| 471 | for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(), |
| 472 | endIter = OID->classmeth_end() ; iter != endIter ; iter++) { |
| 473 | std::string TypeStr; |
| 474 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
| 475 | ClassMethodNames.push_back( |
| 476 | GetAddrOfConstantString((*iter)->getSelector().getName())); |
| 477 | ClassMethodTypes.push_back(GetAddrOfConstantString(TypeStr)); |
| 478 | } |
| 479 | // Collect the names of referenced protocols |
| 480 | llvm::SmallVector<std::string, 16> Protocols; |
| 481 | for (unsigned i = 0, e = ClassDecl->getNumIntfRefProtocols() ; i < e ; i++) |
| 482 | Protocols.push_back(ClassDecl->getReferencedProtocols()[i]->getName()); |
| 483 | |
| 484 | // Generate the category |
| 485 | Runtime->GenerateClass(ClassName, SCName, instanceSize, IvarNames, IvarTypes, |
| 486 | IvarOffsets, InstanceMethodNames, InstanceMethodTypes, ClassMethodNames, |
| 487 | ClassMethodTypes, Protocols); |
| 488 | } |
| 489 | |
Chris Lattner | b326b17 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 490 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 491 | void CodeGenModule::EmitFunction(const FunctionDecl *FD) { |
| 492 | // If this is not a prototype, emit the body. |
Chris Lattner | 74bf555 | 2008-05-04 02:29:49 +0000 | [diff] [blame] | 493 | if (!FD->isThisDeclarationADefinition()) |
| 494 | return; |
| 495 | |
| 496 | // If the function is a static, defer code generation until later so we can |
| 497 | // easily omit unused statics. |
| 498 | if (FD->getStorageClass() != FunctionDecl::Static) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 499 | CodeGenFunction(*this).GenerateCode(FD); |
Chris Lattner | 74bf555 | 2008-05-04 02:29:49 +0000 | [diff] [blame] | 500 | return; |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 501 | } |
Chris Lattner | 74bf555 | 2008-05-04 02:29:49 +0000 | [diff] [blame] | 502 | |
| 503 | StaticDecls.push_back(FD); |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | void CodeGenModule::EmitStatics() { |
| 507 | // Emit code for each used static decl encountered. Since a previously unused |
| 508 | // static decl may become used during the generation of code for a static |
| 509 | // function, iterate until no changes are made. |
| 510 | bool Changed; |
| 511 | do { |
| 512 | Changed = false; |
| 513 | for (unsigned i = 0, e = StaticDecls.size(); i != e; ++i) { |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 514 | const Decl *D = StaticDecls[i]; |
Eli Friedman | a4d4e2f | 2008-05-27 04:58:01 +0000 | [diff] [blame] | 515 | |
| 516 | // Check if we have used a decl with the same name |
| 517 | // FIXME: The AST should have some sort of aggregate decls or |
| 518 | // global symbol map. |
| 519 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 520 | if (!getModule().getFunction(FD->getName())) |
| 521 | continue; |
| 522 | } else { |
| 523 | if (!getModule().getNamedGlobal(cast<VarDecl>(D)->getName())) |
| 524 | continue; |
| 525 | } |
| 526 | |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 527 | // If this is a function decl, generate code for the static function if it |
| 528 | // has a body. Otherwise, we must have a var decl for a static global |
| 529 | // variable. |
| 530 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 531 | if (FD->getBody()) |
| 532 | CodeGenFunction(*this).GenerateCode(FD); |
| 533 | } else { |
Nate Begeman | 91f5201 | 2008-04-20 20:38:08 +0000 | [diff] [blame] | 534 | EmitGlobalVarInit(cast<VarDecl>(D)); |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 535 | } |
| 536 | // Erase the used decl from the list. |
| 537 | StaticDecls[i] = StaticDecls.back(); |
| 538 | StaticDecls.pop_back(); |
| 539 | --i; |
| 540 | --e; |
| 541 | |
| 542 | // Remember that we made a change. |
| 543 | Changed = true; |
| 544 | } |
| 545 | } while (Changed); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Anders Carlsson | d76cead | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 548 | llvm::Constant *CodeGenModule::EmitGlobalInit(const Expr *Expr) { |
| 549 | return EmitConstantExpr(Expr); |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Nate Begeman | 8a70417 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 552 | /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the |
| 553 | /// annotation information for a given GlobalValue. The annotation struct is |
| 554 | /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the |
| 555 | /// GlobalValue being annotated. The second filed is thee constant string |
| 556 | /// created from the AnnotateAttr's annotation. The third field is a constant |
| 557 | /// string containing the name of the translation unit. The fourth field is |
| 558 | /// the line number in the file of the annotated value declaration. |
| 559 | /// |
| 560 | /// FIXME: this does not unique the annotation string constants, as llvm-gcc |
| 561 | /// appears to. |
| 562 | /// |
| 563 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
| 564 | const AnnotateAttr *AA, |
| 565 | unsigned LineNo) { |
| 566 | llvm::Module *M = &getModule(); |
| 567 | |
| 568 | // get [N x i8] constants for the annotation string, and the filename string |
| 569 | // which are the 2nd and 3rd elements of the global annotation structure. |
| 570 | const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 571 | llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); |
| 572 | llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), |
| 573 | true); |
| 574 | |
| 575 | // Get the two global values corresponding to the ConstantArrays we just |
| 576 | // created to hold the bytes of the strings. |
| 577 | llvm::GlobalValue *annoGV = |
| 578 | new llvm::GlobalVariable(anno->getType(), false, |
| 579 | llvm::GlobalValue::InternalLinkage, anno, |
| 580 | GV->getName() + ".str", M); |
| 581 | // translation unit name string, emitted into the llvm.metadata section. |
| 582 | llvm::GlobalValue *unitGV = |
| 583 | new llvm::GlobalVariable(unit->getType(), false, |
| 584 | llvm::GlobalValue::InternalLinkage, unit, ".str", M); |
| 585 | |
| 586 | // Create the ConstantStruct that is the global annotion. |
| 587 | llvm::Constant *Fields[4] = { |
| 588 | llvm::ConstantExpr::getBitCast(GV, SBP), |
| 589 | llvm::ConstantExpr::getBitCast(annoGV, SBP), |
| 590 | llvm::ConstantExpr::getBitCast(unitGV, SBP), |
| 591 | llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) |
| 592 | }; |
| 593 | return llvm::ConstantStruct::get(Fields, 4, false); |
| 594 | } |
| 595 | |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 596 | void CodeGenModule::EmitGlobalVar(const VarDecl *D) { |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 597 | // If the VarDecl is a static, defer code generation until later so we can |
| 598 | // easily omit unused statics. |
| 599 | if (D->getStorageClass() == VarDecl::Static) { |
| 600 | StaticDecls.push_back(D); |
| 601 | return; |
| 602 | } |
| 603 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 604 | // If this is just a forward declaration of the variable, don't emit it now, |
| 605 | // allow it to be emitted lazily on its first use. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 606 | if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0) |
| 607 | return; |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 608 | |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 609 | EmitGlobalVarInit(D); |
| 610 | } |
| 611 | |
| 612 | void CodeGenModule::EmitGlobalVarInit(const VarDecl *D) { |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 613 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 614 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 615 | llvm::Constant *Init = 0; |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 616 | QualType ASTTy = D->getType(); |
| 617 | const llvm::Type *VarTy = getTypes().ConvertTypeForMem(ASTTy); |
| 618 | const llvm::Type *VarPtrTy = |
| 619 | llvm::PointerType::get(VarTy, ASTTy.getAddressSpace()); |
| 620 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 621 | if (D->getInit() == 0) { |
Eli Friedman | 7008e9a | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 622 | // This is a tentative definition; tentative definitions are |
| 623 | // implicitly initialized with { 0 } |
| 624 | const llvm::Type* InitTy; |
| 625 | if (ASTTy->isIncompleteArrayType()) { |
| 626 | // An incomplete array is normally [ TYPE x 0 ], but we need |
| 627 | // to fix it to [ TYPE x 1 ]. |
| 628 | const llvm::ArrayType* ATy = cast<llvm::ArrayType>(VarTy); |
| 629 | InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); |
| 630 | } else { |
| 631 | InitTy = VarTy; |
| 632 | } |
| 633 | Init = llvm::Constant::getNullValue(InitTy); |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 634 | } else { |
| 635 | Init = EmitGlobalInit(D->getInit()); |
| 636 | } |
| 637 | const llvm::Type* InitType = Init->getType(); |
| 638 | |
| 639 | llvm::GlobalVariable *GV = getModule().getGlobalVariable(D->getName(), true); |
| 640 | |
| 641 | if (!GV) { |
| 642 | GV = new llvm::GlobalVariable(InitType, false, |
| 643 | llvm::GlobalValue::ExternalLinkage, |
| 644 | 0, D->getName(), &getModule(), 0, |
| 645 | ASTTy.getAddressSpace()); |
| 646 | } else if (GV->getType()->getElementType() != InitType || |
| 647 | GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) { |
| 648 | // We have a definition after a prototype with the wrong type. |
| 649 | // We must make a new GlobalVariable* and update everything that used OldGV |
| 650 | // (a declaration or tentative definition) with the new GlobalVariable* |
| 651 | // (which will be a definition). |
| 652 | // |
| 653 | // This happens if there is a prototype for a global (e.g. "extern int x[];") |
| 654 | // and then a definition of a different type (e.g. "int x[10];"). This also |
| 655 | // happens when an initializer has a different type from the type of the |
| 656 | // global (this happens with unions). |
Eli Friedman | 7008e9a | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 657 | // |
| 658 | // FIXME: This also ends up happening if there's a definition followed by |
| 659 | // a tentative definition! (Although Sema rejects that construct |
| 660 | // at the moment.) |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 661 | |
| 662 | // Save the old global |
| 663 | llvm::GlobalVariable *OldGV = GV; |
| 664 | |
| 665 | // Make a new global with the correct type |
| 666 | GV = new llvm::GlobalVariable(InitType, false, |
| 667 | llvm::GlobalValue::ExternalLinkage, |
| 668 | 0, D->getName(), &getModule(), 0, |
| 669 | ASTTy.getAddressSpace()); |
| 670 | // Steal the name of the old global |
| 671 | GV->takeName(OldGV); |
| 672 | |
| 673 | // Replace all uses of the old global with the new global |
| 674 | llvm::Constant *NewPtrForOldDecl = |
| 675 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 676 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
| 677 | // Make sure we don't keep around any stale references to globals |
| 678 | // FIXME: This is really slow; we need a better way to walk all |
| 679 | // the decls with the same name |
| 680 | ReplaceMapValuesWith(OldGV, NewPtrForOldDecl); |
| 681 | |
| 682 | // Erase the old global, since it is no longer used. |
| 683 | OldGV->eraseFromParent(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 684 | } |
Devang Patel | 8b5f530 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 685 | |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 686 | GlobalDeclMap[D] = llvm::ConstantExpr::getBitCast(GV, VarPtrTy); |
Devang Patel | 8b5f530 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 687 | |
Nate Begeman | 8a70417 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 688 | if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { |
| 689 | SourceManager &SM = Context.getSourceManager(); |
| 690 | AddAnnotation(EmitAnnotateAttr(GV, AA, |
| 691 | SM.getLogicalLineNumber(D->getLocation()))); |
| 692 | } |
| 693 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 694 | GV->setInitializer(Init); |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 695 | |
Eli Friedman | 7008e9a | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 696 | // FIXME: This is silly; getTypeAlign should just work for incomplete arrays |
| 697 | unsigned Align; |
| 698 | if (const IncompleteArrayType* IAT = D->getType()->getAsIncompleteArrayType()) |
| 699 | Align = Context.getTypeAlign(IAT->getElementType()); |
| 700 | else |
| 701 | Align = Context.getTypeAlign(D->getType()); |
Eli Friedman | b232e99 | 2008-05-29 11:10:27 +0000 | [diff] [blame] | 702 | if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) { |
| 703 | Align = std::max(Align, AA->getAlignment()); |
| 704 | } |
| 705 | GV->setAlignment(Align / 8); |
| 706 | |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 707 | if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) |
Dan Gohman | 4751a3a | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 708 | setVisibility(GV, attr->getVisibility()); |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 709 | // FIXME: else handle -fvisibility |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 710 | |
| 711 | // Set the llvm linkage type as appropriate. |
Chris Lattner | 25094a4 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 712 | if (D->getStorageClass() == VarDecl::Static) |
| 713 | GV->setLinkage(llvm::Function::InternalLinkage); |
| 714 | else if (D->getAttr<DLLImportAttr>()) |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 715 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
| 716 | else if (D->getAttr<DLLExportAttr>()) |
| 717 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
Chris Lattner | 25094a4 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 718 | else if (D->getAttr<WeakAttr>()) |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 719 | GV->setLinkage(llvm::GlobalVariable::WeakLinkage); |
Chris Lattner | 25094a4 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 720 | else { |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 721 | // FIXME: This isn't right. This should handle common linkage and other |
| 722 | // stuff. |
| 723 | switch (D->getStorageClass()) { |
Chris Lattner | 25094a4 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 724 | case VarDecl::Static: assert(0 && "This case handled above"); |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 725 | case VarDecl::Auto: |
| 726 | case VarDecl::Register: |
| 727 | assert(0 && "Can't have auto or register globals"); |
| 728 | case VarDecl::None: |
| 729 | if (!D->getInit()) |
Eli Friedman | a7f4633 | 2008-05-29 11:03:17 +0000 | [diff] [blame] | 730 | GV->setLinkage(llvm::GlobalVariable::CommonLinkage); |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 731 | break; |
| 732 | case VarDecl::Extern: |
| 733 | case VarDecl::PrivateExtern: |
| 734 | // todo: common |
| 735 | break; |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 736 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
| 740 | /// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified |
| 741 | /// declarator chain. |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 742 | void CodeGenModule::EmitGlobalVarDeclarator(const VarDecl *D) { |
| 743 | for (; D; D = cast_or_null<VarDecl>(D->getNextDeclarator())) |
| 744 | if (D->isFileVarDecl()) |
| 745 | EmitGlobalVar(D); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Chris Lattner | 9ec3ca2 | 2008-02-06 05:08:19 +0000 | [diff] [blame] | 748 | void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { |
| 749 | // Make sure that this type is translated. |
| 750 | Types.UpdateCompletedType(TD); |
Chris Lattner | 1b22f8b | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 754 | /// getBuiltinLibFunction |
| 755 | llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { |
Chris Lattner | 9f2d689 | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 756 | if (BuiltinID > BuiltinFunctions.size()) |
| 757 | BuiltinFunctions.resize(BuiltinID); |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 758 | |
Chris Lattner | 9f2d689 | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 759 | // Cache looked up functions. Since builtin id #0 is invalid we don't reserve |
| 760 | // a slot for it. |
| 761 | assert(BuiltinID && "Invalid Builtin ID"); |
| 762 | llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 763 | if (FunctionSlot) |
| 764 | return FunctionSlot; |
| 765 | |
| 766 | assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn"); |
| 767 | |
| 768 | // Get the name, skip over the __builtin_ prefix. |
| 769 | const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10; |
| 770 | |
| 771 | // Get the type for the builtin. |
| 772 | QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context); |
| 773 | const llvm::FunctionType *Ty = |
| 774 | cast<llvm::FunctionType>(getTypes().ConvertType(Type)); |
| 775 | |
| 776 | // FIXME: This has a serious problem with code like this: |
| 777 | // void abs() {} |
| 778 | // ... __builtin_abs(x); |
| 779 | // The two versions of abs will collide. The fix is for the builtin to win, |
| 780 | // and for the existing one to be turned into a constantexpr cast of the |
| 781 | // builtin. In the case where the existing one is a static function, it |
| 782 | // should just be renamed. |
Chris Lattner | 02c60f5 | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 783 | if (llvm::Function *Existing = getModule().getFunction(Name)) { |
| 784 | if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) |
| 785 | return FunctionSlot = Existing; |
| 786 | assert(Existing == 0 && "FIXME: Name collision"); |
| 787 | } |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 788 | |
| 789 | // FIXME: param attributes for sext/zext etc. |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 790 | return FunctionSlot = |
| 791 | llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name, |
| 792 | &getModule()); |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 795 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, |
| 796 | unsigned NumTys) { |
| 797 | return llvm::Intrinsic::getDeclaration(&getModule(), |
| 798 | (llvm::Intrinsic::ID)IID, Tys, NumTys); |
| 799 | } |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 800 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 801 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 802 | if (MemCpyFn) return MemCpyFn; |
| 803 | llvm::Intrinsic::ID IID; |
Chris Lattner | 461a6c5 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 804 | switch (Context.Target.getPointerWidth(0)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 805 | default: assert(0 && "Unknown ptr width"); |
| 806 | case 32: IID = llvm::Intrinsic::memcpy_i32; break; |
| 807 | case 64: IID = llvm::Intrinsic::memcpy_i64; break; |
| 808 | } |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 809 | return MemCpyFn = getIntrinsic(IID); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 810 | } |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 811 | |
Eli Friedman | 8f08a25 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 812 | llvm::Function *CodeGenModule::getMemMoveFn() { |
| 813 | if (MemMoveFn) return MemMoveFn; |
| 814 | llvm::Intrinsic::ID IID; |
| 815 | switch (Context.Target.getPointerWidth(0)) { |
| 816 | default: assert(0 && "Unknown ptr width"); |
| 817 | case 32: IID = llvm::Intrinsic::memmove_i32; break; |
| 818 | case 64: IID = llvm::Intrinsic::memmove_i64; break; |
| 819 | } |
| 820 | return MemMoveFn = getIntrinsic(IID); |
| 821 | } |
| 822 | |
Lauro Ramos Venancio | e5bef73 | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 823 | llvm::Function *CodeGenModule::getMemSetFn() { |
| 824 | if (MemSetFn) return MemSetFn; |
| 825 | llvm::Intrinsic::ID IID; |
Chris Lattner | 461a6c5 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 826 | switch (Context.Target.getPointerWidth(0)) { |
Lauro Ramos Venancio | e5bef73 | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 827 | default: assert(0 && "Unknown ptr width"); |
| 828 | case 32: IID = llvm::Intrinsic::memset_i32; break; |
| 829 | case 64: IID = llvm::Intrinsic::memset_i64; break; |
| 830 | } |
| 831 | return MemSetFn = getIntrinsic(IID); |
| 832 | } |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 833 | |
Anton Korobeynikov | cd5d08d | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 834 | // FIXME: This needs moving into an Apple Objective-C runtime class |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 835 | llvm::Constant *CodeGenModule:: |
| 836 | GetAddrOfConstantCFString(const std::string &str) { |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 837 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 838 | CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 839 | |
| 840 | if (Entry.getValue()) |
| 841 | return Entry.getValue(); |
| 842 | |
| 843 | std::vector<llvm::Constant*> Fields; |
| 844 | |
| 845 | if (!CFConstantStringClassRef) { |
| 846 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 847 | Ty = llvm::ArrayType::get(Ty, 0); |
| 848 | |
| 849 | CFConstantStringClassRef = |
| 850 | new llvm::GlobalVariable(Ty, false, |
| 851 | llvm::GlobalVariable::ExternalLinkage, 0, |
| 852 | "__CFConstantStringClassReference", |
| 853 | &getModule()); |
| 854 | } |
| 855 | |
| 856 | // Class pointer. |
| 857 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 858 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 859 | llvm::Constant *C = |
| 860 | llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2); |
| 861 | Fields.push_back(C); |
| 862 | |
| 863 | // Flags. |
| 864 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 865 | Fields.push_back(llvm::ConstantInt::get(Ty, 1992)); |
| 866 | |
| 867 | // String pointer. |
| 868 | C = llvm::ConstantArray::get(str); |
| 869 | C = new llvm::GlobalVariable(C->getType(), true, |
| 870 | llvm::GlobalValue::InternalLinkage, |
| 871 | C, ".str", &getModule()); |
| 872 | |
| 873 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 874 | Fields.push_back(C); |
| 875 | |
| 876 | // String length. |
| 877 | Ty = getTypes().ConvertType(getContext().LongTy); |
| 878 | Fields.push_back(llvm::ConstantInt::get(Ty, str.length())); |
| 879 | |
| 880 | // The struct. |
| 881 | Ty = getTypes().ConvertType(getContext().getCFConstantStringType()); |
| 882 | C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields); |
Anders Carlsson | 9be009e | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 883 | llvm::GlobalVariable *GV = |
| 884 | new llvm::GlobalVariable(C->getType(), true, |
| 885 | llvm::GlobalVariable::InternalLinkage, |
| 886 | C, "", &getModule()); |
| 887 | GV->setSection("__DATA,__cfstring"); |
| 888 | Entry.setValue(GV); |
| 889 | return GV; |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 890 | } |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 891 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 892 | /// GenerateWritableString -- Creates storage for a string literal. |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 893 | static llvm::Constant *GenerateStringLiteral(const std::string &str, |
| 894 | bool constant, |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 895 | CodeGenModule &CGM) { |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 896 | // Create Constant for this string literal |
| 897 | llvm::Constant *C=llvm::ConstantArray::get(str); |
| 898 | |
| 899 | // Create a global variable for this string |
| 900 | C = new llvm::GlobalVariable(C->getType(), constant, |
| 901 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 902 | C, ".str", &CGM.getModule()); |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 903 | return C; |
| 904 | } |
| 905 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 906 | /// CodeGenModule::GetAddrOfConstantString -- returns a pointer to the character |
| 907 | /// array containing the literal. The result is pointer to array type. |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 908 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str) { |
| 909 | // Don't share any string literals if writable-strings is turned on. |
| 910 | if (Features.WritableStrings) |
| 911 | return GenerateStringLiteral(str, false, *this); |
| 912 | |
| 913 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 914 | ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 915 | |
| 916 | if (Entry.getValue()) |
| 917 | return Entry.getValue(); |
| 918 | |
| 919 | // Create a global variable for this. |
| 920 | llvm::Constant *C = GenerateStringLiteral(str, true, *this); |
| 921 | Entry.setValue(C); |
| 922 | return C; |
| 923 | } |