sparc_transform.c 49.8 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
 */
#include "config.h"

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

#include <limits.h>

DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

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

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


Hannes Rapp's avatar
Hannes Rapp committed
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
89
90
 * @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) {
91
92
		ir_node *lshift = new_bd_sparc_Sll_imm(dbgi, block, op, 16);
		ir_node *rshift = new_bd_sparc_Slr_imm(dbgi, block, lshift, 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, shift_width);
	ir_node *rshift_node = new_bd_sparc_Sra_imm(dbgi, block, lshift_node, 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
154
155
156
157
158
159
/**
 * 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
160
	/* we need to load hi & lo separately */
161
	if (value < -4096 || value > 4095) {
162
163
164
165
166
167
		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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
	}

	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);
182
	dbg_info *dbgi = get_irn_dbg_info(irn);
Hannes Rapp's avatar
Hannes Rapp committed
183
184
	long value;

185

Hannes Rapp's avatar
Hannes Rapp committed
186
187
	if (mode_is_reference(mode)) {
		/* SPARC V8 is 32bit, so we can safely convert a reference tarval into Iu */
188
189
		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
190
	}
191

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

typedef enum {
	MATCH_NONE         = 0,
Michael Beck's avatar
Michael Beck committed
198
	MATCH_COMMUTATIVE  = 1 << 0, /**< commutative operation. */
Hannes Rapp's avatar
Hannes Rapp committed
199
200
201
} 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
213
214
215
216
217
 *
 */
static bool is_imm_encodeable(const ir_node *node)
{
	long val;

	if (!is_Const(node))
		return false;

	val = get_tarval_long(get_Const_tarval(node));

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

/**
 * helper function for binop operations
 *
Michael Beck's avatar
Michael Beck committed
224
225
 * @param new_reg  register generation function ptr
 * @param new_imm  immediate generation function ptr
Hannes Rapp's avatar
Hannes Rapp committed
226
227
228
229
230
231
232
233
234
235
236
237
238
 */
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
239
		return new_imm(dbgi, block, new_op1, get_tarval_long(get_Const_tarval(op2)));
Hannes Rapp's avatar
Hannes Rapp committed
240
241
242
243
244
245
246
247
248
249
250
251
252
	}

	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
253
254
255
/**
 * helper function for FP binop operations
 */
256
static ir_node *gen_helper_binfpop(ir_node *node, ir_mode *mode,
257
258
259
                                   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
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
	dbg_info *dbgi    = get_irn_dbg_info(node);
267
268
269
270
271
272
273
274
275
276
277
278
279
	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
280
281
}

Hannes Rapp's avatar
Hannes Rapp committed
282
283
284
285
286
287
288
289
/**
 * 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
290
	ir_mode *mode = get_irn_mode(node);
Hannes Rapp's avatar
Hannes Rapp committed
291

292
	if (mode_is_float(mode)) {
293
294
		return gen_helper_binfpop(node, mode, new_bd_sparc_fadd_s,
		                          new_bd_sparc_fadd_d, new_bd_sparc_fadd_q);
295
	}
Hannes Rapp's avatar
Hannes Rapp committed
296

297
	return gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_Add_reg, new_bd_sparc_Add_imm);
Hannes Rapp's avatar
Hannes Rapp committed
298
299
300
301
302
303
304
305
306
307
}

/**
 * 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
308
	ir_mode *mode = get_irn_mode(node);
309
310

	if (mode_is_float(mode)) {
311
312
		return gen_helper_binfpop(node, mode, new_bd_sparc_fsub_s,
		                          new_bd_sparc_fsub_d, new_bd_sparc_fsub_q);
313
	}
Hannes Rapp's avatar
Hannes Rapp committed
314

315
	return gen_helper_binop(node, MATCH_NONE, new_bd_sparc_Sub_reg, new_bd_sparc_Sub_imm);
Hannes Rapp's avatar
Hannes Rapp committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
}

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

335
336
337
338
339
	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
340
341
	set_irn_pinned(new_load, get_irn_pinned(node));

Hannes Rapp's avatar
Hannes Rapp committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
	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;

364
365
366
367
368
369
	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
370
371
372
373

	return new_store;
}

374
375
/**
 * Creates an sparc Mul.
376
 * returns the lower 32bits of the 64bit multiply result
377
378
379
 *
 * @return the created sparc Mul node
 */
380
381
382
static ir_node *gen_Mul(ir_node *node)
{
	ir_mode *mode = get_irn_mode(node);
Hannes Rapp's avatar
Hannes Rapp committed
383
	if (mode_is_float(mode)) {
384
385
		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
386
	}
387
388

	assert(mode_is_data(mode));
389
	return gen_helper_binop(node, MATCH_COMMUTATIVE,
390
	                        new_bd_sparc_Mul_reg, new_bd_sparc_Mul_imm);
391
392
393
394
395
396
397
398
}

/**
 * Creates an sparc Mulh.
 * Mulh returns the upper 32bits of a mul instruction
 *
 * @return the created sparc Mulh node
 */
399
400
static ir_node *gen_Mulh(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
401
	ir_mode *mode = get_irn_mode(node);
402
403
404
405
406
407
408
409
	ir_node *mul;
	ir_node *proj_res_hi;

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


	assert(mode_is_data(mode));
410
	mul = gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_Mulh_reg, new_bd_sparc_Mulh_imm);
Hannes Rapp's avatar
Hannes Rapp committed
411
	//arch_irn_add_flags(mul, arch_irn_flags_modify_flags);
Michael Beck's avatar
Michael Beck committed
412
	proj_res_hi = new_r_Proj(mul, mode_gp, pn_sparc_Mulh_low);
413
	return proj_res_hi;
414
}
Hannes Rapp's avatar
Hannes Rapp committed
415

