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