-
Notifications
You must be signed in to change notification settings - Fork 39
/
serial.c
588 lines (500 loc) · 12 KB
/
serial.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/// @file
#include "serial.h"
#include <assert.h>
#include <stdlib.h>
static void*
_oom(const char* cap, void* v)
{
if ( !v ) {
fprintf(stderr,
"ur: hashcons: %s: allocation failed, out of memory\r\n", cap);
abort();
}
return v;
}
static inline void
_bsw_atom(ur_root_t *r, ur_nref ref, ur_bsw_t *bsw, uint64_t len)
{
switch ( ur_nref_tag(ref) ) {
default: assert(0);
case ur_direct: return ur_bsw_atom64(bsw, len, ref);
case ur_iatom: {
uint8_t *byt = r->atoms.bytes[ur_nref_idx(ref)];
return ur_bsw_atom_bytes(bsw, len, byt);
}
}
}
/*
** define opaque struct ur_jam_s (ie, ur_jam_t)
*/
struct ur_jam_s {
ur_root_t *r;
ur_walk_fore_t *w;
ur_dict64_t dict;
ur_bsw_t bsw;
};
static void
_jam_atom(ur_root_t *r, ur_nref ref, void *ptr)
{
ur_jam_t *j = ptr;
ur_dict64_t *dict = &j->dict;
ur_bsw_t *bsw = &j->bsw;
uint64_t bak, len = ur_met(r, 0, ref);
if ( !ur_dict64_get(r, dict, ref, &bak) ) {
ur_dict64_put(r, dict, ref, bsw->bits);
_bsw_atom(r, ref, bsw, len);
}
else {
uint64_t len_bak = ur_met0_64(bak);
if ( len <= len_bak ) {
_bsw_atom(r, ref, bsw, len);
}
else {
ur_bsw_back64(bsw, len_bak, bak);
}
}
}
static ur_bool_t
_jam_cell(ur_root_t *r, ur_nref ref, void *ptr)
{
ur_jam_t *j = ptr;
ur_dict64_t *dict = &j->dict;
ur_bsw_t *bsw = &j->bsw;
uint64_t bak;
if ( !ur_dict64_get(r, dict, ref, &bak) ) {
ur_dict64_put(r, dict, ref, bsw->bits);
ur_bsw_cell(bsw);
return 1;
}
else {
ur_bsw_back64(bsw, ur_met0_64(bak), bak);
return 0;
}
}
static uint64_t
_jam(ur_jam_t *j,
ur_nref ref,
uint64_t *len,
uint8_t **byt)
{
ur_bsw_init(&j->bsw, ur_fib11, ur_fib12);
ur_walk_fore_with(j->w, ref, j, _jam_atom, _jam_cell);
return ur_bsw_done(&j->bsw, len, byt);
}
ur_jam_t*
ur_jam_init_with(ur_root_t *r,
uint64_t d_prev,
uint64_t d_size,
uint32_t s_prev,
uint32_t s_size)
{
ur_jam_t *j = _oom("jam_init", calloc(sizeof(*j), 1));
j->w = ur_walk_fore_init_with(r, s_prev, s_size);
j->r = r;
ur_dict64_grow(r, &j->dict, d_prev, d_size);
return j;
}
ur_jam_t*
ur_jam_init(ur_root_t *r)
{
return ur_jam_init_with(r, ur_fib11, ur_fib12, // dict sizes
ur_fib10, ur_fib11); // stack sizes
}
uint64_t
ur_jam_with(ur_jam_t *j,
ur_nref ref,
uint64_t *len,
uint8_t **byt)
{
uint64_t bits = _jam(j, ref, len, byt);
ur_dict64_wipe(&j->dict);
return bits;
}
void
ur_jam_done(ur_jam_t *j)
{
ur_dict_free((ur_dict_t*)&j->dict);
ur_walk_fore_done(j->w);
free(j);
}
uint64_t
ur_jam(ur_root_t *r,
ur_nref ref,
uint64_t *len,
uint8_t **byt)
{
ur_jam_t *j = ur_jam_init(r);
uint64_t bits = _jam(j, ref, len, byt);
ur_jam_done(j);
return bits;
}
/*
** stack frame for recording head vs tail iteration
**
** $? [CUE_HEAD bits=@]
** [hed=* bits=@]
*/
#define CUE_HEAD 0xffffffffffffffffULL
typedef struct _cue_frame_s {
uint64_t ref;
uint64_t bits;
} _cue_frame_t;
typedef struct _cue_stack_s {
uint32_t prev;
uint32_t size;
uint32_t fill;
_cue_frame_t* f;
} _cue_stack_t;
static inline ur_cue_res_e
_cue_next(ur_root_t *r,
_cue_stack_t *s,
ur_bsr_t *bsr,
ur_dict64_t *dict,
ur_nref *out)
{
while ( 1 ) {
uint64_t len, bits = bsr->bits;
ur_cue_tag_e tag;
ur_cue_res_e res;
if ( ur_cue_good != (res = ur_bsr_tag(bsr, &tag)) ) {
return res;
}
switch ( tag ) {
default: assert(0);
case ur_jam_cell: {
// reallocate the stack if full
//
if ( s->fill == s->size ) {
uint32_t next = s->prev + s->size;
s->f = _oom("cue_next stack", realloc(s->f, next * sizeof(*s->f)));
s->prev = s->size;
s->size = next;
}
// save a head-frame and read the head from the stream
//
{
_cue_frame_t* f = &(s->f[s->fill++]);
f->ref = CUE_HEAD;
f->bits = bits;
}
continue;
}
case ur_jam_back: {
if ( ur_cue_good != (res = ur_bsr_rub_len(bsr, &len)) ) {
return res;
}
else if ( 62 < len ) {
return ur_cue_meme;
}
else {
uint64_t val, bak = ur_bsr64_any(bsr, len);
if ( !ur_dict64_get(r, dict, bak, &val) ) {
return ur_cue_back;
}
*out = (ur_nref)val;
return ur_cue_good;
}
}
case ur_jam_atom: {
if ( ur_cue_good != (res = ur_bsr_rub_len(bsr, &len)) ) {
return res;
}
else if ( 62 >= len ) {
*out = (ur_nref)ur_bsr64_any(bsr, len);
}
else {
uint64_t len_byt = (len >> 3) + !!ur_mask_3(len);
uint8_t *byt = _oom("cue_next bytes", calloc(len_byt, 1));
ur_bsr_bytes_any(bsr, len, byt);
// strip trailing zeroes
//
while ( len_byt && !byt[len_byt - 1] ) {
len_byt--;
}
*out = ur_coin_bytes_unsafe(r, len_byt, byt);
}
ur_dict64_put(r, dict, bits, (uint64_t)*out);
return ur_cue_good;
}
}
}
}
/*
** define opaque struct ur_cue_s (ie, ur_cue_t)
*/
struct ur_cue_s {
ur_root_t *r;
_cue_stack_t s;
ur_dict64_t dict;
};
static ur_cue_res_e
_cue(ur_cue_t *c,
uint64_t len,
const uint8_t *byt,
ur_nref *out)
{
ur_bsr_t bsr = {0};
ur_root_t *r = c->r;
_cue_stack_t *s = &c->s;
ur_dict64_t *dict = &c->dict;
ur_cue_res_e res;
ur_nref ref;
// init bitstream-reader
//
if ( ur_cue_good != (res = ur_bsr_init(&bsr, len, byt)) ) {
return res;
}
// bit-cursor (and backreferences) must fit in 62-bit direct atoms
//
else if ( 0x7ffffffffffffffULL < len ) {
return ur_cue_meme;
}
// advance into stream
//
res = _cue_next(r, s, &bsr, dict, &ref);
// process result
//
while ( s->fill && (ur_cue_good == res) ) {
// peek at the top of the stack
//
_cue_frame_t *f = &(s->f[s->fill - 1]);
// f is a head-frame; stash result and read the tail from the stream
//
if ( CUE_HEAD == f->ref ) {
f->ref = ref;
res = _cue_next(r, s, &bsr, dict, &ref);
}
// f is a tail-frame; pop the stack and continue
//
else {
ref = ur_cons(r, f->ref, ref);
ur_dict64_put(r, dict, f->bits, (uint64_t)ref);
s->fill--;
}
}
if ( ur_cue_good == res ) {
*out = ref;
}
return res;
}
ur_cue_t*
ur_cue_init_with(ur_root_t *r,
uint64_t d_prev,
uint64_t d_size,
uint32_t s_prev,
uint32_t s_size)
{
ur_cue_t* c = _oom("cue_init", calloc(sizeof(*c), 1));
c->r = r;
ur_dict64_grow(r, &c->dict, d_prev, d_size);
c->s.prev = s_prev;
c->s.size = s_size;
c->s.f = _oom("cue_test_init", malloc(s_size * sizeof(*c->s.f)));
return c;
}
ur_cue_t*
ur_cue_init(ur_root_t *r)
{
return ur_cue_init_with(r, ur_fib11, ur_fib12, // dict sizes
ur_fib10, ur_fib11); // stack sizes
}
ur_cue_res_e
ur_cue_with(ur_cue_t *c,
uint64_t len,
const uint8_t *byt,
ur_nref *out)
{
ur_cue_res_e res = _cue(c, len, byt, out);
// XX check size, shrink if above threshold
//
ur_dict64_wipe(&c->dict);
c->s.fill = 0;
return res;
}
void
ur_cue_done(ur_cue_t *c)
{
ur_dict_free((ur_dict_t*)&c->dict);
free(c->s.f);
free(c);
}
ur_cue_res_e
ur_cue(ur_root_t *r,
uint64_t len,
const uint8_t *byt,
ur_nref *out)
{
ur_cue_t *c = ur_cue_init(r);
ur_cue_res_e res = _cue(c, len, byt, out);
ur_cue_done(c);
return res;
}
/*
** stack frame for recording head vs tail iteration
**
** [hed=? bits=@]
**
*/
typedef struct _cue_test_frame_s {
ur_bool_t tal;
uint64_t bits;
} _cue_test_frame_t;
typedef struct _cue_test_stack_s {
uint32_t prev;
uint32_t size;
uint32_t fill;
_cue_test_frame_t* f;
} _cue_test_stack_t;
static inline ur_cue_res_e
_cue_test_next(_cue_test_stack_t *s,
ur_bsr_t *bsr,
ur_dict_t *dict)
{
while ( 1 ) {
uint64_t len, bits = bsr->bits;
ur_cue_tag_e tag;
ur_cue_res_e res;
if ( ur_cue_good != (res = ur_bsr_tag(bsr, &tag)) ) {
return res;
}
switch ( tag ) {
default: assert(0);
case ur_jam_cell: {
// reallocate the stack if full
//
if ( s->fill == s->size ) {
uint32_t next = s->prev + s->size;
s->f = _oom("cue_test", realloc(s->f, next * sizeof(*s->f)));
s->prev = s->size;
s->size = next;
}
// save a head-frame and read the head from the stream
//
{
_cue_test_frame_t* f = &(s->f[s->fill++]);
f->tal = 0;
f->bits = bits;
}
continue;
}
case ur_jam_back: {
if ( ur_cue_good != (res = ur_bsr_rub_len(bsr, &len)) ) {
return res;
}
else if ( 62 < len ) {
return ur_cue_meme;
}
else {
uint64_t bak = ur_bsr64_any(bsr, len);
return ur_dict_get((ur_root_t*)0, dict, bak)
? ur_cue_good
: ur_cue_back;
}
}
case ur_jam_atom: {
if ( ur_cue_good != (res = ur_bsr_rub_len(bsr, &len)) ) {
return res;
}
ur_bsr_skip_any(bsr, len);
ur_dict_put((ur_root_t*)0, dict, bits);
return ur_cue_good;
}
}
}
}
/*
** define opaque struct ur_cue_test_s (ie, ur_cue_test_t)
*/
struct ur_cue_test_s {
_cue_test_stack_t s;
ur_dict_t dict;
};
static ur_cue_res_e
_cue_test(ur_cue_test_t *t,
uint64_t len,
const uint8_t *byt)
{
ur_bsr_t bsr = {0};
_cue_test_stack_t *s = &t->s;
ur_dict_t *dict = &t->dict;
ur_cue_res_e res;
// init bitstream-reader
//
if ( ur_cue_good != (res = ur_bsr_init(&bsr, len, byt)) ) {
return res;
}
// bit-cursor (and backreferences) must fit in 62-bit direct atoms
//
else if ( 0x7ffffffffffffffULL < len ) {
return ur_cue_meme;
}
// advance into stream
//
res = _cue_test_next(s, &bsr, dict);
// process result
//
while ( s->fill && (ur_cue_good == res) ) {
// peek at the top of the stack
//
_cue_test_frame_t *f = &(s->f[s->fill - 1]);
// f is a head-frame; stash result and read the tail from the stream
//
if ( !f->tal ) {
f->tal = 1;
res = _cue_test_next(s, &bsr, dict);
}
// f is a tail-frame; pop the stack and continue
//
else {
ur_dict_put((ur_root_t*)0, dict, f->bits);
s->fill--;
}
}
return res;
}
ur_cue_test_t*
ur_cue_test_init_with(uint64_t d_prev,
uint64_t d_size,
uint32_t s_prev,
uint32_t s_size)
{
ur_cue_test_t* t = _oom("cue_test_init", calloc(sizeof(*t), 1));
ur_dict_grow((ur_root_t*)0, &t->dict, d_prev, d_size);
t->s.prev = s_prev;
t->s.size = s_size;
t->s.f = _oom("cue_test_init", malloc(s_size * sizeof(*t->s.f)));
return t;
}
ur_cue_test_t*
ur_cue_test_init(void)
{
return ur_cue_test_init_with(ur_fib11, ur_fib12, // dict sizes
ur_fib10, ur_fib11); // stack sizes
}
ur_bool_t
ur_cue_test_with(ur_cue_test_t *t,
uint64_t len,
const uint8_t *byt)
{
ur_bool_t ret = ur_cue_good == _cue_test(t, len, byt);
// XX check size, shrink if above threshold
//
ur_dict_wipe(&t->dict);
t->s.fill = 0;
return ret;
}
void
ur_cue_test_done(ur_cue_test_t *t)
{
ur_dict_free(&t->dict);
free(t->s.f);
free(t);
}
ur_bool_t
ur_cue_test(uint64_t len, const uint8_t *byt)
{
ur_cue_test_t *t = ur_cue_test_init();
ur_bool_t ret = ur_cue_good == _cue_test(t, len, byt);
ur_cue_test_done(t);
return ret;
}