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

/**
 * @file
 * @brief   code selection (transform FIRM into SPARC FIRM)
Michael Beck's avatar
Michael Beck committed
23
 * @version $Id$
Hannes Rapp's avatar
Hannes Rapp committed
24
25
26
 */
#include "config.h"

27
28
#include <stdint.h>

Hannes Rapp's avatar
Hannes Rapp committed
29
30
31
32
33
34
35
#include "irnode_t.h"
#include "irgraph_t.h"
#include "irmode_t.h"
#include "irgmod.h"
#include "iredges.h"
#include "ircons.h"
#include "irprintf.h"
36
#include "iroptimize.h"
Hannes Rapp's avatar
Hannes Rapp committed
37
38
39
40
41
42
43
44
45
#include "dbginfo.h"
#include "iropt_t.h"
#include "debug.h"
#include "error.h"

#include "../benode.h"
#include "../beirg.h"
#include "../beutil.h"
#include "../betranshlp.h"
46
#include "../beabihelper.h"
Hannes Rapp's avatar
Hannes Rapp committed
47
48
49
50
51
52
53
54
#include "bearch_sparc_t.h"

#include "sparc_nodes_attr.h"
#include "sparc_transform.h"
#include "sparc_new_nodes.h"
#include "gen_sparc_new_nodes.h"

#include "gen_sparc_regalloc_if.h"
55
#include "sparc_cconv.h"
Hannes Rapp's avatar
Hannes Rapp committed
56
57
58
59
60

#include <limits.h>

DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

61
62
63
64
65
66
67
static sparc_code_gen_t      *env_cg;
static beabi_helper_env_t    *abihelper;
static const arch_register_t *sp_reg = &sparc_gp_regs[REG_SP];
static const arch_register_t *fp_reg = &sparc_gp_regs[REG_FRAME_POINTER];
static calling_convention_t  *cconv  = NULL;
static ir_mode               *mode_gp;
static ir_mode               *mode_fp;
68
69
static ir_mode               *mode_fp2;
//static ir_mode               *mode_fp4;
70
static pmap                  *node_to_stack;
Hannes Rapp's avatar
Hannes Rapp committed
71
72
73
74
75
76

static inline int mode_needs_gp_reg(ir_mode *mode)
{
	return mode_is_int(mode) || mode_is_reference(mode);
}

77
78
79
/**
 * Create an And that will zero out upper bits.
 *
Michael Beck's avatar
Michael Beck committed
80
81
82
 * @param dbgi      debug info
 * @param block     the basic block
 * @param op        the original node
83
84
85
86
87
88
 * @param src_bits  number of lower bits that will remain
 */
static ir_node *gen_zero_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
                                   int src_bits)
{
	if (src_bits == 8) {
89
		return new_bd_sparc_And_imm(dbgi, block, op, NULL, 0xFF);
90
	} else if (src_bits == 16) {
91
92
		ir_node *lshift = new_bd_sparc_Sll_imm(dbgi, block, op, NULL, 16);
		ir_node *rshift = new_bd_sparc_Slr_imm(dbgi, block, lshift, NULL, 16);
93
94
95
96
97
98
99
100
		return rshift;
	} else {
		panic("zero extension only supported for 8 and 16 bits");
	}
}

/**
 * Generate code for a sign extension.
Michael Beck's avatar
Michael Beck committed
101
102
103
104
105
 *
 * @param dbgi      debug info
 * @param block     the basic block
 * @param op        the original node
 * @param src_bits  number of lower bits that will remain
106
107
108
109
110
 */
static ir_node *gen_sign_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
                                   int src_bits)
{
	int shift_width = 32 - src_bits;
111
112
	ir_node *lshift_node = new_bd_sparc_Sll_imm(dbgi, block, op, NULL, shift_width);
	ir_node *rshift_node = new_bd_sparc_Sra_imm(dbgi, block, lshift_node, NULL, shift_width);
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
	return rshift_node;
}

/**
 * returns true if it is assured, that the upper bits of a node are "clean"
 * which means for a 16 or 8 bit value, that the upper bits in the register
 * are 0 for unsigned and a copy of the last significant bit for signed
 * numbers.
 */
static bool upper_bits_clean(ir_node *transformed_node, ir_mode *mode)
{
	(void) transformed_node;
	(void) mode;
	/* TODO */
	return false;
}

Michael Beck's avatar
Michael Beck committed
130
131
132
133
134
135
136
137
/**
 * Extend a value to 32 bit signed/unsigned depending on its mode.
 *
 * @param dbgi      debug info
 * @param block     the basic block
 * @param op        the original node
 * @param orig_mode the original mode of op
 */
138
139
140
141
142
143
144
145
146
147
148
149
150
151
static ir_node *gen_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
                              ir_mode *orig_mode)
{
	int bits = get_mode_size_bits(orig_mode);
	if (bits == 32)
		return op;

	if (mode_is_signed(orig_mode)) {
		return gen_sign_extension(dbgi, block, op, bits);
	} else {
		return gen_zero_extension(dbgi, block, op, bits);
	}
}

Hannes Rapp's avatar
Hannes Rapp committed
152
153
typedef enum {
	MATCH_NONE         = 0,
Michael Beck's avatar
Michael Beck committed
154
	MATCH_COMMUTATIVE  = 1 << 0, /**< commutative operation. */
Hannes Rapp's avatar
Hannes Rapp committed
155
156
157
} match_flags_t;

typedef ir_node* (*new_binop_reg_func) (dbg_info *dbgi, ir_node *block, ir_node *op1, ir_node *op2);
Hannes Rapp's avatar
Hannes Rapp committed
158
typedef ir_node* (*new_binop_fp_func) (dbg_info *dbgi, ir_node *block, ir_node *op1, ir_node *op2, ir_mode *mode);
159
typedef ir_node* (*new_binop_imm_func) (dbg_info *dbgi, ir_node *block, ir_node *op1, ir_entity *entity, int32_t immediate);
160
typedef ir_node* (*new_unop_fp_func) (dbg_info *dbgi, ir_node *block, ir_node *op1, ir_mode *mode);
Hannes Rapp's avatar
Hannes Rapp committed
161

162
163
164
165
166
static bool is_value_imm_encodeable(int32_t value)
{
	return -4096 <= value && value <= 4095;
}

Hannes Rapp's avatar
Hannes Rapp committed
167
/**
168
 * checks if a node's value can be encoded as a immediate
Hannes Rapp's avatar
Hannes Rapp committed
169
170
171
 */
static bool is_imm_encodeable(const ir_node *node)
{
172
	long value;
Hannes Rapp's avatar
Hannes Rapp committed
173
174
175
	if (!is_Const(node))
		return false;

176
	value = get_tarval_long(get_Const_tarval(node));
177
	return is_value_imm_encodeable(value);
Hannes Rapp's avatar
Hannes Rapp committed
178
179
180
181
182
}

/**
 * helper function for binop operations
 *
Michael Beck's avatar
Michael Beck committed
183
184
 * @param new_reg  register generation function ptr
 * @param new_imm  immediate generation function ptr
Hannes Rapp's avatar
Hannes Rapp committed
185
 */
