3fe41fa7c702d505255ed7b7e87b2fe0d67e7344
[libfirm] / ir / be / TEMPLATE / TEMPLATE_emitter.c
1 /* TEMPLATE emitter */
2 /* $Id$ */
3
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7
8 #include <limits.h>
9
10 #include "xmalloc.h"
11 #include "tv.h"
12 #include "iredges.h"
13 #include "debug.h"
14 #include "irgwalk.h"
15 #include "irprintf.h"
16 #include "irop_t.h"
17 #include "irargs_t.h"
18 #include "irprog.h"
19
20 #include "../besched.h"
21
22 #include "TEMPLATE_emitter.h"
23 #include "gen_TEMPLATE_emitter.h"
24 #include "TEMPLATE_nodes_attr.h"
25 #include "TEMPLATE_new_nodes.h"
26 #include "TEMPLATE_map_regs.h"
27
28 #define SNPRINTF_BUF_LEN 128
29
30 static const arch_env_t *arch_env = NULL;
31
32
33 /*************************************************************
34  *             _       _    __   _          _
35  *            (_)     | |  / _| | |        | |
36  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
37  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
38  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
39  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
40  * | |                                       | |
41  * |_|                                       |_|
42  *************************************************************/
43
44 /**
45  * Return a const or symconst as string.
46  */
47 static const char *node_const_to_str(ir_node *n) {
48         /* TODO */
49 }
50
51 /**
52  * Returns node's offset as string.
53  */
54 static char *node_offset_to_str(ir_node *n) {
55         /* TODO */
56 }
57
58 /* We always pass the ir_node which is a pointer. */
59 static int TEMPLATE_get_arg_type(const lc_arg_occ_t *occ) {
60         return lc_arg_type_ptr;
61 }
62
63
64 /**
65  * Returns the register at in position pos.
66  */
67 static const arch_register_t *get_in_reg(ir_node *irn, int pos) {
68         ir_node                *op;
69         const arch_register_t  *reg = NULL;
70
71         assert(get_irn_arity(irn) > pos && "Invalid IN position");
72
73         /* The out register of the operator at position pos is the
74            in register we need. */
75         op = get_irn_n(irn, pos);
76
77         reg = arch_get_irn_register(arch_env, op);
78
79         assert(reg && "no in register found");
80         return reg;
81 }
82
83 /**
84  * Returns the register at out position pos.
85  */
86 static const arch_register_t *get_out_reg(ir_node *irn, int pos) {
87         ir_node                *proj;
88         const arch_register_t  *reg = NULL;
89
90         /* 1st case: irn is not of mode_T, so it has only                 */
91         /*           one OUT register -> good                             */
92         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
93         /*           Proj with the corresponding projnum for the register */
94
95         if (get_irn_mode(irn) != mode_T) {
96                 reg = arch_get_irn_register(arch_env, irn);
97         }
98         else if (is_TEMPLATE_irn(irn)) {
99                 reg = get_TEMPLATE_out_reg(irn, pos);
100         }
101         else {
102                 const ir_edge_t *edge;
103
104                 foreach_out_edge(irn, edge) {
105                         proj = get_edge_src_irn(edge);
106                         assert(is_Proj(proj) && "non-Proj from mode_T node");
107                         if (get_Proj_proj(proj) == pos) {
108                                 reg = arch_get_irn_register(arch_env, proj);
109                                 break;
110                         }
111                 }
112         }
113
114         assert(reg && "no out register found");
115         return reg;
116 }
117
118 /**
119  * Returns the number of the in register at position pos.
120  */
121 int get_TEMPLATE_reg_nr(ir_node *irn, int pos, int in_out) {
122         const arch_register_t *reg;
123
124         if (in_out == 1) {
125                 reg = get_in_reg(irn, pos);
126         }
127         else {
128                 reg = get_out_reg(irn, pos);
129         }
130
131         return arch_register_get_index(reg);
132 }
133
134 /**
135  * Returns the name of the in register at position pos.
136  */
137 const char *get_TEMPLATE_reg_name(ir_node *irn, int pos, int in_out) {
138         const arch_register_t *reg;
139
140         if (in_out == 1) {
141                 reg = get_in_reg(irn, pos);
142         }
143         else {
144                 reg = get_out_reg(irn, pos);
145         }
146
147         return arch_register_get_name(reg);
148 }
149
150 /**
151  * Get the register name for a node.
152  */
153 static int TEMPLATE_get_reg_name(lc_appendable_t *app,
154     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
155 {
156         const char *buf;
157         ir_node    *X  = arg->v_ptr;
158         int         nr = occ->width - 1;
159
160         if (!X)
161                 return lc_arg_append(app, occ, "(null)", 6);
162
163         if (occ->conversion == 'S') {
164                 buf = get_TEMPLATE_reg_name(X, nr, 1);
165         }
166         else { /* 'D' */
167                 buf = get_TEMPLATE_reg_name(X, nr, 0);
168         }
169
170         return buf ? lc_arg_append(app, occ, buf, strlen(buf)) : 0;
171 }
172
173 /**
174  * Returns the tarval or offset of an TEMPLATE node as a string.
175  */
176 static int TEMPLATE_const_to_str(lc_appendable_t *app,
177     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
178 {
179         const char *buf;
180         ir_node    *X = arg->v_ptr;
181
182         if (!X)
183                 return lc_arg_append(app, occ, "(null)", 6);
184
185         if (occ->conversion == 'C') {
186                 buf = node_const_to_str(X);
187         }
188         else { /* 'O' */
189                 buf = node_offset_to_str(X);
190         }
191
192         return lc_arg_append(app, occ, buf, strlen(buf));
193 }
194
195 /**
196  * Determines the SSE suffix depending on the mode.
197  */
198 static int TEMPLATE_get_mode_suffix(lc_appendable_t *app,
199     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
200 {
201         ir_node *X = arg->v_ptr;
202
203         if (!X)
204                 return lc_arg_append(app, occ, "(null)", 6);
205
206         if (get_mode_size_bits(get_irn_mode(X)) == 32)
207                 return lc_appendable_chadd(app, 's');
208         else
209                 return lc_appendable_chadd(app, 'd');
210 }
211
212 /**
213  * Return the TEMPLATE printf arg environment.
214  * We use the firm environment with some additional handlers.
215  */
216 const lc_arg_env_t *TEMPLATE_get_arg_env(void) {
217         static lc_arg_env_t *env = NULL;
218
219         static const lc_arg_handler_t TEMPLATE_reg_handler   = { TEMPLATE_get_arg_type, TEMPLATE_get_reg_name };
220         static const lc_arg_handler_t TEMPLATE_const_handler = { TEMPLATE_get_arg_type, TEMPLATE_const_to_str };
221         static const lc_arg_handler_t TEMPLATE_mode_handler  = { TEMPLATE_get_arg_type, TEMPLATE_get_mode_suffix };
222
223         if(env == NULL) {
224                 /* extend the firm printer */
225                 env = firm_get_arg_env();
226                         //lc_arg_new_env();
227
228                 lc_arg_register(env, "TEMPLATE:sreg", 'S', &TEMPLATE_reg_handler);
229                 lc_arg_register(env, "TEMPLATE:dreg", 'D', &TEMPLATE_reg_handler);
230                 lc_arg_register(env, "TEMPLATE:cnst", 'C', &TEMPLATE_const_handler);
231                 lc_arg_register(env, "TEMPLATE:offs", 'O', &TEMPLATE_const_handler);
232                 lc_arg_register(env, "TEMPLATE:mode", 'M', &TEMPLATE_mode_handler);
233         }
234
235         return env;
236 }
237
238 /*
239  * Add a number to a prefix. This number will not be used a second time.
240  */
241 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
242         static unsigned long id = 0;
243         snprintf(buf, buflen, "%s%lu", prefix, ++id);
244         return buf;
245 }
246
247
248 /**
249  * Returns the target label for a control flow node.
250  */
251 static char *get_cfop_target(const ir_node *irn, char *buf) {
252         ir_node *bl = get_irn_link(irn);
253
254         snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
255         return buf;
256 }
257
258
259
260 /***********************************************************************************
261  *                  _          __                                             _
262  *                 (_)        / _|                                           | |
263  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
264  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
265  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
266  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
267  *
268  ***********************************************************************************/
269
270 /**
271  * Enters the emitter functions for handled nodes into the generic
272  * pointer of an opcode.
273  */
274 static void TEMPLATE_register_emitters(void) {
275
276 /* some convienience macros to register additional emitter functions
277    (other than the generated ones) */
278 #define TEMPLATE_EMIT(a) op_TEMPLATE_##a->ops.generic = (op_func)emit_TEMPLATE_##a
279 #define EMIT(a)          op_##a->ops.generic = (op_func)emit_##a
280 #define BE_EMIT(a)       op_be_##a->ops.generic = (op_func)emit_be_##a
281
282         /* first clear the generic function pointer for all ops */
283         clear_irp_opcodes_generic_func();
284
285         /* register all emitter functions defined in spec */
286         TEMPLATE_register_spec_emitters();
287
288         /* register addtional emitter functions if needed */
289
290 #undef TEMPLATE_EMIT
291 #undef BE_EMIT
292 #undef EMIT
293 }
294
295
296 /**
297  * Emits code for a node.
298  */
299 void TEMPLATE_emit_node(ir_node *irn, void *env) {
300         TEMPLATE_emit_env_t *emit_env = env;
301         FILE                *F        = emit_env->out;
302         ir_op               *op       = get_irn_op(irn);
303         DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
304
305         DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
306
307         if (op->ops.generic) {
308                 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
309                 (*emit)(irn, env);
310         }
311         else {
312                 ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
313         }
314 }
315
316 /**
317  * Walks over the nodes in a block connected by scheduling edges
318  * and emits code for each node.
319  */
320 void TEMPLATE_gen_block(ir_node *block, void *env) {
321         TEMPLATE_emit_env_t *emit_env = env;
322         ir_node *irn;
323
324         if (! is_Block(block))
325                 return;
326
327         fprintf(emit_env->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
328         sched_foreach(block, irn) {
329                 TEMPLATE_emit_node(irn, env);
330         }
331 }
332
333
334 /**
335  * Emits code for function start.
336  */
337 void TEMPLATE_emit_func_prolog(FILE *F, ir_graph *irg) {
338         const char *irg_name = get_entity_name(get_irg_entity(irg));
339
340         /* TODO: emit function header */
341 }
342
343 /**
344  * Emits code for function end
345  */
346 void TEMPLATE_emit_func_epilog(FILE *F, ir_graph *irg) {
347         const char *irg_name = get_entity_name(get_irg_entity(irg));
348
349         /* TODO: emit function end */
350 }
351
352 /**
353  * Sets labels for control flow nodes (jump target)
354  * TODO: Jump optimization
355  */
356 void TEMPLATE_gen_labels(ir_node *block, void *env) {
357         ir_node *pred;
358         int n = get_Block_n_cfgpreds(block);
359
360         for (n--; n >= 0; n--) {
361                 pred = get_Block_cfgpred(block, n);
362                 set_irn_link(pred, block);
363         }
364 }
365
366 /**
367  * Main driver
368  */
369 void TEMPLATE_gen_routine(FILE *F, ir_graph *irg, const TEMPLATE_code_gen_t *cg) {
370         TEMPLATE_emit_env_t emit_env;
371
372         emit_env.out      = F;
373         emit_env.arch_env = cg->arch_env;
374         emit_env.cg       = cg;
375         FIRM_DBG_REGISTER(emit_env.mod, "firm.be.TEMPLATE.emit");
376
377         /* set the global arch_env (needed by print hooks) */
378         arch_env = cg->arch_env;
379
380         /* register all emitter functions */
381         TEMPLATE_register_emitters();
382
383         TEMPLATE_emit_func_prolog(F, irg);
384         irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, &emit_env);
385         irg_walk_blkwise_graph(irg, NULL, TEMPLATE_gen_block, &emit_env);
386         TEMPLATE_emit_func_epilog(F, irg);
387 }