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