Simplify the EncodeInstruction method now that a lot of the special case stuff
[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/JITCodeEmitter.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/MachineModuleInfo.h"
33 #include "llvm/CodeGen/Passes.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/raw_ostream.h"
38 #ifndef NDEBUG
39 #include <iomanip>
40 #endif
41 using namespace llvm;
42
43 STATISTIC(NumEmitted, "Number of machine instructions emitted");
44
45 namespace {
46
47   class ARMCodeEmitter : public MachineFunctionPass {
48     ARMJITInfo                *JTI;
49     const ARMInstrInfo        *II;
50     const TargetData          *TD;
51     const ARMSubtarget        *Subtarget;
52     TargetMachine             &TM;
53     JITCodeEmitter            &MCE;
54     MachineModuleInfo *MMI;
55     const std::vector<MachineConstantPoolEntry> *MCPEs;
56     const std::vector<MachineJumpTableEntry> *MJTEs;
57     bool IsPIC;
58     bool IsThumb;
59
60     void getAnalysisUsage(AnalysisUsage &AU) const {
61       AU.addRequired<MachineModuleInfo>();
62       MachineFunctionPass::getAnalysisUsage(AU);
63     }
64
65     static char ID;
66   public:
67     ARMCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
68       : MachineFunctionPass(ID), JTI(0),
69         II((const ARMInstrInfo *)tm.getInstrInfo()),
70         TD(tm.getTargetData()), TM(tm),
71         MCE(mce), MCPEs(0), MJTEs(0),
72         IsPIC(TM.getRelocationModel() == Reloc::PIC_), IsThumb(false) {}
73
74     /// getBinaryCodeForInstr - This function, generated by the
75     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
76     /// machine instructions.
77     unsigned getBinaryCodeForInstr(const MachineInstr &MI) const;
78
79     bool runOnMachineFunction(MachineFunction &MF);
80
81     virtual const char *getPassName() const {
82       return "ARM Machine Code Emitter";
83     }
84
85     void emitInstruction(const MachineInstr &MI);
86
87   private:
88
89     void emitWordLE(unsigned Binary);
90     void emitDWordLE(uint64_t Binary);
91     void emitConstPoolInstruction(const MachineInstr &MI);
92     void emitMOVi32immInstruction(const MachineInstr &MI);
93     void emitMOVi2piecesInstruction(const MachineInstr &MI);
94     void emitLEApcrelJTInstruction(const MachineInstr &MI);
95     void emitPseudoMoveInstruction(const MachineInstr &MI);
96     void addPCLabel(unsigned LabelID);
97     void emitPseudoInstruction(const MachineInstr &MI);
98     unsigned getMachineSoRegOpValue(const MachineInstr &MI,
99                                     const TargetInstrDesc &TID,
100                                     const MachineOperand &MO,
101                                     unsigned OpIdx);
102
103     unsigned getMachineSoImmOpValue(unsigned SoImm);
104     unsigned getAddrModeSBit(const MachineInstr &MI,
105                              const TargetInstrDesc &TID) const;
106
107     void emitDataProcessingInstruction(const MachineInstr &MI,
108                                        unsigned ImplicitRd = 0,
109                                        unsigned ImplicitRn = 0);
110
111     void emitLoadStoreInstruction(const MachineInstr &MI,
112                                   unsigned ImplicitRd = 0,
113                                   unsigned ImplicitRn = 0);
114
115     void emitMiscLoadStoreInstruction(const MachineInstr &MI,
116                                       unsigned ImplicitRn = 0);
117
118     void emitLoadStoreMultipleInstruction(const MachineInstr &MI);
119
120     void emitMulFrmInstruction(const MachineInstr &MI);
121
122     void emitExtendInstruction(const MachineInstr &MI);
123
124     void emitMiscArithInstruction(const MachineInstr &MI);
125
126     void emitSaturateInstruction(const MachineInstr &MI);
127
128     void emitBranchInstruction(const MachineInstr &MI);
129
130     void emitInlineJumpTable(unsigned JTIndex);
131
132     void emitMiscBranchInstruction(const MachineInstr &MI);
133
134     void emitVFPArithInstruction(const MachineInstr &MI);
135
136     void emitVFPConversionInstruction(const MachineInstr &MI);
137
138     void emitVFPLoadStoreInstruction(const MachineInstr &MI);
139
140     void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI);
141
142     void emitNEONLaneInstruction(const MachineInstr &MI);
143     void emitNEONDupInstruction(const MachineInstr &MI);
144     void emitNEON1RegModImmInstruction(const MachineInstr &MI);
145     void emitNEON2RegInstruction(const MachineInstr &MI);
146     void emitNEON3RegInstruction(const MachineInstr &MI);
147
148     /// getMachineOpValue - Return binary encoding of operand. If the machine
149     /// operand requires relocation, record the relocation and return zero.
150     unsigned getMachineOpValue(const MachineInstr &MI,
151                                const MachineOperand &MO) const;
152     unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) const {
153       return getMachineOpValue(MI, MI.getOperand(OpIdx));
154     }
155
156     // FIXME: The legacy JIT ARMCodeEmitter doesn't rely on the the
157     //  TableGen'erated getBinaryCodeForInstr() function to encode any
158     //  operand values, instead querying getMachineOpValue() directly for
159     //  each operand it needs to encode. Thus, any of the new encoder
160     //  helper functions can simply return 0 as the values the return
161     //  are already handled elsewhere. They are placeholders to allow this
162     //  encoder to continue to function until the MC encoder is sufficiently
163     //  far along that this one can be eliminated entirely.
164     unsigned getCCOutOpValue(const MachineInstr &MI, unsigned Op)
165       const { return 0; }
166     unsigned getSOImmOpValue(const MachineInstr &MI, unsigned Op)
167       const { return 0; }
168     unsigned getSORegOpValue(const MachineInstr &MI, unsigned Op)
169       const { return 0; }
170     unsigned getRotImmOpValue(const MachineInstr &MI, unsigned Op)
171       const { return 0; }
172     unsigned getImmMinusOneOpValue(const MachineInstr &MI, unsigned Op)
173       const { return 0; }
174     unsigned getAddrMode6AddressOpValue(const MachineInstr &MI, unsigned Op)
175       const { return 0; }
176     unsigned getAddrMode6OffsetOpValue(const MachineInstr &MI, unsigned Op)
177       const { return 0; }
178     unsigned getBitfieldInvertedMaskOpValue(const MachineInstr &MI,
179                                             unsigned Op) const { return 0; }
180     uint32_t getAddrModeImmOpValue(const MachineInstr &MI, unsigned Op) const {
181       // {20-17} = reg
182       // {16}    = (U)nsigned (add == '1', sub == '0')
183       // {15-0}  = imm
184       const MachineOperand &MO  = MI.getOperand(Op);
185       const MachineOperand &MO1 = MI.getOperand(Op + 1);
186       if (!MO.isReg()) {
187         emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry);
188         return 0;
189       }
190
191       unsigned Reg = getARMRegisterNumbering(MO.getReg());
192       int32_t Imm = MO1.getImm();
193       uint32_t Binary;
194       Binary = Imm & 0xffff;
195       if (Imm >= 0)
196         Binary |= (1 << 16);
197
198       Binary |= (Reg << 17);
199       return Binary;
200     }
201     unsigned getNEONVcvtImm32OpValue(const MachineInstr &MI, unsigned Op)
202       const { return 0; }
203
204     unsigned getRegisterListOpValue(const MachineInstr &MI, unsigned Op)
205       const { return 0; }
206
207     /// getMovi32Value - Return binary encoding of operand for movw/movt. If the
208     /// machine operand requires relocation, record the relocation and return
209     /// zero.
210     unsigned getMovi32Value(const MachineInstr &MI,const MachineOperand &MO,
211                             unsigned Reloc);
212
213     /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
214     ///
215     unsigned getShiftOp(unsigned Imm) const ;
216
217     /// Routines that handle operands which add machine relocations which are
218     /// fixed up by the relocation stage.
219     void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
220                            bool MayNeedFarStub,  bool Indirect,
221                            intptr_t ACPV = 0) const;
222     void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
223     void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
224     void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const;
225     void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
226                                intptr_t JTBase = 0) const;
227   };
228 }
229
230 char ARMCodeEmitter::ID = 0;
231
232 /// createARMJITCodeEmitterPass - Return a pass that emits the collected ARM
233 /// code to the specified MCE object.
234 FunctionPass *llvm::createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM,
235                                                 JITCodeEmitter &JCE) {
236   return new ARMCodeEmitter(TM, JCE);
237 }
238
239 bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
240   assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
241           MF.getTarget().getRelocationModel() != Reloc::Static) &&
242          "JIT relocation model must be set to static or default!");
243   JTI = ((ARMTargetMachine &)MF.getTarget()).getJITInfo();
244   II = ((const ARMTargetMachine &)MF.getTarget()).getInstrInfo();
245   TD = ((const ARMTargetMachine &)MF.getTarget()).getTargetData();
246   Subtarget = &TM.getSubtarget<ARMSubtarget>();
247   MCPEs = &MF.getConstantPool()->getConstants();
248   MJTEs = 0;
249   if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
250   IsPIC = TM.getRelocationModel() == Reloc::PIC_;
251   IsThumb = MF.getInfo<ARMFunctionInfo>()->isThumbFunction();
252   JTI->Initialize(MF, IsPIC);
253   MMI = &getAnalysis<MachineModuleInfo>();
254   MCE.setModuleInfo(MMI);
255
256   do {
257     DEBUG(errs() << "JITTing function '"
258           << MF.getFunction()->getName() << "'\n");
259     MCE.startFunction(MF);
260     for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
261          MBB != E; ++MBB) {
262       MCE.StartMachineBasicBlock(MBB);
263       for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
264            I != E; ++I)
265         emitInstruction(*I);
266     }
267   } while (MCE.finishFunction(MF));
268
269   return false;
270 }
271
272 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
273 ///
274 unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const {
275   switch (ARM_AM::getAM2ShiftOpc(Imm)) {
276   default: llvm_unreachable("Unknown shift opc!");
277   case ARM_AM::asr: return 2;
278   case ARM_AM::lsl: return 0;
279   case ARM_AM::lsr: return 1;
280   case ARM_AM::ror:
281   case ARM_AM::rrx: return 3;
282   }
283   return 0;
284 }
285
286 /// getMovi32Value - Return binary encoding of operand for movw/movt. If the
287 /// machine operand requires relocation, record the relocation and return zero.
288 unsigned ARMCodeEmitter::getMovi32Value(const MachineInstr &MI,
289                                         const MachineOperand &MO,
290                                         unsigned Reloc) {
291   assert(((Reloc == ARM::reloc_arm_movt) || (Reloc == ARM::reloc_arm_movw))
292       && "Relocation to this function should be for movt or movw");
293
294   if (MO.isImm())
295     return static_cast<unsigned>(MO.getImm());
296   else if (MO.isGlobal())
297     emitGlobalAddress(MO.getGlobal(), Reloc, true, false);
298   else if (MO.isSymbol())
299     emitExternalSymbolAddress(MO.getSymbolName(), Reloc);
300   else if (MO.isMBB())
301     emitMachineBasicBlock(MO.getMBB(), Reloc);
302   else {
303 #ifndef NDEBUG
304     errs() << MO;
305 #endif
306     llvm_unreachable("Unsupported operand type for movw/movt");
307   }
308   return 0;
309 }
310
311 /// getMachineOpValue - Return binary encoding of operand. If the machine
312 /// operand requires relocation, record the relocation and return zero.
313 unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
314                                            const MachineOperand &MO) const {
315   if (MO.isReg())
316     return getARMRegisterNumbering(MO.getReg());
317   else if (MO.isImm())
318     return static_cast<unsigned>(MO.getImm());
319   else if (MO.isGlobal())
320     emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true, false);
321   else if (MO.isSymbol())
322     emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch);
323   else if (MO.isCPI()) {
324     const TargetInstrDesc &TID = MI.getDesc();
325     // For VFP load, the immediate offset is multiplied by 4.
326     unsigned Reloc =  ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm)
327       ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry;
328     emitConstPoolAddress(MO.getIndex(), Reloc);
329   } else if (MO.isJTI())
330     emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
331   else if (MO.isMBB())
332     emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch);
333   else {
334 #ifndef NDEBUG
335     errs() << MO;
336 #endif
337     llvm_unreachable(0);
338   }
339   return 0;
340 }
341
342 /// emitGlobalAddress - Emit the specified address to the code stream.
343 ///
344 void ARMCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
345                                        bool MayNeedFarStub, bool Indirect,
346                                        intptr_t ACPV) const {
347   MachineRelocation MR = Indirect
348     ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
349                                            const_cast<GlobalValue *>(GV),
350                                            ACPV, MayNeedFarStub)
351     : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
352                                const_cast<GlobalValue *>(GV), ACPV,
353                                MayNeedFarStub);
354   MCE.addRelocation(MR);
355 }
356
357 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
358 /// be emitted to the current location in the function, and allow it to be PC
359 /// relative.
360 void ARMCodeEmitter::
361 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
362   MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
363                                                  Reloc, ES));
364 }
365
366 /// emitConstPoolAddress - Arrange for the address of an constant pool
367 /// to be emitted to the current location in the function, and allow it to be PC
368 /// relative.
369 void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
370   // Tell JIT emitter we'll resolve the address.
371   MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
372                                                     Reloc, CPI, 0, true));
373 }
374
375 /// emitJumpTableAddress - Arrange for the address of a jump table to
376 /// be emitted to the current location in the function, and allow it to be PC
377 /// relative.
378 void ARMCodeEmitter::
379 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
380   MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
381                                                     Reloc, JTIndex, 0, true));
382 }
383
384 /// emitMachineBasicBlock - Emit the specified address basic block.
385 void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
386                                            unsigned Reloc,
387                                            intptr_t JTBase) const {
388   MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
389                                              Reloc, BB, JTBase));
390 }
391
392 void ARMCodeEmitter::emitWordLE(unsigned Binary) {
393   DEBUG(errs() << "  0x";
394         errs().write_hex(Binary) << "\n");
395   MCE.emitWordLE(Binary);
396 }
397
398 void ARMCodeEmitter::emitDWordLE(uint64_t Binary) {
399   DEBUG(errs() << "  0x";
400         errs().write_hex(Binary) << "\n");
401   MCE.emitDWordLE(Binary);
402 }
403
404 void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
405   DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
406
407   MCE.processDebugLoc(MI.getDebugLoc(), true);
408
409   ++NumEmitted;  // Keep track of the # of mi's emitted
410   switch (MI.getDesc().TSFlags & ARMII::FormMask) {
411   default: {
412     llvm_unreachable("Unhandled instruction encoding format!");
413     break;
414   }
415   case ARMII::Pseudo:
416     emitPseudoInstruction(MI);
417     break;
418   case ARMII::DPFrm:
419   case ARMII::DPSoRegFrm:
420     emitDataProcessingInstruction(MI);
421     break;
422   case ARMII::LdFrm:
423   case ARMII::StFrm:
424     emitLoadStoreInstruction(MI);
425     break;
426   case ARMII::LdMiscFrm:
427   case ARMII::StMiscFrm:
428     emitMiscLoadStoreInstruction(MI);
429     break;
430   case ARMII::LdStMulFrm:
431     emitLoadStoreMultipleInstruction(MI);
432     break;
433   case ARMII::MulFrm:
434     emitMulFrmInstruction(MI);
435     break;
436   case ARMII::ExtFrm:
437     emitExtendInstruction(MI);
438     break;
439   case ARMII::ArithMiscFrm:
440     emitMiscArithInstruction(MI);
441     break;
442   case ARMII::SatFrm:
443     emitSaturateInstruction(MI);
444     break;
445   case ARMII::BrFrm:
446     emitBranchInstruction(MI);
447     break;
448   case ARMII::BrMiscFrm:
449     emitMiscBranchInstruction(MI);
450     break;
451   // VFP instructions.
452   case ARMII::VFPUnaryFrm:
453   case ARMII::VFPBinaryFrm:
454     emitVFPArithInstruction(MI);
455     break;
456   case ARMII::VFPConv1Frm:
457   case ARMII::VFPConv2Frm:
458   case ARMII::VFPConv3Frm:
459   case ARMII::VFPConv4Frm:
460   case ARMII::VFPConv5Frm:
461     emitVFPConversionInstruction(MI);
462     break;
463   case ARMII::VFPLdStFrm:
464     emitVFPLoadStoreInstruction(MI);
465     break;
466   case ARMII::VFPLdStMulFrm:
467     emitVFPLoadStoreMultipleInstruction(MI);
468     break;
469
470   // NEON instructions.
471   case ARMII::NGetLnFrm:
472   case ARMII::NSetLnFrm:
473     emitNEONLaneInstruction(MI);
474     break;
475   case ARMII::NDupFrm:
476     emitNEONDupInstruction(MI);
477     break;
478   case ARMII::N1RegModImmFrm:
479     emitNEON1RegModImmInstruction(MI);
480     break;
481   case ARMII::N2RegFrm:
482     emitNEON2RegInstruction(MI);
483     break;
484   case ARMII::N3RegFrm:
485     emitNEON3RegInstruction(MI);
486     break;
487   }
488   MCE.processDebugLoc(MI.getDebugLoc(), false);
489 }
490
491 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
492   unsigned CPI = MI.getOperand(0).getImm();       // CP instruction index.
493   unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
494   const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
495
496   // Remember the CONSTPOOL_ENTRY address for later relocation.
497   JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
498
499   // Emit constpool island entry. In most cases, the actual values will be
500   // resolved and relocated after code emission.
501   if (MCPE.isMachineConstantPoolEntry()) {
502     ARMConstantPoolValue *ACPV =
503       static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
504
505     DEBUG(errs() << "  ** ARM constant pool #" << CPI << " @ "
506           << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n');
507
508     assert(ACPV->isGlobalValue() && "unsupported constant pool value");
509     const GlobalValue *GV = ACPV->getGV();
510     if (GV) {
511       Reloc::Model RelocM = TM.getRelocationModel();
512       emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry,
513                         isa<Function>(GV),
514                         Subtarget->GVIsIndirectSymbol(GV, RelocM),
515                         (intptr_t)ACPV);
516      } else  {
517       emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
518     }
519     emitWordLE(0);
520   } else {
521     const Constant *CV = MCPE.Val.ConstVal;
522
523     DEBUG({
524         errs() << "  ** Constant pool #" << CPI << " @ "
525                << (void*)MCE.getCurrentPCValue() << " ";
526         if (const Function *F = dyn_cast<Function>(CV))
527           errs() << F->getName();
528         else
529           errs() << *CV;
530         errs() << '\n';
531       });
532
533     if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
534       emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV), false);
535       emitWordLE(0);
536     } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
537       uint32_t Val = uint32_t(*CI->getValue().getRawData());
538       emitWordLE(Val);
539     } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
540       if (CFP->getType()->isFloatTy())
541         emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
542       else if (CFP->getType()->isDoubleTy())
543         emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
544       else {
545         llvm_unreachable("Unable to handle this constantpool entry!");
546       }
547     } else {
548       llvm_unreachable("Unable to handle this constantpool entry!");
549     }
550   }
551 }
552
553 void ARMCodeEmitter::emitMOVi32immInstruction(const MachineInstr &MI) {
554   const MachineOperand &MO0 = MI.getOperand(0);
555   const MachineOperand &MO1 = MI.getOperand(1);
556
557   // Emit the 'movw' instruction.
558   unsigned Binary = 0x30 << 20;  // mov: Insts{27-20} = 0b00110000
559
560   unsigned Lo16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movw) & 0xFFFF;
561
562   // Set the conditional execution predicate.
563   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
564
565   // Encode Rd.
566   Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
567
568   // Encode imm16 as imm4:imm12
569   Binary |= Lo16 & 0xFFF; // Insts{11-0} = imm12
570   Binary |= ((Lo16 >> 12) & 0xF) << 16; // Insts{19-16} = imm4
571   emitWordLE(Binary);
572
573   unsigned Hi16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movt) >> 16;
574   // Emit the 'movt' instruction.
575   Binary = 0x34 << 20; // movt: Insts{27-20} = 0b00110100
576
577   // Set the conditional execution predicate.
578   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
579
580   // Encode Rd.
581   Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
582
583   // Encode imm16 as imm4:imm1, same as movw above.
584   Binary |= Hi16 & 0xFFF;
585   Binary |= ((Hi16 >> 12) & 0xF) << 16;
586   emitWordLE(Binary);
587 }
588
589 void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) {
590   const MachineOperand &MO0 = MI.getOperand(0);
591   const MachineOperand &MO1 = MI.getOperand(1);
592   assert(MO1.isImm() && ARM_AM::isSOImmTwoPartVal(MO1.getImm()) &&
593                                                   "Not a valid so_imm value!");
594   unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm());
595   unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm());
596
597   // Emit the 'mov' instruction.
598   unsigned Binary = 0xd << 21;  // mov: Insts{24-21} = 0b1101
599
600   // Set the conditional execution predicate.
601   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
602
603   // Encode Rd.
604   Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
605
606   // Encode so_imm.
607   // Set bit I(25) to identify this is the immediate form of <shifter_op>
608   Binary |= 1 << ARMII::I_BitShift;
609   Binary |= getMachineSoImmOpValue(V1);
610   emitWordLE(Binary);
611
612   // Now the 'orr' instruction.
613   Binary = 0xc << 21;  // orr: Insts{24-21} = 0b1100
614
615   // Set the conditional execution predicate.
616   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
617
618   // Encode Rd.
619   Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift;
620
621   // Encode Rn.
622   Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift;
623
624   // Encode so_imm.
625   // Set bit I(25) to identify this is the immediate form of <shifter_op>
626   Binary |= 1 << ARMII::I_BitShift;
627   Binary |= getMachineSoImmOpValue(V2);
628   emitWordLE(Binary);
629 }
630
631 void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) {
632   // It's basically add r, pc, (LJTI - $+8)
633
634   const TargetInstrDesc &TID = MI.getDesc();
635
636   // Emit the 'add' instruction.
637   unsigned Binary = 0x4 << 21;  // add: Insts{24-31} = 0b0100
638
639   // Set the conditional execution predicate
640   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
641
642   // Encode S bit if MI modifies CPSR.
643   Binary |= getAddrModeSBit(MI, TID);
644
645   // Encode Rd.
646   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
647
648   // Encode Rn which is PC.
649   Binary |= getARMRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
650
651   // Encode the displacement.
652   Binary |= 1 << ARMII::I_BitShift;
653   emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base);
654
655   emitWordLE(Binary);
656 }
657
658 void ARMCodeEmitter::emitPseudoMoveInstruction(const MachineInstr &MI) {
659   unsigned Opcode = MI.getDesc().Opcode;
660
661   // Part of binary is determined by TableGn.
662   unsigned Binary = getBinaryCodeForInstr(MI);
663
664   // Set the conditional execution predicate
665   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
666
667   // Encode S bit if MI modifies CPSR.
668   if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag)
669     Binary |= 1 << ARMII::S_BitShift;
670
671   // Encode register def if there is one.
672   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
673
674   // Encode the shift operation.
675   switch (Opcode) {
676   default: break;
677   case ARM::RRX:
678     // rrx
679     Binary |= 0x6 << 4;
680     break;
681   case ARM::MOVsrl_flag:
682     // lsr #1
683     Binary |= (0x2 << 4) | (1 << 7);
684     break;
685   case ARM::MOVsra_flag:
686     // asr #1
687     Binary |= (0x4 << 4) | (1 << 7);
688     break;
689   }
690
691   // Encode register Rm.
692   Binary |= getMachineOpValue(MI, 1);
693
694   emitWordLE(Binary);
695 }
696
697 void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
698   DEBUG(errs() << "  ** LPC" << LabelID << " @ "
699         << (void*)MCE.getCurrentPCValue() << '\n');
700   JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
701 }
702
703 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
704   unsigned Opcode = MI.getDesc().Opcode;
705   switch (Opcode) {
706   default:
707     llvm_unreachable("ARMCodeEmitter::emitPseudoInstruction");
708   case ARM::BX:
709   case ARM::BMOVPCRX:
710   case ARM::BXr9:
711   case ARM::BMOVPCRXr9: {
712     // First emit mov lr, pc
713     unsigned Binary = 0x01a0e00f;
714     Binary |= II->getPredicate(&MI) << ARMII::CondShift;
715     emitWordLE(Binary);
716
717     // and then emit the branch.
718     emitMiscBranchInstruction(MI);
719     break;
720   }
721   case TargetOpcode::INLINEASM: {
722     // We allow inline assembler nodes with empty bodies - they can
723     // implicitly define registers, which is ok for JIT.
724     if (MI.getOperand(0).getSymbolName()[0]) {
725       report_fatal_error("JIT does not support inline asm!");
726     }
727     break;
728   }
729   case TargetOpcode::PROLOG_LABEL:
730   case TargetOpcode::EH_LABEL:
731     MCE.emitLabel(MI.getOperand(0).getMCSymbol());
732     break;
733   case TargetOpcode::IMPLICIT_DEF:
734   case TargetOpcode::KILL:
735     // Do nothing.
736     break;
737   case ARM::CONSTPOOL_ENTRY:
738     emitConstPoolInstruction(MI);
739     break;
740   case ARM::PICADD: {
741     // Remember of the address of the PC label for relocation later.
742     addPCLabel(MI.getOperand(2).getImm());
743     // PICADD is just an add instruction that implicitly read pc.
744     emitDataProcessingInstruction(MI, 0, ARM::PC);
745     break;
746   }
747   case ARM::PICLDR:
748   case ARM::PICLDRB:
749   case ARM::PICSTR:
750   case ARM::PICSTRB: {
751     // Remember of the address of the PC label for relocation later.
752     addPCLabel(MI.getOperand(2).getImm());
753     // These are just load / store instructions that implicitly read pc.
754     emitLoadStoreInstruction(MI, 0, ARM::PC);
755     break;
756   }
757   case ARM::PICLDRH:
758   case ARM::PICLDRSH:
759   case ARM::PICLDRSB:
760   case ARM::PICSTRH: {
761     // Remember of the address of the PC label for relocation later.
762     addPCLabel(MI.getOperand(2).getImm());
763     // These are just load / store instructions that implicitly read pc.
764     emitMiscLoadStoreInstruction(MI, ARM::PC);
765     break;
766   }
767
768   case ARM::MOVi32imm:
769     emitMOVi32immInstruction(MI);
770     break;
771
772   case ARM::MOVi2pieces:
773     // Two instructions to materialize a constant.
774     emitMOVi2piecesInstruction(MI);
775     break;
776   case ARM::LEApcrelJT:
777     // Materialize jumptable address.
778     emitLEApcrelJTInstruction(MI);
779     break;
780   case ARM::RRX:
781   case ARM::MOVsrl_flag:
782   case ARM::MOVsra_flag:
783     emitPseudoMoveInstruction(MI);
784     break;
785   }
786 }
787
788 unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
789                                                 const TargetInstrDesc &TID,
790                                                 const MachineOperand &MO,
791                                                 unsigned OpIdx) {
792   unsigned Binary = getMachineOpValue(MI, MO);
793
794   const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
795   const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
796   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
797
798   // Encode the shift opcode.
799   unsigned SBits = 0;
800   unsigned Rs = MO1.getReg();
801   if (Rs) {
802     // Set shift operand (bit[7:4]).
803     // LSL - 0001
804     // LSR - 0011
805     // ASR - 0101
806     // ROR - 0111
807     // RRX - 0110 and bit[11:8] clear.
808     switch (SOpc) {
809     default: llvm_unreachable("Unknown shift opc!");
810     case ARM_AM::lsl: SBits = 0x1; break;
811     case ARM_AM::lsr: SBits = 0x3; break;
812     case ARM_AM::asr: SBits = 0x5; break;
813     case ARM_AM::ror: SBits = 0x7; break;
814     case ARM_AM::rrx: SBits = 0x6; break;
815     }
816   } else {
817     // Set shift operand (bit[6:4]).
818     // LSL - 000
819     // LSR - 010
820     // ASR - 100
821     // ROR - 110
822     switch (SOpc) {
823     default: llvm_unreachable("Unknown shift opc!");
824     case ARM_AM::lsl: SBits = 0x0; break;
825     case ARM_AM::lsr: SBits = 0x2; break;
826     case ARM_AM::asr: SBits = 0x4; break;
827     case ARM_AM::ror: SBits = 0x6; break;
828     }
829   }
830   Binary |= SBits << 4;
831   if (SOpc == ARM_AM::rrx)
832     return Binary;
833
834   // Encode the shift operation Rs or shift_imm (except rrx).
835   if (Rs) {
836     // Encode Rs bit[11:8].
837     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
838     return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
839   }
840
841   // Encode shift_imm bit[11:7].
842   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
843 }
844
845 unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) {
846   int SoImmVal = ARM_AM::getSOImmVal(SoImm);
847   assert(SoImmVal != -1 && "Not a valid so_imm value!");
848
849   // Encode rotate_imm.
850   unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
851     << ARMII::SoRotImmShift;
852
853   // Encode immed_8.
854   Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
855   return Binary;
856 }
857
858 unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
859                                          const TargetInstrDesc &TID) const {
860   for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
861     const MachineOperand &MO = MI.getOperand(i-1);
862     if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
863       return 1 << ARMII::S_BitShift;
864   }
865   return 0;
866 }
867
868 void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
869                                                    unsigned ImplicitRd,
870                                                    unsigned ImplicitRn) {
871   const TargetInstrDesc &TID = MI.getDesc();
872
873   // Part of binary is determined by TableGn.
874   unsigned Binary = getBinaryCodeForInstr(MI);
875
876   // Set the conditional execution predicate
877   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
878
879   // Encode S bit if MI modifies CPSR.
880   Binary |= getAddrModeSBit(MI, TID);
881
882   // Encode register def if there is one.
883   unsigned NumDefs = TID.getNumDefs();
884   unsigned OpIdx = 0;
885   if (NumDefs)
886     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
887   else if (ImplicitRd)
888     // Special handling for implicit use (e.g. PC).
889     Binary |= (getARMRegisterNumbering(ImplicitRd) << ARMII::RegRdShift);
890
891   if (TID.Opcode == ARM::MOVi16) {
892       // Get immediate from MI.
893       unsigned Lo16 = getMovi32Value(MI, MI.getOperand(OpIdx),
894                       ARM::reloc_arm_movw);
895       // Encode imm which is the same as in emitMOVi32immInstruction().
896       Binary |= Lo16 & 0xFFF;
897       Binary |= ((Lo16 >> 12) & 0xF) << 16;
898       emitWordLE(Binary);
899       return;
900   } else if(TID.Opcode == ARM::MOVTi16) {
901       unsigned Hi16 = (getMovi32Value(MI, MI.getOperand(OpIdx),
902                        ARM::reloc_arm_movt) >> 16);
903       Binary |= Hi16 & 0xFFF;
904       Binary |= ((Hi16 >> 12) & 0xF) << 16;
905       emitWordLE(Binary);
906       return;
907   } else if ((TID.Opcode == ARM::BFC) || (TID.Opcode == ARM::BFI)) {
908       uint32_t v = ~MI.getOperand(2).getImm();
909       int32_t lsb = CountTrailingZeros_32(v);
910       int32_t msb = (32 - CountLeadingZeros_32(v)) - 1;
911       // Instr{20-16} = msb, Instr{11-7} = lsb
912       Binary |= (msb & 0x1F) << 16;
913       Binary |= (lsb & 0x1F) << 7;
914       emitWordLE(Binary);
915       return;
916   } else if ((TID.Opcode == ARM::UBFX) || (TID.Opcode == ARM::SBFX)) {
917       // Encode Rn in Instr{0-3}
918       Binary |= getMachineOpValue(MI, OpIdx++);
919
920       uint32_t lsb = MI.getOperand(OpIdx++).getImm();
921       uint32_t widthm1 = MI.getOperand(OpIdx++).getImm() - 1;
922
923       // Instr{20-16} = widthm1, Instr{11-7} = lsb
924       Binary |= (widthm1 & 0x1F) << 16;
925       Binary |= (lsb & 0x1F) << 7;
926       emitWordLE(Binary);
927       return;
928   }
929
930   // If this is a two-address operand, skip it. e.g. MOVCCr operand 1.
931   if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
932     ++OpIdx;
933
934   // Encode first non-shifter register operand if there is one.
935   bool isUnary = TID.TSFlags & ARMII::UnaryDP;
936   if (!isUnary) {
937     if (ImplicitRn)
938       // Special handling for implicit use (e.g. PC).
939       Binary |= (getARMRegisterNumbering(ImplicitRn) << ARMII::RegRnShift);
940     else {
941       Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
942       ++OpIdx;
943     }
944   }
945
946   // Encode shifter operand.
947   const MachineOperand &MO = MI.getOperand(OpIdx);
948   if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) {
949     // Encode SoReg.
950     emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx));
951     return;
952   }
953
954   if (MO.isReg()) {
955     // Encode register Rm.
956     emitWordLE(Binary | getARMRegisterNumbering(MO.getReg()));
957     return;
958   }
959
960   // Encode so_imm.
961   Binary |= getMachineSoImmOpValue((unsigned)MO.getImm());
962
963   emitWordLE(Binary);
964 }
965
966 void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
967                                               unsigned ImplicitRd,
968                                               unsigned ImplicitRn) {
969   const TargetInstrDesc &TID = MI.getDesc();
970   unsigned Form = TID.TSFlags & ARMII::FormMask;
971   bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
972
973   // Part of binary is determined by TableGn.
974   unsigned Binary = getBinaryCodeForInstr(MI);
975
976   // If this is an LDRi12, STRi12 or LDRcp, nothing more needs be done.
977   if (MI.getOpcode() == ARM::LDRi12 || MI.getOpcode() == ARM::LDRcp ||
978       MI.getOpcode() == ARM::STRi12) {
979     emitWordLE(Binary);
980     return;
981   }
982
983   // Set the conditional execution predicate
984   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
985
986   unsigned OpIdx = 0;
987
988   // Operand 0 of a pre- and post-indexed store is the address base
989   // writeback. Skip it.
990   bool Skipped = false;
991   if (IsPrePost && Form == ARMII::StFrm) {
992     ++OpIdx;
993     Skipped = true;
994   }
995
996   // Set first operand
997   if (ImplicitRd)
998     // Special handling for implicit use (e.g. PC).
999     Binary |= (getARMRegisterNumbering(ImplicitRd) << ARMII::RegRdShift);
1000   else
1001     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1002
1003   // Set second operand
1004   if (ImplicitRn)
1005     // Special handling for implicit use (e.g. PC).
1006     Binary |= (getARMRegisterNumbering(ImplicitRn) << ARMII::RegRnShift);
1007   else
1008     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
1009
1010   // If this is a two-address operand, skip it. e.g. LDR_PRE.
1011   if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1012     ++OpIdx;
1013
1014   const MachineOperand &MO2 = MI.getOperand(OpIdx);
1015   unsigned AM2Opc = (ImplicitRn == ARM::PC)
1016     ? 0 : MI.getOperand(OpIdx+1).getImm();
1017
1018   // Set bit U(23) according to sign of immed value (positive or negative).
1019   Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) <<
1020              ARMII::U_BitShift);
1021   if (!MO2.getReg()) { // is immediate
1022     if (ARM_AM::getAM2Offset(AM2Opc))
1023       // Set the value of offset_12 field
1024       Binary |= ARM_AM::getAM2Offset(AM2Opc);
1025     emitWordLE(Binary);
1026     return;
1027   }
1028
1029   // Set bit I(25), because this is not in immediate encoding.
1030   Binary |= 1 << ARMII::I_BitShift;
1031   assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
1032   // Set bit[3:0] to the corresponding Rm register
1033   Binary |= getARMRegisterNumbering(MO2.getReg());
1034
1035   // If this instr is in scaled register offset/index instruction, set
1036   // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
1037   if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) {
1038     Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift;  // shift
1039     Binary |= ShImm              << ARMII::ShiftShift;     // shift_immed
1040   }
1041
1042   emitWordLE(Binary);
1043 }
1044
1045 void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
1046                                                   unsigned ImplicitRn) {
1047   const TargetInstrDesc &TID = MI.getDesc();
1048   unsigned Form = TID.TSFlags & ARMII::FormMask;
1049   bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0;
1050
1051   // Part of binary is determined by TableGn.
1052   unsigned Binary = getBinaryCodeForInstr(MI);
1053
1054   // Set the conditional execution predicate
1055   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1056
1057   unsigned OpIdx = 0;
1058
1059   // Operand 0 of a pre- and post-indexed store is the address base
1060   // writeback. Skip it.
1061   bool Skipped = false;
1062   if (IsPrePost && Form == ARMII::StMiscFrm) {
1063     ++OpIdx;
1064     Skipped = true;
1065   }
1066
1067   // Set first operand
1068   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1069
1070   // Skip LDRD and STRD's second operand.
1071   if (TID.Opcode == ARM::LDRD || TID.Opcode == ARM::STRD)
1072     ++OpIdx;
1073
1074   // Set second operand
1075   if (ImplicitRn)
1076     // Special handling for implicit use (e.g. PC).
1077     Binary |= (getARMRegisterNumbering(ImplicitRn) << ARMII::RegRnShift);
1078   else
1079     Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
1080
1081   // If this is a two-address operand, skip it. e.g. LDRH_POST.
1082   if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1083     ++OpIdx;
1084
1085   const MachineOperand &MO2 = MI.getOperand(OpIdx);
1086   unsigned AM3Opc = (ImplicitRn == ARM::PC)
1087     ? 0 : MI.getOperand(OpIdx+1).getImm();
1088
1089   // Set bit U(23) according to sign of immed value (positive or negative)
1090   Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) <<
1091              ARMII::U_BitShift);
1092
1093   // If this instr is in register offset/index encoding, set bit[3:0]
1094   // to the corresponding Rm register.
1095   if (MO2.getReg()) {
1096     Binary |= getARMRegisterNumbering(MO2.getReg());
1097     emitWordLE(Binary);
1098     return;
1099   }
1100
1101   // This instr is in immediate offset/index encoding, set bit 22 to 1.
1102   Binary |= 1 << ARMII::AM3_I_BitShift;
1103   if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) {
1104     // Set operands
1105     Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift;  // immedH
1106     Binary |= (ImmOffs & 0xF);                      // immedL
1107   }
1108
1109   emitWordLE(Binary);
1110 }
1111
1112 static unsigned getAddrModeUPBits(unsigned Mode) {
1113   unsigned Binary = 0;
1114
1115   // Set addressing mode by modifying bits U(23) and P(24)
1116   // IA - Increment after  - bit U = 1 and bit P = 0
1117   // IB - Increment before - bit U = 1 and bit P = 1
1118   // DA - Decrement after  - bit U = 0 and bit P = 0
1119   // DB - Decrement before - bit U = 0 and bit P = 1
1120   switch (Mode) {
1121   default: llvm_unreachable("Unknown addressing sub-mode!");
1122   case ARM_AM::da:                                     break;
1123   case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break;
1124   case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break;
1125   case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break;
1126   }
1127
1128   return Binary;
1129 }
1130
1131 void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
1132   const TargetInstrDesc &TID = MI.getDesc();
1133   bool IsUpdating = (TID.TSFlags & ARMII::IndexModeMask) != 0;
1134
1135   // Part of binary is determined by TableGn.
1136   unsigned Binary = getBinaryCodeForInstr(MI);
1137
1138   // Set the conditional execution predicate
1139   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1140
1141   // Skip operand 0 of an instruction with base register update.
1142   unsigned OpIdx = 0;
1143   if (IsUpdating)
1144     ++OpIdx;
1145
1146   // Set base address operand
1147   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
1148
1149   // Set addressing mode by modifying bits U(23) and P(24)
1150   const MachineOperand &MO = MI.getOperand(OpIdx++);
1151   Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm()));
1152
1153   // Set bit W(21)
1154   if (IsUpdating)
1155     Binary |= 0x1 << ARMII::W_BitShift;
1156
1157   // Set registers
1158   for (unsigned i = OpIdx+2, e = MI.getNumOperands(); i != e; ++i) {
1159     const MachineOperand &MO = MI.getOperand(i);
1160     if (!MO.isReg() || MO.isImplicit())
1161       break;
1162     unsigned RegNum = getARMRegisterNumbering(MO.getReg());
1163     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
1164            RegNum < 16);
1165     Binary |= 0x1 << RegNum;
1166   }
1167
1168   emitWordLE(Binary);
1169 }
1170
1171 void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
1172   const TargetInstrDesc &TID = MI.getDesc();
1173
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   // Encode S bit if MI modifies CPSR.
1181   Binary |= getAddrModeSBit(MI, TID);
1182
1183   // 32x32->64bit operations have two destination registers. The number
1184   // of register definitions will tell us if that's what we're dealing with.
1185   unsigned OpIdx = 0;
1186   if (TID.getNumDefs() == 2)
1187     Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift;
1188
1189   // Encode Rd
1190   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift;
1191
1192   // Encode Rm
1193   Binary |= getMachineOpValue(MI, OpIdx++);
1194
1195   // Encode Rs
1196   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift;
1197
1198   // Many multiple instructions (e.g. MLA) have three src operands. Encode
1199   // it as Rn (for multiply, that's in the same offset as RdLo.
1200   if (TID.getNumOperands() > OpIdx &&
1201       !TID.OpInfo[OpIdx].isPredicate() &&
1202       !TID.OpInfo[OpIdx].isOptionalDef())
1203     Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift;
1204
1205   emitWordLE(Binary);
1206 }
1207
1208 void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) {
1209   const TargetInstrDesc &TID = MI.getDesc();
1210
1211   // Part of binary is determined by TableGn.
1212   unsigned Binary = getBinaryCodeForInstr(MI);
1213
1214   // Set the conditional execution predicate
1215   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1216
1217   unsigned OpIdx = 0;
1218
1219   // Encode Rd
1220   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1221
1222   const MachineOperand &MO1 = MI.getOperand(OpIdx++);
1223   const MachineOperand &MO2 = MI.getOperand(OpIdx);
1224   if (MO2.isReg()) {
1225     // Two register operand form.
1226     // Encode Rn.
1227     Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift;
1228
1229     // Encode Rm.
1230     Binary |= getMachineOpValue(MI, MO2);
1231     ++OpIdx;
1232   } else {
1233     Binary |= getMachineOpValue(MI, MO1);
1234   }
1235
1236   // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand.
1237   if (MI.getOperand(OpIdx).isImm() &&
1238       !TID.OpInfo[OpIdx].isPredicate() &&
1239       !TID.OpInfo[OpIdx].isOptionalDef())
1240     Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift;
1241
1242   emitWordLE(Binary);
1243 }
1244
1245 void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) {
1246   const TargetInstrDesc &TID = MI.getDesc();
1247
1248   // Part of binary is determined by TableGn.
1249   unsigned Binary = getBinaryCodeForInstr(MI);
1250
1251   // Set the conditional execution predicate
1252   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1253
1254   unsigned OpIdx = 0;
1255
1256   // Encode Rd
1257   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift;
1258
1259   const MachineOperand &MO = MI.getOperand(OpIdx++);
1260   if (OpIdx == TID.getNumOperands() ||
1261       TID.OpInfo[OpIdx].isPredicate() ||
1262       TID.OpInfo[OpIdx].isOptionalDef()) {
1263     // Encode Rm and it's done.
1264     Binary |= getMachineOpValue(MI, MO);
1265     emitWordLE(Binary);
1266     return;
1267   }
1268
1269   // Encode Rn.
1270   Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift;
1271
1272   // Encode Rm.
1273   Binary |= getMachineOpValue(MI, OpIdx++);
1274
1275   // Encode shift_imm.
1276   unsigned ShiftAmt = MI.getOperand(OpIdx).getImm();
1277   if (TID.Opcode == ARM::PKHTB) {
1278     assert(ShiftAmt != 0 && "PKHTB shift_imm is 0!");
1279     if (ShiftAmt == 32)
1280       ShiftAmt = 0;
1281   }
1282   assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1283   Binary |= ShiftAmt << ARMII::ShiftShift;
1284
1285   emitWordLE(Binary);
1286 }
1287
1288 void ARMCodeEmitter::emitSaturateInstruction(const MachineInstr &MI) {
1289   const TargetInstrDesc &TID = MI.getDesc();
1290
1291   // Part of binary is determined by TableGen.
1292   unsigned Binary = getBinaryCodeForInstr(MI);
1293
1294   // Set the conditional execution predicate
1295   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1296
1297   // Encode Rd
1298   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
1299
1300   // Encode saturate bit position.
1301   unsigned Pos = MI.getOperand(1).getImm();
1302   if (TID.Opcode == ARM::SSAT || TID.Opcode == ARM::SSAT16)
1303     Pos -= 1;
1304   assert((Pos < 16 || (Pos < 32 &&
1305                        TID.Opcode != ARM::SSAT16 &&
1306                        TID.Opcode != ARM::USAT16)) &&
1307          "saturate bit position out of range");
1308   Binary |= Pos << 16;
1309
1310   // Encode Rm
1311   Binary |= getMachineOpValue(MI, 2);
1312
1313   // Encode shift_imm.
1314   if (TID.getNumOperands() == 4) {
1315     unsigned ShiftOp = MI.getOperand(3).getImm();
1316     ARM_AM::ShiftOpc Opc = ARM_AM::getSORegShOp(ShiftOp);
1317     if (Opc == ARM_AM::asr)
1318       Binary |= (1 << 6);
1319     unsigned ShiftAmt = MI.getOperand(3).getImm();
1320     if (ShiftAmt == 32 && Opc == ARM_AM::asr)
1321       ShiftAmt = 0;
1322     assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!");
1323     Binary |= ShiftAmt << ARMII::ShiftShift;
1324   }
1325
1326   emitWordLE(Binary);
1327 }
1328
1329 void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
1330   const TargetInstrDesc &TID = MI.getDesc();
1331
1332   if (TID.Opcode == ARM::TPsoft) {
1333     llvm_unreachable("ARM::TPsoft FIXME"); // FIXME
1334   }
1335
1336   // Part of binary is determined by TableGn.
1337   unsigned Binary = getBinaryCodeForInstr(MI);
1338
1339   // Set the conditional execution predicate
1340   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1341
1342   // Set signed_immed_24 field
1343   Binary |= getMachineOpValue(MI, 0);
1344
1345   emitWordLE(Binary);
1346 }
1347
1348 void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) {
1349   // Remember the base address of the inline jump table.
1350   uintptr_t JTBase = MCE.getCurrentPCValue();
1351   JTI->addJumpTableBaseAddr(JTIndex, JTBase);
1352   DEBUG(errs() << "  ** Jump Table #" << JTIndex << " @ " << (void*)JTBase
1353                << '\n');
1354
1355   // Now emit the jump table entries.
1356   const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs;
1357   for (unsigned i = 0, e = MBBs.size(); i != e; ++i) {
1358     if (IsPIC)
1359       // DestBB address - JT base.
1360       emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase);
1361     else
1362       // Absolute DestBB address.
1363       emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute);
1364     emitWordLE(0);
1365   }
1366 }
1367
1368 void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
1369   const TargetInstrDesc &TID = MI.getDesc();
1370
1371   // Handle jump tables.
1372   if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) {
1373     // First emit a ldr pc, [] instruction.
1374     emitDataProcessingInstruction(MI, ARM::PC);
1375
1376     // Then emit the inline jump table.
1377     unsigned JTIndex =
1378       (TID.Opcode == ARM::BR_JTr)
1379       ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex();
1380     emitInlineJumpTable(JTIndex);
1381     return;
1382   } else if (TID.Opcode == ARM::BR_JTm) {
1383     // First emit a ldr pc, [] instruction.
1384     emitLoadStoreInstruction(MI, ARM::PC);
1385
1386     // Then emit the inline jump table.
1387     emitInlineJumpTable(MI.getOperand(3).getIndex());
1388     return;
1389   }
1390
1391   // Part of binary is determined by TableGn.
1392   unsigned Binary = getBinaryCodeForInstr(MI);
1393
1394   // Set the conditional execution predicate
1395   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1396
1397   if (TID.Opcode == ARM::BX_RET || TID.Opcode == ARM::MOVPCLR)
1398     // The return register is LR.
1399     Binary |= getARMRegisterNumbering(ARM::LR);
1400   else
1401     // otherwise, set the return register
1402     Binary |= getMachineOpValue(MI, 0);
1403
1404   emitWordLE(Binary);
1405 }
1406
1407 static unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) {
1408   unsigned RegD = MI.getOperand(OpIdx).getReg();
1409   unsigned Binary = 0;
1410   bool isSPVFP = ARM::SPRRegisterClass->contains(RegD);
1411   RegD = getARMRegisterNumbering(RegD);
1412   if (!isSPVFP)
1413     Binary |=   RegD               << ARMII::RegRdShift;
1414   else {
1415     Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift;
1416     Binary |=  (RegD & 0x01)       << ARMII::D_BitShift;
1417   }
1418   return Binary;
1419 }
1420
1421 static unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) {
1422   unsigned RegN = MI.getOperand(OpIdx).getReg();
1423   unsigned Binary = 0;
1424   bool isSPVFP = ARM::SPRRegisterClass->contains(RegN);
1425   RegN = getARMRegisterNumbering(RegN);
1426   if (!isSPVFP)
1427     Binary |=   RegN               << ARMII::RegRnShift;
1428   else {
1429     Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift;
1430     Binary |=  (RegN & 0x01)       << ARMII::N_BitShift;
1431   }
1432   return Binary;
1433 }
1434
1435 static unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) {
1436   unsigned RegM = MI.getOperand(OpIdx).getReg();
1437   unsigned Binary = 0;
1438   bool isSPVFP = ARM::SPRRegisterClass->contains(RegM);
1439   RegM = getARMRegisterNumbering(RegM);
1440   if (!isSPVFP)
1441     Binary |=   RegM;
1442   else {
1443     Binary |= ((RegM & 0x1E) >> 1);
1444     Binary |=  (RegM & 0x01)       << ARMII::M_BitShift;
1445   }
1446   return Binary;
1447 }
1448
1449 void ARMCodeEmitter::emitVFPArithInstruction(const MachineInstr &MI) {
1450   const TargetInstrDesc &TID = MI.getDesc();
1451
1452   // Part of binary is determined by TableGn.
1453   unsigned Binary = getBinaryCodeForInstr(MI);
1454
1455   // Set the conditional execution predicate
1456   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1457
1458   unsigned OpIdx = 0;
1459   assert((Binary & ARMII::D_BitShift) == 0 &&
1460          (Binary & ARMII::N_BitShift) == 0 &&
1461          (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!");
1462
1463   // Encode Dd / Sd.
1464   Binary |= encodeVFPRd(MI, OpIdx++);
1465
1466   // If this is a two-address operand, skip it, e.g. FMACD.
1467   if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1468     ++OpIdx;
1469
1470   // Encode Dn / Sn.
1471   if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm)
1472     Binary |= encodeVFPRn(MI, OpIdx++);
1473
1474   if (OpIdx == TID.getNumOperands() ||
1475       TID.OpInfo[OpIdx].isPredicate() ||
1476       TID.OpInfo[OpIdx].isOptionalDef()) {
1477     // FCMPEZD etc. has only one operand.
1478     emitWordLE(Binary);
1479     return;
1480   }
1481
1482   // Encode Dm / Sm.
1483   Binary |= encodeVFPRm(MI, OpIdx);
1484
1485   emitWordLE(Binary);
1486 }
1487
1488 void ARMCodeEmitter::emitVFPConversionInstruction(const MachineInstr &MI) {
1489   const TargetInstrDesc &TID = MI.getDesc();
1490   unsigned Form = TID.TSFlags & ARMII::FormMask;
1491
1492   // Part of binary is determined by TableGn.
1493   unsigned Binary = getBinaryCodeForInstr(MI);
1494
1495   // Set the conditional execution predicate
1496   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1497
1498   switch (Form) {
1499   default: break;
1500   case ARMII::VFPConv1Frm:
1501   case ARMII::VFPConv2Frm:
1502   case ARMII::VFPConv3Frm:
1503     // Encode Dd / Sd.
1504     Binary |= encodeVFPRd(MI, 0);
1505     break;
1506   case ARMII::VFPConv4Frm:
1507     // Encode Dn / Sn.
1508     Binary |= encodeVFPRn(MI, 0);
1509     break;
1510   case ARMII::VFPConv5Frm:
1511     // Encode Dm / Sm.
1512     Binary |= encodeVFPRm(MI, 0);
1513     break;
1514   }
1515
1516   switch (Form) {
1517   default: break;
1518   case ARMII::VFPConv1Frm:
1519     // Encode Dm / Sm.
1520     Binary |= encodeVFPRm(MI, 1);
1521     break;
1522   case ARMII::VFPConv2Frm:
1523   case ARMII::VFPConv3Frm:
1524     // Encode Dn / Sn.
1525     Binary |= encodeVFPRn(MI, 1);
1526     break;
1527   case ARMII::VFPConv4Frm:
1528   case ARMII::VFPConv5Frm:
1529     // Encode Dd / Sd.
1530     Binary |= encodeVFPRd(MI, 1);
1531     break;
1532   }
1533
1534   if (Form == ARMII::VFPConv5Frm)
1535     // Encode Dn / Sn.
1536     Binary |= encodeVFPRn(MI, 2);
1537   else if (Form == ARMII::VFPConv3Frm)
1538     // Encode Dm / Sm.
1539     Binary |= encodeVFPRm(MI, 2);
1540
1541   emitWordLE(Binary);
1542 }
1543
1544 void ARMCodeEmitter::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
1545   // Part of binary is determined by TableGn.
1546   unsigned Binary = getBinaryCodeForInstr(MI);
1547
1548   // Set the conditional execution predicate
1549   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1550
1551   unsigned OpIdx = 0;
1552
1553   // Encode Dd / Sd.
1554   Binary |= encodeVFPRd(MI, OpIdx++);
1555
1556   // Encode address base.
1557   const MachineOperand &Base = MI.getOperand(OpIdx++);
1558   Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift;
1559
1560   // If there is a non-zero immediate offset, encode it.
1561   if (Base.isReg()) {
1562     const MachineOperand &Offset = MI.getOperand(OpIdx);
1563     if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) {
1564       if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add)
1565         Binary |= 1 << ARMII::U_BitShift;
1566       Binary |= ImmOffs;
1567       emitWordLE(Binary);
1568       return;
1569     }
1570   }
1571
1572   // If immediate offset is omitted, default to +0.
1573   Binary |= 1 << ARMII::U_BitShift;
1574
1575   emitWordLE(Binary);
1576 }
1577
1578 void
1579 ARMCodeEmitter::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) {
1580   const TargetInstrDesc &TID = MI.getDesc();
1581   bool IsUpdating = (TID.TSFlags & ARMII::IndexModeMask) != 0;
1582
1583   // Part of binary is determined by TableGn.
1584   unsigned Binary = getBinaryCodeForInstr(MI);
1585
1586   // Set the conditional execution predicate
1587   Binary |= II->getPredicate(&MI) << ARMII::CondShift;
1588
1589   // Skip operand 0 of an instruction with base register update.
1590   unsigned OpIdx = 0;
1591   if (IsUpdating)
1592     ++OpIdx;
1593
1594   // Set base address operand
1595   Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift;
1596
1597   // Set addressing mode by modifying bits U(23) and P(24)
1598   const MachineOperand &MO = MI.getOperand(OpIdx++);
1599   Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm()));
1600
1601   // Set bit W(21)
1602   if (IsUpdating)
1603     Binary |= 0x1 << ARMII::W_BitShift;
1604
1605   // First register is encoded in Dd.
1606   Binary |= encodeVFPRd(MI, OpIdx+2);
1607
1608   // Count the number of registers.
1609   unsigned NumRegs = 1;
1610   for (unsigned i = OpIdx+3, e = MI.getNumOperands(); i != e; ++i) {
1611     const MachineOperand &MO = MI.getOperand(i);
1612     if (!MO.isReg() || MO.isImplicit())
1613       break;
1614     ++NumRegs;
1615   }
1616   // Bit 8 will be set if <list> is consecutive 64-bit registers (e.g., D0)
1617   // Otherwise, it will be 0, in the case of 32-bit registers.
1618   if(Binary & 0x100)
1619     Binary |= NumRegs * 2;
1620   else
1621     Binary |= NumRegs;
1622
1623   emitWordLE(Binary);
1624 }
1625
1626 static unsigned encodeNEONRd(const MachineInstr &MI, unsigned OpIdx) {
1627   unsigned RegD = MI.getOperand(OpIdx).getReg();
1628   unsigned Binary = 0;
1629   RegD = getARMRegisterNumbering(RegD);
1630   Binary |= (RegD & 0xf) << ARMII::RegRdShift;
1631   Binary |= ((RegD >> 4) & 1) << ARMII::D_BitShift;
1632   return Binary;
1633 }
1634
1635 static unsigned encodeNEONRn(const MachineInstr &MI, unsigned OpIdx) {
1636   unsigned RegN = MI.getOperand(OpIdx).getReg();
1637   unsigned Binary = 0;
1638   RegN = getARMRegisterNumbering(RegN);
1639   Binary |= (RegN & 0xf) << ARMII::RegRnShift;
1640   Binary |= ((RegN >> 4) & 1) << ARMII::N_BitShift;
1641   return Binary;
1642 }
1643
1644 static unsigned encodeNEONRm(const MachineInstr &MI, unsigned OpIdx) {
1645   unsigned RegM = MI.getOperand(OpIdx).getReg();
1646   unsigned Binary = 0;
1647   RegM = getARMRegisterNumbering(RegM);
1648   Binary |= (RegM & 0xf);
1649   Binary |= ((RegM >> 4) & 1) << ARMII::M_BitShift;
1650   return Binary;
1651 }
1652
1653 /// convertNEONDataProcToThumb - Convert the ARM mode encoding for a NEON
1654 /// data-processing instruction to the corresponding Thumb encoding.
1655 static unsigned convertNEONDataProcToThumb(unsigned Binary) {
1656   assert((Binary & 0xfe000000) == 0xf2000000 &&
1657          "not an ARM NEON data-processing instruction");
1658   unsigned UBit = (Binary >> 24) & 1;
1659   return 0xef000000 | (UBit << 28) | (Binary & 0xffffff);
1660 }
1661
1662 void ARMCodeEmitter::emitNEONLaneInstruction(const MachineInstr &MI) {
1663   unsigned Binary = getBinaryCodeForInstr(MI);
1664
1665   unsigned RegTOpIdx, RegNOpIdx, LnOpIdx;
1666   const TargetInstrDesc &TID = MI.getDesc();
1667   if ((TID.TSFlags & ARMII::FormMask) == ARMII::NGetLnFrm) {
1668     RegTOpIdx = 0;
1669     RegNOpIdx = 1;
1670     LnOpIdx = 2;
1671   } else { // ARMII::NSetLnFrm
1672     RegTOpIdx = 2;
1673     RegNOpIdx = 0;
1674     LnOpIdx = 3;
1675   }
1676
1677   // Set the conditional execution predicate
1678   Binary |= (IsThumb ? ARMCC::AL : II->getPredicate(&MI)) << ARMII::CondShift;
1679
1680   unsigned RegT = MI.getOperand(RegTOpIdx).getReg();
1681   RegT = getARMRegisterNumbering(RegT);
1682   Binary |= (RegT << ARMII::RegRdShift);
1683   Binary |= encodeNEONRn(MI, RegNOpIdx);
1684
1685   unsigned LaneShift;
1686   if ((Binary & (1 << 22)) != 0)
1687     LaneShift = 0; // 8-bit elements
1688   else if ((Binary & (1 << 5)) != 0)
1689     LaneShift = 1; // 16-bit elements
1690   else
1691     LaneShift = 2; // 32-bit elements
1692
1693   unsigned Lane = MI.getOperand(LnOpIdx).getImm() << LaneShift;
1694   unsigned Opc1 = Lane >> 2;
1695   unsigned Opc2 = Lane & 3;
1696   assert((Opc1 & 3) == 0 && "out-of-range lane number operand");
1697   Binary |= (Opc1 << 21);
1698   Binary |= (Opc2 << 5);
1699
1700   emitWordLE(Binary);
1701 }
1702
1703 void ARMCodeEmitter::emitNEONDupInstruction(const MachineInstr &MI) {
1704   unsigned Binary = getBinaryCodeForInstr(MI);
1705
1706   // Set the conditional execution predicate
1707   Binary |= (IsThumb ? ARMCC::AL : II->getPredicate(&MI)) << ARMII::CondShift;
1708
1709   unsigned RegT = MI.getOperand(1).getReg();
1710   RegT = getARMRegisterNumbering(RegT);
1711   Binary |= (RegT << ARMII::RegRdShift);
1712   Binary |= encodeNEONRn(MI, 0);
1713   emitWordLE(Binary);
1714 }
1715
1716 void ARMCodeEmitter::emitNEON1RegModImmInstruction(const MachineInstr &MI) {
1717   unsigned Binary = getBinaryCodeForInstr(MI);
1718   // Destination register is encoded in Dd.
1719   Binary |= encodeNEONRd(MI, 0);
1720   // Immediate fields: Op, Cmode, I, Imm3, Imm4
1721   unsigned Imm = MI.getOperand(1).getImm();
1722   unsigned Op = (Imm >> 12) & 1;
1723   unsigned Cmode = (Imm >> 8) & 0xf;
1724   unsigned I = (Imm >> 7) & 1;
1725   unsigned Imm3 = (Imm >> 4) & 0x7;
1726   unsigned Imm4 = Imm & 0xf;
1727   Binary |= (I << 24) | (Imm3 << 16) | (Cmode << 8) | (Op << 5) | Imm4;
1728   if (IsThumb)
1729     Binary = convertNEONDataProcToThumb(Binary);
1730   emitWordLE(Binary);
1731 }
1732
1733 void ARMCodeEmitter::emitNEON2RegInstruction(const MachineInstr &MI) {
1734   const TargetInstrDesc &TID = MI.getDesc();
1735   unsigned Binary = getBinaryCodeForInstr(MI);
1736   // Destination register is encoded in Dd; source register in Dm.
1737   unsigned OpIdx = 0;
1738   Binary |= encodeNEONRd(MI, OpIdx++);
1739   if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1740     ++OpIdx;
1741   Binary |= encodeNEONRm(MI, OpIdx);
1742   if (IsThumb)
1743     Binary = convertNEONDataProcToThumb(Binary);
1744   // FIXME: This does not handle VDUPfdf or VDUPfqf.
1745   emitWordLE(Binary);
1746 }
1747
1748 void ARMCodeEmitter::emitNEON3RegInstruction(const MachineInstr &MI) {
1749   const TargetInstrDesc &TID = MI.getDesc();
1750   unsigned Binary = getBinaryCodeForInstr(MI);
1751   // Destination register is encoded in Dd; source registers in Dn and Dm.
1752   unsigned OpIdx = 0;
1753   Binary |= encodeNEONRd(MI, OpIdx++);
1754   if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1755     ++OpIdx;
1756   Binary |= encodeNEONRn(MI, OpIdx++);
1757   if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1)
1758     ++OpIdx;
1759   Binary |= encodeNEONRm(MI, OpIdx);
1760   if (IsThumb)
1761     Binary = convertNEONDataProcToThumb(Binary);
1762   // FIXME: This does not handle VMOVDneon or VMOVQ.
1763   emitWordLE(Binary);
1764 }
1765
1766 #include "ARMGenCodeEmitter.inc"