mips_transform.c 38.8 KB
Newer Older
Christoph Mallon's avatar
Christoph Mallon committed
1
2
3
4
5
6
7
8
9
10
11
12
/*
 * This file is part of libFirm.
 * Copyright (C) 2017 University of Karlsruhe.
 */

#include "mips_transform.h"

#include "beirg.h"
#include "benode.h"
#include "betranshlp.h"
#include "gen_mips_new_nodes.h"
#include "gen_mips_regalloc_if.h"
Christoph Mallon's avatar
Christoph Mallon committed
13
#include "irprog_t.h"
14
#include "mips_bearch_t.h"
15
#include "mips_cconv.h"
Christoph Mallon's avatar
Christoph Mallon committed
16
17
18
19
#include "nodes.h"
#include "panic.h"
#include "util.h"

20
21
static mips_calling_convention_t cur_cconv;

Christoph Mallon's avatar
Christoph Mallon committed
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
static be_stack_env_t stack_env;

static unsigned const callee_saves[] = {
	REG_S0,
	REG_S1,
	REG_S2,
	REG_S3,
	REG_S4,
	REG_S5,
	REG_S6,
	REG_S7,
	REG_S8,
};

static unsigned const caller_saves[] = {
	REG_V0,
	REG_V1,
	REG_A0,
	REG_A1,
	REG_A2,
	REG_A3,
	REG_T0,
	REG_T1,
	REG_T2,
	REG_T3,
	REG_T4,
	REG_T5,
	REG_T6,
	REG_T7,
	REG_T8,
	REG_T9,
	REG_RA,
};

static ir_node *get_Start_sp(ir_graph *const irg)
{
	return be_get_Start_proj(irg, &mips_registers[REG_SP]);
}

61
62
63
64
65
static ir_node *get_Start_zero(ir_graph *const irg)
{
	return be_get_Start_proj(irg, &mips_registers[REG_ZERO]);
}

Christoph Mallon's avatar
Christoph Mallon committed
66
67
68
69
70
static inline bool is_uimm5(long const val)
{
	return 0 <= val && val < 32;
}

71
72
73
74
75
76
77
78
79
80
static inline bool is_simm16(long const val)
{
	return -32768 <= val && val < 32768;
}

static inline bool is_uimm16(long const val)
{
	return 0 <= val && val < 65536;
}

81
82
83
84
85
static inline bool is_imm_lui(long const val)
{
	return (val & 0xFFFFU) == 0;
}

86
87
88
89
90
91
92
93
94
typedef struct mips_addr {
	ir_node   *base;
	ir_entity *ent;
	int32_t    val;
} mips_addr;

static mips_addr make_addr(ir_node *addr)
{
	ir_entity *ent = 0;
95
96
97
98
99
100
101
102
103
104
105
106
107
	int32_t    val = 0;

	if (is_Add(addr)) {
		ir_node *const r = get_Add_right(addr);
		if (is_Const(r)) {
			long const v = get_Const_long(r);
			if (is_simm16(v)) {
				val  = v;
				addr = get_Add_left(addr);
			}
		}
	}

108
109
110
111
112
	if (is_Member(addr)) {
		ent  = get_Member_entity(addr);
		addr = get_Member_ptr(addr);
		assert(is_Proj(addr) && get_Proj_num(addr) == pn_Start_P_frame_base && is_Start(get_Proj_pred(addr)));
	}
113

114
	ir_node *const base = be_transform_node(addr);
115
	return (mips_addr){ base, ent, val };
116
117
}

Christoph Mallon's avatar
Christoph Mallon committed
118
119
120
121
122
123
124
125
static ir_node *make_address(ir_node const *const node, ir_entity *const ent, int32_t const val)
{
	dbg_info *const dbgi  = get_irn_dbg_info(node);
	ir_node  *const block = be_transform_nodes_block(node);
	ir_node  *const lui   = new_bd_mips_lui(dbgi, block, ent, val);
	return new_bd_mips_addiu(dbgi, block, lui, ent, val);
}

126
127
128
129
130
131
132
133
static ir_node *make_extension(dbg_info *const dbgi, ir_node *const op, unsigned const to_size)
{
	ir_node *const new_op  = be_transform_node(op);
	ir_mode *const op_mode = get_irn_mode(op);
	unsigned const op_size = get_mode_size_bits(op_mode);
	if (op_size >= to_size)
		return new_op;

134
135
136
137
138
139
140
	/* Check whether the value is correctly extended already. */
	if (is_Proj(op)) {
		ir_node *const pred = get_Proj_pred(op);
		if (is_Load(pred))
			return new_op;
	}

141
142
143
144
145
146
147
148
149
150
151
152
	assert(op_size <= 16);
	ir_node *const block = get_nodes_block(new_op);
	if (mode_is_signed(op_mode)) {
		int32_t  const val = MIPS_MACHINE_SIZE - op_size;
		ir_node *const sll = new_bd_mips_sll(dbgi, block, new_op, NULL, val);
		ir_node *const sra = new_bd_mips_sra(dbgi, block, sll,    NULL, val);
		return sra;
	} else {
		return new_bd_mips_andi(dbgi, block, new_op, NULL, (1U << op_size) - 1);
	}
}

153
154
155
156
157
static ir_node *extend_value(ir_node *const val)
{
	return make_extension(NULL, val, MIPS_MACHINE_SIZE);
}

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
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
static void mips_parse_constraint_letter(void const *const env, be_asm_constraint_t* const c, char const l)
{
	(void)env;

	switch (l) {
	case 'c':
		c->cls               = &mips_reg_classes[CLASS_mips_gp];
		c->allowed_registers = 1U << REG_GP_T9;
		break;

	case 'g':
		c->all_registers_allowed = true;
		c->memory_possible       = true;
		/* FALLTHROUGH */
	case 'I':
	case 'J':
	case 'K':
	case 'L':
	case 'M':
	case 'N':
	case 'O':
	case 'P':
	case 'i':
	case 'n':
		c->cls            = &mips_reg_classes[CLASS_mips_gp];
		c->immediate_type = l;
		break;

	case 'R':
	case 'm':
		c->memory_possible = true;
		break;

	case 'd':
	case 'r':
	case 'y':
		c->cls                   = &mips_reg_classes[CLASS_mips_gp];
		c->all_registers_allowed = true;
		break;

	case 'v':
		c->cls               = &mips_reg_classes[CLASS_mips_gp];
		c->allowed_registers = 1U << REG_GP_V1;
		break;

	default:
		panic("unknown asm constraint '%c'", l);
	}
}

static bool mips_check_immediate_constraint(long const val, char const imm_type)
{
	switch (imm_type) {
	case 'I': return is_simm16(val);
	case 'J': return val == 0;
	case 'K': return is_uimm16(val);
	case 'L': return is_imm_lui(val);
	case 'M': return !is_simm16(val) && !is_uimm16(val) && !is_imm_lui(val);
	case 'N': return -65535 <= val && val < 0;
	case 'O': return -16384 <= val && val < 16384;
	case 'P': return      1 <= val && val < 65536;

	case 'g':
	case 'i':
	case 'n': return true;
	}
	panic("invalid immediate constraint found");
}

static bool mips_match_immediate(mips_asm_operand_t *const operand, ir_node *const node, char const imm_type)
{
	ir_tarval *offset;
	ir_entity *entity;
	unsigned   reloc_kind;
	if (!be_match_immediate(node, &offset, &entity, &reloc_kind))
		return false;
	assert(reloc_kind == 0);

	if (entity && imm_type != 'g' && imm_type != 'i')
		return false;

	long value;
	if (offset) {
		value = get_tarval_long(offset);
		if (!mips_check_immediate_constraint(value, imm_type))
			return false;
	} else {
		value = 0;
	}

	operand->kind = BE_ASM_OPERAND_IMMEDIATE;
	operand->val  = value;
	operand->ent  = entity;
	return true;
}

