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