urbit/noun/jets.c

2261 lines
50 KiB
C
Raw Normal View History

2014-09-11 04:01:32 +04:00
/* g/j.c
2014-08-26 20:12:45 +04:00
**
*/
2014-09-05 23:55:16 +04:00
#include "all.h"
2014-08-26 20:12:45 +04:00
2018-06-14 21:36:56 +03:00
#if defined(U3_OS_osx)
#include <CommonCrypto/CommonDigest.h>
#else
#include <openssl/sha.h>
#endif
2018-04-30 20:56:19 +03:00
/** Data structures.
**/
2018-05-08 23:33:55 +03:00
/* _cj_hank: cached hook information.
*/
typedef struct {
u3_weak hax; // axis of hooked inner core
u3j_site sit_u; // call-site data
} _cj_hank;
2018-04-30 20:56:19 +03:00
/** Functions.
**/
2018-05-08 23:33:55 +03:00
/* _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;
2014-09-02 04:10:04 +04:00
2018-05-08 23:33:55 +03:00
if ( dev_u ) {
for ( i_w = 0; 0 != dev_u[i_w].cos_c; i_w++ ) {
u3j_core* kid_u = &dev_u[i_w];
2014-09-07 02:39:28 +04:00
2018-05-08 23:33:55 +03:00
if ( par_u ) {
2014-09-07 02:39:28 +04:00
kid_u->par_u = par_u;
}
2018-05-08 23:33:55 +03:00
len_l += _cj_count(kid_u, kid_u->dev_u);
2014-09-02 04:10:04 +04:00
}
}
2018-05-08 23:33:55 +03:00
return 1 + len_l;
}
2018-05-16 19:41:49 +03:00
/* _cj_core_loc(): location noun from u3j_core.
2018-05-08 23:33:55 +03:00
*/
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);
2018-05-08 23:33:55 +03:00
}
}
pat = ( 0 == cop_u->axe_l )
? u3nt(c3y, c3y, pel)
: ( (3 == cop_u->axe_l) && (c3y == u3h(u3h(pel))) )
2018-05-08 23:33:55 +03:00
? 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)
{
2018-05-30 07:33:10 +03:00
c3_w i_w, len_w = strlen(has_c);
if ( 64 != len_w ) {
fprintf(stderr, "bash not 64 characters: %s\r\n", has_c);
c3_assert(0);
}
c3_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;
2018-05-08 23:33:55 +03:00
}
2018-06-14 21:36:56 +03:00
// in the jam jet file
c3_w* u3qe_jam_buf(u3_noun, c3_w* bit_w);
2018-05-16 19:41:49 +03:00
/* _cj_bash(): battery hash. RETAIN.
2018-05-08 23:33:55 +03:00
*/
static u3_noun
_cj_bash(u3_noun bat)
{
2018-10-02 02:56:14 +03:00
if ( u3C.wag_w & u3o_hashless ) {
return u3_nul;
}
2018-05-26 03:53:36 +03:00
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 {
2018-06-14 21:36:56 +03:00
c3_w bit_w, met_w;
c3_w* wor_w;
c3_y* fat_y;
c3_y dig_y[32];
wor_w = u3qe_jam_buf(bat, &bit_w);
met_w = bit_w >> 3;
if ( bit_w != met_w << 3 ) {
++met_w;
}
// assume little-endian
fat_y = (c3_y*) wor_w;
#if defined(U3_OS_osx)
CC_SHA256_CTX ctx_h;
CC_SHA256_Init(&ctx_h);
CC_SHA256_Update(&ctx_h, fat_y, met_w);
CC_SHA256_Final(dig_y, &ctx_h);
#else
SHA256_CTX ctx_h;
SHA256_Init(&ctx_h);
SHA256_Update(&ctx_h, fat_y, met_w);
SHA256_Final(dig_y, &ctx_h);
#endif
pro = u3i_bytes(32, dig_y);
2018-05-26 03:53:36 +03:00
u3h_put(u3R->jed.bas_p, bat, u3k(pro));
2018-06-14 21:36:56 +03:00
u3a_wfree(wor_w);
2018-05-26 03:53:36 +03:00
break;
}
}
return pro;
}
2018-05-08 23:33:55 +03:00
/* _cj_mine_par_old(): register hooks and parent location within existing
2018-05-16 19:41:49 +03:00
* axis in ancestor list or u3_none.
2018-05-08 23:33:55 +03:00
*/
static u3_weak
_cj_mine_par_old(u3_noun lan, u3_noun axe, u3_noun pel, u3_noun loc)
{
u3_noun par;
2018-05-16 19:41:49 +03:00
u3_weak pro;
2018-05-08 23:33:55 +03:00
if ( u3_nul == lan ) {
2018-05-16 19:41:49 +03:00
pro = u3_none;
u3z(axe); u3z(pel); u3z(loc);
2018-05-08 23:33:55 +03:00
}
else if ( c3y == u3r_sing(axe, u3h(par = u3h(lan))) ) {
2018-05-16 19:41:49 +03:00
u3_noun lol = u3kdb_put(u3k(u3t(par)), pel, loc),
rap = u3nc(axe, lol);
pro = u3nc(rap, u3k(u3t(lan)));
2018-05-08 23:33:55 +03:00
}
else {
2018-05-16 19:41:49 +03:00
u3_weak nex = _cj_mine_par_old(u3k(u3t(lan)), axe, pel, loc);
2018-05-08 23:33:55 +03:00
if ( u3_none == nex ) {
2018-05-16 19:41:49 +03:00
pro = u3_none;
2018-05-08 23:33:55 +03:00
}
else {
2018-05-16 19:41:49 +03:00
pro = u3nc(u3k(par), nex);
2018-05-08 23:33:55 +03:00
}
}
2018-05-16 19:41:49 +03:00
u3z(lan);
return pro;
2018-05-08 23:33:55 +03:00
}
2018-05-16 19:41:49 +03:00
/* _cj_mine_par_new(): insert ancestor within lan at sorted index.
2018-05-08 23:33:55 +03:00
*/
static u3_noun
_cj_mine_par_new(u3_noun lan, u3_noun axe, u3_noun pel, u3_noun loc)
{
2018-05-16 19:41:49 +03:00
u3_weak pro;
2018-05-08 23:33:55 +03:00
if ( (u3_nul == lan) || (c3y == u3qa_lth(axe, u3h(u3h(lan)))) ) {
2018-05-16 19:41:49 +03:00
u3_noun par = u3nc(axe, u3kdb_put(u3_nul, pel, loc));
pro = u3nc(par, lan);
2018-05-08 23:33:55 +03:00
}
else {
2018-05-16 19:41:49 +03:00
pro = u3nc(u3k(u3h(lan)),
_cj_mine_par_new(u3k(u3t(lan)), axe, pel, loc));
u3z(lan);
2018-05-08 23:33:55 +03:00
}
2018-05-16 19:41:49 +03:00
return pro;
2018-05-08 23:33:55 +03:00
}
2014-09-02 04:10:04 +04:00
2018-05-16 19:41:49 +03:00
/* _cj_mine_par(): register a location as an ancestor
* in a list of ancestors.
2018-05-08 23:33:55 +03:00
*/
static u3_noun
_cj_mine_par(u3_noun lan, u3_noun axe, u3_noun pel, u3_noun loc)
{
2018-05-16 19:41:49 +03:00
u3_weak old = _cj_mine_par_old(u3k(lan), u3k(axe), u3k(pel), u3k(loc));
2018-05-08 23:33:55 +03:00
if ( u3_none != old ) {
2018-05-16 19:41:49 +03:00
u3z(lan); u3z(axe); u3z(pel); u3z(loc);
2018-05-08 23:33:55 +03:00
return old;
}
else {
return _cj_mine_par_new(lan, axe, pel, loc);
}
}
2018-05-16 19:41:49 +03:00
/* _cj_gust(): add location to registry.
*/
2018-05-08 23:33:55 +03:00
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);
}
2014-09-02 04:10:04 +04:00
2018-05-08 23:33:55 +03:00
ger = ( 0 == axe )
2018-05-16 19:41:49 +03:00
? u3nc(u3kdb_put(u3k(u3h(reg)), pel, loc), u3k(u3t(reg)))
: u3nc(u3k(u3h(reg)), _cj_mine_par(u3k(u3t(reg)), axe, pel, loc));
2018-05-08 23:33:55 +03:00
u3z(reg);
return ger;
}
2014-10-25 07:49:27 +04:00
/* _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;
2018-10-11 02:15:35 +03:00
while ( _(u3du(fol)) && (11 == u3h(fol)) )
2014-10-25 07:49:27 +04:00
{ fol = u3t(u3t(fol)); }
2014-11-06 03:20:01 +03:00
if ( !_(u3r_trel(fol, &p_fol, &q_fol, &r_fol)) ) {
if ( !_(u3r_cell(fol, &p_fol, &q_fol)) ||
2014-10-25 07:49:27 +04:00
(0 != p_fol) ||
2014-11-06 03:20:01 +03:00
(!_(u3a_is_cat(q_fol))) )
2014-10-25 07:49:27 +04:00
{
fprintf(stderr, "axis: bad a\r\n");
return 0;
}
return q_fol;
}
else {
if ( 9 != p_fol )
{ fprintf(stderr, "axis: bad b\r\n"); return 0; }
2014-11-06 03:20:01 +03:00
if ( !_(u3a_is_cat(q_fol)) )
2014-10-25 07:49:27 +04:00
{ fprintf(stderr, "axis: bad c\r\n"); return 0; }
2014-11-05 04:18:47 +03:00
if ( !_(u3du(r_fol)) || (0 != u3h(r_fol)) || (1 != u3t(r_fol)) )
2014-10-25 07:49:27 +04:00
{ fprintf(stderr, "axis: bad d\r\n"); return 0; }
return q_fol;
}
}
2014-10-25 01:04:44 +04:00
/* _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) )
{
fprintf(stderr, "jets: activate: bad fcs %s\r\n", 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 ) {
fprintf(stderr, "jets: activate: bad fcs %s\r\n", 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;
c3_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)),
2018-05-12 22:16:36 +03:00
hap = _cj_warm_hump(jax_l, huc),
toh = u3nq(reg, jax_l, hap, u3k(bal));
u3h_put(u3R->jed.hot_p, key, toh);
2018-05-16 07:59:00 +03:00
u3z(key); u3z(hot);
}
}
jax_l = _cj_install(ray_u, ++jax_l, loc, bal, kid_u->dev_u);
}
}
2018-05-16 07:59:00 +03:00
u3z(pel);
u3z(lab);
return jax_l;
}
2014-11-20 22:49:27 +03:00
#if 0
2014-10-28 20:36:22 +03:00
/* _cj_by_gut(): (~(get by a) b), unifying; RETAINS a, b, AND result.
2014-10-25 16:21:48 +04:00
*/
static u3_weak
_cj_by_gut(u3_noun a, u3_noun b)
{
if ( u3_nul == a ) {
2014-11-06 02:36:30 +03:00
return u3_none;
2014-10-25 16:21:48 +04:00
}
else {
u3_noun l_a, n_a, r_a;
u3_noun pn_a, qn_a;
2014-11-06 03:20:01 +03:00
u3x_trel(a, &n_a, &l_a, &r_a);
u3x_cell(n_a, &pn_a, &qn_a);
2014-10-25 16:21:48 +04:00
{
2014-11-06 03:20:01 +03:00
if ( (c3y == u3r_sing(b, pn_a)) ) {
2014-10-25 16:21:48 +04:00
return qn_a;
}
else {
2014-11-06 22:13:57 +03:00
if ( c3y == u3qc_gor(b, pn_a) ) {
2014-10-25 16:21:48 +04:00
return _cj_by_gut(l_a, b);
}
else return _cj_by_gut(r_a, b);
}
}
}
}
2014-11-20 22:49:27 +03:00
#endif
2014-10-25 16:21:48 +04:00
2014-10-22 00:53:27 +04:00
/* _cj_chum(): decode chum as string.
*/
static c3_c*
_cj_chum(u3_noun chu)
{
2014-11-05 04:18:47 +03:00
if ( _(u3ud(chu)) ) {
2014-11-06 03:20:01 +03:00
return u3r_string(chu);
2014-10-22 00:53:27 +04:00
}
else {
u3_noun h_chu = u3h(chu);
u3_noun t_chu = u3t(chu);
2014-11-06 03:20:01 +03:00
if ( !_(u3a_is_cat(t_chu)) ) {
2014-10-22 00:53:27 +04:00
return 0;
} else {
2014-11-06 03:20:01 +03:00
c3_c* h_chu_c = u3r_string(h_chu);
2014-10-22 00:53:27 +04:00
c3_c buf[33];
memset(buf, 0, 33);
snprintf(buf, 32, "%s%d", h_chu_c, t_chu);
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;
2014-11-06 03:20:01 +03:00
if ( c3n == u3r_trel(clu, &p_clu, &q_clu, &r_clu) ) {
2014-11-06 02:36:30 +03:00
u3z(clu); return u3_none;
2014-10-22 00:53:27 +04:00
}
if ( 0 == (nam_c = _cj_chum(p_clu)) ) {
2014-11-06 02:36:30 +03:00
u3z(clu); return u3_none;
2014-10-22 00:53:27 +04:00
}
2018-10-11 02:15:35 +03:00
while ( _(u3du(q_clu)) && (11 == u3h(q_clu)) ) {
2014-10-22 00:53:27 +04:00
q_clu = u3t(u3t(q_clu));
}
2014-11-05 04:18:47 +03:00
if ( !_(u3du(q_clu)) ) {
2014-11-06 02:36:30 +03:00
u3z(clu); free(nam_c); return u3_none;
2014-10-22 00:53:27 +04:00
}
if ( (1 == u3h(q_clu)) && (0 == u3t(q_clu)) ) {
axe_l = 0;
}
else {
2014-11-06 03:20:01 +03:00
if ( (0 != u3h(q_clu)) || !_(u3a_is_cat(axe_l = u3t(q_clu))) ) {
2014-11-06 02:36:30 +03:00
u3z(clu); free(nam_c); return u3_none;
2014-10-22 00:53:27 +04:00
}
}
{
huk = 0;
2014-11-05 04:18:47 +03:00
while ( _(u3du(r_clu)) ) {
2014-10-22 00:53:27 +04:00
u3_noun ir_clu, tr_clu, pir_clu, qir_clu;
2014-11-06 03:20:01 +03:00
if ( (c3n == u3r_cell(r_clu, &ir_clu, &tr_clu)) ||
(c3n == u3r_cell(ir_clu, &pir_clu, &qir_clu)) ||
2014-11-05 04:18:47 +03:00
(c3n == u3ud(pir_clu)) )
2014-10-22 00:53:27 +04:00
{
2014-11-06 02:36:30 +03:00
u3z(huk); u3z(clu); free(nam_c); return u3_none;
2014-10-22 00:53:27 +04:00
}
2014-11-06 22:13:57 +03:00
huk = u3kdb_put(huk, u3k(pir_clu), u3k(qir_clu));
2014-10-24 04:35:26 +04:00
r_clu = tr_clu;
2014-10-22 00:53:27 +04:00
}
}
2014-10-28 20:36:22 +03:00
u3z(clu);
2014-11-06 03:20:01 +03:00
return u3nt(u3i_string(nam_c), axe_l, huk);
2014-10-22 00:53:27 +04:00
}
2018-05-26 22:03:09 +03:00
/* _cj_find_cold(): search cold state for `bat`s [bash registry].
2018-03-30 02:18:22 +03:00
* RETAIN.
*/
2018-03-26 19:58:36 +03:00
static u3_weak
_cj_find_cold(u3_noun bat)
{
u3a_road* rod_u = u3R;
while ( 1 ) {
2018-05-26 22:03:09 +03:00
u3_weak bar = u3h_gut(rod_u->jed.cod_p, bat);
2018-03-26 19:58:36 +03:00
2018-05-26 22:03:09 +03:00
if ( u3_none != bar ) {
return bar;
2018-03-26 19:58:36 +03:00
}
if ( rod_u->par_p ) {
rod_u = u3to(u3_road, rod_u->par_p);
}
else return u3_none;
}
}
2018-03-30 02:18:22 +03:00
/* _cj_find_warm(): search warm state for `loc`s activation.
* RETAIN.
*/
2018-03-26 19:58:36 +03:00
static u3_weak
_cj_find_warm(u3_noun loc)
{
u3a_road* rod_u = u3R;
while ( 1 ) {
u3_weak ank = u3h_gut(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;
}
}
2018-05-26 22:03:09 +03:00
static u3_weak _cj_spot(u3_noun cor, u3_weak* bas);
2018-05-08 23:33:55 +03:00
2018-05-16 19:41:49 +03:00
/* _cj_reg_find(): locate core within registry. RETAIN.
*/
static u3_weak
2018-05-08 23:33:55 +03:00
_cj_reg_find(u3_noun reg, u3_noun cor)
2018-03-26 19:58:36 +03:00
{
2018-05-08 23:33:55 +03:00
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;
2018-03-26 19:58:36 +03:00
}
else {
2018-05-08 23:33:55 +03:00
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 ) {
2018-05-26 22:03:09 +03:00
pel = _cj_spot(par, NULL);
2018-05-08 23:33:55 +03:00
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;
2018-03-26 19:58:36 +03:00
}
}
}
2018-05-08 23:33:55 +03:00
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;
}
2018-05-12 22:16:36 +03:00
/* _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;
}
2018-05-12 22:16:36 +03:00
/* _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))));
}
2018-05-26 22:03:09 +03:00
/* _cj_spot_cold(): spot, cold dashboard only. *bar is set to
* the [bash registry] found for battery. RETAIN.
2018-05-08 23:33:55 +03:00
*/
static u3_weak
2018-05-26 22:03:09 +03:00
_cj_spot_cold(u3_noun cor, u3_weak* bar)
2018-05-08 23:33:55 +03:00
{
2018-05-26 22:03:09 +03:00
*bar = _cj_find_cold(u3h(cor));
if ( u3_none == *bar ) {
return u3_none;
}
else {
return _cj_reg_find(u3t(*bar), cor);
}
}
2018-05-08 23:33:55 +03:00
/* _cj_spot_hot(): try to locate core by hot dashboard. if found,
* the activation (warm state) is returned and the
2018-05-16 19:41:49 +03:00
* location is produced at *loc. RETAIN.
*/
static u3_weak
2018-05-26 22:03:09 +03:00
_cj_spot_hot(u3_noun cor, u3_noun bas, u3_noun* loc)
{
u3_noun bat = u3h(cor);
u3_weak act = u3_none,
2018-05-26 22:03:09 +03:00
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, &reg, &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;
}
2018-05-08 23:33:55 +03:00
/* _cj_spot(): find location of cor. expensive. RETAIN.
2018-05-26 22:03:09 +03:00
* 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
2018-05-26 22:03:09 +03:00
_cj_spot(u3_noun cor, u3_weak* bas)
{
2018-05-26 22:03:09 +03:00
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);
}
2018-05-08 23:33:55 +03:00
if ( u3_none == loc ) {
2018-05-26 22:03:09 +03:00
if ( u3_none == *bas ) {
*bas = _cj_bash(u3h(cor));
}
2018-10-02 02:56:14 +03:00
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);
}
2018-05-08 23:33:55 +03:00
}
2018-03-26 19:58:36 +03:00
}
2018-05-26 22:03:09 +03:00
if ( u3_none != bak ) {
u3z(bak);
}
if ( u3_none != reg ) {
u3z(reg);
}
return loc;
2018-03-26 19:58:36 +03:00
}
/* _cj_cast(): create a u3j_fink that can be used to efficiently verify
2018-05-16 19:41:49 +03:00
* that another core is located where this one is. RETAIN.
*/
static u3p(u3j_fink)
2018-04-26 21:15:46 +03:00
_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
2018-04-26 21:15:46 +03:00
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);
c3_assert( u3_nul == j );
return u3of(u3j_fink, fin_u);
2018-04-26 21:15:46 +03:00
}
2018-05-16 19:41:49 +03:00
/* _cj_fine(): check that a core matches a u3j_fink. RETAIN.
2018-04-26 21:15:46 +03:00
*/
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);
}
2018-04-30 20:56:19 +03:00
/* _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);
c3_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.
2014-10-24 04:35:26 +04:00
*/
static c3_l
2018-03-26 19:58:36 +03:00
_cj_hot_mean(c3_l par_l, u3_noun nam)
2014-10-24 04:35:26 +04:00
{
2014-11-08 01:54:59 +03:00
u3j_core* par_u;
u3j_core* dev_u;
2014-10-22 00:53:27 +04:00
2014-10-28 20:36:22 +03:00
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;
}
2014-10-22 00:53:27 +04:00
2014-10-28 20:36:22 +03:00
{
c3_w i_l = 0;
2014-11-08 01:54:59 +03:00
u3j_core* cop_u;
2014-10-22 00:53:27 +04:00
2014-10-28 20:36:22 +03:00
while ( (cop_u = &dev_u[i_l])->cos_c ) {
2018-03-26 19:58:36 +03:00
if ( _(u3r_sing_c(cop_u->cos_c, nam)) ) {
2014-10-31 00:40:05 +03:00
#if 0
2018-03-26 19:58:36 +03:00
fprintf(stderr, "hot: bound jet %d/%s/%s/\r\n",
2014-10-28 20:36:22 +03:00
cop_u->jax_l,
cop_u->cos_c,
2018-03-26 19:58:36 +03:00
par_u ? par_u->cos_c : "~");
2014-10-24 04:35:26 +04:00
#endif
2014-10-28 20:36:22 +03:00
return cop_u->jax_l;
2014-10-24 04:35:26 +04:00
}
2014-10-28 20:36:22 +03:00
i_l++;
2014-10-24 04:35:26 +04:00
}
}
2014-10-28 20:36:22 +03:00
return 0;
}
2014-11-06 03:20:01 +03:00
/* u3j_boot(): initialize jet system.
2014-09-02 04:10:04 +04:00
*/
void
u3j_boot(c3_o nuu_o)
2014-09-02 04:10:04 +04:00
{
2014-09-07 02:39:28 +04:00
c3_w jax_l;
c3_assert(u3R == &(u3H->rod_u));
2014-09-07 02:39:28 +04:00
2014-09-06 00:13:24 +04:00
u3D.len_l =_cj_count(0, u3D.dev_u);
2014-10-11 09:32:58 +04:00
u3D.all_l = (2 * u3D.len_l) + 1024; // horrid heuristic
2014-09-02 04:10:04 +04:00
2014-11-08 01:54:59 +03:00
u3D.ray_u = (u3j_core*) malloc(u3D.all_l * sizeof(u3j_core));
memset(u3D.ray_u, 0, (u3D.all_l * sizeof(u3j_core)));
2014-09-02 04:10:04 +04:00
if ( c3n == nuu_o ) {
u3h_free(u3R->jed.hot_p);
}
u3R->jed.hot_p = u3h_new();
jax_l = _cj_install(u3D.ray_u, 1, (c3_l) (long long) u3D.dev_u[0].par_u, u3_nul, u3D.dev_u);
2014-10-03 21:44:08 +04:00
fprintf(stderr, "boot: installed %d jets\n", jax_l);
2014-09-02 04:10:04 +04:00
}
2014-09-18 23:07:36 +04:00
/* _cj_soft(): kick softly by arm axis.
*/
static u3_noun
2014-10-25 07:49:27 +04:00
_cj_soft(u3_noun cor, u3_noun axe)
2014-09-18 23:07:36 +04:00
{
2014-11-06 03:20:01 +03:00
u3_noun arm = u3x_at(axe, cor);
2014-09-18 23:07:36 +04:00
2014-11-06 03:20:01 +03:00
return u3n_nock_on(cor, u3k(arm));
2014-09-18 23:07:36 +04:00
}
void
find_error(u3_noun cor,
u3_noun old,
u3_noun new);
2014-11-06 02:36:30 +03:00
/* _cj_kick_z(): try to kick by jet. If no kick, produce u3_none.
2014-09-02 04:10:04 +04:00
**
2014-10-25 07:49:27 +04:00
** `cor` is RETAINED iff there is no kick, TRANSFERRED if one.
** `axe` is RETAINED.
2014-09-02 04:10:04 +04:00
*/
2014-09-06 00:13:24 +04:00
static u3_weak
2014-11-08 01:54:59 +03:00
_cj_kick_z(u3_noun cor, u3j_core* cop_u, u3j_harm* ham_u, u3_atom axe)
2014-09-02 04:10:04 +04:00
{
if ( 0 == ham_u->fun_f ) {
2014-11-06 02:36:30 +03:00
return u3_none;
2014-09-02 04:10:04 +04:00
}
2014-11-05 04:18:47 +03:00
if ( !_(ham_u->liv) ) {
2014-11-06 02:36:30 +03:00
return u3_none;
2014-09-18 23:07:36 +04:00
}
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
2014-11-05 04:18:47 +03:00
if ( _(ham_u->ice) ) {
2014-09-18 23:07:36 +04:00
u3_weak pro = ham_u->fun_f(cor);
#ifdef U3_MEMORY_DEBUG
u3a_lop(cod_w);
#endif
2014-11-06 02:36:30 +03:00
if ( u3_none != pro ) {
2014-09-18 23:07:36 +04:00
u3z(cor);
return pro;
}
}
else {
u3_weak pro, ame;
2014-11-05 04:18:47 +03:00
ham_u->ice = c3y;
2018-05-30 07:32:47 +03:00
pro = ham_u->fun_f(cor);
2014-11-05 04:18:47 +03:00
ham_u->ice = c3n;
2014-09-02 04:10:04 +04:00
#ifdef U3_MEMORY_DEBUG
u3a_lop(cod_w);
#endif
2014-11-06 02:36:30 +03:00
if ( u3_none == pro ) {
2014-09-18 23:07:36 +04:00
u3z(cor);
return pro;
}
2014-11-05 04:18:47 +03:00
ham_u->liv = c3n;
2014-10-25 07:49:27 +04:00
ame = _cj_soft(cor, axe);
2014-11-05 04:18:47 +03:00
ham_u->liv = c3y;
2014-09-18 23:07:36 +04:00
2014-11-06 03:20:01 +03:00
if ( c3n == u3r_sing(ame, pro) ) {
2014-10-03 21:44:08 +04:00
fprintf(stderr, "test: %s %s: mismatch: good %x, bad %x\r\n",
2014-10-25 07:49:27 +04:00
cop_u->cos_c,
2014-09-18 23:07:36 +04:00
(!strcmp(".2", ham_u->fcs_c)) ? "$" : ham_u->fcs_c,
2014-11-06 03:20:01 +03:00
u3r_mug(ame),
u3r_mug(pro));
ham_u->liv = c3n;
2014-09-18 23:07:36 +04:00
c3_assert(0);
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__fail);
2014-09-18 23:07:36 +04:00
}
else {
2018-05-30 21:32:10 +03:00
#if 1
2014-10-03 21:44:08 +04:00
fprintf(stderr, "test: %s %s\r\n",
2014-10-25 07:49:27 +04:00
cop_u->cos_c,
2014-09-18 23:07:36 +04:00
(!strcmp(".2", ham_u->fcs_c)) ? "$" : ham_u->fcs_c);
#endif
2016-01-24 04:37:49 +03:00
u3z(ame);
return pro;
2014-09-18 23:07:36 +04:00
}
2014-09-02 04:10:04 +04:00
}
2014-11-06 02:36:30 +03:00
return u3_none;
2014-09-02 04:10:04 +04:00
}
}
2014-10-25 23:30:36 +04:00
/* _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)
{
2018-03-30 02:18:22 +03:00
u3_weak loc, col;
u3_noun roc, tem, got, pat, nam, huc;
2018-03-28 21:27:33 +03:00
if ( c3n == u3du(cor) ) {
return u3m_bail(c3__fail);
}
2014-10-25 23:30:36 +04:00
2018-05-26 22:03:09 +03:00
loc = _cj_spot(cor, NULL);
2018-03-30 02:18:22 +03:00
if ( u3_none == loc ) {
return u3m_bail(c3__fail);
}
2018-03-30 21:49:18 +03:00
tem = u3i_string(tam_c);
2018-03-30 02:18:22 +03:00
while ( 1 ) {
2018-03-30 21:49:18 +03:00
u3x_trel(loc, &pat, &nam, &huc);
2018-03-30 02:18:22 +03:00
got = u3qdb_get(huc, tem);
if ( u3_nul != got ) {
2018-03-30 21:49:18 +03:00
c3_l axe_l;
2018-03-30 02:18:22 +03:00
u3_noun pro, fol;
2018-03-28 21:27:33 +03:00
u3j_core* cop_u;
2018-03-30 02:18:22 +03:00
2018-03-28 21:27:33 +03:00
u3z(tem);
fol = u3k(u3t(got));
u3z(got);
axe_l = _cj_axis(fol);
if ( 0 == axe_l ) {
2018-04-27 22:22:39 +03:00
u3t_off(glu_o);
2018-03-28 21:27:33 +03:00
pro = u3n_nock_on(cor, fol);
2018-04-27 22:22:39 +03:00
u3t_on(glu_o);
2018-03-28 21:27:33 +03:00
}
else {
2018-03-30 21:49:18 +03:00
c3_l jax_l, inx_l;
u3_noun hap, act;
2018-03-28 21:27:33 +03:00
u3z(fol);
2018-03-30 02:18:22 +03:00
act = _cj_find_warm(loc);
jax_l = u3h(act);
hap = u3h(u3t(act));
2018-03-28 21:27:33 +03:00
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.
//
2018-04-27 22:22:39 +03:00
u3t_off(glu_o);
2018-03-28 21:27:33 +03:00
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)));
}
2018-04-27 22:22:39 +03:00
u3t_on(glu_o);
2018-03-28 21:27:33 +03:00
u3z(act);
}
u3z(loc);
return pro;
2014-10-25 23:30:36 +04:00
}
2018-03-30 02:18:22 +03:00
else if ( c3n == u3h(pat) ) {
u3_noun dyn = u3t(pat),
axe = u3h(dyn),
pel = u3t(dyn);
// axe already known-valid
2018-03-30 02:18:22 +03:00
roc = u3k(u3r_at(axe, cor));
u3z(cor);
cor = roc;
col = u3k(pel);
u3z(loc);
loc = col;
}
else {
u3_noun sat = u3t(pat);
if ( c3y == u3h(sat) ) {
return u3m_bail(c3__fail);
}
else {
col = u3k(u3t(sat));
u3z(loc);
loc = col;
roc = u3k(u3t(cor));
u3z(cor);
2018-03-30 02:18:22 +03:00
cor = roc;
}
}
2014-10-25 23:30:36 +04:00
}
}
2014-11-06 03:20:01 +03:00
/* u3j_soft(): execute soft hook.
2014-09-17 23:56:55 +04:00
*/
u3_noun
2014-11-06 03:20:01 +03:00
u3j_soft(u3_noun cor,
2018-04-27 02:36:29 +03:00
const c3_c* tam_c)
2014-09-17 23:56:55 +04:00
{
2014-10-28 20:36:22 +03:00
u3_noun pro;
2018-04-27 02:36:29 +03:00
u3t_on(glu_o);
2014-11-05 04:18:47 +03:00
pro = _cj_hook_in(cor, tam_c, c3n);
2018-04-27 02:36:29 +03:00
u3t_off(glu_o);
2014-10-28 20:36:22 +03:00
return pro;
2014-09-17 23:56:55 +04:00
}
2014-11-06 03:20:01 +03:00
/* u3j_hook(): execute hook from core, or fail.
2014-09-17 23:56:55 +04:00
*/
u3_noun
2014-11-06 03:20:01 +03:00
u3j_hook(u3_noun cor,
2018-04-27 02:36:29 +03:00
const c3_c* tam_c)
2014-09-17 23:56:55 +04:00
{
2014-10-28 20:36:22 +03:00
u3_noun pro;
2018-04-27 02:36:29 +03:00
u3t_on(glu_o);
2014-11-05 04:18:47 +03:00
pro = _cj_hook_in(cor, tam_c, c3y);
2018-04-27 02:36:29 +03:00
u3t_off(glu_o);
2014-10-28 20:36:22 +03:00
return pro;
2014-09-17 23:56:55 +04:00
}
2014-09-02 04:10:04 +04:00
2018-04-30 21:10:01 +03:00
/* _cj_prog(): stop tracing glu and find a nock program
*/
static u3p(u3n_prog)
2018-05-26 04:17:19 +03:00
_cj_prog(u3_weak loc, u3_noun fol)
2018-04-30 21:10:01 +03:00
{
u3p(u3n_prog) pog_p;
u3t_off(glu_o);
pog_p = u3n_find((loc == u3_none ? u3_nul : loc), fol);
2018-04-30 21:10:01 +03:00
u3t_on(glu_o);
return pog_p;
}
2018-04-30 20:56:19 +03:00
/* cj_hank_find(): find cached hook information, keyed by arbitrary
* prefix and term cords. RETAIN.
*/
2018-04-27 02:36:29 +03:00
static _cj_hank*
_cj_hank_find(u3_noun pre, u3_noun tam)
{
u3_noun key = u3nc(u3k(pre), u3k(tam));
u3_noun got = u3h_git(u3R->jed.han_p, key);
if ( u3_none != got ) {
u3z(key);
return u3to(_cj_hank, got);
}
else {
_cj_hank* new_u = u3a_walloc(c3_wiseof(_cj_hank));
u3a_road* rod_u = u3R;
while ( rod_u->par_p && u3_none == got ) {
rod_u = u3to(u3a_road, rod_u->par_p);
got = u3h_git(u3R->jed.han_p, key);
}
if ( u3_none == got ) {
new_u->hax = u3_none;
}
else {
_cj_hank* old_u = u3to(_cj_hank, got);
if ( u3_none != (new_u->hax = old_u->hax) ) {
u3j_site_copy(&(new_u->sit_u), &(old_u->sit_u), c3n);
}
}
u3h_put(u3R->jed.han_p, key, u3a_outa(new_u));
u3z(key);
return new_u;
}
}
2018-04-30 20:56:19 +03:00
/* _cj_hank_fine(): check that cached hook information is valid
* for given core. *inn will point to the hooked
* core on return if valid. RETAIN.
*/
2018-04-27 02:36:29 +03:00
static c3_o
_cj_hank_fine(_cj_hank* han_u, u3_noun cor, u3_noun *inn)
{
u3_noun hax = han_u->hax;
if ( u3_none == hax ) {
return c3n;
}
else {
*inn = u3r_at(hax, cor);
if ( u3_none == *inn ) {
return c3n;
}
else {
u3j_site* sit_u = &(han_u->sit_u);
c3_assert(u3_none != sit_u->loc);
return _cj_fine(*inn, sit_u->fin_p);
2018-04-27 02:36:29 +03:00
}
}
}
2018-04-30 20:56:19 +03:00
/* _cj_hank_lose(): release memory maintained in a hook cache.
*/
2018-04-27 02:36:29 +03:00
static void
_cj_hank_lose(_cj_hank* han_u)
{
if ( u3_none != han_u->hax ) {
u3z(han_u->hax);
u3j_site_lose(&(han_u->sit_u));
}
}
2018-04-30 21:10:01 +03:00
/* _cj_hank_fill(): slow path, populate han_u.
2018-04-30 20:56:19 +03:00
*/
2018-04-27 02:36:29 +03:00
static u3_noun
_cj_hank_fill(_cj_hank* han_u, u3_noun tam, u3_noun cor)
{
u3_weak loc, col;
u3_noun got, pat, nam, huc;
u3_noun hax = 1;
u3j_site* sit_u = &(han_u->sit_u);
if ( c3n == u3du(cor) ) {
return u3m_bail(c3__fail);
}
2018-05-26 22:03:09 +03:00
sit_u->bas = u3_none;
if ( u3_none == (col = loc = _cj_spot(cor, NULL)) ) {
2018-04-27 02:36:29 +03:00
return u3m_bail(c3__fail);
}
while ( 1 ) {
u3x_trel(loc, &pat, &nam, &huc);
got = u3qdb_get(huc, tam);
if ( u3_nul != got ) {
u3_noun fol = u3k(u3t(got));
u3z(got);
sit_u->bat = u3k(u3h(cor));
sit_u->loc = u3k(loc);
sit_u->fin_p = _cj_cast(cor, loc);
2018-04-27 02:36:29 +03:00
sit_u->fon_o = c3y;
if ( 0 == (sit_u->axe = _cj_axis(fol)) ) {
sit_u->jet_o = c3n;
2018-05-26 04:17:19 +03:00
sit_u->pog_p = _cj_prog(loc, fol);
2018-04-27 02:36:29 +03:00
}
else {
// loc already known-valid
2018-05-26 04:17:19 +03:00
han_u->sit_u.pog_p = _cj_prog(loc, u3r_at(sit_u->axe, cor));
2018-04-27 02:36:29 +03:00
han_u->sit_u.jet_o = _cj_nail(loc, sit_u->axe,
&(sit_u->lab), &(sit_u->cop_u), &(sit_u->ham_u));
}
u3z(fol);
u3z(col);
han_u->hax = hax;
return cor;
}
else if ( c3n == u3h(pat) ) {
u3_noun dyn = u3t(pat),
axe = u3h(dyn),
xah;
// axe already known-valid
2018-04-27 02:36:29 +03:00
cor = u3r_at(axe, cor);
loc = u3t(dyn);
xah = u3qc_peg(axe, hax);
u3z(hax);
hax = xah;
}
else {
u3_noun sat = u3t(pat);
if ( c3y == u3h(sat) ) {
return u3m_bail(c3__fail);
}
else {
u3_noun xah;
cor = u3t(cor);
loc = u3t(sat);
xah = u3qc_peg(3, hax);
u3z(hax);
hax = xah;
}
}
}
}
2018-05-17 01:18:17 +03:00
/* _cj_sink(): kick by nock.
*/
static u3_noun
_cj_sink(u3_noun cor, u3_noun axe)
{
u3_noun fol = u3x_at(axe, cor);
u3z(axe);
return u3n_nock_on(cor, u3k(fol));
2018-05-17 01:18:17 +03:00
}
2014-11-06 03:20:01 +03:00
/* u3j_kick(): new kick.
2014-09-02 04:10:04 +04:00
**
** `axe` is RETAINED by the caller; `cor` is RETAINED iff there
** is no kick, TRANSFERRED if one.
*/
2014-09-06 00:13:24 +04:00
u3_weak
2014-11-06 03:20:01 +03:00
u3j_kick(u3_noun cor, u3_noun axe)
2014-10-25 07:49:27 +04:00
{
2014-12-03 00:53:35 +03:00
u3t_on(glu_o);
2018-05-26 22:03:09 +03:00
u3_weak loc = _cj_spot(cor, NULL);
if ( u3_none == loc ) {
u3t_off(glu_o);
return u3_none;
}
else {
u3_weak act = _cj_find_warm(loc);
u3z(loc);
if ( u3_none == act ) {
u3t_off(glu_o);
return u3_none;
}
else {
c3_l jax_l;
u3_noun hap, bal, jit, inx;
2014-10-25 07:49:27 +04:00
u3x_qual(act, &jax_l, &hap, &bal, &jit);
2014-10-25 07:49:27 +04:00
if ( u3_none == (inx = u3kdb_get(u3k(hap), u3k(axe))) ) {
2014-12-03 20:36:42 +03:00
u3t_off(glu_o);
{
c3_o pof_o = __(u3C.wag_w & u3o_debug_cpu);
if ( _(pof_o) ) {
pof_o = u3t_come(bal);
2014-12-03 20:36:42 +03:00
}
u3z(act);
2014-12-03 20:36:42 +03:00
if ( _(pof_o) ) {
2018-05-17 01:18:17 +03:00
u3_noun pro = _cj_sink(cor, u3k(axe));
2014-12-03 20:36:42 +03:00
u3t_flee();
return pro;
}
else {
return u3_none;
}
}
2014-10-25 07:49:27 +04:00
}
else {
2014-11-08 01:54:59 +03:00
u3j_core* cop_u = &u3D.ray_u[jax_l];
2014-11-29 05:19:11 +03:00
c3_l inx_l = inx;
2014-11-08 01:54:59 +03:00
u3j_harm* ham_u = &cop_u->arm_u[inx_l];
2014-11-29 05:19:11 +03:00
c3_o pof_o = __(u3C.wag_w & u3o_debug_cpu);
u3_noun pro;
2014-10-25 07:49:27 +04:00
2014-11-29 05:19:11 +03:00
if ( _(pof_o) ) {
pof_o = u3t_come(bal);
2014-11-29 05:19:11 +03:00
}
u3z(act);
2014-12-03 00:53:35 +03:00
u3t_off(glu_o);
2014-10-28 20:36:22 +03:00
pro = _cj_kick_z(cor, cop_u, ham_u, axe);
2014-12-03 00:53:35 +03:00
2014-11-29 05:19:11 +03:00
if ( u3_none == pro ) {
if ( _(pof_o) ) {
2018-05-17 01:18:17 +03:00
pro = _cj_sink(cor, u3k(axe));
2014-10-28 20:36:22 +03:00
2014-11-29 05:19:11 +03:00
u3t_flee();
return pro;
}
else return u3_none;
}
else {
if ( _(pof_o) ) {
u3t_flee();
}
return pro;
}
2014-10-25 07:49:27 +04:00
}
}
}
}
2018-04-30 21:10:01 +03:00
/* _cj_fink_take(): copy u3j_fink from junior road.
*/
static u3j_fink*
_cj_fink_take(u3j_fink* jun_u)
{
c3_w i_w, len_w = jun_u->len_w;
u3j_fink* fin_u = u3a_walloc(c3_wiseof(u3j_fink) +
(len_w * c3_wiseof(u3j_fist)));
fin_u->len_w = len_w;
fin_u->sat = u3a_take(jun_u->sat);
for ( i_w = 0; i_w < len_w; ++i_w ) {
u3j_fist* fis_u = &(fin_u->fis_u[i_w]);
u3j_fist* sif_u = &(jun_u->fis_u[i_w]);
fis_u->bat = u3a_take(sif_u->bat);
fis_u->pax = u3a_take(sif_u->pax);
}
return fin_u;
}
2018-04-30 21:21:04 +03:00
/* _cj_fink_free(): lose and free everything in a u3j_fink.
2018-04-30 21:10:01 +03:00
*/
static void
2018-04-30 21:21:04 +03:00
_cj_fink_free(u3p(u3j_fink) fin_p)
{
c3_w i_w;
2018-04-30 21:21:04 +03:00
u3j_fink* fin_u = u3to(u3j_fink, fin_p);
u3z(fin_u->sat);
for ( i_w = 0; i_w < fin_u->len_w; ++i_w ) {
u3j_fist* fis_u = &(fin_u->fis_u[i_w]);
u3z(fis_u->bat);
u3z(fis_u->pax);
}
u3a_wfree(fin_u);
}
/* u3j_rite_copy(): copy rite references from src_u to dst_u,
** losing old references if los_o is yes
*/
void
u3j_rite_copy(u3j_rite* dst_u, u3j_rite* src_u, c3_o los_o)
{
if ( u3_none == src_u->clu ) {
dst_u->clu = u3_none;
2018-04-30 19:57:09 +03:00
dst_u->fin_p = 0;
}
else {
2018-04-30 21:21:04 +03:00
u3p(u3j_fink) fon_p = dst_u->fin_p;
2018-04-30 19:57:09 +03:00
u3_noun old = dst_u->clu;
c3_o own_o = dst_u->own_o;
if ( c3y == src_u->own_o ) {
dst_u->own_o = c3y;
dst_u->clu = u3a_take(src_u->clu);
2018-04-30 19:57:09 +03:00
dst_u->fin_p = u3of(u3j_fink, _cj_fink_take(u3to(u3j_fink, src_u->fin_p)));
if ( (c3y == los_o) &&
(u3_none != old) &&
(c3y == own_o) ) {
u3z(old);
2018-04-30 21:21:04 +03:00
_cj_fink_free(fon_p);
}
}
}
}
/* u3j_site_copy(): copy site references from src_u to dst_u,
** losing old references if los_o is yes
*/
void
u3j_site_copy(u3j_site* dst_u, u3j_site* src_u, c3_o los_o)
{
u3_noun old = dst_u->axe;
dst_u->axe = u3a_take(src_u->axe);
if ( c3y == los_o ) {
u3z(old);
}
else {
dst_u->bat = u3_none;
2018-05-08 23:33:55 +03:00
dst_u->bas = u3_none;
2018-04-30 20:16:47 +03:00
dst_u->pog_p = 0;
dst_u->loc = u3_none;
dst_u->lab = u3_none;
dst_u->jet_o = c3n;
dst_u->fon_o = c3n;
dst_u->cop_u = NULL;
dst_u->ham_u = NULL;
2018-04-30 20:16:47 +03:00
dst_u->fin_p = 0;
}
if ( u3_none != src_u->loc ) {
u3_noun lob = dst_u->lab,
lod = dst_u->loc;
c3_o fon_o = dst_u->fon_o;
2018-04-30 20:16:47 +03:00
u3p(u3j_fink) fon_p = dst_u->fin_p;
dst_u->loc = u3a_take(src_u->loc);
dst_u->lab = u3a_take(src_u->lab);
dst_u->cop_u = src_u->cop_u;
dst_u->ham_u = src_u->ham_u;
dst_u->jet_o = src_u->jet_o;
if ( c3y == src_u->fon_o ) {
2018-04-30 20:16:47 +03:00
dst_u->fin_p = u3of(u3j_fink, _cj_fink_take(u3to(u3j_fink, src_u->fin_p)));
dst_u->fon_o = c3y;
}
2018-04-30 20:16:47 +03:00
else if ( fon_p != src_u->fin_p ) {
dst_u->fin_p = src_u->fin_p;
dst_u->fon_o = c3n;
}
else {
fon_o = c3n;
}
if ( c3y == los_o ) {
if ( u3_none != lod ) {
u3z(lod);
u3z(lob);
if ( c3y == fon_o ) {
2018-04-30 21:21:04 +03:00
_cj_fink_free(fon_p);
}
}
}
}
}
2018-04-27 02:36:29 +03:00
/* u3j_site_ream(): refresh u3j_site after restoring from checkpoint
*/
void
u3j_site_ream(u3j_site* sit_u)
{
if ( u3_none != sit_u->loc ) {
u3z(sit_u->lab);
sit_u->jet_o = _cj_nail(sit_u->loc, sit_u->axe,
&(sit_u->lab), &(sit_u->cop_u), &(sit_u->ham_u));
}
}
2018-04-30 21:10:01 +03:00
/* _cj_site_lock(): ensure site has a valid program pointer
*/
static void
2018-05-26 04:17:19 +03:00
_cj_site_lock(u3_noun loc, u3_noun cor, u3j_site* sit_u)
{
if ( (u3_none != sit_u->bat) &&
(c3y == u3r_sing(sit_u->bat, u3h(cor))) ) {
2018-04-30 21:10:01 +03:00
return;
}
sit_u->pog_p = _cj_prog(loc, u3x_at(sit_u->axe, cor));
if ( u3_none != sit_u->bat ) {
u3z(sit_u->bat);
}
sit_u->bat = u3k(u3h(cor));
2018-04-30 21:10:01 +03:00
}
/* _cj_burn(): stop tracing glu and call a nock program
*/
static u3_noun
_cj_burn(u3p(u3n_prog) pog_p, u3_noun cor)
{
u3_noun pro;
u3t_off(glu_o);
pro = u3n_burn(pog_p, cor);
u3t_on(glu_o);
return pro;
}
2018-04-30 21:21:04 +03:00
/* _cj_site_kick_hot(): execute site's kick on located core
** (no validity checks).
*/
static u3_weak
2018-05-26 04:17:19 +03:00
_cj_site_kick_hot(u3_noun loc, u3_noun cor, u3j_site* sit_u)
{
u3_weak pro = u3_none;
c3_o jet_o = sit_u->jet_o;
2018-04-27 22:22:39 +03:00
c3_o pof_o = __(u3C.wag_w & u3o_debug_cpu);
if ( c3n == pof_o ) {
if ( c3n == jet_o ) {
pro = u3_none;
}
else {
2018-04-27 22:22:39 +03:00
u3t_off(glu_o);
pro = _cj_kick_z(cor, sit_u->cop_u, sit_u->ham_u, sit_u->axe);
2018-04-27 22:22:39 +03:00
u3t_on(glu_o);
}
if ( u3_none == pro ) {
2018-05-26 04:17:19 +03:00
_cj_site_lock(loc, cor, sit_u);
}
}
else {
2018-04-27 22:22:39 +03:00
pof_o = u3t_come(sit_u->lab);
if ( c3y == jet_o ) {
2018-04-27 22:22:39 +03:00
u3t_off(glu_o);
pro = _cj_kick_z(cor, sit_u->cop_u, sit_u->ham_u, sit_u->axe);
2018-04-27 22:22:39 +03:00
u3t_on(glu_o);
}
if ( u3_none == pro ) {
2018-05-26 04:17:19 +03:00
_cj_site_lock(loc, cor, sit_u);
2018-04-30 21:10:01 +03:00
pro = _cj_burn(sit_u->pog_p, cor);
}
2018-04-27 22:22:39 +03:00
if ( c3y == pof_o ) {
u3t_flee();
}
}
return pro;
}
2018-04-30 21:21:04 +03:00
/* _cj_site_kick(): execute site's kick on core.
*/
2018-04-27 02:36:29 +03:00
static u3_weak
_cj_site_kick(u3_noun cor, u3j_site* sit_u)
{
u3_weak loc, pro;
loc = pro = u3_none;
if ( u3_none != sit_u->loc ) {
if ( c3y == _cj_fine(cor, sit_u->fin_p) ) {
loc = sit_u->loc;
if ( c3y == sit_u->jet_o ) {
2018-05-26 04:17:19 +03:00
pro = _cj_site_kick_hot(loc, cor, sit_u);
}
}
}
if ( u3_none == loc ) {
2018-05-26 22:03:09 +03:00
loc = _cj_spot(cor, &(sit_u->bas));
if ( u3_none != loc ) {
2018-04-30 20:16:47 +03:00
u3p(u3j_fink) fon_p = 0;
u3_weak lod = u3_none;
u3_weak lob = u3_none;
if ( u3_none != sit_u->loc ) {
lod = sit_u->loc;
lob = sit_u->lab;
if ( c3y == sit_u->fon_o ) {
2018-04-30 20:16:47 +03:00
fon_p = sit_u->fin_p;
}
}
sit_u->loc = loc;
sit_u->fin_p = _cj_cast(cor, loc);
sit_u->fon_o = c3y;
if ( c3y ==
(sit_u->jet_o = _cj_nail(loc, sit_u->axe,
&(sit_u->lab), &(sit_u->cop_u), &(sit_u->ham_u))) )
{
2018-05-26 04:17:19 +03:00
pro = _cj_site_kick_hot(loc, cor, sit_u);
}
else {
pro = u3_none;
}
if ( u3_none != lod ) {
u3z(lod);
u3z(lob);
2018-04-30 20:16:47 +03:00
if ( 0 != fon_p ) {
2018-04-30 21:21:04 +03:00
_cj_fink_free(fon_p);
}
}
}
}
if ( u3_none == pro ) {
2018-05-26 04:17:19 +03:00
_cj_site_lock(loc, cor, sit_u);
}
2018-04-27 02:36:29 +03:00
return pro;
}
/* u3j_site_kick(): kick a core with a u3j_site cache.
*/
u3_weak
u3j_site_kick(u3_noun cor, u3j_site* sit_u)
{
u3_weak pro;
u3t_on(glu_o);
pro = _cj_site_kick(cor, sit_u);
u3t_off(glu_o);
return pro;
}
/* u3j_cook(): Execute hook from core, call site cached by arbitrary c string
*/
u3_noun
u3j_cook(const c3_c* key_c,
u3_noun cor,
const c3_c* tam_c)
{
u3_noun pro, key, tam, inn;
_cj_hank* han_u;
u3t_on(glu_o);
key = u3i_string(key_c);
tam = u3i_string(tam_c);
han_u = _cj_hank_find(key, tam);
if ( c3n == _cj_hank_fine(han_u, cor, &inn) ) {
_cj_hank_lose(han_u);
inn = _cj_hank_fill(han_u, tam, cor);
}
pro = _cj_site_kick(u3k(inn), &(han_u->sit_u));
if ( u3_none == pro ) {
2018-04-30 20:56:19 +03:00
pro = _cj_burn(han_u->sit_u.pog_p, inn);
2018-04-27 02:36:29 +03:00
}
u3z(cor);
u3z(key);
u3z(tam);
u3t_off(glu_o);
return pro;
}
2014-11-06 03:20:01 +03:00
/* u3j_kink(): kick either by jet or by nock.
2014-09-02 04:10:04 +04:00
*/
2014-09-06 00:13:24 +04:00
u3_noun
2018-04-27 02:36:29 +03:00
u3j_kink(u3_noun cor, u3_noun axe)
2014-09-02 04:10:04 +04:00
{
2014-11-06 03:20:01 +03:00
u3_weak pro = u3j_kick(cor, axe);
2014-09-02 04:10:04 +04:00
2014-11-06 02:36:30 +03:00
if ( u3_none != pro ) {
2014-09-02 04:10:04 +04:00
return pro;
2018-04-27 02:36:29 +03:00
}
else {
2018-05-17 01:18:17 +03:00
return _cj_sink(cor, axe);
2014-09-02 04:10:04 +04:00
}
}
/* u3j_gate_prep(): prepare a locally cached gate to call repeatedly.
2018-04-30 19:57:09 +03:00
* core is TRANSFERRED.
*/
void
u3j_gate_prep(u3j_site* sit_u, u3_noun cor)
{
u3_noun loc;
u3t_on(glu_o);
2018-04-27 19:33:33 +03:00
if ( c3n == u3du(cor) || c3n == u3du(u3t(cor)) ) {
u3m_bail(c3__exit);
2018-04-30 19:57:09 +03:00
return;
2018-04-27 19:33:33 +03:00
}
2018-05-26 22:03:09 +03:00
sit_u->bas = u3_none;
sit_u->axe = 2;
2018-04-30 19:57:09 +03:00
sit_u->bat = cor; // a lie, this isn't really the battery!
2018-05-26 22:03:09 +03:00
sit_u->loc = loc = _cj_spot(cor, &(sit_u->bas));
2018-05-26 04:17:19 +03:00
sit_u->pog_p = _cj_prog(loc, u3h(cor));
if ( u3_none != loc ) {
u3_noun pax = _cj_loc_axe(loc),
pay = u3qc_cap(pax),
pam = u3qc_mas(pax);
if ( 3 != pay || 2 == pam || (3 != pam && 3 != u3qc_cap(pam)) ) {
fprintf(stderr, "u3j_gate_prep(): parent axis includes sample\r\n");
u3m_p("axis", pax);
u3_weak act = _cj_find_warm(loc);
c3_assert( u3_none != act );
sit_u->jet_o = c3n;
sit_u->lab = u3k(u3h(u3t(u3t(act))));
u3z(act);
}
else {
sit_u->jet_o = _cj_nail(loc, 2,
&(sit_u->lab), &(sit_u->cop_u), &(sit_u->ham_u));
}
u3z(pam); u3z(pax);
}
u3t_off(glu_o);
}
/* u3j_gate_slam(): slam a site prepared by u3j_gate_find() with sample.
2018-04-30 21:21:04 +03:00
* sam is TRANSFERRED.
*/
u3_noun
u3j_gate_slam(u3j_site* sit_u, u3_noun sam)
{
u3_weak pro;
u3_noun cor;
2018-04-27 22:22:39 +03:00
u3t_on(glu_o);
pro = u3_none;
cor = u3nt(u3k(u3h(sit_u->bat)),
sam,
u3k(u3t(u3t(sit_u->bat))));
if ( (u3_none != sit_u->loc) && (c3y == sit_u->jet_o) ) {
2018-05-26 04:17:19 +03:00
pro = _cj_site_kick_hot(sit_u->loc, cor, sit_u);
}
if ( u3_none == pro ) {
2018-04-30 20:56:19 +03:00
pro = _cj_burn(sit_u->pog_p, cor);
}
u3t_off(glu_o);
return pro;
}
/* u3j_gate_lose(): clean up site prepared by u3j_gate_find().
*/
void
u3j_gate_lose(u3j_site* sit_u)
{
u3z(sit_u->bat);
2018-05-16 21:49:08 +03:00
u3z(sit_u->bas);
if ( u3_none != sit_u->loc ) {
u3z(sit_u->loc);
u3z(sit_u->lab);
}
}
2018-05-12 22:16:36 +03:00
/* _cj_minx(): produce location of core from fsck'd clue. RETAIN.
*/
2018-04-15 00:57:12 +03:00
static u3_weak
2018-05-12 22:16:36 +03:00
_cj_minx(u3_noun cey, u3_noun cor)
2014-11-18 23:59:59 +03:00
{
2018-05-12 22:16:36 +03:00
u3_noun nam, axe, huc;
2018-03-28 21:27:33 +03:00
u3x_trel(cey, &nam, &axe, &huc);
2018-05-12 22:16:36 +03:00
2018-03-26 19:58:36 +03:00
if ( 0 == axe ) {
2018-05-12 22:16:36 +03:00
return u3nt(u3nt(c3y, c3y, u3k(u3t(cor))), u3k(nam), u3k(huc));
2018-03-26 19:58:36 +03:00
}
else {
2018-05-12 22:16:36 +03:00
u3_weak par, pel;
2018-03-30 02:18:22 +03:00
u3_noun pat;
par = u3r_at(axe, cor);
2018-03-26 19:58:36 +03:00
if ( u3_none == par || c3n == u3du(par) ) {
fprintf(stderr, "fund: %s is bogus\r\n", u3r_string(nam));
2018-04-15 00:57:12 +03:00
return u3_none;
2018-03-26 19:58:36 +03:00
}
2018-05-26 22:03:09 +03:00
pel = _cj_spot(par, NULL);
if ( u3_none == pel ) {
fprintf(stderr, "fund: in %s, parent %x not found at %d\r\n",
u3r_string(nam),
u3r_mug(u3h(par)),
axe);
2018-04-15 00:57:12 +03:00
return u3_none;
2014-11-19 02:17:56 +03:00
}
2018-03-30 23:01:03 +03:00
pat = ( ( 3 == axe ) && (c3y == u3h(u3h(pel))) )
2018-05-12 22:16:36 +03:00
? u3nt(c3y, c3n, pel)
: u3nt(c3n, u3k(axe), pel);
return u3nt(pat, u3k(nam), u3k(huc));
}
}
static void
_cj_print_tas(FILE* fh, u3_noun tas)
{
c3_w met_w = u3r_met(3, tas);
c3_c* str_c = alloca(met_w + 1);
u3r_bytes(0, met_w, (c3_y*)str_c, tas);
str_c[met_w] = 0;
fprintf(fh, "/%s", str_c);
}
/* _cj_mine(): declare a core and produce location. RETAIN.
*/
static u3_weak
2018-05-26 22:03:09 +03:00
_cj_mine(u3_noun cey, u3_noun cor, u3_noun bas)
2018-05-12 22:16:36 +03:00
{
u3_weak loc = _cj_minx(cey, cor);
if ( u3_none != loc ) {
c3_l par_l, jax_l;
u3_noun pel = _cj_loc_pel(loc),
axe = _cj_loc_axe(loc),
bat = u3h(cor),
nam = u3h(u3t(loc)),
2018-05-26 22:03:09 +03:00
bar = _cj_find_cold(bat),
reg, hap, bal, act;
if ( u3_none == bar ) {
reg = u3_none;
}
else {
reg = u3k(u3t(bar));
u3z(bar);
}
reg = _cj_gust(reg, u3k(axe), u3k(pel), u3k(loc));
2018-05-12 22:16:36 +03:00
if ( 0 == axe ) {
par_l = 0;
bal = u3nc(u3k(nam), u3_nul);
}
else {
u3_weak pac = _cj_find_warm(pel);
c3_assert(u3_none != pac);
par_l = u3h(pac);
bal = u3nc(u3k(nam), u3k(u3h(u3t(u3t(pac)))));
u3z(pac);
}
jax_l = _cj_hot_mean(par_l, nam);
#if 0
u3m_p("new jet", bal);
fprintf(stderr, " bat %x, jax %d\r\n", u3r_mug(bat), jax_l);
2014-12-02 01:05:09 +03:00
#endif
2018-10-02 02:56:14 +03:00
if ( !(u3C.wag_w & u3o_hashless) ) {
if ( jax_l ) {
c3_y dig_y[32];
c3_w i_w;
u3_noun i = bal;
fprintf(stderr, "hot jet: ");
while ( i != u3_nul ) {
_cj_print_tas(stderr, u3h(i));
i = u3t(i);
}
fprintf(stderr, "\r\n axe %d, jax %d,\r\n bash ", axe, jax_l);
u3r_bytes(0, 32, dig_y, bas);
for ( i_w = 32; i_w > 0; ) {
fprintf(stderr, "%02x", dig_y[--i_w]);
}
fprintf(stderr, "\r\n");
2018-05-30 07:33:10 +03:00
}
2018-05-12 22:16:36 +03:00
}
2018-10-02 02:56:14 +03:00
2018-05-12 22:16:36 +03:00
hap = _cj_warm_hump(jax_l, u3t(u3t(loc)));
act = u3nq(jax_l, hap, bal, _cj_jit(jax_l, bat));
2018-05-26 22:03:09 +03:00
u3h_put(u3R->jed.cod_p, bat, u3nc(u3k(bas), reg));
2018-05-12 22:16:36 +03:00
u3h_put(u3R->jed.war_p, loc, act); // see note in _cj_spot
u3z(pel); u3z(axe);
}
2018-05-08 23:33:55 +03:00
2018-05-12 22:16:36 +03:00
return loc;
#if 0
2018-05-08 23:33:55 +03:00
{
u3_noun bas = _cj_bash(bat),
hot = u3h_get(u3D.hot_p, bas);
2018-05-08 23:33:55 +03:00
c3_o hav_o = c3n;
if ( u3_none != hot ) {
u3_noun her = u3h(hot),
hol = _cj_reg_find(her, cor);
2018-05-08 23:33:55 +03:00
if ( hol != u3_none ) {
hav_o = c3y;
u3z(hol);
2018-05-08 23:33:55 +03:00
}
u3z(hot);
}
if ( c3n == hav_o ) {
u3m_p("unregistered battery", bal);
2018-05-08 23:33:55 +03:00
fprintf(stderr, "hash: %x\r\n", bas);
}
u3z(bas);
}
2018-05-12 22:16:36 +03:00
#endif
2018-04-15 00:57:12 +03:00
}
2018-05-12 22:16:36 +03:00
static void
_cj_audit(u3_noun loc, u3_noun cey, u3_noun cor)
2018-04-15 00:57:12 +03:00
{
u3_noun pat, nam, huc, cax, can, cuc, pax;
u3x_trel(loc, &pat, &nam, &huc);
u3x_trel(cey, &can, &cax, &cuc);
pax = _cj_loc_axe(loc);
if ( (c3n == u3r_sing(nam, can)) ||
(c3n == u3r_sing(pax, cax)) ||
(c3n == u3r_sing(huc, cuc)) ) {
2018-05-12 22:16:36 +03:00
u3_noun mix = _cj_minx(cey, cor);
u3m_p("bad audit", loc);
u3m_p("hint says", mix);
u3z(mix);
2018-04-15 00:57:12 +03:00
}
u3z(pax);
2018-04-15 00:57:12 +03:00
}
/* _cj_mile(): register core for jets, returning location.
2018-04-15 00:57:12 +03:00
*/
static u3_weak
_cj_mile(u3_noun clu, u3_noun cor)
2018-04-15 00:57:12 +03:00
{
u3_weak loc = u3_none;
if ( c3n == u3du(cor) ) {
u3z(clu);
u3z(cor);
}
else {
u3_weak cey = _cj_je_fsck(clu);
if ( u3_none != cey ) {
2018-05-26 22:03:09 +03:00
u3_weak bas = u3_none;
loc = _cj_spot(cor, &bas);
if ( u3_none == loc ) {
2018-05-26 22:03:09 +03:00
loc = _cj_mine(cey, cor, bas);
}
else {
2018-05-12 22:16:36 +03:00
_cj_audit(loc, cey, cor);
}
u3z(cey);
if ( u3_none != bas ) {
u3z(bas);
}
}
u3z(cor);
2018-04-15 00:57:12 +03:00
}
return loc;
2014-11-18 23:59:59 +03:00
}
2014-11-19 02:17:56 +03:00
2014-11-20 22:49:27 +03:00
/* u3j_mine(): register core for jets.
2014-09-02 04:10:04 +04:00
*/
2014-10-25 01:04:44 +04:00
void
2014-11-06 03:20:01 +03:00
u3j_mine(u3_noun clu, u3_noun cor)
2014-09-02 04:10:04 +04:00
{
u3_weak loc;
2014-12-03 00:53:35 +03:00
u3t_on(glu_o);
loc = _cj_mile(clu, cor);
u3z(loc);
2014-12-03 00:53:35 +03:00
u3t_off(glu_o);
2014-09-02 04:10:04 +04:00
}
2014-10-31 00:40:05 +03:00
/* u3j_rite_mine(): mine cor with clue, using u3j_rite for caching
*/
void
u3j_rite_mine(u3j_rite* rit_u, u3_noun clu, u3_noun cor)
{
c3_t non_t;
u3t_on(glu_o);
non_t = (u3_none == rit_u->clu);
if ( non_t ||
c3n == u3r_sing(rit_u->clu, clu) ||
c3n == _cj_fine(cor, rit_u->fin_p) ) {
u3_weak loc = _cj_mile(u3k(clu), u3k(cor));
if ( u3_none != loc ) {
2018-04-30 21:21:04 +03:00
u3p(u3j_fink) fon_p = rit_u->fin_p;
u3_noun old = rit_u->clu;
c3_o own_o = rit_u->own_o;
rit_u->own_o = c3y;
rit_u->clu = u3k(clu);
rit_u->fin_p = _cj_cast(cor, loc);
u3z(loc);
if ( !non_t && (c3y == own_o) ) {
u3z(old);
2018-04-30 21:21:04 +03:00
_cj_fink_free(fon_p);
}
}
}
u3z(clu);
u3z(cor);
u3t_off(glu_o);
}
/* _cj_warm_reap(): reap key and value from warm table.
2014-10-31 00:40:05 +03:00
*/
static void
_cj_warm_reap(u3_noun kev)
2014-10-31 00:40:05 +03:00
{
u3_noun loc = u3a_take(u3h(kev));
u3_noun act = u3a_take(u3t(kev));
2018-03-30 21:49:18 +03:00
u3h_put(u3R->jed.war_p, loc, act);
u3z(loc);
2014-10-31 00:40:05 +03:00
}
/* _cj_cold_reap(): reap cold dashboard entries.
*/
2014-10-31 00:40:05 +03:00
static void
_cj_cold_reap(u3_noun kev)
2014-10-31 00:40:05 +03:00
{
u3_noun bat = u3a_take(u3h(kev));
2018-05-12 22:15:17 +03:00
u3_noun reg = u3a_take(u3t(kev));
u3h_put(u3R->jed.cod_p, bat, reg);
2018-05-12 22:15:17 +03:00
u3z(bat);
2014-10-31 00:40:05 +03:00
}
2018-05-26 03:53:36 +03:00
/* _cj_bash_reap(): reap battery hashes.
*/
static void
_cj_bash_reap(u3_noun kev)
{
u3_noun key = u3a_take(u3h(kev)),
val = u3a_take(u3t(kev));
u3h_put(u3R->jed.bas_p, key, val);
u3z(key);
}
2018-04-30 21:21:04 +03:00
/* _cj_hank_reap(): reap hook resolutions.
*/
2018-04-27 02:36:29 +03:00
static void
_cj_hank_reap(u3_noun kev)
{
u3_noun key = u3a_take(u3h(kev));
u3_noun got = u3h_git(u3R->jed.han_p, key);
_cj_hank* nah_u = u3to(_cj_hank, u3t(kev));
_cj_hank* han_u;
if ( u3_none != got ) {
if ( u3_none != nah_u->hax ) {
u3_weak old;
han_u = u3to(_cj_hank, got);
old = han_u->hax;
han_u->hax = u3a_take(nah_u->hax);
u3j_site_copy(&(han_u->sit_u), &(nah_u->sit_u), c3y);
if ( u3_none != old ) {
u3z(old);
}
}
}
else if ( u3_none != nah_u->hax ) {
han_u = u3a_walloc(c3_wiseof(_cj_hank));
han_u->hax = u3a_take(nah_u->hax);
u3j_site_copy(&(han_u->sit_u), &(nah_u->sit_u), c3n);
u3h_put(u3R->jed.han_p, key, u3a_outa(han_u));
}
u3z(key);
}
/* u3j_reap(): promote jet state.
2014-10-31 00:40:05 +03:00
*/
void
2018-05-26 03:53:36 +03:00
u3j_reap(u3p(u3h_root) cod_p, u3p(u3h_root) war_p, u3p(u3h_root) han_p, u3p(u3h_root) bas_p)
2014-10-31 00:40:05 +03:00
{
u3h_walk(cod_p, _cj_cold_reap);
u3h_walk(war_p, _cj_warm_reap);
2018-04-27 02:36:29 +03:00
u3h_walk(han_p, _cj_hank_reap);
2018-05-26 03:53:36 +03:00
u3h_walk(bas_p, _cj_bash_reap);
2014-11-18 02:24:40 +03:00
}
2014-11-28 23:29:10 +03:00
2018-05-30 21:32:10 +03:00
/* _cj_ream(): ream list of battery [bash registry] pairs. RETAIN.
2018-03-30 21:13:48 +03:00
*/
static void
_cj_ream(u3_noun all)
2014-11-28 23:29:10 +03:00
{
2018-03-30 21:13:48 +03:00
c3_l par_l, jax_l;
u3_noun i, j, k, rul, loc, bal, act, lop, kev, rut, hap,
2018-03-30 21:49:18 +03:00
pat, reg, pol, rem, rec, bat, pel, nam, huc;
2018-03-30 21:13:48 +03:00
u3_weak pac;
for ( i = all, lop = u3_nul; i != u3_nul; i = u3t(i) ) {
kev = u3h(i);
bat = u3h(kev);
2018-05-30 21:32:10 +03:00
reg = u3t(u3t(kev));
rut = u3h(reg);
// register roots
rul = u3qdb_tap(rut);
2018-03-30 21:13:48 +03:00
for ( j = rul; j != u3_nul; j = u3t(j) ) {
loc = u3t(u3h(j));
u3x_trel(loc, &pat, &nam, &huc);
bal = u3nc(u3k(nam), u3_nul);
jax_l = _cj_hot_mean(0, nam);
hap = _cj_warm_hump(jax_l, huc);
act = u3nq(jax_l, hap, bal, _cj_jit(jax_l, bat));
#if 0
u3m_p("old jet", bal);
fprintf(stderr, " bat %x, jax %d\r\n", u3r_mug(bat), jax_l);
#endif
u3h_put(u3R->jed.war_p, loc, act);
}
u3z(rul);
2018-03-30 21:13:48 +03:00
// put ancestors in lop (list [battery=^ parent=location this=location])
for ( j = u3t(reg); j != u3_nul; j = u3t(j) ) {
pol = lop;
lop = u3qdb_tap(u3t(u3h(j)));
for ( k = lop; u3_nul != k; k = u3t(k) ) {
pol = u3nc(u3nc(u3k(bat), u3k(u3h(k))), pol);
}
2018-03-30 21:13:48 +03:00
u3z(lop);
lop = pol;
}
}
2018-03-30 21:17:48 +03:00
// ordering is random so we need to push onto rem when parent
// isn't yet present in the warm state
2018-03-30 21:13:48 +03:00
while ( u3_nul != lop ) {
rem = u3_nul;
2018-03-30 21:13:48 +03:00
for ( i = lop; u3_nul != i; i = u3t(i) ) {
rec = u3h(i);
u3x_trel(rec, &bat, &pel, &loc);
pac = _cj_find_warm(pel);
if ( u3_none == pac ) {
2018-03-30 21:13:48 +03:00
rem = u3nc(u3k(rec), rem);
}
else {
2018-03-30 21:13:48 +03:00
u3x_trel(loc, &pat, &nam, &huc);
par_l = u3h(pac);
jax_l = _cj_hot_mean(par_l, nam);
bal = u3nc(u3k(nam), u3k(u3h(u3t(u3t(pac)))));
hap = _cj_warm_hump(jax_l, huc),
u3z(pac);
act = u3nq(jax_l, hap, bal, _cj_jit(jax_l, bat));
#if 0
u3m_p("old jet", bal);
fprintf(stderr, " bat %x, jax %d\r\n", u3r_mug(bat), jax_l);
#endif
2018-03-30 21:13:48 +03:00
u3h_put(u3R->jed.war_p, loc, act);
}
}
2018-03-30 21:13:48 +03:00
u3z(lop);
lop = rem;
}
2018-03-30 21:13:48 +03:00
}
2018-03-30 21:16:05 +03:00
/* _cj_warm_tap(): tap war_p to rel
*/
static void
2018-04-04 21:51:21 +03:00
_cj_warm_tap(u3_noun kev, void* wit)
2018-03-30 21:16:05 +03:00
{
2018-04-04 21:51:21 +03:00
u3_noun* rel = wit;
*rel = u3nc(u3k(kev), *rel);
2018-03-30 21:16:05 +03:00
}
2018-04-30 21:21:04 +03:00
/* _cj_ream_hank(): clear hot state out of hook sites.
*/
2018-04-27 02:36:29 +03:00
static void
_cj_ream_hank(u3_noun kev)
{
u3j_site_ream(&(u3to(_cj_hank, u3t(kev))->sit_u));
}
2018-04-30 21:21:04 +03:00
/* u3j_ream(): rebuild warm state
2018-03-30 21:13:48 +03:00
*/
void
u3j_ream(void)
{
2018-04-04 21:51:21 +03:00
u3_noun rel = u3_nul;
c3_assert(u3R == &(u3H->rod_u));
2018-03-30 21:13:48 +03:00
u3h_free(u3R->jed.war_p);
u3R->jed.war_p = u3h_new();
2018-04-04 21:51:21 +03:00
u3h_walk_with(u3R->jed.cod_p, _cj_warm_tap, &rel);
2018-03-30 21:13:48 +03:00
_cj_ream(rel);
u3z(rel);
2018-04-27 02:36:29 +03:00
u3h_walk(u3R->jed.han_p, _cj_ream_hank);
2014-11-28 23:29:10 +03:00
}
2018-03-30 21:13:48 +03:00
2018-04-30 21:21:04 +03:00
/* _cj_fink_mark(): mark a u3j_fink for gc.
*/
2018-04-26 21:23:03 +03:00
static c3_w
_cj_fink_mark(u3j_fink* fin_u)
{
c3_w i_w, tot_w = u3a_mark_noun(fin_u->sat);
for ( i_w = 0; i_w < fin_u->len_w; ++i_w ) {
u3j_fist* fis_u = &(fin_u->fis_u[i_w]);
tot_w += u3a_mark_noun(fis_u->bat);
tot_w += u3a_mark_noun(fis_u->pax);
}
tot_w += u3a_mark_ptr(fin_u);
return tot_w;
}
/* u3j_site_lose(): lose references of u3j_site (but do not free).
*/
void
u3j_site_lose(u3j_site* sit_u)
{
u3z(sit_u->axe);
if ( u3_none != sit_u->bat ) {
u3z(sit_u->bat);
}
2018-05-16 07:59:00 +03:00
if ( u3_none != sit_u->bas ) {
u3z(sit_u->bas);
}
if ( u3_none != sit_u->loc ) {
u3z(sit_u->loc);
u3z(sit_u->lab);
if ( c3y == sit_u->fon_o ) {
2018-04-30 21:21:04 +03:00
_cj_fink_free(sit_u->fin_p);
}
}
}
/* u3j_rite_lose(): lose references of u3j_rite (but do not free).
*/
void
u3j_rite_lose(u3j_rite* rit_u)
{
if ( (c3y == rit_u->own_o) && u3_none != rit_u->clu ) {
u3z(rit_u->clu);
2018-04-30 21:21:04 +03:00
_cj_fink_free(rit_u->fin_p);
}
}
2018-04-26 21:23:03 +03:00
/* u3j_rite_mark(): mark u3j_rite for gc.
*/
c3_w
u3j_rite_mark(u3j_rite* rit_u)
{
c3_w tot_w = 0;
if ( (c3y == rit_u->own_o) && u3_none != rit_u->clu ) {
tot_w += u3a_mark_noun(rit_u->clu);
2018-04-30 19:57:09 +03:00
tot_w += _cj_fink_mark(u3to(u3j_fink, rit_u->fin_p));
2018-04-26 21:23:03 +03:00
}
return tot_w;
}
/* u3j_site_mark(): mark u3j_site for gc.
*/
c3_w
u3j_site_mark(u3j_site* sit_u)
{
c3_w tot_w = u3a_mark_noun(sit_u->axe);
if ( u3_none != sit_u->bat ) {
tot_w += u3a_mark_noun(sit_u->bat);
}
2018-05-16 07:59:00 +03:00
if ( u3_none != sit_u->bas ) {
tot_w += u3a_mark_noun(sit_u->bas);
}
2018-04-26 21:23:03 +03:00
if ( u3_none != sit_u->loc ) {
tot_w += u3a_mark_noun(sit_u->loc);
tot_w += u3a_mark_noun(sit_u->lab);
if ( c3y == sit_u->fon_o ) {
2018-04-30 20:16:47 +03:00
tot_w += _cj_fink_mark(u3to(u3j_fink, sit_u->fin_p));
2018-04-26 21:23:03 +03:00
}
}
return tot_w;
}
2018-04-30 21:26:13 +03:00
/* _cj_mark_hank(): mark hank cache for gc.
*/
2018-04-27 02:36:29 +03:00
static void
_cj_mark_hank(u3_noun kev, void* dat)
{
c3_w* tot_w = (c3_w*) dat;
_cj_hank* han_u = u3to(_cj_hank, u3t(kev));
*tot_w += u3a_mark_ptr(han_u);
if ( u3_none != han_u->hax ) {
*tot_w += u3a_mark_noun(han_u->hax);
*tot_w += u3j_site_mark(&(han_u->sit_u));
}
}
/* u3j_mark(): mark jet state for gc.
*/
c3_w
u3j_mark(void)
{
c3_w tot_w = 0;
tot_w += u3h_mark(u3R->jed.war_p);
tot_w += u3h_mark(u3R->jed.cod_p);
tot_w += u3h_mark(u3R->jed.han_p);
2018-05-26 03:53:36 +03:00
tot_w += u3h_mark(u3R->jed.bas_p);
2018-04-27 02:36:29 +03:00
u3h_walk_with(u3R->jed.han_p, _cj_mark_hank, &tot_w);
2018-05-16 07:59:00 +03:00
if ( u3R == &(u3H->rod_u) ) {
tot_w += u3h_mark(u3R->jed.hot_p);
2018-05-16 07:59:00 +03:00
}
2018-04-27 02:36:29 +03:00
return tot_w;
}
2018-04-30 21:26:13 +03:00
/* _cj_free_hank(): free hank cache.
*/
2018-04-27 02:36:29 +03:00
static void
_cj_free_hank(u3_noun kev)
{
_cj_hank* han_u = u3to(_cj_hank, u3t(kev));
if ( u3_none != han_u->hax ) {
u3z(han_u->hax);
u3j_site_lose(&(han_u->sit_u));
}
u3a_wfree(han_u);
}
/* u3j_free(): free jet state.
*/
void
u3j_free(void)
{
u3h_walk(u3R->jed.han_p, _cj_free_hank);
u3h_free(u3R->jed.war_p);
u3h_free(u3R->jed.cod_p);
u3h_free(u3R->jed.han_p);
2018-05-26 03:53:36 +03:00
u3h_free(u3R->jed.bas_p);
2018-05-16 07:59:00 +03:00
if ( u3R == &(u3H->rod_u) ) {
u3h_free(u3R->jed.hot_p);
2018-05-16 07:59:00 +03:00
}
2018-04-27 02:36:29 +03:00
}