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