give Bad nodes a mode
[libfirm] / ir / be / TEMPLATE / TEMPLATE_emitter.c
index d1b999b..01be723 100644 (file)
@@ -22,9 +22,7 @@
  * @brief   emit assembler for a backend graph
  * @version $Id$
  */
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
 #include <limits.h>
 
 #include "irprog.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
 
@@ -84,7 +85,7 @@ static const arch_register_t *get_out_reg(const ir_node *node, int pos)
        if (get_irn_mode(node) != mode_T) {
                reg = arch_get_irn_register(node);
        } else if (is_TEMPLATE_irn(node)) {
-               reg = get_TEMPLATE_out_reg(node, pos);
+               reg = arch_irn_get_register(node, pos);
        } else {
                const ir_edge_t *edge;
 
@@ -102,92 +103,149 @@ static const arch_register_t *get_out_reg(const ir_node *node, int pos)
        return reg;
 }
 
-/*************************************************************
- *             _       _    __   _          _
- *            (_)     | |  / _| | |        | |
- *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
- * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
- * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
- * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
- * | |                                       | |
- * |_|                                       |_|
- *************************************************************/
+
 
 void TEMPLATE_emit_immediate(const ir_node *node)
 {
-       (void) node;
-       /* TODO */
+       const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
+       be_emit_tarval(attr->value);
+}
+
+static void emit_register(const arch_register_t *reg)
+{
+       be_emit_string(arch_register_get_name(reg));
 }
 
 void TEMPLATE_emit_source_register(const ir_node *node, int pos)
 {
        const arch_register_t *reg = get_in_reg(node, pos);
-       be_emit_string(arch_register_get_name(reg));
+       emit_register(reg);
 }
 
 void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
 {
        const arch_register_t *reg = get_out_reg(node, pos);
-       be_emit_string(arch_register_get_name(reg));
+       emit_register(reg);
 }
 
 /**
  * Returns the target label for a control flow node.
  */
-static void TEMPLATE_emit_cfop_target(const ir_node *node) {
-       ir_node *block = get_irn_link(node);
-
-       be_emit_irprintf("BLOCK_%ld", get_irn_node_nr(block));
+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);
 }
 
-/***********************************************************************************
- *                  _          __                                             _
- *                 (_)        / _|                                           | |
- *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
- * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
- * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
- * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
- *
- ***********************************************************************************/
+
 
 /**
  * Emits code for a unconditional jump.
  */
