update copyright message
[libfirm] / ir / be / bearch.c
index c00de53..f262c35 100644 (file)
-/**
- * Processor architecture specification.
- * @author Sebastian Hack
- * @date 11.2.2005
+/*
+ * 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.
  *
- * $Id$
+ * 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.
+ */
+
+/**
+ * @file
+ * @brief       Processor architecture specification.
+ * @author      Sebastian Hack
+ * @version     $Id$
  */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 
 #include <string.h>
 
 #include "bearch_t.h"
-
-#include "firm_config.h"
-#include "set.h"
-
-#include "entity.h"
 #include "ircons_t.h"
+#include "irnode_t.h"
+#include "xmalloc.h"
 
-#if 1 /* HAVE_ALLOCA_H */
-#include <alloca.h>
-#endif /* HAVE_ALLOCA_H */
+#include "bitset.h"
+#include "pset.h"
+#include "raw_bitset.h"
 
-#define INIT_HEADER(tgt, kind_suffix, a_isa, str) \
-       do { \
-               arch_header_t *h = (arch_header_t *) (tgt); \
-               memset(tgt, 0, sizeof(*(tgt))); \
-               h->kind = arch_kind_ ## kind_suffix; \
-               h->name = new_id_from_str(str); \
-               h->isa = a_isa; \
-       } while(0)
+#include "irprintf.h"
 
-static INLINE int hash_header(const arch_header_t *header)
+/* Initialize the architecture environment struct. */
+arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa_if, FILE *file_handle, be_main_env_t *main_env)
 {
-       int res = HASH_PTR(header->isa);
-       res = 37 * res + HASH_STR(header->name, strlen(header->name));
-       res = 37 * res + header->kind;
-       return res;
+       memset(env, 0, sizeof(*env));
+       env->isa                  = isa_if->init(file_handle);
+       env->isa->main_env        = main_env;
+       return env;
 }
 
-static int cmp_header(const void *a, const void *b, size_t size)
+arch_env_t *arch_env_push_irn_handler(arch_env_t *env,
+                                      const arch_irn_handler_t *handler)
 {
-       const arch_header_t *h1 = a;
-       const arch_header_t *h2 = b;
+       assert(env->handlers_tos < ARCH_MAX_HANDLERS);
+       env->handlers[env->handlers_tos++] = handler;
+       return env;
+}
 
-       return !(h1->kind == h2->kind && strcmp(h1->name, h2->name) == 0);
+const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env)
+{
+       assert(env->handlers_tos > 0 && env->handlers_tos <= ARCH_MAX_HANDLERS);
+       return env->handlers[--env->handlers_tos];
 }
 
-static set *arch_data = NULL;
+static const arch_irn_ops_t *fallback_irn_ops = NULL;
 
-static set *get_arch_data(void)
+int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
 {
-       if(!arch_data)
-               arch_data = new_set(cmp_header, 256);
+       if(bs) {
+               int i, n;
+               for(i = 0, n = cls->n_regs; i < n; ++i)
+                       bitset_set(bs, i);
+       }
 
-       return arch_data;
+       return cls->n_regs;
 }
 
-typedef struct _obj_info_t {
-       const char *name;
-       int listed_in_isa;
-       size_t size;
-} obj_info_t;
-
-static const obj_info_t obj_info[] = {
-#define ARCH_OBJ(name,listed_in_isa)           { #name, listed_in_isa, sizeof(arch_ ## name ## _t) },
-#include "bearch_obj.def"
-#undef ARCH_OBJ
-       { 0 }
-};
-
 /**
- * Insert an arch object to the global arch obj storage.
- *
- * If the object has already been created there, nothing is done and
- * the old object is created.
- *
- * @param kind The kind of the arch object.
- * @param isa The isa the object belongs to or NULL if it is the isa
- * itself.
- * @param name The name of the object.
- * @param was_new A pointer to an int where 1/0 is stored if the
- * object was created or already present. If NULL, it is simply ignored.
- * @return A pointer to the object.
+ * Get the isa responsible for a node.
+ * @param env The arch environment with the isa stack.
+ * @param irn The node to get the responsible isa for.
+ * @return The irn operations given by the responsible isa.
  */
