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