shrub/noun/nock.c

2648 lines
59 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"
/* 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)
2018-03-12 20:45:04 +03:00
// define to have each opcode printed as it executes,
// along with some other debugging info
# undef VERBOSE_BYTECODE
/* _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;
}
#if 0
// Retained for debugging purposes.
static u3_noun _n_nock_on(u3_noun bus, u3_noun fol);
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
}
}
}
}
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-11-02 01:13:18 +03:00
#endif
2018-01-31 03:27:04 +03:00
2018-04-12 02:30:57 +03:00
/* These must match the order in the section marked OPCODE TABLE,
* and several opcodes "overflow" (from byte to short index) to
* their successor, so order can matter here. */
// general purpose
#define HALT 0
#define BAIL 1
#define COPY 2
#define SWAP 3
#define TOSS 4
#define AUTO 5
#define AULT 6
#define SNOC 7
#define SNOL 8
#define HEAD 9
#define HELD 10
#define TAIL 11
#define TALL 12
// fragment (keep)
#define FABK 13
#define FASK 14
#define FIBK 15
#define FISK 16
// fragment (lose)
#define FABL 17
#define FASL 18
#define FIBL 19
#define FISL 20
// literal (keep)
#define LIT0 21
#define LIT1 22
#define LITB 23
#define LITS 24
#define LIBK 25
#define LISK 26
// literal (lose)
#define LIL0 27
#define LIL1 28
#define LILB 29
#define LILS 30
#define LIBL 31
#define LISL 32
// nock
#define NOLK 33
#define NOCT 34
#define NOCK 35
// 3 & 4
#define DEEP 36
#define BUMP 37
// equality
#define SAM0 38
#define SAM1 39
#define SAMB 40
#define SAMS 41
#define SANB 42
#define SANS 43
#define SAME 44
#define SALM 45
#define SAMC 46
// unconditional skips
#define SBIP 47
#define SIPS 48
#define SWIP 49
// conditional skips
#define SBIN 50
#define SINS 51
#define SWIN 52
// nock 9
#define KICB 53
#define KICS 54
#define TICB 55
#define TICS 56
// nock 11
#define WILS 57
#define WISH 58
// hint processing
#define BUSH 59
#define SUSH 60
#define DROP 61
#define HECK 62
#define SLOG 63
// fast (keep)
#define BAST 64
#define SAST 65
// fast (lose)
#define BALT 66
#define SALT 67
// memo (keep)
#define SKIB 68
#define SKIS 69
// memo (lose)
#define SLIB 70
#define SLIS 71
#define SAVE 72
2018-06-10 20:39:36 +03:00
// nock 12
#define MUTH 73
#define KUTH 74
#define MUTT 75
#define KUTT 76
#define MUSM 77
#define KUSM 78
#define MUTB 79
#define MUTS 80
#define MITB 81
#define MITS 82
#define KUTB 83
#define KUTS 84
#define KITB 85
#define KITS 86
#define LAST 87
2018-04-12 02:30:57 +03:00
2018-04-12 21:31:42 +03:00
/* _n_arg(): return the size (in bytes) of an opcode's argument
*/
static inline c3_y
_n_arg(c3_y cod_y)
{
switch ( cod_y ) {
case FABK: case FABL: case FIBL: case FIBK:
case LILB: case LITB: case LIBL: case LIBK:
case SAMB: case SANB: case SBIP: case SBIN:
case SLIB: case SKIB: case KICB: case TICB:
case BUSH: case BAST: case BALT:
2018-06-11 00:02:07 +03:00
case MUTB: case KUTB: case MITB: case KITB:
2018-04-12 21:31:42 +03:00
return sizeof(c3_y);
case FASK: case FASL: case FISL: case FISK:
case LILS: case LITS: case LISL: case LISK:
case SAMS: case SANS: case SIPS: case SINS:
case SLIS: case SKIS: case KICS: case TICS:
case SUSH: case SAST: case SALT:
2018-06-11 00:02:07 +03:00
case MUTS: case KUTS: case MITS: case KITS:
2018-04-12 21:31:42 +03:00
return sizeof(c3_s);
case SWIP: case SWIN:
return sizeof(c3_l);
default:
2018-06-11 00:02:07 +03:00
c3_assert( cod_y < LAST );
2018-04-12 21:31:42 +03:00
return 0;
}
}
2018-04-12 02:30:57 +03:00
/* _n_melt(): measure space for list of ops (from _n_comp) */
2018-04-13 03:14:40 +03:00
static u3_noun
2018-04-12 02:30:57 +03:00
_n_melt(u3_noun ops, c3_w* byc_w, c3_w* cal_w,
c3_w* reg_w, c3_w* lit_w, c3_w* mem_w)
{
2018-04-13 03:14:40 +03:00
c3_w len_w = u3qb_lent(ops),
i_w = len_w - 1,
a_w;
2018-04-12 21:31:42 +03:00
c3_y cod_y;
2018-04-13 03:14:40 +03:00
c3_y* siz_y = u3a_malloc(len_w);
u3_noun op, sip = u3_nul;
2018-04-12 02:30:57 +03:00
while ( u3_nul != ops ) {
op = u3h(ops);
if ( c3n == u3du(op) ) {
2018-04-13 03:14:40 +03:00
switch ( op ) {
default:
siz_y[i_w] = 1;
break;
case BAST: case BALT:
a_w = (*reg_w)++;
if ( a_w <= 0xFF ) {
siz_y[i_w] = 2;
}
else if ( a_w <= 0xFFFF ) {
siz_y[i_w] = 3;
}
else {
fprintf(stderr, "_n_melt(): over 2^16 registration sites.\r\n");
c3_assert(0);
}
break;
}
2018-04-12 02:30:57 +03:00
}
else {
cod_y = u3h(op);
switch ( cod_y ) {
default:
2018-04-13 03:14:40 +03:00
siz_y[i_w] = 1 + _n_arg(cod_y);
break;
case SBIP: case SBIN: {
c3_l tot_l = 0,
sip_l = u3t(op);
c3_w j_w, k_w = i_w;
for ( j_w = 0; j_w < sip_l; ++j_w ) {
tot_l += siz_y[++k_w];
}
sip = u3nc(tot_l, sip);
siz_y[i_w] = tot_l <= 0xFF ? 2 : tot_l <= 0xFFFF ? 3 : 5;
2018-04-12 02:30:57 +03:00
break;
2018-04-13 03:14:40 +03:00
}
2018-04-12 02:30:57 +03:00
case SKIB: case SLIB: {
c3_l tot_l = 0,
sip_l = u3h(u3t(op));
c3_w j_w, k_w = i_w;
for ( j_w = 0; j_w < sip_l; ++j_w ) {
tot_l += siz_y[++k_w];
}
sip = u3nc(tot_l, sip);
a_w = (*mem_w)++;
if ( a_w <= 0xFF ) {
siz_y[i_w] = 2;
}
else if ( a_w <= 0xFFFF ) {
siz_y[i_w] = 3;
}
else {
fprintf(stderr, "_n_melt(): over 2^16 memos.\r\n");
c3_assert(0);
}
break;
}
2018-04-13 03:14:40 +03:00
case SIPS: case SINS: case SWIP: case SWIN:
2018-04-12 02:30:57 +03:00
case SAST: case SALT: case KICS: case TICS:
case FISK: case FISL: case SUSH: case SANS:
case LISL: case LISK: case SKIS: case SLIS:
c3_assert(0); //overflows
break;
case KICB: case TICB:
2018-04-13 03:14:40 +03:00
a_w = (*cal_w)++;
if ( a_w <= 0xFF ) {
siz_y[i_w] = 2;
2018-04-12 02:30:57 +03:00
}
2018-04-13 03:14:40 +03:00
else if ( a_w <= 0xFFFF ) {
siz_y[i_w] = 3;
2018-04-12 02:30:57 +03:00
}
else {
fprintf(stderr, "_n_melt(): over 2^16 call sites.\r\n");
c3_assert(0);
}
break;
case BUSH: case FIBK: case FIBL:
case SANB: case LIBL: case LIBK:
2018-06-10 20:39:36 +03:00
case KITB: case MITB:
2018-04-13 03:14:40 +03:00
a_w = (*lit_w)++;
if ( a_w <= 0xFF ) {
siz_y[i_w] = 2;
2018-04-12 02:30:57 +03:00
}
2018-04-13 03:14:40 +03:00
else if ( a_w <= 0xFFFF ) {
siz_y[i_w] = 3;
2018-04-12 02:30:57 +03:00
}
else {
fprintf(stderr, "_n_melt(): over 2^16 literals.\r\n");
c3_assert(0);
}
break;
}
}
2018-04-13 03:14:40 +03:00
*(byc_w) += siz_y[i_w--];
2018-04-12 02:30:57 +03:00
ops = u3t(ops);
}
2018-04-13 03:14:40 +03:00
u3a_free(siz_y);
return u3kb_flop(sip);
2018-04-12 02:30:57 +03:00
}
2018-04-30 21:39:33 +03:00
/* _n_prog_dat(): return pointer to program's data segment
*/
static void*
_n_prog_dat(u3n_prog* pog_u)
{
return ((void*) pog_u) + sizeof(u3n_prog);
}
2018-04-30 21:39:33 +03:00
/* _n_prog_new(): allocate and set up pointers for u3n_prog
*/
static u3n_prog*
2018-04-12 21:31:42 +03:00
_n_prog_new(c3_w byc_w, c3_w cal_w,
c3_w reg_w, c3_w lit_w, c3_w mem_w)
2018-04-12 02:30:57 +03:00
{
c3_w cab_w = (sizeof(u3j_site) * cal_w),
reb_w = (sizeof(u3j_rite) * reg_w),
2018-04-12 21:31:42 +03:00
lib_w = (sizeof(u3_noun) * lit_w),
meb_w = (sizeof(u3n_memo) * mem_w),
2018-04-12 21:31:42 +03:00
dat_w = byc_w + cab_w + reb_w + lib_w + meb_w;
2018-04-12 02:30:57 +03:00
u3n_prog* pog_u = u3a_malloc(sizeof(u3n_prog) + dat_w);
2018-04-12 02:30:57 +03:00
pog_u->byc_u.own_o = c3y;
pog_u->byc_u.len_w = byc_w;
pog_u->byc_u.ops_y = (c3_y*) _n_prog_dat(pog_u);
2018-04-12 02:30:57 +03:00
pog_u->lit_u.len_w = lit_w;
2018-04-12 21:31:42 +03:00
pog_u->lit_u.non = (u3_noun*) (pog_u->byc_u.ops_y + pog_u->byc_u.len_w);
2018-04-12 02:30:57 +03:00
pog_u->mem_u.len_w = mem_w;
pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w);
2018-04-12 02:30:57 +03:00
pog_u->cal_u.len_w = cal_w;
pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w);
2018-04-12 02:30:57 +03:00
pog_u->reg_u.len_w = reg_w;
pog_u->reg_u.rit_u = (u3j_rite*) (pog_u->cal_u.sit_u + pog_u->cal_u.len_w);
2018-04-12 02:30:57 +03:00
return pog_u;
}
2018-04-30 21:39:33 +03:00
/* _n_prog_old(): as _n_prog_new(),
* but leech off senior program's data segment
*/
static u3n_prog*
_n_prog_old(u3n_prog* sep_u)
2018-04-12 02:30:57 +03:00
{
c3_w cab_w = sizeof(u3j_site) * sep_u->cal_u.len_w,
reb_w = sizeof(u3j_rite) * sep_u->reg_u.len_w,
2018-04-12 21:31:42 +03:00
lib_w = sizeof(u3_noun) * sep_u->lit_u.len_w,
meb_w = sizeof(u3n_memo) * sep_u->mem_u.len_w,
2018-04-12 21:31:42 +03:00
dat_w = cab_w + reb_w + lib_w + meb_w;
2018-04-12 02:30:57 +03:00
u3n_prog* pog_u = u3a_malloc(sizeof(u3n_prog) + dat_w);
2018-04-12 02:30:57 +03:00
pog_u->byc_u.own_o = c3n;
2018-04-12 21:31:42 +03:00
pog_u->byc_u.len_w = sep_u->byc_u.len_w;
pog_u->byc_u.ops_y = sep_u->byc_u.ops_y;
2018-04-12 02:30:57 +03:00
2018-04-12 21:31:42 +03:00
pog_u->lit_u.len_w = sep_u->lit_u.len_w;
pog_u->lit_u.non = (u3_noun*) _n_prog_dat(pog_u);
2018-04-12 02:30:57 +03:00
2018-04-12 21:31:42 +03:00
pog_u->mem_u.len_w = sep_u->mem_u.len_w;
pog_u->mem_u.sot_u = (u3n_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w);
2018-04-12 02:30:57 +03:00
2018-04-12 21:31:42 +03:00
pog_u->cal_u.len_w = sep_u->cal_u.len_w;
pog_u->cal_u.sit_u = (u3j_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w);
2018-04-12 02:30:57 +03:00
2018-04-12 21:31:42 +03:00
pog_u->reg_u.len_w = sep_u->reg_u.len_w;
pog_u->reg_u.rit_u = (u3j_rite*) (pog_u->cal_u.sit_u + pog_u->cal_u.len_w);
2018-04-12 02:30:57 +03:00
memcpy(pog_u->lit_u.non, sep_u->lit_u.non, dat_w);
2018-04-12 02:30:57 +03:00
return pog_u;
}
2018-04-30 21:39:33 +03:00
/* _n_prog_asm_inx(): write an index to the bytestream with overflow
*/
2018-04-12 21:31:42 +03:00
static void
2018-04-12 02:30:57 +03:00
_n_prog_asm_inx(c3_y* buf_y, c3_w* i_w, c3_s inx_s, c3_y cod)
{
if ( inx_s <= 0xFF ) {
buf_y[(*i_w)--] = (c3_y) (inx_s);
buf_y[*i_w] = (c3_y) cod;
}
else {
buf_y[(*i_w)--] = (c3_y) (inx_s >> 8);
buf_y[(*i_w)--] = (c3_y) (inx_s);
// the short-index versions of these opcodes must immediately
// follow the byte-index versions because of this convention
buf_y[(*i_w)] = cod + 1;
}
}
2018-04-30 21:39:33 +03:00
/* _n_prog_asm(): assemble list of ops (from _n_comp) into u3n_prog
*/
2018-04-12 02:30:57 +03:00
static void
_n_prog_asm(u3_noun ops, u3n_prog* pog_u, u3_noun sip)
2018-04-12 02:30:57 +03:00
{
2018-04-13 03:14:40 +03:00
u3_noun top = ops;
2018-04-12 21:31:42 +03:00
c3_y* buf_y = pog_u->byc_u.ops_y;
2018-04-12 02:30:57 +03:00
c3_s lit_s = 0,
cal_s = 0,
mem_s = 0,
reg_s = 0;
2018-04-13 03:14:40 +03:00
c3_w i_w = pog_u->byc_u.len_w-1;
2018-04-12 02:30:57 +03:00
buf_y[i_w] = HALT;
while ( i_w-- > 0 ) {
u3_noun op = u3h(ops);
if ( c3y == u3ud(op) ) {
2018-04-13 03:14:40 +03:00
switch ( op ) {
default:
buf_y[i_w] = (c3_y) u3h(ops);
break;
/* registration site index args */
case BAST: case BALT: {
_n_prog_asm_inx(buf_y, &i_w, reg_s, op);
u3j_rite* rit_u = &(pog_u->reg_u.rit_u[reg_s++]);
2018-04-15 00:57:12 +03:00
rit_u->own_o = c3n;
rit_u->clu = u3_none;
2018-04-30 19:57:09 +03:00
rit_u->fin_p = 0;
2018-04-13 03:14:40 +03:00
break;
}
}
2018-04-12 02:30:57 +03:00
}
else {
u3_noun cod = u3h(op);
switch ( cod ) {
default:
c3_assert(0);
2018-04-12 21:31:42 +03:00
return;
2018-04-12 02:30:57 +03:00
/* memo index args */
case SKIB: case SLIB: {
u3n_memo* mem_u;
c3_l sip_l = u3h(sip);
u3_noun tmp = sip;
sip = u3k(u3t(sip));
u3z(tmp);
_n_prog_asm_inx(buf_y, &i_w, mem_s, cod);
mem_u = &(pog_u->mem_u.sot_u[mem_s++]);
mem_u->sip_l = sip_l;
mem_u->key = u3k(u3t(u3t(op)));
break;
}
/* skips */
2018-04-13 03:14:40 +03:00
case SBIP: case SBIN: {
c3_l sip_l = u3h(sip);
u3_noun tmp = sip;
sip = u3k(u3t(sip));
u3z(tmp);
if ( sip_l <= 0xFF ) {
buf_y[i_w--] = (c3_y) sip_l;
buf_y[i_w] = (c3_y) cod;
}
else if ( sip_l <= 0xFFFF ) {
buf_y[i_w--] = (c3_y) (sip_l >> 8);
buf_y[i_w--] = (c3_y) sip_l;
buf_y[i_w] = (c3_y) cod + 1;
}
else {
buf_y[i_w--] = (c3_y) (sip_l >> 24);
buf_y[i_w--] = (c3_y) (sip_l >> 16);
buf_y[i_w--] = (c3_y) (sip_l >> 8);
buf_y[i_w--] = (c3_y) sip_l;
buf_y[i_w] = (c3_y) cod + 2;
}
2018-04-12 02:30:57 +03:00
break;
2018-04-13 03:14:40 +03:00
}
2018-04-12 02:30:57 +03:00
/* 8-bit direct args */
case FABK: case FABL:
case LITB: case LILB:
2018-06-11 00:02:07 +03:00
case MUTB: case KUTB:
2018-04-12 02:30:57 +03:00
case SAMB:
buf_y[i_w--] = (c3_y) u3t(op);
buf_y[i_w] = (c3_y) cod;
break;
/* 16-bit direct args */
case FASK: case FASL:
case LILS: case LITS:
2018-06-11 00:02:07 +03:00
case MUTS: case KUTS:
2018-04-12 02:30:57 +03:00
case SAMS: case SIPS: case SINS: {
c3_s off_s = u3t(op);
buf_y[i_w--] = (c3_y) (off_s >> 8);
buf_y[i_w--] = (c3_y) off_s;
buf_y[i_w] = (c3_y) cod;
break;
}
/* 31-bit direct args */
case SWIP: case SWIN: {
c3_w off_l = u3t(op);
buf_y[i_w--] = (c3_y) (off_l >> 24);
buf_y[i_w--] = (c3_y) (off_l >> 16);
buf_y[i_w--] = (c3_y) (off_l >> 8);
buf_y[i_w--] = (c3_y) off_l;
buf_y[i_w] = (c3_y) cod;
break;
}
/* literal index args */
case FIBK: case FIBL:
case LIBK: case LIBL:
case BUSH: case SANB:
2018-06-10 20:39:36 +03:00
case KITB: case MITB:
2018-04-12 02:30:57 +03:00
_n_prog_asm_inx(buf_y, &i_w, lit_s, cod);
pog_u->lit_u.non[lit_s++] = u3k(u3t(op));
break;
/* call site index args */
case TICB: case KICB: {
_n_prog_asm_inx(buf_y, &i_w, cal_s, cod);
u3j_site* sit_u = &(pog_u->cal_u.sit_u[cal_s++]);
2018-04-17 02:54:20 +03:00
sit_u->axe = u3k(u3t(op));
2018-04-30 20:16:47 +03:00
sit_u->pog_p = 0;
2018-04-17 02:54:20 +03:00
sit_u->bat = u3_none;
2018-05-08 23:33:55 +03:00
sit_u->bas = u3_none;
sit_u->loc = u3_none;
sit_u->lab = u3_none;
sit_u->jet_o = c3n;
sit_u->fon_o = c3n;
sit_u->cop_u = NULL;
sit_u->ham_u = NULL;
2018-04-30 20:16:47 +03:00
sit_u->fin_p = 0;
2018-04-12 02:30:57 +03:00
break;
}
}
}
ops = u3t(ops);
}
2018-04-13 03:14:40 +03:00
u3z(top);
2018-04-12 02:30:57 +03:00
// this assert will fail if we overflow a c3_w worth of instructions
c3_assert(u3_nul == ops);
2018-04-13 03:14:40 +03:00
// this is just a sanity check
c3_assert(u3_nul == sip);
2018-04-12 21:31:42 +03:00
}
2018-04-30 21:39:33 +03:00
/* _n_prog_from_ops(): new program from _n_comp() product
*/
static u3n_prog*
2018-04-12 21:31:42 +03:00
_n_prog_from_ops(u3_noun ops)
{
2018-04-13 03:14:40 +03:00
u3_noun sip;
u3n_prog* pog_u;
2018-04-12 21:31:42 +03:00
c3_w byc_w = 1, // HALT
cal_w = 0,
reg_w = 0,
lit_w = 0,
mem_w = 0;
2018-04-13 03:14:40 +03:00
sip = _n_melt(ops, &byc_w, &cal_w, &reg_w, &lit_w, &mem_w);
2018-04-12 21:31:42 +03:00
pog_u = _n_prog_new(byc_w, cal_w, reg_w, lit_w, mem_w);
2018-04-13 03:14:40 +03:00
_n_prog_asm(ops, pog_u, sip);
2018-04-12 21:31:42 +03:00
return pog_u;
2018-04-12 02:30:57 +03:00
}
2018-02-01 20:04:47 +03:00
2018-04-17 02:54:20 +03:00
#if 0
2018-03-12 20:45:04 +03:00
/* _n_print_stack(): print out the cap stack up to a designated "empty"
* used only for debugging
*/
2018-03-12 20:45:04 +03:00
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
2018-04-17 02:54:20 +03:00
#ifdef VERBOSE_BYTECODE
2018-02-20 22:28:01 +03:00
// match to OPCODE TABLE
2018-03-12 20:45:04 +03:00
static char* opcode_names[] = {
2018-02-20 22:28:01 +03:00
"halt", "bail",
"copy", "swap", "toss",
2018-04-12 02:30:57 +03:00
"auto", "ault", "snoc", "snol",
"head", "held", "tail", "tall",
"fabk", "fask", "fibk", "fisk",
"fabl", "fasl", "fibl", "fisl",
"lit0", "lit1", "litb", "lits",
"libk", "lisk",
"lil0", "lil1", "lilb", "lils",
"libl", "lisl",
2018-02-20 22:28:01 +03:00
"nolk", "noct", "nock",
"deep", "bump",
2018-04-12 02:30:57 +03:00
"sam0", "sam1", "samb", "sams",
"sanb", "sans",
2018-02-20 22:28:01 +03:00
"same", "salm", "samc",
2018-04-12 02:30:57 +03:00
"sbip", "sips", "swip",
"sbin", "sins", "swin",
"kicb", "kics", "ticb", "tics",
2018-02-20 22:28:01 +03:00
"wils", "wish",
2018-04-12 02:30:57 +03:00
"bush", "sush",
"drop", "heck", "slog",
"bast", "sast",
"balt", "salt",
"skib", "skis", "slib", "slis",
"save",
2018-06-10 20:39:36 +03:00
"muth", "kuth", "mutt", "kutt",
"musm", "kusm",
2018-06-10 20:39:36 +03:00
"mutb", "muts", "mitb", "mits",
"kutb", "kuts", "kitb", "kits",
2018-02-20 22:28:01 +03:00
};
#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)
{
2018-03-06 00:38:34 +03:00
*dst = u3kb_weld(src, *dst);
}
2018-04-12 02:30:57 +03:00
/* _n_emit(): emit a single instruction to ops
2018-02-20 22:28:01 +03:00
*/
2018-04-12 02:30:57 +03:00
static inline void
2018-02-20 22:28:01 +03:00
_n_emit(u3_noun *ops, u3_noun op)
{
*ops = u3nc(op, *ops);
}
2018-04-12 02:30:57 +03:00
static c3_w _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.
*/
2018-04-12 02:30:57 +03:00
static c3_w
_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-04-12 02:30:57 +03:00
c3_w tot_w = 0;
2018-02-07 21:26:06 +03:00
u3_noun zep, hod;
2018-01-31 03:27:04 +03:00
u3x_cell(hif, &zep, &hod);
switch ( zep ) {
default:
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, hod, c3n, c3n);
++tot_w; _n_emit(ops, TOSS);
tot_w += _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:
2018-03-12 20:45:04 +03:00
case c3__spot:
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, hod, c3n, c3n);
++tot_w; _n_emit(ops, u3nc(BUSH, zep)); // overflows to SUSH
tot_w += _n_comp(ops, nef, los_o, c3n);
++tot_w; _n_emit(ops, DROP);
2018-01-31 03:27:04 +03:00
break;
2018-02-13 02:19:11 +03:00
case c3__live:
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, hod, c3n, c3n);
++tot_w; _n_emit(ops, HECK);
tot_w += _n_comp(ops, nef, los_o, tel_o);
2018-01-31 03:27:04 +03:00
break;
2018-03-12 20:45:04 +03:00
case c3__slog:
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, hod, c3n, c3n);
++tot_w; _n_emit(ops, SLOG);
tot_w += _n_comp(ops, nef, los_o, tel_o);
2018-01-31 03:27:04 +03:00
break;
// germ and sole are unused...
2018-03-12 20:45:04 +03:00
case c3__fast:
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, hod, c3n, c3n);
++tot_w; _n_emit(ops, SWAP);
tot_w += _n_comp(ops, nef, c3n, c3n);
// overflows to SALT / SAST
++tot_w; _n_emit(ops, (c3y == los_o) ? BALT : BAST);
2018-01-31 03:27:04 +03:00
break;
case c3__memo: {
u3_noun mem = u3_nul;
2018-04-12 02:30:57 +03:00
c3_w mem_w = 0;
c3_y op_y;
// we just throw away the hint (why is this not a static hint?)
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, hod, c3n, c3n);
++tot_w; _n_emit(ops, TOSS);
// memoizing code always loses TOS because SAVE needs [pro key]
2018-04-12 02:30:57 +03:00
mem_w += _n_comp(&mem, nef, c3y, c3n);
++mem_w; _n_emit(&mem, SAVE);
op_y = (c3y == los_o) ? SLIB : SKIB; // overflows to SLIS / SKIS
++tot_w; _n_emit(ops, u3nt(op_y, mem_w, u3k(nef)));
2018-04-12 21:31:42 +03:00
tot_w += mem_w; _n_apen(ops, mem);
2018-01-31 03:27:04 +03:00
break;
}
}
2018-04-12 21:31:42 +03:00
return tot_w;
2018-01-31 03:27:04 +03:00
}
}
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.
2018-04-12 02:30:57 +03:00
* 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
2018-04-12 02:30:57 +03:00
* return: number of instructions added to the opcode list
2018-02-01 20:04:47 +03:00
*/
2018-04-12 02:30:57 +03:00
static c3_w
_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;
2018-04-12 02:30:57 +03:00
c3_w tot_w = 0;
2018-01-31 03:27:04 +03:00
u3_noun cod, arg, hed, tel;
u3x_cell(fol, &cod, &arg);
if ( c3y == u3du(cod) ) {
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, cod, c3n, c3n);
++tot_w; _n_emit(ops, SWAP);
tot_w += _n_comp(ops, arg, c3n, c3n);
++tot_w; _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:
2018-04-12 02:30:57 +03:00
++tot_w; _n_emit(ops, BAIL);
2018-01-31 03:27:04 +03:00
break;
case 1:
if ( c3n == los_o ) {
2018-04-12 02:30:57 +03:00
++tot_w; _n_emit(ops, COPY);
}
2018-01-31 03:27:04 +03:00
break;
case 2:
2018-04-12 02:30:57 +03:00
++tot_w; _n_emit(ops, (c3y == los_o) ? HELD : HEAD);
2018-01-31 03:27:04 +03:00
break;
case 3:
2018-04-12 02:30:57 +03:00
++tot_w; _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-04-12 02:30:57 +03:00
? (arg <= 0xFF ? FABL : arg <= 0xFFFF ? FASL : FIBL) // overflows to FISL
: (arg <= 0xFF ? FABK : arg <= 0xFFFF ? FASK : FIBK); // overflows to FISK
++tot_w; _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:
2018-04-12 02:30:57 +03:00
++tot_w; _n_emit(ops, (c3y == los_o) ? LIL0 : LIT0);
2018-02-14 01:17:47 +03:00
break;
case 1:
2018-04-12 02:30:57 +03:00
++tot_w; _n_emit(ops, (c3y == los_o) ? LIL1 : LIT1);
2018-02-14 01:17:47 +03:00
break;
default:
op_y = (c3y == los_o)
2018-04-12 02:30:57 +03:00
? (arg <= 0xFF ? LILB : arg <= 0xFFFF ? LILS : LIBL) // overflows to LISL
: (arg <= 0xFF ? LITB : arg <= 0xFFFF ? LITS : LIBK); // overflows to LISK
++tot_w; _n_emit(ops, u3nc(op_y, u3k(arg)));
2018-02-14 01:17:47 +03:00
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);
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, hed, c3n, c3n);
++tot_w; _n_emit(ops, SWAP);
tot_w += _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 */
2018-03-12 20:45:04 +03:00
op_y = (c3y == tel_o) ? NOCT
: ((c3y == los_o) ? NOLK : NOCK);
2018-04-12 02:30:57 +03:00
++tot_w; _n_emit(ops, op_y);
2018-01-31 03:27:04 +03:00
break;
2018-01-31 03:27:04 +03:00
case 3:
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, arg, los_o, c3n);
++tot_w; _n_emit(ops, DEEP);
2018-01-31 03:27:04 +03:00
break;
2018-01-31 03:27:04 +03:00
case 4:
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, arg, los_o, c3n);
++tot_w; _n_emit(ops, BUMP);
2018-01-31 03:27:04 +03:00
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) ) {
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, arg, los_o, c3n);
++tot_w; _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)) ) {
2018-04-12 02:30:57 +03:00
++tot_w; _n_emit(ops, (c3y == los_o) ? LIL0 : LIT0);
}
else {
2018-04-12 02:30:57 +03:00
++tot_w; _n_emit(ops, (c3y == los_o) ? LIL1 : LIT1);
}
}
else if ( !hec_t && !tec_t ) {
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, hed, c3n, c3n);
++tot_w; _n_emit(ops, SWAP);
tot_w += _n_comp(ops, tel, c3n, c3n);
++tot_w; _n_emit(ops, (c3y == los_o) ? SALM : SAME);
}
else {
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, (hec_t ? tel : hed), los_o, c3n);
u3_noun lit = u3t(hec_t ? hed : tel);
switch ( lit ) {
case 0:
2018-04-12 02:30:57 +03:00
++tot_w; _n_emit(ops, SAM0);
break;
case 1:
2018-04-12 02:30:57 +03:00
++tot_w; _n_emit(ops, SAM1);
break;
default:
2018-04-12 02:30:57 +03:00
// overflows to SANS
op_y = lit <= 0xFF ? SAMB : lit <= 0xFFFF ? SAMS : SANB;
++tot_w; _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: {
2018-03-12 20:45:04 +03:00
u3_noun mid,
yep = u3_nul,
nop = u3_nul;
2018-04-12 02:30:57 +03:00
c3_w yep_w, nop_w;
2018-01-31 03:27:04 +03:00
u3x_trel(arg, &hed, &mid, &tel);
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, hed, c3n, c3n);
yep_w = _n_comp(&yep, mid, los_o, tel_o);
nop_w = _n_comp(&nop, tel, los_o, tel_o);
// SBIP and SBIN get sized during assembly
++yep_w; _n_emit(&yep, u3nc(SBIP, nop_w));
++tot_w; _n_emit(ops, u3nc(SBIN, yep_w));
tot_w += yep_w; _n_apen(ops, yep);
tot_w += nop_w; _n_apen(ops, nop);
2018-01-31 03:27:04 +03:00
break;
}
2018-03-12 20:45:04 +03:00
case 7:
2018-01-31 03:27:04 +03:00
u3x_cell(arg, &hed, &tel);
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, hed, los_o, c3n);
tot_w += _n_comp(ops, tel, c3y, tel_o);
2018-01-31 03:27:04 +03:00
break;
2018-03-12 20:45:04 +03:00
case 8:
u3x_cell(arg, &hed, &tel);
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, hed, c3n, c3n);
++tot_w; _n_emit(ops, (c3y == los_o) ? SNOL : SNOC);
tot_w += _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-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, mac, los_o, tel_o);
2018-01-31 03:27:04 +03:00
u3z(mac);
}
else {
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, tel, (c3y == tel_o ? c3y : los_o), c3n);
op_y = (c3y == tel_o) ? TICB : KICB; // overflows to TICS/KICS
++tot_w; _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);
2018-04-12 02:30:57 +03:00
tot_w += _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);
2018-04-12 02:30:57 +03:00
tot_w += _n_comp(ops, hed, c3n, c3n);
++tot_w; _n_emit(ops, SWAP);
tot_w += _n_comp(ops, tel, c3n, c3n);
++tot_w; _n_emit(ops, (c3y == los_o) ? WILS : WISH);
2018-01-31 03:27:04 +03:00
break;
2018-06-10 20:39:36 +03:00
case 12: {
u3_noun axe, nef;
u3x_cell(arg, &hed, &tel);
u3x_cell(hed, &axe, &nef);
tot_w += _n_comp(ops, tel, c3n, c3n);
++tot_w; _n_emit(ops, SWAP);
tot_w += _n_comp(ops, nef, c3n, c3n);
++tot_w;
switch ( axe ) {
case 2:
_n_emit(ops, (c3y == los_o) ? MUTH : KUTH);
break;
case 3:
_n_emit(ops, (c3y == los_o) ? MUTT : KUTT);
break;
case u3x_sam:
_n_emit(ops, (c3y == los_o) ? MUSM : KUSM);
break;
default:
op_y = (c3y == los_o)
2018-06-11 00:02:07 +03:00
? (axe <= 0xFF) ? MUTB : (axe <= 0xFFFF) ? MUTS : MITB // overflows to MITS
2018-06-10 20:39:36 +03:00
: (axe <= 0xFF) ? KUTB : (axe <= 0xFFFF) ? KUTS : KITB; // overflows to KITS
2018-06-11 00:02:07 +03:00
_n_emit(ops, u3nc(op_y, u3k(axe)));
2018-06-10 20:39:36 +03:00
break;
}
break;
}
default:
u3m_bail(c3__exit);
return 0;
2018-01-31 03:27:04 +03:00
}
2018-04-12 02:30:57 +03:00
return tot_w;
2018-01-31 03:27:04 +03:00
}
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);
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);
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
2018-04-12 21:31:42 +03:00
_n_resh(c3_y* buf, c3_w* ip_w)
2018-01-31 03:27:04 +03:00
{
2018-04-12 21:31:42 +03:00
c3_y les = buf[(*ip_w)++];
c3_y mos = buf[(*ip_w)++];
2018-01-31 03:27:04 +03:00
return les | (mos << 8);
}
2018-04-13 03:14:40 +03:00
/* _n_rewo(): read a c3_w from the bytecode stream.
2018-02-01 20:04:47 +03:00
*/
2018-04-13 03:14:40 +03:00
static inline c3_w
_n_rewo(c3_y* buf, c3_w* ip_w)
2018-01-31 03:27:04 +03:00
{
2018-04-12 02:30:57 +03:00
c3_y one = buf[(*ip_w)++],
two = buf[(*ip_w)++],
tre = buf[(*ip_w)++],
qua = buf[(*ip_w)++];
2018-01-31 03:27:04 +03:00
return one | (two << 8) | (tre << 16) | (qua << 24);
}
2018-04-17 02:54:20 +03:00
#ifdef VERBOSE_BYTECODE
2018-04-13 03:14:40 +03:00
/* _n_print_byc(): print bytecode. used for debugging.
*/
static void
_n_print_byc(c3_y* pog, c3_w her_w)
{
c3_w ip_w = 0;
if ( her_w == 0 ) {
fprintf(stderr, "begin: {");
}
else {
fprintf(stderr, "resume: {");
}
int first = 1;
while ( pog[ip_w] ) {
if ( first ) {
first = 0;
}
else if (ip_w == her_w) {
fprintf(stderr, " [*]");
}
else {
fprintf(stderr, " ");
}
switch ( _n_arg(pog[ip_w]) ) {
case 0:
fprintf(stderr, "%s", opcode_names[pog[ip_w++]]);
break;
case 1:
fprintf(stderr, "[%s ", opcode_names[pog[ip_w++]]);
fprintf(stderr, "%u]", pog[ip_w++]);
break;
case 2:
fprintf(stderr, "[%s ", opcode_names[pog[ip_w++]]);
fprintf(stderr, "%u]", _n_resh(pog, &ip_w));
break;
case 4:
fprintf(stderr, "[%s", opcode_names[pog[ip_w++]]);
fprintf(stderr, "%u]", _n_rewo(pog, &ip_w));
break;
default:
c3_assert(0);
break;
}
}
fprintf(stderr, " halt}\r\n");
}
#endif
2018-04-30 21:39:33 +03:00
/* _n_bite(): compile a nock formula to bytecode. RETAIN.
2018-02-01 20:04:47 +03:00
*/
static inline u3n_prog*
2018-04-12 02:30:57 +03:00
_n_bite(u3_noun fol) {
u3_noun ops = u3_nul;
_n_comp(&ops, fol, c3y, c3y);
2018-04-12 21:31:42 +03:00
return _n_prog_from_ops(ops);
2018-01-31 03:27:04 +03:00
}
/* _n_find(): return prog for given formula with prefix (u3_nul for none).
* RETAIN.
2018-02-01 20:04:47 +03:00
*/
static u3n_prog*
_n_find(u3_noun pre, u3_noun fol)
2018-01-31 03:27:04 +03:00
{
u3_noun key = u3nc(u3k(pre), u3k(fol));
2018-05-26 04:17:19 +03:00
u3_weak pog = u3h_git(u3R->byc.har_p, key);
2018-04-12 02:30:57 +03:00
if ( u3_none != pog ) {
2018-05-26 04:17:19 +03:00
u3z(key);
return u3to(u3n_prog, pog);
2018-04-03 00:08:28 +03:00
}
2018-04-13 03:14:40 +03:00
else if ( u3R != &u3H->rod_u ) {
u3a_road* rod_u = u3R;
while ( rod_u->par_p ) {
rod_u = u3to(u3a_road, rod_u->par_p);
2018-05-26 04:17:19 +03:00
pog = u3h_git(rod_u->byc.har_p, key);
2018-04-12 02:30:57 +03:00
if ( u3_none != pog ) {
2018-04-15 00:57:12 +03:00
c3_w i_w;
u3n_prog* old = _n_prog_old(u3to(u3n_prog, pog));
2018-04-15 00:57:12 +03:00
for ( i_w = 0; i_w < old->reg_u.len_w; ++i_w ) {
u3j_rite* rit_u = &(old->reg_u.rit_u[i_w]);
2018-04-15 00:57:12 +03:00
rit_u->own_o = c3n;
}
2018-04-17 02:54:20 +03:00
for ( i_w = 0; i_w < old->cal_u.len_w; ++i_w ) {
u3j_site* sit_u = &(old->cal_u.sit_u[i_w]);
2018-04-17 02:54:20 +03:00
sit_u->bat = u3_none;
2018-04-30 20:16:47 +03:00
sit_u->pog_p = 0;
sit_u->fon_o = c3n;
2018-04-17 02:54:20 +03:00
}
2018-05-26 04:17:19 +03:00
u3h_put(u3R->byc.har_p, key, u3a_outa(old));
u3z(key);
2018-04-12 02:30:57 +03:00
return old;
}
}
2018-01-31 03:27:04 +03:00
}
2018-04-13 03:14:40 +03:00
{
u3n_prog* gop = _n_bite(fol);
2018-05-26 04:17:19 +03:00
u3h_put(u3R->byc.har_p, key, u3a_outa(gop));
u3z(key);
2018-04-13 03:14:40 +03:00
return gop;
}
2018-01-31 03:27:04 +03:00
}
2018-05-26 04:17:19 +03:00
/* u3n_find(): return prog for given formula,
* split by key (u3_nul for no key). RETAIN.
*/
2018-04-30 21:02:48 +03:00
u3p(u3n_prog)
u3n_find(u3_noun key, u3_noun fol)
2018-04-17 02:54:20 +03:00
{
2018-04-30 21:02:48 +03:00
u3p(u3n_prog) pog_p;
2018-04-17 02:54:20 +03:00
u3t_on(noc_o);
2018-05-26 04:17:19 +03:00
pog_p = u3of(u3n_prog, _n_find(key, fol));
u3t_off(noc_o);
2018-04-30 21:02:48 +03:00
return pog_p;
2018-04-17 02:54:20 +03:00
}
/* _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;
}
2018-04-30 21:39:33 +03:00
/* _n_kick(): stop tracing noc and kick a u3j_site.
*/
static u3_weak
_n_kick(u3_noun cor, u3j_site* sit_u)
{
u3_weak pro;
u3t_off(noc_o);
pro = u3j_site_kick(cor, sit_u);
2018-04-27 22:22:39 +03:00
u3t_on(noc_o);
return pro;
}
/* _n_kale(): bail(exit) if not cell
2018-03-12 20:45:04 +03:00
*/
static inline u3_noun
_n_kale(u3_noun a)
{
if ( c3n == u3du(a) ) {
u3m_bail(c3__exit);
}
return a;
}
2018-06-10 20:39:36 +03:00
/* _n_edit(): PRODUCE a modified big with its axe axis
* changed to som. axe is RETAINED.
*/
static u3_noun
_n_edit(u3_noun big, u3_noun axe, u3_noun som)
{
if ( c3y == u3a_is_cat(axe) ) {
return u3i_molt(big, axe, som, 0);
}
else {
u3_noun mor = u3qc_mas(axe);
u3_noun pro = ( 2 == u3qc_cap(axe) )
? u3nc(_n_edit(u3k(u3h(big)), mor, som), u3k(u3t(big)))
: u3nc(u3k(u3h(big)), _n_edit(u3k(u3t(big)), mor, som));
u3z(mor);
2018-06-12 20:23:47 +03:00
u3z(big);
2018-06-10 20:39:36 +03:00
return pro;
}
}
2018-02-06 02:26:17 +03:00
typedef struct {
u3n_prog* pog_u;
2018-04-12 02:30:57 +03:00
c3_w ip_w;
2018-02-06 02:26:17 +03:00
} burnframe;
/* _n_burn(): pog: program
* bus: subject (TRANSFER)
* mov: -1 north, 1 south
* off: 0 north, -1 south
*/
2018-02-06 02:26:17 +03:00
static u3_noun
_n_burn(u3n_prog* pog_u, 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,
2018-04-12 02:30:57 +03:00
&&do_auto, &&do_ault, &&do_snoc, &&do_snol,
&&do_head, &&do_held, &&do_tail, &&do_tall,
&&do_fabk, &&do_fask, &&do_fibk, &&do_fisk,
&&do_fabl, &&do_fasl, &&do_fibl, &&do_fisl,
&&do_lit0, &&do_lit1, &&do_litb, &&do_lits,
&&do_libk, &&do_lisk,
&&do_lil0, &&do_lil1, &&do_lilb, &&do_lils,
&&do_libl, &&do_lisl,
&&do_nolk, &&do_noct, &&do_nock,
2018-02-13 02:19:11 +03:00
&&do_deep, &&do_bump,
2018-04-12 02:30:57 +03:00
&&do_sam0, &&do_sam1, &&do_samb, &&do_sams,
&&do_sanb, &&do_sans,
&&do_same, &&do_salm, &&do_samc,
2018-04-12 02:30:57 +03:00
&&do_sbip, &&do_sips, &&do_swip,
&&do_sbin, &&do_sins, &&do_swin,
&&do_kicb, &&do_kics, &&do_ticb, &&do_tics,
2018-02-13 02:19:11 +03:00
&&do_wils, &&do_wish,
2018-04-12 02:30:57 +03:00
&&do_bush, &&do_sush,
&&do_drop, &&do_heck, &&do_slog,
&&do_bast, &&do_sast,
&&do_balt, &&do_salt,
&&do_skib, &&do_skis, &&do_slib, &&do_slis,
&&do_save,
2018-06-10 20:39:36 +03:00
&&do_muth, &&do_kuth, &&do_mutt, &&do_kutt,
&&do_musm, &&do_kusm,
2018-06-10 20:39:36 +03:00
&&do_mutb, &&do_muts, &&do_mitb, &&do_mits,
&&do_kutb, &&do_kuts, &&do_kitb, &&do_kits,
2018-01-31 03:27:04 +03:00
};
u3j_site* sit_u;
u3j_rite* rit_u;
u3n_memo* mem_u;
2018-04-12 21:31:42 +03:00
c3_y *pog = pog_u->byc_u.ops_y;
2018-04-12 02:30:57 +03:00
c3_w sip_w, ip_w = 0;
2018-01-31 03:27:04 +03:00
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 U3_CPU_DEBUG
u3R->pro.nox_d += 1;
#endif
2018-03-12 20:45:04 +03:00
#ifdef VERBOSE_BYTECODE
2018-04-12 02:30:57 +03:00
#define BURN() fprintf(stderr, "%s ", opcode_names[pog[ip_w]]); goto *lab[pog[ip_w++]]
#else
2018-04-12 02:30:57 +03:00
#define BURN() goto *lab[pog[ip_w++]]
#endif
2018-01-31 03:27:04 +03:00
BURN();
{
do_halt: // [product ...burnframes...]
2018-02-06 02:26:17 +03:00
x = _n_pep(mov, off);
2018-03-12 20:45:04 +03:00
#ifdef VERBOSE_BYTECODE
fprintf(stderr, "return\r\n");
#endif
2018-02-06 02:26:17 +03:00
if ( empty == u3R->cap_p ) {
return x;
}
else {
2018-04-12 02:30:57 +03:00
fam = u3to(burnframe, u3R->cap_p) + off;
pog_u = fam->pog_u;
2018-04-12 21:31:42 +03:00
pog = pog_u->byc_u.ops_y;
2018-04-12 02:30:57 +03:00
ip_w = fam->ip_w;
2018-02-06 02:26:17 +03:00
u3R->cap_p = u3of(burnframe, fam - (mov+off));
2018-02-06 02:26:17 +03:00
_n_push(mov, off, x);
2018-03-12 20:45:04 +03:00
#ifdef VERBOSE_BYTECODE
2018-04-12 02:30:57 +03:00
_n_print_byc(pog, ip_w);
#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]
2018-03-12 20:45:04 +03:00
top = _n_peek(off);
*top = u3nc(*top, x); // [pro]
2018-01-31 03:27:04 +03:00
BURN();
2018-04-12 02:30:57 +03:00
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);
BURN();
2018-01-31 03:27:04 +03:00
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-04-12 02:30:57 +03:00
do_fisk:
x = pog_u->lit_u.non[_n_resh(pog, &ip_w)];
2018-02-05 22:20:34 +03:00
goto frag_in;
2018-01-31 03:27:04 +03:00
2018-04-12 02:30:57 +03:00
do_fibk:
x = pog_u->lit_u.non[pog[ip_w++]];
2018-02-05 22:20:34 +03:00
goto frag_in;
2018-02-05 21:27:39 +03:00
2018-04-12 02:30:57 +03:00
do_fask:
x = _n_resh(pog, &ip_w);
goto frag_in;
do_fabk:
x = pog[ip_w++];
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();
2018-04-12 02:30:57 +03:00
do_fisl:
x = pog_u->lit_u.non[_n_resh(pog, &ip_w)];
goto flag_in;
do_fibl:
x = pog_u->lit_u.non[pog[ip_w++]];
goto flag_in;
2018-04-12 02:30:57 +03:00
do_fasl:
x = _n_resh(pog, &ip_w);
goto flag_in;
2018-04-12 02:30:57 +03:00
do_fabl:
x = pog[ip_w++];
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:
2018-04-12 02:30:57 +03:00
_n_push(mov, off, pog[ip_w++]);
BURN();
do_lits:
2018-04-12 02:30:57 +03:00
_n_push(mov, off, _n_resh(pog, &ip_w));
BURN();
do_libk:
_n_push(mov, off, u3k(pog_u->lit_u.non[pog[ip_w++]]));
BURN();
2018-04-12 02:30:57 +03:00
do_lisk:
_n_push(mov, off, u3k(pog_u->lit_u.non[_n_resh(pog, &ip_w)]));
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:
2018-04-12 02:30:57 +03:00
x = pog[ip_w++];
goto lil_in;
do_lils:
2018-04-12 02:30:57 +03:00
x = _n_resh(pog, &ip_w);
goto lil_in;
do_libl:
x = u3k(pog_u->lit_u.non[pog[ip_w++]]);
goto lil_in;
2018-01-31 03:27:04 +03:00
2018-04-12 02:30:57 +03:00
do_lisl:
x = u3k(pog_u->lit_u.non[_n_resh(pog, &ip_w)]);
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-04-12 02:30:57 +03:00
fam->ip_w = ip_w;
fam->pog_u = pog_u;
2018-02-06 02:26:17 +03:00
_n_push(mov, off, x);
nock_out:
pog_u = _n_find(u3_nul, o);
2018-04-12 21:31:42 +03:00
pog = pog_u->byc_u.ops_y;
2018-04-12 02:30:57 +03:00
ip_w = 0;
#ifdef U3_CPU_DEBUG
u3R->pro.nox_d += 1;
#endif
2018-03-12 20:45:04 +03:00
#ifdef VERBOSE_BYTECODE
fprintf(stderr, "\r\nnock jump: %u\r\n", o);
2018-04-12 02:30:57 +03:00
_n_print_byc(pog, ip_w);
#endif
u3z(o);
2018-01-31 03:27:04 +03:00
BURN();
2018-03-12 20:45:04 +03:00
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-23 01:21:33 +03:00
do_sam0:
top = _n_peek(off);
if ( *top == 0 ) {
*top = c3y;
}
else {
u3z(*top);
*top = c3n;
}
BURN();
2018-02-14 20:18:53 +03:00
do_sam1:
2018-02-23 01:21:33 +03:00
top = _n_peek(off);
if ( *top == 1 ) {
*top = c3y;
}
else {
u3z(*top);
*top = c3n;
}
BURN();
2018-02-14 20:18:53 +03:00
do_samb:
2018-02-23 01:21:33 +03:00
top = _n_peek(off);
2018-04-12 02:30:57 +03:00
if ( *top == pog[ip_w++] ) {
2018-02-23 01:21:33 +03:00
*top = c3y;
}
else {
u3z(*top);
*top = c3n;
}
BURN();
2018-02-14 20:18:53 +03:00
do_sams:
2018-02-23 01:21:33 +03:00
top = _n_peek(off);
2018-04-12 02:30:57 +03:00
if ( *top == _n_resh(pog, &ip_w) ) {
2018-02-23 01:21:33 +03:00
*top = c3y;
}
else {
u3z(*top);
*top = c3n;
}
BURN();
2018-02-14 20:18:53 +03:00
2018-04-12 02:30:57 +03:00
do_sans:
x = pog_u->lit_u.non[_n_resh(pog, &ip_w)];
goto samn_in;
do_sanb:
x = pog_u->lit_u.non[pog[ip_w++]];
samn_in:
2018-02-14 20:18:53 +03:00
top = _n_peek(off);
o = *top;
2018-04-12 02:30:57 +03:00
*top = u3r_sing(o, x);
2018-02-23 01:21:33 +03:00
u3z(o);
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();
2018-04-12 02:30:57 +03:00
do_sbip:
sip_w = pog[ip_w++];
ip_w += sip_w;
BURN();
2018-04-12 02:30:57 +03:00
do_sips:
sip_w = _n_resh(pog, &ip_w);
ip_w += sip_w;
BURN();
do_swip:
2018-04-13 03:14:40 +03:00
sip_w = _n_rewo(pog, &ip_w);
2018-04-12 02:30:57 +03:00
ip_w += sip_w;
BURN();
2018-04-12 02:30:57 +03:00
do_swin:
2018-04-13 03:14:40 +03:00
sip_w = _n_rewo(pog, &ip_w);
2018-04-12 02:30:57 +03:00
goto skin_in;
do_sins:
sip_w = _n_resh(pog, &ip_w);
goto skin_in;
do_sbin:
2018-04-12 02:30:57 +03:00
sip_w = pog[ip_w++];
skin_in:
2018-04-12 02:30:57 +03:00
x = _n_pep(mov, off);
if ( c3n == x ) {
2018-04-12 02:30:57 +03:00
ip_w += sip_w;
}
else if ( c3y != x ) {
u3m_bail(c3__exit);
return u3_none;
}
BURN();
2018-02-05 22:20:34 +03:00
do_kics:
2018-04-12 02:30:57 +03:00
x = _n_resh(pog, &ip_w);
2018-02-05 22:20:34 +03:00
goto kick_in;
do_kicb:
2018-04-12 02:30:57 +03:00
x = pog[ip_w++];
2018-02-05 22:20:34 +03:00
kick_in:
2018-04-12 02:30:57 +03:00
sit_u = &(pog_u->cal_u.sit_u[x]);
top = _n_peek(off);
o = *top;
2018-04-17 02:54:20 +03:00
*top = _n_kick(o, sit_u);
2018-01-31 05:14:37 +03:00
if ( u3_none == *top ) {
2018-02-13 02:19:11 +03:00
_n_toss(mov, off);
2018-02-06 02:26:17 +03:00
2018-04-12 02:30:57 +03:00
fam = u3to(burnframe, u3R->cap_p) + off + mov;
u3R->cap_p = u3of(burnframe, fam - off);
fam->ip_w = ip_w;
2018-04-12 21:31:42 +03:00
fam->pog_u = pog_u;
2018-02-06 02:26:17 +03:00
2018-04-30 20:16:47 +03:00
pog_u = u3to(u3n_prog, sit_u->pog_p);
2018-04-12 21:31:42 +03:00
pog = pog_u->byc_u.ops_y;
ip_w = 0;
#ifdef U3_CPU_DEBUG
u3R->pro.nox_d += 1;
#endif
2018-03-12 20:45:04 +03:00
#ifdef VERBOSE_BYTECODE
2018-04-17 02:54:20 +03:00
fprintf(stderr, "\r\nhead kick jump: %u, sp: %p\r\n", u3r_at(sit_u->axe, cor), top);
2018-04-12 02:30:57 +03:00
_n_print_byc(pog, ip_w);
#endif
2018-02-06 02:26:17 +03:00
_n_push(mov, off, o);
2018-01-31 05:14:37 +03:00
}
2018-03-12 20:45:04 +03:00
#ifdef VERBOSE_BYTECODE
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:
2018-04-12 02:30:57 +03:00
x = _n_resh(pog, &ip_w);
2018-02-05 22:20:34 +03:00
goto tick_in;
do_ticb:
2018-04-12 02:30:57 +03:00
x = pog[ip_w++];
2018-02-05 22:20:34 +03:00
tick_in:
2018-04-12 02:30:57 +03:00
sit_u = &(pog_u->cal_u.sit_u[x]);
top = _n_peek(off);
o = *top;
2018-04-17 02:54:20 +03:00
*top = _n_kick(o, sit_u);
2018-01-31 05:14:37 +03:00
if ( u3_none == *top ) {
2018-04-12 02:30:57 +03:00
*top = o;
2018-04-30 20:16:47 +03:00
pog_u = u3to(u3n_prog, sit_u->pog_p);
2018-04-12 21:31:42 +03:00
pog = pog_u->byc_u.ops_y;
2018-04-12 02:30:57 +03:00
ip_w = 0;
#ifdef U3_CPU_DEBUG
u3R->pro.nox_d += 1;
#endif
2018-03-12 20:45:04 +03:00
#ifdef VERBOSE_BYTECODE
2018-04-17 02:54:20 +03:00
fprintf(stderr, "\r\ntail kick jump: %u, sp: %p\r\n", u3x_at(sit_u->axe, o);, top);
2018-04-12 02:30:57 +03:00
_n_print_byc(pog, ip_w);
#endif
}
2018-03-12 20:45:04 +03:00
#ifdef VERBOSE_BYTECODE
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();
}
2018-04-12 02:30:57 +03:00
do_sush:
x = _n_resh(pog, &ip_w);
goto cush_in;
do_bush:
x = pog[ip_w++];
cush_in:
x = u3k(pog_u->lit_u.non[x]);
o = _n_pep(mov, off);
2018-04-12 02:30:57 +03:00
u3t_push(u3nc(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
2018-04-12 02:30:57 +03:00
do_sast:
x = _n_resh(pog, &ip_w);
goto fast_in;
do_bast:
x = pog[ip_w++];
goto fast_in;
do_salt:
x = _n_resh(pog, &ip_w);
goto falt_in;
do_balt:
x = pog[ip_w++];
falt_in: // [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);
2018-04-12 02:30:57 +03:00
goto fast_out;
2018-04-12 02:30:57 +03:00
fast_in: // [pro bus clu]
o = _n_pep(mov, off); // [bus clu]
top = _n_swap(mov, off); // [clu bus]
2018-04-12 02:30:57 +03:00
fast_out:
rit_u = &(pog_u->reg_u.rit_u[x]);
u3t_off(noc_o);
u3j_rite_mine(rit_u, *top, u3k(o));
u3t_on(noc_o);
*top = o;
BURN();
2018-04-12 02:30:57 +03:00
do_skis:
x = _n_resh(pog, &ip_w);
goto skim_in;
do_skib:
2018-04-12 02:30:57 +03:00
x = pog[ip_w++];
skim_in:
2018-04-12 21:31:42 +03:00
mem_u = &(pog_u->mem_u.sot_u[x]);
2018-04-12 02:30:57 +03:00
top = _n_peek(off);
x = u3k(*top);
goto skim_out;
2018-04-12 02:30:57 +03:00
do_slis:
x = _n_resh(pog, &ip_w);
goto slim_in;
do_slib:
2018-04-12 02:30:57 +03:00
x = pog[ip_w++];
slim_in:
2018-04-12 21:31:42 +03:00
mem_u = &(pog_u->mem_u.sot_u[x]);
2018-04-12 02:30:57 +03:00
x = _n_pep(mov, off);
skim_out:
2018-04-12 02:30:57 +03:00
o = u3k(mem_u->key);
x = u3nc(x, 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 {
2018-04-12 21:31:42 +03:00
ip_w += mem_u->sip_l;
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-06-10 20:39:36 +03:00
do_kuth:
x = _n_pep(mov, off);
top = _n_swap(mov, off);
goto muth_in;
do_muth:
x = _n_pep(mov, off);
_n_toss(mov, off);
top = _n_peek(off);
muth_in:
o = *top;
*top = u3nc(x, u3k(u3t(o)));
u3z(o);
BURN();
do_kutt:
x = _n_pep(mov, off);
top = _n_swap(mov, off);
goto mutt_in;
do_mutt:
x = _n_pep(mov, off);
_n_toss(mov, off);
top = _n_peek(off);
mutt_in:
o = *top;
*top = u3nc(u3k(u3h(o)), x);
u3z(o);
BURN();
do_kusm:
x = _n_pep(mov, off);
top = _n_swap(mov, off);
goto musm_in;
do_musm:
x = _n_pep(mov, off);
_n_toss(mov, off);
top = _n_peek(off);
musm_in:
o = *top;
*top = u3nt(u3k(u3h(o)), x, u3k(u3t(u3t(o))));
u3z(o);
BURN();
do_kitb:
x = pog_u->lit_u.non[pog[ip_w++]];
goto kut_in;
do_kits:
x = pog_u->lit_u.non[_n_resh(pog, &ip_w)];
goto kut_in;
do_kuts:
x = _n_resh(pog, &ip_w);
goto kut_in;
do_kutb:
x = pog[ip_w++];
kut_in:
o = _n_pep(mov, off);
top = _n_swap(mov, off);
goto edit_in;
do_mitb:
x = pog_u->lit_u.non[pog[ip_w++]];
goto mut_in;
do_mits:
x = pog_u->lit_u.non[_n_resh(pog, &ip_w)];
goto mut_in;
do_muts:
x = _n_resh(pog, &ip_w);
goto mut_in;
do_mutb:
x = pog[ip_w++];
mut_in:
o = _n_pep(mov, off);
_n_toss(mov, off);
top = _n_peek(off);
edit_in:
*top = _n_edit(*top, x, o);
BURN();
2018-01-31 03:27:04 +03:00
}
}
2018-02-01 20:04:47 +03:00
2018-04-30 21:39:33 +03:00
/* _n_burn_out(): execute u3n_prog with bus as subject.
*/
2018-02-01 20:04:47 +03:00
static u3_noun
_n_burn_out(u3_noun bus, u3n_prog* pog_u)
2018-02-01 20:04:47 +03:00
{
c3_ys mov, off;
if ( c3y == u3a_is_north(u3R) ) {
mov = -1;
off = 0;
}
else {
mov = 1;
off = -1;
}
return _n_burn(pog_u, bus, mov, off);
}
/* u3n_burn(): execute u3n_prog with bus as subject.
*/
u3_noun
2018-04-30 20:56:19 +03:00
u3n_burn(u3p(u3n_prog) pog_p, u3_noun bus)
{
u3_noun pro;
u3t_on(noc_o);
2018-04-30 20:56:19 +03:00
pro = _n_burn_out(bus, u3to(u3n_prog, pog_p));
u3t_off(noc_o);
return pro;
}
/* _n_burn_on(): produce .*(bus fol) with bytecode interpreter
*/
static u3_noun
_n_burn_on(u3_noun bus, u3_noun fol)
{
u3n_prog* pog_u = _n_find(u3_nul, fol);
u3z(fol);
return _n_burn_out(bus, pog_u);
2018-02-01 20:04:47 +03:00
}
2018-03-12 20:45:04 +03:00
/* u3n_nock_on(): produce .*(bus fol). Do not virtualize.
*/
u3_noun
2018-03-12 20:45:04 +03:00
u3n_nock_on(u3_noun bus, u3_noun fol)
{
u3_noun pro;
2018-03-12 20:45:04 +03:00
u3t_on(noc_o);
2018-03-12 20:45:04 +03:00
#if 0
pro = _n_nock_on(bus, fol);
#else
pro = _n_burn_on(bus, fol);
2018-03-12 20:45:04 +03:00
#endif
u3t_off(noc_o);
2018-03-12 20:45:04 +03:00
return pro;
}
2018-02-05 21:27:39 +03:00
2018-04-30 21:39:33 +03:00
/* _n_prog_take_dat(): copy references from src u3n_prog. overwritten
* values in dst u3n_prog are lost/freed only if
* los_o is yes.
*/
static void
_n_prog_take_dat(u3n_prog* dst_u, u3n_prog* src_u, c3_o los_o)
{
c3_w i_w;
for ( i_w = 0; i_w < src_u->lit_u.len_w; ++i_w ) {
u3_noun* dst = &(dst_u->lit_u.non[i_w]);
u3_noun* src = &(src_u->lit_u.non[i_w]);
u3_noun old = *dst;
*dst = u3a_take(*src);
if ( c3y == los_o ) {
u3z(old);
}
}
for ( i_w = 0; i_w < src_u->mem_u.len_w; ++i_w ) {
u3n_memo* dst = &(dst_u->mem_u.sot_u[i_w]);
u3n_memo* src = &(src_u->mem_u.sot_u[i_w]);
u3_noun old = dst->key;
dst->sip_l = src->sip_l;
dst->key = u3a_take(src->key);
if ( c3y == los_o ) {
u3z(old);
}
}
for ( i_w = 0; i_w < src_u->cal_u.len_w; ++i_w ) {
u3j_site_copy(&(dst_u->cal_u.sit_u[i_w]),
&(src_u->cal_u.sit_u[i_w]), los_o);
}
2018-04-15 00:57:12 +03:00
for ( i_w = 0; i_w < src_u->reg_u.len_w; ++i_w ) {
u3j_rite_copy(&(dst_u->reg_u.rit_u[i_w]),
&(src_u->reg_u.rit_u[i_w]), los_o);
}
}
2018-04-12 21:31:42 +03:00
/* _n_prog_take(): copy program from a junior road
*/
static u3n_prog*
_n_prog_take(u3n_prog* pog_u)
{
u3n_prog* gop_u;
2018-04-12 21:31:42 +03:00
if ( c3y == pog_u->byc_u.own_o ) {
gop_u = _n_prog_new(pog_u->byc_u.len_w,
pog_u->cal_u.len_w, pog_u->reg_u.len_w,
pog_u->lit_u.len_w, pog_u->mem_u.len_w);
memcpy(gop_u->byc_u.ops_y, pog_u->byc_u.ops_y, pog_u->byc_u.len_w);
}
else {
gop_u = _n_prog_old(pog_u);
}
_n_prog_take_dat(gop_u, pog_u, c3n);
2018-04-12 21:31:42 +03:00
return gop_u;
}
2018-04-17 02:54:20 +03:00
2018-04-12 21:31:42 +03:00
/* _n_prog_free(): free memory retained by program
*/
static void
_n_prog_free(u3n_prog* pog_u)
2018-02-20 22:28:01 +03:00
{
2018-04-12 21:31:42 +03:00
c3_w i_w;
2018-02-20 22:28:01 +03:00
2018-04-12 21:31:42 +03:00
for ( i_w = 0; i_w < pog_u->lit_u.len_w; ++i_w ) {
u3z(pog_u->lit_u.non[i_w]);
2018-02-20 22:28:01 +03:00
}
2018-04-12 21:31:42 +03:00
for ( i_w = 0; i_w < pog_u->mem_u.len_w; ++i_w ) {
u3z(pog_u->mem_u.sot_u[i_w].key);
}
2018-02-20 22:28:01 +03:00
2018-04-12 21:31:42 +03:00
for ( i_w = 0; i_w < pog_u->cal_u.len_w; ++i_w ) {
u3j_site_lose(&(pog_u->cal_u.sit_u[i_w]));
2018-04-12 21:31:42 +03:00
}
2018-02-20 22:28:01 +03:00
2018-04-12 21:31:42 +03:00
for ( i_w = 0; i_w < pog_u->reg_u.len_w; ++i_w ) {
u3j_rite_lose(&(pog_u->reg_u.rit_u[i_w]));
2018-02-20 22:28:01 +03:00
}
2018-04-12 21:31:42 +03:00
u3a_free(pog_u);
2018-02-20 22:28:01 +03:00
}
/* _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 23:13:14 +03:00
u3_noun lof = u3a_take(fol);
2018-04-17 02:54:20 +03:00
u3_noun tog;
2018-04-03 00:08:28 +03:00
u3_weak con = u3h_get(u3R->byc.har_p, lof);
u3n_prog* pog_u = u3to(u3n_prog, got);
2018-04-15 00:57:12 +03:00
if ( u3_none == con ) {
2018-04-17 02:54:20 +03:00
tog = u3a_outa(_n_prog_take(pog_u));
u3h_put(u3R->byc.har_p, lof, tog);
}
else {
u3n_prog* sep_u = u3to(u3n_prog, con);
2018-04-15 00:57:12 +03:00
_n_prog_take_dat(sep_u, pog_u, c3y);
2018-04-17 02:54:20 +03:00
tog = u3a_outa(sep_u);
2018-04-03 00:08:28 +03:00
}
2018-02-20 23:13:14 +03:00
u3z(lof);
}
2018-04-30 21:39:33 +03:00
/* u3n_reap(): promote bytecode state.
2018-02-05 21:27:39 +03:00
*/
void
2018-04-30 21:39:33 +03:00
u3n_reap(u3p(u3h_root) har_p)
2018-02-05 21:27:39 +03:00
{
u3h_walk(har_p, _n_reap);
2018-04-17 02:54:20 +03:00
}
2018-04-30 21:39:33 +03:00
/* _n_ream(): ream program call sites
*/
2018-04-18 22:19:56 +03:00
void
_n_ream(u3_noun kev)
{
c3_w i_w;
u3n_prog* pog_u = u3to(u3n_prog, u3t(kev));
2018-04-18 22:19:56 +03:00
for ( i_w = 0; i_w < pog_u->cal_u.len_w; ++i_w ) {
2018-04-27 02:36:29 +03:00
u3j_site_ream(&(pog_u->cal_u.sit_u[i_w]));
2018-04-18 22:19:56 +03:00
}
}
/* u3n_ream(): refresh after restoring from checkpoint.
*/
void
u3n_ream()
{
c3_assert(u3R == &(u3H->rod_u));
u3h_walk(u3R->byc.har_p, _n_ream);
}
2018-04-12 21:31:42 +03:00
/* _n_prog_mark(): mark program for gc.
2018-03-12 20:45:04 +03:00
*/
static c3_w
_n_prog_mark(u3n_prog* pog_u)
{
2018-04-12 21:31:42 +03:00
c3_w i_w, tot_w = u3a_mark_mptr(pog_u);
2018-04-12 21:31:42 +03:00
for ( i_w = 0; i_w < pog_u->lit_u.len_w; ++i_w ) {
tot_w += u3a_mark_noun(pog_u->lit_u.non[i_w]);
}
2018-04-12 21:31:42 +03:00
for ( i_w = 0; i_w < pog_u->mem_u.len_w; ++i_w ) {
tot_w += u3a_mark_noun(pog_u->mem_u.sot_u[i_w].key);
}
2018-04-12 21:31:42 +03:00
for ( i_w = 0; i_w < pog_u->cal_u.len_w; ++i_w ) {
2018-04-26 21:23:03 +03:00
tot_w += u3j_site_mark(&(pog_u->cal_u.sit_u[i_w]));
2018-04-12 21:31:42 +03:00
}
2018-04-12 21:31:42 +03:00
for ( i_w = 0; i_w < pog_u->reg_u.len_w; ++i_w ) {
2018-04-26 21:23:03 +03:00
tot_w += u3j_rite_mark(&(pog_u->reg_u.rit_u[i_w]));
}
return tot_w;
}
2018-04-30 21:39:33 +03:00
/* _n_bam(): u3h_walk_with helper for u3n_mark
*/
static void
2018-04-03 00:08:28 +03:00
_n_bam(u3_noun kev, void* dat)
{
2018-04-12 21:31:42 +03:00
c3_w* bam_w = dat;
u3n_prog* pog = u3to(u3n_prog, u3t(kev));
2018-04-12 21:31:42 +03:00
*bam_w += _n_prog_mark(pog);
}
2018-04-30 21:39:33 +03:00
/* u3n_mark(): mark the bytecode cache for gc.
*/
c3_w
2018-04-30 21:39:33 +03:00
u3n_mark()
{
2018-04-03 00:08:28 +03:00
c3_w bam_w = 0;
u3p(u3h_root) har_p = u3R->byc.har_p;
2018-04-03 00:08:28 +03:00
u3h_walk_with(har_p, _n_bam, &bam_w);
return bam_w + u3h_mark(har_p);
2018-03-12 20:58:41 +03:00
}
/* _n_feb(): u3h_walk helper for u3n_bree
*/
static void
_n_feb(u3_noun kev)
{
_n_prog_free(u3to(u3n_prog, u3t(kev)));
2018-03-12 20:58:41 +03:00
}
2018-04-30 21:39:33 +03:00
/* u3n_free(): free bytecode cache
2018-03-12 20:58:41 +03:00
*/
void
2018-04-30 21:39:33 +03:00
u3n_free()
2018-03-12 20:58:41 +03:00
{
u3p(u3h_root) har_p = u3R->byc.har_p;
u3h_walk(har_p, _n_feb);
u3h_free(har_p);
}
2018-03-12 20:45:04 +03:00
/* u3n_kick_on(): fire `gat` without changing the sample.
*/
u3_noun
u3n_kick_on(u3_noun gat)
{
return u3j_kink(gat, 2);
}
c3_w exc_w;
/* u3n_slam_on(): produce (gat sam).
*/
u3_noun
u3n_slam_on(u3_noun gat, u3_noun sam)
{
u3_noun cor = u3nc(u3k(u3h(gat)), u3nc(sam, u3k(u3t(u3t(gat)))));
#if 0
2018-03-12 20:45:04 +03:00
if ( &u3H->rod_u == u3R ) {
if ( exc_w == 1 ) {
c3_assert(0);
}
2018-03-12 20:45:04 +03:00
exc_w++;
}
#endif
2018-03-12 20:45:04 +03:00
u3z(gat);
return u3n_kick_on(cor);
}
/* 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);
}
/* 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);
}
/* u3n_nock_an(): as slam_in(), but with empty fly.
*/
u3_noun
u3n_nock_an(u3_noun bus, u3_noun fol)
{
u3_noun gul = u3nt(u3nt(1, 0, 0), 0, 0); // |=(a/{* *} ~)
return u3n_nock_et(gul, bus, fol);
}