sparc_emitter.c 24.5 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   emit assembler for a backend graph
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
44
45
46
47
48
49
50
51
52
53
 */
#include "config.h"

#include <limits.h>

#include "xmalloc.h"
#include "tv.h"
#include "iredges.h"
#include "debug.h"
#include "irgwalk.h"
#include "irprintf.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"

#include "../besched.h"
#include "../beblocksched.h"
#include "../beirg.h"
#include "../begnuas.h"
#include "../be_dbgout.h"
#include "../benode.h"

#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
54
55
#include "gen_sparc_regalloc_if.h"

Hannes Rapp's avatar
Hannes Rapp committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

/**
 * Returns the register at in position pos.
 */
static const arch_register_t *get_in_reg(const ir_node *node, int pos)
{
	ir_node                *op;
	const arch_register_t  *reg = NULL;

	assert(get_irn_arity(node) > pos && "Invalid IN position");

	/* The out register of the operator at position pos is the
	   in register we need. */
	op = get_irn_n(node, pos);

	reg = arch_get_irn_register(op);

	assert(reg && "no in register found");
	return reg;
}

/**
 * Returns the register at out position pos.
 */
static const arch_register_t *get_out_reg(const ir_node *node, int pos)
{
	ir_node                *proj;
	const arch_register_t  *reg = NULL;

	/* 1st case: irn is not of mode_T, so it has only                 */
	/*           one OUT register -> good                             */
	/* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
	/*           Proj with the corresponding projnum for the register */

	if (get_irn_mode(node) != mode_T) {
		reg = arch_get_irn_register(node);
	} else if (is_sparc_irn(node)) {
		reg = arch_irn_get_register(node, pos);
	} else {
		const ir_edge_t *edge;

		foreach_out_edge(node, edge) {
			proj = get_edge_src_irn(edge);
			assert(is_Proj(proj) && "non-Proj from mode_T node");
			if (get_Proj_proj(proj) == pos) {
				reg = arch_get_irn_register(proj);
				break;
			}
		}
	}

	assert(reg && "no out register found");
	return reg;
}

112
113
114
115
116
static bool is_valid_immediate(int32_t value)
{
	return -4096 <= value && value < 4096;
}

Hannes Rapp's avatar
Hannes Rapp committed
117
118
void sparc_emit_immediate(const ir_node *node)
{
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
	const sparc_attr_t *attr   = get_sparc_attr_const(node);
	ir_entity          *entity = attr->immediate_value_entity;

	if (entity == NULL) {
		int32_t value = attr->immediate_value;
		assert(is_valid_immediate(value));
		be_emit_irprintf("%d", value);
	} else {
		be_emit_cstring("%lo(");
		be_gas_emit_entity(entity);
		if (attr->immediate_value != 0) {
			be_emit_irprintf("%+d", attr->immediate_value);
		}
		be_emit_char(')');
	}
134
135
136
137
}

void sparc_emit_high_immediate(const ir_node *node)
{
138
139
140
141
142
143
144
145
146
147
148
149
150
151
	const sparc_attr_t *attr   = get_sparc_attr_const(node);
	ir_entity          *entity = attr->immediate_value_entity;

	be_emit_cstring("%hi(");
	if (entity == NULL) {
		uint32_t value = (uint32_t) attr->immediate_value;
		be_emit_irprintf("0x%X", value);
	} else {
		be_gas_emit_entity(entity);
		if (attr->immediate_value != 0) {
			be_emit_irprintf("%+d", attr->immediate_value);
		}
	}
	be_emit_char(')');
Hannes Rapp's avatar
Hannes Rapp committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
}

void sparc_emit_source_register(const ir_node *node, int pos)
{
	const arch_register_t *reg = get_in_reg(node, pos);
	be_emit_char('%');
	be_emit_string(arch_register_get_name(reg));
}

void sparc_emit_dest_register(const ir_node *node, int pos)
{
	const arch_register_t *reg = get_out_reg(node, pos);
	be_emit_char('%');
	be_emit_string(arch_register_get_name(reg));
}

/**
 * Emits either a imm or register depending on arity of node
 * @param node
 * @param register no (-1 if no register)
 */
void sparc_emit_reg_or_imm(const ir_node *node, int pos)
{
	if (get_irn_arity(node) > pos) {
		// we have reg input
		sparc_emit_source_register(node, pos);
	} else {
		// we have a imm input
		sparc_emit_immediate(node);
	}
}

184
185
186
187
188
189
190
static bool is_stack_pointer_relative(const ir_node *node)
{
	const arch_register_t *sp = &sparc_gp_regs[REG_SP];
	return (is_sparc_St(node) && get_in_reg(node, n_sparc_St_ptr) == sp)
	    || (is_sparc_Ld(node) && get_in_reg(node, n_sparc_Ld_ptr) == sp);
}

