bearch_sparc.c 16.1 KB
Newer Older
Hannes Rapp's avatar
Hannes Rapp committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
 *
 * 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    The main sparc backend driver file.
 * @version  $Id: bearch_TEMPLATE.c 26673 2009-10-01 16:43:13Z matze $
 */

#include "config.h"

#include "lc_opts.h"
#include "lc_opts_enum.h"

#include "pseudo_irg.h"
#include "irgwalk.h"
#include "irprog.h"
#include "irprintf.h"
#include "ircons.h"
#include "irgmod.h"
#include "irgopt.h"
#include "iroptimize.h"
#include "lowering.h"
#include "error.h"

#include "bitset.h"
#include "debug.h"
#include "array_t.h"
#include "irtools.h"

#include "../bearch.h"
#include "../benode.h"
#include "../belower.h"
#include "../besched.h"
#include "be.h"
#include "../beabi.h"
#include "../bemachine.h"
#include "../beilpsched.h"
#include "../bemodule.h"
#include "../beirg.h"
#include "../bespillslots.h"
#include "../begnuas.h"
#include "../belistsched.h"
#include "../beflags.h"

#include "bearch_sparc_t.h"

#include "sparc_new_nodes.h"
#include "gen_sparc_regalloc_if.h"
#include "sparc_transform.h"
#include "sparc_emitter.h"
Hannes Rapp's avatar
Hannes Rapp committed
68
#include "sparc_map_regs.h"
Hannes Rapp's avatar
Hannes Rapp committed
69
70
71
72
73
74
75
76
77

DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

static arch_irn_class_t sparc_classify(const ir_node *irn)
{
	(void) irn;
	return 0;
}

Hannes Rapp's avatar
Hannes Rapp committed
78
static ir_entity *sparc_get_frame_entity(const ir_node *irn)
Hannes Rapp's avatar
Hannes Rapp committed
79
{
Hannes Rapp's avatar
Hannes Rapp committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
	const sparc_attr_t *attr = get_sparc_attr_const(irn);

	if (is_sparc_FrameAddr(irn)) {
		const sparc_symconst_attr_t *attr = get_irn_generic_attr_const(irn);
		return attr->entity;
	}

	if (attr->is_load_store) {
		const sparc_load_store_attr_t *load_store_attr = get_sparc_load_store_attr_const(irn);
		if (load_store_attr->is_frame_entity) {
			return load_store_attr->entity;
		}
	}

Hannes Rapp's avatar
Hannes Rapp committed
94
95
96
97
98
99
100
	return NULL;
}

static void sparc_set_frame_entity(ir_node *node, ir_entity *ent)
{
	(void) node;
	(void) ent;
Hannes Rapp's avatar
Hannes Rapp committed
101
	panic("sparc_set_frame_entity() called. This should not happen.");
Hannes Rapp's avatar
Hannes Rapp committed
102
103
104
105
106
107
108
109
110
	/* TODO: set the ir_entity assigned to the frame */
}

/**
 * This function is called by the generic backend to correct offsets for
 * nodes accessing the stack.
 */
static void sparc_set_frame_offset(ir_node *irn, int offset)
{
Hannes Rapp's avatar
Hannes Rapp committed
111
112
113
114
115
116
117
118
	if (is_sparc_FrameAddr(irn)) {
		sparc_symconst_attr_t *attr = get_irn_generic_attr(irn);
		attr->fp_offset += offset;
	} else {
		sparc_load_store_attr_t *attr = get_sparc_load_store_attr(irn);
		assert(attr->base.is_load_store);
		attr->offset += offset;
	}
Hannes Rapp's avatar
Hannes Rapp committed
119
120
121
122
123
124
125
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
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
184
185
186
187
188
189
190
191
192
193
194
195
}

static int sparc_get_sp_bias(const ir_node *irn)
{
	(void) irn;
	return 0;
}

/* fill register allocator interface */