static void parse_asm_constraints(be_asm_constraint_t *const constraint, ident *const constraint_text, bool const is_output)
{
	be_parse_asm_constraints_internal(constraint, constraint_text, is_output, &mips_parse_constraint_letter, NULL);
}

static ir_node *gen_ASM(ir_node *const node)
{
	if (get_ASM_n_clobbers(node) != 0)
Christoph Mallon's avatar
Christoph Mallon committed
262
		TODO(node);
263
264
265
266
267
268
269
270
271
272
273
274
275
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

	unsigned            const n_operands = be_count_asm_operands(node);
	ir_graph           *const irg        = get_irn_irg(node);
	struct obstack     *const obst       = get_irg_obstack(irg);
	mips_asm_operand_t *const operands   = NEW_ARR_DZ(mips_asm_operand_t, obst, n_operands);

	arch_register_req_t const      **out_reqs          = NEW_ARR_F(arch_register_req_t const*, 0);
	ir_asm_constraint   const *const out_constraints   = get_ASM_output_constraints(node);
	size_t              const  const n_out_constraints = get_ASM_n_output_constraints(node);
	for (size_t o = 0; o != n_out_constraints; ++o) {
		ir_asm_constraint const *const constraint = &out_constraints[o];
		be_asm_constraint_t            parsed_constraint;
		parse_asm_constraints(&parsed_constraint, constraint->constraint, true);

		arch_register_req_t const *const req = be_make_register_req(obst, &parsed_constraint, n_out_constraints, out_reqs, o);
		ARR_APP1(arch_register_req_t const*, out_reqs, req);

		mips_asm_operand_t *const operand = &operands[constraint->pos];
		operand->kind = BE_ASM_OPERAND_OUTPUT_VALUE;
		operand->pos  = o;
	}

	ir_node                        **in             = NEW_ARR_F(ir_node*, 0);
	arch_register_req_t const      **in_reqs        = NEW_ARR_F(arch_register_req_t const*, 0);
	ir_asm_constraint   const *const in_constraints = get_ASM_input_constraints(node);
	int                        const n_inputs       = get_ASM_n_inputs(node);
	for (int i = 0; i < n_inputs; ++i) {
		ir_node                 *const pred       = get_ASM_input(node, i);
		ir_asm_constraint const *const constraint = &in_constraints[i];

		be_asm_constraint_t parsed_constraint;
		parse_asm_constraints(&parsed_constraint, constraint->constraint, false);

		mips_asm_operand_t *const operand = &operands[constraint->pos];

		char const imm_type = parsed_constraint.immediate_type;
		if (imm_type != '\0' && mips_match_immediate(operand, pred, imm_type))
			continue;

		ir_node             *const new_pred = be_transform_node(pred);
		be_asm_operand_kind_t      kind     = BE_ASM_OPERAND_INPUT_VALUE;
		arch_register_req_t const *req      = be_make_register_req(obst, &parsed_constraint, n_out_constraints, out_reqs, i);
		if (req == arch_no_register_req) {
			kind = BE_ASM_OPERAND_MEMORY;
			req  = arch_get_irn_register_req(new_pred)->cls->class_req;
		}

		operand->kind = kind;
		operand->pos  = ARR_LEN(in);
		ARR_APP1(ir_node*, in, new_pred);
		ARR_APP1(arch_register_req_t const*, in_reqs, req);
	}

	return be_make_asm(node, in, in_reqs, out_reqs, operands);
}

319
320
static ir_node *gen_Add(ir_node *const node)
{
Christoph Mallon's avatar
Christoph Mallon committed
321
322
323
324
325
326
327
328
329
	ir_tarval *tv;
	ir_entity *ent;
	unsigned   reloc_kind;
	if (be_match_immediate(node, &tv, &ent, &reloc_kind)) {
		assert(reloc_kind == 0);
		int32_t const val = get_tarval_long(tv);
		return make_address(node, ent, val);
	}

330
331
332
333
334
335
336
337
338
339
	ir_node *const l    = get_Add_left(node);
	ir_node *const r    = get_Add_right(node);
	ir_mode *const mode = get_irn_mode(node);
	if (be_mode_needs_gp_reg(mode)) {
		dbg_info *const dbgi  = get_irn_dbg_info(node);
		ir_node  *const block = be_transform_nodes_block(node);
		ir_node  *const new_l = be_transform_node(l);
		if (is_Const(r)) {
			long const val = get_Const_long(r);
			if (is_simm16(val))
Christoph Mallon's avatar
Christoph Mallon committed
340
				return new_bd_mips_addiu(dbgi, block, new_l, NULL, val);
341
342
343
344
		}
		ir_node *const new_r = be_transform_node(r);
		return new_bd_mips_addu(dbgi, block, new_l, new_r);
	}
Christoph Mallon's avatar
Christoph Mallon committed
345
	TODO(node);
346
347
}

Christoph Mallon's avatar
Christoph Mallon committed
348
349
350
351
352
353
static ir_node *gen_Address(ir_node *const node)
{
	ir_entity *const ent = get_Address_entity(node);
	return make_address(node, ent, 0);
}

354
typedef ir_node *cons_binop(dbg_info*, ir_node*, ir_node*, ir_node*);
Christoph Mallon's avatar
Christoph Mallon committed
355
typedef ir_node *cons_binop_imm(dbg_info*, ir_node*, ir_node*, ir_entity*, int32_t);
356
357
358
359
360
361
362
363
364
365
366

static ir_node *gen_logic_op(ir_node *const node, cons_binop *const cons, cons_binop_imm *const cons_imm)
{
	dbg_info *const dbgi  = get_irn_dbg_info(node);
	ir_node  *const block = be_transform_nodes_block(node);
	ir_node  *const l     = get_binop_left(node);
	ir_node  *const new_l = be_transform_node(l);
	ir_node  *const r     = get_binop_right(node);
	if (is_Const(r)) {
		long const val = get_Const_long(r);
		if (is_uimm16(val))
Christoph Mallon's avatar
Christoph Mallon committed
367
			return cons_imm(dbgi, block, new_l, NULL, val);
368
369
370
371
372
373
374
375
376
377
	}
	ir_node *const new_r = be_transform_node(r);
	return cons(dbgi, block, new_l, new_r);
}

static ir_node *gen_And(ir_node *const node)
{
	return gen_logic_op(node, &new_bd_mips_and, &new_bd_mips_andi);
}

378
static ir_node *gen_saturating_increment(ir_node *const node)
379
380
381
382
383
384
385
386
387
388
389
{
  dbg_info *const dbgi    = get_irn_dbg_info(node);
  ir_node  *const block   = be_transform_nodes_block(node);
  ir_node  *const operand = be_transform_node(get_Builtin_param(node, 0));
  assert(get_mode_size_bits(get_irn_mode(operand)) == 32);

  ir_node *const sltiu = new_bd_mips_sltiu(dbgi, block, operand, NULL, -1);
  ir_node *const addu  = new_bd_mips_addu( dbgi, block, operand, sltiu);
  return addu;
}

