fixed define
[libfirm] / ir / be / ia32 / ia32_emitter.c
1 /**
2  * This file implements the node emitter.
3  *
4  * $Id$
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <limits.h>
12
13 #include "xmalloc.h"
14 #include "tv.h"
15 #include "iredges.h"
16 #include "debug.h"
17 #include "irgwalk.h"
18 #include "irprintf.h"
19 #include "irop_t.h"
20 #include "irargs_t.h"
21 #include "irprog_t.h"
22 #include "iredges_t.h"
23
24 #include "../besched_t.h"
25 #include "../benode_t.h"
26
27 #include "ia32_emitter.h"
28 #include "gen_ia32_emitter.h"
29 #include "gen_ia32_regalloc_if.h"
30 #include "ia32_nodes_attr.h"
31 #include "ia32_new_nodes.h"
32 #include "ia32_map_regs.h"
33 #include "bearch_ia32_t.h"
34
35 #define BLOCK_PREFIX(x) ".L" x
36
37 #define SNPRINTF_BUF_LEN 128
38
39 /* global arch_env for lc_printf functions */
40 static const arch_env_t *arch_env = NULL;
41
42 /** by default, we generate assembler code for the Linux gas */
43 asm_flavour_t asm_flavour = ASM_LINUX_GAS;
44
45 /**
46  * Switch to a new section
47  */
48 void ia32_switch_section(FILE *F, section_t sec) {
49         static section_t curr_sec = NO_SECTION;
50   static const char *text[ASM_MAX][SECTION_MAX] = {
51                 {
52                         ".section\t.text", ".section\t.data", ".section\t.rodata", ".section\t.text"
53                 },
54                 {
55                         ".section\t.text", ".section\t.data", ".section .rdata,\"dr\"", ".section\t.text"
56                 }
57         };
58
59         if (curr_sec == sec)
60                 return;
61
62         curr_sec = sec;
63         switch (sec) {
64
65         case NO_SECTION:
66                 break;
67
68         case SECTION_TEXT:
69         case SECTION_DATA:
70         case SECTION_RODATA:
71         case SECTION_COMMON:
72                 fprintf(F, "\t%s\n", text[asm_flavour][sec]);
73         }
74 }
75
76 static void ia32_dump_function_object(FILE *F, const char *name)
77 {
78         switch (asm_flavour) {
79         case ASM_LINUX_GAS:
80                 fprintf(F, "\t.type\t%s, @function\n", name);
81                 break;
82         case ASM_MINGW_GAS:
83                 fprintf(F, "\t.def\t%s;\t.scl\t2;\t.type\t32;\t.endef\n", name);
84                 break;
85         }
86 }
87
88 static void ia32_dump_function_size(FILE *F, const char *name)
89 {
90         switch (asm_flavour) {
91         case ASM_LINUX_GAS:
92                 fprintf(F, "\t.size\t%s, .-%s\n", name, name);
93                 break;
94         }
95 }
96
97 /*************************************************************
98  *             _       _    __   _          _
99  *            (_)     | |  / _| | |        | |
100  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
101  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
102  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
103  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
104  * | |                                       | |
105  * |_|                                       |_|
106  *************************************************************/
107
108 /**
109  * returns true if a node has x87 registers
110  */
111 static int has_x87_register(const ir_node *n) {
112         return is_irn_machine_user(n, 0);
113 }
114
115 /* We always pass the ir_node which is a pointer. */
116 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
117         return lc_arg_type_ptr;
118 }
119
120
121 /**
122  * Returns the register at in position pos.
123  */
124 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
125         ir_node                *op;
126         const arch_register_t  *reg = NULL;
127
128         assert(get_irn_arity(irn) > pos && "Invalid IN position");
129
130         /* The out register of the operator at position pos is the
131            in register we need. */
132         op = get_irn_n(irn, pos);
133
134         reg = arch_get_irn_register(arch_env, op);
135
136         assert(reg && "no in register found");
137
138         /* in case of unknown: just return a register */
139         if (REGS_ARE_EQUAL(reg, &ia32_gp_regs[REG_GP_UKNWN]))
140                 reg = &ia32_gp_regs[REG_EAX];
141         else if (REGS_ARE_EQUAL(reg, &ia32_xmm_regs[REG_XMM_UKNWN]))
142                 reg = &ia32_xmm_regs[REG_XMM0];
143         else if (REGS_ARE_EQUAL(reg, &ia32_vfp_regs[REG_VFP_UKNWN]))
144                 reg = &ia32_vfp_regs[REG_VF0];
145         else if (REGS_ARE_EQUAL(reg, &ia32_st_regs[REG_ST_UKNWN]))
146                 reg = &ia32_st_regs[REG_ST0];
147
148         return reg;
149 }
150
151 /**
152  * Returns the register at out position pos.
153  */
154 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
155         ir_node                *proj;
156         const arch_register_t  *reg = NULL;
157
158         /* 1st case: irn is not of mode_T, so it has only                 */
159         /*           one OUT register -> good                             */
160         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
161         /*           Proj with the corresponding projnum for the register */
162
163         if (get_irn_mode(irn) != mode_T) {
164                 reg = arch_get_irn_register(arch_env, irn);
165         }
166         else if (is_ia32_irn(irn)) {
167                 reg = get_ia32_out_reg(irn, pos);
168         }
169         else {
170                 const ir_edge_t *edge;
171
172                 foreach_out_edge(irn, edge) {
173                         proj = get_edge_src_irn(edge);
174                         assert(is_Proj(proj) && "non-Proj from mode_T node");
175                         if (get_Proj_proj(proj) == pos) {
176                                 reg = arch_get_irn_register(arch_env, proj);
177                                 break;
178                         }
179                 }
180         }
181
182         assert(reg && "no out register found");
183         return reg;
184 }
185
186 enum io_direction {
187   IN_REG,
188   OUT_REG
189 };
190
191 /**
192  * Returns the name of the in register at position pos.
193  */
194 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
195         const arch_register_t *reg;
196
197         if (in_out == IN_REG) {
198                 reg = get_in_reg(irn, pos);
199
200                 if (reg->reg_class == &ia32_reg_classes[CLASS_ia32_vfp]) {
201                         /* FIXME: works for binop only */
202                         assert(2 <= pos && pos <= 3);
203                         reg = get_ia32_attr(irn)->x87[pos - 2];
204                 }
205         }
206         else {
207                 /* destination address mode nodes don't have outputs */
208                 if (is_ia32_irn(irn) && get_ia32_op_type(irn) == ia32_AddrModeD) {
209                         return "MEM";
210                 }
211
212                 reg = get_out_reg(irn, pos);
213                 if (reg->reg_class == &ia32_reg_classes[CLASS_ia32_vfp])
214                         reg = get_ia32_attr(irn)->x87[pos + 2];
215         }
216         return arch_register_get_name(reg);
217 }
218
219 /**
220  * Get the register name for a node.
221  */
222 static int ia32_get_reg_name(lc_appendable_t *app,
223     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
224 {
225         const char *buf;
226         ir_node    *X  = arg->v_ptr;
227         int         nr = occ->width - 1;
228
229         if (!X)
230                 return lc_appendable_snadd(app, "(null)", 6);
231
232         buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
233
234         /* append the stupid % to register names */
235         lc_appendable_chadd(app, '%');
236         return lc_appendable_snadd(app, buf, strlen(buf));
237 }
238
239 /**
240  * Get the x87 register name for a node.
241  */
242 static int ia32_get_x87_name(lc_appendable_t *app,
243     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
244 {
245         const char *buf;
246         ir_node     *X  = arg->v_ptr;
247         int         nr = occ->width - 1;
248         ia32_attr_t *attr;
249
250         if (!X)
251                 return lc_appendable_snadd(app, "(null)", 6);
252
253         attr = get_ia32_attr(X);
254         buf = attr->x87[nr]->name;
255         lc_appendable_chadd(app, '%');
256         return lc_appendable_snadd(app, buf, strlen(buf));
257 }
258
259 /**
260  * Returns the tarval, offset or scale of an ia32 as a string.
261  */
262 static int ia32_const_to_str(lc_appendable_t *app,
263     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
264 {
265         const char *buf;
266         ir_node    *X = arg->v_ptr;
267
268         if (!X)
269                 return lc_arg_append(app, occ, "(null)", 6);
270
271         if (occ->conversion == 'C') {
272                 buf = get_ia32_cnst(X);
273         }
274         else { /* 'O' */
275                 buf = get_ia32_am_offs(X);
276         }
277
278         return buf ? lc_appendable_snadd(app, buf, strlen(buf)) : 0;
279 }
280
281 /**
282  * Determines the SSE suffix depending on the mode.
283  */
284 static int ia32_get_mode_suffix(lc_appendable_t *app,
285     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
286 {
287         ir_node *X    = arg->v_ptr;
288         ir_mode *mode = get_irn_mode(X);
289
290         if (mode == mode_T) {
291                 mode = is_ia32_AddrModeS(X) || is_ia32_AddrModeD(X) ? get_ia32_ls_mode(X) : get_ia32_res_mode(X);
292         }
293
294         if (!X)
295                 return lc_arg_append(app, occ, "(null)", 6);
296
297         if (mode_is_float(mode)) {
298                 return lc_appendable_chadd(app, get_mode_size_bits(mode) == 32 ? 's' : 'd');
299         }
300         else {
301                 return lc_appendable_chadd(app, mode_is_signed(mode) ? 's' : 'z');
302         }
303 }
304
305 /**
306  * Return the ia32 printf arg environment.
307  * We use the firm environment with some additional handlers.
308  */
309 const lc_arg_env_t *ia32_get_arg_env(void) {
310         static lc_arg_env_t *env = NULL;
311
312         static const lc_arg_handler_t ia32_reg_handler   = { ia32_get_arg_type, ia32_get_reg_name };
313         static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
314         static const lc_arg_handler_t ia32_mode_handler  = { ia32_get_arg_type, ia32_get_mode_suffix };
315         static const lc_arg_handler_t ia32_x87_handler   = { ia32_get_arg_type, ia32_get_x87_name };
316
317         if(env == NULL) {
318                 /* extend the firm printer */
319                 env = firm_get_arg_env();
320
321                 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
322                 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
323                 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
324                 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
325                 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
326                 lc_arg_register(env, "ia32:x87",  'X', &ia32_x87_handler);
327         }
328
329         return env;
330 }
331
332 static char *ia32_get_reg_name_for_mode(ia32_emit_env_t *env, ir_mode *mode, const arch_register_t *reg) {
333         switch(get_mode_size_bits(mode)) {
334                 case 8:
335                         return ia32_get_mapped_reg_name(env->isa->regs_8bit, reg);
336                 case 16:
337                         return ia32_get_mapped_reg_name(env->isa->regs_16bit, reg);
338                 default:
339                         return (char *)arch_register_get_name(reg);
340         }
341 }
342
343 /**
344  * Emits registers and/or address mode of a binary operation.
345  */
346 char *ia32_emit_binop(const ir_node *n, ia32_emit_env_t *env) {
347         static char *buf = NULL;
348
349         /* verify that this function is never called on non-AM supporting operations */
350         //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
351
352 #define PRODUCES_RESULT(n)   \
353         (!(is_ia32_St(n)      || \
354         is_ia32_Store8Bit(n)  || \
355         is_ia32_CondJmp(n)    || \
356         is_ia32_xCondJmp(n)   || \
357         is_ia32_SwitchJmp(n)))
358
359         if (! buf) {
360                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
361         }
362         else {
363                 memset(buf, 0, SNPRINTF_BUF_LEN);
364         }
365
366         switch(get_ia32_op_type(n)) {
367                 case ia32_Normal:
368                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
369                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
370                         }
371                         else {
372                                 const arch_register_t *in1 = get_in_reg(n, 2);
373                                 const arch_register_t *in2 = get_in_reg(n, 3);
374                                 const arch_register_t *out = PRODUCES_RESULT(n) ? get_out_reg(n, 0) : NULL;
375                                 const arch_register_t *in;
376                                 const char            *in_name;
377
378                                 in      = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
379                                 out     = out ? out : in1;
380                                 in_name = arch_register_get_name(in);
381
382                                 if (is_ia32_emit_cl(n)) {
383                                         assert(REGS_ARE_EQUAL(&ia32_gp_regs[REG_ECX], in) && "shift operation needs ecx");
384                                         in_name = "cl";
385                                 }
386
387                                 snprintf(buf, SNPRINTF_BUF_LEN, "%%%s, %%%s", arch_register_get_name(out), in_name);
388                         }
389                         break;
390                 case ia32_AddrModeS:
391                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
392                                 assert(! PRODUCES_RESULT(n) && "Source AM with Const must not produce result");
393                                 snprintf(buf, SNPRINTF_BUF_LEN, "%s, %s", get_ia32_cnst(n), ia32_emit_am(n, env));
394                         }
395                         else {
396                                 if (PRODUCES_RESULT(n)) {
397                                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, ia32_emit_am(n, env));
398                                 }
399                                 else {
400                                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, ia32_emit_am(n, env));
401                                 }
402                         }
403                         break;
404                 case ia32_AddrModeD:
405                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
406                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s,%s%s",
407                                         ia32_emit_am(n, env),
408                                         is_ia32_ImmSymConst(n) ? " OFFSET FLAT:" : " ",  /* In case of a symconst we must add OFFSET to */
409                                         get_ia32_cnst(n));                               /* tell the assembler to store it's address.   */
410                         }
411                         else {
412                                 const arch_register_t *in1 = get_in_reg(n, 2);
413                                 ir_mode              *mode = get_ia32_res_mode(n);
414                                 const char           *in_name;
415
416                                 mode    = mode ? mode : get_ia32_ls_mode(n);
417                                 in_name = ia32_get_reg_name_for_mode(env, mode, in1);
418
419                                 if (is_ia32_emit_cl(n)) {
420                                         assert(REGS_ARE_EQUAL(&ia32_gp_regs[REG_ECX], in1) && "shift operation needs ecx");
421                                         in_name = "cl";
422                                 }
423
424                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %%%s", ia32_emit_am(n, env), in_name);
425                         }
426                         break;
427                 default:
428                         assert(0 && "unsupported op type");
429         }
430
431 #undef PRODUCES_RESULT
432
433         return buf;
434 }
435
436 /**
437  * Emits registers and/or address mode of a binary operation.
438  */
439 char *ia32_emit_x87_binop(const ir_node *n, ia32_emit_env_t *env) {
440         static char *buf = NULL;
441
442         /* verify that this function is never called on non-AM supporting operations */
443         //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
444
445         if (! buf) {
446                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
447         }
448         else {
449                 memset(buf, 0, SNPRINTF_BUF_LEN);
450         }
451
452         switch(get_ia32_op_type(n)) {
453                 case ia32_Normal:
454                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
455                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
456                         }
457                         else {
458                                 ia32_attr_t *attr = get_ia32_attr(n);
459                                 const arch_register_t *in1 = attr->x87[0];
460                                 const arch_register_t *in2 = attr->x87[1];
461                                 const arch_register_t *out = attr->x87[2];
462                                 const arch_register_t *in;
463                                 const char            *in_name;
464
465                                 in      = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
466                                 out     = out ? out : in1;
467                                 in_name = arch_register_get_name(in);
468
469                                 snprintf(buf, SNPRINTF_BUF_LEN, "%%%s, %%%s", arch_register_get_name(out), in_name);
470                         }
471                         break;
472                 case ia32_AddrModeS:
473                 case ia32_AddrModeD:
474                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s", ia32_emit_am(n, env));
475                         break;
476                 default:
477                         assert(0 && "unsupported op type");
478         }
479
480 #undef PRODUCES_RESULT
481
482         return buf;
483 }
484
485 /**
486  * Emits registers and/or address mode of a unary operation.
487  */
488 char *ia32_emit_unop(const ir_node *n, ia32_emit_env_t *env) {
489         static char *buf = NULL;
490
491         if (! buf) {
492                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
493         }
494         else {
495                 memset(buf, 0, SNPRINTF_BUF_LEN);
496         }
497
498         switch(get_ia32_op_type(n)) {
499                 case ia32_Normal:
500                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
501                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%C", n);
502                         }
503                         else {
504                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
505                         }
506                         break;
507                 case ia32_AddrModeD:
508                         snprintf(buf, SNPRINTF_BUF_LEN, "%s", ia32_emit_am(n, env));
509                         break;
510                 default:
511                         assert(0 && "unsupported op type");
512         }
513
514         return buf;
515 }
516
517 /**
518  * Emits address mode.
519  */
520 char *ia32_emit_am(const ir_node *n, ia32_emit_env_t *env) {
521         ia32_am_flavour_t am_flav    = get_ia32_am_flavour(n);
522         int               had_output = 0;
523         char             *s;
524         int               size;
525         static struct obstack *obst  = NULL;
526         ir_mode *mode = get_ia32_ls_mode(n);
527
528         if (! is_ia32_Lea(n))
529                 assert(mode && "AM node must have ls_mode attribute set.");
530
531         if (! obst) {
532                 obst = xcalloc(1, sizeof(*obst));
533         }
534         else {
535                 obstack_free(obst, NULL);
536         }
537
538         /* obstack_free with NULL results in an uninitialized obstack */
539         obstack_init(obst);
540
541         if (mode) {
542                 switch (get_mode_size_bits(mode)) {
543                         case 8:
544                                 obstack_printf(obst, "BYTE PTR ");
545                                 break;
546                         case 16:
547                                 obstack_printf(obst, "WORD PTR ");
548                                 break;
549                         case 32:
550                                 obstack_printf(obst, "DWORD PTR ");
551                                 break;
552                         case 64:
553                                 if (has_x87_register(n))
554                                         /* ARGHHH: stupid gas x87 wants QWORD PTR but SSE must be WITHOUT */
555                                         obstack_printf(obst, "QWORD PTR ");
556                                 break;
557                         case 80:
558                         case 96:
559                                 obstack_printf(obst, "XWORD PTR ");
560                                 break;
561                         default:
562                                 break;
563                 }
564         }
565
566         /* emit address mode symconst */
567         if (get_ia32_am_sc(n)) {
568                 if (is_ia32_am_sc_sign(n))
569                         obstack_printf(obst, "-");
570                 obstack_printf(obst, "%s", get_id_str(get_ia32_am_sc(n)));
571         }
572
573         if (am_flav & ia32_B) {
574                 obstack_printf(obst, "[");
575                 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
576                 had_output = 1;
577         }
578
579         if (am_flav & ia32_I) {
580                 if (had_output) {
581                         obstack_printf(obst, "+");
582                 }
583                 else {
584                         obstack_printf(obst, "[");
585                 }
586
587                 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
588
589                 if (am_flav & ia32_S) {
590                         obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
591                 }
592
593                 had_output = 1;
594         }
595
596         if (am_flav & ia32_O) {
597                 s = get_ia32_am_offs(n);
598
599                 if (s) {
600                         /* omit explicit + if there was no base or index */
601                         if (! had_output) {
602                                 obstack_printf(obst, "[");
603                                 if (s[0] == '+')
604                                         s++;
605                         }
606
607                         obstack_printf(obst, s);
608                         had_output = 1;
609                 }
610         }
611
612         if (had_output)
613                 obstack_printf(obst, "] ");
614
615         size        = obstack_object_size(obst);
616         s           = obstack_finish(obst);
617         s[size - 1] = '\0';
618
619         return s;
620 }
621
622
623
624 /**
625  * Formated print of commands and comments.
626  */
627 static void ia32_fprintf_format(FILE *F, const ir_node *irn, char *cmd_buf, char *cmnt_buf) {
628         unsigned lineno;
629         const char *name = irn ? be_retrieve_dbg_info(get_irn_dbg_info((ir_node *)irn), &lineno) : NULL;
630
631         if (name)
632                 fprintf(F, "\t%-35s %-60s /* %s:%u */\n", cmd_buf, cmnt_buf, name, lineno);
633         else
634                 fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
635 }
636
637
638
639 /**
640  * Add a number to a prefix. This number will not be used a second time.
641  */
642 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
643         static unsigned long id = 0;
644         snprintf(buf, buflen, "%s%lu", prefix, ++id);
645         return buf;
646 }
647
648
649
650 /*************************************************
651  *                 _ _                         _
652  *                (_) |                       | |
653  *   ___ _ __ ___  _| |_    ___ ___  _ __   __| |
654  *  / _ \ '_ ` _ \| | __|  / __/ _ \| '_ \ / _` |
655  * |  __/ | | | | | | |_  | (_| (_) | | | | (_| |
656  *  \___|_| |_| |_|_|\__|  \___\___/|_| |_|\__,_|
657  *
658  *************************************************/
659
660 #undef IA32_DO_EMIT
661 #define IA32_DO_EMIT(irn) ia32_fprintf_format(F, irn, cmd_buf, cmnt_buf)
662
663 /*
664  * coding of conditions
665  */
666 struct cmp2conditon_t {
667         const char *name;
668         pn_Cmp      num;
669 };
670
671 /*
672  * positive conditions for signed compares
673  */
674 static const struct cmp2conditon_t cmp2condition_s[] = {
675   { NULL,              pn_Cmp_False },  /* always false */
676   { "e",               pn_Cmp_Eq },     /* == */
677   { "l",               pn_Cmp_Lt },     /* < */
678   { "le",              pn_Cmp_Le },     /* <= */
679   { "g",               pn_Cmp_Gt },     /* > */
680   { "ge",              pn_Cmp_Ge },     /* >= */
681   { "ne",              pn_Cmp_Lg },     /* != */
682   { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
683   { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
684   { "unordered or ==", pn_Cmp_Ue },     /* Floating point: unordered or == */
685   { "unordered or <",  pn_Cmp_Ul },     /* Floating point: unordered or < */
686   { "unordered or <=", pn_Cmp_Ule },    /* Floating point: unordered or <= */
687   { "unordered or >",  pn_Cmp_Ug },     /* Floating point: unordered or > */
688   { "unordered or >=", pn_Cmp_Uge },    /* Floating point: unordered or >= */
689   { "unordered or !=", pn_Cmp_Ne },     /* Floating point: unordered or != */
690   { NULL,              pn_Cmp_True },   /* always true */
691 };
692
693 /*
694  * positive conditions for unsigned compares
695  */
696 static const struct cmp2conditon_t cmp2condition_u[] = {
697         { NULL,              pn_Cmp_False },  /* always false */
698         { "e",               pn_Cmp_Eq },     /* == */
699         { "b",               pn_Cmp_Lt },     /* < */
700         { "be",              pn_Cmp_Le },     /* <= */
701         { "a",               pn_Cmp_Gt },     /* > */
702         { "ae",              pn_Cmp_Ge },     /* >= */
703         { "ne",              pn_Cmp_Lg },     /* != */
704         { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
705         { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
706         { "unordered or ==", pn_Cmp_Ue },     /* Floating point: unordered or == */
707         { "unordered or <",  pn_Cmp_Ul },     /* Floating point: unordered or < */
708         { "unordered or <=", pn_Cmp_Ule },    /* Floating point: unordered or <= */
709         { "unordered or >",  pn_Cmp_Ug },     /* Floating point: unordered or > */
710         { "unordered or >=", pn_Cmp_Uge },    /* Floating point: unordered or >= */
711         { "unordered or !=", pn_Cmp_Ne },     /* Floating point: unordered or != */
712         { NULL,              pn_Cmp_True },   /* always true */
713 };
714
715 /*
716  * returns the condition code
717  */
718 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
719 {
720         assert(cmp2condition_s[cmp_code].num == cmp_code);
721         assert(cmp2condition_u[cmp_code].num == cmp_code);
722
723         return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
724 }
725
726 /**
727  * Returns the target block for a control flow node.
728  */
729 static ir_node *get_cfop_target_block(const ir_node *irn) {
730         return get_irn_link(irn);
731 }
732
733 /**
734  * Returns the target label for a control flow node.
735  */
736 static char *get_cfop_target(const ir_node *irn, char *buf) {
737         ir_node *bl = get_cfop_target_block(irn);
738
739         snprintf(buf, SNPRINTF_BUF_LEN, BLOCK_PREFIX("%ld"), get_irn_node_nr(bl));
740         return buf;
741 }
742
743 /** Return the next block in Block schedule */
744 static ir_node *next_blk_sched(const ir_node *block) {
745         return get_irn_link(block);
746 }
747
748 /**
749  * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
750  */
751 static void finish_CondJmp(FILE *F, const ir_node *irn, ir_mode *mode) {
752         const ir_node   *proj1, *proj2 = NULL;
753         const ir_node   *block, *next_bl = NULL;
754         const ir_edge_t *edge;
755         char buf[SNPRINTF_BUF_LEN];
756         char cmd_buf[SNPRINTF_BUF_LEN];
757         char cmnt_buf[SNPRINTF_BUF_LEN];
758
759         /* get both Proj's */
760         edge = get_irn_out_edge_first(irn);
761         proj1 = get_edge_src_irn(edge);
762         assert(is_Proj(proj1) && "CondJmp with a non-Proj");
763
764         edge = get_irn_out_edge_next(irn, edge);
765         if (edge) {
766                 proj2 = get_edge_src_irn(edge);
767                 assert(is_Proj(proj2) && "CondJmp with a non-Proj");
768         }
769
770         /* for now, the code works for scheduled and non-schedules blocks */
771         block = get_nodes_block(irn);
772         if (proj2) {
773                 /* we have a block schedule */
774                 next_bl = next_blk_sched(block);
775
776                 if (get_cfop_target_block(proj1) == next_bl) {
777                         /* exchange both proj's so the second one can be omitted */
778                         const ir_node *t = proj1;
779                         proj1 = proj2;
780                         proj2 = t;
781                 }
782         }
783
784         /* the first Proj must always be created */
785         if (get_Proj_proj(proj1) == pn_Cond_true) {
786                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
787                                         get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
788                                         get_cfop_target(proj1, buf));
789                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == TRUE */");
790         }
791         else  {
792                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
793                                         get_cmp_suffix(get_negated_pnc(get_ia32_pncode(irn), mode),
794                                         !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
795                                         get_cfop_target(proj1, buf));
796                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == FALSE */");
797         }
798         IA32_DO_EMIT(irn);
799
800         /* the second Proj might be a fallthrough */
801         if (proj2) {
802                 if (get_cfop_target_block(proj2) != next_bl) {
803                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj2, buf));
804                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* otherwise */");
805                 }
806                 else {
807                         cmd_buf[0] = '\0';
808                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrogh %s */", get_cfop_target(proj2, buf));
809                 }
810                 IA32_DO_EMIT(irn);
811         }
812 }
813
814 /**
815  * Emits code for conditional jump.
816  */
817 static void CondJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
818         FILE *F = env->out;
819         char cmd_buf[SNPRINTF_BUF_LEN];
820         char cmnt_buf[SNPRINTF_BUF_LEN];
821
822         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
823         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
824         IA32_DO_EMIT(irn);
825         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
826 }
827
828 /**
829  * Emits code for conditional jump with two variables.
830  */
831 static void emit_ia32_CondJmp(const ir_node *irn, ia32_emit_env_t *env) {
832         CondJmp_emitter(irn, env);
833 }
834
835 /**
836  * Emits code for conditional test and jump.
837  */
838 static void TestJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
839
840 #define IA32_IS_IMMOP (is_ia32_ImmConst(irn) || is_ia32_ImmSymConst(irn))
841
842         FILE       *F   = env->out;
843         const char *op1 = arch_register_get_name(get_in_reg(irn, 0));
844         const char *op2 = IA32_IS_IMMOP ? get_ia32_cnst(irn) : NULL;
845         char        cmd_buf[SNPRINTF_BUF_LEN];
846         char        cmnt_buf[SNPRINTF_BUF_LEN];
847
848         if (! op2)
849                 op2 = arch_register_get_name(get_in_reg(irn, 1));
850
851         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "test %%%s,%s%s ", op1, IA32_IS_IMMOP ? " " : " %", op2);
852         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
853
854         IA32_DO_EMIT(irn);
855         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
856
857 #undef IA32_IS_IMMOP
858 }
859
860 /**
861  * Emits code for conditional test and jump with two variables.
862  */
863 static void emit_ia32_TestJmp(const ir_node *irn, ia32_emit_env_t *env) {
864         TestJmp_emitter(irn, env);
865 }
866
867 static void emit_ia32_CJmp(const ir_node *irn, ia32_emit_env_t *env) {
868         FILE *F = env->out;
869         char cmd_buf[SNPRINTF_BUF_LEN];
870         char cmnt_buf[SNPRINTF_BUF_LEN];
871
872         snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
873         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test */", irn);
874         IA32_DO_EMIT(irn);
875         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
876 }
877
878 static void emit_ia32_CJmpAM(const ir_node *irn, ia32_emit_env_t *env) {
879         FILE *F = env->out;
880         char cmd_buf[SNPRINTF_BUF_LEN];
881         char cmnt_buf[SNPRINTF_BUF_LEN];
882
883         snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
884         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test/cmp */", irn);
885         IA32_DO_EMIT(irn);
886         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
887 }
888
889 /**
890  * Emits code for conditional x87 floating point jump with two variables.
891  */
892 static void emit_ia32_x87CondJmp(ir_node *irn, ia32_emit_env_t *env) {
893         FILE *F = env->out;
894         char cmd_buf[SNPRINTF_BUF_LEN];
895         char cmnt_buf[SNPRINTF_BUF_LEN];
896         ia32_attr_t *attr = get_ia32_attr(irn);
897         const char *reg = attr->x87[1]->name;
898         const char *instr = "fcom";
899         int reverse = 0;
900
901         switch (get_ia32_pncode(irn)) {
902         case iro_ia32_fcomrJmp:
903                 reverse = 1;
904         case iro_ia32_fcomJmp:
905         default:
906                 instr = "fucom";
907                 break;
908         case iro_ia32_fcomrpJmp:
909                 reverse = 1;
910         case iro_ia32_fcompJmp:
911                 instr = "fucomp";
912                 break;
913         case iro_ia32_fcomrppJmp:
914                 reverse = 1;
915         case iro_ia32_fcomppJmp:
916                 instr = "fucompp";
917                 reg = "";
918                 break;
919         }
920
921         if (reverse)
922                 set_ia32_pncode(irn, (long)get_negated_pnc(get_ia32_pncode(irn), mode_Is));
923
924         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "%s %s", instr, reg);
925         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
926         IA32_DO_EMIT(irn);
927 //      lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fnstsw %3D", irn);
928         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fnstsw %%ax", irn);
929         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* Store x87 FPU Control Word */");
930         IA32_DO_EMIT(irn);
931         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "sahf");
932         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* Store ah into flags */");
933         IA32_DO_EMIT(irn);
934
935         finish_CondJmp(F, irn, mode_Is);
936 }
937
938 /*********************************************************
939  *                 _ _       _
940  *                (_) |     (_)
941  *   ___ _ __ ___  _| |_     _ _   _ _ __ ___  _ __  ___
942  *  / _ \ '_ ` _ \| | __|   | | | | | '_ ` _ \| '_ \/ __|
943  * |  __/ | | | | | | |_    | | |_| | | | | | | |_) \__ \
944  *  \___|_| |_| |_|_|\__|   | |\__,_|_| |_| |_| .__/|___/
945  *                         _/ |               | |
946  *                        |__/                |_|
947  *********************************************************/
948
949 /* jump table entry (target and corresponding number) */
950 typedef struct _branch_t {
951         ir_node *target;
952         int      value;
953 } branch_t;
954
955 /* jump table for switch generation */
956 typedef struct _jmp_tbl_t {
957         ir_node  *defProj;         /**< default target */
958         int       min_value;       /**< smallest switch case */
959         int       max_value;       /**< largest switch case */
960         int       num_branches;    /**< number of jumps */
961         char     *label;           /**< label of the jump table */
962         branch_t *branches;        /**< jump array */
963 } jmp_tbl_t;
964
965 /**
966  * Compare two variables of type branch_t. Used to sort all switch cases
967  */
968 static int ia32_cmp_branch_t(const void *a, const void *b) {
969         branch_t *b1 = (branch_t *)a;
970         branch_t *b2 = (branch_t *)b;
971
972         if (b1->value <= b2->value)
973                 return -1;
974         else
975                 return 1;
976 }
977
978 /**
979  * Emits code for a SwitchJmp (creates a jump table if
980  * possible otherwise a cmp-jmp cascade). Port from
981  * cggg ia32 backend
982  */
983 static void emit_ia32_SwitchJmp(const ir_node *irn, ia32_emit_env_t *emit_env) {
984         unsigned long       interval;
985         char                buf[SNPRINTF_BUF_LEN];
986         int                 last_value, i, pn;
987         jmp_tbl_t           tbl;
988         ir_node            *proj;
989         const ir_edge_t    *edge;
990         const lc_arg_env_t *env = ia32_get_arg_env();
991         FILE               *F   = emit_env->out;
992         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
993
994         /* fill the table structure */
995         tbl.label        = xmalloc(SNPRINTF_BUF_LEN);
996         tbl.label        = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
997         tbl.defProj      = NULL;
998         tbl.num_branches = get_irn_n_edges(irn);
999         tbl.branches     = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
1000         tbl.min_value    = INT_MAX;
1001         tbl.max_value    = INT_MIN;
1002
1003         i = 0;
1004         /* go over all proj's and collect them */
1005         foreach_out_edge(irn, edge) {
1006                 proj = get_edge_src_irn(edge);
1007                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
1008
1009                 pn = get_Proj_proj(proj);
1010
1011                 /* create branch entry */
1012                 tbl.branches[i].target = proj;
1013                 tbl.branches[i].value  = pn;
1014
1015                 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
1016                 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
1017
1018                 /* check for default proj */
1019                 if (pn == get_ia32_pncode(irn)) {
1020                         assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
1021                         tbl.defProj = proj;
1022                 }
1023
1024                 i++;
1025         }
1026
1027         /* sort the branches by their number */
1028         qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
1029
1030         /* two-complement's magic make this work without overflow */
1031         interval = tbl.max_value - tbl.min_value;
1032
1033         /* emit the table */
1034         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, %u", irn, interval);
1035         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* compare for switch */");
1036         IA32_DO_EMIT(irn);
1037
1038         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
1039         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default jump if out of range  */");
1040         IA32_DO_EMIT(irn);
1041
1042         if (tbl.num_branches > 1) {
1043                 /* create table */
1044
1045                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp %s[%1S*4]", tbl.label, irn);
1046                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get jump table entry as target */");
1047                 IA32_DO_EMIT(irn);
1048
1049                 ia32_switch_section(F, SECTION_RODATA);
1050                 fprintf(F, "\t.align 4\n");
1051
1052                 fprintf(F, "%s:\n", tbl.label);
1053
1054                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
1055                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */",  tbl.branches[0].value);
1056                 IA32_DO_EMIT(irn);
1057
1058                 last_value = tbl.branches[0].value;
1059                 for (i = 1; i < tbl.num_branches; ++i) {
1060                         while (++last_value < tbl.branches[i].value) {
1061                                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
1062                                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
1063                                 IA32_DO_EMIT(irn);
1064                         }
1065                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf));
1066                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", last_value);
1067                         IA32_DO_EMIT(irn);
1068                 }
1069                 ia32_switch_section(F, SECTION_TEXT);
1070         }
1071         else {
1072                 /* one jump is enough */
1073                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
1074                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* only one case given */");
1075                 IA32_DO_EMIT(irn);
1076         }
1077
1078         if (tbl.label)
1079                 free(tbl.label);
1080         if (tbl.branches)
1081                 free(tbl.branches);
1082 }
1083
1084 /**
1085  * Emits code for a unconditional jump.
1086  */
1087 static void emit_Jmp(const ir_node *irn, ia32_emit_env_t *env) {
1088         ir_node *block, *next_bl;
1089         FILE *F = env->out;
1090         char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1091
1092         /* for now, the code works for scheduled and non-schedules blocks */
1093         block = get_nodes_block(irn);
1094
1095         /* we have a block schedule */
1096         next_bl = next_blk_sched(block);
1097         if (get_cfop_target_block(irn) != next_bl) {
1098                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf));
1099                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F) */", irn, get_cfop_target_block(irn));
1100         }
1101         else {
1102                 cmd_buf[0] = '\0';
1103                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(irn, buf));
1104         }
1105         IA32_DO_EMIT(irn);
1106 }
1107
1108 /****************************
1109  *                  _
1110  *                 (_)
1111  *  _ __  _ __ ___  _  ___
1112  * | '_ \| '__/ _ \| |/ __|
1113  * | |_) | | | (_) | |\__ \
1114  * | .__/|_|  \___/| ||___/
1115  * | |            _/ |
1116  * |_|           |__/
1117  ****************************/
1118
1119 /**
1120  * Emits code for a proj -> node
1121  */
1122 static void emit_Proj(const ir_node *irn, ia32_emit_env_t *env) {
1123         ir_node *pred = get_Proj_pred(irn);
1124
1125         if (get_irn_op(pred) == op_Start) {
1126                 switch(get_Proj_proj(irn)) {
1127                         case pn_Start_X_initial_exec:
1128                                 emit_Jmp(irn, env);
1129                                 break;
1130                         default:
1131                                 break;
1132                 }
1133         }
1134 }
1135
1136 /**********************************
1137  *   _____                  ____
1138  *  / ____|                |  _ \
1139  * | |     ___  _ __  _   _| |_) |
1140  * | |    / _ \| '_ \| | | |  _ <
1141  * | |___| (_) | |_) | |_| | |_) |
1142  *  \_____\___/| .__/ \__, |____/
1143  *             | |     __/ |
1144  *             |_|    |___/
1145  **********************************/
1146
1147 /**
1148  * Emit movsb/w instructions to make mov count divideable by 4
1149  */
1150 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
1151         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1152
1153         fprintf(F, "\t/* memcopy %d bytes*/\n", size);
1154
1155         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
1156         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward*/");
1157         IA32_DO_EMIT(NULL);
1158
1159         switch(rem) {
1160                 case 1:
1161                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1162                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 1 */");
1163                         break;
1164                 case 2:
1165                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1166                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 2 */");
1167                         break;
1168                 case 3:
1169                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1170                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1171                         IA32_DO_EMIT(NULL);
1172                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1173                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1174                         break;
1175         }
1176
1177         IA32_DO_EMIT(NULL);
1178 }
1179
1180 /**
1181  * Emit rep movsd instruction for memcopy.
1182  */
1183 static void emit_ia32_CopyB(const ir_node *irn, ia32_emit_env_t *emit_env) {
1184         FILE    *F         = emit_env->out;
1185         tarval  *tv        = get_ia32_Immop_tarval(irn);
1186         int      rem       = get_tarval_long(tv);
1187         ir_node *size_node = get_irn_n(irn, 2);
1188         int      size;
1189         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1190
1191         /* beware: size_node could be a be_Copy to fulfill constraints for ecx */
1192         size_node = be_is_Copy(size_node) ? be_get_Copy_op(size_node) : size_node;
1193         size      = get_tarval_long(get_ia32_Immop_tarval(size_node));
1194
1195         emit_CopyB_prolog(F, rem, size);
1196
1197         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "rep movsd");
1198         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy */");
1199         IA32_DO_EMIT(irn);
1200 }
1201
1202 /**
1203  * Emits unrolled memcopy.
1204  */
1205 static void emit_ia32_CopyB_i(const ir_node *irn, ia32_emit_env_t *emit_env) {
1206         tarval *tv   = get_ia32_Immop_tarval(irn);
1207         int     size = get_tarval_long(tv);
1208         FILE   *F    = emit_env->out;
1209         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1210
1211         emit_CopyB_prolog(F, size & 0x3, size);
1212
1213         size >>= 2;
1214         while (size--) {
1215                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsd");
1216                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy unrolled */");
1217                 IA32_DO_EMIT(irn);
1218         }
1219 }
1220
1221
1222
1223 /***************************
1224  *   _____
1225  *  / ____|
1226  * | |     ___  _ ____   __
1227  * | |    / _ \| '_ \ \ / /
1228  * | |___| (_) | | | \ V /
1229  *  \_____\___/|_| |_|\_/
1230  *
1231  ***************************/
1232
1233 /**
1234  * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1235  */
1236 static void emit_ia32_Conv_with_FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1237         FILE               *F        = emit_env->out;
1238         const lc_arg_env_t *env      = ia32_get_arg_env();
1239         ir_mode            *src_mode = get_ia32_src_mode(irn);
1240         ir_mode            *tgt_mode = get_ia32_tgt_mode(irn);
1241         char               *from, *to, buf[64];
1242         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1243
1244         from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
1245         to   = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
1246
1247         switch(get_ia32_op_type(irn)) {
1248                 case ia32_Normal:
1249                         lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
1250                         break;
1251                 case ia32_AddrModeS:
1252                         lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn, emit_env));
1253                         break;
1254                 default:
1255                         assert(0 && "unsupported op type for Conv");
1256         }
1257
1258         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cvt%s2%s %s", from, to, buf);
1259         lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F, %+F) */", irn, src_mode, tgt_mode);
1260         IA32_DO_EMIT(irn);
1261 }
1262
1263 static void emit_ia32_Conv_I2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1264         emit_ia32_Conv_with_FP(irn, emit_env);
1265 }
1266
1267 static void emit_ia32_Conv_FP2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1268         emit_ia32_Conv_with_FP(irn, emit_env);
1269 }
1270
1271 static void emit_ia32_Conv_FP2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1272         emit_ia32_Conv_with_FP(irn, emit_env);
1273 }
1274
1275 /**
1276  * Emits code for an Int conversion.
1277  */
1278 static void emit_ia32_Conv_I2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1279         FILE               *F        = emit_env->out;
1280         const lc_arg_env_t *env      = ia32_get_arg_env();
1281         char               *move_cmd = "movzx";
1282         char               *conv_cmd = NULL;
1283         ir_mode            *src_mode = get_ia32_src_mode(irn);
1284         ir_mode            *tgt_mode = get_ia32_tgt_mode(irn);
1285         int n, m;
1286         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1287         const arch_register_t *in_reg, *out_reg;
1288
1289         n = get_mode_size_bits(src_mode);
1290         m = get_mode_size_bits(tgt_mode);
1291
1292         if (mode_is_signed(n < m ? src_mode : tgt_mode)) {
1293                 move_cmd = "movsx";
1294                 if (n == 8 || m == 8)
1295                         conv_cmd = "cbw";
1296                 else if (n == 16 || m == 16)
1297                         conv_cmd = "cwde";
1298                 else
1299                         assert(0 && "unsupported Conv_I2I");
1300         }
1301
1302         switch(get_ia32_op_type(irn)) {
1303                 case ia32_Normal:
1304                         in_reg  = get_in_reg(irn, 2);
1305                         out_reg = get_out_reg(irn, 0);
1306
1307                         if (REGS_ARE_EQUAL(in_reg, &ia32_gp_regs[REG_EAX]) &&
1308                                 REGS_ARE_EQUAL(out_reg, in_reg)                &&
1309                                 mode_is_signed(n < m ? src_mode : tgt_mode))
1310                         {
1311                                 /* argument and result are both in EAX and */
1312                                 /* signedness is ok: -> use converts       */
1313                                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s", conv_cmd);
1314                         }
1315                         else if (REGS_ARE_EQUAL(out_reg, in_reg) &&
1316                                 ! mode_is_signed(n < m ? src_mode : tgt_mode))
1317                         {
1318                                 /* argument and result are in the same register */
1319                                 /* and signedness is ok: -> use and with mask   */
1320                                 int mask = (1 << (n < m ? n : m)) - 1;
1321                                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "and %1D, 0x%x", irn, mask);
1322                         }
1323                         else {
1324                                 /* use move w/o sign extension */
1325                                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %%%s",
1326                                         move_cmd, irn, ia32_get_reg_name_for_mode(emit_env, n < m ? src_mode : tgt_mode, in_reg));
1327                         }
1328
1329                         break;
1330                 case ia32_AddrModeS:
1331                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %s",
1332                                 move_cmd, irn, ia32_emit_am(irn, emit_env));
1333                         break;
1334                 default:
1335                         assert(0 && "unsupported op type for Conv");
1336         }
1337
1338         lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%d Bit mode_%F -> %d Bit mode_%F) */",
1339                 irn, n, src_mode, m, tgt_mode);
1340
1341         IA32_DO_EMIT(irn);
1342 }
1343
1344 /**
1345  * Emits code for an 8Bit Int conversion.
1346  */
1347 void emit_ia32_Conv_I2I8Bit(const ir_node *irn, ia32_emit_env_t *emit_env) {
1348         emit_ia32_Conv_I2I(irn, emit_env);
1349 }
1350
1351
1352 /*******************************************
1353  *  _                          _
1354  * | |                        | |
1355  * | |__   ___ _ __   ___   __| | ___  ___
1356  * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1357  * | |_) |  __/ | | | (_) | (_| |  __/\__ \
1358  * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1359  *
1360  *******************************************/
1361
1362 /**
1363  * Emits a backend call
1364  */
1365 static void emit_be_Call(const ir_node *irn, ia32_emit_env_t *emit_env) {
1366         FILE *F = emit_env->out;
1367         entity *ent = be_Call_get_entity(irn);
1368         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1369
1370         if (ent) {
1371                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "call %s", get_entity_ld_name(ent));
1372         }
1373         else {
1374                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
1375         }
1376
1377         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
1378
1379         IA32_DO_EMIT(irn);
1380 }
1381
1382 /**
1383  * Emits code to increase stack pointer.
1384  */
1385 static void emit_be_IncSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1386         FILE          *F    = emit_env->out;
1387         unsigned       offs = be_get_IncSP_offset(irn);
1388         be_stack_dir_t dir  = be_get_IncSP_direction(irn);
1389         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1390
1391         if (offs) {
1392                 if (dir == be_stack_dir_expand)
1393                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "sub %1S, %u", irn, offs);
1394                 else
1395                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S, %u", irn, offs);
1396                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (IncSP) */", irn);
1397         }
1398         else {
1399                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1400                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted %+F (IncSP) with 0 */", irn);
1401         }
1402
1403         IA32_DO_EMIT(irn);
1404 }
1405
1406 /**
1407  * Emits code to set stack pointer.
1408  */
1409 static void emit_be_SetSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1410         FILE *F = emit_env->out;
1411         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1412
1413         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1414         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (restore SP) */", irn);
1415         IA32_DO_EMIT(irn);
1416 }
1417
1418 /**
1419  * Emits code for Copy.
1420  */
1421 static void emit_be_Copy(const ir_node *irn, ia32_emit_env_t *emit_env) {
1422         FILE *F = emit_env->out;
1423         const arch_env_t *aenv = emit_env->arch_env;
1424         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1425
1426         if (REGS_ARE_EQUAL(arch_get_irn_register(aenv, irn), arch_get_irn_register(aenv, be_get_Copy_op(irn))))
1427                 return;
1428
1429         if (mode_is_float(get_irn_mode(irn)))
1430                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "movs%M %1D, %1S", irn, irn, irn);
1431         else
1432                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
1433         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1434         IA32_DO_EMIT(irn);
1435 }
1436
1437 /**
1438  * Emits code for exchange.
1439  */
1440 static void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
1441         FILE *F = emit_env->out;
1442         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1443
1444         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1445         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%1A, %2A) */", irn, irn, irn);
1446         IA32_DO_EMIT(irn);
1447 }
1448
1449 /**
1450  * Emits code for Constant loading.
1451  */
1452 static void emit_ia32_Const(const ir_node *n, ia32_emit_env_t *env) {
1453   FILE *F = env->out;
1454   char cmd_buf[256], cmnt_buf[256];
1455   const lc_arg_env_t *arg_env = ia32_get_arg_env();
1456
1457   if (get_ia32_Immop_tarval(n) == get_tarval_null(get_irn_mode(n))) {
1458                 const char *instr = "xor";
1459                 if (env->isa->opt_arch == arch_pentium_4) {
1460                         /* P4 prefers sub r, r, others xor r, r */
1461                         instr = "sub";
1462                 }
1463     lc_esnprintf(arg_env, cmd_buf, 256, "%s %1D, %1D ", instr, n, n);
1464     lc_esnprintf(arg_env, cmnt_buf, 256, "/* optimized mov 0 to register */");
1465   }
1466   else {
1467     if (get_ia32_op_type(n) == ia32_SymConst) {
1468       lc_esnprintf(arg_env, cmd_buf, 256, "mov %1D, OFFSET FLAT:%C ", n, n);
1469       lc_esnprintf(arg_env, cmnt_buf, 256, "/* Move address of SymConst into register */");
1470     }
1471                 else {
1472                                 lc_esnprintf(arg_env, cmd_buf, 256, "mov %1D, %C ", n, n);
1473                                 lc_esnprintf(arg_env, cmnt_buf, 256, "/* Mov Const into register */");
1474                 }
1475   }
1476   lc_efprintf(arg_env, F, "\t%-35s %-60s /* %+F (%+G) */\n", cmd_buf, cmnt_buf, n, n);
1477 }
1478
1479
1480
1481 /***********************************************************************************
1482  *                  _          __                                             _
1483  *                 (_)        / _|                                           | |
1484  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
1485  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1486  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
1487  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
1488  *
1489  ***********************************************************************************/
1490
1491 /**
1492  * Enters the emitter functions for handled nodes into the generic
1493  * pointer of an opcode.
1494  */
1495 static void ia32_register_emitters(void) {
1496
1497 #define IA32_EMIT2(a,b) op_ia32_##a->ops.generic = (op_func)emit_ia32_##b
1498 #define IA32_EMIT(a)    IA32_EMIT2(a,a)
1499 #define EMIT(a)         op_##a->ops.generic = (op_func)emit_##a
1500 #define BE_EMIT(a)      op_be_##a->ops.generic = (op_func)emit_be_##a
1501
1502         /* first clear the generic function pointer for all ops */
1503         clear_irp_opcodes_generic_func();
1504
1505         /* register all emitter functions defined in spec */
1506         ia32_register_spec_emitters();
1507
1508         /* other ia32 emitter functions */
1509         IA32_EMIT(CondJmp);
1510         IA32_EMIT(TestJmp);
1511         IA32_EMIT(CJmp);
1512         IA32_EMIT(CJmpAM);
1513         IA32_EMIT(SwitchJmp);
1514         IA32_EMIT(CopyB);
1515         IA32_EMIT(CopyB_i);
1516         IA32_EMIT(Conv_I2FP);
1517         IA32_EMIT(Conv_FP2I);
1518         IA32_EMIT(Conv_FP2FP);
1519         IA32_EMIT(Conv_I2I);
1520         IA32_EMIT(Conv_I2I8Bit);
1521         IA32_EMIT(Const);
1522         IA32_EMIT2(fcomJmp, x87CondJmp);
1523         IA32_EMIT2(fcompJmp, x87CondJmp);
1524         IA32_EMIT2(fcomppJmp, x87CondJmp);
1525         IA32_EMIT2(fcomrJmp, x87CondJmp);
1526         IA32_EMIT2(fcomrpJmp, x87CondJmp);
1527         IA32_EMIT2(fcomrppJmp, x87CondJmp);
1528
1529         /* benode emitter */
1530         BE_EMIT(Call);
1531         BE_EMIT(IncSP);
1532         BE_EMIT(SetSP);
1533         BE_EMIT(Copy);
1534         BE_EMIT(Perm);
1535
1536         /* firm emitter */
1537         EMIT(Jmp);
1538         EMIT(Proj);
1539
1540 #undef BE_EMIT
1541 #undef EMIT
1542 #undef IA32_EMIT2
1543 #undef IA32_EMIT
1544 }
1545
1546 /**
1547  * Emits code for a node.
1548  */
1549 static void ia32_emit_node(const ir_node *irn, void *env) {
1550         ia32_emit_env_t   *emit_env = env;
1551         FILE              *F        = emit_env->out;
1552         ir_op             *op       = get_irn_op(irn);
1553         DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
1554
1555         DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
1556
1557         if (op->ops.generic) {
1558                 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
1559                 (*emit)(irn, env);
1560         }
1561         else {
1562                 ir_fprintf(F, "\t%35s /* %+F (%+G) */\n", " ", irn, irn);
1563         }
1564 }
1565
1566 /**
1567  * Emits gas alignment directives for Functions dependend on cpu architecture.
1568  */
1569 static void ia32_emit_align_func(FILE *F, cpu_support cpu) {
1570         /* gcc doesn't emit alignment for p4 ?*/
1571     if (cpu == arch_pentium_4)
1572                 return;
1573
1574         fprintf(F, "\t.p2align ");
1575
1576         switch (cpu) {
1577                 case arch_i386:
1578                         /* align 4 bytes, maximum skip 3 bytes */
1579                         fprintf(F, "2,,3");
1580                         break;
1581                 case arch_i486:
1582                         /* align 16 bytes, maximum skip 15 bytes */
1583                         fprintf(F, "4,,15");
1584                         break;
1585                 case arch_k6:
1586                         /* align 32 bytes, maximum skip 31 bytes */
1587                         fprintf(F, "5,,31");
1588                         break;
1589                 default:
1590                         /* align 16 bytes, maximum skip 15 bytes */
1591                         fprintf(F, "4,,15");
1592         }
1593
1594         fprintf(F, "\n");
1595 }
1596
1597 /**
1598  * Emits gas alignment directives for Labels dependend on cpu architecture.
1599  */
1600 static void ia32_emit_align_label(FILE *F, cpu_support cpu) {
1601         /* gcc doesn't emit alignment for p4 ?*/
1602     if (cpu == arch_pentium_4)
1603                 return;
1604
1605         fprintf(F, "\t.p2align ");
1606
1607         switch (cpu) {
1608                 case arch_i386:
1609                         /* align 4 bytes, maximum skip 3 bytes */
1610                         fprintf(F, "2,,3");
1611                         break;
1612                 case arch_i486:
1613                         /* align 16 bytes, maximum skip 15 bytes */
1614                         fprintf(F, "4,,15");
1615                         break;
1616                 case arch_k6:
1617                         /* align 32 bytes, maximum skip 7 bytes */
1618                         fprintf(F, "5,,7");
1619                         break;
1620                 default:
1621                         /* align 16 bytes, maximum skip 7 bytes */
1622                         fprintf(F, "4,,7");
1623         }
1624
1625         fprintf(F, "\n");
1626 }
1627
1628 /**
1629  * Walks over the nodes in a block connected by scheduling edges
1630  * and emits code for each node.
1631  */
1632 static void ia32_gen_block(ir_node *block, void *env) {
1633         ia32_emit_env_t *emit_env = env;
1634         const ir_node *irn;
1635         int need_label = block != get_irg_start_block(get_irn_irg(block));
1636
1637         if (! is_Block(block))
1638                 return;
1639
1640         if (need_label && (emit_env->cg->opt & IA32_OPT_EXTBB)) {
1641                 /* if the extended block scheduler is used, only leader blocks need
1642                    labels. */
1643                 need_label = (block == get_extbb_leader(get_nodes_extbb(block)));
1644         }
1645
1646         if (need_label) {
1647                 ia32_emit_align_label(emit_env->out, emit_env->isa->opt_arch);
1648                 fprintf(emit_env->out, BLOCK_PREFIX("%ld:\n"), get_irn_node_nr(block));
1649         }
1650
1651         sched_foreach(block, irn) {
1652                 ia32_emit_node(irn, env);
1653         }
1654 }
1655
1656 /**
1657  * Emits code for function start.
1658  */
1659 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg, cpu_support cpu) {
1660         entity     *irg_ent  = get_irg_entity(irg);
1661         const char *irg_name = get_entity_ld_name(irg_ent);
1662
1663         fprintf(F, "\n");
1664         ia32_switch_section(F, SECTION_TEXT);
1665         ia32_emit_align_func(F, cpu);
1666         if (get_entity_visibility(irg_ent) == visibility_external_visible) {
1667                 fprintf(F, ".globl %s\n", irg_name);
1668         }
1669         ia32_dump_function_object(F, irg_name);
1670         fprintf(F, "%s:\n", irg_name);
1671 }
1672
1673 /**
1674  * Emits code for function end
1675  */
1676 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1677         const char *irg_name = get_entity_ld_name(get_irg_entity(irg));
1678
1679         fprintf(F, "\tret\n");
1680         ia32_dump_function_size(F, irg_name);
1681         fprintf(F, "\n");
1682 }
1683
1684 /**
1685  * Block-walker:
1686  * Sets labels for control flow nodes (jump target)
1687  * TODO: Jump optimization
1688  */
1689 static void ia32_gen_labels(ir_node *block, void *env) {
1690         ir_node *pred;
1691         int n = get_Block_n_cfgpreds(block);
1692
1693         for (n--; n >= 0; n--) {
1694                 pred = get_Block_cfgpred(block, n);
1695                 set_irn_link(pred, block);
1696         }
1697 }
1698
1699 /**
1700  * Main driver. Emits the code for one routine.
1701  */
1702 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1703         ia32_emit_env_t emit_env;
1704         ir_node *block;
1705
1706         emit_env.out      = F;
1707         emit_env.arch_env = cg->arch_env;
1708         emit_env.cg       = cg;
1709         emit_env.isa      = (ia32_isa_t *)cg->arch_env->isa;
1710         FIRM_DBG_REGISTER(emit_env.mod, "firm.be.ia32.emitter");
1711
1712         /* set the global arch_env (needed by print hooks) */
1713         arch_env = cg->arch_env;
1714
1715         ia32_register_emitters();
1716
1717         ia32_emit_func_prolog(F, irg, emit_env.isa->opt_arch);
1718         irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1719
1720         if ((cg->opt & IA32_OPT_EXTBB) && cg->blk_sched) {
1721                 int i, n = ARR_LEN(cg->blk_sched);
1722
1723                 for (i = 0; i < n;) {
1724                         ir_node *next_bl;
1725
1726                         block   = cg->blk_sched[i];
1727                         ++i;
1728                         next_bl = i < n ? cg->blk_sched[i] : NULL;
1729
1730                         /* set here the link. the emitter expects to find the next block here */
1731                         set_irn_link(block, next_bl);
1732                         ia32_gen_block(block, &emit_env);
1733                 }
1734         }
1735         else {
1736                 /* "normal" block schedule: Note the get_next_block() returns the NUMBER of the block
1737                    in the block schedule. As this number should NEVER be equal the next block,
1738                    we does not need a clear block link here. */
1739                 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1740         }
1741
1742         ia32_emit_func_epilog(F, irg);
1743 }