416
417
418
419
420
/**
 * Creates an sparc Div.
 *
 * @return the created sparc Div node
 */
421
422
423
static ir_node *gen_Div(ir_node *node)
{
	ir_mode *mode = get_Div_resmode(node);
424
425
	ir_node *res;

426
	assert(!mode_is_float(mode));
427
428
429
430
431
432
433
434
	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;
435
436
}

437
438
439
440
static ir_node *gen_Quot(ir_node *node)
{
	ir_mode *mode = get_Quot_resmode(node);
	assert(mode_is_float(mode));
441
442
	return gen_helper_binfpop(node, mode, new_bd_sparc_fdiv_s,
	                          new_bd_sparc_fdiv_d, new_bd_sparc_fdiv_q);
443
}
444

445
446
447
static ir_node *gen_Abs(ir_node *node)
{
	ir_mode *const mode = get_irn_mode(node);
448

449
450
451
	if (mode_is_float(mode)) {
		return gen_helper_binfpop(node, mode, new_bd_sparc_fabs_s,
		                          new_bd_sparc_fdiv_d, new_bd_sparc_fdiv_q);
452
453
454
455
456
457
458
459
460
	} 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);
		ir_node  *const sra    = new_bd_sparc_Sra_imm(dbgi, block, new_op, 31);
		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;
461
	}
462
463
464
465
466
}

/**
 * Transforms a Not node.
 *
Michael Beck's avatar
Michael Beck committed
467
 * @return the created sparc Not node
468
469
470
471
472
473
474
475
476
477
478
 */
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);
}

479
480
481
482
483
484
485
486
487
488
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);
}

489
static ir_node *gen_Eor(ir_node *node)
Hannes Rapp's avatar
Hannes Rapp committed
490
491
492
493
{
	return gen_helper_binop(node, MATCH_COMMUTATIVE, new_bd_sparc_Xor_reg, new_bd_sparc_Xor_imm);
}

494
495
static ir_node *gen_Shl(ir_node *node)
{
496
	return gen_helper_binop(node, MATCH_NONE, new_bd_sparc_Sll_reg, new_bd_sparc_Sll_imm);
497
498
499
500
}

static ir_node *gen_Shr(ir_node *node)
{
501
	return gen_helper_binop(node, MATCH_NONE, new_bd_sparc_Slr_reg, new_bd_sparc_Slr_imm);
502
}
Hannes Rapp's avatar
Hannes Rapp committed
503

504
static ir_node *gen_Shrs(ir_node *node)
505
{
506
	return gen_helper_binop(node, MATCH_NONE, new_bd_sparc_Sra_reg, new_bd_sparc_Sra_imm);
507
508
}

509
510
511
512
513
/**
 * Transforms a Minus node.
 */
