fixed convs
[libfirm] / ir / be / ia32 / ia32_emitter.c
1 /**
2  * This file implements the node emitter.
3  *
4  * $Id$
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <limits.h>
12
13 #include "xmalloc.h"
14 #include "tv.h"
15 #include "iredges.h"
16 #include "debug.h"
17 #include "irgwalk.h"
18 #include "irprintf.h"
19 #include "irop_t.h"
20 #include "irargs_t.h"
21 #include "irprog_t.h"
22 #include "iredges_t.h"
23
24 #include "../besched_t.h"
25 #include "../benode_t.h"
26
27 #include "ia32_emitter.h"
28 #include "gen_ia32_emitter.h"
29 #include "gen_ia32_regalloc_if.h"
30 #include "ia32_nodes_attr.h"
31 #include "ia32_new_nodes.h"
32 #include "ia32_map_regs.h"
33
34 #ifdef obstack_chunk_alloc
35 # undef obstack_chunk_alloc
36 # define obstack_chunk_alloc xmalloc
37 #else
38 # define obstack_chunk_alloc xmalloc
39 # define obstack_chunk_free free
40 #endif
41
42 #define BLOCK_PREFIX(x) ".L" x
43
44 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
45
46 #define SNPRINTF_BUF_LEN 128
47
48 /* global arch_env for lc_printf functions */
49 static const arch_env_t *arch_env = NULL;
50
51 /* indicates whether blocks are scheduled or not
52    (this variable is set automatically) */
53 static int have_block_sched       = 0;
54
55 /*************************************************************
56  *             _       _    __   _          _
57  *            (_)     | |  / _| | |        | |
58  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
59  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
60  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
61  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
62  * | |                                       | |
63  * |_|                                       |_|
64  *************************************************************/
65
66 /* We always pass the ir_node which is a pointer. */
67 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
68         return lc_arg_type_ptr;
69 }
70
71
72 /**
73  * Returns the register at in position pos.
74  */
75 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
76         ir_node                *op;
77         const arch_register_t  *reg = NULL;
78
79         assert(get_irn_arity(irn) > pos && "Invalid IN position");
80
81         /* The out register of the operator at position pos is the
82            in register we need. */
83         op = get_irn_n(irn, pos);
84
85         reg = arch_get_irn_register(arch_env, op);
86
87         assert(reg && "no in register found");
88         return reg;
89 }
90
91 /**
92  * Returns the register at out position pos.
93  */
94 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
95         ir_node                *proj;
96         const arch_register_t  *reg = NULL;
97
98         /* 1st case: irn is not of mode_T, so it has only                 */
99         /*           one OUT register -> good                             */
100         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
101         /*           Proj with the corresponding projnum for the register */
102
103         if (get_irn_mode(irn) != mode_T) {
104                 reg = arch_get_irn_register(arch_env, irn);
105         }
106         else if (is_ia32_irn(irn)) {
107                 reg = get_ia32_out_reg(irn, pos);
108         }
109         else {
110                 const ir_edge_t *edge;
111
112                 foreach_out_edge(irn, edge) {
113                         proj = get_edge_src_irn(edge);
114                         assert(is_Proj(proj) && "non-Proj from mode_T node");
115                         if (get_Proj_proj(proj) == pos) {
116                                 reg = arch_get_irn_register(arch_env, proj);
117                                 break;
118                         }
119                 }
120         }
121
122         assert(reg && "no out register found");
123         return reg;
124 }
125
126 enum io_direction {
127   IN_REG,
128   OUT_REG
129 };
130
131 /**
132  * Returns the name of the in register at position pos.
133  */
134 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
135         const arch_register_t *reg;
136         const char            *name;
137         static char           *buf = NULL;
138         int                    len;
139
140         if (in_out == IN_REG) {
141                 reg = get_in_reg(irn, pos);
142         }
143         else {
144                 /* destination address mode nodes don't have outputs */
145                 if (is_ia32_irn(irn) && get_ia32_op_type(irn) == ia32_AddrModeD) {
146                         return "MEM";
147                 }
148
149                 reg = get_out_reg(irn, pos);
150         }
151
152         name = arch_register_get_name(reg);
153
154         if (buf) {
155                 free(buf);
156         }
157
158         len = strlen(name) + 2;
159         buf = xcalloc(1, len);
160
161         snprintf(buf, len, "%%%s", name);
162
163         return buf;
164 }
165
166 /**
167  * Get the register name for a node.
168  */
169 static int ia32_get_reg_name(lc_appendable_t *app,
170     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
171 {
172         const char *buf;
173         ir_node    *X  = arg->v_ptr;
174         int         nr = occ->width - 1;
175
176         if (!X)
177                 return lc_appendable_snadd(app, "(null)", 6);
178
179         buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
180
181         return lc_appendable_snadd(app, buf, strlen(buf));
182 }
183
184 /**
185  * Returns the tarval, offset or scale of an ia32 as a string.
186  */
187 static int ia32_const_to_str(lc_appendable_t *app,
188     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
189 {
190         const char *buf;
191         ir_node    *X = arg->v_ptr;
192
193         if (!X)
194                 return lc_arg_append(app, occ, "(null)", 6);
195
196         if (occ->conversion == 'C') {
197                 buf = get_ia32_cnst(X);
198         }
199         else { /* 'O' */
200                 buf = get_ia32_am_offs(X);
201         }
202
203         return buf ? lc_appendable_snadd(app, buf, strlen(buf)) : 0;
204 }
205
206 /**
207  * Determines the SSE suffix depending on the mode.
208  */
209 static int ia32_get_mode_suffix(lc_appendable_t *app,
210     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
211 {
212         ir_node *X    = arg->v_ptr;
213         ir_mode *mode = get_irn_mode(X);
214
215         if (mode == mode_T) {
216                 mode = is_ia32_AddrModeS(X) || is_ia32_AddrModeD(X) ? get_ia32_ls_mode(X) : get_ia32_res_mode(X);
217         }
218
219         if (!X)
220                 return lc_arg_append(app, occ, "(null)", 6);
221
222         if (mode_is_float(mode)) {
223                 return lc_appendable_chadd(app, get_mode_size_bits(mode) == 32 ? 's' : 'd');
224         }
225         else {
226                 return lc_appendable_chadd(app, mode_is_signed(mode) ? 's' : 'z');
227         }
228 }
229
230 /**
231  * Return the ia32 printf arg environment.
232  * We use the firm environment with some additional handlers.
233  */
234 const lc_arg_env_t *ia32_get_arg_env(void) {
235         static lc_arg_env_t *env = NULL;
236
237         static const lc_arg_handler_t ia32_reg_handler   = { ia32_get_arg_type, ia32_get_reg_name };
238         static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
239         static const lc_arg_handler_t ia32_mode_handler  = { ia32_get_arg_type, ia32_get_mode_suffix };
240
241         if(env == NULL) {
242                 /* extend the firm printer */
243                 env = firm_get_arg_env();
244
245                 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
246                 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
247                 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
248                 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
249                 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
250         }
251
252         return env;
253 }
254
255 static char *ia32_get_reg_name_for_mode(ia32_emit_env_t *env, ir_mode *mode, const arch_register_t *reg) {
256         switch(get_mode_size_bits(mode)) {
257                 case 8:
258                         return ia32_get_mapped_reg_name(env->isa->regs_8bit, reg);
259                 case 16:
260                         return ia32_get_mapped_reg_name(env->isa->regs_16bit, reg);
261                 case 32:
262                         return (char *)arch_register_get_name(reg);
263                 default:
264                         assert(0 && "unsupported mode size");
265                         return NULL;
266         }
267 }
268
269 /**
270  * Emits registers and/or address mode of a binary operation.
271  */
272 char *ia32_emit_binop(const ir_node *n, ia32_emit_env_t *env) {
273         static char *buf = NULL;
274
275         /* verify that this function is never called on non-AM supporting operations */
276         //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
277
278 #define PRODUCES_RESULT(n)   \
279         (!(is_ia32_St(n)      || \
280         is_ia32_Store8Bit(n)  || \
281         is_ia32_CondJmp(n)    || \
282         is_ia32_fCondJmp(n)   || \
283         is_ia32_SwitchJmp(n)))
284
285         if (! buf) {
286                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
287         }
288         else {
289                 memset(buf, 0, SNPRINTF_BUF_LEN);
290         }
291
292         switch(get_ia32_op_type(n)) {
293                 case ia32_Normal:
294                         if (get_ia32_cnst(n)) {
295                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
296                         }
297                         else {
298                                 const arch_register_t *in1 = get_in_reg(n, 2);
299                                 const arch_register_t *in2 = get_in_reg(n, 3);
300                                 const arch_register_t *out = PRODUCES_RESULT(n) ? get_out_reg(n, 0) : NULL;
301                                 const arch_register_t *in;
302
303                                 in  = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
304                                 out = out ? out : in1;
305
306                                 snprintf(buf, SNPRINTF_BUF_LEN, "%%%s, %%%s", \
307                                         arch_register_get_name(out), arch_register_get_name(in));
308                         }
309                         break;
310                 case ia32_AddrModeS:
311                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%4S, %s", n, ia32_emit_am(n, env));
312                         break;
313                 case ia32_AddrModeD:
314                         if (get_ia32_cnst(n)) {
315                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s,%s%s",
316                                         ia32_emit_am(n, env),
317                                         get_ia32_sc(n) ? " OFFSET FLAT:" : " ",    /* In case of a symconst we must add OFFSET to */
318                                         get_ia32_cnst(n));                         /* tell the assembler to store it's address.   */
319                         }
320                         else {
321                                 const arch_register_t *in1 = get_in_reg(n, 2);
322                                 ir_mode              *mode = get_ia32_res_mode(n);
323
324                                 mode = mode ? mode : get_ia32_ls_mode(n);
325                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %%%s",
326                                         ia32_emit_am(n, env), ia32_get_reg_name_for_mode(env, mode, in1));
327                         }
328                         break;
329                 default:
330                         assert(0 && "unsupported op type");
331         }
332
333 #undef PRODUCES_RESULT
334
335         return buf;
336 }
337
338 /**
339  * Emits registers and/or address mode of a unary operation.
340  */
341 char *ia32_emit_unop(const ir_node *n, ia32_emit_env_t *env) {
342         static char *buf = NULL;
343
344         if (! buf) {
345                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
346         }
347         else {
348                 memset(buf, 0, SNPRINTF_BUF_LEN);
349         }
350
351         switch(get_ia32_op_type(n)) {
352                 case ia32_Normal:
353                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
354                         break;
355                 case ia32_am_Dest:
356                         snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n, env));
357                         break;
358                 default:
359                         assert(0 && "unsupported op type");
360         }
361
362         return buf;
363 }
364
365 /**
366  * Emits address mode.
367  */
368 char *ia32_emit_am(const ir_node *n, ia32_emit_env_t *env) {
369         ia32_am_flavour_t am_flav    = get_ia32_am_flavour(n);
370         int               had_output = 0;
371         char             *s;
372         int               size;
373         static struct obstack *obst  = NULL;
374         ir_mode *mode = get_ia32_ls_mode(n);
375
376         if (! is_ia32_Lea(n))
377                 assert(mode && "AM node must have ls_mode attribute set.");
378
379         if (! obst) {
380                 obst = xcalloc(1, sizeof(*obst));
381         }
382         else {
383                 obstack_free(obst, NULL);
384         }
385
386         /* obstack_free with NULL results in an uninitialized obstack */
387         obstack_init(obst);
388
389         if (mode) {
390                 switch (get_mode_size_bits(mode)) {
391                         case 8:
392                                 obstack_printf(obst, "BYTE PTR ");
393                                 break;
394                         case 16:
395                                 obstack_printf(obst, "WORD PTR ");
396                                 break;
397                         case 32:
398                                 obstack_printf(obst, "DWORD PTR ");
399                                 break;
400                         default:
401                                 break;
402                 }
403         }
404
405         obstack_printf(obst, "[");
406
407         if (am_flav & ia32_B) {
408                 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
409                 had_output = 1;
410         }
411
412         if (am_flav & ia32_I) {
413                 if (had_output) {
414                         obstack_printf(obst, "+");
415                 }
416
417                 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
418
419                 if (am_flav & ia32_S) {
420                         obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
421                 }
422
423                 had_output = 1;
424         }
425
426         if (am_flav & ia32_O) {
427                 obstack_printf(obst, get_ia32_am_offs(n));
428         }
429
430         obstack_printf(obst, "] ");
431
432         size        = obstack_object_size(obst);
433         s           = obstack_finish(obst);
434         s[size - 1] = '\0';
435
436         return s;
437 }
438
439
440
441 /**
442  * Formated print of commands and comments.
443  */
444 static void ia32_fprintf_format(FILE *F, char *cmd_buf, char *cmnt_buf) {
445         fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
446 }
447
448
449
450 /**
451  * Add a number to a prefix. This number will not be used a second time.
452  */
453 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
454         static unsigned long id = 0;
455         snprintf(buf, buflen, "%s%lu", prefix, ++id);
456         return buf;
457 }
458
459
460
461 /*************************************************
462  *                 _ _                         _
463  *                (_) |                       | |
464  *   ___ _ __ ___  _| |_    ___ ___  _ __   __| |
465  *  / _ \ '_ ` _ \| | __|  / __/ _ \| '_ \ / _` |
466  * |  __/ | | | | | | |_  | (_| (_) | | | | (_| |
467  *  \___|_| |_| |_|_|\__|  \___\___/|_| |_|\__,_|
468  *
469  *************************************************/
470
471 #undef IA32_DO_EMIT
472 #define IA32_DO_EMIT ia32_fprintf_format(F, cmd_buf, cmnt_buf)
473
474 /*
475  * coding of conditions
476  */
477 struct cmp2conditon_t {
478         const char *name;
479         pn_Cmp      num;
480 };
481
482 /*
483  * positive conditions for signed compares
484  */
485 static const struct cmp2conditon_t cmp2condition_s[] = {
486   { NULL,              pn_Cmp_False },  /* always false */
487   { "e",               pn_Cmp_Eq },     /* == */
488   { "l",               pn_Cmp_Lt },     /* < */
489   { "le",              pn_Cmp_Le },     /* <= */
490   { "g",               pn_Cmp_Gt },     /* > */
491   { "ge",              pn_Cmp_Ge },     /* >= */
492   { "ne",              pn_Cmp_Lg },     /* != */
493   { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
494   { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
495   { "unordered or ==", pn_Cmp_Ue },     /* Floating point: unordered or == */
496   { "unordered or <",  pn_Cmp_Ul },     /* Floating point: unordered or < */
497   { "unordered or <=", pn_Cmp_Ule },    /* Floating point: unordered or <= */
498   { "unordered or >",  pn_Cmp_Ug },     /* Floating point: unordered or > */
499   { "unordered or >=", pn_Cmp_Uge },    /* Floating point: unordered or >= */
500   { "unordered or !=", pn_Cmp_Ne },     /* Floating point: unordered or != */
501   { NULL,              pn_Cmp_True },   /* always true */
502 };
503
504 /*
505  * positive conditions for unsigned compares
506  */
507 static const struct cmp2conditon_t cmp2condition_u[] = {
508         { NULL,              pn_Cmp_False },  /* always false */
509         { "e",               pn_Cmp_Eq },     /* == */
510         { "b",               pn_Cmp_Lt },     /* < */
511         {       "be",              pn_Cmp_Le },     /* <= */
512         { "a",               pn_Cmp_Gt },     /* > */
513         { "ae",              pn_Cmp_Ge },     /* >= */
514         { "ne",              pn_Cmp_Lg },     /* != */
515         { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
516         { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
517         { "unordered or ==", pn_Cmp_Ue },     /* Floating point: unordered or == */
518         { "unordered or <",  pn_Cmp_Ul },     /* Floating point: unordered or < */
519         { "unordered or <=", pn_Cmp_Ule },    /* Floating point: unordered or <= */
520         { "unordered or >",  pn_Cmp_Ug },     /* Floating point: unordered or > */
521         { "unordered or >=", pn_Cmp_Uge },    /* Floating point: unordered or >= */
522         { "unordered or !=", pn_Cmp_Ne },     /* Floating point: unordered or != */
523         { NULL,              pn_Cmp_True },   /* always true */
524 };
525
526 /*
527  * returns the condition code
528  */
529 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
530 {
531         assert(cmp2condition_s[cmp_code].num == cmp_code);
532         assert(cmp2condition_u[cmp_code].num == cmp_code);
533
534         return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
535 }
536
537 /**
538  * Returns the target block for a control flow node.
539  */
540 static ir_node *get_cfop_target_block(const ir_node *irn) {
541         return get_irn_link(irn);
542 }
543
544 /**
545  * Returns the target label for a control flow node.
546  */
547 static char *get_cfop_target(const ir_node *irn, char *buf) {
548         ir_node *bl = get_cfop_target_block(irn);
549
550         snprintf(buf, SNPRINTF_BUF_LEN, BLOCK_PREFIX("%ld"), get_irn_node_nr(bl));
551         return buf;
552 }
553
554 /** Return the next block in Block schedule */
555 static ir_node *next_blk_sched(const ir_node *block) {
556         return have_block_sched ? get_irn_link(block) : NULL;
557 }
558
559 /**
560  * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
561  */
562 static void finish_CondJmp(FILE *F, const ir_node *irn, ir_mode *mode) {
563         const ir_node   *proj1, *proj2 = NULL;
564         const ir_node   *block, *next_bl = NULL;
565         const ir_edge_t *edge;
566         char buf[SNPRINTF_BUF_LEN];
567         char cmd_buf[SNPRINTF_BUF_LEN];
568         char cmnt_buf[SNPRINTF_BUF_LEN];
569
570         /* get both Proj's */
571         edge = get_irn_out_edge_first(irn);
572         proj1 = get_edge_src_irn(edge);
573         assert(is_Proj(proj1) && "CondJmp with a non-Proj");
574
575         edge = get_irn_out_edge_next(irn, edge);
576         if (edge) {
577                 proj2 = get_edge_src_irn(edge);
578                 assert(is_Proj(proj2) && "CondJmp with a non-Proj");
579         }
580
581         /* for now, the code works for scheduled and non-schedules blocks */
582         block = get_nodes_block(irn);
583         if (proj2) {
584                 /* we have a block schedule */
585                 next_bl = next_blk_sched(block);
586
587                 if (get_cfop_target_block(proj1) == next_bl) {
588                         /* exchange both proj's so the second one can be omitted */
589                         const ir_node *t = proj1;
590                         proj1 = proj2;
591                         proj2 = t;
592                 }
593         }
594
595         /* the first Proj must always be created */
596         if (get_Proj_proj(proj1) == pn_Cond_true) {
597                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
598                                         get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
599                                         get_cfop_target(proj1, buf));
600                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == TRUE */");
601         }
602         else  {
603                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
604                                         get_cmp_suffix(get_negated_pnc(get_ia32_pncode(irn), mode),
605                                         !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
606                                         get_cfop_target(proj1, buf));
607                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == FALSE */");
608         }
609         IA32_DO_EMIT;
610
611         /* the second Proj might be a fallthrough */
612         if (proj2) {
613                 if (get_cfop_target_block(proj2) != next_bl) {
614                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj2, buf));
615                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* otherwise */");
616                 }
617                 else {
618                         cmd_buf[0] = '\0';
619                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrogh %s */", get_cfop_target(proj2, buf));
620                 }
621                 IA32_DO_EMIT;
622         }
623 }
624
625 /**
626  * Emits code for conditional jump.
627  */
628 static void CondJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
629         FILE *F = env->out;
630         char cmd_buf[SNPRINTF_BUF_LEN];
631         char cmnt_buf[SNPRINTF_BUF_LEN];
632
633         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
634         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
635         IA32_DO_EMIT;
636         finish_CondJmp(F, irn, get_irn_mode(get_irn_n(irn, 2)));
637 }
638
639 /**
640  * Emits code for conditional jump with two variables.
641  */
642 static void emit_ia32_CondJmp(const ir_node *irn, ia32_emit_env_t *env) {
643         CondJmp_emitter(irn, env);
644 }
645
646 /**
647  * Emits code for conditional jump with immediate.
648  */
649 void emit_ia32_CondJmp_i(const ir_node *irn, ia32_emit_env_t *env) {
650         CondJmp_emitter(irn, env);
651 }
652
653 /**
654  * Emits code for conditional test and jump.
655  */
656 static void TestJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
657         FILE       *F   = env->out;
658         const char *op1 = arch_register_get_name(get_in_reg(irn, 0));
659         const char *op2 = get_ia32_cnst(irn);
660         char        cmd_buf[SNPRINTF_BUF_LEN];
661         char        cmnt_buf[SNPRINTF_BUF_LEN];
662
663         if (! op2)
664                 op2 = arch_register_get_name(get_in_reg(irn, 1));
665
666         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "test %%%s,%s%s ", op1, get_ia32_cnst(irn) ? " " : " %", op2);
667         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
668         IA32_DO_EMIT;
669         finish_CondJmp(F, irn, get_irn_mode(get_irn_n(irn, 0)));
670 }
671
672 /**
673  * Emits code for conditional test and jump with two variables.
674  */
675 static void emit_ia32_TestJmp(const ir_node *irn, ia32_emit_env_t *env) {
676         TestJmp_emitter(irn, env);
677 }
678
679
680
681 /*********************************************************
682  *                 _ _       _
683  *                (_) |     (_)
684  *   ___ _ __ ___  _| |_     _ _   _ _ __ ___  _ __  ___
685  *  / _ \ '_ ` _ \| | __|   | | | | | '_ ` _ \| '_ \/ __|
686  * |  __/ | | | | | | |_    | | |_| | | | | | | |_) \__ \
687  *  \___|_| |_| |_|_|\__|   | |\__,_|_| |_| |_| .__/|___/
688  *                         _/ |               | |
689  *                        |__/                |_|
690  *********************************************************/
691
692 /* jump table entry (target and corresponding number) */
693 typedef struct _branch_t {
694         ir_node *target;
695         int      value;
696 } branch_t;
697
698 /* jump table for switch generation */
699 typedef struct _jmp_tbl_t {
700         ir_node  *defProj;         /**< default target */
701         int       min_value;       /**< smallest switch case */
702         int       max_value;       /**< largest switch case */
703         int       num_branches;    /**< number of jumps */
704         char     *label;           /**< label of the jump table */
705         branch_t *branches;        /**< jump array */
706 } jmp_tbl_t;
707
708 /**
709  * Compare two variables of type branch_t. Used to sort all switch cases
710  */
711 static int ia32_cmp_branch_t(const void *a, const void *b) {
712         branch_t *b1 = (branch_t *)a;
713         branch_t *b2 = (branch_t *)b;
714
715         if (b1->value <= b2->value)
716                 return -1;
717         else
718                 return 1;
719 }
720
721 /**
722  * Emits code for a SwitchJmp (creates a jump table if
723  * possible otherwise a cmp-jmp cascade). Port from
724  * cggg ia32 backend
725  */
726 void emit_ia32_SwitchJmp(const ir_node *irn, ia32_emit_env_t *emit_env) {
727         unsigned long       interval;
728         char                buf[SNPRINTF_BUF_LEN];
729         int                 last_value, i, pn, do_jmp_tbl = 1;
730         jmp_tbl_t           tbl;
731         ir_node            *proj;
732         const ir_edge_t    *edge;
733         const lc_arg_env_t *env = ia32_get_arg_env();
734         FILE               *F   = emit_env->out;
735         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
736
737         /* fill the table structure */
738         tbl.label        = xmalloc(SNPRINTF_BUF_LEN);
739         tbl.label        = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
740         tbl.defProj      = NULL;
741         tbl.num_branches = get_irn_n_edges(irn);
742         tbl.branches     = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
743         tbl.min_value    = INT_MAX;
744         tbl.max_value    = INT_MIN;
745
746         i = 0;
747         /* go over all proj's and collect them */
748         foreach_out_edge(irn, edge) {
749                 proj = get_edge_src_irn(edge);
750                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
751
752                 pn = get_Proj_proj(proj);
753
754                 /* create branch entry */
755                 tbl.branches[i].target = proj;
756                 tbl.branches[i].value  = pn;
757
758                 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
759                 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
760
761                 /* check for default proj */
762                 if (pn == get_ia32_pncode(irn)) {
763                         assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
764                         tbl.defProj = proj;
765                 }
766
767                 i++;
768         }
769
770         /* sort the branches by their number */
771         qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
772
773         /* two-complement's magic make this work without overflow */
774         interval = tbl.max_value - tbl.min_value;
775
776         /* check value interval */
777         if (interval > 16 * 1024) {
778                 do_jmp_tbl = 0;
779         }
780
781         /* check ratio of value interval to number of branches */
782         if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
783                 do_jmp_tbl = 0;
784         }
785
786         if (do_jmp_tbl) {
787                 /* emit the table */
788                 if (tbl.min_value != 0) {
789                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, -%d(%1S)",
790                                 interval, tbl.min_value, irn);
791                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* first switch value is not 0 */");
792
793                         IA32_DO_EMIT;
794                 }
795                 else {
796                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, %1S", interval, irn);
797                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* compare for switch */");
798
799                         IA32_DO_EMIT;
800                 }
801
802                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
803                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default jump if out of range  */");
804                 IA32_DO_EMIT;
805
806                 if (tbl.num_branches > 1) {
807                         /* create table */
808
809                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp [%1S*4+%s]", irn, tbl.label);
810                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get jump table entry as target */");
811                         IA32_DO_EMIT;
812
813                         fprintf(F, "\t.section\t.rodata\n");
814                         fprintf(F, "\t.align 4\n");
815
816                         fprintf(F, "%s:\n", tbl.label);
817
818                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
819                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */\n",  tbl.branches[0].value);
820                         IA32_DO_EMIT;
821
822                         last_value = tbl.branches[0].value;
823                         for (i = 1; i < tbl.num_branches; ++i) {
824                                 while (++last_value < tbl.branches[i].value) {
825                                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
826                                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
827                                         IA32_DO_EMIT;
828                                 }
829                                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf));
830                                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", last_value);
831                                 IA32_DO_EMIT;
832                         }
833
834                         fprintf(F, "\t.text");
835                 }
836                 else {
837                         /* one jump is enough */
838                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
839                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* only one case given */");
840                         IA32_DO_EMIT;
841                 }
842         }
843         else { // no jump table
844                 for (i = 0; i < tbl.num_branches; ++i) {
845                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %d, %1S", tbl.branches[i].value, irn);
846                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", i);
847                         IA32_DO_EMIT;
848                         fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
849                 }
850
851                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.defProj, buf));
852                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
853                 IA32_DO_EMIT;
854         }
855
856         if (tbl.label)
857                 free(tbl.label);
858         if (tbl.branches)
859                 free(tbl.branches);
860 }
861
862 /**
863  * Emits code for a unconditional jump.
864  */
865 void emit_Jmp(const ir_node *irn, ia32_emit_env_t *env) {
866         ir_node *block, *next_bl;
867         FILE *F = env->out;
868         char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
869
870         /* for now, the code works for scheduled and non-schedules blocks */
871         block = get_nodes_block(irn);
872
873         /* we have a block schedule */
874         next_bl = next_blk_sched(block);
875         if (get_cfop_target_block(irn) != next_bl) {
876                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf));
877                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F) */", irn, get_cfop_target_block(irn));
878         }
879         else {
880                 cmd_buf[0] = '\0';
881                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(irn, buf));
882         }
883         IA32_DO_EMIT;
884 }
885
886 /****************************
887  *                  _
888  *                 (_)
889  *  _ __  _ __ ___  _  ___
890  * | '_ \| '__/ _ \| |/ __|
891  * | |_) | | | (_) | |\__ \
892  * | .__/|_|  \___/| ||___/
893  * | |            _/ |
894  * |_|           |__/
895  ****************************/
896
897 /**
898  * Emits code for a proj -> node
899  */
900 void emit_Proj(const ir_node *irn, ia32_emit_env_t *env) {
901         ir_node *pred = get_Proj_pred(irn);
902
903         if (get_irn_op(pred) == op_Start) {
904                 switch(get_Proj_proj(irn)) {
905                         case pn_Start_X_initial_exec:
906                                 emit_Jmp(irn, env);
907                                 break;
908                         default:
909                                 break;
910                 }
911         }
912 }
913
914 /**********************************
915  *   _____                  ____
916  *  / ____|                |  _ \
917  * | |     ___  _ __  _   _| |_) |
918  * | |    / _ \| '_ \| | | |  _ <
919  * | |___| (_) | |_) | |_| | |_) |
920  *  \_____\___/| .__/ \__, |____/
921  *             | |     __/ |
922  *             |_|    |___/
923  **********************************/
924
925 /**
926  * Emit movsb/w instructions to make mov count divideable by 4
927  */
928 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
929         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
930
931         fprintf(F, "\t/* memcopy %d bytes*/\n", size);
932
933         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
934         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward*/");
935         IA32_DO_EMIT;
936
937         switch(rem) {
938                 case 1:
939                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
940                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 1 */");
941                         break;
942                 case 2:
943                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
944                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 2 */");
945                         break;
946                 case 3:
947                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
948                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
949                         IA32_DO_EMIT;
950                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
951                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
952                         break;
953         }
954
955         IA32_DO_EMIT;
956 }
957
958 /**
959  * Emit rep movsd instruction for memcopy.
960  */
961 void emit_ia32_CopyB(const ir_node *irn, ia32_emit_env_t *emit_env) {
962         FILE   *F    = emit_env->out;
963         tarval *tv   = get_ia32_Immop_tarval(irn);
964         int     rem  = get_tarval_long(tv);
965         int     size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
966         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
967
968         emit_CopyB_prolog(F, rem, size);
969
970         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "rep movsd");
971         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy */");
972         IA32_DO_EMIT;
973 }
974
975 /**
976  * Emits unrolled memcopy.
977  */
978 void emit_ia32_CopyB_i(const ir_node *irn, ia32_emit_env_t *emit_env) {
979         tarval *tv   = get_ia32_Immop_tarval(irn);
980         int     size = get_tarval_long(tv);
981         FILE   *F    = emit_env->out;
982         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
983
984         emit_CopyB_prolog(F, size & 0x3, size);
985
986         size >>= 2;
987         while (size--) {
988                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsd");
989                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy unrolled */");
990                 IA32_DO_EMIT;
991         }
992 }
993
994
995
996 /***************************
997  *   _____
998  *  / ____|
999  * | |     ___  _ ____   __
1000  * | |    / _ \| '_ \ \ / /
1001  * | |___| (_) | | | \ V /
1002  *  \_____\___/|_| |_|\_/
1003  *
1004  ***************************/
1005
1006 /**
1007  * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1008  */
1009 static void emit_ia32_Conv_with_FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1010         FILE               *F    = emit_env->out;
1011         const lc_arg_env_t *env  = ia32_get_arg_env();
1012         char               *from, *to, buf[64];
1013         ir_mode *src_mode, *tgt_mode;
1014         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1015
1016         src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
1017         tgt_mode = get_ia32_res_mode(irn);
1018
1019         from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
1020         to   = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
1021
1022         switch(get_ia32_op_type(irn)) {
1023                 case ia32_Normal:
1024                         lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
1025                         break;
1026                 case ia32_AddrModeS:
1027                         lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn, emit_env));
1028                         break;
1029                 default:
1030                         assert(0 && "unsupported op type for Conv");
1031         }
1032
1033         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cvt%s2%s %s", from, to, buf);
1034         lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F, %+F) */", irn, src_mode, tgt_mode);
1035         IA32_DO_EMIT;
1036 }
1037
1038 void emit_ia32_Conv_I2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1039         emit_ia32_Conv_with_FP(irn, emit_env);
1040 }
1041
1042 void emit_ia32_Conv_FP2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1043         emit_ia32_Conv_with_FP(irn, emit_env);
1044 }
1045
1046 void emit_ia32_Conv_FP2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1047         emit_ia32_Conv_with_FP(irn, emit_env);
1048 }
1049
1050 /**
1051  * Emits code for an Int conversion.
1052  */
1053 void emit_ia32_Conv_I2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1054         FILE               *F    = emit_env->out;
1055         const lc_arg_env_t *env  = ia32_get_arg_env();
1056         char *move_cmd, *conv_cmd;
1057         ir_mode *src_mode, *tgt_mode;
1058         int n, m;
1059         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1060         const arch_register_t *in_reg, *out_reg;
1061
1062         src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
1063         tgt_mode = get_ia32_res_mode(irn);
1064
1065         n = get_mode_size_bits(src_mode);
1066         m = get_mode_size_bits(tgt_mode);
1067
1068         if (mode_is_signed(n < m ? src_mode : tgt_mode)) {
1069                 move_cmd = "movsx";
1070                 if (n == 8 || m == 8)
1071                         conv_cmd = "cbw";
1072                 else if (n == 16 || m == 16)
1073                         conv_cmd = "cwde";
1074                 else
1075                         assert(0 && "unsupported Conv_I2I");
1076         }
1077         else {
1078                 move_cmd = "movzx";
1079                 conv_cmd = NULL;
1080         }
1081
1082         switch(get_ia32_op_type(irn)) {
1083                 case ia32_Normal:
1084                         in_reg  = get_in_reg(irn, 2);
1085                         out_reg = get_out_reg(irn, 0);
1086
1087                         if (REGS_ARE_EQUAL(in_reg, &ia32_gp_regs[REG_EAX]) &&
1088                                 REGS_ARE_EQUAL(out_reg, in_reg)                &&
1089                                 mode_is_signed(n < m ? src_mode : tgt_mode))
1090                         {
1091                                 /* argument and result are both in EAX and */
1092                                 /* signedness is ok: -> use converts       */
1093                                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s", conv_cmd);
1094                         }
1095                         else if (REGS_ARE_EQUAL(out_reg, in_reg) &&
1096                                 ! mode_is_signed(n < m ? src_mode : tgt_mode))
1097                         {
1098                                 /* argument and result are in the same register */
1099                                 /* and signedness is ok: -> use and with mask   */
1100                                 int mask = (1 << (n < m ? n : m)) - 1;
1101                                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "and %1D, 0x%x", irn, mask);
1102                         }
1103                         else {
1104                                 /* use move w/o sign extension */
1105                                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %%%s",
1106                                         move_cmd, irn, ia32_get_reg_name_for_mode(emit_env, n < m ? src_mode : tgt_mode, in_reg));
1107                         }
1108
1109                         break;
1110                 case ia32_AddrModeS:
1111                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %s",
1112                                 move_cmd, irn, ia32_emit_am(irn, emit_env));
1113                         break;
1114                 default:
1115                         assert(0 && "unsupported op type for Conv");
1116         }
1117
1118         lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%d Bit mode_%F -> %d Bit mode_%F) */",
1119                 irn, n, src_mode, m, tgt_mode);
1120
1121         IA32_DO_EMIT;
1122 }
1123
1124 /*******************************************
1125  *  _                          _
1126  * | |                        | |
1127  * | |__   ___ _ __   ___   __| | ___  ___
1128  * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1129  * | |_) |  __/ | | | (_) | (_| |  __/\__ \
1130  * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1131  *
1132  *******************************************/
1133
1134 /**
1135  * Emits a backend call
1136  */
1137 void emit_be_Call(const ir_node *irn, ia32_emit_env_t *emit_env) {
1138         FILE *F = emit_env->out;
1139         entity *ent = be_Call_get_entity(irn);
1140         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1141
1142         if (ent) {
1143                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "call %s", get_entity_name(ent));
1144         }
1145         else {
1146                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
1147         }
1148
1149         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
1150
1151         IA32_DO_EMIT;
1152 }
1153
1154 /**
1155  * Emits code to increase stack pointer.
1156  */
1157 void emit_be_IncSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1158         FILE          *F    = emit_env->out;
1159         unsigned       offs = be_get_IncSP_offset(irn);
1160         be_stack_dir_t dir  = be_get_IncSP_direction(irn);
1161         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1162
1163         if (offs) {
1164                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S,%s%u", irn,
1165                         (dir == be_stack_dir_along) ? " -" : " ", offs);
1166                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (IncSP) */", irn);
1167         }
1168         else {
1169                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1170                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted %+F (IncSP) with 0 */", irn);
1171         }
1172
1173         IA32_DO_EMIT;
1174 }
1175
1176 /**
1177  * Emits code to set stack pointer.
1178  */
1179 void emit_be_SetSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1180         FILE *F = emit_env->out;
1181         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1182
1183         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1184         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (restore SP) */", irn);
1185         IA32_DO_EMIT;
1186 }
1187
1188 /**
1189  * Emits code for Copy.
1190  */
1191 void emit_be_Copy(const ir_node *irn, ia32_emit_env_t *emit_env) {
1192         FILE *F = emit_env->out;
1193         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1194
1195         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
1196         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1197         IA32_DO_EMIT;
1198 }
1199
1200 /**
1201  * Emits code for exchange.
1202  */
1203 void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
1204         FILE *F = emit_env->out;
1205         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1206
1207         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1208         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%1A, %2A) */", irn, irn, irn);
1209         IA32_DO_EMIT;
1210 }
1211
1212 /***********************************************************************************
1213  *                  _          __                                             _
1214  *                 (_)        / _|                                           | |
1215  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
1216  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1217  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
1218  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
1219  *
1220  ***********************************************************************************/
1221
1222 /**
1223  * Enters the emitter functions for handled nodes into the generic
1224  * pointer of an opcode.
1225  */
1226 static void ia32_register_emitters(void) {
1227
1228 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
1229 #define EMIT(a)      op_##a->ops.generic = (op_func)emit_##a
1230 #define BE_EMIT(a)   op_be_##a->ops.generic = (op_func)emit_be_##a
1231
1232         /* first clear the generic function pointer for all ops */
1233         clear_irp_opcodes_generic_func();
1234
1235         /* register all emitter functions defined in spec */
1236         ia32_register_spec_emitters();
1237
1238         /* other ia32 emitter functions */
1239         IA32_EMIT(CondJmp);
1240         IA32_EMIT(TestJmp);
1241         IA32_EMIT(SwitchJmp);
1242         IA32_EMIT(CopyB);
1243         IA32_EMIT(CopyB_i);
1244         IA32_EMIT(Conv_I2FP);
1245         IA32_EMIT(Conv_FP2I);
1246         IA32_EMIT(Conv_FP2FP);
1247         IA32_EMIT(Conv_I2I);
1248
1249         /* benode emitter */
1250         BE_EMIT(Call);
1251         BE_EMIT(IncSP);
1252         BE_EMIT(SetSP);
1253         BE_EMIT(Copy);
1254         BE_EMIT(Perm);
1255
1256         /* firm emitter */
1257         EMIT(Jmp);
1258         EMIT(Proj);
1259
1260 #undef IA32_EMIT
1261 #undef BE_EMIT
1262 #undef EMIT
1263 }
1264
1265 /**
1266  * Emits code for a node.
1267  */
1268 static void ia32_emit_node(const ir_node *irn, void *env) {
1269         ia32_emit_env_t        *emit_env = env;
1270         firm_dbg_module_t *mod      = emit_env->mod;
1271         FILE              *F        = emit_env->out;
1272         ir_op             *op       = get_irn_op(irn);
1273
1274         DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
1275
1276         if (op->ops.generic) {
1277                 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
1278                 (*emit)(irn, env);
1279         }
1280         else {
1281                 ir_fprintf(F, "\t%35s /* %+F */\n", " ", irn);
1282         }
1283 }
1284
1285 /**
1286  * Walks over the nodes in a block connected by scheduling edges
1287  * and emits code for each node.
1288  */
1289 static void ia32_gen_block(ir_node *block, void *env) {
1290         const ir_node *irn;
1291
1292         if (! is_Block(block))
1293                 return;
1294
1295         fprintf(((ia32_emit_env_t *)env)->out, BLOCK_PREFIX("%ld:\n"), get_irn_node_nr(block));
1296         sched_foreach(block, irn) {
1297                 ia32_emit_node(irn, env);
1298         }
1299 }
1300
1301 /**
1302  * Emits code for function start.
1303  */
1304 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
1305         entity     *irg_ent  = get_irg_entity(irg);
1306         const char *irg_name = get_entity_name(irg_ent);
1307
1308         fprintf(F, "\t.text\n");
1309         if (get_entity_visibility(irg_ent) == visibility_external_visible) {
1310                 fprintf(F, ".globl %s\n", irg_name);
1311         }
1312         fprintf(F, "\t.type\t%s, @function\n", irg_name);
1313         fprintf(F, "%s:\n", irg_name);
1314 }
1315
1316 /**
1317  * Emits code for function end
1318  */
1319 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1320         const char *irg_name = get_entity_name(get_irg_entity(irg));
1321
1322         fprintf(F, "\tret\n");
1323         fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
1324 }
1325
1326 /**
1327  * Block-walker:
1328  * Sets labels for control flow nodes (jump target)
1329  * TODO: Jump optimization
1330  */
1331 static void ia32_gen_labels(ir_node *block, void *env) {
1332         ir_node *pred;
1333         int n = get_Block_n_cfgpreds(block);
1334
1335         for (n--; n >= 0; n--) {
1336                 pred = get_Block_cfgpred(block, n);
1337                 set_irn_link(pred, block);
1338         }
1339 }
1340
1341 typedef struct {
1342         ir_node *start;
1343         ir_node *end;
1344 } anchor;
1345
1346 /**
1347  * Ext-Block walker: create a block schedule
1348  */
1349 static void create_block_list(ir_extblk *blk, void *env) {
1350         anchor *list = env;
1351         int i, n;
1352
1353         for (i = 0, n = get_extbb_n_blocks(blk); i < n; ++i) {
1354                 ir_node *block = get_extbb_block(blk, i);
1355
1356                 set_irn_link(block, NULL);
1357                 if (list->start)
1358                         set_irn_link(list->end, block);
1359                 else
1360                         list->start = block;
1361
1362                 list->end = block;
1363         }
1364 }
1365
1366 /**
1367  * Main driver. Emits the code for one routine.
1368  */
1369 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1370         ia32_emit_env_t emit_env;
1371         anchor list;
1372         ir_node *block;
1373
1374         emit_env.mod      = firm_dbg_register("firm.be.ia32.emitter");
1375         emit_env.out      = F;
1376         emit_env.arch_env = cg->arch_env;
1377         emit_env.cg       = cg;
1378         emit_env.isa      = (ia32_isa_t *)cg->arch_env->isa;
1379
1380         /* set the global arch_env (needed by print hooks) */
1381         arch_env = cg->arch_env;
1382
1383         ia32_register_emitters();
1384
1385         ia32_emit_func_prolog(F, irg);
1386         irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1387
1388         if (cg->opt.extbb) {
1389                 /* schedule extended basic blocks */
1390
1391                 compute_extbb(irg);
1392
1393                 list.start = NULL;
1394                 list.end   = NULL;
1395                 irg_extblock_walk_graph(irg, NULL, create_block_list, &list);
1396
1397                 have_block_sched = 1;
1398                 for (block = list.start; block; block = get_irn_link(block))
1399                         ia32_gen_block(block, &emit_env);
1400         }
1401         else {
1402                 /* "normal" block schedule */
1403
1404                 have_block_sched = 0;
1405                 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1406         }
1407
1408         ia32_emit_func_epilog(F, irg);
1409 }