|
|
@@ -1,64 +1,25 @@
|
|
1
|
1
|
#include <assert.h>
|
|
|
2
|
+#include <errno.h>
|
|
2
|
3
|
#include <stdio.h>
|
|
3
|
4
|
#include <stdlib.h>
|
|
4
|
5
|
#include <string.h>
|
|
5
|
6
|
|
|
6
|
7
|
#include "type.h"
|
|
7
|
8
|
|
|
8
|
|
-#define builtin_scalar(_t) { \
|
|
9
|
|
- .ttype = T_SCALAR, \
|
|
10
|
|
- .size = sizeof(_t), \
|
|
11
|
|
- .t = { .scalar = { .name = #_t } }, \
|
|
12
|
|
- }
|
|
13
|
|
-
|
|
14
|
|
-type_t t_void = builtin_scalar(void);
|
|
15
|
|
-
|
|
16
|
|
-type_t t_char = builtin_scalar(char);
|
|
17
|
|
-type_t t_uchar = builtin_scalar(unsigned char);
|
|
18
|
|
-
|
|
19
|
|
-type_t t_short = builtin_scalar(short);
|
|
20
|
|
-type_t t_ushort = builtin_scalar(unsigned short);
|
|
21
|
|
-
|
|
22
|
|
-type_t t_int = builtin_scalar(int);
|
|
23
|
|
-type_t t_uint = builtin_scalar(unsigned int);
|
|
24
|
|
-
|
|
25
|
|
-type_t t_long = builtin_scalar(long);
|
|
26
|
|
-type_t t_ulong = builtin_scalar(unsigned long);
|
|
27
|
|
-
|
|
28
|
|
-type_t t_llong = builtin_scalar(long long);
|
|
29
|
|
-type_t t_ullong = builtin_scalar(unsigned long long);
|
|
30
|
|
-
|
|
31
|
|
-type_t *builtin_types[] = {
|
|
32
|
|
- &t_void,
|
|
33
|
|
- &t_char, &t_uchar,
|
|
34
|
|
- &t_short, &t_ushort,
|
|
35
|
|
- &t_int, &t_uint,
|
|
36
|
|
- &t_long, &t_ulong,
|
|
37
|
|
- &t_llong, &t_ullong,
|
|
38
|
|
-
|
|
39
|
|
- NULL
|
|
40
|
|
-};
|
|
41
|
|
-
|
|
42
|
|
-struct type_list {
|
|
43
|
|
- type_t **type;
|
|
44
|
|
- size_t len;
|
|
45
|
|
-} types;
|
|
46
|
|
-
|
|
47
|
|
-
|
|
48
|
|
-void type_dump_func(type_t *t, FILE *fp)
|
|
|
9
|
+void type_dump_func(struct type *t, FILE *fp)
|
|
49
|
10
|
{
|
|
50
|
|
- field_t *arg;
|
|
|
11
|
+ struct tfield *arg;
|
|
51
|
12
|
|
|
52
|
|
- type_dump(t->t.func.type, fp);
|
|
53
|
|
- fprintf(fp, " (*%s)(", t->t.func.name ? : "");
|
|
|
13
|
+ type_dump(t->func.type, fp);
|
|
|
14
|
+ fputs(" (*)(", fp);
|
|
54
|
15
|
|
|
55
|
|
- if (!t->t.func.args) {
|
|
|
16
|
+ if (!t->func.args) {
|
|
56
|
17
|
fputs("void)", fp);
|
|
57
|
18
|
return;
|
|
58
|
19
|
}
|
|
59
|
20
|
|
|
60
|
|
- for (arg = t->t.func.args; arg->type; arg++) {
|
|
61
|
|
- if (arg != t->t.func.args)
|
|
|
21
|
+ for (arg = t->func.args; arg->type != T_VOID; arg++) {
|
|
|
22
|
+ if (arg != t->func.args)
|
|
62
|
23
|
fputs(", ", fp);
|
|
63
|
24
|
|
|
64
|
25
|
type_dump(arg->type, fp);
|
|
|
@@ -67,7 +28,7 @@ void type_dump_func(type_t *t, FILE *fp)
|
|
67
|
28
|
fputc(')', fp);
|
|
68
|
29
|
}
|
|
69
|
30
|
|
|
70
|
|
-void type_dump(type_t *t, FILE *fp)
|
|
|
31
|
+void type_dump(struct type *t, FILE *fp)
|
|
71
|
32
|
{
|
|
72
|
33
|
if (!t) {
|
|
73
|
34
|
fputs("<NONE>", fp);
|
|
|
@@ -75,47 +36,50 @@ void type_dump(type_t *t, FILE *fp)
|
|
75
|
36
|
}
|
|
76
|
37
|
|
|
77
|
38
|
switch (t->ttype){
|
|
|
39
|
+ case T_VOID:
|
|
|
40
|
+ fputs("void", fp);
|
|
|
41
|
+ break;
|
|
78
|
42
|
case T_TYPEDEF:
|
|
79
|
|
- fputs(t->t.tdef.name, fp);
|
|
|
43
|
+ fputs(t->tdef.name, fp);
|
|
80
|
44
|
break;
|
|
81
|
45
|
case T_SCALAR:
|
|
82
|
|
- fputs(t->t.scalar.name, fp);
|
|
|
46
|
+ fputs(t->scalar.name, fp);
|
|
83
|
47
|
break;
|
|
84
|
48
|
case T_POINTER:
|
|
85
|
|
- type_dump(t->t.pointer.type, fp);
|
|
|
49
|
+ type_dump(t->ptr.type, fp);
|
|
86
|
50
|
fputs(" *", fp);
|
|
87
|
51
|
break;
|
|
88
|
52
|
case T_ARRAY:
|
|
89
|
|
- type_dump(t->t.array.type, fp);
|
|
90
|
|
- fprintf(fp, "[%zu]", t->t.array.len);
|
|
|
53
|
+ type_dump(t->array.type, fp);
|
|
|
54
|
+ fprintf(fp, "[%zu]", t->array.len);
|
|
91
|
55
|
break;
|
|
92
|
56
|
case T_STRUCT:
|
|
93
|
57
|
fputs("struct ", fp);
|
|
94
|
|
- fputs(t->t.sou.name, fp);
|
|
|
58
|
+ fputs(t->sou.name, fp);
|
|
95
|
59
|
break;
|
|
96
|
60
|
case T_UNION:
|
|
97
|
61
|
fputs("union ", fp);
|
|
98
|
|
- fputs(t->t.sou.name, fp);
|
|
|
62
|
+ fputs(t->sou.name, fp);
|
|
99
|
63
|
break;
|
|
100
|
64
|
case T_FUNC:
|
|
101
|
65
|
type_dump_func(t, fp);
|
|
102
|
66
|
break;
|
|
103
|
67
|
case T_MAP:
|
|
104
|
68
|
fputs("map [", fp);
|
|
105
|
|
- type_dump(t->t.map.ktype, fp);
|
|
|
69
|
+ type_dump(t->map.ktype, fp);
|
|
106
|
70
|
fputs("] ", fp);
|
|
107
|
|
- type_dump(t->t.map.vtype, fp);
|
|
|
71
|
+ type_dump(t->map.vtype, fp);
|
|
108
|
72
|
break;
|
|
109
|
73
|
}
|
|
110
|
74
|
}
|
|
111
|
75
|
|
|
112
|
|
-void type_dump_cdecl_sou(type_t *t, FILE *fp)
|
|
|
76
|
+void type_dump_cdecl_sou(struct type *t, FILE *fp)
|
|
113
|
77
|
{
|
|
114
|
|
- field_t *f;
|
|
|
78
|
+ struct tfield *f;
|
|
115
|
79
|
|
|
116
|
80
|
type_dump(t, fp);
|
|
117
|
81
|
fputs(" {\n", fp);
|
|
118
|
|
- for (f = t->t.sou.fields; f->type; f++) {
|
|
|
82
|
+ for (f = t->sou.fields; f->type; f++) {
|
|
119
|
83
|
fputc('\t', fp);
|
|
120
|
84
|
type_dump(f->type, fp);
|
|
121
|
85
|
fprintf(fp, " %s;\n", f->name);
|
|
|
@@ -123,13 +87,13 @@ void type_dump_cdecl_sou(type_t *t, FILE *fp)
|
|
123
|
87
|
fputs("}", fp);
|
|
124
|
88
|
}
|
|
125
|
89
|
|
|
126
|
|
-void type_dump_cdecl(type_t *t, FILE *fp)
|
|
|
90
|
+void type_dump_cdecl(struct type *t, FILE *fp)
|
|
127
|
91
|
{
|
|
128
|
92
|
switch (t->ttype) {
|
|
129
|
93
|
case T_TYPEDEF:
|
|
130
|
94
|
fputs("typedef ", fp);
|
|
131
|
|
- type_dump(t->t.tdef.type, fp);
|
|
132
|
|
- fprintf(fp, " %s", t->t.tdef.name);
|
|
|
95
|
+ type_dump(t->tdef.type, fp);
|
|
|
96
|
+ fprintf(fp, " %s", t->tdef.name);
|
|
133
|
97
|
break;
|
|
134
|
98
|
|
|
135
|
99
|
case T_STRUCT:
|
|
|
@@ -137,6 +101,7 @@ void type_dump_cdecl(type_t *t, FILE *fp)
|
|
137
|
101
|
type_dump_cdecl_sou(t, fp);
|
|
138
|
102
|
break;
|
|
139
|
103
|
|
|
|
104
|
+ case T_VOID:
|
|
140
|
105
|
case T_SCALAR:
|
|
141
|
106
|
case T_POINTER:
|
|
142
|
107
|
case T_ARRAY:
|
|
|
@@ -147,73 +112,74 @@ void type_dump_cdecl(type_t *t, FILE *fp)
|
|
147
|
112
|
}
|
|
148
|
113
|
}
|
|
149
|
114
|
|
|
150
|
|
-void types_dump_cdecl(FILE *fp)
|
|
151
|
|
-{
|
|
152
|
|
- size_t i;
|
|
153
|
|
-
|
|
154
|
|
- for (i = 0; i < types.len; i++) {
|
|
155
|
|
- type_t *t = types.type[i];
|
|
156
|
|
-
|
|
157
|
|
- type_dump_cdecl(t, stdout);
|
|
158
|
|
- printf(" <sz:0x%zx>\n", t->size);
|
|
159
|
|
- }
|
|
160
|
|
-}
|
|
161
|
|
-
|
|
162
|
|
-type_t *type_map_of(type_t *ktype, type_t *vtype)
|
|
163
|
|
-{
|
|
164
|
|
- type_t *t;
|
|
165
|
|
- size_t i;
|
|
166
|
|
-
|
|
167
|
|
- for (i = 0; i < types.len; i++) {
|
|
168
|
|
- t = types.type[i];
|
|
169
|
|
- if ((t->ttype == T_MAP)
|
|
170
|
|
- && (t->t.map.ktype == ktype)
|
|
171
|
|
- && (t->t.map.vtype == vtype))
|
|
172
|
|
- return t;
|
|
173
|
|
- }
|
|
174
|
|
-
|
|
175
|
|
- t = calloc(1, sizeof(*t));
|
|
176
|
|
- t->ttype = T_MAP;
|
|
177
|
|
- t->t.map.vtype = vtype;
|
|
178
|
|
- t->t.map.ktype = ktype;
|
|
179
|
|
- type_add(t);
|
|
180
|
|
- return t;
|
|
181
|
|
-}
|
|
182
|
|
-
|
|
183
|
|
-type_t *type_ptr_of(type_t *type)
|
|
184
|
|
-{
|
|
185
|
|
- type_t *t;
|
|
186
|
|
- size_t i;
|
|
187
|
|
-
|
|
188
|
|
- for (i = 0; i < types.len; i++) {
|
|
189
|
|
- t = types.type[i];
|
|
190
|
|
- if ((t->ttype == T_POINTER)
|
|
191
|
|
- && (t->t.pointer.type == type))
|
|
192
|
|
- return t;
|
|
193
|
|
- }
|
|
194
|
|
-
|
|
195
|
|
- t = calloc(1, sizeof(*t));
|
|
196
|
|
- t->ttype = T_POINTER;
|
|
197
|
|
- t->t.pointer.type = type;
|
|
198
|
|
- type_add(t);
|
|
199
|
|
- return t;
|
|
200
|
|
-}
|
|
201
|
|
-
|
|
202
|
|
-type_t *type_normalize(type_t *t)
|
|
|
115
|
+/* void types_dump_cdecl(FILE *fp) */
|
|
|
116
|
+/* { */
|
|
|
117
|
+/* size_t i; */
|
|
|
118
|
+
|
|
|
119
|
+/* for (i = 0; i < types.len; i++) { */
|
|
|
120
|
+/* struct type *t = types.type[i]; */
|
|
|
121
|
+
|
|
|
122
|
+/* type_dump_cdecl(t, stdout); */
|
|
|
123
|
+/* printf(" <sz:0x%zx>\n", t->size); */
|
|
|
124
|
+/* } */
|
|
|
125
|
+/* } */
|
|
|
126
|
+
|
|
|
127
|
+/* struct type *type_map_of(struct type *ktype, struct type *vtype) */
|
|
|
128
|
+/* { */
|
|
|
129
|
+/* struct type *t; */
|
|
|
130
|
+/* size_t i; */
|
|
|
131
|
+
|
|
|
132
|
+/* for (i = 0; i < types.len; i++) { */
|
|
|
133
|
+/* t = types.type[i]; */
|
|
|
134
|
+/* if ((t->ttype == T_MAP) */
|
|
|
135
|
+/* && (t->map.ktype == ktype) */
|
|
|
136
|
+/* && (t->map.vtype == vtype)) */
|
|
|
137
|
+/* return t; */
|
|
|
138
|
+/* } */
|
|
|
139
|
+
|
|
|
140
|
+/* t = calloc(1, sizeof(*t)); */
|
|
|
141
|
+/* t->ttype = T_MAP; */
|
|
|
142
|
+/* t->map.vtype = vtype; */
|
|
|
143
|
+/* t->map.ktype = ktype; */
|
|
|
144
|
+/* type_add(t); */
|
|
|
145
|
+/* return t; */
|
|
|
146
|
+/* } */
|
|
|
147
|
+
|
|
|
148
|
+/* struct type *type_ptr_of(struct type *type) */
|
|
|
149
|
+/* { */
|
|
|
150
|
+/* struct type *t; */
|
|
|
151
|
+/* size_t i; */
|
|
|
152
|
+
|
|
|
153
|
+/* for (i = 0; i < types.len; i++) { */
|
|
|
154
|
+/* t = types.type[i]; */
|
|
|
155
|
+/* if ((t->ttype == T_POINTER) */
|
|
|
156
|
+/* && (t->pointer.type == type)) */
|
|
|
157
|
+/* return t; */
|
|
|
158
|
+/* } */
|
|
|
159
|
+
|
|
|
160
|
+/* t = calloc(1, sizeof(*t)); */
|
|
|
161
|
+/* t->ttype = T_POINTER; */
|
|
|
162
|
+/* t->pointer.type = type; */
|
|
|
163
|
+/* type_add(t); */
|
|
|
164
|
+/* return t; */
|
|
|
165
|
+/* } */
|
|
|
166
|
+
|
|
|
167
|
+
|
|
|
168
|
+struct type *type_normalize(struct type *t)
|
|
203
|
169
|
{
|
|
204
|
170
|
while (t->ttype == T_TYPEDEF)
|
|
205
|
|
- t = t->t.tdef.type;
|
|
|
171
|
+ t = t->tdef.type;
|
|
206
|
172
|
|
|
207
|
173
|
return t;
|
|
208
|
174
|
}
|
|
209
|
175
|
|
|
210
|
|
-int type_equal(type_t *a, type_t *b)
|
|
|
176
|
+int type_equal(struct type *a, struct type *b)
|
|
211
|
177
|
{
|
|
212
|
178
|
/* TODO */
|
|
213
|
179
|
return a == b;
|
|
214
|
180
|
}
|
|
215
|
181
|
|
|
216
|
|
-int type_compatible(type_t *a, type_t *b)
|
|
|
182
|
+int type_compatible(struct type *a, struct type *b)
|
|
217
|
183
|
{
|
|
218
|
184
|
|
|
219
|
185
|
a = type_normalize(a);
|
|
|
@@ -223,22 +189,22 @@ int type_compatible(type_t *a, type_t *b)
|
|
223
|
189
|
return 0;
|
|
224
|
190
|
|
|
225
|
191
|
switch (a->ttype){
|
|
|
192
|
+ case T_VOID:
|
|
226
|
193
|
case T_SCALAR:
|
|
227
|
|
- return 1;
|
|
228
|
194
|
case T_POINTER:
|
|
229
|
195
|
return 1;
|
|
230
|
196
|
case T_ARRAY:
|
|
231
|
|
- if (a->t.array.len != b->t.array.len)
|
|
|
197
|
+ if (a->array.len != b->array.len)
|
|
232
|
198
|
return 0;
|
|
233
|
199
|
|
|
234
|
|
- return type_compatible(a->t.array.type, b->t.array.type);
|
|
|
200
|
+ return type_compatible(a->array.type, b->array.type);
|
|
235
|
201
|
case T_STRUCT:
|
|
236
|
202
|
case T_UNION:
|
|
237
|
|
- return !strcmp(a->t.sou.name, b->t.sou.name);
|
|
|
203
|
+ return !strcmp(a->sou.name, b->sou.name);
|
|
238
|
204
|
case T_FUNC:
|
|
239
|
|
- return type_compatible(a->t.func.type, b->t.func.type);
|
|
|
205
|
+ return type_compatible(a->func.type, b->func.type);
|
|
240
|
206
|
case T_MAP:
|
|
241
|
|
- return type_compatible(a->t.map.vtype, b->t.map.vtype);
|
|
|
207
|
+ return type_compatible(a->map.vtype, b->map.vtype);
|
|
242
|
208
|
|
|
243
|
209
|
case T_TYPEDEF:
|
|
244
|
210
|
assert(0);
|
|
|
@@ -248,91 +214,281 @@ int type_compatible(type_t *a, type_t *b)
|
|
248
|
214
|
return 0;
|
|
249
|
215
|
}
|
|
250
|
216
|
|
|
251
|
|
-void type_size_set_sou(type_t *t)
|
|
|
217
|
+/* void type_size_set_sou(struct type *t) */
|
|
|
218
|
+/* { */
|
|
|
219
|
+/* struct tfield *f; */
|
|
|
220
|
+/* size_t size = 0; */
|
|
|
221
|
+
|
|
|
222
|
+/* if (!t->sou.fields) */
|
|
|
223
|
+/* return; */
|
|
|
224
|
+
|
|
|
225
|
+/* for (f = t->sou.fields; f->name; f++) { */
|
|
|
226
|
+/* /\* size of all members is now known yet, abort *\/ */
|
|
|
227
|
+/* if (!f->type->size) */
|
|
|
228
|
+/* return; */
|
|
|
229
|
+
|
|
|
230
|
+/* if (!t->sou.packed) */
|
|
|
231
|
+/* size += size % f->type->size; */
|
|
|
232
|
+
|
|
|
233
|
+/* size += f->type->size; */
|
|
|
234
|
+/* f->offset = size; */
|
|
|
235
|
+/* } */
|
|
|
236
|
+
|
|
|
237
|
+/* if (!t->sou.packed && size) { */
|
|
|
238
|
+/* /\* align complete struct to requirements of first */
|
|
|
239
|
+/* * member, if struct keep going downwards *\/ */
|
|
|
240
|
+/* f = t->sou.fields; */
|
|
|
241
|
+/* while (f->type->ttype == T_STRUCT) */
|
|
|
242
|
+/* f = f->type->sou.fields; */
|
|
|
243
|
+
|
|
|
244
|
+/* size += size % f->type->size; */
|
|
|
245
|
+/* } */
|
|
|
246
|
+
|
|
|
247
|
+/* t->size = size; */
|
|
|
248
|
+/* } */
|
|
|
249
|
+
|
|
|
250
|
+/* void type_size_set(struct type *t) */
|
|
|
251
|
+/* { */
|
|
|
252
|
+/* switch (t->ttype) { */
|
|
|
253
|
+/* case T_TYPEDEF: */
|
|
|
254
|
+/* t->size = t->tdef.type->size; */
|
|
|
255
|
+/* break; */
|
|
|
256
|
+/* case T_STRUCT: */
|
|
|
257
|
+/* case T_UNION: */
|
|
|
258
|
+/* type_size_set_sou(t); */
|
|
|
259
|
+/* break; */
|
|
|
260
|
+/* case T_FUNC: */
|
|
|
261
|
+/* t->size = t->func.type->size; */
|
|
|
262
|
+/* assert(t->func.generate_ir); */
|
|
|
263
|
+/* break; */
|
|
|
264
|
+/* case T_SCALAR: */
|
|
|
265
|
+/* break; */
|
|
|
266
|
+/* case T_POINTER: */
|
|
|
267
|
+/* t->size = sizeof(void *); */
|
|
|
268
|
+/* break; */
|
|
|
269
|
+/* case T_ARRAY: */
|
|
|
270
|
+/* t->size = t->array.len * t->array.type->size; */
|
|
|
271
|
+/* break; */
|
|
|
272
|
+/* case T_MAP: */
|
|
|
273
|
+/* /\* map fds are s64:s *\/ */
|
|
|
274
|
+/* t->size = 8; */
|
|
|
275
|
+/* break; */
|
|
|
276
|
+/* } */
|
|
|
277
|
+/* } */
|
|
|
278
|
+
|
|
|
279
|
+static ssize_t type_alignof_union(struct type *t)
|
|
|
280
|
+{
|
|
|
281
|
+ ssize_t amax = -EINVAL;
|
|
|
282
|
+ struct tfield *f;
|
|
|
283
|
+
|
|
|
284
|
+ for (f = t->sou.fields; f->type != T_VOID; f++) {
|
|
|
285
|
+ ssize_t a;
|
|
|
286
|
+
|
|
|
287
|
+ a = type_alignof(f->type);
|
|
|
288
|
+ if (a < 0)
|
|
|
289
|
+ return a;
|
|
|
290
|
+
|
|
|
291
|
+ if (a > amax)
|
|
|
292
|
+ amax = a;
|
|
|
293
|
+ }
|
|
|
294
|
+
|
|
|
295
|
+ return amax;
|
|
|
296
|
+}
|
|
|
297
|
+
|
|
|
298
|
+ssize_t type_alignof(struct type *t)
|
|
|
299
|
+{
|
|
|
300
|
+ switch (t->ttype){
|
|
|
301
|
+ case T_VOID:
|
|
|
302
|
+ case T_SCALAR:
|
|
|
303
|
+ case T_POINTER:
|
|
|
304
|
+ case T_FUNC:
|
|
|
305
|
+ case T_MAP:
|
|
|
306
|
+ return type_sizeof(t);
|
|
|
307
|
+ case T_TYPEDEF:
|
|
|
308
|
+ return type_alignof(t->tdef.type);
|
|
|
309
|
+ case T_ARRAY:
|
|
|
310
|
+ return type_alignof(t->array.type);
|
|
|
311
|
+ case T_STRUCT:
|
|
|
312
|
+ if (!t->sou.fields)
|
|
|
313
|
+ return type_alignof(&t_void);
|
|
|
314
|
+ return type_alignof(t->sou.fields->type);
|
|
|
315
|
+ case T_UNION:
|
|
|
316
|
+ if (!t->sou.fields)
|
|
|
317
|
+ return type_alignof(&t_void);
|
|
|
318
|
+ return type_alignof_union(t);
|
|
|
319
|
+ }
|
|
|
320
|
+
|
|
|
321
|
+ return -EINVAL;
|
|
|
322
|
+}
|
|
|
323
|
+
|
|
|
324
|
+static ssize_t type_sizeof_struct(struct type *t)
|
|
252
|
325
|
{
|
|
253
|
|
- field_t *f;
|
|
|
326
|
+ struct tfield *f;
|
|
254
|
327
|
size_t size = 0;
|
|
|
328
|
+ ssize_t fsize;
|
|
255
|
329
|
|
|
256
|
|
- if (!t->t.sou.fields)
|
|
257
|
|
- return;
|
|
|
330
|
+ if (!t->sou.fields)
|
|
|
331
|
+ return 0;
|
|
258
|
332
|
|
|
259
|
|
- for (f = t->t.sou.fields; f->name; f++) {
|
|
|
333
|
+ for (f = t->sou.fields; f->name; f++) {
|
|
260
|
334
|
/* size of all members is now known yet, abort */
|
|
261
|
|
- if (!f->type->size)
|
|
262
|
|
- return;
|
|
|
335
|
+ fsize = type_sizeof(f->type);
|
|
|
336
|
+ if (fsize < 0)
|
|
|
337
|
+ return fsize;
|
|
263
|
338
|
|
|
264
|
|
- if (!t->t.sou.packed)
|
|
265
|
|
- size += size % f->type->size;
|
|
|
339
|
+ size += fsize;
|
|
266
|
340
|
|
|
267
|
|
- size += f->type->size;
|
|
268
|
|
- f->offset = size;
|
|
|
341
|
+ if (!t->sou.packed)
|
|
|
342
|
+ size += size % type_alignof(f->type);
|
|
269
|
343
|
}
|
|
270
|
344
|
|
|
271
|
|
- if (!t->t.sou.packed && size) {
|
|
272
|
|
- /* align complete struct to requirements of first
|
|
273
|
|
- * member, if struct keep going downwards */
|
|
274
|
|
- f = t->t.sou.fields;
|
|
275
|
|
- while (f->type->ttype == T_STRUCT)
|
|
276
|
|
- f = f->type->t.sou.fields;
|
|
|
345
|
+ if (!t->sou.packed && size)
|
|
|
346
|
+ size += size % type_alignof(t);
|
|
|
347
|
+
|
|
|
348
|
+ return size;
|
|
|
349
|
+}
|
|
|
350
|
+
|
|
|
351
|
+static ssize_t type_sizeof_union(struct type *t)
|
|
|
352
|
+{
|
|
|
353
|
+ struct tfield *f;
|
|
|
354
|
+ ssize_t size = 0;
|
|
|
355
|
+
|
|
|
356
|
+ if (!t->sou.fields)
|
|
|
357
|
+ return 0;
|
|
|
358
|
+
|
|
|
359
|
+ for (f = t->sou.fields; f->name; f++) {
|
|
|
360
|
+ ssize_t z, a;
|
|
|
361
|
+
|
|
|
362
|
+ z = type_sizeof(f->type);
|
|
|
363
|
+ a = t->sou.packed ? type_alignof(f->type) : 0;
|
|
|
364
|
+ if ((z < 0) || (a < 0))
|
|
|
365
|
+ return -EINVAL;
|
|
277
|
366
|
|
|
278
|
|
- size += size % f->type->size;
|
|
|
367
|
+ z += z % a;
|
|
|
368
|
+ if (z > size)
|
|
|
369
|
+ size = z;
|
|
279
|
370
|
}
|
|
280
|
371
|
|
|
281
|
|
- t->size = size;
|
|
|
372
|
+ return size;
|
|
282
|
373
|
}
|
|
283
|
374
|
|
|
284
|
|
-void type_size_set(type_t *t)
|
|
|
375
|
+ssize_t type_sizeof(struct type *t)
|
|
285
|
376
|
{
|
|
286
|
|
- switch (t->ttype) {
|
|
287
|
|
- case T_TYPEDEF:
|
|
288
|
|
- t->size = t->t.tdef.type->size;
|
|
289
|
|
- break;
|
|
290
|
|
- case T_STRUCT:
|
|
291
|
|
- case T_UNION:
|
|
292
|
|
- type_size_set_sou(t);
|
|
293
|
|
- break;
|
|
294
|
|
- case T_FUNC:
|
|
295
|
|
- t->size = t->t.func.type->size;
|
|
296
|
|
- assert(t->t.func.generate_ir);
|
|
297
|
|
- break;
|
|
|
377
|
+ switch (t->ttype){
|
|
|
378
|
+ case T_VOID:
|
|
|
379
|
+ return sizeof(void);
|
|
298
|
380
|
case T_SCALAR:
|
|
299
|
|
- break;
|
|
|
381
|
+ return t->scalar.size;
|
|
|
382
|
+ case T_TYPEDEF:
|
|
|
383
|
+ return type_sizeof(t->tdef.type);
|
|
300
|
384
|
case T_POINTER:
|
|
301
|
|
- t->size = sizeof(void *);
|
|
302
|
|
- break;
|
|
|
385
|
+ case T_FUNC:
|
|
|
386
|
+ return sizeof(void *);
|
|
303
|
387
|
case T_ARRAY:
|
|
304
|
|
- t->size = t->t.array.len * t->t.array.type->size;
|
|
305
|
|
- break;
|
|
|
388
|
+ return t->array.len * type_sizeof(t->array.type);
|
|
|
389
|
+ case T_STRUCT:
|
|
|
390
|
+ return type_sizeof_struct(t);
|
|
|
391
|
+ case T_UNION:
|
|
|
392
|
+ return type_sizeof_union(t);
|
|
306
|
393
|
case T_MAP:
|
|
307
|
|
- /* map fds are s64:s */
|
|
308
|
|
- t->size = 8;
|
|
309
|
|
- break;
|
|
|
394
|
+ return sizeof(int);
|
|
310
|
395
|
}
|
|
|
396
|
+
|
|
|
397
|
+ return -EINVAL;
|
|
311
|
398
|
}
|
|
312
|
399
|
|
|
313
|
|
-int type_cmp(const void *_a, const void *_b)
|
|
|
400
|
+/* int type_walk_fields(struct tfield *f, twalk_fn pre, twalk_fn post, void *ctx) */
|
|
|
401
|
+/* { */
|
|
|
402
|
+/* if (!f) */
|
|
|
403
|
+/* return 0; */
|
|
|
404
|
+
|
|
|
405
|
+/* while (f->type != T_VOID) { */
|
|
|
406
|
+/* if (pre && (err = pre(f->type, ctx))) */
|
|
|
407
|
+/* return err; */
|
|
|
408
|
+
|
|
|
409
|
+/* err = type_walk(f->type, pre, post, ctx); */
|
|
|
410
|
+/* if (err) */
|
|
|
411
|
+/* return err; */
|
|
|
412
|
+
|
|
|
413
|
+/* if (post && (err = post(f->type, ctx))) */
|
|
|
414
|
+/* return err; */
|
|
|
415
|
+/* } */
|
|
|
416
|
+/* } */
|
|
|
417
|
+
|
|
|
418
|
+/* int type_walk(struct type *t, twalk_fn pre, twalk_fn post, void *ctx) */
|
|
|
419
|
+/* { */
|
|
|
420
|
+/* int err; */
|
|
|
421
|
+
|
|
|
422
|
+/* if (pre && (err = pre(t, ctx))) */
|
|
|
423
|
+/* return err; */
|
|
|
424
|
+
|
|
|
425
|
+/* switch (t->ttype){ */
|
|
|
426
|
+/* case T_VOID: */
|
|
|
427
|
+/* case T_SCALAR: */
|
|
|
428
|
+/* break; */
|
|
|
429
|
+/* case T_TYPEDEF: */
|
|
|
430
|
+/* err = type_walk(t->tdef.type, pre, post, ctx); */
|
|
|
431
|
+/* break; */
|
|
|
432
|
+/* case T_POINTER: */
|
|
|
433
|
+/* err = type_walk(t->ptr.type, pre, post, ctx); */
|
|
|
434
|
+/* break; */
|
|
|
435
|
+/* case T_ARRAY: */
|
|
|
436
|
+/* err = type_walk(t->array.type, pre, post, ctx); */
|
|
|
437
|
+/* break; */
|
|
|
438
|
+/* case T_STRUCT: */
|
|
|
439
|
+/* case T_UNION: */
|
|
|
440
|
+/* err = type_walk_fields(t->sou.fields, pre, post, ctx); */
|
|
|
441
|
+/* break; */
|
|
|
442
|
+/* case T_FUNC: */
|
|
|
443
|
+/* err = type_walk_fields(t->func.args, pre, post, ctx); */
|
|
|
444
|
+/* break; */
|
|
|
445
|
+/* case T_MAP: */
|
|
|
446
|
+/* err = type_walk(t->map.ktype, pre, post, ctx); */
|
|
|
447
|
+/* if (err) */
|
|
|
448
|
+/* break; */
|
|
|
449
|
+
|
|
|
450
|
+/* err = type_walk(t->map.vtype, pre, post, ctx); */
|
|
|
451
|
+/* break; */
|
|
|
452
|
+/* } */
|
|
|
453
|
+
|
|
|
454
|
+/* if (err) */
|
|
|
455
|
+/* return err; */
|
|
|
456
|
+
|
|
|
457
|
+/* if (post && (err = post(t, ctx))) */
|
|
|
458
|
+/* return err; */
|
|
|
459
|
+/* } */
|
|
|
460
|
+
|
|
|
461
|
+
|
|
|
462
|
+int all_types_cmp(const void *_a, const void *_b)
|
|
314
|
463
|
{
|
|
315
|
|
- const type_t *a = *((type_t **)_a);
|
|
316
|
|
- const type_t *b = *((type_t **)_b);
|
|
|
464
|
+ const struct type *a = *((struct type **)_a);
|
|
|
465
|
+ const struct type *b = *((struct type **)_b);
|
|
317
|
466
|
|
|
318
|
467
|
return a - b;
|
|
319
|
468
|
}
|
|
320
|
469
|
|
|
321
|
|
-int type_add(type_t *t)
|
|
|
470
|
+struct type_list {
|
|
|
471
|
+ struct type **types;
|
|
|
472
|
+ size_t len;
|
|
|
473
|
+} all_types;
|
|
|
474
|
+
|
|
|
475
|
+int type_add(struct type *t)
|
|
322
|
476
|
{
|
|
323
|
|
- if (bsearch(t, types.type, types.len, sizeof(*types.type), type_cmp))
|
|
|
477
|
+ if (bsearch(t, all_types.types, all_types.len,
|
|
|
478
|
+ sizeof(*all_types.types), all_types_cmp))
|
|
324
|
479
|
return 0;
|
|
325
|
480
|
|
|
326
|
|
- type_size_set(t);
|
|
|
481
|
+ /* type_size_set(t); */
|
|
327
|
482
|
|
|
328
|
|
- types.type = realloc(types.type, ++types.len * sizeof(*types.type));
|
|
329
|
|
- types.type[types.len - 1] = t;
|
|
330
|
|
- qsort(types.type, types.len, sizeof(*types.type), type_cmp);
|
|
|
483
|
+ all_types.types = realloc(all_types.types,
|
|
|
484
|
+ ++all_types.len * sizeof(*all_types.types));
|
|
|
485
|
+ all_types.types[all_types.len - 1] = t;
|
|
|
486
|
+ qsort(all_types.types, all_types.len, sizeof(*all_types.types), all_types_cmp);
|
|
331
|
487
|
|
|
332
|
488
|
return 0;
|
|
333
|
489
|
}
|
|
334
|
490
|
|
|
335
|
|
-int type_add_list(type_t **ts)
|
|
|
491
|
+int type_add_list(struct type **ts)
|
|
336
|
492
|
{
|
|
337
|
493
|
int err;
|
|
338
|
494
|
|
|
|
@@ -345,6 +501,58 @@ int type_add_list(type_t **ts)
|
|
345
|
501
|
return 0;
|
|
346
|
502
|
}
|
|
347
|
503
|
|
|
|
504
|
+#define is_signed(_t) (((_t)(-1)) < 0)
|
|
|
505
|
+
|
|
|
506
|
+#define builtin_scalar(_t) { \
|
|
|
507
|
+ .ttype = T_SCALAR, \
|
|
|
508
|
+ .scalar = { \
|
|
|
509
|
+ .name = #_t, \
|
|
|
510
|
+ .size = sizeof(_t), \
|
|
|
511
|
+ .is_signed = is_signed(_t), \
|
|
|
512
|
+ }, \
|
|
|
513
|
+ }
|
|
|
514
|
+
|
|
|
515
|
+struct type t_void = { .ttype = T_VOID };
|
|
|
516
|
+
|
|
|
517
|
+#pragma GCC diagnostic ignored "-Wtype-limits"
|
|
|
518
|
+/* is_signed will generate a warning for unsigned types since the
|
|
|
519
|
+ * expression can never be true. this is exactly what we're interested
|
|
|
520
|
+ * in here though. it gets us out of having to specify scalar
|
|
|
521
|
+ * signedness per architecture. */
|
|
|
522
|
+
|
|
|
523
|
+struct type t_char = builtin_scalar(char);
|
|
|
524
|
+struct type t_schar = builtin_scalar(signed char);
|
|
|
525
|
+struct type t_uchar = builtin_scalar(unsigned char);
|
|
|
526
|
+
|
|
|
527
|
+struct type t_short = builtin_scalar(short);
|
|
|
528
|
+struct type t_sshort = builtin_scalar(signed short);
|
|
|
529
|
+struct type t_ushort = builtin_scalar(unsigned short);
|
|
|
530
|
+
|
|
|
531
|
+struct type t_int = builtin_scalar(int);
|
|
|
532
|
+struct type t_sint = builtin_scalar(signed int);
|
|
|
533
|
+struct type t_uint = builtin_scalar(unsigned int);
|
|
|
534
|
+
|
|
|
535
|
+struct type t_long = builtin_scalar(long);
|
|
|
536
|
+struct type t_slong = builtin_scalar(signed long);
|
|
|
537
|
+struct type t_ulong = builtin_scalar(unsigned long);
|
|
|
538
|
+
|
|
|
539
|
+struct type t_llong = builtin_scalar(long long);
|
|
|
540
|
+struct type t_sllong = builtin_scalar(signed long long);
|
|
|
541
|
+struct type t_ullong = builtin_scalar(unsigned long long);
|
|
|
542
|
+
|
|
|
543
|
+#pragma GCC diagnostic pop
|
|
|
544
|
+
|
|
|
545
|
+struct type *builtin_types[] = {
|
|
|
546
|
+ &t_void,
|
|
|
547
|
+ &t_char, &t_schar, &t_uchar,
|
|
|
548
|
+ &t_short, &t_sshort, &t_ushort,
|
|
|
549
|
+ &t_int, &t_sint, &t_uint,
|
|
|
550
|
+ &t_long, &t_slong, &t_ulong,
|
|
|
551
|
+ &t_llong, &t_sllong, &t_ullong,
|
|
|
552
|
+
|
|
|
553
|
+ NULL
|
|
|
554
|
+};
|
|
|
555
|
+
|
|
348
|
556
|
__attribute__((constructor))
|
|
349
|
557
|
static void type_init(void)
|
|
350
|
558
|
{
|