1# Copyright 1992, 1994, 1995, 1997, 2004, 2007, 2008, 2009, 2010, 2011
2# Free Software Foundation, Inc.
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17# This file was adapted from bitfields.exp by Paul Hilfinger
18# (Hilfinger@gnat.com)
19
20#
21# Tests for bit-fields that do not fit in type (unsigned) int, but do fit
22# in type (unsigned) long long.  We perform essentially the same tests as
23# in bitfields.c, which considers only bit-fields that are <= 9 bits long.
24#
25
26if $tracelevel then {
27	strace $tracelevel
28}
29
30
31set testfile "bitfields2"
32set srcfile ${testfile}.c
33set binfile ${objdir}/${subdir}/${testfile}
34if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
35    untested bitfields2.exp
36    return -1
37}
38
39set has_signed_bitfields 1
40
41#
42# Continue to expected breakpoint at FUNCTION.  Append TAG to make pass/fail
43# messages (to make them unique).  Suppress tests on failure.
44#
45proc continue_test { function tag } {
46    global decimal
47    global srcfile
48
49    if [gdb_test "cont" "Break.*$function \\(\\) at .*$srcfile:$decimal.*" "continuing to $function $tag"] {
50	gdb_suppress_tests
51    }
52}
53
54#
55# Start next test by running to tester and then to FUNCTION.   Suppresses
56# tests on failure.
57#
58proc start_test { function } {
59    delete_breakpoints
60    if [gdb_test "break tester" ".*" "break tester prior to $function"] {
61	gdb_suppress_tests
62    }
63    continue_test "tester" "prior to $function"
64    if ![gdb_breakpoint $function] {
65	gdb_suppress_tests
66    }
67    continue_test $function "#0"
68}
69
70
71#
72# Test bitfield locating and uniqueness.
73# For each member, set that member to 1 and verify that the member (and only
74# that member) is 1, then reset it back to 0.
75#
76
77proc bitfield_uniqueness {} {
78    global decimal
79    global hex
80    global gdb_prompt
81    global srcfile
82
83    start_test break1
84
85    if [gdb_test "print flags" ".*u1 = 0, u2 = 0, u3 = 0, s1 = 1, s2 = 0, s3 = 0.*" "bitfield uniqueness; flags.s1 = 1"] {
86	gdb_suppress_tests;
87    }
88    continue_test break1 "#1"
89    if [gdb_test "print flags" ".*u1 = 1, u2 = 0, u3 = 0, s1 = 0, s2 = 0, s3 = 0.*" "bitfield uniqueness; flags.u1 = 1"] {
90	gdb_suppress_tests;
91    }
92    continue_test break1 "#2"
93    if [gdb_test "print flags" ".*u1 = 0, u2 = 0, u3 = 0, s1 = 0, s2 = 1, s3 = 0.*" "bitfield uniqueness; flags.s2 = 1"] {
94	gdb_suppress_tests;
95    }
96    continue_test break1 "#3"
97    if [gdb_test "print flags" ".*u1 = 0, u2 = 1, u3 = 0, s1 = 0, s2 = 0, s3 = 0.*" "bitfield uniqueness; flags.u2 = 1"] {
98	gdb_suppress_tests;
99    }
100    continue_test break1 "#4"
101    if [gdb_test "print flags" ".*u1 = 0, u2 = 0, u3 = 0, s1 = 0, s2 = 0, s3 = 1.*" "bitfield uniqueness; flags.s3 = 1"] {
102	gdb_suppress_tests;
103    }
104    continue_test break1 "#5"
105    if [gdb_test "print flags" ".*u1 = 0, u2 = 0, u3 = 1, s1 = 0, s2 = 0, s3 = 0.*" "bitfield uniqueness; flags.u3 = 1"] {
106	gdb_suppress_tests
107    }
108    gdb_stop_suppressing_tests;
109}
110
111
112#
113# Test bitfield containment.
114# Fill alternating fields with all 1's and verify that none of the bits
115# "bleed over" to the other fields.
116#
117
118proc bitfield_containment {} {
119    global decimal
120    global hex
121    global gdb_prompt
122    global srcfile
123
124    start_test break2
125
126    # If program is compiled with Sun CC, signed fields print out as their
127    # actual sizes; if compiled with gcc, they print out as 0xffffffff.
128    if [gdb_test "print/x flags" "= {u1 = 0x7fff, u2 = 0x0, u3 = 0xffff, s1 = 0x0, s2 = 0x(1ffffffff|f*), s3 = 0x0}" "bitfield containment; flags.u1, flags.u3, and flags.s3 to all 1s"] {
129	gdb_suppress_tests
130    }
131
132    continue_test break2 "#1"
133
134    if [gdb_test "print/x flags" "= {u1 = 0x0, u2 = 0x1ffffffff, u3 = 0x0, s1 = 0x(7fff|f*), s2 = 0x0, s3 = 0xf*}" "bitfield containment; flags.u2, flags.s1, flags.s2 to all 1s"] {
135	gdb_suppress_tests
136    }
137    gdb_stop_suppressing_tests;
138}
139
140# Test unsigned bitfields for unsignedness and range.
141# Fill the unsigned fields with the maximum positive value and verify that
142# the values are printed correctly.
143
144proc bitfield_unsignedness {} {
145    global decimal
146    global hex
147    global gdb_prompt
148    global srcfile
149
150    start_test break3
151
152    if [gdb_test "print flags" ".*u1 = 32767, u2 = 8589934591, u3 = 65535, s1 = 0, s2 = 0, s3 = 0.*" "maximum unsigned bitfield values"] {
153	gdb_suppress_tests
154    }
155    gdb_stop_suppressing_tests;
156}
157
158#
159# Test signed bitfields for signedness and range.
160# Fill the signed fields with the maximum positive value, then the maximally
161# negative value, then -1, and verify in each case that the values are
162# printed correctly.
163#
164
165proc bitfield_signedness {} {
166    global decimal
167    global hex
168    global gdb_prompt
169    global srcfile
170    global has_signed_bitfields
171
172    start_test break4
173
174    if [gdb_test "print flags" "= {.*u1 = 0, u2 = 0, u3 = 0, s1 = 16383, s2 = 4294967295, s3 = 32767.*}" "maximum signed bitfield values"] {
175	gdb_suppress_tests
176    }
177
178    continue_test break4 "#1"
179
180    # Determine if the target has signed bitfields so we can skip
181    # the signed bitfield tests if it doesn't.
182    set test "determining signed-ness of bitfields"
183    set has_signed_bitfields 0
184    gdb_test_multiple "print i" $test {
185	-re ".* = -32768.*$gdb_prompt $" {
186	    set has_signed_bitfields 1
187	    pass "determining signed-ness of bitfields"
188	}
189	-re ".* = 32768.*$gdb_prompt $" {
190	    pass "determining signed-ness of bitfields"
191	}
192	-re ".*$gdb_prompt $" {
193	    fail "determining signed-ness of bitfields"
194	    gdb_suppress_tests
195	}
196    }
197
198    set test "most negative signed bitfield values"
199    if $has_signed_bitfields then {
200        if [gdb_test "print flags" "u1 = 0, u2 = 0, u3 = 0, s1 = -16384, s2 = -4294967296, s3 = -32768.*" $test ] {
201            gdb_suppress_tests
202        }
203    } else {
204	unsupported $test
205    }
206
207    continue_test break4 "#2"
208
209    set test "signed bitfields containing -1"
210    if $has_signed_bitfields then {
211	if [gdb_test "print flags" "u1 = 0, u2 = 0, u3 = 0, s1 = -1, s2 = -1, s3 = -1.*" $test ] {
212	    gdb_suppress_tests
213	}
214    } else {
215	unsupported $test
216    }
217
218    gdb_stop_suppressing_tests;
219}
220
221
222# Test setting of long long bit fields from within GDB.
223
224proc bitfield_set {} {
225    global decimal
226    global hex
227    global gdb_prompt
228    global srcfile
229    global has_signed_bitfields
230
231    start_test break5
232
233    set big_set_failed 0
234    set test "set long long unsigned bitfield"
235    gdb_test_multiple "print flags.u2 = 0x100000000" $test {
236	-re "warning: Value does not fit.*$gdb_prompt $" {
237	    fail "$test"
238	    gdb_suppress_tests
239	}
240	-re "= 4294967296.*$gdb_prompt $" {
241	    pass "$test"
242	}
243    }
244
245    set test "set long long signed bitfield positive"
246    gdb_test_multiple "print flags.s2 = 0x80000000" $test {
247	-re "warning: Value does not fit.*$gdb_prompt $" {
248	    fail "$test"
249	    gdb_suppress_tests
250	}
251	-re "= 2147483648.*$gdb_prompt $" {
252	    pass "$test"
253	}
254    }
255
256    if [gdb_test "print flags" "u1 = 0, u2 = 4294967296, u3 = 0, s1 = 0, s2 = 2147483648, s3 = 0.*" "long long bitfield values after set"] {
257	gdb_suppress_tests
258    }
259
260    set test "set long long signed bitfield negative"
261    if $has_signed_bitfields then {
262	gdb_test_multiple "print flags.s2 = -1" $test {
263	    -re "warning: Value does not fit.*$gdb_prompt $" {
264		fail "$test"
265		gdb_suppress_tests
266	    }
267	    -re "= -1.*$gdb_prompt $" {
268		pass "$test"
269	    }
270	}
271    } else {
272	unsupported $test
273    }
274
275    set test  "long long bitfield values after set negative"
276    if $has_signed_bitfields then {
277	if [gdb_test "print flags" "u1 = 0, u2 = 4294967296, u3 = 0, s1 = 0, s2 = -1, s3 = 0.*" $test] {
278	    gdb_suppress_tests
279	}
280    } else {
281	unsupported $test
282    }
283
284    gdb_stop_suppressing_tests;
285}
286
287gdb_start
288gdb_reinitialize_dir $srcdir/$subdir
289gdb_load ${binfile}
290
291gdb_test_no_output "set print sevenbit-strings"
292runto_main
293
294bitfield_uniqueness
295bitfield_containment
296bitfield_unsignedness
297bitfield_signedness
298bitfield_set
299
300