bearch_ppc32.c 25.1 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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.
 */

20
21
22
23
24
25
/**
 * @file
 * @brief   The main ppc backend driver file.
 * @author  Moritz Kroll, Jens Mueller
 * @version $Id$
 */
26
#ifdef HAVE_CONFIG_H
27
#include "config.h"
28
29
30
31
32
33
34
35
36
37
38
39
#endif

#include "pseudo_irg.h"
#include "irgwalk.h"
#include "irprog.h"
#include "irprintf.h"
#include "ircons.h"
#include "irgmod.h"
#include "irdump.h"

#include "bitset.h"
#include "debug.h"
Matthias Braun's avatar
Matthias Braun committed
40
#include "error.h"
41

42
#include "../bearch_t.h"                /* the general register allocator interface */
43
44
45
#include "../benode_t.h"
#include "../belower.h"
#include "../besched_t.h"
46
#include "be.h"
47
#include "../beabi.h"
48
#include "../bemachine.h"
49
#include "../bemodule.h"
Michael Beck's avatar
Michael Beck committed
50
#include "../bespillslots.h"
51
#include "../beblocksched.h"
52
#include "../beirg_t.h"
Michael Beck's avatar
Michael Beck committed
53
#include "../begnuas.h"
54
55
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

#include "pset.h"

#include "bearch_ppc32_t.h"

#include "ppc32_new_nodes.h"           /* ppc nodes interface */
#include "gen_ppc32_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
#include "ppc32_transform.h"
#include "ppc32_transform_conv.h"
#include "ppc32_emitter.h"
#include "ppc32_map_regs.h"

#define DEBUG_MODULE "firm.be.ppc.isa"

int isleaf;

/* TODO: ugly, but we need it to get access to the registers assigned to Phi nodes */
static set *cur_reg_set = NULL;

/**************************************************
 *                         _ _              _  __
 *                        | | |            (_)/ _|
 *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
 * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
 * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
 * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
 *            __/ |
 *           |___/
 **************************************************/

/**
 * Return register requirements for a ppc node.
 * If the node returns a tuple (mode_T) then the proj's
 * will be asked for this information.
 */
Matthias Braun's avatar
Matthias Braun committed
89
90
91
static const
arch_register_req_t *ppc32_get_irn_reg_req(const void *self,
                                           const ir_node *irn, int pos) {
92
93
	long               node_pos = pos == -1 ? 0 : pos;
	ir_mode           *mode     = get_irn_mode(irn);
94
	FIRM_DBG_REGISTER(firm_dbg_module_t *mod, DEBUG_MODULE);
95
	(void) self;
96
97
98

	if (is_Block(irn) || mode == mode_X || mode == mode_M) {
		DBG((mod, LEVEL_1, "ignoring block, mode_X or mode_M node %+F\n", irn));
Matthias Braun's avatar
Matthias Braun committed
99
		return arch_no_register_req;
100
101
102
103
	}

	if (mode == mode_T && pos < 0) {
		DBG((mod, LEVEL_1, "ignoring request for OUT requirements at %+F", irn));
Matthias Braun's avatar
Matthias Braun committed
104
		return arch_no_register_req;
105
106
107
108
109
110
111
112
113
	}

	DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));

	if (is_Proj(irn)) {
		/* in case of a proj, we need to get the correct OUT slot */
		/* of the node corresponding to the proj number */
		if (pos == -1) {
			node_pos = ppc32_translate_proj_pos(irn);
Matthias Braun's avatar
Matthias Braun committed
114
		} else {
115
116
117
			node_pos = pos;
		}

Matthias Braun's avatar
Matthias Braun committed
118
		irn = skip_Proj_const(irn);
119
120
121
122
123
124

		DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
	}

	/* get requirements for our own nodes */
	if (is_ppc32_irn(irn)) {
Matthias Braun's avatar
Matthias Braun committed
125
		const arch_register_req_t *req;
126
		if (pos >= 0) {
Matthias Braun's avatar
Matthias Braun committed
127
128
129
			req = get_ppc32_in_req(irn, pos);
		} else {
			req = get_ppc32_out_req(irn, node_pos);
130
131
132
		}

		DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
Matthias Braun's avatar
Matthias Braun committed
133
		return req;
134
135
	}

