beabi.c 66.8 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
Christian Würdig's avatar
Christian Würdig committed
24
 * @version     $Id$
Sebastian Hack's avatar
Sebastian Hack committed
25
 */
Matthias Braun's avatar
Matthias Braun committed
26
#include "config.h"
27

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

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

#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
38
#include "irprintf_t.h"
39
#include "irgopt.h"
40
#include "irbitset.h"
41
#include "iropt_t.h"
42
#include "heights.h"
43
44
#include "pdeq.h"
#include "irtools.h"
Matthias Braun's avatar
Matthias Braun committed
45
#include "raw_bitset.h"
46
#include "error.h"
47
#include "pset_new.h"
Sebastian Hack's avatar
Sebastian Hack committed
48
49
50

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

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

62
typedef struct be_abi_call_arg_t {
63
64
	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. */
65
	unsigned on_stack : 1;  /**< 1: this argument is transmitted on the stack. */
66
	unsigned callee   : 1;  /**< 1: someone called us. 0: We call another function */
Sebastian Hack's avatar
Sebastian Hack committed
67

68
	int                    pos;
Sebastian Hack's avatar
Sebastian Hack committed
69
	const arch_register_t *reg;
70
71
72
73
74
	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
75
76
} be_abi_call_arg_t;

77
struct be_abi_call_t {
Michael Beck's avatar
Michael Beck committed
78
79
	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. */
80
81
82
	const be_abi_callbacks_t    *cb;
	ir_type                     *between_type;
	set                         *params;
Michael Beck's avatar
Michael Beck committed
83
	const arch_register_class_t *cls_addr; /**< register class of the call address */
Sebastian Hack's avatar
Sebastian Hack committed
84
85
};

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

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

95
	ir_node              *start;        /**< The be_Start params node. */
Sebastian Hack's avatar
Sebastian Hack committed
96
	pmap                 *regs;         /**< A map of all callee-save and ignore regs to
97
98
	                                         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
99

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

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

105
static ir_heights_t *ir_heights;
106

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

Matthias Braun's avatar
Matthias Braun committed
110
111
112
113
114
115
116
117
118
119
120
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);
}

Sebastian Hack's avatar
Sebastian Hack committed
121
122
123
124
125
126
/*
     _    ____ ___    ____      _ _ _                _
    / \  | __ )_ _|  / ___|__ _| | | |__   __ _  ___| | _____
   / _ \ |  _ \| |  | |   / _` | | | '_ \ / _` |/ __| |/ / __|
  / ___ \| |_) | |  | |__| (_| | | | |_) | (_| | (__|   <\__ \
 /_/   \_\____/___|  \____\__,_|_|_|_.__/ \__,_|\___|_|\_\___/
127

Sebastian Hack's avatar
Sebastian Hack committed
128
129
130
  These callbacks are used by the backend to set the parameters
  for a specific call type.
*/
Sebastian Hack's avatar
Sebastian Hack committed
131

Michael Beck's avatar
Michael Beck committed
132
133
134
/**
 * Set compare function: compares two ABI call object arguments.
 */
Sebastian Hack's avatar
Sebastian Hack committed
135
136
static int cmp_call_arg(const void *a, const void *b, size_t n)
{
137
138
	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;
139
	(void) n;
140
	return !(p->is_res == q->is_res && p->pos == q->pos && p->callee == q->callee);
Sebastian Hack's avatar
Sebastian Hack committed
141
142
}

Michael Beck's avatar
Michael Beck committed
143
/**
Michael Beck's avatar
Michael Beck committed
144
 * Get  an ABI call object argument.
Michael Beck's avatar
Michael Beck committed
145
146
147
148
 *
 * @param call      the abi call
 * @param is_res    true for call results, false for call arguments
 * @param pos       position of the argument
149
 * @param callee    context type - if we are callee or caller
Michael Beck's avatar
Michael Beck committed
150
 */
151
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
152
153
154
155
{
	be_abi_call_arg_t arg;
	unsigned hash;

156
	memset(&arg, 0, sizeof(arg));
Sebastian Hack's avatar
Sebastian Hack committed
157
158
	arg.is_res = is_res;
	arg.pos    = pos;
159
	arg.callee = callee;
Sebastian Hack's avatar
Sebastian Hack committed
160

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

163
	return (be_abi_call_arg_t*)set_find(call->params, &arg, sizeof(arg), hash);
Sebastian Hack's avatar
Sebastian Hack committed
164
165
}

