sparc_emitter.c 43.2 KB
Newer Older
Hannes Rapp's avatar
Hannes Rapp committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Hannes Rapp's avatar
Hannes Rapp committed
4
5
6
7
8
 */

/**
 * @file
 * @brief   emit assembler for a backend graph
Matthias Braun's avatar
Matthias Braun committed
9
 * @author  Hannes Rapp, Matthias Braun
Hannes Rapp's avatar
Hannes Rapp committed
10
11
12
13
14
 */
#include "config.h"

#include <limits.h>

15
#include "bitfiddle.h"
Hannes Rapp's avatar
Hannes Rapp committed
16
17
18
19
20
21
22
23
24
25
26
27
#include "xmalloc.h"
#include "tv.h"
#include "iredges.h"
#include "debug.h"
#include "irgwalk.h"
#include "irop_t.h"
#include "irargs_t.h"
#include "irprog.h"
#include "irargs_t.h"
#include "error.h"
#include "raw_bitset.h"
#include "dbginfo.h"
28
#include "heights.h"
29
30
#include "pmap.h"
#include "execfreq_t.h"
Hannes Rapp's avatar
Hannes Rapp committed
31

32
33
34
35
#include "besched.h"
#include "beblocksched.h"
#include "beirg.h"
#include "begnuas.h"
36
#include "bedwarf.h"
37
38
#include "benode.h"
#include "bestack.h"
39
#include "bepeephole.h"
Hannes Rapp's avatar
Hannes Rapp committed
40
41
42
43
44

#include "sparc_emitter.h"
#include "gen_sparc_emitter.h"
#include "sparc_nodes_attr.h"
#include "sparc_new_nodes.h"
Hannes Rapp's avatar
Hannes Rapp committed
45
46
#include "gen_sparc_regalloc_if.h"

Hannes Rapp's avatar
Hannes Rapp committed
47
48
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

49
50
51
static ir_heights_t *heights;
static unsigned     *delay_slot_fillers;
static pmap         *delay_slots;
52

53
54
static bool emitting_delay_slot;

55
56
57
58
59
/**
 * indent before instruction. (Adds additional indentation when emitting
 * delay slots)
 */
static void sparc_emit_indent(void)
60
61
62
63
64
{
	be_emit_char('\t');
	if (emitting_delay_slot)
		be_emit_char(' ');
}
65

66
static void sparc_emit_immediate(int32_t value, ir_entity *entity)
Hannes Rapp's avatar
Hannes Rapp committed
67
{
68
	if (entity == NULL) {
69
		assert(sparc_is_value_imm_encodeable(value));
70
71
		be_emit_irprintf("%d", value);
	} else {
72
73
74
75
76
		if (get_entity_owner(entity) == get_tls_type()) {
			be_emit_cstring("%tle_lox10(");
		} else {
			be_emit_cstring("%lo(");
		}
77
		be_gas_emit_entity(entity);
78
79
		if (value != 0) {
			be_emit_irprintf("%+d", value);
80
81
82
		}
		be_emit_char(')');
	}
83
84
}

85
static void sparc_emit_high_immediate(ir_node const *node)
86
{
87
88
89
90
91
	const sparc_attr_t *attr   = get_sparc_attr_const(node);
	ir_entity          *entity = attr->immediate_value_entity;

	if (entity == NULL) {
		uint32_t value = (uint32_t) attr->immediate_value;
92
		be_emit_irprintf("%%hi(0x%X)", value);
93
	} else {
94
95
96
97
98
		if (get_entity_owner(entity) == get_tls_type()) {
			be_emit_cstring("%tle_hix22(");
		} else {
			be_emit_cstring("%hi(");
		}
99
100
101
102
		be_gas_emit_entity(entity);
		if (attr->immediate_value != 0) {
			be_emit_irprintf("%+d", attr->immediate_value);
		}
103
		be_emit_char(')');
104
	}
Hannes Rapp's avatar
Hannes Rapp committed
105
106
}

107
static void sparc_emit_register(const arch_register_t *const reg)
Hannes Rapp's avatar
Hannes Rapp committed
108
109
{
	be_emit_char('%');
110
	be_emit_string(reg->name);
Hannes Rapp's avatar
Hannes Rapp committed
111
112
}

113
114
115
116
117
118
static void sparc_emit_source_register(ir_node const *node, int const pos)
{
	const arch_register_t *reg = arch_get_irn_register_in(node, pos);
	sparc_emit_register(reg);
}

119
static void sparc_emit_dest_register(ir_node const *const node, int const pos)
Hannes Rapp's avatar
Hannes Rapp committed
120
{
Matthias Braun's avatar
Matthias Braun committed
121
	const arch_register_t *reg = arch_get_irn_register_out(node, pos);
122
	sparc_emit_register(reg);
Hannes Rapp's avatar
Hannes Rapp committed
123
124
}

125
126
127
/**
 * emit SP offset
 */
128
static void sparc_emit_offset(const ir_node *node, int offset_node_pos)
Hannes Rapp's avatar
Hannes Rapp committed
129
{
130
	const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
131

132
133
134
135
136
137
138
	if (attr->is_reg_reg) {
		assert(!attr->is_frame_entity);
		assert(attr->base.immediate_value == 0);
		assert(attr->base.immediate_value_entity == NULL);
		be_emit_char('+');
		sparc_emit_source_register(node, offset_node_pos);
	} else if (attr->is_frame_entity) {
139
140
		int32_t offset = attr->base.immediate_value;
		if (offset != 0) {
141
			assert(sparc_is_value_imm_encodeable(offset));
142
143
			be_emit_irprintf("%+ld", offset);
		}
144
145
	} else if (attr->base.immediate_value != 0
			|| attr->base.immediate_value_entity != NULL) {
146
		be_emit_char('+');
147
148
		sparc_emit_immediate(attr->base.immediate_value,
		                     attr->base.immediate_value_entity);
149
	}
Hannes Rapp's avatar
Hannes Rapp committed
150
151
152
}

/**
153
 *  Emit load mode
Hannes Rapp's avatar
Hannes Rapp committed
154
 */