390
391
392
393
static ir_node *gen_Builtin(ir_node *const node)
{
	ir_builtin_kind const kind = get_Builtin_kind(node);
	switch (kind) {
394
	case ir_bk_saturating_increment: return gen_saturating_increment(node);
395

396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
	case ir_bk_bswap:
	case ir_bk_clz:
	case ir_bk_compare_swap:
	case ir_bk_ctz:
	case ir_bk_debugbreak:
	case ir_bk_ffs:
	case ir_bk_frame_address:
	case ir_bk_inport:
	case ir_bk_may_alias:
	case ir_bk_outport:
	case ir_bk_parity:
	case ir_bk_popcount:
	case ir_bk_prefetch:
	case ir_bk_return_address:
	case ir_bk_trap:
	case ir_bk_va_arg:
	case ir_bk_va_start:
Christoph Mallon's avatar
Christoph Mallon committed
413
		TODO(node);
414
415
416
417
	}
	panic("unexpected Builtin");
}

Christoph Mallon's avatar
Christoph Mallon committed
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
static ir_node *gen_Call(ir_node *const node)
{
	ir_graph *const irg = get_irn_irg(node);

	unsigned                          p        = n_mips_jal_first_argument;
	unsigned                    const n_params = get_Call_n_params(node);
	unsigned                    const n_ins    = p + 1 + n_params;
	arch_register_req_t const **const reqs     = be_allocate_in_reqs(irg, n_ins);
	ir_node                          *ins[n_ins];

	ir_entity     *callee;
	ir_node *const ptr = get_Call_ptr(node);
	if (is_Address(ptr)) {
		callee = get_Address_entity(ptr);
	} else {
		callee  = NULL;
		ins[p]  = be_transform_node(ptr);
		reqs[p] = &mips_class_reg_req_gp;
		++p;
	}

439
440
441
442
443
444
	mips_calling_convention_t cconv;
	ir_type            *const fun_type = get_Call_type(node);
	mips_determine_calling_convention(&cconv, fun_type);

	ir_node *mems[1 + cconv.n_mem_param];
	unsigned m = 0;
Christoph Mallon's avatar
Christoph Mallon committed
445
446
447
448

	ir_node *const mem = get_Call_mem(node);
	mems[m++] = be_transform_node(mem);

449
	int      const frame_size = cconv.param_stack_size;
Christoph Mallon's avatar
Christoph Mallon committed
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
	ir_node *const block      = be_transform_nodes_block(node);
	ir_node *const sp         = get_Start_sp(irg);
	ir_node *const call_frame = be_new_IncSP(&mips_registers[REG_SP], block, sp, frame_size, 0);

	ins[n_mips_jal_stack]  = call_frame;
	reqs[n_mips_jal_stack] = &mips_single_reg_req_gp_sp;

	dbg_info *const dbgi = get_irn_dbg_info(node);
	for (size_t i = 0; i != n_params; ++i) {
		mips_reg_or_slot_t const *const param = &cconv.parameters[i];
		ir_node                  *const arg   = get_Call_param(node, i);

		ir_mode *const arg_mode = get_irn_mode(arg);
		unsigned const size     = get_mode_size_bits(arg_mode);
		if (size != 32)
			panic("unsupported parameter size");

		ir_node *const val = be_transform_node(arg);
		if (param->reg) {
			ins[p]  = val;
			reqs[p] = param->reg->single_req;
			++p;
		} else {
			ir_node *const nomem = get_irg_no_mem(irg);
			mems[m++] = new_bd_mips_sw(dbgi, block, nomem, call_frame, val, NULL, param->offset);
		}
	}

	mips_free_calling_convention(&cconv);

	ins[n_mips_jal_mem]  = be_make_Sync(block, m, mems);
	reqs[n_mips_jal_mem] = arch_memory_req;

	unsigned const n_res = pn_mips_jal_first_result + ARRAY_SIZE(caller_saves);

	ir_node *const jal = callee ?
		new_bd_mips_jal( dbgi, block, p, ins, reqs, n_res, callee, 0) :
		new_bd_mips_jalr(dbgi, block, p, ins, reqs, n_res);

	arch_set_irn_register_req_out(jal, pn_mips_jal_M, arch_memory_req);
	arch_copy_irn_out_info(jal, pn_mips_jal_stack, sp);
	for (size_t i = 0; i != ARRAY_SIZE(caller_saves); ++i) {
		arch_set_irn_register_req_out(jal, pn_mips_jal_first_result + i, mips_registers[caller_saves[i]].single_req);
	}

	ir_node *const jal_stack = be_new_Proj(jal, pn_mips_jal_stack);
	ir_node *const new_stack = be_new_IncSP(&mips_registers[REG_SP], block, jal_stack, -frame_size, 0);
	be_stack_record_chain(&stack_env, call_frame, n_be_IncSP_pred, new_stack);

	return jal;
}

502
503
504
505
506
static ir_node *gen_Cmp(ir_node *const node)
{
	ir_node       *l    = get_Cmp_left(node);
	ir_node       *r    = get_Cmp_right(node);
	ir_mode *const mode = get_irn_mode(l);
507
	if (be_mode_needs_gp_reg(mode)) {
508
		ir_relation const rel = get_Cmp_relation(node) & ir_relation_less_equal_greater;
509
510
511
512
513
514
515
516
517
518
519
		switch (rel) {
		case ir_relation_greater:
		case ir_relation_less_equal: {
			ir_node *const t = l;
			l = r;
			r = t;
		} /* FALLTHROUGH */
		case ir_relation_greater_equal:
		case ir_relation_less: {
			dbg_info *const dbgi  = get_irn_dbg_info(node);
			ir_node  *const block = be_transform_nodes_block(node);
520
521
			ir_node  *const new_l = extend_value(l);
			ir_node  *const new_r = extend_value(r);
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
			if (mode_is_signed(mode)) {
				return new_bd_mips_slt(dbgi, block, new_l, new_r);
			} else {
				return new_bd_mips_sltu(dbgi, block, new_l, new_r);
			}
		}

		case ir_relation_equal:
		case ir_relation_false:
		case ir_relation_less_equal_greater:
		case ir_relation_less_greater:
		case ir_relation_true:
		case ir_relation_unordered:
		case ir_relation_unordered_equal:
		case ir_relation_unordered_greater:
		case ir_relation_unordered_greater_equal:
		case ir_relation_unordered_less:
		case ir_relation_unordered_less_equal:
		case ir_relation_unordered_less_greater:
			panic("unexpected relation");
		}
	}
Christoph Mallon's avatar
Christoph Mallon committed
544
	TODO(node);
545
546
}