Matthias Braun's avatar
Matthias Braun committed
186
187
188
189
190
static ir_node *gen_helper_binop_args(ir_node *node,
                                      ir_node *op1, ir_node *op2,
                                      match_flags_t flags,
                                      new_binop_reg_func new_reg,
                                      new_binop_imm_func new_imm)
Hannes Rapp's avatar
Hannes Rapp committed
191
{
Matthias Braun's avatar
Matthias Braun committed
192
	dbg_info *dbgi    = get_irn_dbg_info(node);
Hannes Rapp's avatar
Hannes Rapp committed
193
194
195
196
197
198
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *new_op1;
	ir_node  *new_op2;

	if (is_imm_encodeable(op2)) {
		ir_node *new_op1 = be_transform_node(op1);
199
200
		int32_t immediate = get_tarval_long(get_Const_tarval(op2));
		return new_imm(dbgi, block, new_op1, NULL, immediate);
Hannes Rapp's avatar
Hannes Rapp committed
201
202
203
204
	}
	new_op2 = be_transform_node(op2);

	if ((flags & MATCH_COMMUTATIVE) && is_imm_encodeable(op1)) {
205
206
		int32_t immediate = get_tarval_long(get_Const_tarval(op1));
		return new_imm(dbgi, block, new_op2, NULL, immediate);
Hannes Rapp's avatar
Hannes Rapp committed
207
208
209
210
211
212
	}
	new_op1 = be_transform_node(op1);

	return new_reg(dbgi, block, new_op1, new_op2);
}

Matthias Braun's avatar
Matthias Braun committed
213
214
215
216
217
218
219
220
221
static ir_node *gen_helper_binop(ir_node *node, match_flags_t flags,
                                 new_binop_reg_func new_reg,
                                 new_binop_imm_func new_imm)
{
	ir_node *op1 = get_binop_left(node);
	ir_node *op2 = get_binop_right(node);
	return gen_helper_binop_args(node, op1, op2, flags, new_reg, new_imm);
}

Hannes Rapp's avatar
Hannes Rapp committed
222
223
224
/**
 * helper function for FP binop operations
 */
225
static ir_node *gen_helper_binfpop(ir_node *node, ir_mode *mode,
226
227
228
                                   new_binop_fp_func new_func_single,
                                   new_binop_fp_func new_func_double,
                                   new_binop_fp_func new_func_quad)
Hannes Rapp's avatar
Hannes Rapp committed
229
230
231
{
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *op1     = get_binop_left(node);
232
	ir_node  *new_op1 = be_transform_node(op1);
Hannes Rapp's avatar
Hannes Rapp committed
233
	ir_node  *op2     = get_binop_right(node);
234
	ir_node  *new_op2 = be_transform_node(op2);
Hannes Rapp's avatar
Hannes Rapp committed
235
	dbg_info *dbgi    = get_irn_dbg_info(node);
236
237
238
239
240
241
242
243
244
245
246
247
248
	unsigned  bits    = get_mode_size_bits(mode);

	switch (bits) {
	case 32:
		return new_func_single(dbgi, block, new_op1, new_op2, mode);
	case 64:
		return new_func_double(dbgi, block, new_op1, new_op2, mode);
	case 128:
		return new_func_quad(dbgi, block, new_op1, new_op2, mode);
	default:
		break;
	}
	panic("unsupported mode %+F for float op", mode);
Hannes Rapp's avatar
Hannes Rapp committed
249
250
}

251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
static ir_node *gen_helper_unfpop(ir_node *node, ir_mode *mode,
                                  new_unop_fp_func new_func_single,
                                  new_unop_fp_func new_func_double,
                                  new_unop_fp_func new_func_quad)
{
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *op1     = get_binop_left(node);
	ir_node  *new_op1 = be_transform_node(op1);
	dbg_info *dbgi    = get_irn_dbg_info(node);
	unsigned  bits    = get_mode_size_bits(mode);

	switch (bits) {
	case 32:
		return new_func_single(dbgi, block, new_op1, mode);
	case 64:
		return new_func_double(dbgi, block, new_op1, mode);
	case 128:
		return new_func_quad(dbgi, block, new_op1, mode);
	default:
		break;
	}
	panic("unsupported mode %+F for float op", mode);
}

275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
typedef struct address_t {
	ir_node   *base;
	ir_entity *entity;
	int32_t    offset;
} address_t;

static void match_address(ir_node *ptr, address_t *address)
{
	ir_node   *base   = ptr;
	int32_t    offset = 0;
	ir_entity *entity = NULL;

	if (is_Add(base)) {
		ir_node *add_right = get_Add_right(base);
		if (is_Const(add_right)) {
			base    = get_Add_left(base);
			offset += get_tarval_long(get_Const_tarval(add_right));
		}
	}
	/* Note that we don't match sub(x, Const) or chains of adds/subs
	 * because this should all be normalized by now */

	/* we only use the symconst if we're the only user otherwise we probably
	 * won't save anything but produce multiple sethi+or combinations with
	 * just different offsets */
	if (is_SymConst(base) && get_irn_n_edges(base) == 1) {
		dbg_info *dbgi      = get_irn_dbg_info(ptr);
		ir_node  *block     = get_nodes_block(ptr);
		ir_node  *new_block = be_transform_node(block);
		entity = get_SymConst_entity(base);
		base   = new_bd_sparc_SetHi(dbgi, new_block, entity, offset);
	} else {
		if (is_value_imm_encodeable(offset)) {
			base = be_transform_node(base);
		} else {
			base   = be_transform_node(ptr);
			offset = 0;
		}
	}

	address->base   = base;
	address->entity = entity;
	address->offset = offset;
}

Hannes Rapp's avatar
Hannes Rapp committed
320
321
322
323
324
325
326
327
/**
 * Creates an sparc Add.
 *
 * @param node   FIRM node
 * @return the created sparc Add node
 */
static ir_node *gen_Add(ir_node *node)
{
328
329
	ir_mode *mode = get_irn_mode(node);
	ir_node *right;
Hannes Rapp's avatar
Hannes Rapp committed
330

331
	if (mode_is_float(mode)) {
332
333
		return gen_helper_binfpop(node, mode, new_bd_sparc_fadd_s,
		                          new_bd_sparc_fadd_d, new_bd_sparc_fadd_q);
334
	}
Hannes Rapp's avatar
Hannes Rapp committed
335

336
	/* special case: + 0x1000 can be represented as - 0x1000 */
337
	right = get_Add_right(node);
338
	if (is_Const(right)) {
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
		tarval  *tv;
		uint32_t val;
		ir_node *left = get_Add_left(node);
		/* is this simple address arithmetic? then we can let the linker do
		 * the calculation. */
		if (is_SymConst(left)) {
			dbg_info *dbgi  = get_irn_dbg_info(node);
			ir_node  *block = be_transform_node(get_nodes_block(node));
			address_t address;

			match_address(node, &address);
			assert(is_sparc_SetHi(address.base));
			return new_bd_sparc_Or_imm(dbgi, block, address.base,
			                           address.entity, address.offset);
		}

		tv  = get_Const_tarval(right);
		val = get_tarval_long(tv);
357
358
359
360
361
		if (val == 0x1000) {
			dbg_info *dbgi   = get_irn_dbg_info(node);
			ir_node  *block  = be_transform_node(get_nodes_block(node));
			ir_node  *op     = get_Add_left(node);
			ir_node  *new_op = be_transform_node(op);
362
			return new_bd_sparc_Sub_imm(dbgi, block, new_op, NULL, -0x1000);
363
364
365
		}
	}

366
367
	return gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_Add_reg,
	                        new_bd_sparc_Add_imm);