155
static void sparc_emit_load_mode(ir_node const *const node)
Hannes Rapp's avatar
Hannes Rapp committed
156
157
{
	const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
158
159
160
161
	ir_mode *mode      = attr->load_store_mode;
	int      bits      = get_mode_size_bits(mode);
	bool     is_signed = mode_is_signed(mode);

162
163
164
165
166
167
168
	switch (bits) {
	case   8: be_emit_string(is_signed ? "sb" : "ub"); break;
	case  16: be_emit_string(is_signed ? "sh" : "uh"); break;
	case  32: break;
	case  64: be_emit_char('d'); break;
	case 128: be_emit_char('q'); break;
	default:  panic("invalid load/store mode %+F", mode);
169
	}
Hannes Rapp's avatar
Hannes Rapp committed
170
171
172
173
174
}

/**
 * Emit store mode char
 */
175
static void sparc_emit_store_mode(ir_node const *const node)
Hannes Rapp's avatar
Hannes Rapp committed
176
177
{
	const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
178
179
180
	ir_mode *mode      = attr->load_store_mode;
	int      bits      = get_mode_size_bits(mode);

181
182
183
184
185
186
187
	switch (bits) {
	case   8: be_emit_char('b'); break;
	case  16: be_emit_char('h'); break;
	case  32: break;
	case  64: be_emit_char('d'); break;
	case 128: be_emit_char('q'); break;
	default:  panic("invalid load/store mode %+F", mode);
188
	}
Hannes Rapp's avatar
Hannes Rapp committed
189
190
}

191
static void emit_fp_suffix(const ir_mode *mode)
Hannes Rapp's avatar
Hannes Rapp committed
192
{
193
	assert(mode_is_float(mode));
194
195
196
197
198
	switch (get_mode_size_bits(mode)) {
	case  32: be_emit_char('s'); break;
	case  64: be_emit_char('d'); break;
	case 128: be_emit_char('q'); break;
	default:  panic("invalid FP mode");
199
	}
Hannes Rapp's avatar
Hannes Rapp committed
200
201
}

202
203
204
205
206
static void set_jump_target(ir_node *jump, ir_node *target)
{
	set_irn_link(jump, target);
}

207
208
static ir_node *get_jump_target(const ir_node *jump)
{
209
	return (ir_node*)get_irn_link(jump);
210
211
}

Hannes Rapp's avatar
Hannes Rapp committed
212
213
214
215
216
/**
 * Returns the target label for a control flow node.
 */
static void sparc_emit_cfop_target(const ir_node *node)
{
217
	ir_node *block = get_jump_target(node);
218
	be_gas_emit_block_name(block);
Hannes Rapp's avatar
Hannes Rapp committed
219
220
}

221
222
223
224
225
226
227
228
229
/**
 * returns true if a sparc_call calls a register and not an immediate
 */
static bool is_sparc_reg_call(const ir_node *node)
{
	const sparc_attr_t *attr = get_sparc_attr_const(node);
	return attr->immediate_value_entity == NULL;
}

230
231
static int get_sparc_Call_dest_addr_pos(const ir_node *node)
{
232
	assert(is_sparc_reg_call(node));
233
234
235
236
237
238
	return get_irn_arity(node)-1;
}

static bool ba_is_fallthrough(const ir_node *node)
{
	ir_node *block      = get_nodes_block(node);
239
	ir_node *next_block = (ir_node*)get_irn_link(block);
240
	return get_jump_target(node) == next_block;
241
242
243
244
245
246
}

static bool is_no_instruction(const ir_node *node)
{
	/* copies are nops if src_reg == dest_reg */
	if (be_is_Copy(node) || be_is_CopyKeep(node)) {
Matthias Braun's avatar
Matthias Braun committed
247
248
		const arch_register_t *src_reg  = arch_get_irn_register_in(node, 0);
		const arch_register_t *dest_reg = arch_get_irn_register_out(node, 0);
249
250
251
252

		if (src_reg == dest_reg)
			return true;
	}
253
254
	if (be_is_IncSP(node) && be_get_IncSP_offset(node) == 0)
		return true;
255
256
257
258
	/* Ba is not emitted if it is a simple fallthrough */
	if (is_sparc_Ba(node) && ba_is_fallthrough(node))
		return true;

259
	return be_is_Keep(node) || be_is_Start(node) || is_Phi(node);
260
261
262
263
}

static bool has_delay_slot(const ir_node *node)
{
264
265
266
	if (is_sparc_Ba(node)) {
		return !ba_is_fallthrough(node);
	}
267

268
	return arch_get_irn_flags(node) & sparc_arch_irn_flag_has_delay_slot;
269
270
271
272
273
274
275
276
277
278
279
280
281
}

/** returns true if the emitter for this sparc node can produce more than one
 * actual sparc instruction.
 * Usually it is a bad sign if we have to add instructions here. We should
 * rather try to get them lowered down. So we can actually put them into
 * delay slots and make them more accessible to the scheduler.
 */
static bool emits_multiple_instructions(const ir_node *node)
{
	if (has_delay_slot(node))
		return true;

282
	if (is_sparc_Call(node))
Matthias Braun's avatar
Matthias Braun committed
283
		return arch_get_irn_flags(node) & sparc_arch_irn_flag_aggregate_return;
284

285
286
	return is_sparc_SMulh(node) || is_sparc_UMulh(node)
		|| is_sparc_SDiv(node) || is_sparc_UDiv(node)
Manuel Mohr's avatar
Manuel Mohr committed
287
		|| be_is_MemPerm(node) || be_is_Perm(node)
288
		|| is_sparc_SubSP(node) || is_sparc_ASM(node);
289
290
}

291
static bool uses_reg(const ir_node *node, unsigned reg_index, unsigned width)
292
293
{
	int arity = get_irn_arity(node);
Matthias Braun's avatar
Matthias Braun committed
294
	for (int i = 0; i < arity; ++i) {
295
296
297
298
299
300
		const arch_register_t     *in_reg = arch_get_irn_register_in(node, i);
		const arch_register_req_t *in_req = arch_get_irn_register_req_in(node, i);
		if (in_reg == NULL)
			continue;
		if (reg_index < (unsigned)in_reg->global_index + in_req->width
			&& reg_index + width > in_reg->global_index)
301
302
303
304
305
			return true;
	}
	return false;
}