static const arch_irn_ops_t sparc_irn_ops = {
	get_sparc_in_req,
	sparc_classify,
	sparc_get_frame_entity,
	sparc_set_frame_entity,
	sparc_set_frame_offset,
	sparc_get_sp_bias,
	NULL,    /* get_inverse             */
	NULL,    /* get_op_estimated_cost   */
	NULL,    /* possible_memory_operand */
	NULL,    /* perform_memory_operand  */
};



/**
 * Transforms the standard firm graph into
 * a SPARC firm graph
 */
static void sparc_prepare_graph(void *self)
{
	sparc_code_gen_t *cg = self;

	/* transform FIRM into SPARC asm nodes */
	sparc_transform_graph(cg);

	if (cg->dump)
		be_dump(cg->irg, "-transformed", dump_ir_block_graph_sched);
}



/**
 * Called immediatly before emit phase.
 */
static void sparc_finish_irg(void *self)
{
	sparc_code_gen_t *cg = self;
	ir_graph            *irg = cg->irg;

	dump_ir_block_graph_sched(irg, "-sparc-finished");
}


static ir_node *sparc_flags_remat(ir_node *node, ir_node *after)
{
	ir_node *block;
	ir_node *copy;

	if (is_Block(after)) {
		block = after;
	} else {
		block = get_nodes_block(after);
	}
	copy = exact_copy(node);
	set_nodes_block(copy, block);
	sched_add_after(after, copy);
	return copy;
}

static void sparc_before_ra(void *self)
{
	sparc_code_gen_t *cg = self;
	/* fixup flags register */
	be_sched_fix_flags(cg->birg, &sparc_reg_classes[CLASS_sparc_flags], &sparc_flags_remat);
}

Hannes Rapp's avatar
Hannes Rapp committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
 * transform reload node => load
 */
static void transform_Reload(ir_node *node)
{
	ir_graph  *irg    = get_irn_irg(node);
	ir_node   *block  = get_nodes_block(node);
	dbg_info  *dbgi   = get_irn_dbg_info(node);
	ir_node   *ptr    = get_irg_frame(irg);
	ir_node   *mem    = get_irn_n(node, be_pos_Reload_mem);
	ir_mode   *mode   = get_irn_mode(node);
	ir_entity *entity = be_get_frame_entity(node);
	const arch_register_t *reg;
	ir_node   *proj;
	ir_node   *load;

	ir_node  *sched_point = sched_prev(node);

	load = new_bd_sparc_Load(dbgi, block, ptr, mem, mode, entity, false, 0, true);
	sched_add_after(sched_point, load);
	sched_remove(node);

	proj = new_rd_Proj(dbgi, block, load, mode, pn_sparc_Load_res);

	reg = arch_get_irn_register(node);
	arch_set_irn_register(proj, reg);

	exchange(node, proj);
}

/**
 * transform spill node => store
 */
static void transform_Spill(ir_node *node)
{
	ir_graph  *irg    = get_irn_irg(node);
	ir_node   *block  = get_nodes_block(node);
	dbg_info  *dbgi   = get_irn_dbg_info(node);
	ir_node   *ptr    = get_irg_frame(irg);
	ir_node   *mem    = new_NoMem();
	ir_node   *val    = get_irn_n(node, be_pos_Spill_val);
	ir_mode   *mode   = get_irn_mode(val);
	ir_entity *entity = be_get_frame_entity(node);
	ir_node   *sched_point;
	ir_node   *store;

	sched_point = sched_prev(node);
	store = new_bd_sparc_Store(dbgi, block, ptr, val, mem, mode, entity, false, 0, true);
	sched_remove(node);
	sched_add_after(sched_point, store);

	exchange(node, store);
}

/**
 * walker to transform be_Spill and be_Reload nodes
 */
static void sparc_after_ra_walker(ir_node *block, void *data)
{
	ir_node *node, *prev;
	(void) data;

	for (node = sched_last(block); !sched_is_begin(node); node = prev) {
		prev = sched_prev(node);

		if (be_is_Reload(node)) {
			transform_Reload(node);
		} else if (be_is_Spill(node)) {
			transform_Spill(node);
		}
	}
}


