+/* TEMPLATE emitter */
+/* $Id$ */
+
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
ir_node *proj;
const arch_register_t *reg = NULL;
- assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
-
/* 1st case: irn is not of mode_T, so it has only */
/* one OUT register -> good */
/* 2nd case: irn is of mode_T -> collect all Projs and ask the */
buf = get_TEMPLATE_reg_name(X, nr, 0);
}
- lc_appendable_chadd(app, '%');
- return lc_arg_append(app, occ, buf, strlen(buf));
+ return buf ? lc_arg_append(app, occ, buf, strlen(buf)) : 0;
}
/**
- * Returns the tarval or offset of an ia32 as a string.
+ * Returns the tarval or offset of an TEMPLATE node as a string.
*/
static int TEMPLATE_const_to_str(lc_appendable_t *app,
const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
}
/**
- * Return the ia32 printf arg environment.
+ * Return the TEMPLATE printf arg environment.
* We use the firm environment with some additional handlers.
*/
const lc_arg_env_t *TEMPLATE_get_arg_env(void) {
static lc_arg_env_t *env = NULL;
- static const lc_arg_handler_t ia32_reg_handler = { ia32_get_arg_type, ia32_get_reg_name };
- static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
- static const lc_arg_handler_t ia32_mode_handler = { ia32_get_arg_type, ia32_get_mode_suffix };
+ static const lc_arg_handler_t TEMPLATE_reg_handler = { TEMPLATE_get_arg_type, TEMPLATE_get_reg_name };
+ static const lc_arg_handler_t TEMPLATE_const_handler = { TEMPLATE_get_arg_type, TEMPLATE_const_to_str };
+ static const lc_arg_handler_t TEMPLATE_mode_handler = { TEMPLATE_get_arg_type, TEMPLATE_get_mode_suffix };
if(env == NULL) {
/* extend the firm printer */
env = firm_get_arg_env();
//lc_arg_new_env();
- lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
- lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
- lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
- lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
- lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
+ lc_arg_register(env, "TEMPLATE:sreg", 'S', &TEMPLATE_reg_handler);
+ lc_arg_register(env, "TEMPLATE:dreg", 'D', &TEMPLATE_reg_handler);
+ lc_arg_register(env, "TEMPLATE:cnst", 'C', &TEMPLATE_const_handler);
+ lc_arg_register(env, "TEMPLATE:offs", 'O', &TEMPLATE_const_handler);
+ lc_arg_register(env, "TEMPLATE:mode", 'M', &TEMPLATE_mode_handler);
}
return env;
}
-/**
- * For 2-address code we need to make sure the first src reg is equal to dest reg.
- */
-void equalize_dest_src(FILE *F, ir_node *n) {
- if (get_ia32_reg_nr(n, 0, 1) != get_ia32_reg_nr(n, 0, 0)) {
- if (get_irn_arity(n) > 1 && get_ia32_reg_nr(n, 1, 1) == get_ia32_reg_nr(n, 0, 0)) {
- if (! is_op_commutative(get_irn_op(n))) {
- /* we only need to exchange for non-commutative ops */
- lc_efprintf(ia32_get_arg_env(), F, "\txchg %1S, %2S\t\t\t/* xchg src1 <-> src2 for 2 address code */\n", n, n);
- }
- }
- else {
- lc_efprintf(ia32_get_arg_env(), F, "\tmovl %1S, %1D\t\t\t/* src -> dest for 2 address code */\n", n, n);
- }
- }
-}
-
/*
* Add a number to a prefix. This number will not be used a second time.
*/
-char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
+static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
static unsigned long id = 0;
snprintf(buf, buflen, "%s%lu", prefix, ++id);
return buf;
return buf;
}
-/*********************************************************
- * _ _ _
- * (_) | (_)
- * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
- * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
- * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
- * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
- * _/ | | |
- * |__/ |_|
- *********************************************************/
-
-/**
- * Emits code for a Switch (creates a jump table if
- * possible otherwise a cmp-jmp cascade).
- */
-void emit_TEMPLATE_Switch(const ir_node *irn, emit_env_t *emit_env) {
- unsigned long interval;
- jmp_tbl_t *tbl;
- ir_node **cases;
- int def_projnum;
- int do_jmp_tbl = 1;
- const lc_arg_env_t *env = ia32_get_arg_env();
- FILE *F = emit_env->out;
-
- /* TODO: */
- /* - create list of projs, each corresponding to one switch case */
- /* - determine the projnumber of the default case */
-
- tbl = create_jump_table(cases, def_projnum, "JMPTBL_");
-
- /* two-complement's magic make this work without overflow */
- interval = tbl.max_value - tbl.min_value;
-
- /* check value interval: do not create jump table if interval is too large */
- if (interval > 16 * 1024) {
- do_jmp_tbl = 0;
- }
-
- /* check ratio of value interval to number of branches */
- if (((float)(interval + 1) / (float)tbl.num_branches) > 8.0) {
- do_jmp_tbl = 0;
- }
-
- if (do_jmp_tbl) {
- /* TODO: emit table code */
- }
- else {
- /* TODO: emit cmp - jmp cascade */
- }
-
- if (tbl.label)
- free(tbl.label);
- if (tbl.branches)
- free(tbl.branches);
-}
-
/***********************************************************************************
***********************************************************************************/
/**
- * Emits code for a node.
+ * Enters the emitter functions for handled nodes into the generic
+ * pointer of an opcode.
*/
-void TEMPLATE_emit_node(ir_node *irn, void *env) {
- emit_env_t *emit_env = env;
- firm_dbg_module_t *mod = emit_env->mod;
- FILE *F = emit_env->out;
-
- DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
-
-#define BE_EMIT(a) if (is_TEMPLATE_##a(irn)) { emit_TEMPLATE_##a(irn, emit_env); return; }
-
- /* generated int emitter functions */
- BE_EMIT(Copy);
- BE_EMIT(Perm);
-
- BE_EMIT(Const);
-
- BE_EMIT(Add);
- BE_EMIT(Add_i);
- BE_EMIT(Sub);
- BE_EMIT(Sub_i);
- BE_EMIT(Minus);
- BE_EMIT(Inc);
- BE_EMIT(Dec);
-
- BE_EMIT(And);
- BE_EMIT(And_i);
- BE_EMIT(Or);
- BE_EMIT(Or_i);
- BE_EMIT(Eor);
- BE_EMIT(Eor_i);
- BE_EMIT(Not);
+static void TEMPLATE_register_emitters(void) {
- BE_EMIT(Shl);
- BE_EMIT(Shl_i);
- BE_EMIT(Shr);
- BE_EMIT(Shr_i);
- BE_EMIT(RotL);
- BE_EMIT(RotL_i);
- BE_EMIT(RotR);
+/* some convienience macros to register additional emitter functions
+ (other than the generated ones) */
+#define TEMPLATE_EMIT(a) op_TEMPLATE_##a->ops.generic = (op_func)emit_TEMPLATE_##a
+#define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
+#define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
- BE_EMIT(Mul);
- BE_EMIT(Mul_i);
+ /* first clear the generic function pointer for all ops */
+ clear_irp_opcodes_generic_func();
- BE_EMIT(Store);
- BE_EMIT(Load);
+ /* register all emitter functions defined in spec */
+ TEMPLATE_register_spec_emitters();
- /* generated floating point emitter */
- BE_EMIT(fConst);
+ /* register addtional emitter functions if needed */
- BE_EMIT(fAdd);
- BE_EMIT(fSub);
- BE_EMIT(fMinus);
-
- BE_EMIT(fMul);
- BE_EMIT(fDiv);
+#undef TEMPLATE_EMIT
+#undef BE_EMIT
+#undef EMIT
+}
- BE_EMIT(fMin);
- BE_EMIT(fMax);
- BE_EMIT(fLoad);
- BE_EMIT(fStore);
+/**
+ * Emits code for a node.
+ */
+void TEMPLATE_emit_node(ir_node *irn, void *env) {
+ emit_env_t *emit_env = env;
+ FILE *F = emit_env->out;
+ ir_op *op = get_irn_op(irn);
+ DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
- /* other emitter functions */
-// BE_EMIT(Switch);
+ DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
- ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
+ if (op->ops.generic) {
+ void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
+ (*emit)(irn, env);
+ }
+ else {
+ ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
+ }
}
/**
/**
* Emits code for function start.
*/
-void TEMPLATE_emit_start(FILE *F, ir_graph *irg) {
+void TEMPLATE_emit_func_prolog(FILE *F, ir_graph *irg) {
const char *irg_name = get_entity_name(get_irg_entity(irg));
/* TODO: emit function header */
/**
* Emits code for function end
*/
-void TEMPLATE_emit_end(FILE *F, ir_graph *irg) {
+void TEMPLATE_emit_func_epilog(FILE *F, ir_graph *irg) {
const char *irg_name = get_entity_name(get_irg_entity(irg));
/* TODO: emit function end */
/**
* Main driver
*/
-void TEMPLATE_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
+void TEMPLATE_gen_routine(FILE *F, ir_graph *irg, const TEMPLATE_code_gen_t *cg) {
emit_env_t emit_env;
- emit_env.mod = firm_dbg_register("firm.be.TEMPLATE.emit");
emit_env.out = F;
emit_env.arch_env = cg->arch_env;
emit_env.cg = cg;
+ FIRM_DBG_REGISTER(emit_env.mod, "firm.be.TEMPLATE.emit");
/* set the global arch_env (needed by print hooks) */
arch_env = cg->arch_env;
- TEMPLATE_emit_start(F, irg);
+ /* register all emitter functions */
+ TEMPLATE_register_emitters();
+
+ TEMPLATE_emit_func_prolog(F, irg);
irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, &emit_env);
irg_walk_blkwise_graph(irg, NULL, TEMPLATE_gen_block, &emit_env);
- TEMPLATE_emit_end(F, irg);
+ TEMPLATE_emit_func_epilog(F, irg);
}