191
192
193
/**
 * emit SP offset
 */
194
void sparc_emit_offset(const ir_node *node, int offset_node_pos)
Hannes Rapp's avatar
Hannes Rapp committed
195
{
196
	const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
197

198
199
200
201
202
203
204
	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) {
205
206
207
208
209
210
211
212
213
		int32_t offset = attr->base.immediate_value;
		/* bad hack: the real stack stuff is behind the always-there spill
		 * space for the register window and stack */
		if (is_stack_pointer_relative(node))
			offset += SPARC_MIN_STACKSIZE;
		if (offset != 0) {
			assert(is_valid_immediate(offset));
			be_emit_irprintf("%+ld", offset);
		}
214
215
	} else if (attr->base.immediate_value != 0
			|| attr->base.immediate_value_entity != NULL) {
216
217
		be_emit_char('+');
		sparc_emit_immediate(node);
218
	}
Hannes Rapp's avatar
Hannes Rapp committed
219
220
}

221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
void sparc_emit_float_load_store_mode(const ir_node *node)
{
	const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
	ir_mode *mode = attr->load_store_mode;
	int      bits = get_mode_size_bits(mode);

	assert(mode_is_float(mode));

	switch (bits) {
	case 32:  return;
	case 64:  be_emit_char('d'); return;
	case 128: be_emit_char('q'); return;
	}
	panic("invalid flaot load/store mode %+F", mode);
}
236

Hannes Rapp's avatar
Hannes Rapp committed
237
238
239
240
241
242
/**
 *  Emit load mode char
 */
void sparc_emit_load_mode(const ir_node *node)
{
	const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
243
244
245
246
247
248
249
250
251
	ir_mode *mode      = attr->load_store_mode;
	int      bits      = get_mode_size_bits(mode);
	bool     is_signed = mode_is_signed(mode);

	if (bits == 16) {
		be_emit_string(is_signed ? "sh" : "uh");
	} else if (bits == 8) {
		be_emit_string(is_signed ? "sb" : "ub");
	} else if (bits == 64) {
252
		be_emit_char('d');
253
254
255
	} else {
		assert(bits == 32);
	}
Hannes Rapp's avatar
Hannes Rapp committed
256
257
258
259
260
261
262
263
}

/**
 * Emit store mode char
 */
void sparc_emit_store_mode(const ir_node *node)
{
	const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
264
265
266
267
268
269
270
271
	ir_mode *mode      = attr->load_store_mode;
	int      bits      = get_mode_size_bits(mode);

	if (bits == 16) {
		be_emit_string("h");
	} else if (bits == 8) {
		be_emit_string("b");
	} else if (bits == 64) {
272
		be_emit_char('d');
273
274
275
	} else {
		assert(bits == 32);
	}
Hannes Rapp's avatar
Hannes Rapp committed
276
277
}

Hannes Rapp's avatar
Hannes Rapp committed
278
279
280
281
282
/**
 * emit integer signed/unsigned prefix char
 */
void sparc_emit_mode_sign_prefix(const ir_node *node)
{
283
284
285
	ir_mode *mode      = get_irn_mode(node);
	bool     is_signed = mode_is_signed(mode);
	be_emit_string(is_signed ? "s" : "u");
Hannes Rapp's avatar
Hannes Rapp committed
286
287
}

288
static void emit_fp_suffix(const ir_mode *mode)
Hannes Rapp's avatar
Hannes Rapp committed
289
{
290
	unsigned bits = get_mode_size_bits(mode);
291
	assert(mode_is_float(mode));
Hannes Rapp's avatar
Hannes Rapp committed
292

293
	if (bits == 32) {
294
		be_emit_char('s');
295
	} else if (bits == 64) {
296
297
298
		be_emit_char('d');
	} else if (bits == 128) {
		be_emit_char('q');
299
	} else {
300
		panic("invalid FP mode");
301
	}
Hannes Rapp's avatar
Hannes Rapp committed
302
303
}

304
void sparc_emit_fp_conv_source(const ir_node *node)
Hannes Rapp's avatar
Hannes Rapp committed
305
{
306
307
308
	const sparc_fp_conv_attr_t *attr = get_sparc_fp_conv_attr_const(node);
	emit_fp_suffix(attr->src_mode);
}
Hannes Rapp's avatar
Hannes Rapp committed
309

310
311
312
313
void sparc_emit_fp_conv_destination(const ir_node *node)
{
	const sparc_fp_conv_attr_t *attr = get_sparc_fp_conv_attr_const(node);
	emit_fp_suffix(attr->dest_mode);
Hannes Rapp's avatar
Hannes Rapp committed
314
315
316
317
318
319
320
}

