beabi.c 78.1 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 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
43
44
#include "height.h"
#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
58
59
#include "bemodule.h"

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

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
 */
Sebastian Hack's avatar
Sebastian Hack committed
88
struct _be_abi_irg_t {
Sebastian Hack's avatar
Sebastian Hack committed
89
90
	survive_dce_t        *dce_survivor;

Sebastian Hack's avatar
Sebastian Hack committed
91
	be_abi_call_t        *call;         /**< The ABI call information. */
Sebastian Hack's avatar
Sebastian Hack committed
92

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

96
	ir_node              *start;        /**< The be_Start params node. */
Sebastian Hack's avatar
Sebastian Hack committed
97
	pmap                 *regs;         /**< A map of all callee-save and ignore regs to
98
	                                         their Projs to the RegParams node. */
Sebastian Hack's avatar
Sebastian Hack committed
99

100
	int                  start_block_bias; /**< The stack bias at the end of the start block. */
Sebastian Hack's avatar
Sebastian Hack committed
101

102
103
	void                 *cb;           /**< ABI Callback self pointer. */

Sebastian Hack's avatar
Sebastian Hack committed
104
105
106
	pmap                 *keep_map;     /**< mapping blocks to keep nodes. */
	pset                 *ignore_regs;  /**< Additional registers which shall be ignored. */

107
	ir_node              **calls;       /**< flexible array containing all be_Call nodes */
108

109
	arch_register_req_t  *sp_req;
Sebastian Hack's avatar
Sebastian Hack committed
110
};
Sebastian Hack's avatar
Sebastian Hack committed
111

112
static heights_t *ir_heights;
113

114
/** Flag: if set, try to omit the frame pointer in all routines. */
115
static int be_omit_fp = 1;
116

117
118
119
/** Flag: if set, try to omit the frame pointer in leaf routines only. */
static int be_omit_leaf_fp = 1;

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

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

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

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

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

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

Michael Beck's avatar
Michael Beck committed
161
	return set_find(call->params, &arg, sizeof(arg), hash);
Sebastian Hack's avatar
Sebastian Hack committed
162
163
}

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

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

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

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


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

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

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

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

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

268
	call->flags.bits.try_omit_fp = be_omit_fp | be_omit_leaf_fp;
269

Sebastian Hack's avatar
Sebastian Hack committed
270
271
272
	return call;
}

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

