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