sparc_transform.c 47.7 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 */
#include "config.h"

#include "irnode_t.h"
#include "irgraph_t.h"
#include "irmode_t.h"
#include "irgmod.h"
#include "iredges.h"
#include "irvrfy.h"
#include "ircons.h"
#include "irprintf.h"
#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"
44
#include "../beabihelper.h"
Hannes Rapp's avatar
Hannes Rapp committed
45
46
47
48
49
50
51
52
#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"
53
#include "sparc_cconv.h"
Hannes Rapp's avatar
Hannes Rapp committed
54
55
56
57
58

#include <limits.h>

DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

59
60
61
62
63
64
65
66
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;
static pmap                  *node_to_stack;
Hannes Rapp's avatar
Hannes Rapp committed
67

Hannes Rapp's avatar
Hannes Rapp committed
68
69
70
static ir_node *gen_SymConst(ir_node *node);


Hannes Rapp's avatar
Hannes Rapp committed
71
72
73
74
75
static inline int mode_needs_gp_reg(ir_mode *mode)
{
	return mode_is_int(mode) || mode_is_reference(mode);
}

76
77
78
/**
 * Create an And that will zero out upper bits.
 *
Michael Beck's avatar
Michael Beck committed
79
80
81
 * @param dbgi      debug info
 * @param block     the basic block
 * @param op        the original node
82
83
84
85
86
87
88
89
 * @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) {
		return new_bd_sparc_And_imm(dbgi, block, op, 0xFF);
	} else if (src_bits == 16) {
90
91
		ir_node *lshift = new_bd_sparc_Sll_imm(dbgi, block, op, 16);
		ir_node *rshift = new_bd_sparc_Slr_imm(dbgi, block, lshift, 16);
92
93
94
95
96
97
98
99
		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
100
101
102
103
104
 *
 * @param dbgi      debug info
 * @param block     the basic block
 * @param op        the original node
 * @param src_bits  number of lower bits that will remain
105
106
107
108
109
 */
