[cfe-commits] r50844 - in /cfe/trunk: include/clang/Basic/TargetInfo.h lib/Basic/TargetInfo.cpp lib/Basic/Targets.cpp lib/CodeGen/CGDecl.cpp lib/CodeGen/CodeGenFunction.h

Chris Lattner sabre at nondot.org
Wed May 7 22:58:21 PDT 2008


Author: lattner
Date: Thu May  8 00:58:21 2008
New Revision: 50844

URL: http://llvm.org/viewvc/llvm-project?rev=50844&view=rev
Log:
Add basic support for the pic-* target triples and add support for
targets that do not support recursion (and thus codegen stack variables
as globals).

Patch contributed by Alireza Moshtaghi!

Modified:
    cfe/trunk/include/clang/Basic/TargetInfo.h
    cfe/trunk/lib/Basic/TargetInfo.cpp
    cfe/trunk/lib/Basic/Targets.cpp
    cfe/trunk/lib/CodeGen/CGDecl.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

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

==============================================================================
--- cfe/trunk/include/clang/Basic/TargetInfo.h (original)
+++ cfe/trunk/include/clang/Basic/TargetInfo.h Thu May  8 00:58:21 2008
@@ -36,6 +36,7 @@
   // values are specified by the TargetInfo constructor.
   bool CharIsSigned;
   unsigned WCharWidth, WCharAlign;
+  unsigned IntWidth, IntAlign;
   unsigned DoubleWidth, DoubleAlign;
   
   const llvm::fltSemantics *FloatFormat, *DoubleFormat, *LongDoubleFormat;
@@ -59,8 +60,8 @@
   
   /// getPointerWidth - Return the width of pointers on this target, for the
   /// specified address space. FIXME: implement correctly.
-  uint64_t getPointerWidth(unsigned AddrSpace) const { return 32; }
-  uint64_t getPointerAlign(unsigned AddrSpace) const { return 32; }
+  virtual uint64_t getPointerWidth(unsigned AddrSpace) const { return 32; }
+  virtual uint64_t getPointerAlign(unsigned AddrSpace) const { return 32; }
   
   /// getBoolWidth/Align - Return the size of '_Bool' and C++ 'bool' for this
   /// target, in bits.
@@ -81,8 +82,8 @@
   
   /// getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for
   /// this target, in bits.
-  unsigned getIntWidth() const { return 32; } // FIXME
-  unsigned getIntAlign() const { return 32; } // FIXME
+  unsigned getIntWidth() const { return IntWidth; }
+  unsigned getIntAlign() const { return IntAlign; }
   
   /// getLongWidth/Align - Return the size of 'signed long' and 'unsigned long'
   /// for this target, in bits.
@@ -191,6 +192,8 @@
     const char * const Aliases[5];
     const char * const Register;
   };
+
+  virtual bool useGlobalsForAutomaticVariables() const {return false;}
   
 protected:
   virtual void getGCCRegNames(const char * const *&Names, 

Modified: cfe/trunk/lib/Basic/TargetInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/TargetInfo.cpp?rev=50844&r1=50843&r2=50844&view=diff

==============================================================================
--- cfe/trunk/lib/Basic/TargetInfo.cpp (original)
+++ cfe/trunk/lib/Basic/TargetInfo.cpp Thu May  8 00:58:21 2008
@@ -24,6 +24,7 @@
   // Set defaults.  These should be overridden by concrete targets as needed.
   CharIsSigned = true;
   WCharWidth = WCharAlign = 32;
+  IntWidth = IntAlign = 32;
   DoubleWidth = 64;
   DoubleAlign = 32;
   FloatFormat = &llvm::APFloat::IEEEsingle;

Modified: cfe/trunk/lib/Basic/Targets.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Targets.cpp?rev=50844&r1=50843&r2=50844&view=diff

==============================================================================
--- cfe/trunk/lib/Basic/Targets.cpp (original)
+++ cfe/trunk/lib/Basic/Targets.cpp Thu May  8 00:58:21 2008
@@ -863,6 +863,36 @@
 } // end anonymous namespace.
 
 
+namespace {
+  class PIC16TargetInfo : public TargetInfo{
+  public:
+    PIC16TargetInfo(const std::string& triple) : TargetInfo(triple) {
+      IntWidth = IntAlign = 16;
+    }
+    virtual uint64_t getPointerWidth(unsigned AddrSpace) const { return 16; }
+    virtual uint64_t getPointerAlign(unsigned AddrSpace) const { return 8; }
+    virtual unsigned getIntWidth() const { return 16; }
+    virtual unsigned getIntAlign() const { return 8; }
+    virtual void getTargetDefines(std::vector<char> &Defines) const {
+      Define(Defines, "__pic16");
+    }
+    virtual void getTargetBuiltins(const Builtin::Info *&Records,
+                                   unsigned &NumRecords) const {}
+    virtual const char *getVAListDeclaration() const { return "";}
+    virtual const char *getClobbers() const {return "";}
+    virtual const char *getTargetPrefix() const {return "";}
+    virtual void getGCCRegNames(const char * const *&Names, 
+                                unsigned &NumNames) const {} 
+    virtual bool validateAsmConstraint(char c, 
+                                       TargetInfo::ConstraintInfo &info) const {
+      return true;
+    }
+    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases, 
+                                  unsigned &NumAliases) const {}
+    virtual bool useGlobalsForAutomaticVariables() const {return true;}
+  };
+}
+
 //===----------------------------------------------------------------------===//
 // Driver code
 //===----------------------------------------------------------------------===//
