beabi.c 57.2 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
2
 * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * 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.
Christian Würdig's avatar
Christian Würdig committed
18
19
20
21
22
 */

/**
 * @file
 * @brief       Backend ABI implementation.
23
 * @author      Sebastian Hack, Michael Beck
Sebastian Hack's avatar
Sebastian Hack committed
24
 */
Matthias Braun's avatar
Matthias Braun committed
25
#include "config.h"
26

Sebastian Hack's avatar
Sebastian Hack committed
27
28
#include "obst.h"

29
#include "irgopt.h"
Sebastian Hack's avatar
Sebastian Hack committed
30
31
32
33
34
35
36

#include "irgraph_t.h"
#include "irnode_t.h"
#include "ircons_t.h"
#include "iredges_t.h"
#include "irgmod.h"
#include "irgwalk.h"
Sebastian Hack's avatar
Sebastian Hack committed
37
#include "irprintf_t.h"
38
#include "irgopt.h"
39
#include "iropt_t.h"
40
#include "irtools.h"
41
#include "heights.h"
42
#include "pdeq.h"
43
#include "util.h"
Matthias Braun's avatar
Matthias Braun committed
44
#include "raw_bitset.h"
45
#include "error.h"
46
#include "pset_new.h"
Sebastian Hack's avatar
Sebastian Hack committed
47
48
49

#include "be.h"
#include "beabi.h"
50
#include "bearch.h"
51
#include "benode.h"
Sebastian Hack's avatar
Sebastian Hack committed
52
#include "belive_t.h"
53
54
#include "besched.h"
#include "beirg.h"
55
#include "bessaconstr.h"
56
#include "bemodule.h"
57
#include "betranshlp.h"
58
59

DEBUG_ONLY(static firm_dbg_module_t *dbg;)
Sebastian Hack's avatar
Sebastian Hack committed
60

61
typedef struct be_abi_call_arg_t {
62
63
	unsigned is_res   : 1;  /**< 1: the call argument is a return value. 0: it's a call parameter. */
	unsigned in_reg   : 1;  /**< 1: this argument is transmitted in registers. */
64
	unsigned on_stack : 1;  /**< 1: this argument is transmitted on the stack. */
65
	unsigned callee   : 1;  /**< 1: someone called us. 0: We call another function */
Sebastian Hack's avatar
Sebastian Hack committed
66

67
	int                    pos;
Sebastian Hack's avatar
Sebastian Hack committed
68
	const arch_register_t *reg;
69
70
71
72
73
	ir_entity             *stack_ent;
	ir_mode               *load_mode;
	unsigned               alignment;    /**< stack alignment */
	unsigned               space_before; /**< allocate space before */
	unsigned               space_after;  /**< allocate space after */
Sebastian Hack's avatar
Sebastian Hack committed
74
75
} be_abi_call_arg_t;

76
struct be_abi_call_t {
Michael Beck's avatar
Michael Beck committed
77
78
	be_abi_call_flags_t          flags;  /**< Flags describing the ABI behavior on calls */
	int                          pop;    /**< number of bytes the stack frame is shrinked by the callee on return. */
79
80
81
	const be_abi_callbacks_t    *cb;
	ir_type                     *between_type;
	set                         *params;
Michael Beck's avatar
Michael Beck committed
82
	const arch_register_class_t *cls_addr; /**< register class of the call address */
Sebastian Hack's avatar
Sebastian Hack committed
83
84
};

Michael Beck's avatar
Michael Beck committed
85
/**
86
 * The ABI information for the current graph.
Michael Beck's avatar
Michael Beck committed
87
 */
88
struct be_abi_irg_t {
Sebastian Hack's avatar
Sebastian Hack committed
89
	be_abi_call_t        *call;         /**< The ABI call information. */
Sebastian Hack's avatar
Sebastian Hack committed
90

Sebastian Hack's avatar
Sebastian Hack committed
91
	ir_node              *init_sp;      /**< The node representing the stack pointer
92
	                                         at the start of the function. */
Sebastian Hack's avatar
Sebastian Hack committed
93

94
	ir_node              *start;        /**< The be_Start params node. */
Sebastian Hack's avatar
Sebastian Hack committed
95
	pmap                 *regs;         /**< A map of all callee-save and ignore regs to
96
97
	                                         their Projs to the RegParams node. */
	int                  start_block_bias; /**< The stack bias at the end of the start block. */
Sebastian Hack's avatar
Sebastian Hack committed
98

Sebastian Hack's avatar
Sebastian Hack committed
99
100
	pmap                 *keep_map;     /**< mapping blocks to keep nodes. */

101
	ir_node              **calls;       /**< flexible array containing all be_Call nodes */
Sebastian Hack's avatar
Sebastian Hack committed
102
};
Sebastian Hack's avatar
Sebastian Hack committed
103

104
static ir_heights_t *ir_heights;
105

106
/** Flag: if set, try to omit the frame pointer in all routines. */
107
static int be_omit_fp = 1;
108

Matthias Braun's avatar
Matthias Braun committed
109
110
111
112
113
114
115
116
117
118
119
static ir_node *be_abi_reg_map_get(pmap *map, const arch_register_t *reg)
{
	return (ir_node*)pmap_get(map, reg);
}

static void be_abi_reg_map_set(pmap *map, const arch_register_t* reg,
                               ir_node *node)
{
	pmap_insert(map, reg, node);
}

