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