b06d8cade4b19af22af593f3ee68f367a3e7cc73
[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 test and jump.
789  */
790 static void TestJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
791
792 #define IA32_IS_IMMOP (is_ia32_ImmConst(irn) || is_ia32_ImmSymConst(irn))
793
794         FILE       *F   = env->out;
795         const char *op1 = arch_register_get_name(get_in_reg(irn, 0));
796         const char *op2 = IA32_IS_IMMOP ? get_ia32_cnst(irn) : NULL;
797         char        cmd_buf[SNPRINTF_BUF_LEN];
798         char        cmnt_buf[SNPRINTF_BUF_LEN];
799
800         if (! op2)
801                 op2 = arch_register_get_name(get_in_reg(irn, 1));
802
803         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "test %%%s,%s%s ", op1, IA32_IS_IMMOP ? " " : " %", op2);
804         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
805
806         IA32_DO_EMIT(irn);
807         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
808
809 #undef IA32_IS_IMMOP
810 }
811
812 /**
813  * Emits code for conditional test and jump with two variables.
814  */
815 static void emit_ia32_TestJmp(const ir_node *irn, ia32_emit_env_t *env) {
816         TestJmp_emitter(irn, env);
817 }
818
819 static void emit_ia32_CJmp(const ir_node *irn, ia32_emit_env_t *env) {
820         FILE *F = env->out;
821         char cmd_buf[SNPRINTF_BUF_LEN];
822         char cmnt_buf[SNPRINTF_BUF_LEN];
823
824         snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
825         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test */", irn);
826         IA32_DO_EMIT(irn);
827         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
828 }
829
830 static void emit_ia32_CJmpAM(const ir_node *irn, ia32_emit_env_t *env) {
831         FILE *F = env->out;
832         char cmd_buf[SNPRINTF_BUF_LEN];
833         char cmnt_buf[SNPRINTF_BUF_LEN];
834
835         snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
836         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test/cmp */", irn);
837         IA32_DO_EMIT(irn);
838         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
839 }
840
841 /*********************************************************
842  *                 _ _       _
843  *                (_) |     (_)
844  *   ___ _ __ ___  _| |_     _ _   _ _ __ ___  _ __  ___
845  *  / _ \ '_ ` _ \| | __|   | | | | | '_ ` _ \| '_ \/ __|
846  * |  __/ | | | | | | |_    | | |_| | | | | | | |_) \__ \
847  *  \___|_| |_| |_|_|\__|   | |\__,_|_| |_| |_| .__/|___/
848  *                         _/ |               | |
849  *                        |__/                |_|
850  *********************************************************/
851
852 /* jump table entry (target and corresponding number) */
853 typedef struct _branch_t {
854         ir_node *target;
855         int      value;
856 } branch_t;
857
858 /* jump table for switch generation */
859 typedef struct _jmp_tbl_t {
860         ir_node  *defProj;         /**< default target */
861         int       min_value;       /**< smallest switch case */
862         int       max_value;       /**< largest switch case */
863         int       num_branches;    /**< number of jumps */
864         char     *label;           /**< label of the jump table */
865         branch_t *branches;        /**< jump array */
866 } jmp_tbl_t;
867
868 /**
869  * Compare two variables of type branch_t. Used to sort all switch cases
870  */
871 static int ia32_cmp_branch_t(const void *a, const void *b) {
872         branch_t *b1 = (branch_t *)a;
873         branch_t *b2 = (branch_t *)b;
874
875         if (b1->value <= b2->value)
876                 return -1;
877         else
878                 return 1;
879 }
880
881 /**
882  * Emits code for a SwitchJmp (creates a jump table if
883  * possible otherwise a cmp-jmp cascade). Port from
884  * cggg ia32 backend
885  */
886 static void emit_ia32_SwitchJmp(const ir_node *irn, ia32_emit_env_t *emit_env) {
887         unsigned long       interval;
888         char                buf[SNPRINTF_BUF_LEN];
889         int                 last_value, i, pn, do_jmp_tbl = 1;
890         jmp_tbl_t           tbl;
891         ir_node            *proj;
892         const ir_edge_t    *edge;
893         const lc_arg_env_t *env = ia32_get_arg_env();
894         FILE               *F   = emit_env->out;
895         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
896
897         /* fill the table structure */
898         tbl.label        = xmalloc(SNPRINTF_BUF_LEN);
899         tbl.label        = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
900         tbl.defProj      = NULL;
901         tbl.num_branches = get_irn_n_edges(irn);
902         tbl.branches     = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
903         tbl.min_value    = INT_MAX;
904         tbl.max_value    = INT_MIN;
905
906         i = 0;
907         /* go over all proj's and collect them */
908         foreach_out_edge(irn, edge) {
909                 proj = get_edge_src_irn(edge);
910                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
911
912                 pn = get_Proj_proj(proj);
913
914                 /* create branch entry */
915                 tbl.branches[i].target = proj;
916                 tbl.branches[i].value  = pn;
917
918                 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
919                 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
920
921                 /* check for default proj */
922                 if (pn == get_ia32_pncode(irn)) {
923                         assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
924                         tbl.defProj = proj;
925                 }
926
927                 i++;
928         }
929
930         /* sort the branches by their number */
931         qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
932
933         /* two-complement's magic make this work without overflow */
934         interval = tbl.max_value - tbl.min_value;
935
936         /* check value interval */
937         if (interval > 16 * 1024) {
938                 do_jmp_tbl = 0;
939         }
940
941         /* check ratio of value interval to number of branches */
942         if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
943                 do_jmp_tbl = 0;
944         }
945
946         if (do_jmp_tbl) {
947                 /* emit the table */
948                 if (tbl.min_value != 0) {
949                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, -%d(%1S)",
950                                 interval, tbl.min_value, irn);
951                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* first switch value is not 0 */");
952
953                         IA32_DO_EMIT(irn);
954                 }
955                 else {
956                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, %1S", interval, irn);
957                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* compare for switch */");
958
959                         IA32_DO_EMIT(irn);
960                 }
961
962                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
963                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default jump if out of range  */");
964                 IA32_DO_EMIT(irn);
965
966                 if (tbl.num_branches > 1) {
967                         /* create table */
968
969                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp [%1S*4+%s]", irn, tbl.label);
970                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get jump table entry as target */");
971                         IA32_DO_EMIT(irn);
972
973                         fprintf(F, "\t.section\t.rodata\n");
974                         fprintf(F, "\t.align 4\n");
975
976                         fprintf(F, "%s:\n", tbl.label);
977
978                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
979                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */\n",  tbl.branches[0].value);
980                         IA32_DO_EMIT(irn);
981
982                         last_value = tbl.branches[0].value;
983                         for (i = 1; i < tbl.num_branches; ++i) {
984                                 while (++last_value < tbl.branches[i].value) {
985                                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
986                                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
987                                         IA32_DO_EMIT(irn);
988                                 }
989                                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf));
990                                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", last_value);
991                                 IA32_DO_EMIT(irn);
992                         }
993
994                         fprintf(F, "\t.text");
995                 }
996                 else {
997                         /* one jump is enough */
998                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
999                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* only one case given */");
1000                         IA32_DO_EMIT(irn);
1001                 }
1002         }
1003         else { // no jump table
1004                 for (i = 0; i < tbl.num_branches; ++i) {
1005                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %d, %1S", tbl.branches[i].value, irn);
1006                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", i);
1007                         IA32_DO_EMIT(irn);
1008                         fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
1009                 }
1010
1011                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.defProj, buf));
1012                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
1013                 IA32_DO_EMIT(irn);
1014         }
1015
1016         if (tbl.label)
1017                 free(tbl.label);
1018         if (tbl.branches)
1019                 free(tbl.branches);
1020 }
1021
1022 /**
1023  * Emits code for a unconditional jump.
1024  */
1025 static void emit_Jmp(const ir_node *irn, ia32_emit_env_t *env) {
1026         ir_node *block, *next_bl;
1027         FILE *F = env->out;
1028         char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1029
1030         /* for now, the code works for scheduled and non-schedules blocks */
1031         block = get_nodes_block(irn);
1032
1033         /* we have a block schedule */
1034         next_bl = next_blk_sched(block);
1035         if (get_cfop_target_block(irn) != next_bl) {
1036                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf));
1037                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F) */", irn, get_cfop_target_block(irn));
1038         }
1039         else {
1040                 cmd_buf[0] = '\0';
1041                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(irn, buf));
1042         }
1043         IA32_DO_EMIT(irn);
1044 }
1045
1046 /****************************
1047  *                  _
1048  *                 (_)
1049  *  _ __  _ __ ___  _  ___
1050  * | '_ \| '__/ _ \| |/ __|
1051  * | |_) | | | (_) | |\__ \
1052  * | .__/|_|  \___/| ||___/
1053  * | |            _/ |
1054  * |_|           |__/
1055  ****************************/
1056
1057 /**
1058  * Emits code for a proj -> node
1059  */
1060 static void emit_Proj(const ir_node *irn, ia32_emit_env_t *env) {
1061         ir_node *pred = get_Proj_pred(irn);
1062
1063         if (get_irn_op(pred) == op_Start) {
1064                 switch(get_Proj_proj(irn)) {
1065                         case pn_Start_X_initial_exec:
1066                                 emit_Jmp(irn, env);
1067                                 break;
1068                         default:
1069                                 break;
1070                 }
1071         }
1072 }
1073
1074 /**********************************
1075  *   _____                  ____
1076  *  / ____|                |  _ \
1077  * | |     ___  _ __  _   _| |_) |
1078  * | |    / _ \| '_ \| | | |  _ <
1079  * | |___| (_) | |_) | |_| | |_) |
1080  *  \_____\___/| .__/ \__, |____/
1081  *             | |     __/ |
1082  *             |_|    |___/
1083  **********************************/
1084
1085 /**
1086  * Emit movsb/w instructions to make mov count divideable by 4
1087  */
1088 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
1089         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1090
1091         fprintf(F, "\t/* memcopy %d bytes*/\n", size);
1092
1093         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
1094         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward*/");
1095         IA32_DO_EMIT(NULL);
1096
1097         switch(rem) {
1098                 case 1:
1099                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1100                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 1 */");
1101                         break;
1102                 case 2:
1103                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1104                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 2 */");
1105                         break;
1106                 case 3:
1107                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1108                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1109                         IA32_DO_EMIT(NULL);
1110                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1111                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1112                         break;
1113         }
1114
1115         IA32_DO_EMIT(NULL);
1116 }
1117
1118 /**
1119  * Emit rep movsd instruction for memcopy.
1120  */
1121 static void emit_ia32_CopyB(const ir_node *irn, ia32_emit_env_t *emit_env) {
1122         FILE   *F    = emit_env->out;
1123         tarval *tv   = get_ia32_Immop_tarval(irn);
1124         int     rem  = get_tarval_long(tv);
1125         int     size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
1126         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1127
1128         emit_CopyB_prolog(F, rem, size);
1129
1130         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "rep movsd");
1131         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy */");
1132         IA32_DO_EMIT(irn);
1133 }
1134
1135 /**
1136  * Emits unrolled memcopy.
1137  */
1138 static void emit_ia32_CopyB_i(const ir_node *irn, ia32_emit_env_t *emit_env) {
1139         tarval *tv   = get_ia32_Immop_tarval(irn);
1140         int     size = get_tarval_long(tv);
1141         FILE   *F    = emit_env->out;
1142         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1143
1144         emit_CopyB_prolog(F, size & 0x3, size);
1145
1146         size >>= 2;
1147         while (size--) {
1148                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsd");
1149                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy unrolled */");
1150                 IA32_DO_EMIT(irn);
1151         }
1152 }
1153
1154
1155
1156 /***************************
1157  *   _____
1158  *  / ____|
1159  * | |     ___  _ ____   __
1160  * | |    / _ \| '_ \ \ / /
1161  * | |___| (_) | | | \ V /
1162  *  \_____\___/|_| |_|\_/
1163  *
1164  ***************************/
1165
1166 /**
1167  * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1168  */
1169 static void emit_ia32_Conv_with_FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1170         FILE               *F    = emit_env->out;
1171         const lc_arg_env_t *env  = ia32_get_arg_env();
1172         char               *from, *to, buf[64];
1173         ir_mode *src_mode, *tgt_mode;
1174         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1175
1176         src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
1177         tgt_mode = get_ia32_res_mode(irn);
1178
1179         from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
1180         to   = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
1181
1182         switch(get_ia32_op_type(irn)) {
1183                 case ia32_Normal:
1184                         lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
1185                         break;
1186                 case ia32_AddrModeS:
1187                         lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn, emit_env));
1188                         break;
1189                 default:
1190                         assert(0 && "unsupported op type for Conv");
1191         }
1192
1193         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cvt%s2%s %s", from, to, buf);
1194         lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F, %+F) */", irn, src_mode, tgt_mode);
1195         IA32_DO_EMIT(irn);
1196 }
1197
1198 static void emit_ia32_Conv_I2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1199         emit_ia32_Conv_with_FP(irn, emit_env);
1200 }
1201
1202 static void emit_ia32_Conv_FP2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1203         emit_ia32_Conv_with_FP(irn, emit_env);
1204 }
1205
1206 static void emit_ia32_Conv_FP2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1207         emit_ia32_Conv_with_FP(irn, emit_env);
1208 }
1209
1210 /**
1211  * Emits code for an Int conversion.
1212  */
1213 static void emit_ia32_Conv_I2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1214         FILE               *F    = emit_env->out;
1215         const lc_arg_env_t *env  = ia32_get_arg_env();
1216         char *move_cmd, *conv_cmd;
1217         ir_mode *src_mode, *tgt_mode;
1218         int n, m;
1219         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1220         const arch_register_t *in_reg, *out_reg;
1221
1222         src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
1223         tgt_mode = get_ia32_res_mode(irn);
1224
1225         n = get_mode_size_bits(src_mode);
1226         m = get_mode_size_bits(tgt_mode);
1227
1228         if (mode_is_signed(n < m ? src_mode : tgt_mode)) {
1229                 move_cmd = "movsx";
1230                 if (n == 8 || m == 8)
1231                         conv_cmd = "cbw";
1232                 else if (n == 16 || m == 16)
1233                         conv_cmd = "cwde";
1234                 else
1235                         assert(0 && "unsupported Conv_I2I");
1236         }
1237         else {
1238                 move_cmd = "movzx";
1239                 conv_cmd = NULL;
1240         }
1241
1242         switch(get_ia32_op_type(irn)) {
1243                 case ia32_Normal:
1244                         in_reg  = get_in_reg(irn, 2);
1245                         out_reg = get_out_reg(irn, 0);
1246
1247                         if (REGS_ARE_EQUAL(in_reg, &ia32_gp_regs[REG_EAX]) &&
1248                                 REGS_ARE_EQUAL(out_reg, in_reg)                &&
1249                                 mode_is_signed(n < m ? src_mode : tgt_mode))
1250                         {
1251                                 /* argument and result are both in EAX and */
1252                                 /* signedness is ok: -> use converts       */
1253                                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s", conv_cmd);
1254                         }
1255                         else if (REGS_ARE_EQUAL(out_reg, in_reg) &&
1256                                 ! mode_is_signed(n < m ? src_mode : tgt_mode))
1257                         {
1258                                 /* argument and result are in the same register */
1259                                 /* and signedness is ok: -> use and with mask   */
1260                                 int mask = (1 << (n < m ? n : m)) - 1;
1261                                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "and %1D, 0x%x", irn, mask);
1262                         }
1263                         else {
1264                                 /* use move w/o sign extension */
1265                                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %%%s",
1266                                         move_cmd, irn, ia32_get_reg_name_for_mode(emit_env, n < m ? src_mode : tgt_mode, in_reg));
1267                         }
1268
1269                         break;
1270                 case ia32_AddrModeS:
1271                         lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %s",
1272                                 move_cmd, irn, ia32_emit_am(irn, emit_env));
1273                         break;
1274                 default:
1275                         assert(0 && "unsupported op type for Conv");
1276         }
1277
1278         lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%d Bit mode_%F -> %d Bit mode_%F) */",
1279                 irn, n, src_mode, m, tgt_mode);
1280
1281         IA32_DO_EMIT(irn);
1282 }
1283
1284 /**
1285  * Emits code for an 8Bit Int conversion.
1286  */
1287 void emit_ia32_Conv_I2I8Bit(const ir_node *irn, ia32_emit_env_t *emit_env) {
1288         emit_ia32_Conv_I2I(irn, emit_env);
1289 }
1290
1291
1292 /*******************************************
1293  *  _                          _
1294  * | |                        | |
1295  * | |__   ___ _ __   ___   __| | ___  ___
1296  * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1297  * | |_) |  __/ | | | (_) | (_| |  __/\__ \
1298  * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1299  *
1300  *******************************************/
1301
1302 /**
1303  * Emits a backend call
1304  */
1305 static void emit_be_Call(const ir_node *irn, ia32_emit_env_t *emit_env) {
1306         FILE *F = emit_env->out;
1307         entity *ent = be_Call_get_entity(irn);
1308         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1309
1310         if (ent) {
1311                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "call %s", get_entity_name(ent));
1312         }
1313         else {
1314                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
1315         }
1316
1317         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
1318
1319         IA32_DO_EMIT(irn);
1320 }
1321
1322 /**
1323  * Emits code to increase stack pointer.
1324  */
1325 static void emit_be_IncSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1326         FILE          *F    = emit_env->out;
1327         unsigned       offs = be_get_IncSP_offset(irn);
1328         be_stack_dir_t dir  = be_get_IncSP_direction(irn);
1329         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1330
1331         if (offs) {
1332                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S,%s%u", irn,
1333                         (dir == be_stack_dir_expand) ? " -" : " ", offs);
1334                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (IncSP) */", irn);
1335         }
1336         else {
1337                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1338                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted %+F (IncSP) with 0 */", irn);
1339         }
1340
1341         IA32_DO_EMIT(irn);
1342 }
1343
1344 /**
1345  * Emits code to set stack pointer.
1346  */
1347 static void emit_be_SetSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1348         FILE *F = emit_env->out;
1349         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1350
1351         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1352         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (restore SP) */", irn);
1353         IA32_DO_EMIT(irn);
1354 }
1355
1356 /**
1357  * Emits code for Copy.
1358  */
1359 static void emit_be_Copy(const ir_node *irn, ia32_emit_env_t *emit_env) {
1360         FILE *F = emit_env->out;
1361         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1362
1363         if (mode_is_float(get_irn_mode(irn)))
1364                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "movs%M %1D, %1S", irn, irn, irn);
1365         else
1366                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
1367         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1368         IA32_DO_EMIT(irn);
1369 }
1370
1371 /**
1372  * Emits code for exchange.
1373  */
1374 static void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
1375         FILE *F = emit_env->out;
1376         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1377
1378         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1379         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%1A, %2A) */", irn, irn, irn);
1380         IA32_DO_EMIT(irn);
1381 }
1382
1383
1384
1385 /***********************************************************************************
1386  *                  _          __                                             _
1387  *                 (_)        / _|                                           | |
1388  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
1389  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1390  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
1391  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
1392  *
1393  ***********************************************************************************/
1394
1395 /**
1396  * Enters the emitter functions for handled nodes into the generic
1397  * pointer of an opcode.
1398  */
1399 static void ia32_register_emitters(void) {
1400
1401 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
1402 #define EMIT(a)      op_##a->ops.generic = (op_func)emit_##a
1403 #define BE_EMIT(a)   op_be_##a->ops.generic = (op_func)emit_be_##a
1404
1405         /* first clear the generic function pointer for all ops */
1406         clear_irp_opcodes_generic_func();
1407
1408         /* register all emitter functions defined in spec */
1409         ia32_register_spec_emitters();
1410
1411         /* other ia32 emitter functions */
1412         IA32_EMIT(CondJmp);
1413         IA32_EMIT(TestJmp);
1414         IA32_EMIT(CJmp);
1415         IA32_EMIT(CJmpAM);
1416         IA32_EMIT(SwitchJmp);
1417         IA32_EMIT(CopyB);
1418         IA32_EMIT(CopyB_i);
1419         IA32_EMIT(Conv_I2FP);
1420         IA32_EMIT(Conv_FP2I);
1421         IA32_EMIT(Conv_FP2FP);
1422         IA32_EMIT(Conv_I2I);
1423         IA32_EMIT(Conv_I2I8Bit);
1424
1425         /* benode emitter */
1426         BE_EMIT(Call);
1427         BE_EMIT(IncSP);
1428         BE_EMIT(SetSP);
1429         BE_EMIT(Copy);
1430         BE_EMIT(Perm);
1431
1432         /* firm emitter */
1433         EMIT(Jmp);
1434         EMIT(Proj);
1435
1436 #undef IA32_EMIT
1437 #undef BE_EMIT
1438 #undef EMIT
1439 }
1440
1441 /**
1442  * Emits code for a node.
1443  */
1444 static void ia32_emit_node(const ir_node *irn, void *env) {
1445         ia32_emit_env_t        *emit_env = env;
1446         firm_dbg_module_t *mod      = emit_env->mod;
1447         FILE              *F        = emit_env->out;
1448         ir_op             *op       = get_irn_op(irn);
1449
1450         DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
1451
1452         if (op->ops.generic) {
1453                 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
1454                 (*emit)(irn, env);
1455         }
1456         else {
1457                 ir_fprintf(F, "\t%35s /* %+F (%+G) */\n", " ", irn, irn);
1458         }
1459 }
1460
1461 /**
1462  * Walks over the nodes in a block connected by scheduling edges
1463  * and emits code for each node.
1464  */
1465 static void ia32_gen_block(ir_node *block, void *env) {
1466         const ir_node *irn;
1467
1468         if (! is_Block(block))
1469                 return;
1470
1471         fprintf(((ia32_emit_env_t *)env)->out, BLOCK_PREFIX("%ld:\n"), get_irn_node_nr(block));
1472         sched_foreach(block, irn) {
1473                 ia32_emit_node(irn, env);
1474         }
1475 }
1476
1477 /**
1478  * Emits code for function start.
1479  */
1480 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
1481         entity     *irg_ent  = get_irg_entity(irg);
1482         const char *irg_name = get_entity_name(irg_ent);
1483
1484         fprintf(F, "\t.section\t.text\n");
1485         if (get_entity_visibility(irg_ent) == visibility_external_visible) {
1486                 fprintf(F, ".globl %s\n", irg_name);
1487         }
1488         fprintf(F, "\t.type\t%s, @function\n", irg_name);
1489         fprintf(F, "%s:\n", irg_name);
1490 }
1491
1492 /**
1493  * Emits code for function end
1494  */
1495 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1496         const char *irg_name = get_entity_name(get_irg_entity(irg));
1497
1498         fprintf(F, "\tret\n");
1499         fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
1500 }
1501
1502 /**
1503  * Block-walker:
1504  * Sets labels for control flow nodes (jump target)
1505  * TODO: Jump optimization
1506  */
1507 static void ia32_gen_labels(ir_node *block, void *env) {
1508         ir_node *pred;
1509         int n = get_Block_n_cfgpreds(block);
1510
1511         for (n--; n >= 0; n--) {
1512                 pred = get_Block_cfgpred(block, n);
1513                 set_irn_link(pred, block);
1514         }
1515 }
1516
1517 /**
1518  * Main driver. Emits the code for one routine.
1519  */
1520 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1521         ia32_emit_env_t emit_env;
1522         ir_node *block;
1523
1524         emit_env.out      = F;
1525         emit_env.arch_env = cg->arch_env;
1526         emit_env.cg       = cg;
1527         emit_env.isa      = (ia32_isa_t *)cg->arch_env->isa;
1528         FIRM_DBG_REGISTER(emit_env.mod, "firm.be.ia32.emitter");
1529
1530         /* set the global arch_env (needed by print hooks) */
1531         arch_env = cg->arch_env;
1532
1533         ia32_register_emitters();
1534
1535         ia32_emit_func_prolog(F, irg);
1536         irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1537
1538         if (cg->opt.extbb && cg->blk_sched) {
1539                 int i, n = ARR_LEN(cg->blk_sched);
1540
1541                 for (i = 0; i < n;) {
1542                         ir_node *next_bl;
1543
1544                         block   = cg->blk_sched[i];
1545                         ++i;
1546                         next_bl = i < n ? cg->blk_sched[i] : NULL;
1547
1548                         /* set here the link. the emitter expects to find the next block here */
1549                         set_irn_link(block, next_bl);
1550                         ia32_gen_block(block, &emit_env);
1551                 }
1552         }
1553         else {
1554                 /* "normal" block schedule: Note the get_next_block() returns the NUMBER of the block
1555                    in the block schedule. As this number should NEVER be equal the next block,
1556                    we does not need a clear block link here. */
1557                 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1558         }
1559
1560         ia32_emit_func_epilog(F, irg);
1561 }