306
static bool writes_reg(const ir_node *node, unsigned reg_index, unsigned width)
307
{
308
	be_foreach_out(node, o) {
Matthias Braun's avatar
Matthias Braun committed
309
		const arch_register_t *out_reg = arch_get_irn_register_out(node, o);
310
311
312
313
314
		if (out_reg == NULL)
			continue;
		const arch_register_req_t *out_req = arch_get_irn_register_req_out(node, o);
		if (reg_index < (unsigned)out_reg->global_index + out_req->width
			&& reg_index + width > out_reg->global_index)
315
316
317
318
319
			return true;
	}
	return false;
}

320
321
322
323
324
325
326
327
328
329
330
331
static bool is_legal_delay_slot_filler(const ir_node *node)
{
	if (is_no_instruction(node))
		return false;
	if (emits_multiple_instructions(node))
		return false;
	if (rbitset_is_set(delay_slot_fillers, get_irn_idx(node)))
		return false;
	return true;
}

static bool can_move_down_into_delayslot(const ir_node *node, const ir_node *to)
332
{
333
334
335
336
	if (!is_legal_delay_slot_filler(node))
		return false;

	if (!be_can_move_down(heights, node, to))
337
338
339
340
		return false;

	if (is_sparc_Call(to)) {
		ir_node *check;
341
		/** all inputs are used after the delay slot so, we're fine */
342
343
344
345
346
347
		if (!is_sparc_reg_call(to))
			return true;

		check = get_irn_n(to, get_sparc_Call_dest_addr_pos(to));
		if (skip_Proj(check) == node)
			return false;
348

349
350
351
		/* the Call also destroys the value of %o7, but since this is
		 * currently marked as ignore register in the backend, it
		 * should never be used by the instruction in the delay slot. */
352
		if (uses_reg(node, REG_O7, 1))
353
354
355
356
357
			return false;
		return true;
	} else if (is_sparc_Return(to)) {
		/* return uses the value of %o7, all other values are not
		 * immediately used */
358
		if (writes_reg(node, REG_O7, 1))
359
360
361
362
363
			return false;
		return true;
	} else {
		/* the node must not use our computed values */
		int arity = get_irn_arity(to);
Matthias Braun's avatar
Matthias Braun committed
364
		for (int i = 0; i < arity; ++i) {
365
366
367
368
369
			ir_node *in = get_irn_n(to, i);
			if (skip_Proj(in) == node)
				return false;
		}
		return true;
370
371
372
	}
}

373
374
375
376
377
378
static bool can_move_up_into_delayslot(const ir_node *node, const ir_node *to)
{
	if (!be_can_move_up(heights, node, to))
		return false;

	/* node must not use any results of 'to' */
Matthias Braun's avatar
Matthias Braun committed
379
	for (int i = 0, arity = get_irn_arity(node); i < arity; ++i) {
380
381
382
383
384
385
386
387
		ir_node *in      = get_irn_n(node, i);
		ir_node *skipped = skip_Proj(in);
		if (skipped == to)
			return false;
	}

	/* register window cycling effects at Restore aren't correctly represented
	 * in the graph yet so we need this exception here */
388
	if (is_sparc_Restore(node) || is_sparc_RestoreZero(node)) {
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
		return false;
	} else if (is_sparc_Call(to)) {
		/* node must not overwrite any of the inputs of the call,
		 * (except for the dest_addr) */
		int dest_addr_pos = is_sparc_reg_call(to)
			? get_sparc_Call_dest_addr_pos(to) : -1;

		int call_arity = get_irn_arity(to);
		for (int i = 0; i < call_arity; ++i) {
			if (i == dest_addr_pos)
				continue;
			const arch_register_t *reg = arch_get_irn_register_in(to, i);
			if (reg == NULL)
				continue;
			const arch_register_req_t *req = arch_get_irn_register_req_in(to, i);
			if (writes_reg(node, reg->global_index, req->width))
				return false;
		}

		/* node must not write to one of the call outputs */
409
		be_foreach_out(to, o) {
410
411
412
413
414
415
416
417
418
419
			const arch_register_t *reg = arch_get_irn_register_out(to, o);
			if (reg == NULL)
				continue;
			const arch_register_req_t *req = arch_get_irn_register_req_out(to, o);
			if (writes_reg(node, reg->global_index, req->width))
				return false;
		}
	} else if (is_sparc_SDiv(to) || is_sparc_UDiv(to)) {
		/* node will be inserted between wr and div so it must not overwrite
		 * anything except the wr input */
Matthias Braun's avatar
Matthias Braun committed
420
		for (int i = 0, arity = get_irn_arity(to); i < arity; ++i) {
421
422
423
424
425
426
427
428
429
430
			assert((long)n_sparc_SDiv_dividend_high == (long)n_sparc_UDiv_dividend_high);
			if (i == n_sparc_SDiv_dividend_high)
				continue;
			const arch_register_t *reg = arch_get_irn_register_in(to, i);
			if (reg == NULL)
				continue;
			const arch_register_req_t *req = arch_get_irn_register_req_in(to, i);
			if (writes_reg(node, reg->global_index, req->width))
				return false;
		}
yb9976's avatar
yb9976 committed
431
	}
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
	return true;
}

static void optimize_fallthrough(ir_node *node)
{
	ir_node *proj_true  = NULL;
	ir_node *proj_false = NULL;

	assert((long)pn_sparc_Bicc_false == (long)pn_sparc_fbfcc_false);
	assert((long)pn_sparc_Bicc_true  == (long)pn_sparc_fbfcc_true);
	foreach_out_edge(node, edge) {
		ir_node *proj = get_edge_src_irn(edge);
		long nr = get_Proj_proj(proj);
		if (nr == pn_sparc_Bicc_true) {
			proj_true = proj;
		} else {
			assert(nr == pn_sparc_Bicc_false);
			proj_false = proj;
		}
	}
	assert(proj_true != NULL && proj_false != NULL);

	/* for now, the code works for scheduled and non-schedules blocks */
	const ir_node *block = get_nodes_block(node);

	/* we have a block schedule */
	const ir_node *next_block = (ir_node*)get_irn_link(block);

	if (get_jump_target(proj_true) == next_block) {
		/* exchange both proj destinations so the second one can be omitted */
		set_Proj_proj(proj_true,  pn_sparc_Bicc_false);
		set_Proj_proj(proj_false, pn_sparc_Bicc_true);

		sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr(node);
		attr->relation = get_negated_relation(attr->relation);
	}
}

Hannes Rapp's avatar
Hannes Rapp committed
470
/**
471
 * search for an instruction that can fill the delay slot of @p node
Hannes Rapp's avatar
Hannes Rapp committed
472
 */
473
static ir_node *pick_delay_slot_for(ir_node *node)
Hannes Rapp's avatar
Hannes Rapp committed
474
{
475
	static const unsigned PICK_DELAY_SLOT_MAX_DISTANCE = 10;
476
477
	assert(has_delay_slot(node));

478
479
480
	if (is_sparc_Bicc(node) || is_sparc_fbfcc(node)) {
		optimize_fallthrough(node);
	}
481

482
	unsigned tries = 0;
483
	sched_foreach_reverse_before(node, schedpoint) {
484
485
		if (has_delay_slot(schedpoint))
			break;
486
487
		if (tries++ >= PICK_DELAY_SLOT_MAX_DISTANCE)
			break;
488

489
		if (!can_move_down_into_delayslot(schedpoint, node))
490
491
			continue;

492
493
494
495
496
497
		/* found something */
		return schedpoint;
	}

	/* search after the current position */
	tries = 0;
498
	sched_foreach_after(node, schedpoint) {
499
500
		if (has_delay_slot(schedpoint))
			break;
501
502
		if (tries++ >= PICK_DELAY_SLOT_MAX_DISTANCE)
			break;
503
		if (!is_legal_delay_slot_filler(schedpoint))
504
			continue;
505
		if (!can_move_up_into_delayslot(schedpoint, node))
506
507
508
509
510
511
			continue;

		/* found something */
		return schedpoint;
	}

512
513
514
515
516
517
518
519
520
521
522
523
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
550
551
552
553
554
555
	/* look in successor blocks */
	ir_node *block = get_nodes_block(node);
	/* TODO: sort succs by execution frequency */
	foreach_block_succ(block, edge) {
		ir_node *succ = get_edge_src_irn(edge);
		/* we can't easily move up stuff from blocks with multiple predecessors
		 * since the instruction is lacking for the other preds then.
		 * (We also don't have to do any phi translation) */
		if (get_Block_n_cfgpreds(succ) > 1)
			continue;

		tries = 0;
		sched_foreach(succ, schedpoint) {
			if (has_delay_slot(schedpoint))
				break;
			/* can't move pinned nodes accross blocks */
			if (get_irn_pinned(schedpoint) == op_pin_state_pinned)
				continue;
			/* restore doesn't model register window switching correctly,
			 * so it appears like we could move it, which is not true */
			if (is_sparc_Restore(schedpoint)
			    || is_sparc_RestoreZero(schedpoint))
				continue;
			if (tries++ >= PICK_DELAY_SLOT_MAX_DISTANCE)
				break;
			if (!is_legal_delay_slot_filler(schedpoint))
				continue;
			if (can_move_up_into_delayslot(schedpoint, node)) {
				/* it's fine to move the insn accross blocks */
				return schedpoint;
			} else if (is_sparc_Bicc(node) || is_sparc_fbfcc(node)) {
				ir_node *proj = get_Block_cfgpred(succ, 0);
				long     nr   = get_Proj_proj(proj);
				if ((nr == pn_sparc_Bicc_true || nr == pn_sparc_fbfcc_true)
					&& be_can_move_up(heights, schedpoint, succ)) {
					/* we can use it with the annul flag */
					sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr(node);
					attr->annul_delay_slot = true;
					return schedpoint;
				}
			}
		}
	}

556
	return NULL;
Hannes Rapp's avatar
Hannes Rapp committed
557
558
}

559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
void sparc_emitf(ir_node const *const node, char const *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	sparc_emit_indent();
	for (;;) {
		char const *start = fmt;

		while (*fmt != '%' && *fmt != '\0')
			++fmt;
		be_emit_string_len(start, fmt - start);
		if (*fmt == '\0')
			break;
		++fmt;

		bool plus = false;
		if (*fmt == '+') {
			plus = true;
			++fmt;
		}

		switch (*fmt++) {
		case '%':
			be_emit_char('%');
			break;

585
586
587
588
589
590
591
592
593
		case 'A': {
			const sparc_jmp_cond_attr_t *attr
				= get_sparc_jmp_cond_attr_const(node);
			if (attr->annul_delay_slot) {
				be_emit_cstring(",a");
			}
			break;
		}

594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
		case 'D':
			if (*fmt < '0' || '9' <= *fmt)
				goto unknown;
			sparc_emit_dest_register(node, *fmt++ - '0');
			break;

		case 'E': {
			sparc_attr_t const *const attr = get_sparc_attr_const(node);
			be_gas_emit_entity(attr->immediate_value_entity);
			if (attr->immediate_value != 0) {
				be_emit_irprintf(plus ? "%+d" : "%d", attr->immediate_value);
			}
			break;
		}

		case 'F': {
			ir_mode *mode;
			switch (*fmt++) {
			case 'D': mode = get_sparc_fp_conv_attr_const(node)->dest_mode; break;
			case 'M': mode = get_sparc_fp_attr_const(node)->fp_mode;        break;
			case 'S': mode = get_sparc_fp_conv_attr_const(node)->src_mode;  break;
			default:  goto unknown;
			}
			emit_fp_suffix(mode);
			break;
		}

		case 'H':
			sparc_emit_high_immediate(node);
			break;

625
626
627
		case 'L': {
			ir_node *n = va_arg(ap, ir_node*);
			sparc_emit_cfop_target(n);
628
			break;
629
		}
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647

		case 'M':
			switch (*fmt++) {
			case 'L': sparc_emit_load_mode(node);  break;
			case 'S': sparc_emit_store_mode(node); break;
			default:  goto unknown;
			}
			break;

		case 'O':
			if (*fmt < '0' || '9' <= *fmt)
				goto unknown;
			sparc_emit_offset(node, *fmt++ - '0');
			break;

		case 'R': {
			arch_register_t const *const reg = va_arg(ap, const arch_register_t*);
			be_emit_char('%');
648
			be_emit_string(reg->name);
649
650
651
652
653
654
655
656
657
658
659
660
661
			break;
		}

		case 'S': {
			bool imm = false;
			if (*fmt == 'I') {
				imm = true;
				++fmt;
			}
			if (*fmt < '0' || '9' <= *fmt)
				goto unknown;
			unsigned const pos = *fmt++ - '0';
			if (imm && arch_get_irn_flags(node) & (arch_irn_flags_t)sparc_arch_irn_flag_immediate_form) {
662
663
664
				const sparc_attr_t *const attr = get_sparc_attr_const(node);
				sparc_emit_immediate(attr->immediate_value,
				                     attr->immediate_value_entity);
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
			} else {
				sparc_emit_source_register(node, pos);
			}
			break;
		}

		case 'd': {
			int const num = va_arg(ap, int);
			be_emit_irprintf(plus ? "%+d" : "%d", num);
			break;
		}

		case 's': {
			char const *const str = va_arg(ap, char const*);
			be_emit_string(str);
			break;
		}

		case 'u': {
			unsigned const num = va_arg(ap, unsigned);
			be_emit_irprintf(plus ? "%+u" : "%u", num);
			break;
		}

689
690
691
692
693
694
		case 'X': {
			unsigned const num = va_arg(ap, unsigned);
			be_emit_irprintf("%X", num);
			break;
		}

695
696
697
698
699
700
701
702
703
		default:
unknown:
			panic("unknown format conversion in sparc_emitf()");
		}
	}
	be_emit_finish_line_gas(node);
	va_end(ap);
}

Hannes Rapp's avatar
Hannes Rapp committed
704
705
706
707
708
/**
 * Emits code for stack space management
 */
static void emit_be_IncSP(const ir_node *irn)
{
709
	int offset = be_get_IncSP_offset(irn);
Hannes Rapp's avatar
Hannes Rapp committed
710

711
	if (offset == 0)
712
		return;
Hannes Rapp's avatar
Hannes Rapp committed
713

714
	/* SPARC stack grows downwards */
715
716
	char const *const insn = offset > 0 ? offset = -offset, "add" : "sub";
	sparc_emitf(irn, "%s %S0, %d, %D0", insn, offset);
717
}
718

719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
static const char *emit_asm_operand(const ir_node *node, const char *s)
{
	assert(*s == '%');
	char c = *(++s);
	/* parse modifiers */
	if (c == '\0') {
		ir_fprintf(stderr, "Warning: asm text (%+F) ends with %%\n", node);
		be_emit_char('%');
		return s;
	} else if (c == '%') {
		be_emit_char('%');
		return s+1;
	} else if (c < '0' || c > '9') {
		ir_fprintf(stderr, "Warning: asm text (%+F) contains unknown modifier '%c' for asm op\n",
		           node, c);
		return s+1;
	}

	/* parse number */
	int num = 0;
	int p   = 0;
	sscanf(s, "%d%n", &num, &p);
	s += p;

	const sparc_asm_attr_t     *const attr     = get_sparc_asm_attr_const(node);
	const sparc_asm_operand_t  *const operands = attr->operands;
	if ((size_t)num > ARR_LEN(operands)) {
		ir_fprintf(stderr,
		           "Error: Custom assembler references invalid input/output (%+F)\n",
		           node);
		return s;
	}

	const sparc_asm_operand_t *const operand = &operands[num];
	const arch_register_t *reg = NULL;
	switch (operand->kind) {
	case ASM_OPERAND_IMMEDIATE:
		sparc_emit_immediate(operand->immediate_value,
		                     operand->immediate_value_entity);
		return s;
	case ASM_OPERAND_INPUT_VALUE:
		reg = arch_get_irn_register_in(node, operand->pos);
		break;
	case ASM_OPERAND_OUTPUT_VALUE:
		reg = arch_get_irn_register_out(node, operand->pos);
		break;
	}

	/* emit the register */
	sparc_emit_register(reg);
	return s;
}

772
773
774
775
776
777
778
779
780
781
static void emit_sparc_ASM(const ir_node *node)
{
	be_emit_cstring("#APP\n");
	be_emit_write_line();

	const sparc_asm_attr_t *attr = get_sparc_asm_attr_const(node);
	const char             *s    = get_id_str(attr->text);

	if (s[0] != '\t')
		be_emit_char('\t');
782
783
784
785
786
787
788
	while (*s != 0) {
		if (*s == '%') {
			s = emit_asm_operand(node, s);
		} else {
			be_emit_char(*s++);
		}
	}
789
790
791
792
793
794

	be_emit_cstring("\n#NO_APP\n");
	be_emit_write_line();

}

Manuel Mohr's avatar
Manuel Mohr committed
795
796
797
798
799
/**
 * Emits code for stack space management.
 */
static void emit_sparc_SubSP(const ir_node *irn)
{
800
801
	sparc_emitf(irn, "sub %S0, %SI1, %D0");
	sparc_emitf(irn, "add %S0, %u, %D1", SPARC_MIN_STACKSIZE);
Hannes Rapp's avatar
Hannes Rapp committed
802
}
803

804
static void fill_delay_slot(const ir_node *node)
805
{
806
	emitting_delay_slot = true;
807
808
809
810
	const ir_node *filler = pmap_get(ir_node, delay_slots, node);
	if (filler != NULL) {
		assert(!is_no_instruction(filler));
		assert(!emits_multiple_instructions(filler));
811
		be_emit_node(filler);
812
	} else {
813
		sparc_emitf(NULL, "nop");
814
	}
815
	emitting_delay_slot = false;
816
817
}

818
static void emit_sparc_Div(const ir_node *node, char const *const insn)
819
{
820
	sparc_emitf(node, "wr %S0, 0, %%y");
821

Matthias Braun's avatar
Matthias Braun committed
822
823
824
825
	/* TODO: we should specify number of delayslots in an architecture
	 * specification */
	unsigned wry_delay_count = 3;
	for (unsigned i = 0; i < wry_delay_count; ++i) {
826
827
828
829
830
831
832
		if (i == 0) {
			fill_delay_slot(node);
		} else {
			emitting_delay_slot = true;
			sparc_emitf(NULL, "nop");
			emitting_delay_slot = false;
		}
833
834
	}

835
	sparc_emitf(node, "%s %S1, %SI2, %D0", insn);
836
837
838
839
}

static void emit_sparc_SDiv(const ir_node *node)
{
840
	emit_sparc_Div(node, "sdiv");
841
842
843
844
}

static void emit_sparc_UDiv(const ir_node *node)
{
845
	emit_sparc_Div(node, "udiv");
846
847
}

