ia32_emitter.c 59.4 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
 *
 * 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
 */
Christian Würdig's avatar
Christian Würdig committed
26
#ifdef HAVE_CONFIG_H
27
#include "config.h"
Christian Würdig's avatar
Christian Würdig committed
28
29
#endif

Christian Würdig's avatar
Christian Würdig committed
30
31
#include <limits.h>

32
#include "xmalloc.h"
33
34
#include "tv.h"
#include "iredges.h"
Christian Würdig's avatar
Christian Würdig committed
35
36
37
38
#include "debug.h"
#include "irgwalk.h"
#include "irprintf.h"
#include "irop_t.h"
Christian Würdig's avatar
Christian Würdig committed
39
#include "irargs_t.h"
40
#include "irprog_t.h"
41
#include "iredges_t.h"
42
#include "execfreq.h"
43
#include "error.h"
Matthias Braun's avatar
Matthias Braun committed
44
#include "raw_bitset.h"
Christian Würdig's avatar
Christian Würdig committed
45

46
#include "../besched_t.h"
Christian Würdig's avatar
Christian Würdig committed
47
#include "../benode_t.h"
Michael Beck's avatar
Michael Beck committed
48
#include "../beabi.h"
49
#include "../be_dbgout.h"
Matthias Braun's avatar
Matthias Braun committed
50
51
#include "../beemitter.h"
#include "../begnuas.h"
52
#include "../beirg_t.h"
Christian Würdig's avatar
Christian Würdig committed
53

54
#include "ia32_emitter.h"
Christian Würdig's avatar
Christian Würdig committed
55
#include "gen_ia32_emitter.h"
Christian Würdig's avatar
Christian Würdig committed
56
#include "gen_ia32_regalloc_if.h"
57
58
#include "ia32_nodes_attr.h"
#include "ia32_new_nodes.h"
Christian Würdig's avatar
Christian Würdig committed
59
#include "ia32_map_regs.h"
Christian Würdig's avatar
Christian Würdig committed
60
#include "bearch_ia32_t.h"
Christian Würdig's avatar
Christian Würdig committed
61

62
63
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

64
#define BLOCK_PREFIX ".L"
Michael Beck's avatar
Michael Beck committed
65

Christian Würdig's avatar
Christian Würdig committed
66
67
68
#define SNPRINTF_BUF_LEN 128

/**
Christian Würdig's avatar
Christian Würdig committed
69
 * Returns the register at in position pos.
Christian Würdig's avatar
Christian Würdig committed
70
 */
71
72
73
74
75
static
const arch_register_t *get_in_reg(ia32_emit_env_t *env, const ir_node *irn,
                                  int pos)
{
	const arch_env_t       *arch_env = env->arch_env;
Christian Würdig's avatar
Christian Würdig committed
76
77
78
79
80
81
82
83
84
	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
85
	reg = arch_get_irn_register(arch_env, op);
Christian Würdig's avatar
Christian Würdig committed
86

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

89
90
	/* in case of a joker register: just return a valid register */
	if (arch_register_type_is(reg, joker)) {
Matthias Braun's avatar
Matthias Braun committed
91
		const arch_register_req_t *req;
92
93

		/* ask for the requirements */
Matthias Braun's avatar
Matthias Braun committed
94
		req = arch_get_register_req(arch_env, irn, pos);
95

Matthias Braun's avatar
Matthias Braun committed
96
		if (arch_register_req_is(req, limited)) {
97
			/* in case of limited requirements: get the first allowed register */
Matthias Braun's avatar
Matthias Braun committed
98
99
			unsigned idx = rbitset_next(req->limited, 0, 1);
			reg = arch_register_for_index(req->cls, idx);
100
		} else {
101
			/* otherwise get first register in class */
Matthias Braun's avatar
Matthias Braun committed
102
			reg = arch_register_for_index(req->cls, 0);
103
104
		}
	}
105

Christian Würdig's avatar
Christian Würdig committed
106
107
108
109
110
111
	return reg;
}

/**
 * Returns the register at out position pos.
 */
