blob: 0b39ca4437817e2388f1c6ad0034c6555f7112e5 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the per-module state used while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenModule.h"
15#include "CodeGenFunction.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Chris Lattner2c8569d2007-12-02 07:19:18 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattner45e8cbd2007-11-28 05:34:05 +000019#include "clang/Basic/LangOptions.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Basic/TargetInfo.h"
Nate Begemanec9426c2008-03-09 03:09:36 +000021#include "llvm/CallingConv.h"
Chris Lattner8f32f712007-07-14 00:23:28 +000022#include "llvm/Constants.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "llvm/DerivedTypes.h"
Chris Lattnerbef20ac2007-08-31 04:31:45 +000024#include "llvm/Module.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "llvm/Intrinsics.h"
Christopher Lambce39faa2007-12-02 08:49:54 +000026#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28using namespace CodeGen;
29
30
Chris Lattner45e8cbd2007-11-28 05:34:05 +000031CodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO,
Chris Lattnerfb97b032007-12-02 01:40:18 +000032 llvm::Module &M, const llvm::TargetData &TD,
33 Diagnostic &diags)
34 : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags),
Chris Lattner2b94fe32008-03-01 08:45:05 +000035 Types(C, M, TD), MemCpyFn(0), MemSetFn(0), CFConstantStringClassRef(0) {
36 //TODO: Make this selectable at runtime
37 Runtime = CreateObjCRuntime(M);
38}
39
40CodeGenModule::~CodeGenModule() {
41 delete Runtime;
42}
Reid Spencer5f016e22007-07-11 17:01:13 +000043
Chris Lattner2c8569d2007-12-02 07:19:18 +000044/// WarnUnsupported - Print out a warning that codegen doesn't support the
45/// specified stmt yet.
46void CodeGenModule::WarnUnsupported(const Stmt *S, const char *Type) {
47 unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning,
48 "cannot codegen this %0 yet");
49 SourceRange Range = S->getSourceRange();
50 std::string Msg = Type;
Ted Kremenek9c728dc2007-12-12 22:39:36 +000051 getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID,
Ted Kremenek7a9d49f2007-12-11 21:27:55 +000052 &Msg, 1, &Range, 1);
Chris Lattner2c8569d2007-12-02 07:19:18 +000053}
Chris Lattner58c3f9e2007-12-02 06:27:33 +000054
Chris Lattnerc6fdc342008-01-12 07:05:38 +000055/// WarnUnsupported - Print out a warning that codegen doesn't support the
56/// specified decl yet.
57void CodeGenModule::WarnUnsupported(const Decl *D, const char *Type) {
58 unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning,
59 "cannot codegen this %0 yet");
60 std::string Msg = Type;
61 getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID,
62 &Msg, 1);
63}
64
Chris Lattner58c3f9e2007-12-02 06:27:33 +000065/// ReplaceMapValuesWith - This is a really slow and bad function that
66/// searches for any entries in GlobalDeclMap that point to OldVal, changing
67/// them to point to NewVal. This is badbadbad, FIXME!
68void CodeGenModule::ReplaceMapValuesWith(llvm::Constant *OldVal,
69 llvm::Constant *NewVal) {
70 for (llvm::DenseMap<const Decl*, llvm::Constant*>::iterator
71 I = GlobalDeclMap.begin(), E = GlobalDeclMap.end(); I != E; ++I)
72 if (I->second == OldVal) I->second = NewVal;
73}
74
75
Chris Lattner9cd4fe42007-12-02 07:09:19 +000076llvm::Constant *CodeGenModule::GetAddrOfFunctionDecl(const FunctionDecl *D,
77 bool isDefinition) {
78 // See if it is already in the map. If so, just return it.
Reid Spencer5f016e22007-07-11 17:01:13 +000079 llvm::Constant *&Entry = GlobalDeclMap[D];
80 if (Entry) return Entry;
81
Chris Lattner9cd4fe42007-12-02 07:09:19 +000082 const llvm::Type *Ty = getTypes().ConvertType(D->getType());
83
84 // Check to see if the function already exists.
85 llvm::Function *F = getModule().getFunction(D->getName());
86 const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty);
87
88 // If it doesn't already exist, just create and return an entry.
89 if (F == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +000090 // FIXME: param attributes for sext/zext etc.
Nate Begemanec9426c2008-03-09 03:09:36 +000091 F = new llvm::Function(FTy, llvm::Function::ExternalLinkage, D->getName(),
92 &getModule());
93
94 // Set the appropriate calling convention for the Function.
95 if (D->getAttr<FastCallAttr>())
96 F->setCallingConv(llvm::CallingConv::Fast);
97 return Entry = F;
Reid Spencer5f016e22007-07-11 17:01:13 +000098 }
99
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000100 // If the pointer type matches, just return it.
Christopher Lambddc23f32007-12-17 01:11:20 +0000101 llvm::Type *PFTy = llvm::PointerType::getUnqual(Ty);
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000102 if (PFTy == F->getType()) return Entry = F;
Chris Lattnerfafad832007-12-02 06:30:46 +0000103
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000104 // If this isn't a definition, just return it casted to the right type.
105 if (!isDefinition)
106 return Entry = llvm::ConstantExpr::getBitCast(F, PFTy);
107
108 // Otherwise, we have a definition after a prototype with the wrong type.
109 // F is the Function* for the one with the wrong type, we must make a new
110 // Function* and update everything that used F (a declaration) with the new
111 // Function* (which will be a definition).
112 //
113 // This happens if there is a prototype for a function (e.g. "int f()") and
114 // then a definition of a different type (e.g. "int f(int x)"). Start by
115 // making a new function of the correct type, RAUW, then steal the name.
116 llvm::Function *NewFn = new llvm::Function(FTy,
117 llvm::Function::ExternalLinkage,
118 "", &getModule());
119 NewFn->takeName(F);
120
121 // Replace uses of F with the Function we will endow with a body.
122 llvm::Constant *NewPtrForOldDecl =
123 llvm::ConstantExpr::getBitCast(NewFn, F->getType());
124 F->replaceAllUsesWith(NewPtrForOldDecl);
125
126 // FIXME: Update the globaldeclmap for the previous decl of this name. We
127 // really want a way to walk all of these, but we don't have it yet. This
128 // is incredibly slow!
129 ReplaceMapValuesWith(F, NewPtrForOldDecl);
130
131 // Ok, delete the old function now, which is dead.
132 assert(F->isDeclaration() && "Shouldn't replace non-declaration");
133 F->eraseFromParent();
134
135 // Return the new function which has the right type.
136 return Entry = NewFn;
137}
138
Chris Lattnerc4b23a52008-02-05 06:37:34 +0000139static bool IsZeroElementArray(const llvm::Type *Ty) {
140 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(Ty))
141 return ATy->getNumElements() == 0;
142 return false;
143}
144
Chris Lattner2b9d2ca2007-12-18 08:16:44 +0000145llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
146 bool isDefinition) {
147 assert(D->hasGlobalStorage() && "Not a global variable");
148
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000149 // See if it is already in the map.
150 llvm::Constant *&Entry = GlobalDeclMap[D];
151 if (Entry) return Entry;
152
Christopher Lambebb97e92008-02-04 02:31:56 +0000153 QualType ASTTy = D->getType();
154 const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy);
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000155
156 // Check to see if the global already exists.
Chris Lattner49573782008-02-02 04:43:11 +0000157 llvm::GlobalVariable *GV = getModule().getGlobalVariable(D->getName(), true);
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000158
159 // If it doesn't already exist, just create and return an entry.
160 if (GV == 0) {
161 return Entry = new llvm::GlobalVariable(Ty, false,
162 llvm::GlobalValue::ExternalLinkage,
Christopher Lambebb97e92008-02-04 02:31:56 +0000163 0, D->getName(), &getModule(), 0,
164 ASTTy.getAddressSpace());
Chris Lattnerfafad832007-12-02 06:30:46 +0000165 }
166
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000167 // If the pointer type matches, just return it.
Christopher Lambddc23f32007-12-17 01:11:20 +0000168 llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000169 if (PTy == GV->getType()) return Entry = GV;
170
171 // If this isn't a definition, just return it casted to the right type.
172 if (!isDefinition)
173 return Entry = llvm::ConstantExpr::getBitCast(GV, PTy);
174
175
176 // Otherwise, we have a definition after a prototype with the wrong type.
177 // GV is the GlobalVariable* for the one with the wrong type, we must make a
178 /// new GlobalVariable* and update everything that used GV (a declaration)
179 // with the new GlobalVariable* (which will be a definition).
180 //
181 // This happens if there is a prototype for a global (e.g. "extern int x[];")
182 // and then a definition of a different type (e.g. "int x[10];"). Start by
183 // making a new global of the correct type, RAUW, then steal the name.
184 llvm::GlobalVariable *NewGV =
185 new llvm::GlobalVariable(Ty, false, llvm::GlobalValue::ExternalLinkage,
Christopher Lambebb97e92008-02-04 02:31:56 +0000186 0, D->getName(), &getModule(), 0,
187 ASTTy.getAddressSpace());
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000188 NewGV->takeName(GV);
189
190 // Replace uses of GV with the globalvalue we will endow with a body.
191 llvm::Constant *NewPtrForOldDecl =
192 llvm::ConstantExpr::getBitCast(NewGV, GV->getType());
193 GV->replaceAllUsesWith(NewPtrForOldDecl);
194
195 // FIXME: Update the globaldeclmap for the previous decl of this name. We
196 // really want a way to walk all of these, but we don't have it yet. This
197 // is incredibly slow!
198 ReplaceMapValuesWith(GV, NewPtrForOldDecl);
199
Chris Lattnerc4b23a52008-02-05 06:37:34 +0000200 // Verify that GV was a declaration or something like x[] which turns into
201 // [0 x type].
202 assert((GV->isDeclaration() ||
203 IsZeroElementArray(GV->getType()->getElementType())) &&
204 "Shouldn't replace non-declaration");
205
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000206 // Ok, delete the old global now, which is dead.
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000207 GV->eraseFromParent();
208
209 // Return the new global which has the right type.
210 return Entry = NewGV;
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000213
Chris Lattner88a69ad2007-07-13 05:13:43 +0000214void CodeGenModule::EmitFunction(const FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 // If this is not a prototype, emit the body.
216 if (FD->getBody())
217 CodeGenFunction(*this).GenerateCode(FD);
218}
219
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000220llvm::Constant *CodeGenModule::EmitGlobalInit(const Expr *Expr) {
221 return EmitConstantExpr(Expr);
Devang Patel9e32d4b2007-10-30 21:27:20 +0000222}
223
Chris Lattner88a69ad2007-07-13 05:13:43 +0000224void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) {
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000225 // If this is just a forward declaration of the variable, don't emit it now,
226 // allow it to be emitted lazily on its first use.
Chris Lattner88a69ad2007-07-13 05:13:43 +0000227 if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0)
228 return;
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000229
230 // Get the global, forcing it to be a direct reference.
231 llvm::GlobalVariable *GV =
Chris Lattner2b9d2ca2007-12-18 08:16:44 +0000232 cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, true));
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000233
234 // Convert the initializer, or use zero if appropriate.
Chris Lattner8f32f712007-07-14 00:23:28 +0000235 llvm::Constant *Init = 0;
236 if (D->getInit() == 0) {
Chris Lattner88a69ad2007-07-13 05:13:43 +0000237 Init = llvm::Constant::getNullValue(GV->getType()->getElementType());
Chris Lattner8f32f712007-07-14 00:23:28 +0000238 } else if (D->getType()->isIntegerType()) {
Hartmut Kaiser7b660002007-10-17 15:00:17 +0000239 llvm::APSInt Value(static_cast<uint32_t>(
Chris Lattner98be4942008-03-05 18:54:05 +0000240 getContext().getTypeSize(D->getInit()->getType())));
Chris Lattner590b6642007-07-15 23:26:56 +0000241 if (D->getInit()->isIntegerConstantExpr(Value, Context))
Chris Lattner8f32f712007-07-14 00:23:28 +0000242 Init = llvm::ConstantInt::get(Value);
243 }
Devang Patel8e53e722007-10-26 16:31:40 +0000244
Devang Patel9e32d4b2007-10-30 21:27:20 +0000245 if (!Init)
Oliver Hunt28247232007-12-02 00:11:25 +0000246 Init = EmitGlobalInit(D->getInit());
Devang Patel8e53e722007-10-26 16:31:40 +0000247
Chris Lattnerf89dfb22007-12-10 00:05:55 +0000248 assert(GV->getType()->getElementType() == Init->getType() &&
249 "Initializer codegen type mismatch!");
Chris Lattner88a69ad2007-07-13 05:13:43 +0000250 GV->setInitializer(Init);
Chris Lattnerddee4232008-03-03 03:28:21 +0000251
252 if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>())
253 GV->setVisibility(attr->getVisibility());
254 // FIXME: else handle -fvisibility
Chris Lattner88a69ad2007-07-13 05:13:43 +0000255
256 // Set the llvm linkage type as appropriate.
Chris Lattnerddee4232008-03-03 03:28:21 +0000257 if (D->getAttr<DLLImportAttr>())
258 GV->setLinkage(llvm::Function::DLLImportLinkage);
259 else if (D->getAttr<DLLExportAttr>())
260 GV->setLinkage(llvm::Function::DLLExportLinkage);
261 else if (D->getAttr<WeakAttr>()) {
262 GV->setLinkage(llvm::GlobalVariable::WeakLinkage);
263
264 } else {
265 // FIXME: This isn't right. This should handle common linkage and other
266 // stuff.
267 switch (D->getStorageClass()) {
268 case VarDecl::Auto:
269 case VarDecl::Register:
270 assert(0 && "Can't have auto or register globals");
271 case VarDecl::None:
272 if (!D->getInit())
273 GV->setLinkage(llvm::GlobalVariable::WeakLinkage);
274 break;
275 case VarDecl::Extern:
276 case VarDecl::PrivateExtern:
277 // todo: common
278 break;
279 case VarDecl::Static:
280 GV->setLinkage(llvm::GlobalVariable::InternalLinkage);
281 break;
282 }
Chris Lattner88a69ad2007-07-13 05:13:43 +0000283 }
284}
Reid Spencer5f016e22007-07-11 17:01:13 +0000285
Chris Lattner32b266c2007-07-14 00:16:50 +0000286/// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified
287/// declarator chain.
288void CodeGenModule::EmitGlobalVarDeclarator(const FileVarDecl *D) {
289 for (; D; D = cast_or_null<FileVarDecl>(D->getNextDeclarator()))
290 EmitGlobalVar(D);
291}
Reid Spencer5f016e22007-07-11 17:01:13 +0000292
Chris Lattnerc5b88062008-02-06 05:08:19 +0000293void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
294 // Make sure that this type is translated.
295 Types.UpdateCompletedType(TD);
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000296}
297
298
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000299/// getBuiltinLibFunction
300llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) {
Chris Lattner1426fec2007-12-13 00:38:03 +0000301 if (BuiltinID > BuiltinFunctions.size())
302 BuiltinFunctions.resize(BuiltinID);
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000303
Chris Lattner1426fec2007-12-13 00:38:03 +0000304 // Cache looked up functions. Since builtin id #0 is invalid we don't reserve
305 // a slot for it.
306 assert(BuiltinID && "Invalid Builtin ID");
307 llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1];
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000308 if (FunctionSlot)
309 return FunctionSlot;
310
311 assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn");
312
313 // Get the name, skip over the __builtin_ prefix.
314 const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10;
315
316 // Get the type for the builtin.
317 QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context);
318 const llvm::FunctionType *Ty =
319 cast<llvm::FunctionType>(getTypes().ConvertType(Type));
320
321 // FIXME: This has a serious problem with code like this:
322 // void abs() {}
323 // ... __builtin_abs(x);
324 // The two versions of abs will collide. The fix is for the builtin to win,
325 // and for the existing one to be turned into a constantexpr cast of the
326 // builtin. In the case where the existing one is a static function, it
327 // should just be renamed.
Chris Lattnerc5e940f2007-08-31 04:44:06 +0000328 if (llvm::Function *Existing = getModule().getFunction(Name)) {
329 if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage())
330 return FunctionSlot = Existing;
331 assert(Existing == 0 && "FIXME: Name collision");
332 }
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000333
334 // FIXME: param attributes for sext/zext etc.
335 return FunctionSlot = new llvm::Function(Ty, llvm::Function::ExternalLinkage,
336 Name, &getModule());
337}
338
Chris Lattner7acda7c2007-12-18 00:25:38 +0000339llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys,
340 unsigned NumTys) {
341 return llvm::Intrinsic::getDeclaration(&getModule(),
342 (llvm::Intrinsic::ID)IID, Tys, NumTys);
343}
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000344
Reid Spencer5f016e22007-07-11 17:01:13 +0000345llvm::Function *CodeGenModule::getMemCpyFn() {
346 if (MemCpyFn) return MemCpyFn;
347 llvm::Intrinsic::ID IID;
Chris Lattnerf72a4432008-03-08 08:34:58 +0000348 switch (Context.Target.getPointerWidth(0)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 default: assert(0 && "Unknown ptr width");
350 case 32: IID = llvm::Intrinsic::memcpy_i32; break;
351 case 64: IID = llvm::Intrinsic::memcpy_i64; break;
352 }
Chris Lattner7acda7c2007-12-18 00:25:38 +0000353 return MemCpyFn = getIntrinsic(IID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000354}
Anders Carlssonc9e20912007-08-21 00:21:21 +0000355
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +0000356llvm::Function *CodeGenModule::getMemSetFn() {
357 if (MemSetFn) return MemSetFn;
358 llvm::Intrinsic::ID IID;
Chris Lattnerf72a4432008-03-08 08:34:58 +0000359 switch (Context.Target.getPointerWidth(0)) {
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +0000360 default: assert(0 && "Unknown ptr width");
361 case 32: IID = llvm::Intrinsic::memset_i32; break;
362 case 64: IID = llvm::Intrinsic::memset_i64; break;
363 }
364 return MemSetFn = getIntrinsic(IID);
365}
Chris Lattner7acda7c2007-12-18 00:25:38 +0000366
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000367llvm::Constant *CodeGenModule::
368GetAddrOfConstantCFString(const std::string &str) {
Anders Carlssonc9e20912007-08-21 00:21:21 +0000369 llvm::StringMapEntry<llvm::Constant *> &Entry =
370 CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
371
372 if (Entry.getValue())
373 return Entry.getValue();
374
375 std::vector<llvm::Constant*> Fields;
376
377 if (!CFConstantStringClassRef) {
378 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
379 Ty = llvm::ArrayType::get(Ty, 0);
380
381 CFConstantStringClassRef =
382 new llvm::GlobalVariable(Ty, false,
383 llvm::GlobalVariable::ExternalLinkage, 0,
384 "__CFConstantStringClassReference",
385 &getModule());
386 }
387
388 // Class pointer.
389 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
390 llvm::Constant *Zeros[] = { Zero, Zero };
391 llvm::Constant *C =
392 llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2);
393 Fields.push_back(C);
394
395 // Flags.
396 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
397 Fields.push_back(llvm::ConstantInt::get(Ty, 1992));
398
399 // String pointer.
400 C = llvm::ConstantArray::get(str);
401 C = new llvm::GlobalVariable(C->getType(), true,
402 llvm::GlobalValue::InternalLinkage,
403 C, ".str", &getModule());
404
405 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
406 Fields.push_back(C);
407
408 // String length.
409 Ty = getTypes().ConvertType(getContext().LongTy);
410 Fields.push_back(llvm::ConstantInt::get(Ty, str.length()));
411
412 // The struct.
413 Ty = getTypes().ConvertType(getContext().getCFConstantStringType());
414 C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields);
Anders Carlsson0c678292007-11-01 00:41:52 +0000415 llvm::GlobalVariable *GV =
416 new llvm::GlobalVariable(C->getType(), true,
417 llvm::GlobalVariable::InternalLinkage,
418 C, "", &getModule());
419 GV->setSection("__DATA,__cfstring");
420 Entry.setValue(GV);
421 return GV;
Anders Carlssonc9e20912007-08-21 00:21:21 +0000422}
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000423
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000424/// GenerateWritableString -- Creates storage for a string literal.
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000425static llvm::Constant *GenerateStringLiteral(const std::string &str,
426 bool constant,
Chris Lattner2c8569d2007-12-02 07:19:18 +0000427 CodeGenModule &CGM) {
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000428 // Create Constant for this string literal
429 llvm::Constant *C=llvm::ConstantArray::get(str);
430
431 // Create a global variable for this string
432 C = new llvm::GlobalVariable(C->getType(), constant,
433 llvm::GlobalValue::InternalLinkage,
Chris Lattner2c8569d2007-12-02 07:19:18 +0000434 C, ".str", &CGM.getModule());
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000435 return C;
436}
437
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000438/// CodeGenModule::GetAddrOfConstantString -- returns a pointer to the character
439/// array containing the literal. The result is pointer to array type.
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000440llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str) {
441 // Don't share any string literals if writable-strings is turned on.
442 if (Features.WritableStrings)
443 return GenerateStringLiteral(str, false, *this);
444
445 llvm::StringMapEntry<llvm::Constant *> &Entry =
446 ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
447
448 if (Entry.getValue())
449 return Entry.getValue();
450
451 // Create a global variable for this.
452 llvm::Constant *C = GenerateStringLiteral(str, true, *this);
453 Entry.setValue(C);
454 return C;
455}