Matthias Braun's avatar
Matthias Braun committed
136
137
138
139
140
	/* unknowns should be transformed by now */
	assert(!is_Unknown(irn));

	DB((mod, LEVEL_1, "returning NULL for %+F (node not supported)\n", irn));
	return arch_no_register_req;
141
142
143
144
}

static void ppc32_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
	int pos = 0;
145
	(void) self;
146
147
148
149
150
151
152
153

	if (is_Proj(irn)) {

		if (get_irn_mode(irn) == mode_X) {
			return;
		}

		pos = ppc32_translate_proj_pos(irn);
Matthias Braun's avatar
Matthias Braun committed
154
		irn = skip_Proj(irn);
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
	}

	if (is_ppc32_irn(irn)) {
		const arch_register_t **slots;

		slots      = get_ppc32_slots(irn);
		slots[pos] = reg;
	}
	else {
		/* here we set the registers for the Phi nodes */
		ppc32_set_firm_reg(irn, reg, cur_reg_set);
	}
}

static const arch_register_t *ppc32_get_irn_reg(const void *self, const ir_node *irn) {
	int pos = 0;
	const arch_register_t *reg = NULL;
172
	(void) self;
173
174
175
176
177
178
179
180

	if (is_Proj(irn)) {

		if (get_irn_mode(irn) == mode_X) {
			return NULL;
		}

		pos = ppc32_translate_proj_pos(irn);
Matthias Braun's avatar
Matthias Braun committed
181
		irn = skip_Proj_const(irn);
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
	}

	if (is_ppc32_irn(irn)) {
		const arch_register_t **slots;
		slots = get_ppc32_slots(irn);
		reg   = slots[pos];
	}
	else {
		reg = ppc32_get_firm_reg(irn, cur_reg_set);
	}

	return reg;
}

static arch_irn_class_t ppc32_classify(const void *self, const ir_node *irn) {
197
	(void) self;
Matthias Braun's avatar
Matthias Braun committed
198
	irn = skip_Proj_const(irn);
199
200
201
202
203
204
205
206
207
208
209
210

	if (is_cfop(irn)) {
		return arch_irn_class_branch;
	}
	else if (is_ppc32_irn(irn)) {
		return arch_irn_class_normal;
	}

	return 0;
}

static arch_irn_flags_t ppc32_get_flags(const void *self, const ir_node *irn) {
211
	(void) self;
Matthias Braun's avatar
Matthias Braun committed
212
	irn = skip_Proj_const(irn);
213
214
215
216
217
218
219
220
221
222
223

	if (is_ppc32_irn(irn)) {
		return get_ppc32_flags(irn);
	}
	else if (is_Unknown(irn)) {
		return arch_irn_flags_ignore;
	}

	return 0;
}

224
static ir_entity *ppc32_get_frame_entity(const void *self, const ir_node *irn) {
225
	(void) self;
226
227
228
229
230
	if(!is_ppc32_irn(irn)) return NULL;
	if(get_ppc32_type(irn)!=ppc32_ac_FrameEntity) return NULL;
	return get_ppc32_frame_entity(irn);
}

Matthias Braun's avatar
Matthias Braun committed
231
static void ppc32_set_frame_entity(const void *self, ir_node *irn, ir_entity *ent) {
232
	(void) self;
233
234
235
236
237
	if (! is_ppc32_irn(irn) || get_ppc32_type(irn) != ppc32_ac_FrameEntity)
		return;
	set_ppc32_frame_entity(irn, ent);
}

238
239
240
241
242
/**
 * This function is called by the generic backend to correct offsets for
 * nodes accessing the stack.
 */
static void ppc32_set_stack_bias(const void *self, ir_node *irn, int bias) {
243
	(void) self;
244
245
246
	set_ppc32_offset(irn, bias);
}

247
static int ppc32_get_sp_bias(const void *self, const ir_node *irn) {
248
249
	(void) self;
	(void) irn;
250
251
252
	return 0;
}

253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
typedef struct
{
	const be_abi_call_t *call;
	ir_graph *irg;
} ppc32_abi_env;

/**
 * Initialize the callback object.
 * @param call The call object.
 * @param aenv The architecture environment.
 * @param irg  The graph with the method.
 * @return     Some pointer. This pointer is passed to all other callback functions as self object.
 */
