1 //===-- SparcAsmBackend.cpp - Sparc Assembler Backend ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCAsmBackend.h"
11 #include "MCTargetDesc/SparcFixupKinds.h"
12 #include "MCTargetDesc/SparcMCTargetDesc.h"
13 #include "llvm/MC/MCELFObjectWriter.h"
14 #include "llvm/MC/MCFixupKindInfo.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/Support/TargetRegistry.h"
20 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
23 llvm_unreachable("Unknown fixup kind!");
29 case Sparc::fixup_sparc_call30:
30 return (Value >> 2) & 0x3fffffff;
31 case Sparc::fixup_sparc_br22:
32 return (Value >> 2) & 0x3fffff;
33 case Sparc::fixup_sparc_br19:
34 return (Value >> 2) & 0x1ffff;
35 case Sparc::fixup_sparc_hi22:
36 return (Value >> 10) & 0x3fffff;
37 case Sparc::fixup_sparc_lo10:
39 case Sparc::fixup_sparc_h44:
40 return (Value >> 22) & 0x3fffff;
41 case Sparc::fixup_sparc_m44:
42 return (Value >> 12) & 0x3ff;
43 case Sparc::fixup_sparc_l44:
45 case Sparc::fixup_sparc_hh:
46 return (Value >> 42) & 0x3fffff;
47 case Sparc::fixup_sparc_hm:
48 return (Value >> 32) & 0x3ff;
53 class SparcAsmBackend : public MCAsmBackend {
54 const Target &TheTarget;
56 SparcAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {}
58 unsigned getNumFixupKinds() const {
59 return Sparc::NumTargetFixupKinds;
62 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
63 const static MCFixupKindInfo Infos[Sparc::NumTargetFixupKinds] = {
64 // name offset bits flags
65 { "fixup_sparc_call30", 2, 30, MCFixupKindInfo::FKF_IsPCRel },
66 { "fixup_sparc_br22", 10, 22, MCFixupKindInfo::FKF_IsPCRel },
67 { "fixup_sparc_br19", 13, 19, MCFixupKindInfo::FKF_IsPCRel },
68 { "fixup_sparc_hi22", 10, 22, 0 },
69 { "fixup_sparc_lo10", 22, 10, 0 },
70 { "fixup_sparc_h44", 10, 22, 0 },
71 { "fixup_sparc_m44", 22, 10, 0 },
72 { "fixup_sparc_l44", 20, 12, 0 },
73 { "fixup_sparc_hh", 10, 22, 0 },
74 { "fixup_sparc_hm", 22, 10, 0 },
77 if (Kind < FirstTargetFixupKind)
78 return MCAsmBackend::getFixupKindInfo(Kind);
80 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
82 return Infos[Kind - FirstTargetFixupKind];
85 bool mayNeedRelaxation(const MCInst &Inst) const {
90 /// fixupNeedsRelaxation - Target specific predicate for whether a given
91 /// fixup requires the associated instruction to be relaxed.
92 bool fixupNeedsRelaxation(const MCFixup &Fixup,
94 const MCRelaxableFragment *DF,
95 const MCAsmLayout &Layout) const {
97 assert(0 && "fixupNeedsRelaxation() unimplemented");
100 void relaxInstruction(const MCInst &Inst, MCInst &Res) const {
102 assert(0 && "relaxInstruction() unimplemented");
105 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const {
106 // FIXME: Zero fill for now.
107 for (uint64_t i = 0; i != Count; ++i)
112 bool is64Bit() const {
113 StringRef name = TheTarget.getName();
114 return name == "sparcv9";
118 class ELFSparcAsmBackend : public SparcAsmBackend {
119 Triple::OSType OSType;
121 ELFSparcAsmBackend(const Target &T, Triple::OSType OSType) :
122 SparcAsmBackend(T), OSType(OSType) { }
124 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
125 uint64_t Value) const {
127 Value = adjustFixupValue(Fixup.getKind(), Value);
128 if (!Value) return; // Doesn't change encoding.
130 unsigned Offset = Fixup.getOffset();
132 // For each byte of the fragment that the fixup touches, mask in the bits
133 // from the fixup value. The Value has been "split up" into the
134 // appropriate bitfields above.
135 for (unsigned i = 0; i != 4; ++i)
136 Data[Offset + i] |= uint8_t((Value >> ((4 - i - 1)*8)) & 0xff);
140 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
141 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType);
142 return createSparcELFObjectWriter(OS, is64Bit(), OSABI);
145 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
150 } // end anonymous namespace
153 MCAsmBackend *llvm::createSparcAsmBackend(const Target &T,
154 const MCRegisterInfo &MRI,
157 return new ELFSparcAsmBackend(T, Triple(TT).getOS());