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 assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
91 /* 1st case: irn is not of mode_T, so it has only */
92 /* one OUT register -> good */
93 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
94 /* Proj with the corresponding projnum for the register */
96 if (get_irn_mode(irn) != mode_T) {
97 reg = arch_get_irn_register(arch_env, irn);
99 else if (is_TEMPLATE_irn(irn)) {
100 reg = get_TEMPLATE_out_reg(irn, pos);
103 const ir_edge_t *edge;
105 foreach_out_edge(irn, edge) {
106 proj = get_edge_src_irn(edge);
107 assert(is_Proj(proj) && "non-Proj from mode_T node");
108 if (get_Proj_proj(proj) == pos) {
109 reg = arch_get_irn_register(arch_env, proj);
115 assert(reg && "no out register found");
120 * Returns the number of the in register at position pos.
122 int get_TEMPLATE_reg_nr(ir_node *irn, int pos, int in_out) {
123 const arch_register_t *reg;
126 reg = get_in_reg(irn, pos);
129 reg = get_out_reg(irn, pos);
132 return arch_register_get_index(reg);
136 * Returns the name of the in register at position pos.
138 const char *get_TEMPLATE_reg_name(ir_node *irn, int pos, int in_out) {
139 const arch_register_t *reg;
142 reg = get_in_reg(irn, pos);
145 reg = get_out_reg(irn, pos);
148 return arch_register_get_name(reg);
152 * Get the register name for a node.
154 static int TEMPLATE_get_reg_name(lc_appendable_t *app,
155 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
158 ir_node *X = arg->v_ptr;
159 int nr = occ->width - 1;
162 return lc_arg_append(app, occ, "(null)", 6);
164 if (occ->conversion == 'S') {
165 buf = get_TEMPLATE_reg_name(X, nr, 1);
168 buf = get_TEMPLATE_reg_name(X, nr, 0);
171 lc_appendable_chadd(app, '%');
172 return lc_arg_append(app, occ, buf, strlen(buf));
176 * Returns the tarval or offset of an TEMPLATE node as a string.
178 static int TEMPLATE_const_to_str(lc_appendable_t *app,
179 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
182 ir_node *X = arg->v_ptr;
185 return lc_arg_append(app, occ, "(null)", 6);
187 if (occ->conversion == 'C') {
188 buf = node_const_to_str(X);
191 buf = node_offset_to_str(X);
194 return lc_arg_append(app, occ, buf, strlen(buf));
198 * Determines the SSE suffix depending on the mode.
200 static int TEMPLATE_get_mode_suffix(lc_appendable_t *app,
201 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
203 ir_node *X = arg->v_ptr;
206 return lc_arg_append(app, occ, "(null)", 6);
208 if (get_mode_size_bits(get_irn_mode(X)) == 32)
209 return lc_appendable_chadd(app, 's');
211 return lc_appendable_chadd(app, 'd');
215 * Return the TEMPLATE printf arg environment.
216 * We use the firm environment with some additional handlers.
218 const lc_arg_env_t *TEMPLATE_get_arg_env(void) {
219 static lc_arg_env_t *env = NULL;
221 static const lc_arg_handler_t TEMPLATE_reg_handler = { TEMPLATE_get_arg_type, TEMPLATE_get_reg_name };
222 static const lc_arg_handler_t TEMPLATE_const_handler = { TEMPLATE_get_arg_type, TEMPLATE_const_to_str };
223 static const lc_arg_handler_t TEMPLATE_mode_handler = { TEMPLATE_get_arg_type, TEMPLATE_get_mode_suffix };
226 /* extend the firm printer */
227 env = firm_get_arg_env();
230 lc_arg_register(env, "TEMPLATE:sreg", 'S', &TEMPLATE_reg_handler);
231 lc_arg_register(env, "TEMPLATE:dreg", 'D', &TEMPLATE_reg_handler);
232 lc_arg_register(env, "TEMPLATE:cnst", 'C', &TEMPLATE_const_handler);
233 lc_arg_register(env, "TEMPLATE:offs", 'O', &TEMPLATE_const_handler);
234 lc_arg_register(env, "TEMPLATE:mode", 'M', &TEMPLATE_mode_handler);
241 * Add a number to a prefix. This number will not be used a second time.
243 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
244 static unsigned long id = 0;
245 snprintf(buf, buflen, "%s%lu", prefix, ++id);
251 * Returns the target label for a control flow node.
253 static char *get_cfop_target(const ir_node *irn, char *buf) {
254 ir_node *bl = get_irn_link(irn);
256 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
262 /***********************************************************************************
265 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
266 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
267 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
268 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
270 ***********************************************************************************/
273 * Emits code for a node.
275 void TEMPLATE_emit_node(ir_node *irn, void *env) {
276 emit_env_t *emit_env = env;
277 firm_dbg_module_t *mod = emit_env->mod;
278 FILE *F = emit_env->out;
280 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
282 #define BE_EMIT(a) if (is_TEMPLATE_##a(irn)) { emit_TEMPLATE_##a(irn, emit_env); return; }
316 /* generated floating point emitter */
332 ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
336 * Walks over the nodes in a block connected by scheduling edges
337 * and emits code for each node.
339 void TEMPLATE_gen_block(ir_node *block, void *env) {
342 if (! is_Block(block))
345 fprintf(((emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
346 sched_foreach(block, irn) {
347 TEMPLATE_emit_node(irn, env);
353 * Emits code for function start.
355 void TEMPLATE_emit_start(FILE *F, ir_graph *irg) {
356 const char *irg_name = get_entity_name(get_irg_entity(irg));
358 /* TODO: emit function header */
362 * Emits code for function end
364 void TEMPLATE_emit_end(FILE *F, ir_graph *irg) {
365 const char *irg_name = get_entity_name(get_irg_entity(irg));
367 /* TODO: emit function end */
371 * Sets labels for control flow nodes (jump target)
372 * TODO: Jump optimization
374 void TEMPLATE_gen_labels(ir_node *block, void *env) {
376 int n = get_Block_n_cfgpreds(block);
378 for (n--; n >= 0; n--) {
379 pred = get_Block_cfgpred(block, n);
380 set_irn_link(pred, block);
387 void TEMPLATE_gen_routine(FILE *F, ir_graph *irg, const TEMPLATE_code_gen_t *cg) {
390 emit_env.mod = firm_dbg_register("firm.be.TEMPLATE.emit");
392 emit_env.arch_env = cg->arch_env;
395 /* set the global arch_env (needed by print hooks) */
396 arch_env = cg->arch_env;
398 TEMPLATE_emit_start(F, irg);
399 irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, &emit_env);
400 irg_walk_blkwise_graph(irg, NULL, TEMPLATE_gen_block, &emit_env);
401 TEMPLATE_emit_end(F, irg);