-static INLINE void *_arch_data_insert(arch_kind_t kind, arch_isa_t *isa,
-               const char *name, size_t size, int *was_new)
+static INLINE const arch_irn_ops_t *
+get_irn_ops(const arch_env_t *env, const ir_node *irn)
 {
-       const obj_info_t *info = &obj_info[kind];
-       arch_header_t *data = alloca(size);
-       arch_header_t *res = NULL;
-
-       memset(data, 0, size);
-       data->kind = kind;
-       data->isa = isa;
-       data->name = get_id_str(new_id_from_str(name));
-       data->is_new = 1;
-
-       res = set_insert(get_arch_data(), data, size, hash_header(data));
-
-       /* If the object is newly created and thus not yet present
-        * in the set, add it to the isa */
-       if(res->is_new) {
-
-               /*
-                * The inserted object was no isa, list it in the isa if this is
-                * desired.
-                */
-               if(isa && info->listed_in_isa)
-                       list_add(&res->list, &isa->heads[kind]);
-
-               /* The inserted object is an isa, so initialize all its list heads. */
-               else {
-                       int i;
-                       arch_isa_t *isa = (arch_isa_t *) res;
-
-                       for(i = 0; i < arch_kind_last; ++i)
-                               INIT_LIST_HEAD(&isa->heads[i]);
-               }
-       }
+       int i;
 
-       /*
-        * If the caller wants to know, of the object was newly created,
-        * give it to him.
-        */
-       if(was_new)
-               *was_new = res->is_new;
+       for(i = env->handlers_tos - 1; i >= 0; --i) {
+               const arch_irn_handler_t *handler = env->handlers[i];
+               const arch_irn_ops_t *ops = handler->get_irn_ops(handler, irn);
 
-       /* Mark the object as NOT new. */
-       res->is_new = 0;
+               if(ops)
+                       return ops;
+       }
 
-       return res;
+       return fallback_irn_ops;
 }
 
-#define arch_data_insert(type_suffix, isa, name, was_new) \
-       _arch_data_insert(arch_kind_ ## type_suffix, isa, name, sizeof(arch_ ## type_suffix ## _t), was_new)
+const arch_irn_ops_t *arch_get_irn_ops(const arch_env_t *env, const ir_node *irn) {
+       return get_irn_ops(env, irn);
+}
 
-static INLINE void *_arch_data_find(arch_kind_t kind, const arch_isa_t *isa, const char *name)
+const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
+                                                 const ir_node *irn, int pos)
 {
-       arch_header_t header;
-
-       memset(&header, 0, sizeof(header));
-       header.kind = kind;
-       header.isa = (arch_isa_t *) isa;
-       header.name = name;
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
+       return ops->impl->get_irn_reg_req(ops, irn, pos);
+}
 
-       return set_find(get_arch_data(), &header, sizeof(header), hash_header(&header));
+void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
+{
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
+       ops->impl->set_frame_offset(ops, irn, offset);
 }
 
-#define arch_data_find(type_suffix, isa, name) \
-       _arch_data_find(arch_kind_ ## type_suffix, isa, name)
+ir_entity *arch_get_frame_entity(const arch_env_t *env, const ir_node *irn)
+{
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
+       return ops->impl->get_frame_entity(ops, irn);
+}
 
-arch_isa_t *arch_add_isa(const char *name)
+void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent)
 {
-       return arch_data_insert(isa, NULL, name, NULL);
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
+       ops->impl->set_frame_entity(ops, irn, ent);
 }
 
-arch_register_class_t *arch_add_register_class(arch_isa_t *isa, const char *name, int n_regs)
+int arch_get_sp_bias(const arch_env_t *env, ir_node *irn)
 {
-       arch_register_class_t *cls =
-               _arch_data_insert(arch_kind_register_class, isa, name,
-                               sizeof(arch_register_class_t) + n_regs * sizeof(arch_register_t *), NULL);
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
+       return ops->impl->get_sp_bias(ops, irn);
+}
 
-       cls->n_regs = n_regs;
+arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
+{
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
 
-       return cls;
+       if(ops->impl->get_inverse) {
+               return ops->impl->get_inverse(ops, irn, i, inverse, obstack);
+       } else {
+               return NULL;
+       }
 }
 
-arch_register_t *arch_add_register(arch_register_class_t *cls, int index, const char *name)
-{
-       arch_register_t *reg = NULL;
+int arch_possible_memory_operand(const arch_env_t *env, const ir_node *irn, unsigned int i) {
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
 
-       assert(index >= 0 && index < cls->n_regs);
-       reg = _arch_data_insert(arch_kind_register, arch_obj_get_isa(cls), name,
-                       sizeof(arch_register_t), NULL);
-       cls->regs[index] = reg;
+       if(ops->impl->possible_memory_operand) {
+               return ops->impl->possible_memory_operand(ops, irn, i);
+       } else {
+               return 0;
+       }
+}
 
-       reg->index = index;
-       reg->reg_class = cls;
-       reg->flags = arch_register_flag_none;
+void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i) {
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
 
-       return reg;
+       if(ops->impl->perform_memory_operand) {
+               ops->impl->perform_memory_operand(ops, irn, spill, i);
+       } else {
+               return;
+       }
 }
 
