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