replaced malloc by xmalloc
[libfirm] / ir / be / ia32 / ia32_emitter.c
1 /**
2  * This file implements the node emitter.
3  *
4  * $Id$
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <limits.h>
12
13 #include "xmalloc.h"
14 #include "tv.h"
15 #include "iredges.h"
16 #include "debug.h"
17 #include "irgwalk.h"
18 #include "irprintf.h"
19 #include "irop_t.h"
20 #include "irargs_t.h"
21 #include "irprog_t.h"
22 #include "iredges_t.h"
23
24 #include "../besched_t.h"
25 #include "../benode_t.h"
26
27 #include "ia32_emitter.h"
28 #include "gen_ia32_emitter.h"
29 #include "ia32_nodes_attr.h"
30 #include "ia32_new_nodes.h"
31 #include "ia32_map_regs.h"
32
33 #ifdef obstack_chunk_alloc
34 # undef obstack_chunk_alloc
35 # define obstack_chunk_alloc xmalloc
36 #else
37 # define obstack_chunk_alloc xmalloc
38 # define obstack_chunk_free free
39 #endif
40
41 #define BLOCK_PREFIX(x) ".L" x
42
43 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
44
45 #define SNPRINTF_BUF_LEN 128
46
47 /* global arch_env for lc_printf functions */
48 static const arch_env_t *arch_env = NULL;
49
50 /* indicates whether blocks are scheduled or not
51    (this variable is set automatically) */
52 static int have_block_sched       = 0;
53
54 /*************************************************************
55  *             _       _    __   _          _
56  *            (_)     | |  / _| | |        | |
57  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
58  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
59  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
60  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
61  * | |                                       | |
62  * |_|                                       |_|
63  *************************************************************/
64
65 /* We always pass the ir_node which is a pointer. */
66 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
67         return lc_arg_type_ptr;
68 }
69
70
71 /**
72  * Returns the register at in position pos.
73  */
74 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
75         ir_node                *op;
76         const arch_register_t  *reg = NULL;
77
78         assert(get_irn_arity(irn) > pos && "Invalid IN position");
79
80         /* The out register of the operator at position pos is the
81            in register we need. */
82         op = get_irn_n(irn, pos);
83
84         reg = arch_get_irn_register(arch_env, op);
85
86         assert(reg && "no in register found");
87         return reg;
88 }
89
90 /**
91  * Returns the register at out position pos.
92  */
93 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
94         ir_node                *proj;
95         const arch_register_t  *reg = NULL;
96
97         /* 1st case: irn is not of mode_T, so it has only                 */
98         /*           one OUT register -> good                             */
99         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
100         /*           Proj with the corresponding projnum for the register */
101
102         if (get_irn_mode(irn) != mode_T) {
103                 reg = arch_get_irn_register(arch_env, irn);
104         }
105         else if (is_ia32_irn(irn)) {
106                 reg = get_ia32_out_reg(irn, pos);
107         }
108         else {
109                 const ir_edge_t *edge;
110
111                 foreach_out_edge(irn, edge) {
112                         proj = get_edge_src_irn(edge);
113                         assert(is_Proj(proj) && "non-Proj from mode_T node");
114                         if (get_Proj_proj(proj) == pos) {
115                                 reg = arch_get_irn_register(arch_env, proj);
116                                 break;
117                         }
118                 }
119         }
120
121         assert(reg && "no out register found");
122         return reg;
123 }
124
125 enum io_direction {
126   IN_REG,
127   OUT_REG
128 };
129
130 /**
131  * Returns the name of the in register at position pos.
132  */
133 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
134         const arch_register_t *reg;
135         const char            *name;
136         static char           *buf = NULL;
137         int                    len;
138
139         if (in_out == IN_REG) {
140                 reg = get_in_reg(irn, pos);
141         }
142         else {
143                 /* destination address mode nodes don't have outputs */
144                 if (is_ia32_irn(irn) && get_ia32_op_type(irn) == ia32_AddrModeD) {
145                         return "MEM";
146                 }
147
148                 reg = get_out_reg(irn, pos);
149         }
150
151         name = arch_register_get_name(reg);
152
153         if (buf) {
154                 free(buf);
155         }
156
157         len = strlen(name) + 2;
158         buf = xcalloc(1, len);
159
160         snprintf(buf, len, "%%%s", name);
161
162         return buf;
163 }
164
165 /**
166  * Get the register name for a node.
167  */
168 static int ia32_get_reg_name(lc_appendable_t *app,
169     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
170 {
171         const char *buf;
172         ir_node    *X  = arg->v_ptr;
173         int         nr = occ->width - 1;
174
175         if (!X)
176                 return lc_appendable_snadd(app, "(null)", 6);
177
178         buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
179
180         return lc_appendable_snadd(app, buf, strlen(buf));
181 }
182
183 /**
184  * Returns the tarval, offset or scale of an ia32 as a string.
185  */
186 static int ia32_const_to_str(lc_appendable_t *app,
187     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
188 {
189         const char *buf;
190         ir_node    *X = arg->v_ptr;
191
192         if (!X)
193                 return lc_arg_append(app, occ, "(null)", 6);
194
195         if (occ->conversion == 'C') {
196                 buf = get_ia32_cnst(X);
197         }
198         else { /* 'O' */
199                 buf = get_ia32_am_offs(X);
200         }
201
202         return buf ? lc_appendable_snadd(app, buf, strlen(buf)) : 0;
203 }
204
205 /**
206  * Determines the SSE suffix depending on the mode.
207  */
208 static int ia32_get_mode_suffix(lc_appendable_t *app,
209     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
210 {
211         ir_node *X    = arg->v_ptr;
212         ir_mode *mode = get_irn_mode(X);
213
214         if (mode == mode_T) {
215                 mode = is_ia32_AddrModeS(X) || is_ia32_AddrModeD(X) ? get_ia32_ls_mode(X) : get_ia32_res_mode(X);
216         }
217
218         if (!X)
219                 return lc_arg_append(app, occ, "(null)", 6);
220
221         if (mode_is_float(mode)) {
222                 return lc_appendable_chadd(app, get_mode_size_bits(mode) == 32 ? 's' : 'd');
223         }
224         else {
225                 return lc_appendable_chadd(app, mode_is_signed(mode) ? 's' : 'z');
226         }
227 }
228
229 /**
230  * Return the ia32 printf arg environment.
231  * We use the firm environment with some additional handlers.
232  */
233 const lc_arg_env_t *ia32_get_arg_env(void) {
234         static lc_arg_env_t *env = NULL;
235
236         static const lc_arg_handler_t ia32_reg_handler   = { ia32_get_arg_type, ia32_get_reg_name };
237         static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
238         static const lc_arg_handler_t ia32_mode_handler  = { ia32_get_arg_type, ia32_get_mode_suffix };
239
240         if(env == NULL) {
241                 /* extend the firm printer */
242                 env = firm_get_arg_env();
243
244                 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
245                 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
246                 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
247                 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
248                 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
249         }
250
251         return env;
252 }
253
254 /**
255  * Emits registers and/or address mode of a binary operation.
256  */
257 char *ia32_emit_binop(const ir_node *n, ia32_emit_env_t *env) {
258         static char *buf = NULL;
259
260         /* verify that this function is never called on non-AM supporting operations */
261         //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
262
263 #define PRODUCES_RESULT(n)   \
264         (!(is_ia32_St(n)      || \
265         is_ia32_Store8Bit(n)  || \
266         is_ia32_CondJmp(n)    || \
267         is_ia32_fCondJmp(n)   || \
268         is_ia32_SwitchJmp(n)))
269
270         if (! buf) {
271                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
272         }
273         else {
274                 memset(buf, 0, SNPRINTF_BUF_LEN);
275         }
276
277         switch(get_ia32_op_type(n)) {
278                 case ia32_Normal:
279                         if (get_ia32_cnst(n)) {
280                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
281                         }
282                         else {
283                                 const arch_register_t *in1 = get_in_reg(n, 2);
284                                 const arch_register_t *in2 = get_in_reg(n, 3);
285                                 const arch_register_t *out = PRODUCES_RESULT(n) ? get_out_reg(n, 0) : NULL;
286                                 const arch_register_t *in;
287
288                                 in  = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
289                                 out = out ? out : in1;
290
291                                 snprintf(buf, SNPRINTF_BUF_LEN, "%%%s, %%%s", \
292                                         arch_register_get_name(out), arch_register_get_name(in));
293                         }
294                         break;
295                 case ia32_AddrModeS:
296                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%4S, %s", n, ia32_emit_am(n, env));
297                         break;
298                 case ia32_AddrModeD:
299                         if (get_ia32_cnst(n)) {
300                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s,%s%s",
301                                         ia32_emit_am(n, env),
302                                         get_ia32_sc(n) ? " OFFSET FLAT:" : " ",    /* In case of a symconst we must add OFFSET to */
303                                         get_ia32_cnst(n));                         /* tell the assembler to store it's address.   */
304                         }
305                         else {
306                                 const arch_register_t *in1 = get_in_reg(n, 2);
307                                 const char *reg_name;
308                                 ir_mode *mode = get_ia32_res_mode(n);
309
310                                 mode = mode ? mode : get_ia32_ls_mode(n);
311
312                                 switch(get_mode_size_bits(mode)) {
313                                         case 8:
314                                                 reg_name = ia32_get_mapped_reg_name(env->isa->regs_8bit, in1);
315                                                 break;
316                                         case 16:
317                                                 reg_name = ia32_get_mapped_reg_name(env->isa->regs_16bit, in1);
318                                                 break;
319                                         case 32:
320                                                 reg_name = arch_register_get_name(in1);
321                                                 break;
322                                         default:
323                                                 assert(0 && "unsupported mode size");
324                                                 break;
325                                 }
326
327                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %%%s", ia32_emit_am(n, env), reg_name);
328                         }
329                         break;
330                 default:
331                         assert(0 && "unsupported op type");
332         }
333
334 #undef PRODUCES_RESULT
335
336         return buf;
337 }
338
339 /**
340  * Emits registers and/or address mode of a unary operation.
341  */
342 char *ia32_emit_unop(const ir_node *n, ia32_emit_env_t *env) {
343         static char *buf = NULL;
344
345         if (! buf) {
346                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
347         }
348         else {
349                 memset(buf, 0, SNPRINTF_BUF_LEN);
350         }
351
352         switch(get_ia32_op_type(n)) {
353                 case ia32_Normal:
354                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
355                         break;
356                 case ia32_am_Dest:
357                         snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n, env));
358                         break;
359                 default:
360                         assert(0 && "unsupported op type");
361         }
362
363         return buf;
364 }
365
366 /**
367  * Emits address mode.
368  */
369 char *ia32_emit_am(const ir_node *n, ia32_emit_env_t *env) {
370         ia32_am_flavour_t am_flav    = get_ia32_am_flavour(n);
371         int               had_output = 0;
372         char             *s;
373         int               size;
374         static struct obstack *obst  = NULL;
375         ir_mode *mode = get_ia32_ls_mode(n);
376
377         if (! is_ia32_Lea(n))
378                 assert(mode && "AM node must have ls_mode attribute set.");
379
380         if (! obst) {
381                 obst = xcalloc(1, sizeof(*obst));
382         }
383         else {
384                 obstack_free(obst, NULL);
385         }
386
387         /* obstack_free with NULL results in an uninitialized obstack */
388         obstack_init(obst);
389
390         if (mode) {
391                 switch (get_mode_size_bits(mode)) {
392                         case 8:
393                                 obstack_printf(obst, "BYTE PTR ");
394                                 break;
395                         case 16:
396                                 obstack_printf(obst, "WORD PTR ");
397                                 break;
398                         case 32:
399                                 obstack_printf(obst, "DWORD PTR ");
400                                 break;
401                         default:
402                                 break;
403                 }
404         }
405
406         obstack_printf(obst, "[");
407
408         if (am_flav & ia32_B) {
409                 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
410                 had_output = 1;
411         }
412
413         if (am_flav & ia32_I) {
414                 if (had_output) {
415                         obstack_printf(obst, "+");
416                 }
417
418                 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
419
420                 if (am_flav & ia32_S) {
421                         obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
422                 }
423
424                 had_output = 1;
425         }
426
427         if (am_flav & ia32_O) {
428                 obstack_printf(obst, get_ia32_am_offs(n));
429         }
430
431         obstack_printf(obst, "] ");
432
433         size        = obstack_object_size(obst);
434         s           = obstack_finish(obst);
435         s[size - 1] = '\0';
436
437         return s;
438 }
439
440
441
442 /**
443  * Formated print of commands and comments.
444  */
445 static void ia32_fprintf_format(FILE *F, char *cmd_buf, char *cmnt_buf) {
446         fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
447 }
448
449
450
451 /**
452  * Add a number to a prefix. This number will not be used a second time.
453  */
454 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
455         static unsigned long id = 0;
456         snprintf(buf, buflen, "%s%lu", prefix, ++id);
457         return buf;
458 }
459
460
461
462 /*************************************************
463  *                 _ _                         _
464  *                (_) |                       | |
465  *   ___ _ __ ___  _| |_    ___ ___  _ __   __| |
466  *  / _ \ '_ ` _ \| | __|  / __/ _ \| '_ \ / _` |
467  * |  __/ | | | | | | |_  | (_| (_) | | | | (_| |
468  *  \___|_| |_| |_|_|\__|  \___\___/|_| |_|\__,_|
469  *
470  *************************************************/
471
472 #undef IA32_DO_EMIT
473 #define IA32_DO_EMIT ia32_fprintf_format(F, cmd_buf, cmnt_buf)
474
475 /*
476  * coding of conditions
477  */
478 struct cmp2conditon_t {
479         const char *name;
480         pn_Cmp      num;
481 };
482
483 /*
484  * positive conditions for signed compares
485  */
486 static const struct cmp2conditon_t cmp2condition_s[] = {
487   { NULL,              pn_Cmp_False },  /* always false */
488   { "e",               pn_Cmp_Eq },     /* == */
489   { "l",               pn_Cmp_Lt },     /* < */
490   { "le",              pn_Cmp_Le },     /* <= */
491   { "g",               pn_Cmp_Gt },     /* > */
492   { "ge",              pn_Cmp_Ge },     /* >= */
493   { "ne",              pn_Cmp_Lg },     /* != */
494   { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
495   { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
496   { "unordered or ==", pn_Cmp_Ue },     /* Floating point: unordered or == */
497   { "unordered or <",  pn_Cmp_Ul },     /* Floating point: unordered or < */
498   { "unordered or <=", pn_Cmp_Ule },    /* Floating point: unordered or <= */
499   { "unordered or >",  pn_Cmp_Ug },     /* Floating point: unordered or > */
500   { "unordered or >=", pn_Cmp_Uge },    /* Floating point: unordered or >= */
501   { "unordered or !=", pn_Cmp_Ne },     /* Floating point: unordered or != */
502   { NULL,              pn_Cmp_True },   /* always true */
503 };
504
505 /*
506  * positive conditions for unsigned compares
507  */
508 static const struct cmp2conditon_t cmp2condition_u[] = {
509         { NULL,              pn_Cmp_False },  /* always false */
510         { "e",               pn_Cmp_Eq },     /* == */
511         { "b",               pn_Cmp_Lt },     /* < */
512         {       "be",              pn_Cmp_Le },     /* <= */
513         { "a",               pn_Cmp_Gt },     /* > */
514         { "ae",              pn_Cmp_Ge },     /* >= */
515         { "ne",              pn_Cmp_Lg },     /* != */
516         { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
517         { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
518         { "unordered or ==", pn_Cmp_Ue },     /* Floating point: unordered or == */
519         { "unordered or <",  pn_Cmp_Ul },     /* Floating point: unordered or < */
520         { "unordered or <=", pn_Cmp_Ule },    /* Floating point: unordered or <= */
521         { "unordered or >",  pn_Cmp_Ug },     /* Floating point: unordered or > */
522         { "unordered or >=", pn_Cmp_Uge },    /* Floating point: unordered or >= */
523         { "unordered or !=", pn_Cmp_Ne },     /* Floating point: unordered or != */
524         { NULL,              pn_Cmp_True },   /* always true */
525 };
526
527 /*
528  * returns the condition code
529  */
530 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
531 {
532         assert(cmp2condition_s[cmp_code].num == cmp_code);
533         assert(cmp2condition_u[cmp_code].num == cmp_code);
534
535         return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
536 }
537
538 /**
539  * Returns the target block for a control flow node.
540  */
541 static ir_node *get_cfop_target_block(const ir_node *irn) {
542         return get_irn_link(irn);
543 }
544
545 /**
546  * Returns the target label for a control flow node.
547  */
548 static char *get_cfop_target(const ir_node *irn, char *buf) {
549         ir_node *bl = get_cfop_target_block(irn);
550
551         snprintf(buf, SNPRINTF_BUF_LEN, BLOCK_PREFIX("%ld"), get_irn_node_nr(bl));
552         return buf;
553 }
554
555 /** Return the next block in Block schedule */
556 static ir_node *next_blk_sched(const ir_node *block) {
557         return have_block_sched ? get_irn_link(block) : NULL;
558 }
559
560 /**
561  * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
562  */
563 static void finish_CondJmp(FILE *F, const ir_node *irn, ir_mode *mode) {
564         const ir_node   *proj1, *proj2 = NULL;
565         const ir_node   *block, *next_bl = NULL;
566         const ir_edge_t *edge;
567         char buf[SNPRINTF_BUF_LEN];
568         char cmd_buf[SNPRINTF_BUF_LEN];
569         char cmnt_buf[SNPRINTF_BUF_LEN];
570
571         /* get both Proj's */
572         edge = get_irn_out_edge_first(irn);
573         proj1 = get_edge_src_irn(edge);
574         assert(is_Proj(proj1) && "CondJmp with a non-Proj");
575
576         edge = get_irn_out_edge_next(irn, edge);
577         if (edge) {
578                 proj2 = get_edge_src_irn(edge);
579                 assert(is_Proj(proj2) && "CondJmp with a non-Proj");
580         }
581
582         /* for now, the code works for scheduled and non-schedules blocks */
583         block = get_nodes_block(irn);
584         if (proj2) {
585                 /* we have a block schedule */
586                 next_bl = next_blk_sched(block);
587
588                 if (get_cfop_target_block(proj1) == next_bl) {
589                         /* exchange both proj's so the second one can be omitted */
590                         const ir_node *t = proj1;
591                         proj1 = proj2;
592                         proj2 = t;
593                 }
594         }
595
596         /* the first Proj must always be created */
597         if (get_Proj_proj(proj1) == pn_Cond_true) {
598                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
599                                         get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
600                                         get_cfop_target(proj1, buf));
601                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == TRUE */");
602         }
603         else  {
604                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
605                                         get_cmp_suffix(get_negated_pnc(get_ia32_pncode(irn), mode),
606                                         !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
607                                         get_cfop_target(proj1, buf));
608                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == FALSE */");
609         }
610         IA32_DO_EMIT;
611
612         /* the second Proj might be a fallthrough */
613         if (proj2) {
614                 if (get_cfop_target_block(proj2) != next_bl) {
615                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj2, buf));
616                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* otherwise */");
617                 }
618                 else {
619                         cmd_buf[0] = '\0';
620                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrogh %s */", get_cfop_target(proj2, buf));
621                 }
622                 IA32_DO_EMIT;
623         }
624 }
625
626 /**
627  * Emits code for conditional jump.
628  */
629 static void CondJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
630         FILE *F = env->out;
631         char cmd_buf[SNPRINTF_BUF_LEN];
632         char cmnt_buf[SNPRINTF_BUF_LEN];
633
634         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
635         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
636         IA32_DO_EMIT;
637         finish_CondJmp(F, irn, get_irn_mode(get_irn_n(irn, 2)));
638 }
639
640 /**
641  * Emits code for conditional jump with two variables.
642  */
643 static void emit_ia32_CondJmp(const ir_node *irn, ia32_emit_env_t *env) {
644         CondJmp_emitter(irn, env);
645 }
646
647 /**
648  * Emits code for conditional jump with immediate.
649  */
650 void emit_ia32_CondJmp_i(const ir_node *irn, ia32_emit_env_t *env) {
651         CondJmp_emitter(irn, env);
652 }
653
654 /**
655  * Emits code for conditional test and jump.
656  */
657 static void TestJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
658         FILE       *F   = env->out;
659         const char *op1 = arch_register_get_name(get_in_reg(irn, 0));
660         const char *op2 = get_ia32_cnst(irn);
661         char        cmd_buf[SNPRINTF_BUF_LEN];
662         char        cmnt_buf[SNPRINTF_BUF_LEN];
663
664         if (! op2)
665                 op2 = arch_register_get_name(get_in_reg(irn, 1));
666
667         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "test %%%s,%s%s ", op1, get_ia32_cnst(irn) ? " " : " %", op2);
668         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
669         IA32_DO_EMIT;
670         finish_CondJmp(F, irn, get_irn_mode(get_irn_n(irn, 0)));
671 }
672
673 /**
674  * Emits code for conditional test and jump with two variables.
675  */
676 static void emit_ia32_TestJmp(const ir_node *irn, ia32_emit_env_t *env) {
677         TestJmp_emitter(irn, env);
678 }
679
680
681
682 /*********************************************************
683  *                 _ _       _
684  *                (_) |     (_)
685  *   ___ _ __ ___  _| |_     _ _   _ _ __ ___  _ __  ___
686  *  / _ \ '_ ` _ \| | __|   | | | | | '_ ` _ \| '_ \/ __|
687  * |  __/ | | | | | | |_    | | |_| | | | | | | |_) \__ \
688  *  \___|_| |_| |_|_|\__|   | |\__,_|_| |_| |_| .__/|___/
689  *                         _/ |               | |
690  *                        |__/                |_|
691  *********************************************************/
692
693 /* jump table entry (target and corresponding number) */
694 typedef struct _branch_t {
695         ir_node *target;
696         int      value;
697 } branch_t;
698
699 /* jump table for switch generation */
700 typedef struct _jmp_tbl_t {
701         ir_node  *defProj;         /**< default target */
702         int       min_value;       /**< smallest switch case */
703         int       max_value;       /**< largest switch case */
704         int       num_branches;    /**< number of jumps */
705         char     *label;           /**< label of the jump table */
706         branch_t *branches;        /**< jump array */
707 } jmp_tbl_t;
708
709 /**
710  * Compare two variables of type branch_t. Used to sort all switch cases
711  */
712 static int ia32_cmp_branch_t(const void *a, const void *b) {
713         branch_t *b1 = (branch_t *)a;
714         branch_t *b2 = (branch_t *)b;
715
716         if (b1->value <= b2->value)
717                 return -1;
718         else
719                 return 1;
720 }
721
722 /**
723  * Emits code for a SwitchJmp (creates a jump table if
724  * possible otherwise a cmp-jmp cascade). Port from
725  * cggg ia32 backend
726  */
727 void emit_ia32_SwitchJmp(const ir_node *irn, ia32_emit_env_t *emit_env) {
728         unsigned long       interval;
729         char                buf[SNPRINTF_BUF_LEN];
730         int                 last_value, i, pn, do_jmp_tbl = 1;
731         jmp_tbl_t           tbl;
732         ir_node            *proj;
733         const ir_edge_t    *edge;
734         const lc_arg_env_t *env = ia32_get_arg_env();
735         FILE               *F   = emit_env->out;
736         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
737
738         /* fill the table structure */
739         tbl.label        = xmalloc(SNPRINTF_BUF_LEN);
740         tbl.label        = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
741         tbl.defProj      = NULL;
742         tbl.num_branches = get_irn_n_edges(irn);
743         tbl.branches     = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
744         tbl.min_value    = INT_MAX;
745         tbl.max_value    = INT_MIN;
746
747         i = 0;
748         /* go over all proj's and collect them */
749         foreach_out_edge(irn, edge) {
750                 proj = get_edge_src_irn(edge);
751                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
752
753                 pn = get_Proj_proj(proj);
754
755                 /* create branch entry */
756                 tbl.branches[i].target = proj;
757                 tbl.branches[i].value  = pn;
758
759                 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
760                 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
761
762                 /* check for default proj */
763                 if (pn == get_ia32_pncode(irn)) {
764                         assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
765                         tbl.defProj = proj;
766                 }
767
768                 i++;
769         }
770
771         /* sort the branches by their number */
772         qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
773
774         /* two-complement's magic make this work without overflow */
775         interval = tbl.max_value - tbl.min_value;
776
777         /* check value interval */
778         if (interval > 16 * 1024) {
779                 do_jmp_tbl = 0;
780         }
781
782         /* check ratio of value interval to number of branches */
783         if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
784                 do_jmp_tbl = 0;
785         }
786
787         if (do_jmp_tbl) {
788                 /* emit the table */
789                 if (tbl.min_value != 0) {
790                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, -%d(%1S)",
791                                 interval, tbl.min_value, irn);
792                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* first switch value is not 0 */");
793
794                         IA32_DO_EMIT;
795                 }
796                 else {
797                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, %1S", interval, irn);
798                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* compare for switch */");
799
800                         IA32_DO_EMIT;
801                 }
802
803                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
804                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default jump if out of range  */");
805                 IA32_DO_EMIT;
806
807                 if (tbl.num_branches > 1) {
808                         /* create table */
809
810                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp [%1S*4+%s]", irn, tbl.label);
811                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get jump table entry as target */");
812                         IA32_DO_EMIT;
813
814                         fprintf(F, "\t.section\t.rodata\n");
815                         fprintf(F, "\t.align 4\n");
816
817                         fprintf(F, "%s:\n", tbl.label);
818
819                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
820                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */\n",  tbl.branches[0].value);
821                         IA32_DO_EMIT;
822
823                         last_value = tbl.branches[0].value;
824                         for (i = 1; i < tbl.num_branches; ++i) {
825                                 while (++last_value < tbl.branches[i].value) {
826                                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
827                                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
828                                         IA32_DO_EMIT;
829                                 }
830                                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf));
831                                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", last_value);
832                                 IA32_DO_EMIT;
833                         }
834
835                         fprintf(F, "\t.text");
836                 }
837                 else {
838                         /* one jump is enough */
839                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
840                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* only one case given */");
841                         IA32_DO_EMIT;
842                 }
843         }
844         else { // no jump table
845                 for (i = 0; i < tbl.num_branches; ++i) {
846                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %d, %1S", tbl.branches[i].value, irn);
847                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", i);
848                         IA32_DO_EMIT;
849                         fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
850                 }
851
852                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.defProj, buf));
853                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
854                 IA32_DO_EMIT;
855         }
856
857         if (tbl.label)
858                 free(tbl.label);
859         if (tbl.branches)
860                 free(tbl.branches);
861 }
862
863 /**
864  * Emits code for a unconditional jump.
865  */
866 void emit_Jmp(const ir_node *irn, ia32_emit_env_t *env) {
867         ir_node *block, *next_bl;
868         FILE *F = env->out;
869         char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
870
871         /* for now, the code works for scheduled and non-schedules blocks */
872         block = get_nodes_block(irn);
873
874         /* we have a block schedule */
875         next_bl = next_blk_sched(block);
876         if (get_cfop_target_block(irn) != next_bl) {
877                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf));
878                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F) */", irn, get_cfop_target_block(irn));
879         }
880         else {
881                 cmd_buf[0] = '\0';
882                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(irn, buf));
883         }
884         IA32_DO_EMIT;
885 }
886
887 /****************************
888  *                  _
889  *                 (_)
890  *  _ __  _ __ ___  _  ___
891  * | '_ \| '__/ _ \| |/ __|
892  * | |_) | | | (_) | |\__ \
893  * | .__/|_|  \___/| ||___/
894  * | |            _/ |
895  * |_|           |__/
896  ****************************/
897
898 /**
899  * Emits code for a proj -> node
900  */
901 void emit_Proj(const ir_node *irn, ia32_emit_env_t *env) {
902         ir_node *pred = get_Proj_pred(irn);
903
904         if (get_irn_op(pred) == op_Start) {
905                 switch(get_Proj_proj(irn)) {
906                         case pn_Start_X_initial_exec:
907                                 emit_Jmp(irn, env);
908                                 break;
909                         default:
910                                 break;
911                 }
912         }
913 }
914
915 /**********************************
916  *   _____                  ____
917  *  / ____|                |  _ \
918  * | |     ___  _ __  _   _| |_) |
919  * | |    / _ \| '_ \| | | |  _ <
920  * | |___| (_) | |_) | |_| | |_) |
921  *  \_____\___/| .__/ \__, |____/
922  *             | |     __/ |
923  *             |_|    |___/
924  **********************************/
925
926 /**
927  * Emit movsb/w instructions to make mov count divideable by 4
928  */
929 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
930         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
931
932         fprintf(F, "\t/* memcopy %d bytes*/\n", size);
933
934         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
935         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward*/");
936         IA32_DO_EMIT;
937
938         switch(rem) {
939                 case 1:
940                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
941                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 1 */");
942                         break;
943                 case 2:
944                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
945                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 2 */");
946                         break;
947                 case 3:
948                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
949                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
950                         IA32_DO_EMIT;
951                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
952                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
953                         break;
954         }
955
956         IA32_DO_EMIT;
957 }
958
959 /**
960  * Emit rep movsd instruction for memcopy.
961  */
962 void emit_ia32_CopyB(const ir_node *irn, ia32_emit_env_t *emit_env) {
963         FILE   *F    = emit_env->out;
964         tarval *tv   = get_ia32_Immop_tarval(irn);
965         int     rem  = get_tarval_long(tv);
966         int     size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
967         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
968
969         emit_CopyB_prolog(F, rem, size);
970
971         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "rep movsd");
972         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy */");
973         IA32_DO_EMIT;
974 }
975
976 /**
977  * Emits unrolled memcopy.
978  */
979 void emit_ia32_CopyB_i(const ir_node *irn, ia32_emit_env_t *emit_env) {
980         tarval *tv   = get_ia32_Immop_tarval(irn);
981         int     size = get_tarval_long(tv);
982         FILE   *F    = emit_env->out;
983         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
984
985         emit_CopyB_prolog(F, size & 0x3, size);
986
987         size >>= 2;
988         while (size--) {
989                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsd");
990                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy unrolled */");
991                 IA32_DO_EMIT;
992         }
993 }
994
995
996
997 /***************************
998  *   _____
999  *  / ____|
1000  * | |     ___  _ ____   __
1001  * | |    / _ \| '_ \ \ / /
1002  * | |___| (_) | | | \ V /
1003  *  \_____\___/|_| |_|\_/
1004  *
1005  ***************************/
1006
1007 /**
1008  * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1009  */
1010 static void emit_ia32_Conv(const ir_node *irn, ia32_emit_env_t *emit_env) {
1011         FILE               *F    = emit_env->out;
1012         const lc_arg_env_t *env  = ia32_get_arg_env();
1013         char               *from, *to, buf[64];
1014         ir_mode *src_mode, *tgt_mode;
1015         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1016
1017         src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
1018         tgt_mode = get_ia32_res_mode(irn);
1019
1020         from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
1021         to   = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
1022
1023         switch(get_ia32_op_type(irn)) {
1024                 case ia32_Normal:
1025                         lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
1026                         break;
1027                 case ia32_AddrModeS:
1028                         lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn, emit_env));
1029                         break;
1030                 default:
1031                         assert(0 && "unsupported op type for Conv");
1032         }
1033
1034         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cvt%s2%s %s", from, to, buf);
1035         lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F, %+F) */", irn, src_mode, tgt_mode);
1036         IA32_DO_EMIT;
1037 }
1038
1039 void emit_ia32_Conv_I2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1040         emit_ia32_Conv(irn, emit_env);
1041 }
1042
1043 void emit_ia32_Conv_FP2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1044         emit_ia32_Conv(irn, emit_env);
1045 }
1046
1047 void emit_ia32_Conv_FP2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1048         emit_ia32_Conv(irn, emit_env);
1049 }
1050
1051
1052
1053 /*******************************************
1054  *  _                          _
1055  * | |                        | |
1056  * | |__   ___ _ __   ___   __| | ___  ___
1057  * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1058  * | |_) |  __/ | | | (_) | (_| |  __/\__ \
1059  * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1060  *
1061  *******************************************/
1062
1063 /**
1064  * Emits a backend call
1065  */
1066 void emit_be_Call(const ir_node *irn, ia32_emit_env_t *emit_env) {
1067         FILE *F = emit_env->out;
1068         entity *ent = be_Call_get_entity(irn);
1069         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1070
1071         if (ent) {
1072                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "call %s", get_entity_name(ent));
1073         }
1074         else {
1075                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
1076         }
1077
1078         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
1079
1080         IA32_DO_EMIT;
1081 }
1082
1083 /**
1084  * Emits code to increase stack pointer.
1085  */
1086 void emit_be_IncSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1087         FILE          *F    = emit_env->out;
1088         unsigned       offs = be_get_IncSP_offset(irn);
1089         be_stack_dir_t dir  = be_get_IncSP_direction(irn);
1090         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1091
1092         if (offs) {
1093                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S,%s%u", irn,
1094                         (dir == be_stack_dir_along) ? " -" : " ", offs);
1095                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (IncSP) */", irn);
1096         }
1097         else {
1098                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1099                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted %+F (IncSP) with 0 */", irn);
1100         }
1101
1102         IA32_DO_EMIT;
1103 }
1104
1105 /**
1106  * Emits code to set stack pointer.
1107  */
1108 void emit_be_SetSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1109         FILE *F = emit_env->out;
1110         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1111
1112         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1113         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (restore SP) */", irn);
1114         IA32_DO_EMIT;
1115 }
1116
1117 /**
1118  * Emits code for Copy.
1119  */
1120 void emit_be_Copy(const ir_node *irn, ia32_emit_env_t *emit_env) {
1121         FILE *F = emit_env->out;
1122         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1123
1124         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
1125         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1126         IA32_DO_EMIT;
1127 }
1128
1129 /**
1130  * Emits code for exchange.
1131  */
1132 void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
1133         FILE *F = emit_env->out;
1134         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1135
1136         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1137         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%1A, %2A) */", irn, irn, irn);
1138         IA32_DO_EMIT;
1139 }
1140
1141 /***********************************************************************************
1142  *                  _          __                                             _
1143  *                 (_)        / _|                                           | |
1144  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
1145  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1146  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
1147  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
1148  *
1149  ***********************************************************************************/
1150
1151 /**
1152  * Enters the emitter functions for handled nodes into the generic
1153  * pointer of an opcode.
1154  */
1155 static void ia32_register_emitters(void) {
1156
1157 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
1158 #define EMIT(a)      op_##a->ops.generic = (op_func)emit_##a
1159 #define BE_EMIT(a)   op_be_##a->ops.generic = (op_func)emit_be_##a
1160
1161         /* first clear the generic function pointer for all ops */
1162         clear_irp_opcodes_generic_func();
1163
1164         /* register all emitter functions defined in spec */
1165         ia32_register_spec_emitters();
1166
1167         /* other ia32 emitter functions */
1168         IA32_EMIT(CondJmp);
1169         IA32_EMIT(TestJmp);
1170         IA32_EMIT(SwitchJmp);
1171         IA32_EMIT(CopyB);
1172         IA32_EMIT(CopyB_i);
1173         IA32_EMIT(Conv_I2FP);
1174         IA32_EMIT(Conv_FP2I);
1175         IA32_EMIT(Conv_FP2FP);
1176
1177         /* benode emitter */
1178         BE_EMIT(Call);
1179         BE_EMIT(IncSP);
1180         BE_EMIT(SetSP);
1181         BE_EMIT(Copy);
1182         BE_EMIT(Perm);
1183
1184         /* firm emitter */
1185         EMIT(Jmp);
1186         EMIT(Proj);
1187
1188 #undef IA32_EMIT
1189 #undef BE_EMIT
1190 #undef EMIT
1191 }
1192
1193 /**
1194  * Emits code for a node.
1195  */
1196 static void ia32_emit_node(const ir_node *irn, void *env) {
1197         ia32_emit_env_t        *emit_env = env;
1198         firm_dbg_module_t *mod      = emit_env->mod;
1199         FILE              *F        = emit_env->out;
1200         ir_op             *op       = get_irn_op(irn);
1201
1202         DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
1203
1204         if (op->ops.generic) {
1205                 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
1206                 (*emit)(irn, env);
1207         }
1208         else {
1209                 ir_fprintf(F, "\t%35s /* %+F */\n", " ", irn);
1210         }
1211 }
1212
1213 /**
1214  * Walks over the nodes in a block connected by scheduling edges
1215  * and emits code for each node.
1216  */
1217 static void ia32_gen_block(ir_node *block, void *env) {
1218         const ir_node *irn;
1219
1220         if (! is_Block(block))
1221                 return;
1222
1223         fprintf(((ia32_emit_env_t *)env)->out, BLOCK_PREFIX("%ld:\n"), get_irn_node_nr(block));
1224         sched_foreach(block, irn) {
1225                 ia32_emit_node(irn, env);
1226         }
1227 }
1228
1229 /**
1230  * Emits code for function start.
1231  */
1232 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
1233         entity     *irg_ent  = get_irg_entity(irg);
1234         const char *irg_name = get_entity_name(irg_ent);
1235
1236         fprintf(F, "\t.text\n");
1237         if (get_entity_visibility(irg_ent) == visibility_external_visible) {
1238                 fprintf(F, ".globl %s\n", irg_name);
1239         }
1240         fprintf(F, "\t.type\t%s, @function\n", irg_name);
1241         fprintf(F, "%s:\n", irg_name);
1242 }
1243
1244 /**
1245  * Emits code for function end
1246  */
1247 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1248         const char *irg_name = get_entity_name(get_irg_entity(irg));
1249
1250         fprintf(F, "\tret\n");
1251         fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
1252 }
1253
1254 /**
1255  * Block-walker:
1256  * Sets labels for control flow nodes (jump target)
1257  * TODO: Jump optimization
1258  */
1259 static void ia32_gen_labels(ir_node *block, void *env) {
1260         ir_node *pred;
1261         int n = get_Block_n_cfgpreds(block);
1262
1263         for (n--; n >= 0; n--) {
1264                 pred = get_Block_cfgpred(block, n);
1265                 set_irn_link(pred, block);
1266         }
1267 }
1268
1269 typedef struct {
1270         ir_node *start;
1271         ir_node *end;
1272 } anchor;
1273
1274 /**
1275  * Ext-Block walker: create a block schedule
1276  */
1277 static void create_block_list(ir_extblk *blk, void *env) {
1278         anchor *list = env;
1279         int i, n;
1280
1281         for (i = 0, n = get_extbb_n_blocks(blk); i < n; ++i) {
1282                 ir_node *block = get_extbb_block(blk, i);
1283
1284                 set_irn_link(block, NULL);
1285                 if (list->start)
1286                         set_irn_link(list->end, block);
1287                 else
1288                         list->start = block;
1289
1290                 list->end = block;
1291         }
1292 }
1293
1294 /**
1295  * Main driver. Emits the code for one routine.
1296  */
1297 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1298         ia32_emit_env_t emit_env;
1299         anchor list;
1300         ir_node *block;
1301
1302         emit_env.mod      = firm_dbg_register("firm.be.ia32.emitter");
1303         emit_env.out      = F;
1304         emit_env.arch_env = cg->arch_env;
1305         emit_env.cg       = cg;
1306         emit_env.isa      = (ia32_isa_t *)cg->arch_env->isa;
1307
1308         /* set the global arch_env (needed by print hooks) */
1309         arch_env = cg->arch_env;
1310
1311         ia32_register_emitters();
1312
1313         ia32_emit_func_prolog(F, irg);
1314         irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1315
1316         if (cg->opt.extbb) {
1317                 /* schedule extended basic blocks */
1318
1319                 compute_extbb(irg);
1320
1321                 list.start = NULL;
1322                 list.end   = NULL;
1323                 irg_extblock_walk_graph(irg, NULL, create_block_list, &list);
1324
1325                 have_block_sched = 1;
1326                 for (block = list.start; block; block = get_irn_link(block))
1327                         ia32_gen_block(block, &emit_env);
1328         }
1329         else {
1330                 /* "normal" block schedule */
1331
1332                 have_block_sched = 0;
1333                 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1334         }
1335
1336         ia32_emit_func_epilog(F, irg);
1337 }