Fix encoding of single-precision VFP registers.
[oota-llvm.git] / lib / Target / ARM / ARMCodeEmitter.cpp
1 //===-- ARM/ARMCodeEmitter.cpp - Convert ARM code to machine code ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the pass that transforms the ARM machine instructions into
11 // relocatable machine code.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "jit"
16 #include "ARM.h"
17 #include "ARMAddressingModes.h"
18 #include "ARMConstantPoolValue.h"
19 #include "ARMInstrInfo.h"
20 #include "ARMRelocations.h"
21 #include "ARMSubtarget.h"
22 #include "ARMTargetMachine.h"
23 #include "llvm/Constants.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/Function.h"
26 #include "llvm/PassManager.h"
27 #include "llvm/CodeGen/MachineCodeEmitter.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineFunctionPass.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/MachineJumpTableInfo.h"
32 #include "llvm/CodeGen/Passes.h"
33 #include "llvm/ADT/Statistic.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/Support/Debug.h"
36 #ifndef NDEBUG
37 #include <iomanip>
38 #endif
39 using namespace llvm;
40
41 STATISTIC(NumEmitted, "Number of machine instructions emitted");
42
43 namespace {
44   class VISIBILITY_HIDDEN ARMCodeEmitter : public MachineFunctionPass {
45     ARMJITInfo                *JTI;
46     const ARMInstrInfo        *II;
47     const TargetData          *TD;
48     TargetMachine             &TM;
49     MachineCodeEmitter        &MCE;
50     const std::vector<MachineConstantPoolEntry> *MCPEs;
51     const std::vector<MachineJumpTableEntry> *MJTEs;
52     bool IsPIC;
53
54   public:
55     static char ID;
56     explicit ARMCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce)
57       : MachineFunctionPass(&ID), JTI(0), II(0), TD(0), TM(tm),
58       MCE(mce), MCPEs(0), MJTEs(0),
59       IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
60     ARMCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce,
61             const ARMInstrInfo &ii, const TargetData &td)
62       : MachineFunctionPass(&ID), JTI(0), II(&ii), TD(&td), TM(tm),
63       MCE(mce), MCPEs(0), MJTEs(0),
64       IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
65
66     bool runOnMachineFunction(MachineFunction &MF);
67
68     virtual const char *getPassName() const {
69       return "ARM Machine Code Emitter";
70     }
71
72     void emitInstruction(const MachineInstr &MI);
73
74   private:
75
76     void emitWordLE(unsigned Binary);
77
78     void emitDWordLE(uint64_t Binary);
79
80     void emitConstPoolInstruction(const MachineInstr &MI);
81
82     void emitMOVi2piecesInstruction(const MachineInstr &MI);
83
84     void emitLEApcrelJTInstruction(const MachineInstr &MI);
85
86     void addPCLabel(unsigned LabelID);
87
88     void emitPseudoInstruction(const MachineInstr &MI);
89
90     unsigned getMachineSoRegOpValue(const MachineInstr &MI,
91                                     const TargetInstrDesc &TID,
92                                     const MachineOperand &MO,
93                                     unsigned OpIdx);
94
95     unsigned getMachineSoImmOpValue(unsigned SoImm);
96
97     unsigned getAddrModeSBit(const MachineInstr &MI,
98                              const TargetInstrDesc &TID) const;
99
100     void emitDataProcessingInstruction(const MachineInstr &MI,
101                                        unsigned ImplicitRd = 0,
102                                        unsigned ImplicitRn = 0);
103
104     void emitLoadStoreInstruction(const MachineInstr &MI,
105                                   unsigned ImplicitRd = 0,
106                                   unsigned ImplicitRn = 0);
107
108     void emitMiscLoadStoreInstruction(const MachineInstr &MI,
109                                       unsigned ImplicitRn = 0);
110
111     void emitLoadStoreMultipleInstruction(const MachineInstr &MI);
112
113     void emitMulFrmInstruction(const MachineInstr &MI);
114
115     void emitExtendInstruction(const MachineInstr &MI);
116
117     void emitMiscArithInstruction(const MachineInstr &MI);
118
119     void emitBranchInstruction(const MachineInstr &MI);
120
121     void emitInlineJumpTable(unsigned JTIndex);
122
123     void emitMiscBranchInstruction(const MachineInstr &MI);
124
125     void emitVFPArithInstruction(const MachineInstr &MI);
126
127     void emitVFPConversionInstruction(const MachineInstr &MI);
128
129     void emitVFPLoadStoreInstruction(const MachineInstr &MI);
130
131     void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI);
132
133     void emitMiscInstruction(const MachineInstr &MI);
134
135     /// getBinaryCodeForInstr - This function, generated by the
136     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
137     /// machine instructions.
138     ///
139     unsigned getBinaryCodeForInstr(const MachineInstr &MI);
140
141     /// getMachineOpValue - Return binary encoding of operand. If the machine
142     /// operand requires relocation, record the relocation and return zero.
143     unsigned getMachineOpValue(const MachineInstr &MI,const MachineOperand &MO);
144     unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) {
145       return getMachineOpValue(MI, MI.getOperand(OpIdx));
146     }
147
148     /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
149     ///
150     unsigned getShiftOp(unsigned Imm) const ;
151
152     /// Routines that handle operands which add machine relocations which are
153     /// fixed up by the relocation stage.
154     void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
155                            bool NeedStub, intptr_t ACPV = 0);
156     void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
157     void emitConstPoolAddress(unsigned CPI, unsigned Reloc);
158     void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc);
159     void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
160                                intptr_t JTBase = 0);
161   };
162   char ARMCodeEmitter::ID = 0;
163 }
164
165 /// createARMCodeEmitterPass - Return a pass that emits the collected ARM code
166 /// to the specified MCE object.
167 FunctionPass *llvm::createARMCodeEmitterPass(ARMTargetMachine &TM,
168                                              MachineCodeEmitter &MCE) {
169   return new ARMCodeEmitter(TM, MCE);
170 }
171
172 bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
173   assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
174           MF.getTarget().getRelocationModel() != Reloc::Static) &&
175          "JIT relocation model must be set to static or default!");
176   II = ((ARMTargetMachine&)MF.getTarget()).getInstrInfo();
177   TD = ((ARMTargetMachine&)MF.getTarget()).getTargetData();
178   JTI = ((ARMTargetMachine&)MF.getTarget()).getJITInfo();
179   MCPEs = &MF.getConstantPool()->getConstants();
180   MJTEs = &MF.getJumpTableInfo()->getJumpTables();
181   IsPIC = TM.getRelocationModel() == Reloc::PIC_;
182   JTI->Initialize(MF, IsPIC);
183
184   do {
185     DOUT << "JITTing function '" << MF.getFunction()->getName() << "'\n";
186     MCE.startFunction(MF);
187     for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); 
188          MBB != E; ++MBB) {
189       MCE.StartMachineBasicBlock(MBB);
190       for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
191            I != E; ++I)
192         emitInstruction(*I);
193     }
194   } while (MCE.finishFunction(MF));
195
196   return false;
197 }
198
199 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
200 ///
201 unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const {
202   switch (ARM_AM::getAM2ShiftOpc(Imm)) {
203   default: assert(0 && "Unknown shift opc!");
204   case ARM_AM::asr: return 2;
205   case ARM_AM::lsl: return 0;
206   case ARM_AM::lsr: return 1;
207   case ARM_AM::ror:
208   case ARM_AM::rrx: return 3;
209   }
210   return 0;
211 }
212
213 /// getMachineOpValue - Return binary encoding of operand. If the machine
214 /// operand requires relocation, record the relocation and return zero.
215 unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
216                                            const MachineOperand &MO) {
217   if (MO.isReg())
218     return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
219   else if (MO.isImm())
220     return static_cast<unsigned>(MO.getImm());
221   else if (MO.isGlobal())
222     emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true);
223   else if (MO.isSymbol())
224     emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
225   else if (MO.isCPI()) {
226     const TargetInstrDesc &TID = MI.getDesc();
227     // For VFP load, the immediate offset is multiplied by 4.
228     unsigned Reloc =  ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
229       ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
230     emitConstPoolAddress(MO.getIndex(), Reloc);
231   } else if (MO.isJTI())
232     emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
233   else if (MO.isMBB())
234     emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
235   else {
236     cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
237     abort();
238   }
239   return 0;
240 }
241
242 /// emitGlobalAddress - Emit the specified address to the code stream.
243 ///
244 void ARMCodeEmitter::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
245                                        bool NeedStub, intptr_t ACPV) {
246   MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
247                                              Reloc, GV, ACPV, NeedStub));
248 }
249
250 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
251 /// be emitted to the current location in the function, and allow it to be PC
252 /// relative.
253 void ARMCodeEmitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
254   MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
255                                                  Reloc, ES));
256 }
257
258 /// emitConstPoolAddress - Arrange for the address of an constant pool
259 /// to be emitted to the current location in the function, and allow it to be PC
260 /// relative.
261 void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) {
262   // Tell JIT emitter we'll resolve the address.
263   MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
264                                                     Reloc, CPI, 0, true));
265 }
266
267 /// emitJumpTableAddress - Arrange for the address of a jump table to
268 /// be emitted to the current location in the function, and allow it to be PC
269 /// relative.
270 void ARMCodeEmitter::emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) {
271   MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
272                                                     Reloc, JTIndex, 0, true));
273 }
274
275 /// emitMachineBasicBlock - Emit the specified address basic block.
276 void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
277                                            unsigned Reloc, intptr_t JTBase) {
278   MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
279                                              Reloc, BB, JTBase));
280 }
281
282 void ARMCodeEmitter::emitWordLE(unsigned Binary) {
283 #ifndef NDEBUG
284   DOUT << "  0x" << std::hex << std::setw(8) << std::setfill('0')
285        << Binary << std::dec << "\n";
286 #endif
287   MCE.emitWordLE(Binary);
288 }
289
290 void ARMCodeEmitter::emitDWordLE(uint64_t Binary) {
291 #ifndef NDEBUG
292   DOUT << "  0x" << std::hex << std::setw(8) << std::setfill('0')
293        << (unsigned)Binary << std::dec << "\n";
294   DOUT << "  0x" << std::hex << std::setw(8) << std::setfill('0')
295        << (unsigned)(Binary >> 32) << std::dec << "\n";
296 #endif
297   MCE.emitDWordLE(Binary);
298 }
299
300 void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
301   DOUT << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI;
302
303   NumEmitted++;  // Keep track of the # of mi's emitted
304   switch (MI.getDesc().TSFlags & ARMII::FormMask) {
305   default:
306     assert(0 && "Unhandled instruction encoding format!");
307     break;
308   case ARMII::Pseudo:
309     emitPseudoInstruction(MI);
310     break;
311   case ARMII::DPFrm:
312   case ARMII::DPSoRegFrm:
313     emitDataProcessingInstruction(MI);
314     break;
315   case ARMII::LdFrm:
316   case ARMII::StFrm:
317     emitLoadStoreInstruction(MI);
318     break;
319   case ARMII::LdMiscFrm:
320   case ARMII::StMiscFrm:
321     emitMiscLoadStoreInstruction(MI);
322     break;
323   case ARMII::LdMulFrm:
324   case ARMII::StMulFrm:
325     emitLoadStoreMultipleInstruction(MI);
326     break;
327   case ARMII::MulFrm:
328     emitMulFrmInstruction(MI);
329     break;
330   case ARMII::ExtFrm:
331     emitExtendInstruction(MI);
332     break;
333   case ARMII::ArithMiscFrm:
334     emitMiscArithInstruction(MI);
335     break;
336   case ARMII::BrFrm:
337     emitBranchInstruction(MI);
338     break;
339   case ARMII::BrMiscFrm:
340     emitMiscBranchInstruction(MI);
341     break;
342   // VFP instructions.
343   case ARMII::VFPUnaryFrm:
344   case ARMII::VFPBinaryFrm:
345     emitVFPArithInstruction(MI);
346     break;
347   case ARMII::VFPConv1Frm:
348   case ARMII::VFPConv2Frm:
349   case ARMII::VFPConv3Frm:
350     emitVFPConversionInstruction(MI);
351     break;
352   case ARMII::VFPLdStFrm:
353     emitVFPLoadStoreInstruction(MI);
354     break;
355   case ARMII::VFPLdStMulFrm:
356     emitVFPLoadStoreMultipleInstruction(MI);
357     break;
358   case ARMII::VFPMiscFrm:
359     emitMiscInstruction(MI);
360     break;
361   }
362 }
363
364 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
365   unsigned CPI = MI.getOperand(0).getImm();       // CP instruction index.
366   unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
367   const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
368   
369   // Remember the CONSTPOOL_ENTRY address for later relocation.
370   JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
371
372   // Emit constpool island entry. In most cases, the actual values will be
373   // resolved and relocated after code emission.
374   if (MCPE.isMachineConstantPoolEntry()) {
375     ARMConstantPoolValue *ACPV =
376       static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
377
378     DOUT << "  ** ARM constant pool #" << CPI << " @ "
379          << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n';
380
381     GlobalValue *GV = ACPV->getGV();
382     if (GV) {
383       assert(!ACPV->isStub() && "Don't know how to deal this yet!");
384       if (ACPV->isNonLazyPointer())
385         MCE.addRelocation(MachineRelocation::getIndirectSymbol(
386                   MCE.getCurrentPCOffset(), ARM::reloc_arm_machine_cp_entry, GV,
387                   (intptr_t)ACPV, false));
388       else 
389         emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
390                           ACPV->isStub(), (intptr_t)ACPV);
391      } else  {
392       assert(!ACPV->isNonLazyPointer() && "Don't know how to deal this yet!");
393       emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
394     }
395     emitWordLE(0);
396   } else {
397     Constant *CV = MCPE.Val.ConstVal;
398
399     DOUT << "  ** Constant pool #" << CPI << " @ "
400          << (void*)MCE.getCurrentPCValue() << " " << *CV << '\n';
401
402     if (GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
403       emitGlobalAddress(GV, ARM::reloc_arm_absolute, false);
404       emitWordLE(0);
405     } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
406       uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
407       emitWordLE(Val);
408     } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
409       if (CFP->getType() == Type::FloatTy)
410         emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
411       else if (CFP->getType() == Type::DoubleTy)
412         emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
413       else {
414         assert(0 && "Unable to handle this constantpool entry!");
415         abort();
416       }
417     } else {
418       assert(0 && "Unable to handle this constantpool entry!");
419       abort();
420     }
421   }
422 }
423
424 void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) {
425   const MachineOperand &MO0 = MI.getOperand(0);
426   const MachineOperand &MO1 = MI.getOperand(1);
427   assert(MO1.isImm() && "Not a valid so_imm value!");
428   unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
429   unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
430
431   // Emit the 'mov' instruction.
432   unsigned Binary = 0xd << 21;  // mov: Insts{24-21} = 0b1101
433
434   // Set the conditional execution predicate.
435   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
436
437   // Encode Rd.
438   Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
439
440   // Encode so_imm.
441   // Set bit I(25) to identify this is the immediate form of <shifter_op>
442   Binary |= 1 << ARMII::I_BitShift;
443   Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V1));
444   emitWordLE(Binary);
445
446   // Now the 'orr' instruction.
447   Binary = 0xc << 21;  // orr: Insts{24-21} = 0b1100
448
449   // Set the conditional execution predicate.
450   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
451
452   // Encode Rd.
453   Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
454
455   // Encode Rn.
456   Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
457
458   // Encode so_imm.
459   // Set bit I(25) to identify this is the immediate form of <shifter_op>
460   Binary |= 1 << ARMII::I_BitShift;
461   Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V2));
462   emitWordLE(Binary);
463 }
464
465 void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) {
466   // It's basically add r, pc, (LJTI - $+8)
467   
468   const TargetInstrDesc &TID = MI.getDesc();
469
470   // Emit the 'add' instruction.
471   unsigned Binary = 0x4 << 21;  // add: Insts{24-31} = 0b0100
472
473   // Set the conditional execution predicate
474   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
475
476   // Encode S bit if MI modifies CPSR.
477   Binary |= getAddrModeSBit(MI, TID);
478
479   // Encode Rd.
480   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
481
482   // Encode Rn which is PC.
483   Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
484
485   // Encode the displacement.
486   // Set bit I(25) to identify this is the immediate form of <shifter_op>.
487   Binary |= 1 << ARMII::I_BitShift;
488   emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
489
490   emitWordLE(Binary);
491 }
492
493 void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
494   DOUT << "  ** LPC" << LabelID << " @ "
495        << (void*)MCE.getCurrentPCValue() << '\n';
496   JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
497 }
498
499 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
500   unsigned Opcode = MI.getDesc().Opcode;
501   switch (Opcode) {
502   default:
503     abort(); // FIXME:
504   case ARM::CONSTPOOL_ENTRY:
505     emitConstPoolInstruction(MI);
506     break;
507   case ARM::PICADD: {
508     // Remember of the address of the PC label for relocation later.
509     addPCLabel(MI.getOperand(2).getImm());
510     // PICADD is just an add instruction that implicitly read pc.
511     emitDataProcessingInstruction(MI, 0, ARM::PC);
512     break;
513   }
514   case ARM::PICLDR:
515   case ARM::PICLDRB:
516   case ARM::PICSTR:
517   case ARM::PICSTRB: {
518     // Remember of the address of the PC label for relocation later.
519     addPCLabel(MI.getOperand(2).getImm());
520     // These are just load / store instructions that implicitly read pc.
521     emitLoadStoreInstruction(MI, 0, ARM::PC);
522     break;
523   }
524   case ARM::PICLDRH:
525   case ARM::PICLDRSH:
526   case ARM::PICLDRSB:
527   case ARM::PICSTRH: {
528     // Remember of the address of the PC label for relocation later.
529     addPCLabel(MI.getOperand(2).getImm());
530     // These are just load / store instructions that implicitly read pc.
531     emitMiscLoadStoreInstruction(MI, ARM::PC);
532     break;
533   }
534   case ARM::MOVi2pieces:
535     // Two instructions to materialize a constant.
536     emitMOVi2piecesInstruction(MI);
537     break;
538   case ARM::LEApcrelJT:
539     // Materialize jumptable address.
540     emitLEApcrelJTInstruction(MI);
541     break;
542   }
543 }
544
545
546 unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
547                                                 const TargetInstrDesc &TID,
548                                                 const MachineOperand &MO,
549                                                 unsigned OpIdx) {
550   unsigned Binary = getMachineOpValue(MI, MO);
551
552   const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
553   const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
554   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
555
556   // Encode the shift opcode.
557   unsigned SBits = 0;
558   unsigned Rs = MO1.getReg();
559   if (Rs) {
560     // Set shift operand (bit[7:4]).
561     // LSL - 0001
562     // LSR - 0011
563     // ASR - 0101
564     // ROR - 0111
565     // RRX - 0110 and bit[11:8] clear.
566     switch (SOpc) {
567     default: assert(0 && "Unknown shift opc!");
568     case ARM_AM::lsl: SBits = 0x1; break;
569     case ARM_AM::lsr: SBits = 0x3; break;
570     case ARM_AM::asr: SBits = 0x5; break;
571     case ARM_AM::ror: SBits = 0x7; break;
572     case ARM_AM::rrx: SBits = 0x6; break;
573     }
574   } else {
575     // Set shift operand (bit[6:4]).
576     // LSL - 000
577     // LSR - 010
578     // ASR - 100
579     // ROR - 110
580     switch (SOpc) {
581     default: assert(0 && "Unknown shift opc!");
582     case ARM_AM::lsl: SBits = 0x0; break;
583     case ARM_AM::lsr: SBits = 0x2; break;
584     case ARM_AM::asr: SBits = 0x4; break;
585     case ARM_AM::ror: SBits = 0x6; break;
586     }
587   }
588   Binary |= SBits << 4;
589   if (SOpc == ARM_AM::rrx)
590     return Binary;
591
592   // Encode the shift operation Rs or shift_imm (except rrx).
593   if (Rs) {
594     // Encode Rs bit[11:8].
595     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
596     return Binary |
597       (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
598   }
599
600   // Encode shift_imm bit[11:7].
601   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
602 }
603
604 unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) {
605   // Encode rotate_imm.
606   unsigned Binary = (ARM_AM::getSOImmValRot(SoImm) >> 1)
607     << ARMII::SoRotImmShift;
608
609   // Encode immed_8.
610   Binary |= ARM_AM::getSOImmValImm(SoImm);
611   return Binary;
612 }
613
614 unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
615                                          const TargetInstrDesc &TID) const {
616   for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
617     const MachineOperand &MO = MI.getOperand(i-1);
618     if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
619       return 1 << ARMII::S_BitShift;
620   }
621   return 0;
622 }
623
624 void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
625                                                    unsigned ImplicitRd,
626                                                    unsigned ImplicitRn) {
627   const TargetInstrDesc &TID = MI.getDesc();
628
629   // Part of binary is determined by TableGn.
630   unsigned Binary = getBinaryCodeForInstr(MI);
631
632   // Set the conditional execution predicate
633   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
634
635   // Encode S bit if MI modifies CPSR.
636   Binary |= getAddrModeSBit(MI, TID);
637
638   // Encode register def if there is one.
639   unsigned NumDefs = TID.getNumDefs();
640   unsigned OpIdx = 0;
641   if (NumDefs)
642     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
643   else if (ImplicitRd)
644     // Special handling for implicit use (e.g. PC).
645     Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
646                << ARMII::RegRdShift);
647
648   // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
649   if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
650     ++OpIdx;
651
652   // Encode first non-shifter register operand if there is one.
653   bool isUnary = TID.TSFlags & ARMII::UnaryDP;
654   if (!isUnary) {
655     if (ImplicitRn)
656       // Special handling for implicit use (e.g. PC).
657       Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
658                  << ARMII::RegRnShift);
659     else {
660       Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
661       ++OpIdx;
662     }
663   }
664
665   // Encode shifter operand.
666   const MachineOperand &MO = MI.getOperand(OpIdx);
667   if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
668     // Encode SoReg.
669     emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
670     return;
671   }
672
673   if (MO.isReg()) {
674     // Encode register Rm.
675     emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg()));
676     return;
677   }
678
679   // Encode so_imm.
680   // Set bit I(25) to identify this is the immediate form of <shifter_op>.
681   Binary |= 1 << ARMII::I_BitShift;
682   Binary |= getMachineSoImmOpValue(MO.getImm());
683
684   emitWordLE(Binary);
685 }
686
687 void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
688                                               unsigned ImplicitRd,
689                                               unsigned ImplicitRn) {
690   const TargetInstrDesc &TID = MI.getDesc();
691
692   // Part of binary is determined by TableGn.
693   unsigned Binary = getBinaryCodeForInstr(MI);
694
695   // Set the conditional execution predicate
696   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
697
698   // Set first operand
699   unsigned OpIdx = 0;
700   if (ImplicitRd)
701     // Special handling for implicit use (e.g. PC).
702     Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
703                << ARMII::RegRdShift);
704   else
705     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
706
707   // Set second operand
708   if (ImplicitRn)
709     // Special handling for implicit use (e.g. PC).
710     Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
711                << ARMII::RegRnShift);
712   else
713     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
714
715   // If this is a two-address operand, skip it. e.g. LDR_PRE.
716   if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
717     ++OpIdx;
718
719   const MachineOperand &MO2 = MI.getOperand(OpIdx);
720   unsigned AM2Opc = (ImplicitRn == ARM::PC)
721     ? 0 : MI.getOperand(OpIdx+1).getImm();
722
723   // Set bit U(23) according to sign of immed value (positive or negative).
724   Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
725              ARMII::U_BitShift);
726   if (!MO2.getReg()) { // is immediate
727     if (ARM_AM::getAM2Offset(AM2Opc))
728       // Set the value of offset_12 field
729       Binary |= ARM_AM::getAM2Offset(AM2Opc);
730     emitWordLE(Binary);
731     return;
732   }
733
734   // Set bit I(25), because this is not in immediate enconding.
735   Binary |= 1 << ARMII::I_BitShift;
736   assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
737   // Set bit[3:0] to the corresponding Rm register
738   Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
739
740   // if this instr is in scaled register offset/index instruction, set
741   // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
742   if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
743     Binary |= getShiftOp(AM2Opc) << 5;  // shift
744     Binary |= ShImm              << 7;  // shift_immed
745   }
746
747   emitWordLE(Binary);
748 }
749
750 void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
751                                                   unsigned ImplicitRn) {
752   const TargetInstrDesc &TID = MI.getDesc();
753
754   // Part of binary is determined by TableGn.
755   unsigned Binary = getBinaryCodeForInstr(MI);
756
757   // Set the conditional execution predicate
758   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
759
760   // Set first operand
761   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
762
763   // Set second operand
764   unsigned OpIdx = 1;
765   if (ImplicitRn)
766     // Special handling for implicit use (e.g. PC).
767     Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
768                << ARMII::RegRnShift);
769   else
770     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
771
772   // If this is a two-address operand, skip it. e.g. LDRH_POST.
773   if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
774     ++OpIdx;
775
776   const MachineOperand &MO2 = MI.getOperand(OpIdx);
777   unsigned AM3Opc = (ImplicitRn == ARM::PC)
778     ? 0 : MI.getOperand(OpIdx+1).getImm();
779
780   // Set bit U(23) according to sign of immed value (positive or negative)
781   Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
782              ARMII::U_BitShift);
783
784   // If this instr is in register offset/index encoding, set bit[3:0]
785   // to the corresponding Rm register.
786   if (MO2.getReg()) {
787     Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
788     emitWordLE(Binary);
789     return;
790   }
791
792   // This instr is in immediate offset/index encoding, set bit 22 to 1.
793   Binary |= 1 << ARMII::AM3_I_BitShift;
794   if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
795     // Set operands
796     Binary |= (ImmOffs >> 4) << 8;  // immedH
797     Binary |= (ImmOffs & ~0xF);     // immedL
798   }
799
800   emitWordLE(Binary);
801 }
802
803 static unsigned getAddrModeUPBits(unsigned Mode) {
804   unsigned Binary = 0;
805
806   // Set addressing mode by modifying bits U(23) and P(24)
807   // IA - Increment after  - bit U = 1 and bit P = 0
808   // IB - Increment before - bit U = 1 and bit P = 1
809   // DA - Decrement after  - bit U = 0 and bit P = 0
810   // DB - Decrement before - bit U = 0 and bit P = 1
811   switch (Mode) {
812   default: assert(0 && "Unknown addressing sub-mode!");
813   case ARM_AM::da:                      break;
814   case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
815   case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
816   case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
817   }
818
819   return Binary;
820 }
821
822 void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
823   // Part of binary is determined by TableGn.
824   unsigned Binary = getBinaryCodeForInstr(MI);
825
826   // Set the conditional execution predicate
827   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
828
829   // Set base address operand
830   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
831
832   // Set addressing mode by modifying bits U(23) and P(24)
833   const MachineOperand &MO = MI.getOperand(1);
834   Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm()));
835
836   // Set bit W(21)
837   if (ARM_AM::getAM4WBFlag(MO.getImm()))
838     Binary |= 0x1 << ARMII::W_BitShift;
839
840   // Set registers
841   for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
842     const MachineOperand &MO = MI.getOperand(i);
843     if (!MO.isReg() || MO.isImplicit())
844       break;
845     unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
846     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
847            RegNum < 16);
848     Binary |= 0x1 << RegNum;
849   }
850
851   emitWordLE(Binary);
852 }
853
854 void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
855   const TargetInstrDesc &TID = MI.getDesc();
856
857   // Part of binary is determined by TableGn.
858   unsigned Binary = getBinaryCodeForInstr(MI);
859
860   // Set the conditional execution predicate
861   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
862
863   // Encode S bit if MI modifies CPSR.
864   Binary |= getAddrModeSBit(MI, TID);
865
866   // 32x32->64bit operations have two destination registers. The number
867   // of register definitions will tell us if that's what we're dealing with.
868   unsigned OpIdx = 0;
869   if (TID.getNumDefs() == 2)
870     Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
871
872   // Encode Rd
873   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
874
875   // Encode Rm
876   Binary |= getMachineOpValue(MI, OpIdx++);
877
878   // Encode Rs
879   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
880
881   // Many multiple instructions (e.g. MLA) have three src operands. Encode
882   // it as Rn (for multiply, that's in the same offset as RdLo.
883   if (TID.getNumOperands() > OpIdx &&
884       !TID.OpInfo[OpIdx].isPredicate() &&
885       !TID.OpInfo[OpIdx].isOptionalDef())
886     Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
887
888   emitWordLE(Binary);
889 }
890
891 void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) {
892   const TargetInstrDesc &TID = MI.getDesc();
893
894   // Part of binary is determined by TableGn.
895   unsigned Binary = getBinaryCodeForInstr(MI);
896
897   // Set the conditional execution predicate
898   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
899
900   unsigned OpIdx = 0;
901
902   // Encode Rd
903   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
904
905   const MachineOperand &MO1 = MI.getOperand(OpIdx++);
906   const MachineOperand &MO2 = MI.getOperand(OpIdx);
907   if (MO2.isReg()) {
908     // Two register operand form.
909     // Encode Rn.
910     Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
911
912     // Encode Rm.
913     Binary |= getMachineOpValue(MI, MO2);
914     ++OpIdx;
915   } else {
916     Binary |= getMachineOpValue(MI, MO1);
917   }
918
919   // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
920   if (MI.getOperand(OpIdx).isImm() &&
921       !TID.OpInfo[OpIdx].isPredicate() &&
922       !TID.OpInfo[OpIdx].isOptionalDef())
923     Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
924
925   emitWordLE(Binary);
926 }
927
928 void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) {
929   const TargetInstrDesc &TID = MI.getDesc();
930
931   // Part of binary is determined by TableGn.
932   unsigned Binary = getBinaryCodeForInstr(MI);
933
934   // Set the conditional execution predicate
935   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
936
937   unsigned OpIdx = 0;
938
939   // Encode Rd
940   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
941
942   const MachineOperand &MO = MI.getOperand(OpIdx++);
943   if (OpIdx == TID.getNumOperands() ||
944       TID.OpInfo[OpIdx].isPredicate() ||
945       TID.OpInfo[OpIdx].isOptionalDef()) {
946     // Encode Rm and it's done.
947     Binary |= getMachineOpValue(MI, MO);
948     emitWordLE(Binary);
949     return;
950   }
951
952   // Encode Rn.
953   Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
954
955   // Encode Rm.
956   Binary |= getMachineOpValue(MI, OpIdx++);
957
958   // Encode shift_imm.
959   unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
960   assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
961   Binary |= ShiftAmt << ARMII::ShiftShift;
962   
963   emitWordLE(Binary);
964 }
965
966 void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
967   const TargetInstrDesc &TID = MI.getDesc();
968
969   if (TID.Opcode == ARM::TPsoft)
970     abort(); // FIXME
971
972   // Part of binary is determined by TableGn.
973   unsigned Binary = getBinaryCodeForInstr(MI);
974
975   // Set the conditional execution predicate
976   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
977
978   // Set signed_immed_24 field
979   Binary |= getMachineOpValue(MI, 0);
980
981   emitWordLE(Binary);
982 }
983
984 void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) {
985   // Remember the base address of the inline jump table.
986   intptr_t JTBase = MCE.getCurrentPCValue();
987   JTI->addJumpTableBaseAddr(JTIndex, JTBase);
988   DOUT << "  ** Jump Table #" << JTIndex << " @ " << (void*)JTBase << '\n';
989
990   // Now emit the jump table entries.
991   const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
992   for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
993     if (IsPIC)
994       // DestBB address - JT base.
995       emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
996     else
997       // Absolute DestBB address.
998       emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
999     emitWordLE(0);
1000   }
1001 }
1002
1003 void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
1004   const TargetInstrDesc &TID = MI.getDesc();
1005
1006   // Handle jump tables.
1007   if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) {
1008     // First emit a ldr pc, [] instruction.
1009     emitDataProcessingInstruction(MI, ARM::PC);
1010
1011     // Then emit the inline jump table.
1012     unsigned JTIndex = (TID.Opcode == ARM::BR_JTr)
1013       ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1014     emitInlineJumpTable(JTIndex);
1015     return;
1016   } else if (TID.Opcode == ARM::BR_JTm) {
1017     // First emit a ldr pc, [] instruction.
1018     emitLoadStoreInstruction(MI, ARM::PC);
1019
1020     // Then emit the inline jump table.
1021     emitInlineJumpTable(MI.getOperand(3).getIndex());
1022     return;
1023   }
1024
1025   // Part of binary is determined by TableGn.
1026   unsigned Binary = getBinaryCodeForInstr(MI);
1027
1028   // Set the conditional execution predicate
1029   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1030
1031   if (TID.Opcode == ARM::BX_RET)
1032     // The return register is LR.
1033     Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
1034   else 
1035     // otherwise, set the return register
1036     Binary |= getMachineOpValue(MI, 0);
1037
1038   emitWordLE(Binary);
1039 }
1040
1041 void ARMCodeEmitter::emitVFPArithInstruction(const MachineInstr &MI) {
1042   const TargetInstrDesc &TID = MI.getDesc();
1043
1044   // Part of binary is determined by TableGn.
1045   unsigned Binary = getBinaryCodeForInstr(MI);
1046
1047   // Set the conditional execution predicate
1048   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1049
1050   unsigned OpIdx = 0;
1051   assert((Binary & ARMII::D_BitShift) == 0 &&
1052          (Binary & ARMII::N_BitShift) == 0 &&
1053          (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1054
1055   // Encode Dd / Sd.
1056   unsigned RegD = MI.getOperand(OpIdx++).getReg();
1057   bool isSPVFP = false;
1058   RegD = ARMRegisterInfo::getRegisterNumbering(RegD, isSPVFP);
1059   if (!isSPVFP)
1060     Binary |=   RegD               << ARMII::RegRdShift;
1061   else {
1062     Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1063     Binary |=  (RegD & 0x01)       << ARMII::D_BitShift;
1064   }
1065     
1066
1067   // If this is a two-address operand, skip it, e.g. FMACD.
1068   if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1069     ++OpIdx;
1070
1071   // Encode Dn / Sn.
1072   if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm) {
1073     unsigned RegN = MI.getOperand(OpIdx++).getReg();
1074     isSPVFP = false;
1075     RegN = ARMRegisterInfo::getRegisterNumbering(RegN, isSPVFP);
1076     if (!isSPVFP)
1077       Binary |=   RegN               << ARMII::RegRnShift;
1078     else {
1079       Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1080       Binary |=  (RegN & 0x01)       << ARMII::N_BitShift;
1081     }
1082   }
1083
1084   // Encode Dm / Sm.
1085   unsigned RegM = MI.getOperand(OpIdx++).getReg();
1086   isSPVFP = false;
1087   RegM = ARMRegisterInfo::getRegisterNumbering(RegM, isSPVFP);
1088   if (!isSPVFP)
1089     Binary |=   RegM;
1090   else {
1091     Binary |= ((RegM & 0x1E) >> 1);
1092     Binary |=  (RegM & 0x01)       << ARMII::M_BitShift;
1093   }
1094   
1095   emitWordLE(Binary);
1096 }
1097
1098 void ARMCodeEmitter::emitVFPConversionInstruction(const MachineInstr &MI) {
1099   const TargetInstrDesc &TID = MI.getDesc();
1100
1101   // Part of binary is determined by TableGn.
1102   unsigned Binary = getBinaryCodeForInstr(MI);
1103
1104   // Set the conditional execution predicate
1105   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1106
1107   // FMDRR encodes registers in reverse order.
1108   unsigned Form = TID.TSFlags & ARMII::FormMask;
1109   unsigned OpIdx = (Form == ARMII::VFPConv2Frm)
1110     ? MI.findFirstPredOperandIdx()-1 : 0;
1111
1112   // Encode Dd / Sd.
1113   unsigned RegD = MI.getOperand(OpIdx).getReg();
1114   bool isSPVFP = false;
1115   RegD = ARMRegisterInfo::getRegisterNumbering(RegD, isSPVFP);
1116   if (!isSPVFP)
1117     Binary |=   RegD               << ARMII::RegRdShift;
1118   else {
1119     Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1120     Binary |=  (RegD & 0x01)       << ARMII::D_BitShift;
1121   }
1122   if (Form == ARMII::VFPConv2Frm)
1123     --OpIdx;
1124   else
1125     ++OpIdx;
1126
1127   if (Form == ARMII::VFPConv3Frm) {
1128     unsigned RegM = MI.getOperand(OpIdx).getReg();
1129     isSPVFP = false;
1130     RegM = ARMRegisterInfo::getRegisterNumbering(RegM, isSPVFP);
1131     if (!isSPVFP)
1132       Binary |=   RegM;
1133     else {
1134       Binary |= ((RegM & 0x1E) >> 1);
1135       Binary |=  (RegM & 0x01)       << ARMII::M_BitShift;
1136     }
1137
1138     emitWordLE(Binary);
1139     return;
1140   }
1141
1142   // Encode Dn / Sn.
1143   unsigned RegN = MI.getOperand(OpIdx).getReg();
1144   isSPVFP = false;
1145   RegN = ARMRegisterInfo::getRegisterNumbering(RegN, isSPVFP);
1146   if (!isSPVFP)
1147     Binary |=   RegN               << ARMII::RegRnShift;
1148   else {
1149     Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1150     Binary |=  (RegN & 0x01)       << ARMII::N_BitShift;
1151   }
1152   if (Form == ARMII::VFPConv2Frm)
1153     --OpIdx;
1154   else
1155     ++OpIdx;
1156
1157   // FMRS / FMSR do not have Rm.
1158   if (TID.getNumOperands() > OpIdx && MI.getOperand(OpIdx).isReg()) {
1159     unsigned RegM = MI.getOperand(OpIdx).getReg();
1160     isSPVFP = false;
1161     RegM = ARMRegisterInfo::getRegisterNumbering(RegM, isSPVFP);
1162     if (!isSPVFP)
1163       Binary |=   RegM;
1164     else {
1165       Binary |= ((RegM & 0x1E) >> 1);
1166       Binary |=  (RegM & 0x01)       << ARMII::M_BitShift;
1167     }
1168   }
1169
1170   emitWordLE(Binary);
1171 }
1172
1173 void ARMCodeEmitter::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
1174   // Part of binary is determined by TableGn.
1175   unsigned Binary = getBinaryCodeForInstr(MI);
1176
1177   // Set the conditional execution predicate
1178   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1179
1180   unsigned OpIdx = 0;
1181
1182   // Encode Dd / Sd.
1183   unsigned RegD = MI.getOperand(OpIdx++).getReg();
1184   bool isSPVFP = false;
1185   RegD = ARMRegisterInfo::getRegisterNumbering(RegD, isSPVFP);
1186   if (!isSPVFP)
1187     Binary |=   RegD               << ARMII::RegRdShift;
1188   else {
1189     Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1190     Binary |=  (RegD & 0x01)       << ARMII::D_BitShift;
1191   }
1192
1193   // Encode address base.
1194   const MachineOperand &Base = MI.getOperand(OpIdx++);
1195   Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1196
1197   // If there is a non-zero immediate offset, encode it.
1198   if (Base.isReg()) {
1199     const MachineOperand &Offset = MI.getOperand(OpIdx);
1200     if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1201       if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1202         Binary |= 1 << ARMII::U_BitShift;
1203       // Immediate offset is multiplied by 4.
1204       Binary |= ImmOffs >> 2;
1205       emitWordLE(Binary);
1206       return;
1207     }
1208   }
1209
1210   // If immediate offset is omitted, default to +0.
1211   Binary |= 1 << ARMII::U_BitShift;
1212
1213   emitWordLE(Binary);
1214 }
1215
1216 void
1217 ARMCodeEmitter::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) {
1218   // Part of binary is determined by TableGn.
1219   unsigned Binary = getBinaryCodeForInstr(MI);
1220
1221   // Set the conditional execution predicate
1222   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1223
1224   // Set base address operand
1225   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
1226
1227   // Set addressing mode by modifying bits U(23) and P(24)
1228   const MachineOperand &MO = MI.getOperand(1);
1229   Binary |= getAddrModeUPBits(ARM_AM::getAM5SubMode(MO.getImm()));
1230
1231   // Set bit W(21)
1232   if (ARM_AM::getAM5WBFlag(MO.getImm()))
1233     Binary |= 0x1 << ARMII::W_BitShift;
1234
1235   // First register is encoded in Dd.
1236   unsigned RegD = MI.getOperand(4).getReg();
1237   bool isSPVFP = false;
1238   RegD = ARMRegisterInfo::getRegisterNumbering(RegD, isSPVFP);
1239   if (!isSPVFP)
1240     Binary |=   RegD               << ARMII::RegRdShift;
1241   else {
1242     Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1243     Binary |=  (RegD & 0x01)       << ARMII::D_BitShift;
1244   }
1245
1246   // Number of registers are encoded in offset field.
1247   unsigned NumRegs = 1;
1248   for (unsigned i = 5, e = MI.getNumOperands(); i != e; ++i) {
1249     const MachineOperand &MO = MI.getOperand(i);
1250     if (!MO.isReg() || MO.isImplicit())
1251       break;
1252     ++NumRegs;
1253   }
1254   Binary |= NumRegs * 2;
1255
1256   emitWordLE(Binary);
1257 }
1258
1259 void ARMCodeEmitter::emitMiscInstruction(const MachineInstr &MI) {
1260   // Part of binary is determined by TableGn.
1261   unsigned Binary = getBinaryCodeForInstr(MI);
1262
1263   // Set the conditional execution predicate
1264   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1265
1266   emitWordLE(Binary);
1267 }
1268
1269 #include "ARMGenCodeEmitter.inc"