sparc_emitter.c 21.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   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

#define SNPRINTF_BUF_LEN 128
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

60
61
62
63
64
65
66
/**
 * attribute of SAVE node which follows immediatelly after the START node
 * we need this to correct all offsets since SPARC expects
 * some reserved stack space after the stackpointer
 */
const sparc_save_attr_t *save_attr;

Hannes Rapp's avatar
Hannes Rapp committed
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
112
113
114
115
116
117
118
119
120
121
122
/**
 * 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;
}

void sparc_emit_immediate(const ir_node *node)
{
123
124
125
	int const val = get_sparc_attr_const(node)->immediate_value;
	assert(-4096 <= val && val < 4096);
	be_emit_irprintf("%d", val);
Hannes Rapp's avatar
Hannes Rapp committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
}

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);
	}
}

158
159
160
/**
 * emit SP offset
 */
Hannes Rapp's avatar
Hannes Rapp committed
161
162
void sparc_emit_offset(const ir_node *node)
{
163
164
	const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
	assert(attr->base.is_load_store);
165

166
	if (attr->offset > 0)
167
		be_emit_irprintf("+%ld", attr->offset);
Hannes Rapp's avatar
Hannes Rapp committed
168
169
}

170

Hannes Rapp's avatar
Hannes Rapp committed
171
172
173
174
175
176
/**
 *  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);
177
178
179
180
181
182
183
184
185
186
187
188
189
	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) {
		be_emit_string("d");
	} else {
		assert(bits == 32);
	}
Hannes Rapp's avatar
Hannes Rapp committed
190
191
192
193
194
195
196
197
}

/**
 * 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);
198
199
200
201
202
203
204
205
206
207
208
209
	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) {
		be_emit_string("d");
	} else {
		assert(bits == 32);
	}
Hannes Rapp's avatar
Hannes Rapp committed
210
211
}

Hannes Rapp's avatar
Hannes Rapp committed
212
213
214
215
216
/**
 * emit integer signed/unsigned prefix char
 */
void sparc_emit_mode_sign_prefix(const ir_node *node)
{
217
218
219
	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
220
221
222
223
224
225
226
227
}

/**
 * emit FP load mode char
 */
void sparc_emit_fp_load_mode(const ir_node *node)
{
	const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
228
229
	ir_mode *mode      = attr->load_store_mode;
	int      bits      = get_mode_size_bits(mode);
Hannes Rapp's avatar
Hannes Rapp committed
230

231
	assert(mode_is_float(mode));
Hannes Rapp's avatar
Hannes Rapp committed
232

233
234
235
236
237
	if (bits == 32) {
		be_emit_string("f");
	} else if (bits == 64) {
		be_emit_string("df");
	} else {
238
		panic("FP load mode > 64bits not implemented yet");
239
	}
Hannes Rapp's avatar
Hannes Rapp committed
240
241
242
243
244
245
246
247
}

/**
 * emit FP store mode char
 */
void sparc_emit_fp_store_mode(const ir_node *node)
{
	const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
248
249
	ir_mode *mode      = attr->load_store_mode;
	int      bits      = get_mode_size_bits(mode);
Hannes Rapp's avatar
Hannes Rapp committed
250

251
	assert(mode_is_float(mode));
Hannes Rapp's avatar
Hannes Rapp committed
252

253
254
255
256
257
	if (bits == 32) {
		be_emit_string("f");
	} else if (bits == 64) {
		be_emit_string("df");
	} else {
258
		panic("FP store mode > 64bits not implemented yet");
259
	}
Hannes Rapp's avatar
Hannes Rapp committed
260
261
262
263
264
265
266
}

/**
 * emits the FP mode suffix char
 */
void sparc_emit_fp_mode_suffix(const ir_node *node)
{
267
268
	ir_mode *mode      = get_irn_mode(node);
	int      bits      = get_mode_size_bits(mode);
Hannes Rapp's avatar
Hannes Rapp committed
269

270
	assert(mode_is_float(mode));
Hannes Rapp's avatar
Hannes Rapp committed
271

272
	if (bits == 32) {
273
		be_emit_string("s");
274
	} else if (bits == 64) {
275
		be_emit_string("d");
276
	} else {
Hannes Rapp's avatar
Hannes Rapp committed
277
		panic("FP mode > 64bits not implemented yet");
278
	}
Hannes Rapp's avatar
Hannes Rapp committed
279
280
}

