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