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