[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cfe-dev
Subject:    Re: [cfe-dev] stack-less model on small devices (patch)
From:       <Alireza.Moshtaghi () microchip ! com>
Date:       2008-04-30 18:31:38
Message-ID: C9F1221126AF8A4BBCFFA4FA3AFA5290012FF1DE () CHN-CL-MAIL01 ! mchp-main ! com
[Download RAW message or body]

I created the patch for my target specific modifications and sent it to
cfe-commits. Since this is my first time to send a patch I don't know if
I have submitted my changes to the right place or not and of course what
is the turnaround time.
I also have attached it to this email just in case.
Please let me know if I have to do it differently.

Thanks
Ali

-----Original Message-----
From: Chris Lattner [mailto:clattner@apple.com] 
Sent: Thursday, April 24, 2008 9:46 AM
To: Alireza Moshtaghi - C13012
Cc: cfe-dev@cs.uiuc.edu
Subject: Re: [cfe-dev] stack-less model on small devices

On Apr 23, 2008, at 4:25 PM, Alireza.Moshtaghi@microchip.com wrote:
> Let me summarize...
> (patches)
> Ok, that makes sense to have the patches applied by someone else,
> however, my patches will surely break other peoples work. I'm just
> curious, how are you going to merge them? Conditional compiling?
> Commandline flags?

The best way to do this is to add a target to clang for your  
architecture, and conditionalize the codegen aspects on your target  
being active.  This means you'll be able to compile with 'clang t.c - 
arch myarch'.

> (alloca)
> We tried code generation in llvm by replacing alloca with global
> addresses however, (1) the name of local variable is not as easily
> available, (2)I don't feel quite comfortable with having the storage
> class of variable change at the last phase of translation. It sounds
> more reasonable to get it right in the front-end to begin with.

Using global variables should work reasonably well.  Just make sure  
they are marked as 'internal'.  The 'globalopt' pass will do SROA and  
other optimizations on them to help eliminate them.  I imagine that  
there are improvements we could also make, but there is nothing "in  
principle" that would prevent this from working.

-Chris

["patch-svn.txt" (text/plain)]

Index: include/clang/Basic/TargetInfo.h
===================================================================
--- include/clang/Basic/TargetInfo.h	(revision 50341)
+++ include/clang/Basic/TargetInfo.h	(working copy)
@@ -59,8 +59,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 +81,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
+  virtual unsigned getIntWidth() const { return 32; } // FIXME
+  virtual unsigned getIntAlign() const { return 32; } // FIXME
   
   /// getLongWidth/Align - Return the size of 'signed long' and 'unsigned long'
   /// for this target, in bits.
Index: lib/Basic/Targets.cpp
===================================================================
--- lib/Basic/Targets.cpp	(revision 50341)
+++ lib/Basic/Targets.cpp	(working copy)
@@ -863,6 +863,28 @@
 } // end anonymous namespace.
 
 
+namespace {
+class PIC16TargetInfo : public TargetInfo{
+public:
+  PIC16TargetInfo(const std::string& triple) : TargetInfo(triple) {}
+  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 {}
+};
+}
+
 //===----------------------------------------------------------------------===//
 // Driver code
 //===----------------------------------------------------------------------===//
@@ -890,6 +912,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);
   
Index: lib/CodeGen/CGDecl.cpp
===================================================================
--- lib/CodeGen/CGDecl.cpp	(revision 50341)
+++ lib/CodeGen/CGDecl.cpp	(working copy)
@@ -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;
@@ -113,10 +114,35 @@
 
   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 (strncmp (this->Target.getTargetTriple(), "pic16-", 6) == 0) {
+      const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
+      llvm::Constant *Init = 0;
+      if (D.getInit() == 0) {
+        Init = llvm::Constant::getNullValue(LTy);
+      } else {
+        Init = CGM.EmitConstantExpr(D.getInit(), this);
+      }
+      
+      assert(Init && "Unable to create initialiser for static decl");
+      
+      std::string ContextName;
+      if (const FunctionDecl * FD = dyn_cast<FunctionDecl>(CurFuncDecl))
+        ContextName = FD->getName();
+      else
+        assert(0 && "Unknown context for block var decl"); // FIXME Handle objc.
+      
+      DeclPtr = 
+        new llvm::GlobalVariable(LTy, false, llvm::GlobalValue::InternalLinkage,
+                                 Init, ContextName + "_ovr_" + D.getName(),
+                                 &CGM.getModule(), 0, Ty.getAddressSpace());
+      
+    }
+    else {
+      // 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 {
     // TODO: Create a dynamic alloca.
     assert(0 && "FIXME: Local VLAs not implemented yet");
@@ -148,20 +174,37 @@
     // Variable sized values always are passed by-reference.
     DeclPtr = Arg;
   } else {
-    // A fixed sized first class variable becomes an alloca in the entry block.
-    const llvm::Type *LTy = ConvertType(Ty);
-    if (LTy->isFirstClassType()) {
-      // TODO: Alignment
-      DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
-                                     AllocaInsertPt);
+    if (strncmp(this->Target.getTargetTriple(), "pic16-", 6) == 0){
+      const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
       
-      // Store the initial value into the alloca.
-      Builder.CreateStore(Arg, DeclPtr);
+      llvm::Constant *Init = llvm::Constant::getNullValue(LTy);
+
+      std::string ContextName;
+      if (const FunctionDecl * FD = dyn_cast<FunctionDecl>(CurFuncDecl))
+        ContextName = FD->getName();
+      else
+        assert(0 && "Unknown context for block var decl"); // FIXME Handle objc.
+      
+      DeclPtr = 
+        new llvm::GlobalVariable(LTy, false, llvm::GlobalValue::InternalLinkage,
+                                 Init, ContextName + "_arg_" + D.getName(),
+                                 &CGM.getModule(), 0, Ty.getAddressSpace());          
     } else {
-      // Otherwise, if this is an aggregate, just use the input pointer.
-      DeclPtr = Arg;
+      // A fixed sized first class variable becomes an alloca in the entry block.
+      const llvm::Type *LTy = ConvertType(Ty);
+      if (LTy->isFirstClassType()) {
+        // TODO: Alignment
+        DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
+                                       AllocaInsertPt);
+        
+        // Store the initial value into the alloca.
+        Builder.CreateStore(Arg, DeclPtr);
+      } else {
+        // Otherwise, if this is an aggregate, just use the input pointer.
+        DeclPtr = Arg;
+      }
+      Arg->setName(D.getName());
     }
-    Arg->setName(D.getName());
   }
 
   llvm::Value *&DMEntry = LocalDeclMap[&D];


_______________________________________________
cfe-dev mailing list
cfe-dev@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic