Merge pull request #937 from keatondunsford/master-fix

Revert "Merge pull request #920 from frodwith/runtime-overflows"
This commit is contained in:
Ted Blackman 2018-03-01 16:40:14 -08:00 committed by GitHub
commit 3ed1eb53aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 275 additions and 305 deletions

View File

@ -291,22 +291,6 @@
void*
u3a_wealloc(void* lag_v, c3_w len_w);
/* u3a_push(): allocate space on the road stack
*/
void*
u3a_push(c3_w len_w);
/* u3a_pop(): deallocate space on the road stack
*/
void
u3a_pop(c3_w len_w);
/* u3a_peek(): examine the top of the road stack
*/
void*
u3a_peek(c3_w len_w);
/* C-style aligned allocation - *not* compatible with above.
*/
/* u3a_malloc(): aligned storage measured in bytes.

View File

@ -3,19 +3,32 @@
*/
#include "all.h"
/* functions
*/
static u3_noun
_jam_in(u3p(u3h_root) har_p, u3_atom, u3_atom, u3_noun);
static u3_noun
_jam_pair(u3_noun x, u3_noun d, u3_noun e)
_jam_in_pair(u3p(u3h_root) har_p,
u3_atom h_a,
u3_atom t_a,
u3_atom b,
u3_noun l)
{
u3_noun r, p_d, q_d, r_d;
u3x_trel(d, &p_d, &q_d, &r_d);
u3_noun w = u3nc(u3nc(2, 1), u3k(l));
u3_noun x = u3qa_add(2, b);
u3_noun d = _jam_in(har_p, h_a, x, w);
u3_noun p_d, q_d, r_d;
u3_noun r;
u3r_trel(d, &p_d, &q_d, &r_d);
{
u3_noun y = u3qa_add(x, p_d);
u3_noun e = _jam_in(har_p, t_a, y, q_d);
u3_noun p_e, q_e, r_e;
u3x_trel(e, &p_e, &q_e, &r_e);
u3r_trel(e, &p_e, &q_e, &r_e);
{
u3_noun z = u3qa_add(p_d, p_e);
@ -23,16 +36,20 @@
u3z(z);
}
u3z(e);
u3z(y);
}
u3z(x);
u3z(d);
u3z(e);
u3z(x);
u3z(w);
return r;
}
static u3_noun
_jam_flat(u3_atom a, u3_noun l)
_jam_in_flat(u3p(u3h_root) har_p,
u3_atom a,
u3_noun l)
{
u3_noun d = u3qe_mat(a);
u3_noun x = u3qa_add(1, u3h(d));
@ -45,7 +62,9 @@
}
static u3_noun
_jam_ptr(u3_atom u_c, u3_noun l)
_jam_in_ptr(u3p(u3h_root) har_p,
u3_atom u_c,
u3_noun l)
{
u3_noun d = u3qe_mat(u_c);
u3_atom x = u3qc_lsh(0, 2, u3t(d));
@ -59,97 +78,45 @@
return z;
}
typedef struct {
u3_noun a;
u3_noun b;
u3_noun l;
u3_noun* r;
u3_noun hed;
u3_noun tel;
} jamframe;
static inline void
_jam_push(u3_noun a, u3_noun b, u3_noun l, u3_noun *r)
static u3_noun
_jam_in(u3p(u3h_root) har_p,
u3_noun a,
u3_atom b,
u3_noun l)
{
jamframe* fam = u3a_push(sizeof(jamframe));
fam->a = a;
fam->b = b;
fam->l = l;
fam->r = r;
fam->hed = u3_none;
fam->tel = u3_none;
}
u3_noun c = u3h_get(har_p, a);
u3_noun x;
static inline void
_jam_pop()
{
u3a_pop(sizeof(jamframe));
}
if ( u3_none == c ) {
u3h_put(har_p, a, u3k(b));
static inline jamframe*
_jam_peek()
{
return (jamframe*) u3a_peek(sizeof(jamframe));
if ( c3y == u3ud(a) ) {
x = _jam_in_flat(har_p, a, l);
} else {
x = _jam_in_pair(har_p, u3h(a), u3t(a), b, l);
}
}
else {
if ( c3y == u3ud(a) && u3r_met(0, a) <= u3r_met(0, c) ) {
x = _jam_in_flat(har_p, a, l);
}
else {
x = _jam_in_ptr(har_p, c, l);
}
}
return x;
}
u3_noun
u3qe_jam(u3_atom a)
{
u3p(u3h_root) har_p = u3h_new();
u3p(jamframe) empty = u3R->cap_p;
jamframe* fam;
u3_noun out, c, x, q, r;
_jam_push(a, 0, u3_nul, &out);
while ( empty != u3R->cap_p ) {
fam = _jam_peek();
if ( u3_none != fam->tel ) {
u3_noun z = u3qa_add(2, fam->b);
x = _jam_pair(z, fam->hed, fam->tel);
}
else if ( u3_none != fam->hed ) {
u3_noun p_d, q_d, r_d;
u3x_trel(fam->hed, &p_d, &q_d, &r_d);
{
u3_noun z = u3qa_add(2, fam->b);
u3_noun y = u3qa_add(z, p_d);
_jam_push(u3t(fam->a), y, q_d, &(fam->tel));
u3z(z);
continue;
}
}
else {
a = fam->a;
c = u3h_get(har_p, a);
if ( u3_none != c ) {
if ( (c3y == u3ud(a)) && u3r_met(0, a) <= u3r_met(0, c) ) {
x = _jam_flat(a, fam->l);
}
else {
x = _jam_ptr(c, fam->l);
}
}
else {
u3h_put(har_p, a, u3k(fam->b));
if ( c3y == u3ud(a) ) {
x = _jam_flat(a, fam->l);
}
else {
u3_noun z = u3qa_add(2, fam->b);
u3_noun w = u3nc(u3nc(2, 1), u3k(fam->l));
_jam_push(u3h(a), z, w, &(fam->hed));
continue;
}
}
}
*(fam->r) = x;
u3z(fam->b);
_jam_pop();
}
u3_noun x = _jam_in(har_p, a, 0, u3_nul);
u3_noun q = u3qb_flop(u3h(u3t(x)));
u3_noun r = u3qc_can(0, q);
q = u3qb_flop(u3h(u3t(out)));
r = u3qc_can(0, q);
u3z(out);
u3z(x);
u3z(q);
u3h_free(har_p);
return r;

View File

@ -576,58 +576,6 @@ u3a_wealloc(void* lag_v, c3_w len_w)
}
}
}
/* u3a_push(): allocate space on the road stack
*/
void*
u3a_push(c3_w len_w)
{
void *cur, *top = u3to(void, u3R->cap_p);
if ( c3y == u3a_is_north(u3R) ) {
top -= len_w;
cur = top;
u3p(void) cap_p = u3R->cap_p = u3of(void, top);
c3_assert(cap_p < u3R->mat_p);
c3_assert(cap_p > u3R->hat_p);
return cur;
}
else {
cur = top;
top += len_w;
u3R->cap_p = u3of(void, top);
u3p(void) cap_p = u3R->cap_p = u3of(void, top);
c3_assert(cap_p > u3R->mat_p);
c3_assert(cap_p < u3R->hat_p);
return cur;
}
}
/* u3a_pop(): deallocate space on the road stack
*/
void
u3a_pop(c3_w len_w)
{
void* top = u3to(void, u3R->cap_p);
if ( c3y == u3a_is_north(u3R) ) {
top += len_w;
u3p(void) cap_p = u3R->cap_p = u3of(void, top);
c3_assert(cap_p <= u3R->mat_p);
c3_assert(cap_p > u3R->hat_p);
}
else {
top -= len_w;
u3p(void) cap_p = u3R->cap_p = u3of(void, top);
c3_assert(cap_p >= u3R->mat_p);
c3_assert(cap_p < u3R->hat_p);
}
}
/* u3a_peek(): examine the top of the road stack
*/
void*
u3a_peek(c3_w len_w)
{
return u3to(void, u3R->cap_p) - (c3y == u3a_is_north(u3R) ? 0 : len_w);
}
/* u3a_wfree(): free storage.
*/