848
static void emit_sparc_Call(const ir_node *node)
Hannes Rapp's avatar
Hannes Rapp committed
849
{
850
851
	if (is_sparc_reg_call(node)) {
		int dest_addr = get_sparc_Call_dest_addr_pos(node);
852
		sparc_emitf(node, "call %R", arch_get_irn_register_in(node, dest_addr));
853
	} else {
854
		sparc_emitf(node, "call %E, 0");
Hannes Rapp's avatar
Hannes Rapp committed
855
	}
856

857
	fill_delay_slot(node);
858

Matthias Braun's avatar
Matthias Braun committed
859
	if (arch_get_irn_flags(node) & sparc_arch_irn_flag_aggregate_return) {
860
		sparc_emitf(NULL, "unimp 8");
861
	}
Hannes Rapp's avatar
Hannes Rapp committed
862
863
}

864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
static void emit_sparc_Cas(const ir_node *node)
{
	/* custom emiter is just here to check for should_be_same constraint
	 * which isn't guaranteed to be fulfilled in current firm backend */
	if (arch_get_irn_register_out(node, pn_sparc_Cas_res) !=
	    arch_get_irn_register_in(node, n_sparc_Cas_new)) {
	    panic("sparc: should_be_same in Cas not fulfilled");
	}
	/* except for some patched gaisler binutils nobody understands cas
	 * in v8/leon mode, so we encode the cas in binary form */
#if 0
	sparc_emitf(node, "cas [%S0], %S1, %S2");
#else
	const arch_register_t *reg_new
		= arch_get_irn_register_in(node, n_sparc_Cas_new);
	const arch_register_t *reg_ptr
		= arch_get_irn_register_in(node, n_sparc_Cas_ptr);
	const arch_register_t *reg_old
		= arch_get_irn_register_in(node, n_sparc_Cas_old);
	uint32_t encoding = 3u<<30 | (reg_new->encoding<<25) | (0x3C << 19)
	       | (reg_ptr->encoding<<14) | (0x80<<5) | (reg_old->encoding);
	sparc_emitf(node, ".long 0x%X  /* cas [%S0], %S1, %S2", encoding);
#endif
}

889
890
static void emit_be_Perm(const ir_node *irn)
{
Matthias Braun's avatar
Matthias Braun committed
891
892
	ir_mode *mode = get_irn_mode(get_irn_n(irn, 0));
	if (mode_is_float(mode)) {
893
894
		arch_register_t const *const reg0 = arch_get_irn_register_out(irn, 0);
		arch_register_t const *const reg1 = arch_get_irn_register_out(irn, 1);
Matthias Braun's avatar
Matthias Braun committed
895
896
		unsigned reg_idx0 = reg0->global_index;
		unsigned reg_idx1 = reg1->global_index;
897
		unsigned width    = arch_get_irn_register_req_out(irn, 0)->width;
Matthias Braun's avatar
Matthias Braun committed
898
899
900
901
902
903
904
905
		for (unsigned i = 0; i < width; ++i) {
			const arch_register_t *r0 = &sparc_registers[reg_idx0+i];
			const arch_register_t *r1 = &sparc_registers[reg_idx1+i];
			sparc_emitf(irn, "fmovs %R, %%f31", r0);
			sparc_emitf(irn, "fmovs %R, %R", r1, r0);
			sparc_emitf(irn, "fmovs %%f31, %R", r1);
		}
	} else {
906
907
908
		sparc_emitf(irn, "xor %D1, %D0, %D0");
		sparc_emitf(irn, "xor %D1, %D0, %D1");
		sparc_emitf(irn, "xor %D1, %D0, %D0");
Matthias Braun's avatar
Matthias Braun committed
909
	}
910
911
}

912
913
/* The stack pointer must always be SPARC_STACK_ALIGNMENT bytes aligned, so get
 * the next bigger integer that's evenly divisible by it. */
914
static unsigned get_aligned_sp_change(const unsigned num_regs)
915
{
916
	const unsigned bytes = num_regs * SPARC_REGISTER_SIZE;
917
	return round_up2(bytes, SPARC_STACK_ALIGNMENT);
918
919
}

920
921
922
/* Spill register l0 or both l0 and l1, depending on n_spilled and n_to_spill.*/
static void memperm_emit_spill_registers(const ir_node *node, int n_spilled,
                                         int n_to_spill)
Hannes Rapp's avatar
Hannes Rapp committed
923
{
924
	assert(n_spilled < n_to_spill);
925

926
927
928
929
930
931
	if (n_spilled == 0) {
		/* We always reserve stack space for two registers because during copy
		 * processing we don't know yet if we also need to handle a cycle which
		 * needs two registers.  More complicated code in emit_MemPerm would
		 * prevent wasting SPARC_REGISTER_SIZE bytes of stack space but
		 * it is not worth the worse readability of emit_MemPerm. */
Hannes Rapp's avatar
Hannes Rapp committed
932

933
934
		/* Keep stack pointer aligned. */
		unsigned sp_change = get_aligned_sp_change(2);
935
		sparc_emitf(node, "sub %%sp, %u, %%sp", sp_change);
Hannes Rapp's avatar
Hannes Rapp committed
936

937
		/* Spill register l0. */
938
		sparc_emitf(node, "st %%l0, [%%sp%+d]", SPARC_MIN_STACKSIZE);
939
	}
Hannes Rapp's avatar
Hannes Rapp committed
940

941
942
	if (n_to_spill == 2) {
		/* Spill register l1. */
943
		sparc_emitf(node, "st %%l1, [%%sp%+d]", SPARC_MIN_STACKSIZE + SPARC_REGISTER_SIZE);
944
945
	}
}
946

947
948
949
950
951
/* Restore register l0 or both l0 and l1, depending on n_spilled. */
static void memperm_emit_restore_registers(const ir_node *node, int n_spilled)
{
	if (n_spilled == 2) {
		/* Restore register l1. */
952
		sparc_emitf(node, "ld [%%sp%+d], %%l1", SPARC_MIN_STACKSIZE + SPARC_REGISTER_SIZE);
953
	}
Hannes Rapp's avatar
Hannes Rapp committed
954

955
	/* Restore register l0. */
956
	sparc_emitf(node, "ld [%%sp%+d], %%l0", SPARC_MIN_STACKSIZE);
957
958

	/* Restore stack pointer. */
Matthias Braun's avatar
Matthias Braun committed
959
	unsigned sp_change = get_aligned_sp_change(2);
960
	sparc_emitf(node, "add %%sp, %u, %%sp", sp_change);
961
962
963
964
965
966
967
968
969
970
971
972
}