static void *ppc32_abi_init(const be_abi_call_t *call, const arch_env_t *aenv, ir_graph *irg)
{
	ppc32_abi_env *env = xmalloc(sizeof(ppc32_abi_env));
Michael Beck's avatar
Michael Beck committed
269
270
	(void) aenv;

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
	env->call = call;
	env->irg = irg;
	return env;
}

/**
 * Destroy the callback object.
 * @param self The callback object.
 */
static void ppc32_abi_done(void *self)
{
	free(self);
}

/**
 * Get the between type for that call.
 * @param self The callback object.
 * @return The between type of for that call.
 */
static ir_type *ppc32_abi_get_between_type(void *self)
{
	static ir_type *between_type = NULL;
293
	static ir_entity *old_bp_ent = NULL;
294
	(void) self;
295
296

	if(!between_type) {
297
		ir_entity *ret_addr_ent;
298
299
300
301
302
303
304
		ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
		ir_type *old_bp_type   = new_type_primitive(new_id_from_str("bp"), mode_P);

		between_type           = new_type_class(new_id_from_str("ppc32_between_type"));
		old_bp_ent             = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
		ret_addr_ent           = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);

305
306
		set_entity_offset(old_bp_ent, 0);
		set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
307
308
309
310
311
312
313
314
315
316
317
318
319
		set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
	}

	return between_type;
}

/**
 * Put all registers which are saved by the prologue/epilogue in a set.
 * @param self The callback object.
 * @param regs A set.
 */
static void ppc32_abi_regs_saved_by_me(void *self, pset *regs)
{
320
321
	(void) self;
	(void) regs;
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
}

/**
 * Generate the prologue.
 * @param self    The callback object.
 * @param mem     A pointer to the mem node. Update this if you define new memory.
 * @param reg_map A mapping mapping all callee_save/ignore/parameter registers to their defining nodes.
 * @return        The register which shall be used as a stack frame base.
 *
 * All nodes which define registers in @p reg_map must keep @p reg_map current.
 */
static const arch_register_t *ppc32_abi_prologue(void *self, ir_node **mem, pmap *reg_map)
{
	ppc32_abi_env *env = (ppc32_abi_env *) self;
	be_abi_call_flags_t flags = be_abi_call_get_flags(env->call);
337
338
	(void) mem;
	(void) reg_map;
339
340
341
	isleaf = flags.bits.irg_is_leaf;

	if(flags.bits.try_omit_fp)
Michael Beck's avatar
Michael Beck committed
342
		return &ppc32_gp_regs[REG_R1];
343
	else
Michael Beck's avatar
Michael Beck committed
344
		return &ppc32_gp_regs[REG_R31];
345
346
347
348
349
350
351
352
353
354
355
356
357
}

/**
 * Generate the epilogue.
 * @param self    The callback object.
 * @param mem     Memory one can attach to.
 * @param reg_map A mapping mapping all callee_save/ignore/return registers to their defining nodes.
 *
 * All nodes which define registers in @p reg_map must keep @p reg_map current.
 * Also, the @p mem variable must be updated, if memory producing nodes are inserted.
 */
static void ppc32_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map)
{
358
359
360
361
	(void) self;
	(void) bl;
	(void) mem;
	(void) reg_map;
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
}

static const be_abi_callbacks_t ppc32_abi_callbacks = {
	ppc32_abi_init,
	ppc32_abi_done,
	ppc32_abi_get_between_type,
	ppc32_abi_regs_saved_by_me,
	ppc32_abi_prologue,
	ppc32_abi_epilogue,
};

/* fill register allocator interface */

static const arch_irn_ops_if_t ppc32_irn_ops_if = {
	ppc32_get_irn_reg_req,
	ppc32_set_irn_reg,
	ppc32_get_irn_reg,
	ppc32_classify,
	ppc32_get_flags,
	ppc32_get_frame_entity,
382
	ppc32_set_frame_entity,
383
	ppc32_set_stack_bias,
384
	ppc32_get_sp_bias,
385
386
387
388
	NULL,    /* get_inverse             */
	NULL,    /* get_op_estimated_cost   */
	NULL,    /* possible_memory_operand */
	NULL,    /* perform_memory_operand  */
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
};