547
548
549
550
551
552
static ir_node *gen_Cond(ir_node *const node)
{
	ir_node *const sel = get_Cond_selector(node);
	if (is_Cmp(sel)) {
		ir_node *const l    = get_Cmp_left(sel);
		ir_mode *const mode = get_irn_mode(l);
553
		if (be_mode_needs_gp_reg(mode)) {
554
			ir_relation    rel = get_Cmp_relation(sel) & ir_relation_less_equal_greater;
555
556
557
558
559
560
561
562
563
			ir_node *const r   = get_Cmp_right(sel);
			if (is_irn_null(r)) {
				if (mode_is_signed(mode)) {
					switch (rel) {
						mips_cond_t cc;
					case ir_relation_greater:       cc = mips_cc_gtz; goto bccrelz;
					case ir_relation_greater_equal: cc = mips_cc_gez; goto bccrelz;
					case ir_relation_less:          cc = mips_cc_ltz; goto bccrelz;
					case ir_relation_less_equal:    cc = mips_cc_lez; goto bccrelz;
564
bccrelz:;
565
566
						dbg_info *const dbgi  = get_irn_dbg_info(node);
						ir_node  *const block = be_transform_nodes_block(node);
567
						ir_node  *const new_l = extend_value(l);
568
						return new_bd_mips_bcc_z(dbgi, block, new_l, cc);
569

570
571
572
573
574
575
576
					default: goto normal;
					}
				} else {
					/* Handle 'x >u 0' as cheaper 'x != 0'. */
					if (rel == ir_relation_greater)
						rel = ir_relation_less_greater;
					goto normal;
577
578
579
580
581
582
583
584
				}
			} else {
normal:
				switch (rel) {
				{
					mips_cond_t cc;
				case ir_relation_equal:        cc = mips_cc_eq; goto bcc;
				case ir_relation_less_greater: cc = mips_cc_ne; goto bcc;
585
bcc:;
586
587
					dbg_info *const dbgi  = get_irn_dbg_info(node);
					ir_node  *const block = be_transform_nodes_block(node);
588
589
					ir_node  *const new_l = extend_value(l);
					ir_node  *const new_r = extend_value(r);
590
591
592
					return new_bd_mips_bcc(dbgi, block, new_l, new_r, cc);
				}

593
594
595
596
597
598
599
600
601
602
603
604
605
606
				{
					mips_cond_t cc;
				case ir_relation_greater:       cc = mips_cc_ne; goto bcceqz;
				case ir_relation_greater_equal: cc = mips_cc_eq; goto bcceqz;
				case ir_relation_less:          cc = mips_cc_ne; goto bcceqz;
				case ir_relation_less_equal:    cc = mips_cc_eq; goto bcceqz;
bcceqz:;
					dbg_info *const dbgi    = get_irn_dbg_info(node);
					ir_node  *const block   = be_transform_nodes_block(node);
					ir_node  *const new_sel = be_transform_node(sel);
					ir_graph *const irg     = get_irn_irg(node);
					ir_node  *const zero    = get_Start_zero(irg);
					return new_bd_mips_bcc(dbgi, block, new_sel, zero, cc);
				}
607

608
609
610
611
612
613
614
615
616
617
618
619
				case ir_relation_false:
				case ir_relation_less_equal_greater:
				case ir_relation_true:
				case ir_relation_unordered:
				case ir_relation_unordered_equal:
				case ir_relation_unordered_greater:
				case ir_relation_unordered_greater_equal:
				case ir_relation_unordered_less:
				case ir_relation_unordered_less_equal:
				case ir_relation_unordered_less_greater:
					panic("unexpected relation");
				}
620
621
622
			}
		}
	}
Christoph Mallon's avatar
Christoph Mallon committed
623
	TODO(node);
624
625
}

Christoph Mallon's avatar
Christoph Mallon committed
626
627
628
629
630
631
static ir_node *gen_Conv(ir_node *const node)
{
	ir_node *const op      = get_Conv_op(node);
	ir_mode *const op_mode = get_irn_mode(op);
	ir_mode *const mode    = get_irn_mode(node);
	if (be_mode_needs_gp_reg(op_mode) && be_mode_needs_gp_reg(mode)) {
632
633
		dbg_info *const dbgi = get_irn_dbg_info(node);
		return make_extension(dbgi, op, get_mode_size_bits(mode));
Christoph Mallon's avatar
Christoph Mallon committed
634
	}
Christoph Mallon's avatar
Christoph Mallon committed
635
	TODO(node);
Christoph Mallon's avatar
Christoph Mallon committed
636
637
}

638
639
static ir_node *gen_Const(ir_node *const node)
{
640
	ir_mode *const mode = get_irn_mode(node);
641
642
643
644
645
	if (be_mode_needs_gp_reg(mode)) {
		long const val = get_Const_long(node);
		if (val == 0) {
			ir_graph *const irg = get_irn_irg(node);
			return get_Start_zero(irg);
646
647
648
649
650
		} else if (is_simm16(val)) {
			dbg_info *const dbgi  = get_irn_dbg_info(node);
			ir_node  *const block = be_transform_nodes_block(node);
			ir_graph *const irg   = get_irn_irg(node);
			ir_node  *const zero  = get_Start_zero(irg);
Christoph Mallon's avatar
Christoph Mallon committed
651
			return new_bd_mips_addiu(dbgi, block, zero, NULL, val);
652
653
654
655
656
657
		} else {
			ir_node        *res;
			dbg_info *const dbgi  = get_irn_dbg_info(node);
			ir_node  *const block = be_transform_nodes_block(node);
			int32_t   const hi    = (uint32_t)val >> 16;
			if (hi != 0) {
Christoph Mallon's avatar
Christoph Mallon committed
658
				res = new_bd_mips_lui(dbgi, block, NULL, hi);
659
660
661
662
663
664
			} else {
				ir_graph *const irg = get_irn_irg(node);
				res = get_Start_zero(irg);
			}
			int32_t const lo = val & 0xFFFF;
			if (lo != 0)
Christoph Mallon's avatar
Christoph Mallon committed
665
				res = new_bd_mips_ori(dbgi, block, res, NULL, lo);
666
			return res;
667
668
		}
	}
Christoph Mallon's avatar
Christoph Mallon committed
669
	TODO(node);
670
671
}

Christoph Mallon's avatar
Christoph Mallon committed
672
673
674
675
676
677
678
679
680
681
682
683
684
685
static ir_node *gen_Div(ir_node *const node)
{
	ir_mode *const mode = get_Div_resmode(node);
	if (be_mode_needs_gp_reg(mode) && get_mode_size_bits(mode) == MIPS_MACHINE_SIZE) {
		dbg_info *const dbgi  = get_irn_dbg_info(node);
		ir_node  *const block = be_transform_nodes_block(node);
		ir_node  *const l     = be_transform_node(get_Div_left(node));
		ir_node  *const r     = be_transform_node(get_Div_right(node));
		if (mode_is_signed(mode)) {
			return new_bd_mips_div_lo(dbgi, block, l, r);
		} else {
			return new_bd_mips_divu_lo(dbgi, block, l, r);
		}
	}
Christoph Mallon's avatar
Christoph Mallon committed
686
	TODO(node);
Christoph Mallon's avatar
Christoph Mallon committed
687
688
}

689
690
691
692
693
static ir_node *gen_Eor(ir_node *const node)
{
	return gen_logic_op(node, &new_bd_mips_xor, &new_bd_mips_xori);
}

Christoph Mallon's avatar
Christoph Mallon committed
694
695
696
697
698
699
700
701
static ir_node *gen_IJmp(ir_node *const node)
{
	dbg_info *const dbgi  = get_irn_dbg_info(node);
	ir_node  *const block = be_transform_nodes_block(node);
	ir_node  *const tgt   = be_transform_node(get_IJmp_target(node));
	return new_bd_mips_ijmp(dbgi, block, tgt);
}

702
703
704
705
706
707
708
static ir_node *gen_Jmp(ir_node *const node)
{
	dbg_info *const dbgi  = get_irn_dbg_info(node);
	ir_node  *const block = be_transform_nodes_block(node);
	return new_bd_mips_b(dbgi, block);
}

Christoph Mallon's avatar
Christoph Mallon committed
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
typedef ir_node *cons_loadop(dbg_info*, ir_node*, ir_node*, ir_node*, ir_entity*, int32_t);

