urbit/noun/nock.c

1965 lines
42 KiB
C
Raw Normal View History

2014-09-11 04:01:32 +04:00
/* g/n.c
2014-09-05 23:55:16 +04:00
**
*/
#include "all.h"
2014-12-03 00:53:35 +03:00
static u3_noun _n_nock_on(u3_noun bus, u3_noun fol);
static u3_noun _n_burn_on(u3_noun bus, u3_noun fol);
2014-10-28 20:36:22 +03:00
/* u3_term_io_hija(): hijack console for cooked print.
*/
FILE*
u3_term_io_hija(void);
/* u3_term_io_loja(): release console from cooked print.
*/
void
u3_term_io_loja(int x);
/* uL, uH: wrap hijack/lojack around fprintf.
**
** uL(fprintf(uH, ...));
*/
# define uH u3_term_io_hija()
# define uL(x) u3_term_io_loja(x)
2014-12-03 00:53:35 +03:00
/* _n_hint(): process hint.
2014-09-05 23:55:16 +04:00
*/
2014-09-06 00:13:24 +04:00
static u3_noun
2014-12-03 00:53:35 +03:00
_n_hint(u3_noun zep,
u3_noun hod,
u3_noun bus,
u3_noun nex)
2014-09-05 23:55:16 +04:00
{
switch ( zep ) {
default: {
2014-11-06 03:20:01 +03:00
// u3m_p("weird zep", zep);
u3a_lose(zep);
u3a_lose(hod);
2014-09-05 23:55:16 +04:00
2014-12-03 00:53:35 +03:00
return _n_nock_on(bus, nex);
2014-09-05 23:55:16 +04:00
}
case c3__hunk:
case c3__lose:
case c3__mean:
case c3__spot: {
2014-09-06 00:13:24 +04:00
u3_noun tac = u3nc(zep, hod);
u3_noun pro;
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3t_push(tac);
2014-09-05 23:55:16 +04:00
#if 0
{
static int low_i;
if ( !low_i ) {
low_i = 1;
if ( 0 == (u3R->pro.nox_d % 65536ULL) ) {
if ( c3__spot == zep ) {
uL(fprintf(uH, "spot %d/%d : %d/%d\r\n",
u3h(u3h(u3t(hod))),
u3t(u3h(u3t(hod))),
u3h(u3t(u3t(hod))),
u3t(u3t(u3t(hod)))));
}
}
low_i = 0;
}
2014-09-05 23:55:16 +04:00
}
#endif
2014-12-03 00:53:35 +03:00
pro = _n_nock_on(bus, nex);
2014-11-06 03:20:01 +03:00
u3t_drop();
2014-09-05 23:55:16 +04:00
return pro;
}
2016-02-23 21:57:34 +03:00
case c3__live: {
if ( c3y == u3ud(hod) ) {
2016-02-23 22:18:14 +03:00
u3t_off(noc_o);
2016-02-23 21:57:34 +03:00
u3t_heck(hod);
2016-02-23 22:18:14 +03:00
u3t_on(noc_o);
2016-02-23 21:57:34 +03:00
} else {
u3z(hod);
}
return _n_nock_on(bus, nex);
}
2014-09-05 23:55:16 +04:00
case c3__slog: {
2015-05-20 03:04:08 +03:00
if ( !(u3C.wag_w & u3o_quiet) ) {
u3t_off(noc_o);
u3t_slog(hod);
u3t_on(noc_o);
}
2014-12-03 00:53:35 +03:00
return _n_nock_on(bus, nex);
2014-09-05 23:55:16 +04:00
}
case c3__germ: {
2014-12-03 00:53:35 +03:00
u3_noun pro = _n_nock_on(bus, nex);
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
if ( c3y == u3r_sing(pro, hod) ) {
2014-09-06 00:13:24 +04:00
u3z(pro); return hod;
2014-09-05 23:55:16 +04:00
} else {
2014-09-06 00:13:24 +04:00
u3z(hod); return pro;
2014-09-05 23:55:16 +04:00
}
}
case c3__fast: {
2014-12-03 00:53:35 +03:00
u3_noun pro = _n_nock_on(bus, nex);
2014-09-05 23:55:16 +04:00
2014-12-03 00:53:35 +03:00
u3t_off(noc_o);
2014-11-06 03:20:01 +03:00
u3j_mine(hod, u3k(pro));
2014-12-03 00:53:35 +03:00
u3t_on(noc_o);
2014-10-25 01:04:44 +04:00
return pro;
2014-09-05 23:55:16 +04:00
}
case c3__memo: {
2014-09-06 00:13:24 +04:00
u3z(hod);
2014-09-17 04:29:12 +04:00
#if 0
2014-12-03 00:53:35 +03:00
return _n_nock_on(bus, nex);
2014-09-17 04:29:12 +04:00
#else
2014-09-05 23:55:16 +04:00
{
2017-11-09 01:19:48 +03:00
u3_noun pro = u3z_find_2(144 + c3__nock, bus, nex);
2014-09-05 23:55:16 +04:00
2014-11-06 02:36:30 +03:00
if ( pro != u3_none ) {
2014-10-11 09:32:58 +04:00
u3z(bus); u3z(nex);
2014-09-05 23:55:16 +04:00
return pro;
}
2014-12-03 00:53:35 +03:00
pro = _n_nock_on(u3k(bus), u3k(nex));
2014-09-05 23:55:16 +04:00
2015-07-02 01:28:55 +03:00
if ( &(u3H->rod_u) != u3R ) {
2017-11-09 01:19:48 +03:00
u3z_save_2(144 + c3__nock, bus, nex, pro);
2015-05-14 01:23:20 +03:00
}
2014-09-06 00:13:24 +04:00
u3z(bus); u3z(nex);
2014-10-10 05:27:02 +04:00
return pro;
2014-09-05 23:55:16 +04:00
}
2014-09-17 04:29:12 +04:00
#endif
2014-09-05 23:55:16 +04:00
}
case c3__sole: {
2014-09-06 00:13:24 +04:00
u3z(hod);
2014-09-05 23:55:16 +04:00
{
2014-12-03 00:53:35 +03:00
u3_noun pro = _n_nock_on(bus, nex);
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
// return u3z_uniq(pro);
2014-09-17 04:29:12 +04:00
return pro;
2014-09-05 23:55:16 +04:00
}
}
}
}
/* _n_mush_in(): see _n_mush().
*/
static u3_noun
_n_mush_in(u3_noun val)
{
if ( c3n == u3du(val) ) {
return u3_nul;
}
else {
u3_noun h_val = u3h(val);
u3_noun ite;
if ( c3n == u3ud(h_val) ) {
ite = u3nc(c3__leaf, u3_nul);
} else {
ite = u3nc(c3__leaf, u3qe_trip(h_val));
}
return u3nc(ite, _n_mush_in(u3t(val)));
}
}
/* _n_mush(): tank from failed path request.
*/
static u3_noun
_n_mush(u3_noun val)
{
u3_noun pro;
pro = u3nt(c3__rose,
u3nt(u3nc('/', u3_nul), u3nc('/', u3_nul), u3_nul),
_n_mush_in(val));
u3z(val);
return pro;
}
2014-12-03 00:53:35 +03:00
/* _n_nock_on(): produce .*(bus fol). Do not virtualize.
2014-09-05 23:55:16 +04:00
*/
static u3_noun
2014-12-03 00:53:35 +03:00
_n_nock_on(u3_noun bus, u3_noun fol)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun hib, gal;
2014-09-05 23:55:16 +04:00
while ( 1 ) {
2014-09-06 00:13:24 +04:00
hib = u3h(fol);
gal = u3t(fol);
2014-09-05 23:55:16 +04:00
#ifdef U3_CPU_DEBUG
2014-09-27 06:14:24 +04:00
u3R->pro.nox_d += 1;
#endif
2014-09-16 03:56:37 +04:00
2014-11-06 03:20:01 +03:00
if ( c3y == u3r_du(hib) ) {
2014-09-06 00:13:24 +04:00
u3_noun poz, riv;
2014-09-05 23:55:16 +04:00
2014-12-03 00:53:35 +03:00
poz = _n_nock_on(u3k(bus), u3k(hib));
riv = _n_nock_on(bus, u3k(gal));
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3a_lose(fol);
return u3i_cell(poz, riv);
2014-09-05 23:55:16 +04:00
}
else switch ( hib ) {
2014-11-06 03:20:01 +03:00
default: return u3m_bail(c3__exit);
2014-09-05 23:55:16 +04:00
case 0: {
2014-11-06 03:20:01 +03:00
if ( c3n == u3r_ud(gal) ) {
return u3m_bail(c3__exit);
2014-09-05 23:55:16 +04:00
}
else {
2014-09-06 00:13:24 +04:00
u3_noun pro = u3k(u3at(gal, bus));
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3a_lose(bus); u3a_lose(fol);
2014-09-05 23:55:16 +04:00
return pro;
}
}
c3_assert(!"not reached");
case 1: {
2014-09-06 00:13:24 +04:00
u3_noun pro = u3k(gal);
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3a_lose(bus); u3a_lose(fol);
2014-09-05 23:55:16 +04:00
return pro;
}
c3_assert(!"not reached");
case 2: {
2014-12-03 00:53:35 +03:00
u3_noun nex = _n_nock_on(u3k(bus), u3k(u3t(gal)));
u3_noun seb = _n_nock_on(bus, u3k(u3h(gal)));
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3a_lose(fol);
2014-09-05 23:55:16 +04:00
bus = seb;
fol = nex;
continue;
}
c3_assert(!"not reached");
case 3: {
2014-09-06 00:13:24 +04:00
u3_noun gof, pro;
2014-09-05 23:55:16 +04:00
2014-12-03 00:53:35 +03:00
gof = _n_nock_on(bus, u3k(gal));
2014-11-06 03:20:01 +03:00
pro = u3r_du(gof);
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3a_lose(gof); u3a_lose(fol);
2014-09-05 23:55:16 +04:00
return pro;
}
c3_assert(!"not reached");
case 4: {
2014-09-06 00:13:24 +04:00
u3_noun gof, pro;
2014-09-05 23:55:16 +04:00
2014-12-03 00:53:35 +03:00
gof = _n_nock_on(bus, u3k(gal));
2014-11-06 03:20:01 +03:00
pro = u3i_vint(gof);
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3a_lose(fol);
2014-09-05 23:55:16 +04:00
return pro;
}
c3_assert(!"not reached");
case 5: {
2014-12-03 00:53:35 +03:00
u3_noun wim = _n_nock_on(bus, u3k(gal));
2014-11-06 03:20:01 +03:00
u3_noun pro = u3r_sing(u3h(wim), u3t(wim));
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3a_lose(wim); u3a_lose(fol);
2014-09-05 23:55:16 +04:00
return pro;
}
c3_assert(!"not reached");
case 6: {
2014-09-06 00:13:24 +04:00
u3_noun b_gal, c_gal, d_gal;
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3x_trel(gal, &b_gal, &c_gal, &d_gal);
2014-09-05 23:55:16 +04:00
{
2014-12-03 00:53:35 +03:00
u3_noun tys = _n_nock_on(u3k(bus), u3k(b_gal));
2014-09-06 00:13:24 +04:00
u3_noun nex;
2014-09-05 23:55:16 +04:00
if ( 0 == tys ) {
2014-09-06 00:13:24 +04:00
nex = u3k(c_gal);
2014-09-05 23:55:16 +04:00
} else if ( 1 == tys ) {
2014-09-06 00:13:24 +04:00
nex = u3k(d_gal);
2014-11-06 03:20:01 +03:00
} else return u3m_bail(c3__exit);
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3a_lose(fol);
2014-09-05 23:55:16 +04:00
fol = nex;
continue;
}
}
c3_assert(!"not reached");
case 7: {
2014-09-06 00:13:24 +04:00
u3_noun b_gal, c_gal;
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3x_cell(gal, &b_gal, &c_gal);
2014-09-05 23:55:16 +04:00
{
2014-12-03 00:53:35 +03:00
u3_noun bod = _n_nock_on(bus, u3k(b_gal));
2014-09-06 00:13:24 +04:00
u3_noun nex = u3k(c_gal);
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3a_lose(fol);
2014-09-05 23:55:16 +04:00
bus = bod;
fol = nex;
continue;
}
}
c3_assert(!"not reached");
case 8: {
2014-09-06 00:13:24 +04:00
u3_noun b_gal, c_gal;
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3x_cell(gal, &b_gal, &c_gal);
2014-09-05 23:55:16 +04:00
{
2014-12-03 00:53:35 +03:00
u3_noun heb = _n_nock_on(u3k(bus), u3k(b_gal));
2014-09-06 00:13:24 +04:00
u3_noun bod = u3nc(heb, bus);
u3_noun nex = u3k(c_gal);
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3a_lose(fol);
2014-09-05 23:55:16 +04:00
bus = bod;
fol = nex;
continue;
}
}
c3_assert(!"not reached");
case 9: {
2014-09-06 00:13:24 +04:00
u3_noun b_gal, c_gal;
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3x_cell(gal, &b_gal, &c_gal);
2014-09-05 23:55:16 +04:00
{
2014-12-03 00:53:35 +03:00
u3_noun seb = _n_nock_on(bus, u3k(c_gal));
2014-12-03 03:26:30 +03:00
u3_noun pro;
u3t_off(noc_o);
pro = u3j_kick(seb, b_gal);
u3t_on(noc_o);
2014-09-05 23:55:16 +04:00
2014-11-06 02:36:30 +03:00
if ( u3_none != pro ) {
2014-11-06 03:20:01 +03:00
u3a_lose(fol);
2014-09-05 23:55:16 +04:00
return pro;
}
else {
2014-11-06 03:20:01 +03:00
if ( c3n == u3r_ud(b_gal) ) {
return u3m_bail(c3__exit);
2014-09-05 23:55:16 +04:00
}
else {
2014-09-06 00:13:24 +04:00
u3_noun nex = u3k(u3at(b_gal, seb));
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3a_lose(fol);
2014-09-05 23:55:16 +04:00
bus = seb;
fol = nex;
continue;
}
}
}
}
c3_assert(!"not reached");
case 10: {
2014-09-06 00:13:24 +04:00
u3_noun p_gal, q_gal;
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
u3x_cell(gal, &p_gal, &q_gal);
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun zep, hod, nex;
2014-09-05 23:55:16 +04:00
2014-11-06 03:20:01 +03:00
if ( c3y == u3r_du(p_gal) ) {
2014-09-06 00:13:24 +04:00
u3_noun b_gal = u3h(p_gal);
u3_noun c_gal = u3t(p_gal);
u3_noun d_gal = q_gal;
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
zep = u3k(b_gal);
2014-12-03 00:53:35 +03:00
hod = _n_nock_on(u3k(bus), u3k(c_gal));
2014-09-06 00:13:24 +04:00
nex = u3k(d_gal);
2014-09-05 23:55:16 +04:00
}
else {
2014-09-06 00:13:24 +04:00
u3_noun b_gal = p_gal;
u3_noun c_gal = q_gal;
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
zep = u3k(b_gal);
hod = u3_nul;
nex = u3k(c_gal);
2014-09-05 23:55:16 +04:00
}
2014-11-06 03:20:01 +03:00
u3a_lose(fol);
2014-12-03 00:53:35 +03:00
return _n_hint(zep, hod, bus, nex);
2014-09-05 23:55:16 +04:00
}
}
case 11: {
2016-01-26 22:03:05 +03:00
u3_noun ref = _n_nock_on(u3k(bus), u3k(u3h(gal)));
u3_noun gof = _n_nock_on(bus, u3k(u3t(gal)));
2014-12-03 03:26:30 +03:00
u3_noun val;
u3t_off(noc_o);
2016-02-02 22:52:10 +03:00
val = u3m_soft_esc(ref, u3k(gof));
2014-12-03 03:26:30 +03:00
u3t_on(noc_o);
2014-10-31 00:40:05 +03:00
2014-11-05 04:18:47 +03:00
if ( !_(u3du(val)) ) {
2014-11-06 03:20:01 +03:00
u3m_bail(u3nt(1, gof, 0));
2014-10-01 10:34:30 +04:00
}
2016-02-04 00:33:43 +03:00
if ( !_(u3du(u3t(val))) ) {
//
// replace with proper error stack push
//
u3t_push(u3nc(c3__hunk, _n_mush(gof)));
2016-02-04 00:33:43 +03:00
return u3m_bail(c3__exit);
2014-10-01 10:34:30 +04:00
}
else {
u3_noun pro;
u3z(gof);
u3z(fol);
2016-02-04 00:33:43 +03:00
pro = u3k(u3t(u3t(val)));
u3z(val);
return pro;
}
}
2014-09-05 23:55:16 +04:00
c3_assert(!"not reached");
}
}
}
2014-12-03 00:53:35 +03:00
/* u3n_nock_on(): produce .*(bus fol). Do not virtualize.
*/
u3_noun
u3n_nock_on(u3_noun bus, u3_noun fol)
{
u3_noun pro;
u3t_on(noc_o);
//pro = _n_nock_on(bus, fol);
pro = _n_burn_on(bus, fol);
2014-12-03 00:53:35 +03:00
u3t_off(noc_o);
return pro;
}
2014-11-06 03:20:01 +03:00
/* u3n_kick_on(): fire `gat` without changing the sample.
2014-09-05 23:55:16 +04:00
*/
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 03:20:01 +03:00
u3n_kick_on(u3_noun gat)
2014-09-05 23:55:16 +04:00
{
2014-11-06 03:20:01 +03:00
return u3j_kink(gat, 2);
2014-09-05 23:55:16 +04:00
}
2014-11-02 01:13:18 +03:00
c3_w exc_w;
2014-11-06 03:20:01 +03:00
/* u3n_slam_on(): produce (gat sam).
2014-09-05 23:55:16 +04:00
*/
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 03:20:01 +03:00
u3n_slam_on(u3_noun gat, u3_noun sam)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun cor = u3nc(u3k(u3h(gat)), u3nc(sam, u3k(u3t(u3t(gat)))));
2014-09-05 23:55:16 +04:00
2014-11-02 01:13:18 +03:00
#if 0
if ( &u3H->rod_u == u3R ) {
if ( exc_w == 1 ) {
c3_assert(0);
}
exc_w++;
}
#endif
2014-09-06 00:13:24 +04:00
u3z(gat);
2014-11-06 03:20:01 +03:00
return u3n_kick_on(cor);
2014-09-05 23:55:16 +04:00
}
2016-02-04 00:33:43 +03:00
/* u3n_nock_et(): produce .*(bus fol), as ++toon, in namespace.
*/
u3_noun
u3n_nock_et(u3_noun gul, u3_noun bus, u3_noun fol)
{
return u3m_soft_run(gul, u3n_nock_on, bus, fol);
2016-02-04 00:33:43 +03:00
}
/* u3n_slam_et(): produce (gat sam), as ++toon, in namespace.
*/
u3_noun
u3n_slam_et(u3_noun gul, u3_noun gat, u3_noun sam)
{
return u3m_soft_run(gul, u3n_slam_on, gat, sam);
2014-09-05 23:55:16 +04:00
}
2014-11-06 03:20:01 +03:00
/* u3n_nock_an(): as slam_in(), but with empty fly.
2014-09-05 23:55:16 +04:00
*/
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 03:20:01 +03:00
u3n_nock_an(u3_noun bus, u3_noun fol)
2014-09-05 23:55:16 +04:00
{
u3_noun gul = u3nt(u3nt(1, 0, 0), 0, 0); // |=(a/{* *} ~)
2014-10-01 10:34:30 +04:00
return u3n_nock_et(gul, bus, fol);
2014-09-05 23:55:16 +04:00
}
2018-01-31 03:27:04 +03:00
/* These must match the order in the section marked OPCODE TABLE */
2018-02-13 02:19:11 +03:00
#define HALT 0
#define BAIL 1
#define COPY 2
#define SWAP 3
#define TOSS 4
#define AUTO 5
#define AULT 6
#define HEAD 7
#define HELD 8
#define TAIL 9
#define TALL 10
#define FRAS 11
#define FRAG 12
#define FRAB 13
#define FLAS 14
#define FLAG 15
#define FLAB 16
2018-02-14 01:17:47 +03:00
#define LIT0 17
#define LIT1 18
#define LITB 19
#define LITS 20
#define LITN 21
#define LIL0 22
#define LIL1 23
#define LILB 24
#define LILS 25
#define LILN 26
#define NOLK 27
#define NOCT 28
#define NOCK 29
#define DEEP 30
#define BUMP 31
2018-02-14 20:18:53 +03:00
#define SAM0 32
#define SAM1 33
#define SAMB 34
#define SAMS 35
#define SAMN 36
#define SAME 37
#define SALM 38
#define SAMC 39
2018-02-18 23:55:00 +03:00
#define SKIP 40
#define SBIP 41
#define SKIN 42
#define SBIN 43
#define SNOC 44
#define SNOL 45
#define SLAM 46
#define KICB 47
#define KICS 48
#define KICK 49
#define SLAT 50
#define TICB 51
#define TICS 52
#define TICK 53
#define WILS 54
#define WISH 55
#define CUSH 56
#define DROP 57
#define HECK 58
#define SLOG 59
#define FALT 60
#define FAST 61
#define SKIB 62
#define SKIM 63
#define SLIB 64
#define SLIM 65
#define SAVE 66
2018-02-01 20:04:47 +03:00
#ifdef VERBYC
2018-02-20 22:28:01 +03:00
// match to OPCODE TABLE
static char* names[] = {
"halt", "bail",
"copy", "swap", "toss",
"auto", "ault",
"head", "held",
"tail", "tall",
"fras", "frag", "frab",
"flas", "flag", "flab",
"lit0", "lit1",
"litb", "lits", "litn",
"lil0", "lil1",
"lilb", "lils", "liln",
"nolk", "noct", "nock",
"deep", "bump",
"sam0", "sam1",
"samb", "sams", "samn",
"same", "salm", "samc",
"skip", "sbip",
"skin", "sbin",
"snoc", "snol",
"slam", "kicb", "kics", "kick",
"slat", "ticb", "tics", "tick",
"wils", "wish",
"cush", "drop",
"heck", "slog",
"falt", "fast",
"skib", "skim",
"slib", "slim",
"save"
};
#endif
2018-02-20 22:28:01 +03:00
2018-02-01 20:04:47 +03:00
/* _n_apen(): emit the instructions contained in src to dst
*/
static inline void
_n_apen(u3_noun* dst, u3_noun src)
{
*dst = u3qb_weld(src, *dst);
}
2018-02-20 22:28:01 +03:00
/* _n_emit(): return the size (in bytes) of an opcode's argument
2018-02-01 20:04:47 +03:00
*/
2018-01-31 03:27:04 +03:00
static inline c3_y
2018-02-20 22:28:01 +03:00
_n_arg(c3_y cod_y)
2018-01-31 03:27:04 +03:00
{
2018-02-20 22:28:01 +03:00
switch ( cod_y ) {
case FRAB: case FLAB: case LILB: case LITB: case SAMB:
case SBIP: case SBIN: case KICB: case TICB:
return sizeof(c3_y);
case FRAS: case FLAS: case LILS: case LITS: case SAMS:
case SKIP: case SKIN: case KICS: case TICS:
return sizeof(c3_s);
2018-02-13 02:19:11 +03:00
2018-02-20 22:28:01 +03:00
case CUSH: case FRAG: case FLAG: case LILN: case LITN:
case SAMN: case TICK: case KICK:
return sizeof(u3_noun);
2018-02-13 02:19:11 +03:00
2018-02-20 22:28:01 +03:00
case SKIM: case SLIM:
return sizeof(c3_s) + sizeof(u3_noun);
case SKIB: case SLIB:
return sizeof(c3_y) + sizeof(u3_noun);
2018-02-13 02:19:11 +03:00
2018-01-31 03:27:04 +03:00
default:
2018-02-20 22:28:01 +03:00
return 0;
2018-01-31 03:27:04 +03:00
}
}
2018-02-20 22:28:01 +03:00
/* _n_emit(): emit a single instruction to ops, returning
* the size (in bytes) required to store that
* opcode.
*/
static inline c3_y
_n_emit(u3_noun *ops, u3_noun op)
{
*ops = u3nc(op, *ops);
return sizeof(c3_y) + (c3n == u3du(op) ? 0 : _n_arg(u3h(op)));
}
static c3_s _n_comp(u3_noun*, u3_noun, c3_o, c3_o);
2018-01-31 03:27:04 +03:00
2018-02-01 20:04:47 +03:00
/* _n_bint(): hint-processing helper for _n_comp.
* hif: hint-formula (first part of 10). RETAIN.
* nef: next-formula (second part of 10). RETAIN.
*/
static c3_s
_n_bint(u3_noun* ops, u3_noun hif, u3_noun nef, c3_o los_o, c3_o tel_o)
2018-01-31 03:27:04 +03:00
{
if ( c3n == u3du(hif) ) {
// no currently recognized static hints
return _n_comp(ops, nef, los_o, tel_o);
2018-01-31 03:27:04 +03:00
}
else {
2018-02-07 21:26:06 +03:00
c3_s tot_s = 0;
u3_noun zep, hod;
2018-01-31 03:27:04 +03:00
u3x_cell(hif, &zep, &hod);
switch ( zep ) {
default:
tot_s += _n_comp(ops, hod, c3n, c3n);
2018-02-07 21:26:06 +03:00
tot_s += _n_emit(ops, TOSS);
tot_s += _n_comp(ops, nef, los_o, tel_o);
2018-01-31 03:27:04 +03:00
break;
case c3__hunk:
case c3__lose:
case c3__mean:
case c3__spot:
tot_s += _n_comp(ops, hod, c3n, c3n);
tot_s += _n_emit(ops, u3nc(CUSH, zep));
tot_s += _n_comp(ops, nef, los_o, c3n);
tot_s += _n_emit(ops, DROP);
2018-01-31 03:27:04 +03:00
break;
2018-02-13 02:19:11 +03:00
case c3__live:
tot_s += _n_comp(ops, hod, c3n, c3n);
tot_s += _n_emit(ops, HECK);
tot_s += _n_comp(ops, nef, los_o, tel_o);
2018-01-31 03:27:04 +03:00
break;
case c3__slog:
tot_s += _n_comp(ops, hod, c3n, c3n);
2018-01-31 03:27:04 +03:00
tot_s += _n_emit(ops, SLOG);
tot_s += _n_comp(ops, nef, los_o, tel_o);
2018-01-31 03:27:04 +03:00
break;
// germ and sole are unused...
2018-01-31 03:27:04 +03:00
case c3__fast:
tot_s += _n_comp(ops, hod, c3n, c3n);
tot_s += _n_emit(ops, SWAP);
tot_s += _n_comp(ops, nef, c3n, c3n);
tot_s += _n_emit(ops, (c3y == los_o) ? FALT : FAST);
2018-01-31 03:27:04 +03:00
break;
case c3__memo: {
u3_noun mem = u3_nul;
2018-02-13 02:19:11 +03:00
c3_s mem_s = 0;
c3_y op_y;
// we just throw away the hint (why is this not a static hint?)
tot_s += _n_comp(ops, hod, c3n, c3n);
tot_s += _n_emit(ops, TOSS);
// memoizing code always loses TOS because SAVE needs [pro key]
mem_s += _n_comp(&mem, nef, c3y, c3n);
mem_s += _n_emit(&mem, SAVE);
op_y = (c3y == los_o)
2018-02-13 02:19:11 +03:00
? (( mem_s <= 0xFF ) ? SLIB : SLIM)
: (( mem_s <= 0xFF ) ? SKIB : SKIM);
tot_s += _n_emit(ops, u3nt(op_y, mem_s, u3k(nef)));
tot_s += mem_s; _n_apen(ops, mem);
2018-01-31 03:27:04 +03:00
break;
}
}
return tot_s;
}
}
2018-02-07 21:26:06 +03:00
/* _n_comp(): compile nock formula to reversed opcode list
* ops is a pointer to a list (to be emitted to)
* fol is the nock formula to compile. RETAIN.
* los_o indicates whether we should remove our subject from the stack
2018-02-07 21:26:06 +03:00
* tel_o is yes if this formula is in tail position
* return: the number of bytes needed for this opcode list
2018-02-01 20:04:47 +03:00
*/
2018-01-31 03:27:04 +03:00
static c3_s
_n_comp(u3_noun* ops, u3_noun fol, c3_o los_o, c3_o tel_o)
{
2018-02-07 21:26:06 +03:00
c3_y op_y;
c3_s tot_s = 0;
2018-01-31 03:27:04 +03:00
u3_noun cod, arg, hed, tel;
u3x_cell(fol, &cod, &arg);
if ( c3y == u3du(cod) ) {
tot_s += _n_comp(ops, cod, c3n, c3n);
tot_s += _n_emit(ops, SWAP);
tot_s += _n_comp(ops, arg, c3n, c3n);
tot_s += _n_emit(ops, (c3y == los_o ) ? AULT : AUTO);
2018-01-31 03:27:04 +03:00
}
else switch ( cod ) {
case 0:
if ( c3n == u3ud(arg) ) {
u3m_bail(c3__exit);
return 0;
}
2018-01-31 03:27:04 +03:00
switch ( arg ) {
case 0:
tot_s += _n_emit(ops, BAIL);
break;
case 1:
if ( c3n == los_o ) {
tot_s += _n_emit(ops, COPY);
}
2018-01-31 03:27:04 +03:00
break;
case 2:
tot_s += _n_emit(ops, (c3y == los_o) ? HELD : HEAD);
2018-01-31 03:27:04 +03:00
break;
case 3:
tot_s += _n_emit(ops, (c3y == los_o) ? TALL : TAIL);
2018-01-31 03:27:04 +03:00
break;
2018-02-07 21:26:06 +03:00
default:
op_y = (c3y == los_o)
2018-02-13 02:19:11 +03:00
? (arg <= 0xFF ? FLAB : arg <= 0xFFFF ? FLAS : FLAG)
: (arg <= 0xFF ? FRAB : arg <= 0xFFFF ? FRAS : FRAG);
tot_s += _n_emit(ops, u3nc(op_y, arg));
2018-02-05 21:27:39 +03:00
break;
2018-01-31 03:27:04 +03:00
}
break;
case 1:
2018-02-14 01:17:47 +03:00
switch ( arg ) {
case 0:
tot_s += _n_emit(ops, (c3y == los_o) ? LIL0 : LIT0);
break;
case 1:
tot_s += _n_emit(ops, (c3y == los_o) ? LIL1 : LIT1);
break;
default:
op_y = (c3y == los_o)
? (arg <= 0xFF ? LILB : arg <= 0xFFFF ? LILS : LILN)
: (arg <= 0xFF ? LITB : arg <= 0xFFFF ? LITS : LITN);
tot_s += _n_emit(ops, u3nc(op_y, u3k(arg)));
break;
}
2018-01-31 03:27:04 +03:00
break;
2018-01-31 03:27:04 +03:00
case 2:
u3x_cell(arg, &hed, &tel);
tot_s += _n_comp(ops, hed, c3n, c3n);
tot_s += _n_emit(ops, SWAP);
tot_s += _n_comp(ops, tel, c3n, c3n);
/* things in tail position replace (so, lose) top of stack,
* so NOCT "loses" and there is no non-losing version */
op_y = (c3y == tel_o) ? NOCT
: ((c3y == los_o) ? NOLK : NOCK);
tot_s += _n_emit(ops, op_y);
2018-01-31 03:27:04 +03:00
break;
2018-01-31 03:27:04 +03:00
case 3:
tot_s += _n_comp(ops, arg, los_o, c3n);
2018-01-31 03:27:04 +03:00
tot_s += _n_emit(ops, DEEP);
break;
2018-01-31 03:27:04 +03:00
case 4:
tot_s += _n_comp(ops, arg, los_o, c3n);
2018-01-31 03:27:04 +03:00
tot_s += _n_emit(ops, BUMP);
break;
2018-02-14 20:18:53 +03:00
case 5: {
2018-01-31 03:27:04 +03:00
u3x_cell(arg, &hed, &tel);
2018-02-14 20:18:53 +03:00
if ( c3n == u3du(hed) ) {
tot_s += _n_comp(ops, arg, los_o, c3n);
tot_s += _n_emit(ops, SAMC);
2018-02-14 20:18:53 +03:00
}
else {
c3_t hec_t, tec_t;
2018-02-14 20:18:53 +03:00
hec_t = (1 == u3h(hed));
if ( c3n == u3du(tel) ) {
u3m_bail(c3__exit);
break;
2018-02-14 20:18:53 +03:00
}
else {
tec_t = (1 == u3h(tel));
2018-02-14 20:18:53 +03:00
}
if ( hec_t && tec_t ) {
if ( c3y == u3r_sing(u3t(hed), u3t(tel)) ) {
tot_s += _n_emit(ops, (c3y == los_o) ? LIL0 : LIT0);
}
else {
tot_s += _n_emit(ops, (c3y == los_o) ? LIL1 : LIT1);
}
}
else if ( !hec_t && !tec_t ) {
tot_s += _n_comp(ops, hed, c3n, c3n);
tot_s += _n_emit(ops, SWAP);
tot_s += _n_comp(ops, tel, c3n, c3n);
tot_s += _n_emit(ops, (c3y == los_o) ? SALM : SAME);
}
else {
tot_s += _n_comp(ops, (hec_t ? tel : hed), los_o, c3n);
u3_noun lit = u3t(hec_t ? hed : tel);
switch ( lit ) {
case 0:
tot_s += _n_emit(ops, SAM0);
break;
case 1:
tot_s += _n_emit(ops, SAM1);
break;
default:
op_y = lit <= 0xFF ? SAMB : lit <= 0xFFFF ? SAMS : SAMN;
tot_s += _n_emit(ops, u3nc(op_y, u3k(lit)));
}
2018-02-14 20:18:53 +03:00
}
}
2018-01-31 03:27:04 +03:00
break;
2018-02-14 20:18:53 +03:00
}
case 6: {
u3_noun mid,
yep = u3_nul,
nop = u3_nul;
2018-02-13 02:19:11 +03:00
c3_s yep_s, nop_s;
2018-01-31 03:27:04 +03:00
u3x_trel(arg, &hed, &mid, &tel);
tot_s += _n_comp(ops, hed, c3n, c3n);
yep_s = _n_comp(&yep, mid, los_o, tel_o);
nop_s = _n_comp(&nop, tel, los_o, tel_o);
2018-02-13 02:19:11 +03:00
op_y = (nop_s <= 0xFF ? SBIP : SKIP);
yep_s += _n_emit(&yep, u3nc(op_y, nop_s));
op_y = (yep_s <= 0xFF ? SBIN : SKIN);
tot_s += _n_emit(ops, u3nc(op_y, yep_s));
tot_s += yep_s; _n_apen(ops, yep);
tot_s += nop_s; _n_apen(ops, nop);
2018-01-31 03:27:04 +03:00
break;
}
case 7:
2018-01-31 03:27:04 +03:00
u3x_cell(arg, &hed, &tel);
tot_s += _n_comp(ops, hed, los_o, c3n);
tot_s += _n_comp(ops, tel, c3y, tel_o);
2018-01-31 03:27:04 +03:00
break;
case 8:
u3x_cell(arg, &hed, &tel);
tot_s += _n_comp(ops, hed, c3n, c3n);
tot_s += _n_emit(ops, (c3y == los_o) ? SNOL : SNOC);
tot_s += _n_comp(ops, tel, c3y, tel_o);
2018-01-31 03:27:04 +03:00
break;
2018-01-31 03:27:04 +03:00
case 9:
u3x_cell(arg, &hed, &tel);
if ( 3 == u3qc_cap(hed) ) {
u3_noun mac = u3nq(7, u3k(tel), 2, u3nt(u3nc(0, 1), 0, u3k(hed)));
2018-02-13 02:19:11 +03:00
tot_s += _n_comp(ops, mac, los_o, tel_o);
2018-01-31 03:27:04 +03:00
u3z(mac);
}
else {
tot_s += _n_comp(ops, tel, (c3y == tel_o ? c3y : los_o), c3n);
2018-02-14 01:32:32 +03:00
if ( 2 == hed ) {
tot_s += _n_emit(ops, (c3y == tel_o) ? SLAT : SLAM);
}
else {
op_y = (c3y == tel_o)
? (hed <= 0xFF ? TICB : hed <= 0xFFFF ? TICS : TICK)
: (hed <= 0xFF ? KICB : hed <= 0xFFFF ? KICS : KICK);
tot_s += _n_emit(ops, u3nc(op_y, u3k(hed)));
}
2018-01-31 03:27:04 +03:00
}
break;
2018-01-31 03:27:04 +03:00
case 10:
u3x_cell(arg, &hed, &tel);
tot_s += _n_bint(ops, hed, tel, los_o, tel_o);
2018-01-31 03:27:04 +03:00
break;
2018-01-31 03:27:04 +03:00
case 11:
u3x_cell(arg, &hed, &tel);
tot_s += _n_comp(ops, hed, c3n, c3n);
tot_s += _n_emit(ops, SWAP);
tot_s += _n_comp(ops, tel, c3n, c3n);
tot_s += _n_emit(ops, (c3y == los_o) ? WILS : WISH);
2018-01-31 03:27:04 +03:00
break;
default:
u3m_bail(c3__exit);
return 0;
2018-01-31 03:27:04 +03:00
}
return tot_s;
}
#ifdef VERBYC
static void _n_print_byc(c3_y* pog, c3_s her_s);
#endif
2018-02-01 20:04:47 +03:00
/* _n_asm(): assemble an accumulated list of instructions (i.e. from _n_comp)
*/
2018-01-31 03:27:04 +03:00
static c3_y*
_n_asm(u3_noun ops, c3_s len_s)
2018-01-31 03:27:04 +03:00
{
u3_noun top = ops;
c3_y* buf_y = u3a_malloc(sizeof(c3_y) * (len_s+1));
c3_s i_s = len_s;
2018-01-31 03:27:04 +03:00
buf_y[i_s] = HALT;
2018-02-01 20:21:10 +03:00
while ( i_s-- > 0 ) {
2018-01-31 03:27:04 +03:00
u3_noun op = u3h(ops);
if ( c3y == u3ud(op) ) {
buf_y[i_s] = (c3_y) u3h(ops);
}
else {
u3_noun cod = u3h(op);
switch ( cod ) {
case FRAB: case FLAB: case LILB: case LITB:
case SAMB: case SBIP: case SBIN:
case KICB: case TICB:
2018-02-05 22:20:34 +03:00
buf_y[i_s--] = (c3_y) u3t(op);
buf_y[i_s] = (c3_y) cod;
break;
case FRAS: case FLAS: case LILS: case LITS:
case SAMS: case SKIP: case SKIN:
case KICS: case TICS: {
2018-02-01 20:21:10 +03:00
c3_s off_s = u3t(op);
2018-01-31 03:27:04 +03:00
buf_y[i_s--] = (c3_y) (off_s >> 8);
buf_y[i_s--] = (c3_y) off_s;
buf_y[i_s] = (c3_y) cod;
break;
}
2018-02-13 02:19:11 +03:00
case CUSH: case FRAG: case FLAG:
case LILN: case LITN:
case SAMN: case TICK: case KICK: {
2018-02-01 20:21:10 +03:00
c3_w non_w = u3k(u3t(op));
2018-01-31 03:27:04 +03:00
buf_y[i_s--] = (c3_y) (non_w >> 24);
buf_y[i_s--] = (c3_y) (non_w >> 16);
buf_y[i_s--] = (c3_y) (non_w >> 8);
buf_y[i_s--] = (c3_y) non_w;
buf_y[i_s] = (c3_y) cod;
break;
}
2018-02-13 02:19:11 +03:00
case SKIB: case SLIB: {
2018-02-13 02:19:11 +03:00
c3_w non_w = u3k(u3t(u3t(op)));
buf_y[i_s--] = (c3_y) (non_w >> 24);
buf_y[i_s--] = (c3_y) (non_w >> 16);
buf_y[i_s--] = (c3_y) (non_w >> 8);
buf_y[i_s--] = (c3_y) non_w;
buf_y[i_s--] = (c3_y) u3h(u3t(op));
buf_y[i_s] = (c3_y) cod;
break;
}
case SKIM: case SLIM: {
2018-02-13 02:19:11 +03:00
c3_w non_w = u3k(u3t(u3t(op)));
c3_s sip_s = u3h(u3t(op));
buf_y[i_s--] = (c3_y) (non_w >> 24);
buf_y[i_s--] = (c3_y) (non_w >> 16);
buf_y[i_s--] = (c3_y) (non_w >> 8);
buf_y[i_s--] = (c3_y) non_w;
buf_y[i_s--] = (c3_y) (sip_s >> 8);
buf_y[i_s--] = (c3_y) sip_s;
buf_y[i_s] = (c3_y) cod;
break;
}
2018-01-31 03:27:04 +03:00
default:
u3m_bail(c3__exit);
return 0;
2018-01-31 03:27:04 +03:00
}
}
ops = u3t(ops);
}
// this will trigger if we ever have a nock formula that compiles to more
// than 2^16 opcodes. if needed, ip can be a c3_w.
c3_assert(u3_nul == ops);
2018-01-31 03:27:04 +03:00
2018-02-01 20:21:10 +03:00
u3z(top);
2018-01-31 03:27:04 +03:00
return buf_y;
}
2018-02-01 20:04:47 +03:00
/* _n_push(): push a noun onto the stack. RETAIN
* mov: -1 north, 1 south
* off: 0 north, -1 south
2018-02-01 20:04:47 +03:00
*/
2018-01-31 03:27:04 +03:00
static inline void
_n_push(c3_ys mov, c3_ys off, u3_noun a)
2018-01-31 03:27:04 +03:00
{
u3R->cap_p += mov;
u3_noun* p = u3to(u3_noun, u3R->cap_p + off);
//u3_noun* p = (u3_noun*) u3a_push(sizeof(u3_noun));
2018-01-31 03:27:04 +03:00
*p = a;
}
/* _n_peek(): pointer to noun at top of stack
* off: 0 north, -1 south
2018-02-01 20:04:47 +03:00
*/
2018-01-31 03:27:04 +03:00
static inline u3_noun*
_n_peek(c3_ys off)
2018-01-31 03:27:04 +03:00
{
return u3to(u3_noun, u3R->cap_p + off);
//return (u3_noun*) u3a_peek(sizeof(u3_noun));
2018-01-31 03:27:04 +03:00
}
2018-02-01 20:04:47 +03:00
/* _n_peet(): address of the next-to-top of stack
* mov: -1 north, 1 south
* off: 0 north, -1 south
2018-02-01 20:04:47 +03:00
*/
static inline u3_noun*
_n_peet(c3_ys mov, c3_ys off)
{
return u3to(u3_noun, (u3R->cap_p - mov) + off);
}
2018-02-01 20:04:47 +03:00
/* _n_pop(): pop a noun from the cap stack
* mov: -1 north, 1 south
*/
static inline void
_n_pop(c3_ys mov)
{
u3R->cap_p -= mov;
}
/* _n_pep(): pop and return noun from the cap stack
* mov: -1 north, 1 south
* off: 0 north, -1 south
2018-02-01 20:04:47 +03:00
*/
2018-01-31 03:27:04 +03:00
static inline u3_noun
_n_pep(c3_ys mov, c3_ys off)
2018-01-31 03:27:04 +03:00
{
u3_noun r = *(_n_peek(off));
_n_pop(mov);
2018-01-31 03:27:04 +03:00
return r;
}
/* _n_toss(): pep and lose
2018-02-01 20:04:47 +03:00
*/
2018-01-31 03:27:04 +03:00
static inline void
_n_toss(c3_ys mov, c3_ys off)
2018-01-31 03:27:04 +03:00
{
u3z(_n_pep(mov, off));
2018-01-31 03:27:04 +03:00
}
/* _n_resh(): read a c3_s from the bytecode stream
2018-02-01 20:04:47 +03:00
*/
2018-01-31 03:27:04 +03:00
static inline c3_s
_n_resh(c3_y* buf, c3_s* ip_s)
{
c3_y les = buf[(*ip_s)++];
c3_y mos = buf[(*ip_s)++];
return les | (mos << 8);
}
/* _n_rean(): read a noun from the bytecode stream.
* refcount is NOT incremented.
2018-02-01 20:04:47 +03:00
*/
2018-01-31 03:27:04 +03:00
static inline u3_noun
_n_rean(c3_y* buf, c3_s* ip_s)
{
c3_y one = buf[(*ip_s)++],
two = buf[(*ip_s)++],
tre = buf[(*ip_s)++],
qua = buf[(*ip_s)++];
return one | (two << 8) | (tre << 16) | (qua << 24);
}
2018-02-01 20:04:47 +03:00
/* _n_bite(): compile a nock formula to bytecode
*/
2018-01-31 03:27:04 +03:00
static inline c3_y*
_n_bite(u3_noun fol)
{
u3_noun bok = u3_nul;
c3_s len_s = _n_comp(&bok, fol, c3y, c3y);
c3_y* buf_y = _n_asm(bok, len_s);
return buf_y;
2018-01-31 03:27:04 +03:00
}
2018-02-01 20:04:47 +03:00
/* _n_find(): return bytecode for given formula. fol is RETAINED.
*/
static inline c3_y*
2018-01-31 03:27:04 +03:00
_n_find(u3_noun fol)
{
u3a_road* rod_u = u3R;
while ( 1 ) {
u3_weak jaw = u3h_gut(rod_u->byc.har_p, fol);
if ( u3_none != jaw ) {
return u3a_into(jaw);
}
if ( rod_u->par_p ) {
rod_u = u3to(u3_road, rod_u->par_p);
}
else break;
2018-01-31 03:27:04 +03:00
}
{
2018-01-31 03:27:04 +03:00
c3_y* gop = _n_bite(fol);
u3h_put(u3R->byc.har_p, fol, u3a_outa(gop));
2018-01-31 03:27:04 +03:00
return gop;
}
}
/* _n_swap(): swap two items on the top of the stack, return pointer to top
*/
static inline u3_noun*
_n_swap(c3_ys mov, c3_ys off)
{
u3_noun* top = _n_peek(off);
u3_noun* up = _n_peet(mov, off);
u3_noun tmp = *up;
*up = *top;
*top = tmp;
return top;
}
/* _n_kale(): bail(exit) if not cell
*/
static inline u3_noun
_n_kale(u3_noun a)
{
if ( c3n == u3du(a) ) {
u3m_bail(c3__exit);
}
return a;
}
2018-02-06 02:26:17 +03:00
typedef struct {
c3_y* pog;
c3_s ip_s;
} burnframe;
/* defining this will cause the bytecode interpreter to print out every opcode
* as it executes, along with some other information. very spammy.
* #define VERBYC
*/
2018-02-06 02:26:17 +03:00
/* _n_burn(): pog: program
* bus: subject
* mov: -1 north, 1 south
* off: 0 north, -1 south
*/
2018-02-06 02:26:17 +03:00
static u3_noun
_n_burn(c3_y* pog, u3_noun bus, c3_ys mov, c3_ys off)
2018-01-31 03:27:04 +03:00
{
/* OPCODE TABLE */
2018-01-31 03:27:04 +03:00
static void* lab[] = {
2018-02-13 02:19:11 +03:00
&&do_halt, &&do_bail,
&&do_copy, &&do_swap, &&do_toss,
&&do_auto, &&do_ault,
&&do_head, &&do_held,
&&do_tail, &&do_tall,
&&do_fras, &&do_frag, &&do_frab,
&&do_flas, &&do_flag, &&do_flab,
2018-02-14 01:17:47 +03:00
&&do_lit0, &&do_lit1,
2018-02-13 02:19:11 +03:00
&&do_litb, &&do_lits, &&do_litn,
2018-02-14 01:17:47 +03:00
&&do_lil0, &&do_lil1,
2018-02-13 02:19:11 +03:00
&&do_lilb, &&do_lils, &&do_liln,
&&do_nolk, &&do_noct, &&do_nock,
2018-02-13 02:19:11 +03:00
&&do_deep, &&do_bump,
2018-02-14 20:18:53 +03:00
&&do_sam0, &&do_sam1,
&&do_samb, &&do_sams, &&do_samn,
&&do_same, &&do_salm, &&do_samc,
2018-02-13 02:19:11 +03:00
&&do_skip, &&do_sbip,
&&do_skin, &&do_sbin,
&&do_snoc, &&do_snol,
2018-02-14 01:32:32 +03:00
&&do_slam, &&do_kicb, &&do_kics, &&do_kick,
&&do_slat, &&do_ticb, &&do_tics, &&do_tick,
2018-02-13 02:19:11 +03:00
&&do_wils, &&do_wish,
&&do_cush, &&do_drop,
2018-02-13 02:19:11 +03:00
&&do_heck, &&do_slog,
&&do_falt, &&do_fast,
&&do_skib, &&do_skim,
&&do_slib, &&do_slim,
&&do_save
2018-01-31 03:27:04 +03:00
};
c3_s sip_s, ip_s = 0;
u3_noun* top;
u3_noun x, o;
2018-02-06 02:26:17 +03:00
u3p(void) empty;
burnframe* fam;
2018-02-01 20:04:47 +03:00
2018-02-06 02:26:17 +03:00
empty = u3R->cap_p;
_n_push(mov, off, bus);
2018-01-31 03:27:04 +03:00
#ifdef VERBYC
#define BURN() fprintf(stderr, "%s ", names[pog[ip_s]]); goto *lab[pog[ip_s++]]
#else
#define BURN() goto *lab[pog[ip_s++]]
#endif
2018-01-31 03:27:04 +03:00
BURN();
while ( 1 ) {
do_halt: // [product ...burnframes...]
2018-02-06 02:26:17 +03:00
x = _n_pep(mov, off);
#ifdef VERBYC
fprintf(stderr, "return\r\n");
#endif
2018-02-06 02:26:17 +03:00
if ( empty == u3R->cap_p ) {
return x;
}
else {
fam = u3to(burnframe, u3R->cap_p) + off;
pog = fam->pog;
ip_s = fam->ip_s;
u3R->cap_p = u3of(burnframe, fam - (mov+off));
2018-02-06 02:26:17 +03:00
_n_push(mov, off, x);
#ifdef VERBYC
_n_print_byc(pog, ip_s);
#endif
2018-02-06 02:26:17 +03:00
BURN();
}
2018-01-31 03:27:04 +03:00
2018-02-13 02:19:11 +03:00
do_bail:
u3m_bail(c3__exit);
return u3_none;
2018-01-31 03:27:04 +03:00
do_copy:
top = _n_peek(off);
_n_push(mov, off, u3k(*top));
2018-01-31 03:27:04 +03:00
BURN();
do_swap:
_n_swap(mov, off);
2018-01-31 03:27:04 +03:00
BURN();
do_toss:
_n_toss(mov, off);
2018-01-31 03:27:04 +03:00
BURN();
do_auto: // [tel bus hed]
x = _n_pep(mov, off); // [bus hed]
top = _n_swap(mov, off); // [hed bus]
*top = u3nc(*top, x); // [pro bus]
BURN();
do_ault: // [tel bus hed]
x = _n_pep(mov, off); // [bus hed]
_n_toss(mov, off); // [hed]
top = _n_peek(off);
*top = u3nc(*top, x); // [pro]
2018-01-31 03:27:04 +03:00
BURN();
do_head:
top = _n_peek(off);
2018-02-13 02:19:11 +03:00
_n_push(mov, off, u3k(u3h(_n_kale(*top))));
2018-01-31 03:27:04 +03:00
BURN();
do_held:
top = _n_peek(off);
o = _n_kale(*top);
2018-01-31 03:27:04 +03:00
*top = u3k(u3h(o));
u3z(o);
BURN();
do_tail:
top = _n_peek(off);
2018-02-13 02:19:11 +03:00
_n_push(mov, off, u3k(u3t(_n_kale(*top))));
BURN();
do_tall:
top = _n_peek(off);
o = _n_kale(*top);
2018-01-31 03:27:04 +03:00
*top = u3k(u3t(o));
u3z(o);
BURN();
2018-02-05 22:20:34 +03:00
do_fras:
x = _n_resh(pog, &ip_s);
goto frag_in;
2018-01-31 03:27:04 +03:00
2018-02-05 22:20:34 +03:00
do_frag:
x = _n_rean(pog, &ip_s);
goto frag_in;
2018-02-05 21:27:39 +03:00
2018-02-05 22:20:34 +03:00
do_frab:
x = pog[ip_s++];
2018-02-05 22:20:34 +03:00
frag_in:
top = _n_peek(off);
2018-02-13 02:19:11 +03:00
_n_push(mov, off, u3k(u3x_at(x, *top)));
BURN();
do_flas:
x = _n_resh(pog, &ip_s);
goto flag_in;
do_flag:
x = _n_rean(pog, &ip_s);
goto flag_in;
do_flab:
x = pog[ip_s++];
flag_in:
top = _n_peek(off);
2018-02-05 22:20:34 +03:00
o = *top;
*top = u3k(u3x_at(x, o));
2018-02-05 22:20:34 +03:00
u3z(o);
2018-02-05 21:27:39 +03:00
BURN();
do_lit0:
_n_push(mov, off, 0);
BURN();
2018-02-14 01:17:47 +03:00
do_lit1:
_n_push(mov, off, 1);
BURN();
do_litb:
_n_push(mov, off, pog[ip_s++]);
BURN();
do_lits:
_n_push(mov, off, _n_resh(pog, &ip_s));
BURN();
do_litn:
_n_push(mov, off, u3k(_n_rean(pog, &ip_s)));
BURN();
do_lil1:
2018-02-14 01:17:47 +03:00
x = 1;
goto lil_in;
2018-02-14 01:17:47 +03:00
do_lilb:
x = pog[ip_s++];
goto lil_in;
do_lils:
x = _n_resh(pog, &ip_s);
goto lil_in;
2018-01-31 03:27:04 +03:00
do_liln:
2018-02-14 01:17:47 +03:00
x = u3k(_n_rean(pog, &ip_s));
goto lil_in;
2018-02-06 02:26:17 +03:00
do_lil0:
x = 0;
lil_in:
top = _n_peek(off);
u3z(*top);
*top = x;
BURN();
do_noct: // [fol old bus]
o = _n_pep(mov, off); // [old bus]
_n_toss(mov, off); // [bus]
goto nock_out;
do_nolk: // [fol old bus]
o = _n_pep(mov, off); // [old bus]
_n_toss(mov, off); // [bus]
goto nock_in;
do_nock: // [fol old bus]
o = _n_pep(mov, off); // [old bus]
_n_swap(mov, off); // [bus old]
nock_in:
x = _n_pep(mov, off);
2018-02-06 02:26:17 +03:00
fam = u3to(burnframe, u3R->cap_p) + off + mov;
2018-02-19 02:40:36 +03:00
u3R->cap_p = u3of(burnframe, fam - off);
2018-02-06 02:26:17 +03:00
fam->ip_s = ip_s;
fam->pog = pog;
_n_push(mov, off, x);
nock_out:
pog = _n_find(o);
2018-01-31 03:27:04 +03:00
ip_s = 0;
#ifdef VERBYC
fprintf(stderr, "\r\nnock jump: %u\r\n", o);
_n_print_byc(pog, ip_s);
#endif
u3z(o);
2018-01-31 03:27:04 +03:00
BURN();
2018-01-31 03:27:04 +03:00
do_deep:
top = _n_peek(off);
2018-01-31 03:27:04 +03:00
o = *top;
*top = u3du(o);
u3z(o);
BURN();
do_bump:
top = _n_peek(off);
2018-02-19 01:11:44 +03:00
*top = u3i_vint(*top);
2018-01-31 03:27:04 +03:00
BURN();
2018-02-14 20:18:53 +03:00
do_sam1:
x = 1;
goto samd_in;
do_samb:
x = pog[ip_s++];
goto samd_in;
do_sams:
x = _n_resh(pog, &ip_s);
goto samd_in;
do_samn:
x = _n_rean(pog, &ip_s);
goto samd_in;
2018-02-14 20:18:53 +03:00
do_sam0:
x = 0;
samd_in:
top = _n_peek(off);
o = *top;
*top = u3r_sing(x, o);
2018-02-14 20:18:53 +03:00
u3z(o); // don't bother losing x
BURN();
2018-02-14 20:18:53 +03:00
do_same:
x = _n_pep(mov, off);
_n_swap(mov, off);
goto same_in;
do_salm:
2018-02-14 20:18:53 +03:00
x = _n_pep(mov, off);
_n_toss(mov, off);
2018-02-14 20:18:53 +03:00
goto same_in;
same_in:
top = _n_peek(off);
2018-01-31 03:27:04 +03:00
o = *top;
*top = u3r_sing(x, o);
u3z(o);
2018-02-14 20:18:53 +03:00
u3z(x);
BURN();
do_samc:
top = _n_peek(off);
o = *top;
*top = u3r_sing(u3h(o), u3t(o));
u3z(o);
BURN();
do_skip:
ip_s += _n_resh(pog, &ip_s);
BURN();
do_sbip:
ip_s += pog[ip_s] + 1;
BURN();
do_skin:
sip_s = _n_resh(pog, &ip_s);
goto skin_in;
do_sbin:
sip_s = pog[ip_s++];
skin_in:
x = _n_pep(mov, off);
if ( c3n == x ) {
ip_s += sip_s;
}
else if ( c3y != x ) {
u3m_bail(c3__exit);
return u3_none;
}
BURN();
do_snoc: // [hed tel]
x = _n_pep(mov, off);
top = _n_peek(off);
_n_push(mov, off, u3nc(x, u3k(*top)));
BURN();
do_snol:
x = _n_pep(mov, off);
top = _n_peek(off);
*top = u3nc(x, *top);
2018-01-31 03:27:04 +03:00
BURN();
2018-01-31 05:14:37 +03:00
2018-02-05 22:20:34 +03:00
do_kics:
x = _n_resh(pog, &ip_s);
goto kick_in;
2018-01-31 05:14:37 +03:00
do_kick:
2018-02-05 22:20:34 +03:00
x = _n_rean(pog, &ip_s);
goto kick_in;
do_kicb:
2018-02-14 01:32:32 +03:00
x = pog[ip_s++];
goto kick_in;
do_slam:
x = 2;
2018-02-05 22:20:34 +03:00
kick_in:
top = _n_peek(off);
2018-01-31 05:14:37 +03:00
o = *top;
u3t_off(noc_o);
*top = u3j_kick(o, x);
u3t_on(noc_o);
if ( u3_none == *top ) {
u3_noun fol = u3x_at(x, o);
2018-02-13 02:19:11 +03:00
_n_toss(mov, off);
2018-02-06 02:26:17 +03:00
fam = u3to(burnframe, u3R->cap_p) + off + mov;
2018-02-19 02:40:36 +03:00
u3R->cap_p = u3of(burnframe, fam - off);
2018-02-06 02:26:17 +03:00
fam->ip_s = ip_s;
fam->pog = pog;
pog = _n_find(fol);
ip_s = 0;
#ifdef VERBYC
fprintf(stderr, "\r\nhead kick jump: %u, sp: %p\r\n", fol, top);
_n_print_byc(pog, ip_s);
#endif
2018-02-06 02:26:17 +03:00
_n_push(mov, off, o);
2018-01-31 05:14:37 +03:00
}
#ifdef VERBYC
else {
fprintf(stderr, "head jet\r\n");
}
#endif
2018-01-31 05:14:37 +03:00
BURN();
2018-02-05 22:20:34 +03:00
do_tics:
x = _n_resh(pog, &ip_s);
goto tick_in;
2018-01-31 05:14:37 +03:00
do_tick:
2018-02-05 22:20:34 +03:00
x = _n_rean(pog, &ip_s);
goto tick_in;
do_ticb:
2018-02-14 01:32:32 +03:00
x = pog[ip_s++];
goto tick_in;
do_slat:
x = 2;
2018-02-05 22:20:34 +03:00
tick_in:
top = _n_peek(off);
2018-01-31 05:14:37 +03:00
o = *top;
u3t_off(noc_o);
*top = u3j_kick(o, x);
u3t_on(noc_o);
if ( u3_none == *top ) {
u3_noun fol = u3x_at(x, o);
2018-01-31 05:14:37 +03:00
*top = o;
pog = _n_find(fol);
ip_s = 0;
#ifdef VERBYC
fprintf(stderr, "\r\ntail kick jump: %u, sp: %p\r\n", fol, top);
_n_print_byc(pog, ip_s);
#endif
}
#ifdef VERBYC
else {
fprintf(stderr, "tail jet\r\n");
2018-01-31 05:14:37 +03:00
}
#endif
2018-01-31 05:14:37 +03:00
BURN();
2018-02-20 22:28:01 +03:00
do_wils: // [gof bus ref]
o = _n_pep(mov,off); // [bus ref]
_n_toss(mov, off); // [ref]
top = _n_peek(off);
goto wish_in;
2018-02-20 22:28:01 +03:00
do_wish: // [gof bus ref]
o = _n_pep(mov,off); // [bus ref]
top = _n_swap(mov, off); // [ref bus]
wish_in:
u3t_off(noc_o);
2018-02-20 22:28:01 +03:00
x = u3m_soft_esc(*top, u3k(o));
u3t_on(noc_o);
if ( c3n == u3du(x) ) {
u3m_bail(u3nt(1, o, 0));
2018-02-06 02:26:17 +03:00
return u3_none;
}
else if ( c3n == u3du(u3t(x)) ) {
// replace with proper error stack push
u3t_push(u3nc(c3__hunk, _n_mush(o)));
2018-02-01 20:21:10 +03:00
u3m_bail(c3__exit);
2018-02-06 02:26:17 +03:00
return u3_none;
}
else {
u3z(o);
*top = u3k(u3t(u3t(x)));
u3z(x);
BURN();
}
do_cush:
x = _n_rean(pog, &ip_s);
o = _n_pep(mov, off);
u3t_push(u3nc(u3k(x), o));
BURN();
do_drop:
u3t_drop();
BURN();
do_heck:
x = _n_pep(mov, off);
if ( c3y == u3ud(x) ) {
u3t_off(noc_o);
u3t_heck(x);
u3t_on(noc_o);
}
else {
u3z(x);
}
BURN();
do_slog:
x = _n_pep(mov, off);
2018-02-01 20:04:47 +03:00
if ( !(u3C.wag_w & u3o_quiet) ) {
u3t_off(noc_o);
u3t_slog(x);
u3t_on(noc_o);
}
else {
u3z(x);
}
BURN();
2018-02-01 20:04:47 +03:00
do_falt: // [pro bus clu]
o = _n_pep(mov, off); // [bus clu]
2018-02-13 02:19:11 +03:00
_n_toss(mov, off); // [clu]
top = _n_peek(off);
goto fast_in;
do_fast: // [pro bus clu]
o = _n_pep(mov, off); // [bus clu]
top = _n_swap(mov, off); // [clu bus]
fast_in:
u3t_off(noc_o);
u3j_mine(*top, u3k(o));
u3t_on(noc_o);
*top = o;
BURN();
do_skim:
sip_s = _n_resh(pog, &ip_s);
goto skim_in;
do_skib:
sip_s = pog[ip_s++];
skim_in:
top = _n_peek(off);
x = u3k(*top);
goto skim_out;
do_slim:
sip_s = _n_resh(pog, &ip_s);
goto slim_in;
do_slib:
sip_s = pog[ip_s++];
slim_in:
x = _n_pep(mov, off);
skim_out:
o = _n_rean(pog, &ip_s);
2018-02-20 22:28:01 +03:00
x = u3nc(x, u3k(o));
o = u3z_find(144 + c3__nock, x);
if ( u3_none == o ) {
_n_push(mov, off, x);
2018-02-20 22:28:01 +03:00
_n_push(mov, off, u3k(u3h(x)));
}
else {
ip_s += sip_s;
2018-02-13 02:19:11 +03:00
_n_push(mov, off, o);
u3z(x);
}
BURN();
do_save:
x = _n_pep(mov, off);
top = _n_peek(off);
o = *top;
if ( &(u3H->rod_u) != u3R ) {
u3z_save(144 + c3__nock, o, x);
}
*top = x;
u3z(o);
BURN();
2018-01-31 03:27:04 +03:00
}
}
2018-02-01 20:04:47 +03:00
#ifdef VERBYC
/* _n_print_byc(): print bytecode. used for debugging.
*/
static void
_n_print_byc(c3_y* pog, c3_s her_s)
{
c3_s ip_s = 0;
if ( her_s == 0 ) {
fprintf(stderr, "begin: {");
}
else {
fprintf(stderr, "resume: {");
}
int first = 1;
while ( pog[ip_s] ) {
if ( first ) {
first = 0;
}
else if (ip_s == her_s) {
fprintf(stderr, " [*]");
}
else {
fprintf(stderr, " ");
}
switch ( pog[ip_s] ) {
default:
fprintf(stderr, "%s", names[pog[ip_s++]]);
break;
2018-02-13 02:19:11 +03:00
case FRAB:
case FLAB:
case LILB:
case LITB:
case SAMB:
case SBIP:
case SBIN:
2018-02-05 22:20:34 +03:00
case KICB:
case TICB:
fprintf(stderr, "[%s ", names[pog[ip_s++]]);
fprintf(stderr, "%u]", pog[ip_s++]);
2018-02-05 22:20:34 +03:00
break;
case FRAS:
2018-02-13 02:19:11 +03:00
case FLAS:
case LILS:
case LITS:
case SAMS:
case SKIP:
2018-02-13 02:19:11 +03:00
case SKIN:
case KICS:
case TICS:
fprintf(stderr, "[%s ", names[pog[ip_s++]]);
fprintf(stderr, "%u]", _n_resh(pog, &ip_s));
break;
2018-02-13 02:19:11 +03:00
case CUSH:
2018-02-05 22:20:34 +03:00
case FRAG:
2018-02-13 02:19:11 +03:00
case FLAG:
case LILN:
case LITN:
case SAMN:
2018-02-13 02:19:11 +03:00
case TICK:
case KICK:
fprintf(stderr, "[%s ", names[pog[ip_s++]]);
fprintf(stderr, "%u]", _n_rean(pog, &ip_s));
break;
2018-02-13 02:19:11 +03:00
case SKIB:
case SLIB:
fprintf(stderr, "[%s", names[pog[ip_s++]]);
fprintf(stderr, " %u ", pog[ip_s++]);
fprintf(stderr, "%u]", _n_rean(pog, &ip_s));
2018-02-13 02:19:11 +03:00
break;
case SKIM:
case SLIM:
fprintf(stderr, "[%s", names[pog[ip_s++]]);
fprintf(stderr, " %u ", _n_resh(pog, &ip_s));
fprintf(stderr, "%u]", _n_rean(pog, &ip_s));
2018-02-13 02:19:11 +03:00
break;
}
}
fprintf(stderr, " halt}\r\n");
}
#endif
2018-02-01 20:04:47 +03:00
/* _n_burn_on(): produce .*(bus fol) with bytecode interpreter
*/
static u3_noun
_n_burn_on(u3_noun bus, u3_noun fol)
{
c3_y* pog = _n_find(fol);
c3_ys mov, off;
2018-02-01 20:04:47 +03:00
u3z(fol);
if ( c3y == u3a_is_north(u3R) ) {
mov = -1;
off = 0;
}
else {
mov = 1;
off = -1;
}
2018-02-20 22:28:01 +03:00
u3_noun pro = _n_burn(pog, bus, mov, off);
return pro;
2018-02-01 20:04:47 +03:00
}
#if 0
/* u3n_burn_on(): useful for distinguishing the bytecode interpreter from the
* tree-walking interpreter.
*/
u3_noun
u3n_burn_on(u3_noun bus, u3_noun fol)
{
u3_noun pro;
u3t_on(noc_o);
pro = _n_burn_on(bus, fol);
u3t_off(noc_o);
return pro;
}
#endif
2018-02-05 21:27:39 +03:00
2018-02-20 22:28:01 +03:00
/* _n_take_byc(): copy bytecode from a junior road
*/
static c3_y*
_n_take_byc(c3_y* pog)
{
u3_noun x;
c3_s sax_s, ip_s, len_s;
c3_y* gop, cod_y;
// measure
for ( ip_s = 0; (cod_y = pog[ip_s]) != HALT; ++ip_s ) {
ip_s += _n_arg(cod_y);
}
len_s = ip_s + 1;
gop = u3a_malloc(len_s);
for ( ip_s = 0; ip_s < len_s; ) {
cod_y = gop[ip_s] = pog[ip_s];
x = u3_none;
sax_s = ip_s + 1;
switch ( cod_y ) {
default:
sax_s += _n_arg(cod_y);
break;
case CUSH: case FRAG: case FLAG: case LILN: case LITN:
case SAMN: case TICK: case KICK:
x = _n_rean(pog, &sax_s);
break;
case SKIB: case SLIB:
sax_s += sizeof(c3_y);
x = _n_rean(pog, &sax_s);
break;
case SKIM: case SLIM:
sax_s += sizeof(c3_s);
x = _n_rean(pog, &sax_s);
break;
}
if ( u3_none != x ) {
2018-02-20 23:13:14 +03:00
x = u3a_take(x);
2018-02-20 22:28:01 +03:00
gop[ip_s++] = cod_y;
gop[ip_s++] = (c3_y) x;
gop[ip_s++] = (c3_y) (x >> 8);
gop[ip_s++] = (c3_y) (x >> 16);
gop[ip_s++] = (c3_y) (x >> 24);
}
else {
while ( ip_s < sax_s ) {
gop[ip_s] = pog[ip_s];
++ip_s;
}
}
}
return gop;
}
/* _n_reap(): reap key and value from byc table.
*/
static void
_n_reap(u3_noun kev)
{
u3_noun fol = u3h(kev);
u3_noun got = u3t(kev);
2018-02-20 22:28:01 +03:00
c3_y* pog = u3a_into(got);
c3_y* gop = _n_take_byc(pog);
u3_noun tog = u3a_outa(gop);
2018-02-20 23:13:14 +03:00
u3_noun lof = u3a_take(fol);
u3h_put(u3R->byc.har_p, lof, tog);
u3z(lof);
}
2018-02-05 21:27:39 +03:00
/* u3n_beep(): promote bytecode state.
*/
void
u3n_beep(u3p(u3h_root) har_p)
{
u3h_walk(har_p, _n_reap);
2018-02-05 21:27:39 +03:00
}
#if 0
/* _n_print_stack(): print out the cap stack up to a designated "empty"
* used only for debugging
*/
static void _n_print_stack(u3p(u3_noun) empty) {
c3_w cur_p = u3R->cap_p;
fprintf(stderr, "[");
int first = 1;
while ( cur_p != empty ) {
if ( first ) {
first = 0;
}
else {
fprintf(stderr, " ");
}
if ( c3y == u3a_is_north(u3R) ) {
fprintf(stderr, "%u", *(u3to(u3_noun, cur_p)));
cur_p++;
}
else {
fprintf(stderr, "%u", *(u3to(u3_noun, cur_p-1)));
cur_p--;
}
}
fprintf(stderr, "]\r\n");
}
#endif