1
2 /*---------------------------------------------------------------*/
3 /*--- begin libvex_basictypes.h ---*/
4 /*---------------------------------------------------------------*/
5
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
10 Copyright (C) 2004-2013 OpenWorks LLP
11 info@open-works.net
12
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 02110-1301, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29
30 Neither the names of the U.S. Department of Energy nor the
31 University of California nor the names of its contributors may be
32 used to endorse or promote products derived from this software
33 without prior written permission.
34 */
35
36 #ifndef __LIBVEX_BASICTYPES_H
37 #define __LIBVEX_BASICTYPES_H
38
39 /* It is important that the sizes of the following data types (on the
40 host) are as stated. LibVEX_Init therefore checks these at
41 startup. */
42
43 /* Always 8 bits. */
44 typedef unsigned char UChar;
45 typedef signed char Char;
46 typedef char HChar; /* signfulness depends on host */
47 /* Only to be used for printf etc
48 format strings */
49
50 /* Always 16 bits. */
51 typedef unsigned short UShort;
52 typedef signed short Short;
53
54 /* Always 32 bits. */
55 typedef unsigned int UInt;
56 typedef signed int Int;
57
58 /* Always 64 bits. */
59 typedef unsigned long long int ULong;
60 typedef signed long long int Long;
61
62 /* Always 128 bits. */
63 typedef UInt U128[4];
64
65 /* Always 256 bits. */
66 typedef UInt U256[8];
67
68 /* A union for doing 128-bit vector primitives conveniently. */
69 typedef
70 union {
71 UChar w8[16];
72 UShort w16[8];
73 UInt w32[4];
74 ULong w64[2];
75 }
76 V128;
77
78 /* A union for doing 256-bit vector primitives conveniently. */
79 typedef
80 union {
81 UChar w8[32];
82 UShort w16[16];
83 UInt w32[8];
84 ULong w64[4];
85 }
86 V256;
87
88 /* Floating point. */
89 typedef float Float; /* IEEE754 single-precision (32-bit) value */
90 typedef double Double; /* IEEE754 double-precision (64-bit) value */
91
92 /* Bool is always 8 bits. */
93 typedef unsigned char Bool;
94 #define True ((Bool)1)
95 #define False ((Bool)0)
96
97 /* Use this to coerce the result of a C comparison to a Bool. This is
98 useful when compiling with Intel icc with ultra-paranoid
99 compilation flags (-Wall). */
toBool(Int x)100 static inline Bool toBool ( Int x ) {
101 Int r = (x == 0) ? False : True;
102 return (Bool)r;
103 }
toUChar(Int x)104 static inline UChar toUChar ( Int x ) {
105 x &= 0xFF;
106 return (UChar)x;
107 }
toHChar(Int x)108 static inline HChar toHChar ( Int x ) {
109 x &= 0xFF;
110 return (HChar)x;
111 }
toUShort(Int x)112 static inline UShort toUShort ( Int x ) {
113 x &= 0xFFFF;
114 return (UShort)x;
115 }
toShort(Int x)116 static inline Short toShort ( Int x ) {
117 x &= 0xFFFF;
118 return (Short)x;
119 }
toUInt(Long x)120 static inline UInt toUInt ( Long x ) {
121 x &= 0xFFFFFFFFLL;
122 return (UInt)x;
123 }
124
125 /* 32/64 bit addresses. */
126 typedef UInt Addr32;
127 typedef ULong Addr64;
128
129 /* Something which has the same size as void* on the host. That is,
130 it is 32 bits on a 32-bit host and 64 bits on a 64-bit host, and so
131 it can safely be coerced to and from a pointer type on the host
132 machine. */
133 typedef unsigned long HWord;
134
135
136 /* This is so useful it should be visible absolutely everywhere. */
137 #if !defined(offsetof)
138 # define offsetof(type,memb) ((Int)(HWord)&((type*)0)->memb)
139 #endif
140 /* Our definition of offsetof is giving the same result as
141 the standard/predefined offsetof. So, we use the same name.
142 We use a prefix vg_ for vg_alignof as its behaviour slightly
143 differs from the standard alignof/gcc defined __alignof__
144 */
145
146 #define vg_alignof(_type) (sizeof(struct {char c;_type _t;})-sizeof(_type))
147 /* vg_alignof returns a "safe" alignement.
148 "safe" is defined as the alignment chosen by the compiler in
149 a struct made of a char followed by this type.
150
151 Note that this is not necessarily the "preferred" alignment
152 for a platform. This preferred alignment is returned by the gcc
153 __alignof__ and by the standard (in recent standard) alignof.
154 Compared to __alignof__, vg_alignof gives on some platforms (e.g.
155 amd64, ppc32, ppc64) a bigger alignment for long double (16 bytes
156 instead of 8).
157 On some platforms (e.g. x86), vg_alignof gives a smaller alignment
158 than __alignof__ for long long and double (4 bytes instead of 8).
159 If we want to have the "preferred" alignment for the basic types,
160 then either we need to depend on gcc __alignof__, or on a (too)
161 recent standard and compiler (implementing <stdalign.h>).
162 */
163
164
165
166 /* We need to know the host word size in order to write Ptr_to_ULong
167 and ULong_to_Ptr in a way that doesn't cause compilers to complain.
168 These functions allow us to cast pointers to and from 64-bit
169 integers without complaints from compilers, regardless of the host
170 word size.
171
172 Also set up VEX_REGPARM.
173 */
174
175 #undef VEX_HOST_WORDSIZE
176 #undef VEX_REGPARM
177
178 /* The following 4 work OK for Linux. */
179 #if defined(__x86_64__)
180 # define VEX_HOST_WORDSIZE 8
181 # define VEX_REGPARM(_n) /* */
182
183 #elif defined(__i386__)
184 # define VEX_HOST_WORDSIZE 4
185 # define VEX_REGPARM(_n) __attribute__((regparm(_n)))
186
187 #elif defined(__powerpc__) && defined(__powerpc64__)
188 # define VEX_HOST_WORDSIZE 8
189 # define VEX_REGPARM(_n) /* */
190
191 #elif defined(__powerpc__) && !defined(__powerpc64__)
192 # define VEX_HOST_WORDSIZE 4
193 # define VEX_REGPARM(_n) /* */
194
195 #elif defined(__arm__) && !defined(__aarch64__)
196 # define VEX_HOST_WORDSIZE 4
197 # define VEX_REGPARM(_n) /* */
198
199 #elif defined(__aarch64__) && !defined(__arm__)
200 # define VEX_HOST_WORDSIZE 8
201 # define VEX_REGPARM(_n) /* */
202
203 #elif defined(__s390x__)
204 # define VEX_HOST_WORDSIZE 8
205 # define VEX_REGPARM(_n) /* */
206
207 #elif defined(__mips__) && (__mips == 64)
208 # define VEX_HOST_WORDSIZE 8
209 # define VEX_REGPARM(_n) /* */
210
211 #elif defined(__mips__) && (__mips != 64)
212 # define VEX_HOST_WORDSIZE 4
213 # define VEX_REGPARM(_n) /* */
214
215 #else
216 # error "Vex: Fatal: Can't establish the host architecture"
217 #endif
218
219
220 #if VEX_HOST_WORDSIZE == 8
Ptr_to_ULong(void * p)221 static inline ULong Ptr_to_ULong ( void* p ) {
222 return (ULong)p;
223 }
ULong_to_Ptr(ULong n)224 static inline void* ULong_to_Ptr ( ULong n ) {
225 return (void*)n;
226 }
227 #elif VEX_HOST_WORDSIZE == 4
Ptr_to_ULong(void * p)228 static inline ULong Ptr_to_ULong ( void* p ) {
229 UInt w = (UInt)p;
230 return (ULong)w;
231 }
ULong_to_Ptr(ULong n)232 static inline void* ULong_to_Ptr ( ULong n ) {
233 UInt w = (UInt)n;
234 return (void*)w;
235 }
236 #else
237 # error "Vex: Fatal: Can't define Ptr_to_ULong / ULong_to_Ptr"
238 #endif
239
240
241 #endif /* ndef __LIBVEX_BASICTYPES_H */
242
243 /*---------------------------------------------------------------*/
244 /*--- libvex_basictypes.h ---*/
245 /*---------------------------------------------------------------*/
246