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

20
21
/**
 * @file
Michael Beck's avatar
Michael Beck committed
22
23
 * @brief   The codegenerator (transform FIRM into arm FIRM)
 * @author  Oliver Richter, Tobias Gneist, Michael Beck
24
25
 * @version $Id$
 */
26
27
28
29
30
31
32
33
34
35
36
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "irnode_t.h"
#include "irgraph_t.h"
#include "irmode_t.h"
#include "irgmod.h"
#include "iredges.h"
#include "irvrfy.h"
#include "ircons.h"
Michael Beck's avatar
Michael Beck committed
37
#include "irprintf.h"
38
39
40
41
42
#include "dbginfo.h"
#include "iropt_t.h"
#include "debug.h"

#include "../benode_t.h"
Michael Beck's avatar
Michael Beck committed
43
#include "../beirg_t.h"
44
45
46
#include "bearch_arm_t.h"

#include "arm_nodes_attr.h"
47
#include "archop.h"
48
49
50
51
52
53
54
55
#include "arm_transform.h"
#include "arm_new_nodes.h"
#include "arm_map_regs.h"

#include "gen_arm_regalloc_if.h"

#include <limits.h>

Michael Beck's avatar
Michael Beck committed
56

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
extern ir_op *get_op_Mulh(void);



/****************************************************************************************************
 *                  _        _                        __                           _   _
 *                 | |      | |                      / _|                         | | (_)
 *  _ __   ___   __| | ___  | |_ _ __ __ _ _ __  ___| |_ ___  _ __ _ __ ___   __ _| |_ _  ___  _ __
 * | '_ \ / _ \ / _` |/ _ \ | __| '__/ _` | '_ \/ __|  _/ _ \| '__| '_ ` _ \ / _` | __| |/ _ \| '_ \
 * | | | | (_) | (_| |  __/ | |_| | | (_| | | | \__ \ || (_) | |  | | | | | | (_| | |_| | (_) | | | |
 * |_| |_|\___/ \__,_|\___|  \__|_|  \__,_|_| |_|___/_| \___/|_|  |_| |_| |_|\__,_|\__|_|\___/|_| |_|
 *
 ****************************************************************************************************/

typedef struct vals_ {
Michael Beck's avatar
Michael Beck committed
72
73
74
	int ops;
	unsigned char values[4];
	unsigned char shifts[4];
75
76
} vals;

77
78
79
80
81
82
83
84
/** Execute ROL. */
static unsigned do_rol(unsigned v, unsigned rol) {
	return (v << rol) | (v >> (32 - rol));
}

/**
 * construct 8bit values und rot amounts for a value
 */
Michael Beck's avatar
Michael Beck committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
static void gen_vals_from_word(unsigned int value, vals *result)
{
	int initial = 0;

	memset(result, 0, sizeof(*result));

	/* special case: we prefer shift amount 0 */
	if (value < 0x100) {
		result->values[0] = value;
		result->ops       = 1;
		return;
	}

	while (value != 0) {
		if (value & 0xFF) {
			unsigned v = do_rol(value, 8) & 0xFFFFFF;
			int shf = 0;
			for (;;) {
				if ((v & 3) != 0)
					break;
				shf += 2;
				v >>= 2;
			}
			v  &= 0xFF;
			shf = (initial + shf - 8) & 0x1F;
			result->values[result->ops] = v;
			result->shifts[result->ops] = shf;
			++result->ops;

			value ^= do_rol(v, shf) >> initial;
		}
		else {
			value >>= 8;
			initial += 8;
		}
	}
121
122
}

Michael Beck's avatar
Michael Beck committed
123
124
125
/**
 * Creates a arm_Const node.
 */
Michael Beck's avatar
Michael Beck committed
126
static ir_node *create_const_node(be_abi_irg_t *abi, ir_node *irn, ir_node *block, long value) {
127
128
	tarval *tv = new_tarval_from_long(value, mode_Iu);
	dbg_info *dbg = get_irn_dbg_info(irn);
Michael Beck's avatar
Michael Beck committed
129
130
131
132
	ir_node *res = new_rd_arm_Mov_i(dbg, current_ir_graph, block, get_irn_mode(irn), tv);
	/* ensure the const is schedules AFTER the barrier */
	add_irn_dep(res, be_abi_get_start_barrier(abi));
	return res;
133
134
}

Michael Beck's avatar
Michael Beck committed
135
136
137
/**
 * Creates a arm_Const_Neg node.
 */
Michael Beck's avatar
Michael Beck committed
138
static ir_node *create_const_neg_node(be_abi_irg_t *abi, ir_node *irn, ir_node *block, long value) {
139
140
	tarval *tv = new_tarval_from_long(value, mode_Iu);
	dbg_info *dbg = get_irn_dbg_info(irn);
Michael Beck's avatar
Michael Beck committed
141
142
143
144
	ir_node *res = new_rd_arm_Mvn_i(dbg, current_ir_graph, block, get_irn_mode(irn), tv);
	add_irn_dep(res, be_abi_get_start_barrier(abi));
	/* ensure the const is schedules AFTER the barrier */
	return res;
145
146
}

147
#define NEW_BINOP_NODE(opname, env, op1, op2) new_rd_arm_##opname(env->dbg, current_ir_graph, env->block, op1, op2, env->mode)
148

Michael Beck's avatar
Michael Beck committed
149
150
151
152
/**
 * Encodes an immediate with shifter operand
 */
static unsigned int arm_encode_imm_w_shift(unsigned int shift, unsigned int immediate) {
153
154
155
	return immediate | ((shift>>1)<<8);
}

Michael Beck's avatar
Michael Beck committed
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
 * Decode an immediate with shifter operand
 */
unsigned int arm_decode_imm_w_shift(tarval *tv) {
	unsigned l = get_tarval_long(tv);
	unsigned rol = (l & ~0xFF) >> 7;

	return do_rol(l & 0xFF, rol);
}

/**
 * Creates a possible DAG for an constant.
 */
Michael Beck's avatar
Michael Beck committed
169
static ir_node *create_const_graph_value(be_abi_irg_t *abi, ir_node *irn, ir_node *block, unsigned int value) {
170
	ir_node *result;
Michael Beck's avatar
Michael Beck committed
171
172
	vals v, vn;
	int cnt;
173
174
	ir_mode *mode = get_irn_mode(irn);
	dbg_info *dbg = get_irn_dbg_info(irn);
175

Michael Beck's avatar
Michael Beck committed
176
177
178
179
180
	gen_vals_from_word(value, &v);
	gen_vals_from_word(~value, &vn);

	if (vn.ops < v.ops) {
		/* remove bits */
Michael Beck's avatar
Michael Beck committed
181
		result = create_const_neg_node(abi, irn, block, arm_encode_imm_w_shift(vn.shifts[0], vn.values[0]));
Michael Beck's avatar
Michael Beck committed
182
183
184

		for (cnt = 1; cnt < vn.ops; ++cnt) {
			tarval *tv = new_tarval_from_long(arm_encode_imm_w_shift(vn.shifts[cnt], vn.values[cnt]), mode_Iu);
185
			ir_node *bic_i_node = new_rd_arm_Bic_i(dbg, current_ir_graph, block, result, mode, tv);
Michael Beck's avatar
Michael Beck committed
186
			result = bic_i_node;
187
188
		}
	}
Michael Beck's avatar
Michael Beck committed
189
190
	else {
		/* add bits */
Michael Beck's avatar
Michael Beck committed
191
		result = create_const_node(abi, irn, block, arm_encode_imm_w_shift(v.shifts[0], v.values[0]));
Michael Beck's avatar
Michael Beck committed
192
193
194

		for (cnt = 1; cnt < v.ops; ++cnt) {
			tarval *tv = new_tarval_from_long(arm_encode_imm_w_shift(v.shifts[cnt], v.values[cnt]), mode_Iu);
195
			ir_node *orr_i_node = new_rd_arm_Or_i(dbg, current_ir_graph, block, result, mode, tv);
Michael Beck's avatar
Michael Beck committed
196
197
			result = orr_i_node;
		}
198
199
200
201
	}
	return result;
}