ppc32_irn_ops_t ppc32_irn_ops = {
	&ppc32_irn_ops_if,
	NULL
};



/**************************************************
 *                _                         _  __
 *               | |                       (_)/ _|
 *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
 *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
 * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
 *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
 *                        __/ |
 *                       |___/
 **************************************************/

static void ppc32_before_abi(void *self) {
	ppc32_code_gen_t *cg = self;
	ir_type *frame_type = get_irg_frame_type(cg->irg);

	frame_alloc_area(frame_type, 24, 4, 1);

	ppc32_init_conv_walk();
	irg_walk_blkwise_graph(cg->irg, NULL, ppc32_conv_walk, cg);

	if (cg->area_size) {
		if(cg->area_size < 32) cg->area_size = 32;
		cg->area = frame_alloc_area(get_irg_frame_type(cg->irg), cg->area_size+24, 16, 1);
	}
}

static void ppc32_search_start_successor(ir_node *block, void *env) {
	ppc32_code_gen_t *cg = env;
	int n = get_Block_n_cfgpreds(block);
	ir_node *startblock = get_irg_start_block(cg->irg);
	if(block == startblock) return;

	for (n--; n >= 0; n--) {
		ir_node *predblock = get_irn_n(get_Block_cfgpred(block, n), -1);
		if(predblock == startblock)
		{
			cg->start_succ_block = block;
			return;
		}
	}
}

/**
 * Transforms the standard firm graph into
 * a ppc firm graph
 */
static void ppc32_prepare_graph(void *self) {
	ppc32_code_gen_t *cg = self;

	irg_block_walk_graph(cg->irg, NULL, ppc32_search_start_successor, cg);
	irg_walk_blkwise_graph(cg->irg, NULL, ppc32_pretransform_walk, cg);
	be_dump(cg->irg, "-pretransformed", dump_ir_block_graph);

451
	ppc32_register_transformers();
452
453
454
455
456
457
458
459
460
461
	irg_walk_blkwise_graph(cg->irg, NULL, ppc32_transform_node, cg);
	be_dump(cg->irg, "-transformed", dump_ir_block_graph);
	irg_walk_blkwise_graph(cg->irg, NULL, ppc32_transform_const, cg);
}



/**
 * Called immediatly before emit phase.
 */
462
static void ppc32_finish_irg(void *self) {
463
	(void) self;
464
465
466
467
468
469
470
471
472
473
	/* TODO: - fix offsets for nodes accessing stack
			 - ...
	*/
}


/**
 * These are some hooks which must be filled but are probably not needed.
 */
static void ppc32_before_sched(void *self) {
474
	(void) self;
475
476
477
478
479
480
481
482
483
484
	/* Some stuff you need to do after scheduling but before register allocation */
}

/**
 * Called before the register allocator.
 * Calculate a block schedule here. We need it for the x87
 * simulator and the emitter.
 */
static void ppc32_before_ra(void *self) {
	ppc32_code_gen_t *cg = self;
485
	cg->blk_sched = be_create_block_schedule(cg->irg, cg->birg->exec_freq);
486
487
488
489
490
491
492
493
}

static void ppc32_transform_spill(ir_node *node, void *env)
{
	ppc32_code_gen_t *cgenv = (ppc32_code_gen_t *)env;

	if(be_is_Spill(node))
	{
494
495
496
		ir_node  *store, *proj;
		dbg_info *dbg   = get_irn_dbg_info(node);
		ir_node  *block = get_nodes_block(node);
497
498
499

		const arch_register_class_t *regclass = arch_get_irn_reg_class(cgenv->arch_env, node, 1);

Michael Beck's avatar
Michael Beck committed
500
		if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp])
501
502
		{
			store = new_rd_ppc32_Stw(dbg, current_ir_graph, block,
Michael Beck's avatar
Michael Beck committed
503
				get_irn_n(node, 0), get_irn_n(node, 1), new_rd_NoMem(current_ir_graph));
504
		}
Michael Beck's avatar
Michael Beck committed
505
		else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp])
506
507
		{
			store = new_rd_ppc32_Stfd(dbg, current_ir_graph, block,
Michael Beck's avatar
Michael Beck committed
508
				get_irn_n(node, 0), get_irn_n(node, 1), new_rd_NoMem(current_ir_graph));
509
		}
