Emit the "t" suffix for f96 numbers in the ia32 backend.
[libfirm] / ir / be / ia32 / ia32_emitter.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       This file implements the ia32 node emitter.
23  * @author      Christian Wuerdig, Matthias Braun
24  * @version     $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <limits.h>
31
32 #include "xmalloc.h"
33 #include "tv.h"
34 #include "iredges.h"
35 #include "debug.h"
36 #include "irgwalk.h"
37 #include "irprintf.h"
38 #include "irop_t.h"
39 #include "irargs_t.h"
40 #include "irprog_t.h"
41 #include "iredges_t.h"
42 #include "execfreq.h"
43 #include "error.h"
44 #include "raw_bitset.h"
45
46 #include "../besched_t.h"
47 #include "../benode_t.h"
48 #include "../beabi.h"
49 #include "../be_dbgout.h"
50 #include "../beemitter.h"
51 #include "../begnuas.h"
52 #include "../beirg_t.h"
53
54 #include "ia32_emitter.h"
55 #include "gen_ia32_emitter.h"
56 #include "gen_ia32_regalloc_if.h"
57 #include "ia32_nodes_attr.h"
58 #include "ia32_new_nodes.h"
59 #include "ia32_map_regs.h"
60 #include "bearch_ia32_t.h"
61
62 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
63
64 #define BLOCK_PREFIX ".L"
65
66 #define SNPRINTF_BUF_LEN 128
67
68 static const arch_env_t *arch_env;
69 static const ia32_isa_t *isa;
70 static ia32_code_gen_t  *cg;
71
72 /**
73  * Returns the register at in position pos.
74  */
75 static const arch_register_t *get_in_reg(const ir_node *irn, int pos)
76 {
77         ir_node               *op;
78         const arch_register_t *reg = NULL;
79
80         assert(get_irn_arity(irn) > pos && "Invalid IN position");
81
82         /* The out register of the operator at position pos is the
83            in register we need. */
84         op = get_irn_n(irn, pos);
85
86         reg = arch_get_irn_register(arch_env, op);
87
88         assert(reg && "no in register found");
89
90         if(reg == &ia32_gp_regs[REG_GP_NOREG])
91                 panic("trying to emit noreg for %+F input %d", irn, pos);
92
93         /* in case of unknown register: just return a valid register */
94         if (reg == &ia32_gp_regs[REG_GP_UKNWN]) {
95                 const arch_register_req_t *req;
96
97                 /* ask for the requirements */
98                 req = arch_get_register_req(arch_env, irn, pos);
99
100                 if (arch_register_req_is(req, limited)) {
101                         /* in case of limited requirements: get the first allowed register */
102                         unsigned idx = rbitset_next(req->limited, 0, 1);
103                         reg = arch_register_for_index(req->cls, idx);
104                 } else {
105                         /* otherwise get first register in class */
106                         reg = arch_register_for_index(req->cls, 0);
107                 }
108         }
109
110         return reg;
111 }
112
113 /**
114  * Returns the register at out position pos.
115  */
116 static const arch_register_t *get_out_reg(const ir_node *irn, int pos)
117 {
118         ir_node               *proj;
119         const arch_register_t *reg = NULL;
120
121         /* 1st case: irn is not of mode_T, so it has only                 */
122         /*           one OUT register -> good                             */
123         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
124         /*           Proj with the corresponding projnum for the register */
125
126         if (get_irn_mode(irn) != mode_T) {
127                 assert(pos == 0);
128                 reg = arch_get_irn_register(arch_env, irn);
129         } else if (is_ia32_irn(irn)) {
130                 reg = get_ia32_out_reg(irn, pos);
131         } else {
132                 const ir_edge_t *edge;
133
134                 foreach_out_edge(irn, edge) {
135                         proj = get_edge_src_irn(edge);
136                         assert(is_Proj(proj) && "non-Proj from mode_T node");
137                         if (get_Proj_proj(proj) == pos) {
138                                 reg = arch_get_irn_register(arch_env, proj);
139                                 break;
140                         }
141                 }
142         }
143
144         assert(reg && "no out register found");
145         return reg;
146 }
147
148 /**
149  * Add a number to a prefix. This number will not be used a second time.
150  */
151 static char *get_unique_label(char *buf, size_t buflen, const char *prefix)
152 {
153         static unsigned long id = 0;
154         snprintf(buf, buflen, "%s%lu", prefix, ++id);
155         return buf;
156 }
157
158 /*************************************************************
159  *             _       _    __   _          _
160  *            (_)     | |  / _| | |        | |
161  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
162  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
163  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
164  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
165  * | |                                       | |
166  * |_|                                       |_|
167  *************************************************************/
168
169 static void emit_8bit_register(const arch_register_t *reg)
170 {
171         const char *reg_name = arch_register_get_name(reg);
172
173         be_emit_char('%');
174         be_emit_char(reg_name[1]);
175         be_emit_char('l');
176 }
177
178 static void emit_16bit_register(const arch_register_t *reg)
179 {
180         const char *reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
181
182         be_emit_char('%');
183         be_emit_string(reg_name);
184 }
185
186 static void emit_register(const arch_register_t *reg, const ir_mode *mode)
187 {
188         const char *reg_name;
189
190         if(mode != NULL) {
191                 int size = get_mode_size_bits(mode);
192                 if(size == 8) {
193                         emit_8bit_register(reg);
194                         return;
195                 } else if(size == 16) {
196                         emit_16bit_register(reg);
197                         return;
198                 } else {
199                         assert(mode_is_float(mode) || size == 32);
200                 }
201         }
202
203         reg_name = arch_register_get_name(reg);
204
205         be_emit_char('%');
206         be_emit_string(reg_name);
207 }
208
209 void ia32_emit_source_register(const ir_node *node, int pos)
210 {
211         const arch_register_t *reg      = get_in_reg(node, pos);
212
213         emit_register(reg, NULL);
214 }
215
216 static void emit_ia32_Immediate(const ir_node *node);
217
218 void ia32_emit_8bit_source_register_or_immediate(const ir_node *node, int pos)
219 {
220         const arch_register_t *reg;
221         ir_node               *in = get_irn_n(node, pos);
222         if(is_ia32_Immediate(in)) {
223                 emit_ia32_Immediate(in);
224                 return;
225         }
226
227         reg = get_in_reg(node, pos);
228         emit_8bit_register(reg);
229 }
230
231 void ia32_emit_dest_register(const ir_node *node, int pos)
232 {
233         const arch_register_t *reg  = get_out_reg(node, pos);
234
235         emit_register(reg, NULL);
236 }
237
238 void ia32_emit_8bit_dest_register(const ir_node *node, int pos)
239 {
240         const arch_register_t *reg  = get_out_reg(node, pos);
241
242         emit_register(reg, mode_Bu);
243 }
244
245 void ia32_emit_x87_register(const ir_node *node, int pos)
246 {
247         const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
248
249         assert(pos < 3);
250         be_emit_char('%');
251         be_emit_string(attr->x87[pos]->name);
252 }
253
254 static void ia32_emit_mode_suffix_mode(const ir_mode *mode)
255 {
256         if(mode_is_float(mode)) {
257                 switch(get_mode_size_bits(mode)) {
258                 case 32: be_emit_char('s'); return;
259                 case 64: be_emit_char('l'); return;
260                 case 80:
261                 case 96: be_emit_char('t'); return;
262                 }
263         } else {
264                 assert(mode_is_int(mode) || mode_is_reference(mode));
265                 switch(get_mode_size_bits(mode)) {
266                 case 64: be_emit_cstring("ll"); return;
267                                  /* gas docu says q is the suffix but gcc, objdump and icc use
268                                         ll apparently */
269                 case 32: be_emit_char('l'); return;
270                 case 16: be_emit_char('w'); return;
271                 case 8:  be_emit_char('b'); return;
272                 }
273         }
274         panic("Can't output mode_suffix for %+F\n", mode);
275 }
276
277 void ia32_emit_mode_suffix(const ir_node *node)
278 {
279         ir_mode *mode = get_ia32_ls_mode(node);
280         if(mode == NULL)
281                 mode = mode_Iu;
282
283         ia32_emit_mode_suffix_mode(mode);
284 }
285
286 void ia32_emit_x87_mode_suffix(const ir_node *node)
287 {
288         ir_mode *mode = get_ia32_ls_mode(node);
289         assert(mode != NULL);
290         /* we only need to emit the mode on address mode */
291         if(get_ia32_op_type(node) != ia32_Normal)
292                 ia32_emit_mode_suffix_mode(mode);
293 }
294
295 static
296 char get_xmm_mode_suffix(ir_mode *mode)
297 {
298         assert(mode_is_float(mode));
299         switch(get_mode_size_bits(mode)) {
300         case 32:
301                 return 's';
302         case 64:
303                 return 'd';
304         default:
305                 assert(0);
306         }
307         return '%';
308 }
309
310 void ia32_emit_xmm_mode_suffix(const ir_node *node)
311 {
312         ir_mode *mode = get_ia32_ls_mode(node);
313         assert(mode != NULL);
314         be_emit_char('s');
315         be_emit_char(get_xmm_mode_suffix(mode));
316 }
317
318 void ia32_emit_xmm_mode_suffix_s(const ir_node *node)
319 {
320         ir_mode *mode = get_ia32_ls_mode(node);
321         assert(mode != NULL);
322         be_emit_char(get_xmm_mode_suffix(mode));
323 }
324
325 void ia32_emit_extend_suffix(const ir_mode *mode)
326 {
327         if(get_mode_size_bits(mode) == 32)
328                 return;
329         if(mode_is_signed(mode)) {
330                 be_emit_char('s');
331         } else {
332                 be_emit_char('z');
333         }
334 }
335
336 static
337 void ia32_emit_function_object(const char *name)
338 {
339         switch (be_gas_flavour) {
340         case GAS_FLAVOUR_NORMAL:
341                 be_emit_cstring("\t.type\t");
342                 be_emit_string(name);
343                 be_emit_cstring(", @function\n");
344                 be_emit_write_line();
345                 break;
346         case GAS_FLAVOUR_MINGW:
347                 be_emit_cstring("\t.def\t");
348                 be_emit_string(name);
349                 be_emit_cstring(";\t.scl\t2;\t.type\t32;\t.endef\n");
350                 be_emit_write_line();
351                 break;
352         default:
353                 break;
354         }
355 }
356
357 static
358 void ia32_emit_function_size(const char *name)
359 {
360         switch (be_gas_flavour) {
361         case GAS_FLAVOUR_NORMAL:
362                 be_emit_cstring("\t.size\t");
363                 be_emit_string(name);
364                 be_emit_cstring(", .-");
365                 be_emit_string(name);
366                 be_emit_char('\n');
367                 be_emit_write_line();
368                 break;
369         default:
370                 break;
371         }
372 }
373
374
375 void ia32_emit_source_register_or_immediate(const ir_node *node, int pos)
376 {
377         ir_node *in = get_irn_n(node, pos);
378         if(is_ia32_Immediate(in)) {
379                 emit_ia32_Immediate(in);
380         } else {
381                 const ir_mode         *mode = get_ia32_ls_mode(node);
382                 const arch_register_t *reg  = get_in_reg(node, pos);
383                 emit_register(reg, mode);
384         }
385 }
386
387 /**
388  * Emits registers and/or address mode of a binary operation.
389  */
390 void ia32_emit_binop(const ir_node *node) {
391         const ir_node         *right_op  = get_irn_n(node, n_ia32_binary_right);
392         const ir_mode         *mode      = get_ia32_ls_mode(node);
393         const arch_register_t *reg_left;
394
395         switch(get_ia32_op_type(node)) {
396         case ia32_Normal:
397                 reg_left = get_in_reg(node, n_ia32_binary_left);
398                 if(is_ia32_Immediate(right_op)) {
399                         emit_ia32_Immediate(right_op);
400                         be_emit_cstring(", ");
401                         emit_register(reg_left, mode);
402                         break;
403                 } else {
404                         const arch_register_t *reg_right
405                                 = get_in_reg(node, n_ia32_binary_right);
406                         emit_register(reg_right, mode);
407                         be_emit_cstring(", ");
408                         emit_register(reg_left, mode);
409                 }
410                 break;
411         case ia32_AddrModeS:
412                 if(is_ia32_Immediate(right_op)) {
413                         emit_ia32_Immediate(right_op);
414                         be_emit_cstring(", ");
415                         ia32_emit_am(node);
416                 } else {
417                         reg_left = get_in_reg(node, n_ia32_binary_left);
418                         ia32_emit_am(node);
419                         be_emit_cstring(", ");
420                         emit_register(reg_left, mode);
421                 }
422                 break;
423         case ia32_AddrModeD:
424                 panic("DestMode can't be output by %%binop anymore");
425                 break;
426         default:
427                 assert(0 && "unsupported op type");
428         }
429 }
430
431 /**
432  * Emits registers and/or address mode of a binary operation.
433  */
434 void ia32_emit_x87_binop(const ir_node *node) {
435         switch(get_ia32_op_type(node)) {
436                 case ia32_Normal:
437                         {
438                                 const ia32_x87_attr_t *x87_attr = get_ia32_x87_attr_const(node);
439                                 const arch_register_t *in1      = x87_attr->x87[0];
440                                 const arch_register_t *in2      = x87_attr->x87[1];
441                                 const arch_register_t *out      = x87_attr->x87[2];
442                                 const arch_register_t *in;
443
444                                 in  = out ? ((out == in2) ? in1 : in2) : in2;
445                                 out = out ? out : in1;
446
447                                 be_emit_char('%');
448                                 be_emit_string(arch_register_get_name(in));
449                                 be_emit_cstring(", %");
450                                 be_emit_string(arch_register_get_name(out));
451                         }
452                         break;
453                 case ia32_AddrModeS:
454                         ia32_emit_am(node);
455                         break;
456                 case ia32_AddrModeD:
457                 default:
458                         assert(0 && "unsupported op type");
459         }
460 }
461
462 void ia32_emit_am_or_dest_register(const ir_node *node,
463                                    int pos) {
464         if(get_ia32_op_type(node) == ia32_Normal) {
465                 ia32_emit_dest_register(node, pos);
466         } else {
467                 assert(get_ia32_op_type(node) == ia32_AddrModeD);
468                 ia32_emit_am(node);
469         }
470 }
471
472 /**
473  * Emits registers and/or address mode of a unary operation.
474  */
475 void ia32_emit_unop(const ir_node *node, int pos) {
476         const ir_node *op;
477
478         switch(get_ia32_op_type(node)) {
479         case ia32_Normal:
480                 op = get_irn_n(node, pos);
481                 if (is_ia32_Immediate(op)) {
482                         emit_ia32_Immediate(op);
483                 } else {
484                         ia32_emit_source_register(node, pos);
485                 }
486                 break;
487         case ia32_AddrModeS:
488         case ia32_AddrModeD:
489                 ia32_emit_am(node);
490                 break;
491         default:
492                 assert(0 && "unsupported op type");
493         }
494 }
495
496 /**
497  * Emits address mode.
498  */
499 void ia32_emit_am(const ir_node *node) {
500         ir_entity *ent       = get_ia32_am_sc(node);
501         int        offs      = get_ia32_am_offs_int(node);
502         ir_node   *base      = get_irn_n(node, 0);
503         int        has_base  = !is_ia32_NoReg_GP(base);
504         ir_node   *index     = get_irn_n(node, 1);
505         int        has_index = !is_ia32_NoReg_GP(index);
506
507         /* just to be sure... */
508         assert(!is_ia32_use_frame(node) || get_ia32_frame_ent(node) != NULL);
509
510         /* emit offset */
511         if (ent != NULL) {
512                 ident *id;
513
514                 set_entity_backend_marked(ent, 1);
515                 id = get_entity_ld_ident(ent);
516                 if (is_ia32_am_sc_sign(node))
517                         be_emit_char('-');
518                 be_emit_ident(id);
519
520                 if(get_entity_owner(ent) == get_tls_type()) {
521                         if (get_entity_visibility(ent) == visibility_external_allocated) {
522                                 be_emit_cstring("@INDNTPOFF");
523                         } else {
524                                 be_emit_cstring("@NTPOFF");
525                         }
526                 }
527         }
528
529         if(offs != 0) {
530                 if(ent != NULL) {
531                         be_emit_irprintf("%+d", offs);
532                 } else {
533                         be_emit_irprintf("%d", offs);
534                 }
535         }
536
537         if (has_base || has_index) {
538                 be_emit_char('(');
539
540                 /* emit base */
541                 if (has_base) {
542                         const arch_register_t *reg = get_in_reg(node, n_ia32_base);
543                         emit_register(reg, NULL);
544                 }
545
546                 /* emit index + scale */
547                 if (has_index) {
548                         const arch_register_t *reg = get_in_reg(node, n_ia32_index);
549                         int scale;
550                         be_emit_char(',');
551                         emit_register(reg, NULL);
552
553                         scale = get_ia32_am_scale(node);
554                         if (scale > 0) {
555                                 be_emit_irprintf(",%d", 1 << get_ia32_am_scale(node));
556                         }
557                 }
558                 be_emit_char(')');
559         }
560
561         /* special case if nothing is set */
562         if(ent == NULL && offs == 0 && !has_base && !has_index) {
563                 be_emit_char('0');
564         }
565 }
566
567 static void emit_ia32_IMul(const ir_node *node)
568 {
569         ir_node               *left    = get_irn_n(node, n_ia32_IMul_left);
570         const arch_register_t *out_reg = get_out_reg(node, pn_ia32_IMul_res);
571
572         be_emit_cstring("\timul");
573         ia32_emit_mode_suffix(node);
574         be_emit_char(' ');
575
576         ia32_emit_binop(node);
577
578         /* do we need the 3-address form? */
579         if(is_ia32_NoReg_GP(left) ||
580                         get_in_reg(node, n_ia32_IMul_left) != out_reg) {
581                 be_emit_cstring(", ");
582                 emit_register(out_reg, get_ia32_ls_mode(node));
583         }
584         be_emit_finish_line_gas(node);
585 }
586
587 /*************************************************
588  *                 _ _                         _
589  *                (_) |                       | |
590  *   ___ _ __ ___  _| |_    ___ ___  _ __   __| |
591  *  / _ \ '_ ` _ \| | __|  / __/ _ \| '_ \ / _` |
592  * |  __/ | | | | | | |_  | (_| (_) | | | | (_| |
593  *  \___|_| |_| |_|_|\__|  \___\___/|_| |_|\__,_|
594  *
595  *************************************************/
596
597 #undef IA32_DO_EMIT
598 #define IA32_DO_EMIT(irn) ia32_fprintf_format(F, irn, cmd_buf, cmnt_buf)
599
600 /*
601  * coding of conditions
602  */
603 struct cmp2conditon_t {
604         const char *name;
605         int         num;
606 };
607
608 /*
609  * positive conditions for signed compares
610  */
611 static const struct cmp2conditon_t cmp2condition_s[] = {
612         { NULL,              pn_Cmp_False },  /* always false */
613         { "e",               pn_Cmp_Eq },     /* == */
614         { "l",               pn_Cmp_Lt },     /* < */
615         { "le",              pn_Cmp_Le },     /* <= */
616         { "g",               pn_Cmp_Gt },     /* > */
617         { "ge",              pn_Cmp_Ge },     /* >= */
618         { "ne",              pn_Cmp_Lg },     /* != */
619         { NULL,              pn_Cmp_Leg},     /* always true */
620 };
621
622 /*
623  * positive conditions for unsigned compares
624  */
625 static const struct cmp2conditon_t cmp2condition_u[] = {
626         { NULL,              pn_Cmp_False },  /* always false */
627         { "e",               pn_Cmp_Eq },     /* == */
628         { "b",               pn_Cmp_Lt },     /* < */
629         { "be",              pn_Cmp_Le },     /* <= */
630         { "a",               pn_Cmp_Gt },     /* > */
631         { "ae",              pn_Cmp_Ge },     /* >= */
632         { "ne",              pn_Cmp_Lg },     /* != */
633         { NULL,              pn_Cmp_Leg },   /* always true  */
634 };
635
636 enum {
637         ia32_pn_Cmp_unsigned = 0x1000,
638         ia32_pn_Cmp_float    = 0x2000,
639 };
640
641 /**
642  * walks up a tree of copies/perms/spills/reloads to find the original value
643  * that is moved around
644  */
645 static ir_node *find_original_value(ir_node *node)
646 {
647         inc_irg_visited(current_ir_graph);
648         while(1) {
649                 mark_irn_visited(node);
650                 if(be_is_Copy(node)) {
651                         node = be_get_Copy_op(node);
652                 } else if(be_is_CopyKeep(node)) {
653                         node = be_get_CopyKeep_op(node);
654                 } else if(is_Proj(node)) {
655                         ir_node *pred = get_Proj_pred(node);
656                         if(be_is_Perm(pred)) {
657                                 node = get_irn_n(pred, get_Proj_proj(node));
658                         } else if(be_is_MemPerm(pred)) {
659                                 node = get_irn_n(pred, get_Proj_proj(node) + 1);
660                         } else if(is_ia32_Load(pred)) {
661                                 node = get_irn_n(pred, n_ia32_Load_mem);
662                         } else {
663                                 return node;
664                         }
665                 } else if(is_ia32_Store(node)) {
666                         node = get_irn_n(node, n_ia32_Store_val);
667                 } else if(is_Phi(node)) {
668                         int i, arity;
669                         arity = get_irn_arity(node);
670                         for(i = 0; i < arity; ++i) {
671                                 ir_node *in = get_irn_n(node, i);
672                                 if(irn_visited(in))
673                                         continue;
674                                 node = in;
675                                 break;
676                         }
677                         assert(i < arity);
678                 } else {
679                         return node;
680                 }
681         }
682 }
683
684 static int determine_final_pnc(const ir_node *node, int flags_pos,
685                                int pnc)
686 {
687         ir_node           *flags = get_irn_n(node, flags_pos);
688         const ia32_attr_t *flags_attr;
689         flags = skip_Proj(flags);
690
691         if(is_ia32_Sahf(flags)) {
692                 ir_node *cmp = get_irn_n(flags, n_ia32_Sahf_val);
693                 if(!(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
694                                 || is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp))) {
695                         cmp = find_original_value(cmp);
696                         assert(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
697                                || is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp));
698                 }
699
700                 flags_attr = get_ia32_attr_const(cmp);
701                 if(flags_attr->data.ins_permuted)
702                         pnc = get_mirrored_pnc(pnc);
703                 pnc |= ia32_pn_Cmp_float;
704         } else if(is_ia32_Ucomi(flags) || is_ia32_Fucomi(flags)
705                         || is_ia32_Fucompi(flags)) {
706                 flags_attr = get_ia32_attr_const(flags);
707
708                 if(flags_attr->data.ins_permuted)
709                         pnc = get_mirrored_pnc(pnc);
710                 pnc |= ia32_pn_Cmp_float;
711         } else {
712                 assert(is_ia32_Cmp(flags) || is_ia32_Test(flags)
713                                 || is_ia32_Cmp8Bit(flags) || is_ia32_Test8Bit(flags));
714                 flags_attr = get_ia32_attr_const(flags);
715
716                 if(flags_attr->data.ins_permuted)
717                         pnc = get_mirrored_pnc(pnc);
718                 if(flags_attr->data.cmp_unsigned)
719                         pnc |= ia32_pn_Cmp_unsigned;
720         }
721
722         return pnc;
723 }
724
725 static void ia32_emit_cmp_suffix(int pnc)
726 {
727         const char        *str;
728
729         if((pnc & ia32_pn_Cmp_float) || (pnc & ia32_pn_Cmp_unsigned)) {
730                 pnc = pnc & 7;
731                 assert(cmp2condition_u[pnc].num == pnc);
732                 str = cmp2condition_u[pnc].name;
733         } else {
734                 pnc = pnc & 7;
735                 assert(cmp2condition_s[pnc].num == pnc);
736                 str = cmp2condition_s[pnc].name;
737         }
738
739         be_emit_string(str);
740 }
741
742 void ia32_emit_cmp_suffix_node(const ir_node *node,
743                                int flags_pos)
744 {
745         const ia32_attr_t *attr = get_ia32_attr_const(node);
746
747         pn_Cmp pnc = get_ia32_condcode(node);
748
749         pnc = determine_final_pnc(node, flags_pos, pnc);
750         if(attr->data.ins_permuted) {
751                 if(pnc & ia32_pn_Cmp_float) {
752                         pnc = get_negated_pnc(pnc, mode_F);
753                 } else {
754                         pnc = get_negated_pnc(pnc, mode_Iu);
755                 }
756         }
757
758         ia32_emit_cmp_suffix(pnc);
759 }
760
761 /**
762  * Returns the target block for a control flow node.
763  */
764 static
765 ir_node *get_cfop_target_block(const ir_node *irn) {
766         return get_irn_link(irn);
767 }
768
769 /**
770  * Emits a block label for the given block.
771  */
772 static
773 void ia32_emit_block_name(const ir_node *block)
774 {
775         if (has_Block_label(block)) {
776                 be_emit_string(be_gas_label_prefix());
777                 be_emit_irprintf("%u", (unsigned)get_Block_label(block));
778         } else {
779                 be_emit_cstring(BLOCK_PREFIX);
780                 be_emit_irprintf("%d", get_irn_node_nr(block));
781         }
782 }
783
784 /**
785  * Emits the target label for a control flow node.
786  */
787 static void ia32_emit_cfop_target(const ir_node *node)
788 {
789         ir_node *block = get_cfop_target_block(node);
790
791         ia32_emit_block_name(block);
792 }
793
794 /** Return the next block in Block schedule */
795 static ir_node *next_blk_sched(const ir_node *block)
796 {
797         return get_irn_link(block);
798 }
799
800 /**
801  * Returns the Proj with projection number proj and NOT mode_M
802  */
803 static ir_node *get_proj(const ir_node *node, long proj) {
804         const ir_edge_t *edge;
805         ir_node         *src;
806
807         assert(get_irn_mode(node) == mode_T && "expected mode_T node");
808
809         foreach_out_edge(node, edge) {
810                 src = get_edge_src_irn(edge);
811
812                 assert(is_Proj(src) && "Proj expected");
813                 if (get_irn_mode(src) == mode_M)
814                         continue;
815
816                 if (get_Proj_proj(src) == proj)
817                         return src;
818         }
819         return NULL;
820 }
821
822 /**
823  * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
824  */
825 static void emit_ia32_Jcc(const ir_node *node)
826 {
827         const ir_node *proj_true;
828         const ir_node *proj_false;
829         const ir_node *block;
830         const ir_node *next_block;
831         pn_Cmp         pnc = get_ia32_condcode(node);
832
833         pnc = determine_final_pnc(node, 0, pnc);
834
835         /* get both Projs */
836         proj_true = get_proj(node, pn_ia32_Jcc_true);
837         assert(proj_true && "Jcc without true Proj");
838
839         proj_false = get_proj(node, pn_ia32_Jcc_false);
840         assert(proj_false && "Jcc without false Proj");
841
842         block      = get_nodes_block(node);
843         next_block = next_blk_sched(block);
844
845         if (get_cfop_target_block(proj_true) == next_block) {
846                 /* exchange both proj's so the second one can be omitted */
847                 const ir_node *t = proj_true;
848
849                 proj_true  = proj_false;
850                 proj_false = t;
851                 if(pnc & ia32_pn_Cmp_float) {
852                         pnc = get_negated_pnc(pnc, mode_F);
853                 } else {
854                         pnc = get_negated_pnc(pnc, mode_Iu);
855                 }
856         }
857
858         if (pnc & ia32_pn_Cmp_float) {
859                 /* Some floating point comparisons require a test of the parity flag,
860                  * which indicates that the result is unordered */
861                 switch (pnc & 15) {
862                         case pn_Cmp_Uo:
863                                 be_emit_cstring("\tjp ");
864                                 ia32_emit_cfop_target(proj_true);
865                                 be_emit_finish_line_gas(proj_true);
866                                 break;
867
868                         case pn_Cmp_Leg:
869                                 be_emit_cstring("\tjnp ");
870                                 ia32_emit_cfop_target(proj_true);
871                                 be_emit_finish_line_gas(proj_true);
872                                 break;
873
874                         case pn_Cmp_Eq:
875                         case pn_Cmp_Lt:
876                         case pn_Cmp_Le:
877                                 be_emit_cstring("\tjp ");
878                                 ia32_emit_cfop_target(proj_false);
879                                 be_emit_finish_line_gas(proj_false);
880                                 goto emit_jcc;
881
882                         case pn_Cmp_Ug:
883                         case pn_Cmp_Uge:
884                         case pn_Cmp_Ne:
885                                 be_emit_cstring("\tjp ");
886                                 ia32_emit_cfop_target(proj_true);
887                                 be_emit_finish_line_gas(proj_true);
888                                 goto emit_jcc;
889
890                         default:
891                                 goto emit_jcc;
892                 }
893         } else {
894 emit_jcc:
895                 be_emit_cstring("\tj");
896                 ia32_emit_cmp_suffix(pnc);
897                 be_emit_char(' ');
898                 ia32_emit_cfop_target(proj_true);
899                 be_emit_finish_line_gas(proj_true);
900         }
901
902         /* the second Proj might be a fallthrough */
903         if (get_cfop_target_block(proj_false) != next_block) {
904                 be_emit_cstring("\tjmp ");
905                 ia32_emit_cfop_target(proj_false);
906                 be_emit_finish_line_gas(proj_false);
907         } else {
908                 be_emit_cstring("\t/* fallthrough to ");
909                 ia32_emit_cfop_target(proj_false);
910                 be_emit_cstring(" */");
911                 be_emit_finish_line_gas(proj_false);
912         }
913 }
914
915 static void emit_ia32_CMov(const ir_node *node)
916 {
917         const ia32_attr_t     *attr         = get_ia32_attr_const(node);
918         int                    ins_permuted = attr->data.ins_permuted;
919         const arch_register_t *out          = arch_get_irn_register(arch_env, node);
920         pn_Cmp                 pnc          = get_ia32_condcode(node);
921         const arch_register_t *in_true;
922         const arch_register_t *in_false;
923
924         pnc = determine_final_pnc(node, n_ia32_CMov_eflags, pnc);
925
926         in_true  = arch_get_irn_register(arch_env,
927                                          get_irn_n(node, n_ia32_CMov_val_true));
928         in_false = arch_get_irn_register(arch_env,
929                                          get_irn_n(node, n_ia32_CMov_val_false));
930
931         /* should be same constraint fullfilled? */
932         if(out == in_false) {
933                 /* yes -> nothing to do */
934         } else if(out == in_true) {
935                 const arch_register_t *tmp;
936
937                 assert(get_ia32_op_type(node) == ia32_Normal);
938
939                 ins_permuted = !ins_permuted;
940
941                 tmp      = in_true;
942                 in_true  = in_false;
943                 in_false = tmp;
944         } else {
945                 /* we need a mov */
946                 be_emit_cstring("\tmovl ");
947                 emit_register(in_false, NULL);
948                 be_emit_cstring(", ");
949                 emit_register(out, NULL);
950                 be_emit_finish_line_gas(node);
951         }
952
953         if(ins_permuted) {
954                 if(pnc & ia32_pn_Cmp_float) {
955                         pnc = get_negated_pnc(pnc, mode_F);
956                 } else {
957                         pnc = get_negated_pnc(pnc, mode_Iu);
958                 }
959         }
960
961         /* TODO: handling of Nans isn't correct yet */
962
963         be_emit_cstring("\tcmov");
964         ia32_emit_cmp_suffix(pnc);
965         be_emit_char(' ');
966         if(get_ia32_op_type(node) == ia32_AddrModeS) {
967                 ia32_emit_am(node);
968         } else {
969                 emit_register(in_true, get_ia32_ls_mode(node));
970         }
971         be_emit_cstring(", ");
972         emit_register(out, get_ia32_ls_mode(node));
973         be_emit_finish_line_gas(node);
974 }
975
976 /*********************************************************
977  *                 _ _       _
978  *                (_) |     (_)
979  *   ___ _ __ ___  _| |_     _ _   _ _ __ ___  _ __  ___
980  *  / _ \ '_ ` _ \| | __|   | | | | | '_ ` _ \| '_ \/ __|
981  * |  __/ | | | | | | |_    | | |_| | | | | | | |_) \__ \
982  *  \___|_| |_| |_|_|\__|   | |\__,_|_| |_| |_| .__/|___/
983  *                         _/ |               | |
984  *                        |__/                |_|
985  *********************************************************/
986
987 /* jump table entry (target and corresponding number) */
988 typedef struct _branch_t {
989         ir_node *target;
990         int      value;
991 } branch_t;
992
993 /* jump table for switch generation */
994 typedef struct _jmp_tbl_t {
995         ir_node  *defProj;         /**< default target */
996         long      min_value;       /**< smallest switch case */
997         long      max_value;       /**< largest switch case */
998         long      num_branches;    /**< number of jumps */
999         char     *label;           /**< label of the jump table */
1000         branch_t *branches;        /**< jump array */
1001 } jmp_tbl_t;
1002
1003 /**
1004  * Compare two variables of type branch_t. Used to sort all switch cases
1005  */
1006 static
1007 int ia32_cmp_branch_t(const void *a, const void *b) {
1008         branch_t *b1 = (branch_t *)a;
1009         branch_t *b2 = (branch_t *)b;
1010
1011         if (b1->value <= b2->value)
1012                 return -1;
1013         else
1014                 return 1;
1015 }
1016
1017 /**
1018  * Emits code for a SwitchJmp (creates a jump table if
1019  * possible otherwise a cmp-jmp cascade). Port from
1020  * cggg ia32 backend
1021  */
1022 static
1023 void emit_ia32_SwitchJmp(const ir_node *node) {
1024         unsigned long       interval;
1025         int                 last_value, i;
1026         long                pnc;
1027         jmp_tbl_t           tbl;
1028         ir_node            *proj;
1029         const ir_edge_t    *edge;
1030
1031         /* fill the table structure */
1032         tbl.label        = xmalloc(SNPRINTF_BUF_LEN);
1033         tbl.label        = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, ".TBL_");
1034         tbl.defProj      = NULL;
1035         tbl.num_branches = get_irn_n_edges(node);
1036         tbl.branches     = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
1037         tbl.min_value    = INT_MAX;
1038         tbl.max_value    = INT_MIN;
1039
1040         i = 0;
1041         /* go over all proj's and collect them */
1042         foreach_out_edge(node, edge) {
1043                 proj = get_edge_src_irn(edge);
1044                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
1045
1046                 pnc = get_Proj_proj(proj);
1047
1048                 /* create branch entry */
1049                 tbl.branches[i].target = proj;
1050                 tbl.branches[i].value  = pnc;
1051
1052                 tbl.min_value = pnc < tbl.min_value ? pnc : tbl.min_value;
1053                 tbl.max_value = pnc > tbl.max_value ? pnc : tbl.max_value;
1054
1055                 /* check for default proj */
1056                 if (pnc == get_ia32_condcode(node)) {
1057                         assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
1058                         tbl.defProj = proj;
1059                 }
1060
1061                 i++;
1062         }
1063
1064         /* sort the branches by their number */
1065         qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
1066
1067         /* two-complement's magic make this work without overflow */
1068         interval = tbl.max_value - tbl.min_value;
1069
1070         /* emit the table */
1071         be_emit_cstring("\tcmpl $");
1072         be_emit_irprintf("%u, ", interval);
1073         ia32_emit_source_register(node, 0);
1074         be_emit_finish_line_gas(node);
1075
1076         be_emit_cstring("\tja ");
1077         ia32_emit_cfop_target(tbl.defProj);
1078         be_emit_finish_line_gas(node);
1079
1080         if (tbl.num_branches > 1) {
1081                 /* create table */
1082                 be_emit_cstring("\tjmp *");
1083                 be_emit_string(tbl.label);
1084                 be_emit_cstring("(,");
1085                 ia32_emit_source_register(node, 0);
1086                 be_emit_cstring(",4)");
1087                 be_emit_finish_line_gas(node);
1088
1089                 be_gas_emit_switch_section(GAS_SECTION_RODATA);
1090                 be_emit_cstring("\t.align 4\n");
1091                 be_emit_write_line();
1092
1093                 be_emit_string(tbl.label);
1094                 be_emit_cstring(":\n");
1095                 be_emit_write_line();
1096
1097                 be_emit_cstring(".long ");
1098                 ia32_emit_cfop_target(tbl.branches[0].target);
1099                 be_emit_finish_line_gas(NULL);
1100
1101                 last_value = tbl.branches[0].value;
1102                 for (i = 1; i < tbl.num_branches; ++i) {
1103                         while (++last_value < tbl.branches[i].value) {
1104                                 be_emit_cstring(".long ");
1105                                 ia32_emit_cfop_target(tbl.defProj);
1106                                 be_emit_finish_line_gas(NULL);
1107                         }
1108                         be_emit_cstring(".long ");
1109                         ia32_emit_cfop_target(tbl.branches[i].target);
1110                         be_emit_finish_line_gas(NULL);
1111                 }
1112                 be_gas_emit_switch_section(GAS_SECTION_TEXT);
1113         } else {
1114                 /* one jump is enough */
1115                 be_emit_cstring("\tjmp ");
1116                 ia32_emit_cfop_target(tbl.branches[0].target);
1117                 be_emit_finish_line_gas(node);
1118         }
1119
1120         if (tbl.label)
1121                 free(tbl.label);
1122         if (tbl.branches)
1123                 free(tbl.branches);
1124 }
1125
1126 /**
1127  * Emits code for a unconditional jump.
1128  */
1129 static void emit_Jmp(const ir_node *node)
1130 {
1131         ir_node *block, *next_block;
1132
1133         /* for now, the code works for scheduled and non-schedules blocks */
1134         block = get_nodes_block(node);
1135
1136         /* we have a block schedule */
1137         next_block = next_blk_sched(block);
1138         if (get_cfop_target_block(node) != next_block) {
1139                 be_emit_cstring("\tjmp ");
1140                 ia32_emit_cfop_target(node);
1141         } else {
1142                 be_emit_cstring("\t/* fallthrough to ");
1143                 ia32_emit_cfop_target(node);
1144                 be_emit_cstring(" */");
1145         }
1146         be_emit_finish_line_gas(node);
1147 }
1148
1149 static void emit_ia32_Immediate(const ir_node *node)
1150 {
1151         const ia32_immediate_attr_t *attr = get_ia32_immediate_attr_const(node);
1152
1153         be_emit_char('$');
1154         if(attr->symconst != NULL) {
1155                 ident *id = get_entity_ld_ident(attr->symconst);
1156
1157                 if(attr->sc_sign)
1158                         be_emit_char('-');
1159                 be_emit_ident(id);
1160         }
1161         if(attr->symconst == NULL || attr->offset != 0) {
1162                 if(attr->symconst != NULL) {
1163                         be_emit_irprintf("%+d", attr->offset);
1164                 } else {
1165                         be_emit_irprintf("0x%X", attr->offset);
1166                 }
1167         }
1168 }
1169
1170 /**
1171  * Emit an inline assembler operand.
1172  *
1173  * @param node  the ia32_ASM node
1174  * @param s     points to the operand (a %c)
1175  *
1176  * @return  pointer to the first char in s NOT in the current operand
1177  */
1178 static const char* emit_asm_operand(const ir_node *node, const char *s)
1179 {
1180         const ia32_attr_t     *ia32_attr = get_ia32_attr_const(node);
1181         const ia32_asm_attr_t *attr      = CONST_CAST_IA32_ATTR(ia32_asm_attr_t,
1182                                                             ia32_attr);
1183         const arch_register_t *reg;
1184         const ia32_asm_reg_t  *asm_regs = attr->register_map;
1185         const ia32_asm_reg_t  *asm_reg;
1186         const char            *reg_name;
1187         char                   c;
1188         char                   modifier = 0;
1189         int                    num      = -1;
1190         int                    p;
1191
1192         assert(*s == '%');
1193         c = *(++s);
1194
1195         /* parse modifiers */
1196         switch(c) {
1197         case 0:
1198                 ir_fprintf(stderr, "Warning: asm text (%+F) ends with %\n", node);
1199                 be_emit_char('%');
1200                 return s + 1;
1201         case '%':
1202                 be_emit_char('%');
1203                 return s + 1;
1204         case 'w':
1205         case 'b':
1206         case 'h':
1207                 modifier = c;
1208                 ++s;
1209                 break;
1210         case '0':
1211         case '1':
1212         case '2':
1213         case '3':
1214         case '4':
1215         case '5':
1216         case '6':
1217         case '7':
1218         case '8':
1219         case '9':
1220                 break;
1221         default:
1222                 ir_fprintf(stderr, "Warning: asm text (%+F) contains unknown modifier "
1223                            "'%c' for asm op\n", node, c);
1224                 ++s;
1225                 break;
1226         }
1227
1228         /* parse number */
1229         sscanf(s, "%d%n", &num, &p);
1230         if(num < 0) {
1231                 ir_fprintf(stderr, "Warning: Couldn't parse assembler operand (%+F)\n",
1232                            node);
1233                 return s;
1234         } else {
1235                 s += p;
1236         }
1237
1238         if(num < 0 || num >= ARR_LEN(asm_regs)) {
1239                 ir_fprintf(stderr, "Error: Custom assembler references invalid "
1240                            "input/output (%+F)\n", node);
1241                 return s;
1242         }
1243         asm_reg = & asm_regs[num];
1244         assert(asm_reg->valid);
1245
1246         /* get register */
1247         if(asm_reg->use_input == 0) {
1248                 reg = get_out_reg(node, asm_reg->inout_pos);
1249         } else {
1250                 ir_node *pred = get_irn_n(node, asm_reg->inout_pos);
1251
1252                 /* might be an immediate value */
1253                 if(is_ia32_Immediate(pred)) {
1254                         emit_ia32_Immediate(pred);
1255                         return s;
1256                 }
1257                 reg = get_in_reg(node, asm_reg->inout_pos);
1258         }
1259         if(reg == NULL) {
1260                 ir_fprintf(stderr, "Warning: no register assigned for %d asm op "
1261                            "(%+F)\n", num, node);
1262                 return s;
1263         }
1264
1265         if(asm_reg->memory) {
1266                 be_emit_char('(');
1267         }
1268
1269         /* emit it */
1270         if(modifier != 0) {
1271                 be_emit_char('%');
1272                 switch(modifier) {
1273                 case 'b':
1274                         reg_name = ia32_get_mapped_reg_name(isa->regs_8bit, reg);
1275                         break;
1276                 case 'h':
1277                         reg_name = ia32_get_mapped_reg_name(isa->regs_8bit_high, reg);
1278                         break;
1279                 case 'w':
1280                         reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
1281                         break;
1282                 default:
1283                         panic("Invalid asm op modifier");
1284                 }
1285                 be_emit_string(reg_name);
1286         } else {
1287                 emit_register(reg, asm_reg->mode);
1288         }
1289
1290         if(asm_reg->memory) {
1291                 be_emit_char(')');
1292         }
1293
1294         return s;
1295 }
1296
1297 /**
1298  * Emits code for an ASM pseudo op.
1299  */
1300 static void emit_ia32_Asm(const ir_node *node)
1301 {
1302         const void            *gen_attr = get_irn_generic_attr_const(node);
1303         const ia32_asm_attr_t *attr
1304                 = CONST_CAST_IA32_ATTR(ia32_asm_attr_t, gen_attr);
1305         ident                 *asm_text = attr->asm_text;
1306         const char            *s        = get_id_str(asm_text);
1307
1308         be_emit_cstring("# Begin ASM \t");
1309         be_emit_finish_line_gas(node);
1310
1311         if (s[0] != '\t')
1312                 be_emit_char('\t');
1313
1314         while(*s != 0) {
1315                 if(*s == '%') {
1316                         s = emit_asm_operand(node, s);
1317                         continue;
1318                 } else {
1319                         be_emit_char(*s);
1320                 }
1321                 ++s;
1322         }
1323
1324         be_emit_char('\n');
1325         be_emit_write_line();
1326
1327         be_emit_cstring("# End ASM\n");
1328         be_emit_write_line();
1329 }
1330
1331 /**********************************
1332  *   _____                  ____
1333  *  / ____|                |  _ \
1334  * | |     ___  _ __  _   _| |_) |
1335  * | |    / _ \| '_ \| | | |  _ <
1336  * | |___| (_) | |_) | |_| | |_) |
1337  *  \_____\___/| .__/ \__, |____/
1338  *             | |     __/ |
1339  *             |_|    |___/
1340  **********************************/
1341
1342 /**
1343  * Emit movsb/w instructions to make mov count divideable by 4
1344  */
1345 static void emit_CopyB_prolog(unsigned size) {
1346         be_emit_cstring("\tcld");
1347         be_emit_finish_line_gas(NULL);
1348
1349         switch (size) {
1350         case 1:
1351                 be_emit_cstring("\tmovsb");
1352                 be_emit_finish_line_gas(NULL);
1353                 break;
1354         case 2:
1355                 be_emit_cstring("\tmovsw");
1356                 be_emit_finish_line_gas(NULL);
1357                 break;
1358         case 3:
1359                 be_emit_cstring("\tmovsb");
1360                 be_emit_finish_line_gas(NULL);
1361                 be_emit_cstring("\tmovsw");
1362                 be_emit_finish_line_gas(NULL);
1363                 break;
1364         }
1365 }
1366
1367 /**
1368  * Emit rep movsd instruction for memcopy.
1369  */
1370 static void emit_ia32_CopyB(const ir_node *node)
1371 {
1372         unsigned size = get_ia32_copyb_size(node);
1373
1374         emit_CopyB_prolog(size);
1375
1376         be_emit_cstring("\trep movsd");
1377         be_emit_finish_line_gas(node);
1378 }
1379
1380 /**
1381  * Emits unrolled memcopy.
1382  */
1383 static void emit_ia32_CopyB_i(const ir_node *node)
1384 {
1385         unsigned size = get_ia32_copyb_size(node);
1386
1387         emit_CopyB_prolog(size & 0x3);
1388
1389         size >>= 2;
1390         while (size--) {
1391                 be_emit_cstring("\tmovsd");
1392                 be_emit_finish_line_gas(NULL);
1393         }
1394 }
1395
1396
1397
1398 /***************************
1399  *   _____
1400  *  / ____|
1401  * | |     ___  _ ____   __
1402  * | |    / _ \| '_ \ \ / /
1403  * | |___| (_) | | | \ V /
1404  *  \_____\___/|_| |_|\_/
1405  *
1406  ***************************/
1407
1408 /**
1409  * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1410  */
1411 static void emit_ia32_Conv_with_FP(const ir_node *node)
1412 {
1413         ir_mode            *ls_mode = get_ia32_ls_mode(node);
1414         int                 ls_bits = get_mode_size_bits(ls_mode);
1415
1416         be_emit_cstring("\tcvt");
1417
1418         if(is_ia32_Conv_I2FP(node)) {
1419                 if(ls_bits == 32) {
1420                         be_emit_cstring("si2ss");
1421                 } else {
1422                         be_emit_cstring("si2sd");
1423                 }
1424         } else if(is_ia32_Conv_FP2I(node)) {
1425                 if(ls_bits == 32) {
1426                         be_emit_cstring("ss2si");
1427                 } else {
1428                         be_emit_cstring("sd2si");
1429                 }
1430         } else {
1431                 assert(is_ia32_Conv_FP2FP(node));
1432                 if(ls_bits == 32) {
1433                         be_emit_cstring("sd2ss");
1434                 } else {
1435                         be_emit_cstring("ss2sd");
1436                 }
1437         }
1438         be_emit_char(' ');
1439
1440         switch(get_ia32_op_type(node)) {
1441                 case ia32_Normal:
1442                         ia32_emit_source_register(node, n_ia32_unary_op);
1443                         break;
1444                 case ia32_AddrModeS:
1445                         ia32_emit_am(node);
1446                         break;
1447                 default:
1448                         assert(0 && "unsupported op type for Conv");
1449         }
1450         be_emit_cstring(", ");
1451         ia32_emit_dest_register(node, 0);
1452         be_emit_finish_line_gas(node);
1453 }
1454
1455 static void emit_ia32_Conv_I2FP(const ir_node *node)
1456 {
1457         emit_ia32_Conv_with_FP(node);
1458 }
1459
1460 static void emit_ia32_Conv_FP2I(const ir_node *node)
1461 {
1462         emit_ia32_Conv_with_FP(node);
1463 }
1464
1465 static void emit_ia32_Conv_FP2FP(const ir_node *node)
1466 {
1467         emit_ia32_Conv_with_FP(node);
1468 }
1469
1470 /**
1471  * Emits code for an Int conversion.
1472  */
1473 static void emit_ia32_Conv_I2I(const ir_node *node)
1474 {
1475         const char            *sign_suffix;
1476         ir_mode               *smaller_mode = get_ia32_ls_mode(node);
1477         int                    smaller_bits = get_mode_size_bits(smaller_mode);
1478         int                    signed_mode;
1479         const arch_register_t *in_reg, *out_reg;
1480
1481         assert(!mode_is_float(smaller_mode));
1482         assert(smaller_bits == 8 || smaller_bits == 16 || smaller_bits == 32);
1483
1484         signed_mode = mode_is_signed(smaller_mode);
1485         if(smaller_bits == 32) {
1486                 // this should not happen as it's no convert
1487                 assert(0);
1488                 sign_suffix = "";
1489         } else {
1490                 sign_suffix = signed_mode ? "s" : "z";
1491         }
1492
1493         out_reg = get_out_reg(node, 0);
1494
1495         switch(get_ia32_op_type(node)) {
1496                 case ia32_Normal:
1497                         in_reg  = get_in_reg(node, n_ia32_unary_op);
1498
1499                         if (in_reg  == &ia32_gp_regs[REG_EAX] &&
1500                                 out_reg == &ia32_gp_regs[REG_EAX] &&
1501                                 signed_mode &&
1502                                 smaller_bits == 16)
1503                         {
1504                                 /* argument and result are both in EAX and */
1505                                 /* signedness is ok: -> use the smaller cwtl opcode */
1506                                 be_emit_cstring("\tcwtl");
1507                         } else {
1508                                 be_emit_cstring("\tmov");
1509                                 be_emit_string(sign_suffix);
1510                                 ia32_emit_mode_suffix_mode(smaller_mode);
1511                                 be_emit_cstring("l ");
1512                                 emit_register(in_reg, smaller_mode);
1513                                 be_emit_cstring(", ");
1514                                 emit_register(out_reg, NULL);
1515                         }
1516                         break;
1517                 case ia32_AddrModeS: {
1518                         be_emit_cstring("\tmov");
1519                         be_emit_string(sign_suffix);
1520                         ia32_emit_mode_suffix_mode(smaller_mode);
1521                         be_emit_cstring("l ");
1522                         ia32_emit_am(node);
1523                         be_emit_cstring(", ");
1524                         emit_register(out_reg, NULL);
1525                         break;
1526                 }
1527                 default:
1528                         assert(0 && "unsupported op type for Conv");
1529         }
1530         be_emit_finish_line_gas(node);
1531 }
1532
1533
1534 /*******************************************
1535  *  _                          _
1536  * | |                        | |
1537  * | |__   ___ _ __   ___   __| | ___  ___
1538  * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1539  * | |_) |  __/ | | | (_) | (_| |  __/\__ \
1540  * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1541  *
1542  *******************************************/
1543
1544 /**
1545  * Emits a backend call
1546  */
1547 static void emit_be_Call(const ir_node *node)
1548 {
1549         ir_entity *ent = be_Call_get_entity(node);
1550
1551         be_emit_cstring("\tcall ");
1552         if (ent) {
1553                 set_entity_backend_marked(ent, 1);
1554                 be_emit_string(get_entity_ld_name(ent));
1555         } else {
1556                 const arch_register_t *reg = get_in_reg(node, be_pos_Call_ptr);
1557                 be_emit_char('*');
1558                 emit_register(reg, NULL);
1559         }
1560         be_emit_finish_line_gas(node);
1561 }
1562
1563 /**
1564  * Emits code to increase stack pointer.
1565  */
1566 static void emit_be_IncSP(const ir_node *node)
1567 {
1568         int                    offs = be_get_IncSP_offset(node);
1569         const arch_register_t *reg  = arch_get_irn_register(arch_env, node);
1570
1571         if (offs == 0)
1572                 return;
1573
1574         if (offs > 0) {
1575                 be_emit_cstring("\tsubl $");
1576                 be_emit_irprintf("%u, ", offs);
1577                 emit_register(reg, NULL);
1578         } else {
1579                 be_emit_cstring("\taddl $");
1580                 be_emit_irprintf("%u, ", -offs);
1581                 emit_register(reg, NULL);
1582         }
1583         be_emit_finish_line_gas(node);
1584 }
1585
1586 /**
1587  * Emits code for Copy/CopyKeep.
1588  */
1589 static void Copy_emitter(const ir_node *node, const ir_node *op)
1590 {
1591         const arch_register_t *in  = arch_get_irn_register(arch_env, op);
1592         const arch_register_t *out = arch_get_irn_register(arch_env, node);
1593         ir_mode               *mode;
1594
1595         if(in == out) {
1596                 return;
1597         }
1598         if(is_unknown_reg(in))
1599                 return;
1600         /* copies of vf nodes aren't real... */
1601         if(arch_register_get_class(in) == &ia32_reg_classes[CLASS_ia32_vfp])
1602                 return;
1603
1604         mode = get_irn_mode(node);
1605         if (mode == mode_E) {
1606                 be_emit_cstring("\tmovsd ");
1607                 emit_register(in, NULL);
1608                 be_emit_cstring(", ");
1609                 emit_register(out, NULL);
1610         } else {
1611                 be_emit_cstring("\tmovl ");
1612                 emit_register(in, NULL);
1613                 be_emit_cstring(", ");
1614                 emit_register(out, NULL);
1615         }
1616         be_emit_finish_line_gas(node);
1617 }
1618
1619 static void emit_be_Copy(const ir_node *node)
1620 {
1621         Copy_emitter(node, be_get_Copy_op(node));
1622 }
1623
1624 static void emit_be_CopyKeep(const ir_node *node)
1625 {
1626         Copy_emitter(node, be_get_CopyKeep_op(node));
1627 }
1628
1629 /**
1630  * Emits code for exchange.
1631  */
1632 static void emit_be_Perm(const ir_node *node)
1633 {
1634         const arch_register_t *in0, *in1;
1635         const arch_register_class_t *cls0, *cls1;
1636
1637         in0 = arch_get_irn_register(arch_env, get_irn_n(node, 0));
1638         in1 = arch_get_irn_register(arch_env, get_irn_n(node, 1));
1639
1640         cls0 = arch_register_get_class(in0);
1641         cls1 = arch_register_get_class(in1);
1642
1643         assert(cls0 == cls1 && "Register class mismatch at Perm");
1644
1645         if (cls0 == &ia32_reg_classes[CLASS_ia32_gp]) {
1646                 be_emit_cstring("\txchg ");
1647                 emit_register(in1, NULL);
1648                 be_emit_cstring(", ");
1649                 emit_register(in0, NULL);
1650                 be_emit_finish_line_gas(node);
1651         } else if (cls0 == &ia32_reg_classes[CLASS_ia32_xmm]) {
1652                 be_emit_cstring("\txorpd ");
1653                 emit_register(in1, NULL);
1654                 be_emit_cstring(", ");
1655                 emit_register(in0, NULL);
1656                 be_emit_finish_line_gas(NULL);
1657
1658                 be_emit_cstring("\txorpd ");
1659                 emit_register(in0, NULL);
1660                 be_emit_cstring(", ");
1661                 emit_register(in1, NULL);
1662                 be_emit_finish_line_gas(NULL);
1663
1664                 be_emit_cstring("\txorpd ");
1665                 emit_register(in1, NULL);
1666                 be_emit_cstring(", ");
1667                 emit_register(in0, NULL);
1668                 be_emit_finish_line_gas(node);
1669         } else if (cls0 == &ia32_reg_classes[CLASS_ia32_vfp]) {
1670                 /* is a NOP */
1671         } else if (cls0 == &ia32_reg_classes[CLASS_ia32_st]) {
1672                 /* is a NOP */
1673         } else {
1674                 panic("unexpected register class in be_Perm (%+F)\n", node);
1675         }
1676 }
1677
1678 /**
1679  * Emits code for Constant loading.
1680  */
1681 static void emit_ia32_Const(const ir_node *node)
1682 {
1683         be_emit_cstring("\tmovl ");
1684         emit_ia32_Immediate(node);
1685         be_emit_cstring(", ");
1686         ia32_emit_dest_register(node, 0);
1687
1688         be_emit_finish_line_gas(node);
1689 }
1690
1691 /**
1692  * Emits code to load the TLS base
1693  */
1694 static void emit_ia32_LdTls(const ir_node *node)
1695 {
1696         be_emit_cstring("\tmovl %gs:0, ");
1697         ia32_emit_dest_register(node, 0);
1698         be_emit_finish_line_gas(node);
1699 }
1700
1701 /* helper function for emit_ia32_Minus64Bit */
1702 static void emit_mov(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1703 {
1704         be_emit_cstring("\tmovl ");
1705         emit_register(src, NULL);
1706         be_emit_cstring(", ");
1707         emit_register(dst, NULL);
1708         be_emit_finish_line_gas(node);
1709 }
1710
1711 /* helper function for emit_ia32_Minus64Bit */
1712 static void emit_neg(const ir_node* node, const arch_register_t *reg)
1713 {
1714         be_emit_cstring("\tnegl ");
1715         emit_register(reg, NULL);
1716         be_emit_finish_line_gas(node);
1717 }
1718
1719 /* helper function for emit_ia32_Minus64Bit */
1720 static void emit_sbb0(const ir_node* node, const arch_register_t *reg)
1721 {
1722         be_emit_cstring("\tsbbl $0, ");
1723         emit_register(reg, NULL);
1724         be_emit_finish_line_gas(node);
1725 }
1726
1727 /* helper function for emit_ia32_Minus64Bit */
1728 static void emit_sbb(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1729 {
1730         be_emit_cstring("\tsbbl ");
1731         emit_register(src, NULL);
1732         be_emit_cstring(", ");
1733         emit_register(dst, NULL);
1734         be_emit_finish_line_gas(node);
1735 }
1736
1737 /* helper function for emit_ia32_Minus64Bit */
1738 static void emit_xchg(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1739 {
1740         be_emit_cstring("\txchgl ");
1741         emit_register(src, NULL);
1742         be_emit_cstring(", ");
1743         emit_register(dst, NULL);
1744         be_emit_finish_line_gas(node);
1745 }
1746
1747 /* helper function for emit_ia32_Minus64Bit */
1748 static void emit_zero(const ir_node* node, const arch_register_t *reg)
1749 {
1750         be_emit_cstring("\txorl ");
1751         emit_register(reg, NULL);
1752         be_emit_cstring(", ");
1753         emit_register(reg, NULL);
1754         be_emit_finish_line_gas(node);
1755 }
1756
1757 static void emit_ia32_Minus64Bit(const ir_node *node)
1758 {
1759         const arch_register_t *in_lo  = get_in_reg(node, 0);
1760         const arch_register_t *in_hi  = get_in_reg(node, 1);
1761         const arch_register_t *out_lo = get_out_reg(node, 0);
1762         const arch_register_t *out_hi = get_out_reg(node, 1);
1763
1764         if (out_lo == in_lo) {
1765                 if (out_hi != in_hi) {
1766                         /* a -> a, b -> d */
1767                         goto zero_neg;
1768                 } else {
1769                         /* a -> a, b -> b */
1770                         goto normal_neg;
1771                 }
1772         } else if (out_lo == in_hi) {
1773                 if (out_hi == in_lo) {
1774                         /* a -> b, b -> a */
1775                         emit_xchg(node, in_lo, in_hi);
1776                         goto normal_neg;
1777                 } else {
1778                         /* a -> b, b -> d */
1779                         emit_mov(node, in_hi, out_hi);
1780                         emit_mov(node, in_lo, out_lo);
1781                         goto normal_neg;
1782                 }
1783         } else {
1784                 if (out_hi == in_lo) {
1785                         /* a -> c, b -> a */
1786                         emit_mov(node, in_lo, out_lo);
1787                         goto zero_neg;
1788                 } else if (out_hi == in_hi) {
1789                         /* a -> c, b -> b */
1790                         emit_mov(node, in_lo, out_lo);
1791                         goto normal_neg;
1792                 } else {
1793                         /* a -> c, b -> d */
1794                         emit_mov(node, in_lo, out_lo);
1795                         goto zero_neg;
1796                 }
1797         }
1798
1799 normal_neg:
1800         emit_neg( node, out_hi);
1801         emit_neg( node, out_lo);
1802         emit_sbb0(node, out_hi);
1803         return;
1804
1805 zero_neg:
1806         emit_zero(node, out_hi);
1807         emit_neg( node, out_lo);
1808         emit_sbb( node, in_hi, out_hi);
1809 }
1810
1811 static void emit_be_Return(const ir_node *node)
1812 {
1813         unsigned pop;
1814         be_emit_cstring("\tret");
1815
1816         pop = be_Return_get_pop(node);
1817         if(pop > 0) {
1818                 be_emit_irprintf(" $%d", pop);
1819         }
1820         be_emit_finish_line_gas(node);
1821 }
1822
1823 static void emit_Nothing(const ir_node *node)
1824 {
1825         (void) node;
1826 }
1827
1828
1829 /***********************************************************************************
1830  *                  _          __                                             _
1831  *                 (_)        / _|                                           | |
1832  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
1833  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1834  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
1835  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
1836  *
1837  ***********************************************************************************/
1838
1839 /**
1840  * Enters the emitter functions for handled nodes into the generic
1841  * pointer of an opcode.
1842  */
1843 static
1844 void ia32_register_emitters(void) {
1845
1846 #define IA32_EMIT2(a,b) op_ia32_##a->ops.generic = (op_func)emit_ia32_##b
1847 #define IA32_EMIT(a)    IA32_EMIT2(a,a)
1848 #define EMIT(a)         op_##a->ops.generic = (op_func)emit_##a
1849 #define IGN(a)                  op_##a->ops.generic = (op_func)emit_Nothing
1850 #define BE_EMIT(a)      op_be_##a->ops.generic = (op_func)emit_be_##a
1851 #define BE_IGN(a)               op_be_##a->ops.generic = (op_func)emit_Nothing
1852
1853         /* first clear the generic function pointer for all ops */
1854         clear_irp_opcodes_generic_func();
1855
1856         /* register all emitter functions defined in spec */
1857         ia32_register_spec_emitters();
1858
1859         /* other ia32 emitter functions */
1860         IA32_EMIT(Asm);
1861         IA32_EMIT(CMov);
1862         IA32_EMIT(IMul);
1863         IA32_EMIT(SwitchJmp);
1864         IA32_EMIT(CopyB);
1865         IA32_EMIT(CopyB_i);
1866         IA32_EMIT(Conv_I2FP);
1867         IA32_EMIT(Conv_FP2I);
1868         IA32_EMIT(Conv_FP2FP);
1869         IA32_EMIT(Conv_I2I);
1870         IA32_EMIT2(Conv_I2I8Bit, Conv_I2I);
1871         IA32_EMIT(Const);
1872         IA32_EMIT(LdTls);
1873         IA32_EMIT(Minus64Bit);
1874         IA32_EMIT(Jcc);
1875
1876         /* benode emitter */
1877         BE_EMIT(Call);
1878         BE_EMIT(IncSP);
1879         BE_EMIT(Copy);
1880         BE_EMIT(CopyKeep);
1881         BE_EMIT(Perm);
1882         BE_EMIT(Return);
1883
1884         BE_IGN(RegParams);
1885         BE_IGN(Barrier);
1886         BE_IGN(Keep);
1887
1888         /* firm emitter */
1889         EMIT(Jmp);
1890         IGN(Proj);
1891         IGN(Phi);
1892         IGN(Start);
1893
1894 #undef BE_EMIT
1895 #undef EMIT
1896 #undef IGN
1897 #undef IA32_EMIT2
1898 #undef IA32_EMIT
1899 }
1900
1901 static const char *last_name = NULL;
1902 static unsigned last_line = -1;
1903 static unsigned num = -1;
1904
1905 /**
1906  * Emit the debug support for node node.
1907  */
1908 static void ia32_emit_dbg(const ir_node *node)
1909 {
1910         dbg_info *db = get_irn_dbg_info(node);
1911         unsigned lineno;
1912         const char *fname = be_retrieve_dbg_info(db, &lineno);
1913
1914         if (! cg->birg->main_env->options->stabs_debug_support)
1915                 return;
1916
1917         if (fname) {
1918                 if (last_name != fname) {
1919                         last_line = -1;
1920                         be_dbg_include_begin(cg->birg->main_env->db_handle, fname);
1921                         last_name = fname;
1922                 }
1923                 if (last_line != lineno) {
1924                         char name[64];
1925
1926                         snprintf(name, sizeof(name), ".LM%u", ++num);
1927                         last_line = lineno;
1928                         be_dbg_line(cg->birg->main_env->db_handle, lineno, name);
1929                         be_emit_string(name);
1930                         be_emit_cstring(":\n");
1931                         be_emit_write_line();
1932                 }
1933         }
1934 }
1935
1936 typedef void (*emit_func_ptr) (const ir_node *);
1937
1938 /**
1939  * Emits code for a node.
1940  */
1941 static void ia32_emit_node(const ir_node *node)
1942 {
1943         ir_op *op = get_irn_op(node);
1944
1945         DBG((dbg, LEVEL_1, "emitting code for %+F\n", node));
1946
1947         if (op->ops.generic) {
1948                 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
1949                 ia32_emit_dbg(node);
1950                 (*func) (node);
1951         } else {
1952                 emit_Nothing(node);
1953                 ir_fprintf(stderr, "Error: No emit handler for node %+F (%+G, graph %+F)\n", node, node, current_ir_graph);
1954                 abort();
1955         }
1956 }
1957
1958 /**
1959  * Emits gas alignment directives
1960  */
1961 static void ia32_emit_alignment(unsigned align, unsigned skip)
1962 {
1963         be_emit_cstring("\t.p2align ");
1964         be_emit_irprintf("%u,,%u\n", align, skip);
1965         be_emit_write_line();
1966 }
1967
1968 /**
1969  * Emits gas alignment directives for Functions depended on cpu architecture.
1970  */
1971 static void ia32_emit_align_func(cpu_support cpu)
1972 {
1973         unsigned align;
1974         unsigned maximum_skip;
1975
1976         switch (cpu) {
1977                 case arch_i386:
1978                         align = 2;
1979                         break;
1980                 case arch_i486:
1981                         align = 4;
1982                         break;
1983                 case arch_k6:
1984                         align = 5;
1985                         break;
1986                 default:
1987                         align = 4;
1988         }
1989         maximum_skip = (1 << align) - 1;
1990         ia32_emit_alignment(align, maximum_skip);
1991 }
1992
1993 /**
1994  * Emits gas alignment directives for Labels depended on cpu architecture.
1995  */
1996 static void ia32_emit_align_label(cpu_support cpu)
1997 {
1998         unsigned align; unsigned maximum_skip;
1999
2000         switch (cpu) {
2001                 case arch_i386:
2002                         align = 2;
2003                         break;
2004                 case arch_i486:
2005                         align = 4;
2006                         break;
2007                 case arch_k6:
2008                         align = 5;
2009                         break;
2010                 default:
2011                         align = 4;
2012         }
2013         maximum_skip = (1 << align) - 1;
2014         ia32_emit_alignment(align, maximum_skip);
2015 }
2016
2017 /**
2018  * Test wether a block should be aligned.
2019  * For cpus in the P4/Athlon class it is useful to align jump labels to
2020  * 16 bytes. However we should only do that if the alignment nops before the
2021  * label aren't executed more often than we have jumps to the label.
2022  */
2023 static int should_align_block(ir_node *block, ir_node *prev)
2024 {
2025         static const double DELTA = .0001;
2026         ir_exec_freq *exec_freq   = cg->birg->exec_freq;
2027         double        block_freq;
2028         double        prev_freq = 0;  /**< execfreq of the fallthrough block */
2029         double        jmp_freq  = 0;  /**< execfreq of all non-fallthrough blocks */
2030         cpu_support   cpu       = isa->opt_arch;
2031         int           i, n_cfgpreds;
2032
2033         if(exec_freq == NULL)
2034                 return 0;
2035         if(cpu == arch_i386 || cpu == arch_i486)
2036                 return 0;
2037
2038         block_freq = get_block_execfreq(exec_freq, block);
2039         if(block_freq < DELTA)
2040                 return 0;
2041
2042         n_cfgpreds = get_Block_n_cfgpreds(block);
2043         for(i = 0; i < n_cfgpreds; ++i) {
2044                 ir_node *pred      = get_Block_cfgpred_block(block, i);
2045                 double   pred_freq = get_block_execfreq(exec_freq, pred);
2046
2047                 if(pred == prev) {
2048                         prev_freq += pred_freq;
2049                 } else {
2050                         jmp_freq  += pred_freq;
2051                 }
2052         }
2053
2054         if(prev_freq < DELTA && !(jmp_freq < DELTA))
2055                 return 1;
2056
2057         jmp_freq /= prev_freq;
2058
2059         switch (cpu) {
2060                 case arch_athlon:
2061                 case arch_athlon_64:
2062                 case arch_k6:
2063                         return jmp_freq > 3;
2064                 default:
2065                         return jmp_freq > 2;
2066         }
2067 }
2068
2069 static void ia32_emit_block_header(ir_node *block, ir_node *prev)
2070 {
2071         int           n_cfgpreds;
2072         int           need_label;
2073         int           i, arity;
2074         ir_exec_freq  *exec_freq = cg->birg->exec_freq;
2075
2076         n_cfgpreds = get_Block_n_cfgpreds(block);
2077         need_label = (n_cfgpreds != 0);
2078
2079         if (should_align_block(block, prev)) {
2080                 assert(need_label);
2081                 ia32_emit_align_label(isa->opt_arch);
2082         }
2083
2084         if(need_label) {
2085                 ia32_emit_block_name(block);
2086                 be_emit_char(':');
2087
2088                 be_emit_pad_comment();
2089                 be_emit_cstring("   /* preds:");
2090
2091                 /* emit list of pred blocks in comment */
2092                 arity = get_irn_arity(block);
2093                 for (i = 0; i < arity; ++i) {
2094                         ir_node *predblock = get_Block_cfgpred_block(block, i);
2095                         be_emit_irprintf(" %d", get_irn_node_nr(predblock));
2096                 }
2097         } else {
2098                 be_emit_cstring("\t/* ");
2099                 ia32_emit_block_name(block);
2100                 be_emit_cstring(": ");
2101         }
2102         if (exec_freq != NULL) {
2103                 be_emit_irprintf(" freq: %f",
2104                                  get_block_execfreq(exec_freq, block));
2105         }
2106         be_emit_cstring(" */\n");
2107         be_emit_write_line();
2108 }
2109
2110 /**
2111  * Walks over the nodes in a block connected by scheduling edges
2112  * and emits code for each node.
2113  */
2114 static void ia32_gen_block(ir_node *block, ir_node *last_block)
2115 {
2116         const ir_node *node;
2117
2118         ia32_emit_block_header(block, last_block);
2119
2120         /* emit the contents of the block */
2121         ia32_emit_dbg(block);
2122         sched_foreach(block, node) {
2123                 ia32_emit_node(node);
2124         }
2125 }
2126
2127 /**
2128  * Emits code for function start.
2129  */
2130 static void ia32_emit_func_prolog(ir_graph *irg)
2131 {
2132         ir_entity  *irg_ent  = get_irg_entity(irg);
2133         const char *irg_name = get_entity_ld_name(irg_ent);
2134         cpu_support cpu      = isa->opt_arch;
2135         const be_irg_t *birg = cg->birg;
2136
2137         /* write the begin line (used by scripts processing the assembler... */
2138         be_emit_write_line();
2139         be_emit_cstring("# -- Begin  ");
2140         be_emit_string(irg_name);
2141         be_emit_char('\n');
2142         be_emit_write_line();
2143
2144         be_gas_emit_switch_section(GAS_SECTION_TEXT);
2145         be_dbg_method_begin(birg->main_env->db_handle, irg_ent, be_abi_get_stack_layout(birg->abi));
2146         ia32_emit_align_func(cpu);
2147         if (get_entity_visibility(irg_ent) == visibility_external_visible) {
2148                 be_emit_cstring(".global ");
2149                 be_emit_string(irg_name);
2150                 be_emit_char('\n');
2151                 be_emit_write_line();
2152         }
2153         ia32_emit_function_object(irg_name);
2154         be_emit_string(irg_name);
2155         be_emit_cstring(":\n");
2156         be_emit_write_line();
2157 }
2158
2159 /**
2160  * Emits code for function end
2161  */
2162 static void ia32_emit_func_epilog(ir_graph *irg)
2163 {
2164         const char     *irg_name = get_entity_ld_name(get_irg_entity(irg));
2165         const be_irg_t *birg     = cg->birg;
2166
2167         ia32_emit_function_size(irg_name);
2168         be_dbg_method_end(birg->main_env->db_handle);
2169
2170         be_emit_cstring("# -- End  ");
2171         be_emit_string(irg_name);
2172         be_emit_char('\n');
2173         be_emit_write_line();
2174
2175         be_emit_char('\n');
2176         be_emit_write_line();
2177 }
2178
2179 /**
2180  * Block-walker:
2181  * Sets labels for control flow nodes (jump target)
2182  */
2183 static void ia32_gen_labels(ir_node *block, void *data)
2184 {
2185         ir_node *pred;
2186         int n = get_Block_n_cfgpreds(block);
2187         (void) data;
2188
2189         for (n--; n >= 0; n--) {
2190                 pred = get_Block_cfgpred(block, n);
2191                 set_irn_link(pred, block);
2192         }
2193 }
2194
2195 /**
2196  * Emit an exception label if the current instruction can fail.
2197  */
2198 void ia32_emit_exc_label(const ir_node *node)
2199 {
2200         if (get_ia32_exc_label(node)) {
2201                 be_emit_irprintf(".EXL%u\n", 0);
2202                 be_emit_write_line();
2203         }
2204 }
2205
2206 /**
2207  * Main driver. Emits the code for one routine.
2208  */
2209 void ia32_gen_routine(ia32_code_gen_t *ia32_cg, ir_graph *irg)
2210 {
2211         ir_node *block;
2212         ir_node *last_block = NULL;
2213         int i, n;
2214
2215         cg       = ia32_cg;
2216         isa      = (const ia32_isa_t*) cg->arch_env->isa;
2217         arch_env = cg->arch_env;
2218
2219         ia32_register_emitters();
2220
2221         ia32_emit_func_prolog(irg);
2222         irg_block_walk_graph(irg, ia32_gen_labels, NULL, NULL);
2223
2224         n = ARR_LEN(cg->blk_sched);
2225         for (i = 0; i < n;) {
2226                 ir_node *next_bl;
2227
2228                 block   = cg->blk_sched[i];
2229                 ++i;
2230                 next_bl = i < n ? cg->blk_sched[i] : NULL;
2231
2232                 /* set here the link. the emitter expects to find the next block here */
2233                 set_irn_link(block, next_bl);
2234                 ia32_gen_block(block, last_block);
2235                 last_block = block;
2236         }
2237
2238         ia32_emit_func_epilog(irg);
2239 }
2240
2241 void ia32_init_emitter(void)
2242 {
2243         FIRM_DBG_REGISTER(dbg, "firm.be.ia32.emitter");
2244 }