-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmacro.c
329 lines (283 loc) · 5.69 KB
/
macro.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
#include <assert.h>
#include <memory.h>
#include <stdio.h>
#include <sys/types.h>
#include "c2ada.h"
/* from scan.c */
extern comment_block_pt fetch_comment_block(void);
#undef NULL
#define NULL 0
#ifndef MACRO_TABLE_SIZE /* must be a power of 2 */
#define MACRO_TABLE_SIZE 512
#endif
#define STRMAC(a) STRMAC__NEXT(a)
#define STRMAC__NEXT(a) #a
macro_t* macro_list_head;
static macro_t* macro_list_tail;
static macro_t* hash_table[MACRO_TABLE_SIZE];
struct autodefs
{
char* name;
char* value;
} deftab[] = {
{"__ANSI_CPP__", "1"},
{"_LANGUAGE_C", "1"},
{"LANGUAGE_C", "1"},
{"__STDC__", "1"},
#ifdef __IEEE_BIG_ENDIAN
{"__IEEE_BIG_ENDIAN", "1"},
#else
{"__IEEE_LITTLE_ENDIAN", "1"},
#endif
#ifdef _IBMR2
{"_IBMR2", "1"},
#endif
#ifdef _AIX
{"_AIX", "1"},
#endif
#ifdef _AIX32
{"_AIX32", "1"},
#endif
#ifdef host_mips
{"host_mips", "1"},
#endif
#ifdef mips
{"mips", "1"},
#endif
#ifdef MIPSEB
{"MIPSEB", "1"},
#endif
#ifdef sgi
{"sgi", "1"},
#endif
#ifdef sun
{"sun", "1"},
#endif
#ifdef __sun
{"__sun", "1"},
#endif
#ifdef sparc
{"sparc", "1"},
#endif
#ifdef __sparc
{"__sparc", "1"},
#endif
#ifdef SVR3
{"SVR3", "1"},
#endif
#ifndef __SVR4
{"__SVR4", "1"},
#endif
#ifdef SYSTYPE_SYSV
{"SYSTYPE_SYSV", "1"},
#endif
#ifdef _MIPSEB
{"_MIPSEB", "1"},
#endif
#ifdef _POSIX_SOURCE
{"_POSIX_SOURCE", "1"},
#endif
#ifdef _SYSTYPE_SYSV
{"_SYSTYPE_SYSV", "1"},
#endif
#ifdef __EXTENSIONS__
{"__EXTENSIONS__", "1"},
#endif
#ifdef __host_mips
{"__host_mips", "1"},
#endif
#ifdef __mips
{"__mips", "1"},
#endif
#ifdef __sgi
{"__sgi", "1"},
#endif
#ifdef __SVR3
{"__SVR3", "1"},
#endif
#ifdef unix
{"unix", "1"},
#endif
#ifdef __unix
{"__unix", "1"},
#endif
#ifdef __unix__
{"__unix__", "1"},
#endif
#ifdef __linux
{"__linux", "1"},
#endif
#ifdef __linux__
{"__linux__", "1"},
#endif
#ifdef __CYGWIN__
{"__CYGWIN__", STRMAC(__CYGWIN__)},
#endif
#ifdef __CYGWIN32__
{"__CYGWIN32__", STRMAC(__CYGWIN32__)},
#endif
#ifdef __CYGWIN64__
{"__CYGWIN64__", STRMAC(__CYGWIN64__)},
#endif
#ifdef _MSC_VER
{"_MSC_VER", STRMAC(_MSC_VER)},
#endif
#include "defaultmacros.h"
{ NULL, NULL }
};
static void macro_add_to_list(macro_t* m)
{
if(macro_list_head)
{
macro_list_tail->macro_next = m;
}
else
{
macro_list_head = m;
}
macro_list_tail = m;
}
static void macro_add_to_table(macro_t* m)
{
int index;
m->macro_hash = common_hash(m->macro_name);
index = m->macro_hash & (MACRO_TABLE_SIZE - 1);
m->macro_hash_link = hash_table[index];
hash_table[index] = m;
}
void macro_undef(char* name)
{
macro_t *m, *last;
hash_t hash;
int index;
assert(name != NULL);
hash = common_hash(name);
index = hash & (MACRO_TABLE_SIZE - 1);
last = NULL;
for(m = hash_table[index]; m; m = m->macro_hash_link)
{
if(m->macro_hash == hash && !strcmp(m->macro_name, name))
{
if(last == NULL)
{
hash_table[index] = m->macro_hash_link;
}
else
{
last->macro_hash_link = m->macro_hash_link;
}
}
else
{
last = m;
}
}
}
void macro_def(
const char* name,
const char* body,
int len,
int params,
char** formals,
file_pos_t definition,
char* eol_comment)
{
macro_t* m;
assert(name != NULL);
m = (macro_t*)allocate(sizeof(macro_t));
m->macro_name = name;
m->macro_body = body;
m->macro_body_len = len;
m->macro_params = params;
m->macro_definition = definition;
m->macro_param_vec = formals;
m->macro_func = NULL;
m->macro_declared_in_header = current_unit_is_header;
m->comment = fetch_comment_block();
m->eol_comment = eol_comment;
if(definition != 0)
{
macro_add_to_list(m);
}
macro_add_to_table(m);
}
macro_t* macro_find(char* name)
{
macro_t* m;
hash_t hash;
int index;
hash = common_hash(name);
index = hash & (MACRO_TABLE_SIZE - 1);
for(m = hash_table[index]; m; m = m->macro_hash_link)
{
if(m->macro_hash == hash && !strcmp(m->macro_name, name))
{
return m;
}
}
return NULL;
}
void macro_init(int force)
{
static int macro_initialized = 0;
macro_t* m;
int i;
if(macro_initialized)
{
if(!force)
return;
memset(&hash_table, 0, sizeof(hash_table));
macro_list_head = NULL;
macro_list_tail = NULL;
}
macro_initialized = 1;
m = (macro_t*)allocate(sizeof(macro_t));
m->macro_name = "__FILE__";
m->macro_params = BUILTIN_FILE;
macro_add_to_table(m);
m = (macro_t*)allocate(sizeof(macro_t));
m->macro_name = "__LINE__";
m->macro_params = BUILTIN_LINE;
macro_add_to_table(m);
for(i = 0; deftab[i].name != NULL; i++)
{
macro_def(deftab[i].name, deftab[i].value, strlen(deftab[i].value), -1,
NULL, 0, 0);
}
}
void cpp_show_predefines()
{
int i;
puts("__FILE__\n__LINE__");
for(i = 0; deftab[i].name != NULL; i++)
{
printf("%s\t%s\n", deftab[i].name, (deftab[i].value) ? deftab[i].value : "");
}
}
#if 0
macro_t* nth_macro(int n)
{
static int lastn = -10;
static macro_t *lastm = NULL;
int i;
assert(n >= 0);
if (lastn == (n - 1)) {
i = n;
assert(lastm != NULL);
lastm = lastm->macro_next;
}
else {
for (lastm = macro_list_head, i = 0; i < n; i++) {
if (lastm == NULL) break;
lastm = lastm->macro_next;
}
}
if (i == n && lastm != NULL) {
lastn = n;
return lastm;
}
lastn = -10;
lastm = NULL;
return NULL;
}
#endif