Hannes Rapp's avatar
Hannes Rapp committed
281
282
283
284
285
286
/**
 * 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);
287
	be_gas_emit_block_name(block);
Hannes Rapp's avatar
Hannes Rapp committed
288
289
290
291
292
293
294
}

/**
 * Emit single entity
 */
static void sparc_emit_entity(ir_entity *entity)
{
295
	be_gas_emit_entity(entity);
Hannes Rapp's avatar
Hannes Rapp committed
296
297
298
299
300
301
302
303
304
}

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

305
306
	if (offs == 0)
			return;
Hannes Rapp's avatar
Hannes Rapp committed
307

308
309
310
311
	/* SPARC stack grows downwards */
	if (offs < 0) {
		be_emit_cstring("\tsub ");
		offs = -offs;
Hannes Rapp's avatar
Hannes Rapp committed
312
	} else {
313
		be_emit_cstring("\tadd ");
Hannes Rapp's avatar
Hannes Rapp committed
314
315
	}

316
317
318
319
320
321
	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);
}
322

323
/**
Hannes Rapp's avatar
Hannes Rapp committed
324
 * emits code for save instruction with min. required stack space
325
326
327
328
329
330
331
332
333
 */
static void emit_sparc_Save(const ir_node *irn)
{
	save_attr = get_sparc_save_attr_const(irn);
	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
334
335
}

336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
 * emits code to load hi 22 bit of a constant
 */
static void emit_sparc_HiImm(const ir_node *irn)
{
	const sparc_attr_t *attr = get_sparc_attr_const(irn);
	be_emit_cstring("\tsethi ");
	be_emit_irprintf("%%hi(%d), ", attr->immediate_value);
	sparc_emit_dest_register(irn, 0);
	be_emit_finish_line_gas(irn);
}

/**
 * emits code to load lo 10bits of a constant
 */
static void emit_sparc_LoImm(const ir_node *irn)
{
	const sparc_attr_t *attr = get_sparc_attr_const(irn);
	be_emit_cstring("\tor ");
	sparc_emit_source_register(irn, 0);
	be_emit_irprintf(", %%lo(%d), ", attr->immediate_value);
	sparc_emit_dest_register(irn, 0);
	be_emit_finish_line_gas(irn);
}

Hannes Rapp's avatar
Hannes Rapp committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
 * emit code for div with the correct sign prefix
 */
static void emit_sparc_Div(const ir_node *irn)
{
	be_emit_cstring("\t");
	sparc_emit_mode_sign_prefix(irn);
	be_emit_cstring("div ");

	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);
}

/**
 * emit code for mul with the correct sign prefix
 */
static void emit_sparc_Mul(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);
}

/**
 * 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
	be_emit_cstring("\tmov ");
	be_emit_char('%');
	be_emit_string(arch_register_get_name(&sparc_flags_regs[REG_Y]));
	be_emit_cstring(", ");
	sparc_emit_dest_register(irn, 0);
	be_emit_finish_line_gas(irn);
}
420

Hannes Rapp's avatar
Hannes Rapp committed
421
422
423
424
425
426
/**
 * Emits code for return node
 */
static void emit_be_Return(const ir_node *irn)
{
	be_emit_cstring("\tret");
427
	//be_emit_cstring("\tjmp %i7+8");
Hannes Rapp's avatar
Hannes Rapp committed
428
	be_emit_finish_line_gas(irn);
429
430
	be_emit_cstring("\trestore");
	be_emit_finish_line_gas(irn);
Hannes Rapp's avatar
Hannes Rapp committed
431
432
433
434
435
436
437
438
439
440
441
442
}

/**
 * Emits code for Call node
 */
