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