MC: Switch assembler API to using MCExpr instead of MCValue.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@84234 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar 2009-10-16 01:58:03 +00:00
parent e00b011e6a
commit 1253a6fa3b
3 changed files with 54 additions and 46 deletions

View File

@ -13,7 +13,6 @@
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
#include "llvm/ADT/ilist.h" #include "llvm/ADT/ilist.h"
#include "llvm/ADT/ilist_node.h" #include "llvm/ADT/ilist_node.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/Casting.h" #include "llvm/Support/Casting.h"
#include "llvm/Support/DataTypes.h" #include "llvm/Support/DataTypes.h"
#include <vector> // FIXME: Shouldn't be needed. #include <vector> // FIXME: Shouldn't be needed.
@ -22,8 +21,10 @@ namespace llvm {
class raw_ostream; class raw_ostream;
class MCAssembler; class MCAssembler;
class MCContext; class MCContext;
class MCExpr;
class MCSection; class MCSection;
class MCSectionData; class MCSectionData;
class MCSymbol;
class MCFragment : public ilist_node<MCFragment> { class MCFragment : public ilist_node<MCFragment> {
MCFragment(const MCFragment&); // DO NOT IMPLEMENT MCFragment(const MCFragment&); // DO NOT IMPLEMENT
@ -174,7 +175,7 @@ public:
class MCFillFragment : public MCFragment { class MCFillFragment : public MCFragment {
/// Value - Value to use for filling bytes. /// Value - Value to use for filling bytes.
MCValue Value; const MCExpr *Value;
/// ValueSize - The size (in bytes) of \arg Value to use when filling. /// ValueSize - The size (in bytes) of \arg Value to use when filling.
unsigned ValueSize; unsigned ValueSize;
@ -183,10 +184,10 @@ class MCFillFragment : public MCFragment {
uint64_t Count; uint64_t Count;
public: public:
MCFillFragment(MCValue _Value, unsigned _ValueSize, uint64_t _Count, MCFillFragment(const MCExpr &_Value, unsigned _ValueSize, uint64_t _Count,
MCSectionData *SD = 0) MCSectionData *SD = 0)
: MCFragment(FT_Fill, SD), : MCFragment(FT_Fill, SD),
Value(_Value), ValueSize(_ValueSize), Count(_Count) {} Value(&_Value), ValueSize(_ValueSize), Count(_Count) {}
/// @name Accessors /// @name Accessors
/// @{ /// @{
@ -195,7 +196,7 @@ public:
return ValueSize * Count; return ValueSize * Count;
} }
MCValue getValue() const { return Value; } const MCExpr &getValue() const { return *Value; }
unsigned getValueSize() const { return ValueSize; } unsigned getValueSize() const { return ValueSize; }
@ -211,15 +212,15 @@ public:
class MCOrgFragment : public MCFragment { class MCOrgFragment : public MCFragment {
/// Offset - The offset this fragment should start at. /// Offset - The offset this fragment should start at.
MCValue Offset; const MCExpr *Offset;
/// Value - Value to use for filling bytes. /// Value - Value to use for filling bytes.
int8_t Value; int8_t Value;
public: public:
MCOrgFragment(MCValue _Offset, int8_t _Value, MCSectionData *SD = 0) MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
: MCFragment(FT_Org, SD), : MCFragment(FT_Org, SD),
Offset(_Offset), Value(_Value) {} Offset(&_Offset), Value(_Value) {}
/// @name Accessors /// @name Accessors
/// @{ /// @{
@ -229,7 +230,7 @@ public:
return ~UINT64_C(0); return ~UINT64_C(0);
} }
MCValue getOffset() const { return Offset; } const MCExpr &getOffset() const { return *Offset; }
uint8_t getValue() const { return Value; } uint8_t getValue() const { return Value; }
@ -294,10 +295,7 @@ public:
uint64_t Offset; uint64_t Offset;
/// Value - The expression to eventually write into the fragment. /// Value - The expression to eventually write into the fragment.
// const MCExpr *Value;
// FIXME: We could probably get away with requiring the client to pass in an
// owned reference whose lifetime extends past that of the fixup.
MCValue Value;
/// Size - The fixup size. /// Size - The fixup size.
unsigned Size; unsigned Size;
@ -308,9 +306,9 @@ public:
uint64_t FixedValue; uint64_t FixedValue;
public: public:
Fixup(MCFragment &_Fragment, uint64_t _Offset, const MCValue &_Value, Fixup(MCFragment &_Fragment, uint64_t _Offset, const MCExpr &_Value,
unsigned _Size) unsigned _Size)
: Fragment(&_Fragment), Offset(_Offset), Value(_Value), Size(_Size), : Fragment(&_Fragment), Offset(_Offset), Value(&_Value), Size(_Size),
FixedValue(0) {} FixedValue(0) {}
}; };

View File

@ -9,7 +9,10 @@
#define DEBUG_TYPE "assembler" #define DEBUG_TYPE "assembler"
#include "llvm/MC/MCAssembler.h" #include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCSectionMachO.h" #include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Target/TargetMachOWriterInfo.h" #include "llvm/Target/TargetMachOWriterInfo.h"
#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
@ -397,6 +400,7 @@ public:
}; };
void ComputeScatteredRelocationInfo(MCAssembler &Asm, void ComputeScatteredRelocationInfo(MCAssembler &Asm,
MCSectionData::Fixup &Fixup, MCSectionData::Fixup &Fixup,
const MCValue &Target,
DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap, DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap,
std::vector<MachRelocationEntry> &Relocs) { std::vector<MachRelocationEntry> &Relocs) {
uint32_t Address = Fixup.Fragment->getOffset() + Fixup.Offset; uint32_t Address = Fixup.Fragment->getOffset() + Fixup.Offset;
@ -404,13 +408,12 @@ public:
unsigned Type = RIT_Vanilla; unsigned Type = RIT_Vanilla;
// See <reloc.h>. // See <reloc.h>.
const MCSymbol *A = Target.getSymA();
const MCSymbol *A = Fixup.Value.getSymA();
MCSymbolData *SD = SymbolMap.lookup(A); MCSymbolData *SD = SymbolMap.lookup(A);
uint32_t Value = SD->getFragment()->getAddress() + SD->getOffset(); uint32_t Value = SD->getFragment()->getAddress() + SD->getOffset();
uint32_t Value2 = 0; uint32_t Value2 = 0;
if (const MCSymbol *B = Fixup.Value.getSymB()) { if (const MCSymbol *B = Target.getSymB()) {
Type = RIT_LocalDifference; Type = RIT_LocalDifference;
MCSymbolData *SD = SymbolMap.lookup(B); MCSymbolData *SD = SymbolMap.lookup(B);
@ -421,7 +424,7 @@ public:
assert((1U << Log2Size) == Fixup.Size && "Invalid fixup size!"); assert((1U << Log2Size) == Fixup.Size && "Invalid fixup size!");
// The value which goes in the fixup is current value of the expression. // The value which goes in the fixup is current value of the expression.
Fixup.FixedValue = Value - Value2 + Fixup.Value.getConstant(); Fixup.FixedValue = Value - Value2 + Target.getConstant();
MachRelocationEntry MRE; MachRelocationEntry MRE;
MRE.Word0 = ((Address << 0) | MRE.Word0 = ((Address << 0) |
@ -450,13 +453,17 @@ public:
MCSectionData::Fixup &Fixup, MCSectionData::Fixup &Fixup,
DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap, DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap,
std::vector<MachRelocationEntry> &Relocs) { std::vector<MachRelocationEntry> &Relocs) {
// If this is a local symbol plus an offset or a difference, then we need a MCValue Target;
if (!Fixup.Value->EvaluateAsRelocatable(Target))
llvm_report_error("expected relocatable expression");
// If this is a difference or a local symbol plus an offset, then we need a
// scattered relocation entry. // scattered relocation entry.
if (Fixup.Value.getSymB()) // a - b if (Target.getSymB() ||
return ComputeScatteredRelocationInfo(Asm, Fixup, SymbolMap, Relocs); (Target.getSymA() && !Target.getSymA()->isUndefined() &&
if (Fixup.Value.getSymA() && Fixup.Value.getConstant()) Target.getConstant()))
if (!Fixup.Value.getSymA()->isUndefined()) return ComputeScatteredRelocationInfo(Asm, Fixup, Target,
return ComputeScatteredRelocationInfo(Asm, Fixup, SymbolMap, Relocs); SymbolMap, Relocs);
// See <reloc.h>. // See <reloc.h>.
uint32_t Address = Fixup.Fragment->getOffset() + Fixup.Offset; uint32_t Address = Fixup.Fragment->getOffset() + Fixup.Offset;
@ -466,13 +473,13 @@ public:
unsigned IsExtern = 0; unsigned IsExtern = 0;
unsigned Type = 0; unsigned Type = 0;
if (Fixup.Value.isAbsolute()) { // constant if (Target.isAbsolute()) { // constant
// SymbolNum of 0 indicates the absolute section. // SymbolNum of 0 indicates the absolute section.
Type = RIT_Vanilla; Type = RIT_Vanilla;
Value = 0; Value = 0;
llvm_unreachable("FIXME: Not yet implemented!"); llvm_unreachable("FIXME: Not yet implemented!");
} else { } else {
const MCSymbol *Symbol = Fixup.Value.getSymA(); const MCSymbol *Symbol = Target.getSymA();
MCSymbolData *SD = SymbolMap.lookup(Symbol); MCSymbolData *SD = SymbolMap.lookup(Symbol);
if (Symbol->isUndefined()) { if (Symbol->isUndefined()) {
@ -495,7 +502,7 @@ public:
} }
// The value which goes in the fixup is current value of the expression. // The value which goes in the fixup is current value of the expression.
Fixup.FixedValue = Value + Fixup.Value.getConstant(); Fixup.FixedValue = Value + Target.getConstant();
unsigned Log2Size = Log2_32(Fixup.Size); unsigned Log2Size = Log2_32(Fixup.Size);
assert((1U << Log2Size) == Fixup.Size && "Invalid fixup size!"); assert((1U << Log2Size) == Fixup.Size && "Invalid fixup size!");
@ -978,8 +985,12 @@ void MCAssembler::LayoutSection(MCSectionData &SD) {
F.setFileSize(F.getMaxFileSize()); F.setFileSize(F.getMaxFileSize());
MCValue Target;
if (!FF.getValue().EvaluateAsRelocatable(Target))
llvm_report_error("expected relocatable expression");
// If the fill value is constant, thats it. // If the fill value is constant, thats it.
if (FF.getValue().isAbsolute()) if (Target.isAbsolute())
break; break;
// Otherwise, add fixups for the values. // Otherwise, add fixups for the values.
@ -994,9 +1005,13 @@ void MCAssembler::LayoutSection(MCSectionData &SD) {
case MCFragment::FT_Org: { case MCFragment::FT_Org: {
MCOrgFragment &OF = cast<MCOrgFragment>(F); MCOrgFragment &OF = cast<MCOrgFragment>(F);
if (!OF.getOffset().isAbsolute()) MCValue Target;
if (!OF.getOffset().EvaluateAsRelocatable(Target))
llvm_report_error("expected relocatable expression");
if (!Target.isAbsolute())
llvm_unreachable("FIXME: Not yet implemented!"); llvm_unreachable("FIXME: Not yet implemented!");
uint64_t OrgOffset = OF.getOffset().getConstant(); uint64_t OrgOffset = Target.getConstant();
uint64_t Offset = Address - SD.getAddress(); uint64_t Offset = Address - SD.getAddress();
// FIXME: We need a way to communicate this error. // FIXME: We need a way to communicate this error.
@ -1077,10 +1092,15 @@ static void WriteFileData(raw_ostream &OS, const MCFragment &F,
MCFillFragment &FF = cast<MCFillFragment>(F); MCFillFragment &FF = cast<MCFillFragment>(F);
int64_t Value = 0; int64_t Value = 0;
if (FF.getValue().isAbsolute())
Value = FF.getValue().getConstant(); MCValue Target;
if (!FF.getValue().EvaluateAsRelocatable(Target))
llvm_report_error("expected relocatable expression");
if (Target.isAbsolute())
Value = Target.getConstant();
for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) { for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
if (!FF.getValue().isAbsolute()) { if (!Target.isAbsolute()) {
// Find the fixup. // Find the fixup.
// //
// FIXME: Find a better way to write in the fixes. // FIXME: Find a better way to write in the fixes.

View File

@ -321,12 +321,7 @@ void MCMachOStreamer::EmitBytes(const StringRef &Data) {
} }
void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size) { void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size) {
MCValue RelocValue; new MCFillFragment(*AddValueSymbols(Value), Size, 1, CurSectionData);
if (!AddValueSymbols(Value)->EvaluateAsRelocatable(getContext(), RelocValue))
return llvm_report_error("expected relocatable expression");
new MCFillFragment(RelocValue, Size, 1, CurSectionData);
} }
void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment, void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
@ -344,12 +339,7 @@ void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset, void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
unsigned char Value) { unsigned char Value) {
MCValue RelocOffset; new MCOrgFragment(*Offset, Value, CurSectionData);
if (!AddValueSymbols(Offset)->EvaluateAsRelocatable(RelocOffset))
return llvm_report_error("expected relocatable expression");
new MCOrgFragment(RelocOffset, Value, CurSectionData);
} }
void MCMachOStreamer::EmitInstruction(const MCInst &Inst) { void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {