sparc_cconv.c 10.6 KB
Newer Older
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2010 University of Karlsruhe.  All right reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

/**
 * @file
 * @brief   calling convention helpers
 * @author  Matthias Braun
 */
#include "config.h"

27
28
#include "be_t.h"
#include "beirg.h"
29
30
#include "sparc_cconv.h"
#include "irmode.h"
Matthias Braun's avatar
Matthias Braun committed
31
#include "irgwalk.h"
32
33
#include "typerep.h"
#include "xmalloc.h"
34
#include "util.h"
35
#include "error.h"
36
#include "gen_sparc_regalloc_if.h"
37
#include "bitfiddle.h"
38
39
40

static const unsigned ignore_regs[] = {
	REG_G0,
yb9976's avatar
yb9976 committed
41
42
43
	/* used in case an address offset does not fit into an immediate: */
	REG_G4,
	/* reserved for SPARC ABI: */
44
45
46
47
48
49
50
51
52
53
54
55
	REG_G5,
	REG_G6,
	REG_G7,

	REG_SP,
	REG_O7,
	REG_FRAME_POINTER,
	REG_I7,

	REG_FPFLAGS,
	REG_FLAGS,
	REG_Y,
Matthias Braun's avatar
Matthias Braun committed
56
57

	REG_F31,
58
};
59

60
61
62
63
64
65
66
67
static const arch_register_t* const param_regs[] = {
	&sparc_registers[REG_I0],
	&sparc_registers[REG_I1],
	&sparc_registers[REG_I2],
	&sparc_registers[REG_I3],
	&sparc_registers[REG_I4],
	&sparc_registers[REG_I5],
};
Matthias Braun's avatar
Matthias Braun committed
68
COMPILETIME_ASSERT(ARRAY_SIZE(param_regs) == SPARC_N_PARAM_REGS, sparcparamregs)
69
70
71
72
73
74

static const arch_register_t* const float_result_regs[] = {
	&sparc_registers[REG_F0],
	&sparc_registers[REG_F1],
	&sparc_registers[REG_F2],
	&sparc_registers[REG_F3],
75
76
77
78
	&sparc_registers[REG_F4],
	&sparc_registers[REG_F5],
	&sparc_registers[REG_F6],
	&sparc_registers[REG_F7],
79
};
80
81
82
static arch_register_req_t float_result_reqs_double[8];
static arch_register_req_t float_result_reqs_quad[8];

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
static const unsigned caller_saves[] = {
	REG_G1,
	REG_G2,
	REG_G3,
	REG_O0,
	REG_O1,
	REG_O2,
	REG_O3,
	REG_O4,
	REG_O5,
	REG_F0,
	REG_F1,
	REG_F2,
	REG_F3,
	REG_F4,
	REG_F5,
	REG_F6,
	REG_F7,
	REG_F8,
	REG_F9,
	REG_F10,
	REG_F11,
	REG_F12,
	REG_F13,
	REG_F14,
	REG_F15,
	REG_F16,
	REG_F17,
	REG_F18,
	REG_F19,
	REG_F20,
	REG_F21,
	REG_F22,
	REG_F23,
	REG_F24,
	REG_F25,
	REG_F26,
	REG_F27,
	REG_F28,
	REG_F29,
	REG_F30,
124
125
126
	REG_FLAGS,
	REG_FPFLAGS,
	REG_Y,
127
128
};
static unsigned default_caller_saves[BITSET_SIZE_ELEMS(N_SPARC_REGISTERS)];
129

130
131
132
133
134
135
136
static const unsigned returns_twice_saved[] = {
	REG_SP,
	REG_FRAME_POINTER,
	REG_I7
};
static unsigned default_returns_twice_saves[BITSET_SIZE_ELEMS(N_SPARC_REGISTERS)];

Michael Beck's avatar
Michael Beck committed
137
138
139
140
/**
 * Maps an input register representing the i'th register input
 * to the i'th register output.
 */