202
203
204
205
206
/**
 * Create a DAG constructing a given Const.
 *
 * @param irn  a Firm const
 */
Michael Beck's avatar
Michael Beck committed
207
static ir_node *create_const_graph(be_abi_irg_t *abi, ir_node *irn, ir_node *block) {
208
	int value = get_tarval_long(get_Const_tarval(irn));
Michael Beck's avatar
Michael Beck committed
209
	return create_const_graph_value(abi, irn, block, value);
210
211
212
}


Michael Beck's avatar
Michael Beck committed
213
214
215
/**
 * Creates code for a Firm Const node.
 */
216
217
218
219
220
221
static ir_node *gen_Const(ir_node *irn, arm_code_gen_t *cg) {
	ir_graph *irg = current_ir_graph;
	ir_node *block = get_nodes_block(irn);
	ir_mode *mode = get_irn_mode(irn);
	dbg_info *dbg = get_irn_dbg_info(irn);

Michael Beck's avatar
Michael Beck committed
222
223
224
225
226
227
228
229
	if (mode_is_float(mode)) {
		if (USE_FPA(cg->isa))
			return new_rd_arm_fpaConst(dbg, irg, block, mode, get_Const_tarval(irn));
		else if (USE_VFP(cg->isa))
			assert(mode != mode_E && "IEEE Extended FP not supported");
		assert(0 && "NYI");
	}
	else if (mode_is_reference(mode))
230
		return irn;
Michael Beck's avatar
Michael Beck committed
231
	return create_const_graph(cg->birg->abi, irn, block);
232
233
}

Michael Beck's avatar
Michael Beck committed
234
static ir_node *gen_mask(be_abi_irg_t *abi, ir_node *irn, ir_node *op, int result_bits) {
235
	ir_node *block = get_nodes_block(irn);
236
	unsigned mask_bits = (1 << result_bits) - 1;
Michael Beck's avatar
Michael Beck committed
237
	ir_node *mask_node = create_const_graph_value(abi, irn, block, mask_bits);
238
239
	dbg_info *dbg = get_irn_dbg_info(irn);
	return new_rd_arm_And(dbg, current_ir_graph, block, op, mask_node, get_irn_mode(irn), ARM_SHF_NONE, NULL);
240
241
}

Michael Beck's avatar
Michael Beck committed
242
static ir_node *gen_sign_extension(be_abi_irg_t *abi, ir_node *irn, ir_node *op, int result_bits) {
243
	ir_node *block = get_nodes_block(irn);
244
	int shift_width = 32 - result_bits;
245
	ir_graph *irg = current_ir_graph;
Michael Beck's avatar
Michael Beck committed
246
	ir_node *shift_const_node = create_const_graph_value(abi, irn, block, shift_width);
247
248
249
	dbg_info *dbg = get_irn_dbg_info(irn);
	ir_node *lshift_node = new_rd_arm_Shl(dbg, irg, block, op, shift_const_node, get_irn_mode(op));
	ir_node *rshift_node = new_rd_arm_Shrs(dbg, irg, block, lshift_node, shift_const_node, get_irn_mode(irn));
250
251
252
	return rshift_node;
}

Michael Beck's avatar
Michael Beck committed
253
254
255
256
257
258
/**
 * Transforms a Conv node.
 *
 * @param env   The transformation environment
 * @return the created arm Conv node
 */
259
260
261
262
static ir_node *gen_Conv(ir_node *irn, arm_code_gen_t *cg) {
	ir_graph *irg = current_ir_graph;
	ir_node *block   = get_nodes_block(irn);
	ir_node *op      = get_Conv_op(irn);
263
	ir_mode *in_mode = get_irn_mode(op);
264
265
	ir_mode *out_mode = get_irn_mode(irn);
	dbg_info *dbg    = get_irn_dbg_info(irn);
266
267
268
269

	if (in_mode == out_mode)
		return op;

Michael Beck's avatar
Michael Beck committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
	if (mode_is_float(in_mode) || mode_is_float(out_mode)) {
		cg->have_fp = 1;

		if (USE_FPA(cg->isa)) {
			if (mode_is_float(in_mode)) {
				if (mode_is_float(out_mode)) {
					/* from float to float */
					return new_rd_arm_fpaMov(dbg, irg, block, op, out_mode);
				}
				else {
					/* from float to int */
					return new_rd_arm_fpaFix(dbg, irg, block, op, out_mode);
				}
			}
			else {
				/* from int to float */
				return new_rd_arm_fpaFlt(dbg, irg, block, op, out_mode);
			}
		}
		assert(0 && "NYI");
	}
	else { /* complete in gp registers */
		int in_bits  = get_mode_size_bits(in_mode);
293
		int out_bits = get_mode_size_bits(out_mode);
Michael Beck's avatar
Michael Beck committed
294
		int in_sign  = get_mode_sign(in_mode);
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
		int out_sign = get_mode_sign(out_mode);

		// 32 -> 32
			// NOPpen
		if (in_bits == out_bits && in_bits == 32)
			return op;

		// 16 -> 16
			// unsigned -> unsigned
				// NOP
			// unsigned -> signed
				// sign extension (31:16)=(15)
			// signed -> unsigned
				// maskieren (31:16)=0
			// signed -> signed
				// NOP
		if (in_bits == out_bits && out_bits < 32) {
			if (in_sign && !out_sign) {
Michael Beck's avatar
Michael Beck committed
313
				return gen_mask(cg->birg->abi, irn, op, out_bits);
314
			} else {
Michael Beck's avatar
Michael Beck committed
315
				return gen_sign_extension(cg->birg->abi, irn, op, out_bits);
316
317
318
319
320
321
322
323
324
325
326
327
328
329
			}
		}

		// 16 -> 32
			// unsigned -> unsigned
				// NOP
			// unsigned -> signed
				// NOP
			// signed -> unsigned
				// sign extension (31:16)=(15)
			// signed -> signed
				// sign extension (31:16)=(15)
		if (in_bits < out_bits) {
			if (in_sign) {
Michael Beck's avatar
Michael Beck committed
330
				return gen_sign_extension(cg->birg->abi, irn, op, out_bits);
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
			} else {
				return op;
			}
		}

		// 32 -> 16
			// unsigned -> unsigned
				// maskieren (31:16)=0
			// unsigned -> signed
				// maskieren (31:16)=0
			// signed -> unsigned
				// maskieren (31:16)=0
			// signed -> signed
				// sign extension (erledigt auch maskieren) (31:16)=(15)
		if (in_bits > out_bits) {
			if (in_sign && out_sign) {
Michael Beck's avatar
Michael Beck committed
347
				return gen_sign_extension(cg->birg->abi, irn, op, out_bits);
348
			} else {
Michael Beck's avatar
Michael Beck committed
349
				return gen_mask(cg->birg->abi, irn, op, out_bits);
350
351
352
			}
		}
		assert(0 && "recheck integer conversion logic!");
353
		return irn;
354
	}
Michael Beck's avatar
Michael Beck committed
355
	return NULL;
356
357
}