static void emit_be_Call(const ir_node *irn)
{
	ir_entity *entity = be_Call_get_entity(irn);

	if (entity != NULL) {
		be_emit_cstring("\tcall ");
	    sparc_emit_entity(entity);
443
		be_emit_cstring(", 0");
Hannes Rapp's avatar
Hannes Rapp committed
444
		be_emit_finish_line_gas(irn);
445
446
447
		be_emit_cstring("\tnop");
		be_emit_pad_comment();
		be_emit_cstring("/* TODO: use delay slot */\n");
Hannes Rapp's avatar
Hannes Rapp committed
448
	} else {
449
450
451
		be_emit_cstring("\tnop\n");
		be_emit_pad_comment();
		be_emit_cstring("/* TODO: Entity == NULL */\n");
Hannes Rapp's avatar
Hannes Rapp committed
452
453
454
455
		be_emit_finish_line_gas(irn);
	}
}

456
/**
Hannes Rapp's avatar
Hannes Rapp committed
457
 * Emit code for Perm node
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
 */
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/**
 * 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;

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

	for (i = 0; i < memperm_arity; ++i) {
		int offset;
		ir_entity *entity = be_get_MemPerm_in_entity(node, i);

		/* spill register */
		sp_change += 4;
		be_emit_irprintf("\tst %%l%d, [%%sp-%d]", i, sp_change);
		be_emit_finish_line_gas(node);

		/* load from entity */
		offset = get_entity_offset(entity) + sp_change;
		be_emit_irprintf("\tld [%%sp+%d], %%l%d", offset, i);
		be_emit_finish_line_gas(node);
	}

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

		/* store to new entity */
		offset = get_entity_offset(entity) + sp_change;
		be_emit_irprintf("\tst %%l%d, [%%sp+%d]", i, offset);
		be_emit_finish_line_gas(node);
		/* restore register */
		be_emit_irprintf("\tld [%%sp-%d], %%l%d", sp_change, i);
		sp_change -= 4;
		be_emit_finish_line_gas(node);
	}
	assert(sp_change == 0);
}

Hannes Rapp's avatar
Hannes Rapp committed
534
535
536
537
538
539
540
/**
 * Emit a SymConst.
 */
static void emit_sparc_SymConst(const ir_node *irn)
{
	const sparc_symconst_attr_t *attr = get_sparc_symconst_attr_const(irn);

541
542
	//sethi %hi(const32),%reg
	//or    %reg,%lo(const32),%reg
Hannes Rapp's avatar
Hannes Rapp committed
543

544
545
546
	be_emit_cstring("\tsethi %hi(");
	be_gas_emit_entity(attr->entity);
	be_emit_cstring("), ");
547
548
	sparc_emit_dest_register(irn, 0);
	be_emit_cstring("\n ");
549
550

	// TODO: could be combined with the following load/store instruction
551
552
	be_emit_cstring("\tor ");
	sparc_emit_dest_register(irn, 0);
553
554
555
	be_emit_cstring(", %lo(");
	be_gas_emit_entity(attr->entity);
	be_emit_cstring("), ");
Hannes Rapp's avatar
Hannes Rapp committed
556
557
558
559
560
561
562
563
564
565
	sparc_emit_dest_register(irn, 0);
	be_emit_finish_line_gas(irn);
}

/**
 * Emits code for FrameAddr fix
 */
static void emit_sparc_FrameAddr(const ir_node *irn)
{
	const sparc_symconst_attr_t *attr = get_irn_generic_attr_const(irn);
566
567
568
569
570
571
572
573
574
575
576
577
578
579

	// no need to fix offset as we are adressing via the framepointer
	if (attr->fp_offset >= 0) {
		be_emit_cstring("\tadd ");
		sparc_emit_source_register(irn, 0);
		be_emit_cstring(", ");
		be_emit_irprintf("%ld", attr->fp_offset + save_attr->initial_stacksize);
	} else {
		be_emit_cstring("\tsub ");
		sparc_emit_source_register(irn, 0);
		be_emit_cstring(", ");
		be_emit_irprintf("%ld", -attr->fp_offset);
	}

Hannes Rapp's avatar
Hannes Rapp committed
580
	be_emit_cstring(", ");
581
	sparc_emit_dest_register(irn, 0);
Hannes Rapp's avatar
Hannes Rapp committed
582
583
584
585
586
587
588
	be_emit_finish_line_gas(irn);
}


