Add a MCObjectFormat class so that code common to all targets that use a
single object format can be shared.
This also adds support for
mov zed+(bar-foo), %eax
on ELF and COFF targets.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@116675 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/MC/CMakeLists.txt b/lib/MC/CMakeLists.txt
index 60a3a3e..5856e07 100644
--- a/lib/MC/CMakeLists.txt
+++ b/lib/MC/CMakeLists.txt
@@ -18,6 +18,7 @@
MCMachOStreamer.cpp
MCNullStreamer.cpp
MCObjectStreamer.cpp
+ MCObjectFormat.cpp
MCObjectWriter.cpp
MCSection.cpp
MCSectionCOFF.cpp
diff --git a/lib/MC/ELFObjectWriter.cpp b/lib/MC/ELFObjectWriter.cpp
index d249c92..5a4f89d 100644
--- a/lib/MC/ELFObjectWriter.cpp
+++ b/lib/MC/ELFObjectWriter.cpp
@@ -472,22 +472,11 @@
const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
if (BE->EvaluateAsRelocatable(Res, &Layout)) {
- uint64_t AddressA = 0;
- uint64_t AddressB = 0;
- const MCSymbol &SymA = Res.getSymA()->getSymbol();
- const MCSymbol &SymB = Res.getSymB()->getSymbol();
-
- if (SymA.isDefined()) {
- MCSymbolData &A = Layout.getAssembler().getSymbolData(SymA);
- AddressA = Layout.getSymbolAddress(&A);
- }
-
- if (SymB.isDefined()) {
- MCSymbolData &B = Layout.getAssembler().getSymbolData(SymB);
- AddressB = Layout.getSymbolAddress(&B);
- }
-
- Size = AddressA - AddressB;
+ const llvm::MCSymbolRefExpr *A = Res.getSymA();
+ const llvm::MCSymbolRefExpr *B = Res.getSymA();
+ assert(!A || !A->getSymbol().isDefined());
+ assert(!B || !B->getSymbol().isDefined());
+ Size = Res.getConstant();
}
} else if (ESize->getKind() == MCExpr::Constant) {
Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
diff --git a/lib/MC/MCExpr.cpp b/lib/MC/MCExpr.cpp
index e9a6062..3524b80 100644
--- a/lib/MC/MCExpr.cpp
+++ b/lib/MC/MCExpr.cpp
@@ -14,6 +14,7 @@
#include "llvm/MC/MCAsmLayout.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCObjectFormat.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/Debug.h"
@@ -237,7 +238,8 @@
return true;
}
-static bool EvaluateSymbolicAdd(const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
+static bool EvaluateSymbolicAdd(const MCAsmLayout *Layout, bool InSet,
+ 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.
@@ -255,12 +257,40 @@
if (!A)
return false;
}
+
+ // Absolutize symbol differences between defined symbols when we have a
+ // layout object and the target requests it.
+
+ if (Layout && A && B) {
+ const MCSymbol &SA = A->getSymbol();
+ const MCSymbol &SB = B->getSymbol();
+ const MCObjectFormat &F =
+ Layout->getAssembler().getBackend().getObjectFormat();
+ if (SA.isDefined() && SB.isDefined() && F.isAbsolute(InSet, SA, SB)) {
+ const MCAssembler &Asm = Layout->getAssembler();
+ MCSymbolData &AD = Asm.getSymbolData(A->getSymbol());
+ MCSymbolData &BD = Asm.getSymbolData(B->getSymbol());
+ Res = MCValue::get(+ Layout->getSymbolAddress(&AD)
+ - Layout->getSymbolAddress(&BD)
+ + LHS.getConstant()
+ + RHS_Cst);
+ return true;
+ }
+ }
+
+
Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
return true;
}
bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
const MCAsmLayout *Layout) const {
+ return EvaluateAsRelocatableImpl(Res, Layout, false);
+}
+
+bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
+ const MCAsmLayout *Layout,
+ bool InSet) const {
++stats::MCExprEvaluate;
switch (getKind()) {
@@ -276,27 +306,9 @@
const MCSymbol &Sym = SRE->getSymbol();
// Evaluate recursively if this is a variable.
- if (Sym.isVariable()) {
- if (!Sym.getVariableValue()->EvaluateAsRelocatable(Res, Layout))
- return false;
-
- // Absolutize symbol differences between defined symbols when we have a
- // layout object and the target requests it.
- if (Layout && Res.getSymB() &&
- Layout->getAssembler().getBackend().hasAbsolutizedSet() &&
- 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(+ Layout->getSymbolAddress(&A)
- - Layout->getSymbolAddress(&B)
- + Res.getConstant());
- }
-
- return true;
- }
+ if (Sym.isVariable())
+ return Sym.getVariableValue()->EvaluateAsRelocatableImpl(Res, Layout,
+ true);
Res = MCValue::get(SRE, 0, 0);
return true;
@@ -306,7 +318,7 @@
const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
MCValue Value;
- if (!AUE->getSubExpr()->EvaluateAsRelocatable(Value, Layout))
+ if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Layout, InSet))
return false;
switch (AUE->getOpcode()) {
@@ -339,8 +351,8 @@
const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
MCValue LHSValue, RHSValue;
- if (!ABE->getLHS()->EvaluateAsRelocatable(LHSValue, Layout) ||
- !ABE->getRHS()->EvaluateAsRelocatable(RHSValue, Layout))
+ if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Layout, InSet) ||
+ !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Layout, InSet))
return false;
// We only support a few operations on non-constant expressions, handle
@@ -351,13 +363,13 @@
return false;
case MCBinaryExpr::Sub:
// Negate RHS and add.
- return EvaluateSymbolicAdd(LHSValue,
+ return EvaluateSymbolicAdd(Layout, InSet, LHSValue,
RHSValue.getSymB(), RHSValue.getSymA(),
-RHSValue.getConstant(),
Res);
case MCBinaryExpr::Add:
- return EvaluateSymbolicAdd(LHSValue,
+ return EvaluateSymbolicAdd(Layout, InSet, LHSValue,
RHSValue.getSymA(), RHSValue.getSymB(),
RHSValue.getConstant(),
Res);
diff --git a/lib/MC/MCObjectFormat.cpp b/lib/MC/MCObjectFormat.cpp
new file mode 100644
index 0000000..07d361c
--- /dev/null
+++ b/lib/MC/MCObjectFormat.cpp
@@ -0,0 +1,31 @@
+//===- lib/MC/MCObjectFormat.cpp - MCObjectFormat implementation ----------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/MC/MCObjectFormat.h"
+#include "llvm/MC/MCSymbol.h"
+
+using namespace llvm;
+
+bool MCELFObjectFormat::isAbsolute(bool IsSet, const MCSymbol &A,
+ const MCSymbol &B) const {
+ // On ELF A - B is absolute if A and B are in the same section.
+ return &A.getSection() == &B.getSection();
+}
+
+bool MCMachOObjectFormat::isAbsolute(bool IsSet, const MCSymbol &A,
+ const MCSymbol &B) const {
+ // On MachO A - B is absolute only if in a set.
+ return IsSet;
+}
+
+bool MCCOFFObjectFormat::isAbsolute(bool IsSet, const MCSymbol &A,
+ const MCSymbol &B) const {
+ // On COFF A - B is absolute if A and B are in the same section.
+ return &A.getSection() == &B.getSection();
+}
diff --git a/lib/MC/TargetAsmBackend.cpp b/lib/MC/TargetAsmBackend.cpp
index bbfddbe..1f10410 100644
--- a/lib/MC/TargetAsmBackend.cpp
+++ b/lib/MC/TargetAsmBackend.cpp
@@ -12,7 +12,6 @@
TargetAsmBackend::TargetAsmBackend(const Target &T)
: TheTarget(T),
- HasAbsolutizedSet(false),
HasReliableSymbolDifference(false),
HasScatteredSymbols(false)
{
diff --git a/lib/Target/ARM/ARMAsmBackend.cpp b/lib/Target/ARM/ARMAsmBackend.cpp
index df439a4..d113cf5 100644
--- a/lib/Target/ARM/ARMAsmBackend.cpp
+++ b/lib/Target/ARM/ARMAsmBackend.cpp
@@ -14,6 +14,7 @@
#include "llvm/MC/ELFObjectWriter.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCExpr.h"
+#include "llvm/MC/MCObjectFormat.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCSectionMachO.h"
@@ -66,14 +67,19 @@
// FIXME: This should be in a separate file.
// ELF is an ELF of course...
class ELFARMAsmBackend : public ARMAsmBackend {
+ MCELFObjectFormat Format;
+
public:
Triple::OSType OSType;
ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
: ARMAsmBackend(T), OSType(_OSType) {
- HasAbsolutizedSet = true;
HasScatteredSymbols = true;
}
+ virtual const MCObjectFormat &getObjectFormat() const {
+ return Format;
+ }
+
void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
uint64_t Value) const;
@@ -98,14 +104,19 @@
// FIXME: This should be in a separate file.
class DarwinARMAsmBackend : public ARMAsmBackend {
+ MCMachOObjectFormat Format;
+
public:
DarwinARMAsmBackend(const Target &T)
: ARMAsmBackend(T) {
- HasAbsolutizedSet = true;
HasScatteredSymbols = true;
assert(0 && "DarwinARMAsmBackend::DarwinARMAsmBackend() unimplemented");
}
+ virtual const MCObjectFormat &getObjectFormat() const {
+ return Format;
+ }
+
void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
uint64_t Value) const;
diff --git a/lib/Target/X86/X86AsmBackend.cpp b/lib/Target/X86/X86AsmBackend.cpp
index e641500..853d3ed 100644
--- a/lib/Target/X86/X86AsmBackend.cpp
+++ b/lib/Target/X86/X86AsmBackend.cpp
@@ -14,6 +14,7 @@
#include "llvm/MC/ELFObjectWriter.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCExpr.h"
+#include "llvm/MC/MCObjectFormat.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSectionCOFF.h"
#include "llvm/MC/MCSectionELF.h"
@@ -186,15 +187,20 @@
namespace {
class ELFX86AsmBackend : public X86AsmBackend {
+ MCELFObjectFormat Format;
+
public:
Triple::OSType OSType;
ELFX86AsmBackend(const Target &T, Triple::OSType _OSType)
: X86AsmBackend(T), OSType(_OSType) {
- HasAbsolutizedSet = true;
HasScatteredSymbols = true;
HasReliableSymbolDifference = true;
}
+ virtual const MCObjectFormat &getObjectFormat() const {
+ return Format;
+ }
+
virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
const MCSectionELF &ES = static_cast<const MCSectionELF&>(Section);
return ES.getFlags() & MCSectionELF::SHF_MERGE;
@@ -242,6 +248,8 @@
class WindowsX86AsmBackend : public X86AsmBackend {
bool Is64Bit;
+ MCCOFFObjectFormat Format;
+
public:
WindowsX86AsmBackend(const Target &T, bool is64Bit)
: X86AsmBackend(T)
@@ -249,6 +257,10 @@
HasScatteredSymbols = true;
}
+ virtual const MCObjectFormat &getObjectFormat() const {
+ return Format;
+ }
+
unsigned getPointerSize() const {
if (Is64Bit)
return 8;
@@ -267,13 +279,18 @@
};
class DarwinX86AsmBackend : public X86AsmBackend {
+ MCMachOObjectFormat Format;
+
public:
DarwinX86AsmBackend(const Target &T)
: X86AsmBackend(T) {
- HasAbsolutizedSet = true;
HasScatteredSymbols = true;
}
+ virtual const MCObjectFormat &getObjectFormat() const {
+ return Format;
+ }
+
bool isVirtualSection(const MCSection &Section) const {
const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
return (SMO.getType() == MCSectionMachO::S_ZEROFILL ||