static ir_node *gen_Minus(ir_node *node)
{
514
515
516
517
518
	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);
519
520

	if (mode_is_float(mode)) {
521
		return new_bd_sparc_fneg(dbgi, block, new_op, mode);
522
523
524
525
526
	}

	return new_bd_sparc_Minus(dbgi, block, new_op);
}

527
528
529
530
531
532
533
534
535
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
536
 * Create an entity for a given (floating point) tarval
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
 */
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
563
564
565
/**
 * Transforms a Const node.
 *
Hannes Rapp's avatar
Hannes Rapp committed
566
 * @param node    the ir Const node
Hannes Rapp's avatar
Hannes Rapp committed
567
568
 * @return The transformed sparc node.
 */
569
570
static ir_node *gen_Const(ir_node *node)
{
571
572
	ir_node *block = be_transform_node(get_nodes_block(node));
	ir_mode *mode  = get_irn_mode(node);
Hannes Rapp's avatar
Hannes Rapp committed
573
574

	if (mode_is_float(mode)) {
575
576
577
578
579
580
581
582
583
584
		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
585
	}
Hannes Rapp's avatar
Hannes Rapp committed
586

Hannes Rapp's avatar
Hannes Rapp committed
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
	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 */
607
	new_op = new_bd_sparc_SubSP(dbgi, block, new_sp, new_sz, nomem);
Hannes Rapp's avatar
Hannes Rapp committed
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628

	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 */
629
	new_op = new_bd_sparc_AddSP(dbgi, block, new_sp, new_sz, nomem);
Hannes Rapp's avatar
Hannes Rapp committed
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
	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.
 */
651
652
static ir_node *gen_be_Copy(ir_node *node)
{
Hannes Rapp's avatar
Hannes Rapp committed
653
654
655
656
	ir_node *result = be_duplicate_node(node);
	ir_mode *mode   = get_irn_mode(result);

	if (mode_needs_gp_reg(mode)) {
657
		set_irn_mode(node, mode_gp);
Hannes Rapp's avatar
Hannes Rapp committed
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
	}

	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
673
674
675
676
/**
 * Transforms a Switch.
 *
 */
Hannes Rapp's avatar
Hannes Rapp committed
677
678
static ir_node *gen_SwitchJmp(ir_node *node)
{
Hannes Rapp's avatar
Hannes Rapp committed
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
705
706
707
708
709
710
711
712
713
714
715
716
717
	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
718
719
}

720
static ir_mode *get_cmp_mode(ir_node *b_value)
721
722
723
724
725
726
727
728
729
730
{
	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);
731
	return get_irn_mode(op);
732
733
}

Hannes Rapp's avatar
Hannes Rapp committed
734
735
736
737
738
739
740
/**
 * 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);
741
	ir_mode  *cmp_mode;
Hannes Rapp's avatar
Hannes Rapp committed
742
743
	ir_node  *block;
	ir_node  *flag_node;
744
745
	bool      is_unsigned;
	pn_Cmp    pnc;
Hannes Rapp's avatar
Hannes Rapp committed
746
747
748
749
	dbg_info *dbgi;

	// switch/case jumps
	if (mode != mode_b) {
Hannes Rapp's avatar
Hannes Rapp committed
750
		return gen_SwitchJmp(node);
Hannes Rapp's avatar
Hannes Rapp committed
751
752
753
754
	}

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

757
758
	cmp_mode = get_cmp_mode(selector);

759
760
761
762
	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);
763
764
765
766
767
768
769
	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
770
771
772
773
774
775
776
777
778
779
780
781
}

/**
 * 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);
782
783
	ir_node  *new_op1  = be_transform_node(op1);
	ir_node  *new_op2  = be_transform_node(op2);
Hannes Rapp's avatar
Hannes Rapp committed
784
785
	assert(get_irn_mode(op2) == cmp_mode);

786
787
	if (mode_is_float(cmp_mode)) {
		return new_bd_sparc_fcmp(dbgi, block, new_op1, new_op2, cmp_mode);
Hannes Rapp's avatar
Hannes Rapp committed
788
789
790
	}

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

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

804
	return make_addr(dbgi, entity);
Hannes Rapp's avatar
Hannes Rapp committed
805
806
}

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

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

874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
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");
}

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
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
/**
 * 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);
For faster browsing, not all history is shown. View entire blame