/**
 * emits the FP mode suffix char
 */
void sparc_emit_fp_mode_suffix(const ir_node *node)
{
321
322
	const sparc_fp_attr_t *attr = get_sparc_fp_attr_const(node);
	emit_fp_suffix(attr->fp_mode);
Hannes Rapp's avatar
Hannes Rapp committed
323
324
}

Hannes Rapp's avatar
Hannes Rapp committed
325
326
327
328
329
330
/**
 * Returns the target label for a control flow node.
 */
static void sparc_emit_cfop_target(const ir_node *node)
{
	ir_node *block = get_irn_link(node);
331
	be_gas_emit_block_name(block);
Hannes Rapp's avatar
Hannes Rapp committed
332
333
334
335
336
337
338
}

/**
 * Emit single entity
 */
static void sparc_emit_entity(ir_entity *entity)
{
339
	be_gas_emit_entity(entity);
Hannes Rapp's avatar
Hannes Rapp committed
340
341
342
343
344
345
346
347
348
}

/**
 * Emits code for stack space management
 */
static void emit_be_IncSP(const ir_node *irn)
{
	int offs = -be_get_IncSP_offset(irn);

349
350
	if (offs == 0)
			return;
Hannes Rapp's avatar
Hannes Rapp committed
351

352
353
354
355
	/* SPARC stack grows downwards */
	if (offs < 0) {
		be_emit_cstring("\tsub ");
		offs = -offs;
Hannes Rapp's avatar
Hannes Rapp committed
356
	} else {
357
		be_emit_cstring("\tadd ");
Hannes Rapp's avatar
Hannes Rapp committed
358
359
	}

360
361
362
363
364
365
	sparc_emit_source_register(irn, 0);
	be_emit_irprintf(", %d", offs);
	be_emit_cstring(", ");
	sparc_emit_dest_register(irn, 0);
	be_emit_finish_line_gas(irn);
}
366

367
/**
Hannes Rapp's avatar
Hannes Rapp committed
368
 * emits code for save instruction with min. required stack space
369
370
371
 */
static void emit_sparc_Save(const ir_node *irn)
{
372
	const sparc_save_attr_t *save_attr = get_sparc_save_attr_const(irn);
373
374
375
376
377
	be_emit_cstring("\tsave ");
	sparc_emit_source_register(irn, 0);
	be_emit_irprintf(", %d, ", -save_attr->initial_stacksize);
	sparc_emit_dest_register(irn, 0);
	be_emit_finish_line_gas(irn);
Hannes Rapp's avatar
Hannes Rapp committed
378
379
}

Hannes Rapp's avatar
Hannes Rapp committed
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/**
 * emits code for mulh
 */
static void emit_sparc_Mulh(const ir_node *irn)
{
	be_emit_cstring("\t");
	sparc_emit_mode_sign_prefix(irn);
	be_emit_cstring("mul ");

	sparc_emit_source_register(irn, 0);
	be_emit_cstring(", ");
	sparc_emit_reg_or_imm(irn, 1);
	be_emit_cstring(", ");
	sparc_emit_dest_register(irn, 0);
	be_emit_finish_line_gas(irn);

	// our result is in the y register now
	// we just copy it to the assigned target reg
398
	be_emit_cstring("\tmov %y, ");
Hannes Rapp's avatar
Hannes Rapp committed
399
400
401
	sparc_emit_dest_register(irn, 0);
	be_emit_finish_line_gas(irn);
}
402

403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
static void emit_sparc_Div(const ir_node *node, bool is_signed)
{
	/* can we get the delay count of the wr instruction somewhere? */
	unsigned wry_delay_count = 3;
	unsigned i;

	be_emit_cstring("\twr ");
	sparc_emit_source_register(node, 0);
	be_emit_cstring(", 0, %y");
	be_emit_finish_line_gas(node);

	for (i = 0; i < wry_delay_count; ++i) {
		be_emit_cstring("\tnop");
		be_emit_finish_line_gas(node);
	}

	be_emit_irprintf("\t%s ", is_signed ? "sdiv" : "udiv");
	sparc_emit_source_register(node, 1);
	be_emit_cstring(", ");
422
	sparc_emit_reg_or_imm(node, 2);
423
424
425
426
427
428
429
	be_emit_cstring(", ");
	sparc_emit_dest_register(node, 0);
	be_emit_finish_line_gas(node);
}

static void emit_sparc_SDiv(const ir_node *node)
{
430
	emit_sparc_Div(node, true);
431
432
433
434
435
436
437
}

