sparc: implement float->unsigned conversions
[libfirm] / ir / be / TEMPLATE / TEMPLATE_emitter.c
index 7f2b1ad..752d348 100644 (file)
@@ -20,7 +20,6 @@
 /**
  * @file
  * @brief   emit assembler for a backend graph
- * @version $Id$
  */
 #include "config.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"
 
 #define SNPRINTF_BUF_LEN 128
 
-/**
- * Returns the register at in position pos.
- */
-static const arch_register_t *get_in_reg(const ir_node *node, int pos)
+void TEMPLATE_emit_immediate(const ir_node *node)
 {
-       ir_node                *op;
-       const arch_register_t  *reg = NULL;
-
-       assert(get_irn_arity(node) > pos && "Invalid IN position");
+       const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
+       be_emit_tarval(attr->value);
+}
 
-       /* The out register of the operator at position pos is the
-          in register we need. */
-       op = get_irn_n(node, pos);
+static void emit_register(const arch_register_t *reg)
+{
+       be_emit_string(arch_register_get_name(reg));
+}
 
-       reg = arch_get_irn_register(op);
+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);
+}
 
-       assert(reg && "no in register found");
-       return reg;
+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 out position pos.
+ * Returns the target label for a control flow node.
  */
-static const arch_register_t *get_out_reg(const ir_node *node, int pos)
+static void TEMPLATE_emit_cfop_target(const ir_node *node)
 {
-       ir_node                *proj;
-       const arch_register_t  *reg = NULL;
-
-       /* 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(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(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(proj);
-                               break;
-                       }
-               }
-       }
-
-       assert(reg && "no out register found");
-       return reg;
+       ir_node *block = (ir_node*)get_irn_link(node);
+       be_gas_emit_block_name(block);
 }
 
-/*************************************************************
- *             _       _    __   _          _
- *            (_)     | |  / _| | |        | |
- *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
- * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
- * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
- * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
- * | |                                       | |
- * |_|                                       |_|
- *************************************************************/
 
-void TEMPLATE_emit_immediate(const ir_node *node)
+
+/**
+ * Emits code for a unconditional jump.
+ */
+static void emit_TEMPLATE_Jmp(const ir_node *node)
 {
-       (void) node;
-       /* TODO */
+       be_emit_cstring("\tjmp ");
+       TEMPLATE_emit_cfop_target(node);
+       be_emit_finish_line_gas(node);
 }
 
-void TEMPLATE_emit_source_register(const ir_node *node, int pos)
+static void emit_be_IncSP(const ir_node *node)
 {
-       const arch_register_t *reg = get_in_reg(node, pos);
-       be_emit_string(arch_register_get_name(reg));
+       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);
 }
 
-void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
+static void emit_be_Start(const ir_node *node)
 {
-       const arch_register_t *reg = get_out_reg(node, pos);
-       be_emit_string(arch_register_get_name(reg));
+       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);
+       }
 }
 
-/**
- * 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);
+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);
+       }
 
-       be_emit_irprintf("BLOCK_%ld", get_irn_node_nr(block));
+       /* return */
+       be_emit_cstring("\tret");
+       be_emit_finish_line_gas(node);
 }
 
-/***********************************************************************************
- *                  _          __                                             _
- *                 (_)        / _|                                           | |
- *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
- * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
- * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
- * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
- *
- ***********************************************************************************/
+static void emit_nothing(const ir_node *node)
+{
+       (void) node;
+}
 
 /**
- * Emits code for a unconditional jump.
+ * The type of a emitter function.
  */
-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);
+typedef void (emit_func)(const ir_node *node);
 
-       be_emit_cstring("\tjmp ");
-       TEMPLATE_emit_cfop_target(node);
-       be_emit_finish_line_gas(node);
+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) {
-
-/* 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 */
-       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 *);
@@ -192,7 +196,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) {
@@ -207,15 +212,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) {
@@ -223,38 +225,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;
@@ -268,15 +243,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);
 }