summaryrefslogtreecommitdiffstats
path: root/libsyscalls_get_datatype_description.c
blob: 974c0feafe1732f1094c815267387153284361a7 (plain) (blame)
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* See LICENSE file for copyright and license details. */
#include "common.h"


#include "generated/types.c"

enum libsyscalls_error
libsyscalls_get_datatype_description(enum libsyscalls_os os, enum libsyscalls_arch arch,
                                     enum libsyscalls_datatype datatype,
                                     struct libsyscalls_datatype_description *description_out)
{
	struct libsyscalls_datatype_description description_discard, larger_type;
	enum libsyscalls_error r;
	unsigned class;
	int half = 0;
	enum endian endian;
	size_t i, j, charbits;
	int divide_array_size_with_type_size_out;

	if (!description_out)
		description_out = &description_discard;

	if ((unsigned)datatype & ~SET_MASK_TRAIL(LIBSYSCALLS_TYPEBITSMASK))
		return LIBSYSCALLS_E_INVAL;

	datatype ^= class = datatype & LIBSYSCALLS_TYPEBITSMASK;

	if (class == LIBSYSCALLS_TYPEBITS_SCALAR) {
		description_out->array_size = 1;
		description_out->relative_position_of_array_size = 0;
		description_out->absolute_position_of_array_size = -1;
		description_out->fill_is_known = 1;
		description_out->is_signed = 0;
		description_out->is_unsigned = 0;
		description_out->min_is_minus_max = 0;
		description_out->padding__ = 0;

	} else if (class == LIBSYSCALLS_TYPEBITS_ARRAY) {
		description_out->array_size = 0;
		description_out->relative_position_of_array_size = +1;
		description_out->absolute_position_of_array_size = -1;
		description_out->fill_is_known = 1;
		description_out->is_signed = 0;
		description_out->is_unsigned = 0;
		description_out->min_is_minus_max = 0;
		description_out->padding__ = 0;

	} else if (class == LIBSYSCALLS_TYPEBITS_ARRAY_UNKNOWN_FILL) {
		description_out->array_size = 0;
		description_out->relative_position_of_array_size = +1;
		description_out->absolute_position_of_array_size = -1;
		description_out->fill_is_known = 0;
		description_out->is_signed = 0;
		description_out->is_unsigned = 0;
		description_out->min_is_minus_max = 0;
		description_out->padding__ = 0;

	} else {
		return LIBSYSCALLS_E_INVAL;
	}

	if (datatype >= LIBSYSCALLS_TYPEOFFSET_STRUCTS_AND_UNIONS)
		return LIBSYSCALLS_E_ISSTRUCT;

	memset(description_out->byteorder, ~0, sizeof(description_out->byteorder));

#define CASE(ARCH, CHARBITS, INTPTR_BITS, SIZE_BITS, INTMAX_ALIGN, ENDIAN, SIGN)\
	case ARCH: description_out->sign_representation = LIBSYSCALLS_SIGN_##SIGN; break
	switch ((int)arch) {
	LIST_ARCH_SPECS(CASE, ;);
	default:
		return LIBSYSCALLS_E_ARCHNOSUP;
	}
#undef CASE

	description_out->annotation = LIBSYSCALLS_ANNOTATION_NONE;
	description_out->section = LIBSYSCALLS_SECTION_WHOLE;

	switch (description_out->sign_representation) {
	case LIBSYSCALLS_SIGN_ONES_COMPLEMENT:
	case LIBSYSCALLS_SIGN_SIGN_MAGNITUDE:
		description_out->min_is_minus_max = 1;
		break;
	case LIBSYSCALLS_SIGN_TWOS_COMPLEMENT:
	case LIBSYSCALLS_SIGN_EXCESS_HALF:
	case LIBSYSCALLS_SIGN_UNDETERMINED:
		break;
	}

