amd64_emitter.c 26 KB
Newer Older
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
4
5
6
7
8
9
 */

/**
 * @file
 * @brief   emit assembler for a backend graph
 */
Matthias Braun's avatar
Matthias Braun committed
10
#include <inttypes.h>
11

12
13
14
#include "amd64_emitter.h"
#include "amd64_new_nodes.h"
#include "amd64_nodes_attr.h"
15
#include "be_t.h"
16
#include "beasm.h"
17
#include "beblocksched.h"
18
#include "bediagnostic.h"
19
20
#include "begnuas.h"
#include "beirg.h"
21
#include "benode.h"
22
#include "besched.h"
23
#include "gen_amd64_emitter.h"
24
#include "gen_amd64_regalloc_if.h"
25
#include "iredges_t.h"
26
27
#include "irgwalk.h"
#include "panic.h"
28

29
30
static be_stack_layout_t *layout;

Christoph Mallon's avatar
Christoph Mallon committed
31
32
33
34
/**
 * Returns the target block for a control flow node.
 */
static ir_node *get_cfop_target_block(const ir_node *irn)
35
{
Christoph Mallon's avatar
Christoph Mallon committed
36
	return (ir_node*)get_irn_link(irn);
37
38
}

Matthias Braun's avatar
Matthias Braun committed
39
static char get_gp_mode_suffix(amd64_insn_mode_t const insn_mode)
40
{
Matthias Braun's avatar
Matthias Braun committed
41
	switch (insn_mode) {
42
43
44
45
	case INSN_MODE_8:  return 'b';
	case INSN_MODE_16: return 'w';
	case INSN_MODE_32: return 'l';
	case INSN_MODE_64: return 'q';
Matthias Braun's avatar
Matthias Braun committed
46
	case INSN_MODE_80:
47
	case INSN_MODE_128:
48
49
	case INSN_MODE_INVALID:
		break;
50
	}
51
52
53
	panic("invalid insn mode");
}

Matthias Braun's avatar
Matthias Braun committed
54
static void amd64_emit_insn_mode_suffix(amd64_insn_mode_t const insn_mode)
55
{
Matthias Braun's avatar
Matthias Braun committed
56
	be_emit_char(get_gp_mode_suffix(insn_mode));
57
58
}

Matthias Braun's avatar
Matthias Braun committed
59
static char get_xmm_mode_suffix(amd64_insn_mode_t const insn_mode)
60
{
Matthias Braun's avatar
Matthias Braun committed
61
	switch (insn_mode) {
62
63
64
65
66
	case INSN_MODE_32:  return 's';
	case INSN_MODE_64:  return 'd';
	case INSN_MODE_128: return 'q';
	case INSN_MODE_8:
	case INSN_MODE_16:
Matthias Braun's avatar
Matthias Braun committed
67
	case INSN_MODE_80:
68
69
	case INSN_MODE_INVALID:
		break;
70
	}
71
72
73
	panic("invalid insn mode");
}

Matthias Braun's avatar
Matthias Braun committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
static void amd64_emit_xmm_mode_suffix(amd64_insn_mode_t const insn_mode)
{
	be_emit_char(get_xmm_mode_suffix(insn_mode));
}

static char get_x87_mode_suffix(amd64_insn_mode_t const insn_mode)
{
	switch (insn_mode) {
	case INSN_MODE_32: return 's';
	case INSN_MODE_64: return 'l';
	case INSN_MODE_80: return 't';
	case INSN_MODE_8:
	case INSN_MODE_16:
	case INSN_MODE_128:
	case INSN_MODE_INVALID:
		break;
	}
	panic("Invalid insn mode");
}

static void amd64_emit_x87_mode_suffix(amd64_insn_mode_t const insn_mode)
95
{
Matthias Braun's avatar
Matthias Braun committed
96
	be_emit_char(get_x87_mode_suffix(insn_mode));
97
98
}

99
static const char *get_register_name_8bit(const arch_register_t *reg)
100
{
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
	switch (reg->global_index) {
	case REG_RAX: return "al";
	case REG_RBX: return "bl";
	case REG_RCX: return "cl";
	case REG_RDX: return "dl";
	case REG_RSP: return "spl";
	case REG_RBP: return "bpl";
	case REG_RSI: return "sil";
	case REG_RDI: return "dil";
	case REG_R8:  return "r8b";
	case REG_R9:  return "r9b";
	case REG_R10: return "r10b";
	case REG_R11: return "r11b";
	case REG_R12: return "r12b";
	case REG_R13: return "r13b";
	case REG_R14: return "r14b";
	case REG_R15: return "r15b";
118
119
	}
	panic("unexpected register number");
120
121
}

122
123
124
125
126
127
128
129
130
131
132
static const char *get_register_name_8bit_high(const arch_register_t *reg)
{
	switch (reg->global_index) {
	case REG_RAX: return "ah";
	case REG_RBX: return "bh";
	case REG_RCX: return "ch";
	case REG_RDX: return "dh";
	}
	panic("unexpected register number");
}

