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