Normalization puts constants on teh right side of commutative nodes.
[libfirm] / ir / ir / irreflect.c
index 8bf4c38..0b6633a 100644 (file)
@@ -1,15 +1,35 @@
-/**
- * @file irreflect.c
- * @date 9.9.2004
- * @author Sebastian Hack
- * @brief Reflection for Firm operands.
+/*
+ * Copyright (C) 1995-2007 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.
  *
- * $Id$
+ * 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   Reflection for Firm operands.
+ * @author  Sebastian Hack
+ * @date    9.9.2004
+ * @version $Id$
  */
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
+#include <assert.h>
+
 #ifdef HAVE_STDLIB_H
 # include <stdlib.h>
 #endif
 
 #include "obst.h"
 
+#include "irnode_t.h"
 #include "irmode.h"
 #include "irreflect.h"
 
 #define obstack_grow_str(obst,s) obstack_grow((obst), (s), strlen((s)))
 #define obstack_grow_str_const(obst,s) obstack_grow((obst), (s), sizeof((s)))
 
-extern int obstack_printf(struct obstack *obst, const char *fmt, ...);
-
-#define INLINE inline
-
 /**
  * Get the number of bits set in a word.
  */
@@ -62,11 +79,11 @@ typedef struct {
 } rflct_args_t;
 
 typedef struct {
-       opcode opc;
+       ir_opcode opc;
        const char *name;
-       bool commutative;
+       int commutative;
        int sig_count;
-       const rflct_arg_t *sigs[MAX_SIG_COUNT];
+       rflct_arg_t *sigs[MAX_SIG_COUNT];
 } rflct_opcode_t;
 
 static struct obstack obst;
