-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtab.c
472 lines (402 loc) · 15.6 KB
/
tab.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
Copyright (C) 1997-2016 Vladimir Makarov.
Written by Vladimir Makarov <[email protected]>
This file is part of the tool MSTA.
This is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* #ifdef HAVE_CONFIG_H */
#include <string.h>
#include "hashtab.h"
#include "common.h"
#include "ird.h"
#include "tab.h"
#include <assert.h>
#include <limits.h>
/* This page contains common functions of some abstract data. */
static unsigned
string_hash_value (const char *string)
{
unsigned result, i;
for (result = i = 0;*string++ != '\0'; i++)
result += ((unsigned char) *string << (i % CHAR_BIT));
return result;
}
static int
string_equality (const char *string_1, const char *string_2)
{
return strcmp (string_1, string_2) == 0;
}
/* This page contains abstract data `tables of string and
identifiers'. Elements of the table is C strings representing
strings and identifiers. */
static unsigned
string_hash_function (hash_table_entry_t string)
{
return string_hash_value (string);
}
static int
string_eq_function (hash_table_entry_t string_1, hash_table_entry_t string_2)
{
return string_equality (string_1, string_2);
}
/* The string table itself is represented by the following variable. */
static hash_table_t string_table;
char *
insert_string (const char *string)
{
hash_table_entry_t *entry_ptr;
entry_ptr = find_hash_table_entry (string_table, string, TRUE);
if (*entry_ptr == NULL)
*entry_ptr = (hash_table_entry_t) string;
else
assert (strcmp (string, *entry_ptr) == 0);
return (char *) *entry_ptr;
}
void
initiate_string_table (void)
{
string_table = create_hash_table (1000, string_hash_function,
string_eq_function);
}
void
finish_string_table (void)
{
#ifndef NDEBUG
if (debug_level >= 1)
fprintf
(stderr,
"String table: entries - %lu, elements - %lu, collisions - %d%%\n",
hash_table_size (string_table),
hash_table_elements_number (string_table),
get_collisions (string_table));
#endif
delete_hash_table (string_table);
}
/* This page contains abstract data `table of single definitions'
Elements of the table are nodes representing single definitions of
terminals and nonterminals. Key of the table elements is
identifier strings (or code of literal terminal) of given terminal
or nonterminal. */
static unsigned
single_definition_hash_function (hash_table_entry_t single_definition)
{
assert
(IR_identifier_or_literal ((IR_node_t) single_definition) != NULL
&& (!IR_IS_OF_TYPE (IR_identifier_or_literal
((IR_node_t) single_definition),
IR_NM_identifier)
|| IR_identifier_itself (IR_identifier_or_literal
((IR_node_t) single_definition)) != NULL));
if (IR_IS_OF_TYPE (IR_identifier_or_literal ((IR_node_t) single_definition),
IR_NM_identifier))
return string_hash_value (IR_identifier_itself
(IR_identifier_or_literal ((IR_node_t)
single_definition)));
else
return IR_literal_code (IR_identifier_or_literal ((IR_node_t)
single_definition));
}
static int
single_definition_eq_function (hash_table_entry_t single_definition_1,
hash_table_entry_t single_definition_2)
{
assert
(single_definition_1 != NULL
&& (!IR_IS_OF_TYPE (IR_identifier_or_literal
((IR_node_t) single_definition_1),
IR_NM_identifier)
|| IR_identifier_itself (IR_identifier_or_literal
((IR_node_t) single_definition_1)) != NULL)
&& single_definition_2 != NULL
&& (!IR_IS_OF_TYPE (IR_identifier_or_literal
((IR_node_t) single_definition_2),
IR_NM_identifier)
|| IR_identifier_itself (IR_identifier_or_literal
((IR_node_t) single_definition_2)) != NULL));
return
(IR_NODE_MODE (IR_identifier_or_literal ((IR_node_t) single_definition_1))
== IR_NODE_MODE (IR_identifier_or_literal ((IR_node_t)
single_definition_2))
&& (IR_IS_OF_TYPE (IR_identifier_or_literal
((IR_node_t) single_definition_1),
IR_NM_identifier)
? string_equality (IR_identifier_itself
(IR_identifier_or_literal ((IR_node_t)
single_definition_1)),
IR_identifier_itself
(IR_identifier_or_literal ((IR_node_t)
single_definition_2)))
: (IR_literal_code (IR_identifier_or_literal
((IR_node_t) single_definition_1))
== IR_literal_code (IR_identifier_or_literal
((IR_node_t) single_definition_2)))));
}
/* The single definition table itself is represented by the following
variable. */
static hash_table_t single_definition_table;
IR_node_t
insert_single_definition (IR_node_t single_definition)
{
hash_table_entry_t *entry_ptr;
assert (!IR_IS_OF_TYPE (single_definition, IR_NM_literal_range_definition));
entry_ptr = find_hash_table_entry (single_definition_table,
single_definition, TRUE);
if (*entry_ptr == NULL)
*entry_ptr = (hash_table_entry_t) single_definition;
return (IR_node_t) *entry_ptr;
}
/* The following variable value is node representing single
definition. The node used for searching single definition with
given identifier or literal (only for terminal). */
static IR_node_t work_single_definition;
IR_node_t
find_single_definition (IR_node_t identifier_or_literal)
{
hash_table_entry_t *entry_ptr;
IR_set_identifier_or_literal (work_single_definition, identifier_or_literal);
entry_ptr = find_hash_table_entry (single_definition_table,
work_single_definition, FALSE);
return (IR_node_t) *entry_ptr;
}
void
initiate_single_definition_table (void)
{
work_single_definition = IR_create_node (IR_NM_single_term_definition);
single_definition_table
= create_hash_table (1000, single_definition_hash_function,
single_definition_eq_function);
}
void
finish_single_definition_table (void)
{
#ifndef NDEBUG
if (debug_level >= 1)
fprintf
(stderr,
"Definition table: entries - %lu, elements - %lu, collisions - %d%%\n",
hash_table_size (single_definition_table),
hash_table_elements_number (single_definition_table),
get_collisions (single_definition_table));
#endif
delete_hash_table (single_definition_table);
}
/* This page contains abstract data `table of literal definitions'.
Elements of the table are single term definitions denoting a
literal and literal range definitions. Key of the table token
value(s). */
static hash_table_t literal_definition_table;
static unsigned
literal_definition_hash_function (hash_table_entry_t literal_definition)
{
assert (!IR_IS_OF_TYPE ((IR_node_t) literal_definition,
IR_NM_literal_range_definition)
|| (IR_value ((IR_node_t) literal_definition)
< IR_right_range_bound_value ((IR_node_t) literal_definition)));
return (IR_value ((IR_node_t) literal_definition)
* (IR_IS_OF_TYPE ((IR_node_t) literal_definition,
IR_NM_literal_range_definition)
? IR_right_range_bound_value ((IR_node_t) literal_definition)
: 1));
}
static int
literal_definition_eq_function (hash_table_entry_t literal_definition_1,
hash_table_entry_t literal_definition_2)
{
assert ((!IR_IS_OF_TYPE ((IR_node_t) literal_definition_1,
IR_NM_literal_range_definition)
|| (IR_value ((IR_node_t) literal_definition_1)
< IR_right_range_bound_value ((IR_node_t)
literal_definition_1)))
&& (!IR_IS_OF_TYPE ((IR_node_t) literal_definition_2,
IR_NM_literal_range_definition)
|| (IR_value ((IR_node_t) literal_definition_2)
< IR_right_range_bound_value ((IR_node_t)
literal_definition_2))));
return (IR_NODE_MODE ((IR_node_t) literal_definition_1)
== IR_NODE_MODE ((IR_node_t) literal_definition_2)
&& (IR_value ((IR_node_t) literal_definition_1)
== IR_value ((IR_node_t) literal_definition_2))
&& (!IR_IS_OF_TYPE ((IR_node_t) literal_definition_1,
IR_NM_literal_range_definition)
|| (IR_right_range_bound_value ((IR_node_t) literal_definition_1)
== IR_right_range_bound_value ((IR_node_t)
literal_definition_2))));
}
IR_node_t
insert_literal_definition (IR_node_t literal_definition)
{
hash_table_entry_t *entry_ptr;
assert (IR_IS_OF_TYPE (IR_identifier_or_literal (literal_definition),
IR_NM_literal));
entry_ptr = find_hash_table_entry (literal_definition_table,
literal_definition, TRUE);
if (*entry_ptr == NULL)
*entry_ptr = (hash_table_entry_t) literal_definition;
return (IR_node_t) *entry_ptr;
}
static IR_node_t work_single_term_definition;
static IR_node_t work_literal_range_definition;
IR_node_t
find_literal_definition (int left_range_value, int right_range_value)
{
hash_table_entry_t *entry_ptr;
assert (left_range_value <= right_range_value);
if (left_range_value == right_range_value)
{
IR_set_value (work_single_term_definition, left_range_value);
entry_ptr = find_hash_table_entry (literal_definition_table,
work_single_term_definition, FALSE);
}
else
{
IR_set_value (work_literal_range_definition, left_range_value);
IR_set_right_range_bound_value (work_literal_range_definition,
right_range_value);
entry_ptr = find_hash_table_entry (literal_definition_table,
work_literal_range_definition, FALSE);
}
if (*entry_ptr == NULL)
return NULL;
else
return (IR_node_t) *entry_ptr;
}
void
initiate_literal_definition_table (void)
{
work_single_term_definition = IR_create_node (IR_NM_single_term_definition);
work_literal_range_definition
= IR_create_node (IR_NM_literal_range_definition);
literal_definition_table
= create_hash_table (1000, literal_definition_hash_function,
literal_definition_eq_function);
}
void
finish_literal_definition_table (void)
{
#ifndef NDEBUG
if (debug_level >= 1)
fprintf
(stderr,
"Literal table: entries - %lu, elements - %lu, collisions - %d%%\n",
hash_table_size (literal_definition_table),
hash_table_elements_number (literal_definition_table),
get_collisions (literal_definition_table));
#endif
delete_hash_table (literal_definition_table);
}
/* This page contains abstract data `table of values of terminals
(including literal ranges)'. Elements of the table is single
terminal definition and literal range definitions. */
struct token_code_value
{
/* Key */
int value;
/* Different structure can refer for the same teminal definition of
course if the definition represents a literal range. */
IR_node_t single_term_definition;
};
typedef struct token_code_value *token_code_value_t;
static os_t token_code_values;
static hash_table_t token_code_value_table;
static unsigned
token_code_value_hash_function (hash_table_entry_t token_code_value)
{
assert (((token_code_value_t) token_code_value)->value >= 0);
return ((token_code_value_t) token_code_value)->value;
}
static int
token_code_value_eq_function (hash_table_entry_t token_code_value_1,
hash_table_entry_t token_code_value_2)
{
assert (((token_code_value_t) token_code_value_1)->value >= 0
&& ((token_code_value_t) token_code_value_2)->value >= 0);
return (((token_code_value_t) token_code_value_1)->value
== ((token_code_value_t) token_code_value_2)->value);
}
IR_node_t
insert_token_code_value (IR_node_t token_definition, int value)
{
hash_table_entry_t *entry_ptr;
token_code_value_t token_code_value;
assert (value >= 0 && token_definition != NULL);
token_code_value = OS_TOP_BEGIN (token_code_values);
token_code_value->value = value;
token_code_value->single_term_definition = token_definition;
entry_ptr = find_hash_table_entry (token_code_value_table, token_code_value,
TRUE);
if (*entry_ptr == NULL)
{
*entry_ptr = (hash_table_entry_t) token_code_value;
OS_TOP_FINISH (token_code_values);
OS_TOP_EXPAND (token_code_values, sizeof (struct token_code_value));
}
return ((token_code_value_t) *entry_ptr)->single_term_definition;
}
void
insert_token_code_value_with_overwriting (IR_node_t token_definition,
int value)
{
hash_table_entry_t *entry_ptr;
token_code_value_t token_code_value;
assert (value >= 0 && token_definition != NULL);
token_code_value = OS_TOP_BEGIN (token_code_values);
token_code_value->value = value;
token_code_value->single_term_definition = token_definition;
entry_ptr = find_hash_table_entry (token_code_value_table, token_code_value,
TRUE);
*entry_ptr = (hash_table_entry_t) token_code_value;
OS_TOP_FINISH (token_code_values);
OS_TOP_EXPAND (token_code_values, sizeof (struct token_code_value));
}
IR_node_t
find_token_code_value (int value)
{
hash_table_entry_t *entry_ptr;
token_code_value_t token_code_value;
assert (value >= 0);
token_code_value = OS_TOP_BEGIN (token_code_values);
token_code_value->value = value;
entry_ptr = find_hash_table_entry (token_code_value_table,
token_code_value, FALSE);
if (*entry_ptr == NULL)
return NULL;
else
return ((token_code_value_t) *entry_ptr)->single_term_definition;
}
void
initiate_token_code_value_table (void)
{
OS_CREATE (token_code_values, 0);
OS_TOP_EXPAND (token_code_values, sizeof (struct token_code_value));
token_code_value_table
= create_hash_table (1000, token_code_value_hash_function,
token_code_value_eq_function);
}
void
finish_token_code_value_table (void)
{
#ifndef NDEBUG
if (debug_level >= 1)
fprintf
(stderr,
"Token code table: entries - %lu, elements - %lu, collisions - %d%%\n",
hash_table_size (token_code_value_table),
hash_table_elements_number (token_code_value_table),
get_collisions (token_code_value_table));
#endif
delete_hash_table (token_code_value_table);
OS_DELETE (token_code_values);
}