Hannes Rapp's avatar
Hannes Rapp committed
368
369
370
371
372
373
374
375
376
377
}

/**
 * Creates an sparc Sub.
 *
 * @param node       FIRM node
 * @return the created sparc Sub node
 */
static ir_node *gen_Sub(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
378
	ir_mode *mode = get_irn_mode(node);
379
380

	if (mode_is_float(mode)) {
381
382
		return gen_helper_binfpop(node, mode, new_bd_sparc_fsub_s,
		                          new_bd_sparc_fsub_d, new_bd_sparc_fsub_q);
383
	}
Hannes Rapp's avatar
Hannes Rapp committed
384

385
	return gen_helper_binop(node, MATCH_NONE, new_bd_sparc_Sub_reg, new_bd_sparc_Sub_imm);
Hannes Rapp's avatar
Hannes Rapp committed
386
387
}

388
389
static ir_node *create_ldf(dbg_info *dbgi, ir_node *block, ir_node *ptr,
                           ir_node *mem, ir_mode *mode, ir_entity *entity,
390
                           long offset, bool is_frame_entity)
391
392
393
394
395
{
	unsigned bits = get_mode_size_bits(mode);
	assert(mode_is_float(mode));
	if (bits == 32) {
		return new_bd_sparc_Ldf_s(dbgi, block, ptr, mem, mode, entity,
396
		                          offset, is_frame_entity);
397
398
	} else if (bits == 64) {
		return new_bd_sparc_Ldf_d(dbgi, block, ptr, mem, mode, entity,
399
		                          offset, is_frame_entity);
400
401
402
	} else {
		assert(bits == 128);
		return new_bd_sparc_Ldf_q(dbgi, block, ptr, mem, mode, entity,
403
		                          offset, is_frame_entity);
404
405
406
407
408
	}
}

static ir_node *create_stf(dbg_info *dbgi, ir_node *block, ir_node *ptr,
                           ir_node *value, ir_node *mem, ir_mode *mode,
409
                           ir_entity *entity, long offset,
410
411
412
413
414
415
                           bool is_frame_entity)
{
	unsigned bits = get_mode_size_bits(mode);
	assert(mode_is_float(mode));
	if (bits == 32) {
		return new_bd_sparc_Stf_s(dbgi, block, ptr, value, mem, mode, entity,
416
		                          offset, is_frame_entity);
417
418
	} else if (bits == 64) {
		return new_bd_sparc_Stf_d(dbgi, block, ptr, value, mem, mode, entity,
419
		                          offset, is_frame_entity);
420
421
422
	} else {
		assert(bits == 128);
		return new_bd_sparc_Stf_q(dbgi, block, ptr, value, mem, mode, entity,
423
		                          offset, is_frame_entity);
424
425
426
	}
}

Hannes Rapp's avatar
Hannes Rapp committed
427
428
429
430
431
432
433
434
/**
 * Transforms a Load.
 *
 * @param node    the ir Load node
 * @return the created sparc Load node
 */
static ir_node *gen_Load(ir_node *node)
{
435
	dbg_info *dbgi     = get_irn_dbg_info(node);
Hannes Rapp's avatar
Hannes Rapp committed
436
437
438
439
440
441
	ir_mode  *mode     = get_Load_mode(node);
	ir_node  *block    = be_transform_node(get_nodes_block(node));
	ir_node  *ptr      = get_Load_ptr(node);
	ir_node  *mem      = get_Load_mem(node);
	ir_node  *new_mem  = be_transform_node(mem);
	ir_node  *new_load = NULL;
442
443
444
	address_t address;

	match_address(ptr, &address);
Hannes Rapp's avatar
Hannes Rapp committed
445

446
	if (mode_is_float(mode)) {
447
448
		new_load = create_ldf(dbgi, block, address.base, new_mem, mode,
		                      address.entity, address.offset, false);
449
	} else {
450
451
		new_load = new_bd_sparc_Ld(dbgi, block, address.base, new_mem, mode,
		                           address.entity, address.offset, false);
452
	}
Hannes Rapp's avatar
Hannes Rapp committed
453
454
	set_irn_pinned(new_load, get_irn_pinned(node));

Hannes Rapp's avatar
Hannes Rapp committed
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
	return new_load;
}

/**
 * Transforms a Store.
 *
 * @param node    the ir Store node
 * @return the created sparc Store node
 */
static ir_node *gen_Store(ir_node *node)
{
	ir_node  *block    = be_transform_node(get_nodes_block(node));
	ir_node  *ptr      = get_Store_ptr(node);
	ir_node  *mem      = get_Store_mem(node);
	ir_node  *new_mem  = be_transform_node(mem);
	ir_node  *val      = get_Store_value(node);
	ir_node  *new_val  = be_transform_node(val);
	ir_mode  *mode     = get_irn_mode(val);
	dbg_info *dbgi     = get_irn_dbg_info(node);
474
475
476
477
	ir_node  *new_store = NULL;
	address_t address;

	match_address(ptr, &address);
Hannes Rapp's avatar
Hannes Rapp committed
478

479
	if (mode_is_float(mode)) {
480
481
		new_store = create_stf(dbgi, block, address.base, new_val, new_mem,
		                       mode, address.entity, address.offset, false);
482
	} else {
483
484
485
		new_store = new_bd_sparc_St(dbgi, block, address.base, new_val, new_mem,
		                            mode, address.entity, address.offset,
		                            false);
486
487
	}
	set_irn_pinned(new_store, get_irn_pinned(node));
Hannes Rapp's avatar
Hannes Rapp committed
488
489
490
491

	return new_store;
}

492
493
/**
 * Creates an sparc Mul.
494
 * returns the lower 32bits of the 64bit multiply result
495
496
497
 *
 * @return the created sparc Mul node
 */
498
499
500
static ir_node *gen_Mul(ir_node *node)
{
	ir_mode *mode = get_irn_mode(node);
Hannes Rapp's avatar
Hannes Rapp committed
501
	if (mode_is_float(mode)) {
502
503
		return gen_helper_binfpop(node, mode, new_bd_sparc_fmul_s,
		                          new_bd_sparc_fmul_d, new_bd_sparc_fmul_q);
Hannes Rapp's avatar
Hannes Rapp committed
504
	}
505
506

	assert(mode_is_data(mode));
507
	return gen_helper_binop(node, MATCH_COMMUTATIVE,
508
	                        new_bd_sparc_Mul_reg, new_bd_sparc_Mul_imm);
509
510
511
512
513
514
515
516
}

/**
 * Creates an sparc Mulh.
 * Mulh returns the upper 32bits of a mul instruction
 *
 * @return the created sparc Mulh node
 */
517
518
static ir_node *gen_Mulh(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
519
	ir_mode *mode = get_irn_mode(node);
520
521
522
523
524
525
526
527
	ir_node *mul;
	ir_node *proj_res_hi;

	if (mode_is_float(mode))
		panic("FP not supported yet");


	assert(mode_is_data(mode));
528
	mul = gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_Mulh_reg, new_bd_sparc_Mulh_imm);
