diff options
| author | Chris Lattner <sabre@nondot.org> | 2009-10-13 07:14:16 +0000 |
|---|---|---|
| committer | Chris Lattner <sabre@nondot.org> | 2009-10-13 07:14:16 +0000 |
| commit | 3eb172a02b34c4925dae38eb743571ae7d9f257c (patch) | |
| tree | 8d08f6dd8942b0d6d0ac3dca63fde8188a4b2013 /clang | |
| parent | 6f7d8347c1c6230187d33fe8fdb21c43ee3454bb (diff) | |
| download | bcm5719-llvm-3eb172a02b34c4925dae38eb743571ae7d9f257c.tar.gz bcm5719-llvm-3eb172a02b34c4925dae38eb743571ae7d9f257c.zip | |
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.
llvm-svn: 83957
Diffstat (limited to 'clang')
| -rw-r--r-- | clang/lib/AST/Expr.cpp | 13 | ||||
| -rw-r--r-- | clang/lib/CodeGen/CGExprConstant.cpp | 23 | ||||
| -rw-r--r-- | clang/test/CodeGen/statements.c | 4 |
3 files changed, 36 insertions, 4 deletions
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 6da11c1cab8..8516d41df6c 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -1256,15 +1256,24 @@ bool Expr::isConstantInitializer(ASTContext &Ctx) const { } 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 diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp index fc24144f442..9621e2a21c0 100644 --- a/clang/lib/CodeGen/CGExprConstant.cpp +++ b/clang/lib/CodeGen/CGExprConstant.cpp @@ -402,7 +402,7 @@ public: 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 @@ public: 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: { diff --git a/clang/test/CodeGen/statements.c b/clang/test/CodeGen/statements.c index c50d1d5664c..45bbd9ac024 100644 --- a/clang/test/CodeGen/statements.c +++ b/clang/test/CodeGen/statements.c @@ -18,7 +18,9 @@ baz: blong: bing: ; -static long x = &&bar; // - &&baz; + +// PR5131 +static long x = &&bar - &&baz; static long y = &&baz; &&bing; &&blong; |