/* Emit code to copy in_ent to out_ent.  Only uses l0. */
static void memperm_emit_copy(const ir_node *node, ir_entity *in_ent,
                              ir_entity *out_ent)
{
	ir_graph          *irg     = get_irn_irg(node);
	be_stack_layout_t *layout  = be_get_irg_stack_layout(irg);
	int                off_in  = be_get_stack_entity_offset(layout, in_ent, 0);
	int                off_out = be_get_stack_entity_offset(layout, out_ent, 0);

	/* Load from input entity. */
973
	sparc_emitf(node, "ld [%%fp%+d], %%l0", off_in);
974
	/* Store to output entity. */
975
	sparc_emitf(node, "st %%l0, [%%fp%+d]", off_out);
976
977
978
979
980
981
982
983
984
985
986
987
}

/* Emit code to swap ent1 and ent2.  Uses l0 and l1. */
static void memperm_emit_swap(const ir_node *node, ir_entity *ent1,
                              ir_entity *ent2)
{
	ir_graph          *irg     = get_irn_irg(node);
	be_stack_layout_t *layout  = be_get_irg_stack_layout(irg);
	int                off1    = be_get_stack_entity_offset(layout, ent1, 0);
	int                off2    = be_get_stack_entity_offset(layout, ent2, 0);

	/* Load from first input entity. */
988
	sparc_emitf(node, "ld [%%fp%+d], %%l0", off1);
989
	/* Load from second input entity. */
990
	sparc_emitf(node, "ld [%%fp%+d], %%l1", off2);
991
	/* Store first value to second output entity. */
992
	sparc_emitf(node, "st %%l0, [%%fp%+d]", off2);
993
	/* Store second value to first output entity. */
994
	sparc_emitf(node, "st %%l1, [%%fp%+d]", off1);
995
996
997
998
999
}

