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