[cfe-commits] r102189 - in /cfe/trunk: include/clang/AST/ASTContext.h include/clang/Basic/TargetInfo.h include/clang/Frontend/PCHBitCodes.h include/clang/Frontend/PCHReader.h lib/AST/ASTContext.cpp lib/CodeGen/CodeGenModule.cpp lib/CodeGen/CodeGenModule.h lib/Frontend/PCHReader.cpp lib/Frontend/PCHWriter.cpp

Fariborz Jahanian fjahanian at apple.com
Fri Apr 23 10:41:07 PDT 2010


Author: fjahanian
Date: Fri Apr 23 12:41:07 2010
New Revision: 102189

URL: http://llvm.org/viewvc/llvm-project?rev=102189&view=rev
Log:
More work toward implementing
NeXt's -fno-constant-cfstrings - wip.

Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/include/clang/Basic/TargetInfo.h
    cfe/trunk/include/clang/Frontend/PCHBitCodes.h
    cfe/trunk/include/clang/Frontend/PCHReader.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.h
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/lib/Frontend/PCHWriter.cpp

Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=102189&r1=102188&r2=102189&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Fri Apr 23 12:41:07 2010
@@ -165,6 +165,8 @@
   QualType ObjCConstantStringType;
   RecordDecl *CFConstantStringTypeDecl;
 
+  RecordDecl *NSConstantStringTypeDecl;
+
   RecordDecl *ObjCFastEnumerationStateTypeDecl;
 
   /// \brief The type for the C FILE type.
@@ -689,6 +691,19 @@
   // constant CFStrings.
   QualType getCFConstantStringType();
 
+  // getNSConstantStringType - Return the C structure type used to represent
+  // constant NSStrings.
+  QualType getNSConstantStringType();
+  /// Get the structure type used to representation NSStrings, or NULL
+  /// if it hasn't yet been built.
+  QualType getRawNSConstantStringType() {
+    if (NSConstantStringTypeDecl)
+      return getTagDeclType(NSConstantStringTypeDecl);
+    return QualType();
+  }
+  void setNSConstantStringType(QualType T);
+
+
   /// Get the structure type used to representation CFStrings, or NULL
   /// if it hasn't yet been built.
   QualType getRawCFConstantStringType() {

Modified: cfe/trunk/include/clang/Basic/TargetInfo.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/TargetInfo.h?rev=102189&r1=102188&r2=102189&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/TargetInfo.h (original)
+++ cfe/trunk/include/clang/Basic/TargetInfo.h Fri Apr 23 12:41:07 2010
@@ -339,6 +339,12 @@
     return "__DATA,__cfstring";
   }
 
+  /// getNSStringSection - Return the section to use for NSString
+  /// literals, or 0 if no special section is used.
+  virtual const char *getNSStringSection() const {
+    return "__OBJC,__cstring_object,regular,no_dead_strip";
+  }
+
   /// isValidSectionSpecifier - This is an optional hook that targets can
   /// implement to perform semantic checking on attribute((section("foo")))
   /// specifiers.  In this case, "foo" is passed in to be checked.  If the

Modified: cfe/trunk/include/clang/Frontend/PCHBitCodes.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHBitCodes.h?rev=102189&r1=102188&r2=102189&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHBitCodes.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHBitCodes.h Fri Apr 23 12:41:07 2010
@@ -453,7 +453,9 @@
       /// \brief Block extedned descriptor type for Blocks CodeGen
       SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR   = 13,
       /// \brief Objective-C "SEL" redefinition type
-      SPECIAL_TYPE_OBJC_SEL_REDEFINITION       = 14
+      SPECIAL_TYPE_OBJC_SEL_REDEFINITION       = 14,
+      /// \brief NSConstantString type
+      SPECIAL_TYPE_NS_CONSTANT_STRING          = 15
     };
 
     /// \brief Record codes for each kind of declaration.

Modified: cfe/trunk/include/clang/Frontend/PCHReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHReader.h?rev=102189&r1=102188&r2=102189&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHReader.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHReader.h Fri Apr 23 12:41:07 2010
@@ -436,7 +436,7 @@
   std::deque<PendingIdentifierInfo> PendingIdentifierInfos;
 
   /// \brief FIXME: document!
