becopyilp: Do not advertise the switch to dump the solution, because this is not...
[libfirm] / ir / be / TEMPLATE / TEMPLATE_emitter.c
index 48524a0..cd9a013 100644 (file)
@@ -1,31 +1,17 @@
 /*
- * 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.
+ * Copyright (C) 2012 University of Karlsruhe.
  */
 
 /**
  * @file
  * @brief   emit assembler for a backend graph
- * @version $Id$
  */
 #include "config.h"
 
 #include <limits.h>
 
+#include "error.h"
 #include "xmalloc.h"
 #include "tv.h"
 #include "iredges.h"
 #include "irargs_t.h"
 #include "irprog.h"
 
-#include "../besched.h"
-#include "../begnuas.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)
-{
-       ir_node                *op;
-       const arch_register_t  *reg = NULL;
-
-       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(node, pos);
-
-       reg = arch_get_irn_register(op);
-
-       assert(reg && "no in register found");
-       return reg;
-}
-
-/**
- * Returns the register at out position pos.
- */
-static const arch_register_t *get_out_reg(const ir_node *node, int pos)
+static void TEMPLATE_emit_immediate(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;
+       const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
+       be_emit_irprintf("%T", attr->value);
 }
 
-/*************************************************************
- *             _       _    __   _          _
- *            (_)     | |  / _| | |        | |
- *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
- * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
- * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
- * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
- * | |                                       | |
- * |_|                                       |_|
- *************************************************************/
-
-void TEMPLATE_emit_immediate(const ir_node *node)
+static void emit_register(const arch_register_t *reg)
 {
-       (void) node;
-       /* TODO */
+       be_emit_string(reg->name);
 }
 
-void TEMPLATE_emit_source_register(const ir_node *node, int pos)
+static 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));
+       const arch_register_t *reg = arch_get_irn_register_in(node, pos);
+       emit_register(reg);
 }
 
-void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
+static 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));
+       const arch_register_t *reg = arch_get_irn_register_out(node, pos);
+       emit_register(reg);
 }
 
 /**
@@ -134,127 +61,184 @@ void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
  */
 static void TEMPLATE_emit_cfop_target(const ir_node *node)
 {
-       ir_node *block = get_irn_link(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)
+void TEMPLATE_emitf(const ir_node *node, const char *format, ...)
 {
-       ir_node *block;
+       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;
+               }
 
-       /* for now, the code works for scheduled and non-schedules blocks */
-       block = get_nodes_block(node);
+               case 'D': {
+                       if (*format < '0' || '9' <= *format)
+                               goto unknown;
+                       unsigned const pos = *format++ - '0';
+                       TEMPLATE_emit_dest_register(node, pos);
+                       break;
+               }
+
+               case 'I':
+                       TEMPLATE_emit_immediate(node);
+                       break;
+
+               case 'X': {
+                       int num = va_arg(ap, int);
+                       be_emit_irprintf("%X", num);
+                       break;
+               }
+
+               case 'u': {
+                       unsigned num = va_arg(ap, unsigned);
+                       be_emit_irprintf("%u", num);
+                       break;
+               }
+
+               case 'd': {
+                       int num = va_arg(ap, int);
+                       be_emit_irprintf("%d", num);
+                       break;
+               }
+
+               case 's': {
+                       const char *string = va_arg(ap, const char *);
+                       be_emit_string(string);
+                       break;
+               }
 
-       be_emit_cstring("\tjmp ");
-       TEMPLATE_emit_cfop_target(node);
+               case 'L': {
+                       TEMPLATE_emit_cfop_target(node);
+                       break;
+               }
+
+               case '\n':
+                       be_emit_char('\n');
+                       be_emit_write_line();
+                       be_emit_char('\t');
+                       break;
+
+               default:
+unknown:
+                       panic("unknown format conversion");
+               }
+       }
+       va_end(ap);
        be_emit_finish_line_gas(node);
+
 }
 
 /**
- * Enters the emitter functions for handled nodes into the generic
- * pointer of an opcode.
+ * Emits code for a unconditional jump.
  */
-static void TEMPLATE_register_emitters(void)
+static void emit_TEMPLATE_Jmp(const ir_node *node)
 {
+       TEMPLATE_emitf(node, "jmp %L");
+}
 
