5d4651cf274c2b33a8870f4cf6692ffdbd3f93e0
[oota-llvm.git] / lib / Target / ARM64 / MCTargetDesc / ARM64MCCodeEmitter.cpp
1 //===-- ARM64/ARM64MCCodeEmitter.cpp - Convert ARM64 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 ARM64MCCodeEmitter class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "mccodeemitter"
15 #include "MCTargetDesc/ARM64AddressingModes.h"
16 #include "MCTargetDesc/ARM64FixupKinds.h"
17 #include "MCTargetDesc/ARM64MCExpr.h"
18 #include "Utils/ARM64BaseInfo.h"
19 #include "llvm/MC/MCCodeEmitter.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/Support/raw_ostream.h"
27 using namespace llvm;
28
29 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
30 STATISTIC(MCNumFixups, "Number of MC fixups created.");
31
32 namespace {
33
34 class ARM64MCCodeEmitter : public MCCodeEmitter {
35   MCContext &Ctx;
36
37   ARM64MCCodeEmitter(const ARM64MCCodeEmitter &); // DO NOT IMPLEMENT
38   void operator=(const ARM64MCCodeEmitter &);     // DO NOT IMPLEMENT
39 public:
40   ARM64MCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
41                      MCContext &ctx)
42       : Ctx(ctx) {}
43
44   ~ARM64MCCodeEmitter() {}
45
46   // getBinaryCodeForInstr - TableGen'erated function for getting the
47   // binary encoding for an instruction.
48   uint64_t getBinaryCodeForInstr(const MCInst &MI,
49                                  SmallVectorImpl<MCFixup> &Fixups,
50                                  const MCSubtargetInfo &STI) const;
51
52   /// getMachineOpValue - Return binary encoding of operand. If the machine
53   /// operand requires relocation, record the relocation and return zero.
54   unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
55                              SmallVectorImpl<MCFixup> &Fixups,
56                              const MCSubtargetInfo &STI) const;
57
58   /// getAMIndexed8OpValue - Return encoding info for base register
59   /// and 12-bit unsigned immediate attached to a load, store or prfm
60   /// instruction. If operand requires a relocation, record it and
61   /// return zero in that part of the encoding.
62   template <uint32_t FixupKind>
63   uint32_t getAMIndexed8OpValue(const MCInst &MI, unsigned OpIdx,
64                                 SmallVectorImpl<MCFixup> &Fixups,
65                                 const MCSubtargetInfo &STI) const;
66
67   /// getAdrLabelOpValue - Return encoding info for 21-bit immediate ADR label
68   /// target.
69   uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
70                               SmallVectorImpl<MCFixup> &Fixups,
71                               const MCSubtargetInfo &STI) const;
72
73   /// getAddSubImmOpValue - Return encoding for the 12-bit immediate value and
74   /// the 2-bit shift field.
75   uint32_t getAddSubImmOpValue(const MCInst &MI, unsigned OpIdx,
76                                SmallVectorImpl<MCFixup> &Fixups,
77                                const MCSubtargetInfo &STI) const;
78
79   /// getCondBranchTargetOpValue - Return the encoded value for a conditional
80   /// branch target.
81   uint32_t getCondBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
82                                       SmallVectorImpl<MCFixup> &Fixups,
83                                       const MCSubtargetInfo &STI) const;
84
85   /// getTestBranchTargetOpValue - Return the encoded value for a test-bit-and-
86   /// branch target.
87   uint32_t getTestBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
88                                       SmallVectorImpl<MCFixup> &Fixups,
89                                       const MCSubtargetInfo &STI) const;
90
91   /// getBranchTargetOpValue - Return the encoded value for an unconditional
92   /// branch target.
93   uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
94                                   SmallVectorImpl<MCFixup> &Fixups,
95                                   const MCSubtargetInfo &STI) const;
96
97   /// getMoveWideImmOpValue - Return the encoded value for the immediate operand
98   /// of a MOVZ or MOVK instruction.
99   uint32_t getMoveWideImmOpValue(const MCInst &MI, unsigned OpIdx,
100                                  SmallVectorImpl<MCFixup> &Fixups,
101                                  const MCSubtargetInfo &STI) const;
102
103   /// getVecShifterOpValue - Return the encoded value for the vector shifter.
104   uint32_t getVecShifterOpValue(const MCInst &MI, unsigned OpIdx,
105                                 SmallVectorImpl<MCFixup> &Fixups,
106                                 const MCSubtargetInfo &STI) const;
107
108   /// getMoveVecShifterOpValue - Return the encoded value for the vector move
109   /// shifter (MSL).
110   uint32_t getMoveVecShifterOpValue(const MCInst &MI, unsigned OpIdx,
111                                     SmallVectorImpl<MCFixup> &Fixups,
112                                     const MCSubtargetInfo &STI) const;
113
114   /// getFixedPointScaleOpValue - Return the encoded value for the
115   // FP-to-fixed-point scale factor.
116   uint32_t getFixedPointScaleOpValue(const MCInst &MI, unsigned OpIdx,
117                                      SmallVectorImpl<MCFixup> &Fixups,
118                                      const MCSubtargetInfo &STI) const;
119
120   uint32_t getVecShiftR64OpValue(const MCInst &MI, unsigned OpIdx,
121                                  SmallVectorImpl<MCFixup> &Fixups,
122                                  const MCSubtargetInfo &STI) const;
123   uint32_t getVecShiftR32OpValue(const MCInst &MI, unsigned OpIdx,
124                                  SmallVectorImpl<MCFixup> &Fixups,
125                                  const MCSubtargetInfo &STI) const;
126   uint32_t getVecShiftR16OpValue(const MCInst &MI, unsigned OpIdx,
127                                  SmallVectorImpl<MCFixup> &Fixups,
128                                  const MCSubtargetInfo &STI) const;
129   uint32_t getVecShiftR8OpValue(const MCInst &MI, unsigned OpIdx,
130                                 SmallVectorImpl<MCFixup> &Fixups,
131                                 const MCSubtargetInfo &STI) const;
132   uint32_t getVecShiftL64OpValue(const MCInst &MI, unsigned OpIdx,
133                                  SmallVectorImpl<MCFixup> &Fixups,
134                                  const MCSubtargetInfo &STI) const;
135   uint32_t getVecShiftL32OpValue(const MCInst &MI, unsigned OpIdx,
136                                  SmallVectorImpl<MCFixup> &Fixups,
137                                  const MCSubtargetInfo &STI) const;
138   uint32_t getVecShiftL16OpValue(const MCInst &MI, unsigned OpIdx,
139                                  SmallVectorImpl<MCFixup> &Fixups,
140                                  const MCSubtargetInfo &STI) const;
141   uint32_t getVecShiftL8OpValue(const MCInst &MI, unsigned OpIdx,
142                                 SmallVectorImpl<MCFixup> &Fixups,
143                                 const MCSubtargetInfo &STI) const;
144
145   /// getSIMDShift64OpValue - Return the encoded value for the
146   // shift-by-immediate AdvSIMD instructions.
147   uint32_t getSIMDShift64OpValue(const MCInst &MI, unsigned OpIdx,
148                                  SmallVectorImpl<MCFixup> &Fixups,
149                                  const MCSubtargetInfo &STI) const;
150
151   uint32_t getSIMDShift64_32OpValue(const MCInst &MI, unsigned OpIdx,
152                                     SmallVectorImpl<MCFixup> &Fixups,
153                                     const MCSubtargetInfo &STI) const;
154
155   uint32_t getSIMDShift32OpValue(const MCInst &MI, unsigned OpIdx,
156                                  SmallVectorImpl<MCFixup> &Fixups,
157                                  const MCSubtargetInfo &STI) const;
158
159   uint32_t getSIMDShift16OpValue(const MCInst &MI, unsigned OpIdx,
160                                  SmallVectorImpl<MCFixup> &Fixups,
161                                  const MCSubtargetInfo &STI) const;
162
163   unsigned fixMOVZ(const MCInst &MI, unsigned EncodedValue,
164                    const MCSubtargetInfo &STI) const;
165
166   void EmitByte(unsigned char C, raw_ostream &OS) const { OS << (char)C; }
167
168   void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
169     // Output the constant in little endian byte order.
170     for (unsigned i = 0; i != Size; ++i) {
171       EmitByte(Val & 255, OS);
172       Val >>= 8;
173     }
174   }
175
176   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
177                          SmallVectorImpl<MCFixup> &Fixups,
178                          const MCSubtargetInfo &STI) const;
179
180   unsigned fixMulHigh(const MCInst &MI, unsigned EncodedValue,
181                       const MCSubtargetInfo &STI) const;
182
183   template<int hasRs, int hasRt2> unsigned
184   fixLoadStoreExclusive(const MCInst &MI, unsigned EncodedValue,
185                         const MCSubtargetInfo &STI) const;
186
187 };
188
189 } // end anonymous namespace
190
191 MCCodeEmitter *llvm::createARM64MCCodeEmitter(const MCInstrInfo &MCII,
192                                               const MCRegisterInfo &MRI,
193                                               const MCSubtargetInfo &STI,
194                                               MCContext &Ctx) {
195   return new ARM64MCCodeEmitter(MCII, STI, Ctx);
196 }
197
198 /// getMachineOpValue - Return binary encoding of operand. If the machine
199 /// operand requires relocation, record the relocation and return zero.
200 unsigned
201 ARM64MCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO,
202                                       SmallVectorImpl<MCFixup> &Fixups,
203                                       const MCSubtargetInfo &STI) const {
204   if (MO.isReg())
205     return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
206   else {
207     assert(MO.isImm() && "did not expect relocated expression");
208     return static_cast<unsigned>(MO.getImm());
209   }
210
211   assert(0 && "Unable to encode MCOperand!");
212   return 0;
213 }
214
215 template <uint32_t FixupKind>
216 uint32_t
217 ARM64MCCodeEmitter::getAMIndexed8OpValue(const MCInst &MI, unsigned OpIdx,
218                                          SmallVectorImpl<MCFixup> &Fixups,
219                                          const MCSubtargetInfo &STI) const {
220   unsigned BaseReg = MI.getOperand(OpIdx).getReg();
221   BaseReg = Ctx.getRegisterInfo()->getEncodingValue(BaseReg);
222
223   const MCOperand &MO = MI.getOperand(OpIdx + 1);
224   uint32_t ImmVal = 0;
225
226   if (MO.isImm())
227     ImmVal = static_cast<uint32_t>(MO.getImm());
228   else {
229     assert(MO.isExpr() && "unable to encode load/store imm operand");
230     MCFixupKind Kind = MCFixupKind(FixupKind);
231     Fixups.push_back(MCFixup::Create(0, MO.getExpr(), Kind, MI.getLoc()));
232     ++MCNumFixups;
233   }
234
235   return BaseReg | (ImmVal << 5);
236 }
237
238 /// getAdrLabelOpValue - Return encoding info for 21-bit immediate ADR label
239 /// target.
240 uint32_t
241 ARM64MCCodeEmitter::getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
242                                        SmallVectorImpl<MCFixup> &Fixups,
243                                        const MCSubtargetInfo &STI) const {
244   const MCOperand &MO = MI.getOperand(OpIdx);
245
246   // If the destination is an immediate, we have nothing to do.
247   if (MO.isImm())
248     return MO.getImm();
249   assert(MO.isExpr() && "Unexpected ADR target type!");
250   const MCExpr *Expr = MO.getExpr();
251
252   MCFixupKind Kind = MI.getOpcode() == ARM64::ADR
253                          ? MCFixupKind(ARM64::fixup_arm64_pcrel_adr_imm21)
254                          : MCFixupKind(ARM64::fixup_arm64_pcrel_adrp_imm21);
255   Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
256
257   MCNumFixups += 1;
258
259   // All of the information is in the fixup.
260   return 0;
261 }
262
263 /// getAddSubImmOpValue - Return encoding for the 12-bit immediate value and
264 /// the 2-bit shift field.  The shift field is stored in bits 13-14 of the
265 /// return value.
266 uint32_t
267 ARM64MCCodeEmitter::getAddSubImmOpValue(const MCInst &MI, unsigned OpIdx,
268                                         SmallVectorImpl<MCFixup> &Fixups,
269                                         const MCSubtargetInfo &STI) const {
270   // Suboperands are [imm, shifter].
271   const MCOperand &MO = MI.getOperand(OpIdx);
272   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
273   assert(ARM64_AM::getShiftType(MO1.getImm()) == ARM64_AM::LSL &&
274          "unexpected shift type for add/sub immediate");
275   unsigned ShiftVal = ARM64_AM::getShiftValue(MO1.getImm());
276   assert((ShiftVal == 0 || ShiftVal == 12) &&
277          "unexpected shift value for add/sub immediate");
278   if (MO.isImm())
279     return MO.getImm() | (ShiftVal == 0 ? 0 : (1 << 12));
280   assert(MO.isExpr() && "Unable to encode MCOperand!");
281   const MCExpr *Expr = MO.getExpr();
282   assert(ShiftVal == 0 && "shift not allowed on add/sub immediate with fixup");
283
284   // Encode the 12 bits of the fixup.
285   MCFixupKind Kind = MCFixupKind(ARM64::fixup_arm64_add_imm12);
286   Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
287
288   ++MCNumFixups;
289
290   return 0;
291 }
292
293 /// getCondBranchTargetOpValue - Return the encoded value for a conditional
294 /// branch target.
295 uint32_t ARM64MCCodeEmitter::getCondBranchTargetOpValue(
296     const MCInst &MI, unsigned OpIdx, SmallVectorImpl<MCFixup> &Fixups,
297     const MCSubtargetInfo &STI) const {
298   const MCOperand &MO = MI.getOperand(OpIdx);
299
300   // If the destination is an immediate, we have nothing to do.
301   if (MO.isImm())
302     return MO.getImm();
303   assert(MO.isExpr() && "Unexpected target type!");
304
305   MCFixupKind Kind = MCFixupKind(ARM64::fixup_arm64_pcrel_imm19);
306   Fixups.push_back(MCFixup::Create(0, MO.getExpr(), Kind, MI.getLoc()));
307
308   ++MCNumFixups;
309
310   // All of the information is in the fixup.
311   return 0;
312 }
313
314 uint32_t
315 ARM64MCCodeEmitter::getMoveWideImmOpValue(const MCInst &MI, unsigned OpIdx,
316                                           SmallVectorImpl<MCFixup> &Fixups,
317                                           const MCSubtargetInfo &STI) const {
318   const MCOperand &MO = MI.getOperand(OpIdx);
319
320   if (MO.isImm())
321     return MO.getImm();
322   assert(MO.isExpr() && "Unexpected movz/movk immediate");
323
324   Fixups.push_back(MCFixup::Create(
325       0, MO.getExpr(), MCFixupKind(ARM64::fixup_arm64_movw), MI.getLoc()));
326
327   ++MCNumFixups;
328
329   return 0;
330 }
331
332 /// getTestBranchTargetOpValue - Return the encoded value for a test-bit-and-
333 /// branch target.
334 uint32_t ARM64MCCodeEmitter::getTestBranchTargetOpValue(
335     const MCInst &MI, unsigned OpIdx, SmallVectorImpl<MCFixup> &Fixups,
336     const MCSubtargetInfo &STI) const {
337   const MCOperand &MO = MI.getOperand(OpIdx);
338
339   // If the destination is an immediate, we have nothing to do.
340   if (MO.isImm())
341     return MO.getImm();
342   assert(MO.isExpr() && "Unexpected ADR target type!");
343
344   MCFixupKind Kind = MCFixupKind(ARM64::fixup_arm64_pcrel_branch14);
345   Fixups.push_back(MCFixup::Create(0, MO.getExpr(), Kind, MI.getLoc()));
346
347   ++MCNumFixups;
348
349   // All of the information is in the fixup.
350   return 0;
351 }
352
353 /// getBranchTargetOpValue - Return the encoded value for an unconditional
354 /// branch target.
355 uint32_t
356 ARM64MCCodeEmitter::getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
357                                            SmallVectorImpl<MCFixup> &Fixups,
358                                            const MCSubtargetInfo &STI) const {
359   const MCOperand &MO = MI.getOperand(OpIdx);
360
361   // If the destination is an immediate, we have nothing to do.
362   if (MO.isImm())
363     return MO.getImm();
364   assert(MO.isExpr() && "Unexpected ADR target type!");
365
366   MCFixupKind Kind = MI.getOpcode() == ARM64::BL
367                          ? MCFixupKind(ARM64::fixup_arm64_pcrel_call26)
368                          : MCFixupKind(ARM64::fixup_arm64_pcrel_branch26);
369   Fixups.push_back(MCFixup::Create(0, MO.getExpr(), Kind, MI.getLoc()));
370
371   ++MCNumFixups;
372
373   // All of the information is in the fixup.
374   return 0;
375 }
376
377 /// getVecShifterOpValue - Return the encoded value for the vector shifter:
378 ///
379 ///   00 -> 0
380 ///   01 -> 8
381 ///   10 -> 16
382 ///   11 -> 24
383 uint32_t
384 ARM64MCCodeEmitter::getVecShifterOpValue(const MCInst &MI, unsigned OpIdx,
385                                          SmallVectorImpl<MCFixup> &Fixups,
386                                          const MCSubtargetInfo &STI) const {
387   const MCOperand &MO = MI.getOperand(OpIdx);
388   assert(MO.isImm() && "Expected an immediate value for the shift amount!");
389
390   switch (MO.getImm()) {
391   default:
392     break;
393   case 0:
394     return 0;
395   case 8:
396     return 1;
397   case 16:
398     return 2;
399   case 24:
400     return 3;
401   }
402
403   assert(false && "Invalid value for vector shift amount!");
404   return 0;
405 }
406
407 uint32_t
408 ARM64MCCodeEmitter::getSIMDShift64OpValue(const MCInst &MI, unsigned OpIdx,
409                                           SmallVectorImpl<MCFixup> &Fixups,
410                                           const MCSubtargetInfo &STI) const {
411   const MCOperand &MO = MI.getOperand(OpIdx);
412   assert(MO.isImm() && "Expected an immediate value for the shift amount!");
413   return 64 - (MO.getImm());
414 }
415
416 uint32_t
417 ARM64MCCodeEmitter::getSIMDShift64_32OpValue(const MCInst &MI, unsigned OpIdx,
418                                              SmallVectorImpl<MCFixup> &Fixups,
419                                              const MCSubtargetInfo &STI) const {
420   const MCOperand &MO = MI.getOperand(OpIdx);
421   assert(MO.isImm() && "Expected an immediate value for the shift amount!");
422   return 64 - (MO.getImm() | 32);
423 }
424
425 uint32_t
426 ARM64MCCodeEmitter::getSIMDShift32OpValue(const MCInst &MI, unsigned OpIdx,
427                                           SmallVectorImpl<MCFixup> &Fixups,
428                                           const MCSubtargetInfo &STI) const {
429   const MCOperand &MO = MI.getOperand(OpIdx);
430   assert(MO.isImm() && "Expected an immediate value for the shift amount!");
431   return 32 - (MO.getImm() | 16);
432 }
433
434 uint32_t
435 ARM64MCCodeEmitter::getSIMDShift16OpValue(const MCInst &MI, unsigned OpIdx,
436                                           SmallVectorImpl<MCFixup> &Fixups,
437                                           const MCSubtargetInfo &STI) const {
438   const MCOperand &MO = MI.getOperand(OpIdx);
439   assert(MO.isImm() && "Expected an immediate value for the shift amount!");
440   return 16 - (MO.getImm() | 8);
441 }
442
443 /// getFixedPointScaleOpValue - Return the encoded value for the
444 // FP-to-fixed-point scale factor.
445 uint32_t ARM64MCCodeEmitter::getFixedPointScaleOpValue(
446     const MCInst &MI, unsigned OpIdx, SmallVectorImpl<MCFixup> &Fixups,
447     const MCSubtargetInfo &STI) const {
448   const MCOperand &MO = MI.getOperand(OpIdx);
449   assert(MO.isImm() && "Expected an immediate value for the scale amount!");
450   return 64 - MO.getImm();
451 }
452
453 uint32_t
454 ARM64MCCodeEmitter::getVecShiftR64OpValue(const MCInst &MI, unsigned OpIdx,
455                                           SmallVectorImpl<MCFixup> &Fixups,
456                                           const MCSubtargetInfo &STI) const {
457   const MCOperand &MO = MI.getOperand(OpIdx);
458   assert(MO.isImm() && "Expected an immediate value for the scale amount!");
459   return 64 - MO.getImm();
460 }
461
462 uint32_t
463 ARM64MCCodeEmitter::getVecShiftR32OpValue(const MCInst &MI, unsigned OpIdx,
464                                           SmallVectorImpl<MCFixup> &Fixups,
465                                           const MCSubtargetInfo &STI) const {
466   const MCOperand &MO = MI.getOperand(OpIdx);
467   assert(MO.isImm() && "Expected an immediate value for the scale amount!");
468   return 32 - MO.getImm();
469 }
470
471 uint32_t
472 ARM64MCCodeEmitter::getVecShiftR16OpValue(const MCInst &MI, unsigned OpIdx,
473                                           SmallVectorImpl<MCFixup> &Fixups,
474                                           const MCSubtargetInfo &STI) const {
475   const MCOperand &MO = MI.getOperand(OpIdx);
476   assert(MO.isImm() && "Expected an immediate value for the scale amount!");
477   return 16 - MO.getImm();
478 }
479
480 uint32_t
481 ARM64MCCodeEmitter::getVecShiftR8OpValue(const MCInst &MI, unsigned OpIdx,
482                                          SmallVectorImpl<MCFixup> &Fixups,
483                                          const MCSubtargetInfo &STI) const {
484   const MCOperand &MO = MI.getOperand(OpIdx);
485   assert(MO.isImm() && "Expected an immediate value for the scale amount!");
486   return 8 - MO.getImm();
487 }
488
489 uint32_t
490 ARM64MCCodeEmitter::getVecShiftL64OpValue(const MCInst &MI, unsigned OpIdx,
491                                           SmallVectorImpl<MCFixup> &Fixups,
492                                           const MCSubtargetInfo &STI) const {
493   const MCOperand &MO = MI.getOperand(OpIdx);
494   assert(MO.isImm() && "Expected an immediate value for the scale amount!");
495   return MO.getImm() - 64;
496 }
497
498 uint32_t
499 ARM64MCCodeEmitter::getVecShiftL32OpValue(const MCInst &MI, unsigned OpIdx,
500                                           SmallVectorImpl<MCFixup> &Fixups,
501                                           const MCSubtargetInfo &STI) const {
502   const MCOperand &MO = MI.getOperand(OpIdx);
503   assert(MO.isImm() && "Expected an immediate value for the scale amount!");
504   return MO.getImm() - 32;
505 }
506
507 uint32_t
508 ARM64MCCodeEmitter::getVecShiftL16OpValue(const MCInst &MI, unsigned OpIdx,
509                                           SmallVectorImpl<MCFixup> &Fixups,
510                                           const MCSubtargetInfo &STI) const {
511   const MCOperand &MO = MI.getOperand(OpIdx);
512   assert(MO.isImm() && "Expected an immediate value for the scale amount!");
513   return MO.getImm() - 16;
514 }
515
516 uint32_t
517 ARM64MCCodeEmitter::getVecShiftL8OpValue(const MCInst &MI, unsigned OpIdx,
518                                          SmallVectorImpl<MCFixup> &Fixups,
519                                          const MCSubtargetInfo &STI) const {
520   const MCOperand &MO = MI.getOperand(OpIdx);
521   assert(MO.isImm() && "Expected an immediate value for the scale amount!");
522   return MO.getImm() - 8;
523 }
524
525 /// getMoveVecShifterOpValue - Return the encoded value for the vector move
526 /// shifter (MSL).
527 uint32_t
528 ARM64MCCodeEmitter::getMoveVecShifterOpValue(const MCInst &MI, unsigned OpIdx,
529                                              SmallVectorImpl<MCFixup> &Fixups,
530                                              const MCSubtargetInfo &STI) const {
531   const MCOperand &MO = MI.getOperand(OpIdx);
532   assert(MO.isImm() &&
533          "Expected an immediate value for the move shift amount!");
534   unsigned ShiftVal = ARM64_AM::getShiftValue(MO.getImm());
535   assert((ShiftVal == 8 || ShiftVal == 16) && "Invalid shift amount!");
536   return ShiftVal == 8 ? 0 : 1;
537 }
538
539 unsigned ARM64MCCodeEmitter::fixMOVZ(const MCInst &MI, unsigned EncodedValue,
540                                      const MCSubtargetInfo &STI) const {
541   // If one of the signed fixup kinds is applied to a MOVZ instruction, the
542   // eventual result could be either a MOVZ or a MOVN. It's the MCCodeEmitter's
543   // job to ensure that any bits possibly affected by this are 0. This means we
544   // must zero out bit 30 (essentially emitting a MOVN).
545   MCOperand UImm16MO = MI.getOperand(1);
546
547   // Nothing to do if there's no fixup.
548   if (UImm16MO.isImm())
549     return EncodedValue;
550
551   return EncodedValue & ~(1u << 30);
552 }
553
554 void ARM64MCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
555                                            SmallVectorImpl<MCFixup> &Fixups,
556                                            const MCSubtargetInfo &STI) const {
557   if (MI.getOpcode() == ARM64::TLSDESCCALL) {
558     // This is a directive which applies an R_AARCH64_TLSDESC_CALL to the
559     // following (BLR) instruction. It doesn't emit any code itself so it
560     // doesn't go through the normal TableGenerated channels.
561     MCFixupKind Fixup = MCFixupKind(ARM64::fixup_arm64_tlsdesc_call);
562     Fixups.push_back(MCFixup::Create(0, MI.getOperand(0).getExpr(), Fixup));
563     return;
564   }
565
566   uint64_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
567   EmitConstant(Binary, 4, OS);
568   ++MCNumEmitted; // Keep track of the # of mi's emitted.
569 }
570
571 unsigned
572 ARM64MCCodeEmitter::fixMulHigh(const MCInst &MI,
573                                unsigned EncodedValue,
574                                const MCSubtargetInfo &STI) const {
575   // The Ra field of SMULH and UMULH is unused: it should be assembled as 31
576   // (i.e. all bits 1) but is ignored by the processor.
577   EncodedValue |= 0x1f << 10;
578   return EncodedValue;
579 }
580
581 template<int hasRs, int hasRt2> unsigned
582 ARM64MCCodeEmitter::fixLoadStoreExclusive(const MCInst &MI,
583                                           unsigned EncodedValue,
584                                           const MCSubtargetInfo &STI) const {
585   if (!hasRs) EncodedValue |= 0x001F0000;
586   if (!hasRt2) EncodedValue |= 0x00007C00;
587
588   return EncodedValue;
589 }
590
591 #include "ARM64GenMCCodeEmitter.inc"