Matthias Braun's avatar
Matthias Braun committed
510
		else panic("Spill for register class not supported yet!");
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529

		set_ppc32_frame_entity(store, be_get_frame_entity(node));

		proj = new_rd_Proj(dbg, current_ir_graph, block, store, mode_M, pn_Store_M);

		if (sched_is_scheduled(node)) {
			sched_add_after(sched_prev(node), store);
			sched_add_after(store, proj);

			sched_remove(node);
		}

		exchange(node, proj);
	}

	if(be_is_Reload(node))
	{
		ir_node *load, *proj;
		const arch_register_t *reg;
530
531
532
		dbg_info *dbg   = get_irn_dbg_info(node);
		ir_node  *block = get_nodes_block(node);
		ir_mode  *mode  = get_irn_mode(node);
533
534
535

		const arch_register_class_t *regclass = arch_get_irn_reg_class(cgenv->arch_env, node, -1);

Michael Beck's avatar
Michael Beck committed
536
		if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp])
537
		{
Michael Beck's avatar
Michael Beck committed
538
			load = new_rd_ppc32_Lwz(dbg, current_ir_graph, block,	get_irn_n(node, 0), get_irn_n(node, 1));
539
		}
Michael Beck's avatar
Michael Beck committed
540
		else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp])
541
		{
Michael Beck's avatar
Michael Beck committed
542
			load = new_rd_ppc32_Lfd(dbg, current_ir_graph, block,	get_irn_n(node, 0), get_irn_n(node, 1));
543
		}
Matthias Braun's avatar
Matthias Braun committed
544
		else panic("Reload for register class not supported yet!");
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

		set_ppc32_frame_entity(load, be_get_frame_entity(node));

		proj = new_rd_Proj(dbg, current_ir_graph, block, load, mode, pn_Load_res);

		if (sched_is_scheduled(node)) {
			sched_add_after(sched_prev(node), load);
			sched_add_after(load, proj);

			sched_remove(node);
		}

		/* copy the register from the old node to the new Load */
		reg = arch_get_irn_register(cgenv->arch_env, node);
		arch_set_irn_register(cgenv->arch_env, load, reg);

		exchange(node, proj);
	}
}

/**
 * Some stuff to do immediately after register allocation
 */
static void ppc32_after_ra(void *self) {
	ppc32_code_gen_t *cg = self;
Michael Beck's avatar
Michael Beck committed
570
	be_coalesce_spillslots(cg->birg);
571
572
573
574
575
576
577
578
579
	irg_walk_blkwise_graph(cg->irg, NULL, ppc32_transform_spill, cg);
}

/**
 * Emits the code, closes the output file and frees
 * the code generator interface.
 */
static void ppc32_emit_and_done(void *self) {
	ppc32_code_gen_t *cg = self;
Michael Beck's avatar
Michael Beck committed
580
	ir_graph         *irg = cg->irg;
581
582

	dump_ir_block_graph_sched(irg, "-ppc-finished");
Michael Beck's avatar
Michael Beck committed
583
	ppc32_gen_routine(cg, irg);
584
585
586
587
588
589
590
591

	cur_reg_set = NULL;

	/* de-allocate code generator */
	del_set(cg->reg_set);
	free(self);
}

592
int is_direct_entity(ir_entity *ent);
593

Matthias Braun's avatar
Matthias Braun committed
594
static void *ppc32_cg_init(be_irg_t *birg);
595
596
597

static const arch_code_generator_if_t ppc32_code_gen_if = {
	ppc32_cg_init,
598
	NULL,                 /* get_pic_base */
599
600
	ppc32_before_abi,
	ppc32_prepare_graph,
601
	NULL,                 /* spill */
602
603
604
	ppc32_before_sched,   /* before scheduling hook */
	ppc32_before_ra,      /* before register allocation hook */
	ppc32_after_ra,
605
	ppc32_finish_irg,
606
607
608
609
610
611
	ppc32_emit_and_done
};

/**
 * Initializes the code generator.
 */