static ir_node *gen_Load(ir_node *const node)
{
	ir_mode *const mode = get_Load_mode(node);
	if (be_mode_needs_gp_reg(mode)) {
		cons_loadop   *cons;
		unsigned const size = get_mode_size_bits(mode);
		if (size == 8) {
			cons = mode_is_signed(mode) ? &new_bd_mips_lb : &new_bd_mips_lbu;
		} else if (size == 16) {
			cons = mode_is_signed(mode) ? &new_bd_mips_lh : &new_bd_mips_lhu;
		} else if (size == 32) {
			cons = new_bd_mips_lw;
		} else {
			panic("invalid load");
		}
		dbg_info *const dbgi  = get_irn_dbg_info(node);
		ir_node  *const block = be_transform_nodes_block(node);
		ir_node  *const mem   = be_transform_node(get_Load_mem(node));
729
730
		mips_addr const addr  = make_addr(get_Load_ptr(node));
		return cons(dbgi, block, mem, addr.base, addr.ent, addr.val);
Christoph Mallon's avatar
Christoph Mallon committed
731
	}
Christoph Mallon's avatar
Christoph Mallon committed
732
	TODO(node);
Christoph Mallon's avatar
Christoph Mallon committed
733
734
}

Christoph Mallon's avatar
Christoph Mallon committed
735
736
737
738
739
740
741
742
743
744
745
746
747
748
static ir_node *gen_Mod(ir_node *const node)
{
	ir_mode *const mode = get_Mod_resmode(node);
	if (be_mode_needs_gp_reg(mode) && get_mode_size_bits(mode) == MIPS_MACHINE_SIZE) {
		dbg_info *const dbgi  = get_irn_dbg_info(node);
		ir_node  *const block = be_transform_nodes_block(node);
		ir_node  *const l     = be_transform_node(get_Mod_left(node));
		ir_node  *const r     = be_transform_node(get_Mod_right(node));
		if (mode_is_signed(mode)) {
			return new_bd_mips_div_hi(dbgi, block, l, r);
		} else {
			return new_bd_mips_divu_hi(dbgi, block, l, r);
		}
	}
Christoph Mallon's avatar
Christoph Mallon committed
749
	TODO(node);
Christoph Mallon's avatar
Christoph Mallon committed
750
751
}

Christoph Mallon's avatar
Christoph Mallon committed
752
753
754
755
756
757
758
759
760
761
762
static ir_node *gen_Member(ir_node *const node)
{
	ir_node *const ptr = get_Member_ptr(node);
	assert(is_Proj(ptr) && get_Proj_num(ptr) == pn_Start_P_frame_base && is_Start(get_Proj_pred(ptr)));
	dbg_info  *const dbgi  = get_irn_dbg_info(node);
	ir_node   *const block = be_transform_nodes_block(node);
	ir_node   *const frame = be_transform_node(ptr);
	ir_entity *const ent   = get_Member_entity(node);
	return new_bd_mips_addiu(dbgi, block, frame, ent, 0);
}

Christoph Mallon's avatar
Christoph Mallon committed
763
764
765
766
767
768
769
770
771
772
773
774
775
776
static ir_node *gen_Mulh(ir_node *const node)
{
	ir_mode *const mode = get_irn_mode(node);
	if (be_mode_needs_gp_reg(mode) && get_mode_size_bits(mode) == MIPS_MACHINE_SIZE) {
		dbg_info *const dbgi  = get_irn_dbg_info(node);
		ir_node  *const block = be_transform_nodes_block(node);
		ir_node  *const l     = be_transform_node(get_Mulh_left(node));
		ir_node  *const r     = be_transform_node(get_Mulh_right(node));
		if (mode_is_signed(mode)) {
			return new_bd_mips_mult_hi(dbgi, block, l, r);
		} else {
			return new_bd_mips_multu_hi(dbgi, block, l, r);
		}
	}
Christoph Mallon's avatar
Christoph Mallon committed
777
	TODO(node);
Christoph Mallon's avatar
Christoph Mallon committed
778
779
}

Christoph Mallon's avatar
Christoph Mallon committed
780
781
782
783
784
785
786
787
788
789
static ir_node *gen_Mul(ir_node *const node)
{
	ir_mode *const mode = get_irn_mode(node);
	if (be_mode_needs_gp_reg(mode)) {
		dbg_info *const dbgi  = get_irn_dbg_info(node);
		ir_node  *const block = be_transform_nodes_block(node);
		ir_node  *const l     = be_transform_node(get_Mul_left(node));
		ir_node  *const r     = be_transform_node(get_Mul_right(node));
		return new_bd_mips_mult_lo(dbgi, block, l, r);
	}
Christoph Mallon's avatar
Christoph Mallon committed
790
	TODO(node);
Christoph Mallon's avatar
Christoph Mallon committed
791
792
}

793
794
795
796
797
798
799
800
801
802
803
804
static ir_node *gen_Minus(ir_node *const node)
{
	ir_node *const val  = get_Minus_op(node);
	ir_mode *const mode = get_irn_mode(node);
	if (be_mode_needs_gp_reg(mode)) {
		dbg_info *const dbgi  = get_irn_dbg_info(node);
		ir_node  *const block = be_transform_nodes_block(node);
		ir_graph *const irg   = get_irn_irg(node);
		ir_node  *const new_l = get_Start_zero(irg);
		ir_node  *const new_r = be_transform_node(val);
		return new_bd_mips_subu(dbgi, block, new_l, new_r);
	}
Christoph Mallon's avatar
Christoph Mallon committed
805
	TODO(node);
806
807
}

808
809
810
811
812
813
814
static ir_node *gen_Mux(ir_node *const node)
{
	ir_mode *const mode = get_irn_mode(node);
	if (be_mode_needs_gp_reg(mode)) {
		if (is_irn_null(get_Mux_false(node)) && is_irn_one(get_Mux_true(node))) {
			ir_node *const sel = get_Mux_sel(node);
			if (is_Cmp(sel)) {
815
				ir_relation const rel = get_Cmp_relation(sel) & ir_relation_less_equal_greater;
816
817
818
819
820
				if (rel == ir_relation_less || rel == ir_relation_greater)
					return be_transform_node(sel);
			}
		}
	}
Christoph Mallon's avatar
Christoph Mallon committed
821
	TODO(node);
822
823
}

Christoph Mallon's avatar
Christoph Mallon committed
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
static ir_node *gen_Not(ir_node *const node)
{
	dbg_info *const dbgi    = get_irn_dbg_info(node);
	ir_node  *const block   = be_transform_nodes_block(node);
	ir_node  *const old_val = get_Not_op(node);
	if (is_Or(old_val)) {
		/* ~(l | r) -> nor(l, r) */
		ir_node *const old_l = get_Or_left(old_val);
		ir_node *const l     = be_transform_node(old_l);
		ir_node *const old_r = get_Or_right(old_val);
		ir_node *const r     = be_transform_node(old_r);
		return new_bd_mips_nor(dbgi, block, l, r);
	}
	/* ~v -> nor(v, v) */
	ir_node *const val = be_transform_node(old_val);
	return new_bd_mips_nor(dbgi, block, val, val);
}

842
843
844
845
846
static ir_node *gen_Or(ir_node *const node)
{
	return gen_logic_op(node, &new_bd_mips_or, &new_bd_mips_ori);
}

847
848
849
850
851
852
853
854
855
856
857
858
859
860
static ir_node *gen_Phi(ir_node *const node)
{
	arch_register_req_t const *req;
	ir_mode            *const  mode = get_irn_mode(node);
	if (be_mode_needs_gp_reg(mode)) {
		req = &mips_class_reg_req_gp;
	} else if (mode == mode_M) {
		req = arch_memory_req;
	} else {
		panic("unhandled mode");
	}
	return be_transform_phi(node, req);
}

