ia32_emitter.c 106 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
 * @file
Christian Würdig's avatar
Christian Würdig committed
22
23
24
 * @brief       This file implements the ia32 node emitter.
 * @author      Christian Wuerdig, Matthias Braun
 * @version     $Id$
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 *
 * Summary table for x86 floatingpoint compares:
 *   pnc_Eq  => !P && E
 *   pnc_Lt  => !P && B
 *   pnc_Le  => !P && BE
 *   pnc_Gt  => A
 *   pnc_Ge  => AE
 *   pnc_Lg  => P || NE
 *   pnc_Leg => NP  (ordered)
 *   pnc_Uo  => P
 *   pnc_Ue  => E
 *   pnc_Ul  => B
 *   pnc_Ule => BE
 *   pnc_Ug  => P || A
 *   pnc_Uge => P || AE
 *   pnc_Ne  => NE
41
 */
42
#include "config.h"
Christian Würdig's avatar
Christian Würdig committed
43

Christian Würdig's avatar
Christian Würdig committed
44
45
#include <limits.h>

46
#include "xmalloc.h"
47
48
#include "tv.h"
#include "iredges.h"
Christian Würdig's avatar
Christian Würdig committed
49
50
51
52
#include "debug.h"
#include "irgwalk.h"
#include "irprintf.h"
#include "irop_t.h"
Christian Würdig's avatar
Christian Würdig committed
53
#include "irargs_t.h"
54
#include "irprog_t.h"
55
#include "iredges_t.h"
56
#include "irtools.h"
57
#include "execfreq.h"
58
#include "error.h"
Matthias Braun's avatar
Matthias Braun committed
59
#include "raw_bitset.h"
60
#include "dbginfo.h"
61
#include "lc_opts.h"
Christian Würdig's avatar
Christian Würdig committed
62

63
#include "../besched.h"
64
#include "../benode.h"
Michael Beck's avatar
Michael Beck committed
65
#include "../beabi.h"
66
#include "../be_dbgout.h"
Matthias Braun's avatar
Matthias Braun committed
67
68
#include "../beemitter.h"
#include "../begnuas.h"
69
#include "../beirg.h"
70
#include "../be_dbgout.h"
Christian Würdig's avatar
Christian Würdig committed
71

72
#include "ia32_emitter.h"
Christian Würdig's avatar
Christian Würdig committed
73
#include "gen_ia32_emitter.h"
Christian Würdig's avatar
Christian Würdig committed
74
#include "gen_ia32_regalloc_if.h"
75
76
#include "ia32_nodes_attr.h"
#include "ia32_new_nodes.h"
Christian Würdig's avatar
Christian Würdig committed
77
#include "ia32_map_regs.h"
78
#include "ia32_architecture.h"
Christian Würdig's avatar
Christian Würdig committed
79
#include "bearch_ia32_t.h"
Christian Würdig's avatar
Christian Würdig committed
80

81
82
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

Christian Würdig's avatar
Christian Würdig committed
83
84
#define SNPRINTF_BUF_LEN 128

85
86
static const ia32_isa_t *isa;
static ia32_code_gen_t  *cg;
87
static char              pic_base_label[128];
88
static ir_label_t        exc_label_id;
89
static int               mark_spill_reload = 0;
90
static int               do_pic;
91

92
93
94
95
96
97
/** Return the next block in Block schedule */
static ir_node *get_prev_block_sched(const ir_node *block)
{
	return get_irn_link(block);
}

98
/** Checks if the current block is a fall-through target. */
99
static int is_fallthrough(const ir_node *cfgpred)
100
101
102
{
	ir_node *pred;

Christoph Mallon's avatar
Christoph Mallon committed
103
	if (!is_Proj(cfgpred))
104
		return 1;
105
	pred = get_Proj_pred(cfgpred);
Christoph Mallon's avatar
Christoph Mallon committed
106
	if (is_ia32_SwitchJmp(pred))
107
		return 0;
108

109
	return 1;
110
111
}

112
113
114
115
/**
 * returns non-zero if the given block needs a label
 * because of being a jump-target (and not a fall-through)
 */
116
static int block_needs_label(const ir_node *block)
117
{
118
	int need_label = 1;
119
120
	int  n_cfgpreds = get_Block_n_cfgpreds(block);

121
122
123
	if (has_Block_entity(block))
		return 1;

124
	if (n_cfgpreds == 0) {
125
		need_label = 0;
126
	} else if (n_cfgpreds == 1) {
Christoph Mallon's avatar
Christoph Mallon committed
127
128
		ir_node *cfgpred       = get_Block_cfgpred(block, 0);
		ir_node *cfgpred_block = get_nodes_block(cfgpred);
129
130
131

		if (get_prev_block_sched(block) == cfgpred_block
				&& is_fallthrough(cfgpred)) {
132
			need_label = 0;
133
134
135
136
137
138
		}
	}

	return need_label;
}

Christian Würdig's avatar
Christian Würdig committed
139
/**
Christian Würdig's avatar
Christian Würdig committed
140
 * Returns the register at in position pos.
Christian Würdig's avatar
Christian Würdig committed
141
 */
