Added custom data registration facility.
authorSebastian Hack <hack@ipd.info.uni-karlsruhe.de>
Tue, 30 Nov 2004 13:27:34 +0000 (13:27 +0000)
committerSebastian Hack <hack@ipd.info.uni-karlsruhe.de>
Tue, 30 Nov 2004 13:27:34 +0000 (13:27 +0000)
[r4509]

ir/ir/irnode.c
ir/ir/irnode.h

index 0200074..87bc606 100644 (file)
@@ -84,9 +84,35 @@ const char *symconst_name_arr [] = {
   "type_tag", "size", "addr_name", "addr_ent"
 };
 
+/**
+ * Indicates, whether additional data can be registered to ir nodes.
+ * If set to 1, this is not possible anymore.
+ */
+static int forbid_new_data = 0;
+
+/**
+ * The amount of additional space for custom data to be allocated upon
+ * creating a new node.
+ */
+static size_t additional_node_data_size = 0;
+
+
+size_t register_additional_node_data(size_t size)
+{
+       assert(!forbid_new_data && "Too late to register additional node data");
+
+       if(forbid_new_data)
+               return 0;
+
+       return additional_node_data_size += size;
+}
+
+
 void
 init_irnode (void)
 {
+       /* Forbid the addition of new data to an ir node. */
+       forbid_new_data = 1;
 }
 
 /*
@@ -100,11 +126,13 @@ new_ir_node (dbg_info *db, ir_graph *irg, ir_node *block, ir_op *op, ir_mode *mo
          int arity, ir_node **in)
 {
   ir_node *res;
-  int node_size = offsetof (ir_node, attr) +  op->attr_size;
+  size_t node_size = offsetof(ir_node, attr) + op->attr_size + additional_node_data_size;
+       char *p;
 
   assert(irg && op && mode);
-  res = (ir_node *) obstack_alloc (irg->obst, node_size);
-  memset((void *)res, 0, node_size);
+  p = obstack_alloc (irg->obst, node_size);
+  memset(p, 0, node_size);
+       res = (ir_node *) (p + additional_node_data_size);
 
   res->kind    = k_ir_node;
   res->op      = op;
@@ -2011,6 +2039,7 @@ is_forking_op(const ir_node *node) {
   return is_op_forking(get_irn_op(node));
 }
 
+
 #ifdef DEBUG_libfirm
 void dump_irn (ir_node *n) {
   int i, arity = get_irn_arity(n);
index 04391d9..739a56b 100644 (file)
@@ -13,6 +13,8 @@
 # ifndef _IRNODE_H_
 # define _IRNODE_H_
 
+#include <stddef.h>
+
 /**
  * Projection numbers of compare: use for Proj nodes!
  * @remark there are numbers with normalized names below!
@@ -889,6 +891,28 @@ ir_node *get_fragile_op_mem(ir_node *node);
  *  operation: Cond. */
 int is_forking_op(const ir_node *node);
 
+/**
+ * Access custom node data.
+ * The data must have been registered with
+ * register_additional_node_data() before.
+ * @param node The ir node to get the data from.
+ * @param type The type of the data you registered.
+ * @param off The value returned by register_additional_node_data.
+ * @return A pointer of type @p type.
+ */
+#define get_irn_data(node,type,off) \
+ (assert(off > 0 && "Invalid node data offset"), (type *) ((char *) (node) - (off)))
+
+/**
+ * Request additional data to be allocated with an ir node.
+ * @param size The size of the additional data required.
+ * @return A positive number, if the opration was successful, which
+ * must be passed to the access macro get_irn_data(), 0 if the
+ * registration failed.
+ */
+size_t register_additional_node_data(size_t size);
+
+
 /*-----------------------------------------------------------------*/
 /** Debug aides                                                   **/
 /*-----------------------------------------------------------------*/