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

llvm-svn: 84234
This commit is contained in:
Daniel Dunbar 2009-10-16 01:58:03 +00:00
parent c3bd60e393
commit 8e6d889d4c
3 changed files with 54 additions and 46 deletions

View File

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

View File

@ -9,7 +9,10 @@
#define DEBUG_TYPE "assembler"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Target/TargetMachOWriterInfo.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallString.h"
@ -397,6 +400,7 @@ public:
};
void ComputeScatteredRelocationInfo(MCAssembler &Asm,
MCSectionData::Fixup &Fixup,
const MCValue &Target,
DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap,
std::vector<MachRelocationEntry> &Relocs) {
uint32_t Address = Fixup.Fragment->getOffset() + Fixup.Offset;
@ -404,13 +408,12 @@ public:
unsigned Type = RIT_Vanilla;
// See <reloc.h>.
const MCSymbol *A = Fixup.Value.getSymA();
const MCSymbol *A = Target.getSymA();
MCSymbolData *SD = SymbolMap.lookup(A);
uint32_t Value = SD->getFragment()->getAddress() + SD->getOffset();
uint32_t Value2 = 0;
if (const MCSymbol *B = Fixup.Value.getSymB()) {
if (const MCSymbol *B = Target.getSymB()) {
Type = RIT_LocalDifference;
MCSymbolData *SD = SymbolMap.lookup(B);
@ -421,7 +424,7 @@ public:
assert((1U << Log2Size) == Fixup.Size && "Invalid fixup size!");
// 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;
MRE.Word0 = ((Address << 0) |
@ -450,13 +453,17 @@ public:
MCSectionData::Fixup &Fixup,
DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap,
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.
if (Fixup.Value.getSymB()) // a - b
return ComputeScatteredRelocationInfo(Asm, Fixup, SymbolMap, Relocs);
if (Fixup.Value.getSymA() && Fixup.Value.getConstant())
if (!Fixup.Value.getSymA()->isUndefined())
return ComputeScatteredRelocationInfo(Asm, Fixup, SymbolMap, Relocs);
if (Target.getSymB() ||
(Target.getSymA() && !Target.getSymA()->isUndefined() &&
Target.getConstant()))
return ComputeScatteredRelocationInfo(Asm, Fixup, Target,
SymbolMap, Relocs);
// See <reloc.h>.
uint32_t Address = Fixup.Fragment->getOffset() + Fixup.Offset;
@ -466,13 +473,13 @@ public:
unsigned IsExtern = 0;
unsigned Type = 0;
if (Fixup.Value.isAbsolute()) { // constant
if (Target.isAbsolute()) { // constant
// SymbolNum of 0 indicates the absolute section.
Type = RIT_Vanilla;
Value = 0;
llvm_unreachable("FIXME: Not yet implemented!");
} else {
const MCSymbol *Symbol = Fixup.Value.getSymA();
const MCSymbol *Symbol = Target.getSymA();
MCSymbolData *SD = SymbolMap.lookup(Symbol);
if (Symbol->isUndefined()) {
@ -495,7 +502,7 @@ public:
}
// 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);
assert((1U << Log2Size) == Fixup.Size && "Invalid fixup size!");
@ -978,8 +985,12 @@ void MCAssembler::LayoutSection(MCSectionData &SD) {
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 (FF.getValue().isAbsolute())
if (Target.isAbsolute())
break;
// Otherwise, add fixups for the values.
@ -994,9 +1005,13 @@ void MCAssembler::LayoutSection(MCSectionData &SD) {
case MCFragment::FT_Org: {
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!");
uint64_t OrgOffset = OF.getOffset().getConstant();
uint64_t OrgOffset = Target.getConstant();
uint64_t Offset = Address - SD.getAddress();
// 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);
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) {
if (!FF.getValue().isAbsolute()) {
if (!Target.isAbsolute()) {
// Find the fixup.
//
// 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) {
MCValue RelocValue;
if (!AddValueSymbols(Value)->EvaluateAsRelocatable(getContext(), RelocValue))
return llvm_report_error("expected relocatable expression");
new MCFillFragment(RelocValue, Size, 1, CurSectionData);
new MCFillFragment(*AddValueSymbols(Value), Size, 1, CurSectionData);
}
void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
@ -344,12 +339,7 @@ void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
unsigned char Value) {
MCValue RelocOffset;
if (!AddValueSymbols(Offset)->EvaluateAsRelocatable(RelocOffset))
return llvm_report_error("expected relocatable expression");
new MCOrgFragment(RelocOffset, Value, CurSectionData);
new MCOrgFragment(*Offset, Value, CurSectionData);
}
void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {