Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / native_client / src / trusted / validator / x86 / decoder / ncopcode_desc.h
1 /*
2  * Copyright (c) 2011 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  * ncopcode_desc.h - Descriptors to model opcode operands.
9  */
10 #ifndef NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_X86_DECODER_NCOPCODE_DESC_H_
11 #define NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_X86_DECODER_NCOPCODE_DESC_H_
12
13 #include <stdio.h>
14
15 #include "native_client/src/include/portability.h"
16 #include "native_client/src/shared/gio/gio.h"
17 #include "native_client/src/shared/utils/types.h"
18 #include "native_client/src/trusted/validator/x86/decoder/gen/ncopcode_prefix.h"
19 #include "native_client/src/trusted/validator/x86/decoder/gen/ncopcode_insts.h"
20 #include "native_client/src/trusted/validator/x86/decoder/gen/ncopcode_opcode_flags.h"
21 #include "native_client/src/trusted/validator/x86/decoder/gen/ncopcode_operand_kind.h"
22 #include "native_client/src/trusted/validator/x86/decoder/gen/ncopcode_operand_flag.h"
23 #include "native_client/src/trusted/validator/x86/x86_insts.h"
24
25 EXTERN_C_BEGIN
26
27 struct NaClDecodeTables;
28
29 /* Defines integer to represent sets of possible opcode (instruction) flags */
30 typedef uint64_t NaClIFlags;
31
32 /* Converts an NaClIFlagEnum to the corresponding bit in NaClIFlags. */
33 #define NACL_IFLAG(x) (((NaClIFlags) 1) << (x))
34
35 /* Models the empty set of opcode flags. */
36 #define NACL_EMPTY_IFLAGS ((NaClIFlags) 0)
37
38 /* Prints out the set of defined instruction flags. */
39 void NaClIFlagsPrint(struct Gio* out, NaClIFlags flags);
40
41 /* Defines integer to represent sets of possible operand flags. */
42 typedef uint32_t NaClOpFlags;
43
44 /* Converts an NaClOpFlag enum to the corresponding bit in NaClOpFlags. */
45 #define NACL_OPFLAG(x) (((NaClOpFlags) 1) << x)
46
47 /* Models the empty set of operand flags. */
48 #define NACL_EMPTY_OPFLAGS ((NaClOpFlags) 0)
49
50 /* Prints out the set of defined OPerand flags. */
51 void NaClOpFlagsPrint(struct Gio* out, NaClOpFlags flags);
52
53 /* Defines integer to represent sets of possible instruction disallow
54  * flags.
55  */
56 typedef uint16_t NaClDisallowsFlags;
57
58 /* Converts a NaClDisallowsFlag to the corresponding bit
59  * in NaClDisallowsFlags.
60  */
61 #define NACL_DISALLOWS_FLAG(x) (((NaClDisallowsFlags) 1) << (x))
62
63 /* Models the empty set of instruction disallows flags. */
64 #define NACL_EMPTY_DISALLOWS_FLAGS ((NaClDisallowsFlags) 0)
65
66 /* Metadata about an instruction operand. */
67 typedef struct NaClOp {
68   /* The kind of the operand (i.e. kind of data modeled by the operand).*/
69   NaClOpKind kind;
70   /* Flags defining additional facts about the operand. */
71   NaClOpFlags flags;
72   /* Printing format string for operand. */
73   const char* format_string;
74 } NaClOp;
75
76 /* Maxmimum number of opcode bytes per instruction. */
77 #define NACL_MAX_OPCODE_BYTES 3
78
79 /* Maximum number of opcode bytes used to model an instruction. Include
80  * opcodes in the modrm byte, and register values encoded in the opcode.
81  */
82 #define NACL_MAX_ALL_OPCODE_BYTES 4
83
84 /* Metadata about an instruction, defining a pattern. Note: Since the same
85  * sequence of opcode bytes may define more than one pattern (depending on
86  * other bytes in the parsed instruction), the patterns are
87  * modeled using a singly linked list.
88  */
89 typedef struct NaClInst {
90   /* Defines the origin of this instruction. */
91   NaClInstType insttype;
92   /* Flags defining additional facts about the instruction. */
93   NaClIFlags flags;
94   /* The instruction that this instruction implements. */
95   NaClMnemonic name;
96   /* Defines opcode extentions, which encodes values for OpcodeInModRm,
97    * OpcodePlusR, and OpcodeInModRmRm. Note: to fit the possible 9
98    * bits of information in 8 bits, we assume that OpcodeInModRm
99    * and OpcodePlusR do not happen in the same instruction.
100    */
101   uint8_t opcode_ext;
102   /* The number of operands modeled for this instruction. */
103   uint8_t num_operands;
104   /* The corresponding models of the operands. */
105   uint16_t operands_offset;
106   /* Pointer to the next pattern to try and match for the
107    * given sequence of opcode bytes.
108    */
109   uint16_t next_rule;
110 } NaClInst;
111
112 /* Returns the OpcodeInModRm value in the opcode_ext field. */
113 uint8_t NaClGetOpcodeInModRm(uint8_t opcode_ext);
114
115 /* Returns the OpcodeInModRmRm value in the opcode_ext field. */
116 uint8_t NaClGetOpcodeInModRmRm(uint8_t opcode_ext);
117
118 /* Returns the OpcodePlusR value in the opcode_ext field. */
119 uint8_t NaClGetOpcodePlusR(uint8_t opcode_ext);
120
121 /* Implements trie nodes for selecting instructions that must match
122  * a specific sequence of bytes. Used to handle NOP cases.
123  */
124 typedef struct NaClInstNode {
125   /* The matching byte for the trie node. */
126   const uint8_t matching_byte;
127   /* The matching modeled instruction, if byte matched. */
128   const uint16_t matching_inst;
129   /* Node to match remaining bytes if matching_byte matches. */
130   const struct NaClInstNode* success;
131   /* Node to try next if match_byte doesn't match. Note:
132    * The trie is generated in such a way that if the next input
133    * byte is > matching_byte, no node in the fail subtree will
134    * match the current input. That is, nodes in the trie are
135    * sorted by the sequence of matching bytes.
136    */
137   const struct NaClInstNode* fail;
138 } NaClInstNode;
139
140 /* Returns the number of logical operands an instruction has. That is,
141  * returns field num_operands unless the first operand is
142  * a special encoding that extends the opcode.
143  */
144 uint8_t NaClGetInstNumberOperands(const NaClInst* inst);
145
146 /* Returns the indexed logical operand for the instruction. That is,
147  * returns the index-th operand unless the first operand is
148  * a special encoding that extends the opcode. In the latter
149  * case, the (index+1)-th operand is returned.
150  */
151 const NaClOp* NaClGetInstOperand(const struct NaClDecodeTables* tables,
152                                  const NaClInst* inst,
153                                  uint8_t index);
154
155 /* Print out the given operand structure to the given file. */
156 void NaClOpPrint(struct Gio* f, const NaClOp* operand);
157
158 /* Returns a string defining bytes of the given prefix that are considered
159  * prefix bytes, independent of the opcode.
160  */
161 const char* OpcodePrefixBytes(NaClInstPrefix prefix);
162
163 /* Print out the given instruction to the given file. However, always
164  * print the value NULL for next_rule, even if the value is non-null. This
165  * function should be used to print out an individual opcode (instruction)
166  * pattern.
167  */
168 void NaClInstPrint(struct Gio* f,
169                    const struct NaClDecodeTables* tables,
170                    const NaClInst* inst);
171
172 EXTERN_C_END
173
174 #endif  /* NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_X86_DECODER_NCOPCODE_DESC_H_ */