simplifyed emitter initialization
[libfirm] / ir / be / ia32 / ia32_emitter.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <limits.h>
6
7 #include "xmalloc.h"
8 #include "tv.h"
9 #include "iredges.h"
10 #include "debug.h"
11 #include "irgwalk.h"
12 #include "irprintf.h"
13 #include "irop_t.h"
14 #include "irargs_t.h"
15 #include "irprog_t.h"
16
17 #include "../besched.h"
18
19 #include "ia32_emitter.h"
20 #include "gen_ia32_emitter.h"
21 #include "ia32_nodes_attr.h"
22 #include "ia32_new_nodes.h"
23 #include "ia32_map_regs.h"
24
25 #ifdef obstack_chunk_alloc
26 # undef obstack_chunk_alloc
27 # define obstack_chunk_alloc xmalloc
28 #else
29 # define obstack_chunk_alloc xmalloc
30 # define obstack_chunk_free free
31 #endif
32
33 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
34
35 #define SNPRINTF_BUF_LEN 128
36
37 static const arch_env_t *arch_env = NULL;
38
39 /**
40  * Emits registers and/or address mode of a binary operation.
41  */
42 char *ia32_emit_binop(const ir_node *n) {
43         static char *buf = NULL;
44
45         if (! buf) {
46                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
47         }
48         else {
49                 memset(buf, 0, SNPRINTF_BUF_LEN);
50         }
51
52         switch(get_ia32_op_type(n)) {
53                 case ia32_Normal:
54                         if (get_ia32_cnst(n)) {
55                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, get_ia32_cnst(n));
56                         }
57                         else {
58                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %4S", n, n);
59                         }
60                         break;
61                 case ia32_am_Source:
62                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, ia32_emit_am(n));
63                         break;
64                 case ia32_am_Dest:
65                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %4S", ia32_emit_am(n), n);
66                         break;
67                 default:
68                         assert(0 && "unsupported op type");
69         }
70
71         return buf;
72 }
73
74 /**
75  * Emits registers and/or address mode of a unary operation.
76  */
77 char *ia32_emit_unop(const ir_node *n) {
78         static char *buf = NULL;
79
80         if (! buf) {
81                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
82         }
83         else {
84                 memset(buf, 0, SNPRINTF_BUF_LEN);
85         }
86
87         switch(get_ia32_op_type(n)) {
88                 case ia32_Normal:
89                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
90                         break;
91                 case ia32_am_Dest:
92                         snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n));
93                         break;
94                 default:
95                         assert(0 && "unsupported op type");
96         }
97
98         return buf;
99 }
100
101 /**
102  * Emits adressmode.
103  */
104 char *ia32_emit_am(const ir_node *n) {
105         ia32_am_flavour_t am_flav    = get_ia32_am_flavour(n);
106         int               had_output = 0;
107         char             *s;
108         int               size;
109         static struct obstack *obst  = NULL;
110
111         if (! obst) {
112                 obst = xcalloc(1, sizeof(*obst));
113         }
114         else {
115                 obstack_free(obst, NULL);
116         }
117
118         /* obstack_free with NULL results in an uninitialized obstack */
119         obstack_init(obst);
120
121         obstack_printf(obst, "[");
122
123         if (am_flav & ia32_B) {
124                 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
125                 had_output = 1;
126         }
127
128         if (am_flav & ia32_I) {
129                 if (had_output) {
130                         obstack_printf(obst, "+");
131                 }
132
133                 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
134
135                 if (am_flav & ia32_S) {
136                         obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
137                 }
138
139                 had_output = 1;
140         }
141
142         if (am_flav & ia32_O) {
143                 obstack_printf(obst, get_ia32_am_offs(n));
144         }
145
146         obstack_printf(obst, "] ");
147
148         size        = obstack_object_size(obst);
149         s           = obstack_finish(obst);
150         s[size - 1] = '\0';
151
152         return s;
153 }
154
155 /*************************************************************
156  *             _       _    __   _          _
157  *            (_)     | |  / _| | |        | |
158  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
159  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
160  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
161  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
162  * | |                                       | |
163  * |_|                                       |_|
164  *************************************************************/
165
166 /* We always pass the ir_node which is a pointer. */
167 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
168         return lc_arg_type_ptr;
169 }
170
171
172 /**
173  * Returns the register at in position pos.
174  */
175 static const arch_register_t *get_in_reg(ir_node *irn, int pos) {
176         ir_node                *op;
177         const arch_register_t  *reg = NULL;
178
179         assert(get_irn_arity(irn) > pos && "Invalid IN position");
180
181         /* The out register of the operator at position pos is the
182            in register we need. */
183         op = get_irn_n(irn, pos);
184
185         reg = arch_get_irn_register(arch_env, op);
186
187         assert(reg && "no in register found");
188         return reg;
189 }
190
191 /**
192  * Returns the register at out position pos.
193  */
194 static const arch_register_t *get_out_reg(ir_node *irn, int pos) {
195         ir_node                *proj;
196         const arch_register_t  *reg = NULL;
197
198         assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
199
200         /* 1st case: irn is not of mode_T, so it has only                 */
201         /*           one OUT register -> good                             */
202         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
203         /*           Proj with the corresponding projnum for the register */
204
205         if (get_irn_mode(irn) != mode_T) {
206                 reg = arch_get_irn_register(arch_env, irn);
207         }
208         else if (is_ia32_irn(irn)) {
209                 reg = get_ia32_out_reg(irn, pos);
210         }
211         else {
212                 const ir_edge_t *edge;
213
214                 foreach_out_edge(irn, edge) {
215                         proj = get_edge_src_irn(edge);
216                         assert(is_Proj(proj) && "non-Proj from mode_T node");
217                         if (get_Proj_proj(proj) == pos) {
218                                 reg = arch_get_irn_register(arch_env, proj);
219                                 break;
220                         }
221                 }
222         }
223
224         assert(reg && "no out register found");
225         return reg;
226 }
227
228 /**
229  * Returns the number of the in register at position pos.
230  */
231 int get_ia32_reg_nr(ir_node *irn, int pos, int in_out) {
232         const arch_register_t *reg;
233
234         if (in_out == 1) {
235                 reg = get_in_reg(irn, pos);
236         }
237         else {
238                 reg = get_out_reg(irn, pos);
239         }
240
241         return arch_register_get_index(reg);
242 }
243
244 enum io_direction {
245   IN_REG,
246   OUT_REG
247 };
248
249 /**
250  * Returns the name of the in register at position pos.
251  */
252 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
253         const arch_register_t *reg;
254
255         if (in_out == IN_REG) {
256                 reg = get_in_reg(irn, pos);
257         }
258         else {
259                 reg = get_out_reg(irn, pos);
260         }
261
262         return arch_register_get_name(reg);
263 }
264
265 /**
266  * Get the register name for a node.
267  */
268 static int ia32_get_reg_name(lc_appendable_t *app,
269     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
270 {
271         const char *buf;
272         ir_node    *X  = arg->v_ptr;
273         int         nr = occ->width - 1;
274
275         if (!X)
276                 return lc_arg_append(app, occ, "(null)", 6);
277
278   buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
279
280         lc_appendable_chadd(app, '%');
281         return lc_arg_append(app, occ, buf, strlen(buf));
282 }
283
284 /**
285  * Returns the tarval, offset or scale of an ia32 as a string.
286  */
287 static int ia32_const_to_str(lc_appendable_t *app,
288     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
289 {
290         const char *buf;
291         ir_node    *X = arg->v_ptr;
292
293         if (!X)
294                 return lc_arg_append(app, occ, "(null)", 6);
295
296         if (occ->conversion == 'C') {
297                 buf = get_ia32_cnst(X);
298         }
299         else { /* 'O' */
300                 buf = get_ia32_am_offs(X);
301         }
302
303         return lc_arg_append(app, occ, buf, strlen(buf));
304 }
305
306 /**
307  * Determines the SSE suffix depending on the mode.
308  */
309 static int ia32_get_mode_suffix(lc_appendable_t *app,
310     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
311 {
312         ir_node *X = arg->v_ptr;
313
314         if (!X)
315                 return lc_arg_append(app, occ, "(null)", 6);
316
317         if (get_mode_size_bits(get_irn_mode(X)) == 32)
318                 return lc_appendable_chadd(app, 's');
319         else
320                 return lc_appendable_chadd(app, 'd');
321 }
322
323 /**
324  * Return the ia32 printf arg environment.
325  * We use the firm environment with some additional handlers.
326  */
327 const lc_arg_env_t *ia32_get_arg_env(void) {
328         static lc_arg_env_t *env = NULL;
329
330         static const lc_arg_handler_t ia32_reg_handler   = { ia32_get_arg_type, ia32_get_reg_name };
331         static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
332         static const lc_arg_handler_t ia32_mode_handler  = { ia32_get_arg_type, ia32_get_mode_suffix };
333
334         if(env == NULL) {
335                 /* extend the firm printer */
336                 env = firm_get_arg_env();
337                         //lc_arg_new_env();
338
339                 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
340                 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
341                 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
342                 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
343                 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
344         }
345
346         return env;
347 }
348
349
350 /*
351  * Add a number to a prefix. This number will not be used a second time.
352  */
353 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
354         static unsigned long id = 0;
355         snprintf(buf, buflen, "%s%lu", prefix, ++id);
356         return buf;
357 }
358
359
360 /*************************************************
361  *                 _ _                         _
362  *                (_) |                       | |
363  *   ___ _ __ ___  _| |_    ___ ___  _ __   __| |
364  *  / _ \ '_ ` _ \| | __|  / __/ _ \| '_ \ / _` |
365  * |  __/ | | | | | | |_  | (_| (_) | | | | (_| |
366  *  \___|_| |_| |_|_|\__|  \___\___/|_| |_|\__,_|
367  *
368  *************************************************/
369
370 /*
371  * coding of conditions
372  */
373 struct cmp2conditon_t {
374         const char *name;
375         pn_Cmp      num;
376 };
377
378 /*
379  * positive conditions for signed compares
380  */
381 static const struct cmp2conditon_t cmp2condition_s[] = {
382   { NULL,              pn_Cmp_False },  /* always false */
383   { "e",               pn_Cmp_Eq },     /* == */
384   { "l",               pn_Cmp_Lt },     /* < */
385   { "le",              pn_Cmp_Le },     /* <= */
386   { "g",               pn_Cmp_Gt },     /* > */
387   { "ge",              pn_Cmp_Ge },     /* >= */
388   { "ne",              pn_Cmp_Lg },     /* != */
389   { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
390   { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
391   { "unordered or ==", pn_Cmp_Ue },     /* Floating point: unordered or == */
392   { "unordered or <",  pn_Cmp_Ul },     /* Floating point: unordered or < */
393   { "unordered or <=", pn_Cmp_Ule },    /* Floating point: unordered or <= */
394   { "unordered or >",  pn_Cmp_Ug },     /* Floating point: unordered or > */
395   { "unordered or >=", pn_Cmp_Uge },    /* Floating point: unordered or >= */
396   { "unordered or !=", pn_Cmp_Ne },     /* Floating point: unordered or != */
397   { NULL,              pn_Cmp_True },   /* always true */
398 };
399
400 /*
401  * positive conditions for unsigned compares
402  */
403 static const struct cmp2conditon_t cmp2condition_u[] = {
404   { NULL,              pn_Cmp_False },  /* always false */
405   { "e",               pn_Cmp_Eq },     /* == */
406   { "b",               pn_Cmp_Lt },     /* < */
407   { "be",              pn_Cmp_Le },     /* <= */
408   { "a",               pn_Cmp_Gt },     /* > */
409   { "ae",              pn_Cmp_Ge },     /* >= */
410   { "ne",              pn_Cmp_Lg },     /* != */
411   { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
412   { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
413   { "unordered or ==", pn_Cmp_Ue },     /* Floating point: unordered or == */
414   { "unordered or <",  pn_Cmp_Ul },     /* Floating point: unordered or < */
415   { "unordered or <=", pn_Cmp_Ule },    /* Floating point: unordered or <= */
416   { "unordered or >",  pn_Cmp_Ug },     /* Floating point: unordered or > */
417   { "unordered or >=", pn_Cmp_Uge },    /* Floating point: unordered or >= */
418   { "unordered or !=", pn_Cmp_Ne },     /* Floating point: unordered or != */
419   { NULL,              pn_Cmp_True },   /* always true */
420 };
421
422 /*
423  * returns the condition code
424  */
425 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
426 {
427         assert(cmp2condition_s[cmp_code].num == cmp_code);
428         assert(cmp2condition_u[cmp_code].num == cmp_code);
429
430         return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
431 }
432
433 /**
434  * Returns the target label for a control flow node.
435  */
436 static char *get_cfop_target(const ir_node *irn, char *buf) {
437         ir_node *bl = get_irn_link(irn);
438
439         snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
440         return buf;
441 }
442
443 /**
444  * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
445  */
446 static void finish_CondJmp(FILE *F, ir_node *irn) {
447         const ir_node   *proj;
448         const ir_edge_t *edge;
449         char buf[SNPRINTF_BUF_LEN];
450
451         edge = get_irn_out_edge_first(irn);
452         proj = get_edge_src_irn(edge);
453         assert(is_Proj(proj) && "CondJmp with a non-Proj");
454
455         if (get_Proj_proj(proj) == 1) {
456                 fprintf(F, "\tj%s %s\t\t\t/* cmp(a, b) == TRUE */\n",
457                                         get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
458                                         get_cfop_target(proj, buf));
459         }
460         else  {
461                 fprintf(F, "\tjn%s %s\t\t\t/* cmp(a, b) == FALSE */\n",
462                                         get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
463                                         get_cfop_target(proj, buf));
464         }
465
466         edge = get_irn_out_edge_next(irn, edge);
467         if (edge) {
468                 proj = get_edge_src_irn(edge);
469                 assert(is_Proj(proj) && "CondJmp with a non-Proj");
470                 fprintf(F, "\tjmp %s\t\t\t/* otherwise */\n", get_cfop_target(proj, buf));
471         }
472 }
473
474 /**
475  * Emits code for conditional jump with two variables.
476  */
477 static void emit_ia32_CondJmp(ir_node *irn, emit_env_t *env) {
478         FILE *F = env->out;
479
480         lc_efprintf(ia32_get_arg_env(), F, "\tcmp %2S, %1S\t\t\t/* CondJmp(%+F, %+F) */\n", irn, irn,
481                                                                                                                                         get_irn_n(irn, 0), get_irn_n(irn, 1));
482         finish_CondJmp(F, irn);
483 }
484
485 /**
486  * Emits code for conditional jump with immediate.
487  */
488 void emit_ia32_CondJmp_i(ir_node *irn, emit_env_t *env) {
489         FILE *F = env->out;
490
491         lc_efprintf(ia32_get_arg_env(), F, "\tcmp %C, %1S\t\t\t/* CondJmp_i(%+F) */\n", irn, irn, get_irn_n(irn, 0));
492         finish_CondJmp(F, irn);
493 }
494
495
496
497 /*********************************************************
498  *                 _ _       _
499  *                (_) |     (_)
500  *   ___ _ __ ___  _| |_     _ _   _ _ __ ___  _ __  ___
501  *  / _ \ '_ ` _ \| | __|   | | | | | '_ ` _ \| '_ \/ __|
502  * |  __/ | | | | | | |_    | | |_| | | | | | | |_) \__ \
503  *  \___|_| |_| |_|_|\__|   | |\__,_|_| |_| |_| .__/|___/
504  *                         _/ |               | |
505  *                        |__/                |_|
506  *********************************************************/
507
508 /* jump table entry (target and corresponding number) */
509 typedef struct _branch_t {
510         ir_node *target;
511         int      value;
512 } branch_t;
513
514 /* jump table for switch generation */
515 typedef struct _jmp_tbl_t {
516         ir_node  *defProj;         /**< default target */
517         int       min_value;       /**< smallest switch case */
518         int       max_value;       /**< largest switch case */
519         int       num_branches;    /**< number of jumps */
520         char     *label;           /**< label of the jump table */
521         branch_t *branches;        /**< jump array */
522 } jmp_tbl_t;
523
524 /**
525  * Compare two variables of type branch_t. Used to sort all switch cases
526  */
527 static int ia32_cmp_branch_t(const void *a, const void *b) {
528         branch_t *b1 = (branch_t *)a;
529         branch_t *b2 = (branch_t *)b;
530
531         if (b1->value <= b2->value)
532                 return -1;
533         else
534                 return 1;
535 }
536
537 /**
538  * Emits code for a SwitchJmp (creates a jump table if
539  * possible otherwise a cmp-jmp cascade). Port from
540  * cggg ia32 backend
541  */
542 void emit_ia32_SwitchJmp(const ir_node *irn, emit_env_t *emit_env) {
543         unsigned long       interval;
544         char                buf[SNPRINTF_BUF_LEN];
545         int                 last_value, i, pn, do_jmp_tbl = 1;
546         jmp_tbl_t           tbl;
547         ir_node            *proj;
548         const ir_edge_t    *edge;
549         const lc_arg_env_t *env = ia32_get_arg_env();
550         FILE               *F   = emit_env->out;
551
552         /* fill the table structure */
553         tbl.label        = xmalloc(SNPRINTF_BUF_LEN);
554         tbl.label        = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
555         tbl.defProj      = NULL;
556         tbl.num_branches = get_irn_n_edges(irn);
557         tbl.branches     = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
558         tbl.min_value    = INT_MAX;
559         tbl.max_value    = INT_MIN;
560
561         i = 0;
562         /* go over all proj's and collect them */
563         foreach_out_edge(irn, edge) {
564                 proj = get_edge_src_irn(edge);
565                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
566
567                 pn = get_Proj_proj(proj);
568
569                 /* create branch entry */
570                 tbl.branches[i].target = proj;
571                 tbl.branches[i].value  = pn;
572
573                 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
574                 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
575
576                 /* check for default proj */
577                 if (pn == get_ia32_pncode(irn)) {
578                         assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
579                         tbl.defProj = proj;
580                 }
581
582                 i++;
583         }
584
585         /* sort the branches by their number */
586         qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
587
588         /* two-complement's magic make this work without overflow */
589         interval = tbl.max_value - tbl.min_value;
590
591         /* check value interval */
592         if (interval > 16 * 1024) {
593                 do_jmp_tbl = 0;
594         }
595
596         /* check ratio of value interval to number of branches */
597         if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
598                 do_jmp_tbl = 0;
599         }
600
601         if (do_jmp_tbl) {
602                 /* emit the table */
603                 if (tbl.min_value != 0) {
604                         fprintf(F, "\tcmpl %lu, -%d", interval, tbl.min_value);
605                         lc_efprintf(env, F, "(%1S)\t\t/* first switch value is not 0 */\n", irn);
606                 }
607                 else {
608                         fprintf(F, "\tcmpl %lu, ", interval);
609                         lc_efprintf(env, F, "%1S\t\t\t/* compare for switch */\n", irn);
610                 }
611
612                 fprintf(F, "\tja %s\t\t\t/* default jump if out of range  */\n", get_cfop_target(tbl.defProj, buf));
613
614                 if (tbl.num_branches > 1) {
615                         /* create table */
616
617                         //fprintf(F, "\tjmp *%s", tbl.label);
618                         lc_efprintf(env, F, "\tjmp *%s(,%1S,4)\t\t/* get jump table entry as target */\n", tbl.label, irn);
619
620                         fprintf(F, "\t.section\t.rodata\t\t/* start jump table */\n");
621                         fprintf(F, "\t.align 4\n");
622
623                         fprintf(F, "%s:\n", tbl.label);
624                         fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[0].target, buf), tbl.branches[0].value);
625
626                         last_value = tbl.branches[0].value;
627                         for (i = 1; i < tbl.num_branches; ++i) {
628                                 while (++last_value < tbl.branches[i].value) {
629                                         fprintf(F, "\t.long %s\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
630                                 }
631                                 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[i].target, buf), last_value);
632                         }
633
634                         fprintf(F, "\t.text\t\t\t\t/* end of jump table */\n");
635                 }
636                 else {
637                         /* one jump is enough */
638                         fprintf(F, "\tjmp %s\t\t/* only one case given */\n", get_cfop_target(tbl.branches[0].target, buf));
639                 }
640         }
641         else { // no jump table
642                 for (i = 0; i < tbl.num_branches; ++i) {
643                         fprintf(F, "\tcmpl %d, ", tbl.branches[i].value);
644                         lc_efprintf(env, F, "%1S", irn);
645                         fprintf(F, "\t\t\t/* case %d */\n", tbl.branches[i].value);
646                         fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
647                 }
648
649                 fprintf(F, "\tjmp %s\t\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
650         }
651
652         if (tbl.label)
653                 free(tbl.label);
654         if (tbl.branches)
655                 free(tbl.branches);
656 }
657
658 /**
659  * Emits code for a unconditional jump.
660  */
661 void emit_Jmp(ir_node *irn, emit_env_t *env) {
662         FILE *F = env->out;
663
664         char buf[SNPRINTF_BUF_LEN];
665         ir_fprintf(F, "\tjmp %s\t\t\t/* Jmp(%+F) */\n", get_cfop_target(irn, buf), get_irn_link(irn));
666 }
667
668
669
670 /****************************
671  *                  _
672  *                 (_)
673  *  _ __  _ __ ___  _  ___
674  * | '_ \| '__/ _ \| |/ __|
675  * | |_) | | | (_) | |\__ \
676  * | .__/|_|  \___/| ||___/
677  * | |            _/ |
678  * |_|           |__/
679  ****************************/
680
681 /**
682  * Emits code for a proj -> node
683  */
684 void emit_Proj(ir_node *irn, emit_env_t *env) {
685         ir_node *pred = get_Proj_pred(irn);
686
687         if (get_irn_op(pred) == op_Start) {
688                 switch(get_Proj_proj(irn)) {
689                         case pn_Start_X_initial_exec:
690                                 emit_Jmp(irn, env);
691                                 break;
692                         default:
693                                 break;
694                 }
695         }
696 }
697
698 /**********************************
699  *   _____                  ____
700  *  / ____|                |  _ \
701  * | |     ___  _ __  _   _| |_) |
702  * | |    / _ \| '_ \| | | |  _ <
703  * | |___| (_) | |_) | |_| | |_) |
704  *  \_____\___/| .__/ \__, |____/
705  *             | |     __/ |
706  *             |_|    |___/
707  **********************************/
708
709 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
710         fprintf(F, "\t/* memcopy %d bytes*/\n", size);
711         fprintf(F, "\tcld\t\t\t\t/* copy direction forward*/\n");
712
713         switch(rem) {
714                 case 1:
715                         fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 1 */\n");
716                         break;
717                 case 2:
718                         fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 2 */\n");
719                         break;
720                 case 3:
721                         fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 3 */\n");
722                         fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 3 */\n");
723                         break;
724         }
725 }
726
727 void emit_ia32_CopyB(ir_node *irn, emit_env_t *emit_env) {
728         FILE   *F    = emit_env->out;
729         tarval *tv   = get_ia32_Immop_tarval(irn);
730         int     rem  = get_tarval_long(tv);
731         int     size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
732
733         emit_CopyB_prolog(F, rem, size);
734
735         fprintf(F, "\trep movsd\t\t\t\t/* memcopy */\n");
736 }
737
738 void emit_ia32_CopyB_i(ir_node *irn, emit_env_t *emit_env) {
739         tarval *tv   = get_ia32_Immop_tarval(irn);
740         int     size = get_tarval_long(tv);
741         FILE   *F    = emit_env->out;
742
743         emit_CopyB_prolog(F, size & 0x3, size);
744
745         size >>= 2;
746         while (size--) {
747                 fprintf(F, "\tmovsd\t\t\t\t/* memcopy unrolled */\n");
748         }
749 }
750
751 /********************
752  *   _____      _ _
753  *  / ____|    | | |
754  * | |     __ _| | |
755  * | |    / _` | | |
756  * | |___| (_| | | |
757  *  \_____\__,_|_|_|
758  *
759  ********************/
760
761 void emit_ia32_Call(ir_node *irn, emit_env_t *emit_env) {
762 }
763
764
765
766 /***********************************************************************************
767  *                  _          __                                             _
768  *                 (_)        / _|                                           | |
769  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
770  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
771  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
772  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
773  *
774  ***********************************************************************************/
775
776 /**
777  * Enters the emitter functions for handled nodes into the generic
778  * pointer of an opcode.
779  */
780 void ia32_register_emitters(void) {
781         int i;
782
783 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
784 #define EMIT(a)      op_##a->ops.generic = (op_func)emit_##a
785
786         /* first clear all generic operations */
787         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
788                 ir_op *op = get_irp_opcode(i);
789                 op->ops.generic = (op_func)NULL;
790         }
791
792         /* generated int emitter functions */
793         IA32_EMIT(Const);
794
795         IA32_EMIT(Add);
796         IA32_EMIT(Sub);
797         IA32_EMIT(Minus);
798         IA32_EMIT(Inc);
799         IA32_EMIT(Dec);
800
801         IA32_EMIT(Max);
802         IA32_EMIT(Min);
803         IA32_EMIT(CMov);
804
805         IA32_EMIT(And);
806         IA32_EMIT(Or);
807         IA32_EMIT(Eor);
808         IA32_EMIT(Not);
809
810         IA32_EMIT(Shl);
811         IA32_EMIT(Shr);
812         IA32_EMIT(Shrs);
813         IA32_EMIT(RotL);
814         IA32_EMIT(RotR);
815
816         IA32_EMIT(Lea);
817
818         IA32_EMIT(Mul);
819
820         IA32_EMIT(Cdq);
821         IA32_EMIT(DivMod);
822
823         IA32_EMIT(Store);
824         IA32_EMIT(Load);
825
826         IA32_EMIT(CopyB);
827         IA32_EMIT(CopyB_i);
828
829         /* generated floating point emitter */
830         IA32_EMIT(fConst);
831
832         IA32_EMIT(fAdd);
833         IA32_EMIT(fSub);
834
835         IA32_EMIT(fMul);
836         IA32_EMIT(fDiv);
837
838         IA32_EMIT(fMin);
839         IA32_EMIT(fMax);
840
841         IA32_EMIT(fLoad);
842         IA32_EMIT(fStore);
843
844         /* other emitter functions */
845         IA32_EMIT(CondJmp);
846         IA32_EMIT(SwitchJmp);
847         IA32_EMIT(Call);
848
849         EMIT(Jmp);
850         EMIT(Proj);
851
852 #undef IA32_EMIT
853 #undef EMIT
854 }
855
856 /**
857  * Emits code for a node.
858  */
859 static void ia32_emit_node(ir_node *irn, void *env) {
860         emit_env_t *emit_env   = env;
861         firm_dbg_module_t *mod = emit_env->mod;
862         FILE              *F   = emit_env->out;
863         ir_op             *op = get_irn_op(irn);
864
865         DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
866
867         if (op->ops.generic) {
868                 void (*emit)(ir_node *, void *) = (void (*)(ir_node *, void *))op->ops.generic;
869                 (*emit)(irn, env);
870         }
871
872         ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
873 }
874
875 /**
876  * Walks over the nodes in a block connected by scheduling edges
877  * and emits code for each node.
878  */
879 static void ia32_gen_block(ir_node *block, void *env) {
880         ir_node *irn;
881
882         if (! is_Block(block))
883                 return;
884
885         fprintf(((emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
886         sched_foreach(block, irn) {
887                 ia32_emit_node(irn, env);
888         }
889 }
890
891
892 /**
893  * Emits code for function start.
894  */
895 void ia32_emit_start(FILE *F, ir_graph *irg) {
896         const char *irg_name = get_entity_name(get_irg_entity(irg));
897
898         fprintf(F, "\t.text\n");
899         fprintf(F, ".globl %s\n", irg_name);
900         fprintf(F, "\t.type\t%s, @function\n", irg_name);
901         fprintf(F, "%s:\n", irg_name);
902 }
903
904 /**
905  * Emits code for function end
906  */
907 void ia32_emit_end(FILE *F, ir_graph *irg) {
908         const char *irg_name = get_entity_name(get_irg_entity(irg));
909
910         fprintf(F, "\tret\n");
911         fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
912 }
913
914 /**
915  * Sets labels for control flow nodes (jump target)
916  * TODO: Jump optimization
917  */
918 void ia32_gen_labels(ir_node *block, void *env) {
919         ir_node *pred;
920         int n = get_Block_n_cfgpreds(block);
921
922         for (n--; n >= 0; n--) {
923                 pred = get_Block_cfgpred(block, n);
924                 set_irn_link(pred, block);
925         }
926 }
927
928 /**
929  * Main driver. Emits the code for one routine.
930  */
931 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
932         emit_env_t emit_env;
933
934         emit_env.mod      = firm_dbg_register("ir.be.codegen.ia32");
935         emit_env.out      = F;
936         emit_env.arch_env = cg->arch_env;
937         emit_env.cg       = cg;
938
939         /* set the global arch_env (needed by print hooks) */
940         arch_env = cg->arch_env;
941
942         ia32_emit_start(F, irg);
943         irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
944         irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
945         ia32_emit_end(F, irg);
946 }