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