120
/**
Christoph Mallon's avatar
Christoph Mallon committed
121
 * Check if the given register is callee save, ie. will be saved by the callee.
122
123
124
125
126
127
128
129
130
131
132
 */
static bool arch_register_is_callee_save(
	const arch_env_t      *arch_env,
	const arch_register_t *reg)
{
	if (arch_env->impl->register_saved_by)
		return arch_env->impl->register_saved_by(reg, /*callee=*/1);
	return false;
}

/**
Christoph Mallon's avatar
Christoph Mallon committed
133
 * Check if the given register is caller save, ie. must be saved by the caller.
134
135
136
137
138
139
140
141
142
143
144
145
 */
static bool arch_register_is_caller_save(
	const arch_env_t      *arch_env,
	const arch_register_t *reg)
{
	if (arch_env->impl->register_saved_by)
		return arch_env->impl->register_saved_by(reg, /*callee=*/0);
	return false;
}



Sebastian Hack's avatar
Sebastian Hack committed
146
147
148
149
150
151
/*
     _    ____ ___    ____      _ _ _                _
    / \  | __ )_ _|  / ___|__ _| | | |__   __ _  ___| | _____
   / _ \ |  _ \| |  | |   / _` | | | '_ \ / _` |/ __| |/ / __|
  / ___ \| |_) | |  | |__| (_| | | | |_) | (_| | (__|   <\__ \
 /_/   \_\____/___|  \____\__,_|_|_|_.__/ \__,_|\___|_|\_\___/
152

Sebastian Hack's avatar
Sebastian Hack committed
153
154
155
  These callbacks are used by the backend to set the parameters
  for a specific call type.
*/
Sebastian Hack's avatar
Sebastian Hack committed
156

Michael Beck's avatar
Michael Beck committed
157
158
159
/**
 * Set compare function: compares two ABI call object arguments.
 */
Sebastian Hack's avatar
Sebastian Hack committed
160
161
static int cmp_call_arg(const void *a, const void *b, size_t n)
{
162
163
	const be_abi_call_arg_t *p = (const be_abi_call_arg_t*)a;
	const be_abi_call_arg_t *q = (const be_abi_call_arg_t*)b;
164
	(void) n;
165
	return !(p->is_res == q->is_res && p->pos == q->pos && p->callee == q->callee);
Sebastian Hack's avatar
Sebastian Hack committed
166
167
}

Michael Beck's avatar
Michael Beck committed
168
/**
Michael Beck's avatar
Michael Beck committed
169
 * Get  an ABI call object argument.
Michael Beck's avatar
Michael Beck committed
170
171
172
173
 *
 * @param call      the abi call
 * @param is_res    true for call results, false for call arguments
 * @param pos       position of the argument
174
 * @param callee    context type - if we are callee or caller
Michael Beck's avatar
Michael Beck committed
175
 */
176
static be_abi_call_arg_t *get_call_arg(be_abi_call_t *call, int is_res, int pos, int callee)
Sebastian Hack's avatar
Sebastian Hack committed
177
178
179
180
{
	be_abi_call_arg_t arg;
	unsigned hash;

181
	memset(&arg, 0, sizeof(arg));
Sebastian Hack's avatar
Sebastian Hack committed
182
183
	arg.is_res = is_res;
	arg.pos    = pos;
184
	arg.callee = callee;
Sebastian Hack's avatar
Sebastian Hack committed
185

Michael Beck's avatar
Michael Beck committed
186
	hash = is_res * 128 + pos;
Sebastian Hack's avatar
Sebastian Hack committed
187

188
	return (be_abi_call_arg_t*)set_find(call->params, &arg, sizeof(arg), hash);
Sebastian Hack's avatar
Sebastian Hack committed
189
190
}

Michael Beck's avatar
Michael Beck committed
191
/**
Michael Beck's avatar
Michael Beck committed
192
 * Set an ABI call object argument.
Michael Beck's avatar
Michael Beck committed
193
 */
194
static void remember_call_arg(be_abi_call_arg_t *arg, be_abi_call_t *call, be_abi_context_t context)
Sebastian Hack's avatar
Sebastian Hack committed
195
{
196
197
198
199
200
201
202
203
204
	unsigned hash = arg->is_res * 128 + arg->pos;
	if (context & ABI_CONTEXT_CALLEE) {
		arg->callee = 1;
		set_insert(call->params, arg, sizeof(*arg), hash);
	}
	if (context & ABI_CONTEXT_CALLER) {
		arg->callee = 0;
		set_insert(call->params, arg, sizeof(*arg), hash);
	}
Sebastian Hack's avatar
Sebastian Hack committed
205
206
}

Michael Beck's avatar
Michael Beck committed
207
/* Set the flags for a call. */
208
void be_abi_call_set_flags(be_abi_call_t *call, be_abi_call_flags_t flags, const be_abi_callbacks_t *cb)
Sebastian Hack's avatar
Sebastian Hack committed
209
{
210
211
	call->flags = flags;
	call->cb    = cb;
Sebastian Hack's avatar
Sebastian Hack committed
212
213
}

Michael Beck's avatar
Michael Beck committed
214
/* Sets the number of bytes the stackframe is shrinked by the callee on return */
215
216
217
218
219
void be_abi_call_set_pop(be_abi_call_t *call, int pop)
{
	assert(pop >= 0);
	call->pop = pop;
}
220
221
222
223
224
225
226
227

