forked from rui314/8cc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct.c
341 lines (303 loc) · 6.56 KB
/
struct.c
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright 2012 Rui Ueyama. Released under the MIT license.
#include <stddef.h>
#include "test.h"
static void t1() {
struct { int a; } x;
x.a = 61;
expect(61, x.a);
}
static void t2() {
struct { int a; int b; } x;
x.a = 61;
x.b = 2;
expect(63, x.a + x.b);
}
static void t3() {
struct { int a; struct { char b; int c; } y; } x;
x.a = 61;
x.y.b = 3;
x.y.c = 3;
expect(67, x.a + x.y.b + x.y.c);
}
static void t4() {
struct tag4 { int a; struct { char b; int c; } y; } x;
struct tag4 s;
s.a = 61;
s.y.b = 3;
s.y.c = 3;
expect(67, s.a + s.y.b + s.y.c);
}
static void t5() {
struct tag5 { int a; } x;
struct tag5 *p = &x;
x.a = 68;
expect(68, (*p).a);
}
static void t6() {
struct tag6 { int a; } x;
struct tag6 *p = &x;
(*p).a = 69;
expect(69, x.a);
}
static void t7() {
struct tag7 { int a; int b; } x;
struct tag7 *p = &x;
x.b = 71;
expect(71, (*p).b);
}
static void t8() {
struct tag8 { int a; int b; } x;
struct tag8 *p = &x;
(*p).b = 72;
expect(72, x.b);
}
static void t9() {
struct tag9 { int a[3]; int b[3]; } x;
x.a[0] = 73;
expect(73, x.a[0]);
x.b[1] = 74;
expect(74, x.b[1]);
expect(74, x.a[4]);
}
struct tag10 {
int a;
struct tag10a {
char b;
int c;
} y;
} v10;
static void t10() {
v10.a = 71;
v10.y.b = 3;
v10.y.c = 3;
expect(77, v10.a + v10.y.b + v10.y.c);
}
struct tag11 { int a; } v11;
static void t11() {
struct tag11 *p = &v11;
v11.a = 78;
expect(78, (*p).a);
expect(78, v11.a);
expect(78, p->a);
p->a = 79;
expect(79, (*p).a);
expect(79, v11.a);
expect(79, p->a);
}
struct tag12 {
int a;
int b;
} x;
static void t12() {
struct tag12 a[3];
a[0].a = 83;
expect(83, a[0].a);
a[0].b = 84;
expect(84, a[0].b);
a[1].b = 85;
expect(85, a[1].b);
int *p = (int *)a;
expect(85, p[3]);
}
static void t13() {
struct { char c; } v = { 'a' };
expect('a', v.c);
}
static void t14() {
struct { int a[3]; } v = { { 1, 2, 3 } };
expect(2, v.a[1]);
}
static void unnamed() {
struct {
union {
struct { int x; int y; };
struct { char c[8]; };
};
} v;
v.x = 1;
v.y = 7;
expect(1, v.c[0]);
expect(7, v.c[4]);
}
static void assign() {
struct { int a, b, c; short d; char f; } v1, v2;
v1.a = 3;
v1.b = 5;
v1.c = 7;
v1.d = 9;
v1.f = 11;
v2 = v1;
expect(3, v2.a);
expect(5, v2.b);
expect(7, v2.c);
expect(9, v2.d);
expect(11, v2.f);
}
static void arrow() {
struct cell { int val; struct cell *next; };
struct cell v1 = { 5, NULL };
struct cell v2 = { 6, &v1 };
struct cell v3 = { 7, &v2 };
struct cell *p = &v3;
expect(7, v3.val);
expect(7, p->val);
expect(6, p->next->val);
expect(5, p->next->next->val);
p->val = 10;
p->next->val = 11;
p->next->next->val = 12;
expect(10, p->val);
expect(11, p->next->val);
expect(12, p->next->next->val);
}
static void address() {
struct tag { int a; struct { int b; } y; } x = { 6, 7 };
int *p1 = &x.a;
int *p2 = &x.y.b;
expect(6, *p1);
expect(7, *p2);
expect(6, *&x.a);
expect(7, *&x.y.b);
struct tag *xp = &x;
int *p3 = &xp->a;
int *p4 = &xp->y.b;
expect(6, *p3);
expect(7, *p4);
expect(6, *&xp->a);
expect(7, *&xp->y.b);
}
static void incomplete() {
struct tag1;
struct tag2 { struct tag1 *p; };
struct tag1 { int x; };
struct tag1 v1 = { 3 };
struct tag2 v2 = { &v1 };
expect(3, v2.p->x);
}
static void bitfield_basic() {
union {
int i;
struct { int a:5; int b:5; };
} x;
x.i = 0;
x.a = 10;
x.b = 11;
expect(10, x.a);
expect(11, x.b);
expect(362, x.i); // 11 << 5 + 10 == 362
}
static void bitfield_mix() {
union {
int i;
struct { char a:5; int b:5; };
} x;
x.a = 10;
x.b = 11;
expect(10, x.a);
expect(11, x.b);
expect(362, x.i);
}
static void bitfield_union() {
union { int a : 10; char b: 5; char c: 5; } x;
x.a = 2;
expect(2, x.a);
expect(2, x.b);
expect(2, x.c);
}
static void bitfield_unnamed() {
union {
int i;
struct { char a:4; char b:4; char : 8; };
} x = { 0 };
x.i = 0;
x.a = 2;
x.b = 4;
expect(2, x.a);
expect(4, x.b);
expect(66, x.i);
union {
int i;
struct { char a:4; char :0; char b:4; };
} y = { 0 };
y.a = 2;
y.b = 4;
expect(2, y.a);
expect(4, y.b);
expect(1026, y.i);
}
struct { char a:4; char b:4; } inittest = { 2, 4 };
static void bitfield_initializer() {
expect(2, inittest.a);
expect(4, inittest.b);
struct { char a:4; char b:4; } x = { 2, 4 };
expect(2, x.a);
expect(4, x.b);
}
static void test_offsetof() {
struct tag10 { int a, b; };
expect(0, offsetof(struct tag10, a));
expect(4, offsetof(struct tag10, b));
int x[offsetof(struct tag10, b)];
expect(4, sizeof(x) / sizeof(x[0]));
expect(4, offsetof(struct { char a; struct { int b; }; }, b));
expect(6, offsetof(struct { char a[3]; int : 10; char c; }, c));
expect(6, offsetof(struct { char a[3]; int : 16; char c; }, c));
expect(7, offsetof(struct { char a[3]; int : 17; char c; }, c));
expect(2, offsetof(struct { char : 7; int : 7; char a; }, a));
expect(0, offsetof(struct { char : 0; char a; }, a));
expect(1, _Alignof(struct { int : 32; }));
expect(2, _Alignof(struct { int : 32; short x; }));
expect(4, _Alignof(struct { int x; int : 32; }));
}
static void flexible_member() {
struct { int a, b[]; } x;
expect(4, sizeof(x));
struct { int a, b[0]; } y;
expect(4, sizeof(y));
struct { int a[0]; } z;
expect(0, sizeof(z));
#ifdef __8cc__ // BUG
struct t { int a, b[]; };
struct t x2 = { 1, 2, 3 };
struct t x3 = { 1, 2, 3, 4, 5 };
expect(2, x3.b[0]);
expect(3, x3.b[1]);
expect(4, x3.b[2]);
expect(5, x3.b[3]);
#endif
}
static void empty_struct() {
struct tag15 {};
expect(0, sizeof(struct tag15));
union tag16 {};
expect(0, sizeof(union tag16));
}
void testmain() {
print("struct");
t1();
t2();
t3();
t4();
t5();
t6();
t7();
t8();
t9();
t10();
t11();
t12();
t13();
t14();
unnamed();
assign();
arrow();
incomplete();
bitfield_basic();
bitfield_mix();
bitfield_union();
bitfield_unnamed();
bitfield_initializer();
test_offsetof();
flexible_member();
empty_struct();
}