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