Teach ARM/MC/ELF to handle R_ARM_JUMP24 relocation type for conditional jumps.
[oota-llvm.git] / lib / Target / ARM / ARMMCCodeEmitter.cpp
1 //===-- ARM/ARMMCCodeEmitter.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 implements the ARMMCCodeEmitter class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "mccodeemitter"
15 #include "ARM.h"
16 #include "ARMAddressingModes.h"
17 #include "ARMFixupKinds.h"
18 #include "ARMInstrInfo.h"
19 #include "ARMMCExpr.h"
20 #include "ARMSubtarget.h"
21 #include "llvm/MC/MCCodeEmitter.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Support/raw_ostream.h"
26 using namespace llvm;
27
28 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
29 STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
30
31 namespace {
32 class ARMMCCodeEmitter : public MCCodeEmitter {
33   ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
34   void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
35   const TargetMachine &TM;
36   const TargetInstrInfo &TII;
37   const ARMSubtarget *Subtarget;
38   MCContext &Ctx;
39
40 public:
41   ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
42     : TM(tm), TII(*TM.getInstrInfo()),
43       Subtarget(&TM.getSubtarget<ARMSubtarget>()), Ctx(ctx) {
44   }
45
46   ~ARMMCCodeEmitter() {}
47
48   unsigned getMachineSoImmOpValue(unsigned SoImm) const;
49
50   // getBinaryCodeForInstr - TableGen'erated function for getting the
51   // binary encoding for an instruction.
52   unsigned getBinaryCodeForInstr(const MCInst &MI,
53                                  SmallVectorImpl<MCFixup> &Fixups) const;
54
55   /// getMachineOpValue - Return binary encoding of operand. If the machine
56   /// operand requires relocation, record the relocation and return zero.
57   unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
58                              SmallVectorImpl<MCFixup> &Fixups) const;
59
60   /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
61   /// the specified operand. This is used for operands with :lower16: and 
62   /// :upper16: prefixes.
63   uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
64                                SmallVectorImpl<MCFixup> &Fixups) const;
65
66   bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
67                               unsigned &Reg, unsigned &Imm,
68                               SmallVectorImpl<MCFixup> &Fixups) const;
69
70   /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
71   /// BL branch target.
72   uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
73                                    SmallVectorImpl<MCFixup> &Fixups) const;
74
75   /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
76   /// BLX branch target.
77   uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
78                                     SmallVectorImpl<MCFixup> &Fixups) const;
79
80   /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
81   uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
82                                    SmallVectorImpl<MCFixup> &Fixups) const;
83
84   /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
85   uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
86                                     SmallVectorImpl<MCFixup> &Fixups) const;
87
88   /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
89   uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
90                                    SmallVectorImpl<MCFixup> &Fixups) const;
91
92   /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
93   /// branch target.
94   uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
95                                   SmallVectorImpl<MCFixup> &Fixups) const;
96
97   /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
98   /// immediate Thumb2 direct branch target.
99   uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
100                                   SmallVectorImpl<MCFixup> &Fixups) const;
101   
102   /// getARMBranchTargetOpValue - Return encoding info for 24-bit immediate
103   /// branch target.
104   uint32_t getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
105                                      SmallVectorImpl<MCFixup> &Fixups) const;
106
107   /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
108   /// ADR label target.
109   uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
110                               SmallVectorImpl<MCFixup> &Fixups) const;
111   uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
112                               SmallVectorImpl<MCFixup> &Fixups) const;
113   uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
114                               SmallVectorImpl<MCFixup> &Fixups) const;
115   
116
117   /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
118   /// operand.
119   uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
120                                    SmallVectorImpl<MCFixup> &Fixups) const;
121
122   /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
123   uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
124                                          SmallVectorImpl<MCFixup> &Fixups)const;
125
126   /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
127   /// operand.
128   uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
129                                    SmallVectorImpl<MCFixup> &Fixups) const;
130
131
132   /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
133   /// operand as needed by load/store instructions.
134   uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
135                                SmallVectorImpl<MCFixup> &Fixups) const;
136
137   /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
138   uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
139                                SmallVectorImpl<MCFixup> &Fixups) const {
140     ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
141     switch (Mode) {
142     default: assert(0 && "Unknown addressing sub-mode!");
143     case ARM_AM::da: return 0;
144     case ARM_AM::ia: return 1;
145     case ARM_AM::db: return 2;
146     case ARM_AM::ib: return 3;
147     }
148   }
149   /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
150   ///
151   unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
152     switch (ShOpc) {
153     default: llvm_unreachable("Unknown shift opc!");
154     case ARM_AM::no_shift:
155     case ARM_AM::lsl: return 0;
156     case ARM_AM::lsr: return 1;
157     case ARM_AM::asr: return 2;
158     case ARM_AM::ror:
159     case ARM_AM::rrx: return 3;
160     }
161     return 0;
162   }
163
164   /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
165   uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
166                                SmallVectorImpl<MCFixup> &Fixups) const;
167
168   /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
169   uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
170                                      SmallVectorImpl<MCFixup> &Fixups) const;
171
172   /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
173   uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
174                                      SmallVectorImpl<MCFixup> &Fixups) const;
175
176   /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
177   uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
178                                SmallVectorImpl<MCFixup> &Fixups) const;
179
180   /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
181   /// operand.
182   uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
183                                      SmallVectorImpl<MCFixup> &Fixups) const;
184
185   /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
186   uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
187                                 SmallVectorImpl<MCFixup> &Fixups) const;
188
189   /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
190   uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
191                                 SmallVectorImpl<MCFixup> &Fixups) const;
192
193   /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
194   uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
195                                SmallVectorImpl<MCFixup> &Fixups) const;
196
197   /// getCCOutOpValue - Return encoding of the 's' bit.
198   unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
199                            SmallVectorImpl<MCFixup> &Fixups) const {
200     // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
201     // '1' respectively.
202     return MI.getOperand(Op).getReg() == ARM::CPSR;
203   }
204
205   /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
206   unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
207                            SmallVectorImpl<MCFixup> &Fixups) const {
208     unsigned SoImm = MI.getOperand(Op).getImm();
209     int SoImmVal = ARM_AM::getSOImmVal(SoImm);
210     assert(SoImmVal != -1 && "Not a valid so_imm value!");
211
212     // Encode rotate_imm.
213     unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
214       << ARMII::SoRotImmShift;
215
216     // Encode immed_8.
217     Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
218     return Binary;
219   }
220
221   /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
222   unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
223                            SmallVectorImpl<MCFixup> &Fixups) const {
224     unsigned SoImm = MI.getOperand(Op).getImm();
225     unsigned Encoded =  ARM_AM::getT2SOImmVal(SoImm);
226     assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
227     return Encoded;
228   }
229
230   unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
231     SmallVectorImpl<MCFixup> &Fixups) const;
232   unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
233     SmallVectorImpl<MCFixup> &Fixups) const;
234   unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
235     SmallVectorImpl<MCFixup> &Fixups) const;
236   unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
237     SmallVectorImpl<MCFixup> &Fixups) const;
238
239   /// getSORegOpValue - Return an encoded so_reg shifted register value.
240   unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
241                            SmallVectorImpl<MCFixup> &Fixups) const;
242   unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
243                              SmallVectorImpl<MCFixup> &Fixups) const;
244
245   unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
246                             SmallVectorImpl<MCFixup> &Fixups) const {
247     switch (MI.getOperand(Op).getImm()) {
248     default: assert (0 && "Not a valid rot_imm value!");
249     case 0:  return 0;
250     case 8:  return 1;
251     case 16: return 2;
252     case 24: return 3;
253     }
254   }
255
256   unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
257                                  SmallVectorImpl<MCFixup> &Fixups) const {
258     return MI.getOperand(Op).getImm() - 1;
259   }
260
261   unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
262                                    SmallVectorImpl<MCFixup> &Fixups) const {
263     return 64 - MI.getOperand(Op).getImm();
264   }
265
266   unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
267                                       SmallVectorImpl<MCFixup> &Fixups) const;
268
269   unsigned getMsbOpValue(const MCInst &MI, unsigned Op,
270                          SmallVectorImpl<MCFixup> &Fixups) const;
271
272   unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
273                                   SmallVectorImpl<MCFixup> &Fixups) const;
274   unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
275                                       SmallVectorImpl<MCFixup> &Fixups) const;
276   unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
277                                         SmallVectorImpl<MCFixup> &Fixups) const;
278   unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
279                                      SmallVectorImpl<MCFixup> &Fixups) const;
280
281   unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
282                                       unsigned EncodedValue) const;
283   unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
284                                           unsigned EncodedValue) const;
285   unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
286                                     unsigned EncodedValue) const;
287
288   unsigned VFPThumb2PostEncoder(const MCInst &MI,
289                                 unsigned EncodedValue) const;
290
291   void EmitByte(unsigned char C, raw_ostream &OS) const {
292     OS << (char)C;
293   }
294
295   void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
296     // Output the constant in little endian byte order.
297     for (unsigned i = 0; i != Size; ++i) {
298       EmitByte(Val & 255, OS);
299       Val >>= 8;
300     }
301   }
302
303   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
304                          SmallVectorImpl<MCFixup> &Fixups) const;
305 };
306
307 } // end anonymous namespace
308
309 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
310                                             MCContext &Ctx) {
311   return new ARMMCCodeEmitter(TM, Ctx);
312 }
313
314 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
315 /// instructions, and rewrite them to their Thumb2 form if we are currently in
316 /// Thumb2 mode.
317 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
318                                                  unsigned EncodedValue) const {
319   if (Subtarget->isThumb2()) {
320     // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
321     // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
322     // set to 1111.
323     unsigned Bit24 = EncodedValue & 0x01000000;
324     unsigned Bit28 = Bit24 << 4;
325     EncodedValue &= 0xEFFFFFFF;
326     EncodedValue |= Bit28;
327     EncodedValue |= 0x0F000000;
328   }
329
330   return EncodedValue;
331 }
332
333 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
334 /// instructions, and rewrite them to their Thumb2 form if we are currently in
335 /// Thumb2 mode.
336 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
337                                                  unsigned EncodedValue) const {
338   if (Subtarget->isThumb2()) {
339     EncodedValue &= 0xF0FFFFFF;
340     EncodedValue |= 0x09000000;
341   }
342
343   return EncodedValue;
344 }
345
346 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
347 /// instructions, and rewrite them to their Thumb2 form if we are currently in
348 /// Thumb2 mode.
349 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
350                                                  unsigned EncodedValue) const {
351   if (Subtarget->isThumb2()) {
352     EncodedValue &= 0x00FFFFFF;
353     EncodedValue |= 0xEE000000;
354   }
355
356   return EncodedValue;
357 }
358
359 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
360 /// them to their Thumb2 form if we are currently in Thumb2 mode.
361 unsigned ARMMCCodeEmitter::
362 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
363   if (Subtarget->isThumb2()) {
364     EncodedValue &= 0x0FFFFFFF;
365     EncodedValue |= 0xE0000000;
366   }
367   return EncodedValue;
368 }
369
370 /// getMachineOpValue - Return binary encoding of operand. If the machine
371 /// operand requires relocation, record the relocation and return zero.
372 unsigned ARMMCCodeEmitter::
373 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
374                   SmallVectorImpl<MCFixup> &Fixups) const {
375   if (MO.isReg()) {
376     unsigned Reg = MO.getReg();
377     unsigned RegNo = getARMRegisterNumbering(Reg);
378
379     // Q registers are encoded as 2x their register number.
380     switch (Reg) {
381     default:
382       return RegNo;
383     case ARM::Q0:  case ARM::Q1:  case ARM::Q2:  case ARM::Q3:
384     case ARM::Q4:  case ARM::Q5:  case ARM::Q6:  case ARM::Q7:
385     case ARM::Q8:  case ARM::Q9:  case ARM::Q10: case ARM::Q11:
386     case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
387       return 2 * RegNo;
388     }
389   } else if (MO.isImm()) {
390     return static_cast<unsigned>(MO.getImm());
391   } else if (MO.isFPImm()) {
392     return static_cast<unsigned>(APFloat(MO.getFPImm())
393                      .bitcastToAPInt().getHiBits(32).getLimitedValue());
394   }
395
396   llvm_unreachable("Unable to encode MCOperand!");
397   return 0;
398 }
399
400 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
401 bool ARMMCCodeEmitter::
402 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
403                        unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
404   const MCOperand &MO  = MI.getOperand(OpIdx);
405   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
406
407   Reg = getARMRegisterNumbering(MO.getReg());
408
409   int32_t SImm = MO1.getImm();
410   bool isAdd = true;
411
412   // Special value for #-0
413   if (SImm == INT32_MIN)
414     SImm = 0;
415
416   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
417   if (SImm < 0) {
418     SImm = -SImm;
419     isAdd = false;
420   }
421
422   Imm = SImm;
423   return isAdd;
424 }
425
426 /// getBranchTargetOpValue - Helper function to get the branch target operand,
427 /// which is either an immediate or requires a fixup.
428 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
429                                        unsigned FixupKind,
430                                        SmallVectorImpl<MCFixup> &Fixups) {
431   const MCOperand &MO = MI.getOperand(OpIdx);
432
433   // If the destination is an immediate, we have nothing to do.
434   if (MO.isImm()) return MO.getImm();
435   assert(MO.isExpr() && "Unexpected branch target type!");
436   const MCExpr *Expr = MO.getExpr();
437   MCFixupKind Kind = MCFixupKind(FixupKind);
438   Fixups.push_back(MCFixup::Create(0, Expr, Kind));
439
440   // All of the information is in the fixup.
441   return 0;
442 }
443
444 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
445 uint32_t ARMMCCodeEmitter::
446 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
447                         SmallVectorImpl<MCFixup> &Fixups) const {
448   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
449 }
450
451 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
452 /// BLX branch target.
453 uint32_t ARMMCCodeEmitter::
454 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
455                          SmallVectorImpl<MCFixup> &Fixups) const {
456   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
457 }
458
459 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
460 uint32_t ARMMCCodeEmitter::
461 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
462                         SmallVectorImpl<MCFixup> &Fixups) const {
463   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
464 }
465
466 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
467 uint32_t ARMMCCodeEmitter::
468 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
469                          SmallVectorImpl<MCFixup> &Fixups) const {
470   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
471 }
472
473 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
474 uint32_t ARMMCCodeEmitter::
475 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
476                         SmallVectorImpl<MCFixup> &Fixups) const {
477   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
478 }
479
480 /// Return true if this branch has a non-always predication
481 static bool HasConditionalBranch(const MCInst &MI) {
482   int NumOp = MI.getNumOperands();
483   if (NumOp >= 2) {
484     for (int i = 0; i < NumOp-1; ++i) {
485       const MCOperand &MCOp1 = MI.getOperand(i);
486       const MCOperand &MCOp2 = MI.getOperand(i + 1);
487       if (MCOp1.isImm() && MCOp2.isReg() && 
488           (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
489         if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL) 
490           return true;
491       }
492     }
493   }
494   return false;
495 }
496
497 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
498 /// target.
499 uint32_t ARMMCCodeEmitter::
500 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
501                        SmallVectorImpl<MCFixup> &Fixups) const {
502   // FIXME: This really, really shouldn't use TargetMachine. We don't want
503   // coupling between MC and TM anywhere we can help it.
504   if (Subtarget->isThumb2())
505     return
506       ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
507   return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
508 }
509
510 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
511 /// target.
512 uint32_t ARMMCCodeEmitter::
513 getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
514                           SmallVectorImpl<MCFixup> &Fixups) const {
515   if (HasConditionalBranch(MI)) 
516     return ::getBranchTargetOpValue(MI, OpIdx,
517                                     ARM::fixup_arm_condbranch, Fixups);
518   return ::getBranchTargetOpValue(MI, OpIdx, 
519                                   ARM::fixup_arm_uncondbranch, Fixups);
520 }
521
522
523
524
525 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
526 /// immediate branch target.
527 uint32_t ARMMCCodeEmitter::
528 getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
529                        SmallVectorImpl<MCFixup> &Fixups) const {
530   unsigned Val =
531     ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
532   bool I  = (Val & 0x800000);
533   bool J1 = (Val & 0x400000);
534   bool J2 = (Val & 0x200000);
535   if (I ^ J1)
536     Val &= ~0x400000;
537   else
538     Val |= 0x400000;
539     
540   if (I ^ J2)
541     Val &= ~0x200000;
542   else
543     Val |= 0x200000;
544   
545   return Val;
546 }
547
548 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
549 /// target.
550 uint32_t ARMMCCodeEmitter::
551 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
552                    SmallVectorImpl<MCFixup> &Fixups) const {
553   assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
554   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
555                                   Fixups);
556 }
557
558 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
559 /// target.
560 uint32_t ARMMCCodeEmitter::
561 getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
562                    SmallVectorImpl<MCFixup> &Fixups) const {
563   assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
564   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
565                                   Fixups);
566 }
567
568 /// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
569 /// target.
570 uint32_t ARMMCCodeEmitter::
571 getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
572                    SmallVectorImpl<MCFixup> &Fixups) const {
573   assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
574   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
575                                   Fixups);
576 }
577
578 /// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
579 /// operand.
580 uint32_t ARMMCCodeEmitter::
581 getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
582                               SmallVectorImpl<MCFixup> &) const {
583   // [Rn, Rm]
584   //   {5-3} = Rm
585   //   {2-0} = Rn
586   const MCOperand &MO1 = MI.getOperand(OpIdx);
587   const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
588   unsigned Rn = getARMRegisterNumbering(MO1.getReg());
589   unsigned Rm = getARMRegisterNumbering(MO2.getReg());
590   return (Rm << 3) | Rn;
591 }
592
593 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
594 uint32_t ARMMCCodeEmitter::
595 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
596                         SmallVectorImpl<MCFixup> &Fixups) const {
597   // {17-13} = reg
598   // {12}    = (U)nsigned (add == '1', sub == '0')
599   // {11-0}  = imm12
600   unsigned Reg, Imm12;
601   bool isAdd = true;
602   // If The first operand isn't a register, we have a label reference.
603   const MCOperand &MO = MI.getOperand(OpIdx);
604   const MCOperand &MO2 = MI.getOperand(OpIdx+1);
605   if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
606     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
607     Imm12 = 0;
608     isAdd = false ; // 'U' bit is set as part of the fixup.
609
610     const MCExpr *Expr = 0;
611     if (!MO.isReg())
612       Expr = MO.getExpr();
613     else
614       Expr = MO2.getExpr();
615
616     MCFixupKind Kind;
617     if (Subtarget->isThumb2())
618       Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
619     else
620       Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
621     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
622
623     ++MCNumCPRelocations;
624   } else
625     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
626
627   uint32_t Binary = Imm12 & 0xfff;
628   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
629   if (isAdd)
630     Binary |= (1 << 12);
631   Binary |= (Reg << 13);
632   return Binary;
633 }
634
635 /// getT2AddrModeImm8s4OpValue - Return encoding info for
636 /// 'reg +/- imm8<<2' operand.
637 uint32_t ARMMCCodeEmitter::
638 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
639                         SmallVectorImpl<MCFixup> &Fixups) const {
640   // {12-9} = reg
641   // {8}    = (U)nsigned (add == '1', sub == '0')
642   // {7-0}  = imm8
643   unsigned Reg, Imm8;
644   bool isAdd = true;
645   // If The first operand isn't a register, we have a label reference.
646   const MCOperand &MO = MI.getOperand(OpIdx);
647   if (!MO.isReg()) {
648     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
649     Imm8 = 0;
650     isAdd = false ; // 'U' bit is set as part of the fixup.
651
652     assert(MO.isExpr() && "Unexpected machine operand type!");
653     const MCExpr *Expr = MO.getExpr();
654     MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
655     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
656
657     ++MCNumCPRelocations;
658   } else
659     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
660
661   uint32_t Binary = (Imm8 >> 2) & 0xff;
662   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
663   if (isAdd)
664     Binary |= (1 << 8);
665   Binary |= (Reg << 9);
666   return Binary;
667 }
668
669 // FIXME: This routine assumes that a binary
670 // expression will always result in a PCRel expression
671 // In reality, its only true if one or more subexpressions
672 // is itself a PCRel (i.e. "." in asm or some other pcrel construct)
673 // but this is good enough for now.
674 static bool EvaluateAsPCRel(const MCExpr *Expr) {
675   switch (Expr->getKind()) {
676   default: assert(0 && "Unexpected expression type");
677   case MCExpr::SymbolRef: return false;
678   case MCExpr::Binary: return true;
679   }
680 }
681
682 uint32_t
683 ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
684                                       SmallVectorImpl<MCFixup> &Fixups) const {
685   // {20-16} = imm{15-12}
686   // {11-0}  = imm{11-0}
687   const MCOperand &MO = MI.getOperand(OpIdx);
688   if (MO.isImm())
689     // Hi / lo 16 bits already extracted during earlier passes.
690     return static_cast<unsigned>(MO.getImm());
691
692   // Handle :upper16: and :lower16: assembly prefixes.
693   const MCExpr *E = MO.getExpr();
694   if (E->getKind() == MCExpr::Target) {
695     const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
696     E = ARM16Expr->getSubExpr();
697
698     MCFixupKind Kind;
699     switch (ARM16Expr->getKind()) {
700     default: assert(0 && "Unsupported ARMFixup");
701     case ARMMCExpr::VK_ARM_HI16:
702       if (!Subtarget->isTargetDarwin() && EvaluateAsPCRel(E)) 
703         Kind = MCFixupKind(Subtarget->isThumb2()
704                            ? ARM::fixup_t2_movt_hi16_pcrel
705                            : ARM::fixup_arm_movt_hi16_pcrel);
706       else
707         Kind = MCFixupKind(Subtarget->isThumb2()
708                            ? ARM::fixup_t2_movt_hi16
709                            : ARM::fixup_arm_movt_hi16);
710       break;
711     case ARMMCExpr::VK_ARM_LO16:
712       if (!Subtarget->isTargetDarwin() && EvaluateAsPCRel(E)) 
713         Kind = MCFixupKind(Subtarget->isThumb2()
714                            ? ARM::fixup_t2_movw_lo16_pcrel
715                            : ARM::fixup_arm_movw_lo16_pcrel);
716       else
717         Kind = MCFixupKind(Subtarget->isThumb2()
718                            ? ARM::fixup_t2_movw_lo16
719                            : ARM::fixup_arm_movw_lo16);
720       break;
721     }
722     Fixups.push_back(MCFixup::Create(0, E, Kind));
723     return 0;
724   };
725
726   llvm_unreachable("Unsupported MCExpr type in MCOperand!");
727   return 0;
728 }
729
730 uint32_t ARMMCCodeEmitter::
731 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
732                     SmallVectorImpl<MCFixup> &Fixups) const {
733   const MCOperand &MO = MI.getOperand(OpIdx);
734   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
735   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
736   unsigned Rn = getARMRegisterNumbering(MO.getReg());
737   unsigned Rm = getARMRegisterNumbering(MO1.getReg());
738   unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
739   bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
740   ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
741   unsigned SBits = getShiftOp(ShOp);
742
743   // {16-13} = Rn
744   // {12}    = isAdd
745   // {11-0}  = shifter
746   //  {3-0}  = Rm
747   //  {4}    = 0
748   //  {6-5}  = type
749   //  {11-7} = imm
750   uint32_t Binary = Rm;
751   Binary |= Rn << 13;
752   Binary |= SBits << 5;
753   Binary |= ShImm << 7;
754   if (isAdd)
755     Binary |= 1 << 12;
756   return Binary;
757 }
758
759 uint32_t ARMMCCodeEmitter::
760 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
761                     SmallVectorImpl<MCFixup> &Fixups) const {
762   // {17-14}  Rn
763   // {13}     1 == imm12, 0 == Rm
764   // {12}     isAdd
765   // {11-0}   imm12/Rm
766   const MCOperand &MO = MI.getOperand(OpIdx);
767   unsigned Rn = getARMRegisterNumbering(MO.getReg());
768   uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
769   Binary |= Rn << 14;
770   return Binary;
771 }
772
773 uint32_t ARMMCCodeEmitter::
774 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
775                           SmallVectorImpl<MCFixup> &Fixups) const {
776   // {13}     1 == imm12, 0 == Rm
777   // {12}     isAdd
778   // {11-0}   imm12/Rm
779   const MCOperand &MO = MI.getOperand(OpIdx);
780   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
781   unsigned Imm = MO1.getImm();
782   bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
783   bool isReg = MO.getReg() != 0;
784   uint32_t Binary = ARM_AM::getAM2Offset(Imm);
785   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
786   if (isReg) {
787     ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
788     Binary <<= 7;                    // Shift amount is bits [11:7]
789     Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
790     Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
791   }
792   return Binary | (isAdd << 12) | (isReg << 13);
793 }
794
795 uint32_t ARMMCCodeEmitter::
796 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
797                           SmallVectorImpl<MCFixup> &Fixups) const {
798   // {9}      1 == imm8, 0 == Rm
799   // {8}      isAdd
800   // {7-4}    imm7_4/zero
801   // {3-0}    imm3_0/Rm
802   const MCOperand &MO = MI.getOperand(OpIdx);
803   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
804   unsigned Imm = MO1.getImm();
805   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
806   bool isImm = MO.getReg() == 0;
807   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
808   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
809   if (!isImm)
810     Imm8 = getARMRegisterNumbering(MO.getReg());
811   return Imm8 | (isAdd << 8) | (isImm << 9);
812 }
813
814 uint32_t ARMMCCodeEmitter::
815 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
816                     SmallVectorImpl<MCFixup> &Fixups) const {
817   // {13}     1 == imm8, 0 == Rm
818   // {12-9}   Rn
819   // {8}      isAdd
820   // {7-4}    imm7_4/zero
821   // {3-0}    imm3_0/Rm
822   const MCOperand &MO = MI.getOperand(OpIdx);
823   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
824   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
825   unsigned Rn = getARMRegisterNumbering(MO.getReg());
826   unsigned Imm = MO2.getImm();
827   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
828   bool isImm = MO1.getReg() == 0;
829   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
830   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
831   if (!isImm)
832     Imm8 = getARMRegisterNumbering(MO1.getReg());
833   return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
834 }
835
836 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
837 uint32_t ARMMCCodeEmitter::
838 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
839                           SmallVectorImpl<MCFixup> &Fixups) const {
840   // [SP, #imm]
841   //   {7-0} = imm8
842   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
843   assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
844          "Unexpected base register!");
845
846   // The immediate is already shifted for the implicit zeroes, so no change
847   // here.
848   return MO1.getImm() & 0xff;
849 }
850
851 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
852 uint32_t ARMMCCodeEmitter::
853 getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
854                      SmallVectorImpl<MCFixup> &Fixups) const {
855   // [Rn, #imm]
856   //   {7-3} = imm5
857   //   {2-0} = Rn
858   const MCOperand &MO = MI.getOperand(OpIdx);
859   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
860   unsigned Rn = getARMRegisterNumbering(MO.getReg());
861   unsigned Imm5 = MO1.getImm();
862   return ((Imm5 & 0x1f) << 3) | Rn;
863 }
864
865 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
866 uint32_t ARMMCCodeEmitter::
867 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
868                      SmallVectorImpl<MCFixup> &Fixups) const {
869   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
870 }
871
872 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
873 uint32_t ARMMCCodeEmitter::
874 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
875                     SmallVectorImpl<MCFixup> &Fixups) const {
876   // {12-9} = reg
877   // {8}    = (U)nsigned (add == '1', sub == '0')
878   // {7-0}  = imm8
879   unsigned Reg, Imm8;
880   bool isAdd;
881   // If The first operand isn't a register, we have a label reference.
882   const MCOperand &MO = MI.getOperand(OpIdx);
883   if (!MO.isReg()) {
884     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
885     Imm8 = 0;
886     isAdd = false; // 'U' bit is handled as part of the fixup.
887
888     assert(MO.isExpr() && "Unexpected machine operand type!");
889     const MCExpr *Expr = MO.getExpr();
890     MCFixupKind Kind;
891     if (Subtarget->isThumb2())
892       Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
893     else
894       Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
895     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
896
897     ++MCNumCPRelocations;
898   } else {
899     EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
900     isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
901   }
902
903   uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
904   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
905   if (isAdd)
906     Binary |= (1 << 8);
907   Binary |= (Reg << 9);
908   return Binary;
909 }
910
911 unsigned ARMMCCodeEmitter::
912 getSORegOpValue(const MCInst &MI, unsigned OpIdx,
913                 SmallVectorImpl<MCFixup> &Fixups) const {
914   // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
915   // shifted. The second is either Rs, the amount to shift by, or reg0 in which
916   // case the imm contains the amount to shift by.
917   //
918   // {3-0} = Rm.
919   // {4}   = 1 if reg shift, 0 if imm shift
920   // {6-5} = type
921   //    If reg shift:
922   //      {11-8} = Rs
923   //      {7}    = 0
924   //    else (imm shift)
925   //      {11-7} = imm
926
927   const MCOperand &MO  = MI.getOperand(OpIdx);
928   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
929   const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
930   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
931
932   // Encode Rm.
933   unsigned Binary = getARMRegisterNumbering(MO.getReg());
934
935   // Encode the shift opcode.
936   unsigned SBits = 0;
937   unsigned Rs = MO1.getReg();
938   if (Rs) {
939     // Set shift operand (bit[7:4]).
940     // LSL - 0001
941     // LSR - 0011
942     // ASR - 0101
943     // ROR - 0111
944     // RRX - 0110 and bit[11:8] clear.
945     switch (SOpc) {
946     default: llvm_unreachable("Unknown shift opc!");
947     case ARM_AM::lsl: SBits = 0x1; break;
948     case ARM_AM::lsr: SBits = 0x3; break;
949     case ARM_AM::asr: SBits = 0x5; break;
950     case ARM_AM::ror: SBits = 0x7; break;
951     case ARM_AM::rrx: SBits = 0x6; break;
952     }
953   } else {
954     // Set shift operand (bit[6:4]).
955     // LSL - 000
956     // LSR - 010
957     // ASR - 100
958     // ROR - 110
959     switch (SOpc) {
960     default: llvm_unreachable("Unknown shift opc!");
961     case ARM_AM::lsl: SBits = 0x0; break;
962     case ARM_AM::lsr: SBits = 0x2; break;
963     case ARM_AM::asr: SBits = 0x4; break;
964     case ARM_AM::ror: SBits = 0x6; break;
965     }
966   }
967
968   Binary |= SBits << 4;
969   if (SOpc == ARM_AM::rrx)
970     return Binary;
971
972   // Encode the shift operation Rs or shift_imm (except rrx).
973   if (Rs) {
974     // Encode Rs bit[11:8].
975     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
976     return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
977   }
978
979   // Encode shift_imm bit[11:7].
980   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
981 }
982
983 unsigned ARMMCCodeEmitter::
984 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
985                 SmallVectorImpl<MCFixup> &Fixups) const {
986   const MCOperand &MO1 = MI.getOperand(OpNum);
987   const MCOperand &MO2 = MI.getOperand(OpNum+1);
988   const MCOperand &MO3 = MI.getOperand(OpNum+2);
989
990   // Encoded as [Rn, Rm, imm].
991   // FIXME: Needs fixup support.
992   unsigned Value = getARMRegisterNumbering(MO1.getReg());
993   Value <<= 4;
994   Value |= getARMRegisterNumbering(MO2.getReg());
995   Value <<= 2;
996   Value |= MO3.getImm();
997
998   return Value;
999 }
1000
1001 unsigned ARMMCCodeEmitter::
1002 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1003                          SmallVectorImpl<MCFixup> &Fixups) const {
1004   const MCOperand &MO1 = MI.getOperand(OpNum);
1005   const MCOperand &MO2 = MI.getOperand(OpNum+1);
1006
1007   // FIXME: Needs fixup support.
1008   unsigned Value = getARMRegisterNumbering(MO1.getReg());
1009
1010   // Even though the immediate is 8 bits long, we need 9 bits in order
1011   // to represent the (inverse of the) sign bit.
1012   Value <<= 9;
1013   int32_t tmp = (int32_t)MO2.getImm();
1014   if (tmp < 0)
1015     tmp = abs(tmp);
1016   else
1017     Value |= 256; // Set the ADD bit
1018   Value |= tmp & 255;
1019   return Value;
1020 }
1021
1022 unsigned ARMMCCodeEmitter::
1023 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1024                          SmallVectorImpl<MCFixup> &Fixups) const {
1025   const MCOperand &MO1 = MI.getOperand(OpNum);
1026
1027   // FIXME: Needs fixup support.
1028   unsigned Value = 0;
1029   int32_t tmp = (int32_t)MO1.getImm();
1030   if (tmp < 0)
1031     tmp = abs(tmp);
1032   else
1033     Value |= 256; // Set the ADD bit
1034   Value |= tmp & 255;
1035   return Value;
1036 }
1037
1038 unsigned ARMMCCodeEmitter::
1039 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1040                          SmallVectorImpl<MCFixup> &Fixups) const {
1041   const MCOperand &MO1 = MI.getOperand(OpNum);
1042
1043   // FIXME: Needs fixup support.
1044   unsigned Value = 0;
1045   int32_t tmp = (int32_t)MO1.getImm();
1046   if (tmp < 0)
1047     tmp = abs(tmp);
1048   else
1049     Value |= 4096; // Set the ADD bit
1050   Value |= tmp & 4095;
1051   return Value;
1052 }
1053
1054 unsigned ARMMCCodeEmitter::
1055 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1056                 SmallVectorImpl<MCFixup> &Fixups) const {
1057   // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1058   // shifted. The second is the amount to shift by.
1059   //
1060   // {3-0} = Rm.
1061   // {4}   = 0
1062   // {6-5} = type
1063   // {11-7} = imm
1064
1065   const MCOperand &MO  = MI.getOperand(OpIdx);
1066   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1067   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1068
1069   // Encode Rm.
1070   unsigned Binary = getARMRegisterNumbering(MO.getReg());
1071
1072   // Encode the shift opcode.
1073   unsigned SBits = 0;
1074   // Set shift operand (bit[6:4]).
1075   // LSL - 000
1076   // LSR - 010
1077   // ASR - 100
1078   // ROR - 110
1079   switch (SOpc) {
1080   default: llvm_unreachable("Unknown shift opc!");
1081   case ARM_AM::lsl: SBits = 0x0; break;
1082   case ARM_AM::lsr: SBits = 0x2; break;
1083   case ARM_AM::asr: SBits = 0x4; break;
1084   case ARM_AM::ror: SBits = 0x6; break;
1085   }
1086
1087   Binary |= SBits << 4;
1088   if (SOpc == ARM_AM::rrx)
1089     return Binary;
1090
1091   // Encode shift_imm bit[11:7].
1092   return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1093 }
1094
1095 unsigned ARMMCCodeEmitter::
1096 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1097                                SmallVectorImpl<MCFixup> &Fixups) const {
1098   // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1099   // msb of the mask.
1100   const MCOperand &MO = MI.getOperand(Op);
1101   uint32_t v = ~MO.getImm();
1102   uint32_t lsb = CountTrailingZeros_32(v);
1103   uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1104   assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1105   return lsb | (msb << 5);
1106 }
1107
1108 unsigned ARMMCCodeEmitter::
1109 getMsbOpValue(const MCInst &MI, unsigned Op,
1110               SmallVectorImpl<MCFixup> &Fixups) const {
1111   // MSB - 5 bits.
1112   uint32_t lsb = MI.getOperand(Op-1).getImm();
1113   uint32_t width = MI.getOperand(Op).getImm();
1114   uint32_t msb = lsb+width-1;
1115   assert (width != 0 && msb < 32 && "Illegal bit width!");
1116   return msb;
1117 }
1118
1119 unsigned ARMMCCodeEmitter::
1120 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1121                        SmallVectorImpl<MCFixup> &Fixups) const {
1122   // VLDM/VSTM:
1123   //   {12-8} = Vd
1124   //   {7-0}  = Number of registers
1125   //
1126   // LDM/STM:
1127   //   {15-0}  = Bitfield of GPRs.
1128   unsigned Reg = MI.getOperand(Op).getReg();
1129   bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1130   bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1131
1132   unsigned Binary = 0;
1133
1134   if (SPRRegs || DPRRegs) {
1135     // VLDM/VSTM
1136     unsigned RegNo = getARMRegisterNumbering(Reg);
1137     unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1138     Binary |= (RegNo & 0x1f) << 8;
1139     if (SPRRegs)
1140       Binary |= NumRegs;
1141     else
1142       Binary |= NumRegs * 2;
1143   } else {
1144     for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1145       unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1146       Binary |= 1 << RegNo;
1147     }
1148   }
1149
1150   return Binary;
1151 }
1152
1153 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1154 /// with the alignment operand.
1155 unsigned ARMMCCodeEmitter::
1156 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1157                            SmallVectorImpl<MCFixup> &Fixups) const {
1158   const MCOperand &Reg = MI.getOperand(Op);
1159   const MCOperand &Imm = MI.getOperand(Op + 1);
1160
1161   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1162   unsigned Align = 0;
1163
1164   switch (Imm.getImm()) {
1165   default: break;
1166   case 2:
1167   case 4:
1168   case 8:  Align = 0x01; break;
1169   case 16: Align = 0x02; break;
1170   case 32: Align = 0x03; break;
1171   }
1172
1173   return RegNo | (Align << 4);
1174 }
1175
1176 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1177 /// alignment operand for use in VLD-dup instructions.  This is the same as
1178 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1179 /// different for VLD4-dup.
1180 unsigned ARMMCCodeEmitter::
1181 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1182                               SmallVectorImpl<MCFixup> &Fixups) const {
1183   const MCOperand &Reg = MI.getOperand(Op);
1184   const MCOperand &Imm = MI.getOperand(Op + 1);
1185
1186   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1187   unsigned Align = 0;
1188
1189   switch (Imm.getImm()) {
1190   default: break;
1191   case 2:
1192   case 4:
1193   case 8:  Align = 0x01; break;
1194   case 16: Align = 0x03; break;
1195   }
1196
1197   return RegNo | (Align << 4);
1198 }
1199
1200 unsigned ARMMCCodeEmitter::
1201 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1202                           SmallVectorImpl<MCFixup> &Fixups) const {
1203   const MCOperand &MO = MI.getOperand(Op);
1204   if (MO.getReg() == 0) return 0x0D;
1205   return MO.getReg();
1206 }
1207
1208 void ARMMCCodeEmitter::
1209 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1210                   SmallVectorImpl<MCFixup> &Fixups) const {
1211   // Pseudo instructions don't get encoded.
1212   const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
1213   uint64_t TSFlags = Desc.TSFlags;
1214   if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1215     return;
1216   int Size;
1217   // Basic size info comes from the TSFlags field.
1218   switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1219   default: llvm_unreachable("Unexpected instruction size!");
1220   case ARMII::Size2Bytes: Size = 2; break;
1221   case ARMII::Size4Bytes: Size = 4; break;
1222   }
1223   uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1224   // Thumb 32-bit wide instructions need to emit the high order halfword
1225   // first.
1226   if (Subtarget->isThumb() && Size == 4) {
1227     EmitConstant(Binary >> 16, 2, OS);
1228     EmitConstant(Binary & 0xffff, 2, OS);
1229   } else
1230     EmitConstant(Binary, Size, OS);
1231   ++MCNumEmitted;  // Keep track of the # of mi's emitted.
1232 }
1233
1234 #include "ARMGenMCCodeEmitter.inc"