LLVM API Documentation

ARMAsmBackend.cpp
Go to the documentation of this file.
00001 //===-- ARMAsmBackend.cpp - ARM 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 "MCTargetDesc/ARMMCTargetDesc.h"
00011 #include "MCTargetDesc/ARMAddressingModes.h"
00012 #include "MCTargetDesc/ARMBaseInfo.h"
00013 #include "MCTargetDesc/ARMFixupKinds.h"
00014 #include "llvm/ADT/StringSwitch.h"
00015 #include "llvm/MC/MCAsmBackend.h"
00016 #include "llvm/MC/MCAssembler.h"
00017 #include "llvm/MC/MCContext.h"
00018 #include "llvm/MC/MCDirectives.h"
00019 #include "llvm/MC/MCELFObjectWriter.h"
00020 #include "llvm/MC/MCExpr.h"
00021 #include "llvm/MC/MCFixupKindInfo.h"
00022 #include "llvm/MC/MCMachObjectWriter.h"
00023 #include "llvm/MC/MCObjectWriter.h"
00024 #include "llvm/MC/MCSectionELF.h"
00025 #include "llvm/MC/MCSectionMachO.h"
00026 #include "llvm/MC/MCSubtargetInfo.h"
00027 #include "llvm/MC/MCValue.h"
00028 #include "llvm/Object/MachOFormat.h"
00029 #include "llvm/Support/ELF.h"
00030 #include "llvm/Support/ErrorHandling.h"
00031 #include "llvm/Support/raw_ostream.h"
00032 using namespace llvm;
00033 
00034 namespace {
00035 class ARMELFObjectWriter : public MCELFObjectTargetWriter {
00036 public:
00037   ARMELFObjectWriter(uint8_t OSABI)
00038     : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI, ELF::EM_ARM,
00039                               /*HasRelocationAddend*/ false) {}
00040 };
00041 
00042 class ARMAsmBackend : public MCAsmBackend {
00043   const MCSubtargetInfo* STI;
00044   bool isThumbMode;  // Currently emitting Thumb code.
00045 public:
00046   ARMAsmBackend(const Target &T, const StringRef TT)
00047     : MCAsmBackend(), STI(ARM_MC::createARMMCSubtargetInfo(TT, "", "")),
00048       isThumbMode(TT.startswith("thumb")) {}
00049 
00050   ~ARMAsmBackend() {
00051     delete STI;
00052   }
00053 
00054   unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
00055 
00056   bool hasNOP() const {
00057     return (STI->getFeatureBits() & ARM::HasV6T2Ops) != 0;
00058   }
00059 
00060   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
00061     const static MCFixupKindInfo Infos[ARM::NumTargetFixupKinds] = {
00062 // This table *must* be in the order that the fixup_* kinds are defined in
00063 // ARMFixupKinds.h.
00064 //
00065 // Name                      Offset (bits) Size (bits)     Flags
00066 { "fixup_arm_ldst_pcrel_12", 0,            32,  MCFixupKindInfo::FKF_IsPCRel },
00067 { "fixup_t2_ldst_pcrel_12",  0,            32,  MCFixupKindInfo::FKF_IsPCRel |
00068                                    MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
00069 { "fixup_arm_pcrel_10_unscaled", 0,        32,  MCFixupKindInfo::FKF_IsPCRel },
00070 { "fixup_arm_pcrel_10",      0,            32,  MCFixupKindInfo::FKF_IsPCRel },
00071 { "fixup_t2_pcrel_10",       0,            32,  MCFixupKindInfo::FKF_IsPCRel |
00072                                    MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
00073 { "fixup_thumb_adr_pcrel_10",0,            8,   MCFixupKindInfo::FKF_IsPCRel |
00074                                    MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
00075 { "fixup_arm_adr_pcrel_12",  0,            32,  MCFixupKindInfo::FKF_IsPCRel },
00076 { "fixup_t2_adr_pcrel_12",   0,            32,  MCFixupKindInfo::FKF_IsPCRel |
00077                                    MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
00078 { "fixup_arm_condbranch",    0,            24,  MCFixupKindInfo::FKF_IsPCRel },
00079 { "fixup_arm_uncondbranch",  0,            24,  MCFixupKindInfo::FKF_IsPCRel },
00080 { "fixup_t2_condbranch",     0,            32,  MCFixupKindInfo::FKF_IsPCRel },
00081 { "fixup_t2_uncondbranch",   0,            32,  MCFixupKindInfo::FKF_IsPCRel },
00082 { "fixup_arm_thumb_br",      0,            16,  MCFixupKindInfo::FKF_IsPCRel },
00083 { "fixup_arm_uncondbl",      0,            24,  MCFixupKindInfo::FKF_IsPCRel },
00084 { "fixup_arm_condbl",        0,            24,  MCFixupKindInfo::FKF_IsPCRel },
00085 { "fixup_arm_blx",           0,            24,  MCFixupKindInfo::FKF_IsPCRel },
00086 { "fixup_arm_thumb_bl",      0,            32,  MCFixupKindInfo::FKF_IsPCRel },
00087 { "fixup_arm_thumb_blx",     0,            32,  MCFixupKindInfo::FKF_IsPCRel },
00088 { "fixup_arm_thumb_cb",      0,            16,  MCFixupKindInfo::FKF_IsPCRel },
00089 { "fixup_arm_thumb_cp",      0,             8,  MCFixupKindInfo::FKF_IsPCRel |
00090                                    MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
00091 { "fixup_arm_thumb_bcc",     0,             8,  MCFixupKindInfo::FKF_IsPCRel },
00092 // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 - 19.
00093 { "fixup_arm_movt_hi16",     0,            20,  0 },
00094 { "fixup_arm_movw_lo16",     0,            20,  0 },
00095 { "fixup_t2_movt_hi16",      0,            20,  0 },
00096 { "fixup_t2_movw_lo16",      0,            20,  0 },
00097 { "fixup_arm_movt_hi16_pcrel", 0,          20,  MCFixupKindInfo::FKF_IsPCRel },
00098 { "fixup_arm_movw_lo16_pcrel", 0,          20,  MCFixupKindInfo::FKF_IsPCRel },
00099 { "fixup_t2_movt_hi16_pcrel", 0,           20,  MCFixupKindInfo::FKF_IsPCRel },
00100 { "fixup_t2_movw_lo16_pcrel", 0,           20,  MCFixupKindInfo::FKF_IsPCRel },
00101     };
00102 
00103     if (Kind < FirstTargetFixupKind)
00104       return MCAsmBackend::getFixupKindInfo(Kind);
00105 
00106     assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
00107            "Invalid kind!");
00108     return Infos[Kind - FirstTargetFixupKind];
00109   }
00110 
00111   /// processFixupValue - Target hook to process the literal value of a fixup
00112   /// if necessary.
00113   void processFixupValue(const MCAssembler &Asm, const MCAsmLayout &Layout,
00114                          const MCFixup &Fixup, const MCFragment *DF,
00115                          MCValue &Target, uint64_t &Value,
00116                          bool &IsResolved);
00117 
00118 
00119   void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
00120                   uint64_t Value) const;
00121 
00122   bool mayNeedRelaxation(const MCInst &Inst) const;
00123 
00124   bool fixupNeedsRelaxation(const MCFixup &Fixup,
00125                             uint64_t Value,
00126                             const MCRelaxableFragment *DF,
00127                             const MCAsmLayout &Layout) const;
00128 
00129   void relaxInstruction(const MCInst &Inst, MCInst &Res) const;
00130 
00131   bool writeNopData(uint64_t Count, MCObjectWriter *OW) const;
00132 
00133   void handleAssemblerFlag(MCAssemblerFlag Flag) {
00134     switch (Flag) {
00135     default: break;
00136     case MCAF_Code16:
00137       setIsThumb(true);
00138       break;
00139     case MCAF_Code32:
00140       setIsThumb(false);
00141       break;
00142     }
00143   }
00144 
00145   unsigned getPointerSize() const { return 4; }
00146   bool isThumb() const { return isThumbMode; }
00147   void setIsThumb(bool it) { isThumbMode = it; }
00148 };
00149 } // end anonymous namespace
00150 
00151 static unsigned getRelaxedOpcode(unsigned Op) {
00152   switch (Op) {
00153   default: return Op;
00154   case ARM::tBcc:       return ARM::t2Bcc;
00155   case ARM::tLDRpciASM: return ARM::t2LDRpci;
00156   case ARM::tADR:       return ARM::t2ADR;
00157   case ARM::tB:         return ARM::t2B;
00158   }
00159 }
00160 
00161 bool ARMAsmBackend::mayNeedRelaxation(const MCInst &Inst) const {
00162   if (getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode())
00163     return true;
00164   return false;
00165 }
00166 
00167 bool ARMAsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup,
00168                                          uint64_t Value,
00169                                          const MCRelaxableFragment *DF,
00170                                          const MCAsmLayout &Layout) const {
00171   switch ((unsigned)Fixup.getKind()) {
00172   case ARM::fixup_arm_thumb_br: {
00173     // Relaxing tB to t2B. tB has a signed 12-bit displacement with the
00174     // low bit being an implied zero. There's an implied +4 offset for the
00175     // branch, so we adjust the other way here to determine what's
00176     // encodable.
00177     //
00178     // Relax if the value is too big for a (signed) i8.
00179     int64_t Offset = int64_t(Value) - 4;
00180     return Offset > 2046 || Offset < -2048;
00181   }
00182   case ARM::fixup_arm_thumb_bcc: {
00183     // Relaxing tBcc to t2Bcc. tBcc has a signed 9-bit displacement with the
00184     // low bit being an implied zero. There's an implied +4 offset for the
00185     // branch, so we adjust the other way here to determine what's
00186     // encodable.
00187     //
00188     // Relax if the value is too big for a (signed) i8.
00189     int64_t Offset = int64_t(Value) - 4;
00190     return Offset > 254 || Offset < -256;
00191   }
00192   case ARM::fixup_thumb_adr_pcrel_10:
00193   case ARM::fixup_arm_thumb_cp: {
00194     // If the immediate is negative, greater than 1020, or not a multiple
00195     // of four, the wide version of the instruction must be used.
00196     int64_t Offset = int64_t(Value) - 4;
00197     return Offset > 1020 || Offset < 0 || Offset & 3;
00198   }
00199   }
00200   llvm_unreachable("Unexpected fixup kind in fixupNeedsRelaxation()!");
00201 }
00202 
00203 void ARMAsmBackend::relaxInstruction(const MCInst &Inst, MCInst &Res) const {
00204   unsigned RelaxedOp = getRelaxedOpcode(Inst.getOpcode());
00205 
00206   // Sanity check w/ diagnostic if we get here w/ a bogus instruction.
00207   if (RelaxedOp == Inst.getOpcode()) {
00208     SmallString<256> Tmp;
00209     raw_svector_ostream OS(Tmp);
00210     Inst.dump_pretty(OS);
00211     OS << "\n";
00212     report_fatal_error("unexpected instruction to relax: " + OS.str());
00213   }
00214 
00215   // The instructions we're relaxing have (so far) the same operands.
00216   // We just need to update to the proper opcode.
00217   Res = Inst;
00218   Res.setOpcode(RelaxedOp);
00219 }
00220 
00221 bool ARMAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
00222   const uint16_t Thumb1_16bitNopEncoding = 0x46c0; // using MOV r8,r8
00223   const uint16_t Thumb2_16bitNopEncoding = 0xbf00; // NOP
00224   const uint32_t ARMv4_NopEncoding = 0xe1a00000; // using MOV r0,r0
00225   const uint32_t ARMv6T2_NopEncoding = 0xe320f000; // NOP
00226   if (isThumb()) {
00227     const uint16_t nopEncoding = hasNOP() ? Thumb2_16bitNopEncoding
00228                                           : Thumb1_16bitNopEncoding;
00229     uint64_t NumNops = Count / 2;
00230     for (uint64_t i = 0; i != NumNops; ++i)
00231       OW->Write16(nopEncoding);
00232     if (Count & 1)
00233       OW->Write8(0);
00234     return true;
00235   }
00236   // ARM mode
00237   const uint32_t nopEncoding = hasNOP() ? ARMv6T2_NopEncoding
00238                                         : ARMv4_NopEncoding;
00239   uint64_t NumNops = Count / 4;
00240   for (uint64_t i = 0; i != NumNops; ++i)
00241     OW->Write32(nopEncoding);
00242   // FIXME: should this function return false when unable to write exactly
00243   // 'Count' bytes with NOP encodings?
00244   switch (Count % 4) {
00245   default: break; // No leftover bytes to write
00246   case 1: OW->Write8(0); break;
00247   case 2: OW->Write16(0); break;
00248   case 3: OW->Write16(0); OW->Write8(0xa0); break;
00249   }
00250 
00251   return true;
00252 }
00253 
00254 static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
00255                                  MCContext *Ctx = NULL) {
00256   unsigned Kind = Fixup.getKind();
00257   switch (Kind) {
00258   default:
00259     llvm_unreachable("Unknown fixup kind!");
00260   case FK_Data_1:
00261   case FK_Data_2:
00262   case FK_Data_4:
00263     return Value;
00264   case ARM::fixup_arm_movt_hi16:
00265     Value >>= 16;
00266     // Fallthrough
00267   case ARM::fixup_arm_movw_lo16:
00268   case ARM::fixup_arm_movt_hi16_pcrel:
00269   case ARM::fixup_arm_movw_lo16_pcrel: {
00270     unsigned Hi4 = (Value & 0xF000) >> 12;
00271     unsigned Lo12 = Value & 0x0FFF;
00272     // inst{19-16} = Hi4;
00273     // inst{11-0} = Lo12;
00274     Value = (Hi4 << 16) | (Lo12);
00275     return Value;
00276   }
00277   case ARM::fixup_t2_movt_hi16:
00278     Value >>= 16;
00279     // Fallthrough
00280   case ARM::fixup_t2_movw_lo16:
00281   case ARM::fixup_t2_movt_hi16_pcrel:  //FIXME: Shouldn't this be shifted like
00282                                        // the other hi16 fixup?
00283   case ARM::fixup_t2_movw_lo16_pcrel: {
00284     unsigned Hi4 = (Value & 0xF000) >> 12;
00285     unsigned i = (Value & 0x800) >> 11;
00286     unsigned Mid3 = (Value & 0x700) >> 8;
00287     unsigned Lo8 = Value & 0x0FF;
00288     // inst{19-16} = Hi4;
00289     // inst{26} = i;
00290     // inst{14-12} = Mid3;
00291     // inst{7-0} = Lo8;
00292     Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
00293     uint64_t swapped = (Value & 0xFFFF0000) >> 16;
00294     swapped |= (Value & 0x0000FFFF) << 16;
00295     return swapped;
00296   }
00297   case ARM::fixup_arm_ldst_pcrel_12:
00298     // ARM PC-relative values are offset by 8.
00299     Value -= 4;
00300     // FALLTHROUGH
00301   case ARM::fixup_t2_ldst_pcrel_12: {
00302     // Offset by 4, adjusted by two due to the half-word ordering of thumb.
00303     Value -= 4;
00304     bool isAdd = true;
00305     if ((int64_t)Value < 0) {
00306       Value = -Value;
00307       isAdd = false;
00308     }
00309     if (Ctx && Value >= 4096)
00310       Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
00311     Value |= isAdd << 23;
00312 
00313     // Same addressing mode as fixup_arm_pcrel_10,
00314     // but with 16-bit halfwords swapped.
00315     if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
00316       uint64_t swapped = (Value & 0xFFFF0000) >> 16;
00317       swapped |= (Value & 0x0000FFFF) << 16;
00318       return swapped;
00319     }
00320 
00321     return Value;
00322   }
00323   case ARM::fixup_thumb_adr_pcrel_10:
00324     return ((Value - 4) >> 2) & 0xff;
00325   case ARM::fixup_arm_adr_pcrel_12: {
00326     // ARM PC-relative values are offset by 8.
00327     Value -= 8;
00328     unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
00329     if ((int64_t)Value < 0) {
00330       Value = -Value;
00331       opc = 2; // 0b0010
00332     }
00333     if (Ctx && ARM_AM::getSOImmVal(Value) == -1)
00334       Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
00335     // Encode the immediate and shift the opcode into place.
00336     return ARM_AM::getSOImmVal(Value) | (opc << 21);
00337   }
00338 
00339   case ARM::fixup_t2_adr_pcrel_12: {
00340     Value -= 4;
00341     unsigned opc = 0;
00342     if ((int64_t)Value < 0) {
00343       Value = -Value;
00344       opc = 5;
00345     }
00346 
00347     uint32_t out = (opc << 21);
00348     out |= (Value & 0x800) << 15;
00349     out |= (Value & 0x700) << 4;
00350     out |= (Value & 0x0FF);
00351 
00352     uint64_t swapped = (out & 0xFFFF0000) >> 16;
00353     swapped |= (out & 0x0000FFFF) << 16;
00354     return swapped;
00355   }
00356 
00357   case ARM::fixup_arm_condbranch:
00358   case ARM::fixup_arm_uncondbranch:
00359   case ARM::fixup_arm_uncondbl:
00360   case ARM::fixup_arm_condbl:
00361   case ARM::fixup_arm_blx:
00362     // These values don't encode the low two bits since they're always zero.
00363     // Offset by 8 just as above.
00364     return 0xffffff & ((Value - 8) >> 2);
00365   case ARM::fixup_t2_uncondbranch: {
00366     Value = Value - 4;
00367     Value >>= 1; // Low bit is not encoded.
00368 
00369     uint32_t out = 0;
00370     bool I =  Value & 0x800000;
00371     bool J1 = Value & 0x400000;
00372     bool J2 = Value & 0x200000;
00373     J1 ^= I;
00374     J2 ^= I;
00375 
00376     out |= I  << 26; // S bit
00377     out |= !J1 << 13; // J1 bit
00378     out |= !J2 << 11; // J2 bit
00379     out |= (Value & 0x1FF800)  << 5; // imm6 field
00380     out |= (Value & 0x0007FF);        // imm11 field
00381 
00382     uint64_t swapped = (out & 0xFFFF0000) >> 16;
00383     swapped |= (out & 0x0000FFFF) << 16;
00384     return swapped;
00385   }
00386   case ARM::fixup_t2_condbranch: {
00387     Value = Value - 4;
00388     Value >>= 1; // Low bit is not encoded.
00389 
00390     uint64_t out = 0;
00391     out |= (Value & 0x80000) << 7; // S bit
00392     out |= (Value & 0x40000) >> 7; // J2 bit
00393     out |= (Value & 0x20000) >> 4; // J1 bit
00394     out |= (Value & 0x1F800) << 5; // imm6 field
00395     out |= (Value & 0x007FF);      // imm11 field
00396 
00397     uint32_t swapped = (out & 0xFFFF0000) >> 16;
00398     swapped |= (out & 0x0000FFFF) << 16;
00399     return swapped;
00400   }
00401   case ARM::fixup_arm_thumb_bl: {
00402      // The value doesn't encode the low bit (always zero) and is offset by
00403      // four. The 32-bit immediate value is encoded as
00404      //   imm32 = SignExtend(S:I1:I2:imm10:imm11:0)
00405      // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S).
00406      // The value is encoded into disjoint bit positions in the destination
00407      // opcode. x = unchanged, I = immediate value bit, S = sign extension bit,
00408      // J = either J1 or J2 bit
00409      //
00410      //   BL:  xxxxxSIIIIIIIIII xxJxJIIIIIIIIIII
00411      //
00412      // Note that the halfwords are stored high first, low second; so we need
00413      // to transpose the fixup value here to map properly.
00414      uint32_t offset = (Value - 4) >> 1;
00415      uint32_t signBit = (offset & 0x800000) >> 23;
00416      uint32_t I1Bit = (offset & 0x400000) >> 22;
00417      uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
00418      uint32_t I2Bit = (offset & 0x200000) >> 21;
00419      uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
00420      uint32_t imm10Bits = (offset & 0x1FF800) >> 11;
00421      uint32_t imm11Bits = (offset & 0x000007FF);
00422  
00423      uint32_t Binary = 0;
00424      uint32_t firstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10Bits);
00425      uint32_t secondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) |
00426                            (uint16_t)imm11Bits);
00427      Binary |= secondHalf << 16;
00428      Binary |= firstHalf;
00429      return Binary;
00430 
00431   }
00432   case ARM::fixup_arm_thumb_blx: {
00433      // The value doesn't encode the low two bits (always zero) and is offset by
00434      // four (see fixup_arm_thumb_cp). The 32-bit immediate value is encoded as
00435      //   imm32 = SignExtend(S:I1:I2:imm10H:imm10L:00)
00436      // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S).
00437      // The value is encoded into disjoint bit positions in the destination 
00438      // opcode. x = unchanged, I = immediate value bit, S = sign extension bit, 
00439      // J = either J1 or J2 bit, 0 = zero.
00440      //
00441      //   BLX: xxxxxSIIIIIIIIII xxJxJIIIIIIIIII0
00442      //
00443      // Note that the halfwords are stored high first, low second; so we need
00444      // to transpose the fixup value here to map properly.
00445      uint32_t offset = (Value - 2) >> 2;
00446      uint32_t signBit = (offset & 0x400000) >> 22;
00447      uint32_t I1Bit = (offset & 0x200000) >> 21;
00448      uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
00449      uint32_t I2Bit = (offset & 0x100000) >> 20;
00450      uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
00451      uint32_t imm10HBits = (offset & 0xFFC00) >> 10;
00452      uint32_t imm10LBits = (offset & 0x3FF);
00453  
00454      uint32_t Binary = 0;
00455      uint32_t firstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10HBits);
00456      uint32_t secondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) | 
00457                            ((uint16_t)imm10LBits) << 1);
00458      Binary |= secondHalf << 16;
00459      Binary |= firstHalf;
00460      return Binary;
00461   }
00462   case ARM::fixup_arm_thumb_cp:
00463     // Offset by 4, and don't encode the low two bits. Two bytes of that
00464     // 'off by 4' is implicitly handled by the half-word ordering of the
00465     // Thumb encoding, so we only need to adjust by 2 here.
00466     return ((Value - 2) >> 2) & 0xff;
00467   case ARM::fixup_arm_thumb_cb: {
00468     // Offset by 4 and don't encode the lower bit, which is always 0.
00469     uint32_t Binary = (Value - 4) >> 1;
00470     return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
00471   }
00472   case ARM::fixup_arm_thumb_br:
00473     // Offset by 4 and don't encode the lower bit, which is always 0.
00474     return ((Value - 4) >> 1) & 0x7ff;
00475   case ARM::fixup_arm_thumb_bcc:
00476     // Offset by 4 and don't encode the lower bit, which is always 0.
00477     return ((Value - 4) >> 1) & 0xff;
00478   case ARM::fixup_arm_pcrel_10_unscaled: {
00479     Value = Value - 8; // ARM fixups offset by an additional word and don't
00480                        // need to adjust for the half-word ordering.
00481     bool isAdd = true;
00482     if ((int64_t)Value < 0) {
00483       Value = -Value;
00484       isAdd = false;
00485     }
00486     // The value has the low 4 bits encoded in [3:0] and the high 4 in [11:8].
00487     if (Ctx && Value >= 256)
00488       Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
00489     Value = (Value & 0xf) | ((Value & 0xf0) << 4);
00490     return Value | (isAdd << 23);
00491   }
00492   case ARM::fixup_arm_pcrel_10:
00493     Value = Value - 4; // ARM fixups offset by an additional word and don't
00494                        // need to adjust for the half-word ordering.
00495     // Fall through.
00496   case ARM::fixup_t2_pcrel_10: {
00497     // Offset by 4, adjusted by two due to the half-word ordering of thumb.
00498     Value = Value - 4;
00499     bool isAdd = true;
00500     if ((int64_t)Value < 0) {
00501       Value = -Value;
00502       isAdd = false;
00503     }
00504     // These values don't encode the low two bits since they're always zero.
00505     Value >>= 2;
00506     if (Ctx && Value >= 256)
00507       Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
00508     Value |= isAdd << 23;
00509 
00510     // Same addressing mode as fixup_arm_pcrel_10, but with 16-bit halfwords
00511     // swapped.
00512     if (Kind == ARM::fixup_t2_pcrel_10) {
00513       uint32_t swapped = (Value & 0xFFFF0000) >> 16;
00514       swapped |= (Value & 0x0000FFFF) << 16;
00515       return swapped;
00516     }
00517 
00518     return Value;
00519   }
00520   }
00521 }
00522 
00523 void ARMAsmBackend::processFixupValue(const MCAssembler &Asm,
00524                                       const MCAsmLayout &Layout,
00525                                       const MCFixup &Fixup,
00526                                       const MCFragment *DF,
00527                                       MCValue &Target, uint64_t &Value,
00528                                       bool &IsResolved) {
00529   const MCSymbolRefExpr *A = Target.getSymA();
00530   // Some fixups to thumb function symbols need the low bit (thumb bit)
00531   // twiddled.
00532   if ((unsigned)Fixup.getKind() != ARM::fixup_arm_ldst_pcrel_12 &&
00533       (unsigned)Fixup.getKind() != ARM::fixup_t2_ldst_pcrel_12 &&
00534       (unsigned)Fixup.getKind() != ARM::fixup_arm_adr_pcrel_12 &&
00535       (unsigned)Fixup.getKind() != ARM::fixup_thumb_adr_pcrel_10 &&
00536       (unsigned)Fixup.getKind() != ARM::fixup_t2_adr_pcrel_12 &&
00537       (unsigned)Fixup.getKind() != ARM::fixup_arm_thumb_cp) {
00538     if (A) {
00539       const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
00540       if (Asm.isThumbFunc(&Sym))
00541         Value |= 1;
00542     }
00543   }
00544   // We must always generate a relocation for BL/BLX instructions if we have
00545   // a symbol to reference, as the linker relies on knowing the destination
00546   // symbol's thumb-ness to get interworking right.
00547   if (A && ((unsigned)Fixup.getKind() == ARM::fixup_arm_thumb_blx ||
00548             (unsigned)Fixup.getKind() == ARM::fixup_arm_thumb_bl ||
00549             (unsigned)Fixup.getKind() == ARM::fixup_arm_blx ||
00550             (unsigned)Fixup.getKind() == ARM::fixup_arm_uncondbl ||
00551             (unsigned)Fixup.getKind() == ARM::fixup_arm_condbl))
00552     IsResolved = false;
00553 
00554   // Try to get the encoded value for the fixup as-if we're mapping it into
00555   // the instruction. This allows adjustFixupValue() to issue a diagnostic
00556   // if the value aren't invalid.
00557   (void)adjustFixupValue(Fixup, Value, &Asm.getContext());
00558 }
00559 
00560 /// getFixupKindNumBytes - The number of bytes the fixup may change.
00561 static unsigned getFixupKindNumBytes(unsigned Kind) {
00562   switch (Kind) {
00563   default:
00564     llvm_unreachable("Unknown fixup kind!");
00565 
00566   case FK_Data_1:
00567   case ARM::fixup_arm_thumb_bcc:
00568   case ARM::fixup_arm_thumb_cp:
00569   case ARM::fixup_thumb_adr_pcrel_10:
00570     return 1;
00571 
00572   case FK_Data_2:
00573   case ARM::fixup_arm_thumb_br:
00574   case ARM::fixup_arm_thumb_cb:
00575     return 2;
00576 
00577   case ARM::fixup_arm_pcrel_10_unscaled:
00578   case ARM::fixup_arm_ldst_pcrel_12:
00579   case ARM::fixup_arm_pcrel_10:
00580   case ARM::fixup_arm_adr_pcrel_12:
00581   case ARM::fixup_arm_uncondbl:
00582   case ARM::fixup_arm_condbl:
00583   case ARM::fixup_arm_blx:
00584   case ARM::fixup_arm_condbranch:
00585   case ARM::fixup_arm_uncondbranch:
00586     return 3;
00587 
00588   case FK_Data_4:
00589   case ARM::fixup_t2_ldst_pcrel_12:
00590   case ARM::fixup_t2_condbranch:
00591   case ARM::fixup_t2_uncondbranch:
00592   case ARM::fixup_t2_pcrel_10:
00593   case ARM::fixup_t2_adr_pcrel_12:
00594   case ARM::fixup_arm_thumb_bl:
00595   case ARM::fixup_arm_thumb_blx:
00596   case ARM::fixup_arm_movt_hi16:
00597   case ARM::fixup_arm_movw_lo16:
00598   case ARM::fixup_arm_movt_hi16_pcrel:
00599   case ARM::fixup_arm_movw_lo16_pcrel:
00600   case ARM::fixup_t2_movt_hi16:
00601   case ARM::fixup_t2_movw_lo16:
00602   case ARM::fixup_t2_movt_hi16_pcrel:
00603   case ARM::fixup_t2_movw_lo16_pcrel:
00604     return 4;
00605   }
00606 }
00607 
00608 void ARMAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
00609                                unsigned DataSize, uint64_t Value) const {
00610   unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
00611   Value = adjustFixupValue(Fixup, Value);
00612   if (!Value) return;           // Doesn't change encoding.
00613 
00614   unsigned Offset = Fixup.getOffset();
00615   assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
00616 
00617   // For each byte of the fragment that the fixup touches, mask in the bits from
00618   // the fixup value. The Value has been "split up" into the appropriate
00619   // bitfields above.
00620   for (unsigned i = 0; i != NumBytes; ++i)
00621     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
00622 }
00623 
00624 namespace {
00625 
00626 // FIXME: This should be in a separate file.
00627 // ELF is an ELF of course...
00628 class ELFARMAsmBackend : public ARMAsmBackend {
00629 public:
00630   uint8_t OSABI;
00631   ELFARMAsmBackend(const Target &T, const StringRef TT,
00632                    uint8_t _OSABI)
00633     : ARMAsmBackend(T, TT), OSABI(_OSABI) { }
00634 
00635   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
00636     return createARMELFObjectWriter(OS, OSABI);
00637   }
00638 };
00639 
00640 // FIXME: This should be in a separate file.
00641 class DarwinARMAsmBackend : public ARMAsmBackend {
00642 public:
00643   const object::mach::CPUSubtypeARM Subtype;
00644   DarwinARMAsmBackend(const Target &T, const StringRef TT,
00645                       object::mach::CPUSubtypeARM st)
00646     : ARMAsmBackend(T, TT), Subtype(st) {
00647       HasDataInCodeSupport = true;
00648     }
00649 
00650   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
00651     return createARMMachObjectWriter(OS, /*Is64Bit=*/false,
00652                                      object::mach::CTM_ARM,
00653                                      Subtype);
00654   }
00655 
00656   virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
00657     return false;
00658   }
00659 };
00660 
00661 } // end anonymous namespace
00662 
00663 MCAsmBackend *llvm::createARMAsmBackend(const Target &T, StringRef TT, StringRef CPU) {
00664   Triple TheTriple(TT);
00665 
00666   if (TheTriple.isOSDarwin()) {
00667     object::mach::CPUSubtypeARM CS =
00668       StringSwitch<object::mach::CPUSubtypeARM>(TheTriple.getArchName())
00669       .Cases("armv4t", "thumbv4t", object::mach::CSARM_V4T)
00670       .Cases("armv5e", "thumbv5e",object::mach::CSARM_V5TEJ)
00671       .Cases("armv6", "thumbv6", object::mach::CSARM_V6)
00672       .Cases("armv6m", "thumbv6m", object::mach::CSARM_V6M)
00673       .Cases("armv7em", "thumbv7em", object::mach::CSARM_V7EM)
00674       .Cases("armv7f", "thumbv7f", object::mach::CSARM_V7F)
00675       .Cases("armv7k", "thumbv7k", object::mach::CSARM_V7K)
00676       .Cases("armv7m", "thumbv7m", object::mach::CSARM_V7M)
00677       .Cases("armv7s", "thumbv7s", object::mach::CSARM_V7S)
00678       .Default(object::mach::CSARM_V7);
00679 
00680     return new DarwinARMAsmBackend(T, TT, CS);
00681   }
00682 
00683   if (TheTriple.isOSWindows())
00684     assert(0 && "Windows not supported on ARM");
00685 
00686   uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
00687   return new ELFARMAsmBackend(T, TT, OSABI);
00688 }