-/**
- * 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.
*/
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-#ifdef HAVE_ALLOCA_H
-#include <alloca.h>
-#endif
-#ifdef HAVE_MALLOC_H
-#include <malloc.h>
-#endif
+/**
+ * @file
+ * @brief Processor architecture specification.
+ * @author Sebastian Hack
+ */
+#include "config.h"
#include <string.h>
#include "bearch.h"
+#include "benode.h"
+#include "beinfo.h"
+#include "ircons_t.h"
+#include "irnode_t.h"
+#include "irop_t.h"
+#include "bitset.h"
#include "pset.h"
+#include "raw_bitset.h"
-#include "entity.h"
-#include "ircons_t.h"
-
-/* Needed for obstack copy */
-#define bcopy(src,dst,n) memcpy(dst,src,n)
+#include "irprintf.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)
+static const arch_register_req_t no_requirement = {
+ arch_register_req_type_none,
+ NULL,
+ NULL,
+ 0,
+ 0,
+ 0
+};
+const arch_register_req_t *arch_no_register_req = &no_requirement;
-static INLINE int hash_header(const arch_header_t *header)
-{
- int res = HASH_PTR(header->isa);
- res = 37 * res + HASH_STR(header->name, strlen(header->name));
- res = 37 * res + header->kind;
- return res;
-}
+static reg_out_info_t dummy_info = {
+ NULL,
+ &no_requirement
+};
-static int cmp_header(const void *a, const void *b)
+/* Initialize the architecture environment struct. */
+arch_env_t *arch_env_begin_codegeneration(const arch_isa_if_t *isa_if,
+ be_main_env_t *main_env)
{
- const arch_header_t *h1 = a;
- const arch_header_t *h2 = b;
-
- return !(h1->kind == h2->kind && h1->isa == h2->isa && strcmp(h1->name, h2->name) == 0);
+ arch_env_t *arch_env = isa_if->begin_codegeneration(main_env);
+ arch_env->main_env = main_env;
+ return arch_env;
}
/**
- * The obstack and pset where the arch data is stored.
+ * Get the isa responsible for a node.
+ * @param irn The node to get the responsible isa for.
+ * @return The irn operations given by the responsible isa.
*/
-typedef struct _arch_data_t {
- struct obstack obst; /**< Here is the data allocated. */
- pset *header_set; /**< Here reside copies of the headers. */
-} arch_data_t;
-
-/**
- * Get the storage (obstack and pset) for the arch objects.
- * @return A struct containing both, the obst and pset where
- * the objects are allocated and their pointer are recorded.
- */
-static INLINE arch_data_t *get_arch_data(void)
+static const arch_irn_ops_t *get_irn_ops(const ir_node *irn)
{
- static arch_data_t arch_data;
- static int inited = 0;
+ const ir_op *ops;
+ const arch_irn_ops_t *be_ops;
- if(!inited) {
- obstack_init(&arch_data.obst);
- arch_data.header_set = new_pset(cmp_header, 512);
- inited = 1;
+ if (is_Proj(irn)) {
+ irn = get_Proj_pred(irn);
+ assert(!is_Proj(irn));
}
- return &arch_data;
-}
+ ops = get_irn_op(irn);
+ be_ops = get_op_ops(ops)->be_ops;
-/**
- * Dump all arch objects in the arch_data collection.
- */
-static void dump_arch_data(void)
-{
- void *p;
- arch_data_t *d = get_arch_data();
- static const char *kind_names[] = {
-#define ARCH_OBJ(name,in_list) #name,
-#include "bearch_obj.def"
-#undef ARCH_OBJ
- ""
- };
-
- printf("arch set:\n");
- for(p = pset_first(d->header_set); p; p = pset_next(d->header_set)) {
- arch_header_t *header = p;
- printf("%20s %10s %10s\n", kind_names[header->kind], header->name,
- header->isa ? header->isa->header.name : "");
- }
+ return be_ops;
}
-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.
- * @return A pointer to the object.
- */
-static INLINE void *_arch_data_insert(arch_kind_t kind, arch_isa_t *isa,
- const char *name, size_t size)
+void arch_set_frame_offset(ir_node *irn, int offset)
{
- arch_data_t *ad = get_arch_data();
- const obj_info_t *info = &obj_info[kind];
- arch_header_t *header = obstack_alloc(&ad->obst, size);
- arch_header_t *res = NULL;
-
- memset(header, 0, size);
- header->name = name;
- header->kind = kind;
- header->isa = isa;
- header->is_new = 1;
-
- res = pset_insert(ad->header_set, header, hash_header(header));
-
- /*
- * If the object is newly created and thus not yet present
- * in the set, add it to the isa
- * The inserted object was no isa, list it in the isa if this is
- * desired.
- */
- if(res->is_new && isa && info->listed_in_isa) {
- list_add(&res->list, &isa->heads[kind]);
- }
-
- /* if it was in the set, remove it from the obstack */
- if(!res->is_new)
- obstack_free(&ad->obst, header);
-
- /* Mark the object as NOT new. */
- res->is_new = 0;
-
- return res;
+ const arch_irn_ops_t *ops = get_irn_ops(irn);
+ ops->set_frame_offset(irn, offset);
}
-#define arch_data_insert(type_suffix, isa, name) \
- _arch_data_insert(arch_kind_ ## type_suffix, isa, name, sizeof(arch_ ## type_suffix ## _t))
-
-static INLINE void *_arch_data_find(arch_kind_t kind, const arch_isa_t *isa, const char *name)
+ir_entity *arch_get_frame_entity(const ir_node *irn)
{
- arch_header_t header;
-
- header.kind = kind;
- header.isa = (arch_isa_t *) isa;
- header.name = name;
-
- return pset_find(get_arch_data()->header_set, &header, hash_header(&header));
+ const arch_irn_ops_t *ops = get_irn_ops(irn);
+ return ops->get_frame_entity(irn);
}
-#define arch_data_find(type_suffix, isa, name) \
- _arch_data_find(arch_kind_ ## type_suffix, isa, name)
-
-arch_isa_t *arch_add_isa(const char *name)
+int arch_get_sp_bias(ir_node *irn)
{
- arch_isa_t *isa;
- int i;
-
- isa = arch_data_insert(isa, NULL, name);
- for(i = 0; i < arch_kind_last; ++i)
- INIT_LIST_HEAD(&isa->heads[i]);
-
- return isa;
+ const arch_irn_ops_t *ops = get_irn_ops(irn);
+ return ops->get_sp_bias(irn);
}
-arch_register_set_t *arch_add_register_set(arch_isa_t *isa,
- const arch_register_class_t *cls, const char *name)
+arch_inverse_t *arch_get_inverse(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
{
- arch_register_set_t *set =
- _arch_data_insert(arch_kind_register_set, isa, name,
- sizeof(arch_register_set_t) + cls->n_regs * sizeof(set->regs[0]));
-
- set->reg_class = cls;
- memset(set->regs, 0, sizeof(set->regs[0]) * cls->n_regs);
+ const arch_irn_ops_t *ops = get_irn_ops(irn);
- return set;
+ if (ops->get_inverse) {
+ return ops->get_inverse(irn, i, inverse, obstack);
+ } else {
+ return NULL;
+ }
}
-arch_register_class_t *arch_add_register_class(arch_isa_t *isa, const char *name, int n_regs)
+int arch_possible_memory_operand(const ir_node *irn, unsigned int i)
{
- char buf[64];
- char *set_name;
- int i, n;
+ const arch_irn_ops_t *ops = get_irn_ops(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 *));
-
- /* Make a name for the set contianing all regs in this class. */
- n = snprintf(buf, sizeof(buf), "%s$set", name);
- set_name = obstack_copy(&get_arch_data()->obst, buf, n);
-
- cls->n_regs = n_regs;
-
- /* make the set of all registers in this class */
- cls->set = arch_add_register_set(isa, cls, name);
-
- /* Add each register in this class to the set */
- for(i = 0; i < n_regs; ++i)
- cls->set->regs[i] = 1;
-
- return cls;
-}
-
-void arch_register_set_add_register(arch_register_set_t *set, int index)
-{
- assert(index >= 0 && index < set->reg_class->n_regs);
- set->regs[index] = 1;
+ if (ops->possible_memory_operand) {
+ return ops->possible_memory_operand(irn, i);
+ } else {
+ return 0;
+ }
}
-arch_register_t *arch_add_register(arch_register_class_t *cls, int index, const char *name)
+void arch_perform_memory_operand(ir_node *irn, ir_node *spill, unsigned int i)
{
- arch_register_t *reg = NULL;
-
- assert(index >= 0 && index < cls->n_regs);
- reg = _arch_data_insert(arch_kind_register, arch_obj_get_isa(cls), name,
- sizeof(arch_register_t));
- cls->regs[index] = reg;
+ const arch_irn_ops_t *ops = get_irn_ops(irn);
- reg->index = index;
- reg->reg_class = cls;
- reg->flags = arch_register_flag_none;
-
- return reg;
+ if (ops->perform_memory_operand) {
+ ops->perform_memory_operand(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 ir_node *irn)
{
- arch_immediate_t *imm = arch_data_insert(immediate, isa, name);
- imm->mode = mode;
- return imm;
-}
+ const arch_irn_ops_t *ops = get_irn_ops(irn);
-/*
- * Size of each operand type which should be allocated in an irn.
- * Keep this list up to date with the arch_operand_type_t enum.
- */
-static const size_t operand_sizes[] = {
-#define ARCH_OPERAND_TYPE(name,size_in_irn) size_in_irn,
-#include "bearch_operand_types.def"
-#undef ARCH_OPERAND_TYPE
- 0
-};
-
-/**
- * Determine the amount of bytes which has to be extra allocated when a
- * new ir node is made from a insn format.
- * This size depends on the operands specified in the insn format.
- * @param fmt The instruction format.
- * @return The number of bytes which the operands of an instruction
- * will need in an ir node.
- */
-static INLINE int arch_get_operands_size(const arch_insn_format_t *fmt)
-{
- int i, res = 0;
-
- for(i = 0; i < fmt->n_in + fmt->n_out; ++i) {
- arch_operand_type_t type = fmt->operands[i].type;
-
- assert(type > arch_operand_type_invalid && type < arch_operand_type_last);
- res += operand_sizes[type];
+ if (ops->get_op_estimated_cost) {
+ return ops->get_op_estimated_cost(irn);
+ } else {
+ return 1;
}
-
- return res;
}
-arch_insn_format_t *arch_add_insn_format(arch_isa_t *isa, const char *name, int n_in, int n_out)
+static reg_out_info_t *get_out_info(const ir_node *node)
{
- int i;
-
- 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_t));
-
- fmt->n_in = n_in;
- fmt->n_out = n_out;
-
- /* initialize each operand with invalid. */
- for(i = 0; i < n_in + n_out; ++i)
- fmt->operands[i].type = arch_operand_type_invalid;
+ size_t pos = 0;
+ const backend_info_t *info;
+ assert(get_irn_mode(node) != mode_T);
+ if (is_Proj(node)) {
+ pos = get_Proj_proj(node);
+ node = get_Proj_pred(node);
+ }
- return fmt;
+ info = be_get_info(node);
+ /* We have a problem with the switch-node where there can be arbitrary
+ * Proj-numbers, so we can't easily allocate an array big-enough to hold
+ * all of them. So until we rewrite Switch-nodes we need this special case
+ */
+ if (info->out_infos == NULL)
+ return &dummy_info;
+ assert(pos < ARR_LEN(info->out_infos));
+ return &info->out_infos[pos];
}
-arch_insn_t *arch_add_insn(arch_insn_format_t *fmt, const char *name)
+static reg_out_info_t *get_out_info_n(const ir_node *node, int pos)
{
- /* Get the size the operands will need in the irn. */
- int operands_size = arch_get_operands_size(fmt);
-
- /* Insert the insn into the isa. */
- arch_insn_t *insn = arch_data_insert(insn, arch_obj_get_isa(fmt), name);
-
- 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) + operands_size);
-
- return insn;
+ const backend_info_t *info = be_get_info(node);
+ assert(!is_Proj(node));
+ assert(pos >= 0 && pos < (int)ARR_LEN(info->out_infos));
+ return &info->out_infos[pos];
}
-arch_insn_format_t *arch_find_insn_format(const arch_isa_t *isa, const char *name)
-{
- return arch_data_find(insn_format, isa, name);
-}
-arch_isa_t *arch_find_isa(const char *name)
+const arch_register_t *arch_get_irn_register(const ir_node *node)
{
- return arch_data_find(isa, NULL, name);
+ const reg_out_info_t *out = get_out_info(node);
+ return out->reg;
}
-arch_insn_t *arch_find_insn(const arch_isa_t *isa, const char *name)
+const arch_register_t *arch_get_irn_register_out(const ir_node *node, int pos)
{
- return arch_data_find(insn, isa, name);
+ const reg_out_info_t *out = get_out_info_n(node, pos);
+ return out->reg;
}
-arch_immediate_t *arch_find_immediate(const arch_isa_t *isa, const char *name)
+const arch_register_t *arch_get_irn_register_in(const ir_node *node, int pos)
{
- return arch_data_find(immediate, isa, name);
+ ir_node *op = get_irn_n(node, pos);
+ return arch_get_irn_register(op);
}
-arch_register_class_t *arch_find_register_class(const arch_isa_t *isa, const char *name)
+void arch_set_irn_register_out(ir_node *node, int pos,
+ const arch_register_t *reg)
{
- return arch_data_find(register_class, isa, name);
+ reg_out_info_t *out = get_out_info_n(node, pos);
+ out->reg = reg;
}
-arch_register_set_t *arch_find_register_set(const arch_isa_t *isa, const char *name)
+void arch_set_irn_register(ir_node *node, const arch_register_t *reg)
{
- return arch_data_find(register_set, isa, name);
+ reg_out_info_t *out = get_out_info(node);
+ out->reg = reg;
}
-arch_register_set_t *arch_get_register_set_for_class(arch_register_class_t *cls)
+const arch_register_req_t *arch_get_irn_register_req(const ir_node *node)
{
- return _arch_get_register_set_for_class(cls);
+ reg_out_info_t *out = get_out_info(node);
+ return out->req;
}
-static INLINE arch_operand_t *_arch_set_operand(arch_insn_format_t *fmt, int pos,
- arch_operand_type_t type)
+arch_irn_flags_t arch_get_irn_flags(const ir_node *node)
{
- arch_operand_t *operand;
- int ofs = arch_inout_to_index(fmt, pos);
-
- assert(ofs < fmt->n_in + fmt->n_out);
+ backend_info_t *info;
+ if (is_Proj(node))
+ return arch_irn_flags_not_scheduled;
- operand = &fmt->operands[ofs];
- operand->type = type;
- return operand;
+ info = be_get_info(node);
+ return info->flags;
}
-arch_operand_t *arch_set_operand_register_set(arch_insn_format_t *fmt,
- int pos, const arch_register_set_t *set)
+void arch_set_irn_flags(ir_node *node, arch_irn_flags_t flags)
{
- arch_operand_t *op = _arch_set_operand(fmt, pos, arch_operand_type_register_set);
- op->data.set = set;
- return op;
+ backend_info_t *info;
+
+ /* setting flags is only supported for instructions currently.
+ * (mainly because we found no use for it yet and saved the space for
+ * be_infos for them */
+ assert(!is_Proj(node));
+ info = be_get_info(node);
+ info->flags = flags;
}
-arch_operand_t *arch_set_operand_callback(arch_insn_format_t *fmt,
- int pos, arch_register_callback_t *cb)
+void arch_add_irn_flags(ir_node *node, arch_irn_flags_t flags)
{
- arch_operand_t *op = _arch_set_operand(fmt, pos, arch_operand_type_callback);
- op->data.callback = cb;
- return op;
+ backend_info_t *info;
+ assert(!is_Proj(node));
+ info = be_get_info(node);
+ info->flags |= flags;
}
-arch_operand_t *arch_set_operand_immediate(arch_insn_format_t *fmt,
- int pos, const arch_immediate_t *imm)
+bool arch_reg_is_allocatable(const arch_register_req_t *req,
+ const arch_register_t *reg)
{
- arch_operand_t *op = _arch_set_operand(fmt, pos, arch_operand_type_immediate);
- op->data.imm = imm;
- return op;
+ if (reg->type & arch_register_type_joker)
+ return true;
+ if (req->type == arch_register_req_type_none)
+ return false;
+ if (req->type & arch_register_req_type_limited) {
+ if (arch_register_get_class(reg) != req->cls)
+ return false;
+ return rbitset_is_set(req->limited, arch_register_get_index(reg));
+ }
+ return req->cls == arch_register_get_class(reg);
}
-arch_operand_t *arch_set_operand_memory(arch_insn_format_t *fmt, int pos)
+void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
+ const ir_node *node)
{
- arch_operand_t *op = _arch_set_operand(fmt, pos, arch_operand_type_memory);
- return op;
-}
+ if (req == NULL || req->type == arch_register_req_type_none) {
+ fprintf(F, "n/a");
+ return;
+ }
-arch_operand_t *arch_set_operand_equals(arch_insn_format_t *fmt, int pos, int same_as_pos)
-{
- arch_operand_t *op = _arch_set_operand(fmt, pos, arch_operand_type_equals);
- op->data.same_as_pos = same_as_pos;
- return op;
-}
+ fprintf(F, "%s", req->cls->name);
-ir_node *arch_new_node(const arch_insn_t *insn, ir_graph *irg, ir_node *block,
- ir_mode *mode, int arity, ir_node **in)
-{
- ir_node *irn = new_ir_node(NULL, irg, block, insn->op, mode, arity, in);
- arch_irn_data_t *data = (void *) &irn->attr;
+ if (arch_register_req_is(req, limited)) {
+ unsigned n_regs = req->cls->n_regs;
+ unsigned i;
- data->magic = ARCH_IRN_FOURCC;
- data->insn = insn;
+ fprintf(F, " limited to");
+ for (i = 0; i < n_regs; ++i) {
+ if (rbitset_is_set(req->limited, i)) {
+ const arch_register_t *reg = &req->cls->regs[i];
+ fprintf(F, " %s", reg->name);
+ }
+ }
+ }
- return irn;
-}
+ if (arch_register_req_is(req, should_be_same)) {
+ const unsigned other = req->other_same;
+ int i;
-ir_node *arch_new_node_bare(const arch_insn_t *insn, ir_graph *irg, int arity)
-{
- int i;
- ir_node **in = alloca(sizeof(in[0]) * arity);
+ fprintf(F, " same as");
+ for (i = 0; 1U << i <= other; ++i) {
+ if (other & (1U << i)) {
+ ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
+ }
+ }
+ }
+
+ if (arch_register_req_is(req, must_be_different)) {
+ const unsigned other = req->other_different;
+ int i;
- for(i = 0; i < arity; ++i)
- in[i] = new_Unknown(mode_Is);
+ fprintf(F, " different from");
+ for (i = 0; 1U << i <= other; ++i) {
+ if (other & (1U << i)) {
+ ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
+ }
+ }
+ }
- return arch_new_node(insn, irg, new_Unknown(mode_BB), mode_Is, arity, in);
+ if (req->width != 1) {
+ fprintf(F, " width:%d", req->width);
+ }
+ if (arch_register_req_is(req, aligned)) {
+ fprintf(F, " aligned");
+ }
+ if (arch_register_req_is(req, ignore)) {
+ fprintf(F, " ignore");
+ }
+ if (arch_register_req_is(req, produces_sp)) {
+ fprintf(F, " produces_sp");
+ }
}
-ir_mode *arch_get_unknown_mode(void)
+void arch_dump_reqs_and_registers(FILE *F, const ir_node *node)
{
- return mode_Is;
+ int n_ins = get_irn_arity(node);
+ int n_outs = arch_get_irn_n_outs(node);
+ arch_irn_flags_t flags = arch_get_irn_flags(node);
+ int i;
+
+ for (i = 0; i < n_ins; ++i) {
+ const arch_register_req_t *req = arch_get_irn_register_req_in(node, i);
+ fprintf(F, "inreq #%d = ", i);
+ arch_dump_register_req(F, req, node);
+ fputs("\n", F);
+ }
+ for (i = 0; i < n_outs; ++i) {
+ const arch_register_req_t *req = arch_get_irn_register_req_out(node, i);
+ fprintf(F, "outreq #%d = ", i);
+ arch_dump_register_req(F, req, node);
+ fputs("\n", F);
+ }
+ for (i = 0; i < n_outs; ++i) {
+ const arch_register_t *reg = arch_get_irn_register_out(node, i);
+ const arch_register_req_t *req = arch_get_irn_register_req_out(node, i);
+ if (req->cls == NULL)
+ continue;
+ fprintf(F, "reg #%d = %s\n", i, reg != NULL ? reg->name : "n/a");
+ }
+
+ fprintf(F, "flags =");
+ if (flags == arch_irn_flags_none) {
+ fprintf(F, " none");
+ } else {
+ if (flags & arch_irn_flags_dont_spill) {
+ fprintf(F, " unspillable");
+ }
+ if (flags & arch_irn_flags_rematerializable) {
+ fprintf(F, " remat");
+ }
+ if (flags & arch_irn_flags_modify_flags) {
+ fprintf(F, " modify_flags");
+ }
+ if (flags & arch_irn_flags_simple_jump) {
+ fprintf(F, " simple_jump");
+ }
+ if (flags & arch_irn_flags_not_scheduled) {
+ fprintf(F, " not_scheduled");
+ }
+ }
+ fprintf(F, " (%d)\n", (int)flags);
}