133
static const char *get_register_name_16bit(const arch_register_t *reg)
134
{
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
	switch (reg->global_index) {
	case REG_RAX: return "ax";
	case REG_RBX: return "bx";
	case REG_RCX: return "cx";
	case REG_RDX: return "dx";
	case REG_RSP: return "sp";
	case REG_RBP: return "bp";
	case REG_RSI: return "si";
	case REG_RDI: return "di";
	case REG_R8:  return "r8w";
	case REG_R9:  return "r9w";
	case REG_R10: return "r10w";
	case REG_R11: return "r11w";
	case REG_R12: return "r12w";
	case REG_R13: return "r13w";
	case REG_R14: return "r14w";
	case REG_R15: return "r15w";
152
153
	}
	panic("unexpected register number");
154
155
}

156
static const char *get_register_name_32bit(const arch_register_t *reg)
157
{
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
	switch (reg->global_index) {
	case REG_RAX: return "eax";
	case REG_RBX: return "ebx";
	case REG_RCX: return "ecx";
	case REG_RDX: return "edx";
	case REG_RSP: return "esp";
	case REG_RBP: return "ebp";
	case REG_RSI: return "esi";
	case REG_RDI: return "edi";
	case REG_R8:  return "r8d";
	case REG_R9:  return "r9d";
	case REG_R10: return "r10d";
	case REG_R11: return "r11d";
	case REG_R12: return "r12d";
	case REG_R13: return "r13d";
	case REG_R14: return "r14d";
	case REG_R15: return "r15d";
175
176
	}
	panic("unexpected register number");
177
178
179
180
181
182
183
184
}

static void emit_register(const arch_register_t *reg)
{
	be_emit_char('%');
	be_emit_string(reg->name);
}

185
186
static const char *get_register_name_mode(const arch_register_t *reg,
                                          const amd64_insn_mode_t mode)
187
188
{
	switch (mode) {
189
190
191
	case INSN_MODE_8:  return get_register_name_8bit(reg);
	case INSN_MODE_16: return get_register_name_16bit(reg);
	case INSN_MODE_32: return get_register_name_32bit(reg);
192
	case INSN_MODE_64:
Matthias Braun's avatar
Matthias Braun committed
193
	case INSN_MODE_80:
194
195
196
	case INSN_MODE_128: return reg->name;
	case INSN_MODE_INVALID:
		break;
197
	}
198
199
200
201
202
203
	panic("invalid mode");
}

static void emit_register_insn_mode(const arch_register_t *reg,
                                    const amd64_insn_mode_t mode)
{
204
	be_emit_char('%');
205
	be_emit_string(get_register_name_mode(reg, mode));
206
207
}

208
static void emit_register_mode(const arch_register_t *reg,
209
210
                               amd64_insn_mode_t insn_mode)
{
211
	if (reg->cls == &amd64_reg_classes[CLASS_amd64_xmm]) {
212
213
214
215
216
217
		emit_register(reg);
	} else {
		emit_register_insn_mode(reg, insn_mode);
	}
}

218
typedef enum amd64_emit_mod_t {
219
220
221
222
223
	EMIT_NONE          = 0,
	EMIT_IGNORE_MODE   = 1U << 1,
	EMIT_FORCE_32      = 1U << 2,
	EMIT_CONV_DEST     = 1U << 3,
	EMIT_INDIRECT_STAR = 1U << 4,
224
} amd64_emit_mod_t;
yb9976's avatar
yb9976 committed
225
ENUM_BITSET(amd64_emit_mod_t)
226

Matthias Braun's avatar
Matthias Braun committed
227
static void emit_relocation_no_offset(x86_immediate_kind_t const kind,
228
                                      ir_entity const *const entity)
229
{
Matthias Braun's avatar
Matthias Braun committed
230
231
	be_gas_emit_entity(entity);
	switch (kind) {
Matthias Braun's avatar
Matthias Braun committed
232
233
234
	case X86_IMM_ADDR:
	case X86_IMM_PCREL:
		return;
Matthias Braun's avatar
Matthias Braun committed
235
	case X86_IMM_GOTPCREL: be_emit_cstring("@GOTPCREL"); return;
236
	case X86_IMM_PLT:      be_emit_cstring("@PLT");      return;
Matthias Braun's avatar
Matthias Braun committed
237
238
239
240
241
	case X86_IMM_VALUE:
	case X86_IMM_TLS_IE:
	case X86_IMM_TLS_LE:
	case X86_IMM_PICBASE_REL:
	case X86_IMM_FRAMEOFFSET:
242
243
	case X86_IMM_GOT:
	case X86_IMM_GOTOFF:
Matthias Braun's avatar
Matthias Braun committed
244
		break;
245
	}
Matthias Braun's avatar
Matthias Braun committed
246
	panic("unexpected or invalid immediate kind");
247
248
}

Matthias Braun's avatar
Matthias Braun committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
static void amd64_emit_immediate64(const amd64_imm64_t *const imm)
{
	if (imm->kind == X86_IMM_VALUE) {
		assert(imm->entity == NULL);
		be_emit_irprintf("0x%" PRIX64, imm->offset);
		return;
	}
	emit_relocation_no_offset(imm->kind, imm->entity);
	if (imm->offset != 0)
		be_emit_irprintf("%+" PRId64, imm->offset);
}

static void amd64_emit_immediate32(bool const prefix,
                                   x86_imm32_t const *const imm)
