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