fixed register name print
[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_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
255                 lc_arg_register(env, "ia32:sreg", 's', &ia32_reg_handler);
256                 lc_arg_register(env, "ia32:dreg", 'd', &ia32_reg_handler);
257                 lc_arg_register(env, "ia32:cnst", 'c', &ia32_const_handler);
258                 lc_arg_register(env, "ia32:offs", 'o', &ia32_const_handler);
259                 lc_arg_register(env, "ia32:mode", 'm', &ia32_mode_handler);
260         }
261
262         return env;
263 }
264
265 /**
266  * For 2-address code we need to make sure the first src reg is equal to dest reg.
267  */
268 void equalize_dest_src(FILE *F, ir_node *n) {
269         if (get_ia32_reg_nr(n, 0, 1) != get_ia32_reg_nr(n, 0, 0)) {
270                 if (get_irn_arity(n) > 1 && get_ia32_reg_nr(n, 1, 1) == get_ia32_reg_nr(n, 0, 0)) {
271                         if (! is_op_commutative(get_irn_op(n))) {
272                                 /* we only need to exchange for non-commutative ops */
273                                 lc_efprintf(ia32_get_arg_env(), F, "\txchg %1s, %2s\t\t\t/* xchg src1 <-> src2 for 2 address code */\n", n, n);
274                         }
275                 }
276                 else {
277                         lc_efprintf(ia32_get_arg_env(), F, "\tmovl %1s, %1d\t\t\t/* src -> dest for 2 address code */\n", n, n);
278                 }
279         }
280 }
281
282 /*
283  * Add a number to a prefix. This number will not be used a second time.
284  */
285 char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
286         static unsigned long id = 0;
287         snprintf(buf, buflen, "%s%lu", prefix, ++id);
288         return buf;
289 }
290
291
292 /*************************************************
293  *                 _ _                         _
294  *                (_) |                       | |
295  *   ___ _ __ ___  _| |_    ___ ___  _ __   __| |
296  *  / _ \ '_ ` _ \| | __|  / __/ _ \| '_ \ / _` |
297  * |  __/ | | | | | | |_  | (_| (_) | | | | (_| |
298  *  \___|_| |_| |_|_|\__|  \___\___/|_| |_|\__,_|
299  *
300  *************************************************/
301
302 /*
303  * coding of conditions
304  */
305 struct cmp2conditon_t {
306         const char *name;
307         pn_Cmp      num;
308 };
309
310 /*
311  * positive conditions for signed compares
312  */
313 static const struct cmp2conditon_t cmp2condition_s[] = {
314   { NULL,              pn_Cmp_False },  /* always false */
315   { "e",               pn_Cmp_Eq },     /* == */
316   { "l",               pn_Cmp_Lt },     /* < */
317   { "le",              pn_Cmp_Le },     /* <= */
318   { "g",               pn_Cmp_Gt },     /* > */
319   { "ge",              pn_Cmp_Ge },     /* >= */
320   { "ne",              pn_Cmp_Lg },     /* != */
321   { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
322   { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
323   { "unordered or ==", pn_Cmp_Ue },     /* Floating point: unordered or == */
324   { "unordered or <",  pn_Cmp_Ul },     /* Floating point: unordered or < */
325   { "unordered or <=", pn_Cmp_Ule },    /* Floating point: unordered or <= */
326   { "unordered or >",  pn_Cmp_Ug },     /* Floating point: unordered or > */
327   { "unordered or >=", pn_Cmp_Uge },    /* Floating point: unordered or >= */
328   { "unordered or !=", pn_Cmp_Ne },     /* Floating point: unordered or != */
329   { NULL,              pn_Cmp_True },   /* always true */
330 };
331
332 /*
333  * positive conditions for unsigned compares
334  */
335 static const struct cmp2conditon_t cmp2condition_u[] = {
336   { NULL,              pn_Cmp_False },  /* always false */
337   { "e",               pn_Cmp_Eq },     /* == */
338   { "b",               pn_Cmp_Lt },     /* < */
339   { "be",              pn_Cmp_Le },     /* <= */
340   { "a",               pn_Cmp_Gt },     /* > */
341   { "ae",              pn_Cmp_Ge },     /* >= */
342   { "ne",              pn_Cmp_Lg },     /* != */
343   { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
344   { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
345   { "unordered or ==", pn_Cmp_Ue },     /* Floating point: unordered or == */
346   { "unordered or <",  pn_Cmp_Ul },     /* Floating point: unordered or < */
347   { "unordered or <=", pn_Cmp_Ule },    /* Floating point: unordered or <= */
348   { "unordered or >",  pn_Cmp_Ug },     /* Floating point: unordered or > */
349   { "unordered or >=", pn_Cmp_Uge },    /* Floating point: unordered or >= */
350   { "unordered or !=", pn_Cmp_Ne },     /* Floating point: unordered or != */
351   { NULL,              pn_Cmp_True },   /* always true */
352 };
353
354 /*
355  * returns the condition code
356  */
357 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
358 {
359         assert(cmp2condition_s[cmp_code].num == cmp_code);
360         assert(cmp2condition_u[cmp_code].num == cmp_code);
361
362         return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
363 }
364
365 /**
366  * Returns the target label for a control flow node.
367  */
368 static char *get_cfop_target(const ir_node *irn, char *buf) {
369         ir_node *bl = get_irn_link(irn);
370
371         snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
372         return buf;
373 }
374
375 /**
376  * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
377  */
378 static void finish_CondJmp(FILE *F, ir_node *irn) {
379         const ir_node   *proj;
380         const ir_edge_t *edge;
381         char buf[SNPRINTF_BUF_LEN];
382
383         edge = get_irn_out_edge_first(irn);
384         proj = get_edge_src_irn(edge);
385         assert(is_Proj(proj) && "CondJmp with a non-Proj");
386
387         if (get_Proj_proj(proj) == 1) {
388                 fprintf(F, "\tj%s %s\t\t\t/* cmp(a, b) == TRUE */\n",
389                                         get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
390                                         get_cfop_target(proj, buf));
391         }
392         else  {
393                 fprintf(F, "\tjn%s %s\t\t\t/* cmp(a, b) == FALSE */\n",
394                                         get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
395                                         get_cfop_target(proj, buf));
396         }
397
398         edge = get_irn_out_edge_next(irn, edge);
399         if (edge) {
400                 proj = get_edge_src_irn(edge);
401                 assert(is_Proj(proj) && "CondJmp with a non-Proj");
402                 fprintf(F, "\tjmp %s\t\t\t/* otherwise */\n", get_cfop_target(proj, buf));
403         }
404 }
405
406 /**
407  * Emits code for conditional jump with two variables.
408  */
409 static void emit_ia32_CondJmp(ir_node *irn, emit_env_t *env) {
410         FILE *F = env->out;
411
412         lc_efprintf(ia32_get_arg_env(), F, "\tcmp %2s, %1s\t\t\t/* CondJmp(%+F, %+F) */\n", irn, irn,
413                                                                                                                                         get_irn_n(irn, 0), get_irn_n(irn, 1));
414         finish_CondJmp(F, irn);
415 }
416
417 /**
418  * Emits code for conditional jump with immediate.
419  */
420 void emit_ia32_CondJmp_i(ir_node *irn, emit_env_t *env) {
421         FILE *F = env->out;
422
423         lc_efprintf(ia32_get_arg_env(), F, "\tcmp %c, %1s\t\t\t/* CondJmp_i(%+F) */\n", irn, irn, get_irn_n(irn, 0));
424         finish_CondJmp(F, irn);
425 }
426
427
428
429 /*********************************************************
430  *                 _ _       _
431  *                (_) |     (_)
432  *   ___ _ __ ___  _| |_     _ _   _ _ __ ___  _ __  ___
433  *  / _ \ '_ ` _ \| | __|   | | | | | '_ ` _ \| '_ \/ __|
434  * |  __/ | | | | | | |_    | | |_| | | | | | | |_) \__ \
435  *  \___|_| |_| |_|_|\__|   | |\__,_|_| |_| |_| .__/|___/
436  *                         _/ |               | |
437  *                        |__/                |_|
438  *********************************************************/
439
440 /* jump table entry (target and corresponding number) */
441 typedef struct _branch_t {
442         ir_node *target;
443         int      value;
444 } branch_t;
445
446 /* jump table for switch generation */
447 typedef struct _jmp_tbl_t {
448         ir_node  *defProj;         /**< default target */
449         int       min_value;       /**< smallest switch case */
450         int       max_value;       /**< largest switch case */
451         int       num_branches;    /**< number of jumps */
452         char     *label;           /**< label of the jump table */
453         branch_t *branches;        /**< jump array */
454 } jmp_tbl_t;
455
456 /**
457  * Compare two variables of type branch_t. Used to sort all switch cases
458  */
459 static int ia32_cmp_branch_t(const void *a, const void *b) {
460         branch_t *b1 = (branch_t *)a;
461         branch_t *b2 = (branch_t *)b;
462
463         if (b1->value <= b2->value)
464                 return -1;
465         else
466                 return 1;
467 }
468
469 /**
470  * Emits code for a SwitchJmp (creates a jump table if
471  * possible otherwise a cmp-jmp cascade). Port from
472  * cggg ia32 backend
473  */
474 void emit_ia32_SwitchJmp(const ir_node *irn, emit_env_t *emit_env) {
475         unsigned long       interval;
476         char                buf[SNPRINTF_BUF_LEN];
477         int                 last_value, i, pn, do_jmp_tbl = 1;
478         jmp_tbl_t           tbl;
479         ir_node            *proj;
480         const ir_edge_t    *edge;
481         const lc_arg_env_t *env = ia32_get_arg_env();
482         FILE               *F   = emit_env->out;
483
484         /* fill the table structure */
485         tbl.label        = malloc(SNPRINTF_BUF_LEN);
486         tbl.label        = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
487         tbl.defProj      = NULL;
488         tbl.num_branches = get_irn_n_edges(irn);
489         tbl.branches     = calloc(tbl.num_branches, sizeof(tbl.branches[0]));
490         tbl.min_value    = INT_MAX;
491         tbl.max_value    = INT_MIN;
492
493         i = 0;
494         /* go over all proj's and collect them */
495         foreach_out_edge(irn, edge) {
496                 proj = get_edge_src_irn(edge);
497                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
498
499                 pn = get_Proj_proj(proj);
500
501                 /* create branch entry */
502                 tbl.branches[i].target = proj;
503                 tbl.branches[i].value  = pn;
504
505                 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
506                 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
507
508                 /* check for default proj */
509                 if (pn == get_ia32_pncode(irn)) {
510                         assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
511                         tbl.defProj = proj;
512                 }
513
514                 i++;
515         }
516
517         /* sort the branches by their number */
518         qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
519
520         /* two-complement's magic make this work without overflow */
521         interval = tbl.max_value - tbl.min_value;
522
523         /* check value interval */
524         if (interval > 16 * 1024) {
525                 do_jmp_tbl = 0;
526         }
527
528         /* check ratio of value interval to number of branches */
529         if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
530                 do_jmp_tbl = 0;
531         }
532
533         if (do_jmp_tbl) {
534                 /* emit the table */
535                 if (tbl.min_value != 0) {
536                         fprintf(F, "\tcmpl %lu, -%d", interval, tbl.min_value);
537                         lc_efprintf(env, F, "(%1s)\t\t/* first switch value is not 0 */\n", irn);
538                 }
539                 else {
540                         fprintf(F, "\tcmpl %lu, ", interval);
541                         lc_efprintf(env, F, "%1s\t\t\t/* compare for switch */\n", irn);
542                 }
543
544                 fprintf(F, "\tja %s\t\t\t/* default jump if out of range  */\n", get_cfop_target(tbl.defProj, buf));
545
546                 if (tbl.num_branches > 1) {
547                         /* create table */
548
549                         fprintf(F, "\tjmp *%s", tbl.label);
550                         lc_efprintf(env, F, "(,%1s,4)\t\t/* get jump table entry as target */\n", irn);
551
552                         fprintf(F, "\t.section\t.rodata\t\t/* start jump table */\n");
553                         fprintf(F, "\t.align 4\n");
554
555                         fprintf(F, "%s:\n", tbl.label);
556                         fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[0].target, buf), tbl.branches[0].value);
557
558                         last_value = tbl.branches[0].value;
559                         for (i = 1; i < tbl.num_branches; ++i) {
560                                 while (++last_value < tbl.branches[i].value) {
561                                         fprintf(F, "\t.long %s\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
562                                 }
563                                 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[i].target, buf), last_value);
564                         }
565
566                         fprintf(F, "\t.text\t\t\t\t/* end of jump table */\n");
567                 }
568                 else {
569                         /* one jump is enough */
570                         fprintf(F, "\tjmp %s\t\t/* only one case given */\n", get_cfop_target(tbl.branches[0].target, buf));
571                 }
572         }
573         else { // no jump table
574                 for (i = 0; i < tbl.num_branches; ++i) {
575                         fprintf(F, "\tcmpl %d, ", tbl.branches[i].value);
576                         lc_efprintf(env, F, "%1s", irn);
577                         fprintf(F, "\t\t\t/* case %d */\n", tbl.branches[i].value);
578                         fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
579                 }
580
581                 fprintf(F, "\tjmp %s\t\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
582         }
583
584         if (tbl.label)
585                 free(tbl.label);
586         if (tbl.branches)
587                 free(tbl.branches);
588 }
589
590 /**
591  * Emits code for a unconditional jump.
592  */
593 void emit_Jmp(ir_node *irn, emit_env_t *env) {
594         FILE *F = env->out;
595
596         char buf[SNPRINTF_BUF_LEN];
597         ir_fprintf(F, "\tjmp %s\t\t\t/* Jmp(%+F) */\n", get_cfop_target(irn, buf), get_irn_link(irn));
598 }
599
600
601
602 /****************************
603  *                  _
604  *                 (_)
605  *  _ __  _ __ ___  _  ___
606  * | '_ \| '__/ _ \| |/ __|
607  * | |_) | | | (_) | |\__ \
608  * | .__/|_|  \___/| ||___/
609  * | |            _/ |
610  * |_|           |__/
611  ****************************/
612
613 /**
614  * Emits code for a proj -> node
615  */
616 void emit_Proj(ir_node *irn, emit_env_t *env) {
617         ir_node *pred = get_Proj_pred(irn);
618
619         if (get_irn_opcode(pred) == iro_Start) {
620                 switch(get_Proj_proj(irn)) {
621                         case pn_Start_X_initial_exec:
622                                 emit_Jmp(irn, env);
623                                 break;
624                         default:
625                                 break;
626                 }
627         }
628 }
629
630
631
632 /***********************************************************************************
633  *                  _          __                                             _
634  *                 (_)        / _|                                           | |
635  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
636  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
637  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
638  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
639  *
640  ***********************************************************************************/
641
642 /**
643  * Emits code for a node.
644  */
645 void ia32_emit_node(ir_node *irn, void *env) {
646         emit_env_t *emit_env   = env;
647         firm_dbg_module_t *mod = emit_env->mod;
648         FILE              *F   = emit_env->out;
649
650         DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
651
652 #define IA32_EMIT(a) if (is_ia32_##a(irn))               { emit_ia32_##a(irn, emit_env); return; }
653 #define EMIT(a)      if (get_irn_opcode(irn) == iro_##a) { emit_##a(irn, emit_env); return; }
654
655         /* generated int emitter functions */
656         IA32_EMIT(Copy);
657         IA32_EMIT(Perm);
658
659         IA32_EMIT(Const);
660
661         IA32_EMIT(Add);
662         IA32_EMIT(Add_i);
663         IA32_EMIT(Sub);
664         IA32_EMIT(Sub_i);
665         IA32_EMIT(Minus);
666         IA32_EMIT(Inc);
667         IA32_EMIT(Dec);
668
669         IA32_EMIT(Max);
670         IA32_EMIT(Min);
671
672         IA32_EMIT(And);
673         IA32_EMIT(And_i);
674         IA32_EMIT(Or);
675         IA32_EMIT(Or_i);
676         IA32_EMIT(Eor);
677         IA32_EMIT(Eor_i);
678         IA32_EMIT(Not);
679
680         IA32_EMIT(Shl);
681         IA32_EMIT(Shl_i);
682         IA32_EMIT(Shr);
683         IA32_EMIT(Shr_i);
684         IA32_EMIT(Shrs);
685         IA32_EMIT(Shrs_i);
686         IA32_EMIT(RotL);
687         IA32_EMIT(RotL_i);
688         IA32_EMIT(RotR);
689
690         IA32_EMIT(Lea);
691         IA32_EMIT(Lea_i);
692
693         IA32_EMIT(Mul);
694         IA32_EMIT(Mul_i);
695         IA32_EMIT(Mulh);
696         IA32_EMIT(Mulh_i);
697
698         IA32_EMIT(Cltd);
699         IA32_EMIT(DivMod);
700
701         IA32_EMIT(Store);
702         IA32_EMIT(Load);
703
704         /* generated floating point emitter */
705         IA32_EMIT(fConst);
706
707         IA32_EMIT(fAdd);
708         IA32_EMIT(fSub);
709         IA32_EMIT(fMinus);
710
711         IA32_EMIT(fMul);
712         IA32_EMIT(fDiv);
713
714         IA32_EMIT(fMin);
715         IA32_EMIT(fMax);
716
717         IA32_EMIT(fLoad);
718         IA32_EMIT(fStore);
719
720         /* other emitter functions */
721         IA32_EMIT(CondJmp);
722         IA32_EMIT(CondJmp_i);
723         IA32_EMIT(SwitchJmp);
724
725         EMIT(Jmp);
726         EMIT(Proj);
727
728         ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
729 }
730
731 /**
732  * Walks over the nodes in a block connected by scheduling edges
733  * and emits code for each node.
734  */
735 void ia32_gen_block(ir_node *block, void *env) {
736         ir_node *irn;
737
738         if (! is_Block(block))
739                 return;
740
741         fprintf(((emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
742         sched_foreach(block, irn) {
743                 ia32_emit_node(irn, env);
744         }
745 }
746
747
748 /**
749  * Emits code for function start.
750  */
751 void ia32_emit_start(FILE *F, ir_graph *irg) {
752         const char *irg_name = get_entity_name(get_irg_entity(irg));
753
754         fprintf(F, "\t.text\n");
755         fprintf(F, ".globl %s\n", irg_name);
756         fprintf(F, "\t.type\t%s, @function\n", irg_name);
757         fprintf(F, "%s:\n", irg_name);
758 }
759
760 /**
761  * Emits code for function end
762  */
763 void ia32_emit_end(FILE *F, ir_graph *irg) {
764         const char *irg_name = get_entity_name(get_irg_entity(irg));
765
766         fprintf(F, "\tret\n");
767         fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
768 }
769
770 /**
771  * Sets labels for control flow nodes (jump target)
772  * TODO: Jump optimization
773  */
774 void ia32_gen_labels(ir_node *block, void *env) {
775         ir_node *pred;
776         int n = get_Block_n_cfgpreds(block);
777
778         for (n--; n >= 0; n--) {
779                 pred = get_Block_cfgpred(block, n);
780                 set_irn_link(pred, block);
781         }
782 }
783
784 /**
785  * Main driver
786  */
787 void ia32_gen_routine(FILE *F, ir_graph *irg, const arch_env_t *env) {
788         emit_env_t emit_env;
789
790         emit_env.mod      = firm_dbg_register("ir.be.codegen.ia32");
791         emit_env.out      = F;
792         emit_env.arch_env = env;
793
794         arch_env = env;
795
796         ia32_emit_start(F, irg);
797         irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
798         irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
799         ia32_emit_end(F, irg);
800 }