141
142
static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
{
143
	unsigned idx = reg->global_index;
144
145
146
	assert(REG_I0 <= idx && idx <= REG_I7);
	idx += REG_O0 - REG_I0;
	assert(REG_O0 <= idx && idx <= REG_O7);
147
	return &sparc_registers[idx];
148
149
}

Matthias Braun's avatar
Matthias Braun committed
150
151
152
153
154
155
156
157
158
static void check_omit_fp(ir_node *node, void *env)
{
	/* omit-fp is not possible if:
	 *  - we have allocations on the stack
	 *  - we have calls (with the exception of tail-calls once we support them)
	 */
	if ((is_Alloc(node) && get_Alloc_where(node) == stack_alloc)
			|| (is_Free(node) && get_Free_where(node) == stack_alloc)
			|| is_Call(node)) {
Matthias Braun's avatar
Matthias Braun committed
159
		bool *can_omit_fp = (bool*) env;
Matthias Braun's avatar
Matthias Braun committed
160
161
162
163
		*can_omit_fp = false;
	}
}

164
165
166
167
168
169
170
171
172
173
174
static unsigned determine_n_float_regs(ir_mode *mode)
{
	unsigned bits = get_mode_size_bits(mode);
	switch (bits) {
	case 32:
		return 1;
	case 64:
		return 2;
	case 128:
		return 4;
	default:
175
		panic("Unexpected floatingpoint mode %+F", mode);
176
177
178
	}
}

179
calling_convention_t *sparc_decide_calling_convention(ir_type *function_type,
Matthias Braun's avatar
Matthias Braun committed
180
                                                      ir_graph *irg)
