aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/target.c
blob: 857ae367e5552430461d958d3dd2a8f9e7e0fd56 (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
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
#include <stdio.h>
#include <string.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_ALPHA] =		&target_alpha,
	[MACH_ARM] =		&target_arm,
	[MACH_ARM64] =		&target_arm64,
	[MACH_BFIN] =		&target_bfin,
	[MACH_H8300] =		&target_h8300,
	[MACH_I386] =		&target_i386,
	[MACH_M68K] =		&target_m68k,
	[MACH_MICROBLAZE] =	&target_microblaze,
	[MACH_MIPS32] =		&target_mips32,
	[MACH_MIPS64] =		&target_mips64,
	[MACH_NDS32] =		&target_nds32,
	[MACH_NIOS2] =		&target_nios2,
	[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_SH] =		&target_sh,
	[MACH_SPARC32] =	&target_sparc32,
	[MACH_SPARC64] =	&target_sparc64,
	[MACH_X86_64] =		&target_x86_64,
	[MACH_XTENSA] =		&target_xtensa,
	[MACH_UNKNOWN] =	&target_default,
};
const struct target *arch_target = &target_default;

enum machine target_parse(const char *name)
{
	static const struct arch {
		const char *name;
		enum machine mach;
		char bits;
	} archs[] = {
		{ "alpha",	MACH_ALPHA,	64, },
		{ "aarch64",	MACH_ARM64,	64, },
		{ "arm64",	MACH_ARM64,	64, },
		{ "arm",	MACH_ARM,	32, },
		{ "bfin",	MACH_BFIN,	32, },
		{ "h8300",	MACH_H8300,	32, },
		{ "i386",	MACH_I386,	32, },
		{ "m68k",	MACH_M68K,	32, },
		{ "microblaze",	MACH_MICROBLAZE,32, },
		{ "mips",	MACH_MIPS32,	0,  },
		{ "nds32",	MACH_NDS32,	32, },
		{ "nios2",	MACH_NIOS2,	32, },
		{ "powerpc",	MACH_PPC32,	0,  },
		{ "ppc",	MACH_PPC32,	0,  },
		{ "riscv",	MACH_RISCV32,	0,  },
		{ "s390x",	MACH_S390X,	64, },
		{ "s390",	MACH_S390,	32, },
		{ "sparc",	MACH_SPARC32,	0,  },
		{ "x86_64",	MACH_X86_64,	64, },
		{ "x86-64",	MACH_X86_64,	64, },
		{ "sh",		MACH_SH,	32, },
		{ "xtensa",	MACH_XTENSA,	32, },
		{ NULL },
	};
	const struct arch *p;

	for (p = &archs[0]; p->name; p++) {
		size_t len = strlen(p->name);
		if (strncmp(p->name, name, len) == 0) {
			enum machine mach = p->mach;
			const char *suf = name + len;
			int bits = p->bits;

			if (bits == 0) {
				if (!strcmp(suf, "") || !strcmp(suf, "32")) {
					;
				} else if (!strcmp(suf, "64")) {
					mach += 1;
				} else {
					die("invalid architecture: %s", name);
				}
			} else {
				if (strcmp(suf, ""))
					die("invalid architecture: %s", name);
			}

			return mach;
		}
	}

	return MACH_UNKNOWN;
}


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

	arch_target = target;
	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;

	if (fpie > fpic)
		fpic = fpie;

	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);

	if (arch_msize_long || target->size_t_long) {
		size_t_ctype = &ulong_ctype;
		ssize_t_ctype = &long_ctype;
	}
	if (fshort_wchar)
		wchar_ctype = &ushort_ctype;
}