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 uint8_t firstPayload() const {
298 return ((~R) & 0x1) << 7 | ((~X) & 0x1) << 6 | ((~B) & 0x1) << 5;
299 }
300 uint8_t lastPayload() const {
301 return ((~VEX_4V) & 0xf) << 3 | VEX_L << 2 | VEX_PP;
302 }
303
304 void emit(SmallVectorImpl<char> &CB) const {
305 switch (Kind) {
306 case None:
307 return;
308 case REX:
309 emitByte(0x40 | W << 3 | R << 2 | X << 1 | B, CB);
310 return;
311 case REX2:
312 emitByte(0xD5, CB);
313 emitByte(M << 7 | R2 << 6 | X2 << 5 | B2 << 4 | W << 3 | R << 2 | X << 1 |
314 B,
315 CB);
316 return;
317 case VEX2:
318 emitByte(0xC5, CB);
319 emitByte(((~R) & 1) << 7 | lastPayload(), CB);
320 return;
321 case VEX3:
322 case XOP:
323 emitByte(Kind == VEX3 ? 0xC4 : 0x8F, CB);
324 emitByte(firstPayload() | VEX_5M, CB);
325 emitByte(W << 7 | lastPayload(), CB);
326 return;
327 case EVEX:
328 assert(VEX_5M && !(VEX_5M & 0x8) && "invalid mmm fields for EVEX!");
329 emitByte(0x62, CB);
330 emitByte(firstPayload() | ((~R2) & 0x1) << 4 | B2 << 3 | VEX_5M, CB);
331 emitByte(W << 7 | ((~VEX_4V) & 0xf) << 3 | ((~X2) & 0x1) << 2 | VEX_PP,
332 CB);
333 emitByte(EVEX_z << 7 | EVEX_L2 << 6 | VEX_L << 5 | EVEX_b << 4 |
334 ((~EVEX_V2) & 0x1) << 3 | EVEX_aaa,
335 CB);
336 return;
337 }
338 }
339};
340
341class X86MCCodeEmitter : public MCCodeEmitter {
342 const MCInstrInfo &MCII;
343 MCContext &Ctx;
344
345public:
346 X86MCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
347 : MCII(mcii), Ctx(ctx) {}
348 X86MCCodeEmitter(const X86MCCodeEmitter &) = delete;
349 X86MCCodeEmitter &operator=(const X86MCCodeEmitter &) = delete;
350 ~X86MCCodeEmitter() override = default;
351
352 void emitPrefix(const MCInst &MI, SmallVectorImpl<char> &CB,
353 const MCSubtargetInfo &STI) const;
354
355 void encodeInstruction(const MCInst &MI, SmallVectorImpl<char> &CB,
357 const MCSubtargetInfo &STI) const override;
358
359private:
360 unsigned getX86RegNum(const MCOperand &MO) const;
361
362 unsigned getX86RegEncoding(const MCInst &MI, unsigned OpNum) const;
363
364 void emitImmediate(const MCOperand &Disp, SMLoc Loc, unsigned FixupKind,
365 bool IsPCRel, uint64_t StartByte,
367 SmallVectorImpl<MCFixup> &Fixups, int ImmOffset = 0) const;
368
369 void emitRegModRMByte(const MCOperand &ModRMReg, unsigned RegOpcodeFld,
370 SmallVectorImpl<char> &CB) const;
371
372 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base,
373 SmallVectorImpl<char> &CB) const;
374
375 void emitMemModRMByte(const MCInst &MI, unsigned Op, unsigned RegOpcodeField,
376 uint64_t TSFlags, PrefixKind Kind, uint64_t StartByte,
379 const MCSubtargetInfo &STI,
380 bool ForceSIB = false) const;
381
382 PrefixKind emitPrefixImpl(const MCInst &MI, const MCInstrDesc &Desc,
383 unsigned OpBias, const MCSubtargetInfo &STI,
384 SmallVectorImpl<char> &CB) const;
385
386 PrefixKind emitVEXOpcodePrefix(int MemOperand, const MCInst &MI,
387 const MCInstrDesc &Desc, unsigned OpBias,
388 const MCSubtargetInfo &STI,
389 SmallVectorImpl<char> &CB) const;
390
391 void emitSegmentOverridePrefix(unsigned SegOperand, const MCInst &MI,
392 SmallVectorImpl<char> &CB) const;
393
394 PrefixKind emitOpcodePrefix(int MemOperand, const MCInst &MI,
395 const MCInstrDesc &Desc, unsigned OpBias,
396 const MCSubtargetInfo &STI,
397 SmallVectorImpl<char> &CB) const;
398
399 PrefixKind emitREXPrefix(int MemOperand, const MCInst &MI,
400 const MCInstrDesc &Desc, unsigned OpBias,
401 const MCSubtargetInfo &STI,
402 SmallVectorImpl<char> &CB) const;
403};
404
405} // end anonymous namespace
406
407static uint8_t modRMByte(unsigned Mod, unsigned RegOpcode, unsigned RM) {
408 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
409 return RM | (RegOpcode << 3) | (Mod << 6);
410}
411
412static void emitConstant(uint64_t Val, unsigned Size,
414 // Output the constant in little endian byte order.
415 for (unsigned i = 0; i != Size; ++i) {
416 emitByte(Val & 255, CB);
417 Val >>= 8;
418 }
419}
420
421/// \returns the appropriate fixup kind to use for an immediate in an
422/// instruction with the specified TSFlags.
424 unsigned Size = X86II::getSizeOfImm(TSFlags);
425 if (X86II::isImmSigned(TSFlags)) {
426 switch (Size) {
427 default:
428 llvm_unreachable("Unsupported signed fixup size!");
429 case 4:
431 }
432 }
433 switch (Size) {
434 default:
435 llvm_unreachable("Invalid generic fixup size!");
436 case 1:
437 return FK_Data_1;
438 case 2:
439 return FK_Data_2;
440 case 4:
441 return FK_Data_4;
442 case 8:
443 return FK_Data_8;
444 }
445}
446
448
449/// Check if this expression starts with _GLOBAL_OFFSET_TABLE_ and if it is
450/// of the form _GLOBAL_OFFSET_TABLE_-symbol. This is needed to support PIC on
451/// ELF i386 as _GLOBAL_OFFSET_TABLE_ is magical. We check only simple case that
452/// are know to be used: _GLOBAL_OFFSET_TABLE_ by itself or at the start of a
453/// binary expression.
454///
455/// TODO: Move this to X86AsmBackend.cpp at relocation decision phase so that we
456/// don't have to mess with MCExpr.
459 const MCExpr *RHS = nullptr;
460 if (Expr->getKind() == MCExpr::Binary) {
461 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Expr);
462 Expr = BE->getLHS();
463 RHS = BE->getRHS();
464 }
465
466 if (Expr->getKind() != MCExpr::SymbolRef)
467 return GOT_None;
468
469 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
470 const MCSymbol &S = Ref->getSymbol();
471 if (S.getName() != "_GLOBAL_OFFSET_TABLE_")
472 return GOT_None;
473 if (RHS && RHS->getKind() == MCExpr::SymbolRef)
474 return GOT_SymDiff;
475 return GOT_Normal;
476}
477
478static bool hasSecRelSymbolRef(const MCExpr *Expr) {
479 if (Expr->getKind() == MCExpr::SymbolRef) {
480 auto *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
481 return Ref->getSpecifier() == X86::S_COFF_SECREL;
482 }
483 return false;
484}
485
486static bool isPCRel32Branch(const MCInst &MI, const MCInstrInfo &MCII) {
487 unsigned Opcode = MI.getOpcode();
488 const MCInstrDesc &Desc = MCII.get(Opcode);
489 if ((Opcode != X86::CALL64pcrel32 && Opcode != X86::JMP_4 &&
490 Opcode != X86::JCC_4) ||
491 !(getImmFixupKind(Desc.TSFlags) == FK_Data_4 &&
492 X86II::isImmPCRel(Desc.TSFlags)))
493 return false;
494
495 unsigned CurOp = X86II::getOperandBias(Desc);
496 const MCOperand &Op = MI.getOperand(CurOp);
497 if (!Op.isExpr())
498 return false;
499
500 auto *Ref = dyn_cast<MCSymbolRefExpr>(Op.getExpr());
501 return Ref && Ref->getSpecifier() == X86::S_None;
502}
503
504unsigned X86MCCodeEmitter::getX86RegNum(const MCOperand &MO) const {
505 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg()) & 0x7;
506}
507
508unsigned X86MCCodeEmitter::getX86RegEncoding(const MCInst &MI,
509 unsigned OpNum) const {
510 return Ctx.getRegisterInfo()->getEncodingValue(MI.getOperand(OpNum).getReg());
511}
512
513void X86MCCodeEmitter::emitImmediate(const MCOperand &DispOp, SMLoc Loc,
514 unsigned FixupKind, bool PCRel,
515 uint64_t StartByte,
516 SmallVectorImpl<char> &CB,
517 SmallVectorImpl<MCFixup> &Fixups,
518 int ImmOffset) const {
519 unsigned Size = 4;
520 switch (FixupKind) {
521 case FK_Data_1:
522 Size = 1;
523 break;
524 case FK_Data_2:
525 Size = 2;
526 break;
527 case FK_Data_8:
528 Size = 8;
529 break;
530 }
531 const MCExpr *Expr = nullptr;
532 if (DispOp.isImm()) {
533 // If this is a simple integer displacement that doesn't require a
534 // relocation, emit it now.
536 PCRel)) {
537 emitConstant(DispOp.getImm() + ImmOffset, Size, CB);
538 return;
539 }
540 Expr = MCConstantExpr::create(DispOp.getImm(), Ctx);
541 } else {
542 Expr = DispOp.getExpr();
543 }
544
545 // If we have an immoffset, add it to the expression.
546 if ((FixupKind == FK_Data_4 || FixupKind == FK_Data_8 ||
549 if (Kind != GOT_None) {
550 assert(ImmOffset == 0);
551
552 if (Size == 8) {
553 FixupKind = FirstLiteralRelocationKind + ELF::R_X86_64_GOTPC64;
554 } else {
555 assert(Size == 4);
557 }
558
559 if (Kind == GOT_Normal)
560 ImmOffset = static_cast<int>(CB.size() - StartByte);
561 } else if (Expr->getKind() == MCExpr::SymbolRef) {
562 if (hasSecRelSymbolRef(Expr)) {
564 }
565 } else if (Expr->getKind() == MCExpr::Binary) {
566 const MCBinaryExpr *Bin = static_cast<const MCBinaryExpr *>(Expr);
567 if (hasSecRelSymbolRef(Bin->getLHS()) ||
568 hasSecRelSymbolRef(Bin->getRHS())) {
570 }
571 }
572 }
573
574 if (ImmOffset)
575 Expr = MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(ImmOffset, Ctx),
576 Ctx, Expr->getLoc());
577
578 // Emit a symbolic constant as a fixup and a few zero bytes.
579 Fixups.push_back(MCFixup::create(static_cast<uint32_t>(CB.size() - StartByte),
580 Expr, FixupKind, PCRel));
581 emitConstant(0, Size, CB);
582}
583
584void X86MCCodeEmitter::emitRegModRMByte(const MCOperand &ModRMReg,
585 unsigned RegOpcodeFld,
586 SmallVectorImpl<char> &CB) const {
587 emitByte(modRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)), CB);
588}
589
590void X86MCCodeEmitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base,
591 SmallVectorImpl<char> &CB) const {
592 // SIB byte is in the same format as the modRMByte.
593 emitByte(modRMByte(SS, Index, Base), CB);
594}
595
596void X86MCCodeEmitter::emitMemModRMByte(
597 const MCInst &MI, unsigned Op, unsigned RegOpcodeField, uint64_t TSFlags,
598 PrefixKind Kind, uint64_t StartByte, SmallVectorImpl<char> &CB,
599 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI,
600 bool ForceSIB) const {
601 const MCOperand &Disp = MI.getOperand(Op + X86::AddrDisp);
602 const MCOperand &Base = MI.getOperand(Op + X86::AddrBaseReg);
603 const MCOperand &Scale = MI.getOperand(Op + X86::AddrScaleAmt);
604 const MCOperand &IndexReg = MI.getOperand(Op + X86::AddrIndexReg);
605 MCRegister BaseReg = Base.getReg();
606
607 // Handle %rip relative addressing.
608 if (BaseReg == X86::RIP ||
609 BaseReg == X86::EIP) { // [disp32+rIP] in X86-64 mode
610 assert(STI.hasFeature(X86::Is64Bit) &&
611 "Rip-relative addressing requires 64-bit mode");
612 assert(!IndexReg.getReg() && !ForceSIB && "Invalid rip-relative address");
613 emitByte(modRMByte(0, RegOpcodeField, 5), CB);
614
615 unsigned Opcode = MI.getOpcode();
616 unsigned FixupKind = [&]() {
617 // Enable relaxed relocation only for a MCSymbolRefExpr. We cannot use a
618 // relaxed relocation if an offset is present (e.g. x@GOTPCREL+4).
619 if (!(Disp.isExpr() && isa<MCSymbolRefExpr>(Disp.getExpr())))
621
622 // Certain loads for GOT references can be relocated against the symbol
623 // directly if the symbol ends up in the same linkage unit.
624 switch (Opcode) {
625 default:
627 case X86::MOV64rm:
628 // movq loads is a subset of reloc_riprel_4byte_relax_rex/rex2. It is a
629 // special case because COFF and Mach-O don't support ELF's more
630 // flexible R_X86_64_REX_GOTPCRELX/R_X86_64_CODE_4_GOTPCRELX relaxation.
633 case X86::ADC32rm:
634 case X86::ADD32rm:
635 case X86::AND32rm:
636 case X86::CMP32rm:
637 case X86::MOV32rm:
638 case X86::OR32rm:
639 case X86::SBB32rm:
640 case X86::SUB32rm:
641 case X86::TEST32mr:
642 case X86::XOR32rm:
643 case X86::CALL64m:
644 case X86::JMP64m:
645 case X86::TAILJMPm64:
646 case X86::TEST64mr:
647 case X86::ADC64rm:
648 case X86::ADD64rm:
649 case X86::AND64rm:
650 case X86::CMP64rm:
651 case X86::OR64rm:
652 case X86::SBB64rm:
653 case X86::SUB64rm:
654 case X86::XOR64rm:
655 case X86::LEA64r:
659 case X86::ADD64rm_NF:
660 case X86::ADD64rm_ND:
661 case X86::ADD64mr_ND:
662 case X86::ADD64mr_NF_ND:
663 case X86::ADD64rm_NF_ND:
665 }
666 }();
667
668 // rip-relative addressing is actually relative to the *next* instruction.
669 // Since an immediate can follow the mod/rm byte for an instruction, this
670 // means that we need to bias the displacement field of the instruction with
671 // the size of the immediate field. If we have this case, add it into the
672 // expression to emit.
673 // Note: rip-relative addressing using immediate displacement values should
674 // not be adjusted, assuming it was the user's intent.
675 int ImmSize = !Disp.isImm() && X86II::hasImm(TSFlags)
676 ? X86II::getSizeOfImm(TSFlags)
677 : 0;
678
679 emitImmediate(Disp, MI.getLoc(), FixupKind, true, StartByte, CB, Fixups,
680 -ImmSize);
681 return;
682 }
683
684 unsigned BaseRegNo = BaseReg ? getX86RegNum(Base) : -1U;
685
686 bool IsAdSize16 = STI.hasFeature(X86::Is32Bit) &&
687 (TSFlags & X86II::AdSizeMask) == X86II::AdSize16;
688
689 // 16-bit addressing forms of the ModR/M byte have a different encoding for
690 // the R/M field and are far more limited in which registers can be used.
691 // 64-bit mode has no 16-bit addressing.
692 if (IsAdSize16 || (!STI.hasFeature(X86::Is64Bit) &&
694 if (BaseReg) {
695 // For 32-bit addressing, the row and column values in Table 2-2 are
696 // basically the same. It's AX/CX/DX/BX/SP/BP/SI/DI in that order, with
697 // some special cases. And getX86RegNum reflects that numbering.
698 // For 16-bit addressing it's more fun, as shown in the SDM Vol 2A,
699 // Table 2-1 "16-Bit Addressing Forms with the ModR/M byte". We can only
700 // use SI/DI/BP/BX, which have "row" values 4-7 in no particular order,
701 // while values 0-3 indicate the allowed combinations (base+index) of
702 // those: 0 for BX+SI, 1 for BX+DI, 2 for BP+SI, 3 for BP+DI.
703 //
704 // R16Table[] is a lookup from the normal RegNo, to the row values from
705 // Table 2-1 for 16-bit addressing modes. Where zero means disallowed.
706 static const unsigned R16Table[] = {0, 0, 0, 7, 0, 6, 4, 5};
707 unsigned RMfield = R16Table[BaseRegNo];
708
709 assert(RMfield && "invalid 16-bit base register");
710
711 if (IndexReg.getReg()) {
712 unsigned IndexReg16 = R16Table[getX86RegNum(IndexReg)];
713
714 assert(IndexReg16 && "invalid 16-bit index register");
715 // We must have one of SI/DI (4,5), and one of BP/BX (6,7).
716 assert(((IndexReg16 ^ RMfield) & 2) &&
717 "invalid 16-bit base/index register combination");
718 assert(Scale.getImm() == 1 &&
719 "invalid scale for 16-bit memory reference");
720
721 // Allow base/index to appear in either order (although GAS doesn't).
722 if (IndexReg16 & 2)
723 RMfield = (RMfield & 1) | ((7 - IndexReg16) << 1);
724 else
725 RMfield = (IndexReg16 & 1) | ((7 - RMfield) << 1);
726 }
727
728 if (Disp.isImm() && isInt<8>(Disp.getImm())) {
729 if (Disp.getImm() == 0 && RMfield != 6) {
730 // There is no displacement; just the register.
731 emitByte(modRMByte(0, RegOpcodeField, RMfield), CB);
732 return;
733 }
734 // Use the [REG]+disp8 form, including for [BP] which cannot be encoded.
735 emitByte(modRMByte(1, RegOpcodeField, RMfield), CB);
736 emitImmediate(Disp, MI.getLoc(), FK_Data_1, false, StartByte, CB,
737 Fixups);
738 return;
739 }
740 // This is the [REG]+disp16 case.
741 emitByte(modRMByte(2, RegOpcodeField, RMfield), CB);
742 } else {
743 assert(!IndexReg.getReg() && "Unexpected index register!");
744 // There is no BaseReg; this is the plain [disp16] case.
745 emitByte(modRMByte(0, RegOpcodeField, 6), CB);
746 }
747
748 // Emit 16-bit displacement for plain disp16 or [REG]+disp16 cases.
749 emitImmediate(Disp, MI.getLoc(), FK_Data_2, false, StartByte, CB, Fixups);
750 return;
751 }
752
753 // Check for presence of {disp8} or {disp32} pseudo prefixes.
754 bool UseDisp8 = MI.getFlags() & X86::IP_USE_DISP8;
755 bool UseDisp32 = MI.getFlags() & X86::IP_USE_DISP32;
756
757 // We only allow no displacement if no pseudo prefix is present.
758 bool AllowNoDisp = !UseDisp8 && !UseDisp32;
759 // Disp8 is allowed unless the {disp32} prefix is present.
760 bool AllowDisp8 = !UseDisp32;
761
762 // Determine whether a SIB byte is needed.
763 if (!ForceSIB && !X86II::needSIB(BaseReg, IndexReg.getReg(),
764 STI.hasFeature(X86::Is64Bit))) {
765 if (!BaseReg) { // [disp32] in X86-32 mode
766 emitByte(modRMByte(0, RegOpcodeField, 5), CB);
767 emitImmediate(Disp, MI.getLoc(), FK_Data_4, false, StartByte, CB, Fixups);
768 return;
769 }
770
771 // If the base is not EBP/ESP/R12/R13/R20/R21/R28/R29 and there is no
772 // displacement, use simple indirect register encoding, this handles
773 // addresses like [EAX]. The encoding for [EBP], [R13], [R20], [R21], [R28]
774 // or [R29] with no displacement means [disp32] so we handle it by emitting
775 // a displacement of 0 later.
776 if (BaseRegNo != N86::EBP) {
777 if (Disp.isImm() && Disp.getImm() == 0 && AllowNoDisp) {
778 emitByte(modRMByte(0, RegOpcodeField, BaseRegNo), CB);
779 return;
780 }
781
782 // If the displacement is @tlscall, treat it as a zero.
783 if (Disp.isExpr()) {
784 auto *Sym = dyn_cast<MCSymbolRefExpr>(Disp.getExpr());
785 if (Sym && Sym->getSpecifier() == X86::S_TLSCALL) {
786 // This is exclusively used by call *a@tlscall(base). The relocation
787 // (R_386_TLSCALL or R_X86_64_TLSCALL) applies to the beginning.
788 Fixups.push_back(MCFixup::create(0, Sym, FK_NONE));
789 emitByte(modRMByte(0, RegOpcodeField, BaseRegNo), CB);
790 return;
791 }
792 }
793 }
794
795 // Otherwise, if the displacement fits in a byte, encode as [REG+disp8].
796 // Including a compressed disp8 for EVEX instructions that support it.
797 // This also handles the 0 displacement for [EBP], [R13], [R21] or [R29]. We
798 // can't use disp8 if the {disp32} pseudo prefix is present.
799 if (Disp.isImm() && AllowDisp8) {
800 int ImmOffset = 0;
801 if (X86II::isDispOrCDisp8(TSFlags, Disp.getImm(), &ImmOffset)) {
802 emitByte(modRMByte(1, RegOpcodeField, BaseRegNo), CB);
803 emitConstant(Disp.getImm() + ImmOffset, 1, CB);
804 return;
805 }
806 }
807
808 // Otherwise, emit the most general non-SIB encoding: [REG+disp32].
809 // Displacement may be 0 for [EBP], [R13], [R21], [R29] case if {disp32}
810 // pseudo prefix prevented using disp8 above.
811 emitByte(modRMByte(2, RegOpcodeField, BaseRegNo), CB);
812 unsigned Opcode = MI.getOpcode();
813 unsigned FixupKind = Opcode == X86::MOV32rm ? X86::reloc_signed_4byte_relax
815 emitImmediate(Disp, MI.getLoc(), MCFixupKind(FixupKind), false, StartByte,
816 CB, Fixups);
817 return;
818 }
819
820 // We need a SIB byte, so start by outputting the ModR/M byte first
821 assert(IndexReg.getReg() != X86::ESP && IndexReg.getReg() != X86::RSP &&
822 "Cannot use ESP as index reg!");
823
824 bool ForceDisp32 = false;
825 bool ForceDisp8 = false;
826 int ImmOffset = 0;
827 if (!BaseReg) {
828 // If there is no base register, we emit the special case SIB byte with
829 // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
830 BaseRegNo = 5;
831 emitByte(modRMByte(0, RegOpcodeField, 4), CB);
832 ForceDisp32 = true;
833 } else if (Disp.isImm() && Disp.getImm() == 0 && AllowNoDisp &&
834 // Base reg can't be EBP/RBP/R13/R21/R29 as that would end up with
835 // '5' as the base field, but that is the magic [*] nomenclature
836 // that indicates no base when mod=0. For these cases we'll emit a
837 // 0 displacement instead.
838 BaseRegNo != N86::EBP) {
839 // Emit no displacement ModR/M byte
840 emitByte(modRMByte(0, RegOpcodeField, 4), CB);
841 } else if (Disp.isImm() && AllowDisp8 &&
842 X86II::isDispOrCDisp8(TSFlags, Disp.getImm(), &ImmOffset)) {
843 // Displacement fits in a byte or matches an EVEX compressed disp8, use
844 // disp8 encoding. This also handles EBP/R13/R21/R29 base with 0
845 // displacement unless {disp32} pseudo prefix was used.
846 emitByte(modRMByte(1, RegOpcodeField, 4), CB);
847 ForceDisp8 = true;
848 } else {
849 // Otherwise, emit the normal disp32 encoding.
850 emitByte(modRMByte(2, RegOpcodeField, 4), CB);
851 ForceDisp32 = true;
852 }
853
854 // Calculate what the SS field value should be...
855 static const unsigned SSTable[] = {~0U, 0, 1, ~0U, 2, ~0U, ~0U, ~0U, 3};
856 unsigned SS = SSTable[Scale.getImm()];
857
858 unsigned IndexRegNo = IndexReg.getReg() ? getX86RegNum(IndexReg) : 4;
859
860 emitSIBByte(SS, IndexRegNo, BaseRegNo, CB);
861
862 // Do we need to output a displacement?
863 if (ForceDisp8)
864 emitImmediate(Disp, MI.getLoc(), FK_Data_1, false, StartByte, CB, Fixups,
865 ImmOffset);
866 else if (ForceDisp32)
867 emitImmediate(Disp, MI.getLoc(), X86::reloc_signed_4byte, false, StartByte,
868 CB, Fixups);
869}
870
871/// Emit all instruction prefixes.
872///
873/// \returns one of the REX, XOP, VEX2, VEX3, EVEX if any of them is used,
874/// otherwise returns None.
875PrefixKind X86MCCodeEmitter::emitPrefixImpl(const MCInst &MI,
876 const MCInstrDesc &Desc,
877 unsigned OpBias,
878 const MCSubtargetInfo &STI,
879 SmallVectorImpl<char> &CB) const {
880 uint64_t TSFlags = Desc.TSFlags;
881 // Determine where the memory operand starts, if present, and emit its
882 // segment override opcode prefix as needed.
883 int MemoryOperand = X86II::getMemoryOperandNo(TSFlags);
884 if (MemoryOperand != -1) {
885 MemoryOperand += OpBias;
886 emitSegmentOverridePrefix(MemoryOperand + X86::AddrSegmentReg, MI, CB);
887 }
888
889 // Emit the repeat opcode prefix as needed.
890 unsigned Flags = MI.getFlags();
891 if (TSFlags & X86II::REP || Flags & X86::IP_HAS_REPEAT)
892 emitByte(0xF3, CB);
893 if (Flags & X86::IP_HAS_REPEAT_NE)
894 emitByte(0xF2, CB);
895
896 // Emit the address size opcode prefix as needed.
897 if (X86_MC::needsAddressSizeOverride(MI, STI, MemoryOperand, TSFlags) ||
898 Flags & X86::IP_HAS_AD_SIZE)
899 emitByte(0x67, CB);
900
901 uint64_t Form = TSFlags & X86II::FormMask;
902 switch (Form) {
903 default:
904 break;
906 // Emit segment override opcode prefix as needed (not for %ds).
907 if (MI.getOperand(2).getReg() != X86::DS)
908 emitSegmentOverridePrefix(2, MI, CB);
909 break;
910 case X86II::RawFrmSrc:
911 // Emit segment override opcode prefix as needed (not for %ds).
912 if (MI.getOperand(1).getReg() != X86::DS)
913 emitSegmentOverridePrefix(1, MI, CB);
914 break;
916 // Emit segment override opcode prefix as needed.
917 emitSegmentOverridePrefix(1, MI, CB);
918 break;
919 }
920
921 // REX prefix is optional, but if used must be immediately before the opcode
922 // Encoding type for this instruction.
923 return (TSFlags & X86II::EncodingMask)
924 ? emitVEXOpcodePrefix(MemoryOperand, MI, Desc, OpBias, STI, CB)
925 : emitOpcodePrefix(MemoryOperand, MI, Desc, OpBias, STI, CB);
926}
927
928// AVX instructions are encoded using an encoding scheme that combines
929// prefix bytes, opcode extension field, operand encoding fields, and vector
930// length encoding capability into a new prefix, referred to as VEX.
931
932// The majority of the AVX-512 family of instructions (operating on
933// 512/256/128-bit vector register operands) are encoded using a new prefix
934// (called EVEX).
935
936// XOP is a revised subset of what was originally intended as SSE5. It was
937// changed to be similar but not overlapping with AVX.
938
939/// Emit XOP, VEX2, VEX3 or EVEX prefix.
940/// \returns the used prefix.
941PrefixKind X86MCCodeEmitter::emitVEXOpcodePrefix(
942 int MemOperand, const MCInst &MI, const MCInstrDesc &Desc, unsigned OpBias,
943 const MCSubtargetInfo &STI, SmallVectorImpl<char> &CB) const {
944 uint64_t TSFlags = Desc.TSFlags;
945
946 assert(!(TSFlags & X86II::LOCK) && "Can't have LOCK VEX.");
947
948#ifndef NDEBUG
949 unsigned NumOps = MI.getNumOperands();
950 for (unsigned I = NumOps ? OpBias : 0; I != NumOps; ++I) {
951 const MCOperand &MO = MI.getOperand(I);
952 if (!MO.isReg())
953 continue;
954 MCRegister Reg = MO.getReg();
955 if (Reg == X86::AH || Reg == X86::BH || Reg == X86::CH || Reg == X86::DH)
957 "Cannot encode high byte register in VEX/EVEX-prefixed instruction");
958 }
959#endif
960
961 X86OpcodePrefixHelper Prefix(*Ctx.getRegisterInfo());
962 switch (TSFlags & X86II::EncodingMask) {
963 default:
964 break;
965 case X86II::XOP:
966 Prefix.setLowerBound(XOP);
967 break;
968 case X86II::VEX:
969 // VEX can be 2 byte or 3 byte, not determined yet if not explicit
970 Prefix.setLowerBound((MI.getFlags() & X86::IP_USE_VEX3) ? VEX3 : VEX2);
971 break;
972 case X86II::EVEX:
973 Prefix.setLowerBound(EVEX);
974 break;
975 }
976
977 Prefix.setW(TSFlags & X86II::REX_W);
978 Prefix.setNF(TSFlags & X86II::EVEX_NF);
979
980 bool HasEVEX_K = TSFlags & X86II::EVEX_K;
981 bool HasVEX_4V = TSFlags & X86II::VEX_4V;
982 bool IsND = X86II::hasNewDataDest(TSFlags); // IsND implies HasVEX_4V
983 bool HasEVEX_RC = TSFlags & X86II::EVEX_RC;
984
985 switch (TSFlags & X86II::OpMapMask) {
986 default:
987 llvm_unreachable("Invalid prefix!");
988 case X86II::TB:
989 Prefix.set5M(0x1); // 0F
990 break;
991 case X86II::T8:
992 Prefix.set5M(0x2); // 0F 38
993 break;
994 case X86II::TA:
995 Prefix.set5M(0x3); // 0F 3A
996 break;
997 case X86II::XOP8:
998 Prefix.set5M(0x8);
999 break;
1000 case X86II::XOP9:
1001 Prefix.set5M(0x9);
1002 break;
1003 case X86II::XOPA:
1004 Prefix.set5M(0xA);
1005 break;
1006 case X86II::T_MAP4:
1007 Prefix.set5M(0x4);
1008 break;
1009 case X86II::T_MAP5:
1010 Prefix.set5M(0x5);
1011 break;
1012 case X86II::T_MAP6:
1013 Prefix.set5M(0x6);
1014 break;
1015 case X86II::T_MAP7:
1016 Prefix.set5M(0x7);
1017 break;
1018 }
1019
1020 Prefix.setL(TSFlags & X86II::VEX_L);
1021 Prefix.setL2(TSFlags & X86II::EVEX_L2);
1022 switch (TSFlags & X86II::OpPrefixMask) {
1023 case X86II::PD:
1024 Prefix.setPP(0x1); // 66
1025 break;
1026 case X86II::XS:
1027 Prefix.setPP(0x2); // F3
1028 break;
1029 case X86II::XD:
1030 Prefix.setPP(0x3); // F2
1031 break;
1032 }
1033
1034 Prefix.setZ(HasEVEX_K && (TSFlags & X86II::EVEX_Z));
1035 Prefix.setEVEX_b(TSFlags & X86II::EVEX_B);
1036 Prefix.setEVEX_U(TSFlags & X86II::EVEX_U);
1037
1038 bool EncodeRC = false;
1039 uint8_t EVEX_rc = 0;
1040
1041 unsigned CurOp = OpBias;
1042 bool HasTwoConditionalOps = TSFlags & X86II::TwoConditionalOps;
1043
1044 switch (TSFlags & X86II::FormMask) {
1045 default:
1046 llvm_unreachable("Unexpected form in emitVEXOpcodePrefix!");
1048 // src1(ModR/M), MemAddr, src2(VEX_4V)
1049 Prefix.setRR2(MI, CurOp++);
1050 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1051 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1052 CurOp += X86::AddrNumOperands;
1053 Prefix.set4VV2(MI, CurOp++);
1054 break;
1055 }
1056 case X86II::MRM_C0:
1057 case X86II::RawFrm:
1058 break;
1061 case X86II::MRMDestMem: {
1062 // MRMDestMem instructions forms:
1063 // MemAddr, src1(ModR/M)
1064 // MemAddr, src1(VEX_4V), src2(ModR/M)
1065 // MemAddr, src1(ModR/M), imm8
1066 //
1067 // NDD:
1068 // dst(VEX_4V), MemAddr, src1(ModR/M)
1069 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1070 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1071 Prefix.setV2(MI, MemOperand + X86::AddrIndexReg, HasVEX_4V);
1072
1073 if (IsND)
1074 Prefix.set4VV2(MI, CurOp++);
1075
1076 CurOp += X86::AddrNumOperands;
1077
1078 if (HasEVEX_K)
1079 Prefix.setAAA(MI, CurOp++);
1080
1081 if (!IsND && HasVEX_4V)
1082 Prefix.set4VV2(MI, CurOp++);
1083
1084 Prefix.setRR2(MI, CurOp++);
1085 if (HasTwoConditionalOps) {
1086 Prefix.set4V(MI, CurOp++, /*IsImm=*/true);
1087 Prefix.setSC(MI, CurOp++);
1088 }
1089 break;
1090 }
1091 case X86II::MRMSrcMemCC:
1093 case X86II::MRMSrcMem: {
1094 // MRMSrcMem instructions forms:
1095 // src1(ModR/M), MemAddr
1096 // src1(ModR/M), src2(VEX_4V), MemAddr
1097 // src1(ModR/M), MemAddr, imm8
1098 // src1(ModR/M), MemAddr, src2(Imm[7:4])
1099 //
1100 // FMA4:
1101 // dst(ModR/M.reg), src1(VEX_4V), src2(ModR/M), src3(Imm[7:4])
1102 //
1103 // NDD:
1104 // dst(VEX_4V), src1(ModR/M), MemAddr
1105 if (IsND)
1106 Prefix.set4VV2(MI, CurOp++);
1107
1108 Prefix.setRR2(MI, CurOp++);
1109
1110 if (HasEVEX_K)
1111 Prefix.setAAA(MI, CurOp++);
1112
1113 if (!IsND && HasVEX_4V)
1114 Prefix.set4VV2(MI, CurOp++);
1115
1116 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1117 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1118 Prefix.setV2(MI, MemOperand + X86::AddrIndexReg, HasVEX_4V);
1119 CurOp += X86::AddrNumOperands;
1120 if (HasTwoConditionalOps) {
1121 Prefix.set4V(MI, CurOp++, /*IsImm=*/true);
1122 Prefix.setSC(MI, CurOp++);
1123 }
1124 break;
1125 }
1126 case X86II::MRMSrcMem4VOp3: {
1127 // Instruction format for 4VOp3:
1128 // src1(ModR/M), MemAddr, src3(VEX_4V)
1129 Prefix.setRR2(MI, CurOp++);
1130 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1131 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1132 Prefix.set4VV2(MI, CurOp + X86::AddrNumOperands);
1133 break;
1134 }
1135 case X86II::MRMSrcMemOp4: {
1136 // dst(ModR/M.reg), src1(VEX_4V), src2(Imm[7:4]), src3(ModR/M),
1137 Prefix.setR(MI, CurOp++);
1138 Prefix.set4V(MI, CurOp++);
1139 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1140 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1141 break;
1142 }
1143 case X86II::MRMXmCC:
1144 case X86II::MRM0m:
1145 case X86II::MRM1m:
1146 case X86II::MRM2m:
1147 case X86II::MRM3m:
1148 case X86II::MRM4m:
1149 case X86II::MRM5m:
1150 case X86II::MRM6m:
1151 case X86II::MRM7m: {
1152 // MRM[0-9]m instructions forms:
1153 // MemAddr
1154 // src1(VEX_4V), MemAddr
1155 if (HasVEX_4V)
1156 Prefix.set4VV2(MI, CurOp++);
1157
1158 if (HasEVEX_K)
1159 Prefix.setAAA(MI, CurOp++);
1160
1161 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1162 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1163 Prefix.setV2(MI, MemOperand + X86::AddrIndexReg, HasVEX_4V);
1164 CurOp += X86::AddrNumOperands + 1; // Skip first imm.
1165 if (HasTwoConditionalOps) {
1166 Prefix.set4V(MI, CurOp++, /*IsImm=*/true);
1167 Prefix.setSC(MI, CurOp++);
1168 }
1169 break;
1170 }
1171 case X86II::MRMSrcRegCC:
1172 case X86II::MRMSrcReg: {
1173 // MRMSrcReg instructions forms:
1174 // dst(ModR/M), src1(VEX_4V), src2(ModR/M), src3(Imm[7:4])
1175 // dst(ModR/M), src1(ModR/M)
1176 // dst(ModR/M), src1(ModR/M), imm8
1177 //
1178 // FMA4:
1179 // dst(ModR/M.reg), src1(VEX_4V), src2(Imm[7:4]), src3(ModR/M),
1180 //
1181 // NDD:
1182 // dst(VEX_4V), src1(ModR/M.reg), src2(ModR/M)
1183 if (IsND)
1184 Prefix.set4VV2(MI, CurOp++);
1185 Prefix.setRR2(MI, CurOp++);
1186
1187 if (HasEVEX_K)
1188 Prefix.setAAA(MI, CurOp++);
1189
1190 if (!IsND && HasVEX_4V)
1191 Prefix.set4VV2(MI, CurOp++);
1192
1193 Prefix.setBB2(MI, CurOp);
1194 Prefix.setX(MI, CurOp, 4);
1195 ++CurOp;
1196
1197 if (HasTwoConditionalOps) {
1198 Prefix.set4V(MI, CurOp++, /*IsImm=*/true);
1199 Prefix.setSC(MI, CurOp++);
1200 }
1201
1202 if (TSFlags & X86II::EVEX_B) {
1203 if (HasEVEX_RC) {
1204 unsigned NumOps = Desc.getNumOperands();
1205 unsigned RcOperand = NumOps - 1;
1206 assert(RcOperand >= CurOp);
1207 EVEX_rc = MI.getOperand(RcOperand).getImm();
1208 assert(EVEX_rc <= 3 && "Invalid rounding control!");
1209 }
1210 EncodeRC = true;
1211 }
1212 break;
1213 }
1214 case X86II::MRMSrcReg4VOp3: {
1215 // Instruction format for 4VOp3:
1216 // src1(ModR/M), src2(ModR/M), src3(VEX_4V)
1217 Prefix.setRR2(MI, CurOp++);
1218 Prefix.setBB2(MI, CurOp);
1219 Prefix.setX(MI, CurOp, 4);
1220 ++CurOp;
1221 Prefix.set4VV2(MI, CurOp++);
1222 break;
1223 }
1224 case X86II::MRMSrcRegOp4: {
1225 // dst(ModR/M.reg), src1(VEX_4V), src2(Imm[7:4]), src3(ModR/M),
1226 Prefix.setR(MI, CurOp++);
1227 Prefix.set4V(MI, CurOp++);
1228 // Skip second register source (encoded in Imm[7:4])
1229 ++CurOp;
1230
1231 Prefix.setB(MI, CurOp);
1232 Prefix.setX(MI, CurOp, 4);
1233 ++CurOp;
1234 break;
1235 }
1237 case X86II::MRMDestReg: {
1238 // MRMDestReg instructions forms:
1239 // dst(ModR/M), src(ModR/M)
1240 // dst(ModR/M), src(ModR/M), imm8
1241 // dst(ModR/M), src1(VEX_4V), src2(ModR/M)
1242 //
1243 // NDD:
1244 // dst(VEX_4V), src1(ModR/M), src2(ModR/M)
1245 if (IsND)
1246 Prefix.set4VV2(MI, CurOp++);
1247 Prefix.setBB2(MI, CurOp);
1248 Prefix.setX(MI, CurOp, 4);
1249 ++CurOp;
1250
1251 if (HasEVEX_K)
1252 Prefix.setAAA(MI, CurOp++);
1253
1254 if (!IsND && HasVEX_4V)
1255 Prefix.set4VV2(MI, CurOp++);
1256
1257 Prefix.setRR2(MI, CurOp++);
1258 if (HasTwoConditionalOps) {
1259 Prefix.set4V(MI, CurOp++, /*IsImm=*/true);
1260 Prefix.setSC(MI, CurOp++);
1261 }
1262 if (TSFlags & X86II::EVEX_B)
1263 EncodeRC = true;
1264 break;
1265 }
1266 case X86II::MRMr0: {
1267 // MRMr0 instructions forms:
1268 // 11:rrr:000
1269 // dst(ModR/M)
1270 Prefix.setRR2(MI, CurOp++);
1271 break;
1272 }
1273 case X86II::MRMXrCC:
1274 case X86II::MRM0r:
1275 case X86II::MRM1r:
1276 case X86II::MRM2r:
1277 case X86II::MRM3r:
1278 case X86II::MRM4r:
1279 case X86II::MRM5r:
1280 case X86II::MRM6r:
1281 case X86II::MRM7r: {
1282 // MRM0r-MRM7r instructions forms:
1283 // dst(VEX_4V), src(ModR/M), imm8
1284 if (HasVEX_4V)
1285 Prefix.set4VV2(MI, CurOp++);
1286
1287 if (HasEVEX_K)
1288 Prefix.setAAA(MI, CurOp++);
1289
1290 Prefix.setBB2(MI, CurOp);
1291 Prefix.setX(MI, CurOp, 4);
1292 ++CurOp;
1293 if (HasTwoConditionalOps) {
1294 Prefix.set4V(MI, ++CurOp, /*IsImm=*/true);
1295 Prefix.setSC(MI, ++CurOp);
1296 }
1297 break;
1298 }
1299 }
1300 if (EncodeRC) {
1301 Prefix.setL(EVEX_rc & 0x1);
1302 Prefix.setL2(EVEX_rc & 0x2);
1303 }
1304 PrefixKind Kind = Prefix.determineOptimalKind();
1305 Prefix.emit(CB);
1306 return Kind;
1307}
1308
1309/// Emit REX prefix which specifies
1310/// 1) 64-bit instructions,
1311/// 2) non-default operand size, and
1312/// 3) use of X86-64 extended registers.
1313///
1314/// \returns the used prefix (REX or None).
1315PrefixKind X86MCCodeEmitter::emitREXPrefix(int MemOperand, const MCInst &MI,
1316 const MCInstrDesc &Desc,
1317 unsigned OpBias,
1318 const MCSubtargetInfo &STI,
1319 SmallVectorImpl<char> &CB) const {
1320 if (!STI.hasFeature(X86::Is64Bit))
1321 return None;
1322 X86OpcodePrefixHelper Prefix(*Ctx.getRegisterInfo());
1323 uint64_t TSFlags = Desc.TSFlags;
1324 Prefix.setW(TSFlags & X86II::REX_W);
1325 unsigned NumOps = MI.getNumOperands();
1326 unsigned CurOp = NumOps ? OpBias : 0;
1327 // 8-bit registers only occur as the register operands visited below.
1328 bool UsesHighByteReg = false;
1329 auto RegOp = [&](unsigned OpNum) {
1330 MCRegister Reg = MI.getOperand(OpNum).getReg();
1331 if (Reg == X86::AH || Reg == X86::BH || Reg == X86::CH || Reg == X86::DH)
1332 UsesHighByteReg = true;
1333 // If it accesses SPL, BPL, SIL, or DIL, then it requires a REX prefix.
1335 Prefix.setLowerBound(REX);
1336 return OpNum;
1337 };
1338 switch (TSFlags & X86II::FormMask) {
1339 default:
1340 assert(none_of(drop_begin(MI, CurOp),
1341 [](const MCOperand &MO) { return MO.isReg(); }) &&
1342 "Unexpected form in emitREXPrefix!");
1343 break;
1344 case X86II::RawFrm:
1346 case X86II::RawFrmSrc:
1347 case X86II::RawFrmDst:
1349 break;
1350 case X86II::AddRegFrm:
1351 Prefix.setBB2(MI, RegOp(CurOp++));
1352 break;
1353 case X86II::MRMSrcReg:
1354 case X86II::MRMSrcRegCC:
1355 Prefix.setRR2(MI, RegOp(CurOp++));
1356 Prefix.setBB2(MI, RegOp(CurOp++));
1357 break;
1358 case X86II::MRMSrcMem:
1359 case X86II::MRMSrcMemCC:
1360 Prefix.setRR2(MI, RegOp(CurOp++));
1361 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1362 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1363 CurOp += X86::AddrNumOperands;
1364 break;
1365 case X86II::MRMDestReg:
1366 Prefix.setBB2(MI, RegOp(CurOp++));
1367 Prefix.setRR2(MI, RegOp(CurOp++));
1368 break;
1369 case X86II::MRMDestMem:
1370 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1371 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1372 CurOp += X86::AddrNumOperands;
1373 Prefix.setRR2(MI, RegOp(CurOp++));
1374 break;
1375 case X86II::MRMXmCC:
1376 case X86II::MRMXm:
1377 case X86II::MRM0m:
1378 case X86II::MRM1m:
1379 case X86II::MRM2m:
1380 case X86II::MRM3m:
1381 case X86II::MRM4m:
1382 case X86II::MRM5m:
1383 case X86II::MRM6m:
1384 case X86II::MRM7m:
1385 Prefix.setBB2(MI, MemOperand + X86::AddrBaseReg);
1386 Prefix.setXX2(MI, MemOperand + X86::AddrIndexReg);
1387 break;
1388 case X86II::MRMXrCC:
1389 case X86II::MRMXr:
1390 case X86II::MRM0r:
1391 case X86II::MRM1r:
1392 case X86II::MRM2r:
1393 case X86II::MRM3r:
1394 case X86II::MRM4r:
1395 case X86II::MRM5r:
1396 case X86II::MRM6r:
1397 case X86II::MRM7r:
1398 Prefix.setBB2(MI, RegOp(CurOp++));
1399 break;
1400 }
1401 if (STI.getTargetTriple().isX32()) {
1402 for (unsigned I = OpBias; I < NumOps; ++I) {
1403 const MCOperand &MO = MI.getOperand(I);
1404 if (!MO.isExpr())
1405 continue;
1406 // GOTTPOFF and TLSDESC relocations require a REX prefix to allow
1407 // linker optimizations: even if the instructions we see may not require
1408 // any prefix, they may be replaced by instructions that do. This is
1409 // handled as a special case here so that it also works for hand-written
1410 // assembly without the user needing to write REX, as with GNU as.
1411 const auto *Ref = dyn_cast<MCSymbolRefExpr>(MO.getExpr());
1412 if (Ref && (Ref->getSpecifier() == X86::S_GOTTPOFF ||
1413 Ref->getSpecifier() == X86::S_TLSDESC)) {
1414 Prefix.setLowerBound(REX);
1415 }
1416 }
1417 }
1418 if (MI.getFlags() & X86::IP_USE_REX)
1419 Prefix.setLowerBound(REX);
1421 MI.getFlags() & X86::IP_USE_REX2)
1422 Prefix.setLowerBound(REX2);
1423 Prefix.setM((TSFlags & X86II::OpMapMask) == X86II::TB);
1424 PrefixKind Kind = Prefix.determineOptimalKind();
1425 if (Kind && UsesHighByteReg)
1427 "Cannot encode high byte register in REX-prefixed instruction");
1428 Prefix.emit(CB);
1429 return Kind;
1430}
1431
1432/// Emit segment override opcode prefix as needed.
1433void X86MCCodeEmitter::emitSegmentOverridePrefix(
1434 unsigned SegOperand, const MCInst &MI, SmallVectorImpl<char> &CB) const {
1435 // Check for explicit segment override on memory operand.
1436 if (MCRegister Reg = MI.getOperand(SegOperand).getReg())
1438}
1439
1440/// Emit all instruction prefixes prior to the opcode.
1441///
1442/// \param MemOperand the operand # of the start of a memory operand if present.
1443/// If not present, it is -1.
1444///
1445/// \returns the used prefix (REX or None).
1446PrefixKind X86MCCodeEmitter::emitOpcodePrefix(int MemOperand, const MCInst &MI,
1447 const MCInstrDesc &Desc,
1448 unsigned OpBias,
1449 const MCSubtargetInfo &STI,
1450 SmallVectorImpl<char> &CB) const {
1451 uint64_t TSFlags = Desc.TSFlags;
1452
1453 // Emit the operand size opcode prefix as needed.
1454 if ((TSFlags & X86II::OpSizeMask) ==
1455 (STI.hasFeature(X86::Is16Bit) ? X86II::OpSize32 : X86II::OpSize16))
1456 emitByte(0x66, CB);
1457
1458 // Emit the LOCK opcode prefix.
1459 if (TSFlags & X86II::LOCK || MI.getFlags() & X86::IP_HAS_LOCK)
1460 emitByte(0xF0, CB);
1461
1462 // Emit the NOTRACK opcode prefix.
1463 if (TSFlags & X86II::NOTRACK || MI.getFlags() & X86::IP_HAS_NOTRACK)
1464 emitByte(0x3E, CB);
1465
1466 switch (TSFlags & X86II::OpPrefixMask) {
1467 case X86II::PD: // 66
1468 emitByte(0x66, CB);
1469 break;
1470 case X86II::XS: // F3
1471 emitByte(0xF3, CB);
1472 break;
1473 case X86II::XD: // F2
1474 emitByte(0xF2, CB);
1475 break;
1476 }
1477
1478 // Handle REX prefix.
1479 assert((STI.hasFeature(X86::Is64Bit) || !(TSFlags & X86II::REX_W)) &&
1480 "REX.W requires 64bit mode.");
1481 PrefixKind Kind = emitREXPrefix(MemOperand, MI, Desc, OpBias, STI, CB);
1482
1483 // 0x0F escape code must be emitted just before the opcode.
1484 switch (TSFlags & X86II::OpMapMask) {
1485 case X86II::TB: // Two-byte opcode map
1486 // Encoded by M bit in REX2
1487 if (Kind == REX2)
1488 break;
1489 [[fallthrough]];
1490 case X86II::T8: // 0F 38
1491 case X86II::TA: // 0F 3A
1492 case X86II::ThreeDNow: // 0F 0F, second 0F emitted by caller.
1493 emitByte(0x0F, CB);
1494 break;
1495 }
1496
1497 switch (TSFlags & X86II::OpMapMask) {
1498 case X86II::T8: // 0F 38
1499 emitByte(0x38, CB);
1500 break;
1501 case X86II::TA: // 0F 3A
1502 emitByte(0x3A, CB);
1503 break;
1504 }
1505
1506 return Kind;
1507}
1508
1509void X86MCCodeEmitter::emitPrefix(const MCInst &MI, SmallVectorImpl<char> &CB,
1510 const MCSubtargetInfo &STI) const {
1511 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1512
1513 // Pseudo instructions don't get encoded.
1514 if (X86II::isPseudo(Desc.TSFlags))
1515 return;
1516
1517 emitPrefixImpl(MI, Desc, X86II::getOperandBias(Desc), STI, CB);
1518}
1519
1521 SmallVectorImpl<char> &CB, const MCSubtargetInfo &STI) {
1522 static_cast<X86MCCodeEmitter &>(MCE).emitPrefix(MI, CB, STI);
1523}
1524
1525void X86MCCodeEmitter::encodeInstruction(const MCInst &MI,
1528 const MCSubtargetInfo &STI) const {
1529 unsigned Opcode = MI.getOpcode();
1530 const MCInstrDesc &Desc = MCII.get(Opcode);
1531 uint64_t TSFlags = Desc.TSFlags;
1532
1533 // Pseudo instructions don't get encoded.
1534 if (X86II::isPseudo(TSFlags))
1535 return;
1536
1537 unsigned NumOps = Desc.getNumOperands();
1538 unsigned CurOp = X86II::getOperandBias(Desc);
1539
1540 uint64_t StartByte = CB.size();
1541
1542 PrefixKind Kind = emitPrefixImpl(MI, Desc, CurOp, STI, CB);
1543
1544 // It uses the VEX.VVVV field?
1545 bool HasVEX_4V = TSFlags & X86II::VEX_4V;
1546 bool HasVEX_I8Reg = (TSFlags & X86II::ImmMask) == X86II::Imm8Reg;
1547
1548 // It uses the EVEX.aaa field?
1549 bool HasEVEX_K = TSFlags & X86II::EVEX_K;
1550 bool HasEVEX_RC = TSFlags & X86II::EVEX_RC;
1551
1552 // Used if a register is encoded in 7:4 of immediate.
1553 unsigned I8RegNum = 0;
1554
1555 uint8_t BaseOpcode = X86II::getBaseOpcodeFor(TSFlags);
1556
1557 if ((TSFlags & X86II::OpMapMask) == X86II::ThreeDNow)
1558 BaseOpcode = 0x0F; // Weird 3DNow! encoding.
1559
1560 unsigned OpcodeOffset = 0;
1561
1562 bool IsND = X86II::hasNewDataDest(TSFlags);
1563 bool HasTwoConditionalOps = TSFlags & X86II::TwoConditionalOps;
1564
1565 uint64_t Form = TSFlags & X86II::FormMask;
1566 switch (Form) {
1567 default:
1568 errs() << "FORM: " << Form << "\n";
1569 llvm_unreachable("Unknown FormMask value in X86MCCodeEmitter!");
1570 case X86II::Pseudo:
1571 llvm_unreachable("Pseudo instruction shouldn't be emitted");
1573 emitByte(BaseOpcode, CB);
1574 CurOp += 3; // Consume operands.
1575 break;
1576 case X86II::RawFrmSrc:
1577 emitByte(BaseOpcode, CB);
1578 CurOp += 2; // Consume operands.
1579 break;
1580 case X86II::RawFrmDst:
1581 emitByte(BaseOpcode, CB);
1582 ++CurOp; // Consume operand.
1583 break;
1584 case X86II::PrefixByte:
1585 emitByte(BaseOpcode, CB);
1586 break;
1587 case X86II::AddCCFrm: {
1588 // This will be added to the opcode in the fallthrough.
1589 OpcodeOffset = MI.getOperand(NumOps - 1).getImm();
1590 assert(OpcodeOffset < 16 && "Unexpected opcode offset!");
1591 --NumOps; // Drop the operand from the end.
1592 [[fallthrough]];
1593 case X86II::RawFrm:
1594 emitByte(BaseOpcode + OpcodeOffset, CB);
1595
1596 if (!STI.hasFeature(X86::Is64Bit) || !isPCRel32Branch(MI, MCII))
1597 break;
1598
1599 const MCOperand &Op = MI.getOperand(CurOp++);
1600 emitImmediate(Op, MI.getLoc(), X86::reloc_branch_4byte_pcrel, true,
1601 StartByte, CB, Fixups);
1602 break;
1603 }
1605 emitByte(BaseOpcode, CB);
1606 emitImmediate(MI.getOperand(CurOp++), MI.getLoc(), getImmFixupKind(TSFlags),
1607 X86II::isImmPCRel(TSFlags), StartByte, CB, Fixups);
1608 ++CurOp; // skip segment operand
1609 break;
1610 case X86II::RawFrmImm8:
1611 emitByte(BaseOpcode, CB);
1612 emitImmediate(MI.getOperand(CurOp++), MI.getLoc(), getImmFixupKind(TSFlags),
1613 X86II::isImmPCRel(TSFlags), StartByte, CB, Fixups);
1614 emitImmediate(MI.getOperand(CurOp++), MI.getLoc(), FK_Data_1, false,
1615 StartByte, CB, Fixups);
1616 break;
1617 case X86II::RawFrmImm16:
1618 emitByte(BaseOpcode, CB);
1619 emitImmediate(MI.getOperand(CurOp++), MI.getLoc(), getImmFixupKind(TSFlags),
1620 X86II::isImmPCRel(TSFlags), StartByte, CB, Fixups);
1621 emitImmediate(MI.getOperand(CurOp++), MI.getLoc(), FK_Data_2, false,
1622 StartByte, CB, Fixups);
1623 break;
1624
1625 case X86II::AddRegFrm:
1626 emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++)), CB);
1627 break;
1628
1629 case X86II::MRMDestReg: {
1630 emitByte(BaseOpcode, CB);
1631 unsigned SrcRegNum = CurOp + 1;
1632
1633 if (HasEVEX_K) // Skip writemask
1634 ++SrcRegNum;
1635
1636 if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1637 ++SrcRegNum;
1638 if (IsND) // Skip the NDD operand encoded in EVEX_VVVV
1639 ++CurOp;
1640
1641 emitRegModRMByte(MI.getOperand(CurOp),
1642 getX86RegNum(MI.getOperand(SrcRegNum)), CB);
1643 CurOp = SrcRegNum + 1;
1644 break;
1645 }
1646 case X86II::MRMDestRegCC: {
1647 unsigned FirstOp = CurOp++;
1648 unsigned SecondOp = CurOp++;
1649 unsigned CC = MI.getOperand(CurOp++).getImm();
1650 emitByte(BaseOpcode + CC, CB);
1651 emitRegModRMByte(MI.getOperand(FirstOp),
1652 getX86RegNum(MI.getOperand(SecondOp)), CB);
1653 break;
1654 }
1656 unsigned CC = MI.getOperand(8).getImm();
1657 emitByte(BaseOpcode + CC, CB);
1658 unsigned SrcRegNum = CurOp + X86::AddrNumOperands;
1659 emitMemModRMByte(MI, CurOp + 1, getX86RegNum(MI.getOperand(0)), TSFlags,
1660 Kind, StartByte, CB, Fixups, STI, false);
1661 CurOp = SrcRegNum + 3; // skip reg, VEX_V4 and CC
1662 break;
1663 }
1665 case X86II::MRMDestMem: {
1666 emitByte(BaseOpcode, CB);
1667 unsigned SrcRegNum = CurOp + X86::AddrNumOperands;
1668
1669 if (HasEVEX_K) // Skip writemask
1670 ++SrcRegNum;
1671
1672 if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1673 ++SrcRegNum;
1674
1675 if (IsND) // Skip new data destination
1676 ++CurOp;
1677
1678 bool ForceSIB = (Form == X86II::MRMDestMemFSIB);
1679 emitMemModRMByte(MI, CurOp, getX86RegNum(MI.getOperand(SrcRegNum)), TSFlags,
1680 Kind, StartByte, CB, Fixups, STI, ForceSIB);
1681 CurOp = SrcRegNum + 1;
1682 break;
1683 }
1684 case X86II::MRMDestMemCC: {
1685 unsigned MemOp = CurOp;
1686 CurOp = MemOp + X86::AddrNumOperands;
1687 unsigned RegOp = CurOp++;
1688 unsigned CC = MI.getOperand(CurOp++).getImm();
1689 emitByte(BaseOpcode + CC, CB);
1690 emitMemModRMByte(MI, MemOp, getX86RegNum(MI.getOperand(RegOp)), TSFlags,
1691 Kind, StartByte, CB, Fixups, STI);
1692 break;
1693 }
1694 case X86II::MRMSrcReg: {
1695 emitByte(BaseOpcode, CB);
1696 unsigned SrcRegNum = CurOp + 1;
1697
1698 if (HasEVEX_K) // Skip writemask
1699 ++SrcRegNum;
1700
1701 if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1702 ++SrcRegNum;
1703
1704 if (IsND) // Skip new data destination
1705 ++CurOp;
1706
1707 emitRegModRMByte(MI.getOperand(SrcRegNum),
1708 getX86RegNum(MI.getOperand(CurOp)), CB);
1709 CurOp = SrcRegNum + 1;
1710 if (HasVEX_I8Reg)
1711 I8RegNum = getX86RegEncoding(MI, CurOp++);
1712 // do not count the rounding control operand
1713 if (HasEVEX_RC)
1714 --NumOps;
1715 break;
1716 }
1717 case X86II::MRMSrcReg4VOp3: {
1718 emitByte(BaseOpcode, CB);
1719 unsigned SrcRegNum = CurOp + 1;
1720
1721 emitRegModRMByte(MI.getOperand(SrcRegNum),
1722 getX86RegNum(MI.getOperand(CurOp)), CB);
1723 CurOp = SrcRegNum + 1;
1724 ++CurOp; // Encoded in VEX.VVVV
1725 break;
1726 }
1727 case X86II::MRMSrcRegOp4: {
1728 emitByte(BaseOpcode, CB);
1729 unsigned SrcRegNum = CurOp + 1;
1730
1731 // Skip 1st src (which is encoded in VEX_VVVV)
1732 ++SrcRegNum;
1733
1734 // Capture 2nd src (which is encoded in Imm[7:4])
1735 assert(HasVEX_I8Reg && "MRMSrcRegOp4 should imply VEX_I8Reg");
1736 I8RegNum = getX86RegEncoding(MI, SrcRegNum++);
1737
1738 emitRegModRMByte(MI.getOperand(SrcRegNum),
1739 getX86RegNum(MI.getOperand(CurOp)), CB);
1740 CurOp = SrcRegNum + 1;
1741 break;
1742 }
1743 case X86II::MRMSrcRegCC: {
1744 if (IsND) // Skip new data destination
1745 ++CurOp;
1746 unsigned FirstOp = CurOp++;
1747 unsigned SecondOp = CurOp++;
1748
1749 unsigned CC = MI.getOperand(CurOp++).getImm();
1750 emitByte(BaseOpcode + CC, CB);
1751
1752 emitRegModRMByte(MI.getOperand(SecondOp),
1753 getX86RegNum(MI.getOperand(FirstOp)), CB);
1754 break;
1755 }
1757 case X86II::MRMSrcMem: {
1758 unsigned FirstMemOp = CurOp + 1;
1759
1760 if (IsND) // Skip new data destination
1761 CurOp++;
1762
1763 if (HasEVEX_K) // Skip writemask
1764 ++FirstMemOp;
1765
1766 if (HasVEX_4V)
1767 ++FirstMemOp; // Skip the register source (which is encoded in VEX_VVVV).
1768
1769 emitByte(BaseOpcode, CB);
1770
1771 bool ForceSIB = (Form == X86II::MRMSrcMemFSIB);
1772 emitMemModRMByte(MI, FirstMemOp, getX86RegNum(MI.getOperand(CurOp)),
1773 TSFlags, Kind, StartByte, CB, Fixups, STI, ForceSIB);
1774 CurOp = FirstMemOp + X86::AddrNumOperands;
1775 if (HasVEX_I8Reg)
1776 I8RegNum = getX86RegEncoding(MI, CurOp++);
1777 break;
1778 }
1779 case X86II::MRMSrcMem4VOp3: {
1780 unsigned FirstMemOp = CurOp + 1;
1781
1782 emitByte(BaseOpcode, CB);
1783
1784 emitMemModRMByte(MI, FirstMemOp, getX86RegNum(MI.getOperand(CurOp)),
1785 TSFlags, Kind, StartByte, CB, Fixups, STI);
1786 CurOp = FirstMemOp + X86::AddrNumOperands;
1787 ++CurOp; // Encoded in VEX.VVVV.
1788 break;
1789 }
1790 case X86II::MRMSrcMemOp4: {
1791 unsigned FirstMemOp = CurOp + 1;
1792
1793 ++FirstMemOp; // Skip the register source (which is encoded in VEX_VVVV).
1794
1795 // Capture second register source (encoded in Imm[7:4])
1796 assert(HasVEX_I8Reg && "MRMSrcRegOp4 should imply VEX_I8Reg");
1797 I8RegNum = getX86RegEncoding(MI, FirstMemOp++);
1798
1799 emitByte(BaseOpcode, CB);
1800
1801 emitMemModRMByte(MI, FirstMemOp, getX86RegNum(MI.getOperand(CurOp)),
1802 TSFlags, Kind, StartByte, CB, Fixups, STI);
1803 CurOp = FirstMemOp + X86::AddrNumOperands;
1804 break;
1805 }
1806 case X86II::MRMSrcMemCC: {
1807 if (IsND) // Skip new data destination
1808 ++CurOp;
1809 unsigned RegOp = CurOp++;
1810 unsigned FirstMemOp = CurOp;
1811 CurOp = FirstMemOp + X86::AddrNumOperands;
1812
1813 unsigned CC = MI.getOperand(CurOp++).getImm();
1814 emitByte(BaseOpcode + CC, CB);
1815
1816 emitMemModRMByte(MI, FirstMemOp, getX86RegNum(MI.getOperand(RegOp)),
1817 TSFlags, Kind, StartByte, CB, Fixups, STI);
1818 break;
1819 }
1820
1821 case X86II::MRMXrCC: {
1822 unsigned RegOp = CurOp++;
1823
1824 unsigned CC = MI.getOperand(CurOp++).getImm();
1825 emitByte(BaseOpcode + CC, CB);
1826 emitRegModRMByte(MI.getOperand(RegOp), 0, CB);
1827 break;
1828 }
1829
1830 case X86II::MRMXr:
1831 case X86II::MRM0r:
1832 case X86II::MRM1r:
1833 case X86II::MRM2r:
1834 case X86II::MRM3r:
1835 case X86II::MRM4r:
1836 case X86II::MRM5r:
1837 case X86II::MRM6r:
1838 case X86II::MRM7r:
1839 if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV).
1840 ++CurOp;
1841 if (HasEVEX_K) // Skip writemask
1842 ++CurOp;
1843 emitByte(BaseOpcode, CB);
1844 emitRegModRMByte(MI.getOperand(CurOp++),
1845 (Form == X86II::MRMXr) ? 0 : Form - X86II::MRM0r, CB);
1846 break;
1847 case X86II::MRMr0:
1848 emitByte(BaseOpcode, CB);
1849 emitByte(modRMByte(3, getX86RegNum(MI.getOperand(CurOp++)), 0), CB);
1850 break;
1851
1852 case X86II::MRMXmCC: {
1853 unsigned FirstMemOp = CurOp;
1854 CurOp = FirstMemOp + X86::AddrNumOperands;
1855
1856 unsigned CC = MI.getOperand(CurOp++).getImm();
1857 emitByte(BaseOpcode + CC, CB);
1858
1859 emitMemModRMByte(MI, FirstMemOp, 0, TSFlags, Kind, StartByte, CB, Fixups,
1860 STI);
1861 break;
1862 }
1863
1864 case X86II::MRMXm:
1865 case X86II::MRM0m:
1866 case X86II::MRM1m:
1867 case X86II::MRM2m:
1868 case X86II::MRM3m:
1869 case X86II::MRM4m:
1870 case X86II::MRM5m:
1871 case X86II::MRM6m:
1872 case X86II::MRM7m:
1873 if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV).
1874 ++CurOp;
1875 if (HasEVEX_K) // Skip writemask
1876 ++CurOp;
1877 emitByte(BaseOpcode, CB);
1878 emitMemModRMByte(MI, CurOp,
1879 (Form == X86II::MRMXm) ? 0 : Form - X86II::MRM0m, TSFlags,
1880 Kind, StartByte, CB, Fixups, STI);
1881 CurOp += X86::AddrNumOperands;
1882 break;
1883
1884 case X86II::MRM0X:
1885 case X86II::MRM1X:
1886 case X86II::MRM2X:
1887 case X86II::MRM3X:
1888 case X86II::MRM4X:
1889 case X86II::MRM5X:
1890 case X86II::MRM6X:
1891 case X86II::MRM7X:
1892 emitByte(BaseOpcode, CB);
1893 emitByte(0xC0 + ((Form - X86II::MRM0X) << 3), CB);
1894 break;
1895
1896 case X86II::MRM_C0:
1897 case X86II::MRM_C1:
1898 case X86II::MRM_C2:
1899 case X86II::MRM_C3:
1900 case X86II::MRM_C4:
1901 case X86II::MRM_C5:
1902 case X86II::MRM_C6:
1903 case X86II::MRM_C7:
1904 case X86II::MRM_C8:
1905 case X86II::MRM_C9:
1906 case X86II::MRM_CA:
1907 case X86II::MRM_CB:
1908 case X86II::MRM_CC:
1909 case X86II::MRM_CD:
1910 case X86II::MRM_CE:
1911 case X86II::MRM_CF:
1912 case X86II::MRM_D0:
1913 case X86II::MRM_D1:
1914 case X86II::MRM_D2:
1915 case X86II::MRM_D3:
1916 case X86II::MRM_D4:
1917 case X86II::MRM_D5:
1918 case X86II::MRM_D6:
1919 case X86II::MRM_D7:
1920 case X86II::MRM_D8:
1921 case X86II::MRM_D9:
1922 case X86II::MRM_DA:
1923 case X86II::MRM_DB:
1924 case X86II::MRM_DC:
1925 case X86II::MRM_DD:
1926 case X86II::MRM_DE:
1927 case X86II::MRM_DF:
1928 case X86II::MRM_E0:
1929 case X86II::MRM_E1:
1930 case X86II::MRM_E2:
1931 case X86II::MRM_E3:
1932 case X86II::MRM_E4:
1933 case X86II::MRM_E5:
1934 case X86II::MRM_E6:
1935 case X86II::MRM_E7:
1936 case X86II::MRM_E8:
1937 case X86II::MRM_E9:
1938 case X86II::MRM_EA:
1939 case X86II::MRM_EB:
1940 case X86II::MRM_EC:
1941 case X86II::MRM_ED:
1942 case X86II::MRM_EE:
1943 case X86II::MRM_EF:
1944 case X86II::MRM_F0:
1945 case X86II::MRM_F1:
1946 case X86II::MRM_F2:
1947 case X86II::MRM_F3:
1948 case X86II::MRM_F4:
1949 case X86II::MRM_F5:
1950 case X86II::MRM_F6:
1951 case X86II::MRM_F7:
1952 case X86II::MRM_F8:
1953 case X86II::MRM_F9:
1954 case X86II::MRM_FA:
1955 case X86II::MRM_FB:
1956 case X86II::MRM_FC:
1957 case X86II::MRM_FD:
1958 case X86II::MRM_FE:
1959 case X86II::MRM_FF:
1960 emitByte(BaseOpcode, CB);
1961 emitByte(0xC0 + Form - X86II::MRM_C0, CB);
1962 break;
1963 }
1964
1965 if (HasVEX_I8Reg) {
1966 // The last source register of a 4 operand instruction in AVX is encoded
1967 // in bits[7:4] of a immediate byte.
1968 assert(I8RegNum < 16 && "Register encoding out of range");
1969 I8RegNum <<= 4;
1970 if (CurOp != NumOps) {
1971 unsigned Val = MI.getOperand(CurOp++).getImm();
1972 assert(Val < 16 && "Immediate operand value out of range");
1973 I8RegNum |= Val;
1974 }
1975 emitImmediate(MCOperand::createImm(I8RegNum), MI.getLoc(), FK_Data_1, false,
1976 StartByte, CB, Fixups);
1977 } else {
1978 // If there is a remaining operand, it must be a trailing immediate. Emit it
1979 // according to the right size for the instruction. Some instructions
1980 // (SSE4a extrq and insertq) have two trailing immediates.
1981
1982 // Skip two trainling conditional operands encoded in EVEX prefix
1983 unsigned RemainingOps = NumOps - CurOp - 2 * HasTwoConditionalOps;
1984 // Verify that hasImm(TSFlags) matches the presence of remaining operands.
1985 // Exclude forms that emit immediates in the switch above (RawFrm and
1986 // AddCCFrm may consume a PC-relative operand; RawFrmImm8/16 and
1987 // RawFrmMemOffs always consume their immediates there).
1988 assert((!X86II::hasImm(TSFlags) || RemainingOps || Form == X86II::RawFrm ||
1989 Form == X86II::AddCCFrm || Form == X86II::RawFrmImm8 ||
1990 Form == X86II::RawFrmImm16 || Form == X86II::RawFrmMemOffs) &&
1991 "TSFlags indicates immediate but no operand provides it");
1992 while (RemainingOps) {
1993 emitImmediate(MI.getOperand(CurOp++), MI.getLoc(),
1994 getImmFixupKind(Desc.TSFlags),
1995 X86II::isImmPCRel(Desc.TSFlags), StartByte, CB, Fixups);
1996 --RemainingOps;
1997 }
1998 CurOp += 2 * HasTwoConditionalOps;
1999 }
2000
2001 if ((TSFlags & X86II::OpMapMask) == X86II::ThreeDNow)
2002 emitByte(X86II::getBaseOpcodeFor(TSFlags), CB);
2003
2004 if (CB.size() - StartByte > 15)
2005 Ctx.reportError(MI.getLoc(), "instruction length exceeds the limit of 15");
2006#ifndef NDEBUG
2007 // FIXME: Verify.
2008 if (/*!Desc.isVariadic() &&*/ CurOp != NumOps) {
2009 errs() << "Cannot encode all operands of: ";
2010 MI.dump();
2011 errs() << '\n';
2012 abort();
2013 }
2014#endif
2015}
2016
2018 MCContext &Ctx) {
2019 return new X86MCCodeEmitter(MCII, Ctx);
2020}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
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
RISCVCC::CondCode CC
MachineRegisterInfo & MRI
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)
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:295
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition MCExpr.h:442
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:339
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition MCExpr.h:445
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:84
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:31
@ SymbolRef
References to labels and assigned expressions.
Definition MCExpr.h:40
@ Binary
Binary expressions.
Definition MCExpr.h:38
ExprKind getKind() const
Definition MCExpr.h:82
SMLoc getLoc() const
Definition MCExpr.h:83
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:90
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:187
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:39
int64_t getImm() const
Definition MCInst.h:83
static MCOperand createImm(int64_t Val)
Definition MCInst.h:144
bool isImm() const
Definition MCInst.h:65
bool isReg() const
Definition MCInst.h:64
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:72
const MCExpr * getExpr() const
Definition MCInst.h:117
bool isExpr() const
Definition MCInst.h:68
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 and target specific virtual registers.
Definition MCRegister.h:42
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:187
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:1246
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isDispOrCDisp8(uint64_t TSFlags, int64_t Value, int *ImmOffset=nullptr)
Determine if this immediate can fit in a disp8 or a compressed disp8 for EVEX instructions.
bool hasImm(uint64_t TSFlags)
bool hasNewDataDest(uint64_t TSFlags)
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...
@ 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)
int getMemoryOperandNo(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:37
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:56
@ 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.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
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
static Lanai::Fixups FixupKind(const MCExpr *Expr)
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1769
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
@ FK_Data_8
A eight-byte fixup.
Definition MCFixup.h:41
@ FK_Data_1
A one-byte fixup.
Definition MCFixup.h:38
@ FK_Data_4
A four-byte fixup.
Definition MCFixup.h:40
@ FK_NONE
A no-op fixup.
Definition MCFixup.h:37
@ FK_SecRel_4
A four-byte section relative fixup.
Definition MCFixup.h:45
@ FK_Data_2
A two-byte fixup.
Definition MCFixup.h:39
DWARFExpression::Operation Op
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
constexpr MCFixupKind FirstLiteralRelocationKind
Definition MCFixup.h:32