OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. |
| 5 */ |
| 6 |
| 7 /* |
| 8 * This file contains common parts of x86-32 and x86-64 internals (inline |
| 9 * functions and defines). |
| 10 */ |
| 11 |
| 12 #ifndef NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_INTERNAL_H_ |
| 13 #define NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_INTERNAL_H_ |
| 14 |
| 15 #include "native_client/src/trusted/validator_ragel/decoding.h" |
| 16 #include "native_client/src/trusted/validator_ragel/decoder.h" |
| 17 |
| 18 /* |
| 19 * Set of macroses used in actions defined in parse_instruction.rl to pull |
| 20 * parts of the instruction from a byte stream and store them for future use. |
| 21 */ |
| 22 #define GET_REX_PREFIX() instruction.prefix.rex |
| 23 #define SET_REX_PREFIX(P) instruction.prefix.rex = (P) |
| 24 #define GET_VEX_PREFIX2() vex_prefix2 |
| 25 #define SET_VEX_PREFIX2(P) vex_prefix2 = (P) |
| 26 #define GET_VEX_PREFIX3() vex_prefix3 |
| 27 #define SET_VEX_PREFIX3(P) vex_prefix3 = (P) |
| 28 #define SET_DATA16_PREFIX(S) instruction.prefix.data16 = (S) |
| 29 #define SET_LOCK_PREFIX(S) instruction.prefix.lock = (S) |
| 30 #define SET_REPZ_PREFIX(S) instruction.prefix.repz = (S) |
| 31 #define SET_REPNZ_PREFIX(S) instruction.prefix.repnz = (S) |
| 32 #define SET_BRANCH_TAKEN(S) instruction.prefix.branch_taken = (S) |
| 33 #define SET_BRANCH_NOT_TAKEN(S) instruction.prefix.branch_not_taken = (S) |
| 34 #define SET_INSTRUCTION_NAME(N) instruction.name = (N) |
| 35 #define GET_OPERAND_NAME(N) instruction.operands[(N)].name |
| 36 #define SET_OPERAND_NAME(N, S) instruction.operands[(N)].name = (S) |
| 37 #define SET_OPERAND_TYPE(N, S) instruction.operands[(N)].type = (S) |
| 38 #define SET_OPERANDS_COUNT(N) instruction.operands_count = (N) |
| 39 #define SET_MODRM_BASE(N) instruction.rm.base = (N) |
| 40 #define SET_MODRM_INDEX(N) instruction.rm.index = (N) |
| 41 #define SET_MODRM_SCALE(S) instruction.rm.scale = (S) |
| 42 #define SET_DISP_TYPE(T) instruction.rm.disp_type = (T) |
| 43 #define SET_DISP_PTR(P) \ |
| 44 instruction.rm.offset = DecodeDisplacementValue(instruction.rm.disp_type, (P)) |
| 45 #define SET_IMM_TYPE(T) imm_operand = (T) |
| 46 #define SET_IMM_PTR(P) \ |
| 47 instruction.imm[0] = DecodeImmediateValue(imm_operand, (P)) |
| 48 #define SET_IMM2_TYPE(T) imm2_operand = (T) |
| 49 #define SET_IMM2_PTR(P) \ |
| 50 instruction.imm[1] = DecodeImmediateValue(imm2_operand, (P)) |
| 51 #define SET_CPU_FEATURE(F) |
| 52 #define SET_ATT_INSTRUCTION_SUFFIX(S) instruction.att_instruction_suffix = (S) |
| 53 #define CLEAR_SPURIOUS_DATA16() instruction.prefix.data16_spurious = FALSE |
| 54 #define SET_SPURIOUS_DATA16() instruction.prefix.data16_spurious = TRUE |
| 55 #define CLEAR_SPURIOUS_REX_B() \ |
| 56 instruction.prefix.rex_b_spurious = FALSE |
| 57 #define SET_SPURIOUS_REX_B() \ |
| 58 if (GET_REX_PREFIX() & REX_B) instruction.prefix.rex_b_spurious = TRUE |
| 59 #define CLEAR_SPURIOUS_REX_X() \ |
| 60 instruction.prefix.rex_x_spurious = FALSE |
| 61 #define SET_SPURIOUS_REX_X() \ |
| 62 if (GET_REX_PREFIX() & REX_X) instruction.prefix.rex_x_spurious = TRUE |
| 63 #define CLEAR_SPURIOUS_REX_R() \ |
| 64 instruction.prefix.rex_r_spurious = FALSE |
| 65 #define SET_SPURIOUS_REX_R() \ |
| 66 if (GET_REX_PREFIX() & REX_R) instruction.prefix.rex_r_spurious = TRUE |
| 67 #define CLEAR_SPURIOUS_REX_W() \ |
| 68 instruction.prefix.rex_w_spurious = FALSE |
| 69 #define SET_SPURIOUS_REX_W() \ |
| 70 if (GET_REX_PREFIX() & REX_W) instruction.prefix.rex_w_spurious = TRUE |
| 71 |
| 72 /* |
| 73 * Immediate mode: size of the instruction's immediate operand. Note that there |
| 74 * IMMNONE (which means there are no immediate operand) and IMM2 (which is |
| 75 * two-bit immediate which shares it's byte with other operands). |
| 76 */ |
| 77 enum ImmediateMode { |
| 78 IMMNONE, |
| 79 IMM2, |
| 80 IMM8, |
| 81 IMM16, |
| 82 IMM32, |
| 83 IMM64 |
| 84 }; |
| 85 |
| 86 static FORCEINLINE uint64_t DecodeDisplacementValue( |
| 87 enum DisplacementMode disp_mode, const uint8_t *disp_ptr) { |
| 88 switch(disp_mode) { |
| 89 case DISPNONE: return 0; |
| 90 case DISP8: return SignExtend8Bit(AnyFieldValue8bit(disp_ptr)); |
| 91 case DISP16: return SignExtend16Bit(AnyFieldValue16bit(disp_ptr)); |
| 92 case DISP32: return SignExtend32Bit(AnyFieldValue32bit(disp_ptr)); |
| 93 case DISP64: return AnyFieldValue64bit(disp_ptr); |
| 94 } |
| 95 assert(FALSE); |
| 96 return 0; |
| 97 } |
| 98 |
| 99 |
| 100 static FORCEINLINE uint64_t DecodeImmediateValue(enum ImmediateMode imm_mode, |
| 101 const uint8_t *imm_ptr) { |
| 102 switch(imm_mode) { |
| 103 case IMMNONE: return 0; |
| 104 case IMM2: return imm_ptr[0] & 0x03; |
| 105 case IMM8: return AnyFieldValue8bit(imm_ptr); |
| 106 case IMM16: return AnyFieldValue16bit(imm_ptr); |
| 107 case IMM32: return AnyFieldValue32bit(imm_ptr); |
| 108 case IMM64: return AnyFieldValue64bit(imm_ptr); |
| 109 } |
| 110 assert(FALSE); |
| 111 return 0; |
| 112 } |
| 113 |
| 114 #endif /* NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_INTERNAL_H_ */ |
OLD | NEW |