Use symbolic names instead of magic values for the position parameter of get_irn_n().
[libfirm] / ir / be / TEMPLATE / TEMPLATE_new_nodes.c
index 4d4a2d7..12778e9 100644 (file)
@@ -1,18 +1,33 @@
+/*
+ * 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.
+ */
+
 /**
- * This file implements the creation of the achitecture specific firm opcodes
- * and the coresponding node constructors for the TEMPLATE assembler irg.
- * $Id$
+ * @file
+ * @brief   This file implements the creation of the achitecture specific firm
+ *          opcodes and the coresponding node constructors for the TEMPLATE
+ *          assembler irg.
+ * @version $Id$
  */
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#ifdef _WIN32
-#include <malloc.h>
-#else
-#include <alloca.h>
-#endif
-
 #include <stdlib.h>
 
 #include "irprog_t.h"
@@ -25,8 +40,9 @@
 #include "firm_common_t.h"
 #include "irvrfy_t.h"
 #include "irprintf.h"
+#include "xmalloc.h"
 
-#include "../bearch.h"
+#include "../bearch_t.h"
 
 #include "TEMPLATE_nodes_attr.h"
 #include "TEMPLATE_new_nodes.h"
  *                       |_|
  ***********************************************************************************/
 
-/**
- * Returns a string containing the names of all registers within the limited bitset
- */
-static char *get_limited_regs(const arch_register_req_t *req, char *buf, int max) {
-       bitset_t *bs   = bitset_alloca(req->cls->n_regs);
-       char     *p    = buf;
-       int       size = 0;
-       int       i, cnt;
-
-       req->limited(NULL, bs);
-
-       for (i = 0; i < req->cls->n_regs; i++) {
-               if (bitset_is_set(bs, i)) {
-                       cnt = snprintf(p, max - size, " %s", req->cls->regs[i].name);
-                       if (cnt < 0) {
-                               fprintf(stderr, "dumper problem, exiting\n");
-                               exit(1);
-                       }
-
-                       p    += cnt;
-                       size += cnt;
-
-                       if (size >= max)
-                               break;
-               }
-       }
-
-       return buf;
-}
-
 /**
  * Dumps the register requirements for either in or out.
  */