Hannes Rapp's avatar
Hannes Rapp committed
270
271
static void sparc_after_ra(void *self)
{
Hannes Rapp's avatar
Hannes Rapp committed
272
273
274
275
	sparc_code_gen_t *cg = self;
	be_coalesce_spillslots(cg->birg);

	irg_block_walk_graph(cg->irg, NULL, sparc_after_ra_walker, NULL);
Hannes Rapp's avatar
Hannes Rapp committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
}



/**
 * Emits the code, closes the output file and frees
 * the code generator interface.
 */
static void sparc_emit_and_done(void *self)
{
	sparc_code_gen_t *cg = self;
	ir_graph           *irg = cg->irg;

	sparc_gen_routine(cg, irg);

	/* de-allocate code generator */
	free(cg);
}

static void *sparc_cg_init(be_irg_t *birg);

static const arch_code_generator_if_t sparc_code_gen_if = {
	sparc_cg_init,
	NULL,                    /* get_pic_base hook */
	NULL,                    /* before abi introduce hook */
	sparc_prepare_graph,
	NULL,                    /* spill hook */
	sparc_before_ra,      /* before register allocation hook */
	sparc_after_ra,       /* after register allocation hook */
	sparc_finish_irg,
	sparc_emit_and_done
};

/**
 * Initializes the code generator.
 */
static void *sparc_cg_init(be_irg_t *birg)
{
	static ir_type *int_tp = NULL;
	sparc_isa_t      *isa = (sparc_isa_t *)birg->main_env->arch_env;
	sparc_code_gen_t *cg;

	if (! int_tp) {
		/* create an integer type with machine size */
320
		int_tp = new_type_primitive(mode_Is);
Hannes Rapp's avatar
Hannes Rapp committed
321
322
323
324
325
326
327
328
329
330
331
332
333
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
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
420
421
422
423
424
425
426
427
428
429
430
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
470
471
472
473
474
475
476
477
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
	}

	cg 				 = XMALLOC(sparc_code_gen_t);
	cg->impl				= &sparc_code_gen_if;
	cg->irg				= birg->irg;
	//cg->reg_set				= new_set(arm_cmp_irn_reg_assoc, 1024);
	cg->isa				= isa;
	cg->birg				= birg;
	//cg->int_tp				= int_tp;
	//cg->have_fp_insn	= 0;
	//cg->unknown_gp		= NULL;
	//cg->unknown_fpa		= NULL;
	cg->dump				= (birg->main_env->options->dump_flags & DUMP_BE) ? 1 : 0;

	FIRM_DBG_REGISTER(cg->mod, "firm.be.sparc.cg");

	/* enter the current code generator */
	isa->cg = cg;

	return (arch_code_generator_t *)cg;
}



const arch_isa_if_t sparc_isa_if;
static sparc_isa_t sparc_isa_template = {
	{
		&sparc_isa_if,             /* isa interface implementation */
		&sparc_gp_regs[REG_SP],  /* stack pointer register */
		&sparc_gp_regs[REG_FP],  /* base pointer register */
		&sparc_reg_classes[CLASS_sparc_gp],  /* link pointer register class */
		-1,                          /* stack direction */
		1,                           /* power of two stack alignment for calls, 2^2 == 4 */
		NULL,                        /* main environment */
		7,                           /* costs for a spill instruction */
		5,                           /* costs for a reload instruction */
	},
	NULL						/* current code generator */
};

/**
 * Initializes the backend ISA
 */
static arch_env_t *sparc_init(FILE *outfile)
{
	static int run_once = 0;
	sparc_isa_t *isa;

	if(run_once)
		return NULL;
	run_once = 1;

	isa = XMALLOC(sparc_isa_t);
	memcpy(isa, &sparc_isa_template, sizeof(*isa));

	be_emit_init(outfile);

	sparc_register_init();
	sparc_create_opcodes(&sparc_irn_ops);

	return &isa->arch_env;
}



/**
 * Closes the output file and frees the ISA structure.
 */
static void sparc_done(void *self)
{
	sparc_isa_t *isa = self;

	/* emit now all global declarations */
	be_gas_emit_decls(isa->arch_env.main_env, 0);

	be_emit_exit();
	free(self);
}