142
static const arch_register_t *get_in_reg(const ir_node *irn, int pos)
143
{
144
145
	ir_node               *op;
	const arch_register_t *reg = NULL;
Christian Würdig's avatar
Christian Würdig committed
146
147
148
149
150
151
152

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

153
	reg = arch_get_irn_register(op);
Christian Würdig's avatar
Christian Würdig committed
154

Christian Würdig's avatar
Christian Würdig committed
155
	assert(reg && "no in register found");
156

Christoph Mallon's avatar
Christoph Mallon committed
157
	if (reg == &ia32_gp_regs[REG_GP_NOREG])
158
		panic("trying to emit noreg for %+F input %d", irn, pos);
159

Christian Würdig's avatar
Christian Würdig committed
160
161
162
163
164
165
	return reg;
}

/**
 * Returns the register at out position pos.
 */
166
static const arch_register_t *get_out_reg(const ir_node *irn, int pos)
167
168
169
{
	ir_node               *proj;
	const arch_register_t *reg = NULL;
Christian Würdig's avatar
Christian Würdig committed
170
171
172
173
174
175
176

	/* 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) {
177
		assert(pos == 0);
178
		reg = arch_get_irn_register(irn);
179
	} else if (is_ia32_irn(irn)) {
180
		reg = arch_irn_get_register(irn, pos);
181
	} else {
Christian Würdig's avatar
Christian Würdig committed
182
183
184
185
186
187
		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) {
188
				reg = arch_get_irn_register(proj);
Christian Würdig's avatar
Christian Würdig committed
189
190
191
192
193
194
				break;
			}
		}
	}

	assert(reg && "no out register found");
Christian Würdig's avatar
Christian Würdig committed
195
196
197
	return reg;
}

Michael Beck's avatar
Michael Beck committed
198
/**
199
 * Add a number to a prefix. This number will not be used a second time.
Michael Beck's avatar
Michael Beck committed
200
 */
201
202
static char *get_unique_label(char *buf, size_t buflen, const char *prefix)
{
203
	static unsigned long id = 0;
204
	snprintf(buf, buflen, "%s%s%lu", be_gas_get_private_prefix(), prefix, ++id);
205
206
207
208
209
210
211
212
213
214
215
216
217
218
	return buf;
}

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

Michael Beck's avatar
Michael Beck committed
219
220
221
/**
 * Emit the name of the 8bit low register
 */
222
223
static void emit_8bit_register(const arch_register_t *reg)
{
224
	const char *reg_name = arch_register_get_name(reg);
Christian Würdig's avatar
Christian Würdig committed
225

226
227
228
229
230
	be_emit_char('%');
	be_emit_char(reg_name[1]);
	be_emit_char('l');
}

Michael Beck's avatar
Michael Beck committed
231
232
233
234
235
236
237
238
239
240
241
242
/**
 * Emit the name of the 8bit high register
 */
static void emit_8bit_register_high(const arch_register_t *reg)
{
	const char *reg_name = arch_register_get_name(reg);

	be_emit_char('%');
	be_emit_char(reg_name[1]);
	be_emit_char('h');
}

243
244
245
static void emit_16bit_register(const arch_register_t *reg)
{
	const char *reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
Christian Würdig's avatar
Christian Würdig committed
246

247
248
	be_emit_char('%');
	be_emit_string(reg_name);
249
250
}

251
252
253
254
255
256
/**
 * emit a register, possible shortened by a mode
 *
 * @param reg   the register
 * @param mode  the mode of the register or NULL for full register
 */
257
258
259
260
static void emit_register(const arch_register_t *reg, const ir_mode *mode)
{
	const char *reg_name;

Christoph Mallon's avatar
Christoph Mallon committed
261
	if (mode != NULL) {
262
		int size = get_mode_size_bits(mode);
Christoph Mallon's avatar
Christoph Mallon committed
263
264
265
		switch (size) {
			case  8: emit_8bit_register(reg);  return;
			case 16: emit_16bit_register(reg); return;
266
		}
Christoph Mallon's avatar
Christoph Mallon committed
267
		assert(mode_is_float(mode) || size == 32);
268
269
270
	}

	reg_name = arch_register_get_name(reg);
Christian Würdig's avatar
Christian Würdig committed
271

272
273
	be_emit_char('%');
	be_emit_string(reg_name);
Christian Würdig's avatar
Christian Würdig committed
274
275
}

276
void ia32_emit_source_register(const ir_node *node, int pos)
277
{
Christoph Mallon's avatar
Christoph Mallon committed
278
	const arch_register_t *reg = get_in_reg(node, pos);
279

280
	emit_register(reg, NULL);
281
282
}

283
284
static void ia32_emit_entity(ir_entity *entity, int no_pic_adjust)
{
285
	be_gas_emit_entity(entity);
286
287

	if (get_entity_owner(entity) == get_tls_type()) {
288
		if (get_entity_visibility(entity) == ir_visibility_external) {
289
290
291
292
293
294
			be_emit_cstring("@INDNTPOFF");
		} else {
			be_emit_cstring("@NTPOFF");
		}
	}

295
	if (do_pic && !no_pic_adjust) {
296
297
298
299
300
301
302
303
304
305
306
307
		be_emit_char('-');
		be_emit_string(pic_base_label);
	}
}

static void emit_ia32_Immediate_no_prefix(const ir_node *node)
{
	const ia32_immediate_attr_t *attr = get_ia32_immediate_attr_const(node);

	if (attr->symconst != NULL) {
		if (attr->sc_sign)
			be_emit_char('-');
308
		ia32_emit_entity(attr->symconst, attr->no_pic_adjust);
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
	}
	if (attr->symconst == NULL || attr->offset != 0) {
		if (attr->symconst != NULL) {
			be_emit_irprintf("%+d", attr->offset);
		} else {
			be_emit_irprintf("0x%X", attr->offset);
		}
	}
}

static void emit_ia32_Immediate(const ir_node *node)
{
	be_emit_char('$');
	emit_ia32_Immediate_no_prefix(node);
}
324
325
326
327

void ia32_emit_8bit_source_register_or_immediate(const ir_node *node, int pos)
{
	const arch_register_t *reg;
Michael Beck's avatar
Michael Beck committed
328
	const ir_node         *in = get_irn_n(node, pos);
Christoph Mallon's avatar
Christoph Mallon committed
329
	if (is_ia32_Immediate(in)) {
330
331
332
333
334
335
336
337
		emit_ia32_Immediate(in);
		return;
	}

	reg = get_in_reg(node, pos);
	emit_8bit_register(reg);
}

Michael Beck's avatar
Michael Beck committed
338
339
340
341
342
343
void ia32_emit_8bit_high_source_register(const ir_node *node, int pos)
{
	const arch_register_t *reg = get_in_reg(node, pos);
	emit_8bit_register_high(reg);
}

344
345
346
347
348
349
350
351
352
353
354
355
356
void ia32_emit_16bit_source_register_or_immediate(const ir_node *node, int pos)
{
	const arch_register_t *reg;
	const ir_node         *in = get_irn_n(node, pos);
	if (is_ia32_Immediate(in)) {
		emit_ia32_Immediate(in);
		return;
	}

	reg = get_in_reg(node, pos);
	emit_16bit_register(reg);
}

357
358
359
360
361
362
363
void ia32_emit_dest_register(const ir_node *node, int pos)
{
	const arch_register_t *reg  = get_out_reg(node, pos);

	emit_register(reg, NULL);
}

364
365
366
367
368
369
370
void ia32_emit_dest_register_size(const ir_node *node, int pos)
{
	const arch_register_t *reg  = get_out_reg(node, pos);

	emit_register(reg, get_ia32_ls_mode(node));
}

371
372
373
374
375
376
377
378
void ia32_emit_8bit_dest_register(const ir_node *node, int pos)
{
	const arch_register_t *reg  = get_out_reg(node, pos);

	emit_register(reg, mode_Bu);
}

void ia32_emit_x87_register(const ir_node *node, int pos)
Christian Würdig's avatar
Christian Würdig committed
379
{
380
	const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
381

Matthias Braun's avatar
Matthias Braun committed
382
	assert(pos < 3);
383
384
	be_emit_char('%');
	be_emit_string(attr->x87[pos]->name);
385
}
Christian Würdig's avatar
Christian Würdig committed
386

Matthias Braun's avatar
Matthias Braun committed
387
static void ia32_emit_mode_suffix_mode(const ir_mode *mode)
388
{
389
390
391
392
393
	assert(mode_is_int(mode) || mode_is_reference(mode));
	switch (get_mode_size_bits(mode)) {
		case 8:  be_emit_char('b');     return;
		case 16: be_emit_char('w');     return;
		case 32: be_emit_char('l');     return;
Christoph Mallon's avatar
Christoph Mallon committed
394
395
		/* gas docu says q is the suffix but gcc, objdump and icc use ll
		 * apparently */
Matthias Braun's avatar
Matthias Braun committed
396
397
		case 64: be_emit_cstring("ll"); return;
	}
398
	panic("Can't output mode_suffix for %+F", mode);
Christian Würdig's avatar
Christian Würdig committed
399
400
}

401
void ia32_emit_mode_suffix(const ir_node *node)
402
403
{
	ir_mode *mode = get_ia32_ls_mode(node);
Christoph Mallon's avatar
Christoph Mallon committed
404
	if (mode == NULL)
Matthias Braun's avatar
Matthias Braun committed
405
406
		mode = mode_Iu;

407
	ia32_emit_mode_suffix_mode(mode);
408
409
}

410
void ia32_emit_x87_mode_suffix(const ir_node *node)
411
{
412
413
	ir_mode *mode;

414
	/* we only need to emit the mode on address mode */
415
416
417
418
419
420
421
422
	if (get_ia32_op_type(node) == ia32_Normal)
		return;

	mode = get_ia32_ls_mode(node);
	assert(mode != NULL);

	if (mode_is_float(mode)) {
		switch (get_mode_size_bits(mode)) {
423
424
425
426
427
428
429
			case  32: be_emit_char('s'); return;
			case  64: be_emit_char('l'); return;
			/* long doubles have different sizes due to alignment on different
			 * platforms. */
			case  80:
			case  96:
			case 128: be_emit_char('t'); return;
430
431
432
433
434
435
436
437
438
439
		}
	} else {
		assert(mode_is_int(mode));
		switch (get_mode_size_bits(mode)) {
			case 16: be_emit_char('s');     return;
			case 32: be_emit_char('l');     return;
			/* gas docu says q is the suffix but gcc, objdump and icc use ll
			 * apparently */
			case 64: be_emit_cstring("ll"); return;
		}
440
	}
441
	panic("Can't output mode_suffix for %+F", mode);
442
443
}

444
static char get_xmm_mode_suffix(ir_mode *mode)
445
446
{
	assert(mode_is_float(mode));
447
	switch (get_mode_size_bits(mode)) {
Christoph Mallon's avatar
Christoph Mallon committed
448
449
450
	case 32: return 's';
	case 64: return 'd';
	default: panic("Invalid XMM mode");
451
452
453
	}
}

454
void ia32_emit_xmm_mode_suffix(const ir_node *node)
455
456
{
	ir_mode *mode = get_ia32_ls_mode(node);
457
	assert(mode != NULL);
458
459
	be_emit_char('s');
	be_emit_char(get_xmm_mode_suffix(mode));
460
461
}

462
void ia32_emit_xmm_mode_suffix_s(const ir_node *node)
463
464
465
{
	ir_mode *mode = get_ia32_ls_mode(node);
	assert(mode != NULL);
466
	be_emit_char(get_xmm_mode_suffix(mode));
467
468
}

469
void ia32_emit_extend_suffix(const ir_node *node)
470
{
471
	ir_mode *mode = get_ia32_ls_mode(node);
Christoph Mallon's avatar
Christoph Mallon committed
472
	if (get_mode_size_bits(mode) == 32)
473
		return;
Christoph Mallon's avatar
Christoph Mallon committed
474
	be_emit_char(mode_is_signed(mode) ? 's' : 'z');
475
	ia32_emit_mode_suffix_mode(mode);
Matthias Braun's avatar
Matthias Braun committed
476
477
}

478
void ia32_emit_source_register_or_immediate(const ir_node *node, int pos)
479
480
{
	ir_node *in = get_irn_n(node, pos);
Christoph Mallon's avatar
Christoph Mallon committed
481
	if (is_ia32_Immediate(in)) {
482
		emit_ia32_Immediate(in);
483
	} else {
484
485
486
		const ir_mode         *mode = get_ia32_ls_mode(node);
		const arch_register_t *reg  = get_in_reg(node, pos);
		emit_register(reg, mode);
487
488
489
	}
}

Christian Würdig's avatar
Christian Würdig committed
490
/**
491
 * Returns the target block for a control flow node.
Christian Würdig's avatar
Christian Würdig committed
492
 */
493
static ir_node *get_cfop_target_block(const ir_node *irn)
Christoph Mallon's avatar
Christoph Mallon committed
494
{
495
496
497
	assert(get_irn_mode(irn) == mode_X);
	return get_irn_link(irn);
}
Christian Würdig's avatar
Christian Würdig committed
498

499
500
501
502
503
504
/**
 * Emits the target label for a control flow node.
 */
static void ia32_emit_cfop_target(const ir_node *node)
{
	ir_node *block = get_cfop_target_block(node);
505
	be_gas_emit_block_name(block);
506
507
508
509
510
}

/*
 * positive conditions for signed compares
 */
511
512
513
514
515
516
517
518
519
static const char *const cmp2condition_s[] = {
	NULL, /* always false */
	"e",  /* == */
	"l",  /* <  */
	"le", /* <= */
	"g",  /* >  */
	"ge", /* >= */
	"ne", /* != */
	NULL  /* always true */
520
521
522
523
524
};

/*
 * positive conditions for unsigned compares
 */
525
526
527
528
529
530
531
532
533
static const char *const cmp2condition_u[] = {
	NULL, /* always false */
	"e",  /* == */
	"b",  /* <  */
	"be", /* <= */
	"a",  /* >  */
	"ae", /* >= */
	"ne", /* != */
	NULL  /* always true */
534
535
};

Michael Beck's avatar
Michael Beck committed
536
537
538
/**
 * Emit the suffix for a compare instruction.
 */
539
540
541
542
static void ia32_emit_cmp_suffix(int pnc)
{
	const char *str;

543
544
545
546
	if (pnc == ia32_pn_Cmp_parity) {
		be_emit_char('p');
		return;
	}
547

548
549
	if (pnc & ia32_pn_Cmp_float || pnc & ia32_pn_Cmp_unsigned) {
		str = cmp2condition_u[pnc & 7];
550
	} else {
551
		str = cmp2condition_s[pnc & 7];
552
553
554
555
556
	}

	be_emit_string(str);
}

557
558
typedef enum ia32_emit_mod_t {
	EMIT_RESPECT_LS   = 1U << 0,
559
	EMIT_ALTERNATE_AM = 1U << 1,
560
561
562
	EMIT_LONG         = 1U << 2,
	EMIT_HIGH_REG     = 1U << 3,
	EMIT_LOW_REG      = 1U << 4
563
564
} ia32_emit_mod_t;

565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/**
 * Emits address mode.
 */
void ia32_emit_am(const ir_node *node)
{
	ir_entity *ent       = get_ia32_am_sc(node);
	int        offs      = get_ia32_am_offs_int(node);
	ir_node   *base      = get_irn_n(node, n_ia32_base);
	int        has_base  = !is_ia32_NoReg_GP(base);
	ir_node   *index     = get_irn_n(node, n_ia32_index);
	int        has_index = !is_ia32_NoReg_GP(index);

	/* just to be sure... */
	assert(!is_ia32_use_frame(node) || get_ia32_frame_ent(node) != NULL);

	/* emit offset */
	if (ent != NULL) {
		const ia32_attr_t *attr = get_ia32_attr_const(node);
		if (is_ia32_am_sc_sign(node))
			be_emit_char('-');
		ia32_emit_entity(ent, attr->data.am_sc_no_pic_adjust);
	}

	/* also handle special case if nothing is set */
	if (offs != 0 || (ent == NULL && !has_base && !has_index)) {
		if (ent != NULL) {
			be_emit_irprintf("%+d", offs);
		} else {
			be_emit_irprintf("%d", offs);
		}
	}

	if (has_base || has_index) {
		be_emit_char('(');

		/* emit base */
		if (has_base) {
			const arch_register_t *reg = get_in_reg(node, n_ia32_base);
			emit_register(reg, NULL);
		}

		/* emit index + scale */
		if (has_index) {
			const arch_register_t *reg = get_in_reg(node, n_ia32_index);
			int scale;
			be_emit_char(',');
			emit_register(reg, NULL);

			scale = get_ia32_am_scale(node);
			if (scale > 0) {
				be_emit_irprintf(",%d", 1 << scale);
			}
		}
		be_emit_char(')');
	}
}

622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/**
 * fmt  parameter               output
 * ---- ----------------------  ---------------------------------------------
 * %%                           %
 * %AM  <node>                  address mode of the node
 * %AR  const arch_register_t*  address mode of the node or register
 * %ASx <node>                  address mode of the node or source register x
 * %Dx  <node>                  destination register x
 * %I   <node>                  immediate of the node
 * %L   <node>                  control flow target of the node
 * %M   <node>                  mode suffix of the node
 * %P   int                     condition code
 * %R   const arch_register_t*  register
 * %Sx  <node>                  source register x
 * %s   const char*             string
 * %u   unsigned int            unsigned int
638
 * %d   signed int              signed int
639
640
 *
 * x starts at 0
641
 * # modifier for %ASx, %D, %R, and %S uses ls mode of node to alter register width
642
 * * modifier does not prefix immediates with $, but AM with *
643
 * l modifier for %lu and %ld
644
645
 * > modifier to output high 8bit register (ah, bh)
 * < modifier to output low 8bit register (al, bl)
646
647
648
649
650
651
652
 */
static void ia32_emitf(const ir_node *node, const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);

	for (;;) {
653
654
		const char      *start = fmt;
		ia32_emit_mod_t  mod   = 0;
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670

		while (*fmt != '%' && *fmt != '\n' && *fmt != '\0')
			++fmt;
		if (fmt != start) {
			be_emit_string_len(start, fmt - start);
		}

		if (*fmt == '\n') {
			be_emit_finish_line_gas(node);
			++fmt;
			if (*fmt == '\0')
				break;
			continue;
		}

		if (*fmt == '\0')
Christian Würdig's avatar
Christian Würdig committed
671
			break;
672
673

		++fmt;
674
675
		for (;;) {
			switch (*fmt) {
676
677
678
			case '*': mod |= EMIT_ALTERNATE_AM; break;
			case '#': mod |= EMIT_RESPECT_LS;   break;
			case 'l': mod |= EMIT_LONG;         break;
679
680
			case '>': mod |= EMIT_HIGH_REG;     break;
			case '<': mod |= EMIT_LOW_REG;      break;
681
682
683
			default:
				goto end_of_mods;
			}
684
685
			++fmt;
		}
686
end_of_mods:
687

688
689
690
691
692
693
694
		switch (*fmt++) {
			case '%':
				be_emit_char('%');
				break;

			case 'A': {
				switch (*fmt++) {
695
emit_AM:
696
					case 'M':
697
698
						if (mod & EMIT_ALTERNATE_AM)
							be_emit_char('*');
699
700
701
702
						ia32_emit_am(node);
						break;

					case 'R': {
703
						const arch_register_t *reg = va_arg(ap, const arch_register_t*);
704
						if (get_ia32_op_type(node) == ia32_AddrModeS) {
705
							goto emit_AM;
706
						} else {
707
708
							if (mod & EMIT_ALTERNATE_AM)
								be_emit_char('*');
709
710
711
712
713
714
715
716
							emit_register(reg, NULL);
						}
						break;
					}

					case 'S':
						if (get_ia32_op_type(node) == ia32_AddrModeS) {
							++fmt;
717
							goto emit_AM;
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
						} else {
							assert(get_ia32_op_type(node) == ia32_Normal);
							goto emit_S;
						}
						break;

					default: goto unknown;
				}
				break;
			}

			case 'D': {
				unsigned               pos;
				const arch_register_t *reg;

				if (*fmt < '0' || '9' <= *fmt)
					goto unknown;

				pos = *fmt++ - '0';
				reg = get_out_reg(node, pos);
738
				emit_register(reg, mod & EMIT_RESPECT_LS ? get_ia32_ls_mode(node) : NULL);
739
740
741
742
				break;
			}

			case 'I':
743
744
745
				if (!(mod & EMIT_ALTERNATE_AM))
					be_emit_char('$');
				emit_ia32_Immediate_no_prefix(node);
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
				break;

			case 'L':
				ia32_emit_cfop_target(node);
				break;

			case 'M': {
				ia32_emit_mode_suffix_mode(get_ia32_ls_mode(node));
				break;
			}

			case 'P': {
				int pnc = va_arg(ap, int);
				ia32_emit_cmp_suffix(pnc);
				break;
			}

			case 'R': {
				const arch_register_t *reg = va_arg(ap, const arch_register_t*);
765
766
767
768
769
770
771
				if (mod & EMIT_HIGH_REG) {
					emit_8bit_register_high(reg);
				} else if (mod & EMIT_LOW_REG) {
					emit_8bit_register(reg);
				} else {
					emit_register(reg, mod & EMIT_RESPECT_LS ? get_ia32_ls_mode(node) : NULL);
				}
772
773
774
775
776
777
778
779
780
781
782
783
784
785
				break;
			}

emit_S:
			case 'S': {
				unsigned       pos;
				const ir_node *in;

				if (*fmt < '0' || '9' <= *fmt)
					goto unknown;

				pos = *fmt++ - '0';
				in  = get_irn_n(node, pos);
				if (is_ia32_Immediate(in)) {
786
787
788
					if (!(mod & EMIT_ALTERNATE_AM))
						be_emit_char('$');
					emit_ia32_Immediate_no_prefix(in);
789
				} else {
Michael Beck's avatar
Michael Beck committed
790
791
					const arch_register_t *reg;

792
793
					if (mod & EMIT_ALTERNATE_AM)
						be_emit_char('*');
Michael Beck's avatar
Michael Beck committed
794
					reg = get_in_reg(node, pos);
795
					emit_register(reg, mod & EMIT_RESPECT_LS ? get_ia32_ls_mode(node) : NULL);
796
797
798
799
800
801
802
803
804
805
				}
				break;
			}

			case 's': {
				const char *str = va_arg(ap, const char*);
				be_emit_string(str);
				break;
			}

806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
			case 'u':
				if (mod & EMIT_LONG) {
					unsigned long num = va_arg(ap, unsigned long);
					be_emit_irprintf("%lu", num);
				} else {
					unsigned num = va_arg(ap, unsigned);
					be_emit_irprintf("%u", num);
				}
				break;

			case 'd':
				if (mod & EMIT_LONG) {
					long num = va_arg(ap, long);
					be_emit_irprintf("%ld", num);
				} else {
					int num = va_arg(ap, int);
					be_emit_irprintf("%d", num);
				}
824
825
826
827
				break;

			default:
unknown:
828
				panic("unknown format conversion in ia32_emitf()");
829
		}
830
831
832
833
834
835
836
837
838
839
840
841
842
843
	}

	va_end(ap);
}

