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