static unsigned sparc_get_n_reg_class(void)
{
	return N_CLASSES;
}

static const arch_register_class_t *sparc_get_reg_class(unsigned i)
{
	assert(i < N_CLASSES);
	return &sparc_reg_classes[i];
}



/**
 * Get the register class which shall be used to store a value of a given mode.
 * @param self The this pointer.
 * @param mode The mode in question.
 * @return A register class which can hold values of the given mode.
 */
const arch_register_class_t *sparc_get_reg_class_for_mode(const ir_mode *mode)
{
	if (mode_is_float(mode))
		return &sparc_reg_classes[CLASS_sparc_fp];
	else
		return &sparc_reg_classes[CLASS_sparc_gp];
}



typedef struct {
	be_abi_call_flags_bits_t flags;
	const arch_env_t *arch_env;
	ir_graph *irg;
} sparc_abi_env_t;

static void *sparc_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
{
	sparc_abi_env_t *env = XMALLOC(sparc_abi_env_t);
	be_abi_call_flags_t fl = be_abi_call_get_flags(call);
	env->flags    = fl.bits;
	env->irg      = irg;
	env->arch_env = arch_env;
	return env;
}

/**
 * Get the between type for that call.
 * @param self The callback object.
 * @return The between type of for that call.
 */
static ir_type *sparc_get_between_type(void *self)
{
	static ir_type *between_type = NULL;
	(void) self;

	if (between_type == NULL) {
		between_type = new_type_class(new_id_from_str("sparc_between_type"));
		set_type_size_bytes(between_type, 0);
	}

	return between_type;
}

/**
 * Build the prolog, return the BASE POINTER register
 */
static const arch_register_t *sparc_abi_prologue(void *self, ir_node **mem,
                                                    pmap *reg_map, int *stack_bias)
{
	sparc_abi_env_t *env = self;
	(void) reg_map;
	(void) mem;
	(void) stack_bias;

	if(env->flags.try_omit_fp)
		return env->arch_env->sp;

	//panic("framepointer not implemented yet");
	return env->arch_env->bp;
}

/* Build the epilog */
static void sparc_abi_epilogue(void *self, ir_node *bl, ir_node **mem,
                                  pmap *reg_map)
{
	(void) self;
	(void) bl;
	(void) mem;
	(void) reg_map;
}

static const be_abi_callbacks_t sparc_abi_callbacks = {
	sparc_abi_init,
	free,
	sparc_get_between_type,
	sparc_abi_prologue,
	sparc_abi_epilogue,
};

/**
 * Get the ABI restrictions for procedure calls.
 * @param self        The this pointer.
 * @param method_type The type of the method (procedure) in question.
 * @param abi         The abi object to be modified
 */
void sparc_get_call_abi(const void *self, ir_type *method_type,
                           be_abi_call_t *abi)
{
	ir_type  *tp;
	ir_mode  *mode;
	int       i, n = get_method_n_params(method_type);
	be_abi_call_flags_t call_flags;
	(void) self;

	/* set abi flags for calls */
	call_flags.bits.left_to_right         = 0;
	call_flags.bits.store_args_sequential = 1;
	call_flags.bits.try_omit_fp           = 1;
	call_flags.bits.fp_free               = 0;
	call_flags.bits.call_has_imm          = 1;

	/* set stack parameter passing style */
	be_abi_call_set_flags(abi, call_flags, &sparc_abi_callbacks);

	for (i = 0; i < n; i++) {
		/* reg = get reg for param i;          */
		/* be_abi_call_param_reg(abi, i, reg); */
Hannes Rapp's avatar
Hannes Rapp committed
528
529
530
531
532
533
534
535
536

		/* pass args 0-5 via registers, remaining via stack */
		if (i < 6) {
			be_abi_call_param_reg(abi, i, sparc_get_RegParam_reg(i));
		} else {
			tp   = get_method_param_type(method_type, i);
			mode = get_type_mode(tp);
			be_abi_call_param_stack(abi, i, mode, 4, 0, 0);
		}
Hannes Rapp's avatar
Hannes Rapp committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
	}

	/* TODO: set correct return register */
	/* default: return value is in O0 resp. F0 */
	if (get_method_n_ress(method_type) > 0) {
		tp   = get_method_res_type(method_type, 0);
		mode = get_type_mode(tp);

		be_abi_call_res_reg(abi, 0,
			mode_is_float(mode) ? &sparc_fp_regs[REG_F0] : &sparc_gp_regs[REG_O0]);
	}
}