/**
 * Emits registers and/or address mode of a binary operation.
 */
void ia32_emit_binop(const ir_node *node)
{
	if (is_ia32_Immediate(get_irn_n(node, n_ia32_binary_right))) {
		ia32_emitf(node, "%#S4, %#AS3");
	} else {
		ia32_emitf(node, "%#AS4, %#S3");
Christian Würdig's avatar
Christian Würdig committed
844
	}
845
846
}

847
848
849
/**
 * Emits registers and/or address mode of a binary operation.
 */
Christoph Mallon's avatar
Christoph Mallon committed
850
851
void ia32_emit_x87_binop(const ir_node *node)
{
852
	switch (get_ia32_op_type(node)) {
853
		case ia32_Normal:
854
			{
855
856
				const ia32_x87_attr_t *x87_attr = get_ia32_x87_attr_const(node);
				const arch_register_t *in1      = x87_attr->x87[0];
857
				const arch_register_t *in       = x87_attr->x87[1];
858
				const arch_register_t *out      = x87_attr->x87[2];
859

860
861
862
863
864
				if (out == NULL) {
					out = in1;
				} else if (out == in) {
					in = in1;
				}
865

866
867
868
869
				be_emit_char('%');
				be_emit_string(arch_register_get_name(in));
				be_emit_cstring(", %");
				be_emit_string(arch_register_get_name(out));
870
871
872
			}
			break;
		case ia32_AddrModeS:
873
			ia32_emit_am(node);
874
			break;
875
		case ia32_AddrModeD:
876
877
878
879
880
		default:
			assert(0 && "unsupported op type");
	}
}