Michael Beck's avatar
Michael Beck committed
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/**
 * Return true if an operand is a shifter operand
 */
static int is_shifter_operand(ir_node *n, arm_shift_modifier *pmod) {
	arm_shift_modifier mod = ARM_SHF_NONE;

	if (is_arm_Mov(n))
		mod = get_arm_shift_modifier(n);

	*pmod = mod;
	if (mod != ARM_SHF_NONE) {
		long v = get_tarval_long(get_arm_value(n));
		if (v < 32)
			return (int)v;
	}
	return 0;
}
375
376
377
378
379
380
381

/**
 * Creates an arm Add.
 *
 * @param env   The transformation environment
 * @return the created arm Add node
 */
382
383
static ir_node *gen_Add(ir_node *irn, arm_code_gen_t *cg) {
	ir_node *block = get_nodes_block(irn);
Michael Beck's avatar
Michael Beck committed
384
385
	ir_node *op1 = get_Add_left(irn);
	ir_node *op2 = get_Add_right(irn);
386
387
	ir_mode *mode = get_irn_mode(irn);
	ir_graph *irg = current_ir_graph;
Michael Beck's avatar
Michael Beck committed
388
389
390
	ir_node *op3;
	int v;
	arm_shift_modifier mod;
391
	dbg_info *dbg = get_irn_dbg_info(irn);
Michael Beck's avatar
Michael Beck committed
392

393
394
	if (mode_is_float(mode)) {
		cg->have_fp = 1;
Michael Beck's avatar
Michael Beck committed
395
396
397
398
399
400
		if (USE_FPA(cg->isa))
			return new_rd_arm_fpaAdd(dbg, irg, block, op1, op2, mode);
		else if (USE_VFP(cg->isa)) {
			assert(mode != mode_E && "IEEE Extended FP not supported");
		}
		assert(0 && "NYI");
401
	}
402
	if (mode_is_numP(mode)) {
Michael Beck's avatar
Michael Beck committed
403
		if (is_arm_Mov_i(op1))
404
			return new_rd_arm_Add_i(dbg, irg, block, op2, mode, get_arm_value(op1));
Michael Beck's avatar
Michael Beck committed
405
		if (is_arm_Mov_i(op2))
406
			return new_rd_arm_Add_i(dbg, irg, block, op1, mode, get_arm_value(op2));
Michael Beck's avatar
Michael Beck committed
407
408
409
410
411
412
413

		/* check for MLA */
		if (is_arm_Mul(op1) && get_irn_n_edges(op1) == 1) {
			op3 = op2;
			op2 = get_irn_n(op1, 1);
			op1 = get_irn_n(op1, 0);

414
			return new_rd_arm_Mla(dbg, irg, block, op1, op2, op3, mode);
Michael Beck's avatar
Michael Beck committed
415
416
417
418
419
420
		}
		if (is_arm_Mul(op2) && get_irn_n_edges(op2) == 1) {
			op3 = op1;
			op1 = get_irn_n(op2, 0);
			op2 = get_irn_n(op2, 1);

421
			return new_rd_arm_Mla(dbg, irg, block, op1, op2, op3, mode);
Michael Beck's avatar
Michael Beck committed
422
		}
423

Michael Beck's avatar
Michael Beck committed
424
425
426
427
		/* is the first a shifter */
		v = is_shifter_operand(op1, &mod);
		if (v) {
			op1 = get_irn_n(op1, 0);
428
			return new_rd_arm_Add(dbg, irg, block, op2, op1, mode, mod, new_tarval_from_long(v, mode_Iu));
Michael Beck's avatar
Michael Beck committed
429
430
431
432
433
		}
		/* is the second a shifter */
		v = is_shifter_operand(op2, &mod);
		if (v) {
			op2 = get_irn_n(op2, 0);
434
			return new_rd_arm_Add(dbg, irg, block, op1, op2, mode, mod, new_tarval_from_long(v, mode_Iu));
Michael Beck's avatar
Michael Beck committed
435
		}
436

Michael Beck's avatar
Michael Beck committed
437
		/* normal ADD */
438
		return new_rd_arm_Add(dbg, irg, block, op1, op2, mode, ARM_SHF_NONE, NULL);
Michael Beck's avatar
Michael Beck committed
439
440
441
442
443
	}

	assert(0 && "unknown mode for add");
	return NULL;
}
444
445
446
447

/**
 * Creates an arm Mul.
 *
Michael Beck's avatar
Michael Beck committed
448
 * @param env   The transformation environment
449
450
 * @return the created arm Mul node
 */
451
452
static ir_node *gen_Mul(ir_node *irn, arm_code_gen_t *cg) {
	ir_node *block = get_nodes_block(irn);
Michael Beck's avatar
Michael Beck committed
453
454
	ir_node *op1 = get_Mul_left(irn);
	ir_node *op2 = get_Mul_right(irn);
455
456
457
	ir_mode *mode = get_irn_mode(irn);
	ir_graph *irg = current_ir_graph;
	dbg_info *dbg = get_irn_dbg_info(irn);
Michael Beck's avatar
Michael Beck committed
458

459
460
	if (mode_is_float(mode)) {
		cg->have_fp = 1;
Michael Beck's avatar
Michael Beck committed
461
462
463
464
465
466
		if (USE_FPA(cg->isa))
			return new_rd_arm_fpaMul(dbg, irg, block, op1, op2, mode);
		else if (USE_VFP(cg->isa)) {
			assert(mode != mode_E && "IEEE Extended FP not supported");
		}
		assert(0 && "NYI");
467
	}
468
	return new_rd_arm_Mul(dbg, irg, block, op1, op2, mode);
469
470
471
}

/**
Michael Beck's avatar
Michael Beck committed
472
 * Creates an arm floating point Div.
473
 *
Michael Beck's avatar
Michael Beck committed
474
 * @param env   The transformation environment
475
476
 * @return the created arm fDiv node
 */
477
478
static ir_node *gen_Quot(ir_node *irn, arm_code_gen_t *cg) {
	ir_node *block = get_nodes_block(irn);
Michael Beck's avatar
Michael Beck committed
479
480
	ir_node *op1 = get_Quot_left(irn);
	ir_node *op2 = get_Quot_right(irn);
481
482
	ir_mode *mode = get_irn_mode(irn);
	dbg_info *dbg = get_irn_dbg_info(irn);
Michael Beck's avatar
Michael Beck committed
483

484
	assert(mode != mode_E && "IEEE Extended FP not supported");
485

Michael Beck's avatar
Michael Beck committed
486
487
488
489
490
491
492
493
494
	cg->have_fp = 1;
	if (USE_FPA(cg->isa))
		return new_rd_arm_fpaDiv(dbg, current_ir_graph, block, op1, op2, mode);
	else if (USE_VFP(cg->isa)) {
		assert(mode != mode_E && "IEEE Extended FP not supported");
	}
	assert(0 && "NYI");

	return NULL;
Michael Beck's avatar
Michael Beck committed
495
496
497
}

#define GEN_INT_OP(op) \
498
499
500
static ir_node *gen_ ## op(ir_node *irn, arm_code_gen_t *cg) { \
	ir_graph *irg = current_ir_graph; \
	ir_node *block = get_nodes_block(irn); \
Michael Beck's avatar
Michael Beck committed
501
502
503
504
	ir_node *op1 = get_ ## op ## _left(irn); \
	ir_node *op2 = get_ ## op ## _right(irn); \
	int v; \
	arm_shift_modifier mod; \
505
506
	ir_mode *mode = get_irn_mode(irn); \
	dbg_info *dbg = get_irn_dbg_info(irn); \
Michael Beck's avatar
Michael Beck committed
507
508
 \
	if (is_arm_Mov_i(op1)) \
509
		return new_rd_arm_ ## op ## _i(dbg, irg, block, op2, mode, get_arm_value(op1)); \
Michael Beck's avatar
Michael Beck committed
510
	if (is_arm_Mov_i(op2)) \
511
		return new_rd_arm_ ## op ## _i(dbg, irg, block, op1, mode, get_arm_value(op2)); \
Michael Beck's avatar
Michael Beck committed
512
513
514
515
	/* is the first a shifter */ \
	v = is_shifter_operand(op1, &mod); \
	if (v) { \
		op1 = get_irn_n(op1, 0); \
516
		return new_rd_arm_ ## op(dbg, irg, block, op2, op1, mode, mod, new_tarval_from_long(v, mode_Iu)); \
Michael Beck's avatar
Michael Beck committed
517
518
519
520
521
	} \
	/* is the second a shifter */ \
	v = is_shifter_operand(op2, &mod); \
	if (v) { \
		op2 = get_irn_n(op2, 0); \
522
		return new_rd_arm_ ## op(dbg, irg, block, op1, op2, mode, mod, new_tarval_from_long(v, mode_Iu)); \
Michael Beck's avatar
Michael Beck committed
523
524
	} \
	/* Normal op */ \
525
	return new_rd_arm_ ## op(dbg, irg, block, op1, op2, mode, ARM_SHF_NONE, NULL); \
526
527
528
529
530
531
}


/**
 * Creates an arm And.
 *
Michael Beck's avatar
Michael Beck committed
532
 * @param env   The transformation environment
533
534
 * @return the created arm And node
 */
535
static ir_node *gen_And(ir_node *irn, arm_code_gen_t *cg);
Michael Beck's avatar
Michael Beck committed
536
GEN_INT_OP(And)
537
538

/**
Michael Beck's avatar
Michael Beck committed
539
 * Creates an arm Orr.
540
 *
Michael Beck's avatar
Michael Beck committed
541
 * @param env   The transformation environment
542
543
 * @return the created arm Or node
 */
544
static ir_node *gen_Or(ir_node *irn, arm_code_gen_t *cg);
Michael Beck's avatar
Michael Beck committed
545
GEN_INT_OP(Or)
546
547
548
549

/**
 * Creates an arm Eor.
 *
Michael Beck's avatar
Michael Beck committed
550
 * @param env   The transformation environment
551
552
 * @return the created arm Eor node
 */
553
static ir_node *gen_Eor(ir_node *irn, arm_code_gen_t *cg);
Michael Beck's avatar
Michael Beck committed
554
GEN_INT_OP(Eor)
555
556
557
558

/**
 * Creates an arm Sub.
 *
Michael Beck's avatar
Michael Beck committed
559
 * @param env   The transformation environment
560
561
 * @return the created arm Sub node
 */
562
563
static ir_node *gen_Sub(ir_node *irn, arm_code_gen_t *cg) {
	ir_node *block = get_nodes_block(irn);
Michael Beck's avatar
Michael Beck committed
564
565
566
567
	ir_node *op1 = get_Sub_left(irn);
	ir_node *op2 = get_Sub_right(irn);
	int v;
	arm_shift_modifier mod;
568
569
570
	ir_mode *mode = get_irn_mode(irn);
	ir_graph *irg = current_ir_graph;
	dbg_info *dbg = get_irn_dbg_info(irn);
Michael Beck's avatar
Michael Beck committed
571

572
573
	if (mode_is_float(mode)) {
		cg->have_fp = 1;
Michael Beck's avatar
Michael Beck committed
574
575
576
577
578
579
		if (USE_FPA(cg->isa))
			return new_rd_arm_fpaSub(dbg, irg, block, op1, op2, mode);
		else if (USE_VFP(cg->isa)) {
			assert(mode != mode_E && "IEEE Extended FP not supported");
		}
		assert(0 && "NYI");
Michael Beck's avatar
Michael Beck committed
580
	}
581
	if (mode_is_numP(mode)) {
Michael Beck's avatar
Michael Beck committed
582
		if (is_arm_Mov_i(op1))
583
			return new_rd_arm_Rsb_i(dbg, irg, block, op2, mode, get_arm_value(op1));
Michael Beck's avatar
Michael Beck committed
584
		if (is_arm_Mov_i(op2))
585
			return new_rd_arm_Sub_i(dbg, irg, block, op1, mode, get_arm_value(op2));
Michael Beck's avatar
Michael Beck committed
586
587
588
589
590

		/* is the first a shifter */
		v = is_shifter_operand(op1, &mod);
		if (v) {
			op1 = get_irn_n(op1, 0);
591
			return new_rd_arm_Rsb(dbg, irg, block, op2, op1, mode, mod, new_tarval_from_long(v, mode_Iu));
592
		}
Michael Beck's avatar
Michael Beck committed
593
594
595
596
		/* is the second a shifter */
		v = is_shifter_operand(op2, &mod);
		if (v) {
			op2 = get_irn_n(op2, 0);
597
			return new_rd_arm_Sub(dbg, irg, block, op1, op2, mode, mod, new_tarval_from_long(v, mode_Iu));
Michael Beck's avatar
Michael Beck committed
598
599
		}
		/* normal sub */
600
		return new_rd_arm_Sub(dbg, irg, block, op1, op2, mode, ARM_SHF_NONE, NULL);
601
	}
Michael Beck's avatar
Michael Beck committed
602
603
	assert(0 && "unknown mode for sub");
	return NULL;
604
605
606
607
608
}

/**
 * Creates an arm Shl.
 *
Michael Beck's avatar
Michael Beck committed
609
 * @param env   The transformation environment
610
611
 * @return the created arm Shl node
 */
612
static ir_node *gen_Shl(ir_node *irn, arm_code_gen_t *cg) {
613
	ir_node *result;
614
	ir_node *block = get_nodes_block(irn);
Michael Beck's avatar
Michael Beck committed
615
616
	ir_node *op1 = get_Shl_left(irn);
	ir_node *op2 = get_Shl_right(irn);
617
618
619
	ir_mode *mode = get_irn_mode(irn);
	ir_graph *irg = current_ir_graph;
	dbg_info *dbg = get_irn_dbg_info(irn);
Michael Beck's avatar
Michael Beck committed
620
621

	if (is_arm_Mov_i(op2)) {
622
		result = new_rd_arm_Mov(dbg, irg, block, op1, mode, ARM_SHF_LSL, get_arm_value(op2));
623
	} else {
624
		result = new_rd_arm_Shl(dbg, irg, block, op1, op2, mode);
625
626
627
628
629
630
631
	}
	return result;
}