/* Find the index of ent in ents or return -1 if not found. */
static int get_index(ir_entity **ents, int n, ir_entity *ent)
{
Matthias Braun's avatar
Matthias Braun committed
1000
	for (int i = 0; i < n; ++i) {
1001
1002
		if (ents[i] == ent)
			return i;
Matthias Braun's avatar
Matthias Braun committed
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

	return -1;
}

/*
 * Emit code for a MemPerm node.
 *
 * Analyze MemPerm for copy chains and cyclic swaps and resolve them using
 * loads and stores.
 * This function is conceptually very similar to permute_values in
 * beprefalloc.c.
 */
static void emit_be_MemPerm(const ir_node *node)
{
	int         memperm_arity = be_get_MemPerm_entity_arity(node);
	/* Upper limit for the number of participating entities is twice the
	 * arity, e.g., for a simple copying MemPerm node with one input/output. */
	int         max_size      = 2 * memperm_arity;
	ir_entity **entities      = ALLOCANZ(ir_entity *, max_size);
	/* sourceof contains the input entity for each entity.  If an entity is
	 * never used as an output, its entry in sourceof is a fix point. */
	int        *sourceof      = ALLOCANZ(int,         max_size);
	/* n_users counts how many output entities use this entity as their input.*/
	int        *n_users       = ALLOCANZ(int,         max_size);
	/* n_spilled records the number of spilled registers, either 1 or 2. */
	int         n_spilled     = 0;

yb9976's avatar
yb9976 committed
1031
#ifndef NDEBUG
1032
1033
1034
1035
	/* This implementation currently only works with frame pointers. */
	ir_graph          *irg    = get_irn_irg(node);
	be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
	assert(!layout->sp_relative && "MemPerms currently do not work without frame pointers");
yb9976's avatar
yb9976 committed
1036
#endif
1037

Matthias Braun's avatar
Matthias Braun committed
1038
	for (int i = 0; i < max_size; ++i) {
1039
		sourceof[i] = i;
Hannes Rapp's avatar
Hannes Rapp committed
1040
1041
	}

Matthias Braun's avatar
Matthias Braun committed
1042
1043
	int n = 0;
	for (int i = 0; i < memperm_arity; ++i) {
1044
1045
		ir_entity *out  = be_get_MemPerm_out_entity(node, i);
		ir_entity *in   = be_get_MemPerm_in_entity(node, i);
1046

1047
1048
1049
1050
1051
		/* Insert into entities to be able to operate on unique indices. */
		if (get_index(entities, n, out) == -1)
			entities[n++] = out;
		if (get_index(entities, n, in) == -1)
			entities[n++] = in;
Hannes Rapp's avatar
Hannes Rapp committed
1052

Matthias Braun's avatar
Matthias Braun committed
1053
1054
		int oidx = get_index(entities, n, out);
		int iidx = get_index(entities, n, in);
1055
1056
1057

		sourceof[oidx] = iidx; /* Remember the source. */
		++n_users[iidx]; /* Increment number of users of this entity. */
Hannes Rapp's avatar
Hannes Rapp committed
1058
	}
1059

1060
	/* First do all the copies. */
Matthias Braun's avatar
Matthias Braun committed
1061
	for (int oidx = 0; oidx < n; /* empty */) {
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
		int iidx = sourceof[oidx];

		/* Nothing to do for fix points.
		 * Also, if entities[oidx] is used as an input by another copy, we
		 * can't overwrite entities[oidx] yet.*/
		if (iidx == oidx || n_users[oidx] > 0) {
			++oidx;
			continue;
		}

		/* We found the end of a 'chain', so do the copy. */
		if (n_spilled == 0) {
			memperm_emit_spill_registers(node, n_spilled, /*n_to_spill=*/1);
			n_spilled = 1;
		}
		memperm_emit_copy(node, entities[iidx], entities[oidx]);

		/* Mark as done. */
		sourceof[oidx] = oidx;

		assert(n_users[iidx] > 0);
		/* Decrementing the number of users might enable us to do another
		 * copy. */
		--n_users[iidx];

		if (iidx < oidx && n_users[iidx] == 0) {
			oidx = iidx;
		} else {
			++oidx;
		}
	}

	/* The rest are cycles. */
Matthias Braun's avatar
Matthias Braun committed
1095
	for (int oidx = 0; oidx < n; /* empty */) {
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
		int iidx = sourceof[oidx];

		/* Nothing to do for fix points. */
		if (iidx == oidx) {
			++oidx;
			continue;
		}

		assert(n_users[iidx] == 1);

		/* Swap the two values to resolve the cycle. */
		if (n_spilled < 2) {
			memperm_emit_spill_registers(node, n_spilled, /*n_to_spill=*/2);
			n_spilled = 2;
		}
		memperm_emit_swap(node, entities[iidx], entities[oidx]);

Matthias Braun's avatar
Matthias Braun committed
1113
		int tidx = sourceof[iidx];
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
		/* Mark as done. */
		sourceof[iidx] = iidx;

		/* The source of oidx is now the old source of iidx, because we swapped
		 * the two entities. */
		sourceof[oidx] = tidx;
	}

#ifdef DEBUG_libfirm
	/* Only fix points should remain. */
Matthias Braun's avatar
Matthias Braun committed
1124
	for (int i = 0; i < max_size; ++i) {
1125
1126
1127
1128
1129
		assert(sourceof[i] == i);
	}
#endif

	assert(n_spilled > 0 && "Useless MemPerm node");
1130

1131
	memperm_emit_restore_registers(node, n_spilled);
Hannes Rapp's avatar
Hannes Rapp committed
1132
1133
}

1134
static void emit_sparc_Return(const ir_node *node)
1135
{
1136
1137
1138
1139
	ir_graph  *irg    = get_irn_irg(node);
	ir_entity *entity = get_irg_entity(irg);
	ir_type   *type   = get_entity_type(entity);

1140
1141
1142
1143
	const char *destreg = "%o7";

	/* hack: we don't explicitely model register changes because of the
	 * restore node. So we have to do it manually here */
1144
1145
1146
	const ir_node *delay_slot = pmap_get(ir_node, delay_slots, node);
	if (delay_slot != NULL &&
	    (is_sparc_Restore(delay_slot) || is_sparc_RestoreZero(delay_slot))) {
1147
1148
		destreg = "%i7";
	}
1149
1150
	char const *const offset = get_method_calling_convention(type) & cc_compound_ret ? "12" : "8";
	sparc_emitf(node, "jmp %s+%s", destreg, offset);
1151
	fill_delay_slot(node);
1152
1153
}

1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
{
	unsigned idx = reg->global_index;
	if (idx < REG_I0 || idx > REG_I7)
		return reg;
	idx += REG_O0 - REG_I0;
	assert(REG_O0 <= idx && idx <= REG_O7);
	return &sparc_registers[idx];
}

static void emit_sparc_Restore(const ir_node *node)
{
	const arch_register_t *destreg
		= arch_get_irn_register_out(node, pn_sparc_Restore_res);
1168
	sparc_emitf(node, "restore %S2, %SI3, %R", map_i_to_o_reg(destreg));
1169
1170
}

1171
static void emit_sparc_FrameAddr(const ir_node *node)
Hannes Rapp's avatar
Hannes Rapp committed
1172
{
1173
1174
	const sparc_attr_t *attr   = get_sparc_attr_const(node);
	int32_t             offset = attr->immediate_value;
1175

1176
1177
1178
	char const *const insn = offset > 0 ? offset = -offset, "sub" : "add";
	assert(sparc_is_value_imm_encodeable(offset));
	sparc_emitf(node, "%s %S0, %d, %D0", insn, offset);
Hannes Rapp's avatar
Hannes Rapp committed
1179
1180
}

1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
static const char *get_icc_unsigned(ir_relation relation)
{
	switch (relation & (ir_relation_less_equal_greater)) {
	case ir_relation_false:              return "bn";
	case ir_relation_equal:              return "be";
	case ir_relation_less:               return "blu";
	case ir_relation_less_equal:         return "bleu";
	case ir_relation_greater:            return "bgu";
	case ir_relation_greater_equal:      return "bgeu";
	case ir_relation_less_greater:       return "bne";
	case ir_relation_less_equal_greater: return "ba";
	default: panic("Cmp has unsupported relation");
1193
1194
1195
	}
}

1196
static const char *get_icc_signed(ir_relation relation)
1197
{
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
	switch (relation & (ir_relation_less_equal_greater)) {
	case ir_relation_false:              return "bn";
	case ir_relation_equal:              return "be";
	case ir_relation_less:               return "bl";
	case ir_relation_less_equal:         return "ble";
	case ir_relation_greater:            return "bg";
	case ir_relation_greater_equal:      return "bge";
	case ir_relation_less_greater:       return "bne";
	case ir_relation_less_equal_greater: return "ba";
	default: panic("Cmp has unsupported relation");
1208
1209
1210
	}
}

1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
static const char *get_fcc(ir_relation relation)
{
	switch (relation) {
	case ir_relation_false:                   return "fbn";
	case ir_relation_equal:                   return "fbe";
	case ir_relation_less:                    return "fbl";
	case ir_relation_less_equal:              return "fble";
	case ir_relation_greater:                 return "fbg";
	case ir_relation_greater_equal:           return "fbge";
	case ir_relation_less_greater:            return "fblg";
	case ir_relation_less_equal_greater:      return "fbo";
	case ir_relation_unordered:               return "fbu";
	case ir_relation_unordered_equal:         return "fbue";
	case ir_relation_unordered_less:          return "fbul";
	case ir_relation_unordered_less_equal:    return "fbule";
	case ir_relation_unordered_greater:       return "fbug";
	case ir_relation_unordered_greater_equal: return "fbuge";
	case ir_relation_unordered_less_greater:  return "fbne";
	case ir_relation_true:                    return "fba";
1230
	}
1231
	panic("invalid relation");
1232
1233
}

1234
typedef const char* (*get_cc_func)(ir_relation relation);
Hannes Rapp's avatar
Hannes Rapp committed
1235

1236
static void emit_sparc_branch(const ir_node *node, get_cc_func get_cc)
Hannes Rapp's avatar
Hannes Rapp committed
1237
{
1238
	const sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr_const(node);
1239
1240
1241
	ir_relation    relation    = attr->relation;
	const ir_node *proj_true   = NULL;
	const ir_node *proj_false  = NULL;