Matthias Braun's avatar
Matthias Braun committed
612
static void *ppc32_cg_init(be_irg_t *birg) {
613
614
615
616
617
618
619
	ppc32_isa_t      *isa = (ppc32_isa_t *)birg->main_env->arch_env->isa;
	ppc32_code_gen_t *cg  = xmalloc(sizeof(*cg));

	cg->impl      = &ppc32_code_gen_if;
	cg->irg       = birg->irg;
	cg->reg_set   = new_set(ppc32_cmp_irn_reg_assoc, 1024);
	cg->arch_env  = birg->main_env->arch_env;
620
	cg->isa       = isa;
621
622
623
624
625
	cg->birg      = birg;
	cg->area_size = 0;
	cg->area      = NULL;
	cg->start_succ_block = NULL;
	cg->blk_sched = NULL;
626
	FIRM_DBG_REGISTER(cg->mod, "firm.be.ppc.cg");
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646

	cur_reg_set = cg->reg_set;
	ppc32_irn_ops.cg = cg;

	return (arch_code_generator_t *)cg;
}



/*****************************************************************
 *  ____             _                  _   _____  _____
 * |  _ \           | |                | | |_   _|/ ____|  /\
 * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
 * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
 * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
 * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
 *
 *****************************************************************/

static ppc32_isa_t ppc32_isa_template = {
Michael Beck's avatar
Michael Beck committed
647
648
649
650
651
	{
		&ppc32_isa_if,           /* isa interface */
		&ppc32_gp_regs[REG_R1],  /* stack pointer */
		&ppc32_gp_regs[REG_R31], /* base pointer */
		-1,                      /* stack is decreasing */
652
		1,                       /* call stack alignment */
Michael Beck's avatar
Michael Beck committed
653
		NULL,                    /* main environment */
654
655
		7,                       /* spill costs */
		5,                       /* reload costs */
Michael Beck's avatar
Michael Beck committed
656
657
	},
	NULL                    /* symbol set */
658
659
};

Michael Beck's avatar
Michael Beck committed
660
661
662
663
664
665
666
667
668
669
670
/**
 * Collects all SymConsts which need to be accessed "indirectly"
 *
 * @param node    the firm node
 * @param env     the symbol set
 */
static void ppc32_collect_symconsts_walk(ir_node *node, void *env) {
	pset *symbol_set = env;

	if (is_SymConst(node)) {
		ir_entity *ent = get_SymConst_entity(node);
Michael Beck's avatar
Michael Beck committed
671
		set_entity_backend_marked(ent, 1);
Michael Beck's avatar
Michael Beck committed
672
673
674
675
676
		if (! is_direct_entity(ent))
			pset_insert_ptr(symbol_set, ent);
	}
}

677
678
679
/**
 * Initializes the backend ISA and opens the output file.
 */
680
static void *ppc32_init(FILE *file_handle) {
681
682
	static int inited = 0;
	ppc32_isa_t *isa;
Michael Beck's avatar
Michael Beck committed
683
	int i;
684

Michael Beck's avatar
Michael Beck committed
685
	if (inited)
686
687
		return NULL;

688
	isa = xmalloc(sizeof(*isa));
689
690
	memcpy(isa, &ppc32_isa_template, sizeof(*isa));

691
	be_emit_init(file_handle);
692

Matthias Braun's avatar
Matthias Braun committed
693
	ppc32_register_init();
694
695
696
697
	ppc32_create_opcodes();

	inited = 1;

Michael Beck's avatar
Michael Beck committed
698
699
700
701
702
703
704
705
706
707
708
709
	isa->symbol_set = pset_new_ptr(8);
	for (i = 0; i < get_irp_n_irgs(); ++i) {
		ir_graph *irg = get_irp_irg(i);
		irg_walk_blkwise_graph(irg, NULL, ppc32_collect_symconsts_walk, isa->symbol_set);
	}

	/* we mark referenced global entities, so we can only emit those which
	 * are actually referenced. (Note: you mustn't use the type visited flag
	 * elsewhere in the backend)
	 */
	inc_master_type_visited();

710
711
712
	return isa;
}

Michael Beck's avatar
Michael Beck committed
713
714
static void ppc32_dump_indirect_symbols(ppc32_isa_t *isa) {
	ir_entity *ent;
715

Michael Beck's avatar
Michael Beck committed
716
717
	foreach_pset(isa->symbol_set, ent) {
		const char *ld_name = get_entity_ld_name(ent);
718
719
		be_emit_irprintf(".non_lazy_symbol_pointer\n%s:\n\t.indirect_symbol _%s\n\t.long 0\n\n", ld_name, ld_name);
		be_emit_write_line();
Michael Beck's avatar
Michael Beck committed
720
721
	}
}
722
723
724
725
726

