CodeGenModule.cpp revision 8fabd78f1976243cb223fb3e969c6f317d1ae44d
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This coordinates the per-module state used while generating code.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenModule.h"
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenFunction.h"
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/Decl.h"
182c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner#include "clang/Basic/Diagnostic.h"
1945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner#include "clang/Basic/LangOptions.h"
208bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman#include "clang/Basic/SourceManager.h"
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
22ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman#include "llvm/CallingConv.h"
238f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner#include "llvm/Constants.h"
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/DerivedTypes.h"
25bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner#include "llvm/Module.h"
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Intrinsics.h"
27a1945fa74d04098d7e22d7c31585342555eca928Chris Lattner#include "llvm/Analysis/Verifier.h"
28ce39faa836d0fe2f3be9ff11865c6433f734b2c6Christopher Lamb#include <algorithm>
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris LattnerCodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO,
34fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner                             llvm::Module &M, const llvm::TargetData &TD,
35fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner                             Diagnostic &diags)
36fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner  : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags),
372b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner    Types(C, M, TD), MemCpyFn(0), MemSetFn(0), CFConstantStringClassRef(0) {
382b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner  //TODO: Make this selectable at runtime
39391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  Runtime = CreateObjCRuntime(M,
40391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner      getTypes().ConvertType(getContext().IntTy),
41391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner      getTypes().ConvertType(getContext().LongTy));
422b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner}
432b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner
442b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris LattnerCodeGenModule::~CodeGenModule() {
45391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction();
46ce5605ecf76d8cde6372138f830bb144d174ced9Chris Lattner  if (ObjCInitFunction)
47391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner    AddGlobalCtor(ObjCInitFunction);
484c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  EmitStatics();
496d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  EmitGlobalCtors();
50532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  EmitAnnotations();
512b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner  delete Runtime;
52a1945fa74d04098d7e22d7c31585342555eca928Chris Lattner
53a1945fa74d04098d7e22d7c31585342555eca928Chris Lattner  // Run the verifier to check that the generated code is consistent.
54a1945fa74d04098d7e22d7c31585342555eca928Chris Lattner  assert(!verifyModule(TheModule));
552b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner}
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
572c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// WarnUnsupported - Print out a warning that codegen doesn't support the
582c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet.
592c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattnervoid CodeGenModule::WarnUnsupported(const Stmt *S, const char *Type) {
602c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner  unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning,
612c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner                                               "cannot codegen this %0 yet");
622c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner  SourceRange Range = S->getSourceRange();
632c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner  std::string Msg = Type;
649c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek  getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID,
657a9d49fd2bfac00e905b361ba76d26ab5b6c3b09Ted Kremenek                    &Msg, 1, &Range, 1);
662c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner}
6758c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner
68c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// WarnUnsupported - Print out a warning that codegen doesn't support the
69c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet.
70c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattnervoid CodeGenModule::WarnUnsupported(const Decl *D, const char *Type) {
71c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning,
72c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner                                               "cannot codegen this %0 yet");
73c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  std::string Msg = Type;
74c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID,
75c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner                    &Msg, 1);
76c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner}
77c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
786d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before
796d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs.
806d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattnervoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor) {
816d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  // TODO: Type coercion of void()* types.
826d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  GlobalCtors.push_back(Ctor);
836d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner}
846d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner
85391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner/// EmitGlobalCtors - Generates the array of contsturctor functions to be
86391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner/// called on module load, if any have been registered with AddGlobalCtor.
876d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattnervoid CodeGenModule::EmitGlobalCtors() {
88391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  if (GlobalCtors.empty()) return;
89ce5605ecf76d8cde6372138f830bb144d174ced9Chris Lattner
906d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  // Get the type of @llvm.global_ctors
916d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  std::vector<const llvm::Type*> CtorFields;
926d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  CtorFields.push_back(llvm::IntegerType::get(32));
936d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  // Constructor function type
946d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  std::vector<const llvm::Type*> VoidArgs;
95ce5605ecf76d8cde6372138f830bb144d174ced9Chris Lattner  llvm::FunctionType* CtorFuncTy =
96ce5605ecf76d8cde6372138f830bb144d174ced9Chris Lattner    llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false);
97ce5605ecf76d8cde6372138f830bb144d174ced9Chris Lattner
986d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  // i32, function type pair
99572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner  const llvm::Type *FPType = llvm::PointerType::getUnqual(CtorFuncTy);
100572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner  llvm::StructType* CtorStructTy =
101572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner  llvm::StructType::get(llvm::Type::Int32Ty, FPType, NULL);
1026d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  // Array of fields
103572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner  llvm::ArrayType* GlobalCtorsTy =
104572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner    llvm::ArrayType::get(CtorStructTy, GlobalCtors.size());
1056d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner
1066d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  // Define the global variable
107572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner  llvm::GlobalVariable *GlobalCtorsVal =
108572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner    new llvm::GlobalVariable(GlobalCtorsTy, false,
109572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner                             llvm::GlobalValue::AppendingLinkage,
110572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner                             (llvm::Constant*)0, "llvm.global_ctors",
111572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner                             &TheModule);
1126d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner
1136d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  // Populate the array
1146d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  std::vector<llvm::Constant*> CtorValues;
115572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner  llvm::Constant *MagicNumber =
116572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner    llvm::ConstantInt::get(llvm::Type::Int32Ty, 65535, false);
117572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner  std::vector<llvm::Constant*> StructValues;
1186d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  for (std::vector<llvm::Constant*>::iterator I = GlobalCtors.begin(),
119572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner       E = GlobalCtors.end(); I != E; ++I) {
120572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner    StructValues.clear();
1216d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner    StructValues.push_back(MagicNumber);
1226d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner    StructValues.push_back(*I);
1236d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner
124572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner    CtorValues.push_back(llvm::ConstantStruct::get(CtorStructTy, StructValues));
1256d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  }
126572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner
127572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner  GlobalCtorsVal->setInitializer(llvm::ConstantArray::get(GlobalCtorsTy,
128572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner                                                          CtorValues));
1296d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner}
1306d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner
131391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
132391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
133532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() {
134532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  if (Annotations.empty())
135532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman    return;
136532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman
137532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  // Create a new global variable for the ConstantStruct in the Module.
138532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  llvm::Constant *Array =
139532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(),
140532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                                                Annotations.size()),
141532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                           Annotations);
142532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  llvm::GlobalValue *gv =
143532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  new llvm::GlobalVariable(Array->getType(), false,
144532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                           llvm::GlobalValue::AppendingLinkage, Array,
145532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman                           "llvm.global.annotations", &TheModule);
146532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  gv->setSection("llvm.metadata");
147532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman}
148532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman
14958c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner/// ReplaceMapValuesWith - This is a really slow and bad function that
15058c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner/// searches for any entries in GlobalDeclMap that point to OldVal, changing
15158c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner/// them to point to NewVal.  This is badbadbad, FIXME!
15258c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattnervoid CodeGenModule::ReplaceMapValuesWith(llvm::Constant *OldVal,
15358c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner                                         llvm::Constant *NewVal) {
15458c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner  for (llvm::DenseMap<const Decl*, llvm::Constant*>::iterator
15558c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner       I = GlobalDeclMap.begin(), E = GlobalDeclMap.end(); I != E; ++I)
15658c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner    if (I->second == OldVal) I->second = NewVal;
15758c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner}
15858c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner
15958c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner
1609cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattnerllvm::Constant *CodeGenModule::GetAddrOfFunctionDecl(const FunctionDecl *D,
1619cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner                                                     bool isDefinition) {
1629cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // See if it is already in the map.  If so, just return it.
1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Constant *&Entry = GlobalDeclMap[D];
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Entry) return Entry;
1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1669cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  const llvm::Type *Ty = getTypes().ConvertType(D->getType());
1679cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
1689cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // Check to see if the function already exists.
1699cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  llvm::Function *F = getModule().getFunction(D->getName());
1709cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty);
1719cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
1729cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // If it doesn't already exist, just create and return an entry.
1739cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  if (F == 0) {
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FIXME: param attributes for sext/zext etc.
1754c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    F = llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
1764c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman                               D->getName(), &getModule());
177ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman
178ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman    // Set the appropriate calling convention for the Function.
179ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman    if (D->getAttr<FastCallAttr>())
180ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman      F->setCallingConv(llvm::CallingConv::Fast);
181ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman    return Entry = F;
1825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1849cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // If the pointer type matches, just return it.
185ddc23f3e6fdc4f83dd46ef7e20394cfbd6063ff9Christopher Lamb  llvm::Type *PFTy = llvm::PointerType::getUnqual(Ty);
1869cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  if (PFTy == F->getType()) return Entry = F;
187fafad83da6eff6bf090f6eb2dc1019ace7473f38Chris Lattner
1889cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // If this isn't a definition, just return it casted to the right type.
1899cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  if (!isDefinition)
1909cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner    return Entry = llvm::ConstantExpr::getBitCast(F, PFTy);
1919cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
1929cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // Otherwise, we have a definition after a prototype with the wrong type.
1939cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // F is the Function* for the one with the wrong type, we must make a new
1949cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // Function* and update everything that used F (a declaration) with the new
1959cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // Function* (which will be a definition).
1969cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  //
1979cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // This happens if there is a prototype for a function (e.g. "int f()") and
1989cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // then a definition of a different type (e.g. "int f(int x)").  Start by
1999cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // making a new function of the correct type, RAUW, then steal the name.
200984d0b414bc76d3530b9bc55a5a55834ba76c607Gabor Greif  llvm::Function *NewFn = llvm::Function::Create(FTy,
2019cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner                                             llvm::Function::ExternalLinkage,
2029cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner                                             "", &getModule());
2039cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  NewFn->takeName(F);
2049cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
2059cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // Replace uses of F with the Function we will endow with a body.
2069cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  llvm::Constant *NewPtrForOldDecl =
2079cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner    llvm::ConstantExpr::getBitCast(NewFn, F->getType());
2089cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  F->replaceAllUsesWith(NewPtrForOldDecl);
2099cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
2109cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // FIXME: Update the globaldeclmap for the previous decl of this name.  We
2119cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // really want a way to walk all of these, but we don't have it yet.  This
2129cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // is incredibly slow!
2139cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  ReplaceMapValuesWith(F, NewPtrForOldDecl);
2149cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
2159cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // Ok, delete the old function now, which is dead.
2169cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  assert(F->isDeclaration() && "Shouldn't replace non-declaration");
2179cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  F->eraseFromParent();
2189cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
2199cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // Return the new function which has the right type.
2209cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  return Entry = NewFn;
2219cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner}
2229cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
223c4b23a530b04ee77b56772a70191d32119eadc3eChris Lattnerstatic bool IsZeroElementArray(const llvm::Type *Ty) {
224c4b23a530b04ee77b56772a70191d32119eadc3eChris Lattner  if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(Ty))
225c4b23a530b04ee77b56772a70191d32119eadc3eChris Lattner    return ATy->getNumElements() == 0;
226c4b23a530b04ee77b56772a70191d32119eadc3eChris Lattner  return false;
227c4b23a530b04ee77b56772a70191d32119eadc3eChris Lattner}
228c4b23a530b04ee77b56772a70191d32119eadc3eChris Lattner
2292b9d2ca4ce53fffbe8a77c7db2fe4df5ccfb9fd4Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
2302b9d2ca4ce53fffbe8a77c7db2fe4df5ccfb9fd4Chris Lattner                                                  bool isDefinition) {
2312b9d2ca4ce53fffbe8a77c7db2fe4df5ccfb9fd4Chris Lattner  assert(D->hasGlobalStorage() && "Not a global variable");
2322b9d2ca4ce53fffbe8a77c7db2fe4df5ccfb9fd4Chris Lattner
2339cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // See if it is already in the map.
2349cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  llvm::Constant *&Entry = GlobalDeclMap[D];
2359cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  if (Entry) return Entry;
2369cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
237ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  QualType ASTTy = D->getType();
238ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy);
2399cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
2409cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // Check to see if the global already exists.
241495737830441e3e01ed2699eb9e38c8f15116cb5Chris Lattner  llvm::GlobalVariable *GV = getModule().getGlobalVariable(D->getName(), true);
2429cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
2439cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // If it doesn't already exist, just create and return an entry.
2449cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  if (GV == 0) {
2459cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner    return Entry = new llvm::GlobalVariable(Ty, false,
2469cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner                                            llvm::GlobalValue::ExternalLinkage,
247ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb                                            0, D->getName(), &getModule(), 0,
248ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb                                            ASTTy.getAddressSpace());
249fafad83da6eff6bf090f6eb2dc1019ace7473f38Chris Lattner  }
250fafad83da6eff6bf090f6eb2dc1019ace7473f38Chris Lattner
2519cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // If the pointer type matches, just return it.
252ddc23f3e6fdc4f83dd46ef7e20394cfbd6063ff9Christopher Lamb  llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
2539cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  if (PTy == GV->getType()) return Entry = GV;
2549cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
2559cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // If this isn't a definition, just return it casted to the right type.
2569cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  if (!isDefinition)
2579cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner    return Entry = llvm::ConstantExpr::getBitCast(GV, PTy);
2589cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
2599cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
2609cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // Otherwise, we have a definition after a prototype with the wrong type.
2619cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // GV is the GlobalVariable* for the one with the wrong type, we must make a
2629cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  /// new GlobalVariable* and update everything that used GV (a declaration)
2639cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // with the new GlobalVariable* (which will be a definition).
2649cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  //
2659cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // This happens if there is a prototype for a global (e.g. "extern int x[];")
2669cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // and then a definition of a different type (e.g. "int x[10];").  Start by
2679cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // making a new global of the correct type, RAUW, then steal the name.
2689cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  llvm::GlobalVariable *NewGV =
2699cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner    new llvm::GlobalVariable(Ty, false, llvm::GlobalValue::ExternalLinkage,
270ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb                             0, D->getName(), &getModule(), 0,
271ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb                             ASTTy.getAddressSpace());
2729cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  NewGV->takeName(GV);
2739cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
2749cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // Replace uses of GV with the globalvalue we will endow with a body.
2759cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  llvm::Constant *NewPtrForOldDecl =
2769cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner    llvm::ConstantExpr::getBitCast(NewGV, GV->getType());
2779cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  GV->replaceAllUsesWith(NewPtrForOldDecl);
2789cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
2799cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // FIXME: Update the globaldeclmap for the previous decl of this name.  We
2809cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // really want a way to walk all of these, but we don't have it yet.  This
2819cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // is incredibly slow!
2829cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  ReplaceMapValuesWith(GV, NewPtrForOldDecl);
2839cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
284c4b23a530b04ee77b56772a70191d32119eadc3eChris Lattner  // Verify that GV was a declaration or something like x[] which turns into
285c4b23a530b04ee77b56772a70191d32119eadc3eChris Lattner  // [0 x type].
286c4b23a530b04ee77b56772a70191d32119eadc3eChris Lattner  assert((GV->isDeclaration() ||
287c4b23a530b04ee77b56772a70191d32119eadc3eChris Lattner          IsZeroElementArray(GV->getType()->getElementType())) &&
288c4b23a530b04ee77b56772a70191d32119eadc3eChris Lattner         "Shouldn't replace non-declaration");
289c4b23a530b04ee77b56772a70191d32119eadc3eChris Lattner
2909cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // Ok, delete the old global now, which is dead.
2919cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  GV->eraseFromParent();
2929cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
2939cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // Return the new global which has the right type.
2949cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  return Entry = NewGV;
2955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2979cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
298391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattnervoid CodeGenModule::EmitObjCMethod(const ObjCMethodDecl *OMD) {
299391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // If this is not a prototype, emit the body.
300391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  if (OMD->getBody())
301391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner    CodeGenFunction(*this).GenerateObjCMethod(OMD);
302391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner}
303391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
30488a69ad80e1550e9932666e6efa050a5b1223889Chris Lattnervoid CodeGenModule::EmitFunction(const FunctionDecl *FD) {
3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is not a prototype, emit the body.
3064c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  if (FD->getBody()) {
3074c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    // If the function is a static, defer code generation until later so we can
3084c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    // easily omit unused statics.
3094c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    if (FD->getStorageClass() == FunctionDecl::Static) {
3101a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman      // We need to check the Module here to see if GetAddrOfFunctionDecl() has
3111a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman      // already added this function to the Module because the address of the
3121a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman      // function's prototype was taken.  If this is the case, call
3131a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman      // GetAddrOfFunctionDecl to insert the static FunctionDecl into the used
3141a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman      // GlobalDeclsMap, so that EmitStatics will generate code for it later.
3151a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman      //
3161a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman      // Example:
3171a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman      // static int foo();
3181a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman      // int bar() { return foo(); }
3191a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman      // static int foo() { return 5; }
3201a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman      if (getModule().getFunction(FD->getName()))
3211a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman        GetAddrOfFunctionDecl(FD, true);
3221a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman
3234c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      StaticDecls.push_back(FD);
3244c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      return;
3254c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    }
3265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CodeGenFunction(*this).GenerateCode(FD);
3274c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  }
3284c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman}
3294c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman
3304c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begemanvoid CodeGenModule::EmitStatics() {
3314c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  // Emit code for each used static decl encountered.  Since a previously unused
3324c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  // static decl may become used during the generation of code for a static
3334c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  // function, iterate until no changes are made.
3344c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  bool Changed;
3354c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  do {
3364c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    Changed = false;
3374c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    for (unsigned i = 0, e = StaticDecls.size(); i != e; ++i) {
3384c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      // Check the map of used decls for our static. If not found, continue.
3394c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      const Decl *D = StaticDecls[i];
3401a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman      if (!GlobalDeclMap.count(D))
3414c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman        continue;
3424c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman
3434c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      // If this is a function decl, generate code for the static function if it
3444c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      // has a body.  Otherwise, we must have a var decl for a static global
3454c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      // variable.
3464c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3474c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman        if (FD->getBody())
3484c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman          CodeGenFunction(*this).GenerateCode(FD);
3494c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      } else {
3501a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman        EmitGlobalVarInit(cast<VarDecl>(D));
3514c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      }
3524c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      // Erase the used decl from the list.
3534c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      StaticDecls[i] = StaticDecls.back();
3544c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      StaticDecls.pop_back();
3554c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      --i;
3564c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      --e;
3574c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman
3584c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      // Remember that we made a change.
3594c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      Changed = true;
3604c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    }
3614c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  } while (Changed);
3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3643b1d57b623702865e4158b00cb1d984ba6dd5d50Anders Carlssonllvm::Constant *CodeGenModule::EmitGlobalInit(const Expr *Expr) {
3653b1d57b623702865e4158b00cb1d984ba6dd5d50Anders Carlsson  return EmitConstantExpr(Expr);
3669e32d4b4a7270a9701b7cb454381eeaa4cc42a77Devang Patel}
3679e32d4b4a7270a9701b7cb454381eeaa4cc42a77Devang Patel
3688bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the
3698bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue.  The annotation struct is
3708bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// {i8 *, i8 *, i8 *, i32}.  The first field is a constant expression, the
3718bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// GlobalValue being annotated.  The second filed is thee constant string
3728bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// created from the AnnotateAttr's annotation.  The third field is a constant
3738bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit.  The fourth field is
3748bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration.
3758bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///
3768bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc
3778bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///        appears to.
3788bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///
3798bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begemanllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
3808bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                const AnnotateAttr *AA,
3818bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                unsigned LineNo) {
3828bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Module *M = &getModule();
3838bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
3848bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // get [N x i8] constants for the annotation string, and the filename string
3858bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // which are the 2nd and 3rd elements of the global annotation structure.
3868bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3878bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true);
3888bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(),
3898bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                  true);
3908bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
3918bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // Get the two global values corresponding to the ConstantArrays we just
3928bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // created to hold the bytes of the strings.
3938bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::GlobalValue *annoGV =
3948bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  new llvm::GlobalVariable(anno->getType(), false,
3958bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                           llvm::GlobalValue::InternalLinkage, anno,
3968bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                           GV->getName() + ".str", M);
3978bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // translation unit name string, emitted into the llvm.metadata section.
3988bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::GlobalValue *unitGV =
3998bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  new llvm::GlobalVariable(unit->getType(), false,
4008bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                           llvm::GlobalValue::InternalLinkage, unit, ".str", M);
4018bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
4028bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  // Create the ConstantStruct that is the global annotion.
4038bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  llvm::Constant *Fields[4] = {
4048bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantExpr::getBitCast(GV, SBP),
4058bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantExpr::getBitCast(annoGV, SBP),
4068bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantExpr::getBitCast(unitGV, SBP),
4078bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo)
4088bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  };
4098bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  return llvm::ConstantStruct::get(Fields, 4, false);
4108bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman}
4118bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
412248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroffvoid CodeGenModule::EmitGlobalVar(const VarDecl *D) {
4134c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  // If the VarDecl is a static, defer code generation until later so we can
4144c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  // easily omit unused statics.
4154c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  if (D->getStorageClass() == VarDecl::Static) {
4164c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    StaticDecls.push_back(D);
4174c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    return;
4184c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  }
4194c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman
4209cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // If this is just a forward declaration of the variable, don't emit it now,
4219cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // allow it to be emitted lazily on its first use.
42288a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner  if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0)
42388a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner    return;
4249cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
4254c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  EmitGlobalVarInit(D);
4264c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman}
4274c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman
4284c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begemanvoid CodeGenModule::EmitGlobalVarInit(const VarDecl *D) {
4299cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // Get the global, forcing it to be a direct reference.
4309cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  llvm::GlobalVariable *GV =
4312b9d2ca4ce53fffbe8a77c7db2fe4df5ccfb9fd4Chris Lattner    cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, true));
4329cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner
4339cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattner  // Convert the initializer, or use zero if appropriate.
4348f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner  llvm::Constant *Init = 0;
4358f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner  if (D->getInit() == 0) {
43688a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner    Init = llvm::Constant::getNullValue(GV->getType()->getElementType());
4378f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner  } else if (D->getType()->isIntegerType()) {
4387b66000bdfc2684351fb03208e672f23012ed569Hartmut Kaiser    llvm::APSInt Value(static_cast<uint32_t>(
43998be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getContext().getTypeSize(D->getInit()->getType())));
440590b6646ef747d2f7b42e5f40487ff07642d7b6fChris Lattner    if (D->getInit()->isIntegerConstantExpr(Value, Context))
4418f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner      Init = llvm::ConstantInt::get(Value);
4428f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner  }
4438e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel
4449e32d4b4a7270a9701b7cb454381eeaa4cc42a77Devang Patel  if (!Init)
4452824723d6d181d2dfa56e62caabd68b0b18f0b9dOliver Hunt    Init = EmitGlobalInit(D->getInit());
4469e32d4b4a7270a9701b7cb454381eeaa4cc42a77Devang Patel
4478bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) {
4488bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    SourceManager &SM = Context.getSourceManager();
4498bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    AddAnnotation(EmitAnnotateAttr(GV, AA,
4508bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                   SM.getLogicalLineNumber(D->getLocation())));
4518bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  }
4528bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
453f89dfb292478b0921c68ef7177ceb738bee1e11bChris Lattner  assert(GV->getType()->getElementType() == Init->getType() &&
454f89dfb292478b0921c68ef7177ceb738bee1e11bChris Lattner         "Initializer codegen type mismatch!");
45588a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner  GV->setInitializer(Init);
456ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner
457ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner  if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>())
458ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    GV->setVisibility(attr->getVisibility());
459ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner  // FIXME: else handle -fvisibility
46088a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner
46188a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner  // Set the llvm linkage type as appropriate.
4628fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner  if (D->getStorageClass() == VarDecl::Static)
4638fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner    GV->setLinkage(llvm::Function::InternalLinkage);
4648fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner  else if (D->getAttr<DLLImportAttr>())
465ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    GV->setLinkage(llvm::Function::DLLImportLinkage);
466ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner  else if (D->getAttr<DLLExportAttr>())
467ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    GV->setLinkage(llvm::Function::DLLExportLinkage);
4688fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner  else if (D->getAttr<WeakAttr>())
469ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    GV->setLinkage(llvm::GlobalVariable::WeakLinkage);
4708fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner  else {
471ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    // FIXME: This isn't right.  This should handle common linkage and other
472ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    // stuff.
473ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    switch (D->getStorageClass()) {
4748fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner    case VarDecl::Static: assert(0 && "This case handled above");
475ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    case VarDecl::Auto:
476ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    case VarDecl::Register:
477ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner      assert(0 && "Can't have auto or register globals");
478ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    case VarDecl::None:
479ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner      if (!D->getInit())
480ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner        GV->setLinkage(llvm::GlobalVariable::WeakLinkage);
481ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner      break;
482ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    case VarDecl::Extern:
483ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    case VarDecl::PrivateExtern:
484ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner      // todo: common
485ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner      break;
486ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner    }
48788a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner  }
48888a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner}
4895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
49032b266cff60737fedff00e8e721f3474533835efChris Lattner/// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified
49132b266cff60737fedff00e8e721f3474533835efChris Lattner/// declarator chain.
492248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroffvoid CodeGenModule::EmitGlobalVarDeclarator(const VarDecl *D) {
493248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff  for (; D; D = cast_or_null<VarDecl>(D->getNextDeclarator()))
494248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff    if (D->isFileVarDecl())
495248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff      EmitGlobalVar(D);
49632b266cff60737fedff00e8e721f3474533835efChris Lattner}
4975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
498c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
499c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner  // Make sure that this type is translated.
500c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner  Types.UpdateCompletedType(TD);
501d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner}
502d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner
503d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner
504bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner/// getBuiltinLibFunction
505bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) {
5061426fecdf90dd1986751b9940422e675880ff671Chris Lattner  if (BuiltinID > BuiltinFunctions.size())
5071426fecdf90dd1986751b9940422e675880ff671Chris Lattner    BuiltinFunctions.resize(BuiltinID);
508bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
5091426fecdf90dd1986751b9940422e675880ff671Chris Lattner  // Cache looked up functions.  Since builtin id #0 is invalid we don't reserve
5101426fecdf90dd1986751b9940422e675880ff671Chris Lattner  // a slot for it.
5111426fecdf90dd1986751b9940422e675880ff671Chris Lattner  assert(BuiltinID && "Invalid Builtin ID");
5121426fecdf90dd1986751b9940422e675880ff671Chris Lattner  llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1];
513bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  if (FunctionSlot)
514bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner    return FunctionSlot;
515bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
516bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn");
517bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
518bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // Get the name, skip over the __builtin_ prefix.
519bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10;
520bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
521bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // Get the type for the builtin.
522bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context);
523bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  const llvm::FunctionType *Ty =
524bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner    cast<llvm::FunctionType>(getTypes().ConvertType(Type));
525bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
526bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // FIXME: This has a serious problem with code like this:
527bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  //  void abs() {}
528bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  //    ... __builtin_abs(x);
529bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // The two versions of abs will collide.  The fix is for the builtin to win,
530bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // and for the existing one to be turned into a constantexpr cast of the
531bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // builtin.  In the case where the existing one is a static function, it
532bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // should just be renamed.
533c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner  if (llvm::Function *Existing = getModule().getFunction(Name)) {
534c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner    if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage())
535c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner      return FunctionSlot = Existing;
536c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner    assert(Existing == 0 && "FIXME: Name collision");
537c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner  }
538bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
539bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // FIXME: param attributes for sext/zext etc.
5404c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman  return FunctionSlot =
5414c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman    llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name,
5424c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman                           &getModule());
543bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner}
544bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
5457acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys,
5467acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner                                            unsigned NumTys) {
5477acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner  return llvm::Intrinsic::getDeclaration(&getModule(),
5487acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner                                         (llvm::Intrinsic::ID)IID, Tys, NumTys);
5497acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner}
550bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner
5515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::Function *CodeGenModule::getMemCpyFn() {
5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (MemCpyFn) return MemCpyFn;
5535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Intrinsic::ID IID;
554f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  switch (Context.Target.getPointerWidth(0)) {
5555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: assert(0 && "Unknown ptr width");
5565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 32: IID = llvm::Intrinsic::memcpy_i32; break;
5575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 64: IID = llvm::Intrinsic::memcpy_i64; break;
5585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5597acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner  return MemCpyFn = getIntrinsic(IID);
5605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
561c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
56241ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venanciollvm::Function *CodeGenModule::getMemSetFn() {
56341ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio  if (MemSetFn) return MemSetFn;
56441ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio  llvm::Intrinsic::ID IID;
565f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  switch (Context.Target.getPointerWidth(0)) {
56641ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio  default: assert(0 && "Unknown ptr width");
56741ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio  case 32: IID = llvm::Intrinsic::memset_i32; break;
56841ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio  case 64: IID = llvm::Intrinsic::memset_i64; break;
56941ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio  }
57041ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio  return MemSetFn = getIntrinsic(IID);
57141ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio}
5727acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner
573bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Constant *CodeGenModule::
574bef20ac367a09555b30d6eb3847a81ec164caf88Chris LattnerGetAddrOfConstantCFString(const std::string &str) {
575c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  llvm::StringMapEntry<llvm::Constant *> &Entry =
576c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
577c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
578c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  if (Entry.getValue())
579c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    return Entry.getValue();
580c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
581c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  std::vector<llvm::Constant*> Fields;
582c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
583c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  if (!CFConstantStringClassRef) {
584c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
585c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    Ty = llvm::ArrayType::get(Ty, 0);
586c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
587c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    CFConstantStringClassRef =
588c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson      new llvm::GlobalVariable(Ty, false,
589c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               llvm::GlobalVariable::ExternalLinkage, 0,
590c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               "__CFConstantStringClassReference",
591c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               &getModule());
592c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  }
593c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
594c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // Class pointer.
595c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
596c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  llvm::Constant *Zeros[] = { Zero, Zero };
597c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  llvm::Constant *C =
598c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2);
599c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  Fields.push_back(C);
600c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
601c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // Flags.
602c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
603c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  Fields.push_back(llvm::ConstantInt::get(Ty, 1992));
604c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
605c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // String pointer.
606c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  C = llvm::ConstantArray::get(str);
607c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  C = new llvm::GlobalVariable(C->getType(), true,
608c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               llvm::GlobalValue::InternalLinkage,
609c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                               C, ".str", &getModule());
610c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
611c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
612c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  Fields.push_back(C);
613c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
614c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // String length.
615c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  Ty = getTypes().ConvertType(getContext().LongTy);
616c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  Fields.push_back(llvm::ConstantInt::get(Ty, str.length()));
617c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson
618c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // The struct.
619c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  Ty = getTypes().ConvertType(getContext().getCFConstantStringType());
620c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields);
6210c67829763b98bc670062b553897a851fab17401Anders Carlsson  llvm::GlobalVariable *GV =
6220c67829763b98bc670062b553897a851fab17401Anders Carlsson    new llvm::GlobalVariable(C->getType(), true,
6230c67829763b98bc670062b553897a851fab17401Anders Carlsson                             llvm::GlobalVariable::InternalLinkage,
6240c67829763b98bc670062b553897a851fab17401Anders Carlsson                             C, "", &getModule());
6250c67829763b98bc670062b553897a851fab17401Anders Carlsson  GV->setSection("__DATA,__cfstring");
6260c67829763b98bc670062b553897a851fab17401Anders Carlsson  Entry.setValue(GV);
6270c67829763b98bc670062b553897a851fab17401Anders Carlsson  return GV;
628c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson}
62945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
630a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal.
63145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattnerstatic llvm::Constant *GenerateStringLiteral(const std::string &str,
63245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner                                             bool constant,
6332c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner                                             CodeGenModule &CGM) {
63445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  // Create Constant for this string literal
63545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  llvm::Constant *C=llvm::ConstantArray::get(str);
63645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
63745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  // Create a global variable for this string
63845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  C = new llvm::GlobalVariable(C->getType(), constant,
63945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner                               llvm::GlobalValue::InternalLinkage,
6402c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner                               C, ".str", &CGM.getModule());
64145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  return C;
64245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner}
64345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
644a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// CodeGenModule::GetAddrOfConstantString -- returns a pointer to the character
645a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// array containing the literal.  The result is pointer to array type.
64645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str) {
64745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  // Don't share any string literals if writable-strings is turned on.
64845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  if (Features.WritableStrings)
64945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner    return GenerateStringLiteral(str, false, *this);
65045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
65145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  llvm::StringMapEntry<llvm::Constant *> &Entry =
65245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
65345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
65445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  if (Entry.getValue())
65545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner      return Entry.getValue();
65645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
65745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  // Create a global variable for this.
65845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  llvm::Constant *C = GenerateStringLiteral(str, true, *this);
65945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  Entry.setValue(C);
66045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  return C;
66145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner}
662