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