Michael Beck's avatar
Michael Beck committed
166
/**
Michael Beck's avatar
Michael Beck committed
167
 * Set an ABI call object argument.
Michael Beck's avatar
Michael Beck committed
168
 */
169
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
170
{
171
172
173
174
175
176
177
178
179
	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
180
181
}

Michael Beck's avatar
Michael Beck committed
182
/* Set the flags for a call. */
183
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
184
{
185
186
	call->flags = flags;
	call->cb    = cb;
Sebastian Hack's avatar
Sebastian Hack committed
187
188
}

Michael Beck's avatar
Michael Beck committed
189
/* Sets the number of bytes the stackframe is shrinked by the callee on return */
190
191
192
193
194
void be_abi_call_set_pop(be_abi_call_t *call, int pop)
{
	assert(pop >= 0);
	call->pop = pop;
}
195
196
197
198
199
200
201
202

/* 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;
}


203
204
205
206
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
207
{
208
209
	be_abi_call_arg_t arg;
	memset(&arg, 0, sizeof(arg));
210
	assert(alignment > 0 && "Alignment must be greater than 0");
211
212
213
214
215
216
217
218
219
	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
220
221
}

222
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
223
{
224
225
226
227
228
229
230
231
232
	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
233
234
}

235
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
236
{
237
238
239
240
241
242
243
244
245
	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
246
247
}

Michael Beck's avatar
Michael Beck committed
248
/* Get the flags of a ABI call object. */
249
250
251
252
253
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
254
255
256
/**
 * Constructor for a new ABI call object.
 *
Michael Beck's avatar
Michael Beck committed
257
258
 * @param cls_addr  register class of the call address
 *
Michael Beck's avatar
Michael Beck committed
259
260
 * @return the new ABI call object
 */
261
static be_abi_call_t *be_abi_call_new(const arch_register_class_t *cls_addr)
Sebastian Hack's avatar
Sebastian Hack committed
262
{
263
	be_abi_call_t *call = XMALLOCZ(be_abi_call_t);
264

Sebastian Hack's avatar
Sebastian Hack committed
265
266
267
	call->flags.val  = 0;
	call->params     = new_set(cmp_call_arg, 16);
	call->cb         = NULL;
268
	call->cls_addr   = cls_addr;
269

270
	call->flags.bits.try_omit_fp = be_omit_fp;
271

Sebastian Hack's avatar
Sebastian Hack committed
272
273
274
	return call;
}

Michael Beck's avatar
Michael Beck committed
275
276
277
278
/**
 * Destructor for an ABI call object.
 */
static void be_abi_call_free(be_abi_call_t *call)
Sebastian Hack's avatar
Sebastian Hack committed
279
280
281
282
283
{
	del_set(call->params);
	free(call);
}

284
285
286
287
288
289
290
/**
 * 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
291
 * @param param_map an array mapping method argument positions to the stack argument type
292
293
294
295
 *
 * @return the initialized stack layout
 */
static be_stack_layout_t *stack_frame_init(be_stack_layout_t *frame, ir_type *args,
296
                                           ir_type *between, ir_type *locals,
297
                                           ir_entity *param_map[])
298
299
300
301
302
{
	frame->arg_type       = args;
	frame->between_type   = between;
	frame->frame_type     = locals;
	frame->initial_offset = 0;
Matthias Braun's avatar
Matthias Braun committed
303
	frame->initial_bias   = 0;
304
	frame->order[1]       = between;
305
	frame->param_map      = param_map;
306

307
308
309
310
	/* typical decreasing stack: locals have the
	 * lowest addresses, arguments the highest */
	frame->order[0] = locals;
	frame->order[2] = args;
311
312
313
	return frame;
}

314
315
316
317
318
319
320
321
322
323
324
/*
   ____      _ _
  / ___|__ _| | |___
 | |   / _` | | / __|
 | |__| (_| | | \__ \
  \____\__,_|_|_|___/

  Adjustment of the calls inside a graph.

*/

Sebastian Hack's avatar
Sebastian Hack committed
325
/**
326
327
 * Transform a call node into a be_Call node.
 *
Sebastian Hack's avatar
Sebastian Hack committed
328
 * @param env The ABI environment for the current irg.
329
330
331
 * @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
332
 */
Matthias Braun's avatar
Matthias Braun committed
333
static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
Sebastian Hack's avatar
Sebastian Hack committed
334
{
335
336
	ir_graph *irg              = get_irn_irg(irn);
	const arch_env_t *arch_env = be_get_irg_arch_env(irg);
337
	ir_type *call_tp           = get_Call_type(irn);
338
	ir_node *call_ptr          = get_Call_ptr(irn);
339
	size_t   n_params          = get_method_n_params(call_tp);
340
341
342
	ir_node *curr_mem          = get_Call_mem(irn);
	ir_node *bl                = get_nodes_block(irn);
	int stack_size             = 0;
343
	const arch_register_t *sp  = arch_env->sp;
344
345
346
	be_abi_call_t *call        = be_abi_call_new(sp->reg_class);
	ir_mode *mach_mode         = sp->reg_class->mode;
	int no_alloc               = call->flags.bits.frame_is_setup_on_call;
347
	int n_res                  = get_method_n_ress(call_tp);
348
	int do_seq                 = call->flags.bits.store_args_sequential && !no_alloc;
349
350
351
352
353

	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
354

355
356
	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);
357
358
359
360
361
362
363
	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;
364
	int                     i, n, destroy_all_regs;
365
	int                     throws_exception;
Matthias Braun's avatar
Matthias Braun committed
366
	size_t                  s;
367
	size_t                  p;
368
	dbg_info               *dbgi;
Sebastian Hack's avatar
Sebastian Hack committed
369
370

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

	/* Insert code to put the stack arguments on the stack. */
374
	assert(get_Call_n_params(irn) == n_params);
375
	stack_param_idx = ALLOCAN(int, n_params);
376
377
	for (p = 0; p < n_params; ++p) {
		be_abi_call_arg_t *arg = get_call_arg(call, 0, p, 0);
378
		assert(arg);
Michael Beck's avatar
Michael Beck committed
379
		if (arg->on_stack) {
380
			int arg_size = get_type_size_bytes(get_method_param_type(call_tp, p));
Michael Beck's avatar
Michael Beck committed
381
382
383
384

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

386
			stack_param_idx[n_stack_params++] = p;
Sebastian Hack's avatar
Sebastian Hack committed
387
388
389
390
		}
	}

	/* Collect all arguments which are passed in registers. */
391
	reg_param_idxs = ALLOCAN(int, n_params);
392
393
	for (p = 0; p < n_params; ++p) {
		be_abi_call_arg_t *arg = get_call_arg(call, 0, p, 0);
394
		if (arg && arg->in_reg) {
395
			reg_param_idxs[n_reg_params++] = p;
Sebastian Hack's avatar
Sebastian Hack committed
396
397
398
		}
	}

399
400
401
402
403
404
405
406
407
	/*
	 * 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.
	 */
408
	if (!do_seq && !no_alloc) {
409
		curr_sp = be_new_IncSP(sp, bl, curr_sp, stack_size, 1);
410
411
	}

Christoph Mallon's avatar
Christoph Mallon committed
412
	dbgi = get_irn_dbg_info(irn);
Sebastian Hack's avatar
Sebastian Hack committed
413
	/* If there are some parameters which shall be passed on the stack. */