Hannes Rapp's avatar
Hannes Rapp committed
529
	//arch_irn_add_flags(mul, arch_irn_flags_modify_flags);
Michael Beck's avatar
Michael Beck committed
530
	proj_res_hi = new_r_Proj(mul, mode_gp, pn_sparc_Mulh_low);
531
	return proj_res_hi;
532
}
Hannes Rapp's avatar
Hannes Rapp committed
533

534
535
536
537
538
/**
 * Creates an sparc Div.
 *
 * @return the created sparc Div node
 */
539
540
541
static ir_node *gen_Div(ir_node *node)
{
	ir_mode *mode = get_Div_resmode(node);
542
543
	ir_node *res;

544
	assert(!mode_is_float(mode));
545
546
547
548
549
550
551
552
	if (mode_is_signed(mode)) {
		res = gen_helper_binop(node, 0, new_bd_sparc_SDiv_reg,
		                       new_bd_sparc_SDiv_imm);
	} else {
		res = gen_helper_binop(node, 0, new_bd_sparc_UDiv_reg,
		                       new_bd_sparc_UDiv_imm);
	}
	return res;
553
554
}

555
556
557
558
static ir_node *gen_Quot(ir_node *node)
{
	ir_mode *mode = get_Quot_resmode(node);
	assert(mode_is_float(mode));
559
560
	return gen_helper_binfpop(node, mode, new_bd_sparc_fdiv_s,
	                          new_bd_sparc_fdiv_d, new_bd_sparc_fdiv_q);
561
}
562

563
564
565
static ir_node *gen_Abs(ir_node *node)
{
	ir_mode *const mode = get_irn_mode(node);
566

567
	if (mode_is_float(mode)) {
568
569
		return gen_helper_unfpop(node, mode, new_bd_sparc_fabs_s,
		                         new_bd_sparc_fabs_d, new_bd_sparc_fabs_q);
570
571
572
573
574
	} else {
		ir_node  *const block  = be_transform_node(get_nodes_block(node));
		dbg_info *const dbgi   = get_irn_dbg_info(node);
		ir_node  *const op     = get_Abs_op(node);
		ir_node  *const new_op = be_transform_node(op);
575
		ir_node  *const sra    = new_bd_sparc_Sra_imm(dbgi, block, new_op, NULL, 31);
576
577
578
		ir_node  *const xor    = new_bd_sparc_Xor_reg(dbgi, block, new_op, sra);
		ir_node  *const sub    = new_bd_sparc_Sub_reg(dbgi, block, xor,    sra);
		return sub;
579
	}
580
581
}

582
583
584
585
586
static ir_node *get_g0(void)
{
	return be_prolog_get_reg_value(abihelper, &sparc_gp_regs[REG_G0]);
}

587
588
589
/**
 * Transforms a Not node.
 *
Michael Beck's avatar
Michael Beck committed
590
 * @return the created sparc Not node
591
592
593
 */
static ir_node *gen_Not(ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
594
595
	ir_node  *op     = get_Not_op(node);
	ir_node  *zero   = get_g0();
596
597
598
	dbg_info *dbgi   = get_irn_dbg_info(node);
	ir_node  *block  = be_transform_node(get_nodes_block(node));
	ir_node  *new_op = be_transform_node(op);
599

Matthias Braun's avatar
Matthias Braun committed
600
601
602
603
	/* Note: Not(Eor()) is normalize in firm locatopts already so
	 * we don't match it for xnor here */

	/* Not can be represented with xnor 0, n */
604
	return new_bd_sparc_XNor_reg(dbgi, block, zero, new_op);
605
606
}

607
608
static ir_node *gen_And(ir_node *node)
{
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
	ir_node *left  = get_And_left(node);
	ir_node *right = get_And_right(node);

	if (is_Not(right)) {
		ir_node *not_op = get_Not_op(right);
		return gen_helper_binop_args(node, left, not_op, MATCH_NONE,
		                             new_bd_sparc_AndN_reg,
		                             new_bd_sparc_AndN_imm);
	}
	if (is_Not(left)) {
		ir_node *not_op = get_Not_op(left);
		return gen_helper_binop_args(node, right, not_op, MATCH_NONE,
		                             new_bd_sparc_AndN_reg,
		                             new_bd_sparc_AndN_imm);
	}

	return gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_And_reg,
	                        new_bd_sparc_And_imm);
627
628
629
630
}

static ir_node *gen_Or(ir_node *node)
{
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
	ir_node *left  = get_Or_left(node);
	ir_node *right = get_Or_right(node);

	if (is_Not(right)) {
		ir_node *not_op = get_Not_op(right);
		return gen_helper_binop_args(node, left, not_op, MATCH_NONE,
		                             new_bd_sparc_OrN_reg,
		                             new_bd_sparc_OrN_imm);
	}
	if (is_Not(left)) {
		ir_node *not_op = get_Not_op(left);
		return gen_helper_binop_args(node, right, not_op, MATCH_NONE,
		                             new_bd_sparc_OrN_reg,
		                             new_bd_sparc_OrN_imm);
	}

	return gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_Or_reg,
	                        new_bd_sparc_Or_imm);
649
650
}

651
static ir_node *gen_Eor(ir_node *node)
Hannes Rapp's avatar
Hannes Rapp committed
652
{
Matthias Braun's avatar
Matthias Braun committed
653
654
655
656
657
658
659
660
661
	ir_node *left  = get_Eor_left(node);
	ir_node *right = get_Eor_right(node);

	if (is_Not(right)) {
		ir_node *not_op = get_Not_op(right);
		return gen_helper_binop_args(node, left, not_op, MATCH_COMMUTATIVE,
		                             new_bd_sparc_XNor_reg,
		                             new_bd_sparc_XNor_imm);
	}
662
663
664
665
666
667
668
	if (is_Not(left)) {
		ir_node *not_op = get_Not_op(left);
		return gen_helper_binop_args(node, not_op, right,
		                             MATCH_COMMUTATIVE | MATCH_MODE_NEUTRAL,
		                             new_bd_sparc_XNor_reg,
		                             new_bd_sparc_XNor_imm);
	}
Matthias Braun's avatar
Matthias Braun committed
669
670
671

	return gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_Xor_reg,
	                        new_bd_sparc_Xor_imm);
Hannes Rapp's avatar
Hannes Rapp committed
672
673
}

674
675
static ir_node *gen_Shl(ir_node *node)
{
676
	return gen_helper_binop(node, MATCH_NONE, new_bd_sparc_Sll_reg, new_bd_sparc_Sll_imm);
677
678
679
680
}

static ir_node *gen_Shr(ir_node *node)
{
681
	return gen_helper_binop(node, MATCH_NONE, new_bd_sparc_Slr_reg, new_bd_sparc_Slr_imm);
682
}
Hannes Rapp's avatar
Hannes Rapp committed
683

684
static ir_node *gen_Shrs(ir_node *node)
685
{
686
	return gen_helper_binop(node, MATCH_NONE, new_bd_sparc_Sra_reg, new_bd_sparc_Sra_imm);
687
688
}

689
690
691
692
693
/**
 * Transforms a Minus node.
 */