/**
 * Creates an arm Shr.
 *
Michael Beck's avatar
Michael Beck committed
632
 * @param env   The transformation environment
633
634
 * @return the created arm Shr node
 */
635
static ir_node *gen_Shr(ir_node *irn, arm_code_gen_t *cg) {
636
	ir_node *result;
637
	ir_node *block = get_nodes_block(irn);
Michael Beck's avatar
Michael Beck committed
638
639
	ir_node *op1 = get_Shr_left(irn);
	ir_node *op2 = get_Shr_right(irn);
640
641
642
	ir_mode *mode = get_irn_mode(irn);
	ir_graph *irg = current_ir_graph;
	dbg_info *dbg = get_irn_dbg_info(irn);
Michael Beck's avatar
Michael Beck committed
643
644

	if (is_arm_Mov_i(op2)) {
645
		result = new_rd_arm_Mov(dbg, irg, block, op1, mode, ARM_SHF_LSR, get_arm_value(op2));
646
	} else {
647
		result = new_rd_arm_Shr(dbg, irg, block, op1, op2, mode);
648
649
650
651
652
653
654
	}
	return result;
}

/**
 * Creates an arm Shrs.
 *
Michael Beck's avatar
Michael Beck committed
655
 * @param env   The transformation environment
656
657
 * @return the created arm Shrs node
 */
658
static ir_node *gen_Shrs(ir_node *irn, arm_code_gen_t *cg) {
659
	ir_node *result;
660
	ir_node *block = get_nodes_block(irn);
Michael Beck's avatar
Michael Beck committed
661
662
	ir_node *op1 = get_Shrs_left(irn);
	ir_node *op2 = get_Shrs_right(irn);
663
664
	ir_mode *mode = get_irn_mode(irn);
	dbg_info *dbg = get_irn_dbg_info(irn);
Michael Beck's avatar
Michael Beck committed
665
666

	if (is_arm_Mov_i(op2)) {
667
		result = new_rd_arm_Mov(dbg, current_ir_graph, block, op1, mode, ARM_SHF_ASR, get_arm_value(op2));
668
	} else {
669
		result = new_rd_arm_Shrs(dbg, current_ir_graph, block, op1, op2, mode);
670
671
672
673
674
675
676
	}
	return result;
}

/**
 * Transforms a Not node.
 *
Michael Beck's avatar
Michael Beck committed
677
 * @param env   The transformation environment
678
679
 * @return the created arm Not node
 */
680
681
682
static ir_node *gen_Not(ir_node *irn, arm_code_gen_t *cg) {
	ir_node *block = get_nodes_block(irn);
	ir_node *op = get_Not_op(irn);
Michael Beck's avatar
Michael Beck committed
683
684
685
	int v;
	arm_shift_modifier mod = ARM_SHF_NONE;
	tarval  *tv = NULL;
686
	dbg_info *dbg = get_irn_dbg_info(irn);
Michael Beck's avatar
Michael Beck committed
687
688
689
690
691
692

	v = is_shifter_operand(op, &mod);
	if (v) {
		op = get_irn_n(op, 0);
		tv = new_tarval_from_long(v, mode_Iu);
	}
693
	return new_rd_arm_Mvn(dbg, current_ir_graph, block, op, get_irn_mode(irn), mod, tv);
694
695
}

Michael Beck's avatar
Michael Beck committed
696
697
698
699
700
701
/**
 * Transforms an Abs node.
 *
 * @param env   The transformation environment
 * @return the created arm Abs node
 */
702
703
704
705
706
static ir_node *gen_Abs(ir_node *irn, arm_code_gen_t *cg) {
	ir_node *block = get_nodes_block(irn);
	ir_node *op = get_Abs_op(irn);
	ir_mode *mode = get_irn_mode(irn);
	dbg_info *dbg = get_irn_dbg_info(irn);
707

708
709
	if (mode_is_float(mode)) {
		cg->have_fp = 1;
Michael Beck's avatar
Michael Beck committed
710
711
712
713
714
715
		if (USE_FPA(cg->isa))
			return new_rd_arm_fpaAbs(dbg, current_ir_graph, block, op, mode);
		else if (USE_VFP(cg->isa)) {
			assert(mode != mode_E && "IEEE Extended FP not supported");
		}
		assert(0 && "NYI");
716
	}
717
	return new_rd_arm_Abs(dbg, current_ir_graph, block, op, mode);
718
719
720
721
722
}

/**
 * Transforms a Minus node.
 *
Michael Beck's avatar
Michael Beck committed
723
 * @param env   The transformation environment
724
725
 * @return the created arm Minus node
 */
726
727
728
729
730
731
static ir_node *gen_Minus(ir_node *irn, arm_code_gen_t *cg) {
	ir_node *block = get_nodes_block(irn);
	ir_node *op = get_Minus_op(irn);
	ir_mode *mode = get_irn_mode(irn);
	ir_graph *irg = current_ir_graph;
	dbg_info *dbg = get_irn_dbg_info(irn);
Michael Beck's avatar
Michael Beck committed
732

733
	if (mode_is_float(mode)) {
Michael Beck's avatar
Michael Beck committed
734
735
736
737
738
739
740
		cg->have_fp = 1;
		if (USE_FPA(cg->isa))
			return new_rd_arm_fpaMnv(dbg, irg, block, op, mode);
		else if (USE_VFP(cg->isa)) {
			assert(mode != mode_E && "IEEE Extended FP not supported");
		}
		assert(0 && "NYI");
741
	}
742
	return new_rd_arm_Rsb_i(dbg, irg, block, op, mode, get_mode_null(mode));
743
744
745
746
747
748
749
750
751
752
753
}

/**
 * Transforms a Load.
 *
 * @param mod     the debug module
 * @param block   the block the new node should belong to
 * @param node    the ir Load node
 * @param mode    node mode
 * @return the created arm Load node
 */
754
755
756
757
758
static ir_node *gen_Load(ir_node *irn, arm_code_gen_t *cg) {
	ir_node *block = get_nodes_block(irn);
	ir_mode *mode = get_Load_mode(irn);
	ir_graph *irg = current_ir_graph;
	dbg_info *dbg = get_irn_dbg_info(irn);
759

Michael Beck's avatar
Michael Beck committed
760
	if (mode_is_float(mode)) {
761
		cg->have_fp = 1;
Michael Beck's avatar
Michael Beck committed
762
763
764
765
766
767
768
		if (USE_FPA(cg->isa))
			return new_rd_arm_fpaLdf(dbg, irg, block, get_Load_ptr(irn), get_Load_mem(irn),
				get_Load_mode(irn));
		else if (USE_VFP(cg->isa)) {
			assert(mode != mode_E && "IEEE Extended FP not supported");
		}
		assert(0 && "NYI");
769
770
	}
	if (mode == mode_Bu) {
Michael Beck's avatar
Michael Beck committed
771
		return new_rd_arm_Loadb(dbg, irg, block, get_Load_ptr(irn), get_Load_mem(irn));
772
773
	}
	if (mode == mode_Bs) {
Michael Beck's avatar
Michael Beck committed
774
		return new_rd_arm_Loadbs(dbg, irg, block, get_Load_ptr(irn), get_Load_mem(irn));
775
776
	}
	if (mode == mode_Hu) {
Michael Beck's avatar
Michael Beck committed
777
		return new_rd_arm_Loadh(dbg, irg, block, get_Load_ptr(irn), get_Load_mem(irn));
778
779
	}
	if (mode == mode_Hs) {
Michael Beck's avatar
Michael Beck committed
780
		return new_rd_arm_Loadhs(dbg, irg, block, get_Load_ptr(irn), get_Load_mem(irn));
781
782
	}
	if (mode_is_reference(mode)) {
Michael Beck's avatar
Michael Beck committed
783
		return new_rd_arm_Load(dbg, irg, block, get_Load_ptr(irn), get_Load_mem(irn));
784
	}
Michael Beck's avatar
Michael Beck committed
785
	return new_rd_arm_Load(dbg, irg, block, get_Load_ptr(irn), get_Load_mem(irn));
786
787
788
789
790
791
792
793
794
795
796
}