414
	if (n_stack_params > 0) {
415
416
417
		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
418

419
420
		/*
		 * Reverse list of stack parameters if call arguments are from left to right.
421
		 * We must them reverse again if they are pushed (not stored) and the stack
422
423
		 * direction is downwards.
		 */
424
		if (call->flags.bits.left_to_right ^ do_seq) {
425
426
427
428
429
			for (i = 0; i < n_stack_params >> 1; ++i) {
				int other  = n_stack_params - i - 1;
				int tmp    = stack_param_idx[i];
				stack_param_idx[i]     = stack_param_idx[other];
				stack_param_idx[other] = tmp;
Sebastian Hack's avatar
Sebastian Hack committed
430
431
432
			}
		}

433
		curr_mem = get_Call_mem(irn);
434
		if (! do_seq) {
435
			in[n_in++] = curr_mem;
436
437
		}

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

447
448
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.
			 */
			if (do_seq) {
				curr_ofs = 0;
454
455
				addr = curr_sp = be_new_IncSP(sp, bl, curr_sp,
				                              param_size + arg->space_before, 0);
Sebastian Hack's avatar
Sebastian Hack committed
456
				add_irn_dep(curr_sp, curr_mem);
457
			} else {
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. */
462
				if (curr_ofs > 0) {
463
					ir_mode *constmode = mach_mode;
464
					if (mode_is_reference(mach_mode)) {
465
466
						constmode = mode_Is;
					}
467
					addr = new_r_Const_long(irg, constmode, curr_ofs);
468
					addr = new_r_Add(bl, curr_sp, addr, mach_mode);
469
				}
Sebastian Hack's avatar
Sebastian Hack committed
470
471
472
			}

			/* Insert a store for primitive arguments. */
473
			if (is_atomic_type(param_type)) {
474
475
				ir_node *nomem     = get_irg_no_mem(irg);
				ir_node *mem_input = do_seq ? curr_mem : nomem;
476
				ir_node *store     = new_rd_Store(dbgi, bl, mem_input, addr, param, cons_none);
477
				mem   = new_r_Proj(store, mode_M, pn_Store_M);
478
479
			} else {
				/* Make a mem copy for compound arguments. */
480
481
				ir_node *copy;

Sebastian Hack's avatar
Sebastian Hack committed
482
				assert(mode_is_reference(get_irn_mode(param)));
483
				copy = new_rd_CopyB(dbgi, bl, curr_mem, addr, param, param_type);
484
				mem = new_r_Proj(copy, mode_M, pn_CopyB_M);
Sebastian Hack's avatar
Sebastian Hack committed
485
486
487
488
			}

			curr_ofs += param_size;

489
			if (do_seq)
Sebastian Hack's avatar
Sebastian Hack committed
490
				curr_mem = mem;
491
			else
492
				in[n_in++] = mem;
Sebastian Hack's avatar
Sebastian Hack committed
493
494
495
		}

		/* We need the sync only, if we didn't build the stores sequentially. */
496
497
		if (! do_seq) {
			if (n_stack_params >= 1) {
498
				curr_mem = new_r_Sync(bl, n_in, in);
499
500
501
502
			} else {
				curr_mem = get_Call_mem(irn);
			}
		}
Sebastian Hack's avatar
Sebastian Hack committed
503
504
	}

505
506
507
508
509
510
511
512
513
514
515
516
517
518
	/* check for the return_twice property */
	destroy_all_regs = 0;
	if (is_SymConst_addr_ent(call_ptr)) {
		ir_entity *ent = get_SymConst_entity(call_ptr);

		if (get_entity_additional_properties(ent) & mtp_property_returns_twice)
			destroy_all_regs = 1;
	} else {
		ir_type *call_tp = get_Call_type(irn);

		if (get_method_additional_properties(call_tp) & mtp_property_returns_twice)
			destroy_all_regs = 1;
	}

519
520
	/* Put caller save into the destroyed set and state registers in the states
	 * set */
