blob: eff26f91b12dcc055eb9a12f143471130226fea7 [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"
Chris Lattner8f32f712007-07-14 00:23:28 +000021#include "llvm/Constants.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/DerivedTypes.h"
Chris Lattnerbef20ac2007-08-31 04:31:45 +000023#include "llvm/Module.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "llvm/Intrinsics.h"
Christopher Lambce39faa2007-12-02 08:49:54 +000025#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27using namespace CodeGen;
28
29
Chris Lattner45e8cbd2007-11-28 05:34:05 +000030CodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO,
Chris Lattnerfb97b032007-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),
Devang Patel7a4718e2007-10-31 20:01:01 +000034 Types(C, M, TD), MemCpyFn(0), CFConstantStringClassRef(0) {}
Reid Spencer5f016e22007-07-11 17:01:13 +000035
Chris Lattner2c8569d2007-12-02 07:19:18 +000036/// WarnUnsupported - Print out a warning that codegen doesn't support the
37/// specified stmt yet.
38void CodeGenModule::WarnUnsupported(const Stmt *S, const char *Type) {
39 unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning,
40 "cannot codegen this %0 yet");
41 SourceRange Range = S->getSourceRange();
42 std::string Msg = Type;
Ted Kremenek9c728dc2007-12-12 22:39:36 +000043 getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID,
Ted Kremenek7a9d49f2007-12-11 21:27:55 +000044 &Msg, 1, &Range, 1);
Chris Lattner2c8569d2007-12-02 07:19:18 +000045}
Chris Lattner58c3f9e2007-12-02 06:27:33 +000046
Chris Lattnerc6fdc342008-01-12 07:05:38 +000047/// WarnUnsupported - Print out a warning that codegen doesn't support the
48/// specified decl yet.
49void CodeGenModule::WarnUnsupported(const Decl *D, const char *Type) {
50 unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning,
51 "cannot codegen this %0 yet");
52 std::string Msg = Type;
53 getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID,
54 &Msg, 1);
55}
56
Chris Lattner58c3f9e2007-12-02 06:27:33 +000057/// ReplaceMapValuesWith - This is a really slow and bad function that
58/// searches for any entries in GlobalDeclMap that point to OldVal, changing
59/// them to point to NewVal. This is badbadbad, FIXME!
60void CodeGenModule::ReplaceMapValuesWith(llvm::Constant *OldVal,
61 llvm::Constant *NewVal) {
62 for (llvm::DenseMap<const Decl*, llvm::Constant*>::iterator
63 I = GlobalDeclMap.begin(), E = GlobalDeclMap.end(); I != E; ++I)
64 if (I->second == OldVal) I->second = NewVal;
65}
66
67
Chris Lattner9cd4fe42007-12-02 07:09:19 +000068llvm::Constant *CodeGenModule::GetAddrOfFunctionDecl(const FunctionDecl *D,
69 bool isDefinition) {
70 // See if it is already in the map. If so, just return it.
Reid Spencer5f016e22007-07-11 17:01:13 +000071 llvm::Constant *&Entry = GlobalDeclMap[D];
72 if (Entry) return Entry;
73
Chris Lattner9cd4fe42007-12-02 07:09:19 +000074 const llvm::Type *Ty = getTypes().ConvertType(D->getType());
75
76 // Check to see if the function already exists.
77 llvm::Function *F = getModule().getFunction(D->getName());
78 const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty);
79
80 // If it doesn't already exist, just create and return an entry.
81 if (F == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +000082 // FIXME: param attributes for sext/zext etc.
83 return Entry = new llvm::Function(FTy, llvm::Function::ExternalLinkage,
84 D->getName(), &getModule());
85 }
86
Chris Lattner9cd4fe42007-12-02 07:09:19 +000087 // If the pointer type matches, just return it.
Christopher Lambddc23f32007-12-17 01:11:20 +000088 llvm::Type *PFTy = llvm::PointerType::getUnqual(Ty);
Chris Lattner9cd4fe42007-12-02 07:09:19 +000089 if (PFTy == F->getType()) return Entry = F;
Chris Lattnerfafad832007-12-02 06:30:46 +000090
Chris Lattner9cd4fe42007-12-02 07:09:19 +000091 // If this isn't a definition, just return it casted to the right type.
92 if (!isDefinition)
93 return Entry = llvm::ConstantExpr::getBitCast(F, PFTy);
94
95 // Otherwise, we have a definition after a prototype with the wrong type.
96 // F is the Function* for the one with the wrong type, we must make a new
97 // Function* and update everything that used F (a declaration) with the new
98 // Function* (which will be a definition).
99 //
100 // This happens if there is a prototype for a function (e.g. "int f()") and
101 // then a definition of a different type (e.g. "int f(int x)"). Start by
102 // making a new function of the correct type, RAUW, then steal the name.
103 llvm::Function *NewFn = new llvm::Function(FTy,
104 llvm::Function::ExternalLinkage,
105 "", &getModule());
106 NewFn->takeName(F);
107
108 // Replace uses of F with the Function we will endow with a body.
109 llvm::Constant *NewPtrForOldDecl =
110 llvm::ConstantExpr::getBitCast(NewFn, F->getType());
111 F->replaceAllUsesWith(NewPtrForOldDecl);
112
113 // FIXME: Update the globaldeclmap for the previous decl of this name. We
114 // really want a way to walk all of these, but we don't have it yet. This
115 // is incredibly slow!
116 ReplaceMapValuesWith(F, NewPtrForOldDecl);
117
118 // Ok, delete the old function now, which is dead.
119 assert(F->isDeclaration() && "Shouldn't replace non-declaration");
120 F->eraseFromParent();
121
122 // Return the new function which has the right type.
123 return Entry = NewFn;
124}
125
Chris Lattnerc4b23a52008-02-05 06:37:34 +0000126static bool IsZeroElementArray(const llvm::Type *Ty) {
127 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(Ty))
128 return ATy->getNumElements() == 0;
129 return false;
130}
131
Chris Lattner2b9d2ca2007-12-18 08:16:44 +0000132llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
133 bool isDefinition) {
134 assert(D->hasGlobalStorage() && "Not a global variable");
135
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000136 // See if it is already in the map.
137 llvm::Constant *&Entry = GlobalDeclMap[D];
138 if (Entry) return Entry;
139
Christopher Lambebb97e92008-02-04 02:31:56 +0000140 QualType ASTTy = D->getType();
141 const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy);
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000142
143 // Check to see if the global already exists.
Chris Lattner49573782008-02-02 04:43:11 +0000144 llvm::GlobalVariable *GV = getModule().getGlobalVariable(D->getName(), true);
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000145
146 // If it doesn't already exist, just create and return an entry.
147 if (GV == 0) {
148 return Entry = new llvm::GlobalVariable(Ty, false,
149 llvm::GlobalValue::ExternalLinkage,
Christopher Lambebb97e92008-02-04 02:31:56 +0000150 0, D->getName(), &getModule(), 0,
151 ASTTy.getAddressSpace());
Chris Lattnerfafad832007-12-02 06:30:46 +0000152 }
153
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000154 // If the pointer type matches, just return it.
Christopher Lambddc23f32007-12-17 01:11:20 +0000155 llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000156 if (PTy == GV->getType()) return Entry = GV;
157
158 // If this isn't a definition, just return it casted to the right type.
159 if (!isDefinition)
160 return Entry = llvm::ConstantExpr::getBitCast(GV, PTy);
161
162
163 // Otherwise, we have a definition after a prototype with the wrong type.
164 // GV is the GlobalVariable* for the one with the wrong type, we must make a
165 /// new GlobalVariable* and update everything that used GV (a declaration)
166 // with the new GlobalVariable* (which will be a definition).
167 //
168 // This happens if there is a prototype for a global (e.g. "extern int x[];")
169 // and then a definition of a different type (e.g. "int x[10];"). Start by
170 // making a new global of the correct type, RAUW, then steal the name.
171 llvm::GlobalVariable *NewGV =
172 new llvm::GlobalVariable(Ty, false, llvm::GlobalValue::ExternalLinkage,
Christopher Lambebb97e92008-02-04 02:31:56 +0000173 0, D->getName(), &getModule(), 0,
174 ASTTy.getAddressSpace());
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000175 NewGV->takeName(GV);
176
177 // Replace uses of GV with the globalvalue we will endow with a body.
178 llvm::Constant *NewPtrForOldDecl =
179 llvm::ConstantExpr::getBitCast(NewGV, GV->getType());
180 GV->replaceAllUsesWith(NewPtrForOldDecl);
181
182 // FIXME: Update the globaldeclmap for the previous decl of this name. We
183 // really want a way to walk all of these, but we don't have it yet. This
184 // is incredibly slow!
185 ReplaceMapValuesWith(GV, NewPtrForOldDecl);
186
Chris Lattnerc4b23a52008-02-05 06:37:34 +0000187 // Verify that GV was a declaration or something like x[] which turns into
188 // [0 x type].
189 assert((GV->isDeclaration() ||
190 IsZeroElementArray(GV->getType()->getElementType())) &&
191 "Shouldn't replace non-declaration");
192
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000193 // Ok, delete the old global now, which is dead.
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000194 GV->eraseFromParent();
195
196 // Return the new global which has the right type.
197 return Entry = NewGV;
Reid Spencer5f016e22007-07-11 17:01:13 +0000198}
199
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000200
Chris Lattner88a69ad2007-07-13 05:13:43 +0000201void CodeGenModule::EmitFunction(const FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 // If this is not a prototype, emit the body.
203 if (FD->getBody())
204 CodeGenFunction(*this).GenerateCode(FD);
205}
206
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000207llvm::Constant *CodeGenModule::EmitGlobalInit(const Expr *Expr) {
208 return EmitConstantExpr(Expr);
Devang Patel9e32d4b2007-10-30 21:27:20 +0000209}
210
Chris Lattner88a69ad2007-07-13 05:13:43 +0000211void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) {
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000212 // If this is just a forward declaration of the variable, don't emit it now,
213 // allow it to be emitted lazily on its first use.
Chris Lattner88a69ad2007-07-13 05:13:43 +0000214 if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0)
215 return;
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000216
217 // Get the global, forcing it to be a direct reference.
218 llvm::GlobalVariable *GV =
Chris Lattner2b9d2ca2007-12-18 08:16:44 +0000219 cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, true));
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000220
221 // Convert the initializer, or use zero if appropriate.
Chris Lattner8f32f712007-07-14 00:23:28 +0000222 llvm::Constant *Init = 0;
223 if (D->getInit() == 0) {
Chris Lattner88a69ad2007-07-13 05:13:43 +0000224 Init = llvm::Constant::getNullValue(GV->getType()->getElementType());
Chris Lattner8f32f712007-07-14 00:23:28 +0000225 } else if (D->getType()->isIntegerType()) {
Hartmut Kaiser7b660002007-10-17 15:00:17 +0000226 llvm::APSInt Value(static_cast<uint32_t>(
Chris Lattner47f7dbf2007-09-04 02:34:27 +0000227 getContext().getTypeSize(D->getInit()->getType(), SourceLocation())));
Chris Lattner590b6642007-07-15 23:26:56 +0000228 if (D->getInit()->isIntegerConstantExpr(Value, Context))
Chris Lattner8f32f712007-07-14 00:23:28 +0000229 Init = llvm::ConstantInt::get(Value);
230 }
Devang Patel8e53e722007-10-26 16:31:40 +0000231
Devang Patel9e32d4b2007-10-30 21:27:20 +0000232 if (!Init)
Oliver Hunt28247232007-12-02 00:11:25 +0000233 Init = EmitGlobalInit(D->getInit());
Devang Patel8e53e722007-10-26 16:31:40 +0000234
Chris Lattnerf89dfb22007-12-10 00:05:55 +0000235 assert(GV->getType()->getElementType() == Init->getType() &&
236 "Initializer codegen type mismatch!");
Chris Lattner88a69ad2007-07-13 05:13:43 +0000237 GV->setInitializer(Init);
238
239 // Set the llvm linkage type as appropriate.
240 // FIXME: This isn't right. This should handle common linkage and other
241 // stuff.
242 switch (D->getStorageClass()) {
243 case VarDecl::Auto:
244 case VarDecl::Register:
245 assert(0 && "Can't have auto or register globals");
246 case VarDecl::None:
247 case VarDecl::Extern:
Steve Naroffd6326c62008-01-25 22:14:40 +0000248 case VarDecl::PrivateExtern:
Chris Lattner88a69ad2007-07-13 05:13:43 +0000249 // todo: common
250 break;
251 case VarDecl::Static:
252 GV->setLinkage(llvm::GlobalVariable::InternalLinkage);
253 break;
254 }
255}
Reid Spencer5f016e22007-07-11 17:01:13 +0000256
Chris Lattner32b266c2007-07-14 00:16:50 +0000257/// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified
258/// declarator chain.
259void CodeGenModule::EmitGlobalVarDeclarator(const FileVarDecl *D) {
260 for (; D; D = cast_or_null<FileVarDecl>(D->getNextDeclarator()))
261 EmitGlobalVar(D);
262}
Reid Spencer5f016e22007-07-11 17:01:13 +0000263
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000264void CodeGenModule::EmitType(const TypeDecl *D) {
265 if (isa<TypedefDecl>(D)) {
266 // TODO: Emit debug info.
267 return;
268 }
269
270 assert(!isa<ObjCInterfaceDecl>(D) && "FIXME: ADD OBJC SUPPORT");
271
272 // This must be a tag decl.
273 const TagDecl *TD = cast<TagDecl>(D);
274
275 // Get the LLVM type for this TagDecl. If it is non-opaque or if this decl
276 // is still a forward declaration, just return.
277 QualType NewTy = Context.getTagDeclType(const_cast<TagDecl *>(TD));
278 const llvm::Type *T = Types.ConvertType(NewTy);
279 if (isa<llvm::OpaqueType>(T) && TD->isDefinition())
280 // Make sure that this type is translated.
281 Types.ForceTypeCompilation(NewTy);
282}
283
284
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000285/// getBuiltinLibFunction
286llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) {
Chris Lattner1426fec2007-12-13 00:38:03 +0000287 if (BuiltinID > BuiltinFunctions.size())
288 BuiltinFunctions.resize(BuiltinID);
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000289
Chris Lattner1426fec2007-12-13 00:38:03 +0000290 // Cache looked up functions. Since builtin id #0 is invalid we don't reserve
291 // a slot for it.
292 assert(BuiltinID && "Invalid Builtin ID");
293 llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1];
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000294 if (FunctionSlot)
295 return FunctionSlot;
296
297 assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn");
298
299 // Get the name, skip over the __builtin_ prefix.
300 const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10;
301
302 // Get the type for the builtin.
303 QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context);
304 const llvm::FunctionType *Ty =
305 cast<llvm::FunctionType>(getTypes().ConvertType(Type));
306
307 // FIXME: This has a serious problem with code like this:
308 // void abs() {}
309 // ... __builtin_abs(x);
310 // The two versions of abs will collide. The fix is for the builtin to win,
311 // and for the existing one to be turned into a constantexpr cast of the
312 // builtin. In the case where the existing one is a static function, it
313 // should just be renamed.
Chris Lattnerc5e940f2007-08-31 04:44:06 +0000314 if (llvm::Function *Existing = getModule().getFunction(Name)) {
315 if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage())
316 return FunctionSlot = Existing;
317 assert(Existing == 0 && "FIXME: Name collision");
318 }
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000319
320 // FIXME: param attributes for sext/zext etc.
321 return FunctionSlot = new llvm::Function(Ty, llvm::Function::ExternalLinkage,
322 Name, &getModule());
323}
324
Chris Lattner7acda7c2007-12-18 00:25:38 +0000325llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys,
326 unsigned NumTys) {
327 return llvm::Intrinsic::getDeclaration(&getModule(),
328 (llvm::Intrinsic::ID)IID, Tys, NumTys);
329}
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000330
Reid Spencer5f016e22007-07-11 17:01:13 +0000331llvm::Function *CodeGenModule::getMemCpyFn() {
332 if (MemCpyFn) return MemCpyFn;
333 llvm::Intrinsic::ID IID;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000334 uint64_t Size; unsigned Align;
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000335 Context.Target.getPointerInfo(Size, Align, FullSourceLoc());
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000336 switch (Size) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 default: assert(0 && "Unknown ptr width");
338 case 32: IID = llvm::Intrinsic::memcpy_i32; break;
339 case 64: IID = llvm::Intrinsic::memcpy_i64; break;
340 }
Chris Lattner7acda7c2007-12-18 00:25:38 +0000341 return MemCpyFn = getIntrinsic(IID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000342}
Anders Carlssonc9e20912007-08-21 00:21:21 +0000343
Chris Lattner7acda7c2007-12-18 00:25:38 +0000344
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000345llvm::Constant *CodeGenModule::
346GetAddrOfConstantCFString(const std::string &str) {
Anders Carlssonc9e20912007-08-21 00:21:21 +0000347 llvm::StringMapEntry<llvm::Constant *> &Entry =
348 CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
349
350 if (Entry.getValue())
351 return Entry.getValue();
352
353 std::vector<llvm::Constant*> Fields;
354
355 if (!CFConstantStringClassRef) {
356 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
357 Ty = llvm::ArrayType::get(Ty, 0);
358
359 CFConstantStringClassRef =
360 new llvm::GlobalVariable(Ty, false,
361 llvm::GlobalVariable::ExternalLinkage, 0,
362 "__CFConstantStringClassReference",
363 &getModule());
364 }
365
366 // Class pointer.
367 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
368 llvm::Constant *Zeros[] = { Zero, Zero };
369 llvm::Constant *C =
370 llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2);
371 Fields.push_back(C);
372
373 // Flags.
374 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
375 Fields.push_back(llvm::ConstantInt::get(Ty, 1992));
376
377 // String pointer.
378 C = llvm::ConstantArray::get(str);
379 C = new llvm::GlobalVariable(C->getType(), true,
380 llvm::GlobalValue::InternalLinkage,
381 C, ".str", &getModule());
382
383 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
384 Fields.push_back(C);
385
386 // String length.
387 Ty = getTypes().ConvertType(getContext().LongTy);
388 Fields.push_back(llvm::ConstantInt::get(Ty, str.length()));
389
390 // The struct.
391 Ty = getTypes().ConvertType(getContext().getCFConstantStringType());
392 C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields);
Anders Carlsson0c678292007-11-01 00:41:52 +0000393 llvm::GlobalVariable *GV =
394 new llvm::GlobalVariable(C->getType(), true,
395 llvm::GlobalVariable::InternalLinkage,
396 C, "", &getModule());
397 GV->setSection("__DATA,__cfstring");
398 Entry.setValue(GV);
399 return GV;
Anders Carlssonc9e20912007-08-21 00:21:21 +0000400}
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000401
402/// GenerateWritableString -- Creates storage for a string literal
403static llvm::Constant *GenerateStringLiteral(const std::string &str,
404 bool constant,
Chris Lattner2c8569d2007-12-02 07:19:18 +0000405 CodeGenModule &CGM) {
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000406 // Create Constant for this string literal
407 llvm::Constant *C=llvm::ConstantArray::get(str);
408
409 // Create a global variable for this string
410 C = new llvm::GlobalVariable(C->getType(), constant,
411 llvm::GlobalValue::InternalLinkage,
Chris Lattner2c8569d2007-12-02 07:19:18 +0000412 C, ".str", &CGM.getModule());
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000413 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
414 llvm::Constant *Zeros[] = { Zero, Zero };
415 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
416 return C;
417}
418
419/// CodeGenModule::GetAddrOfConstantString -- returns a pointer to the first
420/// element of a character array containing the literal.
421llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str) {
422 // Don't share any string literals if writable-strings is turned on.
423 if (Features.WritableStrings)
424 return GenerateStringLiteral(str, false, *this);
425
426 llvm::StringMapEntry<llvm::Constant *> &Entry =
427 ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
428
429 if (Entry.getValue())
430 return Entry.getValue();
431
432 // Create a global variable for this.
433 llvm::Constant *C = GenerateStringLiteral(str, true, *this);
434 Entry.setValue(C);
435 return C;
436}