-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgearlance.c
463 lines (387 loc) · 10.4 KB
/
gearlance.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
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
/*
* gearlance bfjoust interpreter; based on cranklance, itself based on
* chainlance.
*
* Copyright (c) 2011-2013 Heikki Kallasjoki
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gearlance.h"
/* #define TRACE 1 */
#ifdef CRANK_IT
#define CRANK(x) x
#else
#define CRANK(x) /* no action */
#endif
int scores[2][MAXTAPE+1];
#ifdef CRANK_IT
static struct {
unsigned long long cycles;
} stats = { 0 };
static struct {
unsigned char tape_max[2][MAXTAPE];
unsigned heat_position[2][MAXTAPE];
} xstats = { {{0}}, {{0}} };
#endif
/* main application */
#ifndef NO_MAIN
int main(int argc, char *argv[])
{
/* check args */
if (argc != 3)
{
fprintf(stderr, "usage: %s prog1.bfjoust prog2.bfjoust\n", argv[0]);
return 1;
}
/* parse competitors */
int fdA = sopen(argv[1]), fdB = sopen(argv[2]);
if (setjmp(fail_buf))
{
printf("parse error: %s\n", fail_msg);
return 1;
}
struct oplist *opsA = parse(fdA), *opsB = parse(fdB);
/* for debugging purposes, dump out the parse */
#if 0
unsigned char opchars[] = {
[OP_INC] = '+', [OP_DEC] = '-', [OP_LEFT] = '<', [OP_RIGHT] = '>',
[OP_WAIT] = '.', [OP_LOOP1] = '[', [OP_LOOP2] = ']',
[OP_REP1] = '(', [OP_REP2] = ')',
[OP_IREP1] = '(', [OP_INNER1] = '{', [OP_INNER2] = '}', [OP_IREP2] = ')',
[OP_DONE] = '_',
};
for (int at = 0; at < opsA->len; at++)
{
struct op *op = &opsA->ops[at];
printf("%3d: %c ", at, opchars[op->type]);
if (op->match != -1) printf("m%-2d ", op->match); else printf(" ");
if (op->inner != -1) printf("i%-2d ", op->inner); else printf(" ");
if (op->count != -1) printf("*%d", op->count);
printf("\n");
}
return 0;
#endif
/* compile and run them */
union opcode *codeA = core(core_compile_a, opsA, 0, 0);
union opcode *codeB = core(core_compile_b, opsB, 0, 0);
core(core_run, 0, codeA, codeB);
free(codeA);
free(codeB);
/* summarize results */
CRANK(printf("SUMMARY: "));
int score = 0;
for (int pol = 0; pol < 2; pol++)
{
for (int tlen = MINTAPE; tlen <= MAXTAPE; tlen++)
{
putchar(scores[pol][tlen] ? (scores[pol][tlen] > 0 ? '<' : '>') : 'X');
score += scores[pol][tlen];
}
putchar(' ');
}
#ifdef CRANK_IT
printf("%d c%lld sl%d sr%d\n", score, stats.cycles, opsA->len, opsB->len);
#else
printf("%d\n", score);
#endif
opl_free(opsA);
opl_free(opsB);
return 0;
}
#endif /* !NO_MAIN */
/* actual interpretation, impl */
static unsigned char tape[MAXTAPE];
union opcode *core(enum core_action act, struct oplist *ops, union opcode *codeA, union opcode *codeB)
{
static void * const xtableA[OP_MAX] = {
[OP_INC] = &&op_incA, [OP_DEC] = &&op_decA,
[OP_LEFT] = &&op_leftA, [OP_RIGHT] = &&op_rightA,
[OP_WAIT] = &&op_waitA,
[OP_LOOP1] = &&op_loop1A, [OP_LOOP2] = &&op_loop2A,
[OP_REP1] = &&op_rep1A, [OP_REP2] = &&op_rep2A,
[OP_IREP1] = &&op_rep1A, [OP_INNER1] = &&op_rep2A,
[OP_INNER2] = &&op_inner2A, [OP_IREP2] = &&op_irep2A,
[OP_DONE] = &&op_doneA,
};
static void * const xtableB[OP_MAX] = {
[OP_INC] = &&op_incB, [OP_DEC] = &&op_decB,
[OP_LEFT] = &&op_leftB, [OP_RIGHT] = &&op_rightB,
[OP_WAIT] = &&op_waitB,
[OP_LOOP1] = &&op_loop1B, [OP_LOOP2] = &&op_loop2B,
[OP_REP1] = &&op_rep1B, [OP_REP2] = &&op_rep2B,
[OP_IREP1] = &&op_rep1B, [OP_INNER1] = &&op_rep2B,
[OP_INNER2] = &&op_inner2B, [OP_IREP2] = &&op_irep2B,
[OP_DONE] = &&nextcycle, /* shortcut */
};
/* compilation to threaded code */
if (act == core_compile_a || act == core_compile_b)
{
unsigned *offsets = smalloc(ops->len * sizeof *offsets);
unsigned len = 0;
for (unsigned at = 0; at < ops->len; at++)
{
offsets[at] = len;
switch (ops->ops[at].type)
{
case OP_IREP2:
len += 3;
break;
case OP_LOOP1: case OP_LOOP2: case OP_REP1: case OP_REP2: case OP_IREP1: case OP_INNER1:
len += 2;
break;
default:
len += 1;
break;
}
}
union opcode *code = smalloc(len * sizeof *code);
void * const *xtable = (act == core_compile_a ? xtableA : xtableB);
union opcode *opc = code;
for (unsigned at = 0; at < ops->len; at++)
{
struct op *op = &ops->ops[at];
opc->xt = xtable[op->type];
opc++;
switch (op->type)
{
case OP_LOOP1: case OP_LOOP2: case OP_REP2: case OP_INNER1:
opc->match = &code[offsets[op->match+1]];
opc++;
break;
case OP_REP1: case OP_IREP1:
opc->count = op->count;
opc++;
break;
case OP_IREP2:
opc->count = op->count; opc++;
opc->match = &code[offsets[op->match+1]]; opc++;
break;
default: /* no extra operands */
break;
}
}
return code;
}
/* state-holding variables */
static int repStackA[MAXNEST], repStackB[MAXNEST];
union opcode *ipA = 0, *ipB = 0;
unsigned char *ptrA = 0, *ptrB = 0, bcache = 0;
int repA = 0, repB = 0, *repSA = 0, *repSB = 0;
int deathsA = 0, deathsB = 0;
int cycles = 0;
int score = 0;
/* execute with all tape lengths and both relative polarities */
#ifdef CRANK_IT
stats.cycles = 0;
#ifndef EXECUTE_STATS
/* use old text-based statistics output for plain 'cranklance' */
#define EXECUTE_STATS(pol) \
stats.cycles += (MAXCYCLES - cycles); \
printf("CYCLES[%d,%d] = %d\n", pol, tapesize, MAXCYCLES - cycles); \
\
printf("TAPEABS[%d,%d] =", pol, tapesize); \
for (int p = 0; p < tapesize; p++) printf(" %d", tape[p] >= 128 ? tape[p]-256 : tape[p]); \
printf("\n"); \
printf("TAPEMAX[%d,%d] =", pol, tapesize); \
for (int p = 0; p < tapesize; p++) printf(" %u/%u", xstats.tape_max[0][p], xstats.tape_max[1][p]); \
printf("\n"); \
printf("TAPEHEAT[%d,%d] =", pol, tapesize); \
for (int p = 0; p < tapesize; p++) printf(" %u/%u", xstats.heat_position[0][p], xstats.heat_position[1][p]); \
printf("\n")
#endif
#endif
#define EXECUTE_ALL(sym, pol) \
ret = &&sym; \
for (tapesize = MINTAPE; tapesize <= MAXTAPE; tapesize++) \
{ \
ipA = &codeA[0], ipB = &codeB[0]; \
\
memset(tape, 0, tapesize); \
ptrA = &tape[0], ptrB = &tape[tapesize-1]; \
*ptrA = 128, *ptrB = 128; bcache = 128; \
\
repSA = repStackA, repSB = repStackB; \
deathsA = 0, deathsB = 0; \
\
cycles = MAXCYCLES; \
CRANK(memset(xstats.tape_max, 0, sizeof xstats.tape_max)); \
CRANK(memset(xstats.heat_position, 0, sizeof xstats.heat_position)); \
\
score = 0; \
goto *ipA->xt; \
sym: \
scores[pol][tapesize] = score; \
CRANK(EXECUTE_STATS(pol)); \
}
void *ret;
int tapesize = 0;
EXECUTE_ALL(done_normal, 0);
/* FIXME: try to make this loop not interpret all operands as xts */
for (union opcode *op = codeB; op->xt != xtableB[OP_DONE]; op++)
{
if (op->xt == xtableB[OP_INC]) op->xt = xtableB[OP_DEC];
else if (op->xt == xtableB[OP_DEC]) op->xt = xtableB[OP_INC];
}
EXECUTE_ALL(done_flipped, 1);
return 0;
/* actual core */
#define NEXTA ipA++; goto *ipB->xt
#define NEXTB ipB++; goto nextcycle
/* #define TRACE 1 */
nextcycle:
cycles--;
if (!tape[0]) deathsA++; else if (deathsA == 1) deathsA = 0;
if (!tape[tapesize-1]) deathsB++; else if (deathsB == 1) deathsB = 0;
#ifdef TRACE
printf("%6d: ", MAXCYCLES-1-cycles);
for (int i = 0; i < tapesize; i++)
printf("%c%02x ",
(ptrA - tape) == i
? ((ptrB - tape) == i ? 'X' : 'A')
: ((ptrB - tape) == i ? 'B' : ' '),
tape[i]);
printf(" dA %d dB %d ipA %td ipB %td repA %d repB %d\n", deathsA, deathsB, ipA-codeA, ipB-codeB, repA, repB);
#endif
if (deathsA >= 2 && deathsB >= 2)
goto *ret;
else if (deathsA >= 2)
{
score--;
goto *ret;
}
else if (deathsB >= 2)
{
score++;
goto *ret;
}
#ifdef CRANK_IT
xstats.heat_position[0][ptrA-tape]++;
xstats.heat_position[1][ptrB-tape]++;
#endif
if (!cycles)
goto *ret;
bcache = *ptrB;
goto *ipA->xt;
fallA:
deathsA = 2;
goto *ipB->xt;
fallB:
if (!tape[0]) deathsA++;
if (deathsA >= 2)
goto *ret;
score++;
goto *ret;
op_incA:
(*ptrA)++;
NEXTA;
op_incB:
(*ptrB)++;
NEXTB;
op_decA:
(*ptrA)--;
NEXTA;
op_decB:
(*ptrB)--;
NEXTB;
#ifdef CRANK_IT
#define MAXSTAT(ptr,i) { \
unsigned char max = *ptr >= 128 ? 256 - *ptr : *ptr; \
if (max > xstats.tape_max[i][ptr-tape]) xstats.tape_max[i][ptr-tape] = max; }
#endif
op_leftA:
CRANK(MAXSTAT(ptrA, 0));
ptrA--;
if (ptrA < tape) goto fallA;
NEXTA;
op_leftB:
CRANK(MAXSTAT(ptrB, 1));
ptrB++;
if (ptrB >= &tape[tapesize]) goto fallB;
NEXTB;
op_rightA:
CRANK(MAXSTAT(ptrA, 0));
ptrA++;
if (ptrA >= &tape[tapesize]) goto fallA;
NEXTA;
op_rightB:
CRANK(MAXSTAT(ptrB, 1));
ptrB--;
if (ptrB < tape) goto fallB;
NEXTB;
op_waitA:
NEXTA;
op_waitB:
NEXTB;
op_loop1A:
ipA++;
if (!*ptrA) ipA = ipA->match - 1;
NEXTA;
op_loop1B:
ipB++;
if (!bcache) ipB = ipB->match - 1;
NEXTB;
op_loop2A:
ipA++;
if (*ptrA) ipA = ipA->match - 1;
NEXTA;
op_loop2B:
ipB++;
if (bcache) ipB = ipB->match - 1;
NEXTB;
/* simple (..) repeats with no corresponding {..} block */
op_rep1A:
ipA++;
*repSA++ = repA; repA = ipA->count;
goto *(++ipA)->xt;
op_rep1B:
ipB++;
*repSB++ = repB; repB = ipB->count;
goto *(++ipB)->xt;
op_rep2A:
ipA++;
if (--repA) ipA = ipA->match - 1;
else repA = *--repSA;
goto *(++ipA)->xt;
op_rep2B:
ipB++;
if (--repB) ipB = ipB->match - 1;
else repB = *--repSB;
goto *(++ipB)->xt;
/* complex (..{ and }..) repeats; count in different dirs in }..) */
op_inner2A:
*repSA++ = repA; repA = 1;
goto *(++ipA)->xt;
op_inner2B:
*repSB++ = repB; repB = 1;
goto *(++ipB)->xt;
op_irep2A:
ipA += 3;
if (++repA <= ipA[-2].count) ipA = ipA[-1].match;
else repA = *--repSA;
goto *ipA->xt;
op_irep2B:
ipB += 3;
if (++repB <= ipB[-2].count) ipB = ipB[-1].match;
else repB = *--repSB;
goto *ipB->xt;
op_doneA:
goto *ipB->xt;
}