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