static ir_node *gen_sign_extension(dbg_info *dbgi, ir_node *block, ir_node *op,
                                   int src_bits)
{
	int shift_width = 32 - src_bits;
110
111
	ir_node *lshift_node = new_bd_sparc_Sll_imm(dbgi, block, op, shift_width);
	ir_node *rshift_node = new_bd_sparc_Sra_imm(dbgi, block, lshift_node, shift_width);
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
	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
129
130
131
132
133
134
135
136
/**
 * 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
 */
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
151
152
153
154
155
156
157
158
/**
 * Creates a possible DAG for a constant.
 */
static ir_node *create_const_graph_value(dbg_info *dbgi, ir_node *block,
				long value)
{
	ir_node *result;

Michael Beck's avatar
Michael Beck committed
159
	/* we need to load hi & lo separately */
160
	if (value < -4096 || value > 4095) {
161
162
163
164
165
166
		ir_node *hi = new_bd_sparc_HiImm(dbgi, block, (int) value);
		result = new_bd_sparc_LoImm(dbgi, block, hi, value);
		be_dep_on_frame(hi);
	} else {
		result = new_bd_sparc_Mov_imm(dbgi, block, (int) value);
		be_dep_on_frame(result);
Hannes Rapp's avatar
Hannes Rapp committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
	}

	return result;
}

/**
 * Create a DAG constructing a given Const.
 *
 * @param irn  a Firm const
 */
static ir_node *create_const_graph(ir_node *irn, ir_node *block)
{
	tarval  *tv = get_Const_tarval(irn);
	ir_mode *mode = get_tarval_mode(tv);
181
	dbg_info *dbgi = get_irn_dbg_info(irn);
Hannes Rapp's avatar
Hannes Rapp committed
182
183
	long value;

184

Hannes Rapp's avatar
Hannes Rapp committed
185
186
	if (mode_is_reference(mode)) {
		/* SPARC V8 is 32bit, so we can safely convert a reference tarval into Iu */
187
188
		assert(get_mode_size_bits(mode) == get_mode_size_bits(mode_gp));
		tv = tarval_convert_to(tv, mode_gp);
Hannes Rapp's avatar
Hannes Rapp committed
189
	}
190

Hannes Rapp's avatar
Hannes Rapp committed
191
	value = get_tarval_long(tv);
192
	return create_const_graph_value(dbgi, block, value);
Hannes Rapp's avatar
Hannes Rapp committed
193
194
195
196
}

typedef enum {
	MATCH_NONE         = 0,
Michael Beck's avatar
Michael Beck committed
197
	MATCH_COMMUTATIVE  = 1 << 0, /**< commutative operation. */
Hannes Rapp's avatar
Hannes Rapp committed
198
199
200
201
	MATCH_SIZE_NEUTRAL = 1 << 1,
} 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
202
typedef ir_node* (*new_binop_fp_func) (dbg_info *dbgi, ir_node *block, ir_node *op1, ir_node *op2, ir_mode *mode);
Hannes Rapp's avatar
Hannes Rapp committed
203
204
205
typedef ir_node* (*new_binop_imm_func) (dbg_info *dbgi, ir_node *block, ir_node *op1, int simm13);

/**
206
 * checks if a node's value can be encoded as a immediate
Hannes Rapp's avatar
Hannes Rapp committed
207
208
209
210
211
212
 *
 */
static bool is_imm_encodeable(const ir_node *node)
{
	long val;

Hannes Rapp's avatar
Hannes Rapp committed
213
214
	//assert(mode_is_float_vector(get_irn_mode(node)));

Hannes Rapp's avatar
Hannes Rapp committed
215
216
217
218
219
	if (!is_Const(node))
		return false;

	val = get_tarval_long(get_Const_tarval(node));

Michael Beck's avatar
Michael Beck committed
220
	return -4096 <= val && val <= 4095;
Hannes Rapp's avatar
Hannes Rapp committed
221
222
223
224
225
}

/**
 * helper function for binop operations
 *
Michael Beck's avatar
Michael Beck committed
226
227
 * @param new_reg  register generation function ptr
 * @param new_imm  immediate generation function ptr
Hannes Rapp's avatar
Hannes Rapp committed
228
229
230
231
232
233
234
235
236
237
238
239
240
 */
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  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *op1     = get_binop_left(node);
	ir_node  *new_op1;
	ir_node  *op2     = get_binop_right(node);
	ir_node  *new_op2;
	dbg_info *dbgi    = get_irn_dbg_info(node);

	if (is_imm_encodeable(op2)) {
		ir_node *new_op1 = be_transform_node(op1);
Hannes Rapp's avatar
Hannes Rapp committed
241
		return new_imm(dbgi, block, new_op1, get_tarval_long(get_Const_tarval(op2)));
Hannes Rapp's avatar
Hannes Rapp committed
242
243
244
245
246
247
248
249
250
251
252
253
254
	}

	new_op2 = be_transform_node(op2);

	if ((flags & MATCH_COMMUTATIVE) && is_imm_encodeable(op1)) {
		return new_imm(dbgi, block, new_op2, get_tarval_long(get_Const_tarval(op1)) );
	}

	new_op1 = be_transform_node(op1);

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

Hannes Rapp's avatar
Hannes Rapp committed
255
256
257
/**
 * helper function for FP binop operations
 */
258
259
static ir_node *gen_helper_binfpop(ir_node *node, ir_mode *mode,
                                   new_binop_fp_func new_func)
Hannes Rapp's avatar
Hannes Rapp committed
260
261
262
{
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *op1     = get_binop_left(node);
263
	ir_node  *new_op1 = be_transform_node(op1);
Hannes Rapp's avatar
Hannes Rapp committed
264
	ir_node  *op2     = get_binop_right(node);
265
	ir_node  *new_op2 = be_transform_node(op2);
Hannes Rapp's avatar
Hannes Rapp committed
266
267
	dbg_info *dbgi    = get_irn_dbg_info(node);

268
	return new_func(dbgi, block, new_op1, new_op2, mode);
Hannes Rapp's avatar
Hannes Rapp committed
269
270
}

Hannes Rapp's avatar
Hannes Rapp committed
271
272
273
274
275
276
277
278
/**
 * Creates an sparc Add.
 *
 * @param node   FIRM node
 * @return the created sparc Add node
 */
static ir_node *gen_Add(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
279
	ir_mode *mode = get_irn_mode(node);
Hannes Rapp's avatar
Hannes Rapp committed
280

281
282
283
	if (mode_is_float(mode)) {
		return gen_helper_binfpop(node, mode, new_bd_sparc_fadd);
	}
Hannes Rapp's avatar
Hannes Rapp committed
284
285
286
287
288
289
290
291
292
293
294
295

	return gen_helper_binop(node, MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL, new_bd_sparc_Add_reg, new_bd_sparc_Add_imm);
}

/**
 * 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
296
	ir_mode *mode = get_irn_mode(node);
297
298
299
300

	if (mode_is_float(mode)) {
		return gen_helper_binfpop(node, mode, new_bd_sparc_fsub);
	}
Hannes Rapp's avatar
Hannes Rapp committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321

	return gen_helper_binop(node, MATCH_SIZE_NEUTRAL, new_bd_sparc_Sub_reg, new_bd_sparc_Sub_imm);
}

/**
 * Transforms a Load.
 *
 * @param node    the ir Load node
 * @return the created sparc Load node
 */
static ir_node *gen_Load(ir_node *node)
{
	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  *new_ptr  = be_transform_node(ptr);
	ir_node  *mem      = get_Load_mem(node);
	ir_node  *new_mem  = be_transform_node(mem);
	dbg_info *dbgi      = get_irn_dbg_info(node);
	ir_node  *new_load = NULL;

322
323
324
325
326
	if (mode_is_float(mode)) {
		new_load = new_bd_sparc_Ldf(dbgi, block, new_ptr, new_mem, mode, NULL, 0, 0, false);
	} else {
		new_load = new_bd_sparc_Ld(dbgi, block, new_ptr, new_mem, mode, NULL, 0, 0, false);
	}
Hannes Rapp's avatar
Hannes Rapp committed
327
328
	set_irn_pinned(new_load, get_irn_pinned(node));

Hannes Rapp's avatar
Hannes Rapp committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
	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  *new_ptr  = be_transform_node(ptr);
	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);
	ir_node *new_store = NULL;

351
352
353
354
355
356
	if (mode_is_float(mode)) {
		new_store = new_bd_sparc_Stf(dbgi, block, new_ptr, new_val, new_mem, mode, NULL, 0, 0, false);
	} else {
		new_store = new_bd_sparc_St(dbgi, block, new_ptr, new_val, new_mem, mode, NULL, 0, 0, false);
	}
	set_irn_pinned(new_store, get_irn_pinned(node));
Hannes Rapp's avatar
Hannes Rapp committed
357
358
359
360

	return new_store;
}

361
362
/**
 * Creates an sparc Mul.
363
 * returns the lower 32bits of the 64bit multiply result
364
365
366
 *
 * @return the created sparc Mul node
 */
367
368
369
static ir_node *gen_Mul(ir_node *node)
{
	ir_mode *mode = get_irn_mode(node);
Hannes Rapp's avatar
Hannes Rapp committed
370
	if (mode_is_float(mode)) {
371
		return gen_helper_binfpop(node, mode, new_bd_sparc_fmul);
Hannes Rapp's avatar
Hannes Rapp committed
372
	}
373
374

	assert(mode_is_data(mode));
375
376
	return gen_helper_binop(node, MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL,
	                        new_bd_sparc_Mul_reg, new_bd_sparc_Mul_imm);
377
378
379
380
381
382
383
384
}

/**
 * Creates an sparc Mulh.
 * Mulh returns the upper 32bits of a mul instruction
 *
 * @return the created sparc Mulh node
 */
385
386
static ir_node *gen_Mulh(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
387
	ir_mode *mode = get_irn_mode(node);
388
389
390
391
392
393
394
395
	ir_node *mul;
	ir_node *proj_res_hi;

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


	assert(mode_is_data(mode));
Hannes Rapp's avatar
Hannes Rapp committed
396
397
	mul = gen_helper_binop(node, MATCH_COMMUTATIVE | MATCH_SIZE_NEUTRAL, new_bd_sparc_Mulh_reg, new_bd_sparc_Mulh_imm);
	//arch_irn_add_flags(mul, arch_irn_flags_modify_flags);
Michael Beck's avatar
Michael Beck committed
398
	proj_res_hi = new_r_Proj(mul, mode_gp, pn_sparc_Mulh_low);
399
	return proj_res_hi;
400
}
Hannes Rapp's avatar
Hannes Rapp committed
401

402
403
404
405
406
/**
 * Creates an sparc Div.
 *
 * @return the created sparc Div node
 */