/**
 * Transforms a Store.
 *
 * @param mod     the debug module
 * @param block   the block the new node should belong to
 * @param node    the ir Store node
 * @param mode    node mode
 * @return the created arm Store node
 */
797
798
799
800
801
static ir_node *gen_Store(ir_node *irn, arm_code_gen_t *cg) {
	ir_node *block = get_nodes_block(irn);
	ir_mode *mode = get_irn_mode(get_Store_value(irn));
	ir_graph *irg = current_ir_graph;
	dbg_info *dbg = get_irn_dbg_info(irn);
802

803
	assert(mode != mode_E && "IEEE Extended FP not supported");
Michael Beck's avatar
Michael Beck committed
804
	if (mode_is_float(mode)) {
805
		cg->have_fp = 1;
Michael Beck's avatar
Michael Beck committed
806
807
808
809
810
811
812
		if (USE_FPA(cg->isa))
			return new_rd_arm_fpaStf(dbg, irg, block, get_Store_ptr(irn), get_Store_value(irn),
				get_Store_mem(irn), get_irn_mode(get_Store_value(irn)));
		else if (USE_VFP(cg->isa)) {
			assert(mode != mode_E && "IEEE Extended FP not supported");
		}
		assert(0 && "NYI");
813
814
	}
	if (mode == mode_Bu) {
Michael Beck's avatar
Michael Beck committed
815
		return new_rd_arm_Storeb(dbg, irg, block, get_Store_ptr(irn), get_Store_value(irn), get_Store_mem(irn));
816
817
	}
	if (mode == mode_Bs) {
Michael Beck's avatar
Michael Beck committed
818
		return new_rd_arm_Storebs(dbg, irg, block, get_Store_ptr(irn), get_Store_value(irn), get_Store_mem(irn));
819
820
	}
	if (mode == mode_Hu) {
Michael Beck's avatar
Michael Beck committed
821
		return new_rd_arm_Storeh(dbg, irg, block, get_Store_ptr(irn), get_Store_value(irn), get_Store_mem(irn));
822
823
	}
	if (mode == mode_Hs) {
Michael Beck's avatar
Michael Beck committed
824
		return new_rd_arm_Storehs(dbg, irg, block, get_Store_ptr(irn), get_Store_value(irn), get_Store_mem(irn));
825
	}
Michael Beck's avatar
Michael Beck committed
826
	return new_rd_arm_Store(dbg, irg, block, get_Store_ptr(irn), get_Store_value(irn), get_Store_mem(irn));
827
828
829
}


830
static ir_node *gen_Cond(ir_node *irn, arm_code_gen_t *cg) {
Christian Würdig's avatar
Christian Würdig committed
831
	ir_node *result   = NULL;
832
833
834
835
	ir_node *selector = get_Cond_selector(irn);
	ir_node *block    = get_nodes_block(irn);
	ir_graph *irg     = current_ir_graph;
	dbg_info *dbg     = get_irn_dbg_info(irn);
Christian Würdig's avatar
Christian Würdig committed
836

837
838
839
840
841
842
	if ( get_irn_mode(selector) == mode_b ) {
		//CondJmp
		ir_node *proj_node = get_Cond_selector(irn);
		ir_node *cmp_node = get_Proj_pred(proj_node);
		ir_node *op1 = get_Cmp_left(cmp_node);
		ir_node *op2 = get_Cmp_right(cmp_node);
843
		result = new_rd_arm_CondJmp(dbg, irg, block, op1, op2, mode_T);
844
845
846
		set_arm_proj_num(result, get_Proj_proj(proj_node));
	} else {
		//SwitchJmp
847
		ir_node *op = get_irn_n(irn, 0);
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
		ir_node *const_graph;
		ir_node *sub;
		ir_node *const_node;

		ir_node *proj;
		const ir_edge_t *edge;
		int min = INT_MAX;
		int max = INT_MIN;
		int translation;
		int norm_max;
		int norm_min;
		int pn;
		int n_projs;
		ir_node **projs;

		foreach_out_edge(irn, edge) {
			proj = get_edge_src_irn(edge);
			assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");

			pn = get_Proj_proj(proj);

			min = pn<min ? pn : min;
			max = pn>max ? pn : max;
		}
		translation = min;
		norm_max = max - translation;
		norm_min = min - translation;

		n_projs = norm_max + 1;
		projs = xcalloc(n_projs , sizeof(ir_node*));


		foreach_out_edge(irn, edge) {
			proj = get_edge_src_irn(edge);
			assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");

			pn = get_Proj_proj(proj) - translation;
			set_Proj_proj(proj, pn);
		}


889
890
891
892
		const_node = new_rd_Const(dbg, irg, block, mode_Iu, new_tarval_from_long(translation, mode_Iu));
		const_graph = gen_Const(const_node, cg);
		sub = new_rd_arm_Sub(dbg, irg, block, op, const_graph, get_irn_mode(op), ARM_SHF_NONE, NULL);
		result = new_rd_arm_SwitchJmp(dbg, irg, block, sub, mode_T);
893
894
895
896
897
898
899
900
901
902
903
		set_arm_n_projs(result, n_projs);
		set_arm_default_proj_num(result, get_Cond_defaultProj(irn)-translation);
	}
	return result;
}

/**
 * Returns the name of a SymConst.
 * @param symc  the SymConst
 * @return name of the SymConst
 */
Michael Beck's avatar
Michael Beck committed
904
ident *get_sc_ident(ir_node *symc) {
Michael Beck's avatar
Michael Beck committed
905
	ir_entity *ent;
906
907
908

	switch (get_SymConst_kind(symc)) {
		case symconst_addr_name:
Michael Beck's avatar
Michael Beck committed
909
			return get_SymConst_name(symc);
910
911

		case symconst_addr_ent:
Michael Beck's avatar
Michael Beck committed
912
913
			ent = get_SymConst_entity(symc);
			mark_entity_visited(ent);
Michael Beck's avatar
Michael Beck committed
914
			return get_entity_ld_ident(ent);
915
916
917
918
919
920
921
922

		default:
			assert(0 && "Unsupported SymConst");
	}

	return NULL;
}

