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