Support for Toshiba MeP and for complex relocations.
[binutils.git] / opcodes / cgen-bitset.c
blob7a41662769977e942ce37e9b6d8d01212f8d1436
1 /* CGEN generic opcode support.
3 Copyright 2002, 2005
4 Free Software Foundation, Inc.
6 This file is part of the GNU Binutils and GDB, the GNU debugger.
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.
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.
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. */
22 /* Functions for manipulating CGEN_BITSET. */
24 #include "libiberty.h"
25 #include "opcode/cgen-bitset.h"
26 #include <string.h>
28 /* Create a bit mask. */
29 CGEN_BITSET *
30 cgen_bitset_create (unsigned bit_count)
32 CGEN_BITSET * mask = xmalloc (sizeof (* mask));
33 cgen_bitset_init (mask, bit_count);
34 return mask;
37 /* Initialize an existing bit mask. */
39 void
40 cgen_bitset_init (CGEN_BITSET * mask, unsigned bit_count)
42 if (! mask)
43 return;
44 mask->length = (bit_count / 8) + 1;
45 mask->bits = xmalloc (mask->length);
46 cgen_bitset_clear (mask);
49 /* Clear the bits of a bit mask. */
51 void
52 cgen_bitset_clear (CGEN_BITSET * mask)
54 unsigned i;
56 if (! mask)
57 return;
59 for (i = 0; i < mask->length; ++i)
60 mask->bits[i] = 0;
63 /* Add a bit to a bit mask. */
65 void
66 cgen_bitset_add (CGEN_BITSET * mask, unsigned bit_num)
68 int byte_ix, bit_ix;
69 int bit_mask;
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;
79 /* Set a bit mask. */
81 void
82 cgen_bitset_set (CGEN_BITSET * mask, unsigned bit_num)
84 if (! mask)
85 return;
86 cgen_bitset_clear (mask);
87 cgen_bitset_add (mask, bit_num);
90 /* Test for a bit in a bit mask.
91 Returns 1 if the bit is found */
93 int
94 cgen_bitset_contains (CGEN_BITSET * mask, unsigned bit_num)
96 int byte_ix, bit_ix;
97 int bit_mask;
99 if (! mask)
100 return 1; /* No bit restrictions. */
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;
108 /* Compare two bit masks for equality.
109 Returns 0 if they are equal. */
112 cgen_bitset_compare (CGEN_BITSET * mask1, CGEN_BITSET * mask2)
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);
123 /* Test two bit masks for common bits.
124 Returns 1 if a common bit is found. */
127 cgen_bitset_intersect_p (CGEN_BITSET * mask1, CGEN_BITSET * mask2)
129 unsigned i, limit;
131 if (mask1 == mask2)
132 return 1;
133 if (! mask1 || ! mask2)
134 return 0;
135 limit = mask1->length < mask2->length ? mask1->length : mask2->length;
137 for (i = 0; i < limit; ++i)
138 if ((mask1->bits[i] & mask2->bits[i]))
139 return 1;
141 return 0;
144 /* Make a copy of a bit mask. */
146 CGEN_BITSET *
147 cgen_bitset_copy (CGEN_BITSET * mask)
149 CGEN_BITSET* newmask;
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;
158 /* Combine two bit masks. */
160 void
161 cgen_bitset_union (CGEN_BITSET * mask1, CGEN_BITSET * mask2,
162 CGEN_BITSET * result)
164 unsigned i;
166 if (! mask1 || ! mask2 || ! result
167 || mask1->length != mask2->length
168 || mask1->length != result->length)
169 return;
171 for (i = 0; i < result->length; ++i)
172 result->bits[i] = mask1->bits[i] | mask2->bits[i];