[llvm-commits] [llvm-gcc-4.2] r42199 - in /llvm-gcc-4.2/trunk/gcc: c-common.c llvm-convert.cpp llvm-internal.h

Chris Lattner sabre at nondot.org
Fri Sep 21 10:49:57 PDT 2007


Author: lattner
Date: Fri Sep 21 12:49:57 2007
New Revision: 42199

URL: http://llvm.org/viewvc/llvm-project?rev=42199&view=rev
Log:
Add supprot for a new gcroot attribute, patch by 
Eric Christopher

Modified:
    llvm-gcc-4.2/trunk/gcc/c-common.c
    llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp
    llvm-gcc-4.2/trunk/gcc/llvm-internal.h

Modified: llvm-gcc-4.2/trunk/gcc/c-common.c
URL: http://llvm.org/viewvc/llvm-project/llvm-gcc-4.2/trunk/gcc/c-common.c?rev=42199&r1=42198&r2=42199&view=diff

==============================================================================
--- llvm-gcc-4.2/trunk/gcc/c-common.c (original)
+++ llvm-gcc-4.2/trunk/gcc/c-common.c Fri Sep 21 12:49:57 2007
@@ -630,6 +630,7 @@
 /* LLVM LOCAL begin */
 #ifdef ENABLE_LLVM
 static tree handle_annotate_attribute (tree*, tree, tree, int, bool *);
+static tree handle_gcroot_attribute (tree *, tree, tree, int, bool *);
 #endif
 /* LLVM LOCAL end */
 
@@ -738,6 +739,8 @@
   #ifdef ENABLE_LLVM
   { "annotate",                0, -1, true, false, false,
                               handle_annotate_attribute },
+  { "gcroot",		      0, 0, false, true, false,
+			      handle_gcroot_attribute },
   #endif
   /* LLVM LOCAL end */
   { NULL,                     0, 0, false, false, false, NULL }
@@ -6068,6 +6071,21 @@
   
   return NULL_TREE;
 }
+
+/* Handle the "gcroot" attribute */
+static tree
+handle_gcroot_attribute (tree *node, tree name, tree ARG_UNUSED(args),
+			 int ARG_UNUSED(flags), bool *ARG_UNUSED(no_add_attrs))
+{
+  if (!TYPE_P (*node)
+      || !POINTER_TYPE_P (*node))
+    {
+      warning ("%qs attribute ignored", IDENTIFIER_POINTER (name));
+      *no_add_attrs = true;
+    }
+  
+  return NULL_TREE;
+}
 #endif
 /* LLVM LOCAL end */
 

Modified: llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp?rev=42199&r1=42198&r2=42199&view=diff

==============================================================================
--- llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp (original)
+++ llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp Fri Sep 21 12:49:57 2007
@@ -683,6 +683,11 @@
       // Emit annotate intrinsic if arg has annotate attr
       if (DECL_ATTRIBUTES(Args))
         EmitAnnotateIntrinsic(Tmp, Args);
+
+      // Emit gcroot intrinsic if arg has attribute
+      if (POINTER_TYPE_P(TREE_TYPE(Args))
+	  && lookup_attribute ("gcroot", TYPE_ATTRIBUTES(TREE_TYPE(Args))))
+      	EmitTypeGcroot(Tmp, Args);
       
       Client.setName(Name);
       Client.setLocation(Tmp);
@@ -1366,6 +1371,25 @@
 }
 
 
+// Emits code to do something for a type attribute
+void TreeToLLVM::EmitTypeGcroot(Value *V, tree decl) {
+
+  Function *gcrootFun = Intrinsic::getDeclaration(TheModule,
+						  Intrinsic::gcroot);
+  
+  // The idea is that it's a pointer to type "Value"
+  // which is opaque* but the routine expects i8** and i8*.
+  const PointerType *Ty = PointerType::get(Type::Int8Ty);
+  V = Builder.CreateBitCast(V, PointerType::get(Ty), "tmp");
+  
+  Value *Ops[2] = {
+    V,
+    ConstantPointerNull::get(Ty)
+  };
+  
+  Builder.CreateCall(gcrootFun, Ops, Ops+2);
+}  
+
 // Emits annotate intrinsic if the decl has the annotate attribute set.
 void TreeToLLVM::EmitAnnotateIntrinsic(Value *V, tree decl) {
   
@@ -1527,6 +1551,17 @@
   // Handle annotate attributes
   if (DECL_ATTRIBUTES(decl))
     EmitAnnotateIntrinsic(AI, decl);
+
+  // Handle gcroot attribute
+  if (POINTER_TYPE_P(TREE_TYPE (decl))
+      && lookup_attribute("gcroot", TYPE_ATTRIBUTES(TREE_TYPE (decl))))
+    {
+      // We should null out local variables so that a stack crawl
+      // before initialization doesn't get garbage results to follow.
+      const Type *T = cast<PointerType>(AI->getType())->getElementType();
+      EmitTypeGcroot(AI, decl);
+      Builder.CreateStore(Constant::getNullValue(T), AI);
+    }
   
   if (TheDebugInfo) {
     if (DECL_NAME(decl)) {

Modified: llvm-gcc-4.2/trunk/gcc/llvm-internal.h
URL: http://llvm.org/viewvc/llvm-project/llvm-gcc-4.2/trunk/gcc/llvm-internal.h?rev=42199&r1=42198&r2=42199&view=diff

==============================================================================
--- llvm-gcc-4.2/trunk/gcc/llvm-internal.h (original)
+++ llvm-gcc-4.2/trunk/gcc/llvm-internal.h Fri Sep 21 12:49:57 2007
@@ -435,7 +435,9 @@
   
   /// EmitAnnotateIntrinsic - Emits call to annotate attr intrinsic
   void EmitAnnotateIntrinsic(Value *V, tree_node *decl);
-  
+
+  /// EmitTypeGcroot - Emits call to make type a gcroot
+  void EmitTypeGcroot(Value *V, tree_node *decl);
 private:
 
   // Emit* - These are delegates from Emit, and have the same parameter





More information about the llvm-commits mailing list