-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgearlanced.c
298 lines (260 loc) · 8.89 KB
/
gearlanced.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
/*
* gearlanced bfjoust interpreter; based on gearlance, itself based on
* cranklance, itself based on chainlance.
*
* This one is for maintaining a hill of bfjoust programs. You invoke
* it as "./gearlanced N", where N is a positive integer denoting the
* hill size. The hill will be initialized with N copies of a
* hypothetical program that will always lose. After initialization,
* the program will read commands from stdin, and reply results to
* stdout. This "GearTalk" protocol works as follows.
*
* The caller must send an 8-byte command header, potentially (for
* some commands) followed by bfjoust program code. The program
* replies with a four-byte reply header, followed by reply data.
*
* The first byte of the command header specifies the action to
* perform: test (0x01), set (0x02) or unset (0x03). For set and
* unset, the following three bytes are a hill position index as an
* unsigned little-endian 24-bit integer; the test command does not
* use these bytes and they should be set to 0. For test and set, the
* last 4 bytes are the length of the following program code, as an
* unsigned little-endian 32-bit integer; the unset command does not
* take a program, so these bytes should be set to 0.
*
* The first byte of the reply header contains flags. Bit 0 (least
* significant) is the success flag: if unset, an error occurred. In
* this case, the other three bytes are the length of the error
* message, again in unsigned 24-bit little-endian format. The error
* message text follows the reply header.
*
* If the command succeeded, the bit is set. Other flag bits and
* reply data depend on the command.
*
* The test command (0x01) runs the program (as the "right" program)
* against all the programs currently on the hill (as "left"
* programs). The other three reply header bytes indicate the
* parameters of the hill: the minimum (10) and maximum (30) tape
* lengths and (the low 8 bits of) the hill size, respectively. Let C
* be the number of tape configurations (max - min + 1, normally 21)
* and N be the hill size.
*
* The reply header is followed by N chunks of results. For
* gearlanced, the reply chunk has 2*C (normally 42) bytes, each
* containing the signed 8-bit value -1 (for a loss of the tested
* "right" program), 0 (for a tie) or +1 (for a win). The first half
* are for the sieve (normal) polarity runs in order of increasing
* tape length. The second half are for kettle (inverted) polarity.
*
* If the binary is compiled as cranklanced, bit 1 of the reply flags
* will be set to indicate this. In this case, before the normal
* points there will also be 2*C statistics chunks, in the same order
* as the points. Letting T be the tape length of each configuration,
* the statistics chunks are 4+19*T bytes, with the following fields:
*
* - 4 bytes, 32le: number of executed cycles
* - T bytes: absolute value (0..128) of tape at the end of joust
* - 2*T bytes: largest value on tape when moving away from cell
* - 2*4*T bytes: heatmap of how long the program stayed in cell
* - 2*4*T bytes: heatmap of how often the program modified the cell
*
* For all the tape maps that have 2*T elements, the first half are
* the statistics for the "left" program, and the second half for the
* "right". In both cases the tape is unflipped, i.e., as seen from
* the perspective of the left program.
*
* The total reply data without statistics is 2*C*N (42*N) bytes, and with statistics ...*N (...*N) bytes.
*
* The set (0x01) and unset (0x02) either set or blank the indicated
* program. A blanked program loses immediately: this is used for
* efficiency reasons on the program that's being replaced on the
* hill. The reply header contains only the success flag (or an
* error).
*
* An EOF condition in place of a command will terminate the program.
*/
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "gearlance.h"
/* geartalk utilities */
#define ACTION_TEST(x) (((x) & 0xff) == 0x01)
#define ACTION_SET(x) (((x) & 0xff) == 0x02)
#define ACTION_UNSET(x) (((x) & 0xff) == 0x03)
static inline uint32_t get_u32le(unsigned char *p) {
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3]) << 24;
}
static inline void put_u32le(unsigned char *p, uint32_t u) {
p[0] = u; p[1] = u >> 8; p[2] = u >> 16; p[3] = u >> 24;
}
int read_n(unsigned char *buf, size_t count) {
size_t left = count;
while (left) {
ssize_t ret = read(0, buf, left);
if (ret < 0) {
perror("read");
abort();
} else if (ret == 0 && left < count) {
fputs("short read\n", stderr);
abort();
} else if (ret == 0) {
return 0;
}
left -= ret;
}
return 1;
}
void write_n(const unsigned char *buf, size_t count) {
while (count) {
ssize_t wrote = write(1, buf, count);
if (wrote <= 0) abort();
count -= wrote;
buf += wrote;
}
}
void write_error(char *fmt, ...) {
unsigned char reply[4 + 256];
va_list ap;
va_start(ap, fmt);
int len = vsnprintf((char*)reply + 4, sizeof reply - 4, fmt, ap);
va_end(ap);
put_u32le(reply, (uint32_t)len << 8);
write_n(reply, 4 + len);
}
/* statistics output code for cranklanced */
#ifdef CRANK_IT
#define EXECUTE_STATS(pol) do { \
geartalk_Statistics s = Statistics_init_default; \
s.has_cycles = true; \
s.cycles = MAXCYCLES - cycles; \
s.has_tape_abs = true; s.tape_abs.size = tapesize; \
for (int p = 0; p < tapesize; p++) s.tape_abs.bytes[p] = tape[p]; \
s.has_tape_max = true; s.tape_max.size = 2 * tapesize; \
for (int p = 0; p < tapesize; p++) { \
s.tape_max.bytes[p] = xstats.tape_max[0][p]; \
s.tape_max.bytes[tapesize+p] = xstats.tape_max[1][p]; \
} \
s.heat_position_count = 2 * tapesize; \
for (int p = 0; p < tapesize; p++) { \
s.heat_position[p] = xstats.heat_position[0][p]; \
s.heat_position[tapesize+p] = xstats.heat_position[1][p]; \
} \
pb_put(Statistics_fields, &s); \
} while (0)
#endif
/* main application for gearlanced/cranklanced */
static union opcode *readprog(enum core_action act, uint32_t len)
{
if (setjmp(fail_buf))
{
write_error("parse error: %s\n", fail_msg);
return 0;
}
union opcode *code = 0;
struct oplist *ops = parse(&len);
if (len == 0) code = core(act, ops, 0, 0);
else write_error("parse did not consume entire program");
opl_free(ops);
return code;
}
int main(int argc, char *argv[])
{
/* grok arguments */
if (argc != 2)
{
fprintf(stderr, "usage: %s N\n", argv[0]);
return 1;
}
unsigned hillsize = (unsigned)strtol(argv[1], 0, 10);
if (hillsize == 0 || hillsize > 1000)
{
fprintf(stderr, "sorry, hillsize %u looks too suspicious\n", hillsize);
return 1;
}
/* construct initial hill */
union opcode **hill = smalloc(hillsize * sizeof *hill);
for (unsigned i = 0; i < hillsize; i++)
hill[i] = 0;
/* main loop */
unsigned char cmd[8];
while (read_n(cmd, 8))
{
uint32_t action = get_u32le(cmd);
unsigned char reply[4] = {0x01, 0x00, 0x00, 0x00};
if (ACTION_TEST(action))
{
union opcode *code = readprog(core_compile_b, get_u32le(cmd + 4));
if (!code)
continue; /* error reply already sent */
#ifdef CRANK_IT
reply[0] |= 0x02;
#endif
reply[1] = MINTAPE;
reply[2] = MAXTAPE;
reply[3] = hillsize;
write_n(reply, sizeof reply);
for (unsigned i = 0; i < hillsize; i++)
{
if (!hill[i])
{
// TODO: fake statistics for cranklance
for (unsigned pol = 0; pol < 2; pol++)
for (unsigned tlen = MINTAPE; tlen <= MAXTAPE; tlen++)
scores[pol][tlen] = 1; // always a win
}
else
core(core_run, 0, hill[i], code);
int8_t reply_points[2][NTAPES];
/*
* Executing a program will, as a side effect, flip the polarity by rewriting
* the opcodes. As a result, this result-parsing loop has to keep switching
* the mapping of which block of scores truly corresponds to sieve vs. kettle.
*/
int sieve = i % 2, kettle = !sieve;
for (unsigned tlen = MINTAPE; tlen <= MAXTAPE; tlen++)
{
reply_points[0][tlen - MINTAPE] = -scores[sieve][tlen];
reply_points[1][tlen - MINTAPE] = -scores[kettle][tlen];
}
write_n((unsigned char*)&reply_points[0][0], sizeof reply_points);
}
}
else if (ACTION_SET(action))
{
uint32_t index = action >> 8;
if (index >= hillsize)
{
write_error("invalid index for set: %u", (unsigned)index);
continue;
}
union opcode *code = readprog(core_compile_a, get_u32le(cmd + 4));
if (!code)
continue; /* error reply already sent */
free(hill[index]);
hill[index] = code;
write_n(reply, sizeof reply);
}
else if (ACTION_UNSET(action))
{
uint32_t index = action >> 8;
if (index >= hillsize)
{
write_error("invalid index for unset: %u", (unsigned)index);
continue;
}
free(hill[index]);
hill[index] = 0;
write_n(reply, sizeof reply);
}
else
{
write_error("unknown action: 0x%04x", (unsigned)action);
}
}
return 0;
}