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