fb11964d7f57e986827fd28e223a8cd3dff899a2
[libfirm] / ir / be / ia32 / ia32_emitter.c
1 /**
2  * This file implements the node emitter.
3  * @author Christian Wuerdig
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 #include "../beabi.h"
27 #include "../be_dbgout.h"
28
29 #include "ia32_emitter.h"
30 #include "gen_ia32_emitter.h"
31 #include "gen_ia32_regalloc_if.h"
32 #include "ia32_nodes_attr.h"
33 #include "ia32_new_nodes.h"
34 #include "ia32_map_regs.h"
35 #include "bearch_ia32_t.h"
36
37 #define BLOCK_PREFIX(x) ".L" x
38
39 #define SNPRINTF_BUF_LEN 128
40
41 /* global arch_env for lc_printf functions */
42 static const arch_env_t *arch_env = NULL;
43
44 /** by default, we generate assembler code for the Linux gas */
45 asm_flavour_t asm_flavour = ASM_LINUX_GAS;
46
47 /**
48  * Switch to a new section
49  */
50 void ia32_switch_section(FILE *F, section_t sec) {
51         static section_t curr_sec = NO_SECTION;
52         static const char *text[ASM_MAX][SECTION_MAX] = {
53                 {
54                         ".section\t.text",
55                         ".section\t.data",
56                         ".section\t.rodata",
57                         ".section\t.text",
58                         ".section\t.tbss,\"awT\",@nobits",
59                         ".section\t.ctors,\"aw\",@progbits"
60                 },
61                 {
62                         ".section\t.text",
63                         ".section\t.data",
64                         ".section .rdata,\"dr\"",
65                         ".section\t.text",
66                         ".section\t.tbss,\"awT\",@nobits",
67                         ".section\t.ctors,\"aw\",@progbits"
68                 }
69         };
70
71         if (curr_sec == sec)
72                 return;
73
74         curr_sec = sec;
75         switch (sec) {
76
77         case NO_SECTION:
78                 break;
79
80         case SECTION_TEXT:
81         case SECTION_DATA:
82         case SECTION_RODATA:
83         case SECTION_COMMON:
84         case SECTION_TLS:
85         case SECTION_CTOR:
86                 fprintf(F, "\t%s\n", text[asm_flavour][sec]);
87                 break;
88
89         default:
90                 break;
91         }
92 }
93
94 static void ia32_dump_function_object(FILE *F, const char *name)
95 {
96         switch (asm_flavour) {
97         case ASM_LINUX_GAS:
98                 fprintf(F, "\t.type\t%s, @function\n", name);
99                 break;
100         case ASM_MINGW_GAS:
101                 fprintf(F, "\t.def\t%s;\t.scl\t2;\t.type\t32;\t.endef\n", name);
102                 break;
103         default:
104                 break;
105         }
106 }
107
108 static void ia32_dump_function_size(FILE *F, const char *name)
109 {
110         switch (asm_flavour) {
111         case ASM_LINUX_GAS:
112                 fprintf(F, "\t.size\t%s, .-%s\n", name, name);
113                 break;
114         default:
115                 break;
116         }
117 }
118
119 /*************************************************************
120  *             _       _    __   _          _
121  *            (_)     | |  / _| | |        | |
122  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
123  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
124  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
125  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
126  * | |                                       | |
127  * |_|                                       |_|
128  *************************************************************/
129
130 static INLINE int be_is_unknown_reg(const arch_register_t *reg) {
131         return \
132                 REGS_ARE_EQUAL(reg, &ia32_gp_regs[REG_GP_UKNWN])   || \
133                 REGS_ARE_EQUAL(reg, &ia32_xmm_regs[REG_XMM_UKNWN]) || \
134                 REGS_ARE_EQUAL(reg, &ia32_vfp_regs[REG_VFP_UKNWN]);
135 }
136
137 /**
138  * returns true if a node has x87 registers
139  */
140 static INLINE int has_x87_register(const ir_node *n) {
141         return is_irn_machine_user(n, 0);
142 }
143
144 /* We always pass the ir_node which is a pointer. */
145 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
146         return lc_arg_type_ptr;
147 }
148
149
150 /**
151  * Returns the register at in position pos.
152  */
153 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
154         ir_node                *op;
155         const arch_register_t  *reg = NULL;
156
157         assert(get_irn_arity(irn) > pos && "Invalid IN position");
158
159         /* The out register of the operator at position pos is the
160            in register we need. */
161         op = get_irn_n(irn, pos);
162
163         reg = arch_get_irn_register(arch_env, op);
164
165         assert(reg && "no in register found");
166
167         /* in case of unknown: just return a register */
168         if (REGS_ARE_EQUAL(reg, &ia32_gp_regs[REG_GP_UKNWN]))
169                 reg = &ia32_gp_regs[REG_EAX];
170         else if (REGS_ARE_EQUAL(reg, &ia32_xmm_regs[REG_XMM_UKNWN]))
171                 reg = &ia32_xmm_regs[REG_XMM0];
172         else if (REGS_ARE_EQUAL(reg, &ia32_vfp_regs[REG_VFP_UKNWN]))
173                 reg = &ia32_vfp_regs[REG_VF0];
174
175         return reg;
176 }
177
178 /**
179  * Returns the register at out position pos.
180  */
181 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
182         ir_node                *proj;
183         const arch_register_t  *reg = NULL;
184
185         /* 1st case: irn is not of mode_T, so it has only                 */
186         /*           one OUT register -> good                             */
187         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
188         /*           Proj with the corresponding projnum for the register */
189
190         if (get_irn_mode(irn) != mode_T) {
191                 reg = arch_get_irn_register(arch_env, irn);
192         }
193         else if (is_ia32_irn(irn)) {
194                 reg = get_ia32_out_reg(irn, pos);
195         }
196         else {
197                 const ir_edge_t *edge;
198
199                 foreach_out_edge(irn, edge) {
200                         proj = get_edge_src_irn(edge);
201                         assert(is_Proj(proj) && "non-Proj from mode_T node");
202                         if (get_Proj_proj(proj) == pos) {
203                                 reg = arch_get_irn_register(arch_env, proj);
204                                 break;
205                         }
206                 }
207         }
208
209         assert(reg && "no out register found");
210         return reg;
211 }
212
213 enum io_direction {
214   IN_REG,
215   OUT_REG
216 };
217
218 /**
219  * Returns the name of the in register at position pos.
220  */
221 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
222         const arch_register_t *reg;
223
224         if (in_out == IN_REG) {
225                 reg = get_in_reg(irn, pos);
226
227                 if (reg->reg_class == &ia32_reg_classes[CLASS_ia32_vfp]) {
228                         /* FIXME: works for binop only */
229                         assert(2 <= pos && pos <= 3);
230                         reg = get_ia32_attr(irn)->x87[pos - 2];
231                 }
232         }
233         else {
234                 /* destination address mode nodes don't have outputs */
235                 if (is_ia32_irn(irn) && get_ia32_op_type(irn) == ia32_AddrModeD) {
236                         return "MEM";
237                 }
238
239                 reg = get_out_reg(irn, pos);
240                 if (reg->reg_class == &ia32_reg_classes[CLASS_ia32_vfp])
241                         reg = get_ia32_attr(irn)->x87[pos + 2];
242         }
243         return arch_register_get_name(reg);
244 }
245
246 /**
247  * Get the register name for a node.
248  */
249 static int ia32_get_reg_name(lc_appendable_t *app,
250     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
251 {
252         const char *buf;
253         ir_node    *irn = arg->v_ptr;
254         int         nr = occ->width - 1;
255
256         if (! irn)
257                 return lc_appendable_snadd(app, "(null)", 6);
258
259         buf = get_ia32_reg_name(irn, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
260
261         /* append the stupid % to register names */
262         lc_appendable_chadd(app, '%');
263         return lc_appendable_snadd(app, buf, strlen(buf));
264 }
265
266 /**
267  * Get the x87 register name for a node.
268  */
269 static int ia32_get_x87_name(lc_appendable_t *app,
270     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
271 {
272         const char *buf;
273         ir_node     *irn = arg->v_ptr;
274         int         nr = occ->width - 1;
275         ia32_attr_t *attr;
276
277         if (! irn)
278                 return lc_appendable_snadd(app, "(null)", 6);
279
280         attr = get_ia32_attr(irn);
281         buf = attr->x87[nr]->name;
282         lc_appendable_chadd(app, '%');
283         return lc_appendable_snadd(app, buf, strlen(buf));
284 }
285
286 /**
287  * Returns the tarval, offset or scale of an ia32 as a string.
288  */
289 static int ia32_const_to_str(lc_appendable_t *app,
290     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
291 {
292         const char *buf;
293         ir_node    *irn = arg->v_ptr;
294
295         if (! irn)
296                 return lc_arg_append(app, occ, "(null)", 6);
297
298         if (occ->conversion == 'C') {
299                 buf = get_ia32_cnst(irn);
300         }
301         else { /* 'O' */
302                 buf = get_ia32_am_offs(irn);
303         }
304
305         return buf ? lc_appendable_snadd(app, buf, strlen(buf)) : 0;
306 }
307
308 /**
309  * Determines the SSE suffix depending on the mode.
310  */
311 static int ia32_get_mode_suffix(lc_appendable_t *app,
312     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
313 {
314         ir_node *irn  = arg->v_ptr;
315         ir_mode *mode = get_irn_mode(irn);
316
317         if (mode == mode_T) {
318                 mode = get_ia32_res_mode(irn);
319                 if (! mode)
320                         mode = get_ia32_ls_mode(irn);
321         }
322
323         if (! irn)
324                 return lc_arg_append(app, occ, "(null)", 6);
325
326         if (mode_is_float(mode)) {
327                 return lc_appendable_chadd(app, get_mode_size_bits(mode) == 32 ? 's' : 'd');
328         }
329         else {
330                 return lc_appendable_chadd(app, mode_is_signed(mode) ? 's' : 'z');
331         }
332 }
333
334 /**
335  * Return the ia32 printf arg environment.
336  * We use the firm environment with some additional handlers.
337  */
338 const lc_arg_env_t *ia32_get_arg_env(void) {
339         static lc_arg_env_t *env = NULL;
340
341         static const lc_arg_handler_t ia32_reg_handler   = { ia32_get_arg_type, ia32_get_reg_name };
342         static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
343         static const lc_arg_handler_t ia32_mode_handler  = { ia32_get_arg_type, ia32_get_mode_suffix };
344         static const lc_arg_handler_t ia32_x87_handler   = { ia32_get_arg_type, ia32_get_x87_name };
345
346         if(env == NULL) {
347                 /* extend the firm printer */
348                 env = firm_get_arg_env();
349
350                 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
351                 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
352                 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
353                 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
354                 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
355                 lc_arg_register(env, "ia32:x87",  'X', &ia32_x87_handler);
356         }
357
358         return env;
359 }
360
361 static const char *ia32_get_reg_name_for_mode(ia32_emit_env_t *env, ir_mode *mode, const arch_register_t *reg) {
362         switch(get_mode_size_bits(mode)) {
363                 case 8:
364                         return ia32_get_mapped_reg_name(env->isa->regs_8bit, reg);
365                 case 16:
366                         return ia32_get_mapped_reg_name(env->isa->regs_16bit, reg);
367                 default:
368                         return (char *)arch_register_get_name(reg);
369         }
370 }
371
372 /**
373  * Emits registers and/or address mode of a binary operation.
374  */
375 const char *ia32_emit_binop(const ir_node *n, ia32_emit_env_t *env) {
376         static char *buf = NULL;
377
378         /* verify that this function is never called on non-AM supporting operations */
379         //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
380
381 #define PRODUCES_RESULT(n)   \
382         (!(is_ia32_St(n)      || \
383         is_ia32_Store8Bit(n)  || \
384         is_ia32_CondJmp(n)    || \
385         is_ia32_xCondJmp(n)   || \
386         is_ia32_CmpSet(n)     || \
387         is_ia32_xCmpSet(n)    || \
388         is_ia32_SwitchJmp(n)))
389
390         if (! buf) {
391                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
392         }
393         else {
394                 memset(buf, 0, SNPRINTF_BUF_LEN);
395         }
396
397         switch(get_ia32_op_type(n)) {
398                 case ia32_Normal:
399                         if (is_ia32_ImmConst(n)) {
400                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
401                         }
402                         else if (is_ia32_ImmSymConst(n)) {
403                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, OFFSET FLAT:%s", n, get_ia32_cnst(n));
404                         }
405                         else {
406                                 const arch_register_t *in1 = get_in_reg(n, 2);
407                                 const arch_register_t *in2 = get_in_reg(n, 3);
408                                 const arch_register_t *out = PRODUCES_RESULT(n) ? get_out_reg(n, 0) : NULL;
409                                 const arch_register_t *in;
410                                 const char            *in_name;
411
412                                 in      = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
413                                 out     = out ? out : in1;
414                                 in_name = arch_register_get_name(in);
415
416                                 if (is_ia32_emit_cl(n)) {
417                                         assert(REGS_ARE_EQUAL(&ia32_gp_regs[REG_ECX], in) && "shift operation needs ecx");
418                                         in_name = "cl";
419                                 }
420
421                                 snprintf(buf, SNPRINTF_BUF_LEN, "%%%s, %%%s", arch_register_get_name(out), in_name);
422                         }
423                         break;
424                 case ia32_AddrModeS:
425                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
426                                 assert(! PRODUCES_RESULT(n) && "Source AM with Const must not produce result");
427                                 snprintf(buf, SNPRINTF_BUF_LEN, "%s, %s", get_ia32_cnst(n), ia32_emit_am(n, env));
428                         }
429                         else {
430                                 if (PRODUCES_RESULT(n)) {
431                                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, ia32_emit_am(n, env));
432                                 }
433                                 else {
434                                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, ia32_emit_am(n, env));
435                                 }
436                         }
437                         break;
438                 case ia32_AddrModeD:
439                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
440                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s,%s%s",
441                                         ia32_emit_am(n, env),
442                                         is_ia32_ImmSymConst(n) ? " OFFSET FLAT:" : " ",  /* In case of a symconst we must add OFFSET to */
443                                         get_ia32_cnst(n));                               /* tell the assembler to store it's address.   */
444                         }
445                         else {
446                                 const arch_register_t *in1 = get_in_reg(n, get_irn_arity(n) == 5 ? 3 : 2);
447                                 ir_mode               *mode = get_ia32_res_mode(n);
448                                 const char            *in_name;
449
450                                 mode    = mode ? mode : get_ia32_ls_mode(n);
451                                 in_name = ia32_get_reg_name_for_mode(env, mode, in1);
452
453                                 if (is_ia32_emit_cl(n)) {
454                                         assert(REGS_ARE_EQUAL(&ia32_gp_regs[REG_ECX], in1) && "shift operation needs ecx");
455                                         in_name = "cl";
456                                 }
457
458                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %%%s", ia32_emit_am(n, env), in_name);
459                         }
460                         break;
461                 default:
462                         assert(0 && "unsupported op type");
463         }
464
465 #undef PRODUCES_RESULT
466
467         return buf;
468 }
469
470 /**
471  * Returns the xxx PTR string for a given mode
472  *
473  * @param mode      the mode
474  * @param x87_insn  if non-zero returns the string for a x87 instruction
475  *                  else for a SSE instruction
476  */
477 static const char *pointer_size(ir_mode *mode, int x87_insn)
478 {
479         if (mode) {
480                 switch (get_mode_size_bits(mode)) {
481                 case 8:  return "BYTE PTR";
482                 case 16: return "WORD PTR";
483                 case 32: return "DWORD PTR";
484                 case 64:
485                         if (x87_insn)
486                                 return "QWORD PTR";
487                         return NULL;
488                 case 80:
489                 case 96: return "XWORD PTR";
490                 default: return NULL;
491                 }
492         }
493         return NULL;
494 }
495
496 /**
497  * Emits registers and/or address mode of a binary operation.
498  */
499 const char *ia32_emit_x87_binop(const ir_node *n, ia32_emit_env_t *env) {
500         static char *buf = NULL;
501
502         /* verify that this function is never called on non-AM supporting operations */
503         //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
504
505         if (! buf) {
506                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
507         }
508         else {
509                 memset(buf, 0, SNPRINTF_BUF_LEN);
510         }
511
512         switch(get_ia32_op_type(n)) {
513                 case ia32_Normal:
514                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
515                                 ir_mode *mode = get_ia32_ls_mode(n);
516                                 const char *p = pointer_size(mode, 1);
517                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s %s", p, get_ia32_cnst(n));
518                         }
519                         else {
520                                 ia32_attr_t *attr = get_ia32_attr(n);
521                                 const arch_register_t *in1 = attr->x87[0];
522                                 const arch_register_t *in2 = attr->x87[1];
523                                 const arch_register_t *out = attr->x87[2];
524                                 const arch_register_t *in;
525                                 const char            *in_name;
526
527                                 in      = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
528                                 out     = out ? out : in1;
529                                 in_name = arch_register_get_name(in);
530
531                                 snprintf(buf, SNPRINTF_BUF_LEN, "%%%s, %%%s", arch_register_get_name(out), in_name);
532                         }
533                         break;
534                 case ia32_AddrModeS:
535                 case ia32_AddrModeD:
536                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s", ia32_emit_am(n, env));
537                         break;
538                 default:
539                         assert(0 && "unsupported op type");
540         }
541
542         return buf;
543 }
544
545 /**
546  * Emits registers and/or address mode of a unary operation.
547  */
548 const char *ia32_emit_unop(const ir_node *n, ia32_emit_env_t *env) {
549         static char *buf = NULL;
550
551         if (! buf) {
552                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
553         }
554         else {
555                 memset(buf, 0, SNPRINTF_BUF_LEN);
556         }
557
558         switch(get_ia32_op_type(n)) {
559                 case ia32_Normal:
560                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
561                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%C", n);
562                         }
563                         else {
564                                 if (is_ia32_MulS(n) || is_ia32_Mulh(n)) {
565                                         /* MulS and Mulh implicitly multiply by EAX */
566                                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%4S", n);
567                                 }
568                                 else
569                                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
570                         }
571                         break;
572                 case ia32_AddrModeD:
573                         snprintf(buf, SNPRINTF_BUF_LEN, "%s", ia32_emit_am(n, env));
574                         break;
575                 case ia32_AddrModeS:
576                         /*
577                                 Mulh is emitted via emit_unop
578                                 imul [MEM]  means EDX:EAX <- EAX * [MEM]
579                         */
580                         assert((is_ia32_Mulh(n) || is_ia32_MulS(n)) && "Only MulS and Mulh can have AM source as unop");
581                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s", ia32_emit_am(n, env));
582                         break;
583                 default:
584                         assert(0 && "unsupported op type");
585         }
586
587         return buf;
588 }
589
590 /**
591  * Emits address mode.
592  */
593 const char *ia32_emit_am(const ir_node *n, ia32_emit_env_t *env) {
594         ia32_am_flavour_t am_flav    = get_ia32_am_flavour(n);
595         int               had_output = 0;
596         char              *s;
597         const char        *p;
598         static struct obstack *obst  = NULL;
599         ir_mode *mode = get_ia32_ls_mode(n);
600
601         if (! is_ia32_Lea(n))
602                 assert(mode && "AM node must have ls_mode attribute set.");
603
604         if (! obst) {
605                 obst = xcalloc(1, sizeof(*obst));
606         }
607         else {
608                 obstack_free(obst, NULL);
609         }
610
611         /* obstack_free with NULL results in an uninitialized obstack */
612         obstack_init(obst);
613
614         p = pointer_size(mode, has_x87_register(n) || is_ia32_GetST0(n) || is_ia32_SetST0(n));
615         if (p)
616                 obstack_printf(obst, "%s ", p);
617
618         /* emit address mode symconst */
619         if (get_ia32_am_sc(n)) {
620                 if (is_ia32_am_sc_sign(n))
621                         obstack_printf(obst, "-");
622                 obstack_printf(obst, "%s", get_id_str(get_ia32_am_sc(n)));
623         }
624
625         if (am_flav & ia32_B) {
626                 obstack_printf(obst, "[");
627                 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
628                 had_output = 1;
629         }
630
631         if (am_flav & ia32_I) {
632                 if (had_output) {
633                         obstack_printf(obst, "+");
634                 }
635                 else {
636                         obstack_printf(obst, "[");
637                 }
638
639                 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
640
641                 if (am_flav & ia32_S) {
642                         obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
643                 }
644
645                 had_output = 1;
646         }
647
648         if (am_flav & ia32_O) {
649                 s = get_ia32_am_offs(n);
650
651                 if (s) {
652                         /* omit explicit + if there was no base or index */
653                         if (! had_output) {
654                                 obstack_printf(obst, "[");
655                                 if (s[0] == '+')
656                                         s++;
657                         }
658
659                         obstack_printf(obst, s);
660                         had_output = 1;
661                 }
662         }
663
664         if (had_output)
665                 obstack_printf(obst, "] ");
666
667         obstack_1grow(obst, '\0');
668         s = obstack_finish(obst);
669
670         return s;
671 }
672
673 /**
674  * emit an address
675  */
676 const char *ia32_emit_adr(const ir_node *irn, ia32_emit_env_t *env)
677 {
678         static char buf[SNPRINTF_BUF_LEN];
679         ir_mode    *mode = get_ia32_ls_mode(irn);
680         const char *adr  = get_ia32_cnst(irn);
681         const char *pref = pointer_size(mode, has_x87_register(irn));
682
683         snprintf(buf, SNPRINTF_BUF_LEN, "%s %s", pref ? pref : "", adr);
684         return buf;
685 }
686
687 /**
688  * Formated print of commands and comments.
689  */
690 static void ia32_fprintf_format(FILE *F, const ir_node *irn, char *cmd_buf, char *cmnt_buf) {
691         unsigned lineno;
692         const char *name = irn ? be_retrieve_dbg_info(get_irn_dbg_info((ir_node *)irn), &lineno) : NULL;
693
694         if (name)
695                 fprintf(F, "\t%-35s %-60s /* %s:%u */\n", cmd_buf, cmnt_buf, name, lineno);
696         else
697                 fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
698 }
699
700
701
702 /**
703  * Add a number to a prefix. This number will not be used a second time.
704  */
705 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
706         static unsigned long id = 0;
707         snprintf(buf, buflen, "%s%lu", prefix, ++id);
708         return buf;
709 }
710
711
712
713 /*************************************************
714  *                 _ _                         _
715  *                (_) |                       | |
716  *   ___ _ __ ___  _| |_    ___ ___  _ __   __| |
717  *  / _ \ '_ ` _ \| | __|  / __/ _ \| '_ \ / _` |
718  * |  __/ | | | | | | |_  | (_| (_) | | | | (_| |
719  *  \___|_| |_| |_|_|\__|  \___\___/|_| |_|\__,_|
720  *
721  *************************************************/
722
723 #undef IA32_DO_EMIT
724 #define IA32_DO_EMIT(irn) ia32_fprintf_format(F, irn, cmd_buf, cmnt_buf)
725
726 /*
727  * coding of conditions
728  */
729 struct cmp2conditon_t {
730         const char *name;
731         pn_Cmp      num;
732 };
733
734 /*
735  * positive conditions for signed compares
736  */
737 static const struct cmp2conditon_t cmp2condition_s[] = {
738   { NULL,              pn_Cmp_False },  /* always false */
739   { "e",               pn_Cmp_Eq },     /* == */
740   { "l",               pn_Cmp_Lt },     /* < */
741   { "le",              pn_Cmp_Le },     /* <= */
742   { "g",               pn_Cmp_Gt },     /* > */
743   { "ge",              pn_Cmp_Ge },     /* >= */
744   { "ne",              pn_Cmp_Lg },     /* != */
745   { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
746   { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
747   { "e",               pn_Cmp_Ue },     /* Floating point: unordered or == */
748   { "b",               pn_Cmp_Ul },     /* Floating point: unordered or < */
749   { "be",              pn_Cmp_Ule },    /* Floating point: unordered or <= */
750   { "a",               pn_Cmp_Ug },     /* Floating point: unordered or > */
751   { "ae",              pn_Cmp_Uge },    /* Floating point: unordered or >= */
752   { "ne",              pn_Cmp_Ne },     /* Floating point: unordered or != */
753   { NULL,              pn_Cmp_True },   /* always true */
754 };
755
756 /*
757  * positive conditions for unsigned compares
758  */
759 static const struct cmp2conditon_t cmp2condition_u[] = {
760         { NULL,              pn_Cmp_False },  /* always false */
761         { "e",               pn_Cmp_Eq },     /* == */
762         { "b",               pn_Cmp_Lt },     /* < */
763         { "be",              pn_Cmp_Le },     /* <= */
764         { "a",               pn_Cmp_Gt },     /* > */
765         { "ae",              pn_Cmp_Ge },     /* >= */
766         { "ne",              pn_Cmp_Lg },     /* != */
767         { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
768         { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
769         { "e",               pn_Cmp_Ue },     /* Floating point: unordered or == */
770         { "b",               pn_Cmp_Ul },     /* Floating point: unordered or < */
771         { "be",              pn_Cmp_Ule },    /* Floating point: unordered or <= */
772         { "a",               pn_Cmp_Ug },     /* Floating point: unordered or > */
773         { "ae",              pn_Cmp_Uge },    /* Floating point: unordered or >= */
774         { "ne",              pn_Cmp_Ne },     /* Floating point: unordered or != */
775         { NULL,              pn_Cmp_True },   /* always true */
776 };
777
778 /*
779  * returns the condition code
780  */
781 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
782 {
783         assert(cmp2condition_s[cmp_code].num == cmp_code);
784         assert(cmp2condition_u[cmp_code].num == cmp_code);
785
786         return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
787 }
788
789 /**
790  * Returns the target block for a control flow node.
791  */
792 static ir_node *get_cfop_target_block(const ir_node *irn) {
793         return get_irn_link(irn);
794 }
795
796 /**
797  * Returns the target label for a control flow node.
798  */
799 static char *get_cfop_target(const ir_node *irn, char *buf) {
800         ir_node *bl = get_cfop_target_block(irn);
801
802         snprintf(buf, SNPRINTF_BUF_LEN, BLOCK_PREFIX("%ld"), get_irn_node_nr(bl));
803         return buf;
804 }
805
806 /** Return the next block in Block schedule */
807 static ir_node *next_blk_sched(const ir_node *block) {
808         return get_irn_link(block);
809 }
810
811 /**
812  * Returns the Proj with projection number proj and NOT mode_M
813  */
814 static ir_node *get_proj(const ir_node *irn, long proj) {
815         const ir_edge_t *edge;
816         ir_node         *src;
817
818         assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
819
820         foreach_out_edge(irn, edge) {
821                 src = get_edge_src_irn(edge);
822
823                 assert(is_Proj(src) && "Proj expected");
824                 if (get_irn_mode(src) == mode_M)
825                         continue;
826
827                 if (get_Proj_proj(src) == proj)
828                         return src;
829         }
830         return NULL;
831 }
832
833 /**
834  * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
835  */
836 static void finish_CondJmp(FILE *F, const ir_node *irn, ir_mode *mode) {
837         const ir_node *proj_true;
838         const ir_node *proj_false;
839         const ir_node *block;
840         const ir_node *next_block;
841         char buf[SNPRINTF_BUF_LEN];
842         char cmd_buf[SNPRINTF_BUF_LEN];
843         char cmnt_buf[SNPRINTF_BUF_LEN];
844         int is_unsigned;
845         int pnc;
846         int flipped = 0;
847
848         /* get both Proj's */
849         proj_true = get_proj(irn, pn_Cond_true);
850         assert(proj_true && "CondJmp without true Proj");
851
852         proj_false = get_proj(irn, pn_Cond_false);
853         assert(proj_false && "CondJmp without false Proj");
854
855         pnc = get_ia32_pncode(irn);
856
857         /* for now, the code works for scheduled and non-schedules blocks */
858         block = get_nodes_block(irn);
859
860         /* we have a block schedule */
861         next_block = next_blk_sched(block);
862
863         if (get_cfop_target_block(proj_true) == next_block) {
864                 /* exchange both proj's so the second one can be omitted */
865                 const ir_node *t = proj_true;
866                 proj_true = proj_false;
867                 proj_false = t;
868
869                 flipped = 1;
870                 pnc = get_negated_pnc(pnc, mode);
871         }
872
873         /* the first Proj must always be created */
874         is_unsigned = mode_is_float(mode) || ! mode_is_signed(mode);
875         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
876                  get_cmp_suffix(pnc, is_unsigned),
877                  get_cfop_target(proj_true, buf));
878         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* %s(a, b) %s*/",
879                  get_pnc_string(pnc), flipped ? "(was flipped)" : "");
880         IA32_DO_EMIT(irn);
881
882         /* the second Proj might be a fallthrough */
883         if (get_cfop_target_block(proj_false) != next_block) {
884                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj_false, buf));
885                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* otherwise */");
886         }
887         else {
888                 cmd_buf[0] = '\0';
889                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(proj_false, buf));
890         }
891         IA32_DO_EMIT(irn);
892 }
893
894 /**
895  * Emits code for conditional jump.
896  */
897 static void CondJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
898         FILE *F = env->out;
899         char cmd_buf[SNPRINTF_BUF_LEN];
900         char cmnt_buf[SNPRINTF_BUF_LEN];
901
902         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
903         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
904         IA32_DO_EMIT(irn);
905         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
906 }
907
908 /**
909  * Emits code for conditional jump with two variables.
910  */
911 static void emit_ia32_CondJmp(const ir_node *irn, ia32_emit_env_t *env) {
912         CondJmp_emitter(irn, env);
913 }
914
915 /**
916  * Emits code for conditional test and jump.
917  */
918 static void TestJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
919
920 #define IA32_IS_IMMOP (is_ia32_ImmConst(irn) || is_ia32_ImmSymConst(irn))
921
922         FILE       *F   = env->out;
923         const char *op1 = arch_register_get_name(get_in_reg(irn, 0));
924         const char *op2 = IA32_IS_IMMOP ? get_ia32_cnst(irn) : NULL;
925         char        cmd_buf[SNPRINTF_BUF_LEN];
926         char        cmnt_buf[SNPRINTF_BUF_LEN];
927
928         if (! op2)
929                 op2 = arch_register_get_name(get_in_reg(irn, 1));
930
931         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "test %%%s,%s%s ", op1, IA32_IS_IMMOP ? " " : " %", op2);
932         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
933
934         IA32_DO_EMIT(irn);
935         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
936
937 #undef IA32_IS_IMMOP
938 }
939
940 /**
941  * Emits code for conditional test and jump with two variables.
942  */
943 static void emit_ia32_TestJmp(const ir_node *irn, ia32_emit_env_t *env) {
944         TestJmp_emitter(irn, env);
945 }
946
947 static void emit_ia32_CJmp(const ir_node *irn, ia32_emit_env_t *env) {
948         FILE *F = env->out;
949         char cmd_buf[SNPRINTF_BUF_LEN];
950         char cmnt_buf[SNPRINTF_BUF_LEN];
951
952         snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
953         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test */", irn);
954         IA32_DO_EMIT(irn);
955         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
956 }
957
958 static void emit_ia32_CJmpAM(const ir_node *irn, ia32_emit_env_t *env) {
959         FILE *F = env->out;
960         char cmd_buf[SNPRINTF_BUF_LEN];
961         char cmnt_buf[SNPRINTF_BUF_LEN];
962
963         snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
964         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test/cmp */", irn);
965         IA32_DO_EMIT(irn);
966         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
967 }
968
969 /**
970  * Emits code for conditional SSE floating point jump with two variables.
971  */
972 static void emit_ia32_xCondJmp(ir_node *irn, ia32_emit_env_t *env) {
973         FILE *F = env->out;
974         char cmd_buf[SNPRINTF_BUF_LEN];
975         char cmnt_buf[SNPRINTF_BUF_LEN];
976
977         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ucomis%M %s", irn, ia32_emit_binop(irn, env));
978         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
979         IA32_DO_EMIT(irn);
980         finish_CondJmp(F, irn, mode_F);
981
982 }
983
984 /**
985  * Emits code for conditional x87 floating point jump with two variables.
986  */
987 static void emit_ia32_x87CondJmp(ir_node *irn, ia32_emit_env_t *env) {
988         FILE *F = env->out;
989         char cmd_buf[SNPRINTF_BUF_LEN];
990         char cmnt_buf[SNPRINTF_BUF_LEN];
991         ia32_attr_t *attr = get_ia32_attr(irn);
992         const char *reg = attr->x87[1]->name;
993         const char *instr = "fcom";
994         int reverse = 0;
995
996         switch (get_ia32_irn_opcode(irn)) {
997         case iro_ia32_fcomrJmp:
998                 reverse = 1;
999         case iro_ia32_fcomJmp:
1000         default:
1001                 instr = "fucom";
1002                 break;
1003         case iro_ia32_fcomrpJmp:
1004                 reverse = 1;
1005         case iro_ia32_fcompJmp:
1006                 instr = "fucomp";
1007                 break;
1008         case iro_ia32_fcomrppJmp:
1009                 reverse = 1;
1010         case iro_ia32_fcomppJmp:
1011                 instr = "fucompp";
1012                 reg = "";
1013                 break;
1014         }
1015
1016         if (reverse)
1017                 set_ia32_pncode(irn, (long)get_inversed_pnc(get_ia32_pncode(irn)));
1018
1019         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "%s %s%s", instr, reg[0] == '\0' ? "" : "%", reg);
1020         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1021         IA32_DO_EMIT(irn);
1022         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fnstsw %%ax", irn);
1023         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* Store x87 FPU Control Word */");
1024         IA32_DO_EMIT(irn);
1025         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "sahf");
1026         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* Store ah into flags */");
1027         IA32_DO_EMIT(irn);
1028
1029         /* the compare flags must be evaluated using carry , ie unsigned */
1030         finish_CondJmp(F, irn, mode_Iu);
1031 }
1032
1033 static void CMov_emitter(ir_node *irn, ia32_emit_env_t *env) {
1034         FILE               *F       = env->out;
1035         const lc_arg_env_t *arg_env = ia32_get_arg_env();
1036         ir_mode    *mode       = get_irn_mode(get_irn_n(irn, 0));
1037         int        is_unsigned = mode_is_float(mode) || ! mode_is_signed(mode);
1038         const char *cmp_suffix = get_cmp_suffix(get_ia32_pncode(irn), is_unsigned);
1039         int is_PsiCondCMov     = is_ia32_PsiCondCMov(irn);
1040         int idx_left  = 2 - is_PsiCondCMov;
1041         int idx_right = 3 - is_PsiCondCMov;
1042
1043         char cmd_buf[SNPRINTF_BUF_LEN];
1044         char cmnt_buf[SNPRINTF_BUF_LEN];
1045         const arch_register_t *in1, *in2, *out;
1046
1047         out = arch_get_irn_register(env->arch_env, irn);
1048         in1 = arch_get_irn_register(env->arch_env, get_irn_n(irn, idx_left));
1049         in2 = arch_get_irn_register(env->arch_env, get_irn_n(irn, idx_right));
1050
1051         /* we have to emit the cmp first, because the destination register */
1052         /* could be one of the compare registers                           */
1053         if (is_ia32_CmpCMov(irn)) {
1054                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, %2S", irn, irn);
1055         }
1056         else if (is_ia32_xCmpCMov(irn)) {
1057                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ucomis%M %1S, %2S", get_irn_n(irn, 0), irn, irn);
1058         }
1059         else if (is_PsiCondCMov) {
1060                 /* omit compare because flags are already set by And/Or */
1061                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "test %1S, %1S", irn, irn);
1062         }
1063         else {
1064                 assert(0 && "unsupported CMov");
1065         }
1066         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* Psi condition */" );
1067         IA32_DO_EMIT(irn);
1068
1069         if (REGS_ARE_EQUAL(out, in2)) {
1070                 /* best case: default in == out -> do nothing */
1071         }
1072         else if (REGS_ARE_EQUAL(out, in1)) {
1073                 /* true in == out -> need complement compare and exchange true and default in */
1074                 ir_node *t = get_irn_n(irn, idx_left);
1075                 set_irn_n(irn, idx_left, get_irn_n(irn, idx_right));
1076                 set_irn_n(irn, idx_right, t);
1077
1078                 cmp_suffix  = get_cmp_suffix(get_negated_pnc(get_ia32_pncode(irn), get_irn_mode(irn)), is_unsigned);
1079
1080         }
1081         else {
1082                 /* out is different from in: need copy default -> out */
1083                 if (is_PsiCondCMov)
1084                         lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1085                 else
1086                         lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %4S", irn, irn);
1087
1088                 lc_esnprintf(arg_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* copy default -> out */" );
1089                 IA32_DO_EMIT(irn);
1090         }
1091
1092         if (is_PsiCondCMov)
1093                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "cmov%s %1D, %2S", cmp_suffix, irn, irn);
1094         else
1095                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "cmov%s %1D, %3S", cmp_suffix, irn, irn);
1096
1097         lc_esnprintf(arg_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* condition is true case */" );
1098         IA32_DO_EMIT(irn);
1099 }
1100
1101 static void emit_ia32_CmpCMov(ir_node *irn, ia32_emit_env_t *env) {
1102         CMov_emitter(irn, env);
1103 }
1104
1105 static void emit_ia32_PsiCondCMov(ir_node *irn, ia32_emit_env_t *env) {
1106         CMov_emitter(irn, env);
1107 }
1108
1109 static void emit_ia32_xCmpCMov(ir_node *irn, ia32_emit_env_t *env) {
1110         CMov_emitter(irn, env);
1111 }
1112
1113 static void Set_emitter(ir_node *irn, ir_mode *mode, ia32_emit_env_t *env) {
1114         FILE               *F       = env->out;
1115         const lc_arg_env_t *arg_env = ia32_get_arg_env();
1116         int        is_unsigned = mode_is_float(mode) || ! mode_is_signed(mode);
1117         const char *cmp_suffix = get_cmp_suffix(get_ia32_pncode(irn), is_unsigned);
1118         const char *reg8bit;
1119
1120         char cmd_buf[SNPRINTF_BUF_LEN];
1121         char cmnt_buf[SNPRINTF_BUF_LEN];
1122         const arch_register_t *out;
1123
1124         out     = arch_get_irn_register(env->arch_env, irn);
1125         reg8bit = ia32_get_mapped_reg_name(env->isa->regs_8bit, out);
1126
1127         if (is_ia32_CmpSet(irn)) {
1128                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
1129         }
1130         else if (is_ia32_xCmpSet(irn)) {
1131                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ucomis%M %s", get_irn_n(irn, 2), ia32_emit_binop(irn, env));
1132         }
1133         else if (is_ia32_PsiCondSet(irn)) {
1134                 /* omit compare because flags are already set by And/Or */
1135                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1136         }
1137         else {
1138                 assert(0 && "unsupported Set");
1139         }
1140         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* calculate Psi condition */" );
1141         IA32_DO_EMIT(irn);
1142
1143         /* use mov to clear target because it doesn't affect the eflags */
1144         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "mov %%%s, 0", arch_register_get_name(out));
1145         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* clear target as set modifies only lower 8 bit */");
1146         IA32_DO_EMIT(irn);
1147
1148         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "set%s %%%s", cmp_suffix, reg8bit);
1149         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* set 1 iff true, 0 otherweise */" );
1150         IA32_DO_EMIT(irn);
1151 }
1152
1153 static void emit_ia32_CmpSet(ir_node *irn, ia32_emit_env_t *env) {
1154         Set_emitter(irn, get_irn_mode(get_irn_n(irn, 2)), env);
1155 }
1156
1157 static void emit_ia32_PsiCondSet(ir_node *irn, ia32_emit_env_t *env) {
1158         Set_emitter(irn, get_irn_mode(get_irn_n(irn, 0)), env);
1159 }
1160
1161 static void emit_ia32_xCmpSet(ir_node *irn, ia32_emit_env_t *env) {
1162         Set_emitter(irn, get_irn_mode(get_irn_n(irn, 2)), env);
1163 }
1164
1165 static void emit_ia32_xCmp(ir_node *irn, ia32_emit_env_t *env) {
1166         FILE               *F       = env->out;
1167         const lc_arg_env_t *arg_env = ia32_get_arg_env();
1168         int                sse_pnc  = -1;
1169         long               pnc      = get_ia32_pncode(irn);
1170         long               unord    = pnc & pn_Cmp_Uo;
1171         char cmd_buf[SNPRINTF_BUF_LEN];
1172         char cmnt_buf[SNPRINTF_BUF_LEN];
1173
1174         switch (pnc) {
1175                 case pn_Cmp_Leg: /* odered */
1176                         sse_pnc = 7;
1177                         break;
1178                 case pn_Cmp_Uo:  /* unordered */
1179                         sse_pnc = 3;
1180                         break;
1181                 case pn_Cmp_Ue:
1182                 case pn_Cmp_Eq:  /* == */
1183                         sse_pnc = 0;
1184                         break;
1185                 case pn_Cmp_Ul:
1186                 case pn_Cmp_Lt:  /* < */
1187                         sse_pnc = 1;
1188                         break;
1189                 case pn_Cmp_Ule:
1190                 case pn_Cmp_Le: /* <= */
1191                         sse_pnc = 2;
1192                         break;
1193                 case pn_Cmp_Ug:
1194                 case pn_Cmp_Gt:  /* > */
1195                         sse_pnc = 6;
1196                         break;
1197                 case pn_Cmp_Uge:
1198                 case pn_Cmp_Ge: /* >= */
1199                         sse_pnc = 5;
1200                         break;
1201                 case pn_Cmp_Ne:
1202                 case pn_Cmp_Lg:  /* != */
1203                         sse_pnc = 4;
1204                         break;
1205         }
1206
1207         assert(sse_pnc >= 0 && "unsupported compare");
1208
1209         if (unord && sse_pnc != 3) {
1210                 /*
1211                         We need a separate compare against unordered.
1212                         Quick and Dirty solution:
1213                         - get some memory on stack
1214                         - compare
1215                         - store result
1216                         - compare
1217                         - and result and stored result
1218                     - cleanup stack
1219                 */
1220                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "sub %%esp, 8");
1221                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* reserve some space for unordered compare result */");
1222                 IA32_DO_EMIT(NULL);
1223                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmpsd %s, 3", ia32_emit_binop(irn, env));
1224                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* SSE compare: unordered */");
1225                 IA32_DO_EMIT(NULL);
1226                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "movsd [%%esp], %1D", irn);
1227                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* store compare result */");
1228                 IA32_DO_EMIT(NULL);
1229         }
1230
1231         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmpsd %s, %d", ia32_emit_binop(irn, env), sse_pnc);
1232         lc_esnprintf(arg_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* SSE compare (%+F) with result in %1D */", irn, irn);
1233         IA32_DO_EMIT(irn);
1234
1235         if (unord && sse_pnc != 3) {
1236                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "andpd %1D, [%%esp]", irn);
1237                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* build the final result */");
1238                 IA32_DO_EMIT(NULL);
1239                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "add %%esp, 8");
1240                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* free allocated space */");
1241                 IA32_DO_EMIT(NULL);
1242         }
1243 }
1244
1245 /*********************************************************
1246  *                 _ _       _
1247  *                (_) |     (_)
1248  *   ___ _ __ ___  _| |_     _ _   _ _ __ ___  _ __  ___
1249  *  / _ \ '_ ` _ \| | __|   | | | | | '_ ` _ \| '_ \/ __|
1250  * |  __/ | | | | | | |_    | | |_| | | | | | | |_) \__ \
1251  *  \___|_| |_| |_|_|\__|   | |\__,_|_| |_| |_| .__/|___/
1252  *                         _/ |               | |
1253  *                        |__/                |_|
1254  *********************************************************/
1255
1256 /* jump table entry (target and corresponding number) */
1257 typedef struct _branch_t {
1258         ir_node *target;
1259         int      value;
1260 } branch_t;
1261
1262 /* jump table for switch generation */
1263 typedef struct _jmp_tbl_t {
1264         ir_node  *defProj;         /**< default target */
1265         int       min_value;       /**< smallest switch case */
1266         int       max_value;       /**< largest switch case */
1267         int       num_branches;    /**< number of jumps */
1268         char     *label;           /**< label of the jump table */
1269         branch_t *branches;        /**< jump array */
1270 } jmp_tbl_t;
1271
1272 /**
1273  * Compare two variables of type branch_t. Used to sort all switch cases
1274  */
1275 static int ia32_cmp_branch_t(const void *a, const void *b) {
1276         branch_t *b1 = (branch_t *)a;
1277         branch_t *b2 = (branch_t *)b;
1278
1279         if (b1->value <= b2->value)
1280                 return -1;
1281         else
1282                 return 1;
1283 }
1284
1285 /**
1286  * Emits code for a SwitchJmp (creates a jump table if
1287  * possible otherwise a cmp-jmp cascade). Port from
1288  * cggg ia32 backend
1289  */
1290 static void emit_ia32_SwitchJmp(const ir_node *irn, ia32_emit_env_t *emit_env) {
1291         unsigned long       interval;
1292         char                buf[SNPRINTF_BUF_LEN];
1293         int                 last_value, i, pn;
1294         jmp_tbl_t           tbl;
1295         ir_node            *proj;
1296         const ir_edge_t    *edge;
1297         const lc_arg_env_t *env = ia32_get_arg_env();
1298         FILE               *F   = emit_env->out;
1299         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1300
1301         /* fill the table structure */
1302         tbl.label        = xmalloc(SNPRINTF_BUF_LEN);
1303         tbl.label        = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, ".TBL_");
1304         tbl.defProj      = NULL;
1305         tbl.num_branches = get_irn_n_edges(irn);
1306         tbl.branches     = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
1307         tbl.min_value    = INT_MAX;
1308         tbl.max_value    = INT_MIN;
1309
1310         i = 0;
1311         /* go over all proj's and collect them */
1312         foreach_out_edge(irn, edge) {
1313                 proj = get_edge_src_irn(edge);
1314                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
1315
1316                 pn = get_Proj_proj(proj);
1317
1318                 /* create branch entry */
1319                 tbl.branches[i].target = proj;
1320                 tbl.branches[i].value  = pn;
1321
1322                 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
1323                 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
1324
1325                 /* check for default proj */
1326                 if (pn == get_ia32_pncode(irn)) {
1327                         assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
1328                         tbl.defProj = proj;
1329                 }
1330
1331                 i++;
1332         }
1333
1334         /* sort the branches by their number */
1335         qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
1336
1337         /* two-complement's magic make this work without overflow */
1338         interval = tbl.max_value - tbl.min_value;
1339
1340         /* emit the table */
1341         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, %u", irn, interval);
1342         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* compare for switch */");
1343         IA32_DO_EMIT(irn);
1344
1345         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
1346         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default jump if out of range  */");
1347         IA32_DO_EMIT(irn);
1348
1349         if (tbl.num_branches > 1) {
1350                 /* create table */
1351
1352                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp %s[%1S*4]", tbl.label, irn);
1353                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get jump table entry as target */");
1354                 IA32_DO_EMIT(irn);
1355
1356                 ia32_switch_section(F, SECTION_RODATA);
1357                 fprintf(F, "\t.align 4\n");
1358
1359                 fprintf(F, "%s:\n", tbl.label);
1360
1361                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
1362                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */",  tbl.branches[0].value);
1363                 IA32_DO_EMIT(irn);
1364
1365                 last_value = tbl.branches[0].value;
1366                 for (i = 1; i < tbl.num_branches; ++i) {
1367                         while (++last_value < tbl.branches[i].value) {
1368                                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
1369                                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
1370                                 IA32_DO_EMIT(irn);
1371                         }
1372                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf));
1373                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", last_value);
1374                         IA32_DO_EMIT(irn);
1375                 }
1376                 ia32_switch_section(F, SECTION_TEXT);
1377         }
1378         else {
1379                 /* one jump is enough */
1380                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
1381                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* only one case given */");
1382                 IA32_DO_EMIT(irn);
1383         }
1384
1385         if (tbl.label)
1386                 free(tbl.label);
1387         if (tbl.branches)
1388                 free(tbl.branches);
1389 }
1390
1391 /**
1392  * Emits code for a unconditional jump.
1393  */
1394 static void emit_Jmp(const ir_node *irn, ia32_emit_env_t *env) {
1395         ir_node *block, *next_bl;
1396         FILE *F = env->out;
1397         char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1398
1399         /* for now, the code works for scheduled and non-schedules blocks */
1400         block = get_nodes_block(irn);
1401
1402         /* we have a block schedule */
1403         next_bl = next_blk_sched(block);
1404         if (get_cfop_target_block(irn) != next_bl) {
1405                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf));
1406                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F) */", irn, get_cfop_target_block(irn));
1407         }
1408         else {
1409                 cmd_buf[0] = '\0';
1410                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(irn, buf));
1411         }
1412         IA32_DO_EMIT(irn);
1413 }
1414
1415 /****************************
1416  *                  _
1417  *                 (_)
1418  *  _ __  _ __ ___  _  ___
1419  * | '_ \| '__/ _ \| |/ __|
1420  * | |_) | | | (_) | |\__ \
1421  * | .__/|_|  \___/| ||___/
1422  * | |            _/ |
1423  * |_|           |__/
1424  ****************************/
1425
1426 /**
1427  * Emits code for a proj -> node
1428  */
1429 static void emit_Proj(const ir_node *irn, ia32_emit_env_t *env) {
1430         ir_node *pred = get_Proj_pred(irn);
1431
1432         if (get_irn_op(pred) == op_Start) {
1433                 switch(get_Proj_proj(irn)) {
1434                         case pn_Start_X_initial_exec:
1435                                 emit_Jmp(irn, env);
1436                                 break;
1437                         default:
1438                                 break;
1439                 }
1440         }
1441 }
1442
1443 /**********************************
1444  *   _____                  ____
1445  *  / ____|                |  _ \
1446  * | |     ___  _ __  _   _| |_) |
1447  * | |    / _ \| '_ \| | | |  _ <
1448  * | |___| (_) | |_) | |_| | |_) |
1449  *  \_____\___/| .__/ \__, |____/
1450  *             | |     __/ |
1451  *             |_|    |___/
1452  **********************************/
1453
1454 /**
1455  * Emit movsb/w instructions to make mov count divideable by 4
1456  */
1457 static void emit_CopyB_prolog(FILE *F, const ir_node *irn, int rem) {
1458         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1459
1460         ir_fprintf(F, "\t/* memcopy prolog %+F */\n", irn);
1461
1462         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
1463         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward */");
1464
1465         switch(rem) {
1466                 case 1:
1467                         IA32_DO_EMIT(NULL);
1468                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1469                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 1 */");
1470                         break;
1471                 case 2:
1472                         IA32_DO_EMIT(NULL);
1473                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1474                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 2 */");
1475                         break;
1476                 case 3:
1477                         IA32_DO_EMIT(NULL);
1478                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1479                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1480                         IA32_DO_EMIT(NULL);
1481                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1482                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1483                         break;
1484         }
1485
1486         IA32_DO_EMIT(NULL);
1487 }
1488
1489 /**
1490  * Emit rep movsd instruction for memcopy.
1491  */
1492 static void emit_ia32_CopyB(const ir_node *irn, ia32_emit_env_t *emit_env) {
1493         FILE   *F  = emit_env->out;
1494         tarval *tv = get_ia32_Immop_tarval(irn);
1495         int    rem = get_tarval_long(tv);
1496         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1497
1498         emit_CopyB_prolog(F, irn, rem);
1499
1500         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "rep movsd");
1501         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy */");
1502         IA32_DO_EMIT(irn);
1503 }
1504
1505 /**
1506  * Emits unrolled memcopy.
1507  */
1508 static void emit_ia32_CopyB_i(const ir_node *irn, ia32_emit_env_t *emit_env) {
1509         tarval *tv   = get_ia32_Immop_tarval(irn);
1510         int     size = get_tarval_long(tv);
1511         FILE   *F    = emit_env->out;
1512         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1513
1514         emit_CopyB_prolog(F, irn, size & 0x3);
1515
1516         size >>= 2;
1517         while (size--) {
1518                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsd");
1519                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy unrolled */");
1520                 IA32_DO_EMIT(irn);
1521         }
1522 }
1523
1524
1525
1526 /***************************
1527  *   _____
1528  *  / ____|
1529  * | |     ___  _ ____   __
1530  * | |    / _ \| '_ \ \ / /
1531  * | |___| (_) | | | \ V /
1532  *  \_____\___/|_| |_|\_/
1533  *
1534  ***************************/
1535
1536 /**
1537  * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1538  */
1539 static void emit_ia32_Conv_with_FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1540         FILE               *F        = emit_env->out;
1541         const lc_arg_env_t *env      = ia32_get_arg_env();
1542         ir_mode            *src_mode = get_ia32_src_mode(irn);
1543         ir_mode            *tgt_mode = get_ia32_tgt_mode(irn);
1544         char               *from, *to, buf[64];
1545         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1546
1547         from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
1548         to   = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
1549
1550         switch(get_ia32_op_type(irn)) {
1551                 case ia32_Normal:
1552                         lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
1553                         break;
1554                 case ia32_AddrModeS:
1555                         lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn, emit_env));
1556                         break;
1557                 default:
1558                         assert(0 && "unsupported op type for Conv");
1559         }
1560
1561         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cvt%s2%s %s", from, to, buf);
1562         lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F, %+F) */", irn, src_mode, tgt_mode);
1563         IA32_DO_EMIT(irn);
1564 }
1565
1566 static void emit_ia32_Conv_I2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1567         emit_ia32_Conv_with_FP(irn, emit_env);
1568 }
1569
1570 static void emit_ia32_Conv_FP2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1571         emit_ia32_Conv_with_FP(irn, emit_env);
1572 }
1573
1574 static void emit_ia32_Conv_FP2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1575         emit_ia32_Conv_with_FP(irn, emit_env);
1576 }
1577
1578 /**
1579  * Emits code for an Int conversion.
1580  */
1581 static void emit_ia32_Conv_I2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1582         FILE               *F        = emit_env->out;
1583         const lc_arg_env_t *env      = ia32_get_arg_env();
1584         char               *move_cmd = "movzx";
1585         char               *conv_cmd = NULL;
1586         ir_mode            *src_mode = get_ia32_src_mode(irn);
1587         ir_mode            *tgt_mode = get_ia32_tgt_mode(irn);
1588         int n, m;
1589         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1590         const arch_register_t *in_reg, *out_reg;
1591
1592         n = get_mode_size_bits(src_mode);
1593         m = get_mode_size_bits(tgt_mode);
1594
1595         if (mode_is_signed(n < m ? src_mode : tgt_mode)) {
1596                 move_cmd = "movsx";
1597                 if (n == 8 || m == 8)
1598                         conv_cmd = "cbw";
1599                 else if (n == 16 || m == 16)
1600                         conv_cmd = "cwde";
1601                 else {
1602                         printf("%d -> %d unsupported\n", n, m);
1603                         assert(0 && "unsupported Conv_I2I");
1604                 }
1605         }
1606
1607          switch(get_ia32_op_type(irn)) {
1608                 case ia32_Normal:
1609                         in_reg  = get_in_reg(irn, 2);
1610                         out_reg = get_out_reg(irn, 0);
1611
1612                         if (REGS_ARE_EQUAL(in_reg, &ia32_gp_regs[REG_EAX]) &&
1613                                 REGS_ARE_EQUAL(out_reg, in_reg)                &&
1614                                 mode_is_signed(n < m ? src_mode : tgt_mode))
1615                         {
1616                                 /* argument and result are both in EAX and */
1617                                 /* signedness is ok: -> use converts       */
1618                                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s", conv_cmd);
1619                         }
1620                         else if (REGS_ARE_EQUAL(out_reg, in_reg) &&
1621                                 ! mode_is_signed(n < m ? src_mode : tgt_mode))
1622                         {
1623                                 /* argument and result are in the same register */
1624                                 /* and signedness is ok: -> use and with mask   */
1625                                 int mask = (1 << (n < m ? n : m)) - 1;
1626                                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "and %1D, 0x%x", irn, mask);
1627                         }
1628                         else {
1629                                 /* use move w/o sign extension */
1630                                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %%%s",
1631                                         move_cmd, irn, ia32_get_reg_name_for_mode(emit_env, n < m ? src_mode : tgt_mode, in_reg));
1632                         }
1633
1634                         break;
1635                 case ia32_AddrModeS:
1636                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %s",
1637                                 move_cmd, irn, ia32_emit_am(irn, emit_env));
1638                         break;
1639                 default:
1640                         assert(0 && "unsupported op type for Conv");
1641         }
1642
1643         lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%d Bit mode_%F -> %d Bit mode_%F) */",
1644                 irn, n, src_mode, m, tgt_mode);
1645
1646         IA32_DO_EMIT(irn);
1647 }
1648
1649 /**
1650  * Emits code for an 8Bit Int conversion.
1651  */
1652 void emit_ia32_Conv_I2I8Bit(const ir_node *irn, ia32_emit_env_t *emit_env) {
1653         emit_ia32_Conv_I2I(irn, emit_env);
1654 }
1655
1656
1657 /*******************************************
1658  *  _                          _
1659  * | |                        | |
1660  * | |__   ___ _ __   ___   __| | ___  ___
1661  * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1662  * | |_) |  __/ | | | (_) | (_| |  __/\__ \
1663  * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1664  *
1665  *******************************************/
1666
1667 /**
1668  * Emits a backend call
1669  */
1670 static void emit_be_Call(const ir_node *irn, ia32_emit_env_t *emit_env) {
1671         FILE *F = emit_env->out;
1672         entity *ent = be_Call_get_entity(irn);
1673         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1674
1675         if (ent) {
1676                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "call %s", get_entity_ld_name(ent));
1677         }
1678         else {
1679                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "call %1D", get_irn_n(irn, be_pos_Call_ptr));
1680         }
1681
1682         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
1683
1684         IA32_DO_EMIT(irn);
1685 }
1686
1687 /**
1688  * Emits code to increase stack pointer.
1689  */
1690 static void emit_be_IncSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1691         FILE          *F    = emit_env->out;
1692         int offs = be_get_IncSP_offset(irn);
1693         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1694
1695         if (offs) {
1696                 if (offs > 0)
1697                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "sub %1S, %u", irn, offs);
1698                 else
1699                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S, %u", irn, -offs);
1700                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (IncSP) */", irn);
1701         }
1702         else {
1703                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1704                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted %+F (IncSP) with 0 */", irn);
1705         }
1706
1707         IA32_DO_EMIT(irn);
1708 }
1709
1710 /**
1711  * Emits code to set stack pointer.
1712  */
1713 static void emit_be_SetSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1714         FILE *F = emit_env->out;
1715         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1716
1717         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1718         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (restore SP) */", irn);
1719         IA32_DO_EMIT(irn);
1720 }
1721
1722 /**
1723  * Emits code for Copy/CopyKeep.
1724  */
1725 static void Copy_emitter(const ir_node *irn, ir_node *op, ia32_emit_env_t *emit_env) {
1726         FILE             *F    = emit_env->out;
1727         const arch_env_t *aenv = emit_env->arch_env;
1728         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1729
1730         if (REGS_ARE_EQUAL(arch_get_irn_register(aenv, irn), arch_get_irn_register(aenv, op)) ||
1731                 be_is_unknown_reg(arch_get_irn_register(aenv, op)))
1732                 return;
1733
1734         if (mode_is_float(get_irn_mode(irn)))
1735                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "movs%M %1D, %1S", irn, irn, irn);
1736         else
1737                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
1738         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1739         IA32_DO_EMIT(irn);
1740 }
1741
1742 static void emit_be_Copy(const ir_node *irn, ia32_emit_env_t *emit_env) {
1743         Copy_emitter(irn, be_get_Copy_op(irn), emit_env);
1744 }
1745
1746 static void emit_be_CopyKeep(const ir_node *irn, ia32_emit_env_t *emit_env) {
1747         Copy_emitter(irn, be_get_CopyKeep_op(irn), emit_env);
1748 }
1749
1750 /**
1751  * Emits code for exchange.
1752  */
1753 static void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
1754         FILE *F = emit_env->out;
1755         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1756         const arch_register_t *in1, *in2;
1757         const arch_register_class_t *cls1, *cls2;
1758
1759         in1 = arch_get_irn_register(emit_env->arch_env, get_irn_n(irn, 0));
1760         in2 = arch_get_irn_register(emit_env->arch_env, get_irn_n(irn, 1));
1761
1762         cls1 = arch_register_get_class(in1);
1763         cls2 = arch_register_get_class(in2);
1764
1765         assert(cls1 == cls2 && "Register class mismatch at Perm");
1766
1767         if (cls1 == &ia32_reg_classes[CLASS_ia32_gp]) {
1768                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1769         }
1770         else if (cls1 == &ia32_reg_classes[CLASS_ia32_xmm]) {
1771                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN,
1772                         "pxor %1S, %2S\n\tpxor %2S, %1S\n\tpxor %1S, %2S", irn, irn, irn, irn, irn, irn);
1773         }
1774         else if (cls1 == &ia32_reg_classes[CLASS_ia32_vfp]) {
1775                 /* is a NOP */
1776                 cmd_buf[0] = '\0';
1777         }
1778         else if (cls1 == &ia32_reg_classes[CLASS_ia32_st]) {
1779                 /* is a NOP */
1780                 cmd_buf[0] = '\0';
1781         }
1782
1783         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%1A, %2A) */", irn, irn, irn);
1784         IA32_DO_EMIT(irn);
1785 }
1786
1787 /**
1788  * Emits code for Constant loading.
1789  */
1790 static void emit_ia32_Const(const ir_node *n, ia32_emit_env_t *env) {
1791         FILE *F = env->out;
1792         char cmd_buf[256], cmnt_buf[256];
1793         const lc_arg_env_t *arg_env = ia32_get_arg_env();
1794         ir_mode *mode = get_irn_mode(n);
1795         tarval *tv = get_ia32_Immop_tarval(n);
1796
1797         if (get_ia32_op_type(n) == ia32_SymConst) {
1798                 lc_esnprintf(arg_env, cmd_buf, 256, "mov %1D, OFFSET FLAT:%C ", n, n);
1799                 lc_esnprintf(arg_env, cmnt_buf, 256, "/* Move address of SymConst into register */");
1800         } else {
1801                 assert(mode == get_tarval_mode(tv));
1802                 /* beware: in some rare cases mode is mode_b which has no tarval_null() */
1803                 if (tv == get_tarval_b_false() || tv == get_tarval_null(mode)) {
1804                         const char *instr = "xor";
1805                         if (env->isa->opt_arch == arch_pentium_4) {
1806                                 /* P4 prefers sub r, r, others xor r, r */
1807                                 instr = "sub";
1808                         }
1809                         lc_esnprintf(arg_env, cmd_buf, 256, "%s %1D, %1D ", instr, n, n);
1810                         lc_esnprintf(arg_env, cmnt_buf, 256, "/* optimized mov 0 to register */");
1811                 } else {
1812                         lc_esnprintf(arg_env, cmd_buf, 256, "mov %1D, %C ", n, n);
1813                         lc_esnprintf(arg_env, cmnt_buf, 256, "/* Mov Const into register */");
1814                 }
1815         }
1816         lc_efprintf(arg_env, F, "\t%-35s %-60s /* %+F (%+G) */\n", cmd_buf, cmnt_buf, n, n);
1817 }
1818
1819 /**
1820  * Emits code to increase stack pointer.
1821  */
1822 static void emit_ia32_AddSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1823         FILE *F = emit_env->out;
1824         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1825
1826         if (is_ia32_ImmConst(irn)) {
1827                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "sub %1D, %C", irn, irn);
1828         }
1829         else if (is_ia32_ImmSymConst(irn)) {
1830                 if (get_ia32_op_type(irn) == ia32_Normal)
1831                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "sub %1D, OFFSET_FLAT:%C", irn, irn);
1832                 else /* source address mode */
1833                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "sub %1D, [%s%s]", irn, get_id_str(get_ia32_am_sc(irn)), get_ia32_am_offs(irn));
1834         }
1835         else {
1836                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "sub %1D, %2S", irn, irn);
1837         }
1838         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* reserve space on stack */");
1839
1840         IA32_DO_EMIT(irn);
1841 }
1842
1843 /**
1844  * Emits code to increase stack pointer.
1845  */
1846 static void emit_ia32_SubSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1847         FILE *F = emit_env->out;
1848         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1849
1850         if (is_ia32_ImmConst(irn)) {
1851                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1D, %C", irn, irn);
1852         }
1853         else if (is_ia32_ImmSymConst(irn)) {
1854                 if (get_ia32_op_type(irn) == ia32_Normal)
1855                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1D, OFFSET_FLAT:%C", irn, irn);
1856                 else /* source address mode */
1857                         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1D, [%s%s]", irn, get_id_str(get_ia32_am_sc(irn)), get_ia32_am_offs(irn));
1858         }
1859         else {
1860                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1D, %2S", irn, irn);
1861         }
1862         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* free space on stack */");
1863
1864         IA32_DO_EMIT(irn);
1865 }
1866
1867 /**
1868  * Emits code to load the TLS base
1869  */
1870 static void emit_ia32_LdTls(const ir_node *irn, ia32_emit_env_t *emit_env) {
1871         FILE *F = emit_env->out;
1872         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1873
1874         switch (asm_flavour) {
1875         case ASM_LINUX_GAS:
1876                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, DWORD PTR %%gs:0", irn);
1877                 break;
1878         case ASM_MINGW_GAS:
1879                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, DWORD PTR %%gs:0", irn);
1880                 break;
1881         default:
1882                 assert(0 && "unsupported TLS");
1883                 break;
1884         }
1885         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get thread local storage base */");
1886
1887         IA32_DO_EMIT(irn);
1888 }
1889
1890 static void emit_be_Return(const ir_node *n, ia32_emit_env_t *env) {
1891         FILE *F = env->out;
1892         const lc_arg_env_t *arg_env = ia32_get_arg_env();
1893
1894         lc_efprintf(arg_env, F, "\t%-35s %-60s /* %+F (%+G) */\n", "ret", "/* be_Return */", n, n);
1895 }
1896
1897 static void emit_Nothing(const ir_node *n, ia32_emit_env_t *env) {
1898         FILE *F = env->out;
1899
1900         ir_fprintf(F, "\t%35s /* %+F (%+G) */\n", " ", n, n);
1901 }
1902
1903
1904 /***********************************************************************************
1905  *                  _          __                                             _
1906  *                 (_)        / _|                                           | |
1907  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
1908  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1909  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
1910  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
1911  *
1912  ***********************************************************************************/
1913
1914 /**
1915  * Enters the emitter functions for handled nodes into the generic
1916  * pointer of an opcode.
1917  */
1918 static void ia32_register_emitters(void) {
1919
1920 #define IA32_EMIT2(a,b) op_ia32_##a->ops.generic = (op_func)emit_ia32_##b
1921 #define IA32_EMIT(a)    IA32_EMIT2(a,a)
1922 #define EMIT(a)         op_##a->ops.generic = (op_func)emit_##a
1923 #define IGN(a)                  op_##a->ops.generic = (op_func)emit_Nothing
1924 #define BE_EMIT(a)      op_be_##a->ops.generic = (op_func)emit_be_##a
1925 #define BE_IGN(a)               op_be_##a->ops.generic = (op_func)emit_Nothing
1926
1927         /* first clear the generic function pointer for all ops */
1928         clear_irp_opcodes_generic_func();
1929
1930         /* register all emitter functions defined in spec */
1931         ia32_register_spec_emitters();
1932
1933         /* other ia32 emitter functions */
1934         IA32_EMIT(CondJmp);
1935         IA32_EMIT(TestJmp);
1936         IA32_EMIT(CJmp);
1937         IA32_EMIT(CJmpAM);
1938         IA32_EMIT(CmpCMov);
1939         IA32_EMIT(PsiCondCMov);
1940         IA32_EMIT(CmpSet);
1941         IA32_EMIT(PsiCondSet);
1942         IA32_EMIT(SwitchJmp);
1943         IA32_EMIT(CopyB);
1944         IA32_EMIT(CopyB_i);
1945         IA32_EMIT(Conv_I2FP);
1946         IA32_EMIT(Conv_FP2I);
1947         IA32_EMIT(Conv_FP2FP);
1948         IA32_EMIT(Conv_I2I);
1949         IA32_EMIT(Conv_I2I8Bit);
1950         IA32_EMIT(Const);
1951         IA32_EMIT(AddSP);
1952         IA32_EMIT(SubSP);
1953         IA32_EMIT(LdTls);
1954         IA32_EMIT(xCmp);
1955         IA32_EMIT(xCmpSet);
1956         IA32_EMIT(xCmpCMov);
1957         IA32_EMIT(xCondJmp);
1958         IA32_EMIT2(fcomJmp, x87CondJmp);
1959         IA32_EMIT2(fcompJmp, x87CondJmp);
1960         IA32_EMIT2(fcomppJmp, x87CondJmp);
1961         IA32_EMIT2(fcomrJmp, x87CondJmp);
1962         IA32_EMIT2(fcomrpJmp, x87CondJmp);
1963         IA32_EMIT2(fcomrppJmp, x87CondJmp);
1964
1965         /* benode emitter */
1966         BE_EMIT(Call);
1967         BE_EMIT(IncSP);
1968         BE_EMIT(SetSP);
1969         BE_EMIT(Copy);
1970         BE_EMIT(CopyKeep);
1971         BE_EMIT(Perm);
1972         BE_EMIT(Return);
1973
1974         BE_IGN(RegParams);
1975         BE_IGN(Barrier);
1976         BE_IGN(Keep);
1977
1978         /* firm emitter */
1979         EMIT(Jmp);
1980         EMIT(Proj);
1981         IGN(Phi);
1982         IGN(Start);
1983
1984 #undef BE_EMIT
1985 #undef EMIT
1986 #undef IGN
1987 #undef IA32_EMIT2
1988 #undef IA32_EMIT
1989 }
1990
1991 static const char *last_name = NULL;
1992 static unsigned last_line = -1;
1993 static unsigned num = -1;
1994
1995 /**
1996  * Emit the debug support for node irn.
1997  */
1998 static void ia32_emit_dbg(const ir_node *irn, ia32_emit_env_t *env) {
1999         dbg_info *db = get_irn_dbg_info(irn);
2000         unsigned lineno;
2001         const char *fname = be_retrieve_dbg_info(db, &lineno);
2002
2003         if (fname) {
2004                 if (last_name != fname) {
2005                         last_line = -1;
2006                         be_dbg_include_begin(env->cg->birg->main_env->db_handle, fname);
2007                         last_name = fname;
2008                 }
2009                 if (last_line != lineno) {
2010                         char name[64];
2011                         FILE *F = env->out;
2012
2013                         snprintf(name, sizeof(name), ".LM%u", ++num);
2014                         last_line = lineno;
2015                         be_dbg_line(env->cg->birg->main_env->db_handle, lineno, name);
2016                         fprintf(F, "%s:\n", name);
2017                 }
2018         }
2019 }
2020
2021 /**
2022  * Emits code for a node.
2023  */
2024 static void ia32_emit_node(const ir_node *irn, void *env) {
2025         ia32_emit_env_t   *emit_env = env;
2026         ir_op             *op       = get_irn_op(irn);
2027         DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
2028
2029         DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
2030
2031         if (op->ops.generic) {
2032                 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
2033                 ia32_emit_dbg(irn, emit_env);
2034                 (*emit)(irn, env);
2035         }
2036         else {
2037                 emit_Nothing(irn, env);
2038                 ir_fprintf(stderr, "Warning: No emit handler for node %+F (%+G)\n", irn, irn);
2039         }
2040 }
2041
2042 /**
2043  * Emits gas alignment directives
2044  */
2045 static void ia32_emit_alignment(FILE *F, unsigned align, unsigned skip) {
2046         fprintf(F, "\t.p2align %u,,%u\n", align, skip);
2047 }
2048
2049 /**
2050  * Emits gas alignment directives for Functions depended on cpu architecture.
2051  */
2052 static void ia32_emit_align_func(FILE *F, cpu_support cpu) {
2053         unsigned align; unsigned maximum_skip;
2054
2055         switch (cpu) {
2056                 case arch_i386:
2057                         align = 2;
2058                         break;
2059                 case arch_i486:
2060                         align = 4;
2061                         break;
2062                 case arch_k6:
2063                         align = 5;
2064                         break;
2065                 default:
2066                         align = 4;
2067         }
2068         maximum_skip = (1 << align) - 1;
2069         ia32_emit_alignment(F, align, maximum_skip);
2070 }
2071
2072 /**
2073  * Emits gas alignment directives for Labels depended on cpu architecture.
2074  */
2075 static void ia32_emit_align_label(FILE *F, cpu_support cpu) {
2076         unsigned align; unsigned maximum_skip;
2077
2078         switch (cpu) {
2079                 case arch_i386:
2080                         align = 2;
2081                         break;
2082                 case arch_i486:
2083                         align = 4;
2084                         break;
2085                 case arch_k6:
2086                         align = 5;
2087                         break;
2088                 default:
2089                         align = 4;
2090         }
2091         maximum_skip = (1 << align) - 1;
2092         ia32_emit_alignment(F, align, maximum_skip);
2093 }
2094
2095 /**
2096  * Walks over the nodes in a block connected by scheduling edges
2097  * and emits code for each node.
2098  */
2099 static void ia32_gen_block(ir_node *block, void *env) {
2100         ia32_emit_env_t *emit_env = env;
2101         const ir_node *irn;
2102         int need_label = block != get_irg_start_block(get_irn_irg(block));
2103         FILE *F = emit_env->out;
2104
2105         if (! is_Block(block))
2106                 return;
2107
2108         if (need_label && (emit_env->cg->opt & IA32_OPT_EXTBB)) {
2109                 /* if the extended block scheduler is used, only leader blocks need
2110                    labels. */
2111                 need_label = (block == get_extbb_leader(get_nodes_extbb(block)));
2112         }
2113
2114         if (need_label) {
2115                 char cmd_buf[SNPRINTF_BUF_LEN];
2116                 int i, arity;
2117
2118                 ia32_emit_align_label(emit_env->out, emit_env->isa->opt_arch);
2119
2120                 ir_snprintf(cmd_buf, sizeof(cmd_buf), BLOCK_PREFIX("%d:"),
2121                             get_irn_node_nr(block));
2122                 fprintf(F, "%-43s ", cmd_buf);
2123
2124                 /* emit list of pred blocks in comment */
2125                 fprintf(F, "/* preds:");
2126
2127                 arity = get_irn_arity(block);
2128                 for(i = 0; i < arity; ++i) {
2129                         ir_node *predblock = get_Block_cfgpred_block(block, i);
2130                         fprintf(F, " %ld", get_irn_node_nr(predblock));
2131                 }
2132                 fprintf(F, " */\n");
2133         }
2134
2135         /* emit the contents of the block */
2136         ia32_emit_dbg(block, env);
2137         sched_foreach(block, irn) {
2138                 ia32_emit_node(irn, env);
2139         }
2140 }
2141
2142 /**
2143  * Emits code for function start.
2144  */
2145 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg, ia32_emit_env_t *emit_env) {
2146         entity     *irg_ent  = get_irg_entity(irg);
2147         const char *irg_name = get_entity_ld_name(irg_ent);
2148         cpu_support cpu      = emit_env->isa->opt_arch;
2149         const be_irg_t *birg = emit_env->cg->birg;
2150
2151         fprintf(F, "\n");
2152         ia32_switch_section(F, SECTION_TEXT);
2153         be_dbg_method_begin(birg->main_env->db_handle, irg_ent, be_abi_get_stack_layout(birg->abi));
2154         ia32_emit_align_func(F, cpu);
2155         if (get_entity_visibility(irg_ent) == visibility_external_visible) {
2156                 fprintf(F, ".globl %s\n", irg_name);
2157         }
2158         ia32_dump_function_object(F, irg_name);
2159         fprintf(F, "%s:\n", irg_name);
2160 }
2161
2162 /**
2163  * Emits code for function end
2164  */
2165 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg, ia32_emit_env_t *emit_env) {
2166         const char *irg_name = get_entity_ld_name(get_irg_entity(irg));
2167         const be_irg_t *birg = emit_env->cg->birg;
2168
2169         ia32_dump_function_size(F, irg_name);
2170         be_dbg_method_end(birg->main_env->db_handle);
2171         fprintf(F, "\n");
2172 }
2173
2174 /**
2175  * Block-walker:
2176  * Sets labels for control flow nodes (jump target)
2177  * TODO: Jump optimization
2178  */
2179 static void ia32_gen_labels(ir_node *block, void *env) {
2180         ir_node *pred;
2181         int n = get_Block_n_cfgpreds(block);
2182
2183         for (n--; n >= 0; n--) {
2184                 pred = get_Block_cfgpred(block, n);
2185                 set_irn_link(pred, block);
2186         }
2187 }
2188
2189 /**
2190  * Main driver. Emits the code for one routine.
2191  */
2192 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
2193         ia32_emit_env_t emit_env;
2194         ir_node *block;
2195
2196         emit_env.out      = F;
2197         emit_env.arch_env = cg->arch_env;
2198         emit_env.cg       = cg;
2199         emit_env.isa      = (ia32_isa_t *)cg->arch_env->isa;
2200         FIRM_DBG_REGISTER(emit_env.mod, "firm.be.ia32.emitter");
2201
2202         /* set the global arch_env (needed by print hooks) */
2203         arch_env = cg->arch_env;
2204
2205         ia32_register_emitters();
2206
2207         ia32_emit_func_prolog(F, irg, &emit_env);
2208         irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
2209
2210         if ((cg->opt & IA32_OPT_EXTBB) && cg->blk_sched) {
2211                 int i, n = ARR_LEN(cg->blk_sched);
2212
2213                 for (i = 0; i < n;) {
2214                         ir_node *next_bl;
2215
2216                         block   = cg->blk_sched[i];
2217                         ++i;
2218                         next_bl = i < n ? cg->blk_sched[i] : NULL;
2219
2220                         /* set here the link. the emitter expects to find the next block here */
2221                         set_irn_link(block, next_bl);
2222                         ia32_gen_block(block, &emit_env);
2223                 }
2224         }
2225         else {
2226                 /* "normal" block schedule: Note the get_next_block() returns the NUMBER of the block
2227                    in the block schedule. As this number should NEVER be equal the next block,
2228                    we does not need a clear block link here. */
2229                 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
2230         }
2231
2232         ia32_emit_func_epilog(F, irg, &emit_env);
2233 }