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