forked from urbit/vere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjets.c
2403 lines (2156 loc) · 53.2 KB
/
jets.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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// @file
#include "jets.h"
#include "allocate.h"
#include "hashtable.h"
#include "imprison.h"
#include "jets/k.h"
#include "jets/q.h"
#include "log.h"
#include "manage.h"
#include "nock.h"
#include "options.h"
#include "retrieve.h"
#include "serial.h"
#include "trace.h"
#include "urcrypt.h"
#include "vortex.h"
#include "xtract.h"
/** Functions.
**/
/* _cj_count(): count and link dashboard entries.
*/
static c3_w
_cj_count(u3j_core* par_u, u3j_core* dev_u)
{
c3_w len_l = 0;
c3_w i_w;
if ( dev_u ) {
for ( i_w = 0; 0 != dev_u[i_w].cos_c; i_w++ ) {
u3j_core* kid_u = &dev_u[i_w];
if ( par_u ) {
kid_u->par_u = par_u;
}
len_l += _cj_count(kid_u, kid_u->dev_u);
}
}
return 1 + len_l;
}
/* _cj_core_loc(): location noun from u3j_core.
*/
static u3_noun
_cj_core_loc(u3_noun pel, u3j_core* cop_u)
{
c3_w i_w;
u3_noun nam = u3i_string(cop_u->cos_c),
huc = u3_nul,
pat;
if ( cop_u->huc_u ) {
for ( i_w = 0; 0 != cop_u->huc_u[i_w].nam_c; ++i_w ) {
u3j_hood* huc_u = &(cop_u->huc_u[i_w]);
u3_noun fol = ( c3n == huc_u->kic_o )
? u3nc(0, huc_u->axe_l)
: u3nt(9, huc_u->axe_l, u3nc(0,
(0 == huc_u->sax_l) ? 1 : huc_u->sax_l));
huc = u3kdb_put(huc, u3i_string(huc_u->nam_c), fol);
}
}
pat = ( 0 == cop_u->axe_l )
? u3nt(c3y, c3y, pel)
: ( (3 == cop_u->axe_l) && (c3y == u3h(u3h(pel))) )
? u3nt(c3y, c3n, pel)
: u3nt(c3n, cop_u->axe_l, pel);
return u3nt(pat, nam, huc);
}
/* _cj_hash(): noun from pasted-in battery hash
*/
static u3_noun
_cj_hash(c3_c* has_c)
{
c3_w i_w, len_w = strlen(has_c);
if ( 64 != len_w ) {
u3l_log("bash not 64 characters: %s", has_c);
u3_assert(0);
}
u3_assert( 64 == len_w );
c3_y dig_y[32];
for ( i_w = 0; i_w < 64; ) {
c3_y hi_y = has_c[i_w++],
lo_y = has_c[i_w++],
hid_y = hi_y >= 'a' ? (hi_y - 'a') + 10 : hi_y - '0',
lod_y = lo_y >= 'a' ? (lo_y - 'a') + 10 : lo_y - '0';
dig_y[32-(i_w>>1)] = hid_y << 4 | lod_y;
}
u3_noun pro = u3i_bytes(32, dig_y);
return pro;
}
// in the jam jet file
c3_w* u3qe_jam_buf(u3_noun, c3_w* bit_w);
/* _cj_bash(): battery hash. RETAIN.
*/
static u3_noun
_cj_bash(u3_noun bat)
{
if ( u3C.wag_w & u3o_hashless ) {
return u3_nul;
}
u3_weak pro;
u3a_road* rod_u = u3R;
while ( 1 ) {
pro = u3h_get(rod_u->jed.bas_p, bat);
if ( u3_none != pro ) {
break;
}
if ( rod_u->par_p ) {
rod_u = u3to(u3_road, rod_u->par_p);
}
else {
u3i_slab sab_u;
c3_w bit_w = u3s_jam_fib(&sab_u, bat);
c3_w met_w = (bit_w + 0x7) >> 3;
// XX assumes little-endian
//
c3_y* fat_y = sab_u.buf_y;
c3_y dig_y[32];
urcrypt_shay(fat_y, met_w, dig_y);
pro = u3i_bytes(32, dig_y);
u3h_put(u3R->jed.bas_p, bat, u3k(pro));
u3i_slab_free(&sab_u);
break;
}
}
return pro;
}
/* _cj_mine_par_old(): register hooks and parent location within existing
* axis in ancestor list or u3_none.
*/
static u3_weak
_cj_mine_par_old(u3_noun lan, u3_noun axe, u3_noun pel, u3_noun loc)
{
u3_noun par;
u3_weak pro;
if ( u3_nul == lan ) {
pro = u3_none;
u3z(axe); u3z(pel); u3z(loc);
}
else if ( c3y == u3r_sing(axe, u3h(par = u3h(lan))) ) {
u3_noun lol = u3kdb_put(u3k(u3t(par)), pel, loc),
rap = u3nc(axe, lol);
pro = u3nc(rap, u3k(u3t(lan)));
}
else {
u3_weak nex = _cj_mine_par_old(u3k(u3t(lan)), axe, pel, loc);
if ( u3_none == nex ) {
pro = u3_none;
}
else {
pro = u3nc(u3k(par), nex);
}
}
u3z(lan);
return pro;
}
/* _cj_mine_par_new(): insert ancestor within lan at sorted index.
*/
static u3_noun
_cj_mine_par_new(u3_noun lan, u3_noun axe, u3_noun pel, u3_noun loc)
{
u3_weak pro;
if ( (u3_nul == lan) || (c3y == u3qa_lth(axe, u3h(u3h(lan)))) ) {
u3_noun par = u3nc(axe, u3kdb_put(u3_nul, pel, loc));
pro = u3nc(par, lan);
}
else {
pro = u3nc(u3k(u3h(lan)),
_cj_mine_par_new(u3k(u3t(lan)), axe, pel, loc));
u3z(lan);
}
return pro;
}
/* _cj_mine_par(): register a location as an ancestor
* in a list of ancestors.
*/
static u3_noun
_cj_mine_par(u3_noun lan, u3_noun axe, u3_noun pel, u3_noun loc)
{
u3_weak old = _cj_mine_par_old(u3k(lan), u3k(axe), u3k(pel), u3k(loc));
if ( u3_none != old ) {
u3z(lan); u3z(axe); u3z(pel); u3z(loc);
return old;
}
else {
return _cj_mine_par_new(lan, axe, pel, loc);
}
}
/* _cj_gust(): add location to registry.
*/
static u3_noun
_cj_gust(u3_weak reg, u3_noun axe, u3_noun pel, u3_noun loc)
{
u3_noun ger;
if ( u3_none == reg ) {
reg = u3nc(u3_nul, u3_nul);
}
ger = ( 0 == axe )
? u3nc(u3kdb_put(u3k(u3h(reg)), pel, loc), u3k(u3t(reg)))
: u3nc(u3k(u3h(reg)), _cj_mine_par(u3k(u3t(reg)), axe, pel, loc));
u3z(reg);
return ger;
}
/* _cj_axis(): axis from formula, or 0. `fol` is RETAINED.
*/
static c3_l
_cj_axis(u3_noun fol)
{
u3_noun p_fol, q_fol, r_fol;
while ( _(u3du(fol)) && (11 == u3h(fol)) )
{ fol = u3t(u3t(fol)); }
if ( !_(u3r_trel(fol, &p_fol, &q_fol, &r_fol)) ) {
if ( !_(u3r_cell(fol, &p_fol, &q_fol)) ||
(0 != p_fol) ||
(!_(u3a_is_cat(q_fol))) )
{
u3l_log("axis: bad a");
return 0;
}
return q_fol;
}
else {
if ( 9 != p_fol )
{ u3l_log("axis: bad b"); return 0; }
if ( !_(u3a_is_cat(q_fol)) )
{ u3l_log("axis: bad c"); return 0; }
if ( !_(u3du(r_fol)) || (0 != u3h(r_fol)) || (1 != u3t(r_fol)) )
{ u3l_log("axis: bad d"); return 0; }
return q_fol;
}
}
/* _cj_warm_hump(): generate axis-to-arm map. RETAIN.
*/
static u3_noun
_cj_warm_hump(c3_l jax_l, u3_noun huc)
{
u3_noun hap = u3_nul;
u3j_core* cop_u;
/* Compute axes of all correctly declared arms.
*/
if ( jax_l && (cop_u = &u3D.ray_u[jax_l])->arm_u ) {
u3j_harm* jet_u;
c3_l i_l;
for ( i_l = 0; (jet_u = &cop_u->arm_u[i_l])->fcs_c; i_l++ ) {
c3_l axe_l = 0;
if ( '.' == *(jet_u->fcs_c) ) {
c3_d axe_d = 0;
if ( (1 != sscanf(jet_u->fcs_c+1, "%" SCNu64, &axe_d)) ||
axe_d >> 32ULL ||
((1 << 31) & (axe_l = (c3_w)axe_d)) ||
(axe_l < 2) )
{
u3l_log("jets: activate: bad fcs %s", jet_u->fcs_c);
}
}
else {
u3_noun nam = u3i_string(jet_u->fcs_c);
u3_noun fol = u3kdb_get(u3k(huc), nam);
if ( u3_none == fol ) {
u3l_log("jets: activate: bad fcs %s", jet_u->fcs_c);
}
else {
axe_l = _cj_axis(fol);
u3z(fol);
}
}
if ( 0 != axe_l ) {
hap = u3kdb_put(hap, axe_l, i_l);
}
}
}
return hap;
}
/* _cj_install(): install dashboard entries.
*/
static c3_w
_cj_install(u3j_core* ray_u, c3_w jax_l, u3_noun pel, u3_noun lab, u3j_core* dev_u)
{
c3_w i_w;
u3_assert(u3R == &(u3H->rod_u));
if ( dev_u ) {
for ( i_w = 0; 0 != dev_u[i_w].cos_c; i_w++ ) {
u3j_core* kid_u = &dev_u[i_w];
u3_noun loc = _cj_core_loc(u3k(pel), kid_u),
bal = u3nc(u3k(u3h(u3t(loc))), u3k(lab));
kid_u->jax_l = jax_l;
ray_u[jax_l] = *kid_u;
if ( kid_u->bas_u ) {
c3_w j_w;
for ( j_w = 0; 0 != kid_u->bas_u[j_w]; j_w++ ) {
u3_noun key = _cj_hash(kid_u->bas_u[j_w]),
hot = u3h_get(u3R->jed.hot_p, key),
old = ( u3_none == hot ) ? u3_none : u3k(u3h(hot)),
reg = _cj_gust(old, kid_u->axe_l, u3k(pel), u3k(loc)),
huc = u3t(u3t(loc)),
hap = _cj_warm_hump(jax_l, huc),
toh = u3nq(reg, jax_l, hap, u3k(bal));
u3h_put(u3R->jed.hot_p, key, toh);
u3z(key); u3z(hot);
}
}
jax_l = _cj_install(ray_u, ++jax_l, loc, bal, kid_u->dev_u);
}
}
u3z(pel);
u3z(lab);
return jax_l;
}
#if 0
/* _cj_by_gut(): (~(get by a) b), unifying; RETAINS a, b, AND result.
*/
static u3_weak
_cj_by_gut(u3_noun a, u3_noun b)
{
if ( u3_nul == a ) {
return u3_none;
}
else {
u3_noun l_a, n_a, r_a;
u3_noun pn_a, qn_a;
u3x_trel(a, &n_a, &l_a, &r_a);
u3x_cell(n_a, &pn_a, &qn_a);
{
if ( (c3y == u3r_sing(b, pn_a)) ) {
return qn_a;
}
else {
if ( c3y == u3qc_gor(b, pn_a) ) {
return _cj_by_gut(l_a, b);
}
else return _cj_by_gut(r_a, b);
}
}
}
}
#endif
/* _cj_chum(): decode chum as string.
*/
static c3_c*
_cj_chum(u3_noun chu)
{
if ( _(u3ud(chu)) ) {
return u3r_string(chu);
}
else {
u3_noun h_chu = u3h(chu);
u3_noun t_chu = u3t(chu);
if ( !_(u3a_is_cat(t_chu)) ) {
return 0;
} else {
c3_c* h_chu_c = u3r_string(h_chu);
c3_c buf[33];
memset(buf, 0, 33);
snprintf(buf, 32, "%s%d", h_chu_c, t_chu);
c3_free(h_chu_c);
return strdup(buf);
}
}
}
/* _cj_je_fsck: fsck:je, or none.
*/
static u3_noun
_cj_je_fsck(u3_noun clu)
{
u3_noun p_clu, q_clu, r_clu;
u3_noun huk;
c3_c* nam_c;
c3_l axe_l;
if ( c3n == u3r_trel(clu, &p_clu, &q_clu, &r_clu) ) {
u3z(clu); return u3_none;
}
if ( 0 == (nam_c = _cj_chum(p_clu)) ) {
u3z(clu); return u3_none;
}
while ( _(u3du(q_clu)) && (11 == u3h(q_clu)) ) {
q_clu = u3t(u3t(q_clu));
}
if ( !_(u3du(q_clu)) ) {
u3z(clu); c3_free(nam_c); return u3_none;
}
if ( (1 == u3h(q_clu)) && (0 == u3t(q_clu)) ) {
axe_l = 0;
}
else {
if ( (0 != u3h(q_clu)) || !_(u3a_is_cat(axe_l = u3t(q_clu))) ) {
u3z(clu); c3_free(nam_c); return u3_none;
}
}
{
huk = 0;
while ( _(u3du(r_clu)) ) {
u3_noun ir_clu, tr_clu, pir_clu, qir_clu;
if ( (c3n == u3r_cell(r_clu, &ir_clu, &tr_clu)) ||
(c3n == u3r_cell(ir_clu, &pir_clu, &qir_clu)) ||
(c3n == u3ud(pir_clu)) )
{
u3z(huk); u3z(clu); c3_free(nam_c); return u3_none;
}
huk = u3kdb_put(huk, u3k(pir_clu), u3k(qir_clu));
r_clu = tr_clu;
}
}
u3z(clu);
{
u3_noun pro = u3nt(u3i_string(nam_c), axe_l, huk);
c3_free(nam_c);
return pro;
}
}
/* _cj_find_cold(): search cold state for `bat`s [bash registry].
* RETAIN.
*/
static u3_weak
_cj_find_cold(u3_noun bat)
{
u3a_road* rod_u = u3R;
while ( 1 ) {
u3_weak bar = u3h_get(rod_u->jed.cod_p, bat);
if ( u3_none != bar ) {
return bar;
}
if ( rod_u->par_p ) {
rod_u = u3to(u3_road, rod_u->par_p);
}
else return u3_none;
}
}
/* _cj_find_warm(): search warm state for `loc`s activation.
* RETAIN.
*/
static u3_weak
_cj_find_warm(u3_noun loc)
{
u3a_road* rod_u = u3R;
while ( 1 ) {
u3_weak ank = u3h_get(rod_u->jed.war_p, loc);
if ( u3_none != ank ) {
return ank;
}
if ( rod_u->par_p ) {
rod_u = u3to(u3_road, rod_u->par_p);
}
else return u3_none;
}
}
static u3_weak _cj_spot(u3_noun cor, u3_weak* bas);
/* _cj_reg_find(): locate core within registry. RETAIN.
*/
static u3_weak
_cj_reg_find(u3_noun reg, u3_noun cor)
{
u3_noun rut = u3h(reg),
pas = u3t(reg),
rum = u3qdb_get(rut, u3t(cor));
if ( u3_nul != rum ) {
u3_noun loc = u3k(u3t(rum));
u3z(rum);
return loc;
}
else {
while ( u3_nul != pas ) {
u3_noun pap = u3h(pas),
axe = u3h(pap),
lol = u3t(pap);
u3_weak par = u3r_at(axe, cor),
pel;
if ( u3_none != par ) {
pel = _cj_spot(par, NULL);
if ( u3_none != pel ) {
u3_noun nit = u3qdb_get(lol, pel);
u3z(pel);
if ( u3_nul != nit ) {
u3_noun loc = u3k(u3t(nit));
u3z(nit);
return loc;
}
}
}
pas = u3t(pas);
}
return u3_none;
}
}
/* _cj_jit(): generate arbitrary warm jet-associated data. RETAIN.
*/
static u3_noun
_cj_jit(c3_l jax_l, u3_noun bat)
{
return u3_nul;
}
/* _cj_loc_axe(): axis-within-core from location (0 for root). RETAIN.
*/
static u3_noun
_cj_loc_axe(u3_noun loc)
{
u3_noun pat = u3h(loc);
return ( c3n == u3h(pat) )
? u3k(u3h(u3t(pat)))
: (c3y == u3h(u3t(pat))) ? 0 : 3;
}
/* _cj_loc_pel(): parent location (or root noun, if root) of loc. RETAIN.
*/
static u3_noun
_cj_loc_pel(u3_noun loc)
{
return u3k(u3t(u3t(u3h(loc))));
}
/* _cj_spot_cold(): spot, cold dashboard only. *bar is set to
* the [bash registry] found for battery. RETAIN.
*/
static u3_weak
_cj_spot_cold(u3_noun cor, u3_weak* bar)
{
*bar = _cj_find_cold(u3h(cor));
if ( u3_none == *bar ) {
return u3_none;
}
else {
return _cj_reg_find(u3t(*bar), cor);
}
}
/* _cj_spot_hot(): try to locate core by hot dashboard. if found,
* the activation (warm state) is returned and the
* location is produced at *loc. RETAIN.
*/
static u3_weak
_cj_spot_hot(u3_noun cor, u3_noun bas, u3_noun* loc)
{
u3_noun bat = u3h(cor);
u3_weak act = u3_none,
hot = u3h_get(u3H->rod_u.jed.hot_p, bas);
if ( u3_none != hot ) {
u3_noun reg, jax, hap, bal;
c3_l jax_l;
u3x_qual(hot, ®, &jax, &hap, &bal);
jax_l = (c3_l) jax;
if ( u3_none != (*loc = _cj_reg_find(reg, cor)) ) {
act = u3nq(jax_l, u3k(hap), u3k(bal), _cj_jit(jax_l, bat));
}
u3z(hot);
}
return act;
}
/* _cj_spot(): find location of cor. expensive. RETAIN.
* bas is complicated to make it easy to cache bashes.
* you can safely ignore it by passing NULL. Otherwise,
* if it points to u3_none, and no location is found,
* a bash will be PRODUCED there. if it contains a bash,
* that will be used instead of calling _cj_bash.
*/
static u3_weak
_cj_spot(u3_noun cor, u3_weak* bas)
{
u3_weak bak = u3_none,
bar = u3_none,
reg = u3_none,
loc = _cj_spot_cold(cor, &bar);
if ( NULL == bas ) {
bas = &bak;
}
if ( u3_none != bar ) {
if ( (u3_none == *bas) ) {
*bas = u3k(u3h(bar));
}
reg = u3k(u3t(bar));
u3z(bar);
}
if ( u3_none == loc ) {
if ( u3_none == *bas ) {
*bas = _cj_bash(u3h(cor));
}
if ( !(u3C.wag_w & u3o_hashless) ) {
u3_weak act = _cj_spot_hot(cor, *bas, &loc);
if ( u3_none != act ) {
reg = _cj_gust(reg, _cj_loc_axe(loc), _cj_loc_pel(loc), u3k(loc));
u3h_put(u3R->jed.cod_p, u3h(cor), u3nc(u3k(*bas), u3k(reg)));
/* caution: could overwrites old value, debug batteries etc.
** old value contains old _cj_jit (from different
** battery). if we change jit to (map battery *),
** will need to merge with that map here.
*/
u3h_put(u3R->jed.war_p, loc, act);
}
}
}
if ( u3_none != bak ) {
u3z(bak);
}
if ( u3_none != reg ) {
u3z(reg);
}
return loc;
}
/* _cj_cast(): create a u3j_fink that can be used to efficiently verify
* that another core is located where this one is. RETAIN.
*/
static u3p(u3j_fink)
_cj_cast(u3_noun cor, u3_noun loc)
{
c3_w i_w = 0;
u3_noun j, par, bat, dyn, pax,
rev = u3_nul,
pat = u3h(loc);
u3j_fink* fin_u;
while ( c3n == u3h(pat) ) {
bat = u3h(cor);
dyn = u3t(pat);
pax = u3h(dyn);
loc = u3t(dyn);
pat = u3h(loc);
rev = u3nc(u3nc(u3k(bat), u3k(pax)), rev);
// pax already known-valid
cor = u3r_at(pax, cor);
++i_w;
}
fin_u = u3a_walloc(c3_wiseof(u3j_fink) +
(i_w * c3_wiseof(u3j_fist)));
fin_u->len_w = i_w;
fin_u->sat = u3k(cor);
for ( j = rev; i_w-- > 0; j = u3t(j) ) {
u3j_fist* fis_u = &(fin_u->fis_u[i_w]);
par = u3h(j);
fis_u->bat = u3k(u3h(par));
fis_u->pax = u3k(u3t(par));
}
u3z(rev);
u3_assert( u3_nul == j );
return u3of(u3j_fink, fin_u);
}
/* _cj_fine(): check that a core matches a u3j_fink. RETAIN.
*/
static c3_o
_cj_fine(u3_noun cor, u3p(u3j_fink) fin_p)
{
u3j_fink* fin_u = u3to(u3j_fink, fin_p);
c3_w i_w;
for ( i_w = 0; i_w < fin_u->len_w; ++i_w ) {
u3j_fist* fis_u = &(fin_u->fis_u[i_w]);
if ( c3n == u3r_sing(fis_u->bat, u3h(cor)) ) {
return c3n;
}
else {
cor = u3r_at(fis_u->pax, cor);
}
}
return u3r_sing(fin_u->sat, cor);
}
/* _cj_nail(): resolve hot state for arm at axis within cores located
* at loc. a label will be PRODUCED at *lab, unconditionally.
* Arguments are RETAINED. Return value is yes if a jet driver
* is present.
*/
static c3_o
_cj_nail(u3_noun loc, u3_noun axe,
u3_noun* lab, u3j_core** cop_u, u3j_harm** ham_u)
{
c3_o ret_o;
u3_noun jax, hap, bal, jit;
u3_weak act;
act = _cj_find_warm(loc);
u3_assert(u3_none != act);
u3x_qual(act, &jax, &hap, &bal, &jit);
*lab = u3k(bal);
if ( 0 == jax ) {
ret_o = c3n;
}
else {
u3_weak inx = u3kdb_get(u3k(hap), u3k(axe));
if ( u3_none == inx ) {
ret_o = c3n;
}
else {
c3_l jax_l = jax,
inx_l = inx;
*cop_u = &(u3D.ray_u[jax_l]);
*ham_u = &((*cop_u)->arm_u[inx_l]);
ret_o = c3y;
}
}
u3z(act);
return ret_o;
}
/* _cj_hot_mean(): in parent, declare a core. RETAINS.
*/
static c3_l
_cj_hot_mean(c3_l par_l, u3_noun nam)
{
u3j_core* par_u;
u3j_core* dev_u;
if ( 0 != par_l ) {
par_u = &u3D.ray_u[par_l];
dev_u = par_u->dev_u;
}
else {
par_u = 0;
dev_u = u3D.dev_u;
}
{
c3_w i_l = 0;
u3j_core* cop_u;
while ( (cop_u = &dev_u[i_l])->cos_c ) {
if ( _(u3r_sing_c(cop_u->cos_c, nam)) ) {
#if 0
u3l_log("hot: bound jet %d/%s/%s/",
cop_u->jax_l,
cop_u->cos_c,
par_u ? par_u->cos_c : "~");
#endif
return cop_u->jax_l;
}
i_l++;
}
}
return 0;
}
/* u3j_boot(): initialize jet system.
*/
c3_w
u3j_boot(c3_o nuu_o)
{
u3_assert(u3R == &(u3H->rod_u));
u3D.len_l = _cj_count(0, u3D.dev_u);
u3D.all_l = (2 * u3D.len_l) + 1024; // horrid heuristic
u3D.ray_u = c3_malloc(u3D.all_l * sizeof(u3j_core));
memset(u3D.ray_u, 0, (u3D.all_l * sizeof(u3j_core)));
if ( c3n == nuu_o ) {
u3h_free(u3R->jed.hot_p);
}
u3R->jed.hot_p = u3h_new();
return _cj_install(u3D.ray_u, 1,
(c3_l) (long long) u3D.dev_u[0].par_u,
u3_nul,
u3D.dev_u);
}
/* _cj_soft(): kick softly by arm axis.
*/
static u3_noun
_cj_soft(u3_noun cor, u3_noun axe)
{
u3_noun arm = u3x_at(axe, cor);
return u3n_nock_on(cor, u3k(arm));
}
void
find_error(u3_noun cor,
u3_noun old,
u3_noun new);
/* _cj_kick_z(): try to kick by jet. If no kick, produce u3_none.
**
** `cor` is RETAINED iff there is no kick, TRANSFERRED if one.
** `axe` is RETAINED.
*/
static u3_weak
_cj_kick_z(u3_noun cor, u3j_core* cop_u, u3j_harm* ham_u, u3_atom axe)
{
if ( 0 == ham_u->fun_f ) {
return u3_none;
}
if ( !_(ham_u->liv) ) {
return u3_none;
}
else {
#ifdef U3_MEMORY_DEBUG
c3_w cod_w;
{
char soc_c[5];
memset(soc_c, 0, 5);
strncpy(soc_c, cop_u->cos_c, 4);
soc_c[4] = 0;
cod_w = u3i_string(soc_c);
cod_w = u3a_lush(cod_w);
}
#endif
if ( _(ham_u->ice) ) {
u3_weak pro = ham_u->fun_f(cor);
#ifdef U3_MEMORY_DEBUG
u3a_lop(cod_w);
#endif
if ( u3_none != pro ) {
u3z(cor);
return pro;
}
}
else {
u3_weak pro, ame;
ham_u->ice = c3y;
pro = ham_u->fun_f(cor);
ham_u->ice = c3n;
#ifdef U3_MEMORY_DEBUG
u3a_lop(cod_w);
#endif
if ( u3_none == pro ) {
u3z(cor);
return pro;
}
ham_u->liv = c3n;
ame = _cj_soft(cor, axe);
ham_u->liv = c3y;
if ( c3n == u3r_sing(ame, pro) ) {
u3l_log("test: %s %s: mismatch: good %x, bad %x",
cop_u->cos_c,
(!strcmp(".2", ham_u->fcs_c)) ? "$" : ham_u->fcs_c,
u3r_mug(ame),
u3r_mug(pro));
ham_u->liv = c3n;
return u3m_bail(c3__fail);
}
else {
#if 0
u3l_log("test: %s %s",
cop_u->cos_c,
(!strcmp(".2", ham_u->fcs_c)) ? "$" : ham_u->fcs_c);
#endif
u3z(ame);
return pro;
}
}
return u3_none;
}
}
/* _cj_hook_in(): execute hook from core, or fail.
*/
static u3_noun
_cj_hook_in(u3_noun cor,
const c3_c* tam_c,
c3_o jet_o)
{
u3_weak loc, col;
u3_noun roc, tem, got, pat, nam, huc;
if ( c3n == u3du(cor) ) {
u3l_log("_cj_hook_in failure: c3n == u3du(cor)");
return u3m_bail(c3__fail);
}
loc = _cj_spot(cor, NULL);
if ( u3_none == loc ) {
u3l_log("_cj_hook_in failure: u3_none == loc");
return u3m_bail(c3__fail);
}
tem = u3i_string(tam_c);
while ( 1 ) {
u3x_trel(loc, &pat, &nam, &huc);
got = u3qdb_get(huc, tem);
if ( u3_nul != got ) {
c3_l axe_l;
u3_noun pro, fol;
u3j_core* cop_u;
u3z(tem);
fol = u3k(u3t(got));
u3z(got);
axe_l = _cj_axis(fol);
if ( 0 == axe_l ) {
u3t_off(glu_o);
pro = u3n_nock_on(cor, fol);
u3t_on(glu_o);
}
else {
c3_l jax_l, inx_l;
u3_noun hap, act;
u3z(fol);
act = _cj_find_warm(loc);
jax_l = u3h(act);
hap = u3h(u3t(act));
cop_u = &u3D.ray_u[jax_l];
// Tricky: the above case would work here too, but would
// disable jet_o and create some infinite recursions.
//
u3t_off(glu_o);
if ( (c3n == jet_o) ||
(u3_none == (inx_l = u3kdb_get(u3k(hap), axe_l))) ||
(u3_none == (pro = _cj_kick_z(cor,
cop_u,
&cop_u->arm_u[inx_l],
axe_l))) ) {
pro = u3n_nock_on(cor, u3k(u3x_at(axe_l, cor)));
}
u3t_on(glu_o);
u3z(act);
}
u3z(loc);
return pro;
}
else if ( c3n == u3h(pat) ) {
u3_noun dyn = u3t(pat),
axe = u3h(dyn),
pel = u3t(dyn);
// axe already known-valid
roc = u3k(u3r_at(axe, cor));
u3z(cor);
cor = roc;
col = u3k(pel);