-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump.c
More file actions
470 lines (431 loc) · 9.47 KB
/
dump.c
File metadata and controls
470 lines (431 loc) · 9.47 KB
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
/* dump.c -- dump es's internal state as a c program ($Revision: 1.1.1.1 $) */
#include "es.h"
#include "stdenv.h"
#define MAXVARNAME 20
/*
* the $&dumpstate prints the appropriate C data structures for
* representing the parts of es's memory that can be stored in
* the text (read-only) segment of the program. (some liberties
* are taken with regard to what the initial.es routines can do
* regarding changing lexically bound values in order that more
* things can be here.)
*
* since these things are read-only they cannot point to structures
* that need to be garbage collected. (think of this like a very
* old generation in a generational collector.)
*
* to simplify matters, all values are stored in C variables with
* idiosyncratic names:
* S_string "string"
* X_address string at address, when name wouldn't fit
* L_address List at address
* E_address Term at address
* T_address Tree at address
* B_address Binding at address
* C_address Closure at address
*
* in order that addresses are internally consistent, garbage collection
* is disabled during the dumping process.
*/
static Dict *cvars, *strings;
/* nstreq and deepequal are taken from wryun/es-shell.
* formatting has been changed to p9 style (more or less)
*/
Boolean
nstreq(char *a, char *b)
{
if(!a && !b)
return TRUE;
if(!a || !b)
return FALSE;
return streq(a, b);
}
Boolean
deepequal(Tree *t1, Tree *t2)
{
if(!t1 && !t2)
return TRUE;
if(!t1 || !t2)
return FALSE;
if(t1->kind != t2->kind)
return FALSE;
switch(t1->kind) {
case nWord:
case nQword:
case nPrim:
return nstreq(t1->u[0].s, t2->u[0].s);
case nCall:
case nThunk:
case nVar:
case nRegex:
return deepequal(t1->u[0].p, t2->u[0].p);
case nAssign:
case nConcat:
case nClosure:
case nFor:
case nLambda:
case nLet:
case nLets:
case nList:
case nLocal:
case nVarsub:
case nMatch:
case nExtract:
return deepequal(t1->u[0].p, t2->u[0].p) && deepequal(t1->u[1].p, t2->u[1].p);
default:
panic("deepequal: bad node kind %d", t1->kind);
break;
}
return FALSE;
}
void
treededup(void *arg, char *unused, void *v)
{
Tree **new = arg;
Tree *old = v;
used(unused);
if(deepequal(*new, old))
*new = old;
}
static Boolean
allprintable(const char *s)
{
int c;
for(; (c = *(unsigned char *)s) != '\0'; s++)
if(!isprint(c) || c == '"' || c == '\\')
return FALSE;
return TRUE;
}
static char *
dumpstring(char *string)
{
char *name;
if(string == NULL)
return "NULL";
name = dictget(strings, string);
if(name == NULL) {
name = str("S_%F", string);
if(strlen(name) > MAXVARNAME)
name = str("X_%ulx", string);
print("static const char %s[] = ", name);
if(allprintable(string))
print("\"%s\";\n", string);
else {
int c;
char *s;
print("{ ");
for(s = string; (c = *(unsigned char *)s) != '\0'; s++) {
switch(c) {
case '\a':
print("'\\a'");
break;
case '\b':
print("'\\b'");
break;
case '\f':
print("'\\f'");
break;
case '\n':
print("'\\n'");
break;
case '\r':
print("'\\r'");
break;
case '\t':
print("'\\t'");
break;
case '\'':
print("'\\''");
break;
case '\\':
print("'\\\\'");
break;
default:
print(isprint(c) ? "'%c'" : "%d", c);
break;
}
print(", ");
}
print("'\\0', };\n");
}
strings = dictput(strings, string, name);
}
return name;
}
static char *dumplist(List *list);
static const char *
nodename(NodeKind k)
{
switch(k) {
default:
panic("nodename: bad node kind %d", k);
case nAssign:
return "Assign";
case nCall:
return "Call";
case nClosure:
return "Closure";
case nConcat:
return "Concat";
case nFor:
return "For";
case nLambda:
return "Lambda";
case nLet:
return "Let";
case nLets:
return "Lets";
case nList:
return "List";
case nLocal:
return "Local";
case nMatch:
return "Match";
case nExtract:
return "Extract";
case nPrim:
return "Prim";
case nQword:
return "Qword";
case nRegex:
return "Regex";
case nThunk:
return "Thunk";
case nVar:
return "Var";
case nVarsub:
return "Varsub";
case nWord:
return "Word";
}
}
char *
dumptree(Tree *tree)
{
char *name;
if(tree == NULL)
return "NULL";
/* dictforall(cvars, treededup, &tree); */
name = str("&T_%ulx", tree);
if(dictget(cvars, name) == NULL) {
switch(tree->kind) {
default:
panic("dumptree: bad node kind %d", tree->kind);
case nWord:
case nQword:
case nPrim:
print("static const Tree_s %s = { n%s, { { (char *) %s } } };\n", name + 1,
nodename(tree->kind), dumpstring(tree->u[0].s));
break;
case nCall:
case nThunk:
case nVar:
case nRegex:
print("static const Tree_p %s = { n%s, { { (Tree *) %s } } };\n", name + 1,
nodename(tree->kind), dumptree(tree->u[0].p));
break;
case nAssign:
case nConcat:
case nClosure:
case nFor:
case nLambda:
case nLet:
case nList:
case nLocal:
case nVarsub:
case nMatch:
case nExtract:
case nLets:
print("static const Tree_pp %s = { n%s, { { (Tree *) %s }, { (Tree *) %s } } };\n",
name + 1, nodename(tree->kind), dumptree(tree->u[0].p), dumptree(tree->u[1].p));
}
cvars = dictput(cvars, name, tree);
}
return name;
}
static char *
dumpbinding(Binding *binding)
{
char *name;
if(binding == NULL)
return "NULL";
name = str("&B_%ulx", binding);
if(dictget(cvars, name) == NULL) {
print("static const Binding %s = { (char *) %s, (List *) %s, (Binding *) %s };\n", name + 1,
dumpstring(binding->name), dumplist(binding->defn), dumpbinding(binding->next));
cvars = dictput(cvars, name, binding);
}
return name;
}
static char *
dumpclosure(Closure *closure)
{
char *name;
if(closure == NULL)
return "NULL";
name = str("&C_%ulx", closure);
if(dictget(cvars, name) == NULL) {
print("static const Closure %s = { (Binding *) %s, (Tree *) %s };\n", name + 1,
dumpbinding(closure->binding), dumptree(closure->tree));
cvars = dictput(cvars, name, closure);
}
return name;
}
char *
termtype(Term *term)
{
switch(term->kind) {
case tkRegex:
return "tkRegex";
case tkString:
return "tkString";
case tkClosure:
return "tkClosure";
case tkDict:
return "tkDict";
case tkObject:
return "tkObject";
default:
panic("invalid term kind: %d\n", term->kind);
break;
}
}
static char *
dumpterm(Term *term)
{
char *name;
char *dstring;
if(term == NULL)
return "NULL";
name = str("&E_%ulx", term);
if(dictget(cvars, name) == NULL) {
switch(term->kind) {
default:
unreachable();
break;
case tkString:
case tkRegex:
dstring = estrdup(dumpstring(term->str));
break;
case tkClosure:
dstring = estrdup(dumpclosure(term->closure));
break;
}
print("static const Term %s = {%s, ttNone, ", name + 1, termtype(term));
switch(term->kind) {
default:
unreachable();
break;
case tkString:
case tkRegex:
print("{.str = (char*) %s}", dstring);
break;
case tkClosure:
print("{.closure = (Closure*) %s}", dstring);
break;
}
efree(dstring);
print("};\n");
cvars = dictput(cvars, name, term);
}
return name;
}
static char *
dumplist(List *list)
{
char *name;
if(list == NULL)
return "NULL";
name = str("&L_%ulx", list);
if(dictget(cvars, name) == NULL) {
print("static const List %s = { (Term *) %s, (List *) %s };\n", name + 1,
dumpterm(list->term), dumplist(list->next));
cvars = dictput(cvars, name, list);
}
return name;
}
static void
dumpvar(void *ignore, char *key, void *value)
{
Var *var = value;
dumpstring(key);
dumplist(var->defn);
}
static void
dumpdef(char *name, Var *var)
{
print("\t{ %s, (const List *) %s },\n", dumpstring(name), dumplist(var->defn));
}
static void
dumpfunctions(void *ignore, char *key, void *value)
{
if(hasprefix(key, "fn-"))
dumpdef(key, value);
}
static void
dumpsettors(void *ignore, char *key, void *value)
{
if(hasprefix(key, "set-"))
dumpdef(key, value);
}
static void
dumpvariables(void *ignore, char *key, void *value)
{
if(!hasprefix(key, "fn-") && !hasprefix(key, "set-"))
dumpdef(key, value);
}
#define TreeTypes \
typedef struct { \
NodeKind k; \
struct { \
char *s; \
} u[1]; \
} Tree_s; \
typedef struct { \
NodeKind k; \
struct { \
Tree *p; \
} u[1]; \
} Tree_p; \
typedef struct { \
NodeKind k; \
struct { \
Tree *p; \
} u[2]; \
} Tree_pp;
TreeTypes
#define PPSTRING(s) STRING(s)
static void
printheader(List *title)
{
if(offsetof(Tree, u[0].s) != offsetof(Tree_s, u[0].s) ||
offsetof(Tree, u[0].p) != offsetof(Tree_p, u[0].p) ||
offsetof(Tree, u[0].p) != offsetof(Tree_pp, u[0].p) ||
offsetof(Tree, u[1].p) != offsetof(Tree_pp, u[1].p))
panic("dumpstate: Tree union sizes do not match struct sizes");
print("/* autogenerated file! do not edit! */\n\n#include \"es.h\"\n\n", title, " ");
print("%s\n\n", PPSTRING(TreeTypes));
}
extern void
runinitial(void)
{
List *title = runfd(0, "system.es", 0);
gcdisable();
cvars = mkdict();
strings = mkdict();
printheader(title);
dictforall(vars, dumpvar, NULL);
/* these must be assigned in this order, or things just won't work */
print("\nstatic const struct { const char *name; const List *value; } defs[] = {\n");
dictforall(vars, dumpfunctions, NULL);
dictforall(vars, dumpsettors, NULL);
dictforall(vars, dumpvariables, NULL);
print("\t{ NULL, NULL }\n");
print("};\n\n");
print("\nextern void runinitial(void) {\n");
print("\tint i;\n");
print("\tfor (i = 0; defs[i].name != NULL; i++)\n");
print("\t\tvardef((char *) defs[i].name, NULL, (List *) defs[i].value);\n");
print("}\n");
esexit(0);
}