-static void dump_reg_req(FILE *F, ir_node *n, const TEMPLATE_register_req_t **reqs, int inout) {
+static void dump_reg_req(FILE *F, ir_node *n, const arch_register_req_t **reqs,
+                         int inout) {
        char *dir = inout ? "out" : "in";
        int   max = inout ? get_TEMPLATE_n_res(n) : get_irn_arity(n);
-       char *buf = alloca(1024);
+       char  buf[1024];
        int   i;
 
-       memset(buf, 0, 1024);
+       memset(buf, 0, sizeof(buf));
 
        if (reqs) {
                for (i = 0; i < max; i++) {
                        fprintf(F, "%sreq #%d =", dir, i);
 
-                       if (reqs[i]->req.type == arch_register_req_type_none) {
+                       if (reqs[i]->type == arch_register_req_type_none) {
                                fprintf(F, " n/a");
                        }
 
-                       if (reqs[i]->req.type & arch_register_req_type_normal) {
-                               fprintf(F, " %s", reqs[i]->req.cls->name);
+                       if (reqs[i]->type & arch_register_req_type_normal) {
+                               fprintf(F, " %s", reqs[i]->cls->name);
                        }
 
-                       if (reqs[i]->req.type & arch_register_req_type_limited) {
-                               fprintf(F, " %s", get_limited_regs(&reqs[i]->req, buf, 1024));
+                       if (reqs[i]->type & arch_register_req_type_limited) {
+                               fprintf(F, " %s",
+                                               arch_register_req_format(buf, sizeof(buf), reqs[i], n));
                        }
 
-                       if (reqs[i]->req.type & arch_register_req_type_should_be_same) {
-                               ir_fprintf(F, " same as %+F", get_irn_n(n, reqs[i]->same_pos));
+                       if (reqs[i]->type & arch_register_req_type_should_be_same) {
+                               const unsigned other = reqs[i]->other_same;
+                               int i;
+
+                               ir_fprintf(F, " same as");
+                               for (i = 0; 1U << i <= other; ++i) {
+                                       if (other & (1U << i)) {
+                                               ir_fprintf(F, " %+F", get_irn_n(n, i));
+                                       }
+                               }
                        }
 
-                       if (reqs[i]->req.type & arch_register_req_type_should_be_different) {
-                               ir_fprintf(F, " different from %+F", get_irn_n(n, reqs[i]->different_pos));
+                       if (reqs[i]->type & arch_register_req_type_must_be_different) {
+                               const unsigned other = reqs[i]->other_different;
+                               int i;
+
+                               ir_fprintf(F, " different from");
+                               for (i = 0; 1U << i <= other; ++i) {
+                                       if (other & (1U << i)) {
+                                               ir_fprintf(F, " %+F", get_irn_n(n, i));
+                                       }
+                               }
                        }
 
                        fprintf(F, "\n");
                }
 
                fprintf(F, "\n");
-       }
-       else {
+       } else {
                fprintf(F, "%sreq = N/A\n", dir);
        }
 }
@@ -132,8 +135,8 @@ static int TEMPLATE_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
        ir_mode     *mode = NULL;
        int          bad  = 0;
        int          i;
-       TEMPLATE_attr_t *attr;
-       const TEMPLATE_register_req_t **reqs;
+       const TEMPLATE_attr_t *attr;
+       const arch_register_req_t **reqs;
        const arch_register_t     **slots;
 
        switch (reason) {
@@ -160,7 +163,7 @@ static int TEMPLATE_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
                        break;
 
                case dump_node_info_txt:
-                       attr = get_TEMPLATE_attr(n);
+                       attr = get_TEMPLATE_attr_const(n);
                        fprintf(F, "=== TEMPLATE attr begin ===\n");
 
                        /* dump IN requirements */
@@ -170,15 +173,15 @@ static int TEMPLATE_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
                        }
 
                        /* dump OUT requirements */
-                       if (attr->n_res > 0) {
+                       if (ARR_LEN(attr->slots) > 0) {
                                reqs = get_TEMPLATE_out_req_all(n);
                                dump_reg_req(F, n, reqs, 1);
                        }
 
                        /* dump assigned registers */
                        slots = get_TEMPLATE_slots(n);
-                       if (slots && attr->n_res > 0) {
-                               for (i = 0; i < attr->n_res; i++) {
+                       if (slots && ARR_LEN(attr->slots) > 0) {
+                               for (i = 0; i < ARR_LEN(attr->slots); i++) {
                                        if (slots[i]) {
                                                fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
                                        }
@@ -234,51 +237,52 @@ static int TEMPLATE_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
  *                                       |___/
  ***************************************************************************************************/
 
-/**
- * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
- * Firm was made by people hating const :-(
- */
-TEMPLATE_attr_t *get_TEMPLATE_attr(const ir_node *node) {
+const TEMPLATE_attr_t *get_TEMPLATE_attr_const(const ir_node *node) {
+       assert(is_TEMPLATE_irn(node) && "need TEMPLATE node to get attributes");
+       return (const TEMPLATE_attr_t *)get_irn_generic_attr_const(node);
+}
+
+TEMPLATE_attr_t *get_TEMPLATE_attr(ir_node *node) {
        assert(is_TEMPLATE_irn(node) && "need TEMPLATE node to get attributes");
-       return (TEMPLATE_attr_t *)get_irn_generic_attr((ir_node *)node);
+       return (TEMPLATE_attr_t *)get_irn_generic_attr(node);
 }
 
 /**
  * Returns the argument register requirements of a TEMPLATE node.
  */
-const TEMPLATE_register_req_t **get_TEMPLATE_in_req_all(const ir_node *node) {
-       TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
+const arch_register_req_t **get_TEMPLATE_in_req_all(const ir_node *node) {
+       const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
        return attr->in_req;
 }
 
 /**
  * Returns the result register requirements of an TEMPLATE node.
  */
-const TEMPLATE_register_req_t **get_TEMPLATE_out_req_all(const ir_node *node) {
-       TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
+const arch_register_req_t **get_TEMPLATE_out_req_all(const ir_node *node) {
+       const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
        return attr->out_req;
 }
 
 /**
  * Returns the argument register requirement at position pos of an TEMPLATE node.
  */
-const TEMPLATE_register_req_t *get_TEMPLATE_in_req(const ir_node *node, int pos) {
-       TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
+const arch_register_req_t *get_TEMPLATE_in_req(const ir_node *node, int pos) {
+       const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
        return attr->in_req[pos];
 }
 
 /**
  * Returns the result register requirement at position pos of an TEMPLATE node.
  */
-const TEMPLATE_register_req_t *get_TEMPLATE_out_req(const ir_node *node, int pos) {
-       TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
+const arch_register_req_t *get_TEMPLATE_out_req(const ir_node *node, int pos) {
+       const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
        return attr->out_req[pos];
 }
 
 /**
  * Sets the OUT register requirements at position pos.
  */
-void set_TEMPLATE_req_out(ir_node *node, const TEMPLATE_register_req_t *req, int pos) {
+void set_TEMPLATE_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
        TEMPLATE_attr_t *attr   = get_TEMPLATE_attr(node);
        attr->out_req[pos] = req;
 }
@@ -286,7 +290,7 @@ void set_TEMPLATE_req_out(ir_node *node, const TEMPLATE_register_req_t *req, int
 /**
  * Sets the IN register requirements at position pos.
  */
-void set_TEMPLATE_req_in(ir_node *node, const TEMPLATE_register_req_t *req, int pos) {
+void set_TEMPLATE_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
        TEMPLATE_attr_t *attr  = get_TEMPLATE_attr(node);
        attr->in_req[pos] = req;
 }
@@ -295,14 +299,14 @@ void set_TEMPLATE_req_in(ir_node *node, const TEMPLATE_register_req_t *req, int
  * Returns the register flag of an TEMPLATE node.
  */
 arch_irn_flags_t get_TEMPLATE_flags(const ir_node *node) {
-       TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
+       const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
        return attr->flags;
 }
 
 /**
  * Sets the register flag of an TEMPLATE node.
  */
-void set_TEMPLATE_flags(const ir_node *node, arch_irn_flags_t flags) {
+void set_TEMPLATE_flags(ir_node *node, arch_irn_flags_t flags) {
        TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
        attr->flags      = flags;
 }
@@ -310,19 +314,27 @@ void set_TEMPLATE_flags(const ir_node *node, arch_irn_flags_t flags) {
 /**
  * Returns the result register slots of an TEMPLATE node.
  */
-const arch_register_t **get_TEMPLATE_slots(const ir_node *node) {
+const arch_register_t **get_TEMPLATE_slots(ir_node *node) {
        TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
        return attr->slots;
 }
 
+/**
+ * Returns the result register slots of an TEMPLATE node.
+ */
+const arch_register_t * const *get_TEMPLATE_slots_const(const ir_node *node) {
+       const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
+       return attr->slots;
+}
+
 /**
  * Returns the name of the OUT register at position pos.
  */
 const char *get_TEMPLATE_out_reg_name(const ir_node *node, int pos) {
-       TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
+       const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
 
        assert(is_TEMPLATE_irn(node) && "Not an TEMPLATE node.");
-       assert(pos < attr->n_res && "Invalid OUT position.");
+       assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
        assert(attr->slots[pos]  && "No register assigned");
 
        return arch_register_get_name(attr->slots[pos]);
@@ -332,10 +344,10 @@ const char *get_TEMPLATE_out_reg_name(const ir_node *node, int pos) {
  * Returns the index of the OUT register at position pos within its register class.
  */
 int get_TEMPLATE_out_regnr(const ir_node *node, int pos) {
-       TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
+       const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
 
        assert(is_TEMPLATE_irn(node) && "Not an TEMPLATE node.");
-       assert(pos < attr->n_res && "Invalid OUT position.");
+       assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
        assert(attr->slots[pos]  && "No register assigned");
 
        return arch_register_get_index(attr->slots[pos]);
@@ -345,32 +357,44 @@ int get_TEMPLATE_out_regnr(const ir_node *node, int pos) {
  * Returns the OUT register at position pos.
  */
 const arch_register_t *get_TEMPLATE_out_reg(const ir_node *node, int pos) {
-       TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
+       const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
 
        assert(is_TEMPLATE_irn(node) && "Not an TEMPLATE node.");
-       assert(pos < attr->n_res && "Invalid OUT position.");
+       assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
        assert(attr->slots[pos]  && "No register assigned");
 
        return attr->slots[pos];
 }
 
 /**
- * Sets the number of results.
+ * Returns the number of results.
  */
-void set_TEMPLATE_n_res(ir_node *node, int n_res) {
-       TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
-       attr->n_res      = n_res;
+int get_TEMPLATE_n_res(const ir_node *node) {
+       const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
+       return ARR_LEN(attr->slots);
 }
 
 /**
- * Returns the number of results.
+ * Initializes the nodes attributes.
  */
-int get_TEMPLATE_n_res(const ir_node *node) {
+void init_TEMPLATE_attributes(ir_node *node, arch_irn_flags_t flags,
+                              const arch_register_req_t **in_reqs,
+                              const arch_register_req_t **out_reqs,
+                              const be_execution_unit_t ***execution_units,
+                              int n_res)
+{
+       ir_graph        *irg  = get_irn_irg(node);
+       struct obstack  *obst = get_irg_obstack(irg);
        TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
-       return attr->n_res;
-}
+       (void) execution_units;
 
+       attr->flags   = flags;
+       attr->out_req = out_reqs;
+       attr->in_req  = in_reqs;
 
+       attr->slots = NEW_ARR_D(const arch_register_t*, obst, n_res);
+       memset(attr->slots, 0, n_res * sizeof(attr->slots[0]));
+}
 
 /***************************************************************************************
  *                  _                            _                   _
@@ -382,5 +406,18 @@ int get_TEMPLATE_n_res(const ir_node *node) {
  *
  ***************************************************************************************/
 
+static
+int TEMPLATE_compare_attr(ir_node *a, ir_node *b)
+{
+       const TEMPLATE_attr_t *attr_a = get_TEMPLATE_attr_const(a);
+       const TEMPLATE_attr_t *attr_b = get_TEMPLATE_attr_const(b);
+
+       if(attr_a->flags != attr_b->flags)
+               return 1;
+
+       return 0;
+}
+
+
 /* Include the generated constructor functions */
 #include "gen_TEMPLATE_new_nodes.c.inl"