ia32_emitter.c 24.9 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
3
4
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

Christian Würdig's avatar
Christian Würdig committed
5
6
#include <limits.h>

7
#include "xmalloc.h"
8
9
#include "tv.h"
#include "iredges.h"
Christian Würdig's avatar
Christian Würdig committed
10
11
12
13
#include "debug.h"
#include "irgwalk.h"
#include "irprintf.h"
#include "irop_t.h"
Christian Würdig's avatar
Christian Würdig committed
14
#include "irargs_t.h"
Christian Würdig's avatar
Christian Würdig committed
15
16
17

#include "../besched.h"

18
#include "ia32_emitter.h"
Christian Würdig's avatar
Christian Würdig committed
19
#include "gen_ia32_emitter.h"
20
21
#include "ia32_nodes_attr.h"
#include "ia32_new_nodes.h"
Christian Würdig's avatar
Christian Würdig committed
22
23
#include "ia32_map_regs.h"

Christian Würdig's avatar
Christian Würdig committed
24
25
26
27
28
29
30
31
#ifdef obstack_chunk_alloc
# undef obstack_chunk_alloc
# define obstack_chunk_alloc xmalloc
#else
# define obstack_chunk_alloc xmalloc
# define obstack_chunk_free free
#endif

Christian Würdig's avatar
Christian Würdig committed
32
33
#define SNPRINTF_BUF_LEN 128

Christian Würdig's avatar
Christian Würdig committed
34
static const arch_env_t *arch_env = NULL;
35

Christian Würdig's avatar
Christian Würdig committed
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
char *ia32_emit_binop(const ir_node *n) {
	static char *buf = NULL;

	if (! buf) {
		buf = xcalloc(1, SNPRINTF_BUF_LEN);
	}
	else {
		memset(buf, 0, SNPRINTF_BUF_LEN);
	}

	switch(get_ia32_op_type(n)) {
		case ia32_Normal:
			if (get_ia32_cnst(n)) {
				lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, get_ia32_cnst(n));
			}
			else {
				lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %4S", n, n);
			}
			break;
		case ia32_am_Source:
			lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, ia32_emit_am(n));
			break;
		case ia32_am_Dest:
			lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %4S", ia32_emit_am(n), n);
			break;
		default:
			assert(0 && "unsupported op type");
	}

	return buf;
66
67
}

Christian Würdig's avatar
Christian Würdig committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
char *ia32_emit_unop(const ir_node *n) {
	static char *buf = NULL;

	if (! buf) {
		buf = xcalloc(1, SNPRINTF_BUF_LEN);
	}
	else {
		memset(buf, 0, SNPRINTF_BUF_LEN);
	}

	switch(get_ia32_op_type(n)) {
		case ia32_Normal:
			lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
			break;
		case ia32_am_Dest:
			snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n));
			break;
		default:
			assert(0 && "unsupported op type");
	}

	return buf;
90
91
}

Christian Würdig's avatar
Christian Würdig committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
char *ia32_emit_am(const ir_node *n) {
	ia32_am_flavour_t am_flav    = get_ia32_am_flavour(n);
	int               had_output = 0;
	char             *s;
	int               size;
	static struct obstack *obst  = NULL;

	if (! obst) {
		obst = xcalloc(1, sizeof(*obst));
	}
	else {
		obstack_free(obst, NULL);
	}

	/* obstack_free with NULL results in an uninitialized obstack */
	obstack_init(obst);

	obstack_printf(obst, "[");

	if (am_flav & ia32_B) {
		lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
		had_output = 1;
	}

	if (am_flav & ia32_I) {
		if (had_output) {
			obstack_printf(obst, "+");
		}

		lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);

		if (am_flav & ia32_S) {
			obstack_printf(obst, "*%d", get_ia32_am_scale(n));
		}

		had_output = 1;
	}

	if (am_flav & ia32_O) {
		s = get_ia32_am_offs(n);

		if (! had_output) {
			/* skip the first 3 characters which are " <op> " */
			s += 3;
		}

		obstack_printf(obst, "%s", s);
	}

	obstack_printf(obst, "] ");

	size        = obstack_object_size(obst);
	s           = obstack_finish(obst);
	s[size - 1] = '\0';

	return s;
148
}
149

