change backends to produce 1 big array with all registers
[libfirm] / ir / be / ia32 / ia32_emitter.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       This file implements the ia32 node emitter.
23  * @author      Christian Wuerdig, Matthias Braun
24  * @version     $Id$
25  *
26  * Summary table for x86 floatingpoint compares:
27  *   pnc_Eq  => !P && E
28  *   pnc_Lt  => !P && B
29  *   pnc_Le  => !P && BE
30  *   pnc_Gt  => A
31  *   pnc_Ge  => AE
32  *   pnc_Lg  => P || NE
33  *   pnc_Leg => NP  (ordered)
34  *   pnc_Uo  => P
35  *   pnc_Ue  => E
36  *   pnc_Ul  => B
37  *   pnc_Ule => BE
38  *   pnc_Ug  => P || A
39  *   pnc_Uge => P || AE
40  *   pnc_Ne  => NE
41  */
42 #include "config.h"
43
44 #include <limits.h>
45
46 #include "xmalloc.h"
47 #include "tv.h"
48 #include "iredges.h"
49 #include "debug.h"
50 #include "irgwalk.h"
51 #include "irprintf.h"
52 #include "irop_t.h"
53 #include "irargs_t.h"
54 #include "irprog_t.h"
55 #include "iredges_t.h"
56 #include "irtools.h"
57 #include "execfreq.h"
58 #include "error.h"
59 #include "raw_bitset.h"
60 #include "dbginfo.h"
61 #include "lc_opts.h"
62
63 #include "../besched.h"
64 #include "../benode.h"
65 #include "../beabi.h"
66 #include "../be_dbgout.h"
67 #include "../beemitter.h"
68 #include "../begnuas.h"
69 #include "../beirg.h"
70 #include "../be_dbgout.h"
71
72 #include "ia32_emitter.h"
73 #include "ia32_common_transform.h"
74 #include "gen_ia32_emitter.h"
75 #include "gen_ia32_regalloc_if.h"
76 #include "ia32_nodes_attr.h"
77 #include "ia32_new_nodes.h"
78 #include "ia32_map_regs.h"
79 #include "ia32_architecture.h"
80 #include "bearch_ia32_t.h"
81
82 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
83
84 #define SNPRINTF_BUF_LEN 128
85
86 static const ia32_isa_t *isa;
87 static char              pic_base_label[128];
88 static ir_label_t        exc_label_id;
89 static int               mark_spill_reload = 0;
90 static int               do_pic;
91
92 /** Return the next block in Block schedule */
93 static ir_node *get_prev_block_sched(const ir_node *block)
94 {
95         return get_irn_link(block);
96 }
97
98 /** Checks if the current block is a fall-through target. */
99 static int is_fallthrough(const ir_node *cfgpred)
100 {
101         ir_node *pred;
102
103         if (!is_Proj(cfgpred))
104                 return 1;
105         pred = get_Proj_pred(cfgpred);
106         if (is_ia32_SwitchJmp(pred))
107                 return 0;
108
109         return 1;
110 }
111
112 /**
113  * returns non-zero if the given block needs a label
114  * because of being a jump-target (and not a fall-through)
115  */
116 static int block_needs_label(const ir_node *block)
117 {
118         int need_label = 1;
119         int  n_cfgpreds = get_Block_n_cfgpreds(block);
120
121         if (has_Block_entity(block))
122                 return 1;
123
124         if (n_cfgpreds == 0) {
125                 need_label = 0;
126         } else if (n_cfgpreds == 1) {
127                 ir_node *cfgpred       = get_Block_cfgpred(block, 0);
128                 ir_node *cfgpred_block = get_nodes_block(cfgpred);
129
130                 if (get_prev_block_sched(block) == cfgpred_block
131                                 && is_fallthrough(cfgpred)) {
132                         need_label = 0;
133                 }
134         }
135
136         return need_label;
137 }
138
139 /**
140  * Returns the register at in position pos.
141  */
142 static const arch_register_t *get_in_reg(const ir_node *irn, int pos)
143 {
144         ir_node               *op;
145         const arch_register_t *reg = NULL;
146
147         assert(get_irn_arity(irn) > pos && "Invalid IN position");
148
149         /* The out register of the operator at position pos is the
150            in register we need. */
151         op = get_irn_n(irn, pos);
152
153         reg = arch_get_irn_register(op);
154
155         assert(reg && "no in register found");
156
157         if (reg == &ia32_registers[REG_GP_NOREG])
158                 panic("trying to emit noreg for %+F input %d", irn, pos);
159
160         return reg;
161 }
162
163 /**
164  * Returns the register at out position pos.
165  */
166 static const arch_register_t *get_out_reg(const ir_node *irn, int pos)
167 {
168         ir_node               *proj;
169         const arch_register_t *reg = NULL;
170
171         /* 1st case: irn is not of mode_T, so it has only                 */
172         /*           one OUT register -> good                             */
173         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
174         /*           Proj with the corresponding projnum for the register */
175
176         if (get_irn_mode(irn) != mode_T) {
177                 assert(pos == 0);
178                 reg = arch_get_irn_register(irn);
179         } else if (is_ia32_irn(irn)) {
180                 reg = arch_irn_get_register(irn, pos);
181         } else {
182                 const ir_edge_t *edge;
183
184                 foreach_out_edge(irn, edge) {
185                         proj = get_edge_src_irn(edge);
186                         assert(is_Proj(proj) && "non-Proj from mode_T node");
187                         if (get_Proj_proj(proj) == pos) {
188                                 reg = arch_get_irn_register(proj);
189                                 break;
190                         }
191                 }
192         }
193
194         assert(reg && "no out register found");
195         return reg;
196 }
197
198 /**
199  * Add a number to a prefix. This number will not be used a second time.
200  */
201 static char *get_unique_label(char *buf, size_t buflen, const char *prefix)
202 {
203         static unsigned long id = 0;
204         snprintf(buf, buflen, "%s%s%lu", be_gas_get_private_prefix(), prefix, ++id);
205         return buf;
206 }
207
208
209 /**
210  * Emit the name of the 8bit low register
211  */
212 static void emit_8bit_register(const arch_register_t *reg)
213 {
214         const char *reg_name = arch_register_get_name(reg);
215
216         be_emit_char('%');
217         be_emit_char(reg_name[1]);
218         be_emit_char('l');
219 }
220
221 /**
222  * Emit the name of the 8bit high register
223  */
224 static void emit_8bit_register_high(const arch_register_t *reg)
225 {
226         const char *reg_name = arch_register_get_name(reg);
227
228         be_emit_char('%');
229         be_emit_char(reg_name[1]);
230         be_emit_char('h');
231 }
232
233 static void emit_16bit_register(const arch_register_t *reg)
234 {
235         const char *reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
236
237         be_emit_char('%');
238         be_emit_string(reg_name);
239 }
240
241 /**
242  * emit a register, possible shortened by a mode
243  *
244  * @param reg   the register
245  * @param mode  the mode of the register or NULL for full register
246  */
247 static void emit_register(const arch_register_t *reg, const ir_mode *mode)
248 {
249         const char *reg_name;
250
251         if (mode != NULL) {
252                 int size = get_mode_size_bits(mode);
253                 switch (size) {
254                         case  8: emit_8bit_register(reg);  return;
255                         case 16: emit_16bit_register(reg); return;
256                 }
257                 assert(mode_is_float(mode) || size == 32);
258         }
259
260         reg_name = arch_register_get_name(reg);
261
262         be_emit_char('%');
263         be_emit_string(reg_name);
264 }
265
266 void ia32_emit_source_register(const ir_node *node, int pos)
267 {
268         const arch_register_t *reg = get_in_reg(node, pos);
269
270         emit_register(reg, NULL);
271 }
272
273 static void ia32_emit_entity(ir_entity *entity, int no_pic_adjust)
274 {
275         be_gas_emit_entity(entity);
276
277         if (get_entity_owner(entity) == get_tls_type()) {
278                 if (get_entity_visibility(entity) == ir_visibility_external) {
279                         be_emit_cstring("@INDNTPOFF");
280                 } else {
281                         be_emit_cstring("@NTPOFF");
282                 }
283         }
284
285         if (do_pic && !no_pic_adjust) {
286                 be_emit_char('-');
287                 be_emit_string(pic_base_label);
288         }
289 }
290
291 static void emit_ia32_Immediate_no_prefix(const ir_node *node)
292 {
293         const ia32_immediate_attr_t *attr = get_ia32_immediate_attr_const(node);
294
295         if (attr->symconst != NULL) {
296                 if (attr->sc_sign)
297                         be_emit_char('-');
298                 ia32_emit_entity(attr->symconst, attr->no_pic_adjust);
299         }
300         if (attr->symconst == NULL || attr->offset != 0) {
301                 if (attr->symconst != NULL) {
302                         be_emit_irprintf("%+d", attr->offset);
303                 } else {
304                         be_emit_irprintf("0x%X", attr->offset);
305                 }
306         }
307 }
308
309 static void emit_ia32_Immediate(const ir_node *node)
310 {
311         be_emit_char('$');
312         emit_ia32_Immediate_no_prefix(node);
313 }
314
315 void ia32_emit_8bit_source_register_or_immediate(const ir_node *node, int pos)
316 {
317         const arch_register_t *reg;
318         const ir_node         *in = get_irn_n(node, pos);
319         if (is_ia32_Immediate(in)) {
320                 emit_ia32_Immediate(in);
321                 return;
322         }
323
324         reg = get_in_reg(node, pos);
325         emit_8bit_register(reg);
326 }
327
328 void ia32_emit_8bit_high_source_register(const ir_node *node, int pos)
329 {
330         const arch_register_t *reg = get_in_reg(node, pos);
331         emit_8bit_register_high(reg);
332 }
333
334 void ia32_emit_16bit_source_register_or_immediate(const ir_node *node, int pos)
335 {
336         const arch_register_t *reg;
337         const ir_node         *in = get_irn_n(node, pos);
338         if (is_ia32_Immediate(in)) {
339                 emit_ia32_Immediate(in);
340                 return;
341         }
342
343         reg = get_in_reg(node, pos);
344         emit_16bit_register(reg);
345 }
346
347 void ia32_emit_dest_register(const ir_node *node, int pos)
348 {
349         const arch_register_t *reg  = get_out_reg(node, pos);
350
351         emit_register(reg, NULL);
352 }
353
354 void ia32_emit_dest_register_size(const ir_node *node, int pos)
355 {
356         const arch_register_t *reg  = get_out_reg(node, pos);
357
358         emit_register(reg, get_ia32_ls_mode(node));
359 }
360
361 void ia32_emit_8bit_dest_register(const ir_node *node, int pos)
362 {
363         const arch_register_t *reg  = get_out_reg(node, pos);
364
365         emit_register(reg, mode_Bu);
366 }
367
368 void ia32_emit_x87_register(const ir_node *node, int pos)
369 {
370         const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
371
372         assert(pos < 3);
373         be_emit_char('%');
374         be_emit_string(attr->x87[pos]->name);
375 }
376
377 static void ia32_emit_mode_suffix_mode(const ir_mode *mode)
378 {
379         assert(mode_is_int(mode) || mode_is_reference(mode));
380         switch (get_mode_size_bits(mode)) {
381                 case 8:  be_emit_char('b');     return;
382                 case 16: be_emit_char('w');     return;
383                 case 32: be_emit_char('l');     return;
384                 /* gas docu says q is the suffix but gcc, objdump and icc use ll
385                  * apparently */
386                 case 64: be_emit_cstring("ll"); return;
387         }
388         panic("Can't output mode_suffix for %+F", mode);
389 }
390
391 void ia32_emit_mode_suffix(const ir_node *node)
392 {
393         ir_mode *mode = get_ia32_ls_mode(node);
394         if (mode == NULL)
395                 mode = mode_Iu;
396
397         ia32_emit_mode_suffix_mode(mode);
398 }
399
400 void ia32_emit_x87_mode_suffix(const ir_node *node)
401 {
402         ir_mode *mode;
403
404         /* we only need to emit the mode on address mode */
405         if (get_ia32_op_type(node) == ia32_Normal)
406                 return;
407
408         mode = get_ia32_ls_mode(node);
409         assert(mode != NULL);
410
411         if (mode_is_float(mode)) {
412                 switch (get_mode_size_bits(mode)) {
413                         case  32: be_emit_char('s'); return;
414                         case  64: be_emit_char('l'); return;
415                         /* long doubles have different sizes due to alignment on different
416                          * platforms. */
417                         case  80:
418                         case  96:
419                         case 128: be_emit_char('t'); return;
420                 }
421         } else {
422                 assert(mode_is_int(mode) || mode_is_reference(mode));
423                 switch (get_mode_size_bits(mode)) {
424                         case 16: be_emit_char('s');     return;
425                         case 32: be_emit_char('l');     return;
426                         /* gas docu says q is the suffix but gcc, objdump and icc use ll
427                          * apparently */
428                         case 64: be_emit_cstring("ll"); return;
429                 }
430         }
431         panic("Can't output mode_suffix for %+F", mode);
432 }
433
434 static char get_xmm_mode_suffix(ir_mode *mode)
435 {
436         assert(mode_is_float(mode));
437         switch (get_mode_size_bits(mode)) {
438         case 32: return 's';
439         case 64: return 'd';
440         default: panic("Invalid XMM mode");
441         }
442 }
443
444 void ia32_emit_xmm_mode_suffix(const ir_node *node)
445 {
446         ir_mode *mode = get_ia32_ls_mode(node);
447         assert(mode != NULL);
448         be_emit_char('s');
449         be_emit_char(get_xmm_mode_suffix(mode));
450 }
451
452 void ia32_emit_xmm_mode_suffix_s(const ir_node *node)
453 {
454         ir_mode *mode = get_ia32_ls_mode(node);
455         assert(mode != NULL);
456         be_emit_char(get_xmm_mode_suffix(mode));
457 }
458
459 void ia32_emit_extend_suffix(const ir_node *node)
460 {
461         ir_mode *mode = get_ia32_ls_mode(node);
462         if (get_mode_size_bits(mode) == 32)
463                 return;
464         be_emit_char(mode_is_signed(mode) ? 's' : 'z');
465         ia32_emit_mode_suffix_mode(mode);
466 }
467
468 void ia32_emit_source_register_or_immediate(const ir_node *node, int pos)
469 {
470         ir_node *in = get_irn_n(node, pos);
471         if (is_ia32_Immediate(in)) {
472                 emit_ia32_Immediate(in);
473         } else {
474                 const ir_mode         *mode = get_ia32_ls_mode(node);
475                 const arch_register_t *reg  = get_in_reg(node, pos);
476                 emit_register(reg, mode);
477         }
478 }
479
480 /**
481  * Returns the target block for a control flow node.
482  */
483 static ir_node *get_cfop_target_block(const ir_node *irn)
484 {
485         assert(get_irn_mode(irn) == mode_X);
486         return get_irn_link(irn);
487 }
488
489 /**
490  * Emits the target label for a control flow node.
491  */
492 static void ia32_emit_cfop_target(const ir_node *node)
493 {
494         ir_node *block = get_cfop_target_block(node);
495         be_gas_emit_block_name(block);
496 }
497
498 /*
499  * positive conditions for signed compares
500  */
501 static const char *const cmp2condition_s[] = {
502         NULL, /* always false */
503         "e",  /* == */
504         "l",  /* <  */
505         "le", /* <= */
506         "g",  /* >  */
507         "ge", /* >= */
508         "ne", /* != */
509         NULL  /* always true */
510 };
511
512 /*
513  * positive conditions for unsigned compares
514  */
515 static const char *const cmp2condition_u[] = {
516         NULL, /* always false */
517         "e",  /* == */
518         "b",  /* <  */
519         "be", /* <= */
520         "a",  /* >  */
521         "ae", /* >= */
522         "ne", /* != */
523         NULL  /* always true */
524 };
525
526 /**
527  * Emit the suffix for a compare instruction.
528  */
529 static void ia32_emit_cmp_suffix(int pnc)
530 {
531         const char *str;
532
533         if (pnc == ia32_pn_Cmp_parity) {
534                 be_emit_char('p');
535                 return;
536         }
537
538         if (pnc & ia32_pn_Cmp_float || pnc & ia32_pn_Cmp_unsigned) {
539                 str = cmp2condition_u[pnc & 7];
540         } else {
541                 str = cmp2condition_s[pnc & 7];
542         }
543
544         be_emit_string(str);
545 }
546
547 typedef enum ia32_emit_mod_t {
548         EMIT_RESPECT_LS   = 1U << 0,
549         EMIT_ALTERNATE_AM = 1U << 1,
550         EMIT_LONG         = 1U << 2,
551         EMIT_HIGH_REG     = 1U << 3,
552         EMIT_LOW_REG      = 1U << 4
553 } ia32_emit_mod_t;
554
555 /**
556  * Emits address mode.
557  */
558 void ia32_emit_am(const ir_node *node)
559 {
560         ir_entity *ent       = get_ia32_am_sc(node);
561         int        offs      = get_ia32_am_offs_int(node);
562         ir_node   *base      = get_irn_n(node, n_ia32_base);
563         int        has_base  = !is_ia32_NoReg_GP(base);
564         ir_node   *index     = get_irn_n(node, n_ia32_index);
565         int        has_index = !is_ia32_NoReg_GP(index);
566
567         /* just to be sure... */
568         assert(!is_ia32_use_frame(node) || get_ia32_frame_ent(node) != NULL);
569
570         /* emit offset */
571         if (ent != NULL) {
572                 const ia32_attr_t *attr = get_ia32_attr_const(node);
573                 if (is_ia32_am_sc_sign(node))
574                         be_emit_char('-');
575                 ia32_emit_entity(ent, attr->data.am_sc_no_pic_adjust);
576         }
577
578         /* also handle special case if nothing is set */
579         if (offs != 0 || (ent == NULL && !has_base && !has_index)) {
580                 if (ent != NULL) {
581                         be_emit_irprintf("%+d", offs);
582                 } else {
583                         be_emit_irprintf("%d", offs);
584                 }
585         }
586
587         if (has_base || has_index) {
588                 be_emit_char('(');
589
590                 /* emit base */
591                 if (has_base) {
592                         const arch_register_t *reg = get_in_reg(node, n_ia32_base);
593                         emit_register(reg, NULL);
594                 }
595
596                 /* emit index + scale */
597                 if (has_index) {
598                         const arch_register_t *reg = get_in_reg(node, n_ia32_index);
599                         int scale;
600                         be_emit_char(',');
601                         emit_register(reg, NULL);
602
603                         scale = get_ia32_am_scale(node);
604                         if (scale > 0) {
605                                 be_emit_irprintf(",%d", 1 << scale);
606                         }
607                 }
608                 be_emit_char(')');
609         }
610 }
611
612 /**
613  * fmt  parameter               output
614  * ---- ----------------------  ---------------------------------------------
615  * %%                           %
616  * %AM  <node>                  address mode of the node
617  * %AR  const arch_register_t*  address mode of the node or register
618  * %ASx <node>                  address mode of the node or source register x
619  * %Dx  <node>                  destination register x
620  * %I   <node>                  immediate of the node
621  * %L   <node>                  control flow target of the node
622  * %M   <node>                  mode suffix of the node
623  * %P   int                     condition code
624  * %R   const arch_register_t*  register
625  * %Sx  <node>                  source register x
626  * %s   const char*             string
627  * %u   unsigned int            unsigned int
628  * %d   signed int              signed int
629  *
630  * x starts at 0
631  * # modifier for %ASx, %D, %R, and %S uses ls mode of node to alter register width
632  * * modifier does not prefix immediates with $, but AM with *
633  * l modifier for %lu and %ld
634  * > modifier to output high 8bit register (ah, bh)
635  * < modifier to output low 8bit register (al, bl)
636  */
637 static void ia32_emitf(const ir_node *node, const char *fmt, ...)
638 {
639         va_list ap;
640         va_start(ap, fmt);
641
642         for (;;) {
643                 const char      *start = fmt;
644                 ia32_emit_mod_t  mod   = 0;
645
646                 while (*fmt != '%' && *fmt != '\n' && *fmt != '\0')
647                         ++fmt;
648                 if (fmt != start) {
649                         be_emit_string_len(start, fmt - start);
650                 }
651
652                 if (*fmt == '\n') {
653                         be_emit_finish_line_gas(node);
654                         ++fmt;
655                         if (*fmt == '\0')
656                                 break;
657                         continue;
658                 }
659
660                 if (*fmt == '\0')
661                         break;
662
663                 ++fmt;
664                 for (;;) {
665                         switch (*fmt) {
666                         case '*': mod |= EMIT_ALTERNATE_AM; break;
667                         case '#': mod |= EMIT_RESPECT_LS;   break;
668                         case 'l': mod |= EMIT_LONG;         break;
669                         case '>': mod |= EMIT_HIGH_REG;     break;
670                         case '<': mod |= EMIT_LOW_REG;      break;
671                         default:
672                                 goto end_of_mods;
673                         }
674                         ++fmt;
675                 }
676 end_of_mods:
677
678                 switch (*fmt++) {
679                         case '%':
680                                 be_emit_char('%');
681                                 break;
682
683                         case 'A': {
684                                 switch (*fmt++) {
685 emit_AM:
686                                         case 'M':
687                                                 if (mod & EMIT_ALTERNATE_AM)
688                                                         be_emit_char('*');
689                                                 ia32_emit_am(node);
690                                                 break;
691
692                                         case 'R': {
693                                                 const arch_register_t *reg = va_arg(ap, const arch_register_t*);
694                                                 if (get_ia32_op_type(node) == ia32_AddrModeS) {
695                                                         goto emit_AM;
696                                                 } else {
697                                                         if (mod & EMIT_ALTERNATE_AM)
698                                                                 be_emit_char('*');
699                                                         emit_register(reg, NULL);
700                                                 }
701                                                 break;
702                                         }
703
704                                         case 'S':
705                                                 if (get_ia32_op_type(node) == ia32_AddrModeS) {
706                                                         ++fmt;
707                                                         goto emit_AM;
708                                                 } else {
709                                                         assert(get_ia32_op_type(node) == ia32_Normal);
710                                                         goto emit_S;
711                                                 }
712                                                 break;
713
714                                         default: goto unknown;
715                                 }
716                                 break;
717                         }
718
719                         case 'D': {
720                                 unsigned               pos;
721                                 const arch_register_t *reg;
722
723                                 if (*fmt < '0' || '9' <= *fmt)
724                                         goto unknown;
725
726                                 pos = *fmt++ - '0';
727                                 reg = get_out_reg(node, pos);
728                                 emit_register(reg, mod & EMIT_RESPECT_LS ? get_ia32_ls_mode(node) : NULL);
729                                 break;
730                         }
731
732                         case 'I':
733                                 if (!(mod & EMIT_ALTERNATE_AM))
734                                         be_emit_char('$');
735                                 emit_ia32_Immediate_no_prefix(node);
736                                 break;
737
738                         case 'L':
739                                 ia32_emit_cfop_target(node);
740                                 break;
741
742                         case 'M': {
743                                 ia32_emit_mode_suffix_mode(get_ia32_ls_mode(node));
744                                 break;
745                         }
746
747                         case 'P': {
748                                 int pnc = va_arg(ap, int);
749                                 ia32_emit_cmp_suffix(pnc);
750                                 break;
751                         }
752
753                         case 'R': {
754                                 const arch_register_t *reg = va_arg(ap, const arch_register_t*);
755                                 if (mod & EMIT_HIGH_REG) {
756                                         emit_8bit_register_high(reg);
757                                 } else if (mod & EMIT_LOW_REG) {
758                                         emit_8bit_register(reg);
759                                 } else {
760                                         emit_register(reg, mod & EMIT_RESPECT_LS ? get_ia32_ls_mode(node) : NULL);
761                                 }
762                                 break;
763                         }
764
765 emit_S:
766                         case 'S': {
767                                 unsigned       pos;
768                                 const ir_node *in;
769
770                                 if (*fmt < '0' || '9' <= *fmt)
771                                         goto unknown;
772
773                                 pos = *fmt++ - '0';
774                                 in  = get_irn_n(node, pos);
775                                 if (is_ia32_Immediate(in)) {
776                                         if (!(mod & EMIT_ALTERNATE_AM))
777                                                 be_emit_char('$');
778                                         emit_ia32_Immediate_no_prefix(in);
779                                 } else {
780                                         const arch_register_t *reg;
781
782                                         if (mod & EMIT_ALTERNATE_AM)
783                                                 be_emit_char('*');
784                                         reg = get_in_reg(node, pos);
785                                         emit_register(reg, mod & EMIT_RESPECT_LS ? get_ia32_ls_mode(node) : NULL);
786                                 }
787                                 break;
788                         }
789
790                         case 's': {
791                                 const char *str = va_arg(ap, const char*);
792                                 be_emit_string(str);
793                                 break;
794                         }
795
796                         case 'u':
797                                 if (mod & EMIT_LONG) {
798                                         unsigned long num = va_arg(ap, unsigned long);
799                                         be_emit_irprintf("%lu", num);
800                                 } else {
801                                         unsigned num = va_arg(ap, unsigned);
802                                         be_emit_irprintf("%u", num);
803                                 }
804                                 break;
805
806                         case 'd':
807                                 if (mod & EMIT_LONG) {
808                                         long num = va_arg(ap, long);
809                                         be_emit_irprintf("%ld", num);
810                                 } else {
811                                         int num = va_arg(ap, int);
812                                         be_emit_irprintf("%d", num);
813                                 }
814                                 break;
815
816                         default:
817 unknown:
818                                 panic("unknown format conversion in ia32_emitf()");
819                 }
820         }
821
822         va_end(ap);
823 }
824
825 /**
826  * Emits registers and/or address mode of a binary operation.
827  */
828 void ia32_emit_binop(const ir_node *node)
829 {
830         if (is_ia32_Immediate(get_irn_n(node, n_ia32_binary_right))) {
831                 ia32_emitf(node, "%#S4, %#AS3");
832         } else {
833                 ia32_emitf(node, "%#AS4, %#S3");
834         }
835 }
836
837 /**
838  * Emits registers and/or address mode of a binary operation.
839  */
840 void ia32_emit_x87_binop(const ir_node *node)
841 {
842         switch (get_ia32_op_type(node)) {
843                 case ia32_Normal:
844                         {
845                                 const ia32_x87_attr_t *x87_attr = get_ia32_x87_attr_const(node);
846                                 const arch_register_t *in1      = x87_attr->x87[0];
847                                 const arch_register_t *in       = x87_attr->x87[1];
848                                 const arch_register_t *out      = x87_attr->x87[2];
849
850                                 if (out == NULL) {
851                                         out = in1;
852                                 } else if (out == in) {
853                                         in = in1;
854                                 }
855
856                                 be_emit_char('%');
857                                 be_emit_string(arch_register_get_name(in));
858                                 be_emit_cstring(", %");
859                                 be_emit_string(arch_register_get_name(out));
860                         }
861                         break;
862                 case ia32_AddrModeS:
863                         ia32_emit_am(node);
864                         break;
865                 case ia32_AddrModeD:
866                 default:
867                         assert(0 && "unsupported op type");
868         }
869 }
870
871 /**
872  * Emits registers and/or address mode of a unary operation.
873  */
874 void ia32_emit_unop(const ir_node *node, int pos)
875 {
876         char fmt[] = "%ASx";
877         fmt[3] = '0' + pos;
878         ia32_emitf(node, fmt);
879 }
880
881 static void emit_ia32_IMul(const ir_node *node)
882 {
883         ir_node               *left    = get_irn_n(node, n_ia32_IMul_left);
884         const arch_register_t *out_reg = get_out_reg(node, pn_ia32_IMul_res);
885
886         /* do we need the 3-address form? */
887         if (is_ia32_NoReg_GP(left) ||
888                         get_in_reg(node, n_ia32_IMul_left) != out_reg) {
889                 ia32_emitf(node, "\timul%M %#S4, %#AS3, %#D0\n");
890         } else {
891                 ia32_emitf(node, "\timul%M %#AS4, %#S3\n");
892         }
893 }
894
895 /**
896  * walks up a tree of copies/perms/spills/reloads to find the original value
897  * that is moved around
898  */
899 static ir_node *find_original_value(ir_node *node)
900 {
901         if (irn_visited(node))
902                 return NULL;
903
904         mark_irn_visited(node);
905         if (be_is_Copy(node)) {
906                 return find_original_value(be_get_Copy_op(node));
907         } else if (be_is_CopyKeep(node)) {
908                 return find_original_value(be_get_CopyKeep_op(node));
909         } else if (is_Proj(node)) {
910                 ir_node *pred = get_Proj_pred(node);
911                 if (be_is_Perm(pred)) {
912                         return find_original_value(get_irn_n(pred, get_Proj_proj(node)));
913                 } else if (be_is_MemPerm(pred)) {
914                         return find_original_value(get_irn_n(pred, get_Proj_proj(node) + 1));
915                 } else if (is_ia32_Load(pred)) {
916                         return find_original_value(get_irn_n(pred, n_ia32_Load_mem));
917                 } else {
918                         return node;
919                 }
920         } else if (is_ia32_Store(node)) {
921                 return find_original_value(get_irn_n(node, n_ia32_Store_val));
922         } else if (is_Phi(node)) {
923                 int i, arity;
924                 arity = get_irn_arity(node);
925                 for (i = 0; i < arity; ++i) {
926                         ir_node *in  = get_irn_n(node, i);
927                         ir_node *res = find_original_value(in);
928
929                         if (res != NULL)
930                                 return res;
931                 }
932                 return NULL;
933         } else {
934                 return node;
935         }
936 }
937
938 static int determine_final_pnc(const ir_node *node, int flags_pos, int pnc)
939 {
940         ir_node           *flags = get_irn_n(node, flags_pos);
941         const ia32_attr_t *flags_attr;
942         flags = skip_Proj(flags);
943
944         if (is_ia32_Sahf(flags)) {
945                 ir_node *cmp = get_irn_n(flags, n_ia32_Sahf_val);
946                 if (!(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
947                                 || is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp))) {
948                         inc_irg_visited(current_ir_graph);
949                         cmp = find_original_value(cmp);
950                         assert(cmp != NULL);
951                         assert(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
952                                || is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp));
953                 }
954
955                 flags_attr = get_ia32_attr_const(cmp);
956                 if (flags_attr->data.ins_permuted)
957                         pnc = get_mirrored_pnc(pnc);
958                 pnc |= ia32_pn_Cmp_float;
959         } else if (is_ia32_Ucomi(flags) || is_ia32_Fucomi(flags)
960                         || is_ia32_Fucompi(flags)) {
961                 flags_attr = get_ia32_attr_const(flags);
962
963                 if (flags_attr->data.ins_permuted)
964                         pnc = get_mirrored_pnc(pnc);
965                 pnc |= ia32_pn_Cmp_float;
966         } else {
967                 flags_attr = get_ia32_attr_const(flags);
968
969                 if (flags_attr->data.ins_permuted)
970                         pnc = get_mirrored_pnc(pnc);
971                 if (flags_attr->data.cmp_unsigned)
972                         pnc |= ia32_pn_Cmp_unsigned;
973         }
974
975         return pnc;
976 }
977
978 static pn_Cmp ia32_get_negated_pnc(pn_Cmp pnc)
979 {
980         ir_mode *mode = pnc & ia32_pn_Cmp_float ? mode_F : mode_Iu;
981         return get_negated_pnc(pnc, mode);
982 }
983
984 void ia32_emit_cmp_suffix_node(const ir_node *node, int flags_pos)
985 {
986         pn_Cmp pnc = get_ia32_condcode(node);
987         pnc = determine_final_pnc(node, flags_pos, pnc);
988
989         ia32_emit_cmp_suffix(pnc);
990 }
991
992 /**
993  * Emits an exception label for a given node.
994  */
995 static void ia32_emit_exc_label(const ir_node *node)
996 {
997         be_emit_string(be_gas_insn_label_prefix());
998         be_emit_irprintf("%lu", get_ia32_exc_label_id(node));
999 }
1000
1001 /**
1002  * Returns the Proj with projection number proj and NOT mode_M
1003  */
1004 static ir_node *get_proj(const ir_node *node, long proj)
1005 {
1006         const ir_edge_t *edge;
1007         ir_node         *src;
1008
1009         assert(get_irn_mode(node) == mode_T && "expected mode_T node");
1010
1011         foreach_out_edge(node, edge) {
1012                 src = get_edge_src_irn(edge);
1013
1014                 assert(is_Proj(src) && "Proj expected");
1015                 if (get_irn_mode(src) == mode_M)
1016                         continue;
1017
1018                 if (get_Proj_proj(src) == proj)
1019                         return src;
1020         }
1021         return NULL;
1022 }
1023
1024 static int can_be_fallthrough(const ir_node *node)
1025 {
1026         ir_node *target_block = get_cfop_target_block(node);
1027         ir_node *block        = get_nodes_block(node);
1028         return get_prev_block_sched(target_block) == block;
1029 }
1030
1031 /**
1032  * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
1033  */
1034 static void emit_ia32_Jcc(const ir_node *node)
1035 {
1036         int            need_parity_label = 0;
1037         const ir_node *proj_true;
1038         const ir_node *proj_false;
1039         pn_Cmp         pnc = get_ia32_condcode(node);
1040
1041         pnc = determine_final_pnc(node, 0, pnc);
1042
1043         /* get both Projs */
1044         proj_true = get_proj(node, pn_ia32_Jcc_true);
1045         assert(proj_true && "Jcc without true Proj");
1046
1047         proj_false = get_proj(node, pn_ia32_Jcc_false);
1048         assert(proj_false && "Jcc without false Proj");
1049
1050         if (can_be_fallthrough(proj_true)) {
1051                 /* exchange both proj's so the second one can be omitted */
1052                 const ir_node *t = proj_true;
1053
1054                 proj_true  = proj_false;
1055                 proj_false = t;
1056                 pnc        = ia32_get_negated_pnc(pnc);
1057         }
1058
1059         if (pnc & ia32_pn_Cmp_float) {
1060                 /* Some floating point comparisons require a test of the parity flag,
1061                  * which indicates that the result is unordered */
1062                 switch (pnc & 0x0f) {
1063                 case pn_Cmp_Uo: {
1064                         ia32_emitf(proj_true, "\tjp %L\n");
1065                         break;
1066                 }
1067
1068                 case pn_Cmp_Leg:
1069                         ia32_emitf(proj_true, "\tjnp %L\n");
1070                         break;
1071
1072                 case pn_Cmp_Eq:
1073                 case pn_Cmp_Lt:
1074                 case pn_Cmp_Le:
1075                         /* we need a local label if the false proj is a fallthrough
1076                          * as the falseblock might have no label emitted then */
1077                         if (can_be_fallthrough(proj_false)) {
1078                                 need_parity_label = 1;
1079                                 ia32_emitf(proj_false, "\tjp 1f\n");
1080                         } else {
1081                                 ia32_emitf(proj_false, "\tjp %L\n");
1082                         }
1083                         goto emit_jcc;
1084
1085                 case pn_Cmp_Ug:
1086                 case pn_Cmp_Uge:
1087                 case pn_Cmp_Ne:
1088                         ia32_emitf(proj_true, "\tjp %L\n");
1089                         goto emit_jcc;
1090
1091                 default:
1092                         goto emit_jcc;
1093                 }
1094         } else {
1095 emit_jcc:
1096                 ia32_emitf(proj_true, "\tj%P %L\n", pnc);
1097         }
1098
1099         if (need_parity_label) {
1100                 ia32_emitf(NULL, "1:\n");
1101         }
1102
1103         /* the second Proj might be a fallthrough */
1104         if (can_be_fallthrough(proj_false)) {
1105                 ia32_emitf(proj_false, "\t/* fallthrough to %L */\n");
1106         } else {
1107                 ia32_emitf(proj_false, "\tjmp %L\n");
1108         }
1109 }
1110
1111 /**
1112  * Emits an ia32 Setcc. This is mostly easy but some floating point compares
1113  * are tricky.
1114  */
1115 static void emit_ia32_Setcc(const ir_node *node)
1116 {
1117         const arch_register_t *dreg = get_out_reg(node, pn_ia32_Setcc_res);
1118
1119         pn_Cmp pnc = get_ia32_condcode(node);
1120         pnc        = determine_final_pnc(node, n_ia32_Setcc_eflags, pnc);
1121         if (pnc & ia32_pn_Cmp_float) {
1122                 switch (pnc & 0x0f) {
1123                 case pn_Cmp_Uo:
1124                         ia32_emitf(node, "\tsetp %#R\n", dreg);
1125                         return;
1126
1127                 case pn_Cmp_Leg:
1128                         ia32_emitf(node, "\tsetnp %#R\n", dreg);
1129                         return;
1130
1131                 case pn_Cmp_Eq:
1132                 case pn_Cmp_Lt:
1133                 case pn_Cmp_Le:
1134                         ia32_emitf(node, "\tset%P %<R\n", pnc, dreg);
1135                         ia32_emitf(node, "\tsetnp %>R\n", dreg);
1136                         ia32_emitf(node, "\tandb %>R, %<R\n", dreg, dreg);
1137                         return;
1138
1139                 case pn_Cmp_Ug:
1140                 case pn_Cmp_Uge:
1141                 case pn_Cmp_Ne:
1142                         ia32_emitf(node, "\tset%P %<R\n", pnc, dreg);
1143                         ia32_emitf(node, "\tsetp %>R\n", dreg);
1144                         ia32_emitf(node, "\torb %>R, %<R\n", dreg, dreg);
1145                         return;
1146
1147                 default:
1148                         break;
1149                 }
1150         }
1151         ia32_emitf(node, "\tset%P %#R\n", pnc, dreg);
1152 }
1153
1154 static void emit_ia32_CMovcc(const ir_node *node)
1155 {
1156         const ia32_attr_t     *attr         = get_ia32_attr_const(node);
1157         const arch_register_t *out          = arch_irn_get_register(node, pn_ia32_res);
1158         pn_Cmp                 pnc          = get_ia32_condcode(node);
1159         const arch_register_t *in_true;
1160         const arch_register_t *in_false;
1161
1162         pnc = determine_final_pnc(node, n_ia32_CMovcc_eflags, pnc);
1163         /* although you can't set ins_permuted in the constructor it might still
1164          * be set by memory operand folding
1165          * Permuting inputs of a cmov means the condition is negated!
1166          */
1167         if (attr->data.ins_permuted)
1168                 pnc = ia32_get_negated_pnc(pnc);
1169
1170         in_true  = arch_get_irn_register(get_irn_n(node, n_ia32_CMovcc_val_true));
1171         in_false = arch_get_irn_register(get_irn_n(node, n_ia32_CMovcc_val_false));
1172
1173         /* should be same constraint fullfilled? */
1174         if (out == in_false) {
1175                 /* yes -> nothing to do */
1176         } else if (out == in_true) {
1177                 const arch_register_t *tmp;
1178
1179                 assert(get_ia32_op_type(node) == ia32_Normal);
1180
1181                 pnc = ia32_get_negated_pnc(pnc);
1182
1183                 tmp      = in_true;
1184                 in_true  = in_false;
1185                 in_false = tmp;
1186         } else {
1187                 /* we need a mov */
1188                 ia32_emitf(node, "\tmovl %R, %R\n", in_false, out);
1189         }
1190
1191         /* TODO: handling of Nans isn't correct yet */
1192         if (pnc & ia32_pn_Cmp_float) {
1193                 switch (pnc & 0x0f) {
1194                 case pn_Cmp_Uo:
1195                 case pn_Cmp_Leg:
1196                 case pn_Cmp_Eq:
1197                 case pn_Cmp_Lt:
1198                 case pn_Cmp_Le:
1199                 case pn_Cmp_Ug:
1200                 case pn_Cmp_Uge:
1201                 case pn_Cmp_Ne:
1202                         panic("CMov with floatingpoint compare/parity not supported yet");
1203                 }
1204         }
1205
1206         ia32_emitf(node, "\tcmov%P %#AR, %#R\n", pnc, in_true, out);
1207 }
1208
1209
1210 /* jump table entry (target and corresponding number) */
1211 typedef struct branch_t {
1212         ir_node *target;
1213         int      value;
1214 } branch_t;
1215
1216 /* jump table for switch generation */
1217 typedef struct jmp_tbl_t {
1218         ir_node  *defProj;                 /**< default target */
1219         long      min_value;               /**< smallest switch case */
1220         long      max_value;               /**< largest switch case */
1221         long      num_branches;            /**< number of jumps */
1222         char      label[SNPRINTF_BUF_LEN]; /**< label of the jump table */
1223         branch_t *branches;                /**< jump array */
1224 } jmp_tbl_t;
1225
1226 /**
1227  * Compare two variables of type branch_t. Used to sort all switch cases
1228  */
1229 static int ia32_cmp_branch_t(const void *a, const void *b)
1230 {
1231         branch_t *b1 = (branch_t *)a;
1232         branch_t *b2 = (branch_t *)b;
1233
1234         if (b1->value <= b2->value)
1235                 return -1;
1236         else
1237                 return 1;
1238 }
1239
1240 static void generate_jump_table(jmp_tbl_t *tbl, const ir_node *node)
1241 {
1242         int                 i;
1243         long                pnc;
1244         long                default_pn;
1245         ir_node            *proj;
1246         const ir_edge_t    *edge;
1247
1248         /* fill the table structure */
1249         get_unique_label(tbl->label, SNPRINTF_BUF_LEN, "TBL_");
1250         tbl->defProj      = NULL;
1251         tbl->num_branches = get_irn_n_edges(node) - 1;
1252         tbl->branches     = XMALLOCNZ(branch_t, tbl->num_branches);
1253         tbl->min_value    = LONG_MAX;
1254         tbl->max_value    = LONG_MIN;
1255
1256         default_pn = get_ia32_condcode(node);
1257         i = 0;
1258         /* go over all proj's and collect them */
1259         foreach_out_edge(node, edge) {
1260                 proj = get_edge_src_irn(edge);
1261                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
1262
1263                 pnc = get_Proj_proj(proj);
1264
1265                 /* check for default proj */
1266                 if (pnc == default_pn) {
1267                         assert(tbl->defProj == NULL && "found two default Projs at SwitchJmp");
1268                         tbl->defProj = proj;
1269                 } else {
1270                         tbl->min_value = pnc < tbl->min_value ? pnc : tbl->min_value;
1271                         tbl->max_value = pnc > tbl->max_value ? pnc : tbl->max_value;
1272
1273                         /* create branch entry */
1274                         tbl->branches[i].target = proj;
1275                         tbl->branches[i].value  = pnc;
1276                         ++i;
1277                 }
1278
1279         }
1280         assert(i == tbl->num_branches);
1281
1282         /* sort the branches by their number */
1283         qsort(tbl->branches, tbl->num_branches, sizeof(tbl->branches[0]), ia32_cmp_branch_t);
1284 }
1285
1286 /**
1287  * Emits code for a SwitchJmp (creates a jump table if
1288  * possible otherwise a cmp-jmp cascade). Port from
1289  * cggg ia32 backend
1290  */
1291 static void emit_ia32_SwitchJmp(const ir_node *node)
1292 {
1293         unsigned long       interval;
1294         int                 last_value, i;
1295         jmp_tbl_t           tbl;
1296
1297         /* fill the table structure */
1298         generate_jump_table(&tbl, node);
1299
1300         /* two-complement's magic make this work without overflow */
1301         interval = tbl.max_value - tbl.min_value;
1302
1303         /* emit the table */
1304         ia32_emitf(node,        "\tcmpl $%u, %S0\n", interval);
1305         ia32_emitf(tbl.defProj, "\tja %L\n");
1306
1307         if (tbl.num_branches > 1) {
1308                 /* create table */
1309                 ia32_emitf(node, "\tjmp *%s(,%S0,4)\n", tbl.label);
1310
1311                 be_gas_emit_switch_section(GAS_SECTION_RODATA);
1312                 ia32_emitf(NULL, "\t.align 4\n");
1313                 ia32_emitf(NULL, "%s:\n", tbl.label);
1314
1315                 last_value = tbl.branches[0].value;
1316                 for (i = 0; i != tbl.num_branches; ++i) {
1317                         while (last_value != tbl.branches[i].value) {
1318                                 ia32_emitf(tbl.defProj, ".long %L\n");
1319                                 ++last_value;
1320                         }
1321                         ia32_emitf(tbl.branches[i].target, ".long %L\n");
1322                         ++last_value;
1323                 }
1324                 be_gas_emit_switch_section(GAS_SECTION_TEXT);
1325         } else {
1326                 /* one jump is enough */
1327                 ia32_emitf(tbl.branches[0].target, "\tjmp %L\n");
1328         }
1329
1330         free(tbl.branches);
1331 }
1332
1333 /**
1334  * Emits code for a unconditional jump.
1335  */
1336 static void emit_ia32_Jmp(const ir_node *node)
1337 {
1338         ir_node *block;
1339
1340         /* for now, the code works for scheduled and non-schedules blocks */
1341         block = get_nodes_block(node);
1342
1343         /* we have a block schedule */
1344         if (can_be_fallthrough(node)) {
1345                 ia32_emitf(node, "\t/* fallthrough to %L */\n");
1346         } else {
1347                 ia32_emitf(node, "\tjmp %L\n");
1348         }
1349 }
1350
1351 /**
1352  * Emit an inline assembler operand.
1353  *
1354  * @param node  the ia32_ASM node
1355  * @param s     points to the operand (a %c)
1356  *
1357  * @return  pointer to the first char in s NOT in the current operand
1358  */
1359 static const char* emit_asm_operand(const ir_node *node, const char *s)
1360 {
1361         const ia32_attr_t     *ia32_attr = get_ia32_attr_const(node);
1362         const ia32_asm_attr_t *attr      = CONST_CAST_IA32_ATTR(ia32_asm_attr_t,
1363                                                             ia32_attr);
1364         const arch_register_t *reg;
1365         const ia32_asm_reg_t  *asm_regs = attr->register_map;
1366         const ia32_asm_reg_t  *asm_reg;
1367         const char            *reg_name;
1368         char                   c;
1369         char                   modifier = 0;
1370         int                    num      = -1;
1371         int                    p;
1372
1373         assert(*s == '%');
1374         c = *(++s);
1375
1376         /* parse modifiers */
1377         switch (c) {
1378         case 0:
1379                 ir_fprintf(stderr, "Warning: asm text (%+F) ends with %%\n", node);
1380                 be_emit_char('%');
1381                 return s + 1;
1382         case '%':
1383                 be_emit_char('%');
1384                 return s + 1;
1385         case 'w':
1386         case 'b':
1387         case 'h':
1388                 modifier = c;
1389                 ++s;
1390                 break;
1391         case '0':
1392         case '1':
1393         case '2':
1394         case '3':
1395         case '4':
1396         case '5':
1397         case '6':
1398         case '7':
1399         case '8':
1400         case '9':
1401                 break;
1402         default:
1403                 ir_fprintf(stderr,
1404                                 "Warning: asm text (%+F) contains unknown modifier '%c' for asm op\n",
1405                                 node, c);
1406                 ++s;
1407                 break;
1408         }
1409
1410         /* parse number */
1411         sscanf(s, "%d%n", &num, &p);
1412         if (num < 0) {
1413                 ir_fprintf(stderr, "Warning: Couldn't parse assembler operand (%+F)\n",
1414                            node);
1415                 return s;
1416         } else {
1417                 s += p;
1418         }
1419
1420         if (num < 0 || ARR_LEN(asm_regs) <= num) {
1421                 ir_fprintf(stderr,
1422                                 "Error: Custom assembler references invalid input/output (%+F)\n",
1423                                 node);
1424                 return s;
1425         }
1426         asm_reg = & asm_regs[num];
1427         assert(asm_reg->valid);
1428
1429         /* get register */
1430         if (asm_reg->use_input == 0) {
1431                 reg = get_out_reg(node, asm_reg->inout_pos);
1432         } else {
1433                 ir_node *pred = get_irn_n(node, asm_reg->inout_pos);
1434
1435                 /* might be an immediate value */
1436                 if (is_ia32_Immediate(pred)) {
1437                         emit_ia32_Immediate(pred);
1438                         return s;
1439                 }
1440                 reg = get_in_reg(node, asm_reg->inout_pos);
1441         }
1442         if (reg == NULL) {
1443                 ir_fprintf(stderr,
1444                                 "Warning: no register assigned for %d asm op (%+F)\n",
1445                                 num, node);
1446                 return s;
1447         }
1448
1449         if (asm_reg->memory) {
1450                 be_emit_char('(');
1451         }
1452
1453         /* emit it */
1454         if (modifier != 0) {
1455                 be_emit_char('%');
1456                 switch (modifier) {
1457                 case 'b':
1458                         reg_name = ia32_get_mapped_reg_name(isa->regs_8bit, reg);
1459                         break;
1460                 case 'h':
1461                         reg_name = ia32_get_mapped_reg_name(isa->regs_8bit_high, reg);
1462                         break;
1463                 case 'w':
1464                         reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
1465                         break;
1466                 default:
1467                         panic("Invalid asm op modifier");
1468                 }
1469                 be_emit_string(reg_name);
1470         } else {
1471                 emit_register(reg, asm_reg->mode);
1472         }
1473
1474         if (asm_reg->memory) {
1475                 be_emit_char(')');
1476         }
1477
1478         return s;
1479 }
1480
1481 /**
1482  * Emits code for an ASM pseudo op.
1483  */
1484 static void emit_ia32_Asm(const ir_node *node)
1485 {
1486         const void            *gen_attr = get_irn_generic_attr_const(node);
1487         const ia32_asm_attr_t *attr
1488                 = CONST_CAST_IA32_ATTR(ia32_asm_attr_t, gen_attr);
1489         ident                 *asm_text = attr->asm_text;
1490         const char            *s        = get_id_str(asm_text);
1491
1492         ia32_emitf(node, "#APP\t\n");
1493
1494         if (s[0] != '\t')
1495                 be_emit_char('\t');
1496
1497         while (*s != 0) {
1498                 if (*s == '%') {
1499                         s = emit_asm_operand(node, s);
1500                 } else {
1501                         be_emit_char(*s++);
1502                 }
1503         }
1504
1505         ia32_emitf(NULL, "\n#NO_APP\n");
1506 }
1507
1508
1509 /**
1510  * Emit movsb/w instructions to make mov count divideable by 4
1511  */
1512 static void emit_CopyB_prolog(unsigned size)
1513 {
1514         if (size & 1)
1515                 ia32_emitf(NULL, "\tmovsb\n");
1516         if (size & 2)
1517                 ia32_emitf(NULL, "\tmovsw\n");
1518 }
1519
1520 /**
1521  * Emit rep movsd instruction for memcopy.
1522  */
1523 static void emit_ia32_CopyB(const ir_node *node)
1524 {
1525         unsigned size = get_ia32_copyb_size(node);
1526
1527         emit_CopyB_prolog(size);
1528         ia32_emitf(node, "\trep movsd\n");
1529 }
1530
1531 /**
1532  * Emits unrolled memcopy.
1533  */
1534 static void emit_ia32_CopyB_i(const ir_node *node)
1535 {
1536         unsigned size = get_ia32_copyb_size(node);
1537
1538         emit_CopyB_prolog(size);
1539
1540         size >>= 2;
1541         while (size--) {
1542                 ia32_emitf(NULL, "\tmovsd\n");
1543         }
1544 }
1545
1546
1547 /**
1548  * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1549  */
1550 static void emit_ia32_Conv_with_FP(const ir_node *node, const char* conv_f,
1551                 const char* conv_d)
1552 {
1553         ir_mode            *ls_mode = get_ia32_ls_mode(node);
1554         int                 ls_bits = get_mode_size_bits(ls_mode);
1555         const char         *conv    = ls_bits == 32 ? conv_f : conv_d;
1556
1557         ia32_emitf(node, "\tcvt%s %AS3, %D0\n", conv);
1558 }
1559
1560 static void emit_ia32_Conv_I2FP(const ir_node *node)
1561 {
1562         emit_ia32_Conv_with_FP(node, "si2ss", "si2sd");
1563 }
1564
1565 static void emit_ia32_Conv_FP2I(const ir_node *node)
1566 {
1567         emit_ia32_Conv_with_FP(node, "ss2si", "sd2si");
1568 }
1569
1570 static void emit_ia32_Conv_FP2FP(const ir_node *node)
1571 {
1572         emit_ia32_Conv_with_FP(node, "sd2ss", "ss2sd");
1573 }
1574
1575 /**
1576  * Emits code for an Int conversion.
1577  */
1578 static void emit_ia32_Conv_I2I(const ir_node *node)
1579 {
1580         ir_mode *smaller_mode = get_ia32_ls_mode(node);
1581         int      signed_mode  = mode_is_signed(smaller_mode);
1582         const char *sign_suffix;
1583
1584         assert(!mode_is_float(smaller_mode));
1585
1586         sign_suffix = signed_mode ? "s" : "z";
1587         ia32_emitf(node, "\tmov%s%Ml %#AS3, %D0\n", sign_suffix);
1588 }
1589
1590 /**
1591  * Emits a call
1592  */
1593 static void emit_ia32_Call(const ir_node *node)
1594 {
1595         /* Special case: Call must not have its immediates prefixed by $, instead
1596          * address mode is prefixed by *. */
1597         ia32_emitf(node, "\tcall %*AS3\n");
1598 }
1599
1600
1601 /**
1602  * Emits code to increase stack pointer.
1603  */
1604 static void emit_be_IncSP(const ir_node *node)
1605 {
1606         int offs = be_get_IncSP_offset(node);
1607
1608         if (offs == 0)
1609                 return;
1610
1611         if (offs > 0) {
1612                 ia32_emitf(node, "\tsubl $%u, %D0\n", offs);
1613         } else {
1614                 ia32_emitf(node, "\taddl $%u, %D0\n", -offs);
1615         }
1616 }
1617
1618 /**
1619  * Emits code for Copy/CopyKeep.
1620  */
1621 static void Copy_emitter(const ir_node *node, const ir_node *op)
1622 {
1623         const arch_register_t *in  = arch_get_irn_register(op);
1624         const arch_register_t *out = arch_get_irn_register(node);
1625
1626         if (in == out) {
1627                 return;
1628         }
1629         /* copies of vf nodes aren't real... */
1630         if (arch_register_get_class(in) == &ia32_reg_classes[CLASS_ia32_vfp])
1631                 return;
1632
1633         if (get_irn_mode(node) == mode_E) {
1634                 ia32_emitf(node, "\tmovsd %R, %R\n", in, out);
1635         } else {
1636                 ia32_emitf(node, "\tmovl %R, %R\n", in, out);
1637         }
1638 }
1639
1640 static void emit_be_Copy(const ir_node *node)
1641 {
1642         Copy_emitter(node, be_get_Copy_op(node));
1643 }
1644
1645 static void emit_be_CopyKeep(const ir_node *node)
1646 {
1647         Copy_emitter(node, be_get_CopyKeep_op(node));
1648 }
1649
1650 /**
1651  * Emits code for exchange.
1652  */
1653 static void emit_be_Perm(const ir_node *node)
1654 {
1655         const arch_register_t *in0, *in1;
1656         const arch_register_class_t *cls0, *cls1;
1657
1658         in0 = arch_get_irn_register(get_irn_n(node, 0));
1659         in1 = arch_get_irn_register(get_irn_n(node, 1));
1660
1661         cls0 = arch_register_get_class(in0);
1662         cls1 = arch_register_get_class(in1);
1663
1664         assert(cls0 == cls1 && "Register class mismatch at Perm");
1665
1666         if (cls0 == &ia32_reg_classes[CLASS_ia32_gp]) {
1667                 ia32_emitf(node, "\txchg %R, %R\n", in1, in0);
1668         } else if (cls0 == &ia32_reg_classes[CLASS_ia32_xmm]) {
1669                 ia32_emitf(NULL, "\txorpd %R, %R\n", in1, in0);
1670                 ia32_emitf(NULL, "\txorpd %R, %R\n", in0, in1);
1671                 ia32_emitf(node, "\txorpd %R, %R\n", in1, in0);
1672         } else if (cls0 == &ia32_reg_classes[CLASS_ia32_vfp]) {
1673                 /* is a NOP */
1674         } else if (cls0 == &ia32_reg_classes[CLASS_ia32_st]) {
1675                 /* is a NOP */
1676         } else {
1677                 panic("unexpected register class in be_Perm (%+F)", node);
1678         }
1679 }
1680
1681 /**
1682  * Emits code for Constant loading.
1683  */
1684 static void emit_ia32_Const(const ir_node *node)
1685 {
1686         ia32_emitf(node, "\tmovl %I, %D0\n");
1687 }
1688
1689 /**
1690  * Emits code to load the TLS base
1691  */
1692 static void emit_ia32_LdTls(const ir_node *node)
1693 {
1694         ia32_emitf(node, "\tmovl %%gs:0, %D0\n");
1695 }
1696
1697 /* helper function for emit_ia32_Minus64Bit */
1698 static void emit_mov(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1699 {
1700         ia32_emitf(node, "\tmovl %R, %R\n", src, dst);
1701 }
1702
1703 /* helper function for emit_ia32_Minus64Bit */
1704 static void emit_neg(const ir_node* node, const arch_register_t *reg)
1705 {
1706         ia32_emitf(node, "\tnegl %R\n", reg);
1707 }
1708
1709 /* helper function for emit_ia32_Minus64Bit */
1710 static void emit_sbb0(const ir_node* node, const arch_register_t *reg)
1711 {
1712         ia32_emitf(node, "\tsbbl $0, %R\n", reg);
1713 }
1714
1715 /* helper function for emit_ia32_Minus64Bit */
1716 static void emit_sbb(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1717 {
1718         ia32_emitf(node, "\tsbbl %R, %R\n", src, dst);
1719 }
1720
1721 /* helper function for emit_ia32_Minus64Bit */
1722 static void emit_xchg(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1723 {
1724         ia32_emitf(node, "\txchgl %R, %R\n", src, dst);
1725 }
1726
1727 /* helper function for emit_ia32_Minus64Bit */
1728 static void emit_zero(const ir_node* node, const arch_register_t *reg)
1729 {
1730         ia32_emitf(node, "\txorl %R, %R\n", reg, reg);
1731 }
1732
1733 static void emit_ia32_Minus64Bit(const ir_node *node)
1734 {
1735         const arch_register_t *in_lo  = get_in_reg(node, 0);
1736         const arch_register_t *in_hi  = get_in_reg(node, 1);
1737         const arch_register_t *out_lo = get_out_reg(node, 0);
1738         const arch_register_t *out_hi = get_out_reg(node, 1);
1739
1740         if (out_lo == in_lo) {
1741                 if (out_hi != in_hi) {
1742                         /* a -> a, b -> d */
1743                         goto zero_neg;
1744                 } else {
1745                         /* a -> a, b -> b */
1746                         goto normal_neg;
1747                 }
1748         } else if (out_lo == in_hi) {
1749                 if (out_hi == in_lo) {
1750                         /* a -> b, b -> a */
1751                         emit_xchg(node, in_lo, in_hi);
1752                         goto normal_neg;
1753                 } else {
1754                         /* a -> b, b -> d */
1755                         emit_mov(node, in_hi, out_hi);
1756                         emit_mov(node, in_lo, out_lo);
1757                         goto normal_neg;
1758                 }
1759         } else {
1760                 if (out_hi == in_lo) {
1761                         /* a -> c, b -> a */
1762                         emit_mov(node, in_lo, out_lo);
1763                         goto zero_neg;
1764                 } else if (out_hi == in_hi) {
1765                         /* a -> c, b -> b */
1766                         emit_mov(node, in_lo, out_lo);
1767                         goto normal_neg;
1768                 } else {
1769                         /* a -> c, b -> d */
1770                         emit_mov(node, in_lo, out_lo);
1771                         goto zero_neg;
1772                 }
1773         }
1774
1775 normal_neg:
1776         emit_neg( node, out_hi);
1777         emit_neg( node, out_lo);
1778         emit_sbb0(node, out_hi);
1779         return;
1780
1781 zero_neg:
1782         emit_zero(node, out_hi);
1783         emit_neg( node, out_lo);
1784         emit_sbb( node, in_hi, out_hi);
1785 }
1786
1787 static void emit_ia32_GetEIP(const ir_node *node)
1788 {
1789         ia32_emitf(node, "\tcall %s\n", pic_base_label);
1790         ia32_emitf(NULL, "%s:\n", pic_base_label);
1791         ia32_emitf(node, "\tpopl %D0\n");
1792 }
1793
1794 static void emit_ia32_ClimbFrame(const ir_node *node)
1795 {
1796         const ia32_climbframe_attr_t *attr = get_ia32_climbframe_attr_const(node);
1797
1798         ia32_emitf(node, "\tmovl %S0, %D0\n");
1799         ia32_emitf(node, "\tmovl $%u, %S1\n", attr->count);
1800         be_gas_emit_block_name(node);
1801         be_emit_cstring(":\n");
1802         be_emit_write_line();
1803         ia32_emitf(node, "\tmovl (%D0), %D0\n");
1804         ia32_emitf(node, "\tdec %S1\n");
1805         be_emit_cstring("\tjnz ");
1806         be_gas_emit_block_name(node);
1807         be_emit_finish_line_gas(node);
1808 }
1809
1810 static void emit_be_Return(const ir_node *node)
1811 {
1812         unsigned pop = be_Return_get_pop(node);
1813
1814         if (pop > 0 || be_Return_get_emit_pop(node)) {
1815                 ia32_emitf(node, "\tret $%u\n", pop);
1816         } else {
1817                 ia32_emitf(node, "\tret\n");
1818         }
1819 }
1820
1821 static void emit_Nothing(const ir_node *node)
1822 {
1823         (void) node;
1824 }
1825
1826
1827 /**
1828  * Enters the emitter functions for handled nodes into the generic
1829  * pointer of an opcode.
1830  */
1831 static void ia32_register_emitters(void)
1832 {
1833 #define IA32_EMIT2(a,b) op_ia32_##a->ops.generic = (op_func)emit_ia32_##b
1834 #define IA32_EMIT(a)    IA32_EMIT2(a,a)
1835 #define EMIT(a)         op_##a->ops.generic = (op_func)emit_##a
1836 #define IGN(a)          op_##a->ops.generic = (op_func)emit_Nothing
1837 #define BE_EMIT(a)      op_be_##a->ops.generic = (op_func)emit_be_##a
1838 #define BE_IGN(a)       op_be_##a->ops.generic = (op_func)emit_Nothing
1839
1840         /* first clear the generic function pointer for all ops */
1841         clear_irp_opcodes_generic_func();
1842
1843         /* register all emitter functions defined in spec */
1844         ia32_register_spec_emitters();
1845
1846         /* other ia32 emitter functions */
1847         IA32_EMIT2(Conv_I2I8Bit, Conv_I2I);
1848         IA32_EMIT(Asm);
1849         IA32_EMIT(CMovcc);
1850         IA32_EMIT(Call);
1851         IA32_EMIT(Const);
1852         IA32_EMIT(Conv_FP2FP);
1853         IA32_EMIT(Conv_FP2I);
1854         IA32_EMIT(Conv_I2FP);
1855         IA32_EMIT(Conv_I2I);
1856         IA32_EMIT(CopyB);
1857         IA32_EMIT(CopyB_i);
1858         IA32_EMIT(GetEIP);
1859         IA32_EMIT(IMul);
1860         IA32_EMIT(Jcc);
1861         IA32_EMIT(Setcc);
1862         IA32_EMIT(LdTls);
1863         IA32_EMIT(Minus64Bit);
1864         IA32_EMIT(SwitchJmp);
1865         IA32_EMIT(ClimbFrame);
1866         IA32_EMIT(Jmp);
1867
1868         /* benode emitter */
1869         BE_EMIT(Copy);
1870         BE_EMIT(CopyKeep);
1871         BE_EMIT(IncSP);
1872         BE_EMIT(Perm);
1873         BE_EMIT(Return);
1874
1875         BE_IGN(Barrier);
1876         BE_IGN(Keep);
1877         BE_IGN(Start);
1878
1879         /* firm emitter */
1880         IGN(Phi);
1881
1882 #undef BE_EMIT
1883 #undef EMIT
1884 #undef IGN
1885 #undef IA32_EMIT2
1886 #undef IA32_EMIT
1887 }
1888
1889 typedef void (*emit_func_ptr) (const ir_node *);
1890
1891 /**
1892  * Assign and emit an exception label if the current instruction can fail.
1893  */
1894 static void ia32_assign_exc_label(ir_node *node)
1895 {
1896         /* assign a new ID to the instruction */
1897         set_ia32_exc_label_id(node, ++exc_label_id);
1898         /* print it */
1899         ia32_emit_exc_label(node);
1900         be_emit_char(':');
1901         be_emit_pad_comment();
1902         be_emit_cstring("/* exception to Block ");
1903         ia32_emit_cfop_target(node);
1904         be_emit_cstring(" */\n");
1905         be_emit_write_line();
1906 }
1907
1908 /**
1909  * Emits code for a node.
1910  */
1911 static void ia32_emit_node(ir_node *node)
1912 {
1913         ir_op *op = get_irn_op(node);
1914
1915         DBG((dbg, LEVEL_1, "emitting code for %+F\n", node));
1916
1917         if (is_ia32_irn(node)) {
1918                 if (get_ia32_exc_label(node)) {
1919                         /* emit the exception label of this instruction */
1920                         ia32_assign_exc_label(node);
1921                 }
1922                 if (mark_spill_reload) {
1923                         if (is_ia32_is_spill(node)) {
1924                                 ia32_emitf(NULL, "\txchg %ebx, %ebx        /* spill mark */\n");
1925                         }
1926                         if (is_ia32_is_reload(node)) {
1927                                 ia32_emitf(NULL, "\txchg %edx, %edx        /* reload mark */\n");
1928                         }
1929                         if (is_ia32_is_remat(node)) {
1930                                 ia32_emitf(NULL, "\txchg %ecx, %ecx        /* remat mark */\n");
1931                         }
1932                 }
1933         }
1934         if (op->ops.generic) {
1935                 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
1936
1937                 be_dbg_set_dbg_info(get_irn_dbg_info(node));
1938
1939                 (*func) (node);
1940         } else {
1941                 emit_Nothing(node);
1942                 ir_fprintf(stderr, "Error: No emit handler for node %+F (%+G, graph %+F)\n", node, node, current_ir_graph);
1943                 abort();
1944         }
1945 }
1946
1947 /**
1948  * Emits gas alignment directives
1949  */
1950 static void ia32_emit_alignment(unsigned align, unsigned skip)
1951 {
1952         ia32_emitf(NULL, "\t.p2align %u,,%u\n", align, skip);
1953 }
1954
1955 /**
1956  * Emits gas alignment directives for Labels depended on cpu architecture.
1957  */
1958 static void ia32_emit_align_label(void)
1959 {
1960         unsigned align        = ia32_cg_config.label_alignment;
1961         unsigned maximum_skip = ia32_cg_config.label_alignment_max_skip;
1962         ia32_emit_alignment(align, maximum_skip);
1963 }
1964
1965 /**
1966  * Test whether a block should be aligned.
1967  * For cpus in the P4/Athlon class it is useful to align jump labels to
1968  * 16 bytes. However we should only do that if the alignment nops before the
1969  * label aren't executed more often than we have jumps to the label.
1970  */
1971 static int should_align_block(const ir_node *block)
1972 {
1973         static const double DELTA = .0001;
1974         ir_graph     *irg         = get_irn_irg(block);
1975         ir_exec_freq *exec_freq   = be_get_irg_exec_freq(irg);
1976         ir_node      *prev        = get_prev_block_sched(block);
1977         double        block_freq;
1978         double        prev_freq = 0;  /**< execfreq of the fallthrough block */
1979         double        jmp_freq  = 0;  /**< execfreq of all non-fallthrough blocks */
1980         int           i, n_cfgpreds;
1981
1982         if (exec_freq == NULL)
1983                 return 0;
1984         if (ia32_cg_config.label_alignment_factor <= 0)
1985                 return 0;
1986
1987         block_freq = get_block_execfreq(exec_freq, block);
1988         if (block_freq < DELTA)
1989                 return 0;
1990
1991         n_cfgpreds = get_Block_n_cfgpreds(block);
1992         for (i = 0; i < n_cfgpreds; ++i) {
1993                 const ir_node *pred      = get_Block_cfgpred_block(block, i);
1994                 double         pred_freq = get_block_execfreq(exec_freq, pred);
1995
1996                 if (pred == prev) {
1997                         prev_freq += pred_freq;
1998                 } else {
1999                         jmp_freq  += pred_freq;
2000                 }
2001         }
2002
2003         if (prev_freq < DELTA && !(jmp_freq < DELTA))
2004                 return 1;
2005
2006         jmp_freq /= prev_freq;
2007
2008         return jmp_freq > ia32_cg_config.label_alignment_factor;
2009 }
2010
2011 /**
2012  * Emit the block header for a block.
2013  *
2014  * @param block       the block
2015  * @param prev_block  the previous block
2016  */
2017 static void ia32_emit_block_header(ir_node *block)
2018 {
2019         ir_graph     *irg = current_ir_graph;
2020         int           need_label = block_needs_label(block);
2021         int           i, arity;
2022         ir_exec_freq *exec_freq = be_get_irg_exec_freq(irg);
2023
2024         if (block == get_irg_end_block(irg))
2025                 return;
2026
2027         if (ia32_cg_config.label_alignment > 0) {
2028                 /* align the current block if:
2029                  * a) if should be aligned due to its execution frequency
2030                  * b) there is no fall-through here
2031                  */
2032                 if (should_align_block(block)) {
2033                         ia32_emit_align_label();
2034                 } else {
2035                         /* if the predecessor block has no fall-through,
2036                            we can always align the label. */
2037                         int i;
2038                         int has_fallthrough = 0;
2039
2040                         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
2041                                 ir_node *cfg_pred = get_Block_cfgpred(block, i);
2042                                 if (can_be_fallthrough(cfg_pred)) {
2043                                         has_fallthrough = 1;
2044                                         break;
2045                                 }
2046                         }
2047
2048                         if (!has_fallthrough)
2049                                 ia32_emit_align_label();
2050                 }
2051         }
2052
2053         if (need_label) {
2054                 be_gas_emit_block_name(block);
2055                 be_emit_char(':');
2056
2057                 be_emit_pad_comment();
2058                 be_emit_cstring("   /* ");
2059         } else {
2060                 be_emit_cstring("\t/* ");
2061                 be_gas_emit_block_name(block);
2062                 be_emit_cstring(": ");
2063         }
2064
2065         be_emit_cstring("preds:");
2066
2067         /* emit list of pred blocks in comment */
2068         arity = get_irn_arity(block);
2069         if (arity <= 0) {
2070                 be_emit_cstring(" none");
2071         } else {
2072                 for (i = 0; i < arity; ++i) {
2073                         ir_node *predblock = get_Block_cfgpred_block(block, i);
2074                         be_emit_irprintf(" %d", get_irn_node_nr(predblock));
2075                 }
2076         }
2077         if (exec_freq != NULL) {
2078                 be_emit_irprintf(", freq: %f",
2079                                  get_block_execfreq(exec_freq, block));
2080         }
2081         be_emit_cstring(" */\n");
2082         be_emit_write_line();
2083 }
2084
2085 /**
2086  * Walks over the nodes in a block connected by scheduling edges
2087  * and emits code for each node.
2088  */
2089 static void ia32_gen_block(ir_node *block)
2090 {
2091         ir_node *node;
2092
2093         ia32_emit_block_header(block);
2094
2095         /* emit the contents of the block */
2096         be_dbg_set_dbg_info(get_irn_dbg_info(block));
2097         sched_foreach(block, node) {
2098                 ia32_emit_node(node);
2099         }
2100 }
2101
2102 typedef struct exc_entry {
2103         ir_node *exc_instr;  /** The instruction that can issue an exception. */
2104         ir_node *block;      /** The block to call then. */
2105 } exc_entry;
2106
2107 /**
2108  * Block-walker:
2109  * Sets labels for control flow nodes (jump target).
2110  * Links control predecessors to there destination blocks.
2111  */
2112 static void ia32_gen_labels(ir_node *block, void *data)
2113 {
2114         exc_entry **exc_list = data;
2115         ir_node *pred;
2116         int     n;
2117
2118         for (n = get_Block_n_cfgpreds(block) - 1; n >= 0; --n) {
2119                 pred = get_Block_cfgpred(block, n);
2120                 set_irn_link(pred, block);
2121
2122                 pred = skip_Proj(pred);
2123                 if (is_ia32_irn(pred) && get_ia32_exc_label(pred)) {
2124                         exc_entry e;
2125
2126                         e.exc_instr = pred;
2127                         e.block     = block;
2128                         ARR_APP1(exc_entry, *exc_list, e);
2129                         set_irn_link(pred, block);
2130                 }
2131         }
2132 }
2133
2134 /**
2135  * Compare two exception_entries.
2136  */
2137 static int cmp_exc_entry(const void *a, const void *b)
2138 {
2139         const exc_entry *ea = a;
2140         const exc_entry *eb = b;
2141
2142         if (get_ia32_exc_label_id(ea->exc_instr) < get_ia32_exc_label_id(eb->exc_instr))
2143                 return -1;
2144         return +1;
2145 }
2146
2147 /**
2148  * Main driver. Emits the code for one routine.
2149  */
2150 void ia32_gen_routine(ir_graph *irg)
2151 {
2152         ir_entity        *entity    = get_irg_entity(irg);
2153         exc_entry        *exc_list  = NEW_ARR_F(exc_entry, 0);
2154         const arch_env_t *arch_env  = be_get_irg_arch_env(irg);
2155         ia32_irg_data_t  *irg_data  = ia32_get_irg_data(irg);
2156         ir_node         **blk_sched = irg_data->blk_sched;
2157         int i, n;
2158
2159         isa      = (ia32_isa_t*) arch_env;
2160         do_pic   = be_get_irg_options(irg)->pic;
2161
2162         be_gas_elf_type_char = '@';
2163
2164         ia32_register_emitters();
2165
2166         get_unique_label(pic_base_label, sizeof(pic_base_label), "PIC_BASE");
2167
2168         be_dbg_method_begin(entity);
2169         be_gas_emit_function_prolog(entity, ia32_cg_config.function_alignment);
2170
2171         /* we use links to point to target blocks */
2172         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
2173         irg_block_walk_graph(irg, ia32_gen_labels, NULL, &exc_list);
2174
2175         /* initialize next block links */
2176         n = ARR_LEN(blk_sched);
2177         for (i = 0; i < n; ++i) {
2178                 ir_node *block = blk_sched[i];
2179                 ir_node *prev  = i > 0 ? blk_sched[i-1] : NULL;
2180
2181                 set_irn_link(block, prev);
2182         }
2183
2184         for (i = 0; i < n; ++i) {
2185                 ir_node *block = blk_sched[i];
2186
2187                 ia32_gen_block(block);
2188         }
2189
2190         be_gas_emit_function_epilog(entity);
2191         be_dbg_method_end();
2192         be_emit_char('\n');
2193         be_emit_write_line();
2194
2195         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
2196
2197         /* Sort the exception table using the exception label id's.
2198            Those are ascending with ascending addresses. */
2199         qsort(exc_list, ARR_LEN(exc_list), sizeof(exc_list[0]), cmp_exc_entry);
2200         {
2201                 int i;
2202
2203                 for (i = 0; i < ARR_LEN(exc_list); ++i) {
2204                         be_emit_cstring("\t.long ");
2205                         ia32_emit_exc_label(exc_list[i].exc_instr);
2206                         be_emit_char('\n');
2207                         be_emit_cstring("\t.long ");
2208                         be_gas_emit_block_name(exc_list[i].block);
2209                         be_emit_char('\n');
2210                 }
2211         }
2212         DEL_ARR_F(exc_list);
2213 }
2214
2215 static const lc_opt_table_entry_t ia32_emitter_options[] = {
2216         LC_OPT_ENT_BOOL("mark_spill_reload",   "mark spills and reloads with ud opcodes", &mark_spill_reload),
2217         LC_OPT_LAST
2218 };
2219
2220 /* ==== Experimental binary emitter ==== */
2221
2222 static unsigned char reg_gp_map[N_ia32_gp_REGS];
2223 //static unsigned char reg_mmx_map[N_ia32_mmx_REGS];
2224 //static unsigned char reg_sse_map[N_ia32_xmm_REGS];
2225 static unsigned char pnc_map_signed[8];
2226 static unsigned char pnc_map_unsigned[8];
2227
2228 static void build_reg_map(void)
2229 {
2230         reg_gp_map[REG_GP_EAX] = 0x0;
2231         reg_gp_map[REG_GP_ECX] = 0x1;
2232         reg_gp_map[REG_GP_EDX] = 0x2;
2233         reg_gp_map[REG_GP_EBX] = 0x3;
2234         reg_gp_map[REG_GP_ESP] = 0x4;
2235         reg_gp_map[REG_GP_EBP] = 0x5;
2236         reg_gp_map[REG_GP_ESI] = 0x6;
2237         reg_gp_map[REG_GP_EDI] = 0x7;
2238
2239         pnc_map_signed[pn_Cmp_Eq]    = 0x04;
2240         pnc_map_signed[pn_Cmp_Lt]    = 0x0C;
2241         pnc_map_signed[pn_Cmp_Le]    = 0x0E;
2242         pnc_map_signed[pn_Cmp_Gt]    = 0x0F;
2243         pnc_map_signed[pn_Cmp_Ge]    = 0x0D;
2244         pnc_map_signed[pn_Cmp_Lg]    = 0x05;
2245
2246         pnc_map_unsigned[pn_Cmp_Eq]    = 0x04;
2247         pnc_map_unsigned[pn_Cmp_Lt]    = 0x02;
2248         pnc_map_unsigned[pn_Cmp_Le]    = 0x06;
2249         pnc_map_unsigned[pn_Cmp_Gt]    = 0x07;
2250         pnc_map_unsigned[pn_Cmp_Ge]    = 0x03;
2251         pnc_map_unsigned[pn_Cmp_Lg]    = 0x05;
2252 }
2253
2254 /** Returns the encoding for a pnc field. */
2255 static unsigned char pnc2cc(int pnc)
2256 {
2257         unsigned char cc;
2258         if (pnc == ia32_pn_Cmp_parity) {
2259                 cc = 0x0A;
2260         } else if (pnc & ia32_pn_Cmp_float || pnc & ia32_pn_Cmp_unsigned) {
2261                 cc = pnc_map_unsigned[pnc & 0x07];
2262         } else {
2263                 cc = pnc_map_signed[pnc & 0x07];
2264         }
2265         assert(cc != 0);
2266         return cc;
2267 }
2268
2269 /** Sign extension bit values for binops */
2270 enum SignExt {
2271         UNSIGNED_IMM = 0,  /**< unsigned immediate */
2272         SIGNEXT_IMM  = 2,  /**< sign extended immediate */
2273 };
2274
2275 /** The mod encoding of the ModR/M */
2276 enum Mod {
2277         MOD_IND          = 0x00, /**< [reg1] */
2278         MOD_IND_BYTE_OFS = 0x40, /**< [reg1 + byte ofs] */
2279         MOD_IND_WORD_OFS = 0x80, /**< [reg1 + word ofs] */
2280         MOD_REG          = 0xC0  /**< reg1 */
2281 };
2282
2283 /** create R/M encoding for ModR/M */
2284 #define ENC_RM(x) (x)
2285 /** create REG encoding for ModR/M */
2286 #define ENC_REG(x) ((x) << 3)
2287
2288 /** create encoding for a SIB byte */
2289 #define ENC_SIB(scale, index, base) ((scale) << 6 | (index) << 3 | (base))
2290
2291 /* Node: The following routines are supposed to append bytes, words, dwords
2292    to the output stream.
2293    Currently the implementation is stupid in that it still creates output
2294    for an "assembler" in the form of .byte, .long
2295    We will change this when enough infrastructure is there to create complete
2296    machine code in memory/object files */
2297
2298 static void bemit8(const unsigned char byte)
2299 {
2300         be_emit_irprintf("\t.byte 0x%x\n", byte);
2301         be_emit_write_line();
2302 }
2303
2304 static void bemit16(const unsigned short u16)
2305 {
2306         be_emit_irprintf("\t.word 0x%x\n", u16);
2307         be_emit_write_line();
2308 }
2309
2310 static void bemit32(const unsigned u32)
2311 {
2312         be_emit_irprintf("\t.long 0x%x\n", u32);
2313         be_emit_write_line();
2314 }
2315
2316 /**
2317  * Emit address of an entity. If @p is_relative is true then a relative
2318  * offset from behind the address to the entity is created.
2319  */
2320 static void bemit_entity(ir_entity *entity, bool entity_sign, int offset,
2321                          bool is_relative)
2322 {
2323         if (entity == NULL) {
2324                 bemit32(offset);
2325                 return;
2326         }
2327
2328         /* the final version should remember the position in the bytestream
2329            and patch it with the correct address at linktime... */
2330         be_emit_cstring("\t.long ");
2331         if (entity_sign)
2332                 be_emit_char('-');
2333         be_gas_emit_entity(entity);
2334
2335         if (get_entity_owner(entity) == get_tls_type()) {
2336                 if (get_entity_visibility(entity) == ir_visibility_external) {
2337                         be_emit_cstring("@INDNTPOFF");
2338                 } else {
2339                         be_emit_cstring("@NTPOFF");
2340                 }
2341         }
2342
2343         if (is_relative) {
2344                 be_emit_cstring("-.");
2345                 offset -= 4;
2346         }
2347
2348         if (offset != 0) {
2349                 be_emit_irprintf("%+d", offset);
2350         }
2351         be_emit_char('\n');
2352         be_emit_write_line();
2353 }
2354
2355 static void bemit_jmp_destination(const ir_node *dest_block)
2356 {
2357         be_emit_cstring("\t.long ");
2358         be_gas_emit_block_name(dest_block);
2359         be_emit_cstring(" - . - 4\n");
2360         be_emit_write_line();
2361 }
2362
2363 /* end emit routines, all emitters following here should only use the functions
2364    above. */
2365
2366 typedef enum reg_modifier {
2367         REG_LOW  = 0,
2368         REG_HIGH = 1
2369 } reg_modifier_t;
2370
2371 /** Create a ModR/M byte for src1,src2 registers */
2372 static void bemit_modrr(const arch_register_t *src1,
2373                         const arch_register_t *src2)
2374 {
2375         unsigned char modrm = MOD_REG;
2376         modrm |= ENC_RM(reg_gp_map[src1->index]);
2377         modrm |= ENC_REG(reg_gp_map[src2->index]);
2378         bemit8(modrm);
2379 }
2380
2381 /** Create a ModR/M8 byte for src1,src2 registers */
2382 static void bemit_modrr8(reg_modifier_t high_part1, const arch_register_t *src1,
2383                                                  reg_modifier_t high_part2, const arch_register_t *src2)
2384 {
2385         unsigned char modrm = MOD_REG;
2386         modrm |= ENC_RM(reg_gp_map[src1->index] +  (high_part1 == REG_HIGH ? 4 : 0));
2387         modrm |= ENC_REG(reg_gp_map[src2->index] + (high_part2 == REG_HIGH ? 4 : 0));
2388         bemit8(modrm);
2389 }
2390
2391 /** Create a ModR/M byte for one register and extension */
2392 static void bemit_modru(const arch_register_t *reg, unsigned ext)
2393 {
2394         unsigned char modrm = MOD_REG;
2395         assert(ext <= 7);
2396         modrm |= ENC_RM(reg_gp_map[reg->index]);
2397         modrm |= ENC_REG(ext);
2398         bemit8(modrm);
2399 }
2400
2401 /** Create a ModR/M8 byte for one register */
2402 static void bemit_modrm8(reg_modifier_t high_part, const arch_register_t *reg)
2403 {
2404         unsigned char modrm = MOD_REG;
2405         assert(reg_gp_map[reg->index] < 4);
2406         modrm |= ENC_RM(reg_gp_map[reg->index] + (high_part == REG_HIGH ? 4 : 0));
2407         modrm |= MOD_REG;
2408         bemit8(modrm);
2409 }
2410
2411 /**
2412  * Calculate the size of an signed immediate in bytes.
2413  *
2414  * @param offset  an offset
2415  */
2416 static unsigned get_signed_imm_size(int offset)
2417 {
2418         if (-128 <= offset && offset < 128) {
2419                 return 1;
2420         } else if (-32768 <= offset && offset < 32768) {
2421                 return 2;
2422         } else {
2423                 return 4;
2424         }
2425 }
2426
2427 /**
2428  * Emit an address mode.
2429  *
2430  * @param reg   content of the reg field: either a register index or an opcode extension
2431  * @param node  the node
2432  */
2433 static void bemit_mod_am(unsigned reg, const ir_node *node)
2434 {
2435         ir_entity *ent       = get_ia32_am_sc(node);
2436         int        offs      = get_ia32_am_offs_int(node);
2437         ir_node   *base      = get_irn_n(node, n_ia32_base);
2438         int        has_base  = !is_ia32_NoReg_GP(base);
2439         ir_node   *index     = get_irn_n(node, n_ia32_index);
2440         int        has_index = !is_ia32_NoReg_GP(index);
2441         unsigned   modrm     = 0;
2442         unsigned   sib       = 0;
2443         unsigned   emitoffs  = 0;
2444         bool       emitsib   = false;
2445         unsigned   base_enc;
2446
2447         /* set the mod part depending on displacement */
2448         if (ent != NULL) {
2449                 modrm |= MOD_IND_WORD_OFS;
2450                 emitoffs = 32;
2451         } else if (offs == 0) {
2452                 modrm |= MOD_IND;
2453                 emitoffs = 0;
2454         } else if (-128 <= offs && offs < 128) {
2455                 modrm |= MOD_IND_BYTE_OFS;
2456                 emitoffs = 8;
2457         } else {
2458                 modrm |= MOD_IND_WORD_OFS;
2459                 emitoffs = 32;
2460         }
2461
2462         if (has_base) {
2463                 const arch_register_t *base_reg = arch_get_irn_register(base);
2464                 base_enc = reg_gp_map[base_reg->index];
2465         } else {
2466                 /* Use the EBP encoding + MOD_IND if NO base register. There is
2467                  * always a 32bit offset present in this case. */
2468                 modrm    = MOD_IND;
2469                 base_enc = 0x05;
2470                 emitoffs = 32;
2471         }
2472
2473         /* Determine if we need a SIB byte. */
2474         if (has_index) {
2475                 const arch_register_t *reg_index = arch_get_irn_register(index);
2476                 int                    scale     = get_ia32_am_scale(node);
2477                 assert(scale < 4);
2478                 /* R/M set to ESP means SIB in 32bit mode. */
2479                 modrm   |= ENC_RM(0x04);
2480                 sib      = ENC_SIB(scale, reg_gp_map[reg_index->index], base_enc);
2481                 emitsib = true;
2482         } else if (base_enc == 0x04) {
2483                 /* for the above reason we are forced to emit a SIB when base is ESP.
2484                  * Only the base is used, index must be ESP too, which means no index.
2485                  */
2486                 modrm   |= ENC_RM(0x04);
2487                 sib      = ENC_SIB(0, 0x04, 0x04);
2488                 emitsib  = true;
2489         } else {
2490                 modrm |= ENC_RM(base_enc);
2491         }
2492
2493         /* We are forced to emit an 8bit offset as EBP base without offset is a
2494          * special case for SIB without base register. */
2495         if (base_enc == 0x05 && emitoffs == 0) {
2496                 modrm    |= MOD_IND_BYTE_OFS;
2497                 emitoffs  = 8;
2498         }
2499
2500         modrm |= ENC_REG(reg);
2501
2502         bemit8(modrm);
2503         if (emitsib)
2504                 bemit8(sib);
2505
2506         /* emit displacement */
2507         if (emitoffs == 8) {
2508                 bemit8((unsigned) offs);
2509         } else if (emitoffs == 32) {
2510                 bemit_entity(ent, is_ia32_am_sc_sign(node), offs, false);
2511         }
2512 }
2513
2514 /**
2515  * Emit a binop with a immediate operand.
2516  *
2517  * @param node        the node to emit
2518  * @param opcode_eax  the opcode for the op eax, imm variant
2519  * @param opcode      the opcode for the reg, imm variant
2520  * @param ruval       the opcode extension for opcode
2521  */
2522 static void bemit_binop_with_imm(
2523         const ir_node *node,
2524         unsigned char opcode_ax,
2525         unsigned char opcode, unsigned char ruval)
2526 {
2527         /* Use in-reg, because some instructions (cmp, test) have no out-reg. */
2528         const ir_node               *op   = get_irn_n(node, n_ia32_binary_right);
2529         const ia32_immediate_attr_t *attr = get_ia32_immediate_attr_const(op);
2530         unsigned                     size;
2531
2532         /* Some instructions (test) have no short form with 32bit value + 8bit
2533          * immediate. */
2534         if (attr->symconst != NULL || opcode & SIGNEXT_IMM) {
2535                 size = 4;
2536         } else {
2537                 /* check for sign extension */
2538                 size = get_signed_imm_size(attr->offset);
2539         }
2540
2541         switch (size) {
2542         case 1:
2543                 bemit8(opcode | SIGNEXT_IMM);
2544                 /* cmp has this special mode */
2545                 if (get_ia32_op_type(node) == ia32_AddrModeS) {
2546                         bemit_mod_am(ruval, node);
2547                 } else {
2548                         const arch_register_t *reg = get_in_reg(node, n_ia32_binary_left);
2549                         bemit_modru(reg, ruval);
2550                 }
2551                 bemit8((unsigned char)attr->offset);
2552                 return;
2553         case 2:
2554         case 4:
2555                 /* check for eax variant: this variant is shorter for 32bit immediates only */
2556                 if (get_ia32_op_type(node) == ia32_AddrModeS) {
2557                         bemit8(opcode);
2558                         bemit_mod_am(ruval, node);
2559                 } else {
2560                         const arch_register_t *reg = get_in_reg(node, n_ia32_binary_left);
2561                         if (reg->index == REG_GP_EAX) {
2562                                 bemit8(opcode_ax);
2563                         } else {
2564                                 bemit8(opcode);
2565                                 bemit_modru(reg, ruval);
2566                         }
2567                 }
2568                 bemit_entity(attr->symconst, attr->sc_sign, attr->offset, false);
2569                 return;
2570         }
2571         panic("invalid imm size?!?");
2572 }
2573
2574 /**
2575  * Emits a binop.
2576  */
2577 static void bemit_binop_2(const ir_node *node, unsigned code)
2578 {
2579         const arch_register_t *out = get_in_reg(node, n_ia32_binary_left);
2580         bemit8(code);
2581         if (get_ia32_op_type(node) == ia32_Normal) {
2582                 const arch_register_t *op2 = get_in_reg(node, n_ia32_binary_right);
2583                 bemit_modrr(op2, out);
2584         } else {
2585                 bemit_mod_am(reg_gp_map[out->index], node);
2586         }
2587 }
2588
2589 /**
2590  * Emit a binop.
2591  */
2592 static void bemit_binop(const ir_node *node, const unsigned char opcodes[4])
2593 {
2594         ir_node *right = get_irn_n(node, n_ia32_binary_right);
2595         if (is_ia32_Immediate(right)) {
2596                 bemit_binop_with_imm(node, opcodes[1], opcodes[2], opcodes[3]);
2597         } else {
2598                 bemit_binop_2(node, opcodes[0]);
2599         }
2600 }
2601
2602 /**
2603  * Emit an unop.
2604  */
2605 static void bemit_unop(const ir_node *node, unsigned char code, unsigned char ext, int input)
2606 {
2607         bemit8(code);
2608         if (get_ia32_op_type(node) == ia32_Normal) {
2609                 const arch_register_t *in = get_in_reg(node, input);
2610                 bemit_modru(in, ext);
2611         } else {
2612                 bemit_mod_am(ext, node);
2613         }
2614 }
2615
2616 static void bemit_unop_reg(const ir_node *node, unsigned char code, int input)
2617 {
2618         const arch_register_t *out = get_out_reg(node, 0);
2619         bemit_unop(node, code, reg_gp_map[out->index], input);
2620 }
2621
2622 static void bemit_unop_mem(const ir_node *node, unsigned char code, unsigned char ext)
2623 {
2624         unsigned size = get_mode_size_bits(get_ia32_ls_mode(node));
2625         if (size == 16)
2626                 bemit8(0x66);
2627         bemit8(size == 8 ? code : code + 1);
2628         bemit_mod_am(ext, node);
2629 }
2630
2631 static void bemit_immediate(const ir_node *node, bool relative)
2632 {
2633         const ia32_immediate_attr_t *attr = get_ia32_immediate_attr_const(node);
2634         bemit_entity(attr->symconst, attr->sc_sign, attr->offset, relative);
2635 }
2636
2637 static void bemit_copy(const ir_node *copy)
2638 {
2639         const arch_register_t *in  = get_in_reg(copy, 0);
2640         const arch_register_t *out = get_out_reg(copy, 0);
2641
2642         if (in == out)
2643                 return;
2644         /* copies of vf nodes aren't real... */
2645         if (arch_register_get_class(in) == &ia32_reg_classes[CLASS_ia32_vfp])
2646                 return;
2647
2648         if (get_irn_mode(copy) == mode_E) {
2649                 panic("NIY");
2650         } else {
2651                 assert(arch_register_get_class(in) == &ia32_reg_classes[CLASS_ia32_gp]);
2652                 bemit8(0x8B);
2653                 bemit_modrr(in, out);
2654         }
2655 }
2656
2657 static void bemit_perm(const ir_node *node)
2658 {
2659         const arch_register_t       *in0  = arch_get_irn_register(get_irn_n(node, 0));
2660         const arch_register_t       *in1  = arch_get_irn_register(get_irn_n(node, 1));
2661         const arch_register_class_t *cls0 = arch_register_get_class(in0);
2662
2663         assert(cls0 == arch_register_get_class(in1) && "Register class mismatch at Perm");
2664
2665         if (cls0 == &ia32_reg_classes[CLASS_ia32_gp]) {
2666                 if (in0->index == REG_GP_EAX) {
2667                         bemit8(0x90 + reg_gp_map[in1->index]);
2668                 } else if (in1->index == REG_GP_EAX) {
2669                         bemit8(0x90 + reg_gp_map[in0->index]);
2670                 } else {
2671                         bemit8(0x87);
2672                         bemit_modrr(in0, in1);
2673                 }
2674         } else if (cls0 == &ia32_reg_classes[CLASS_ia32_xmm]) {
2675                 panic("unimplemented"); // TODO implement
2676                 //ia32_emitf(NULL, "\txorpd %R, %R\n", in1, in0);
2677                 //ia32_emitf(NULL, "\txorpd %R, %R\n", in0, in1);
2678                 //ia32_emitf(node, "\txorpd %R, %R\n", in1, in0);
2679         } else if (cls0 == &ia32_reg_classes[CLASS_ia32_vfp]) {
2680                 /* is a NOP */
2681         } else if (cls0 == &ia32_reg_classes[CLASS_ia32_st]) {
2682                 /* is a NOP */
2683         } else {
2684                 panic("unexpected register class in be_Perm (%+F)", node);
2685         }
2686 }
2687
2688 static void bemit_xor0(const ir_node *node)
2689 {
2690         const arch_register_t *out = get_out_reg(node, 0);
2691         bemit8(0x31);
2692         bemit_modrr(out, out);
2693 }
2694
2695 static void bemit_mov_const(const ir_node *node)
2696 {
2697         const arch_register_t *out = get_out_reg(node, 0);
2698         bemit8(0xB8 + reg_gp_map[out->index]);
2699         bemit_immediate(node, false);
2700 }
2701
2702 /**
2703  * Creates a function for a Binop with 3 possible encodings.
2704  */
2705 #define BINOP(op, op0, op1, op2, op2_ext)                                 \
2706 static void bemit_ ## op(const ir_node *node) {                           \
2707         static const unsigned char op ## _codes[] = {op0, op1, op2, op2_ext}; \
2708         bemit_binop(node, op ## _codes);                                      \
2709 }
2710
2711 /*    insn  def  eax,imm   imm */
2712 BINOP(add,  0x03, 0x05, 0x81, 0)
2713 BINOP(or,   0x0B, 0x0D, 0x81, 1)
2714 BINOP(adc,  0x13, 0x15, 0x81, 2)
2715 BINOP(sbb,  0x1B, 0x1D, 0x81, 3)
2716 BINOP(and,  0x23, 0x25, 0x81, 4)
2717 BINOP(sub,  0x2B, 0x2D, 0x81, 5)
2718 BINOP(xor,  0x33, 0x35, 0x81, 6)
2719 BINOP(test, 0x85, 0xA9, 0xF7, 0)
2720
2721 #define BINOPMEM(op, ext) \
2722 static void bemit_##op(const ir_node *node) \
2723 { \
2724         ir_node *val; \
2725         unsigned size = get_mode_size_bits(get_ia32_ls_mode(node)); \
2726         if (size == 16) \
2727                 bemit8(0x66); \
2728         val = get_irn_n(node, n_ia32_unary_op); \
2729         if (is_ia32_Immediate(val)) { \
2730                 const ia32_immediate_attr_t *attr   = get_ia32_immediate_attr_const(val); \
2731                 int                          offset = attr->offset; \
2732                 if (attr->symconst == NULL && get_signed_imm_size(offset) == 1) { \
2733                         bemit8(0x83); \
2734                         bemit_mod_am(ext, node); \
2735                         bemit8(offset); \
2736                 } else { \
2737                         bemit8(0x81); \
2738                         bemit_mod_am(ext, node); \
2739                         if (size == 16) { \
2740                                 bemit16(offset); \
2741                         } else { \
2742                                 bemit_entity(attr->symconst, attr->sc_sign, offset, false); \
2743                         } \
2744                 } \
2745         } else { \
2746                 bemit8(ext << 3 | 1); \
2747                 bemit_mod_am(reg_gp_map[get_out_reg(val, 0)->index], node); \
2748         } \
2749 } \
2750  \
2751 static void bemit_##op##8bit(const ir_node *node) \
2752 { \
2753         ir_node *val = get_irn_n(node, n_ia32_unary_op); \
2754         if (is_ia32_Immediate(val)) { \
2755                 bemit8(0x80); \
2756                 bemit_mod_am(ext, node); \
2757                 bemit8(get_ia32_immediate_attr_const(val)->offset); \
2758         } else { \
2759                 bemit8(ext << 3); \
2760                 bemit_mod_am(reg_gp_map[get_out_reg(val, 0)->index], node); \
2761         } \
2762 }
2763
2764 BINOPMEM(addmem,  0)
2765 BINOPMEM(ormem,   1)
2766 BINOPMEM(andmem,  4)
2767 BINOPMEM(submem,  5)
2768 BINOPMEM(xormem,  6)
2769
2770
2771 /**
2772  * Creates a function for an Unop with code /ext encoding.
2773  */
2774 #define UNOP(op, code, ext, input)              \
2775 static void bemit_ ## op(const ir_node *node) { \
2776         bemit_unop(node, code, ext, input);         \
2777 }
2778
2779 UNOP(not,     0xF7, 2, n_ia32_Not_val)
2780 UNOP(neg,     0xF7, 3, n_ia32_Neg_val)
2781 UNOP(mul,     0xF7, 4, n_ia32_Mul_right)
2782 UNOP(imul1op, 0xF7, 5, n_ia32_IMul1OP_right)
2783 UNOP(div,     0xF7, 6, n_ia32_Div_divisor)
2784 UNOP(idiv,    0xF7, 7, n_ia32_IDiv_divisor)
2785
2786 /* TODO: am support for IJmp */
2787 UNOP(ijmp,    0xFF, 4, n_ia32_IJmp_target)
2788
2789 #define SHIFT(op, ext) \
2790 static void bemit_##op(const ir_node *node) \
2791 { \
2792         const arch_register_t *out   = get_out_reg(node, 0); \
2793         ir_node               *count = get_irn_n(node, 1); \
2794         if (is_ia32_Immediate(count)) { \
2795                 int offset = get_ia32_immediate_attr_const(count)->offset; \
2796                 if (offset == 1) { \
2797                         bemit8(0xD1); \
2798                         bemit_modru(out, ext); \
2799                 } else { \
2800                         bemit8(0xC1); \
2801                         bemit_modru(out, ext); \
2802                         bemit8(offset); \
2803                 } \
2804         } else { \
2805                 bemit8(0xD3); \
2806                 bemit_modru(out, ext); \
2807         } \
2808 } \
2809  \
2810 static void bemit_##op##mem(const ir_node *node) \
2811 { \
2812         ir_node *count; \
2813         unsigned size = get_mode_size_bits(get_ia32_ls_mode(node)); \
2814         if (size == 16) \
2815                 bemit8(0x66); \
2816         count = get_irn_n(node, 1); \
2817         if (is_ia32_Immediate(count)) { \
2818                 int offset = get_ia32_immediate_attr_const(count)->offset; \
2819                 if (offset == 1) { \
2820                         bemit8(size == 8 ? 0xD0 : 0xD1); \
2821                         bemit_mod_am(ext, node); \
2822                 } else { \
2823                         bemit8(size == 8 ? 0xC0 : 0xC1); \
2824                         bemit_mod_am(ext, node); \
2825                         bemit8(offset); \
2826                 } \
2827         } else { \
2828                 bemit8(size == 8 ? 0xD2 : 0xD3); \
2829                 bemit_mod_am(ext, node); \
2830         } \
2831 }
2832
2833 SHIFT(rol, 0)
2834 SHIFT(ror, 1)
2835 SHIFT(shl, 4)
2836 SHIFT(shr, 5)
2837 SHIFT(sar, 7)
2838
2839 static void bemit_shld(const ir_node *node)
2840 {
2841         const arch_register_t *in  = get_in_reg(node, n_ia32_ShlD_val_low);
2842         const arch_register_t *out = get_out_reg(node, pn_ia32_ShlD_res);
2843         ir_node *count = get_irn_n(node, n_ia32_ShlD_count);
2844         bemit8(0x0F);
2845         if (is_ia32_Immediate(count)) {
2846                 bemit8(0xA4);
2847                 bemit_modrr(out, in);
2848                 bemit8(get_ia32_immediate_attr_const(count)->offset);
2849         } else {
2850                 bemit8(0xA5);
2851                 bemit_modrr(out, in);
2852         }
2853 }
2854
2855 static void bemit_shrd(const ir_node *node)
2856 {
2857         const arch_register_t *in  = get_in_reg(node, n_ia32_ShrD_val_low);
2858         const arch_register_t *out = get_out_reg(node, pn_ia32_ShrD_res);
2859         ir_node *count = get_irn_n(node, n_ia32_ShrD_count);
2860         bemit8(0x0F);
2861         if (is_ia32_Immediate(count)) {
2862                 bemit8(0xAC);
2863                 bemit_modrr(out, in);
2864                 bemit8(get_ia32_immediate_attr_const(count)->offset);
2865         } else {
2866                 bemit8(0xAD);
2867                 bemit_modrr(out, in);
2868         }
2869 }
2870
2871 /**
2872  * binary emitter for setcc.
2873  */
2874 static void bemit_setcc(const ir_node *node)
2875 {
2876         const arch_register_t *dreg = get_out_reg(node, pn_ia32_Setcc_res);
2877
2878         pn_Cmp pnc = get_ia32_condcode(node);
2879         pnc        = determine_final_pnc(node, n_ia32_Setcc_eflags, pnc);
2880         if (pnc & ia32_pn_Cmp_float) {
2881                 switch (pnc & 0x0f) {
2882                 case pn_Cmp_Uo:
2883                          /* setp <dreg */
2884                         bemit8(0x0F);
2885                         bemit8(0x9A);
2886                         bemit_modrm8(REG_LOW, dreg);
2887                         return;
2888
2889                 case pn_Cmp_Leg:
2890                          /* setnp <dreg*/
2891                         bemit8(0x0F);
2892                         bemit8(0x9B);
2893                         bemit_modrm8(REG_LOW, dreg);
2894                         return;
2895
2896                 case pn_Cmp_Eq:
2897                 case pn_Cmp_Lt:
2898                 case pn_Cmp_Le:
2899                          /* set%PNC <dreg */
2900                         bemit8(0x0F);
2901                         bemit8(0x90 | pnc2cc(pnc));
2902                         bemit_modrm8(REG_LOW, dreg);
2903
2904                         /* setnp >dreg */
2905                         bemit8(0x0F);
2906                         bemit8(0x9B);
2907                         bemit_modrm8(REG_HIGH, dreg);
2908
2909                         /* andb %>dreg, %<dreg */
2910                         bemit8(0x20);
2911                         bemit_modrr8(REG_LOW, dreg, REG_HIGH, dreg);
2912                         return;
2913
2914                 case pn_Cmp_Ug:
2915                 case pn_Cmp_Uge:
2916                 case pn_Cmp_Ne:
2917                         /* set%PNC <dreg */
2918                         bemit8(0x0F);
2919                         bemit8(0x90 | pnc2cc(pnc));
2920                         bemit_modrm8(REG_LOW, dreg);
2921
2922                         /* setp >dreg */
2923                         bemit8(0x0F);
2924                         bemit8(0x9A);
2925                         bemit_modrm8(REG_HIGH, dreg);
2926
2927                         /* orb %>dreg, %<dreg */
2928                         bemit8(0x08);
2929                         bemit_modrr8(REG_LOW, dreg, REG_HIGH, dreg);
2930                         return;
2931
2932                 default:
2933                         break;
2934                 }
2935         }
2936         /* set%PNC <dreg */
2937         bemit8(0x0F);
2938         bemit8(0x90 | pnc2cc(pnc));
2939         bemit_modrm8(REG_LOW, dreg);
2940 }
2941
2942 static void bemit_cmovcc(const ir_node *node)
2943 {
2944         const ia32_attr_t     *attr         = get_ia32_attr_const(node);
2945         int                    ins_permuted = attr->data.ins_permuted;
2946         const arch_register_t *out          = arch_irn_get_register(node, pn_ia32_res);
2947         pn_Cmp                 pnc          = get_ia32_condcode(node);
2948         const arch_register_t *in_true;
2949         const arch_register_t *in_false;
2950
2951         pnc = determine_final_pnc(node, n_ia32_CMovcc_eflags, pnc);
2952
2953         in_true  = arch_get_irn_register(get_irn_n(node, n_ia32_CMovcc_val_true));
2954         in_false = arch_get_irn_register(get_irn_n(node, n_ia32_CMovcc_val_false));
2955
2956         /* should be same constraint fullfilled? */
2957         if (out == in_false) {
2958                 /* yes -> nothing to do */
2959         } else if (out == in_true) {
2960                 assert(get_ia32_op_type(node) == ia32_Normal);
2961                 ins_permuted = !ins_permuted;
2962                 in_true      = in_false;
2963         } else {
2964                 /* we need a mov */
2965                 bemit8(0x8B); // mov %in_false, %out
2966                 bemit_modrr(in_false, out);
2967         }
2968
2969         if (ins_permuted)
2970                 pnc = ia32_get_negated_pnc(pnc);
2971
2972         /* TODO: handling of Nans isn't correct yet */
2973
2974         bemit8(0x0F);
2975         bemit8(0x40 | pnc2cc(pnc));
2976         if (get_ia32_op_type(node) == ia32_Normal) {
2977                 bemit_modrr(in_true, out);
2978         } else {
2979                 bemit_mod_am(reg_gp_map[out->index], node);
2980         }
2981 }
2982
2983 static void bemit_cmp(const ir_node *node)
2984 {
2985         unsigned  ls_size = get_mode_size_bits(get_ia32_ls_mode(node));
2986         ir_node  *right;
2987
2988         if (ls_size == 16)
2989                 bemit8(0x66);
2990
2991         right = get_irn_n(node, n_ia32_binary_right);
2992         if (is_ia32_Immediate(right)) {
2993                 /* Use in-reg, because some instructions (cmp, test) have no out-reg. */
2994                 const ir_node               *op   = get_irn_n(node, n_ia32_binary_right);
2995                 const ia32_immediate_attr_t *attr = get_ia32_immediate_attr_const(op);
2996                 unsigned                     size;
2997
2998                 if (attr->symconst != NULL) {
2999                         size = 4;
3000                 } else {
3001                         /* check for sign extension */
3002                         size = get_signed_imm_size(attr->offset);
3003                 }
3004
3005                 switch (size) {
3006                         case 1:
3007                                 bemit8(0x81 | SIGNEXT_IMM);
3008                                 /* cmp has this special mode */
3009                                 if (get_ia32_op_type(node) == ia32_AddrModeS) {
3010                                         bemit_mod_am(7, node);
3011                                 } else {
3012                                         const arch_register_t *reg = get_in_reg(node, n_ia32_binary_left);
3013                                         bemit_modru(reg, 7);
3014                                 }
3015                                 bemit8((unsigned char)attr->offset);
3016                                 return;
3017                         case 2:
3018                         case 4:
3019                                 /* check for eax variant: this variant is shorter for 32bit immediates only */
3020                                 if (get_ia32_op_type(node) == ia32_AddrModeS) {
3021                                         bemit8(0x81);
3022                                         bemit_mod_am(7, node);
3023                                 } else {
3024                                         const arch_register_t *reg = get_in_reg(node, n_ia32_binary_left);
3025                                         if (reg->index == REG_GP_EAX) {
3026                                                 bemit8(0x3D);
3027                                         } else {
3028                                                 bemit8(0x81);
3029                                                 bemit_modru(reg, 7);
3030                                         }
3031                                 }
3032                                 if (ls_size == 16) {
3033                                         bemit16(attr->offset);
3034                                 } else {
3035                                         bemit_entity(attr->symconst, attr->sc_sign, attr->offset, false);
3036                                 }
3037                                 return;
3038                 }
3039                 panic("invalid imm size?!?");
3040         } else {
3041                 const arch_register_t *out = get_in_reg(node, n_ia32_binary_left);
3042                 bemit8(0x3B);
3043                 if (get_ia32_op_type(node) == ia32_Normal) {
3044                         const arch_register_t *op2 = get_in_reg(node, n_ia32_binary_right);
3045                         bemit_modrr(op2, out);
3046                 } else {
3047                         bemit_mod_am(reg_gp_map[out->index], node);
3048                 }
3049         }
3050 }
3051
3052 static void bemit_cmp8bit(const ir_node *node)
3053 {
3054         ir_node *right = get_irn_n(node, n_ia32_binary_right);
3055         if (is_ia32_Immediate(right)) {
3056                 if (get_ia32_op_type(node) == ia32_Normal) {
3057                         const arch_register_t *out = get_in_reg(node, n_ia32_Cmp_left);
3058                         if (out->index == REG_GP_EAX) {
3059                                 bemit8(0x3C);
3060                         } else {
3061                                 bemit8(0x80);
3062                                 bemit_modru(out, 7);
3063                         }
3064                 } else {
3065                         bemit8(0x80);
3066                         bemit_mod_am(7, node);
3067                 }
3068                 bemit8(get_ia32_immediate_attr_const(right)->offset);
3069         } else {
3070                 const arch_register_t *out = get_in_reg(node, n_ia32_Cmp_left);
3071                 bemit8(0x3A);
3072                 if (get_ia32_op_type(node) == ia32_Normal) {
3073                         const arch_register_t *in = get_in_reg(node, n_ia32_Cmp_right);
3074                         bemit_modrr(out, in);
3075                 } else {
3076                         bemit_mod_am(reg_gp_map[out->index], node);
3077                 }
3078         }
3079 }
3080
3081 static void bemit_test8bit(const ir_node *node)
3082 {
3083         ir_node *right = get_irn_n(node, n_ia32_Test8Bit_right);
3084         if (is_ia32_Immediate(right)) {
3085                 if (get_ia32_op_type(node) == ia32_Normal) {
3086                         const arch_register_t *out = get_in_reg(node, n_ia32_Test8Bit_left);
3087                         if (out->index == REG_GP_EAX) {
3088                                 bemit8(0xA8);
3089                         } else {
3090                                 bemit8(0xF6);
3091                                 bemit_modru(out, 0);
3092                         }
3093                 } else {
3094                         bemit8(0xF6);
3095                         bemit_mod_am(0, node);
3096                 }
3097                 bemit8(get_ia32_immediate_attr_const(right)->offset);
3098         } else {
3099                 const arch_register_t *out = get_in_reg(node, n_ia32_Test8Bit_left);
3100                 bemit8(0x84);
3101                 if (get_ia32_op_type(node) == ia32_Normal) {
3102                         const arch_register_t *in = get_in_reg(node, n_ia32_Test8Bit_right);
3103                         bemit_modrr(out, in);
3104                 } else {
3105                         bemit_mod_am(reg_gp_map[out->index], node);
3106                 }
3107         }
3108 }
3109
3110 static void bemit_imul(const ir_node *node)
3111 {
3112         ir_node *right = get_irn_n(node, n_ia32_IMul_right);
3113         /* Do we need the immediate form? */
3114         if (is_ia32_Immediate(right)) {
3115                 int imm = get_ia32_immediate_attr_const(right)->offset;
3116                 if (get_signed_imm_size(imm) == 1) {
3117                         bemit_unop_reg(node, 0x6B, n_ia32_IMul_left);
3118                         bemit8(imm);
3119                 } else {
3120                         bemit_unop_reg(node, 0x69, n_ia32_IMul_left);
3121                         bemit32(imm);
3122                 }
3123         } else {
3124                 bemit8(0x0F);
3125                 bemit_unop_reg(node, 0xAF, n_ia32_IMul_right);
3126         }
3127 }
3128
3129 static void bemit_dec(const ir_node *node)
3130 {
3131         const arch_register_t *out = get_out_reg(node, pn_ia32_Dec_res);
3132         bemit8(0x48 + reg_gp_map[out->index]);
3133 }
3134
3135 static void bemit_inc(const ir_node *node)
3136 {
3137         const arch_register_t *out = get_out_reg(node, pn_ia32_Inc_res);
3138         bemit8(0x40 + reg_gp_map[out->index]);
3139 }
3140
3141 #define UNOPMEM(op, code, ext) \
3142 static void bemit_##op(const ir_node *node) \
3143 { \
3144         bemit_unop_mem(node, code, ext); \
3145 }
3146
3147 UNOPMEM(notmem, 0xF6, 2)
3148 UNOPMEM(negmem, 0xF6, 3)
3149 UNOPMEM(incmem, 0xFE, 0)
3150 UNOPMEM(decmem, 0xFE, 1)
3151
3152 static void bemit_ldtls(const ir_node *node)
3153 {
3154         const arch_register_t *out = get_out_reg(node, 0);
3155
3156         bemit8(0x65); // gs:
3157         if (out->index == REG_GP_EAX) {
3158                 bemit8(0xA1); // movl 0, %eax
3159         } else {
3160                 bemit8(0x8B); // movl 0, %reg
3161                 bemit8(MOD_IND | ENC_REG(reg_gp_map[out->index]) | ENC_RM(0x05));
3162         }
3163         bemit32(0);
3164 }
3165
3166 /**
3167  * Emit a Lea.
3168  */
3169 static void bemit_lea(const ir_node *node)
3170 {
3171         const arch_register_t *out = get_out_reg(node, 0);
3172         bemit8(0x8D);
3173         bemit_mod_am(reg_gp_map[out->index], node);
3174 }
3175
3176 /* helper function for bemit_minus64bit */
3177 static void bemit_helper_mov(const arch_register_t *src, const arch_register_t *dst)
3178 {
3179         bemit8(0x8B); // movl %src, %dst
3180         bemit_modrr(src, dst);
3181 }
3182
3183 /* helper function for bemit_minus64bit */
3184 static void bemit_helper_neg(const arch_register_t *reg)
3185 {
3186         bemit8(0xF7); // negl %reg
3187         bemit_modru(reg, 3);
3188 }
3189
3190 /* helper function for bemit_minus64bit */
3191 static void bemit_helper_sbb0(const arch_register_t *reg)
3192 {
3193         bemit8(0x83); // sbbl $0, %reg
3194         bemit_modru(reg, 3);
3195         bemit8(0);
3196 }
3197
3198 /* helper function for bemit_minus64bit */
3199 static void bemit_helper_sbb(const arch_register_t *src, const arch_register_t *dst)
3200 {
3201         bemit8(0x1B); // sbbl %src, %dst
3202         bemit_modrr(src, dst);
3203 }
3204
3205 /* helper function for bemit_minus64bit */
3206 static void bemit_helper_xchg(const arch_register_t *src, const arch_register_t *dst)
3207 {
3208         if (src->index == REG_GP_EAX) {
3209                 bemit8(0x90 + reg_gp_map[dst->index]); // xchgl %eax, %dst
3210         } else if (dst->index == REG_GP_EAX) {
3211                 bemit8(0x90 + reg_gp_map[src->index]); // xchgl %src, %eax
3212         } else {
3213                 bemit8(0x87); // xchgl %src, %dst
3214                 bemit_modrr(src, dst);
3215         }
3216 }
3217
3218 /* helper function for bemit_minus64bit */
3219 static void bemit_helper_zero(const arch_register_t *reg)
3220 {
3221         bemit8(0x33); // xorl %reg, %reg
3222         bemit_modrr(reg, reg);
3223 }
3224
3225 static void bemit_minus64bit(const ir_node *node)
3226 {
3227         const arch_register_t *in_lo  = get_in_reg(node, 0);
3228         const arch_register_t *in_hi  = get_in_reg(node, 1);
3229         const arch_register_t *out_lo = get_out_reg(node, 0);
3230         const arch_register_t *out_hi = get_out_reg(node, 1);
3231
3232         if (out_lo == in_lo) {
3233                 if (out_hi != in_hi) {
3234                         /* a -> a, b -> d */
3235                         goto zero_neg;
3236                 } else {
3237                         /* a -> a, b -> b */
3238                         goto normal_neg;
3239                 }
3240         } else if (out_lo == in_hi) {
3241                 if (out_hi == in_lo) {
3242                         /* a -> b, b -> a */
3243                         bemit_helper_xchg(in_lo, in_hi);
3244                         goto normal_neg;
3245                 } else {
3246                         /* a -> b, b -> d */
3247                         bemit_helper_mov(in_hi, out_hi);
3248                         bemit_helper_mov(in_lo, out_lo);
3249                         goto normal_neg;
3250                 }
3251         } else {
3252                 if (out_hi == in_lo) {
3253                         /* a -> c, b -> a */
3254                         bemit_helper_mov(in_lo, out_lo);
3255                         goto zero_neg;
3256                 } else if (out_hi == in_hi) {
3257                         /* a -> c, b -> b */
3258                         bemit_helper_mov(in_lo, out_lo);
3259                         goto normal_neg;
3260                 } else {
3261                         /* a -> c, b -> d */
3262                         bemit_helper_mov(in_lo, out_lo);
3263                         goto zero_neg;
3264                 }
3265         }
3266
3267 normal_neg:
3268         bemit_helper_neg( out_hi);
3269         bemit_helper_neg( out_lo);
3270         bemit_helper_sbb0(out_hi);
3271         return;
3272
3273 zero_neg:
3274         bemit_helper_zero(out_hi);
3275         bemit_helper_neg( out_lo);
3276         bemit_helper_sbb( in_hi, out_hi);
3277 }
3278
3279 /**
3280  * Emit a single opcode.
3281  */
3282 #define EMIT_SINGLEOP(op, code)                 \
3283 static void bemit_ ## op(const ir_node *node) { \
3284         (void) node;                                \
3285         bemit8(code);                               \
3286 }
3287
3288 //EMIT_SINGLEOP(daa,  0x27)
3289 //EMIT_SINGLEOP(das,  0x2F)
3290 //EMIT_SINGLEOP(aaa,  0x37)
3291 //EMIT_SINGLEOP(aas,  0x3F)
3292 //EMIT_SINGLEOP(nop,  0x90)
3293 EMIT_SINGLEOP(cwtl,  0x98)
3294 EMIT_SINGLEOP(cltd,  0x99)
3295 //EMIT_SINGLEOP(fwait, 0x9B)
3296 EMIT_SINGLEOP(sahf,  0x9E)
3297 //EMIT_SINGLEOP(popf, 0x9D)
3298 EMIT_SINGLEOP(leave, 0xC9)
3299 EMIT_SINGLEOP(int3,  0xCC)
3300 //EMIT_SINGLEOP(iret, 0xCF)
3301 //EMIT_SINGLEOP(xlat, 0xD7)
3302 //EMIT_SINGLEOP(lock, 0xF0)
3303 EMIT_SINGLEOP(rep,   0xF3)
3304 //EMIT_SINGLEOP(halt, 0xF4)
3305 EMIT_SINGLEOP(cmc,   0xF5)
3306 EMIT_SINGLEOP(stc,   0xF9)
3307 //EMIT_SINGLEOP(cli,  0xFA)
3308 //EMIT_SINGLEOP(sti,  0xFB)
3309 //EMIT_SINGLEOP(std,  0xFD)
3310
3311 /**
3312  * Emits a MOV out, [MEM].
3313  */
3314 static void bemit_load(const ir_node *node)
3315 {
3316         const arch_register_t *out = get_out_reg(node, 0);
3317
3318         if (out->index == REG_GP_EAX) {
3319                 ir_node   *base      = get_irn_n(node, n_ia32_base);
3320                 int        has_base  = !is_ia32_NoReg_GP(base);
3321                 ir_node   *index     = get_irn_n(node, n_ia32_index);
3322                 int        has_index = !is_ia32_NoReg_GP(index);
3323                 if (!has_base && !has_index) {
3324                         ir_entity *ent  = get_ia32_am_sc(node);
3325                         int        offs = get_ia32_am_offs_int(node);
3326                         /* load from constant address to EAX can be encoded
3327                            as 0xA1 [offset] */
3328                         bemit8(0xA1);
3329                         bemit_entity(ent, 0, offs, false);
3330                         return;
3331                 }
3332         }
3333         bemit8(0x8B);
3334         bemit_mod_am(reg_gp_map[out->index], node);
3335 }
3336
3337 /**
3338  * Emits a MOV [mem], in.
3339  */
3340 static void bemit_store(const ir_node *node)
3341 {
3342         const ir_node *value = get_irn_n(node, n_ia32_Store_val);
3343         unsigned       size  = get_mode_size_bits(get_ia32_ls_mode(node));
3344
3345         if (is_ia32_Immediate(value)) {
3346                 if (size == 8) {
3347                         bemit8(0xC6);
3348                         bemit_mod_am(0, node);
3349                         bemit8(get_ia32_immediate_attr_const(value)->offset);
3350                 } else if (size == 16) {
3351                         bemit8(0x66);
3352                         bemit8(0xC7);
3353                         bemit_mod_am(0, node);
3354                         bemit16(get_ia32_immediate_attr_const(value)->offset);
3355                 } else {
3356                         bemit8(0xC7);
3357                         bemit_mod_am(0, node);
3358                         bemit_immediate(value, false);
3359                 }
3360         } else {
3361                 const arch_register_t *in = get_in_reg(node, n_ia32_Store_val);
3362
3363                 if (in->index == REG_GP_EAX) {
3364                         ir_node   *base      = get_irn_n(node, n_ia32_base);
3365                         int        has_base  = !is_ia32_NoReg_GP(base);
3366                         ir_node   *index     = get_irn_n(node, n_ia32_index);
3367                         int        has_index = !is_ia32_NoReg_GP(index);
3368                         if (!has_base && !has_index) {
3369                                 ir_entity *ent  = get_ia32_am_sc(node);
3370                                 int        offs = get_ia32_am_offs_int(node);
3371                                 /* store to constant address from EAX can be encoded as
3372                                  * 0xA2/0xA3 [offset]*/
3373                                 if (size == 8) {
3374                                         bemit8(0xA2);
3375                                 } else {
3376                                         if (size == 16)
3377                                                 bemit8(0x66);
3378                                         bemit8(0xA3);
3379                                 }
3380                                 bemit_entity(ent, 0, offs, false);
3381                                 return;
3382                         }
3383                 }
3384
3385                 if (size == 8) {
3386                         bemit8(0x88);
3387                 } else {
3388                         if (size == 16)
3389                                 bemit8(0x66);
3390                         bemit8(0x89);
3391                 }
3392                 bemit_mod_am(reg_gp_map[in->index], node);
3393         }
3394 }
3395
3396 static void bemit_conv_i2i(const ir_node *node)
3397 {
3398         ir_mode  *smaller_mode = get_ia32_ls_mode(node);
3399         unsigned  opcode;
3400
3401         bemit8(0x0F);
3402         /*        8 16 bit source
3403          * movzx B6 B7
3404          * movsx BE BF
3405          */
3406         opcode = 0xB6;
3407         if (mode_is_signed(smaller_mode))           opcode |= 0x08;
3408         if (get_mode_size_bits(smaller_mode) == 16) opcode |= 0x01;
3409         bemit_unop_reg(node, opcode, n_ia32_Conv_I2I_val);
3410 }
3411
3412 /**
3413  * Emit a Push.
3414  */
3415 static void bemit_push(const ir_node *node)
3416 {
3417         const ir_node *value = get_irn_n(node, n_ia32_Push_val);
3418
3419         if (is_ia32_Immediate(value)) {
3420                 const ia32_immediate_attr_t *attr
3421                         = get_ia32_immediate_attr_const(value);
3422                 unsigned size = get_signed_imm_size(attr->offset);
3423                 if (attr->symconst)
3424                         size = 4;
3425                 switch (size) {
3426                 case 1:
3427                         bemit8(0x6A);
3428                         bemit8((unsigned char)attr->offset);
3429                         break;
3430                 case 2:
3431                 case 4:
3432                         bemit8(0x68);
3433                         bemit_immediate(value, false);
3434                         break;
3435                 }
3436         } else if (is_ia32_NoReg_GP(value)) {
3437                 bemit8(0xFF);
3438                 bemit_mod_am(6, node);
3439         } else {
3440                 const arch_register_t *reg = get_in_reg(node, n_ia32_Push_val);
3441                 bemit8(0x50 + reg_gp_map[reg->index]);
3442         }
3443 }
3444
3445 /**
3446  * Emit a Pop.
3447  */
3448 static void bemit_pop(const ir_node *node)
3449 {
3450         const arch_register_t *reg = get_out_reg(node, pn_ia32_Pop_res);
3451         bemit8(0x58 + reg_gp_map[reg->index]);
3452 }
3453
3454 static void bemit_popmem(const ir_node *node)
3455 {
3456         bemit8(0x8F);
3457         bemit_mod_am(0, node);
3458 }
3459
3460 static void bemit_call(const ir_node *node)
3461 {
3462         ir_node *proc = get_irn_n(node, n_ia32_Call_addr);
3463
3464         if (is_ia32_Immediate(proc)) {
3465                 bemit8(0xE8);
3466                 bemit_immediate(proc, true);
3467         } else {
3468                 bemit_unop(node, 0xFF, 2, n_ia32_Call_addr);
3469         }
3470 }
3471
3472 static void bemit_jmp(const ir_node *dest_block)
3473 {
3474         bemit8(0xE9);
3475         bemit_jmp_destination(dest_block);
3476 }
3477
3478 static void bemit_jump(const ir_node *node)
3479 {
3480         if (can_be_fallthrough(node))
3481                 return;
3482
3483         bemit_jmp(get_cfop_target_block(node));
3484 }
3485
3486 static void bemit_jcc(int pnc, const ir_node *dest_block)
3487 {
3488         unsigned char cc = pnc2cc(pnc);
3489         bemit8(0x0F);
3490         bemit8(0x80 + cc);
3491         bemit_jmp_destination(dest_block);
3492 }
3493
3494 static void bemit_jp(bool odd, const ir_node *dest_block)
3495 {
3496         bemit8(0x0F);
3497         bemit8(0x8A + odd);
3498         bemit_jmp_destination(dest_block);
3499 }
3500
3501 static void bemit_ia32_jcc(const ir_node *node)
3502 {
3503         int            pnc = get_ia32_condcode(node);
3504         const ir_node *proj_true;
3505         const ir_node *proj_false;
3506         const ir_node *dest_true;
3507         const ir_node *dest_false;
3508         const ir_node *block;
3509
3510         pnc = determine_final_pnc(node, 0, pnc);
3511
3512         /* get both Projs */
3513         proj_true = get_proj(node, pn_ia32_Jcc_true);
3514         assert(proj_true && "Jcc without true Proj");
3515
3516         proj_false = get_proj(node, pn_ia32_Jcc_false);
3517         assert(proj_false && "Jcc without false Proj");
3518
3519         block = get_nodes_block(node);
3520
3521         if (can_be_fallthrough(proj_true)) {
3522                 /* exchange both proj's so the second one can be omitted */
3523                 const ir_node *t = proj_true;
3524
3525                 proj_true  = proj_false;
3526                 proj_false = t;
3527                 pnc        = ia32_get_negated_pnc(pnc);
3528         }
3529
3530         dest_true  = get_cfop_target_block(proj_true);
3531         dest_false = get_cfop_target_block(proj_false);
3532
3533         if (pnc & ia32_pn_Cmp_float) {
3534                 /* Some floating point comparisons require a test of the parity flag,
3535                  * which indicates that the result is unordered */
3536                 switch (pnc & 15) {
3537                         case pn_Cmp_Uo: {
3538                                 bemit_jp(false, dest_true);
3539                                 break;
3540                         }
3541
3542                         case pn_Cmp_Leg:
3543                                 bemit_jp(true, dest_true);
3544                                 break;
3545
3546                         case pn_Cmp_Eq:
3547                         case pn_Cmp_Lt:
3548                         case pn_Cmp_Le:
3549                                 /* we need a local label if the false proj is a fallthrough
3550                                  * as the falseblock might have no label emitted then */
3551                                 if (can_be_fallthrough(proj_false)) {
3552                                         bemit8(0x7A);
3553                                         bemit8(0x06);  // jp + 6
3554                                 } else {
3555                                         bemit_jp(false, dest_false);
3556                                 }
3557                                 goto emit_jcc;
3558
3559                         case pn_Cmp_Ug:
3560                         case pn_Cmp_Uge:
3561                         case pn_Cmp_Ne:
3562                                 bemit_jp(false, dest_true);
3563                                 goto emit_jcc;
3564
3565                         default:
3566                                 goto emit_jcc;
3567                 }
3568         } else {
3569 emit_jcc:
3570                 bemit_jcc(pnc, dest_true);
3571         }
3572
3573         /* the second Proj might be a fallthrough */
3574         if (can_be_fallthrough(proj_false)) {
3575                 /* it's a fallthrough */
3576         } else {
3577                 bemit_jmp(dest_false);
3578         }
3579 }
3580
3581 static void bemit_switchjmp(const ir_node *node)
3582 {
3583         unsigned long          interval;
3584         int                    last_value;
3585         int                    i;
3586         jmp_tbl_t              tbl;
3587         const arch_register_t *in;
3588
3589         /* fill the table structure */
3590         generate_jump_table(&tbl, node);
3591
3592         /* two-complement's magic make this work without overflow */
3593         interval = tbl.max_value - tbl.min_value;
3594
3595         in = get_in_reg(node, 0);
3596         /* emit the table */
3597         if (get_signed_imm_size(interval) == 1) {
3598                 bemit8(0x83); // cmpl $imm8, %in
3599                 bemit_modru(in, 7);
3600                 bemit8(interval);
3601         } else {
3602                 bemit8(0x81); // cmpl $imm32, %in
3603                 bemit_modru(in, 7);
3604                 bemit32(interval);
3605         }
3606         bemit8(0x0F); // ja tbl.defProj
3607         bemit8(0x87);
3608         ia32_emitf(tbl.defProj, ".long %L - . - 4\n");
3609
3610         if (tbl.num_branches > 1) {
3611                 /* create table */
3612                 bemit8(0xFF); // jmp *tbl.label(,%in,4)
3613                 bemit8(MOD_IND | ENC_REG(4) | ENC_RM(0x04));
3614                 bemit8(ENC_SIB(2, reg_gp_map[in->index], 0x05));
3615                 be_emit_irprintf("\t.long %s\n", tbl.label);
3616
3617                 be_gas_emit_switch_section(GAS_SECTION_RODATA);
3618                 be_emit_cstring(".align 4\n");
3619                 be_emit_irprintf("%s:\n", tbl.label);
3620
3621                 last_value = tbl.branches[0].value;
3622                 for (i = 0; i != tbl.num_branches; ++i) {
3623                         while (last_value != tbl.branches[i].value) {
3624                                 ia32_emitf(tbl.defProj, ".long %L\n");
3625                                 ++last_value;
3626                         }
3627                         ia32_emitf(tbl.branches[i].target, ".long %L\n");
3628                         ++last_value;
3629                 }
3630                 be_gas_emit_switch_section(GAS_SECTION_TEXT);
3631         } else {
3632                 /* one jump is enough */
3633                 panic("switch only has one case");
3634                 //ia32_emitf(tbl.branches[0].target, "\tjmp %L\n");
3635         }
3636
3637         be_emit_write_line();
3638
3639         free(tbl.branches);
3640 }
3641
3642 /**
3643  * Emits a return.
3644  */
3645 static void bemit_return(const ir_node *node)
3646 {
3647         unsigned pop = be_Return_get_pop(node);
3648         if (pop > 0 || be_Return_get_emit_pop(node)) {
3649                 bemit8(0xC2);
3650                 assert(pop <= 0xffff);
3651                 bemit16(pop);
3652         } else {
3653                 bemit8(0xC3);
3654         }
3655 }
3656
3657 static void bemit_subsp(const ir_node *node)
3658 {
3659         const arch_register_t *out;
3660         /* sub %in, %esp */
3661         bemit_sub(node);
3662         /* mov %esp, %out */
3663         bemit8(0x8B);
3664         out = get_out_reg(node, 1);
3665         bemit8(MOD_REG | ENC_REG(reg_gp_map[out->index]) | ENC_RM(0x04));
3666 }
3667
3668 static void bemit_incsp(const ir_node *node)
3669 {
3670         int                    offs;
3671         const arch_register_t *reg;
3672         unsigned               size;
3673         unsigned               ext;
3674
3675         offs = be_get_IncSP_offset(node);
3676         if (offs == 0)
3677                 return;
3678
3679         if (offs > 0) {
3680                 ext = 5; /* sub */
3681         } else {
3682                 ext = 0; /* add */
3683                 offs = -offs;
3684         }
3685
3686         size = get_signed_imm_size(offs);
3687         bemit8(size == 1 ? 0x83 : 0x81);
3688
3689         reg  = get_out_reg(node, 0);
3690         bemit_modru(reg, ext);
3691
3692         if (size == 1) {
3693                 bemit8(offs);
3694         } else {
3695                 bemit32(offs);
3696         }
3697 }
3698
3699 static void bemit_copybi(const ir_node *node)
3700 {
3701         unsigned size = get_ia32_copyb_size(node);
3702         if (size & 1)
3703                 bemit8(0xA4); // movsb
3704         if (size & 2) {
3705                 bemit8(0x66);
3706                 bemit8(0xA5); // movsw
3707         }
3708         size >>= 2;
3709         while (size--) {
3710                 bemit8(0xA5); // movsl
3711         }
3712 }
3713
3714 static void bemit_fbinop(const ir_node *node, unsigned code, unsigned code_to)
3715 {
3716         if (get_ia32_op_type(node) == ia32_Normal) {
3717                 const ia32_x87_attr_t *x87_attr = get_ia32_x87_attr_const(node);
3718                 const arch_register_t *in1      = x87_attr->x87[0];
3719                 const arch_register_t *in       = x87_attr->x87[1];
3720                 const arch_register_t *out      = x87_attr->x87[2];
3721
3722                 if (out == NULL) {
3723                         out = in1;
3724                 } else if (out == in) {
3725                         in = in1;
3726                 }
3727
3728                 if (out->index == 0) {
3729                         bemit8(0xD8);
3730                         bemit8(MOD_REG | ENC_REG(code) | ENC_RM(in->index));
3731                 } else {
3732                         bemit8(0xDC);
3733                         bemit8(MOD_REG | ENC_REG(code_to) | ENC_RM(out->index));
3734                 }
3735         } else {
3736                 if (get_mode_size_bits(get_ia32_ls_mode(node)) == 32) {
3737                         bemit8(0xD8);
3738                 } else {
3739                         bemit8(0xDC);
3740                 }
3741                 bemit_mod_am(code, node);
3742         }
3743 }
3744
3745 static void bemit_fbinopp(const ir_node *node, unsigned const code)
3746 {
3747         const ia32_x87_attr_t *x87_attr = get_ia32_x87_attr_const(node);
3748         const arch_register_t *out      = x87_attr->x87[2];
3749         bemit8(0xDE);
3750         bemit8(code + out->index);
3751 }
3752
3753 static void bemit_fabs(const ir_node *node)
3754 {
3755         (void)node;
3756
3757         bemit8(0xD9);
3758         bemit8(0xE1);
3759 }
3760
3761 static void bemit_fadd(const ir_node *node)
3762 {
3763         bemit_fbinop(node, 0, 0);
3764 }
3765
3766 static void bemit_faddp(const ir_node *node)
3767 {
3768         bemit_fbinopp(node, 0xC0);
3769 }
3770
3771 static void bemit_fchs(const ir_node *node)
3772 {
3773         (void)node;
3774
3775         bemit8(0xD9);
3776         bemit8(0xE0);
3777 }
3778
3779 static void bemit_fdiv(const ir_node *node)
3780 {
3781         bemit_fbinop(node, 6, 7);
3782 }
3783
3784 static void bemit_fdivp(const ir_node *node)
3785 {
3786         bemit_fbinopp(node, 0xF8);
3787 }
3788
3789 static void bemit_fdivr(const ir_node *node)
3790 {
3791         bemit_fbinop(node, 7, 6);
3792 }
3793
3794 static void bemit_fdivrp(const ir_node *node)
3795 {
3796         bemit_fbinopp(node, 0xF0);
3797 }
3798
3799 static void bemit_fild(const ir_node *node)
3800 {
3801         switch (get_mode_size_bits(get_ia32_ls_mode(node))) {
3802                 case 16:
3803                         bemit8(0xDF); // filds
3804                         bemit_mod_am(0, node);
3805                         return;
3806
3807                 case 32:
3808                         bemit8(0xDB); // fildl
3809                         bemit_mod_am(0, node);
3810                         return;
3811
3812                 case 64:
3813                         bemit8(0xDF); // fildll
3814                         bemit_mod_am(5, node);
3815                         return;
3816
3817                 default:
3818                         panic("invalid mode size");
3819         }
3820 }
3821
3822 static void bemit_fist(const ir_node *node)
3823 {
3824         switch (get_mode_size_bits(get_ia32_ls_mode(node))) {
3825                 case 16:
3826                         bemit8(0xDF); // fists
3827                         break;
3828
3829                 case 32:
3830                         bemit8(0xDB); // fistl
3831                         break;
3832
3833                 default:
3834                         panic("invalid mode size");
3835         }
3836         bemit_mod_am(2, node);
3837 }
3838
3839 static void bemit_fistp(const ir_node *node)
3840 {
3841         switch (get_mode_size_bits(get_ia32_ls_mode(node))) {
3842                 case 16:
3843                         bemit8(0xDF); // fistps
3844                         bemit_mod_am(3, node);
3845                         return;
3846
3847                 case 32:
3848                         bemit8(0xDB); // fistpl
3849                         bemit_mod_am(3, node);
3850                         return;
3851
3852                 case 64:
3853                         bemit8(0xDF); // fistpll
3854                         bemit_mod_am(7, node);
3855                         return;
3856
3857                 default:
3858                         panic("invalid mode size");
3859         }
3860 }
3861
3862 static void bemit_fld(const ir_node *node)
3863 {
3864         switch (get_mode_size_bits(get_ia32_ls_mode(node))) {
3865                 case 32:
3866                         bemit8(0xD9); // flds
3867                         bemit_mod_am(0, node);
3868                         return;
3869
3870                 case 64:
3871                         bemit8(0xDD); // fldl
3872                         bemit_mod_am(0, node);
3873                         return;
3874
3875                 case 80:
3876                 case 96:
3877                         bemit8(0xDB); // fldt
3878                         bemit_mod_am(5, node);
3879                         return;
3880
3881                 default:
3882                         panic("invalid mode size");
3883         }
3884 }
3885
3886 static void bemit_fld1(const ir_node *node)
3887 {
3888         (void)node;
3889         bemit8(0xD9);
3890         bemit8(0xE8); // fld1
3891 }
3892
3893 static void bemit_fldcw(const ir_node *node)
3894 {
3895         bemit8(0xD9); // fldcw
3896         bemit_mod_am(5, node);
3897 }
3898
3899 static void bemit_fldz(const ir_node *node)
3900 {
3901         (void)node;
3902         bemit8(0xD9);
3903         bemit8(0xEE); // fldz
3904 }
3905
3906 static void bemit_fmul(const ir_node *node)
3907 {
3908         bemit_fbinop(node, 1, 1);
3909 }
3910
3911 static void bemit_fmulp(const ir_node *node)
3912 {
3913         bemit_fbinopp(node, 0xC8);
3914 }
3915
3916 static void bemit_fpop(const ir_node *node)
3917 {
3918         const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
3919         bemit8(0xDD);
3920         bemit8(0xD8 + attr->x87[0]->index);
3921 }
3922
3923 static void bemit_fpush(const ir_node *node)
3924 {
3925         const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
3926         bemit8(0xD9);
3927         bemit8(0xC0 + attr->x87[0]->index);
3928 }
3929
3930 static void bemit_fpushcopy(const ir_node *node)
3931 {
3932         const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
3933         bemit8(0xD9);
3934         bemit8(0xC0 + attr->x87[0]->index);
3935 }
3936
3937 static void bemit_fst(const ir_node *node)
3938 {
3939         switch (get_mode_size_bits(get_ia32_ls_mode(node))) {
3940                 case 32:
3941                         bemit8(0xD9); // fsts
3942                         break;
3943
3944                 case 64:
3945                         bemit8(0xDD); // fstl
3946                         break;
3947
3948                 default:
3949                         panic("invalid mode size");
3950         }
3951         bemit_mod_am(2, node);
3952 }
3953
3954 static void bemit_fstp(const ir_node *node)
3955 {
3956         switch (get_mode_size_bits(get_ia32_ls_mode(node))) {
3957                 case 32:
3958                         bemit8(0xD9); // fstps
3959                         bemit_mod_am(3, node);
3960                         return;
3961
3962                 case 64:
3963                         bemit8(0xDD); // fstpl
3964                         bemit_mod_am(3, node);
3965                         return;
3966
3967                 case 80:
3968                 case 96:
3969                         bemit8(0xDB); // fstpt
3970                         bemit_mod_am(7, node);
3971                         return;
3972
3973                 default:
3974                         panic("invalid mode size");
3975         }
3976 }
3977
3978 static void bemit_fsub(const ir_node *node)
3979 {
3980         bemit_fbinop(node, 4, 5);
3981 }
3982
3983 static void bemit_fsubp(const ir_node *node)
3984 {
3985         bemit_fbinopp(node, 0xE8);
3986 }
3987
3988 static void bemit_fsubr(const ir_node *node)
3989 {
3990         bemit_fbinop(node, 5, 4);
3991 }
3992
3993 static void bemit_fsubrp(const ir_node *node)
3994 {
3995         bemit_fbinopp(node, 0xE0);
3996 }
3997
3998 static void bemit_fnstcw(const ir_node *node)
3999 {
4000         bemit8(0xD9); // fnstcw
4001         bemit_mod_am(7, node);
4002 }
4003
4004 static void bemit_fnstsw(void)
4005 {
4006         bemit8(0xDF); // fnstsw %ax
4007         bemit8(0xE0);
4008 }
4009
4010 static void bemit_ftstfnstsw(const ir_node *node)
4011 {
4012         (void)node;
4013
4014         bemit8(0xD9); // ftst
4015         bemit8(0xE4);
4016         bemit_fnstsw();
4017 }
4018
4019 static void bemit_fucomi(const ir_node *node)
4020 {
4021         const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
4022         bemit8(0xDB); // fucomi
4023         bemit8(0xE8 + attr->x87[1]->index);
4024 }
4025
4026 static void bemit_fucomip(const ir_node *node)
4027 {
4028         const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
4029         bemit8(0xDF); // fucomip
4030         bemit8(0xE8 + attr->x87[1]->index);
4031 }
4032
4033 static void bemit_fucomfnstsw(const ir_node *node)
4034 {
4035         const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
4036         bemit8(0xDD); // fucom
4037         bemit8(0xE0 + attr->x87[1]->index);
4038         bemit_fnstsw();
4039 }
4040
4041 static void bemit_fucompfnstsw(const ir_node *node)
4042 {
4043         const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
4044         bemit8(0xDD); // fucomp
4045         bemit8(0xE8 + attr->x87[1]->index);
4046         bemit_fnstsw();
4047 }
4048
4049 static void bemit_fucomppfnstsw(const ir_node *node)
4050 {
4051         (void)node;
4052
4053         bemit8(0xDA); // fucompp
4054         bemit8(0xE9);
4055         bemit_fnstsw();
4056 }
4057
4058 static void bemit_fxch(const ir_node *node)
4059 {
4060         const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
4061         bemit8(0xD9);
4062         bemit8(0xC8 + attr->x87[0]->index);
4063 }
4064
4065 /**
4066  * The type of a emitter function.
4067  */
4068 typedef void (*emit_func) (const ir_node *);
4069
4070 /**
4071  * Set a node emitter. Make it a bit more type safe.
4072  */
4073 static void register_emitter(ir_op *op, emit_func func)
4074 {
4075         op->ops.generic = (op_func) func;
4076 }
4077
4078 static void ia32_register_binary_emitters(void)
4079 {
4080         /* first clear the generic function pointer for all ops */
4081         clear_irp_opcodes_generic_func();
4082
4083         /* benode emitter */
4084         register_emitter(op_be_Copy,            bemit_copy);
4085         register_emitter(op_be_CopyKeep,        bemit_copy);
4086         register_emitter(op_be_IncSP,           bemit_incsp);
4087         register_emitter(op_be_Perm,            bemit_perm);
4088         register_emitter(op_be_Return,          bemit_return);
4089         register_emitter(op_ia32_Adc,           bemit_adc);
4090         register_emitter(op_ia32_Add,           bemit_add);
4091         register_emitter(op_ia32_AddMem,        bemit_addmem);
4092         register_emitter(op_ia32_AddMem8Bit,    bemit_addmem8bit);
4093         register_emitter(op_ia32_And,           bemit_and);
4094         register_emitter(op_ia32_AndMem,        bemit_andmem);
4095         register_emitter(op_ia32_AndMem8Bit,    bemit_andmem8bit);
4096         register_emitter(op_ia32_Breakpoint,    bemit_int3);
4097         register_emitter(op_ia32_CMovcc,        bemit_cmovcc);
4098         register_emitter(op_ia32_Call,          bemit_call);
4099         register_emitter(op_ia32_Cltd,          bemit_cltd);
4100         register_emitter(op_ia32_Cmc,           bemit_cmc);
4101         register_emitter(op_ia32_Cmp,           bemit_cmp);
4102         register_emitter(op_ia32_Cmp8Bit,       bemit_cmp8bit);
4103         register_emitter(op_ia32_Const,         bemit_mov_const);
4104         register_emitter(op_ia32_Conv_I2I,      bemit_conv_i2i);
4105         register_emitter(op_ia32_Conv_I2I8Bit,  bemit_conv_i2i);
4106         register_emitter(op_ia32_CopyB_i,       bemit_copybi);
4107         register_emitter(op_ia32_Cwtl,          bemit_cwtl);
4108         register_emitter(op_ia32_Dec,           bemit_dec);
4109         register_emitter(op_ia32_DecMem,        bemit_decmem);
4110         register_emitter(op_ia32_Div,           bemit_div);
4111         register_emitter(op_ia32_FldCW,         bemit_fldcw);
4112         register_emitter(op_ia32_FnstCW,        bemit_fnstcw);
4113         register_emitter(op_ia32_FtstFnstsw,    bemit_ftstfnstsw);
4114         register_emitter(op_ia32_FucomFnstsw,   bemit_fucomfnstsw);
4115         register_emitter(op_ia32_Fucomi,        bemit_fucomi);
4116         register_emitter(op_ia32_FucompFnstsw,  bemit_fucompfnstsw);
4117         register_emitter(op_ia32_Fucompi,       bemit_fucomip);
4118         register_emitter(op_ia32_FucomppFnstsw, bemit_fucomppfnstsw);
4119         register_emitter(op_ia32_IDiv,          bemit_idiv);
4120         register_emitter(op_ia32_IJmp,          bemit_ijmp);
4121         register_emitter(op_ia32_IMul,          bemit_imul);
4122         register_emitter(op_ia32_IMul1OP,       bemit_imul1op);
4123         register_emitter(op_ia32_Inc,           bemit_inc);
4124         register_emitter(op_ia32_IncMem,        bemit_incmem);
4125         register_emitter(op_ia32_Jcc,           bemit_ia32_jcc);
4126         register_emitter(op_ia32_Jmp,           bemit_jump);
4127         register_emitter(op_ia32_LdTls,         bemit_ldtls);
4128         register_emitter(op_ia32_Lea,           bemit_lea);
4129         register_emitter(op_ia32_Leave,         bemit_leave);
4130         register_emitter(op_ia32_Load,          bemit_load);
4131         register_emitter(op_ia32_Minus64Bit,    bemit_minus64bit);
4132         register_emitter(op_ia32_Mul,           bemit_mul);
4133         register_emitter(op_ia32_Neg,           bemit_neg);
4134         register_emitter(op_ia32_NegMem,        bemit_negmem);
4135         register_emitter(op_ia32_Not,           bemit_not);
4136         register_emitter(op_ia32_NotMem,        bemit_notmem);
4137         register_emitter(op_ia32_Or,            bemit_or);
4138         register_emitter(op_ia32_OrMem,         bemit_ormem);
4139         register_emitter(op_ia32_OrMem8Bit,     bemit_ormem8bit);
4140         register_emitter(op_ia32_Pop,           bemit_pop);
4141         register_emitter(op_ia32_PopEbp,        bemit_pop);
4142         register_emitter(op_ia32_PopMem,        bemit_popmem);
4143         register_emitter(op_ia32_Push,          bemit_push);
4144         register_emitter(op_ia32_RepPrefix,     bemit_rep);
4145         register_emitter(op_ia32_Rol,           bemit_rol);
4146         register_emitter(op_ia32_RolMem,        bemit_rolmem);
4147         register_emitter(op_ia32_Ror,           bemit_ror);
4148         register_emitter(op_ia32_RorMem,        bemit_rormem);
4149         register_emitter(op_ia32_Sahf,          bemit_sahf);
4150         register_emitter(op_ia32_Sar,           bemit_sar);
4151         register_emitter(op_ia32_SarMem,        bemit_sarmem);
4152         register_emitter(op_ia32_Sbb,           bemit_sbb);
4153         register_emitter(op_ia32_Setcc,         bemit_setcc);
4154         register_emitter(op_ia32_Shl,           bemit_shl);
4155         register_emitter(op_ia32_ShlD,          bemit_shld);
4156         register_emitter(op_ia32_ShlMem,        bemit_shlmem);
4157         register_emitter(op_ia32_Shr,           bemit_shr);
4158         register_emitter(op_ia32_ShrD,          bemit_shrd);
4159         register_emitter(op_ia32_ShrMem,        bemit_shrmem);
4160         register_emitter(op_ia32_Stc,           bemit_stc);
4161         register_emitter(op_ia32_Store,         bemit_store);
4162         register_emitter(op_ia32_Store8Bit,     bemit_store);
4163         register_emitter(op_ia32_Sub,           bemit_sub);
4164         register_emitter(op_ia32_SubMem,        bemit_submem);
4165         register_emitter(op_ia32_SubMem8Bit,    bemit_submem8bit);
4166         register_emitter(op_ia32_SubSP,         bemit_subsp);
4167         register_emitter(op_ia32_SwitchJmp,     bemit_switchjmp);
4168         register_emitter(op_ia32_Test,          bemit_test);
4169         register_emitter(op_ia32_Test8Bit,      bemit_test8bit);
4170         register_emitter(op_ia32_Xor,           bemit_xor);
4171         register_emitter(op_ia32_Xor0,          bemit_xor0);
4172         register_emitter(op_ia32_XorMem,        bemit_xormem);
4173         register_emitter(op_ia32_XorMem8Bit,    bemit_xormem8bit);
4174         register_emitter(op_ia32_fabs,          bemit_fabs);
4175         register_emitter(op_ia32_fadd,          bemit_fadd);
4176         register_emitter(op_ia32_faddp,         bemit_faddp);
4177         register_emitter(op_ia32_fchs,          bemit_fchs);
4178         register_emitter(op_ia32_fdiv,          bemit_fdiv);
4179         register_emitter(op_ia32_fdivp,         bemit_fdivp);
4180         register_emitter(op_ia32_fdivr,         bemit_fdivr);
4181         register_emitter(op_ia32_fdivrp,        bemit_fdivrp);
4182         register_emitter(op_ia32_fild,          bemit_fild);
4183         register_emitter(op_ia32_fist,          bemit_fist);
4184         register_emitter(op_ia32_fistp,         bemit_fistp);
4185         register_emitter(op_ia32_fld,           bemit_fld);
4186         register_emitter(op_ia32_fld1,          bemit_fld1);
4187         register_emitter(op_ia32_fldz,          bemit_fldz);
4188         register_emitter(op_ia32_fmul,          bemit_fmul);
4189         register_emitter(op_ia32_fmulp,         bemit_fmulp);
4190         register_emitter(op_ia32_fpop,          bemit_fpop);
4191         register_emitter(op_ia32_fpush,         bemit_fpush);
4192         register_emitter(op_ia32_fpushCopy,     bemit_fpushcopy);
4193         register_emitter(op_ia32_fst,           bemit_fst);
4194         register_emitter(op_ia32_fstp,          bemit_fstp);
4195         register_emitter(op_ia32_fsub,          bemit_fsub);
4196         register_emitter(op_ia32_fsubp,         bemit_fsubp);
4197         register_emitter(op_ia32_fsubr,         bemit_fsubr);
4198         register_emitter(op_ia32_fsubrp,        bemit_fsubrp);
4199         register_emitter(op_ia32_fxch,          bemit_fxch);
4200
4201         /* ignore the following nodes */
4202         register_emitter(op_ia32_ProduceVal,   emit_Nothing);
4203         register_emitter(op_be_Barrier,        emit_Nothing);
4204         register_emitter(op_be_Keep,           emit_Nothing);
4205         register_emitter(op_be_Start,          emit_Nothing);
4206         register_emitter(op_Phi,               emit_Nothing);
4207         register_emitter(op_Start,             emit_Nothing);
4208 }
4209
4210 static void gen_binary_block(ir_node *block)
4211 {
4212         ir_node *node;
4213
4214         ia32_emit_block_header(block);
4215
4216         /* emit the contents of the block */
4217         sched_foreach(block, node) {
4218                 ia32_emit_node(node);
4219         }
4220 }
4221
4222 void ia32_gen_binary_routine(ir_graph *irg)
4223 {
4224         ir_entity        *entity    = get_irg_entity(irg);
4225         const arch_env_t *arch_env  = be_get_irg_arch_env(irg);
4226         ia32_irg_data_t  *irg_data  = ia32_get_irg_data(irg);
4227         ir_node         **blk_sched = irg_data->blk_sched;
4228         int i, n;
4229
4230         isa = (ia32_isa_t*) arch_env;
4231
4232         ia32_register_binary_emitters();
4233
4234         be_gas_emit_function_prolog(entity, ia32_cg_config.function_alignment);
4235
4236         /* we use links to point to target blocks */
4237         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
4238         irg_block_walk_graph(irg, ia32_gen_labels, NULL, NULL);
4239
4240         /* initialize next block links */
4241         n = ARR_LEN(blk_sched);
4242         for (i = 0; i < n; ++i) {
4243                 ir_node *block = blk_sched[i];
4244                 ir_node *prev  = i > 0 ? blk_sched[i-1] : NULL;
4245
4246                 set_irn_link(block, prev);
4247         }
4248
4249         for (i = 0; i < n; ++i) {
4250                 ir_node *block = blk_sched[i];
4251                 gen_binary_block(block);
4252         }
4253
4254         be_gas_emit_function_epilog(entity);
4255         be_dbg_method_end();
4256         be_emit_char('\n');
4257         be_emit_write_line();
4258
4259         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
4260 }
4261
4262
4263 void ia32_init_emitter(void)
4264 {
4265         lc_opt_entry_t *be_grp;
4266         lc_opt_entry_t *ia32_grp;
4267
4268         be_grp   = lc_opt_get_grp(firm_opt_get_root(), "be");
4269         ia32_grp = lc_opt_get_grp(be_grp, "ia32");
4270
4271         lc_opt_add_table(ia32_grp, ia32_emitter_options);
4272
4273         build_reg_map();
4274
4275         FIRM_DBG_REGISTER(dbg, "firm.be.ia32.emitter");
4276 }