Remove the unused parameter const arch_env_t *env from arch_get_irn_register().
[libfirm] / ir / be / TEMPLATE / TEMPLATE_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   emit assembler for a backend graph
23  * @version $Id$
24  */
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <limits.h>
30
31 #include "xmalloc.h"
32 #include "tv.h"
33 #include "iredges.h"
34 #include "debug.h"
35 #include "irgwalk.h"
36 #include "irprintf.h"
37 #include "irop_t.h"
38 #include "irargs_t.h"
39 #include "irprog.h"
40
41 #include "../besched.h"
42
43 #include "TEMPLATE_emitter.h"
44 #include "gen_TEMPLATE_emitter.h"
45 #include "TEMPLATE_nodes_attr.h"
46 #include "TEMPLATE_new_nodes.h"
47 #include "TEMPLATE_map_regs.h"
48
49 #define SNPRINTF_BUF_LEN 128
50
51 /**
52  * Returns the register at in position pos.
53  */
54 static const arch_register_t *get_in_reg(const ir_node *node, int pos)
55 {
56         ir_node                *op;
57         const arch_register_t  *reg = NULL;
58
59         assert(get_irn_arity(node) > pos && "Invalid IN position");
60
61         /* The out register of the operator at position pos is the
62            in register we need. */
63         op = get_irn_n(node, pos);
64
65         reg = arch_get_irn_register(op);
66
67         assert(reg && "no in register found");
68         return reg;
69 }
70
71 /**
72  * Returns the register at out position pos.
73  */
74 static const arch_register_t *get_out_reg(const ir_node *node, int pos)
75 {
76         ir_node                *proj;
77         const arch_register_t  *reg = NULL;
78
79         /* 1st case: irn is not of mode_T, so it has only                 */
80         /*           one OUT register -> good                             */
81         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
82         /*           Proj with the corresponding projnum for the register */
83
84         if (get_irn_mode(node) != mode_T) {
85                 reg = arch_get_irn_register(node);
86         } else if (is_TEMPLATE_irn(node)) {
87                 reg = get_TEMPLATE_out_reg(node, pos);
88         } else {
89                 const ir_edge_t *edge;
90
91                 foreach_out_edge(node, edge) {
92                         proj = get_edge_src_irn(edge);
93                         assert(is_Proj(proj) && "non-Proj from mode_T node");
94                         if (get_Proj_proj(proj) == pos) {
95                                 reg = arch_get_irn_register(proj);
96                                 break;
97                         }
98                 }
99         }
100
101         assert(reg && "no out register found");
102         return reg;
103 }
104
105 /*************************************************************
106  *             _       _    __   _          _
107  *            (_)     | |  / _| | |        | |
108  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
109  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
110  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
111  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
112  * | |                                       | |
113  * |_|                                       |_|
114  *************************************************************/
115
116 void TEMPLATE_emit_immediate(const ir_node *node)
117 {
118         (void) node;
119         /* TODO */
120 }
121
122 void TEMPLATE_emit_source_register(const ir_node *node, int pos)
123 {
124         const arch_register_t *reg = get_in_reg(node, pos);
125         be_emit_string(arch_register_get_name(reg));
126 }
127
128 void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
129 {
130         const arch_register_t *reg = get_out_reg(node, pos);
131         be_emit_string(arch_register_get_name(reg));
132 }
133
134 /**
135  * Returns the target label for a control flow node.
136  */
137 static void TEMPLATE_emit_cfop_target(const ir_node *node) {
138         ir_node *block = get_irn_link(node);
139
140         be_emit_irprintf("BLOCK_%ld", get_irn_node_nr(block));
141 }
142
143 /***********************************************************************************
144  *                  _          __                                             _
145  *                 (_)        / _|                                           | |
146  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
147  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
148  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
149  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
150  *
151  ***********************************************************************************/
152
153 /**
154  * Emits code for a unconditional jump.
155  */
156 static void emit_Jmp(const ir_node *node) {
157         ir_node *block;
158
159         /* for now, the code works for scheduled and non-schedules blocks */
160         block = get_nodes_block(node);
161
162         be_emit_cstring("\tjmp ");
163         TEMPLATE_emit_cfop_target(node);
164         be_emit_finish_line_gas(node);
165 }
166
167 /**
168  * Enters the emitter functions for handled nodes into the generic
169  * pointer of an opcode.
170  */
171 static void TEMPLATE_register_emitters(void) {
172
173 /* some convienience macros to register additional emitter functions
174    (other than the generated ones) */
175 #define TEMPLATE_EMIT(a) op_TEMPLATE_##a->ops.generic = (op_func)emit_TEMPLATE_##a
176 #define EMIT(a)          op_##a->ops.generic = (op_func)emit_##a
177 #define BE_EMIT(a)       op_be_##a->ops.generic = (op_func)emit_be_##a
178
179         /* first clear the generic function pointer for all ops */
180         clear_irp_opcodes_generic_func();
181
182         /* register all emitter functions defined in spec */
183         TEMPLATE_register_spec_emitters();
184
185         /* register addtional emitter functions if needed */
186         EMIT(Jmp);
187
188 #undef TEMPLATE_EMIT
189 #undef BE_EMIT
190 #undef EMIT
191 }
192
193 typedef void (*emit_func_ptr) (const ir_node *);
194
195 /**
196  * Emits code for a node.
197  */
198 void TEMPLATE_emit_node(const ir_node *node) {
199         ir_op               *op       = get_irn_op(node);
200
201         if (op->ops.generic) {
202                 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
203                 (*func) (node);
204         } else {
205                 ir_fprintf(stderr, "No emitter for node %+F\n", node);
206         }
207 }
208
209 /**
210  * Walks over the nodes in a block connected by scheduling edges
211  * and emits code for each node.
212  */
213 void TEMPLATE_gen_block(ir_node *block, void *data) {
214         ir_node *node;
215         (void) data;
216
217         if (! is_Block(block))
218                 return;
219
220         be_emit_cstring("BLOCK_");
221         be_emit_irprintf("%ld:\n", get_irn_node_nr(block));
222         be_emit_write_line();
223
224         sched_foreach(block, node) {
225                 TEMPLATE_emit_node(node);
226         }
227 }
228
229
230 /**
231  * Emits code for function start.
232  */
233 void TEMPLATE_emit_func_prolog(ir_graph *irg) {
234         const char *irg_name = get_entity_name(get_irg_entity(irg));
235
236         /* TODO: emit function header */
237         be_emit_cstring("/* start of ");
238         be_emit_string(irg_name);
239         be_emit_cstring(" */\n");
240         be_emit_write_line();
241 }
242
243 /**
244  * Emits code for function end
245  */
246 void TEMPLATE_emit_func_epilog(ir_graph *irg) {
247         const char *irg_name = get_entity_name(get_irg_entity(irg));
248
249         /* TODO: emit function end */
250         be_emit_cstring("/* end of ");
251         be_emit_string(irg_name);
252         be_emit_cstring(" */\n");
253         be_emit_write_line();
254 }
255
256 /**
257  * Sets labels for control flow nodes (jump target)
258  * TODO: Jump optimization
259  */
260 void TEMPLATE_gen_labels(ir_node *block, void *env) {
261         ir_node *pred;
262         int n = get_Block_n_cfgpreds(block);
263         (void) env;
264
265         for (n--; n >= 0; n--) {
266                 pred = get_Block_cfgpred(block, n);
267                 set_irn_link(pred, block);
268         }
269 }
270
271 /**
272  * Main driver
273  */
274 void TEMPLATE_gen_routine(const TEMPLATE_code_gen_t *cg, ir_graph *irg)
275 {
276         (void)cg;
277
278         /* register all emitter functions */
279         TEMPLATE_register_emitters();
280
281         TEMPLATE_emit_func_prolog(irg);
282         irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, NULL);
283         irg_walk_blkwise_graph(irg, NULL, TEMPLATE_gen_block, NULL);
284         TEMPLATE_emit_func_epilog(irg);
285 }