beabi.c 73.9 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
 */
Michael Beck's avatar
Michael Beck committed
26
#ifdef HAVE_CONFIG_H
27
# include "config.h"
Michael Beck's avatar
Michael Beck committed
28
#endif
29

Sebastian Hack's avatar
Sebastian Hack committed
30
#include "obst.h"
31
#include "offset.h"
Sebastian Hack's avatar
Sebastian Hack committed
32

33
#include "irgopt.h"
Sebastian Hack's avatar
Sebastian Hack committed
34
35
36
37
38
39
40

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

#include "be.h"
#include "beabi.h"
52
#include "bearch_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
53
#include "benode_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
54
#include "belive_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
55
#include "besched_t.h"
56
#include "beirg_t.h"
57
#include "bessaconstr.h"
Sebastian Hack's avatar
Sebastian Hack committed
58
59

typedef struct _be_abi_call_arg_t {
60
61
62
	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. */
Sebastian Hack's avatar
Sebastian Hack committed
63

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

struct _be_abi_call_t {
74
75
	be_abi_call_flags_t          flags;
	int                          pop;
76
77
78
79
	const be_abi_callbacks_t    *cb;
	ir_type                     *between_type;
	set                         *params;
	const arch_register_class_t *cls_addr;
Sebastian Hack's avatar
Sebastian Hack committed
80
81
82
};

struct _be_abi_irg_t {
Sebastian Hack's avatar
Sebastian Hack committed
83
	struct obstack       obst;
84
	be_irg_t             *birg;         /**< The back end IRG. */
85
	const arch_env_t     *arch_env;
Sebastian Hack's avatar
Sebastian Hack committed
86
87
	survive_dce_t        *dce_survivor;

Sebastian Hack's avatar
Sebastian Hack committed
88
	be_abi_call_t        *call;         /**< The ABI call information. */
89
	ir_type              *method_type;  /**< The type of the method of the IRG. */
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

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

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

100
101
	void                 *cb;           /**< ABI Callback self pointer. */

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

105
	ir_node              **calls;       /**< flexible array containing all be_Call nodes */
106

107
108
109
110
	arch_register_req_t  sp_req;
	arch_register_req_t  sp_cls_req;

	be_stack_layout_t    frame;         /**< The stack frame model. */
Matthias Braun's avatar
Matthias Braun committed
111

112
	DEBUG_ONLY(firm_dbg_module_t    *dbg;)  /**< The debugging module. */
Sebastian Hack's avatar
Sebastian Hack committed
113
};
Sebastian Hack's avatar
Sebastian Hack committed
114

115
static heights_t *ir_heights;
116

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

120
121
122
/** 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
123
124
125
126
127
128
/*
     _    ____ ___    ____      _ _ _                _
    / \  | __ )_ _|  / ___|__ _| | | |__   __ _  ___| | _____
   / _ \ |  _ \| |  | |   / _` | | | '_ \ / _` |/ __| |/ / __|
  / ___ \| |_) | |  | |__| (_| | | | |_) | (_| | (__|   <\__ \
 /_/   \_\____/___|  \____\__,_|_|_|_.__/ \__,_|\___|_|\_\___/
129

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

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

Michael Beck's avatar
Michael Beck committed
144
145
146
147
148
149
150
151
/**
 * Get or set an ABI call object argument.
 *
 * @param call      the abi call
 * @param is_res    true for call results, false for call arguments
 * @param pos       position of the argument
 * @param do_insert true if the argument is set, false if it's retrieved
 */
Sebastian Hack's avatar
Sebastian Hack committed
152
153
154
155
156
static be_abi_call_arg_t *get_or_set_call_arg(be_abi_call_t *call, int is_res, int pos, int do_insert)
{
	be_abi_call_arg_t arg;
	unsigned hash;

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

Michael Beck's avatar
Michael Beck committed
161
	hash = is_res * 128 + pos;
Sebastian Hack's avatar
Sebastian Hack committed
162
163
164
165
166
167

	return do_insert
		? set_insert(call->params, &arg, sizeof(arg), hash)
		: set_find(call->params, &arg, sizeof(arg), hash);
}

Michael Beck's avatar
Michael Beck committed
168
169
170
171
172
173
174
/**
 * Retrieve an ABI call object argument.
 *
 * @param call      the ABI call object
 * @param is_res    true for call results, false for call arguments
 * @param pos       position of the argument
 */
Sebastian Hack's avatar
Sebastian Hack committed
175
176
177
178
179
static INLINE be_abi_call_arg_t *get_call_arg(be_abi_call_t *call, int is_res, int pos)
{
	return get_or_set_call_arg(call, is_res, pos, 0);
}

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
}

