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