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