407
408
409
410
411
static ir_node *gen_Div(ir_node *node)
{
	ir_mode *mode = get_Div_resmode(node);
	assert(!mode_is_float(mode));
	return gen_helper_binop(node, MATCH_SIZE_NEUTRAL, new_bd_sparc_Div_reg, new_bd_sparc_Div_imm);
412
413
}

414
415
416
417
418
419
static ir_node *gen_Quot(ir_node *node)
{
	ir_mode *mode = get_Quot_resmode(node);
	assert(mode_is_float(mode));
	return gen_helper_binfpop(node, mode, new_bd_sparc_fdiv);
}
420

421
422
423
424
425
426
427
428
429
/**
 * transform abs node:
 * mov a, b
 * sra b, 31, b
 * xor a, b
 * sub a, b
 *
 * @return
 */
430
static ir_node *gen_Abs(ir_node *node) {
431
432
433
434
435
436
437
438
439
440
441
442
443
	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);
	ir_node   *op     = get_Abs_op(node);

	ir_node *mov, *sra, *xor, *sub, *new_op;

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

	new_op = be_transform_node(op);

	mov = new_bd_sparc_Mov_reg(dbgi, block, new_op);
444
	sra = new_bd_sparc_Sra_imm(dbgi, block, mov, 31);
445
446
447
448
449
450
451
452
453
	xor = new_bd_sparc_Xor_reg(dbgi, block, new_op, sra);
	sub = new_bd_sparc_Sub_reg(dbgi, block, sra, xor);

	return sub;
}

/**
 * Transforms a Not node.
 *
Michael Beck's avatar
Michael Beck committed
454
 * @return the created sparc Not node
455
456
457
458
459
460
461
462
463
464
465
 */
static ir_node *gen_Not(ir_node *node)
{
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *op      = get_Not_op(node);
	ir_node  *new_op  = be_transform_node(op);
	dbg_info *dbgi    = get_irn_dbg_info(node);

	return new_bd_sparc_Not(dbgi, block, new_op);
}

466
467
468
469
470
471
472
473
474
475
static ir_node *gen_And(ir_node *node)
{
	return gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_And_reg, new_bd_sparc_And_imm);
}

static ir_node *gen_Or(ir_node *node)
{
	return gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_Or_reg, new_bd_sparc_Or_imm);
}

476
static ir_node *gen_Eor(ir_node *node)
Hannes Rapp's avatar
Hannes Rapp committed
477
478
479
480
{
	return gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_Xor_reg, new_bd_sparc_Xor_imm);
}

481
482
static ir_node *gen_Shl(ir_node *node)
{
483
	return gen_helper_binop(node, MATCH_SIZE_NEUTRAL, new_bd_sparc_Sll_reg, new_bd_sparc_Sll_imm);
484
485
486
487
}

static ir_node *gen_Shr(ir_node *node)
{
488
	return gen_helper_binop(node, MATCH_SIZE_NEUTRAL, new_bd_sparc_Slr_reg, new_bd_sparc_Slr_imm);
489
}
Hannes Rapp's avatar
Hannes Rapp committed
490

491
static ir_node *gen_Shrs(ir_node *node)
492
{
493
	return gen_helper_binop(node, MATCH_SIZE_NEUTRAL, new_bd_sparc_Sra_reg, new_bd_sparc_Sra_imm);
494
495
}

496
497
498
499
500
/**
 * Transforms a Minus node.
 */
static ir_node *gen_Minus(ir_node *node)
{
501
502
503
504
505
	ir_node  *block  = be_transform_node(get_nodes_block(node));
	ir_node  *op     = get_Minus_op(node);
	ir_node  *new_op = be_transform_node(op);
	dbg_info *dbgi   = get_irn_dbg_info(node);
	ir_mode  *mode   = get_irn_mode(node);
506
507

	if (mode_is_float(mode)) {
508
		return new_bd_sparc_fneg(dbgi, block, new_op, mode);
509
510
511
512
513
	}

	return new_bd_sparc_Minus(dbgi, block, new_op);
}

514
515
516
517
518
519
520
521
522
static ir_node *make_addr(dbg_info *dbgi, ir_entity *entity)
{
	ir_node *block = get_irg_start_block(current_ir_graph);
	ir_node *node  = new_bd_sparc_SymConst(dbgi, block, entity);
	be_dep_on_frame(node);
	return node;
}

/**
Michael Beck's avatar
Michael Beck committed
523
 * Create an entity for a given (floating point) tarval
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
 */
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;
}

Hannes Rapp's avatar
Hannes Rapp committed
550
551
552
/**
 * Transforms a Const node.
 *
Hannes Rapp's avatar
Hannes Rapp committed
553
 * @param node    the ir Const node
Hannes Rapp's avatar
Hannes Rapp committed
554
555
 * @return The transformed sparc node.
 */
556
557
static ir_node *gen_Const(ir_node *node)
{
558
559
	ir_node *block = be_transform_node(get_nodes_block(node));
	ir_mode *mode  = get_irn_mode(node);
Hannes Rapp's avatar
Hannes Rapp committed
560
561

	if (mode_is_float(mode)) {
562
563
564
565
566
567
568
569
570
571
		dbg_info  *dbgi   = get_irn_dbg_info(node);
		tarval    *tv     = get_Const_tarval(node);
		ir_entity *entity = create_float_const_entity(tv);
		ir_node   *addr   = make_addr(dbgi, entity);
		ir_node   *mem    = new_NoMem();
		ir_node   *new_op
			= new_bd_sparc_Ldf(dbgi, block, addr, mem, mode, NULL, 0, 0, false);

		ir_node   *proj   = new_Proj(new_op, mode, pn_sparc_Ldf_res);
		return proj;
Hannes Rapp's avatar
Hannes Rapp committed
572
	}
Hannes Rapp's avatar
Hannes Rapp committed
573

Hannes Rapp's avatar
Hannes Rapp committed
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
	return create_const_graph(node, block);
}

/**
 * AddSP
 * @param node the ir AddSP node
 * @return transformed sparc SAVE node
 */
