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-immdis.h
32 /// @author Mark Charney <mark.charney@intel.com>
36 #ifndef _XED_IMMDIS_H_
37 # define _XED_IMMDIS_H_
39 #include <xed/xed-types.h>
40 #include <xed/xed-common-defs.h>
41 #include <xed/xed-util.h>
44 ////////////////////////////////////////////////////////////////////////////
46 ////////////////////////////////////////////////////////////////////////////
48 ////////////////////////////////////////////////////////////////////////////
50 ////////////////////////////////////////////////////////////////////////////
51 ////////////////////////////////////////////////////////////////////////////
53 ////////////////////////////////////////////////////////////////////////////
55 ////////////////////////////////////////////////////////////////////////////
57 ////////////////////////////////////////////////////////////////////////////
59 #define XED_MAX_IMMDIS_BYTES 8
61 // A union for speed of zeroing
62 union xed_immdis_values_t
64 xed_uint8_t x[XED_MAX_IMMDIS_BYTES];// STORED LITTLE ENDIAN. BYTE 0 is LSB
68 /// Stores immediates and displacements for the encoder & decoder.
69 typedef struct XED_DLL_EXPORT xed_immdis_s {
70 union xed_immdis_values_t value;
71 unsigned int currently_used_space :4; // current number of assigned bytes
72 unsigned int max_allocated_space :4; // max allocation, 4 or 8
73 xed_bool_t present : 1;
74 xed_bool_t immediate_is_unsigned : 1;
77 XED_DLL_EXPORT void xed_immdis__check(xed_immdis_t* q, int p) ;
80 XED_DLL_EXPORT void xed_immdis_init(xed_immdis_t* p, int max_bytes);
82 /// @name Sizes and lengths
84 /// return the number of bytes added
85 XED_DLL_EXPORT unsigned int xed_immdis_get_bytes(const xed_immdis_t* p) ;
89 /// @name Accessors for the value of the immediate or displacement
91 XED_DLL_EXPORT xed_int64_t
92 xed_immdis_get_signed64(const xed_immdis_t* p);
94 XED_DLL_EXPORT xed_uint64_t
95 xed_immdis_get_unsigned64(const xed_immdis_t* p);
97 XED_DLL_EXPORT xed_bool_t
98 xed_immdis_is_zero(const xed_immdis_t* p) ;
100 XED_DLL_EXPORT xed_bool_t
101 xed_immdis_is_one(const xed_immdis_t* p) ;
103 /// Access the i'th byte of the immediate
104 XED_DLL_EXPORT xed_uint8_t xed_immdis_get_byte(const xed_immdis_t* p, unsigned int i) ;
107 /// @name Presence / absence of an immediate or displacement
109 XED_DLL_EXPORT void xed_immdis_set_present(xed_immdis_t* p) ;
111 /// True if the object has had a value or individual bytes added to it.
112 XED_DLL_EXPORT xed_bool_t xed_immdis_is_present(const xed_immdis_t* p) ;
116 /// @name Initialization and setup
118 XED_DLL_EXPORT void xed_immdis_set_max_len(xed_immdis_t* p, unsigned int mx) ;
120 xed_immdis_zero(xed_immdis_t* p);
122 XED_DLL_EXPORT unsigned int xed_immdis_get_max_length(const xed_immdis_t* p) ;
126 /// @name Signed vs Unsigned
128 /// Return true if signed.
129 XED_DLL_EXPORT xed_bool_t
130 xed_immdis_is_unsigned(const xed_immdis_t* p) ;
131 /// Return true if signed.
132 XED_DLL_EXPORT xed_bool_t
133 xed_immdis_is_signed(const xed_immdis_t* p) ;
135 /// Set the immediate to be signed; For decoder use only.
137 xed_immdis_set_signed(xed_immdis_t* p) ;
138 /// Set the immediate to be unsigned; For decoder use only.
140 xed_immdis_set_unsigned( xed_immdis_t* p) ;
144 /// @name Adding / setting values
147 xed_immdis_add_byte(xed_immdis_t* p, xed_uint8_t b);
151 xed_immdis_add_byte_array(xed_immdis_t* p, int nb, xed_uint8_t* ba);
153 /// Add 1, 2, 4 or 8 bytes depending on the value x and the mask of
154 /// legal_widths. The default value of legal_widths = 0x5 only stops
155 /// adding bytes only on 1 or 4 byte quantities - depending on which
156 /// bytes of x are zero -- as is used for most memory addressing. You
157 /// can set legal_widths to 0x7 for branches (1, 2 or 4 byte branch
158 /// displacements). Or if you have an 8B displacement, you can set
159 /// legal_widths to 0x8. NOTE: add_shortest_width will add up to
160 /// XED_MAX_IMMDIS_BYTES if the x value requires it. NOTE: 16b memory
161 /// addressing can have 16b immediates.
163 xed_immdis_add_shortest_width_signed(xed_immdis_t* p, xed_int64_t x, xed_uint8_t legal_widths);
165 /// See add_shortest_width_signed()
167 xed_immdis_add_shortest_width_unsigned(xed_immdis_t* p, xed_uint64_t x, xed_uint8_t legal_widths );
170 /// add an 8 bit value to the byte array
172 xed_immdis_add8(xed_immdis_t* p, xed_int8_t d);
174 /// add a 16 bit value to the byte array
176 xed_immdis_add16(xed_immdis_t* p, xed_int16_t d);
178 /// add a 32 bit value to the byte array
180 xed_immdis_add32(xed_immdis_t* p, xed_int32_t d);
182 /// add a 64 bit value to the byte array.
184 xed_immdis_add64(xed_immdis_t* p, xed_int64_t d);
189 /// @name printing / debugging
192 /// just print the raw bytes in hex with a leading 0x
193 XED_DLL_EXPORT int xed_immdis_print(const xed_immdis_t* p, char* buf, int buflen);
195 /// Print the value as a signed or unsigned number depending on the
196 /// value of the immediate_is_unsigned variable.
198 xed_immdis_print_signed_or_unsigned(const xed_immdis_t* p, char* buf, int buflen);
200 /// print the signed value, appropriate width, with a leading 0x
202 xed_immdis_print_value_signed(const xed_immdis_t* p, char* buf, int buflen);
204 /// print the unsigned value, appropriate width, with a leading 0x
206 xed_immdis_print_value_unsigned(const xed_immdis_t* p, char* buf, int buflen);
208 int xed_immdis__print_ptr(const xed_immdis_t* p, char* buf, int buflen);
214 ////////////////////////////////////////////////////////////////////////////
216 //pref: "../../xed-immdis.c"