861
862
863
864
865
866
static ir_node *gen_Proj_Builtin(ir_node *const node)
{
	ir_node         *const pred     = get_Proj_pred(node);
	ir_node         *const new_pred = be_transform_node(pred);
	ir_builtin_kind  const kind     = get_Builtin_kind(pred);
	switch (kind) {
867
868
869
870
	case ir_bk_saturating_increment:
		assert(get_Proj_num(node) == pn_Builtin_max + 1);
		return new_pred;

871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
	case ir_bk_bswap:
	case ir_bk_clz:
	case ir_bk_compare_swap:
	case ir_bk_ctz:
	case ir_bk_debugbreak:
	case ir_bk_ffs:
	case ir_bk_frame_address:
	case ir_bk_inport:
	case ir_bk_may_alias:
	case ir_bk_outport:
	case ir_bk_parity:
	case ir_bk_popcount:
	case ir_bk_prefetch:
	case ir_bk_return_address:
	case ir_bk_trap:
	case ir_bk_va_arg:
	case ir_bk_va_start:
Christoph Mallon's avatar
Christoph Mallon committed
888
		TODO(node);
889
890
891
892
	}
	panic("unexpected Builtin");
}

Christoph Mallon's avatar
Christoph Mallon committed
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
static ir_node *gen_Proj_Call(ir_node *const node)
{
	ir_node *const pred = get_Proj_pred(node);
	ir_node *const call = be_transform_node(pred);
	unsigned const pn   = get_Proj_num(node);
	switch ((pn_Call)pn) {
	case pn_Call_M:
		return be_new_Proj(call, pn_mips_jal_M);
	case pn_Call_T_result:
	case pn_Call_X_regular:
	case pn_Call_X_except:
		break;
	}
	panic("unexpected Proj-Call");
}

Christoph Mallon's avatar
Christoph Mallon committed
909
910
911
912
913
914
915
916
917
918
919
static ir_node *gen_Proj_Div(ir_node *const node)
{
	ir_node *const pred = get_Proj_pred(node);
	unsigned const pn   = get_Proj_num(node);
	switch ((pn_Div)pn) {
	case pn_Div_M:   return get_Div_mem(pred);
	case pn_Div_res: return be_transform_node(pred);
	case pn_Div_X_regular:
	case pn_Div_X_except:
		break;
	}
Christoph Mallon's avatar
Christoph Mallon committed
920
	TODO(node);
Christoph Mallon's avatar
Christoph Mallon committed
921
922
}

Christoph Mallon's avatar
Christoph Mallon committed
923
924
925
926
927
928
929
930
931
932
933
934
static ir_node *gen_Proj_Load(ir_node *const node)
{
	ir_node *const pred = get_Proj_pred(node);
	ir_node *const load = be_transform_node(pred);
	unsigned const pn   = get_Proj_num(node);
	switch ((pn_Load)pn) {
	case pn_Load_M:   return be_new_Proj(load, pn_mips_lw_M);
	case pn_Load_res: return be_new_Proj(load, pn_mips_lw_res);
	case pn_Load_X_regular:
	case pn_Load_X_except:
		break;
	}
Christoph Mallon's avatar
Christoph Mallon committed
935
	TODO(node);
Christoph Mallon's avatar
Christoph Mallon committed
936
937
}

Christoph Mallon's avatar
Christoph Mallon committed
938
939
940
941
942
943
944
945
946
947
948
static ir_node *gen_Proj_Mod(ir_node *const node)
{
	ir_node *const pred = get_Proj_pred(node);
	unsigned const pn   = get_Proj_num(node);
	switch ((pn_Mod)pn) {
	case pn_Mod_M:   return get_Mod_mem(pred);
	case pn_Mod_res: return be_transform_node(pred);
	case pn_Mod_X_regular:
	case pn_Mod_X_except:
		break;
	}
Christoph Mallon's avatar
Christoph Mallon committed
949
	TODO(node);
Christoph Mallon's avatar
Christoph Mallon committed
950
951
}

Christoph Mallon's avatar
Christoph Mallon committed
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
static ir_node *gen_Proj_Proj_Call(ir_node *const node)
{
	ir_node *const pred = get_Proj_pred(node);
	assert(get_Proj_num(pred) == pn_Call_T_result);

	ir_node *const ocall    = get_Proj_pred(pred);
	ir_type *const fun_type = get_Call_type(ocall);

	mips_calling_convention_t cconv;
	mips_determine_calling_convention(&cconv, fun_type);

	ir_node               *const call = be_transform_node(ocall);
	unsigned               const num  = get_Proj_num(node);
	arch_register_t const *const reg  = cconv.results[num].reg;
	unsigned               const pos  = be_get_out_for_reg(call, reg);

	mips_free_calling_convention(&cconv);

	return be_new_Proj(call, pos);
}

973
974
975
976
977
978
979
980
981
982
static ir_node *gen_Proj_Proj_Start(ir_node *const node)
{
	assert(get_Proj_num(get_Proj_pred(node)) == pn_Start_T_args);

	ir_graph           *const irg   = get_irn_irg(node);
	unsigned            const num   = get_Proj_num(node);
	mips_reg_or_slot_t *const param = &cur_cconv.parameters[num];
	if (param->reg) {
		return be_get_Start_proj(irg, param->reg);
	} else {
983
984
985
986
987
988
		dbg_info *const dbgi  = get_irn_dbg_info(node);
		ir_node  *const block = be_transform_nodes_block(node);
		ir_node  *const mem   = be_get_Start_mem(irg);
		ir_node  *const base  = get_Start_sp(irg);
		ir_node  *const load  = new_bd_mips_lw(dbgi, block, mem, base, param->entity, 0);
		return be_new_Proj(load, pn_mips_lw_res);
989
990
991
992
993
994
995
996
	}
}

static ir_node *gen_Proj_Proj(ir_node *const node)
{
	ir_node *const pred      = get_Proj_pred(node);
	ir_node *const pred_pred = get_Proj_pred(pred);
	switch (get_irn_opcode(pred_pred)) {
Christoph Mallon's avatar
Christoph Mallon committed
997
	case iro_Call:  return gen_Proj_Proj_Call(node);
998
999
1000
1001
1002
	case iro_Start: return gen_Proj_Proj_Start(node);
	default:        panic("unexpected Proj-Proj");
	}
}

Christoph Mallon's avatar
Christoph Mallon committed
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
static ir_node *gen_Proj_Start(ir_node *const node)
{
	ir_graph *const irg = get_irn_irg(node);
	switch ((pn_Start)get_Proj_num(node)) {
	case pn_Start_M:            return be_get_Start_mem(irg);
	case pn_Start_P_frame_base: return get_Start_sp(irg);
	case pn_Start_T_args:       return new_r_Bad(irg, mode_T);
	}
	panic("unexpected Proj");
}

Christoph Mallon's avatar
Christoph Mallon committed
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
static ir_node *gen_Proj_Store(ir_node *const node)
{
	ir_node *const pred  = get_Proj_pred(node);
	ir_node *const store = be_transform_node(pred);
	unsigned const pn    = get_Proj_num(node);
	switch ((pn_Store)pn) {
	case pn_Store_M: return store;
	case pn_Store_X_regular:
	case pn_Store_X_except:
		break;
	}
Christoph Mallon's avatar
Christoph Mallon committed
1025
	TODO(node);
Christoph Mallon's avatar
Christoph Mallon committed
1026
1027
}

