Handle floating point constpool_entry's.
[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     emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry);
227   else if (MO.isJTI())
228     emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
229   else if (MO.isMBB())
230     emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
231   else {
232     cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
233     abort();
234   }
235   return 0;
236 }
237
238 /// emitGlobalAddress - Emit the specified address to the code stream.
239 ///
240 void ARMCodeEmitter::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
241                                        bool NeedStub, intptr_t ACPV) {
242   MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
243                                              Reloc, GV, ACPV, NeedStub));
244 }
245
246 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
247 /// be emitted to the current location in the function, and allow it to be PC
248 /// relative.
249 void ARMCodeEmitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
250   MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
251                                                  Reloc, ES));
252 }
253
254 /// emitConstPoolAddress - Arrange for the address of an constant pool
255 /// to be emitted to the current location in the function, and allow it to be PC
256 /// relative.
257 void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) {
258   // Tell JIT emitter we'll resolve the address.
259   MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
260                                                     Reloc, CPI, 0, true));
261 }
262
263 /// emitJumpTableAddress - Arrange for the address of a jump table to
264 /// be emitted to the current location in the function, and allow it to be PC
265 /// relative.
266 void ARMCodeEmitter::emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) {
267   MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
268                                                     Reloc, JTIndex, 0, true));
269 }
270
271 /// emitMachineBasicBlock - Emit the specified address basic block.
272 void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
273                                            unsigned Reloc, intptr_t JTBase) {
274   MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
275                                              Reloc, BB, JTBase));
276 }
277
278 void ARMCodeEmitter::emitWordLE(unsigned Binary) {
279 #ifndef NDEBUG
280   DOUT << "  0x" << std::hex << std::setw(8) << std::setfill('0')
281        << Binary << std::dec << "\n";
282 #endif
283   MCE.emitWordLE(Binary);
284 }
285
286 void ARMCodeEmitter::emitDWordLE(uint64_t Binary) {
287 #ifndef NDEBUG
288   DOUT << "  0x" << std::hex << std::setw(8) << std::setfill('0')
289        << (unsigned)Binary << std::dec << "\n";
290   DOUT << "  0x" << std::hex << std::setw(8) << std::setfill('0')
291        << (unsigned)(Binary >> 32) << std::dec << "\n";
292 #endif
293   MCE.emitDWordLE(Binary);
294 }
295
296 void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
297   DOUT << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI;
298
299   NumEmitted++;  // Keep track of the # of mi's emitted
300   switch (MI.getDesc().TSFlags & ARMII::FormMask) {
301   default:
302     assert(0 && "Unhandled instruction encoding format!");
303     break;
304   case ARMII::Pseudo:
305     emitPseudoInstruction(MI);
306     break;
307   case ARMII::DPFrm:
308   case ARMII::DPSoRegFrm:
309     emitDataProcessingInstruction(MI);
310     break;
311   case ARMII::LdFrm:
312   case ARMII::StFrm:
313     emitLoadStoreInstruction(MI);
314     break;
315   case ARMII::LdMiscFrm:
316   case ARMII::StMiscFrm:
317     emitMiscLoadStoreInstruction(MI);
318     break;
319   case ARMII::LdMulFrm:
320   case ARMII::StMulFrm:
321     emitLoadStoreMultipleInstruction(MI);
322     break;
323   case ARMII::MulFrm:
324     emitMulFrmInstruction(MI);
325     break;
326   case ARMII::ExtFrm:
327     emitExtendInstruction(MI);
328     break;
329   case ARMII::ArithMiscFrm:
330     emitMiscArithInstruction(MI);
331     break;
332   case ARMII::BrFrm:
333     emitBranchInstruction(MI);
334     break;
335   case ARMII::BrMiscFrm:
336     emitMiscBranchInstruction(MI);
337     break;
338   // VFP instructions.
339   case ARMII::VFPUnaryFrm:
340   case ARMII::VFPBinaryFrm:
341     emitVFPArithInstruction(MI);
342     break;
343   case ARMII::VFPConv1Frm:
344   case ARMII::VFPConv2Frm:
345     emitVFPConversionInstruction(MI);
346     break;
347   case ARMII::VFPLdStFrm:
348     emitVFPLoadStoreInstruction(MI);
349     break;
350   case ARMII::VFPLdStMulFrm:
351     emitVFPLoadStoreMultipleInstruction(MI);
352     break;
353   case ARMII::VFPMiscFrm:
354     emitMiscInstruction(MI);
355     break;
356   }
357 }
358
359 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
360   unsigned CPI = MI.getOperand(0).getImm();       // CP instruction index.
361   unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
362   const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
363   
364   // Remember the CONSTPOOL_ENTRY address for later relocation.
365   JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
366
367   // Emit constpool island entry. In most cases, the actual values will be
368   // resolved and relocated after code emission.
369   if (MCPE.isMachineConstantPoolEntry()) {
370     ARMConstantPoolValue *ACPV =
371       static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
372
373     DOUT << "  ** ARM constant pool #" << CPI << " @ "
374          << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n';
375
376     GlobalValue *GV = ACPV->getGV();
377     if (GV) {
378       assert(!ACPV->isStub() && "Don't know how to deal this yet!");
379       if (ACPV->isNonLazyPointer())
380         MCE.addRelocation(MachineRelocation::getIndirectSymbol(
381                   MCE.getCurrentPCOffset(), ARM::reloc_arm_machine_cp_entry, GV,
382                   (intptr_t)ACPV, false));
383       else 
384         emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
385                           ACPV->isStub(), (intptr_t)ACPV);
386      } else  {
387       assert(!ACPV->isNonLazyPointer() && "Don't know how to deal this yet!");
388       emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
389     }
390     emitWordLE(0);
391   } else {
392     Constant *CV = MCPE.Val.ConstVal;
393
394     DOUT << "  ** Constant pool #" << CPI << " @ "
395          << (void*)MCE.getCurrentPCValue() << " " << *CV << '\n';
396
397     if (GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
398       emitGlobalAddress(GV, ARM::reloc_arm_absolute, false);
399       emitWordLE(0);
400     } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
401       uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
402       emitWordLE(Val);
403     } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
404       if (CFP->getType() == Type::FloatTy)
405         emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
406       else if (CFP->getType() == Type::DoubleTy)
407         emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
408       else {
409         assert(0 && "Unable to handle this constantpool entry!");
410         abort();
411       }
412     } else {
413       assert(0 && "Unable to handle this constantpool entry!");
414       abort();
415     }
416   }
417 }
418
419 void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) {
420   const MachineOperand &MO0 = MI.getOperand(0);
421   const MachineOperand &MO1 = MI.getOperand(1);
422   assert(MO1.isImm() && "Not a valid so_imm value!");
423   unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
424   unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
425
426   // Emit the 'mov' instruction.
427   unsigned Binary = 0xd << 21;  // mov: Insts{24-21} = 0b1101
428
429   // Set the conditional execution predicate.
430   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
431
432   // Encode Rd.
433   Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
434
435   // Encode so_imm.
436   // Set bit I(25) to identify this is the immediate form of <shifter_op>
437   Binary |= 1 << ARMII::I_BitShift;
438   Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V1));
439   emitWordLE(Binary);
440
441   // Now the 'orr' instruction.
442   Binary = 0xc << 21;  // orr: Insts{24-21} = 0b1100
443
444   // Set the conditional execution predicate.
445   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
446
447   // Encode Rd.
448   Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
449
450   // Encode Rn.
451   Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
452
453   // Encode so_imm.
454   // Set bit I(25) to identify this is the immediate form of <shifter_op>
455   Binary |= 1 << ARMII::I_BitShift;
456   Binary |= getMachineSoImmOpValue(ARM_AM::getSOImmVal(V2));
457   emitWordLE(Binary);
458 }
459
460 void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) {
461   // It's basically add r, pc, (LJTI - $+8)
462   
463   const TargetInstrDesc &TID = MI.getDesc();
464
465   // Emit the 'add' instruction.
466   unsigned Binary = 0x4 << 21;  // add: Insts{24-31} = 0b0100
467
468   // Set the conditional execution predicate
469   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
470
471   // Encode S bit if MI modifies CPSR.
472   Binary |= getAddrModeSBit(MI, TID);
473
474   // Encode Rd.
475   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
476
477   // Encode Rn which is PC.
478   Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
479
480   // Encode the displacement.
481   // Set bit I(25) to identify this is the immediate form of <shifter_op>.
482   Binary |= 1 << ARMII::I_BitShift;
483   emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
484
485   emitWordLE(Binary);
486 }
487
488 void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
489   DOUT << "  ** LPC" << LabelID << " @ "
490        << (void*)MCE.getCurrentPCValue() << '\n';
491   JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
492 }
493
494 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
495   unsigned Opcode = MI.getDesc().Opcode;
496   switch (Opcode) {
497   default:
498     abort(); // FIXME:
499   case ARM::CONSTPOOL_ENTRY:
500     emitConstPoolInstruction(MI);
501     break;
502   case ARM::PICADD: {
503     // Remember of the address of the PC label for relocation later.
504     addPCLabel(MI.getOperand(2).getImm());
505     // PICADD is just an add instruction that implicitly read pc.
506     emitDataProcessingInstruction(MI, 0, ARM::PC);
507     break;
508   }
509   case ARM::PICLDR:
510   case ARM::PICLDRB:
511   case ARM::PICSTR:
512   case ARM::PICSTRB: {
513     // Remember of the address of the PC label for relocation later.
514     addPCLabel(MI.getOperand(2).getImm());
515     // These are just load / store instructions that implicitly read pc.
516     emitLoadStoreInstruction(MI, 0, ARM::PC);
517     break;
518   }
519   case ARM::PICLDRH:
520   case ARM::PICLDRSH:
521   case ARM::PICLDRSB:
522   case ARM::PICSTRH: {
523     // Remember of the address of the PC label for relocation later.
524     addPCLabel(MI.getOperand(2).getImm());
525     // These are just load / store instructions that implicitly read pc.
526     emitMiscLoadStoreInstruction(MI, ARM::PC);
527     break;
528   }
529   case ARM::MOVi2pieces:
530     // Two instructions to materialize a constant.
531     emitMOVi2piecesInstruction(MI);
532     break;
533   case ARM::LEApcrelJT:
534     // Materialize jumptable address.
535     emitLEApcrelJTInstruction(MI);
536     break;
537   }
538 }
539
540
541 unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
542                                                 const TargetInstrDesc &TID,
543                                                 const MachineOperand &MO,
544                                                 unsigned OpIdx) {
545   unsigned Binary = getMachineOpValue(MI, MO);
546
547   const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
548   const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
549   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
550
551   // Encode the shift opcode.
552   unsigned SBits = 0;
553   unsigned Rs = MO1.getReg();
554   if (Rs) {
555     // Set shift operand (bit[7:4]).
556     // LSL - 0001
557     // LSR - 0011
558     // ASR - 0101
559     // ROR - 0111
560     // RRX - 0110 and bit[11:8] clear.
561     switch (SOpc) {
562     default: assert(0 && "Unknown shift opc!");
563     case ARM_AM::lsl: SBits = 0x1; break;
564     case ARM_AM::lsr: SBits = 0x3; break;
565     case ARM_AM::asr: SBits = 0x5; break;
566     case ARM_AM::ror: SBits = 0x7; break;
567     case ARM_AM::rrx: SBits = 0x6; break;
568     }
569   } else {
570     // Set shift operand (bit[6:4]).
571     // LSL - 000
572     // LSR - 010
573     // ASR - 100
574     // ROR - 110
575     switch (SOpc) {
576     default: assert(0 && "Unknown shift opc!");
577     case ARM_AM::lsl: SBits = 0x0; break;
578     case ARM_AM::lsr: SBits = 0x2; break;
579     case ARM_AM::asr: SBits = 0x4; break;
580     case ARM_AM::ror: SBits = 0x6; break;
581     }
582   }
583   Binary |= SBits << 4;
584   if (SOpc == ARM_AM::rrx)
585     return Binary;
586
587   // Encode the shift operation Rs or shift_imm (except rrx).
588   if (Rs) {
589     // Encode Rs bit[11:8].
590     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
591     return Binary |
592       (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
593   }
594
595   // Encode shift_imm bit[11:7].
596   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
597 }
598
599 unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) {
600   // Encode rotate_imm.
601   unsigned Binary = (ARM_AM::getSOImmValRot(SoImm) >> 1)
602     << ARMII::SoRotImmShift;
603
604   // Encode immed_8.
605   Binary |= ARM_AM::getSOImmValImm(SoImm);
606   return Binary;
607 }
608
609 unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
610                                          const TargetInstrDesc &TID) const {
611   for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
612     const MachineOperand &MO = MI.getOperand(i-1);
613     if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
614       return 1 << ARMII::S_BitShift;
615   }
616   return 0;
617 }
618
619 void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
620                                                    unsigned ImplicitRd,
621                                                    unsigned ImplicitRn) {
622   const TargetInstrDesc &TID = MI.getDesc();
623
624   // Part of binary is determined by TableGn.
625   unsigned Binary = getBinaryCodeForInstr(MI);
626
627   // Set the conditional execution predicate
628   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
629
630   // Encode S bit if MI modifies CPSR.
631   Binary |= getAddrModeSBit(MI, TID);
632
633   // Encode register def if there is one.
634   unsigned NumDefs = TID.getNumDefs();
635   unsigned OpIdx = 0;
636   if (NumDefs)
637     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
638   else if (ImplicitRd)
639     // Special handling for implicit use (e.g. PC).
640     Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
641                << ARMII::RegRdShift);
642
643   // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
644   if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
645     ++OpIdx;
646
647   // Encode first non-shifter register operand if there is one.
648   bool isUnary = TID.TSFlags & ARMII::UnaryDP;
649   if (!isUnary) {
650     if (ImplicitRn)
651       // Special handling for implicit use (e.g. PC).
652       Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
653                  << ARMII::RegRnShift);
654     else {
655       Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
656       ++OpIdx;
657     }
658   }
659
660   // Encode shifter operand.
661   const MachineOperand &MO = MI.getOperand(OpIdx);
662   if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
663     // Encode SoReg.
664     emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
665     return;
666   }
667
668   if (MO.isReg()) {
669     // Encode register Rm.
670     emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg()));
671     return;
672   }
673
674   // Encode so_imm.
675   // Set bit I(25) to identify this is the immediate form of <shifter_op>.
676   Binary |= 1 << ARMII::I_BitShift;
677   Binary |= getMachineSoImmOpValue(MO.getImm());
678
679   emitWordLE(Binary);
680 }
681
682 void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
683                                               unsigned ImplicitRd,
684                                               unsigned ImplicitRn) {
685   const TargetInstrDesc &TID = MI.getDesc();
686
687   // Part of binary is determined by TableGn.
688   unsigned Binary = getBinaryCodeForInstr(MI);
689
690   // Set the conditional execution predicate
691   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
692
693   // Set first operand
694   unsigned OpIdx = 0;
695   if (ImplicitRd)
696     // Special handling for implicit use (e.g. PC).
697     Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd)
698                << ARMII::RegRdShift);
699   else
700     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
701
702   // Set second operand
703   if (ImplicitRn)
704     // Special handling for implicit use (e.g. PC).
705     Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
706                << ARMII::RegRnShift);
707   else
708     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
709
710   // If this is a two-address operand, skip it. e.g. LDR_PRE.
711   if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
712     ++OpIdx;
713
714   const MachineOperand &MO2 = MI.getOperand(OpIdx);
715   unsigned AM2Opc = (ImplicitRn == ARM::PC)
716     ? 0 : MI.getOperand(OpIdx+1).getImm();
717
718   // Set bit U(23) according to sign of immed value (positive or negative).
719   Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
720              ARMII::U_BitShift);
721   if (!MO2.getReg()) { // is immediate
722     if (ARM_AM::getAM2Offset(AM2Opc))
723       // Set the value of offset_12 field
724       Binary |= ARM_AM::getAM2Offset(AM2Opc);
725     emitWordLE(Binary);
726     return;
727   }
728
729   // Set bit I(25), because this is not in immediate enconding.
730   Binary |= 1 << ARMII::I_BitShift;
731   assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
732   // Set bit[3:0] to the corresponding Rm register
733   Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
734
735   // if this instr is in scaled register offset/index instruction, set
736   // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
737   if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
738     Binary |= getShiftOp(AM2Opc) << 5;  // shift
739     Binary |= ShImm              << 7;  // shift_immed
740   }
741
742   emitWordLE(Binary);
743 }
744
745 void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
746                                                   unsigned ImplicitRn) {
747   const TargetInstrDesc &TID = MI.getDesc();
748
749   // Part of binary is determined by TableGn.
750   unsigned Binary = getBinaryCodeForInstr(MI);
751
752   // Set the conditional execution predicate
753   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
754
755   // Set first operand
756   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
757
758   // Set second operand
759   unsigned OpIdx = 1;
760   if (ImplicitRn)
761     // Special handling for implicit use (e.g. PC).
762     Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn)
763                << ARMII::RegRnShift);
764   else
765     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
766
767   // If this is a two-address operand, skip it. e.g. LDRH_POST.
768   if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
769     ++OpIdx;
770
771   const MachineOperand &MO2 = MI.getOperand(OpIdx);
772   unsigned AM3Opc = (ImplicitRn == ARM::PC)
773     ? 0 : MI.getOperand(OpIdx+1).getImm();
774
775   // Set bit U(23) according to sign of immed value (positive or negative)
776   Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
777              ARMII::U_BitShift);
778
779   // If this instr is in register offset/index encoding, set bit[3:0]
780   // to the corresponding Rm register.
781   if (MO2.getReg()) {
782     Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
783     emitWordLE(Binary);
784     return;
785   }
786
787   // This instr is in immediate offset/index encoding, set bit 22 to 1.
788   Binary |= 1 << ARMII::AM3_I_BitShift;
789   if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
790     // Set operands
791     Binary |= (ImmOffs >> 4) << 8;  // immedH
792     Binary |= (ImmOffs & ~0xF);     // immedL
793   }
794
795   emitWordLE(Binary);
796 }
797
798 static unsigned getAddrModeUPBits(unsigned Mode) {
799   unsigned Binary = 0;
800
801   // Set addressing mode by modifying bits U(23) and P(24)
802   // IA - Increment after  - bit U = 1 and bit P = 0
803   // IB - Increment before - bit U = 1 and bit P = 1
804   // DA - Decrement after  - bit U = 0 and bit P = 0
805   // DB - Decrement before - bit U = 0 and bit P = 1
806   switch (Mode) {
807   default: assert(0 && "Unknown addressing sub-mode!");
808   case ARM_AM::da:                      break;
809   case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
810   case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
811   case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
812   }
813
814   return Binary;
815 }
816
817 void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
818   // Part of binary is determined by TableGn.
819   unsigned Binary = getBinaryCodeForInstr(MI);
820
821   // Set the conditional execution predicate
822   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
823
824   // Set base address operand
825   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
826
827   // Set addressing mode by modifying bits U(23) and P(24)
828   const MachineOperand &MO = MI.getOperand(1);
829   Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm()));
830
831   // Set bit W(21)
832   if (ARM_AM::getAM4WBFlag(MO.getImm()))
833     Binary |= 0x1 << ARMII::W_BitShift;
834
835   // Set registers
836   for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
837     const MachineOperand &MO = MI.getOperand(i);
838     if (!MO.isReg() || MO.isImplicit())
839       break;
840     unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
841     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
842            RegNum < 16);
843     Binary |= 0x1 << RegNum;
844   }
845
846   emitWordLE(Binary);
847 }
848
849 void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
850   const TargetInstrDesc &TID = MI.getDesc();
851
852   // Part of binary is determined by TableGn.
853   unsigned Binary = getBinaryCodeForInstr(MI);
854
855   // Set the conditional execution predicate
856   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
857
858   // Encode S bit if MI modifies CPSR.
859   Binary |= getAddrModeSBit(MI, TID);
860
861   // 32x32->64bit operations have two destination registers. The number
862   // of register definitions will tell us if that's what we're dealing with.
863   unsigned OpIdx = 0;
864   if (TID.getNumDefs() == 2)
865     Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
866
867   // Encode Rd
868   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
869
870   // Encode Rm
871   Binary |= getMachineOpValue(MI, OpIdx++);
872
873   // Encode Rs
874   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
875
876   // Many multiple instructions (e.g. MLA) have three src operands. Encode
877   // it as Rn (for multiply, that's in the same offset as RdLo.
878   if (TID.getNumOperands() > OpIdx &&
879       !TID.OpInfo[OpIdx].isPredicate() &&
880       !TID.OpInfo[OpIdx].isOptionalDef())
881     Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
882
883   emitWordLE(Binary);
884 }
885
886 void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) {
887   const TargetInstrDesc &TID = MI.getDesc();
888
889   // Part of binary is determined by TableGn.
890   unsigned Binary = getBinaryCodeForInstr(MI);
891
892   // Set the conditional execution predicate
893   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
894
895   unsigned OpIdx = 0;
896
897   // Encode Rd
898   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
899
900   const MachineOperand &MO1 = MI.getOperand(OpIdx++);
901   const MachineOperand &MO2 = MI.getOperand(OpIdx);
902   if (MO2.isReg()) {
903     // Two register operand form.
904     // Encode Rn.
905     Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
906
907     // Encode Rm.
908     Binary |= getMachineOpValue(MI, MO2);
909     ++OpIdx;
910   } else {
911     Binary |= getMachineOpValue(MI, MO1);
912   }
913
914   // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
915   if (MI.getOperand(OpIdx).isImm() &&
916       !TID.OpInfo[OpIdx].isPredicate() &&
917       !TID.OpInfo[OpIdx].isOptionalDef())
918     Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
919
920   emitWordLE(Binary);
921 }
922
923 void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) {
924   const TargetInstrDesc &TID = MI.getDesc();
925
926   // Part of binary is determined by TableGn.
927   unsigned Binary = getBinaryCodeForInstr(MI);
928
929   // Set the conditional execution predicate
930   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
931
932   unsigned OpIdx = 0;
933
934   // Encode Rd
935   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
936
937   const MachineOperand &MO = MI.getOperand(OpIdx++);
938   if (OpIdx == TID.getNumOperands() ||
939       TID.OpInfo[OpIdx].isPredicate() ||
940       TID.OpInfo[OpIdx].isOptionalDef()) {
941     // Encode Rm and it's done.
942     Binary |= getMachineOpValue(MI, MO);
943     emitWordLE(Binary);
944     return;
945   }
946
947   // Encode Rn.
948   Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
949
950   // Encode Rm.
951   Binary |= getMachineOpValue(MI, OpIdx++);
952
953   // Encode shift_imm.
954   unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
955   assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
956   Binary |= ShiftAmt << ARMII::ShiftShift;
957   
958   emitWordLE(Binary);
959 }
960
961 void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
962   const TargetInstrDesc &TID = MI.getDesc();
963
964   if (TID.Opcode == ARM::TPsoft)
965     abort(); // FIXME
966
967   // Part of binary is determined by TableGn.
968   unsigned Binary = getBinaryCodeForInstr(MI);
969
970   // Set the conditional execution predicate
971   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
972
973   // Set signed_immed_24 field
974   Binary |= getMachineOpValue(MI, 0);
975
976   emitWordLE(Binary);
977 }
978
979 void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) {
980   // Remember the base address of the inline jump table.
981   intptr_t JTBase = MCE.getCurrentPCValue();
982   JTI->addJumpTableBaseAddr(JTIndex, JTBase);
983   DOUT << "  ** Jump Table #" << JTIndex << " @ " << (void*)JTBase << '\n';
984
985   // Now emit the jump table entries.
986   const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
987   for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
988     if (IsPIC)
989       // DestBB address - JT base.
990       emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
991     else
992       // Absolute DestBB address.
993       emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
994     emitWordLE(0);
995   }
996 }
997
998 void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
999   const TargetInstrDesc &TID = MI.getDesc();
1000
1001   // Handle jump tables.
1002   if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) {
1003     // First emit a ldr pc, [] instruction.
1004     emitDataProcessingInstruction(MI, ARM::PC);
1005
1006     // Then emit the inline jump table.
1007     unsigned JTIndex = (TID.Opcode == ARM::BR_JTr)
1008       ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1009     emitInlineJumpTable(JTIndex);
1010     return;
1011   } else if (TID.Opcode == ARM::BR_JTm) {
1012     // First emit a ldr pc, [] instruction.
1013     emitLoadStoreInstruction(MI, ARM::PC);
1014
1015     // Then emit the inline jump table.
1016     emitInlineJumpTable(MI.getOperand(3).getIndex());
1017     return;
1018   }
1019
1020   // Part of binary is determined by TableGn.
1021   unsigned Binary = getBinaryCodeForInstr(MI);
1022
1023   // Set the conditional execution predicate
1024   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1025
1026   if (TID.Opcode == ARM::BX_RET)
1027     // The return register is LR.
1028     Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR);
1029   else 
1030     // otherwise, set the return register
1031     Binary |= getMachineOpValue(MI, 0);
1032
1033   emitWordLE(Binary);
1034 }
1035
1036 void ARMCodeEmitter::emitVFPArithInstruction(const MachineInstr &MI) {
1037   const TargetInstrDesc &TID = MI.getDesc();
1038
1039   // Part of binary is determined by TableGn.
1040   unsigned Binary = getBinaryCodeForInstr(MI);
1041
1042   // Set the conditional execution predicate
1043   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1044
1045   unsigned OpIdx = 0;
1046   assert((Binary & ARMII::D_BitShift) == 0 &&
1047          (Binary & ARMII::N_BitShift) == 0 &&
1048          (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1049
1050   // Encode Dd / Sd.
1051   unsigned RegD = getMachineOpValue(MI, OpIdx++);
1052   Binary |= (RegD & 0x0f) << ARMII::RegRdShift;
1053   Binary |= (RegD & 0x10) << ARMII::D_BitShift;
1054
1055   // If this is a two-address operand, skip it, e.g. FMACD.
1056   if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1057     ++OpIdx;
1058
1059   // Encode Dn / Sn.
1060   if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm) {
1061     unsigned RegN = getMachineOpValue(MI, OpIdx++);
1062     Binary |= (RegN & 0x0f) << ARMII::RegRnShift;
1063     Binary |= (RegN & 0x10) << ARMII::N_BitShift;
1064   }
1065
1066   // Encode Dm / Sm.
1067   unsigned RegM = getMachineOpValue(MI, OpIdx++);
1068   Binary |= (RegM & 0x0f);
1069   Binary |= (RegM & 0x10) << ARMII::M_BitShift;
1070   
1071   emitWordLE(Binary);
1072 }
1073
1074 void ARMCodeEmitter::emitVFPConversionInstruction(const MachineInstr &MI) {
1075   const TargetInstrDesc &TID = MI.getDesc();
1076
1077   // Part of binary is determined by TableGn.
1078   unsigned Binary = getBinaryCodeForInstr(MI);
1079
1080   // Set the conditional execution predicate
1081   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1082
1083   unsigned OpIdx = 0;
1084
1085   // Encode Dd / Sd.
1086   unsigned RegD = getMachineOpValue(MI, OpIdx++);
1087   Binary |= (RegD & 0x0f) << ARMII::RegRdShift;
1088   Binary |= (RegD & 0x10) << ARMII::D_BitShift;
1089
1090   // Encode Dn / Sn.
1091   if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPConv1Frm) {
1092     unsigned RegN = getMachineOpValue(MI, OpIdx++);
1093     Binary |= (RegN & 0x0f) << ARMII::RegRnShift;
1094     Binary |= (RegN & 0x10) << ARMII::N_BitShift;
1095
1096     // FMRS / FMSR do not have Rm.
1097     if (!TID.OpInfo[2].isPredicate()) {
1098       unsigned RegM = getMachineOpValue(MI, OpIdx++);
1099       Binary |= (RegM & 0x0f);
1100       Binary |= (RegM & 0x10) << ARMII::M_BitShift;
1101     }
1102   } else {
1103     unsigned RegM = getMachineOpValue(MI, OpIdx++);
1104     Binary |= (RegM & 0x0f);
1105     Binary |= (RegM & 0x10) << ARMII::M_BitShift;
1106   }
1107
1108   emitWordLE(Binary);
1109 }
1110
1111 void ARMCodeEmitter::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
1112   // Part of binary is determined by TableGn.
1113   unsigned Binary = getBinaryCodeForInstr(MI);
1114
1115   // Set the conditional execution predicate
1116   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1117
1118   unsigned OpIdx = 0;
1119
1120   // Encode Dd / Sd.
1121   unsigned RegD = getMachineOpValue(MI, OpIdx++);
1122   Binary |= (RegD & 0x0f) << ARMII::RegRdShift;
1123   Binary |= (RegD & 0x10) << ARMII::D_BitShift;
1124
1125   // Encode address base.
1126   const MachineOperand &Base = MI.getOperand(OpIdx++);
1127   Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1128
1129   // If there is a non-zero immediate offset, encode it.
1130   if (Base.isReg()) {
1131     const MachineOperand &Offset = MI.getOperand(OpIdx);
1132     if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1133       if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1134         Binary |= 1 << ARMII::U_BitShift;
1135       // Immediate offset is multiplied by 4.
1136       Binary |= ImmOffs >> 2;
1137       emitWordLE(Binary);
1138       return;
1139     }
1140   }
1141
1142   // If immediate offset is omitted, default to +0.
1143   Binary |= 1 << ARMII::U_BitShift;
1144
1145   emitWordLE(Binary);
1146 }
1147
1148 void
1149 ARMCodeEmitter::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) {
1150   // Part of binary is determined by TableGn.
1151   unsigned Binary = getBinaryCodeForInstr(MI);
1152
1153   // Set the conditional execution predicate
1154   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1155
1156   // Set base address operand
1157   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
1158
1159   // Set addressing mode by modifying bits U(23) and P(24)
1160   const MachineOperand &MO = MI.getOperand(1);
1161   Binary |= getAddrModeUPBits(ARM_AM::getAM5SubMode(MO.getImm()));
1162
1163   // Set bit W(21)
1164   if (ARM_AM::getAM5WBFlag(MO.getImm()))
1165     Binary |= 0x1 << ARMII::W_BitShift;
1166
1167   // First register is encoded in Dd.
1168   unsigned FirstReg = MI.getOperand(4).getReg();
1169   Binary |= ARMRegisterInfo::getRegisterNumbering(FirstReg)<< ARMII::RegRdShift;
1170
1171   // Number of registers are encoded in offset field.
1172   unsigned NumRegs = 1;
1173   for (unsigned i = 5, e = MI.getNumOperands(); i != e; ++i) {
1174     const MachineOperand &MO = MI.getOperand(i);
1175     if (!MO.isReg() || MO.isImplicit())
1176       break;
1177     ++NumRegs;
1178   }
1179   Binary |= NumRegs * 2;
1180
1181   emitWordLE(Binary);
1182 }
1183
1184 void ARMCodeEmitter::emitMiscInstruction(const MachineInstr &MI) {
1185   // Part of binary is determined by TableGn.
1186   unsigned Binary = getBinaryCodeForInstr(MI);
1187
1188   // Set the conditional execution predicate
1189   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1190
1191   emitWordLE(Binary);
1192 }
1193
1194 #include "ARMGenCodeEmitter.inc"