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