/* Set register class for call address */
void be_abi_call_set_call_address_reg_class(be_abi_call_t *call, const arch_register_class_t *cls)
{
	call->cls_addr = cls;
}


228
229
230
231
void be_abi_call_param_stack(be_abi_call_t *call, int arg_pos,
                             ir_mode *load_mode, unsigned alignment,
                             unsigned space_before, unsigned space_after,
                             be_abi_context_t context)
Sebastian Hack's avatar
Sebastian Hack committed
232
{
233
234
	be_abi_call_arg_t arg;
	memset(&arg, 0, sizeof(arg));
235
	assert(alignment > 0 && "Alignment must be greater than 0");
236
237
238
239
240
241
242
243
244
	arg.on_stack     = 1;
	arg.load_mode    = load_mode;
	arg.alignment    = alignment;
	arg.space_before = space_before;
	arg.space_after  = space_after;
	arg.is_res       = 0;
	arg.pos          = arg_pos;

	remember_call_arg(&arg, call, context);
Sebastian Hack's avatar
Sebastian Hack committed
245
246
}

247
void be_abi_call_param_reg(be_abi_call_t *call, int arg_pos, const arch_register_t *reg, be_abi_context_t context)
Sebastian Hack's avatar
Sebastian Hack committed
248
{
249
250
251
252
253
254
255
256
257
	be_abi_call_arg_t arg;
	memset(&arg, 0, sizeof(arg));

	arg.in_reg = 1;
	arg.reg    = reg;
	arg.is_res = 0;
	arg.pos    = arg_pos;

	remember_call_arg(&arg, call, context);
Sebastian Hack's avatar
Sebastian Hack committed
258
259
}

260
void be_abi_call_res_reg(be_abi_call_t *call, int arg_pos, const arch_register_t *reg, be_abi_context_t context)
Sebastian Hack's avatar
Sebastian Hack committed
261
{
262
263
264
265
266
267
268
269
270
	be_abi_call_arg_t arg;
	memset(&arg, 0, sizeof(arg));

	arg.in_reg = 1;
	arg.reg    = reg;
	arg.is_res = 1;
	arg.pos    = arg_pos;

	remember_call_arg(&arg, call, context);
Sebastian Hack's avatar
Sebastian Hack committed
271
272
}

Michael Beck's avatar
Michael Beck committed
273
/* Get the flags of a ABI call object. */
274
275
276
277
278
be_abi_call_flags_t be_abi_call_get_flags(const be_abi_call_t *call)
{
	return call->flags;
}

Michael Beck's avatar
Michael Beck committed
279
280
281
/**
 * Constructor for a new ABI call object.
 *
Michael Beck's avatar
Michael Beck committed
282
283
 * @param cls_addr  register class of the call address
 *
Michael Beck's avatar
Michael Beck committed
284
285
 * @return the new ABI call object
 */
286
static be_abi_call_t *be_abi_call_new(const arch_register_class_t *cls_addr)
Sebastian Hack's avatar
Sebastian Hack committed
287
{
288
	be_abi_call_t *call = XMALLOCZ(be_abi_call_t);
289

Sebastian Hack's avatar
Sebastian Hack committed
290
291
292
	call->flags.val  = 0;
	call->params     = new_set(cmp_call_arg, 16);
	call->cb         = NULL;
293
	call->cls_addr   = cls_addr;
294

295
	call->flags.bits.try_omit_fp = be_omit_fp;
296

Sebastian Hack's avatar
Sebastian Hack committed
297
298
299
	return call;
}

Michael Beck's avatar
Michael Beck committed
300
301
302
303
/**
 * Destructor for an ABI call object.
 */
static void be_abi_call_free(be_abi_call_t *call)
Sebastian Hack's avatar
Sebastian Hack committed
304
305
306
307
308
{
	del_set(call->params);
	free(call);
}

309
310
311
312
313
314
315
316
317
318
319
/**
 * Initializes the frame layout from parts
 *
 * @param frame     the stack layout that will be initialized
 * @param args      the stack argument layout type
 * @param between   the between layout type
 * @param locals    the method frame type
 *
 * @return the initialized stack layout
 */
static be_stack_layout_t *stack_frame_init(be_stack_layout_t *frame, ir_type *args,
320
                                           ir_type *between, ir_type *locals)
321
322
323
324
325
{
	frame->arg_type       = args;
	frame->between_type   = between;
	frame->frame_type     = locals;
	frame->initial_offset = 0;
Matthias Braun's avatar
Matthias Braun committed
326
	frame->initial_bias   = 0;
327
328
	frame->order[1]       = between;

329
330
331
332
	/* typical decreasing stack: locals have the
	 * lowest addresses, arguments the highest */
	frame->order[0] = locals;
	frame->order[2] = args;
333
334
335
	return frame;
}

336
337
338
339
340
341
342
343
344
345
346
/*
   ____      _ _
  / ___|__ _| | |___
 | |   / _` | | / __|
 | |__| (_| | | \__ \
  \____\__,_|_|_|___/

  Adjustment of the calls inside a graph.

*/

