Skip to content

Commit dc3e4cc

Browse files
committed
MCExpr: Add FindAssociatedSection, which attempts to mirror the 'as' semantics
that associate sections with expressions. llvm-svn: 130517
1 parent 252e8f9 commit dc3e4cc

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

llvm/include/llvm/MC/MCExpr.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class MCAsmInfo;
1919
class MCAsmLayout;
2020
class MCAssembler;
2121
class MCContext;
22+
class MCSection;
2223
class MCSectionData;
2324
class MCSymbol;
2425
class MCValue;
@@ -92,6 +93,12 @@ class MCExpr {
9293
/// @result - True on success.
9394
bool EvaluateAsRelocatable(MCValue &Res, const MCAsmLayout &Layout) const;
9495

96+
/// FindAssociatedSection - Find the "associated section" for this expression,
97+
/// which is currently defined as the absolute section for constants, or
98+
/// otherwise the section associated with the first defined symbol in the
99+
/// expression.
100+
const MCSection *FindAssociatedSection() const;
101+
95102
/// @}
96103

97104
static bool classof(const MCExpr *) { return true; }
@@ -420,6 +427,7 @@ class MCTargetExpr : public MCExpr {
420427
virtual bool EvaluateAsRelocatableImpl(MCValue &Res,
421428
const MCAsmLayout *Layout) const = 0;
422429
virtual void AddValueSymbols(MCAssembler *) const = 0;
430+
virtual const MCSection *FindAssociatedSection() const = 0;
423431

424432
static bool classof(const MCExpr *E) {
425433
return E->getKind() == MCExpr::Target;

llvm/include/llvm/MC/MCSymbol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ namespace llvm {
5656
mutable unsigned IsUsed : 1;
5757

5858
private: // MCContext creates and uniques these.
59+
friend class MCExpr;
5960
friend class MCContext;
6061
MCSymbol(StringRef name, bool isTemporary)
6162
: Name(name), Section(0), Value(0),

llvm/lib/MC/MCExpr.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,3 +559,45 @@ bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
559559
assert(0 && "Invalid assembly expression kind!");
560560
return false;
561561
}
562+
563+
const MCSection *MCExpr::FindAssociatedSection() const {
564+
switch (getKind()) {
565+
case Target:
566+
// We never look through target specific expressions.
567+
return cast<MCTargetExpr>(this)->FindAssociatedSection();
568+
569+
case Constant:
570+
return MCSymbol::AbsolutePseudoSection;
571+
572+
case SymbolRef: {
573+
const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
574+
const MCSymbol &Sym = SRE->getSymbol();
575+
576+
if (Sym.isDefined())
577+
return &Sym.getSection();
578+
579+
return 0;
580+
}
581+
582+
case Unary:
583+
return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
584+
585+
case Binary: {
586+
const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
587+
const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
588+
const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
589+
590+
// If either section is absolute, return the other.
591+
if (LHS_S == MCSymbol::AbsolutePseudoSection)
592+
return RHS_S;
593+
if (RHS_S == MCSymbol::AbsolutePseudoSection)
594+
return LHS_S;
595+
596+
// Otherwise, return the first non-null section.
597+
return LHS_S ? LHS_S : RHS_S;
598+
}
599+
}
600+
601+
assert(0 && "Invalid assembly expression kind!");
602+
return 0;
603+
}

llvm/lib/Target/ARM/ARMMCExpr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class ARMMCExpr : public MCTargetExpr {
6060
bool EvaluateAsRelocatableImpl(MCValue &Res,
6161
const MCAsmLayout *Layout) const;
6262
void AddValueSymbols(MCAssembler *) const;
63+
const MCSection *FindAssociatedSection() const {
64+
return getSubExpr()->FindAssociatedSection();
65+
}
6366

6467
static bool classof(const MCExpr *E) {
6568
return E->getKind() == MCExpr::Target;

0 commit comments

Comments
 (0)