@@ -890,6 +920,9 @@
   if (T.find("x86_64-") == 0)
     return new DarwinX86_64TargetInfo(T);
   
+  if (T.find("pic16-") == 0)
+    return new PIC16TargetInfo(T);
+
   if (IsX86(T))
     return new DarwinI386TargetInfo(T);
   

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Thu May  8 00:58:21 2008
@@ -15,6 +15,7 @@
 #include "CodeGenModule.h"
 #include "clang/AST/AST.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Basic/TargetInfo.h"
 #include "llvm/GlobalVariable.h"
 #include "llvm/Type.h"
 using namespace clang;
@@ -67,16 +68,16 @@
   }
 }
 
-void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
+llvm::GlobalValue *
+CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D,
+                                            bool NoInit,
+                                            const char *Separator) {
   QualType Ty = D.getType();
   assert(Ty->isConstantSizeType() && "VLAs can't be static");
   
-  llvm::Value *&DMEntry = LocalDeclMap[&D];
-  assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
-  
   const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
   llvm::Constant *Init = 0;
-  if (D.getInit() == 0) {
+  if ((D.getInit() == 0) || NoInit) {
     Init = llvm::Constant::getNullValue(LTy);
   } else {
     Init = CGM.EmitConstantExpr(D.getInit(), this);
@@ -92,9 +93,19 @@
 
   llvm::GlobalValue *GV = 
     new llvm::GlobalVariable(LTy, false, llvm::GlobalValue::InternalLinkage,
-                             Init, ContextName + "." + D.getName(),
+                             Init, ContextName + Separator + D.getName(),
                              &CGM.getModule(), 0, Ty.getAddressSpace());
 
+  return GV;
+}
+
+void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) { 
+
+  llvm::Value *&DMEntry = LocalDeclMap[&D];
+  assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
+  
+  llvm::GlobalValue *GV = GenerateStaticBlockVarDecl(D, false, ".");
+
   if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
     SourceManager &SM = CGM.getContext().getSourceManager();
     llvm::Constant *Ann =
@@ -107,16 +118,23 @@
   
 /// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
 /// variable declaration with auto, register, or no storage class specifier.
-/// These turn into simple stack objects.
+/// These turn into simple stack objects, or GlobalValues depending on target.
 void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
   QualType Ty = D.getType();
 
   llvm::Value *DeclPtr;
   if (Ty->isConstantSizeType()) {
-    // A normal fixed sized variable becomes an alloca in the entry block.
-    const llvm::Type *LTy = ConvertType(Ty);
-    // TODO: Alignment
-    DeclPtr = CreateTempAlloca(LTy, D.getName());
+    if (!Target.useGlobalsForAutomaticVariables()) {
+      // A normal fixed sized variable becomes an alloca in the entry block.
+      const llvm::Type *LTy = ConvertType(Ty);
+      // TODO: Alignment
+      DeclPtr = CreateTempAlloca(LTy, D.getName());
+    } else {
+      // Targets that don't support recursion emit locals as globals.
+      const char *Class =
+        D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
+      DeclPtr = GenerateStaticBlockVarDecl(D, true, Class);
+    }
   } else {
     // TODO: Create a dynamic alloca.
     assert(0 && "FIXME: Local VLAs not implemented yet");
@@ -139,7 +157,8 @@
   }
 }
 
-/// Emit an alloca for the specified parameter and set up LocalDeclMap.
+/// Emit an alloca (or GlobalValue depending on target) 
+/// for the specified parameter and set up LocalDeclMap.
 void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
   QualType Ty = D.getType();
   
@@ -147,6 +166,8 @@
   if (!Ty->isConstantSizeType()) {
     // Variable sized values always are passed by-reference.
     DeclPtr = Arg;
+  } else if (Target.useGlobalsForAutomaticVariables()) {
+    DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg.");
   } else {
     // A fixed sized first class variable becomes an alloca in the entry block.
     const llvm::Type *LTy = ConvertType(Ty);

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=50844&r1=50843&r2=50844&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Thu May  8 00:58:21 2008
@@ -488,6 +488,12 @@
                                bool DestIsVolatile);
   /// LoadComplexFromAddr - Load a complex number from the specified address.
   ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile);
+
+  /// GenerateStaticBlockVarDecl - return the the static
+  /// declaration of local variable. 
+  llvm::GlobalValue *GenerateStaticBlockVarDecl(const VarDecl &D,
+                                                bool NoInit,
+                                                const char *Separator);
 };
 }  // end namespace CodeGen
 }  // end namespace clang





More information about the cfe-commits mailing list