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