static ir_node *gen_Minus(ir_node *node)
{
694
695
696
697
698
699
	ir_mode  *mode = get_irn_mode(node);
	ir_node  *op;
	ir_node  *block;
	ir_node  *new_op;
	ir_node  *zero;
	dbg_info *dbgi;
700
701

	if (mode_is_float(mode)) {
702
703
		return gen_helper_unfpop(node, mode, new_bd_sparc_fneg_s,
		                         new_bd_sparc_fneg_d, new_bd_sparc_fneg_q);
704
	}
705
706
707
708
709
710
	block  = be_transform_node(get_nodes_block(node));
	dbgi   = get_irn_dbg_info(node);
	op     = get_Minus_op(node);
	new_op = be_transform_node(op);
	zero   = get_g0();
	return new_bd_sparc_Sub_reg(dbgi, block, zero, new_op);
711
712
}

713
/**
Michael Beck's avatar
Michael Beck committed
714
 * Create an entity for a given (floating point) tarval
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
 */
static ir_entity *create_float_const_entity(tarval *tv)
{
	ir_entity        *entity = (ir_entity*) pmap_get(env_cg->constants, tv);
	ir_initializer_t *initializer;
	ir_mode          *mode;
	ir_type          *type;
	ir_type          *glob;

	if (entity != NULL)
		return entity;

	mode   = get_tarval_mode(tv);
	type   = get_type_for_mode(mode);
	glob   = get_glob_type();
	entity = new_entity(glob, id_unique("C%u"), type);
	set_entity_visibility(entity, ir_visibility_private);
	add_entity_linkage(entity, IR_LINKAGE_CONSTANT);

	initializer = create_initializer_tarval(tv);
	set_entity_initializer(entity, initializer);

	pmap_insert(env_cg->constants, tv, entity);
	return entity;
}

741
742
static ir_node *gen_Const(ir_node *node)
{
743
744
745
746
747
	ir_node  *block = be_transform_node(get_nodes_block(node));
	ir_mode  *mode  = get_irn_mode(node);
	dbg_info *dbgi  = get_irn_dbg_info(node);
	tarval   *tv;
	long      value;
Hannes Rapp's avatar
Hannes Rapp committed
748
749

	if (mode_is_float(mode)) {
750
751
		tarval    *tv     = get_Const_tarval(node);
		ir_entity *entity = create_float_const_entity(tv);
752
		ir_node   *hi     = new_bd_sparc_SetHi(dbgi, block, entity, 0);
753
754
		ir_node   *mem    = new_NoMem();
		ir_node   *new_op
755
			= create_ldf(dbgi, block, hi, mem, mode, entity, 0, false);
756
		ir_node   *proj   = new_Proj(new_op, mode, pn_sparc_Ldf_res);
757
		be_dep_on_frame(hi);
758
759

		set_irn_pinned(new_op, op_pin_state_floats);
760
		return proj;
Hannes Rapp's avatar
Hannes Rapp committed
761
	}
Hannes Rapp's avatar
Hannes Rapp committed
762

763
764
765
	tv    = get_Const_tarval(node);
	value = get_tarval_long(tv);
	if (value == 0) {
766
		return get_g0();
767
	} else if (-4096 <= value && value <= 4095) {
768
		return new_bd_sparc_Or_imm(dbgi, block, get_g0(), NULL, value);
769
	} else {
770
		ir_node *hi = new_bd_sparc_SetHi(dbgi, block, NULL, value);
771
772
		be_dep_on_frame(hi);
		if ((value & 0x3ff) != 0) {
773
			return new_bd_sparc_Or_imm(dbgi, block, hi, NULL, value & 0x3ff);
774
775
776
		} else {
			return hi;
		}
777
	}
Hannes Rapp's avatar
Hannes Rapp committed
778
779
}

780
static ir_mode *get_cmp_mode(ir_node *b_value)
781
782
783
784
785
786
787
788
789
790
{
	ir_node *pred;
	ir_node *op;

	if (!is_Proj(b_value))
		panic("can't determine cond signednes");
	pred = get_Proj_pred(b_value);
	if (!is_Cmp(pred))
		panic("can't determine cond signednes (no cmp)");
	op = get_Cmp_left(pred);
791
	return get_irn_mode(op);
792
793
}

Hannes Rapp's avatar
Hannes Rapp committed
794
795
796
797
798
799
800
/**
 * Transform Cond nodes
 */
static ir_node *gen_Cond(ir_node *node)
{
	ir_node  *selector = get_Cond_selector(node);
	ir_mode  *mode     = get_irn_mode(selector);
801
	ir_mode  *cmp_mode;
Hannes Rapp's avatar
Hannes Rapp committed
802
803
	ir_node  *block;
	ir_node  *flag_node;
804
805
	bool      is_unsigned;
	pn_Cmp    pnc;
Hannes Rapp's avatar
Hannes Rapp committed
806
807
808
809
	dbg_info *dbgi;

	// switch/case jumps
	if (mode != mode_b) {
810
		panic("SwitchJump not implemented yet");
Hannes Rapp's avatar
Hannes Rapp committed
811
812
813
814
	}

	// regular if/else jumps
	assert(is_Proj(selector));
815
	assert(is_Cmp(get_Proj_pred(selector)));
Hannes Rapp's avatar
Hannes Rapp committed
816

817
818
	cmp_mode = get_cmp_mode(selector);

819
820
821
822
	block       = be_transform_node(get_nodes_block(node));
	dbgi        = get_irn_dbg_info(node);
	flag_node   = be_transform_node(get_Proj_pred(selector));
	pnc         = get_Proj_proj(selector);
823
824
825
826
827
828
829
	is_unsigned = !mode_is_signed(cmp_mode);
	if (mode_is_float(cmp_mode)) {
		assert(!is_unsigned);
		return new_bd_sparc_fbfcc(dbgi, block, flag_node, pnc);
	} else {
		return new_bd_sparc_Bicc(dbgi, block, flag_node, pnc, is_unsigned);
	}
Hannes Rapp's avatar
Hannes Rapp committed
830
831
832
833
834
835
836
837
838
839
840
841
}

/**
 * transform Cmp
 */
static ir_node *gen_Cmp(ir_node *node)
{
	ir_node  *block    = be_transform_node(get_nodes_block(node));
	ir_node  *op1      = get_Cmp_left(node);
	ir_node  *op2      = get_Cmp_right(node);
	ir_mode  *cmp_mode = get_irn_mode(op1);
	dbg_info *dbgi     = get_irn_dbg_info(node);
842
843
	ir_node  *new_op1  = be_transform_node(op1);
	ir_node  *new_op2  = be_transform_node(op2);
Hannes Rapp's avatar
Hannes Rapp committed
844
845
	assert(get_irn_mode(op2) == cmp_mode);

846
	if (mode_is_float(cmp_mode)) {
847
848
849
850
851
852
853
854
855
		unsigned bits = get_mode_size_bits(cmp_mode);
		if (bits == 32) {
			return new_bd_sparc_fcmp_s(dbgi, block, new_op1, new_op2, cmp_mode);
		} else if (bits == 64) {
			return new_bd_sparc_fcmp_d(dbgi, block, new_op1, new_op2, cmp_mode);
		} else {
			assert(bits == 128);
			return new_bd_sparc_fcmp_q(dbgi, block, new_op1, new_op2, cmp_mode);
		}
Hannes Rapp's avatar
Hannes Rapp committed
856
857
858
	}

	/* integer compare */
859
860
	new_op1 = gen_extension(dbgi, block, new_op1, cmp_mode);
	new_op2 = gen_extension(dbgi, block, new_op2, cmp_mode);
861
	return new_bd_sparc_Cmp_reg(dbgi, block, new_op1, new_op2);
Hannes Rapp's avatar
Hannes Rapp committed
862
863
864
865
866
867
868
}

