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