521
	for (i = 0, n = arch_env->n_register_classes; i < n; ++i) {
522
		unsigned j;
523
		const arch_register_class_t *cls = &arch_env->register_classes[i];
524
		for (j = 0; j < cls->n_regs; ++j) {
Sebastian Hack's avatar
Sebastian Hack committed
525
			const arch_register_t *reg = arch_register_for_index(cls, j);
526

527
528
529
530
531
			/* 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;

532
			if (reg->type & arch_register_type_state) {
533
534
535
536
537
538
				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;
			}
539
			if (destroy_all_regs || arch_register_is_caller_save(arch_env, reg)) {
540
				if (!(reg->type & arch_register_type_ignore)) {
541
542
					ARR_APP1(const arch_register_t*, destroyed_regs, reg);
				}
543
			}
Sebastian Hack's avatar
Sebastian Hack committed
544
545
		}
	}
Sebastian Hack's avatar
Sebastian Hack committed
546

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

Sebastian Hack's avatar
Sebastian Hack committed
550
551
	foreach_out_edge(irn, edge) {
		const ir_edge_t *res_edge;
Matthias Braun's avatar
Matthias Braun committed
552
		ir_node         *irn = get_edge_src_irn(edge);
Sebastian Hack's avatar
Sebastian Hack committed
553

554
		if (!is_Proj(irn) || get_Proj_proj(irn) != pn_Call_T_result)
Matthias Braun's avatar
Matthias Braun committed
555
556
557
558
559
560
561
562
563
564
			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
565
			assert(res_projs[proj] == NULL);
Matthias Braun's avatar
Matthias Braun committed
566
			res_projs[proj] = res;
Sebastian Hack's avatar
Sebastian Hack committed
567
		}
Matthias Braun's avatar
Matthias Braun committed
568
569
		res_proj = irn;
		break;
Sebastian Hack's avatar
Sebastian Hack committed
570
	}
571

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

577
	n_ins = 0;
578
	in    = ALLOCAN(ir_node*, n_reg_params + ARR_LEN(states));
579

580
	/* make the back end call node and set its register requirements. */
581
	for (i = 0; i < n_reg_params; ++i) {
582
		in[n_ins++] = get_Call_param(irn, reg_param_idxs[i]);
583
	}
584
585

	/* add state registers ins */
Matthias Braun's avatar
Matthias Braun committed
586
587
	for (s = 0; s < ARR_LEN(states); ++s) {
		const arch_register_t       *reg = states[s];
588
		const arch_register_class_t *cls = arch_register_get_class(reg);
589
		ir_node *regnode = new_r_Unknown(irg, arch_register_class_mode(cls));
590
		in[n_ins++]      = regnode;
591
	}
592
	assert(n_ins == (int) (n_reg_params + ARR_LEN(states)));
593

594
	/* ins collected, build the call */
595
	throws_exception = ir_throws_exception(irn);
596
597
	if (env->call->flags.bits.call_has_imm && is_SymConst(call_ptr)) {
		/* direct call */
598
		low_call = be_new_Call(dbgi, irg, bl, curr_mem, curr_sp, curr_sp,
599
		                       n_reg_results + pn_be_Call_first_res + ARR_LEN(destroyed_regs),
Matthias Braun's avatar
Matthias Braun committed
600
		                       n_ins, in, get_Call_type(irn));
Sebastian Hack's avatar
Sebastian Hack committed
601
		be_Call_set_entity(low_call, get_SymConst_entity(call_ptr));
602
	} else {
603
		/* indirect call */
604
		low_call = be_new_Call(dbgi, irg, bl, curr_mem, curr_sp, call_ptr,
605
		                       n_reg_results + pn_be_Call_first_res + ARR_LEN(destroyed_regs),
606
		                       n_ins, in, get_Call_type(irn));
Sebastian Hack's avatar
Sebastian Hack committed
607
	}
608
	ir_set_throws_exception(low_call, throws_exception);
609
	be_Call_set_pop(low_call, call->pop);
610
611

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

Matthias Braun's avatar
Matthias Braun committed
614
	/* create new stack pointer */
615
	curr_sp = new_r_Proj(low_call, get_irn_mode(curr_sp), pn_be_Call_sp);
616
617
	be_set_constr_single_reg_out(low_call, pn_be_Call_sp, sp,
			arch_register_req_type_ignore | arch_register_req_type_produces_sp);
618
	arch_set_irn_register(curr_sp, sp);
Matthias Braun's avatar
Matthias Braun committed
619

620
	/* now handle results */
621
	for (i = 0; i < n_res; ++i) {
Matthias Braun's avatar
Matthias Braun committed
622
		ir_node           *proj = res_projs[i];
623
		be_abi_call_arg_t *arg  = get_call_arg(call, 1, i, 0);
624
		long               pn   = i + pn_be_Call_first_res;
Matthias Braun's avatar
Matthias Braun committed
625

Matthias Braun's avatar
Matthias Braun committed
626
627
628
		/* returns values on stack not supported yet */
		assert(arg->in_reg);

Matthias Braun's avatar
Matthias Braun committed
629
630
631
632
633
634
635
		/*
			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;

636
		if (proj == NULL) {
637
			ir_type *res_type = get_method_res_type(call_tp, i);
Matthias Braun's avatar
Matthias Braun committed
638
			ir_mode *mode     = get_type_mode(res_type);
639
			proj              = new_r_Proj(low_call, mode, pn);
Matthias Braun's avatar
Matthias Braun committed
640
641
642
643
644
645
646
			res_projs[i]      = proj;
		} else {
			set_Proj_pred(proj, low_call);
			set_Proj_proj(proj, pn);
		}

		if (arg->in_reg) {
647
			/* remove register from destroyed regs */
648
649
			size_t j;
			size_t n = ARR_LEN(destroyed_regs);
650
651
652
653
654
655
656
			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
657
658
659
		}
	}

660
	/*
661
662
		Set the register class of the call address to
		the backend provided class (default: stack pointer class)
663
	*/
664
	be_node_set_reg_class_in(low_call, n_be_Call_ptr, call->cls_addr);
665

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

Sebastian Hack's avatar
Sebastian Hack committed
668
	/* Set the register classes and constraints of the Call parameters. */
669
670
	for (i = 0; i < n_reg_params; ++i) {
		int index = reg_param_idxs[i];
671
		be_abi_call_arg_t *arg = get_call_arg(call, 0, index, 0);
Sebastian Hack's avatar
Sebastian Hack committed
672
		assert(arg->reg != NULL);
673

674
		be_set_constr_single_reg_in(low_call, n_be_Call_first_arg + i,
675
		                            arg->reg, arch_register_req_type_none);
Sebastian Hack's avatar
Sebastian Hack committed
676
677
678
	}

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

Sebastian Hack's avatar
Sebastian Hack committed
684
		assert(arg->in_reg);
685
686
		be_set_constr_single_reg_out(low_call, pn, arg->reg,
		                             arch_register_req_type_none);
687
		arch_set_irn_register(proj, arg->reg);
Sebastian Hack's avatar
Sebastian Hack committed
688
	}
