LLVM API Documentation

ARMCodeEmitter.cpp

Go to the documentation of this file.
00001 //===-- ARM/ARMCodeEmitter.cpp - Convert ARM code to machine code ---------===//
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 // This file contains the pass that transforms the ARM machine instructions into
00011 // relocatable machine code.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "jit"
00016 #include "ARM.h"
00017 #include "ARMAddressingModes.h"
00018 #include "ARMConstantPoolValue.h"
00019 #include "ARMInstrInfo.h"
00020 #include "ARMRelocations.h"
00021 #include "ARMSubtarget.h"
00022 #include "ARMTargetMachine.h"
00023 #include "llvm/Constants.h"
00024 #include "llvm/DerivedTypes.h"
00025 #include "llvm/Function.h"
00026 #include "llvm/PassManager.h"
00027 #include "llvm/CodeGen/MachineCodeEmitter.h"
00028 #include "llvm/CodeGen/MachineConstantPool.h"
00029 #include "llvm/CodeGen/MachineFunctionPass.h"
00030 #include "llvm/CodeGen/MachineInstr.h"
00031 #include "llvm/CodeGen/MachineJumpTableInfo.h"
00032 #include "llvm/CodeGen/Passes.h"
00033 #include "llvm/ADT/Statistic.h"
00034 #include "llvm/Support/Compiler.h"
00035 #include "llvm/Support/Debug.h"
00036 #ifndef NDEBUG
00037 #include <iomanip>
00038 #endif
00039 using namespace llvm;
00040 
00041 STATISTIC(NumEmitted, "Number of machine instructions emitted");
00042 
00043 namespace {
00044   class VISIBILITY_HIDDEN ARMCodeEmitter : public MachineFunctionPass {
00045     ARMJITInfo                *JTI;
00046     const ARMInstrInfo        *II;
00047     const TargetData          *TD;
00048     TargetMachine             &TM;
00049     MachineCodeEmitter        &MCE;
00050     const std::vector<MachineConstantPoolEntry> *MCPEs;
00051     const std::vector<MachineJumpTableEntry> *MJTEs;
00052     bool IsPIC;
00053 
00054   public:
00055     static char ID;
00056     explicit ARMCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce)
00057       : MachineFunctionPass(&ID), JTI(0), II(0), TD(0), TM(tm),
00058       MCE(mce), MCPEs(0), MJTEs(0),
00059       IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
00060     ARMCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce,
00061             const ARMInstrInfo &ii, const TargetData &td)
00062       : MachineFunctionPass(&ID), JTI(0), II(&ii), TD(&td), TM(tm),
00063       MCE(mce), MCPEs(0), MJTEs(0),
00064       IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
00065 
00066     bool runOnMachineFunction(MachineFunction &MF);
00067 
00068     virtual const char *getPassName() const {
00069       return "ARM Machine Code Emitter";
00070     }
00071 
00072     void emitInstruction(const MachineInstr &MI);
00073 
00074   private:
00075 
00076     void emitWordLE(unsigned Binary);
00077 
00078     void emitDWordLE(uint64_t Binary);
00079 
00080     void emitConstPoolInstruction(const MachineInstr &MI);
00081 
00082     void emitMOVi2piecesInstruction(const MachineInstr &MI);
00083 
00084     void emitLEApcrelJTInstruction(const MachineInstr &MI);
00085 
00086     void emitPseudoMoveInstruction(const MachineInstr &MI);
00087 
00088     void addPCLabel(unsigned LabelID);
00089 
00090     void emitPseudoInstruction(const MachineInstr &MI);
00091 
00092     unsigned getMachineSoRegOpValue(const MachineInstr &MI,
00093                                     const TargetInstrDesc &TID,
00094                                     const MachineOperand &MO,
00095                                     unsigned OpIdx);
00096 
00097     unsigned getMachineSoImmOpValue(unsigned SoImm);
00098 
00099     unsigned getAddrModeSBit(const MachineInstr &MI,
00100                              const TargetInstrDesc &TID) const;
00101 
00102     void emitDataProcessingInstruction(const MachineInstr &MI,
00103                                        unsigned ImplicitRd = 0,
00104                                        unsigned ImplicitRn = 0);
00105 
00106     void emitLoadStoreInstruction(const MachineInstr &MI,
00107                                   unsigned ImplicitRd = 0,
00108                                   unsigned ImplicitRn = 0);
00109 
00110     void emitMiscLoadStoreInstruction(const MachineInstr &MI,
00111                                       unsigned ImplicitRn = 0);
00112 
00113     void emitLoadStoreMultipleInstruction(const MachineInstr &MI);
00114 
00115     void emitMulFrmInstruction(const MachineInstr &MI);
00116 
00117     void emitExtendInstruction(const MachineInstr &MI);
00118 
00119     void emitMiscArithInstruction(const MachineInstr &MI);
00120 
00121     void emitBranchInstruction(const MachineInstr &MI);
00122 
00123     void emitInlineJumpTable(unsigned JTIndex);
00124 
00125     void emitMiscBranchInstruction(const MachineInstr &MI);
00126 
00127     void emitVFPArithInstruction(const MachineInstr &MI);
00128 
00129     void emitVFPConversionInstruction(const MachineInstr &MI);
00130 
00131     void emitVFPLoadStoreInstruction(const MachineInstr &MI);
00132 
00133     void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI);
00134 
00135     void emitMiscInstruction(const MachineInstr &MI);
00136 
00137     /// getBinaryCodeForInstr - This function, generated by the
00138     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
00139     /// machine instructions.
00140     ///
00141     unsigned getBinaryCodeForInstr(const MachineInstr &MI);
00142 
00143     /// getMachineOpValue - Return binary encoding of operand. If the machine
00144     /// operand requires relocation, record the relocation and return zero.
00145     unsigned getMachineOpValue(const MachineInstr &MI,const MachineOperand &MO);
00146     unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) {
00147       return getMachineOpValue(MI, MI.getOperand(OpIdx));
00148     }
00149 
00150     /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
00151     ///
00152     unsigned getShiftOp(unsigned Imm) const ;
00153 
00154     /// Routines that handle operands which add machine relocations which are
00155     /// fixed up by the relocation stage.
00156     void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
00157                            bool NeedStub, intptr_t ACPV = 0);
00158     void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
00159     void emitConstPoolAddress(unsigned CPI, unsigned Reloc);
00160     void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc);
00161     void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
00162                                intptr_t JTBase = 0);
00163   };
00164   char ARMCodeEmitter::ID = 0;
00165 }
00166 
00167 /// createARMCodeEmitterPass - Return a pass that emits the collected ARM code
00168 /// to the specified MCE object.
00169 FunctionPass *llvm::createARMCodeEmitterPass(ARMTargetMachine &TM,
00170                                              MachineCodeEmitter &MCE) {
00171   return new ARMCodeEmitter(TM, MCE);
00172 }
00173 
00174 bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
00175   assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
00176           MF.getTarget().getRelocationModel() != Reloc::Static) &&
00177          "JIT relocation model must be set to static or default!");
00178   II = ((ARMTargetMachine&)MF.getTarget()).getInstrInfo();
00179   TD = ((ARMTargetMachine&)MF.getTarget()).getTargetData();
00180   JTI = ((ARMTargetMachine&)MF.getTarget()).getJITInfo();
00181   MCPEs = &MF.getConstantPool()->getConstants();
00182   MJTEs = &MF.getJumpTableInfo()->getJumpTables();
00183   IsPIC = TM.getRelocationModel() == Reloc::PIC_;
00184   JTI->Initialize(MF, IsPIC);
00185 
00186   do {
00187     DOUT << "JITTing function '" << MF.getFunction()->getName() << "'\n";
00188     MCE.startFunction(MF);
00189     for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); 
00190          MBB != E; ++MBB) {
00191       MCE.StartMachineBasicBlock(MBB);
00192       for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
00193            I != E; ++I)
00194         emitInstruction(*I);
00195     }
00196   } while (MCE.finishFunction(MF));
00197 
00198   return false;
00199 }
00200 
00201 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
00202 ///
00203 unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const {
00204   switch (ARM_AM::getAM2ShiftOpc(Imm)) {
00205   default: assert(0 && "Unknown shift opc!");
00206   case ARM_AM::asr: return 2;
00207   case ARM_AM::lsl: return 0;
00208   case ARM_AM::lsr: return 1;
00209   case ARM_AM::ror:
00210   case ARM_AM::rrx: return 3;
00211   }
00212   return 0;
00213 }
00214 
00215 /// getMachineOpValue - Return binary encoding of operand. If the machine
00216 /// operand requires relocation, record the relocation and return zero.
00217 unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
00218                                            const MachineOperand &MO) {
00219   if (MO.isReg())
00220     return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
00221   else if (MO.isImm())
00222     return static_cast<unsigned>(MO.getImm());
00223   else if (MO.isGlobal())
00224     emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true);
00225   else if (MO.isSymbol())
00226     emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
00227   else if (MO.isCPI()) {
00228     const TargetInstrDesc &TID = MI.getDesc();
00229     // For VFP load, the immediate offset is multiplied by 4.
00230     unsigned Reloc =  ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
00231       ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
00232     emitConstPoolAddress(MO.getIndex(), Reloc);
00233   } else if (MO.isJTI())
00234     emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
00235   else if (MO.isMBB())
00236     emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
00237   else {
00238     cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
00239     abort();
00240   }
00241   return 0;
00242 }
00243 
00244 /// emitGlobalAddress - Emit the specified address to the code stream.
00245 ///
00246 void ARMCodeEmitter::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
00247                                        bool NeedStub, intptr_t ACPV) {
00248   MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
00249                                              Reloc, GV, ACPV, NeedStub));
00250 }
00251 
00252 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
00253 /// be emitted to the current location in the function, and allow it to be PC
00254 /// relative.
00255 void ARMCodeEmitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
00256   MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
00257                                                  Reloc, ES));
00258 }
00259 
00260 /// emitConstPoolAddress - Arrange for the address of an constant pool
00261 /// to be emitted to the current location in the function, and allow it to be PC
00262 /// relative.
00263 void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) {
00264   // Tell JIT emitter we'll resolve the address.
00265   MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
00266                                                     Reloc, CPI, 0, true));
00267 }
00268 
00269 /// emitJumpTableAddress - Arrange for the address of a jump table to
00270 /// be emitted to the current location in the function, and allow it to be PC
00271 /// relative.
00272 void ARMCodeEmitter::emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) {
00273   MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
00274                                                     Reloc, JTIndex, 0, true));
00275 }
00276 
00277 /// emitMachineBasicBlock - Emit the specified address basic block.
00278 void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
00279                                            unsigned Reloc, intptr_t JTBase) {
00280   MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
00281                                              Reloc, BB, JTBase));
00282 }
00283 
00284 void ARMCodeEmitter::emitWordLE(unsigned Binary) {
00285 #ifndef NDEBUG
00286   DOUT << "  0x" << std::hex << std::setw(8) << std::setfill('0')
00287        << Binary << std::dec << "\n";
00288 #endif
00289   MCE.emitWordLE(Binary);
00290 }
00291 
00292 void ARMCodeEmitter::emitDWordLE(uint64_t Binary) {
00293 #ifndef NDEBUG
00294   DOUT << "  0x" << std::hex << std::setw(8) << std::setfill('0')
00295        << (unsigned)Binary << std::dec << "\n";
00296   DOUT << "  0x" << std::hex << std::setw(8) << std::setfill('0')
00297        << (unsigned)(Binary >> 32) << std::dec << "\n";
00298 #endif
00299   MCE.emitDWordLE(Binary);
00300 }
00301 
00302 void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
00303   DOUT << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI;
00304 
00305   NumEmitted++;  // Keep track of the # of mi's emitted
00306   switch (MI.getDesc().TSFlags & ARMII::FormMask) {
00307   default: {
00308     assert(0 && "Unhandled instruction encoding format!");
00309     break;
00310   }
00311   case ARMII::Pseudo:
00312     emitPseudoInstruction(MI);
00313     break;
00314   case ARMII::DPFrm:
00315   case ARMII::DPSoRegFrm:
00316     emitDataProcessingInstruction(MI);
00317     break;
00318   case ARMII::LdFrm:
00319   case ARMII::StFrm:
00320     emitLoadStoreInstruction(MI);
00321     break;
00322   case ARMII::LdMiscFrm:
00323   case ARMII::StMiscFrm:
00324     emitMiscLoadStoreInstruction(MI);
00325     break;
00326   case ARMII::LdStMulFrm:
00327     emitLoadStoreMultipleInstruction(MI);
00328     break;
00329   case ARMII::MulFrm:
00330     emitMulFrmInstruction(MI);
00331     break;
00332   case ARMII::ExtFrm:
00333     emitExtendInstruction(MI);
00334     break;
00335   case ARMII::ArithMiscFrm:
00336     emitMiscArithInstruction(MI);
00337     break;
00338   case ARMII::BrFrm:
00339     emitBranchInstruction(MI);
00340     break;
00341   case ARMII::BrMiscFrm:
00342     emitMiscBranchInstruction(MI);
00343     break;
00344   // VFP instructions.
00345   case ARMII::VFPUnaryFrm:
00346   case ARMII::VFPBinaryFrm:
00347     emitVFPArithInstruction(MI);
00348     break;
00349   case ARMII::VFPConv1Frm:
00350   case ARMII::VFPConv2Frm:
00351   case ARMII::VFPConv3Frm:
00352   case ARMII::VFPConv4Frm:
00353   case ARMII::VFPConv5Frm:
00354     emitVFPConversionInstruction(MI);
00355     break;
00356   case ARMII::VFPLdStFrm:
00357     emitVFPLoadStoreInstruction(MI);
00358     break;
00359   case ARMII::VFPLdStMulFrm:
00360     emitVFPLoadStoreMultipleInstruction(MI);
00361     break;
00362   case ARMII::VFPMiscFrm:
00363     emitMiscInstruction(MI);
00364     break;
00365   }
00366 }
00367 
00368 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
00369   unsigned CPI = MI.getOperand(0).getImm();       // CP instruction index.
00370   unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
00371   const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
00372   
00373   // Remember the CONSTPOOL_ENTRY address for later relocation.
00374   JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
00375 
00376   // Emit constpool island entry. In most cases, the actual values will be
00377   // resolved and relocated after code emission.
00378   if (MCPE.isMachineConstantPoolEntry()) {
00379     ARMConstantPoolValue *ACPV =
00380       static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
00381 
00382     DOUT << "  ** ARM constant pool #" << CPI << " @ "
00383          << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n';
00384 
00385     GlobalValue *GV = ACPV->getGV();
00386     if (GV) {
00387       assert(!ACPV->isStub() && "Don't know how to deal this yet!");
00388       if (ACPV->isNonLazyPointer())
00389         MCE.addRelocation(MachineRelocation::getIndirectSymbol(
00390                   MCE.getCurrentPCOffset(), ARM::reloc_arm_machine_cp_entry, GV,
00391                   (intptr_t)ACPV, false));
00392       else 
00393         emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
00394                           ACPV->isStub() || isa<Function>(GV), (intptr_t)ACPV);
00395      } else  {
00396       assert(!ACPV->isNonLazyPointer() && "Don't know how to deal this yet!");
00397       emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
00398     }
00399     emitWordLE(0);
00400   } else {
00401     Constant *CV = MCPE.Val.ConstVal;
00402 
00403 #ifndef NDEBUG
00404     DOUT << "  ** Constant pool #" << CPI << " @ "
00405          << (void*)MCE.getCurrentPCValue() << " ";
00406     if (const Function *F = dyn_cast<Function>(CV))
00407       DOUT << F->getName();
00408     else
00409       DOUT << *CV;
00410     DOUT << '\n';
00411 #endif
00412 
00413     if (GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
00414       emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV));
00415       emitWordLE(0);
00416     } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
00417       uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
00418       emitWordLE(Val);
00419     } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
00420       if (CFP->getType() == Type::FloatTy)
00421         emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
00422       else if (CFP->getType() == Type::DoubleTy)
00423         emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
00424       else {
00425         assert(0 && "Unable to handle this constantpool entry!");
00426         abort();
00427       }
00428     } else {
00429       assert(0 && "Unable to handle this constantpool entry!");
00430       abort();
00431     }
00432   }
00433 }
00434 
00435 void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) {
00436   const MachineOperand &MO0 = MI.getOperand(0);
00437   const MachineOperand &MO1 = MI.getOperand(1);
00438   assert(MO1.isImm() && "Not a valid so_imm value!");
00439   unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
00440   unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
00441 
00442   // Emit the 'mov' instruction.
00443   unsigned Binary = 0xd << 21;  // mov: Insts{24-21} = 0b1101
00444 
00445   // Set the conditional execution predicate.
00446   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
00447 
00448   // Encode Rd.
00449   Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
00450 
00451   // Encode so_imm.
00452   // Set bit I(25) to identify this is the immediate form of <shifter_op>
00453   Binary |= 1 << ARMII::I_BitShift;
00454   Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V1));
00455   emitWordLE(Binary);
00456 
00457   // Now the 'orr' instruction.
00458   Binary = 0xc << 21;  // orr: Insts{24-21} = 0b1100
00459 
00460   // Set the conditional execution predicate.
00461   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
00462 
00463   // Encode Rd.
00464   Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
00465 
00466   // Encode Rn.
00467   Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
00468 
00469   // Encode so_imm.
00470   // Set bit I(25) to identify this is the immediate form of <shifter_op>
00471   Binary |= 1 << ARMII::I_BitShift;
00472   Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V2));
00473   emitWordLE(Binary);
00474 }
00475 
00476 void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) {
00477   // It's basically add r, pc, (LJTI - $+8)
00478   
00479   const TargetInstrDesc &TID = MI.getDesc();
00480 
00481   // Emit the 'add' instruction.
00482   unsigned Binary = 0x4 << 21;  // add: Insts{24-31} = 0b0100
00483 
00484   // Set the conditional execution predicate
00485   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
00486 
00487   // Encode S bit if MI modifies CPSR.
00488   Binary |= getAddrModeSBit(MI, TID);
00489 
00490   // Encode Rd.
00491   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
00492 
00493   // Encode Rn which is PC.
00494   Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
00495 
00496   // Encode the displacement.
00497   // Set bit I(25) to identify this is the immediate form of <shifter_op>.
00498   Binary |= 1 << ARMII::I_BitShift;
00499   emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
00500 
00501   emitWordLE(Binary);
00502 }
00503 
00504 void ARMCodeEmitter::emitPseudoMoveInstruction(const MachineInstr &MI) {
00505   unsigned Opcode = MI.getDesc().Opcode;
00506 
00507   // Part of binary is determined by TableGn.
00508   unsigned Binary = getBinaryCodeForInstr(MI);
00509 
00510   // Set the conditional execution predicate
00511   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
00512 
00513   // Encode S bit if MI modifies CPSR.
00514   if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag)
00515     Binary |= 1 << ARMII::S_BitShift;
00516 
00517   // Encode register def if there is one.
00518   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
00519 
00520   // Encode the shift operation.
00521   switch (Opcode) {
00522   default: break;
00523   case ARM::MOVrx:
00524     // rrx
00525     Binary |= 0x6 << 4;
00526     break;
00527   case ARM::MOVsrl_flag:
00528     // lsr #1
00529     Binary |= (0x2 << 4) | (1 << 7);
00530     break;
00531   case ARM::MOVsra_flag:
00532     // asr #1
00533     Binary |= (0x4 << 4) | (1 << 7);
00534     break;
00535   }
00536 
00537   // Encode register Rm.
00538   Binary |= getMachineOpValue(MI, 1);
00539 
00540   emitWordLE(Binary);
00541 }
00542 
00543 void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
00544   DOUT << "  ** LPC" << LabelID << " @ "
00545        << (void*)MCE.getCurrentPCValue() << '\n';
00546   JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
00547 }
00548 
00549 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
00550   unsigned Opcode = MI.getDesc().Opcode;
00551   switch (Opcode) {
00552   default:
00553     abort(); // FIXME:
00554   case TargetInstrInfo::INLINEASM: {
00555     // We allow inline assembler nodes with empty bodies - they can
00556     // implicitly define registers, which is ok for JIT.
00557     if (MI.getOperand(0).getSymbolName()[0]) {
00558       assert(0 && "JIT does not support inline asm!\n");
00559       abort();
00560     }
00561     break;
00562   }
00563   case TargetInstrInfo::DBG_LABEL:
00564   case TargetInstrInfo::EH_LABEL:
00565     MCE.emitLabel(MI.getOperand(0).getImm());
00566     break;
00567   case TargetInstrInfo::IMPLICIT_DEF:
00568   case TargetInstrInfo::DECLARE:
00569   case ARM::DWARF_LOC:
00570     // Do nothing.
00571     break;
00572   case ARM::CONSTPOOL_ENTRY:
00573     emitConstPoolInstruction(MI);
00574     break;
00575   case ARM::PICADD: {
00576     // Remember of the address of the PC label for relocation later.
00577     addPCLabel(MI.getOperand(2).getImm());
00578     // PICADD is just an add instruction that implicitly read pc.
00579     emitDataProcessingInstruction(MI, 0, ARM::PC);
00580     break;
00581   }
00582   case ARM::PICLDR:
00583   case ARM::PICLDRB:
00584   case ARM::PICSTR:
00585   case ARM::PICSTRB: {
00586     // Remember of the address of the PC label for relocation later.
00587     addPCLabel(MI.getOperand(2).getImm());
00588     // These are just load / store instructions that implicitly read pc.
00589     emitLoadStoreInstruction(MI, 0, ARM::PC);
00590     break;
00591   }
00592   case ARM::PICLDRH:
00593   case ARM::PICLDRSH:
00594   case ARM::PICLDRSB:
00595   case ARM::PICSTRH: {
00596     // Remember of the address of the PC label for relocation later.
00597     addPCLabel(MI.getOperand(2).getImm());
00598     // These are just load / store instructions that implicitly read pc.
00599     emitMiscLoadStoreInstruction(MI, ARM::PC);
00600     break;
00601   }
00602   case ARM::MOVi2pieces:
00603     // Two instructions to materialize a constant.
00604     emitMOVi2piecesInstruction(MI);
00605     break;
00606   case ARM::LEApcrelJT:
00607     // Materialize jumptable address.
00608     emitLEApcrelJTInstruction(MI);
00609     break;
00610   case ARM::MOVrx:
00611   case ARM::MOVsrl_flag:
00612   case ARM::MOVsra_flag:
00613     emitPseudoMoveInstruction(MI);
00614     break;
00615   }
00616 }
00617 
00618 
00619 unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
00620                                                 const TargetInstrDesc &TID,
00621                                                 const MachineOperand &MO,
00622                                                 unsigned OpIdx) {
00623   unsigned Binary = getMachineOpValue(MI, MO);
00624 
00625   const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
00626   const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
00627   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
00628 
00629   // Encode the shift opcode.
00630   unsigned SBits = 0;
00631   unsigned Rs = MO1.getReg();
00632   if (Rs) {
00633     // Set shift operand (bit[7:4]).
00634     // LSL - 0001
00635     // LSR - 0011
00636     // ASR - 0101
00637     // ROR - 0111
00638     // RRX - 0110 and bit[11:8] clear.
00639     switch (SOpc) {
00640     default: assert(0 && "Unknown shift opc!");
00641     case ARM_AM::lsl: SBits = 0x1; break;
00642     case ARM_AM::lsr: SBits = 0x3; break;
00643     case ARM_AM::asr: SBits = 0x5; break;
00644     case ARM_AM::ror: SBits = 0x7; break;
00645     case ARM_AM::rrx: SBits = 0x6; break;
00646     }
00647   } else {
00648     // Set shift operand (bit[6:4]).
00649     // LSL - 000
00650     // LSR - 010
00651     // ASR - 100
00652     // ROR - 110
00653     switch (SOpc) {
00654     default: assert(0 && "Unknown shift opc!");
00655     case ARM_AM::lsl: SBits = 0x0; break;
00656     case ARM_AM::lsr: SBits = 0x2; break;
00657     case ARM_AM::asr: SBits = 0x4; break;
00658     case ARM_AM::ror: SBits = 0x6; break;
00659     }
00660   }
00661   Binary |= SBits << 4;
00662   if (SOpc == ARM_AM::rrx)
00663     return Binary;
00664 
00665   // Encode the shift operation Rs or shift_imm (except rrx).
00666   if (Rs) {
00667     // Encode Rs bit[11:8].
00668     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
00669     return Binary |
00670       (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
00671   }
00672 
00673   // Encode shift_imm bit[11:7].
00674   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
00675 }
00676 
00677 unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) {
00678   // Encode rotate_imm.
00679   unsigned Binary = (ARM_AM::getSOImmValRot(SoImm) >> 1)
00680     << ARMII::SoRotImmShift;
00681 
00682   // Encode immed_8.
00683   Binary |= ARM_AM::getSOImmValImm(SoImm);
00684   return Binary;
00685 }
00686 
00687 unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
00688                                          const TargetInstrDesc &TID) const {
00689   for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
00690     const MachineOperand &MO = MI.getOperand(i-1);
00691     if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
00692       return 1 << ARMII::S_BitShift;
00693   }
00694   return 0;
00695 }
00696 
00697 void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
00698                                                    unsigned ImplicitRd,
00699                                                    unsigned ImplicitRn) {
00700   const TargetInstrDesc &TID = MI.getDesc();
00701 
00702   // Part of binary is determined by TableGn.
00703   unsigned Binary = getBinaryCodeForInstr(MI);
00704 
00705   // Set the conditional execution predicate
00706   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
00707 
00708   // Encode S bit if MI modifies CPSR.
00709   Binary |= getAddrModeSBit(MI, TID);
00710 
00711   // Encode register def if there is one.
00712   unsigned NumDefs = TID.getNumDefs();
00713   unsigned OpIdx = 0;
00714   if (NumDefs)
00715     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
00716   else if (ImplicitRd)
00717     // Special handling for implicit use (e.g. PC).
00718     Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
00719                << ARMII::RegRdShift);
00720 
00721   // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
00722   if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
00723     ++OpIdx;
00724 
00725   // Encode first non-shifter register operand if there is one.
00726   bool isUnary = TID.TSFlags & ARMII::UnaryDP;
00727   if (!isUnary) {
00728     if (ImplicitRn)
00729       // Special handling for implicit use (e.g. PC).
00730       Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
00731                  << ARMII::RegRnShift);
00732     else {
00733       Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
00734       ++OpIdx;
00735     }
00736   }
00737 
00738   // Encode shifter operand.
00739   const MachineOperand &MO = MI.getOperand(OpIdx);
00740   if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
00741     // Encode SoReg.
00742     emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
00743     return;
00744   }
00745 
00746   if (MO.isReg()) {
00747     // Encode register Rm.
00748     emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg()));
00749     return;
00750   }
00751 
00752   // Encode so_imm.
00753   // Set bit I(25) to identify this is the immediate form of <shifter_op>.
00754   Binary |= 1 << ARMII::I_BitShift;
00755   Binary |= getMachineSoImmOpValue(MO.getImm());
00756 
00757   emitWordLE(Binary);
00758 }
00759 
00760 void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
00761                                               unsigned ImplicitRd,
00762                                               unsigned ImplicitRn) {
00763   const TargetInstrDesc &TID = MI.getDesc();
00764   unsigned Form = TID.TSFlags & ARMII::FormMask;
00765   bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
00766 
00767   // Part of binary is determined by TableGn.
00768   unsigned Binary = getBinaryCodeForInstr(MI);
00769 
00770   // Set the conditional execution predicate
00771   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
00772 
00773   unsigned OpIdx = 0;
00774 
00775   // Operand 0 of a pre- and post-indexed store is the address base
00776   // writeback. Skip it.
00777   bool Skipped = false;
00778   if (IsPrePost && Form == ARMII::StFrm) {
00779     ++OpIdx;
00780     Skipped = true;
00781   }
00782 
00783   // Set first operand
00784   if (ImplicitRd)
00785     // Special handling for implicit use (e.g. PC).
00786     Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
00787                << ARMII::RegRdShift);
00788   else
00789     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
00790 
00791   // Set second operand
00792   if (ImplicitRn)
00793     // Special handling for implicit use (e.g. PC).
00794     Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
00795                << ARMII::RegRnShift);
00796   else
00797     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
00798 
00799   // If this is a two-address operand, skip it. e.g. LDR_PRE.
00800   if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
00801     ++OpIdx;
00802 
00803   const MachineOperand &MO2 = MI.getOperand(OpIdx);
00804   unsigned AM2Opc = (ImplicitRn == ARM::PC)
00805     ? 0 : MI.getOperand(OpIdx+1).getImm();
00806 
00807   // Set bit U(23) according to sign of immed value (positive or negative).
00808   Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
00809              ARMII::U_BitShift);
00810   if (!MO2.getReg()) { // is immediate
00811     if (ARM_AM::getAM2Offset(AM2Opc))
00812       // Set the value of offset_12 field
00813       Binary |= ARM_AM::getAM2Offset(AM2Opc);
00814     emitWordLE(Binary);
00815     return;
00816   }
00817 
00818   // Set bit I(25), because this is not in immediate enconding.
00819   Binary |= 1 << ARMII::I_BitShift;
00820   assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
00821   // Set bit[3:0] to the corresponding Rm register
00822   Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
00823 
00824   // If this instr is in scaled register offset/index instruction, set
00825   // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
00826   if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
00827     Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift;  // shift
00828     Binary |= ShImm              << ARMII::ShiftShift;     // shift_immed
00829   }
00830 
00831   emitWordLE(Binary);
00832 }
00833 
00834 void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
00835                                                   unsigned ImplicitRn) {
00836   const TargetInstrDesc &TID = MI.getDesc();
00837   unsigned Form = TID.TSFlags & ARMII::FormMask;
00838   bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
00839 
00840   // Part of binary is determined by TableGn.
00841   unsigned Binary = getBinaryCodeForInstr(MI);
00842 
00843   // Set the conditional execution predicate
00844   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
00845 
00846   unsigned OpIdx = 0;
00847 
00848   // Operand 0 of a pre- and post-indexed store is the address base
00849   // writeback. Skip it.
00850   bool Skipped = false;
00851   if (IsPrePost && Form == ARMII::StMiscFrm) {
00852     ++OpIdx;
00853     Skipped = true;
00854   }
00855 
00856   // Set first operand
00857   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
00858 
00859   // Set second operand
00860   if (ImplicitRn)
00861     // Special handling for implicit use (e.g. PC).
00862     Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
00863                << ARMII::RegRnShift);
00864   else
00865     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
00866 
00867   // If this is a two-address operand, skip it. e.g. LDRH_POST.
00868   if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
00869     ++OpIdx;
00870 
00871   const MachineOperand &MO2 = MI.getOperand(OpIdx);
00872   unsigned AM3Opc = (ImplicitRn == ARM::PC)
00873     ? 0 : MI.getOperand(OpIdx+1).getImm();
00874 
00875   // Set bit U(23) according to sign of immed value (positive or negative)
00876   Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
00877              ARMII::U_BitShift);
00878 
00879   // If this instr is in register offset/index encoding, set bit[3:0]
00880   // to the corresponding Rm register.
00881   if (MO2.getReg()) {
00882     Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
00883     emitWordLE(Binary);
00884     return;
00885   }
00886 
00887   // This instr is in immediate offset/index encoding, set bit 22 to 1.
00888   Binary |= 1 << ARMII::AM3_I_BitShift;
00889   if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
00890     // Set operands
00891     Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift;  // immedH
00892     Binary |= (ImmOffs & 0xF);                      // immedL
00893   }
00894 
00895   emitWordLE(Binary);
00896 }
00897 
00898 static unsigned getAddrModeUPBits(unsigned Mode) {
00899   unsigned Binary = 0;
00900 
00901   // Set addressing mode by modifying bits U(23) and P(24)
00902   // IA - Increment after  - bit U = 1 and bit P = 0
00903   // IB - Increment before - bit U = 1 and bit P = 1
00904   // DA - Decrement after  - bit U = 0 and bit P = 0
00905   // DB - Decrement before - bit U = 0 and bit P = 1
00906   switch (Mode) {
00907   default: assert(0 && "Unknown addressing sub-mode!");
00908   case ARM_AM::da:                      break;
00909   case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
00910   case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
00911   case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
00912   }
00913 
00914   return Binary;
00915 }
00916 
00917 void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
00918   // Part of binary is determined by TableGn.
00919   unsigned Binary = getBinaryCodeForInstr(MI);
00920 
00921   // Set the conditional execution predicate
00922   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
00923 
00924   // Set base address operand
00925   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
00926 
00927   // Set addressing mode by modifying bits U(23) and P(24)
00928   const MachineOperand &MO = MI.getOperand(1);
00929   Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm()));
00930 
00931   // Set bit W(21)
00932   if (ARM_AM::getAM4WBFlag(MO.getImm()))
00933     Binary |= 0x1 << ARMII::W_BitShift;
00934 
00935   // Set registers
00936   for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
00937     const MachineOperand &MO = MI.getOperand(i);
00938     if (!MO.isReg() || MO.isImplicit())
00939       break;
00940     unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
00941     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
00942            RegNum < 16);
00943     Binary |= 0x1 << RegNum;
00944   }
00945 
00946   emitWordLE(Binary);
00947 }
00948 
00949 void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
00950   const TargetInstrDesc &TID = MI.getDesc();
00951 
00952   // Part of binary is determined by TableGn.
00953   unsigned Binary = getBinaryCodeForInstr(MI);
00954 
00955   // Set the conditional execution predicate
00956   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
00957 
00958   // Encode S bit if MI modifies CPSR.
00959   Binary |= getAddrModeSBit(MI, TID);
00960 
00961   // 32x32->64bit operations have two destination registers. The number
00962   // of register definitions will tell us if that's what we're dealing with.
00963   unsigned OpIdx = 0;
00964   if (TID.getNumDefs() == 2)
00965     Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
00966 
00967   // Encode Rd
00968   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
00969 
00970   // Encode Rm
00971   Binary |= getMachineOpValue(MI, OpIdx++);
00972 
00973   // Encode Rs
00974   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
00975 
00976   // Many multiple instructions (e.g. MLA) have three src operands. Encode
00977   // it as Rn (for multiply, that's in the same offset as RdLo.
00978   if (TID.getNumOperands() > OpIdx &&
00979       !TID.OpInfo[OpIdx].isPredicate() &&
00980       !TID.OpInfo[OpIdx].isOptionalDef())
00981     Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
00982 
00983   emitWordLE(Binary);
00984 }
00985 
00986 void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) {
00987   const TargetInstrDesc &TID = MI.getDesc();
00988 
00989   // Part of binary is determined by TableGn.
00990   unsigned Binary = getBinaryCodeForInstr(MI);
00991 
00992   // Set the conditional execution predicate
00993   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
00994 
00995   unsigned OpIdx = 0;
00996 
00997   // Encode Rd
00998   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
00999 
01000   const MachineOperand &MO1 = MI.getOperand(OpIdx++);
01001   const MachineOperand &MO2 = MI.getOperand(OpIdx);
01002   if (MO2.isReg()) {
01003     // Two register operand form.
01004     // Encode Rn.
01005     Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
01006 
01007     // Encode Rm.
01008     Binary |= getMachineOpValue(MI, MO2);
01009     ++OpIdx;
01010   } else {
01011     Binary |= getMachineOpValue(MI, MO1);
01012   }
01013 
01014   // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
01015   if (MI.getOperand(OpIdx).isImm() &&
01016       !TID.OpInfo[OpIdx].isPredicate() &&
01017       !TID.OpInfo[OpIdx].isOptionalDef())
01018     Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
01019 
01020   emitWordLE(Binary);
01021 }
01022 
01023 void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) {
01024   const TargetInstrDesc &TID = MI.getDesc();
01025 
01026   // Part of binary is determined by TableGn.
01027   unsigned Binary = getBinaryCodeForInstr(MI);
01028 
01029   // Set the conditional execution predicate
01030   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
01031 
01032   unsigned OpIdx = 0;
01033 
01034   // Encode Rd
01035   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
01036 
01037   const MachineOperand &MO = MI.getOperand(OpIdx++);
01038   if (OpIdx == TID.getNumOperands() ||
01039       TID.OpInfo[OpIdx].isPredicate() ||
01040       TID.OpInfo[OpIdx].isOptionalDef()) {
01041     // Encode Rm and it's done.
01042     Binary |= getMachineOpValue(MI, MO);
01043     emitWordLE(Binary);
01044     return;
01045   }
01046 
01047   // Encode Rn.
01048   Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
01049 
01050   // Encode Rm.
01051   Binary |= getMachineOpValue(MI, OpIdx++);
01052 
01053   // Encode shift_imm.
01054   unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
01055   assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
01056   Binary |= ShiftAmt << ARMII::ShiftShift;
01057   
01058   emitWordLE(Binary);
01059 }
01060 
01061 void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
01062   const TargetInstrDesc &TID = MI.getDesc();
01063 
01064   if (TID.Opcode == ARM::TPsoft)
01065     abort(); // FIXME
01066 
01067   // Part of binary is determined by TableGn.
01068   unsigned Binary = getBinaryCodeForInstr(MI);
01069 
01070   // Set the conditional execution predicate
01071   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
01072 
01073   // Set signed_immed_24 field
01074   Binary |= getMachineOpValue(MI, 0);
01075 
01076   emitWordLE(Binary);
01077 }
01078 
01079 void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) {
01080   // Remember the base address of the inline jump table.
01081   uintptr_t JTBase = MCE.getCurrentPCValue();
01082   JTI->addJumpTableBaseAddr(JTIndex, JTBase);
01083   DOUT << "  ** Jump Table #" << JTIndex << " @ " << (void*)JTBase << '\n';
01084 
01085   // Now emit the jump table entries.
01086   const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
01087   for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
01088     if (IsPIC)
01089       // DestBB address - JT base.
01090       emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
01091     else
01092       // Absolute DestBB address.
01093       emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
01094     emitWordLE(0);
01095   }
01096 }
01097 
01098 void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
01099   const TargetInstrDesc &TID = MI.getDesc();
01100 
01101   // Handle jump tables.
01102   if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) {
01103     // First emit a ldr pc, [] instruction.
01104     emitDataProcessingInstruction(MI, ARM::PC);
01105 
01106     // Then emit the inline jump table.
01107     unsigned JTIndex = (TID.Opcode == ARM::BR_JTr)
01108       ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
01109     emitInlineJumpTable(JTIndex);
01110     return;
01111   } else if (TID.Opcode == ARM::BR_JTm) {
01112     // First emit a ldr pc, [] instruction.
01113     emitLoadStoreInstruction(MI, ARM::PC);
01114 
01115     // Then emit the inline jump table.
01116     emitInlineJumpTable(MI.getOperand(3).getIndex());
01117     return;
01118   }
01119 
01120   // Part of binary is determined by TableGn.
01121   unsigned Binary = getBinaryCodeForInstr(MI);
01122 
01123   // Set the conditional execution predicate
01124   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
01125 
01126   if (TID.Opcode == ARM::BX_RET)
01127     // The return register is LR.
01128     Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
01129   else 
01130     // otherwise, set the return register
01131     Binary |= getMachineOpValue(MI, 0);
01132 
01133   emitWordLE(Binary);
01134 }
01135 
01136 static unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) {
01137   unsigned RegD = MI.getOperand(OpIdx).getReg();
01138   unsigned Binary = 0;
01139   bool isSPVFP = false;
01140   RegD = ARMRegisterInfo::getRegisterNumbering(RegD, isSPVFP);
01141   if (!isSPVFP)
01142     Binary |=   RegD               << ARMII::RegRdShift;
01143   else {
01144     Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
01145     Binary |=  (RegD & 0x01)       << ARMII::D_BitShift;
01146   }
01147   return Binary;
01148 }
01149 
01150 static unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) {
01151   unsigned RegN = MI.getOperand(OpIdx).getReg();
01152   unsigned Binary = 0;
01153   bool isSPVFP = false;
01154   RegN = ARMRegisterInfo::getRegisterNumbering(RegN, isSPVFP);
01155   if (!isSPVFP)
01156     Binary |=   RegN               << ARMII::RegRnShift;
01157   else {
01158     Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
01159     Binary |=  (RegN & 0x01)       << ARMII::N_BitShift;
01160   }
01161   return Binary;
01162 }
01163 
01164 static unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) {
01165   unsigned RegM = MI.getOperand(OpIdx).getReg();
01166   unsigned Binary = 0;
01167   bool isSPVFP = false;
01168   RegM = ARMRegisterInfo::getRegisterNumbering(RegM, isSPVFP);
01169   if (!isSPVFP)
01170     Binary |=   RegM;
01171   else {
01172     Binary |= ((RegM & 0x1E) >> 1);
01173     Binary |=  (RegM & 0x01)       << ARMII::M_BitShift;
01174   }
01175   return Binary;
01176 }
01177 
01178 void ARMCodeEmitter::emitVFPArithInstruction(const MachineInstr &MI) {
01179   const TargetInstrDesc &TID = MI.getDesc();
01180 
01181   // Part of binary is determined by TableGn.
01182   unsigned Binary = getBinaryCodeForInstr(MI);
01183 
01184   // Set the conditional execution predicate
01185   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
01186 
01187   unsigned OpIdx = 0;
01188   assert((Binary & ARMII::D_BitShift) == 0 &&
01189          (Binary & ARMII::N_BitShift) == 0 &&
01190          (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
01191 
01192   // Encode Dd / Sd.
01193   Binary |= encodeVFPRd(MI, OpIdx++);
01194 
01195   // If this is a two-address operand, skip it, e.g. FMACD.
01196   if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
01197     ++OpIdx;
01198 
01199   // Encode Dn / Sn.
01200   if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
01201     Binary |= encodeVFPRn(MI, OpIdx++);
01202 
01203   if (OpIdx == TID.getNumOperands() ||
01204       TID.OpInfo[OpIdx].isPredicate() ||
01205       TID.OpInfo[OpIdx].isOptionalDef()) {
01206     // FCMPEZD etc. has only one operand.
01207     emitWordLE(Binary);
01208     return;
01209   }
01210 
01211   // Encode Dm / Sm.
01212   Binary |= encodeVFPRm(MI, OpIdx);
01213   
01214   emitWordLE(Binary);
01215 }
01216 
01217 void ARMCodeEmitter::emitVFPConversionInstruction(const MachineInstr &MI) {
01218   const TargetInstrDesc &TID = MI.getDesc();
01219   unsigned Form = TID.TSFlags & ARMII::FormMask;
01220 
01221   // Part of binary is determined by TableGn.
01222   unsigned Binary = getBinaryCodeForInstr(MI);
01223 
01224   // Set the conditional execution predicate
01225   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
01226 
01227   switch (Form) {
01228   default: break;
01229   case ARMII::VFPConv1Frm:
01230   case ARMII::VFPConv2Frm:
01231   case ARMII::VFPConv3Frm:
01232     // Encode Dd / Sd.
01233     Binary |= encodeVFPRd(MI, 0);
01234     break;
01235   case ARMII::VFPConv4Frm:
01236     // Encode Dn / Sn.
01237     Binary |= encodeVFPRn(MI, 0);
01238     break;
01239   case ARMII::VFPConv5Frm:
01240     // Encode Dm / Sm.
01241     Binary |= encodeVFPRm(MI, 0);
01242     break;
01243   }
01244 
01245   switch (Form) {
01246   default: break;
01247   case ARMII::VFPConv1Frm:
01248     // Encode Dm / Sm.
01249     Binary |= encodeVFPRm(MI, 1);
01250     break;
01251   case ARMII::VFPConv2Frm:
01252   case ARMII::VFPConv3Frm:
01253     // Encode Dn / Sn.
01254     Binary |= encodeVFPRn(MI, 1);
01255     break;
01256   case ARMII::VFPConv4Frm:
01257   case ARMII::VFPConv5Frm:
01258     // Encode Dd / Sd.
01259     Binary |= encodeVFPRd(MI, 1);
01260     break;
01261   }
01262 
01263   if (Form == ARMII::VFPConv5Frm)
01264     // Encode Dn / Sn.
01265     Binary |= encodeVFPRn(MI, 2);
01266   else if (Form == ARMII::VFPConv3Frm)
01267     // Encode Dm / Sm.
01268     Binary |= encodeVFPRm(MI, 2);
01269 
01270   emitWordLE(Binary);
01271 }
01272 
01273 void ARMCodeEmitter::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
01274   // Part of binary is determined by TableGn.
01275   unsigned Binary = getBinaryCodeForInstr(MI);
01276 
01277   // Set the conditional execution predicate
01278   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
01279 
01280   unsigned OpIdx = 0;
01281 
01282   // Encode Dd / Sd.
01283   Binary |= encodeVFPRd(MI, OpIdx++);
01284 
01285   // Encode address base.
01286   const MachineOperand &Base = MI.getOperand(OpIdx++);
01287   Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
01288 
01289   // If there is a non-zero immediate offset, encode it.
01290   if (Base.isReg()) {
01291     const MachineOperand &Offset = MI.getOperand(OpIdx);
01292     if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
01293       if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
01294         Binary |= 1 << ARMII::U_BitShift;
01295       Binary |= ImmOffs;
01296       emitWordLE(Binary);
01297       return;
01298     }
01299   }
01300 
01301   // If immediate offset is omitted, default to +0.
01302   Binary |= 1 << ARMII::U_BitShift;
01303 
01304   emitWordLE(Binary);
01305 }
01306 
01307 void
01308 ARMCodeEmitter::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) {
01309   // Part of binary is determined by TableGn.
01310   unsigned Binary = getBinaryCodeForInstr(MI);
01311 
01312   // Set the conditional execution predicate
01313   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
01314 
01315   // Set base address operand
01316   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
01317 
01318   // Set addressing mode by modifying bits U(23) and P(24)
01319   const MachineOperand &MO = MI.getOperand(1);
01320   Binary |= getAddrModeUPBits(ARM_AM::getAM5SubMode(MO.getImm()));
01321 
01322   // Set bit W(21)
01323   if (ARM_AM::getAM5WBFlag(MO.getImm()))
01324     Binary |= 0x1 << ARMII::W_BitShift;
01325 
01326   // First register is encoded in Dd.
01327   Binary |= encodeVFPRd(MI, 4);
01328 
01329   // Number of registers are encoded in offset field.
01330   unsigned NumRegs = 1;
01331   for (unsigned i = 5, e = MI.getNumOperands(); i != e; ++i) {
01332     const MachineOperand &MO = MI.getOperand(i);
01333     if (!MO.isReg() || MO.isImplicit())
01334       break;
01335     ++NumRegs;
01336   }
01337   Binary |= NumRegs * 2;
01338 
01339   emitWordLE(Binary);
01340 }
01341 
01342 void ARMCodeEmitter::emitMiscInstruction(const MachineInstr &MI) {
01343   // Part of binary is determined by TableGn.
01344   unsigned Binary = getBinaryCodeForInstr(MI);
01345 
01346   // Set the conditional execution predicate
01347   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
01348 
01349   emitWordLE(Binary);
01350 }
01351 
01352 #include "ARMGenCodeEmitter.inc"