int sparc_to_appear_in_schedule(void *block_env, const ir_node *irn)
{
	(void) block_env;

	if(!is_sparc_irn(irn))
		return -1;

	return 1;
}

/**
 * Initializes the code generator interface.
 */
static const arch_code_generator_if_t *sparc_get_code_generator_if(
		void *self)
{
	(void) self;
	return &sparc_code_gen_if;
}

list_sched_selector_t sparc_sched_selector;

/**
 * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
 */
static const list_sched_selector_t *sparc_get_list_sched_selector(
		const void *self, list_sched_selector_t *selector)
{
	(void) self;
	(void) selector;

	sparc_sched_selector = trivial_selector;
	sparc_sched_selector.to_appear_in_schedule = sparc_to_appear_in_schedule;
	return &sparc_sched_selector;
}

static const ilp_sched_selector_t *sparc_get_ilp_sched_selector(
		const void *self)
{
	(void) self;
	return NULL;
}

/**
 * Returns the necessary byte alignment for storing a register of given class.
 */
static int sparc_get_reg_class_alignment(const arch_register_class_t *cls)
{
	ir_mode *mode = arch_register_class_mode(cls);
	return get_mode_size_bytes(mode);
}

/**
 * Returns the libFirm configuration parameter for this backend.
 */
static const backend_params *sparc_get_backend_params(void) {
	static backend_params p = {
		0,     /* no dword lowering */
		0,     /* no inline assembly */
		NULL,  /* will be set later */
		NULL,  /* no creator function */
		NULL,  /* context for create_intrinsic_fkt */
		NULL,  /* parameter for if conversion */
		NULL,  /* float arithmetic mode */
		0,     /* no trampoline support: size 0 */
		0,     /* no trampoline support: align 0 */
		NULL,  /* no trampoline support: no trampoline builder */
		4      /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
	};
	return &p;
}

static const be_execution_unit_t ***sparc_get_allowed_execution_units(
		const ir_node *irn)
{
	(void) irn;
	/* TODO */
	assert(0);
	return NULL;
}

static const be_machine_t *sparc_get_machine(const void *self)
{
	(void) self;
	/* TODO */
	assert(0);
	return NULL;
}

static ir_graph **sparc_get_backend_irg_list(const void *self,
                                                ir_graph ***irgs)
{
	(void) self;
	(void) irgs;
	return NULL;
}

static asm_constraint_flags_t sparc_parse_asm_constraint(const char **c)
{
	(void) c;
	return ASM_CONSTRAINT_FLAG_INVALID;
}

static int sparc_is_valid_clobber(const char *clobber)
{
	(void) clobber;
	return 0;
}

const arch_isa_if_t sparc_isa_if = {
	sparc_init,
	sparc_done,
	NULL,                /* handle intrinsics */
	sparc_get_n_reg_class,
	sparc_get_reg_class,
	sparc_get_reg_class_for_mode,
	sparc_get_call_abi,
	sparc_get_code_generator_if,
	sparc_get_list_sched_selector,
	sparc_get_ilp_sched_selector,
	sparc_get_reg_class_alignment,
    sparc_get_backend_params,
	sparc_get_allowed_execution_units,
	sparc_get_machine,
	sparc_get_backend_irg_list,
	NULL,                    /* mark remat */
	sparc_parse_asm_constraint,
	sparc_is_valid_clobber
};

void be_init_arch_sparc(void)
{
	be_register_isa_if("sparc", &sparc_isa_if);
	FIRM_DBG_REGISTER(dbg, "firm.be.sparc.cg");
	sparc_init_transform();
	sparc_init_emitter();
}
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_sparc);