1 /* -*- mode: C; c-basic-offset: 3; -*- */
2 
3 /*
4    This file is part of MemCheck, a heavyweight Valgrind tool for
5    detecting memory errors.
6 
7    Copyright (C) 2012-2015  Florian Krohm
8 
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.
13 
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.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22    02111-1307, USA.
23 
24    The GNU General Public License is contained in the file COPYING.
25 */
26 
27 #include <assert.h>
28 #include "vtest.h"
29 
30 
31 /* Check the result of a unary operation. */
32 static void
check_result_for_unary(const irop_t * op,const test_data_t * data)33 check_result_for_unary(const irop_t *op, const test_data_t *data)
34 {
35    const opnd_t *result = &data->result;
36    const opnd_t *opnd   = &data->opnds[0];
37    unsigned num_bits = result->vbits.num_bits;
38    vbits_t expected_vbits;
39 
40    /* Only handle those undef-kinds that actually occur. */
41    switch (op->undef_kind) {
42    case UNDEF_ALL:
43       expected_vbits = undefined_vbits(num_bits);
44       break;
45 
46    case UNDEF_SAME:
47       expected_vbits = opnd->vbits;
48       break;
49 
50    case UNDEF_TRUNC:
51       expected_vbits = truncate_vbits(opnd->vbits, num_bits);
52       break;
53 
54    case UNDEF_LEFT:
55       expected_vbits = left_vbits(opnd->vbits, num_bits);
56       break;
57 
58    case UNDEF_UPPER:
59       assert(num_bits * 2 == opnd->vbits.num_bits);
60       expected_vbits = upper_vbits(opnd->vbits);
61       break;
62 
63    case UNDEF_SEXT:
64       expected_vbits = sextend_vbits(opnd->vbits, num_bits);
65       break;
66 
67    case UNDEF_ZEXT:
68       expected_vbits = zextend_vbits(opnd->vbits, num_bits);
69       break;
70 
71    default:
72       panic(__func__);
73    }
74 
75    if (! equal_vbits(result->vbits, expected_vbits))
76       complain(op, data, expected_vbits);
77 }
78 
79 
80 int
test_unary_op(const irop_t * op,test_data_t * data)81 test_unary_op(const irop_t *op, test_data_t *data)
82 {
83    unsigned num_input_bits, bitpos;
84    int tests_done = 0;
85 
86    num_input_bits = bitsof_irtype(data->opnds[0].type);
87 
88    for (bitpos = 0; bitpos < num_input_bits; ++bitpos) {
89       data->opnds[0].vbits = onehot_vbits(bitpos, num_input_bits);
90 
91       valgrind_execute_test(op, data);
92 
93       check_result_for_unary(op, data);
94       tests_done++;
95    }
96    return tests_done;
97 }
98