static ir_node *gen_be_AddSP(ir_node *node)
{
	ir_node  *block  = be_transform_node(get_nodes_block(node));
	ir_node  *sz     = get_irn_n(node, be_pos_AddSP_size);
	ir_node  *new_sz = be_transform_node(sz);
	ir_node  *sp     = get_irn_n(node, be_pos_AddSP_old_sp);
	ir_node  *new_sp = be_transform_node(sp);
	dbg_info *dbgi   = get_irn_dbg_info(node);
	ir_node  *nomem  = new_NoMem();
	ir_node  *new_op;

	/* SPARC stack grows in reverse direction */
594
	new_op = new_bd_sparc_SubSP(dbgi, block, new_sp, new_sz, nomem);
Hannes Rapp's avatar
Hannes Rapp committed
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615

	return new_op;
}

/**
 * SubSP
 * @param node the ir SubSP node
 * @return transformed sparc SAVE node
 */
static ir_node *gen_be_SubSP(ir_node *node)
{
	ir_node  *block  = be_transform_node(get_nodes_block(node));
	ir_node  *sz     = get_irn_n(node, be_pos_SubSP_size);
	ir_node  *new_sz = be_transform_node(sz);
	ir_node  *sp     = get_irn_n(node, be_pos_SubSP_old_sp);
	ir_node  *new_sp = be_transform_node(sp);
	dbg_info *dbgi   = get_irn_dbg_info(node);
	ir_node  *nomem  = new_NoMem();
	ir_node  *new_op;

	/* SPARC stack grows in reverse direction */
616
	new_op = new_bd_sparc_AddSP(dbgi, block, new_sp, new_sz, nomem);
Hannes Rapp's avatar
Hannes Rapp committed
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
	return new_op;
}

/**
 * transform FrameAddr
 */
static ir_node *gen_be_FrameAddr(ir_node *node)
{
	ir_node   *block  = be_transform_node(get_nodes_block(node));
	ir_entity *ent    = be_get_frame_entity(node);
	ir_node   *fp     = be_get_FrameAddr_frame(node);
	ir_node   *new_fp = be_transform_node(fp);
	dbg_info  *dbgi   = get_irn_dbg_info(node);
	ir_node   *new_node;
	new_node = new_bd_sparc_FrameAddr(dbgi, block, new_fp, ent);
	return new_node;
}

/**
 * Transform a be_Copy.
 */
638
639
static ir_node *gen_be_Copy(ir_node *node)
{
Hannes Rapp's avatar
Hannes Rapp committed
640
641
642
643
	ir_node *result = be_duplicate_node(node);
	ir_mode *mode   = get_irn_mode(result);

	if (mode_needs_gp_reg(mode)) {
644
		set_irn_mode(node, mode_gp);
Hannes Rapp's avatar
Hannes Rapp committed
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
	}

	return result;
}

/**
 * Transform a Call
 */
static ir_node *gen_be_Call(ir_node *node)
{
	ir_node *res = be_duplicate_node(node);
	arch_irn_add_flags(res, arch_irn_flags_modify_flags);
	return res;
}

Hannes Rapp's avatar
Hannes Rapp committed
660
661
662
663
/**
 * Transforms a Switch.
 *
 */
Hannes Rapp's avatar
Hannes Rapp committed
664
665
static ir_node *gen_SwitchJmp(ir_node *node)
{
Hannes Rapp's avatar
Hannes Rapp committed
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
	ir_node  *block    = be_transform_node(get_nodes_block(node));
	ir_node  *selector = get_Cond_selector(node);
	dbg_info *dbgi     = get_irn_dbg_info(node);
	ir_node *new_op = be_transform_node(selector);
	ir_node *const_graph;
	ir_node *sub;

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

	foreach_out_edge(node, 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;
	n_projs = max - translation + 1;

	foreach_out_edge(node, 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);
	}

	const_graph = create_const_graph_value(dbgi, block, translation);
	sub = new_bd_sparc_Sub_reg(dbgi, block, new_op, const_graph);
	return new_bd_sparc_SwitchJmp(dbgi, block, sub, n_projs, get_Cond_default_proj(node) - translation);
Hannes Rapp's avatar
Hannes Rapp committed
705
706
}

707
708
709
710
711
712
713
714
715
716
717
718
719
720
static bool is_cmp_unsigned(ir_node *b_value)
{
	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);
	return !mode_is_signed(get_irn_mode(op));
}

Hannes Rapp's avatar
Hannes Rapp committed
721
722
723
724
725
726
727
728
729
/**
 * 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);
	ir_node  *block;
	ir_node  *flag_node;
730
731
	bool      is_unsigned;
	pn_Cmp    pnc;
Hannes Rapp's avatar
Hannes Rapp committed
732
733
734
735
	dbg_info *dbgi;

	// switch/case jumps
	if (mode != mode_b) {
Hannes Rapp's avatar
Hannes Rapp committed
736
		return gen_SwitchJmp(node);
Hannes Rapp's avatar
Hannes Rapp committed
737
738
739
740
	}

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

743
744
745
746
747
748
	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);
	is_unsigned = is_cmp_unsigned(selector);
	return new_bd_sparc_BXX(dbgi, block, flag_node, pnc, is_unsigned);
Hannes Rapp's avatar
Hannes Rapp committed
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
}

/**
 * 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);
	ir_node  *new_op1;
	ir_node  *new_op2;

	if (mode_is_float(cmp_mode)) {
		panic("FloatCmp not implemented");
	}

768
	/*
Hannes Rapp's avatar
Hannes Rapp committed
769
770
771
	if (get_mode_size_bits(cmp_mode) != 32) {
		panic("CmpMode != 32bit not supported yet");
	}
772
	*/
Hannes Rapp's avatar
Hannes Rapp committed
773
774
775
776

	assert(get_irn_mode(op2) == cmp_mode);

	/* compare with 0 can be done with Tst */
777
	/*
Hannes Rapp's avatar
Hannes Rapp committed
778
779
780
781
782
783
784
785
786
787
788
	if (is_Const(op2) && tarval_is_null(get_Const_tarval(op2))) {
		new_op1 = be_transform_node(op1);
		return new_bd_sparc_Tst(dbgi, block, new_op1, false,
		                          is_unsigned);
	}

	if (is_Const(op1) && tarval_is_null(get_Const_tarval(op1))) {
		new_op2 = be_transform_node(op2);
		return new_bd_sparc_Tst(dbgi, block, new_op2, true,
		                          is_unsigned);
	}
789
	*/
Hannes Rapp's avatar
Hannes Rapp committed
790
791
792

	/* integer compare */
	new_op1 = be_transform_node(op1);
793
	new_op1 = gen_extension(dbgi, block, new_op1, cmp_mode);
Hannes Rapp's avatar
Hannes Rapp committed
794
	new_op2 = be_transform_node(op2);
