| 1 |
// |
| 2 |
// Copyright (C) 2010 Wayne Meissner |
| 3 |
// Copyright (c) 2008-2009, Petr Kobalicek <[email protected]> |
| 4 |
// |
| 5 |
// Permission is hereby granted, free of charge, to any person |
| 6 |
// obtaining a copy of this software and associated documentation |
| 7 |
// files (the "Software"), to deal in the Software without |
| 8 |
// restriction, including without limitation the rights to use, |
| 9 |
// copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
// copies of the Software, and to permit persons to whom the |
| 11 |
// Software is furnished to do so, subject to the following |
| 12 |
// conditions: |
| 13 |
// |
| 14 |
// The above copyright notice and this permission notice shall be |
| 15 |
// included in all copies or substantial portions of the Software. |
| 16 |
// |
| 17 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 19 |
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 21 |
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 22 |
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 24 |
// OTHER DEALINGS IN THE SOFTWARE. |
| 25 |
|
| 26 |
package com.kenai.jnr.x86asm; |
| 27 |
|
| 28 |
import java.nio.ByteBuffer; |
| 29 |
import java.util.LinkedList; |
| 30 |
import java.util.List; |
| 31 |
import static com.kenai.jnr.x86asm.HINT.*; |
| 32 |
import static com.kenai.jnr.x86asm.INST_CODE.*; |
| 33 |
import static com.kenai.jnr.x86asm.InstructionGroup.*; |
| 34 |
import static com.kenai.jnr.x86asm.OP.*; |
| 35 |
import static com.kenai.jnr.x86asm.OperandFlags.*; |
| 36 |
import static com.kenai.jnr.x86asm.PROPERTY.*; |
| 37 |
import static com.kenai.jnr.x86asm.RELOC_MODE.*; |
| 38 |
import static com.kenai.jnr.x86asm.REG.*; |
| 39 |
|
| 40 |
import static com.kenai.jnr.x86asm.Util.*; |
| 41 |
|
| 42 |
|
| 43 |
/** |
| 44 |
* Low level code generation. |
| 45 |
*/ |
| 46 |
@Deprecated |
| 47 |
public final class Assembler extends Serializer { |
| 48 |
private final CodeBuffer _buffer = new CodeBuffer(); |
| 49 |
private final List<RelocData> _relocData = new LinkedList<RelocData>(); |
| 50 |
private final CpuInfo cpuInfo = CpuInfo.GENERIC; |
| 51 |
private int _properties = 0; |
| 52 |
|
| 53 |
/** Size of possible trampolines. */ |
| 54 |
int _trampolineSize; |
| 55 |
|
| 56 |
private final Logger _logger = null; |
| 57 |
|
| 58 |
private final CPU cpu; |
| 59 |
|
| 60 |
@Override |
| 61 |
boolean is64() { |
| 62 |
return cpu == CPU.X86_64; |
| 63 |
} |
| 64 |
|
| 65 |
private static final int intValue(boolean b) { |
| 66 |
return b ? 1 : 0; |
| 67 |
} |
| 68 |
|
| 69 |
public static final CPU I386 = CPU.I386; |
| 70 |
public static final CPU X86_64 = CPU.X86_64; |
| 71 |
|
| 72 |
public Assembler(CPU cpu) { |
| 73 |
this.cpu = cpu; |
| 74 |
} |
| 75 |
|
| 76 |
public final int offset() { |
| 77 |
return _buffer.offset(); |
| 78 |
} |
| 79 |
|
| 80 |
/** Gets the required size of memory required to store all the generated code */ |
| 81 |
public final int codeSize() { |
| 82 |
return _buffer.offset() + trampolineSize(); |
| 83 |
} |
| 84 |
|
| 85 |
/** Return size of all possible trampolines needed to successfuly generate |
| 86 |
* relative jumps to absolute addresses. This value is only non-zero if jmp |
| 87 |
* of call instructions were used with immediate operand (this means jump or |
| 88 |
* call absolute address directly). |
| 89 |
* |
| 90 |
* Currently only _emitJmpOrCallReloc() method can increase trampoline size |
| 91 |
* value. |
| 92 |
*/ |
| 93 |
int trampolineSize() { |
| 94 |
return _trampolineSize; |
| 95 |
} |
| 96 |
|
| 97 |
//! @brief Set byte at position @a pos. |
| 98 |
public final byte getByteAt(int pos) { |
| 99 |
return _buffer.getByteAt(pos); |
| 100 |
} |
| 101 |
|
| 102 |
//! @brief Set word at position @a pos. |
| 103 |
public final short getWordAt(int pos) { |
| 104 |
return _buffer.getWordAt(pos); |
| 105 |
} |
| 106 |
|
| 107 |
//! @brief Set word at position @a pos. |
| 108 |
public final int getDWordAt(int pos) { |
| 109 |
return _buffer.getDWordAt(pos); |
| 110 |
} |
| 111 |
|
| 112 |
//! @brief Set word at position @a pos. |
| 113 |
public final long getQWordAt(int pos) { |
| 114 |
return _buffer.getQWordAt(pos); |
| 115 |
} |
| 116 |
|
| 117 |
//! @brief Set byte at position @a pos. |
| 118 |
public final void setByteAt(int pos, byte x) { |
| 119 |
_buffer.setByteAt(pos, x); |
| 120 |
} |
| 121 |
|
| 122 |
//! @brief Set word at position @a pos. |
| 123 |
public final void setWordAt(int pos, short x) { |
| 124 |
_buffer.setWordAt(pos, x); |
| 125 |
} |
| 126 |
|
| 127 |
//! @brief Set word at position @a pos. |
| 128 |
public final void setDWordAt(int pos, int x) { |
| 129 |
_buffer.setDWordAt(pos, x); |
| 130 |
} |
| 131 |
|
| 132 |
//! @brief Set word at position @a pos. |
| 133 |
public final void setQWordAt(int pos, long x) { |
| 134 |
_buffer.setQWordAt(pos, x); |
| 135 |
} |
| 136 |
|
| 137 |
//! @brief Set word at position @a pos. |
| 138 |
public final int getInt32At(int pos) { |
| 139 |
return (int) _buffer.getDWordAt(pos); |
| 140 |
} |
| 141 |
|
| 142 |
//! @brief Set int32 at position @a pos. |
| 143 |
public final void setInt32At(int pos, long x) { |
| 144 |
_buffer.setDWordAt(pos, (int) x); |
| 145 |
} |
| 146 |
|
| 147 |
public final void setVarAt(int pos, long i, boolean isUnsigned, int size) { |
| 148 |
switch (size) { |
| 149 |
case 1: |
| 150 |
setByteAt(pos, (byte) i); |
| 151 |
break; |
| 152 |
case 2: |
| 153 |
setWordAt(pos, (short) i); |
| 154 |
break; |
| 155 |
case 4: |
| 156 |
setDWordAt(pos, (int) i); |
| 157 |
break; |
| 158 |
case 8: |
| 159 |
setQWordAt(pos, i); |
| 160 |
default: |
| 161 |
throw new IllegalArgumentException("invalid size"); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** Emit Byte to internal buffer. */ |
| 166 |
final void _emitByte(int x) { |
| 167 |
_buffer.emitByte((byte) x); |
| 168 |
} |
| 169 |
|
| 170 |
/** Emit Word (2 bytes) to internal buffer. */ |
| 171 |
final void _emitWord(int x) { |
| 172 |
_buffer.emitWord((short) x); |
| 173 |
} |
| 174 |
|
| 175 |
/** Emit DWord (4 bytes) to internal buffer. */ |
| 176 |
final void _emitDWord(int x) { |
| 177 |
_buffer.emitDWord(x); |
| 178 |
} |
| 179 |
|
| 180 |
/** Emit QWord (8 bytes) to internal buffer. */ |
| 181 |
final void _emitQWord(long x) { |
| 182 |
_buffer.emitQWord(x); |
| 183 |
} |
| 184 |
|
| 185 |
/** Emit Int32 (4 bytes) to internal buffer. */ |
| 186 |
final void _emitInt32(int x) { |
| 187 |
_buffer.emitDWord(x); |
| 188 |
} |
| 189 |
|
| 190 |
/** Emit system signed integer (4 or 8 bytes) to internal buffer. */ |
| 191 |
final void _emitSysInt(long x) { |
| 192 |
if (is64()) { |
| 193 |
_buffer.emitQWord(x); |
| 194 |
} else { |
| 195 |
_buffer.emitDWord((int) x); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
//! @brief Emit single @a opCode without operands. |
| 200 |
final void _emitOpCode(int opCode) { |
| 201 |
// instruction prefix |
| 202 |
if ((opCode & 0xFF000000) != 0) { |
| 203 |
_emitByte((byte) ((opCode & 0xFF000000) >> 24)); |
| 204 |
} |
| 205 |
// instruction opcodes |
| 206 |
if ((opCode & 0x00FF0000) != 0) { |
| 207 |
_emitByte((byte) ((opCode & 0x00FF0000) >> 16)); |
| 208 |
} |
| 209 |
if ((opCode & 0x0000FF00) != 0) { |
| 210 |
_emitByte((byte) ((opCode & 0x0000FF00) >> 8)); |
| 211 |
} |
| 212 |
// last opcode is always emitted (can be also 0x00) |
| 213 |
_emitByte((byte) (opCode & 0x000000FF)); |
| 214 |
} |
| 215 |
|
| 216 |
void _emitSegmentPrefix(Operand rm) { |
| 217 |
if (rm.isMem()) { |
| 218 |
SEGMENT segmentPrefix = ((Mem) rm).segmentPrefix(); |
| 219 |
if (segmentPrefix != SEGMENT.SEGMENT_NONE) { |
| 220 |
_emitByte(segmentPrefix.prefix()); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
void _emitImmediate(Immediate imm, int size) { |
| 226 |
switch (size) { |
| 227 |
case 1: |
| 228 |
_emitByte(imm.byteValue()); |
| 229 |
break; |
| 230 |
case 2: |
| 231 |
_emitWord(imm.shortValue()); |
| 232 |
break; |
| 233 |
case 4: |
| 234 |
_emitDWord(imm.intValue()); |
| 235 |
break; |
| 236 |
case 8: |
| 237 |
if (!is64()) { |
| 238 |
throw new IllegalArgumentException("64 bit immediate values not supported for 32bit"); |
| 239 |
} |
| 240 |
_emitQWord(imm.longValue()); |
| 241 |
break; |
| 242 |
default: |
| 243 |
throw new IllegalArgumentException("invalid immediate operand size"); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
/** Emit REX prefix (64 bit mode only). */ |
| 248 |
void _emitRexR(int w, int opReg, int regCode) { |
| 249 |
if (is64()) { |
| 250 |
|
| 251 |
boolean r = (opReg & 0x8) != 0; |
| 252 |
boolean b = (regCode & 0x8) != 0; |
| 253 |
|
| 254 |
// w Default operand size(0=Default, 1=64 bits). |
| 255 |
// r Register field (1=high bit extension of the ModR/M REG field). |
| 256 |
// x Index field not used in RexR |
| 257 |
// b Base field (1=high bit extension of the ModR/M or SIB Base field). |
| 258 |
if (w != 0 || r || b || (_properties & (1 << PROPERTY_X86_FORCE_REX)) != 0) { |
| 259 |
_emitByte(0x40 | (w << 3) | (intValue(r) << 2) | intValue(b)); |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
void _emitRexR(boolean w, int opReg, int regCode) { |
| 265 |
_emitRexR(intValue(w), opReg, regCode); |
| 266 |
} |
| 267 |
|
| 268 |
/** Emit REX prefix (64 bit mode only). */ |
| 269 |
void _emitRexRM(int w, int opReg, Operand rm) { |
| 270 |
if (is64()) { |
| 271 |
|
| 272 |
boolean r = (opReg & 0x8) != 0; |
| 273 |
boolean x = false; |
| 274 |
boolean b = false; |
| 275 |
|
| 276 |
if (rm.isReg()) { |
| 277 |
b = (((BaseReg) rm).code() & 0x8) != 0; |
| 278 |
|
| 279 |
} else if (rm.isMem()) { |
| 280 |
x = (((Mem) rm).index() & 0x8) != 0 && ((Mem) rm).index() != NO_REG; |
| 281 |
b = (((Mem) rm).base() & 0x8) != 0 && ((Mem) rm).base() != NO_REG; |
| 282 |
} |
| 283 |
|
| 284 |
// w Default operand size(0=Default, 1=64 bits). |
| 285 |
// r Register field (1=high bit extension of the ModR/M REG field). |
| 286 |
// x Index field (1=high bit extension of the SIB Index field). |
| 287 |
// b Base field (1=high bit extension of the ModR/M or SIB Base field). |
| 288 |
if (w != 0 || r || x || b || (_properties & (1 << PROPERTY_X86_FORCE_REX)) != 0) { |
| 289 |
_emitByte(0x40 | (w << 3) | (intValue(r) << 2) | (intValue(x) << 1) | intValue(b)); |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
void _emitRexRM(boolean w, int opReg, Operand rm) { |
| 296 |
_emitRexRM(intValue(w), opReg, rm); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
void _emitModM(int opReg, Mem mem, int immSize) { |
| 301 |
assert (mem.op() == OP_MEM); |
| 302 |
|
| 303 |
int baseReg = mem.base() & 0x7; |
| 304 |
int indexReg = mem.index() & 0x7; |
| 305 |
long disp = mem.displacement(); |
| 306 |
int shift = mem.shift(); |
| 307 |
|
| 308 |
// [base + displacemnt] |
| 309 |
if (mem.hasBase() && !mem.hasIndex()) { |
| 310 |
// ESP/RSP/R12 == 4 |
| 311 |
if (baseReg == 4) { |
| 312 |
int mod = 0; |
| 313 |
|
| 314 |
if (disp != 0) { |
| 315 |
mod = isInt8(disp) ? 1 : 2; |
| 316 |
} |
| 317 |
|
| 318 |
_emitMod(mod, opReg, 4); |
| 319 |
_emitSib(0, 4, 4); |
| 320 |
|
| 321 |
if (disp != 0) { |
| 322 |
if (isInt8(disp)) { |
| 323 |
_emitByte((byte) disp); |
| 324 |
} else { |
| 325 |
_emitInt32((int) disp); |
| 326 |
} |
| 327 |
} |
| 328 |
} // EBP/RBP/R13 == 5 |
| 329 |
else if (baseReg != 5 && disp == 0) { |
| 330 |
_emitMod(0, opReg, baseReg); |
| 331 |
} else if (isInt8(disp)) { |
| 332 |
_emitMod(1, opReg, baseReg); |
| 333 |
_emitByte((byte) disp); |
| 334 |
} else { |
| 335 |
_emitMod(2, opReg, baseReg); |
| 336 |
_emitInt32((int) disp); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
// [base + index * scale + displacemnt] |
| 341 |
else if (mem.hasBase() && mem.hasIndex()) { |
| 342 |
// ASMJIT_ASSERT(indexReg != RID_ESP); |
| 343 |
|
| 344 |
// EBP/RBP/R13 == 5 |
| 345 |
if (baseReg != 5 && disp == 0) { |
| 346 |
_emitMod(0, opReg, 4); |
| 347 |
_emitSib(shift, indexReg, baseReg); |
| 348 |
} else if (isInt8(disp)) { |
| 349 |
_emitMod(1, opReg, 4); |
| 350 |
_emitSib(shift, indexReg, baseReg); |
| 351 |
_emitByte((byte) disp); |
| 352 |
} else { |
| 353 |
_emitMod(2, opReg, 4); |
| 354 |
_emitSib(shift, indexReg, baseReg); |
| 355 |
_emitInt32((int) disp); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
// Address | 32-bit mode | 64-bit mode |
| 360 |
// ------------------------------+-------------+--------------- |
| 361 |
// [displacement] | ABSOLUTE | RELATIVE (RIP) |
| 362 |
// [index * scale + displacemnt] | ABSOLUTE | ABSOLUTE (ZERO EXTENDED) |
| 363 |
else { |
| 364 |
// In 32 bit mode is used absolute addressing model. |
| 365 |
// In 64 bit mode is used relative addressing model together with absolute |
| 366 |
// addressing one. Main problem is that if instruction contains SIB then |
| 367 |
// relative addressing (RIP) is not possible. |
| 368 |
if (!is64()) { |
| 369 |
|
| 370 |
if (mem.hasIndex()) { |
| 371 |
// ASMJIT_ASSERT(mem.index() != 4); // ESP/RSP == 4 |
| 372 |
_emitMod(0, opReg, 4); |
| 373 |
_emitSib(shift, indexReg, 5); |
| 374 |
} else { |
| 375 |
_emitMod(0, opReg, 5); |
| 376 |
} |
| 377 |
|
| 378 |
// X86 uses absolute addressing model, all relative addresses will be |
| 379 |
// relocated to absolute ones. |
| 380 |
if (mem.hasLabel()) { |
| 381 |
Label label = mem.label(); |
| 382 |
int relocId = _relocData.size(); |
| 383 |
|
| 384 |
|
| 385 |
long destination = disp; |
| 386 |
if (label.isBound()) { |
| 387 |
destination += label.position(); |
| 388 |
// Dummy DWORD |
| 389 |
_emitInt32(0); |
| 390 |
} else { |
| 391 |
_emitDisplacement(label, -4 - immSize, 4).relocId = relocId; |
| 392 |
} |
| 393 |
|
| 394 |
// Relative addressing will be relocated to absolute address. |
| 395 |
RelocData rd = new RelocData(RelocData.Type.RELATIVE_TO_ABSOLUTE, 4, offset(), destination); |
| 396 |
|
| 397 |
_relocData.add(rd); |
| 398 |
} else { |
| 399 |
// Absolute address |
| 400 |
_emitInt32((int) (mem.target() + disp)); |
| 401 |
} |
| 402 |
|
| 403 |
} else { |
| 404 |
|
| 405 |
// X64 uses relative addressing model |
| 406 |
if (mem.hasLabel()) { |
| 407 |
Label label = mem.label(); |
| 408 |
|
| 409 |
if (mem.hasIndex()) { |
| 410 |
// Indexing is not possible |
| 411 |
throw new IllegalArgumentException("illegal addressing"); |
| 412 |
} |
| 413 |
|
| 414 |
// Relative address (RIP +/- displacement) |
| 415 |
_emitMod(0, opReg, 5); |
| 416 |
|
| 417 |
disp -= (4 + immSize); |
| 418 |
|
| 419 |
if (label.isBound()) { |
| 420 |
disp += offset() - label.position(); |
| 421 |
_emitInt32((int) disp); |
| 422 |
} else { |
| 423 |
_emitDisplacement(label, disp, 4); |
| 424 |
} |
| 425 |
} else { |
| 426 |
// Absolute address (truncated to 32 bits), this kind of address requires |
| 427 |
// SIB byte (4) |
| 428 |
_emitMod(0, opReg, 4); |
| 429 |
|
| 430 |
if (mem.hasIndex()) { |
| 431 |
// ASMJIT_ASSERT(mem.index() != 4); // ESP/RSP == 4 |
| 432 |
_emitSib(shift, indexReg, 5); |
| 433 |
} else { |
| 434 |
_emitSib(0, 4, 5); |
| 435 |
} |
| 436 |
|
| 437 |
// truncate to 32 bits |
| 438 |
long target = mem.target() + disp; |
| 439 |
|
| 440 |
if (target > 0xFFFFFFFFL) { |
| 441 |
_logger.log("; Warning: Absolute address truncated to 32 bits\n"); |
| 442 |
} |
| 443 |
|
| 444 |
_emitInt32((int) target); |
| 445 |
} |
| 446 |
|
| 447 |
} |
| 448 |
|
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
void _emitX86Inl(int opCode, boolean i16bit, boolean rexw, int reg) { |
| 453 |
_emitX86Inl(opCode, i16bit, intValue(rexw), reg); |
| 454 |
} |
| 455 |
|
| 456 |
void _emitX86Inl(int opCode, boolean i16bit, int rexw, int reg) { |
| 457 |
// 16 bit prefix |
| 458 |
if (i16bit) { |
| 459 |
_emitByte(0x66); |
| 460 |
} |
| 461 |
|
| 462 |
// instruction prefix |
| 463 |
if ((opCode & 0xFF000000) != 0) { |
| 464 |
_emitByte(((opCode & 0xFF000000) >> 24)); |
| 465 |
} |
| 466 |
|
| 467 |
// rex prefix |
| 468 |
if (is64()) { |
| 469 |
_emitRexR(rexw, 0, reg); |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
// instruction opcodes |
| 474 |
if ((opCode & 0x00FF0000) != 0) { |
| 475 |
_emitByte(((opCode & 0x00FF0000) >> 16)); |
| 476 |
} |
| 477 |
if ((opCode & 0x0000FF00) != 0) { |
| 478 |
_emitByte(((opCode & 0x0000FF00) >> 8)); |
| 479 |
} |
| 480 |
|
| 481 |
_emitByte((opCode & 0x000000FF) + (reg & 0x7)); |
| 482 |
} |
| 483 |
|
| 484 |
void _emitModRM(int opReg, Operand op, int immSize) { |
| 485 |
assert (op.op() == OP_REG || op.op() == OP_MEM); |
| 486 |
|
| 487 |
if (op.op() == OP_REG) { |
| 488 |
_emitModR(opReg, ((BaseReg) op).code()); |
| 489 |
} else { |
| 490 |
_emitModM(opReg, (Mem) op, immSize); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
/** Emit MODR/M byte. */ |
| 495 |
void _emitMod(int m, int o, int r) { |
| 496 |
_emitByte((byte) (((m & 0x03) << 6) | ((o & 0x07) << 3) | (r & 0x07))); |
| 497 |
} |
| 498 |
|
| 499 |
/** Emit SIB byte. */ |
| 500 |
void _emitSib(int s, int i, int b) { |
| 501 |
_emitByte((byte) (((s & 0x03) << 6) | ((i & 0x07) << 3) | (b & 0x07))); |
| 502 |
} |
| 503 |
|
| 504 |
/** Emit Register / Register - calls _emitMod(3, opReg, r) */ |
| 505 |
void _emitModR(int opReg, int r) { |
| 506 |
_emitMod(3, opReg, r); |
| 507 |
} |
| 508 |
|
| 509 |
/** Emit Register / Register - calls _emitMod(3, opReg, r.code()) */ |
| 510 |
void _emitModR(int opReg, BaseReg r) { |
| 511 |
_emitMod(3, opReg, r.code()); |
| 512 |
} |
| 513 |
|
| 514 |
void _emitX86RM(int opCode, boolean i16bit, boolean rexw, int o, Operand op, int immSize) { |
| 515 |
_emitX86RM(opCode, i16bit, intValue(rexw), o, op, immSize); |
| 516 |
} |
| 517 |
|
| 518 |
void _emitX86RM(int opCode, boolean i16bit, int rexw, int o, Operand op, int immSize) { |
| 519 |
// 16 bit prefix |
| 520 |
if (i16bit) { |
| 521 |
_emitByte(0x66); |
| 522 |
} |
| 523 |
|
| 524 |
// segment prefix |
| 525 |
_emitSegmentPrefix(op); |
| 526 |
|
| 527 |
// instruction prefix |
| 528 |
if ((opCode & 0xFF000000) != 0) { |
| 529 |
_emitByte(((opCode & 0xFF000000) >> 24)); |
| 530 |
} |
| 531 |
|
| 532 |
// rex prefix |
| 533 |
if (is64()) { |
| 534 |
_emitRexRM(rexw, o, op); |
| 535 |
} |
| 536 |
|
| 537 |
// instruction opcodes |
| 538 |
if ((opCode & 0x00FF0000) != 0) { |
| 539 |
_emitByte((byte) ((opCode & 0x00FF0000) >> 16)); |
| 540 |
} |
| 541 |
|
| 542 |
if ((opCode & 0x0000FF00) != 0) { |
| 543 |
_emitByte((byte) ((opCode & 0x0000FF00) >> 8)); |
| 544 |
} |
| 545 |
|
| 546 |
_emitByte((byte) (opCode & 0x000000FF)); |
| 547 |
|
| 548 |
// ModR/M |
| 549 |
_emitModRM(o, op, immSize); |
| 550 |
} |
| 551 |
|
| 552 |
void _emitX86(INST_CODE code, Operand o1, Operand o2, Operand o3) { |
| 553 |
|
| 554 |
InstructionDescription id = InstructionDescription.find(code); |
| 555 |
switch (id.group) { |
| 556 |
case I_EMIT: |
| 557 |
{ |
| 558 |
_emitOpCode(id.opCode1); |
| 559 |
return; |
| 560 |
} |
| 561 |
|
| 562 |
case I_ALU: |
| 563 |
{ |
| 564 |
int opCode = id.opCode1; |
| 565 |
int opReg = id.opCodeR; |
| 566 |
|
| 567 |
// Mem <- Reg |
| 568 |
if (o1.isMem() && o2.isReg()) |
| 569 |
{ |
| 570 |
_emitX86RM(opCode + intValue(!o2.isRegType(REG_GPB)), |
| 571 |
o2.isRegType(REG_GPW), |
| 572 |
o2.isRegType(REG_GPQ), |
| 573 |
((Register) o2).code(), |
| 574 |
o1, |
| 575 |
0); |
| 576 |
return; |
| 577 |
} |
| 578 |
|
| 579 |
// Reg <- Reg|Mem |
| 580 |
if (o1.isReg() && o2.isRegMem()) |
| 581 |
{ |
| 582 |
_emitX86RM(opCode + 2 + intValue(!o1.isRegType(REG_GPB)), |
| 583 |
o1.isRegType(REG_GPW), |
| 584 |
o1.isRegType(REG_GPQ), |
| 585 |
((Register) o1).code(), |
| 586 |
o2, |
| 587 |
0); |
| 588 |
return; |
| 589 |
} |
| 590 |
|
| 591 |
// AL, AX, EAX, RAX register shortcuts |
| 592 |
if (o1.isRegIndex(0) && o2.isImm()) |
| 593 |
{ |
| 594 |
if (o1.isRegType(REG_GPW)) |
| 595 |
_emitByte(0x66); // 16 bit |
| 596 |
else if (o1.isRegType(REG_GPQ)) |
| 597 |
_emitByte(0x48); // REX.W |
| 598 |
|
| 599 |
_emitByte((opReg << 3) | (0x04 + intValue(!o1.isRegType(REG_GPB)))); |
| 600 |
_emitImmediate( |
| 601 |
(Immediate) o2, o1.size() <= 4 ? o1.size() : 4); |
| 602 |
return; |
| 603 |
} |
| 604 |
|
| 605 |
if (o1.isRegMem() && o2.isImm()) |
| 606 |
{ |
| 607 |
final Immediate imm = (Immediate) o2; |
| 608 |
int immSize = Util.isInt8(imm.value()) ? 1 : (o1.size() <= 4 ? o1.size() : 4); |
| 609 |
|
| 610 |
_emitX86RM(id.opCode2 + (o1.size() != 1 ? (immSize != 1 ? 1 : 3) : 0), |
| 611 |
o1.size() == 2, |
| 612 |
o1.size() == 8, |
| 613 |
opReg, o1, |
| 614 |
immSize); |
| 615 |
_emitImmediate( |
| 616 |
(Immediate) o2, |
| 617 |
immSize); |
| 618 |
return; |
| 619 |
} |
| 620 |
|
| 621 |
break; |
| 622 |
} |
| 623 |
|
| 624 |
case I_BSWAP: |
| 625 |
{ |
| 626 |
if (o1.isReg()) |
| 627 |
{ |
| 628 |
final Register dst = (Register) o1; |
| 629 |
|
| 630 |
if (is64()) { |
| 631 |
_emitRexR(dst.type() == REG_GPQ, 1, dst.code()); |
| 632 |
} |
| 633 |
|
| 634 |
_emitByte(0x0F); |
| 635 |
_emitModR(1, dst.code()); |
| 636 |
return; |
| 637 |
} |
| 638 |
|
| 639 |
break; |
| 640 |
} |
| 641 |
|
| 642 |
case I_BT: |
| 643 |
{ |
| 644 |
if (o1.isRegMem() && o2.isReg()) |
| 645 |
{ |
| 646 |
final Operand dst = o1; |
| 647 |
final Register src = (Register) o2; |
| 648 |
|
| 649 |
_emitX86RM(id.opCode1, |
| 650 |
src.isRegType(REG_GPW), |
| 651 |
src.isRegType(REG_GPQ), |
| 652 |
src.code(), |
| 653 |
dst, |
| 654 |
0); |
| 655 |
return; |
| 656 |
} |
| 657 |
|
| 658 |
if (o1.isRegMem() && o2.isImm()) |
| 659 |
{ |
| 660 |
final Operand dst = o1; |
| 661 |
final Immediate src = (Immediate) o2; |
| 662 |
|
| 663 |
_emitX86RM(id.opCode2, |
| 664 |
src.size() == 2, |
| 665 |
src.size() == 8, |
| 666 |
id.opCodeR, |
| 667 |
dst, |
| 668 |
1); |
| 669 |
_emitImmediate(src, 1); |
| 670 |
return; |
| 671 |
} |
| 672 |
|
| 673 |
break; |
| 674 |
} |
| 675 |
|
| 676 |
case I_CALL: |
| 677 |
{ |
| 678 |
if (o1.isRegMem(is64() ? REG_GPQ : REG_GPD)) |
| 679 |
{ |
| 680 |
final Operand dst = o1; |
| 681 |
_emitX86RM(0xFF, |
| 682 |
false, |
| 683 |
false, 2, dst, |
| 684 |
0); |
| 685 |
return; |
| 686 |
} |
| 687 |
|
| 688 |
if (o1.isImm()) |
| 689 |
{ |
| 690 |
final Immediate imm = (Immediate) o1; |
| 691 |
_emitByte(0xE8); |
| 692 |
_emitJmpOrCallReloc(I_CALL, imm.value()); |
| 693 |
return; |
| 694 |
} |
| 695 |
|
| 696 |
if (o1.isLabel()) |
| 697 |
{ |
| 698 |
Label label = (Label) o1; |
| 699 |
|
| 700 |
if (label.isBound()) |
| 701 |
{ |
| 702 |
final int rel32_size = 5; |
| 703 |
final int offs = label.position() - offset(); |
| 704 |
assert(offs <= 0); |
| 705 |
|
| 706 |
_emitByte(0xE8); |
| 707 |
_emitInt32((int)(offs - rel32_size)); |
| 708 |
} |
| 709 |
else |
| 710 |
{ |
| 711 |
_emitByte(0xE8); |
| 712 |
_emitDisplacement(label, -4, 4); |
| 713 |
} |
| 714 |
return; |
| 715 |
} |
| 716 |
|
| 717 |
break; |
| 718 |
} |
| 719 |
|
| 720 |
case I_CRC32: |
| 721 |
{ |
| 722 |
if (o1.isReg() && o2.isRegMem()) |
| 723 |
{ |
| 724 |
final Register dst = (Register) o1; |
| 725 |
final Operand src = o2; |
| 726 |
assert(dst.type() == REG_GPD || dst.type() == REG_GPQ); |
| 727 |
|
| 728 |
_emitX86RM(id.opCode1 + intValue(src.size() != 1), |
| 729 |
src.size() == 2, |
| 730 |
dst.type() == 8, dst.code(), src, |
| 731 |
0); |
| 732 |
return; |
| 733 |
} |
| 734 |
|
| 735 |
break; |
| 736 |
} |
| 737 |
|
| 738 |
case I_ENTER: |
| 739 |
{ |
| 740 |
if (o1.isImm() && o2.isImm()) |
| 741 |
{ |
| 742 |
_emitByte(0xC8); |
| 743 |
_emitImmediate((Immediate) o1, 2); |
| 744 |
_emitImmediate((Immediate) o2, 1); |
| 745 |
} |
| 746 |
break; |
| 747 |
} |
| 748 |
|
| 749 |
case I_IMUL: |
| 750 |
{ |
| 751 |
// 1 operand |
| 752 |
if (o1.isRegMem() && o2.isNone() && o3.isNone()) |
| 753 |
{ |
| 754 |
final Operand src = o1; |
| 755 |
_emitX86RM(0xF6 + intValue(src.size() != 1), |
| 756 |
src.size() == 2, |
| 757 |
src.size() == 8, 5, src, |
| 758 |
0); |
| 759 |
return; |
| 760 |
} |
| 761 |
// 2 operands |
| 762 |
else if (o1.isReg() && !o2.isNone() && o3.isNone()) |
| 763 |
{ |
| 764 |
final Register dst = (Register) o1; |
| 765 |
assert(!dst.isRegType(REG_GPW)); |
| 766 |
|
| 767 |
if (o2.isRegMem()) |
| 768 |
{ |
| 769 |
final Operand src = o2; |
| 770 |
|
| 771 |
_emitX86RM(0x0FAF, |
| 772 |
dst.isRegType(REG_GPW), |
| 773 |
dst.isRegType(REG_GPQ), dst.code(), src, |
| 774 |
0); |
| 775 |
return; |
| 776 |
} |
| 777 |
else if (o2.isImm()) |
| 778 |
{ |
| 779 |
final Immediate imm = (Immediate) o2; |
| 780 |
|
| 781 |
if (isInt8(imm.value()) && imm.relocMode() == RELOC_NONE) |
| 782 |
{ |
| 783 |
_emitX86RM(0x6B, |
| 784 |
dst.isRegType(REG_GPW), |
| 785 |
dst.isRegType(REG_GPQ), dst.code(), dst, |
| 786 |
1); |
| 787 |
_emitImmediate(imm, 1); |
| 788 |
} |
| 789 |
else |
| 790 |
{ |
| 791 |
int immSize = dst.isRegType(REG_GPW) ? 2 : 4; |
| 792 |
_emitX86RM(0x69, |
| 793 |
dst.isRegType(REG_GPW), |
| 794 |
dst.isRegType(REG_GPQ), dst.code(), dst, |
| 795 |
immSize); |
| 796 |
_emitImmediate(imm, immSize); |
| 797 |
} |
| 798 |
return; |
| 799 |
} |
| 800 |
} |
| 801 |
// 3 operands |
| 802 |
else if (o1.isReg() && o2.isRegMem() && o3.isImm()) |
| 803 |
{ |
| 804 |
final Register dst = (Register) o1; |
| 805 |
final Operand src = o2; |
| 806 |
final Immediate imm = (Immediate) o3; |
| 807 |
|
| 808 |
if (isInt8(imm.value()) && imm.relocMode() == RELOC_NONE) |
| 809 |
{ |
| 810 |
_emitX86RM(0x6B, |
| 811 |
dst.isRegType(REG_GPW), |
| 812 |
dst.isRegType(REG_GPQ), dst.code(), src, |
| 813 |
1); |
| 814 |
_emitImmediate(imm, 1); |
| 815 |
} |
| 816 |
else |
| 817 |
{ |
| 818 |
int immSize = dst.isRegType(REG_GPW) ? 2 : 4; |
| 819 |
_emitX86RM(0x69, |
| 820 |
dst.isRegType(REG_GPW), |
| 821 |
dst.isRegType(REG_GPQ), dst.code(), src, |
| 822 |
immSize); |
| 823 |
_emitImmediate(imm, immSize); |
| 824 |
} |
| 825 |
return; |
| 826 |
} |
| 827 |
|
| 828 |
break; |
| 829 |
} |
| 830 |
|
| 831 |
case I_INC_DEC: |
| 832 |
{ |
| 833 |
if (o1.isRegMem()) |
| 834 |
{ |
| 835 |
final Operand dst = o1; |
| 836 |
|
| 837 |
// INC [r16|r32] in 64 bit mode is not encodable. |
| 838 |
if (!is64() && dst.isReg() && (dst.isRegType(REG_GPW) || dst.isRegType(REG_GPD))) |
| 839 |
{ |
| 840 |
_emitX86Inl(id.opCode1, |
| 841 |
dst.isRegType(REG_GPW), |
| 842 |
0, ((BaseReg) dst).code()); |
| 843 |
return; |
| 844 |
} |
| 845 |
|
| 846 |
_emitX86RM(id.opCode2 + intValue(dst.size() != 1), |
| 847 |
dst.size() == 2, |
| 848 |
dst.size() == 8, id.opCodeR, dst, |
| 849 |
0); |
| 850 |
return; |
| 851 |
} |
| 852 |
|
| 853 |
break; |
| 854 |
} |
| 855 |
|
| 856 |
case I_J: |
| 857 |
{ |
| 858 |
if (o1.isLabel()) |
| 859 |
{ |
| 860 |
Label label = (Label) o1; |
| 861 |
|
| 862 |
boolean isShortJump = (code.ordinal() >= INST_J_SHORT.ordinal() && code.ordinal() <= INST_JMP_SHORT.ordinal()); |
| 863 |
|
| 864 |
final HINT hint = o2.isImm() ? HINT.valueOf((int) ((Immediate) o2).value()) : HINT.HINT_NONE; |
| 865 |
|
| 866 |
|
| 867 |
// Emit jump hint if configured for that. |
| 868 |
if (hint == HINT_TAKEN || hint == HINT_NOT_TAKEN |
| 869 |
&& (_properties & (1 << PROPERTY_X86_JCC_HINTS)) != 0) |
| 870 |
{ |
| 871 |
_emitByte(hint.value()); |
| 872 |
} |
| 873 |
|
| 874 |
if (label.isBound()) |
| 875 |
{ |
| 876 |
final int rel8_size = 2; |
| 877 |
final int rel32_size = 6; |
| 878 |
final int offs = label.position() - offset(); |
| 879 |
|
| 880 |
assert(offs <= 0); |
| 881 |
|
| 882 |
if (isInt8(offs - rel8_size)) |
| 883 |
{ |
| 884 |
_emitByte(0x70 | (id.opCode1 & 0xff)); |
| 885 |
_emitByte((byte) (offs - rel8_size)); |
| 886 |
} |
| 887 |
else |
| 888 |
{ |
| 889 |
if (isShortJump && _logger != null) |
| 890 |
{ |
| 891 |
_logger.log("; WARNING: Emitting long conditional jump, but short jump instruction forced!"); |
| 892 |
} |
| 893 |
|
| 894 |
_emitByte(0x0F); |
| 895 |
_emitByte(0x80 | (id.opCode1 & 0xff)); |
| 896 |
_emitInt32((int) (offs - rel32_size)); |
| 897 |
} |
| 898 |
} |
| 899 |
else |
| 900 |
{ |
| 901 |
if (isShortJump) |
| 902 |
{ |
| 903 |
_emitByte(0x70 | (id.opCode1 & 0xff)); |
| 904 |
_emitDisplacement(label, -1, 1); |
| 905 |
} |
| 906 |
else |
| 907 |
{ |
| 908 |
_emitByte(0x0F); |
| 909 |
_emitByte(0x80 | (id.opCode1 & 0xff)); |
| 910 |
_emitDisplacement(label, -4, 4); |
| 911 |
} |
| 912 |
} |
| 913 |
return; |
| 914 |
} |
| 915 |
|
| 916 |
break; |
| 917 |
} |
| 918 |
|
| 919 |
case I_JMP: |
| 920 |
{ |
| 921 |
if (o1.isRegMem()) |
| 922 |
{ |
| 923 |
final Operand dst = o1; |
| 924 |
|
| 925 |
_emitX86RM(0xFF, |
| 926 |
false, |
| 927 |
false, 4, dst, |
| 928 |
0); |
| 929 |
return; |
| 930 |
} |
| 931 |
|
| 932 |
if (o1.isImm()) |
| 933 |
{ |
| 934 |
final Immediate imm = (Immediate) o1; |
| 935 |
_emitByte(0xE9); |
| 936 |
_emitJmpOrCallReloc(I_JMP, imm.value()); |
| 937 |
return; |
| 938 |
} |
| 939 |
|
| 940 |
if (o1.isLabel()) |
| 941 |
{ |
| 942 |
Label label = (Label) o1; |
| 943 |
boolean isShortJump = (code == INST_JMP_SHORT); |
| 944 |
|
| 945 |
if (label.isBound()) |
| 946 |
{ |
| 947 |
final int rel8_size = 2; |
| 948 |
final int rel32_size = 5; |
| 949 |
final int offs = label.position() - offset(); |
| 950 |
|
| 951 |
if (isInt8(offs - rel8_size)) |
| 952 |
{ |
| 953 |
_emitByte(0xEB); |
| 954 |
_emitByte((byte) (offs - rel8_size)); |
| 955 |
} |
| 956 |
else |
| 957 |
{ |
| 958 |
if (isShortJump && _logger != null) |
| 959 |
{ |
| 960 |
_logger.log("; WARNING: Emitting long jump, but short jump instruction forced!"); |
| 961 |
} |
| 962 |
|
| 963 |
_emitByte(0xE9); |
| 964 |
_emitInt32((int)(offs - rel32_size)); |
| 965 |
} |
| 966 |
} |
| 967 |
else |
| 968 |
{ |
| 969 |
if (isShortJump) |
| 970 |
{ |
| 971 |
_emitByte(0xEB); |
| 972 |
_emitDisplacement(label, -1, 1); |
| 973 |
} |
| 974 |
else |
| 975 |
{ |
| 976 |
_emitByte(0xE9); |
| 977 |
_emitDisplacement(label, -4, 4); |
| 978 |
} |
| 979 |
} |
| 980 |
return; |
| 981 |
} |
| 982 |
|
| 983 |
break; |
| 984 |
} |
| 985 |
|
| 986 |
case I_LEA: |
| 987 |
{ |
| 988 |
if (o1.isReg() && o2.isMem()) |
| 989 |
{ |
| 990 |
final Register dst = (Register) o1; |
| 991 |
final Mem src = (Mem) o2; |
| 992 |
_emitX86RM(0x8D, |
| 993 |
dst.isRegType(REG_GPW), |
| 994 |
dst.isRegType(REG_GPQ), dst.code(), src, |
| 995 |
0); |
| 996 |
return; |
| 997 |
} |
| 998 |
|
| 999 |
break; |
| 1000 |
} |
| 1001 |
|
| 1002 |
case I_M: |
| 1003 |
{ |
| 1004 |
if (o1.isMem()) |
| 1005 |
{ |
| 1006 |
_emitX86RM(id.opCode1, false, (byte) id.opCode2, id.opCodeR, (Mem) o1, 0); |
| 1007 |
return; |
| 1008 |
} |
| 1009 |
break; |
| 1010 |
} |
| 1011 |
|
| 1012 |
case I_MOV: |
| 1013 |
{ |
| 1014 |
final Operand dst = o1; |
| 1015 |
final Operand src = o2; |
| 1016 |
|
| 1017 |
switch (dst.op() << 4 | src.op()) |
| 1018 |
{ |
| 1019 |
// Reg <- Reg/Mem |
| 1020 |
case (OP_REG << 4) | OP_REG: |
| 1021 |
{ |
| 1022 |
assert(src.isRegType(REG_GPB) || src.isRegType(REG_GPW) || |
| 1023 |
src.isRegType(REG_GPD) || src.isRegType(REG_GPQ)); |
| 1024 |
// ... fall through ... |
| 1025 |
} |
| 1026 |
case (OP_REG << 4) | OP_MEM: |
| 1027 |
{ |
| 1028 |
assert(dst.isRegType(REG_GPB) || dst.isRegType(REG_GPW) || |
| 1029 |
dst.isRegType(REG_GPD) || dst.isRegType(REG_GPQ)); |
| 1030 |
|
| 1031 |
_emitX86RM(0x0000008A + intValue(!dst.isRegType(REG_GPB)), |
| 1032 |
dst.isRegType(REG_GPW), |
| 1033 |
dst.isRegType(REG_GPQ), |
| 1034 |
((Register) dst).code(), |
| 1035 |
src, |
| 1036 |
0); |
| 1037 |
return; |
| 1038 |
} |
| 1039 |
|
| 1040 |
// Reg <- Imm |
| 1041 |
case (OP_REG << 4) | OP_IMM: |
| 1042 |
{ |
| 1043 |
final Immediate isrc = (Immediate) o2; |
| 1044 |
|
| 1045 |
// in 64 bit mode immediate can be 8 byte long! |
| 1046 |
int immSize = dst.size(); |
| 1047 |
|
| 1048 |
// Optimize instruction size by using 32 bit immediate if value can |
| 1049 |
// fit to it |
| 1050 |
if (is64() && immSize == 8 && isInt32(isrc.value()) && isrc.relocMode() == RELOC_NONE) |
| 1051 |
{ |
| 1052 |
_emitX86RM(0xC7, |
| 1053 |
dst.isRegType(REG_GPW), |
| 1054 |
dst.isRegType(REG_GPQ), |
| 1055 |
0, |
| 1056 |
dst, |
| 1057 |
0); |
| 1058 |
immSize = 4; |
| 1059 |
} |
| 1060 |
else |
| 1061 |
{ |
| 1062 |
_emitX86Inl((dst.size() == 1 ? 0xB0 : 0xB8), |
| 1063 |
dst.isRegType(REG_GPW), |
| 1064 |
dst.isRegType(REG_GPQ), |
| 1065 |
((Register) dst).code()); |
| 1066 |
} |
| 1067 |
|
| 1068 |
_emitImmediate(isrc, immSize); |
| 1069 |
return; |
| 1070 |
} |
| 1071 |
|
| 1072 |
// Mem <- Reg |
| 1073 |
case (OP_MEM << 4) | OP_REG: |
| 1074 |
{ |
| 1075 |
assert(src.isRegType(REG_GPB) || src.isRegType(REG_GPW) || |
| 1076 |
src.isRegType(REG_GPD) || src.isRegType(REG_GPQ)); |
| 1077 |
|
| 1078 |
_emitX86RM(0x88 + intValue(!src.isRegType(REG_GPB)), |
| 1079 |
src.isRegType(REG_GPW), |
| 1080 |
src.isRegType(REG_GPQ), |
| 1081 |
((Register) src).code(), |
| 1082 |
dst, |
| 1083 |
0); |
| 1084 |
return; |
| 1085 |
} |
| 1086 |
|
| 1087 |
// Mem <- Imm |
| 1088 |
case (OP_MEM << 4) | OP_IMM: |
| 1089 |
{ |
| 1090 |
int immSize = dst.size() <= 4 ? dst.size() : 4; |
| 1091 |
|
| 1092 |
_emitX86RM(0xC6 + intValue(dst.size() != 1), |
| 1093 |
dst.size() == 2, |
| 1094 |
dst.size() == 8, |
| 1095 |
0, |
| 1096 |
dst, |
| 1097 |
immSize); |
| 1098 |
_emitImmediate((Immediate) src, |
| 1099 |
immSize); |
| 1100 |
return; |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|
| 1104 |
break; |
| 1105 |
} |
| 1106 |
|
| 1107 |
case I_MOV_PTR: |
| 1108 |
{ |
| 1109 |
if ((o1.isReg() && o2.isImm()) || (o1.isImm() && o2.isReg())) |
| 1110 |
{ |
| 1111 |
boolean reverse = o1.op() == OP_REG; |
| 1112 |
int opCode = !reverse ? 0xA0 : 0xA2; |
| 1113 |
final Register reg = (Register)(!reverse ? o1 : o2); |
| 1114 |
final Immediate imm = (Immediate)(!reverse ? o2 : o1); |
| 1115 |
|
| 1116 |
if (reg.index() != 0) throw new IllegalStateException("reg.index() != 0"); |
| 1117 |
|
| 1118 |
if (reg.isRegType(REG_GPW)) _emitByte(0x66); |
| 1119 |
|
| 1120 |
if (is64()) { |
| 1121 |
_emitRexR(reg.size() == 8, 0, 0); |
| 1122 |
} |
| 1123 |
|
| 1124 |
_emitByte(opCode + intValue(reg.size() != 1)); |
| 1125 |
_emitImmediate(imm, is64() ? 8 : 4); |
| 1126 |
return; |
| 1127 |
} |
| 1128 |
|
| 1129 |
break; |
| 1130 |
} |
| 1131 |
|
| 1132 |
case I_MOVSX_MOVZX: |
| 1133 |
{ |
| 1134 |
if (o1.isReg() && o2.isRegMem()) |
| 1135 |
{ |
| 1136 |
final Register dst = (Register)(o1); |
| 1137 |
final Operand src = (o2); |
| 1138 |
|
| 1139 |
if (dst.isRegType(REG_GPB)) throw new IllegalArgumentException("not gpb"); |
| 1140 |
if (src.size() != 1 && src.size() != 2) throw new IllegalArgumentException("src.size !=1 && src.size != 2"); |
| 1141 |
if (src.size() == 2 && dst.isRegType(REG_GPW)) throw new IllegalArgumentException("not gpw"); |
| 1142 |
|
| 1143 |
_emitX86RM(id.opCode1 + intValue(src.size() != 1), |
| 1144 |
dst.isRegType(REG_GPW), |
| 1145 |
dst.isRegType(REG_GPQ), |
| 1146 |
dst.code(), |
| 1147 |
src, |
| 1148 |
0); |
| 1149 |
return; |
| 1150 |
} |
| 1151 |
|
| 1152 |
break; |
| 1153 |
} |
| 1154 |
|
| 1155 |
case I_MOVSXD: |
| 1156 |
{ |
| 1157 |
if (!is64()) { |
| 1158 |
throw new IllegalStateException("illegal instruction"); |
| 1159 |
} |
| 1160 |
if (o1.isReg() && o2.isRegMem()) |
| 1161 |
{ |
| 1162 |
final Register dst = (Register)(o1); |
| 1163 |
final Operand src = (o2); |
| 1164 |
_emitX86RM(0x00000063, |
| 1165 |
false, |
| 1166 |
1, dst.code(), src, |
| 1167 |
0); |
| 1168 |
return; |
| 1169 |
} |
| 1170 |
|
| 1171 |
break; |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
case I_PUSH: |
| 1176 |
{ |
| 1177 |
// This section is only for immediates, memory/register operands are handled in I_POP. |
| 1178 |
if (o1.isImm()) |
| 1179 |
{ |
| 1180 |
final Immediate imm = (Immediate)(o1); |
| 1181 |
|
| 1182 |
if (isInt8(imm.value()) && imm.relocMode() == RELOC_NONE) |
| 1183 |
{ |
| 1184 |
_emitByte(0x6A); |
| 1185 |
_emitImmediate(imm, 1); |
| 1186 |
} |
| 1187 |
else |
| 1188 |
{ |
| 1189 |
_emitByte(0x68); |
| 1190 |
_emitImmediate(imm, 4); |
| 1191 |
} |
| 1192 |
return; |
| 1193 |
} |
| 1194 |
|
| 1195 |
// ... goto I_POP ... |
| 1196 |
} |
| 1197 |
|
| 1198 |
case I_POP: |
| 1199 |
{ |
| 1200 |
if (o1.isReg()) |
| 1201 |
{ |
| 1202 |
assert(o1.isRegType(REG_GPW) || o1.isRegType(is64() ? REG_GPQ : REG_GPD)); |
| 1203 |
_emitX86Inl(id.opCode1, o1.isRegType(REG_GPW), 0, ((Register) o1).code()); |
| 1204 |
return; |
| 1205 |
} |
| 1206 |
|
| 1207 |
if (o1.isMem()) |
| 1208 |
{ |
| 1209 |
_emitX86RM(id.opCode2, o1.size() == 2, 0, id.opCodeR, (o1), 0); |
| 1210 |
return; |
| 1211 |
} |
| 1212 |
|
| 1213 |
break; |
| 1214 |
} |
| 1215 |
|
| 1216 |
case I_R_RM: |
| 1217 |
{ |
| 1218 |
if (o1.isReg() && o2.isRegMem()) |
| 1219 |
{ |
| 1220 |
final Register dst = (Register)(o1); |
| 1221 |
assert(dst.type() != REG_GPB); |
| 1222 |
final Operand src = (o2); |
| 1223 |
|
| 1224 |
_emitX86RM(id.opCode1, |
| 1225 |
dst.type() == REG_GPW, |
| 1226 |
dst.type() == REG_GPQ, dst.code(), src, |
| 1227 |
0); |
| 1228 |
return; |
| 1229 |
} |
| 1230 |
|
| 1231 |
break; |
| 1232 |
} |
| 1233 |
|
| 1234 |
case I_RM_B: |
| 1235 |
{ |
| 1236 |
if (o1.isRegMem()) |
| 1237 |
{ |
| 1238 |
final Operand op = (o1); |
| 1239 |
_emitX86RM(id.opCode1, false, false, 0, op, 0); |
| 1240 |
return; |
| 1241 |
} |
| 1242 |
|
| 1243 |
break; |
| 1244 |
} |
| 1245 |
|
| 1246 |
case I_RM: |
| 1247 |
{ |
| 1248 |
if (o1.isRegMem()) |
| 1249 |
{ |
| 1250 |
final Operand op = (o1); |
| 1251 |
_emitX86RM(id.opCode1 + intValue(op.size() != 1), |
| 1252 |
op.size() == 2, |
| 1253 |
op.size() == 8, id.opCodeR, op, |
| 1254 |
0); |
| 1255 |
return; |
| 1256 |
} |
| 1257 |
|
| 1258 |
break; |
| 1259 |
} |
| 1260 |
|
| 1261 |
case I_RM_R: |
| 1262 |
{ |
| 1263 |
if (o1.isRegMem() && o2.isReg()) |
| 1264 |
{ |
| 1265 |
final Operand dst = (o1); |
| 1266 |
final Register src = (Register)(o2); |
| 1267 |
_emitX86RM(id.opCode1 + intValue(src.type() != REG_GPB), |
| 1268 |
src.type() == REG_GPW, |
| 1269 |
src.type() == REG_GPQ, src.code(), dst, |
| 1270 |
0); |
| 1271 |
return; |
| 1272 |
} |
| 1273 |
|
| 1274 |
break; |
| 1275 |
} |
| 1276 |
|
| 1277 |
case I_RET: |
| 1278 |
{ |
| 1279 |
if (o1.isNone()) |
| 1280 |
{ |
| 1281 |
_emitByte(0xC3); |
| 1282 |
return; |
| 1283 |
} |
| 1284 |
else if (o1.isImm()) |
| 1285 |
{ |
| 1286 |
final Immediate imm = (Immediate)(o1); |
| 1287 |
assert(isUInt16(imm.value())); |
| 1288 |
|
| 1289 |
if (imm.value() == 0 && imm.relocMode() == RELOC_NONE) |
| 1290 |
{ |
| 1291 |
_emitByte(0xC3); |
| 1292 |
} |
| 1293 |
else |
| 1294 |
{ |
| 1295 |
_emitByte(0xC2); |
| 1296 |
_emitImmediate(imm, 2); |
| 1297 |
} |
| 1298 |
return; |
| 1299 |
} |
| 1300 |
|
| 1301 |
break; |
| 1302 |
} |
| 1303 |
|
| 1304 |
case I_ROT: |
| 1305 |
{ |
| 1306 |
if (o1.isRegMem() && (o2.isRegCode(REG_CL) || o2.isImm())) |
| 1307 |
{ |
| 1308 |
// generate opcode. For these operations is base 0xC0 or 0xD0. |
| 1309 |
boolean useImm8 = (o2.isImm() && |
| 1310 |
(((Immediate) o2).value() != 1 || |
| 1311 |
((Immediate) o2).relocMode() != RELOC_NONE)); |
| 1312 |
int opCode = useImm8 ? 0xC0 : 0xD0; |
| 1313 |
|
| 1314 |
// size and operand type modifies the opcode |
| 1315 |
if (o1.size() != 1) opCode |= 0x01; |
| 1316 |
if (o2.op() == OP_REG) opCode |= 0x02; |
| 1317 |
|
| 1318 |
_emitX86RM(opCode, |
| 1319 |
o1.size() == 2, |
| 1320 |
o1.size() == 8, |
| 1321 |
id.opCodeR, (o1), |
| 1322 |
intValue(useImm8)); |
| 1323 |
if (useImm8) |
| 1324 |
_emitImmediate((Immediate)(o2), 1); |
| 1325 |
return; |
| 1326 |
} |
| 1327 |
|
| 1328 |
break; |
| 1329 |
} |
| 1330 |
|
| 1331 |
case I_SHLD_SHRD: |
| 1332 |
{ |
| 1333 |
if (o1.isRegMem() && o2.isReg() && (o3.isImm() || (o3.isReg() && o3.isRegCode(REG_CL)))) |
| 1334 |
{ |
| 1335 |
final Operand dst = (o1); |
| 1336 |
final Register src1 = (Register)(o2); |
| 1337 |
final Operand src2 = (o3); |
| 1338 |
|
| 1339 |
assert(dst.size() == src1.size()); |
| 1340 |
|
| 1341 |
_emitX86RM(id.opCode1 + intValue(src2.isReg()), |
| 1342 |
src1.isRegType(REG_GPW), |
| 1343 |
src1.isRegType(REG_GPQ), |
| 1344 |
src1.code(), dst, |
| 1345 |
intValue(src2.isImm())); |
| 1346 |
if (src2.isImm()) |
| 1347 |
_emitImmediate((Immediate)(src2), 1); |
| 1348 |
return; |
| 1349 |
} |
| 1350 |
|
| 1351 |
break; |
| 1352 |
} |
| 1353 |
|
| 1354 |
case I_TEST: |
| 1355 |
{ |
| 1356 |
if (o1.isRegMem() && o2.isReg()) |
| 1357 |
{ |
| 1358 |
assert(o1.size() == o2.size()); |
| 1359 |
_emitX86RM(0x84 + intValue(o2.size() != 1), |
| 1360 |
o2.size() == 2, o2.size() == 8, |
| 1361 |
((BaseReg) o2).code(), |
| 1362 |
(o1), |
| 1363 |
0); |
| 1364 |
return; |
| 1365 |
} |
| 1366 |
|
| 1367 |
if (o1.isRegIndex(0) && o2.isImm()) |
| 1368 |
{ |
| 1369 |
int immSize = o1.size() <= 4 ? o1.size() : 4; |
| 1370 |
|
| 1371 |
if (o1.size() == 2) _emitByte(0x66); // 16 bit |
| 1372 |
|
| 1373 |
if (is64()) { |
| 1374 |
_emitRexRM(o1.size() == 8, 0, (o1)); |
| 1375 |
} |
| 1376 |
|
| 1377 |
_emitByte(0xA8 + intValue(o1.size() != 1)); |
| 1378 |
_emitImmediate((Immediate)(o2), immSize); |
| 1379 |
return; |
| 1380 |
} |
| 1381 |
|
| 1382 |
if (o1.isRegMem() && o2.isImm()) |
| 1383 |
{ |
| 1384 |
int immSize = o1.size() <= 4 ? o1.size() : 4; |
| 1385 |
|
| 1386 |
if (o1.size() == 2) _emitByte(0x66); // 16 bit |
| 1387 |
_emitSegmentPrefix((o1)); // segment prefix |
| 1388 |
|
| 1389 |
if (is64()) _emitRexRM(o1.size() == 8, 0, (o1)); |
| 1390 |
|
| 1391 |
_emitByte(0xF6 + intValue(o1.size() != 1)); |
| 1392 |
_emitModRM(0, (o1), immSize); |
| 1393 |
_emitImmediate((Immediate)(o2), immSize); |
| 1394 |
return; |
| 1395 |
} |
| 1396 |
|
| 1397 |
break; |
| 1398 |
} |
| 1399 |
|
| 1400 |
case I_XCHG: |
| 1401 |
{ |
| 1402 |
if (o1.isRegMem() && o2.isReg()) |
| 1403 |
{ |
| 1404 |
final Operand dst = (o1); |
| 1405 |
final Register src = (Register)(o2); |
| 1406 |
|
| 1407 |
if (src.isRegType(REG_GPW)) _emitByte(0x66); // 16 bit |
| 1408 |
_emitSegmentPrefix(dst); // segment prefix |
| 1409 |
|
| 1410 |
if (is64()) _emitRexRM(src.isRegType(REG_GPQ), src.code(), dst); |
| 1411 |
|
| 1412 |
// Special opcode for index 0 registers (AX, EAX, RAX vs register) |
| 1413 |
if ((dst.op() == OP_REG && dst.size() > 1) && |
| 1414 |
(((Register) dst).code() == 0 || |
| 1415 |
((Register) src).code() == 0)) |
| 1416 |
{ |
| 1417 |
int index = ((Register) dst).code() | src.code(); |
| 1418 |
_emitByte((byte) (0x90 + index)); |
| 1419 |
return; |
| 1420 |
} |
| 1421 |
|
| 1422 |
_emitByte(0x86 + intValue(!src.isRegType(REG_GPB))); |
| 1423 |
_emitModRM(src.code(), dst, 0); |
| 1424 |
return; |
| 1425 |
} |
| 1426 |
|
| 1427 |
break; |
| 1428 |
} |
| 1429 |
|
| 1430 |
case I_MOVBE: |
| 1431 |
{ |
| 1432 |
if (o1.isReg() && o2.isMem()) |
| 1433 |
{ |
| 1434 |
_emitX86RM(0x000F38F0, |
| 1435 |
o1.isRegType(REG_GPW), |
| 1436 |
o1.isRegType(REG_GPQ), |
| 1437 |
((Register) o1).code(), |
| 1438 |
(Mem)(o2), |
| 1439 |
0); |
| 1440 |
return; |
| 1441 |
} |
| 1442 |
|
| 1443 |
if (o1.isMem() && o2.isReg()) |
| 1444 |
{ |
| 1445 |
_emitX86RM(0x000F38F1, |
| 1446 |
o2.isRegType(REG_GPW), |
| 1447 |
o2.isRegType(REG_GPQ), |
| 1448 |
((Register) o2).code(), |
| 1449 |
(Mem)(o1), |
| 1450 |
0); |
| 1451 |
return; |
| 1452 |
} |
| 1453 |
|
| 1454 |
break; |
| 1455 |
} |
| 1456 |
|
| 1457 |
case I_X87_FPU: |
| 1458 |
{ |
| 1459 |
if (o1.isRegType(REG_X87)) |
| 1460 |
{ |
| 1461 |
int i1 = ((X87Register) o1).index(); |
| 1462 |
int i2 = 0; |
| 1463 |
|
| 1464 |
if (code != INST_FCOM && code != INST_FCOMP) |
| 1465 |
{ |
| 1466 |
if (!o2.isRegType(REG_X87)) throw new IllegalArgumentException("not x87 reg"); |
| 1467 |
i2 = ((X87Register) o2).index(); |
| 1468 |
} |
| 1469 |
else if (i1 != 0 && i2 != 0) |
| 1470 |
{ |
| 1471 |
throw new IllegalArgumentException("illegal instruction"); |
| 1472 |
} |
| 1473 |
|
| 1474 |
_emitByte(i1 == 0 |
| 1475 |
? ((id.opCode1 & 0xFF000000) >> 24) |
| 1476 |
: ((id.opCode1 & 0x00FF0000) >> 16)); |
| 1477 |
_emitByte(i1 == 0 |
| 1478 |
? ((id.opCode1 & 0x0000FF00) >> 8) + i2 |
| 1479 |
: ((id.opCode1 & 0x000000FF) ) + i1); |
| 1480 |
return; |
| 1481 |
} |
| 1482 |
|
| 1483 |
if (o1.isMem() && (o1.size() == 4 || o1.size() == 8) && o2.isNone()) |
| 1484 |
{ |
| 1485 |
final Mem m = (Mem)(o1); |
| 1486 |
|
| 1487 |
// segment prefix |
| 1488 |
_emitSegmentPrefix(m); |
| 1489 |
|
| 1490 |
_emitByte(o1.size() == 4 |
| 1491 |
? ((id.opCode1 & 0xFF000000) >> 24) |
| 1492 |
: ((id.opCode1 & 0x00FF0000) >> 16)); |
| 1493 |
_emitModM(id.opCodeR, m, 0); |
| 1494 |
return; |
| 1495 |
} |
| 1496 |
|
| 1497 |
break; |
| 1498 |
} |
| 1499 |
|
| 1500 |
case I_X87_STI: |
| 1501 |
{ |
| 1502 |
if (o1.isRegType(REG_X87)) |
| 1503 |
{ |
| 1504 |
int i = ((X87Register) o1).index(); |
| 1505 |
_emitByte(((id.opCode1 & 0x0000FF00) >> 8)); |
| 1506 |
_emitByte(((id.opCode1 & 0x000000FF) + i)); |
| 1507 |
return; |
| 1508 |
} |
| 1509 |
break; |
| 1510 |
} |
| 1511 |
|
| 1512 |
case I_X87_FSTSW: |
| 1513 |
{ |
| 1514 |
if (o1.isReg() && |
| 1515 |
((BaseReg) o1).type() <= REG_GPQ && |
| 1516 |
((BaseReg) o1).index() == 0) |
| 1517 |
{ |
| 1518 |
_emitOpCode(id.opCode2); |
| 1519 |
return; |
| 1520 |
} |
| 1521 |
|
| 1522 |
if (o1.isMem()) |
| 1523 |
{ |
| 1524 |
_emitX86RM(id.opCode1, false, 0, id.opCodeR, (Mem)(o1), 0); |
| 1525 |
return; |
| 1526 |
} |
| 1527 |
|
| 1528 |
break; |
| 1529 |
} |
| 1530 |
|
| 1531 |
case I_X87_MEM_STI: |
| 1532 |
{ |
| 1533 |
if (o1.isRegType(REG_X87)) |
| 1534 |
{ |
| 1535 |
_emitByte(((id.opCode2 & 0xFF000000) >> 24)); |
| 1536 |
_emitByte(((id.opCode2 & 0x00FF0000) >> 16) + |
| 1537 |
((X87Register) o1).index()); |
| 1538 |
return; |
| 1539 |
} |
| 1540 |
|
| 1541 |
// ... fall through to I_X87_MEM ... |
| 1542 |
} |
| 1543 |
|
| 1544 |
case I_X87_MEM: |
| 1545 |
{ |
| 1546 |
if (!o1.isMem()) throw new IllegalArgumentException("not x87 mem"); |
| 1547 |
final Mem m = (Mem)(o1); |
| 1548 |
|
| 1549 |
int opCode = 0x00, mod = 0; |
| 1550 |
|
| 1551 |
if (o1.size() == 2 && (id.o1Flags & O_FM_2) != 0) |
| 1552 |
{ |
| 1553 |
opCode = ((id.opCode1 & 0xFF000000) >> 24); |
| 1554 |
mod = id.opCodeR; |
| 1555 |
} |
| 1556 |
if (o1.size() == 4 && (id.o1Flags & O_FM_4) != 0) |
| 1557 |
{ |
| 1558 |
opCode = ((id.opCode1 & 0x00FF0000) >> 16); |
| 1559 |
mod = id.opCodeR; |
| 1560 |
} |
| 1561 |
if (o1.size() == 8 && (id.o1Flags & O_FM_8) != 0) |
| 1562 |
{ |
| 1563 |
opCode = ((id.opCode1 & 0x0000FF00) >> 8); |
| 1564 |
mod = ((id.opCode1 & 0x000000FF) ); |
| 1565 |
} |
| 1566 |
|
| 1567 |
if (opCode != 0) |
| 1568 |
{ |
| 1569 |
_emitSegmentPrefix(m); |
| 1570 |
_emitByte(opCode); |
| 1571 |
_emitModM(mod, m, 0); |
| 1572 |
return; |
| 1573 |
} |
| 1574 |
|
| 1575 |
break; |
| 1576 |
} |
| 1577 |
|
| 1578 |
case I_MMU_MOV: |
| 1579 |
{ |
| 1580 |
assert(id.o1Flags != 0); |
| 1581 |
assert(id.o2Flags != 0); |
| 1582 |
|
| 1583 |
// Check parameters (X)MM|GP32_64 <- (X)MM|GP32_64|Mem|Imm |
| 1584 |
if ((o1.isMem() && (id.o1Flags & O_MEM) == 0) || |
| 1585 |
(o1.isRegType(REG_MM ) && (id.o1Flags & O_MM ) == 0) || |
| 1586 |
(o1.isRegType(REG_XMM) && (id.o1Flags & O_XMM) == 0) || |
| 1587 |
(o1.isRegType(REG_GPD) && (id.o1Flags & O_G32) == 0) || |
| 1588 |
(o1.isRegType(REG_GPQ) && (id.o1Flags & O_G64) == 0) || |
| 1589 |
(o2.isRegType(REG_MM ) && (id.o2Flags & O_MM ) == 0) || |
| 1590 |
(o2.isRegType(REG_XMM) && (id.o2Flags & O_XMM) == 0) || |
| 1591 |
(o2.isRegType(REG_GPD) && (id.o2Flags & O_G32) == 0) || |
| 1592 |
(o2.isRegType(REG_GPQ) && (id.o2Flags & O_G64) == 0) || |
| 1593 |
(o2.isMem() && (id.o2Flags & O_MEM) == 0) ) |
| 1594 |
{ |
| 1595 |
throw new IllegalArgumentException("illegal instruction"); |
| 1596 |
} |
| 1597 |
|
| 1598 |
// Illegal |
| 1599 |
if (o1.isMem() && o2.isMem()) throw new IllegalArgumentException("illegal instruction"); |
| 1600 |
|
| 1601 |
int rexw = ((id.o1Flags|id.o2Flags) & O_NOREX) != 0 |
| 1602 |
? 0 |
| 1603 |
: intValue(o1.isRegType(REG_GPQ) || o1.isRegType(REG_GPQ)); |
| 1604 |
|
| 1605 |
// (X)MM|Reg <- (X)MM|Reg |
| 1606 |
if (o1.isReg() && o2.isReg()) |
| 1607 |
{ |
| 1608 |
_emitMmu(id.opCode1, rexw, |
| 1609 |
((BaseReg) o1).code(), |
| 1610 |
(BaseReg)(o2), |
| 1611 |
0); |
| 1612 |
return; |
| 1613 |
} |
| 1614 |
|
| 1615 |
// (X)MM|Reg <- Mem |
| 1616 |
if (o1.isReg() && o2.isMem()) |
| 1617 |
{ |
| 1618 |
_emitMmu(id.opCode1, rexw, |
| 1619 |
((BaseReg) o1).code(), |
| 1620 |
(Mem)(o2), |
| 1621 |
0); |
| 1622 |
return; |
| 1623 |
} |
| 1624 |
|
| 1625 |
// Mem <- (X)MM|Reg |
| 1626 |
if (o1.isMem() && o2.isReg()) |
| 1627 |
{ |
| 1628 |
_emitMmu(id.opCode2, rexw, |
| 1629 |
((BaseReg) o2).code(), |
| 1630 |
(Mem)(o1), |
| 1631 |
0); |
| 1632 |
return; |
| 1633 |
} |
| 1634 |
|
| 1635 |
break; |
| 1636 |
} |
| 1637 |
|
| 1638 |
case I_MMU_MOVD: |
| 1639 |
{ |
| 1640 |
if ((o1.isRegType(REG_MM) || o1.isRegType(REG_XMM)) && (o2.isRegType(REG_GPD) || o2.isMem())) |
| 1641 |
{ |
| 1642 |
_emitMmu(o1.isRegType(REG_XMM) ? 0x66000F6E : 0x00000F6E, 0, |
| 1643 |
((BaseReg) o1).code(), |
| 1644 |
(o2), |
| 1645 |
0); |
| 1646 |
return; |
| 1647 |
} |
| 1648 |
|
| 1649 |
if ((o1.isRegType(REG_GPD) || o1.isMem()) && (o2.isRegType(REG_MM) || o2.isRegType(REG_XMM))) |
| 1650 |
{ |
| 1651 |
_emitMmu(o2.isRegType(REG_XMM) ? 0x66000F7E : 0x00000F7E, 0, |
| 1652 |
((BaseReg) o2).code(), |
| 1653 |
(o1), |
| 1654 |
0); |
| 1655 |
return; |
| 1656 |
} |
| 1657 |
|
| 1658 |
break; |
| 1659 |
} |
| 1660 |
|
| 1661 |
case I_MMU_MOVQ: |
| 1662 |
{ |
| 1663 |
if (o1.isRegType(REG_MM) && o2.isRegType(REG_MM)) |
| 1664 |
{ |
| 1665 |
_emitMmu(0x00000F6F, 0, |
| 1666 |
((MMRegister) o1).code(), |
| 1667 |
(MMRegister)(o2), |
| 1668 |
0); |
| 1669 |
return; |
| 1670 |
} |
| 1671 |
|
| 1672 |
if (o1.isRegType(REG_XMM) && o2.isRegType(REG_XMM)) |
| 1673 |
{ |
| 1674 |
_emitMmu(0xF3000F7E, 0, |
| 1675 |
((XMMRegister) o1).code(), |
| 1676 |
(XMMRegister)(o2), |
| 1677 |
0); |
| 1678 |
return; |
| 1679 |
} |
| 1680 |
|
| 1681 |
// Convenience - movdq2q |
| 1682 |
if (o1.isRegType(REG_MM) && o2.isRegType(REG_XMM)) |
| 1683 |
{ |
| 1684 |
_emitMmu(0xF2000FD6, 0, |
| 1685 |
((MMRegister) o1).code(), |
| 1686 |
(XMMRegister)(o2), |
| 1687 |
0); |
| 1688 |
return; |
| 1689 |
} |
| 1690 |
|
| 1691 |
// Convenience - movq2dq |
| 1692 |
if (o1.isRegType(REG_XMM) && o2.isRegType(REG_MM)) |
| 1693 |
{ |
| 1694 |
_emitMmu(0xF3000FD6, 0, |
| 1695 |
((XMMRegister) o1).code(), |
| 1696 |
(MMRegister)(o2), |
| 1697 |
0); |
| 1698 |
return; |
| 1699 |
} |
| 1700 |
|
| 1701 |
if (o1.isRegType(REG_MM) && o2.isMem()) |
| 1702 |
{ |
| 1703 |
_emitMmu(0x00000F6F, 0, |
| 1704 |
((MMRegister) o1).code(), |
| 1705 |
(Mem)(o2), |
| 1706 |
0); |
| 1707 |
return; |
| 1708 |
} |
| 1709 |
|
| 1710 |
if (o1.isRegType(REG_XMM) && o2.isMem()) |
| 1711 |
{ |
| 1712 |
_emitMmu(0xF3000F7E, 0, |
| 1713 |
((XMMRegister) o1).code(), |
| 1714 |
(Mem)(o2), |
| 1715 |
0); |
| 1716 |
return; |
| 1717 |
} |
| 1718 |
|
| 1719 |
if (o1.isMem() && o2.isRegType(REG_MM)) |
| 1720 |
{ |
| 1721 |
_emitMmu(0x00000F7F, 0, |
| 1722 |
((MMRegister) o2).code(), |
| 1723 |
(Mem)(o1), |
| 1724 |
0); |
| 1725 |
return; |
| 1726 |
} |
| 1727 |
|
| 1728 |
if (o1.isMem() && o2.isRegType(REG_XMM)) |
| 1729 |
{ |
| 1730 |
_emitMmu(0x66000FD6, 0, |
| 1731 |
((XMMRegister) o2).code(), |
| 1732 |
(Mem)(o1), |
| 1733 |
0); |
| 1734 |
return; |
| 1735 |
} |
| 1736 |
|
| 1737 |
|
| 1738 |
if (is64()) { |
| 1739 |
if ((o1.isRegType(REG_MM) || o1.isRegType(REG_XMM)) && (o2.isRegType(REG_GPQ) || o2.isMem())) |
| 1740 |
{ |
| 1741 |
_emitMmu(o1.isRegType(REG_XMM) ? 0x66000F6E : 0x00000F6E, 1, |
| 1742 |
((BaseReg) o1).code(), |
| 1743 |
(o2), |
| 1744 |
0); |
| 1745 |
return; |
| 1746 |
} |
| 1747 |
|
| 1748 |
if ((o1.isRegType(REG_GPQ) || o1.isMem()) && (o2.isRegType(REG_MM) || o2.isRegType(REG_XMM))) |
| 1749 |
{ |
| 1750 |
_emitMmu(o2.isRegType(REG_XMM) ? 0x66000F7E : 0x00000F7E, 1, |
| 1751 |
((BaseReg) o2).code(), |
| 1752 |
(o1), |
| 1753 |
0); |
| 1754 |
return; |
| 1755 |
} |
| 1756 |
} |
| 1757 |
|
| 1758 |
break; |
| 1759 |
} |
| 1760 |
|
| 1761 |
case I_MMU_PREFETCH: |
| 1762 |
{ |
| 1763 |
if (o1.isMem() && o2.isImm()) |
| 1764 |
{ |
| 1765 |
final Mem mem = (Mem)(o1); |
| 1766 |
final Immediate hint = (Immediate)(o2); |
| 1767 |
|
| 1768 |
_emitMmu(0x00000F18, 0, (int) hint.value(), mem, 0); |
| 1769 |
return; |
| 1770 |
} |
| 1771 |
|
| 1772 |
break; |
| 1773 |
} |
| 1774 |
|
| 1775 |
case I_MMU_PEXTR: |
| 1776 |
{ |
| 1777 |
if (!(o1.isRegMem() && |
| 1778 |
(o2.isRegType(REG_XMM) || (code == INST_PEXTRW && o2.isRegType(REG_MM))) && |
| 1779 |
o3.isImm())) |
| 1780 |
{ |
| 1781 |
throw new IllegalStateException("illegal instruction"); |
| 1782 |
} |
| 1783 |
|
| 1784 |
int opCode = id.opCode1; |
| 1785 |
boolean isGpdGpq = o1.isRegType(REG_GPD) || o1.isRegType(REG_GPQ); |
| 1786 |
|
| 1787 |
if (code == INST_PEXTRB && (o1.size() != 0 && o1.size() != 1) && !isGpdGpq) throw new IllegalStateException("illegal instruction"); |
| 1788 |
if (code == INST_PEXTRW && (o1.size() != 0 && o1.size() != 2) && !isGpdGpq) throw new IllegalStateException("illegal instruction"); |
| 1789 |
if (code == INST_PEXTRD && (o1.size() != 0 && o1.size() != 4) && !isGpdGpq) throw new IllegalStateException("illegal instruction"); |
| 1790 |
if (code == INST_PEXTRQ && (o1.size() != 0 && o1.size() != 8) && !isGpdGpq) throw new IllegalStateException("illegal instruction"); |
| 1791 |
|
| 1792 |
if (o2.isRegType(REG_XMM)) opCode |= 0x66000000; |
| 1793 |
|
| 1794 |
if (o1.isReg()) |
| 1795 |
{ |
| 1796 |
_emitMmu(opCode, id.opCodeR | intValue(o1.isRegType(REG_GPQ)), |
| 1797 |
((BaseReg) o2).code(), |
| 1798 |
(BaseReg)(o1), 1); |
| 1799 |
_emitImmediate( |
| 1800 |
(Immediate)(o3), 1); |
| 1801 |
return; |
| 1802 |
} |
| 1803 |
|
| 1804 |
if (o1.isMem()) |
| 1805 |
{ |
| 1806 |
_emitMmu(opCode, id.opCodeR, |
| 1807 |
((BaseReg) o2).code(), |
| 1808 |
(Mem)(o1), 1); |
| 1809 |
_emitImmediate( |
| 1810 |
(Immediate)(o3), 1); |
| 1811 |
return; |
| 1812 |
} |
| 1813 |
|
| 1814 |
break; |
| 1815 |
} |
| 1816 |
|
| 1817 |
case I_MMU_RMI: |
| 1818 |
{ |
| 1819 |
assert(id.o1Flags != 0); |
| 1820 |
assert(id.o2Flags != 0); |
| 1821 |
|
| 1822 |
// Check parameters (X)MM|GP32_64 <- (X)MM|GP32_64|Mem|Imm |
| 1823 |
if (!o1.isReg() || |
| 1824 |
(o1.isRegType(REG_MM ) && (id.o1Flags & O_MM ) == 0) || |
| 1825 |
(o1.isRegType(REG_XMM) && (id.o1Flags & O_XMM) == 0) || |
| 1826 |
(o1.isRegType(REG_GPD) && (id.o1Flags & O_G32) == 0) || |
| 1827 |
(o1.isRegType(REG_GPQ) && (id.o1Flags & O_G64) == 0) || |
| 1828 |
(o2.isRegType(REG_MM ) && (id.o2Flags & O_MM ) == 0) || |
| 1829 |
(o2.isRegType(REG_XMM) && (id.o2Flags & O_XMM) == 0) || |
| 1830 |
(o2.isRegType(REG_GPD) && (id.o2Flags & O_G32) == 0) || |
| 1831 |
(o2.isRegType(REG_GPQ) && (id.o2Flags & O_G64) == 0) || |
| 1832 |
(o2.isMem() && (id.o2Flags & O_MEM) == 0) || |
| 1833 |
(o2.isImm() && (id.o2Flags & O_IMM) == 0)) |
| 1834 |
{ |
| 1835 |
throw new IllegalStateException("illegal instruction"); |
| 1836 |
} |
| 1837 |
|
| 1838 |
int prefix = |
| 1839 |
((id.o1Flags & O_MM_XMM) == O_MM_XMM && o1.isRegType(REG_XMM)) || |
| 1840 |
((id.o2Flags & O_MM_XMM) == O_MM_XMM && o2.isRegType(REG_XMM)) |
| 1841 |
? 0x66000000 |
| 1842 |
: 0x00000000; |
| 1843 |
int rexw = ((id.o1Flags|id.o2Flags) & O_NOREX) != 0 |
| 1844 |
? 0 |
| 1845 |
: intValue(o1.isRegType(REG_GPQ) || o1.isRegType(REG_GPQ)); |
| 1846 |
|
| 1847 |
// (X)MM <- (X)MM (opcode1) |
| 1848 |
if (o2.isReg()) |
| 1849 |
{ |
| 1850 |
if ((id.o2Flags & (O_MM_XMM | O_G32_64)) == 0) throw new IllegalStateException("illegal instruction"); |
| 1851 |
_emitMmu(id.opCode1 | prefix, rexw, |
| 1852 |
((BaseReg) o1).code(), |
| 1853 |
(BaseReg)(o2), 0); |
| 1854 |
return; |
| 1855 |
} |
| 1856 |
// (X)MM <- Mem (opcode1) |
| 1857 |
if (o2.isMem()) |
| 1858 |
{ |
| 1859 |
if ((id.o2Flags & O_MEM) == 0) throw new IllegalStateException("illegal instruction"); |
| 1860 |
_emitMmu(id.opCode1 | prefix, rexw, |
| 1861 |
((BaseReg) o1).code(), |
| 1862 |
(Mem)(o2), 0); |
| 1863 |
return; |
| 1864 |
} |
| 1865 |
// (X)MM <- Imm (opcode2+opcodeR) |
| 1866 |
if (o2.isImm()) |
| 1867 |
{ |
| 1868 |
if ((id.o2Flags & O_IMM) == 0) throw new IllegalStateException("illegal instruction"); |
| 1869 |
_emitMmu(id.opCode2 | prefix, rexw, |
| 1870 |
id.opCodeR, |
| 1871 |
(BaseReg)(o1), 1); |
| 1872 |
_emitImmediate( |
| 1873 |
(Immediate)(o2), 1); |
| 1874 |
return; |
| 1875 |
} |
| 1876 |
|
| 1877 |
break; |
| 1878 |
} |
| 1879 |
|
| 1880 |
case I_MMU_RM_IMM8: |
| 1881 |
{ |
| 1882 |
assert(id.o1Flags != 0); |
| 1883 |
assert(id.o2Flags != 0); |
| 1884 |
|
| 1885 |
// Check parameters (X)MM|GP32_64 <- (X)MM|GP32_64|Mem|Imm |
| 1886 |
if (!o1.isReg() || |
| 1887 |
(o1.isRegType(REG_MM ) && (id.o1Flags & O_MM ) == 0) || |
| 1888 |
(o1.isRegType(REG_XMM) && (id.o1Flags & O_XMM) == 0) || |
| 1889 |
(o1.isRegType(REG_GPD) && (id.o1Flags & O_G32) == 0) || |
| 1890 |
(o1.isRegType(REG_GPQ) && (id.o1Flags & O_G64) == 0) || |
| 1891 |
(o2.isRegType(REG_MM ) && (id.o2Flags & O_MM ) == 0) || |
| 1892 |
(o2.isRegType(REG_XMM) && (id.o2Flags & O_XMM) == 0) || |
| 1893 |
(o2.isRegType(REG_GPD) && (id.o2Flags & O_G32) == 0) || |
| 1894 |
(o2.isRegType(REG_GPQ) && (id.o2Flags & O_G64) == 0) || |
| 1895 |
(o2.isMem() && (id.o2Flags & O_MEM) == 0) || |
| 1896 |
!o3.isImm()) |
| 1897 |
{ |
| 1898 |
throw new IllegalStateException("illegal instruction"); |
| 1899 |
} |
| 1900 |
|
| 1901 |
int prefix = |
| 1902 |
((id.o1Flags & O_MM_XMM) == O_MM_XMM && o1.isRegType(REG_XMM)) || |
| 1903 |
((id.o2Flags & O_MM_XMM) == O_MM_XMM && o2.isRegType(REG_XMM)) |
| 1904 |
? 0x66000000 |
| 1905 |
: 0x00000000; |
| 1906 |
int rexw = ((id.o1Flags|id.o2Flags) & O_NOREX) != 0 |
| 1907 |
? 0 |
| 1908 |
: intValue(o1.isRegType(REG_GPQ) || o1.isRegType(REG_GPQ)); |
| 1909 |
|
| 1910 |
// (X)MM <- (X)MM (opcode1) |
| 1911 |
if (o2.isReg()) |
| 1912 |
{ |
| 1913 |
if ((id.o2Flags & (O_MM_XMM | O_G32_64)) == 0) throw new IllegalStateException("illegal instruction"); |
| 1914 |
_emitMmu(id.opCode1 | prefix, rexw, |
| 1915 |
((BaseReg) o1).code(), |
| 1916 |
(BaseReg)(o2), 1); |
| 1917 |
_emitImmediate((Immediate)(o3), 1); |
| 1918 |
return; |
| 1919 |
} |
| 1920 |
// (X)MM <- Mem (opcode1) |
| 1921 |
if (o2.isMem()) |
| 1922 |
{ |
| 1923 |
if ((id.o2Flags & O_MEM) == 0) throw new IllegalStateException("illegal instruction"); |
| 1924 |
_emitMmu(id.opCode1 | prefix, rexw, |
| 1925 |
((BaseReg) o1).code(), |
| 1926 |
(Mem)(o2), 1); |
| 1927 |
_emitImmediate((Immediate)(o3), 1); |
| 1928 |
return; |
| 1929 |
} |
| 1930 |
|
| 1931 |
break; |
| 1932 |
} |
| 1933 |
|
| 1934 |
case I_MMU_RM_3DNOW: |
| 1935 |
{ |
| 1936 |
if (o1.isRegType(REG_MM) && (o2.isRegType(REG_MM) || o2.isMem())) |
| 1937 |
{ |
| 1938 |
_emitMmu(id.opCode1, 0, |
| 1939 |
((BaseReg) o1).code(), |
| 1940 |
(Mem)(o2), 1); |
| 1941 |
_emitByte(id.opCode2); |
| 1942 |
return; |
| 1943 |
} |
| 1944 |
|
| 1945 |
break; |
| 1946 |
} |
| 1947 |
} |
| 1948 |
} |
| 1949 |
|
| 1950 |
void _emitFpu(int opCode) { |
| 1951 |
_emitOpCode(opCode); |
| 1952 |
} |
| 1953 |
|
| 1954 |
void _emitFpuSTI(int opCode, int sti) { |
| 1955 |
// illegal stack offset |
| 1956 |
assert (0 <= sti && sti < 8); |
| 1957 |
_emitOpCode(opCode + sti); |
| 1958 |
} |
| 1959 |
|
| 1960 |
void _emitFpuMEM(int opCode, int opReg, Mem mem) { |
| 1961 |
// segment prefix |
| 1962 |
_emitSegmentPrefix(mem); |
| 1963 |
|
| 1964 |
// instruction prefix |
| 1965 |
if ((opCode & 0xFF000000) != 0) { |
| 1966 |
_emitByte(((opCode & 0xFF000000) >> 24)); |
| 1967 |
} |
| 1968 |
|
| 1969 |
// rex prefix |
| 1970 |
if (is64()) { |
| 1971 |
_emitRexRM(0, opReg, mem); |
| 1972 |
} |
| 1973 |
|
| 1974 |
// instruction opcodes |
| 1975 |
if ((opCode & 0x00FF0000) != 0) { |
| 1976 |
_emitByte(((opCode & 0x00FF0000) >> 16)); |
| 1977 |
} |
| 1978 |
if ((opCode & 0x0000FF00) != 0) { |
| 1979 |
_emitByte(((opCode & 0x0000FF00) >> 8)); |
| 1980 |
} |
| 1981 |
|
| 1982 |
_emitByte(((opCode & 0x000000FF))); |
| 1983 |
_emitModM(opReg, mem, 0); |
| 1984 |
} |
| 1985 |
|
| 1986 |
void _emitMmu(int opCode, int rexw, int opReg, |
| 1987 |
Operand src, int immSize) { |
| 1988 |
// Segment prefix. |
| 1989 |
_emitSegmentPrefix(src); |
| 1990 |
|
| 1991 |
// Instruction prefix. |
| 1992 |
if ((opCode & 0xFF000000) != 0) { |
| 1993 |
_emitByte(((opCode & 0xFF000000) >> 24)); |
| 1994 |
} |
| 1995 |
|
| 1996 |
// Rex prefix |
| 1997 |
if (is64()) { |
| 1998 |
_emitRexRM(rexw, opReg, src); |
| 1999 |
} |
| 2000 |
|
| 2001 |
// Instruction opcodes. |
| 2002 |
if ((opCode & 0x00FF0000) != 0) { |
| 2003 |
_emitByte(((opCode & 0x00FF0000) >> 16)); |
| 2004 |
} |
| 2005 |
|
| 2006 |
// No checking, MMX/SSE instructions have always two opcodes or more. |
| 2007 |
_emitByte(((opCode & 0x0000FF00) >> 8)); |
| 2008 |
_emitByte(((opCode & 0x000000FF))); |
| 2009 |
|
| 2010 |
if (src.isReg()) { |
| 2011 |
_emitModR(opReg, ((BaseReg) src).code()); |
| 2012 |
} else { |
| 2013 |
_emitModM(opReg, (Mem) src, immSize); |
| 2014 |
} |
| 2015 |
} |
| 2016 |
|
| 2017 |
LinkData _emitDisplacement(Label label, long inlinedDisplacement, int size) { |
| 2018 |
assert (!label.isBound()); |
| 2019 |
assert (size == 1 || size == 4); |
| 2020 |
|
| 2021 |
// Chain with label. |
| 2022 |
LinkData link = new LinkData(offset(), inlinedDisplacement, -1); |
| 2023 |
|
| 2024 |
label.link(link); |
| 2025 |
|
| 2026 |
// Emit dummy DWORD. |
| 2027 |
if (size == 1) { |
| 2028 |
_emitByte(0x01); |
| 2029 |
} else // if (size == 4) |
| 2030 |
{ |
| 2031 |
_emitDWord(0x04040404); |
| 2032 |
} |
| 2033 |
|
| 2034 |
return link; |
| 2035 |
} |
| 2036 |
|
| 2037 |
void _emitJmpOrCallReloc(InstructionGroup instruction, long target) { |
| 2038 |
|
| 2039 |
|
| 2040 |
if (is64()) { |
| 2041 |
// If we are compiling in 64-bit mode, we can use trampoline if relative jump |
| 2042 |
// is not possible. |
| 2043 |
_trampolineSize += TrampolineWriter.TRAMPOLINE_SIZE; |
| 2044 |
} |
| 2045 |
|
| 2046 |
RelocData rd = new RelocData(RelocData.Type.ABSOLUTE_TO_RELATIVE_TRAMPOLINE, 4, offset(), target); |
| 2047 |
_relocData.add(rd); |
| 2048 |
|
| 2049 |
// Emit dummy 32-bit integer (will be overwritten by relocCode()). |
| 2050 |
_emitInt32(0); |
| 2051 |
} |
| 2052 |
|
| 2053 |
public void relocCode(ByteBuffer buffer, long address) { |
| 2054 |
// Copy code to virtual memory (this is a given _dst pointer). |
| 2055 |
int csize = codeSize(); |
| 2056 |
|
| 2057 |
// We are copying exactly size of generated code. Extra code for trampolines |
| 2058 |
// is generated on-the-fly by relocator (this code not exists at now). |
| 2059 |
_buffer.copyTo(buffer); |
| 2060 |
|
| 2061 |
// Relocate recorded locations. |
| 2062 |
for (RelocData r : _relocData) { |
| 2063 |
|
| 2064 |
long val; |
| 2065 |
|
| 2066 |
// Whether to use trampoline, can be only used if relocation type is |
| 2067 |
// ABSOLUTE_TO_RELATIVE_TRAMPOLINE. |
| 2068 |
boolean useTrampoline = false; |
| 2069 |
|
| 2070 |
|
| 2071 |
// Be sure that reloc data structure is correct. |
| 2072 |
assert ((r.offset + r.size) <= csize); |
| 2073 |
|
| 2074 |
switch (r.type) { |
| 2075 |
case ABSOLUTE_TO_ABSOLUTE: |
| 2076 |
val = r.destination; |
| 2077 |
break; |
| 2078 |
|
| 2079 |
case RELATIVE_TO_ABSOLUTE: |
| 2080 |
val = address + r.destination; |
| 2081 |
break; |
| 2082 |
|
| 2083 |
case ABSOLUTE_TO_RELATIVE: |
| 2084 |
case ABSOLUTE_TO_RELATIVE_TRAMPOLINE: |
| 2085 |
val = r.destination - (address + r.offset + 4); |
| 2086 |
|
| 2087 |
if (is64() && r.type == RelocData.Type.ABSOLUTE_TO_RELATIVE_TRAMPOLINE && !isInt32(val)) { |
| 2088 |
val = (long) buffer.position() - (r.offset + 4); |
| 2089 |
useTrampoline = true; |
| 2090 |
} |
| 2091 |
|
| 2092 |
break; |
| 2093 |
|
| 2094 |
default: |
| 2095 |
throw new IllegalStateException("invalid relocation type"); |
| 2096 |
} |
| 2097 |
|
| 2098 |
switch (r.size) { |
| 2099 |
case 4: |
| 2100 |
buffer.putInt(r.offset, (int) val); |
| 2101 |
break; |
| 2102 |
|
| 2103 |
case 8: |
| 2104 |
buffer.putLong(r.offset, val); |
| 2105 |
break; |
| 2106 |
|
| 2107 |
default: |
| 2108 |
throw new IllegalStateException("invalid relocation size"); |
| 2109 |
} |
| 2110 |
|
| 2111 |
if (is64() && useTrampoline) { |
| 2112 |
if (_logger != null) { |
| 2113 |
_logger.log(String.format("; Trampoline from %x -> %x\n", address + r.offset, r.destination)); |
| 2114 |
} |
| 2115 |
|
| 2116 |
TrampolineWriter.writeTrampoline(buffer, r.destination); |
| 2117 |
} |
| 2118 |
} |
| 2119 |
} |
| 2120 |
// NOPs optimized for Intel: |
| 2121 |
// Intel 64 and IA-32 Architectures Software Developer's Manual |
| 2122 |
// - Volume 2B |
| 2123 |
// - Instruction Set Reference N-Z |
| 2124 |
// - NOP |
| 2125 |
|
| 2126 |
// NOPs optimized for AMD: |
| 2127 |
// Software Optimization Guide for AMD Family 10h Processors (Quad-Core) |
| 2128 |
// - 4.13 - Code Padding with Operand-Size Override and Multibyte NOP |
| 2129 |
// Intel and AMD |
| 2130 |
private static final int nop1[] = { 0x90 }; |
| 2131 |
private static final int nop2[] = { 0x66, 0x90 }; |
| 2132 |
private static final int nop3[] = { 0x0F, 0x1F, 0x00 }; |
| 2133 |
private static final int nop4[] = { 0x0F, 0x1F, 0x40, 0x00 }; |
| 2134 |
private static final int nop5[] = { 0x0F, 0x1F, 0x44, 0x00, 0x00 }; |
| 2135 |
private static final int nop6[] = { 0x66, 0x0F, 0x1F, 0x44, 0x00, 0x00 }; |
| 2136 |
private static final int nop7[] = { 0x0F, 0x1F, 0x80, 0x00, 0x00, 0x00, 0x00 }; |
| 2137 |
private static final int nop8[] = { 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 2138 |
private static final int nop9[] = { 0x66, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 2139 |
|
| 2140 |
// AMD |
| 2141 |
private static final int nop10[] = { 0x66, 0x66, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 2142 |
private static final int nop11[] = { 0x66, 0x66, 0x66, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 2143 |
|
| 2144 |
public void align(long m) { |
| 2145 |
if (_logger != null) _logger.logAlign(m); |
| 2146 |
|
| 2147 |
if (m < 1) return; |
| 2148 |
|
| 2149 |
if (m > 64) { |
| 2150 |
assert (m <= 64); |
| 2151 |
return; |
| 2152 |
} |
| 2153 |
|
| 2154 |
int i = (int) (m - (offset() % m)); |
| 2155 |
if (i == m) return; |
| 2156 |
|
| 2157 |
if ((_properties & (1 << PROPERTY_OPTIMIZE_ALIGN)) != 0) { |
| 2158 |
int n; |
| 2159 |
|
| 2160 |
if (cpuInfo.vendor == CpuInfo.Vendor.INTEL && |
| 2161 |
((cpuInfo.family & 0x0F) == 6 || |
| 2162 |
(cpuInfo.family & 0x0F) == 15)) { |
| 2163 |
do { |
| 2164 |
int[] p; |
| 2165 |
switch (i) { |
| 2166 |
case 1: p = nop1; n = 1; break; |
| 2167 |
case 2: p = nop2; n = 2; break; |
| 2168 |
case 3: p = nop3; n = 3; break; |
| 2169 |
case 4: p = nop4; n = 4; break; |
| 2170 |
case 5: p = nop5; n = 5; break; |
| 2171 |
case 6: p = nop6; n = 6; break; |
| 2172 |
case 7: p = nop7; n = 7; break; |
| 2173 |
case 8: p = nop8; n = 8; break; |
| 2174 |
default: p = nop9; n = 9; break; |
| 2175 |
} |
| 2176 |
|
| 2177 |
i -= n; |
| 2178 |
for (int idx = 0; n > 0; ++idx, --n) { |
| 2179 |
_emitByte(p[idx]); |
| 2180 |
} |
| 2181 |
} while (i > 0); |
| 2182 |
|
| 2183 |
return; |
| 2184 |
} |
| 2185 |
|
| 2186 |
if (cpuInfo.vendor == CpuInfo.Vendor.AMD |
| 2187 |
&& cpuInfo.family >= 0x0F) { |
| 2188 |
do { |
| 2189 |
int[] p; |
| 2190 |
switch (i) { |
| 2191 |
case 1: p = nop1 ; n = 1; break; |
| 2192 |
case 2: p = nop2 ; n = 2; break; |
| 2193 |
case 3: p = nop3 ; n = 3; break; |
| 2194 |
case 4: p = nop4 ; n = 4; break; |
| 2195 |
case 5: p = nop5 ; n = 5; break; |
| 2196 |
case 6: p = nop6 ; n = 6; break; |
| 2197 |
case 7: p = nop7 ; n = 7; break; |
| 2198 |
case 8: p = nop8 ; n = 8; break; |
| 2199 |
case 9: p = nop9 ; n = 9; break; |
| 2200 |
case 10: p = nop10; n = 10; break; |
| 2201 |
default: p = nop11; n = 11; break; |
| 2202 |
} |
| 2203 |
|
| 2204 |
i -= n; |
| 2205 |
for (int idx = 0; n > 0; ++idx, --n) { |
| 2206 |
_emitByte(p[idx]); |
| 2207 |
} |
| 2208 |
} while (i > 0); |
| 2209 |
|
| 2210 |
return; |
| 2211 |
} |
| 2212 |
|
| 2213 |
if (!is64()) { |
| 2214 |
// legacy NOPs, 0x90 with 0x66 prefix. |
| 2215 |
do { |
| 2216 |
switch (i) { |
| 2217 |
default: _emitByte(0x66); i--; |
| 2218 |
case 3: _emitByte(0x66); i--; |
| 2219 |
case 2: _emitByte(0x66); i--; |
| 2220 |
case 1: _emitByte(0x90); i--; |
| 2221 |
} |
| 2222 |
} while (i > 0); |
| 2223 |
} |
| 2224 |
} |
| 2225 |
|
| 2226 |
// legacy NOPs, only 0x90 |
| 2227 |
// In 64-bit mode, we can't use 0x66 prefix |
| 2228 |
while (i-- > 0) { |
| 2229 |
_emitByte(0x90); |
| 2230 |
} |
| 2231 |
} |
| 2232 |
|
| 2233 |
} |