Christian Würdig's avatar
Christian Würdig committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*************************************************************
 *             _       _    __   _          _
 *            (_)     | |  / _| | |        | |
 *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
 * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
 * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
 * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
 * | |                                       | |
 * |_|                                       |_|
 *************************************************************/

/* We always pass the ir_node which is a pointer. */
static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
	return lc_arg_type_ptr;
164
165
}

Christian Würdig's avatar
Christian Würdig committed
166
167

/**
Christian Würdig's avatar
Christian Würdig committed
168
 * Returns the register at in position pos.
Christian Würdig's avatar
Christian Würdig committed
169
170
171
172
173
174
175
176
177
178
179
 */
static const arch_register_t *get_in_reg(ir_node *irn, int pos) {
	ir_node                *op;
	const arch_register_t  *reg = NULL;

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

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

Christian Würdig's avatar
Christian Würdig committed
180
	reg = arch_get_irn_register(arch_env, op);
Christian Würdig's avatar
Christian Würdig committed
181

Christian Würdig's avatar
Christian Würdig committed
182
183
184
185
186
187
188
189
190
191
192
193
	assert(reg && "no in register found");
	return reg;
}

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

	assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
Christian Würdig's avatar
Christian Würdig committed
194

Christian Würdig's avatar
Christian Würdig committed
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
	/* 1st case: irn is not of mode_T, so it has only                 */
	/*           one OUT register -> good                             */
	/* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
	/*           Proj with the corresponding projnum for the register */

	if (get_irn_mode(irn) != mode_T) {
		reg = arch_get_irn_register(arch_env, irn);
	}
	else if (is_ia32_irn(irn)) {
		reg = get_ia32_out_reg(irn, pos);
	}
	else {
		const ir_edge_t *edge;

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

	assert(reg && "no out register found");
Christian Würdig's avatar
Christian Würdig committed
220
221
222
223
224
225
	return reg;
}

/**
 * Returns the number of the in register at position pos.
 */
Christian Würdig's avatar
Christian Würdig committed
226
int get_ia32_reg_nr(ir_node *irn, int pos, int in_out) {
Christian Würdig's avatar
Christian Würdig committed
227
	const arch_register_t *reg;
Christian Würdig's avatar
Christian Würdig committed
228
229
230
231
232
233
234
235
236

	if (in_out == 1) {
		reg = get_in_reg(irn, pos);
	}
	else {
		reg = get_out_reg(irn, pos);
	}

	return arch_register_get_index(reg);
Christian Würdig's avatar
Christian Würdig committed
237
238
239
240
241
}

/**
 * Returns the name of the in register at position pos.
 */
Christian Würdig's avatar
Christian Würdig committed
242
const char *get_ia32_reg_name(ir_node *irn, int pos, int in_out) {
Christian Würdig's avatar
Christian Würdig committed
243
244
	const arch_register_t *reg;

Christian Würdig's avatar
Christian Würdig committed
245
246
247
248
249
250
251
252
	if (in_out == 1) {
		reg = get_in_reg(irn, pos);
	}
	else {
		reg = get_out_reg(irn, pos);
	}

	return arch_register_get_name(reg);
Christian Würdig's avatar
Christian Würdig committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
}

/**
 * Get the register name for a node.
 */
