2 Intel Open Source License
4 Copyright (c) 2002-2007 Intel Corporation
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
10 Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer. Redistributions
12 in binary form must reproduce the above copyright notice, this list of
13 conditions and the following disclaimer in the documentation and/or
14 other materials provided with the distribution. Neither the name of
15 the Intel Corporation nor the names of its contributors may be used to
16 endorse or promote products derived from this software without
17 specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
23 ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 /// @file xed-encode.h
32 /// @author Mark Charney <mark.charney@intel.com>
35 #ifndef _XED_ENCODE_H_
36 # define _XED_ENCODE_H_
37 #include <xed/xed-common-hdrs.h>
38 #include <xed/xed-types.h>
39 #include <xed/xed-error-enum.h>
40 #include <xed/xed-operand-values-interface.h>
41 #include <xed/xed-operand-width-enum.h>
42 #include <xed/xed-encoder-iforms.h> //generated
43 #include <xed/xed-encoder-gen-defs.h> //generated
45 // we now (mostly) share the decode data structure
46 #include <xed/xed-decoded-inst.h>
49 // establish a type equivalence for the xed_encoder_request_t and the corresponding xed_decoded_inst_t.
52 typedef struct xed_decoded_inst_s xed_encoder_request_s;
54 typedef xed_decoded_inst_t xed_encoder_request_t;
58 XED_DLL_EXPORT xed_iclass_enum_t
59 xed_encoder_request_get_iclass( const xed_encoder_request_t* p);
61 /////////////////////////////////////////////////////////
66 xed_encoder_request_set_iclass( xed_encoder_request_t* p,
67 xed_iclass_enum_t iclass);
72 /// For locked (atomic read-modify-write) memops requests.
73 XED_DLL_EXPORT void xed_encoder_request_set_lock(xed_encoder_request_t* p);
75 /// for REP(F3) and REPNE(F2) prefixe on string ops
76 XED_DLL_EXPORT void xed_encoder_request_set_repne(xed_encoder_request_t* p);
78 /// for REP(F3) and REPNE(F2) prefixe on string ops
79 XED_DLL_EXPORT void xed_encoder_request_set_rep(xed_encoder_request_t* p);
82 /// @name Primary Encode Functions
85 XED_DLL_EXPORT void xed_encoder_request_set_effective_operand_width( xed_encoder_request_t* p,
86 xed_uint_t width_bits);
88 XED_DLL_EXPORT void xed_encoder_request_set_effective_address_size( xed_encoder_request_t* p,
89 xed_uint_t width_bits);
91 * Set the operands array element indexed by operand to the actual register name reg.
93 * @param[in] p xed_encoder_request_t
94 * @param[in] operand indicates which register operand storage field to use
95 * @param[in] reg the actual register represented (EAX, etc.) to store.
97 XED_DLL_EXPORT void xed_encoder_request_set_reg(xed_encoder_request_t* p,
98 xed_operand_enum_t operand,
102 /// @name Operand Order
105 * Specify the name as the n'th operand in the operand order.
107 * The complication of this function is that the register operand names are
108 * specific to the position of the operand (REG0, REG1, REG2...). One can
109 * use this function for registers or one can use the
110 * xed_encoder_request_set_operand_name_reg() which takes integers instead
113 * @param[in] p #xed_encoder_request_t
114 * @param[in] operand_index xed_uint_t representing n'th operand position
115 * @param[in] name #xed_operand_enum_t operand name.
117 XED_DLL_EXPORT void xed_encoder_request_set_operand_order(xed_encoder_request_t* p,
118 xed_uint_t operand_index,
119 xed_operand_enum_t name);
122 * Retreive the name of the n'th operand in the operand order.
124 * @param[in] p #xed_encoder_request_t
125 * @param[in] operand_index xed_uint_t representing n'th operand position
126 * @return The #xed_operand_enum_t operand name.
128 XED_DLL_EXPORT xed_operand_enum_t xed_encoder_request_get_operand_order(xed_encoder_request_t* p,
129 xed_uint_t operand_index);
133 /// Retreive the number of entries in the encoder operand order array
134 /// @return The number of entries in the encoder operand order array
136 xed_uint_t xed_encoder_request_operand_order_entries(xed_encoder_request_t* p) {
137 return p->_n_operand_order;
143 /// @name branches and far pointers
146 XED_DLL_EXPORT void xed_encoder_request_set_relbr(xed_encoder_request_t* p);
148 XED_DLL_EXPORT void xed_encoder_request_set_branch_displacement(xed_encoder_request_t* p,
152 XED_DLL_EXPORT void xed_encoder_request_set_ptr(xed_encoder_request_t* p);
159 /// Set the uimm0 using a BYTE width.
160 XED_DLL_EXPORT void xed_encoder_request_set_uimm0(xed_encoder_request_t* p,
164 /// Set the uimm0 using a BIT width.
165 XED_DLL_EXPORT void xed_encoder_request_set_uimm0_bits(xed_encoder_request_t* p,
169 XED_DLL_EXPORT void xed_encoder_request_set_uimm1(xed_encoder_request_t* p,
172 /// same storage as uimm0
173 XED_DLL_EXPORT void xed_encoder_request_set_simm(xed_encoder_request_t* p,
181 XED_DLL_EXPORT void xed_encoder_request_set_memory_displacement(xed_encoder_request_t* p,
186 XED_DLL_EXPORT void xed_encoder_request_set_agen(xed_encoder_request_t* p);
188 XED_DLL_EXPORT void xed_encoder_request_set_mem0(xed_encoder_request_t* p);
190 XED_DLL_EXPORT void xed_encoder_request_set_mem1(xed_encoder_request_t* p);
192 XED_DLL_EXPORT void xed_encoder_request_set_memory_operand_length(xed_encoder_request_t* p,
195 XED_DLL_EXPORT void xed_encoder_request_set_seg0(xed_encoder_request_t* p,
196 xed_reg_enum_t seg_reg);
198 XED_DLL_EXPORT void xed_encoder_request_set_seg1(xed_encoder_request_t* p,
199 xed_reg_enum_t seg_reg);
201 XED_DLL_EXPORT void xed_encoder_request_set_base0(xed_encoder_request_t* p,
202 xed_reg_enum_t base_reg);
204 XED_DLL_EXPORT void xed_encoder_request_set_base1(xed_encoder_request_t* p,
205 xed_reg_enum_t base_reg) ;
207 XED_DLL_EXPORT void xed_encoder_request_set_index(xed_encoder_request_t* p,
208 xed_reg_enum_t index_reg);
210 XED_DLL_EXPORT void xed_encoder_request_set_scale(xed_encoder_request_t* p,
214 //////////////////////////////////////////////
216 XED_DLL_EXPORT const xed_operand_values_t* xed_encoder_request_operands_const(const xed_encoder_request_t* p);
218 XED_DLL_EXPORT xed_operand_values_t* xed_encoder_request_operands(xed_encoder_request_t* p);
220 /// @name Initialization
223 * clear the operand order array
224 * @param[in] p xed_encoder_request_t
226 XED_DLL_EXPORT void xed_encoder_request_zero_operand_order(xed_encoder_request_t* p);
229 XED_DLL_EXPORT void xed_encoder_request_zero_set_mode(xed_encoder_request_t* p,
230 const xed_state_t* dstate);
232 XED_DLL_EXPORT void xed_encoder_request_zero(xed_encoder_request_t* p) ;
235 struct xed_decoded_inst_s; //fwd decl
237 /// Converts an decoder request to a valid encoder request.
238 XED_DLL_EXPORT void xed_encoder_request_init_from_decode(struct xed_decoded_inst_s* d);
241 xed_encoder_request_encode_emit(xed_encoder_request_t* q,
242 const unsigned int bits,
243 const xed_uint64_t value);
246 xed_encoder_request__memop_compatible(const xed_encoder_request_t* p,
247 xed_operand_width_enum_t operand_width);
249 /// @name String Printing
252 XED_DLL_EXPORT void xed_encode_request_print(const xed_encoder_request_t* p,
253 char* buf, xed_uint_t buflen);
256 // Type signature for an encode function
257 typedef xed_uint_t (*xed_encode_function_pointer_t)(xed_encoder_request_t* enc_req);
262 /// This is the main interface to the encoder. The array should be
263 /// at most 15 bytes long. The ilen parameter should indiciate
264 /// this length. If the array is too short, the encoder may fail to
265 /// encode the request. Failure is indicated by a return value of
266 /// type #xed_error_enum_t that is not equal to
267 /// #XED_ERROR_NONE. Otherwise, #XED_ERROR_NONE is returned and the
268 /// length of the encoded instruction is returned in olen.
270 /// @param r encoder request description (#xed_encoder_request_t), includes mode info
271 /// @param array the encoded instruction bytes are stored here
272 /// @param ilen the input length of array.
273 /// @param olen the actual length of array used for encoding
274 /// @return success/failure as a #xed_error_enum_t
276 XED_DLL_EXPORT xed_error_enum_t
277 xed_encode(xed_encoder_request_t* r,
279 const unsigned int ilen,
282 /// This function will attempt to encode a NOP of exactly ilen
283 /// bytes. If such a NOP is not encodeable, then false will be returned.
285 /// @param array the encoded instruction bytes are stored here
286 /// @param ilen the input length array.
287 /// @return success/failure as a #xed_error_enum_t
289 XED_DLL_EXPORT xed_error_enum_t
290 xed_encode_nop(xed_uint8_t* array,
291 const unsigned int ilen);
295 ////////////////////////////////////////////////////////////////////////////
297 //pref: "../../xed-encode.c"