BugFix: return gcc-like names for st0-st7: %st, %st(1) - %st(7)
[libfirm] / ir / be / ia32 / ia32_emitter.c
1 /**
2  * This file implements the node emitter.
3  * @author Christian Wuerdig
4  * $Id$
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <limits.h>
12
13 #include "xmalloc.h"
14 #include "tv.h"
15 #include "iredges.h"
16 #include "debug.h"
17 #include "irgwalk.h"
18 #include "irprintf.h"
19 #include "irop_t.h"
20 #include "irargs_t.h"
21 #include "irprog_t.h"
22 #include "iredges_t.h"
23 #include "execfreq.h"
24
25 #include "../besched_t.h"
26 #include "../benode_t.h"
27 #include "../beabi.h"
28 #include "../be_dbgout.h"
29
30 #include "ia32_emitter.h"
31 #include "gen_ia32_emitter.h"
32 #include "gen_ia32_regalloc_if.h"
33 #include "ia32_nodes_attr.h"
34 #include "ia32_new_nodes.h"
35 #include "ia32_map_regs.h"
36 #include "bearch_ia32_t.h"
37
38 #define BLOCK_PREFIX(x) ".L" x
39
40 #define SNPRINTF_BUF_LEN 128
41
42 /* global arch_env for lc_printf functions */
43 static const arch_env_t *arch_env = NULL;
44
45 /** by default, we generate assembler code for the Linux gas */
46 asm_flavour_t asm_flavour = ASM_LINUX_GAS;
47
48 /**
49  * Switch to a new section
50  */
51 void ia32_switch_section(FILE *F, section_t sec) {
52         static section_t curr_sec = NO_SECTION;
53         static const char *text[ASM_MAX][SECTION_MAX] = {
54                 {
55                         ".section\t.text",
56                         ".section\t.data",
57                         ".section\t.rodata",
58                         ".section\t.text",
59                         ".section\t.tbss,\"awT\",@nobits",
60                         ".section\t.ctors,\"aw\",@progbits"
61                 },
62                 {
63                         ".section\t.text",
64                         ".section\t.data",
65                         ".section .rdata,\"dr\"",
66                         ".section\t.text",
67                         ".section\t.tbss,\"awT\",@nobits",
68                         ".section\t.ctors,\"aw\",@progbits"
69                 }
70         };
71
72         if (curr_sec == sec)
73                 return;
74
75         curr_sec = sec;
76         switch (sec) {
77
78         case NO_SECTION:
79                 break;
80
81         case SECTION_TEXT:
82         case SECTION_DATA:
83         case SECTION_RODATA:
84         case SECTION_COMMON:
85         case SECTION_TLS:
86         case SECTION_CTOR:
87                 fprintf(F, "\t%s\n", text[asm_flavour][sec]);
88                 break;
89
90         default:
91                 break;
92         }
93 }
94
95 static void ia32_dump_function_object(FILE *F, const char *name)
96 {
97         switch (asm_flavour) {
98         case ASM_LINUX_GAS:
99                 fprintf(F, "\t.type\t%s, @function\n", name);
100                 break;
101         case ASM_MINGW_GAS:
102                 fprintf(F, "\t.def\t%s;\t.scl\t2;\t.type\t32;\t.endef\n", name);
103                 break;
104         default:
105                 break;
106         }
107 }
108
109 static void ia32_dump_function_size(FILE *F, const char *name)
110 {
111         switch (asm_flavour) {
112         case ASM_LINUX_GAS:
113                 fprintf(F, "\t.size\t%s, .-%s\n", name, name);
114                 break;
115         default:
116                 break;
117         }
118 }
119
120 /*************************************************************
121  *             _       _    __   _          _
122  *            (_)     | |  / _| | |        | |
123  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
124  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
125  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
126  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
127  * | |                                       | |
128  * |_|                                       |_|
129  *************************************************************/
130
131 /* We always pass the ir_node which is a pointer. */
132 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
133         return lc_arg_type_ptr;
134 }
135
136
137 /**
138  * Returns the register at in position pos.
139  */
140 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
141         ir_node                *op;
142         const arch_register_t  *reg = NULL;
143
144         assert(get_irn_arity(irn) > pos && "Invalid IN position");
145
146         /* The out register of the operator at position pos is the
147            in register we need. */
148         op = get_irn_n(irn, pos);
149
150         reg = arch_get_irn_register(arch_env, op);
151
152         assert(reg && "no in register found");
153
154         /* in case of a joker register: just return a valid register */
155         if (arch_register_type_is(reg, joker)) {
156                 arch_register_req_t       req;
157                 const arch_register_req_t *p_req;
158
159                 /* ask for the requirements */
160                 p_req = arch_get_register_req(arch_env, &req, irn, pos);
161
162                 if (arch_register_req_is(p_req, limited)) {
163                         /* in case of limited requirements: get the first allowed register */
164
165                         bitset_t *bs = bitset_alloca(arch_register_class_n_regs(p_req->cls));
166                         int      idx;
167
168                         p_req->limited(p_req->limited_env, bs);
169                         idx = bitset_next_set(bs, 0);
170                         reg = arch_register_for_index(p_req->cls, idx);
171                 }
172                 else {
173                         /* otherwise get first register in class */
174                         reg = arch_register_for_index(p_req->cls, 0);
175                 }
176         }
177
178         return reg;
179 }
180
181 /**
182  * Returns the register at out position pos.
183  */
184 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
185         ir_node                *proj;
186         const arch_register_t  *reg = NULL;
187
188         /* 1st case: irn is not of mode_T, so it has only                 */
189         /*           one OUT register -> good                             */
190         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
191         /*           Proj with the corresponding projnum for the register */
192
193         if (get_irn_mode(irn) != mode_T) {
194                 reg = arch_get_irn_register(arch_env, irn);
195         }
196         else if (is_ia32_irn(irn)) {
197                 reg = get_ia32_out_reg(irn, pos);
198         }
199         else {
200                 const ir_edge_t *edge;
201
202                 foreach_out_edge(irn, edge) {
203                         proj = get_edge_src_irn(edge);
204                         assert(is_Proj(proj) && "non-Proj from mode_T node");
205                         if (get_Proj_proj(proj) == pos) {
206                                 reg = arch_get_irn_register(arch_env, proj);
207                                 break;
208                         }
209                 }
210         }
211
212         assert(reg && "no out register found");
213         return reg;
214 }
215
216 enum io_direction {
217   IN_REG,
218   OUT_REG
219 };
220
221 /**
222  * Returns the name of the in register at position pos.
223  */
224 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
225         const arch_register_t *reg;
226
227         if (in_out == IN_REG) {
228                 reg = get_in_reg(irn, pos);
229
230                 if (reg->reg_class == &ia32_reg_classes[CLASS_ia32_vfp]) {
231                         /* FIXME: works for binop only */
232                         assert(2 <= pos && pos <= 3);
233                         reg = get_ia32_attr(irn)->x87[pos - 2];
234                 }
235         }
236         else {
237                 /* destination address mode nodes don't have outputs */
238                 if (is_ia32_irn(irn) && get_ia32_op_type(irn) == ia32_AddrModeD) {
239                         return "MEM";
240                 }
241
242                 reg = get_out_reg(irn, pos);
243                 if (reg->reg_class == &ia32_reg_classes[CLASS_ia32_vfp])
244                         reg = get_ia32_attr(irn)->x87[pos + 2];
245         }
246         return arch_register_get_name(reg);
247 }
248
249 /**
250  * Get the register name for a node.
251  */
252 static int ia32_get_reg_name(lc_appendable_t *app,
253     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
254 {
255         const char *buf;
256         ir_node    *irn = arg->v_ptr;
257         int         nr = occ->width - 1;
258
259         if (! irn)
260                 return lc_appendable_snadd(app, "(null)", 6);
261
262         buf = get_ia32_reg_name(irn, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
263
264         /* append the stupid % to register names */
265         lc_appendable_chadd(app, '%');
266         return lc_appendable_snadd(app, buf, strlen(buf));
267 }
268
269 /**
270  * Get the x87 register name for a node.
271  */
272 static int ia32_get_x87_name(lc_appendable_t *app,
273     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
274 {
275         const char  *buf;
276         ir_node     *irn = arg->v_ptr;
277         int         nr = occ->width - 1;
278         ia32_attr_t *attr;
279         char        *n;
280         int         res = 0;
281
282         if (! irn)
283                 return lc_appendable_snadd(app, "(null)", 6);
284
285         attr = get_ia32_attr(irn);
286         buf  = attr->x87[nr]->name;
287
288         res += lc_appendable_chadd(app, '%');
289
290         if (buf[2] == '0') {
291                 /* just omit 'st' (skip the 0) */
292                 res += lc_appendable_snadd(app, buf, strlen(buf) - 1);
293         }
294         else {
295                 res += lc_appendable_snadd(app, "st(", 3);
296                 res += lc_appendable_chadd(app, buf[2]);
297                 res += lc_appendable_chadd(app, ')');
298         }
299
300         return res;
301 }
302
303 /**
304  * Returns the tarval, offset or scale of an ia32 as a string.
305  */
306 static int ia32_const_to_str(lc_appendable_t *app,
307     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
308 {
309         const char *buf;
310         ir_node    *irn = arg->v_ptr;
311
312         if (! irn)
313                 return lc_arg_append(app, occ, "(null)", 6);
314
315         if (occ->conversion == 'C') {
316                 buf = get_ia32_cnst(irn);
317         }
318         else { /* 'O' */
319                 buf = get_ia32_am_offs(irn);
320         }
321
322         return buf ? lc_appendable_snadd(app, buf, strlen(buf)) : 0;
323 }
324
325 /**
326  * Determines the SSE suffix depending on the mode.
327  */
328 static int ia32_get_mode_suffix(lc_appendable_t *app,
329     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
330 {
331         ir_node *irn  = arg->v_ptr;
332         ir_mode *mode = get_irn_mode(irn);
333
334         if (mode == mode_T) {
335                 mode = get_ia32_res_mode(irn);
336                 if (! mode)
337                         mode = get_ia32_ls_mode(irn);
338         }
339
340         if (! irn)
341                 return lc_arg_append(app, occ, "(null)", 6);
342
343         if (mode_is_float(mode)) {
344                 return lc_appendable_chadd(app, get_mode_size_bits(mode) == 32 ? 's' : 'd');
345         }
346         else {
347                 return lc_appendable_chadd(app, mode_is_signed(mode) ? 's' : 'z');
348         }
349 }
350
351 /**
352  * Return the ia32 printf arg environment.
353  * We use the firm environment with some additional handlers.
354  */
355 const lc_arg_env_t *ia32_get_arg_env(void) {
356         static lc_arg_env_t *env = NULL;
357
358         static const lc_arg_handler_t ia32_reg_handler   = { ia32_get_arg_type, ia32_get_reg_name };
359         static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
360         static const lc_arg_handler_t ia32_mode_handler  = { ia32_get_arg_type, ia32_get_mode_suffix };
361         static const lc_arg_handler_t ia32_x87_handler   = { ia32_get_arg_type, ia32_get_x87_name };
362
363         if(env == NULL) {
364                 /* extend the firm printer */
365                 env = firm_get_arg_env();
366
367                 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
368                 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
369                 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
370                 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
371                 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
372                 lc_arg_register(env, "ia32:x87",  'X', &ia32_x87_handler);
373         }
374
375         return env;
376 }
377
378 static const char *ia32_get_reg_name_for_mode(ia32_emit_env_t *env, ir_mode *mode, const arch_register_t *reg) {
379         switch(get_mode_size_bits(mode)) {
380                 case 8:
381                         return ia32_get_mapped_reg_name(env->isa->regs_8bit, reg);
382                 case 16:
383                         return ia32_get_mapped_reg_name(env->isa->regs_16bit, reg);
384                 default:
385                         return (char *)arch_register_get_name(reg);
386         }
387 }
388
389 /**
390  * Emits registers and/or address mode of a binary operation.
391  */
392 const char *ia32_emit_binop(const ir_node *n, ia32_emit_env_t *env) {
393         static char *buf = NULL;
394
395         /* verify that this function is never called on non-AM supporting operations */
396         //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
397
398 #define PRODUCES_RESULT(n)   \
399         (!(is_ia32_St(n)      || \
400         is_ia32_Store8Bit(n)  || \
401         is_ia32_CondJmp(n)    || \
402         is_ia32_xCondJmp(n)   || \
403         is_ia32_CmpSet(n)     || \
404         is_ia32_xCmpSet(n)    || \
405         is_ia32_SwitchJmp(n)))
406
407         if (! buf) {
408                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
409         }
410         else {
411                 memset(buf, 0, SNPRINTF_BUF_LEN);
412         }
413
414         switch(get_ia32_op_type(n)) {
415                 case ia32_Normal:
416                         if (is_ia32_ImmConst(n)) {
417                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
418                         }
419                         else if (is_ia32_ImmSymConst(n)) {
420                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, OFFSET FLAT:%s", n, get_ia32_cnst(n));
421                         }
422                         else {
423                                 const arch_register_t *in1 = get_in_reg(n, 2);
424                                 const arch_register_t *in2 = get_in_reg(n, 3);
425                                 const arch_register_t *out = PRODUCES_RESULT(n) ? get_out_reg(n, 0) : NULL;
426                                 const arch_register_t *in;
427                                 const char            *in_name;
428
429                                 in      = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
430                                 out     = out ? out : in1;
431                                 in_name = arch_register_get_name(in);
432
433                                 if (is_ia32_emit_cl(n)) {
434                                         assert(REGS_ARE_EQUAL(&ia32_gp_regs[REG_ECX], in) && "shift operation needs ecx");
435                                         in_name = "cl";
436                                 }
437
438                                 snprintf(buf, SNPRINTF_BUF_LEN, "%%%s, %%%s", arch_register_get_name(out), in_name);
439                         }
440                         break;
441                 case ia32_AddrModeS:
442                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
443                                 assert(! PRODUCES_RESULT(n) && "Source AM with Const must not produce result");
444                                 snprintf(buf, SNPRINTF_BUF_LEN, "%s, %s", get_ia32_cnst(n), ia32_emit_am(n, env));
445                         }
446                         else {
447                                 if (PRODUCES_RESULT(n)) {
448                                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, ia32_emit_am(n, env));
449                                 }
450                                 else {
451                                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, ia32_emit_am(n, env));
452                                 }
453                         }
454                         break;
455                 case ia32_AddrModeD:
456                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
457                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s,%s%s",
458                                         ia32_emit_am(n, env),
459                                         is_ia32_ImmSymConst(n) ? " OFFSET FLAT:" : " ",  /* In case of a symconst we must add OFFSET to */
460                                         get_ia32_cnst(n));                               /* tell the assembler to store it's address.   */
461                         }
462                         else {
463                                 const arch_register_t *in1 = get_in_reg(n, get_irn_arity(n) == 5 ? 3 : 2);
464                                 ir_mode               *mode = get_ia32_res_mode(n);
465                                 const char            *in_name;
466
467                                 mode    = mode ? mode : get_ia32_ls_mode(n);
468                                 in_name = ia32_get_reg_name_for_mode(env, mode, in1);
469
470                                 if (is_ia32_emit_cl(n)) {
471                                         assert(REGS_ARE_EQUAL(&ia32_gp_regs[REG_ECX], in1) && "shift operation needs ecx");
472                                         in_name = "cl";
473                                 }
474
475                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %%%s", ia32_emit_am(n, env), in_name);
476                         }
477                         break;
478                 default:
479                         assert(0 && "unsupported op type");
480         }
481
482 #undef PRODUCES_RESULT
483
484         return buf;
485 }
486
487 /**
488  * Returns the xxx PTR string for a given mode
489  *
490  * @param mode      the mode
491  * @param x87_insn  if non-zero returns the string for a x87 instruction
492  *                  else for a SSE instruction
493  */
494 static const char *pointer_size(ir_mode *mode, int x87_insn)
495 {
496         if (mode) {
497                 switch (get_mode_size_bits(mode)) {
498                 case 8:  return "BYTE PTR";
499                 case 16: return "WORD PTR";
500                 case 32: return "DWORD PTR";
501                 case 64:
502                         if (x87_insn)
503                                 return "QWORD PTR";
504                         return NULL;
505                 case 80:
506                 case 96: return "XWORD PTR";
507                 default: return NULL;
508                 }
509         }
510         return NULL;
511 }
512
513 /**
514  * Translate the stx names into %st(x).
515  */
516 static char *get_x87_reg_name(const arch_register_t *reg, char *buf) {
517         const char *name = arch_register_get_name(reg);
518
519         buf[0] = '%';
520         buf[1] = 's';
521         buf[2] = 't';
522
523         if (name[2] == '0')
524                 buf[3] = '\0';
525         else {
526                 buf[3] = '(';
527                 buf[4] = name[2];
528                 buf[5] = ')';
529                 buf[6] = '\0';
530         }
531
532         return buf;
533 }
534
535 /**
536  * Emits registers and/or address mode of a binary operation.
537  */
538 const char *ia32_emit_x87_binop(const ir_node *n, ia32_emit_env_t *env) {
539         static char *buf = NULL;
540
541         /* verify that this function is never called on non-AM supporting operations */
542         //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
543
544         if (! buf) {
545                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
546         }
547         else {
548                 memset(buf, 0, SNPRINTF_BUF_LEN);
549         }
550
551         switch(get_ia32_op_type(n)) {
552                 case ia32_Normal:
553                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
554                                 ir_mode *mode = get_ia32_ls_mode(n);
555                                 const char *p = pointer_size(mode, 1);
556                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s %s", p, get_ia32_cnst(n));
557                         }
558                         else {
559                                 ia32_attr_t *attr = get_ia32_attr(n);
560                                 const arch_register_t *in1 = attr->x87[0];
561                                 const arch_register_t *in2 = attr->x87[1];
562                                 const arch_register_t *out = attr->x87[2];
563                                 const arch_register_t *in;
564                                 char                  buf1[7], buf2[7];
565
566                                 in  = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
567                                 out = out ? out : in1;
568
569                                 snprintf(buf, SNPRINTF_BUF_LEN, "%s, %s", get_x87_reg_name(out, buf1), get_x87_reg_name(in, buf2));
570                         }
571                         break;
572                 case ia32_AddrModeS:
573                 case ia32_AddrModeD:
574                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s", ia32_emit_am(n, env));
575                         break;
576                 default:
577                         assert(0 && "unsupported op type");
578         }
579
580         return buf;
581 }
582
583 /**
584  * Emits registers and/or address mode of a unary operation.
585  */
586 const char *ia32_emit_unop(const ir_node *n, ia32_emit_env_t *env) {
587         static char *buf = NULL;
588
589         if (! buf) {
590                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
591         }
592         else {
593                 memset(buf, 0, SNPRINTF_BUF_LEN);
594         }
595
596         switch(get_ia32_op_type(n)) {
597                 case ia32_Normal:
598                         if (is_ia32_ImmConst(n)) {
599                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%C", n);
600                         }
601                         else if (is_ia32_ImmSymConst(n)) {
602                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "OFFSET FLAT:%C", n);
603                         }
604                         else {
605                                 if (is_ia32_MulS(n) || is_ia32_Mulh(n)) {
606                                         /* MulS and Mulh implicitly multiply by EAX */
607                                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%4S", n);
608                                 } else if(is_ia32_Push(n)) {
609                                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S", n);
610                                 } else {
611                                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
612                                 }
613                         }
614                         break;
615                 case ia32_AddrModeD:
616                         assert(!is_ia32_Push(n));
617                         snprintf(buf, SNPRINTF_BUF_LEN, "%s", ia32_emit_am(n, env));
618                         break;
619                 case ia32_AddrModeS:
620                         /*
621                                 Mulh is emitted via emit_unop
622                                 imul [MEM]  means EDX:EAX <- EAX * [MEM]
623                         */
624                         assert((is_ia32_Mulh(n) || is_ia32_MulS(n) || is_ia32_Push(n)) && "Only MulS and Mulh can have AM source as unop");
625                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s", ia32_emit_am(n, env));
626                         break;
627                 default:
628                         assert(0 && "unsupported op type");
629         }
630
631         return buf;
632 }
633
634 /**
635  * Emits address mode.
636  */
637 const char *ia32_emit_am(const ir_node *n, ia32_emit_env_t *env) {
638         ia32_am_flavour_t am_flav    = get_ia32_am_flavour(n);
639         int               had_output = 0;
640         char              *s;
641         const char        *p;
642         static struct obstack *obst  = NULL;
643         ir_mode *mode = get_ia32_ls_mode(n);
644
645         if (! is_ia32_Lea(n))
646                 assert(mode && "AM node must have ls_mode attribute set.");
647
648         if (! obst) {
649                 obst = xcalloc(1, sizeof(*obst));
650         }
651         else {
652                 obstack_free(obst, NULL);
653         }
654
655         /* obstack_free with NULL results in an uninitialized obstack */
656         obstack_init(obst);
657
658         p = pointer_size(mode, ia32_has_x87_register(n) || is_ia32_GetST0(n) || is_ia32_SetST0(n));
659         if (p)
660                 obstack_printf(obst, "%s ", p);
661
662         /* emit address mode symconst */
663         if (get_ia32_am_sc(n)) {
664                 if (is_ia32_am_sc_sign(n))
665                         obstack_printf(obst, "-");
666                 obstack_printf(obst, "%s", get_id_str(get_ia32_am_sc(n)));
667         }
668
669         if (am_flav & ia32_B) {
670                 obstack_printf(obst, "[");
671                 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
672                 had_output = 1;
673         }
674
675         if (am_flav & ia32_I) {
676                 if (had_output) {
677                         obstack_printf(obst, "+");
678                 }
679                 else {
680                         obstack_printf(obst, "[");
681                 }
682
683                 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
684
685                 if (am_flav & ia32_S) {
686                         obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
687                 }
688
689                 had_output = 1;
690         }
691
692         if (am_flav & ia32_O) {
693                 int offs = get_ia32_am_offs_int(n);
694
695                 if (offs != 0) {
696                         /* omit explicit + if there was no base or index */
697                         if (! had_output) {
698                                 obstack_printf(obst, "[%d", offs);
699                         } else {
700                                 obstack_printf(obst, "%+d", offs);
701                         }
702
703                         had_output = 1;
704                 }
705         }
706
707         if (had_output)
708                 obstack_printf(obst, "] ");
709
710         obstack_1grow(obst, '\0');
711         s = obstack_finish(obst);
712
713         return s;
714 }
715
716 /**
717  * emit an address
718  */
719 const char *ia32_emit_adr(const ir_node *irn, ia32_emit_env_t *env)
720 {
721         static char buf[SNPRINTF_BUF_LEN];
722         ir_mode    *mode = get_ia32_ls_mode(irn);
723         const char *adr  = get_ia32_cnst(irn);
724         const char *pref = pointer_size(mode, ia32_has_x87_register(irn));
725
726         snprintf(buf, SNPRINTF_BUF_LEN, "%s %s", pref ? pref : "", adr);
727         return buf;
728 }
729
730 /**
731  * Formated print of commands and comments.
732  */
733 static void ia32_fprintf_format(FILE *F, const ir_node *irn, char *cmd_buf, char *cmnt_buf) {
734         unsigned lineno;
735         const char *name = irn ? be_retrieve_dbg_info(get_irn_dbg_info((ir_node *)irn), &lineno) : NULL;
736
737         if (name)
738                 fprintf(F, "\t%-35s %-60s /* %s:%u */\n", cmd_buf, cmnt_buf, name, lineno);
739         else
740                 fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
741 }
742
743
744
745 /**
746  * Add a number to a prefix. This number will not be used a second time.
747  */
748 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
749         static unsigned long id = 0;
750         snprintf(buf, buflen, "%s%lu", prefix, ++id);
751         return buf;
752 }
753
754
755
756 /*************************************************
757  *                 _ _                         _
758  *                (_) |                       | |
759  *   ___ _ __ ___  _| |_    ___ ___  _ __   __| |
760  *  / _ \ '_ ` _ \| | __|  / __/ _ \| '_ \ / _` |
761  * |  __/ | | | | | | |_  | (_| (_) | | | | (_| |
762  *  \___|_| |_| |_|_|\__|  \___\___/|_| |_|\__,_|
763  *
764  *************************************************/
765
766 #undef IA32_DO_EMIT
767 #define IA32_DO_EMIT(irn) ia32_fprintf_format(F, irn, cmd_buf, cmnt_buf)
768
769 /*
770  * coding of conditions
771  */
772 struct cmp2conditon_t {
773         const char *name;
774         pn_Cmp      num;
775 };
776
777 /*
778  * positive conditions for signed compares
779  */
780 static const struct cmp2conditon_t cmp2condition_s[] = {
781   { NULL,              pn_Cmp_False },  /* always false */
782   { "e",               pn_Cmp_Eq },     /* == */
783   { "l",               pn_Cmp_Lt },     /* < */
784   { "le",              pn_Cmp_Le },     /* <= */
785   { "g",               pn_Cmp_Gt },     /* > */
786   { "ge",              pn_Cmp_Ge },     /* >= */
787   { "ne",              pn_Cmp_Lg },     /* != */
788   { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
789   { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
790   { "e",               pn_Cmp_Ue },     /* Floating point: unordered or == */
791   { "b",               pn_Cmp_Ul },     /* Floating point: unordered or < */
792   { "be",              pn_Cmp_Ule },    /* Floating point: unordered or <= */
793   { "a",               pn_Cmp_Ug },     /* Floating point: unordered or > */
794   { "ae",              pn_Cmp_Uge },    /* Floating point: unordered or >= */
795   { "ne",              pn_Cmp_Ne },     /* Floating point: unordered or != */
796   { NULL,              pn_Cmp_True },   /* always true */
797 };
798
799 /*
800  * positive conditions for unsigned compares
801  */
802 static const struct cmp2conditon_t cmp2condition_u[] = {
803         { NULL,              pn_Cmp_False },  /* always false */
804         { "e",               pn_Cmp_Eq },     /* == */
805         { "b",               pn_Cmp_Lt },     /* < */
806         { "be",              pn_Cmp_Le },     /* <= */
807         { "a",               pn_Cmp_Gt },     /* > */
808         { "ae",              pn_Cmp_Ge },     /* >= */
809         { "ne",              pn_Cmp_Lg },     /* != */
810         { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
811         { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
812         { "e",               pn_Cmp_Ue },     /* Floating point: unordered or == */
813         { "b",               pn_Cmp_Ul },     /* Floating point: unordered or < */
814         { "be",              pn_Cmp_Ule },    /* Floating point: unordered or <= */
815         { "a",               pn_Cmp_Ug },     /* Floating point: unordered or > */
816         { "ae",              pn_Cmp_Uge },    /* Floating point: unordered or >= */
817         { "ne",              pn_Cmp_Ne },     /* Floating point: unordered or != */
818         { NULL,              pn_Cmp_True },   /* always true */
819 };
820
821 /*
822  * returns the condition code
823  */
824 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
825 {
826         assert(cmp2condition_s[cmp_code].num == cmp_code);
827         assert(cmp2condition_u[cmp_code].num == cmp_code);
828
829         return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
830 }
831
832 /**
833  * Returns the target block for a control flow node.
834  */
835 static ir_node *get_cfop_target_block(const ir_node *irn) {
836         return get_irn_link(irn);
837 }
838
839 /**
840  * Returns the target label for a control flow node.
841  */
842 static char *get_cfop_target(const ir_node *irn, char *buf) {
843         ir_node *bl = get_cfop_target_block(irn);
844
845         snprintf(buf, SNPRINTF_BUF_LEN, BLOCK_PREFIX("%ld"), get_irn_node_nr(bl));
846         return buf;
847 }
848
849 /** Return the next block in Block schedule */
850 static ir_node *next_blk_sched(const ir_node *block) {
851         return get_irn_link(block);
852 }
853
854 /**
855  * Returns the Proj with projection number proj and NOT mode_M
856  */
857 static ir_node *get_proj(const ir_node *irn, long proj) {
858         const ir_edge_t *edge;
859         ir_node         *src;
860
861         assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
862
863         foreach_out_edge(irn, edge) {
864                 src = get_edge_src_irn(edge);
865
866                 assert(is_Proj(src) && "Proj expected");
867                 if (get_irn_mode(src) == mode_M)
868                         continue;
869
870                 if (get_Proj_proj(src) == proj)
871                         return src;
872         }
873         return NULL;
874 }
875
876 /**
877  * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
878  */
879 static void finish_CondJmp(FILE *F, const ir_node *irn, ir_mode *mode) {
880         const ir_node *proj_true;
881         const ir_node *proj_false;
882         const ir_node *block;
883         const ir_node *next_block;
884         char buf[SNPRINTF_BUF_LEN];
885         char cmd_buf[SNPRINTF_BUF_LEN];
886         char cmnt_buf[SNPRINTF_BUF_LEN];
887         int is_unsigned;
888         int pnc;
889         int flipped = 0;
890
891         /* get both Proj's */
892         proj_true = get_proj(irn, pn_Cond_true);
893         assert(proj_true && "CondJmp without true Proj");
894
895         proj_false = get_proj(irn, pn_Cond_false);
896         assert(proj_false && "CondJmp without false Proj");
897
898         pnc = get_ia32_pncode(irn);
899
900         /* for now, the code works for scheduled and non-schedules blocks */
901         block = get_nodes_block(irn);
902
903         /* we have a block schedule */
904         next_block = next_blk_sched(block);
905
906         if (get_cfop_target_block(proj_true) == next_block) {
907                 /* exchange both proj's so the second one can be omitted */
908                 const ir_node *t = proj_true;
909
910                 proj_true  = proj_false;
911                 proj_false = t;
912                 flipped    = 1;
913                 pnc        = get_negated_pnc(pnc, mode);
914         }
915
916         /* the first Proj must always be created */
917         is_unsigned = mode_is_float(mode) || ! mode_is_signed(mode);
918
919         /* in case of unordered compare, check for parity */
920         if (pnc & pn_Cmp_Uo) {
921                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jp %s", get_cfop_target(proj_true, buf));
922                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* jump to false if result is unordered */");
923                 IA32_DO_EMIT(irn);
924         }
925
926         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
927                  get_cmp_suffix(pnc, is_unsigned),
928                  get_cfop_target(proj_true, buf));
929         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* %s(a, b) %s*/",
930                  get_pnc_string(pnc), flipped ? "(was flipped)" : "");
931         IA32_DO_EMIT(irn);
932
933         /* the second Proj might be a fallthrough */
934         if (get_cfop_target_block(proj_false) != next_block) {
935                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj_false, buf));
936                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* otherwise */");
937         }
938         else {
939                 cmd_buf[0] = '\0';
940                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(proj_false, buf));
941         }
942         IA32_DO_EMIT(irn);
943 }
944
945 /**
946  * Emits code for conditional jump.
947  */
948 static void CondJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
949         FILE *F = env->out;
950         char cmd_buf[SNPRINTF_BUF_LEN];
951         char cmnt_buf[SNPRINTF_BUF_LEN];
952
953         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
954         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
955         IA32_DO_EMIT(irn);
956         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
957 }
958
959 /**
960  * Emits code for conditional jump with two variables.
961  */
962 static void emit_ia32_CondJmp(const ir_node *irn, ia32_emit_env_t *env) {
963         CondJmp_emitter(irn, env);
964 }
965
966 /**
967  * Emits code for conditional test and jump.
968  */
969 static void TestJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
970
971 #define IA32_IS_IMMOP (is_ia32_ImmConst(irn) || is_ia32_ImmSymConst(irn))
972
973         FILE       *F   = env->out;
974         const char *op1 = arch_register_get_name(get_in_reg(irn, 0));
975         const char *op2 = IA32_IS_IMMOP ? get_ia32_cnst(irn) : NULL;
976         char        cmd_buf[SNPRINTF_BUF_LEN];
977         char        cmnt_buf[SNPRINTF_BUF_LEN];
978
979         if (! op2)
980                 op2 = arch_register_get_name(get_in_reg(irn, 1));
981
982         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "test %%%s,%s%s ", op1, IA32_IS_IMMOP ? " " : " %", op2);
983         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
984
985         IA32_DO_EMIT(irn);
986         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
987
988 #undef IA32_IS_IMMOP
989 }
990
991 /**
992  * Emits code for conditional test and jump with two variables.
993  */
994 static void emit_ia32_TestJmp(const ir_node *irn, ia32_emit_env_t *env) {
995         TestJmp_emitter(irn, env);
996 }
997
998 static void emit_ia32_CJmp(const ir_node *irn, ia32_emit_env_t *env) {
999         FILE *F = env->out;
1000         char cmd_buf[SNPRINTF_BUF_LEN];
1001         char cmnt_buf[SNPRINTF_BUF_LEN];
1002
1003         snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1004         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test */", irn);
1005         IA32_DO_EMIT(irn);
1006         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
1007 }
1008
1009 static void emit_ia32_CJmpAM(const ir_node *irn, ia32_emit_env_t *env) {
1010         FILE *F = env->out;
1011         char cmd_buf[SNPRINTF_BUF_LEN];
1012         char cmnt_buf[SNPRINTF_BUF_LEN];
1013
1014         snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1015         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test/cmp */", irn);
1016         IA32_DO_EMIT(irn);
1017         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
1018 }
1019
1020 /**
1021  * Emits code for conditional SSE floating point jump with two variables.
1022  */
1023 static void emit_ia32_xCondJmp(ir_node *irn, ia32_emit_env_t *env) {
1024         FILE *F = env->out;
1025         char cmd_buf[SNPRINTF_BUF_LEN];
1026         char cmnt_buf[SNPRINTF_BUF_LEN];
1027         const lc_arg_env_t *arg_env = ia32_get_arg_env();
1028
1029         lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ucomis%M %s", irn, ia32_emit_binop(irn, env));
1030         lc_esnprintf(arg_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1031         IA32_DO_EMIT(irn);
1032
1033         finish_CondJmp(F, irn, mode_F);
1034 }
1035
1036 /**
1037  * Emits code for conditional x87 floating point jump with two variables.
1038  */
1039 static void emit_ia32_x87CondJmp(ir_node *irn, ia32_emit_env_t *env) {
1040         FILE *F = env->out;
1041         char cmd_buf[SNPRINTF_BUF_LEN];
1042         char cmnt_buf[SNPRINTF_BUF_LEN];
1043         ia32_attr_t *attr = get_ia32_attr(irn);
1044         const char *reg = attr->x87[1]->name;
1045         const char *instr = "fcom";
1046         int reverse = 0;
1047
1048         switch (get_ia32_irn_opcode(irn)) {
1049         case iro_ia32_fcomrJmp:
1050                 reverse = 1;
1051         case iro_ia32_fcomJmp:
1052         default:
1053                 instr = "fucom";
1054                 break;
1055         case iro_ia32_fcomrpJmp:
1056                 reverse = 1;
1057         case iro_ia32_fcompJmp:
1058                 instr = "fucomp";
1059                 break;
1060         case iro_ia32_fcomrppJmp:
1061                 reverse = 1;
1062         case iro_ia32_fcomppJmp:
1063                 instr = "fucompp";
1064                 reg = "";
1065                 break;
1066         }
1067
1068         if (reverse)
1069                 set_ia32_pncode(irn, (long)get_inversed_pnc(get_ia32_pncode(irn)));
1070
1071         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "%s %s%s", instr, reg[0] == '\0' ? "" : "%", reg);
1072         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1073         IA32_DO_EMIT(irn);
1074         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fnstsw %%ax", irn);
1075         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* Store x87 FPU Control Word */");
1076         IA32_DO_EMIT(irn);
1077         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "sahf");
1078         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* Store ah into flags */");
1079         IA32_DO_EMIT(irn);
1080
1081         /* the compare flags must be evaluated using carry , ie unsigned */
1082         finish_CondJmp(F, irn, mode_Iu);
1083 }
1084
1085 static void CMov_emitter(ir_node *irn, ia32_emit_env_t *env) {
1086         FILE               *F       = env->out;
1087         const lc_arg_env_t *arg_env = ia32_get_arg_env();
1088         ir_mode    *mode       = get_irn_mode(get_irn_n(irn, 0));
1089         int        is_unsigned = mode_is_float(mode) || ! mode_is_signed(mode);
1090         const char *cmp_suffix = get_cmp_suffix(get_ia32_pncode(irn), is_unsigned);
1091         int is_PsiCondCMov     = is_ia32_PsiCondCMov(irn);
1092         int idx_left  = 2 - is_PsiCondCMov;
1093         int idx_right = 3 - is_PsiCondCMov;
1094
1095         char cmd_buf[SNPRINTF_BUF_LEN];
1096         char cmnt_buf[SNPRINTF_BUF_LEN];
1097         const arch_register_t *in1, *in2, *out;
1098
1099         out = arch_get_irn_register(env->arch_env, irn);
1100         in1 = arch_get_irn_register(env->arch_env, get_irn_n(irn, idx_left));
1101         in2 = arch_get_irn_register(env->arch_env, get_irn_n(irn, idx_right));
1102
1103         /* we have to emit the cmp first, because the destination register */
1104         /* could be one of the compare registers                           */
1105         if (is_ia32_CmpCMov(irn)) {
1106                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, %2S", irn, irn);
1107         }
1108         else if (is_ia32_xCmpCMov(irn)) {
1109                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ucomis%M %1S, %2S", get_irn_n(irn, 0), irn, irn);
1110         }
1111         else if (is_PsiCondCMov) {
1112                 /* omit compare because flags are already set by And/Or */
1113                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "test %1S, %1S", irn, irn);
1114         }
1115         else {
1116                 assert(0 && "unsupported CMov");
1117         }
1118         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* Psi condition */" );
1119         IA32_DO_EMIT(irn);
1120
1121         if (REGS_ARE_EQUAL(out, in2)) {
1122                 /* best case: default in == out -> do nothing */
1123         }
1124         else if (REGS_ARE_EQUAL(out, in1)) {
1125                 /* true in == out -> need complement compare and exchange true and default in */
1126                 ir_node *t = get_irn_n(irn, idx_left);
1127                 set_irn_n(irn, idx_left, get_irn_n(irn, idx_right));
1128                 set_irn_n(irn, idx_right, t);
1129
1130                 cmp_suffix  = get_cmp_suffix(get_negated_pnc(get_ia32_pncode(irn), get_irn_mode(irn)), is_unsigned);
1131
1132         }
1133         else {
1134                 /* out is different from in: need copy default -> out */
1135                 if (is_PsiCondCMov)
1136                         lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1137                 else
1138                         lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %4S", irn, irn);
1139
1140                 lc_esnprintf(arg_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* copy default -> out */" );
1141                 IA32_DO_EMIT(irn);
1142         }
1143
1144         if (is_PsiCondCMov)
1145                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "cmov%s %1D, %2S", cmp_suffix, irn, irn);
1146         else
1147                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "cmov%s %1D, %3S", cmp_suffix, irn, irn);
1148
1149         lc_esnprintf(arg_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* condition is true case */" );
1150         IA32_DO_EMIT(irn);
1151 }
1152
1153 static void emit_ia32_CmpCMov(ir_node *irn, ia32_emit_env_t *env) {
1154         CMov_emitter(irn, env);
1155 }
1156
1157 static void emit_ia32_PsiCondCMov(ir_node *irn, ia32_emit_env_t *env) {
1158         CMov_emitter(irn, env);
1159 }
1160
1161 static void emit_ia32_xCmpCMov(ir_node *irn, ia32_emit_env_t *env) {
1162         CMov_emitter(irn, env);
1163 }
1164
1165 static void Set_emitter(ir_node *irn, ir_mode *mode, ia32_emit_env_t *env) {
1166         FILE               *F       = env->out;
1167         const lc_arg_env_t *arg_env = ia32_get_arg_env();
1168         int        is_unsigned = mode_is_float(mode) || ! mode_is_signed(mode);
1169         const char *cmp_suffix = get_cmp_suffix(get_ia32_pncode(irn), is_unsigned);
1170         const char *reg8bit;
1171
1172         char cmd_buf[SNPRINTF_BUF_LEN];
1173         char cmnt_buf[SNPRINTF_BUF_LEN];
1174         const arch_register_t *out;
1175
1176         out     = arch_get_irn_register(env->arch_env, irn);
1177         reg8bit = ia32_get_mapped_reg_name(env->isa->regs_8bit, out);
1178
1179         if (is_ia32_CmpSet(irn)) {
1180                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
1181         }
1182         else if (is_ia32_xCmpSet(irn)) {
1183                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ucomis%M %s", get_irn_n(irn, 2), ia32_emit_binop(irn, env));
1184         }
1185         else if (is_ia32_PsiCondSet(irn)) {
1186                 /* omit compare because flags are already set by And/Or */
1187                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1188         }
1189         else {
1190                 assert(0 && "unsupported Set");
1191         }
1192         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* calculate Psi condition */" );
1193         IA32_DO_EMIT(irn);
1194
1195         /* use mov to clear target because it doesn't affect the eflags */
1196         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "mov %%%s, 0", arch_register_get_name(out));
1197         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* clear target as set modifies only lower 8 bit */");
1198         IA32_DO_EMIT(irn);
1199
1200         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "set%s %%%s", cmp_suffix, reg8bit);
1201         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* set 1 iff true, 0 otherweise */" );
1202         IA32_DO_EMIT(irn);
1203 }
1204
1205 static void emit_ia32_CmpSet(ir_node *irn, ia32_emit_env_t *env) {
1206         Set_emitter(irn, get_irn_mode(get_irn_n(irn, 2)), env);
1207 }
1208
1209 static void emit_ia32_PsiCondSet(ir_node *irn, ia32_emit_env_t *env) {
1210         Set_emitter(irn, get_irn_mode(get_irn_n(irn, 0)), env);
1211 }
1212
1213 static void emit_ia32_xCmpSet(ir_node *irn, ia32_emit_env_t *env) {
1214         Set_emitter(irn, get_irn_mode(get_irn_n(irn, 2)), env);
1215 }
1216
1217 static void emit_ia32_xCmp(ir_node *irn, ia32_emit_env_t *env) {
1218         FILE               *F       = env->out;
1219         const lc_arg_env_t *arg_env = ia32_get_arg_env();
1220         int                sse_pnc  = -1;
1221         long               pnc      = get_ia32_pncode(irn);
1222         long               unord    = pnc & pn_Cmp_Uo;
1223         char cmd_buf[SNPRINTF_BUF_LEN];
1224         char cmnt_buf[SNPRINTF_BUF_LEN];
1225
1226         switch (pnc) {
1227                 case pn_Cmp_Leg: /* odered */
1228                         sse_pnc = 7;
1229                         break;
1230                 case pn_Cmp_Uo:  /* unordered */
1231                         sse_pnc = 3;
1232                         break;
1233                 case pn_Cmp_Ue:
1234                 case pn_Cmp_Eq:  /* == */
1235                         sse_pnc = 0;
1236                         break;
1237                 case pn_Cmp_Ul:
1238                 case pn_Cmp_Lt:  /* < */
1239                         sse_pnc = 1;
1240                         break;
1241                 case pn_Cmp_Ule:
1242                 case pn_Cmp_Le: /* <= */
1243                         sse_pnc = 2;
1244                         break;
1245                 case pn_Cmp_Ug:
1246                 case pn_Cmp_Gt:  /* > */
1247                         sse_pnc = 6;
1248                         break;
1249                 case pn_Cmp_Uge:
1250                 case pn_Cmp_Ge: /* >= */
1251                         sse_pnc = 5;
1252                         break;
1253                 case pn_Cmp_Ne:
1254                 case pn_Cmp_Lg:  /* != */
1255                         sse_pnc = 4;
1256                         break;
1257         }
1258
1259         assert(sse_pnc >= 0 && "unsupported compare");
1260
1261         if (unord && sse_pnc != 3) {
1262                 /*
1263                         We need a separate compare against unordered.
1264                         Quick and Dirty solution:
1265                         - get some memory on stack
1266                         - compare
1267                         - store result
1268                         - compare
1269                         - and result and stored result
1270                     - cleanup stack
1271                 */
1272                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "sub %%esp, 8");
1273                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* reserve some space for unordered compare result */");
1274                 IA32_DO_EMIT(NULL);
1275                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmpsd %s, 3", ia32_emit_binop(irn, env));
1276                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* SSE compare: unordered */");
1277                 IA32_DO_EMIT(NULL);
1278                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "movsd [%%esp], %1D", irn);
1279                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* store compare result */");
1280                 IA32_DO_EMIT(NULL);
1281         }
1282
1283         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmpsd %s, %d", ia32_emit_binop(irn, env), sse_pnc);
1284         lc_esnprintf(arg_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* SSE compare (%+F) with result in %1D */", irn, irn);
1285         IA32_DO_EMIT(irn);
1286
1287         if (unord && sse_pnc != 3) {
1288                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "andpd %1D, [%%esp]", irn);
1289                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* build the final result */");
1290                 IA32_DO_EMIT(NULL);
1291                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "add %%esp, 8");
1292                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* free allocated space */");
1293                 IA32_DO_EMIT(NULL);
1294         }
1295 }
1296
1297 /*********************************************************
1298  *                 _ _       _
1299  *                (_) |     (_)
1300  *   ___ _ __ ___  _| |_     _ _   _ _ __ ___  _ __  ___
1301  *  / _ \ '_ ` _ \| | __|   | | | | | '_ ` _ \| '_ \/ __|
1302  * |  __/ | | | | | | |_    | | |_| | | | | | | |_) \__ \
1303  *  \___|_| |_| |_|_|\__|   | |\__,_|_| |_| |_| .__/|___/
1304  *                         _/ |               | |
1305  *                        |__/                |_|
1306  *********************************************************/
1307
1308 /* jump table entry (target and corresponding number) */
1309 typedef struct _branch_t {
1310         ir_node *target;
1311         int      value;
1312 } branch_t;
1313
1314 /* jump table for switch generation */
1315 typedef struct _jmp_tbl_t {
1316         ir_node  *defProj;         /**< default target */
1317         int       min_value;       /**< smallest switch case */
1318         int       max_value;       /**< largest switch case */
1319         int       num_branches;    /**< number of jumps */
1320         char     *label;           /**< label of the jump table */
1321         branch_t *branches;        /**< jump array */
1322 } jmp_tbl_t;
1323
1324 /**
1325  * Compare two variables of type branch_t. Used to sort all switch cases
1326  */
1327 static int ia32_cmp_branch_t(const void *a, const void *b) {
1328         branch_t *b1 = (branch_t *)a;
1329         branch_t *b2 = (branch_t *)b;
1330
1331         if (b1->value <= b2->value)
1332                 return -1;
1333         else
1334                 return 1;
1335 }
1336
1337 /**
1338  * Emits code for a SwitchJmp (creates a jump table if
1339  * possible otherwise a cmp-jmp cascade). Port from
1340  * cggg ia32 backend
1341  */
1342 static void emit_ia32_SwitchJmp(const ir_node *irn, ia32_emit_env_t *emit_env) {
1343         unsigned long       interval;
1344         char                buf[SNPRINTF_BUF_LEN];
1345         int                 last_value, i, pn;
1346         jmp_tbl_t           tbl;
1347         ir_node            *proj;
1348         const ir_edge_t    *edge;
1349         const lc_arg_env_t *env = ia32_get_arg_env();
1350         FILE               *F   = emit_env->out;
1351         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1352
1353         /* fill the table structure */
1354         tbl.label        = xmalloc(SNPRINTF_BUF_LEN);
1355         tbl.label        = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, ".TBL_");
1356         tbl.defProj      = NULL;
1357         tbl.num_branches = get_irn_n_edges(irn);
1358         tbl.branches     = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
1359         tbl.min_value    = INT_MAX;
1360         tbl.max_value    = INT_MIN;
1361
1362         i = 0;
1363         /* go over all proj's and collect them */
1364         foreach_out_edge(irn, edge) {
1365                 proj = get_edge_src_irn(edge);
1366                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
1367
1368                 pn = get_Proj_proj(proj);
1369
1370                 /* create branch entry */
1371                 tbl.branches[i].target = proj;
1372                 tbl.branches[i].value  = pn;
1373
1374                 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
1375                 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
1376
1377                 /* check for default proj */
1378                 if (pn == get_ia32_pncode(irn)) {
1379                         assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
1380                         tbl.defProj = proj;
1381                 }
1382
1383                 i++;
1384         }
1385
1386         /* sort the branches by their number */
1387         qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
1388
1389         /* two-complement's magic make this work without overflow */
1390         interval = tbl.max_value - tbl.min_value;
1391
1392         /* emit the table */
1393         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, %u", irn, interval);
1394         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* compare for switch */");
1395         IA32_DO_EMIT(irn);
1396
1397         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
1398         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default jump if out of range  */");
1399         IA32_DO_EMIT(irn);
1400
1401         if (tbl.num_branches > 1) {
1402                 /* create table */
1403
1404                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp %s[%1S*4]", tbl.label, irn);
1405                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get jump table entry as target */");
1406                 IA32_DO_EMIT(irn);
1407
1408                 ia32_switch_section(F, SECTION_RODATA);
1409                 fprintf(F, "\t.align 4\n");
1410
1411                 fprintf(F, "%s:\n", tbl.label);
1412
1413                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
1414                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */",  tbl.branches[0].value);
1415                 IA32_DO_EMIT(irn);
1416
1417                 last_value = tbl.branches[0].value;
1418                 for (i = 1; i < tbl.num_branches; ++i) {
1419                         while (++last_value < tbl.branches[i].value) {
1420                                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
1421                                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
1422                                 IA32_DO_EMIT(irn);
1423                         }
1424                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf));
1425                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", last_value);
1426                         IA32_DO_EMIT(irn);
1427                 }
1428                 ia32_switch_section(F, SECTION_TEXT);
1429         }
1430         else {
1431                 /* one jump is enough */
1432                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
1433                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* only one case given */");
1434                 IA32_DO_EMIT(irn);
1435         }
1436
1437         if (tbl.label)
1438                 free(tbl.label);
1439         if (tbl.branches)
1440                 free(tbl.branches);
1441 }
1442
1443 /**
1444  * Emits code for a unconditional jump.
1445  */
1446 static void emit_Jmp(const ir_node *irn, ia32_emit_env_t *env) {
1447         ir_node *block, *next_bl;
1448         FILE *F = env->out;
1449         char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1450
1451         /* for now, the code works for scheduled and non-schedules blocks */
1452         block = get_nodes_block(irn);
1453
1454         /* we have a block schedule */
1455         next_bl = next_blk_sched(block);
1456         if (get_cfop_target_block(irn) != next_bl) {
1457                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf));
1458                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F) */", irn, get_cfop_target_block(irn));
1459         }
1460         else {
1461                 cmd_buf[0] = '\0';
1462                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(irn, buf));
1463         }
1464         IA32_DO_EMIT(irn);
1465 }
1466
1467 /****************************
1468  *                  _
1469  *                 (_)
1470  *  _ __  _ __ ___  _  ___
1471  * | '_ \| '__/ _ \| |/ __|
1472  * | |_) | | | (_) | |\__ \
1473  * | .__/|_|  \___/| ||___/
1474  * | |            _/ |
1475  * |_|           |__/
1476  ****************************/
1477
1478 /**
1479  * Emits code for a proj -> node
1480  */
1481 static void emit_Proj(const ir_node *irn, ia32_emit_env_t *env) {
1482         ir_node *pred = get_Proj_pred(irn);
1483
1484         if (get_irn_op(pred) == op_Start) {
1485                 switch(get_Proj_proj(irn)) {
1486                         case pn_Start_X_initial_exec:
1487                                 emit_Jmp(irn, env);
1488                                 break;
1489                         default:
1490                                 break;
1491                 }
1492         }
1493 }
1494
1495 /**********************************
1496  *   _____                  ____
1497  *  / ____|                |  _ \
1498  * | |     ___  _ __  _   _| |_) |
1499  * | |    / _ \| '_ \| | | |  _ <
1500  * | |___| (_) | |_) | |_| | |_) |
1501  *  \_____\___/| .__/ \__, |____/
1502  *             | |     __/ |
1503  *             |_|    |___/
1504  **********************************/
1505
1506 /**
1507  * Emit movsb/w instructions to make mov count divideable by 4
1508  */
1509 static void emit_CopyB_prolog(FILE *F, const ir_node *irn, int rem) {
1510         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1511
1512         ir_fprintf(F, "\t/* memcopy prolog %+F */\n", irn);
1513
1514         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
1515         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward */");
1516
1517         switch(rem) {
1518                 case 1:
1519                         IA32_DO_EMIT(NULL);
1520                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1521                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 1 */");
1522                         break;
1523                 case 2:
1524                         IA32_DO_EMIT(NULL);
1525                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1526                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 2 */");
1527                         break;
1528                 case 3:
1529                         IA32_DO_EMIT(NULL);
1530                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1531                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1532                         IA32_DO_EMIT(NULL);
1533                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1534                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1535                         break;
1536         }
1537
1538         IA32_DO_EMIT(NULL);
1539 }
1540
1541 /**
1542  * Emit rep movsd instruction for memcopy.
1543  */
1544 static void emit_ia32_CopyB(const ir_node *irn, ia32_emit_env_t *emit_env) {
1545         FILE   *F  = emit_env->out;
1546         tarval *tv = get_ia32_Immop_tarval(irn);
1547         int    rem = get_tarval_long(tv);
1548         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1549
1550         emit_CopyB_prolog(F, irn, rem);
1551
1552         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "rep movsd");
1553         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy */");
1554         IA32_DO_EMIT(irn);
1555 }
1556
1557 /**
1558  * Emits unrolled memcopy.
1559  */
1560 static void emit_ia32_CopyB_i(const ir_node *irn, ia32_emit_env_t *emit_env) {
1561         tarval *tv   = get_ia32_Immop_tarval(irn);
1562         int     size = get_tarval_long(tv);
1563         FILE   *F    = emit_env->out;
1564         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1565
1566         emit_CopyB_prolog(F, irn, size & 0x3);
1567
1568         size >>= 2;
1569         while (size--) {
1570                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsd");
1571                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy unrolled */");
1572                 IA32_DO_EMIT(irn);
1573         }
1574 }
1575
1576
1577
1578 /***************************
1579  *   _____
1580  *  / ____|
1581  * | |     ___  _ ____   __
1582  * | |    / _ \| '_ \ \ / /
1583  * | |___| (_) | | | \ V /
1584  *  \_____\___/|_| |_|\_/
1585  *
1586  ***************************/
1587
1588 /**
1589  * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1590  */
1591 static void emit_ia32_Conv_with_FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1592         FILE               *F        = emit_env->out;
1593         const lc_arg_env_t *env      = ia32_get_arg_env();
1594         ir_mode            *src_mode = get_ia32_src_mode(irn);
1595         ir_mode            *tgt_mode = get_ia32_tgt_mode(irn);
1596         char               *from, *to, buf[64];
1597         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1598
1599         from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
1600         to   = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
1601
1602         switch(get_ia32_op_type(irn)) {
1603                 case ia32_Normal:
1604                         lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
1605                         break;
1606                 case ia32_AddrModeS:
1607                         lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn, emit_env));
1608                         break;
1609                 default:
1610                         assert(0 && "unsupported op type for Conv");
1611         }
1612
1613         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cvt%s2%s %s", from, to, buf);
1614         lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F, %+F) */", irn, src_mode, tgt_mode);
1615         IA32_DO_EMIT(irn);
1616 }
1617
1618 static void emit_ia32_Conv_I2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1619         emit_ia32_Conv_with_FP(irn, emit_env);
1620 }
1621
1622 static void emit_ia32_Conv_FP2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1623         emit_ia32_Conv_with_FP(irn, emit_env);
1624 }
1625
1626 static void emit_ia32_Conv_FP2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1627         emit_ia32_Conv_with_FP(irn, emit_env);
1628 }
1629
1630 /**
1631  * Emits code for an Int conversion.
1632  */
1633 static void emit_ia32_Conv_I2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1634         FILE               *F        = emit_env->out;
1635         const lc_arg_env_t *env      = ia32_get_arg_env();
1636         char               *move_cmd = "movzx";
1637         char               *conv_cmd = NULL;
1638         ir_mode            *src_mode = get_ia32_src_mode(irn);
1639         ir_mode            *tgt_mode = get_ia32_tgt_mode(irn);
1640         int                signed_mode;
1641         int n, m;
1642         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1643         const arch_register_t *in_reg, *out_reg;
1644
1645         n = get_mode_size_bits(src_mode);
1646         m = get_mode_size_bits(tgt_mode);
1647
1648         assert(n == 8 || n == 16 || n == 32);
1649         assert(m == 8 || m == 16 || m == 32);
1650         assert(n != m);
1651
1652         signed_mode = mode_is_signed(n < m ? src_mode : tgt_mode);
1653         if(signed_mode) {
1654                 move_cmd = "movsx";
1655         }
1656
1657         switch(get_ia32_op_type(irn)) {
1658                 case ia32_Normal:
1659                         in_reg  = get_in_reg(irn, 2);
1660                         out_reg = get_out_reg(irn, 0);
1661
1662                         if (REGS_ARE_EQUAL(in_reg, &ia32_gp_regs[REG_EAX]) &&
1663                                 REGS_ARE_EQUAL(out_reg, in_reg)                &&
1664                                 signed_mode)
1665                         {
1666                                 if (n == 8 || m == 8)
1667                                         conv_cmd = "cbw";
1668                                 else if (n == 16 || m == 16)
1669                                         conv_cmd = "cwde";
1670
1671                                 /* argument and result are both in EAX and */
1672                                 /* signedness is ok: -> use converts       */
1673                                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s", conv_cmd);
1674                         }
1675                         else if (REGS_ARE_EQUAL(out_reg, in_reg) &&     ! signed_mode)
1676                         {
1677                                 /* argument and result are in the same register */
1678                                 /* and signedness is ok: -> use and with mask   */
1679                                 int mask = (1 << (n < m ? n : m)) - 1;
1680                                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "and %1D, 0x%x", irn, mask);
1681                         }
1682                         else {
1683                                 /* use move w/o sign extension */
1684                                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %%%s",
1685                                         move_cmd, irn, ia32_get_reg_name_for_mode(emit_env, n < m ? src_mode : tgt_mode, in_reg));
1686                         }
1687
1688                         break;
1689                 case ia32_AddrModeS:
1690                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %s",
1691                                 move_cmd, irn, ia32_emit_am(irn, emit_env));
1692                         break;
1693                 default:
1694                         assert(0 && "unsupported op type for Conv");
1695         }
1696
1697         lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%d Bit mode_%F -> %d Bit mode_%F) */",
1698                 irn, n, src_mode, m, tgt_mode);
1699
1700         IA32_DO_EMIT(irn);
1701 }
1702
1703 /**
1704  * Emits code for an 8Bit Int conversion.
1705  */
1706 void emit_ia32_Conv_I2I8Bit(const ir_node *irn, ia32_emit_env_t *emit_env) {
1707         emit_ia32_Conv_I2I(irn, emit_env);
1708 }
1709
1710
1711 /*******************************************
1712  *  _                          _
1713  * | |                        | |
1714  * | |__   ___ _ __   ___   __| | ___  ___
1715  * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1716  * | |_) |  __/ | | | (_) | (_| |  __/\__ \
1717  * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1718  *
1719  *******************************************/
1720
1721 /**
1722  * Emits a backend call
1723  */
1724 static void emit_be_Call(const ir_node *irn, ia32_emit_env_t *emit_env) {
1725         FILE *F = emit_env->out;
1726         entity *ent = be_Call_get_entity(irn);
1727         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1728
1729         if (ent) {
1730                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "call %s", get_entity_ld_name(ent));
1731         }
1732         else {
1733                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "call %1D", get_irn_n(irn, be_pos_Call_ptr));
1734         }
1735
1736         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
1737
1738         IA32_DO_EMIT(irn);
1739 }
1740
1741 /**
1742  * Emits code to increase stack pointer.
1743  */
1744 static void emit_be_IncSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1745         FILE          *F    = emit_env->out;
1746         int offs = be_get_IncSP_offset(irn);
1747         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1748
1749         if (offs) {
1750                 if (offs > 0)
1751                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "sub %1S, %u", irn, offs);
1752                 else
1753                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S, %u", irn, -offs);
1754                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (IncSP) */", irn);
1755         }
1756         else {
1757                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1758                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted %+F (IncSP) with 0 */", irn);
1759         }
1760
1761         IA32_DO_EMIT(irn);
1762 }
1763
1764 /**
1765  * Emits code to set stack pointer.
1766  */
1767 static void emit_be_SetSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1768         FILE *F = emit_env->out;
1769         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1770
1771         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1772         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (restore SP) */", irn);
1773         IA32_DO_EMIT(irn);
1774 }
1775
1776 /**
1777  * Emits code for Copy/CopyKeep.
1778  */
1779 static void Copy_emitter(const ir_node *irn, ir_node *op, ia32_emit_env_t *emit_env) {
1780         FILE             *F    = emit_env->out;
1781         const arch_env_t *aenv = emit_env->arch_env;
1782         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1783
1784         if (REGS_ARE_EQUAL(arch_get_irn_register(aenv, irn), arch_get_irn_register(aenv, op)) ||
1785                 arch_register_type_is(arch_get_irn_register(aenv, op), virtual))
1786                 return;
1787
1788         if (mode_is_float(get_irn_mode(irn)))
1789                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "movs%M %1D, %1S", irn, irn, irn);
1790         else
1791                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
1792         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1793         IA32_DO_EMIT(irn);
1794 }
1795
1796 static void emit_be_Copy(const ir_node *irn, ia32_emit_env_t *emit_env) {
1797         Copy_emitter(irn, be_get_Copy_op(irn), emit_env);
1798 }
1799
1800 static void emit_be_CopyKeep(const ir_node *irn, ia32_emit_env_t *emit_env) {
1801         Copy_emitter(irn, be_get_CopyKeep_op(irn), emit_env);
1802 }
1803
1804 /**
1805  * Emits code for exchange.
1806  */
1807 static void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
1808         FILE *F = emit_env->out;
1809         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1810         const arch_register_t *in1, *in2;
1811         const arch_register_class_t *cls1, *cls2;
1812
1813         in1 = arch_get_irn_register(emit_env->arch_env, get_irn_n(irn, 0));
1814         in2 = arch_get_irn_register(emit_env->arch_env, get_irn_n(irn, 1));
1815
1816         cls1 = arch_register_get_class(in1);
1817         cls2 = arch_register_get_class(in2);
1818
1819         assert(cls1 == cls2 && "Register class mismatch at Perm");
1820
1821         if (cls1 == &ia32_reg_classes[CLASS_ia32_gp]) {
1822                 if(emit_env->isa->opt_arch == arch_athlon) {
1823                         // xchg commands are Vector path on athlons and therefore stall the DirectPath pipeline
1824                         // it is nearly always beneficial to use the 3 xor trick instead of an xchg
1825                         cmnt_buf[0] = 0;
1826                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xor %1S, %2S", irn, irn);
1827                         IA32_DO_EMIT(irn);
1828                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xor %2S, %1S", irn, irn);
1829                         IA32_DO_EMIT(irn);
1830                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xor %1S, %2S", irn, irn);
1831                 } else {
1832                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1833                 }
1834         }
1835         else if (cls1 == &ia32_reg_classes[CLASS_ia32_xmm]) {
1836                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN,
1837                         "pxor %1S, %2S\n\tpxor %2S, %1S\n\tpxor %1S, %2S", irn, irn, irn, irn, irn, irn);
1838         }
1839         else if (cls1 == &ia32_reg_classes[CLASS_ia32_vfp]) {
1840                 /* is a NOP */
1841                 cmd_buf[0] = '\0';
1842         }
1843         else if (cls1 == &ia32_reg_classes[CLASS_ia32_st]) {
1844                 /* is a NOP */
1845                 cmd_buf[0] = '\0';
1846         }
1847
1848         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%1A, %2A) */", irn, irn, irn);
1849         IA32_DO_EMIT(irn);
1850 }
1851
1852 /**
1853  * Emits code for Constant loading.
1854  */
1855 static void emit_ia32_Const(const ir_node *n, ia32_emit_env_t *env) {
1856         FILE *F = env->out;
1857         char cmd_buf[256], cmnt_buf[256];
1858         const lc_arg_env_t *arg_env = ia32_get_arg_env();
1859         ir_mode *mode = get_irn_mode(n);
1860         tarval *tv = get_ia32_Immop_tarval(n);
1861
1862         if (get_ia32_op_type(n) == ia32_SymConst) {
1863                 lc_esnprintf(arg_env, cmd_buf, 256, "mov %1D, OFFSET FLAT:%C ", n, n);
1864                 lc_esnprintf(arg_env, cmnt_buf, 256, "/* Move address of SymConst into register */");
1865         } else {
1866                 assert(mode == get_tarval_mode(tv) || (mode_is_reference(get_tarval_mode(tv)) && mode == mode_Iu));
1867                 /* beware: in some rare cases mode is mode_b which has no tarval_null() */
1868                 if (tv == get_tarval_b_false() || tv == get_tarval_null(mode)) {
1869                         const char *instr = "xor";
1870                         if (env->isa->opt_arch == arch_pentium_4) {
1871                                 /* P4 prefers sub r, r, others xor r, r */
1872                                 instr = "sub";
1873                         }
1874                         lc_esnprintf(arg_env, cmd_buf, 256, "%s %1D, %1D ", instr, n, n);
1875                         lc_esnprintf(arg_env, cmnt_buf, 256, "/* optimized mov 0 to register */");
1876                 } else {
1877                         lc_esnprintf(arg_env, cmd_buf, 256, "mov %1D, %C ", n, n);
1878                         lc_esnprintf(arg_env, cmnt_buf, 256, "/* Mov Const into register */");
1879                 }
1880         }
1881         lc_efprintf(arg_env, F, "\t%-35s %-60s /* %+F (%+G) */\n", cmd_buf, cmnt_buf, n, n);
1882 }
1883
1884 /**
1885  * Emits code to increase stack pointer.
1886  */
1887 static void emit_ia32_AddSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1888         FILE *F = emit_env->out;
1889         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1890
1891         if (is_ia32_ImmConst(irn)) {
1892                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "sub %1D, %C", irn, irn);
1893         }
1894         else if (is_ia32_ImmSymConst(irn)) {
1895                 if (get_ia32_op_type(irn) == ia32_Normal)
1896                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "sub %1D, OFFSET_FLAT:%C", irn, irn);
1897                 else /* source address mode */
1898                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "sub %1D, [%s%s]", irn, get_id_str(get_ia32_am_sc(irn)), get_ia32_am_offs(irn));
1899         }
1900         else {
1901                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "sub %1D, %2S", irn, irn);
1902         }
1903         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* reserve space on stack */");
1904
1905         IA32_DO_EMIT(irn);
1906 }
1907
1908 /**
1909  * Emits code to increase stack pointer.
1910  */
1911 static void emit_ia32_SubSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1912         FILE *F = emit_env->out;
1913         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1914
1915         if (is_ia32_ImmConst(irn)) {
1916                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1D, %C", irn, irn);
1917         }
1918         else if (is_ia32_ImmSymConst(irn)) {
1919                 if (get_ia32_op_type(irn) == ia32_Normal)
1920                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1D, OFFSET_FLAT:%C", irn, irn);
1921                 else /* source address mode */
1922                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1D, [%s%s]", irn, get_id_str(get_ia32_am_sc(irn)), get_ia32_am_offs(irn));
1923         }
1924         else {
1925                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1D, %2S", irn, irn);
1926         }
1927         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* free space on stack */");
1928
1929         IA32_DO_EMIT(irn);
1930 }
1931
1932 /**
1933  * Emits code to load the TLS base
1934  */
1935 static void emit_ia32_LdTls(const ir_node *irn, ia32_emit_env_t *emit_env) {
1936         FILE *F = emit_env->out;
1937         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1938
1939         switch (asm_flavour) {
1940         case ASM_LINUX_GAS:
1941                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, DWORD PTR %%gs:0", irn);
1942                 break;
1943         case ASM_MINGW_GAS:
1944                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, DWORD PTR %%gs:0", irn);
1945                 break;
1946         default:
1947                 assert(0 && "unsupported TLS");
1948                 break;
1949         }
1950         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get thread local storage base */");
1951
1952         IA32_DO_EMIT(irn);
1953 }
1954
1955 static void emit_be_Return(const ir_node *n, ia32_emit_env_t *env) {
1956         FILE *F = env->out;
1957         const lc_arg_env_t *arg_env = ia32_get_arg_env();
1958
1959         lc_efprintf(arg_env, F, "\t%-35s %-60s /* %+F (%+G) */\n", "ret", "/* be_Return */", n, n);
1960 }
1961
1962 static void emit_Nothing(const ir_node *n, ia32_emit_env_t *env) {
1963         FILE *F = env->out;
1964
1965         ir_fprintf(F, "\t%35s /* %+F (%+G) */\n", " ", n, n);
1966 }
1967
1968
1969 /***********************************************************************************
1970  *                  _          __                                             _
1971  *                 (_)        / _|                                           | |
1972  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
1973  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1974  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
1975  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
1976  *
1977  ***********************************************************************************/
1978
1979 /**
1980  * Enters the emitter functions for handled nodes into the generic
1981  * pointer of an opcode.
1982  */
1983 static void ia32_register_emitters(void) {
1984
1985 #define IA32_EMIT2(a,b) op_ia32_##a->ops.generic = (op_func)emit_ia32_##b
1986 #define IA32_EMIT(a)    IA32_EMIT2(a,a)
1987 #define EMIT(a)         op_##a->ops.generic = (op_func)emit_##a
1988 #define IGN(a)                  op_##a->ops.generic = (op_func)emit_Nothing
1989 #define BE_EMIT(a)      op_be_##a->ops.generic = (op_func)emit_be_##a
1990 #define BE_IGN(a)               op_be_##a->ops.generic = (op_func)emit_Nothing
1991
1992         /* first clear the generic function pointer for all ops */
1993         clear_irp_opcodes_generic_func();
1994
1995         /* register all emitter functions defined in spec */
1996         ia32_register_spec_emitters();
1997
1998         /* other ia32 emitter functions */
1999         IA32_EMIT(CondJmp);
2000         IA32_EMIT(TestJmp);
2001         IA32_EMIT(CJmp);
2002         IA32_EMIT(CJmpAM);
2003         IA32_EMIT(CmpCMov);
2004         IA32_EMIT(PsiCondCMov);
2005         IA32_EMIT(CmpSet);
2006         IA32_EMIT(PsiCondSet);
2007         IA32_EMIT(SwitchJmp);
2008         IA32_EMIT(CopyB);
2009         IA32_EMIT(CopyB_i);
2010         IA32_EMIT(Conv_I2FP);
2011         IA32_EMIT(Conv_FP2I);
2012         IA32_EMIT(Conv_FP2FP);
2013         IA32_EMIT(Conv_I2I);
2014         IA32_EMIT(Conv_I2I8Bit);
2015         IA32_EMIT(Const);
2016         IA32_EMIT(AddSP);
2017         IA32_EMIT(SubSP);
2018         IA32_EMIT(LdTls);
2019         IA32_EMIT(xCmp);
2020         IA32_EMIT(xCmpSet);
2021         IA32_EMIT(xCmpCMov);
2022         IA32_EMIT(xCondJmp);
2023         IA32_EMIT2(fcomJmp, x87CondJmp);
2024         IA32_EMIT2(fcompJmp, x87CondJmp);
2025         IA32_EMIT2(fcomppJmp, x87CondJmp);
2026         IA32_EMIT2(fcomrJmp, x87CondJmp);
2027         IA32_EMIT2(fcomrpJmp, x87CondJmp);
2028         IA32_EMIT2(fcomrppJmp, x87CondJmp);
2029
2030         /* benode emitter */
2031         BE_EMIT(Call);
2032         BE_EMIT(IncSP);
2033         BE_EMIT(SetSP);
2034         BE_EMIT(Copy);
2035         BE_EMIT(CopyKeep);
2036         BE_EMIT(Perm);
2037         BE_EMIT(Return);
2038
2039         BE_IGN(RegParams);
2040         BE_IGN(Barrier);
2041         BE_IGN(Keep);
2042
2043         /* firm emitter */
2044         EMIT(Jmp);
2045         EMIT(Proj);
2046         IGN(Phi);
2047         IGN(Start);
2048
2049 #undef BE_EMIT
2050 #undef EMIT
2051 #undef IGN
2052 #undef IA32_EMIT2
2053 #undef IA32_EMIT
2054 }
2055
2056 static const char *last_name = NULL;
2057 static unsigned last_line = -1;
2058 static unsigned num = -1;
2059
2060 /**
2061  * Emit the debug support for node irn.
2062  */
2063 static void ia32_emit_dbg(const ir_node *irn, ia32_emit_env_t *env) {
2064         dbg_info *db = get_irn_dbg_info(irn);
2065         unsigned lineno;
2066         const char *fname = be_retrieve_dbg_info(db, &lineno);
2067
2068         if (! env->cg->birg->main_env->options->stabs_debug_support)
2069                 return;
2070
2071         if (fname) {
2072                 if (last_name != fname) {
2073                         last_line = -1;
2074                         be_dbg_include_begin(env->cg->birg->main_env->db_handle, fname);
2075                         last_name = fname;
2076                 }
2077                 if (last_line != lineno) {
2078                         char name[64];
2079                         FILE *F = env->out;
2080
2081                         snprintf(name, sizeof(name), ".LM%u", ++num);
2082                         last_line = lineno;
2083                         be_dbg_line(env->cg->birg->main_env->db_handle, lineno, name);
2084                         fprintf(F, "%s:\n", name);
2085                 }
2086         }
2087 }
2088
2089 /**
2090  * Emits code for a node.
2091  */
2092 static void ia32_emit_node(const ir_node *irn, void *env) {
2093         ia32_emit_env_t   *emit_env = env;
2094         ir_op             *op       = get_irn_op(irn);
2095         DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
2096
2097         DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
2098
2099         if (op->ops.generic) {
2100                 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
2101                 ia32_emit_dbg(irn, emit_env);
2102                 (*emit)(irn, env);
2103         }
2104         else {
2105                 emit_Nothing(irn, env);
2106                 ir_fprintf(stderr, "Warning: No emit handler for node %+F (%+G)\n", irn, irn);
2107         }
2108 }
2109
2110 /**
2111  * Emits gas alignment directives
2112  */
2113 static void ia32_emit_alignment(FILE *F, unsigned align, unsigned skip) {
2114         fprintf(F, "\t.p2align %u,,%u\n", align, skip);
2115 }
2116
2117 /**
2118  * Emits gas alignment directives for Functions depended on cpu architecture.
2119  */
2120 static void ia32_emit_align_func(FILE *F, cpu_support cpu) {
2121         unsigned align;
2122         unsigned maximum_skip;
2123
2124         switch (cpu) {
2125                 case arch_i386:
2126                         align = 2;
2127                         break;
2128                 case arch_i486:
2129                         align = 4;
2130                         break;
2131                 case arch_k6:
2132                         align = 5;
2133                         break;
2134                 default:
2135                         align = 4;
2136         }
2137         maximum_skip = (1 << align) - 1;
2138         ia32_emit_alignment(F, align, maximum_skip);
2139 }
2140
2141 /**
2142  * Emits gas alignment directives for Labels depended on cpu architecture.
2143  */
2144 static void ia32_emit_align_label(FILE *F, cpu_support cpu) {
2145         unsigned align; unsigned maximum_skip;
2146
2147         switch (cpu) {
2148                 case arch_i386:
2149                         align = 2;
2150                         break;
2151                 case arch_i486:
2152                         align = 4;
2153                         break;
2154                 case arch_k6:
2155                         align = 5;
2156                         break;
2157                 default:
2158                         align = 4;
2159         }
2160         maximum_skip = (1 << align) - 1;
2161         ia32_emit_alignment(F, align, maximum_skip);
2162 }
2163
2164 static int is_first_loop_block(ir_node *block, ir_node *prev_block, ia32_emit_env_t *env) {
2165         ir_exec_freq *execfreqs = env->cg->birg->execfreqs;
2166         double block_freq, prev_freq;
2167         static const double DELTA = .0001;
2168         cpu_support cpu = env->isa->opt_arch;
2169
2170         if(execfreqs == NULL)
2171                 return 0;
2172         if(cpu == arch_i386 || cpu == arch_i486)
2173                 return 0;
2174
2175         block_freq = get_block_execfreq(execfreqs, block);
2176         prev_freq = get_block_execfreq(execfreqs, prev_block);
2177
2178         if(block_freq < DELTA || prev_freq < DELTA)
2179                 return 0;
2180
2181         block_freq /= prev_freq;
2182
2183         switch (cpu) {
2184                 case arch_athlon:
2185                 case arch_athlon_64:
2186                 case arch_k6:
2187                         return block_freq > 3;
2188                 default:
2189                         break;
2190         }
2191
2192         return block_freq > 2;
2193 }
2194
2195 /**
2196  * Walks over the nodes in a block connected by scheduling edges
2197  * and emits code for each node.
2198  */
2199 static void ia32_gen_block(ir_node *block, ir_node *last_block, ia32_emit_env_t *env) {
2200         ir_graph      *irg         = get_irn_irg(block);
2201         ir_node       *start_block = get_irg_start_block(irg);
2202         int           need_label   = 1;
2203         FILE          *F           = env->out;
2204         const ir_node *irn;
2205         int           i;
2206
2207         assert(is_Block(block));
2208
2209         if (block == start_block)
2210                 need_label = 0;
2211
2212         if (need_label && get_irn_arity(block) == 1) {
2213                 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
2214
2215                 if (pred_block == last_block && get_irn_n_edges_kind(pred_block, EDGE_KIND_BLOCK) <= 2)
2216                         need_label = 0;
2217         }
2218
2219         /* special case: if one of our cfg preds is a switch-jmp we need a label, */
2220         /*               otherwise there might be jump table entries jumping to   */
2221         /*               non-existent (omitted) labels                            */
2222         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
2223                 ir_node *pred = get_Block_cfgpred(block, i);
2224
2225                 if (is_Proj(pred)) {
2226                         assert(get_irn_mode(pred) == mode_X);
2227                         if (is_ia32_SwitchJmp(get_Proj_pred(pred))) {
2228                                 need_label = 1;
2229                                 break;
2230                         }
2231                 }
2232         }
2233
2234         /* special case because the start block contains no jump instruction */
2235         if (last_block == start_block) {
2236                 const ir_edge_t *edge;
2237                 ir_node *startsucc = NULL;
2238
2239                 foreach_block_succ(start_block, edge) {
2240                         startsucc = get_edge_src_irn(edge);
2241                         if (startsucc != start_block)
2242                                 break;
2243                 }
2244                 assert(startsucc != NULL);
2245
2246                 /* if the last block was the start block and we are not inside the */
2247                 /* start successor, emit a jump to the start successor             */
2248                 if (startsucc != block) {
2249                         char buf[SNPRINTF_BUF_LEN];
2250                         ir_snprintf(buf, sizeof(buf), BLOCK_PREFIX("%d"),
2251                                     get_irn_node_nr(startsucc));
2252                         ir_fprintf(F, "\tjmp %s\n", buf);
2253                 }
2254         }
2255
2256         if (need_label) {
2257                 char cmd_buf[SNPRINTF_BUF_LEN];
2258                 int i, arity;
2259                 int align = 1;
2260                 ir_exec_freq *execfreqs = env->cg->birg->execfreqs;
2261
2262                 /* align the loop headers */
2263                 if (! is_first_loop_block(block, last_block, env)) {
2264                         /* align blocks where the previous block has no fallthrough */
2265                         arity = get_irn_arity(block);
2266
2267                         for (i = 0; i < arity; ++i) {
2268                                 ir_node *predblock = get_Block_cfgpred_block(block, i);
2269
2270                                 if (predblock == last_block) {
2271                                         align = 0;
2272                                         break;
2273                                 }
2274                         }
2275                 }
2276
2277                 if (align)
2278                         ia32_emit_align_label(env->out, env->isa->opt_arch);
2279
2280                 ir_snprintf(cmd_buf, sizeof(cmd_buf), BLOCK_PREFIX("%d:"),
2281                             get_irn_node_nr(block));
2282                 fprintf(F, "%-43s ", cmd_buf);
2283
2284                 /* emit list of pred blocks in comment */
2285                 fprintf(F, "/* preds:");
2286
2287                 arity = get_irn_arity(block);
2288                 for (i = 0; i < arity; ++i) {
2289                         ir_node *predblock = get_Block_cfgpred_block(block, i);
2290                         fprintf(F, " %ld", get_irn_node_nr(predblock));
2291                 }
2292
2293                 if (execfreqs != NULL) {
2294                         fprintf(F, " freq: %f", get_block_execfreq(execfreqs, block));
2295                 }
2296
2297                 fprintf(F, " */\n");
2298         }
2299
2300         /* emit the contents of the block */
2301         ia32_emit_dbg(block, env);
2302         sched_foreach(block, irn) {
2303                 ia32_emit_node(irn, env);
2304         }
2305 }
2306
2307 /**
2308  * Emits code for function start.
2309  */
2310 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg, ia32_emit_env_t *emit_env) {
2311         entity     *irg_ent  = get_irg_entity(irg);
2312         const char *irg_name = get_entity_ld_name(irg_ent);
2313         cpu_support cpu      = emit_env->isa->opt_arch;
2314         const be_irg_t *birg = emit_env->cg->birg;
2315
2316         fprintf(F, "\n");
2317         ia32_switch_section(F, SECTION_TEXT);
2318         be_dbg_method_begin(birg->main_env->db_handle, irg_ent, be_abi_get_stack_layout(birg->abi));
2319         ia32_emit_align_func(F, cpu);
2320         if (get_entity_visibility(irg_ent) == visibility_external_visible) {
2321                 fprintf(F, ".globl %s\n", irg_name);
2322         }
2323         ia32_dump_function_object(F, irg_name);
2324         fprintf(F, "%s:\n", irg_name);
2325 }
2326
2327 /**
2328  * Emits code for function end
2329  */
2330 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg, ia32_emit_env_t *emit_env) {
2331         const char *irg_name = get_entity_ld_name(get_irg_entity(irg));
2332         const be_irg_t *birg = emit_env->cg->birg;
2333
2334         ia32_dump_function_size(F, irg_name);
2335         be_dbg_method_end(birg->main_env->db_handle);
2336         fprintf(F, "\n");
2337 }
2338
2339 /**
2340  * Block-walker:
2341  * Sets labels for control flow nodes (jump target)
2342  * TODO: Jump optimization
2343  */
2344 static void ia32_gen_labels(ir_node *block, void *env) {
2345         ir_node *pred;
2346         int n = get_Block_n_cfgpreds(block);
2347
2348         for (n--; n >= 0; n--) {
2349                 pred = get_Block_cfgpred(block, n);
2350                 set_irn_link(pred, block);
2351         }
2352 }
2353
2354 /**
2355  * Main driver. Emits the code for one routine.
2356  */
2357 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
2358         ia32_emit_env_t emit_env;
2359         ir_node *block;
2360         ir_node *last_block = NULL;
2361
2362         emit_env.out      = F;
2363         emit_env.arch_env = cg->arch_env;
2364         emit_env.cg       = cg;
2365         emit_env.isa      = (ia32_isa_t *)cg->arch_env->isa;
2366         FIRM_DBG_REGISTER(emit_env.mod, "firm.be.ia32.emitter");
2367
2368         /* set the global arch_env (needed by print hooks) */
2369         arch_env = cg->arch_env;
2370
2371         ia32_register_emitters();
2372
2373         ia32_emit_func_prolog(F, irg, &emit_env);
2374         irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
2375
2376         if ((cg->opt & IA32_OPT_EXTBB) && cg->blk_sched) {
2377                 int i, n = ARR_LEN(cg->blk_sched);
2378
2379                 for (i = 0; i < n;) {
2380                         ir_node *next_bl;
2381
2382                         block   = cg->blk_sched[i];
2383                         ++i;
2384                         next_bl = i < n ? cg->blk_sched[i] : NULL;
2385
2386                         /* set here the link. the emitter expects to find the next block here */
2387                         set_irn_link(block, next_bl);
2388                         ia32_gen_block(block, last_block, &emit_env);
2389                         last_block = block;
2390                 }
2391         }
2392         else {
2393                 /* "normal" block schedule: Note the get_next_block() returns the NUMBER of the block
2394                    in the block schedule. As this number should NEVER be equal the next block,
2395                    we does not need a clear block link here. */
2396
2397                 //irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
2398                 // TODO
2399         }
2400
2401         ia32_emit_func_epilog(F, irg, &emit_env);
2402 }