Sebastian Hack's avatar
Sebastian Hack committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
  _____                           _   _                 _ _ _
 |  ___| __ __ _ _ __ ___   ___  | | | | __ _ _ __   __| | (_)_ __   __ _
 | |_ | '__/ _` | '_ ` _ \ / _ \ | |_| |/ _` | '_ \ / _` | | | '_ \ / _` |
 |  _|| | | (_| | | | | | |  __/ |  _  | (_| | | | | (_| | | | | | | (_| |
 |_|  |_|  \__,_|_| |_| |_|\___| |_| |_|\__,_|_| |_|\__,_|_|_|_| |_|\__, |
                                                                    |___/

  Handling of the stack frame. It is composed of three types:
  1) The type of the arguments which are pushed on the stack.
  2) The "between type" which consists of stuff the call of the
     function pushes on the stack (like the return address and
	 the old base pointer for ia32).
  3) The Firm frame type which consists of all local variables
     and the spills.
*/

299
300
int be_get_stack_entity_offset(be_stack_layout_t *frame, ir_entity *ent,
                               int bias)
301
{
302
	ir_type *t = get_entity_owner(ent);
303
	int ofs    = get_entity_offset(ent);
304

305
	int index;
306
307

	/* Find the type the entity is contained in. */
308
309
	for (index = 0; index < N_FRAME_TYPES; ++index) {
		if (frame->order[index] == t)
310
			break;
311
312
		/* Add the size of all the types below the one of the entity to the entity's offset */
		ofs += get_type_size_bytes(frame->order[index]);
313
314
315
316
317
318
319
320
321
322
323
	}

	/* correct the offset by the initial position of the frame pointer */
	ofs -= frame->initial_offset;

	/* correct the offset with the current bias. */
	ofs += bias;

	return ofs;
}

Michael Beck's avatar
Michael Beck committed
324
325
326
/**
 * Retrieve the entity with given offset from a frame type.
 */
327
static ir_entity *search_ent_with_offset(ir_type *t, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
328
329
330
{
	int i, n;

331
	for (i = 0, n = get_compound_n_members(t); i < n; ++i) {
332
		ir_entity *ent = get_compound_member(t, i);
333
		if (get_entity_offset(ent) == offset)
Sebastian Hack's avatar
Sebastian Hack committed
334
335
336
337
338
339
			return ent;
	}

	return NULL;
}

340
static int stack_frame_compute_initial_offset(be_stack_layout_t *frame)
341
{
342
343
	ir_type  *base = frame->stack_dir < 0 ? frame->between_type : frame->frame_type;
	ir_entity *ent = search_ent_with_offset(base, 0);
344

345
346
347
348
	if (ent == NULL) {
		frame->initial_offset
			= frame->stack_dir < 0 ? get_type_size_bytes(frame->frame_type) : get_type_size_bytes(frame->between_type);
	} else {
349
		frame->initial_offset = be_get_stack_entity_offset(frame, ent, 0);
350
	}
351

352
353
354
	return frame->initial_offset;
}

355
356
357
358
359
360
361
/**
 * 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
362
 * @param stack_dir the stack direction: < 0 decreasing, > 0 increasing addresses
363
 * @param param_map an array mapping method argument positions to the stack argument type
364
365
366
367
 *
 * @return the initialized stack layout
 */
static be_stack_layout_t *stack_frame_init(be_stack_layout_t *frame, ir_type *args,
368
                                           ir_type *between, ir_type *locals, int stack_dir,
369
                                           ir_entity *param_map[])
370
371
372
373
374
{
	frame->arg_type       = args;
	frame->between_type   = between;
	frame->frame_type     = locals;
	frame->initial_offset = 0;
Matthias Braun's avatar
Matthias Braun committed
375
	frame->initial_bias   = 0;
376
377
	frame->stack_dir      = stack_dir;
	frame->order[1]       = between;
378
	frame->param_map      = param_map;
379

380
	if (stack_dir > 0) {
381
382
		frame->order[0] = args;
		frame->order[2] = locals;
383
	} else {
384
385
		/* typical decreasing stack: locals have the
		 * lowest addresses, arguments the highest */
386
387
388
389
390
391
		frame->order[0] = locals;
		frame->order[2] = args;
	}
	return frame;
}

392
393
394
395
396
397
398
399
400
401
402
/*
   ____      _ _
  / ___|__ _| | |___
 | |   / _` | | / __|
 | |__| (_| | | \__ \
  \____\__,_|_|_|___/

  Adjustment of the calls inside a graph.

*/

Sebastian Hack's avatar
Sebastian Hack committed
403
/**
404
405
 * Transform a call node into a be_Call node.
 *
Sebastian Hack's avatar
Sebastian Hack committed
406
 * @param env The ABI environment for the current irg.
407
408
409
 * @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
410
 */
Matthias Braun's avatar
Matthias Braun committed
411
static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
Sebastian Hack's avatar
Sebastian Hack committed
412
{
413
414
	ir_graph *irg              = get_irn_irg(irn);
	const arch_env_t *arch_env = be_get_irg_arch_env(irg);
415
	ir_type *call_tp           = get_Call_type(irn);
416
	ir_node *call_ptr          = get_Call_ptr(irn);
417
	int n_params               = get_method_n_params(call_tp);
418
419
420
	ir_node *curr_mem          = get_Call_mem(irn);
	ir_node *bl                = get_nodes_block(irn);
	int stack_size             = 0;
421
422
	int stack_dir              = arch_env->stack_dir;
	const arch_register_t *sp  = arch_env->sp;
423
424
425
	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;
426
	int n_res                  = get_method_n_ress(call_tp);
427
	int do_seq                 = call->flags.bits.store_args_sequential && !no_alloc;
428
429
430
431
432

	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
433

434
435
	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);
436
437
438
439
440
441
442
	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;
443
	int                     i, n, destroy_all_regs;
444
	dbg_info               *dbgi;
Sebastian Hack's avatar
Sebastian Hack committed
445
446

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

	/* Insert code to put the stack arguments on the stack. */
450
	assert(get_Call_n_params(irn) == n_params);
451
	stack_param_idx = ALLOCAN(int, n_params);
452
	for (i = 0; i < n_params; ++i) {
453
		be_abi_call_arg_t *arg = get_call_arg(call, 0, i, 0);
454
		assert(arg);
Michael Beck's avatar
Michael Beck committed
455
		if (arg->on_stack) {
456
			int arg_size = get_type_size_bytes(get_method_param_type(call_tp, i));
Michael Beck's avatar
Michael Beck committed
457
458
459
460

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

			stack_param_idx[n_stack_params++] = i;
Sebastian Hack's avatar
Sebastian Hack committed
463
464
465
466
		}
	}

	/* Collect all arguments which are passed in registers. */
467
	reg_param_idxs = ALLOCAN(int, n_params);
468
	for (i = 0; i < n_params; ++i) {
469
		be_abi_call_arg_t *arg = get_call_arg(call, 0, i, 0);
470
		if (arg && arg->in_reg) {
471
			reg_param_idxs[n_reg_params++] = i;
Sebastian Hack's avatar
Sebastian Hack committed
472
473
474
		}
	}

475
476
477
478
479
480
481
482
483
484
	/*
	 * 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.
	 */
	if (stack_dir < 0 && !do_seq && !no_alloc) {
485
		curr_sp = be_new_IncSP(sp, bl, curr_sp, stack_size, 1);
486
487
	}

Christoph Mallon's avatar
Christoph Mallon committed
488
	dbgi = get_irn_dbg_info(irn);
Sebastian Hack's avatar
Sebastian Hack committed
489
	/* If there are some parameters which shall be passed on the stack. */
490
	if (n_stack_params > 0) {
491
492
493
		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
494

495
496
		/*
		 * Reverse list of stack parameters if call arguments are from left to right.
497
		 * We must them reverse again if they are pushed (not stored) and the stack
498
499
500
		 * direction is downwards.
		 */
		if (call->flags.bits.left_to_right ^ (do_seq && stack_dir < 0)) {
501
502
503
504
505
			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
506
507
508
			}
		}

509
		curr_mem = get_Call_mem(irn);
510
		if (! do_seq) {
511
			in[n_in++] = curr_mem;
512
513
		}

514
515
		for (i = 0; i < n_stack_params; ++i) {
			int p                  = stack_param_idx[i];
516
			be_abi_call_arg_t *arg = get_call_arg(call, 0, p, 0);
517
518
519
			ir_node *param         = get_Call_param(irn, p);
			ir_node *addr          = curr_sp;
			ir_node *mem           = NULL;
520
			ir_type *param_type    = get_method_param_type(call_tp, p);
521
522
			int param_size         = get_type_size_bytes(param_type) + arg->space_after;

523
524
525
526
527
528
529
			/*
			 * 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;
530
531
				addr = curr_sp = be_new_IncSP(sp, bl, curr_sp,
				                              param_size + arg->space_before, 0);
Sebastian Hack's avatar
Sebastian Hack committed
532
				add_irn_dep(curr_sp, curr_mem);
533
			} else {
534
535
536
537
				curr_ofs += arg->space_before;
				curr_ofs =  round_up2(curr_ofs, arg->alignment);

				/* Make the expression to compute the argument's offset. */
538
				if (curr_ofs > 0) {
539
					ir_mode *constmode = mach_mode;
540
					if (mode_is_reference(mach_mode)) {
541
542
						constmode = mode_Is;
					}
543
					addr = new_r_Const_long(irg, constmode, curr_ofs);
544
					addr = new_r_Add(bl, curr_sp, addr, mach_mode);
545
				}
Sebastian Hack's avatar
Sebastian Hack committed
546
547
548
			}

			/* Insert a store for primitive arguments. */
549
			if (is_atomic_type(param_type)) {
550
				ir_node *store;
551
				ir_node *mem_input = do_seq ? curr_mem : new_NoMem();
552
				store = new_rd_Store(dbgi, bl, mem_input, addr, param, 0);
553
				mem   = new_r_Proj(store, mode_M, pn_Store_M);
554
555
			} else {
				/* Make a mem copy for compound arguments. */
556
557
				ir_node *copy;

Sebastian Hack's avatar
Sebastian Hack committed
558
				assert(mode_is_reference(get_irn_mode(param)));
559
				copy = new_rd_CopyB(dbgi, bl, curr_mem, addr, param, param_type);
560
				mem = new_r_Proj(copy, mode_M, pn_CopyB_M);
Sebastian Hack's avatar
Sebastian Hack committed
561
562
563
564
			}

			curr_ofs += param_size;

565
			if (do_seq)
Sebastian Hack's avatar
Sebastian Hack committed
566
				curr_mem = mem;
567
			else
568
				in[n_in++] = mem;
Sebastian Hack's avatar
Sebastian Hack committed
569
570
571
		}

		/* We need the sync only, if we didn't build the stores sequentially. */
