forked from urbit/vere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimprison.c
768 lines (644 loc) · 14.1 KB
/
imprison.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/// @file
#include "imprison.h"
#include "jets/k.h"
#include "jets/q.h"
#include "manage.h"
#include "retrieve.h"
#include "trace.h"
#include "xtract.h"
/* _ci_slab_size(): calculate slab bloq-size, checking for overflow.
*/
static c3_w
_ci_slab_size(c3_g met_g, c3_d len_d)
{
c3_d bit_d = len_d << met_g;
c3_d wor_d = (bit_d + 0x1f) >> 5;
c3_w wor_w = (c3_w)wor_d;
if ( (wor_w != wor_d)
|| (len_d != (bit_d >> met_g)) )
{
return (c3_w)u3m_bail(c3__fail);
}
return wor_w;
}
/* _ci_slab_init(): initialize slab with heap allocation.
** NB: callers must ensure [len_w] >0
*/
static void
_ci_slab_init(u3i_slab* sab_u, c3_w len_w)
{
c3_w* nov_w = u3a_walloc(len_w + c3_wiseof(u3a_atom));
u3a_atom* vat_u = (void *)nov_w;
vat_u->mug_w = 0;
vat_u->len_w = len_w;
#ifdef U3_MEMORY_DEBUG
u3_assert( len_w );
#endif
sab_u->_._vat_u = vat_u;
sab_u->buf_w = vat_u->buf_w;
sab_u->len_w = len_w;
}
/* _ci_slab_grow(): update slab with heap reallocation.
*/
static void
_ci_slab_grow(u3i_slab* sab_u, c3_w len_w)
{
c3_w* old_w = (void*)sab_u->_._vat_u;
// XX implement a more efficient u3a_wealloc()
//
c3_w* nov_w = u3a_wealloc(old_w, len_w + c3_wiseof(u3a_atom));
u3a_atom* vat_u = (void *)nov_w;
vat_u->len_w = len_w;
sab_u->_._vat_u = vat_u;
sab_u->buf_w = vat_u->buf_w;
sab_u->len_w = len_w;
}
/* _ci_atom_mint(): finalize a heap-allocated atom at specified length.
*/
static u3_atom
_ci_atom_mint(u3a_atom* vat_u, c3_w len_w)
{
c3_w* nov_w = (void*)vat_u;
if ( 0 == len_w ) {
u3a_wfree(nov_w);
return (u3_atom)0;
}
else if ( 1 == len_w ) {
c3_w dat_w = *vat_u->buf_w;
if ( c3y == u3a_is_cat(dat_w) ) {
u3a_wfree(nov_w);
return (u3_atom)dat_w;
}
}
// try to strip a block off the end
//
{
c3_w old_w = vat_u->len_w;
if ( old_w > len_w ) {
c3_y wiz_y = c3_wiseof(u3a_atom);
u3a_wtrim(nov_w, old_w + wiz_y, len_w + wiz_y);
}
}
vat_u->len_w = len_w;
return u3a_to_pug(u3a_outa(nov_w));
}
/* u3i_slab_init(): configure bloq-length slab, zero-initialize.
*/
void
u3i_slab_init(u3i_slab* sab_u, c3_g met_g, c3_d len_d)
{
u3i_slab_bare(sab_u, met_g, len_d);
u3t_on(mal_o);
memset(sab_u->buf_y, 0, (size_t)sab_u->len_w * 4);
u3t_off(mal_o);
}
/* u3i_slab_bare(): configure bloq-length slab, uninitialized.
*/
void
u3i_slab_bare(u3i_slab* sab_u, c3_g met_g, c3_d len_d)
{
u3t_on(mal_o);
{
c3_w wor_w = _ci_slab_size(met_g, len_d);
// if we only need one word, use the static storage in [sab_u]
//
if ( (0 == wor_w) || (1 == wor_w) ) {
sab_u->_._vat_u = 0;
sab_u->buf_w = &sab_u->_._sat_w;
sab_u->len_w = 1;
}
// allocate an indirect atom
//
else {
_ci_slab_init(sab_u, wor_w);
}
}
u3t_off(mal_o);
}
/* u3i_slab_from(): configure bloq-length slab, initialize with [a].
*/
void
u3i_slab_from(u3i_slab* sab_u, u3_atom a, c3_g met_g, c3_d len_d)
{
u3i_slab_bare(sab_u, met_g, len_d);
// copies [a], zero-initializes any additional space
//
u3r_words(0, sab_u->len_w, sab_u->buf_w, a);
// if necessary, mask off extra most-significant bits
// from most-significant word
//
if ( (5 > met_g) && (u3r_met(5, a) >= sab_u->len_w) ) {
// NB: overflow already checked in _ci_slab_size()
//
c3_d bit_d = len_d << met_g;
c3_w wor_w = bit_d >> 5;
c3_w bit_w = bit_d & 0x1f;
if ( bit_w ) {
sab_u->buf_w[wor_w] &= ((c3_w)1 << bit_w) - 1;
}
}
}
/* u3i_slab_grow(): resize slab, zero-initializing new space.
*/
void
u3i_slab_grow(u3i_slab* sab_u, c3_g met_g, c3_d len_d)
{
c3_w old_w = sab_u->len_w;
u3t_on(mal_o);
{
c3_w wor_w = _ci_slab_size(met_g, len_d);
// XX actually shrink?
//
if ( wor_w <= old_w ) {
sab_u->len_w = wor_w;
}
else {
// upgrade from static storage
//
if ( 1 == old_w ) {
c3_w dat_w = *sab_u->buf_w;
_ci_slab_init(sab_u, wor_w);
sab_u->buf_w[0] = dat_w;
}
// reallocate
//
else {
_ci_slab_grow(sab_u, wor_w);
}
{
c3_y* buf_y = (void*)(sab_u->buf_w + old_w);
size_t dif_i = wor_w - old_w;
memset(buf_y, 0, dif_i * 4);
}
}
}
u3t_off(mal_o);
}
/* u3i_slab_free(): dispose memory backing slab.
*/
void
u3i_slab_free(u3i_slab* sab_u)
{
c3_w len_w = sab_u->len_w;
u3a_atom* vat_u = sab_u->_._vat_u;
u3t_on(mal_o);
if ( 1 == len_w ) {
u3_assert( !vat_u );
}
else {
c3_w* tav_w = (sab_u->buf_w - c3_wiseof(u3a_atom));
u3_assert( tav_w == (c3_w*)vat_u );
u3a_wfree(vat_u);
}
u3t_off(mal_o);
}
/* u3i_slab_mint(): produce atom from slab, trimming.
*/
u3_atom
u3i_slab_mint(u3i_slab* sab_u)
{
c3_w len_w = sab_u->len_w;
u3a_atom* vat_u = sab_u->_._vat_u;
u3_atom pro;
u3t_on(mal_o);
if ( 1 == len_w ) {
c3_w dat_w = *sab_u->buf_w;
u3_assert( !vat_u );
u3t_off(mal_o);
pro = u3i_word(dat_w);
u3t_on(mal_o);
}
else {
u3a_atom* vat_u = sab_u->_._vat_u;
c3_w* tav_w = (sab_u->buf_w - c3_wiseof(u3a_atom));
u3_assert( tav_w == (c3_w*)vat_u );
// trim trailing zeros
//
while ( len_w && !(sab_u->buf_w[len_w - 1]) ) {
len_w--;
}
pro = _ci_atom_mint(vat_u, len_w);
}
u3t_off(mal_o);
return pro;
}
/* u3i_slab_moot(): produce atom from slab, no trimming.
*/
u3_atom
u3i_slab_moot(u3i_slab* sab_u)
{
c3_w len_w = sab_u->len_w;
u3_atom pro;
u3t_on(mal_o);
if ( 1 == len_w) {
c3_w dat_w = *sab_u->buf_w;
u3_assert( !sab_u->_._vat_u );
u3t_off(mal_o);
pro = u3i_word(dat_w);
u3t_on(mal_o);
}
else {
u3a_atom* vat_u = sab_u->_._vat_u;
c3_w* tav_w = (sab_u->buf_w - c3_wiseof(u3a_atom));
u3_assert( tav_w == (c3_w*)vat_u );
pro = _ci_atom_mint(vat_u, len_w);
}
u3t_off(mal_o);
return pro;
}
/* u3i_word(): construct u3_atom from c3_w.
*/
u3_atom
u3i_word(c3_w dat_w)
{
u3_atom pro;
u3t_on(mal_o);
if ( c3y == u3a_is_cat(dat_w) ) {
pro = (u3_atom)dat_w;
}
else {
c3_w* nov_w = u3a_walloc(1 + c3_wiseof(u3a_atom));
u3a_atom* vat_u = (void *)nov_w;
vat_u->mug_w = 0;
vat_u->len_w = 1;
vat_u->buf_w[0] = dat_w;
pro = u3a_to_pug(u3a_outa(nov_w));
}
u3t_off(mal_o);
return pro;
}
/* u3i_chub(): construct u3_atom from c3_d.
*/
u3_atom
u3i_chub(c3_d dat_d)
{
if ( c3y == u3a_is_cat(dat_d) ) {
return (u3_atom)dat_d;
}
else {
c3_w dat_w[2] = {
dat_d & 0xffffffffULL,
dat_d >> 32
};
return u3i_words(2, dat_w);
}
}
/* u3i_bytes(): Copy [a] bytes from [b] to an LSB first atom.
*/
u3_atom
u3i_bytes(c3_w a_w,
const c3_y* b_y)
{
// strip trailing zeroes.
//
while ( a_w && !b_y[a_w - 1] ) {
a_w--;
}
if ( !a_w ) {
return (u3_atom)0;
}
else {
u3i_slab sab_u;
u3i_slab_bare(&sab_u, 3, a_w);
u3t_on(mal_o);
{
// zero-initialize last word
//
sab_u.buf_w[sab_u.len_w - 1] = 0;
memcpy(sab_u.buf_y, b_y, a_w);
}
u3t_off(mal_o);
return u3i_slab_moot_bytes(&sab_u);
}
}
/* u3i_words(): Copy [a] words from [b] into an atom.
*/
u3_atom
u3i_words(c3_w a_w,
const c3_w* b_w)
{
// strip trailing zeroes.
//
while ( a_w && !b_w[a_w - 1] ) {
a_w--;
}
if ( !a_w ) {
return (u3_atom)0;
}
else {
u3i_slab sab_u;
u3i_slab_bare(&sab_u, 5, a_w);
u3t_on(mal_o);
memcpy(sab_u.buf_w, b_w, (size_t)4 * a_w);
u3t_off(mal_o);
return u3i_slab_moot(&sab_u);
}
}
/* u3i_chubs(): Copy [a] chubs from [b] into an atom.
*/
u3_atom
u3i_chubs(c3_w a_w,
const c3_d* b_d)
{
// strip trailing zeroes.
//
while ( a_w && !b_d[a_w - 1] ) {
a_w--;
}
if ( !a_w ) {
return (u3_atom)0;
}
else if ( 1 == a_w ) {
return u3i_chub(b_d[0]);
}
else {
u3i_slab sab_u;
u3i_slab_bare(&sab_u, 6, a_w);
u3t_on(mal_o);
{
c3_w* buf_w = sab_u.buf_w;
c3_w i_w;
c3_d i_d;
for ( i_w = 0; i_w < a_w; i_w++ ) {
i_d = b_d[i_w];
*buf_w++ = i_d & 0xffffffffULL;
*buf_w++ = i_d >> 32;
}
}
u3t_off(mal_o);
return u3i_slab_mint(&sab_u);
}
}
/* u3i_mp(): Copy the GMP integer [a] into an atom, and clear it.
*/
u3_atom
u3i_mp(mpz_t a_mp)
{
size_t siz_i = mpz_sizeinbase(a_mp, 2);
u3i_slab sab_u;
u3i_slab_init(&sab_u, 0, siz_i);
mpz_export(sab_u.buf_w, 0, -1, sizeof(c3_w), 0, 0, a_mp);
mpz_clear(a_mp);
// per the mpz_export() docs:
//
// > If op is non-zero then the most significant word produced
// > will be non-zero.
//
return u3i_slab_moot(&sab_u);
}
/* u3i_vint(): increment [a].
*/
u3_atom
u3i_vint(u3_noun a)
{
u3_assert(u3_none != a);
if ( _(u3a_is_cat(a)) ) {
return ( a == 0x7fffffff ) ? u3i_word(a + 1) : (a + 1);
}
else if ( _(u3a_is_cell(a)) ) {
return u3m_bail(c3__exit);
}
else {
mpz_t a_mp;
u3r_mp(a_mp, a);
u3z(a);
mpz_add_ui(a_mp, a_mp, 1);
return u3i_mp(a_mp);
}
}
/* u3i_defcons(): allocate cell for deferred construction.
** NB: [hed] and [tel] pointers MUST be filled.
*/
u3_cell
u3i_defcons(u3_noun** hed, u3_noun** tel)
{
u3_noun pro;
u3t_on(mal_o);
{
c3_w* nov_w = u3a_celloc();
u3a_cell* nov_u = (void *)nov_w;
nov_u->mug_w = 0;
#ifdef U3_MEMORY_DEBUG
nov_u->hed = u3_none;
nov_u->tel = u3_none;
#endif
*hed = &nov_u->hed;
*tel = &nov_u->tel;
pro = u3a_to_pom(u3a_outa(nov_w));
}
u3t_off(mal_o);
return pro;
}
/* u3i_cell(): Produce the cell `[a b]`.
*/
u3_noun
u3i_cell(u3_noun a, u3_noun b)
{
u3_noun pro;
u3t_on(mal_o);
{
c3_w* nov_w = u3a_celloc();
u3a_cell* nov_u = (void *)nov_w;
nov_u->mug_w = 0;
nov_u->hed = a;
nov_u->tel = b;
pro = u3a_to_pom(u3a_outa(nov_w));
}
u3t_off(mal_o);
return pro;
}
/* u3i_trel(): Produce the triple `[a b c]`.
*/
u3_noun
u3i_trel(u3_noun a, u3_noun b, u3_noun c)
{
return u3i_cell(a, u3i_cell(b, c));
}
/* u3i_qual(): Produce the cell `[a b c d]`.
*/
u3_noun
u3i_qual(u3_noun a, u3_noun b, u3_noun c, u3_noun d)
{
return u3i_cell(a, u3i_trel(b, c, d));
}
/* u3i_string(): Produce an LSB-first atom from the C string [a].
*/
u3_atom
u3i_string(const c3_c* a_c)
{
return u3i_bytes(strlen(a_c), (c3_y *)a_c);
}
/* u3i_tape(): from a C string, to a list of bytes.
*/
u3_noun
u3i_tape(const c3_c* txt_c)
{
if ( !*txt_c ) {
return u3_nul;
} else return u3i_cell(*txt_c, u3i_tape(txt_c + 1));
}
/* u3i_list(): list from `u3_none`-terminated varargs.
*/
u3_noun
u3i_list(u3_weak som, ...)
{
u3_noun lit = u3_nul;
va_list ap;
if ( u3_none == som ) {
return lit;
}
else {
lit = u3nc(som, lit);
}
{
u3_noun tem;
va_start(ap, som);
while ( 1 ) {
if ( u3_none == (tem = va_arg(ap, u3_weak)) ) {
break;
}
else {
lit = u3nc(tem, lit);
}
}
va_end(ap);
}
return u3kb_flop(lit);
}
/* u3i_edit():
**
** Mutate `big` at axis `axe` with new value `som`.
** `axe` is RETAINED.
*/
u3_noun
u3i_edit(u3_noun big, u3_noun axe, u3_noun som)
{
u3_noun pro;
u3_noun* out = &pro;
switch ( axe ) {
case 0: return u3m_bail(c3__exit);
case 1: break;
default: {
c3_w dep_w = u3r_met(0, u3x_atom(axe)) - 2;
const c3_w* axe_w = ( c3y == u3a_is_cat(axe) )
? &axe
: ((u3a_atom*)u3a_to_ptr(axe))->buf_w;
do {
u3a_cell* big_u = u3a_to_ptr(big);
u3_noun* old = (u3_noun*)&(big_u->hed);
const c3_y bit_y = 1 & (axe_w[dep_w >> 5] >> (dep_w & 31));
if ( c3n == u3a_is_cell(big) ) {
return u3m_bail(c3__exit);
}
else if ( c3y == u3a_is_mutable(u3R, big) ) {
*out = big;
out = &(old[bit_y]);
big = *out;
big_u->mug_w = 0;
}
else {
u3_noun luz = big;
u3_noun* new[2];
*out = u3i_defcons(&new[0], &new[1]);
out = new[bit_y];
big = u3k(old[bit_y]);
*(new[!bit_y]) = u3k(old[!bit_y]);
u3z(luz);
}
}
while ( dep_w-- );
}
}
u3z(big);
*out = som;
return pro;
}
/* u3i_molt():
**
** Mutate `som` with a 0-terminated list of axis, noun pairs.
** Axes must be cats (31 bit).
*/
struct _molt_pair {
c3_w axe_w;
u3_noun som;
};
static c3_w
_molt_cut(c3_w len_w,
struct _molt_pair* pms_m)
{
c3_w i_w, cut_t, cut_w;
cut_t = 0;
cut_w = 0;
for ( i_w = 0; i_w < len_w; i_w++ ) {
c3_w axe_w = pms_m[i_w].axe_w;
if ( (cut_t == 0) && (3 == u3x_cap(axe_w)) ) {
cut_t = 1;
cut_w = i_w;
}
pms_m[i_w].axe_w = u3x_mas(axe_w);
}
return cut_t ? cut_w : i_w;
}
static u3_noun // transfer
_molt_apply(u3_noun som, // retain
c3_w len_w,
struct _molt_pair* pms_m) // transfer
{
if ( len_w == 0 ) {
return u3k(som);
}
else if ( (len_w == 1) && (1 == pms_m[0].axe_w) ) {
return pms_m[0].som;
}
else {
c3_w cut_w = _molt_cut(len_w, pms_m);
if ( c3n == u3a_is_cell(som) ) {
return u3m_bail(c3__exit);
}
else {
return u3i_cell
(_molt_apply(u3a_h(som), cut_w, pms_m),
_molt_apply(u3a_t(som), (len_w - cut_w), (pms_m + cut_w)));
}
}
}
u3_noun
u3i_molt(u3_noun som, ...)
{
va_list ap;
c3_w len_w;
struct _molt_pair* pms_m;
u3_noun pro;
// Count.
//
len_w = 0;
{
va_start(ap, som);
while ( 1 ) {
if ( 0 == va_arg(ap, c3_w) ) {
break;
}
va_arg(ap, u3_weak*);
len_w++;
}
va_end(ap);
}
u3_assert( 0 != len_w );
pms_m = alloca(len_w * sizeof(struct _molt_pair));
// Install.
//
{
c3_w i_w;
va_start(ap, som);
for ( i_w = 0; i_w < len_w; i_w++ ) {
pms_m[i_w].axe_w = va_arg(ap, c3_w);
pms_m[i_w].som = va_arg(ap, u3_noun);
}
va_end(ap);
}
// Apply.
//
pro = _molt_apply(som, len_w, pms_m);
u3z(som);
return pro;
}