Matthias Braun's avatar
Matthias Braun committed
263
{
264
265
	if (prefix)
		be_emit_char('$');
Matthias Braun's avatar
Matthias Braun committed
266
267
268
	if (imm->kind == X86_IMM_VALUE) {
		assert(imm->entity == NULL);
		be_emit_irprintf("%" PRId32, imm->offset);
Matthias Braun's avatar
Matthias Braun committed
269
		return;
Matthias Braun's avatar
Matthias Braun committed
270
	}
Matthias Braun's avatar
Matthias Braun committed
271
272
273
	emit_relocation_no_offset(imm->kind, imm->entity);
	if (imm->offset != 0)
		be_emit_irprintf("%+" PRId32, imm->offset);
Matthias Braun's avatar
Matthias Braun committed
274
275
}

yb9976's avatar
yb9976 committed
276
#ifndef NDEBUG
277
278
279
280
281
static bool is_fp_relative(const ir_entity *entity)
{
	ir_type *owner = get_entity_owner(entity);
	return is_frame_type(owner) || owner == layout->arg_type;
}
yb9976's avatar
yb9976 committed
282
#endif
283

Matthias Braun's avatar
Matthias Braun committed
284
static void amd64_emit_addr(const ir_node *const node,
Matthias Braun's avatar
Matthias Braun committed
285
                            const amd64_addr_t *const addr)
Matthias Braun's avatar
Matthias Braun committed
286
{
287
288
289
290
	int32_t            const offset  = addr->immediate.offset;
	ir_entity         *const entity  = addr->immediate.entity;
	x86_addr_variant_t const variant = addr->variant;
	assert(variant != X86_ADDR_INVALID);
291

Matthias Braun's avatar
Matthias Braun committed
292
	if (entity != NULL) {
293
		assert(addr->immediate.kind != X86_IMM_VALUE);
294
295
296
		assert(!is_fp_relative(entity));
		emit_relocation_no_offset(addr->immediate.kind, entity);
		if (offset != 0)
Matthias Braun's avatar
Matthias Braun committed
297
			be_emit_irprintf("%+" PRId32, offset);
298
	} else if (offset != 0 || variant == X86_ADDR_JUST_IMM) {
299
300
		assert(addr->immediate.kind == X86_IMM_VALUE);
		be_emit_irprintf("%" PRId32, offset);
Matthias Braun's avatar
Matthias Braun committed
301
302
	}

303
	if (variant != X86_ADDR_JUST_IMM) {
Matthias Braun's avatar
Matthias Braun committed
304
305
		be_emit_char('(');

306
		if (variant == X86_ADDR_RIP) {
Matthias Braun's avatar
Matthias Braun committed
307
			be_emit_cstring("%rip");
308
		} else {
309
			if (x86_addr_variant_has_base(variant)) {
310
311
312
313
				arch_register_t const *const reg
					= arch_get_irn_register_in(node, addr->base_input);
				emit_register(reg);
			}
Matthias Braun's avatar
Matthias Braun committed
314

315
			if (x86_addr_variant_has_index(variant)) {
316
317
318
319
				be_emit_char(',');
				arch_register_t const *const reg
					= arch_get_irn_register_in(node, addr->index_input);
				emit_register(reg);
Matthias Braun's avatar
Matthias Braun committed
320

321
322
323
324
				unsigned scale = addr->log_scale;
				if (scale > 0)
					be_emit_irprintf(",%u", 1 << scale);
			}
Matthias Braun's avatar
Matthias Braun committed
325
326
327
328
329
		}
		be_emit_char(')');
	}
}