572
573
		if (! do_seq) {
			if (n_stack_params >= 1) {
574
				curr_mem = new_r_Sync(bl, n_in, in);
575
576
577
578
			} else {
				curr_mem = get_Call_mem(irn);
			}
		}
Sebastian Hack's avatar
Sebastian Hack committed
579
580
	}

581
582
583
584
585
586
587
588
589
590
591
592
593
594
	/* 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;
	}

595
596
	/* Put caller save into the destroyed set and state registers in the states
	 * set */
597
	for (i = 0, n = arch_env_get_n_reg_class(arch_env); i < n; ++i) {
598
		unsigned j;
599
		const arch_register_class_t *cls = arch_env_get_reg_class(arch_env, i);
600
		for (j = 0; j < cls->n_regs; ++j) {
Sebastian Hack's avatar
Sebastian Hack committed
601
			const arch_register_t *reg = arch_register_for_index(cls, j);
602

603
604
605
606
607
			/* 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;

608
			if (arch_register_type_is(reg, state)) {
609
610
611
612
613
614
615
616
617
618
				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;
			}
			if (destroy_all_regs || arch_register_type_is(reg, caller_save)) {
				if (! arch_register_type_is(reg, ignore)) {
					ARR_APP1(const arch_register_t*, destroyed_regs, reg);
				}
619
			}
Sebastian Hack's avatar
Sebastian Hack committed
620
621
		}
	}
Sebastian Hack's avatar
Sebastian Hack committed
622

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

Sebastian Hack's avatar
Sebastian Hack committed
626
627
	foreach_out_edge(irn, edge) {
		const ir_edge_t *res_edge;
Matthias Braun's avatar
Matthias Braun committed
628
		ir_node         *irn = get_edge_src_irn(edge);
Sebastian Hack's avatar
Sebastian Hack committed
629

630
		if (!is_Proj(irn) || get_Proj_proj(irn) != pn_Call_T_result)
Matthias Braun's avatar
Matthias Braun committed
631
632
633
634
635
636
637
638
639
640
			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
641
			assert(res_projs[proj] == NULL);
Matthias Braun's avatar
Matthias Braun committed
642
			res_projs[proj] = res;
Sebastian Hack's avatar
Sebastian Hack committed
643
		}
Matthias Braun's avatar
Matthias Braun committed
644
645
		res_proj = irn;
		break;
Sebastian Hack's avatar
Sebastian Hack committed
646
	}
647

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

653
	n_ins = 0;
654
	in    = ALLOCAN(ir_node*, n_reg_params + ARR_LEN(states));
655

656
	/* make the back end call node and set its register requirements. */
657
	for (i = 0; i < n_reg_params; ++i) {
658
		in[n_ins++] = get_Call_param(irn, reg_param_idxs[i]);
659
	}
660
661

	/* add state registers ins */
662
663
	for (i = 0; i < ARR_LEN(states); ++i) {
		const arch_register_t       *reg = states[i];
664
665
666
667
668
		const arch_register_class_t *cls = arch_register_get_class(reg);
#if 0
		ir_node *regnode = be_abi_reg_map_get(env->regs, reg);
		ir_fprintf(stderr, "Adding %+F\n", regnode);
#endif
669
		ir_node *regnode = new_r_Unknown(irg, arch_register_class_mode(cls));
670
		in[n_ins++]      = regnode;
671
	}
672
	assert(n_ins == (int) (n_reg_params + ARR_LEN(states)));
673

674
	/* ins collected, build the call */
675
676
	if (env->call->flags.bits.call_has_imm && is_SymConst(call_ptr)) {
		/* direct call */
677
		low_call = be_new_Call(dbgi, irg, bl, curr_mem, curr_sp, curr_sp,
678
		                       n_reg_results + pn_be_Call_first_res + ARR_LEN(destroyed_regs),
Matthias Braun's avatar
Matthias Braun committed
679
		                       n_ins, in, get_Call_type(irn));
Sebastian Hack's avatar
Sebastian Hack committed
680
		be_Call_set_entity(low_call, get_SymConst_entity(call_ptr));
681
	} else {
682
		/* indirect call */
683
		low_call = be_new_Call(dbgi, irg, bl, curr_mem, curr_sp, call_ptr,
684
		                       n_reg_results + pn_be_Call_first_res + ARR_LEN(destroyed_regs),
685
		                       n_ins, in, get_Call_type(irn));
Sebastian Hack's avatar
Sebastian Hack committed
686
	}
687
	be_Call_set_pop(low_call, call->pop);
688
689

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

Matthias Braun's avatar
Matthias Braun committed
692
	/* create new stack pointer */
693
	curr_sp = new_r_Proj(low_call, get_irn_mode(curr_sp), pn_be_Call_sp);
694
695
	be_set_constr_single_reg_out(low_call, pn_be_Call_sp, sp,
			arch_register_req_type_ignore | arch_register_req_type_produces_sp);
696
	arch_set_irn_register(curr_sp, sp);
Matthias Braun's avatar
Matthias Braun committed
697

698
	/* now handle results */
699
	for (i = 0; i < n_res; ++i) {
Matthias Braun's avatar
Matthias Braun committed
700
701
		int pn;
		ir_node           *proj = res_projs[i];
702
		be_abi_call_arg_t *arg  = get_call_arg(call, 1, i, 0);
Matthias Braun's avatar
Matthias Braun committed
703

Matthias Braun's avatar
Matthias Braun committed
704
705
706
		/* returns values on stack not supported yet */
		assert(arg->in_reg);

Matthias Braun's avatar
Matthias Braun committed
707
708
709
710
711
712
713
		/*
			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;

714
		if (proj == NULL) {
715
			ir_type *res_type = get_method_res_type(call_tp, i);
Matthias Braun's avatar
Matthias Braun committed
716
			ir_mode *mode     = get_type_mode(res_type);
717
			proj              = new_r_Proj(low_call, mode, pn);
Matthias Braun's avatar
Matthias Braun committed
718
719
720
721
722
723
724
			res_projs[i]      = proj;
		} else {
			set_Proj_pred(proj, low_call);
			set_Proj_proj(proj, pn);
		}

		if (arg->in_reg) {
725
726
727
728
729
730
731
732
733
734
			/* remove register from destroyed regs */
			int j;
			int n = ARR_LEN(destroyed_regs);
			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
735
736
737
		}
	}

738
	/*
739
740
		Set the register class of the call address to
		the backend provided class (default: stack pointer class)
741
	*/
742
	be_node_set_reg_class_in(low_call, be_pos_Call_ptr, call->cls_addr);
743

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

Sebastian Hack's avatar
Sebastian Hack committed
746
	/* Set the register classes and constraints of the Call parameters. */
747
748
	for (i = 0; i < n_reg_params; ++i) {
		int index = reg_param_idxs[i];
749
		be_abi_call_arg_t *arg = get_call_arg(call, 0, index, 0);
Sebastian Hack's avatar
Sebastian Hack committed
750
		assert(arg->reg != NULL);
751

752
753
		be_set_constr_single_reg_in(low_call, be_pos_Call_first_arg + i,
		                            arg->reg, 0);
Sebastian Hack's avatar
Sebastian Hack committed
754
755
756
	}

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

Sebastian Hack's avatar
Sebastian Hack committed
762
		assert(arg->in_reg);
763
		be_set_constr_single_reg_out(low_call, pn, arg->reg, 0);
764
		arch_set_irn_register(proj, arg->reg);
Sebastian Hack's avatar
Sebastian Hack committed
765
	}