795
	new_op2 = gen_extension(dbgi, block, new_op2, cmp_mode);
796
	return new_bd_sparc_Cmp_reg(dbgi, block, new_op1, new_op2);
Hannes Rapp's avatar
Hannes Rapp committed
797
798
799
800
801
802
803
804
805
806
}

/**
 * Transforms a SymConst node.
 */
static ir_node *gen_SymConst(ir_node *node)
{
	ir_entity *entity = get_SymConst_entity(node);
	dbg_info  *dbgi   = get_irn_dbg_info(node);

807
	return make_addr(dbgi, entity);
Hannes Rapp's avatar
Hannes Rapp committed
808
809
}

Hannes Rapp's avatar
Hannes Rapp committed
810
811
812
813
/**
 * Transforms a Conv node.
 *
 */
814
815
static ir_node *gen_Conv(ir_node *node)
{
Hannes Rapp's avatar
Hannes Rapp committed
816
817
818
819
820
821
822
	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
823
824
825
	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
826
827
828
829
	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
830
831
832
833
		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
834
				/* float -> float conv */
835
				return new_bd_sparc_fftof(dbg, block, new_op, src_mode, dst_mode);
Hannes Rapp's avatar
Hannes Rapp committed
836
			} else {
Michael Beck's avatar
Michael Beck committed
837
				/* float -> int conv */
838
839
840
				if (!mode_is_signed(dst_mode))
					panic("float to unsigned not implemented yet");
				return new_bd_sparc_fftoi(dbg, block, new_op, src_mode);
Hannes Rapp's avatar
Hannes Rapp committed
841
842
			}
		} else {
Michael Beck's avatar
Michael Beck committed
843
			/* int -> float conv */
844
845
846
			if (!mode_is_signed(src_mode))
				panic("unsigned to float not implemented yet");
			return new_bd_sparc_fitof(dbg, block, new_op, dst_mode);
Hannes Rapp's avatar
Hannes Rapp committed
847
		}
Hannes Rapp's avatar
Hannes Rapp committed
848
849
850
851
852
	} else { /* complete in gp registers */
		int min_bits;
		ir_mode *min_mode;

		if (src_bits == dst_bits) {
Michael Beck's avatar
Michael Beck committed
853
			/* kill unnecessary conv */
Hannes Rapp's avatar
Hannes Rapp committed
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
			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);
		}
	}
}

877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
static ir_node *gen_Unknown(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);

	/* 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)) {
		return create_const_graph_value(dbgi, new_block, 0);
	}

	panic("Unexpected Unknown mode");
}

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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
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
1103
1104
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
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
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
/**
 * 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);
	/* 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,
                                     ir_node *node)
{
	ir_graph *irg   = current_ir_graph;
	ir_node  *stack = get_irg_frame(irg);
	ir_node  *nomem = new_NoMem();
	ir_node  *st    = new_bd_sparc_St(dbgi, block, stack, node, nomem, mode_gp,
	                                  NULL, 0, 0, true);
	ir_node  *ldf;
	set_irn_pinned(st, op_pin_state_floats);

	ldf = new_bd_sparc_Ldf(dbgi, block, stack, st, mode_fp, NULL, 0, 0, true);
	set_irn_pinned(ldf, op_pin_state_floats);

	return new_Proj(ldf, mode_fp, pn_sparc_Ldf_res);
}

static ir_node *bitcast_float_to_int(dbg_info *dbgi, ir_node *block,
                                     ir_node *node)
{
	ir_graph *irg   = current_ir_graph;
	ir_node  *stack = get_irg_frame(irg);
	ir_node  *nomem = new_NoMem();
	ir_node  *stf   = new_bd_sparc_Stf(dbgi, block, stack, node, nomem, mode_fp,
	                                   NULL, 0, 0, true);
	ir_node  *ld;
	set_irn_pinned(stf, op_pin_state_floats);

	ld = new_bd_sparc_Ld(dbgi, block, stack, stf, mode_gp, NULL, 0, 0, true);
	set_irn_pinned(ld, op_pin_state_floats);

	return new_Proj(ld, mode_fp, pn_sparc_Ld_res);
}

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);
		ir_node                  *new_value1 = NULL;
		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);
		ir_node                  *str;

		if (mode_is_float(mode) && param->reg0 != NULL) {
			unsigned size_bits = get_mode_size_bits(mode);
			assert(size_bits == 32);
			new_value = bitcast_float_to_int(dbgi, new_block, new_value);
		}

		/* put value into registers */
		if (param->reg0 != NULL) {
			in[in_arity]     = new_value;
			in_req[in_arity] = param->reg0->single_req;
			++in_arity;
			if (new_value1 == NULL)
				continue;
		}
		if (param->reg1 != NULL) {
			assert(new_value1 != NULL);
			in[in_arity]     = new_value1;
			in_req[in_arity] = param->reg1->single_req;
			++in_arity;
			continue;
		}

		/* we need a store if we're here */
		if (new_value1 != NULL) {
			new_value = new_value1;
			mode      = mode_gp;
		}

		/* create a parameter frame if necessary */
		if (mode_is_float(mode)) {
			str = new_bd_sparc_Stf(dbgi, new_block, incsp, new_value, new_mem,
			                       mode, NULL, 0, param->offset, true);
		} else {
			str = new_bd_sparc_St(dbgi, new_block, incsp, new_value, new_mem,
								  mode, NULL, 0, param->offset, true);
		}
		sync_ins[sync_arity++] = str;
	}
	assert(in_arity <= max_inputs);

	/* construct memory input */
	if (sync_arity == 0) {
		in[mem_pos] = new_mem;
	} else if (sync_arity == 1) {
		in[mem_pos] = sync_ins[0];
	} else {
		in[mem_pos] = new_rd_Sync(NULL, new_block, sync_arity, sync_ins);
	}

	if (is_SymConst(callee)) {
		entity = get_SymConst_entity(callee);
	} else {
		in[in_arity]     = be_transform_node(callee);
		in_req[in_arity] = sparc_reg_classes[CLASS_sparc_gp].class_req;
		++in_arity;
	}

	/* outputs:
	 *  - memory
	 *  - caller saves
	 */
	out_arity = 1 + n_caller_saves;

	/* create call node */
	if (entity != NULL) {
		res = new_bd_sparc_Call_imm(dbgi, new_block, in_arity, in, out_arity,
		                            entity, 0);
	} else {
		res = new_bd_sparc_Call_reg(dbgi, new_block, in_arity, in, out_arity);
	}
	set_sparc_in_req_all(res, in_req);

	/* create output register reqs */
	o = 0;
	arch_set_out_register_req(res, o++, arch_no_register_req);
	for (i = 0; i < n_caller_saves; ++i) {
		const arch_register_t *reg = caller_saves[i];
		arch_set_out_register_req(res, o++, reg->single_req);
	}
	assert(o == out_arity);

	/* copy pinned attribute */
	set_irn_pinned(res, get_irn_pinned(node));

	/* IncSP to destroy the call stackframe */
	incsp = be_new_IncSP(sp_reg, new_block, incsp, -cconv->param_stack_size, 0);
	/* if we are the last IncSP producer in a block then we have to keep
	 * the stack value.
	 * Note: This here keeps all producers which is more than necessary */
	add_irn_dep(incsp, res);
	keep_alive(incsp);

	pmap_insert(node_to_stack, node, incsp);

	sparc_free_calling_convention(cconv);
	return res;
}

