aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.apple/align-test-4.c
blob: f07a10911f9d97843cb5044001c23a0e299000b6 (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
218
219
220
221
222
223
224
/* APPLE LOCAL file Macintosh alignment */

/* { dg-do run } */
/* { dg-options "-Wno-long-long" } */

/*
 * GCC alignment test for bit-fields.
 * This came up initially as an alignment problem in the kernel.
 * Fred Forsman
 * Apple Computer, Inc.
 * (C) 2000-2002.
 * Last modified 2002-2-18.
 */
 
 /* Check whether we are testing GCC 3 or later.  */
#ifdef __GNUC__
#if __GNUC__ >= 3
#define GCC3 1
#else
#define GCC3 0
#endif
#endif

#include <stdio.h>
#include <stddef.h>
#include <string.h>

/*
 * The following defined determines whether we should compare against
 * the values produced by GCC 2.95 or against the values I expect given
 * my understanding of the alignment rules.
 */
#define USE_GCC2_VALUES 1

#define NAME2(name,mode) mode##_##name
#define NAME(name,mode) NAME2(name,mode)

#define STR(s) #s
#define SIZEOF3(name) STR(sizeof(name)), sizeof(name)
#define SIZEOF2(name,mode) SIZEOF3(mode##_##name)
#define SIZEOF(name,mode) SIZEOF2(name,mode)

#define OFFSETOF3(name,field) STR(offsetof(name,field)), offsetof(name,field)
#define OFFSETOF2(name,mode,field) OFFSETOF3(mode##_##name,field)
#define OFFSETOF(name,mode,field) OFFSETOF2(name,mode,field)

typedef unsigned char UINT8;
typedef unsigned long UINT32;

static int bad_option = 0;
static int flag_verbose = 0;
static int nbr_failures = 0;

/* === mac68k alignment problem in kernel === */

typedef unsigned PEF_UBits32, ByteCount;
typedef short SInt16;

/* === power === */
#pragma options align=power
#define MODE power
#include "align-test-4.h"

/* === mac68k === */
#pragma options align=mac68k
#undef MODE
#define MODE mac68k
#include "align-test-4.h"

/* === pack(2) === */
#pragma pack(2)
#undef MODE
#define MODE pack2
#include "align-test-4.h"


static void check(char * rec_name, int actual, int expected, char * comment)
{
    if (flag_verbose || (actual != expected)) {
        printf("%-30s = %2d (%2d) ", rec_name, actual, expected);
        if (actual != expected) {
            printf("*** FAIL");
            nbr_failures++;
        } else
            printf("    PASS");
        printf(": %s\n", comment);
    }
}

static void check_option(char *option)
{
    if (*option == '-') {
        if (strcmp(option, "-v") == 0)
            flag_verbose = 1;
        else {
            fprintf(stderr, "*** unrecognized option '%s'.\n", option);
            bad_option = 1;
        }
    } else {
        fprintf(stderr, "*** unrecognized option '%s'.\n", option);
        bad_option = 1;
    }
}

int main(int argc, char *argv[])
{
    int i;
    
    for (i = 1; i < argc; i++)
        check_option(argv[i]);
    
    if (bad_option)
        return 1;

#undef MODE
#define MODE power

#if USE_GCC2_VALUES
    check(SIZEOF(LoaderExport, MODE), 12, "kernel struct");
    check(OFFSETOF(LoaderExport, MODE, offset), 4, "offset of offset");
    check(OFFSETOF(LoaderExport, MODE, sectionNumber), 8, "offset of sectionNumber");
    check(SIZEOF(S1, MODE), 8, "bitfields & char");
    check(SIZEOF(S2, MODE), 8, "int & char");
    check(SIZEOF(S3, MODE), 12, "char, bitfields(32), char");
    check(OFFSETOF(S3, MODE, f3), 8, "offset of 2nd char");
    check(SIZEOF(S4, MODE), 8, "char, bitfields(32), char");
    check(OFFSETOF(S4, MODE, f3), 7, "offset of 2nd char");
    check(SIZEOF(S5, MODE), 4, "char, bitfields(16), char");
    check(OFFSETOF(S5, MODE, f3), 3, "offset of 2nd char");
    check(SIZEOF(S6, MODE), 4, "char, bitfields(8), char");
    check(OFFSETOF(S6, MODE, f3), 2, "offset of 2nd char");
#else
    check(SIZEOF(LoaderExport, MODE), 12, "kernel struct");
    check(OFFSETOF(LoaderExport, MODE, offset), 4, "offset of offset");
    check(OFFSETOF(LoaderExport, MODE, sectionNumber), 8, "offset of sectionNumber");
    check(SIZEOF(S1, MODE), 8, "bitfields & char");
    check(SIZEOF(S2, MODE), 8, "int & char");
    check(SIZEOF(S3, MODE), 12, "char, bitfields(32), char");
    check(OFFSETOF(S3, MODE, f3), 8, "offset of 2nd char");
    check(SIZEOF(S4, MODE), 12, "char, bitfields(32), char");
    check(OFFSETOF(S4, MODE, f3), 8, "offset of 2nd char");
    check(SIZEOF(S5, MODE), 12, "char, bitfields(16), char");
    check(OFFSETOF(S5, MODE, f3), 8, "offset of 2nd char");
    check(SIZEOF(S6, MODE), 12, "char, bitfields(8), char");
    check(OFFSETOF(S6, MODE, f3), 8, "offset of 2nd char");
#endif

#undef MODE
#define MODE mac68k

#if USE_GCC2_VALUES
    check(SIZEOF(LoaderExport, MODE), 10, "kernel struct");
    check(OFFSETOF(LoaderExport, MODE, offset), 4, "offset of offset");
    check(OFFSETOF(LoaderExport, MODE, sectionNumber), 8, "offset of sectionNumber");
#if 1
    // GCC 2 is wrong on the following.
    check(SIZEOF(S1, MODE), 6, "bitfields & char");
#else
    check(SIZEOF(S1, MODE), 8, "bitfields & char");
#endif
    check(SIZEOF(S2, MODE), 6, "int & char");
    check(SIZEOF(S3, MODE), 6, "char, bitfields(32), char");
    check(OFFSETOF(S3, MODE, f3), 5, "offset of 2nd char");
    check(SIZEOF(S4, MODE), 6, "char, bitfields(32), char");
    check(OFFSETOF(S4, MODE, f3), 5, "offset of 2nd char");
    check(SIZEOF(S5, MODE), 4, "char, bitfields(16), char");
    check(OFFSETOF(S5, MODE, f3), 3, "offset of 2nd char");
    check(SIZEOF(S6, MODE), 4, "char, bitfields(8), char");
    check(OFFSETOF(S6, MODE, f3), 2, "offset of 2nd char");
#else
    check(SIZEOF(LoaderExport, MODE), 10, "kernel struct");
    check(OFFSETOF(LoaderExport, MODE, offset), 4, "offset of offset");
    check(OFFSETOF(LoaderExport, MODE, sectionNumber), 8, "offset of sectionNumber");
    check(SIZEOF(S1, MODE), 6, "bitfields & char");
    check(SIZEOF(S2, MODE), 6, "int & char");
    check(SIZEOF(S3, MODE), 8, "char, bitfields(32), char");
    check(OFFSETOF(S3, MODE, f3), 6, "offset of 2nd char");
    check(SIZEOF(S4, MODE), 8, "char, bitfields(32), char");
    check(OFFSETOF(S4, MODE, f3), 6, "offset of 2nd char");
    check(SIZEOF(S5, MODE), 6, "char, bitfields(16), char");
    check(OFFSETOF(S5, MODE, f3), 4, "offset of 2nd char");
    check(SIZEOF(S6, MODE), 4, "char, bitfields(8), char");
    check(OFFSETOF(S6, MODE, f3), 2, "offset of 2nd char");
#endif

#undef MODE
#define MODE pack2

#if USE_GCC2_VALUES
    check(SIZEOF(LoaderExport, MODE), 10, "kernel struct");
    check(OFFSETOF(LoaderExport, MODE, offset), 4, "offset of offset");
    check(OFFSETOF(LoaderExport, MODE, sectionNumber), 8, "offset of sectionNumber");
    /* GCC2 used to have this as '8', but it should really be 6.  */
    check(SIZEOF(S1, MODE), 6, "bitfields & char");
    check(SIZEOF(S2, MODE), 6, "int & char");
    check(SIZEOF(S3, MODE), 6, "char, bitfields(32), char");
    check(OFFSETOF(S3, MODE, f3), 5, "offset of 2nd char");
    check(SIZEOF(S4, MODE), 6, "char, bitfields(32), char");
    check(OFFSETOF(S4, MODE, f3), 5, "offset of 2nd char");
    check(SIZEOF(S5, MODE), 4, "char, bitfields(16), char");
    check(OFFSETOF(S5, MODE, f3), 3, "offset of 2nd char");
    check(SIZEOF(S6, MODE), 4, "char, bitfields(8), char");
    check(OFFSETOF(S6, MODE, f3), 2, "offset of 2nd char");
#else
    check(SIZEOF(LoaderExport, MODE), 10, "kernel struct");
    check(OFFSETOF(LoaderExport, MODE, offset), 4, "offset of offset");
    check(OFFSETOF(LoaderExport, MODE, sectionNumber), 8, "offset of sectionNumber");
    check(SIZEOF(S1, MODE), 6, "bitfields & char");
    check(SIZEOF(S2, MODE), 6, "int & char");
    check(SIZEOF(S3, MODE), 8, "char, bitfields(32), char");
    check(OFFSETOF(S3, MODE, f3), 6, "offset of 2nd char");
    check(SIZEOF(S4, MODE), 8, "char, bitfields(32), char");
    check(OFFSETOF(S4, MODE, f3), 6, "offset of 2nd char");
    check(SIZEOF(S5, MODE), 6, "char, bitfields(16), char");
    check(OFFSETOF(S5, MODE, f3), 4, "offset of 2nd char");
    check(SIZEOF(S6, MODE), 4, "char, bitfields(8), char");
    check(OFFSETOF(S6, MODE, f3), 2, "offset of 2nd char");
#endif

    if (nbr_failures > 0)
    	return 1;
    else
    	return 0;
}