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