I think we got non-machine specific constpool entries covered.
[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/Passes.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/Debug.h"
35 using namespace llvm;
36
37 STATISTIC(NumEmitted, "Number of machine instructions emitted");
38
39 namespace {
40   class VISIBILITY_HIDDEN ARMCodeEmitter : public MachineFunctionPass {
41     ARMJITInfo                *JTI;
42     const ARMInstrInfo        *II;
43     const TargetData          *TD;
44     TargetMachine             &TM;
45     MachineCodeEmitter        &MCE;
46     const MachineConstantPool *MCP;
47   public:
48     static char ID;
49     explicit ARMCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce)
50       : MachineFunctionPass(&ID), JTI(0), II(0), TD(0), TM(tm),
51       MCE(mce), MCP(0) {}
52     ARMCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce,
53             const ARMInstrInfo &ii, const TargetData &td)
54       : MachineFunctionPass(&ID), JTI(0), II(&ii), TD(&td), TM(tm),
55       MCE(mce), MCP(0) {}
56
57     bool runOnMachineFunction(MachineFunction &MF);
58
59     virtual const char *getPassName() const {
60       return "ARM Machine Code Emitter";
61     }
62
63     void emitInstruction(const MachineInstr &MI);
64
65   private:
66
67     void emitConstPoolInstruction(const MachineInstr &MI);
68
69     void emitPseudoInstruction(const MachineInstr &MI);
70
71     unsigned getAddrModeNoneInstrBinary(const MachineInstr &MI,
72                                         const TargetInstrDesc &TID,
73                                         unsigned Binary);
74
75     unsigned getMachineSoRegOpValue(const MachineInstr &MI,
76                                     const TargetInstrDesc &TID,
77                                     unsigned OpIdx);
78
79     unsigned getAddrMode1SBit(const MachineInstr &MI,
80                               const TargetInstrDesc &TID) const;
81
82     unsigned getAddrMode1InstrBinary(const MachineInstr &MI,
83                                      const TargetInstrDesc &TID,
84                                      unsigned Binary);
85     unsigned getAddrMode2InstrBinary(const MachineInstr &MI,
86                                      const TargetInstrDesc &TID,
87                                      unsigned Binary);
88     unsigned getAddrMode3InstrBinary(const MachineInstr &MI,
89                                      const TargetInstrDesc &TID,
90                                      unsigned Binary);
91     unsigned getAddrMode4InstrBinary(const MachineInstr &MI,
92                                      const TargetInstrDesc &TID,
93                                      unsigned Binary);
94
95     /// getInstrBinary - Return binary encoding for the specified
96     /// machine instruction.
97     unsigned getInstrBinary(const MachineInstr &MI);
98
99     /// getBinaryCodeForInstr - This function, generated by the
100     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
101     /// machine instructions.
102     ///
103     unsigned getBinaryCodeForInstr(const MachineInstr &MI);
104
105     /// getMachineOpValue - Return binary encoding of operand. If the machine
106     /// operand requires relocation, record the relocation and return zero.
107     unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) {
108       return getMachineOpValue(MI, MI.getOperand(OpIdx));
109     }
110     unsigned getMachineOpValue(const MachineInstr &MI,
111                                const MachineOperand &MO);
112
113     /// getBaseOpcodeFor - Return the opcode value.
114     ///
115     unsigned getBaseOpcodeFor(const TargetInstrDesc &TID) const {
116       return (TID.TSFlags & ARMII::OpcodeMask) >> ARMII::OpcodeShift;
117     }
118
119     /// getShiftOp - Return the shift opcode (bit[6:5]) of the machine operand.
120     ///
121     unsigned getShiftOp(const MachineOperand &MO) const ;
122
123     /// Routines that handle operands which add machine relocations which are
124     /// fixed up by the JIT fixup stage.
125     void emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
126                            bool NeedStub);
127     void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
128     void emitConstPoolAddress(unsigned CPI, unsigned Reloc,
129                               int Disp = 0, unsigned PCAdj = 0 );
130     void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc,
131                               unsigned PCAdj = 0);
132     void emitGlobalConstant(const Constant *CV);
133     void emitMachineBasicBlock(MachineBasicBlock *BB);
134   };
135   char ARMCodeEmitter::ID = 0;
136 }
137
138 /// createARMCodeEmitterPass - Return a pass that emits the collected ARM code
139 /// to the specified MCE object.
140 FunctionPass *llvm::createARMCodeEmitterPass(ARMTargetMachine &TM,
141                                              MachineCodeEmitter &MCE) {
142   return new ARMCodeEmitter(TM, MCE);
143 }
144
145 bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
146   assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
147           MF.getTarget().getRelocationModel() != Reloc::Static) &&
148          "JIT relocation model must be set to static or default!");
149   II = ((ARMTargetMachine&)MF.getTarget()).getInstrInfo();
150   TD = ((ARMTargetMachine&)MF.getTarget()).getTargetData();
151   JTI = ((ARMTargetMachine&)MF.getTarget()).getJITInfo();
152   MCP = MF.getConstantPool();
153
154   do {
155     DOUT << "JITTing function '" << MF.getFunction()->getName() << "'\n";
156     MCE.startFunction(MF);
157     for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); 
158          MBB != E; ++MBB) {
159       MCE.StartMachineBasicBlock(MBB);
160       for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
161            I != E; ++I)
162         emitInstruction(*I);
163     }
164   } while (MCE.finishFunction(MF));
165
166   return false;
167 }
168
169 /// getShiftOp - Return the shift opcode (bit[6:5]) of the machine operand.
170 ///
171 unsigned ARMCodeEmitter::getShiftOp(const MachineOperand &MO) const {
172   switch (ARM_AM::getAM2ShiftOpc(MO.getImm())) {
173   default: assert(0 && "Unknown shift opc!");
174   case ARM_AM::asr: return 2;
175   case ARM_AM::lsl: return 0;
176   case ARM_AM::lsr: return 1;
177   case ARM_AM::ror:
178   case ARM_AM::rrx: return 3;
179   }
180   return 0;
181 }
182
183 /// getMachineOpValue - Return binary encoding of operand. If the machine
184 /// operand requires relocation, record the relocation and return zero.
185 unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
186                                            const MachineOperand &MO) {
187   if (MO.isReg())
188     return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
189   else if (MO.isImm())
190     return static_cast<unsigned>(MO.getImm());
191   else if (MO.isGlobal())
192     emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true);
193   else if (MO.isSymbol())
194     emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_relative);
195   else if (MO.isCPI())
196     emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry);
197   else if (MO.isJTI())
198     emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative);
199   else if (MO.isMBB())
200     emitMachineBasicBlock(MO.getMBB());
201   else {
202     cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
203     abort();
204   }
205   return 0;
206 }
207
208 /// emitGlobalAddress - Emit the specified address to the code stream.
209 ///
210 void ARMCodeEmitter::emitGlobalAddress(GlobalValue *GV,
211                                        unsigned Reloc, bool NeedStub) {
212   MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
213                                              Reloc, GV, 0, NeedStub));
214 }
215
216 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
217 /// be emitted to the current location in the function, and allow it to be PC
218 /// relative.
219 void ARMCodeEmitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
220   MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
221                                                  Reloc, ES));
222 }
223
224 /// emitConstPoolAddress - Arrange for the address of an constant pool
225 /// to be emitted to the current location in the function, and allow it to be PC
226 /// relative.
227 void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
228                                           int Disp /* = 0 */,
229                                           unsigned PCAdj /* = 0 */) {
230   // Tell JIT emitter we'll resolve the address.
231   MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
232                                                     Reloc, CPI, PCAdj, true));
233 }
234
235 /// emitJumpTableAddress - Arrange for the address of a jump table to
236 /// be emitted to the current location in the function, and allow it to be PC
237 /// relative.
238 void ARMCodeEmitter::emitJumpTableAddress(unsigned JTIndex, unsigned Reloc,
239                                           unsigned PCAdj /* = 0 */) {
240   MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
241                                                     Reloc, JTIndex, PCAdj));
242 }
243
244 /// emitMachineBasicBlock - Emit the specified address basic block.
245 void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB) {
246   MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
247                                              ARM::reloc_arm_branch, BB));
248 }
249
250 void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
251   DOUT << "JIT: " << "0x" << MCE.getCurrentPCValue() << ":\t" << MI;
252
253   NumEmitted++;  // Keep track of the # of mi's emitted
254   if ((MI.getDesc().TSFlags & ARMII::FormMask) == ARMII::Pseudo)
255     emitPseudoInstruction(MI);
256   else
257     MCE.emitWordLE(getInstrBinary(MI));
258 }
259
260 unsigned ARMCodeEmitter::getAddrModeNoneInstrBinary(const MachineInstr &MI,
261                                                     const TargetInstrDesc &TID,
262                                                     unsigned Binary) {
263   // Set the conditional execution predicate
264   Binary |= II->getPredicate(&MI) << 28;
265
266   switch (TID.TSFlags & ARMII::FormMask) {
267   default:
268     assert(0 && "Unknown instruction subtype!");
269     break;
270   case ARMII::Branch: {
271     // Set signed_immed_24 field
272     Binary |= getMachineOpValue(MI, 0);
273
274     // if it is a conditional branch, set cond field
275     if (TID.Opcode == ARM::Bcc) {
276       Binary &= 0x0FFFFFFF;                      // clear conditional field
277       Binary |= getMachineOpValue(MI, 1) << 28;  // set conditional field
278     }
279     break;
280   }
281   case ARMII::BranchMisc: {
282     if (TID.Opcode == ARM::BX)
283       abort(); // FIXME
284     if (TID.Opcode == ARM::BX_RET)
285       Binary |= 0xe; // the return register is LR
286     else 
287       // otherwise, set the return register
288       Binary |= getMachineOpValue(MI, 0);
289     break;
290   }
291   }
292
293   return Binary;
294 }
295
296 unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
297                                                 const TargetInstrDesc &TID,
298                                                 unsigned OpIdx) {
299   // Set last operand (register Rm)
300   unsigned Binary = getMachineOpValue(MI, OpIdx);
301
302   const MachineOperand &MO1 = MI.getOperand(OpIdx + 1);
303   const MachineOperand &MO2 = MI.getOperand(OpIdx + 2);
304   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
305
306   // Encode the shift opcode.
307   unsigned SBits = 0;
308   unsigned Rs = MO1.getReg();
309   if (Rs) {
310     // Set shift operand (bit[7:4]).
311     // LSL - 0001
312     // LSR - 0011
313     // ASR - 0101
314     // ROR - 0111
315     // RRX - 0110 and bit[11:8] clear.
316     switch (SOpc) {
317     default: assert(0 && "Unknown shift opc!");
318     case ARM_AM::lsl: SBits = 0x1; break;
319     case ARM_AM::lsr: SBits = 0x3; break;
320     case ARM_AM::asr: SBits = 0x5; break;
321     case ARM_AM::ror: SBits = 0x7; break;
322     case ARM_AM::rrx: SBits = 0x6; break;
323     }
324   } else {
325     // Set shift operand (bit[6:4]).
326     // LSL - 000
327     // LSR - 010
328     // ASR - 100
329     // ROR - 110
330     switch (SOpc) {
331     default: assert(0 && "Unknown shift opc!");
332     case ARM_AM::lsl: SBits = 0x0; break;
333     case ARM_AM::lsr: SBits = 0x2; break;
334     case ARM_AM::asr: SBits = 0x4; break;
335     case ARM_AM::ror: SBits = 0x6; break;
336     }
337   }
338   Binary |= SBits << 4;
339   if (SOpc == ARM_AM::rrx)
340     return Binary;
341
342   // Encode the shift operation Rs or shift_imm (except rrx).
343   if (Rs) {
344     // Encode Rs bit[11:8].
345     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
346     return Binary |
347       (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift);
348   }
349
350   // Encode shift_imm bit[11:7].
351   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
352 }
353
354 unsigned ARMCodeEmitter::getAddrMode1SBit(const MachineInstr &MI,
355                                           const TargetInstrDesc &TID) const {
356   for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
357     const MachineOperand &MO = MI.getOperand(i-1);
358     if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)
359       return 1 << ARMII::S_BitShift;
360   }
361   return 0;
362 }
363
364 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
365   unsigned CPI = MI.getOperand(0).getImm();
366   unsigned CPIndex = MI.getOperand(1).getIndex();
367   const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIndex];
368   
369   // Remember the CONSTPOOL_ENTRY address for later relocation.
370   JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue());
371
372   // Emit constpool island entry. In most cases, the actual values will be
373   // resolved and relocated after code emission.
374   if (MCPE.isMachineConstantPoolEntry()) {
375     ARMConstantPoolValue *ACPV =
376       static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
377
378     DOUT << "\t** ARM constant pool #" << CPI << ", ' @ "
379          << (void*)MCE.getCurrentPCValue() << *ACPV << '\n';
380
381     GlobalValue *GV = ACPV->getGV();
382     if (GV) {
383       assert(!ACPV->isStub() && "Don't know how to deal this yet!");
384       emitGlobalAddress(GV, ARM::reloc_arm_absolute, false);
385     } else  {
386       assert(!ACPV->isNonLazyPointer() && "Don't know how to deal this yet!");
387       emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute);
388     }
389     MCE.emitWordLE(0);
390   } else {
391     Constant *CV = MCPE.Val.ConstVal;
392
393     DOUT << "\t** Constant pool #" << CPI << ", ' @ "
394          << (void*)MCE.getCurrentPCValue() << *CV << '\n';
395
396     if (GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
397       emitGlobalAddress(GV, ARM::reloc_arm_absolute, false);
398       MCE.emitWordLE(0);
399     } else {
400       assert(CV->getType()->isInteger() &&
401              "Not expecting non-integer constpool entries yet!");
402       const ConstantInt *CI = dyn_cast<ConstantInt>(CV);
403       uint32_t Val = *(uint32_t*)CI->getValue().getRawData();
404       MCE.emitWordLE(Val);
405     }
406   }
407 }
408
409 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
410   unsigned Opcode = MI.getDesc().Opcode;
411   switch (Opcode) {
412   default:
413     abort(); // FIXME:
414   case ARM::CONSTPOOL_ENTRY: {
415     emitConstPoolInstruction(MI);
416     break;
417   }
418   }
419 }
420
421 unsigned ARMCodeEmitter::getAddrMode1InstrBinary(const MachineInstr &MI,
422                                                  const TargetInstrDesc &TID,
423                                                  unsigned Binary) {
424   // Set the conditional execution predicate
425   Binary |= II->getPredicate(&MI) << 28;
426
427   // Encode S bit if MI modifies CPSR.
428   Binary |= getAddrMode1SBit(MI, TID);
429
430   // Encode register def if there is one.
431   unsigned NumDefs = TID.getNumDefs();
432   unsigned OpIdx = 0;
433   if (NumDefs) {
434     Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdShift;
435     ++OpIdx;
436   }
437
438   // Encode first non-shifter register operand if there is one.
439   unsigned Format = TID.TSFlags & ARMII::FormMask;
440   bool hasRnOperand= !(Format == ARMII::DPRdMisc  ||
441                        Format == ARMII::DPRdIm    ||
442                        Format == ARMII::DPRdReg   ||
443                        Format == ARMII::DPRdSoReg);
444   if (hasRnOperand) {
445     Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift;
446     ++OpIdx;
447   }
448
449   // Encode shifter operand.
450   bool HasSoReg = (Format == ARMII::DPRdSoReg ||
451                    Format == ARMII::DPRnSoReg ||
452                    Format == ARMII::DPRSoReg  ||
453                    Format == ARMII::DPRSoRegS);
454   if (HasSoReg)
455     // Encode SoReg.
456     return Binary | getMachineSoRegOpValue(MI, TID, OpIdx);
457
458   const MachineOperand &MO = MI.getOperand(OpIdx);
459   if (MO.isReg())
460     // Encode register Rm.
461     return Binary | getMachineOpValue(MI, NumDefs);
462
463   // Encode so_imm.
464   // Set bit I(25) to identify this is the immediate form of <shifter_op>
465   Binary |= 1 << ARMII::I_BitShift;
466   unsigned SoImm = MO.getImm();
467   // Encode rotate_imm.
468   Binary |= ARM_AM::getSOImmValRot(SoImm) << ARMII::RotImmShift;
469   // Encode immed_8.
470   Binary |= ARM_AM::getSOImmVal(SoImm);
471   return Binary;
472 }
473
474 unsigned ARMCodeEmitter::getAddrMode2InstrBinary(const MachineInstr &MI,
475                                                  const TargetInstrDesc &TID,
476                                                  unsigned Binary) {
477   // Set the conditional execution predicate
478   Binary |= II->getPredicate(&MI) << 28;
479
480   // Set first operand
481   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
482
483   // Set second operand
484   Binary |= getMachineOpValue(MI, 1) << ARMII::RegRnShift;
485
486   const MachineOperand &MO2 = MI.getOperand(2);
487   const MachineOperand &MO3 = MI.getOperand(3);
488
489   // Set bit U(23) according to sign of immed value (positive or negative).
490   Binary |= ((ARM_AM::getAM2Op(MO3.getImm()) == ARM_AM::add ? 1 : 0) <<
491              ARMII::U_BitShift);
492   if (!MO2.getReg()) { // is immediate
493     if (ARM_AM::getAM2Offset(MO3.getImm()))
494       // Set the value of offset_12 field
495       Binary |= ARM_AM::getAM2Offset(MO3.getImm());
496     return Binary;
497   }
498
499   // Set bit I(25), because this is not in immediate enconding.
500   Binary |= 1 << ARMII::I_BitShift;
501   assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
502   // Set bit[3:0] to the corresponding Rm register
503   Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
504
505   // if this instr is in scaled register offset/index instruction, set
506   // shift_immed(bit[11:7]) and shift(bit[6:5]) fields.
507   if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm())) {
508     Binary |= getShiftOp(MO3) << 5;  // shift
509     Binary |= ShImm           << 7;  // shift_immed
510   }
511
512   return Binary;
513 }
514
515 unsigned ARMCodeEmitter::getAddrMode3InstrBinary(const MachineInstr &MI,
516                                                  const TargetInstrDesc &TID,
517                                                  unsigned Binary) {
518   // Set the conditional execution predicate
519   Binary |= II->getPredicate(&MI) << 28;
520
521   // Set first operand
522   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift;
523
524   // Set second operand
525   Binary |= getMachineOpValue(MI, 1) << ARMII::RegRnShift;
526
527   const MachineOperand &MO2 = MI.getOperand(2);
528   const MachineOperand &MO3 = MI.getOperand(3);
529
530   // Set bit U(23) according to sign of immed value (positive or negative)
531   Binary |= ((ARM_AM::getAM2Op(MO3.getImm()) == ARM_AM::add ? 1 : 0) <<
532              ARMII::U_BitShift);
533
534   // If this instr is in register offset/index encoding, set bit[3:0]
535   // to the corresponding Rm register.
536   if (MO2.getReg()) {
537     Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg());
538     return Binary;
539   }
540
541   // if this instr is in immediate offset/index encoding, set bit 22 to 1
542   if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm())) {
543     Binary |= 1 << 22;
544     // Set operands
545     Binary |= (ImmOffs >> 4) << 8;  // immedH
546     Binary |= (ImmOffs & ~0xF);     // immedL
547   }
548
549   return Binary;
550 }
551
552 unsigned ARMCodeEmitter::getAddrMode4InstrBinary(const MachineInstr &MI,
553                                                  const TargetInstrDesc &TID,
554                                                  unsigned Binary) {
555   // Set the conditional execution predicate
556   Binary |= II->getPredicate(&MI) << 28;
557
558   // Set first operand
559   Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift;
560
561   // Set addressing mode by modifying bits U(23) and P(24)
562   // IA - Increment after  - bit U = 1 and bit P = 0
563   // IB - Increment before - bit U = 1 and bit P = 1
564   // DA - Decrement after  - bit U = 0 and bit P = 0
565   // DB - Decrement before - bit U = 0 and bit P = 1
566   const MachineOperand &MO = MI.getOperand(1);
567   ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO.getImm());
568   switch (Mode) {
569   default: assert(0 && "Unknown addressing sub-mode!");
570   case ARM_AM::da:                      break;
571   case ARM_AM::db: Binary |= 0x1 << 24; break;
572   case ARM_AM::ia: Binary |= 0x1 << 23; break;
573   case ARM_AM::ib: Binary |= 0x3 << 23; break;
574   }
575
576   // Set bit W(21)
577   if (ARM_AM::getAM4WBFlag(MO.getImm()))
578     Binary |= 0x1 << 21;
579
580   // Set registers
581   for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) {
582     const MachineOperand &MO = MI.getOperand(i);
583     if (MO.isReg() && MO.isImplicit())
584       continue;
585     unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg());
586     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
587            RegNum < 16);
588     Binary |= 0x1 << RegNum;
589   }
590
591   return Binary;
592 }
593
594 /// getInstrBinary - Return binary encoding for the specified
595 /// machine instruction.
596 unsigned ARMCodeEmitter::getInstrBinary(const MachineInstr &MI) {
597   // Part of binary is determined by TableGn.
598   unsigned Binary = getBinaryCodeForInstr(MI);
599
600   const TargetInstrDesc &TID = MI.getDesc();
601   switch (TID.TSFlags & ARMII::AddrModeMask) {
602   case ARMII::AddrModeNone:
603     return getAddrModeNoneInstrBinary(MI, TID, Binary);
604   case ARMII::AddrMode1:
605     return getAddrMode1InstrBinary(MI, TID, Binary);
606   case ARMII::AddrMode2:
607     return getAddrMode2InstrBinary(MI, TID, Binary);
608   case ARMII::AddrMode3:
609     return getAddrMode3InstrBinary(MI, TID, Binary);
610   case ARMII::AddrMode4:
611     return getAddrMode4InstrBinary(MI, TID, Binary);
612   }
613
614   abort();
615   return 0;
616 }
617
618 #include "ARMGenCodeEmitter.inc"