fix for fehler87
[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                 reg = arch_get_irn_register(arch_env, irn);
128         } else if (is_ia32_irn(irn)) {
129                 reg = get_ia32_out_reg(irn, pos);
130         } else {
131                 const ir_edge_t *edge;
132
133                 foreach_out_edge(irn, edge) {
134                         proj = get_edge_src_irn(edge);
135                         assert(is_Proj(proj) && "non-Proj from mode_T node");
136                         if (get_Proj_proj(proj) == pos) {
137                                 reg = arch_get_irn_register(arch_env, proj);
138                                 break;
139                         }
140                 }
141         }
142
143         assert(reg && "no out register found");
144         return reg;
145 }
146
147 /**
148  * Add a number to a prefix. This number will not be used a second time.
149  */
150 static char *get_unique_label(char *buf, size_t buflen, const char *prefix)
151 {
152         static unsigned long id = 0;
153         snprintf(buf, buflen, "%s%lu", prefix, ++id);
154         return buf;
155 }
156
157 /*************************************************************
158  *             _       _    __   _          _
159  *            (_)     | |  / _| | |        | |
160  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
161  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
162  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
163  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
164  * | |                                       | |
165  * |_|                                       |_|
166  *************************************************************/
167
168 static void emit_8bit_register(const arch_register_t *reg)
169 {
170         const char *reg_name = arch_register_get_name(reg);
171
172         be_emit_char('%');
173         be_emit_char(reg_name[1]);
174         be_emit_char('l');
175 }
176
177 static void emit_16bit_register(const arch_register_t *reg)
178 {
179         const char *reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
180
181         be_emit_char('%');
182         be_emit_string(reg_name);
183 }
184
185 static void emit_register(const arch_register_t *reg, const ir_mode *mode)
186 {
187         const char *reg_name;
188
189         if(mode != NULL) {
190                 int size = get_mode_size_bits(mode);
191                 if(size == 8) {
192                         emit_8bit_register(reg);
193                         return;
194                 } else if(size == 16) {
195                         emit_16bit_register(reg);
196                         return;
197                 } else {
198                         assert(size == 32);
199                 }
200         }
201
202         reg_name = arch_register_get_name(reg);
203
204         be_emit_char('%');
205         be_emit_string(reg_name);
206 }
207
208 void ia32_emit_source_register(const ir_node *node, int pos)
209 {
210         const arch_register_t *reg      = get_in_reg(node, pos);
211
212         emit_register(reg, NULL);
213 }
214
215 static void emit_ia32_Immediate(const ir_node *node);
216
217 void ia32_emit_8bit_source_register_or_immediate(const ir_node *node, int pos)
218 {
219         const arch_register_t *reg;
220         ir_node               *in = get_irn_n(node, pos);
221         if(is_ia32_Immediate(in)) {
222                 emit_ia32_Immediate(in);
223                 return;
224         }
225
226         reg = get_in_reg(node, pos);
227         emit_8bit_register(reg);
228 }
229
230 void ia32_emit_dest_register(const ir_node *node, int pos)
231 {
232         const arch_register_t *reg  = get_out_reg(node, pos);
233
234         emit_register(reg, NULL);
235 }
236
237 void ia32_emit_8bit_dest_register(const ir_node *node, int pos)
238 {
239         const arch_register_t *reg  = get_out_reg(node, pos);
240
241         emit_register(reg, mode_Bu);
242 }
243
244 void ia32_emit_x87_register(const ir_node *node, int pos)
245 {
246         const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
247
248         assert(pos < 3);
249         be_emit_char('%');
250         be_emit_string(attr->x87[pos]->name);
251 }
252
253 static void ia32_emit_mode_suffix_mode(const ir_mode *mode)
254 {
255         if(mode_is_float(mode)) {
256                 switch(get_mode_size_bits(mode)) {
257                 case 32: be_emit_char('s'); return;
258                 case 64: be_emit_char('l'); return;
259                 case 80: be_emit_char('t'); return;
260                 }
261         } else {
262                 assert(mode_is_int(mode) || mode_is_reference(mode));
263                 switch(get_mode_size_bits(mode)) {
264                 case 64: be_emit_cstring("ll"); return;
265                                  /* gas docu says q is the suffix but gcc, objdump and icc use
266                                         ll apparently */
267                 case 32: be_emit_char('l'); return;
268                 case 16: be_emit_char('w'); return;
269                 case 8:  be_emit_char('b'); return;
270                 }
271         }
272         panic("Can't output mode_suffix for %+F\n", mode);
273 }
274
275 void ia32_emit_mode_suffix(const ir_node *node)
276 {
277         ir_mode *mode = get_ia32_ls_mode(node);
278         if(mode == NULL)
279                 mode = mode_Iu;
280
281         ia32_emit_mode_suffix_mode(mode);
282 }
283
284 void ia32_emit_x87_mode_suffix(const ir_node *node)
285 {
286         ir_mode *mode = get_ia32_ls_mode(node);
287         if(mode != NULL)
288                 ia32_emit_mode_suffix_mode(mode);
289 }
290
291 static
292 char get_xmm_mode_suffix(ir_mode *mode)
293 {
294         assert(mode_is_float(mode));
295         switch(get_mode_size_bits(mode)) {
296         case 32:
297                 return 's';
298         case 64:
299                 return 'd';
300         default:
301                 assert(0);
302         }
303         return '%';
304 }
305
306 void ia32_emit_xmm_mode_suffix(const ir_node *node)
307 {
308         ir_mode *mode = get_ia32_ls_mode(node);
309         assert(mode != NULL);
310         be_emit_char('s');
311         be_emit_char(get_xmm_mode_suffix(mode));
312 }
313
314 void ia32_emit_xmm_mode_suffix_s(const ir_node *node)
315 {
316         ir_mode *mode = get_ia32_ls_mode(node);
317         assert(mode != NULL);
318         be_emit_char(get_xmm_mode_suffix(mode));
319 }
320
321 void ia32_emit_extend_suffix(const ir_mode *mode)
322 {
323         if(get_mode_size_bits(mode) == 32)
324                 return;
325         if(mode_is_signed(mode)) {
326                 be_emit_char('s');
327         } else {
328                 be_emit_char('z');
329         }
330 }
331
332 static
333 void ia32_emit_function_object(const char *name)
334 {
335         switch (be_gas_flavour) {
336         case GAS_FLAVOUR_NORMAL:
337                 be_emit_cstring("\t.type\t");
338                 be_emit_string(name);
339                 be_emit_cstring(", @function\n");
340                 be_emit_write_line();
341                 break;
342         case GAS_FLAVOUR_MINGW:
343                 be_emit_cstring("\t.def\t");
344                 be_emit_string(name);
345                 be_emit_cstring(";\t.scl\t2;\t.type\t32;\t.endef\n");
346                 be_emit_write_line();
347                 break;
348         default:
349                 break;
350         }
351 }
352
353 static
354 void ia32_emit_function_size(const char *name)
355 {
356         switch (be_gas_flavour) {
357         case GAS_FLAVOUR_NORMAL:
358                 be_emit_cstring("\t.size\t");
359                 be_emit_string(name);
360                 be_emit_cstring(", .-");
361                 be_emit_string(name);
362                 be_emit_char('\n');
363                 be_emit_write_line();
364                 break;
365         default:
366                 break;
367         }
368 }
369
370
371 void ia32_emit_source_register_or_immediate(const ir_node *node, int pos)
372 {
373         ir_node *in = get_irn_n(node, pos);
374         if(is_ia32_Immediate(in)) {
375                 emit_ia32_Immediate(in);
376         } else {
377                 const ir_mode         *mode = get_ia32_ls_mode(node);
378                 const arch_register_t *reg  = get_in_reg(node, pos);
379                 emit_register(reg, mode);
380         }
381 }
382
383 /**
384  * Emits registers and/or address mode of a binary operation.
385  */
386 void ia32_emit_binop(const ir_node *node, int produces_result) {
387         const ir_node         *right_op  = get_irn_n(node, n_ia32_binary_right);
388         const ir_mode         *mode      = get_ia32_ls_mode(node);
389         const arch_register_t *reg_left;
390
391         switch(get_ia32_op_type(node)) {
392         case ia32_Normal:
393                 reg_left = get_in_reg(node, n_ia32_binary_left);
394                 if(is_ia32_Immediate(right_op)) {
395                         emit_ia32_Immediate(right_op);
396                         be_emit_cstring(", ");
397                         emit_register(reg_left, mode);
398                         break;
399                 } else {
400                         const arch_register_t *reg_right
401                                 = get_in_reg(node, n_ia32_binary_right);
402                         emit_register(reg_right, mode);
403                         be_emit_cstring(", ");
404                         emit_register(reg_left, mode);
405                 }
406                 break;
407         case ia32_AddrModeS:
408                 if(is_ia32_Immediate(right_op)) {
409                         assert(!produces_result && "Source AM with Const must not produce result");
410
411                         emit_ia32_Immediate(right_op);
412                         be_emit_cstring(", ");
413                         ia32_emit_am(node);
414                 } else {
415                         reg_left = get_in_reg(node, n_ia32_binary_left);
416                         ia32_emit_am(node);
417                         be_emit_cstring(", ");
418                         emit_register(reg_left, mode);
419                 }
420                 break;
421         case ia32_AddrModeD:
422                 panic("DestMode can't be output by %%binop anymore");
423                 break;
424         default:
425                 assert(0 && "unsupported op type");
426         }
427 }
428
429 /**
430  * Emits registers and/or address mode of a binary operation.
431  */
432 void ia32_emit_x87_binop(const ir_node *node) {
433         switch(get_ia32_op_type(node)) {
434                 case ia32_Normal:
435                         {
436                                 const ia32_x87_attr_t *x87_attr = get_ia32_x87_attr_const(node);
437                                 const arch_register_t *in1      = x87_attr->x87[0];
438                                 const arch_register_t *in2      = x87_attr->x87[1];
439                                 const arch_register_t *out      = x87_attr->x87[2];
440                                 const arch_register_t *in;
441
442                                 in  = out ? ((out == in2) ? in1 : in2) : in2;
443                                 out = out ? out : in1;
444
445                                 be_emit_char('%');
446                                 be_emit_string(arch_register_get_name(in));
447                                 be_emit_cstring(", %");
448                                 be_emit_string(arch_register_get_name(out));
449                         }
450                         break;
451                 case ia32_AddrModeS:
452                         ia32_emit_am(node);
453                         break;
454                 case ia32_AddrModeD:
455                 default:
456                         assert(0 && "unsupported op type");
457         }
458 }
459
460 void ia32_emit_am_or_dest_register(const ir_node *node,
461                                    int pos) {
462         if(get_ia32_op_type(node) == ia32_Normal) {
463                 ia32_emit_dest_register(node, pos);
464         } else {
465                 assert(get_ia32_op_type(node) == ia32_AddrModeD);
466                 ia32_emit_am(node);
467         }
468 }
469
470 /**
471  * Emits registers and/or address mode of a unary operation.
472  */
473 void ia32_emit_unop(const ir_node *node, int pos) {
474         const ir_node *op;
475
476         switch(get_ia32_op_type(node)) {
477         case ia32_Normal:
478                 op = get_irn_n(node, pos);
479                 if (is_ia32_Immediate(op)) {
480                         emit_ia32_Immediate(op);
481                 } else {
482                         ia32_emit_source_register(node, pos);
483                 }
484                 break;
485         case ia32_AddrModeS:
486         case ia32_AddrModeD:
487                 ia32_emit_am(node);
488                 break;
489         default:
490                 assert(0 && "unsupported op type");
491         }
492 }
493
494 /**
495  * Emits address mode.
496  */
497 void ia32_emit_am(const ir_node *node) {
498         ir_entity *ent       = get_ia32_am_sc(node);
499         int        offs      = get_ia32_am_offs_int(node);
500         ir_node   *base      = get_irn_n(node, 0);
501         int        has_base  = !is_ia32_NoReg_GP(base);
502         ir_node   *index     = get_irn_n(node, 1);
503         int        has_index = !is_ia32_NoReg_GP(index);
504
505         /* just to be sure... */
506         assert(!is_ia32_use_frame(node) || get_ia32_frame_ent(node) != NULL);
507
508         /* emit offset */
509         if (ent != NULL) {
510                 ident *id;
511
512                 set_entity_backend_marked(ent, 1);
513                 id = get_entity_ld_ident(ent);
514                 if (is_ia32_am_sc_sign(node))
515                         be_emit_char('-');
516                 be_emit_ident(id);
517
518                 if(get_entity_owner(ent) == get_tls_type()) {
519                         if (get_entity_visibility(ent) == visibility_external_allocated) {
520                                 be_emit_cstring("@INDNTPOFF");
521                         } else {
522                                 be_emit_cstring("@NTPOFF");
523                         }
524                 }
525         }
526
527         if(offs != 0) {
528                 if(ent != NULL) {
529                         be_emit_irprintf("%+d", offs);
530                 } else {
531                         be_emit_irprintf("%d", offs);
532                 }
533         }
534
535         if (has_base || has_index) {
536                 be_emit_char('(');
537
538                 /* emit base */
539                 if (has_base) {
540                         const arch_register_t *reg = get_in_reg(node, n_ia32_base);
541                         emit_register(reg, NULL);
542                 }
543
544                 /* emit index + scale */
545                 if (has_index) {
546                         const arch_register_t *reg = get_in_reg(node, n_ia32_index);
547                         int scale;
548                         be_emit_char(',');
549                         emit_register(reg, NULL);
550
551                         scale = get_ia32_am_scale(node);
552                         if (scale > 0) {
553                                 be_emit_irprintf(",%d", 1 << get_ia32_am_scale(node));
554                         }
555                 }
556                 be_emit_char(')');
557         }
558
559         /* special case if nothing is set */
560         if(ent == NULL && offs == 0 && !has_base && !has_index) {
561                 be_emit_char('0');
562         }
563 }
564
565 /*************************************************
566  *                 _ _                         _
567  *                (_) |                       | |
568  *   ___ _ __ ___  _| |_    ___ ___  _ __   __| |
569  *  / _ \ '_ ` _ \| | __|  / __/ _ \| '_ \ / _` |
570  * |  __/ | | | | | | |_  | (_| (_) | | | | (_| |
571  *  \___|_| |_| |_|_|\__|  \___\___/|_| |_|\__,_|
572  *
573  *************************************************/
574
575 #undef IA32_DO_EMIT
576 #define IA32_DO_EMIT(irn) ia32_fprintf_format(F, irn, cmd_buf, cmnt_buf)
577
578 /*
579  * coding of conditions
580  */
581 struct cmp2conditon_t {
582         const char *name;
583         int         num;
584 };
585
586 /*
587  * positive conditions for signed compares
588  */
589 static const struct cmp2conditon_t cmp2condition_s[] = {
590         { NULL,              pn_Cmp_False },  /* always false */
591         { "e",               pn_Cmp_Eq },     /* == */
592         { "l",               pn_Cmp_Lt },     /* < */
593         { "le",              pn_Cmp_Le },     /* <= */
594         { "g",               pn_Cmp_Gt },     /* > */
595         { "ge",              pn_Cmp_Ge },     /* >= */
596         { "ne",              pn_Cmp_Lg },     /* != */
597         { NULL,              pn_Cmp_Leg},     /* always true */
598 };
599
600 /*
601  * positive conditions for unsigned compares
602  */
603 static const struct cmp2conditon_t cmp2condition_u[] = {
604         { NULL,              pn_Cmp_False },  /* always false */
605         { "e",               pn_Cmp_Eq },     /* == */
606         { "b",               pn_Cmp_Lt },     /* < */
607         { "be",              pn_Cmp_Le },     /* <= */
608         { "a",               pn_Cmp_Gt },     /* > */
609         { "ae",              pn_Cmp_Ge },     /* >= */
610         { "ne",              pn_Cmp_Lg },     /* != */
611         { NULL,              pn_Cmp_Leg },   /* always true  */
612 };
613
614 enum {
615         ia32_pn_Cmp_unsigned = 0x1000,
616         ia32_pn_Cmp_float    = 0x2000,
617 };
618
619 /**
620  * walks up a tree of copies/perms/spills/reloads to find the original value
621  * that is moved around
622  */
623 static ir_node *find_original_value(ir_node *node)
624 {
625         inc_irg_visited(current_ir_graph);
626         while(1) {
627                 mark_irn_visited(node);
628                 if(be_is_Copy(node)) {
629                         node = be_get_Copy_op(node);
630                 } else if(be_is_CopyKeep(node)) {
631                         node = be_get_CopyKeep_op(node);
632                 } else if(is_Proj(node)) {
633                         ir_node *pred = get_Proj_pred(node);
634                         if(be_is_Perm(pred)) {
635                                 node = get_irn_n(pred, get_Proj_proj(node));
636                         } else if(be_is_MemPerm(pred)) {
637                                 node = get_irn_n(pred, get_Proj_proj(node) + 1);
638                         } else if(is_ia32_Load(pred)) {
639                                 node = get_irn_n(pred, n_ia32_Load_mem);
640                         } else {
641                                 return node;
642                         }
643                 } else if(is_ia32_Store(node)) {
644                         node = get_irn_n(node, n_ia32_Store_val);
645                 } else if(is_Phi(node)) {
646                         int i, arity;
647                         arity = get_irn_arity(node);
648                         for(i = 0; i < arity; ++i) {
649                                 ir_node *in = get_irn_n(node, i);
650                                 if(irn_visited(in))
651                                         continue;
652                                 node = in;
653                                 break;
654                         }
655                         assert(i < arity);
656                 } else {
657                         return node;
658                 }
659         }
660 }
661
662 static int determine_final_pnc(const ir_node *node, int flags_pos,
663                                int pnc)
664 {
665         ir_node           *flags = get_irn_n(node, flags_pos);
666         const ia32_attr_t *flags_attr;
667         flags = skip_Proj(flags);
668
669         if(is_ia32_Sahf(flags)) {
670                 ir_node *cmp = get_irn_n(flags, n_ia32_Sahf_val);
671                 if(!is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
672                                 || is_ia32_FucomppFnstsw(cmp)) {
673                         cmp = find_original_value(cmp);
674                         assert(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
675                                || is_ia32_FucomppFnstsw(cmp));
676                 }
677
678                 flags_attr = get_ia32_attr_const(cmp);
679                 if(flags_attr->data.cmp_flipped)
680                         pnc = get_mirrored_pnc(pnc);
681                 pnc |= ia32_pn_Cmp_float;
682         } else if(is_ia32_Ucomi(flags)) {
683                 flags_attr = get_ia32_attr_const(flags);
684
685                 if(flags_attr->data.cmp_flipped)
686                         pnc = get_mirrored_pnc(pnc);
687                 pnc |= ia32_pn_Cmp_float;
688         } else {
689                 assert(is_ia32_Cmp(flags) || is_ia32_Test(flags)
690                                 || is_ia32_Cmp8Bit(flags) || is_ia32_Test8Bit(flags));
691                 flags_attr = get_ia32_attr_const(flags);
692
693                 if(flags_attr->data.cmp_flipped)
694                         pnc = get_mirrored_pnc(pnc);
695                 if(flags_attr->data.cmp_unsigned)
696                         pnc |= ia32_pn_Cmp_unsigned;
697         }
698
699         return pnc;
700 }
701
702 static void ia32_emit_cmp_suffix(int pnc)
703 {
704         const char        *str;
705
706         if((pnc & ia32_pn_Cmp_float) || (pnc & ia32_pn_Cmp_unsigned)) {
707                 pnc = pnc & 7;
708                 assert(cmp2condition_u[pnc].num == pnc);
709                 str = cmp2condition_u[pnc].name;
710         } else {
711                 pnc = pnc & 7;
712                 assert(cmp2condition_s[pnc].num == pnc);
713                 str = cmp2condition_s[pnc].name;
714         }
715
716         be_emit_string(str);
717 }
718
719 void ia32_emit_cmp_suffix_node(const ir_node *node,
720                                int flags_pos)
721 {
722         pn_Cmp pnc = get_ia32_pncode(node);
723
724         pnc = determine_final_pnc(node, flags_pos, pnc);
725         ia32_emit_cmp_suffix(pnc);
726 }
727
728 /**
729  * Returns the target block for a control flow node.
730  */
731 static
732 ir_node *get_cfop_target_block(const ir_node *irn) {
733         return get_irn_link(irn);
734 }
735
736 /**
737  * Emits a block label for the given block.
738  */
739 static
740 void ia32_emit_block_name(const ir_node *block)
741 {
742         if (has_Block_label(block)) {
743                 be_emit_string(be_gas_label_prefix());
744                 be_emit_irprintf("%u", (unsigned)get_Block_label(block));
745         } else {
746                 be_emit_cstring(BLOCK_PREFIX);
747                 be_emit_irprintf("%d", get_irn_node_nr(block));
748         }
749 }
750
751 /**
752  * Emits the target label for a control flow node.
753  */
754 static void ia32_emit_cfop_target(const ir_node *node)
755 {
756         ir_node *block = get_cfop_target_block(node);
757
758         ia32_emit_block_name(block);
759 }
760
761 /** Return the next block in Block schedule */
762 static ir_node *next_blk_sched(const ir_node *block)
763 {
764         return get_irn_link(block);
765 }
766
767 /**
768  * Returns the Proj with projection number proj and NOT mode_M
769  */
770 static ir_node *get_proj(const ir_node *node, long proj) {
771         const ir_edge_t *edge;
772         ir_node         *src;
773
774         assert(get_irn_mode(node) == mode_T && "expected mode_T node");
775
776         foreach_out_edge(node, edge) {
777                 src = get_edge_src_irn(edge);
778
779                 assert(is_Proj(src) && "Proj expected");
780                 if (get_irn_mode(src) == mode_M)
781                         continue;
782
783                 if (get_Proj_proj(src) == proj)
784                         return src;
785         }
786         return NULL;
787 }
788
789 /**
790  * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
791  */
792 static void emit_ia32_Jcc(const ir_node *node)
793 {
794         const ir_node *proj_true;
795         const ir_node *proj_false;
796         const ir_node *block;
797         const ir_node *next_block;
798         pn_Cmp         pnc = get_ia32_pncode(node);
799
800         pnc = determine_final_pnc(node, 0, pnc);
801
802         /* get both Projs */
803         proj_true = get_proj(node, pn_ia32_Jcc_true);
804         assert(proj_true && "Jcc without true Proj");
805
806         proj_false = get_proj(node, pn_ia32_Jcc_false);
807         assert(proj_false && "Jcc without false Proj");
808
809         block      = get_nodes_block(node);
810         next_block = next_blk_sched(block);
811
812         if (get_cfop_target_block(proj_true) == next_block) {
813                 /* exchange both proj's so the second one can be omitted */
814                 const ir_node *t = proj_true;
815
816                 proj_true  = proj_false;
817                 proj_false = t;
818                 if(pnc & ia32_pn_Cmp_float) {
819                         pnc = get_negated_pnc(pnc, mode_F);
820                 } else {
821                         pnc = get_negated_pnc(pnc, mode_Iu);
822                 }
823         }
824
825         if (pnc & ia32_pn_Cmp_float) {
826                 /* Some floating point comparisons require a test of the parity flag,
827                  * which indicates that the result is unordered */
828                 switch (pnc & 15) {
829                         case pn_Cmp_Uo:
830                                 be_emit_cstring("\tjp ");
831                                 ia32_emit_cfop_target(proj_true);
832                                 be_emit_finish_line_gas(proj_true);
833                                 break;
834
835                         case pn_Cmp_Leg:
836                                 be_emit_cstring("\tjnp ");
837                                 ia32_emit_cfop_target(proj_true);
838                                 be_emit_finish_line_gas(proj_true);
839                                 break;
840
841                         case pn_Cmp_Eq:
842                         case pn_Cmp_Lt:
843                         case pn_Cmp_Le:
844                                 be_emit_cstring("\tjp ");
845                                 ia32_emit_cfop_target(proj_false);
846                                 be_emit_finish_line_gas(proj_false);
847                                 goto emit_jcc;
848
849                         case pn_Cmp_Ug:
850                         case pn_Cmp_Uge:
851                         case pn_Cmp_Ne:
852                                 be_emit_cstring("\tjp ");
853                                 ia32_emit_cfop_target(proj_true);
854                                 be_emit_finish_line_gas(proj_true);
855                                 goto emit_jcc;
856
857                         default:
858                                 goto emit_jcc;
859                 }
860         } else {
861 emit_jcc:
862                 be_emit_cstring("\tj");
863                 ia32_emit_cmp_suffix(pnc);
864                 be_emit_char(' ');
865                 ia32_emit_cfop_target(proj_true);
866                 be_emit_finish_line_gas(proj_true);
867         }
868
869         /* the second Proj might be a fallthrough */
870         if (get_cfop_target_block(proj_false) != next_block) {
871                 be_emit_cstring("\tjmp ");
872                 ia32_emit_cfop_target(proj_false);
873                 be_emit_finish_line_gas(proj_false);
874         } else {
875                 be_emit_cstring("\t/* fallthrough to ");
876                 ia32_emit_cfop_target(proj_false);
877                 be_emit_cstring(" */");
878                 be_emit_finish_line_gas(proj_false);
879         }
880 }
881
882 static void emit_ia32_CMov(const ir_node *node)
883 {
884         const arch_register_t *out = arch_get_irn_register(arch_env, node);
885         const arch_register_t *in_true;
886         const arch_register_t *in_false;
887         pn_Cmp                 pnc = get_ia32_pncode(node);
888
889         pnc = determine_final_pnc(node, n_ia32_CMov_eflags, pnc);
890
891         in_true  = arch_get_irn_register(arch_env,
892                                          get_irn_n(node, n_ia32_CMov_val_true));
893         in_false = arch_get_irn_register(arch_env,
894                                          get_irn_n(node, n_ia32_CMov_val_false));
895
896         /* should be same constraint fullfilled? */
897         if(out == in_false) {
898                 /* yes -> nothing to do */
899         } else if(out == in_true) {
900                 const arch_register_t *tmp;
901
902                 /* swap left/right and negate pnc */
903                 pnc = get_negated_pnc(pnc, mode_Iu);
904
905                 tmp      = in_true;
906                 in_true  = in_false;
907                 in_false = tmp;
908         } else {
909                 /* we need a mov */
910                 be_emit_cstring("\tmovl ");
911                 emit_register(in_false, NULL);
912                 be_emit_cstring(", ");
913                 emit_register(out, NULL);
914                 be_emit_finish_line_gas(node);
915         }
916
917         be_emit_cstring("\tcmov");
918         ia32_emit_cmp_suffix(pnc);
919         be_emit_char(' ');
920         if(get_ia32_op_type(node) == ia32_AddrModeS) {
921                 ia32_emit_am(node);
922         } else {
923                 emit_register(in_true, get_ia32_ls_mode(node));
924         }
925         be_emit_cstring(", ");
926         emit_register(out, get_ia32_ls_mode(node));
927         be_emit_finish_line_gas(node);
928 }
929
930 /*********************************************************
931  *                 _ _       _
932  *                (_) |     (_)
933  *   ___ _ __ ___  _| |_     _ _   _ _ __ ___  _ __  ___
934  *  / _ \ '_ ` _ \| | __|   | | | | | '_ ` _ \| '_ \/ __|
935  * |  __/ | | | | | | |_    | | |_| | | | | | | |_) \__ \
936  *  \___|_| |_| |_|_|\__|   | |\__,_|_| |_| |_| .__/|___/
937  *                         _/ |               | |
938  *                        |__/                |_|
939  *********************************************************/
940
941 /* jump table entry (target and corresponding number) */
942 typedef struct _branch_t {
943         ir_node *target;
944         int      value;
945 } branch_t;
946
947 /* jump table for switch generation */
948 typedef struct _jmp_tbl_t {
949         ir_node  *defProj;         /**< default target */
950         long      min_value;       /**< smallest switch case */
951         long      max_value;       /**< largest switch case */
952         long      num_branches;    /**< number of jumps */
953         char     *label;           /**< label of the jump table */
954         branch_t *branches;        /**< jump array */
955 } jmp_tbl_t;
956
957 /**
958  * Compare two variables of type branch_t. Used to sort all switch cases
959  */
960 static
961 int ia32_cmp_branch_t(const void *a, const void *b) {
962         branch_t *b1 = (branch_t *)a;
963         branch_t *b2 = (branch_t *)b;
964
965         if (b1->value <= b2->value)
966                 return -1;
967         else
968                 return 1;
969 }
970
971 /**
972  * Emits code for a SwitchJmp (creates a jump table if
973  * possible otherwise a cmp-jmp cascade). Port from
974  * cggg ia32 backend
975  */
976 static
977 void emit_ia32_SwitchJmp(const ir_node *node) {
978         unsigned long       interval;
979         int                 last_value, i;
980         long                pnc;
981         jmp_tbl_t           tbl;
982         ir_node            *proj;
983         const ir_edge_t    *edge;
984
985         /* fill the table structure */
986         tbl.label        = xmalloc(SNPRINTF_BUF_LEN);
987         tbl.label        = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, ".TBL_");
988         tbl.defProj      = NULL;
989         tbl.num_branches = get_irn_n_edges(node);
990         tbl.branches     = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
991         tbl.min_value    = INT_MAX;
992         tbl.max_value    = INT_MIN;
993
994         i = 0;
995         /* go over all proj's and collect them */
996         foreach_out_edge(node, edge) {
997                 proj = get_edge_src_irn(edge);
998                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
999
1000                 pnc = get_Proj_proj(proj);
1001
1002                 /* create branch entry */
1003                 tbl.branches[i].target = proj;
1004                 tbl.branches[i].value  = pnc;
1005
1006                 tbl.min_value = pnc < tbl.min_value ? pnc : tbl.min_value;
1007                 tbl.max_value = pnc > tbl.max_value ? pnc : tbl.max_value;
1008
1009                 /* check for default proj */
1010                 if (pnc == get_ia32_pncode(node)) {
1011                         assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
1012                         tbl.defProj = proj;
1013                 }
1014
1015                 i++;
1016         }
1017
1018         /* sort the branches by their number */
1019         qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
1020
1021         /* two-complement's magic make this work without overflow */
1022         interval = tbl.max_value - tbl.min_value;
1023
1024         /* emit the table */
1025         be_emit_cstring("\tcmpl $");
1026         be_emit_irprintf("%u, ", interval);
1027         ia32_emit_source_register(node, 0);
1028         be_emit_finish_line_gas(node);
1029
1030         be_emit_cstring("\tja ");
1031         ia32_emit_cfop_target(tbl.defProj);
1032         be_emit_finish_line_gas(node);
1033
1034         if (tbl.num_branches > 1) {
1035                 /* create table */
1036                 be_emit_cstring("\tjmp *");
1037                 be_emit_string(tbl.label);
1038                 be_emit_cstring("(,");
1039                 ia32_emit_source_register(node, 0);
1040                 be_emit_cstring(",4)");
1041                 be_emit_finish_line_gas(node);
1042
1043                 be_gas_emit_switch_section(GAS_SECTION_RODATA);
1044                 be_emit_cstring("\t.align 4\n");
1045                 be_emit_write_line();
1046
1047                 be_emit_string(tbl.label);
1048                 be_emit_cstring(":\n");
1049                 be_emit_write_line();
1050
1051                 be_emit_cstring(".long ");
1052                 ia32_emit_cfop_target(tbl.branches[0].target);
1053                 be_emit_finish_line_gas(NULL);
1054
1055                 last_value = tbl.branches[0].value;
1056                 for (i = 1; i < tbl.num_branches; ++i) {
1057                         while (++last_value < tbl.branches[i].value) {
1058                                 be_emit_cstring(".long ");
1059                                 ia32_emit_cfop_target(tbl.defProj);
1060                                 be_emit_finish_line_gas(NULL);
1061                         }
1062                         be_emit_cstring(".long ");
1063                         ia32_emit_cfop_target(tbl.branches[i].target);
1064                         be_emit_finish_line_gas(NULL);
1065                 }
1066                 be_gas_emit_switch_section(GAS_SECTION_TEXT);
1067         } else {
1068                 /* one jump is enough */
1069                 be_emit_cstring("\tjmp ");
1070                 ia32_emit_cfop_target(tbl.branches[0].target);
1071                 be_emit_finish_line_gas(node);
1072         }
1073
1074         if (tbl.label)
1075                 free(tbl.label);
1076         if (tbl.branches)
1077                 free(tbl.branches);
1078 }
1079
1080 /**
1081  * Emits code for a unconditional jump.
1082  */
1083 static void emit_Jmp(const ir_node *node)
1084 {
1085         ir_node *block, *next_block;
1086
1087         /* for now, the code works for scheduled and non-schedules blocks */
1088         block = get_nodes_block(node);
1089
1090         /* we have a block schedule */
1091         next_block = next_blk_sched(block);
1092         if (get_cfop_target_block(node) != next_block) {
1093                 be_emit_cstring("\tjmp ");
1094                 ia32_emit_cfop_target(node);
1095         } else {
1096                 be_emit_cstring("\t/* fallthrough to ");
1097                 ia32_emit_cfop_target(node);
1098                 be_emit_cstring(" */");
1099         }
1100         be_emit_finish_line_gas(node);
1101 }
1102
1103 static void emit_ia32_Immediate(const ir_node *node)
1104 {
1105         const ia32_immediate_attr_t *attr = get_ia32_immediate_attr_const(node);
1106
1107         be_emit_char('$');
1108         if(attr->symconst != NULL) {
1109                 ident *id = get_entity_ld_ident(attr->symconst);
1110
1111                 if(attr->attr.data.am_sc_sign)
1112                         be_emit_char('-');
1113                 be_emit_ident(id);
1114         }
1115         if(attr->symconst == NULL || attr->offset != 0) {
1116                 if(attr->symconst != NULL) {
1117                         be_emit_irprintf("%+d", attr->offset);
1118                 } else {
1119                         be_emit_irprintf("0x%X", attr->offset);
1120                 }
1121         }
1122 }
1123
1124 static const char* emit_asm_operand(const ir_node *node, const char *s)
1125 {
1126         const arch_register_t *reg;
1127         const char            *reg_name;
1128         char                   c;
1129         char                   modifier = 0;
1130         int                    num      = -1;
1131         const ia32_attr_t     *attr;
1132         int                    n_outs;
1133         int                    p;
1134
1135         assert(*s == '%');
1136         c = *(++s);
1137
1138         /* parse modifiers */
1139         switch(c) {
1140         case 0:
1141                 ir_fprintf(stderr, "Warning: asm text (%+F) ends with %\n", node);
1142                 be_emit_char('%');
1143                 return s + 1;
1144         case '%':
1145                 be_emit_char('%');
1146                 return s + 1;
1147         case 'w':
1148         case 'b':
1149         case 'h':
1150                 modifier = c;
1151                 ++s;
1152                 break;
1153         case '0':
1154         case '1':
1155         case '2':
1156         case '3':
1157         case '4':
1158         case '5':
1159         case '6':
1160         case '7':
1161         case '8':
1162         case '9':
1163                 break;
1164         default:
1165                 ir_fprintf(stderr, "Warning: asm text (%+F) contains unknown modifier "
1166                            "'%c' for asm op\n", node, c);
1167                 ++s;
1168                 break;
1169         }
1170
1171         /* parse number */
1172         sscanf(s, "%d%n", &num, &p);
1173         if(num < 0) {
1174                 ir_fprintf(stderr, "Warning: Couldn't parse assembler operand (%+F)\n",
1175                            node);
1176                 return s;
1177         } else {
1178                 s += p;
1179         }
1180
1181         /* get register */
1182         attr   = get_ia32_attr_const(node);
1183         n_outs = ARR_LEN(attr->slots);
1184         if(num < n_outs) {
1185                 reg = get_out_reg(node, num);
1186         } else {
1187                 ir_node *pred;
1188                 int      in = num - n_outs;
1189                 if(in >= get_irn_arity(node)) {
1190                         ir_fprintf(stderr, "Warning: Invalid input %d specified in asm "
1191                                    "op (%+F)\n", num, node);
1192                         return s;
1193                 }
1194                 pred = get_irn_n(node, in);
1195                 /* might be an immediate value */
1196                 if(is_ia32_Immediate(pred)) {
1197                         emit_ia32_Immediate(pred);
1198                         return s;
1199                 }
1200                 reg = get_in_reg(node, in);
1201         }
1202         if(reg == NULL) {
1203                 ir_fprintf(stderr, "Warning: no register assigned for %d asm op "
1204                            "(%+F)\n", num, node);
1205                 return s;
1206         }
1207
1208         /* emit it */
1209         be_emit_char('%');
1210         switch(modifier) {
1211         case 0:
1212                 reg_name = arch_register_get_name(reg);
1213                 break;
1214         case 'b':
1215                 reg_name = ia32_get_mapped_reg_name(isa->regs_8bit, reg);
1216                 break;
1217         case 'h':
1218                 reg_name = ia32_get_mapped_reg_name(isa->regs_8bit_high, reg);
1219                 break;
1220         case 'w':
1221                 reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
1222                 break;
1223         default:
1224                 panic("Invalid asm op modifier");
1225         }
1226         be_emit_string(reg_name);
1227
1228         return s;
1229 }
1230
1231 /**
1232  * Emits code for an ASM pseudo op.
1233  */
1234 static void emit_ia32_Asm(const ir_node *node)
1235 {
1236         const void            *gen_attr = get_irn_generic_attr_const(node);
1237         const ia32_asm_attr_t *attr
1238                 = CONST_CAST_IA32_ATTR(ia32_asm_attr_t, gen_attr);
1239         ident                 *asm_text = attr->asm_text;
1240         const char            *s        = get_id_str(asm_text);
1241
1242         be_emit_cstring("# Begin ASM \t");
1243         be_emit_finish_line_gas(node);
1244
1245         if (s[0] != '\t')
1246                 be_emit_char('\t');
1247
1248         while(*s != 0) {
1249                 if(*s == '%') {
1250                         s = emit_asm_operand(node, s);
1251                         continue;
1252                 } else {
1253                         be_emit_char(*s);
1254                 }
1255                 ++s;
1256         }
1257
1258         be_emit_char('\n');
1259         be_emit_write_line();
1260
1261         be_emit_cstring("# End ASM\n");
1262         be_emit_write_line();
1263 }
1264
1265 /**********************************
1266  *   _____                  ____
1267  *  / ____|                |  _ \
1268  * | |     ___  _ __  _   _| |_) |
1269  * | |    / _ \| '_ \| | | |  _ <
1270  * | |___| (_) | |_) | |_| | |_) |
1271  *  \_____\___/| .__/ \__, |____/
1272  *             | |     __/ |
1273  *             |_|    |___/
1274  **********************************/
1275
1276 /**
1277  * Emit movsb/w instructions to make mov count divideable by 4
1278  */
1279 static void emit_CopyB_prolog(int rem) {
1280         be_emit_cstring("\tcld");
1281         be_emit_finish_line_gas(NULL);
1282
1283         switch(rem) {
1284         case 1:
1285                 be_emit_cstring("\tmovsb");
1286                 be_emit_finish_line_gas(NULL);
1287                 break;
1288         case 2:
1289                 be_emit_cstring("\tmovsw");
1290                 be_emit_finish_line_gas(NULL);
1291                 break;
1292         case 3:
1293                 be_emit_cstring("\tmovsb");
1294                 be_emit_finish_line_gas(NULL);
1295                 be_emit_cstring("\tmovsw");
1296                 be_emit_finish_line_gas(NULL);
1297                 break;
1298         }
1299 }
1300
1301 /**
1302  * Emit rep movsd instruction for memcopy.
1303  */
1304 static void emit_ia32_CopyB(const ir_node *node)
1305 {
1306         int rem = get_ia32_pncode(node);
1307
1308         emit_CopyB_prolog(rem);
1309
1310         be_emit_cstring("\trep movsd");
1311         be_emit_finish_line_gas(node);
1312 }
1313
1314 /**
1315  * Emits unrolled memcopy.
1316  */
1317 static void emit_ia32_CopyB_i(const ir_node *node)
1318 {
1319         int size = get_ia32_pncode(node);
1320
1321         emit_CopyB_prolog(size & 0x3);
1322
1323         size >>= 2;
1324         while (size--) {
1325                 be_emit_cstring("\tmovsd");
1326                 be_emit_finish_line_gas(NULL);
1327         }
1328 }
1329
1330
1331
1332 /***************************
1333  *   _____
1334  *  / ____|
1335  * | |     ___  _ ____   __
1336  * | |    / _ \| '_ \ \ / /
1337  * | |___| (_) | | | \ V /
1338  *  \_____\___/|_| |_|\_/
1339  *
1340  ***************************/
1341
1342 /**
1343  * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1344  */
1345 static void emit_ia32_Conv_with_FP(const ir_node *node)
1346 {
1347         ir_mode            *ls_mode = get_ia32_ls_mode(node);
1348         int                 ls_bits = get_mode_size_bits(ls_mode);
1349
1350         be_emit_cstring("\tcvt");
1351
1352         if(is_ia32_Conv_I2FP(node)) {
1353                 if(ls_bits == 32) {
1354                         be_emit_cstring("si2ss");
1355                 } else {
1356                         be_emit_cstring("si2sd");
1357                 }
1358         } else if(is_ia32_Conv_FP2I(node)) {
1359                 if(ls_bits == 32) {
1360                         be_emit_cstring("ss2si");
1361                 } else {
1362                         be_emit_cstring("sd2si");
1363                 }
1364         } else {
1365                 assert(is_ia32_Conv_FP2FP(node));
1366                 if(ls_bits == 32) {
1367                         be_emit_cstring("sd2ss");
1368                 } else {
1369                         be_emit_cstring("ss2sd");
1370                 }
1371         }
1372         be_emit_char(' ');
1373
1374         switch(get_ia32_op_type(node)) {
1375                 case ia32_Normal:
1376                         ia32_emit_source_register(node, n_ia32_unary_op);
1377                         be_emit_cstring(", ");
1378                         ia32_emit_dest_register(node, 0);
1379                         break;
1380                 case ia32_AddrModeS:
1381                         ia32_emit_dest_register(node, 0);
1382                         be_emit_cstring(", ");
1383                         ia32_emit_am(node);
1384                         break;
1385                 default:
1386                         assert(0 && "unsupported op type for Conv");
1387         }
1388         be_emit_finish_line_gas(node);
1389 }
1390
1391 static void emit_ia32_Conv_I2FP(const ir_node *node)
1392 {
1393         emit_ia32_Conv_with_FP(node);
1394 }
1395
1396 static void emit_ia32_Conv_FP2I(const ir_node *node)
1397 {
1398         emit_ia32_Conv_with_FP(node);
1399 }
1400
1401 static void emit_ia32_Conv_FP2FP(const ir_node *node)
1402 {
1403         emit_ia32_Conv_with_FP(node);
1404 }
1405
1406 /**
1407  * Emits code for an Int conversion.
1408  */
1409 static void emit_ia32_Conv_I2I(const ir_node *node)
1410 {
1411         const char            *sign_suffix;
1412         ir_mode               *smaller_mode = get_ia32_ls_mode(node);
1413         int                    smaller_bits = get_mode_size_bits(smaller_mode);
1414         int                    signed_mode;
1415         const arch_register_t *in_reg, *out_reg;
1416
1417         assert(!mode_is_float(smaller_mode));
1418         assert(smaller_bits == 8 || smaller_bits == 16 || smaller_bits == 32);
1419
1420         signed_mode = mode_is_signed(smaller_mode);
1421         if(smaller_bits == 32) {
1422                 // this should not happen as it's no convert
1423                 assert(0);
1424                 sign_suffix = "";
1425         } else {
1426                 sign_suffix = signed_mode ? "s" : "z";
1427         }
1428
1429         out_reg = get_out_reg(node, 0);
1430
1431         switch(get_ia32_op_type(node)) {
1432                 case ia32_Normal:
1433                         in_reg  = get_in_reg(node, n_ia32_unary_op);
1434
1435                         if (in_reg  == &ia32_gp_regs[REG_EAX] &&
1436                                 out_reg == &ia32_gp_regs[REG_EAX] &&
1437                                 signed_mode &&
1438                                 smaller_bits == 16)
1439                         {
1440                                 /* argument and result are both in EAX and */
1441                                 /* signedness is ok: -> use the smaller cwtl opcode */
1442                                 be_emit_cstring("\tcwtl");
1443                         } else {
1444                                 be_emit_cstring("\tmov");
1445                                 be_emit_string(sign_suffix);
1446                                 ia32_emit_mode_suffix_mode(smaller_mode);
1447                                 be_emit_cstring("l ");
1448                                 emit_register(in_reg, smaller_mode);
1449                                 be_emit_cstring(", ");
1450                                 emit_register(out_reg, NULL);
1451                         }
1452                         break;
1453                 case ia32_AddrModeS: {
1454                         be_emit_cstring("\tmov");
1455                         be_emit_string(sign_suffix);
1456                         ia32_emit_mode_suffix_mode(smaller_mode);
1457                         be_emit_cstring("l ");
1458                         ia32_emit_am(node);
1459                         be_emit_cstring(", ");
1460                         emit_register(out_reg, NULL);
1461                         break;
1462                 }
1463                 default:
1464                         assert(0 && "unsupported op type for Conv");
1465         }
1466         be_emit_finish_line_gas(node);
1467 }
1468
1469 /**
1470  * Emits code for an 8Bit Int conversion.
1471  */
1472 static void emit_ia32_Conv_I2I8Bit(const ir_node *node)
1473 {
1474         emit_ia32_Conv_I2I(node);
1475 }
1476
1477
1478 /*******************************************
1479  *  _                          _
1480  * | |                        | |
1481  * | |__   ___ _ __   ___   __| | ___  ___
1482  * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1483  * | |_) |  __/ | | | (_) | (_| |  __/\__ \
1484  * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1485  *
1486  *******************************************/
1487
1488 /**
1489  * Emits a backend call
1490  */
1491 static void emit_be_Call(const ir_node *node)
1492 {
1493         ir_entity *ent = be_Call_get_entity(node);
1494
1495         be_emit_cstring("\tcall ");
1496         if (ent) {
1497                 set_entity_backend_marked(ent, 1);
1498                 be_emit_string(get_entity_ld_name(ent));
1499         } else {
1500                 const arch_register_t *reg = get_in_reg(node, be_pos_Call_ptr);
1501                 be_emit_char('*');
1502                 emit_register(reg, NULL);
1503         }
1504         be_emit_finish_line_gas(node);
1505 }
1506
1507 /**
1508  * Emits code to increase stack pointer.
1509  */
1510 static void emit_be_IncSP(const ir_node *node)
1511 {
1512         int                    offs = be_get_IncSP_offset(node);
1513         const arch_register_t *reg  = arch_get_irn_register(arch_env, node);
1514
1515         if (offs == 0)
1516                 return;
1517
1518         if (offs > 0) {
1519                 be_emit_cstring("\tsubl $");
1520                 be_emit_irprintf("%u, ", offs);
1521                 emit_register(reg, NULL);
1522         } else {
1523                 be_emit_cstring("\taddl $");
1524                 be_emit_irprintf("%u, ", -offs);
1525                 emit_register(reg, NULL);
1526         }
1527         be_emit_finish_line_gas(node);
1528 }
1529
1530 /**
1531  * Emits code for Copy/CopyKeep.
1532  */
1533 static void Copy_emitter(const ir_node *node, const ir_node *op)
1534 {
1535         const arch_register_t *in  = arch_get_irn_register(arch_env, op);
1536         const arch_register_t *out = arch_get_irn_register(arch_env, node);
1537         ir_mode               *mode;
1538
1539         if(in == out) {
1540                 return;
1541         }
1542         if(is_unknown_reg(in))
1543                 return;
1544         /* copies of vf nodes aren't real... */
1545         if(arch_register_get_class(in) == &ia32_reg_classes[CLASS_ia32_vfp])
1546                 return;
1547
1548         mode = get_irn_mode(node);
1549         if (mode == mode_E) {
1550                 be_emit_cstring("\tmovsd ");
1551                 emit_register(in, NULL);
1552                 be_emit_cstring(", ");
1553                 emit_register(out, NULL);
1554         } else {
1555                 be_emit_cstring("\tmovl ");
1556                 emit_register(in, NULL);
1557                 be_emit_cstring(", ");
1558                 emit_register(out, NULL);
1559         }
1560         be_emit_finish_line_gas(node);
1561 }
1562
1563 static void emit_be_Copy(const ir_node *node)
1564 {
1565         Copy_emitter(node, be_get_Copy_op(node));
1566 }
1567
1568 static void emit_be_CopyKeep(const ir_node *node)
1569 {
1570         Copy_emitter(node, be_get_CopyKeep_op(node));
1571 }
1572
1573 /**
1574  * Emits code for exchange.
1575  */
1576 static void emit_be_Perm(const ir_node *node)
1577 {
1578         const arch_register_t *in0, *in1;
1579         const arch_register_class_t *cls0, *cls1;
1580
1581         in0 = arch_get_irn_register(arch_env, get_irn_n(node, 0));
1582         in1 = arch_get_irn_register(arch_env, get_irn_n(node, 1));
1583
1584         cls0 = arch_register_get_class(in0);
1585         cls1 = arch_register_get_class(in1);
1586
1587         assert(cls0 == cls1 && "Register class mismatch at Perm");
1588
1589         if (cls0 == &ia32_reg_classes[CLASS_ia32_gp]) {
1590                 be_emit_cstring("\txchg ");
1591                 emit_register(in1, NULL);
1592                 be_emit_cstring(", ");
1593                 emit_register(in0, NULL);
1594                 be_emit_finish_line_gas(node);
1595         } else if (cls0 == &ia32_reg_classes[CLASS_ia32_xmm]) {
1596                 be_emit_cstring("\txorpd ");
1597                 emit_register(in1, NULL);
1598                 be_emit_cstring(", ");
1599                 emit_register(in0, NULL);
1600                 be_emit_finish_line_gas(NULL);
1601
1602                 be_emit_cstring("\txorpd ");
1603                 emit_register(in0, NULL);
1604                 be_emit_cstring(", ");
1605                 emit_register(in1, NULL);
1606                 be_emit_finish_line_gas(NULL);
1607
1608                 be_emit_cstring("\txorpd ");
1609                 emit_register(in1, NULL);
1610                 be_emit_cstring(", ");
1611                 emit_register(in0, NULL);
1612                 be_emit_finish_line_gas(node);
1613         } else if (cls0 == &ia32_reg_classes[CLASS_ia32_vfp]) {
1614                 /* is a NOP */
1615         } else if (cls0 == &ia32_reg_classes[CLASS_ia32_st]) {
1616                 /* is a NOP */
1617         } else {
1618                 panic("unexpected register class in be_Perm (%+F)\n", node);
1619         }
1620 }
1621
1622 /**
1623  * Emits code for Constant loading.
1624  */
1625 static void emit_ia32_Const(const ir_node *node)
1626 {
1627         const ia32_immediate_attr_t *attr = get_ia32_immediate_attr_const(node);
1628
1629         /* a zero? */
1630         if(attr->symconst == NULL && attr->offset == 0) {
1631                 assert(get_ia32_flags(node) & arch_irn_flags_modify_flags);
1632                 be_emit_cstring("\txorl ");
1633                 ia32_emit_dest_register(node, 0);
1634                 be_emit_cstring(", ");
1635                 ia32_emit_dest_register(node, 0);
1636         } else {
1637                 be_emit_cstring("\tmovl ");
1638                 emit_ia32_Immediate(node);
1639                 be_emit_cstring(", ");
1640                 ia32_emit_dest_register(node, 0);
1641         }
1642
1643         be_emit_finish_line_gas(node);
1644 }
1645
1646 /**
1647  * Emits code to load the TLS base
1648  */
1649 static void emit_ia32_LdTls(const ir_node *node)
1650 {
1651         be_emit_cstring("\tmovl %gs:0, ");
1652         ia32_emit_dest_register(node, 0);
1653         be_emit_finish_line_gas(node);
1654 }
1655
1656 /* helper function for emit_ia32_Minus64Bit */
1657 static void emit_mov(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1658 {
1659         be_emit_cstring("\tmovl ");
1660         emit_register(src, NULL);
1661         be_emit_cstring(", ");
1662         emit_register(dst, NULL);
1663         be_emit_finish_line_gas(node);
1664 }
1665
1666 /* helper function for emit_ia32_Minus64Bit */
1667 static void emit_neg(const ir_node* node, const arch_register_t *reg)
1668 {
1669         be_emit_cstring("\tnegl ");
1670         emit_register(reg, NULL);
1671         be_emit_finish_line_gas(node);
1672 }
1673
1674 /* helper function for emit_ia32_Minus64Bit */
1675 static void emit_sbb0(const ir_node* node, const arch_register_t *reg)
1676 {
1677         be_emit_cstring("\tsbbl $0, ");
1678         emit_register(reg, NULL);
1679         be_emit_finish_line_gas(node);
1680 }
1681
1682 /* helper function for emit_ia32_Minus64Bit */
1683 static void emit_sbb(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1684 {
1685         be_emit_cstring("\tsbbl ");
1686         emit_register(src, NULL);
1687         be_emit_cstring(", ");
1688         emit_register(dst, NULL);
1689         be_emit_finish_line_gas(node);
1690 }
1691
1692 /* helper function for emit_ia32_Minus64Bit */
1693 static void emit_xchg(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1694 {
1695         be_emit_cstring("\txchgl ");
1696         emit_register(src, NULL);
1697         be_emit_cstring(", ");
1698         emit_register(dst, NULL);
1699         be_emit_finish_line_gas(node);
1700 }
1701
1702 /* helper function for emit_ia32_Minus64Bit */
1703 static void emit_zero(const ir_node* node, const arch_register_t *reg)
1704 {
1705         be_emit_cstring("\txorl ");
1706         emit_register(reg, NULL);
1707         be_emit_cstring(", ");
1708         emit_register(reg, NULL);
1709         be_emit_finish_line_gas(node);
1710 }
1711
1712 static void emit_ia32_Minus64Bit(const ir_node *node)
1713 {
1714         const arch_register_t *in_lo  = get_in_reg(node, 0);
1715         const arch_register_t *in_hi  = get_in_reg(node, 1);
1716         const arch_register_t *out_lo = get_out_reg(node, 0);
1717         const arch_register_t *out_hi = get_out_reg(node, 1);
1718
1719         if (out_lo == in_lo) {
1720                 if (out_hi != in_hi) {
1721                         /* a -> a, b -> d */
1722                         goto zero_neg;
1723                 } else {
1724                         /* a -> a, b -> b */
1725                         goto normal_neg;
1726                 }
1727         } else if (out_lo == in_hi) {
1728                 if (out_hi == in_lo) {
1729                         /* a -> b, b -> a */
1730                         emit_xchg(node, in_lo, in_hi);
1731                         goto normal_neg;
1732                 } else {
1733                         /* a -> b, b -> d */
1734                         emit_mov(node, in_hi, out_hi);
1735                         emit_mov(node, in_lo, out_lo);
1736                         goto normal_neg;
1737                 }
1738         } else {
1739                 if (out_hi == in_lo) {
1740                         /* a -> c, b -> a */
1741                         emit_mov(node, in_lo, out_lo);
1742                         goto zero_neg;
1743                 } else if (out_hi == in_hi) {
1744                         /* a -> c, b -> b */
1745                         emit_mov(node, in_lo, out_lo);
1746                         goto normal_neg;
1747                 } else {
1748                         /* a -> c, b -> d */
1749                         emit_mov(node, in_lo, out_lo);
1750                         goto zero_neg;
1751                 }
1752         }
1753
1754 normal_neg:
1755         emit_neg( node, out_hi);
1756         emit_neg( node, out_lo);
1757         emit_sbb0(node, out_hi);
1758         return;
1759
1760 zero_neg:
1761         emit_zero(node, out_hi);
1762         emit_neg( node, out_lo);
1763         emit_sbb( node, in_hi, out_hi);
1764 }
1765
1766 static void emit_be_Return(const ir_node *node)
1767 {
1768         be_emit_cstring("\tret");
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_EMIT(Conv_I2I8Bit);
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 }