/**
 * Closes the output file and frees the ISA structure.
 */
static void ppc32_done(void *self) {
Michael Beck's avatar
Michael Beck committed
727
728
	ppc32_isa_t *isa = self;

729
730
	be_gas_emit_decls(isa->arch_isa.main_env, 1);
	be_gas_emit_switch_section(GAS_SECTION_DATA);
Michael Beck's avatar
Michael Beck committed
731
732
	ppc32_dump_indirect_symbols(isa);

733
	be_emit_exit();
Michael Beck's avatar
Michael Beck committed
734
735
	del_pset(isa->symbol_set);

736
737
738
739
740
	free(self);
}



741
static unsigned ppc32_get_n_reg_class(const void *self) {
742
	(void) self;
743
744
745
	return N_CLASSES;
}

746
747
static const arch_register_class_t *ppc32_get_reg_class(const void *self,
                                                        unsigned i) {
748
	(void) self;
749
	assert(i < N_CLASSES && "Invalid ppc register class requested.");
750
751
752
753
754
755
756
757
758
759
760
761
	return &ppc32_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 *ppc32_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
762
	(void) self;
763
	if (mode_is_float(mode))
Michael Beck's avatar
Michael Beck committed
764
		return &ppc32_reg_classes[CLASS_ppc32_fp];
765
	else
Michael Beck's avatar
Michael Beck committed
766
		return &ppc32_reg_classes[CLASS_ppc32_gp];
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
}


/**
 * 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
 */
static void ppc32_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);
	int		  stackoffs = 0, lastoffs = 0, stackparamsize;

	int		  gpregi = REG_R3;
	int		  fpregi = REG_F1;

	const arch_register_t *reg;
786
	be_abi_call_flags_t call_flags = { { 0, 0, 1, 0, 0, 0, 1 } };
787

788
	(void) self;
789
790
791
792
793
794
795
796
	if(get_type_visibility(method_type)!=visibility_external_allocated)
		call_flags.bits.call_has_imm = 1;

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

	for (i = 0; i < n; i++) {
		tp   = get_method_param_type(method_type, i);
797
		mode = get_type_mode(tp);
798
799
800
801
802
803
804
805
		if(is_atomic_type(tp))
		{
			if(mode_is_float(mode))
			{
				if(fpregi <= REG_F13)
				{
					if(get_mode_size_bits(mode) == 32) gpregi++, stackparamsize=4;
					else gpregi += 2, stackparamsize=8;								// mode == irm_D
Michael Beck's avatar
Michael Beck committed
806
					reg = &ppc32_fp_regs[fpregi++];
807
808
809
810
811
812
813
814
815
816
817
				}
				else
				{
					if(get_mode_size_bits(mode) == 32) stackparamsize=4;
					else stackparamsize=8;								// mode == irm_D
					reg = NULL;
				}
			}
			else
			{
				if(gpregi <= REG_R10)
Michael Beck's avatar
Michael Beck committed
818
					reg = &ppc32_gp_regs[gpregi++];
819
820
821
822
823
824
825
826
827
				else
					reg = NULL;
				stackparamsize=4;
			}

			if(reg)
				be_abi_call_param_reg(abi, i, reg);
			else
			{
828
				be_abi_call_param_stack(abi, i, mode, 4, stackoffs - lastoffs, 0);
829
830
831
832
833
834
				lastoffs = stackoffs+stackparamsize;
			}
			stackoffs += stackparamsize;
		}
		else
		{
835
			be_abi_call_param_stack(abi, i, mode, 4, stackoffs - lastoffs, 0);
836
837
838
839
840
841
842
843
844
845
846
			stackoffs += (get_type_size_bytes(tp)+3) & -4;
			lastoffs = stackoffs;
		}
	}

	/* explain where result can be found if any */
	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,
Michael Beck's avatar
Michael Beck committed
847
			mode_is_float(mode) ? &ppc32_fp_regs[REG_F1] : &ppc32_gp_regs[REG_R3]);
848
849
850
851
	}
}

static const void *ppc32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
852
853
	(void) self;
	(void) irn;