static ir_node *gen_Sel(ir_node *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   *ptr       = get_Sel_ptr(node);
	ir_node   *new_ptr   = be_transform_node(ptr);
	ir_entity *entity    = get_Sel_entity(node);

	/* must be the frame pointer all other sels must have been lowered
	 * already */
	assert(is_Proj(ptr) && is_Start(get_Proj_pred(ptr)));
	/* we should not have value types from parameters anymore - they should be
	   lowered */
	assert(get_entity_owner(entity) !=
			get_method_value_param_type(get_entity_type(get_irg_entity(get_irn_irg(node)))));

	return new_bd_sparc_FrameAddr(dbgi, new_block, new_ptr, entity);
}

Hannes Rapp's avatar
Hannes Rapp committed
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
/**
 * Transform some Phi nodes
 */
static ir_node *gen_Phi(ir_node *node)
{
	const arch_register_req_t *req;
	ir_node  *block = be_transform_node(get_nodes_block(node));
	ir_graph *irg   = current_ir_graph;
	dbg_info *dbgi  = get_irn_dbg_info(node);
	ir_mode  *mode  = get_irn_mode(node);
	ir_node  *phi;

	if (mode_needs_gp_reg(mode)) {
		/* we shouldn't have any 64bit stuff around anymore */
		assert(get_mode_size_bits(mode) <= 32);
		/* all integer operations are on 32bit registers now */
1313
		mode = mode_gp;
Hannes Rapp's avatar
Hannes Rapp committed
1314
1315
1316
1317
1318
1319
1320
		req  = sparc_reg_classes[CLASS_sparc_gp].class_req;
	} else {
		req = arch_no_register_req;
	}

	/* phi nodes allow loops, so we use the old arguments for now
	 * and fix this later */
Hannes Rapp's avatar
Hannes Rapp committed
1321
	phi = new_ir_node(dbgi, irg, block, op_Phi, mode, get_irn_arity(node), get_irn_in(node) + 1);
1322
	copy_node_attr(irg, node, phi);
Hannes Rapp's avatar
Hannes Rapp committed
1323
1324
1325
1326
1327
1328
	be_duplicate_deps(node, phi);
	arch_set_out_register_req(phi, 0, req);
	be_enqueue_preds(node);
	return phi;
}

Hannes Rapp's avatar
Hannes Rapp committed
1329
1330
1331
/**
 * Transform a Proj from a Load.
 */
1332
1333
static ir_node *gen_Proj_Load(ir_node *node)
{
Hannes Rapp's avatar
Hannes Rapp committed
1334
1335
1336
	ir_node  *load     = get_Proj_pred(node);
	ir_node  *new_load = be_transform_node(load);
	dbg_info *dbgi     = get_irn_dbg_info(node);
1337
	long      pn       = get_Proj_proj(node);
Hannes Rapp's avatar
Hannes Rapp committed
1338
1339
1340

	/* renumber the proj */
	switch (get_sparc_irn_opcode(new_load)) {
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
	case iro_sparc_Ld:
		/* handle all gp loads equal: they have the same proj numbers. */
		if (pn == pn_Load_res) {
			return new_rd_Proj(dbgi, new_load, mode_gp, pn_sparc_Ld_res);
		} else if (pn == pn_Load_M) {
			return new_rd_Proj(dbgi, new_load, mode_M, pn_sparc_Ld_M);
		}
		break;
	case iro_sparc_Ldf:
		if (pn == pn_Load_res) {
			return new_rd_Proj(dbgi, new_load, mode_fp, pn_sparc_Ldf_res);
		} else if (pn == pn_Load_M) {
			return new_rd_Proj(dbgi, new_load, mode_M, pn_sparc_Ld_M);
		}
		break;
	default:
		break;
Hannes Rapp's avatar
Hannes Rapp committed
1358
	}
1359
1360
	panic("Unsupported Proj from Load");
}
Hannes Rapp's avatar
Hannes Rapp committed
1361

1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
static ir_node *gen_Proj_Store(ir_node *node)
{
	ir_node  *store     = get_Proj_pred(node);
	ir_node  *new_store = be_transform_node(store);
	long      pn        = get_Proj_proj(node);

	/* renumber the proj */
	switch (get_sparc_irn_opcode(new_store)) {
	case iro_sparc_St:
		if (pn == pn_Store_M) {
			return new_store;
		}
		break;
	case iro_sparc_Stf:
		if (pn == pn_Store_M) {
			return new_store;
		}
		break;
	default:
		break;
	}
	panic("Unsupported Proj from Store");
Hannes Rapp's avatar
Hannes Rapp committed
1384
1385
1386
1387
1388
}

/**
 * Transform the Projs from a Cmp.
 */
1389
1390
static ir_node *gen_Proj_Cmp(ir_node *node)
{
Hannes Rapp's avatar
Hannes Rapp committed
1391
1392
1393
1394
	(void) node;
	panic("not implemented");
}

Hannes Rapp's avatar
Hannes Rapp committed
1395
1396
1397
/**
 * transform Projs from a Div
 */
