a59027e6aaf37f683a4e65f9634f768a667ac69f
[libfirm] / ir / be / sparc / sparc_emitter.c
1 /*
2  * Copyright (C) 1995-2010 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   emit assembler for a backend graph
23  * @version $Id$
24  */
25 #include "config.h"
26
27 #include <limits.h>
28
29 #include "xmalloc.h"
30 #include "tv.h"
31 #include "iredges.h"
32 #include "debug.h"
33 #include "irgwalk.h"
34 #include "irprintf.h"
35 #include "irop_t.h"
36 #include "irargs_t.h"
37 #include "irprog.h"
38 #include "irargs_t.h"
39 #include "error.h"
40 #include "raw_bitset.h"
41 #include "dbginfo.h"
42 #include "heights.h"
43
44 #include "../besched.h"
45 #include "../beblocksched.h"
46 #include "../beirg.h"
47 #include "../begnuas.h"
48 #include "../be_dbgout.h"
49 #include "../benode.h"
50 #include "../bestack.h"
51
52 #include "sparc_emitter.h"
53 #include "gen_sparc_emitter.h"
54 #include "sparc_nodes_attr.h"
55 #include "sparc_new_nodes.h"
56 #include "gen_sparc_regalloc_if.h"
57
58 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
59
60 static ir_heights_t  *heights;
61 static const ir_node *delay_slot_filler; /**< this node has been choosen to fill
62                                               the next delay slot */
63
64 static void sparc_emit_node(const ir_node *node);
65
66 /**
67  * Returns the register at in position pos.
68  */
69 static const arch_register_t *get_in_reg(const ir_node *node, int pos)
70 {
71         ir_node                *op;
72         const arch_register_t  *reg = NULL;
73
74         assert(get_irn_arity(node) > pos && "Invalid IN position");
75
76         /* The out register of the operator at position pos is the
77            in register we need. */
78         op = get_irn_n(node, pos);
79
80         reg = arch_get_irn_register(op);
81
82         assert(reg && "no in register found");
83         return reg;
84 }
85
86 /**
87  * Returns the register at out position pos.
88  */
89 static const arch_register_t *get_out_reg(const ir_node *node, int pos)
90 {
91         ir_node                *proj;
92         const arch_register_t  *reg = NULL;
93
94         /* 1st case: irn is not of mode_T, so it has only                 */
95         /*           one OUT register -> good                             */
96         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
97         /*           Proj with the corresponding projnum for the register */
98
99         if (get_irn_mode(node) != mode_T) {
100                 reg = arch_get_irn_register(node);
101         } else if (is_sparc_irn(node)) {
102                 reg = arch_irn_get_register(node, pos);
103         } else {
104                 const ir_edge_t *edge;
105
106                 foreach_out_edge(node, edge) {
107                         proj = get_edge_src_irn(edge);
108                         assert(is_Proj(proj) && "non-Proj from mode_T node");
109                         if (get_Proj_proj(proj) == pos) {
110                                 reg = arch_get_irn_register(proj);
111                                 break;
112                         }
113                 }
114         }
115
116         assert(reg && "no out register found");
117         return reg;
118 }
119
120 static bool is_valid_immediate(int32_t value)
121 {
122         return -4096 <= value && value < 4096;
123 }
124
125 void sparc_emit_immediate(const ir_node *node)
126 {
127         const sparc_attr_t *attr   = get_sparc_attr_const(node);
128         ir_entity          *entity = attr->immediate_value_entity;
129
130         if (entity == NULL) {
131                 int32_t value = attr->immediate_value;
132                 assert(is_valid_immediate(value));
133                 be_emit_irprintf("%d", value);
134         } else {
135                 be_emit_cstring("%lo(");
136                 be_gas_emit_entity(entity);
137                 if (attr->immediate_value != 0) {
138                         be_emit_irprintf("%+d", attr->immediate_value);
139                 }
140                 be_emit_char(')');
141         }
142 }
143
144 void sparc_emit_high_immediate(const ir_node *node)
145 {
146         const sparc_attr_t *attr   = get_sparc_attr_const(node);
147         ir_entity          *entity = attr->immediate_value_entity;
148
149         be_emit_cstring("%hi(");
150         if (entity == NULL) {
151                 uint32_t value = (uint32_t) attr->immediate_value;
152                 be_emit_irprintf("0x%X", value);
153         } else {
154                 be_gas_emit_entity(entity);
155                 if (attr->immediate_value != 0) {
156                         be_emit_irprintf("%+d", attr->immediate_value);
157                 }
158         }
159         be_emit_char(')');
160 }
161
162 void sparc_emit_source_register(const ir_node *node, int pos)
163 {
164         const arch_register_t *reg = get_in_reg(node, pos);
165         be_emit_char('%');
166         be_emit_string(arch_register_get_name(reg));
167 }
168
169 void sparc_emit_dest_register(const ir_node *node, int pos)
170 {
171         const arch_register_t *reg = get_out_reg(node, pos);
172         be_emit_char('%');
173         be_emit_string(arch_register_get_name(reg));
174 }
175
176 /**
177  * Emits either a imm or register depending on arity of node
178  * @param node
179  * @param register no (-1 if no register)
180  */
181 void sparc_emit_reg_or_imm(const ir_node *node, int pos)
182 {
183         if (get_irn_arity(node) > pos) {
184                 // we have reg input
185                 sparc_emit_source_register(node, pos);
186         } else {
187                 // we have a imm input
188                 sparc_emit_immediate(node);
189         }
190 }
191
192 static bool is_stack_pointer_relative(const ir_node *node)
193 {
194         const arch_register_t *sp = &sparc_registers[REG_SP];
195         return (is_sparc_St(node) && get_in_reg(node, n_sparc_St_ptr) == sp)
196             || (is_sparc_Ld(node) && get_in_reg(node, n_sparc_Ld_ptr) == sp);
197 }
198
199 /**
200  * emit SP offset
201  */
202 void sparc_emit_offset(const ir_node *node, int offset_node_pos)
203 {
204         const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
205
206         if (attr->is_reg_reg) {
207                 assert(!attr->is_frame_entity);
208                 assert(attr->base.immediate_value == 0);
209                 assert(attr->base.immediate_value_entity == NULL);
210                 be_emit_char('+');
211                 sparc_emit_source_register(node, offset_node_pos);
212         } else if (attr->is_frame_entity) {
213                 int32_t offset = attr->base.immediate_value;
214                 /* bad hack: the real stack stuff is behind the always-there spill
215                  * space for the register window and stack */
216                 if (is_stack_pointer_relative(node))
217                         offset += SPARC_MIN_STACKSIZE;
218                 if (offset != 0) {
219                         assert(is_valid_immediate(offset));
220                         be_emit_irprintf("%+ld", offset);
221                 }
222         } else if (attr->base.immediate_value != 0
223                         || attr->base.immediate_value_entity != NULL) {
224                 be_emit_char('+');
225                 sparc_emit_immediate(node);
226         }
227 }
228
229 void sparc_emit_float_load_store_mode(const ir_node *node)
230 {
231         const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
232         ir_mode *mode = attr->load_store_mode;
233         int      bits = get_mode_size_bits(mode);
234
235         assert(mode_is_float(mode));
236
237         switch (bits) {
238         case 32:  return;
239         case 64:  be_emit_char('d'); return;
240         case 128: be_emit_char('q'); return;
241         }
242         panic("invalid flaot load/store mode %+F", mode);
243 }
244
245 /**
246  *  Emit load mode char
247  */
248 void sparc_emit_load_mode(const ir_node *node)
249 {
250         const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
251         ir_mode *mode      = attr->load_store_mode;
252         int      bits      = get_mode_size_bits(mode);
253         bool     is_signed = mode_is_signed(mode);
254
255         if (bits == 16) {
256                 be_emit_string(is_signed ? "sh" : "uh");
257         } else if (bits == 8) {
258                 be_emit_string(is_signed ? "sb" : "ub");
259         } else if (bits == 64) {
260                 be_emit_char('d');
261         } else {
262                 assert(bits == 32);
263         }
264 }
265
266 /**
267  * Emit store mode char
268  */
269 void sparc_emit_store_mode(const ir_node *node)
270 {
271         const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
272         ir_mode *mode      = attr->load_store_mode;
273         int      bits      = get_mode_size_bits(mode);
274
275         if (bits == 16) {
276                 be_emit_string("h");
277         } else if (bits == 8) {
278                 be_emit_string("b");
279         } else if (bits == 64) {
280                 be_emit_char('d');
281         } else {
282                 assert(bits == 32);
283         }
284 }
285
286 /**
287  * emit integer signed/unsigned prefix char
288  */
289 void sparc_emit_mode_sign_prefix(const ir_node *node)
290 {
291         ir_mode *mode      = get_irn_mode(node);
292         bool     is_signed = mode_is_signed(mode);
293         be_emit_string(is_signed ? "s" : "u");
294 }
295
296 static void emit_fp_suffix(const ir_mode *mode)
297 {
298         unsigned bits = get_mode_size_bits(mode);
299         assert(mode_is_float(mode));
300
301         if (bits == 32) {
302                 be_emit_char('s');
303         } else if (bits == 64) {
304                 be_emit_char('d');
305         } else if (bits == 128) {
306                 be_emit_char('q');
307         } else {
308                 panic("invalid FP mode");
309         }
310 }
311
312 void sparc_emit_fp_conv_source(const ir_node *node)
313 {
314         const sparc_fp_conv_attr_t *attr = get_sparc_fp_conv_attr_const(node);
315         emit_fp_suffix(attr->src_mode);
316 }
317
318 void sparc_emit_fp_conv_destination(const ir_node *node)
319 {
320         const sparc_fp_conv_attr_t *attr = get_sparc_fp_conv_attr_const(node);
321         emit_fp_suffix(attr->dest_mode);
322 }
323
324 /**
325  * emits the FP mode suffix char
326  */
327 void sparc_emit_fp_mode_suffix(const ir_node *node)
328 {
329         const sparc_fp_attr_t *attr = get_sparc_fp_attr_const(node);
330         emit_fp_suffix(attr->fp_mode);
331 }
332
333 static ir_node *get_jump_target(const ir_node *jump)
334 {
335         return (ir_node*)get_irn_link(jump);
336 }
337
338 /**
339  * Returns the target label for a control flow node.
340  */
341 static void sparc_emit_cfop_target(const ir_node *node)
342 {
343         ir_node *block = get_jump_target(node);
344         be_gas_emit_block_name(block);
345 }
346
347 static int get_sparc_Call_dest_addr_pos(const ir_node *node)
348 {
349         return get_irn_arity(node)-1;
350 }
351
352 static bool ba_is_fallthrough(const ir_node *node)
353 {
354         ir_node *block      = get_nodes_block(node);
355         ir_node *next_block = (ir_node*)get_irn_link(block);
356         return get_irn_link(node) == next_block;
357 }
358
359 static bool is_no_instruction(const ir_node *node)
360 {
361         /* copies are nops if src_reg == dest_reg */
362         if (be_is_Copy(node) || be_is_CopyKeep(node)) {
363                 const arch_register_t *src_reg  = get_in_reg(node, 0);
364                 const arch_register_t *dest_reg = get_out_reg(node, 0);
365
366                 if (src_reg == dest_reg)
367                         return true;
368         }
369         if (be_is_IncSP(node) && be_get_IncSP_offset(node) == 0)
370                 return true;
371         /* Ba is not emitted if it is a simple fallthrough */
372         if (is_sparc_Ba(node) && ba_is_fallthrough(node))
373                 return true;
374
375         return be_is_Keep(node) || be_is_Barrier(node) || be_is_Start(node)
376                 || is_Phi(node);
377 }
378
379 static bool has_delay_slot(const ir_node *node)
380 {
381         if (is_sparc_Ba(node) && ba_is_fallthrough(node))
382                 return false;
383
384         return is_sparc_Bicc(node) || is_sparc_fbfcc(node) || is_sparc_Ba(node)
385                 || is_sparc_SwitchJmp(node) || is_sparc_Call(node)
386                 || is_sparc_SDiv(node) || is_sparc_UDiv(node)
387                 || be_is_Return(node);
388 }
389
390 /** returns true if the emitter for this sparc node can produce more than one
391  * actual sparc instruction.
392  * Usually it is a bad sign if we have to add instructions here. We should
393  * rather try to get them lowered down. So we can actually put them into
394  * delay slots and make them more accessible to the scheduler.
395  */
396 static bool emits_multiple_instructions(const ir_node *node)
397 {
398         if (has_delay_slot(node))
399                 return true;
400
401         return is_sparc_Mulh(node) || is_sparc_SDiv(node) || is_sparc_UDiv(node)
402                 || be_is_MemPerm(node) || be_is_Perm(node);
403 }
404
405 /**
406  * search for an instruction that can fill the delay slot of @p node
407  */
408 static const ir_node *pick_delay_slot_for(const ir_node *node)
409 {
410         const ir_node *check      = node;
411         const ir_node *schedpoint = node;
412         unsigned       tries      = 0;
413         /* currently we don't track which registers are still alive, so we can't
414          * pick any other instructions other than the one directly preceding */
415         static const unsigned PICK_DELAY_SLOT_MAX_DISTANCE = 1;
416
417         assert(has_delay_slot(node));
418
419         if (is_sparc_Call(node)) {
420                 const sparc_attr_t *attr   = get_sparc_attr_const(node);
421                 ir_entity          *entity = attr->immediate_value_entity;
422                 if (entity != NULL) {
423                         check = NULL; /* pick any instruction, dependencies on Call
424                                          don't matter */
425                 } else {
426                         /* we only need to check the value for the call destination */
427                         check = get_irn_n(node, get_sparc_Call_dest_addr_pos(node));
428                 }
429
430                 /* the Call also destroys the value of %o7, but since this is currently
431                  * marked as ignore register in the backend, it should never be used by
432                  * the instruction in the delay slot. */
433         } else if (be_is_Return(node)) {
434                 /* we only have to check the jump destination value */
435                 int arity = get_irn_arity(node);
436                 int i;
437
438                 check = NULL;
439                 for (i = 0; i < arity; ++i) {
440                         ir_node               *in  = get_irn_n(node, i);
441                         const arch_register_t *reg = arch_get_irn_register(in);
442                         if (reg == &sparc_registers[REG_O7]) {
443                                 check = skip_Proj(in);
444                                 break;
445                         }
446                 }
447         } else {
448                 check = node;
449         }
450
451         while (sched_has_prev(schedpoint)) {
452                 schedpoint = sched_prev(schedpoint);
453
454                 if (has_delay_slot(schedpoint))
455                         break;
456
457                 /* skip things which don't really result in instructions */
458                 if (is_no_instruction(schedpoint))
459                         continue;
460
461                 if (tries++ >= PICK_DELAY_SLOT_MAX_DISTANCE)
462                         break;
463
464                 if (emits_multiple_instructions(schedpoint))
465                         continue;
466
467                 /* allowed for delayslot: any instruction which is not necessary to
468                  * compute an input to the branch. */
469                 if (check != NULL
470                                 && heights_reachable_in_block(heights, check, schedpoint))
471                         continue;
472
473                 /* found something */
474                 return schedpoint;
475         }
476
477         return NULL;
478 }
479
480 /**
481  * Emits code for stack space management
482  */
483 static void emit_be_IncSP(const ir_node *irn)
484 {
485         int offs = -be_get_IncSP_offset(irn);
486
487         if (offs == 0)
488                 return;
489
490         /* SPARC stack grows downwards */
491         if (offs < 0) {
492                 be_emit_cstring("\tsub ");
493                 offs = -offs;
494         } else {
495                 be_emit_cstring("\tadd ");
496         }
497
498         sparc_emit_source_register(irn, 0);
499         be_emit_irprintf(", %d", offs);
500         be_emit_cstring(", ");
501         sparc_emit_dest_register(irn, 0);
502         be_emit_finish_line_gas(irn);
503 }
504
505 /**
506  * emits code for mulh
507  */
508 static void emit_sparc_Mulh(const ir_node *irn)
509 {
510         be_emit_cstring("\t");
511         sparc_emit_mode_sign_prefix(irn);
512         be_emit_cstring("mul ");
513
514         sparc_emit_source_register(irn, 0);
515         be_emit_cstring(", ");
516         sparc_emit_reg_or_imm(irn, 1);
517         be_emit_cstring(", ");
518         sparc_emit_dest_register(irn, 0);
519         be_emit_finish_line_gas(irn);
520
521         // our result is in the y register now
522         // we just copy it to the assigned target reg
523         be_emit_cstring("\tmov %y, ");
524         sparc_emit_dest_register(irn, 0);
525         be_emit_finish_line_gas(irn);
526 }
527
528 static void fill_delay_slot(void)
529 {
530         if (delay_slot_filler != NULL) {
531                 sparc_emit_node(delay_slot_filler);
532                 delay_slot_filler = NULL;
533         } else {
534                 be_emit_cstring("\tnop\n");
535                 be_emit_write_line();
536         }
537 }
538
539 static void emit_sparc_Div(const ir_node *node, bool is_signed)
540 {
541         /* can we get the delay count of the wr instruction somewhere? */
542         unsigned wry_delay_count = 3;
543         unsigned i;
544
545         be_emit_cstring("\twr ");
546         sparc_emit_source_register(node, 0);
547         be_emit_cstring(", 0, %y");
548         be_emit_finish_line_gas(node);
549
550         for (i = 0; i < wry_delay_count; ++i) {
551                 fill_delay_slot();
552         }
553
554         be_emit_irprintf("\t%s ", is_signed ? "sdiv" : "udiv");
555         sparc_emit_source_register(node, 1);
556         be_emit_cstring(", ");
557         sparc_emit_reg_or_imm(node, 2);
558         be_emit_cstring(", ");
559         sparc_emit_dest_register(node, 0);
560         be_emit_finish_line_gas(node);
561 }
562
563 static void emit_sparc_SDiv(const ir_node *node)
564 {
565         emit_sparc_Div(node, true);
566 }
567
568 static void emit_sparc_UDiv(const ir_node *node)
569 {
570         emit_sparc_Div(node, false);
571 }
572
573 /**
574  * Emits code for Call node
575  */
576 static void emit_sparc_Call(const ir_node *node)
577 {
578         const sparc_attr_t *attr   = get_sparc_attr_const(node);
579         ir_entity          *entity = attr->immediate_value_entity;
580
581         be_emit_cstring("\tcall ");
582         if (entity != NULL) {
583             be_gas_emit_entity(entity);
584             if (attr->immediate_value != 0) {
585                         be_emit_irprintf("%+d", attr->immediate_value);
586                 }
587                 be_emit_cstring(", 0");
588         } else {
589                 int dest_addr = get_sparc_Call_dest_addr_pos(node);
590                 sparc_emit_source_register(node, dest_addr);
591         }
592         be_emit_finish_line_gas(node);
593
594         fill_delay_slot();
595 }
596
597 /**
598  * Emit code for Perm node
599  */
600 static void emit_be_Perm(const ir_node *irn)
601 {
602         be_emit_cstring("\txor ");
603         sparc_emit_source_register(irn, 1);
604         be_emit_cstring(", ");
605         sparc_emit_source_register(irn, 0);
606         be_emit_cstring(", ");
607         sparc_emit_source_register(irn, 0);
608         be_emit_finish_line_gas(NULL);
609
610         be_emit_cstring("\txor ");
611         sparc_emit_source_register(irn, 1);
612         be_emit_cstring(", ");
613         sparc_emit_source_register(irn, 0);
614         be_emit_cstring(", ");
615         sparc_emit_source_register(irn, 1);
616         be_emit_finish_line_gas(NULL);
617
618         be_emit_cstring("\txor ");
619         sparc_emit_source_register(irn, 1);
620         be_emit_cstring(", ");
621         sparc_emit_source_register(irn, 0);
622         be_emit_cstring(", ");
623         sparc_emit_source_register(irn, 0);
624         be_emit_finish_line_gas(irn);
625 }
626
627 static void emit_be_MemPerm(const ir_node *node)
628 {
629         int i;
630         int memperm_arity;
631         int sp_change = 0;
632         ir_graph          *irg    = get_irn_irg(node);
633         be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
634
635         /* this implementation only works with frame pointers currently */
636         assert(layout->sp_relative == false);
637
638         /* TODO: this implementation is slower than necessary.
639            The longterm goal is however to avoid the memperm node completely */
640
641         memperm_arity = be_get_MemPerm_entity_arity(node);
642         // we use our local registers - so this is limited to 8 inputs !
643         if (memperm_arity > 8)
644                 panic("memperm with more than 8 inputs not supported yet");
645
646         be_emit_irprintf("\tsub %%sp, %d, %%sp", memperm_arity*4);
647         be_emit_finish_line_gas(node);
648
649         for (i = 0; i < memperm_arity; ++i) {
650                 ir_entity *entity = be_get_MemPerm_in_entity(node, i);
651                 int        offset = be_get_stack_entity_offset(layout, entity, 0);
652
653                 /* spill register */
654                 be_emit_irprintf("\tst %%l%d, [%%sp%+d]", i, sp_change + SPARC_MIN_STACKSIZE);
655                 be_emit_finish_line_gas(node);
656
657                 /* load from entity */
658                 be_emit_irprintf("\tld [%%fp%+d], %%l%d", offset, i);
659                 be_emit_finish_line_gas(node);
660                 sp_change += 4;
661         }
662
663         for (i = memperm_arity-1; i >= 0; --i) {
664                 ir_entity *entity = be_get_MemPerm_out_entity(node, i);
665                 int        offset = be_get_stack_entity_offset(layout, entity, 0);
666
667                 sp_change -= 4;
668
669                 /* store to new entity */
670                 be_emit_irprintf("\tst %%l%d, [%%fp%+d]", i, offset);
671                 be_emit_finish_line_gas(node);
672                 /* restore register */
673                 be_emit_irprintf("\tld [%%sp%+d], %%l%d", sp_change + SPARC_MIN_STACKSIZE, i);
674                 be_emit_finish_line_gas(node);
675         }
676
677         be_emit_irprintf("\tadd %%sp, %d, %%sp", memperm_arity*4);
678         be_emit_finish_line_gas(node);
679
680         assert(sp_change == 0);
681 }
682
683 static void emit_be_Return(const ir_node *node)
684 {
685         const char *destreg = "%o7";
686
687         /* hack: we don't explicitely model register changes because of the
688          * restore node. So we have to do it manually here */
689         if (delay_slot_filler != NULL &&
690                         (is_sparc_Restore(delay_slot_filler)
691                          || is_sparc_RestoreZero(delay_slot_filler))) {
692                 destreg = "%i7";
693         }
694         be_emit_cstring("\tjmp ");
695         be_emit_string(destreg);
696         be_emit_cstring("+8");
697         be_emit_finish_line_gas(node);
698         fill_delay_slot();
699 }
700
701 static void emit_sparc_FrameAddr(const ir_node *node)
702 {
703         const sparc_attr_t *attr = get_sparc_attr_const(node);
704
705         // no need to fix offset as we are adressing via the framepointer
706         if (attr->immediate_value >= 0) {
707                 be_emit_cstring("\tadd ");
708                 sparc_emit_source_register(node, 0);
709                 be_emit_cstring(", ");
710                 be_emit_irprintf("%ld", attr->immediate_value);
711         } else {
712                 be_emit_cstring("\tsub ");
713                 sparc_emit_source_register(node, 0);
714                 be_emit_cstring(", ");
715                 be_emit_irprintf("%ld", -attr->immediate_value);
716         }
717
718         be_emit_cstring(", ");
719         sparc_emit_dest_register(node, 0);
720         be_emit_finish_line_gas(node);
721 }
722
723 static const char *get_icc_unsigned(ir_relation relation)
724 {
725         switch (relation & (ir_relation_less_equal_greater)) {
726         case ir_relation_false:              return "bn";
727         case ir_relation_equal:              return "be";
728         case ir_relation_less:               return "blu";
729         case ir_relation_less_equal:         return "bleu";
730         case ir_relation_greater:            return "bgu";
731         case ir_relation_greater_equal:      return "bgeu";
732         case ir_relation_less_greater:       return "bne";
733         case ir_relation_less_equal_greater: return "ba";
734         default: panic("Cmp has unsupported relation");
735         }
736 }
737
738 static const char *get_icc_signed(ir_relation relation)
739 {
740         switch (relation & (ir_relation_less_equal_greater)) {
741         case ir_relation_false:              return "bn";
742         case ir_relation_equal:              return "be";
743         case ir_relation_less:               return "bl";
744         case ir_relation_less_equal:         return "ble";
745         case ir_relation_greater:            return "bg";
746         case ir_relation_greater_equal:      return "bge";
747         case ir_relation_less_greater:       return "bne";
748         case ir_relation_less_equal_greater: return "ba";
749         default: panic("Cmp has unsupported relation");
750         }
751 }
752
753 static const char *get_fcc(ir_relation relation)
754 {
755         switch (relation) {
756         case ir_relation_false:                   return "fbn";
757         case ir_relation_equal:                   return "fbe";
758         case ir_relation_less:                    return "fbl";
759         case ir_relation_less_equal:              return "fble";
760         case ir_relation_greater:                 return "fbg";
761         case ir_relation_greater_equal:           return "fbge";
762         case ir_relation_less_greater:            return "fblg";
763         case ir_relation_less_equal_greater:      return "fbo";
764         case ir_relation_unordered:               return "fbu";
765         case ir_relation_unordered_equal:         return "fbue";
766         case ir_relation_unordered_less:          return "fbul";
767         case ir_relation_unordered_less_equal:    return "fbule";
768         case ir_relation_unordered_greater:       return "fbug";
769         case ir_relation_unordered_greater_equal: return "fbuge";
770         case ir_relation_unordered_less_greater:  return "fbne";
771         case ir_relation_true:                    return "fba";
772         }
773         panic("invalid relation");
774 }
775
776 typedef const char* (*get_cc_func)(ir_relation relation);
777
778 static void emit_sparc_branch(const ir_node *node, get_cc_func get_cc)
779 {
780         const sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr_const(node);
781         ir_relation      relation    = attr->relation;
782         const ir_node   *proj_true   = NULL;
783         const ir_node   *proj_false  = NULL;
784         const ir_edge_t *edge;
785         const ir_node   *block;
786         const ir_node   *next_block;
787
788         foreach_out_edge(node, edge) {
789                 ir_node *proj = get_edge_src_irn(edge);
790                 long nr = get_Proj_proj(proj);
791                 if (nr == pn_Cond_true) {
792                         proj_true = proj;
793                 } else {
794                         proj_false = proj;
795                 }
796         }
797
798         /* for now, the code works for scheduled and non-schedules blocks */
799         block = get_nodes_block(node);
800
801         /* we have a block schedule */
802         next_block = (ir_node*)get_irn_link(block);
803
804         if (get_irn_link(proj_true) == next_block) {
805                 /* exchange both proj's so the second one can be omitted */
806                 const ir_node *t = proj_true;
807
808                 proj_true  = proj_false;
809                 proj_false = t;
810                 relation   = get_negated_relation(relation);
811         }
812
813         /* emit the true proj */
814         be_emit_cstring("\t");
815         be_emit_string(get_cc(relation));
816         be_emit_char(' ');
817         sparc_emit_cfop_target(proj_true);
818         be_emit_finish_line_gas(proj_true);
819
820         fill_delay_slot();
821
822         if (get_irn_link(proj_false) == next_block) {
823                 be_emit_cstring("\t/* fallthrough to ");
824                 sparc_emit_cfop_target(proj_false);
825                 be_emit_cstring(" */");
826                 be_emit_finish_line_gas(proj_false);
827         } else {
828                 be_emit_cstring("\tba ");
829                 sparc_emit_cfop_target(proj_false);
830                 be_emit_finish_line_gas(proj_false);
831                 fill_delay_slot();
832         }
833 }
834
835 static void emit_sparc_Bicc(const ir_node *node)
836 {
837         const sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr_const(node);
838         bool             is_unsigned = attr->is_unsigned;
839         emit_sparc_branch(node, is_unsigned ? get_icc_unsigned : get_icc_signed);
840 }
841
842 static void emit_sparc_fbfcc(const ir_node *node)
843 {
844         emit_sparc_branch(node, get_fcc);
845 }
846
847 static void emit_sparc_Ba(const ir_node *node)
848 {
849         if (ba_is_fallthrough(node)) {
850                 be_emit_cstring("\t/* fallthrough to ");
851                 sparc_emit_cfop_target(node);
852                 be_emit_cstring(" */");
853         } else {
854                 be_emit_cstring("\tba ");
855                 sparc_emit_cfop_target(node);
856                 be_emit_finish_line_gas(node);
857                 fill_delay_slot();
858         }
859         be_emit_finish_line_gas(node);
860 }
861
862 static void emit_jump_table(const ir_node *node)
863 {
864         const sparc_switch_jmp_attr_t *attr = get_sparc_switch_jmp_attr_const(node);
865         long             switch_max    = LONG_MIN;
866         long             default_pn    = attr->default_proj_num;
867         ir_entity       *entity        = attr->jump_table;
868         ir_node         *default_block = NULL;
869         unsigned long    length;
870         const ir_edge_t *edge;
871         unsigned         i;
872         ir_node        **table;
873
874         /* go over all proj's and collect them */
875         foreach_out_edge(node, edge) {
876                 ir_node *proj = get_edge_src_irn(edge);
877                 long     pn   = get_Proj_proj(proj);
878
879                 /* check for default proj */
880                 if (pn == default_pn) {
881                         assert(default_block == NULL); /* more than 1 default_pn? */
882                         default_block = get_jump_target(proj);
883                 } else {
884                         switch_max = pn > switch_max ? pn : switch_max;
885                 }
886         }
887         assert(switch_max > LONG_MIN);
888
889         length = (unsigned long) switch_max + 1;
890         /* the 16000 isn't a real limit of the architecture. But should protect us
891          * from seamingly endless compiler runs */
892         if (length > 16000) {
893                 /* switch lowerer should have broken this monster to pieces... */
894                 panic("too large switch encountered");
895         }
896
897         table = XMALLOCNZ(ir_node*, length);
898         foreach_out_edge(node, edge) {
899                 ir_node *proj = get_edge_src_irn(edge);
900                 long     pn   = get_Proj_proj(proj);
901                 if (pn == default_pn)
902                         continue;
903
904                 table[pn] = get_jump_target(proj);
905         }
906
907         /* emit table */
908         be_gas_emit_switch_section(GAS_SECTION_RODATA);
909         be_emit_cstring("\t.align 4\n");
910         be_gas_emit_entity(entity);
911         be_emit_cstring(":\n");
912         for (i = 0; i < length; ++i) {
913                 ir_node *block = table[i];
914                 if (block == NULL)
915                         block = default_block;
916                 be_emit_cstring("\t.long ");
917                 be_gas_emit_block_name(block);
918                 be_emit_char('\n');
919                 be_emit_write_line();
920         }
921         be_gas_emit_switch_section(GAS_SECTION_TEXT);
922
923         xfree(table);
924 }
925
926 static void emit_sparc_SwitchJmp(const ir_node *node)
927 {
928         be_emit_cstring("\tjmp ");
929         sparc_emit_source_register(node, 0);
930         be_emit_finish_line_gas(node);
931         fill_delay_slot();
932
933         emit_jump_table(node);
934 }
935
936 static void emit_fmov(const ir_node *node, const arch_register_t *src_reg,
937                       const arch_register_t *dst_reg)
938 {
939         be_emit_cstring("\tfmovs %");
940         be_emit_string(arch_register_get_name(src_reg));
941         be_emit_cstring(", %");
942         be_emit_string(arch_register_get_name(dst_reg));
943         be_emit_finish_line_gas(node);
944 }
945
946 static const arch_register_t *get_next_fp_reg(const arch_register_t *reg)
947 {
948         unsigned index = reg->index;
949         assert(reg == &sparc_registers[index]);
950         index++;
951         assert(index - REG_F0 < N_sparc_fp_REGS);
952         return &sparc_registers[index];
953 }
954
955 static void emit_be_Copy(const ir_node *node)
956 {
957         ir_mode               *mode    = get_irn_mode(node);
958         const arch_register_t *src_reg = get_in_reg(node, 0);
959         const arch_register_t *dst_reg = get_out_reg(node, 0);
960
961         if (src_reg == dst_reg)
962                 return;
963
964         if (mode_is_float(mode)) {
965                 unsigned bits = get_mode_size_bits(mode);
966                 int      n    = bits > 32 ? bits > 64 ? 3 : 1 : 0;
967                 int      i;
968                 emit_fmov(node, src_reg, dst_reg);
969                 for (i = 0; i < n; ++i) {
970                         src_reg = get_next_fp_reg(src_reg);
971                         dst_reg = get_next_fp_reg(dst_reg);
972                         emit_fmov(node, src_reg, dst_reg);
973                 }
974         } else if (mode_is_data(mode)) {
975                 be_emit_cstring("\tmov ");
976                 sparc_emit_source_register(node, 0);
977                 be_emit_cstring(", ");
978                 sparc_emit_dest_register(node, 0);
979                 be_emit_finish_line_gas(node);
980         } else {
981                 panic("emit_be_Copy: invalid mode");
982         }
983 }
984
985 static void emit_nothing(const ir_node *irn)
986 {
987         (void) irn;
988 }
989
990 typedef void (*emit_func) (const ir_node *);
991
992 static inline void set_emitter(ir_op *op, emit_func sparc_emit_node)
993 {
994         op->ops.generic = (op_func)sparc_emit_node;
995 }
996
997 /**
998  * Enters the emitter functions for handled nodes into the generic
999  * pointer of an opcode.
1000  */
1001 static void sparc_register_emitters(void)
1002 {
1003         /* first clear the generic function pointer for all ops */
1004         clear_irp_opcodes_generic_func();
1005         /* register all emitter functions defined in spec */
1006         sparc_register_spec_emitters();
1007
1008         /* custom emitter */
1009         set_emitter(op_be_Copy,         emit_be_Copy);
1010         set_emitter(op_be_CopyKeep,     emit_be_Copy);
1011         set_emitter(op_be_IncSP,        emit_be_IncSP);
1012         set_emitter(op_be_MemPerm,      emit_be_MemPerm);
1013         set_emitter(op_be_Perm,         emit_be_Perm);
1014         set_emitter(op_be_Return,       emit_be_Return);
1015         set_emitter(op_sparc_Ba,        emit_sparc_Ba);
1016         set_emitter(op_sparc_Bicc,      emit_sparc_Bicc);
1017         set_emitter(op_sparc_Call,      emit_sparc_Call);
1018         set_emitter(op_sparc_fbfcc,     emit_sparc_fbfcc);
1019         set_emitter(op_sparc_FrameAddr, emit_sparc_FrameAddr);
1020         set_emitter(op_sparc_Mulh,      emit_sparc_Mulh);
1021         set_emitter(op_sparc_SDiv,      emit_sparc_SDiv);
1022         set_emitter(op_sparc_SwitchJmp, emit_sparc_SwitchJmp);
1023         set_emitter(op_sparc_UDiv,      emit_sparc_UDiv);
1024
1025         /* no need to emit anything for the following nodes */
1026         set_emitter(op_be_Barrier, emit_nothing);
1027         set_emitter(op_be_Keep,    emit_nothing);
1028         set_emitter(op_be_Start,   emit_nothing);
1029         set_emitter(op_Phi,        emit_nothing);
1030 }
1031
1032 /**
1033  * Emits code for a node.
1034  */
1035 static void sparc_emit_node(const ir_node *node)
1036 {
1037         ir_op *op = get_irn_op(node);
1038
1039         if (op->ops.generic) {
1040                 emit_func func = (emit_func) op->ops.generic;
1041                 be_dbg_set_dbg_info(get_irn_dbg_info(node));
1042                 (*func) (node);
1043         } else {
1044                 panic("No emit handler for node %+F (graph %+F)\n", node,
1045                       current_ir_graph);
1046         }
1047 }
1048
1049 static ir_node *find_next_delay_slot(ir_node *from)
1050 {
1051         ir_node *schedpoint = from;
1052         while (!has_delay_slot(schedpoint)) {
1053                 if (!sched_has_next(schedpoint))
1054                         return NULL;
1055                 schedpoint = sched_next(schedpoint);
1056         }
1057         return schedpoint;
1058 }
1059
1060 /**
1061  * Walks over the nodes in a block connected by scheduling edges
1062  * and emits code for each node.
1063  */
1064 static void sparc_emit_block(ir_node *block)
1065 {
1066         ir_node *node;
1067         ir_node *next_delay_slot;
1068
1069         assert(is_Block(block));
1070
1071         be_gas_emit_block_name(block);
1072         be_emit_cstring(":\n");
1073         be_emit_write_line();
1074
1075         next_delay_slot = find_next_delay_slot(sched_first(block));
1076         if (next_delay_slot != NULL)
1077                 delay_slot_filler = pick_delay_slot_for(next_delay_slot);
1078
1079         sched_foreach(block, node) {
1080                 if (node == delay_slot_filler) {
1081                         continue;
1082                 }
1083
1084                 sparc_emit_node(node);
1085
1086                 if (node == next_delay_slot) {
1087                         assert(delay_slot_filler == NULL);
1088                         next_delay_slot = find_next_delay_slot(sched_next(node));
1089                         if (next_delay_slot != NULL)
1090                                 delay_slot_filler = pick_delay_slot_for(next_delay_slot);
1091                 }
1092         }
1093 }
1094
1095 /**
1096  * Emits code for function start.
1097  */
1098 static void sparc_emit_func_prolog(ir_graph *irg)
1099 {
1100         ir_entity *ent = get_irg_entity(irg);
1101         be_gas_emit_function_prolog(ent, 4);
1102         be_emit_write_line();
1103 }
1104
1105 /**
1106  * Emits code for function end
1107  */
1108 static void sparc_emit_func_epilog(ir_graph *irg)
1109 {
1110         ir_entity *ent = get_irg_entity(irg);
1111         const char *irg_name = get_entity_ld_name(ent);
1112         be_emit_write_line();
1113         be_emit_irprintf("\t.size  %s, .-%s\n", irg_name, irg_name);
1114         be_emit_cstring("# -- End ");
1115         be_emit_string(irg_name);
1116         be_emit_cstring("\n");
1117         be_emit_write_line();
1118 }
1119
1120 static void sparc_gen_labels(ir_node *block, void *env)
1121 {
1122         ir_node *pred;
1123         int n = get_Block_n_cfgpreds(block);
1124         (void) env;
1125
1126         for (n--; n >= 0; n--) {
1127                 pred = get_Block_cfgpred(block, n);
1128                 set_irn_link(pred, block); // link the pred of a block (which is a jmp)
1129         }
1130 }
1131
1132 void sparc_emit_routine(ir_graph *irg)
1133 {
1134         ir_entity  *entity = get_irg_entity(irg);
1135         ir_node   **block_schedule;
1136         size_t      i;
1137         size_t      n;
1138
1139         be_gas_elf_type_char      = '#';
1140         be_gas_object_file_format = OBJECT_FILE_FORMAT_ELF_SPARC;
1141
1142         heights = heights_new(irg);
1143
1144         /* register all emitter functions */
1145         sparc_register_emitters();
1146         be_dbg_method_begin(entity);
1147
1148         /* create the block schedule. For now, we don't need it earlier. */
1149         block_schedule = be_create_block_schedule(irg);
1150
1151         sparc_emit_func_prolog(irg);
1152         irg_block_walk_graph(irg, sparc_gen_labels, NULL, NULL);
1153
1154         /* inject block scheduling links & emit code of each block */
1155         n = ARR_LEN(block_schedule);
1156         for (i = 0; i < n; ++i) {
1157                 ir_node *block      = block_schedule[i];
1158                 ir_node *next_block = i+1 < n ? block_schedule[i+1] : NULL;
1159                 set_irn_link(block, next_block);
1160         }
1161
1162         for (i = 0; i < n; ++i) {
1163                 ir_node *block = block_schedule[i];
1164                 if (block == get_irg_end_block(irg))
1165                         continue;
1166                 sparc_emit_block(block);
1167         }
1168
1169         /* emit function epilog */
1170         sparc_emit_func_epilog(irg);
1171
1172         heights_free(heights);
1173 }
1174
1175 void sparc_init_emitter(void)
1176 {
1177         FIRM_DBG_REGISTER(dbg, "firm.be.sparc.emit");
1178 }