static void emit_sparc_UDiv(const ir_node *node)
{
	emit_sparc_Div(node, false);
}

Hannes Rapp's avatar
Hannes Rapp committed
438
439
440
441
442
443
/**
 * Emits code for return node
 */
static void emit_be_Return(const ir_node *irn)
{
	be_emit_cstring("\tret");
444
	//be_emit_cstring("\tjmp %i7+8");
Hannes Rapp's avatar
Hannes Rapp committed
445
	be_emit_finish_line_gas(irn);
446
447
	be_emit_cstring("\trestore");
	be_emit_finish_line_gas(irn);
Hannes Rapp's avatar
Hannes Rapp committed
448
449
450
451
452
}

/**
 * Emits code for Call node
 */
453
static void emit_sparc_Call(const ir_node *node)
Hannes Rapp's avatar
Hannes Rapp committed
454
{
455
456
	const sparc_attr_t *attr   = get_sparc_attr_const(node);
	ir_entity          *entity = attr->immediate_value_entity;
Hannes Rapp's avatar
Hannes Rapp committed
457

458
	be_emit_cstring("\tcall ");
Hannes Rapp's avatar
Hannes Rapp committed
459
460
	if (entity != NULL) {
	    sparc_emit_entity(entity);
461
462
463
	    if (attr->immediate_value != 0) {
			be_emit_irprintf("%+d", attr->immediate_value);
		}
464
		be_emit_cstring(", 0");
Hannes Rapp's avatar
Hannes Rapp committed
465
	} else {
466
467
		int last = get_irn_arity(node);
		sparc_emit_source_register(node, last-1);
Hannes Rapp's avatar
Hannes Rapp committed
468
	}
469
470
471
472
473
	be_emit_finish_line_gas(node);

	/* fill delay slot */
	be_emit_cstring("\tnop");
	be_emit_finish_line_gas(node);
Hannes Rapp's avatar
Hannes Rapp committed
474
475
}

476
/**
Hannes Rapp's avatar
Hannes Rapp committed
477
 * Emit code for Perm node
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
 */
static void emit_be_Perm(const ir_node *irn)
{
	be_emit_cstring("\txor ");
	sparc_emit_source_register(irn, 1);
	be_emit_cstring(", ");
	sparc_emit_source_register(irn, 0);
	be_emit_cstring(", ");
	sparc_emit_source_register(irn, 0);
	be_emit_finish_line_gas(NULL);

	be_emit_cstring("\txor ");
	sparc_emit_source_register(irn, 1);
	be_emit_cstring(", ");
	sparc_emit_source_register(irn, 0);
	be_emit_cstring(", ");
	sparc_emit_source_register(irn, 1);
	be_emit_finish_line_gas(NULL);

	be_emit_cstring("\txor ");
	sparc_emit_source_register(irn, 1);
	be_emit_cstring(", ");
	sparc_emit_source_register(irn, 0);
	be_emit_cstring(", ");
	sparc_emit_source_register(irn, 0);
	be_emit_finish_line_gas(irn);
}

Hannes Rapp's avatar
Hannes Rapp committed
506
507
508
509
510
511
512
513
/**
 * TODO: not really tested but seems to work with memperm_arity == 1
 */
