19 #include "../besched.h"
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"
27 #define SNPRINTF_BUF_LEN 128
29 static const arch_env_t *arch_env = NULL;
32 /*************************************************************
34 * (_) | | / _| | | | |
35 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
36 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
37 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
38 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
41 *************************************************************/
44 * Return a const or symconst as string.
46 static const char *node_const_to_str(ir_node *n) {
51 * Returns node's offset as string.
53 static char *node_offset_to_str(ir_node *n) {
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;
64 * Returns the register at in position pos.
66 static const arch_register_t *get_in_reg(ir_node *irn, int pos) {
68 const arch_register_t *reg = NULL;
70 assert(get_irn_arity(irn) > pos && "Invalid IN position");
72 /* The out register of the operator at position pos is the
73 in register we need. */
74 op = get_irn_n(irn, pos);
76 reg = arch_get_irn_register(arch_env, op);
78 assert(reg && "no in register found");
83 * Returns the register at out position pos.
85 static const arch_register_t *get_out_reg(ir_node *irn, int pos) {
87 const arch_register_t *reg = NULL;
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 */
94 if (get_irn_mode(irn) != mode_T) {
95 reg = arch_get_irn_register(arch_env, irn);
97 else if (is_TEMPLATE_irn(irn)) {
98 reg = get_TEMPLATE_out_reg(irn, pos);
101 const ir_edge_t *edge;
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);
113 assert(reg && "no out register found");
118 * Returns the number of the in register at position pos.
120 int get_TEMPLATE_reg_nr(ir_node *irn, int pos, int in_out) {
121 const arch_register_t *reg;
124 reg = get_in_reg(irn, pos);
127 reg = get_out_reg(irn, pos);
130 return arch_register_get_index(reg);
134 * Returns the name of the in register at position pos.
136 const char *get_TEMPLATE_reg_name(ir_node *irn, int pos, int in_out) {
137 const arch_register_t *reg;
140 reg = get_in_reg(irn, pos);
143 reg = get_out_reg(irn, pos);
146 return arch_register_get_name(reg);
150 * Get the register name for a node.
152 static int TEMPLATE_get_reg_name(lc_appendable_t *app,
153 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
156 ir_node *X = arg->v_ptr;
157 int nr = occ->width - 1;
160 return lc_arg_append(app, occ, "(null)", 6);
162 if (occ->conversion == 'S') {
163 buf = get_TEMPLATE_reg_name(X, nr, 1);
166 buf = get_TEMPLATE_reg_name(X, nr, 0);
169 return buf ? lc_arg_append(app, occ, buf, strlen(buf)) : 0;
173 * Returns the tarval or offset of an TEMPLATE node as a string.
175 static int TEMPLATE_const_to_str(lc_appendable_t *app,
176 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
179 ir_node *X = arg->v_ptr;
182 return lc_arg_append(app, occ, "(null)", 6);
184 if (occ->conversion == 'C') {
185 buf = node_const_to_str(X);
188 buf = node_offset_to_str(X);
191 return lc_arg_append(app, occ, buf, strlen(buf));
195 * Determines the SSE suffix depending on the mode.
197 static int TEMPLATE_get_mode_suffix(lc_appendable_t *app,
198 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
200 ir_node *X = arg->v_ptr;
203 return lc_arg_append(app, occ, "(null)", 6);
205 if (get_mode_size_bits(get_irn_mode(X)) == 32)
206 return lc_appendable_chadd(app, 's');
208 return lc_appendable_chadd(app, 'd');
212 * Return the TEMPLATE printf arg environment.
213 * We use the firm environment with some additional handlers.
215 const lc_arg_env_t *TEMPLATE_get_arg_env(void) {
216 static lc_arg_env_t *env = NULL;
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 };
223 /* extend the firm printer */
224 env = firm_get_arg_env();
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);
238 * Add a number to a prefix. This number will not be used a second time.
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);
248 * Returns the target label for a control flow node.
250 static char *get_cfop_target(const ir_node *irn, char *buf) {
251 ir_node *bl = get_irn_link(irn);
253 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
259 /***********************************************************************************
262 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
263 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
264 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
265 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
267 ***********************************************************************************/
270 * Enters the emitter functions for handled nodes into the generic
271 * pointer of an opcode.
273 static void TEMPLATE_register_emitters(void) {
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
281 /* first clear the generic function pointer for all ops */
282 clear_irp_opcodes_generic_func();
284 /* register all emitter functions defined in spec */
285 TEMPLATE_register_spec_emitters();
287 /* register addtional emitter functions if needed */
296 * Emits code for a node.
298 void TEMPLATE_emit_node(ir_node *irn, void *env) {
299 emit_env_t *emit_env = env;
300 FILE *F = emit_env->out;
301 ir_op *op = get_irn_op(irn);
302 DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
304 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
306 if (op->ops.generic) {
307 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
311 ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
316 * Walks over the nodes in a block connected by scheduling edges
317 * and emits code for each node.
319 void TEMPLATE_gen_block(ir_node *block, void *env) {
322 if (! is_Block(block))
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);
333 * Emits code for function start.
335 void TEMPLATE_emit_func_prolog(FILE *F, ir_graph *irg) {
336 const char *irg_name = get_entity_name(get_irg_entity(irg));
338 /* TODO: emit function header */
342 * Emits code for function end
344 void TEMPLATE_emit_func_epilog(FILE *F, ir_graph *irg) {
345 const char *irg_name = get_entity_name(get_irg_entity(irg));
347 /* TODO: emit function end */
351 * Sets labels for control flow nodes (jump target)
352 * TODO: Jump optimization
354 void TEMPLATE_gen_labels(ir_node *block, void *env) {
356 int n = get_Block_n_cfgpreds(block);
358 for (n--; n >= 0; n--) {
359 pred = get_Block_cfgpred(block, n);
360 set_irn_link(pred, block);
367 void TEMPLATE_gen_routine(FILE *F, ir_graph *irg, const TEMPLATE_code_gen_t *cg) {
371 emit_env.arch_env = cg->arch_env;
373 FIRM_DBG_REGISTER(emit_env.mod, "firm.be.TEMPLATE.emit");
375 /* set the global arch_env (needed by print hooks) */
376 arch_env = cg->arch_env;
378 /* register all emitter functions */
379 TEMPLATE_register_emitters();
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);