330
static void amd64_emit_am(const ir_node *const node, bool indirect_star)
Matthias Braun's avatar
Matthias Braun committed
331
{
Matthias Braun's avatar
Matthias Braun committed
332
	const amd64_addr_attr_t *const attr = get_amd64_addr_attr_const(node);
333

Matthias Braun's avatar
Matthias Braun committed
334
335
336
337
	switch ((amd64_op_mode_t)attr->base.op_mode) {
	case AMD64_OP_REG_IMM: {
		const amd64_binop_addr_attr_t *const binop_attr
			= (const amd64_binop_addr_attr_t*)attr;
338
		amd64_emit_immediate32(true, &binop_attr->u.immediate);
Matthias Braun's avatar
Matthias Braun committed
339
		be_emit_cstring(", ");
340
		goto emit_reg_in0;
Matthias Braun's avatar
Matthias Braun committed
341
	}
Matthias Braun's avatar
Matthias Braun committed
342
	case AMD64_OP_REG_REG: {
Matthias Braun's avatar
Matthias Braun committed
343
		const arch_register_t *reg1 = arch_get_irn_register_in(node, 1);
344
		emit_register_mode(reg1, attr->insn_mode);
Matthias Braun's avatar
Matthias Braun committed
345
		be_emit_cstring(", ");
346
		goto emit_reg_in0;
Matthias Braun's avatar
Matthias Braun committed
347
	}
348
	case AMD64_OP_REG_ADDR: {
Matthias Braun's avatar
Matthias Braun committed
349
350
351
		const amd64_binop_addr_attr_t *const binop_attr
			= (const amd64_binop_addr_attr_t*)attr;
		amd64_emit_addr(node, &attr->addr);
Matthias Braun's avatar
Matthias Braun committed
352
353
		be_emit_cstring(", ");
		const arch_register_t *reg
Matthias Braun's avatar
Matthias Braun committed
354
			= arch_get_irn_register_in(node, binop_attr->u.reg_input);
355
		emit_register_mode(reg, binop_attr->base.insn_mode);
356
357
		return;
	}
358
359
360
361
362
	case AMD64_OP_ADDR_IMM: {
		const amd64_binop_addr_attr_t *const binop_attr
			= (const amd64_binop_addr_attr_t*)attr;
		amd64_emit_immediate32(true, &binop_attr->u.immediate);
		be_emit_cstring(", ");
363
		goto emit_addr;
364
	}
Matthias Braun's avatar
Matthias Braun committed
365
	case AMD64_OP_ADDR:
Matthias Braun's avatar
Matthias Braun committed
366
	case AMD64_OP_X87_ADDR_REG:
367
368
		if (indirect_star)
			be_emit_char('*');
369
		goto emit_addr;
370
371
372
373
374
	case AMD64_OP_ADDR_REG: {
		amd64_binop_addr_attr_t const *const binop_attr = (amd64_binop_addr_attr_t const*)attr;
		arch_register_t const *const reg = arch_get_irn_register_in(node, binop_attr->u.reg_input);
		emit_register_mode(reg, binop_attr->base.insn_mode);
		be_emit_cstring(", ");
375
emit_addr:
376
377
378
		amd64_emit_addr(node, &attr->addr);
		return;
	}
379
	case AMD64_OP_REG: {
380
381
		if (indirect_star)
			be_emit_char('*');
382
emit_reg_in0:;
Matthias Braun's avatar
Matthias Braun committed
383
		const arch_register_t *reg = arch_get_irn_register_in(node, 0);
384
		emit_register_mode(reg, attr->insn_mode);
Matthias Braun's avatar
Matthias Braun committed
385
386
		return;
	}
387
	case AMD64_OP_IMM32:
388
		amd64_emit_immediate32(false, &attr->addr.immediate);
Matthias Braun's avatar
Matthias Braun committed
389
		return;
390

Matthias Braun's avatar
Matthias Braun committed
391
392
	case AMD64_OP_X87:
		return;
Matthias Braun's avatar
Matthias Braun committed
393
394
395
396
	case AMD64_OP_IMM64:
	case AMD64_OP_NONE:
	case AMD64_OP_SHIFT_REG:
	case AMD64_OP_SHIFT_IMM:
397
	case AMD64_OP_CC:
Matthias Braun's avatar
Matthias Braun committed
398
		break;
Matthias Braun's avatar
Matthias Braun committed
399
400
401
402
403
404
	}
	panic("invalid op_mode");
}

static void emit_shiftop(const ir_node *const node)
{
Matthias Braun's avatar
Matthias Braun committed
405
	amd64_shift_attr_t const *const attr = get_amd64_shift_attr_const(node);
406

Matthias Braun's avatar
Matthias Braun committed
407
408
409
	switch (attr->base.op_mode) {
	case AMD64_OP_SHIFT_IMM: {
		be_emit_irprintf("$0x%X, ", attr->immediate);
Matthias Braun's avatar
Matthias Braun committed
410
		const arch_register_t *reg = arch_get_irn_register_in(node, 0);
411
		emit_register_mode(reg, attr->insn_mode);
Matthias Braun's avatar
Matthias Braun committed
412
413
		return;
	}
Matthias Braun's avatar
Matthias Braun committed
414
	case AMD64_OP_SHIFT_REG: {
Matthias Braun's avatar
Matthias Braun committed
415
416
		const arch_register_t *reg0 = arch_get_irn_register_in(node, 0);
		const arch_register_t *reg1 = arch_get_irn_register_in(node, 1);
417
		emit_register_mode(reg1, INSN_MODE_8);
Matthias Braun's avatar
Matthias Braun committed
418
		be_emit_cstring(", ");
419
		emit_register_mode(reg0, attr->insn_mode);
Matthias Braun's avatar
Matthias Braun committed
420
421
		return;
	}
Matthias Braun's avatar
Matthias Braun committed
422
423
	default:
		break;
Matthias Braun's avatar
Matthias Braun committed
424
425
426
427
	}
	panic("invalid op_mode for shiftop");
}