854
855
856
857
858
859
860
861
	return &ppc32_irn_ops;
}

const arch_irn_handler_t ppc32_irn_handler = {
	ppc32_get_irn_ops
};

const arch_irn_handler_t *ppc32_get_irn_handler(const void *self) {
862
	(void) self;
863
864
865
866
	return &ppc32_irn_handler;
}

int ppc32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
867
	(void) block_env;
868
869
870
871
	if(!is_ppc32_irn(irn))
		return -1;

	return 1;
872
873
874
875
876
877
}

/**
 * Initializes the code generator interface.
 */
static const arch_code_generator_if_t *ppc32_get_code_generator_if(void *self) {
878
	(void) self;
879
880
881
882
883
884
885
886
	return &ppc32_code_gen_if;
}

list_sched_selector_t ppc32_sched_selector;

/**
 * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
 */
Christian Würdig's avatar
Christian Würdig committed
887
static const list_sched_selector_t *ppc32_get_list_sched_selector(const void *self, list_sched_selector_t *selector) {
888
889
	(void) self;
	(void) selector;
890
	ppc32_sched_selector = trivial_selector;
891
892
893
894
	ppc32_sched_selector.to_appear_in_schedule = ppc32_to_appear_in_schedule;
	return &ppc32_sched_selector;
}

895
static const ilp_sched_selector_t *ppc32_get_ilp_sched_selector(const void *self) {
896
	(void) self;
897
898
899
	return NULL;
}

900
901
902
903
904
/**
 * Returns the necessary byte alignment for storing a register of given class.
 */
static int ppc32_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
	ir_mode *mode = arch_register_class_mode(cls);
Michael Beck's avatar
Michael Beck committed
905
906
	(void) self;

907
908
909
	return get_mode_size_bytes(mode);
}

910
static const be_execution_unit_t ***ppc32_get_allowed_execution_units(const void *self, const ir_node *irn) {
911
912
	(void) self;
	(void) irn;
913
914
915
916
917
918
	/* TODO */
	assert(0);
	return NULL;
}

static const be_machine_t *ppc32_get_machine(const void *self) {
919
	(void) self;
920
921
922
923
924
	/* TODO */
	assert(0);
	return NULL;
}

925
926
927
/**
 * Return irp irgs in the desired order.
 */
Christian Würdig's avatar
Christian Würdig committed
928
static ir_graph **ppc32_get_irg_list(const void *self, ir_graph ***irg_list) {
929
930
	(void) self;
	(void) irg_list;
931
932
933
	return NULL;
}

Michael Beck's avatar
Michael Beck committed
934
935
936
937
938
/**
 * Returns the libFirm configuration parameter for this backend.
 */
static const backend_params *ppc32_get_libfirm_params(void) {
	static backend_params p = {
939
940
		1,     /* need dword lowering */
		0,     /* don't support inlien assembler yet */
Michael Beck's avatar
Michael Beck committed
941
942
943
		NULL,  /* no additional opcodes */
		NULL,  /* will be set later */
		NULL,  /* but yet no creator function */
944
		NULL,  /* context for create_intrinsic_fkt */
Michael Beck's avatar
Michael Beck committed
945
		NULL,  /* no if conversion settings */
Michael Beck's avatar
Michael Beck committed
946
947
948
949
950
	};

	return &p;
}

951
952
953
954
955
956
957
958
959
960
const arch_isa_if_t ppc32_isa_if = {
	ppc32_init,
	ppc32_done,
	ppc32_get_n_reg_class,
	ppc32_get_reg_class,
	ppc32_get_reg_class_for_mode,
	ppc32_get_call_abi,
	ppc32_get_irn_handler,
	ppc32_get_code_generator_if,
	ppc32_get_list_sched_selector,
961
	ppc32_get_ilp_sched_selector,
962
	ppc32_get_reg_class_alignment,
Michael Beck's avatar
Michael Beck committed
963
	ppc32_get_libfirm_params,
964
965
	ppc32_get_allowed_execution_units,
	ppc32_get_machine,
966
	ppc32_get_irg_list,
967
};
968
969
970
971
972
973
974

void be_init_arch_ppc32(void)
{
	be_register_isa_if("ppc32", &ppc32_isa_if);
}

BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_ppc32);