Fixed the last fix again:
[libfirm] / ir / tr / type_identify.c
1 /*
2  * Copyright (C) 1995-2007 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 "type_identify.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 "typegmod.h"
40 #include "array.h"
41 #include "irprog_t.h"
42 #include "mangle.h"
43 #include "pset.h"
44 #include "irtools.h"
45
46 /* The hash set for types. */
47 static pset *type_table = NULL;
48
49 /* hash and compare types */
50 static hash_types_func_t    *hash_types_func;
51 static compare_types_func_t *compare_types_func;
52
53 int compare_names (const void *tp1, const void *tp2) {
54         ir_type *t1 = (ir_type *) tp1;
55         ir_type *t2 = (ir_type *) tp2;
56
57         return (t1 != t2 &&
58                 (t1->type_op !=  t2->type_op ||
59                  t1->name    !=  t2->name      ) );
60 }
61
62 /* stuff for comparing two types. */
63 int compare_strict(const void *tp1, const void *tp2) {
64         const ir_type *t1 = tp1;
65         const ir_type *t2 = tp2;
66         return t1 != t2;
67 }
68
69 /* stuff to compute a hash value for a type. */
70 int firm_hash_name(ir_type *tp) {
71         unsigned h = (unsigned)PTR_TO_INT(tp->type_op);
72         h = 9*h + (unsigned)PTR_TO_INT(tp->name);
73         return h;
74 }
75
76 /* The function that hashes a type. */
77 ir_type *mature_type(ir_type *tp) {
78         ir_type *o;
79
80         assert(type_table);
81
82         o = pset_insert (type_table, tp, hash_types_func(tp) );
83         if (!o || o == tp) return tp;
84         exchange_types(tp, o);
85
86         return o;
87 }
88
89
90 /* The function that hashes a type. */
91 ir_type *mature_type_free(ir_type *tp) {
92         ir_type *o;
93
94         assert(type_table);
95
96         o = pset_insert (type_table, tp, hash_types_func(tp) );
97         if (!o || o == tp) return tp;
98
99         free_type_entities(tp);
100         free_type(tp);
101
102         return o;
103 }
104
105 /* The function that hashes a type. */
106 ir_type *mature_type_free_entities(ir_type *tp) {
107         ir_type *o;
108
109         assert(type_table);
110
111         o = pset_insert (type_table, tp, hash_types_func(tp) );
112         if (!o || o == tp) return tp;
113
114         free_type_entities(tp);
115         exchange_types(tp, o);
116
117         return o;
118 }
119
120 /* initialize this module */
121 void init_type_identify(type_identify_if_t *ti_if) {
122         compare_types_func = ti_if && ti_if->cmp  ? ti_if->cmp  : compare_strict;
123         hash_types_func    = ti_if && ti_if->hash ? ti_if->hash : firm_hash_name;
124
125         type_table = new_pset (compare_types_func, 8);
126 }