	if (datatype >= LIBSYSCALLS_TYPEOFFSET_FIXED_ARRAYS) {
		if (class != LIBSYSCALLS_TYPEBITS_SCALAR)
			return LIBSYSCALLS_E_INVAL;
		switch ((int)datatype) {
		case LIBSYSCALLS_TYPE_2_INTS:
			datatype = LIBSYSCALLS_TYPE_INT;
			description_out->array_size = 2;
			break;
		case LIBSYSCALLS_TYPE_2_INTS_FD:
			datatype = LIBSYSCALLS_TYPE_INT;
			description_out->annotation = LIBSYSCALLS_ANNOTATION_FD;
			description_out->array_size = 2;
			break;
		case LIBSYSCALLS_TYPE_2_UINT32S:
			datatype = LIBSYSCALLS_TYPE_UINT32;
			description_out->array_size = 2;
			break;
		case LIBSYSCALLS_TYPE_BUFFER_9:
			datatype = LIBSYSCALLS_TYPE_CHAR;
			description_out->array_size = 9;
			break;
		case LIBSYSCALLS_TYPE_BUFFER_65:
			datatype = LIBSYSCALLS_TYPE_CHAR;
			description_out->array_size = 65;
			break;
		case LIBSYSCALLS_TYPE_FD_SET:
			goto os_dependent;
		default:
			return LIBSYSCALLS_E_INVAL;
		}
		goto unannotated;

	} else if (datatype >= LIBSYSCALLS_TYPEOFFSET_ANNOTATED_NUMERICALS) {
		switch ((int)datatype) {
		case LIBSYSCALLS_TYPE_INT_SIGNAL:
			datatype = LIBSYSCALLS_TYPE_INT;
			description_out->annotation = LIBSYSCALLS_ANNOTATION_SIGNAL;
			break;
		case LIBSYSCALLS_TYPE_INT_FD:
			datatype = LIBSYSCALLS_TYPE_INT;
			description_out->annotation = LIBSYSCALLS_ANNOTATION_FD;
			break;
		case LIBSYSCALLS_TYPE_INT_ATFD:
			datatype = LIBSYSCALLS_TYPE_INT;
			description_out->annotation = LIBSYSCALLS_ANNOTATION_ATFD;
			break;
		case LIBSYSCALLS_TYPE_LONG_FD:
			datatype = LIBSYSCALLS_TYPE_LONG;
			description_out->annotation = LIBSYSCALLS_ANNOTATION_FD;
			break;
		case LIBSYSCALLS_TYPE_LONG_ATFD:
			datatype = LIBSYSCALLS_TYPE_LONG;
			description_out->annotation = LIBSYSCALLS_ANNOTATION_ATFD;
			break;
		default:
			return LIBSYSCALLS_E_INVAL;
		}
		goto unannotated;

	} else if (datatype >= LIBSYSCALLS_TYPEOFFSET_UNANNOTATED_NUMERICALS) {
	unannotated:
		if (datatype - LIBSYSCALLS_TYPEOFFSET_UNANNOTATED_NUMERICALS
		    >= COUNT_LIST(LIBSYSCALLS_LIST_UNANNOTATED_NUMERICALS)) {
			return LIBSYSCALLS_E_INVAL;
		} else if (datatype == LIBSYSCALLS_TYPE_MEMORY_ADDRESS) {
			datatype = LIBSYSCALLS_TYPE_INTPTR;
		} else if (datatype == LIBSYSCALLS_TYPE_CHAR) {
			datatype = LIBSYSCALLS_TYPE_SCHAR;
		} else {
			description_out->is_unsigned = (datatype - LIBSYSCALLS_TYPE_SCHAR) & 1;
			description_out->is_signed = 1 ^ description_out->is_unsigned;
			datatype -= description_out->is_unsigned;
		}
		goto arch_or_os_dependent;

	} else if (datatype >= LIBSYSCALLS_TYPEOFFSET_COMPOSITE_PRIMITIVES) {
		if (class != LIBSYSCALLS_TYPEBITS_SCALAR)
			return LIBSYSCALLS_E_INVAL;
		if (datatype - LIBSYSCALLS_TYPEOFFSET_COMPOSITE_PRIMITIVES
		    >= COUNT_LIST(LIBSYSCALLS_LIST_COMPOSITE_PRIMITIVES))
			return LIBSYSCALLS_E_INVAL;
		datatype = LIBSYSCALLS_TYPE_MEMORY_ADDRESS;
		goto unannotated;

	} else if (datatype >= LIBSYSCALLS_TYPEOFFSET_SPLIT_PRIMITIVES) {
		enum libsyscalls_datatype t = datatype;
		if (class != LIBSYSCALLS_TYPEBITS_SCALAR)
			return LIBSYSCALLS_E_INVAL;
		switch ((int)datatype) {
		case LIBSYSCALLS_TYPE_UINT64_HIGH_32:
		case LIBSYSCALLS_TYPE_UINT64_LOW_32:
		case LIBSYSCALLS_TYPE_UINT64_FRONT_32:
		case LIBSYSCALLS_TYPE_UINT64_BACK_32:
			description_out->is_unsigned = 1;
			datatype = LIBSYSCALLS_TYPE_UINT32;
			break;
		case LIBSYSCALLS_TYPE_INT64_HIGH_32:
		case LIBSYSCALLS_TYPE_INT64_LOW_32:
		case LIBSYSCALLS_TYPE_INT64_FRONT_32:
		case LIBSYSCALLS_TYPE_INT64_BACK_32:
			t -= LIBSYSCALLS_TYPE_INT64_HIGH_32;
			t += LIBSYSCALLS_TYPE_UINT64_HIGH_32;
			description_out->is_signed = 1;
			datatype = LIBSYSCALLS_TYPE_INT32;
			break;
		default:
			return LIBSYSCALLS_E_INVAL;
		}
		switch ((int)t) {
		case LIBSYSCALLS_TYPE_UINT64_HIGH_32:
			description_out->section = LIBSYSCALLS_SECTION_UPPER_HALF;
			break;
		case LIBSYSCALLS_TYPE_UINT64_LOW_32:
			description_out->section = LIBSYSCALLS_SECTION_LOWER_HALF;
			break;
		case LIBSYSCALLS_TYPE_UINT64_FRONT_32:
		case LIBSYSCALLS_TYPE_UINT64_BACK_32:
			half = t == LIBSYSCALLS_TYPE_UINT64_FRONT_32 ? -1 : +1;
			r = libsyscalls_get_datatype_description(os, arch, LIBSYSCALLS_TYPE_UINT64, &larger_type);
			if (r)
				return r;
			break;
		default:
			abort();
		}
		goto unannotated;

	} else {
		if (class)
			return LIBSYSCALLS_E_INVAL;
		switch ((int)datatype) {
		case LIBSYSCALLS_TYPE_UNKNOWN:
		case LIBSYSCALLS_TYPE_DYNAMIC:
			datatype = LIBSYSCALLS_TYPE_DYNAMIC;
			description_out->is_signed = 1;
			description_out->is_unsigned = 1;
			description_out->sign_representation = LIBSYSCALLS_SIGN_UNDETERMINED;
			description_out->annotation = LIBSYSCALLS_ANNOTATION_UNDETERMINED;
			description_out->section = LIBSYSCALLS_SECTION_UNDETERMINED;
			goto os_dependent;
		case LIBSYSCALLS_TYPE_NO_RETURN:
		case LIBSYSCALLS_TYPE_VOID:
			description_out->width_in_bits = 0;
			break;
		default:
			return LIBSYSCALLS_E_INVAL;
		}
	}

