blob: d2b60475250bf7a7c65115284f8c236029f8f937 [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 Lattner8cd0e932008-03-05 18:54:05 +0000234 getContext().getTypeSize(D->getInit()->getType())));
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);
Chris Lattner402b3372008-03-03 03:28:21 +0000245
246 if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>())
247 GV->setVisibility(attr->getVisibility());
248 // FIXME: else handle -fvisibility
Chris Lattner4b009652007-07-25 00:24:17 +0000249
250 // Set the llvm linkage type as appropriate.
Chris Lattner402b3372008-03-03 03:28:21 +0000251 if (D->getAttr<DLLImportAttr>())
252 GV->setLinkage(llvm::Function::DLLImportLinkage);
253 else if (D->getAttr<DLLExportAttr>())
254 GV->setLinkage(llvm::Function::DLLExportLinkage);
255 else if (D->getAttr<WeakAttr>()) {
256 GV->setLinkage(llvm::GlobalVariable::WeakLinkage);
257
258 } else {
259 // FIXME: This isn't right. This should handle common linkage and other
260 // stuff.
261 switch (D->getStorageClass()) {
262 case VarDecl::Auto:
263 case VarDecl::Register:
264 assert(0 && "Can't have auto or register globals");
265 case VarDecl::None:
266 if (!D->getInit())
267 GV->setLinkage(llvm::GlobalVariable::WeakLinkage);
268 break;
269 case VarDecl::Extern:
270 case VarDecl::PrivateExtern:
271 // todo: common
272 break;
273 case VarDecl::Static:
274 GV->setLinkage(llvm::GlobalVariable::InternalLinkage);
275 break;
276 }
Chris Lattner4b009652007-07-25 00:24:17 +0000277 }
278}
279
280/// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified
281/// declarator chain.
282void CodeGenModule::EmitGlobalVarDeclarator(const FileVarDecl *D) {
283 for (; D; D = cast_or_null<FileVarDecl>(D->getNextDeclarator()))
284 EmitGlobalVar(D);
285}
286
Chris Lattner9ec3ca22008-02-06 05:08:19 +0000287void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
288 // Make sure that this type is translated.
289 Types.UpdateCompletedType(TD);
Chris Lattner1b22f8b2008-02-05 08:06:13 +0000290}
291
292
Chris Lattnerab862cc2007-08-31 04:31:45 +0000293/// getBuiltinLibFunction
294llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) {
Chris Lattner9f2d6892007-12-13 00:38:03 +0000295 if (BuiltinID > BuiltinFunctions.size())
296 BuiltinFunctions.resize(BuiltinID);
Chris Lattnerab862cc2007-08-31 04:31:45 +0000297
Chris Lattner9f2d6892007-12-13 00:38:03 +0000298 // Cache looked up functions. Since builtin id #0 is invalid we don't reserve
299 // a slot for it.
300 assert(BuiltinID && "Invalid Builtin ID");
301 llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1];
Chris Lattnerab862cc2007-08-31 04:31:45 +0000302 if (FunctionSlot)
303 return FunctionSlot;
304
305 assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn");
306
307 // Get the name, skip over the __builtin_ prefix.
308 const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10;
309
310 // Get the type for the builtin.
311 QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context);
312 const llvm::FunctionType *Ty =
313 cast<llvm::FunctionType>(getTypes().ConvertType(Type));
314
315 // FIXME: This has a serious problem with code like this:
316 // void abs() {}
317 // ... __builtin_abs(x);
318 // The two versions of abs will collide. The fix is for the builtin to win,
319 // and for the existing one to be turned into a constantexpr cast of the
320 // builtin. In the case where the existing one is a static function, it
321 // should just be renamed.
Chris Lattner02c60f52007-08-31 04:44:06 +0000322 if (llvm::Function *Existing = getModule().getFunction(Name)) {
323 if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage())
324 return FunctionSlot = Existing;
325 assert(Existing == 0 && "FIXME: Name collision");
326 }
Chris Lattnerab862cc2007-08-31 04:31:45 +0000327
328 // FIXME: param attributes for sext/zext etc.
329 return FunctionSlot = new llvm::Function(Ty, llvm::Function::ExternalLinkage,
330 Name, &getModule());
331}
332
Chris Lattner4b23f942007-12-18 00:25:38 +0000333llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys,
334 unsigned NumTys) {
335 return llvm::Intrinsic::getDeclaration(&getModule(),
336 (llvm::Intrinsic::ID)IID, Tys, NumTys);
337}
Chris Lattnerab862cc2007-08-31 04:31:45 +0000338
Chris Lattner4b009652007-07-25 00:24:17 +0000339llvm::Function *CodeGenModule::getMemCpyFn() {
340 if (MemCpyFn) return MemCpyFn;
341 llvm::Intrinsic::ID IID;
Chris Lattner461a6c52008-03-08 08:34:58 +0000342 switch (Context.Target.getPointerWidth(0)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000343 default: assert(0 && "Unknown ptr width");
344 case 32: IID = llvm::Intrinsic::memcpy_i32; break;
345 case 64: IID = llvm::Intrinsic::memcpy_i64; break;
346 }
Chris Lattner4b23f942007-12-18 00:25:38 +0000347 return MemCpyFn = getIntrinsic(IID);
Chris Lattner4b009652007-07-25 00:24:17 +0000348}
Anders Carlsson36a04872007-08-21 00:21:21 +0000349
Lauro Ramos Venancioe5bef732008-02-19 22:01:01 +0000350llvm::Function *CodeGenModule::getMemSetFn() {
351 if (MemSetFn) return MemSetFn;
352 llvm::Intrinsic::ID IID;
Chris Lattner461a6c52008-03-08 08:34:58 +0000353 switch (Context.Target.getPointerWidth(0)) {
Lauro Ramos Venancioe5bef732008-02-19 22:01:01 +0000354 default: assert(0 && "Unknown ptr width");
355 case 32: IID = llvm::Intrinsic::memset_i32; break;
356 case 64: IID = llvm::Intrinsic::memset_i64; break;
357 }
358 return MemSetFn = getIntrinsic(IID);
359}
Chris Lattner4b23f942007-12-18 00:25:38 +0000360
Chris Lattnerab862cc2007-08-31 04:31:45 +0000361llvm::Constant *CodeGenModule::
362GetAddrOfConstantCFString(const std::string &str) {
Anders Carlsson36a04872007-08-21 00:21:21 +0000363 llvm::StringMapEntry<llvm::Constant *> &Entry =
364 CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
365
366 if (Entry.getValue())
367 return Entry.getValue();
368
369 std::vector<llvm::Constant*> Fields;
370
371 if (!CFConstantStringClassRef) {
372 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
373 Ty = llvm::ArrayType::get(Ty, 0);
374
375 CFConstantStringClassRef =
376 new llvm::GlobalVariable(Ty, false,
377 llvm::GlobalVariable::ExternalLinkage, 0,
378 "__CFConstantStringClassReference",
379 &getModule());
380 }
381
382 // Class pointer.
383 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
384 llvm::Constant *Zeros[] = { Zero, Zero };
385 llvm::Constant *C =
386 llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2);
387 Fields.push_back(C);
388
389 // Flags.
390 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
391 Fields.push_back(llvm::ConstantInt::get(Ty, 1992));
392
393 // String pointer.
394 C = llvm::ConstantArray::get(str);
395 C = new llvm::GlobalVariable(C->getType(), true,
396 llvm::GlobalValue::InternalLinkage,
397 C, ".str", &getModule());
398
399 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
400 Fields.push_back(C);
401
402 // String length.
403 Ty = getTypes().ConvertType(getContext().LongTy);
404 Fields.push_back(llvm::ConstantInt::get(Ty, str.length()));
405
406 // The struct.
407 Ty = getTypes().ConvertType(getContext().getCFConstantStringType());
408 C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields);
Anders Carlsson9be009e2007-11-01 00:41:52 +0000409 llvm::GlobalVariable *GV =
410 new llvm::GlobalVariable(C->getType(), true,
411 llvm::GlobalVariable::InternalLinkage,
412 C, "", &getModule());
413 GV->setSection("__DATA,__cfstring");
414 Entry.setValue(GV);
415 return GV;
Anders Carlsson36a04872007-08-21 00:21:21 +0000416}
Chris Lattnerdb6be562007-11-28 05:34:05 +0000417
Chris Lattnera6dcce32008-02-11 00:02:17 +0000418/// GenerateWritableString -- Creates storage for a string literal.
Chris Lattnerdb6be562007-11-28 05:34:05 +0000419static llvm::Constant *GenerateStringLiteral(const std::string &str,
420 bool constant,
Chris Lattnercf9c9d02007-12-02 07:19:18 +0000421 CodeGenModule &CGM) {
Chris Lattnerdb6be562007-11-28 05:34:05 +0000422 // Create Constant for this string literal
423 llvm::Constant *C=llvm::ConstantArray::get(str);
424
425 // Create a global variable for this string
426 C = new llvm::GlobalVariable(C->getType(), constant,
427 llvm::GlobalValue::InternalLinkage,
Chris Lattnercf9c9d02007-12-02 07:19:18 +0000428 C, ".str", &CGM.getModule());
Chris Lattnerdb6be562007-11-28 05:34:05 +0000429 return C;
430}
431
Chris Lattnera6dcce32008-02-11 00:02:17 +0000432/// CodeGenModule::GetAddrOfConstantString -- returns a pointer to the character
433/// array containing the literal. The result is pointer to array type.
Chris Lattnerdb6be562007-11-28 05:34:05 +0000434llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str) {
435 // Don't share any string literals if writable-strings is turned on.
436 if (Features.WritableStrings)
437 return GenerateStringLiteral(str, false, *this);
438
439 llvm::StringMapEntry<llvm::Constant *> &Entry =
440 ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
441
442 if (Entry.getValue())
443 return Entry.getValue();
444
445 // Create a global variable for this.
446 llvm::Constant *C = GenerateStringLiteral(str, true, *this);
447 Entry.setValue(C);
448 return C;
449}