-/* 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
-
-       /* first clear the generic function pointer for all ops */
-       clear_irp_opcodes_generic_func();
+static void emit_be_IncSP(const ir_node *node)
+{
+       int offset = be_get_IncSP_offset(node);
 
-       /* register all emitter functions defined in spec */
-       TEMPLATE_register_spec_emitters();
+       if (offset == 0)
+               return;
 
-       /* register addtional emitter functions if needed */
-       EMIT(Jmp);
+       /* downwards growing stack */
+       const char *op = "add";
+       if (offset < 0) {
+               op = "sub";
+               offset = -offset;
+       }
 
-#undef TEMPLATE_EMIT
-#undef BE_EMIT
-#undef EMIT
+       TEMPLATE_emitf(node, "%s %S0, %d, %D0", op, offset);
 }
 
-typedef void (*emit_func_ptr) (const ir_node *);
-
-/**
- * Emits code for a node.
- */
-static void TEMPLATE_emit_node(const ir_node *node)
+static void emit_be_Start(const ir_node *node)
 {
-       ir_op               *op       = get_irn_op(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 */
 
-       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);
+       /* allocate stackframe */
+       if (size > 0) {
+               TEMPLATE_emitf(node, "sub %%sp, %u, %%sp", size);
        }
 }
 
-/**
- * Walks over the nodes in a block connected by scheduling edges
- * and emits code for each node.
- */
-static void TEMPLATE_gen_block(ir_node *block, void *data)
+static void emit_be_Return(const ir_node *node)
 {
-       ir_node *node;
-       (void) data;
+       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 (! is_Block(block))
-               return;
-
-       be_gas_emit_block_name(block);
-       be_emit_cstring(":\n");
-       be_emit_write_line();
+       /* emit function epilog here */
 
-       sched_foreach(block, node) {
-               TEMPLATE_emit_node(node);
+       /* deallocate stackframe */
+       if (size > 0) {
+               TEMPLATE_emitf(node, "add %%sp, %u, %%sp", size);
        }
-}
 
+       /* return */
+       TEMPLATE_emitf(node, "ret");
+}
 
 /**
- * Emits code for function start.
+ * Enters the emitter functions for handled nodes into the generic
+ * pointer of an opcode.
  */
-static void TEMPLATE_emit_func_prolog(ir_graph *irg)
+static void TEMPLATE_register_emitters(void)
 {
-       const char *irg_name = get_entity_name(get_irg_entity(irg));
+       /* first clear the generic function pointer for all ops */
+       ir_clear_opcodes_generic_func();
+
+       /* register all emitter functions defined in spec */
+       TEMPLATE_register_spec_emitters();
 
-       /* TODO: emit function header */
-       be_emit_cstring("/* start of ");
-       be_emit_string(irg_name);
-       be_emit_cstring(" */\n");
-       be_emit_write_line();
+       /* custom emitters not provided by the spec */
+       be_set_emitter(op_TEMPLATE_Jmp, emit_TEMPLATE_Jmp);
+       be_set_emitter(op_be_IncSP,     emit_be_IncSP);
+       be_set_emitter(op_be_Return,    emit_be_Return);
+       be_set_emitter(op_be_Start,     emit_be_Start);
+
+       /* no need to emit anything for the following nodes */
+       be_set_emitter(op_Phi,     be_emit_nothing);
+       be_set_emitter(op_be_Keep, be_emit_nothing);
 }
 
 /**
- * Emits code for function end
+ * Walks over the nodes in a block connected by scheduling edges
+ * and emits code for each node.
  */
-static void TEMPLATE_emit_func_epilog(ir_graph *irg)
+static void TEMPLATE_emit_block(ir_node *block)
 {
-       const char *irg_name = get_entity_name(get_irg_entity(irg));
+       be_gas_begin_block(block, true);
 
-       /* TODO: emit function end */
-       be_emit_cstring("/* end of ");
-       be_emit_string(irg_name);
-       be_emit_cstring(" */\n");
-       be_emit_write_line();
+       sched_foreach(block, node) {
+               be_emit_node(node);
+       }
 }
 
 /**
@@ -275,15 +259,29 @@ static 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, NULL);
+
+       /* 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);
 }