-arch_immediate_t *arch_add_immediate(arch_isa_t *isa, const char *name, ir_mode *mode)
+int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
 {
-       arch_immediate_t *imm = arch_data_insert(immediate, isa, name, NULL);
-       imm->mode = mode;
-       return imm;
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
+
+       if(ops->impl->get_op_estimated_cost) {
+               return ops->impl->get_op_estimated_cost(ops, irn);
+       } else {
+               return 1;
+       }
 }
 
-static const size_t operand_sizes[] = {
-       0,
-       0,
-       sizeof(entity *),
-       sizeof(arch_register_t *),
-       sizeof(tarval *)
-};
+int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
+{
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
 
-arch_insn_format_t *arch_add_insn_format(arch_isa_t *isa, const char *name, int n_in, int n_out)
+       if(ops->impl->possible_memory_operand) {
+               return ops->impl->possible_memory_operand(ops, irn, i);
+       } else {
+               return 0;
+       }
+}
+
+int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
 {
-       int i;
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
+       const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
 
-       arch_insn_format_t *fmt =
-               _arch_data_insert(arch_kind_insn_format, isa, name,
-                               sizeof(arch_insn_format_t) + (n_in + n_out) * sizeof(arch_operand_type_t), NULL);
-
-       fmt->n_in = n_in;
-       fmt->n_out = n_out;
-       fmt->irn_data_size = 0;
-
-       /*
-        * Compute the number of bytes which must be extra allocated if this
-        * opcode is instantiated.
-        */
-       for(i = 0; i < fmt->n_in; ++i) {
-               arch_operand_t *op = arch_get_in_operand(fmt, i);
-               op->offset_in_irn_data = fmt->irn_data_size;
-               fmt->irn_data_size += operand_sizes[op->type];
+       if(req->type == arch_register_req_type_none) {
+               bitset_clear_all(bs);
+               return 0;
        }
 
-       if(fmt->n_out == 1) {
-               arch_operand_t *op = arch_get_in_operand(fmt, i);
-               op->offset_in_irn_data = fmt->irn_data_size;
-               fmt->irn_data_size += operand_sizes[op->type];
+       if(arch_register_req_is(req, limited)) {
+               rbitset_copy_to_bitset(req->limited, bs);
+               return bitset_popcnt(bs);
        }
 
-       return fmt;
+       arch_register_class_put(req->cls, bs);
+       return req->cls->n_regs;
 }
 
-arch_insn_t *arch_add_insn(arch_insn_format_t *fmt, const char *name)
+void arch_put_non_ignore_regs(const arch_env_t *env,
+                              const arch_register_class_t *cls, bitset_t *bs)
 {
-       /* Insert the insn into the isa. */
-       arch_insn_t *insn = arch_data_insert(insn, arch_obj_get_isa(fmt), name, NULL);
+       unsigned i;
+       (void) env;
 
-       insn->format = fmt;
-       insn->op = new_ir_op(get_next_ir_opcode(), name, op_pin_state_pinned, 0,
-                       oparity_dynamic, 0, sizeof(arch_irn_data_t) + fmt->irn_data_size);
+       for(i = 0; i < cls->n_regs; ++i) {
+               if(!arch_register_type_is(&cls->regs[i], ignore))
+                       bitset_set(bs, i);
+       }
+}
 
-       return insn;
+int arch_count_non_ignore_regs(const arch_env_t *env,
+                               const arch_register_class_t *cls)
+{
+       unsigned i;
+       int result = 0;
+       (void) env;
+
+       for(i = 0; i < cls->n_regs; ++i) {
+               if(!arch_register_type_is(&cls->regs[i], ignore))
+                       result++;
+       }
+
+       return result;
 }
 
-arch_insn_format_t *arch_find_insn_format(const arch_isa_t *isa, const char *name)
+int arch_is_register_operand(const arch_env_t *env,
+    const ir_node *irn, int pos)
 {
-       return arch_data_find(insn_format, isa, name);
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
+       const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
+
+       return req != NULL;
 }
 
-arch_isa_t *arch_find_isa(const char *name)
+int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
+    int pos, const arch_register_t *reg)
 {
-       return arch_data_find(isa, NULL, name);
+       const arch_register_req_t *req;
+
+       req = arch_get_register_req(env, irn, pos);
+
+       if(req->type == arch_register_req_type_none)
+               return 0;
+
+       if(arch_register_req_is(req, limited)) {
+               assert(arch_register_get_class(reg) == req->cls);
+               return rbitset_is_set(req->limited, arch_register_get_index(reg));
+       }
+
+       return req->cls == reg->reg_class;
 }
 
-arch_insn_t *arch_find_insn(const arch_isa_t *isa, const char *name)
+const arch_register_class_t *
+arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
 {
-       return arch_data_find(insn, isa, name);
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
+       const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
+
+       assert(req->type != arch_register_req_type_none || req->cls == NULL);
+
+       return req->cls;
 }
 
-arch_register_class_t *arch_find_register_class_t(arch_isa_t *isa, const char *name)
+extern const arch_register_t *
+arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
 {
-       return arch_data_find(register_class, isa, name);
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
+       return ops->impl->get_irn_reg(ops, irn);
 }
 
-arch_register_set_t *arch_get_register_set_for_class(arch_register_class_t *cls)
+extern void arch_set_irn_register(const arch_env_t *env,
+    ir_node *irn, const arch_register_t *reg)
 {
-       return _arch_get_register_set_for_class(cls);
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
+       ops->impl->set_irn_reg(ops, irn, reg);
 }
 
-ir_node *arch_new_node(const arch_insn_t *insn, ir_graph *irg, ir_node *block,
-               ir_mode *mode, int arity, ir_node **in)
+extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
 {
-       ir_node *irn = new_ir_node(NULL, irg, block, insn->op, mode, arity, in);
-       arch_irn_data_t *data = (void *) &irn->attr;
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
+       return ops->impl->classify(ops, irn);
+}
 
-       data->magic = ARCH_IRN_FOURCC;
-       data->insn = insn;
+extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
+{
+       const arch_irn_ops_t *ops = get_irn_ops(env, irn);
+       return ops->impl->get_flags(ops, irn);
+}
 
-       return irn;
+extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
+{
+       switch(fl) {
+#define XXX(x) case arch_irn_flags_ ## x: return #x;
+               XXX(dont_spill);
+               XXX(ignore);
+               XXX(rematerializable);
+               XXX(modify_sp);
+               XXX(modify_flags);
+               XXX(none);
+#undef XXX
+       }
+       return "n/a";
 }
 
-ir_node *arch_new_node_bare(const arch_insn_t *insn, ir_graph *irg, int arity)
+extern char *arch_register_req_format(char *buf, size_t len,
+                                      const arch_register_req_t *req,
+                                      const ir_node *node)
 {
-       int i;
-       ir_node **in = alloca(sizeof(in[0]) * arity);
+       char tmp[128];
+       snprintf(buf, len, "class: %s", req->cls->name);
+
+       if(arch_register_req_is(req, limited)) {
+               unsigned n_regs = req->cls->n_regs;
+               unsigned i;
+
+               strncat(buf, " limited:", len);
+               for(i = 0; i < n_regs; ++i) {
+                       if(rbitset_is_set(req->limited, i)) {
+                               const arch_register_t *reg = &req->cls->regs[i];
+                               strncat(buf, " ", len);
+                               strncat(buf, reg->name, len);
+                       }
+               }
+       }
 
-       for(i = 0; i < arity; ++i)
-               in[i] = new_Unknown(mode_Is);
+       if(arch_register_req_is(req, should_be_same)) {
+               const unsigned other = req->other_same;
+               int i;
 
-       return arch_new_node(insn, irg, new_Unknown(mode_BB), mode_Is, arity, in);
-}
+               ir_snprintf(tmp, sizeof(tmp), " same to:");
+               for (i = 0; 1U << i <= other; ++i) {
+                       if (other & (1U << i)) {
+                               ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
+                               strncat(buf, tmp, len);
+                       }
+               }
+       }
 
-ir_mode *arch_get_unknown_mode(void)
-{
-       return mode_Is;
+       if(arch_register_req_is(req, should_be_different)) {
+               const unsigned other = req->other_different;
+               int i;
+
+               ir_snprintf(tmp, sizeof(tmp), " different from:");
+               for (i = 0; 1U << i <= other; ++i) {
+                       if (other & (1U << i)) {
+                               ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
+                               strncat(buf, tmp, len);
+                       }
+               }
+       }
+
+       return buf;
 }
+
+static const arch_register_req_t no_requirement = {
+       arch_register_req_type_none,
+       NULL,
+       NULL,
+       0,
+       0
+};
+const arch_register_req_t *arch_no_register_req = &no_requirement;