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