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