update copyright message
[libfirm] / ir / tr / type_identify.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file    type_identify.c
22  * @brief   Representation of types.
23  * @author  Goetz Lindenmaier
24  * @version $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "typerep.h"
31
32 #include <stdlib.h>
33 #include <stddef.h>
34 #include <string.h>
35
36 #include "type_t.h"
37 #include "tpop_t.h"
38 #include "irprog_t.h"
39 #include "array.h"
40 #include "irprog_t.h"
41 #include "pset.h"
42 #include "irtools.h"
43
44 /* The hash set for types. */
45 static pset *type_table = NULL;
46
47 /* hash and compare types */
48 static hash_types_func_t    *hash_types_func;
49 static compare_types_func_t *compare_types_func;
50
51 int compare_names (const void *tp1, const void *tp2) {
52         ir_type *t1 = (ir_type *) tp1;
53         ir_type *t2 = (ir_type *) tp2;
54
55         return (t1 != t2 &&
56                 (t1->type_op !=  t2->type_op ||
57                  t1->name    !=  t2->name      ) );
58 }
59
60 /* stuff for comparing two types. */
61 int compare_strict(const void *tp1, const void *tp2) {
62         const ir_type *t1 = tp1;
63         const ir_type *t2 = tp2;
64         return t1 != t2;
65 }
66
67 /* stuff to compute a hash value for a type. */
68 int firm_hash_name(ir_type *tp) {
69         unsigned h = (unsigned)PTR_TO_INT(tp->type_op);
70         h = 9*h + (unsigned)PTR_TO_INT(tp->name);
71         return h;
72 }
73
74 /* The function that hashes a type. */
75 ir_type *mature_type(ir_type *tp) {
76         ir_type *o;
77
78         assert(type_table);
79
80         o = pset_insert (type_table, tp, hash_types_func(tp) );
81         if (!o || o == tp) return tp;
82         exchange_types(tp, o);
83
84         return o;
85 }
86
87
88 /* The function that hashes a type. */
89 ir_type *mature_type_free(ir_type *tp) {
90         ir_type *o;
91
92         assert(type_table);
93
94         o = pset_insert (type_table, tp, hash_types_func(tp) );
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         if (!o || o == tp) return tp;
111
112         free_type_entities(tp);
113         exchange_types(tp, o);
114
115         return o;
116 }
117
118 /* initialize this module */
119 void init_type_identify(type_identify_if_t *ti_if) {
120         compare_types_func = ti_if && ti_if->cmp  ? ti_if->cmp  : compare_strict;
121         hash_types_func    = ti_if && ti_if->hash ? ti_if->hash : firm_hash_name;
122
123         type_table = new_pset (compare_types_func, 8);
124 }