181
{
Matthias Braun's avatar
Matthias Braun committed
182
	bool omit_fp = false;
Matthias Braun's avatar
Matthias Braun committed
183
	if (irg != NULL) {
184
		omit_fp = be_options.omit_fp;
185
186
187
188
189
190
191
		/* our current vaarg handling needs the standard space to store the
		 * args 0-5 in it */
		if (get_method_variadicity(function_type) == variadicity_variadic)
			omit_fp = false;
		if (omit_fp == true) {
			irg_walk_graph(irg, check_omit_fp, NULL, &omit_fp);
		}
Matthias Braun's avatar
Matthias Braun committed
192
	}
193

Matthias Braun's avatar
Matthias Braun committed
194
195
196
	mtp_additional_properties mtp
		= get_method_additional_properties(function_type);
	unsigned *caller_saves = rbitset_malloc(N_SPARC_REGISTERS);
197
198
199
200
201
202
	if (mtp & mtp_property_returns_twice) {
		rbitset_copy(caller_saves, default_returns_twice_saves,
		             N_SPARC_REGISTERS);
	} else {
		rbitset_copy(caller_saves, default_caller_saves, N_SPARC_REGISTERS);
	}
203

204
	/* determine how parameters are passed */
Matthias Braun's avatar
Matthias Braun committed
205
206
207
	int                 n_params = get_method_n_params(function_type);
	int                 regnum   = 0;
	reg_or_stackslot_t *params   = XMALLOCNZ(reg_or_stackslot_t, n_params);
208

Matthias Braun's avatar
Matthias Braun committed
209
210
211
	int      n_param_regs = ARRAY_SIZE(param_regs);
	unsigned stack_offset = 0;
	for (int i = 0; i < n_params; ++i) {
212
		ir_type            *param_type = get_method_param_type(function_type,i);
213
214
215
216
217
		ir_mode            *mode;
		int                 bits;
		reg_or_stackslot_t *param;

		if (is_compound_type(param_type))
218
			panic("compound arguments not supported yet");
219
220
221
222

		mode  = get_type_mode(param_type);
		bits  = get_mode_size_bits(mode);
		param = &params[i];
223

224
225
		if (i == 0 &&
		    (get_method_calling_convention(function_type) & cc_compound_ret)) {
226
227
228
229
230
231
232
233
			assert(mode_is_reference(mode) && bits == 32);
			/* special case, we have reserved space for this on the between
			 * type */
			param->type   = param_type;
			param->offset = -SPARC_MIN_STACKSIZE+SPARC_AGGREGATE_RETURN_OFFSET;
			continue;
		}

234
		if (regnum < n_param_regs) {
235
			const arch_register_t *reg = param_regs[regnum];
Matthias Braun's avatar
Matthias Braun committed
236
			if (irg == NULL || omit_fp)
237
				reg = map_i_to_o_reg(reg);
238
			param->reg0       = reg;
239
			param->req0       = reg->single_req;
240
241
			param->reg_offset = regnum;
			++regnum;
242
		} else {
243
			param->type   = param_type;
244
			param->offset = stack_offset;
245
246
			/* increase offset by at least SPARC_REGISTER_SIZE bytes so everything is aligned */
			stack_offset += bits > 8 * SPARC_REGISTER_SIZE ? bits / 8 : SPARC_REGISTER_SIZE;
247
248
249
250
251
252
			continue;
		}

		/* we might need a 2nd 32bit component (for 64bit or double values) */
		if (bits > 32) {
			if (bits > 64)
253
				panic("only 32 and 64bit modes supported");
254
255

			if (regnum < n_param_regs) {
256
				const arch_register_t *reg = param_regs[regnum];
Matthias Braun's avatar
Matthias Braun committed
257
				if (irg == NULL || omit_fp)
258
					reg = map_i_to_o_reg(reg);
259
				param->reg1       = reg;
260
				param->req1       = reg->single_req;
261
				++regnum;
262
			} else {
263
264
265
266
267
				ir_mode *regmode = param_regs[0]->reg_class->mode;
				ir_type *type    = get_type_for_mode(regmode);
				param->type      = type;
				param->offset    = stack_offset;
				assert(get_mode_size_bits(regmode) == 32);
268
				stack_offset += SPARC_REGISTER_SIZE;
269
270
271
			}
		}
	}
Matthias Braun's avatar
Matthias Braun committed
272
	unsigned n_param_regs_used = regnum;
273
274

	/* determine how results are passed */
Matthias Braun's avatar
Matthias Braun committed
275
276
277
278
279
280
281
	int                 n_results           = get_method_n_ress(function_type);
	unsigned            float_regnum        = 0;
	unsigned            n_reg_results       = 0;
	unsigned            n_float_result_regs = ARRAY_SIZE(float_result_regs);
	reg_or_stackslot_t *results = XMALLOCNZ(reg_or_stackslot_t, n_results);
	regnum        = 0;
	for (int i = 0; i < n_results; ++i) {
282
283
284
285
286
		ir_type            *result_type = get_method_res_type(function_type, i);
		ir_mode            *result_mode = get_type_mode(result_type);
		reg_or_stackslot_t *result      = &results[i];

		if (mode_is_float(result_mode)) {
287
288
289
290
			unsigned n_regs   = determine_n_float_regs(result_mode);
			unsigned next_reg = round_up2(float_regnum, n_regs);

			if (next_reg >= n_float_result_regs) {
291
				panic("Too many float results");
292
			} else {
293
294
295
296
297
298
299
300
301
302
303
304
305
306
				const arch_register_t *reg = float_result_regs[next_reg];
				rbitset_clear(caller_saves, reg->global_index);
				result->reg_offset = i;
				if (n_regs == 1) {
					result->req0 = reg->single_req;
				} else if (n_regs == 2) {
					result->req0 = &float_result_reqs_double[next_reg];
					rbitset_clear(caller_saves, reg->global_index+1);
				} else if (n_regs == 4) {
					result->req0 = &float_result_reqs_quad[next_reg];
					rbitset_clear(caller_saves, reg->global_index+1);
					rbitset_clear(caller_saves, reg->global_index+2);
					rbitset_clear(caller_saves, reg->global_index+3);
				} else {
307
					panic("invalid number of registers in result");
308
309
310
311
				}
				float_regnum = next_reg + n_regs;

				++n_reg_results;
312
313
314
			}
		} else {
			if (get_mode_size_bits(result_mode) > 32) {
315
				panic("Results with more than 32bits not supported yet");
316
317
318
			}

			if (regnum >= n_param_regs) {
319
				panic("Too many results");
320
321
			} else {
				const arch_register_t *reg = param_regs[regnum++];
Matthias Braun's avatar
Matthias Braun committed
322
				if (irg == NULL || omit_fp)
323
					reg = map_i_to_o_reg(reg);
324
325
326
327
				result->req0       = reg->single_req;
				result->reg_offset = i;
				rbitset_clear(caller_saves, reg->global_index);
				++n_reg_results;
328
329
330
331
			}
		}
	}

Matthias Braun's avatar
Matthias Braun committed
332
	calling_convention_t *cconv = XMALLOCZ(calling_convention_t);
333
334
	cconv->parameters       = params;
	cconv->param_stack_size = stack_offset;
335
	cconv->n_param_regs     = n_param_regs_used;
336
	cconv->results          = results;
Matthias Braun's avatar
Matthias Braun committed
337
	cconv->omit_fp          = omit_fp;
338
339
	cconv->caller_saves     = caller_saves;
	cconv->n_reg_results    = n_reg_results;
340

341
342
343
344
345
346
347
348
349
350
351
352
353
354
	/* setup ignore register array */
	if (irg != NULL) {
		be_irg_t       *birg      = be_birg_from_irg(irg);
		size_t          n_ignores = ARRAY_SIZE(ignore_regs);
		struct obstack *obst      = &birg->obst;
		size_t          r;

		birg->allocatable_regs = rbitset_obstack_alloc(obst, N_SPARC_REGISTERS);
		rbitset_set_all(birg->allocatable_regs, N_SPARC_REGISTERS);
		for (r = 0; r < n_ignores; ++r) {
			rbitset_clear(birg->allocatable_regs, ignore_regs[r]);
		}
	}

355
356
357
358
359
360
361
	return cconv;
}

