aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/target.c
blob: f23ed30881bd1755afc799be56697ea3d59d64db (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
#include <stdio.h>

#include "symbol.h"
#include "target.h"
#include "machine.h"

struct symbol *size_t_ctype = &ulong_ctype;
struct symbol *ssize_t_ctype = &long_ctype;
struct symbol *intmax_ctype = &long_ctype;
struct symbol *uintmax_ctype = &ulong_ctype;
struct symbol *int64_ctype = &long_ctype;
struct symbol *uint64_ctype = &ulong_ctype;
struct symbol *int32_ctype = &int_ctype;
struct symbol *uint32_ctype = &uint_ctype;
struct symbol *wchar_ctype = &int_ctype;
struct symbol *wint_ctype = &uint_ctype;

/*
 * For "__attribute__((aligned))"
 */
int max_alignment = 16;

/*
 * Integer data types
 */
int bits_in_bool = 1;
int bits_in_char = 8;
int bits_in_short = 16;
int bits_in_int = 32;
int bits_in_long = 64;
int bits_in_longlong = 64;
int bits_in_longlonglong = 128;

int max_int_alignment = 8;

/*
 * Floating point data types
 */
int bits_in_float = 32;
int bits_in_double = 64;
int bits_in_longdouble = 128;

int max_fp_alignment = 16;

/*
 * Pointer data type
 */
int bits_in_pointer = 64;
int pointer_alignment = 8;

/*
 * Enum data types
 */
int bits_in_enum = 32;
int enum_alignment = 4;


static const struct target *targets[] = {
	[MACH_ARM] =		&target_arm,
	[MACH_ARM64] =		&target_arm64,
	[MACH_I386] =		&target_i386,
	[MACH_X86_64] =		&target_x86_64,
	[MACH_MIPS32] =		&target_mips32,
	[MACH_MIPS64] =		&target_mips64,
	[MACH_PPC32] =		&target_ppc32,
	[MACH_PPC64] =		&target_ppc64,
	[MACH_RISCV32] =	&target_riscv32,
	[MACH_RISCV64] =	&target_riscv64,
	[MACH_S390] =		&target_s390,
	[MACH_S390X] =		&target_s390x,
	[MACH_SPARC32] =	&target_sparc32,
	[MACH_SPARC64] =	&target_sparc64,
	[MACH_M68K] =		&target_m68k,
	[MACH_UNKNOWN] =	&target_default,
};
const struct target *arch_target = &target_default;


void target_config(enum machine mach)
{
	const struct target *target = targets[mach];

	arch_target = target;
	arch_mach = target->mach;
	arch_m64 = target->bitness;
	arch_big_endian = target->big_endian;

	funsigned_char = target->unsigned_char;
}


void target_init(void)
{
	const struct target *target = arch_target;

	switch (arch_m64) {
	case ARCH_LP32:
		max_int_alignment = 4;
		/* fallthrough */
	case ARCH_X32:
		bits_in_long = 32;
		bits_in_pointer = 32;
		pointer_alignment = 4;
		size_t_ctype = &uint_ctype;
		ssize_t_ctype = &int_ctype;
		int64_ctype = &llong_ctype;
		uint64_ctype = &ullong_ctype;
		intmax_ctype = &llong_ctype;
		uintmax_ctype = &ullong_ctype;
		if (target->target_32bit)
			target = target->target_32bit;
		break;

	case ARCH_LLP64:
		bits_in_long = 32;
		size_t_ctype = &ullong_ctype;
		ssize_t_ctype = &llong_ctype;
		int64_ctype = &llong_ctype;
		uint64_ctype = &ullong_ctype;
		intmax_ctype = &llong_ctype;
		uintmax_ctype = &ullong_ctype;
		/* fallthrough */
	case ARCH_LP64:
		if (target->target_64bit)
			target = target->target_64bit;
		break;
	}
	arch_target = target;
	arch_mach = target->mach;

	if (target->wchar)
		wchar_ctype = target->wchar;
	if (target->wint)
		wint_ctype = target->wint;
	if (target->bits_in_longdouble)
		bits_in_longdouble = target->bits_in_longdouble;
	if (target->max_fp_alignment)
		max_fp_alignment = target->max_fp_alignment;

	if (target->init)
		target->init(target);
}