summaryrefslogtreecommitdiffstats
path: root/libsyscalls_get_integer_alignment.c
blob: 7802a06ad25ef2778d4a4c8213994b2829dd3531 (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
/* See LICENSE file for copyright and license details. */
#include "common.h"


#include "generated/integers.c"

enum libsyscalls_error
libsyscalls_get_integer_alignment(enum libsyscalls_os os, enum libsyscalls_arch arch, unsigned width_in_bits, unsigned *alignment_out)
{
	unsigned maxalign;

	if (!width_in_bits)
		return LIBSYSCALLS_E_INVAL;

	switch ((int)arch) {
	case LIBSYSCALLS_ARCH_M68K: /* https://m680x0.github.io/doc/abi.html#scalar-types
	                             * https://lists.nongnu.org/archive/html/qemu-devel/2015-02/msg04797.html */
		maxalign = 16;
		break;

	case LIBSYSCALLS_ARCH_I386:
		maxalign = 32;
		break;

	case LIBSYSCALLS_ARCH_AMD64:
	case LIBSYSCALLS_ARCH_AMD64_X32:
	case LIBSYSCALLS_ARCH_ALPHA: /* https://static.lwn.net/images/pdf/LDD3/ch11.pdf */
	case LIBSYSCALLS_ARCH_IA64: /* https://static.lwn.net/images/pdf/LDD3/ch11.pdf */
	case LIBSYSCALLS_ARCH_MIPS_O32: /* https://static.lwn.net/images/pdf/LDD3/ch11.pdf */
	case LIBSYSCALLS_ARCH_PARISC_32: /* https://www.ece.lsu.edu/ee4720/doc/pa1.1.pdf (page 26) */
	case LIBSYSCALLS_ARCH_PARISC_64:
	case LIBSYSCALLS_ARCH_POWERPC_32: /* https://static.lwn.net/images/pdf/LDD3/ch11.pdf */
	case LIBSYSCALLS_ARCH_SPARC_32: /* https://www.gaisler.com/doc/sparcv8.pdf (page 46) */
	case LIBSYSCALLS_ARCH_SPARC_64: /* https://static.lwn.net/images/pdf/LDD3/ch11.pdf */
	case LIBSYSCALLS_ARCH_XTENSA: /* https://loboris.eu/ESP32/Xtensa_lx%20Overview%20handbook.pdf (page 97) */
		maxalign = 64;
		break;

	default:
		return LIBSYSCALLS_E_ARCHNOSUP;
	}

	if (width_in_bits & (width_in_bits - 1) || width_in_bits & 7 || width_in_bits > 64)
		return LIBSYSCALLS_E_NOSUCHTYPE;
	*alignment_out = width_in_bits < maxalign ? width_in_bits : maxalign;

#define CASE(UPPERCASE, LOWERCASE)\
	case LIBSYSCALLS_OS_##UPPERCASE:\
		return get_##LOWERCASE##_integer_alignment(arch, width_in_bits, alignment_out)

	switch ((int)os) {
	LIST_OSES(CASE, ;);
	default:
		return LIBSYSCALLS_E_OSNOSUP;
	}

#undef CASE
}