Sebastian Hack's avatar
Sebastian Hack committed
347
/**
348
349
 * Transform a call node into a be_Call node.
 *
Sebastian Hack's avatar
Sebastian Hack committed
350
 * @param env The ABI environment for the current irg.
351
352
353
 * @param irn The call node.
 * @param curr_sp The stack pointer node to use.
 * @return The stack pointer after the call.
Sebastian Hack's avatar
Sebastian Hack committed
354
 */
Matthias Braun's avatar
Matthias Braun committed
355
static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
Sebastian Hack's avatar
Sebastian Hack committed
356
{
357
358
	ir_graph *irg              = get_irn_irg(irn);
	const arch_env_t *arch_env = be_get_irg_arch_env(irg);
359
	ir_type *call_tp           = get_Call_type(irn);
360
	ir_node *call_ptr          = get_Call_ptr(irn);
361
	size_t   n_params          = get_method_n_params(call_tp);
362
363
364
	ir_node *curr_mem          = get_Call_mem(irn);
	ir_node *bl                = get_nodes_block(irn);
	int stack_size             = 0;
365
	const arch_register_t *sp  = arch_env->sp;
366
367
	be_abi_call_t *call        = be_abi_call_new(sp->reg_class);
	ir_mode *mach_mode         = sp->reg_class->mode;
368
	int n_res                  = get_method_n_ress(call_tp);
369
370
371
372
373

	ir_node *res_proj  = NULL;
	int n_reg_params   = 0;
	int n_stack_params = 0;
	int n_ins;
Sebastian Hack's avatar
Sebastian Hack committed
374

375
376
	const arch_register_t **states = NEW_ARR_F(const arch_register_t*, 0);
	const arch_register_t **destroyed_regs = NEW_ARR_F(const arch_register_t*, 0);
377
378
379
380
381
382
383
	ir_node                *low_call;
	ir_node               **in;
	ir_node               **res_projs;
	int                     n_reg_results = 0;
	const ir_edge_t        *edge;
	int                    *reg_param_idxs;
	int                    *stack_param_idx;
384
	int                     i, n;
385
	int                     throws_exception;
Matthias Braun's avatar
Matthias Braun committed
386
	size_t                  s;
387
	size_t                  p;
388
	dbg_info               *dbgi;
Sebastian Hack's avatar
Sebastian Hack committed
389
390

	/* Let the isa fill out the abi description for that call node. */
391
	arch_env_get_call_abi(arch_env, call_tp, call);
Sebastian Hack's avatar
Sebastian Hack committed
392
393

	/* Insert code to put the stack arguments on the stack. */
394
	assert(get_Call_n_params(irn) == n_params);
395
	stack_param_idx = ALLOCAN(int, n_params);
396
397
	for (p = 0; p < n_params; ++p) {
		be_abi_call_arg_t *arg = get_call_arg(call, 0, p, 0);
398
		assert(arg);
Michael Beck's avatar
Michael Beck committed
399
		if (arg->on_stack) {
400
			int arg_size = get_type_size_bytes(get_method_param_type(call_tp, p));
Michael Beck's avatar
Michael Beck committed
401
402
403
404

			stack_size += round_up2(arg->space_before, arg->alignment);
			stack_size += round_up2(arg_size, arg->alignment);
			stack_size += round_up2(arg->space_after, arg->alignment);
405

406
			stack_param_idx[n_stack_params++] = p;
Sebastian Hack's avatar
Sebastian Hack committed
407
408
409
410
		}
	}

	/* Collect all arguments which are passed in registers. */
411
	reg_param_idxs = ALLOCAN(int, n_params);
412
413
	for (p = 0; p < n_params; ++p) {
		be_abi_call_arg_t *arg = get_call_arg(call, 0, p, 0);
414
		if (arg && arg->in_reg) {
415
			reg_param_idxs[n_reg_params++] = p;
Sebastian Hack's avatar
Sebastian Hack committed
416
417
418
		}
	}

419
420
421
422
423
424
425
426
427
	/*
	 * If the stack is decreasing and we do not want to store sequentially,
	 * or someone else allocated the call frame
	 * we allocate as much space on the stack all parameters need, by
	 * moving the stack pointer along the stack's direction.
	 *
	 * Note: we also have to do this for stack_size == 0, because we may have
	 * to adjust stack alignment for the call.
	 */
428
	curr_sp = be_new_IncSP(sp, bl, curr_sp, stack_size, 1);
429

Christoph Mallon's avatar
Christoph Mallon committed
430
	dbgi = get_irn_dbg_info(irn);
Sebastian Hack's avatar
Sebastian Hack committed
431
	/* If there are some parameters which shall be passed on the stack. */
432
	if (n_stack_params > 0) {
433
434
435
		int       curr_ofs = 0;
		ir_node **in       = ALLOCAN(ir_node*, n_stack_params+1);
		unsigned  n_in     = 0;
Sebastian Hack's avatar
Sebastian Hack committed
436

437
		curr_mem = get_Call_mem(irn);
438
		in[n_in++] = curr_mem;
439

440
441
		for (i = 0; i < n_stack_params; ++i) {
			int p                  = stack_param_idx[i];
442
			be_abi_call_arg_t *arg = get_call_arg(call, 0, p, 0);
443
444
445
			ir_node *param         = get_Call_param(irn, p);
			ir_node *addr          = curr_sp;
			ir_node *mem           = NULL;
446
			ir_type *param_type    = get_method_param_type(call_tp, p);
447
448
			int param_size         = get_type_size_bytes(param_type) + arg->space_after;

449
450
451
452
453
			/*
			 * If we wanted to build the arguments sequentially,
			 * the stack pointer for the next must be incremented,
			 * and the memory value propagated.
			 */
454
455
456
457
458
459
460
461
			curr_ofs += arg->space_before;
			curr_ofs =  round_up2(curr_ofs, arg->alignment);

			/* Make the expression to compute the argument's offset. */
			if (curr_ofs > 0) {
				ir_mode *constmode = mach_mode;
				if (mode_is_reference(mach_mode)) {
					constmode = mode_Is;
462
				}
463
464
				addr = new_r_Const_long(irg, constmode, curr_ofs);
				addr = new_r_Add(bl, curr_sp, addr, mach_mode);
Sebastian Hack's avatar
Sebastian Hack committed
465
466
467
			}

			/* Insert a store for primitive arguments. */
468
			if (is_atomic_type(param_type)) {
469
				ir_node *nomem     = get_irg_no_mem(irg);
470
				ir_node *mem_input = nomem;
471
				ir_node *store     = new_rd_Store(dbgi, bl, mem_input, addr, param, cons_none);
472
				mem   = new_r_Proj(store, mode_M, pn_Store_M);
473
474
			} else {
				/* Make a mem copy for compound arguments. */
475
476
				ir_node *copy;

Sebastian Hack's avatar
Sebastian Hack committed
477
				assert(mode_is_reference(get_irn_mode(param)));
478
				copy = new_rd_CopyB(dbgi, bl, curr_mem, addr, param, param_type);
479
				mem = new_r_Proj(copy, mode_M, pn_CopyB_M);
Sebastian Hack's avatar
Sebastian Hack committed
480
481
482
483
			}

			curr_ofs += param_size;

484
			in[n_in++] = mem;
Sebastian Hack's avatar
Sebastian Hack committed
485
486
487
		}

		/* We need the sync only, if we didn't build the stores sequentially. */
488
489
490
491
		if (n_stack_params >= 1) {
			curr_mem = new_r_Sync(bl, n_in, in);
		} else {
			curr_mem = get_Call_mem(irn);
492
		}
Sebastian Hack's avatar
Sebastian Hack committed
493
494
	}

495
496
	/* Put caller save into the destroyed set and state registers in the states
	 * set */
497
	for (i = 0, n = arch_env->n_register_classes; i < n; ++i) {
498
		unsigned j;
499
		const arch_register_class_t *cls = &arch_env->register_classes[i];
500
		for (j = 0; j < cls->n_regs; ++j) {
Sebastian Hack's avatar
Sebastian Hack committed
501
			const arch_register_t *reg = arch_register_for_index(cls, j);
502

503
504
505
506
507
			/* even if destroyed all is specified, neither SP nor FP are
			 * destroyed (else bad things will happen) */
			if (reg == arch_env->sp || reg == arch_env->bp)
				continue;

508
			if (reg->type & arch_register_type_state) {
509
510
511
512
513
514
				ARR_APP1(const arch_register_t*, destroyed_regs, reg);
				ARR_APP1(const arch_register_t*, states, reg);
				/* we're already in the destroyed set so no need for further
				 * checking */
				continue;
			}
515
			if (arch_register_is_caller_save(arch_env, reg)) {
516
				if (!(reg->type & arch_register_type_ignore)) {
517
518
					ARR_APP1(const arch_register_t*, destroyed_regs, reg);
				}
519
			}
Sebastian Hack's avatar
Sebastian Hack committed
520
521
		}
	}
Sebastian Hack's avatar
Sebastian Hack committed
522

Christoph Mallon's avatar
Christoph Mallon committed
523
	/* search the largest result proj number */
524
	res_projs = ALLOCANZ(ir_node*, n_res);
Matthias Braun's avatar
Matthias Braun committed
525

Sebastian Hack's avatar
Sebastian Hack committed
526
527
	foreach_out_edge(irn, edge) {
		const ir_edge_t *res_edge;
Matthias Braun's avatar
Matthias Braun committed
528
		ir_node         *irn = get_edge_src_irn(edge);
Sebastian Hack's avatar
Sebastian Hack committed
529

530
		if (!is_Proj(irn) || get_Proj_proj(irn) != pn_Call_T_result)
Matthias Braun's avatar
Matthias Braun committed
531
532
533
534
535
536
537
538
539
540
			continue;

		foreach_out_edge(irn, res_edge) {
			int proj;
			ir_node *res = get_edge_src_irn(res_edge);

			assert(is_Proj(res));

			proj = get_Proj_proj(res);
			assert(proj < n_res);
Matthias Braun's avatar
Matthias Braun committed
541
			assert(res_projs[proj] == NULL);
Matthias Braun's avatar
Matthias Braun committed
542
			res_projs[proj] = res;
Sebastian Hack's avatar
Sebastian Hack committed
543
		}
Matthias Braun's avatar
Matthias Braun committed
544
545
		res_proj = irn;
		break;
Sebastian Hack's avatar
Sebastian Hack committed
546
	}
547

Matthias Braun's avatar
Matthias Braun committed
548
	/** TODO: this is not correct for cases where return values are passed
Christoph Mallon's avatar
Christoph Mallon committed
549
	 * on the stack, but no known ABI does this currently...
Matthias Braun's avatar
Matthias Braun committed
550
551
	 */
	n_reg_results = n_res;
Sebastian Hack's avatar
Sebastian Hack committed
552

553
	n_ins = 0;
554
	in    = ALLOCAN(ir_node*, n_reg_params + ARR_LEN(states));
555

556
	/* make the back end call node and set its register requirements. */
557
	for (i = 0; i < n_reg_params; ++i) {
558
		in[n_ins++] = get_Call_param(irn, reg_param_idxs[i]);
559
	}
560
561

	/* add state registers ins */
Matthias Braun's avatar
Matthias Braun committed
562
563
	for (s = 0; s < ARR_LEN(states); ++s) {
		const arch_register_t       *reg = states[s];
564
565
		const arch_register_class_t *cls = reg->reg_class;
		ir_node *regnode = new_r_Unknown(irg, cls->mode);
566
		in[n_ins++]      = regnode;
567
	}
568
	assert(n_ins == (int) (n_reg_params + ARR_LEN(states)));
569

570
	/* ins collected, build the call */
571
	throws_exception = ir_throws_exception(irn);
572
573
	if (env->call->flags.bits.call_has_imm && is_SymConst(call_ptr)) {
		/* direct call */
574
		low_call = be_new_Call(dbgi, irg, bl, curr_mem, curr_sp, curr_sp,
575
		                       n_reg_results + pn_be_Call_first_res + ARR_LEN(destroyed_regs),
Matthias Braun's avatar
Matthias Braun committed
576
		                       n_ins, in, get_Call_type(irn));
Sebastian Hack's avatar
Sebastian Hack committed
577
		be_Call_set_entity(low_call, get_SymConst_entity(call_ptr));
578
	} else {
579
		/* indirect call */
580
		low_call = be_new_Call(dbgi, irg, bl, curr_mem, curr_sp, call_ptr,
581
		                       n_reg_results + pn_be_Call_first_res + ARR_LEN(destroyed_regs),
582
		                       n_ins, in, get_Call_type(irn));
Sebastian Hack's avatar
Sebastian Hack committed
583
	}
584
	ir_set_throws_exception(low_call, throws_exception);
585
	be_Call_set_pop(low_call, call->pop);
586
587

	/* put the call into the list of all calls for later processing */
588
	ARR_APP1(ir_node *, env->calls, low_call);
589

Matthias Braun's avatar
Matthias Braun committed
590
	/* create new stack pointer */
591
	curr_sp = new_r_Proj(low_call, get_irn_mode(curr_sp), pn_be_Call_sp);
592
593
	be_set_constr_single_reg_out(low_call, pn_be_Call_sp, sp,
			arch_register_req_type_ignore | arch_register_req_type_produces_sp);
594
	arch_set_irn_register(curr_sp, sp);
Matthias Braun's avatar
Matthias Braun committed
595

596
	/* now handle results */
597
	for (i = 0; i < n_res; ++i) {
Matthias Braun's avatar
Matthias Braun committed
598
		ir_node           *proj = res_projs[i];
599
		be_abi_call_arg_t *arg  = get_call_arg(call, 1, i, 0);
600
		long               pn   = i + pn_be_Call_first_res;
Matthias Braun's avatar
Matthias Braun committed
601

Matthias Braun's avatar
Matthias Braun committed
602
603
604
		/* returns values on stack not supported yet */
		assert(arg->in_reg);

Matthias Braun's avatar
Matthias Braun committed
605
606
607
608
609
610
611
		/*
			shift the proj number to the right, since we will drop the
			unspeakable Proj_T from the Call. Therefore, all real argument
			Proj numbers must be increased by pn_be_Call_first_res
		*/
		pn = i + pn_be_Call_first_res;

612
		if (proj == NULL) {
613
			ir_type *res_type = get_method_res_type(call_tp, i);
Matthias Braun's avatar
Matthias Braun committed
614
			ir_mode *mode     = get_type_mode(res_type);
615
			proj              = new_r_Proj(low_call, mode, pn);
Matthias Braun's avatar
Matthias Braun committed
616
617
618
619
620
621
622
			res_projs[i]      = proj;
		} else {
			set_Proj_pred(proj, low_call);
			set_Proj_proj(proj, pn);
		}

		if (arg->in_reg) {
623
			/* remove register from destroyed regs */
624
625
			size_t j;
			size_t n = ARR_LEN(destroyed_regs);
626
627
628
629
630
631
632
			for (j = 0; j < n; ++j) {
				if (destroyed_regs[j] == arg->reg) {
					destroyed_regs[j] = destroyed_regs[n-1];
					ARR_SHRINKLEN(destroyed_regs,n-1);
					break;
				}
			}
Matthias Braun's avatar
Matthias Braun committed
633
634
635
		}
	}

636
	/*
637
638
		Set the register class of the call address to
		the backend provided class (default: stack pointer class)
639
	*/
640
	be_node_set_reg_class_in(low_call, n_be_Call_ptr, call->cls_addr);
641

642
	DBG((dbg, LEVEL_3, "\tcreated backend call %+F\n", low_call));
643

Sebastian Hack's avatar
Sebastian Hack committed
644
	/* Set the register classes and constraints of the Call parameters. */
645
646
	for (i = 0; i < n_reg_params; ++i) {
		int index = reg_param_idxs[i];
647
		be_abi_call_arg_t *arg = get_call_arg(call, 0, index, 0);
Sebastian Hack's avatar
Sebastian Hack committed
648
		assert(arg->reg != NULL);
649

650
		be_set_constr_single_reg_in(low_call, n_be_Call_first_arg + i,
651
		                            arg->reg, arch_register_req_type_none);
Sebastian Hack's avatar
Sebastian Hack committed
652
653
654
	}

	/* Set the register constraints of the results. */
Matthias Braun's avatar
Matthias Braun committed
655
656
	for (i = 0; i < n_res; ++i) {
		ir_node                 *proj = res_projs[i];
657
		const be_abi_call_arg_t *arg  = get_call_arg(call, 1, i, 0);
Matthias Braun's avatar
Matthias Braun committed
658
		int                      pn   = get_Proj_proj(proj);
659

Sebastian Hack's avatar
Sebastian Hack committed
660
		assert(arg->in_reg);
661
662
		be_set_constr_single_reg_out(low_call, pn, arg->reg,
		                             arch_register_req_type_none);
663
		arch_set_irn_register(proj, arg->reg);
Sebastian Hack's avatar
Sebastian Hack committed
664
	}
665
666
	exchange(irn, low_call);

Matthias Braun's avatar
Matthias Braun committed
667
	/* kill the ProjT node */
668
	if (res_proj != NULL) {
669
		kill_node(res_proj);
670
671
	}

Sebastian Hack's avatar
Sebastian Hack committed
672
673
	/* Make additional projs for the caller save registers
	   and the Keep node which keeps them alive. */
674
	{
675
		ir_node               **in, *keep;
676
		int                   i;
Matthias Braun's avatar
Matthias Braun committed
677
		size_t                d;
678
		int                   n = 0;
679
		int                   curr_res_proj = pn_be_Call_first_res + n_reg_results;
680
		int                   n_ins;
Sebastian Hack's avatar
Sebastian Hack committed
681

682
		n_ins = ARR_LEN(destroyed_regs) + n_reg_results + 1;
683
		in    = ALLOCAN(ir_node *, n_ins);
684

685
686
		/* also keep the stack pointer */
		set_irn_link(curr_sp, (void*) sp);
687
		in[n++] = curr_sp;
688

Matthias Braun's avatar
Matthias Braun committed
689
690
		for (d = 0; d < ARR_LEN(destroyed_regs); ++d) {
			const arch_register_t *reg = destroyed_regs[d];
691
			ir_node *proj = new_r_Proj(low_call, reg->reg_class->mode, curr_res_proj);
Sebastian Hack's avatar
Sebastian Hack committed
692

Sebastian Hack's avatar
Sebastian Hack committed
693
			/* memorize the register in the link field. we need afterwards to set the register class of the keep correctly. */
694
695
			be_set_constr_single_reg_out(low_call, curr_res_proj, reg,
			                             arch_register_req_type_none);
696
			arch_set_irn_register(proj, reg);
697

Matthias Braun's avatar
Matthias Braun committed
698
			set_irn_link(proj, (void*) reg);
699
			in[n++] = proj;
700
			++curr_res_proj;
Sebastian Hack's avatar
Sebastian Hack committed
701
702
		}

703
		for (i = 0; i < n_reg_results; ++i) {
Matthias Braun's avatar
Matthias Braun committed
704
			ir_node *proj = res_projs[i];
705
			const arch_register_t *reg = arch_get_irn_register(proj);
Matthias Braun's avatar
Matthias Braun committed
706
			set_irn_link(proj, (void*) reg);
707
			in[n++] = proj;
Matthias Braun's avatar
Matthias Braun committed
708
		}
709
		assert(n <= n_ins);
Matthias Braun's avatar
Matthias Braun committed
710

711
		/* create the Keep for the caller save registers */
712
		keep = be_new_Keep(bl, n, in);
713
		for (i = 0; i < n; ++i) {
714
			const arch_register_t *reg = (const arch_register_t*)get_irn_link(in[i]);
715
			be_node_set_reg_class_in(keep, i, arch_register_get_class(reg));
Sebastian Hack's avatar
Sebastian Hack committed
716
		}
Sebastian Hack's avatar
Sebastian Hack committed
717
718
719
	}

	/* Clean up the stack. */
720
721
722
	assert(stack_size >= call->pop);
	stack_size -= call->pop;

723
	if (stack_size > 0) {
724
725
726
727
		ir_node *mem_proj = NULL;

		foreach_out_edge(low_call, edge) {
			ir_node *irn = get_edge_src_irn(edge);
728
			if (is_Proj(irn) && get_Proj_proj(irn) == pn_Call_M) {
729
730
731
732
				mem_proj = irn;
				break;
			}
		}
Sebastian Hack's avatar
Sebastian Hack committed
733

734
		if (! mem_proj) {
735
			mem_proj = new_r_Proj(low_call, mode_M, pn_be_Call_M);
Sebastian Hack's avatar
Sebastian Hack committed
736
737
			keep_alive(mem_proj);
		}
738
739
	}
	/* Clean up the stack frame or revert alignment fixes if we allocated it */
740
	curr_sp = be_new_IncSP(sp, bl, curr_sp, -stack_size, 0);
Sebastian Hack's avatar
Sebastian Hack committed
741
742

	be_abi_call_free(call);
743

744
745
	DEL_ARR_F(states);
	DEL_ARR_F(destroyed_regs);
746
747
748
749

	return curr_sp;
}

