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