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