forked from larsbrinkhoff/pdp10-its-disassembler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfasl-file.c
361 lines (332 loc) · 10.4 KB
/
fasl-file.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
/* Copyright (C) 2022 Lars Brinkhoff <[email protected]>
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dis.h"
#include "memory.h"
#include "symbols.h"
#define NFASL 0124641635413LL /* *FASL+ */
#define OFASL 0124641635412LL /* *FASL* */
static int atom_index = 1;
static char *atomtable[1000];
static int type[1000];
static int address; //Current absolute address to load into.
static int offset; //Relocation offset.
static int
fasl_magic (word_t word)
{
return word == NFASL || word == OFASL;
}
static void
read_atomtable (FILE *f, word_t header)
{
char string[100];
int length;
word_t data;
char *p;
//fprintf (stderr, "Atomtable type %llo: ", (header >> 33) & 7);
type[atom_index] = (header >> 33) & 7;
switch (type[atom_index])
{
case 0: //Atom.
p = string;
length = header & 0777777;
while (length--)
{
data = get_word (f);
*p++ = (data >> 29) & 0177;
*p++ = (data >> 22) & 0177;
*p++ = (data >> 15) & 0177;
*p++ = (data >> 8) & 0177;
*p++ = (data >> 1) & 0177;
}
//fprintf (stderr, "Atom index %d pname \"%s\"\n", atom_index, string);
atomtable[atom_index++] = strdup (string);
string[5] = 0;
break;
case 1: //Fixnum.
data = get_word (f);
//fprintf (stderr, "Atom index %d fixnum %012llo\n", atom_index, data);
snprintf (string, sizeof string, "Fixnum %012llo", data);
atomtable[atom_index++] = strdup (string);
break;
case 2: //Flonum.
data = get_word (f);
//fprintf (stderr, "Atom index %d flonum %012llo\n", atom_index, data);
snprintf (string, sizeof string, "Flonum %012llo", data);
atomtable[atom_index++] = strdup (string);
break;
case 3: //Bignum.
length = header & 0777777;
while (length--)
data = get_word (f);
//fprintf (stderr, "Atom index %d bignum\n", atom_index);
atomtable[atom_index++] = strdup ("Bignum");
break;
case 4: //Double-precision number.
data = get_word (f);
data = get_word (f);
//fprintf (stderr, "Atom index %d double\n", atom_index);
atomtable[atom_index++] = strdup ("Double");
break;
case 5: //Complex number.
data = get_word (f);
data = get_word (f);
//fprintf (stderr, "Atom index %d complex\n", atom_index);
atomtable[atom_index++] = strdup ("Complex");
break;
case 6: //Duplex number.
data = get_word (f);
data = get_word (f);
data = get_word (f);
data = get_word (f);
//fprintf (stderr, "Atom index %d duplex\n", atom_index);
atomtable[atom_index++] = strdup ("Duplex");
break;
case 7: //Unused.
fprintf (stderr, "Bad value.\n");
exit (1);
break;
}
}
static void
load (struct pdp10_memory *memory, word_t data)
{
word_t *p = malloc (sizeof data);
*p = data;
add_memory (memory, address++, 1, p);
}
static word_t
relocate (word_t word, word_t offset, word_t mask)
{
return (word & ~mask) | ((word + offset) & mask);
}
static void
patch (struct pdp10_memory *memory, word_t data, word_t mask, const char *x)
{
word_t word = get_word_at (memory, address - 1);
word = relocate (word, data, mask);
set_word_at (memory, address - 1 , word);
(void)x;
//fprintf (stderr, "%06o/ %012llo (%s)\n", address - 1, word, x);
}
static void
discard (struct pdp10_memory *memory, word_t data)
{
(void)memory;
(void)data;
//fprintf (stderr, "Discard.\n");
}
static void
read_sexp (FILE *f, word_t data, struct pdp10_memory *memory,
void (*end) (struct pdp10_memory *, word_t))
{
int items = 0;
for (;;)
{
switch ((data >> 33) & 7)
{
case 0:
//fprintf (stderr, "Push atom %lld onto stack.\n", data & 0777777);
items++;
break;
case 2:
//fprintf (stderr, "Make a list ended by atom popped off list.\n");
items--;
// Fall through.
case 1:
//fprintf (stderr, "Pop %lld items off stack and make a list.\n", data & 0777777);
items -= data & 0777777;
items++;
break;
case 3:
//fprintf (stderr, "Evaluate top item on stack.\n");
break;
case 4:
//fprintf (stderr, "Make a hunk from %lld stack items.\n", data & 0777777);
items -= data & 0777777;
items++;
break;
case 5:
case 6:
fprintf (stderr, "Bad value.\n");
exit (1);
break;
case 7:
if (items != 1)
//fprintf (stderr, "Stack items now: %d\n", items);
switch (data >> 18)
{
case 0777777:
end (memory, data);
break;
case 0777776:
//fprintf (stderr, "Load into atomtable index %d.\n", atom_index);
atomtable[atom_index++] = "(list)";
break;
default:
fprintf (stderr, "Bad value.\n");
break;
}
if (end == load)
{
data = get_word (f);
//fprintf (stderr, "Hash key: %012llo.\n", data);
}
return;
}
data = get_word (f);
}
}
static int
read_block (FILE *f, struct pdp10_memory *memory)
{
char symbol[7];
char string[100];
word_t relocations;
word_t data, value;
int i;
relocations = get_word (f);
for (i = 0; i < 9; i++)
{
data = get_word (f);
switch ((relocations >> 32) & 017)
{
case 000:
load (memory, data);
//fprintf (stderr, "%06o/ %012llo\n", address - 1, data);
break;
case 001:
load (memory, data);
data += offset;
patch (memory, data, 0777777LL, "RELOCATABLE");
break;
case 002:
snprintf (string, sizeof string, "SPECIAL %s", atomtable[data & 0777777]);
data &= 0777777000000LL;
data |= 0 & 0777777;
load (memory, data);
break;
case 003:
snprintf (string, sizeof string, "SMASHABLE CALL %s", atomtable[data & 0777777]);
load (memory, data);
//fprintf (stderr, "%06o/ %012llo (%s)\n", address - 1, data, string);
break;
case 004:
snprintf (string, sizeof string, "QUOTED ATOM %s", atomtable[data & 0777777]);
data = (data & 0777777000000LL) | 0;
load (memory, data);
//fprintf (stderr, "%06o/ %012llo (%s)\n", address - 1, data, string);
break;
case 005:
//fprintf (stderr, "Type 05, QUOTED LIST\n");
read_sexp (f, data, memory, load);
break;
case 006:
snprintf (string, sizeof string, "GLOBALSYM index %llu", data & 0777777);
data = 0;
patch (memory, data, 0777777LL, string);
break;
case 007:
if (data == 0777777777777LL)
{
patch (memory, (word_t)offset << 18, 0777777000000LL, "LH");
}
else
{
squoze_to_ascii (data & 037777777777LL, symbol);
snprintf (string, sizeof string, "DDT SYMBOL %s", symbol);
data = 0;
patch (memory, data, 0777777LL, string);
}
break;
case 010:
snprintf (string, sizeof string, "ARRAY REFERENCE %s", atomtable[data & 0777777]);
data = 0;
patch (memory, data, 0777777LL, string);
break;
case 011:
/* Unused. */
fprintf (stderr, "Bad value.\n");
exit (1);
break;
case 012:
read_atomtable (f, data);
break;
case 013:
/*fprintf (stderr, "DEFUN %s %s",
atomtable[data >> 18], atomtable[data & 0777777]);*/
value = get_word (f);
/*fprintf (stderr, ", address %06llo args %06llo\n",
value & 0777777, value >> 18);*/
value = relocate (value & 0777777, offset, 0777777);
add_symbol (atomtable[data >> 18], value, SYMBOL_HALFKILLED);
break;
case 014:
address = relocate (data & 0777777, offset, 0777777);
break;
case 015:
squoze_to_ascii (data & 037777777777LL, symbol);
if (data & 0400000000000LL)
value = get_word (f);
else
value = address;
fprintf (stderr, "PUTDDTSYM %s = %012llo", symbol, value);
if (data & 0100000000000LL)
value = relocate (value, offset, 0777777);
if (data & 0200000000000LL)
value = relocate (value, (word_t)offset << 18, 0777777000000LL);
//fprintf (stderr, "PUTDDTSYM %s = %012llo\n", symbol, value);
fprintf (stderr, " -> %012llo\n", value);
add_symbol (symbol, value, SYMBOL_HALFKILLED);
break;
case 016:
//fprintf (stderr, "Type 05, EVAL MUNGEABLE\n");
read_sexp (f, data, memory, discard);
break;
case 017:
if (!fasl_magic (data))
fprintf (stderr, "Not a valid FASL file.\n");
return 0;
}
relocations <<= 4;
}
return 1;
}
static void
read_fasl (FILE *f, struct pdp10_memory *memory, int cpu_model)
{
char version[7];
word_t word;
(void)cpu_model;
fprintf (output_file, "FASL format\n");
word = get_word (f);
if (!fasl_magic (word))
{
fprintf (stderr, "Not a valid FASL file.\n");
exit (1);
}
word = get_word (f);
sixbit_to_ascii (word, version);
fprintf (output_file, " Lisp version: %s\n", version);
// The conventional starting address.
offset = 0100;
address = offset;
while (read_block (f, memory))
;
}
struct file_format fasl_file_format = {
"fasl",
read_fasl,
NULL
};