/**
 * Transforms a SymConst node.
 */
static ir_node *gen_SymConst(ir_node *node)
{
869
870
871
872
873
874
875
	ir_entity *entity    = get_SymConst_entity(node);
	dbg_info  *dbgi      = get_irn_dbg_info(node);
	ir_node   *block     = get_nodes_block(node);
	ir_node   *new_block = be_transform_node(block);
	ir_node   *hi        = new_bd_sparc_SetHi(dbgi, new_block, entity, 0);
	ir_node   *low       = new_bd_sparc_Or_imm(dbgi, new_block, hi, entity, 0);
	be_dep_on_frame(hi);
Hannes Rapp's avatar
Hannes Rapp committed
876

877
	return low;
Hannes Rapp's avatar
Hannes Rapp committed
878
879
}

880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
static ir_node *create_fftof(dbg_info *dbgi, ir_node *block, ir_node *op,
                             ir_mode *src_mode, ir_mode *dst_mode)
{
	unsigned src_bits = get_mode_size_bits(src_mode);
	unsigned dst_bits = get_mode_size_bits(dst_mode);
	if (src_bits == 32) {
		if (dst_bits == 64) {
			return new_bd_sparc_fftof_s_d(dbgi, block, op, src_mode, dst_mode);
		} else {
			assert(dst_bits == 128);
			return new_bd_sparc_fftof_s_q(dbgi, block, op, src_mode, dst_mode);
		}
	} else if (src_bits == 64) {
		if (dst_bits == 32) {
			return new_bd_sparc_fftof_d_s(dbgi, block, op, src_mode, dst_mode);
		} else {
			assert(dst_bits == 128);
			return new_bd_sparc_fftof_d_q(dbgi, block, op, src_mode, dst_mode);
		}
	} else {
		assert(src_bits == 128);
		if (dst_bits == 32) {
			return new_bd_sparc_fftof_q_s(dbgi, block, op, src_mode, dst_mode);
		} else {
			assert(dst_bits == 64);
			return new_bd_sparc_fftof_q_d(dbgi, block, op, src_mode, dst_mode);
		}
	}
}

static ir_node *create_ftoi(dbg_info *dbgi, ir_node *block, ir_node *op,
                            ir_mode *src_mode)
{
	unsigned bits = get_mode_size_bits(src_mode);
	if (bits == 32) {
		return new_bd_sparc_fftoi_s(dbgi, block, op, src_mode);
	} else if (bits == 64) {
		return new_bd_sparc_fftoi_d(dbgi, block, op, src_mode);
	} else {
		assert(bits == 128);
		return new_bd_sparc_fftoi_q(dbgi, block, op, src_mode);
	}
}

static ir_node *create_itof(dbg_info *dbgi, ir_node *block, ir_node *op,
                            ir_mode *dst_mode)
{
	unsigned bits = get_mode_size_bits(dst_mode);
	if (bits == 32) {
		return new_bd_sparc_fitof_s(dbgi, block, op, dst_mode);
	} else if (bits == 64) {
		return new_bd_sparc_fitof_d(dbgi, block, op, dst_mode);
	} else {
		assert(bits == 128);
		return new_bd_sparc_fitof_q(dbgi, block, op, dst_mode);
	}
}

Hannes Rapp's avatar
Hannes Rapp committed
938
939
940
941
/**
 * Transforms a Conv node.
 *
 */
942
943
static ir_node *gen_Conv(ir_node *node)
{
Hannes Rapp's avatar
Hannes Rapp committed
944
945
946
947
948
949
950
	ir_node  *block    = be_transform_node(get_nodes_block(node));
	ir_node  *op       = get_Conv_op(node);
	ir_node  *new_op   = be_transform_node(op);
	ir_mode  *src_mode = get_irn_mode(op);
	ir_mode  *dst_mode = get_irn_mode(node);
	dbg_info *dbg      = get_irn_dbg_info(node);

Hannes Rapp's avatar
Hannes Rapp committed
951
952
953
	int src_bits = get_mode_size_bits(src_mode);
	int dst_bits = get_mode_size_bits(dst_mode);

Hannes Rapp's avatar
Hannes Rapp committed
954
955
956
957
	if (src_mode == dst_mode)
		return new_op;

	if (mode_is_float(src_mode) || mode_is_float(dst_mode)) {
Hannes Rapp's avatar
Hannes Rapp committed
958
959
960
961
		assert((src_bits <= 64 && dst_bits <= 64) && "quad FP not implemented");

		if (mode_is_float(src_mode)) {
			if (mode_is_float(dst_mode)) {
Michael Beck's avatar
Michael Beck committed
962
				/* float -> float conv */
963
				return create_fftof(dbg, block, new_op, src_mode, dst_mode);
Hannes Rapp's avatar
Hannes Rapp committed
964
			} else {
Michael Beck's avatar
Michael Beck committed
965
				/* float -> int conv */
966
967
				if (!mode_is_signed(dst_mode))
					panic("float to unsigned not implemented yet");
968
				return create_ftoi(dbg, block, new_op, src_mode);
Hannes Rapp's avatar
Hannes Rapp committed
969
970
			}
		} else {
Michael Beck's avatar
Michael Beck committed
971
			/* int -> float conv */
972
973
			if (!mode_is_signed(src_mode))
				panic("unsigned to float not implemented yet");
974
			return create_itof(dbg, block, new_op, dst_mode);
Hannes Rapp's avatar
Hannes Rapp committed
975
		}
Hannes Rapp's avatar
Hannes Rapp committed
976
977
978
979
980
	} else { /* complete in gp registers */
		int min_bits;
		ir_mode *min_mode;

		if (src_bits == dst_bits) {
Michael Beck's avatar
Michael Beck committed
981
			/* kill unnecessary conv */
Hannes Rapp's avatar
Hannes Rapp committed
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
			return new_op;
		}

		if (src_bits < dst_bits) {
			min_bits = src_bits;
			min_mode = src_mode;
		} else {
			min_bits = dst_bits;
			min_mode = dst_mode;
		}

		if (upper_bits_clean(new_op, min_mode)) {
			return new_op;
		}

		if (mode_is_signed(min_mode)) {
			return gen_sign_extension(dbg, block, new_op, min_bits);
		} else {
			return gen_zero_extension(dbg, block, new_op, min_bits);
		}
	}
}

1005
1006
1007
1008
1009
1010
1011
1012
1013
static ir_node *gen_Unknown(ir_node *node)
{
	/* just produce a 0 */
	ir_mode *mode = get_irn_mode(node);
	if (mode_is_float(mode)) {
		panic("FP not implemented");
		be_dep_on_frame(node);
		return node;
	} else if (mode_needs_gp_reg(mode)) {
1014
		return get_g0();
1015
1016
1017
1018
1019
	}

	panic("Unexpected Unknown mode");
}