750
751
752
753
754
/**
 * Adjust the size of a node representing a stack alloc or free for the minimum stack alignment.
 *
 * @param alignment  the minimum stack alignment
 * @param size       the node containing the non-aligned size
755
 * @param block      the block where new nodes are allocated on
756
757
758
759
 * @param dbg        debug info for new nodes
 *
 * @return a node representing the aligned size
 */
760
static ir_node *adjust_alloc_size(unsigned stack_alignment, ir_node *size,
761
                                  ir_node *block, dbg_info *dbg)
762
{
763
	if (stack_alignment > 1) {
Matthias Braun's avatar
Matthias Braun committed
764
765
766
767
		ir_mode   *mode;
		ir_tarval *tv;
		ir_node   *mask;
		ir_graph  *irg;
Michael Beck's avatar
Michael Beck committed
768

769
		assert(is_po2(stack_alignment));
770

Michael Beck's avatar
Michael Beck committed
771
772
		mode = get_irn_mode(size);
		tv   = new_tarval_from_long(stack_alignment-1, mode);
773
		irg  = get_Block_irg(block);
774
		mask = new_r_Const(irg, tv);
775
		size = new_rd_Add(dbg, block, size, mask, mode);
776
777

		tv   = new_tarval_from_long(-(long)stack_alignment, mode);
778
		mask = new_r_Const(irg, tv);
779
		size = new_rd_And(dbg, block, size, mask, mode);
780
781
782
	}
	return size;
}
783
784
785
786
/**
 * Adjust an alloca.
 * The alloca is transformed into a back end alloca node and connected to the stack nodes.
 */