-  llvm::SmallVector<uint64_t, 4> SpecialTypes;
+  llvm::SmallVector<uint64_t, 16> SpecialTypes;
 
   /// \brief Contains declarations and definitions that will be
   /// "interesting" to the ASTConsumer, when we get that AST consumer.

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=102189&r1=102188&r2=102189&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Fri Apr 23 12:41:07 2010
@@ -41,6 +41,7 @@
                        Builtin::Context &builtins,
                        bool FreeMem, unsigned size_reserve) :
   GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
+  NSConstantStringTypeDecl(0),
   ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
   sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
   SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
@@ -2906,6 +2907,45 @@
   CFConstantStringTypeDecl = Rec->getDecl();
 }
 
+// getNSConstantStringType - Return the type used for constant NSStrings.
+QualType ASTContext::getNSConstantStringType() {
+  if (!NSConstantStringTypeDecl) {
+    NSConstantStringTypeDecl =
+    CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
+                     &Idents.get("__builtin_NSString"));
+    NSConstantStringTypeDecl->startDefinition();
+    
+    QualType FieldTypes[3];
+    
+    // const int *isa;
+    FieldTypes[0] = getPointerType(IntTy.withConst());
+    // const char *str;
+    FieldTypes[1] = getPointerType(CharTy.withConst());
+    // unsigned int length;
+    FieldTypes[2] = UnsignedIntTy;
+    
+    // Create fields
+    for (unsigned i = 0; i < 3; ++i) {
+      FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
+                                           SourceLocation(), 0,
+                                           FieldTypes[i], /*TInfo=*/0,
+                                           /*BitWidth=*/0,
+                                           /*Mutable=*/false);
+      NSConstantStringTypeDecl->addDecl(Field);
+    }
+    
+    NSConstantStringTypeDecl->completeDefinition();
+  }
+  
+  return getTagDeclType(NSConstantStringTypeDecl);
+}
+
+void ASTContext::setNSConstantStringType(QualType T) {
+  const RecordType *Rec = T->getAs<RecordType>();
+  assert(Rec && "Invalid NSConstantStringType");
+  NSConstantStringTypeDecl = Rec->getDecl();
+}
+
 QualType ASTContext::getObjCFastEnumerationStateType() {
   if (!ObjCFastEnumerationStateTypeDecl) {
     ObjCFastEnumerationStateTypeDecl =

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=102189&r1=102188&r2=102189&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Fri Apr 23 12:41:07 2010
@@ -50,6 +50,7 @@
     Types(C, M, TD, getTargetCodeGenInfo().getABIInfo()),
     MangleCtx(C, diags), VTables(*this), Runtime(0),
     CFConstantStringClassRef(0),
+    NSConstantStringClassRef(0),
     VMContext(M.getContext()) {
 
   if (!Features.ObjC1)
@@ -1595,8 +1596,81 @@
 
 llvm::Constant *
 CodeGenModule::GetAddrOfConstantNSString(const StringLiteral *Literal) {
-  // FIXME. This is temporary so -fno-constant-cfstrings same as old.
-  return GetAddrOfConstantCFString(Literal);
+  unsigned StringLength = 0;
+  bool isUTF16 = false;
+  llvm::StringMapEntry<llvm::Constant*> &Entry =
+    GetConstantCFStringEntry(CFConstantStringMap, Literal,
+                             getTargetData().isLittleEndian(),
+                             isUTF16, StringLength);
+  
+  if (llvm::Constant *C = Entry.getValue())
+    return C;
+  
+  llvm::Constant *Zero =
+  llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext));
+  llvm::Constant *Zeros[] = { Zero, Zero };
+  
+  // If we don't already have it, get __NSConstantStringClassReference.
+  if (!NSConstantStringClassRef) {
+    const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
+    Ty = llvm::ArrayType::get(Ty, 0);
+    llvm::Constant *GV = CreateRuntimeVariable(Ty,
+                                               "__NSConstantStringClassReference");
+    // Decay array -> ptr
+    NSConstantStringClassRef = 
+      llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
+  }
+  
+  QualType NSTy = getContext().getNSConstantStringType();
+  
+  const llvm::StructType *STy =
+  cast<llvm::StructType>(getTypes().ConvertType(NSTy));
+  
+  std::vector<llvm::Constant*> Fields(3);
+  
+  // Class pointer.
+  Fields[0] = NSConstantStringClassRef;
+  
+  // String pointer.
+  llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str());
+  
+  llvm::GlobalValue::LinkageTypes Linkage;
+  bool isConstant;
+  if (isUTF16) {
+    // FIXME: why do utf strings get "_" labels instead of "L" labels?
+    Linkage = llvm::GlobalValue::InternalLinkage;
+    // Note: -fwritable-strings doesn't make unicode NSStrings writable, but
+    // does make plain ascii ones writable.
+    isConstant = true;
+  } else {
+    Linkage = llvm::GlobalValue::PrivateLinkage;
+    isConstant = !Features.WritableStrings;
+  }
+  
+  llvm::GlobalVariable *GV =
+  new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C,
+                           ".str");
+  if (isUTF16) {
+    CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy);
+    GV->setAlignment(Align.getQuantity());
+  }
+  Fields[1] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
+  
+  // String length.
+  const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
+  Fields[2] = llvm::ConstantInt::get(Ty, StringLength);
+  
+  // The struct.
+  C = llvm::ConstantStruct::get(STy, Fields);
+  GV = new llvm::GlobalVariable(getModule(), C->getType(), true,
+                                llvm::GlobalVariable::PrivateLinkage, C,
+                                "_unnamed_nsstring_");
+  // FIXME. Fix section.
+  if (const char *Sect = getContext().Target.getNSStringSection())
+    GV->setSection(Sect);
+  Entry.setValue(GV);
+  
+  return GV;
 }
 
 /// GetStringForStringLiteral - Return the appropriate bytes for a

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=102189&r1=102188&r2=102189&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Fri Apr 23 12:41:07 2010
@@ -147,6 +147,10 @@
   /// strings. This value has type int * but is actually an Obj-C class pointer.
   llvm::Constant *CFConstantStringClassRef;
 
+  /// NSConstantStringClassRef - Cached reference to the class for constant
+  /// strings. This value has type int * but is actually an Obj-C class pointer.
+  llvm::Constant *NSConstantStringClassRef;
+
   /// Lazily create the Objective-C runtime
   void createObjCRuntime();
 

Modified: cfe/trunk/lib/Frontend/PCHReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReader.cpp?rev=102189&r1=102188&r2=102189&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Fri Apr 23 12:41:07 2010
@@ -1766,17 +1766,16 @@
   if (unsigned ObjCClassRedef
       = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
     Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
-#if 0
-  // FIXME. Accommodate for this in several PCH/Index tests
-  if (unsigned ObjCSelRedef
-      = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
-    Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
-#endif
   if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_DESCRIPTOR])
     Context->setBlockDescriptorType(GetType(String));
   if (unsigned String
       = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
     Context->setBlockDescriptorExtendedType(GetType(String));
+  if (unsigned ObjCSelRedef
+      = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
+    Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
+  if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_NS_CONSTANT_STRING])
+    Context->setNSConstantStringType(GetType(String));
 }
 
 /// \brief Retrieve the name of the original source file name

Modified: cfe/trunk/lib/Frontend/PCHWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriter.cpp?rev=102189&r1=102188&r2=102189&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Fri Apr 23 12:41:07 2010
@@ -2095,12 +2095,10 @@
   AddTypeRef(Context.getsigjmp_bufType(), Record);
   AddTypeRef(Context.ObjCIdRedefinitionType, Record);
   AddTypeRef(Context.ObjCClassRedefinitionType, Record);
-#if 0
-  // FIXME. Accommodate for this in several PCH/Indexer tests
-  AddTypeRef(Context.ObjCSelRedefinitionType, Record);
-#endif
   AddTypeRef(Context.getRawBlockdescriptorType(), Record);
   AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
+  AddTypeRef(Context.ObjCSelRedefinitionType, Record);
+  AddTypeRef(Context.getRawNSConstantStringType(), Record);
   Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
 
   // Keep writing types and declarations until all types and





More information about the cfe-commits mailing list