1398
1399
1400
1401
static ir_node *gen_Proj_Div(ir_node *node)
{
	ir_node  *pred     = get_Proj_pred(node);
	ir_node  *new_pred = be_transform_node(pred);
1402
	long      pn       = get_Proj_proj(node);
1403

1404
1405
	assert(is_sparc_Div(new_pred));
	switch (pn) {
1406
	case pn_Div_res:
1407
1408
1409
		return new_r_Proj(new_pred, mode_gp, pn_sparc_Div_res);
	case pn_Div_M:
		return new_r_Proj(new_pred, mode_gp, pn_sparc_Div_M);
1410
1411
1412
1413
1414
1415
	default:
		break;
	}
	panic("Unsupported Proj from Div");
}

1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
static ir_node *gen_Proj_Quot(ir_node *node)
{
	ir_node  *pred     = get_Proj_pred(node);
	ir_node  *new_pred = be_transform_node(pred);
	long      pn       = get_Proj_proj(node);

	assert(is_sparc_fdiv(new_pred));
	switch (pn) {
	case pn_Quot_res:
		return new_r_Proj(new_pred, mode_gp, pn_sparc_fdiv_res);
	case pn_Quot_M:
		return new_r_Proj(new_pred, mode_gp, pn_sparc_fdiv_M);
	default:
		break;
	}
	panic("Unsupported Proj from Quot");
}

1434
1435
1436
1437
1438
1439
1440
1441
1442
static ir_node *gen_Proj_Start(ir_node *node)
{
	ir_node *block     = get_nodes_block(node);
	ir_node *new_block = be_transform_node(block);
	ir_node *barrier   = be_transform_node(get_Proj_pred(node));
	long     pn        = get_Proj_proj(node);

	switch ((pn_Start) pn) {
	case pn_Start_X_initial_exec:
Michael Beck's avatar
Michael Beck committed
1443
		/* exchange ProjX with a jump */
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
		return new_bd_sparc_Ba(NULL, new_block);
	case pn_Start_M:
		return new_r_Proj(barrier, mode_M, 0);
	case pn_Start_T_args:
		return barrier;
	case pn_Start_P_frame_base:
		return be_prolog_get_reg_value(abihelper, fp_reg);
	case pn_Start_P_tls:
		return new_Bad();
	case pn_Start_max:
		break;
	}
	panic("Unexpected start proj: %ld\n", pn);
}

static ir_node *gen_Proj_Proj_Start(ir_node *node)
{
	long       pn          = get_Proj_proj(node);
	ir_node   *block       = get_nodes_block(node);
	ir_node   *new_block   = be_transform_node(block);
	ir_entity *entity      = get_irg_entity(current_ir_graph);
	ir_type   *method_type = get_entity_type(entity);
	ir_type   *param_type  = get_method_param_type(method_type, pn);
	const reg_or_stackslot_t *param;

	/* Proj->Proj->Start must be a method argument */
	assert(get_Proj_proj(get_Proj_pred(node)) == pn_Start_T_args);

	param = &cconv->parameters[pn];

	if (param->reg0 != NULL) {
		/* argument transmitted in register */
		ir_mode               *mode  = get_type_mode(param_type);
		const arch_register_t *reg   = param->reg0;
		ir_node               *value = be_prolog_get_reg_value(abihelper, reg);

		if (mode_is_float(mode)) {
			/* convert integer value to float */
			value = bitcast_int_to_float(NULL, new_block, value);
		}
		return value;
	} else {
		/* argument transmitted on stack */
		ir_node  *fp   = be_prolog_get_reg_value(abihelper, fp_reg);
		ir_node  *mem  = be_prolog_get_memory(abihelper);
		ir_mode  *mode = get_type_mode(param->type);
		ir_node  *load;
		ir_node  *value;

		if (mode_is_float(mode)) {
			load  = new_bd_sparc_Ldf(NULL, new_block, fp, mem, mode,
			                         param->entity, 0, 0, true);
			value = new_r_Proj(load, mode_fp, pn_sparc_Ldf_res);
		} else {
			load  = new_bd_sparc_Ld(NULL, new_block, fp, mem, mode,
			                        param->entity, 0, 0, true);
			value = new_r_Proj(load, mode_gp, pn_sparc_Ld_res);
		}
		set_irn_pinned(load, op_pin_state_floats);

		return value;
	}
}

static ir_node *gen_Proj_Call(ir_node *node)
{
	long     pn        = get_Proj_proj(node);
	ir_node *call      = get_Proj_pred(node);
	ir_node *new_call  = be_transform_node(call);

	switch ((pn_Call) pn) {
	case pn_Call_M:
		return new_r_Proj(new_call, mode_M, 0);
	case pn_Call_X_regular:
	case pn_Call_X_except:
	case pn_Call_T_result:
	case pn_Call_P_value_res_base:
	case pn_Call_max:
		break;
	}
	panic("Unexpected Call proj %ld\n", pn);
}

/**
 * Finds number of output value of a mode_T node which is constrained to
 * a single specific register.
 */
static int find_out_for_reg(ir_node *node, const arch_register_t *reg)
{
	int n_outs = arch_irn_get_n_outs(node);
	int o;

	for (o = 0; o < n_outs; ++o) {
		const arch_register_req_t *req = arch_get_out_register_req(node, o);
		if (req == reg->single_req)
			return o;
	}
	return -1;
}

static ir_node *gen_Proj_Proj_Call(ir_node *node)
{
	long                  pn            = get_Proj_proj(node);
	ir_node              *call          = get_Proj_pred(get_Proj_pred(node));
	ir_node              *new_call      = be_transform_node(call);
	ir_type              *function_type = get_Call_type(call);
	calling_convention_t *cconv
		= sparc_decide_calling_convention(function_type, true);
	const reg_or_stackslot_t *res = &cconv->results[pn];
	const arch_register_t    *reg = res->reg0;
	ir_mode                  *mode;
	int                       regn;

	assert(res->reg0 != NULL && res->reg1 == NULL);
	regn = find_out_for_reg(new_call, reg);
	if (regn < 0) {
		panic("Internal error in calling convention for return %+F", node);
	}
	mode = res->reg0->reg_class->mode;

	sparc_free_calling_convention(cconv);

	return new_r_Proj(new_call, mode, regn);
}
1568

Hannes Rapp's avatar
Hannes Rapp committed
1569
1570
1571
/**
 * Transform a Proj node.
 */