Matthias Braun's avatar
Matthias Braun committed
787
static ir_node *adjust_alloc(be_abi_irg_t *env, ir_node *alloc, ir_node *curr_sp)
788
{
789
790
791
792
793
794
795
	ir_node          *block     = get_nodes_block(alloc);
	ir_graph         *irg       = get_Block_irg(block);
	const arch_env_t *arch_env  = be_get_irg_arch_env(irg);
	ir_node          *alloc_mem = NULL;
	ir_node          *alloc_res = NULL;
	ir_type          *type      = get_Alloc_type(alloc);
	dbg_info         *dbg;
796

797
	const ir_edge_t *edge;
798
799
800
801
	ir_node *new_alloc;
	ir_node *count;
	ir_node *size;
	ir_node *ins[2];
802
	unsigned stack_alignment;
803

804
	/* all non-stack Alloc nodes should already be lowered before the backend */
805
	assert(get_Alloc_where(alloc) == stack_alloc);
806

807
808
	foreach_out_edge(alloc, edge) {
		ir_node *irn = get_edge_src_irn(edge);
809

810
		assert(is_Proj(irn));
811
		switch (get_Proj_proj(irn)) {
812
813
814
815
816
817
818
819
		case pn_Alloc_M:
			alloc_mem = irn;
			break;
		case pn_Alloc_res:
			alloc_res = irn;
			break;
		default:
			break;
820
		}
821
	}
822

823
824
825
826
827
828
829
830
	/* Beware: currently Alloc nodes without a result might happen,
	   only escape analysis kills them and this phase runs only for object
	   oriented source. We kill the Alloc here. */
	if (alloc_res == NULL && alloc_mem) {
		exchange(alloc_mem, get_Alloc_mem(alloc));
		return curr_sp;
	}

831
832
	dbg   = get_irn_dbg_info(alloc);
	count = get_Alloc_count(alloc);
833

834
	/* we might need to multiply the count with the element size */
835
	if (!is_unknown_type(type) && get_type_size_bytes(type) != 1) {
Matthias Braun's avatar
Matthias Braun committed
836
837
838
839
840
		ir_mode   *mode  = get_irn_mode(count);
		ir_tarval *tv    = new_tarval_from_long(get_type_size_bytes(type),
		                                        mode);
		ir_node   *cnst = new_rd_Const(dbg, irg, tv);
		size            = new_rd_Mul(dbg, block, count, cnst, mode);
841
842
	} else {
		size = count;
843
	}
844

845
846
847
	/* The stack pointer will be modified in an unknown manner.
	   We cannot omit it. */
	env->call->flags.bits.try_omit_fp = 0;
848

849
	stack_alignment = 1 << arch_env->stack_alignment;
850
	size            = adjust_alloc_size(stack_alignment, size, block, dbg);
851
	new_alloc       = be_new_AddSP(arch_env->sp, block, curr_sp, size);
852
	set_irn_dbg_info(new_alloc, dbg);
853

854
	if (alloc_mem != NULL) {
855
856
		ir_node *addsp_mem;
		ir_node *sync;
857

858
		addsp_mem = new_r_Proj(new_alloc, mode_M, pn_be_AddSP_M);
859

860
861
		/* We need to sync the output mem of the AddSP with the input mem
		   edge into the alloc node. */
862
863
		ins[0] = get_Alloc_mem(alloc);
		ins[1] = addsp_mem;
864
		sync = new_r_Sync(block, 2, ins);
865

866
867
		exchange(alloc_mem, sync);
	}
868

869
	exchange(alloc, new_alloc);