	return LIBSYSCALLS_E_OK;

arch_or_os_dependent:
	divide_array_size_with_type_size_out = 0;

arch_dependent:
	switch ((int)datatype) {
	/* Note that LIBSYSCALLS_TYPE_INTN does not have the same semantics
	 * as POSIX's intN_t: these are not necessarily two's complement
	 * encoded and can have any range of valid numbers as long as that
	 * type has the correct width */
	case LIBSYSCALLS_TYPE_INT8:
		description_out->width_in_bits = 8;
		goto not_os_dependent;
	case LIBSYSCALLS_TYPE_INT16:
		description_out->width_in_bits = 16;
		goto not_os_dependent;
	case LIBSYSCALLS_TYPE_INT32:
		description_out->width_in_bits = 32;
		goto not_os_dependent;
	case LIBSYSCALLS_TYPE_INT64:
		description_out->width_in_bits = 64;
		goto not_os_dependent;
	case LIBSYSCALLS_TYPE_INTPTR:
	case LIBSYSCALLS_TYPE_PTRDIFF:
#define CASE(ARCH, CHARBITS, INTPTR_BITS, SIZE_BITS, INTMAX_ALIGN, ENDIAN, SIGN)\
		case ARCH: description_out->width_in_bits = INTPTR_BITS; break
		switch ((int)arch) {
		LIST_ARCH_SPECS(CASE, ;);
		default:
			abort();
		}
		goto not_os_dependent;
#undef CASE
	case LIBSYSCALLS_TYPE_SSIZE:
#define CASE(ARCH, CHARBITS, INTPTR_BITS, SIZE_BITS, INTMAX_ALIGN, ENDIAN, SIGN)\
		case ARCH: description_out->width_in_bits = SIZE_BITS; break
		switch ((int)arch) {
		LIST_ARCH_SPECS(CASE, ;);
		default:
			abort();
		}
		goto not_os_dependent;
#undef CASE
	default:
		break;
	}

os_dependent:
#define CASE(UPPERCASE, LOWERCASE)\
	case LIBSYSCALLS_OS_##UPPERCASE:\
		r = get_##LOWERCASE##_datatype_description(arch, &datatype, description_out,\
		                                           &divide_array_size_with_type_size_out);\
		break

	switch ((int)os) {
	LIST_OSES(CASE, ;);
	default:
		return LIBSYSCALLS_E_OSNOSUP;
	}
	if (r)
		return r;