1572
1573
static ir_node *gen_Proj(ir_node *node)
{
1574
	ir_node *pred = get_Proj_pred(node);
Hannes Rapp's avatar
Hannes Rapp committed
1575

1576
1577
	switch (get_irn_opcode(pred)) {
	case iro_Store:
1578
		return gen_Proj_Store(node);
1579
	case iro_Load:
Hannes Rapp's avatar
Hannes Rapp committed
1580
		return gen_Proj_Load(node);
1581
1582
1583
	case iro_Call:
		return gen_Proj_Call(node);
	case iro_Cmp:
Hannes Rapp's avatar
Hannes Rapp committed
1584
		return gen_Proj_Cmp(node);
1585
1586
1587
	case iro_Cond:
		return be_duplicate_node(node);
	case iro_Div:
1588
		return gen_Proj_Div(node);
1589
1590
	case iro_Quot:
		return gen_Proj_Quot(node);
1591
1592
1593
1594
1595
1596
1597
1598
	case iro_Start:
		return gen_Proj_Start(node);
	case iro_Proj: {
		ir_node *pred_pred = get_Proj_pred(pred);
		if (is_Call(pred_pred)) {
			return gen_Proj_Proj_Call(node);
		} else if (is_Start(pred_pred)) {
			return gen_Proj_Proj_Start(node);
Hannes Rapp's avatar
Hannes Rapp committed
1599
		}
1600
1601
1602
1603
		/* FALLTHROUGH */
	}
	default:
		panic("code selection didn't expect Proj after %+F\n", pred);
Hannes Rapp's avatar
Hannes Rapp committed
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
	}
}

/**
 * transform a Jmp
 */
static ir_node *gen_Jmp(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);

1616
	return new_bd_sparc_Ba(dbgi, new_block);
Hannes Rapp's avatar
Hannes Rapp committed
1617
1618
}

Hannes Rapp's avatar
Hannes Rapp committed
1619
1620
1621
1622
1623
/**
 * configure transformation callbacks
 */
void sparc_register_transformers(void)
{
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
	be_start_transform_setup();

	be_set_transform_function(op_Abs,          gen_Abs);
	be_set_transform_function(op_Add,          gen_Add);
	be_set_transform_function(op_And,          gen_And);
	be_set_transform_function(op_be_AddSP,     gen_be_AddSP);
	be_set_transform_function(op_be_Call,      gen_be_Call);
	be_set_transform_function(op_be_Copy,      gen_be_Copy);
	be_set_transform_function(op_be_FrameAddr, gen_be_FrameAddr);
	be_set_transform_function(op_be_SubSP,     gen_be_SubSP);
1634
	be_set_transform_function(op_Call,         gen_Call);
1635
1636
1637
1638
1639
	be_set_transform_function(op_Cmp,          gen_Cmp);
	be_set_transform_function(op_Cond,         gen_Cond);
	be_set_transform_function(op_Const,        gen_Const);
	be_set_transform_function(op_Conv,         gen_Conv);
	be_set_transform_function(op_Div,          gen_Div);
1640
	be_set_transform_function(op_Eor,          gen_Eor);
1641
1642
1643
1644
1645
1646
1647
1648
1649
	be_set_transform_function(op_Jmp,          gen_Jmp);
	be_set_transform_function(op_Load,         gen_Load);
	be_set_transform_function(op_Minus,        gen_Minus);
	be_set_transform_function(op_Mul,          gen_Mul);
	be_set_transform_function(op_Mulh,         gen_Mulh);
	be_set_transform_function(op_Not,          gen_Not);
	be_set_transform_function(op_Or,           gen_Or);
	be_set_transform_function(op_Phi,          gen_Phi);
	be_set_transform_function(op_Proj,         gen_Proj);
1650
	be_set_transform_function(op_Quot,         gen_Quot);
1651
1652
	be_set_transform_function(op_Return,       gen_Return);
	be_set_transform_function(op_Sel,          gen_Sel);
1653
1654
	be_set_transform_function(op_Shl,          gen_Shl);
	be_set_transform_function(op_Shr,          gen_Shr);
1655
	be_set_transform_function(op_Shrs,         gen_Shrs);
1656
	be_set_transform_function(op_Start,        gen_Start);
1657
1658
1659
1660
1661
1662
	be_set_transform_function(op_Store,        gen_Store);
	be_set_transform_function(op_Sub,          gen_Sub);
	be_set_transform_function(op_SymConst,     gen_SymConst);
	be_set_transform_function(op_Unknown,      gen_Unknown);

	be_set_transform_function(op_sparc_Save,   be_duplicate_node);
Hannes Rapp's avatar
Hannes Rapp committed
1663
1664
}

1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
/* hack to avoid unused fp proj at start barrier */
static void assure_fp_keep(void)
{
	unsigned         n_users = 0;
	const ir_edge_t *edge;
	ir_node         *fp_proj = be_prolog_get_reg_value(abihelper, fp_reg);

	foreach_out_edge(fp_proj, edge) {
		ir_node *succ = get_edge_src_irn(edge);
		if (is_End(succ) || is_Anchor(succ))
			continue;
		++n_users;
	}

	if (n_users == 0) {
		ir_node *block = get_nodes_block(fp_proj);
		ir_node *in[1] = { fp_proj };
		be_new_Keep(block, 1, in);
	}
}

Hannes Rapp's avatar
Hannes Rapp committed
1686
1687
1688
1689
1690
/**
 * Transform a Firm graph into a SPARC graph.
 */
void sparc_transform_graph(sparc_code_gen_t *cg)
{
1691
1692
1693
1694
	ir_graph  *irg    = cg->irg;
	ir_entity *entity = get_irg_entity(irg);
	ir_type   *frame_type;

Hannes Rapp's avatar
Hannes Rapp committed
1695
1696
	sparc_register_transformers();
	env_cg = cg;
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707

	node_to_stack = pmap_create();

	mode_gp = mode_Iu;
	mode_fp = mode_F;

	abihelper = be_abihelper_prepare(irg);
	be_collect_stacknodes(abihelper);
	cconv = sparc_decide_calling_convention(get_entity_type(entity), false);
	create_stacklayout(irg);

1708
	be_transform_graph(cg->irg, NULL);
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
	assure_fp_keep();

	be_abihelper_finish(abihelper);
	sparc_free_calling_convention(cconv);

	frame_type = get_irg_frame_type(irg);
	if (get_type_state(frame_type) == layout_undefined)
		default_layout_compound_type(frame_type);

	pmap_destroy(node_to_stack);
	node_to_stack = NULL;

	be_add_missing_keeps(irg);
Hannes Rapp's avatar
Hannes Rapp committed
1722
1723
1724
1725
1726
1727
}

void sparc_init_transform(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.be.sparc.transform");
}