[cfe-commits] r83957 - in /cfe/trunk: lib/AST/Expr.cpp lib/CodeGen/CGExprConstant.cpp test/CodeGen/statements.c

Chris Lattner sabre at nondot.org
Tue Oct 13 00:14:17 PDT 2009


Author: lattner
Date: Tue Oct 13 02:14:16 2009
New Revision: 83957

URL: http://llvm.org/viewvc/llvm-project?rev=83957&view=rev
Log:
Teach sema and codegen about the difference between address of labels,
which is a common idiom to improve PIC'ness of code using the addr of
label extension.  This implementation is a gross hack, but the only other
alternative would be to teach evalutate about this horrid combination.
While GCC allows things like "&&foo - &&bar + 1", people don't use this
in practice.  This implements PR5131.

Modified:
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/lib/CodeGen/CGExprConstant.cpp
    cfe/trunk/test/CodeGen/statements.c

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=83957&r1=83956&r2=83957&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Tue Oct 13 02:14:16 2009
@@ -1256,15 +1256,24 @@
   }
   case ImplicitValueInitExprClass:
     return true;
-  case ParenExprClass: {
+  case ParenExprClass:
     return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
-  }
   case UnaryOperatorClass: {
     const UnaryOperator* Exp = cast<UnaryOperator>(this);
     if (Exp->getOpcode() == UnaryOperator::Extension)
       return Exp->getSubExpr()->isConstantInitializer(Ctx);
     break;
   }
+  case BinaryOperatorClass: {
+    // Special case &&foo - &&bar.  It would be nice to generalize this somehow
+    // but this handles the common case.
+    const BinaryOperator *Exp = cast<BinaryOperator>(this);
+    if (Exp->getOpcode() == BinaryOperator::Sub &&
+        isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
+        isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
+      return true;
+    break;
+  }
   case ImplicitCastExprClass:
   case CStyleCastExprClass:
     // Handle casts with a destination that's a struct or union; this

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprConstant.cpp Tue Oct 13 02:14:16 2009
@@ -402,7 +402,7 @@
   llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
     return Visit(E->getInitializer());
   }
-  
+    
   llvm::Constant *EmitMemberFunctionPointer(CXXMethodDecl *MD) {
     assert(MD->isInstance() && "Member function must not be static!");
     
@@ -446,6 +446,27 @@
     return 0;
   }
     
+  llvm::Constant *VisitBinSub(BinaryOperator *E) {
+    // This must be a pointer/pointer subtraction.  This only happens for
+    // address of label.
+    if (!isa<AddrLabelExpr>(E->getLHS()->IgnoreParenNoopCasts(CGM.getContext())) ||
+       !isa<AddrLabelExpr>(E->getRHS()->IgnoreParenNoopCasts(CGM.getContext())))
+      return 0;
+    
+    llvm::Constant *LHS = CGM.EmitConstantExpr(E->getLHS(),
+                                               E->getLHS()->getType(), CGF);
+    llvm::Constant *RHS = CGM.EmitConstantExpr(E->getRHS(),
+                                               E->getRHS()->getType(), CGF);
+
+    const llvm::Type *ResultType = ConvertType(E->getType());
+    LHS = llvm::ConstantExpr::getPtrToInt(LHS, ResultType);
+    RHS = llvm::ConstantExpr::getPtrToInt(RHS, ResultType);
+        
+    // No need to divide by element size, since addr of label is always void*,
+    // which has size 1 in GNUish.
+    return llvm::ConstantExpr::getSub(LHS, RHS);
+  }
+    
   llvm::Constant *VisitCastExpr(CastExpr* E) {
     switch (E->getCastKind()) {
     case CastExpr::CK_ToUnion: {

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

==============================================================================
--- cfe/trunk/test/CodeGen/statements.c (original)
+++ cfe/trunk/test/CodeGen/statements.c Tue Oct 13 02:14:16 2009
@@ -18,7 +18,9 @@
 blong:
 bing:
  ;
-static long x = &&bar; // - &&baz;
+
+// PR5131
+static long x = &&bar - &&baz;
 static long y = &&baz;
   &&bing;
   &&blong;





More information about the cfe-commits mailing list