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