LLVM 24.0.0git
X86MCCodeEmitter.cpp
Go to the documentation of this file.
1//===-- X86MCCodeEmitter.cpp - Convert X86 code to machine code -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the X86MCCodeEmitter class.
10//
11//===----------------------------------------------------------------------===//
12
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCFixup.h"
23#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCInstrDesc.h"
25#include "llvm/MC/MCInstrInfo.h"
28#include "llvm/MC/MCSymbol.h"
31#include <cassert>
32#include <cstdint>
33
34using namespace llvm;
35
36#define DEBUG_TYPE "mccodeemitter"
37
38namespace {
39
40enum PrefixKind { None, REX, REX2, XOP, VEX2, VEX3, EVEX };
41
42static void emitByte(uint8_t C, SmallVectorImpl<char> &CB) { CB.push_back(C); }
43
44class X86OpcodePrefixHelper {
45 // REX (1 byte)
46 // +-----+ +------+
47 // | 40H | | WRXB |
48 // +-----+ +------+
49
50 // REX2 (2 bytes)
51 // +-----+ +-------------------+
52 // | D5H | | M | R'X'B' | WRXB |
53 // +-----+ +-------------------+
54
55 // XOP (3-byte)
56 // +-----+ +--------------+ +-------------------+
57 // | 8Fh | | RXB | m-mmmm | | W | vvvv | L | pp |
58 // +-----+ +--------------+ +-------------------+
59
60 // VEX2 (2 bytes)
61 // +-----+ +-------------------+
62 // | C5h | | R | vvvv | L | pp |
63 // +-----+ +-------------------+
64
65 // VEX3 (3 bytes)
66 // +-----+ +--------------+ +-------------------+
67 // | C4h | | RXB | m-mmmm | | W | vvvv | L | pp |
68 // +-----+ +--------------+ +-------------------+
69
70 // VEX_R: opcode externsion equivalent to REX.R in
71 // 1's complement (inverted) form
72 //
73 // 1: Same as REX_R=0 (must be 1 in 32-bit mode)
74 // 0: Same as REX_R=1 (64 bit mode only)
75
76 // VEX_X: equivalent to REX.X, only used when a
77 // register is used for index in SIB Byte.
78 //
79 // 1: Same as REX.X=0 (must be 1 in 32-bit mode)
80 // 0: Same as REX.X=1 (64-bit mode only)
81
82 // VEX_B:
83 // 1: Same as REX_B=0 (ignored in 32-bit mode)
84 // 0: Same as REX_B=1 (64 bit mode only)
85
86 // VEX_W: opcode specific (use like REX.W, or used for
87 // opcode extension, or ignored, depending on the opcode byte)
88
89 // VEX_5M (VEX m-mmmmm field):
90 //
91 // 0b00000: Reserved for future use
92 // 0b00001: implied 0F leading opcode
93 // 0b00010: implied 0F 38 leading opcode bytes
94 // 0b00011: implied 0F 3A leading opcode bytes
95 // 0b00100: Reserved for future use
96 // 0b00101: VEX MAP5
97 // 0b00110: VEX MAP6
98 // 0b00111: VEX MAP7
99 // 0b00111-0b11111: Reserved for future use
100 // 0b01000: XOP map select - 08h instructions with imm byte
101 // 0b01001: XOP map select - 09h instructions with no imm byte
102 // 0b01010: XOP map select - 0Ah instructions with imm dword
103
104 // VEX_4V (VEX vvvv field): a register specifier
105 // (in 1's complement form) or 1111 if unused.
106
107 // VEX_PP: opcode extension providing equivalent
108 // functionality of a SIMD prefix
109 // 0b00: None
110 // 0b01: 66
111 // 0b10: F3
112 // 0b11: F2
113
114 // EVEX (4 bytes)
115 // +-----+ +---------------+ +-------------------+ +------------------------+
116 // | 62h | | RXBR' | B'mmm | | W | vvvv | U | pp | | z | L'L | b | v' | aaa |
117 // +-----+ +---------------+ +-------------------+ +------------------------+
118
119 // EVEX_L2/VEX_L (Vector Length):
120 // L2 L
121 // 0 0: scalar or 128-bit vector
122 // 0 1: 256-bit vector
123 // 1 0: 512-bit vector
124
125 // 32-Register Support in 64-bit Mode Using EVEX with Embedded REX/REX2 Bits:
126 //
127 // +----------+---------+--------+-----------+---------+--------------+
128 // | | 4 | 3 | [2:0] | Type | Common Usage |
129 // +----------+---------+--------+-----------+---------+--------------+
130 // | REG | EVEX_R' | EVEX_R | modrm.reg | GPR, VR | Dest or Src |
131 // | VVVV | EVEX_v' | EVEX.vvvv | GPR, VR | Dest or Src |
132 // | RM (VR) | EVEX_X | EVEX_B | modrm.r/m | VR | Dest or Src |
133 // | RM (GPR) | EVEX_B' | EVEX_B | modrm.r/m | GPR | Dest or Src |
134 // | BASE | EVEX_B' | EVEX_B | modrm.r/m | GPR | MA |
135 // | INDEX | EVEX_U | EVEX_X | sib.index | GPR | MA |
136 // | VIDX | EVEX_v' | EVEX_X | sib.index | VR | VSIB MA |
137 // +----------+---------+--------+-----------+---------+--------------+
138 //
139 // * GPR - General-purpose register
140 // * VR - Vector register
141 // * VIDX - Vector index
142 // * VSIB - Vector SIB
143 // * MA - Memory addressing
144
145private:
146 unsigned W : 1;
147 unsigned R : 1;
148 unsigned X : 1;
149 unsigned B : 1;
150 unsigned M : 1;
151 unsigned R2 : 1;
152 unsigned X2 : 1;
153 unsigned B2 : 1;
154 unsigned VEX_4V : 4;
155 unsigned VEX_L : 1;
156 unsigned VEX_PP : 2;
157 unsigned VEX_5M : 5;
158 unsigned EVEX_z : 1;
159 unsigned EVEX_L2 : 1;
160 unsigned EVEX_b : 1;
161 unsigned EVEX_V2 : 1;
162 unsigned EVEX_aaa : 3;
163 PrefixKind Kind = None;
164 const MCRegisterInfo &MRI;
165
166 unsigned getRegEncoding(const MCInst &MI, unsigned OpNum) const {
167 return MRI.getEncodingValue(MI.getOperand(OpNum).getReg());
168 }
169
170 void setR(unsigned Encoding) { R = Encoding >> 3 & 1; }
171 void setR2(unsigned Encoding) {
172 R2 = Encoding >> 4 & 1;
173 assert((!R2 || (Kind <= REX2 || Kind == EVEX)) && "invalid setting");
174 }
175 void setX(unsigned Encoding) { X = Encoding >> 3 & 1; }
176 void setX2(unsigned Encoding) {
177 assert((Kind <= REX2 || Kind == EVEX) && "invalid setting");
178 X2 = Encoding >> 4 & 1;
179 }
180 void setB(unsigned Encoding) { B = Encoding >> 3 & 1; }
181 void setB2(unsigned Encoding) {
182 assert((Kind <= REX2 || Kind == EVEX) && "invalid setting");
183 B2 = Encoding >> 4 & 1;
184 }
185 void set4V(unsigned Encoding) { VEX_4V = Encoding & 0xf; }
186 void setV2(unsigned Encoding) { EVEX_V2 = Encoding >> 4 & 1; }
187
188public:
189 void setW(bool V) { W = V; }
190 void setR(const MCInst &MI, unsigned OpNum) {
191 setR(getRegEncoding(MI, OpNum));
192 }
193 void setX(const MCInst &MI, unsigned OpNum, unsigned Shift = 3) {
194 MCRegister Reg = MI.getOperand(OpNum).getReg();
195 // X is used to extend vector register only when shift is not 3.
196 if (Shift != 3 && X86II::isApxExtendedReg(Reg))
197 return;
198 unsigned Encoding = MRI.getEncodingValue(Reg);
199 X = Encoding >> Shift & 1;
200 }
201 void setB(const MCInst &MI, unsigned OpNum) {
202 B = getRegEncoding(MI, OpNum) >> 3 & 1;
203 }
204 void set4V(const MCInst &MI, unsigned OpNum, bool IsImm = false) {
205 // OF, SF, ZF and CF reuse VEX_4V bits but are not reversed
206 if (IsImm)
207 set4V(~(MI.getOperand(OpNum).getImm()));
208 else
209 set4V(getRegEncoding(MI, OpNum));
210 }
211 void setL(bool V) { VEX_L = V; }
212 void setPP(unsigned V) { VEX_PP = V; }
213 void set5M(unsigned V) { VEX_5M = V; }
214 void setR2(const MCInst &MI, unsigned OpNum) {
215 setR2(getRegEncoding(MI, OpNum));
216 }
217 void setRR2(const MCInst &MI, unsigned OpNum) {
218 unsigned Encoding = getRegEncoding(MI, OpNum);
219 setR(Encoding);
220 setR2(Encoding);
221 }
222 void setM(bool V) { M = V; }
223 void setXX2(const MCInst &MI, unsigned OpNum) {
224 MCRegister Reg = MI.getOperand(OpNum).getReg();
225 unsigned Encoding = MRI.getEncodingValue(Reg);
226 setX(Encoding);
227 // Index can be a vector register while X2 is used to extend GPR only.
228 if (Kind <= REX2 || X86II::isApxExtendedReg(Reg))
229 setX2(Encoding);
230 }
231 void setBB2(const MCInst &MI, unsigned OpNum) {
232 MCRegister Reg = MI.getOperand(OpNum).getReg();
233 unsigned Encoding = MRI.getEncodingValue(Reg);
234 setB(Encoding);
235 // Base can be a vector register while B2 is used to extend GPR only
236 if (Kind <= REX2 || X86II::isApxExtendedReg(Reg))
237 setB2(Encoding);
238 }
239 void setZ(bool V) { EVEX_z = V; }
240 void setL2(bool V) { EVEX_L2 = V; }
241 void setEVEX_b(bool V) { EVEX_b = V; }
242 void setEVEX_U(bool V) { X2 = V; }
243 void setV2(const MCInst &MI, unsigned OpNum, bool HasVEX_4V) {
244 // Only needed with VSIB which don't use VVVV.
245 if (HasVEX_4V)
246 return;
247 MCRegister Reg = MI.getOperand(OpNum).getReg();
249 return;
250 setV2(MRI.getEncodingValue(Reg));
251 }
252 void set4VV2(const MCInst &MI, unsigned OpNum) {
253 unsigned Encoding = getRegEncoding(MI, OpNum);
254 set4V(Encoding);
255 setV2(Encoding);
256 }
257 void setAAA(const MCInst &MI, unsigned OpNum) {
258 EVEX_aaa = getRegEncoding(MI, OpNum);
259 }
260 void setNF(bool V) { EVEX_aaa |= V << 2; }
261 void setSC(const MCInst &MI, unsigned OpNum) {
262 unsigned Encoding = MI.getOperand(OpNum).getImm();
263 EVEX_V2 = ~(Encoding >> 3) & 0x1;
264 EVEX_aaa = Encoding & 0x7;
265 }
266
267 X86OpcodePrefixHelper(const MCRegisterInfo &MRI)
268 : W(0), R(0), X(0), B(0), M(0), R2(0), X2(0), B2(0), VEX_4V(0), VEX_L(0),
269 VEX_PP(0), VEX_5M(0), EVEX_z(0), EVEX_L2(0), EVEX_b(0), EVEX_V2(0),
270 EVEX_aaa(0), MRI(MRI) {}
271
272 void setLowerBound(PrefixKind K) { Kind = K; }
273
274 PrefixKind determineOptimalKind() {
275 switch (Kind) {
276 case None:
277 // Not M bit here by intention b/c
278 // 1. No guarantee that REX2 is supported by arch w/o explict EGPR
279 // 2. REX2 is longer than 0FH
280 Kind = (R2 | X2 | B2) ? REX2 : (W | R | X | B) ? REX : None;
281 break;
282 case REX:
283 Kind = (R2 | X2 | B2) ? REX2 : REX;
284 break;
285 case REX2:
286 case XOP:
287 case VEX3:
288 case EVEX:
289 break;
290 case VEX2:
291 Kind = (W | X | B | (VEX_5M != 1)) ? VEX3 : VEX2;
292 break;
293 }
294 return Kind;
295 }
296
297 void emit(SmallVectorImpl<char> &CB) const {
298 uint8_t FirstPayload =
299 ((~R) & 0x1) << 7 | ((~X) & 0x1) << 6 | ((~B) & 0x1) << 5;
300 uint8_t LastPayload = ((~VEX_4V) & 0xf) << 3 | VEX_L << 2 | VEX_PP;
301 switch (Kind) {
302 case None:
303 return;
304 case REX:
305 emitByte(0x40 | W << 3 | R << 2 | X << 1 | B, CB);
306 return;
307 case REX2:
308 emitByte(0xD5, CB);
309 emitByte(M << 7 | R2 << 6 | X2 << 5 | B2 << 4 | W << 3 | R << 2 | X << 1 |
310 B,
311 CB);
312 return;
313 case VEX2:
314 emitByte(0xC5, CB);
315 emitByte(((~R) & 1) << 7 | LastPayload, CB);
316 return;
317 case VEX3:
318 case XOP:
319 emitByte(Kind == VEX3 ? 0xC4 : 0x8F, CB);
320 emitByte(FirstPayload | VEX_5M, CB);
321 emitByte(W << 7 | LastPayload, CB);
322 return;
323 case EVEX:
324 assert(VEX_5M && !(VEX_5M & 0x8) && "invalid mmm fields for EVEX!");
325 emitByte(0x62, CB);
326 emitByte(FirstPayload | ((~R2) & 0x1) << 4 | B2 << 3 | VEX_5M, CB);
327 emitByte(W << 7 | ((~VEX_4V) & 0xf) << 3 | ((~X2) & 0x1) << 2 | VEX_PP,
328 CB);
329 emitByte(EVEX_z << 7 | EVEX_L2 << 6 | VEX_L << 5 | EVEX_b << 4 |
330 ((~EVEX_V2) & 0x1) << 3 | EVEX_aaa,
331 CB);
332 return;
333 }
334 }
335};
336
337class X86MCCodeEmitter : public MCCodeEmitter {
338 const MCInstrInfo &MCII;
339 MCContext &Ctx;
340
341public:
342 X86MCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
343 : MCII(mcii), Ctx(ctx) {}
344 X86MCCodeEmitter(const X86MCCodeEmitter &) = delete;
345 X86MCCodeEmitter &operator=(const X86MCCodeEmitter &) = delete;
346 ~X86MCCodeEmitter() override = default;
347
348 void emitPrefix(const MCInst &MI, SmallVectorImpl<char> &CB,
349 const MCSubtargetInfo &STI) const;
350
351 void encodeInstruction(const MCInst &MI, SmallVectorImpl<char> &CB,
353 const MCSubtargetInfo &STI) const override;
354
355private:
356 unsigned getX86RegNum(const MCOperand &MO) const;
357
358 unsigned getX86RegEncoding(const MCInst &MI, unsigned OpNum) const;
359
360 void emitImmediate(const MCOperand &Disp, SMLoc Loc, unsigned FixupKind,
361 bool IsPCRel, uint64_t StartByte,
363 SmallVectorImpl<MCFixup> &Fixups, int ImmOffset = 0) const;
364
365 void emitRegModRMByte(const MCOperand &ModRMReg, unsigned RegOpcodeFld,
366 SmallVectorImpl<char> &CB) const;
367
368 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base,
369 SmallVectorImpl<char> &CB) const;
370
371 void emitMemModRMByte(const MCInst &MI, unsigned Op, unsigned RegOpcodeField,
372 uint64_t TSFlags, PrefixKind Kind, uint64_t StartByte,
375 const MCSubtargetInfo &STI,
376 bool ForceSIB = false) const;
377
378 PrefixKind emitPrefixImpl(const MCInst &MI, const MCSubtargetInfo &STI,
379 SmallVectorImpl<char> &CB) const;
380
381 PrefixKind emitVEXOpcodePrefix(int MemOperand, const MCInst &MI,
382 const MCSubtargetInfo &STI,
383 SmallVectorImpl<char> &CB) const;
384
385 void emitSegmentOverridePrefix(unsigned SegOperand, const MCInst &MI,
386 SmallVectorImpl<char> &CB) const;
387
388 PrefixKind emitOpcodePrefix(int MemOperand, const MCInst &MI,
389 const MCSubtargetInfo &STI,
390 SmallVectorImpl<char> &CB) const;
391
392 PrefixKind emitREXPrefix(int MemOperand, const MCInst &MI,
393 const MCSubtargetInfo &STI,
394 SmallVectorImpl<char> &CB) const;
395};
396
397} // end anonymous namespace
398
399static uint8_t modRMByte(unsigned Mod, unsigned RegOpcode, unsigned RM) {
400 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
401 return RM | (RegOpcode << 3) | (Mod << 6);
402}
403
404static void emitConstant(uint64_t Val, unsigned Size,
406 // Output the constant in little endian byte order.
407 for (unsigned i = 0; i != Size; ++i) {
408 emitByte(Val & 255, CB);
409 Val >>= 8;
410 }
411}
412
413/// Determine if this immediate can fit in a disp8 or a compressed disp8 for
414/// EVEX instructions. \p will be set to the value to pass to the ImmOffset
415/// parameter of emitImmediate.
416static bool isDispOrCDisp8(uint64_t TSFlags, int Value, int &ImmOffset) {
417 bool HasEVEX = (TSFlags & X86II::EncodingMask) == X86II::EVEX;
418
419 unsigned CD8_Scale =
421 CD8_Scale = CD8_Scale ? 1U << (CD8_Scale - 1) : 0U;
422 if (!HasEVEX || !CD8_Scale)
423 return isInt<8>(Value);
424
425 assert(isPowerOf2_32(CD8_Scale) && "Unexpected CD8 scale!");
426 if (Value & (CD8_Scale - 1)) // Unaligned offset
427 return false;
428
429 int CDisp8 = Value / static_cast<int>(CD8_Scale);
430 if (!isInt<8>(CDisp8))
431 return false;
432
433 // ImmOffset will be added to Value in emitImmediate leaving just CDisp8.
434 ImmOffset = CDisp8 - Value;
435 return true;
436}
437
438/// \returns the appropriate fixup kind to use for an immediate in an
439/// instruction with the specified TSFlags.
441 unsigned Size = X86II::getSizeOfImm(TSFlags);
442 if (X86II::isImmSigned(TSFlags)) {
443 switch (Size) {
444 default:
445 llvm_unreachable("Unsupported signed fixup size!");
446 case 4:
448 }
449 }
450 switch (Size) {
451 default:
452 llvm_unreachable("Invalid generic fixup size!");
453 case 1:
454 return FK_Data_1;
455 case 2:
456 return FK_Data_2;
457 case 4:
458 return FK_Data_4;
459 case 8:
460 return FK_Data_8;
461 }
462}
463
465
466/// Check if this expression starts with _GLOBAL_OFFSET_TABLE_ and if it is
467/// of the form _GLOBAL_OFFSET_TABLE_-symbol. This is needed to support PIC on
468/// ELF i386 as _GLOBAL_OFFSET_TABLE_ is magical. We check only simple case that
469/// are know to be used: _GLOBAL_OFFSET_TABLE_ by itself or at the start of a
470/// binary expression.
471///
472/// TODO: Move this to X86AsmBackend.cpp at relocation decision phase so that we
473/// don't have to mess with MCExpr.
476 const MCExpr *RHS = nullptr;
477 if (Expr->getKind() == MCExpr::Binary) {
478 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Expr);
479 Expr = BE->getLHS();
480 RHS = BE->getRHS();
481 }
482
483 if (Expr->getKind() != MCExpr::SymbolRef)
484 return GOT_None;
485
486 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
487 const MCSymbol &S = Ref->getSymbol();
488 if (S.getName() != "_GLOBAL_OFFSET_TABLE_")
489 return GOT_None;
490 if (RHS && RHS->getKind() == MCExpr::SymbolRef)
491 return GOT_SymDiff;
492 return GOT_Normal;
493}
494
495static bool hasSecRelSymbolRef(const MCExpr *Expr) {
496 if (Expr->getKind() == MCExpr::SymbolRef) {
497 auto *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
498 return Ref->getSpecifier() == X86::S_COFF_SECREL;
499 }
500 return false;
501}
502
503static bool isPCRel32Branch(const MCInst &MI, const MCInstrInfo &MCII) {
504 unsigned Opcode = MI.getOpcode();
505 const MCInstrDesc &Desc = MCII.get(Opcode);
506 if ((Opcode != X86::CALL64pcrel32 && Opcode != X86::JMP_4 &&
507 Opcode != X86::JCC_4) ||
508 !(getImmFixupKind(Desc.TSFlags) == FK_Data_4 &&
509 X86II::isImmPCRel(Desc.TSFlags)))
510 return false;
511
512 unsigned CurOp = X86II::getOperandBias(Desc);
513 const MCOperand &Op = MI.getOperand(CurOp);
514 if (!Op.isExpr())
515 return false;
516
517 auto *Ref = dyn_cast<MCSymbolRefExpr>(Op.getExpr());
518 return Ref && Ref->getSpecifier() == X86::S_None;
519}
520
521unsigned X86MCCodeEmitter::getX86RegNum(const MCOperand &MO) const {
522 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg()) & 0x7;
523}
524
525unsigned X86MCCodeEmitter::getX86RegEncoding(const MCInst &MI,
526 unsigned OpNum) const {
527 return Ctx.getRegisterInfo()->getEncodingValue(MI.getOperand(OpNum).getReg());
528}
529
530void X86MCCodeEmitter::emitImmediate(const MCOperand &DispOp, SMLoc Loc,
531 unsigned FixupKind, bool PCRel,
532 uint64_t StartByte,
533 SmallVectorImpl<char> &CB,
534 SmallVectorImpl<MCFixup> &Fixups,
535 int ImmOffset) const {
536 unsigned Size = 4;
537 switch (FixupKind) {
538 case FK_Data_1:
539 Size = 1;
540 break;
541 case FK_Data_2:
542 Size = 2;
543 break;
544 case FK_Data_8:
545 Size = 8;
546 break;
547 }
548 const MCExpr *Expr = nullptr;
549 if (DispOp.isImm()) {
550 // If this is a simple integer displacement that doesn't require a
551 // relocation, emit it now.
553 PCRel)) {
554 emitConstant(DispOp.getImm() + ImmOffset, Size, CB);
555 return;
556 }
557 Expr = MCConstantExpr::create(DispOp.getImm(), Ctx);
558 } else {
559 Expr = DispOp.getExpr();
560 }
561
562 // If we have an immoffset, add it to the expression.
563 if ((FixupKind == FK_Data_4 || FixupKind == FK_Data_8 ||
566 if (Kind != GOT_None) {
567 assert(ImmOffset == 0);
568
569 if (Size == 8) {
570 FixupKind = FirstLiteralRelocationKind + ELF::R_X86_64_GOTPC64;
571 } else {
572 assert(Size == 4);
574 }
575
576 if (Kind == GOT_Normal)
577 ImmOffset = static_cast<int>(CB.size() - StartByte);
578 } else if (Expr->getKind() == MCExpr::SymbolRef) {
579 if (hasSecRelSymbolRef(Expr)) {
581 }
582 } else if (Expr->getKind() == MCExpr::Binary) {
583 const MCBinaryExpr *Bin = static_cast<const MCBinaryExpr *>(Expr);
584 if (hasSecRelSymbolRef(Bin->getLHS()) ||
585 hasSecRelSymbolRef(Bin->getRHS())) {
587 }
588 }
589 }
590
591 if (ImmOffset)
592 Expr = MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(ImmOffset, Ctx),
593 Ctx, Expr->getLoc());
594
595 // Emit a symbolic constant as a fixup and a few zero bytes.
596 Fixups.push_back(MCFixup::create(static_cast<uint32_t>(CB.size() - StartByte),
597 Expr, FixupKind, PCRel));
598 emitConstant(0, Size, CB);
599}
600
601void X86MCCodeEmitter::emitRegModRMByte(const MCOperand &ModRMReg,
602 unsigned RegOpcodeFld,
603 SmallVectorImpl<char> &CB) const {
604 emitByte(modRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)), CB);
605}
606
607void X86MCCodeEmitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base,
608 SmallVectorImpl<char> &CB) const {
609 // SIB byte is in the same format as the modRMByte.
610 emitByte(modRMByte(SS, Index, Base), CB);
611}
612
613void X86MCCodeEmitter::emitMemModRMByte(
614 const MCInst &MI, unsigned Op, unsigned RegOpcodeField, uint64_t TSFlags,
615 PrefixKind Kind, uint64_t StartByte, SmallVectorImpl<char> &CB,
616 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI,
617 bool ForceSIB) const {
618 const MCOperand &Disp = MI.getOperand(Op + X86::AddrDisp);
619 const MCOperand &Base = MI.getOperand(Op + X86::AddrBaseReg);
620 const MCOperand &Scale = MI.getOperand(Op + X86::AddrScaleAmt);
621 const MCOperand &IndexReg = MI.getOperand(Op + X86::AddrIndexReg);
622 MCRegister BaseReg = Base.getReg();
623
624 // Handle %rip relative addressing.
625 if (BaseReg == X86::RIP ||
626 BaseReg == X86::EIP) { // [disp32+rIP] in X86-64 mode
627 assert(STI.hasFeature(X86::Is64Bit) &&
628 "Rip-relative addressing requires 64-bit mode");
629 assert(!IndexReg.getReg() && !ForceSIB && "Invalid rip-relative address");
630 emitByte(modRMByte(0, RegOpcodeField, 5), CB);
631
632 unsigned Opcode = MI.getOpcode();
633 unsigned FixupKind = [&]() {
634 // Enable relaxed relocation only for a MCSymbolRefExpr. We cannot use a
635 // relaxed relocation if an offset is present (e.g. x@GOTPCREL+4).
636 if (!(Disp.isExpr() && isa<MCSymbolRefExpr>(Disp.getExpr())))
638
639 // Certain loads for GOT references can be relocated against the symbol
640 // directly if the symbol ends up in the same linkage unit.
641 switch (Opcode) {
642 default:
644 case X86::MOV64rm:
645 // movq loads is a subset of reloc_riprel_4byte_relax_rex/rex2. It is a
646 // special case because COFF and Mach-O don't support ELF's more
647 // flexible R_X86_64_REX_GOTPCRELX/R_X86_64_CODE_4_GOTPCRELX relaxation.
650 case X86::ADC32rm:
651 case X86::ADD32rm:
652 case X86::AND32rm:
653 case X86::CMP32rm:
654 case X86::MOV32rm:
655 case X86::OR32rm:
656 case X86::SBB32rm:
657 case X86::SUB32rm:
658 case X86::TEST32mr:
659 case X86::XOR32rm:
660 case X86::CALL64m:
661 case X86::JMP64m:
662 case X86::TAILJMPm64:
663 case X86::TEST64mr:
664 case X86::ADC64rm:
665 case X86::ADD64rm:
666 case X86::AND64rm:
667 case X86::CMP64rm:
668 case X86::OR64rm:
669 case X86::SBB64rm:
670 case X86::SUB64rm:
671 case X86::XOR64rm:
672 case X86::LEA64r:
676 case X86::ADD64rm_NF:
677 case X86::ADD64rm_ND:
678 case X86::ADD64mr_ND:
679 case X86::ADD64mr_NF_ND:
680 case X86::ADD64rm_NF_ND:
682 }
683 }();
684
685 // rip-relative addressing is actually relative to the *next* instruction.
686 // Since an immediate can follow the mod/rm byte for an instruction, this
687 // means that we need to bias the displacement field of the instruction with
688 // the size of the immediate field. If we have this case, add it into the
689 // expression to emit.
690 // Note: rip-relative addressing using immediate displacement values should
691 // not be adjusted, assuming it was the user's intent.
692 int ImmSize = !Disp.isImm() && X86II::hasImm(TSFlags)
693 ? X86II::getSizeOfImm(TSFlags)
694 : 0;
695
696 emitImmediate(Disp, MI.getLoc(), FixupKind, true, StartByte, CB, Fixups,
697 -ImmSize);
698 return;
699 }
700
701 unsigned BaseRegNo = BaseReg ? getX86RegNum(Base) : -1U;
702
703 bool IsAdSize16 = STI.hasFeature(X86::Is32Bit) &&
704 (TSFlags & X86II::AdSizeMask) == X86II::AdSize16;
705
706 // 16-bit addressing forms of the ModR/M byte have a different encoding for
707 // the R/M field and are far more limited in which registers can be used.
708 if (IsAdSize16 || X86_MC::is16BitMemOperand(MI, Op, STI)) {
709 if (BaseReg) {
710 // For 32-bit addressing, the row and column values in Table 2-2 are
711 // basically the same. It's AX/CX/DX/BX/SP/BP/SI/DI in that order, with
712 // some special cases. And getX86RegNum reflects that numbering.
713 // For 16-bit addressing it's more fun, as shown in the SDM Vol 2A,
714 // Table 2-1 "16-Bit Addressing Forms with the ModR/M byte". We can only
715 // use SI/DI/BP/BX, which have "row" values 4-7 in no particular order,
716 // while values 0-3 indicate the allowed combinations (base+index) of
717 // those: 0 for BX+SI, 1 for BX+DI, 2 for BP+SI, 3 for BP+DI.
718 //
719 // R16Table[] is a lookup from the normal RegNo, to the row values from
720 // Table 2-1 for 16-bit addressing modes. Where zero means disallowed.
721 static const unsigned R16Table[] = {0, 0, 0, 7, 0, 6, 4, 5};
722 unsigned RMfield = R16Table[BaseRegNo];
723
724 assert(RMfield && "invalid 16-bit base register");
725
726 if (IndexReg.getReg()) {
727 unsigned IndexReg16 = R16Table[getX86RegNum(IndexReg)];
728
729 assert(IndexReg16 && "invalid 16-bit index register");
730 // We must have one of SI/DI (4,5), and one of BP/BX (6,7).
731 assert(((IndexReg16 ^ RMfield) & 2) &&
732 "invalid 16-bit base/index register combination");
733 assert(Scale.getImm() == 1 &&
734 "invalid scale for 16-bit memory reference");
735
736 // Allow base/index to appear in either order (although GAS doesn't).
737 if (IndexReg16 & 2)
738 RMfield = (RMfield & 1) | ((7 - IndexReg16) << 1);
739 else
740 RMfield = (IndexReg16 & 1) | ((7 - RMfield) << 1);
741 }
742
743 if (Disp.isImm() && isInt<8>(Disp.getImm())) {
744 if (Disp.getImm() == 0 && RMfield != 6) {
745 // There is no displacement; just the register.
746 emitByte(modRMByte(0, RegOpcodeField, RMfield), CB);
747 return;
748 }
749 // Use the [REG]+disp8 form, including for [BP] which cannot be encoded.
750 emitByte(modRMByte(1, RegOpcodeField, RMfield), CB);
751 emitImmediate(Disp, MI.getLoc(), FK_Data_1, false, StartByte, CB,
752 Fixups);
753 return;
754 }
755 // This is the [REG]+disp16 case.
756 emitByte(modRMByte(2, RegOpcodeField, RMfield), CB);
757 } else {
758 assert(!IndexReg.getReg() && "Unexpected index register!");
759 // There is no BaseReg; this is the plain [disp16] case.
760 emitByte(modRMByte(0, RegOpcodeField, 6), CB);
761 }
762
763 // Emit 16-bit displacement for plain disp16 or [REG]+disp16 cases.
764 emitImmediate(Disp, MI.getLoc(), FK_Data_2, false, StartByte, CB, Fixups);
765 return;
766 }
767
768 // Check for presence of {disp8} or {disp32} pseudo prefixes.
769 bool UseDisp8 = MI.getFlags() & X86::IP_USE_DISP8;
770 bool UseDisp32 = MI.getFlags() & X86::IP_USE_DISP32;
771
772 // We only allow no displacement if no pseudo prefix is present.
773 bool AllowNoDisp = !UseDisp8 && !UseDisp32;
774 // Disp8 is allowed unless the {disp32} prefix is present.
775 bool AllowDisp8 = !UseDisp32;
776
777 // Determine whether a SIB byte is needed.
778 if (!ForceSIB && !X86II::needSIB(BaseReg, IndexReg.getReg(),
779 STI.hasFeature(X86::Is64Bit))) {
780 if (!BaseReg) { // [disp32] in X86-32 mode
781 emitByte(modRMByte(0, RegOpcodeField, 5), CB);
782 emitImmediate(Disp, MI.getLoc(), FK_Data_4, false, StartByte, CB, Fixups);
783 return;
784 }
785
786 // If the base is not EBP/ESP/R12/R13/R20/R21/R28/R29 and there is no
787 // displacement, use simple indirect register encoding, this handles
788 // addresses like [EAX]. The encoding for [EBP], [R13], [R20], [R21], [R28]
789 // or [R29] with no displacement means [disp32] so we handle it by emitting
790 // a displacement of 0 later.
791 if (BaseRegNo != N86::EBP) {
792 if (Disp.isImm() && Disp.getImm() == 0 && AllowNoDisp) {
793 emitByte(modRMByte(0, RegOpcodeField, BaseRegNo), CB);
794 return;
795 }
796
797 // If the displacement is @tlscall, treat it as a zero.
798 if (Disp.isExpr()) {
799 auto *Sym = dyn_cast<MCSymbolRefExpr>(Disp.getExpr());
800 if (Sym && Sym->getSpecifier() == X86::S_TLSCALL) {
801 // This is exclusively used by call *a@tlscall(base). The relocation
802 // (R_386_TLSCALL or R_X86_64_TLSCALL) applies to the beginning.
803 Fixups.push_back(MCFixup::create(0, Sym, FK_NONE));
804 emitByte(modRMByte(0, RegOpcodeField, BaseRegNo), CB);
805 return;
806 }
807 }
808 }
809
810 // Otherwise, if the displacement fits in a byte, encode as [REG+disp8].
811 // Including a compressed disp8 for EVEX instructions that support it.
812 // This also handles the 0 displacement for [EBP], [R13], [R21] or [R29]. We
813 // can't use disp8 if the {disp32} pseudo prefix is present.
814 if (Disp.isImm() && AllowDisp8) {
815 int ImmOffset = 0;
816 if (isDispOrCDisp8(TSFlags, Disp.getImm(), ImmOffset)) {
817 emitByte(modRMByte(1, RegOpcodeField, BaseRegNo), CB);
818 emitImmediate(Disp, MI.getLoc(), FK_Data_1, false, StartByte, CB,
819 Fixups, ImmOffset);
820 return;
821 }
822 }
823
824 // Otherwise, emit the most general non-SIB encoding: [REG+disp32].
825 // Displacement may be 0 for [EBP], [R13], [R21], [R29] case if {disp32}
826 // pseudo prefix prevented using disp8 above.
827 emitByte(modRMByte(2, RegOpcodeField, BaseRegNo), CB);
828 unsigned Opcode = MI.getOpcode();
829 unsigned FixupKind = Opcode == X86::MOV32rm ? X86::reloc_signed_4byte_relax
831 emitImmediate(Disp, MI.getLoc(), MCFixupKind(FixupKind), false, StartByte,
832 CB, Fixups);
833 return;
834 }
835
836 // We need a SIB byte, so start by outputting the ModR/M byte first
837 assert(IndexReg.getReg() != X86::ESP && IndexReg.getReg() != X86::RSP &&
838 "Cannot use ESP as index reg!");
839
840 bool ForceDisp32 = false;
841 bool ForceDisp8 = false;
842 int ImmOffset = 0;
843 if (!BaseReg) {
844 // If there is no base register, we emit the special case SIB byte with
845 // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
846 BaseRegNo = 5;
847 emitByte(modRMByte(0, RegOpcodeField, 4), CB);
848 ForceDisp32 = true;
849 } else if (Disp.isImm() && Disp.getImm() == 0 && AllowNoDisp &&
850 // Base reg can't be EBP/RBP/R13/R21/R29 as that would end up with
851 // '5' as the base field, but that is the magic [*] nomenclature
852 // that indicates no base when mod=0. For these cases we'll emit a
853 // 0 displacement instead.
854 BaseRegNo != N86::EBP) {
855 // Emit no displacement ModR/M byte
856 emitByte(modRMByte(0, RegOpcodeField, 4), CB);
857 } else if (Disp.isImm() && AllowDisp8 &&
858 isDispOrCDisp8(TSFlags, Disp.getImm(), ImmOffset)) {
859 // Displacement fits in a byte or matches an EVEX compressed disp8, use
860 // disp8 encoding. This also handles EBP/R13/R21/R29 base with 0
861 // displacement unless {disp32} pseudo prefix was used.
862 emitByte(modRMByte(1, RegOpcodeField, 4), CB);
863 ForceDisp8 = true;
864 } else {
865 // Otherwise, emit the normal disp32 encoding.
866 emitByte(modRMByte(2, RegOpcodeField, 4), CB);
867 ForceDisp32 = true;
868 }
869
870 // Calculate what the SS field value should be...
871 static const unsigned SSTable[] = {~0U, 0, 1, ~0U, 2, ~0U, ~0U, ~0U, 3};
872 unsigned SS = SSTable[Scale.getImm()];
873
874 unsigned IndexRegNo = IndexReg.getReg() ? getX86RegNum(IndexReg) : 4;
875
876 emitSIBByte(SS, IndexRegNo, BaseRegNo, CB);
877
878 // Do we need to output a displacement?
879 if (ForceDisp8)
880 emitImmediate(Disp, MI.getLoc(), FK_Data_1, false, StartByte, CB, Fixups,
881 ImmOffset);
882 else if (ForceDisp32)
883 emitImmediate(Disp, MI.getLoc(), X86::reloc_signed_4byte, false, StartByte,
884 CB, Fixups);
885}
886
887/// Emit all instruction prefixes.
888///
889/// \returns one of the REX, XOP, VEX2, VEX3, EVEX if any of them is used,
890/// otherwise returns None.
891PrefixKind X86MCCodeEmitter::emitPrefixImpl(const MCInst &MI,
892 const MCSubtargetInfo &STI,
893 SmallVectorImpl<char> &CB) const {
894 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
895 uint64_t TSFlags = Desc.TSFlags;
896 // Determine where the memory operand starts, if present.
897 int MemoryOperand = X86II::getMemoryOperandIdx(Desc);
898 // Emit segment override opcode prefix as needed.
899 if (MemoryOperand != -1)
900 emitSegmentOverridePrefix(MemoryOperand + X86::AddrSegmentReg, MI, CB);
901
902 // Emit the repeat opcode prefix as needed.
903 unsigned Flags = MI.getFlags();
904 if (TSFlags & X86II::REP || Flags & X86::IP_HAS_REPEAT)
905 emitByte(0xF3, CB);
906 if (Flags & X86::IP_HAS_REPEAT_NE)
907 emitByte(0xF2, CB);
908
909 // Emit the address size opcode prefix as needed.
910 if (X86_MC::needsAddressSizeOverride(MI, STI, MemoryOperand, TSFlags) ||
911 Flags & X86::IP_HAS_AD_SIZE)
912 emitByte(0x67, CB);
913
914 uint64_t Form = TSFlags & X86II::FormMask;
915 switch (Form) {
916 default:
917 break;
919 // Emit segment override opcode prefix as needed (not for %ds).
920 if (MI.getOperand(2).getReg() != X86::DS)
921 emitSegmentOverridePrefix(2, MI, CB);
922 break;
923 case X86II::RawFrmSrc:
924 // Emit segment override opcode prefix as needed (not for %ds).
925 if (MI.getOperand(1).getReg() != X86::DS)
926 emitSegmentOverridePrefix(1, MI, CB);
927 break;
929 // Emit segment override opcode prefix as needed.
930 emitSegmentOverridePrefix(1, MI, CB);
931 break;
932 }
933
934 // REX prefix is optional, but if used must be immediately before the opcode
935 // Encoding type for this instruction.
936 return (TSFlags & X86II::EncodingMask)
937 ? emitVEXOpcodePrefix(MemoryOperand, MI, STI, CB)
938 : emitOpcodePrefix(MemoryOperand, MI, STI, CB);
939}
940
941// AVX instructions are encoded using an encoding scheme that combines
942// prefix bytes, opcode extension field, operand encoding fields, and vector
943// length encoding capability into a new prefix, referred to as VEX.
944
945// The majority of the AVX-512 family of instructions (operating on
946// 512/256/128-bit vector register operands) are encoded using a new prefix
947// (called EVEX).
948
949// XOP is a revised subset of what was originally intended as SSE5. It was
950// changed to be similar but not overlapping with AVX.
951
952/// Emit XOP, VEX2, VEX3 or EVEX prefix.
953/// \returns the used prefix.
954PrefixKind
955X86MCCodeEmitter::emitVEXOpcodePrefix(int MemOperand, const MCInst &MI,
956 const MCSubtargetInfo &STI,
957 SmallVectorImpl<char> &CB) const {
958 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
959 uint64_t TSFlags = Desc.TSFlags;
960
961 assert(!(TSFlags & X86II::LOCK) && "Can't have LOCK VEX.");
962
963#ifndef NDEBUG
964 unsigned NumOps = MI.getNumOperands();
965 for (unsigned I = NumOps ? X86II::getOperandBias(Desc) : 0; I != NumOps;
966 ++I) {
967 const MCOperand &MO = MI.getOperand(I);
968 if (!MO.isReg())
969 continue;
970 MCRegister Reg = MO.getReg();
971 if (Reg == X86::AH || Reg == X86::BH || Reg == X86::CH || Reg == X86::DH)
973 "Cannot encode high byte register in VEX/EVEX-prefixed instruction");
974 }
975#endif
976
977 X86OpcodePrefixHelper Prefix(*Ctx.getRegisterInfo());
978 switch (TSFlags & X86II::EncodingMask) {
979 default:
980 break;
981 case X86II::XOP:
982 Prefix.setLowerBound(XOP);
983 break;
984 case X86II::VEX:
985 // VEX can be 2 byte or 3 byte, not determined yet if not explicit
986 Prefix.setLowerBound((MI.getFlags() & X86::IP_USE_VEX3) ? VEX3 : VEX2);
987 break;
988 case X86II::EVEX:
989 Prefix.setLowerBound(EVEX);
990 break;
991 }
992
993 Prefix.setW(TSFlags & X86II::REX_W);
994 Prefix.setNF(TSFlags & X86II::EVEX_NF);
995
996 bool HasEVEX_K = TSFlags & X86II::EVEX_K;
997 bool HasVEX_4V = TSFlags & X86II::VEX_4V;
998 bool IsND = X86II::hasNewDataDest(TSFlags); // IsND implies HasVEX_4V
999 bool HasEVEX_RC = TSFlags & X86II::EVEX_RC;
1000
1001 switch (TSFlags & X86II::OpMapMask) {
1002 default:
1003 llvm_unreachable("Invalid prefix!");
1004 case X86II::TB:
1005 Prefix.set5M(0x1); // 0F
1006 break;
1007 case X86II::T8:
1008 Prefix.set5M(0x2); // 0F 38
1009 break;
1010 case X86II::TA:
1011 Prefix.set5M(0x3); // 0F 3A
1012 break;
1013 case X86II::XOP8:
1014 Prefix.set5M(0x8);
1015 break;
1016 case X86II::XOP9:
1017 Prefix.set5M(0x9);
1018 break;
1019 case X86II::XOPA:
1020 Prefix.set5M(0xA);
1021 break;
1022 case X86II::T_MAP4:
1023 Prefix.set5M(0x4);
1024 break;
1025 case X86II::T_MAP5:
1026 Prefix.set5M(0x5);
1027 break;
1028 case X86II::T_MAP6:
1029 Prefix.set5M(0x6);
1030 break;
1031 case X86II::T_MAP7:
1032 Prefix.set5M(0x7);
1033 break;
1034 }
1035
1036 Prefix.setL(TSFlags & X86II::VEX_L);
1037 Prefix.setL2(TSFlags & X86II::EVEX_L2);
1038 switch (TSFlags & X86II::OpPrefixMask) {
1039 case X86II::PD:
1040 Prefix.setPP(0x1); // 66
1041 break;
1042 case X86II::XS:
1043 Prefix.setPP(0x2); // F3
1044 break;
1045 case X86II::XD:
1046 Prefix.setPP(0x3); // F2
1047 break;
1048 }
1049
1050 Prefix.setZ(HasEVEX_K && (TSFlags & X86II::EVEX_Z));
1051 Prefix.setEVEX_b(TSFlags & X86II::EVEX_B);
1052 Prefix.setEVEX_U(TSFlags & X86II::EVEX_U);
1053
1054 bool EncodeRC = false;
1055 uint8_t EVEX_rc = 0;
1056
1057 unsigned CurOp = X86II::getOperandBias(Desc);
1058 bool HasTwoConditionalOps = TSFlags & X86II::TwoConditionalOps;
1059
1060 switch (TSFlags & X86II::FormMask) {
1061 default:
1062 llvm_unreachable("Unexpected form in emitVEXOpcodePrefix!");
1064 // src1(ModR/M), MemAddr, src2(VEX_4V)
1065 Prefix.setRR2(MI, CurOp++);
1066 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1067 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1068 CurOp += X86::AddrNumOperands;
1069 Prefix.set4VV2(MI, CurOp++);
1070 break;
1071 }
1072 case X86II::MRM_C0:
1073 case X86II::RawFrm:
1074 break;
1077 case X86II::MRMDestMem: {
1078 // MRMDestMem instructions forms:
1079 // MemAddr, src1(ModR/M)
1080 // MemAddr, src1(VEX_4V), src2(ModR/M)
1081 // MemAddr, src1(ModR/M), imm8
1082 //
1083 // NDD:
1084 // dst(VEX_4V), MemAddr, src1(ModR/M)
1085 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1086 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1087 Prefix.setV2(MI, MemOperand + X86::AddrIndexReg, HasVEX_4V);
1088
1089 if (IsND)
1090 Prefix.set4VV2(MI, CurOp++);
1091
1092 CurOp += X86::AddrNumOperands;
1093
1094 if (HasEVEX_K)
1095 Prefix.setAAA(MI, CurOp++);
1096
1097 if (!IsND && HasVEX_4V)
1098 Prefix.set4VV2(MI, CurOp++);
1099
1100 Prefix.setRR2(MI, CurOp++);
1101 if (HasTwoConditionalOps) {
1102 Prefix.set4V(MI, CurOp++, /*IsImm=*/true);
1103 Prefix.setSC(MI, CurOp++);
1104 }
1105 break;
1106 }
1107 case X86II::MRMSrcMemCC:
1109 case X86II::MRMSrcMem: {
1110 // MRMSrcMem instructions forms:
1111 // src1(ModR/M), MemAddr
1112 // src1(ModR/M), src2(VEX_4V), MemAddr
1113 // src1(ModR/M), MemAddr, imm8
1114 // src1(ModR/M), MemAddr, src2(Imm[7:4])
1115 //
1116 // FMA4:
1117 // dst(ModR/M.reg), src1(VEX_4V), src2(ModR/M), src3(Imm[7:4])
1118 //
1119 // NDD:
1120 // dst(VEX_4V), src1(ModR/M), MemAddr
1121 if (IsND)
1122 Prefix.set4VV2(MI, CurOp++);
1123
1124 Prefix.setRR2(MI, CurOp++);
1125
1126 if (HasEVEX_K)
1127 Prefix.setAAA(MI, CurOp++);
1128
1129 if (!IsND && HasVEX_4V)
1130 Prefix.set4VV2(MI, CurOp++);
1131
1132 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1133 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1134 Prefix.setV2(MI, MemOperand + X86::AddrIndexReg, HasVEX_4V);
1135 CurOp += X86::AddrNumOperands;
1136 if (HasTwoConditionalOps) {
1137 Prefix.set4V(MI, CurOp++, /*IsImm=*/true);
1138 Prefix.setSC(MI, CurOp++);
1139 }
1140 break;
1141 }
1142 case X86II::MRMSrcMem4VOp3: {
1143 // Instruction format for 4VOp3:
1144 // src1(ModR/M), MemAddr, src3(VEX_4V)
1145 Prefix.setRR2(MI, CurOp++);
1146 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1147 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1148 Prefix.set4VV2(MI, CurOp + X86::AddrNumOperands);
1149 break;
1150 }
1151 case X86II::MRMSrcMemOp4: {
1152 // dst(ModR/M.reg), src1(VEX_4V), src2(Imm[7:4]), src3(ModR/M),
1153 Prefix.setR(MI, CurOp++);
1154 Prefix.set4V(MI, CurOp++);
1155 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1156 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1157 break;
1158 }
1159 case X86II::MRMXmCC:
1160 case X86II::MRM0m:
1161 case X86II::MRM1m:
1162 case X86II::MRM2m:
1163 case X86II::MRM3m:
1164 case X86II::MRM4m:
1165 case X86II::MRM5m:
1166 case X86II::MRM6m:
1167 case X86II::MRM7m: {
1168 // MRM[0-9]m instructions forms:
1169 // MemAddr
1170 // src1(VEX_4V), MemAddr
1171 if (HasVEX_4V)
1172 Prefix.set4VV2(MI, CurOp++);
1173
1174 if (HasEVEX_K)
1175 Prefix.setAAA(MI, CurOp++);
1176
1177 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1178 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1179 Prefix.setV2(MI, MemOperand + X86::AddrIndexReg, HasVEX_4V);
1180 CurOp += X86::AddrNumOperands + 1; // Skip first imm.
1181 if (HasTwoConditionalOps) {
1182 Prefix.set4V(MI, CurOp++, /*IsImm=*/true);
1183 Prefix.setSC(MI, CurOp++);
1184 }
1185 break;
1186 }
1187 case X86II::MRMSrcRegCC:
1188 case X86II::MRMSrcReg: {
1189 // MRMSrcReg instructions forms:
1190 // dst(ModR/M), src1(VEX_4V), src2(ModR/M), src3(Imm[7:4])
1191 // dst(ModR/M), src1(ModR/M)
1192 // dst(ModR/M), src1(ModR/M), imm8
1193 //
1194 // FMA4:
1195 // dst(ModR/M.reg), src1(VEX_4V), src2(Imm[7:4]), src3(ModR/M),
1196 //
1197 // NDD:
1198 // dst(VEX_4V), src1(ModR/M.reg), src2(ModR/M)
1199 if (IsND)
1200 Prefix.set4VV2(MI, CurOp++);
1201 Prefix.setRR2(MI, CurOp++);
1202
1203 if (HasEVEX_K)
1204 Prefix.setAAA(MI, CurOp++);
1205
1206 if (!IsND && HasVEX_4V)
1207 Prefix.set4VV2(MI, CurOp++);
1208
1209 Prefix.setBB2(MI, CurOp);
1210 Prefix.setX(MI, CurOp, 4);
1211 ++CurOp;
1212
1213 if (HasTwoConditionalOps) {
1214 Prefix.set4V(MI, CurOp++, /*IsImm=*/true);
1215 Prefix.setSC(MI, CurOp++);
1216 }
1217
1218 if (TSFlags & X86II::EVEX_B) {
1219 if (HasEVEX_RC) {
1220 unsigned NumOps = Desc.getNumOperands();
1221 unsigned RcOperand = NumOps - 1;
1222 assert(RcOperand >= CurOp);
1223 EVEX_rc = MI.getOperand(RcOperand).getImm();
1224 assert(EVEX_rc <= 3 && "Invalid rounding control!");
1225 }
1226 EncodeRC = true;
1227 }
1228 break;
1229 }
1230 case X86II::MRMSrcReg4VOp3: {
1231 // Instruction format for 4VOp3:
1232 // src1(ModR/M), src2(ModR/M), src3(VEX_4V)
1233 Prefix.setRR2(MI, CurOp++);
1234 Prefix.setBB2(MI, CurOp++);
1235 Prefix.set4VV2(MI, CurOp++);
1236 break;
1237 }
1238 case X86II::MRMSrcRegOp4: {
1239 // dst(ModR/M.reg), src1(VEX_4V), src2(Imm[7:4]), src3(ModR/M),
1240 Prefix.setR(MI, CurOp++);
1241 Prefix.set4V(MI, CurOp++);
1242 // Skip second register source (encoded in Imm[7:4])
1243 ++CurOp;
1244
1245 Prefix.setB(MI, CurOp);
1246 Prefix.setX(MI, CurOp, 4);
1247 ++CurOp;
1248 break;
1249 }
1251 case X86II::MRMDestReg: {
1252 // MRMDestReg instructions forms:
1253 // dst(ModR/M), src(ModR/M)
1254 // dst(ModR/M), src(ModR/M), imm8
1255 // dst(ModR/M), src1(VEX_4V), src2(ModR/M)
1256 //
1257 // NDD:
1258 // dst(VEX_4V), src1(ModR/M), src2(ModR/M)
1259 if (IsND)
1260 Prefix.set4VV2(MI, CurOp++);
1261 Prefix.setBB2(MI, CurOp);
1262 Prefix.setX(MI, CurOp, 4);
1263 ++CurOp;
1264
1265 if (HasEVEX_K)
1266 Prefix.setAAA(MI, CurOp++);
1267
1268 if (!IsND && HasVEX_4V)
1269 Prefix.set4VV2(MI, CurOp++);
1270
1271 Prefix.setRR2(MI, CurOp++);
1272 if (HasTwoConditionalOps) {
1273 Prefix.set4V(MI, CurOp++, /*IsImm=*/true);
1274 Prefix.setSC(MI, CurOp++);
1275 }
1276 if (TSFlags & X86II::EVEX_B)
1277 EncodeRC = true;
1278 break;
1279 }
1280 case X86II::MRMr0: {
1281 // MRMr0 instructions forms:
1282 // 11:rrr:000
1283 // dst(ModR/M)
1284 Prefix.setRR2(MI, CurOp++);
1285 break;
1286 }
1287 case X86II::MRMXrCC:
1288 case X86II::MRM0r:
1289 case X86II::MRM1r:
1290 case X86II::MRM2r:
1291 case X86II::MRM3r:
1292 case X86II::MRM4r:
1293 case X86II::MRM5r:
1294 case X86II::MRM6r:
1295 case X86II::MRM7r: {
1296 // MRM0r-MRM7r instructions forms:
1297 // dst(VEX_4V), src(ModR/M), imm8
1298 if (HasVEX_4V)
1299 Prefix.set4VV2(MI, CurOp++);
1300
1301 if (HasEVEX_K)
1302 Prefix.setAAA(MI, CurOp++);
1303
1304 Prefix.setBB2(MI, CurOp);
1305 Prefix.setX(MI, CurOp, 4);
1306 ++CurOp;
1307 if (HasTwoConditionalOps) {
1308 Prefix.set4V(MI, ++CurOp, /*IsImm=*/true);
1309 Prefix.setSC(MI, ++CurOp);
1310 }
1311 break;
1312 }
1313 }
1314 if (EncodeRC) {
1315 Prefix.setL(EVEX_rc & 0x1);
1316 Prefix.setL2(EVEX_rc & 0x2);
1317 }
1318 PrefixKind Kind = Prefix.determineOptimalKind();
1319 Prefix.emit(CB);
1320 return Kind;
1321}
1322
1323/// Emit REX prefix which specifies
1324/// 1) 64-bit instructions,
1325/// 2) non-default operand size, and
1326/// 3) use of X86-64 extended registers.
1327///
1328/// \returns the used prefix (REX or None).
1329PrefixKind X86MCCodeEmitter::emitREXPrefix(int MemOperand, const MCInst &MI,
1330 const MCSubtargetInfo &STI,
1331 SmallVectorImpl<char> &CB) const {
1332 if (!STI.hasFeature(X86::Is64Bit))
1333 return None;
1334 X86OpcodePrefixHelper Prefix(*Ctx.getRegisterInfo());
1335 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1336 uint64_t TSFlags = Desc.TSFlags;
1337 Prefix.setW(TSFlags & X86II::REX_W);
1338 unsigned NumOps = MI.getNumOperands();
1339 bool UsesHighByteReg = false;
1340#ifndef NDEBUG
1341 bool HasRegOp = false;
1342#endif
1343 unsigned CurOp = NumOps ? X86II::getOperandBias(Desc) : 0;
1344 for (unsigned i = CurOp; i != NumOps; ++i) {
1345 const MCOperand &MO = MI.getOperand(i);
1346 if (MO.isReg()) {
1347#ifndef NDEBUG
1348 HasRegOp = true;
1349#endif
1350 MCRegister Reg = MO.getReg();
1351 if (Reg == X86::AH || Reg == X86::BH || Reg == X86::CH || Reg == X86::DH)
1352 UsesHighByteReg = true;
1353 // If it accesses SPL, BPL, SIL, or DIL, then it requires a REX prefix.
1355 Prefix.setLowerBound(REX);
1356 } else if (MO.isExpr() && STI.getTargetTriple().isX32()) {
1357 // GOTTPOFF and TLSDESC relocations require a REX prefix to allow
1358 // linker optimizations: even if the instructions we see may not require
1359 // any prefix, they may be replaced by instructions that do. This is
1360 // handled as a special case here so that it also works for hand-written
1361 // assembly without the user needing to write REX, as with GNU as.
1362 const auto *Ref = dyn_cast<MCSymbolRefExpr>(MO.getExpr());
1363 if (Ref && (Ref->getSpecifier() == X86::S_GOTTPOFF ||
1364 Ref->getSpecifier() == X86::S_TLSDESC)) {
1365 Prefix.setLowerBound(REX);
1366 }
1367 }
1368 }
1369 if (MI.getFlags() & X86::IP_USE_REX)
1370 Prefix.setLowerBound(REX);
1372 MI.getFlags() & X86::IP_USE_REX2)
1373 Prefix.setLowerBound(REX2);
1374 switch (TSFlags & X86II::FormMask) {
1375 default:
1376 assert(!HasRegOp && "Unexpected form in emitREXPrefix!");
1377 break;
1378 case X86II::RawFrm:
1380 case X86II::RawFrmSrc:
1381 case X86II::RawFrmDst:
1383 break;
1384 case X86II::AddRegFrm:
1385 Prefix.setBB2(MI, CurOp++);
1386 break;
1387 case X86II::MRMSrcReg:
1388 case X86II::MRMSrcRegCC:
1389 Prefix.setRR2(MI, CurOp++);
1390 Prefix.setBB2(MI, CurOp++);
1391 break;
1392 case X86II::MRMSrcMem:
1393 case X86II::MRMSrcMemCC:
1394 Prefix.setRR2(MI, CurOp++);
1395 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1396 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1397 CurOp += X86::AddrNumOperands;
1398 break;
1399 case X86II::MRMDestReg:
1400 Prefix.setBB2(MI, CurOp++);
1401 Prefix.setRR2(MI, CurOp++);
1402 break;
1403 case X86II::MRMDestMem:
1404 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1405 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1406 CurOp += X86::AddrNumOperands;
1407 Prefix.setRR2(MI, CurOp++);
1408 break;
1409 case X86II::MRMXmCC:
1410 case X86II::MRMXm:
1411 case X86II::MRM0m:
1412 case X86II::MRM1m:
1413 case X86II::MRM2m:
1414 case X86II::MRM3m:
1415 case X86II::MRM4m:
1416 case X86II::MRM5m:
1417 case X86II::MRM6m:
1418 case X86II::MRM7m:
1419 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1420 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1421 break;
1422 case X86II::MRMXrCC:
1423 case X86II::MRMXr:
1424 case X86II::MRM0r:
1425 case X86II::MRM1r:
1426 case X86II::MRM2r:
1427 case X86II::MRM3r:
1428 case X86II::MRM4r:
1429 case X86II::MRM5r:
1430 case X86II::MRM6r:
1431 case X86II::MRM7r:
1432 Prefix.setBB2(MI, CurOp++);
1433 break;
1434 }
1435 Prefix.setM((TSFlags & X86II::OpMapMask) == X86II::TB);
1436 PrefixKind Kind = Prefix.determineOptimalKind();
1437 if (Kind && UsesHighByteReg)
1439 "Cannot encode high byte register in REX-prefixed instruction");
1440 Prefix.emit(CB);
1441 return Kind;
1442}
1443
1444/// Emit segment override opcode prefix as needed.
1445void X86MCCodeEmitter::emitSegmentOverridePrefix(
1446 unsigned SegOperand, const MCInst &MI, SmallVectorImpl<char> &CB) const {
1447 // Check for explicit segment override on memory operand.
1448 if (MCRegister Reg = MI.getOperand(SegOperand).getReg())
1450}
1451
1452/// Emit all instruction prefixes prior to the opcode.
1453///
1454/// \param MemOperand the operand # of the start of a memory operand if present.
1455/// If not present, it is -1.
1456///
1457/// \returns the used prefix (REX or None).
1458PrefixKind X86MCCodeEmitter::emitOpcodePrefix(int MemOperand, const MCInst &MI,
1459 const MCSubtargetInfo &STI,
1460 SmallVectorImpl<char> &CB) const {
1461 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1462 uint64_t TSFlags = Desc.TSFlags;
1463
1464 // Emit the operand size opcode prefix as needed.
1465 if ((TSFlags & X86II::OpSizeMask) ==
1466 (STI.hasFeature(X86::Is16Bit) ? X86II::OpSize32 : X86II::OpSize16))
1467 emitByte(0x66, CB);
1468
1469 // Emit the LOCK opcode prefix.
1470 if (TSFlags & X86II::LOCK || MI.getFlags() & X86::IP_HAS_LOCK)
1471 emitByte(0xF0, CB);
1472
1473 // Emit the NOTRACK opcode prefix.
1474 if (TSFlags & X86II::NOTRACK || MI.getFlags() & X86::IP_HAS_NOTRACK)
1475 emitByte(0x3E, CB);
1476
1477 switch (TSFlags & X86II::OpPrefixMask) {
1478 case X86II::PD: // 66
1479 emitByte(0x66, CB);
1480 break;
1481 case X86II::XS: // F3
1482 emitByte(0xF3, CB);
1483 break;
1484 case X86II::XD: // F2
1485 emitByte(0xF2, CB);
1486 break;
1487 }
1488
1489 // Handle REX prefix.
1490 assert((STI.hasFeature(X86::Is64Bit) || !(TSFlags & X86II::REX_W)) &&
1491 "REX.W requires 64bit mode.");
1492 PrefixKind Kind = emitREXPrefix(MemOperand, MI, STI, CB);
1493
1494 // 0x0F escape code must be emitted just before the opcode.
1495 switch (TSFlags & X86II::OpMapMask) {
1496 case X86II::TB: // Two-byte opcode map
1497 // Encoded by M bit in REX2
1498 if (Kind == REX2)
1499 break;
1500 [[fallthrough]];
1501 case X86II::T8: // 0F 38
1502 case X86II::TA: // 0F 3A
1503 case X86II::ThreeDNow: // 0F 0F, second 0F emitted by caller.
1504 emitByte(0x0F, CB);
1505 break;
1506 }
1507
1508 switch (TSFlags & X86II::OpMapMask) {
1509 case X86II::T8: // 0F 38
1510 emitByte(0x38, CB);
1511 break;
1512 case X86II::TA: // 0F 3A
1513 emitByte(0x3A, CB);
1514 break;
1515 }
1516
1517 return Kind;
1518}
1519
1520void X86MCCodeEmitter::emitPrefix(const MCInst &MI, SmallVectorImpl<char> &CB,
1521 const MCSubtargetInfo &STI) const {
1522 uint64_t TSFlags = MCII.get(MI.getOpcode()).TSFlags;
1523
1524 // Pseudo instructions don't get encoded.
1525 if (X86II::isPseudo(TSFlags))
1526 return;
1527
1528 emitPrefixImpl(MI, STI, CB);
1529}
1530
1532 SmallVectorImpl<char> &CB, const MCSubtargetInfo &STI) {
1533 static_cast<X86MCCodeEmitter &>(MCE).emitPrefix(MI, CB, STI);
1534}
1535
1536void X86MCCodeEmitter::encodeInstruction(const MCInst &MI,
1539 const MCSubtargetInfo &STI) const {
1540 unsigned Opcode = MI.getOpcode();
1541 const MCInstrDesc &Desc = MCII.get(Opcode);
1542 uint64_t TSFlags = Desc.TSFlags;
1543
1544 // Pseudo instructions don't get encoded.
1545 if (X86II::isPseudo(TSFlags))
1546 return;
1547
1548 unsigned NumOps = Desc.getNumOperands();
1549 unsigned CurOp = X86II::getOperandBias(Desc);
1550
1551 uint64_t StartByte = CB.size();
1552
1553 PrefixKind Kind = emitPrefixImpl(MI, STI, CB);
1554
1555 // It uses the VEX.VVVV field?
1556 bool HasVEX_4V = TSFlags & X86II::VEX_4V;
1557 bool HasVEX_I8Reg = (TSFlags & X86II::ImmMask) == X86II::Imm8Reg;
1558
1559 // It uses the EVEX.aaa field?
1560 bool HasEVEX_K = TSFlags & X86II::EVEX_K;
1561 bool HasEVEX_RC = TSFlags & X86II::EVEX_RC;
1562
1563 // Used if a register is encoded in 7:4 of immediate.
1564 unsigned I8RegNum = 0;
1565
1566 uint8_t BaseOpcode = X86II::getBaseOpcodeFor(TSFlags);
1567
1568 if ((TSFlags & X86II::OpMapMask) == X86II::ThreeDNow)
1569 BaseOpcode = 0x0F; // Weird 3DNow! encoding.
1570
1571 unsigned OpcodeOffset = 0;
1572
1573 bool IsND = X86II::hasNewDataDest(TSFlags);
1574 bool HasTwoConditionalOps = TSFlags & X86II::TwoConditionalOps;
1575
1576 uint64_t Form = TSFlags & X86II::FormMask;
1577 switch (Form) {
1578 default:
1579 errs() << "FORM: " << Form << "\n";
1580 llvm_unreachable("Unknown FormMask value in X86MCCodeEmitter!");
1581 case X86II::Pseudo:
1582 llvm_unreachable("Pseudo instruction shouldn't be emitted");
1584 emitByte(BaseOpcode, CB);
1585 CurOp += 3; // Consume operands.
1586 break;
1587 case X86II::RawFrmSrc:
1588 emitByte(BaseOpcode, CB);
1589 CurOp += 2; // Consume operands.
1590 break;
1591 case X86II::RawFrmDst:
1592 emitByte(BaseOpcode, CB);
1593 ++CurOp; // Consume operand.
1594 break;
1595 case X86II::PrefixByte:
1596 emitByte(BaseOpcode, CB);
1597 break;
1598 case X86II::AddCCFrm: {
1599 // This will be added to the opcode in the fallthrough.
1600 OpcodeOffset = MI.getOperand(NumOps - 1).getImm();
1601 assert(OpcodeOffset < 16 && "Unexpected opcode offset!");
1602 --NumOps; // Drop the operand from the end.
1603 [[fallthrough]];
1604 case X86II::RawFrm:
1605 emitByte(BaseOpcode + OpcodeOffset, CB);
1606
1607 if (!STI.hasFeature(X86::Is64Bit) || !isPCRel32Branch(MI, MCII))
1608 break;
1609
1610 const MCOperand &Op = MI.getOperand(CurOp++);
1611 emitImmediate(Op, MI.getLoc(), X86::reloc_branch_4byte_pcrel, true,
1612 StartByte, CB, Fixups);
1613 break;
1614 }
1616 emitByte(BaseOpcode, CB);
1617 emitImmediate(MI.getOperand(CurOp++), MI.getLoc(), getImmFixupKind(TSFlags),
1618 X86II::isImmPCRel(TSFlags), StartByte, CB, Fixups);
1619 ++CurOp; // skip segment operand
1620 break;
1621 case X86II::RawFrmImm8:
1622 emitByte(BaseOpcode, CB);
1623 emitImmediate(MI.getOperand(CurOp++), MI.getLoc(), getImmFixupKind(TSFlags),
1624 X86II::isImmPCRel(TSFlags), StartByte, CB, Fixups);
1625 emitImmediate(MI.getOperand(CurOp++), MI.getLoc(), FK_Data_1, false,
1626 StartByte, CB, Fixups);
1627 break;
1628 case X86II::RawFrmImm16:
1629 emitByte(BaseOpcode, CB);
1630 emitImmediate(MI.getOperand(CurOp++), MI.getLoc(), getImmFixupKind(TSFlags),
1631 X86II::isImmPCRel(TSFlags), StartByte, CB, Fixups);
1632 emitImmediate(MI.getOperand(CurOp++), MI.getLoc(), FK_Data_2, false,
1633 StartByte, CB, Fixups);
1634 break;
1635
1636 case X86II::AddRegFrm:
1637 emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++)), CB);
1638 break;
1639
1640 case X86II::MRMDestReg: {
1641 emitByte(BaseOpcode, CB);
1642 unsigned SrcRegNum = CurOp + 1;
1643
1644 if (HasEVEX_K) // Skip writemask
1645 ++SrcRegNum;
1646
1647 if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1648 ++SrcRegNum;
1649 if (IsND) // Skip the NDD operand encoded in EVEX_VVVV
1650 ++CurOp;
1651
1652 emitRegModRMByte(MI.getOperand(CurOp),
1653 getX86RegNum(MI.getOperand(SrcRegNum)), CB);
1654 CurOp = SrcRegNum + 1;
1655 break;
1656 }
1657 case X86II::MRMDestRegCC: {
1658 unsigned FirstOp = CurOp++;
1659 unsigned SecondOp = CurOp++;
1660 unsigned CC = MI.getOperand(CurOp++).getImm();
1661 emitByte(BaseOpcode + CC, CB);
1662 emitRegModRMByte(MI.getOperand(FirstOp),
1663 getX86RegNum(MI.getOperand(SecondOp)), CB);
1664 break;
1665 }
1667 unsigned CC = MI.getOperand(8).getImm();
1668 emitByte(BaseOpcode + CC, CB);
1669 unsigned SrcRegNum = CurOp + X86::AddrNumOperands;
1670 emitMemModRMByte(MI, CurOp + 1, getX86RegNum(MI.getOperand(0)), TSFlags,
1671 Kind, StartByte, CB, Fixups, STI, false);
1672 CurOp = SrcRegNum + 3; // skip reg, VEX_V4 and CC
1673 break;
1674 }
1676 case X86II::MRMDestMem: {
1677 emitByte(BaseOpcode, CB);
1678 unsigned SrcRegNum = CurOp + X86::AddrNumOperands;
1679
1680 if (HasEVEX_K) // Skip writemask
1681 ++SrcRegNum;
1682
1683 if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1684 ++SrcRegNum;
1685
1686 if (IsND) // Skip new data destination
1687 ++CurOp;
1688
1689 bool ForceSIB = (Form == X86II::MRMDestMemFSIB);
1690 emitMemModRMByte(MI, CurOp, getX86RegNum(MI.getOperand(SrcRegNum)), TSFlags,
1691 Kind, StartByte, CB, Fixups, STI, ForceSIB);
1692 CurOp = SrcRegNum + 1;
1693 break;
1694 }
1695 case X86II::MRMDestMemCC: {
1696 unsigned MemOp = CurOp;
1697 CurOp = MemOp + X86::AddrNumOperands;
1698 unsigned RegOp = CurOp++;
1699 unsigned CC = MI.getOperand(CurOp++).getImm();
1700 emitByte(BaseOpcode + CC, CB);
1701 emitMemModRMByte(MI, MemOp, getX86RegNum(MI.getOperand(RegOp)), TSFlags,
1702 Kind, StartByte, CB, Fixups, STI);
1703 break;
1704 }
1705 case X86II::MRMSrcReg: {
1706 emitByte(BaseOpcode, CB);
1707 unsigned SrcRegNum = CurOp + 1;
1708
1709 if (HasEVEX_K) // Skip writemask
1710 ++SrcRegNum;
1711
1712 if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1713 ++SrcRegNum;
1714
1715 if (IsND) // Skip new data destination
1716 ++CurOp;
1717
1718 emitRegModRMByte(MI.getOperand(SrcRegNum),
1719 getX86RegNum(MI.getOperand(CurOp)), CB);
1720 CurOp = SrcRegNum + 1;
1721 if (HasVEX_I8Reg)
1722 I8RegNum = getX86RegEncoding(MI, CurOp++);
1723 // do not count the rounding control operand
1724 if (HasEVEX_RC)
1725 --NumOps;
1726 break;
1727 }
1728 case X86II::MRMSrcReg4VOp3: {
1729 emitByte(BaseOpcode, CB);
1730 unsigned SrcRegNum = CurOp + 1;
1731
1732 emitRegModRMByte(MI.getOperand(SrcRegNum),
1733 getX86RegNum(MI.getOperand(CurOp)), CB);
1734 CurOp = SrcRegNum + 1;
1735 ++CurOp; // Encoded in VEX.VVVV
1736 break;
1737 }
1738 case X86II::MRMSrcRegOp4: {
1739 emitByte(BaseOpcode, CB);
1740 unsigned SrcRegNum = CurOp + 1;
1741
1742 // Skip 1st src (which is encoded in VEX_VVVV)
1743 ++SrcRegNum;
1744
1745 // Capture 2nd src (which is encoded in Imm[7:4])
1746 assert(HasVEX_I8Reg && "MRMSrcRegOp4 should imply VEX_I8Reg");
1747 I8RegNum = getX86RegEncoding(MI, SrcRegNum++);
1748
1749 emitRegModRMByte(MI.getOperand(SrcRegNum),
1750 getX86RegNum(MI.getOperand(CurOp)), CB);
1751 CurOp = SrcRegNum + 1;
1752 break;
1753 }
1754 case X86II::MRMSrcRegCC: {
1755 if (IsND) // Skip new data destination
1756 ++CurOp;
1757 unsigned FirstOp = CurOp++;
1758 unsigned SecondOp = CurOp++;
1759
1760 unsigned CC = MI.getOperand(CurOp++).getImm();
1761 emitByte(BaseOpcode + CC, CB);
1762
1763 emitRegModRMByte(MI.getOperand(SecondOp),
1764 getX86RegNum(MI.getOperand(FirstOp)), CB);
1765 break;
1766 }
1768 case X86II::MRMSrcMem: {
1769 unsigned FirstMemOp = CurOp + 1;
1770
1771 if (IsND) // Skip new data destination
1772 CurOp++;
1773
1774 if (HasEVEX_K) // Skip writemask
1775 ++FirstMemOp;
1776
1777 if (HasVEX_4V)
1778 ++FirstMemOp; // Skip the register source (which is encoded in VEX_VVVV).
1779
1780 emitByte(BaseOpcode, CB);
1781
1782 bool ForceSIB = (Form == X86II::MRMSrcMemFSIB);
1783 emitMemModRMByte(MI, FirstMemOp, getX86RegNum(MI.getOperand(CurOp)),
1784 TSFlags, Kind, StartByte, CB, Fixups, STI, ForceSIB);
1785 CurOp = FirstMemOp + X86::AddrNumOperands;
1786 if (HasVEX_I8Reg)
1787 I8RegNum = getX86RegEncoding(MI, CurOp++);
1788 break;
1789 }
1790 case X86II::MRMSrcMem4VOp3: {
1791 unsigned FirstMemOp = CurOp + 1;
1792
1793 emitByte(BaseOpcode, CB);
1794
1795 emitMemModRMByte(MI, FirstMemOp, getX86RegNum(MI.getOperand(CurOp)),
1796 TSFlags, Kind, StartByte, CB, Fixups, STI);
1797 CurOp = FirstMemOp + X86::AddrNumOperands;
1798 ++CurOp; // Encoded in VEX.VVVV.
1799 break;
1800 }
1801 case X86II::MRMSrcMemOp4: {
1802 unsigned FirstMemOp = CurOp + 1;
1803
1804 ++FirstMemOp; // Skip the register source (which is encoded in VEX_VVVV).
1805
1806 // Capture second register source (encoded in Imm[7:4])
1807 assert(HasVEX_I8Reg && "MRMSrcRegOp4 should imply VEX_I8Reg");
1808 I8RegNum = getX86RegEncoding(MI, FirstMemOp++);
1809
1810 emitByte(BaseOpcode, CB);
1811
1812 emitMemModRMByte(MI, FirstMemOp, getX86RegNum(MI.getOperand(CurOp)),
1813 TSFlags, Kind, StartByte, CB, Fixups, STI);
1814 CurOp = FirstMemOp + X86::AddrNumOperands;
1815 break;
1816 }
1817 case X86II::MRMSrcMemCC: {
1818 if (IsND) // Skip new data destination
1819 ++CurOp;
1820 unsigned RegOp = CurOp++;
1821 unsigned FirstMemOp = CurOp;
1822 CurOp = FirstMemOp + X86::AddrNumOperands;
1823
1824 unsigned CC = MI.getOperand(CurOp++).getImm();
1825 emitByte(BaseOpcode + CC, CB);
1826
1827 emitMemModRMByte(MI, FirstMemOp, getX86RegNum(MI.getOperand(RegOp)),
1828 TSFlags, Kind, StartByte, CB, Fixups, STI);
1829 break;
1830 }
1831
1832 case X86II::MRMXrCC: {
1833 unsigned RegOp = CurOp++;
1834
1835 unsigned CC = MI.getOperand(CurOp++).getImm();
1836 emitByte(BaseOpcode + CC, CB);
1837 emitRegModRMByte(MI.getOperand(RegOp), 0, CB);
1838 break;
1839 }
1840
1841 case X86II::MRMXr:
1842 case X86II::MRM0r:
1843 case X86II::MRM1r:
1844 case X86II::MRM2r:
1845 case X86II::MRM3r:
1846 case X86II::MRM4r:
1847 case X86II::MRM5r:
1848 case X86II::MRM6r:
1849 case X86II::MRM7r:
1850 if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV).
1851 ++CurOp;
1852 if (HasEVEX_K) // Skip writemask
1853 ++CurOp;
1854 emitByte(BaseOpcode, CB);
1855 emitRegModRMByte(MI.getOperand(CurOp++),
1856 (Form == X86II::MRMXr) ? 0 : Form - X86II::MRM0r, CB);
1857 break;
1858 case X86II::MRMr0:
1859 emitByte(BaseOpcode, CB);
1860 emitByte(modRMByte(3, getX86RegNum(MI.getOperand(CurOp++)), 0), CB);
1861 break;
1862
1863 case X86II::MRMXmCC: {
1864 unsigned FirstMemOp = CurOp;
1865 CurOp = FirstMemOp + X86::AddrNumOperands;
1866
1867 unsigned CC = MI.getOperand(CurOp++).getImm();
1868 emitByte(BaseOpcode + CC, CB);
1869
1870 emitMemModRMByte(MI, FirstMemOp, 0, TSFlags, Kind, StartByte, CB, Fixups,
1871 STI);
1872 break;
1873 }
1874
1875 case X86II::MRMXm:
1876 case X86II::MRM0m:
1877 case X86II::MRM1m:
1878 case X86II::MRM2m:
1879 case X86II::MRM3m:
1880 case X86II::MRM4m:
1881 case X86II::MRM5m:
1882 case X86II::MRM6m:
1883 case X86II::MRM7m:
1884 if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV).
1885 ++CurOp;
1886 if (HasEVEX_K) // Skip writemask
1887 ++CurOp;
1888 emitByte(BaseOpcode, CB);
1889 emitMemModRMByte(MI, CurOp,
1890 (Form == X86II::MRMXm) ? 0 : Form - X86II::MRM0m, TSFlags,
1891 Kind, StartByte, CB, Fixups, STI);
1892 CurOp += X86::AddrNumOperands;
1893 break;
1894
1895 case X86II::MRM0X:
1896 case X86II::MRM1X:
1897 case X86II::MRM2X:
1898 case X86II::MRM3X:
1899 case X86II::MRM4X:
1900 case X86II::MRM5X:
1901 case X86II::MRM6X:
1902 case X86II::MRM7X:
1903 emitByte(BaseOpcode, CB);
1904 emitByte(0xC0 + ((Form - X86II::MRM0X) << 3), CB);
1905 break;
1906
1907 case X86II::MRM_C0:
1908 case X86II::MRM_C1:
1909 case X86II::MRM_C2:
1910 case X86II::MRM_C3:
1911 case X86II::MRM_C4:
1912 case X86II::MRM_C5:
1913 case X86II::MRM_C6:
1914 case X86II::MRM_C7:
1915 case X86II::MRM_C8:
1916 case X86II::MRM_C9:
1917 case X86II::MRM_CA:
1918 case X86II::MRM_CB:
1919 case X86II::MRM_CC:
1920 case X86II::MRM_CD:
1921 case X86II::MRM_CE:
1922 case X86II::MRM_CF:
1923 case X86II::MRM_D0:
1924 case X86II::MRM_D1:
1925 case X86II::MRM_D2:
1926 case X86II::MRM_D3:
1927 case X86II::MRM_D4:
1928 case X86II::MRM_D5:
1929 case X86II::MRM_D6:
1930 case X86II::MRM_D7:
1931 case X86II::MRM_D8:
1932 case X86II::MRM_D9:
1933 case X86II::MRM_DA:
1934 case X86II::MRM_DB:
1935 case X86II::MRM_DC:
1936 case X86II::MRM_DD:
1937 case X86II::MRM_DE:
1938 case X86II::MRM_DF:
1939 case X86II::MRM_E0:
1940 case X86II::MRM_E1:
1941 case X86II::MRM_E2:
1942 case X86II::MRM_E3:
1943 case X86II::MRM_E4:
1944 case X86II::MRM_E5:
1945 case X86II::MRM_E6:
1946 case X86II::MRM_E7:
1947 case X86II::MRM_E8:
1948 case X86II::MRM_E9:
1949 case X86II::MRM_EA:
1950 case X86II::MRM_EB:
1951 case X86II::MRM_EC:
1952 case X86II::MRM_ED:
1953 case X86II::MRM_EE:
1954 case X86II::MRM_EF:
1955 case X86II::MRM_F0:
1956 case X86II::MRM_F1:
1957 case X86II::MRM_F2:
1958 case X86II::MRM_F3:
1959 case X86II::MRM_F4:
1960 case X86II::MRM_F5:
1961 case X86II::MRM_F6:
1962 case X86II::MRM_F7:
1963 case X86II::MRM_F8:
1964 case X86II::MRM_F9:
1965 case X86II::MRM_FA:
1966 case X86II::MRM_FB:
1967 case X86II::MRM_FC:
1968 case X86II::MRM_FD:
1969 case X86II::MRM_FE:
1970 case X86II::MRM_FF:
1971 emitByte(BaseOpcode, CB);
1972 emitByte(0xC0 + Form - X86II::MRM_C0, CB);
1973 break;
1974 }
1975
1976 if (HasVEX_I8Reg) {
1977 // The last source register of a 4 operand instruction in AVX is encoded
1978 // in bits[7:4] of a immediate byte.
1979 assert(I8RegNum < 16 && "Register encoding out of range");
1980 I8RegNum <<= 4;
1981 if (CurOp != NumOps) {
1982 unsigned Val = MI.getOperand(CurOp++).getImm();
1983 assert(Val < 16 && "Immediate operand value out of range");
1984 I8RegNum |= Val;
1985 }
1986 emitImmediate(MCOperand::createImm(I8RegNum), MI.getLoc(), FK_Data_1, false,
1987 StartByte, CB, Fixups);
1988 } else {
1989 // If there is a remaining operand, it must be a trailing immediate. Emit it
1990 // according to the right size for the instruction. Some instructions
1991 // (SSE4a extrq and insertq) have two trailing immediates.
1992
1993 // Skip two trainling conditional operands encoded in EVEX prefix
1994 unsigned RemainingOps = NumOps - CurOp - 2 * HasTwoConditionalOps;
1995 // Verify that hasImm(TSFlags) matches the presence of remaining operands.
1996 // Exclude forms that emit immediates in the switch above (RawFrm and
1997 // AddCCFrm may consume a PC-relative operand; RawFrmImm8/16 and
1998 // RawFrmMemOffs always consume their immediates there).
1999 assert((!X86II::hasImm(TSFlags) || RemainingOps || Form == X86II::RawFrm ||
2000 Form == X86II::AddCCFrm || Form == X86II::RawFrmImm8 ||
2001 Form == X86II::RawFrmImm16 || Form == X86II::RawFrmMemOffs) &&
2002 "TSFlags indicates immediate but no operand provides it");
2003 while (RemainingOps) {
2004 emitImmediate(MI.getOperand(CurOp++), MI.getLoc(),
2005 getImmFixupKind(Desc.TSFlags),
2006 X86II::isImmPCRel(Desc.TSFlags), StartByte, CB, Fixups);
2007 --RemainingOps;
2008 }
2009 CurOp += 2 * HasTwoConditionalOps;
2010 }
2011
2012 if ((TSFlags & X86II::OpMapMask) == X86II::ThreeDNow)
2013 emitByte(X86II::getBaseOpcodeFor(TSFlags), CB);
2014
2015 if (CB.size() - StartByte > 15)
2016 Ctx.reportError(MI.getLoc(), "instruction length exceeds the limit of 15");
2017#ifndef NDEBUG
2018 // FIXME: Verify.
2019 if (/*!Desc.isVariadic() &&*/ CurOp != NumOps) {
2020 errs() << "Cannot encode all operands of: ";
2021 MI.dump();
2022 errs() << '\n';
2023 abort();
2024 }
2025#endif
2026}
2027
2029 MCContext &Ctx) {
2030 return new X86MCCodeEmitter(MCII, Ctx);
2031}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
#define X(NUM, ENUM, NAME)
Definition ELF.h:857
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
IRTranslator LLVM IR MI
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
#define R2(n)
if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod
This file defines the SmallVector class.
static MCFixupKind getImmFixupKind(uint64_t TSFlags)
static bool isPCRel32Branch(const MCInst &MI, const MCInstrInfo &MCII)
static GlobalOffsetTableExprKind startsWithGlobalOffsetTable(const MCExpr *Expr)
Check if this expression starts with GLOBAL_OFFSET_TABLE and if it is of the form GLOBAL_OFFSET_TABLE...
static uint8_t modRMByte(unsigned Mod, unsigned RegOpcode, unsigned RM)
static bool isDispOrCDisp8(uint64_t TSFlags, int Value, int &ImmOffset)
Determine if this immediate can fit in a disp8 or a compressed disp8 for EVEX instructions.
GlobalOffsetTableExprKind
@ GOT_Normal
@ GOT_SymDiff
static void emitConstant(uint64_t Val, unsigned Size, SmallVectorImpl< char > &CB)
static bool hasSecRelSymbolRef(const MCExpr *Expr)
Value * RHS
Binary assembler expressions.
Definition MCExpr.h:298
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition MCExpr.h:445
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:342
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition MCExpr.h:448
MCCodeEmitter - Generic instruction encoding interface.
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Context object for machine code objects.
Definition MCContext.h:83
const MCRegisterInfo * getRegisterInfo() const
Definition MCContext.h:411
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
@ SymbolRef
References to labels and assigned expressions.
Definition MCExpr.h:43
@ Binary
Binary expressions.
Definition MCExpr.h:41
ExprKind getKind() const
Definition MCExpr.h:85
SMLoc getLoc() const
Definition MCExpr.h:86
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, bool PCRel=false)
Consider bit fields if we need more flags.
Definition MCFixup.h:86
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
Describe properties that are true of each instruction in the target description file.
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition MCInstrInfo.h:89
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
int64_t getImm() const
Definition MCInst.h:84
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
bool isImm() const
Definition MCInst.h:66
bool isReg() const
Definition MCInst.h:65
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:73
const MCExpr * getExpr() const
Definition MCInst.h:118
bool isExpr() const
Definition MCInst.h:69
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
uint16_t getEncodingValue(MCRegister Reg) const
Returns the encoding for Reg.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
const Triple & getTargetTriple() const
Represent a reference to a symbol from inside an expression.
Definition MCExpr.h:190
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
Represents a location in source code.
Definition SMLoc.h:22
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
bool isX32() const
Tests whether the target is X32.
Definition Triple.h:1230
LLVM Value Representation.
Definition Value.h:75
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool hasImm(uint64_t TSFlags)
bool hasNewDataDest(uint64_t TSFlags)
int getMemoryOperandIdx(const MCInstrDesc &Desc)
bool isX86_64NonExtLowByteReg(MCRegister Reg)
@ MRM0X
MRM0X-MRM7X - Instructions that operate that have mod=11 and an opcode but ignore r/m.
@ RawFrm
Raw - This form is for instructions that don't have any operands, so they are just a fixed opcode val...
@ RawFrmDstSrc
RawFrmDstSrc - This form is for instructions that use the source index register SI/ESI/RSI with a pos...
@ EVEX
EVEX - Specifies that this instruction use EVEX form which provides syntax support up to 32 512-bit r...
@ ExplicitREX2Prefix
For instructions that require REX2 prefix even if EGPR is not used.
@ MRMSrcMemCC
MRMSrcMemCC - This form is used for instructions that use the Mod/RM byte to specify the operands and...
@ MRM_C0
MRM_XX (XX: C0-FF)- A mod/rm byte of exactly 0xXX.
@ RawFrmDst
RawFrmDst - This form is for instructions that use the destination index register DI/EDI/RDI.
@ MRMDestMem4VOp3CC
MRMDestMem4VOp3CC - This form is used for instructions that use the Mod/RM byte to specify a destinat...
@ AddCCFrm
AddCCFrm - This form is used for Jcc that encode the condition code in the lower 4 bits of the opcode...
@ T_MAP4
MAP4, MAP5, MAP6, MAP7 - Prefix after the 0x0F prefix.
@ PrefixByte
PrefixByte - This form is used for instructions that represent a prefix byte like data16 or rep.
@ MRMr0
Instructions operate on a register Reg/Opcode operand not the r/m field.
@ MRMXm
MRMXm - This form is used for instructions that use the Mod/RM byte to specify a memory source,...
@ MRM0r
MRM0r-MRM7r - Instructions that operate on a register r/m operand and use reg field to hold extended ...
@ MRMDestMemFSIB
MRMDestMem - But force to use the SIB field.
@ AddRegFrm
AddRegFrm - This form is used for instructions like 'push r32' that have their one register operand a...
@ VEX
VEX - encoding using 0xC4/0xC5.
@ RawFrmImm8
RawFrmImm8 - This is used for the ENTER instruction, which has two immediates, the first of which is ...
@ TB
TB - TwoByte - Set if this instruction has a two byte opcode, which starts with a 0x0F byte before th...
@ XOP
XOP - Opcode prefix used by XOP instructions.
@ MRMXr
MRMXr - This form is used for instructions that use the Mod/RM byte to specify a register source,...
@ MRMSrcMem4VOp3
MRMSrcMem4VOp3 - This form is used for instructions that encode operand 3 with VEX....
@ XOP8
XOP8 - Prefix to include use of imm byte.
@ MRMDestRegCC
MRMDestRegCC - This form is used for the cfcmov instructions, which use the Mod/RM byte to specify th...
@ PD
PD - Prefix code for packed double precision vector floating point operations performed in the SSE re...
@ MRMDestMem
MRMDestMem - This form is used for instructions that use the Mod/RM byte to specify a destination,...
@ MRMSrcMemFSIB
MRMSrcMem - But force to use the SIB field.
@ MRMSrcRegOp4
MRMSrcRegOp4 - This form is used for instructions that use the Mod/RM byte to specify the fourth sour...
@ MRMXrCC
MRMXCCr - This form is used for instructions that use the Mod/RM byte to specify a register source,...
@ T8
T8, TA - Prefix after the 0x0F prefix.
@ MRMDestMemCC
MRMDestMemCC - This form is used for the cfcmov instructions, which use the Mod/RM byte to specify th...
@ XOP9
XOP9 - Prefix to exclude use of imm byte.
@ MRMXmCC
MRMXm - This form is used for instructions that use the Mod/RM byte to specify a memory source,...
@ RawFrmImm16
RawFrmImm16 - This is used for CALL FAR instructions, which have two immediates, the first of which i...
@ MRMSrcReg
MRMSrcReg - This form is used for instructions that use the Mod/RM byte to specify a source,...
@ RawFrmSrc
RawFrmSrc - This form is for instructions that use the source index register SI/ESI/RSI with a possib...
@ MRMDestReg
MRMDestReg - This form is used for instructions that use the Mod/RM byte to specify a destination,...
@ MRMSrcMem
MRMSrcMem - This form is used for instructions that use the Mod/RM byte to specify a source,...
@ MRMSrcMemOp4
MRMSrcMemOp4 - This form is used for instructions that use the Mod/RM byte to specify the fourth sour...
@ Pseudo
PseudoFrm - This represents an instruction that is a pseudo instruction or one that has not been impl...
@ CD8_Scale_Shift
The scaling factor for the AVX512's 8-bit compressed displacement.
@ MRMSrcRegCC
MRMSrcRegCC - This form is used for instructions that use the Mod/RM byte to specify the operands and...
@ MRM0m
MRM0m-MRM7m - Instructions that operate on a memory r/m operand and use reg field to hold extended op...
@ ThreeDNow
ThreeDNow - This indicates that the instruction uses the wacky 0x0F 0x0F prefix for 3DNow!
@ XS
XS, XD - These prefix codes are for single and double precision scalar floating point operations perf...
@ XOPA
XOPA - Prefix to encode 0xA in VEX.MMMM of XOP instructions.
@ MRMSrcReg4VOp3
MRMSrcReg4VOp3 - This form is used for instructions that encode operand 3 with VEX....
@ RawFrmMemOffs
RawFrmMemOffs - This form is for instructions that store an absolute memory offset as an immediate wi...
bool isPseudo(uint64_t TSFlags)
bool isImmPCRel(uint64_t TSFlags)
unsigned getSizeOfImm(uint64_t TSFlags)
Decode the "size of immediate" field from the TSFlags field of the specified instruction.
bool needSIB(MCRegister BaseReg, MCRegister IndexReg, bool In64BitMode)
uint8_t getBaseOpcodeFor(uint64_t TSFlags)
bool isApxExtendedReg(MCRegister Reg)
unsigned getOperandBias(const MCInstrDesc &Desc)
Compute whether all of the def operands are repeated in the uses and therefore should be skipped.
bool isImmSigned(uint64_t TSFlags)
bool is16BitMemOperand(const MCInst &MI, unsigned Op, const MCSubtargetInfo &STI)
bool needsAddressSizeOverride(const MCInst &MI, const MCSubtargetInfo &STI, int MemoryOperand, uint64_t TSFlags)
Returns true if this instruction needs an Address-Size override prefix.
void emitPrefix(MCCodeEmitter &MCE, const MCInst &MI, SmallVectorImpl< char > &CB, const MCSubtargetInfo &STI)
@ AddrNumOperands
Definition X86BaseInfo.h:36
EncodingOfSegmentOverridePrefix getSegmentOverridePrefixForReg(MCRegister Reg)
Given a segment register, return the encoding of the segment override prefix for it.
@ IP_HAS_REPEAT_NE
Definition X86BaseInfo.h:55
@ reloc_riprel_4byte_movq_load_rex2
@ reloc_signed_4byte_relax
@ reloc_branch_4byte_pcrel
@ reloc_riprel_4byte_relax
@ reloc_riprel_4byte_relax_evex
@ reloc_riprel_4byte_relax_rex
@ reloc_global_offset_table
@ reloc_riprel_4byte_movq_load
@ reloc_riprel_4byte_relax_rex2
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
Definition SFrame.h:77
This is an optimization pass for GlobalISel generic memory operations.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
MCCodeEmitter * createX86MCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)
Op::Description Desc
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
static Lanai::Fixups FixupKind(const MCExpr *Expr)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
@ FirstLiteralRelocationKind
Definition MCFixup.h:29
@ FK_Data_8
A eight-byte fixup.
Definition MCFixup.h:37
@ FK_Data_1
A one-byte fixup.
Definition MCFixup.h:34
@ FK_Data_4
A four-byte fixup.
Definition MCFixup.h:36
@ FK_NONE
A no-op fixup.
Definition MCFixup.h:33
@ FK_SecRel_4
A four-byte section relative fixup.
Definition MCFixup.h:41
@ FK_Data_2
A two-byte fixup.
Definition MCFixup.h:35
DWARFExpression::Operation Op
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947