@@ -79,9 +96,7 @@ static INLINE void assure_opcode_capacity(int opcode)
 {
        if(opcode >= opcodes_size) {
                int new_size = 2 * opcode;
-               rflct_opcode_t **new_opcodes = xmalloc(sizeof(new_opcodes[0]) * new_size);
-
-               memset(new_opcodes, 0, sizeof(*new_opcodes) * new_size);
+               rflct_opcode_t **new_opcodes = xcalloc(new_size, sizeof(new_opcodes[0]));
 
                if(opcodes != NULL) {
                        memcpy(new_opcodes, opcodes, sizeof(*opcodes) * opcodes_size);
@@ -94,7 +109,10 @@ static INLINE void assure_opcode_capacity(int opcode)
 }
 
 
+#if 0
 #define OPCODES_COUNT (sizeof(opcodes) / sizeof(opcodes[0]))
+#endif
+#define OPCODES_COUNT opcodes_size
 
 
 rflct_mode_class_t rflct_get_mode_class(const ir_mode *mode) {
@@ -124,12 +142,12 @@ rflct_mode_class_t rflct_get_mode_class(const ir_mode *mode) {
        return RFLCT_MC(None);
 }
 
-static INLINE const rflct_opcode_t *get_opcode(opcode opc) {
+static INLINE const rflct_opcode_t *get_opcode(ir_opcode opc) {
        assert(opc >= 0 && opc < OPCODES_COUNT && "Invalid opcode");
        return opcodes[opc];
 }
 
-static INLINE const rflct_arg_t *get_args(opcode opc, int sig) {
+static INLINE const rflct_arg_t *get_args(ir_opcode opc, int sig) {
        const rflct_opcode_t *opcode = get_opcode(opc);
        assert(sig >= 0 && sig < opcode->sig_count
                        && "Invalid signature");
@@ -139,12 +157,12 @@ static INLINE const rflct_arg_t *get_args(opcode opc, int sig) {
 #define GET_OPCODE(opc) get_opcode(opc)
 #define GET_ARGS(opc,args) get_args(opc, args)
 
-int rflct_get_signature_count(opcode opc) {
+int rflct_get_signature_count(ir_opcode opc) {
        const rflct_opcode_t *opcode = GET_OPCODE(opc);
        return opcode->sig_count;
 }
 
-int rflct_get_in_args_count(opcode opc, int sig) {
+int rflct_get_in_args_count(ir_opcode opc, int sig) {
        const rflct_arg_t *args = GET_ARGS(opc, sig);
        int res = 0, i = 0;
 
@@ -153,7 +171,7 @@ int rflct_get_in_args_count(opcode opc, int sig) {
        return res;
 }
 
-int rflct_get_out_args_count(opcode opc, int sig) {
+int rflct_get_out_args_count(ir_opcode opc, int sig) {
        const rflct_arg_t *args = GET_ARGS(opc, sig);
        int i = 0;
        for(i = 0; args[i].name != NULL; i++);
@@ -161,7 +179,7 @@ int rflct_get_out_args_count(opcode opc, int sig) {
 }
 
 
-const rflct_arg_t *rflct_get_in_args(opcode opc, int sig) {
+const rflct_arg_t *rflct_get_in_args(ir_opcode opc, int sig) {
        const rflct_arg_t *args = GET_ARGS(opc, sig);
        int i;
 
@@ -169,12 +187,12 @@ const rflct_arg_t *rflct_get_in_args(opcode opc, int sig) {
        return &args[i + 1];
 }
 
-const rflct_arg_t *rflct_get_out_args(opcode opc, int sig) {
+const rflct_arg_t *rflct_get_out_args(ir_opcode opc, int sig) {
        return GET_ARGS(opc, sig);
 }
 
-int rflct_signature_match(ir_node *irn, int sig) {
-       opcode op = get_irn_opcode(irn);
+int rflct_signature_match(const ir_node *irn, int sig) {
+       ir_opcode op = get_irn_opcode(irn);
        const rflct_arg_t *args = rflct_get_in_args(op, sig);
        int dst = 0;
        int i, j;
@@ -198,7 +216,7 @@ int rflct_signature_match(ir_node *irn, int sig) {
        return dst;
 }
 
-int rflct_get_signature(ir_node *irn) {
+int rflct_get_signature(const ir_node *irn) {
        const rflct_opcode_t *opc = GET_OPCODE(get_irn_opcode(irn));
        int min_dist = INT_MAX;
        int min_sig = INT_MAX;
@@ -300,7 +318,7 @@ static void rflct_obstack_grow_args(struct obstack *obst,
 
 }
 
-char *rflct_to_string(char *buf, int n, opcode opc, int sig) {
+char *rflct_to_string(char *buf, int n, ir_opcode opc, int sig) {
        struct obstack obst;
        char *s;
        const rflct_opcode_t *opcode = GET_OPCODE(opc);
@@ -325,23 +343,26 @@ char *rflct_to_string(char *buf, int n, opcode opc, int sig) {
        return buf;
 }
 
+#define NON_VARIADIC 0
+#define VARIADIC     1
+
 #define ARG(name,modes) \
-_ARG(name, modes, false, -1)
+_ARG(name, modes, NON_VARIADIC, -1)
 
 #define ARG_SAME(name,modes,mode_same) \
-_ARG(name, modes, false, mode_same)
+_ARG(name, modes, NON_VARIADIC, mode_same)
 
 #define VARG(name,modes) \
-_ARG(name, modes, true, 0)
+_ARG(name, modes, VARIADIC, 0)
 
 #define VARG_SAME(name,modes) \
-_ARG(name, modes, true, 1)
+_ARG(name, modes, VARIADIC, 1)
 
 #define MARK \
-_ARG(NULL, None, false, -1)
+_ARG(NULL, None, NON_VARIADIC, -1)
 
 #define FINISH \
-_ARG(NULL, None, false, 0)
+_ARG(NULL, None, NON_VARIADIC, 0)
 
 #define BLOCK ARG("Block", BB)
 
@@ -400,7 +421,7 @@ arg->name = _name; \
        arg->is_variadic = _var; \
        arg->mode_equals = _me;
 
-void rflct_new_opcode(opcode opc, const char *name, bool commutative)
+void rflct_new_opcode(ir_opcode opc, const char *name, int commutative)
 {
        rflct_opcode_t *ropc = obstack_alloc(&obst, sizeof(*ropc));
 
@@ -413,9 +434,9 @@ void rflct_new_opcode(opcode opc, const char *name, bool commutative)
        opcodes[opc] = ropc;
 }
 
-bool rflct_opcode_add_signature(opcode opc, rflct_sig_t *sig)
+int rflct_opcode_add_signature(ir_opcode opc, rflct_sig_t *sig)
 {
-       const rflct_arg_t *args = sig->args;
+       rflct_arg_t *args = sig->args;
        rflct_opcode_t *op = opcodes[opc];
        int i;
 
@@ -424,12 +445,12 @@ bool rflct_opcode_add_signature(opcode opc, rflct_sig_t *sig)
        for(i = 0; i < MAX_SIG_COUNT && op->sigs[i] != NULL; i++);
 
        if(i >= MAX_SIG_COUNT)
-               return false;
+               return 0;
 
        op->sigs[op->sig_count++] = args;
 
        free(sig);
-       return true;
+       return 1;
 }
 
 
@@ -453,7 +474,7 @@ rflct_sig_t *rflct_signature_allocate(int defs, int uses)
        return sig;
 }
 
-int rflct_signature_get_index(const rflct_sig_t *sig, bool is_use, int num)
+int rflct_signature_get_index(const rflct_sig_t *sig, int is_use, int num)
 {
        return is_use ? num + sig->defs + 1 : num;
 }
@@ -465,8 +486,8 @@ arg->name = _name; \
        arg->is_variadic = _var; \
        arg->mode_equals = _me;
 
-int rflct_signature_set_arg(rflct_sig_t *sig, bool is_use, int num,
-               const char *name, rflct_mode_class_t mc, bool is_variadic, int mode_equals)
+int rflct_signature_set_arg(rflct_sig_t *sig, int is_use, int num,
+               const char *name, rflct_mode_class_t mc, int is_variadic, int mode_equals)
 {
        int index = rflct_signature_get_index(sig, is_use, num);
        rflct_arg_t *arg = sig->args + index;
@@ -475,6 +496,9 @@ int rflct_signature_set_arg(rflct_sig_t *sig, bool is_use, int num,
 }
 
 
-void init_rflct(void) {
+void firm_init_rflct(void) {
        init_ops();
 }
+
+#undef VARIADIC
+#undef NON_VARIADIC