1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
/**
 * Produces the type which sits between the stack args and the locals on the
 * stack.
 */
static ir_type *sparc_get_between_type(void)
{
	static ir_type *between_type = NULL;

	if (between_type == NULL) {
		between_type = new_type_class(new_id_from_str("sparc_between_type"));
		set_type_size_bytes(between_type, SPARC_MIN_STACKSIZE);
	}

	return between_type;
}

static void create_stacklayout(ir_graph *irg)
{
	ir_entity         *entity        = get_irg_entity(irg);
	ir_type           *function_type = get_entity_type(entity);
	be_stack_layout_t *layout        = be_get_irg_stack_layout(irg);
	ir_type           *arg_type;
	int                p;
	int                n_params;

	/* calling conventions must be decided by now */
	assert(cconv != NULL);

	/* construct argument type */
	arg_type = new_type_struct(id_mangle_u(get_entity_ident(entity), new_id_from_chars("arg_type", 8)));
	n_params = get_method_n_params(function_type);
	for (p = 0; p < n_params; ++p) {
		reg_or_stackslot_t *param = &cconv->parameters[p];
		char                buf[128];
		ident              *id;

		if (param->type == NULL)
			continue;

		snprintf(buf, sizeof(buf), "param_%d", p);
		id            = new_id_from_str(buf);
		param->entity = new_entity(arg_type, id, param->type);
		set_entity_offset(param->entity, param->offset);
	}

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

	layout->frame_type     = get_irg_frame_type(irg);
	layout->between_type   = sparc_get_between_type();
	layout->arg_type       = arg_type;
	layout->initial_offset = 0;
	layout->initial_bias   = 0;
	layout->stack_dir      = -1;
	layout->sp_relative    = false;

	assert(N_FRAME_TYPES == 3);
	layout->order[0] = layout->frame_type;
	layout->order[1] = layout->between_type;
	layout->order[2] = layout->arg_type;
}

/**
 * transform the start node to the prolog code + initial barrier
 */
static ir_node *gen_Start(ir_node *node)
{
	ir_graph  *irg           = get_irn_irg(node);
	ir_entity *entity        = get_irg_entity(irg);
	ir_type   *function_type = get_entity_type(entity);
	ir_node   *block         = get_nodes_block(node);
	ir_node   *new_block     = be_transform_node(block);
	dbg_info  *dbgi          = get_irn_dbg_info(node);
	ir_node   *mem;
	ir_node   *start;
	ir_node   *sp;
	ir_node   *fp;
	ir_node   *barrier;
	ir_node   *save;
	int        i;

	/* stackpointer is important at function prolog */
	be_prolog_add_reg(abihelper, sp_reg,
			arch_register_req_type_produces_sp | arch_register_req_type_ignore);
1103
1104
	be_prolog_add_reg(abihelper, &sparc_gp_regs[REG_G0],
	        arch_register_req_type_ignore);
1105
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
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
	/* function parameters in registers */
	for (i = 0; i < get_method_n_params(function_type); ++i) {
		const reg_or_stackslot_t *param = &cconv->parameters[i];
		if (param->reg0 != NULL)
			be_prolog_add_reg(abihelper, param->reg0, 0);
		if (param->reg1 != NULL)
			be_prolog_add_reg(abihelper, param->reg1, 0);
	}

	start = be_prolog_create_start(abihelper, dbgi, new_block);

	mem  = be_prolog_get_memory(abihelper);
	sp   = be_prolog_get_reg_value(abihelper, sp_reg);
	save = new_bd_sparc_Save(NULL, block, sp, mem, SPARC_MIN_STACKSIZE);
	fp   = new_r_Proj(save, mode_gp, pn_sparc_Save_frame);
	sp   = new_r_Proj(save, mode_gp, pn_sparc_Save_stack);
	mem  = new_r_Proj(save, mode_M, pn_sparc_Save_mem);
	arch_set_irn_register(fp, fp_reg);
	arch_set_irn_register(sp, sp_reg);

	be_prolog_add_reg(abihelper, fp_reg, arch_register_req_type_ignore);
	be_prolog_set_reg_value(abihelper, fp_reg, fp);

	sp = be_new_IncSP(sp_reg, new_block, sp, BE_STACK_FRAME_SIZE_EXPAND, 0);
	be_prolog_set_reg_value(abihelper, sp_reg, sp);
	be_prolog_set_memory(abihelper, mem);

	barrier = be_prolog_create_barrier(abihelper, new_block);

	return barrier;
}

static ir_node *get_stack_pointer_for(ir_node *node)
{
	/* get predecessor in stack_order list */
	ir_node *stack_pred = be_get_stack_pred(abihelper, node);
	ir_node *stack_pred_transformed;
	ir_node *stack;

	if (stack_pred == NULL) {
		/* first stack user in the current block. We can simply use the
		 * initial sp_proj for it */
		ir_node *sp_proj = be_prolog_get_reg_value(abihelper, sp_reg);
		return sp_proj;
	}

	stack_pred_transformed = be_transform_node(stack_pred);
	stack                  = pmap_get(node_to_stack, stack_pred);
	if (stack == NULL) {
		return get_stack_pointer_for(stack_pred);
	}

	return stack;
}

/**
 * transform a Return node into epilogue code + return statement
 */
static ir_node *gen_Return(ir_node *node)
{
	ir_node  *block          = get_nodes_block(node);
	ir_node  *new_block      = be_transform_node(block);
	dbg_info *dbgi           = get_irn_dbg_info(node);
	ir_node  *mem            = get_Return_mem(node);
	ir_node  *new_mem        = be_transform_node(mem);
	ir_node  *sp_proj        = get_stack_pointer_for(node);
	int       n_res          = get_Return_n_ress(node);
	ir_node  *bereturn;
	ir_node  *incsp;
	int       i;

	be_epilog_begin(abihelper);
	be_epilog_set_memory(abihelper, new_mem);
	/* connect stack pointer with initial stack pointer. fix_stack phase
	   will later serialize all stack pointer adjusting nodes */
	be_epilog_add_reg(abihelper, sp_reg,
			arch_register_req_type_produces_sp | arch_register_req_type_ignore,
			sp_proj);

	/* result values */
	for (i = 0; i < n_res; ++i) {
		ir_node                  *res_value     = get_Return_res(node, i);
		ir_node                  *new_res_value = be_transform_node(res_value);
		const reg_or_stackslot_t *slot          = &cconv->results[i];
		const arch_register_t    *reg           = slot->reg0;
		assert(slot->reg1 == NULL);
		be_epilog_add_reg(abihelper, reg, 0, new_res_value);
	}

	/* create the barrier before the epilog code */
	be_epilog_create_barrier(abihelper, new_block);

	/* epilog code: an incsp */
	sp_proj = be_epilog_get_reg_value(abihelper, sp_reg);
	incsp   = be_new_IncSP(sp_reg, new_block, sp_proj,
	                       BE_STACK_FRAME_SIZE_SHRINK, 0);
	be_epilog_set_reg_value(abihelper, sp_reg, incsp);

	bereturn = be_epilog_create_return(abihelper, dbgi, new_block);

	return bereturn;
}