766
767
	exchange(irn, low_call);

Matthias Braun's avatar
Matthias Braun committed
768
	/* kill the ProjT node */
769
	if (res_proj != NULL) {
770
		kill_node(res_proj);
771
772
	}

Sebastian Hack's avatar
Sebastian Hack committed
773
774
	/* Make additional projs for the caller save registers
	   and the Keep node which keeps them alive. */
775
	{
776
		ir_node               **in, *keep;
777
778
		int                   i;
		int                   n = 0;
779
		int                   curr_res_proj = pn_be_Call_first_res + n_reg_results;
780
		int                   n_ins;
Sebastian Hack's avatar
Sebastian Hack committed
781

782
		n_ins = ARR_LEN(destroyed_regs) + n_reg_results + 1;
783
		in    = ALLOCAN(ir_node *, n_ins);
784

785
786
		/* also keep the stack pointer */
		set_irn_link(curr_sp, (void*) sp);
787
		in[n++] = curr_sp;
788

789
790
		for (i = 0; i < ARR_LEN(destroyed_regs); ++i) {
			const arch_register_t *reg = destroyed_regs[i];
791
			ir_node *proj = new_r_Proj(low_call, reg->reg_class->mode, curr_res_proj);
Sebastian Hack's avatar
Sebastian Hack committed
792

Sebastian Hack's avatar
Sebastian Hack committed
793
			/* memorize the register in the link field. we need afterwards to set the register class of the keep correctly. */
794
			be_set_constr_single_reg_out(low_call, curr_res_proj, reg, 0);
795
			arch_set_irn_register(proj, reg);
796

Matthias Braun's avatar
Matthias Braun committed
797
			set_irn_link(proj, (void*) reg);
798
			in[n++] = proj;
799
			++curr_res_proj;
Sebastian Hack's avatar
Sebastian Hack committed
800
801
		}

802
		for (i = 0; i < n_reg_results; ++i) {
Matthias Braun's avatar
Matthias Braun committed
803
			ir_node *proj = res_projs[i];
804
			const arch_register_t *reg = arch_get_irn_register(proj);
Matthias Braun's avatar
Matthias Braun committed
805
			set_irn_link(proj, (void*) reg);
806
			in[n++] = proj;
Matthias Braun's avatar
Matthias Braun committed
807
		}
808
		assert(n <= n_ins);
Matthias Braun's avatar
Matthias Braun committed
809

810
		/* create the Keep for the caller save registers */
811
		keep = be_new_Keep(bl, n, in);
812
		for (i = 0; i < n; ++i) {
Sebastian Hack's avatar
Sebastian Hack committed
813
			const arch_register_t *reg = get_irn_link(in[i]);
814
			be_node_set_reg_class_in(keep, i, reg->reg_class);
Sebastian Hack's avatar
Sebastian Hack committed
815
		}
Sebastian Hack's avatar
Sebastian Hack committed
816
817
818
	}

	/* Clean up the stack. */