Christoph Mallon's avatar
Christoph Mallon committed
428
void amd64_emitf(ir_node const *const node, char const *fmt, ...)
429
{
Christoph Mallon's avatar
Christoph Mallon committed
430
431
	va_list ap;
	va_start(ap, fmt);
432

Christoph Mallon's avatar
Christoph Mallon committed
433
434
435
	be_emit_char('\t');
	for (;;) {
		char const *start = fmt;
436

Christoph Mallon's avatar
Christoph Mallon committed
437
438
439
440
441
		while (*fmt != '%' && *fmt != '\n' && *fmt != '\0')
			++fmt;
		if (fmt != start) {
			be_emit_string_len(start, fmt - start);
		}
442

Christoph Mallon's avatar
Christoph Mallon committed
443
444
445
446
447
448
449
		if (*fmt == '\n') {
			be_emit_char('\n');
			be_emit_write_line();
			be_emit_char('\t');
			++fmt;
			continue;
		}
450

Christoph Mallon's avatar
Christoph Mallon committed
451
452
453
454
		if (*fmt == '\0')
			break;

		++fmt;
455
456
457
		amd64_emit_mod_t mod = EMIT_NONE;
		for (;;) {
			switch (*fmt) {
458
459
460
461
			case '^': mod |= EMIT_IGNORE_MODE;   break;
			case '3': mod |= EMIT_FORCE_32;      break;
			case '#': mod |= EMIT_CONV_DEST;     break;
			case '*': mod |= EMIT_INDIRECT_STAR; break;
462
463
464
465
466
467
			default:
				goto end_of_mods;
			}
			++fmt;
		}
end_of_mods:
Christoph Mallon's avatar
Christoph Mallon committed
468
469
470
471
472
473
474
475

		switch (*fmt++) {
			arch_register_t const *reg;

			case '%':
				be_emit_char('%');
				break;

476
477
			case 'A':
				switch (*fmt++) {
478
479
480
481
482
483
484
485
				case 'F': {
					x87_attr_t const *const attr
						= amd64_get_x87_attr_const(node);
					char const *const fmt
						= attr->res_in_reg ? "%%st, %%%s" : "%%%s, %%st";
					be_emit_irprintf(fmt, attr->reg->name);
					break;
				}
Matthias Braun's avatar
Matthias Braun committed
486
				case 'M':
487
					amd64_emit_am(node, mod & EMIT_INDIRECT_STAR);
488
					break;
Matthias Braun's avatar
Matthias Braun committed
489
				default: {
Matthias Braun's avatar
Matthias Braun committed
490
491
492
					amd64_addr_attr_t const *const attr
						= get_amd64_addr_attr_const(node);
					amd64_emit_addr(node, &attr->addr);
Matthias Braun's avatar
Matthias Braun committed
493
					--fmt;
494
495
496
497
				}
				}
				break;

Christoph Mallon's avatar
Christoph Mallon committed
498
			case 'C': {
499
500
				amd64_movimm_attr_t const *const attr
					= get_amd64_movimm_attr_const(node);
Matthias Braun's avatar
Matthias Braun committed
501
				amd64_emit_immediate64(&attr->immediate);
Christoph Mallon's avatar
Christoph Mallon committed
502
503
504
505
				break;
			}

			case 'D':
506
				if (!is_digit(*fmt))
Christoph Mallon's avatar
Christoph Mallon committed
507
508
509
510
511
512
513
514
515
516
					goto unknown;
				reg = arch_get_irn_register_out(node, *fmt++ - '0');
				goto emit_R;

			case 'E': {
				ir_entity const *const ent = va_arg(ap, ir_entity const*);
				be_gas_emit_entity(ent);
				break;
			}

517
518
519
520
521
522
			case 'F': {
				if (*fmt == 'M') {
					++fmt;
					amd64_addr_attr_t const *const attr
						= get_amd64_addr_attr_const(node);
					amd64_emit_x87_mode_suffix(attr->insn_mode);
Matthias Braun's avatar
Matthias Braun committed
523
524
525
526
527
528
				} else if (*fmt == 'P') {
					++fmt;
					x87_attr_t const *const attr
						= amd64_get_x87_attr_const(node);
					if (attr->pop)
						be_emit_char('p');
529
530
531
532
533
534
				} else if (*fmt == '0') {
					++fmt;
					x87_attr_t const *const attr
						= amd64_get_x87_attr_const(node);
					be_emit_char('%');
					be_emit_string(attr->reg->name);
535
536
				} else if (*fmt == 'R') {
					++fmt;
Matthias Braun's avatar
Matthias Braun committed
537
538
					x87_attr_t const *const attr
						= amd64_get_x87_attr_const(node);
539
540
541
542
543
					/** see also ia32_emitter comment */
					if (attr->reverse)
						be_emit_char('r');
				} else
					goto unknown;
544
545
546
				break;
			}

Christoph Mallon's avatar
Christoph Mallon committed
547
548
549
550
551
552
			case 'L': {
				ir_node *const block = get_cfop_target_block(node);
				be_gas_emit_block_name(block);
				break;
			}

553
554
555
			case 'P': {
				x86_condition_code_t cc;
				if (*fmt == 'X') {
Andreas Fried's avatar
Andreas Fried committed
556
					// Fetch cc from varargs
557
558
					++fmt;
					cc = (x86_condition_code_t)va_arg(ap, int);
Andreas Fried's avatar
Andreas Fried committed
559
560
561
562
563
				} else if (is_digit(*fmt)) {
					// Format string is backwards compatible to IA32 backend.
					// Fetch cc from node attributes
					++fmt;
					cc = get_amd64_cc_attr_const(node)->cc;
564
565
566
567
568
569
570
				} else {
					panic("unknown modifier");
				}
				x86_emit_condition_code(cc);
				break;
			}

Christoph Mallon's avatar
Christoph Mallon committed
571
572
			case 'R':
				reg = va_arg(ap, arch_register_t const*);
Matthias Braun's avatar
Matthias Braun committed
573
				goto emit_R;
Christoph Mallon's avatar
Christoph Mallon committed
574
575

			case 'S': {
Matthias Braun's avatar
Matthias Braun committed
576
577
578
579
580
				if (*fmt == 'O') {
					++fmt;
					emit_shiftop(node);
					break;
				}
581
				if (!is_digit(*fmt))
Christoph Mallon's avatar
Christoph Mallon committed
582
					goto unknown;
583
				int const pos = *fmt++ - '0';
Christoph Mallon's avatar
Christoph Mallon committed
584
				reg = arch_get_irn_register_in(node, pos);
Matthias Braun's avatar
Matthias Braun committed
585
586
587
588
emit_R:
				if (mod & EMIT_IGNORE_MODE) {
					emit_register(reg);
				} else if (mod & EMIT_FORCE_32) {
589
					emit_register_mode(reg, INSN_MODE_32);
Matthias Braun's avatar
Matthias Braun committed
590
591
592
593
				} else if (mod & EMIT_CONV_DEST) {
					amd64_insn_mode_t src_mode  = get_amd64_insn_mode(node);
					amd64_insn_mode_t dest_mode = src_mode == INSN_MODE_64
					                            ? INSN_MODE_64 : INSN_MODE_32;
594
					emit_register_mode(reg, dest_mode);
Matthias Braun's avatar
Matthias Braun committed
595
				} else {
596
597
					amd64_insn_mode_t insn_mode = get_amd64_insn_mode(node);
					emit_register_mode(reg, insn_mode);
Matthias Braun's avatar
Matthias Braun committed
598
599
				}
				break;
Christoph Mallon's avatar
Christoph Mallon committed
600
601
			}

602
			case 'M': {
Matthias Braun's avatar
Matthias Braun committed
603
604
605
606
				if (*fmt == 'S') {
					++fmt;
					const amd64_shift_attr_t *attr
						= get_amd64_shift_attr_const(node);
607
					amd64_emit_insn_mode_suffix(attr->insn_mode);
Matthias Braun's avatar
Matthias Braun committed
608
609
610
611
				} else if (*fmt == 'M') {
					++fmt;
					const amd64_movimm_attr_t *attr
						= get_amd64_movimm_attr_const(node);
612
613
614
					amd64_emit_insn_mode_suffix(attr->insn_mode);
				} else if (*fmt == 'X') {
					++fmt;
615
616
617
					amd64_addr_attr_t const *const attr
						= get_amd64_addr_attr_const(node);
					amd64_emit_xmm_mode_suffix(attr->insn_mode);
Matthias Braun's avatar
Matthias Braun committed
618
619
620
				} else {
					amd64_addr_attr_t const *const attr
						= get_amd64_addr_attr_const(node);
621
					amd64_emit_insn_mode_suffix(attr->insn_mode);
Matthias Braun's avatar
Matthias Braun committed
622
				}
623
624
625
				break;
			}

Christoph Mallon's avatar
Christoph Mallon committed
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
			case 'd': {
				int const num = va_arg(ap, int);
				be_emit_irprintf("%d", num);
				break;
			}

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

			case 'u': {
				unsigned const num = va_arg(ap, unsigned);
				be_emit_irprintf("%u", num);
				break;
			}

			default:
unknown:
				panic("unknown format conversion");
		}
	}
649

Christoph Mallon's avatar
Christoph Mallon committed
650
651
	be_emit_finish_line_gas(node);
	va_end(ap);
652
653
}