static void emit_be_MemPerm(const ir_node *node)
{
	int i;
	int memperm_arity;
	int sp_change = 0;
514
515
516
517
518
	ir_graph          *irg    = get_irn_irg(node);
	be_stack_layout_t *layout = be_get_irg_stack_layout(irg);

	/* this implementation only works with frame pointers currently */
	assert(layout->sp_relative == false);
Hannes Rapp's avatar
Hannes Rapp committed
519
520
521
522
523
524
525
526
527

	/* TODO: this implementation is slower than necessary.
	   The longterm goal is however to avoid the memperm node completely */

	memperm_arity = be_get_MemPerm_entity_arity(node);
	// we use our local registers - so this is limited to 8 inputs !
	if (memperm_arity > 8)
		panic("memperm with more than 8 inputs not supported yet");

528
529
530
	be_emit_irprintf("\tsub %%sp, %d, %%sp", memperm_arity*4);
	be_emit_finish_line_gas(node);

Hannes Rapp's avatar
Hannes Rapp committed
531
532
	for (i = 0; i < memperm_arity; ++i) {
		ir_entity *entity = be_get_MemPerm_in_entity(node, i);
533
		int        offset = be_get_stack_entity_offset(layout, entity, 0);
Hannes Rapp's avatar
Hannes Rapp committed
534
535

		/* spill register */
536
		be_emit_irprintf("\tst %%l%d, [%%sp%+d]", i, sp_change + SPARC_MIN_STACKSIZE);
Hannes Rapp's avatar
Hannes Rapp committed
537
538
539
		be_emit_finish_line_gas(node);

		/* load from entity */
540
		be_emit_irprintf("\tld [%%fp%+d], %%l%d", offset, i);
Hannes Rapp's avatar
Hannes Rapp committed
541
		be_emit_finish_line_gas(node);
542
		sp_change += 4;
Hannes Rapp's avatar
Hannes Rapp committed
543
544
545
546
	}

	for (i = memperm_arity-1; i >= 0; --i) {
		ir_entity *entity = be_get_MemPerm_out_entity(node, i);
547
548
549
		int        offset = be_get_stack_entity_offset(layout, entity, 0);

		sp_change -= 4;
Hannes Rapp's avatar
Hannes Rapp committed
550
551

		/* store to new entity */
552
		be_emit_irprintf("\tst %%l%d, [%%fp%+d]", i, offset);
Hannes Rapp's avatar
Hannes Rapp committed
553
554
		be_emit_finish_line_gas(node);
		/* restore register */
555
		be_emit_irprintf("\tld [%%sp%+d], %%l%d", sp_change + SPARC_MIN_STACKSIZE, i);
Hannes Rapp's avatar
Hannes Rapp committed
556
557
		be_emit_finish_line_gas(node);
	}
558
559
560
561

	be_emit_irprintf("\tadd %%sp, %d, %%sp", memperm_arity*4);
	be_emit_finish_line_gas(node);

Hannes Rapp's avatar
Hannes Rapp committed
562
563
564
	assert(sp_change == 0);
}

Hannes Rapp's avatar
Hannes Rapp committed
565
566
567
/**
 * Emits code for FrameAddr fix
 */
568
static void emit_sparc_FrameAddr(const ir_node *node)
Hannes Rapp's avatar
Hannes Rapp committed
569
{
570
	const sparc_attr_t *attr = get_sparc_attr_const(node);
571
572

	// no need to fix offset as we are adressing via the framepointer
573
	if (attr->immediate_value >= 0) {
574
		be_emit_cstring("\tadd ");
575
		sparc_emit_source_register(node, 0);
576
		be_emit_cstring(", ");
577
		be_emit_irprintf("%ld", attr->immediate_value);
578
579
	} else {
		be_emit_cstring("\tsub ");
580
		sparc_emit_source_register(node, 0);
581
		be_emit_cstring(", ");
582
		be_emit_irprintf("%ld", -attr->immediate_value);
583
584
	}

Hannes Rapp's avatar
Hannes Rapp committed
585
	be_emit_cstring(", ");
586
587
	sparc_emit_dest_register(node, 0);
	be_emit_finish_line_gas(node);
Hannes Rapp's avatar
Hannes Rapp committed
588
589
}

590
591
592
593
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
static const char *get_icc_unsigned(pn_Cmp pnc)
{
	switch (pnc) {
	case pn_Cmp_False: return "bn";
	case pn_Cmp_Eq:    return "be";
	case pn_Cmp_Lt:    return "blu";
	case pn_Cmp_Le:    return "bleu";
	case pn_Cmp_Gt:    return "bgu";
	case pn_Cmp_Ge:    return "bgeu";
	case pn_Cmp_Lg:    return "bne";
	case pn_Cmp_Leg:   return "ba";
	default: panic("Cmp has unsupported pnc");
	}
}

static const char *get_icc_signed(pn_Cmp pnc)
{
	switch (pnc) {
	case pn_Cmp_False: return "bn";
	case pn_Cmp_Eq:    return "be";
	case pn_Cmp_Lt:    return "bl";
	case pn_Cmp_Le:    return "ble";
	case pn_Cmp_Gt:    return "bg";
	case pn_Cmp_Ge:    return "bge";
	case pn_Cmp_Lg:    return "bne";
	case pn_Cmp_Leg:   return "ba";
	default: panic("Cmp has unsupported pnc");
	}
}

static const char *get_fcc(pn_Cmp pnc)
{
	switch (pnc) {
	case pn_Cmp_False: return "fbn";
	case pn_Cmp_Eq:    return "fbe";
	case pn_Cmp_Lt:    return "fbl";
	case pn_Cmp_Le:    return "fble";
	case pn_Cmp_Gt:    return "fbg";
	case pn_Cmp_Ge:    return "fbge";
	case pn_Cmp_Lg:    return "fblg";
	case pn_Cmp_Leg:   return "fbo";
	case pn_Cmp_Uo:    return "fbu";
	case pn_Cmp_Ue:    return "fbue";
	case pn_Cmp_Ul:    return "fbul";
	case pn_Cmp_Ule:   return "fbule";
	case pn_Cmp_Ug:    return "fbug";
	case pn_Cmp_Uge:   return "fbuge";
	case pn_Cmp_Ne:    return "fbne";
	case pn_Cmp_True:  return "fba";
	case pn_Cmp_max:
		break;
	}
	panic("invalid pnc");
}