819
820
821
	assert(stack_size >= call->pop);
	stack_size -= call->pop;

822
	if (stack_size > 0) {
823
824
825
826
		ir_node *mem_proj = NULL;

		foreach_out_edge(low_call, edge) {
			ir_node *irn = get_edge_src_irn(edge);
827
			if (is_Proj(irn) && get_Proj_proj(irn) == pn_Call_M) {
828
829
830
831
				mem_proj = irn;
				break;
			}
		}
Sebastian Hack's avatar
Sebastian Hack committed
832

833
		if (! mem_proj) {
834
			mem_proj = new_r_Proj(low_call, mode_M, pn_be_Call_M_regular);
Sebastian Hack's avatar
Sebastian Hack committed
835
836
			keep_alive(mem_proj);
		}
837
838
839
	}
	/* Clean up the stack frame or revert alignment fixes if we allocated it */
	if (! no_alloc) {
840
		curr_sp = be_new_IncSP(sp, bl, curr_sp, -stack_size, 0);
Sebastian Hack's avatar
Sebastian Hack committed
841
842
843
	}

	be_abi_call_free(call);
844

845
846
	DEL_ARR_F(states);
	DEL_ARR_F(destroyed_regs);
847
848
849
850

	return curr_sp;
}

851
852
853
854
855
/**
 * 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
856
 * @param block      the block where new nodes are allocated on
857
858
859
860
 * @param dbg        debug info for new nodes
 *
 * @return a node representing the aligned size
 */