Christian Würdig's avatar
Christian Würdig committed
881
882
883
/**
 * Emits registers and/or address mode of a unary operation.
 */
Christoph Mallon's avatar
Christoph Mallon committed
884
885
void ia32_emit_unop(const ir_node *node, int pos)
{
886
887
888
	char fmt[] = "%ASx";
	fmt[3] = '0' + pos;
	ia32_emitf(node, fmt);
Christian Würdig's avatar
Christian Würdig committed
889
890
}

Matthias Braun's avatar
Matthias Braun committed
891
892
893
894
895
896
static void emit_ia32_IMul(const ir_node *node)
{
	ir_node               *left    = get_irn_n(node, n_ia32_IMul_left);
	const arch_register_t *out_reg = get_out_reg(node, pn_ia32_IMul_res);

	/* do we need the 3-address form? */
Christoph Mallon's avatar
Christoph Mallon committed
897
	if (is_ia32_NoReg_GP(left) ||
Matthias Braun's avatar
Matthias Braun committed
898
			get_in_reg(node, n_ia32_IMul_left) != out_reg) {
899
900
901
		ia32_emitf(node, "\timul%M %#S4, %#AS3, %#D0\n");
	} else {
		ia32_emitf(node, "\timul%M %#AS4, %#S3\n");
Matthias Braun's avatar
Matthias Braun committed
902
903
904
	}
}

