XED
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
xed-util.h
Go to the documentation of this file.
1 /*BEGIN_LEGAL
2 Intel Open Source License
3 
4 Copyright (c) 2002-2014 Intel Corporation. All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
8 met:
9 
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.
18 
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.
30 END_LEGAL */
33 
34 
35 
36 #ifndef _XED_UTIL_H_
37 # define _XED_UTIL_H_
38 
39 #include "xed-common-hdrs.h"
40 #include "xed-types.h"
41 #include "xed-portability.h"
42 
43 
45 // DEFINES
47 extern int xed_verbose;
48 #if XED_MESSAGES==1
49 # include <stdio.h>
50 extern FILE* xed_log_file;
51 #endif
52 #define XED_EMIT_MESSAGES (XED_MESSAGES==1 && xed_verbose >= 1)
53 #define XED_INFO_VERBOSE (XED_MESSAGES==1 && xed_verbose >= 2)
54 #define XED_INFO2_VERBOSE (XED_MESSAGES==1 && xed_verbose >= 3)
55 #define XED_VERBOSE (XED_MESSAGES==1 && xed_verbose >= 4)
56 #define XED_MORE_VERBOSE (XED_MESSAGES==1 && xed_verbose >= 5)
57 #define XED_VERY_VERBOSE (XED_MESSAGES==1 && xed_verbose >= 6)
58 
59 #if defined(__GNUC__)
60 # define XED_FUNCNAME __func__
61 #else
62 # define XED_FUNCNAME ""
63 #endif
64 
65 #if XED_MESSAGES==1
66 #define XED2IMSG(x) \
67  do { \
68  if (XED_EMIT_MESSAGES) { \
69  if (XED_VERY_VERBOSE) { \
70  fprintf(xed_log_file,"%s:%d:%s: ", \
71  __FILE__, __LINE__, XED_FUNCNAME); \
72  } \
73  fprintf x; \
74  fflush(xed_log_file); \
75  } \
76  } while(0)
77 
78 #define XED2TMSG(x) \
79  do { \
80  if (XED_VERBOSE) { \
81  if (XED_VERY_VERBOSE) { \
82  fprintf(xed_log_file,"%s:%d:%s: ", \
83  __FILE__, __LINE__, XED_FUNCNAME); \
84  } \
85  fprintf x; \
86  fflush(xed_log_file); \
87  } \
88  } while(0)
89 
90 #define XED2VMSG(x) \
91  do { \
92  if (XED_VERY_VERBOSE) { \
93  fprintf(xed_log_file,"%s:%d:%s: ", \
94  __FILE__, __LINE__, XED_FUNCNAME); \
95  fprintf x; \
96  fflush(xed_log_file); \
97  } \
98  } while(0)
99 
100 #define XED2DIE(x) \
101  do { \
102  if (XED_EMIT_MESSAGES) { \
103  fprintf(xed_log_file,"%s:%d:%s: ", \
104  __FILE__, __LINE__, XED_FUNCNAME); \
105  fprintf x; \
106  fflush(xed_log_file); \
107  } \
108  xed_assert(0); \
109  } while(0)
110 
111 
112 
113 #else
114 # define XED2IMSG(x)
115 # define XED2TMSG(x)
116 # define XED2VMSG(x)
117 # define XED2DIE(x) do { xed_assert(0); } while(0)
118 #endif
119 
120 #if defined(XED_ASSERTS)
121 # define xed_assert(x) do { if (( x )== 0) xed_internal_assert( #x, __FILE__, __LINE__); } while(0)
122 #else
123 # define xed_assert(x) do { } while(0)
124 #endif
125 XED_NORETURN XED_NOINLINE XED_DLL_EXPORT void xed_internal_assert(const char* s, const char* file, int line);
126 
127 typedef void (*xed_user_abort_function_t)(const char* msg,
128  const char* file,
129  int line,
130  void* other);
131 
152  void* other);
153 
154 
156 // PROTOTYPES
158 char* xed_downcase_buf(char* s);
159 
160 /* copy from src to dst, downcasing bytes as the copy proceeds. len is the
161  * available space in the buffer*/
162 int xed_strncat_lower(char* dst, const char* src, int len);
163 
164 XED_DLL_EXPORT int xed_itoa(char* buf,
165  xed_uint64_t f,
166  int buflen);
167 
168 int xed_itoa_hex_zeros(char* buf,
169  xed_uint64_t f,
170  xed_uint_t xed_bits_to_print,
171  xed_bool_t leading_zeros,
172  int buflen);
173 
174 XED_DLL_EXPORT int xed_itoa_hex(char* buf,
175  xed_uint64_t f,
176  xed_uint_t xed_bits_to_print,
177  int buflen);
178 
179 int xed_itoa_signed(char* buf, xed_int64_t f, int buflen);
180 
182 
183 int xed_sprintf_uint8_hex(char* buf, xed_uint8_t x, int buflen);
184 int xed_sprintf_uint16_hex(char* buf, xed_uint16_t x, int buflen);
185 int xed_sprintf_uint32_hex(char* buf, xed_uint32_t x, int buflen);
186 int xed_sprintf_uint64_hex(char* buf, xed_uint64_t x, int buflen);
187 int xed_sprintf_uint8(char* buf, xed_uint8_t x, int buflen);
188 int xed_sprintf_uint16(char* buf, xed_uint16_t x, int buflen);
189 int xed_sprintf_uint32(char* buf, xed_uint32_t x, int buflen);
190 int xed_sprintf_uint64(char* buf, xed_uint64_t x, int buflen);
191 int xed_sprintf_int8(char* buf, xed_int8_t x, int buflen);
192 int xed_sprintf_int16(char* buf, xed_int16_t x, int buflen);
193 int xed_sprintf_int32(char* buf, xed_int32_t x, int buflen);
194 int xed_sprintf_int64(char* buf, xed_int64_t x, int buflen);
195 
198 XED_DLL_EXPORT void xed_set_log_file(void* o);
199 
200 
203 
204 void xed_derror(const char* s);
205 void xed_dwarn(const char* s);
206 
207 XED_DLL_EXPORT xed_int64_t xed_sign_extend32_64(xed_int32_t x);
208 XED_DLL_EXPORT xed_int64_t xed_sign_extend16_64(xed_int16_t x);
209 XED_DLL_EXPORT xed_int64_t xed_sign_extend8_64(xed_int8_t x);
210 
211 XED_DLL_EXPORT xed_int32_t xed_sign_extend16_32(xed_int16_t x);
212 XED_DLL_EXPORT xed_int32_t xed_sign_extend8_32(xed_int8_t x);
213 
214 XED_DLL_EXPORT xed_int16_t xed_sign_extend8_16(xed_int8_t x);
215 
217 XED_DLL_EXPORT xed_int32_t xed_sign_extend_arbitrary_to_32(xed_uint32_t x, unsigned int bits);
218 
220 XED_DLL_EXPORT xed_int64_t xed_sign_extend_arbitrary_to_64(xed_uint64_t x, unsigned int bits);
221 
222 
223 XED_DLL_EXPORT xed_uint64_t xed_zero_extend32_64(xed_uint32_t x);
224 XED_DLL_EXPORT xed_uint64_t xed_zero_extend16_64(xed_uint16_t x);
225 XED_DLL_EXPORT xed_uint64_t xed_zero_extend8_64(xed_uint8_t x);
226 
227 XED_DLL_EXPORT xed_uint32_t xed_zero_extend16_32(xed_uint16_t x);
228 XED_DLL_EXPORT xed_uint32_t xed_zero_extend8_32(xed_uint8_t x);
229 
230 XED_DLL_EXPORT xed_uint16_t xed_zero_extend8_16(xed_uint8_t x);
231 
232 #if defined(XED_LITTLE_ENDIAN_SWAPPING)
233 XED_DLL_EXPORT xed_int32_t
234 xed_little_endian_to_int32(xed_uint64_t x, unsigned int len);
235 
236 XED_DLL_EXPORT xed_int64_t
237 xed_little_endian_to_int64(xed_uint64_t x, unsigned int len);
238 XED_DLL_EXPORT xed_uint64_t
239 xed_little_endian_to_uint64(xed_uint64_t x, unsigned int len);
240 
241 XED_DLL_EXPORT xed_int64_t
242 xed_little_endian_hilo_to_int64(xed_uint32_t hi_le, xed_uint32_t lo_le, unsigned int len);
243 XED_DLL_EXPORT xed_uint64_t
244 xed_little_endian_hilo_to_uint64(xed_uint32_t hi_le, xed_uint32_t lo_le, unsigned int len);
245 #endif
246 
247 XED_DLL_EXPORT xed_uint8_t
248 xed_get_byte(xed_uint64_t x, unsigned int i, unsigned int len);
249 
250 static XED_INLINE xed_uint64_t xed_make_uint64(xed_uint32_t hi, xed_uint32_t lo) {
251  xed_union64_t y;
252  y.s.lo32= lo;
253  y.s.hi32= hi;
254  return y.u64;
255 }
256 static XED_INLINE xed_int64_t xed_make_int64(xed_uint32_t hi, xed_uint32_t lo) {
257  xed_union64_t y;
258  y.s.lo32= lo;
259  y.s.hi32= hi;
260  return y.i64;
261 }
262 
269 XED_DLL_EXPORT xed_uint_t xed_shortest_width_unsigned(xed_uint64_t x, xed_uint8_t legal_widths);
270 
276 XED_DLL_EXPORT xed_uint_t xed_shortest_width_signed(xed_int64_t x, xed_uint8_t legal_widths);
277 
279 // GLOBALS
281 
283 #endif
xed_uint32_t hi32
Definition: xed-types.h:130
int xed_sprintf_uint32_hex(char *buf, xed_uint32_t x, int buflen)
int xed_sprintf_int64(char *buf, xed_int64_t x, int buflen)
int xed_strncat_lower(char *dst, const char *src, int len)
char xed_to_ascii_hex_nibble(xed_uint_t x)
XED_DLL_EXPORT int xed_itoa_hex(char *buf, xed_uint64_t f, xed_uint_t xed_bits_to_print, int buflen)
XED_DLL_EXPORT xed_uint16_t xed_zero_extend8_16(xed_uint8_t x)
void xed_derror(const char *s)
xed_uint64_t u64
Definition: xed-types.h:132
XED_DLL_EXPORT xed_uint32_t xed_zero_extend16_32(xed_uint16_t x)
XED_DLL_EXPORT xed_int32_t xed_sign_extend8_32(xed_int8_t x)
XED_DLL_EXPORT xed_uint64_t xed_zero_extend16_64(xed_uint16_t x)
#define XED_NOINLINE
unsigned int xed_bool_t
Definition: xed-types.h:68
XED_DLL_EXPORT xed_uint64_t xed_zero_extend32_64(xed_uint32_t x)
struct xed_union64_t::@12 s
void(* xed_user_abort_function_t)(const char *msg, const char *file, int line, void *other)
Definition: xed-util.h:127
static XED_INLINE xed_int64_t xed_make_int64(xed_uint32_t hi, xed_uint32_t lo)
Definition: xed-util.h:256
#define XED_INLINE
int xed_itoa_hex_zeros(char *buf, xed_uint64_t f, xed_uint_t xed_bits_to_print, xed_bool_t leading_zeros, int buflen)
int xed_sprintf_int8(char *buf, xed_int8_t x, int buflen)
XED_DLL_EXPORT xed_uint32_t xed_zero_extend8_32(xed_uint8_t x)
xed_int64_t i64
Definition: xed-types.h:133
int xed_sprintf_uint8(char *buf, xed_uint8_t x, int buflen)
XED_DLL_EXPORT xed_uint_t xed_shortest_width_unsigned(xed_uint64_t x, xed_uint8_t legal_widths)
returns the number of bytes required to store the UNSIGNED number x given a mask of legal lengths...
XED_DLL_EXPORT xed_int32_t xed_sign_extend16_32(xed_int16_t x)
XED_DLL_EXPORT void xed_set_log_file(void *o)
Set the FILE* for XED's log msgs.
int xed_sprintf_uint64_hex(char *buf, xed_uint64_t x, int buflen)
int xed_sprintf_uint16(char *buf, xed_uint16_t x, int buflen)
int xed_sprintf_uint64(char *buf, xed_uint64_t x, int buflen)
int xed_sprintf_uint32(char *buf, xed_uint32_t x, int buflen)
char * xed_downcase_buf(char *s)
int xed_verbose
XED_NORETURN XED_NOINLINE XED_DLL_EXPORT void xed_internal_assert(const char *s, const char *file, int line)
XED_DLL_EXPORT int xed_itoa(char *buf, xed_uint64_t f, int buflen)
unsigned int xed_uint_t
Definition: xed-types.h:65
XED_DLL_EXPORT xed_uint64_t xed_zero_extend8_64(xed_uint8_t x)
int xed_itoa_signed(char *buf, xed_int64_t f, int buflen)
int xed_sprintf_int16(char *buf, xed_int16_t x, int buflen)
int xed_sprintf_uint16_hex(char *buf, xed_uint16_t x, int buflen)
XED_DLL_EXPORT xed_int64_t xed_sign_extend16_64(xed_int16_t x)
#define XED_NORETURN
XED_DLL_EXPORT xed_int64_t xed_sign_extend_arbitrary_to_64(xed_uint64_t x, unsigned int bits)
arbitrary sign extension from a qty of "bits" length to 64b
XED_DLL_EXPORT xed_int16_t xed_sign_extend8_16(xed_int8_t x)
XED_DLL_EXPORT xed_int32_t xed_sign_extend_arbitrary_to_32(xed_uint32_t x, unsigned int bits)
arbitrary sign extension from a qty of "bits" length to 32b
XED_DLL_EXPORT xed_uint8_t xed_get_byte(xed_uint64_t x, unsigned int i, unsigned int len)
static XED_INLINE xed_uint64_t xed_make_uint64(xed_uint32_t hi, xed_uint32_t lo)
Definition: xed-util.h:250
XED_DLL_EXPORT xed_int64_t xed_sign_extend8_64(xed_int8_t x)
int xed_sprintf_int32(char *buf, xed_int32_t x, int buflen)
void xed_dwarn(const char *s)
XED_DLL_EXPORT void xed_set_verbosity(int v)
Set the verbosity level for XED.
XED_DLL_EXPORT xed_int64_t xed_sign_extend32_64(xed_int32_t x)
int xed_sprintf_uint8_hex(char *buf, xed_uint8_t x, int buflen)
XED_DLL_EXPORT void xed_register_abort_function(xed_user_abort_function_t fn, void *other)
This is for registering a function to be called during XED's assert processing.
#define XED_DLL_EXPORT
xed_uint32_t lo32
Definition: xed-types.h:129
XED_DLL_EXPORT xed_uint_t xed_shortest_width_signed(xed_int64_t x, xed_uint8_t legal_widths)
returns the number of bytes required to store the SIGNED number x given a mask of legal lengths...