View File

@ -502,39 +502,87 @@ _sang_one(u3_noun* a, u3_noun* b)
}
}
typedef struct {
u3_noun a;
u3_noun b;
c3_y sat_y;
} eqframe;
static inline eqframe*
_eq_peek()
{
return (eqframe*) u3a_peek(sizeof(eqframe));
}
static inline void
_eq_push(u3_noun a, u3_noun b)
{
eqframe* cur = (eqframe*) u3a_push(sizeof(eqframe));
cur->a = a;
cur->b = b;
cur->sat_y = 0;
}
static inline void
_eq_pop()
{
u3a_pop(sizeof(eqframe));
}
/* _sing_one(): do not pick a unified pointer for identical (a) and (b).
/* _sang_x(): yes if a and b are the same noun, unifying but leaking.
*/
static void
_sing_one(u3_noun* a, u3_noun* b)
static c3_o
_sang_x(u3_noun a, u3_noun b)
{
// this space left intentionally blank
if ( a == b ) {
return c3y;
}
else {
if ( _(u3a_is_atom(a)) ) {
u3a_atom* a_u = u3a_to_ptr(a);
if ( !_(u3a_is_atom(b)) ||
_(u3a_is_cat(a)) ||
_(u3a_is_cat(b)) )
{
return c3n;
}
else {
u3a_atom* b_u = u3a_to_ptr(b);
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) )
{
return c3n;
}
else {
c3_w w_rez = a_u->len_w;
c3_w w_mox = b_u->len_w;
if ( w_rez != w_mox ) {
return c3n;
}
else {
c3_w i_w;
for ( i_w = 0; i_w < w_rez; i_w++ ) {
if ( a_u->buf_w[i_w] != b_u->buf_w[i_w] ) {
return c3n;
}
}
return c3y;
}
}
}
}
else {
if ( _(u3a_is_atom(b)) ) {
return c3n;
}
else {
u3a_cell* a_u = u3a_to_ptr(a);
u3a_cell* b_u = u3a_to_ptr(b);
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) )
{
return c3n;
}
else {
if ( c3n == _sang_x(a_u->hed, b_u->hed) ) {
return c3n;
}
else {
_sang_one(&a_u->hed, &b_u->hed);
if ( c3n == _sang_x(a_u->tel, b_u->tel) ) {
return c3n;
}
else {
_sang_one(&a_u->tel, &b_u->tel);
return c3y;
}
}
}
}
}
}
}
/* _sung_one(): pick a unified pointer for identical (a) and (b).
@ -621,145 +669,168 @@ _sung_one(u3_noun* a, u3_noun* b)
}
}
static inline c3_o
_song_atom(u3_atom a, u3_atom b)
/* _sung_x(): yes if a and b are the same noun, unifying.
*/
static c3_o
_sung_x(u3_noun a, u3_noun b)
{
u3a_atom* a_u = u3a_to_ptr(a);
if ( !_(u3a_is_atom(b)) ||
_(u3a_is_cat(a)) ||
_(u3a_is_cat(b)) )
{
return c3n;
if ( a == b ) {
return c3y;
}
else {
u3a_atom* b_u = u3a_to_ptr(b);
if ( _(u3a_is_atom(a)) ) {
u3a_atom* a_u = u3a_to_ptr(a);
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) )
{
return c3n;
}
else {
c3_w w_rez = a_u->len_w;
c3_w w_mox = b_u->len_w;
if ( w_rez != w_mox ) {
if ( !_(u3a_is_atom(b)) ||
_(u3a_is_cat(a)) ||
_(u3a_is_cat(b)) )
{
return c3n;
}
else {
c3_w i_w;
u3a_atom* b_u = u3a_to_ptr(b);
for ( i_w = 0; i_w < w_rez; i_w++ ) {
if ( a_u->buf_w[i_w] != b_u->buf_w[i_w] ) {
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) )
{
return c3n;
}
else {
c3_w w_rez = a_u->len_w;
c3_w w_mox = b_u->len_w;
if ( w_rez != w_mox ) {
return c3n;
}
}
}
}
}
return c3y;
}
/* knob: set lower to get more/earlier memoize-by-pointer,
* higher to avoid allocating the u3h as often */
#define EQ_WHISTLE 1024
/* _song_x(): yes if a and b are the same noun, use uni to unify
*/
static c3_o
_song_x(u3_noun a, u3_noun b, void (*uni)(u3_noun*, u3_noun*))
{
eqframe* fam;
u3p(eqframe) empty = u3R->cap_p;
c3_w wis_w = 0;
c3_o r_o = c3n;
u3p(u3h_root) har_p = 0;
_eq_push(a, b);
/* there's a while and a switch here. continues all mean "do the loop again"
** and breaks all mean "fall through this switch case". There are no breaks
** that early terminate the loop.
*/
while ( empty != u3R->cap_p ) {
fam = _eq_peek();
if ( (a = fam->a) == (b = fam->b) ) {
r_o = c3y;
}
else if ( c3y == u3a_is_atom(a) ) {
r_o = _song_atom(a, b);
}
else if ( c3y == u3a_is_atom(b) ) {
r_o = c3n;
}
else {
u3a_cell* a_u = u3a_to_ptr(a);
u3a_cell* b_u = u3a_to_ptr(b);
switch ( fam->sat_y ) {
case 2:
uni(&(a_u->tel), &(b_u->tel));
r_o = c3y;
break;
case 1:
uni(&(a_u->hed), &(b_u->hed));
_eq_push(a_u->tel, b_u->tel);
fam->sat_y = 2;
continue;
case 0: {
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) ) {
r_o = c3n;
break;
}
else {
if ( har_p != 0 ) {
u3_noun key = u3nc(u3a_to_off(a), u3a_to_off(b));
u3_noun got = u3h_get(har_p, key);
u3z(key);
if ( u3_none != got ) {
_eq_pop();
continue;
c3_w i_w;
for ( i_w = 0; i_w < w_rez; i_w++ ) {
if ( a_u->buf_w[i_w] != b_u->buf_w[i_w] ) {
return c3n;
}
}
_eq_push(a_u->hed, b_u->hed);
fam->sat_y = 1;
continue;
return c3y;
}
}
default:
c3_assert(0);
}
}
if ( c3n == r_o ) {
if ( 0 != har_p ) {
u3h_free(har_p);
}
u3R->cap_p = empty;
return c3n;
}
else {
if ( 0 == har_p && (wis_w++ > EQ_WHISTLE) ) {
har_p = u3h_new();
if ( _(u3a_is_atom(b)) ) {
return c3n;
}
if ( 0 != har_p ) {
u3_noun key = u3nc(u3a_to_off(a), u3a_to_off(b));
u3h_put(har_p, key, c3y);
u3z(key);
else {
u3a_cell* a_u = u3a_to_ptr(a);
u3a_cell* b_u = u3a_to_ptr(b);
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) )
{
return c3n;
}
else {
if ( c3n == _sung_x(a_u->hed, b_u->hed) ) {
return c3n;
}
else {
_sung_one(&a_u->hed, &b_u->hed);
if ( c3n == _sung_x(a_u->tel, b_u->tel) ) {
return c3n;
}
else {
_sung_one(&a_u->tel, &b_u->tel);
return c3y;
}
}
}
}
_eq_pop();
}
}
}
if ( 0 != har_p ) {
u3h_free(har_p);
/* _sing_x():
**
** Yes iff (a) and (b) are the same noun.
*/
static c3_o
_sing_x(u3_noun a,
u3_noun b)
{
c3_assert(u3_none != a);
c3_assert(u3_none != b);
if ( a == b ) {
return c3y;
}
else {
if ( _(u3a_is_atom(a)) ) {
u3a_atom* a_u = u3a_to_ptr(a);
if ( !_(u3a_is_atom(b)) ||
_(u3a_is_cat(a)) ||
_(u3a_is_cat(b)) )
{
return c3n;
}
else {
u3a_atom* b_u = u3a_to_ptr(b);
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) )
{
return c3n;
}
else {
c3_w w_rez = a_u->len_w;
c3_w w_mox = b_u->len_w;
if ( w_rez != w_mox ) {
return c3n;
}
else {
c3_w i_w;
for ( i_w = 0; i_w < w_rez; i_w++ ) {
if ( a_u->buf_w[i_w] != b_u->buf_w[i_w] ) {
return c3n;
}
}
return c3y;
}
}
}
}
else {
if ( _(u3a_is_atom(b)) ) {
return c3n;
}
else {
u3a_cell* a_u = u3a_to_ptr(a);
u3a_cell* b_u = u3a_to_ptr(b);
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) )
{
return c3n;
}
else {
if ( c3n == _sing_x(u3a_h(a), u3a_h(b)) ) {
return c3n;
}
else if ( c3n == _sing_x(u3a_t(a), u3a_t(b)) ) {
return c3n;
}
return c3y;
}
}
}
}
return c3y;
}
/* u3r_sang(): yes iff (a) and (b) are the same noun, unifying equals.
@ -767,7 +838,7 @@ _song_x(u3_noun a, u3_noun b, void (*uni)(u3_noun*, u3_noun*))
c3_o
u3r_sang(u3_noun a, u3_noun b)
{
return _song_x(a, b, &_sang_one);
return _sang_x(a, b);
}
/* u3r_sing():
@ -792,7 +863,7 @@ u3r_sing(u3_noun a, u3_noun b)
c3_o ret_o;
u3t_on(euq_o);
ret_o = _song_x(a, b, &_sing_one);
ret_o = _sing_x(a, b);
u3t_off(euq_o);
return ret_o;
@ -804,7 +875,7 @@ u3r_sing(u3_noun a, u3_noun b)
c3_o
u3r_sung(u3_noun a, u3_noun b)
{
return _song_x(a, b, &_sung_one);
return _sung_x(a, b);
}
c3_o