905
906
907
/**
 * walks up a tree of copies/perms/spills/reloads to find the original value
 * that is moved around
908
 */
909
910
static ir_node *find_original_value(ir_node *node)
{
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
	if (irn_visited(node))
		return NULL;

	mark_irn_visited(node);
	if (be_is_Copy(node)) {
		return find_original_value(be_get_Copy_op(node));
	} else if (be_is_CopyKeep(node)) {
		return find_original_value(be_get_CopyKeep_op(node));
	} else if (is_Proj(node)) {
		ir_node *pred = get_Proj_pred(node);
		if (be_is_Perm(pred)) {
			return find_original_value(get_irn_n(pred, get_Proj_proj(node)));
		} else if (be_is_MemPerm(pred)) {
			return find_original_value(get_irn_n(pred, get_Proj_proj(node) + 1));
		} else if (is_ia32_Load(pred)) {
			return find_original_value(get_irn_n(pred, n_ia32_Load_mem));
927
928
929
		} else {
			return node;
		}
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
	} else if (is_ia32_Store(node)) {
		return find_original_value(get_irn_n(node, n_ia32_Store_val));
	} else if (is_Phi(node)) {
		int i, arity;
		arity = get_irn_arity(node);
		for (i = 0; i < arity; ++i) {
			ir_node *in  = get_irn_n(node, i);
			ir_node *res = find_original_value(in);

			if (res != NULL)
				return res;
		}
		return NULL;
	} else {
		return node;
945
946
947
	}
}