689
690
	exchange(irn, low_call);

Matthias Braun's avatar
Matthias Braun committed
691
	/* kill the ProjT node */
692
	if (res_proj != NULL) {
693
		kill_node(res_proj);
694
695
	}

Sebastian Hack's avatar
Sebastian Hack committed
696
697
	/* Make additional projs for the caller save registers
	   and the Keep node which keeps them alive. */
698
	{
699
		ir_node               **in, *keep;
700
		int                   i;
Matthias Braun's avatar
Matthias Braun committed
701
		size_t                d;
702
		int                   n = 0;
703
		int                   curr_res_proj = pn_be_Call_first_res + n_reg_results;
704
		int                   n_ins;
Sebastian Hack's avatar
Sebastian Hack committed
705

706
		n_ins = ARR_LEN(destroyed_regs) + n_reg_results + 1;
707
		in    = ALLOCAN(ir_node *, n_ins);
708

709
710
		/* also keep the stack pointer */
		set_irn_link(curr_sp, (void*) sp);
711
		in[n++] = curr_sp;
712

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

Sebastian Hack's avatar
Sebastian Hack committed
717
			/* memorize the register in the link field. we need afterwards to set the register class of the keep correctly. */
718
719
			be_set_constr_single_reg_out(low_call, curr_res_proj, reg,
			                             arch_register_req_type_none);
720
			arch_set_irn_register(proj, reg);
721

Matthias Braun's avatar
Matthias Braun committed
722
			set_irn_link(proj, (void*) reg);
723
			in[n++] = proj;
724
			++curr_res_proj;
Sebastian Hack's avatar
Sebastian Hack committed
725
726
		}

727
		for (i = 0; i < n_reg_results; ++i) {
Matthias Braun's avatar
Matthias Braun committed
728
			ir_node *proj = res_projs[i];
729
			const arch_register_t *reg = arch_get_irn_register(proj);
Matthias Braun's avatar
Matthias Braun committed
730
			set_irn_link(proj, (void*) reg);
731
			in[n++] = proj;
Matthias Braun's avatar
Matthias Braun committed
732
		}
733
		assert(n <= n_ins);
Matthias Braun's avatar
Matthias Braun committed
734

735
		/* create the Keep for the caller save registers */
736
		keep = be_new_Keep(bl, n, in);
737
		for (i = 0; i < n; ++i) {
738
			const arch_register_t *reg = (const arch_register_t*)get_irn_link(in[i]);
739
			be_node_set_reg_class_in(keep, i, reg->reg_class);
Sebastian Hack's avatar
Sebastian Hack committed
740
		}