typedef const char* (*get_cc_func)(pn_Cmp pnc);
Hannes Rapp's avatar
Hannes Rapp committed
646
647
648
649

/**
 * Emits code for Branch
 */
650
static void emit_sparc_branch(const ir_node *node, get_cc_func get_cc)
Hannes Rapp's avatar
Hannes Rapp committed
651
{
652
	const sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr_const(node);
653
	pn_Cmp           pnc         = attr->pnc;
654
655
	const ir_node   *proj_true   = NULL;
	const ir_node   *proj_false  = NULL;
656
	const ir_edge_t *edge;
657
658
659
660
	const ir_node   *block;
	const ir_node   *next_block;

	foreach_out_edge(node, edge) {
661
662
663
664
665
666
667
668
669
670
		ir_node *proj = get_edge_src_irn(edge);
		long nr = get_Proj_proj(proj);
		if (nr == pn_Cond_true) {
			proj_true = proj;
		} else {
			proj_false = proj;
		}
	}

	/* for now, the code works for scheduled and non-schedules blocks */
671
	block = get_nodes_block(node);
672
673
674
675
676
677
678
679
680
681

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

	if (get_irn_link(proj_true) == next_block) {
		/* exchange both proj's so the second one can be omitted */
		const ir_node *t = proj_true;

		proj_true  = proj_false;
		proj_false = t;
682
683
684
685
		if (is_sparc_fbfcc(node)) {
			pnc = get_negated_pnc(pnc, mode_F);
		} else {
			pnc = get_negated_pnc(pnc, mode_Iu);
686
		}
687
688
689
	}

	/* emit the true proj */
690
691
	be_emit_cstring("\t");
	be_emit_string(get_cc(pnc));
692
	be_emit_char(' ');
693
694
695
696
697
698
699
700
	sparc_emit_cfop_target(proj_true);
	be_emit_finish_line_gas(proj_true);

	be_emit_cstring("\tnop");
	be_emit_pad_comment();
	be_emit_cstring("/* TODO: use delay slot */\n");

	if (get_irn_link(proj_false) == next_block) {
701
		be_emit_cstring("\t/* fallthrough to ");
702
703
704
705
706
707
708
		sparc_emit_cfop_target(proj_false);
		be_emit_cstring(" */");
		be_emit_finish_line_gas(proj_false);
	} else {
		be_emit_cstring("\tba ");
		sparc_emit_cfop_target(proj_false);
		be_emit_finish_line_gas(proj_false);
709
710
		be_emit_cstring("\tnop\t\t/* TODO: use delay slot */\n");
		be_emit_finish_line_gas(proj_false);
711
	}
Hannes Rapp's avatar
Hannes Rapp committed
712
713
}

714
715
716
717
718
719
720
721
722
723
724
725
static void emit_sparc_Bicc(const ir_node *node)
{
	const sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr_const(node);
	bool             is_unsigned = attr->is_unsigned;
	emit_sparc_branch(node, is_unsigned ? get_icc_unsigned : get_icc_signed);
}

static void emit_sparc_fbfcc(const ir_node *node)
{
	emit_sparc_branch(node, get_fcc);
}

726
/**
727
 * emit Jmp (which actually is a branch always (ba) instruction)
728
 */
729
static void emit_sparc_Ba(const ir_node *node)
730
731
732
733
734
735
736
737
738
739
740
{
	ir_node *block, *next_block;

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

	/* we have a block schedule */
	next_block = get_irn_link(block);
	if (get_irn_link(node) != next_block) {
		be_emit_cstring("\tba ");
		sparc_emit_cfop_target(node);
741
742
		be_emit_finish_line_gas(node);
		be_emit_cstring("\tnop\t\t/* TODO: use delay slot */\n");
743
744
745
746
747
748
749
750
	} else {
		be_emit_cstring("\t/* fallthrough to ");
		sparc_emit_cfop_target(node);
		be_emit_cstring(" */");
	}
	be_emit_finish_line_gas(node);
}

751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
static void emit_fmov(const ir_node *node, const arch_register_t *src_reg,
                      const arch_register_t *dst_reg)
{
	be_emit_cstring("\tfmov ");
	be_emit_string(arch_register_get_name(src_reg));
	be_emit_cstring(", ");
	be_emit_string(arch_register_get_name(dst_reg));
	be_emit_finish_line_gas(node);
}

