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