948
static int determine_final_pnc(const ir_node *node, int flags_pos, int pnc)
949
{
950
951
952
953
	ir_node           *flags = get_irn_n(node, flags_pos);
	const ia32_attr_t *flags_attr;
	flags = skip_Proj(flags);

Christoph Mallon's avatar
Christoph Mallon committed
954
	if (is_ia32_Sahf(flags)) {
955
		ir_node *cmp = get_irn_n(flags, n_ia32_Sahf_val);
Christoph Mallon's avatar
Christoph Mallon committed
956
		if (!(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
957
				|| is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp))) {
958
			inc_irg_visited(current_ir_graph);
959
			cmp = find_original_value(cmp);
960
			assert(cmp != NULL);
961
			assert(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
962
			       || is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp));
963
		}
964

965
		flags_attr = get_ia32_attr_const(cmp);
Christoph Mallon's avatar
Christoph Mallon committed
966
		if (flags_attr->data.ins_permuted)
967
968
			pnc = get_mirrored_pnc(pnc);
		pnc |= ia32_pn_Cmp_float;
Christoph Mallon's avatar
Christoph Mallon committed
969
	} else if (is_ia32_Ucomi(flags) || is_ia32_Fucomi(flags)
Matthias Braun's avatar
Matthias Braun committed
970
			|| is_ia32_Fucompi(flags)) {
971
972
		flags_attr = get_ia32_attr_const(flags);

Christoph Mallon's avatar
Christoph Mallon committed
973
		if (flags_attr->data.ins_permuted)
974
975
			pnc = get_mirrored_pnc(pnc);
		pnc |= ia32_pn_Cmp_float;
976
	} else {
977
978
		flags_attr = get_ia32_attr_const(flags);

Christoph Mallon's avatar
Christoph Mallon committed
979
		if (flags_attr->data.ins_permuted)
980
			pnc = get_mirrored_pnc(pnc);
Christoph Mallon's avatar
Christoph Mallon committed
981
		if (flags_attr->data.cmp_unsigned)
982
			pnc |= ia32_pn_Cmp_unsigned;
983
	}