static const arch_register_t *get_next_fp_reg(const arch_register_t *reg)
{
	unsigned index = reg->index;
	assert(reg == &sparc_fp_regs[index]);
	index++;
	assert(index < N_sparc_fp_REGS);
	return &sparc_fp_regs[index];
}

770
771
772
/**
 * emit copy node
 */
773
static void emit_be_Copy(const ir_node *node)
774
{
775
776
777
	ir_mode               *mode    = get_irn_mode(node);
	const arch_register_t *src_reg = get_in_reg(node, 0);
	const arch_register_t *dst_reg = get_out_reg(node, 0);
778

779
	if (src_reg == dst_reg)
780
781
782
		return;

	if (mode_is_float(mode)) {
783
784
785
786
787
788
789
790
791
		unsigned bits = get_mode_size_bits(mode);
		int      n    = bits > 32 ? bits > 64 ? 3 : 1 : 0;
		int      i;
		emit_fmov(node, src_reg, dst_reg);
		for (i = 0; i < n; ++i) {
			src_reg = get_next_fp_reg(src_reg);
			dst_reg = get_next_fp_reg(dst_reg);
			emit_fmov(node, src_reg, dst_reg);
		}
792
793
	} else if (mode_is_data(mode)) {
		be_emit_cstring("\tmov ");
794
		sparc_emit_source_register(node, 0);
795
		be_emit_cstring(", ");
796
797
		sparc_emit_dest_register(node, 0);
		be_emit_finish_line_gas(node);
798
	} else {
799
		panic("emit_be_Copy: invalid mode");
800
801
802
803
	}
}


Hannes Rapp's avatar
Hannes Rapp committed
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
/**
 * dummy emitter for ignored nodes
 */
static void emit_nothing(const ir_node *irn)
{
	(void) irn;
}

/**
 * type of emitter function
 */
typedef void (*emit_func) (const ir_node *);

/**
 * Set a node emitter. Make it a bit more type safe.
 */
static inline void set_emitter(ir_op *op, emit_func sparc_emit_node)
{
	op->ops.generic = (op_func)sparc_emit_node;
}

/**
 * Enters the emitter functions for handled nodes into the generic
 * pointer of an opcode.
 */
static void sparc_register_emitters(void)
{
	/* first clear the generic function pointer for all ops */
	clear_irp_opcodes_generic_func();
	/* register all emitter functions defined in spec */
	sparc_register_spec_emitters();

	/* custom emitter */
837
838
839
840
841
842
	set_emitter(op_be_Copy,         emit_be_Copy);
	set_emitter(op_be_CopyKeep,     emit_be_Copy);
	set_emitter(op_be_IncSP,        emit_be_IncSP);
	set_emitter(op_be_MemPerm,      emit_be_MemPerm);
	set_emitter(op_be_Perm,         emit_be_Perm);
	set_emitter(op_be_Return,       emit_be_Return);
843
	set_emitter(op_sparc_Ba,        emit_sparc_Ba);
844
	set_emitter(op_sparc_Bicc,      emit_sparc_Bicc);
845
	set_emitter(op_sparc_Call,      emit_sparc_Call);
846
	set_emitter(op_sparc_fbfcc,     emit_sparc_fbfcc);
847
848
849
	set_emitter(op_sparc_FrameAddr, emit_sparc_FrameAddr);
	set_emitter(op_sparc_Mulh,      emit_sparc_Mulh);
	set_emitter(op_sparc_Save,      emit_sparc_Save);
850
851
	set_emitter(op_sparc_SDiv,      emit_sparc_SDiv);
	set_emitter(op_sparc_UDiv,      emit_sparc_UDiv);
852
853
854
855
856
857

	/* no need to emit anything for the following nodes */
	set_emitter(op_be_Barrier, emit_nothing);
	set_emitter(op_be_Keep,    emit_nothing);
	set_emitter(op_be_Start,   emit_nothing);
	set_emitter(op_Phi,        emit_nothing);
Hannes Rapp's avatar
Hannes Rapp committed
858
859
860
861
862
}

/**
 * Emits code for a node.
 */
863
static void sparc_emit_node(const ir_node *node)
Hannes Rapp's avatar
Hannes Rapp committed
864
{
865
	ir_op *op = get_irn_op(node);
Hannes Rapp's avatar
Hannes Rapp committed
866
867
868
869
870
871

	if (op->ops.generic) {
		emit_func func = (emit_func) op->ops.generic;
		be_dbg_set_dbg_info(get_irn_dbg_info(node));
		(*func) (node);
	} else {
872
873
		panic("No emit handler for node %+F (graph %+F)\n",	node,
		      current_ir_graph);
Hannes Rapp's avatar
Hannes Rapp committed
874
875
876
877
878
879
880
	}
}

