-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsymbols.c
341 lines (279 loc) · 7.45 KB
/
symbols.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
/* Copyright (C) 2018, 2020 Lars Brinkhoff <lars@nocrew.org>
Copyright (C) 2018, 2019, 2020 Adam Sampson <ats@offog.org>
This program 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 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dis.h"
#include "symbols.h"
#define MAX_SYMBOLS 16384
static int symbols_mode = SYMBOLS_NONE;
void
usage_symbols_mode (void)
{
fprintf (stderr, "Valid symbol modes are: none, ddt, all.\n");
}
int
parse_symbols_mode (const char *string)
{
if (strcmp (string, "none") == 0)
symbols_mode = SYMBOLS_NONE;
else if (strcmp (string, "ddt") == 0)
symbols_mode = SYMBOLS_DDT;
else if (strcmp (string, "all") == 0)
symbols_mode = SYMBOLS_ALL;
else
return -1;
return 0;
}
struct symbol symbols[MAX_SYMBOLS];
int num_symbols = 0;
typedef enum {
SORT_NONE,
SORT_NAME,
SORT_VALUE
} sort_mode_t;
static sort_mode_t sorted = SORT_NONE;
void
add_symbol (const char *name, word_t value, int flags)
{
int i = num_symbols++;
char *p;
if (num_symbols > MAX_SYMBOLS)
{
fprintf (stderr, "Too many symbols\n");
exit (1);
}
/* Copy the name, with trailing spaces stripped off. */
p = strdup (name);
symbols[i].name = p;
while (*p)
p++;
while (p > symbols[i].name && *(p - 1) == ' ')
*--p = '\0';
symbols[i].value = value;
symbols[i].sequence = num_symbols;
symbols[i].flags = flags;
sorted = SORT_NONE;
}
/* When searching symbols, we can't assume that anything other than
the field we're searching for is valid, as one of the arguments
might be the key. */
static int
compare_name_search (const void *a, const void *b)
{
const struct symbol *sa = a;
const struct symbol *sb = b;
return strcmp (sa->name, sb->name);
}
static int
compare_value_search (const void *a, const void *b)
{
const struct symbol *sa = a;
const struct symbol *sb = b;
if (sa->value == sb->value)
return 0;
else if (sa->value < sb->value)
return -1;
else
return 1;
}
/* When sorting, all the fields are valid. Order first by the key we're
sorting by, then by visibility so the most visible symbols come
first, then by their original declaration order. */
static int
concealment (const struct symbol *s)
{
if (s->flags & SYMBOL_KILLED)
return 2;
if (s->flags & SYMBOL_HALFKILLED)
return 1;
return 0;
}
static int
compare_default (const void *a, const void *b)
{
const struct symbol *sa = a;
const struct symbol *sb = b;
int r = concealment (sa) - concealment (sb);
if (r != 0)
return r;
if (sa->sequence < sb->sequence)
return -1;
else
return 1;
}
static int
compare_name_sort (const void *a, const void *b)
{
int r = compare_name_search (a, b);
if (r == 0)
return compare_default (a, b);
else
return r;
}
static int
compare_value_sort (const void *a, const void *b)
{
int r = compare_value_search (a, b);
if (r == 0)
return compare_default (a, b);
else
return r;
}
static void
sort_by (sort_mode_t wanted)
{
if (sorted != wanted)
{
qsort (symbols, num_symbols, sizeof *symbols,
wanted == SORT_NAME ? compare_name_sort : compare_value_sort);
sorted = wanted;
}
}
static const struct symbol *
hint_accumulator (const struct symbol *first, word_t value)
{
const struct symbol *symbol = first;
/* Look for a single-letter symbol that matches. */
do
{
if (strlen (symbol->name) == 1)
return symbol;
symbol++;
}
while (symbol < symbols + num_symbols && symbol->value == value);
/* Failing that, try two-letter symbols. */
symbol = first;
do
{
if (strlen (symbol->name) == 2)
return symbol;
symbol++;
}
while (symbol < symbols + num_symbols && symbol->value == value);
return first;
}
static const struct symbol *
hint_address (const struct symbol *first, word_t value)
{
if (value < 020)
return hint_accumulator (first, value);
return first;
}
static const struct symbol *
hint_offset (const struct symbol *first, word_t value)
{
const struct symbol *symbol = first;
/* Look for a symbol with more than one letter. */
do
{
if (strlen (symbol->name) > 1)
return symbol;
symbol++;
}
while (symbol < symbols + num_symbols && symbol->value == value);
return first;
}
static const struct symbol *
hint_channel (const struct symbol *first, word_t value)
{
const struct symbol *symbol = first;
/* Look for a symbol containing CH. */
do
{
if (strstr (symbol->name, "ch"))
return symbol;
symbol++;
}
while (symbol < symbols + num_symbols && symbol->value == value);
/* Second try, symbols that end with C. */
symbol = first;
do
{
if (symbol->name[strlen (symbol->name) - 1] == 'c')
return symbol;
symbol++;
}
while (symbol < symbols + num_symbols && symbol->value == value);
return first;
}
static const struct symbol *
hint_xctr (const struct symbol *first, word_t value)
{
const struct symbol *symbol = first;
/* Look for matching symbol that begins with X. */
do
{
if (*symbol->name == 'x')
return symbol;
symbol++;
}
while (symbol < symbols + num_symbols && symbol->value == value);
return first;
}
const struct symbol *
get_symbol_by_value (word_t value, int hint)
{
struct symbol key = { NULL, value, -1, 0 };
const struct symbol *first;
if (symbols_mode == SYMBOLS_NONE || hint == HINT_NUMBER)
return NULL;
sort_by (SORT_VALUE);
first = bsearch (&key, symbols, num_symbols, sizeof *symbols,
compare_value_search);
if (first == NULL)
return NULL;
/* Wind the pointer back to find the first symbol that matches. */
while (first > symbols && (first - 1)->value == value)
first--;
switch (hint)
{
case HINT_ACCUMULATOR: first = hint_accumulator (first, value); break;
case HINT_CHANNEL: first = hint_channel (first, value); break;
case HINT_ADDRESS: first = hint_address (first, value); break;
case HINT_OFFSET: first = hint_offset (first, value); break;
case HINT_IMMEDIATE: first = hint_offset (first, value); break;
case HINT_XCTR: first = hint_xctr (first, value); break;
}
if (symbols_mode == SYMBOLS_DDT)
{
if (first != NULL && first->flags & (SYMBOL_KILLED | SYMBOL_HALFKILLED))
return NULL;
}
return first;
}
const struct symbol *
get_symbol_by_name (const char *name)
{
struct symbol key = { name, 0, -1, 0 };
const struct symbol *first;
sort_by (SORT_NAME);
first = bsearch (&key, symbols, num_symbols, sizeof *symbols,
compare_name_search);
if (first == NULL)
return NULL;
/* Wind the pointer back to find the first symbol that matches. */
while (first > symbols && strcmp ((first - 1)->name, name) == 0)
first--;
return first;
}
word_t
get_symbol_value (const char *name)
{
const struct symbol *symbol = get_symbol_by_name (name);
if (symbol == NULL)
return -1;
else
return symbol->value;
}