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