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