X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fbe%2FTEMPLATE%2FTEMPLATE_emitter.c;h=4ba42d2a570b2a845d7d09e7c1943e01af3fb8d7;hb=ebdaa596d904a0651c8d794481288d2d629bdb3a;hp=4a38e5fe5cd8441cdde7fcf2a3ca49aed3ce1326;hpb=17e47394fa72025d14172a2acef2d258a067aa42;p=libfirm diff --git a/ir/be/TEMPLATE/TEMPLATE_emitter.c b/ir/be/TEMPLATE/TEMPLATE_emitter.c index 4a38e5fe5..4ba42d2a5 100644 --- a/ir/be/TEMPLATE/TEMPLATE_emitter.c +++ b/ir/be/TEMPLATE/TEMPLATE_emitter.c @@ -1,9 +1,28 @@ -/* TEMPLATE emitter */ -/* $Id$ */ +/* + * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved. + * + * This file is part of libFirm. + * + * This file may be distributed and/or modified under the terms of the + * GNU General Public License version 2 as published by the Free Software + * Foundation and appearing in the file LICENSE.GPL included in the + * packaging of this file. + * + * Licensees holding valid libFirm Professional Edition licenses may use + * this file in accordance with the libFirm Commercial License. + * Agreement provided with the Software. + * + * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE + * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE. + */ -#ifdef HAVE_CONFIG_H +/** + * @file + * @brief emit assembler for a backend graph + * @version $Id$ + */ #include "config.h" -#endif #include @@ -15,6 +34,7 @@ #include "irprintf.h" #include "irop_t.h" #include "irargs_t.h" +#include "irprog.h" #include "../besched.h" @@ -22,58 +42,24 @@ #include "gen_TEMPLATE_emitter.h" #include "TEMPLATE_nodes_attr.h" #include "TEMPLATE_new_nodes.h" -#include "TEMPLATE_map_regs.h" #define SNPRINTF_BUF_LEN 128 -static const arch_env_t *arch_env = NULL; - - -/************************************************************* - * _ _ __ _ _ - * (_) | | / _| | | | | - * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __ - * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__| - * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ | - * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_| - * | | | | - * |_| |_| - *************************************************************/ - -/** - * Return a const or symconst as string. - */ -static const char *node_const_to_str(ir_node *n) { - /* TODO */ -} - -/** - * Returns node's offset as string. - */ -static char *node_offset_to_str(ir_node *n) { - /* TODO */ -} - -/* We always pass the ir_node which is a pointer. */ -static int TEMPLATE_get_arg_type(const lc_arg_occ_t *occ) { - return lc_arg_type_ptr; -} - - /** * Returns the register at in position pos. */ -static const arch_register_t *get_in_reg(ir_node *irn, int pos) { +static const arch_register_t *get_in_reg(const ir_node *node, int pos) +{ ir_node *op; const arch_register_t *reg = NULL; - assert(get_irn_arity(irn) > pos && "Invalid IN position"); + assert(get_irn_arity(node) > pos && "Invalid IN position"); /* The out register of the operator at position pos is the in register we need. */ - op = get_irn_n(irn, pos); + op = get_irn_n(node, pos); - reg = arch_get_irn_register(arch_env, op); + reg = arch_get_irn_register(op); assert(reg && "no in register found"); return reg; @@ -82,31 +68,28 @@ static const arch_register_t *get_in_reg(ir_node *irn, int pos) { /** * Returns the register at out position pos. */ -static const arch_register_t *get_out_reg(ir_node *irn, int pos) { +static const arch_register_t *get_out_reg(const ir_node *node, int pos) +{ 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 */ /* Proj with the corresponding projnum for the register */ - if (get_irn_mode(irn) != mode_T) { - reg = arch_get_irn_register(arch_env, irn); - } - else if (is_TEMPLATE_irn(irn)) { - reg = get_TEMPLATE_out_reg(irn, pos); - } - else { + if (get_irn_mode(node) != mode_T) { + reg = arch_get_irn_register(node); + } else if (is_TEMPLATE_irn(node)) { + reg = arch_irn_get_register(node, pos); + } else { const ir_edge_t *edge; - foreach_out_edge(irn, edge) { + foreach_out_edge(node, edge) { proj = get_edge_src_irn(edge); assert(is_Proj(proj) && "non-Proj from mode_T node"); if (get_Proj_proj(proj) == pos) { - reg = arch_get_irn_register(arch_env, proj); + reg = arch_get_irn_register(proj); break; } } @@ -116,149 +99,45 @@ static const arch_register_t *get_out_reg(ir_node *irn, int pos) { return reg; } -/** - * Returns the number of the in register at position pos. - */ -int get_TEMPLATE_reg_nr(ir_node *irn, int pos, int in_out) { - const arch_register_t *reg; - - if (in_out == 1) { - reg = get_in_reg(irn, pos); - } - else { - reg = get_out_reg(irn, pos); - } - - return arch_register_get_index(reg); -} - -/** - * Returns the name of the in register at position pos. - */ -const char *get_TEMPLATE_reg_name(ir_node *irn, int pos, int in_out) { - const arch_register_t *reg; - - if (in_out == 1) { - reg = get_in_reg(irn, pos); - } - else { - reg = get_out_reg(irn, pos); - } - - return arch_register_get_name(reg); -} +/************************************************************* + * _ _ __ _ _ + * (_) | | / _| | | | | + * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __ + * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__| + * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ | + * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_| + * | | | | + * |_| |_| + *************************************************************/ -/** - * Get the register name for a node. - */ -static int TEMPLATE_get_reg_name(lc_appendable_t *app, - const lc_arg_occ_t *occ, const lc_arg_value_t *arg) +void TEMPLATE_emit_immediate(const ir_node *node) { - const char *buf; - ir_node *X = arg->v_ptr; - int nr = occ->width - 1; - - if (!X) - return lc_arg_append(app, occ, "(null)", 6); - - if (occ->conversion == 'S') { - buf = get_TEMPLATE_reg_name(X, nr, 1); - } - else { /* 'D' */ - buf = get_TEMPLATE_reg_name(X, nr, 0); - } - - lc_appendable_chadd(app, '%'); - return lc_arg_append(app, occ, buf, strlen(buf)); + (void) node; + /* TODO */ } -/** - * 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) +void TEMPLATE_emit_source_register(const ir_node *node, int pos) { - const char *buf; - ir_node *X = arg->v_ptr; - - if (!X) - return lc_arg_append(app, occ, "(null)", 6); - - if (occ->conversion == 'C') { - buf = node_const_to_str(X); - } - else { /* 'O' */ - buf = node_offset_to_str(X); - } - - return lc_arg_append(app, occ, buf, strlen(buf)); + const arch_register_t *reg = get_in_reg(node, pos); + be_emit_string(arch_register_get_name(reg)); } -/** - * Determines the SSE suffix depending on the mode. - */ -static int TEMPLATE_get_mode_suffix(lc_appendable_t *app, - const lc_arg_occ_t *occ, const lc_arg_value_t *arg) +void TEMPLATE_emit_dest_register(const ir_node *node, int pos) { - ir_node *X = arg->v_ptr; - - if (!X) - return lc_arg_append(app, occ, "(null)", 6); - - if (get_mode_size_bits(get_irn_mode(X)) == 32) - return lc_appendable_chadd(app, 's'); - else - return lc_appendable_chadd(app, 'd'); + const arch_register_t *reg = get_out_reg(node, pos); + be_emit_string(arch_register_get_name(reg)); } -/** - * 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 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, "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; -} - -/* - * Add a number to a prefix. This number will not be used a second time. - */ -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; -} - - /** * Returns the target label for a control flow node. */ -static char *get_cfop_target(const ir_node *irn, char *buf) { - ir_node *bl = get_irn_link(irn); +static void TEMPLATE_emit_cfop_target(const ir_node *node) +{ + ir_node *block = get_irn_link(node); - snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl)); - return buf; + be_emit_irprintf("BLOCK_%ld", get_irn_node_nr(block)); } - - /*********************************************************************************** * _ __ _ * (_) / _| | | @@ -270,81 +149,82 @@ static char *get_cfop_target(const ir_node *irn, char *buf) { ***********************************************************************************/ /** - * Emits code for a node. + * Emits code for a unconditional jump. */ -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; } - - BE_EMIT(Const); +static void emit_Jmp(const ir_node *node) +{ + ir_node *block; - BE_EMIT(Add); - BE_EMIT(Add_i); - BE_EMIT(Sub); - BE_EMIT(Sub_i); - BE_EMIT(Minus); - BE_EMIT(Inc); - BE_EMIT(Dec); + /* for now, the code works for scheduled and non-schedules blocks */ + block = get_nodes_block(node); - BE_EMIT(And); - BE_EMIT(And_i); - BE_EMIT(Or); - BE_EMIT(Or_i); - BE_EMIT(Eor); - BE_EMIT(Eor_i); - BE_EMIT(Not); + be_emit_cstring("\tjmp "); + TEMPLATE_emit_cfop_target(node); + be_emit_finish_line_gas(node); +} - BE_EMIT(Shl); - BE_EMIT(Shl_i); - BE_EMIT(Shr); - BE_EMIT(Shr_i); - BE_EMIT(RotL); - BE_EMIT(RotL_i); - BE_EMIT(RotR); +/** + * Enters the emitter functions for handled nodes into the generic + * pointer of an opcode. + */ +static void TEMPLATE_register_emitters(void) +{ - BE_EMIT(Mul); - BE_EMIT(Mul_i); +/* 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(Store); - BE_EMIT(Load); + /* first clear the generic function pointer for all ops */ + clear_irp_opcodes_generic_func(); - /* generated floating point emitter */ - BE_EMIT(fConst); + /* register all emitter functions defined in spec */ + TEMPLATE_register_spec_emitters(); - BE_EMIT(fAdd); - BE_EMIT(fSub); - BE_EMIT(fMinus); + /* register addtional emitter functions if needed */ + EMIT(Jmp); - BE_EMIT(fMul); - BE_EMIT(fDiv); +#undef TEMPLATE_EMIT +#undef BE_EMIT +#undef EMIT +} - BE_EMIT(fMin); - BE_EMIT(fMax); +typedef void (*emit_func_ptr) (const ir_node *); - BE_EMIT(fLoad); - BE_EMIT(fStore); +/** + * Emits code for a node. + */ +void TEMPLATE_emit_node(const ir_node *node) +{ + ir_op *op = get_irn_op(node); - ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn); + if (op->ops.generic) { + emit_func_ptr func = (emit_func_ptr) op->ops.generic; + (*func) (node); + } else { + ir_fprintf(stderr, "No emitter for node %+F\n", node); + } } /** * Walks over the nodes in a block connected by scheduling edges * and emits code for each node. */ -void TEMPLATE_gen_block(ir_node *block, void *env) { - ir_node *irn; +void TEMPLATE_gen_block(ir_node *block, void *data) +{ + ir_node *node; + (void) data; if (! is_Block(block)) return; - fprintf(((emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block)); - sched_foreach(block, irn) { - TEMPLATE_emit_node(irn, env); + be_emit_cstring("BLOCK_"); + be_emit_irprintf("%ld:\n", get_irn_node_nr(block)); + be_emit_write_line(); + + sched_foreach(block, node) { + TEMPLATE_emit_node(node); } } @@ -352,28 +232,40 @@ void TEMPLATE_gen_block(ir_node *block, void *env) { /** * Emits code for function start. */ -void TEMPLATE_emit_start(FILE *F, ir_graph *irg) { +void TEMPLATE_emit_func_prolog(ir_graph *irg) +{ const char *irg_name = get_entity_name(get_irg_entity(irg)); /* TODO: emit function header */ + be_emit_cstring("/* start of "); + be_emit_string(irg_name); + be_emit_cstring(" */\n"); + be_emit_write_line(); } /** * Emits code for function end */ -void TEMPLATE_emit_end(FILE *F, ir_graph *irg) { +void TEMPLATE_emit_func_epilog(ir_graph *irg) +{ const char *irg_name = get_entity_name(get_irg_entity(irg)); /* TODO: emit function end */ + be_emit_cstring("/* end of "); + be_emit_string(irg_name); + be_emit_cstring(" */\n"); + be_emit_write_line(); } /** * Sets labels for control flow nodes (jump target) * TODO: Jump optimization */ -void TEMPLATE_gen_labels(ir_node *block, void *env) { +void TEMPLATE_gen_labels(ir_node *block, void *env) +{ ir_node *pred; int n = get_Block_n_cfgpreds(block); + (void) env; for (n--; n >= 0; n--) { pred = get_Block_cfgpred(block, n); @@ -384,19 +276,15 @@ void TEMPLATE_gen_labels(ir_node *block, void *env) { /** * Main driver */ -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; +void TEMPLATE_gen_routine(const TEMPLATE_code_gen_t *cg, ir_graph *irg) +{ + (void)cg; - /* set the global arch_env (needed by print hooks) */ - arch_env = cg->arch_env; + /* register all emitter functions */ + TEMPLATE_register_emitters(); - TEMPLATE_emit_start(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_prolog(irg); + irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, NULL); + irg_walk_blkwise_graph(irg, NULL, TEMPLATE_gen_block, NULL); + TEMPLATE_emit_func_epilog(irg); }