187
188
189
190
191
void be_abi_call_set_pop(be_abi_call_t *call, int pop)
{
	assert(pop >= 0);
	call->pop = pop;
}
192
193
194
195
196
197
198
199

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


200
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)
Sebastian Hack's avatar
Sebastian Hack committed
201
202
{
	be_abi_call_arg_t *arg = get_or_set_call_arg(call, 0, arg_pos, 1);
203
	arg->on_stack     = 1;
204
	arg->load_mode    = load_mode;
205
206
207
	arg->alignment    = alignment;
	arg->space_before = space_before;
	arg->space_after  = space_after;
208
	assert(alignment > 0 && "Alignment must be greater than 0");
Sebastian Hack's avatar
Sebastian Hack committed
209
210
211
212
213
}

void be_abi_call_param_reg(be_abi_call_t *call, int arg_pos, const arch_register_t *reg)
{
	be_abi_call_arg_t *arg = get_or_set_call_arg(call, 0, arg_pos, 1);
214
	arg->in_reg = 1;
Sebastian Hack's avatar
Sebastian Hack committed
215
216
217
218
219
220
	arg->reg = reg;
}

void be_abi_call_res_reg(be_abi_call_t *call, int arg_pos, const arch_register_t *reg)
{
	be_abi_call_arg_t *arg = get_or_set_call_arg(call, 1, arg_pos, 1);
221
	arg->in_reg = 1;
Sebastian Hack's avatar
Sebastian Hack committed
222
223
224
	arg->reg = reg;
}

Michael Beck's avatar
Michael Beck committed
225
/* Get the flags of a ABI call object. */
226
227
228
229
230
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
231
232
233
234
235
/**
 * Constructor for a new ABI call object.
 *
 * @return the new ABI call object
 */
236
static be_abi_call_t *be_abi_call_new(const arch_register_class_t *cls_addr)
Sebastian Hack's avatar
Sebastian Hack committed
237
{
238
	be_abi_call_t *call = XMALLOCZ(be_abi_call_t);
239

Sebastian Hack's avatar
Sebastian Hack committed
240
241
242
	call->flags.val  = 0;
	call->params     = new_set(cmp_call_arg, 16);
	call->cb         = NULL;
243
	call->cls_addr   = cls_addr;
244

245
	call->flags.bits.try_omit_fp = be_omit_fp | be_omit_leaf_fp;
246

Sebastian Hack's avatar
Sebastian Hack committed
247
248
249
	return call;
}

Michael Beck's avatar
Michael Beck committed
250
251
252
253
/**
 * Destructor for an ABI call object.
 */
static void be_abi_call_free(be_abi_call_t *call)
Sebastian Hack's avatar
Sebastian Hack committed
254
255
256
257
258
{
	del_set(call->params);
	free(call);
}

