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