LLVM API Documentation

SparcAsmBackend.cpp
Go to the documentation of this file.
00001 //===-- SparcAsmBackend.cpp - Sparc Assembler Backend ---------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/MC/MCAsmBackend.h"
00011 #include "MCTargetDesc/SparcFixupKinds.h"
00012 #include "MCTargetDesc/SparcMCTargetDesc.h"
00013 #include "llvm/MC/MCELFObjectWriter.h"
00014 #include "llvm/MC/MCExpr.h"
00015 #include "llvm/MC/MCFixupKindInfo.h"
00016 #include "llvm/MC/MCObjectWriter.h"
00017 #include "llvm/MC/MCValue.h"
00018 #include "llvm/Support/TargetRegistry.h"
00019 
00020 using namespace llvm;
00021 
00022 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
00023   switch (Kind) {
00024   default:
00025     llvm_unreachable("Unknown fixup kind!");
00026   case FK_Data_1:
00027   case FK_Data_2:
00028   case FK_Data_4:
00029   case FK_Data_8:
00030     return Value;
00031 
00032   case Sparc::fixup_sparc_wplt30:
00033   case Sparc::fixup_sparc_call30:
00034     return (Value >> 2) & 0x3fffffff;
00035 
00036   case Sparc::fixup_sparc_br22:
00037     return (Value >> 2) & 0x3fffff;
00038 
00039   case Sparc::fixup_sparc_br19:
00040     return (Value >> 2) & 0x7ffff;
00041 
00042   case Sparc::fixup_sparc_br16_2:
00043     return (Value >> 2) & 0xc000;
00044 
00045   case Sparc::fixup_sparc_br16_14:
00046     return (Value >> 2) & 0x3fff;
00047 
00048   case Sparc::fixup_sparc_pc22:
00049   case Sparc::fixup_sparc_got22:
00050   case Sparc::fixup_sparc_tls_gd_hi22:
00051   case Sparc::fixup_sparc_tls_ldm_hi22:
00052   case Sparc::fixup_sparc_tls_ie_hi22:
00053   case Sparc::fixup_sparc_hi22:
00054     return (Value >> 10) & 0x3fffff;
00055 
00056   case Sparc::fixup_sparc_pc10:
00057   case Sparc::fixup_sparc_got10:
00058   case Sparc::fixup_sparc_tls_gd_lo10:
00059   case Sparc::fixup_sparc_tls_ldm_lo10:
00060   case Sparc::fixup_sparc_tls_ie_lo10:
00061   case Sparc::fixup_sparc_lo10:
00062     return Value & 0x3ff;
00063 
00064   case Sparc::fixup_sparc_tls_ldo_hix22:
00065   case Sparc::fixup_sparc_tls_le_hix22:
00066     return (~Value >> 10) & 0x3fffff;
00067 
00068   case Sparc::fixup_sparc_tls_ldo_lox10:
00069   case Sparc::fixup_sparc_tls_le_lox10:
00070     return (~(~Value & 0x3ff)) & 0x1fff;
00071 
00072   case Sparc::fixup_sparc_h44:
00073     return (Value >> 22) & 0x3fffff;
00074 
00075   case Sparc::fixup_sparc_m44:
00076     return (Value >> 12) & 0x3ff;
00077 
00078   case Sparc::fixup_sparc_l44:
00079     return Value & 0xfff;
00080 
00081   case Sparc::fixup_sparc_hh:
00082     return (Value >> 42) & 0x3fffff;
00083 
00084   case Sparc::fixup_sparc_hm:
00085     return (Value >> 32) & 0x3ff;
00086 
00087   case Sparc::fixup_sparc_tls_gd_add:
00088   case Sparc::fixup_sparc_tls_gd_call:
00089   case Sparc::fixup_sparc_tls_ldm_add:
00090   case Sparc::fixup_sparc_tls_ldm_call:
00091   case Sparc::fixup_sparc_tls_ldo_add:
00092   case Sparc::fixup_sparc_tls_ie_ld:
00093   case Sparc::fixup_sparc_tls_ie_ldx:
00094   case Sparc::fixup_sparc_tls_ie_add:
00095     return 0;
00096   }
00097 }
00098 
00099 namespace {
00100   class SparcAsmBackend : public MCAsmBackend {
00101     const Target &TheTarget;
00102   public:
00103     SparcAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {}
00104 
00105     unsigned getNumFixupKinds() const override {
00106       return Sparc::NumTargetFixupKinds;
00107     }
00108 
00109     const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
00110       const static MCFixupKindInfo Infos[Sparc::NumTargetFixupKinds] = {
00111         // name                    offset bits  flags
00112         { "fixup_sparc_call30",     2,     30,  MCFixupKindInfo::FKF_IsPCRel },
00113         { "fixup_sparc_br22",      10,     22,  MCFixupKindInfo::FKF_IsPCRel },
00114         { "fixup_sparc_br19",      13,     19,  MCFixupKindInfo::FKF_IsPCRel },
00115         { "fixup_sparc_br16_2",    10,      2,  MCFixupKindInfo::FKF_IsPCRel },
00116         { "fixup_sparc_br16_14",   18,     14,  MCFixupKindInfo::FKF_IsPCRel },
00117         { "fixup_sparc_hi22",      10,     22,  0 },
00118         { "fixup_sparc_lo10",      22,     10,  0 },
00119         { "fixup_sparc_h44",       10,     22,  0 },
00120         { "fixup_sparc_m44",       22,     10,  0 },
00121         { "fixup_sparc_l44",       20,     12,  0 },
00122         { "fixup_sparc_hh",        10,     22,  0 },
00123         { "fixup_sparc_hm",        22,     10,  0 },
00124         { "fixup_sparc_pc22",      10,     22,  MCFixupKindInfo::FKF_IsPCRel },
00125         { "fixup_sparc_pc10",      22,     10,  MCFixupKindInfo::FKF_IsPCRel },
00126         { "fixup_sparc_got22",     10,     22,  0 },
00127         { "fixup_sparc_got10",     22,     10,  0 },
00128         { "fixup_sparc_wplt30",     2,     30,  MCFixupKindInfo::FKF_IsPCRel },
00129         { "fixup_sparc_tls_gd_hi22",   10, 22,  0 },
00130         { "fixup_sparc_tls_gd_lo10",   22, 10,  0 },
00131         { "fixup_sparc_tls_gd_add",     0,  0,  0 },
00132         { "fixup_sparc_tls_gd_call",    0,  0,  0 },
00133         { "fixup_sparc_tls_ldm_hi22",  10, 22,  0 },
00134         { "fixup_sparc_tls_ldm_lo10",  22, 10,  0 },
00135         { "fixup_sparc_tls_ldm_add",    0,  0,  0 },
00136         { "fixup_sparc_tls_ldm_call",   0,  0,  0 },
00137         { "fixup_sparc_tls_ldo_hix22", 10, 22,  0 },
00138         { "fixup_sparc_tls_ldo_lox10", 22, 10,  0 },
00139         { "fixup_sparc_tls_ldo_add",    0,  0,  0 },
00140         { "fixup_sparc_tls_ie_hi22",   10, 22,  0 },
00141         { "fixup_sparc_tls_ie_lo10",   22, 10,  0 },
00142         { "fixup_sparc_tls_ie_ld",      0,  0,  0 },
00143         { "fixup_sparc_tls_ie_ldx",     0,  0,  0 },
00144         { "fixup_sparc_tls_ie_add",     0,  0,  0 },
00145         { "fixup_sparc_tls_le_hix22",   0,  0,  0 },
00146         { "fixup_sparc_tls_le_lox10",   0,  0,  0 }
00147       };
00148 
00149       if (Kind < FirstTargetFixupKind)
00150         return MCAsmBackend::getFixupKindInfo(Kind);
00151 
00152       assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
00153              "Invalid kind!");
00154       return Infos[Kind - FirstTargetFixupKind];
00155     }
00156 
00157     void processFixupValue(const MCAssembler &Asm, const MCAsmLayout &Layout,
00158                            const MCFixup &Fixup, const MCFragment *DF,
00159                            const MCValue &Target, uint64_t &Value,
00160                            bool &IsResolved) override {
00161       switch ((Sparc::Fixups)Fixup.getKind()) {
00162       default: break;
00163       case Sparc::fixup_sparc_wplt30:
00164         if (Target.getSymA()->getSymbol().isTemporary())
00165           return;
00166       case Sparc::fixup_sparc_tls_gd_hi22:
00167       case Sparc::fixup_sparc_tls_gd_lo10:
00168       case Sparc::fixup_sparc_tls_gd_add:
00169       case Sparc::fixup_sparc_tls_gd_call:
00170       case Sparc::fixup_sparc_tls_ldm_hi22:
00171       case Sparc::fixup_sparc_tls_ldm_lo10:
00172       case Sparc::fixup_sparc_tls_ldm_add:
00173       case Sparc::fixup_sparc_tls_ldm_call:
00174       case Sparc::fixup_sparc_tls_ldo_hix22:
00175       case Sparc::fixup_sparc_tls_ldo_lox10:
00176       case Sparc::fixup_sparc_tls_ldo_add:
00177       case Sparc::fixup_sparc_tls_ie_hi22:
00178       case Sparc::fixup_sparc_tls_ie_lo10:
00179       case Sparc::fixup_sparc_tls_ie_ld:
00180       case Sparc::fixup_sparc_tls_ie_ldx:
00181       case Sparc::fixup_sparc_tls_ie_add:
00182       case Sparc::fixup_sparc_tls_le_hix22:
00183       case Sparc::fixup_sparc_tls_le_lox10:  IsResolved = false; break;
00184       }
00185     }
00186 
00187     bool mayNeedRelaxation(const MCInst &Inst) const override {
00188       // FIXME.
00189       return false;
00190     }
00191 
00192     /// fixupNeedsRelaxation - Target specific predicate for whether a given
00193     /// fixup requires the associated instruction to be relaxed.
00194     bool fixupNeedsRelaxation(const MCFixup &Fixup,
00195                               uint64_t Value,
00196                               const MCRelaxableFragment *DF,
00197                               const MCAsmLayout &Layout) const override {
00198       // FIXME.
00199       llvm_unreachable("fixupNeedsRelaxation() unimplemented");
00200       return false;
00201     }
00202     void relaxInstruction(const MCInst &Inst, MCInst &Res) const override {
00203       // FIXME.
00204       llvm_unreachable("relaxInstruction() unimplemented");
00205     }
00206 
00207     bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override {
00208       // Cannot emit NOP with size not multiple of 32 bits.
00209       if (Count % 4 != 0)
00210         return false;
00211 
00212       uint64_t NumNops = Count / 4;
00213       for (uint64_t i = 0; i != NumNops; ++i)
00214         OW->Write32(0x01000000);
00215 
00216       return true;
00217     }
00218 
00219     bool is64Bit() const {
00220       StringRef name = TheTarget.getName();
00221       return name == "sparcv9";
00222     }
00223   };
00224 
00225   class ELFSparcAsmBackend : public SparcAsmBackend {
00226     Triple::OSType OSType;
00227   public:
00228     ELFSparcAsmBackend(const Target &T, Triple::OSType OSType) :
00229       SparcAsmBackend(T), OSType(OSType) { }
00230 
00231     void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
00232                     uint64_t Value, bool IsPCRel) const override {
00233 
00234       Value = adjustFixupValue(Fixup.getKind(), Value);
00235       if (!Value) return;           // Doesn't change encoding.
00236 
00237       unsigned Offset = Fixup.getOffset();
00238 
00239       // For each byte of the fragment that the fixup touches, mask in the bits
00240       // from the fixup value. The Value has been "split up" into the
00241       // appropriate bitfields above.
00242       for (unsigned i = 0; i != 4; ++i)
00243         Data[Offset + i] |= uint8_t((Value >> ((4 - i - 1)*8)) & 0xff);
00244 
00245     }
00246 
00247     MCObjectWriter *createObjectWriter(raw_ostream &OS) const override {
00248       uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType);
00249       return createSparcELFObjectWriter(OS, is64Bit(), OSABI);
00250     }
00251   };
00252 
00253 } // end anonymous namespace
00254 
00255 
00256 MCAsmBackend *llvm::createSparcAsmBackend(const Target &T,
00257                                           const MCRegisterInfo &MRI,
00258                                           StringRef TT,
00259                                           StringRef CPU) {
00260   return new ELFSparcAsmBackend(T, Triple(TT).getOS());
00261 }