Sebastian Hack's avatar
Sebastian Hack committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
  _____                           _   _                 _ _ _
 |  ___| __ __ _ _ __ ___   ___  | | | | __ _ _ __   __| | (_)_ __   __ _
 | |_ | '__/ _` | '_ ` _ \ / _ \ | |_| |/ _` | '_ \ / _` | | | '_ \ / _` |
 |  _|| | | (_| | | | | | |  __/ |  _  | (_| | | | | (_| | | | | | | (_| |
 |_|  |_|  \__,_|_| |_| |_|\___| |_| |_|\__,_|_| |_|\__,_|_|_|_| |_|\__, |
                                                                    |___/

  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.
*/

276
277
static int get_stack_entity_offset(be_stack_layout_t *frame, ir_entity *ent,
                                   int bias)
278
{
279
	ir_type *t = get_entity_owner(ent);
280
	int ofs    = get_entity_offset(ent);
281

282
	int index;
283
284

	/* Find the type the entity is contained in. */
285
286
	for (index = 0; index < N_FRAME_TYPES; ++index) {
		if (frame->order[index] == t)
287
			break;
288
289
		/* 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]);
290
291
292
293
294
295
296
297
298
299
300
	}

	/* 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
301
302
303
/**
 * Retrieve the entity with given offset from a frame type.
 */
304
static ir_entity *search_ent_with_offset(ir_type *t, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
305
306
307
{
	int i, n;

308
	for(i = 0, n = get_compound_n_members(t); i < n; ++i) {
309
		ir_entity *ent = get_compound_member(t, i);
310
		if (get_entity_offset(ent) == offset)
Sebastian Hack's avatar
Sebastian Hack committed
311
312
313
314
315
316
			return ent;
	}

	return NULL;
}

317
static int stack_frame_compute_initial_offset(be_stack_layout_t *frame)
318
{
319
320
	ir_type  *base = frame->stack_dir < 0 ? frame->between_type : frame->frame_type;
	ir_entity *ent = search_ent_with_offset(base, 0);
321
322
323

	frame->initial_offset = ent ? get_stack_entity_offset(frame, ent, 0) : 0;

324
325
326
	return frame->initial_offset;
}

327
328
329
330
331
332
333
334
/**
 * 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
 * @param stack_dir the stack direction
335
 * @param param_map an array mapping method argument positions to the stack argument type
336
337
338
339
 *
 * @return the initialized stack layout
 */
static be_stack_layout_t *stack_frame_init(be_stack_layout_t *frame, ir_type *args,
340
                                           ir_type *between, ir_type *locals, int stack_dir,
341
                                           ir_entity *param_map[])
342
343
344
345
346
{
	frame->arg_type       = args;
	frame->between_type   = between;
	frame->frame_type     = locals;
	frame->initial_offset = 0;
Matthias Braun's avatar
Matthias Braun committed
347
	frame->initial_bias   = 0;
348
349
	frame->stack_dir      = stack_dir;
	frame->order[1]       = between;
350
	frame->param_map      = param_map;
351
352
353
354
355
356
357
358
359
360
361
362

	if(stack_dir > 0) {
		frame->order[0] = args;
		frame->order[2] = locals;
	}
	else {
		frame->order[0] = locals;
		frame->order[2] = args;
	}
	return frame;
}

363
#if 0
364
365
/** Dumps the stack layout to file. */
static void stack_layout_dump(FILE *file, be_stack_layout_t *frame)
Sebastian Hack's avatar
Sebastian Hack committed
366
367
368
369
{
	int i, j, n;

	ir_fprintf(file, "initial offset: %d\n", frame->initial_offset);
370
371
	for (j = 0; j < N_FRAME_TYPES; ++j) {
		ir_type *t = frame->order[j];
Sebastian Hack's avatar
Sebastian Hack committed
372

373
374
		ir_fprintf(file, "type %d: %F size: %d\n", j, t, get_type_size_bytes(t));
		for (i = 0, n = get_compound_n_members(t); i < n; ++i) {
375
			ir_entity *ent = get_compound_member(t, i);
Sebastian Hack's avatar
Sebastian Hack committed
376
377
378
379
			ir_fprintf(file, "\t%F int ofs: %d glob ofs: %d\n", ent, get_entity_offset_bytes(ent), get_stack_entity_offset(frame, ent, 0));
		}
	}
}
380
#endif
Sebastian Hack's avatar
Sebastian Hack committed
381

Michael Beck's avatar
Michael Beck committed
382
383
384
385
/**
 * Returns non-zero if the call argument at given position
 * is transfered on the stack.
 */
Sebastian Hack's avatar
Sebastian Hack committed
386
387
388
389
390
391
static INLINE int is_on_stack(be_abi_call_t *call, int pos)
{
	be_abi_call_arg_t *arg = get_call_arg(call, 0, pos);
	return arg && !arg->in_reg;
}

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
	ir_graph *irg              = env->birg->irg;
414
	const arch_env_t *arch_env = env->birg->main_env->arch_env;
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
421
422
423
	ir_node *curr_mem          = get_Call_mem(irn);
	ir_node *bl                = get_nodes_block(irn);
	pset *results              = pset_new_ptr(8);
	pset *caller_save          = pset_new_ptr(8);
	pset *states               = pset_new_ptr(2);
	int stack_size             = 0;
424
425
	int stack_dir              = arch_env_stack_dir(arch_env);
	const arch_register_t *sp  = arch_env_sp(arch_env);
426
427
428
429
	be_abi_call_t *call        = be_abi_call_new(sp->reg_class);
	ir_mode *mach_mode         = sp->reg_class->mode;
	struct obstack *obst       = &env->obst;
	int no_alloc               = call->flags.bits.frame_is_setup_on_call;
430
	int n_res                  = get_method_n_ress(call_tp);
431
	int do_seq                 = call->flags.bits.store_args_sequential && !no_alloc;
432
433
434
435
436

	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
437
438
439

	ir_node *low_call;
	ir_node **in;
440
	ir_node **res_projs;
Matthias Braun's avatar
Matthias Braun committed
441
	int      n_reg_results = 0;
442
	const arch_register_t *reg;
Sebastian Hack's avatar
Sebastian Hack committed
443
	const ir_edge_t *edge;
444
445
	int *reg_param_idxs;
	int *stack_param_idx;
Sebastian Hack's avatar
Sebastian Hack committed
446
447
448
	int i, n;

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

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

			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);
Sebastian Hack's avatar
Sebastian Hack committed
462
			obstack_int_grow(obst, i);
463
			++n_stack_params;
Sebastian Hack's avatar
Sebastian Hack committed
464
465
		}
	}
466
	stack_param_idx = obstack_finish(obst);
Sebastian Hack's avatar
Sebastian Hack committed
467
468

	/* Collect all arguments which are passed in registers. */
469
	for (i = 0; i < n_params; ++i) {
Sebastian Hack's avatar
Sebastian Hack committed
470
		be_abi_call_arg_t *arg = get_call_arg(call, 0, i);
471
		if (arg && arg->in_reg) {
Sebastian Hack's avatar
Sebastian Hack committed
472
			obstack_int_grow(obst, i);
473
			++n_reg_params;
Sebastian Hack's avatar
Sebastian Hack committed
474
475
		}
	}
476
	reg_param_idxs = obstack_finish(obst);
Sebastian Hack's avatar
Sebastian Hack committed
477

478
479
480
481
482
483
484
485
486
487
488
489
490
	/*
	 * 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) {
		curr_sp = be_new_IncSP(sp, irg, bl, curr_sp, stack_size, 1);
	}

Sebastian Hack's avatar
Sebastian Hack committed
491
	/* If there are some parameters which shall be passed on the stack. */
492
	if (n_stack_params > 0) {
Sebastian Hack's avatar
Sebastian Hack committed
493
494
		int curr_ofs      = 0;

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
			obstack_ptr_grow(obst, curr_mem);
512
513
		}

514
515
		for (i = 0; i < n_stack_params; ++i) {
			int p                  = stack_param_idx[i];
516
517
518
519
			be_abi_call_arg_t *arg = get_call_arg(call, 0, p);
			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
				addr = curr_sp = be_new_IncSP(sp, irg, bl, curr_sp, param_size + arg->space_before, 0);
Sebastian Hack's avatar
Sebastian Hack committed
531
				add_irn_dep(curr_sp, curr_mem);
532
533
534
535
536
537
			}
			else {
				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
540
541
542
543
					ir_mode *constmode = mach_mode;
					if(mode_is_reference(mach_mode)) {
						constmode = mode_Is;
					}
					addr = new_r_Const_long(irg, bl, constmode, curr_ofs);
544
545
					addr = new_r_Add(irg, bl, curr_sp, addr, mach_mode);
				}
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
552
				ir_node *mem_input = do_seq ? curr_mem : new_NoMem();
				store = new_r_Store(irg, bl, mem_input, addr, param);
553
				mem = new_r_Proj(irg, bl, store, mode_M, pn_Store_M);
Sebastian Hack's avatar
Sebastian Hack committed
554
555
			}

556
			/* Make a mem copy for compound arguments. */
Sebastian Hack's avatar
Sebastian Hack committed
557
			else {
558
559
				ir_node *copy;

Sebastian Hack's avatar
Sebastian Hack committed
560
				assert(mode_is_reference(get_irn_mode(param)));
561
562
				copy = new_r_CopyB(irg, bl, curr_mem, addr, param, param_type);
				mem = new_r_Proj(irg, bl, copy, mode_M, pn_CopyB_M_regular);
Sebastian Hack's avatar
Sebastian Hack committed
563
564
565
566
			}

			curr_ofs += param_size;

567
			if (do_seq)
Sebastian Hack's avatar
Sebastian Hack committed
568
				curr_mem = mem;
569
570
			else
				obstack_ptr_grow(obst, mem);
Sebastian Hack's avatar
Sebastian Hack committed
571
572
573
574
575
		}

		in = (ir_node **) obstack_finish(obst);

		/* We need the sync only, if we didn't build the stores sequentially. */
576
577
578
		if (! do_seq) {
			if (n_stack_params >= 1) {
				curr_mem = new_r_Sync(irg, bl, n_stack_params + 1, in);
579
580
581
582
			} else {
				curr_mem = get_Call_mem(irn);
			}
		}
Sebastian Hack's avatar
Sebastian Hack committed
583
584
585
586
		obstack_free(obst, in);
	}

	/* Collect caller save registers */
587
	for (i = 0, n = arch_env_get_n_reg_class(arch_env); i < n; ++i) {
588
		unsigned j;
589
		const arch_register_class_t *cls = arch_env_get_reg_class(arch_env, i);
590
		for (j = 0; j < cls->n_regs; ++j) {
Sebastian Hack's avatar
Sebastian Hack committed
591
			const arch_register_t *reg = arch_register_for_index(cls, j);
592
			if (arch_register_type_is(reg, caller_save)) {
Sebastian Hack's avatar
Sebastian Hack committed
593
				pset_insert_ptr(caller_save, (void *) reg);
594
			}
595
			if (arch_register_type_is(reg, state)) {
596
597
598
				pset_insert_ptr(caller_save, (void*) reg);
				pset_insert_ptr(states, (void*) reg);
			}
Sebastian Hack's avatar
Sebastian Hack committed
599
600
		}
	}
Sebastian Hack's avatar
Sebastian Hack committed
601
602

	/* search the greatest result proj number */
Michael Beck's avatar
Michael Beck committed
603

Matthias Braun's avatar
Matthias Braun committed
604
605
606
	res_projs = alloca(n_res * sizeof(res_projs[0]));
	memset(res_projs, 0, n_res * sizeof(res_projs[0]));

Sebastian Hack's avatar
Sebastian Hack committed
607
608
	foreach_out_edge(irn, edge) {
		const ir_edge_t *res_edge;
Matthias Braun's avatar
Matthias Braun committed
609
		ir_node         *irn = get_edge_src_irn(edge);
Sebastian Hack's avatar
Sebastian Hack committed
610

Matthias Braun's avatar
Matthias Braun committed
611
612
613
614
615
616
617
618
619
620
621
		if(!is_Proj(irn) || get_Proj_proj(irn) != pn_Call_T_result)
			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
622
			assert(res_projs[proj] == NULL);
Matthias Braun's avatar
Matthias Braun committed
623
			res_projs[proj] = res;
Sebastian Hack's avatar
Sebastian Hack committed
624
		}
Matthias Braun's avatar
Matthias Braun committed
625
626
		res_proj = irn;
		break;
Sebastian Hack's avatar
Sebastian Hack committed
627
	}
628

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

634
	/* make the back end call node and set its register requirements. */
635
636
	for (i = 0; i < n_reg_params; ++i) {
		obstack_ptr_grow(obst, get_Call_param(irn, reg_param_idxs[i]));
637
638
639
640
641
642
643
644
645
646
	}
	foreach_pset(states, reg) {
		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
		ir_node *regnode = new_rd_Unknown(irg, arch_register_class_mode(cls));
		obstack_ptr_grow(obst, regnode);
	}
647
	n_ins = n_reg_params + pset_count(states);
648
649

	in = obstack_finish(obst);
650

651
652
	if (env->call->flags.bits.call_has_imm && is_SymConst(call_ptr)) {
		/* direct call */
653
654
		low_call = be_new_Call(get_irn_dbg_info(irn), irg, bl, curr_mem,
		                       curr_sp, curr_sp,
Matthias Braun's avatar
Matthias Braun committed
655
656
		                       n_reg_results + pn_be_Call_first_res + pset_count(caller_save),
		                       n_ins, in, get_Call_type(irn));
Sebastian Hack's avatar
Sebastian Hack committed
657
		be_Call_set_entity(low_call, get_SymConst_entity(call_ptr));
658
	} else {
659
		/* indirect call */
660
661
		low_call = be_new_Call(get_irn_dbg_info(irn), irg, bl, curr_mem,
		                       curr_sp, call_ptr,
Matthias Braun's avatar
Matthias Braun committed
662
		                       n_reg_results + pn_be_Call_first_res + pset_count(caller_save),
663
		                       n_ins, in, get_Call_type(irn));
Sebastian Hack's avatar
Sebastian Hack committed
664
	}
665
	be_Call_set_pop(low_call, call->pop);
666
	ARR_APP1(ir_node *, env->calls, low_call);
667

Matthias Braun's avatar
Matthias Braun committed
668
669
670
671
672
673
674
675
	/* create new stack pointer */
	curr_sp = new_r_Proj(irg, bl, low_call, get_irn_mode(curr_sp),
	                     pn_be_Call_sp);
	be_set_constr_single_reg(low_call, BE_OUT_POS(pn_be_Call_sp), sp);
	arch_set_irn_register(arch_env, curr_sp, sp);
	be_node_set_flags(low_call, BE_OUT_POS(pn_be_Call_sp),
			arch_irn_flags_ignore | arch_irn_flags_modify_sp);

Matthias Braun's avatar
Matthias Braun committed
676
677
678
679
680
	for(i = 0; i < n_res; ++i) {
		int pn;
		ir_node           *proj = res_projs[i];
		be_abi_call_arg_t *arg  = get_call_arg(call, 1, i);

Matthias Braun's avatar
Matthias Braun committed
681
682
683
		/* returns values on stack not supported yet */
		assert(arg->in_reg);

Matthias Braun's avatar
Matthias Braun committed
684
685
686
687
688
689
690
691
		/*
			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;

		if(proj == NULL) {
692
			ir_type *res_type = get_method_res_type(call_tp, i);
Matthias Braun's avatar
Matthias Braun committed
693
694
695
696
697
698
699
700
701
702
703
704
705
			ir_mode *mode     = get_type_mode(res_type);
			proj              = new_r_Proj(irg, bl, low_call, mode, pn);
			res_projs[i]      = proj;
		} else {
			set_Proj_pred(proj, low_call);
			set_Proj_proj(proj, pn);
		}

		if (arg->in_reg) {
			pset_remove_ptr(caller_save, arg->reg);
		}
	}

706
	/*
707
708
		Set the register class of the call address to
		the backend provided class (default: stack pointer class)
709
	*/
710
	be_node_set_reg_class(low_call, be_pos_Call_ptr, call->cls_addr);
711

712
713
	DBG((env->dbg, LEVEL_3, "\tcreated backend call %+F\n", low_call));

Sebastian Hack's avatar
Sebastian Hack committed
714
	/* Set the register classes and constraints of the Call parameters. */
715
716
	for (i = 0; i < n_reg_params; ++i) {
		int index = reg_param_idxs[i];
Sebastian Hack's avatar
Sebastian Hack committed
717
718
		be_abi_call_arg_t *arg = get_call_arg(call, 0, index);
		assert(arg->reg != NULL);
719

Matthias Braun's avatar
Matthias Braun committed
720
		be_set_constr_single_reg(low_call, be_pos_Call_first_arg + i, arg->reg);
Sebastian Hack's avatar
Sebastian Hack committed
721
722
723
	}

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

Sebastian Hack's avatar
Sebastian Hack committed
729
		assert(arg->in_reg);
730
		be_set_constr_single_reg(low_call, BE_OUT_POS(pn), arg->reg);
Matthias Braun's avatar
Matthias Braun committed
731
		arch_set_irn_register(arch_env, proj, arg->reg);
Sebastian Hack's avatar
Sebastian Hack committed
732
	}
733
734
735
	obstack_free(obst, in);
	exchange(irn, low_call);

Matthias Braun's avatar
Matthias Braun committed
736
	/* kill the ProjT node */
737
	if (res_proj != NULL) {
738
		kill_node(res_proj);
739
740
	}

Sebastian Hack's avatar
Sebastian Hack committed
741
742
	/* Make additional projs for the caller save registers
	   and the Keep node which keeps them alive. */
743
	if (1 || pset_count(caller_save) + n_reg_results > 0) {
Sebastian Hack's avatar
Sebastian Hack committed
744
		const arch_register_t *reg;
745
		ir_node               **in, *keep;
746
747
		int                   i;
		int                   n = 0;
Matthias Braun's avatar
Matthias Braun committed
748
749
		int                   curr_res_proj
			= pn_be_Call_first_res + n_reg_results;
Sebastian Hack's avatar
Sebastian Hack committed
750

751
752
753
754
755
756
		/* also keep the stack pointer */
		++n;
		set_irn_link(curr_sp, (void*) sp);
		obstack_ptr_grow(obst, curr_sp);

		for (reg = pset_first(caller_save); reg; reg = pset_next(caller_save), ++n) {
Matthias Braun's avatar
Matthias Braun committed
757
758
			ir_node *proj = new_r_Proj(irg, bl, low_call, reg->reg_class->mode,
			                           curr_res_proj);
Sebastian Hack's avatar
Sebastian Hack committed
759

Sebastian Hack's avatar
Sebastian Hack committed
760
			/* memorize the register in the link field. we need afterwards to set the register class of the keep correctly. */
761
			be_set_constr_single_reg(low_call, BE_OUT_POS(curr_res_proj), reg);
Matthias Braun's avatar
Matthias Braun committed
762
			arch_set_irn_register(arch_env, proj, reg);
763
764
765

			/* a call can produce ignore registers, in this case set the flag and register for the Proj */
			if (arch_register_type_is(reg, ignore)) {
Matthias Braun's avatar
Matthias Braun committed
766
767
				be_node_set_flags(low_call, BE_OUT_POS(curr_res_proj),
				                  arch_irn_flags_ignore);
768
769
			}

Matthias Braun's avatar
Matthias Braun committed
770
			set_irn_link(proj, (void*) reg);
Sebastian Hack's avatar
Sebastian Hack committed
771
			obstack_ptr_grow(obst, proj);
772
			curr_res_proj++;
Sebastian Hack's avatar
Sebastian Hack committed
773
774
		}

Matthias Braun's avatar
Matthias Braun committed
775
776
777
778
779
780
781
782
		for(i = 0; i < n_reg_results; ++i) {
			ir_node *proj = res_projs[i];
			const arch_register_t *reg = arch_get_irn_register(arch_env, proj);
			set_irn_link(proj, (void*) reg);
			obstack_ptr_grow(obst, proj);
		}
		n += n_reg_results;

783
		/* create the Keep for the caller save registers */
Sebastian Hack's avatar
Sebastian Hack committed
784
785
		in   = (ir_node **) obstack_finish(obst);
		keep = be_new_Keep(NULL, irg, bl, n, in);
786
		for (i = 0; i < n; ++i) {
Sebastian Hack's avatar
Sebastian Hack committed
787
788
789
			const arch_register_t *reg = get_irn_link(in[i]);
			be_node_set_reg_class(keep, i, reg->reg_class);
		}
Sebastian Hack's avatar
Sebastian Hack committed
790
791
792
793
		obstack_free(obst, in);
	}

	/* Clean up the stack. */
794
795
796
	assert(stack_size >= call->pop);
	stack_size -= call->pop;

797
	if (stack_size > 0) {
798
799
800
801
802
803
804
805
806
		ir_node *mem_proj = NULL;

		foreach_out_edge(low_call, edge) {
			ir_node *irn = get_edge_src_irn(edge);
			if(is_Proj(irn) && get_Proj_proj(irn) == pn_Call_M) {
				mem_proj = irn;
				break;
			}
		}
Sebastian Hack's avatar
Sebastian Hack committed
807

808
		if (! mem_proj) {
809
			mem_proj = new_r_Proj(irg, bl, low_call, mode_M, pn_be_Call_M_regular);
Sebastian Hack's avatar
Sebastian Hack committed
810
811
			keep_alive(mem_proj);
		}
812
813
814
815
	}
	/* Clean up the stack frame or revert alignment fixes if we allocated it */
	if (! no_alloc) {
		curr_sp = be_new_IncSP(sp, irg, bl, curr_sp, -stack_size, 0);
Sebastian Hack's avatar
Sebastian Hack committed
816
817
818
	}

	be_abi_call_free(call);
819
	obstack_free(obst, stack_param_idx);
Sebastian Hack's avatar
Sebastian Hack committed
820
	del_pset(results);
821
	del_pset(states);
Sebastian Hack's avatar
Sebastian Hack committed
822
	del_pset(caller_save);
823
824
825
826

	return curr_sp;
}

827
828
829
830
831
832
833
834
835
836
837
/**
 * 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
 * @param irg        the irg where new nodes are allocated on
 * @param irg        the block where new nodes are allocated on
 * @param dbg        debug info for new nodes
 *
 * @return a node representing the aligned size
 */
838
839
840
static ir_node *adjust_alloc_size(unsigned stack_alignment, ir_node *size,
                                  ir_graph *irg, ir_node *block, dbg_info *dbg)
{
841
	if (stack_alignment > 1) {
Michael Beck's avatar
Michael Beck committed
842
843
844
845
		ir_mode *mode;
		tarval  *tv;
		ir_node *mask;

846
		assert(is_po2(stack_alignment));
847

Michael Beck's avatar
Michael Beck committed
848
849
850
		mode = get_irn_mode(size);
		tv   = new_tarval_from_long(stack_alignment-1, mode);
		mask = new_r_Const(irg, block, mode, tv);
851
852
853
854
855
856
857
858
		size = new_rd_Add(dbg, irg, block, size, mask, mode);

		tv   = new_tarval_from_long(-(long)stack_alignment, mode);
		mask = new_r_Const(irg, block, mode, tv);
		size = new_rd_And(dbg, irg, block, size, mask, mode);
	}
	return size;
}
859
860
861
862
/**
 * 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
863
static ir_node *adjust_alloc(be_abi_irg_t *env, ir_node *alloc, ir_node *curr_sp)
864
{
865
866
867
868
869
	ir_node *block;
	ir_graph *irg;
	ir_node *alloc_mem;
	ir_node *alloc_res;
	ir_type *type;
870
	dbg_info *dbg;
871

872
	const ir_edge_t *edge;
Matthias Braun's avatar
Matthias Braun committed
873
	ir_node *new_alloc, *size, *addr, *ins[2];
874
	unsigned stack_alignment;
875

876
	assert(get_Alloc_where(alloc) == stack_alloc);
877

878
879
880
881
882
	block = get_nodes_block(alloc);
	irg = get_irn_irg(block);
	alloc_mem = NULL;
	alloc_res = NULL;
	type = get_Alloc_type(alloc);
883

884
885
	foreach_out_edge(alloc, edge) {
		ir_node *irn = get_edge_src_irn(edge);
886

887
		assert(is_Proj(irn));
888
		switch (get_Proj_proj(irn)) {
889
890
891
892
893
894
895
896
		case pn_Alloc_M:
			alloc_mem = irn;
			break;
		case pn_Alloc_res:
			alloc_res = irn;
			break;
		default:
			break;
897
		}
898
	}
899

900
901
902
903
904
905
906
907
	/* 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;
	}

908
909
	dbg = get_irn_dbg_info(alloc);

910
	/* we might need to multiply the size with the element size */
911
	if (type != firm_unknown_type && get_type_size_bytes(type) != 1) {
912
913
		tarval *tv    = new_tarval_from_long(get_type_size_bytes(type),
		                                     mode_Iu);
914
		ir_node *cnst = new_rd_Const(dbg, irg, block, mode_Iu, tv);
915
916
		ir_node *mul  = new_rd_Mul(dbg, irg, block, get_Alloc_size(alloc),
		                           cnst, mode_Iu);
917
918
919
920
		size = mul;
	} else {
		size = get_Alloc_size(alloc);
	}
921

922
923
924
	/* The stack pointer will be modified in an unknown manner.
	   We cannot omit it. */
	env->call->flags.bits.try_omit_fp = 0;
925

926
	stack_alignment = 1 << env->arch_env->stack_alignment;
927
	size            = adjust_alloc_size(stack_alignment, size, irg, block, dbg);
928
	new_alloc       = be_new_AddSP(env->arch_env->sp, irg, block, curr_sp, size);
929
	set_irn_dbg_info(new_alloc, dbg);
930

931
932
933
	if(alloc_mem != NULL) {
		ir_node *addsp_mem;
		ir_node *sync;
934

935
		addsp_mem = new_r_Proj(irg, block, new_alloc, mode_M, pn_be_AddSP_M);
936

937
938
		/* We need to sync the output mem of the AddSP with the input mem
		   edge into the alloc node. */
939
940
941
		ins[0] = get_Alloc_mem(alloc);
		ins[1] = addsp_mem;
		sync = new_r_Sync(irg, block, 2, ins);
942

943
944
		exchange(alloc_mem, sync);
	}
945

946
	exchange(alloc, new_alloc);
947

948
949
	/* fix projnum of alloca res */
	set_Proj_proj(alloc_res, pn_be_AddSP_res);
950

951
952
953
	addr    = alloc_res;
	curr_sp = new_r_Proj(irg, block, new_alloc,  get_irn_mode(curr_sp),
	                     pn_be_AddSP_sp);
954

955
956
957
958
959
960
961
962
963
	return curr_sp;
}  /* adjust_alloc */

/**
 * Adjust a Free.
 * The Free is transformed into a back end free node and connected to the stack nodes.
 */
static ir_node *adjust_free(be_abi_irg_t *env, ir_node *free, ir_node *curr_sp)
{
964
965
	ir_node *block;
	ir_graph *irg;
966
	ir_node *subsp, *mem, *res, *size, *sync;
967
	ir_type *type;
968
	ir_node *in[2];
969
	ir_mode *sp_mode;
970
971
	unsigned stack_alignment;
	dbg_info *dbg;
972

973
	assert(get_Free_where(free) == stack_alloc);
974

975
976
977
	block = get_nodes_block(free);
	irg = get_irn_irg(block);
	type = get_Free_type(free);
978
	sp_mode = env->arch_env->sp->reg_class->mode;
979
	dbg = get_irn_dbg_info(free);
980
981

	/* we might need to multiply the size with the element size */