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