MCValue: Change to holding MCSymbolRefExprs instead of MCSymbols, we will need this for accessing to symbol modifiers.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@98791 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar 2010-03-18 00:59:10 +00:00
parent daf9733369
commit 9a1d20042f
4 changed files with 53 additions and 45 deletions

View File

@ -19,8 +19,9 @@
#include <cassert>
namespace llvm {
class MCSymbol;
class MCAsmInfo;
class MCSymbol;
class MCSymbolRefExpr;
class raw_ostream;
/// MCValue - This represents an "assembler immediate". In its most general
@ -34,13 +35,13 @@ class raw_ostream;
/// Note that this class must remain a simple POD value class, because we need
/// it to live in unions etc.
class MCValue {
const MCSymbol *SymA, *SymB;
const MCSymbolRefExpr *SymA, *SymB;
int64_t Cst;
public:
int64_t getConstant() const { return Cst; }
const MCSymbol *getSymA() const { return SymA; }
const MCSymbol *getSymB() const { return SymB; }
const MCSymbolRefExpr *getSymA() const { return SymA; }
const MCSymbolRefExpr *getSymB() const { return SymB; }
/// isAbsolute - Is this an absolute (as opposed to relocatable) value.
bool isAbsolute() const { return !SymA && !SymB; }
@ -57,11 +58,11 @@ public:
/// print - Print the value to the stream \arg OS.
void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
/// dump - Print the value to stderr.
void dump() const;
static MCValue get(const MCSymbol *SymA, const MCSymbol *SymB = 0,
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=0,
int64_t Val = 0) {
MCValue R;
assert((!SymB || SymA) && "Invalid relocatable MCValue!");
@ -70,7 +71,7 @@ public:
R.SymB = SymB;
return R;
}
static MCValue get(int64_t Val) {
MCValue R;
R.Cst = Val;
@ -78,7 +79,7 @@ public:
R.SymB = 0;
return R;
}
};
} // end namespace llvm

View File