Christoph Mallon's avatar
Christoph Mallon committed
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
static ir_node *gen_Return(ir_node *const node)
{
	unsigned       p     = n_mips_ret_first_result;
	unsigned const n_res = get_Return_n_ress(node);
	unsigned const n_ins = p + n_res + ARRAY_SIZE(callee_saves);

	ir_graph                   *const irg  = get_irn_irg(node);
	arch_register_req_t const **const reqs = be_allocate_in_reqs(irg, n_ins);
	ir_node                   **const in   = ALLOCAN(ir_node*, n_ins);

	ir_node *const mem = get_Return_mem(node);
	in[n_mips_ret_mem]   = be_transform_node(mem);
	reqs[n_mips_ret_mem] = arch_memory_req;

	in[n_mips_ret_stack]   = get_Start_sp(irg);
	reqs[n_mips_ret_stack] = &mips_single_reg_req_gp_sp;

	in[n_mips_ret_addr]    = be_get_Start_proj(irg, &mips_registers[REG_RA]);
	reqs[n_mips_ret_addr]  = &mips_class_reg_req_gp;

1048
1049
1050
1051
1052
1053
1054
	mips_reg_or_slot_t *const results = cur_cconv.results;
	for (size_t i = 0; i != n_res; ++i) {
		ir_node *const res = get_Return_res(node, i);
		in[p]   = be_transform_node(res);
		reqs[p] = results[i].reg->single_req;
		++p;
	}
Christoph Mallon's avatar
Christoph Mallon committed
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070

	for (size_t i = 0; i != ARRAY_SIZE(callee_saves); ++i) {
		arch_register_t const *const reg = &mips_registers[callee_saves[i]];
		in[p]   = be_get_Start_proj(irg, reg);
		reqs[p] = reg->single_req;
		++p;
	}

	assert(p == n_ins);
	dbg_info *const dbgi  = get_irn_dbg_info(node);
	ir_node  *const block = be_transform_nodes_block(node);
	ir_node  *const ret   = new_bd_mips_ret(dbgi, block, n_ins, in, reqs);
	be_stack_record_chain(&stack_env, ret, n_mips_ret_stack, NULL);
	return ret;
}

Christoph Mallon's avatar
Christoph Mallon committed
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
static ir_node *gen_shift_op(ir_node *const node, cons_binop *const cons, cons_binop_imm *const cons_imm)
{
	dbg_info *const dbgi  = get_irn_dbg_info(node);
	ir_node  *const block = be_transform_nodes_block(node);
	ir_node  *const l     = get_binop_left(node);
	ir_node  *const new_l = be_transform_node(l);
	ir_node  *const r     = get_binop_right(node);
	if (is_Const(r)) {
		long const val = get_Const_long(r);
		if (is_uimm5(val))
Christoph Mallon's avatar
Christoph Mallon committed
1081
			return cons_imm(dbgi, block, new_l, NULL, val);
Christoph Mallon's avatar
Christoph Mallon committed
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
	}
	ir_node *const new_r = be_transform_node(r);
	return cons(dbgi, block, new_l, new_r);
}

static ir_node *gen_Shl(ir_node *const node)
{
	return gen_shift_op(node, &new_bd_mips_sllv, &new_bd_mips_sll);
}

static ir_node *gen_Shr(ir_node *const node)
{
	ir_mode *const mode = get_irn_mode(node);
	unsigned const size = get_mode_size_bits(mode);
	if (size == MIPS_MACHINE_SIZE)
		return gen_shift_op(node, &new_bd_mips_srlv, &new_bd_mips_srl);
Christoph Mallon's avatar
Christoph Mallon committed
1098
	TODO(node);
Christoph Mallon's avatar
Christoph Mallon committed
1099
1100
1101
1102
1103
1104
1105
1106
}

static ir_node *gen_Shrs(ir_node *const node)
{
	ir_mode *const mode = get_irn_mode(node);
	unsigned const size = get_mode_size_bits(mode);
	if (size == MIPS_MACHINE_SIZE)
		return gen_shift_op(node, &new_bd_mips_srav, &new_bd_mips_sra);
Christoph Mallon's avatar
Christoph Mallon committed
1107
	TODO(node);
Christoph Mallon's avatar
Christoph Mallon committed
1108
1109
}

Christoph Mallon's avatar
Christoph Mallon committed
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
static ir_node *gen_Start(ir_node *const node)
{
	be_start_out outs[N_MIPS_REGISTERS] = {
		[REG_ZERO] = BE_START_IGNORE,
		[REG_SP]   = BE_START_IGNORE,
		[REG_RA]   = BE_START_REG,
	};
	for (size_t i = 0; i != ARRAY_SIZE(callee_saves); ++i) {
		outs[callee_saves[i]] = BE_START_REG;
	}

1121
1122
1123
1124
1125
1126
1127
1128
1129
	ir_graph  *const irg  = get_irn_irg(node);
	ir_entity *const ent  = get_irg_entity(irg);
	ir_type   *const type = get_entity_type(ent);
	for (size_t i = 0, n = get_method_n_params(type); i != n; ++i) {
		arch_register_t const *const reg = cur_cconv.parameters[i].reg;
		if (reg)
			outs[reg->global_index] = BE_START_REG;
	}

Christoph Mallon's avatar
Christoph Mallon committed
1130
1131
1132
	return be_new_Start(irg, outs);
}

Christoph Mallon's avatar
Christoph Mallon committed
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
typedef ir_node *cons_storeop(dbg_info*, ir_node*, ir_node*, ir_node*, ir_node*, ir_entity*, int32_t);

static ir_node *gen_Store(ir_node *const node)
{
	ir_node       *old_val = get_Store_value(node);
	ir_mode *const mode    = get_irn_mode(old_val);
	if (be_mode_needs_gp_reg(mode)) {
		cons_storeop  *cons;
		unsigned const size = get_mode_size_bits(mode);
		if (size == 8) {
			cons = &new_bd_mips_sb;
		} else if (size == 16) {
			cons = &new_bd_mips_sh;
		} else if (size == 32) {
			cons = &new_bd_mips_sw;
		} else {
			panic("invalid store");
		}
		old_val = be_skip_downconv(old_val, false);
		dbg_info *const dbgi  = get_irn_dbg_info(node);
		ir_node  *const block = be_transform_nodes_block(node);
		ir_node  *const mem   = be_transform_node(get_Store_mem(node));
		ir_node  *const val   = be_transform_node(old_val);
1156
1157
		mips_addr const addr  = make_addr(get_Store_ptr(node));
		return cons(dbgi, block, mem, addr.base, val, addr.ent, addr.val);
Christoph Mallon's avatar
Christoph Mallon committed
1158
	}
Christoph Mallon's avatar
Christoph Mallon committed
1159
	TODO(node);
Christoph Mallon's avatar
Christoph Mallon committed
1160
1161
}

1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
static ir_node *gen_Sub(ir_node *const node)
{
	ir_node *const l    = get_Sub_left(node);
	ir_node *const r    = get_Sub_right(node);
	ir_mode *const mode = get_irn_mode(node);
	if (be_mode_needs_gp_reg(mode)) {
		dbg_info *const dbgi  = get_irn_dbg_info(node);
		ir_node  *const block = be_transform_nodes_block(node);
		ir_node  *const new_l = be_transform_node(l);
		ir_node  *const new_r = be_transform_node(r);
		return new_bd_mips_subu(dbgi, block, new_l, new_r);
	}
Christoph Mallon's avatar
Christoph Mallon committed
1174
	TODO(node);
1175
1176
}

