get_default_cc_mask() added
[libfirm] / ir / ident / ident.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/common/ident.c
4  * Purpose:     Hash table to store names.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1999-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <assert.h>
18 #include <ctype.h>
19 #include <string.h>
20 #include <stddef.h>
21 #include <stdlib.h>
22
23 #include "ident_t.h"
24 #include "set.h"
25
26 /* for debugging only, not the real implementation */
27 struct ident {
28   char reserved[sizeof(unsigned) + sizeof(size_t)];
29   char data[1];
30 };
31
32 /** The current ident module implementation. */
33 static ident_if_t impl;
34
35 /**
36  * Stores a string in the ident module and returns a handle for the string.
37  *
38  * @param handle   the handle for the set
39  * @param str      the string which shall be stored
40  * @param len      length of str in bytes
41  *
42  * @return id - a handle for the generated ident
43  *
44  * Default implementation using libfirm sets.
45  */
46 static ident *set_new_id_from_chars(void *handle, const char *str, int len)
47 {
48   set *id_set = handle;
49
50   /* GL: Who added this assert?  And why? */
51   //assert(len > 0);
52   return (ident *)set_hinsert0(id_set, str, len, ID_HASH(str, len));
53 }
54
55 /**
56  * Stores a string in the ident module and returns a handle for the string.
57  *
58  * @param handle   the handle for the set
59  * @param str      the string (or whatever) which shall be stored
60  *
61  * Default implementation using libfirm sets.
62  */
63 static ident *set_new_id_from_str(void *handle, const char *str)
64 {
65   assert(str);
66   return (ident *)set_new_id_from_chars(handle, str, strlen(str));
67 }
68
69 /**
70  * Returns a string represented by an ident.
71  *
72  * @param handle   the handle for the set
73  * @param id       the ident
74  *
75  * Default implementation using libfirm sets.
76  */
77 static const char *set_get_id_str(void *handle, ident *id)
78 {
79   struct set_entry *entry = (struct set_entry *)id;
80
81   return (const char *)entry->dptr;
82 }
83
84 /**
85  * Returns the length of the string represented by an ident.
86  *
87  * @param handle   the handle for the set
88  * @param id       the ident
89  *
90  * Default implementation using libfirm sets.
91  */
92 static int set_get_id_strlen(void *handle, ident *id)
93 {
94   struct set_entry *entry = (struct set_entry *)id;
95
96   return entry->size;
97 }
98
99 /**
100  * Default implementation using libfirm sets.
101  */
102 void set_finish_ident(void *handle) {
103   set *id_set = handle;
104
105   del_set(id_set);
106 }
107
108 /**
109  * Default implementation if no new_id_from_str() is provided.
110  */
111 static ident *def_new_id_from_str(void *handle, const char *str)
112 {
113   return impl.new_id_from_chars(handle, str, strlen(str));
114 }
115
116 /**
117  * Default implementation if no get_id_strlen() is provided.
118  */
119 static int def_get_id_strlen(void *handle, ident *id)
120 {
121   return strlen(impl.get_id_str(handle, id));
122 }
123
124 /* Initialize the ident module. */
125 void init_ident(ident_if_t *id_if, int initial_n_idents)
126 {
127   if (id_if) {
128     memcpy(&impl, id_if, sizeof(impl));
129
130     if (! impl.new_id_from_str)
131       impl.new_id_from_str = def_new_id_from_str;
132     if (! impl.get_id_strlen)
133       impl.get_id_strlen = def_get_id_strlen;
134   }
135   else {
136    impl.new_id_from_str   = set_new_id_from_str;
137    impl.new_id_from_chars = set_new_id_from_chars;
138    impl.get_id_str        = set_get_id_str;
139    impl.get_id_strlen     = set_get_id_strlen;
140    impl.finish_ident      = set_finish_ident;
141
142    impl.handle = new_set(memcmp, initial_n_idents);
143   }
144 }
145
146 ident *new_id_from_str(const char *str)
147 {
148   assert(str);
149   return impl.new_id_from_str(impl.handle, str);
150 }
151
152 ident *new_id_from_chars(const char *str, int len)
153 {
154   assert(len > 0);
155   return impl.new_id_from_chars(impl.handle, str, len);
156 }
157
158 const char *get_id_str(ident *id)
159 {
160   return impl.get_id_str(impl.handle, id);
161 }
162
163 int get_id_strlen(ident *id)
164 {
165   return impl.get_id_strlen(impl.handle, id);
166 }
167
168 void finish_ident(void) {
169   if (impl.finish_ident)
170     impl.finish_ident(impl.handle);
171 }
172
173 int id_is_prefix(ident *prefix, ident *id)
174 {
175   if (get_id_strlen(prefix) > get_id_strlen(id)) return 0;
176   return 0 == memcmp(get_id_str(prefix), get_id_str(id), get_id_strlen(prefix));
177 }
178
179 int id_is_suffix(ident *suffix, ident *id)
180 {
181   int suflen = get_id_strlen(suffix);
182   int idlen  = get_id_strlen(id);
183   const char *part;
184
185   if (suflen > idlen) return 0;
186
187   part = get_id_str(id);
188   part = part + (idlen - suflen);
189
190   return 0 == memcmp(get_id_str(suffix), part, suflen);
191 }
192
193 int id_contains_char(ident *id, char c)
194 {
195   return strchr(get_id_str(id), c) != NULL;
196 }
197
198 int print_id (ident *id)
199 {
200   return printf("%s", get_id_str(id));
201 }
202
203 int fprint_id (FILE *F, ident *id)
204 {
205   return fprintf(F, "%s", get_id_str(id));
206 }