@ -478,7 +478,7 @@ public:
unsigned Type = RIT_Vanilla;
// See <reloc.h>.
const MCSymbol *A = Target.getSymA();
const MCSymbol *A = &Target.getSymA()->getSymbol();
MCSymbolData *A_SD = &Asm.getSymbolData(*A);
if (!A_SD->getFragment())
@ -488,11 +488,11 @@ public:
uint32_t Value = A_SD->getAddress();
uint32_t Value2 = 0;
if (const MCSymbol *B = Target.getSymB()) {
MCSymbolData *B_SD = &Asm.getSymbolData(*B);
if (const MCSymbolRefExpr *B = Target.getSymB()) {
MCSymbolData *B_SD = &Asm.getSymbolData(B->getSymbol());
if (!B_SD->getFragment())
llvm_report_error("symbol '" + B->getName() +
llvm_report_error("symbol '" + B->getSymbol().getName() +
"' can not be undefined in a subtraction expression");
// Select the appropriate difference relocation type.
@ -545,7 +545,7 @@ public:
if (IsPCRel)
Offset += 1 << Log2Size;
if (Target.getSymB() ||
(Target.getSymA() && !Target.getSymA()->isUndefined() &&
(Target.getSymA() && !Target.getSymA()->getSymbol().isUndefined() &&
Offset))
return ComputeScatteredRelocationInfo(Asm, Fragment, Fixup, Target,
Relocs);
@ -565,7 +565,7 @@ public:
Type = RIT_Vanilla;
Value = 0;
} else {
const MCSymbol *Symbol = Target.getSymA();
const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
MCSymbolData *SD = &Asm.getSymbolData(*Symbol);
if (Symbol->isUndefined()) {
@ -1033,28 +1033,28 @@ bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout, MCAsmFixup &Fixup,
// atom, and so the value is resolved. We need explicit atom's to implement
// this more precisely.
bool IsResolved = true, IsPCRel = isFixupKindPCRel(Fixup.Kind);
if (const MCSymbol *Symbol = Target.getSymA()) {
if (Symbol->isDefined())
Value += getSymbolData(*Symbol).getAddress();
if (const MCSymbolRefExpr *A = Target.getSymA()) {
if (A->getSymbol().isDefined())
Value += getSymbolData(A->getSymbol()).getAddress();
else
IsResolved = false;
// With scattered symbols, we assume anything that isn't a PCrel temporary
// access can have an arbitrary value.
if (getBackend().hasScatteredSymbols() &&
(!IsPCRel || !Symbol->isTemporary()))
(!IsPCRel || !A->getSymbol().isTemporary()))
IsResolved = false;
}
if (const MCSymbol *Symbol = Target.getSymB()) {
if (Symbol->isDefined())
Value -= getSymbolData(*Symbol).getAddress();
if (const MCSymbolRefExpr *B = Target.getSymB()) {
if (B->getSymbol().isDefined())
Value -= getSymbolData(B->getSymbol()).getAddress();
else
IsResolved = false;
// With scattered symbols, we assume anything that isn't a PCrel temporary
// access can have an arbitrary value.
if (getBackend().hasScatteredSymbols() &&
(!IsPCRel || !Symbol->isTemporary()))
(!IsPCRel || !B->getSymbol().isTemporary()))
IsResolved = false;
}

View File

@ -30,7 +30,7 @@ void MCExpr::print(raw_ostream &OS) const {
case MCExpr::SymbolRef: {
const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
const MCSymbol &Sym = SRE.getSymbol();
// Parenthesize names that start with $ so that they don't look like
// absolute names.
if (Sym.getName()[0] == '$')
@ -59,14 +59,14 @@ void MCExpr::print(raw_ostream &OS) const {
case MCExpr::Binary: {
const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
// Only print parens around the LHS if it is non-trivial.
if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
OS << *BE.getLHS();
} else {
OS << '(' << *BE.getLHS() << ')';
}
switch (BE.getOpcode()) {
default: assert(0 && "Invalid opcode!");
case MCBinaryExpr::Add:
@ -77,7 +77,7 @@ void MCExpr::print(raw_ostream &OS) const {
return;
}
}
OS << '+';
break;
case MCBinaryExpr::And: OS << '&'; break;
@ -98,7 +98,7 @@ void MCExpr::print(raw_ostream &OS) const {
case MCBinaryExpr::Sub: OS << '-'; break;
case MCBinaryExpr::Xor: OS << '^'; break;
}
// Only print parens around the LHS if it is non-trivial.
if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
OS << *BE.getRHS();
@ -193,7 +193,7 @@ void MCTargetExpr::Anchor() {}
bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout *Layout) const {
MCValue Value;
if (!EvaluateAsRelocatable(Value, Layout) || !Value.isAbsolute())
return false;
@ -201,16 +201,16 @@ bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout *Layout) const {
return true;
}
static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A,
const MCSymbol *RHS_B, int64_t RHS_Cst,
static bool EvaluateSymbolicAdd(const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
MCValue &Res) {
// We can't add or subtract two symbols.
if ((LHS.getSymA() && RHS_A) ||
(LHS.getSymB() && RHS_B))
return false;
const MCSymbol *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
const MCSymbol *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
const MCSymbolRefExpr *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
const MCSymbolRefExpr *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
if (B) {
// If we have a negated symbol, then we must have also have a non-negated
// symbol in order to encode the expression. We can do this check later to
@ -228,13 +228,14 @@ bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
switch (getKind()) {
case Target:
return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
case Constant:
Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
return true;
case SymbolRef: {
const MCSymbol &Sym = cast<MCSymbolRefExpr>(this)->getSymbol();
const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
const MCSymbol &Sym = SRE->getSymbol();
// Evaluate recursively if this is a variable.
if (Sym.isVariable()) {
@ -245,9 +246,12 @@ bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
// layout object and the target requests it.
if (Layout && Res.getSymB() &&
Layout->getAssembler().getBackend().hasAbsolutizedSet() &&
Res.getSymA()->isDefined() && Res.getSymB()->isDefined()) {
MCSymbolData &A = Layout->getAssembler().getSymbolData(*Res.getSymA());
MCSymbolData &B = Layout->getAssembler().getSymbolData(*Res.getSymB());
Res.getSymA()->getSymbol().isDefined() &&
Res.getSymB()->getSymbol().isDefined()) {
MCSymbolData &A =
Layout->getAssembler().getSymbolData(Res.getSymA()->getSymbol());
MCSymbolData &B =
Layout->getAssembler().getSymbolData(Res.getSymB()->getSymbol());
Res = MCValue::get(+ A.getFragment()->getAddress() + A.getOffset()
- B.getFragment()->getAddress() - B.getOffset()
+ Res.getConstant());
@ -256,7 +260,7 @@ bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
return true;
}
Res = MCValue::get(&Sym, 0, 0);
Res = MCValue::get(SRE, 0, 0);
return true;
}
@ -277,13 +281,13 @@ bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
/// -(a - b + const) ==> (b - a - const)
if (Value.getSymA() && !Value.getSymB())
return false;
Res = MCValue::get(Value.getSymB(), Value.getSymA(),
-Value.getConstant());
Res = MCValue::get(Value.getSymB(), Value.getSymA(),
-Value.getConstant());
break;
case MCUnaryExpr::Not:
if (!Value.isAbsolute())
return false;
Res = MCValue::get(~Value.getConstant());
Res = MCValue::get(~Value.getConstant());
break;
case MCUnaryExpr::Plus:
Res = Value;
@ -296,7 +300,7 @@ bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
case Binary: {
const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
MCValue LHSValue, RHSValue;
if (!ABE->getLHS()->EvaluateAsRelocatable(LHSValue, Layout) ||
!ABE->getRHS()->EvaluateAsRelocatable(RHSValue, Layout))
return false;

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/MC/MCValue.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
@ -19,10 +20,12 @@ void MCValue::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
return;
}
OS << *getSymA();
getSymA()->print(OS);
if (getSymB())
OS << " - " << *getSymB();
if (getSymB()) {
OS << " - ";
getSymB()->print(OS);
}
if (getConstant())
OS << " + " << getConstant();