/**
 * Emits code for Branch
 */
589
static void emit_sparc_BXX(const ir_node *irn)
Hannes Rapp's avatar
Hannes Rapp committed
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
	const ir_edge_t *edge;
	const ir_node *proj_true  = NULL;
	const ir_node *proj_false = NULL;
	const ir_node *block;
	const ir_node *next_block;
	ir_node *op1 = get_irn_n(irn, 0);
	const char *suffix;
	int proj_num = get_sparc_jmp_cond_proj_num(irn);
	const sparc_cmp_attr_t *cmp_attr = get_irn_generic_attr_const(op1);
	// bool is_signed = !cmp_attr->is_unsigned;

	assert(is_sparc_Cmp(op1) || is_sparc_Tst(op1));

	foreach_out_edge(irn, edge) {
		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;
		}
	}

	if (cmp_attr->ins_permuted) {
		proj_num = get_mirrored_pnc(proj_num);
	}

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

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

	assert(proj_num != pn_Cmp_False);
	assert(proj_num != pn_Cmp_True);

	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;
		proj_num   = get_negated_pnc(proj_num, mode_Iu);
	}

636

637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
	switch (proj_num) {
		case pn_Cmp_Eq:  suffix = "e"; break;
		case pn_Cmp_Lt:  suffix = "l"; break;
		case pn_Cmp_Le:  suffix = "le"; break;
		case pn_Cmp_Gt:  suffix = "g"; break;
		case pn_Cmp_Ge:  suffix = "ge"; break;
		case pn_Cmp_Lg:  suffix = "ne"; break;
		case pn_Cmp_Leg: suffix = "a"; break;
		default: panic("Cmp has unsupported pnc");
	}

	/* emit the true proj */
	be_emit_irprintf("\tb%s ", suffix);
	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) {
		be_emit_cstring("\t/* false-fallthrough to ");
		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);
666
667
		be_emit_cstring("\tnop\t\t/* TODO: use delay slot */\n");
		be_emit_finish_line_gas(proj_false);
668
	}
Hannes Rapp's avatar
Hannes Rapp committed
669
670
}

671
/**
672
 * emit Jmp (which actually is a branch always (ba) instruction)
673
 */
674
static void emit_sparc_Ba(const ir_node *node)
675
676
677
678
679
680
681
682
683
684
685
{
	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);
686
687
		be_emit_finish_line_gas(node);
		be_emit_cstring("\tnop\t\t/* TODO: use delay slot */\n");
688
689
690
691
692
693
694
695
	} else {
		be_emit_cstring("\t/* fallthrough to ");
		sparc_emit_cfop_target(node);
		be_emit_cstring(" */");
	}
	be_emit_finish_line_gas(node);
}

696
697
698
/**
 * emit copy node
 */
699
700
static void emit_be_Copy(const ir_node *irn)
{
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
	ir_mode *mode = get_irn_mode(irn);

	if (get_in_reg(irn, 0) == get_out_reg(irn, 0)) {
		/* omitted Copy */
		return;
	}

	if (mode_is_float(mode)) {
		panic("emit_be_Copy: move not supported for FP");
	} else if (mode_is_data(mode)) {
		be_emit_cstring("\tmov ");
		sparc_emit_source_register(irn, 0);
		be_emit_cstring(", ");
		sparc_emit_dest_register(irn, 0);
		be_emit_finish_line_gas(irn);
	} else {
		panic("emit_be_Copy: move not supported for this mode");
	}
}


Hannes Rapp's avatar
Hannes Rapp committed
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
/**
 * 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 */
757
758
759
760
761
762
763
	set_emitter(op_be_Call,         emit_be_Call);
	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);
764
	set_emitter(op_sparc_BXX,       emit_sparc_BXX);
