Rework Block labels: They are entities now so we don't need a special symconst type...
[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 "execfreq.h"
41 #include "error.h"
42 #include "raw_bitset.h"
43 #include "dbginfo.h"
44
45 #include "../besched_t.h"
46 #include "../benode_t.h"
47 #include "../beabi.h"
48 #include "../be_dbgout.h"
49 #include "../beemitter.h"
50 #include "../begnuas.h"
51 #include "../beirg_t.h"
52 #include "../be_dbgout.h"
53
54 #include "ia32_emitter.h"
55 #include "gen_ia32_emitter.h"
56 #include "gen_ia32_regalloc_if.h"
57 #include "ia32_nodes_attr.h"
58 #include "ia32_new_nodes.h"
59 #include "ia32_map_regs.h"
60 #include "ia32_architecture.h"
61 #include "bearch_ia32_t.h"
62
63 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
64
65 #define BLOCK_PREFIX ".L"
66
67 #define SNPRINTF_BUF_LEN 128
68
69 static const ia32_isa_t *isa;
70 static ia32_code_gen_t  *cg;
71 static char              pic_base_label[128];
72 static ir_label_t        exc_label_id;
73 static int               mark_spill_reload = 0;
74 static int               do_pic;
75
76 /** Return the next block in Block schedule */
77 static ir_node *get_prev_block_sched(const ir_node *block)
78 {
79         return get_irn_link(block);
80 }
81
82 /** Checks if the current block is a fall-through target. */
83 static int is_fallthrough(const ir_node *cfgpred)
84 {
85         ir_node *pred;
86
87         if (!is_Proj(cfgpred))
88                 return 1;
89         pred = get_Proj_pred(cfgpred);
90         if (is_ia32_SwitchJmp(pred))
91                 return 0;
92
93         return 1;
94 }
95
96 /**
97  * returns non-zero if the given block needs a label
98  * because of being a jump-target (and not a fall-through)
99  */
100 static int block_needs_label(const ir_node *block)
101 {
102         int need_label = 1;
103         int  n_cfgpreds = get_Block_n_cfgpreds(block);
104
105         if (has_Block_entity(block))
106                 return 1;
107
108         if (n_cfgpreds == 0) {
109                 need_label = 0;
110         } else if (n_cfgpreds == 1) {
111                 ir_node *cfgpred       = get_Block_cfgpred(block, 0);
112                 ir_node *cfgpred_block = get_nodes_block(cfgpred);
113
114                 if (get_prev_block_sched(block) == cfgpred_block
115                                 && is_fallthrough(cfgpred)) {
116                         need_label = 0;
117                 }
118         }
119
120         return need_label;
121 }
122
123 /**
124  * Returns the register at in position pos.
125  */
126 static const arch_register_t *get_in_reg(const ir_node *irn, int pos)
127 {
128         ir_node               *op;
129         const arch_register_t *reg = NULL;
130
131         assert(get_irn_arity(irn) > pos && "Invalid IN position");
132
133         /* The out register of the operator at position pos is the
134            in register we need. */
135         op = get_irn_n(irn, pos);
136
137         reg = arch_get_irn_register(op);
138
139         assert(reg && "no in register found");
140
141         if (reg == &ia32_gp_regs[REG_GP_NOREG])
142                 panic("trying to emit noreg for %+F input %d", irn, pos);
143
144         /* in case of unknown register: just return a valid register */
145         if (reg == &ia32_gp_regs[REG_GP_UKNWN]) {
146                 const arch_register_req_t *req = arch_get_register_req(irn, pos);
147
148                 if (arch_register_req_is(req, limited)) {
149                         /* in case of limited requirements: get the first allowed register */
150                         unsigned idx = rbitset_next(req->limited, 0, 1);
151                         reg = arch_register_for_index(req->cls, idx);
152                 } else {
153                         /* otherwise get first register in class */
154                         reg = arch_register_for_index(req->cls, 0);
155                 }
156         }
157
158         return reg;
159 }
160
161 /**
162  * Returns the register at out position pos.
163  */
164 static const arch_register_t *get_out_reg(const ir_node *irn, int pos)
165 {
166         ir_node               *proj;
167         const arch_register_t *reg = NULL;
168
169         /* 1st case: irn is not of mode_T, so it has only                 */
170         /*           one OUT register -> good                             */
171         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
172         /*           Proj with the corresponding projnum for the register */
173
174         if (get_irn_mode(irn) != mode_T) {
175                 assert(pos == 0);
176                 reg = arch_get_irn_register(irn);
177         } else if (is_ia32_irn(irn)) {
178                 reg = arch_irn_get_register(irn, pos);
179         } else {
180                 const ir_edge_t *edge;
181
182                 foreach_out_edge(irn, edge) {
183                         proj = get_edge_src_irn(edge);
184                         assert(is_Proj(proj) && "non-Proj from mode_T node");
185                         if (get_Proj_proj(proj) == pos) {
186                                 reg = arch_get_irn_register(proj);
187                                 break;
188                         }
189                 }
190         }
191
192         assert(reg && "no out register found");
193         return reg;
194 }
195
196 /**
197  * Add a number to a prefix. This number will not be used a second time.
198  */
199 static char *get_unique_label(char *buf, size_t buflen, const char *prefix)
200 {
201         static unsigned long id = 0;
202         snprintf(buf, buflen, "%s%lu", prefix, ++id);
203         return buf;
204 }
205
206 /*************************************************************
207  *             _       _    __   _          _
208  *            (_)     | |  / _| | |        | |
209  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
210  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
211  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
212  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
213  * | |                                       | |
214  * |_|                                       |_|
215  *************************************************************/
216
217 /**
218  * Emit the name of the 8bit low register
219  */
220 static void emit_8bit_register(const arch_register_t *reg)
221 {
222         const char *reg_name = arch_register_get_name(reg);
223
224         be_emit_char('%');
225         be_emit_char(reg_name[1]);
226         be_emit_char('l');
227 }
228
229 /**
230  * Emit the name of the 8bit high register
231  */
232 static void emit_8bit_register_high(const arch_register_t *reg)
233 {
234         const char *reg_name = arch_register_get_name(reg);
235
236         be_emit_char('%');
237         be_emit_char(reg_name[1]);
238         be_emit_char('h');
239 }
240
241 static void emit_16bit_register(const arch_register_t *reg)
242 {
243         const char *reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
244
245         be_emit_char('%');
246         be_emit_string(reg_name);
247 }
248
249 /**
250  * emit a register, possible shortened by a mode
251  *
252  * @param reg   the register
253  * @param mode  the mode of the register or NULL for full register
254  */
255 static void emit_register(const arch_register_t *reg, const ir_mode *mode)
256 {
257         const char *reg_name;
258
259         if (mode != NULL) {
260                 int size = get_mode_size_bits(mode);
261                 switch (size) {
262                         case  8: emit_8bit_register(reg);  return;
263                         case 16: emit_16bit_register(reg); return;
264                 }
265                 assert(mode_is_float(mode) || size == 32);
266         }
267
268         reg_name = arch_register_get_name(reg);
269
270         be_emit_char('%');
271         be_emit_string(reg_name);
272 }
273
274 void ia32_emit_source_register(const ir_node *node, int pos)
275 {
276         const arch_register_t *reg = get_in_reg(node, pos);
277
278         emit_register(reg, NULL);
279 }
280
281 static void ia32_emit_entity(ir_entity *entity, int no_pic_adjust)
282 {
283         set_entity_backend_marked(entity, 1);
284         be_gas_emit_entity(entity);
285
286         if (get_entity_owner(entity) == get_tls_type()) {
287                 if (get_entity_visibility(entity) == visibility_external_allocated) {
288                         be_emit_cstring("@INDNTPOFF");
289                 } else {
290                         be_emit_cstring("@NTPOFF");
291                 }
292         }
293
294         if (do_pic && !no_pic_adjust) {
295                 be_emit_char('-');
296                 be_emit_string(pic_base_label);
297         }
298 }
299
300 static void emit_ia32_Immediate_no_prefix(const ir_node *node)
301 {
302         const ia32_immediate_attr_t *attr = get_ia32_immediate_attr_const(node);
303
304         if (attr->symconst != NULL) {
305                 if (attr->sc_sign)
306                         be_emit_char('-');
307                 ia32_emit_entity(attr->symconst, attr->no_pic_adjust);
308         }
309         if (attr->symconst == NULL || attr->offset != 0) {
310                 if (attr->symconst != NULL) {
311                         be_emit_irprintf("%+d", attr->offset);
312                 } else {
313                         be_emit_irprintf("0x%X", attr->offset);
314                 }
315         }
316 }
317
318 static void emit_ia32_Immediate(const ir_node *node)
319 {
320         be_emit_char('$');
321         emit_ia32_Immediate_no_prefix(node);
322 }
323
324 void ia32_emit_8bit_source_register_or_immediate(const ir_node *node, int pos)
325 {
326         const arch_register_t *reg;
327         const ir_node         *in = get_irn_n(node, pos);
328         if (is_ia32_Immediate(in)) {
329                 emit_ia32_Immediate(in);
330                 return;
331         }
332
333         reg = get_in_reg(node, pos);
334         emit_8bit_register(reg);
335 }
336
337 void ia32_emit_8bit_high_source_register(const ir_node *node, int pos)
338 {
339         const arch_register_t *reg = get_in_reg(node, pos);
340         emit_8bit_register_high(reg);
341 }
342
343 void ia32_emit_16bit_source_register_or_immediate(const ir_node *node, int pos)
344 {
345         const arch_register_t *reg;
346         const ir_node         *in = get_irn_n(node, pos);
347         if (is_ia32_Immediate(in)) {
348                 emit_ia32_Immediate(in);
349                 return;
350         }
351
352         reg = get_in_reg(node, pos);
353         emit_16bit_register(reg);
354 }
355
356 void ia32_emit_dest_register(const ir_node *node, int pos)
357 {
358         const arch_register_t *reg  = get_out_reg(node, pos);
359
360         emit_register(reg, NULL);
361 }
362
363 void ia32_emit_dest_register_size(const ir_node *node, int pos)
364 {
365         const arch_register_t *reg  = get_out_reg(node, pos);
366
367         emit_register(reg, get_ia32_ls_mode(node));
368 }
369
370 void ia32_emit_8bit_dest_register(const ir_node *node, int pos)
371 {
372         const arch_register_t *reg  = get_out_reg(node, pos);
373
374         emit_register(reg, mode_Bu);
375 }
376
377 void ia32_emit_x87_register(const ir_node *node, int pos)
378 {
379         const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
380
381         assert(pos < 3);
382         be_emit_char('%');
383         be_emit_string(attr->x87[pos]->name);
384 }
385
386 static void ia32_emit_mode_suffix_mode(const ir_mode *mode)
387 {
388         assert(mode_is_int(mode) || mode_is_reference(mode));
389         switch (get_mode_size_bits(mode)) {
390                 case 8:  be_emit_char('b');     return;
391                 case 16: be_emit_char('w');     return;
392                 case 32: be_emit_char('l');     return;
393                 /* gas docu says q is the suffix but gcc, objdump and icc use ll
394                  * apparently */
395                 case 64: be_emit_cstring("ll"); return;
396         }
397         panic("Can't output mode_suffix for %+F", mode);
398 }
399
400 void ia32_emit_mode_suffix(const ir_node *node)
401 {
402         ir_mode *mode = get_ia32_ls_mode(node);
403         if (mode == NULL)
404                 mode = mode_Iu;
405
406         ia32_emit_mode_suffix_mode(mode);
407 }
408
409 void ia32_emit_x87_mode_suffix(const ir_node *node)
410 {
411         ir_mode *mode;
412
413         /* we only need to emit the mode on address mode */
414         if (get_ia32_op_type(node) == ia32_Normal)
415                 return;
416
417         mode = get_ia32_ls_mode(node);
418         assert(mode != NULL);
419
420         if (mode_is_float(mode)) {
421                 switch (get_mode_size_bits(mode)) {
422                         case 32: be_emit_char('s'); return;
423                         case 64: be_emit_char('l'); return;
424                         case 80:
425                         case 96: be_emit_char('t'); return;
426                 }
427         } else {
428                 assert(mode_is_int(mode));
429                 switch (get_mode_size_bits(mode)) {
430                         case 16: be_emit_char('s');     return;
431                         case 32: be_emit_char('l');     return;
432                         /* gas docu says q is the suffix but gcc, objdump and icc use ll
433                          * apparently */
434                         case 64: be_emit_cstring("ll"); return;
435                 }
436         }
437         panic("Can't output mode_suffix for %+F", mode);
438 }
439
440 static char get_xmm_mode_suffix(ir_mode *mode)
441 {
442         assert(mode_is_float(mode));
443         switch(get_mode_size_bits(mode)) {
444         case 32: return 's';
445         case 64: return 'd';
446         default: panic("Invalid XMM mode");
447         }
448 }
449
450 void ia32_emit_xmm_mode_suffix(const ir_node *node)
451 {
452         ir_mode *mode = get_ia32_ls_mode(node);
453         assert(mode != NULL);
454         be_emit_char('s');
455         be_emit_char(get_xmm_mode_suffix(mode));
456 }
457
458 void ia32_emit_xmm_mode_suffix_s(const ir_node *node)
459 {
460         ir_mode *mode = get_ia32_ls_mode(node);
461         assert(mode != NULL);
462         be_emit_char(get_xmm_mode_suffix(mode));
463 }
464
465 void ia32_emit_extend_suffix(const ir_node *node)
466 {
467         ir_mode *mode = get_ia32_ls_mode(node);
468         if (get_mode_size_bits(mode) == 32)
469                 return;
470         be_emit_char(mode_is_signed(mode) ? 's' : 'z');
471         ia32_emit_mode_suffix_mode(mode);
472 }
473
474 void ia32_emit_source_register_or_immediate(const ir_node *node, int pos)
475 {
476         ir_node *in = get_irn_n(node, pos);
477         if (is_ia32_Immediate(in)) {
478                 emit_ia32_Immediate(in);
479         } else {
480                 const ir_mode         *mode = get_ia32_ls_mode(node);
481                 const arch_register_t *reg  = get_in_reg(node, pos);
482                 emit_register(reg, mode);
483         }
484 }
485
486 /**
487  * Returns the target block for a control flow node.
488  */
489 static ir_node *get_cfop_target_block(const ir_node *irn)
490 {
491         assert(get_irn_mode(irn) == mode_X);
492         return get_irn_link(irn);
493 }
494
495 /**
496  * Emits a block label for the given block.
497  */
498 static void ia32_emit_block_name(const ir_node *block)
499 {
500         if (has_Block_entity(block)) {
501                 ir_entity *entity = get_Block_entity(block);
502                 be_gas_emit_entity(entity);
503         } else {
504                 be_emit_cstring(BLOCK_PREFIX);
505                 be_emit_irprintf("%ld", get_irn_node_nr(block));
506         }
507 }
508
509 /**
510  * Emits the target label for a control flow node.
511  */
512 static void ia32_emit_cfop_target(const ir_node *node)
513 {
514         ir_node *block = get_cfop_target_block(node);
515         ia32_emit_block_name(block);
516 }
517
518 /*
519  * positive conditions for signed compares
520  */
521 static const char *const cmp2condition_s[] = {
522         NULL, /* always false */
523         "e",  /* == */
524         "l",  /* <  */
525         "le", /* <= */
526         "g",  /* >  */
527         "ge", /* >= */
528         "ne", /* != */
529         NULL  /* always true */
530 };
531
532 /*
533  * positive conditions for unsigned compares
534  */
535 static const char *const cmp2condition_u[] = {
536         NULL, /* always false */
537         "e",  /* == */
538         "b",  /* <  */
539         "be", /* <= */
540         "a",  /* >  */
541         "ae", /* >= */
542         "ne", /* != */
543         NULL  /* always true */
544 };
545
546 /**
547  * Emit the suffix for a compare instruction.
548  */
549 static void ia32_emit_cmp_suffix(int pnc)
550 {
551         const char *str;
552
553         if (pnc == ia32_pn_Cmp_parity) {
554                 be_emit_char('p');
555                 return;
556         }
557         if (pnc & ia32_pn_Cmp_float || pnc & ia32_pn_Cmp_unsigned) {
558                 str = cmp2condition_u[pnc & 7];
559         } else {
560                 str = cmp2condition_s[pnc & 7];
561         }
562
563         be_emit_string(str);
564 }
565
566 typedef enum ia32_emit_mod_t {
567         EMIT_RESPECT_LS   = 1U << 0,
568         EMIT_ALTERNATE_AM = 1U << 1,
569         EMIT_LONG         = 1U << 2
570 } ia32_emit_mod_t;
571
572 /**
573  * Emits address mode.
574  */
575 void ia32_emit_am(const ir_node *node)
576 {
577         ir_entity *ent       = get_ia32_am_sc(node);
578         int        offs      = get_ia32_am_offs_int(node);
579         ir_node   *base      = get_irn_n(node, n_ia32_base);
580         int        has_base  = !is_ia32_NoReg_GP(base);
581         ir_node   *index     = get_irn_n(node, n_ia32_index);
582         int        has_index = !is_ia32_NoReg_GP(index);
583
584         /* just to be sure... */
585         assert(!is_ia32_use_frame(node) || get_ia32_frame_ent(node) != NULL);
586
587         /* emit offset */
588         if (ent != NULL) {
589                 const ia32_attr_t *attr = get_ia32_attr_const(node);
590                 if (is_ia32_am_sc_sign(node))
591                         be_emit_char('-');
592                 ia32_emit_entity(ent, attr->data.am_sc_no_pic_adjust);
593         }
594
595         /* also handle special case if nothing is set */
596         if (offs != 0 || (ent == NULL && !has_base && !has_index)) {
597                 if (ent != NULL) {
598                         be_emit_irprintf("%+d", offs);
599                 } else {
600                         be_emit_irprintf("%d", offs);
601                 }
602         }
603
604         if (has_base || has_index) {
605                 be_emit_char('(');
606
607                 /* emit base */
608                 if (has_base) {
609                         const arch_register_t *reg = get_in_reg(node, n_ia32_base);
610                         emit_register(reg, NULL);
611                 }
612
613                 /* emit index + scale */
614                 if (has_index) {
615                         const arch_register_t *reg = get_in_reg(node, n_ia32_index);
616                         int scale;
617                         be_emit_char(',');
618                         emit_register(reg, NULL);
619
620                         scale = get_ia32_am_scale(node);
621                         if (scale > 0) {
622                                 be_emit_irprintf(",%d", 1 << scale);
623                         }
624                 }
625                 be_emit_char(')');
626         }
627 }
628
629 /**
630  * fmt  parameter               output
631  * ---- ----------------------  ---------------------------------------------
632  * %%                           %
633  * %AM  <node>                  address mode of the node
634  * %AR  const arch_register_t*  address mode of the node or register
635  * %ASx <node>                  address mode of the node or source register x
636  * %Dx  <node>                  destination register x
637  * %I   <node>                  immediate of the node
638  * %L   <node>                  control flow target of the node
639  * %M   <node>                  mode suffix of the node
640  * %P   int                     condition code
641  * %R   const arch_register_t*  register
642  * %Sx  <node>                  source register x
643  * %s   const char*             string
644  * %u   unsigned int            unsigned int
645  * %d   signed int              signed int
646  *
647  * x starts at 0
648  * # modifier for %ASx, %D and %S uses ls mode of node to alter register width
649  * * modifier does not prefix immediates with $, but AM with *
650  * l modifier for %lu and %ld
651  */
652 static void ia32_emitf(const ir_node *node, const char *fmt, ...)
653 {
654         va_list ap;
655         va_start(ap, fmt);
656
657         for (;;) {
658                 const char      *start = fmt;
659                 ia32_emit_mod_t  mod   = 0;
660
661                 while (*fmt != '%' && *fmt != '\n' && *fmt != '\0')
662                         ++fmt;
663                 if (fmt != start) {
664                         be_emit_string_len(start, fmt - start);
665                 }
666
667                 if (*fmt == '\n') {
668                         be_emit_finish_line_gas(node);
669                         ++fmt;
670                         if (*fmt == '\0')
671                                 break;
672                         continue;
673                 }
674
675                 if (*fmt == '\0')
676                         break;
677
678                 ++fmt;
679                 if (*fmt == '*') {
680                         mod |= EMIT_ALTERNATE_AM;
681                         ++fmt;
682                 }
683
684                 if (*fmt == '#') {
685                         mod |= EMIT_RESPECT_LS;
686                         ++fmt;
687                 }
688
689                 if (*fmt == 'l') {
690                         mod |= EMIT_LONG;
691                         ++fmt;
692                 }
693
694                 switch (*fmt++) {
695                         case '%':
696                                 be_emit_char('%');
697                                 break;
698
699                         case 'A': {
700                                 switch (*fmt++) {
701                                         case 'M':
702                                                 if (mod & EMIT_ALTERNATE_AM)
703                                                         be_emit_char('*');
704
705                                                 ia32_emit_am(node);
706                                                 break;
707
708                                         case 'R': {
709                                                 const arch_register_t *reg = va_arg(ap, const arch_register_t*);
710                                                 if (mod & EMIT_ALTERNATE_AM)
711                                                         be_emit_char('*');
712                                                 if (get_ia32_op_type(node) == ia32_AddrModeS) {
713                                                         ia32_emit_am(node);
714                                                 } else {
715                                                         emit_register(reg, NULL);
716                                                 }
717                                                 break;
718                                         }
719
720                                         case 'S':
721                                                 if (get_ia32_op_type(node) == ia32_AddrModeS) {
722                                                         if (mod & EMIT_ALTERNATE_AM)
723                                                                 be_emit_char('*');
724                                                         ia32_emit_am(node);
725                                                         ++fmt;
726                                                 } else {
727                                                         assert(get_ia32_op_type(node) == ia32_Normal);
728                                                         goto emit_S;
729                                                 }
730                                                 break;
731
732                                         default: goto unknown;
733                                 }
734                                 break;
735                         }
736
737                         case 'D': {
738                                 unsigned               pos;
739                                 const arch_register_t *reg;
740
741                                 if (*fmt < '0' || '9' <= *fmt)
742                                         goto unknown;
743
744                                 pos = *fmt++ - '0';
745                                 reg = get_out_reg(node, pos);
746                                 emit_register(reg, mod & EMIT_RESPECT_LS ? get_ia32_ls_mode(node) : NULL);
747                                 break;
748                         }
749
750                         case 'I':
751                                 if (!(mod & EMIT_ALTERNATE_AM))
752                                         be_emit_char('$');
753                                 emit_ia32_Immediate_no_prefix(node);
754                                 break;
755
756                         case 'L':
757                                 ia32_emit_cfop_target(node);
758                                 break;
759
760                         case 'M': {
761                                 ia32_emit_mode_suffix_mode(get_ia32_ls_mode(node));
762                                 break;
763                         }
764
765                         case 'P': {
766                                 int pnc = va_arg(ap, int);
767                                 ia32_emit_cmp_suffix(pnc);
768                                 break;
769                         }
770
771                         case 'R': {
772                                 const arch_register_t *reg = va_arg(ap, const arch_register_t*);
773                                 emit_register(reg, NULL);
774                                 break;
775                         }
776
777 emit_S:
778                         case 'S': {
779                                 unsigned       pos;
780                                 const ir_node *in;
781
782                                 if (*fmt < '0' || '9' <= *fmt)
783                                         goto unknown;
784
785                                 pos = *fmt++ - '0';
786                                 in  = get_irn_n(node, pos);
787                                 if (is_ia32_Immediate(in)) {
788                                         if (!(mod & EMIT_ALTERNATE_AM))
789                                                 be_emit_char('$');
790                                         emit_ia32_Immediate_no_prefix(in);
791                                 } else {
792                                         const arch_register_t *reg;
793
794                                         if (mod & EMIT_ALTERNATE_AM)
795                                                 be_emit_char('*');
796                                         reg = get_in_reg(node, pos);
797                                         emit_register(reg, mod & EMIT_RESPECT_LS ? get_ia32_ls_mode(node) : NULL);
798                                 }
799                                 break;
800                         }
801
802                         case 's': {
803                                 const char *str = va_arg(ap, const char*);
804                                 be_emit_string(str);
805                                 break;
806                         }
807
808                         case 'u':
809                                 if (mod & EMIT_LONG) {
810                                         unsigned long num = va_arg(ap, unsigned long);
811                                         be_emit_irprintf("%lu", num);
812                                 } else {
813                                         unsigned num = va_arg(ap, unsigned);
814                                         be_emit_irprintf("%u", num);
815                                 }
816                                 break;
817
818                         case 'd':
819                                 if (mod & EMIT_LONG) {
820                                         long num = va_arg(ap, long);
821                                         be_emit_irprintf("%ld", num);
822                                 } else {
823                                         int num = va_arg(ap, int);
824                                         be_emit_irprintf("%d", num);
825                                 }
826                                 break;
827
828                         default:
829 unknown:
830                                 panic("unknown format conversion in ia32_emitf()");
831                 }
832         }
833
834         va_end(ap);
835 }
836
837 /**
838  * Emits registers and/or address mode of a binary operation.
839  */
840 void ia32_emit_binop(const ir_node *node)
841 {
842         if (is_ia32_Immediate(get_irn_n(node, n_ia32_binary_right))) {
843                 ia32_emitf(node, "%#S4, %#AS3");
844         } else {
845                 ia32_emitf(node, "%#AS4, %#S3");
846         }
847 }
848
849 /**
850  * Emits registers and/or address mode of a binary operation.
851  */
852 void ia32_emit_x87_binop(const ir_node *node)
853 {
854         switch(get_ia32_op_type(node)) {
855                 case ia32_Normal:
856                         {
857                                 const ia32_x87_attr_t *x87_attr = get_ia32_x87_attr_const(node);
858                                 const arch_register_t *in1      = x87_attr->x87[0];
859                                 const arch_register_t *in       = x87_attr->x87[1];
860                                 const arch_register_t *out      = x87_attr->x87[2];
861
862                                 if (out == NULL) {
863                                         out = in1;
864                                 } else if (out == in) {
865                                         in = in1;
866                                 }
867
868                                 be_emit_char('%');
869                                 be_emit_string(arch_register_get_name(in));
870                                 be_emit_cstring(", %");
871                                 be_emit_string(arch_register_get_name(out));
872                         }
873                         break;
874                 case ia32_AddrModeS:
875                         ia32_emit_am(node);
876                         break;
877                 case ia32_AddrModeD:
878                 default:
879                         assert(0 && "unsupported op type");
880         }
881 }
882
883 /**
884  * Emits registers and/or address mode of a unary operation.
885  */
886 void ia32_emit_unop(const ir_node *node, int pos)
887 {
888         char fmt[] = "%ASx";
889         fmt[3] = '0' + pos;
890         ia32_emitf(node, fmt);
891 }
892
893 static void emit_ia32_IMul(const ir_node *node)
894 {
895         ir_node               *left    = get_irn_n(node, n_ia32_IMul_left);
896         const arch_register_t *out_reg = get_out_reg(node, pn_ia32_IMul_res);
897
898         /* do we need the 3-address form? */
899         if (is_ia32_NoReg_GP(left) ||
900                         get_in_reg(node, n_ia32_IMul_left) != out_reg) {
901                 ia32_emitf(node, "\timul%M %#S4, %#AS3, %#D0\n");
902         } else {
903                 ia32_emitf(node, "\timul%M %#AS4, %#S3\n");
904         }
905 }
906
907 /**
908  * walks up a tree of copies/perms/spills/reloads to find the original value
909  * that is moved around
910  */
911 static ir_node *find_original_value(ir_node *node)
912 {
913         if (irn_visited(node))
914                 return NULL;
915
916         mark_irn_visited(node);
917         if (be_is_Copy(node)) {
918                 return find_original_value(be_get_Copy_op(node));
919         } else if (be_is_CopyKeep(node)) {
920                 return find_original_value(be_get_CopyKeep_op(node));
921         } else if (is_Proj(node)) {
922                 ir_node *pred = get_Proj_pred(node);
923                 if (be_is_Perm(pred)) {
924                         return find_original_value(get_irn_n(pred, get_Proj_proj(node)));
925                 } else if (be_is_MemPerm(pred)) {
926                         return find_original_value(get_irn_n(pred, get_Proj_proj(node) + 1));
927                 } else if (is_ia32_Load(pred)) {
928                         return find_original_value(get_irn_n(pred, n_ia32_Load_mem));
929                 } else {
930                         return node;
931                 }
932         } else if (is_ia32_Store(node)) {
933                 return find_original_value(get_irn_n(node, n_ia32_Store_val));
934         } else if (is_Phi(node)) {
935                 int i, arity;
936                 arity = get_irn_arity(node);
937                 for (i = 0; i < arity; ++i) {
938                         ir_node *in  = get_irn_n(node, i);
939                         ir_node *res = find_original_value(in);
940
941                         if (res != NULL)
942                                 return res;
943                 }
944                 return NULL;
945         } else {
946                 return node;
947         }
948 }
949
950 static int determine_final_pnc(const ir_node *node, int flags_pos,
951                                int pnc)
952 {
953         ir_node           *flags = get_irn_n(node, flags_pos);
954         const ia32_attr_t *flags_attr;
955         flags = skip_Proj(flags);
956
957         if (is_ia32_Sahf(flags)) {
958                 ir_node *cmp = get_irn_n(flags, n_ia32_Sahf_val);
959                 if (!(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
960                                 || is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp))) {
961                         inc_irg_visited(current_ir_graph);
962                         cmp = find_original_value(cmp);
963                         assert(cmp != NULL);
964                         assert(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
965                                || is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp));
966                 }
967
968                 flags_attr = get_ia32_attr_const(cmp);
969                 if (flags_attr->data.ins_permuted)
970                         pnc = get_mirrored_pnc(pnc);
971                 pnc |= ia32_pn_Cmp_float;
972         } else if (is_ia32_Ucomi(flags) || is_ia32_Fucomi(flags)
973                         || is_ia32_Fucompi(flags)) {
974                 flags_attr = get_ia32_attr_const(flags);
975
976                 if (flags_attr->data.ins_permuted)
977                         pnc = get_mirrored_pnc(pnc);
978                 pnc |= ia32_pn_Cmp_float;
979         } else {
980                 flags_attr = get_ia32_attr_const(flags);
981
982                 if (flags_attr->data.ins_permuted)
983                         pnc = get_mirrored_pnc(pnc);
984                 if (flags_attr->data.cmp_unsigned)
985                         pnc |= ia32_pn_Cmp_unsigned;
986         }
987
988         return pnc;
989 }
990
991 static pn_Cmp ia32_get_negated_pnc(pn_Cmp pnc)
992 {
993         ir_mode *mode = pnc & ia32_pn_Cmp_float ? mode_F : mode_Iu;
994         return get_negated_pnc(pnc, mode);
995 }
996
997 void ia32_emit_cmp_suffix_node(const ir_node *node,
998                                int flags_pos)
999 {
1000         const ia32_attr_t *attr = get_ia32_attr_const(node);
1001
1002         pn_Cmp pnc = get_ia32_condcode(node);
1003
1004         pnc = determine_final_pnc(node, flags_pos, pnc);
1005         if (attr->data.ins_permuted)
1006                 pnc = ia32_get_negated_pnc(pnc);
1007
1008         ia32_emit_cmp_suffix(pnc);
1009 }
1010
1011 /**
1012  * Emits an exception label for a given node.
1013  */
1014 static void ia32_emit_exc_label(const ir_node *node)
1015 {
1016         be_emit_string(be_gas_insn_label_prefix());
1017         be_emit_irprintf("%lu", get_ia32_exc_label_id(node));
1018 }
1019
1020 /**
1021  * Returns the Proj with projection number proj and NOT mode_M
1022  */
1023 static ir_node *get_proj(const ir_node *node, long proj)
1024 {
1025         const ir_edge_t *edge;
1026         ir_node         *src;
1027
1028         assert(get_irn_mode(node) == mode_T && "expected mode_T node");
1029
1030         foreach_out_edge(node, edge) {
1031                 src = get_edge_src_irn(edge);
1032
1033                 assert(is_Proj(src) && "Proj expected");
1034                 if (get_irn_mode(src) == mode_M)
1035                         continue;
1036
1037                 if (get_Proj_proj(src) == proj)
1038                         return src;
1039         }
1040         return NULL;
1041 }
1042
1043 static int can_be_fallthrough(const ir_node *node)
1044 {
1045         ir_node *target_block = get_cfop_target_block(node);
1046         ir_node *block        = get_nodes_block(node);
1047         return get_prev_block_sched(target_block) == block;
1048 }
1049
1050 /**
1051  * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
1052  */
1053 static void emit_ia32_Jcc(const ir_node *node)
1054 {
1055         int            need_parity_label = 0;
1056         const ir_node *proj_true;
1057         const ir_node *proj_false;
1058         const ir_node *block;
1059         pn_Cmp         pnc = get_ia32_condcode(node);
1060
1061         pnc = determine_final_pnc(node, 0, pnc);
1062
1063         /* get both Projs */
1064         proj_true = get_proj(node, pn_ia32_Jcc_true);
1065         assert(proj_true && "Jcc without true Proj");
1066
1067         proj_false = get_proj(node, pn_ia32_Jcc_false);
1068         assert(proj_false && "Jcc without false Proj");
1069
1070         block      = get_nodes_block(node);
1071
1072         if (can_be_fallthrough(proj_true)) {
1073                 /* exchange both proj's so the second one can be omitted */
1074                 const ir_node *t = proj_true;
1075
1076                 proj_true  = proj_false;
1077                 proj_false = t;
1078                 pnc        = ia32_get_negated_pnc(pnc);
1079         }
1080
1081         if (pnc & ia32_pn_Cmp_float) {
1082                 /* Some floating point comparisons require a test of the parity flag,
1083                  * which indicates that the result is unordered */
1084                 switch (pnc & 15) {
1085                         case pn_Cmp_Uo: {
1086                                 ia32_emitf(proj_true, "\tjp %L\n");
1087                                 break;
1088                         }
1089
1090                         case pn_Cmp_Leg:
1091                                 ia32_emitf(proj_true, "\tjnp %L\n");
1092                                 break;
1093
1094                         case pn_Cmp_Eq:
1095                         case pn_Cmp_Lt:
1096                         case pn_Cmp_Le:
1097                                 /* we need a local label if the false proj is a fallthrough
1098                                  * as the falseblock might have no label emitted then */
1099                                 if (can_be_fallthrough(proj_false)) {
1100                                         need_parity_label = 1;
1101                                         ia32_emitf(proj_false, "\tjp 1f\n");
1102                                 } else {
1103                                         ia32_emitf(proj_false, "\tjp %L\n");
1104                                 }
1105                                 goto emit_jcc;
1106
1107                         case pn_Cmp_Ug:
1108                         case pn_Cmp_Uge:
1109                         case pn_Cmp_Ne:
1110                                 ia32_emitf(proj_true, "\tjp %L\n");
1111                                 goto emit_jcc;
1112
1113                         default:
1114                                 goto emit_jcc;
1115                 }
1116         } else {
1117 emit_jcc:
1118                 ia32_emitf(proj_true, "\tj%P %L\n", pnc);
1119         }
1120
1121         if (need_parity_label) {
1122                 ia32_emitf(NULL, "1:\n");
1123         }
1124
1125         /* the second Proj might be a fallthrough */
1126         if (can_be_fallthrough(proj_false)) {
1127                 ia32_emitf(proj_false, "\t/* fallthrough to %L */\n");
1128         } else {
1129                 ia32_emitf(proj_false, "\tjmp %L\n");
1130         }
1131 }
1132
1133 static void emit_ia32_CMov(const ir_node *node)
1134 {
1135         const ia32_attr_t     *attr         = get_ia32_attr_const(node);
1136         int                    ins_permuted = attr->data.ins_permuted;
1137         const arch_register_t *out          = arch_irn_get_register(node, pn_ia32_res);
1138         pn_Cmp                 pnc          = get_ia32_condcode(node);
1139         const arch_register_t *in_true;
1140         const arch_register_t *in_false;
1141
1142         pnc = determine_final_pnc(node, n_ia32_CMov_eflags, pnc);
1143
1144         in_true  = arch_get_irn_register(get_irn_n(node, n_ia32_CMov_val_true));
1145         in_false = arch_get_irn_register(get_irn_n(node, n_ia32_CMov_val_false));
1146
1147         /* should be same constraint fullfilled? */
1148         if (out == in_false) {
1149                 /* yes -> nothing to do */
1150         } else if (out == in_true) {
1151                 const arch_register_t *tmp;
1152
1153                 assert(get_ia32_op_type(node) == ia32_Normal);
1154
1155                 ins_permuted = !ins_permuted;
1156
1157                 tmp      = in_true;
1158                 in_true  = in_false;
1159                 in_false = tmp;
1160         } else {
1161                 /* we need a mov */
1162                 ia32_emitf(node, "\tmovl %R, %R\n", in_false, out);
1163         }
1164
1165         if (ins_permuted)
1166                 pnc = ia32_get_negated_pnc(pnc);
1167
1168         /* TODO: handling of Nans isn't correct yet */
1169
1170         ia32_emitf(node, "\tcmov%P %#AR, %#R\n", pnc, in_true, out);
1171 }
1172
1173 /*********************************************************
1174  *                 _ _       _
1175  *                (_) |     (_)
1176  *   ___ _ __ ___  _| |_     _ _   _ _ __ ___  _ __  ___
1177  *  / _ \ '_ ` _ \| | __|   | | | | | '_ ` _ \| '_ \/ __|
1178  * |  __/ | | | | | | |_    | | |_| | | | | | | |_) \__ \
1179  *  \___|_| |_| |_|_|\__|   | |\__,_|_| |_| |_| .__/|___/
1180  *                         _/ |               | |
1181  *                        |__/                |_|
1182  *********************************************************/
1183
1184 /* jump table entry (target and corresponding number) */
1185 typedef struct _branch_t {
1186         ir_node *target;
1187         int      value;
1188 } branch_t;
1189
1190 /* jump table for switch generation */
1191 typedef struct _jmp_tbl_t {
1192         ir_node  *defProj;         /**< default target */
1193         long      min_value;       /**< smallest switch case */
1194         long      max_value;       /**< largest switch case */
1195         long      num_branches;    /**< number of jumps */
1196         char     *label;           /**< label of the jump table */
1197         branch_t *branches;        /**< jump array */
1198 } jmp_tbl_t;
1199
1200 /**
1201  * Compare two variables of type branch_t. Used to sort all switch cases
1202  */
1203 static int ia32_cmp_branch_t(const void *a, const void *b)
1204 {
1205         branch_t *b1 = (branch_t *)a;
1206         branch_t *b2 = (branch_t *)b;
1207
1208         if (b1->value <= b2->value)
1209                 return -1;
1210         else
1211                 return 1;
1212 }
1213
1214 /**
1215  * Emits code for a SwitchJmp (creates a jump table if
1216  * possible otherwise a cmp-jmp cascade). Port from
1217  * cggg ia32 backend
1218  */
1219 static void emit_ia32_SwitchJmp(const ir_node *node)
1220 {
1221         unsigned long       interval;
1222         int                 last_value, i;
1223         long                pnc;
1224         long                default_pn;
1225         jmp_tbl_t           tbl;
1226         ir_node            *proj;
1227         const ir_edge_t    *edge;
1228
1229         /* fill the table structure */
1230         tbl.label        = XMALLOCN(char, SNPRINTF_BUF_LEN);
1231         tbl.label        = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, ".TBL_");
1232         tbl.defProj      = NULL;
1233         tbl.num_branches = get_irn_n_edges(node) - 1;
1234         tbl.branches     = XMALLOCNZ(branch_t, tbl.num_branches);
1235         tbl.min_value    = INT_MAX;
1236         tbl.max_value    = INT_MIN;
1237
1238         default_pn = get_ia32_condcode(node);
1239         i = 0;
1240         /* go over all proj's and collect them */
1241         foreach_out_edge(node, edge) {
1242                 proj = get_edge_src_irn(edge);
1243                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
1244
1245                 pnc = get_Proj_proj(proj);
1246
1247                 /* check for default proj */
1248                 if (pnc == default_pn) {
1249                         assert(tbl.defProj == NULL && "found two default Projs at SwitchJmp");
1250                         tbl.defProj = proj;
1251                 } else {
1252                         tbl.min_value = pnc < tbl.min_value ? pnc : tbl.min_value;
1253                         tbl.max_value = pnc > tbl.max_value ? pnc : tbl.max_value;
1254
1255                         /* create branch entry */
1256                         tbl.branches[i].target = proj;
1257                         tbl.branches[i].value  = pnc;
1258                         ++i;
1259                 }
1260
1261         }
1262         assert(i == tbl.num_branches);
1263
1264         /* sort the branches by their number */
1265         qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
1266
1267         /* two-complement's magic make this work without overflow */
1268         interval = tbl.max_value - tbl.min_value;
1269
1270         /* emit the table */
1271         ia32_emitf(node,        "\tcmpl $%u, %S0\n", interval);
1272         ia32_emitf(tbl.defProj, "\tja %L\n");
1273
1274         if (tbl.num_branches > 1) {
1275                 /* create table */
1276                 ia32_emitf(node, "\tjmp *%s(,%S0,4)\n", tbl.label);
1277
1278                 be_gas_emit_switch_section(GAS_SECTION_RODATA);
1279                 ia32_emitf(NULL, "\t.align 4\n");
1280                 ia32_emitf(NULL, "%s:\n", tbl.label);
1281
1282                 last_value = tbl.branches[0].value;
1283                 for (i = 0; i != tbl.num_branches; ++i) {
1284                         while (last_value != tbl.branches[i].value) {
1285                                 ia32_emitf(tbl.defProj, ".long %L\n");
1286                                 ++last_value;
1287                         }
1288                         ia32_emitf(tbl.branches[i].target, ".long %L\n");
1289                         ++last_value;
1290                 }
1291                 be_gas_emit_switch_section(GAS_SECTION_TEXT);
1292         } else {
1293                 /* one jump is enough */
1294                 ia32_emitf(tbl.branches[0].target, "\tjmp %L\n");
1295         }
1296
1297         if (tbl.label)
1298                 free(tbl.label);
1299         if (tbl.branches)
1300                 free(tbl.branches);
1301 }
1302
1303 /**
1304  * Emits code for a unconditional jump.
1305  */
1306 static void emit_Jmp(const ir_node *node)
1307 {
1308         ir_node *block;
1309
1310         /* for now, the code works for scheduled and non-schedules blocks */
1311         block = get_nodes_block(node);
1312
1313         /* we have a block schedule */
1314         if (can_be_fallthrough(node)) {
1315                 ia32_emitf(node, "\t/* fallthrough to %L */\n");
1316         } else {
1317                 ia32_emitf(node, "\tjmp %L\n");
1318         }
1319 }
1320
1321 /**
1322  * Emit an inline assembler operand.
1323  *
1324  * @param node  the ia32_ASM node
1325  * @param s     points to the operand (a %c)
1326  *
1327  * @return  pointer to the first char in s NOT in the current operand
1328  */
1329 static const char* emit_asm_operand(const ir_node *node, const char *s)
1330 {
1331         const ia32_attr_t     *ia32_attr = get_ia32_attr_const(node);
1332         const ia32_asm_attr_t *attr      = CONST_CAST_IA32_ATTR(ia32_asm_attr_t,
1333                                                             ia32_attr);
1334         const arch_register_t *reg;
1335         const ia32_asm_reg_t  *asm_regs = attr->register_map;
1336         const ia32_asm_reg_t  *asm_reg;
1337         const char            *reg_name;
1338         char                   c;
1339         char                   modifier = 0;
1340         int                    num      = -1;
1341         int                    p;
1342
1343         assert(*s == '%');
1344         c = *(++s);
1345
1346         /* parse modifiers */
1347         switch(c) {
1348         case 0:
1349                 ir_fprintf(stderr, "Warning: asm text (%+F) ends with %%\n", node);
1350                 be_emit_char('%');
1351                 return s + 1;
1352         case '%':
1353                 be_emit_char('%');
1354                 return s + 1;
1355         case 'w':
1356         case 'b':
1357         case 'h':
1358                 modifier = c;
1359                 ++s;
1360                 break;
1361         case '0':
1362         case '1':
1363         case '2':
1364         case '3':
1365         case '4':
1366         case '5':
1367         case '6':
1368         case '7':
1369         case '8':
1370         case '9':
1371                 break;
1372         default:
1373                 ir_fprintf(stderr,
1374                                 "Warning: asm text (%+F) contains unknown modifier '%c' for asm op\n",
1375                                 node, c);
1376                 ++s;
1377                 break;
1378         }
1379
1380         /* parse number */
1381         sscanf(s, "%d%n", &num, &p);
1382         if (num < 0) {
1383                 ir_fprintf(stderr, "Warning: Couldn't parse assembler operand (%+F)\n",
1384                            node);
1385                 return s;
1386         } else {
1387                 s += p;
1388         }
1389
1390         if (num < 0 || ARR_LEN(asm_regs) <= num) {
1391                 ir_fprintf(stderr,
1392                                 "Error: Custom assembler references invalid input/output (%+F)\n",
1393                                 node);
1394                 return s;
1395         }
1396         asm_reg = & asm_regs[num];
1397         assert(asm_reg->valid);
1398
1399         /* get register */
1400         if (asm_reg->use_input == 0) {
1401                 reg = get_out_reg(node, asm_reg->inout_pos);
1402         } else {
1403                 ir_node *pred = get_irn_n(node, asm_reg->inout_pos);
1404
1405                 /* might be an immediate value */
1406                 if (is_ia32_Immediate(pred)) {
1407                         emit_ia32_Immediate(pred);
1408                         return s;
1409                 }
1410                 reg = get_in_reg(node, asm_reg->inout_pos);
1411         }
1412         if (reg == NULL) {
1413                 ir_fprintf(stderr,
1414                                 "Warning: no register assigned for %d asm op (%+F)\n",
1415                                 num, node);
1416                 return s;
1417         }
1418
1419         if (asm_reg->memory) {
1420                 be_emit_char('(');
1421         }
1422
1423         /* emit it */
1424         if (modifier != 0) {
1425                 be_emit_char('%');
1426                 switch(modifier) {
1427                 case 'b':
1428                         reg_name = ia32_get_mapped_reg_name(isa->regs_8bit, reg);
1429                         break;
1430                 case 'h':
1431                         reg_name = ia32_get_mapped_reg_name(isa->regs_8bit_high, reg);
1432                         break;
1433                 case 'w':
1434                         reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
1435                         break;
1436                 default:
1437                         panic("Invalid asm op modifier");
1438                 }
1439                 be_emit_string(reg_name);
1440         } else {
1441                 emit_register(reg, asm_reg->mode);
1442         }
1443
1444         if (asm_reg->memory) {
1445                 be_emit_char(')');
1446         }
1447
1448         return s;
1449 }
1450
1451 /**
1452  * Emits code for an ASM pseudo op.
1453  */
1454 static void emit_ia32_Asm(const ir_node *node)
1455 {
1456         const void            *gen_attr = get_irn_generic_attr_const(node);
1457         const ia32_asm_attr_t *attr
1458                 = CONST_CAST_IA32_ATTR(ia32_asm_attr_t, gen_attr);
1459         ident                 *asm_text = attr->asm_text;
1460         const char            *s        = get_id_str(asm_text);
1461
1462         ia32_emitf(node, "#APP\t\n");
1463
1464         if (s[0] != '\t')
1465                 be_emit_char('\t');
1466
1467         while(*s != 0) {
1468                 if (*s == '%') {
1469                         s = emit_asm_operand(node, s);
1470                 } else {
1471                         be_emit_char(*s++);
1472                 }
1473         }
1474
1475         ia32_emitf(NULL, "\n#NO_APP\n");
1476 }
1477
1478 /**********************************
1479  *   _____                  ____
1480  *  / ____|                |  _ \
1481  * | |     ___  _ __  _   _| |_) |
1482  * | |    / _ \| '_ \| | | |  _ <
1483  * | |___| (_) | |_) | |_| | |_) |
1484  *  \_____\___/| .__/ \__, |____/
1485  *             | |     __/ |
1486  *             |_|    |___/
1487  **********************************/
1488
1489 /**
1490  * Emit movsb/w instructions to make mov count divideable by 4
1491  */
1492 static void emit_CopyB_prolog(unsigned size)
1493 {
1494         if (size & 1)
1495                 ia32_emitf(NULL, "\tmovsb\n");
1496         if (size & 2)
1497                 ia32_emitf(NULL, "\tmovsw\n");
1498 }
1499
1500 /**
1501  * Emit rep movsd instruction for memcopy.
1502  */
1503 static void emit_ia32_CopyB(const ir_node *node)
1504 {
1505         unsigned size = get_ia32_copyb_size(node);
1506
1507         emit_CopyB_prolog(size);
1508         ia32_emitf(node, "\trep movsd\n");
1509 }
1510
1511 /**
1512  * Emits unrolled memcopy.
1513  */
1514 static void emit_ia32_CopyB_i(const ir_node *node)
1515 {
1516         unsigned size = get_ia32_copyb_size(node);
1517
1518         emit_CopyB_prolog(size);
1519
1520         size >>= 2;
1521         while (size--) {
1522                 ia32_emitf(NULL, "\tmovsd\n");
1523         }
1524 }
1525
1526
1527
1528 /***************************
1529  *   _____
1530  *  / ____|
1531  * | |     ___  _ ____   __
1532  * | |    / _ \| '_ \ \ / /
1533  * | |___| (_) | | | \ V /
1534  *  \_____\___/|_| |_|\_/
1535  *
1536  ***************************/
1537
1538 /**
1539  * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1540  */
1541 static void emit_ia32_Conv_with_FP(const ir_node *node, const char* conv_f,
1542                 const char* conv_d)
1543 {
1544         ir_mode            *ls_mode = get_ia32_ls_mode(node);
1545         int                 ls_bits = get_mode_size_bits(ls_mode);
1546         const char         *conv    = ls_bits == 32 ? conv_f : conv_d;
1547
1548         ia32_emitf(node, "\tcvt%s %AS3, %D0\n", conv);
1549 }
1550
1551 static void emit_ia32_Conv_I2FP(const ir_node *node)
1552 {
1553         emit_ia32_Conv_with_FP(node, "si2ss", "si2sd");
1554 }
1555
1556 static void emit_ia32_Conv_FP2I(const ir_node *node)
1557 {
1558         emit_ia32_Conv_with_FP(node, "ss2si", "sd2si");
1559 }
1560
1561 static void emit_ia32_Conv_FP2FP(const ir_node *node)
1562 {
1563         emit_ia32_Conv_with_FP(node, "sd2ss", "ss2sd");
1564 }
1565
1566 /**
1567  * Emits code for an Int conversion.
1568  */
1569 static void emit_ia32_Conv_I2I(const ir_node *node)
1570 {
1571         ir_mode *smaller_mode = get_ia32_ls_mode(node);
1572         int      signed_mode  = mode_is_signed(smaller_mode);
1573         const char *sign_suffix;
1574
1575         assert(!mode_is_float(smaller_mode));
1576
1577         sign_suffix = signed_mode ? "s" : "z";
1578         ia32_emitf(node, "\tmov%s%Ml %#AS3, %D0\n", sign_suffix);
1579 }
1580
1581 /**
1582  * Emits a call
1583  */
1584 static void emit_ia32_Call(const ir_node *node)
1585 {
1586         /* Special case: Call must not have its immediates prefixed by $, instead
1587          * address mode is prefixed by *. */
1588         ia32_emitf(node, "\tcall %*AS3\n");
1589 }
1590
1591
1592 /*******************************************
1593  *  _                          _
1594  * | |                        | |
1595  * | |__   ___ _ __   ___   __| | ___  ___
1596  * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1597  * | |_) |  __/ | | | (_) | (_| |  __/\__ \
1598  * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1599  *
1600  *******************************************/
1601
1602 /**
1603  * Emits code to increase stack pointer.
1604  */
1605 static void emit_be_IncSP(const ir_node *node)
1606 {
1607         int offs = be_get_IncSP_offset(node);
1608
1609         if (offs == 0)
1610                 return;
1611
1612         if (offs > 0) {
1613                 ia32_emitf(node, "\tsubl $%u, %D0\n", offs);
1614         } else {
1615                 ia32_emitf(node, "\taddl $%u, %D0\n", -offs);
1616         }
1617 }
1618
1619 /**
1620  * Emits code for Copy/CopyKeep.
1621  */
1622 static void Copy_emitter(const ir_node *node, const ir_node *op)
1623 {
1624         const arch_register_t *in  = arch_get_irn_register(op);
1625         const arch_register_t *out = arch_get_irn_register(node);
1626
1627         if (in == out) {
1628                 return;
1629         }
1630         if (is_unknown_reg(in))
1631                 return;
1632         /* copies of vf nodes aren't real... */
1633         if (arch_register_get_class(in) == &ia32_reg_classes[CLASS_ia32_vfp])
1634                 return;
1635
1636         if (get_irn_mode(node) == mode_E) {
1637                 ia32_emitf(node, "\tmovsd %R, %R\n", in, out);
1638         } else {
1639                 ia32_emitf(node, "\tmovl %R, %R\n", in, out);
1640         }
1641 }
1642
1643 static void emit_be_Copy(const ir_node *node)
1644 {
1645         Copy_emitter(node, be_get_Copy_op(node));
1646 }
1647
1648 static void emit_be_CopyKeep(const ir_node *node)
1649 {
1650         Copy_emitter(node, be_get_CopyKeep_op(node));
1651 }
1652
1653 /**
1654  * Emits code for exchange.
1655  */
1656 static void emit_be_Perm(const ir_node *node)
1657 {
1658         const arch_register_t *in0, *in1;
1659         const arch_register_class_t *cls0, *cls1;
1660
1661         in0 = arch_get_irn_register(get_irn_n(node, 0));
1662         in1 = arch_get_irn_register(get_irn_n(node, 1));
1663
1664         cls0 = arch_register_get_class(in0);
1665         cls1 = arch_register_get_class(in1);
1666
1667         assert(cls0 == cls1 && "Register class mismatch at Perm");
1668
1669         if (cls0 == &ia32_reg_classes[CLASS_ia32_gp]) {
1670                 ia32_emitf(node, "\txchg %R, %R\n", in1, in0);
1671         } else if (cls0 == &ia32_reg_classes[CLASS_ia32_xmm]) {
1672                 ia32_emitf(NULL, "\txorpd %R, %R\n", in1, in0);
1673                 ia32_emitf(NULL, "\txorpd %R, %R\n", in0, in1);
1674                 ia32_emitf(node, "\txorpd %R, %R\n", in1, in0);
1675         } else if (cls0 == &ia32_reg_classes[CLASS_ia32_vfp]) {
1676                 /* is a NOP */
1677         } else if (cls0 == &ia32_reg_classes[CLASS_ia32_st]) {
1678                 /* is a NOP */
1679         } else {
1680                 panic("unexpected register class in be_Perm (%+F)", node);
1681         }
1682 }
1683
1684 /**
1685  * Emits code for Constant loading.
1686  */
1687 static void emit_ia32_Const(const ir_node *node)
1688 {
1689         ia32_emitf(node, "\tmovl %I, %D0\n");
1690 }
1691
1692 /**
1693  * Emits code to load the TLS base
1694  */
1695 static void emit_ia32_LdTls(const ir_node *node)
1696 {
1697         ia32_emitf(node, "\tmovl %%gs:0, %D0\n");
1698 }
1699
1700 /* helper function for emit_ia32_Minus64Bit */
1701 static void emit_mov(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1702 {
1703         ia32_emitf(node, "\tmovl %R, %R\n", src, dst);
1704 }
1705
1706 /* helper function for emit_ia32_Minus64Bit */
1707 static void emit_neg(const ir_node* node, const arch_register_t *reg)
1708 {
1709         ia32_emitf(node, "\tnegl %R\n", reg);
1710 }
1711
1712 /* helper function for emit_ia32_Minus64Bit */
1713 static void emit_sbb0(const ir_node* node, const arch_register_t *reg)
1714 {
1715         ia32_emitf(node, "\tsbbl $0, %R\n", reg);
1716 }
1717
1718 /* helper function for emit_ia32_Minus64Bit */
1719 static void emit_sbb(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1720 {
1721         ia32_emitf(node, "\tsbbl %R, %R\n", src, dst);
1722 }
1723
1724 /* helper function for emit_ia32_Minus64Bit */
1725 static void emit_xchg(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
1726 {
1727         ia32_emitf(node, "\txchgl %R, %R\n", src, dst);
1728 }
1729
1730 /* helper function for emit_ia32_Minus64Bit */
1731 static void emit_zero(const ir_node* node, const arch_register_t *reg)
1732 {
1733         ia32_emitf(node, "\txorl %R, %R\n", reg, reg);
1734 }
1735
1736 static void emit_ia32_Minus64Bit(const ir_node *node)
1737 {
1738         const arch_register_t *in_lo  = get_in_reg(node, 0);
1739         const arch_register_t *in_hi  = get_in_reg(node, 1);
1740         const arch_register_t *out_lo = get_out_reg(node, 0);
1741         const arch_register_t *out_hi = get_out_reg(node, 1);
1742
1743         if (out_lo == in_lo) {
1744                 if (out_hi != in_hi) {
1745                         /* a -> a, b -> d */
1746                         goto zero_neg;
1747                 } else {
1748                         /* a -> a, b -> b */
1749                         goto normal_neg;
1750                 }
1751         } else if (out_lo == in_hi) {
1752                 if (out_hi == in_lo) {
1753                         /* a -> b, b -> a */
1754                         emit_xchg(node, in_lo, in_hi);
1755                         goto normal_neg;
1756                 } else {
1757                         /* a -> b, b -> d */
1758                         emit_mov(node, in_hi, out_hi);
1759                         emit_mov(node, in_lo, out_lo);
1760                         goto normal_neg;
1761                 }
1762         } else {
1763                 if (out_hi == in_lo) {
1764                         /* a -> c, b -> a */
1765                         emit_mov(node, in_lo, out_lo);
1766                         goto zero_neg;
1767                 } else if (out_hi == in_hi) {
1768                         /* a -> c, b -> b */
1769                         emit_mov(node, in_lo, out_lo);
1770                         goto normal_neg;
1771                 } else {
1772                         /* a -> c, b -> d */
1773                         emit_mov(node, in_lo, out_lo);
1774                         goto zero_neg;
1775                 }
1776         }
1777
1778 normal_neg:
1779         emit_neg( node, out_hi);
1780         emit_neg( node, out_lo);
1781         emit_sbb0(node, out_hi);
1782         return;
1783
1784 zero_neg:
1785         emit_zero(node, out_hi);
1786         emit_neg( node, out_lo);
1787         emit_sbb( node, in_hi, out_hi);
1788 }
1789
1790 static void emit_ia32_GetEIP(const ir_node *node)
1791 {
1792         ia32_emitf(node, "\tcall %s\n", pic_base_label);
1793         ia32_emitf(NULL, "%s:\n", pic_base_label);
1794         ia32_emitf(node, "\tpopl %D0\n");
1795 }
1796
1797 static void emit_ia32_ClimbFrame(const ir_node *node)
1798 {
1799         const ia32_climbframe_attr_t *attr = get_ia32_climbframe_attr_const(node);
1800
1801         ia32_emitf(node, "\tmovl %S0, %D0\n");
1802         ia32_emitf(node, "\tmovl $%u, %S1\n", attr->count);
1803         ia32_emitf(NULL, BLOCK_PREFIX "%ld:\n", get_irn_node_nr(node));
1804         ia32_emitf(node, "\tmovl (%D0), %D0\n");
1805         ia32_emitf(node, "\tdec %S1\n");
1806         ia32_emitf(node, "\tjnz " BLOCK_PREFIX "%ld\n", get_irn_node_nr(node));
1807 }
1808
1809 static void emit_be_Return(const ir_node *node)
1810 {
1811         unsigned pop = be_Return_get_pop(node);
1812
1813         if (pop > 0 || be_Return_get_emit_pop(node)) {
1814                 ia32_emitf(node, "\tret $%u\n", pop);
1815         } else {
1816                 ia32_emitf(node, "\tret\n");
1817         }
1818 }
1819
1820 static void emit_Nothing(const ir_node *node)
1821 {
1822         (void) node;
1823 }
1824
1825
1826 /***********************************************************************************
1827  *                  _          __                                             _
1828  *                 (_)        / _|                                           | |
1829  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
1830  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1831  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
1832  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
1833  *
1834  ***********************************************************************************/
1835
1836 /**
1837  * Enters the emitter functions for handled nodes into the generic
1838  * pointer of an opcode.
1839  */
1840 static void ia32_register_emitters(void)
1841 {
1842 #define IA32_EMIT2(a,b) op_ia32_##a->ops.generic = (op_func)emit_ia32_##b
1843 #define IA32_EMIT(a)    IA32_EMIT2(a,a)
1844 #define EMIT(a)         op_##a->ops.generic = (op_func)emit_##a
1845 #define IGN(a)                  op_##a->ops.generic = (op_func)emit_Nothing
1846 #define BE_EMIT(a)      op_be_##a->ops.generic = (op_func)emit_be_##a
1847 #define BE_IGN(a)               op_be_##a->ops.generic = (op_func)emit_Nothing
1848
1849         /* first clear the generic function pointer for all ops */
1850         clear_irp_opcodes_generic_func();
1851
1852         /* register all emitter functions defined in spec */
1853         ia32_register_spec_emitters();
1854
1855         /* other ia32 emitter functions */
1856         IA32_EMIT2(Conv_I2I8Bit, Conv_I2I);
1857         IA32_EMIT(Asm);
1858         IA32_EMIT(CMov);
1859         IA32_EMIT(Call);
1860         IA32_EMIT(Const);
1861         IA32_EMIT(Conv_FP2FP);
1862         IA32_EMIT(Conv_FP2I);
1863         IA32_EMIT(Conv_I2FP);
1864         IA32_EMIT(Conv_I2I);
1865         IA32_EMIT(CopyB);
1866         IA32_EMIT(CopyB_i);
1867         IA32_EMIT(GetEIP);
1868         IA32_EMIT(IMul);
1869         IA32_EMIT(Jcc);
1870         IA32_EMIT(LdTls);
1871         IA32_EMIT(Minus64Bit);
1872         IA32_EMIT(SwitchJmp);
1873         IA32_EMIT(ClimbFrame);
1874
1875         /* benode emitter */
1876         BE_EMIT(Copy);
1877         BE_EMIT(CopyKeep);
1878         BE_EMIT(IncSP);
1879         BE_EMIT(Perm);
1880         BE_EMIT(Return);
1881
1882         BE_IGN(Barrier);
1883         BE_IGN(Keep);
1884         BE_IGN(RegParams);
1885
1886         /* firm emitter */
1887         EMIT(Jmp);
1888         IGN(Phi);
1889         IGN(Start);
1890
1891 #undef BE_EMIT
1892 #undef EMIT
1893 #undef IGN
1894 #undef IA32_EMIT2
1895 #undef IA32_EMIT
1896 }
1897
1898 typedef void (*emit_func_ptr) (const ir_node *);
1899
1900 /**
1901  * Assign and emit an exception label if the current instruction can fail.
1902  */
1903 static void ia32_assign_exc_label(ir_node *node)
1904 {
1905         /* assign a new ID to the instruction */
1906         set_ia32_exc_label_id(node, ++exc_label_id);
1907         /* print it */
1908         ia32_emit_exc_label(node);
1909         be_emit_char(':');
1910         be_emit_pad_comment();
1911         be_emit_cstring("/* exception to Block ");
1912         ia32_emit_cfop_target(node);
1913         be_emit_cstring(" */\n");
1914         be_emit_write_line();
1915 }
1916
1917 /**
1918  * Emits code for a node.
1919  */
1920 static void ia32_emit_node(ir_node *node)
1921 {
1922         ir_op *op = get_irn_op(node);
1923
1924         DBG((dbg, LEVEL_1, "emitting code for %+F\n", node));
1925
1926         if (is_ia32_irn(node)) {
1927                 if (get_ia32_exc_label(node)) {
1928                         /* emit the exception label of this instruction */
1929                         ia32_assign_exc_label(node);
1930                 }
1931                 if (mark_spill_reload) {
1932                         if (is_ia32_is_spill(node)) {
1933                                 ia32_emitf(NULL, "\txchg %ebx, %ebx        /* spill mark */\n");
1934                         }
1935                         if (is_ia32_is_reload(node)) {
1936                                 ia32_emitf(NULL, "\txchg %edx, %edx        /* reload mark */\n");
1937                         }
1938                         if (is_ia32_is_remat(node)) {
1939                                 ia32_emitf(NULL, "\txchg %ecx, %ecx        /* remat mark */\n");
1940                         }
1941                 }
1942         }
1943         if (op->ops.generic) {
1944                 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
1945
1946                 be_dbg_set_dbg_info(get_irn_dbg_info(node));
1947
1948                 (*func) (node);
1949         } else {
1950                 emit_Nothing(node);
1951                 ir_fprintf(stderr, "Error: No emit handler for node %+F (%+G, graph %+F)\n", node, node, current_ir_graph);
1952                 abort();
1953         }
1954 }
1955
1956 /**
1957  * Emits gas alignment directives
1958  */
1959 static void ia32_emit_alignment(unsigned align, unsigned skip)
1960 {
1961         ia32_emitf(NULL, "\t.p2align %u,,%u\n", align, skip);
1962 }
1963
1964 /**
1965  * Emits gas alignment directives for Labels depended on cpu architecture.
1966  */
1967 static void ia32_emit_align_label(void)
1968 {
1969         unsigned align        = ia32_cg_config.label_alignment;
1970         unsigned maximum_skip = ia32_cg_config.label_alignment_max_skip;
1971         ia32_emit_alignment(align, maximum_skip);
1972 }
1973
1974 /**
1975  * Test whether a block should be aligned.
1976  * For cpus in the P4/Athlon class it is useful to align jump labels to
1977  * 16 bytes. However we should only do that if the alignment nops before the
1978  * label aren't executed more often than we have jumps to the label.
1979  */
1980 static int should_align_block(const ir_node *block)
1981 {
1982         static const double DELTA = .0001;
1983         ir_exec_freq *exec_freq   = cg->birg->exec_freq;
1984         ir_node      *prev        = get_prev_block_sched(block);
1985         double        block_freq;
1986         double        prev_freq = 0;  /**< execfreq of the fallthrough block */
1987         double        jmp_freq  = 0;  /**< execfreq of all non-fallthrough blocks */
1988         int           i, n_cfgpreds;
1989
1990         if (exec_freq == NULL)
1991                 return 0;
1992         if (ia32_cg_config.label_alignment_factor <= 0)
1993                 return 0;
1994
1995         block_freq = get_block_execfreq(exec_freq, block);
1996         if (block_freq < DELTA)
1997                 return 0;
1998
1999         n_cfgpreds = get_Block_n_cfgpreds(block);
2000         for(i = 0; i < n_cfgpreds; ++i) {
2001                 const ir_node *pred      = get_Block_cfgpred_block(block, i);
2002                 double         pred_freq = get_block_execfreq(exec_freq, pred);
2003
2004                 if (pred == prev) {
2005                         prev_freq += pred_freq;
2006                 } else {
2007                         jmp_freq  += pred_freq;
2008                 }
2009         }
2010
2011         if (prev_freq < DELTA && !(jmp_freq < DELTA))
2012                 return 1;
2013
2014         jmp_freq /= prev_freq;
2015
2016         return jmp_freq > ia32_cg_config.label_alignment_factor;
2017 }
2018
2019 /**
2020  * Emit the block header for a block.
2021  *
2022  * @param block       the block
2023  * @param prev_block  the previous block
2024  */
2025 static void ia32_emit_block_header(ir_node *block)
2026 {
2027         ir_graph     *irg = current_ir_graph;
2028         int           need_label = block_needs_label(block);
2029         int           i, arity;
2030         ir_exec_freq *exec_freq = cg->birg->exec_freq;
2031
2032         if (block == get_irg_end_block(irg))
2033                 return;
2034
2035         if (ia32_cg_config.label_alignment > 0) {
2036                 /* align the current block if:
2037                  * a) if should be aligned due to its execution frequency
2038                  * b) there is no fall-through here
2039                  */
2040                 if (should_align_block(block)) {
2041                         ia32_emit_align_label();
2042                 } else {
2043                         /* if the predecessor block has no fall-through,
2044                            we can always align the label. */
2045                         int i;
2046                         int has_fallthrough = 0;
2047
2048                         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
2049                                 ir_node *cfg_pred = get_Block_cfgpred(block, i);
2050                                 if (can_be_fallthrough(cfg_pred)) {
2051                                         has_fallthrough = 1;
2052                                         break;
2053                                 }
2054                         }
2055
2056                         if (!has_fallthrough)
2057                                 ia32_emit_align_label();
2058                 }
2059         }
2060
2061         if (need_label) {
2062                 ia32_emit_block_name(block);
2063                 be_emit_char(':');
2064
2065                 be_emit_pad_comment();
2066                 be_emit_cstring("   /* ");
2067         } else {
2068                 be_emit_cstring("\t/* ");
2069                 ia32_emit_block_name(block);
2070                 be_emit_cstring(": ");
2071         }
2072
2073         be_emit_cstring("preds:");
2074
2075         /* emit list of pred blocks in comment */
2076         arity = get_irn_arity(block);
2077         if (arity <= 0) {
2078                 be_emit_cstring(" none");
2079         } else {
2080                 for (i = 0; i < arity; ++i) {
2081                         ir_node *predblock = get_Block_cfgpred_block(block, i);
2082                         be_emit_irprintf(" %d", get_irn_node_nr(predblock));
2083                 }
2084         }
2085         if (exec_freq != NULL) {
2086                 be_emit_irprintf(", freq: %f",
2087                                  get_block_execfreq(exec_freq, block));
2088         }
2089         be_emit_cstring(" */\n");
2090         be_emit_write_line();
2091 }
2092
2093 /**
2094  * Walks over the nodes in a block connected by scheduling edges
2095  * and emits code for each node.
2096  */
2097 static void ia32_gen_block(ir_node *block)
2098 {
2099         ir_node *node;
2100
2101         ia32_emit_block_header(block);
2102
2103         /* emit the contents of the block */
2104         be_dbg_set_dbg_info(get_irn_dbg_info(block));
2105         sched_foreach(block, node) {
2106                 ia32_emit_node(node);
2107         }
2108 }
2109
2110 typedef struct exc_entry {
2111         ir_node *exc_instr;  /** The instruction that can issue an exception. */
2112         ir_node *block;      /** The block to call then. */
2113 } exc_entry;
2114
2115 /**
2116  * Block-walker:
2117  * Sets labels for control flow nodes (jump target).
2118  * Links control predecessors to there destination blocks.
2119  */
2120 static void ia32_gen_labels(ir_node *block, void *data)
2121 {
2122         exc_entry **exc_list = data;
2123         ir_node *pred;
2124         int     n;
2125
2126         for (n = get_Block_n_cfgpreds(block) - 1; n >= 0; --n) {
2127                 pred = get_Block_cfgpred(block, n);
2128                 set_irn_link(pred, block);
2129
2130                 pred = skip_Proj(pred);
2131                 if (is_ia32_irn(pred) && get_ia32_exc_label(pred)) {
2132                         exc_entry e;
2133
2134                         e.exc_instr = pred;
2135                         e.block     = block;
2136                         ARR_APP1(exc_entry, *exc_list, e);
2137                         set_irn_link(pred, block);
2138                 }
2139         }
2140 }
2141
2142 /**
2143  * Compare two exception_entries.
2144  */
2145 static int cmp_exc_entry(const void *a, const void *b)
2146 {
2147         const exc_entry *ea = a;
2148         const exc_entry *eb = b;
2149
2150         if (get_ia32_exc_label_id(ea->exc_instr) < get_ia32_exc_label_id(eb->exc_instr))
2151                 return -1;
2152         return +1;
2153 }
2154
2155 /**
2156  * Main driver. Emits the code for one routine.
2157  */
2158 void ia32_gen_routine(ia32_code_gen_t *ia32_cg, ir_graph *irg)
2159 {
2160         ir_entity *entity     = get_irg_entity(irg);
2161         exc_entry *exc_list   = NEW_ARR_F(exc_entry, 0);
2162         int i, n;
2163
2164         cg       = ia32_cg;
2165         isa      = cg->isa;
2166         do_pic   = cg->birg->main_env->options->pic;
2167
2168         ia32_register_emitters();
2169
2170         get_unique_label(pic_base_label, sizeof(pic_base_label), ".PIC_BASE");
2171
2172         be_dbg_method_begin(entity, be_abi_get_stack_layout(cg->birg->abi));
2173         be_gas_emit_function_prolog(entity, ia32_cg_config.function_alignment);
2174
2175         /* we use links to point to target blocks */
2176         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
2177         irg_block_walk_graph(irg, ia32_gen_labels, NULL, &exc_list);
2178
2179         /* initialize next block links */
2180         n = ARR_LEN(cg->blk_sched);
2181         for (i = 0; i < n; ++i) {
2182                 ir_node *block = cg->blk_sched[i];
2183                 ir_node *prev  = i > 0 ? cg->blk_sched[i-1] : NULL;
2184
2185                 set_irn_link(block, prev);
2186         }
2187
2188         for (i = 0; i < n; ++i) {
2189                 ir_node *block = cg->blk_sched[i];
2190
2191                 ia32_gen_block(block);
2192         }
2193
2194         be_gas_emit_function_epilog(entity);
2195         be_dbg_method_end();
2196         be_emit_char('\n');
2197         be_emit_write_line();
2198
2199         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
2200
2201         /* Sort the exception table using the exception label id's.
2202            Those are ascending with ascending addresses. */
2203         qsort(exc_list, ARR_LEN(exc_list), sizeof(exc_list[0]), cmp_exc_entry);
2204         {
2205                 int i;
2206
2207                 for (i = 0; i < ARR_LEN(exc_list); ++i) {
2208                         be_emit_cstring("\t.long ");
2209                         ia32_emit_exc_label(exc_list[i].exc_instr);
2210                         be_emit_char('\n');
2211                         be_emit_cstring("\t.long ");
2212                         ia32_emit_block_name(exc_list[i].block);
2213                         be_emit_char('\n');
2214                 }
2215         }
2216         DEL_ARR_F(exc_list);
2217 }
2218
2219 static const lc_opt_table_entry_t ia32_emitter_options[] = {
2220         LC_OPT_ENT_BOOL("mark_spill_reload",   "mark spills and reloads with ud opcodes", &mark_spill_reload),
2221         LC_OPT_LAST
2222 };
2223
2224 void ia32_init_emitter(void)
2225 {
2226         lc_opt_entry_t *be_grp;
2227         lc_opt_entry_t *ia32_grp;
2228
2229         be_grp   = lc_opt_get_grp(firm_opt_get_root(), "be");
2230         ia32_grp = lc_opt_get_grp(be_grp, "ia32");
2231
2232         lc_opt_add_table(ia32_grp, ia32_emitter_options);
2233
2234         FIRM_DBG_REGISTER(dbg, "firm.be.ia32.emitter");
2235 }