blob: 5e63a80c94aab29efdb88005f3c487cc3aca827b [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnercf9c9d02007-12-02 07:19:18 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattnerdb6be562007-11-28 05:34:05 +000019#include "clang/Basic/LangOptions.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/Basic/TargetInfo.h"
21#include "llvm/Constants.h"
22#include "llvm/DerivedTypes.h"
Chris Lattnerab862cc2007-08-31 04:31:45 +000023#include "llvm/Module.h"
Chris Lattner4b009652007-07-25 00:24:17 +000024#include "llvm/Intrinsics.h"
Christopher Lamb6db92f32007-12-02 08:49:54 +000025#include <algorithm>
Chris Lattner4b009652007-07-25 00:24:17 +000026using namespace clang;
27using namespace CodeGen;
28
29
Chris Lattnerdb6be562007-11-28 05:34:05 +000030CodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO,
Chris Lattner22595b82007-12-02 01:40:18 +000031 llvm::Module &M, const llvm::TargetData &TD,
32 Diagnostic &diags)
33 : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags),
Chris Lattnercbfb5512008-03-01 08:45:05 +000034 Types(C, M, TD), MemCpyFn(0), MemSetFn(0), CFConstantStringClassRef(0) {
35 //TODO: Make this selectable at runtime
36 Runtime = CreateObjCRuntime(M);
37}
38
39CodeGenModule::~CodeGenModule() {
40 delete Runtime;
41}
Chris Lattner4b009652007-07-25 00:24:17 +000042
Chris Lattnercf9c9d02007-12-02 07:19:18 +000043/// WarnUnsupported - Print out a warning that codegen doesn't support the
44/// specified stmt yet.
45void CodeGenModule::WarnUnsupported(const Stmt *S, const char *Type) {
46 unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning,
47 "cannot codegen this %0 yet");
48 SourceRange Range = S->getSourceRange();
49 std::string Msg = Type;
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000050 getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID,
Ted Kremenekb3ee1932007-12-11 21:27:55 +000051 &Msg, 1, &Range, 1);
Chris Lattnercf9c9d02007-12-02 07:19:18 +000052}
Chris Lattner0e4755d2007-12-02 06:27:33 +000053
Chris Lattner806a5f52008-01-12 07:05:38 +000054/// WarnUnsupported - Print out a warning that codegen doesn't support the
55/// specified decl yet.
56void CodeGenModule::WarnUnsupported(const Decl *D, const char *Type) {
57 unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning,
58 "cannot codegen this %0 yet");
59 std::string Msg = Type;
60 getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID,
61 &Msg, 1);
62}
63
Chris Lattner0e4755d2007-12-02 06:27:33 +000064/// ReplaceMapValuesWith - This is a really slow and bad function that
65/// searches for any entries in GlobalDeclMap that point to OldVal, changing
66/// them to point to NewVal. This is badbadbad, FIXME!
67void CodeGenModule::ReplaceMapValuesWith(llvm::Constant *OldVal,
68 llvm::Constant *NewVal) {
69 for (llvm::DenseMap<const Decl*, llvm::Constant*>::iterator
70 I = GlobalDeclMap.begin(), E = GlobalDeclMap.end(); I != E; ++I)
71 if (I->second == OldVal) I->second = NewVal;
72}
73
74
Chris Lattner1a3c1e22007-12-02 07:09:19 +000075llvm::Constant *CodeGenModule::GetAddrOfFunctionDecl(const FunctionDecl *D,
76 bool isDefinition) {
77 // See if it is already in the map. If so, just return it.
Chris Lattner4b009652007-07-25 00:24:17 +000078 llvm::Constant *&Entry = GlobalDeclMap[D];
79 if (Entry) return Entry;
80
Chris Lattner1a3c1e22007-12-02 07:09:19 +000081 const llvm::Type *Ty = getTypes().ConvertType(D->getType());
82
83 // Check to see if the function already exists.
84 llvm::Function *F = getModule().getFunction(D->getName());
85 const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty);
86
87 // If it doesn't already exist, just create and return an entry.
88 if (F == 0) {
Chris Lattner4b009652007-07-25 00:24:17 +000089 // FIXME: param attributes for sext/zext etc.
90 return Entry = new llvm::Function(FTy, llvm::Function::ExternalLinkage,
91 D->getName(), &getModule());
92 }
93
Chris Lattner1a3c1e22007-12-02 07:09:19 +000094 // If the pointer type matches, just return it.
Christopher Lamb4fe5e702007-12-17 01:11:20 +000095 llvm::Type *PFTy = llvm::PointerType::getUnqual(Ty);
Chris Lattner1a3c1e22007-12-02 07:09:19 +000096 if (PFTy == F->getType()) return Entry = F;
Chris Lattner77ce67c2007-12-02 06:30:46 +000097
Chris Lattner1a3c1e22007-12-02 07:09:19 +000098 // If this isn't a definition, just return it casted to the right type.
99 if (!isDefinition)
100 return Entry = llvm::ConstantExpr::getBitCast(F, PFTy);
101
102 // Otherwise, we have a definition after a prototype with the wrong type.
103 // F is the Function* for the one with the wrong type, we must make a new
104 // Function* and update everything that used F (a declaration) with the new
105 // Function* (which will be a definition).
106 //
107 // This happens if there is a prototype for a function (e.g. "int f()") and
108 // then a definition of a different type (e.g. "int f(int x)"). Start by
109 // making a new function of the correct type, RAUW, then steal the name.
110 llvm::Function *NewFn = new llvm::Function(FTy,
111 llvm::Function::ExternalLinkage,
112 "", &getModule());
113 NewFn->takeName(F);
114
115 // Replace uses of F with the Function we will endow with a body.
116 llvm::Constant *NewPtrForOldDecl =
117 llvm::ConstantExpr::getBitCast(NewFn, F->getType());
118 F->replaceAllUsesWith(NewPtrForOldDecl);
119
120 // FIXME: Update the globaldeclmap for the previous decl of this name. We
121 // really want a way to walk all of these, but we don't have it yet. This
122 // is incredibly slow!
123 ReplaceMapValuesWith(F, NewPtrForOldDecl);
124
125 // Ok, delete the old function now, which is dead.
126 assert(F->isDeclaration() && "Shouldn't replace non-declaration");
127 F->eraseFromParent();
128
129 // Return the new function which has the right type.
130 return Entry = NewFn;
131}
132
Chris Lattner0db06992008-02-05 06:37:34 +0000133static bool IsZeroElementArray(const llvm::Type *Ty) {
134 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(Ty))
135 return ATy->getNumElements() == 0;
136 return false;
137}
138
Chris Lattnerd2df2b52007-12-18 08:16:44 +0000139llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
140 bool isDefinition) {
141 assert(D->hasGlobalStorage() && "Not a global variable");
142
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000143 // See if it is already in the map.
144 llvm::Constant *&Entry = GlobalDeclMap[D];
145 if (Entry) return Entry;
146
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000147 QualType ASTTy = D->getType();
148 const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy);
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000149
150 // Check to see if the global already exists.
Chris Lattnered430e92008-02-02 04:43:11 +0000151 llvm::GlobalVariable *GV = getModule().getGlobalVariable(D->getName(), true);
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000152
153 // If it doesn't already exist, just create and return an entry.
154 if (GV == 0) {
155 return Entry = new llvm::GlobalVariable(Ty, false,
156 llvm::GlobalValue::ExternalLinkage,
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000157 0, D->getName(), &getModule(), 0,
158 ASTTy.getAddressSpace());
Chris Lattner77ce67c2007-12-02 06:30:46 +0000159 }
160
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000161 // If the pointer type matches, just return it.
Christopher Lamb4fe5e702007-12-17 01:11:20 +0000162 llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000163 if (PTy == GV->getType()) return Entry = GV;
164
165 // If this isn't a definition, just return it casted to the right type.
166 if (!isDefinition)
167 return Entry = llvm::ConstantExpr::getBitCast(GV, PTy);
168
169
170 // Otherwise, we have a definition after a prototype with the wrong type.
171 // GV is the GlobalVariable* for the one with the wrong type, we must make a
172 /// new GlobalVariable* and update everything that used GV (a declaration)
173 // with the new GlobalVariable* (which will be a definition).
174 //
175 // This happens if there is a prototype for a global (e.g. "extern int x[];")
176 // and then a definition of a different type (e.g. "int x[10];"). Start by
177 // making a new global of the correct type, RAUW, then steal the name.
178 llvm::GlobalVariable *NewGV =
179 new llvm::GlobalVariable(Ty, false, llvm::GlobalValue::ExternalLinkage,
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000180 0, D->getName(), &getModule(), 0,
181 ASTTy.getAddressSpace());
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000182 NewGV->takeName(GV);
183
184 // Replace uses of GV with the globalvalue we will endow with a body.
185 llvm::Constant *NewPtrForOldDecl =
186 llvm::ConstantExpr::getBitCast(NewGV, GV->getType());
187 GV->replaceAllUsesWith(NewPtrForOldDecl);
188
189 // FIXME: Update the globaldeclmap for the previous decl of this name. We
190 // really want a way to walk all of these, but we don't have it yet. This
191 // is incredibly slow!
192 ReplaceMapValuesWith(GV, NewPtrForOldDecl);
193
Chris Lattner0db06992008-02-05 06:37:34 +0000194 // Verify that GV was a declaration or something like x[] which turns into
195 // [0 x type].
196 assert((GV->isDeclaration() ||
197 IsZeroElementArray(GV->getType()->getElementType())) &&
198 "Shouldn't replace non-declaration");
199
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000200 // Ok, delete the old global now, which is dead.
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000201 GV->eraseFromParent();
202
203 // Return the new global which has the right type.
204 return Entry = NewGV;
Chris Lattner4b009652007-07-25 00:24:17 +0000205}
206
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000207
Chris Lattner4b009652007-07-25 00:24:17 +0000208void CodeGenModule::EmitFunction(const FunctionDecl *FD) {
209 // If this is not a prototype, emit the body.
210 if (FD->getBody())
211 CodeGenFunction(*this).GenerateCode(FD);
212}
213
Anders Carlssond76cead2008-01-26 01:36:00 +0000214llvm::Constant *CodeGenModule::EmitGlobalInit(const Expr *Expr) {
215 return EmitConstantExpr(Expr);
Devang Patel08a10cc2007-10-30 21:27:20 +0000216}
217
Chris Lattner4b009652007-07-25 00:24:17 +0000218void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) {
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000219 // If this is just a forward declaration of the variable, don't emit it now,
220 // allow it to be emitted lazily on its first use.
Chris Lattner4b009652007-07-25 00:24:17 +0000221 if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0)
222 return;
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000223
224 // Get the global, forcing it to be a direct reference.
225 llvm::GlobalVariable *GV =
Chris Lattnerd2df2b52007-12-18 08:16:44 +0000226 cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, true));
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000227
228 // Convert the initializer, or use zero if appropriate.
Chris Lattner4b009652007-07-25 00:24:17 +0000229 llvm::Constant *Init = 0;
230 if (D->getInit() == 0) {
231 Init = llvm::Constant::getNullValue(GV->getType()->getElementType());
232 } else if (D->getType()->isIntegerType()) {
Hartmut Kaiserff08d2c2007-10-17 15:00:17 +0000233 llvm::APSInt Value(static_cast<uint32_t>(
Chris Lattnera96e0d82007-09-04 02:34:27 +0000234 getContext().getTypeSize(D->getInit()->getType(), SourceLocation())));
Chris Lattner4b009652007-07-25 00:24:17 +0000235 if (D->getInit()->isIntegerConstantExpr(Value, Context))
236 Init = llvm::ConstantInt::get(Value);
237 }
Devang Patel8b5f5302007-10-26 16:31:40 +0000238
Devang Patel08a10cc2007-10-30 21:27:20 +0000239 if (!Init)
Oliver Hunt253e0a72007-12-02 00:11:25 +0000240 Init = EmitGlobalInit(D->getInit());
Devang Patel8b5f5302007-10-26 16:31:40 +0000241
Chris Lattnerc7e4f672007-12-10 00:05:55 +0000242 assert(GV->getType()->getElementType() == Init->getType() &&
243 "Initializer codegen type mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000244 GV->setInitializer(Init);
245
246 // Set the llvm linkage type as appropriate.
247 // FIXME: This isn't right. This should handle common linkage and other
248 // stuff.
249 switch (D->getStorageClass()) {
250 case VarDecl::Auto:
251 case VarDecl::Register:
252 assert(0 && "Can't have auto or register globals");
253 case VarDecl::None:
Lauro Ramos Venanciob38c0742008-02-19 00:04:15 +0000254 if (!D->getInit())
255 GV->setLinkage(llvm::GlobalVariable::WeakLinkage);
256 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000257 case VarDecl::Extern:
Steve Naroff1cbb2762008-01-25 22:14:40 +0000258 case VarDecl::PrivateExtern:
Chris Lattner4b009652007-07-25 00:24:17 +0000259 // todo: common
260 break;
261 case VarDecl::Static:
262 GV->setLinkage(llvm::GlobalVariable::InternalLinkage);
263 break;
264 }
265}
266
267/// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified
268/// declarator chain.
269void CodeGenModule::EmitGlobalVarDeclarator(const FileVarDecl *D) {
270 for (; D; D = cast_or_null<FileVarDecl>(D->getNextDeclarator()))
271 EmitGlobalVar(D);
272}
273
Chris Lattner9ec3ca22008-02-06 05:08:19 +0000274void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
275 // Make sure that this type is translated.
276 Types.UpdateCompletedType(TD);
Chris Lattner1b22f8b2008-02-05 08:06:13 +0000277}
278
279
Chris Lattnerab862cc2007-08-31 04:31:45 +0000280/// getBuiltinLibFunction
281llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) {
Chris Lattner9f2d6892007-12-13 00:38:03 +0000282 if (BuiltinID > BuiltinFunctions.size())
283 BuiltinFunctions.resize(BuiltinID);
Chris Lattnerab862cc2007-08-31 04:31:45 +0000284
Chris Lattner9f2d6892007-12-13 00:38:03 +0000285 // Cache looked up functions. Since builtin id #0 is invalid we don't reserve
286 // a slot for it.
287 assert(BuiltinID && "Invalid Builtin ID");
288 llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1];
Chris Lattnerab862cc2007-08-31 04:31:45 +0000289 if (FunctionSlot)
290 return FunctionSlot;
291
292 assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn");
293
294 // Get the name, skip over the __builtin_ prefix.
295 const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10;
296
297 // Get the type for the builtin.
298 QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context);
299 const llvm::FunctionType *Ty =
300 cast<llvm::FunctionType>(getTypes().ConvertType(Type));
301
302 // FIXME: This has a serious problem with code like this:
303 // void abs() {}
304 // ... __builtin_abs(x);
305 // The two versions of abs will collide. The fix is for the builtin to win,
306 // and for the existing one to be turned into a constantexpr cast of the
307 // builtin. In the case where the existing one is a static function, it
308 // should just be renamed.
Chris Lattner02c60f52007-08-31 04:44:06 +0000309 if (llvm::Function *Existing = getModule().getFunction(Name)) {
310 if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage())
311 return FunctionSlot = Existing;
312 assert(Existing == 0 && "FIXME: Name collision");
313 }
Chris Lattnerab862cc2007-08-31 04:31:45 +0000314
315 // FIXME: param attributes for sext/zext etc.
316 return FunctionSlot = new llvm::Function(Ty, llvm::Function::ExternalLinkage,
317 Name, &getModule());
318}
319
Chris Lattner4b23f942007-12-18 00:25:38 +0000320llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys,
321 unsigned NumTys) {
322 return llvm::Intrinsic::getDeclaration(&getModule(),
323 (llvm::Intrinsic::ID)IID, Tys, NumTys);
324}
Chris Lattnerab862cc2007-08-31 04:31:45 +0000325
Chris Lattner4b009652007-07-25 00:24:17 +0000326llvm::Function *CodeGenModule::getMemCpyFn() {
327 if (MemCpyFn) return MemCpyFn;
328 llvm::Intrinsic::ID IID;
329 uint64_t Size; unsigned Align;
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000330 Context.Target.getPointerInfo(Size, Align, FullSourceLoc());
Chris Lattner4b009652007-07-25 00:24:17 +0000331 switch (Size) {
332 default: assert(0 && "Unknown ptr width");
333 case 32: IID = llvm::Intrinsic::memcpy_i32; break;
334 case 64: IID = llvm::Intrinsic::memcpy_i64; break;
335 }
Chris Lattner4b23f942007-12-18 00:25:38 +0000336 return MemCpyFn = getIntrinsic(IID);
Chris Lattner4b009652007-07-25 00:24:17 +0000337}
Anders Carlsson36a04872007-08-21 00:21:21 +0000338
Lauro Ramos Venancioe5bef732008-02-19 22:01:01 +0000339llvm::Function *CodeGenModule::getMemSetFn() {
340 if (MemSetFn) return MemSetFn;
341 llvm::Intrinsic::ID IID;
342 uint64_t Size; unsigned Align;
343 Context.Target.getPointerInfo(Size, Align, FullSourceLoc());
344 switch (Size) {
345 default: assert(0 && "Unknown ptr width");
346 case 32: IID = llvm::Intrinsic::memset_i32; break;
347 case 64: IID = llvm::Intrinsic::memset_i64; break;
348 }
349 return MemSetFn = getIntrinsic(IID);
350}
Chris Lattner4b23f942007-12-18 00:25:38 +0000351
Chris Lattnerab862cc2007-08-31 04:31:45 +0000352llvm::Constant *CodeGenModule::
353GetAddrOfConstantCFString(const std::string &str) {
Anders Carlsson36a04872007-08-21 00:21:21 +0000354 llvm::StringMapEntry<llvm::Constant *> &Entry =
355 CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
356
357 if (Entry.getValue())
358 return Entry.getValue();
359
360 std::vector<llvm::Constant*> Fields;
361
362 if (!CFConstantStringClassRef) {
363 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
364 Ty = llvm::ArrayType::get(Ty, 0);
365
366 CFConstantStringClassRef =
367 new llvm::GlobalVariable(Ty, false,
368 llvm::GlobalVariable::ExternalLinkage, 0,
369 "__CFConstantStringClassReference",
370 &getModule());
371 }
372
373 // Class pointer.
374 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
375 llvm::Constant *Zeros[] = { Zero, Zero };
376 llvm::Constant *C =
377 llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2);
378 Fields.push_back(C);
379
380 // Flags.
381 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
382 Fields.push_back(llvm::ConstantInt::get(Ty, 1992));
383
384 // String pointer.
385 C = llvm::ConstantArray::get(str);
386 C = new llvm::GlobalVariable(C->getType(), true,
387 llvm::GlobalValue::InternalLinkage,
388 C, ".str", &getModule());
389
390 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
391 Fields.push_back(C);
392
393 // String length.
394 Ty = getTypes().ConvertType(getContext().LongTy);
395 Fields.push_back(llvm::ConstantInt::get(Ty, str.length()));
396
397 // The struct.
398 Ty = getTypes().ConvertType(getContext().getCFConstantStringType());
399 C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields);
Anders Carlsson9be009e2007-11-01 00:41:52 +0000400 llvm::GlobalVariable *GV =
401 new llvm::GlobalVariable(C->getType(), true,
402 llvm::GlobalVariable::InternalLinkage,
403 C, "", &getModule());
404 GV->setSection("__DATA,__cfstring");
405 Entry.setValue(GV);
406 return GV;
Anders Carlsson36a04872007-08-21 00:21:21 +0000407}
Chris Lattnerdb6be562007-11-28 05:34:05 +0000408
Chris Lattnera6dcce32008-02-11 00:02:17 +0000409/// GenerateWritableString -- Creates storage for a string literal.
Chris Lattnerdb6be562007-11-28 05:34:05 +0000410static llvm::Constant *GenerateStringLiteral(const std::string &str,
411 bool constant,
Chris Lattnercf9c9d02007-12-02 07:19:18 +0000412 CodeGenModule &CGM) {
Chris Lattnerdb6be562007-11-28 05:34:05 +0000413 // Create Constant for this string literal
414 llvm::Constant *C=llvm::ConstantArray::get(str);
415
416 // Create a global variable for this string
417 C = new llvm::GlobalVariable(C->getType(), constant,
418 llvm::GlobalValue::InternalLinkage,
Chris Lattnercf9c9d02007-12-02 07:19:18 +0000419 C, ".str", &CGM.getModule());
Chris Lattnerdb6be562007-11-28 05:34:05 +0000420 return C;
421}
422
Chris Lattnera6dcce32008-02-11 00:02:17 +0000423/// CodeGenModule::GetAddrOfConstantString -- returns a pointer to the character
424/// array containing the literal. The result is pointer to array type.
Chris Lattnerdb6be562007-11-28 05:34:05 +0000425llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str) {
426 // Don't share any string literals if writable-strings is turned on.
427 if (Features.WritableStrings)
428 return GenerateStringLiteral(str, false, *this);
429
430 llvm::StringMapEntry<llvm::Constant *> &Entry =
431 ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
432
433 if (Entry.getValue())
434 return Entry.getValue();
435
436 // Create a global variable for this.
437 llvm::Constant *C = GenerateStringLiteral(str, true, *this);
438 Entry.setValue(C);
439 return C;
440}