Sebastian Hack's avatar
Sebastian Hack committed
741
742
743
	}

	/* Clean up the stack. */
744
745
746
	assert(stack_size >= call->pop);
	stack_size -= call->pop;

747
	if (stack_size > 0) {
748
749
750
751
		ir_node *mem_proj = NULL;

		foreach_out_edge(low_call, edge) {
			ir_node *irn = get_edge_src_irn(edge);
752
			if (is_Proj(irn) && get_Proj_proj(irn) == pn_Call_M) {
753
754
755
756
				mem_proj = irn;
				break;
			}
		}
Sebastian Hack's avatar
Sebastian Hack committed
757

758
		if (! mem_proj) {
759
			mem_proj = new_r_Proj(low_call, mode_M, pn_be_Call_M);
Sebastian Hack's avatar
Sebastian Hack committed
760
761
			keep_alive(mem_proj);
		}
762
763
764
	}
	/* Clean up the stack frame or revert alignment fixes if we allocated it */
	if (! no_alloc) {
765
		curr_sp = be_new_IncSP(sp, bl, curr_sp, -stack_size, 0);
Sebastian Hack's avatar
Sebastian Hack committed
766
767
768
	}

	be_abi_call_free(call);
769

770
771
	DEL_ARR_F(states);
	DEL_ARR_F(destroyed_regs);
772
773
774
775

	return curr_sp;
}

776
777
778
779
780
/**
 * 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
781
 * @param block      the block where new nodes are allocated on
782
783
784
785
 * @param dbg        debug info for new nodes
 *
 * @return a node representing the aligned size
 */
786
static ir_node *adjust_alloc_size(unsigned stack_alignment, ir_node *size,
787
                                  ir_node *block, dbg_info *dbg)
788
{
789
	if (stack_alignment > 1) {
Matthias Braun's avatar
Matthias Braun committed
790
791
792
793
		ir_mode   *mode;
		ir_tarval *tv;
		ir_node   *mask;
		ir_graph  *irg;
Michael Beck's avatar
Michael Beck committed
794

795
		assert(is_po2(stack_alignment));
796

Michael Beck's avatar
Michael Beck committed
797
798
		mode = get_irn_mode(size);
		tv   = new_tarval_from_long(stack_alignment-1, mode);
799
		irg  = get_Block_irg(block);
800
		mask = new_r_Const(irg, tv);
801
		size = new_rd_Add(dbg, block, size, mask, mode);
802
803

		tv   = new_tarval_from_long(-(long)stack_alignment, mode);
804
		mask = new_r_Const(irg, tv);
805
		size = new_rd_And(dbg, block, size, mask, mode);
806
807
808
	}
	return size;
}
809
810
811
812
/**
 * 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
813
static ir_node *adjust_alloc(be_abi_irg_t *env, ir_node *alloc, ir_node *curr_sp)
814
{
815
816
817
818
819
820
821
	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;
822

823
	const ir_edge_t *edge;
824
825
826
827
	ir_node *new_alloc;
	ir_node *count;
	ir_node *size;
	ir_node *ins[2];
828
	unsigned stack_alignment;
829

830
	/* all non-stack Alloc nodes should already be lowered before the backend */
831
	assert(get_Alloc_where(alloc) == stack_alloc);
832

833
834
	foreach_out_edge(alloc, edge) {
		ir_node *irn = get_edge_src_irn(edge);
835

836
		assert(is_Proj(irn));
837
		switch (get_Proj_proj(irn)) {
838
839
840
841
842
843
844
845
		case pn_Alloc_M:
			alloc_mem = irn;
			break;
		case pn_Alloc_res:
			alloc_res = irn;
			break;
		default:
			break;
846
		}
847
	}
848

849
850
851
852
853
854
855
856
	/* 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;
	}

857
858
	dbg   = get_irn_dbg_info(alloc);
	count = get_Alloc_count(alloc);
859

860
	/* we might need to multiply the count with the element size */
861
	if (type != firm_unknown_type && get_type_size_bytes(type) != 1) {
Matthias Braun's avatar
Matthias Braun committed
862
863
864
865
866
		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);