| 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 static com.kenai.jnr.x86asm.INST_CODE.*; |
| 29 |
|
| 30 |
/** |
| 31 |
* Assembler intrinsics seralizer. |
| 32 |
* |
| 33 |
* SerializerCore is abstract class that is used by @c Assembler and @a Compiler. |
| 34 |
* You probably never use this class directly, instead you use it to serialize |
| 35 |
* intrinsics to @c Assembler or @c Compiler. @c SerializerIntrinsics implements |
| 36 |
* all intruction intrinsics thats used and @c Serializer is public serializer |
| 37 |
* class that should be used (instead of @c SerializerCore or @c SerializerInstrinsics). |
| 38 |
* |
| 39 |
* <b>Note:</b> Use always {@link Serializer} class, this class is only designed to |
| 40 |
* decrease code size when exporting AsmJit library symbols. Some compilers |
| 41 |
* (for example MSVC) are exporting inline symbols when class is declared |
| 42 |
* to export them and {@link Serializer} class contains really huge count of |
| 43 |
* symbols that will be never used (everything is inlined). |
| 44 |
*/ |
| 45 |
@Deprecated |
| 46 |
public abstract class SerializerCore { |
| 47 |
|
| 48 |
static final Operand _none = new Operand(OP.OP_NONE, 0) {}; |
| 49 |
|
| 50 |
//! @brief Emits X86/FPU or MM instruction. |
| 51 |
//! |
| 52 |
//! Operands @a o1, @a o2 or @a o3 can be @c NULL if they are not used. |
| 53 |
//! |
| 54 |
//! Hint: Use @c emitX86() helpers to emit instructions. |
| 55 |
abstract void _emitX86(INST_CODE code, Operand o1, Operand o2, Operand o3); |
| 56 |
|
| 57 |
|
| 58 |
// Helpers to decrease binary code size. These four emit methods are just |
| 59 |
// helpers thats used by serializer. They call _emitX86() adding NULLs |
| 60 |
// to first, second and third operand if needed. |
| 61 |
|
| 62 |
//! @brief Emits instruction with no operand. |
| 63 |
//! |
| 64 |
//! Should be use as an alternative to @c _emitX86() method. |
| 65 |
void emitX86(INST_CODE code) { |
| 66 |
_emitX86(code, _none, _none, _none); |
| 67 |
} |
| 68 |
|
| 69 |
//! @brief Emits instruction with one operand. |
| 70 |
//! |
| 71 |
//! Should be use as an alternative to @c _emitX86() method. |
| 72 |
void emitX86(INST_CODE code, Operand o1) { |
| 73 |
_emitX86(code, o1, _none, _none); |
| 74 |
} |
| 75 |
|
| 76 |
//! @brief Emits instruction with two operands. |
| 77 |
//! |
| 78 |
//! Should be use as an alternative to @c _emitX86() method. |
| 79 |
void emitX86(INST_CODE code, Operand o1, Operand o2) { |
| 80 |
_emitX86(code, o1, o2, _none); |
| 81 |
} |
| 82 |
|
| 83 |
//! @brief Emits instruction with three operands. |
| 84 |
//! |
| 85 |
//! Should be use as an alternative to @c _emitX86() method. |
| 86 |
void emitX86(INST_CODE code, Operand o1, Operand o2, Operand o3) { |
| 87 |
_emitX86(code, o1, o2, o3); |
| 88 |
} |
| 89 |
|
| 90 |
//! @brief Private method for emitting jcc. |
| 91 |
//! @internal This should be probably private. |
| 92 |
void _emitJcc(INST_CODE code, Label label, final int hint) { |
| 93 |
if (hint == 0) { |
| 94 |
emitX86(code, label); |
| 95 |
} else { |
| 96 |
emitX86(code, label, Immediate.imm(hint)); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
//! @brief Private method for emitting jcc. |
| 101 |
//! @internal This should be probably private. |
| 102 |
void _emitJcc(INST_CODE code, Label label, final HINT hint) { |
| 103 |
if (hint == HINT.HINT_NONE) { |
| 104 |
emitX86(code, label); |
| 105 |
} else { |
| 106 |
emitX86(code, label, Immediate.imm(hint.value())); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
abstract boolean is64(); |
| 111 |
|
| 112 |
//! @brief Translate condition code @a CC to AsmJit jump (jcc) instruction code. |
| 113 |
//! @sa @c INST_CODE, @c INST_J. |
| 114 |
static INST_CODE conditionToJCC(CONDITION cc) { |
| 115 |
assert (cc.value() <= 0xF); |
| 116 |
return _jcctable[cc.value()]; |
| 117 |
} |
| 118 |
|
| 119 |
//! @brief Translate condition code @a CC to AsmJit cmov (cmovcc) instruction code. |
| 120 |
//! @sa @c INST_CODE, @c INST_CMOV. |
| 121 |
static INST_CODE conditionToCMovCC(CONDITION cc) { |
| 122 |
assert (cc.value() <= 0xF); |
| 123 |
return _cmovcctable[cc.value()]; |
| 124 |
} |
| 125 |
|
| 126 |
//! @brief Translate condition code @a CC to AsmJit set (setcc) instruction code. |
| 127 |
//! @sa @c INST_CODE, @c INST_SET. |
| 128 |
static INST_CODE conditionToSetCC(CONDITION cc) { |
| 129 |
assert (cc.value() <= 0xF); |
| 130 |
return _setcctable[cc.value()]; |
| 131 |
} |
| 132 |
|
| 133 |
/** Map used for jcc instructions. */ |
| 134 |
static INST_CODE[] _jcctable = { |
| 135 |
INST_JO, |
| 136 |
INST_JNO, |
| 137 |
INST_JB, |
| 138 |
INST_JAE, |
| 139 |
INST_JE, |
| 140 |
INST_JNE, |
| 141 |
INST_JBE, |
| 142 |
INST_JA, |
| 143 |
INST_JS, |
| 144 |
INST_JNS, |
| 145 |
INST_JPE, |
| 146 |
INST_JPO, |
| 147 |
INST_JL, |
| 148 |
INST_JGE, |
| 149 |
INST_JLE, |
| 150 |
INST_JG |
| 151 |
}; |
| 152 |
|
| 153 |
/** Map used for cmovcc instructions. */ |
| 154 |
static INST_CODE[] _cmovcctable = { |
| 155 |
INST_CMOVO, |
| 156 |
INST_CMOVNO, |
| 157 |
INST_CMOVB, |
| 158 |
INST_CMOVAE, |
| 159 |
INST_CMOVE, |
| 160 |
INST_CMOVNE, |
| 161 |
INST_CMOVBE, |
| 162 |
INST_CMOVA, |
| 163 |
INST_CMOVS, |
| 164 |
INST_CMOVNS, |
| 165 |
INST_CMOVPE, |
| 166 |
INST_CMOVPO, |
| 167 |
INST_CMOVL, |
| 168 |
INST_CMOVGE, |
| 169 |
INST_CMOVLE, |
| 170 |
INST_CMOVG |
| 171 |
}; |
| 172 |
|
| 173 |
static final INST_CODE[] _setcctable = { |
| 174 |
INST_SETO, |
| 175 |
INST_SETNO, |
| 176 |
INST_SETB, |
| 177 |
INST_SETAE, |
| 178 |
INST_SETE, |
| 179 |
INST_SETNE, |
| 180 |
INST_SETBE, |
| 181 |
INST_SETA, |
| 182 |
INST_SETS, |
| 183 |
INST_SETNS, |
| 184 |
INST_SETPE, |
| 185 |
INST_SETPO, |
| 186 |
INST_SETL, |
| 187 |
INST_SETGE, |
| 188 |
INST_SETLE, |
| 189 |
INST_SETG |
| 190 |
}; |
| 191 |
} |