static ir_node *bitcast_int_to_float(dbg_info *dbgi, ir_node *block,
1209
                                     ir_node *value0, ir_node *value1)
1210
1211
{
	ir_graph *irg   = current_ir_graph;
1212
	ir_node  *sp    = get_irg_frame(irg);
1213
	ir_node  *nomem = new_NoMem();
1214
	ir_node  *st    = new_bd_sparc_St(dbgi, block, sp, value0, nomem, mode_gp,
1215
	                                  NULL, 0, true);
1216
	ir_mode  *mode;
1217
	ir_node  *ldf;
1218
	ir_node  *mem;
1219
1220
	set_irn_pinned(st, op_pin_state_floats);

1221
1222
	if (value1 != NULL) {
		ir_node *st1 = new_bd_sparc_St(dbgi, block, sp, value1, nomem, mode_gp,
1223
		                               NULL, 4, true);
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
		ir_node *in[2] = { st, st1 };
		ir_node *sync  = new_r_Sync(block, 2, in);
		set_irn_pinned(st1, op_pin_state_floats);
		mem  = sync;
		mode = mode_fp2;
	} else {
		mem  = st;
		mode = mode_fp;
	}

1234
	ldf = create_ldf(dbgi, block, sp, mem, mode, NULL, 0, true);
1235
1236
	set_irn_pinned(ldf, op_pin_state_floats);

1237
	return new_Proj(ldf, mode, pn_sparc_Ldf_res);
1238
1239
}

1240
1241
1242
static void bitcast_float_to_int(dbg_info *dbgi, ir_node *block,
                                 ir_node *node, ir_mode *float_mode,
                                 ir_node **result)
1243
1244
1245
1246
{
	ir_graph *irg   = current_ir_graph;
	ir_node  *stack = get_irg_frame(irg);
	ir_node  *nomem = new_NoMem();
1247
	ir_node  *stf   = create_stf(dbgi, block, stack, node, nomem, float_mode,
1248
	                             NULL, 0, true);
1249
	int       bits  = get_mode_size_bits(float_mode);
1250
1251
1252
	ir_node  *ld;
	set_irn_pinned(stf, op_pin_state_floats);

1253
	ld = new_bd_sparc_Ld(dbgi, block, stack, stf, mode_gp, NULL, 0, true);
1254
	set_irn_pinned(ld, op_pin_state_floats);
1255
	result[0] = new_Proj(ld, mode_gp, pn_sparc_Ld_res);
1256

1257
1258
	if (bits == 64) {
		ir_node *ld2 = new_bd_sparc_Ld(dbgi, block, stack, stf, mode_gp,
1259
		                               NULL, 4, true);
1260
1261
1262
1263
1264
1265
1266
1267
1268
		set_irn_pinned(ld, op_pin_state_floats);
		result[1] = new_Proj(ld2, mode_gp, pn_sparc_Ld_res);

		arch_irn_add_flags(ld, sparc_arch_irn_flag_needs_64bit_spillslot);
		arch_irn_add_flags(ld2, sparc_arch_irn_flag_needs_64bit_spillslot);
	} else {
		assert(bits == 32);
		result[1] = NULL;
	}
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
}

static ir_node *gen_Call(ir_node *node)
{
	ir_graph        *irg          = get_irn_irg(node);
	ir_node         *callee       = get_Call_ptr(node);
	ir_node         *block        = get_nodes_block(node);
	ir_node         *new_block    = be_transform_node(block);
	ir_node         *mem          = get_Call_mem(node);
	ir_node         *new_mem      = be_transform_node(mem);
	dbg_info        *dbgi         = get_irn_dbg_info(node);
	ir_type         *type         = get_Call_type(node);
	int              n_params     = get_Call_n_params(node);
	int              n_param_regs = sizeof(param_regs)/sizeof(param_regs[0]);
	/* max inputs: memory, callee, register arguments */
	int              max_inputs   = 2 + n_param_regs;
	ir_node        **in           = ALLOCAN(ir_node*, max_inputs);
	ir_node        **sync_ins     = ALLOCAN(ir_node*, max_inputs);
	struct obstack  *obst         = be_get_be_obst(irg);
	const arch_register_req_t **in_req
		= OALLOCNZ(obst, const arch_register_req_t*, max_inputs);
	calling_convention_t *cconv
		= sparc_decide_calling_convention(type, true);
	int              in_arity     = 0;
	int              sync_arity   = 0;
	int              n_caller_saves
		= sizeof(caller_saves)/sizeof(caller_saves[0]);
	ir_entity       *entity       = NULL;
	ir_node         *new_frame    = get_stack_pointer_for(node);
	ir_node         *incsp;
	int              mem_pos;
	ir_node         *res;
	int              p;
	int              i;
	int              o;
	int              out_arity;

	assert(n_params == get_method_n_params(type));

	/* construct arguments */

	/* memory input */
	in_req[in_arity] = arch_no_register_req;
	mem_pos          = in_arity;
	++in_arity;

	/* stack pointer input */
	/* construct an IncSP -> we have to always be sure that the stack is
	 * aligned even if we don't push arguments on it */
	incsp = be_new_IncSP(sp_reg, new_block, new_frame,
	                     cconv->param_stack_size, 1);
	in_req[in_arity] = sp_reg->single_req;
	in[in_arity]     = incsp;
	++in_arity;

	/* parameters */
	for (p = 0; p < n_params; ++p) {
		ir_node                  *value      = get_Call_param(node, p);
		ir_node                  *new_value  = be_transform_node(value);
		const reg_or_stackslot_t *param      = &cconv->parameters[p];
		ir_type                  *param_type = get_method_param_type(type, p);
		ir_mode                  *mode       = get_type_mode(param_type);
1331
		ir_node                  *new_values[2];
1332
1333
1334
1335
		ir_node                  *str;

		if (mode_is_float(mode) && param->reg0 != NULL) {
			unsigned size_bits = get_mode_size_bits(mode);
1336
1337
1338
1339
1340
			assert(size_bits <= 64);
			bitcast_float_to_int(dbgi, new_block, new_value, mode, new_values);
		} else {
			new_values[0] = new_value;
			new_values[1] = NULL;
1341
1342
1343
1344
		}

		/* put value into registers */
		if (param->reg0 != NULL) {
1345
			in[in_arity]     = new_values[0];
1346
1347
			in_req[in_arity] = param->reg0->single_req;
			++in_arity;
1348
			if (new_values[1] == NULL)
1349
1350
1351
				continue;
		}
		if (param->reg1 != NULL) {
1352
1353
			assert(new_values[1] != NULL);
			in[in_arity]     = new_values[1];
1354
1355
1356
1357
1358
1359
			in_req[in_arity] = param->reg1->single_req;
			++in_arity;
			continue;
		}

		/* we need a store if we're here */
1360
1361
		if (new_values[1] != NULL) {
			new_value = new_values[1];
1362
1363
1364
1365
1366
			mode      = mode_gp;
		}

		/* create a parameter frame if necessary */
		if (mode_is_float(mode)) {
1367
			str = create_stf(dbgi, new_block, incsp, new_value, new_mem,
1368
			                 mode, NULL, param->offset, true);
1369
1370
		} else {
			str = new_bd_sparc_St(dbgi, new_block, incsp, new_value, new_mem,
1371
								  mode, NULL, param->offset, true);