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