923
924
925
926
static ir_node *gen_SymConst(ir_node *irn, arm_code_gen_t *cg) {
	ir_node *block = get_nodes_block(irn);
	ir_mode *mode = get_irn_mode(irn);
	dbg_info *dbg = get_irn_dbg_info(irn);
Michael Beck's avatar
Michael Beck committed
927
	return new_rd_arm_SymConst(dbg, current_ir_graph, block, mode, get_sc_ident(irn));
928
929
930
931
932
933
934
935
936
937
}



/**
 * Transforms a CopyB node.
 *
 * @param env   The transformation environment
 * @return The transformed node.
 */
938
static ir_node *gen_CopyB(ir_node *irn, arm_code_gen_t *cg) {
939
	ir_node  *res   = NULL;
940
941
942
943
944
945
946
947
	dbg_info *dbg   = get_irn_dbg_info(irn);
	ir_mode  *mode  = get_irn_mode(irn);
	ir_node  *src   = get_CopyB_src(irn);
	ir_node  *dst   = get_CopyB_dst(irn);
	ir_node  *mem   = get_CopyB_mem(irn);
	ir_node  *block = get_nodes_block(irn);
	int       size  = get_type_size_bytes(get_CopyB_type(irn));
	ir_graph *irg   = current_ir_graph;
948
949
950
	ir_node *src_copy;
	ir_node *dst_copy;

Michael Beck's avatar
Michael Beck committed
951
952
	src_copy = be_new_Copy(&arm_reg_classes[CLASS_arm_gp], irg, block, src);
	dst_copy = be_new_Copy(&arm_reg_classes[CLASS_arm_gp], irg, block, dst);
953
954
955
956
957
958
959
960
961
962
963

 	res = new_rd_arm_CopyB( dbg, irg, block, dst_copy, src_copy, new_rd_arm_EmptyReg(dbg, irg, block, mode_Iu), new_rd_arm_EmptyReg(dbg, irg, block, mode_Iu), new_rd_arm_EmptyReg(dbg, irg, block, mode_Iu), mem, mode);
	set_arm_value(res, new_tarval_from_long(size, mode_Iu));

	return res;
}





Michael Beck's avatar
Michael Beck committed
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
/********************************************
 *  _                          _
 * | |                        | |
 * | |__   ___ _ __   ___   __| | ___  ___
 * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
 * | |_) |  __/ | | | (_) | (_| |  __/\__ \
 * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
 *
 ********************************************/

/**
 * Return an expanding stack offset.
 * Note that function is called in the transform phase
 * where the stack offsets are still relative regarding
 * the first (frame allocating) IncSP.
 * However this is exactly what we want because frame
 * access must be done relative the the fist IncSP ...
 */
static int get_sp_expand_offset(ir_node *inc_sp) {
983
	int offset = be_get_IncSP_offset(inc_sp);
Michael Beck's avatar
Michael Beck committed
984

985
	if (offset == BE_STACK_FRAME_SIZE_EXPAND)
Michael Beck's avatar
Michael Beck committed
986
		return 0;
987
988

	return offset;
Michael Beck's avatar
Michael Beck committed
989
990
991
}

#if 0
Matthias Braun's avatar
Matthias Braun committed
992
static ir_node *gen_StackParam(ir_node *irn, arm_code_gen_t *cg) {
993
994
995
996
997
998
999
	ir_node   *new_op = NULL;
	ir_node   *block  = get_nodes_block(irn);
	ir_node   *noreg  = ia32_new_NoReg_gp(env->cg);
	ir_node   *mem    = new_rd_NoMem(env->irg);
	ir_node   *ptr    = get_irn_n(irn, 0);
	ir_entity *ent    = be_get_frame_entity(irn);
	ir_mode   *mode   = env->mode;
Michael Beck's avatar
Michael Beck committed
1000
1001
1002
1003
1004
1005
1006
1007
1008

//	/* If the StackParam has only one user ->     */
//	/* put it in the Block where the user resides */
//	if (get_irn_n_edges(node) == 1) {
//		env->block = get_nodes_block(get_edge_src_irn(get_irn_out_edge_first(node)));
//	}

	if (mode_is_float(mode)) {
		if (USE_SSE2(env->cg))
1009
			new_op = new_rd_ia32_fLoad(env->dbg, env->irg, block, ptr, noreg, mem, mode_T);
Michael Beck's avatar
Michael Beck committed
1010
1011
		else {
			env->cg->used_x87 = 1;
1012
			new_op = new_rd_ia32_vfld(env->dbg, env->irg, block, ptr, noreg, mem, mode_T);
Michael Beck's avatar
Michael Beck committed
1013
1014
1015
		}
	}
	else {
1016
		new_op = new_rd_ia32_Load(env->dbg, env->irg, block, ptr, noreg, mem, mode_T);
Michael Beck's avatar
Michael Beck committed
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
	}

	set_ia32_frame_ent(new_op, ent);
	set_ia32_use_frame(new_op);

	set_ia32_am_support(new_op, ia32_am_Source);
	set_ia32_op_type(new_op, ia32_AddrModeS);
	set_ia32_am_flavour(new_op, ia32_B);
	set_ia32_ls_mode(new_op, mode);

	SET_IA32_ORIG_NODE(new_op, ia32_get_old_node_name(env->cg, env->irn));

1029
	return new_rd_Proj(env->dbg, env->irg, block, new_op, mode, 0);
Michael Beck's avatar
Michael Beck committed
1030
}
Matthias Braun's avatar
Matthias Braun committed
1031
#endif
Michael Beck's avatar
Michael Beck committed
1032
1033
1034
1035

/**
 * Transforms a FrameAddr into an ia32 Add.
 */
1036
1037
static ir_node *gen_be_FrameAddr(ir_node *irn, arm_code_gen_t *cg) {
	ir_node *block  = get_nodes_block(irn);
1038
	ir_entity *ent  = be_get_frame_entity(irn);
1039
	int     offset  = get_entity_offset(ent);
1040
	ir_node *op     = get_irn_n(irn, 0);
Michael Beck's avatar
Michael Beck committed
1041
	ir_node *cnst;
1042
1043
	ir_mode *mode   = get_irn_mode(irn);
	dbg_info *dbg   = get_irn_dbg_info(irn);
Michael Beck's avatar
Michael Beck committed
1044
1045
1046
1047
1048
1049

	if (be_is_IncSP(op)) {
		/* BEWARE: we get an offset which is absolute from an offset that
		   is relative. Both must be merged */
		offset += get_sp_expand_offset(op);
	}
Michael Beck's avatar
Michael Beck committed
1050
	cnst = create_const_graph_value(cg->birg->abi, irn, block, (unsigned)offset);
1051
1052
1053
	if (is_arm_Mov_i(cnst))
		return new_rd_arm_Add_i(dbg, current_ir_graph, block, op, mode, get_arm_value(cnst));
	return new_rd_arm_Add(dbg, current_ir_graph, block, op, cnst, mode, ARM_SHF_NONE, NULL);
Michael Beck's avatar
Michael Beck committed
1054
1055
}

Matthias Braun's avatar
Matthias Braun committed
1056
#if 0
Michael Beck's avatar
Michael Beck committed
1057
1058
1059
/**
 * Transforms a FrameLoad into an ia32 Load.
 */