765
766
767
	set_emitter(op_sparc_Div,       emit_sparc_Div);
	set_emitter(op_sparc_FrameAddr, emit_sparc_FrameAddr);
	set_emitter(op_sparc_HiImm,     emit_sparc_HiImm);
768
	set_emitter(op_sparc_Ba,        emit_sparc_Ba);
769
770
771
772
773
774
775
776
777
778
779
	set_emitter(op_sparc_LoImm,     emit_sparc_LoImm);
	set_emitter(op_sparc_Mul,       emit_sparc_Mul);
	set_emitter(op_sparc_Mulh,      emit_sparc_Mulh);
	set_emitter(op_sparc_Save,      emit_sparc_Save);
	set_emitter(op_sparc_SymConst,  emit_sparc_SymConst);

	/* 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
780
781
782
783
784
}

/**
 * Emits code for a node.
 */
785
static void sparc_emit_node(const ir_node *node)
Hannes Rapp's avatar
Hannes Rapp committed
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
{
	ir_op               *op       = get_irn_op(node);

	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 {
		panic("Error: No emit handler for node %+F (graph %+F)\n",
			node, current_ir_graph);
	}
}

/**
 * Walks over the nodes in a block connected by scheduling edges
 * and emits code for each node.
 */
803
static void sparc_gen_block(ir_node *block, void *data)
804
{
Hannes Rapp's avatar
Hannes Rapp committed
805
806
807
808
809
	ir_node *node;
	(void) data;

	if (! is_Block(block))
		return;
810
811
812

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

Hannes Rapp's avatar
Hannes Rapp committed
815
816
817
818
819
820
821
822
823
	sched_foreach(block, node) {
		sparc_emit_node(node);
	}
}


/**
 * Emits code for function start.
 */
824
static void sparc_emit_func_prolog(ir_graph *irg)
825
{
Hannes Rapp's avatar
Hannes Rapp committed
826
	ir_entity *ent = get_irg_entity(irg);
827
	be_gas_emit_function_prolog(ent, 4);
Hannes Rapp's avatar
Hannes Rapp committed
828
829
830
831
832
833
	be_emit_write_line();
}

/**
 * Emits code for function end
 */
834
static void sparc_emit_func_epilog(ir_graph *irg)
835
{
Hannes Rapp's avatar
Hannes Rapp committed
836
837
	ir_entity *ent = get_irg_entity(irg);
	const char *irg_name = get_entity_ld_name(ent);
838
	be_emit_write_line();
Hannes Rapp's avatar
Hannes Rapp committed
839
840
841
842
843
844
845
846
	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();
}

/**
847
848
849
 * 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
850
 */
851
static void sparc_gen_labels(ir_node *block, void *env)
852
{
Hannes Rapp's avatar
Hannes Rapp committed
853
854
855
856
857
858
	ir_node *pred;
	int n = get_Block_n_cfgpreds(block);
	(void) env;

	for (n--; n >= 0; n--) {
		pred = get_Block_cfgpred(block, n);
859
		set_irn_link(pred, block); // link the pred of a block (which is a jmp)
Hannes Rapp's avatar
Hannes Rapp committed
860
861
862
863
864
865
866
867
868
	}
}


/**
 * Main driver
 */
void sparc_gen_routine(const sparc_code_gen_t *cg, ir_graph *irg)
{
869
870
	ir_node **blk_sched;
	ir_node *last_block = NULL;
Hannes Rapp's avatar
Hannes Rapp committed
871
	ir_entity *entity     = get_irg_entity(irg);
872
	int i, n;
873
	(void) cg;
Hannes Rapp's avatar
Hannes Rapp committed
874

875
	be_gas_elf_type_char = '#';
876
	be_gas_object_file_format = OBJECT_FILE_FORMAT_ELF_SPARC;
877

Hannes Rapp's avatar
Hannes Rapp committed
878
879
	/* register all emitter functions */
	sparc_register_emitters();
880
	be_dbg_method_begin(entity);
881
882

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

	// emit function prolog
Hannes Rapp's avatar
Hannes Rapp committed
886
	sparc_emit_func_prolog(irg);
887
888

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

891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
	// 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
908
909
910
911
912
913
}

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