forked from unya/usim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syms.c
232 lines (191 loc) · 3.94 KB
/
syms.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
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include "usim.h"
struct sym_s {
struct sym_s *next;
char *name;
unsigned int v;
int mtype;
};
struct symtab_s {
char *name;
struct sym_s *syms;
int sym_count;
struct sym_s **sorted_syms;
};
struct symtab_s sym_prom;
struct symtab_s sym_mcr;
static void
_sym_add(struct symtab_s *tab, int memory, char *name, int v)
{
struct sym_s *s;
s = (struct sym_s *) malloc(sizeof(struct sym_s));
if (s) {
tab->sym_count++;
s->name = strdup(name);
s->v = v;
s->mtype = memory;
s->next = tab->syms;
tab->syms = s;
}
}
static char *
_sym_find_by_val(struct symtab_s *tab, int memory, int v)
{
struct sym_s *s;
for (s = tab->syms; s; s = s->next) {
if (s->v == v && s->mtype == memory)
return s->name;
}
return 0;
}
char *
_sym_find_last(struct symtab_s *tab, int memory, int v, int *poffset)
{
int i;
struct sym_s *s;
if (tab->sorted_syms == 0)
return 0;
for (i = 0; i < tab->sym_count; i++) {
s = tab->sorted_syms[i];
if (s->mtype != memory)
continue;
if (v == s->v) {
*poffset = 0;
return s->name;
}
if (v < s->v || i == tab->sym_count - 1) {
while (tab->sorted_syms[i - 1]->mtype != memory)
i--;
s = tab->sorted_syms[i - 1];
*poffset = v - s->v;
return s->name;
}
}
return 0;
}
int
_sym_find(struct symtab_s *tab, char *name, int *pval)
{
struct sym_s *s;
for (s = tab->syms; s; s = s->next) {
if (strcasecmp(name, s->name) == 0) {
*pval = s->v;
return 0;
}
}
return -1;
}
// Read a CADR MCR symbol file.
int
_sym_read_file(struct symtab_s *tab, const char *filename)
{
int first = 0;
FILE *f;
char line[8 * 1024];
f = fopen(filename, "r");
if (f == NULL)
return -1;
tab->name = strdup(filename);
fgets(line, sizeof(line), f);
fgets(line, sizeof(line), f);
fgets(line, sizeof(line), f);
while (fgets(line, sizeof(line), f) != NULL) {
char sym[64];
char symtype[64];
int loc;
int n;
if (first) {
strcpy(line, line + 3);
first = 0;
}
n = sscanf(line, "%s %s %o", sym, symtype, &loc);
if (n == 3) {
n = 0;
if (strcmp(symtype, "I-MEM") == 0)
n = 1;
if (strcmp(symtype, "D-MEM") == 0)
n = 2;
if (strcmp(symtype, "A-MEM") == 0)
n = 4;
if (strcmp(symtype, "M-MEM") == 0)
n = 5;
if (strcmp(symtype, "NUMBER") == 0)
n = 6;
if (n == 0)
printf("? %s", symtype);
_sym_add(tab, n, sym, loc);
}
}
fclose(f);
return 0;
}
static int
_sym_loc_compare(const void *p1, const void *p2)
{
struct sym_s *s1 = *(struct sym_s **) p1;
struct sym_s *s2 = *(struct sym_s **) p2;
if (s1->v < s2->v)
return -1;
if (s1->v > s2->v)
return 1;
return 0;
}
int
_sym_sort(struct symtab_s *tab)
{
struct sym_s *s;
int i;
// Make vector of pointers to symbols.
tab->sorted_syms = (struct sym_s **) malloc(sizeof(struct sym_s *) * tab->sym_count);
if (tab->sorted_syms == 0)
return -1;
// Fill in vector.
i = 0;
for (s = tab->syms; s; s = s->next) {
tab->sorted_syms[i++] = s;
}
printf("[%s] sort %d symbols (originally %d)\n", tab->name, i, tab->sym_count);
// Sort the vector...
qsort((void *) tab->sorted_syms, tab->sym_count, sizeof(void *), _sym_loc_compare);
return 0;
}
void
read_sym_files(void)
{
_sym_read_file(&sym_prom, promsym_filename);
_sym_sort(&sym_prom);
_sym_read_file(&sym_mcr, mcrsym_filename);
_sym_sort(&sym_mcr);
}
int
sym_find(int mcr, char *name, int *pval)
{
if (mcr)
return _sym_find(&sym_mcr, name, pval);
return _sym_find(&sym_prom, name, pval);
}
char *
sym_find_by_val(int mcr, int v)
{
if (mcr)
return _sym_find_by_val(&sym_mcr, 1, v);
return _sym_find_by_val(&sym_prom, 1, v);
}
char *
sym_find_by_type_val(int mcr, int t, int v)
{
if (mcr)
return _sym_find_by_val(&sym_mcr, t, v);
return _sym_find_by_val(&sym_prom, t, v);
}
char *
sym_find_last(int mcr, int v, int *poffset)
{
if (mcr)
return _sym_find_last(&sym_mcr, 1, v, poffset);
return _sym_find_last(&sym_prom, 1, v, poffset);
}