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