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