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