1 /* -*- mode: C; c-basic-offset: 3; -*- */
4 This file is part of MemCheck, a heavyweight Valgrind tool for
5 detecting memory errors.
7 Copyright (C) 2012-2017 Florian Krohm
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 The GNU General Public License is contained in the file COPYING.
25 #include <stdio.h> // fprintf
26 #include <stdlib.h> // exit
27 #include <assert.h> // assert
28 #if defined(__APPLE__)
29 #include <machine/endian.h>
30 #define __BYTE_ORDER BYTE_ORDER
31 #define __LITTLE_ENDIAN LITTLE_ENDIAN
33 #define __LITTLE_ENDIAN 1234
34 #define __BIG_ENDIAN 4321
35 # if defined(_LITTLE_ENDIAN)
36 # define __BYTE_ORDER __LITTLE_ENDIAN
38 # define __BYTE_ORDER __BIG_ENDIAN
40 #elif defined(__linux__)
43 #define __BYTE_ORDER BYTE_ORDER
44 #define __LITTLE_ENDIAN LITTLE_ENDIAN
45 #include <sys/endian.h>
50 #include "memcheck.h" // VALGRIND_MAKE_MEM_DEFINED
53 /* Something bad happened. Cannot continue. */
54 void __attribute__((noreturn
))
55 panic(const char *string
)
57 fprintf(stderr
, "*** OOPS: %s\n", string
);
62 /* Issue a complaint because the V-bits of the result of an operation
63 differ from what was expected. */
65 complain(const irop_t
*op
, const test_data_t
*data
, vbits_t expected
)
67 fprintf(stderr
, "*** Incorrect result for operator %s\n", op
->name
);
69 int num_operands
= get_num_operands(op
->op
);
71 for (unsigned i
= 0; i
< num_operands
; ++i
) {
72 fprintf(stderr
, " opnd %u: ", i
);
73 print_opnd(stderr
, &data
->opnds
[i
]);
74 fprintf(stderr
, "\n");
76 fprintf(stderr
, " result: ");
77 print_opnd(stderr
, &data
->result
);
78 fprintf(stderr
, "\n");
79 fprintf(stderr
, " expect: vbits = ");
80 print_vbits(stderr
, expected
);
81 fprintf(stderr
, "\n");
86 print_value(FILE *fp
, value_t val
, unsigned num_bits
)
89 case 1: fprintf(fp
, "%02x", val
.u8
); break;
90 case 8: fprintf(fp
, "%02x", val
.u8
); break;
91 case 16: fprintf(fp
, "%04x", val
.u16
); break;
92 case 32: fprintf(fp
, "%08x", val
.u32
); break;
93 case 64: fprintf(fp
, "%016"PRIx64
, val
.u64
); break;
95 if (__BYTE_ORDER
== __LITTLE_ENDIAN
) {
96 fprintf(fp
, "%016"PRIx64
, val
.u128
[1]);
97 fprintf(fp
, "%016"PRIx64
, val
.u128
[0]);
99 fprintf(fp
, "%016"PRIx64
, val
.u128
[0]);
100 fprintf(fp
, "%016"PRIx64
, val
.u128
[1]);
104 if (__BYTE_ORDER
== __LITTLE_ENDIAN
) {
105 fprintf(fp
, "%016"PRIx64
, val
.u256
[3]);
106 fprintf(fp
, "%016"PRIx64
, val
.u256
[2]);
107 fprintf(fp
, "%016"PRIx64
, val
.u256
[1]);
108 fprintf(fp
, "%016"PRIx64
, val
.u256
[0]);
110 fprintf(fp
, "%016"PRIx64
, val
.u256
[0]);
111 fprintf(fp
, "%016"PRIx64
, val
.u256
[1]);
112 fprintf(fp
, "%016"PRIx64
, val
.u256
[2]);
113 fprintf(fp
, "%016"PRIx64
, val
.u256
[3]);
123 print_opnd(FILE *fp
, const opnd_t
*opnd
)
125 fprintf(fp
, "vbits = ");
126 print_vbits(fp
, opnd
->vbits
);
127 /* The value itself might be partially or fully undefined, so take a
128 copy, paint the copy as defined, and print the copied value. This is
129 so as to avoid error messages from Memcheck, which are correct, but
131 volatile value_t value_copy
= opnd
->value
;
132 VALGRIND_MAKE_MEM_DEFINED(&value_copy
, sizeof(value_copy
));
133 fprintf(fp
, " value = ");
134 print_value(fp
, value_copy
, opnd
->vbits
.num_bits
);
139 is_floating_point_type(IRType type
)
157 is_floating_point_op_with_rounding_mode(IROp op
)
159 IRType t_dst
, t_arg1
, t_arg2
, t_arg3
, t_arg4
;
161 typeof_primop(op
, &t_dst
, &t_arg1
, &t_arg2
, &t_arg3
, &t_arg4
);
163 // A unary operator cannot have a rounding mode
164 if (t_arg2
== Ity_INVALID
) return 0;
166 if (is_floating_point_type(t_dst
) ||
167 is_floating_point_type(t_arg1
) ||
168 is_floating_point_type(t_arg2
) ||
169 is_floating_point_type(t_arg3
) ||
170 is_floating_point_type(t_arg4
)) {
171 // Rounding mode, if present, is the 1st operand
172 return t_arg1
== Ity_I32
;
178 /* Return the number of operands for which input values can
179 be freely chosen. For floating point ops, the rounding mode
180 is not counted here, as it is restricted. */
182 get_num_operands(IROp op
)
184 IRType unused
, t1
, t2
, t3
, t4
;
186 typeof_primop(op
, &unused
, &t1
, &t2
, &t3
, &t4
);
188 int num_operands
= 4;
189 if (t4
== Ity_INVALID
) num_operands
= 3;
190 if (t3
== Ity_INVALID
) num_operands
= 2;
191 if (t2
== Ity_INVALID
) num_operands
= 1;
193 if (is_floating_point_op_with_rounding_mode(op
))
201 sizeof_irtype(IRType ty
)
203 return sizeofIRType(ty
);
208 typeof_primop(IROp op
, IRType
*t_dst
, IRType
*t_arg1
, IRType
*t_arg2
,
209 IRType
*t_arg3
, IRType
*t_arg4
)
211 return typeOfPrimop(op
, t_dst
, t_arg1
, t_arg2
, t_arg3
, t_arg4
);