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