861
static ir_node *adjust_alloc_size(unsigned stack_alignment, ir_node *size,
862
                                  ir_node *block, dbg_info *dbg)
863
{
864
	if (stack_alignment > 1) {
865
866
867
868
		ir_mode  *mode;
		tarval   *tv;
		ir_node  *mask;
		ir_graph *irg;
Michael Beck's avatar
Michael Beck committed
869

870
		assert(is_po2(stack_alignment));
871

Michael Beck's avatar
Michael Beck committed
872
873
		mode = get_irn_mode(size);
		tv   = new_tarval_from_long(stack_alignment-1, mode);
874
		irg  = get_Block_irg(block);
875
		mask = new_r_Const(irg, tv);
876
		size = new_rd_Add(dbg, block, size, mask, mode);
877
878

		tv   = new_tarval_from_long(-(long)stack_alignment, mode);
879
		mask = new_r_Const(irg, tv);
880
		size = new_rd_And(dbg, block, size, mask, mode);
881
882
883
	}
	return size;
}
884
885
886
887
/**
 * 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
888
static ir_node *adjust_alloc(be_abi_irg_t *env, ir_node *alloc, ir_node *curr_sp)
889
{
890
891
892
893
894
895
896
	ir_node          *block     = get_nodes_block(alloc);
	ir_graph         *irg       = get_Block_irg(block);