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