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