20 #include "../besched.h"
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"
28 #define SNPRINTF_BUF_LEN 128
30 static const arch_env_t *arch_env = NULL;
33 /*************************************************************
35 * (_) | | / _| | | | |
36 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
37 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
38 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
39 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
42 *************************************************************/
45 * Return a const or symconst as string.
47 static const char *node_const_to_str(ir_node *n) {
52 * Returns node's offset as string.
54 static char *node_offset_to_str(ir_node *n) {
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;
65 * Returns the register at in position pos.
67 static const arch_register_t *get_in_reg(ir_node *irn, int pos) {
69 const arch_register_t *reg = NULL;
71 assert(get_irn_arity(irn) > pos && "Invalid IN position");
73 /* The out register of the operator at position pos is the
74 in register we need. */
75 op = get_irn_n(irn, pos);
77 reg = arch_get_irn_register(arch_env, op);
79 assert(reg && "no in register found");
84 * Returns the register at out position pos.
86 static const arch_register_t *get_out_reg(ir_node *irn, int pos) {
88 const arch_register_t *reg = NULL;
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 */
95 if (get_irn_mode(irn) != mode_T) {
96 reg = arch_get_irn_register(arch_env, irn);
98 else if (is_TEMPLATE_irn(irn)) {
99 reg = get_TEMPLATE_out_reg(irn, pos);
102 const ir_edge_t *edge;
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);
114 assert(reg && "no out register found");
119 * Returns the number of the in register at position pos.
121 int get_TEMPLATE_reg_nr(ir_node *irn, int pos, int in_out) {
122 const arch_register_t *reg;
125 reg = get_in_reg(irn, pos);
128 reg = get_out_reg(irn, pos);
131 return arch_register_get_index(reg);
135 * Returns the name of the in register at position pos.
137 const char *get_TEMPLATE_reg_name(ir_node *irn, int pos, int in_out) {
138 const arch_register_t *reg;
141 reg = get_in_reg(irn, pos);
144 reg = get_out_reg(irn, pos);
147 return arch_register_get_name(reg);
151 * Get the register name for a node.
153 static int TEMPLATE_get_reg_name(lc_appendable_t *app,
154 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
157 ir_node *X = arg->v_ptr;
158 int nr = occ->width - 1;
161 return lc_arg_append(app, occ, "(null)", 6);
163 if (occ->conversion == 'S') {
164 buf = get_TEMPLATE_reg_name(X, nr, 1);
167 buf = get_TEMPLATE_reg_name(X, nr, 0);
170 return buf ? lc_arg_append(app, occ, buf, strlen(buf)) : 0;
174 * Returns the tarval or offset of an TEMPLATE node as a string.
176 static int TEMPLATE_const_to_str(lc_appendable_t *app,
177 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
180 ir_node *X = arg->v_ptr;
183 return lc_arg_append(app, occ, "(null)", 6);
185 if (occ->conversion == 'C') {
186 buf = node_const_to_str(X);
189 buf = node_offset_to_str(X);
192 return lc_arg_append(app, occ, buf, strlen(buf));
196 * Determines the SSE suffix depending on the mode.
198 static int TEMPLATE_get_mode_suffix(lc_appendable_t *app,
199 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
201 ir_node *X = arg->v_ptr;
204 return lc_arg_append(app, occ, "(null)", 6);
206 if (get_mode_size_bits(get_irn_mode(X)) == 32)
207 return lc_appendable_chadd(app, 's');
209 return lc_appendable_chadd(app, 'd');
213 * Return the TEMPLATE printf arg environment.
214 * We use the firm environment with some additional handlers.
216 const lc_arg_env_t *TEMPLATE_get_arg_env(void) {
217 static lc_arg_env_t *env = NULL;
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 };
224 /* extend the firm printer */
225 env = firm_get_arg_env();
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);
239 * Add a number to a prefix. This number will not be used a second time.
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);
249 * Returns the target label for a control flow node.
251 static char *get_cfop_target(const ir_node *irn, char *buf) {
252 ir_node *bl = get_irn_link(irn);
254 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
260 /***********************************************************************************
263 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
264 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
265 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
266 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
268 ***********************************************************************************/
271 * Enters the emitter functions for handled nodes into the generic
272 * pointer of an opcode.
274 static void TEMPLATE_register_emitters(void) {
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
282 /* first clear the generic function pointer for all ops */
283 clear_irp_opcodes_generic_func();
285 /* register all emitter functions defined in spec */
286 TEMPLATE_register_spec_emitters();
288 /* register addtional emitter functions if needed */
297 * Emits code for a node.
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;)
305 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
307 if (op->ops.generic) {
308 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
312 ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
317 * Walks over the nodes in a block connected by scheduling edges
318 * and emits code for each node.
320 void TEMPLATE_gen_block(ir_node *block, void *env) {
321 TEMPLATE_emit_env_t *emit_env = env;
324 if (! is_Block(block))
327 fprintf(emit_env->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
328 sched_foreach(block, irn) {
329 TEMPLATE_emit_node(irn, env);
335 * Emits code for function start.
337 void TEMPLATE_emit_func_prolog(FILE *F, ir_graph *irg) {
338 const char *irg_name = get_entity_name(get_irg_entity(irg));
340 /* TODO: emit function header */
344 * Emits code for function end
346 void TEMPLATE_emit_func_epilog(FILE *F, ir_graph *irg) {
347 const char *irg_name = get_entity_name(get_irg_entity(irg));
349 /* TODO: emit function end */
353 * Sets labels for control flow nodes (jump target)
354 * TODO: Jump optimization
356 void TEMPLATE_gen_labels(ir_node *block, void *env) {
358 int n = get_Block_n_cfgpreds(block);
360 for (n--; n >= 0; n--) {
361 pred = get_Block_cfgpred(block, n);
362 set_irn_link(pred, block);
369 void TEMPLATE_gen_routine(FILE *F, ir_graph *irg, const TEMPLATE_code_gen_t *cg) {
370 TEMPLATE_emit_env_t emit_env;
373 emit_env.arch_env = cg->arch_env;
375 FIRM_DBG_REGISTER(emit_env.mod, "firm.be.TEMPLATE.emit");
377 /* set the global arch_env (needed by print hooks) */
378 arch_env = cg->arch_env;
380 /* register all emitter functions */
381 TEMPLATE_register_emitters();
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);