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