654
655
656
657
658
/**
 * Returns the next block in a block schedule.
 */
static ir_node *sched_next_block(const ir_node *block)
{
659
    return (ir_node*)get_irn_link(block);
660
661
}

662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
static const char *get_register_name_ir_mode(const arch_register_t *reg,
                                             ir_mode *mode)
{
	if (get_mode_arithmetic(mode) != irma_twos_complement)
		return reg->name;
	switch (get_mode_size_bits(mode)) {
	case 8:  return get_register_name_8bit(reg);
	case 16: return get_register_name_16bit(reg);
	case 32: return get_register_name_32bit(reg);
	case 64: return reg->name;
	default:
		panic("unexpected mode size");
	}
}

static void emit_amd64_asm_register(const arch_register_t *reg, char modifier,
                                    ir_mode *mode)
{
	const char *name;
	switch (modifier) {
	case '\0':
		name = mode != NULL ? get_register_name_ir_mode(reg, mode) : reg->name;
		break;
	case  'b': name = get_register_name_8bit(reg); break;
	case  'h': name = get_register_name_8bit_high(reg); break;
	case  'w': name = get_register_name_16bit(reg); break;
	case  'k': name = get_register_name_32bit(reg); break;
	case  'q': name = reg->name; break;
	// gcc also knows 'x' V4SFmode, 't' V8SFmode, 'y' "st(0)" instead of "st",
	// 'd' duplicate operand for AVX instruction
	default:
		panic("invalid asm op modifier");
	}
	be_emit_char('%');
	be_emit_string(name);
}

