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
46 #include "memcheck.h" // VALGRIND_MAKE_MEM_DEFINED
49 /* Something bad happened. Cannot continue. */
50 void __attribute__((noreturn
))
51 panic(const char *string
)
53 fprintf(stderr
, "*** OOPS: %s\n", string
);
58 /* Issue a complaint because the V-bits of the result of an operation
59 differ from what was expected. */
61 complain(const irop_t
*op
, const test_data_t
*data
, vbits_t expected
)
63 fprintf(stderr
, "*** Incorrect result for operator %s\n", op
->name
);
65 int num_operands
= get_num_operands(op
->op
);
67 for (unsigned i
= 0; i
< num_operands
; ++i
) {
68 fprintf(stderr
, " opnd %u: ", i
);
69 print_opnd(stderr
, &data
->opnds
[i
]);
70 fprintf(stderr
, "\n");
72 fprintf(stderr
, " result: ");
73 print_opnd(stderr
, &data
->result
);
74 fprintf(stderr
, "\n");
75 fprintf(stderr
, " expect: vbits = ");
76 print_vbits(stderr
, expected
);
77 fprintf(stderr
, "\n");
82 print_value(FILE *fp
, value_t val
, unsigned num_bits
)
85 case 1: fprintf(fp
, "%02x", val
.u8
); break;
86 case 8: fprintf(fp
, "%02x", val
.u8
); break;
87 case 16: fprintf(fp
, "%04x", val
.u16
); break;
88 case 32: fprintf(fp
, "%08x", val
.u32
); break;
89 case 64: fprintf(fp
, "%016"PRIx64
, val
.u64
); break;
91 if (__BYTE_ORDER
== __LITTLE_ENDIAN
) {
92 fprintf(fp
, "%016"PRIx64
, val
.u128
[1]);
93 fprintf(fp
, "%016"PRIx64
, val
.u128
[0]);
95 fprintf(fp
, "%016"PRIx64
, val
.u128
[0]);
96 fprintf(fp
, "%016"PRIx64
, val
.u128
[1]);
100 if (__BYTE_ORDER
== __LITTLE_ENDIAN
) {
101 fprintf(fp
, "%016"PRIx64
, val
.u256
[3]);
102 fprintf(fp
, "%016"PRIx64
, val
.u256
[2]);
103 fprintf(fp
, "%016"PRIx64
, val
.u256
[1]);
104 fprintf(fp
, "%016"PRIx64
, val
.u256
[0]);
106 fprintf(fp
, "%016"PRIx64
, val
.u256
[0]);
107 fprintf(fp
, "%016"PRIx64
, val
.u256
[1]);
108 fprintf(fp
, "%016"PRIx64
, val
.u256
[2]);
109 fprintf(fp
, "%016"PRIx64
, val
.u256
[3]);
119 print_opnd(FILE *fp
, const opnd_t
*opnd
)
121 fprintf(fp
, "vbits = ");
122 print_vbits(fp
, opnd
->vbits
);
123 /* The value itself might be partially or fully undefined, so take a
124 copy, paint the copy as defined, and print the copied value. This is
125 so as to avoid error messages from Memcheck, which are correct, but
127 volatile value_t value_copy
= opnd
->value
;
128 VALGRIND_MAKE_MEM_DEFINED(&value_copy
, sizeof(value_copy
));
129 fprintf(fp
, " value = ");
130 print_value(fp
, value_copy
, opnd
->vbits
.num_bits
);
135 is_floating_point_type(IRType type
)
153 is_floating_point_op_with_rounding_mode(IROp op
)
155 IRType t_dst
, t_arg1
, t_arg2
, t_arg3
, t_arg4
;
157 typeof_primop(op
, &t_dst
, &t_arg1
, &t_arg2
, &t_arg3
, &t_arg4
);
159 // A unary operator cannot have a rounding mode
160 if (t_arg2
== Ity_INVALID
) return 0;
162 if (is_floating_point_type(t_dst
) ||
163 is_floating_point_type(t_arg1
) ||
164 is_floating_point_type(t_arg2
) ||
165 is_floating_point_type(t_arg3
) ||
166 is_floating_point_type(t_arg4
)) {
167 // Rounding mode, if present, is the 1st operand
168 return t_arg1
== Ity_I32
;
174 /* Return the number of operands for which input values can
175 be freely chosen. For floating point ops, the rounding mode
176 is not counted here, as it is restricted. */
178 get_num_operands(IROp op
)
180 IRType unused
, t1
, t2
, t3
, t4
;
182 typeof_primop(op
, &unused
, &t1
, &t2
, &t3
, &t4
);
184 int num_operands
= 4;
185 if (t4
== Ity_INVALID
) num_operands
= 3;
186 if (t3
== Ity_INVALID
) num_operands
= 2;
187 if (t2
== Ity_INVALID
) num_operands
= 1;
189 if (is_floating_point_op_with_rounding_mode(op
))
197 sizeof_irtype(IRType ty
)
199 return sizeofIRType(ty
);
204 typeof_primop(IROp op
, IRType
*t_dst
, IRType
*t_arg1
, IRType
*t_arg2
,
205 IRType
*t_arg3
, IRType
*t_arg4
)
207 return typeOfPrimop(op
, t_dst
, t_arg1
, t_arg2
, t_arg3
, t_arg4
);