984
985

	return pnc;
986
987
}

988
989
990
991
992
993
static pn_Cmp ia32_get_negated_pnc(pn_Cmp pnc)
{
	ir_mode *mode = pnc & ia32_pn_Cmp_float ? mode_F : mode_Iu;
	return get_negated_pnc(pnc, mode);
}

994
void ia32_emit_cmp_suffix_node(const ir_node *node, int flags_pos)
995
{
Michael Beck's avatar
Michael Beck committed
996
	pn_Cmp pnc = get_ia32_condcode(node);
997
	pnc = determine_final_pnc(node, flags_pos, pnc);
998

999
	ia32_emit_cmp_suffix(pnc);
1000
}
1001

1002
1003
1004
1005
1006
1007
/**
 * Emits an exception label for a given node.
 */
static void ia32_emit_exc_label(const ir_node *node)
{
	be_emit_string(be_gas_insn_label_prefix());
1008
	be_emit_irprintf("%lu", get_ia32_exc_label_id(node));
1009
1010
}

1011
1012
1013
/**
 * Returns the Proj with projection number proj and NOT mode_M
 */
Christoph Mallon's avatar
Christoph Mallon committed
1014
1015
static ir_node *get_proj(const ir_node *node, long proj)
{
1016
1017
1018
	const ir_edge_t *edge;
	ir_node         *src;

1019
	assert(get_irn_mode(node) == mode_T && "expected mode_T node");
1020

1021
	foreach_out_edge(node, edge) {
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
		src = get_edge_src_irn(edge);

		assert(is_Proj(src) && "Proj expected");
		if (get_irn_mode(src) == mode_M)
			continue;

		if (get_Proj_proj(src) == proj)
			return src;
	}
	return NULL;
}

