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