urbit/n/j.c

1029 lines
22 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
**
** This file is in the public domain.
*/
2014-09-05 23:55:16 +04:00
#include "all.h"
2014-08-26 20:12:45 +04:00
2014-09-02 04:10:04 +04:00
/* _cj_count(): count and link dashboard entries.
*/
static c3_w
2014-11-08 01:54:59 +03:00
_cj_count(u3j_core* par_u, u3j_core* dev_u)
2014-09-02 04:10:04 +04:00
{
c3_w len_l = 0;
c3_w i_w;
2014-09-07 02:39:28 +04:00
if ( dev_u ) {
for ( i_w = 0; 0 != dev_u[i_w].cos_c; i_w++ ) {
2014-11-08 01:54:59 +03:00
u3j_core* kid_u = &dev_u[i_w];
2014-09-07 02:39:28 +04:00
kid_u->par_u = par_u;
2014-09-07 10:11:09 +04:00
len_l += _cj_count(kid_u, kid_u->dev_u);
2014-09-07 02:39:28 +04:00
}
2014-09-02 04:10:04 +04:00
}
return 1 + len_l;
}
/* _cj_install(): install dashboard entries.
*/
static c3_w
2014-11-08 01:54:59 +03:00
_cj_install(u3j_core* ray_u, c3_w jax_l, u3j_core* dev_u)
2014-09-02 04:10:04 +04:00
{
c3_w i_w;
2014-09-07 02:39:28 +04:00
if ( dev_u ) {
for ( i_w = 0; 0 != dev_u[i_w].cos_c; i_w++ ) {
2014-11-08 01:54:59 +03:00
u3j_core* kid_u = &dev_u[i_w];
2014-09-02 04:10:04 +04:00
2014-09-07 02:39:28 +04:00
kid_u->jax_l = jax_l;
ray_u[jax_l++] = *kid_u;
2014-09-02 04:10:04 +04:00
2014-09-07 02:39:28 +04:00
jax_l = _cj_install(ray_u, jax_l, kid_u->dev_u);
}
2014-09-02 04:10:04 +04:00
}
return jax_l;
}
2014-09-07 02:39:28 +04:00
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;
2014-11-05 04:18:47 +03:00
while ( _(u3du(fol)) && (10 == 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
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
}
2014-11-05 04:18:47 +03:00
while ( _(u3du(q_clu)) && (10 == 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
}
/* _cj_sham(): ++sham.
*/
static u3_atom
_cj_sham(u3_noun som) // XX wrong, does not match ++sham
{
2014-11-06 22:13:57 +03:00
u3_atom jam = u3ke_jam(som);
u3_noun sha = u3qe_shax(jam);
u3_noun haf = u3qc_end(7, 1, sha);
2014-10-22 00:53:27 +04:00
u3z(jam); u3z(sha);
return haf;
}
2014-11-29 05:19:11 +03:00
/* u3j_find(): in warm state, return u3_none or calx. RETAINS.
2014-10-25 07:49:27 +04:00
*/
u3_weak
2014-11-29 05:19:11 +03:00
u3j_find(u3_noun bat)
2014-10-25 07:49:27 +04:00
{
2014-11-06 03:20:01 +03:00
u3a_road* rod_u = u3R;
2014-10-25 07:49:27 +04:00
while ( 1 ) {
2014-11-06 03:20:01 +03:00
u3_weak jaw = u3h_gut(rod_u->jed.har_p, bat);
2014-10-25 07:49:27 +04:00
2014-11-06 02:36:30 +03:00
if ( u3_none != jaw ) {
2014-10-31 00:40:05 +03:00
return jaw;
2014-10-25 07:49:27 +04:00
}
2014-10-31 00:40:05 +03:00
2014-10-25 07:49:27 +04:00
if ( rod_u->par_u ) {
rod_u = rod_u->par_u;
}
2014-11-06 02:36:30 +03:00
else return u3_none;
2014-10-25 07:49:27 +04:00
}
}
/* _cj_warm_hump(): generate axis-to-arm map. RETAIN.
*/
static u3_noun
_cj_warm_hump(c3_l jax_l, u3_noun huc)
{
2014-10-28 20:36:22 +03:00
u3_noun hap = u3_nul;
2014-11-08 01:54:59 +03:00
u3j_core* cop_u;
2014-10-25 07:49:27 +04:00
/* Compute axes of all correctly declared arms.
*/
2014-10-28 20:36:22 +03:00
if ( jax_l && (cop_u = &u3D.ray_u[jax_l])->arm_u ) {
2014-11-08 01:54:59 +03:00
u3j_harm* jet_u;
2014-10-25 07:49:27 +04:00
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)) ||
2014-10-25 07:49:27 +04:00
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 {
2014-11-06 03:20:01 +03:00
u3_noun nam = u3i_string(jet_u->fcs_c);
2014-11-06 22:13:57 +03:00
u3_noun fol = u3kdb_get(u3k(huc), nam);
2014-10-25 07:49:27 +04:00
2014-11-06 02:36:30 +03:00
if ( u3_none == fol ) {
2014-10-25 07:49:27 +04:00
fprintf(stderr, "jets: activate: bad fcs %s\r\n", jet_u->fcs_c);
}
2014-10-28 20:36:22 +03:00
else {
axe_l = _cj_axis(fol);
u3z(fol);
}
2014-10-25 07:49:27 +04:00
}
if ( 0 != axe_l ) {
2014-11-06 22:13:57 +03:00
hap = u3kdb_put(hap, axe_l, i_l);
2014-10-25 07:49:27 +04:00
}
}
}
return hap;
}
/* _cj_hot_mean(): in parent, declare a core. RETAINS.
2014-10-31 00:40:05 +03:00
**
** XX bat is used only for printing, remove.
2014-10-24 04:35:26 +04:00
*/
static c3_l
_cj_hot_mean(c3_l par_l, u3_noun mop, u3_noun bat)
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 ) {
2014-11-06 03:20:01 +03:00
if ( _(u3r_sing_c(cop_u->cos_c, u3h(mop))) ) {
2014-10-31 00:40:05 +03:00
#if 0
fprintf(stderr, "hot: bound jet %d/%s/%s/%x\r\n",
2014-10-28 20:36:22 +03:00
cop_u->jax_l,
cop_u->cos_c,
2014-10-31 00:40:05 +03:00
par_u ? par_u->cos_c : "~",
2014-11-06 03:20:01 +03:00
u3r_mug(bat));
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-28 23:29:10 +03:00
/* _cj_hot_mine(): in hoting state, declare a core. RETAINS.
2014-10-28 20:36:22 +03:00
*/
static c3_l
_cj_hot_mine(u3_noun mop, u3_noun cor)
2014-10-28 20:36:22 +03:00
{
u3_noun p_mop, q_mop, r_mop, hr_mop, tr_mop;
2014-11-06 03:20:01 +03:00
u3x_trel(mop, &p_mop, &q_mop, &r_mop);
u3x_cell(r_mop, &hr_mop, &tr_mop);
2014-10-28 20:36:22 +03:00
{
c3_l par_l;
// Calculate parent axis.
//
2014-11-05 04:18:47 +03:00
if ( c3y == hr_mop ) {
2014-11-29 05:19:11 +03:00
u3_noun cax = u3j_find(u3h(u3r_at(q_mop, cor)));
2014-10-22 00:53:27 +04:00
2014-11-18 01:57:05 +03:00
par_l = u3h(u3h(cax));
2014-10-28 20:36:22 +03:00
u3z(cax);
}
else par_l = 0;
return _cj_hot_mean(par_l, mop, u3h(cor));
2014-10-28 20:36:22 +03:00
}
}
2014-11-06 03:20:01 +03:00
/* u3j_boot(): initialize jet system.
2014-09-02 04:10:04 +04:00
*/
void
2014-11-06 03:20:01 +03:00
u3j_boot(void)
2014-09-02 04:10:04 +04:00
{
2014-09-07 02:39:28 +04:00
c3_w jax_l;
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
2014-09-07 02:39:28 +04:00
jax_l = _cj_install(u3D.ray_u, 1, 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
}
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 {
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);
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;
2014-09-18 23:07:36 +04:00
pro = ham_u->fun_f(u3k(cor));
2014-11-05 04:18:47 +03:00
ham_u->ice = c3n;
2014-09-02 04:10:04 +04:00
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));
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 {
2014-09-25 01:29:19 +04: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
}
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)
{
u3_noun bat = u3h(cor);
2014-11-06 03:20:01 +03:00
if ( !_(u3du(cor)) ) { return u3m_bail(c3__fail); }
2014-10-25 23:30:36 +04:00
{
2014-11-29 05:19:11 +03:00
u3_weak cax = u3j_find(bat);
2014-10-25 23:30:36 +04:00
2014-11-06 03:20:01 +03:00
if ( u3_none == cax ) { return u3m_bail(c3__fail); }
2014-10-25 23:30:36 +04:00
{
2014-11-18 01:57:05 +03:00
u3_noun p_cax, q_cax, r_cax;
2014-11-19 02:17:56 +03:00
u3_noun jax, soh, mop, huc, hap, jit;
2014-11-18 13:49:03 +03:00
u3_noun p_mop, q_mop, r_mop;
2014-10-25 23:30:36 +04:00
2014-11-18 01:57:05 +03:00
u3x_trel(cax, &p_cax, &q_cax, &r_cax);
2014-11-19 02:17:56 +03:00
u3x_trel(p_cax, &jax, &hap, &jit);
2014-11-18 23:59:59 +03:00
u3x_cell(q_cax, &soh, &mop);
2014-11-18 13:49:03 +03:00
u3x_trel(mop, &p_mop, &q_mop, &r_mop);
2014-11-18 02:57:08 +03:00
huc = u3t(r_cax);
2014-10-25 23:30:36 +04:00
{
2014-11-18 02:57:08 +03:00
c3_l jax_l = jax;
2014-11-08 01:54:59 +03:00
u3j_core* cop_u = &u3D.ray_u[jax_l];
2014-11-18 02:57:08 +03:00
u3_noun fol = u3kdb_get(u3k(huc), u3i_string(tam_c));
2014-10-25 23:30:36 +04:00
2014-11-06 02:36:30 +03:00
if ( u3_none == fol ) {
2014-10-25 23:30:36 +04:00
// The caller wants a deeper core.
//
2014-11-18 13:49:03 +03:00
if ( 0 == q_mop ) { return u3m_bail(c3__fail); }
2014-10-25 23:30:36 +04:00
else {
2014-11-18 13:49:03 +03:00
u3_noun inn = u3k(u3x_at(q_mop, cor));
2014-10-25 23:41:37 +04:00
u3z(cax); u3z(cor);
return _cj_hook_in(inn, tam_c, jet_o);
2014-10-25 23:30:36 +04:00
}
}
else {
u3_noun pro;
c3_l axe_l = _cj_axis(fol);
c3_l inx_l;
if ( (0 == axe_l) ||
2014-11-05 04:18:47 +03:00
(c3n == jet_o) ||
2014-11-06 22:13:57 +03:00
(u3_none == (inx_l = u3kdb_get(u3k(hap), axe_l))) ||
2014-11-06 02:36:30 +03:00
(u3_none == (pro = _cj_kick_z(cor,
2014-10-25 23:30:36 +04:00
cop_u,
&cop_u->arm_u[inx_l],
axe_l))) )
{
if ( 0 == axe_l ) {
2014-11-29 05:19:11 +03:00
u3z(cax);
2014-11-06 03:20:01 +03:00
return u3n_nock_on(cor, fol);
2014-10-25 23:30:36 +04:00
} else {
// Tricky: the above case would work here too, but would
// disable jet_o and create some infinite recursions.
//
2014-10-25 23:41:37 +04:00
u3z(cax); u3z(fol);
2014-11-06 03:20:01 +03:00
return u3n_nock_on(cor, u3k(u3x_at(axe_l, cor)));
2014-10-25 23:30:36 +04:00
}
}
else {
2014-10-25 23:41:37 +04:00
u3z(cax); u3z(fol);
2014-10-25 23:30:36 +04:00
return pro;
}
}
}
}
}
}
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,
2014-09-17 23:56:55 +04:00
const c3_c* tam_c)
{
2014-10-28 20:36:22 +03:00
u3_noun pro;
2014-11-05 04:18:47 +03:00
pro = _cj_hook_in(cor, tam_c, c3n);
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,
2014-09-17 23:56:55 +04:00
const c3_c* tam_c)
{
2014-10-28 20:36:22 +03:00
u3_noun pro;
2014-11-05 04:18:47 +03:00
pro = _cj_hook_in(cor, tam_c, c3y);
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
/* _cj_fine(): validate core. RETAIN.
*/
static c3_o
_cj_fine(u3_noun cup, u3_noun mop, u3_noun cor)
{
if ( c3y == u3h(cup) ) {
return u3r_sing(cor, u3t(cup));
}
else {
u3_noun par = u3t(u3t(mop));
u3_noun pax = u3h(u3t(mop));
if ( c3n == u3h(par) ) {
c3_assert(3 == pax);
2014-12-02 01:05:09 +03:00
if ( c3n == u3r_sing(u3t(par), u3t(cor)) ) {
fprintf(stderr, "fine: core mismatch: %x, %x\r\n",
u3t(par), u3t(cor));
}
return u3r_sing(u3t(par), u3t(cor));
}
else {
u3_weak pac = u3r_at(pax, cor);
if ( u3_none == pac || !_(u3du(pac)) ) {
2014-12-02 01:05:09 +03:00
fprintf(stderr, "fail 1\r\n");
return c3n;
}
else {
2014-11-29 05:19:11 +03:00
u3_weak cax = u3j_find(u3h(pac));
if ( u3_none == cax ) {
2014-12-01 10:32:19 +03:00
fprintf(stderr, "fine: parent not found\r\n");
return c3n;
}
else {
c3_o pro_o;
cup = u3h(u3t(u3t(cax)));
mop = u3t(u3h(u3t(cax)));
cor = pac;
pro_o = _cj_fine(cup, mop, cor);
u3z(cax);
return pro_o;
}
}
}
}
}
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-11-06 02:36:30 +03:00
if ( !_(u3du(cor)) ) { return u3_none; }
2014-12-03 00:53:35 +03:00
u3t_on(glu_o);
2014-10-25 07:49:27 +04:00
{
u3_noun bat = u3h(cor);
2014-11-29 05:19:11 +03:00
u3_weak cax = u3j_find(bat);
2014-10-25 07:49:27 +04:00
2014-12-03 00:53:35 +03:00
if ( u3_none == cax ) { u3t_off(glu_o); return u3_none; }
2014-10-25 07:49:27 +04:00
{
u3_noun mop = u3t(u3h(u3t(cax)));
2014-11-18 21:56:28 +03:00
u3_noun hap = u3h(u3t(u3h(cax)));
u3_noun cup = u3h(u3t(u3t(cax)));
2014-11-06 22:13:57 +03:00
u3_noun inx = u3kdb_get(u3k(hap), u3k(axe));
2014-10-25 07:49:27 +04:00
2014-11-06 02:36:30 +03:00
if ( u3_none == inx ) {
2014-12-03 00:53:35 +03:00
u3z(cax); u3t_off(glu_o); return u3_none;
2014-10-25 07:49:27 +04:00
}
#if 1
else if ( !_(_cj_fine(cup, mop, cor)) ) {
fprintf(stderr, "improper core %x\r\n", u3r_mug(cor));
2014-12-03 00:53:35 +03:00
u3m_p("improper label", u3h(u3t(u3t(u3h(cax)))));
u3t_off(glu_o);
return u3_none;
}
#endif
2014-10-25 07:49:27 +04:00
else {
2014-11-29 05:19:11 +03:00
c3_l jax_l = u3h(u3h(cax));
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-10-25 23:41:37 +04:00
u3z(cax);
2014-11-29 05:19:11 +03:00
if ( _(pof_o) ) {
pof_o = u3t_come(bat);
}
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) ) {
pro = u3n_nock_on(cor, u3nq(9, axe, 0, 1));
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
}
}
}
}
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
2014-11-06 03:20:01 +03:00
u3j_kink(u3_noun cor,
2014-09-06 00:13:24 +04:00
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;
} else {
2014-11-06 03:20:01 +03:00
return u3n_nock_on(cor, u3nq(9, axe, 0, 1));
2014-09-02 04:10:04 +04:00
}
}
2014-11-20 22:49:27 +03:00
/* _cj_jit(): generate arbitrary warm jet-associated data. RETAIN.
2014-11-18 23:59:59 +03:00
*/
2014-11-19 02:17:56 +03:00
static u3_noun
2014-11-28 23:29:10 +03:00
_cj_jit(c3_l jax_l, u3_noun bat)
2014-11-19 02:17:56 +03:00
{
return u3_nul;
}
2014-11-20 22:49:27 +03:00
/* _cj_mine(): declare a core. RETAIN.
2014-11-19 02:17:56 +03:00
*/
static void
2014-11-20 22:49:27 +03:00
_cj_mine(u3_noun cey, u3_noun cor)
2014-11-18 23:59:59 +03:00
{
u3_noun bat = u3h(cor);
u3_noun p_cey, q_cey, r_cey;
u3r_trel(cey, &p_cey, &q_cey, &r_cey);
{
/* Calculate semantic identity (cope) and match state (corp).
*/
2014-11-19 02:17:56 +03:00
u3_noun mop; // ++cope
u3_noun cup; // ++corp
u3_noun soh; // ++bash
u3_noun cuz; // ++club
2014-11-28 23:29:10 +03:00
u3_noun lab; // ++path
2014-11-20 22:49:27 +03:00
2014-11-18 23:59:59 +03:00
if ( 0 == q_cey ) {
2014-11-19 02:17:56 +03:00
mop = u3nq(u3k(p_cey), 3, c3n, u3k(u3t(cor)));
2014-11-18 23:59:59 +03:00
cup = u3nc(c3n, u3k(cor));
2014-11-28 23:29:10 +03:00
lab = u3_nul;
2014-11-18 23:59:59 +03:00
}
else {
u3_weak rah = u3r_at(q_cey, cor);
if ( (u3_none == rah) || !_(u3du(rah)) ) {
fprintf(stderr, "fund: %s is bogus\r\n", u3r_string(p_cey));
2014-11-19 02:17:56 +03:00
return;
2014-11-18 23:59:59 +03:00
}
else {
2014-11-19 02:17:56 +03:00
u3_noun tab = u3h(rah);
2014-11-29 05:19:11 +03:00
u3_weak cax = u3j_find(tab);
2014-11-18 23:59:59 +03:00
2014-11-19 02:17:56 +03:00
if ( u3_none == cax ) {
2014-11-18 23:59:59 +03:00
fprintf(stderr, "fund: in %s, parent %x not found at %d\r\n",
u3r_string(p_cey),
u3r_mug(u3h(rah)),
q_cey);
2014-11-19 02:17:56 +03:00
return;
2014-11-18 23:59:59 +03:00
}
else {
2014-11-19 02:17:56 +03:00
u3_noun hos = u3h(u3h(u3t(cax)));
u3_noun puc = u3h(u3t(u3t(cax)));
mop = u3nq(u3k(p_cey), u3k(q_cey), c3y, u3k(hos));
if ( (3 == q_cey) && (c3y == u3h(puc)) ) {
cup = u3nc(c3y, u3k(cor));
}
else {
cup = u3nc(c3n, u3k(tab));
}
2014-11-30 13:06:41 +03:00
lab = u3k(u3h(u3t(u3t(u3h(cax)))));
2014-11-20 22:49:27 +03:00
u3z(cax);
2014-11-18 23:59:59 +03:00
}
}
}
2014-11-19 02:17:56 +03:00
soh = _cj_sham(u3k(mop));
cuz = u3nc(cup, u3k(r_cey));
// Save cold state.
//
{
2014-11-20 22:58:55 +03:00
u3_noun hoe = u3kdb_get(u3k(u3R->jed.das), u3k(soh));
2014-11-19 02:17:56 +03:00
u3_noun sab;
if ( u3_none == hoe ) {
2014-11-20 22:49:27 +03:00
sab = u3nt(u3nc(u3k(bat), u3k(cuz)), u3_nul, u3_nul);
2014-11-19 02:17:56 +03:00
}
else {
sab = u3kdb_put(u3k(u3t(hoe)), u3k(bat), u3k(cuz));
u3z(hoe);
}
2014-11-20 22:58:55 +03:00
u3R->jed.das = u3kdb_put(u3R->jed.das, u3k(soh), u3nc(u3k(mop), sab));
2014-11-19 02:17:56 +03:00
}
2014-11-18 23:59:59 +03:00
2014-11-20 22:49:27 +03:00
// Save warm state.
2014-11-19 02:17:56 +03:00
//
{
2014-12-01 10:32:19 +03:00
c3_l jax_l = _cj_hot_mine(mop, cor);
u3_noun bal = u3nc(u3k(p_cey), lab);
2014-11-19 02:17:56 +03:00
2014-12-02 01:05:09 +03:00
#if 0
2014-12-01 10:32:19 +03:00
u3m_p("new jet", bal);
2014-12-02 01:05:09 +03:00
fprintf(stderr, "label %x, batt %x\r\n", u3r_mug(bal), u3r_mug(bat));
#endif
2014-11-19 02:17:56 +03:00
u3h_put(u3R->jed.har_p,
bat,
2014-11-28 23:29:10 +03:00
u3nt(u3nq(jax_l,
2014-11-20 22:49:27 +03:00
_cj_warm_hump(jax_l, r_cey),
2014-12-02 01:05:09 +03:00
bal,
2014-11-28 23:29:10 +03:00
_cj_jit(jax_l, bat)),
2014-11-19 02:17:56 +03:00
u3nc(soh, mop),
cuz));
2014-11-20 22:49:27 +03:00
}
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
{
2014-11-18 23:59:59 +03:00
u3_noun bat = u3h(cor);
2014-11-20 22:49:27 +03:00
u3_noun cax;
2014-11-18 23:59:59 +03:00
2014-12-03 00:53:35 +03:00
u3t_on(glu_o);
2014-11-18 23:59:59 +03:00
if ( !_(u3du(cor)) ) {
u3z(clu);
}
2014-11-29 05:19:11 +03:00
else if ( u3_none != (cax = u3j_find(bat)) ) {
2014-11-18 23:59:59 +03:00
u3z(cax); u3z(clu);
}
2014-11-19 02:17:56 +03:00
else {
2014-11-20 22:49:27 +03:00
u3_noun cey = _cj_je_fsck(clu);
if ( u3_none != cey ) {
_cj_mine(cey, cor);
u3z(cey);
}
2014-11-18 23:59:59 +03:00
}
2014-11-20 22:49:27 +03:00
u3z(cor);
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
/* _cj_cold_reap_to: reap clog map. RETAINS `sab`, TRANSFERS `bas`.
*/
static u3_noun
_cj_cold_reap_to(u3_noun sab, u3_noun bas)
{
if ( u3_nul == sab ) {
return bas;
}
else {
u3_noun n_sab, l_sab, r_sab, pn_sab, qn_sab;
2014-11-06 03:20:01 +03:00
u3x_trel(sab, &n_sab, &l_sab, &r_sab);
u3x_cell(n_sab, &pn_sab, &qn_sab);
2014-10-31 00:40:05 +03:00
{
bas = _cj_cold_reap_to(l_sab, bas);
bas = _cj_cold_reap_to(r_sab, bas);
// If the battery is not junior, or if it has been
// already collected for the product, promote it.
//
2014-11-06 03:20:01 +03:00
if ( _(u3a_left(pn_sab)) ) {
u3_noun bat = u3a_take(pn_sab);
2014-10-31 00:40:05 +03:00
2014-11-06 22:13:57 +03:00
bas = u3kdb_put(bas, bat, u3a_take(qn_sab));
2014-10-31 00:40:05 +03:00
}
return bas;
}
}
}
/* _cj_cold_reap_at(): reap haw node. RETAINS.
*/
static void
_cj_cold_reap_at(u3_noun soh, u3_noun cag)
{
u3_noun sab = _cj_cold_reap_to(u3t(cag), u3_nul);
if ( u3_nul != sab ) {
2014-11-06 03:20:01 +03:00
soh = u3a_take(soh);
cag = u3nc(u3a_take(u3h(cag)), sab);
2014-10-31 00:40:05 +03:00
2014-11-20 22:58:55 +03:00
u3R->jed.das = u3kdb_put(u3R->jed.das, soh, cag);
2014-10-31 00:40:05 +03:00
}
}
/* _cj_cold_reap_in(): reap in (junior) haw. RETAINS.
*/
static void
_cj_cold_reap_in(u3_noun taw)
{
if ( u3_nul != taw ) {
u3_noun n_taw, l_taw, r_taw, pn_taw, qn_taw;
2014-11-06 03:20:01 +03:00
u3x_trel(taw, &n_taw, &l_taw, &r_taw);
u3x_cell(n_taw, &pn_taw, &qn_taw);
2014-10-31 00:40:05 +03:00
_cj_cold_reap_at(pn_taw, qn_taw);
_cj_cold_reap_in(l_taw);
_cj_cold_reap_in(r_taw);
}
}
/* _cj_warm_reap(): reap key and value from warm table.
*/
static void
_cj_warm_reap(u3_noun kev)
{
u3_noun bat = u3h(kev);
u3_noun cax = u3t(kev);
2014-11-06 03:20:01 +03:00
if ( _(u3a_left(bat)) ) {
2014-12-02 01:05:09 +03:00
if ( !_(u3a_is_junior(u3R, bat)) &&
(u3_none != u3h_git(u3R->jed.har_p, bat)) ) {
// fprintf(stderr, "reap: promote collision (bat %x)\r\n", u3r_mug(bat));
}
else {
u3_noun tab = u3a_take(bat);
u3_noun xac = u3a_take(cax);
2014-10-31 00:40:05 +03:00
#if 0
2014-12-02 01:05:09 +03:00
fprintf(stderr, "reap to %p: bat %x (%d, %d), cax %x\r\n",
u3R,
u3r_mug(tab),
u3a_is_junior(u3R, bat),
u3a_use(tab),
u3r_mug(xac));
2014-10-31 00:40:05 +03:00
#endif
2014-12-02 01:05:09 +03:00
u3h_put(u3R->jed.har_p, tab, xac);
u3z(tab);
}
2014-10-31 00:40:05 +03:00
}
}
2014-11-06 03:20:01 +03:00
/* u3j_reap(): promote jet state. RETAINS.
2014-10-31 00:40:05 +03:00
*/
void
2014-11-06 03:20:01 +03:00
u3j_reap(u3_noun das, u3p(u3h_root) har_p)
2014-10-31 00:40:05 +03:00
{
2014-11-20 22:58:55 +03:00
_cj_cold_reap_in(das);
2014-11-06 03:20:01 +03:00
u3h_walk(har_p, _cj_warm_reap);
2014-11-18 02:24:40 +03:00
}
2014-11-28 23:29:10 +03:00
static c3_l _cj_warm_ream_at(u3_noun soh, u3_noun* lab, u3_noun cag);
/* _cj_warm_ream_be(): install battery; RETAINS.
*/
static void
_cj_warm_ream_be(c3_l jax_l,
u3_noun soh,
u3_noun lab,
u3_noun mop,
u3_noun bat,
u3_noun cuz)
{
u3h_put(u3R->jed.har_p,
bat,
u3nt(u3nq(jax_l,
_cj_warm_hump(jax_l, u3t(cuz)),
u3k(lab),
_cj_jit(jax_l, bat)),
u3nc(u3k(soh), u3k(mop)),
u3k(cuz)));
}
/* _cj_warm_ream_is(): reream battery; RETAINS.
*/
static void
_cj_warm_ream_is(c3_l jax_l,
u3_noun soh,
u3_noun lab,
u3_noun mop,
u3_noun sab)
{
if ( u3_nul != sab ) {
u3_noun n_sab, l_sab, r_sab, pn_sab, qn_sab;
u3x_trel(sab, &n_sab, &l_sab, &r_sab);
u3x_cell(n_sab, &pn_sab, &qn_sab);
_cj_warm_ream_be(jax_l, soh, lab, mop, pn_sab, qn_sab);
_cj_warm_ream_is(jax_l, soh, lab, mop, l_sab);
_cj_warm_ream_is(jax_l, soh, lab, mop, r_sab);
}
}
/* _cj_warm_ream_un(): reream under `soh`; RETAINS, transfers `*lab`.
*/
static c3_l
_cj_warm_ream_un(u3_noun soh, u3_noun* lab)
{
u3_noun cag = u3kdb_got(u3k(u3R->jed.das), u3k(soh));
u3_noun sab = u3t(cag);
u3_noun cax;
c3_l jax_l;
if ( u3_none != (cax = u3h_get(u3R->jed.har_p, u3h(u3h(sab)))) ) {
jax_l = u3h(u3h(cax));
*lab = u3k(u3h(u3t(u3t(u3h(cax)))));
u3z(cax);
}
else {
jax_l = _cj_warm_ream_at(soh, lab, cag);
}
u3z(cag);
return jax_l;
}
/* _cj_warm_ream_at(): reream at `soh` and `cag`; RETAINS, transfers `*lab`.
*/
static c3_l
_cj_warm_ream_at(u3_noun soh, u3_noun* lab, u3_noun cag)
{
u3_noun mop = u3h(cag);
u3_noun sab = u3t(cag);
u3_noun p_mop, q_mop, r_mop, hr_mop, tr_mop;
u3x_trel(mop, &p_mop, &q_mop, &r_mop);
u3x_cell(r_mop, &hr_mop, &tr_mop);
{
c3_l par_l, jax_l;
u3_noun pal = u3_nul;
if ( c3y == hr_mop ) {
par_l = _cj_warm_ream_un(tr_mop, &pal);
}
else {
par_l = 0;
pal = u3_nul;
}
*lab = u3nc(u3k(p_mop), pal);
jax_l = _cj_hot_mean(par_l, mop, 0);
_cj_warm_ream_is(jax_l, soh, *lab, mop, sab);
return jax_l;
}
}
/* _cj_warm_ream_in(): reream in `taw`; RETAINS.
*/
static void
_cj_warm_ream_in(u3_noun taw)
{
if ( u3_nul != taw ) {
u3_noun n_taw, l_taw, r_taw, pn_taw, qn_taw;
u3_noun lab;
u3x_trel(taw, &n_taw, &l_taw, &r_taw);
u3x_cell(n_taw, &pn_taw, &qn_taw);
_cj_warm_ream_at(pn_taw, &lab, qn_taw);
u3z(lab);
_cj_warm_ream_in(l_taw);
_cj_warm_ream_in(r_taw);
}
}
/* _cj_warm_ream(): reream warm from cold state.
*/
static void
_cj_warm_ream(void)
{
c3_assert(u3R == &(u3H->rod_u));
{
_cj_warm_ream_in(u3R->jed.das);
}
}
/* u3j_ream(): reream after restoring from checkpoint.
*/
void
u3j_ream(void)
{
u3h_free(u3R->jed.har_p);
u3R->jed.har_p = u3h_new();
_cj_warm_ream();
}