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