static int ia32_get_reg_name(lc_appendable_t *app,
    const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
{
	const char *buf;
	ir_node    *X  = arg->v_ptr;
	int         nr = occ->width - 1;

	if (!X)
		return lc_arg_append(app, occ, "(null)", 6);

268
	if (occ->conversion == 'S') {
Christian Würdig's avatar
Christian Würdig committed
269
		buf = get_ia32_reg_name(X, nr, 1);
Christian Würdig's avatar
Christian Würdig committed
270
	}
271
	else { /* 'D' */
Christian Würdig's avatar
Christian Würdig committed
272
		buf = get_ia32_reg_name(X, nr, 0);
Christian Würdig's avatar
Christian Würdig committed
273
274
	}

Christian Würdig's avatar
Christian Würdig committed
275
	lc_appendable_chadd(app, '%');
Christian Würdig's avatar
Christian Würdig committed
276
	return lc_arg_append(app, occ, buf, strlen(buf));
277
278
}

Christian Würdig's avatar
Christian Würdig committed
279
280
281
282
283
284
285
286
287
288
289
290
/**
 * Returns the tarval or offset of an ia32 as a string.
 */
static int ia32_const_to_str(lc_appendable_t *app,
    const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
{
	const char *buf;
	ir_node    *X = arg->v_ptr;

	if (!X)
		return lc_arg_append(app, occ, "(null)", 6);

291
	if (occ->conversion == 'C') {
Christian Würdig's avatar
Christian Würdig committed
292
		buf = get_ia32_cnst(X);
Christian Würdig's avatar
Christian Würdig committed
293
	}
294
	else { /* 'O' */
Christian Würdig's avatar
Christian Würdig committed
295
		buf = get_ia32_am_offs(X);
Christian Würdig's avatar
Christian Würdig committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
	}

	return lc_arg_append(app, occ, buf, strlen(buf));
}

/**
 * Determines the SSE suffix depending on the mode.
 */
static int ia32_get_mode_suffix(lc_appendable_t *app,
    const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
{
	ir_node *X = arg->v_ptr;

	if (!X)
		return lc_arg_append(app, occ, "(null)", 6);

	if (get_mode_size_bits(get_irn_mode(X)) == 32)
		return lc_appendable_chadd(app, 's');
	else
		return lc_appendable_chadd(app, 'd');
}

/**
 * Return the ia32 printf arg environment.
 * We use the firm environment with some additional handlers.
 */
const lc_arg_env_t *ia32_get_arg_env(void) {
Christian Würdig's avatar
Christian Würdig committed
323
	static lc_arg_env_t *env = NULL;
Christian Würdig's avatar
Christian Würdig committed
324

Christian Würdig's avatar
Christian Würdig committed
325
326
327
	static const lc_arg_handler_t ia32_reg_handler   = { ia32_get_arg_type, ia32_get_reg_name };
	static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
	static const lc_arg_handler_t ia32_mode_handler  = { ia32_get_arg_type, ia32_get_mode_suffix };
Christian Würdig's avatar
Christian Würdig committed
328

Christian Würdig's avatar
Christian Würdig committed
329
330
331
	if(env == NULL) {
		/* extend the firm printer */
		env = firm_get_arg_env();
332
			//lc_arg_new_env();
Christian Würdig's avatar
Christian Würdig committed
333

334
335
336
337
338
		lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
		lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
		lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
		lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
		lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
Christian Würdig's avatar
Christian Würdig committed
339
	}
Christian Würdig's avatar
Christian Würdig committed
340

Christian Würdig's avatar
Christian Würdig committed
341
	return env;
Christian Würdig's avatar
Christian Würdig committed
342
343
344
345
346
}

/**
 * For 2-address code we need to make sure the first src reg is equal to dest reg.
 */
347
void equalize_dest_src(FILE *F, ir_node *n) {
Christian Würdig's avatar
Christian Würdig committed
348
349
	if (get_ia32_reg_nr(n, 0, 1) != get_ia32_reg_nr(n, 0, 0)) {
		if (get_irn_arity(n) > 1 && get_ia32_reg_nr(n, 1, 1) == get_ia32_reg_nr(n, 0, 0)) {
Christian Würdig's avatar
Christian Würdig committed
350
			if (! is_op_commutative(get_irn_op(n))) {
Christian Würdig's avatar
Christian Würdig committed
351
				/* we only need to exchange for non-commutative ops */
352
				lc_efprintf(ia32_get_arg_env(), F, "\txchg %1S, %2S\t\t\t/* xchg src1 <-> src2 for 2 address code */\n", n, n);
Christian Würdig's avatar
Christian Würdig committed
353
354
355
			}
		}
		else {
356
			lc_efprintf(ia32_get_arg_env(), F, "\tmovl %1S, %1D\t\t\t/* src -> dest for 2 address code */\n", n, n);
Christian Würdig's avatar
Christian Würdig committed
357
358
359
360
361
362
363
364
365
366
367
		}
	}
}

/*
 * Add a number to a prefix. This number will not be used a second time.
 */
char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
	static unsigned long id = 0;
	snprintf(buf, buflen, "%s%lu", prefix, ++id);
	return buf;
368
369
}

Christian Würdig's avatar
Christian Würdig committed
370
371
372
373
374
375
376
377
378
379
380

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

381
382
383
384
/*
 * coding of conditions
 */
struct cmp2conditon_t {
Christian Würdig's avatar
Christian Würdig committed
385
386
	const char *name;
	pn_Cmp      num;
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
};

/*
 * positive conditions for signed compares
 */
static const struct cmp2conditon_t cmp2condition_s[] = {
  { NULL,              pn_Cmp_False },  /* always false */
  { "e",               pn_Cmp_Eq },     /* == */
  { "l",               pn_Cmp_Lt },     /* < */
  { "le",              pn_Cmp_Le },     /* <= */
  { "g",               pn_Cmp_Gt },     /* > */
  { "ge",              pn_Cmp_Ge },     /* >= */
  { "ne",              pn_Cmp_Lg },     /* != */
  { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
  { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
  { "unordered or ==", pn_Cmp_Ue },     /* Floating point: unordered or == */
  { "unordered or <",  pn_Cmp_Ul },     /* Floating point: unordered or < */
  { "unordered or <=", pn_Cmp_Ule },    /* Floating point: unordered or <= */
  { "unordered or >",  pn_Cmp_Ug },     /* Floating point: unordered or > */
  { "unordered or >=", pn_Cmp_Uge },    /* Floating point: unordered or >= */
  { "unordered or !=", pn_Cmp_Ne },     /* Floating point: unordered or != */
  { NULL,              pn_Cmp_True },   /* always true */
};

/*
 * positive conditions for unsigned compares
 */
static const struct cmp2conditon_t cmp2condition_u[] = {
  { NULL,              pn_Cmp_False },  /* always false */
  { "e",               pn_Cmp_Eq },     /* == */
  { "b",               pn_Cmp_Lt },     /* < */
  { "be",              pn_Cmp_Le },     /* <= */
  { "a",               pn_Cmp_Gt },     /* > */
  { "ae",              pn_Cmp_Ge },     /* >= */
  { "ne",              pn_Cmp_Lg },     /* != */
  { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
  { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
  { "unordered or ==", pn_Cmp_Ue },     /* Floating point: unordered or == */
  { "unordered or <",  pn_Cmp_Ul },     /* Floating point: unordered or < */
  { "unordered or <=", pn_Cmp_Ule },    /* Floating point: unordered or <= */
  { "unordered or >",  pn_Cmp_Ug },     /* Floating point: unordered or > */
  { "unordered or >=", pn_Cmp_Uge },    /* Floating point: unordered or >= */
  { "unordered or !=", pn_Cmp_Ne },     /* Floating point: unordered or != */
  { NULL,              pn_Cmp_True },   /* always true */
};

/*
 * returns the condition code
 */
Christian Würdig's avatar
Christian Würdig committed
436
static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
437
{
Christian Würdig's avatar
Christian Würdig committed
438
439
	assert(cmp2condition_s[cmp_code].num == cmp_code);
	assert(cmp2condition_u[cmp_code].num == cmp_code);
440

Christian Würdig's avatar
Christian Würdig committed
441
	return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
442
443
}

Christian Würdig's avatar
Christian Würdig committed
444
445
446
447
448
/**
 * Returns the target label for a control flow node.
 */
static char *get_cfop_target(const ir_node *irn, char *buf) {
	ir_node *bl = get_irn_link(irn);
449

Christian Würdig's avatar
Christian Würdig committed
450
451
452
	snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
	return buf;
}
453

Christian Würdig's avatar
Christian Würdig committed
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
/**
 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
 */
static void finish_CondJmp(FILE *F, ir_node *irn) {
	const ir_node   *proj;
	const ir_edge_t *edge;
	char buf[SNPRINTF_BUF_LEN];

	edge = get_irn_out_edge_first(irn);
	proj = get_edge_src_irn(edge);
	assert(is_Proj(proj) && "CondJmp with a non-Proj");

	if (get_Proj_proj(proj) == 1) {
		fprintf(F, "\tj%s %s\t\t\t/* cmp(a, b) == TRUE */\n",
					get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
					get_cfop_target(proj, buf));
	}
	else  {
		fprintf(F, "\tjn%s %s\t\t\t/* cmp(a, b) == FALSE */\n",
					get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
					get_cfop_target(proj, buf));
	}

	edge = get_irn_out_edge_next(irn, edge);
	if (edge) {
		proj = get_edge_src_irn(edge);
		assert(is_Proj(proj) && "CondJmp with a non-Proj");
		fprintf(F, "\tjmp %s\t\t\t/* otherwise */\n", get_cfop_target(proj, buf));
	}
}

/**
 * Emits code for conditional jump with two variables.
 */
static void emit_ia32_CondJmp(ir_node *irn, emit_env_t *env) {
	FILE *F = env->out;

491
	lc_efprintf(ia32_get_arg_env(), F, "\tcmp %2S, %1S\t\t\t/* CondJmp(%+F, %+F) */\n", irn, irn,
Christian Würdig's avatar
Christian Würdig committed
492
493
494
495
496
497
498
499
500
501
																	get_irn_n(irn, 0), get_irn_n(irn, 1));
	finish_CondJmp(F, irn);
}

/**
 * Emits code for conditional jump with immediate.
 */
void emit_ia32_CondJmp_i(ir_node *irn, emit_env_t *env) {
	FILE *F = env->out;

502
	lc_efprintf(ia32_get_arg_env(), F, "\tcmp %C, %1S\t\t\t/* CondJmp_i(%+F) */\n", irn, irn, get_irn_n(irn, 0));
Christian Würdig's avatar
Christian Würdig committed
503
504
505
	finish_CondJmp(F, irn);
}

Christian Würdig's avatar
Christian Würdig committed
506
507
508
509
510
511
512
513
514
515
516
517
518
519


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

/* jump table entry (target and corresponding number) */
Christian Würdig's avatar
Christian Würdig committed
520
521
522
523
524
typedef struct _branch_t {
	ir_node *target;
	int      value;
} branch_t;

Christian Würdig's avatar
Christian Würdig committed
525
/* jump table for switch generation */
Christian Würdig's avatar
Christian Würdig committed
526
typedef struct _jmp_tbl_t {
Christian Würdig's avatar
Christian Würdig committed
527
528
529
530
531
532
	ir_node  *defProj;         /**< default target */
	int       min_value;       /**< smallest switch case */
	int       max_value;       /**< largest switch case */
	int       num_branches;    /**< number of jumps */
	char     *label;           /**< label of the jump table */
	branch_t *branches;        /**< jump array */
Christian Würdig's avatar
Christian Würdig committed
533
534
} jmp_tbl_t;

Christian Würdig's avatar
Christian Würdig committed
535
536
537
/**
 * Compare two variables of type branch_t. Used to sort all switch cases
 */
Christian Würdig's avatar
Christian Würdig committed
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
static int ia32_cmp_branch_t(const void *a, const void *b) {
	branch_t *b1 = (branch_t *)a;
	branch_t *b2 = (branch_t *)b;

	if (b1->value <= b2->value)
		return -1;
	else
		return 1;
}

/**
 * Emits code for a SwitchJmp (creates a jump table if
 * possible otherwise a cmp-jmp cascade). Port from
 * cggg ia32 backend
 */
void emit_ia32_SwitchJmp(const ir_node *irn, emit_env_t *emit_env) {
	unsigned long       interval;
	char                buf[SNPRINTF_BUF_LEN];
	int                 last_value, i, pn, do_jmp_tbl = 1;
	jmp_tbl_t           tbl;
	ir_node            *proj;
	const ir_edge_t    *edge;
	const lc_arg_env_t *env = ia32_get_arg_env();
	FILE               *F   = emit_env->out;

	/* fill the table structure */
564
	tbl.label        = xmalloc(SNPRINTF_BUF_LEN);
Christian Würdig's avatar
Christian Würdig committed
565
566
567
	tbl.label        = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
	tbl.defProj      = NULL;
	tbl.num_branches = get_irn_n_edges(irn);
568
	tbl.branches     = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
Christian Würdig's avatar
Christian Würdig committed
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
	tbl.min_value    = INT_MAX;
	tbl.max_value    = INT_MIN;

	i = 0;
	/* go over all proj's and collect them */
	foreach_out_edge(irn, edge) {
		proj = get_edge_src_irn(edge);
		assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");

		pn = get_Proj_proj(proj);

		/* create branch entry */
		tbl.branches[i].target = proj;
		tbl.branches[i].value  = pn;

		tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
		tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;

		/* check for default proj */
		if (pn == get_ia32_pncode(irn)) {
			assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
			tbl.defProj = proj;
		}

		i++;
	}

	/* sort the branches by their number */
Christian Würdig's avatar
Christian Würdig committed
597
	qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
Christian Würdig's avatar
Christian Würdig committed
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615

	/* two-complement's magic make this work without overflow */
	interval = tbl.max_value - tbl.min_value;

	/* check value interval */
	if (interval > 16 * 1024) {
		do_jmp_tbl = 0;
	}

	/* check ratio of value interval to number of branches */
	if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
		do_jmp_tbl = 0;
	}

	if (do_jmp_tbl) {
		/* emit the table */
		if (tbl.min_value != 0) {
			fprintf(F, "\tcmpl %lu, -%d", interval, tbl.min_value);
616
			lc_efprintf(env, F, "(%1S)\t\t/* first switch value is not 0 */\n", irn);
Christian Würdig's avatar
Christian Würdig committed
617
618
619
		}
		else {
			fprintf(F, "\tcmpl %lu, ", interval);
620
			lc_efprintf(env, F, "%1S\t\t\t/* compare for switch */\n", irn);
Christian Würdig's avatar
Christian Würdig committed
621
622
623
624
625
626
627
		}

		fprintf(F, "\tja %s\t\t\t/* default jump if out of range  */\n", get_cfop_target(tbl.defProj, buf));

		if (tbl.num_branches > 1) {
			/* create table */

628
629
			//fprintf(F, "\tjmp *%s", tbl.label);
			lc_efprintf(env, F, "\tjmp *%s(,%1S,4)\t\t/* get jump table entry as target */\n", tbl.label, irn);
Christian Würdig's avatar
Christian Würdig committed
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

			fprintf(F, "\t.section\t.rodata\t\t/* start jump table */\n");
			fprintf(F, "\t.align 4\n");

			fprintf(F, "%s:\n", tbl.label);
			fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[0].target, buf), tbl.branches[0].value);

			last_value = tbl.branches[0].value;
			for (i = 1; i < tbl.num_branches; ++i) {
				while (++last_value < tbl.branches[i].value) {
					fprintf(F, "\t.long %s\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
				}
				fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[i].target, buf), last_value);
			}

			fprintf(F, "\t.text\t\t\t\t/* end of jump table */\n");
		}
		else {
			/* one jump is enough */
			fprintf(F, "\tjmp %s\t\t/* only one case given */\n", get_cfop_target(tbl.branches[0].target, buf));
		}
	}
	else { // no jump table
		for (i = 0; i < tbl.num_branches; ++i) {
			fprintf(F, "\tcmpl %d, ", tbl.branches[i].value);
655
			lc_efprintf(env, F, "%1S", irn);
Christian Würdig's avatar
Christian Würdig committed
656
657
658
659
660
661
662
			fprintf(F, "\t\t\t/* case %d */\n", tbl.branches[i].value);
			fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
		}

		fprintf(F, "\tjmp %s\t\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
	}

Christian Würdig's avatar
Christian Würdig committed
663
664
	if (tbl.label)
		free(tbl.label);
Christian Würdig's avatar
Christian Würdig committed
665
666
667
668
669
670
671
672
673
674
675
676
677
678
	if (tbl.branches)
		free(tbl.branches);
}

/**
 * Emits code for a unconditional jump.
 */
void emit_Jmp(ir_node *irn, emit_env_t *env) {
	FILE *F = env->out;

	char buf[SNPRINTF_BUF_LEN];
	ir_fprintf(F, "\tjmp %s\t\t\t/* Jmp(%+F) */\n", get_cfop_target(irn, buf), get_irn_link(irn));
}

Christian Würdig's avatar
Christian Würdig committed
679
680
681
682
683
684
685
686
687
688
689
690
691


/****************************
 *                  _
 *                 (_)
 *  _ __  _ __ ___  _  ___
 * | '_ \| '__/ _ \| |/ __|
 * | |_) | | | (_) | |\__ \
 * | .__/|_|  \___/| ||___/
 * | |            _/ |
 * |_|           |__/
 ****************************/

Christian Würdig's avatar
Christian Würdig committed
692
693
694
695
696
697
/**
 * Emits code for a proj -> node
 */
void emit_Proj(ir_node *irn, emit_env_t *env) {
	ir_node *pred = get_Proj_pred(irn);

Christian Würdig's avatar
Christian Würdig committed
698
	if (get_irn_op(pred) == op_Start) {
Christian Würdig's avatar
Christian Würdig committed
699
700
701
702
703
704
705
706
707
708
		switch(get_Proj_proj(irn)) {
			case pn_Start_X_initial_exec:
				emit_Jmp(irn, env);
				break;
			default:
				break;
		}
	}
}

Christian Würdig's avatar
Christian Würdig committed
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
/********************
 *   _____      _ _
 *  / ____|    | | |
 * | |     __ _| | |
 * | |    / _` | | |
 * | |___| (_| | | |
 *  \_____\__,_|_|_|
 *
 ********************/

void emit_ia32_Call(ir_node *irn, emit_env_t *emit_env) {
	int                 i, n      = get_irn_arity(irn);
	int                 args_size = 0;
	ir_node            *sync      = get_irn_n(irn, n - 1);
	FILE               *F         = emit_env->out;
	const lc_arg_env_t *env       = ia32_get_arg_env();

	if (get_irn_op(sync) == op_Sync) {
		/* We have stack arguments */
		ir_node **args = get_Sync_preds_arr(sync);

		for (i = 0; i < get_Sync_n_preds(sync); i++) {
731
732
			ir_node *n = get_irn_n(args[i], 1);
			lc_efprintf(env, F, "\tpush %1D\t\t\t\t/* push %+F(%+F) on stack */\n", n, args[i], n);
Christian Würdig's avatar
Christian Würdig committed
733

Christian Würdig's avatar
Christian Würdig committed
734
			if (mode_is_float(get_irn_mode(n))) {
Christian Würdig's avatar
Christian Würdig committed
735
736
737
738
739
740
741
742
743
744
745
746
747
748
				args_size += 4;
			}
			else {
				args_size += 16;
			}
		}
	}

	lc_efprintf(env, F, "\tcall %C\t\t\t/* %+F */\n", irn, irn);

	if (get_irn_op(sync) == op_Sync) {
		/* We had stack arguments: clear the stack */
		fprintf(F, "\tadd %d, ", args_size);
		if (emit_env->cg->has_alloca) {
Christian Würdig's avatar
Christian Würdig committed
749
			fprintf(F, "%%ebp");
Christian Würdig's avatar
Christian Würdig committed
750
751
		}
		else {
Christian Würdig's avatar
Christian Würdig committed
752
			fprintf(F, "%%esp");
Christian Würdig's avatar
Christian Würdig committed
753
754
755
756
		}
		fprintf(F, "\t\t\t\t/* clear stack after call */\n");
	}
}
Christian Würdig's avatar
Christian Würdig committed
757
758


Christian Würdig's avatar
Christian Würdig committed
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800

/**
 * Emits code for Alloca (increase stack pointer, cpoy to destination)
 */
void emit_Alloca(ir_node *irn, emit_env_t *emit_env, int is_imm) {
	const lc_arg_env_t *env = ia32_get_arg_env();
	FILE               *F   = emit_env->out;
	char               *sp;

	if (emit_env->cg->has_alloca) {
		sp = "%ebp";
	}
	else {
		sp = "%esp";
	}


	/* allocate the memory */
	fprintf(F, "\tsub %s", sp);

	if (is_imm) {
		lc_efprintf(env, F, "%C", irn);
	}
	else {
		lc_efprintf(env, F, "%1S", irn);
	}

	fprintf(F, "\t\t\t\t/* reserve memory on stack */\n");

	/* copy the new stack pointer to destination register */
	lc_efprintf(env, F, "\tmov %s, %1D\t\t\t/* copy stack pointer to destination */\n", sp, irn);
}

void emit_ia32_Alloca(ir_node *irn, emit_env_t *emit_env) {
	emit_Alloca(irn, emit_env, 0);
}

void emit_ia32_Alloca_i(ir_node *irn, emit_env_t *emit_env) {
	emit_Alloca(irn, emit_env, 1);
}


Christian Würdig's avatar
Christian Würdig committed
801
802
803
804
805
806
807
808
809
810
/***********************************************************************************
 *                  _          __                                             _
 *                 (_)        / _|                                           | |
 *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
 * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
 * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
 * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
 *
 ***********************************************************************************/

Christian Würdig's avatar
Christian Würdig committed
811
/**
Christian Würdig's avatar
Christian Würdig committed
812
 * Emits code for a node.
Christian Würdig's avatar
Christian Würdig committed
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
 */
void ia32_emit_node(ir_node *irn, void *env) {
	emit_env_t *emit_env   = env;
	firm_dbg_module_t *mod = emit_env->mod;
	FILE              *F   = emit_env->out;

	DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));

#define IA32_EMIT(a) if (is_ia32_##a(irn))               { emit_ia32_##a(irn, emit_env); return; }
#define EMIT(a)      if (get_irn_opcode(irn) == iro_##a) { emit_##a(irn, emit_env); return; }

	/* generated int emitter functions */
	IA32_EMIT(Const);

	IA32_EMIT(Add);
	IA32_EMIT(Sub);
	IA32_EMIT(Minus);
	IA32_EMIT(Inc);
	IA32_EMIT(Dec);

	IA32_EMIT(Max);
	IA32_EMIT(Min);

	IA32_EMIT(And);
	IA32_EMIT(Or);
	IA32_EMIT(Eor);
	IA32_EMIT(Not);

	IA32_EMIT(Shl);
	IA32_EMIT(Shr);
	IA32_EMIT(Shrs);
	IA32_EMIT(RotL);
	IA32_EMIT(RotR);

	IA32_EMIT(Lea);

	IA32_EMIT(Mul);

851
	IA32_EMIT(Cdq);
Christian Würdig's avatar
Christian Würdig committed
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
	IA32_EMIT(DivMod);

	IA32_EMIT(Store);
	IA32_EMIT(Load);

	/* generated floating point emitter */
	IA32_EMIT(fConst);

	IA32_EMIT(fAdd);
	IA32_EMIT(fSub);

	IA32_EMIT(fMul);
	IA32_EMIT(fDiv);

	IA32_EMIT(fMin);
	IA32_EMIT(fMax);

	IA32_EMIT(fLoad);
	IA32_EMIT(fStore);

	/* other emitter functions */
	IA32_EMIT(CondJmp);
	IA32_EMIT(SwitchJmp);
Christian Würdig's avatar
Christian Würdig committed
875
	IA32_EMIT(Call);
Christian Würdig's avatar
Christian Würdig committed
876
877
878
879
880
881
882
883
884
885
886
887
888
889

	EMIT(Jmp);
	EMIT(Proj);

	ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
}

/**
 * Walks over the nodes in a block connected by scheduling edges
 * and emits code for each node.
 */
void ia32_gen_block(ir_node *block, void *env) {
	ir_node *irn;

Christian Würdig's avatar
Christian Würdig committed
890
891
892
	if (! is_Block(block))
		return;

Christian Würdig's avatar
Christian Würdig committed
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
	fprintf(((emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
	sched_foreach(block, irn) {
		ia32_emit_node(irn, env);
	}
}


/**
 * Emits code for function start.
 */
void ia32_emit_start(FILE *F, ir_graph *irg) {
	const char *irg_name = get_entity_name(get_irg_entity(irg));

	fprintf(F, "\t.text\n");
	fprintf(F, ".globl %s\n", irg_name);
	fprintf(F, "\t.type\t%s, @function\n", irg_name);
	fprintf(F, "%s:\n", irg_name);
}

/**
 * Emits code for function end
 */
void ia32_emit_end(FILE *F, ir_graph *irg) {
	const char *irg_name = get_entity_name(get_irg_entity(irg));

	fprintf(F, "\tret\n");
	fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
}

/**
 * Sets labels for control flow nodes (jump target)
 * TODO: Jump optimization
 */
void ia32_gen_labels(ir_node *block, void *env) {
	ir_node *pred;
	int n = get_Block_n_cfgpreds(block);

	for (n--; n >= 0; n--) {
		pred = get_Block_cfgpred(block, n);
		set_irn_link(pred, block);
	}
}

/**
 * Main driver
 */
Christian Würdig's avatar
Christian Würdig committed
939
void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
Christian Würdig's avatar
Christian Würdig committed
940
941
	emit_env_t emit_env;

Christian Würdig's avatar
Christian Würdig committed
942
943
	emit_env.mod      = firm_dbg_register("ir.be.codegen.ia32");
	emit_env.out      = F;
Christian Würdig's avatar
Christian Würdig committed
944
945
	emit_env.arch_env = cg->arch_env;
	emit_env.cg       = cg;
Christian Würdig's avatar
Christian Würdig committed
946

947
948
949
	/* set the global arch_env (needed by print hooks) */
	arch_env = cg->arch_env;

Christian Würdig's avatar
Christian Würdig committed
950
951
	ia32_emit_start(F, irg);
	irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
Christian Würdig's avatar
Christian Würdig committed
952
	irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
Christian Würdig's avatar
Christian Würdig committed
953
	ia32_emit_end(F, irg);
954
}