699
700
static void emit_amd64_asm_operand(ir_node const *const node, char const modifier, unsigned const pos)
{
701
	x86_asm_operand_kind_t required;
702
703
704
705
706
707
708
	switch (modifier) {
	case '\0':
	case 'b':
	case 'h':
	case 'k':
	case 'q':
	case 'w':
709
710
711
712
713
		required = ASM_OP_INVALID;
		break;

	case 'c':
		required = ASM_OP_IMMEDIATE;
714
715
716
717
718
719
720
		break;

	default:
		be_errorf(node, "asm contains unknown modifier '%c'", modifier);
		return;
	}

721
722
	be_asm_attr_t     const *const attr = get_be_asm_attr_const(node);
	x86_asm_operand_t const *const op   = &((x86_asm_operand_t const*)attr->operands)[pos];
723
724
725
726
727
728
729

	if (required != ASM_OP_INVALID && required != op->kind) {
		char const *const name = x86_get_constraint_name(required);
		be_errorf(node, "asm modifier '%c' requires an operand of type '%s'", modifier, name);
		return;
	}

730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
	switch ((x86_asm_operand_kind_t)op->kind) {
	case ASM_OP_INVALID:
		panic("invalid asm operand");

	case ASM_OP_IN_REG: {
		arch_register_t const *const reg = arch_get_irn_register_in(node, op->inout_pos);
		emit_amd64_asm_register(reg, modifier, op->u.mode);
		return;
	}

	case ASM_OP_OUT_REG: {
		arch_register_t const *const reg = arch_get_irn_register_out(node, op->inout_pos);
		emit_amd64_asm_register(reg, modifier, op->u.mode);
		return;
	}

	case ASM_OP_MEMORY: {
		arch_register_t const *const reg = arch_get_irn_register_in(node, op->inout_pos);
		be_emit_irprintf("(%%%s)", reg->name);
		return;
	}

	case ASM_OP_IMMEDIATE: {
753
		amd64_emit_immediate32(modifier != 'c', &op->u.imm32);
754
755
756
757
758
759
		return;
	}
	}
	panic("invalid asm operand kind");
}

760
761
static void emit_amd64_asm(const ir_node *node)
{
762
	be_emit_asm(node, emit_amd64_asm_operand);
763
764
}

765
766
767
/**
 * Emit a Jmp.
 */
768
static void emit_amd64_jmp(const ir_node *node)
769
770
771
772
773
774
775
776
777
{
	ir_node *block, *next_block;

	/* for now, the code works for scheduled and non-schedules blocks */
	block = get_nodes_block(node);

	/* we have a block schedule */
	next_block = sched_next_block(block);
	if (get_cfop_target_block(node) != next_block) {
Christoph Mallon's avatar
Christoph Mallon committed
778
779
780
		amd64_emitf(node, "jmp %L");
	} else if (be_options.verbose_asm) {
		amd64_emitf(node, "/* fallthrough to %L */");
781
782
783
	}
}

784
785
786
787
788
static void emit_jumptable_target(ir_entity const *const table,
                                  ir_node const *const proj_x)
{
	ir_node const *const block = get_cfop_target_block(proj_x);
	be_gas_emit_block_name(block);
789
	if (be_options.pic_style != BE_PIC_NONE) {
790
791
792
793
794
		be_emit_char('-');
		be_gas_emit_entity(table);
	}
}

795
static void emit_amd64_jmp_switch(const ir_node *node)
796
797
798
{
	const amd64_switch_jmp_attr_t *attr = get_amd64_switch_jmp_attr_const(node);

799
	amd64_emitf(node, "jmp %*AM");
800
801
	ir_mode *entry_mode = be_options.pic_style != BE_PIC_NONE ? mode_Iu
	                                                          : mode_Lu;
802
	be_emit_jump_table(node, attr->table, attr->table_entity, entry_mode,
803
	                   emit_jumptable_target);
804
805
}

806
807
808
809
810
811
812
813
814
815
816
static x86_condition_code_t determine_final_cc(ir_node const *const flags,
                                               x86_condition_code_t cc)
{
	if (is_amd64_fucomi(flags)) {
		amd64_x87_attr_t const *const attr = get_amd64_x87_attr_const(flags);
		if (attr->x87.reverse)
			cc = x86_invert_condition_code(cc);
	}
	return cc;
}

817
818
819
/**
 * Emit a Compare with conditional branch.
 */
820
static void emit_amd64_jcc(const ir_node *irn)
821
{
822
823
824
825
	const ir_node         *proj_true  = NULL;
	const ir_node         *proj_false = NULL;
	const ir_node         *block;
	const ir_node         *next_block;
826
827
828
	const ir_node         *flags = get_irn_n(irn, n_amd64_jcc_eflags);
	const amd64_cc_attr_t *attr  = get_amd64_cc_attr_const(irn);
	x86_condition_code_t   cc    = determine_final_cc(flags, attr->cc);
829
830
831

	foreach_out_edge(irn, edge) {
		ir_node *proj = get_edge_src_irn(edge);
832
		unsigned nr = get_Proj_num(proj);
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
		if (nr == pn_Cond_true) {
			proj_true = proj;
		} else {
			proj_false = proj;
		}
	}

	/* for now, the code works for scheduled and non-schedules blocks */
	block = get_nodes_block(irn);

	/* we have a block schedule */
	next_block = sched_next_block(block);

	if (get_cfop_target_block(proj_true) == next_block) {
		/* exchange both proj's so the second one can be omitted */
		const ir_node *t = proj_true;

		proj_true  = proj_false;
		proj_false = t;
852
		cc         = x86_negate_condition_code(cc);
853
854
	}

855
856
857
858
859
860
861
862
863
864
	if (cc & x86_cc_float_parity_cases) {
		/* Some floating point comparisons require a test of the parity flag,
		 * which indicates that the result is unordered */
		if (cc & x86_cc_negated) {
			amd64_emitf(proj_true, "jp %L");
		} else {
			amd64_emitf(proj_false, "jp %L");
		}
	}

865
	/* emit the true proj */
866
	amd64_emitf(proj_true, "j%PX %L", (int)cc);
Christoph Mallon's avatar
Christoph Mallon committed
867

868
869
870
871
	if (get_cfop_target_block(proj_false) == next_block) {
		if (be_options.verbose_asm)
			amd64_emitf(proj_false, "/* fallthrough to %L */");
	} else  {
Christoph Mallon's avatar
Christoph Mallon committed
872
		amd64_emitf(proj_false, "jmp %L");
873
874
875
	}
}