-static void emit_Jmp(const ir_node *node) {
-       ir_node *block;
-
-       /* for now, the code works for scheduled and non-schedules blocks */
-       block = get_nodes_block(node);
-
+static void emit_TEMPLATE_Jmp(const ir_node *node)
+{
        be_emit_cstring("\tjmp ");
        TEMPLATE_emit_cfop_target(node);
        be_emit_finish_line_gas(node);
 }
 
+static void emit_be_IncSP(const ir_node *node)
+{
+       int offset = be_get_IncSP_offset(node);
+
+       if (offset == 0)
+               return;
+
+       /* downwards growing stack */
+       if (offset > 0) {
+               be_emit_cstring("\tsub ");
+       } else {
+               be_emit_cstring("\tadd ");
+               offset = -offset;
+       }
+
+       TEMPLATE_emit_source_register(node, 0);
+       be_emit_irprintf(", %d, ", offset);
+       TEMPLATE_emit_dest_register(node, 0);
+       be_emit_finish_line_gas(node);
+}
+
+static void emit_be_Start(const ir_node *node)
+{
+       ir_graph *irg        = get_irn_irg(node);
+       ir_type  *frame_type = get_irg_frame_type(irg);
+       unsigned  size       = get_type_size_bytes(frame_type);
+
+       /* emit function prolog */
+
+       /* allocate stackframe */
+       if (size > 0) {
+               be_emit_cstring("\tsub ");
+               emit_register(&TEMPLATE_registers[REG_SP]);
+               be_emit_irprintf(", %u, ", size);
+               emit_register(&TEMPLATE_registers[REG_SP]);
+               be_emit_finish_line_gas(node);
+       }
+}
+
+static void emit_be_Return(const ir_node *node)
+{
+       ir_graph *irg        = get_irn_irg(node);
+       ir_type  *frame_type = get_irg_frame_type(irg);
+       unsigned  size       = get_type_size_bytes(frame_type);
+
+       /* emit function epilog here */
+
+       /* deallocate stackframe */
+       if (size > 0) {
+               be_emit_cstring("\tadd ");
+               emit_register(&TEMPLATE_registers[REG_SP]);
+               be_emit_irprintf(", %u, ", size);
+               emit_register(&TEMPLATE_registers[REG_SP]);
+               be_emit_finish_line_gas(node);
+       }
+
+       /* return */
+       be_emit_cstring("\tret");
+       be_emit_finish_line_gas(node);
+}
+
+static void emit_nothing(const ir_node *node)
+{
+       (void) node;
+}
+
 /**
- * Enters the emitter functions for handled nodes into the generic
- * pointer of an opcode.
+ * The type of a emitter function.
  */
-static void TEMPLATE_register_emitters(void) {
+typedef void (emit_func)(const ir_node *node);
 
-/* 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 inline void set_emitter(ir_op *op, emit_func func)
+{
+       op->ops.generic = (op_func)func;
+}
 
+/**
+ * Enters the emitter functions for handled nodes into the generic
+ * pointer of an opcode.
+ */
+static void TEMPLATE_register_emitters(void)
+{
        /* first clear the generic function pointer for all ops */
        clear_irp_opcodes_generic_func();
 
        /* register all emitter functions defined in spec */
        TEMPLATE_register_spec_emitters();
 
-       /* register addtional emitter functions if needed */
-       EMIT(Jmp);
+       /* 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 *);
@@ -195,7 +253,8 @@ typedef void (*emit_func_ptr) (const ir_node *);
 /**
  * Emits code for a node.
  */
-void TEMPLATE_emit_node(const ir_node *node) {
+static void TEMPLATE_emit_node(const ir_node *node)
+{
        ir_op               *op       = get_irn_op(node);
 
        if (op->ops.generic) {
@@ -210,15 +269,12 @@ void TEMPLATE_emit_node(const ir_node *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 *data) {
+static void TEMPLATE_emit_block(ir_node *block)
+{
        ir_node *node;
-       (void) data;
-
-       if (! is_Block(block))
-               return;
 
-       be_emit_cstring("BLOCK_");
-       be_emit_irprintf("%ld:\n", get_irn_node_nr(block));
+       be_gas_emit_block_name(block);
+       be_emit_cstring(":\n");
        be_emit_write_line();
 
        sched_foreach(block, node) {
@@ -226,38 +282,11 @@ void TEMPLATE_gen_block(ir_node *block, void *data) {
        }
 }
 
-
-/**
- * Emits code for function start.
- */
-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_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) {
+static void TEMPLATE_gen_labels(ir_node *block, void *env)
+{
        ir_node *pred;
        int n = get_Block_n_cfgpreds(block);
        (void) env;
@@ -271,15 +300,29 @@ void TEMPLATE_gen_labels(ir_node *block, void *env) {
 /**
  * Main driver
  */
-void TEMPLATE_gen_routine(const TEMPLATE_code_gen_t *cg, ir_graph *irg)
+void TEMPLATE_emit_routine(ir_graph *irg)
 {
-       (void)cg;
+       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(irg);
+       /* create the block schedule */
+       block_schedule = be_create_block_schedule(irg);
+
+       /* emit assembler prolog */
+       be_gas_emit_function_prolog(entity, 4);
+
+       /* populate jump link fields with their destinations */
        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);
+
+       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);
 }