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