/src/keystone/llvm/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp
Line | Count | Source |
1 | | //===-- X86MCCodeEmitter.cpp - Convert X86 code to machine code -----------===// |
2 | | // |
3 | | // The LLVM Compiler Infrastructure |
4 | | // |
5 | | // This file is distributed under the University of Illinois Open Source |
6 | | // License. See LICENSE.TXT for details. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | // |
10 | | // This file implements the X86MCCodeEmitter class. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "MCTargetDesc/X86MCTargetDesc.h" |
15 | | #include "MCTargetDesc/X86BaseInfo.h" |
16 | | #include "MCTargetDesc/X86FixupKinds.h" |
17 | | #include "llvm/MC/MCCodeEmitter.h" |
18 | | #include "llvm/MC/MCContext.h" |
19 | | #include "llvm/MC/MCExpr.h" |
20 | | #include "llvm/MC/MCInst.h" |
21 | | #include "llvm/MC/MCInstrInfo.h" |
22 | | #include "llvm/MC/MCRegisterInfo.h" |
23 | | #include "llvm/MC/MCSubtargetInfo.h" |
24 | | #include "llvm/MC/MCSymbol.h" |
25 | | #include "llvm/Support/raw_ostream.h" |
26 | | |
27 | | #include <keystone/keystone.h> |
28 | | |
29 | | using namespace llvm_ks; |
30 | | |
31 | | #define DEBUG_TYPE "mccodeemitter" |
32 | | |
33 | | namespace { |
34 | | class X86MCCodeEmitter : public MCCodeEmitter { |
35 | | X86MCCodeEmitter(const X86MCCodeEmitter &) = delete; |
36 | | void operator=(const X86MCCodeEmitter &) = delete; |
37 | | const MCInstrInfo &MCII; |
38 | | MCContext &Ctx; |
39 | | public: |
40 | | X86MCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx) |
41 | 16.6k | : MCII(mcii), Ctx(ctx) { |
42 | 16.6k | } |
43 | | |
44 | 0 | ~X86MCCodeEmitter() override {} |
45 | | |
46 | 181k | bool is64BitMode(const MCSubtargetInfo &STI) const { |
47 | 181k | return STI.getFeatureBits()[X86::Mode64Bit]; |
48 | 181k | } |
49 | | |
50 | 103k | bool is32BitMode(const MCSubtargetInfo &STI) const { |
51 | 103k | return STI.getFeatureBits()[X86::Mode32Bit]; |
52 | 103k | } |
53 | | |
54 | 141k | bool is16BitMode(const MCSubtargetInfo &STI) const { |
55 | 141k | return STI.getFeatureBits()[X86::Mode16Bit]; |
56 | 141k | } |
57 | | |
58 | | /// Is16BitMemOperand - Return true if the specified instruction has |
59 | | /// a 16-bit memory operand. Op specifies the operand # of the memoperand. |
60 | | bool Is16BitMemOperand(const MCInst &MI, unsigned Op, |
61 | 19.0k | const MCSubtargetInfo &STI) const { |
62 | 19.0k | const MCOperand &BaseReg = MI.getOperand(Op+X86::AddrBaseReg); |
63 | 19.0k | const MCOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg); |
64 | 19.0k | const MCOperand &Disp = MI.getOperand(Op+X86::AddrDisp); |
65 | | |
66 | 19.0k | if (is16BitMode(STI) && BaseReg.getReg() == 0 && |
67 | 2.36k | Disp.isImm() && Disp.getImm() < 0x10000) |
68 | 340 | return true; |
69 | 18.7k | if ((BaseReg.getReg() != 0 && |
70 | 2.48k | X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg.getReg())) || |
71 | 18.6k | (IndexReg.getReg() != 0 && |
72 | 981 | X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg.getReg()))) |
73 | 105 | return true; |
74 | 18.6k | return false; |
75 | 18.7k | } |
76 | | |
77 | 2.98k | unsigned GetX86RegNum(const MCOperand &MO) const { |
78 | 2.98k | return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg()) & 0x7; |
79 | 2.98k | } |
80 | | |
81 | | // On regular x86, both XMM0-XMM7 and XMM8-XMM15 are encoded in the range |
82 | | // 0-7 and the difference between the 2 groups is given by the REX prefix. |
83 | | // In the VEX prefix, registers are seen sequencially from 0-15 and encoded |
84 | | // in 1's complement form, example: |
85 | | // |
86 | | // ModRM field => XMM9 => 1 |
87 | | // VEX.VVVV => XMM9 => ~9 |
88 | | // |
89 | | // See table 4-35 of Intel AVX Programming Reference for details. |
90 | | unsigned char getVEXRegisterEncoding(const MCInst &MI, |
91 | 0 | unsigned OpNum) const { |
92 | 0 | unsigned SrcReg = MI.getOperand(OpNum).getReg(); |
93 | 0 | unsigned SrcRegNum = GetX86RegNum(MI.getOperand(OpNum)); |
94 | 0 | if (X86II::isX86_64ExtendedReg(SrcReg)) |
95 | 0 | SrcRegNum |= 8; |
96 | | |
97 | | // The registers represented through VEX_VVVV should |
98 | | // be encoded in 1's complement form. |
99 | 0 | return (~SrcRegNum) & 0xf; |
100 | 0 | } |
101 | | |
102 | | unsigned char getWriteMaskRegisterEncoding(const MCInst &MI, |
103 | 0 | unsigned OpNum) const { |
104 | 0 | assert(X86::K0 != MI.getOperand(OpNum).getReg() && |
105 | 0 | "Invalid mask register as write-mask!"); |
106 | 0 | unsigned MaskRegNum = GetX86RegNum(MI.getOperand(OpNum)); |
107 | 0 | return MaskRegNum; |
108 | 0 | } |
109 | | |
110 | 175k | void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const { |
111 | 175k | OS << (char)C; |
112 | 175k | ++CurByte; |
113 | 175k | } |
114 | | |
115 | | void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte, |
116 | 33.8k | raw_ostream &OS) const { |
117 | | // Output the constant in little endian byte order. |
118 | 122k | for (unsigned i = 0; i != Size; ++i) { |
119 | 88.4k | EmitByte(Val & 255, CurByte, OS); |
120 | 88.4k | Val >>= 8; |
121 | 88.4k | } |
122 | 33.8k | } |
123 | | |
124 | | void EmitImmediate(const MCInst &MI, const MCOperand &Disp, SMLoc Loc, |
125 | | unsigned ImmSize, MCFixupKind FixupKind, |
126 | | unsigned &CurByte, raw_ostream &OS, |
127 | | SmallVectorImpl<MCFixup> &Fixups, |
128 | | unsigned int &KsError, |
129 | | bool is64bit, |
130 | | int ImmOffset = 0, bool RIP_rel = false) const; |
131 | | |
132 | | inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode, |
133 | 11.4k | unsigned RM) { |
134 | 11.4k | assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!"); |
135 | 11.4k | return RM | (RegOpcode << 3) | (Mod << 6); |
136 | 11.4k | } |
137 | | |
138 | | void EmitRegModRMByte(const MCOperand &ModRMReg, unsigned RegOpcodeFld, |
139 | 351 | unsigned &CurByte, raw_ostream &OS) const { |
140 | 351 | EmitByte(ModRMByte(3, RegOpcodeFld, GetX86RegNum(ModRMReg)), CurByte, OS); |
141 | 351 | } |
142 | | |
143 | | void EmitSIBByte(unsigned SS, unsigned Index, unsigned Base, |
144 | 1.56k | unsigned &CurByte, raw_ostream &OS) const { |
145 | | // SIB byte is in the same format as the ModRMByte. |
146 | 1.56k | EmitByte(ModRMByte(SS, Index, Base), CurByte, OS); |
147 | 1.56k | } |
148 | | |
149 | | |
150 | | void EmitMemModRMByte(const MCInst &MI, unsigned Op, |
151 | | unsigned RegOpcodeField, |
152 | | uint64_t TSFlags, unsigned &CurByte, raw_ostream &OS, |
153 | | SmallVectorImpl<MCFixup> &Fixups, |
154 | | const MCSubtargetInfo &STI) const; |
155 | | |
156 | | void encodeInstruction(MCInst &MI, raw_ostream &OS, |
157 | | SmallVectorImpl<MCFixup> &Fixups, |
158 | | const MCSubtargetInfo &STI, unsigned int &KsError) const override; |
159 | | |
160 | | bool EmitVEXOpcodePrefix(uint64_t TSFlags, unsigned &CurByte, int MemOperand, |
161 | | const MCInst &MI, const MCInstrDesc &Desc, |
162 | | raw_ostream &OS) const; |
163 | | |
164 | | bool EmitSegmentOverridePrefix(unsigned &CurByte, unsigned SegOperand, |
165 | | const MCInst &MI, raw_ostream &OS, int BaseReg = 0) const; |
166 | | |
167 | | void EmitOpcodePrefix(uint64_t TSFlags, unsigned &CurByte, int MemOperand, |
168 | | const MCInst &MI, const MCInstrDesc &Desc, |
169 | | const MCSubtargetInfo &STI, |
170 | | raw_ostream &OS) const; |
171 | | }; |
172 | | |
173 | | } // end anonymous namespace |
174 | | |
175 | | MCCodeEmitter *llvm_ks::createX86MCCodeEmitter(const MCInstrInfo &MCII, |
176 | | const MCRegisterInfo &MRI, |
177 | 16.6k | MCContext &Ctx) { |
178 | 16.6k | return new X86MCCodeEmitter(MCII, Ctx); |
179 | 16.6k | } |
180 | | |
181 | | /// isDisp8 - Return true if this signed displacement fits in a 8-bit |
182 | | /// sign-extended field. |
183 | 669 | static bool isDisp8(int Value) { |
184 | 669 | return Value == (signed char)Value; |
185 | 669 | } |
186 | | |
187 | | /// isCDisp8 - Return true if this signed displacement fits in a 8-bit |
188 | | /// compressed dispacement field. |
189 | 0 | static bool isCDisp8(uint64_t TSFlags, int Value, int& CValue) { |
190 | 0 | assert(((TSFlags & X86II::EncodingMask) == X86II::EVEX) && |
191 | 0 | "Compressed 8-bit displacement is only valid for EVEX inst."); |
192 | | |
193 | 0 | unsigned CD8_Scale = |
194 | 0 | (TSFlags & X86II::CD8_Scale_Mask) >> X86II::CD8_Scale_Shift; |
195 | 0 | if (CD8_Scale == 0) { |
196 | 0 | CValue = Value; |
197 | 0 | return isDisp8(Value); |
198 | 0 | } |
199 | | |
200 | 0 | unsigned Mask = CD8_Scale - 1; |
201 | 0 | assert((CD8_Scale & Mask) == 0 && "Invalid memory object size."); |
202 | 0 | if (Value & Mask) // Unaligned offset |
203 | 0 | return false; |
204 | 0 | Value /= (int)CD8_Scale; |
205 | 0 | bool Ret = (Value == (signed char)Value); |
206 | |
|
207 | 0 | if (Ret) |
208 | 0 | CValue = Value; |
209 | 0 | return Ret; |
210 | 0 | } |
211 | | |
212 | | /// getImmFixupKind - Return the appropriate fixup kind to use for an immediate |
213 | | /// in an instruction with the specified TSFlags. |
214 | 24.7k | static MCFixupKind getImmFixupKind(uint64_t TSFlags) { |
215 | 24.7k | unsigned Size = X86II::getSizeOfImm(TSFlags); |
216 | 24.7k | bool isPCRel = X86II::isImmPCRel(TSFlags); |
217 | | |
218 | 24.7k | if (X86II::isImmSigned(TSFlags)) { |
219 | 1 | switch (Size) { |
220 | 0 | default: llvm_unreachable("Unsupported signed fixup size!"); |
221 | 1 | case 4: return MCFixupKind(X86::reloc_signed_4byte); |
222 | 1 | } |
223 | 1 | } |
224 | 24.7k | return MCFixup::getKindForSize(Size, isPCRel); |
225 | 24.7k | } |
226 | | |
227 | | /// Is32BitMemOperand - Return true if the specified instruction has |
228 | | /// a 32-bit memory operand. Op specifies the operand # of the memoperand. |
229 | 1.52k | static bool Is32BitMemOperand(const MCInst &MI, unsigned Op) { |
230 | 1.52k | const MCOperand &BaseReg = MI.getOperand(Op+X86::AddrBaseReg); |
231 | 1.52k | const MCOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg); |
232 | | |
233 | 1.52k | if ((BaseReg.getReg() != 0 && |
234 | 527 | X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg.getReg())) || |
235 | 1.52k | (IndexReg.getReg() != 0 && |
236 | 179 | X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg.getReg()))) |
237 | 0 | return true; |
238 | 1.52k | return false; |
239 | 1.52k | } |
240 | | |
241 | | /// Is64BitMemOperand - Return true if the specified instruction has |
242 | | /// a 64-bit memory operand. Op specifies the operand # of the memoperand. |
243 | 8.04k | static bool Is64BitMemOperand(const MCInst &MI, unsigned Op) { |
244 | 8.04k | const MCOperand &BaseReg = MI.getOperand(Op+X86::AddrBaseReg); |
245 | 8.04k | const MCOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg); |
246 | | |
247 | 8.04k | if ((BaseReg.getReg() != 0 && |
248 | 716 | X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg.getReg())) || |
249 | 8.04k | (IndexReg.getReg() != 0 && |
250 | 342 | X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg.getReg()))) |
251 | 0 | return true; |
252 | 8.04k | return false; |
253 | 8.04k | } |
254 | | |
255 | | /// StartsWithGlobalOffsetTable - Check if this expression starts with |
256 | | /// _GLOBAL_OFFSET_TABLE_ and if it is of the form |
257 | | /// _GLOBAL_OFFSET_TABLE_-symbol. This is needed to support PIC on ELF |
258 | | /// i386 as _GLOBAL_OFFSET_TABLE_ is magical. We check only simple case that |
259 | | /// are know to be used: _GLOBAL_OFFSET_TABLE_ by itself or at the start |
260 | | /// of a binary expression. |
261 | | enum GlobalOffsetTableExprKind { |
262 | | GOT_None, |
263 | | GOT_Normal, |
264 | | GOT_SymDiff |
265 | | }; |
266 | | static GlobalOffsetTableExprKind |
267 | 7.94k | StartsWithGlobalOffsetTable(const MCExpr *Expr) { |
268 | 7.94k | const MCExpr *RHS = nullptr; |
269 | 7.94k | if (Expr->getKind() == MCExpr::Binary) { |
270 | 194 | const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Expr); |
271 | 194 | Expr = BE->getLHS(); |
272 | 194 | RHS = BE->getRHS(); |
273 | 194 | } |
274 | | |
275 | 7.94k | if (Expr->getKind() != MCExpr::SymbolRef) |
276 | 251 | return GOT_None; |
277 | | |
278 | 7.69k | const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Expr); |
279 | 7.69k | const MCSymbol &S = Ref->getSymbol(); |
280 | 7.69k | if (S.getName() != "_GLOBAL_OFFSET_TABLE_") |
281 | 7.69k | return GOT_None; |
282 | 0 | if (RHS && RHS->getKind() == MCExpr::SymbolRef) |
283 | 0 | return GOT_SymDiff; |
284 | 0 | return GOT_Normal; |
285 | 0 | } |
286 | | |
287 | 7.97k | static bool HasSecRelSymbolRef(const MCExpr *Expr) { |
288 | 7.97k | if (Expr->getKind() == MCExpr::SymbolRef) { |
289 | 7.77k | const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Expr); |
290 | 7.77k | return Ref->getKind() == MCSymbolRefExpr::VK_SECREL; |
291 | 7.77k | } |
292 | 205 | return false; |
293 | 7.97k | } |
294 | | |
295 | | // return false if Imm value is invalid for a given size |
296 | | static bool validImmRange(int64_t Imm, unsigned int Size) |
297 | 18.6k | { |
298 | 18.6k | if (Imm > 0 && Imm > 0xffffffff) |
299 | 52 | return false; |
300 | | |
301 | 18.6k | return true; |
302 | 18.6k | } |
303 | | |
304 | | void X86MCCodeEmitter:: |
305 | | EmitImmediate(const MCInst &MI, const MCOperand &DispOp, SMLoc Loc, unsigned Size, |
306 | | MCFixupKind FixupKind, unsigned &CurByte, raw_ostream &OS, |
307 | | SmallVectorImpl<MCFixup> &Fixups, |
308 | | unsigned int &KsError, bool is64bit, |
309 | | int ImmOffset, bool RIP_rel) const |
310 | 33.9k | { |
311 | 33.9k | KsError = 0; |
312 | 33.9k | const MCExpr *Expr = nullptr; |
313 | 33.9k | if (DispOp.isImm()) { |
314 | 22.1k | if (!is64bit && !validImmRange(DispOp.getImm(), Size)) { |
315 | 52 | KsError = KS_ERR_ASM_INVALIDOPERAND; |
316 | 52 | return; |
317 | 52 | } |
318 | | // If this is a simple integer displacement that doesn't require a |
319 | | // relocation, emit it now. |
320 | 22.0k | if (FixupKind != FK_PCRel_1 && |
321 | 10.6k | FixupKind != FK_PCRel_2 && |
322 | 10.6k | FixupKind != FK_PCRel_4 && |
323 | 2.21k | FixupKind != FK_PCRel_4 && |
324 | 2.21k | (FixupKind != MCFixupKind(X86::reloc_riprel_4byte) || !RIP_rel)) { |
325 | 2.12k | EmitConstant(DispOp.getImm(), Size, CurByte, OS); |
326 | 2.12k | return; |
327 | 2.12k | } |
328 | 19.9k | Expr = MCConstantExpr::create(DispOp.getImm(), Ctx); |
329 | 19.9k | } else { |
330 | 11.8k | Expr = DispOp.getExpr(); |
331 | 11.8k | } |
332 | | |
333 | | // If we have an immoffset, add it to the expression. |
334 | 31.7k | if ((FixupKind == FK_Data_4 || |
335 | 24.7k | FixupKind == FK_Data_8 || |
336 | 24.7k | FixupKind == MCFixupKind(X86::reloc_signed_4byte))) { |
337 | 7.94k | GlobalOffsetTableExprKind Kind = StartsWithGlobalOffsetTable(Expr); |
338 | 7.94k | if (Kind != GOT_None) { |
339 | 0 | assert(ImmOffset == 0); |
340 | | |
341 | 0 | if (Size == 8) { |
342 | 0 | FixupKind = MCFixupKind(X86::reloc_global_offset_table8); |
343 | 0 | } else { |
344 | 0 | assert(Size == 4); |
345 | 0 | FixupKind = MCFixupKind(X86::reloc_global_offset_table); |
346 | 0 | } |
347 | | |
348 | 0 | if (Kind == GOT_Normal) |
349 | 0 | ImmOffset = CurByte; |
350 | 7.94k | } else if (Expr->getKind() == MCExpr::SymbolRef) { |
351 | 7.58k | if (HasSecRelSymbolRef(Expr)) { |
352 | 0 | FixupKind = MCFixupKind(FK_SecRel_4); |
353 | 0 | } |
354 | 7.58k | } else if (Expr->getKind() == MCExpr::Binary) { |
355 | 194 | const MCBinaryExpr *Bin = static_cast<const MCBinaryExpr*>(Expr); |
356 | 194 | if (HasSecRelSymbolRef(Bin->getLHS()) |
357 | 194 | || HasSecRelSymbolRef(Bin->getRHS())) { |
358 | 0 | FixupKind = MCFixupKind(FK_SecRel_4); |
359 | 0 | } |
360 | 194 | } |
361 | 7.94k | } |
362 | | |
363 | | // If the fixup is pc-relative, we need to bias the value to be relative to |
364 | | // the start of the field, not the end of the field. |
365 | 31.7k | if (FixupKind == FK_PCRel_4 || |
366 | 22.4k | FixupKind == MCFixupKind(X86::reloc_riprel_4byte) || |
367 | 22.3k | FixupKind == MCFixupKind(X86::reloc_riprel_4byte_movq_load)) |
368 | 9.40k | ImmOffset -= 4; |
369 | 31.7k | if (FixupKind == FK_PCRel_2) |
370 | 363 | ImmOffset -= 2; |
371 | 31.7k | if (FixupKind == FK_PCRel_1) |
372 | 13.3k | ImmOffset -= 1; |
373 | | |
374 | 31.7k | if (ImmOffset) |
375 | 23.0k | Expr = MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(ImmOffset, Ctx), |
376 | 23.0k | Ctx); |
377 | | |
378 | | // Emit a symbolic constant as a fixup and 4 zeros. |
379 | 31.7k | Fixups.push_back(MCFixup::create(CurByte, Expr, FixupKind, Loc)); |
380 | 31.7k | EmitConstant(0, Size, CurByte, OS); |
381 | 31.7k | } |
382 | | |
383 | 188 | #define ABS_SUB(a, b) (a < b? b - a: a - b) |
384 | | void X86MCCodeEmitter::EmitMemModRMByte(const MCInst &MI, unsigned Op, |
385 | | unsigned RegOpcodeField, |
386 | | uint64_t TSFlags, unsigned &CurByte, |
387 | | raw_ostream &OS, |
388 | | SmallVectorImpl<MCFixup> &Fixups, |
389 | | const MCSubtargetInfo &STI) const |
390 | 9.56k | { |
391 | 9.56k | const MCOperand &Disp = MI.getOperand(Op+X86::AddrDisp); |
392 | 9.56k | const MCOperand &Base = MI.getOperand(Op+X86::AddrBaseReg); |
393 | 9.56k | const MCOperand &Scale = MI.getOperand(Op+X86::AddrScaleAmt); |
394 | 9.56k | const MCOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg); |
395 | 9.56k | unsigned BaseReg = Base.getReg(); |
396 | 9.56k | bool HasEVEX = (TSFlags & X86II::EncodingMask) == X86II::EVEX; |
397 | 9.56k | unsigned int KsError; |
398 | 9.56k | bool RIP_rel = false; |
399 | | |
400 | | // do we need x64 RIP relative encoding? |
401 | 9.56k | if (BaseReg == 0 && is64BitMode(STI) && IndexReg.getReg() == 0 && Disp.isImm()) { |
402 | 188 | if (ABS_SUB(MI.getAddress(), (uint64_t)Disp.getImm()) < 2 * (1UL << 30)) |
403 | 92 | RIP_rel = true; |
404 | 188 | } |
405 | | |
406 | | // Handle %rip relative addressing. |
407 | 9.56k | if (RIP_rel || BaseReg == X86::RIP) { // [disp32+RIP] in X86-64 mode |
408 | 92 | assert(is64BitMode(STI) && "Rip-relative addressing requires 64-bit mode"); |
409 | 92 | assert(IndexReg.getReg() == 0 && "Invalid rip-relative address"); |
410 | 92 | EmitByte(ModRMByte(0, RegOpcodeField, 5), CurByte, OS); |
411 | | |
412 | 92 | unsigned FixupKind = X86::reloc_riprel_4byte; |
413 | | |
414 | | // movq loads are handled with a special relocation form which allows the |
415 | | // linker to eliminate some loads for GOT references which end up in the |
416 | | // same linkage unit. |
417 | 92 | if (MI.getOpcode() == X86::MOV64rm) |
418 | 0 | FixupKind = X86::reloc_riprel_4byte_movq_load; |
419 | | |
420 | | // rip-relative addressing is actually relative to the *next* instruction. |
421 | | // Since an immediate can follow the mod/rm byte for an instruction, this |
422 | | // means that we need to bias the immediate field of the instruction with |
423 | | // the size of the immediate field. If we have this case, add it into the |
424 | | // expression to emit. |
425 | 92 | int ImmSize = X86II::hasImm(TSFlags) ? X86II::getSizeOfImm(TSFlags) : 0; |
426 | | |
427 | 92 | EmitImmediate(MI, Disp, MI.getLoc(), 4, MCFixupKind(FixupKind), |
428 | 92 | CurByte, OS, Fixups, KsError, is64BitMode(STI), -ImmSize, RIP_rel); |
429 | | |
430 | 92 | return; |
431 | 92 | } |
432 | | |
433 | 9.47k | unsigned BaseRegNo = BaseReg ? GetX86RegNum(Base) : -1U; |
434 | | //printf(">> BaseReg = %u, BaseRegNo = %u\n", BaseReg, BaseRegNo); |
435 | | //printf(">> CurByte = %u\n", CurByte); |
436 | | |
437 | | // 16-bit addressing forms of the ModR/M byte have a different encoding for |
438 | | // the R/M field and are far more limited in which registers can be used. |
439 | 9.47k | if (Is16BitMemOperand(MI, Op, STI)) { |
440 | 220 | if (BaseReg) { |
441 | | // For 32-bit addressing, the row and column values in Table 2-2 are |
442 | | // basically the same. It's AX/CX/DX/BX/SP/BP/SI/DI in that order, with |
443 | | // some special cases. And GetX86RegNum reflects that numbering. |
444 | | // For 16-bit addressing it's more fun, as shown in the SDM Vol 2A, |
445 | | // Table 2-1 "16-Bit Addressing Forms with the ModR/M byte". We can only |
446 | | // use SI/DI/BP/BX, which have "row" values 4-7 in no particular order, |
447 | | // while values 0-3 indicate the allowed combinations (base+index) of |
448 | | // those: 0 for BX+SI, 1 for BX+DI, 2 for BP+SI, 3 for BP+DI. |
449 | | // |
450 | | // R16Table[] is a lookup from the normal RegNo, to the row values from |
451 | | // Table 2-1 for 16-bit addressing modes. Where zero means disallowed. |
452 | 49 | static const unsigned R16Table[] = { 0, 0, 0, 7, 0, 6, 4, 5 }; |
453 | 49 | unsigned RMfield = R16Table[BaseRegNo]; |
454 | | |
455 | 49 | assert(RMfield && "invalid 16-bit base register"); |
456 | | |
457 | 49 | if (IndexReg.getReg()) { |
458 | 0 | unsigned IndexReg16 = R16Table[GetX86RegNum(IndexReg)]; |
459 | |
|
460 | 0 | assert(IndexReg16 && "invalid 16-bit index register"); |
461 | | // We must have one of SI/DI (4,5), and one of BP/BX (6,7). |
462 | 0 | assert(((IndexReg16 ^ RMfield) & 2) && |
463 | 0 | "invalid 16-bit base/index register combination"); |
464 | 0 | assert(Scale.getImm() == 1 && |
465 | 0 | "invalid scale for 16-bit memory reference"); |
466 | | |
467 | | // Allow base/index to appear in either order (although GAS doesn't). |
468 | 0 | if (IndexReg16 & 2) |
469 | 0 | RMfield = (RMfield & 1) | ((7 - IndexReg16) << 1); |
470 | 0 | else |
471 | 0 | RMfield = (IndexReg16 & 1) | ((7 - RMfield) << 1); |
472 | 0 | } |
473 | | |
474 | 49 | if (Disp.isImm() && isDisp8(Disp.getImm())) { |
475 | 10 | if (Disp.getImm() == 0 && BaseRegNo != N86::EBP) { |
476 | | // There is no displacement; just the register. |
477 | 5 | EmitByte(ModRMByte(0, RegOpcodeField, RMfield), CurByte, OS); |
478 | 5 | return; |
479 | 5 | } |
480 | | // Use the [REG]+disp8 form, including for [BP] which cannot be encoded. |
481 | 5 | EmitByte(ModRMByte(1, RegOpcodeField, RMfield), CurByte, OS); |
482 | 5 | EmitImmediate(MI, Disp, MI.getLoc(), 1, FK_Data_1, CurByte, OS, Fixups, KsError, |
483 | 5 | is64BitMode(STI)); |
484 | 5 | return; |
485 | 10 | } |
486 | | // This is the [REG]+disp16 case. |
487 | 39 | EmitByte(ModRMByte(2, RegOpcodeField, RMfield), CurByte, OS); |
488 | 171 | } else { |
489 | | // There is no BaseReg; this is the plain [disp16] case. |
490 | 171 | EmitByte(ModRMByte(0, RegOpcodeField, 6), CurByte, OS); |
491 | 171 | } |
492 | | |
493 | | // Emit 16-bit displacement for plain disp16 or [REG]+disp16 cases. |
494 | 210 | EmitImmediate(MI, Disp, MI.getLoc(), 2, FK_Data_2, CurByte, OS, Fixups, KsError, |
495 | 210 | is64BitMode(STI)); |
496 | 210 | return; |
497 | 220 | } |
498 | | |
499 | | // Determine whether a SIB byte is needed. |
500 | | // If no BaseReg, issue a RIP relative instruction only if the MCE can |
501 | | // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table |
502 | | // 2-7) and absolute references. |
503 | | |
504 | 9.25k | if (// The SIB byte must be used if there is an index register. |
505 | 9.25k | IndexReg.getReg() == 0 && |
506 | | // The SIB byte must be used if the base is ESP/RSP/R12, all of which |
507 | | // encode to an R/M value of 4, which indicates that a SIB byte is |
508 | | // present. |
509 | 8.76k | BaseRegNo != N86::ESP && |
510 | | // If there is no base register and we're in 64-bit mode, we need a SIB |
511 | | // byte to emit an addr that is just 'disp32' (the non-RIP relative form). |
512 | 8.56k | (!is64BitMode(STI) || BaseReg != 0)) { |
513 | | |
514 | 7.68k | if (BaseReg == 0) { // [disp32] in X86-32 mode |
515 | 7.11k | EmitByte(ModRMByte(0, RegOpcodeField, 5), CurByte, OS); |
516 | 7.11k | EmitImmediate(MI, Disp, MI.getLoc(), 4, FK_Data_4, CurByte, OS, Fixups, KsError, |
517 | 7.11k | is64BitMode(STI)); |
518 | 7.11k | return; |
519 | 7.11k | } |
520 | | |
521 | | // If the base is not EBP/ESP and there is no displacement, use simple |
522 | | // indirect register encoding, this handles addresses like [EAX]. The |
523 | | // encoding for [EBP] with no displacement means [disp32] so we handle it |
524 | | // by emitting a displacement of 0 below. |
525 | 569 | if (Disp.isImm() && Disp.getImm() == 0 && BaseRegNo != N86::EBP) { |
526 | 326 | EmitByte(ModRMByte(0, RegOpcodeField, BaseRegNo), CurByte, OS); |
527 | 326 | return; |
528 | 326 | } |
529 | | |
530 | | // Otherwise, if the displacement fits in a byte, encode as [REG+disp8]. |
531 | 243 | if (Disp.isImm()) { |
532 | 166 | if (!HasEVEX && isDisp8(Disp.getImm())) { |
533 | 125 | EmitByte(ModRMByte(1, RegOpcodeField, BaseRegNo), CurByte, OS); |
534 | 125 | EmitImmediate(MI, Disp, MI.getLoc(), 1, FK_Data_1, CurByte, OS, Fixups, KsError, |
535 | 125 | is64BitMode(STI)); |
536 | 125 | return; |
537 | 125 | } |
538 | | // Try EVEX compressed 8-bit displacement first; if failed, fall back to |
539 | | // 32-bit displacement. |
540 | 41 | int CDisp8 = 0; |
541 | 41 | if (HasEVEX && isCDisp8(TSFlags, Disp.getImm(), CDisp8)) { |
542 | 0 | EmitByte(ModRMByte(1, RegOpcodeField, BaseRegNo), CurByte, OS); |
543 | 0 | EmitImmediate(MI, Disp, MI.getLoc(), 1, FK_Data_1, CurByte, OS, Fixups, |
544 | 0 | KsError, is64BitMode(STI), CDisp8 - Disp.getImm()); |
545 | 0 | return; |
546 | 0 | } |
547 | 41 | } |
548 | | |
549 | | // Otherwise, emit the most general non-SIB encoding: [REG+disp32] |
550 | 118 | EmitByte(ModRMByte(2, RegOpcodeField, BaseRegNo), CurByte, OS); |
551 | 118 | EmitImmediate(MI, Disp, MI.getLoc(), 4, MCFixupKind(X86::reloc_signed_4byte), |
552 | 118 | CurByte, OS, Fixups, KsError, is64BitMode(STI)); |
553 | 118 | return; |
554 | 243 | } |
555 | | |
556 | | // We need a SIB byte, so start by outputting the ModR/M byte first |
557 | 9.25k | assert(IndexReg.getReg() != X86::ESP && |
558 | 1.56k | IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!"); |
559 | | |
560 | | //printf(">> 22\n"); |
561 | | //printf(">> CurByte = %u\n", CurByte); |
562 | 1.56k | bool ForceDisp32 = false; |
563 | 1.56k | bool ForceDisp8 = false; |
564 | 1.56k | int CDisp8 = 0; |
565 | 1.56k | int ImmOffset = 0; |
566 | 1.56k | if (BaseReg == 0) { |
567 | | // If there is no base register, we emit the special case SIB byte with |
568 | | // MOD=0, BASE=5, to JUST get the index, scale, and displacement. |
569 | 942 | EmitByte(ModRMByte(0, RegOpcodeField, 4), CurByte, OS); |
570 | 942 | ForceDisp32 = true; |
571 | 942 | } else if (!Disp.isImm()) { |
572 | | // Emit the normal disp32 encoding. |
573 | 40 | EmitByte(ModRMByte(2, RegOpcodeField, 4), CurByte, OS); |
574 | 40 | ForceDisp32 = true; |
575 | 585 | } else if (Disp.getImm() == 0 && |
576 | | // Base reg can't be anything that ends up with '5' as the base |
577 | | // reg, it is the magic [*] nomenclature that indicates no base. |
578 | 97 | BaseRegNo != N86::EBP) { |
579 | | // Emit no displacement ModR/M byte |
580 | 93 | EmitByte(ModRMByte(0, RegOpcodeField, 4), CurByte, OS); |
581 | 492 | } else if (!HasEVEX && isDisp8(Disp.getImm())) { |
582 | | //printf(">> 55\n"); |
583 | | // Emit the disp8 encoding. |
584 | 379 | EmitByte(ModRMByte(1, RegOpcodeField, 4), CurByte, OS); |
585 | 379 | ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP |
586 | 379 | } else if (HasEVEX && isCDisp8(TSFlags, Disp.getImm(), CDisp8)) { |
587 | | // Emit the disp8 encoding. |
588 | 0 | EmitByte(ModRMByte(1, RegOpcodeField, 4), CurByte, OS); |
589 | 0 | ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP |
590 | 0 | ImmOffset = CDisp8 - Disp.getImm(); |
591 | 113 | } else { |
592 | | // Emit the normal disp32 encoding. |
593 | 113 | EmitByte(ModRMByte(2, RegOpcodeField, 4), CurByte, OS); |
594 | 113 | } |
595 | | |
596 | | //printf(">> 88\n"); |
597 | | //printf(">> CurByte = %u\n", CurByte); |
598 | | // Calculate what the SS field value should be... |
599 | 1.56k | static const unsigned SSTable[] = { ~0U, 0, 1, ~0U, 2, ~0U, ~0U, ~0U, 3 }; |
600 | 1.56k | unsigned SS = SSTable[Scale.getImm()]; |
601 | | |
602 | | //printf(">> 99\n"); |
603 | 1.56k | if (BaseReg == 0) { |
604 | | // Handle the SIB byte for the case where there is no base, see Intel |
605 | | // Manual 2A, table 2-7. The displacement has already been output. |
606 | 942 | unsigned IndexRegNo; |
607 | 942 | if (IndexReg.getReg()) |
608 | 60 | IndexRegNo = GetX86RegNum(IndexReg); |
609 | 882 | else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5) |
610 | 882 | IndexRegNo = 4; |
611 | 942 | EmitSIBByte(SS, IndexRegNo, 5, CurByte, OS); |
612 | 942 | } else { |
613 | 625 | unsigned IndexRegNo; |
614 | 625 | if (IndexReg.getReg()) |
615 | 427 | IndexRegNo = GetX86RegNum(IndexReg); |
616 | 198 | else |
617 | 198 | IndexRegNo = 4; // For example [ESP+1*<noreg>+4] |
618 | | //printf(">> 10\n"); |
619 | | //printf(">> CurByte = %u\n", CurByte); |
620 | 625 | EmitSIBByte(SS, IndexRegNo, GetX86RegNum(Base), CurByte, OS); |
621 | 625 | } |
622 | | //printf(">> ++ CurByte = %u\n", CurByte); |
623 | | |
624 | | // Do we need to output a displacement? |
625 | 1.56k | if (ForceDisp8) |
626 | 379 | EmitImmediate(MI, Disp, MI.getLoc(), 1, FK_Data_1, CurByte, OS, Fixups, KsError, is64BitMode(STI), ImmOffset); |
627 | 1.18k | else if (ForceDisp32 || Disp.getImm() != 0) |
628 | 1.09k | EmitImmediate(MI, Disp, MI.getLoc(), 4, MCFixupKind(X86::reloc_signed_4byte), |
629 | 1.09k | CurByte, OS, Fixups, KsError, is64BitMode(STI)); |
630 | 1.56k | } |
631 | | |
632 | | /// EmitVEXOpcodePrefix - AVX instructions are encoded using a opcode prefix |
633 | | /// called VEX. |
634 | | bool X86MCCodeEmitter::EmitVEXOpcodePrefix(uint64_t TSFlags, unsigned &CurByte, |
635 | | int MemOperand, const MCInst &MI, |
636 | | const MCInstrDesc &Desc, |
637 | | raw_ostream &OS) const |
638 | 0 | { |
639 | 0 | assert(!(TSFlags & X86II::LOCK) && "Can't have LOCK VEX."); |
640 | | |
641 | 0 | uint64_t Encoding = TSFlags & X86II::EncodingMask; |
642 | 0 | bool HasEVEX_K = TSFlags & X86II::EVEX_K; |
643 | 0 | bool HasVEX_4V = TSFlags & X86II::VEX_4V; |
644 | 0 | bool HasVEX_4VOp3 = TSFlags & X86II::VEX_4VOp3; |
645 | 0 | bool HasMemOp4 = TSFlags & X86II::MemOp4; |
646 | 0 | bool HasEVEX_RC = TSFlags & X86II::EVEX_RC; |
647 | | |
648 | | // VEX_R: opcode externsion equivalent to REX.R in |
649 | | // 1's complement (inverted) form |
650 | | // |
651 | | // 1: Same as REX_R=0 (must be 1 in 32-bit mode) |
652 | | // 0: Same as REX_R=1 (64 bit mode only) |
653 | | // |
654 | 0 | unsigned char VEX_R = 0x1; |
655 | 0 | unsigned char EVEX_R2 = 0x1; |
656 | | |
657 | | // VEX_X: equivalent to REX.X, only used when a |
658 | | // register is used for index in SIB Byte. |
659 | | // |
660 | | // 1: Same as REX.X=0 (must be 1 in 32-bit mode) |
661 | | // 0: Same as REX.X=1 (64-bit mode only) |
662 | 0 | unsigned char VEX_X = 0x1; |
663 | | |
664 | | // VEX_B: |
665 | | // |
666 | | // 1: Same as REX_B=0 (ignored in 32-bit mode) |
667 | | // 0: Same as REX_B=1 (64 bit mode only) |
668 | | // |
669 | 0 | unsigned char VEX_B = 0x1; |
670 | | |
671 | | // VEX_W: opcode specific (use like REX.W, or used for |
672 | | // opcode extension, or ignored, depending on the opcode byte) |
673 | 0 | unsigned char VEX_W = 0; |
674 | | |
675 | | // VEX_5M (VEX m-mmmmm field): |
676 | | // |
677 | | // 0b00000: Reserved for future use |
678 | | // 0b00001: implied 0F leading opcode |
679 | | // 0b00010: implied 0F 38 leading opcode bytes |
680 | | // 0b00011: implied 0F 3A leading opcode bytes |
681 | | // 0b00100-0b11111: Reserved for future use |
682 | | // 0b01000: XOP map select - 08h instructions with imm byte |
683 | | // 0b01001: XOP map select - 09h instructions with no imm byte |
684 | | // 0b01010: XOP map select - 0Ah instructions with imm dword |
685 | 0 | unsigned char VEX_5M = 0; |
686 | | |
687 | | // VEX_4V (VEX vvvv field): a register specifier |
688 | | // (in 1's complement form) or 1111 if unused. |
689 | 0 | unsigned char VEX_4V = 0xf; |
690 | 0 | unsigned char EVEX_V2 = 0x1; |
691 | | |
692 | | // VEX_L (Vector Length): |
693 | | // |
694 | | // 0: scalar or 128-bit vector |
695 | | // 1: 256-bit vector |
696 | | // |
697 | 0 | unsigned char VEX_L = 0; |
698 | 0 | unsigned char EVEX_L2 = 0; |
699 | | |
700 | | // VEX_PP: opcode extension providing equivalent |
701 | | // functionality of a SIMD prefix |
702 | | // |
703 | | // 0b00: None |
704 | | // 0b01: 66 |
705 | | // 0b10: F3 |
706 | | // 0b11: F2 |
707 | | // |
708 | 0 | unsigned char VEX_PP = 0; |
709 | | |
710 | | // EVEX_U |
711 | 0 | unsigned char EVEX_U = 1; // Always '1' so far |
712 | | |
713 | | // EVEX_z |
714 | 0 | unsigned char EVEX_z = 0; |
715 | | |
716 | | // EVEX_b |
717 | 0 | unsigned char EVEX_b = 0; |
718 | | |
719 | | // EVEX_rc |
720 | 0 | unsigned char EVEX_rc = 0; |
721 | | |
722 | | // EVEX_aaa |
723 | 0 | unsigned char EVEX_aaa = 0; |
724 | |
|
725 | 0 | bool EncodeRC = false; |
726 | |
|
727 | 0 | if (TSFlags & X86II::VEX_W) |
728 | 0 | VEX_W = 1; |
729 | |
|
730 | 0 | if (TSFlags & X86II::VEX_L) |
731 | 0 | VEX_L = 1; |
732 | 0 | if (TSFlags & X86II::EVEX_L2) |
733 | 0 | EVEX_L2 = 1; |
734 | |
|
735 | 0 | if (HasEVEX_K && (TSFlags & X86II::EVEX_Z)) |
736 | 0 | EVEX_z = 1; |
737 | |
|
738 | 0 | if ((TSFlags & X86II::EVEX_B)) |
739 | 0 | EVEX_b = 1; |
740 | |
|
741 | 0 | switch (TSFlags & X86II::OpPrefixMask) { |
742 | 0 | default: break; // VEX_PP already correct |
743 | 0 | case X86II::PD: VEX_PP = 0x1; break; // 66 |
744 | 0 | case X86II::XS: VEX_PP = 0x2; break; // F3 |
745 | 0 | case X86II::XD: VEX_PP = 0x3; break; // F2 |
746 | 0 | } |
747 | | |
748 | 0 | switch (TSFlags & X86II::OpMapMask) { |
749 | 0 | default: llvm_unreachable("Invalid prefix!"); |
750 | 0 | case X86II::TB: VEX_5M = 0x1; break; // 0F |
751 | 0 | case X86II::T8: VEX_5M = 0x2; break; // 0F 38 |
752 | 0 | case X86II::TA: VEX_5M = 0x3; break; // 0F 3A |
753 | 0 | case X86II::XOP8: VEX_5M = 0x8; break; |
754 | 0 | case X86II::XOP9: VEX_5M = 0x9; break; |
755 | 0 | case X86II::XOPA: VEX_5M = 0xA; break; |
756 | 0 | } |
757 | | |
758 | | // Classify VEX_B, VEX_4V, VEX_R, VEX_X |
759 | 0 | unsigned NumOps = Desc.getNumOperands(); |
760 | 0 | unsigned CurOp = X86II::getOperandBias(Desc); |
761 | |
|
762 | 0 | switch (TSFlags & X86II::FormMask) { |
763 | 0 | default: llvm_unreachable("Unexpected form in EmitVEXOpcodePrefix!"); |
764 | 0 | case X86II::RawFrm: |
765 | 0 | break; |
766 | 0 | case X86II::MRMDestMem: { |
767 | | // MRMDestMem instructions forms: |
768 | | // MemAddr, src1(ModR/M) |
769 | | // MemAddr, src1(VEX_4V), src2(ModR/M) |
770 | | // MemAddr, src1(ModR/M), imm8 |
771 | | // |
772 | 0 | if (X86II::isX86_64ExtendedReg(MI.getOperand(MemOperand + |
773 | 0 | X86::AddrBaseReg).getReg())) |
774 | 0 | VEX_B = 0x0; |
775 | 0 | if (X86II::isX86_64ExtendedReg(MI.getOperand(MemOperand + |
776 | 0 | X86::AddrIndexReg).getReg())) |
777 | 0 | VEX_X = 0x0; |
778 | 0 | if (X86II::is32ExtendedReg(MI.getOperand(MemOperand + |
779 | 0 | X86::AddrIndexReg).getReg())) |
780 | 0 | EVEX_V2 = 0x0; |
781 | |
|
782 | 0 | CurOp += X86::AddrNumOperands; |
783 | |
|
784 | 0 | if (HasEVEX_K) |
785 | 0 | EVEX_aaa = getWriteMaskRegisterEncoding(MI, CurOp++); |
786 | |
|
787 | 0 | if (HasVEX_4V) { |
788 | 0 | VEX_4V = getVEXRegisterEncoding(MI, CurOp); |
789 | 0 | if (X86II::is32ExtendedReg(MI.getOperand(CurOp).getReg())) |
790 | 0 | EVEX_V2 = 0x0; |
791 | 0 | CurOp++; |
792 | 0 | } |
793 | |
|
794 | 0 | const MCOperand &MO = MI.getOperand(CurOp); |
795 | 0 | if (MO.isReg()) { |
796 | 0 | if (X86II::isX86_64ExtendedReg(MO.getReg())) |
797 | 0 | VEX_R = 0x0; |
798 | 0 | if (X86II::is32ExtendedReg(MO.getReg())) |
799 | 0 | EVEX_R2 = 0x0; |
800 | 0 | } |
801 | 0 | break; |
802 | 0 | } |
803 | 0 | case X86II::MRMSrcMem: |
804 | | // MRMSrcMem instructions forms: |
805 | | // src1(ModR/M), MemAddr |
806 | | // src1(ModR/M), src2(VEX_4V), MemAddr |
807 | | // src1(ModR/M), MemAddr, imm8 |
808 | | // src1(ModR/M), MemAddr, src2(VEX_I8IMM) |
809 | | // |
810 | | // FMA4: |
811 | | // dst(ModR/M.reg), src1(VEX_4V), src2(ModR/M), src3(VEX_I8IMM) |
812 | | // dst(ModR/M.reg), src1(VEX_4V), src2(VEX_I8IMM), src3(ModR/M), |
813 | 0 | if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg())) |
814 | 0 | VEX_R = 0x0; |
815 | 0 | if (X86II::is32ExtendedReg(MI.getOperand(CurOp).getReg())) |
816 | 0 | EVEX_R2 = 0x0; |
817 | 0 | CurOp++; |
818 | |
|
819 | 0 | if (HasEVEX_K) |
820 | 0 | EVEX_aaa = getWriteMaskRegisterEncoding(MI, CurOp++); |
821 | |
|
822 | 0 | if (HasVEX_4V) { |
823 | 0 | VEX_4V = getVEXRegisterEncoding(MI, CurOp); |
824 | 0 | if (X86II::is32ExtendedReg(MI.getOperand(CurOp).getReg())) |
825 | 0 | EVEX_V2 = 0x0; |
826 | 0 | CurOp++; |
827 | 0 | } |
828 | |
|
829 | 0 | if (X86II::isX86_64ExtendedReg( |
830 | 0 | MI.getOperand(MemOperand+X86::AddrBaseReg).getReg())) |
831 | 0 | VEX_B = 0x0; |
832 | 0 | if (X86II::isX86_64ExtendedReg( |
833 | 0 | MI.getOperand(MemOperand+X86::AddrIndexReg).getReg())) |
834 | 0 | VEX_X = 0x0; |
835 | 0 | if (X86II::is32ExtendedReg(MI.getOperand(MemOperand + |
836 | 0 | X86::AddrIndexReg).getReg())) |
837 | 0 | EVEX_V2 = 0x0; |
838 | |
|
839 | 0 | if (HasVEX_4VOp3) |
840 | | // Instruction format for 4VOp3: |
841 | | // src1(ModR/M), MemAddr, src3(VEX_4V) |
842 | | // CurOp points to start of the MemoryOperand, |
843 | | // it skips TIED_TO operands if exist, then increments past src1. |
844 | | // CurOp + X86::AddrNumOperands will point to src3. |
845 | 0 | VEX_4V = getVEXRegisterEncoding(MI, CurOp+X86::AddrNumOperands); |
846 | 0 | break; |
847 | 0 | case X86II::MRM0m: case X86II::MRM1m: |
848 | 0 | case X86II::MRM2m: case X86II::MRM3m: |
849 | 0 | case X86II::MRM4m: case X86II::MRM5m: |
850 | 0 | case X86II::MRM6m: case X86II::MRM7m: { |
851 | | // MRM[0-9]m instructions forms: |
852 | | // MemAddr |
853 | | // src1(VEX_4V), MemAddr |
854 | 0 | if (HasVEX_4V) { |
855 | 0 | VEX_4V = getVEXRegisterEncoding(MI, CurOp); |
856 | 0 | if (X86II::is32ExtendedReg(MI.getOperand(CurOp).getReg())) |
857 | 0 | EVEX_V2 = 0x0; |
858 | 0 | CurOp++; |
859 | 0 | } |
860 | |
|
861 | 0 | if (HasEVEX_K) |
862 | 0 | EVEX_aaa = getWriteMaskRegisterEncoding(MI, CurOp++); |
863 | |
|
864 | 0 | if (X86II::isX86_64ExtendedReg( |
865 | 0 | MI.getOperand(MemOperand+X86::AddrBaseReg).getReg())) |
866 | 0 | VEX_B = 0x0; |
867 | 0 | if (X86II::isX86_64ExtendedReg( |
868 | 0 | MI.getOperand(MemOperand+X86::AddrIndexReg).getReg())) |
869 | 0 | VEX_X = 0x0; |
870 | 0 | break; |
871 | 0 | } |
872 | 0 | case X86II::MRMSrcReg: |
873 | | // MRMSrcReg instructions forms: |
874 | | // dst(ModR/M), src1(VEX_4V), src2(ModR/M), src3(VEX_I8IMM) |
875 | | // dst(ModR/M), src1(ModR/M) |
876 | | // dst(ModR/M), src1(ModR/M), imm8 |
877 | | // |
878 | | // FMA4: |
879 | | // dst(ModR/M.reg), src1(VEX_4V), src2(ModR/M), src3(VEX_I8IMM) |
880 | | // dst(ModR/M.reg), src1(VEX_4V), src2(VEX_I8IMM), src3(ModR/M), |
881 | 0 | if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg())) |
882 | 0 | VEX_R = 0x0; |
883 | 0 | if (X86II::is32ExtendedReg(MI.getOperand(CurOp).getReg())) |
884 | 0 | EVEX_R2 = 0x0; |
885 | 0 | CurOp++; |
886 | |
|
887 | 0 | if (HasEVEX_K) |
888 | 0 | EVEX_aaa = getWriteMaskRegisterEncoding(MI, CurOp++); |
889 | |
|
890 | 0 | if (HasVEX_4V) { |
891 | 0 | VEX_4V = getVEXRegisterEncoding(MI, CurOp); |
892 | 0 | if (X86II::is32ExtendedReg(MI.getOperand(CurOp).getReg())) |
893 | 0 | EVEX_V2 = 0x0; |
894 | 0 | CurOp++; |
895 | 0 | } |
896 | |
|
897 | 0 | if (HasMemOp4) // Skip second register source (encoded in I8IMM) |
898 | 0 | CurOp++; |
899 | |
|
900 | 0 | if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg())) |
901 | 0 | VEX_B = 0x0; |
902 | 0 | if (X86II::is32ExtendedReg(MI.getOperand(CurOp).getReg())) |
903 | 0 | VEX_X = 0x0; |
904 | 0 | CurOp++; |
905 | 0 | if (HasVEX_4VOp3) |
906 | 0 | VEX_4V = getVEXRegisterEncoding(MI, CurOp++); |
907 | 0 | if (EVEX_b) { |
908 | 0 | if (HasEVEX_RC) { |
909 | 0 | unsigned RcOperand = NumOps-1; |
910 | | //assert(RcOperand >= CurOp); |
911 | 0 | if (RcOperand < CurOp || !MI.getOperand(RcOperand).isImm()) |
912 | 0 | return true; |
913 | 0 | EVEX_rc = MI.getOperand(RcOperand).getImm() & 0x3; |
914 | 0 | } |
915 | 0 | EncodeRC = true; |
916 | 0 | } |
917 | 0 | break; |
918 | 0 | case X86II::MRMDestReg: |
919 | | // MRMDestReg instructions forms: |
920 | | // dst(ModR/M), src(ModR/M) |
921 | | // dst(ModR/M), src(ModR/M), imm8 |
922 | | // dst(ModR/M), src1(VEX_4V), src2(ModR/M) |
923 | 0 | if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg())) |
924 | 0 | VEX_B = 0x0; |
925 | 0 | if (X86II::is32ExtendedReg(MI.getOperand(CurOp).getReg())) |
926 | 0 | VEX_X = 0x0; |
927 | 0 | CurOp++; |
928 | |
|
929 | 0 | if (HasEVEX_K) |
930 | 0 | EVEX_aaa = getWriteMaskRegisterEncoding(MI, CurOp++); |
931 | |
|
932 | 0 | if (HasVEX_4V) { |
933 | 0 | VEX_4V = getVEXRegisterEncoding(MI, CurOp); |
934 | 0 | if (X86II::is32ExtendedReg(MI.getOperand(CurOp).getReg())) |
935 | 0 | EVEX_V2 = 0x0; |
936 | 0 | CurOp++; |
937 | 0 | } |
938 | |
|
939 | 0 | if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg())) |
940 | 0 | VEX_R = 0x0; |
941 | 0 | if (X86II::is32ExtendedReg(MI.getOperand(CurOp).getReg())) |
942 | 0 | EVEX_R2 = 0x0; |
943 | 0 | if (EVEX_b) |
944 | 0 | EncodeRC = true; |
945 | 0 | break; |
946 | 0 | case X86II::MRM0r: case X86II::MRM1r: |
947 | 0 | case X86II::MRM2r: case X86II::MRM3r: |
948 | 0 | case X86II::MRM4r: case X86II::MRM5r: |
949 | 0 | case X86II::MRM6r: case X86II::MRM7r: |
950 | | // MRM0r-MRM7r instructions forms: |
951 | | // dst(VEX_4V), src(ModR/M), imm8 |
952 | 0 | if (HasVEX_4V) { |
953 | 0 | VEX_4V = getVEXRegisterEncoding(MI, CurOp); |
954 | 0 | if (X86II::is32ExtendedReg(MI.getOperand(CurOp).getReg())) |
955 | 0 | EVEX_V2 = 0x0; |
956 | 0 | CurOp++; |
957 | 0 | } |
958 | 0 | if (HasEVEX_K) |
959 | 0 | EVEX_aaa = getWriteMaskRegisterEncoding(MI, CurOp++); |
960 | |
|
961 | 0 | if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg())) |
962 | 0 | VEX_B = 0x0; |
963 | 0 | if (X86II::is32ExtendedReg(MI.getOperand(CurOp).getReg())) |
964 | 0 | VEX_X = 0x0; |
965 | 0 | break; |
966 | 0 | } |
967 | | |
968 | 0 | if (Encoding == X86II::VEX || Encoding == X86II::XOP) { |
969 | | // VEX opcode prefix can have 2 or 3 bytes |
970 | | // |
971 | | // 3 bytes: |
972 | | // +-----+ +--------------+ +-------------------+ |
973 | | // | C4h | | RXB | m-mmmm | | W | vvvv | L | pp | |
974 | | // +-----+ +--------------+ +-------------------+ |
975 | | // 2 bytes: |
976 | | // +-----+ +-------------------+ |
977 | | // | C5h | | R | vvvv | L | pp | |
978 | | // +-----+ +-------------------+ |
979 | | // |
980 | | // XOP uses a similar prefix: |
981 | | // +-----+ +--------------+ +-------------------+ |
982 | | // | 8Fh | | RXB | m-mmmm | | W | vvvv | L | pp | |
983 | | // +-----+ +--------------+ +-------------------+ |
984 | 0 | unsigned char LastByte = VEX_PP | (VEX_L << 2) | (VEX_4V << 3); |
985 | | |
986 | | // Can we use the 2 byte VEX prefix? |
987 | 0 | if (Encoding == X86II::VEX && VEX_B && VEX_X && !VEX_W && (VEX_5M == 1)) { |
988 | 0 | EmitByte(0xC5, CurByte, OS); |
989 | 0 | EmitByte(LastByte | (VEX_R << 7), CurByte, OS); |
990 | 0 | return false; |
991 | 0 | } |
992 | | |
993 | | // 3 byte VEX prefix |
994 | 0 | EmitByte(Encoding == X86II::XOP ? 0x8F : 0xC4, CurByte, OS); |
995 | 0 | EmitByte(VEX_R << 7 | VEX_X << 6 | VEX_B << 5 | VEX_5M, CurByte, OS); |
996 | 0 | EmitByte(LastByte | (VEX_W << 7), CurByte, OS); |
997 | 0 | } else { |
998 | 0 | assert(Encoding == X86II::EVEX && "unknown encoding!"); |
999 | | // EVEX opcode prefix can have 4 bytes |
1000 | | // |
1001 | | // +-----+ +--------------+ +-------------------+ +------------------------+ |
1002 | | // | 62h | | RXBR' | 00mm | | W | vvvv | U | pp | | z | L'L | b | v' | aaa | |
1003 | | // +-----+ +--------------+ +-------------------+ +------------------------+ |
1004 | 0 | assert((VEX_5M & 0x3) == VEX_5M |
1005 | 0 | && "More than 2 significant bits in VEX.m-mmmm fields for EVEX!"); |
1006 | | |
1007 | 0 | VEX_5M &= 0x3; |
1008 | |
|
1009 | 0 | EmitByte(0x62, CurByte, OS); |
1010 | 0 | EmitByte((VEX_R << 7) | |
1011 | 0 | (VEX_X << 6) | |
1012 | 0 | (VEX_B << 5) | |
1013 | 0 | (EVEX_R2 << 4) | |
1014 | 0 | VEX_5M, CurByte, OS); |
1015 | 0 | EmitByte((VEX_W << 7) | |
1016 | 0 | (VEX_4V << 3) | |
1017 | 0 | (EVEX_U << 2) | |
1018 | 0 | VEX_PP, CurByte, OS); |
1019 | 0 | if (EncodeRC) |
1020 | 0 | EmitByte((EVEX_z << 7) | |
1021 | 0 | (EVEX_rc << 5) | |
1022 | 0 | (EVEX_b << 4) | |
1023 | 0 | (EVEX_V2 << 3) | |
1024 | 0 | EVEX_aaa, CurByte, OS); |
1025 | 0 | else |
1026 | 0 | EmitByte((EVEX_z << 7) | |
1027 | 0 | (EVEX_L2 << 6) | |
1028 | 0 | (VEX_L << 5) | |
1029 | 0 | (EVEX_b << 4) | |
1030 | 0 | (EVEX_V2 << 3) | |
1031 | 0 | EVEX_aaa, CurByte, OS); |
1032 | 0 | } |
1033 | | |
1034 | 0 | return false; |
1035 | 0 | } |
1036 | | |
1037 | | /// DetermineREXPrefix - Determine if the MCInst has to be encoded with a X86-64 |
1038 | | /// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand |
1039 | | /// size, and 3) use of X86-64 extended registers. |
1040 | | static unsigned DetermineREXPrefix(const MCInst &MI, uint64_t TSFlags, |
1041 | 6.51k | const MCInstrDesc &Desc) { |
1042 | 6.51k | unsigned REX = 0; |
1043 | 6.51k | bool UsesHighByteReg = false; |
1044 | | |
1045 | 6.51k | if (TSFlags & X86II::REX_W) |
1046 | 1.05k | REX |= 1 << 3; // set REX.W |
1047 | | |
1048 | 6.51k | if (MI.getNumOperands() == 0) return REX; |
1049 | | |
1050 | 5.89k | unsigned NumOps = MI.getNumOperands(); |
1051 | | // FIXME: MCInst should explicitize the two-addrness. |
1052 | 5.89k | bool isTwoAddr = NumOps > 1 && |
1053 | 2.67k | Desc.getOperandConstraint(1, MCOI::TIED_TO) != -1; |
1054 | | |
1055 | | // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix. |
1056 | 5.89k | unsigned i = isTwoAddr ? 1 : 0; |
1057 | 20.1k | for (; i != NumOps; ++i) { |
1058 | 14.2k | const MCOperand &MO = MI.getOperand(i); |
1059 | 14.2k | if (!MO.isReg()) continue; |
1060 | 8.00k | unsigned Reg = MO.getReg(); |
1061 | 8.00k | if (Reg == X86::AH || Reg == X86::BH || Reg == X86::CH || Reg == X86::DH) |
1062 | 279 | UsesHighByteReg = true; |
1063 | 8.00k | if (!X86II::isX86_64NonExtLowByteReg(Reg)) continue; |
1064 | | // FIXME: The caller of DetermineREXPrefix slaps this prefix onto anything |
1065 | | // that returns non-zero. |
1066 | 0 | REX |= 0x40; // REX fixed encoding prefix |
1067 | 0 | break; |
1068 | 8.00k | } |
1069 | | |
1070 | 5.89k | switch (TSFlags & X86II::FormMask) { |
1071 | 0 | case X86II::MRMSrcReg: |
1072 | 0 | if (MI.getOperand(0).isReg() && |
1073 | 0 | X86II::isX86_64ExtendedReg(MI.getOperand(0).getReg())) |
1074 | 0 | REX |= 1 << 2; // set REX.R |
1075 | 0 | i = isTwoAddr ? 2 : 1; |
1076 | 0 | for (; i != NumOps; ++i) { |
1077 | 0 | const MCOperand &MO = MI.getOperand(i); |
1078 | 0 | if (MO.isReg() && X86II::isX86_64ExtendedReg(MO.getReg())) |
1079 | 0 | REX |= 1 << 0; // set REX.B |
1080 | 0 | } |
1081 | 0 | break; |
1082 | 89 | case X86II::MRMSrcMem: { |
1083 | 89 | if (MI.getOperand(0).isReg() && |
1084 | 89 | X86II::isX86_64ExtendedReg(MI.getOperand(0).getReg())) |
1085 | 0 | REX |= 1 << 2; // set REX.R |
1086 | 89 | unsigned Bit = 0; |
1087 | 89 | i = isTwoAddr ? 2 : 1; |
1088 | 534 | for (; i != NumOps; ++i) { |
1089 | 445 | const MCOperand &MO = MI.getOperand(i); |
1090 | 445 | if (MO.isReg()) { |
1091 | 267 | if (X86II::isX86_64ExtendedReg(MO.getReg())) |
1092 | 0 | REX |= 1 << Bit; // set REX.B (Bit=0) and REX.X (Bit=1) |
1093 | 267 | Bit++; |
1094 | 267 | } |
1095 | 445 | } |
1096 | 89 | break; |
1097 | 0 | } |
1098 | 27 | case X86II::MRMXm: |
1099 | 72 | case X86II::MRM0m: case X86II::MRM1m: |
1100 | 990 | case X86II::MRM2m: case X86II::MRM3m: |
1101 | 1.34k | case X86II::MRM4m: case X86II::MRM5m: |
1102 | 1.42k | case X86II::MRM6m: case X86II::MRM7m: |
1103 | 1.43k | case X86II::MRMDestMem: { |
1104 | 1.43k | unsigned e = (isTwoAddr ? X86::AddrNumOperands+1 : X86::AddrNumOperands); |
1105 | 1.43k | i = isTwoAddr ? 1 : 0; |
1106 | 1.43k | if (NumOps > e && MI.getOperand(e).isReg() && |
1107 | 12 | X86II::isX86_64ExtendedReg(MI.getOperand(e).getReg())) |
1108 | 0 | REX |= 1 << 2; // set REX.R |
1109 | 1.43k | unsigned Bit = 0; |
1110 | 8.59k | for (; i != e; ++i) { |
1111 | 7.16k | const MCOperand &MO = MI.getOperand(i); |
1112 | 7.16k | if (MO.isReg()) { |
1113 | 4.29k | if (X86II::isX86_64ExtendedReg(MO.getReg())) |
1114 | 0 | REX |= 1 << Bit; // REX.B (Bit=0) and REX.X (Bit=1) |
1115 | 4.29k | Bit++; |
1116 | 4.29k | } |
1117 | 7.16k | } |
1118 | 1.43k | break; |
1119 | 1.42k | } |
1120 | 4.37k | default: |
1121 | 4.37k | if (MI.getOperand(0).isReg() && |
1122 | 1.21k | X86II::isX86_64ExtendedReg(MI.getOperand(0).getReg())) |
1123 | 0 | REX |= 1 << 0; // set REX.B |
1124 | 4.37k | i = isTwoAddr ? 2 : 1; |
1125 | 6.55k | for (unsigned e = NumOps; i != e; ++i) { |
1126 | 2.18k | const MCOperand &MO = MI.getOperand(i); |
1127 | 2.18k | if (MO.isReg() && X86II::isX86_64ExtendedReg(MO.getReg())) |
1128 | 0 | REX |= 1 << 2; // set REX.R |
1129 | 2.18k | } |
1130 | 4.37k | break; |
1131 | 5.89k | } |
1132 | 5.89k | if (REX && UsesHighByteReg) |
1133 | 0 | report_fatal_error("Cannot encode high byte register in REX-prefixed instruction"); |
1134 | | |
1135 | 5.89k | return REX; |
1136 | 5.89k | } |
1137 | | |
1138 | | /// EmitSegmentOverridePrefix - Emit segment override opcode prefix as needed |
1139 | | // return true on error |
1140 | | bool X86MCCodeEmitter::EmitSegmentOverridePrefix(unsigned &CurByte, |
1141 | | unsigned SegOperand, |
1142 | | const MCInst &MI, |
1143 | | raw_ostream &OS, |
1144 | | int BaseReg) const |
1145 | 27.0k | { |
1146 | 27.0k | if (!MI.getOperand(SegOperand).isReg()) |
1147 | 0 | return true; |
1148 | | |
1149 | | // Check for explicit segment override on memory operand. |
1150 | 27.0k | switch (MI.getOperand(SegOperand).getReg()) { |
1151 | 1 | default: return true; |
1152 | 27.0k | case 0: break; |
1153 | 1 | case X86::CS: EmitByte(0x2E, CurByte, OS); break; |
1154 | 1 | case X86::SS: |
1155 | | // SS is the default segment register for stack memory operand |
1156 | 1 | if (BaseReg != X86::ESP && BaseReg != X86::EBP) |
1157 | 1 | EmitByte(0x36, CurByte, OS); |
1158 | 1 | break; |
1159 | 1 | case X86::DS: /* ignore default segment DS */ break; |
1160 | 1 | case X86::ES: EmitByte(0x26, CurByte, OS); break; |
1161 | 1 | case X86::FS: EmitByte(0x64, CurByte, OS); break; |
1162 | 1 | case X86::GS: EmitByte(0x65, CurByte, OS); break; |
1163 | 27.0k | } |
1164 | | |
1165 | 27.0k | return false; |
1166 | 27.0k | } |
1167 | | |
1168 | | /// EmitOpcodePrefix - Emit all instruction prefixes prior to the opcode. |
1169 | | /// |
1170 | | /// MemOperand is the operand # of the start of a memory operand if present. If |
1171 | | /// Not present, it is -1. |
1172 | | void X86MCCodeEmitter::EmitOpcodePrefix(uint64_t TSFlags, unsigned &CurByte, |
1173 | | int MemOperand, const MCInst &MI, |
1174 | | const MCInstrDesc &Desc, |
1175 | | const MCSubtargetInfo &STI, |
1176 | | raw_ostream &OS) const |
1177 | 60.4k | { |
1178 | | // Emit the operand size opcode prefix as needed. |
1179 | 60.4k | if ((TSFlags & X86II::OpSizeMask) == (is16BitMode(STI) ? X86II::OpSize32 |
1180 | 60.4k | : X86II::OpSize16)) { |
1181 | 1.73k | if (MI.getOpcode() != X86::MOV16sm) |
1182 | 1.69k | EmitByte(0x66, CurByte, OS); |
1183 | 1.73k | } |
1184 | | |
1185 | | // Emit the LOCK opcode prefix. |
1186 | 60.4k | if (TSFlags & X86II::LOCK) |
1187 | 0 | EmitByte(0xF0, CurByte, OS); |
1188 | | |
1189 | 60.4k | switch (TSFlags & X86II::OpPrefixMask) { |
1190 | 0 | case X86II::PD: // 66 |
1191 | 0 | EmitByte(0x66, CurByte, OS); |
1192 | 0 | break; |
1193 | 0 | case X86II::XS: // F3 |
1194 | 0 | EmitByte(0xF3, CurByte, OS); |
1195 | 0 | break; |
1196 | 0 | case X86II::XD: // F2 |
1197 | 0 | EmitByte(0xF2, CurByte, OS); |
1198 | 0 | break; |
1199 | 60.4k | } |
1200 | | |
1201 | | // Emit AddressSize and segment override prefixes before REX prefix. |
1202 | 60.4k | switch (TSFlags & X86II::FormMask) { |
1203 | 3.80k | case X86II::RawFrmDstSrc: { |
1204 | 3.80k | unsigned siReg = MI.getOperand(1).getReg(); |
1205 | | // Emit segment override opcode prefix as needed (not for %ds). |
1206 | 3.80k | if (MI.getOperand(2).getReg() != X86::DS) { |
1207 | 3.80k | EmitSegmentOverridePrefix(CurByte, 2, MI, OS); |
1208 | 3.80k | } |
1209 | | // Emit AdSize prefix as needed. |
1210 | 3.80k | if ((!is32BitMode(STI) && siReg == X86::ESI) || |
1211 | 3.80k | (is32BitMode(STI) && siReg == X86::SI)) { |
1212 | 0 | EmitByte(0x67, CurByte, OS); |
1213 | 0 | } |
1214 | 3.80k | break; |
1215 | 0 | } |
1216 | 13.2k | case X86II::RawFrmSrc: { |
1217 | 13.2k | unsigned siReg = MI.getOperand(0).getReg(); |
1218 | | // Emit segment override opcode prefix as needed (not for %ds). |
1219 | 13.2k | if (MI.getOperand(1).getReg() != X86::DS) { |
1220 | 13.2k | EmitSegmentOverridePrefix(CurByte, 1, MI, OS); |
1221 | 13.2k | } |
1222 | | // Emit AdSize prefix as needed. |
1223 | 13.2k | if ((!is32BitMode(STI) && siReg == X86::ESI) || |
1224 | 13.2k | (is32BitMode(STI) && siReg == X86::SI)) { |
1225 | 0 | EmitByte(0x67, CurByte, OS); |
1226 | 0 | } |
1227 | 13.2k | break; |
1228 | 0 | } |
1229 | 480 | case X86II::RawFrmDst: { |
1230 | 480 | unsigned siReg = MI.getOperand(0).getReg(); |
1231 | | // Emit AdSize prefix as needed. |
1232 | 480 | if ((!is32BitMode(STI) && siReg == X86::EDI) || |
1233 | 480 | (is32BitMode(STI) && siReg == X86::DI)) { |
1234 | 0 | EmitByte(0x67, CurByte, OS); |
1235 | 0 | } |
1236 | 480 | break; |
1237 | 0 | } |
1238 | 368 | case X86II::RawFrmMemOffs: |
1239 | | // Emit segment override opcode prefix as needed. |
1240 | 368 | EmitSegmentOverridePrefix(CurByte, 1, MI, OS); |
1241 | 368 | break; |
1242 | 60.4k | } |
1243 | | |
1244 | | // Handle REX prefix. |
1245 | | // FIXME: Can this come before F2 etc to simplify emission? |
1246 | 60.4k | if (is64BitMode(STI)) { |
1247 | 6.51k | if (unsigned REX = DetermineREXPrefix(MI, TSFlags, Desc)) |
1248 | 1.05k | EmitByte(0x40 | REX, CurByte, OS); |
1249 | 6.51k | } |
1250 | | |
1251 | | // 0x0F escape code must be emitted just before the opcode. |
1252 | 60.4k | switch (TSFlags & X86II::OpMapMask) { |
1253 | 8.92k | case X86II::TB: // Two-byte opcode map |
1254 | 8.92k | case X86II::T8: // 0F 38 |
1255 | 8.92k | case X86II::TA: // 0F 3A |
1256 | 8.92k | EmitByte(0x0F, CurByte, OS); |
1257 | 8.92k | break; |
1258 | 60.4k | } |
1259 | | |
1260 | 60.4k | switch (TSFlags & X86II::OpMapMask) { |
1261 | 0 | case X86II::T8: // 0F 38 |
1262 | 0 | EmitByte(0x38, CurByte, OS); |
1263 | 0 | break; |
1264 | 0 | case X86II::TA: // 0F 3A |
1265 | 0 | EmitByte(0x3A, CurByte, OS); |
1266 | 0 | break; |
1267 | 60.4k | } |
1268 | 60.4k | } |
1269 | | |
1270 | | void X86MCCodeEmitter:: |
1271 | | encodeInstruction(MCInst &MI, raw_ostream &OS, |
1272 | | SmallVectorImpl<MCFixup> &Fixups, |
1273 | | const MCSubtargetInfo &STI, unsigned int &KsError) const |
1274 | 60.4k | { |
1275 | 60.4k | unsigned Opcode = MI.getOpcode(); |
1276 | 60.4k | const MCInstrDesc &Desc = MCII.get(Opcode); |
1277 | 60.4k | uint64_t TSFlags = Desc.TSFlags; |
1278 | | |
1279 | 60.4k | KsError = 0; |
1280 | | |
1281 | | // Pseudo instructions don't get encoded. |
1282 | 60.4k | if ((TSFlags & X86II::FormMask) == X86II::Pseudo) |
1283 | 0 | return; |
1284 | | |
1285 | 60.4k | unsigned NumOps = Desc.getNumOperands(); |
1286 | 60.4k | unsigned CurOp = X86II::getOperandBias(Desc); |
1287 | | |
1288 | | // Keep track of the current byte being emitted. |
1289 | 60.4k | unsigned CurByte = 0; |
1290 | | |
1291 | | // Encoding type for this instruction. |
1292 | 60.4k | uint64_t Encoding = TSFlags & X86II::EncodingMask; |
1293 | | |
1294 | | // It uses the VEX.VVVV field? |
1295 | 60.4k | bool HasVEX_4V = TSFlags & X86II::VEX_4V; |
1296 | 60.4k | bool HasVEX_4VOp3 = TSFlags & X86II::VEX_4VOp3; |
1297 | 60.4k | bool HasMemOp4 = TSFlags & X86II::MemOp4; |
1298 | 60.4k | const unsigned MemOp4_I8IMMOperand = 2; |
1299 | 60.4k | unsigned BaseReg = 0; |
1300 | 60.4k | unsigned SegReg = 0; |
1301 | | |
1302 | | // It uses the EVEX.aaa field? |
1303 | 60.4k | bool HasEVEX_K = TSFlags & X86II::EVEX_K; |
1304 | 60.4k | bool HasEVEX_RC = TSFlags & X86II::EVEX_RC; |
1305 | | |
1306 | | // Determine where the memory operand starts, if present. |
1307 | 60.4k | int MemoryOperand = X86II::getMemoryOperandNo(TSFlags, Opcode); |
1308 | 60.4k | if (MemoryOperand != -1) MemoryOperand += CurOp; |
1309 | | |
1310 | | // Emit segment override opcode prefix as needed. |
1311 | 60.4k | if (MemoryOperand >= 0) { |
1312 | 9.57k | const MCOperand &Base = MI.getOperand(MemoryOperand + X86::AddrBaseReg); |
1313 | 9.57k | const MCOperand &Seg = MI.getOperand(MemoryOperand + X86::AddrSegmentReg); |
1314 | | |
1315 | 9.57k | BaseReg = Base.getReg(); |
1316 | 9.57k | SegReg = Seg.getReg(); |
1317 | | |
1318 | 9.57k | if ((SegReg != X86::SS) || (BaseReg != X86::ESP && BaseReg != X86::EBP)) { |
1319 | 9.57k | if (EmitSegmentOverridePrefix(CurByte, MemoryOperand+X86::AddrSegmentReg, |
1320 | 9.57k | MI, OS)) { |
1321 | 1 | KsError = KS_ERR_ASM_INVALIDOPERAND; |
1322 | 1 | return; |
1323 | 1 | } |
1324 | 9.57k | } |
1325 | 9.57k | } |
1326 | | |
1327 | | // Emit the repeat opcode prefix as needed. |
1328 | 60.4k | if (TSFlags & X86II::REP) |
1329 | 0 | EmitByte(0xF3, CurByte, OS); |
1330 | | |
1331 | | // Emit the address size opcode prefix as needed. |
1332 | 60.4k | bool need_address_override; |
1333 | 60.4k | uint64_t AdSize = TSFlags & X86II::AdSizeMask; |
1334 | 60.4k | if ((is16BitMode(STI) && AdSize == X86II::AdSize32) || |
1335 | 60.4k | (is32BitMode(STI) && AdSize == X86II::AdSize16) || |
1336 | 60.4k | (is64BitMode(STI) && AdSize == X86II::AdSize32)) { |
1337 | 0 | need_address_override = true; |
1338 | 60.4k | } else if (MemoryOperand < 0) { |
1339 | 50.8k | need_address_override = false; |
1340 | 50.8k | } else if (is64BitMode(STI)) { |
1341 | | //assert(!Is16BitMemOperand(MI, MemoryOperand, STI)); |
1342 | 1.52k | if (Is16BitMemOperand(MI, MemoryOperand, STI)) { |
1343 | 5 | KsError = KS_ERR_ASM_INSN_UNSUPPORTED; |
1344 | 5 | return; |
1345 | 5 | } |
1346 | 1.52k | need_address_override = Is32BitMemOperand(MI, MemoryOperand); |
1347 | 8.04k | } else if (is32BitMode(STI)) { |
1348 | | //assert(!Is64BitMemOperand(MI, MemoryOperand)); |
1349 | 6.42k | if (Is64BitMemOperand(MI, MemoryOperand)) { |
1350 | 0 | KsError = KS_ERR_ASM_INSN_UNSUPPORTED; |
1351 | 0 | return; |
1352 | 0 | } |
1353 | 6.42k | need_address_override = Is16BitMemOperand(MI, MemoryOperand, STI); |
1354 | 6.42k | } else { |
1355 | | //assert(is16BitMode(STI)); |
1356 | | //assert(!Is64BitMemOperand(MI, MemoryOperand)); |
1357 | 1.61k | if (!is16BitMode(STI) || Is64BitMemOperand(MI, MemoryOperand)) { |
1358 | 0 | KsError = KS_ERR_ASM_INSN_UNSUPPORTED; |
1359 | 0 | return; |
1360 | 0 | } |
1361 | 1.61k | need_address_override = !Is16BitMemOperand(MI, MemoryOperand, STI); |
1362 | 1.61k | } |
1363 | | |
1364 | 60.4k | if (need_address_override) |
1365 | 1.46k | EmitByte(0x67, CurByte, OS); |
1366 | | |
1367 | 60.4k | if (Encoding == 0) |
1368 | 60.4k | EmitOpcodePrefix(TSFlags, CurByte, MemoryOperand, MI, Desc, STI, OS); |
1369 | 0 | else { |
1370 | 0 | if (EmitVEXOpcodePrefix(TSFlags, CurByte, MemoryOperand, MI, Desc, OS)) { |
1371 | 0 | KsError = KS_ERR_ASM_INVALIDOPERAND; |
1372 | 0 | return; |
1373 | 0 | } |
1374 | 0 | } |
1375 | | |
1376 | 60.4k | unsigned char BaseOpcode = X86II::getBaseOpcodeFor(TSFlags); |
1377 | | |
1378 | 60.4k | if (TSFlags & X86II::Has3DNow0F0FOpcode) |
1379 | 0 | BaseOpcode = 0x0F; // Weird 3DNow! encoding. |
1380 | | |
1381 | 60.4k | unsigned SrcRegNum = 0; |
1382 | 60.4k | switch (TSFlags & X86II::FormMask) { |
1383 | 0 | default: errs() << "FORM: " << (TSFlags & X86II::FormMask) << "\n"; |
1384 | 0 | llvm_unreachable("Unknown FormMask value in X86MCCodeEmitter!"); |
1385 | 0 | case X86II::Pseudo: |
1386 | 0 | llvm_unreachable("Pseudo instruction shouldn't be emitted"); |
1387 | 3.80k | case X86II::RawFrmDstSrc: { |
1388 | | //printf(">> aa\n"); |
1389 | 3.80k | unsigned siReg = MI.getOperand(1).getReg(); |
1390 | | //assert(((siReg == X86::SI && MI.getOperand(0).getReg() == X86::DI) || |
1391 | | // (siReg == X86::ESI && MI.getOperand(0).getReg() == X86::EDI) || |
1392 | | // (siReg == X86::RSI && MI.getOperand(0).getReg() == X86::RDI)) && |
1393 | | // "SI and DI register sizes do not match"); |
1394 | 3.80k | if (!(siReg == X86::SI && MI.getOperand(0).getReg() == X86::DI) && |
1395 | 2.75k | !(siReg == X86::ESI && MI.getOperand(0).getReg() == X86::EDI) && |
1396 | 1.03k | !(siReg == X86::RSI && MI.getOperand(0).getReg() == X86::RDI)) { |
1397 | 0 | KsError = KS_ERR_ASM_INSN_UNSUPPORTED; |
1398 | 0 | return; |
1399 | 0 | } |
1400 | 3.80k | CurOp += 3; // Consume operands. |
1401 | 3.80k | EmitByte(BaseOpcode, CurByte, OS); |
1402 | 3.80k | break; |
1403 | 3.80k | } |
1404 | 13.2k | case X86II::RawFrmSrc: { |
1405 | | //printf(">> bb\n"); |
1406 | 13.2k | CurOp += 2; // Consume operands. |
1407 | 13.2k | EmitByte(BaseOpcode, CurByte, OS); |
1408 | 13.2k | break; |
1409 | 3.80k | } |
1410 | 480 | case X86II::RawFrmDst: { |
1411 | | //printf(">> cc\n"); |
1412 | 480 | ++CurOp; // Consume operand. |
1413 | 480 | EmitByte(BaseOpcode, CurByte, OS); |
1414 | 480 | break; |
1415 | 3.80k | } |
1416 | 30.7k | case X86II::RawFrm: |
1417 | | //printf(">> dd\n"); |
1418 | 30.7k | EmitByte(BaseOpcode, CurByte, OS); |
1419 | 30.7k | break; |
1420 | 368 | case X86II::RawFrmMemOffs: |
1421 | | //printf(">> ee\n"); |
1422 | 368 | EmitByte(BaseOpcode, CurByte, OS); |
1423 | 368 | EmitImmediate(MI, MI.getOperand(CurOp++), MI.getLoc(), |
1424 | 368 | X86II::getSizeOfImm(TSFlags), getImmFixupKind(TSFlags), |
1425 | 368 | CurByte, OS, Fixups, KsError, is64BitMode(STI)); |
1426 | 368 | ++CurOp; // skip segment operand |
1427 | 368 | break; |
1428 | 0 | case X86II::RawFrmImm8: |
1429 | | //printf(">> ff\n"); |
1430 | 0 | EmitByte(BaseOpcode, CurByte, OS); |
1431 | 0 | EmitImmediate(MI, MI.getOperand(CurOp++), MI.getLoc(), |
1432 | 0 | X86II::getSizeOfImm(TSFlags), getImmFixupKind(TSFlags), |
1433 | 0 | CurByte, OS, Fixups, KsError, is64BitMode(STI)); |
1434 | 0 | EmitImmediate(MI, MI.getOperand(CurOp++), MI.getLoc(), 1, FK_Data_1, CurByte, |
1435 | 0 | OS, Fixups, KsError, is64BitMode(STI)); |
1436 | 0 | break; |
1437 | 1 | case X86II::RawFrmImm16: |
1438 | | //printf(">> gg\n"); |
1439 | 1 | EmitByte(BaseOpcode, CurByte, OS); |
1440 | 1 | EmitImmediate(MI, MI.getOperand(CurOp++), MI.getLoc(), |
1441 | 1 | X86II::getSizeOfImm(TSFlags), getImmFixupKind(TSFlags), |
1442 | 1 | CurByte, OS, Fixups, KsError, is64BitMode(STI)); |
1443 | 1 | EmitImmediate(MI, MI.getOperand(CurOp++), MI.getLoc(), 2, FK_Data_2, CurByte, |
1444 | 1 | OS, Fixups, KsError, is64BitMode(STI)); |
1445 | 1 | break; |
1446 | | |
1447 | 64 | case X86II::AddRegFrm: |
1448 | | //printf(">> hh = %x\n", BaseOpcode + GetX86RegNum(MI.getOperand(CurOp))); |
1449 | 64 | EmitByte(BaseOpcode + GetX86RegNum(MI.getOperand(CurOp++)), CurByte, OS); |
1450 | 64 | break; |
1451 | | |
1452 | 4 | case X86II::MRMDestReg: |
1453 | | //printf(">> jj\n"); |
1454 | 4 | EmitByte(BaseOpcode, CurByte, OS); |
1455 | 4 | SrcRegNum = CurOp + 1; |
1456 | | |
1457 | 4 | if (HasEVEX_K) // Skip writemask |
1458 | 0 | SrcRegNum++; |
1459 | | |
1460 | 4 | if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV) |
1461 | 0 | ++SrcRegNum; |
1462 | | |
1463 | 4 | EmitRegModRMByte(MI.getOperand(CurOp), |
1464 | 4 | GetX86RegNum(MI.getOperand(SrcRegNum)), CurByte, OS); |
1465 | 4 | CurOp = SrcRegNum + 1; |
1466 | 4 | break; |
1467 | | |
1468 | 18 | case X86II::MRMDestMem: |
1469 | | //printf(">> kk\n"); |
1470 | 18 | EmitByte(BaseOpcode, CurByte, OS); |
1471 | 18 | SrcRegNum = CurOp + X86::AddrNumOperands; |
1472 | | |
1473 | 18 | if (HasEVEX_K) // Skip writemask |
1474 | 0 | SrcRegNum++; |
1475 | | |
1476 | 18 | if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV) |
1477 | 0 | ++SrcRegNum; |
1478 | | |
1479 | 18 | EmitMemModRMByte(MI, CurOp, |
1480 | 18 | GetX86RegNum(MI.getOperand(SrcRegNum)), |
1481 | 18 | TSFlags, CurByte, OS, Fixups, STI); |
1482 | 18 | CurOp = SrcRegNum + 1; |
1483 | 18 | break; |
1484 | | |
1485 | 3 | case X86II::MRMSrcReg: |
1486 | | //printf(">> mm\n"); |
1487 | 3 | EmitByte(BaseOpcode, CurByte, OS); |
1488 | 3 | SrcRegNum = CurOp + 1; |
1489 | | |
1490 | 3 | if (HasEVEX_K) // Skip writemask |
1491 | 0 | SrcRegNum++; |
1492 | | |
1493 | 3 | if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV) |
1494 | 0 | ++SrcRegNum; |
1495 | | |
1496 | 3 | if (HasMemOp4) // Skip 2nd src (which is encoded in I8IMM) |
1497 | 0 | ++SrcRegNum; |
1498 | | |
1499 | 3 | EmitRegModRMByte(MI.getOperand(SrcRegNum), |
1500 | 3 | GetX86RegNum(MI.getOperand(CurOp)), CurByte, OS); |
1501 | | |
1502 | | // 2 operands skipped with HasMemOp4, compensate accordingly |
1503 | 3 | CurOp = HasMemOp4 ? SrcRegNum : SrcRegNum + 1; |
1504 | 3 | if (HasVEX_4VOp3) |
1505 | 0 | ++CurOp; |
1506 | | // do not count the rounding control operand |
1507 | 3 | if (HasEVEX_RC) |
1508 | 0 | NumOps--; |
1509 | 3 | break; |
1510 | | |
1511 | 193 | case X86II::MRMSrcMem: { |
1512 | | //printf(">> nn\n"); |
1513 | 193 | int AddrOperands = X86::AddrNumOperands; |
1514 | 193 | unsigned FirstMemOp = CurOp+1; |
1515 | | |
1516 | 193 | if (HasEVEX_K) { // Skip writemask |
1517 | 0 | ++AddrOperands; |
1518 | 0 | ++FirstMemOp; |
1519 | 0 | } |
1520 | | |
1521 | 193 | if (HasVEX_4V) { |
1522 | 0 | ++AddrOperands; |
1523 | 0 | ++FirstMemOp; // Skip the register source (which is encoded in VEX_VVVV). |
1524 | 0 | } |
1525 | 193 | if (HasMemOp4) // Skip second register source (encoded in I8IMM) |
1526 | 0 | ++FirstMemOp; |
1527 | | |
1528 | 193 | EmitByte(BaseOpcode, CurByte, OS); |
1529 | | |
1530 | 193 | EmitMemModRMByte(MI, FirstMemOp, GetX86RegNum(MI.getOperand(CurOp)), |
1531 | 193 | TSFlags, CurByte, OS, Fixups, STI); |
1532 | 193 | CurOp += AddrOperands + 1; |
1533 | 193 | if (HasVEX_4VOp3) |
1534 | 0 | ++CurOp; |
1535 | 193 | break; |
1536 | 3.80k | } |
1537 | | |
1538 | 0 | case X86II::MRMXr: |
1539 | 35 | case X86II::MRM0r: case X86II::MRM1r: |
1540 | 64 | case X86II::MRM2r: case X86II::MRM3r: |
1541 | 158 | case X86II::MRM4r: case X86II::MRM5r: |
1542 | 344 | case X86II::MRM6r: case X86II::MRM7r: { |
1543 | | //printf(">> pp\n"); |
1544 | 344 | if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV). |
1545 | 0 | ++CurOp; |
1546 | 344 | if (HasEVEX_K) // Skip writemask |
1547 | 0 | ++CurOp; |
1548 | 344 | EmitByte(BaseOpcode, CurByte, OS); |
1549 | 344 | uint64_t Form = TSFlags & X86II::FormMask; |
1550 | 344 | EmitRegModRMByte(MI.getOperand(CurOp++), |
1551 | 344 | (Form == X86II::MRMXr) ? 0 : Form-X86II::MRM0r, |
1552 | 344 | CurByte, OS); |
1553 | 344 | break; |
1554 | 237 | } |
1555 | | |
1556 | 195 | case X86II::MRMXm: |
1557 | 318 | case X86II::MRM0m: case X86II::MRM1m: |
1558 | 7.83k | case X86II::MRM2m: case X86II::MRM3m: |
1559 | 8.74k | case X86II::MRM4m: case X86II::MRM5m: |
1560 | 9.35k | case X86II::MRM6m: case X86II::MRM7m: { |
1561 | | //printf(">> qq\n"); |
1562 | 9.35k | if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV). |
1563 | 0 | ++CurOp; |
1564 | 9.35k | if (HasEVEX_K) // Skip writemask |
1565 | 0 | ++CurOp; |
1566 | 9.35k | EmitByte(BaseOpcode, CurByte, OS); |
1567 | 9.35k | uint64_t Form = TSFlags & X86II::FormMask; |
1568 | 9.35k | EmitMemModRMByte(MI, CurOp, (Form == X86II::MRMXm) ? 0 : Form-X86II::MRM0m, |
1569 | 9.35k | TSFlags, CurByte, OS, Fixups, STI); |
1570 | 9.35k | CurOp += X86::AddrNumOperands; |
1571 | 9.35k | break; |
1572 | 9.07k | } |
1573 | 0 | case X86II::MRM_C0: case X86II::MRM_C1: case X86II::MRM_C2: |
1574 | 3 | case X86II::MRM_C3: case X86II::MRM_C4: case X86II::MRM_C5: |
1575 | 6 | case X86II::MRM_C6: case X86II::MRM_C7: case X86II::MRM_C8: |
1576 | 143 | case X86II::MRM_C9: case X86II::MRM_CA: case X86II::MRM_CB: |
1577 | 143 | case X86II::MRM_CC: case X86II::MRM_CD: case X86II::MRM_CE: |
1578 | 143 | case X86II::MRM_CF: case X86II::MRM_D0: case X86II::MRM_D1: |
1579 | 144 | case X86II::MRM_D2: case X86II::MRM_D3: case X86II::MRM_D4: |
1580 | 173 | case X86II::MRM_D5: case X86II::MRM_D6: case X86II::MRM_D7: |
1581 | 176 | case X86II::MRM_D8: case X86II::MRM_D9: case X86II::MRM_DA: |
1582 | 178 | case X86II::MRM_DB: case X86II::MRM_DC: case X86II::MRM_DD: |
1583 | 1.50k | case X86II::MRM_DE: case X86II::MRM_DF: case X86II::MRM_E0: |
1584 | 1.66k | case X86II::MRM_E1: case X86II::MRM_E2: case X86II::MRM_E3: |
1585 | 1.67k | case X86II::MRM_E4: case X86II::MRM_E5: case X86II::MRM_E6: |
1586 | 1.69k | case X86II::MRM_E7: case X86II::MRM_E8: case X86II::MRM_E9: |
1587 | 1.71k | case X86II::MRM_EA: case X86II::MRM_EB: case X86II::MRM_EC: |
1588 | 1.72k | case X86II::MRM_ED: case X86II::MRM_EE: case X86II::MRM_EF: |
1589 | 1.74k | case X86II::MRM_F0: case X86II::MRM_F1: case X86II::MRM_F2: |
1590 | 1.74k | case X86II::MRM_F3: case X86II::MRM_F4: case X86II::MRM_F5: |
1591 | 1.74k | case X86II::MRM_F6: case X86II::MRM_F7: case X86II::MRM_F8: |
1592 | 1.74k | case X86II::MRM_F9: case X86II::MRM_FA: case X86II::MRM_FB: |
1593 | 1.77k | case X86II::MRM_FC: case X86II::MRM_FD: case X86II::MRM_FE: |
1594 | 1.78k | case X86II::MRM_FF: |
1595 | | //printf(">> ss\n"); |
1596 | 1.78k | EmitByte(BaseOpcode, CurByte, OS); |
1597 | | |
1598 | 1.78k | uint64_t Form = TSFlags & X86II::FormMask; |
1599 | 1.78k | EmitByte(0xC0 + Form - X86II::MRM_C0, CurByte, OS); |
1600 | 1.78k | break; |
1601 | 60.4k | } |
1602 | | //printf(">> tt CurByte = %x\n", CurByte); |
1603 | | |
1604 | | // If there is a remaining operand, it must be a trailing immediate. Emit it |
1605 | | // according to the right size for the instruction. Some instructions |
1606 | | // (SSE4a extrq and insertq) have two trailing immediates. |
1607 | 84.8k | while (CurOp != NumOps && NumOps - CurOp <= 2) { |
1608 | | // The last source register of a 4 operand instruction in AVX is encoded |
1609 | | // in bits[7:4] of a immediate byte. |
1610 | 24.4k | if (TSFlags & X86II::VEX_I8IMM) { |
1611 | 0 | const MCOperand &MO = MI.getOperand(HasMemOp4 ? MemOp4_I8IMMOperand |
1612 | 0 | : CurOp); |
1613 | 0 | ++CurOp; |
1614 | 0 | unsigned RegNum = GetX86RegNum(MO) << 4; |
1615 | 0 | if (X86II::isX86_64ExtendedReg(MO.getReg())) |
1616 | 0 | RegNum |= 1 << 7; |
1617 | | // If there is an additional 5th operand it must be an immediate, which |
1618 | | // is encoded in bits[3:0] |
1619 | 0 | if (CurOp != NumOps) { |
1620 | 0 | const MCOperand &MIMM = MI.getOperand(CurOp++); |
1621 | 0 | if (MIMM.isImm()) { |
1622 | 0 | unsigned Val = MIMM.getImm(); |
1623 | | // assert(Val < 16 && "Immediate operand value out of range"); |
1624 | 0 | if (Val >= 16) { |
1625 | 0 | KsError = KS_ERR_ASM_INSN_UNSUPPORTED; |
1626 | 0 | return; |
1627 | 0 | } |
1628 | 0 | RegNum |= Val; |
1629 | 0 | } |
1630 | 0 | } |
1631 | 0 | EmitImmediate(MI, MCOperand::createImm(RegNum), MI.getLoc(), 1, FK_Data_1, |
1632 | 0 | CurByte, OS, Fixups, KsError, is64BitMode(STI)); |
1633 | 24.4k | } else { |
1634 | | //printf(">> vv BaseOpcode = %u, Opcode = %u\n", BaseOpcode, Opcode); |
1635 | 24.4k | EmitImmediate(MI, MI.getOperand(CurOp++), MI.getLoc(), |
1636 | 24.4k | X86II::getSizeOfImm(TSFlags), getImmFixupKind(TSFlags), |
1637 | 24.4k | CurByte, OS, Fixups, KsError, is64BitMode(STI)); |
1638 | 24.4k | if (KsError) |
1639 | 23 | break; |
1640 | 24.4k | } |
1641 | 24.4k | } |
1642 | | |
1643 | 60.4k | if (KsError == 0 && TSFlags & X86II::Has3DNow0F0FOpcode) |
1644 | 0 | EmitByte(X86II::getBaseOpcodeFor(TSFlags), CurByte, OS); |
1645 | | |
1646 | | |
1647 | | // Keystone: update Inst.Address to point to the next instruction |
1648 | 60.4k | MI.setAddress(MI.getAddress() + CurByte); |
1649 | | |
1650 | 60.4k | #ifndef NDEBUG |
1651 | | // FIXME: Verify. |
1652 | 60.4k | if (/*!Desc.isVariadic() &&*/ CurOp != NumOps) { |
1653 | 0 | errs() << "Cannot encode all operands of: "; |
1654 | 0 | MI.dump(); |
1655 | 0 | errs() << '\n'; |
1656 | 0 | abort(); |
1657 | 0 | } |
1658 | 60.4k | #endif |
1659 | 60.4k | } |