Commit | Line | Data |
---|---|---|
3609e0fe DB |
1 | /* CGEN generic opcode support. |
2 | ||
3 | Copyright 2002, 2005 | |
4 | Free Software Foundation, Inc. | |
5 | ||
6 | This file is part of the GNU Binutils and GDB, the GNU debugger. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License along | |
19 | with this program; if not, write to the Free Software Foundation, Inc., | |
20 | 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ | |
21 | ||
22 | /* Functions for manipulating CGEN_BITSET. */ | |
23 | ||
24 | #include "libiberty.h" | |
25 | #include "opcode/cgen-bitset.h" | |
26 | #include <string.h> | |
27 | ||
28 | /* Create a bit mask. */ | |
29 | CGEN_BITSET * | |
30 | cgen_bitset_create (unsigned bit_count) | |
31 | { | |
32 | CGEN_BITSET * mask = xmalloc (sizeof (* mask)); | |
33 | cgen_bitset_init (mask, bit_count); | |
34 | return mask; | |
35 | } | |
36 | ||
37 | /* Initialize an existing bit mask. */ | |
38 | ||
39 | void | |
40 | cgen_bitset_init (CGEN_BITSET * mask, unsigned bit_count) | |
41 | { | |
42 | if (! mask) | |
43 | return; | |
44 | mask->length = (bit_count / 8) + 1; | |
45 | mask->bits = xmalloc (mask->length); | |
46 | cgen_bitset_clear (mask); | |
47 | } | |
48 | ||
49 | /* Clear the bits of a bit mask. */ | |
50 | ||
51 | void | |
52 | cgen_bitset_clear (CGEN_BITSET * mask) | |
53 | { | |
54 | unsigned i; | |
55 | ||
56 | if (! mask) | |
57 | return; | |
58 | ||
59 | for (i = 0; i < mask->length; ++i) | |
60 | mask->bits[i] = 0; | |
61 | } | |
62 | ||
63 | /* Add a bit to a bit mask. */ | |
64 | ||
65 | void | |
66 | cgen_bitset_add (CGEN_BITSET * mask, unsigned bit_num) | |
67 | { | |
68 | int byte_ix, bit_ix; | |
69 | int bit_mask; | |
70 | ||
71 | if (! mask) | |
72 | return; | |
73 | byte_ix = bit_num / 8; | |
74 | bit_ix = bit_num % 8; | |
75 | bit_mask = 1 << (7 - bit_ix); | |
76 | mask->bits[byte_ix] |= bit_mask; | |
77 | } | |
78 | ||
79 | /* Set a bit mask. */ | |
80 | ||
81 | void | |
82 | cgen_bitset_set (CGEN_BITSET * mask, unsigned bit_num) | |
83 | { | |
84 | if (! mask) | |
85 | return; | |
86 | cgen_bitset_clear (mask); | |
87 | cgen_bitset_add (mask, bit_num); | |
88 | } | |
89 | ||
90 | /* Test for a bit in a bit mask. | |
91 | Returns 1 if the bit is found */ | |
92 | ||
93 | int | |
94 | cgen_bitset_contains (CGEN_BITSET * mask, unsigned bit_num) | |
95 | { | |
96 | int byte_ix, bit_ix; | |
97 | int bit_mask; | |
98 | ||
99 | if (! mask) | |
100 | return 1; /* No bit restrictions. */ | |
101 | ||
102 | byte_ix = bit_num / 8; | |
103 | bit_ix = 7 - (bit_num % 8); | |
104 | bit_mask = 1 << bit_ix; | |
105 | return (mask->bits[byte_ix] & bit_mask) >> bit_ix; | |
106 | } | |
107 | ||
108 | /* Compare two bit masks for equality. | |
109 | Returns 0 if they are equal. */ | |
110 | ||
111 | int | |
112 | cgen_bitset_compare (CGEN_BITSET * mask1, CGEN_BITSET * mask2) | |
113 | { | |
114 | if (mask1 == mask2) | |
115 | return 0; | |
116 | if (! mask1 || ! mask2) | |
117 | return 1; | |
118 | if (mask1->length != mask2->length) | |
119 | return 1; | |
120 | return memcmp (mask1->bits, mask2->bits, mask1->length); | |
121 | } | |
122 | ||
123 | /* Test two bit masks for common bits. | |
124 | Returns 1 if a common bit is found. */ | |
125 | ||
126 | int | |
127 | cgen_bitset_intersect_p (CGEN_BITSET * mask1, CGEN_BITSET * mask2) | |
128 | { | |
129 | unsigned i, limit; | |
130 | ||
131 | if (mask1 == mask2) | |
132 | return 1; | |
133 | if (! mask1 || ! mask2) | |
134 | return 0; | |
135 | limit = mask1->length < mask2->length ? mask1->length : mask2->length; | |
136 | ||
137 | for (i = 0; i < limit; ++i) | |
138 | if ((mask1->bits[i] & mask2->bits[i])) | |
139 | return 1; | |
140 | ||
141 | return 0; | |
142 | } | |
143 | ||
144 | /* Make a copy of a bit mask. */ | |
145 | ||
146 | CGEN_BITSET * | |
147 | cgen_bitset_copy (CGEN_BITSET * mask) | |
148 | { | |
149 | CGEN_BITSET* newmask; | |
150 | ||
151 | if (! mask) | |
152 | return NULL; | |
153 | newmask = cgen_bitset_create ((mask->length * 8) - 1); | |
154 | memcpy (newmask->bits, mask->bits, mask->length); | |
155 | return newmask; | |
156 | } | |
157 | ||
158 | /* Combine two bit masks. */ | |
159 | ||
160 | void | |
161 | cgen_bitset_union (CGEN_BITSET * mask1, CGEN_BITSET * mask2, | |
162 | CGEN_BITSET * result) | |
163 | { | |
164 | unsigned i; | |
165 | ||
166 | if (! mask1 || ! mask2 || ! result | |
167 | || mask1->length != mask2->length | |
168 | || mask1->length != result->length) | |
169 | return; | |
170 | ||
171 | for (i = 0; i < result->length; ++i) | |
172 | result->bits[i] = mask1->bits[i] | mask2->bits[i]; | |
173 | } |