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