#undef CASE

	switch ((int)datatype) {
	case LIBSYSCALLS_TYPE_INT8:
	case LIBSYSCALLS_TYPE_INT16:
	case LIBSYSCALLS_TYPE_INT32:
	case LIBSYSCALLS_TYPE_INT64:
	case LIBSYSCALLS_TYPE_INTPTR:
	case LIBSYSCALLS_TYPE_PTRDIFF:
	case LIBSYSCALLS_TYPE_SSIZE:
		goto arch_dependent;
	default:
		break;
	}

not_os_dependent:
	if (divide_array_size_with_type_size_out)
		description_out->array_size /= description_out->width_in_bits;

#define CASE(ARCH, CHARBITS, INTPTR_BITS, SIZE_BITS, INTMAX_ALIGN, ENDIAN, SIGN)\
	case ARCH: charbits = CHARBITS; endian = ENDIAN; break
	switch ((int)arch) {
	LIST_ARCH_SPECS(CASE, ;);
	default:
		abort();
	}
#undef CASE

	if (description_out->width_in_bits % charbits)
		return LIBSYSCALLS_E_NOSUCHTYPE;

	if (description_out->width_in_bits > (size_t)1 << LIBSYSCALLS_BITSIZEOF_(*description_out->byteorder) ||
	    description_out->width_in_bits > ELEMSOF(description_out->byteorder) * charbits) {
		abort();
	}

#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wimplicit-int-conversion" /* we don't want to hardcode the type here */
#elif defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wconversion"
#endif

	switch (endian) {
	case Big:
		for (i = 0, j = description_out->width_in_bits; j;)
			description_out->byteorder[i++] = j -= charbits;
		break;
	case Little:
		for (i = 0, j = 0; j < description_out->width_in_bits; j += charbits)
			description_out->byteorder[i++] = j;
		break;
	default:
		abort();
	}

#if defined(__clang__)
# pragma clang diagnostic pop
#elif defined(__GNUC__)
# pragma GCC diagnostic pop
#endif

	if (half) {
		unsigned long long int bytemask, coverage = 0;
		unsigned long long int remmask;
		unsigned long long int bytebits;

		bytebits = (unsigned long long int)larger_type.width_in_bits;
		for (i = 0; !LIBSYSCALLS_IS_BYTEORDER_END_AT(&larger_type, i); i++)
			if (larger_type.byteorder[i] && (unsigned long long int)larger_type.byteorder[i] < bytebits)
				bytebits = (unsigned long long int)larger_type.byteorder[i];
		bytemask = (1ULL << bytebits) - 1ULL;
		remmask = 0xFFFFFFFFULL; /* we known from the code above that we are working with 32-bit sections */

		for (i = 0; !LIBSYSCALLS_IS_BYTEORDER_END_AT(&larger_type, i); i++) {
			coverage |= (remmask & bytemask) << larger_type.byteorder[i];
			remmask >>= bytebits;
		}

		/* we known from the code above that we are working with split 64-bit integers */
		if (half == +1)
			coverage ^= 0xFFFFFFFFFFFFFFFFULL;
#define CASE(SECTION, PAT16_FIRST, PAT16, PAT32_FIRST, PAT32, PAT64_FIRST, PAT64)\
		if (PAT64_FIRST && LIBSYSCALLS_GET_SECTION_FRACTION(SECTION) && coverage == PAT64##ULL)\
			description_out->section = SECTION
		LIBSYSCALLS_LIST_SECTIONS(CASE, ; else); else abort();
#undef CASE
	}

	return LIBSYSCALLS_E_OK;
}