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