Remove address name SymConsts.
[libfirm] / ir / be / mips / mips_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   implementation of mips assembly emitter
23  * @author  Matthias Braun, Mehdi
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include <limits.h>
29
30 #include "xmalloc.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_t.h"
38 #include "irouts.h"
39 #include "tv.h"
40 #include "error.h"
41
42 #include "../besched.h"
43 #include "../benode.h"
44 #include "../beutil.h"
45 #include "../begnuas.h"
46
47 #include "mips_emitter.h"
48 #include "gen_mips_emitter.h"
49 #include "mips_nodes_attr.h"
50 #include "mips_new_nodes.h"
51 #include "mips_map_regs.h"
52
53 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
54
55 #define SNPRINTF_BUF_LEN 128
56
57 /**
58  * Returns the register at in position pos.
59  */
60 static const arch_register_t *get_in_reg(const ir_node *node, int pos)
61 {
62         ir_node                *op;
63         const arch_register_t  *reg = NULL;
64
65         assert(get_irn_arity(node) > pos && "Invalid IN position");
66
67         /* The out register of the operator at position pos is the
68            in register we need. */
69         op = get_irn_n(node, pos);
70
71         reg = arch_get_irn_register(op);
72
73         assert(reg && "no in register found");
74         return reg;
75 }
76
77 /**
78  * Returns the register at out position pos.
79  */
80 static const arch_register_t *get_out_reg(const ir_node *node, int pos)
81 {
82         ir_node                *proj;
83         const arch_register_t  *reg = NULL;
84
85         /* 1st case: irn is not of mode_T, so it has only                 */
86         /*           one OUT register -> good                             */
87         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
88         /*           Proj with the corresponding projnum for the register */
89
90         if (get_irn_mode(node) != mode_T) {
91                 reg = arch_get_irn_register(node);
92         } else if (is_mips_irn(node)) {
93                 reg = arch_irn_get_register(node, pos);
94         } else {
95                 const ir_edge_t *edge;
96
97                 foreach_out_edge(node, edge) {
98                         proj = get_edge_src_irn(edge);
99                         assert(is_Proj(proj) && "non-Proj from mode_T node");
100                         if (get_Proj_proj(proj) == pos) {
101                                 reg = arch_get_irn_register(proj);
102                                 break;
103                         }
104                 }
105         }
106
107         assert(reg && "no out register found");
108         return reg;
109 }
110
111 /*************************************************************
112  *             _       _    __   _          _
113  *            (_)     | |  / _| | |        | |
114  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
115  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
116  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
117  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
118  * | |                                       | |
119  * |_|                                       |_|
120  *************************************************************/
121
122 /**
123  * Emit the name of the source register at given input position.
124  */
125 void mips_emit_source_register(const ir_node *node, int pos)
126 {
127         const arch_register_t *reg = get_in_reg(node, pos);
128         be_emit_char('$');
129         be_emit_string(arch_register_get_name(reg));
130 }
131
132 /**
133  * Emit the name of the destination register at given output position.
134  */
135 void mips_emit_dest_register(const ir_node *node, int pos)
136 {
137         const arch_register_t *reg = get_out_reg(node, pos);
138         be_emit_char('$');
139         be_emit_string(arch_register_get_name(reg));
140 }
141
142 #if 0
143 static const char *get_symconst_str(ir_node *node)
144 {
145         ident *id;
146
147         switch (get_SymConst_kind(node)) {
148         case symconst_addr_ent:
149                 id = get_entity_ident(get_SymConst_entity(node));
150                 return get_id_str(id);
151         default:
152                 panic("Unsupported SymConst kind");
153         }
154
155         return NULL;
156 }
157
158 /**
159  * Return a const or symconst as string.
160  */
161 static const char *node_const_to_str(ir_node *n)
162 {
163         static char buf[64];
164         const mips_attr_t *attr = get_mips_attr(n);
165         long val;
166
167         if (is_mips_load_r(n) || is_mips_store_r(n)) {
168                 mips_attr_t *attr = get_mips_attr(n);
169                 ir_node *symconst;
170
171                 if (attr->tv != NULL) {
172                         val = get_tarval_long(attr->tv);
173                         snprintf(buf, sizeof(buf), "%ld", val);
174
175                         return buf;
176                 }
177                 if (attr->stack_entity != NULL) {
178                         snprintf(buf, sizeof(buf), "%d", attr->stack_entity_offset);
179                         return buf;
180                 }
181
182                 symconst = get_irn_n(n, 1);
183                 assert(get_irn_opcode(symconst) == iro_SymConst);
184
185                 return get_symconst_str(symconst);
186         } else if (is_mips_la(n)) {
187                 snprintf(buf, sizeof(buf), "%s", get_id_str(attr->symconst_id));
188                 return buf;
189         } else if (is_mips_lli(n)) {
190                 assert(attr->tv != NULL);
191                 if (get_mode_sign(get_tarval_mode(attr->tv))) {
192                         long val = get_tarval_long(attr->tv);
193                         snprintf(buf, sizeof(buf), "0x%04lX", val & 0xffff);
194                 } else {
195                         unsigned long val = get_tarval_long(attr->tv);
196                         snprintf(buf, sizeof(buf), "0x%04lX", val & 0xffff);
197                 }
198
199                 return buf;
200         } else if (is_mips_lui(n)) {
201                 assert(attr->tv != NULL);
202                 if (get_mode_sign(get_tarval_mode(attr->tv))) {
203                         long val = get_tarval_long(attr->tv);
204                         val = (val & 0xffff0000) >> 16;
205                         snprintf(buf, sizeof(buf), "0x%04lX", val & 0xffff);
206                 } else {
207                         unsigned long val = get_tarval_long(attr->tv);
208                         val = (val & 0xffff0000) >> 16;
209                         snprintf(buf, sizeof(buf), "0x%04lX", val & 0xffff);
210                 }
211
212                 return buf;
213         }
214
215         assert(attr->tv != NULL);
216         val = get_tarval_long(attr->tv);
217         snprintf(buf, sizeof(buf), "%ld", val);
218
219         return buf;
220 }
221 #endif
222
223 void mips_emit_load_store_address(const ir_node *node, int pos)
224 {
225         const mips_load_store_attr_t *attr = get_mips_load_store_attr_const(node);
226
227         be_emit_irprintf("%d(", attr->offset);
228         mips_emit_source_register(node, pos);
229         be_emit_char(')');
230 }
231
232 void mips_emit_immediate_suffix(const ir_node *node, int pos)
233 {
234         ir_node *op = get_irn_n(node, pos);
235         if (is_mips_Immediate(op))
236                 be_emit_char('i');
237 }
238
239 void mips_emit_immediate(const ir_node *node)
240 {
241         const mips_immediate_attr_t *attr = get_mips_immediate_attr_const(node);
242
243         switch (attr->imm_type) {
244         case MIPS_IMM_CONST:
245                 be_emit_irprintf("%d", attr->val);
246                 break;
247         case MIPS_IMM_SYMCONST_LO:
248                 be_emit_cstring("%lo($");
249                 be_emit_ident(get_entity_ld_ident(attr->entity));
250                 if (attr->val != 0) {
251                         be_emit_irprintf("%+d", attr->val);
252                 }
253                 be_emit_char(')');
254                 break;
255         case MIPS_IMM_SYMCONST_HI:
256                 be_emit_cstring("%hi($");
257                 be_emit_ident(get_entity_ld_ident(attr->entity));
258                 if (attr->val != 0) {
259                         be_emit_irprintf("%+d", attr->val);
260                 }
261                 be_emit_char(')');
262                 break;
263         default:
264                 panic("invalid immediate type found");
265         }
266 }
267
268 /**
269  * Emit the name of the destination register at given output position.
270  */
271 void mips_emit_source_register_or_immediate(const ir_node *node, int pos)
272 {
273         const ir_node *op = get_irn_n(node, pos);
274         if (is_mips_Immediate(op)) {
275                 mips_emit_immediate(op);
276         } else {
277                 mips_emit_source_register(node, pos);
278         }
279 }
280
281 #if 0
282 /*
283  * Add a number to a prefix. This number will not be used a second time.
284  */
285 static char *get_unique_label(char *buf, size_t buflen, const char *prefix)
286 {
287         static unsigned long id = 0;
288         snprintf(buf, buflen, "%s%lu", prefix, ++id);
289         return buf;
290 }
291 #endif
292
293 /************************************************************************/
294 /* ABI Handling                                                         */
295 /************************************************************************/
296
297 static void mips_emit_IncSP(const ir_node *node)
298 {
299         int   offset = be_get_IncSP_offset(node);
300
301         if (offset == 0) {
302                 return;
303         }
304
305         if (offset > 0xffff || offset < -0xffff) {
306                 panic("stackframe > 2^16 bytes not supported yet");
307         }
308
309         if (offset > 0) {
310                 be_emit_irprintf("\tsubu $sp, $sp, %d", offset);
311         } else {
312                 be_emit_irprintf("\taddu $sp, $sp, %d", -offset);
313         }
314         be_emit_finish_line_gas(node);
315 }
316
317 static void mips_emit_Copy(const ir_node *node)
318 {
319         be_emit_cstring("\tmove ");
320         mips_emit_dest_register(node, 0);
321         be_emit_cstring(", ");
322         mips_emit_source_register(node, 0);
323         be_emit_finish_line_gas(node);
324 }
325
326 static void mips_emit_Return(const ir_node* node)
327 {
328         be_emit_cstring("\tj $ra");
329         be_emit_finish_line_gas(node);
330 }
331
332 static __attribute__((unused))
333 void mips_emit_nops(int n)
334 {
335         int i;
336
337         for (i = 0; i < n; ++i) {
338                 be_emit_cstring("\tnop\n");
339                 be_emit_write_line();
340         }
341 }
342
343 static void mips_emit_Perm(const ir_node *node)
344 {
345         assert(get_irn_arity(node) == 2);
346
347         be_emit_cstring("\txor ");
348         mips_emit_source_register(node, 0);
349         be_emit_cstring(", ");
350         mips_emit_source_register(node, 0);
351         be_emit_cstring(", ");
352         mips_emit_source_register(node, 1);
353         be_emit_finish_line_gas(node);
354
355         /* mips_emit_nops(3); */
356
357         be_emit_cstring("\txor ");
358         mips_emit_source_register(node, 1);
359         be_emit_cstring(", ");
360         mips_emit_source_register(node, 1);
361         be_emit_cstring(", ");
362         mips_emit_source_register(node, 0);
363         be_emit_finish_line_gas(node);
364
365         /* mips_emit_nops(3); */
366
367         be_emit_cstring("\txor ");
368         mips_emit_source_register(node, 0);
369         be_emit_cstring(", ");
370         mips_emit_source_register(node, 0);
371         be_emit_cstring(", ");
372         mips_emit_source_register(node, 1);
373         be_emit_finish_line_gas(node);
374
375         /* mips_emit_nops(3); */
376 }
377
378 /************************************************************************/
379 /* Calls                                                                */
380 /************************************************************************/
381
382 static void mips_emit_Call(const ir_node *node)
383 {
384         ir_entity *callee;
385
386         be_emit_cstring("\tjal ");
387
388         /* call of immediate value (label) */
389         callee = be_Call_get_entity(node);
390         if (callee != NULL) {
391                 be_emit_ident(get_entity_ld_ident(callee));
392         } else {
393                 mips_emit_source_register(node, be_pos_Call_ptr);
394         }
395         be_emit_finish_line_gas(node);
396 }
397
398 /************************************************************************
399  *      _
400  *     | |_   _ _ __ ___  _ __  ___
401  *  _  | | | | | '_ ` _ \| '_ \/ __|
402  * | |_| | |_| | | | | | | |_) \__ \
403  *  \___/ \__,_|_| |_| |_| .__/|___/
404  *                       |_|
405  ************************************************************************/
406
407 static void mips_emit_Jump(const ir_node *node)
408 {
409         const ir_node *block = get_irn_link(node);
410         assert(is_Block(block));
411
412         be_emit_cstring("\tb ");
413         be_gas_emit_block_name(block);
414         be_emit_finish_line_gas(node);
415 }
416
417 ir_node *mips_get_jump_block(const ir_node* node, long projn)
418 {
419         const ir_edge_t *oute;
420         for (oute = get_irn_out_edge_first(node); oute != NULL;
421             oute = get_irn_out_edge_next(node, oute)) {
422                 ir_node *proj = get_edge_src_irn(oute);
423                 long n;
424                 assert(is_Proj(proj));
425
426                 n = get_Proj_proj(proj);
427                 if (n == projn)
428                         return get_irn_link(proj);
429         }
430
431         return NULL;
432 }
433
434 void mips_emit_jump_target_proj(const ir_node *node, long projn)
435 {
436         ir_node *jumpblock = mips_get_jump_block(node, projn);
437         assert(jumpblock != NULL);
438
439         be_gas_emit_block_name(jumpblock);
440 }
441
442 void mips_emit_jump_target(const ir_node *node)
443 {
444         ir_node *jumpblock = get_irn_link(node);
445         assert(jumpblock != NULL);
446
447         be_gas_emit_block_name(jumpblock);
448 }
449
450 void mips_emit_jump_or_fallthrough(const ir_node *node, long pn)
451 {
452         ir_node *jumpblock = mips_get_jump_block(node, pn);
453         assert(jumpblock != NULL);
454
455         /* TODO: use fallthrough when possible */
456         be_emit_cstring("b ");
457         be_gas_emit_block_name(jumpblock);
458 }
459
460 /************************************************************************
461  *  ____          _ _       _         _                                 *
462  * / ___|_      _(_) |_ ___| |__     | |_   _ _ __ ___  _ __            *
463  * \___ \ \ /\ / / | __/ __| '_ \ _  | | | | | '_ ` _ \| '_ \           *
464  *  ___) \ V  V /| | || (__| | | | |_| | |_| | | | | | | |_) |          *
465  * |____/ \_/\_/ |_|\__\___|_| |_|\___/ \__,_|_| |_| |_| .__/           *
466  *                                                     |_|              *
467  *                                                                      *
468  ************************************************************************/
469
470 #if 0
471 /* jump table entry (target and corresponding number) */
472 typedef struct _branch_t {
473         ir_node *target;
474         int      value;
475 } branch_t;
476
477 /* jump table for switch generation */
478 typedef struct _jmp_tbl_t {
479         ir_node  *defBlock;        /**< default target */
480         int       min_value;       /**< smallest switch case */
481         int       max_value;       /**< largest switch case */
482         int       num_branches;    /**< number of jumps */
483         char     *label;           /**< label of the jump table */
484         branch_t *branches;        /**< jump array */
485 } jmp_tbl_t;
486
487 /**
488  * Compare two variables of type branch_t. Used to sort all switch cases
489  */
490 static int mips_cmp_branch_t(const void *a, const void *b)
491 {
492         branch_t *b1 = (branch_t *)a;
493         branch_t *b2 = (branch_t *)b;
494
495         if (b1->value <= b2->value)
496                 return -1;
497         else
498                 return 1;
499 }
500
501 const char* mips_get_jumptbl_label(const ir_node* switchjmp)
502 {
503         static char buf[64];
504         snprintf(buf, sizeof(buf), "__JUMPTBL%ld", get_irn_node_nr(switchjmp));
505
506         return buf;
507 }
508
509 /**
510  * Emits code for a SwitchJmp (creates a jump table if
511  * possible otherwise a cmp-jmp cascade). Stolen from ia32
512  */
513 void emit_mips_jump_table(const ir_node *irn)
514 {
515         int                lastval, i, i2, pn;
516         jmp_tbl_t          tbl;
517         ir_node           *proj;
518         const ir_edge_t   *edge;
519         const mips_attr_t *attr = get_mips_attr_const(irn);
520
521         /* fill the table structure */
522         tbl.label        = XMALLOCN(char, SNPRINTF_BUF_LEN);
523         tbl.label        = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
524         tbl.defBlock     = NULL;
525         tbl.num_branches = get_irn_n_edges(irn);
526         tbl.branches     = XMALLOCNZ(branch_t, tbl.num_branches);
527         tbl.min_value    = INT_MAX;
528         tbl.max_value    = INT_MIN;
529
530         i = 0;
531         /* go over all proj's and collect them */
532         foreach_out_edge(irn, edge) {
533                 proj = get_edge_src_irn(edge);
534                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
535
536                 pn = get_Proj_proj(proj);
537
538                 /* create branch entry */
539                 tbl.branches[i].target = get_irn_link(proj);
540                 tbl.branches[i].value  = pn;
541
542                 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
543                 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
544
545                 i++;
546         }
547
548         /* sort the branches by their number */
549         qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), mips_cmp_branch_t);
550
551         be_emit_string(mips_get_jumptbl_label(irn));
552         be_emit_cstring(":\n");
553         be_emit_write_line();
554         lastval = tbl.min_value;
555         for (i = 0; i < tbl.num_branches; ++i) {
556                 const branch_t *branch = &tbl.branches[i];
557                 int value = branch->value;
558
559                 for (i2 = lastval + 1; i2 < value; ++i2) {
560                         be_emit_cstring("\t.word ");
561                         be_emit_ident(get_entity_ld_ident(attr->symconst));
562                         be_emit_char('\n');
563                         be_emit_write_line();
564                 }
565
566                 be_emit_cstring("\t.word ");
567                 be_gas_emit_block_name(branch->target);
568                 be_emit_char('\n');
569                 be_emit_write_line();
570
571                 lastval = branch->value;
572         }
573
574         if (tbl.label)
575                 free(tbl.label);
576         if (tbl.branches)
577                 free(tbl.branches);
578 }
579
580 static void dump_jump_tables(ir_node* node, void *data)
581 {
582         (void) data;
583
584         // emit jump tables
585         if (is_mips_SwitchJump(node)) {
586                 be_emit_cstring(".data\n");
587                 be_emit_write_line();
588
589                 emit_mips_jump_table(node);
590
591                 be_emit_cstring(".text\n");
592                 be_emit_write_line();
593         }
594 }
595 #endif
596
597 /***********************************************************************************
598  *                  _          __                                             _
599  *                 (_)        / _|                                           | |
600  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
601  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
602  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
603  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
604  *
605  ***********************************************************************************/
606
607 static void mips_emit_nothing(const ir_node *node)
608 {
609         (void) node;
610 }
611
612 static void mips_emit_this_shouldnt_happen(const ir_node *node)
613 {
614         panic("Found non-lowered node %+F while emitting", node);
615 }
616
617 /**
618  * The type of a emitter function.
619  */
620 typedef void (*emit_func) (const ir_node *);
621
622 /**
623  * Set a node emitter. Make it a bit more type safe.
624  */
625 static void register_emitter(ir_op *op, emit_func func)
626 {
627         op->ops.generic = (op_func) func;
628 }
629
630 /**
631  * Register emitter functions for mips backend
632  */
633 void mips_register_emitters(void)
634 {
635         /* first clear the generic function pointer for all ops */
636         clear_irp_opcodes_generic_func();
637
638         /* register all emitter functions defined in spec */
639         mips_register_spec_emitters();
640
641         /* benode emitter */
642         register_emitter(op_be_IncSP, mips_emit_IncSP);
643         register_emitter(op_be_AddSP, mips_emit_this_shouldnt_happen);
644         register_emitter(op_be_Call, mips_emit_Call);
645         register_emitter(op_be_Copy, mips_emit_Copy);
646         register_emitter(op_be_Keep, mips_emit_nothing);
647         register_emitter(op_be_Barrier, mips_emit_nothing);
648         register_emitter(op_be_Return, mips_emit_Return);
649         register_emitter(op_be_Start, mips_emit_nothing);
650         register_emitter(op_be_Spill, mips_emit_this_shouldnt_happen);
651         register_emitter(op_be_Reload, mips_emit_this_shouldnt_happen);
652         register_emitter(op_be_Perm, mips_emit_Perm);
653
654         register_emitter(op_Proj, mips_emit_nothing);
655         register_emitter(op_SymConst, mips_emit_this_shouldnt_happen);
656         register_emitter(op_Const, mips_emit_this_shouldnt_happen);
657         register_emitter(op_Jmp, mips_emit_Jump);
658         register_emitter(op_Cmp, mips_emit_this_shouldnt_happen);
659         register_emitter(op_Cond, mips_emit_this_shouldnt_happen);
660         register_emitter(op_Phi, mips_emit_nothing);
661 }
662
663 /**
664  * Emits assembly for a single node
665  */
666 static void mips_emit_node(const ir_node *node)
667 {
668         ir_op *op = get_irn_op(node);
669
670         if (op->ops.generic) {
671                 emit_func emit = (emit_func) op->ops.generic;
672                 (*emit) (node);
673         } else {
674                 panic("No emitter defined for node %+F", node);
675         }
676 }
677
678 /**
679  * Walks over the nodes in a block connected by scheduling edges
680  * and emits code for each node.
681  */
682 void mips_gen_block(const ir_node *block)
683 {
684         ir_node *node;
685
686         if (! is_Block(block))
687                 return;
688
689         be_gas_emit_block_name(block);
690         be_emit_cstring(":\n");
691         be_emit_write_line();
692
693         sched_foreach(block, node) {
694                 mips_emit_node(node);
695         }
696
697         be_emit_char('\n');
698         be_emit_write_line();
699 }
700
701 /**
702  * Emits code for function start.
703  */
704 void mips_emit_func_prolog(ir_graph *irg)
705 {
706         ident *irg_ident = get_entity_ld_ident(get_irg_entity(irg));
707
708         // dump jump tables
709         //irg_walk_graph(irg, NULL, dump_jump_tables, env);
710
711         be_emit_write_line();
712         be_gas_emit_switch_section(GAS_SECTION_TEXT);
713
714         be_emit_cstring("\t.balign\t4\n");
715
716         be_emit_cstring("\t.global\t");
717         be_emit_ident(irg_ident);
718         be_emit_char('\n');
719
720         be_emit_cstring("\t.set\tnomips16\n");
721
722         be_emit_cstring("\t.ent\t");
723         be_emit_ident(irg_ident);
724         be_emit_char('\n');
725
726         be_emit_ident(irg_ident);
727         be_emit_cstring(":\n");
728
729         be_emit_cstring("\t.frame\t$fp, 24, $ra\n");
730         be_emit_cstring("\t.mask\t0xc0000000, -4\n");
731         be_emit_cstring("\t.fmask\t0x00000000, 0\n");
732
733         be_emit_write_line();
734 }
735
736 /**
737  * Emits code for function end
738  */
739 void mips_emit_func_epilog(ir_graph *irg)
740 {
741         ident *irg_ident = get_entity_ident(get_irg_entity(irg));
742
743         be_emit_cstring("\t.end\t");
744         be_emit_ident(irg_ident);
745         be_emit_char('\n');
746         be_emit_write_line();
747 }
748
749 /**
750  * Sets labels for control flow nodes (jump target)
751  */
752 void mips_gen_labels(ir_node *block, void *env)
753 {
754         ir_node *pred;
755         int n = get_Block_n_cfgpreds(block);
756         (void) env;
757
758         for (n--; n >= 0; n--) {
759                 pred = get_Block_cfgpred(block, n);
760                 set_irn_link(pred, block);
761         }
762 }
763
764 /**
765  * Main driver
766  */
767 void mips_gen_routine(mips_code_gen_t *mips_cg, ir_graph *irg)
768 {
769         int i, n;
770
771         mips_register_emitters();
772
773         irg_block_walk_graph(irg, mips_gen_labels, NULL, NULL);
774
775         mips_emit_func_prolog(irg);
776
777         n = ARR_LEN(mips_cg->block_schedule);
778         for (i = 0; i < n; ++i) {
779                 ir_node *block = mips_cg->block_schedule[i];
780                 mips_gen_block(block);
781         }
782
783         mips_emit_func_epilog(irg);
784 }
785
786 void mips_init_emitter(void)
787 {
788         FIRM_DBG_REGISTER(dbg, "firm.be.mips.emitter");
789 }