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