-/* TEMPLATE emitter */
-/* $Id$ */
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+/*
+ * 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.
+ */
+
+/**
+ * @file
+ * @brief emit assembler for a backend graph
+ */
+#include "config.h"
#include <limits.h>
#include "irargs_t.h"
#include "irprog.h"
-#include "../besched.h"
+#include "besched.h"
+#include "begnuas.h"
+#include "beblocksched.h"
+#include "benode.h"
#include "TEMPLATE_emitter.h"
#include "gen_TEMPLATE_emitter.h"
+#include "gen_TEMPLATE_regalloc_if.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 */
+static void TEMPLATE_emit_immediate(const ir_node *node)
+{
+ const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
+ be_emit_irprintf("%T", attr->value);
}
-/**
- * Returns node's offset as string.
- */
-static char *node_offset_to_str(ir_node *n) {
- /* TODO */
+static void emit_register(const arch_register_t *reg)
+{
+ be_emit_string(arch_register_get_name(reg));
}
-/* 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;
+static void TEMPLATE_emit_source_register(const ir_node *node, int pos)
+{
+ const arch_register_t *reg = arch_get_irn_register_in(node, pos);
+ emit_register(reg);
}
+static void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
+{
+ const arch_register_t *reg = arch_get_irn_register_out(node, pos);
+ emit_register(reg);
+}
/**
- * Returns the register at in position pos.
+ * Returns the target label for a control flow node.
*/
-static const arch_register_t *get_in_reg(ir_node *irn, int pos) {
- ir_node *op;
- const arch_register_t *reg = NULL;
+static void TEMPLATE_emit_cfop_target(const ir_node *node)
+{
+ ir_node *block = (ir_node*)get_irn_link(node);
+ be_gas_emit_block_name(block);
+}
- assert(get_irn_arity(irn) > pos && "Invalid IN position");
+void TEMPLATE_emitf(const ir_node *node, const char *format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+ be_emit_char('\t');
+ for (;;) {
+ const char *start = format;
+ while (*format != '%' && *format != '\0')
+ ++format;
+ be_emit_string_len(start, format-start);
+ if (*format == '\0')
+ break;
+ ++format;
+
+ switch (*format++) {
+ case '%':
+ be_emit_char('%');
+ break;
+
+ case 'S': {
+ if (*format < '0' || '9' <= *format)
+ goto unknown;
+ unsigned const pos = *format++ - '0';
+ TEMPLATE_emit_source_register(node, pos);
+ break;
+ }
- /* The out register of the operator at position pos is the
- in register we need. */
- op = get_irn_n(irn, pos);
+ case 'D': {
+ if (*format < '0' || '9' <= *format)
+ goto unknown;
+ unsigned const pos = *format++ - '0';
+ TEMPLATE_emit_dest_register(node, pos);
+ break;
+ }
- reg = arch_get_irn_register(arch_env, op);
+ case 'I':
+ TEMPLATE_emit_immediate(node);
+ break;
- assert(reg && "no in register found");
- return reg;
-}
+ case 'X': {
+ int num = va_arg(ap, int);
+ be_emit_irprintf("%X", num);
+ break;
+ }
-/**
- * Returns the register at out position pos.
- */
-static const arch_register_t *get_out_reg(ir_node *irn, int pos) {
- ir_node *proj;
- const arch_register_t *reg = NULL;
+ case 'u': {
+ unsigned num = va_arg(ap, unsigned);
+ be_emit_irprintf("%u", num);
+ break;
+ }
- /* 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 */
+ case 'd': {
+ int num = va_arg(ap, int);
+ be_emit_irprintf("%d", num);
+ break;
+ }
- 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 {
- const ir_edge_t *edge;
-
- foreach_out_edge(irn, 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);
- break;
- }
+ case 's': {
+ const char *string = va_arg(ap, const char *);
+ be_emit_string(string);
+ break;
}
- }
- assert(reg && "no out register found");
- return reg;
-}
+ case 'L': {
+ TEMPLATE_emit_cfop_target(node);
+ break;
+ }
-/**
- * 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;
+ case '\n':
+ be_emit_char('\n');
+ be_emit_write_line();
+ be_emit_char('\t');
+ break;
- if (in_out == 1) {
- reg = get_in_reg(irn, pos);
- }
- else {
- reg = get_out_reg(irn, pos);
+ default:
+unknown:
+ panic("unknown format conversion in arm_emitf()");
+ }
}
+ va_end(ap);
+ be_emit_finish_line_gas(node);
- return arch_register_get_index(reg);
}
/**
- * Returns the name of the in register at position pos.
+ * Emits code for a unconditional jump.
*/
-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);
+static void emit_TEMPLATE_Jmp(const ir_node *node)
+{
+ TEMPLATE_emitf(node, "jmp %L");
}
-/**
- * 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)
+static void emit_be_IncSP(const ir_node *node)
{
- const char *buf;
- ir_node *X = arg->v_ptr;
- int nr = occ->width - 1;
+ int offset = be_get_IncSP_offset(node);
- if (!X)
- return lc_arg_append(app, occ, "(null)", 6);
+ if (offset == 0)
+ return;
- if (occ->conversion == 'S') {
- buf = get_TEMPLATE_reg_name(X, nr, 1);
- }
- else { /* 'D' */
- buf = get_TEMPLATE_reg_name(X, nr, 0);
+ /* downwards growing stack */
+ const char *op = "add";
+ if (offset < 0) {
+ op = "sub";
+ offset = -offset;
}
- return buf ? lc_arg_append(app, occ, buf, strlen(buf)) : 0;
+ TEMPLATE_emitf(node, "%s %S0, %d, %D0", op, offset);
}
-/**
- * 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)
+static void emit_be_Start(const ir_node *node)
{
- const char *buf;
- ir_node *X = arg->v_ptr;
+ ir_graph *irg = get_irn_irg(node);
+ ir_type *frame_type = get_irg_frame_type(irg);
+ unsigned size = get_type_size_bytes(frame_type);
- if (!X)
- return lc_arg_append(app, occ, "(null)", 6);
+ /* emit function prolog */
- if (occ->conversion == 'C') {
- buf = node_const_to_str(X);
+ /* allocate stackframe */
+ if (size > 0) {
+ TEMPLATE_emitf(node, "sub %%sp, %u, %%sp", size);
}
- else { /* 'O' */
- buf = node_offset_to_str(X);
- }
-
- return lc_arg_append(app, occ, buf, strlen(buf));
}
-/**
- * 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)
+static void emit_be_Return(const ir_node *node)
{
- ir_node *X = arg->v_ptr;
-
- if (!X)
- return lc_arg_append(app, occ, "(null)", 6);
+ ir_graph *irg = get_irn_irg(node);
+ ir_type *frame_type = get_irg_frame_type(irg);
+ unsigned size = get_type_size_bytes(frame_type);
- if (get_mode_size_bits(get_irn_mode(X)) == 32)
- return lc_appendable_chadd(app, 's');
- else
- return lc_appendable_chadd(app, 'd');
-}
+ /* emit function epilog here */
-/**
- * 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);
+ /* deallocate stackframe */
+ if (size > 0) {
+ TEMPLATE_emitf(node, "add %%sp, %u, %%sp", size);
}
- return env;
+ /* return */
+ TEMPLATE_emitf(node, "ret");
}
-/*
- * 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;
+static void emit_nothing(const ir_node *node)
+{
+ (void) node;
}
-
/**
- * Returns the target label for a control flow node.
+ * The type of a emitter function.
*/
-static char *get_cfop_target(const ir_node *irn, char *buf) {
- ir_node *bl = get_irn_link(irn);
+typedef void (emit_func)(const ir_node *node);
- snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
- return buf;
+static inline void set_emitter(ir_op *op, emit_func func)
+{
+ op->ops.generic = (op_func)func;
}
-
-
-/***********************************************************************************
- * _ __ _
- * (_) / _| | |
- * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
- * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
- * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
- * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
- *
- ***********************************************************************************/
-
/**
* Enters the emitter functions for handled nodes into the generic
* pointer of an opcode.
*/
-static void TEMPLATE_register_emitters(void) {
-
-/* 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
-
+static void TEMPLATE_register_emitters(void)
+{
/* first clear the generic function pointer for all ops */
- clear_irp_opcodes_generic_func();
+ ir_clear_opcodes_generic_func();
/* register all emitter functions defined in spec */
TEMPLATE_register_spec_emitters();
- /* register addtional emitter functions if needed */
+ /* custom emitters not provided by the spec */
+ set_emitter(op_TEMPLATE_Jmp, emit_TEMPLATE_Jmp);
+ set_emitter(op_be_IncSP, emit_be_IncSP);
+ set_emitter(op_be_Return, emit_be_Return);
+ set_emitter(op_be_Start, emit_be_Start);
-#undef TEMPLATE_EMIT
-#undef BE_EMIT
-#undef EMIT
+ /* no need to emit anything for the following nodes */
+ set_emitter(op_Phi, emit_nothing);
+ set_emitter(op_be_Keep, emit_nothing);
}
+typedef void (*emit_func_ptr) (const ir_node *);
/**
* Emits code for a node.
*/
-void TEMPLATE_emit_node(ir_node *irn, void *env) {
- TEMPLATE_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;)
-
- DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
+static void TEMPLATE_emit_node(const ir_node *node)
+{
+ ir_op *op = get_irn_op(node);
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);
+ 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) {
- TEMPLATE_emit_env_t *emit_env = env;
- ir_node *irn;
-
- if (! is_Block(block))
- return;
+static void TEMPLATE_emit_block(ir_node *block)
+{
+ be_gas_begin_block(block, true);
- fprintf(emit_env->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
- sched_foreach(block, irn) {
- TEMPLATE_emit_node(irn, env);
+ sched_foreach(block, node) {
+ TEMPLATE_emit_node(node);
}
}
-
-/**
- * Emits code for function start.
- */
-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_func_epilog(FILE *F, ir_graph *irg) {
- const char *irg_name = get_entity_name(get_irg_entity(irg));
-
- /* TODO: emit function end */
-}
-
/**
* Sets labels for control flow nodes (jump target)
- * TODO: Jump optimization
*/
-void TEMPLATE_gen_labels(ir_node *block, void *env) {
+static 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);
/**
* Main driver
*/
-void TEMPLATE_gen_routine(FILE *F, ir_graph *irg, const TEMPLATE_code_gen_t *cg) {
- TEMPLATE_emit_env_t emit_env;
-
- 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;
+void TEMPLATE_emit_routine(ir_graph *irg)
+{
+ ir_node **block_schedule;
+ ir_entity *entity = get_irg_entity(irg);
+ size_t i;
+ size_t n;
/* 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_func_epilog(F, irg);
+ /* create the block schedule */
+ block_schedule = be_create_block_schedule(irg);
+
+ /* emit assembler prolog */
+ be_gas_emit_function_prolog(entity, 4, NULL);
+
+ /* populate jump link fields with their destinations */
+ irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, NULL);
+
+ n = ARR_LEN(block_schedule);
+ for (i = 0; i < n; ++i) {
+ ir_node *block = block_schedule[i];
+ TEMPLATE_emit_block(block);
+ }
+ be_gas_emit_function_epilog(entity);
}