112
113
114
115
116
117
118
static
const arch_register_t *get_out_reg(ia32_emit_env_t *env, const ir_node *irn,
                                   int pos)
{
	const arch_env_t      *arch_env = env->arch_env;
	ir_node               *proj;
	const arch_register_t *reg = NULL;
Christian Würdig's avatar
Christian Würdig committed
119
120
121
122
123
124
125
126

	/* 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);
127
	} else if (is_ia32_irn(irn)) {
Christian Würdig's avatar
Christian Würdig committed
128
		reg = get_ia32_out_reg(irn, pos);
129
	} else {
Christian Würdig's avatar
Christian Würdig committed
130
131
132
133
134
135
136
137
138
139
140
141
142
		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
143
144
145
	return reg;
}

146
147
148
/**
 * Determine the gnu assembler suffix that indicates a mode
 */
149
150
static
char get_mode_suffix(const ir_mode *mode) {
151
152
153
154
155
156
157
158
	if(mode_is_float(mode)) {
		switch(get_mode_size_bits(mode)) {
		case 32:
			return 's';
		case 64:
			return 'l';
		case 80:
			return 't';
159
		}
160
	} else {
Michael Beck's avatar
Michael Beck committed
161
		assert(mode_is_int(mode) || mode_is_reference(mode) || mode_is_character(mode));
162
163
164
165
166
167
168
169
170
		switch(get_mode_size_bits(mode)) {
		case 64:
			return 'q';
		case 32:
			return 'l';
		case 16:
			return 'w';
		case 8:
			return 'b';
Christian Würdig's avatar
Christian Würdig committed
171
		}
172
173
174
	}
	panic("Can't output mode_suffix for %+F\n", mode);
}
Christian Würdig's avatar
Christian Würdig committed
175

176
177
static
int produces_result(const ir_node *node) {
178
179
180
181
182
183
184
185
	return !(is_ia32_St(node) ||
		is_ia32_CondJmp(node) ||
		is_ia32_xCondJmp(node) ||
		is_ia32_CmpSet(node) ||
		is_ia32_xCmpSet(node) ||
		is_ia32_SwitchJmp(node));
}

186
187
188
static
const char *ia32_get_reg_name_for_mode(ia32_emit_env_t *env, ir_mode *mode,
                                       const arch_register_t *reg) {
189
190
191
192
193
194
195
	switch(get_mode_size_bits(mode)) {
		case 8:
			return ia32_get_mapped_reg_name(env->isa->regs_8bit, reg);
		case 16:
			return ia32_get_mapped_reg_name(env->isa->regs_16bit, reg);
		default:
			return (char *)arch_register_get_name(reg);
Christian Würdig's avatar
Christian Würdig committed
196
	}
Christian Würdig's avatar
Christian Würdig committed
197
198
}

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

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

Matthias Braun's avatar
Matthias Braun committed
220
221
222
223
224
225
226
227
228
229
// we have no C++ and can't define an implicit ia32_emit_env_t* cast to
// be_emit_env_t* so we cheat a bit...
#define be_emit_char(env,c)             be_emit_char(env->emit,c)
#define be_emit_string(env,s)           be_emit_string(env->emit,s)
#undef be_emit_cstring
#define be_emit_cstring(env,x)          { be_emit_string_len(env->emit, x, sizeof(x)-1); }
#define be_emit_ident(env,i)            be_emit_ident(env->emit,i)
#define be_emit_write_line(env)         be_emit_write_line(env->emit)
#define be_emit_finish_line_gas(env,n)  be_emit_finish_line_gas(env->emit,n)
#define be_emit_pad_comment(env)        be_emit_pad_comment(env->emit)
Michael Beck's avatar
Michael Beck committed
230

231
void ia32_emit_source_register(ia32_emit_env_t *env, const ir_node *node, int pos)
Christian Würdig's avatar
Christian Würdig committed
232
{
233
	const arch_register_t *reg = get_in_reg(env, node, pos);
234
	const char *reg_name = arch_register_get_name(reg);
Christian Würdig's avatar
Christian Würdig committed
235

236
	assert(pos < get_irn_arity(node));
Christian Würdig's avatar
Christian Würdig committed
237

Matthias Braun's avatar
Matthias Braun committed
238
239
	be_emit_char(env, '%');
	be_emit_string(env, reg_name);
240
241
242
}

void ia32_emit_dest_register(ia32_emit_env_t *env, const ir_node *node, int pos) {
243
	const arch_register_t *reg = get_out_reg(env, node, pos);
244
	const char *reg_name = arch_register_get_name(reg);
Christian Würdig's avatar
Christian Würdig committed
245

Matthias Braun's avatar
Matthias Braun committed
246
247
	be_emit_char(env, '%');
	be_emit_string(env, reg_name);
Christian Würdig's avatar
Christian Würdig committed
248
249
}

250
void ia32_emit_x87_name(ia32_emit_env_t *env, const ir_node *node, int pos)
Christian Würdig's avatar
Christian Würdig committed
251
{
252
	ia32_attr_t *attr = get_ia32_attr(node);
253

Matthias Braun's avatar
Matthias Braun committed
254
	assert(pos < 3);
Matthias Braun's avatar
Matthias Braun committed
255
256
	be_emit_char(env, '%');
	be_emit_string(env, attr->x87[pos]->name);
257
}
Christian Würdig's avatar
Christian Würdig committed
258

259
260
261
void ia32_emit_immediate(ia32_emit_env_t *env, const ir_node *node)
{
	tarval *tv;
262
	ir_entity *ent;
263
	ident *id;
Christian Würdig's avatar
Christian Würdig committed
264

265
266
	be_emit_char(env, '$');

267
268
269
	switch(get_ia32_immop_type(node)) {
	case ia32_ImmConst:
		tv = get_ia32_Immop_tarval(node);
270
271
		be_emit_tarval(env->emit, tv);
		return;
272
	case ia32_ImmSymConst:
273
274
275
		ent = get_ia32_Immop_symconst(node);
		mark_entity_visited(ent);
		id = get_entity_ld_ident(ent);
276
		be_emit_ident(env, id);
277
		return;
278
279
	case ia32_ImmNone:
		break;
Christian Würdig's avatar
Christian Würdig committed
280
	}
Christian Würdig's avatar
Christian Würdig committed
281

282
283
284
	assert(0);
	be_emit_string(env, "BAD");
	return;
285
}
Christian Würdig's avatar
Christian Würdig committed
286

Matthias Braun's avatar
Matthias Braun committed
287
288
static
void ia32_emit_mode_suffix_mode(ia32_emit_env_t *env, const ir_mode *mode)
289
{
Matthias Braun's avatar
Matthias Braun committed
290
	be_emit_char(env, get_mode_suffix(mode));
Christian Würdig's avatar
Christian Würdig committed
291
292
}

Matthias Braun's avatar
Matthias Braun committed
293
void ia32_emit_mode_suffix(ia32_emit_env_t *env, const ir_node *node)
294
295
{
	ir_mode *mode = get_ia32_ls_mode(node);
Matthias Braun's avatar
Matthias Braun committed
296
297
298
299
	if(mode == NULL)
		mode = mode_Iu;

	ia32_emit_mode_suffix_mode(env, mode);
300
301
}

302
303
304
305
306
307
308
void ia32_emit_x87_mode_suffix(ia32_emit_env_t *env, const ir_node *node)
{
	ir_mode *mode = get_ia32_ls_mode(node);
	if(mode != NULL)
		ia32_emit_mode_suffix_mode(env, mode);
}

309
310
static
char get_xmm_mode_suffix(ir_mode *mode)
311
312
313
314
315
316
317
318
319
320
321
322
323
{
	assert(mode_is_float(mode));
	switch(get_mode_size_bits(mode)) {
	case 32:
		return 's';
	case 64:
		return 'd';
	default:
		assert(0);
	}
	return '%';
}

324
325
326
void ia32_emit_xmm_mode_suffix(ia32_emit_env_t *env, const ir_node *node)
{
	ir_mode *mode = get_ia32_ls_mode(node);
327
	assert(mode != NULL);
Matthias Braun's avatar
Matthias Braun committed
328
329
	be_emit_char(env, 's');
	be_emit_char(env, get_xmm_mode_suffix(mode));
330
331
332
333
334
335
}

void ia32_emit_xmm_mode_suffix_s(ia32_emit_env_t *env, const ir_node *node)
{
	ir_mode *mode = get_ia32_ls_mode(node);
	assert(mode != NULL);
Matthias Braun's avatar
Matthias Braun committed
336
	be_emit_char(env, get_xmm_mode_suffix(mode));
337
338
}

339
340
341
342
343
void ia32_emit_extend_suffix(ia32_emit_env_t *env, const ir_mode *mode)
{
	if(get_mode_size_bits(mode) == 32)
		return;
	if(mode_is_signed(mode)) {
Matthias Braun's avatar
Matthias Braun committed
344
		be_emit_char(env, 's');
345
	} else {
Matthias Braun's avatar
Matthias Braun committed
346
347
348
349
		be_emit_char(env, 'z');
	}
}

350
351
static
void ia32_emit_function_object(ia32_emit_env_t *env, const char *name)
Matthias Braun's avatar
Matthias Braun committed
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
{
	switch (be_gas_flavour) {
	case GAS_FLAVOUR_NORMAL:
		be_emit_cstring(env, "\t.type\t");
		be_emit_string(env, name);
		be_emit_cstring(env, ", @function\n");
		be_emit_write_line(env);
		break;
	case GAS_FLAVOUR_MINGW:
		be_emit_cstring(env, "\t.def\t");
		be_emit_string(env, name);
		be_emit_cstring(env, ";\t.scl\t2;\t.type\t32;\t.endef\n");
		be_emit_write_line(env);
		break;
	default:
		break;
	}
}

371
372
static
void ia32_emit_function_size(ia32_emit_env_t *env, const char *name)
Matthias Braun's avatar
Matthias Braun committed
373
374
375
376
377
378
379
380
381
382
383
384
{
	switch (be_gas_flavour) {
	case GAS_FLAVOUR_NORMAL:
		be_emit_cstring(env, "\t.size\t");
		be_emit_string(env, name);
		be_emit_cstring(env, ", .-");
		be_emit_string(env, name);
		be_emit_char(env, '\n');
		be_emit_write_line(env);
		break;
	default:
		break;
Christian Würdig's avatar
Christian Würdig committed
385
386
387
	}
}

Matthias Braun's avatar
Matthias Braun committed
388
389


Christian Würdig's avatar
Christian Würdig committed
390
391
392
/**
 * Emits registers and/or address mode of a binary operation.
 */
393
394
void ia32_emit_binop(ia32_emit_env_t *env, const ir_node *node) {
	switch(get_ia32_op_type(node)) {
Christian Würdig's avatar
Christian Würdig committed
395
		case ia32_Normal:
396
397
			if (is_ia32_ImmConst(node) || is_ia32_ImmSymConst(node)) {
				ia32_emit_immediate(env, node);
Matthias Braun's avatar
Matthias Braun committed
398
				be_emit_cstring(env, ", ");
399
400
				ia32_emit_source_register(env, node, 2);
			} else {
401
402
403
				const arch_register_t *in1 = get_in_reg(env, node, 2);
				const arch_register_t *in2 = get_in_reg(env, node, 3);
				const arch_register_t *out = produces_result(node) ? get_out_reg(env, node, 0) : NULL;
Christian Würdig's avatar
Christian Würdig committed
404
				const arch_register_t *in;
Christian Würdig's avatar
Christian Würdig committed
405
				const char            *in_name;
Christian Würdig's avatar
Christian Würdig committed
406

Christian Würdig's avatar
Christian Würdig committed
407
408
409
				in      = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
				out     = out ? out : in1;
				in_name = arch_register_get_name(in);
Christian Würdig's avatar
Christian Würdig committed
410

411
				if (is_ia32_emit_cl(node)) {
Christian Würdig's avatar
Christian Würdig committed
412
413
414
415
					assert(REGS_ARE_EQUAL(&ia32_gp_regs[REG_ECX], in) && "shift operation needs ecx");
					in_name = "cl";
				}

Matthias Braun's avatar
Matthias Braun committed
416
417
418
419
				be_emit_char(env, '%');
				be_emit_string(env, in_name);
				be_emit_cstring(env, ", %");
				be_emit_string(env, arch_register_get_name(out));
Christian Würdig's avatar
Christian Würdig committed
420
421
422
			}
			break;
		case ia32_AddrModeS:
423
424
			ia32_emit_am(env, node);
			be_emit_cstring(env, ", ");
425
426
427
428
429
430
431
			if (is_ia32_ImmConst(node) || is_ia32_ImmSymConst(node)) {
				assert(!produces_result(node) && "Source AM with Const must not produce result");
				ia32_emit_immediate(env, node);
			} else if (produces_result(node)) {
				ia32_emit_dest_register(env, node, 0);
			} else {
				ia32_emit_source_register(env, node, 2);
432
			}
Christian Würdig's avatar
Christian Würdig committed
433
434
			break;
		case ia32_AddrModeD:
435
436
			if (is_ia32_ImmConst(node) || is_ia32_ImmSymConst(node)) {
				ia32_emit_immediate(env, node);
Matthias Braun's avatar
Matthias Braun committed
437
				be_emit_cstring(env, ", ");
438
439
				ia32_emit_am(env, node);
			} else {
440
441
				const arch_register_t *in1 = get_in_reg(env, node,
				                                        get_irn_arity(node) == 5 ? 3 : 2);
442
				ir_mode               *mode = get_ia32_ls_mode(node);
Christian Würdig's avatar
Christian Würdig committed
443
				const char            *in_name;
Christian Würdig's avatar
Christian Würdig committed
444
445
446

				in_name = ia32_get_reg_name_for_mode(env, mode, in1);

447
				if (is_ia32_emit_cl(node)) {
Christian Würdig's avatar
Christian Würdig committed
448
449
450
					assert(REGS_ARE_EQUAL(&ia32_gp_regs[REG_ECX], in1) && "shift operation needs ecx");
					in_name = "cl";
				}
451

Matthias Braun's avatar
Matthias Braun committed
452
453
454
				be_emit_char(env, '%');
				be_emit_string(env, in_name);
				be_emit_cstring(env, ", ");
455
				ia32_emit_am(env, node);
Christian Würdig's avatar
Christian Würdig committed
456
457
458
459
460
			}
			break;
		default:
			assert(0 && "unsupported op type");
	}
461
462
}

463
464
465
/**
 * Emits registers and/or address mode of a binary operation.
 */
466
467
void ia32_emit_x87_binop(ia32_emit_env_t *env, const ir_node *node) {
	switch(get_ia32_op_type(node)) {
468
		case ia32_Normal:
469
470
471
472
473
			if (is_ia32_ImmConst(node) || is_ia32_ImmSymConst(node)) {
				// should not happen...
				assert(0);
			} else {
				ia32_attr_t *attr = get_ia32_attr(node);
474
475
476
477
478
				const arch_register_t *in1 = attr->x87[0];
				const arch_register_t *in2 = attr->x87[1];
				const arch_register_t *out = attr->x87[2];
				const arch_register_t *in;

479
480
				in  = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
				out = out ? out : in1;
481

Matthias Braun's avatar
Matthias Braun committed
482
483
484
485
				be_emit_char(env, '%');
				be_emit_string(env, arch_register_get_name(in));
				be_emit_cstring(env, ", %");
				be_emit_string(env, arch_register_get_name(out));
486
487
488
489
			}
			break;
		case ia32_AddrModeS:
		case ia32_AddrModeD:
490
			ia32_emit_am(env, node);
491
492
493
494
495
496
			break;
		default:
			assert(0 && "unsupported op type");
	}
}

Christian Würdig's avatar
Christian Würdig committed
497
498
499
/**
 * Emits registers and/or address mode of a unary operation.
 */
500
501
void ia32_emit_unop(ia32_emit_env_t *env, const ir_node *node) {
	switch(get_ia32_op_type(node)) {
Christian Würdig's avatar
Christian Würdig committed
502
		case ia32_Normal:
503
504
505
			if (is_ia32_ImmConst(node) || is_ia32_ImmSymConst(node)) {
				ia32_emit_immediate(env, node);
			} else {
Matthias Braun's avatar
Matthias Braun committed
506
				if (is_ia32_Mul(node) || is_ia32_IMul1OP(node)) {
507
					ia32_emit_source_register(env, node, 3);
Matthias Braun's avatar
Matthias Braun committed
508
509
				} else if(is_ia32_IDiv(node) || is_ia32_Div(node)) {
					ia32_emit_source_register(env, node, 4);
510
511
512
513
				} else if(is_ia32_Push(node)) {
					ia32_emit_source_register(env, node, 2);
				} else if(is_ia32_Pop(node)) {
					ia32_emit_dest_register(env, node, 1);
514
				} else {
515
					ia32_emit_dest_register(env, node, 0);
516
				}
517
			}
Christian Würdig's avatar
Christian Würdig committed
518
			break;
519
		case ia32_AddrModeS:
520
521
		case ia32_AddrModeD:
			ia32_emit_am(env, node);
522
			break;
Christian Würdig's avatar
Christian Würdig committed
523
524
525
526
527
528
		default:
			assert(0 && "unsupported op type");
	}
}

/**
529
 * Emits address mode.
Christian Würdig's avatar
Christian Würdig committed
530
 */
531
void ia32_emit_am(ia32_emit_env_t *env, const ir_node *node) {
532
533
534
535
536
537
	ir_entity *ent       = get_ia32_am_sc(node);
	int        offs      = get_ia32_am_offs_int(node);
	ir_node   *base      = get_irn_n(node, 0);
	int        has_base  = !is_ia32_NoReg_GP(base);
	ir_node   *index     = get_irn_n(node, 1);
	int        has_index = !is_ia32_NoReg_GP(index);
Christian Würdig's avatar
Christian Würdig committed
538

539
	/* just to be sure... */
540
	assert(!is_ia32_use_frame(node) || get_ia32_frame_ent(node) != NULL);
Christian Würdig's avatar
Christian Würdig committed
541

542
	/* emit offset */
543
544
545
546
547
	if (ent != NULL) {
		ident *id;

		mark_entity_visited(ent);
		id = get_entity_ld_ident(ent);
548
		if (is_ia32_am_sc_sign(node))
Matthias Braun's avatar
Matthias Braun committed
549
550
			be_emit_char(env, '-');
		be_emit_ident(env, id);
Matthias Braun's avatar
Matthias Braun committed
551
552
553
554
555
556
557
558

		if(get_entity_owner(ent) == get_tls_type()) {
			if (get_entity_visibility(ent) == visibility_external_allocated) {
				be_emit_cstring(env, "@INDNTPOFF");
			} else {
				be_emit_cstring(env, "@NTPOFF");
			}
		}
Christian Würdig's avatar
Christian Würdig committed
559
560
	}

561
	if(offs != 0) {
562
		if(ent != NULL) {
Matthias Braun's avatar
Matthias Braun committed
563
			be_emit_irprintf(env->emit, "%+d", offs);
564
		} else {
Matthias Braun's avatar
Matthias Braun committed
565
			be_emit_irprintf(env->emit, "%d", offs);
566
		}
567
	}
Christian Würdig's avatar
Christian Würdig committed
568

569
	if (has_base || has_index) {
Matthias Braun's avatar
Matthias Braun committed
570
		be_emit_char(env, '(');
Christian Würdig's avatar
Christian Würdig committed
571

572
		/* emit base */
573
		if (has_base) {
574
			ia32_emit_source_register(env, node, 0);
Christian Würdig's avatar
Christian Würdig committed
575
576
		}

577
		/* emit index + scale */
578
579
		if (has_index) {
			int scale;
Matthias Braun's avatar
Matthias Braun committed
580
			be_emit_char(env, ',');
581
			ia32_emit_source_register(env, node, 1);
582

583
584
			scale = get_ia32_am_scale(node);
			if (scale > 0) {
Matthias Braun's avatar
Matthias Braun committed
585
				be_emit_irprintf(env->emit, ",%d", 1 << get_ia32_am_scale(node));
586
587
			}
		}
Matthias Braun's avatar
Matthias Braun committed
588
		be_emit_char(env, ')');
Christian Würdig's avatar
Christian Würdig committed
589
	}
590
}
Christian Würdig's avatar
Christian Würdig committed
591

Christian Würdig's avatar
Christian Würdig committed
592
593
594
595
596
597
598
599
600
601
/*************************************************
 *                 _ _                         _
 *                (_) |                       | |
 *   ___ _ __ ___  _| |_    ___ ___  _ __   __| |
 *  / _ \ '_ ` _ \| | __|  / __/ _ \| '_ \ / _` |
 * |  __/ | | | | | | |_  | (_| (_) | | | | (_| |
 *  \___|_| |_| |_|_|\__|  \___\___/|_| |_|\__,_|
 *
 *************************************************/

Christian Würdig's avatar
Christian Würdig committed
602
#undef IA32_DO_EMIT
Christian Würdig's avatar
Christian Würdig committed
603
#define IA32_DO_EMIT(irn) ia32_fprintf_format(F, irn, cmd_buf, cmnt_buf)
Christian Würdig's avatar
Christian Würdig committed
604

605
606
607
608
/*
 * coding of conditions
 */
struct cmp2conditon_t {
Christian Würdig's avatar
Christian Würdig committed
609
610
	const char *name;
	pn_Cmp      num;
611
612
613
614
615
};

/*
 * positive conditions for signed compares
 */
616
617
static
const struct cmp2conditon_t cmp2condition_s[] = {
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
	{ 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 },     /* != */
	{ NULL,              pn_Cmp_Leg},     /* Floating point: ordered */
	{ NULL,              pn_Cmp_Uo },     /* Floating point: unordered */
	{ "e",               pn_Cmp_Ue },     /* Floating point: unordered or == */
	{ "b",               pn_Cmp_Ul },     /* Floating point: unordered or < */
	{ "be",              pn_Cmp_Ule },    /* Floating point: unordered or <= */
	{ "a",               pn_Cmp_Ug },     /* Floating point: unordered or > */
	{ "ae",              pn_Cmp_Uge },    /* Floating point: unordered or >= */
	{ "ne",              pn_Cmp_Ne },     /* Floating point: unordered or != */
	{ NULL,              pn_Cmp_True },   /* always true */
634
635
636
637
638
};

/*
 * positive conditions for unsigned compares
 */
639
640
static
const struct cmp2conditon_t cmp2condition_u[] = {
Christian Würdig's avatar
Christian Würdig committed
641
642
643
	{ NULL,              pn_Cmp_False },  /* always false */
	{ "e",               pn_Cmp_Eq },     /* == */
	{ "b",               pn_Cmp_Lt },     /* < */
Christian Würdig's avatar
Christian Würdig committed
644
	{ "be",              pn_Cmp_Le },     /* <= */
Christian Würdig's avatar
Christian Würdig committed
645
646
647
648
	{ "a",               pn_Cmp_Gt },     /* > */
	{ "ae",              pn_Cmp_Ge },     /* >= */
	{ "ne",              pn_Cmp_Lg },     /* != */
	{ NULL,              pn_Cmp_True },   /* always true */
649
650
651
652
653
};

/*
 * returns the condition code
 */
654
655
static
const char *get_cmp_suffix(int cmp_code)
656
{
657
658
	assert( (cmp2condition_s[cmp_code & 15].num) == (cmp_code & 15));
	assert( (cmp2condition_u[cmp_code & 7].num) == (cmp_code & 7));
659

660
661
662
663
664
	if((cmp_code & ia32_pn_Cmp_Unsigned)) {
		return cmp2condition_u[cmp_code & 7].name;
	} else {
		return cmp2condition_s[cmp_code & 15].name;
	}
665
666
}

667
668
void ia32_emit_cmp_suffix(ia32_emit_env_t *env, long pnc)
{
Matthias Braun's avatar
Matthias Braun committed
669
	be_emit_string(env, get_cmp_suffix(pnc));
670
671
672
}


673
674
675
/**
 * Returns the target block for a control flow node.
 */
676
677
static
ir_node *get_cfop_target_block(const ir_node *irn) {
678
679
680
	return get_irn_link(irn);
}

681
682
683
684
685
686
687
static
void ia32_emit_block_name(ia32_emit_env_t *env, const ir_node *block)
{
	be_emit_cstring(env, BLOCK_PREFIX);
	be_emit_irprintf(env->emit, "%d", get_irn_node_nr(block));
}

Christian Würdig's avatar
Christian Würdig committed
688
689
690
/**
 * Returns the target label for a control flow node.
 */
691
static
692
693
void ia32_emit_cfop_target(ia32_emit_env_t * env, const ir_node *node) {
	ir_node *block = get_cfop_target_block(node);
694

695
	ia32_emit_block_name(env, block);
Christian Würdig's avatar
Christian Würdig committed
696
}
697

698
699
/** Return the next block in Block schedule */
static ir_node *next_blk_sched(const ir_node *block) {
Michael Beck's avatar
Michael Beck committed
700
	return get_irn_link(block);
701
702
}

703
704
705
/**
 * Returns the Proj with projection number proj and NOT mode_M
 */
706
707
static
ir_node *get_proj(const ir_node *node, long proj) {
708
709
710
	const ir_edge_t *edge;
	ir_node         *src;

711
	assert(get_irn_mode(node) == mode_T && "expected mode_T node");
712

713
	foreach_out_edge(node, edge) {
714
715
716
717
718
719
720
721
722
723
724
725
		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;
}

Christian Würdig's avatar
Christian Würdig committed
726
727
728
/**
 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
 */
729
730
731
static
void finish_CondJmp(ia32_emit_env_t *env, const ir_node *node, ir_mode *mode,
                    long pnc) {
732
733
734
735
736
	const ir_node *proj_true;
	const ir_node *proj_false;
	const ir_node *block;
	const ir_node *next_block;
	int flipped = 0;
Christian Würdig's avatar
Christian Würdig committed
737

738
	/* get both Proj's */
739
	proj_true = get_proj(node, pn_Cond_true);
740
	assert(proj_true && "CondJmp without true Proj");
Christian Würdig's avatar
Christian Würdig committed
741

742
	proj_false = get_proj(node, pn_Cond_false);
743
744
	assert(proj_false && "CondJmp without false Proj");

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

	/* we have a block schedule */
749
	next_block = next_blk_sched(block);
750

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

755
756
757
758
		proj_true  = proj_false;
		proj_false = t;
		flipped    = 1;
		pnc        = get_negated_pnc(pnc, mode);
759
760
	}

761
762
	/* in case of unordered compare, check for parity */
	if (pnc & pn_Cmp_Uo) {
Matthias Braun's avatar
Matthias Braun committed
763
		be_emit_cstring(env, "\tjp ");
764
		ia32_emit_cfop_target(env, proj_true);
Matthias Braun's avatar
Matthias Braun committed
765
		be_emit_finish_line_gas(env, proj_true);
766
767
	}

Matthias Braun's avatar
Matthias Braun committed
768
	be_emit_cstring(env, "\tj");
769
	ia32_emit_cmp_suffix(env, pnc);
Matthias Braun's avatar
Matthias Braun committed
770
	be_emit_char(env, ' ');
771
	ia32_emit_cfop_target(env, proj_true);
Matthias Braun's avatar
Matthias Braun committed
772
	be_emit_finish_line_gas(env, proj_true);
Christian Würdig's avatar
Christian Würdig committed
773

774
	/* the second Proj might be a fallthrough */
775
	if (get_cfop_target_block(proj_false) != next_block) {
Matthias Braun's avatar
Matthias Braun committed
776
		be_emit_cstring(env, "\tjmp ");
777
		ia32_emit_cfop_target(env, proj_false);
Matthias Braun's avatar
Matthias Braun committed
778
		be_emit_finish_line_gas(env, proj_false);
779
	} else {
780
		be_emit_cstring(env, "\t/* fallthrough to ");
781
		ia32_emit_cfop_target(env, proj_false);
Matthias Braun's avatar
Matthias Braun committed
782
783
		be_emit_cstring(env, " */");
		be_emit_finish_line_gas(env, proj_false);
784
	}
Christian Würdig's avatar
Christian Würdig committed
785
786
787
}

/**
Christian Würdig's avatar
Christian Würdig committed
788
 * Emits code for conditional jump.
Christian Würdig's avatar
Christian Würdig committed
789
 */
790
791
static
void CondJmp_emitter(ia32_emit_env_t *env, const ir_node *node) {
Matthias Braun's avatar
Matthias Braun committed
792
	be_emit_cstring(env, "\tcmp ");
793
	ia32_emit_binop(env, node);
Matthias Braun's avatar
Matthias Braun committed
794
	be_emit_finish_line_gas(env, node);
Christian Würdig's avatar
Christian Würdig committed
795

796
	finish_CondJmp(env, node, mode_Iu, get_ia32_pncode(node));
Christian Würdig's avatar
Christian Würdig committed
797
798
}

Christian Würdig's avatar
Christian Würdig committed
799
800
801
/**
 * Emits code for conditional jump with two variables.
 */
802
803
static
void emit_ia32_CondJmp(ia32_emit_env_t *env, const ir_node *node) {
804
	CondJmp_emitter(env, node);
Christian Würdig's avatar
Christian Würdig committed
805
806
}

807
808
809
/**
 * Emits code for conditional test and jump.
 */
810
811
static
void TestJmp_emitter(ia32_emit_env_t *env, const ir_node *node) {
812
	if(is_ia32_ImmSymConst(node) || is_ia32_ImmConst(node)) {
813
		be_emit_cstring(env, "\ttest ");
814
		ia32_emit_immediate(env, node);
Matthias Braun's avatar
Matthias Braun committed
815
		be_emit_cstring(env, ", ");
816
		ia32_emit_source_register(env, node, 0);
Matthias Braun's avatar
Matthias Braun committed
817
		be_emit_finish_line_gas(env, node);
818
	} else {
Matthias Braun's avatar
Matthias Braun committed
819
		be_emit_cstring(env, "\ttest ");
820
		ia32_emit_source_register(env, node, 1);
Matthias Braun's avatar
Matthias Braun committed
821
		be_emit_cstring(env, ", ");
822
		ia32_emit_source_register(env, node, 0);
Matthias Braun's avatar
Matthias Braun committed
823
		be_emit_finish_line_gas(env, node);
824
825
	}
	finish_CondJmp(env, node, mode_Iu, get_ia32_pncode(node));
826
827
828
829
830
}

/**
 * Emits code for conditional test and jump with two variables.
 */
831
832
static
void emit_ia32_TestJmp(ia32_emit_env_t *env, const ir_node *node) {
833
	TestJmp_emitter(env, node);
834
835
}

836
837
static
void emit_ia32_CJmp(ia32_emit_env_t *env, const ir_node *node) {
Matthias Braun's avatar
Matthias Braun committed
838
839
	be_emit_cstring(env, "/* omitted redundant test */");
	be_emit_finish_line_gas(env, node);
840

841
	finish_CondJmp(env, node, mode_Is, get_ia32_pncode(node));
842
}
843

844
845
static
void emit_ia32_CJmpAM(ia32_emit_env_t *env, const ir_node *node) {
Matthias Braun's avatar
Matthias Braun committed
846
847
	be_emit_cstring(env, "/* omitted redundant test/cmp */");
	be_emit_finish_line_gas(env, node);
848

849
	finish_CondJmp(env, node, mode_Is, get_ia32_pncode(node));
850
}
Christian Würdig's avatar
Christian Würdig committed
851

Christian Würdig's avatar
Christian Würdig committed
852
853
854
/**
 * Emits code for conditional SSE floating point jump with two variables.
 */
855
856
static
void emit_ia32_xCondJmp(ia32_emit_env_t *env, const ir_node *node) {
Matthias Braun's avatar
Matthias Braun committed
857
	be_emit_cstring(env, "\tucomi");
Matthias Braun's avatar
Matthias Braun committed
858
	ia32_emit_xmm_mode_suffix(env, node);
Matthias Braun's avatar
Matthias Braun committed
859
	be_emit_char(env, ' ');
860
	ia32_emit_binop(env, node);
Matthias Braun's avatar
Matthias Braun committed
861
	be_emit_finish_line_gas(env, node);
Christian Würdig's avatar
Christian Würdig committed
862

863
	finish_CondJmp(env, node, mode_F, get_ia32_pncode(node));
Christian Würdig's avatar
Christian Würdig committed
864
865
}

Michael Beck's avatar
Michael Beck committed
866
867
868
/**
 * Emits code for conditional x87 floating point jump with two variables.
 */
869
870
static
void emit_ia32_x87CondJmp(ia32_emit_env_t *env, const ir_node *node) {
871
	ia32_attr_t *attr = get_ia32_attr(node);
Michael Beck's avatar
Michael Beck committed
872
	const char *reg = attr->x87[1]->name;
873
	long pnc = get_ia32_pncode(node);
Michael Beck's avatar
Michael Beck committed
874

875
	switch (get_ia32_irn_opcode(node)) {
Michael Beck's avatar
Michael Beck committed
876
	case iro_ia32_fcomrJmp:
877
		pnc = get_inversed_pnc(pnc);
878
		reg = attr->x87[0]->name;
Michael Beck's avatar
Michael Beck committed
879
880
	case iro_ia32_fcomJmp:
	default:
Matthias Braun's avatar
Matthias Braun committed
881
		be_emit_cstring(env, "\tfucom ");
Michael Beck's avatar
Michael Beck committed
882
883
		break;
	case iro_ia32_fcomrpJmp:
884
		pnc = get_inversed_pnc(pnc);
885
		reg = attr->x87[0]->name;
Michael Beck's avatar
Michael Beck committed
886
	case iro_ia32_fcompJmp:
Matthias Braun's avatar
Matthias Braun committed
887
		be_emit_cstring(env, "\tfucomp ");
Michael Beck's avatar
Michael Beck committed
888
889
		break;
	case iro_ia32_fcomrppJmp:
890
		pnc = get_inversed_pnc(pnc);
Michael Beck's avatar
Michael Beck committed
891
	case iro_ia32_fcomppJmp:
Matthias Braun's avatar
Matthias Braun committed
892
		be_emit_cstring(env, "\tfucompp ");
Michael Beck's avatar
Michael Beck committed
893
894
895
896
		reg = "";
		break;
	}

897
	if(reg[0] != '\0') {
Matthias Braun's avatar
Matthias Braun committed
898
899
		be_emit_char(env, '%');
		be_emit_string(env, reg);
900
	}
Matthias Braun's avatar
Matthias Braun committed
901
	be_emit_finish_line_gas(env, node);
Michael Beck's avatar
Michael Beck committed
902

Matthias Braun's avatar
Matthias Braun committed
903
904
905
906
	be_emit_cstring(env, "\tfnstsw %ax");
	be_emit_finish_line_gas(env, node);
	be_emit_cstring(env, "\tsahf");
	be_emit_finish_line_gas(env, node);
Michael Beck's avatar
Michael Beck committed
907

908
	finish_CondJmp(env, node, mode_E, pnc);
Michael Beck's avatar
Michael Beck committed
909
910
}

911
912
static
void CMov_emitter(ia32_emit_env_t *env, const ir_node *node) {
913
914
	long pnc = get_ia32_pncode(node);
	int is_PsiCondCMov = is_ia32_PsiCondCMov(node);
Matthias Braun's avatar
Matthias Braun committed
915
916
	int idx_left  = 2 - is_PsiCondCMov;
	int idx_right = 3 - is_PsiCondCMov;
Christian Würdig's avatar
Christian Würdig committed
917
918
	const arch_register_t *in1, *in2, *out;

919
920
921
	out = arch_get_irn_register(env->arch_env, node);
	in1 = arch_get_irn_register(env->arch_env, get_irn_n(node, idx_left));
	in2 = arch_get_irn_register(env->arch_env, get_irn_n(node, idx_right));
Christian Würdig's avatar
Christian Würdig committed
922

Christian Würdig's avatar
Christian Würdig committed
923
924
	/* we have to emit the cmp first, because the destination register */
	/* could be one of the compare registers                           */
925
	if (is_ia32_CmpCMov(node)) {
Matthias Braun's avatar
Matthias Braun committed
926
		be_emit_cstring(env, "\tcmp ");
927
		ia32_emit_source_register(env, node, 1);
Matthias Braun's avatar
Matthias Braun committed
928
		be_emit_cstring(env, ", ");
929
930
		ia32_emit_source_register(env, node, 0);
	} else if (is_ia32_xCmpCMov(node)) {
Matthias Braun's avatar
Matthias Braun committed
931
		be_emit_cstring(env, "\tucomis");
Matthias Braun's avatar
Matthias Braun committed
932
		ia32_emit_mode_suffix_mode(env, get_irn_mode(node));
Matthias Braun's avatar
Matthias Braun committed
933
		be_emit_char(env, ' ');
934