Every node has now a pinned attribute that is inherited from the op.
[libfirm] / ir / tr / type_identify.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/type_identify.c
4  * Purpose:     Representation of types.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2001-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  *  @file type_identify.c
15  *
16  *  (C) 2004 by Universitaet Karlsruhe
17  *  Goetz Lindenmaier
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "type_identify_t.h"
26
27 #include <stdlib.h>
28 #include <stddef.h>
29 #include <string.h>
30
31 #include "type_t.h"
32 #include "tpop_t.h"
33 #include "irprog_t.h"
34 #include "typegmod.h"
35 #include "array.h"
36 #include "irprog_t.h"
37 #include "mangle.h"
38 #include "pset.h"
39 #include "irtools.h"
40
41 /* The hash set for types. */
42 static pset *type_table = NULL;
43
44 /* hash and compare types */
45 static hash_types_func_t    *hash_types_func;
46 static compare_types_func_t *compare_types_func;
47
48 int compare_names (const void *tp1, const void *tp2) {
49   ir_type *t1 = (ir_type *) tp1;
50   ir_type *t2 = (ir_type *) tp2;
51
52   return (t1 != t2 &&
53           (t1->type_op !=  t2->type_op ||
54            t1->name    !=  t2->name      )  );
55 }
56
57 /* stuff for comparing two types. */
58 int compare_strict (const void *tp1, const void *tp2) {
59   ir_type *t1 = (ir_type *) tp1;
60   ir_type *t2 = (ir_type *) tp2;
61   return t1 != t2;
62 }
63
64 /* stuff to compute a hash value for a type. */
65 int firm_hash_name (ir_type *tp) {
66   unsigned h = (unsigned)PTR_TO_INT(tp->type_op);
67   h = 9*h + (unsigned)PTR_TO_INT(tp->name);
68   return h;
69 }
70
71 /* The function that hashes a type. */
72 ir_type *mature_type(ir_type *tp) {
73   ir_type *o;
74
75   assert(type_table);
76
77   o = pset_insert (type_table, tp, hash_types_func(tp) );
78
79   if (!o || o == tp) return tp;
80
81   exchange_types(tp, o);
82
83   return o;
84 }
85
86
87 /* The function that hashes a type. */
88 ir_type *mature_type_free(ir_type *tp) {
89   ir_type *o;
90
91   assert(type_table);
92
93   o = pset_insert (type_table, tp, hash_types_func(tp) );
94
95   if (!o || o == tp) return tp;
96
97   free_type_entities(tp);
98   free_type(tp);
99
100   return o;
101 }
102
103 /* The function that hashes a type. */
104 ir_type *mature_type_free_entities(ir_type *tp) {
105   ir_type *o;
106
107   assert(type_table);
108
109   o = pset_insert (type_table, tp, hash_types_func(tp) );
110
111   if (!o || o == tp) return tp;
112
113   free_type_entities(tp);
114   exchange_types(tp, o);
115
116   return o;
117 }
118
119 /* initialize this module */
120 void init_type_identify(type_identify_if_t *ti_if) {
121   compare_types_func = ti_if && ti_if->cmp  ? ti_if->cmp  : compare_strict;
122   hash_types_func    = ti_if && ti_if->hash ? ti_if->hash : firm_hash_name;
123
124   type_table = new_pset (compare_types_func, 8);
125 }