1034
static int can_be_fallthrough(const ir_node *node)
1035
1036
1037
1038
1039
1040
{
	ir_node *target_block = get_cfop_target_block(node);
	ir_node *block        = get_nodes_block(node);
	return get_prev_block_sched(target_block) == block;
}

Christian Würdig's avatar
Christian Würdig committed
1041
1042
1043
/**
 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
 */
1044
static void emit_ia32_Jcc(const ir_node *node)
1045
{
1046
	int            need_parity_label = 0;
1047
1048
	const ir_node *proj_true;
	const ir_node *proj_false;
Michael Beck's avatar
Michael Beck committed
1049
	pn_Cmp         pnc = get_ia32_condcode(node);
Christian Würdig's avatar
Christian Würdig committed
1050

1051
	pnc = determine_final_pnc(node, 0, pnc);
Christian Würdig's avatar
Christian Würdig committed
1052

1053
1054
1055
	/* get both Projs */
	proj_true = get_proj(node, pn_ia32_Jcc_true);
	assert(proj_true && "Jcc without true Proj");
1056

1057
1058
	proj_false = get_proj(node, pn_ia32_Jcc_false);
	assert(proj_false && "Jcc without false Proj");
1059

1060
	if (can_be_fallthrough(proj_true)) {
1061
		/* exchange both proj's so the second one can be omitted */
1062
1063
		const ir_node *t = proj_true;

1064
1065
		proj_true  = proj_false;
		proj_false = t;
1066
		pnc        = ia32_get_negated_pnc(pnc);
1067
1068
	}

1069
1070
1071
	if (pnc & ia32_pn_Cmp_float) {
		/* Some floating point comparisons require a test of the parity flag,
		 * which indicates that the result is unordered */
1072
1073
1074
1075
1076
		switch (pnc & 0x0f) {
		case pn_Cmp_Uo: {
			ia32_emitf(proj_true, "\tjp %L\n");
			break;
		}
1077

1078
1079
1080
		case pn_Cmp_Leg:
			ia32_emitf(proj_true, "\tjnp %L\n");
			break;
1081

1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
		case pn_Cmp_Eq:
		case pn_Cmp_Lt:
		case pn_Cmp_Le:
			/* we need a local label if the false proj is a fallthrough
			 * as the falseblock might have no label emitted then */
			if (can_be_fallthrough(proj_false)) {
				need_parity_label = 1;
				ia32_emitf(proj_false, "\tjp 1f\n");
			} else {
				ia32_emitf(proj_false, "\tjp %L\n");
			}
			goto emit_jcc;
1094

1095
1096
1097
1098
1099
		case pn_Cmp_Ug:
		case pn_Cmp_Uge:
		case pn_Cmp_Ne:
			ia32_emitf(proj_true, "\tjp %L\n");
			goto emit_jcc;
1100

1101
1102
		default:
			goto emit_jcc;
1103
1104
1105
		}
	} else {
emit_jcc:
1106
		ia32_emitf(proj_true, "\tj%P %L\n", pnc);
1107
1108
	}

Christoph Mallon's avatar
Christoph Mallon committed
1109
	if (need_parity_label) {
1110
		ia32_emitf(NULL, "1:\n");
1111
1112
	}

1113
	/* the second Proj might be a fallthrough */
Matthias Braun's avatar
Matthias Braun committed
1114
	if (can_be_fallthrough(proj_false)) {
1115
		ia32_emitf(proj_false, "\t/* fallthrough to %L */\n");