Bug 439685 compiler warning in callgrind/main.c
[valgrind.git] / VEX / priv / guest_x86_defs.h
blob3f86339bc14e0eb07020fc59d98c3e9e70d43247
2 /*---------------------------------------------------------------*/
3 /*--- begin guest_x86_defs.h ---*/
4 /*---------------------------------------------------------------*/
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
10 Copyright (C) 2004-2017 OpenWorks LLP
11 info@open-works.net
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.
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.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, see <http://www.gnu.org/licenses/>.
26 The GNU General Public License is contained in the file COPYING.
28 Neither the names of the U.S. Department of Energy nor the
29 University of California nor the names of its contributors may be
30 used to endorse or promote products derived from this software
31 without prior written permission.
34 /* Only to be used within the guest-x86 directory. */
36 #ifndef __VEX_GUEST_X86_DEFS_H
37 #define __VEX_GUEST_X86_DEFS_H
39 #include "libvex_basictypes.h"
40 #include "libvex_guest_x86.h" // VexGuestX86State
41 #include "libvex_emnote.h" // VexEmNote
42 #include "guest_generic_bb_to_IR.h" // DisResult
44 /*---------------------------------------------------------*/
45 /*--- x86 to IR conversion ---*/
46 /*---------------------------------------------------------*/
48 /* Convert one x86 insn to IR. See the type DisOneInstrFn in
49 guest_generic_bb_to_IR.h. */
50 extern
51 DisResult disInstr_X86 ( IRSB* irbb,
52 const UChar* guest_code,
53 Long delta,
54 Addr guest_IP,
55 VexArch guest_arch,
56 const VexArchInfo* archinfo,
57 const VexAbiInfo* abiinfo,
58 VexEndness host_endness,
59 Bool sigill_diag );
61 /* Used by the optimiser to specialise calls to helpers. */
62 extern
63 IRExpr* guest_x86_spechelper ( const HChar* function_name,
64 IRExpr** args,
65 IRStmt** precedingStmts,
66 Int n_precedingStmts );
68 /* Describes to the optimiser which part of the guest state require
69 precise memory exceptions. This is logically part of the guest
70 state description. */
71 extern
72 Bool guest_x86_state_requires_precise_mem_exns ( Int, Int,
73 VexRegisterUpdates );
75 extern
76 VexGuestLayout x86guest_layout;
79 /*---------------------------------------------------------*/
80 /*--- x86 guest helpers ---*/
81 /*---------------------------------------------------------*/
83 /* --- CLEAN HELPERS --- */
85 extern UInt x86g_calculate_eflags_all (
86 UInt cc_op, UInt cc_dep1, UInt cc_dep2, UInt cc_ndep
89 VEX_REGPARM(3)
90 extern UInt x86g_calculate_eflags_c (
91 UInt cc_op, UInt cc_dep1, UInt cc_dep2, UInt cc_ndep
94 extern UInt x86g_calculate_condition (
95 UInt/*X86Condcode*/ cond,
96 UInt cc_op,
97 UInt cc_dep1, UInt cc_dep2, UInt cc_ndep
100 extern UInt x86g_calculate_FXAM ( UInt tag, ULong dbl );
102 extern ULong x86g_calculate_RCR (
103 UInt arg, UInt rot_amt, UInt eflags_in, UInt sz
105 extern ULong x86g_calculate_RCL (
106 UInt arg, UInt rot_amt, UInt eflags_in, UInt sz
109 extern UInt x86g_calculate_daa_das_aaa_aas ( UInt AX_and_flags, UInt opcode );
111 extern UInt x86g_calculate_aad_aam ( UInt AX_and_flags, UInt opcode );
113 extern ULong x86g_check_fldcw ( UInt fpucw );
115 extern UInt x86g_create_fpucw ( UInt fpround );
117 extern ULong x86g_check_ldmxcsr ( UInt mxcsr );
119 extern UInt x86g_create_mxcsr ( UInt sseround );
122 /* Translate a guest virtual_addr into a guest linear address by
123 consulting the supplied LDT/GDT structures. Their representation
124 must be as specified in pub/libvex_guest_x86.h. To indicate a
125 translation failure, 1<<32 is returned. On success, the lower 32
126 bits of the returned result indicate the linear address.
128 extern
129 ULong x86g_use_seg_selector ( HWord ldt, HWord gdt,
130 UInt seg_selector, UInt virtual_addr );
132 extern ULong x86g_calculate_mmx_pmaddwd ( ULong, ULong );
133 extern ULong x86g_calculate_mmx_psadbw ( ULong, ULong );
136 /* --- DIRTY HELPERS --- */
138 extern ULong x86g_dirtyhelper_loadF80le ( Addr );
140 extern void x86g_dirtyhelper_storeF80le ( Addr, ULong );
142 extern void x86g_dirtyhelper_CPUID_sse0 ( VexGuestX86State* );
143 extern void x86g_dirtyhelper_CPUID_mmxext ( VexGuestX86State* );
144 extern void x86g_dirtyhelper_CPUID_sse1 ( VexGuestX86State* );
145 extern void x86g_dirtyhelper_CPUID_sse2 ( VexGuestX86State* );
146 extern void x86g_dirtyhelper_CPUID_sse3 ( VexGuestX86State* );
148 extern void x86g_dirtyhelper_FINIT ( VexGuestX86State* );
150 extern void x86g_dirtyhelper_FXSAVE ( VexGuestX86State*, HWord );
151 extern void x86g_dirtyhelper_FSAVE ( VexGuestX86State*, HWord );
152 extern void x86g_dirtyhelper_FSTENV ( VexGuestX86State*, HWord );
154 extern ULong x86g_dirtyhelper_RDTSC ( void );
156 extern UInt x86g_dirtyhelper_IN ( UInt portno, UInt sz/*1,2 or 4*/ );
157 extern void x86g_dirtyhelper_OUT ( UInt portno, UInt data,
158 UInt sz/*1,2 or 4*/ );
160 extern void x86g_dirtyhelper_SxDT ( void* address,
161 UInt op /* 0 or 1 */ );
163 extern VexEmNote
164 x86g_dirtyhelper_FXRSTOR ( VexGuestX86State*, HWord );
166 extern VexEmNote
167 x86g_dirtyhelper_FRSTOR ( VexGuestX86State*, HWord );
169 extern VexEmNote
170 x86g_dirtyhelper_FLDENV ( VexGuestX86State*, HWord );
173 /*---------------------------------------------------------*/
174 /*--- Condition code stuff ---*/
175 /*---------------------------------------------------------*/
177 /* eflags masks */
178 #define X86G_CC_SHIFT_O 11
179 #define X86G_CC_SHIFT_S 7
180 #define X86G_CC_SHIFT_Z 6
181 #define X86G_CC_SHIFT_A 4
182 #define X86G_CC_SHIFT_C 0
183 #define X86G_CC_SHIFT_P 2
185 #define X86G_CC_MASK_O (1 << X86G_CC_SHIFT_O)
186 #define X86G_CC_MASK_S (1 << X86G_CC_SHIFT_S)
187 #define X86G_CC_MASK_Z (1 << X86G_CC_SHIFT_Z)
188 #define X86G_CC_MASK_A (1 << X86G_CC_SHIFT_A)
189 #define X86G_CC_MASK_C (1 << X86G_CC_SHIFT_C)
190 #define X86G_CC_MASK_P (1 << X86G_CC_SHIFT_P)
192 /* additional eflags masks */
193 #define X86G_CC_SHIFT_ID 21
194 #define X86G_CC_SHIFT_AC 18
195 #define X86G_CC_SHIFT_D 10
197 #define X86G_CC_MASK_ID (1 << X86G_CC_SHIFT_ID)
198 #define X86G_CC_MASK_AC (1 << X86G_CC_SHIFT_AC)
199 #define X86G_CC_MASK_D (1 << X86G_CC_SHIFT_D)
201 /* FPU flag masks */
202 #define X86G_FC_SHIFT_C3 14
203 #define X86G_FC_SHIFT_C2 10
204 #define X86G_FC_SHIFT_C1 9
205 #define X86G_FC_SHIFT_C0 8
207 #define X86G_FC_MASK_C3 (1 << X86G_FC_SHIFT_C3)
208 #define X86G_FC_MASK_C2 (1 << X86G_FC_SHIFT_C2)
209 #define X86G_FC_MASK_C1 (1 << X86G_FC_SHIFT_C1)
210 #define X86G_FC_MASK_C0 (1 << X86G_FC_SHIFT_C0)
213 /* %EFLAGS thunk descriptors. A four-word thunk is used to record
214 details of the most recent flag-setting operation, so the flags can
215 be computed later if needed. It is possible to do this a little
216 more efficiently using a 3-word thunk, but that makes it impossible
217 to describe the flag data dependencies sufficiently accurately for
218 Memcheck. Hence 4 words are used, with minimal loss of efficiency.
220 The four words are:
222 CC_OP, which describes the operation.
224 CC_DEP1 and CC_DEP2. These are arguments to the operation.
225 We want Memcheck to believe that the resulting flags are
226 data-dependent on both CC_DEP1 and CC_DEP2, hence the
227 name DEP.
229 CC_NDEP. This is a 3rd argument to the operation which is
230 sometimes needed. We arrange things so that Memcheck does
231 not believe the resulting flags are data-dependent on CC_NDEP
232 ("not dependent").
234 To make Memcheck believe that (the definedness of) the encoded
235 flags depends only on (the definedness of) CC_DEP1 and CC_DEP2
236 requires two things:
238 (1) In the guest state layout info (x86guest_layout), CC_OP and
239 CC_NDEP are marked as always defined.
241 (2) When passing the thunk components to an evaluation function
242 (calculate_condition, calculate_eflags, calculate_eflags_c) the
243 IRCallee's mcx_mask must be set so as to exclude from
244 consideration all passed args except CC_DEP1 and CC_DEP2.
246 Strictly speaking only (2) is necessary for correctness. However,
247 (1) helps efficiency in that since (2) means we never ask about the
248 definedness of CC_OP or CC_NDEP, we may as well not even bother to
249 track their definedness.
251 When building the thunk, it is always necessary to write words into
252 CC_DEP1 and CC_DEP2, even if those args are not used given the
253 CC_OP field (eg, CC_DEP2 is not used if CC_OP is CC_LOGIC1/2/4).
254 This is important because otherwise Memcheck could give false
255 positives as it does not understand the relationship between the
256 CC_OP field and CC_DEP1 and CC_DEP2, and so believes that the
257 definedness of the stored flags always depends on both CC_DEP1 and
258 CC_DEP2.
260 However, it is only necessary to set CC_NDEP when the CC_OP value
261 requires it, because Memcheck ignores CC_NDEP, and the evaluation
262 functions do understand the CC_OP fields and will only examine
263 CC_NDEP for suitable values of CC_OP.
265 A summary of the field usages is:
267 Operation DEP1 DEP2 NDEP
268 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
270 add/sub/mul first arg second arg unused
272 adc/sbb first arg (second arg)
273 XOR old_carry old_carry
275 and/or/xor result zero unused
277 inc/dec result zero old_carry
279 shl/shr/sar result subshifted- unused
280 result
282 rol/ror result zero old_flags
284 copy old_flags zero unused.
287 Therefore Memcheck will believe the following:
289 * add/sub/mul -- definedness of result flags depends on definedness
290 of both args.
292 * adc/sbb -- definedness of result flags depends on definedness of
293 both args and definedness of the old C flag. Because only two
294 DEP fields are available, the old C flag is XOR'd into the second
295 arg so that Memcheck sees the data dependency on it. That means
296 the NDEP field must contain a second copy of the old C flag
297 so that the evaluation functions can correctly recover the second
298 arg.
300 * and/or/xor are straightforward -- definedness of result flags
301 depends on definedness of result value.
303 * inc/dec -- definedness of result flags depends only on
304 definedness of result. This isn't really true -- it also depends
305 on the old C flag. However, we don't want Memcheck to see that,
306 and so the old C flag must be passed in NDEP and not in DEP2.
307 It's inconceivable that a compiler would generate code that puts
308 the C flag in an undefined state, then does an inc/dec, which
309 leaves C unchanged, and then makes a conditional jump/move based
310 on C. So our fiction seems a good approximation.
312 * shl/shr/sar -- straightforward, again, definedness of result
313 flags depends on definedness of result value. The subshifted
314 value (value shifted one less) is also needed, but its
315 definedness is the same as the definedness of the shifted value.
317 * rol/ror -- these only set O and C, and leave A Z C P alone.
318 However it seems prudent (as per inc/dec) to say the definedness
319 of all resulting flags depends on the definedness of the result,
320 hence the old flags must go in as NDEP and not DEP2.
322 * rcl/rcr are too difficult to do in-line, and so are done by a
323 helper function. They are not part of this scheme. The helper
324 function takes the value to be rotated, the rotate amount and the
325 old flags, and returns the new flags and the rotated value.
326 Since the helper's mcx_mask does not have any set bits, Memcheck
327 will lazily propagate undefinedness from any of the 3 args into
328 both results (flags and actual value).
330 enum {
331 X86G_CC_OP_COPY=0, /* DEP1 = current flags, DEP2 = 0, NDEP = unused */
332 /* just copy DEP1 to output */
334 X86G_CC_OP_ADDB, /* 1 */
335 X86G_CC_OP_ADDW, /* 2 DEP1 = argL, DEP2 = argR, NDEP = unused */
336 X86G_CC_OP_ADDL, /* 3 */
338 X86G_CC_OP_SUBB, /* 4 */
339 X86G_CC_OP_SUBW, /* 5 DEP1 = argL, DEP2 = argR, NDEP = unused */
340 X86G_CC_OP_SUBL, /* 6 */
342 X86G_CC_OP_ADCB, /* 7 */
343 X86G_CC_OP_ADCW, /* 8 DEP1 = argL, DEP2 = argR ^ oldCarry, NDEP = oldCarry */
344 X86G_CC_OP_ADCL, /* 9 */
346 X86G_CC_OP_SBBB, /* 10 */
347 X86G_CC_OP_SBBW, /* 11 DEP1 = argL, DEP2 = argR ^ oldCarry, NDEP = oldCarry */
348 X86G_CC_OP_SBBL, /* 12 */
350 X86G_CC_OP_LOGICB, /* 13 */
351 X86G_CC_OP_LOGICW, /* 14 DEP1 = result, DEP2 = 0, NDEP = unused */
352 X86G_CC_OP_LOGICL, /* 15 */
354 X86G_CC_OP_INCB, /* 16 */
355 X86G_CC_OP_INCW, /* 17 DEP1 = result, DEP2 = 0, NDEP = oldCarry (0 or 1) */
356 X86G_CC_OP_INCL, /* 18 */
358 X86G_CC_OP_DECB, /* 19 */
359 X86G_CC_OP_DECW, /* 20 DEP1 = result, DEP2 = 0, NDEP = oldCarry (0 or 1) */
360 X86G_CC_OP_DECL, /* 21 */
362 X86G_CC_OP_SHLB, /* 22 DEP1 = res, DEP2 = res', NDEP = unused */
363 X86G_CC_OP_SHLW, /* 23 where res' is like res but shifted one bit less */
364 X86G_CC_OP_SHLL, /* 24 */
366 X86G_CC_OP_SHRB, /* 25 DEP1 = res, DEP2 = res', NDEP = unused */
367 X86G_CC_OP_SHRW, /* 26 where res' is like res but shifted one bit less */
368 X86G_CC_OP_SHRL, /* 27 */
370 X86G_CC_OP_ROLB, /* 28 */
371 X86G_CC_OP_ROLW, /* 29 DEP1 = res, DEP2 = 0, NDEP = old flags */
372 X86G_CC_OP_ROLL, /* 30 */
374 X86G_CC_OP_RORB, /* 31 */
375 X86G_CC_OP_RORW, /* 32 DEP1 = res, DEP2 = 0, NDEP = old flags */
376 X86G_CC_OP_RORL, /* 33 */
378 X86G_CC_OP_UMULB, /* 34 */
379 X86G_CC_OP_UMULW, /* 35 DEP1 = argL, DEP2 = argR, NDEP = unused */
380 X86G_CC_OP_UMULL, /* 36 */
382 X86G_CC_OP_SMULB, /* 37 */
383 X86G_CC_OP_SMULW, /* 38 DEP1 = argL, DEP2 = argR, NDEP = unused */
384 X86G_CC_OP_SMULL, /* 39 */
386 X86G_CC_OP_NUMBER
389 typedef
390 enum {
391 X86CondO = 0, /* overflow */
392 X86CondNO = 1, /* no overflow */
394 X86CondB = 2, /* below */
395 X86CondNB = 3, /* not below */
397 X86CondZ = 4, /* zero */
398 X86CondNZ = 5, /* not zero */
400 X86CondBE = 6, /* below or equal */
401 X86CondNBE = 7, /* not below or equal */
403 X86CondS = 8, /* negative */
404 X86CondNS = 9, /* not negative */
406 X86CondP = 10, /* parity even */
407 X86CondNP = 11, /* not parity even */
409 X86CondL = 12, /* jump less */
410 X86CondNL = 13, /* not less */
412 X86CondLE = 14, /* less or equal */
413 X86CondNLE = 15, /* not less or equal */
415 X86CondAlways = 16 /* HACK */
417 X86Condcode;
419 #endif /* ndef __VEX_GUEST_X86_DEFS_H */
421 /*---------------------------------------------------------------*/
422 /*--- end guest_x86_defs.h ---*/
423 /*---------------------------------------------------------------*/