/**
 * Walks over the nodes in a block connected by scheduling edges
 * and emits code for each node.
 */
881
static void sparc_gen_block(ir_node *block, void *data)
882
{
Hannes Rapp's avatar
Hannes Rapp committed
883
884
885
886
887
	ir_node *node;
	(void) data;

	if (! is_Block(block))
		return;
888
889
890

	be_gas_emit_block_name(block);
	be_emit_cstring(":\n");
Hannes Rapp's avatar
Hannes Rapp committed
891
	be_emit_write_line();
892

Hannes Rapp's avatar
Hannes Rapp committed
893
894
895
896
897
898
899
900
901
	sched_foreach(block, node) {
		sparc_emit_node(node);
	}
}


/**
 * Emits code for function start.
 */
902
static void sparc_emit_func_prolog(ir_graph *irg)
903
{
Hannes Rapp's avatar
Hannes Rapp committed
904
	ir_entity *ent = get_irg_entity(irg);
905
	be_gas_emit_function_prolog(ent, 4);
Hannes Rapp's avatar
Hannes Rapp committed
906
907
908
909
910
911
	be_emit_write_line();
}

/**
 * Emits code for function end
 */
912
static void sparc_emit_func_epilog(ir_graph *irg)
913
{
Hannes Rapp's avatar
Hannes Rapp committed
914
915
	ir_entity *ent = get_irg_entity(irg);
	const char *irg_name = get_entity_ld_name(ent);
916
	be_emit_write_line();
Hannes Rapp's avatar
Hannes Rapp committed
917
918
919
920
921
922
923
924
	be_emit_irprintf("\t.size  %s, .-%s\n", irg_name, irg_name);
	be_emit_cstring("# -- End ");
	be_emit_string(irg_name);
	be_emit_cstring("\n");
	be_emit_write_line();
}

/**
925
926
927
 * Block-walker:
 * TODO: Sets labels for control flow nodes (jump target).
 * Links control predecessors to there destination blocks.
Hannes Rapp's avatar
Hannes Rapp committed
928
 */
929
static void sparc_gen_labels(ir_node *block, void *env)
930
{
Hannes Rapp's avatar
Hannes Rapp committed
931
932
933
934
935
936
	ir_node *pred;
	int n = get_Block_n_cfgpreds(block);
	(void) env;

	for (n--; n >= 0; n--) {
		pred = get_Block_cfgpred(block, n);
937
		set_irn_link(pred, block); // link the pred of a block (which is a jmp)
Hannes Rapp's avatar
Hannes Rapp committed
938
939
940
941
942
943
944
945
946
	}
}


/**
 * Main driver
 */
void sparc_gen_routine(const sparc_code_gen_t *cg, ir_graph *irg)
{
947
948
	ir_node **blk_sched;
	ir_node *last_block = NULL;
Hannes Rapp's avatar
Hannes Rapp committed
949
	ir_entity *entity     = get_irg_entity(irg);
950
	int i, n;
951
	(void) cg;
Hannes Rapp's avatar
Hannes Rapp committed
952

953
	be_gas_elf_type_char = '#';
954
	be_gas_object_file_format = OBJECT_FILE_FORMAT_ELF_SPARC;
955

Hannes Rapp's avatar
Hannes Rapp committed
956
957
	/* register all emitter functions */
	sparc_register_emitters();
958
	be_dbg_method_begin(entity);
959
960

	/* create the block schedule. For now, we don't need it earlier. */
961
	blk_sched = be_create_block_schedule(irg);
962
963

	// emit function prolog
Hannes Rapp's avatar
Hannes Rapp committed
964
	sparc_emit_func_prolog(irg);
965
966

	// generate BLOCK labels
Hannes Rapp's avatar
Hannes Rapp committed
967
968
	irg_block_walk_graph(irg, sparc_gen_labels, NULL, NULL);

969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
	// inject block scheduling links & emit code of each block
	n = ARR_LEN(blk_sched);
	for (i = 0; i < n;) {
		ir_node *block, *next_bl;

		block = blk_sched[i];
		++i;
		next_bl = i < n ? blk_sched[i] : NULL;

		/* set here the link. the emitter expects to find the next block here */
		set_irn_link(block, next_bl);
		sparc_gen_block(block, last_block);
		last_block = block;
	}

	// emit function epilog
	sparc_emit_func_epilog(irg);
Hannes Rapp's avatar
Hannes Rapp committed
986
987
988
989
990
991
}

void sparc_init_emitter(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.be.sparc.emit");
}