aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/bitfields.c
blob: 3a6b76fb99be7786de2216f55adf0927eb947e67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* Test program to test bit field operations */

/* For non-ANSI compilers, use plain ints for the signed bit fields.  However,
   whether they actually end up signed or not is implementation defined, so
   this may cause some tests to fail.  But at least we can still compile
   the test program and run the tests... */

#if !defined(__STDC__) && !defined(__cplusplus)
#define signed  /**/
#endif

struct fields
{
  unsigned char	uc    ;
  signed int	s1 : 1;
  unsigned int	u1 : 1;
  signed int	s2 : 2;
  unsigned int	u2 : 2;
  signed int	s3 : 3;
  unsigned int	u3 : 3;
  signed int	s9 : 9;
  unsigned int  u9 : 9;
  signed char	sc    ;
} flags;

struct internalvartest
{
  unsigned int a : 1;
  struct
    {
      unsigned int b : 1;
      struct
	{
	  unsigned int c : 1;
	  signed int   d : 1;
	} deep;
      signed int   e : 1;
    } inner;
  signed int   f : 1;
} dummy_internalvartest;

void break1 ()
{
}

void break2 ()
{
}

void break3 ()
{
}

void break4 ()
{
}

void break5 ()
{
}

void break6 ()
{
}

void break7 ()
{
}

void break8 ()
{
}

void break9 ()
{
}

void break10 ()
{
}

struct container
{
  struct fields one;
  struct fields two;
} container;

/* This is used by bitfields.exp to determine if the target understands
   signed bitfields.  */
int i;

int main ()
{
  /* For each member, set that member to 1, allow gdb to verify that the
     member (and only that member) is 1, and then reset it back to 0. */

  flags.uc = 1;
  break1 ();
  flags.uc = 0;

  flags.s1 = -1;
  break1 ();
  flags.s1 = 0;

  flags.u1 = 1;
  break1 ();
  flags.u1 = 0;

  flags.s2  = 1;
  break1 ();
  flags.s2 = 0;

  flags.u2 = 1;
  break1 ();
  flags.u2 = 0;

  flags.s3  = 1;
  break1 ();
  flags.s3 = 0;

  flags.u3 = 1;
  break1 ();
  flags.u3 = 0;

  flags.s9 = 1;
  break1 ();
  flags.s9 = 0;

  flags.u9 = 1;
  break1 ();
  flags.u9 = 0;

  flags.sc = 1;
  break1 ();
  flags.sc = 0;

  /* Fill alternating fields with all 1's and verify that none of the bits
     "bleed over" to the other fields. */

  flags.uc = 0xFF;
  flags.u1 = 0x1;
  flags.u2 = 0x3;
  flags.u3 = 0x7;
  flags.u9 = 0x1FF;
  break2 ();
  flags.uc = 0;
  flags.u1 = 0;
  flags.u2 = 0;
  flags.u3 = 0;
  flags.u9 = 0;

  flags.s1 = -1;
  flags.s2 = -1;
  flags.s3 = -1;
  flags.s9 = -1;
  flags.sc = 0xFF;
  break2 ();
  flags.s1 = 0;
  flags.s2 = 0;
  flags.s3 = 0;
  flags.s9 = 0;
  flags.sc = 0;

  /* Fill the unsigned fields with the maximum positive value and verify
     that the values are printed correctly. */

  /* Maximum positive values */
  flags.u1 = 0x1;
  flags.u2 = 0x3;
  flags.u3 = 0x7;
  flags.u9 = 0x1FF;
  break3 ();
  flags.u1 = 0;
  flags.u2 = 0;
  flags.u3 = 0;
  flags.u9 = 0;

  /* Fill the signed fields with the maximum positive value, then the maximally
     negative value, then -1, and verify in each case that the values are
     printed correctly. */

  /* Maximum positive values */
  flags.s1 = 0x0;
  flags.s2 = 0x1;
  flags.s3 = 0x3;
  flags.s9 = 0xFF;
  break4 ();

  /* Maximally negative values */
  flags.s1 = -0x1;
  flags.s2 = -0x2;
  flags.s3 = -0x4;
  flags.s9 = -0x100;
  /* Extract bitfield value so that bitfield.exp can check if the target
     understands signed bitfields.  */
  i = flags.s9;
  break4 ();

  /* -1 */
  flags.s1 = -1;
  flags.s2 = -1;
  flags.s3 = -1;
  flags.s9 = -1;
  break4 ();

  flags.s1 = 0;
  flags.s2 = 0;
  flags.s3 = 0;
  flags.s9 = 0;

  /* Bitfields at a non-zero offset in a containing structure.  */
  container.one.u3 = 5;
  container.two.u3 = 3;
  break5 ();

  return 0;
}