1060
static ir_node *gen_FrameLoad(ir_node *irn, arm_code_gen_t *cg) {
1061
1062
1063
1064
1065
1066
	ir_node   *new_op = NULL;
	ir_node   *noreg  = ia32_new_NoReg_gp(env->cg);
	ir_node   *mem    = get_irn_n(irn, 0);
	ir_node   *ptr    = get_irn_n(irn, 1);
	ir_entity *ent    = be_get_frame_entity(irn);
	ir_mode   *mode   = get_type_mode(get_entity_type(ent));
Michael Beck's avatar
Michael Beck committed
1067
1068
1069

	if (mode_is_float(mode)) {
		if (USE_SSE2(env->cg))
1070
			new_op = new_rd_ia32_fLoad(env->dbg, current_ir_graph, env->block, ptr, noreg, mem, mode_T);
Michael Beck's avatar
Michael Beck committed
1071
1072
		else {
			env->cg->used_x87 = 1;
1073
			new_op = new_rd_ia32_vfld(env->dbg, current_ir_graph, env->block, ptr, noreg, mem, mode_T);
Michael Beck's avatar
Michael Beck committed
1074
1075
1076
		}
	}
	else {
1077
		new_op = new_rd_ia32_Load(env->dbg, current_ir_graph, env->block, ptr, noreg, mem, mode_T);
Michael Beck's avatar
Michael Beck committed
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
	}

	set_ia32_frame_ent(new_op, ent);
	set_ia32_use_frame(new_op);

	set_ia32_am_support(new_op, ia32_am_Source);
	set_ia32_op_type(new_op, ia32_AddrModeS);
	set_ia32_am_flavour(new_op, ia32_B);
	set_ia32_ls_mode(new_op, mode);

	SET_IA32_ORIG_NODE(new_op, ia32_get_old_node_name(env->cg, env->irn));

	return new_op;
}
Matthias Braun's avatar
Matthias Braun committed
1092
#endif
Michael Beck's avatar
Michael Beck committed
1093

Matthias Braun's avatar
Matthias Braun committed
1094
#if 0
Michael Beck's avatar
Michael Beck committed
1095
1096
1097
/**
 * Transforms a FrameStore into an ia32 Store.
 */
1098
static ir_node *gen_FrameStore(ir_node *irn, arm_code_gen_t *cg) {
1099
1100
1101
1102
1103
1104
1105
	ir_node   *new_op = NULL;
	ir_node   *noreg  = ia32_new_NoReg_gp(env->cg);
	ir_node   *mem    = get_irn_n(irn, 0);
	ir_node   *ptr    = get_irn_n(irn, 1);
	ir_node   *val    = get_irn_n(irn, 2);
	ir_entity *ent    = be_get_frame_entity(irn);
	ir_mode   *mode   = get_irn_mode(val);
Michael Beck's avatar
Michael Beck committed
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133

	if (mode_is_float(mode)) {
		if (USE_SSE2(env->cg))
			new_op = new_rd_ia32_fStore(env->dbg, env->irg, env->block, ptr, noreg, val, mem, mode_T);
		else {
			env->cg->used_x87 = 1;
			new_op = new_rd_ia32_vfst(env->dbg, env->irg, env->block, ptr, noreg, val, mem, mode_T);
		}
	}
	else if (get_mode_size_bits(mode) == 8) {
		new_op = new_rd_ia32_Store8Bit(env->dbg, env->irg, env->block, ptr, noreg, val, mem, mode_T);
	}
	else {
		new_op = new_rd_ia32_Store(env->dbg, env->irg, env->block, ptr, noreg, val, mem, mode_T);
	}

	set_ia32_frame_ent(new_op, ent);
	set_ia32_use_frame(new_op);

	set_ia32_am_support(new_op, ia32_am_Dest);
	set_ia32_op_type(new_op, ia32_AddrModeD);
	set_ia32_am_flavour(new_op, ia32_B);
	set_ia32_ls_mode(new_op, mode);

	SET_IA32_ORIG_NODE(new_op, ia32_get_old_node_name(env->cg, env->irn));

	return new_op;
}
Matthias Braun's avatar
Matthias Braun committed
1134
#endif
Michael Beck's avatar
Michael Beck committed
1135
1136


1137
// static ir_node *gen_be_Copy(ir_node *irn, arm_code_gen_t *cg) {
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
// 	return new_rd_arm_Copy(env->dbg, env->irg, env->block, op, env->mode);
// }

/*********************************************************
 *                  _             _      _
 *                 (_)           | |    (_)
 *  _ __ ___   __ _ _ _ __     __| |_ __ ___   _____ _ __
 * | '_ ` _ \ / _` | | '_ \   / _` | '__| \ \ / / _ \ '__|
 * | | | | | | (_| | | | | | | (_| | |  | |\ V /  __/ |
 * |_| |_| |_|\__,_|_|_| |_|  \__,_|_|  |_| \_/ \___|_|
 *
 *********************************************************/

Michael Beck's avatar
Michael Beck committed
1151
1152
1153
/**
 * move constants out of the start block
 */
1154
void arm_move_consts(ir_node *node, void *env) {
Michael Beck's avatar
Michael Beck committed
1155
	arm_code_gen_t *cg = env;
1156
	int i;
Michael Beck's avatar
Michael Beck committed
1157

1158
1159
	if (is_Block(node))
		return;
Michael Beck's avatar
Michael Beck committed
1160

1161
	if (is_Phi(node)) {
Michael Beck's avatar
Michael Beck committed
1162
		for (i = get_irn_arity(node) - 1; i >= 0; --i) {
1163
			ir_node *pred = get_irn_n(node,i);
1164
			ir_opcode pred_code = get_irn_opcode(pred);
1165
1166
			if (pred_code == iro_Const) {
				ir_node *const_graph;
Michael Beck's avatar
Michael Beck committed
1167
				const_graph = create_const_graph(cg->birg->abi, pred, get_nodes_block(get_irn_n(get_nodes_block(node),i)));
1168
				set_irn_n(node, i, const_graph);
Michael Beck's avatar
Michael Beck committed
1169
1170
1171
1172
1173
			}
			else if (pred_code == iro_SymConst) {
				/* FIXME: in general, SymConst always require a load, so it
				   might be better to place them into the first real block
				   and let the spiller rematerialize them. */
Michael Beck's avatar
Michael Beck committed
1174
				ident *id = get_sc_ident(pred);
1175
1176
				ir_node *symconst_node;
				symconst_node = new_rd_arm_SymConst(get_irn_dbg_info(pred),
Michael Beck's avatar
Michael Beck committed
1177
					current_ir_graph, get_nodes_block(get_irn_n(get_nodes_block(node),i)),
Michael Beck's avatar
Michael Beck committed
1178
					get_irn_mode(pred), id);
1179
1180
1181
1182
1183
				set_irn_n(node, i, symconst_node);
			}
		}
		return;
	}
Michael Beck's avatar
Michael Beck committed
1184
	for (i = 0; i < get_irn_arity(node); i++) {
1185
		ir_node *pred = get_irn_n(node,i);
1186
		ir_opcode pred_code = get_irn_opcode(pred);
1187
1188
		if (pred_code == iro_Const) {
			ir_node *const_graph;
Michael Beck's avatar
Michael Beck committed
1189
			const_graph = create_const_graph(cg->birg->abi, pred, get_nodes_block(node));
1190
1191
			set_irn_n(node, i, const_graph);
		} else if (pred_code == iro_SymConst) {
Michael Beck's avatar
Michael Beck committed