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