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