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