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