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