changed SwitchJmp emitter (but still broken in some cases)
[libfirm] / ir / be / ia32 / ia32_emitter.c
1 /**
2  * This file implements the node emitter.
3  *
4  * $Id$
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <limits.h>
12
13 #include "xmalloc.h"
14 #include "tv.h"
15 #include "iredges.h"
16 #include "debug.h"
17 #include "irgwalk.h"
18 #include "irprintf.h"
19 #include "irop_t.h"
20 #include "irargs_t.h"
21 #include "irprog_t.h"
22 #include "iredges_t.h"
23
24 #include "../besched_t.h"
25 #include "../benode_t.h"
26
27 #include "ia32_emitter.h"
28 #include "gen_ia32_emitter.h"
29 #include "gen_ia32_regalloc_if.h"
30 #include "ia32_nodes_attr.h"
31 #include "ia32_new_nodes.h"
32 #include "ia32_map_regs.h"
33
34 #ifdef obstack_chunk_alloc
35 # undef obstack_chunk_alloc
36 # define obstack_chunk_alloc xmalloc
37 #else
38 # define obstack_chunk_alloc xmalloc
39 # define obstack_chunk_free free
40 #endif
41
42 #define BLOCK_PREFIX(x) ".L" x
43
44 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
45
46 #define SNPRINTF_BUF_LEN 128
47
48 /* global arch_env for lc_printf functions */
49 static const arch_env_t *arch_env = NULL;
50
51 /*************************************************************
52  *             _       _    __   _          _
53  *            (_)     | |  / _| | |        | |
54  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
55  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
56  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
57  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
58  * | |                                       | |
59  * |_|                                       |_|
60  *************************************************************/
61
62 /**
63  * returns true if a node has x87 registers
64  */
65 static int has_x87_register(const ir_node *n)
66 {
67         return get_irn_op(n)->flags & (irop_flag_machine << 1);
68 }
69
70 /* We always pass the ir_node which is a pointer. */
71 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
72         return lc_arg_type_ptr;
73 }
74
75
76 /**
77  * Returns the register at in position pos.
78  */
79 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
80         ir_node                *op;
81         const arch_register_t  *reg = NULL;
82
83         assert(get_irn_arity(irn) > pos && "Invalid IN position");
84
85         /* The out register of the operator at position pos is the
86            in register we need. */
87         op = get_irn_n(irn, pos);
88
89         reg = arch_get_irn_register(arch_env, op);
90
91         assert(reg && "no in register found");
92         return reg;
93 }
94
95 /**
96  * Returns the register at out position pos.
97  */
98 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
99         ir_node                *proj;
100         const arch_register_t  *reg = NULL;
101
102         /* 1st case: irn is not of mode_T, so it has only                 */
103         /*           one OUT register -> good                             */
104         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
105         /*           Proj with the corresponding projnum for the register */
106
107         if (get_irn_mode(irn) != mode_T) {
108                 reg = arch_get_irn_register(arch_env, irn);
109         }
110         else if (is_ia32_irn(irn)) {
111                 reg = get_ia32_out_reg(irn, pos);
112         }
113         else {
114                 const ir_edge_t *edge;
115
116                 foreach_out_edge(irn, edge) {
117                         proj = get_edge_src_irn(edge);
118                         assert(is_Proj(proj) && "non-Proj from mode_T node");
119                         if (get_Proj_proj(proj) == pos) {
120                                 reg = arch_get_irn_register(arch_env, proj);
121                                 break;
122                         }
123                 }
124         }
125
126         assert(reg && "no out register found");
127         return reg;
128 }
129
130 enum io_direction {
131   IN_REG,
132   OUT_REG
133 };
134
135 /**
136  * Returns the name of the in register at position pos.
137  */
138 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
139         const arch_register_t *reg;
140
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                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%4S, %s", n, ia32_emit_am(n, env));
341                         }
342                         break;
343                 case ia32_AddrModeD:
344                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
345                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s,%s%s",
346                                         ia32_emit_am(n, env),
347                                         is_ia32_ImmSymConst(n) ? " OFFSET FLAT:" : " ",  /* In case of a symconst we must add OFFSET to */
348                                         get_ia32_cnst(n));                               /* tell the assembler to store it's address.   */
349                         }
350                         else {
351                                 const arch_register_t *in1 = get_in_reg(n, 2);
352                                 ir_mode              *mode = get_ia32_res_mode(n);
353                                 const char           *in_name;
354
355                                 mode    = mode ? mode : get_ia32_ls_mode(n);
356                                 in_name = ia32_get_reg_name_for_mode(env, mode, in1);
357
358                                 if (is_ia32_emit_cl(n)) {
359                                         assert(REGS_ARE_EQUAL(&ia32_gp_regs[REG_ECX], in1) && "shift operation needs ecx");
360                                         in_name = "cl";
361                                 }
362
363                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %%%s", ia32_emit_am(n, env), in_name);
364                         }
365                         break;
366                 default:
367                         assert(0 && "unsupported op type");
368         }
369
370 #undef PRODUCES_RESULT
371
372         return buf;
373 }
374
375 /**
376  * Emits registers and/or address mode of a binary operation.
377  */
378 char *ia32_emit_x87_binop(const ir_node *n, ia32_emit_env_t *env) {
379         static char *buf = NULL;
380
381         /* verify that this function is never called on non-AM supporting operations */
382         //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
383
384         if (! buf) {
385                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
386         }
387         else {
388                 memset(buf, 0, SNPRINTF_BUF_LEN);
389         }
390
391         switch(get_ia32_op_type(n)) {
392                 case ia32_Normal:
393                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
394                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
395                         }
396                         else {
397                                 ia32_attr_t *attr = get_ia32_attr(n);
398                                 const arch_register_t *in1 = attr->x87[0];
399                                 const arch_register_t *in2 = attr->x87[1];
400                                 const arch_register_t *out = attr->x87[2];
401                                 const arch_register_t *in;
402                                 const char            *in_name;
403
404                                 in      = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
405                                 out     = out ? out : in1;
406                                 in_name = arch_register_get_name(in);
407
408                                 snprintf(buf, SNPRINTF_BUF_LEN, "%%%s, %%%s", arch_register_get_name(out), in_name);
409                         }
410                         break;
411                 case ia32_AddrModeS:
412                 case ia32_AddrModeD:
413                         lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s", ia32_emit_am(n, env));
414                         break;
415                 default:
416                         assert(0 && "unsupported op type");
417         }
418
419 #undef PRODUCES_RESULT
420
421         return buf;
422 }
423
424 /**
425  * Emits registers and/or address mode of a unary operation.
426  */
427 char *ia32_emit_unop(const ir_node *n, ia32_emit_env_t *env) {
428         static char *buf = NULL;
429
430         if (! buf) {
431                 buf = xcalloc(1, SNPRINTF_BUF_LEN);
432         }
433         else {
434                 memset(buf, 0, SNPRINTF_BUF_LEN);
435         }
436
437         switch(get_ia32_op_type(n)) {
438                 case ia32_Normal:
439                         if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
440                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%C", n);
441                         }
442                         else {
443                                 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
444                         }
445                         break;
446                 case ia32_am_Dest:
447                         snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n, env));
448                         break;
449                 default:
450                         assert(0 && "unsupported op type");
451         }
452
453         return buf;
454 }
455
456 /**
457  * Emits address mode.
458  */
459 char *ia32_emit_am(const ir_node *n, ia32_emit_env_t *env) {
460         ia32_am_flavour_t am_flav    = get_ia32_am_flavour(n);
461         int               had_output = 0;
462         char             *s;
463         int               size;
464         static struct obstack *obst  = NULL;
465         ir_mode *mode = get_ia32_ls_mode(n);
466
467         if (! is_ia32_Lea(n))
468                 assert(mode && "AM node must have ls_mode attribute set.");
469
470         if (! obst) {
471                 obst = xcalloc(1, sizeof(*obst));
472         }
473         else {
474                 obstack_free(obst, NULL);
475         }
476
477         /* obstack_free with NULL results in an uninitialized obstack */
478         obstack_init(obst);
479
480         if (mode) {
481                 switch (get_mode_size_bits(mode)) {
482                         case 8:
483                                 obstack_printf(obst, "BYTE PTR ");
484                                 break;
485                         case 16:
486                                 obstack_printf(obst, "WORD PTR ");
487                                 break;
488                         case 32:
489                                 obstack_printf(obst, "DWORD PTR ");
490                                 break;
491                         case 64:
492                                 if (has_x87_register(n))
493                                         /* ARGHHH: stupid gas x87 wants QWORD PTR but SSE must be WITHOUT */
494                                         obstack_printf(obst, "QWORD PTR ");
495                                 break;
496                         case 80:
497                         case 96:
498                                 obstack_printf(obst, "XWORD PTR ");
499                                 break;
500                         default:
501                                 break;
502                 }
503         }
504
505         /* emit address mode symconst */
506         if (get_ia32_am_sc(n)) {
507                 if (is_ia32_am_sc_sign(n))
508                         obstack_printf(obst, "-");
509                 obstack_printf(obst, "%s", get_id_str(get_ia32_am_sc(n)));
510         }
511
512         if (am_flav & ia32_B) {
513                 obstack_printf(obst, "[");
514                 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
515                 had_output = 1;
516         }
517
518         if (am_flav & ia32_I) {
519                 if (had_output) {
520                         obstack_printf(obst, "+");
521                 }
522                 else {
523                         obstack_printf(obst, "[");
524                 }
525
526                 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
527
528                 if (am_flav & ia32_S) {
529                         obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
530                 }
531
532                 had_output = 1;
533         }
534
535         if (am_flav & ia32_O) {
536                 s = get_ia32_am_offs(n);
537
538                 if (s) {
539                         /* omit explicit + if there was no base or index */
540                         if (! had_output) {
541                                 obstack_printf(obst, "[");
542                                 if (s[0] == '+')
543                                         s++;
544                         }
545
546                         obstack_printf(obst, s);
547                         had_output = 1;
548                 }
549         }
550
551         if (had_output)
552                 obstack_printf(obst, "] ");
553
554         size        = obstack_object_size(obst);
555         s           = obstack_finish(obst);
556         s[size - 1] = '\0';
557
558         return s;
559 }
560
561
562
563 /**
564  * Formated print of commands and comments.
565  */
566 static void ia32_fprintf_format(FILE *F, const ir_node *irn, char *cmd_buf, char *cmnt_buf) {
567         unsigned lineno;
568         const char *name = irn ? be_retrieve_dbg_info(get_irn_dbg_info((ir_node *)irn), &lineno) : NULL;
569
570         if (name)
571                 fprintf(F, "\t%-35s %-60s /* %s:%u */\n", cmd_buf, cmnt_buf, name, lineno);
572         else
573                 fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
574 }
575
576
577
578 /**
579  * Add a number to a prefix. This number will not be used a second time.
580  */
581 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
582         static unsigned long id = 0;
583         snprintf(buf, buflen, "%s%lu", prefix, ++id);
584         return buf;
585 }
586
587
588
589 /*************************************************
590  *                 _ _                         _
591  *                (_) |                       | |
592  *   ___ _ __ ___  _| |_    ___ ___  _ __   __| |
593  *  / _ \ '_ ` _ \| | __|  / __/ _ \| '_ \ / _` |
594  * |  __/ | | | | | | |_  | (_| (_) | | | | (_| |
595  *  \___|_| |_| |_|_|\__|  \___\___/|_| |_|\__,_|
596  *
597  *************************************************/
598
599 #undef IA32_DO_EMIT
600 #define IA32_DO_EMIT(irn) ia32_fprintf_format(F, irn, cmd_buf, cmnt_buf)
601
602 /*
603  * coding of conditions
604  */
605 struct cmp2conditon_t {
606         const char *name;
607         pn_Cmp      num;
608 };
609
610 /*
611  * positive conditions for signed compares
612  */
613 static const struct cmp2conditon_t cmp2condition_s[] = {
614   { NULL,              pn_Cmp_False },  /* always false */
615   { "e",               pn_Cmp_Eq },     /* == */
616   { "l",               pn_Cmp_Lt },     /* < */
617   { "le",              pn_Cmp_Le },     /* <= */
618   { "g",               pn_Cmp_Gt },     /* > */
619   { "ge",              pn_Cmp_Ge },     /* >= */
620   { "ne",              pn_Cmp_Lg },     /* != */
621   { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
622   { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
623   { "unordered or ==", pn_Cmp_Ue },     /* Floating point: unordered or == */
624   { "unordered or <",  pn_Cmp_Ul },     /* Floating point: unordered or < */
625   { "unordered or <=", pn_Cmp_Ule },    /* Floating point: unordered or <= */
626   { "unordered or >",  pn_Cmp_Ug },     /* Floating point: unordered or > */
627   { "unordered or >=", pn_Cmp_Uge },    /* Floating point: unordered or >= */
628   { "unordered or !=", pn_Cmp_Ne },     /* Floating point: unordered or != */
629   { NULL,              pn_Cmp_True },   /* always true */
630 };
631
632 /*
633  * positive conditions for unsigned compares
634  */
635 static const struct cmp2conditon_t cmp2condition_u[] = {
636         { NULL,              pn_Cmp_False },  /* always false */
637         { "e",               pn_Cmp_Eq },     /* == */
638         { "b",               pn_Cmp_Lt },     /* < */
639         { "be",              pn_Cmp_Le },     /* <= */
640         { "a",               pn_Cmp_Gt },     /* > */
641         { "ae",              pn_Cmp_Ge },     /* >= */
642         { "ne",              pn_Cmp_Lg },     /* != */
643         { "ordered",         pn_Cmp_Leg },    /* Floating point: ordered */
644         { "unordered",       pn_Cmp_Uo },     /* FLoting point: unordered */
645         { "unordered or ==", pn_Cmp_Ue },     /* Floating point: unordered or == */
646         { "unordered or <",  pn_Cmp_Ul },     /* Floating point: unordered or < */
647         { "unordered or <=", pn_Cmp_Ule },    /* Floating point: unordered or <= */
648         { "unordered or >",  pn_Cmp_Ug },     /* Floating point: unordered or > */
649         { "unordered or >=", pn_Cmp_Uge },    /* Floating point: unordered or >= */
650         { "unordered or !=", pn_Cmp_Ne },     /* Floating point: unordered or != */
651         { NULL,              pn_Cmp_True },   /* always true */
652 };
653
654 /*
655  * returns the condition code
656  */
657 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
658 {
659         assert(cmp2condition_s[cmp_code].num == cmp_code);
660         assert(cmp2condition_u[cmp_code].num == cmp_code);
661
662         return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
663 }
664
665 /**
666  * Returns the target block for a control flow node.
667  */
668 static ir_node *get_cfop_target_block(const ir_node *irn) {
669         return get_irn_link(irn);
670 }
671
672 /**
673  * Returns the target label for a control flow node.
674  */
675 static char *get_cfop_target(const ir_node *irn, char *buf) {
676         ir_node *bl = get_cfop_target_block(irn);
677
678         snprintf(buf, SNPRINTF_BUF_LEN, BLOCK_PREFIX("%ld"), get_irn_node_nr(bl));
679         return buf;
680 }
681
682 /** Return the next block in Block schedule */
683 static ir_node *next_blk_sched(const ir_node *block) {
684         return get_irn_link(block);
685 }
686
687 /**
688  * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
689  */
690 static void finish_CondJmp(FILE *F, const ir_node *irn, ir_mode *mode) {
691         const ir_node   *proj1, *proj2 = NULL;
692         const ir_node   *block, *next_bl = NULL;
693         const ir_edge_t *edge;
694         char buf[SNPRINTF_BUF_LEN];
695         char cmd_buf[SNPRINTF_BUF_LEN];
696         char cmnt_buf[SNPRINTF_BUF_LEN];
697
698         /* get both Proj's */
699         edge = get_irn_out_edge_first(irn);
700         proj1 = get_edge_src_irn(edge);
701         assert(is_Proj(proj1) && "CondJmp with a non-Proj");
702
703         edge = get_irn_out_edge_next(irn, edge);
704         if (edge) {
705                 proj2 = get_edge_src_irn(edge);
706                 assert(is_Proj(proj2) && "CondJmp with a non-Proj");
707         }
708
709         /* for now, the code works for scheduled and non-schedules blocks */
710         block = get_nodes_block(irn);
711         if (proj2) {
712                 /* we have a block schedule */
713                 next_bl = next_blk_sched(block);
714
715                 if (get_cfop_target_block(proj1) == next_bl) {
716                         /* exchange both proj's so the second one can be omitted */
717                         const ir_node *t = proj1;
718                         proj1 = proj2;
719                         proj2 = t;
720                 }
721         }
722
723         /* the first Proj must always be created */
724         if (get_Proj_proj(proj1) == pn_Cond_true) {
725                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
726                                         get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
727                                         get_cfop_target(proj1, buf));
728                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == TRUE */");
729         }
730         else  {
731                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
732                                         get_cmp_suffix(get_negated_pnc(get_ia32_pncode(irn), mode),
733                                         !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
734                                         get_cfop_target(proj1, buf));
735                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == FALSE */");
736         }
737         IA32_DO_EMIT(irn);
738
739         /* the second Proj might be a fallthrough */
740         if (proj2) {
741                 if (get_cfop_target_block(proj2) != next_bl) {
742                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj2, buf));
743                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* otherwise */");
744                 }
745                 else {
746                         cmd_buf[0] = '\0';
747                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrogh %s */", get_cfop_target(proj2, buf));
748                 }
749                 IA32_DO_EMIT(irn);
750         }
751 }
752
753 /**
754  * Emits code for conditional jump.
755  */
756 static void CondJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
757         FILE *F = env->out;
758         char cmd_buf[SNPRINTF_BUF_LEN];
759         char cmnt_buf[SNPRINTF_BUF_LEN];
760
761         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
762         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
763         IA32_DO_EMIT(irn);
764         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
765 }
766
767 /**
768  * Emits code for conditional jump with two variables.
769  */
770 static void emit_ia32_CondJmp(const ir_node *irn, ia32_emit_env_t *env) {
771         CondJmp_emitter(irn, env);
772 }
773
774 /**
775  * Emits code for conditional test and jump.
776  */
777 static void TestJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
778
779 #define IA32_IS_IMMOP (is_ia32_ImmConst(irn) || is_ia32_ImmSymConst(irn))
780
781         FILE       *F   = env->out;
782         const char *op1 = arch_register_get_name(get_in_reg(irn, 0));
783         const char *op2 = IA32_IS_IMMOP ? get_ia32_cnst(irn) : NULL;
784         char        cmd_buf[SNPRINTF_BUF_LEN];
785         char        cmnt_buf[SNPRINTF_BUF_LEN];
786
787         if (! op2)
788                 op2 = arch_register_get_name(get_in_reg(irn, 1));
789
790         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "test %%%s,%s%s ", op1, IA32_IS_IMMOP ? " " : " %", op2);
791         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
792
793         IA32_DO_EMIT(irn);
794         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
795
796 #undef IA32_IS_IMMOP
797 }
798
799 /**
800  * Emits code for conditional test and jump with two variables.
801  */
802 static void emit_ia32_TestJmp(const ir_node *irn, ia32_emit_env_t *env) {
803         TestJmp_emitter(irn, env);
804 }
805
806 static void emit_ia32_CJmp(const ir_node *irn, ia32_emit_env_t *env) {
807         FILE *F = env->out;
808         char cmd_buf[SNPRINTF_BUF_LEN];
809         char cmnt_buf[SNPRINTF_BUF_LEN];
810
811         snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
812         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test */", irn);
813         IA32_DO_EMIT(irn);
814         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
815 }
816
817 static void emit_ia32_CJmpAM(const ir_node *irn, ia32_emit_env_t *env) {
818         FILE *F = env->out;
819         char cmd_buf[SNPRINTF_BUF_LEN];
820         char cmnt_buf[SNPRINTF_BUF_LEN];
821
822         snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
823         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test/cmp */", irn);
824         IA32_DO_EMIT(irn);
825         finish_CondJmp(F, irn, get_ia32_res_mode(irn));
826 }
827
828 /*********************************************************
829  *                 _ _       _
830  *                (_) |     (_)
831  *   ___ _ __ ___  _| |_     _ _   _ _ __ ___  _ __  ___
832  *  / _ \ '_ ` _ \| | __|   | | | | | '_ ` _ \| '_ \/ __|
833  * |  __/ | | | | | | |_    | | |_| | | | | | | |_) \__ \
834  *  \___|_| |_| |_|_|\__|   | |\__,_|_| |_| |_| .__/|___/
835  *                         _/ |               | |
836  *                        |__/                |_|
837  *********************************************************/
838
839 /* jump table entry (target and corresponding number) */
840 typedef struct _branch_t {
841         ir_node *target;
842         int      value;
843 } branch_t;
844
845 /* jump table for switch generation */
846 typedef struct _jmp_tbl_t {
847         ir_node  *defProj;         /**< default target */
848         int       min_value;       /**< smallest switch case */
849         int       max_value;       /**< largest switch case */
850         int       num_branches;    /**< number of jumps */
851         char     *label;           /**< label of the jump table */
852         branch_t *branches;        /**< jump array */
853 } jmp_tbl_t;
854
855 /**
856  * Compare two variables of type branch_t. Used to sort all switch cases
857  */
858 static int ia32_cmp_branch_t(const void *a, const void *b) {
859         branch_t *b1 = (branch_t *)a;
860         branch_t *b2 = (branch_t *)b;
861
862         if (b1->value <= b2->value)
863                 return -1;
864         else
865                 return 1;
866 }
867
868 /**
869  * Emits code for a SwitchJmp (creates a jump table if
870  * possible otherwise a cmp-jmp cascade). Port from
871  * cggg ia32 backend
872  */
873 static void emit_ia32_SwitchJmp(const ir_node *irn, ia32_emit_env_t *emit_env) {
874         unsigned long       interval;
875         char                buf[SNPRINTF_BUF_LEN];
876         int                 last_value, i, pn;
877         jmp_tbl_t           tbl;
878         ir_node            *proj;
879         const ir_edge_t    *edge;
880         const lc_arg_env_t *env = ia32_get_arg_env();
881         FILE               *F   = emit_env->out;
882         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
883
884         /* fill the table structure */
885         tbl.label        = xmalloc(SNPRINTF_BUF_LEN);
886         tbl.label        = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
887         tbl.defProj      = NULL;
888         tbl.num_branches = get_irn_n_edges(irn);
889         tbl.branches     = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
890         tbl.min_value    = INT_MAX;
891         tbl.max_value    = INT_MIN;
892
893         i = 0;
894         /* go over all proj's and collect them */
895         foreach_out_edge(irn, edge) {
896                 proj = get_edge_src_irn(edge);
897                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
898
899                 pn = get_Proj_proj(proj);
900
901                 /* create branch entry */
902                 tbl.branches[i].target = proj;
903                 tbl.branches[i].value  = pn;
904
905                 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
906                 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
907
908                 /* check for default proj */
909                 if (pn == get_ia32_pncode(irn)) {
910                         assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
911                         tbl.defProj = proj;
912                 }
913
914                 i++;
915         }
916
917         /* sort the branches by their number */
918         qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
919
920         /* two-complement's magic make this work without overflow */
921         interval = tbl.max_value - tbl.min_value;
922
923         /* emit the table */
924         if (tbl.min_value != 0) {
925                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "sub [%1S-%d], %u", irn, tbl.min_value * 4, interval);
926                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmp DWORD PTR [%1S-%d], %u", irn, tbl.min_value * 4, interval);
927                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* first switch value is not 0 */");
928
929                 IA32_DO_EMIT(irn);
930         }
931         else {
932                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, %u", irn, interval);
933                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* compare for switch */");
934
935                 IA32_DO_EMIT(irn);
936         }
937
938         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
939         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default jump if out of range  */");
940         IA32_DO_EMIT(irn);
941
942         if (tbl.num_branches > 1) {
943                 /* create table */
944
945                 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp %s[%1S*4]", tbl.label, irn);
946                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get jump table entry as target */");
947                 IA32_DO_EMIT(irn);
948
949                 fprintf(F, "\t.section\t.rodata\n");
950                 fprintf(F, "\t.align 4\n");
951
952                 fprintf(F, "%s:\n", tbl.label);
953
954                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
955                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */\n",  tbl.branches[0].value);
956                 IA32_DO_EMIT(irn);
957
958                 last_value = tbl.branches[0].value;
959                 for (i = 1; i < tbl.num_branches; ++i) {
960                         while (++last_value < tbl.branches[i].value) {
961                                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
962                                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
963                                 IA32_DO_EMIT(irn);
964                         }
965                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf));
966                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", last_value);
967                         IA32_DO_EMIT(irn);
968                 }
969
970                 fprintf(F, "\t.text");
971         }
972         else {
973                 /* one jump is enough */
974                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
975                 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* only one case given */");
976                 IA32_DO_EMIT(irn);
977         }
978
979         if (tbl.label)
980                 free(tbl.label);
981         if (tbl.branches)
982                 free(tbl.branches);
983 }
984
985 /**
986  * Emits code for a unconditional jump.
987  */
988 static void emit_Jmp(const ir_node *irn, ia32_emit_env_t *env) {
989         ir_node *block, *next_bl;
990         FILE *F = env->out;
991         char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
992
993         /* for now, the code works for scheduled and non-schedules blocks */
994         block = get_nodes_block(irn);
995
996         /* we have a block schedule */
997         next_bl = next_blk_sched(block);
998         if (get_cfop_target_block(irn) != next_bl) {
999                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf));
1000                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F) */", irn, get_cfop_target_block(irn));
1001         }
1002         else {
1003                 cmd_buf[0] = '\0';
1004                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(irn, buf));
1005         }
1006         IA32_DO_EMIT(irn);
1007 }
1008
1009 /****************************
1010  *                  _
1011  *                 (_)
1012  *  _ __  _ __ ___  _  ___
1013  * | '_ \| '__/ _ \| |/ __|
1014  * | |_) | | | (_) | |\__ \
1015  * | .__/|_|  \___/| ||___/
1016  * | |            _/ |
1017  * |_|           |__/
1018  ****************************/
1019
1020 /**
1021  * Emits code for a proj -> node
1022  */
1023 static void emit_Proj(const ir_node *irn, ia32_emit_env_t *env) {
1024         ir_node *pred = get_Proj_pred(irn);
1025
1026         if (get_irn_op(pred) == op_Start) {
1027                 switch(get_Proj_proj(irn)) {
1028                         case pn_Start_X_initial_exec:
1029                                 emit_Jmp(irn, env);
1030                                 break;
1031                         default:
1032                                 break;
1033                 }
1034         }
1035 }
1036
1037 /**********************************
1038  *   _____                  ____
1039  *  / ____|                |  _ \
1040  * | |     ___  _ __  _   _| |_) |
1041  * | |    / _ \| '_ \| | | |  _ <
1042  * | |___| (_) | |_) | |_| | |_) |
1043  *  \_____\___/| .__/ \__, |____/
1044  *             | |     __/ |
1045  *             |_|    |___/
1046  **********************************/
1047
1048 /**
1049  * Emit movsb/w instructions to make mov count divideable by 4
1050  */
1051 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
1052         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1053
1054         fprintf(F, "\t/* memcopy %d bytes*/\n", size);
1055
1056         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
1057         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward*/");
1058         IA32_DO_EMIT(NULL);
1059
1060         switch(rem) {
1061                 case 1:
1062                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1063                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 1 */");
1064                         break;
1065                 case 2:
1066                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1067                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 2 */");
1068                         break;
1069                 case 3:
1070                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1071                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1072                         IA32_DO_EMIT(NULL);
1073                         snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1074                         snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1075                         break;
1076         }
1077
1078         IA32_DO_EMIT(NULL);
1079 }
1080
1081 /**
1082  * Emit rep movsd instruction for memcopy.
1083  */
1084 static void emit_ia32_CopyB(const ir_node *irn, ia32_emit_env_t *emit_env) {
1085         FILE   *F    = emit_env->out;
1086         tarval *tv   = get_ia32_Immop_tarval(irn);
1087         int     rem  = get_tarval_long(tv);
1088         int     size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
1089         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
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_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                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S,%s%u", irn,
1289                         (dir == be_stack_dir_expand) ? " -" : " ", offs);
1290                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (IncSP) */", irn);
1291         }
1292         else {
1293                 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1294                 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted %+F (IncSP) with 0 */", irn);
1295         }
1296
1297         IA32_DO_EMIT(irn);
1298 }
1299
1300 /**
1301  * Emits code to set stack pointer.
1302  */
1303 static void emit_be_SetSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1304         FILE *F = emit_env->out;
1305         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1306
1307         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1308         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (restore SP) */", irn);
1309         IA32_DO_EMIT(irn);
1310 }
1311
1312 /**
1313  * Emits code for Copy.
1314  */
1315 static void emit_be_Copy(const ir_node *irn, ia32_emit_env_t *emit_env) {
1316         FILE *F = emit_env->out;
1317         const arch_env_t *aenv = emit_env->arch_env;
1318         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1319
1320         if (REGS_ARE_EQUAL(arch_get_irn_register(aenv, irn), arch_get_irn_register(aenv, be_get_Copy_op(irn))))
1321                 return;
1322
1323         if (mode_is_float(get_irn_mode(irn)))
1324                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "movs%M %1D, %1S", irn, irn, irn);
1325         else
1326                 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
1327         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1328         IA32_DO_EMIT(irn);
1329 }
1330
1331 /**
1332  * Emits code for exchange.
1333  */
1334 static void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
1335         FILE *F = emit_env->out;
1336         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1337
1338         lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1339         lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%1A, %2A) */", irn, irn, irn);
1340         IA32_DO_EMIT(irn);
1341 }
1342
1343
1344
1345 /***********************************************************************************
1346  *                  _          __                                             _
1347  *                 (_)        / _|                                           | |
1348  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
1349  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1350  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
1351  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
1352  *
1353  ***********************************************************************************/
1354
1355 /**
1356  * Enters the emitter functions for handled nodes into the generic
1357  * pointer of an opcode.
1358  */
1359 static void ia32_register_emitters(void) {
1360
1361 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
1362 #define EMIT(a)      op_##a->ops.generic = (op_func)emit_##a
1363 #define BE_EMIT(a)   op_be_##a->ops.generic = (op_func)emit_be_##a
1364
1365         /* first clear the generic function pointer for all ops */
1366         clear_irp_opcodes_generic_func();
1367
1368         /* register all emitter functions defined in spec */
1369         ia32_register_spec_emitters();
1370
1371         /* other ia32 emitter functions */
1372         IA32_EMIT(CondJmp);
1373         IA32_EMIT(TestJmp);
1374         IA32_EMIT(CJmp);
1375         IA32_EMIT(CJmpAM);
1376         IA32_EMIT(SwitchJmp);
1377         IA32_EMIT(CopyB);
1378         IA32_EMIT(CopyB_i);
1379         IA32_EMIT(Conv_I2FP);
1380         IA32_EMIT(Conv_FP2I);
1381         IA32_EMIT(Conv_FP2FP);
1382         IA32_EMIT(Conv_I2I);
1383         IA32_EMIT(Conv_I2I8Bit);
1384
1385         /* benode emitter */
1386         BE_EMIT(Call);
1387         BE_EMIT(IncSP);
1388         BE_EMIT(SetSP);
1389         BE_EMIT(Copy);
1390         BE_EMIT(Perm);
1391
1392         /* firm emitter */
1393         EMIT(Jmp);
1394         EMIT(Proj);
1395
1396 #undef IA32_EMIT
1397 #undef BE_EMIT
1398 #undef EMIT
1399 }
1400
1401 /**
1402  * Emits code for a node.
1403  */
1404 static void ia32_emit_node(const ir_node *irn, void *env) {
1405         ia32_emit_env_t   *emit_env = env;
1406         FILE              *F        = emit_env->out;
1407         ir_op             *op       = get_irn_op(irn);
1408         DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
1409
1410         DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
1411
1412         if (op->ops.generic) {
1413                 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
1414                 (*emit)(irn, env);
1415         }
1416         else {
1417                 ir_fprintf(F, "\t%35s /* %+F (%+G) */\n", " ", irn, irn);
1418         }
1419 }
1420
1421 /**
1422  * Walks over the nodes in a block connected by scheduling edges
1423  * and emits code for each node.
1424  */
1425 static void ia32_gen_block(ir_node *block, void *env) {
1426         const ir_node *irn;
1427
1428         if (! is_Block(block))
1429                 return;
1430
1431         fprintf(((ia32_emit_env_t *)env)->out, BLOCK_PREFIX("%ld:\n"), get_irn_node_nr(block));
1432         sched_foreach(block, irn) {
1433                 ia32_emit_node(irn, env);
1434         }
1435 }
1436
1437 /**
1438  * Emits code for function start.
1439  */
1440 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
1441         entity     *irg_ent  = get_irg_entity(irg);
1442         const char *irg_name = get_entity_name(irg_ent);
1443
1444         fprintf(F, "\t.section\t.text\n");
1445         if (get_entity_visibility(irg_ent) == visibility_external_visible) {
1446                 fprintf(F, ".globl %s\n", irg_name);
1447         }
1448         fprintf(F, "\t.type\t%s, @function\n", irg_name);
1449         fprintf(F, "%s:\n", irg_name);
1450 }
1451
1452 /**
1453  * Emits code for function end
1454  */
1455 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1456         const char *irg_name = get_entity_name(get_irg_entity(irg));
1457
1458         fprintf(F, "\tret\n");
1459         fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
1460 }
1461
1462 /**
1463  * Block-walker:
1464  * Sets labels for control flow nodes (jump target)
1465  * TODO: Jump optimization
1466  */
1467 static void ia32_gen_labels(ir_node *block, void *env) {
1468         ir_node *pred;
1469         int n = get_Block_n_cfgpreds(block);
1470
1471         for (n--; n >= 0; n--) {
1472                 pred = get_Block_cfgpred(block, n);
1473                 set_irn_link(pred, block);
1474         }
1475 }
1476
1477 /**
1478  * Main driver. Emits the code for one routine.
1479  */
1480 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1481         ia32_emit_env_t emit_env;
1482         ir_node *block;
1483
1484         emit_env.out      = F;
1485         emit_env.arch_env = cg->arch_env;
1486         emit_env.cg       = cg;
1487         emit_env.isa      = (ia32_isa_t *)cg->arch_env->isa;
1488         FIRM_DBG_REGISTER(emit_env.mod, "firm.be.ia32.emitter");
1489
1490         /* set the global arch_env (needed by print hooks) */
1491         arch_env = cg->arch_env;
1492
1493         ia32_register_emitters();
1494
1495         ia32_emit_func_prolog(F, irg);
1496         irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1497
1498         if (cg->opt.extbb && cg->blk_sched) {
1499                 int i, n = ARR_LEN(cg->blk_sched);
1500
1501                 for (i = 0; i < n;) {
1502                         ir_node *next_bl;
1503
1504                         block   = cg->blk_sched[i];
1505                         ++i;
1506                         next_bl = i < n ? cg->blk_sched[i] : NULL;
1507
1508                         /* set here the link. the emitter expects to find the next block here */
1509                         set_irn_link(block, next_bl);
1510                         ia32_gen_block(block, &emit_env);
1511                 }
1512         }
1513         else {
1514                 /* "normal" block schedule: Note the get_next_block() returns the NUMBER of the block
1515                    in the block schedule. As this number should NEVER be equal the next block,
1516                    we does not need a clear block link here. */
1517                 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1518         }
1519
1520         ia32_emit_func_epilog(F, irg);
1521 }