Removed mode parameter from Const and Const_type constructors (now derived from tarval)
[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 #include "config.h"
27
28 #include "typerep.h"
29
30 #include <stdlib.h>
31 #include <stddef.h>
32 #include <string.h>
33
34 #include "type_t.h"
35 #include "tpop_t.h"
36 #include "irprog_t.h"
37 #include "array.h"
38 #include "irprog_t.h"
39 #include "pset.h"
40 #include "irtools.h"
41
42 /* The hash set for types. */
43 static pset *type_table = NULL;
44
45 /* hash and compare types */
46 static hash_types_func_t    *hash_types_func;
47 static compare_types_func_t *compare_types_func;
48
49 int compare_names (const void *tp1, const void *tp2) {
50         ir_type *t1 = (ir_type *) tp1;
51         ir_type *t2 = (ir_type *) tp2;
52
53         return (t1 != t2 &&
54                 (t1->type_op !=  t2->type_op ||
55                  t1->name    !=  t2->name      ) );
56 }
57
58 /* stuff for comparing two types. */
59 int compare_strict(const void *tp1, const void *tp2) {
60         const ir_type *t1 = tp1;
61         const ir_type *t2 = tp2;
62         return t1 != t2;
63 }
64
65 /* stuff to compute a hash value for a type. */
66 int firm_hash_name(ir_type *tp) {
67         unsigned h = (unsigned)PTR_TO_INT(tp->type_op);
68         h = 9*h + (unsigned)PTR_TO_INT(tp->name);
69         return h;
70 }
71
72 /* The function that hashes a type. */
73 ir_type *mature_type(ir_type *tp) {
74         ir_type *o;
75
76         assert(type_table);
77
78         o = pset_insert (type_table, tp, hash_types_func(tp) );
79         if (!o || o == tp) return tp;
80         exchange_types(tp, o);
81
82         return o;
83 }
84
85
86 /* The function that hashes a type. */
87 ir_type *mature_type_free(ir_type *tp) {
88         ir_type *o;
89
90         assert(type_table);
91
92         o = pset_insert (type_table, tp, hash_types_func(tp) );
93         if (!o || o == tp) return tp;
94
95         free_type_entities(tp);
96         free_type(tp);
97
98         return o;
99 }
100
101 /* The function that hashes a type. */
102 ir_type *mature_type_free_entities(ir_type *tp) {
103         ir_type *o;
104
105         assert(type_table);
106
107         o = pset_insert (type_table, tp, hash_types_func(tp) );
108         if (!o || o == tp) return tp;
109
110         free_type_entities(tp);
111         exchange_types(tp, o);
112
113         return o;
114 }
115
116 /* initialize this module */
117 void init_type_identify(type_identify_if_t *ti_if) {
118         compare_types_func = ti_if && ti_if->cmp  ? ti_if->cmp  : compare_strict;
119         hash_types_func    = ti_if && ti_if->hash ? ti_if->hash : firm_hash_name;
120
121         type_table = new_pset (compare_types_func, 8);
122 }