//===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "assembler"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCAsmLayout.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCValue.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Debug.h"
#include "llvm/Target/TargetRegistry.h"
#include "llvm/Target/TargetAsmBackend.h"
#include <vector>
using namespace llvm;
namespace {
namespace stats {
STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
STATISTIC(EvaluateFixup, "Number of evaluated fixups");
STATISTIC(ObjectBytes, "Number of emitted object file bytes");
}
}
// FIXME FIXME FIXME: There are number of places in this file where we convert
// what is a 64-bit assembler value used for computation into a value in the
// object file, which may truncate it. We should detect that truncation where
// invalid and report errors back.
/* *** */
uint64_t MCAsmLayout::getFragmentAddress(const MCFragment *F) const {
assert(F->getParent() && "Missing section()!");
return getSectionAddress(F->getParent()) + getFragmentOffset(F);
}
uint64_t MCAsmLayout::getFragmentEffectiveSize(const MCFragment *F) const {
assert(F->EffectiveSize != ~UINT64_C(0) && "Address not set!");
return F->EffectiveSize;
}
void MCAsmLayout::setFragmentEffectiveSize(MCFragment *F, uint64_t Value) {
F->EffectiveSize = Value;
}
uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
assert(F->Offset != ~UINT64_C(0) && "Address not set!");
return F->Offset;
}
void MCAsmLayout::setFragmentOffset(MCFragment *F, uint64_t Value) {
F->Offset = Value;
}
uint64_t MCAsmLayout::getSymbolAddress(const MCSymbolData *SD) const {
assert(SD->getFragment() && "Invalid getAddress() on undefined symbol!");
return getFragmentAddress(SD->getFragment()) + SD->getOffset();
}
uint64_t MCAsmLayout::getSectionAddress(const MCSectionData *SD) const {
assert(SD->Address != ~UINT64_C(0) && "Address not set!");
return SD->Address;
}
void MCAsmLayout::setSectionAddress(MCSectionData *SD, uint64_t Value) {
SD->Address = Value;
}
uint64_t MCAsmLayout::getSectionSize(const MCSectionData *SD) const {
assert(SD->Size != ~UINT64_C(0) && "File size not set!");
return SD->Size;
}
void MCAsmLayout::setSectionSize(MCSectionData *SD, uint64_t Value) {
SD->Size = Value;
}
uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
assert(SD->FileSize != ~UINT64_C(0) && "File size not set!");
return SD->FileSize;
}
void MCAsmLayout::setSectionFileSize(MCSectionData *SD, uint64_t Value) {
SD->FileSize = Value;
}
/// @}
/* *** */
MCFragment::MCFragment() : Kind(FragmentType(~0)) {
}
MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
: Kind(_Kind),
Parent(_Parent),
EffectiveSize(~UINT64_C(0))
{
if (Parent)
Parent->getFragmentList().push_back(this);
}
MCFragment::~MCFragment() {
}
/* *** */
MCSectionData::MCSectionData() : Section(0) {}
MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
: Section(&_Section),
Alignment(1),
Address(~UINT64_C(0)),
Size(~UINT64_C(0)),
FileSize(~UINT64_C(0)),
HasInstructions(false)
{
if (A)
A->getSectionList().push_back(this);
}
/* *** */
MCSymbolData::MCSymbolData() : Symbol(0) {}
MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
uint64_t _Offset, MCAssembler *A)
: Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
IsExternal(false