[cfe-commits] r45778 - in /cfe/trunk: CodeGen/CGDecl.cpp CodeGen/CodeGenModule.cpp CodeGen/CodeGenTypes.cpp CodeGen/CodeGenTypes.h include/clang/Basic/TargetInfo.h test/CodeGen/globalinit.c

Chris Lattner sabre at nondot.org
Wed Jan 9 10:47:26 PST 2008


Author: lattner
Date: Wed Jan  9 12:47:25 2008
New Revision: 45778

URL: http://llvm.org/viewvc/llvm-project?rev=45778&view=rev
Log:
implement proper support for _Bool in memory, which is usually i8, not i1.
This fixes a crash reported by Seo Sanghyeon

Modified:
    cfe/trunk/CodeGen/CGDecl.cpp
    cfe/trunk/CodeGen/CodeGenModule.cpp
    cfe/trunk/CodeGen/CodeGenTypes.cpp
    cfe/trunk/CodeGen/CodeGenTypes.h
    cfe/trunk/include/clang/Basic/TargetInfo.h
    cfe/trunk/test/CodeGen/globalinit.c

Modified: cfe/trunk/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGDecl.cpp?rev=45778&r1=45777&r2=45778&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/CodeGen/CGDecl.cpp Wed Jan  9 12:47:25 2008
@@ -71,7 +71,7 @@
   llvm::Value *&DMEntry = LocalDeclMap[&D];
   assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
   
-  const llvm::Type *LTy = ConvertType(Ty);
+  const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
   llvm::Constant *Init = 0;
   if (D.getInit() == 0) {
     Init = llvm::Constant::getNullValue(LTy);

Modified: cfe/trunk/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenModule.cpp?rev=45778&r1=45777&r2=45778&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/CodeGen/CodeGenModule.cpp Wed Jan  9 12:47:25 2008
@@ -121,7 +121,7 @@
   llvm::Constant *&Entry = GlobalDeclMap[D];
   if (Entry) return Entry;
   
-  const llvm::Type *Ty = getTypes().ConvertType(D->getType());
+  const llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType());
 
   // Check to see if the global already exists.
   llvm::GlobalVariable *GV = getModule().getGlobalVariable(D->getName());

Modified: cfe/trunk/CodeGen/CodeGenTypes.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenTypes.cpp?rev=45778&r1=45777&r2=45778&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CodeGenTypes.cpp (original)
+++ cfe/trunk/CodeGen/CodeGenTypes.cpp Wed Jan  9 12:47:25 2008
@@ -149,6 +149,25 @@
   return ResultType;
 }
 
+/// ConvertTypeForMem - Convert type T into a llvm::Type. Maintain and use
+/// type cache through TypeHolderMap.  This differs from ConvertType in that
+/// it is used to convert to the memory representation for a type.  For
+/// example, the scalar representation for _Bool is i1, but the memory
+/// representation is usually i8 or i32, depending on the target.
+const llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) {
+  const llvm::Type *R = ConvertType(T);
+  
+  // If this is a non-bool type, don't map it.
+  if (R != llvm::Type::Int1Ty)
+    return R;
+    
+  // Otherwise, return an integer of the target-specified size.
+  unsigned BoolWidth = (unsigned)Context.getTypeSize(T, SourceLocation());
+  return llvm::IntegerType::get(BoolWidth);
+  
+}
+
+
 const llvm::Type *CodeGenTypes::ConvertNewType(QualType T) {
   const clang::Type &Ty = *T.getCanonicalType();
   
@@ -165,8 +184,7 @@
       return llvm::IntegerType::get(8);
 
     case BuiltinType::Bool:
-      // FIXME: This is very strange.  We want scalars to be i1, but in memory
-      // they can be i1 or i32.  Should the codegen handle this issue?
+      // Note that we always return bool as i1 for use as a scalar type.
       return llvm::Type::Int1Ty;
       
     case BuiltinType::Char_S:

Modified: cfe/trunk/CodeGen/CodeGenTypes.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenTypes.h?rev=45778&r1=45777&r2=45778&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CodeGenTypes.h (original)
+++ cfe/trunk/CodeGen/CodeGenTypes.h Wed Jan  9 12:47:25 2008
@@ -118,16 +118,26 @@
   ASTContext &getContext() const { return Context; }
 
   /// ConvertType - Convert type T into a llvm::Type. Maintain and use
-  /// type cache through TypeHOlderMap.
+  /// type cache through TypeHolderMap.
   const llvm::Type *ConvertType(QualType T);
-  void DecodeArgumentTypes(const FunctionTypeProto &FTP, 
-                           std::vector<const llvm::Type*> &ArgTys);
-
+  
+  /// ConvertTypeForMem - Convert type T into a llvm::Type. Maintain and use
+  /// type cache through TypeHolderMap.  This differs from ConvertType in that
+  /// it is used to convert to the memory representation for a type.  For
+  /// example, the scalar representation for _Bool is i1, but the memory
+  /// representation is usually i8 or i32, depending on the target.
+  const llvm::Type *ConvertTypeForMem(QualType T);
+  
+  
   const CGRecordLayout *getCGRecordLayout(const llvm::Type*) const;
   
   /// getLLVMFieldNo - Return llvm::StructType element number
   /// that corresponds to the field FD.
   unsigned getLLVMFieldNo(const FieldDecl *FD);
+    
+public:  // These are internal details of CGT that shouldn't be used externally.
+  void DecodeArgumentTypes(const FunctionTypeProto &FTP, 
+                           std::vector<const llvm::Type*> &ArgTys);
 
   /// addFieldInfo - Assign field number to field FD.
   void addFieldInfo(const FieldDecl *FD, unsigned No, unsigned Begin, 

Modified: cfe/trunk/include/clang/Basic/TargetInfo.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/TargetInfo.h?rev=45778&r1=45777&r2=45778&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/TargetInfo.h (original)
+++ cfe/trunk/include/clang/Basic/TargetInfo.h Wed Jan  9 12:47:25 2008
@@ -233,6 +233,12 @@
   
   ///===---- Some helper methods ------------------------------------------===//
 
+  unsigned getBoolWidth(FullSourceLoc Loc) {
+    uint64_t Size; unsigned Align;
+    getBoolInfo(Size, Align, Loc);
+    return static_cast<unsigned>(Size);
+  }
+  
   unsigned getCharWidth(FullSourceLoc Loc) {
     uint64_t Size; unsigned Align;
     getCharInfo(Size, Align, Loc);

Modified: cfe/trunk/test/CodeGen/globalinit.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/globalinit.c?rev=45778&r1=45777&r2=45778&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/globalinit.c (original)
+++ cfe/trunk/test/CodeGen/globalinit.c Wed Jan  9 12:47:25 2008
@@ -26,3 +26,9 @@
  return c ? buf : bufptr;
 }
 
+
+_Bool booltest = 0;
+void booltest2() {
+  static _Bool booltest3 = 4;
+}
+





More information about the cfe-commits mailing list