876
static void emit_amd64_mov_gp(const ir_node *node)
877
{
Matthias Braun's avatar
Matthias Braun committed
878
879
	const amd64_addr_attr_t *attr = get_amd64_addr_attr_const(node);
	switch (attr->insn_mode) {
880
881
882
883
	case INSN_MODE_8:  amd64_emitf(node, "movzbq %AM, %^D0"); return;
	case INSN_MODE_16: amd64_emitf(node, "movzwq %AM, %^D0"); return;
	case INSN_MODE_32: amd64_emitf(node, "movl %AM, %3D0");   return;
	case INSN_MODE_64: amd64_emitf(node, "movq %AM, %^D0");   return;
Matthias Braun's avatar
Matthias Braun committed
884
	case INSN_MODE_80:
885
886
887
	case INSN_MODE_128:
	case INSN_MODE_INVALID:
		break;
888
	}
889
	panic("invalid insn mode");
890
891
}

892
893
894
895
896
/**
 * emit copy node
 */
static void emit_be_Copy(const ir_node *irn)
{
897
898
	arch_register_t const *const out = arch_get_irn_register_out(irn, 0);
	if (arch_get_irn_register_in(irn, 0) == out) {
899
900
901
902
		/* omitted Copy */
		return;
	}

903
904
	arch_register_class_t const *const cls = out->cls;
	if (cls == &amd64_reg_classes[CLASS_amd64_gp]) {
905
		amd64_emitf(irn, "mov %^S0, %^D0");
906
907
	} else if (cls == &amd64_reg_classes[CLASS_amd64_xmm]) {
		amd64_emitf(irn, "movapd %^S0, %^D0");
908
909
	} else if (cls == &amd64_reg_classes[CLASS_amd64_x87]) {
		/* nothing to do */
910
	} else {
911
		panic("move not supported for this register class");
912
913
	}
}
914

915
916
static void emit_be_Perm(const ir_node *node)
{
917
918
	arch_register_t const *const reg0 = arch_get_irn_register_out(node, 0);
	arch_register_t const *const reg1 = arch_get_irn_register_out(node, 1);
919

920
921
	arch_register_class_t const* const cls = reg0->cls;
	assert(cls == reg1->cls && "Register class mismatch at Perm");
922

923
924
925
926
927
928
929
	if (cls == &amd64_reg_classes[CLASS_amd64_gp]) {
		amd64_emitf(node, "xchg %^R, %^R", reg0, reg1);
	} else if (cls == &amd64_reg_classes[CLASS_amd64_xmm]) {
		amd64_emitf(node, "pxor %^R, %^R", reg0, reg1);
		amd64_emitf(node, "pxor %^R, %^R", reg1, reg0);
		amd64_emitf(node, "pxor %^R, %^R", reg0, reg1);
	} else {
930
931
932
933
		panic("unexpected register class in be_Perm (%+F)", node);
	}
}

934
935
936
937
938
939
940
941
942
943
944
/**
 * Emits code to increase stack pointer.
 */
static void emit_be_IncSP(const ir_node *node)
{
	int offs = be_get_IncSP_offset(node);

	if (offs == 0)
		return;

	if (offs > 0) {
Matthias Braun's avatar
Matthias Braun committed
945
		amd64_emitf(node, "subq $%d, %^D0", offs);
946
	} else {
Matthias Braun's avatar
Matthias Braun committed
947
		amd64_emitf(node, "addq $%d, %^D0", -offs);
948
949
950
	}
}

951
952
953
954
955
956
/**
 * Enters the emitter functions for handled nodes into the generic
 * pointer of an opcode.
 */
static void amd64_register_emitters(void)
{
957
	be_init_emitters();
958
959
960
961

	/* register all emitter functions defined in spec */
	amd64_register_spec_emitters();

962
963
964
	be_set_emitter(op_amd64_jcc,        emit_amd64_jcc);
	be_set_emitter(op_amd64_jmp,        emit_amd64_jmp);
	be_set_emitter(op_amd64_jmp_switch, emit_amd64_jmp_switch);
965
	be_set_emitter(op_amd64_mov_gp,     emit_amd64_mov_gp);
966
	be_set_emitter(op_be_Asm,           emit_amd64_asm);
967
968
969
970
	be_set_emitter(op_be_Copy,          emit_be_Copy);
	be_set_emitter(op_be_CopyKeep,      emit_be_Copy);
	be_set_emitter(op_be_IncSP,         emit_be_IncSP);
	be_set_emitter(op_be_Perm,          emit_be_Perm);
971
972
973
974
975
976
}

/**
 * Walks over the nodes in a block connected by scheduling edges
 * and emits code for each node.
 */
977
static void amd64_gen_block(ir_node *block)
978
{
979
	be_gas_begin_block(block, true);
980
981

	sched_foreach(block, node) {
982
		be_emit_node(node);
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
	}
}


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

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