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