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