void sparc_free_calling_convention(calling_convention_t *cconv)
{
	free(cconv->parameters);
	free(cconv->results);
362
	free(cconv->caller_saves);
363
364
	free(cconv);
}
365
366
367

void sparc_cconv_init(void)
{
Matthias Braun's avatar
Matthias Braun committed
368
	for (size_t i = 0; i < ARRAY_SIZE(caller_saves); ++i) {
369
370
371
372
		rbitset_set(default_caller_saves, caller_saves[i]);
	}

	rbitset_set_all(default_returns_twice_saves, N_SPARC_REGISTERS);
Matthias Braun's avatar
Matthias Braun committed
373
	for (size_t i = 0; i < ARRAY_SIZE(returns_twice_saved); ++i) {
374
375
		rbitset_clear(default_returns_twice_saves, returns_twice_saved[i]);
	}
Matthias Braun's avatar
Matthias Braun committed
376
	for (size_t i = 0; i < ARRAY_SIZE(ignore_regs); ++i) {
377
		rbitset_clear(default_returns_twice_saves, ignore_regs[i]);
378
379
	}

Matthias Braun's avatar
Matthias Braun committed
380
	for (size_t i = 0; i < ARRAY_SIZE(float_result_reqs_double); i += 2) {
381
382
383
384
385
		arch_register_req_t *req = &float_result_reqs_double[i];
		*req = *float_result_regs[i]->single_req;
		req->type |= arch_register_req_type_aligned;
		req->width = 2;
	}
Matthias Braun's avatar
Matthias Braun committed
386
	for (size_t i = 0; i < ARRAY_SIZE(float_result_reqs_quad); i += 4) {
387
388
389
390
391
392
		arch_register_req_t *req = &float_result_reqs_quad[i];
		*req = *float_result_regs[i]->single_req;
		req->type |= arch_register_req_type_aligned;
		req->width = 4;
	}
}