Christoph Mallon's avatar
Christoph Mallon committed
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
static ir_node *gen_Switch(ir_node *const node)
{
	ir_graph              *const irg   = get_irn_irg(node);
	ir_switch_table const *const table = ir_switch_table_duplicate(irg, get_Switch_table(node));

	ir_type   *const utype  = get_unknown_type();
	ident     *const id     = id_unique("TBL");
	ir_entity *const entity = new_global_entity(irp->dummy_owner, id, utype, ir_visibility_private, IR_LINKAGE_CONSTANT | IR_LINKAGE_NO_IDENTITY);

	dbg_info  *const dbgi   = get_irn_dbg_info(node);
	ir_node   *const block  = be_transform_nodes_block(node);
	ir_node   *const nomem  = get_irg_no_mem(irg);
	ir_node   *const sel    = be_transform_node(get_Switch_selector(node));
	ir_node   *const sll    = new_bd_mips_sll(dbgi, block, sel, NULL, 2);
	ir_node   *const lui    = new_bd_mips_lui(dbgi, block, entity, 0);
	ir_node   *const add    = new_bd_mips_addu(dbgi, block, sll, lui);
	ir_node   *const load   = new_bd_mips_lw(dbgi, block, nomem, add, entity, 0);
	ir_node   *const res    = be_new_Proj(load, pn_mips_lw_res);
	unsigned   const n_outs = get_Switch_n_outs(node);
	return new_bd_mips_switch(dbgi, block, res, n_outs, table, entity);
}

Christoph Mallon's avatar
Christoph Mallon committed
1199
1200
1201
1202
1203
1204
1205
static ir_node *gen_Unknown(ir_node *const node)
{
	ir_node *const block = be_transform_nodes_block(node);
	ir_mode *const mode  = get_irn_mode(node);
	if (be_mode_needs_gp_reg(mode)) {
		return be_new_Unknown(block, &mips_class_reg_req_gp);
	} else {
Christoph Mallon's avatar
Christoph Mallon committed
1206
		TODO(node);
Christoph Mallon's avatar
Christoph Mallon committed
1207
1208
1209
	}
}

Christoph Mallon's avatar
Christoph Mallon committed
1210
1211
1212
1213
static void mips_register_transformers(void)
{
	be_start_transform_setup();

1214
	be_set_transform_function(op_ASM,     gen_ASM);
Christoph Mallon's avatar
Christoph Mallon committed
1215
1216
1217
	be_set_transform_function(op_Add,     gen_Add);
	be_set_transform_function(op_Address, gen_Address);
	be_set_transform_function(op_And,     gen_And);
1218
	be_set_transform_function(op_Builtin, gen_Builtin);
Christoph Mallon's avatar
Christoph Mallon committed
1219
	be_set_transform_function(op_Call,    gen_Call);
Christoph Mallon's avatar
Christoph Mallon committed
1220
1221
	be_set_transform_function(op_Cmp,     gen_Cmp);
	be_set_transform_function(op_Cond,    gen_Cond);
Christoph Mallon's avatar
Christoph Mallon committed
1222
	be_set_transform_function(op_Conv,    gen_Conv);
Christoph Mallon's avatar
Christoph Mallon committed
1223
	be_set_transform_function(op_Const,   gen_Const);
Christoph Mallon's avatar
Christoph Mallon committed
1224
	be_set_transform_function(op_Div,     gen_Div);
Christoph Mallon's avatar
Christoph Mallon committed
1225
	be_set_transform_function(op_Eor,     gen_Eor);
Christoph Mallon's avatar
Christoph Mallon committed
1226
	be_set_transform_function(op_IJmp,    gen_IJmp);
Christoph Mallon's avatar
Christoph Mallon committed
1227
	be_set_transform_function(op_Jmp,     gen_Jmp);
Christoph Mallon's avatar
Christoph Mallon committed
1228
	be_set_transform_function(op_Load,    gen_Load);
Christoph Mallon's avatar
Christoph Mallon committed
1229
	be_set_transform_function(op_Member,  gen_Member);
Christoph Mallon's avatar
Christoph Mallon committed
1230
	be_set_transform_function(op_Mulh,    gen_Mulh);
Christoph Mallon's avatar
Christoph Mallon committed
1231
	be_set_transform_function(op_Mul,     gen_Mul);
Christoph Mallon's avatar
Christoph Mallon committed
1232
	be_set_transform_function(op_Minus,   gen_Minus);
Christoph Mallon's avatar
Christoph Mallon committed
1233
	be_set_transform_function(op_Mod,     gen_Mod);
1234
	be_set_transform_function(op_Mux,     gen_Mux);
Christoph Mallon's avatar
Christoph Mallon committed
1235
1236
1237
1238
1239
1240
1241
1242
	be_set_transform_function(op_Not,     gen_Not);
	be_set_transform_function(op_Or,      gen_Or);
	be_set_transform_function(op_Phi,     gen_Phi);
	be_set_transform_function(op_Return,  gen_Return);
	be_set_transform_function(op_Shl,     gen_Shl);
	be_set_transform_function(op_Shr,     gen_Shr);
	be_set_transform_function(op_Shrs,    gen_Shrs);
	be_set_transform_function(op_Start,   gen_Start);
Christoph Mallon's avatar
Christoph Mallon committed
1243
	be_set_transform_function(op_Store,   gen_Store);
Christoph Mallon's avatar
Christoph Mallon committed
1244
	be_set_transform_function(op_Sub,     gen_Sub);
Christoph Mallon's avatar
Christoph Mallon committed
1245
	be_set_transform_function(op_Switch,  gen_Switch);
Christoph Mallon's avatar
Christoph Mallon committed
1246
	be_set_transform_function(op_Unknown, gen_Unknown);
Christoph Mallon's avatar
Christoph Mallon committed
1247

1248
	be_set_transform_proj_function(op_Builtin, gen_Proj_Builtin);
Christoph Mallon's avatar
Christoph Mallon committed
1249
	be_set_transform_proj_function(op_Call,    gen_Proj_Call);
1250
1251
1252
1253
1254
1255
	be_set_transform_proj_function(op_Div,     gen_Proj_Div);
	be_set_transform_proj_function(op_Load,    gen_Proj_Load);
	be_set_transform_proj_function(op_Mod,     gen_Proj_Mod);
	be_set_transform_proj_function(op_Proj,    gen_Proj_Proj);
	be_set_transform_proj_function(op_Start,   gen_Proj_Start);
	be_set_transform_proj_function(op_Store,   gen_Proj_Store);
Christoph Mallon's avatar
Christoph Mallon committed
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
}

static void mips_set_allocatable_regs(ir_graph *const irg)
{
	be_irg_t       *const birg = be_birg_from_irg(irg);
	struct obstack *const obst = &birg->obst;

	unsigned *const a = rbitset_obstack_alloc(obst, N_MIPS_REGISTERS);
	for (size_t i = 0; i != ARRAY_SIZE(callee_saves); ++i) {
		rbitset_set(a, callee_saves[i]);
	}
	for (size_t i = 0; i != ARRAY_SIZE(caller_saves); ++i) {
		rbitset_set(a, caller_saves[i]);
	}
	birg->allocatable_regs = a;
}

void mips_transform_graph(ir_graph *const irg)
{
	mips_register_transformers();
	mips_set_allocatable_regs(irg);
	be_stack_init(&stack_env);
1278
1279
1280
1281

	ir_entity *const fun_ent  = get_irg_entity(irg);
	ir_type   *const fun_type = get_entity_type(fun_ent);
	mips_determine_calling_convention(&cur_cconv, fun_type);
1282
1283
	mips_layout_parameter_entities(&cur_cconv, irg);
	be_add_parameter_entity_stores(irg);
Christoph Mallon's avatar
Christoph Mallon committed
1284
	be_transform_graph(irg, NULL);
1285
1286
	mips_free_calling_convention(&cur_cconv);

Christoph Mallon's avatar
Christoph Mallon committed
1287
1288
	be_stack_finish(&stack_env);
}