lisp: 63 bit -> 56 bit SMI

This massively simplifies the tagging implementation as all types now
have a 1 byte tag.  However, this does make the need for Big Integers
much greater as we've lost 8 bits of precision.
This commit is contained in:
2026-03-05 18:29:00 +00:00
parent 7f2dcc3ad2
commit a50ca72b24
2 changed files with 28 additions and 46 deletions

View File

@@ -12,22 +12,22 @@
lisp_t *tag_int(i64 i)
{
return TAG((u64)i, INT);
return TAG(i, INT);
}
lisp_t *tag_sym(const char *str)
{
return TAG((u64)str, SYM);
return TAG(str, SYM);
}
lisp_t *tag_vec(const vec_t *vec)
{
return TAG((u64)vec, VEC);
return TAG(vec, VEC);
}
lisp_t *tag_cons(const cons_t *cons)
{
return TAG((u64)cons, CONS);
return TAG(cons, CONS);
}
lisp_t *tag_generic(void *ptr, tag_t type)
@@ -35,7 +35,7 @@ lisp_t *tag_generic(void *ptr, tag_t type)
switch (type)
{
case TAG_NIL:
return NIL;
return TAG(ptr, NIL);
case TAG_INT:
return tag_int((i64)ptr);
case TAG_SYM:
@@ -53,39 +53,34 @@ lisp_t *tag_generic(void *ptr, tag_t type)
tag_t get_tag(const lisp_t *lisp)
{
static_assert(NUM_TAGS == 5);
if (!lisp)
return TAG_NIL;
else if (IS_TAG(lisp, INT))
return TAG_INT;
return (u64)lisp & 0xFF;
return GET_TAG(lisp);
}
i64 as_int(lisp_t *obj)
{
assert(IS_TAG(obj, INT));
u64 p_obj = (u64)obj;
return UNTAG(p_obj, INT) | // Delete the tag
(NTH_BYTE(p_obj, 7) & 0x80) << 56 // duplicate the MSB (preserve sign)
;
u64 raw_obj = UNTAG(obj);
u64 msb = (NTH_BYTE(raw_obj, 6) & 0x80) >> 7;
msb = ((1LU << 8) - msb) << 56;
return (i64)(raw_obj | msb);
}
char *as_sym(lisp_t *obj)
{
assert(IS_TAG(obj, SYM));
return (char *)UNTAG(obj, SYM);
return (char *)UNTAG(obj);
}
cons_t *as_cons(lisp_t *obj)
{
assert(IS_TAG(obj, CONS));
return (cons_t *)UNTAG(obj, CONS);
return (cons_t *)UNTAG(obj);
}
vec_t *as_vec(lisp_t *obj)
{
assert(IS_TAG(obj, VEC));
return (vec_t *)UNTAG(obj, VEC);
return (vec_t *)UNTAG(obj);
}
void lisp_print(FILE *fp, lisp_t *lisp)