2023-02-28 06:31:08 +01:00
|
|
|
/* i.c -- instructions */
|
|
|
|
|
2023-03-04 06:07:52 +01:00
|
|
|
#include "n.h"
|
2023-02-28 06:31:08 +01:00
|
|
|
#include "i.h"
|
|
|
|
|
|
|
|
/* imports */
|
|
|
|
extern obj cx__2Aglobals_2A;
|
2023-03-03 19:18:00 +01:00
|
|
|
extern obj cx__2Atransformers_2A;
|
2023-03-30 05:18:39 +02:00
|
|
|
extern obj cx__2Adynamic_2Dstate_2A;
|
2023-03-11 07:50:00 +01:00
|
|
|
extern obj cx_continuation_2Dadapter_2Dcode;
|
2023-03-06 21:53:37 +01:00
|
|
|
extern obj cx_callmv_2Dadapter_2Dclosure;
|
2023-03-31 00:13:07 +02:00
|
|
|
extern obj cx__2Acurrent_2Dinput_2A;
|
|
|
|
extern obj cx__2Acurrent_2Doutput_2A;
|
|
|
|
extern obj cx__2Acurrent_2Derror_2A;
|
2023-02-28 06:31:08 +01:00
|
|
|
|
2023-04-02 22:16:43 +02:00
|
|
|
//#define istagged(o, t) istagged_inlined(o, t)
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
/* forwards */
|
2023-03-18 19:17:45 +01:00
|
|
|
static struct intgtab_entry *lookup_integrable(int sym);
|
2024-07-07 05:04:35 +02:00
|
|
|
static int intgtab_count(void);
|
2023-03-18 19:17:45 +01:00
|
|
|
static int isintegrable(obj x);
|
2023-03-19 00:13:38 +01:00
|
|
|
static struct intgtab_entry *integrabledata(obj x);
|
|
|
|
static obj mkintegrable(struct intgtab_entry *);
|
2023-03-18 19:17:45 +01:00
|
|
|
static int integrable_type(struct intgtab_entry *pi);
|
|
|
|
static const char *integrable_global(struct intgtab_entry *pi);
|
|
|
|
static const char *integrable_code(struct intgtab_entry *pi, int n);
|
2023-02-28 06:31:08 +01:00
|
|
|
static obj *rds_intgtab(obj *r, obj *sp, obj *hp);
|
|
|
|
static obj *rds_stox(obj *r, obj *sp, obj *hp);
|
|
|
|
static obj *rds_stoc(obj *r, obj *sp, obj *hp);
|
2023-03-03 19:18:00 +01:00
|
|
|
static obj *init_modules(obj *r, obj *sp, obj *hp);
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
/* platform-dependent optimizations */
|
|
|
|
#if defined(__clang__)
|
|
|
|
#define unlikely(x) __builtin_expect(x, 0)
|
|
|
|
#define likely(x) __builtin_expect(x, 1)
|
|
|
|
#if __clang_major__ >= 13
|
|
|
|
#define VM_AC_IN_REG
|
|
|
|
#define outofline __attribute__((noinline))
|
|
|
|
#define VM_MUSTTAIL_GUARANTEE
|
|
|
|
#define musttail __attribute__((musttail))
|
2023-03-04 17:51:51 +01:00
|
|
|
#define regcall __regcall
|
|
|
|
#define noalias restrict
|
2023-02-28 06:31:08 +01:00
|
|
|
#else
|
2023-03-04 17:51:51 +01:00
|
|
|
#define outofline
|
2023-02-28 06:31:08 +01:00
|
|
|
#define musttail
|
2023-03-04 17:51:51 +01:00
|
|
|
#define regcall
|
|
|
|
#define noalias
|
2023-02-28 06:31:08 +01:00
|
|
|
#endif
|
|
|
|
/* #pragma GCC optimize ("align-functions=16") */
|
|
|
|
#define nochecks __attribute__((no_stack_protector, aligned(8)))
|
|
|
|
/* __attribute__((nocf_check)) */
|
|
|
|
#define VM_INS_CODE_ALIGNED
|
|
|
|
#else
|
|
|
|
#define unlikely(x) (x)
|
|
|
|
#define likely(x) (x)
|
|
|
|
#define outofline
|
|
|
|
#define musttail
|
|
|
|
#define regcall
|
|
|
|
#define noalias
|
|
|
|
#define nochecks
|
|
|
|
#endif
|
|
|
|
|
2023-03-30 01:02:18 +02:00
|
|
|
/* copying objects */
|
2023-04-01 00:25:40 +02:00
|
|
|
#ifndef OBJCOPY_LOOP
|
2023-03-30 01:02:18 +02:00
|
|
|
#define objcpy(dst, src, cnt) \
|
|
|
|
memcpy((dst), (src), (cnt)*sizeof(obj))
|
|
|
|
#define objmove_left(dst, src, cnt) \
|
|
|
|
memmove((dst), (src), (cnt)*sizeof(obj))
|
|
|
|
#define objmove(dst, src, cnt) \
|
|
|
|
memmove((dst), (src), (cnt)*sizeof(obj))
|
|
|
|
#else
|
|
|
|
#define objcpy(dst, src, cnt) \
|
|
|
|
do { obj *noalias dp__ = (dst), *noalias sp__ = (src); int n__ = (cnt); \
|
|
|
|
while (n__-- > 0) *dp__++ = *sp__++; } while(0)
|
|
|
|
#define objmove_left(dst, src, cnt) \
|
|
|
|
do { int n__ = (cnt); obj *dp__ = (dst), *sp__ = (src); \
|
|
|
|
while (n__-- > 0) *dp__++ = *sp__++; } while(0)
|
|
|
|
#define objmove_right(dst, src, cnt) \
|
|
|
|
do { int n__ = (cnt); obj *dp__ = (dst)+n__, *sp__ = (src)+n__; \
|
|
|
|
while (n__-- > 0) *--dp__ = *--sp__; } while(0)
|
|
|
|
#define objmove(dst, src, cnt) \
|
|
|
|
do { int n__ = (cnt); obj *dp__ = (dst), *sp__ = (src); \
|
|
|
|
if (dp__ > sp__) { dp__ += n__, sp__ += n; while (n__-- > 0) *--dp__ = *--sp__; } \
|
|
|
|
else if (dp__ < sp__) while (n__-- > 0) *dp__++ = *sp__++; } while(0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
#ifdef VM_INS_CODE_ALIGNED /* direct representation */
|
|
|
|
#define ins_from_obj(x) ((ins_t)(x))
|
|
|
|
#else /* indirect representation (alignment needed) */
|
|
|
|
#define ins_from_obj(x) (*(ins_t*)(x))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* instruction calling convention (platform-dependent) */
|
|
|
|
#ifdef VM_AC_IN_REG
|
|
|
|
#define ILOCALS obj ac, *r, *ip, *sp, *hp
|
|
|
|
#define IPARAMS obj ac, obj *noalias r, obj *noalias ip, obj *noalias sp, obj *noalias hp
|
|
|
|
#define IARGS ac, r, ip, sp, hp
|
|
|
|
#define IARGS1 ac, r, ip+1, sp, hp
|
|
|
|
#define unload_ac() (ra = ac)
|
|
|
|
#define reload_ac() (ac = ra)
|
|
|
|
#else
|
|
|
|
#define ILOCALS obj *r, *ip, *sp, *hp
|
|
|
|
#define IPARAMS obj *r, obj *noalias ip, obj *noalias sp, obj *noalias hp
|
|
|
|
#define IARGS r, ip, sp, hp
|
|
|
|
#define IARGS1 r, ip+1, sp, hp
|
|
|
|
#define ac ra
|
|
|
|
#define unload_ac() (0)
|
|
|
|
#define reload_ac() (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* gc-safe obj registers; some of them used for saving/restoring during gc */
|
|
|
|
#define rk (r[0]) /* #f or any non-false value for unwindi; run result */
|
|
|
|
#define ra (r[1]) /* shadow reg for the accumulator (argc on call) */
|
|
|
|
#define rx (r[2]) /* next instruction (index in closure's code) */
|
|
|
|
#define rd (r[3]) /* current closure/display (vector of [0]=code, display) */
|
|
|
|
#define rs (r[4]) /* shadow reg for stack pointer */
|
2023-03-12 19:20:35 +01:00
|
|
|
#define rz (r[5]) /* red zone for stack pointer (r + len - rsz) */
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
/* the rest of the register file is used as a stack */
|
2023-04-01 00:25:40 +02:00
|
|
|
#define VM_REGC 6 /* r[0] ... r[5] */
|
|
|
|
#define VM_STACK_LEN 256000 /* r[6] ... r[256005] */
|
|
|
|
#define VM_STACK_RSZ 256 /* red zone for overflow checks */
|
2023-03-12 19:20:35 +01:00
|
|
|
#define VM_STACK_GSZ (VM_STACK_LEN-VM_STACK_RSZ)
|
2023-02-28 06:31:08 +01:00
|
|
|
|
2023-04-03 15:23:04 +02:00
|
|
|
/* faster non-debug type testing */
|
|
|
|
#ifdef NDEBUG /* quick */
|
|
|
|
static int istagged_inline(obj o, int t) { return isobjptr(o) && hblkref(o, 0) == obj_from_size(t); }
|
|
|
|
#define istagged(o, t) istagged_inline(o, t)
|
|
|
|
#endif
|
|
|
|
|
2023-03-26 06:16:52 +02:00
|
|
|
/* box representation extras */
|
|
|
|
#define boxbsz() hbsz(1+1)
|
|
|
|
#define hend_box() (*--hp = obj_from_size(BOX_BTAG), hendblk(1+1))
|
|
|
|
|
|
|
|
/* pair representation extras */
|
|
|
|
#define pairbsz() hbsz(2+1)
|
|
|
|
#define hend_pair() (*--hp = obj_from_size(PAIR_BTAG), hendblk(2+1))
|
|
|
|
|
|
|
|
/* vector representation extras */
|
|
|
|
#define vecbsz(n) hbsz((n)+1)
|
|
|
|
#define hend_vec(n) (*--hp = obj_from_size(VECTOR_BTAG), hendblk((n)+1))
|
|
|
|
|
2023-03-28 21:39:00 +02:00
|
|
|
/* record representation extras */
|
2023-04-02 22:16:43 +02:00
|
|
|
#define recbsz(c) hbsz((c)+1)
|
|
|
|
#define hend_rec(rtd, c) (*--hp = rtd, hendblk((c)+1))
|
2023-03-28 21:39:00 +02:00
|
|
|
|
2023-03-03 21:44:12 +01:00
|
|
|
/* vm closure representation */
|
2023-03-12 20:38:22 +01:00
|
|
|
#ifdef NDEBUG /* quick */
|
|
|
|
#define isvmclo(x) (isobjptr(x) && isobjptr(hblkref(x, 0)))
|
|
|
|
#define vmcloref(x,i) hblkref(x, i)
|
|
|
|
#define vmclolen(x) hblklen(x)
|
|
|
|
#define vmclobsz(c) hbsz(c)
|
2023-03-26 06:16:52 +02:00
|
|
|
#define hend_vmclo(c) hendblk(c)
|
2023-03-12 20:38:22 +01:00
|
|
|
#else /* slow but thorough */
|
2023-03-03 21:44:12 +01:00
|
|
|
#define isvmclo isprocedure
|
|
|
|
#define vmcloref *procedureref
|
|
|
|
#define vmclolen procedurelen
|
|
|
|
#define vmclobsz(c) hbsz(c)
|
2023-03-26 06:16:52 +02:00
|
|
|
#define hend_vmclo(c) hendblk(c)
|
2023-03-03 21:44:12 +01:00
|
|
|
#endif
|
|
|
|
|
2023-03-12 22:32:16 +01:00
|
|
|
/* vm tuple representation (c != 1) */
|
2023-04-02 22:16:43 +02:00
|
|
|
#define istuple(x) istagged(x, 0)
|
|
|
|
#define tupleref(x,i) *taggedref(x, 0, i)
|
|
|
|
#define tuplelen(x) taggedlen(x, 0)
|
|
|
|
#define tuplebsz(c) hbsz((c)+1)
|
|
|
|
#define hend_tuple(c) (*--hp = obj_from_size(0), hendblk((c)+1))
|
2023-03-12 22:32:16 +01:00
|
|
|
|
|
|
|
/* in/re-loading gc-save shadow registers */
|
2023-03-03 21:44:12 +01:00
|
|
|
#define unload_ip() (rx = obj_from_fixnum(ip - &vectorref(vmcloref(rd, 0), 0)))
|
|
|
|
#define reload_ip() (ip = &vectorref(vmcloref(rd, 0), fixnum_from_obj(rx)))
|
2023-02-28 06:31:08 +01:00
|
|
|
#define unload_sp() (rs = obj_from_fixnum(sp - r))
|
|
|
|
#define reload_sp() (sp = r + fixnum_from_obj(rs))
|
|
|
|
|
2023-03-12 22:32:16 +01:00
|
|
|
/* access to stack, display, global cells */
|
2023-02-28 06:31:08 +01:00
|
|
|
#define sref(i) (sp[-(i)-1])
|
2023-03-03 21:44:12 +01:00
|
|
|
#define dref(i) (vmcloref(rd, (i)+1))
|
2023-03-12 19:20:35 +01:00
|
|
|
#define gref(p) (boxref(p))
|
|
|
|
#ifdef _DEBUG
|
|
|
|
static void _sck(obj *s) {
|
|
|
|
assert(s != NULL);
|
|
|
|
assert(s >= cxg_regs + VM_REGC);
|
|
|
|
assert(s < cxg_rend);
|
|
|
|
}
|
|
|
|
#define spush(o) (_sck(sp), *sp++ = o)
|
|
|
|
#define spop() (_sck(sp), *--sp)
|
|
|
|
#define sdrop(n) (_sck(sp), sp -= (n))
|
|
|
|
#define sgrow(n) (_sck(sp), sp += (n))
|
|
|
|
#else
|
|
|
|
#define spush(o) (*sp++ = o)
|
|
|
|
#define spop() (*--sp)
|
|
|
|
#define sdrop(n) (sp -= (n))
|
|
|
|
#define sgrow(n) (sp += (n))
|
|
|
|
#endif
|
2023-02-28 06:31:08 +01:00
|
|
|
|
2024-07-24 23:42:28 +02:00
|
|
|
/* forcing gc explicitly */
|
|
|
|
#define hp_collect() do { \
|
|
|
|
unload_ac(); unload_ip(); \
|
|
|
|
hp = cxm_hgc(r, sp, hp, 0); \
|
|
|
|
reload_ac(); reload_ip(); \
|
|
|
|
} while (0)
|
2023-03-26 06:16:52 +02:00
|
|
|
/* reserving heap memory inside instructions */
|
2023-02-28 06:31:08 +01:00
|
|
|
#define hp_reserve(n) do { \
|
|
|
|
if (unlikely(hp < cxg_heap + (n))) { \
|
|
|
|
unload_ac(); unload_ip(); \
|
|
|
|
hp = cxm_hgc(r, sp, hp, n); \
|
|
|
|
reload_ac(); reload_ip(); \
|
|
|
|
} } while (0)
|
2023-03-26 06:16:52 +02:00
|
|
|
#define hp_reserve_inline(n) \
|
|
|
|
((hp < cxg_heap + (n)) \
|
|
|
|
? (unload_ac(), unload_ip(), hp = cxm_hgc(r, sp, hp, (n)), reload_ac(), reload_ip(), hp) \
|
|
|
|
: hp)
|
|
|
|
#define hp_pushptr(p, pt) \
|
|
|
|
(hp_reserve_inline(2), *--hp = (obj)(p), *--hp = (obj)(pt), (obj)(hp+1))
|
|
|
|
|
2023-03-27 22:49:49 +02:00
|
|
|
/* object representation extras */
|
2023-03-26 06:16:52 +02:00
|
|
|
#define bool_obj(b) obj_from_bool(b)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define is_bool(o) is_bool_obj(o)
|
|
|
|
#define get_bool(o) bool_from_obj(o)
|
2023-03-26 06:16:52 +02:00
|
|
|
#define char_obj(b) obj_from_char(b)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define is_char(o) is_char_obj(o)
|
|
|
|
#define get_char(o) char_from_obj(o)
|
2023-03-26 18:02:36 +02:00
|
|
|
#define void_obj() obj_from_void(0)
|
2023-03-29 00:14:45 +02:00
|
|
|
#define is_void(o) (o == obj_from_void(0))
|
2024-07-17 23:47:08 +02:00
|
|
|
#define is_shebang(o) isshebang(o)
|
|
|
|
#define get_shebang(o) getshebang(o)
|
|
|
|
#define shebang_obj(i) mkshebang(i)
|
2023-03-29 01:51:37 +02:00
|
|
|
#define unit_obj() obj_from_unit()
|
|
|
|
#define is_unit(o) (o == obj_from_unit())
|
2023-03-26 06:38:50 +02:00
|
|
|
#define null_obj() mknull()
|
2023-03-27 22:49:49 +02:00
|
|
|
#define is_null(o) isnull(o)
|
2023-03-26 06:16:52 +02:00
|
|
|
#define eof_obj() mkeof()
|
2023-03-27 22:49:49 +02:00
|
|
|
#define is_eof(o) ((o) == mkeof())
|
2023-03-26 06:16:52 +02:00
|
|
|
#define fixnum_obj(x) obj_from_fixnum(x)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define is_fixnum(o) is_fixnum_obj(o)
|
2023-04-02 22:16:43 +02:00
|
|
|
#define are_fixnums(o1, o2) (is_fixnum(o1) && is_fixnum(o2))
|
|
|
|
#define get_fixnum(o) fixnum_from_obj(o)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define is_byte(o) is_byte_obj(o)
|
2023-03-28 00:58:37 +02:00
|
|
|
#define byte_obj(x) obj_from_fixnum((unsigned char)(x))
|
2023-04-02 22:16:43 +02:00
|
|
|
#define get_byte(o) ((unsigned char)fixnum_from_obj(o))
|
2023-04-03 00:11:08 +02:00
|
|
|
#ifdef FLONUMS_BOXED
|
2023-03-26 06:16:52 +02:00
|
|
|
#define flonum_obj(x) hp_pushptr(dupflonum(x), FLONUM_NTAG)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define is_flonum(o) is_flonum_obj(o)
|
|
|
|
#define get_flonum(o) flonum_from_obj(o)
|
2023-04-03 00:11:08 +02:00
|
|
|
#else
|
|
|
|
#define flonum_obj(x) obj_from_flonum(0, x)
|
|
|
|
#define is_flonum(o) is_flonum_obj(o)
|
|
|
|
#define get_flonum(o) flonum_from_obj(o)
|
|
|
|
#endif
|
2023-03-27 22:49:49 +02:00
|
|
|
#define is_symbol(o) issymbol(o)
|
|
|
|
#define get_symbol(o) getsymbol(o)
|
2024-07-17 23:47:08 +02:00
|
|
|
#define symbol_obj(i) mksymbol(i)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define is_pair(o) ispair(o)
|
|
|
|
#define pair_car(o) car(o)
|
|
|
|
#define pair_cdr(o) cdr(o)
|
|
|
|
#define is_list(o) islist(o)
|
2023-03-28 06:09:25 +02:00
|
|
|
#define is_circular(o) iscircular(o)
|
|
|
|
#define is_noncircular(o) (!iscircular(o))
|
2023-03-27 22:49:49 +02:00
|
|
|
#define is_vector(o) isvector(o)
|
2023-03-29 00:14:45 +02:00
|
|
|
#define vector_len(o) vectorlen(o)
|
|
|
|
#define vector_ref(o, i) vectorref(o, i)
|
2023-03-26 06:16:52 +02:00
|
|
|
#define string_obj(s) hp_pushptr((s), STRING_NTAG)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define is_string(o) isstring(o)
|
2023-03-29 00:14:45 +02:00
|
|
|
#define string_len(o) stringlen(o)
|
|
|
|
#define string_ref(o, i) (*stringref(o, i))
|
2023-03-26 19:20:33 +02:00
|
|
|
#define bytevector_obj(s) hp_pushptr((s), BYTEVECTOR_NTAG)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define is_bytevector(o) isbytevector(o)
|
2023-03-29 00:14:45 +02:00
|
|
|
#define bytevector_len(o) bytevectorlen(o)
|
|
|
|
#define bytevector_ref(o, i) (*bytevectorref(o, i))
|
2023-03-26 06:16:52 +02:00
|
|
|
#define iport_file_obj(fp) hp_pushptr((fp), IPORT_FILE_NTAG)
|
|
|
|
#define oport_file_obj(fp) hp_pushptr((fp), OPORT_FILE_NTAG)
|
|
|
|
#define iport_string_obj(fp) hp_pushptr((fp), IPORT_STRING_NTAG)
|
|
|
|
#define oport_string_obj(fp) hp_pushptr((fp), OPORT_STRING_NTAG)
|
2023-03-28 00:58:37 +02:00
|
|
|
#define iport_bytevector_obj(fp) hp_pushptr((fp), IPORT_BYTEVECTOR_NTAG)
|
|
|
|
#define oport_bytevector_obj(fp) hp_pushptr((fp), OPORT_BYTEVECTOR_NTAG)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define is_iport(o) isiport(o)
|
|
|
|
#define is_oport(o) isoport(o)
|
|
|
|
#define is_box(o) isbox(o)
|
2023-03-29 00:14:45 +02:00
|
|
|
#define box_ref(o) boxref(o)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define is_proc(o) isvmclo(o)
|
2023-03-28 21:39:00 +02:00
|
|
|
#define is_tuple(o) (isrecord(o) && recordrtd(o) == 0)
|
2023-03-29 01:32:54 +02:00
|
|
|
#define tuple_len(o) tuplelen(o)
|
|
|
|
#define tuple_ref(o, i) tupleref(o, i)
|
2023-03-28 21:39:00 +02:00
|
|
|
#define is_record(o) (isrecord(o) && recordrtd(o) != 0)
|
|
|
|
#define record_rtd(o) recordrtd(o)
|
|
|
|
#define record_len(o) recordlen(o)
|
2023-03-29 00:14:45 +02:00
|
|
|
#define record_ref(o, i) recordref(o, i)
|
2023-03-28 21:39:00 +02:00
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
/* cxi instructions protocol; retval is new hp: */
|
|
|
|
typedef obj* regcall (*ins_t)(IPARAMS);
|
|
|
|
|
|
|
|
/* calling next instruction and returning from instruction */
|
|
|
|
#define goih(ih) musttail return ((ih)(IARGS))
|
|
|
|
#define goi(name) musttail return (glue(cxi_, name)(IARGS))
|
|
|
|
#define gonexti() musttail return ((ins_from_obj(*ip))(IARGS1))
|
|
|
|
#define unwindi(c) return (unload_ac(), unload_ip(), rd = (c), hp)
|
2023-03-12 19:20:35 +01:00
|
|
|
#define check_sp() if (unlikely(sp > (obj*)rz)) fail("stack overflow")
|
2023-02-28 06:31:08 +01:00
|
|
|
#ifdef VM_MUSTTAIL_GUARANTEE
|
2023-03-12 19:20:35 +01:00
|
|
|
#define callsubi() check_sp(); reload_ip(); gonexti()
|
2023-02-28 06:31:08 +01:00
|
|
|
#define retfromi() reload_ip(); gonexti()
|
|
|
|
#define trampcnd() (0)
|
|
|
|
#else
|
2023-03-12 19:20:35 +01:00
|
|
|
#define callsubi() check_sp(); unload_sp(); return hp
|
2023-02-28 06:31:08 +01:00
|
|
|
#define retfromi() unload_sp(); return hp
|
|
|
|
#define trampcnd() (rd)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* defining instruction helpers */
|
|
|
|
#define define_instrhelper(name) \
|
|
|
|
static obj* regcall outofline nochecks name(IPARAMS)
|
|
|
|
|
|
|
|
/* defining and binding instructions */
|
|
|
|
#define define_instruction(name) \
|
|
|
|
obj* regcall nochecks glue(cxi_, name)(IPARAMS)
|
|
|
|
#ifdef VM_INS_CODE_ALIGNED
|
|
|
|
#define declare_instruction_global(name) \
|
|
|
|
obj glue(cx_ins_2D, name) = (obj)(&glue(cxi_, name));
|
|
|
|
#else
|
|
|
|
#define declare_instruction_global(name) \
|
|
|
|
static ins_t glue(cxib_, name)[1] = { &glue(cxi_, name) }; \
|
|
|
|
obj glue(cx_ins_2D, name) = (obj)(&glue(cxib_, name)[0]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static obj vmhost(obj);
|
2023-03-18 19:17:45 +01:00
|
|
|
obj vmcases[13] = {
|
2023-02-28 06:31:08 +01:00
|
|
|
(obj)vmhost, (obj)vmhost, (obj)vmhost, (obj)vmhost,
|
2023-03-18 19:17:45 +01:00
|
|
|
(obj)vmhost, (obj)vmhost, (obj)vmhost, (obj)vmhost,
|
|
|
|
(obj)vmhost, (obj)vmhost, (obj)vmhost, (obj)vmhost,
|
|
|
|
(obj)vmhost
|
2023-02-28 06:31:08 +01:00
|
|
|
};
|
|
|
|
/* vmhost procedure */
|
|
|
|
static obj vmhost(obj pc)
|
|
|
|
{
|
|
|
|
ILOCALS;
|
2023-03-12 19:20:35 +01:00
|
|
|
int rc = cxg_rc, i;
|
2023-02-28 06:31:08 +01:00
|
|
|
r = cxg_regs; hp = cxg_hp;
|
|
|
|
jump:
|
|
|
|
switch (objptr_from_obj(pc)-vmcases) {
|
|
|
|
|
2023-03-03 19:18:00 +01:00
|
|
|
case 0: /* execute-thunk-closure */
|
2023-02-28 06:31:08 +01:00
|
|
|
/* r[0] = self, r[1] = k, r[2] = closure */
|
2023-03-12 19:20:35 +01:00
|
|
|
{ obj k, arg;
|
2023-02-28 06:31:08 +01:00
|
|
|
assert(rc == 3);
|
|
|
|
k = r[1]; arg = r[2];
|
|
|
|
r = cxm_rgc(NULL, VM_REGC + VM_STACK_LEN);
|
|
|
|
rk = k; /* continuation, kept there */
|
|
|
|
ra = obj_from_fixnum(0); /* argc, shadow ac */
|
|
|
|
rx = obj_from_fixnum(0); /* shadow ip */
|
|
|
|
rd = arg; /* thunk closure to execute */
|
|
|
|
rs = obj_from_fixnum(VM_REGC); /* sp */
|
2023-03-12 19:20:35 +01:00
|
|
|
rz = (obj)(r + VM_STACK_GSZ); /* sp red zone */
|
2023-02-28 06:31:08 +01:00
|
|
|
do { /* unwindi trampoline */
|
|
|
|
reload_ac(); /* ra => ac */
|
|
|
|
reload_ip(); /* rd/rx => ip */
|
|
|
|
reload_sp(); /* rs => sp */
|
|
|
|
hp = (ins_from_obj(*ip))(IARGS1);
|
2023-03-12 19:20:35 +01:00
|
|
|
} while (likely(trampcnd()));
|
2023-02-28 06:31:08 +01:00
|
|
|
/* r[0] = k, r[1] = result */
|
|
|
|
r[2] = r[1];
|
|
|
|
r[1] = obj_from_ktrap();
|
|
|
|
pc = objptr_from_obj(r[0])[0];
|
|
|
|
rc = 3;
|
|
|
|
goto jump; }
|
|
|
|
|
|
|
|
case 1: /* make-closure */
|
|
|
|
/* r[0] = clo, r[1] = k, r[2] = code */
|
|
|
|
{ assert(rc == 3);
|
|
|
|
r[0] = r[1]; r[1] = r[2];
|
|
|
|
/* r[0] = k; r[1] = code */
|
2023-03-03 21:44:12 +01:00
|
|
|
hreserve(vmclobsz(1), 2); /* 2 live regs */
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = r[1];
|
2023-03-26 06:16:52 +02:00
|
|
|
r[2] = hend_vmclo(1);
|
2023-02-28 06:31:08 +01:00
|
|
|
r[1] = obj_from_ktrap();
|
|
|
|
pc = objptr_from_obj(r[0])[0];
|
|
|
|
rc = 3;
|
|
|
|
goto jump; }
|
|
|
|
|
|
|
|
case 2: /* decode-sexp */
|
|
|
|
/* r[0] = clo, r[1] = k, r[2] = xstr */
|
|
|
|
{ assert(rc == 3);
|
|
|
|
r[0] = r[1]; r[1] = r[2];
|
|
|
|
/* r[0] = k; r[1] = xstr */
|
2023-03-12 19:20:35 +01:00
|
|
|
for (i = 3; i < VM_REGC; ++i) r[i] = 0;
|
|
|
|
rz = (obj)(r + VM_STACK_GSZ); /* sp red zone */
|
|
|
|
hp = rds_stox(r, r+VM_REGC, hp); /* r[1] -> r[1] */
|
2023-02-28 06:31:08 +01:00
|
|
|
r[2] = r[1]; r[1] = obj_from_ktrap();
|
|
|
|
/* r[0] = k; r[1] = ek; r[2] = code */
|
|
|
|
pc = objptr_from_obj(r[0])[0];
|
|
|
|
rc = 3;
|
|
|
|
goto jump; }
|
|
|
|
|
|
|
|
case 3: /* decode-code */
|
|
|
|
/* r[0] = clo, r[1] = k, r[2] = cstr */
|
|
|
|
{ assert(rc == 3);
|
|
|
|
r[0] = r[1]; r[1] = r[2];
|
|
|
|
/* r[0] = k; r[1] = cstr */
|
2023-03-12 19:20:35 +01:00
|
|
|
for (i = 3; i < VM_REGC; ++i) r[i] = 0;
|
|
|
|
rz = (obj)(r + VM_STACK_GSZ); /* sp red zone */
|
|
|
|
hp = rds_stoc(r, r+VM_REGC, hp); /* r[1] -> r[1] */
|
2023-02-28 06:31:08 +01:00
|
|
|
r[2] = r[1]; r[1] = obj_from_ktrap();
|
|
|
|
/* r[0] = k; r[1] = ek; r[2] = code */
|
|
|
|
pc = objptr_from_obj(r[0])[0];
|
|
|
|
rc = 3;
|
|
|
|
goto jump; }
|
|
|
|
|
|
|
|
case 4: /* find-integrable-encoding */
|
|
|
|
/* r[0] = clo, r[1] = k, r[2] = id, r[3] = argc */
|
|
|
|
{ assert(rc == 4);
|
2023-03-21 20:29:28 +01:00
|
|
|
r[2] = obj_from_bool(0);;
|
2023-02-28 06:31:08 +01:00
|
|
|
r[0] = r[1]; r[1] = obj_from_ktrap();
|
|
|
|
pc = objptr_from_obj(r[0])[0];
|
|
|
|
rc = 3;
|
|
|
|
goto jump; }
|
|
|
|
|
|
|
|
case 5: /* encode-integrable */
|
|
|
|
/* r[0] = clo, r[1] = k, r[2] = argc, r[3] = pe, r[4] = port */
|
|
|
|
{ assert(rc == 5);
|
2023-03-21 20:29:28 +01:00
|
|
|
assert(0);
|
2023-02-28 06:31:08 +01:00
|
|
|
r[0] = r[1]; r[1] = obj_from_ktrap();
|
|
|
|
pc = objptr_from_obj(r[0])[0];
|
|
|
|
rc = 3;
|
|
|
|
goto jump; }
|
|
|
|
|
|
|
|
case 6: /* install-global-lambdas */
|
|
|
|
/* r[0] = clo, r[1] = k */
|
|
|
|
{ assert(rc == 2);
|
|
|
|
r[0] = r[1];
|
2023-03-12 19:20:35 +01:00
|
|
|
for (i = 2; i < VM_REGC; ++i) r[i] = 0;
|
|
|
|
rz = (obj)(r + VM_STACK_GSZ); /* sp red zone */
|
|
|
|
hp = rds_intgtab(r, r+VM_REGC, hp);
|
2023-02-28 06:31:08 +01:00
|
|
|
r[1] = obj_from_ktrap();
|
|
|
|
r[2] = obj_from_void(0);
|
|
|
|
pc = objptr_from_obj(r[0])[0];
|
|
|
|
rc = 3;
|
|
|
|
goto jump; }
|
2023-03-03 19:18:00 +01:00
|
|
|
|
|
|
|
case 7: /* initialize-modules */
|
|
|
|
/* r[0] = clo, r[1] = k */
|
|
|
|
{ assert(rc == 2);
|
|
|
|
r[0] = r[1];
|
|
|
|
r = cxm_rgc(NULL, VM_REGC + VM_STACK_LEN);
|
2023-03-12 19:20:35 +01:00
|
|
|
for (i = 2; i < VM_REGC; ++i) r[i] = 0;
|
|
|
|
rz = (obj)(r + VM_STACK_GSZ); /* sp red zone */
|
|
|
|
hp = init_modules(r, r+VM_REGC, hp);
|
2023-03-03 19:18:00 +01:00
|
|
|
r[1] = obj_from_ktrap();
|
|
|
|
r[2] = obj_from_void(0);
|
|
|
|
pc = objptr_from_obj(r[0])[0];
|
|
|
|
rc = 3;
|
|
|
|
goto jump; }
|
2023-03-18 19:17:45 +01:00
|
|
|
|
|
|
|
case 8: /* integrable? */
|
|
|
|
/* r[0] = clo, r[1] = k, r[2] = obj */
|
|
|
|
{ assert(rc == 3);
|
|
|
|
r[2] = obj_from_bool(isintegrable(r[2]));
|
|
|
|
r[0] = r[1]; r[1] = obj_from_ktrap();
|
|
|
|
pc = objptr_from_obj(r[0])[0];
|
|
|
|
rc = 3;
|
|
|
|
goto jump; }
|
|
|
|
|
|
|
|
case 9: /* lookup-integrable */
|
|
|
|
/* r[0] = clo, r[1] = k, r[2] = id */
|
|
|
|
{ assert(rc == 3);
|
|
|
|
if (issymbol(r[2])) {
|
|
|
|
int sym = getsymbol(r[2]);
|
|
|
|
struct intgtab_entry *pe = lookup_integrable(sym);
|
2023-03-19 00:13:38 +01:00
|
|
|
r[2] = pe ? mkintegrable(pe) : obj_from_bool(0);
|
2023-03-18 19:17:45 +01:00
|
|
|
} else r[2] = obj_from_bool(0);
|
|
|
|
r[0] = r[1]; r[1] = obj_from_ktrap();
|
|
|
|
pc = objptr_from_obj(r[0])[0];
|
|
|
|
rc = 3;
|
|
|
|
goto jump; }
|
|
|
|
|
|
|
|
case 10: /* integrable-type */
|
|
|
|
/* r[0] = clo, r[1] = k, r[2] = ig */
|
|
|
|
{ assert(rc == 3);
|
|
|
|
if (isintegrable(r[2])) {
|
2023-03-19 00:13:38 +01:00
|
|
|
int it = integrable_type(integrabledata(r[2]));
|
2023-03-18 19:17:45 +01:00
|
|
|
r[2] = it ? obj_from_char(it) : obj_from_bool(0);
|
|
|
|
} else r[2] = obj_from_bool(0);
|
|
|
|
r[0] = r[1]; r[1] = obj_from_ktrap();
|
|
|
|
pc = objptr_from_obj(r[0])[0];
|
|
|
|
rc = 3;
|
|
|
|
goto jump; }
|
|
|
|
|
|
|
|
case 11: /* integrable-global */
|
|
|
|
/* r[0] = clo, r[1] = k, r[2] = ig */
|
|
|
|
{ assert(rc == 3);
|
|
|
|
if (isintegrable(r[2])) {
|
2023-03-19 00:13:38 +01:00
|
|
|
const char *igs = integrable_global(integrabledata(r[2]));
|
2023-03-18 19:17:45 +01:00
|
|
|
r[2] = igs ? mksymbol(internsym((char*)igs)) : obj_from_bool(0);
|
|
|
|
} else r[2] = obj_from_bool(0);
|
|
|
|
r[0] = r[1]; r[1] = obj_from_ktrap();
|
|
|
|
pc = objptr_from_obj(r[0])[0];
|
|
|
|
rc = 3;
|
|
|
|
goto jump; }
|
|
|
|
|
|
|
|
case 12: /* integrable-code */
|
|
|
|
/* r[0] = clo, r[1] = k, r[2] = ig, r[3] = i */
|
|
|
|
{ assert(rc == 4);
|
2023-03-27 22:49:49 +02:00
|
|
|
if (isintegrable(r[2]) && is_fixnum(r[3])) {
|
|
|
|
const char *cs = integrable_code(integrabledata(r[2]), get_fixnum(r[3]));
|
2023-03-18 19:17:45 +01:00
|
|
|
r[2] = cs ? hpushstr(3, newstring((char*)cs)) : obj_from_bool(0);
|
|
|
|
} else r[2] = obj_from_bool(0);
|
|
|
|
r[0] = r[1]; r[1] = obj_from_ktrap();
|
|
|
|
pc = objptr_from_obj(r[0])[0];
|
|
|
|
rc = 3;
|
|
|
|
goto jump; }
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
default: /* inter-host call */
|
|
|
|
cxg_hp = hp;
|
|
|
|
cxm_rgc(r, 1);
|
|
|
|
cxg_rc = rc;
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* instructions for basic vm machinery */
|
|
|
|
|
|
|
|
define_instrhelper(cxi_fail) {
|
2023-03-26 18:02:36 +02:00
|
|
|
fprintf(stderr, "run-time failure: %s\n", (char*)ac);
|
2024-07-12 01:12:03 +02:00
|
|
|
ac = void_obj(); /* so it is not printed by repl */
|
2023-02-28 06:31:08 +01:00
|
|
|
unwindi(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instrhelper(cxi_failactype) {
|
|
|
|
char *msg = (char*)spop(); obj p;
|
|
|
|
fprintf(stderr, "run-time failure: argument is not a %s:\n", msg);
|
2023-03-26 06:16:52 +02:00
|
|
|
p = oport_file_obj(stderr); spush(p);
|
2023-02-28 06:31:08 +01:00
|
|
|
oportputcircular(ac, p, 0);
|
|
|
|
fputc('\n', stderr);
|
|
|
|
spop();
|
2024-07-12 01:12:03 +02:00
|
|
|
ac = void_obj(); /* so it is not printed by repl */
|
2023-02-28 06:31:08 +01:00
|
|
|
unwindi(0);
|
|
|
|
}
|
|
|
|
|
2023-03-07 01:36:15 +01:00
|
|
|
#define fail(msg) do { ac = (obj)msg; musttail return cxi_fail(IARGS); } while (0)
|
|
|
|
#define failtype(x, msg) do { ac = (x); spush((obj)msg); musttail return cxi_failactype(IARGS); } while (0)
|
|
|
|
#define failactype(msg) do { spush((obj)msg); musttail return cxi_failactype(IARGS); } while (0)
|
|
|
|
|
2023-03-27 22:49:49 +02:00
|
|
|
#define ckp(x) do { obj _x = (x); if (unlikely(!is_pair(_x))) \
|
2023-03-07 01:36:15 +01:00
|
|
|
{ ac = _x; spush((obj)"pair"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define ckl(x) do { obj _x = (x); if (unlikely(!is_list(_x))) \
|
2023-03-07 01:36:15 +01:00
|
|
|
{ ac = _x; spush((obj)"list"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define cku(x) do { obj _x = (x); if (unlikely(!is_null(_x))) \
|
2023-03-26 19:20:33 +02:00
|
|
|
{ ac = _x; spush((obj)"proper list"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-28 21:39:00 +02:00
|
|
|
#define cko(x) do { obj _x = (x); if (unlikely(!is_record(_x))) \
|
|
|
|
{ ac = _x; spush((obj)"record"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define ckv(x) do { obj _x = (x); if (unlikely(!is_vector(_x))) \
|
2023-03-12 00:43:51 +01:00
|
|
|
{ ac = _x; spush((obj)"vector"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define ckc(x) do { obj _x = (x); if (unlikely(!is_char(_x))) \
|
2023-03-07 01:36:15 +01:00
|
|
|
{ ac = _x; spush((obj)"char"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define cks(x) do { obj _x = (x); if (unlikely(!is_string(_x))) \
|
2023-03-07 01:36:15 +01:00
|
|
|
{ ac = _x; spush((obj)"string"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define ckb(x) do { obj _x = (x); if (unlikely(!is_bytevector(_x))) \
|
2023-03-26 19:20:33 +02:00
|
|
|
{ ac = _x; spush((obj)"bytevector"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define cki(x) do { obj _x = (x); if (unlikely(!is_fixnum(_x))) \
|
2023-03-12 00:43:51 +01:00
|
|
|
{ ac = _x; spush((obj)"fixnum"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define ckj(x) do { obj _x = (x); if (unlikely(!is_flonum(_x))) \
|
2023-03-12 00:43:51 +01:00
|
|
|
{ ac = _x; spush((obj)"flonum"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define ckn(x) do { obj _x = (x); if (unlikely(!is_fixnum(_x) && !is_flonum(_x))) \
|
2023-03-12 00:43:51 +01:00
|
|
|
{ ac = _x; spush((obj)"number"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define ckk(x) do { obj _x = (x); if (unlikely(!is_fixnum(_x) || get_fixnum(_x) < 0)) \
|
2023-03-12 00:43:51 +01:00
|
|
|
{ ac = _x; spush((obj)"nonnegative fixnum"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define ck8(x) do { obj _x = (x); if (unlikely(!is_byte(_x))) \
|
2023-03-26 19:20:33 +02:00
|
|
|
{ ac = _x; spush((obj)"byte"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define cky(x) do { obj _x = (x); if (unlikely(!is_symbol(_x))) \
|
2023-03-07 01:36:15 +01:00
|
|
|
{ ac = _x; spush((obj)"symbol"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define ckr(x) do { obj _x = (x); if (unlikely(!is_iport(_x))) \
|
2023-03-07 01:36:15 +01:00
|
|
|
{ ac = _x; spush((obj)"input port"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define ckw(x) do { obj _x = (x); if (unlikely(!is_oport(_x))) \
|
2023-03-07 01:36:15 +01:00
|
|
|
{ ac = _x; spush((obj)"output port"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define ckx(x) do { obj _x = (x); if (unlikely(!is_proc(_x))) \
|
2023-03-07 01:36:15 +01:00
|
|
|
{ ac = _x; spush((obj)"procedure"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-27 22:49:49 +02:00
|
|
|
#define ckz(x) do { obj _x = (x); if (unlikely(!is_box(_x))) \
|
2023-03-12 00:43:51 +01:00
|
|
|
{ ac = _x; spush((obj)"box, cell, or promise"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-18 19:17:45 +01:00
|
|
|
#define ckg(x) do { obj _x = (x); if (unlikely(!isintegrable(_x))) \
|
|
|
|
{ ac = _x; spush((obj)"integrable entry"); musttail return cxi_failactype(IARGS); } } while (0)
|
2024-07-17 23:47:08 +02:00
|
|
|
#define cksb(x) do { obj _x = (x); if (unlikely(!is_shebang(_x))) \
|
|
|
|
{ ac = _x; spush((obj)"directive"); musttail return cxi_failactype(IARGS); } } while (0)
|
2023-03-07 01:36:15 +01:00
|
|
|
|
|
|
|
|
2023-03-20 04:31:28 +01:00
|
|
|
define_instruction(halt) {
|
|
|
|
unwindi(0);
|
|
|
|
}
|
2023-02-28 06:31:08 +01:00
|
|
|
|
2023-03-26 18:02:36 +02:00
|
|
|
define_instruction(panic) {
|
|
|
|
obj l, p; cks(ac); ckl(sref(0));
|
|
|
|
p = oport_file_obj(stderr);
|
|
|
|
fprintf(stderr, "error: %s", stringchars(ac));
|
2023-03-27 22:49:49 +02:00
|
|
|
if (is_pair(sref(0))) fputs(":\n", stderr);
|
2023-03-26 18:02:36 +02:00
|
|
|
else fputs("\n", stderr);
|
2023-03-27 22:49:49 +02:00
|
|
|
for (l = sref(0); is_pair(l); l = pair_cdr(l)) {
|
|
|
|
oportputcircular(pair_car(l), p, 0);
|
2023-03-26 18:02:36 +02:00
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
sdrop(1);
|
|
|
|
ac = void_obj();
|
|
|
|
unwindi(0);
|
|
|
|
}
|
|
|
|
|
2023-03-30 22:09:25 +02:00
|
|
|
define_instruction(abort) {
|
|
|
|
/* exit code ignored */
|
|
|
|
abort();
|
|
|
|
unwindi(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(exit) {
|
|
|
|
int excode;
|
|
|
|
if (ac == bool_obj(0)) excode = 1;
|
|
|
|
else if (ac == bool_obj(1)) excode = 0;
|
|
|
|
else if (is_fixnum(ac)) excode = (int)get_fixnum(ac);
|
|
|
|
else excode = 1;
|
|
|
|
exit(excode);
|
|
|
|
unwindi(0);
|
|
|
|
}
|
|
|
|
|
2023-03-20 04:31:28 +01:00
|
|
|
define_instruction(lit) {
|
|
|
|
ac = *ip++;
|
|
|
|
gonexti();
|
|
|
|
}
|
2023-02-28 06:31:08 +01:00
|
|
|
|
2023-03-20 04:31:28 +01:00
|
|
|
define_instruction(sref) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int i = get_fixnum(*ip++);
|
2023-03-20 04:31:28 +01:00
|
|
|
ac = sref(i);
|
|
|
|
gonexti();
|
|
|
|
}
|
2023-02-28 06:31:08 +01:00
|
|
|
|
2023-03-20 04:31:28 +01:00
|
|
|
define_instruction(dref) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int i = get_fixnum(*ip++);
|
2023-03-20 04:31:28 +01:00
|
|
|
ac = dref(i);
|
|
|
|
gonexti();
|
|
|
|
}
|
2023-02-28 06:31:08 +01:00
|
|
|
|
2023-03-20 04:31:28 +01:00
|
|
|
define_instruction(gref) {
|
|
|
|
obj p = *ip++;
|
|
|
|
ac = gref(p);
|
|
|
|
gonexti();
|
|
|
|
}
|
2023-02-28 06:31:08 +01:00
|
|
|
|
2023-03-20 04:31:28 +01:00
|
|
|
define_instruction(iref) {
|
2023-03-29 00:14:45 +02:00
|
|
|
ac = box_ref(ac);
|
2023-03-20 04:31:28 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
2023-03-12 00:43:51 +01:00
|
|
|
|
2023-03-20 04:31:28 +01:00
|
|
|
define_instruction(iset) {
|
2023-03-29 00:14:45 +02:00
|
|
|
box_ref(ac) = spop();
|
2023-03-20 04:31:28 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
define_instruction(dclose) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int i, n = get_fixnum(*ip++), c = n+1;
|
2023-03-03 21:44:12 +01:00
|
|
|
hp_reserve(vmclobsz(c));
|
2023-02-28 06:31:08 +01:00
|
|
|
for (i = n-1; i >= 0; --i) *--hp = sref(i); /* display */
|
|
|
|
*--hp = *ip++; /* code */
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = hend_vmclo(c); /* closure */
|
2023-02-28 06:31:08 +01:00
|
|
|
sdrop(n);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sbox) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int i = get_fixnum(*ip++);
|
2023-03-26 06:16:52 +02:00
|
|
|
hp_reserve(boxbsz());
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(i);
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(i) = hend_box();
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-20 04:31:28 +01:00
|
|
|
define_instruction(br) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int dx = get_fixnum(*ip++);
|
2023-03-20 04:31:28 +01:00
|
|
|
ip += dx;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(brt) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int dx = get_fixnum(*ip++);
|
2023-03-20 04:31:28 +01:00
|
|
|
if (ac) ip += dx;
|
|
|
|
gonexti();
|
|
|
|
}
|
2023-02-28 06:31:08 +01:00
|
|
|
|
2023-03-20 04:31:28 +01:00
|
|
|
define_instruction(brnot) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int dx = get_fixnum(*ip++);
|
2023-03-20 04:31:28 +01:00
|
|
|
if (!ac) ip += dx;
|
|
|
|
gonexti();
|
|
|
|
}
|
2023-02-28 06:31:08 +01:00
|
|
|
|
2023-03-20 04:31:28 +01:00
|
|
|
define_instruction(andbo) {
|
|
|
|
if (ac) { /* go to next binary instruction w/2 args */
|
|
|
|
ac = spop();
|
|
|
|
} else { /* skip the next instruction, drop its args */
|
|
|
|
sdrop(2);
|
|
|
|
ip += 1;
|
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
define_instruction(sseti) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int i = get_fixnum(*ip++);
|
2023-03-29 00:14:45 +02:00
|
|
|
box_ref(sref(i)) = ac;
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(dseti) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int i = get_fixnum(*ip++);
|
2023-03-29 00:14:45 +02:00
|
|
|
box_ref(dref(i)) = ac;
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-12 21:54:44 +01:00
|
|
|
define_instruction(gloc) {
|
|
|
|
ac = *ip++;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(gset) {
|
|
|
|
obj p = *ip++;
|
|
|
|
gref(p) = ac;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(appl) {
|
|
|
|
int n, i; obj l = spop(), t = l;
|
2023-03-27 22:49:49 +02:00
|
|
|
for (n = 0; is_pair(t); t = pair_cdr(t)) ++n; sgrow(n);
|
|
|
|
for (i = 0; i < n; ++i, l = pair_cdr(l)) sref(i) = pair_car(l);
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0);
|
|
|
|
ac = fixnum_obj(n); /* argc */
|
2023-02-28 06:31:08 +01:00
|
|
|
callsubi();
|
|
|
|
}
|
|
|
|
|
2023-03-06 21:53:37 +01:00
|
|
|
define_instruction(cwmv) {
|
2023-03-11 21:40:06 +01:00
|
|
|
obj t = ac, x = spop();
|
|
|
|
ckx(t); ckx(x);
|
2023-03-30 07:27:28 +02:00
|
|
|
/* we can run in constant space in some situations */
|
|
|
|
if (vmcloref(x, 0) == cx_continuation_2Dadapter_2Dcode
|
|
|
|
&& vmcloref(x, 1) == cx__2Adynamic_2Dstate_2A) {
|
2023-03-11 21:40:06 +01:00
|
|
|
/* arrange call of t with x as continuation */
|
2023-03-30 07:27:28 +02:00
|
|
|
/* [0] adapter_code, [1] dynamic_state */
|
|
|
|
int n = vmclolen(x) - 2;
|
2023-03-11 21:40:06 +01:00
|
|
|
assert((cxg_rend - cxg_regs - VM_REGC) > n);
|
|
|
|
sp = r + VM_REGC; /* stack is empty */
|
2023-03-30 07:27:28 +02:00
|
|
|
objcpy(sp, &vmcloref(x, 2), n);
|
2023-03-11 21:40:06 +01:00
|
|
|
sp += n; /* contains n elements now */
|
2023-03-26 06:16:52 +02:00
|
|
|
rd = t; rx = fixnum_obj(0);
|
|
|
|
ac = fixnum_obj(0);
|
2023-03-11 21:40:06 +01:00
|
|
|
callsubi();
|
|
|
|
} else {
|
|
|
|
/* arrange return to cwmv code w/x */
|
|
|
|
spush(x);
|
|
|
|
spush(cx_callmv_2Dadapter_2Dclosure);
|
2023-03-26 06:16:52 +02:00
|
|
|
spush(fixnum_obj(0));
|
2023-03-11 21:40:06 +01:00
|
|
|
/* call the producer */
|
2023-03-26 06:16:52 +02:00
|
|
|
rd = t; rx = fixnum_obj(0); ac = fixnum_obj(0);
|
2023-03-11 21:40:06 +01:00
|
|
|
callsubi();
|
|
|
|
}
|
2023-03-06 21:53:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(rcmv) {
|
|
|
|
/* single-value producer call returns here with result in ac, cns on stack */
|
2023-03-11 21:40:06 +01:00
|
|
|
obj val = ac, x = spop();
|
2023-03-29 01:32:54 +02:00
|
|
|
/* tail-call the consumer with the returned value(s) */
|
2023-03-29 01:51:37 +02:00
|
|
|
if (is_unit(val)) { /* (values) in improper context */
|
2023-03-29 01:32:54 +02:00
|
|
|
ac = fixnum_obj(0);
|
|
|
|
} else if (is_tuple(val)) { /* (values a1 a2 a ...) in improper context */
|
|
|
|
int n = tuple_len(val), i;
|
|
|
|
for (i = n-1; i >= 0; --i) spush(tuple_ref(val, i));
|
|
|
|
ac = fixnum_obj(n);
|
|
|
|
} else { /* regular single value */
|
|
|
|
spush(val); ac = fixnum_obj(1);
|
|
|
|
}
|
2023-03-26 06:16:52 +02:00
|
|
|
rd = x; rx = fixnum_obj(0);
|
2023-03-10 23:30:41 +01:00
|
|
|
callsubi();
|
2023-03-06 21:53:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sdmv) {
|
|
|
|
/* sending values on stack, ac contains argc */
|
2023-03-26 06:16:52 +02:00
|
|
|
if (ac == fixnum_obj(1)) {
|
2023-03-06 21:53:37 +01:00
|
|
|
/* can return anywhere, including rcmv */
|
|
|
|
ac = spop();
|
|
|
|
rx = spop();
|
|
|
|
rd = spop();
|
|
|
|
retfromi();
|
|
|
|
} else {
|
|
|
|
/* can only pseudo-return to rcmv */
|
2023-03-27 22:49:49 +02:00
|
|
|
int n = get_fixnum(ac), m = 3, i;
|
2023-03-26 06:16:52 +02:00
|
|
|
if (sref(n) == fixnum_obj(0) && sref(n+1) == cx_callmv_2Dadapter_2Dclosure) {
|
2023-03-06 21:53:37 +01:00
|
|
|
/* tail-call the consumer with the produced values */
|
2023-03-26 06:16:52 +02:00
|
|
|
rd = sref(n+2); rx = fixnum_obj(0); /* cns */
|
2023-03-06 21:53:37 +01:00
|
|
|
/* NB: can be sped up for popular cases: n == 0, n == 2 */
|
2023-03-30 01:02:18 +02:00
|
|
|
objmove_left(sp-n-m, sp-n, n);
|
2023-03-06 21:53:37 +01:00
|
|
|
sdrop(m); callsubi();
|
2023-03-29 01:51:37 +02:00
|
|
|
} else if (n == 0) { /* return unit (n = 0) */
|
|
|
|
ac = unit_obj();
|
2023-03-29 01:32:54 +02:00
|
|
|
rx = spop();
|
|
|
|
rd = spop();
|
|
|
|
retfromi();
|
2023-03-29 01:51:37 +02:00
|
|
|
} else { /* return args as tuple (n > 1) */
|
2023-03-12 22:32:16 +01:00
|
|
|
hp_reserve(tuplebsz(n));
|
|
|
|
for (i = n-1; i >= 0; --i) *--hp = sref(i);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = hend_tuple(n);
|
2023-03-12 22:32:16 +01:00
|
|
|
sdrop(n);
|
|
|
|
rx = spop();
|
|
|
|
rd = spop();
|
|
|
|
retfromi();
|
2023-03-06 21:53:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-10 23:30:41 +01:00
|
|
|
define_instruction(lck) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int m = get_fixnum(*ip++);
|
2023-03-10 23:30:41 +01:00
|
|
|
int n; cki(sref(m)); ckx(sref(m+1));
|
|
|
|
n = (int)(sp-m-(r+VM_REGC));
|
2023-03-30 05:18:39 +02:00
|
|
|
hp_reserve(vmclobsz(n+2));
|
2023-03-30 01:02:18 +02:00
|
|
|
hp -= n; objcpy(hp, sp-n-m, n);
|
2023-03-30 05:18:39 +02:00
|
|
|
/* [0] adapter_code, [1] dynamic_state */
|
|
|
|
*--hp = cx__2Adynamic_2Dstate_2A;
|
2023-03-11 07:50:00 +01:00
|
|
|
*--hp = cx_continuation_2Dadapter_2Dcode;
|
2023-03-30 05:18:39 +02:00
|
|
|
ac = hend_vmclo(n+2);
|
2023-03-10 23:30:41 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(lck0) {
|
2023-03-30 05:18:39 +02:00
|
|
|
int n; cki(sref(0)); ckx(sref(1));
|
2023-03-10 23:30:41 +01:00
|
|
|
n = (int)(sp-(r+VM_REGC));
|
2023-03-30 05:18:39 +02:00
|
|
|
hp_reserve(vmclobsz(n+2));
|
|
|
|
hp -= n; objcpy(hp, sp-n, n);
|
|
|
|
/* [0] adapter_code, [1] dynamic_state */
|
|
|
|
*--hp = cx__2Adynamic_2Dstate_2A;
|
2023-03-11 07:50:00 +01:00
|
|
|
*--hp = cx_continuation_2Dadapter_2Dcode;
|
2023-03-30 05:18:39 +02:00
|
|
|
ac = hend_vmclo(n+2);
|
2023-03-10 23:30:41 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(wck) {
|
2023-03-30 07:27:28 +02:00
|
|
|
obj x = ac, t = spop(); ckx(t); ckx(x);
|
2023-03-11 07:50:00 +01:00
|
|
|
if (vmcloref(x, 0) != cx_continuation_2Dadapter_2Dcode)
|
|
|
|
failactype("continuation");
|
2023-03-30 05:18:39 +02:00
|
|
|
/* [0] adapter_code, [1] dynamic_state */
|
2023-03-30 07:27:28 +02:00
|
|
|
if (vmcloref(x, 1) == cx__2Adynamic_2Dstate_2A) {
|
|
|
|
/* restore cont stack and invoke t there */
|
|
|
|
int n = vmclolen(x) - 2;
|
|
|
|
assert((cxg_rend - cxg_regs - VM_REGC) > n);
|
|
|
|
sp = r + VM_REGC; /* stack is empty */
|
|
|
|
objcpy(sp, &vmcloref(x, 2), n);
|
|
|
|
sp += n; /* contains n elements now */
|
|
|
|
rd = t; rx = fixnum_obj(0);
|
|
|
|
ac = fixnum_obj(0);
|
|
|
|
callsubi();
|
|
|
|
} else {
|
|
|
|
/* have to arrange call of cont adapter */
|
|
|
|
spush(x);
|
|
|
|
spush(cx_callmv_2Dadapter_2Dclosure);
|
|
|
|
spush(fixnum_obj(0));
|
|
|
|
/* call the thunk as producer */
|
|
|
|
rd = t; rx = fixnum_obj(0);
|
|
|
|
ac = fixnum_obj(0);
|
|
|
|
callsubi();
|
|
|
|
}
|
2023-03-10 23:30:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(wckr) {
|
2023-03-30 07:27:28 +02:00
|
|
|
obj x = ac, o = spop(); ckx(x);
|
2023-03-11 07:50:00 +01:00
|
|
|
if (vmcloref(x, 0) != cx_continuation_2Dadapter_2Dcode)
|
|
|
|
failactype("continuation");
|
2023-03-30 05:18:39 +02:00
|
|
|
/* [0] adapter_code, [1] dynamic_state */
|
2023-03-30 07:27:28 +02:00
|
|
|
if (vmcloref(x, 1) == cx__2Adynamic_2Dstate_2A) {
|
|
|
|
/* restore cont stack and return o there */
|
|
|
|
int n = vmclolen(x) - 2;
|
|
|
|
assert((cxg_rend - cxg_regs - VM_REGC) > n);
|
|
|
|
sp = r + VM_REGC; /* stack is empty */
|
|
|
|
objcpy(sp, &vmcloref(x, 2), n);
|
|
|
|
sp += n;
|
|
|
|
ac = o;
|
|
|
|
rx = spop();
|
|
|
|
rd = spop();
|
|
|
|
retfromi();
|
|
|
|
} else {
|
|
|
|
/* have to arrange call of cont adapter */
|
|
|
|
spush(o);
|
|
|
|
rd = x; rx = fixnum_obj(0);
|
|
|
|
ac = fixnum_obj(1);
|
|
|
|
callsubi();
|
|
|
|
}
|
2023-03-10 23:30:41 +01:00
|
|
|
}
|
|
|
|
|
2023-03-30 05:18:39 +02:00
|
|
|
|
2023-03-11 07:50:00 +01:00
|
|
|
define_instruction(rck) {
|
2023-03-30 05:18:39 +02:00
|
|
|
/* called with continuation as rd:
|
|
|
|
* in: ac:argc, args on stack, rd display is dys, saved stack */
|
|
|
|
if (vmcloref(rd, 1) != cx__2Adynamic_2Dstate_2A) {
|
|
|
|
/* need to run the rest of the code to unwind/rewind on the
|
|
|
|
* old stack; rck will be called again when done */
|
|
|
|
gonexti();
|
|
|
|
} else if (ac == fixnum_obj(1)) { /* easy, popular case */
|
2023-03-11 07:50:00 +01:00
|
|
|
ac = rd;
|
|
|
|
goi(wckr);
|
|
|
|
} else { /* multiple results case */
|
2023-03-30 05:18:39 +02:00
|
|
|
/* rd[0] adapter_code, rd[1] dynamic_state */
|
|
|
|
int c = get_fixnum(ac), n = vmclolen(rd) - 2, i;
|
|
|
|
obj *ks = &vmcloref(rd, 2), *ke = ks + n;
|
2023-03-26 06:16:52 +02:00
|
|
|
if (ke-ks > 3 && *--ke == fixnum_obj(0) && *--ke == cx_callmv_2Dadapter_2Dclosure) {
|
2023-03-11 07:50:00 +01:00
|
|
|
obj *sb = r + VM_REGC;
|
2023-04-13 23:59:31 +02:00
|
|
|
rd = *--ke; rx = fixnum_obj(0); n = (int)(ke - ks); /* cns */
|
2023-03-11 07:50:00 +01:00
|
|
|
/* arrange stack as follows: [ks..ke] [arg ...] */
|
|
|
|
assert((cxg_rend - cxg_regs - VM_REGC) > n + c);
|
2023-03-30 01:02:18 +02:00
|
|
|
if (c) objmove(sb+n, sp-c, c);
|
|
|
|
objcpy(sb, ks, n);
|
2023-03-11 07:50:00 +01:00
|
|
|
sp = sb+n+c; callsubi();
|
2023-03-29 01:51:37 +02:00
|
|
|
} else if (c == 0) { /* return unit (n = 0) */
|
|
|
|
spush(unit_obj());
|
2023-03-29 01:32:54 +02:00
|
|
|
ac = rd;
|
|
|
|
goi(wckr);
|
|
|
|
} else { /* return args as tuple (n > 1) */
|
2023-03-12 22:32:16 +01:00
|
|
|
hp_reserve(tuplebsz(c));
|
|
|
|
for (i = c-1; i >= 0; --i) *--hp = sref(i);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = hend_tuple(c);
|
2023-03-12 22:32:16 +01:00
|
|
|
sdrop(c);
|
|
|
|
spush(ac);
|
|
|
|
ac = rd;
|
|
|
|
goi(wckr);
|
2023-03-11 07:50:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 05:18:39 +02:00
|
|
|
define_instruction(dys) {
|
|
|
|
ac = cx__2Adynamic_2Dstate_2A;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(setdys) {
|
|
|
|
cx__2Adynamic_2Dstate_2A = ac;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(save) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int dx = get_fixnum(*ip++);
|
2023-02-28 06:31:08 +01:00
|
|
|
spush(rd);
|
2023-03-29 00:14:45 +02:00
|
|
|
spush(fixnum_obj(ip + dx - &vector_ref(vmcloref(rd, 0), 0)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(push) { spush(ac); gonexti(); }
|
|
|
|
|
2023-03-07 19:11:46 +01:00
|
|
|
define_instruction(jdceq) {
|
|
|
|
obj v = *ip++, i = *ip++;
|
|
|
|
if (ac == v) {
|
2023-03-27 22:49:49 +02:00
|
|
|
rd = dref(get_fixnum(i));
|
2023-03-26 06:16:52 +02:00
|
|
|
rx = fixnum_obj(0);
|
2023-03-07 19:11:46 +01:00
|
|
|
callsubi();
|
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jdcge) {
|
|
|
|
obj v = *ip++, i = *ip++;
|
|
|
|
if (ac >= v) { /* unsigned tagged fixnums can be compared as-is */
|
2023-03-27 22:49:49 +02:00
|
|
|
rd = dref(get_fixnum(i));
|
2023-03-26 06:16:52 +02:00
|
|
|
rx = fixnum_obj(0);
|
2023-03-07 19:11:46 +01:00
|
|
|
callsubi();
|
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jdref) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int i = get_fixnum(*ip++);
|
2023-03-07 19:42:29 +01:00
|
|
|
rd = dref(i);
|
2023-03-26 06:16:52 +02:00
|
|
|
rx = fixnum_obj(0);
|
2023-03-07 19:11:46 +01:00
|
|
|
callsubi();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(call) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int n = get_fixnum(*ip++);
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0);
|
|
|
|
ac = fixnum_obj(n); /* argc */
|
2023-02-28 06:31:08 +01:00
|
|
|
callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int m = get_fixnum(*ip++), n = get_fixnum(*ip++);
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0);
|
|
|
|
ac = fixnum_obj(n); /* argc */
|
2023-03-30 01:02:18 +02:00
|
|
|
objmove_left(sp-n-m, sp-n, n);
|
2023-02-28 06:31:08 +01:00
|
|
|
sdrop(m);
|
|
|
|
callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(return) {
|
|
|
|
rx = spop();
|
|
|
|
rd = spop();
|
|
|
|
retfromi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sreturn) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int n = get_fixnum(*ip++);
|
2023-02-28 06:31:08 +01:00
|
|
|
sdrop(n);
|
|
|
|
rx = spop();
|
|
|
|
rd = spop();
|
|
|
|
retfromi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(adrop) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int n = get_fixnum(*ip++);
|
2023-02-28 06:31:08 +01:00
|
|
|
sdrop(n);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-20 04:31:28 +01:00
|
|
|
define_instruction(pop) {
|
|
|
|
ac = spop();
|
|
|
|
gonexti();
|
|
|
|
}
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
define_instruction(atest) {
|
|
|
|
obj no = *ip++;
|
|
|
|
if (unlikely(ac != no)) fail("argument count error on entry");
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(shrarg) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int n = get_fixnum(*ip++), c = get_fixnum(ac), m = c-n;
|
2023-02-28 06:31:08 +01:00
|
|
|
/* rest arg ends up on top */
|
|
|
|
if (unlikely(m < 0)) {
|
2023-03-07 01:36:15 +01:00
|
|
|
fail("argument count error on entry (not enough arguments)");
|
2023-02-28 06:31:08 +01:00
|
|
|
} else if (unlikely(m == 0)) {
|
2023-03-26 06:38:50 +02:00
|
|
|
spush(null_obj());
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
2023-03-26 06:38:50 +02:00
|
|
|
obj l = null_obj();
|
2023-03-26 06:16:52 +02:00
|
|
|
hp_reserve(pairbsz()*m);
|
2023-02-28 06:31:08 +01:00
|
|
|
while (m > 0) {
|
|
|
|
*--hp = l; *--hp = sref(n + m - 1);
|
2023-03-26 06:16:52 +02:00
|
|
|
l = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
--m;
|
|
|
|
}
|
2023-03-30 01:02:18 +02:00
|
|
|
objmove_left(sp-c, sp-n, n);
|
2023-02-28 06:31:08 +01:00
|
|
|
sdrop(c-n);
|
|
|
|
spush(l);
|
|
|
|
}
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = fixnum_obj(n+1);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-07 19:11:46 +01:00
|
|
|
define_instruction(aerr) {
|
|
|
|
fail("argument count error on entry");
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-20 04:31:28 +01:00
|
|
|
define_instruction(shlit) {
|
|
|
|
spush(ac);
|
|
|
|
ac = *ip++;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(shi0) {
|
|
|
|
spush(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = fixnum_obj(0);
|
2023-03-20 04:31:28 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* type checks */
|
|
|
|
|
2023-03-12 00:43:51 +01:00
|
|
|
define_instruction(ckp) { ckp(ac); gonexti(); }
|
|
|
|
define_instruction(ckl) { ckl(ac); gonexti(); }
|
|
|
|
define_instruction(ckv) { ckv(ac); gonexti(); }
|
|
|
|
define_instruction(ckc) { ckc(ac); gonexti(); }
|
|
|
|
define_instruction(cks) { cks(ac); gonexti(); }
|
|
|
|
define_instruction(cki) { cki(ac); gonexti(); }
|
|
|
|
define_instruction(ckj) { ckj(ac); gonexti(); }
|
|
|
|
define_instruction(ckn) { ckn(ac); gonexti(); }
|
|
|
|
define_instruction(ckk) { ckk(ac); gonexti(); }
|
|
|
|
define_instruction(cky) { cky(ac); gonexti(); }
|
|
|
|
define_instruction(ckr) { ckr(ac); gonexti(); }
|
|
|
|
define_instruction(ckw) { ckw(ac); gonexti(); }
|
|
|
|
define_instruction(ckx) { ckx(ac); gonexti(); }
|
|
|
|
define_instruction(ckz) { ckz(ac); gonexti(); }
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* integrable instructions */
|
|
|
|
|
|
|
|
define_instruction(isq) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(x == y);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(isv) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(iseqv(x,y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ise) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(isequal(x,y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-29 00:14:45 +02:00
|
|
|
define_instruction(unbox) {
|
|
|
|
ckz(ac);
|
|
|
|
ac = box_ref(ac);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(setbox) {
|
|
|
|
ckz(ac);
|
|
|
|
box_ref(ac) = spop();
|
|
|
|
gonexti();
|
|
|
|
}
|
2023-03-12 00:43:51 +01:00
|
|
|
|
|
|
|
define_instruction(box) {
|
2023-03-26 06:16:52 +02:00
|
|
|
hp_reserve(boxbsz());
|
2023-03-12 00:43:51 +01:00
|
|
|
*--hp = ac;
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = hend_box();
|
2023-03-12 00:43:51 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-27 22:49:49 +02:00
|
|
|
define_instruction(car) { ckp(ac); ac = pair_car(ac); gonexti(); }
|
|
|
|
define_instruction(cdr) { ckp(ac); ac = pair_cdr(ac); gonexti(); }
|
|
|
|
define_instruction(setcar) { ckp(ac); pair_car(ac) = spop(); gonexti(); }
|
|
|
|
define_instruction(setcdr) { ckp(ac); pair_cdr(ac) = spop(); gonexti(); }
|
2023-03-12 00:43:51 +01:00
|
|
|
|
2023-03-27 22:49:49 +02:00
|
|
|
define_instruction(caar) { ckp(ac); ac = pair_car(ac); ckp(ac); ac = pair_car(ac); gonexti(); }
|
|
|
|
define_instruction(cadr) { ckp(ac); ac = pair_cdr(ac); ckp(ac); ac = pair_car(ac); gonexti(); }
|
|
|
|
define_instruction(cdar) { ckp(ac); ac = pair_car(ac); ckp(ac); ac = pair_cdr(ac); gonexti(); }
|
|
|
|
define_instruction(cddr) { ckp(ac); ac = pair_cdr(ac); ckp(ac); ac = pair_cdr(ac); gonexti(); }
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
define_instruction(nullp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_null(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(pairp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_pair(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cons) {
|
2023-03-26 06:16:52 +02:00
|
|
|
hp_reserve(pairbsz());
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = spop(); /* cdr */
|
|
|
|
*--hp = ac; /* car */
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(listp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_list(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(list) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int i, n = get_fixnum(*ip++);
|
2023-03-26 06:16:52 +02:00
|
|
|
hp_reserve(pairbsz()*n);
|
2023-03-26 06:38:50 +02:00
|
|
|
for (ac = null_obj(), i = n-1; i >= 0; --i) {
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = ac; /* cdr */
|
|
|
|
*--hp = sref(i); /* car */
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
sdrop(n);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-21 18:43:26 +01:00
|
|
|
define_instruction(lmk) {
|
|
|
|
int i, n; obj v; ckk(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
n = get_fixnum(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
hp_reserve(pairbsz()*n); v = sref(0);
|
2023-03-26 06:38:50 +02:00
|
|
|
ac = null_obj();
|
2023-03-21 18:43:26 +01:00
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
*--hp = ac; /* cdr */
|
|
|
|
*--hp = v; /* car */
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = hend_pair();
|
2023-03-21 18:43:26 +01:00
|
|
|
}
|
|
|
|
sdrop(1);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(llen) {
|
|
|
|
int n = 0;
|
2023-03-27 22:49:49 +02:00
|
|
|
while (is_pair(ac)) { ac = pair_cdr(ac); ++n; }
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = fixnum_obj(n);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(lget) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = spop(); int c;
|
2023-03-27 22:49:49 +02:00
|
|
|
ckk(x); c = get_fixnum(x);
|
|
|
|
while (c-- > 0) { ckp(ac); ac = pair_cdr(ac); }
|
|
|
|
ckp(ac); ac = pair_car(ac);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(lput) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = spop(); int c;
|
2023-03-27 22:49:49 +02:00
|
|
|
ckk(x); c = get_fixnum(x);
|
|
|
|
while (c-- > 0) { ckp(ac); ac = pair_cdr(ac); }
|
|
|
|
ckp(ac); pair_car(ac) = spop();
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
define_instruction(memq) {
|
|
|
|
obj l = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
for (; is_pair(l); l = pair_cdr(l)) {
|
|
|
|
if (pair_car(l) == ac) { ac = l; gonexti(); }
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(memv) {
|
2024-07-04 00:47:26 +02:00
|
|
|
obj l = spop(); ckl(l);
|
|
|
|
ac = ismemv(ac, l); /* FIXME: inline? */
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(meme) {
|
2024-07-04 00:47:26 +02:00
|
|
|
obj l = spop(); ckl(l);
|
|
|
|
ac = ismember(ac, l); /* FIXME: inline? */
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(assq) {
|
|
|
|
obj l = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
for (; is_pair(l); l = pair_cdr(l)) {
|
|
|
|
obj p = pair_car(l); if (is_pair(p) && pair_car(p) == ac) { ac = p; gonexti(); }
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(assv) {
|
2024-07-04 00:47:26 +02:00
|
|
|
obj l = spop(); ckl(l);
|
|
|
|
ac = isassv(ac, l); /* FIXME: inline? */
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(asse) {
|
2024-07-04 00:47:26 +02:00
|
|
|
obj l = spop(); ckl(l);
|
|
|
|
ac = isassoc(ac, l); /* FIXME: inline? */
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ltail) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = spop(); int c;
|
2023-03-27 22:49:49 +02:00
|
|
|
ckk(x); c = get_fixnum(x);
|
|
|
|
while (c-- > 0) { ckp(ac); ac = pair_cdr(ac); }
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(lpair) {
|
2023-03-26 06:16:52 +02:00
|
|
|
obj l = bool_obj(0);
|
2023-03-27 22:49:49 +02:00
|
|
|
while (is_pair(ac)) { l = ac; ac = pair_cdr(ac); }
|
2023-02-28 06:31:08 +01:00
|
|
|
ac = l;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(lrev) {
|
2023-03-26 06:38:50 +02:00
|
|
|
obj l = ac, o = null_obj(); int n = 0;
|
2023-03-27 22:49:49 +02:00
|
|
|
while (is_pair(ac)) { ac = pair_cdr(ac); ++n; }
|
2023-03-30 05:18:39 +02:00
|
|
|
cku(ac); ac = l;
|
2023-03-26 06:16:52 +02:00
|
|
|
hp_reserve(pairbsz()*n);
|
2023-03-30 05:18:39 +02:00
|
|
|
for (; ac != null_obj(); ac = pair_cdr(ac)) {
|
2023-03-27 22:49:49 +02:00
|
|
|
*--hp = o; *--hp = pair_car(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
o = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
ac = o;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(lrevi) {
|
2023-03-26 06:38:50 +02:00
|
|
|
obj t, v = null_obj();
|
2023-03-27 22:49:49 +02:00
|
|
|
while (is_pair(ac)) t = pair_cdr(ac), pair_cdr(ac) = v, v = ac, ac = t;
|
2023-02-28 06:31:08 +01:00
|
|
|
ac = v;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-28 06:09:25 +02:00
|
|
|
define_instruction(circp) {
|
|
|
|
ac = bool_obj(is_circular(ac));
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(charp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_char(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(strp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_string(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(str) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int i, n = get_fixnum(*ip++);
|
2023-03-26 06:16:52 +02:00
|
|
|
obj o = string_obj(allocstring(n, ' '));
|
2023-02-28 06:31:08 +01:00
|
|
|
unsigned char *s = (unsigned char *)stringchars(o);
|
2023-03-07 01:36:15 +01:00
|
|
|
for (i = 0; i < n; ++i) {
|
2023-03-27 22:49:49 +02:00
|
|
|
obj x = sref(i); ckc(x); s[i] = get_char(x);
|
2023-03-07 01:36:15 +01:00
|
|
|
}
|
2023-02-28 06:31:08 +01:00
|
|
|
sdrop(n); ac = o;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(smk) {
|
2023-03-07 01:36:15 +01:00
|
|
|
int n, c; obj x = spop();
|
|
|
|
ckk(ac); ckc(x);
|
2023-03-27 22:49:49 +02:00
|
|
|
n = get_fixnum(ac), c = get_char(x);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = string_obj(allocstring(n, c));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(slen) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cks(ac);
|
2023-03-29 00:14:45 +02:00
|
|
|
ac = fixnum_obj(string_len(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sget) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = spop(); int i;
|
|
|
|
cks(ac); ckk(x);
|
2023-03-27 22:49:49 +02:00
|
|
|
i = get_fixnum(x);
|
2023-03-29 00:14:45 +02:00
|
|
|
if (i >= string_len(ac)) failtype(x, "valid string index");
|
|
|
|
ac = char_obj(string_ref(ac, i));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sput) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = spop(), y = spop(); int i;
|
|
|
|
cks(ac); ckk(x); ckc(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
i = get_fixnum(x);
|
2023-03-29 00:14:45 +02:00
|
|
|
if (i >= string_len(ac)) failtype(x, "valid string index");
|
|
|
|
string_ref(ac, i) = get_char(y);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scat) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); int *d;
|
|
|
|
cks(x); cks(y);
|
|
|
|
d = stringcat(stringdata(x), stringdata(y));
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = string_obj(d);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ssub) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = spop(), y = spop(); int is, ie, *d;
|
|
|
|
cks(ac); ckk(x); ckk(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
is = get_fixnum(x), ie = get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
if (is > ie) failtype(x, "valid start string index");
|
2023-03-29 00:14:45 +02:00
|
|
|
if (ie > string_len(ac)) failtype(y, "valid end string index");
|
2023-03-07 01:36:15 +01:00
|
|
|
d = substring(stringdata(ac), is, ie);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = string_obj(d);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-26 20:52:30 +02:00
|
|
|
define_instruction(spos) {
|
|
|
|
obj x = ac, y = spop(); char *s, *p;
|
2024-07-18 04:43:19 +02:00
|
|
|
cks(y); s = stringchars(y);
|
|
|
|
if (is_string(x)) {
|
|
|
|
p = strstr(s, stringchars(x));
|
|
|
|
} else {
|
|
|
|
ckc(x);
|
|
|
|
p = strchr(s, get_char(x));
|
|
|
|
}
|
2023-03-26 20:52:30 +02:00
|
|
|
ac = p ? fixnum_obj(p-s) : bool_obj(0);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-28 06:09:25 +02:00
|
|
|
define_instruction(supc) {
|
|
|
|
int *d; char *s; cks(ac);
|
|
|
|
d = dupstring(stringdata(ac));
|
|
|
|
for (s = sdatachars(d); *s; ++s) *s = toupper(*s);
|
|
|
|
ac = string_obj(d);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sdnc) {
|
|
|
|
int *d; char *s; cks(ac);
|
|
|
|
d = dupstring(stringdata(ac));
|
|
|
|
for (s = sdatachars(d); *s; ++s) *s = tolower(*s);
|
|
|
|
ac = string_obj(d);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sflc) {
|
|
|
|
int *d; char *s; cks(ac);
|
|
|
|
d = dupstring(stringdata(ac));
|
|
|
|
for (s = sdatachars(d); *s; ++s) *s = tolower(*s); /* stub */
|
|
|
|
ac = string_obj(d);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-26 19:20:33 +02:00
|
|
|
|
|
|
|
define_instruction(bvecp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_bytevector(ac));
|
2023-03-26 19:20:33 +02:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(bvec) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int i, n = get_fixnum(*ip++);
|
2023-04-02 22:16:43 +02:00
|
|
|
obj o = bytevector_obj(allocbytevector(n));
|
2023-03-26 19:20:33 +02:00
|
|
|
unsigned char *s = (unsigned char *)bytevectorbytes(o);
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
obj x = sref(i); ck8(x); s[i] = byte_from_obj(x);
|
|
|
|
}
|
|
|
|
sdrop(n); ac = o;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(bmk) {
|
|
|
|
int n, b; obj x = spop();
|
|
|
|
ckk(ac); ck8(x);
|
2023-03-27 22:49:49 +02:00
|
|
|
n = get_fixnum(ac), b = byte_from_obj(x);
|
2023-04-02 22:16:43 +02:00
|
|
|
ac = bytevector_obj(makebytevector(n, b));
|
2023-03-26 19:20:33 +02:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(blen) {
|
|
|
|
ckb(ac);
|
2023-03-29 00:14:45 +02:00
|
|
|
ac = fixnum_obj(bytevector_len(ac));
|
2023-03-26 19:20:33 +02:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(bget) {
|
|
|
|
obj x = spop(); int i;
|
|
|
|
ckb(ac); ckk(x);
|
2023-03-27 22:49:49 +02:00
|
|
|
i = get_fixnum(x);
|
2023-03-29 00:14:45 +02:00
|
|
|
if (i >= bytevector_len(ac)) failtype(x, "valid bytevector index");
|
|
|
|
ac = fixnum_obj(bytevector_ref(ac, i));
|
2023-03-26 19:20:33 +02:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(bput) {
|
|
|
|
obj x = spop(), y = spop(); int i;
|
|
|
|
ckb(ac); ckk(x); ck8(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
i = get_fixnum(x);
|
2023-03-29 00:14:45 +02:00
|
|
|
if (i >= bytevector_len(ac)) failtype(x, "valid bytevector index");
|
|
|
|
bytevector_ref(ac, i) = byte_from_obj(y);
|
2023-03-26 19:20:33 +02:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-26 20:21:19 +02:00
|
|
|
define_instruction(bsub) {
|
|
|
|
obj x = spop(), y = spop(); int is, ie, *d;
|
|
|
|
ckb(ac); ckk(x); ckk(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
is = get_fixnum(x), ie = get_fixnum(y);
|
2023-03-26 20:21:19 +02:00
|
|
|
if (is > ie) failtype(x, "valid start bytevector index");
|
2023-03-29 00:14:45 +02:00
|
|
|
if (ie > bytevector_len(ac)) failtype(y, "valid end bytevector index");
|
2023-03-26 20:21:19 +02:00
|
|
|
d = subbytevector(bytevectordata(ac), is, ie);
|
|
|
|
ac = bytevector_obj(d);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(beq) {
|
|
|
|
obj x = ac, y = spop(); ckb(x); ckb(y);
|
|
|
|
ac = bool_obj(bytevectoreq(bytevectordata(x), bytevectordata(y)));
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-29 00:14:45 +02:00
|
|
|
|
2023-03-28 21:39:00 +02:00
|
|
|
define_instruction(recp) {
|
2023-03-29 00:14:45 +02:00
|
|
|
obj x = ac, y = spop();
|
|
|
|
if (is_void(y)) {
|
|
|
|
ac = bool_obj(is_record(ac));
|
|
|
|
} else {
|
|
|
|
ac = bool_obj(is_record(ac) && record_rtd(ac) == y);
|
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(rmk) {
|
|
|
|
int i, n; obj v; ckk(sref(0));
|
|
|
|
n = get_fixnum(sref(0));
|
|
|
|
hp_reserve(recbsz(n)); v = sref(1);
|
|
|
|
for (i = 0; i < n; ++i) *--hp = v;
|
|
|
|
ac = hend_rec(ac, n);
|
|
|
|
sdrop(2);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(rlen) {
|
|
|
|
cko(ac);
|
|
|
|
ac = fixnum_obj(record_len(ac));
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(rget) {
|
|
|
|
obj x = spop(); int i;
|
|
|
|
cko(ac); ckk(x);
|
|
|
|
i = get_fixnum(x);
|
|
|
|
if (i >= record_len(ac)) failtype(x, "valid record index");
|
|
|
|
ac = record_ref(ac, i);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(rput) {
|
|
|
|
obj x = spop(), y = spop(); int i;
|
|
|
|
cko(ac); ckk(x);
|
|
|
|
i = get_fixnum(x);
|
|
|
|
if (i >= record_len(ac)) failtype(x, "valid record index");
|
|
|
|
record_ref(ac, i) = y;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(rrtd) {
|
|
|
|
cko(ac);
|
|
|
|
ac = record_rtd(ac);
|
2023-03-28 21:39:00 +02:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-26 19:20:33 +02:00
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(vecp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_vector(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(vec) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int i, n = get_fixnum(*ip++);
|
2023-03-26 06:16:52 +02:00
|
|
|
hp_reserve(vecbsz(n));
|
2023-02-28 06:31:08 +01:00
|
|
|
for (i = n-1; i >= 0; --i) *--hp = sref(i);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = hend_vec(n);
|
2023-02-28 06:31:08 +01:00
|
|
|
sdrop(n);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(vmk) {
|
2023-03-07 01:36:15 +01:00
|
|
|
int i, n; obj v; ckk(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
n = get_fixnum(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
hp_reserve(vecbsz(n)); v = sref(0);
|
2023-02-28 06:31:08 +01:00
|
|
|
for (i = 0; i < n; ++i) *--hp = v;
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = hend_vec(n);
|
2023-02-28 06:31:08 +01:00
|
|
|
sdrop(1);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(vlen) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckv(ac);
|
2023-03-29 00:14:45 +02:00
|
|
|
ac = fixnum_obj(vector_len(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(vget) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = spop(); int i;
|
|
|
|
ckv(ac); ckk(x);
|
2023-03-27 22:49:49 +02:00
|
|
|
i = get_fixnum(x);
|
2023-03-29 00:14:45 +02:00
|
|
|
if (i >= vector_len(ac)) failtype(x, "valid vector index");
|
|
|
|
ac = vector_ref(ac, i);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(vput) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = spop(), y = spop(); int i;
|
|
|
|
ckv(ac); ckk(x);
|
2023-03-27 22:49:49 +02:00
|
|
|
i = get_fixnum(x);
|
2023-03-29 00:14:45 +02:00
|
|
|
if (i >= vector_len(ac)) failtype(x, "valid vector index");
|
|
|
|
vector_ref(ac, i) = y;
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(vcat) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = sref(0); int n1, n2, n;
|
|
|
|
ckv(x); ckv(y);
|
2023-03-29 00:14:45 +02:00
|
|
|
n1 = vector_len(x), n2 = vector_len(y), n = n1 + n2;
|
2023-03-26 06:16:52 +02:00
|
|
|
hp_reserve(vecbsz(n));
|
2023-03-29 00:14:45 +02:00
|
|
|
/* NB: vector_ref fails to return pointer to empty vector's start */
|
2023-03-30 01:02:18 +02:00
|
|
|
hp -= n2; if (n2) objcpy(hp, &vector_ref(y, 0), n2);
|
|
|
|
hp -= n1; if (n1) objcpy(hp, &vector_ref(x, 0), n1);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = hend_vec(n);
|
2023-03-07 01:36:15 +01:00
|
|
|
sdrop(1);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(vtol) {
|
2023-03-26 06:38:50 +02:00
|
|
|
obj l = null_obj(); int n;
|
2023-03-29 00:14:45 +02:00
|
|
|
ckv(ac); n = vector_len(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
hp_reserve(pairbsz()*n);
|
2023-02-28 06:31:08 +01:00
|
|
|
while (n > 0) {
|
2023-03-29 00:14:45 +02:00
|
|
|
*--hp = l; *--hp = vector_ref(ac, n-1);
|
2023-03-26 06:16:52 +02:00
|
|
|
l = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
--n;
|
|
|
|
}
|
|
|
|
ac = l;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ltov) {
|
|
|
|
obj l = ac; int n = 0, i;
|
2023-03-27 22:49:49 +02:00
|
|
|
while (is_pair(l)) { l = pair_cdr(l); ++n; } cku(l);
|
2023-03-26 06:16:52 +02:00
|
|
|
hp_reserve(vecbsz(n));
|
2023-03-27 22:49:49 +02:00
|
|
|
for (l = ac, i = 0, hp -= n; i < n; ++i, l = pair_cdr(l)) hp[i] = pair_car(l);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = hend_vec(n);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-26 19:20:33 +02:00
|
|
|
define_instruction(ltob) {
|
|
|
|
obj l = ac, o; int n = 0, i; unsigned char *s;
|
2023-03-27 22:49:49 +02:00
|
|
|
while (is_pair(l)) { l = pair_cdr(l); ++n; } cku(l);
|
2023-04-02 22:16:43 +02:00
|
|
|
o = bytevector_obj(allocbytevector(n));
|
2023-03-26 19:20:33 +02:00
|
|
|
s = bytevectorbytes(o);
|
2023-03-27 22:49:49 +02:00
|
|
|
for (i = 0, l = ac; i < n; ++i, l = pair_cdr(l)) {
|
|
|
|
obj x = pair_car(l); ck8(x);
|
|
|
|
s[i] = get_byte(x);
|
2023-03-26 19:20:33 +02:00
|
|
|
}
|
|
|
|
ac = o;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(stol) {
|
2023-03-26 06:38:50 +02:00
|
|
|
obj l = null_obj(); int n;
|
2023-03-29 00:14:45 +02:00
|
|
|
cks(ac); n = string_len(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
hp_reserve(pairbsz()*n);
|
2023-02-28 06:31:08 +01:00
|
|
|
while (n > 0) {
|
2023-03-29 00:14:45 +02:00
|
|
|
*--hp = l; *--hp = char_obj(string_ref(ac, n-1));
|
2023-03-26 06:16:52 +02:00
|
|
|
l = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
--n;
|
|
|
|
}
|
|
|
|
ac = l;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ltos) {
|
|
|
|
obj l = ac; int n = 0, i, *d;
|
2023-03-27 22:49:49 +02:00
|
|
|
while (is_pair(l)) { l = pair_cdr(l); ++n; }
|
2023-02-28 06:31:08 +01:00
|
|
|
d = allocstring(n, ' ');
|
2024-06-15 20:29:16 +02:00
|
|
|
for (i = 0; i < n; ac = pair_cdr(ac), ++i) {
|
|
|
|
obj x = pair_car(ac); ckc(x);
|
|
|
|
sdatachars(d)[i] = get_char(x);
|
|
|
|
}
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = string_obj(d);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ytos) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cky(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = string_obj(newstring(symbolname(get_symbol(ac))));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(stoy) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cks(ac);
|
2023-02-28 06:31:08 +01:00
|
|
|
ac = mksymbol(internsym(stringchars(ac)));
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(itos) {
|
|
|
|
char buf[35], *s = buf + sizeof(buf) - 1;
|
2023-03-07 01:36:15 +01:00
|
|
|
int neg = 0; long num, radix;
|
|
|
|
obj x = ac, y = spop(); cki(x); ckk(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
num = get_fixnum(x), radix = get_fixnum(y);
|
2023-02-28 06:31:08 +01:00
|
|
|
if (num < 0) { neg = 1; num = -num; }
|
2023-03-07 01:36:15 +01:00
|
|
|
if (radix < 2 || radix > 10 + 'z' - 'a') failtype(y, "valid radix");
|
2023-02-28 06:31:08 +01:00
|
|
|
*s = 0;
|
|
|
|
do { int d = num % radix; *--s = d < 10 ? d + '0' : d - 10 + 'a'; }
|
|
|
|
while (num /= radix);
|
|
|
|
if (neg) *--s = '-';
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = string_obj(newstring(s));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(stoi) {
|
2023-03-20 21:43:06 +01:00
|
|
|
char *e, *s; long l, radix;
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cks(x); ckk(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
s = stringchars(x); radix = get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
if (radix < 2 || radix > 10 + 'z' - 'a') failtype(y, "valid radix");
|
2023-02-28 06:31:08 +01:00
|
|
|
if (s[0] == '#' && (s[1] == 'b' || s[1] == 'B')) s += 2, radix = 2;
|
|
|
|
else if (s[0] == '#' && (s[1] == 'o' || s[1] == 'O')) s += 2, radix = 8;
|
|
|
|
else if (s[0] == '#' && (s[1] == 'd' || s[1] == 'D')) s += 2, radix = 10;
|
|
|
|
else if (s[0] == '#' && (s[1] == 'x' || s[1] == 'X')) s += 2, radix = 16;
|
|
|
|
l = (errno = 0, strtol(s, &e, radix));
|
2023-03-26 06:16:52 +02:00
|
|
|
if (errno || l < FIXNUM_MIN || l > FIXNUM_MAX || e == s || *e) ac = bool_obj(0);
|
|
|
|
else ac = fixnum_obj(l);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ctoi) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckc(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(get_char(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(itoc) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cki(ac); /* TODO: range check? */
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = char_obj(get_fixnum(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jtos) {
|
2023-03-07 01:36:15 +01:00
|
|
|
char buf[30], *s; double d; ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
d = get_flonum(ac); sprintf(buf, "%.15g", d);
|
2023-02-28 06:31:08 +01:00
|
|
|
for (s = buf; *s != 0; s++) if (strchr(".eE", *s)) break;
|
|
|
|
if (d != d) strcpy(buf, "+nan.0"); else if (d <= -HUGE_VAL) strcpy(buf, "-inf.0");
|
|
|
|
else if (d >= HUGE_VAL) strcpy(buf, "+inf.0"); else if (*s == 'E') *s = 'e';
|
|
|
|
else if (*s == 0) { *s++ = '.'; *s++ = '0'; *s = 0; }
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = string_obj(newstring(buf));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(stoj) {
|
2023-03-07 01:36:15 +01:00
|
|
|
char *e = "", *s; double d; cks(ac);
|
|
|
|
s = stringchars(ac); errno = 0;
|
2023-02-28 06:31:08 +01:00
|
|
|
if (*s != '+' && *s != '-') d = strtod(s, &e);
|
|
|
|
else if (strcmp_ci(s+1, "inf.0") == 0) d = (*s == '-' ? -HUGE_VAL : HUGE_VAL);
|
|
|
|
else if (strcmp_ci(s+1, "nan.0") == 0) d = HUGE_VAL - HUGE_VAL;
|
|
|
|
else d = strtod(s, &e);
|
2023-03-26 06:16:52 +02:00
|
|
|
if (errno || e == s || *e) ac = bool_obj(0);
|
|
|
|
else ac = flonum_obj(d);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ntos) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (is_fixnum(ac)) goi(itos);
|
2023-03-26 06:16:52 +02:00
|
|
|
if (unlikely(spop() != fixnum_obj(10))) fail("non-10 radix in flonum conversion");
|
2023-02-28 06:31:08 +01:00
|
|
|
goi(jtos);
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ston) {
|
2023-03-07 01:36:15 +01:00
|
|
|
char *s; int radix; long l; double d;
|
|
|
|
obj x = ac, y = spop(); cks(x); ckk(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
s = stringchars(x); radix = get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
if (radix < 2 || radix > 10 + 'z' - 'a') failtype(y, "valid radix");
|
2023-02-28 06:31:08 +01:00
|
|
|
switch (strtofxfl(s, radix, &l, &d)) {
|
2023-03-26 06:16:52 +02:00
|
|
|
case 'e': ac = fixnum_obj(l); break;
|
|
|
|
case 'i': ac = flonum_obj(d); break;
|
|
|
|
default : ac = bool_obj(0); break;
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(not) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(ac == 0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(izerop) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cki(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(ac == fixnum_obj(0));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(iposp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cki(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_fixnum(ac) > 0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(inegp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cki(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_fixnum(ac) < 0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ievnp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cki(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj((get_fixnum(ac) & 1) == 0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ioddp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cki(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj((get_fixnum(ac) & 1) != 0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(iadd) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxadd(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(isub) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxsub(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(imul) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxmul(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(iquo) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
if (y == fixnum_obj(0)) fail("division by zero");
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxquo(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(irem) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
if (y == fixnum_obj(0)) fail("division by zero");
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxrem(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(imin) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = get_fixnum(x) < get_fixnum(y) ? x : y;
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(imax) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = get_fixnum(x) > get_fixnum(y) ? x : y;
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ineg) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cki(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxneg(get_fixnum(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(iabs) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cki(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxabs(get_fixnum(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(itoj) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cki(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj((double)get_fixnum(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(idiv) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
if (y == fixnum_obj(0)) fail("division by zero");
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxdiv(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(imqu) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
if (y == fixnum_obj(0)) fail("division by zero");
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxmqu(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(imlo) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
if (y == fixnum_obj(0)) fail("division by zero");
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxmlo(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ieuq) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
if (y == fixnum_obj(0)) fail("division by zero");
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxeuq(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ieur) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
if (y == fixnum_obj(0)) fail("division by zero");
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxeur(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(igcd) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxgcd(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ipow) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxpow(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(isqrt) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cki(ac); /* TODO: check for negative */
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxsqrt(get_fixnum(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(inot) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cki(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(~get_fixnum(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(iand) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(get_fixnum(x) & get_fixnum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(iior) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(get_fixnum(x) | get_fixnum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ixor) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(get_fixnum(x) ^ get_fixnum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(iasl) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxasl(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(iasr) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxasr(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ilt) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_fixnum(x) < get_fixnum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(igt) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_fixnum(x) > get_fixnum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ile) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_fixnum(x) <= get_fixnum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ige) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_fixnum(x) >= get_fixnum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ieq) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(x == y);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ine) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cki(x); cki(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(x != y);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
define_instruction(jzerop) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_flonum(ac) == 0.0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jposp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_flonum(ac) > 0.0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jnegp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_flonum(ac) < 0.0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jevnp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
double f; ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
f = get_flonum(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(flisint(f / 2.0));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(joddp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
double f; ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
f = get_flonum(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(flisint((f + 1.0) / 2.0));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jintp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
double f; ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
f = get_flonum(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(f > -HUGE_VAL && f < HUGE_VAL && f == floor(f));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jnanp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
double f; ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
f = get_flonum(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(f != f);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jfinp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
double f; ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
f = get_flonum(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(f > -HUGE_VAL && f < HUGE_VAL);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jinfp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
double f; ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
f = get_flonum(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(f <= -HUGE_VAL || f >= HUGE_VAL);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jadd) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(get_flonum(x) + get_flonum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jsub) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(get_flonum(x) - get_flonum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jmul) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(get_flonum(x) * get_flonum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jdiv) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(get_flonum(x) / get_flonum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jlt) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_flonum(x) < get_flonum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jgt) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_flonum(x) > get_flonum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jle) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_flonum(x) <= get_flonum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jge) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_flonum(x) >= get_flonum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jeq) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_flonum(x) == get_flonum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jne) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_flonum(x) != get_flonum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jmin) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = get_flonum(x) < get_flonum(y) ? x : y;
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jmax) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = get_flonum(x) > get_flonum(y) ? x : y;
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jneg) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(-get_flonum(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jabs) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(fabs(get_flonum(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-25 16:30:30 +01:00
|
|
|
define_instruction(jgcd) {
|
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(flgcd(get_flonum(x), get_flonum(y)));
|
2023-03-25 16:30:30 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jpow) {
|
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(pow(get_flonum(x), get_flonum(y)));
|
2023-03-25 16:30:30 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jsqrt) {
|
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(sqrt(get_flonum(ac)));
|
2023-03-25 16:30:30 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(jtoi) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxflo(get_flonum(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jquo) {
|
2023-04-03 17:31:56 +02:00
|
|
|
obj x = ac, y = spop(); double n, d, i;
|
|
|
|
ckj(x); ckj(y);
|
|
|
|
n = get_flonum(x), d = get_flonum(y); modf(n/d, &i);
|
|
|
|
ac = flonum_obj(i);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-04-03 17:31:56 +02:00
|
|
|
define_instruction(jrem) {
|
|
|
|
/* NB: we keep sign: (flremainder -10.0 2.0) => -0.0 */
|
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
|
|
|
ac = flonum_obj(fmod(get_flonum(x), get_flonum(y)));
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(jmqu) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(flmqu(get_flonum(x), get_flonum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jmlo) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckj(x); ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(flmlo(get_flonum(x), get_flonum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jfloor) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(floor(get_flonum(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jceil) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(ceil(get_flonum(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jtrunc) {
|
2023-04-03 17:31:56 +02:00
|
|
|
double i; ckj(ac);
|
|
|
|
modf(get_flonum(ac), &i);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(i);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jround) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(flround(get_flonum(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-25 20:06:47 +01:00
|
|
|
define_instruction(jexp) {
|
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(exp(get_flonum(ac)));
|
2023-03-25 20:06:47 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jlog) {
|
|
|
|
obj x = ac, y = spop(); ckj(ac);
|
|
|
|
if (likely(!y)) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(log(get_flonum(ac)));
|
2023-03-25 20:06:47 +01:00
|
|
|
} else {
|
2023-03-27 22:49:49 +02:00
|
|
|
double b; ckj(y); b = get_flonum(y);
|
|
|
|
if (likely(b == 10.0)) ac = flonum_obj(log10(get_flonum(ac)));
|
|
|
|
else ac = flonum_obj(log(get_flonum(ac))/log(b));
|
2023-03-25 20:06:47 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jsin) {
|
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(sin(get_flonum(ac)));
|
2023-03-25 20:06:47 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jcos) {
|
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(cos(get_flonum(ac)));
|
2023-03-25 20:06:47 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jtan) {
|
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(tan(get_flonum(ac)));
|
2023-03-25 20:06:47 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jasin) {
|
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(asin(get_flonum(ac)));
|
2023-03-25 20:06:47 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jacos) {
|
|
|
|
ckj(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(acos(get_flonum(ac)));
|
2023-03-25 20:06:47 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(jatan) {
|
|
|
|
obj x = ac, y = spop(); ckj(ac);
|
|
|
|
if (likely(!y)) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(atan(get_flonum(x)));
|
2023-03-25 20:06:47 +01:00
|
|
|
} else {
|
|
|
|
ckj(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = flonum_obj(atan2(get_flonum(x), get_flonum(y)));
|
2023-03-25 20:06:47 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
define_instruction(zerop) {
|
|
|
|
obj x = ac;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_fixnum(x))) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(x == fixnum_obj(0));
|
2023-03-27 22:49:49 +02:00
|
|
|
} else if (likely(is_flonum(x))) {
|
|
|
|
ac = bool_obj(get_flonum(x) == 0.0);
|
2023-03-07 01:36:15 +01:00
|
|
|
} else failactype("number");
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(posp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_fixnum(ac))) ac = bool_obj(get_fixnum(ac) > 0);
|
|
|
|
else if (likely(is_flonum(ac))) ac = bool_obj(get_flonum(ac) > 0.0);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failactype("number");
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(negp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_fixnum(ac))) ac = bool_obj(get_fixnum(ac) < 0);
|
|
|
|
else if (likely(is_flonum(ac))) ac = bool_obj(get_flonum(ac) < 0.0);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failactype("number");
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(add) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
|
|
|
long lx = get_fixnum(x), ly = get_fixnum(y);
|
|
|
|
long long llz = (long long)lx + (long long)ly;
|
|
|
|
if (likely(llz >= FIXNUM_MIN && llz <= FIXNUM_MAX)) ac = fixnum_obj((long)llz);
|
|
|
|
else ac = flonum_obj((double)lx + (double)ly);
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(dx + dy);
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sub) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
|
|
|
long lx = get_fixnum(x), ly = get_fixnum(y);
|
|
|
|
long long llz = (long long)lx - (long long)ly;
|
|
|
|
if (likely(llz >= FIXNUM_MIN && llz <= FIXNUM_MAX)) ac = fixnum_obj((long)llz);
|
|
|
|
else ac = flonum_obj((double)lx - (double)ly);
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(dx - dy);
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(mul) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
|
|
|
long lx = get_fixnum(x), ly = get_fixnum(y);
|
|
|
|
long long llz = (long long)lx * (long long)ly;
|
|
|
|
if (likely(llz >= FIXNUM_MIN && llz <= FIXNUM_MAX)) ac = fixnum_obj((long)llz);
|
|
|
|
else ac = flonum_obj((double)lx * (double)ly);
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(dx * dy);
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(div) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
|
|
|
long lx, ly; long long llz, llr;
|
2023-03-26 06:16:52 +02:00
|
|
|
if (unlikely(y == fixnum_obj(0))) fail("division by zero");
|
2023-03-27 22:49:49 +02:00
|
|
|
lx = get_fixnum(x), ly = get_fixnum(y);
|
|
|
|
llz = (long long)lx / (long long)ly, llr = (long long)lx % (long long)ly;
|
|
|
|
if (likely(!llr && llz >= FIXNUM_MIN && llz <= FIXNUM_MAX)) ac = fixnum_obj((long)llz);
|
|
|
|
else ac = flonum_obj((double)lx / (double)ly);
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(dx / dy);
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(quo) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
2023-03-26 06:16:52 +02:00
|
|
|
if (unlikely(y == fixnum_obj(0))) fail("division by zero");
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxquo(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
2023-04-03 17:31:56 +02:00
|
|
|
double dx, dy, dz;
|
|
|
|
if (likely(is_flonum(x) && flisint(dx = get_flonum(x)))) /* ok */;
|
2023-03-27 22:49:49 +02:00
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-04-03 17:31:56 +02:00
|
|
|
else failtype(x, "integer");
|
|
|
|
if (likely(is_flonum(y) && flisint(dy = get_flonum(y)))) /* ok */;
|
2023-03-27 22:49:49 +02:00
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-04-03 17:31:56 +02:00
|
|
|
else failtype(y, "integer");
|
|
|
|
modf(dx / dy, &dz);
|
|
|
|
ac = flonum_obj(dz);
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(rem) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
2023-03-26 06:16:52 +02:00
|
|
|
if (unlikely(y == fixnum_obj(0))) fail("division by zero");
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxrem(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
2023-04-03 17:31:56 +02:00
|
|
|
double dx, dy, dz;
|
|
|
|
if (likely(is_flonum(x) && flisint(dx = get_flonum(x)))) /* ok */;
|
2023-03-27 22:49:49 +02:00
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-04-03 17:31:56 +02:00
|
|
|
else failtype(x, "integer");
|
|
|
|
if (likely(is_flonum(y) && flisint(dy = get_flonum(y)))) /* ok */;
|
2023-03-27 22:49:49 +02:00
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-04-03 17:31:56 +02:00
|
|
|
else failtype(y, "integer");
|
|
|
|
dz = fmod(dx, dy);
|
|
|
|
/* keep zero positive: (remainder -10.0 2.0) => 0.0, not -0.0 */
|
|
|
|
ac = flonum_obj((dz == 0.0) ? 0.0 : dz);
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(mqu) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
2023-03-26 06:16:52 +02:00
|
|
|
if (unlikely(y == fixnum_obj(0))) fail("division by zero");
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxmqu(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(flmqu(dx, dy));
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(mlo) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
2023-03-26 06:16:52 +02:00
|
|
|
if (unlikely(y == fixnum_obj(0))) fail("division by zero");
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxmlo(get_fixnum(x), get_fixnum(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(flmlo(dx, dy));
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
define_instruction(lt) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
|
|
|
ac = bool_obj(get_fixnum(x) < get_fixnum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(dx < dy);
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(gt) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
|
|
|
ac = bool_obj(get_fixnum(x) > get_fixnum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(dx > dy);
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(le) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
|
|
|
ac = bool_obj(get_fixnum(x) <= get_fixnum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(dx <= dy);
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ge) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
|
|
|
ac = bool_obj(get_fixnum(x) >= get_fixnum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(dx >= dy);
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(eq) {
|
|
|
|
obj x = ac, y = spop();
|
2023-04-03 17:31:56 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
|
|
|
ac = bool_obj(x == y);
|
|
|
|
} else if (is_flonum(x) || is_flonum(y)) {
|
2023-02-28 06:31:08 +01:00
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(dx == dy);
|
|
|
|
} else ac = bool_obj(0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ne) {
|
|
|
|
obj x = ac, y = spop();
|
2023-04-03 17:31:56 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
|
|
|
ac = bool_obj(x != y);
|
|
|
|
} else if (is_flonum(x) || is_flonum(y)) {
|
2023-02-28 06:31:08 +01:00
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(dx != dy);
|
|
|
|
} else ac = bool_obj(1);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(min) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
|
|
|
ac = (get_fixnum(x) < get_fixnum(y)) ? x : y;
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = dx < dy ? flonum_obj(dx) : flonum_obj(dy);
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(max) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
|
|
|
ac = (get_fixnum(x) > get_fixnum(y)) ? x : y;
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-07 01:36:15 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = dx > dy ? flonum_obj(dx) : flonum_obj(dy);
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-25 16:30:30 +01:00
|
|
|
define_instruction(gcd) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
|
|
|
ac = fixnum_obj(fxgcd(get_fixnum(x), get_fixnum(y)));
|
2023-03-25 16:30:30 +01:00
|
|
|
} else {
|
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-25 16:30:30 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-25 16:30:30 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(flgcd(dx, dy));
|
2023-03-25 16:30:30 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(pow) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
2023-03-25 16:30:30 +01:00
|
|
|
/* fixme: this will either overflow, or fail on negative y */
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = fixnum_obj(fxpow(get_fixnum(x), get_fixnum(y)));
|
2023-03-25 16:30:30 +01:00
|
|
|
} else {
|
|
|
|
double dx, dy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
2023-03-25 16:30:30 +01:00
|
|
|
else failtype(x, "number");
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
2023-03-25 16:30:30 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(pow(dx, dy));
|
2023-03-25 16:30:30 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sqrt) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(ac))) {
|
|
|
|
ac = flonum_obj(sqrt(get_flonum(ac)));
|
|
|
|
} else if (likely(is_fixnum(ac))) {
|
|
|
|
long x = get_fixnum(ac), y;
|
2023-03-26 06:16:52 +02:00
|
|
|
if (x < 0) ac = flonum_obj((HUGE_VAL - HUGE_VAL));
|
|
|
|
else if (y = fxsqrt(x), y*y == x) ac = fixnum_obj(y);
|
|
|
|
else ac = flonum_obj(sqrt((double)x));
|
2023-03-25 16:30:30 +01:00
|
|
|
} else failactype("number");
|
|
|
|
gonexti();
|
|
|
|
}
|
2023-02-28 06:31:08 +01:00
|
|
|
|
2023-03-25 20:06:47 +01:00
|
|
|
define_instruction(exp) {
|
|
|
|
double x;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (unlikely(is_fixnum(ac))) x = (double)get_fixnum(ac);
|
|
|
|
else if (likely(is_flonum(ac))) x = get_flonum(ac);
|
2023-03-25 20:06:47 +01:00
|
|
|
else failactype("number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(exp(x));
|
2023-03-25 20:06:47 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(log) {
|
|
|
|
double x; obj y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (unlikely(is_fixnum(ac))) x = (double)get_fixnum(ac);
|
|
|
|
else if (likely(is_flonum(ac))) x = get_flonum(ac);
|
2023-03-25 20:06:47 +01:00
|
|
|
else failactype("number");
|
|
|
|
if (likely(!y)) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(log(x));
|
|
|
|
} else if (likely(y == fixnum_obj(10))) {
|
|
|
|
ac = flonum_obj(log10(x));
|
2023-03-25 20:06:47 +01:00
|
|
|
} else {
|
|
|
|
double b;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (unlikely(is_fixnum(y))) b = (double)get_fixnum(y);
|
|
|
|
else if (likely(is_flonum(y))) b = get_flonum(y);
|
2023-03-25 20:06:47 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
if (likely(b == 10.0)) ac = flonum_obj(log10(x));
|
|
|
|
else ac = flonum_obj(log(x)/log(b));
|
2023-03-25 20:06:47 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sin) {
|
|
|
|
double x;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (unlikely(is_fixnum(ac))) {
|
|
|
|
x = (double)get_fixnum(ac);
|
|
|
|
} else if (likely(is_flonum(ac))) {
|
|
|
|
x = get_flonum(ac);
|
2023-03-25 20:06:47 +01:00
|
|
|
} else failactype("number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(sin(x));
|
2023-03-25 20:06:47 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cos) {
|
|
|
|
double x;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (unlikely(is_fixnum(ac))) {
|
|
|
|
x = (double)get_fixnum(ac);
|
|
|
|
} else if (likely(is_flonum(ac))) {
|
|
|
|
x = get_flonum(ac);
|
2023-03-25 20:06:47 +01:00
|
|
|
} else failactype("number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(cos(x));
|
2023-03-25 20:06:47 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(tan) {
|
|
|
|
double x;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (unlikely(is_fixnum(ac))) {
|
|
|
|
x = (double)get_fixnum(ac);
|
|
|
|
} else if (likely(is_flonum(ac))) {
|
|
|
|
x = get_flonum(ac);
|
2023-03-25 20:06:47 +01:00
|
|
|
} else failactype("number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(tan(x));
|
2023-03-25 20:06:47 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(asin) {
|
|
|
|
double x;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (unlikely(is_fixnum(ac))) {
|
|
|
|
x = (double)get_fixnum(ac);
|
|
|
|
} else if (likely(is_flonum(ac))) {
|
|
|
|
x = get_flonum(ac);
|
2023-03-25 20:06:47 +01:00
|
|
|
} else failactype("number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(asin(x));
|
2023-03-25 20:06:47 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(acos) {
|
|
|
|
double x;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (unlikely(is_fixnum(ac))) {
|
|
|
|
x = (double)get_fixnum(ac);
|
|
|
|
} else if (likely(is_flonum(ac))) {
|
|
|
|
x = get_flonum(ac);
|
2023-03-25 20:06:47 +01:00
|
|
|
} else failactype("number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(acos(x));
|
2023-03-25 20:06:47 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(atan) {
|
|
|
|
double x; obj y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (unlikely(is_fixnum(ac))) x = (double)get_fixnum(ac);
|
|
|
|
else if (likely(is_flonum(ac))) x = get_flonum(ac);
|
2023-03-25 20:06:47 +01:00
|
|
|
else failactype("number");
|
|
|
|
if (likely(!y)) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(atan(x));
|
2023-03-25 20:06:47 +01:00
|
|
|
} else {
|
|
|
|
double b;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (unlikely(is_fixnum(y))) b = (double)get_fixnum(y);
|
|
|
|
else if (likely(is_flonum(y))) b = get_flonum(y);
|
2023-03-25 20:06:47 +01:00
|
|
|
else failtype(y, "number");
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(atan2(x, b));
|
2023-03-25 20:06:47 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(neg) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_fixnum(ac))) {
|
|
|
|
ac = fixnum_obj(-get_fixnum(ac));
|
|
|
|
} else if (likely(is_flonum(ac))) {
|
|
|
|
ac = flonum_obj(-get_flonum(ac));
|
2023-03-07 01:36:15 +01:00
|
|
|
} else failactype("number");
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(abs) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_fixnum(ac))) {
|
|
|
|
ac = fixnum_obj(fxabs(get_fixnum(ac)));
|
|
|
|
} else if (likely(is_flonum(ac))) {
|
|
|
|
ac = flonum_obj(fabs(get_flonum(ac)));
|
2023-03-07 01:36:15 +01:00
|
|
|
} else failactype("number");
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
2023-03-25 16:30:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(floor) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(ac))) {
|
|
|
|
ac = flonum_obj(floor(get_flonum(ac)));
|
|
|
|
} else if (unlikely(!is_fixnum(ac))) {
|
2023-03-25 16:30:30 +01:00
|
|
|
failactype("number");
|
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ceil) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(ac))) {
|
|
|
|
ac = flonum_obj(ceil(get_flonum(ac)));
|
|
|
|
} else if (unlikely(!is_fixnum(ac))) {
|
2023-03-25 16:30:30 +01:00
|
|
|
failactype("number");
|
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(trunc) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(ac))) {
|
|
|
|
double x = get_flonum(ac);
|
2023-03-25 16:30:30 +01:00
|
|
|
double i; modf(x, &i);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = flonum_obj(i);
|
2023-03-27 22:49:49 +02:00
|
|
|
} else if (unlikely(!is_fixnum(ac))) {
|
2023-03-25 16:30:30 +01:00
|
|
|
failactype("number");
|
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(round) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(ac))) {
|
|
|
|
ac = flonum_obj(flround(get_flonum(ac)));
|
|
|
|
} else if (unlikely(!is_fixnum(ac))) {
|
2023-03-25 16:30:30 +01:00
|
|
|
failactype("number");
|
|
|
|
}
|
|
|
|
gonexti();
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
define_instruction(nump) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_fixnum(ac) || is_flonum(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(fixp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_fixnum(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(flop) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_flonum(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(intp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_fixnum(ac))) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(1);
|
2023-02-28 06:31:08 +01:00
|
|
|
} else { /* accepts any object! */
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_flonum(ac) && flisint(get_flonum(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-30 23:02:22 +02:00
|
|
|
define_instruction(ratp) {
|
|
|
|
if (likely(is_fixnum(ac))) {
|
|
|
|
ac = bool_obj(1);
|
|
|
|
} else if (likely(is_flonum(ac))) {
|
|
|
|
double f = get_flonum(ac);
|
|
|
|
ac = bool_obj(f > -HUGE_VAL && f < HUGE_VAL);
|
|
|
|
} else {
|
|
|
|
ac = bool_obj(0);
|
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(nanp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (unlikely(is_fixnum(ac))) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(0);
|
2023-03-27 22:49:49 +02:00
|
|
|
} else if (likely(is_flonum(ac))) {
|
|
|
|
double f = get_flonum(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(f != f);
|
2023-03-07 01:36:15 +01:00
|
|
|
} else failactype("number");
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(finp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (unlikely(is_fixnum(ac))) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(1);
|
2023-03-27 22:49:49 +02:00
|
|
|
} else if (likely(is_flonum(ac))) {
|
|
|
|
double f = get_flonum(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(f > -HUGE_VAL && f < HUGE_VAL);
|
2023-03-07 01:36:15 +01:00
|
|
|
} else failactype("number");
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(infp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (unlikely(is_fixnum(ac))) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(0);
|
2023-03-27 22:49:49 +02:00
|
|
|
} else if (likely(is_flonum(ac))) {
|
|
|
|
double f = get_flonum(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(f <= -HUGE_VAL || f >= HUGE_VAL);
|
2023-03-07 01:36:15 +01:00
|
|
|
} else failactype("number");
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(evnp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_fixnum(ac))) {
|
|
|
|
ac = bool_obj((get_fixnum(ac) & 1) == 0);
|
|
|
|
} else if (likely(is_flonum(ac))) {
|
|
|
|
double f = get_flonum(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(flisint(f / 2.0));
|
2023-03-07 01:36:15 +01:00
|
|
|
} else failactype("number");
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(oddp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_fixnum(ac))) {
|
|
|
|
ac = bool_obj((get_fixnum(ac) & 1) != 0);
|
|
|
|
} else if (likely(is_flonum(ac))) {
|
|
|
|
double f = get_flonum(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(flisint((f + 1.0) / 2.0));
|
2023-03-07 01:36:15 +01:00
|
|
|
} else failactype("number");
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ntoi) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(ac))) ac = fixnum_obj(fxflo(get_flonum(ac)));
|
|
|
|
else if (likely(is_fixnum(ac))) /* keep ac as-is */ ;
|
2023-03-07 01:36:15 +01:00
|
|
|
else failactype("number");
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ntoj) {
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_fixnum(ac))) ac = flonum_obj((flonum_t)get_fixnum(ac));
|
|
|
|
else if (likely(is_flonum(ac))) /* keep ac as-is */ ;
|
2023-03-07 01:36:15 +01:00
|
|
|
else failactype("number");
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
define_instruction(lcat) {
|
|
|
|
obj t, l, *p, *d; int c;
|
2023-03-27 22:49:49 +02:00
|
|
|
for (l = ac, c = 0; is_pair(l); l = pair_cdr(l)) ++c;
|
2023-03-26 06:16:52 +02:00
|
|
|
hp_reserve(pairbsz()*c);
|
2023-02-28 06:31:08 +01:00
|
|
|
p = --sp; t = *p; /* pop & take addr */
|
2023-03-27 22:49:49 +02:00
|
|
|
for (l = ac; is_pair(l); l = pair_cdr(l)) {
|
|
|
|
*--hp = t; d = hp; *--hp = pair_car(l);
|
2023-03-26 06:16:52 +02:00
|
|
|
*p = hend_pair(); p = d;
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
ac = *sp;
|
|
|
|
gonexti();
|
2023-03-28 06:09:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(lcpy) {
|
|
|
|
obj t, l, *p, *d; int c;
|
|
|
|
for (l = ac, c = 0; is_pair(l); l = pair_cdr(l)) ++c;
|
|
|
|
hp_reserve(pairbsz()*c);
|
|
|
|
p = sp; *p = t = l; /* tail of last pair */
|
|
|
|
for (l = ac; is_pair(l); l = pair_cdr(l)) {
|
|
|
|
*--hp = t; d = hp; *--hp = pair_car(l);
|
|
|
|
*p = hend_pair(); p = d;
|
|
|
|
}
|
|
|
|
ac = *sp;
|
|
|
|
gonexti();
|
|
|
|
}
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
define_instruction(ccmp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); int cmp; ckc(x); ckc(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
cmp = get_char(x) - get_char(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = fixnum_obj(cmp);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ceq) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckc(x); ckc(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_char(x) == get_char(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(clt) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckc(x); ckc(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_char(x) < get_char(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cgt) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckc(x); ckc(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_char(x) > get_char(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cle) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckc(x); ckc(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_char(x) <= get_char(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cge) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckc(x); ckc(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(get_char(x) >= get_char(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cicmp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); int cmp; ckc(x); ckc(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
cmp = tolower(get_char(x)) - tolower(get_char(y));
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = fixnum_obj(cmp);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cieq) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckc(x); ckc(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(tolower(get_char(x)) == tolower(get_char(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cilt) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckc(x); ckc(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(tolower(get_char(x)) < tolower(get_char(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cigt) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckc(x); ckc(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(tolower(get_char(x)) > tolower(get_char(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cile) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckc(x); ckc(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(tolower(get_char(x)) <= tolower(get_char(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cige) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckc(x); ckc(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(tolower(get_char(x)) >= tolower(get_char(y)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cwsp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckc(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(isspace(get_char(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(clcp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckc(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(islower(get_char(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cucp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckc(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(isupper(get_char(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(calp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckc(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(isalpha(get_char(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cnup) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckc(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(isdigit(get_char(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cupc) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckc(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = char_obj(toupper(get_char(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cdnc) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckc(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = char_obj(tolower(get_char(ac)));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-26 04:22:28 +02:00
|
|
|
define_instruction(cflc) {
|
|
|
|
ckc(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = char_obj(tolower(get_char(ac))); /* stub */
|
2023-03-26 04:22:28 +02:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cdgv) {
|
|
|
|
int ch; ckc(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
ch = get_char(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
if (likely('0' <= ch && ch <= '9')) ac = fixnum_obj(ch - '0');
|
2023-03-26 04:22:28 +02:00
|
|
|
/* R7RS won't allow hex and any larger radix digits
|
2023-03-26 06:16:52 +02:00
|
|
|
else if (likely('a' <= ch && ch <= 'z')) ac = fixnum_obj(10 + ch - 'a');
|
|
|
|
else if (likely('A' <= ch && ch <= 'Z')) ac = fixnum_obj(10 + ch - 'A'); */
|
|
|
|
else ac = bool_obj(0);
|
2023-03-26 04:22:28 +02:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(scmp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); int cmp; cks(x); cks(y);
|
|
|
|
cmp = strcmp(stringchars(x), stringchars(y));
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = fixnum_obj(cmp);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(seq) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cks(x); cks(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(strcmp(stringchars(x), stringchars(y)) == 0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(slt) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cks(x); cks(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(strcmp(stringchars(x), stringchars(y)) < 0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sgt) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cks(x); cks(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(strcmp(stringchars(x), stringchars(y)) > 0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sle) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cks(x); cks(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(strcmp(stringchars(x), stringchars(y)) <= 0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sge) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cks(x); cks(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(strcmp(stringchars(x), stringchars(y)) >= 0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sicmp) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); int cmp; cks(x); cks(y);
|
|
|
|
cmp = strcmp_ci(stringchars(x), stringchars(y));
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = fixnum_obj(cmp);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sieq) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cks(x); cks(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(strcmp_ci(stringchars(x), stringchars(y)) == 0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(silt) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cks(x); cks(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(strcmp_ci(stringchars(x), stringchars(y)) < 0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sigt) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cks(x); cks(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(strcmp_ci(stringchars(x), stringchars(y)) > 0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sile) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cks(x); cks(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(strcmp_ci(stringchars(x), stringchars(y)) <= 0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sige) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cks(x); cks(y);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(strcmp_ci(stringchars(x), stringchars(y)) >= 0);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
define_instruction(symp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_symbol(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(boolp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_bool(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-12 00:43:51 +01:00
|
|
|
define_instruction(boxp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_box(ac));
|
2023-03-12 00:43:51 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2024-07-17 23:47:08 +02:00
|
|
|
define_instruction(shebangp) {
|
|
|
|
ac = bool_obj(is_shebang(ac));
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(ytosb) {
|
|
|
|
cky(ac);
|
|
|
|
ac = shebang_obj(get_symbol(ac));
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sbtoy) {
|
|
|
|
cksb(ac);
|
|
|
|
ac = symbol_obj(get_shebang(ac));
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(funp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_proc(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-29 00:14:45 +02:00
|
|
|
define_instruction(voidp) {
|
|
|
|
ac = bool_obj(is_void(ac));
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(void) {
|
|
|
|
ac = void_obj();
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(ipp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_iport(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(opp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_oport(ac));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2024-07-12 01:12:03 +02:00
|
|
|
define_instruction(ttyp) {
|
|
|
|
extern int is_tty_port(obj o); /* s.c */
|
|
|
|
ac = bool_obj(is_tty_port(ac));
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-31 00:13:07 +02:00
|
|
|
define_instruction(cin) {
|
|
|
|
ac = cx__2Acurrent_2Dinput_2A;
|
|
|
|
assert(is_iport(ac));
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cout) {
|
|
|
|
ac = cx__2Acurrent_2Doutput_2A;
|
|
|
|
assert(is_oport(ac));
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cerr) {
|
|
|
|
ac = cx__2Acurrent_2Derror_2A;
|
|
|
|
assert(is_oport(ac));
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-31 01:41:22 +02:00
|
|
|
define_instruction(setcin) {
|
|
|
|
ckr(ac);
|
|
|
|
cx__2Acurrent_2Dinput_2A = ac;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(setcout) {
|
|
|
|
ckw(ac);
|
|
|
|
cx__2Acurrent_2Doutput_2A = ac;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(setcerr) {
|
|
|
|
ckw(ac);
|
|
|
|
cx__2Acurrent_2Derror_2A = ac;
|
|
|
|
gonexti();
|
|
|
|
gonexti();
|
|
|
|
}
|
2023-03-31 00:13:07 +02:00
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(sip) {
|
2023-03-31 00:13:07 +02:00
|
|
|
ac = iport_file_obj(stdin);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sop) {
|
2023-03-31 00:13:07 +02:00
|
|
|
ac = oport_file_obj(stdout);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sep) {
|
2023-03-31 00:13:07 +02:00
|
|
|
ac = oport_file_obj(stderr);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-07 05:52:38 +01:00
|
|
|
define_instruction(ipop) {
|
|
|
|
cxtype_iport_t *vt; ckr(ac);
|
|
|
|
vt = iportvt(ac); assert(vt);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(vt != (cxtype_iport_t *)IPORT_CLOSED_NTAG);
|
2023-03-07 05:52:38 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(opop) {
|
|
|
|
cxtype_oport_t *vt; ckw(ac);
|
|
|
|
vt = oportvt(ac); assert(vt);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(vt != (cxtype_oport_t *)OPORT_CLOSED_NTAG);
|
2023-03-07 05:52:38 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-28 00:58:37 +02:00
|
|
|
define_instruction(oif) {
|
2024-07-13 01:26:56 +02:00
|
|
|
FILE *fp; cks(ac);
|
|
|
|
fp = fopen(stringchars(ac), "r");
|
2023-03-30 22:24:03 +02:00
|
|
|
ac = (fp == NULL) ? bool_obj(0) : iport_file_obj(fp);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-28 00:58:37 +02:00
|
|
|
define_instruction(oof) {
|
2024-07-13 01:26:56 +02:00
|
|
|
FILE *fp; cks(ac);
|
|
|
|
fp = fopen(stringchars(ac), "w");
|
2023-03-30 22:24:03 +02:00
|
|
|
ac = (fp == NULL) ? bool_obj(0) : oport_file_obj(fp);
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-28 00:58:37 +02:00
|
|
|
define_instruction(obif) {
|
2024-07-13 01:26:56 +02:00
|
|
|
FILE *fp; cks(ac);
|
|
|
|
fp = fopen(stringchars(ac), "rb");
|
2023-03-30 22:24:03 +02:00
|
|
|
ac = (fp == NULL) ? bool_obj(0) : iport_file_obj(fp);
|
2023-03-28 00:58:37 +02:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(obof) {
|
2024-07-13 01:26:56 +02:00
|
|
|
FILE *fp; cks(ac);
|
|
|
|
fp = fopen(stringchars(ac), "wb");
|
2023-03-30 22:24:03 +02:00
|
|
|
ac = (fp == NULL) ? bool_obj(0) : oport_file_obj(fp);
|
2023-03-28 00:58:37 +02:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(ois) {
|
2023-03-07 01:36:15 +01:00
|
|
|
int *d; cks(ac);
|
|
|
|
d = dupstring(stringdata(ac));
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = iport_string_obj(sialloc(sdatachars(d), d));
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(oos) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = oport_string_obj(newcb());
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-28 00:58:37 +02:00
|
|
|
define_instruction(oib) {
|
|
|
|
int *d; unsigned char *p, *e; ckb(ac);
|
|
|
|
d = dupbytevector(bytevectordata(ac));
|
|
|
|
p = bvdatabytes(d), e = p + *d;
|
|
|
|
ac = iport_bytevector_obj(bvialloc(p, e, d));
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(oob) {
|
|
|
|
ac = oport_bytevector_obj(newcb());
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(cip) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cxtype_iport_t *vt; ckr(ac);
|
|
|
|
vt = iportvt(ac); assert(vt);
|
2023-02-28 06:31:08 +01:00
|
|
|
vt->close(iportdata(ac)); vt->free(iportdata(ac));
|
|
|
|
objptr_from_obj(ac)[-1] = (obj)IPORT_CLOSED_NTAG;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cop) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cxtype_oport_t *vt; ckw(ac);
|
|
|
|
vt = oportvt(ac); assert(vt);
|
2023-02-28 06:31:08 +01:00
|
|
|
vt->close(oportdata(ac)); vt->free(oportdata(ac));
|
|
|
|
objptr_from_obj(ac)[-1] = (obj)OPORT_CLOSED_NTAG;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-26 18:02:36 +02:00
|
|
|
define_instruction(fop) {
|
|
|
|
ckw(ac);
|
|
|
|
oportflush(ac);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2024-06-05 00:37:55 +02:00
|
|
|
define_instruction(pfc) {
|
|
|
|
ckr(ac);
|
|
|
|
/* port-fold-case NYI: return false for now */
|
|
|
|
ac = bool_obj(0);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(spfc) {
|
|
|
|
ckr(ac);
|
|
|
|
if (spop() != bool_obj(0)) {
|
|
|
|
/* set-port-fold-case! NYI: ignore for now */
|
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(gos) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cxtype_oport_t *vt; ckw(ac);
|
|
|
|
vt = ckoportvt(ac);
|
2023-02-28 06:31:08 +01:00
|
|
|
if (vt != (cxtype_oport_t *)OPORT_STRING_NTAG &&
|
|
|
|
vt != (cxtype_oport_t *)OPORT_BYTEVECTOR_NTAG) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = eof_obj();
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
|
|
|
cbuf_t *pcb = oportdata(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = string_obj(newstring(cbdata(pcb)));
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-28 00:58:37 +02:00
|
|
|
define_instruction(gob) {
|
|
|
|
cxtype_oport_t *vt; ckw(ac);
|
|
|
|
vt = ckoportvt(ac);
|
|
|
|
if (vt != (cxtype_oport_t *)OPORT_BYTEVECTOR_NTAG &&
|
|
|
|
vt != (cxtype_oport_t *)OPORT_STRING_NTAG) {
|
|
|
|
ac = eof_obj();
|
|
|
|
} else {
|
|
|
|
cbuf_t *pcb = oportdata(ac);
|
|
|
|
int len = (int)(pcb->fill - pcb->buf);
|
|
|
|
ac = bytevector_obj(newbytevector((unsigned char *)pcb->buf, len));
|
|
|
|
}
|
|
|
|
gonexti();
|
|
|
|
}
|
2023-03-07 05:52:38 +01:00
|
|
|
|
|
|
|
define_instruction(rdc) {
|
|
|
|
int c; ckr(ac);
|
|
|
|
c = iportgetc(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
if (unlikely(c == EOF)) ac = eof_obj();
|
|
|
|
else ac = char_obj(c);
|
2023-03-07 05:52:38 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(rdac) {
|
|
|
|
int c; ckr(ac);
|
|
|
|
c = iportpeekc(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
if (unlikely(c == EOF)) ac = eof_obj();
|
|
|
|
else ac = char_obj(c);
|
2023-03-07 05:52:38 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(rdcr) {
|
|
|
|
ckr(ac);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(1); /* no portable way to detect hanging? */
|
2023-03-07 05:52:38 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-28 00:58:37 +02:00
|
|
|
define_instruction(rd8) {
|
|
|
|
int c; ckr(ac);
|
|
|
|
c = iportgetc(ac);
|
|
|
|
if (unlikely(c == EOF)) ac = eof_obj();
|
|
|
|
else ac = byte_obj(c);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(rda8) {
|
|
|
|
int c; ckr(ac);
|
|
|
|
c = iportpeekc(ac);
|
|
|
|
if (unlikely(c == EOF)) ac = eof_obj();
|
|
|
|
else ac = byte_obj(c);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(rd8r) {
|
|
|
|
ckr(ac);
|
|
|
|
ac = bool_obj(1); /* no portable way to detect hanging? */
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-07 05:52:38 +01:00
|
|
|
define_instruction(eofp) {
|
2023-03-27 22:49:49 +02:00
|
|
|
ac = bool_obj(is_eof(ac));
|
2023-03-07 05:52:38 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(eof) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = eof_obj();
|
2023-03-07 05:52:38 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(wrc) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckc(x); ckw(y);
|
2023-03-27 22:49:49 +02:00
|
|
|
oportputc(get_char(x), y);
|
2023-03-26 18:02:36 +02:00
|
|
|
ac = void_obj();
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(wrs) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); cks(x); ckw(y);
|
|
|
|
oportputs(stringchars(x), y);
|
2023-03-26 18:02:36 +02:00
|
|
|
ac = void_obj();
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-28 00:58:37 +02:00
|
|
|
define_instruction(wr8) {
|
|
|
|
obj x = ac, y = spop(); ck8(x); ckw(y);
|
|
|
|
oportputc(get_byte(x), y);
|
|
|
|
ac = void_obj();
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(wrb) {
|
|
|
|
obj x = ac, y = spop(); int *d; ckb(x); ckw(y);
|
|
|
|
d = bytevectordata(x);
|
|
|
|
oportwrite((char *)bvdatabytes(d), *d, y);
|
|
|
|
ac = void_obj();
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(wrcd) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckw(y);
|
|
|
|
oportputcircular(x, y, 1);
|
2023-03-26 18:02:36 +02:00
|
|
|
ac = void_obj();
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(wrcw) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckw(y);
|
|
|
|
oportputcircular(x, y, 0);
|
2023-03-26 18:02:36 +02:00
|
|
|
ac = void_obj();
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(wrnl) {
|
2023-03-07 01:36:15 +01:00
|
|
|
ckw(ac);
|
2023-02-28 06:31:08 +01:00
|
|
|
oportputc('\n', ac);
|
2023-03-26 18:02:36 +02:00
|
|
|
ac = void_obj();
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(wrhw) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckw(y);
|
|
|
|
oportputshared(x, y, 0);
|
2023-03-26 18:02:36 +02:00
|
|
|
ac = void_obj();
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(wriw) {
|
2023-03-07 01:36:15 +01:00
|
|
|
obj x = ac, y = spop(); ckw(y);
|
|
|
|
oportputsimple(x, y, 0);
|
2023-03-26 18:02:36 +02:00
|
|
|
ac = void_obj();
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-04-13 23:59:31 +02:00
|
|
|
define_instruction(itrs) {
|
|
|
|
ac = cx__2Atransformers_2A;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2024-07-11 01:28:27 +02:00
|
|
|
define_instruction(glos){
|
|
|
|
ac = cx__2Aglobals_2A;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-18 19:17:45 +01:00
|
|
|
define_instruction(igp) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(isintegrable(ac));
|
2023-03-18 19:17:45 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(iglk) {
|
2024-07-07 05:04:35 +02:00
|
|
|
struct intgtab_entry *pe;
|
|
|
|
/* lookup by index: returns #f for out-of-scope, void for missing */
|
|
|
|
if (is_fixnum(ac)) {
|
|
|
|
int i = get_fixnum(ac), cnt = intgtab_count();
|
|
|
|
if (i < 0 || i >= cnt) ac = bool_obj(0);
|
|
|
|
else if (!isintegrable(ac)) ac = void_obj();
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
/* lookup by by symbol */
|
|
|
|
cky(ac);
|
2023-03-27 22:49:49 +02:00
|
|
|
pe = lookup_integrable(get_symbol(ac));
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = pe ? mkintegrable(pe) : bool_obj(0);
|
2023-03-18 19:17:45 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(igty) {
|
|
|
|
int it; ckg(ac);
|
2023-03-19 00:13:38 +01:00
|
|
|
it = integrable_type(integrabledata(ac));
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = it ? char_obj(it) : bool_obj(0);
|
2023-03-18 19:17:45 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(iggl) {
|
|
|
|
const char *igs; ckg(ac);
|
2023-03-19 00:13:38 +01:00
|
|
|
igs = integrable_global(integrabledata(ac));
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = igs ? mksymbol(internsym((char*)igs)) : bool_obj(0);
|
2023-03-18 19:17:45 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(igco) {
|
|
|
|
int n; const char *cs; ckg(ac); ckk(sref(0));
|
2023-03-27 22:49:49 +02:00
|
|
|
n = get_fixnum(spop());
|
2023-03-19 00:13:38 +01:00
|
|
|
cs = integrable_code(integrabledata(ac), n);
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = cs ? string_obj(newstring((char*)cs)) : bool_obj(0);
|
2023-03-18 19:17:45 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-04-22 05:11:45 +02:00
|
|
|
define_instruction(vmclo) {
|
|
|
|
int i, n = get_fixnum(*ip++);
|
|
|
|
if (n < 1) fail("invalid closure size");
|
|
|
|
hp_reserve(vmclobsz(n));
|
|
|
|
for (i = n-1; i >= 0; --i) *--hp = sref(i);
|
|
|
|
ac = hend_vmclo(n);
|
|
|
|
sdrop(n);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-04-20 17:03:14 +02:00
|
|
|
define_instruction(hshim) {
|
|
|
|
unsigned long long v = (unsigned long long)ac, base = 0; obj b = spop();
|
2024-07-04 00:47:26 +02:00
|
|
|
if (v && isaptr(v)) { ac = fixnum_obj(0); gonexti(); }
|
2023-04-20 17:03:14 +02:00
|
|
|
if (b) { ckk(b); base = get_fixnum(b); }
|
|
|
|
if (!base) base = 1 + (unsigned long long)FIXNUM_MAX;
|
|
|
|
ac = fixnum_obj((fixnum_t)(v % base));
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(rdsx) {
|
2023-03-26 06:16:52 +02:00
|
|
|
cks(ac);
|
|
|
|
unload_ac(); /* ac->ra (string) */
|
|
|
|
unload_ip(); /* ip->rx */
|
2023-02-28 06:31:08 +01:00
|
|
|
hp = rds_stox(r, sp, hp);
|
|
|
|
reload_ac(); /* ra->ac (sexp or eof) */
|
2023-03-26 06:16:52 +02:00
|
|
|
reload_ip(); /* rx->ip */
|
|
|
|
if (ac == eof_obj()) fail("failed to read serialized s-expression");
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(rdsc) {
|
2023-03-07 01:36:15 +01:00
|
|
|
cks(ac); unload_ac(); /* ac->ra (string) */
|
2023-03-26 06:16:52 +02:00
|
|
|
unload_ac(); /* ac->ra (string) */
|
|
|
|
unload_ip(); /* ip->rx */
|
2023-02-28 06:31:08 +01:00
|
|
|
hp = rds_stoc(r, sp, hp);
|
|
|
|
reload_ac(); /* ra->ac (codevec or eof) */
|
2023-03-26 06:16:52 +02:00
|
|
|
reload_ip(); /* rx->ip */
|
|
|
|
if (ac == eof_obj()) fail("failed to read serialized code");
|
2023-02-28 06:31:08 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-04-20 17:03:14 +02:00
|
|
|
|
2023-03-26 06:16:52 +02:00
|
|
|
define_instruction(litf) { ac = bool_obj(0); gonexti(); }
|
|
|
|
define_instruction(litt) { ac = bool_obj(1); gonexti(); }
|
2023-03-26 06:38:50 +02:00
|
|
|
define_instruction(litn) { ac = null_obj(); gonexti(); }
|
2023-03-26 06:16:52 +02:00
|
|
|
define_instruction(pushlitf) { ac = bool_obj(0); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushlitt) { ac = bool_obj(1); spush(ac); gonexti(); }
|
2023-03-26 06:38:50 +02:00
|
|
|
define_instruction(pushlitn) { ac = null_obj(); spush(ac); gonexti(); }
|
2023-02-28 06:31:08 +01:00
|
|
|
|
2023-03-26 06:16:52 +02:00
|
|
|
define_instruction(lit0) { ac = fixnum_obj(0); gonexti(); }
|
|
|
|
define_instruction(lit1) { ac = fixnum_obj(1); gonexti(); }
|
|
|
|
define_instruction(lit2) { ac = fixnum_obj(2); gonexti(); }
|
|
|
|
define_instruction(lit3) { ac = fixnum_obj(3); gonexti(); }
|
|
|
|
define_instruction(lit4) { ac = fixnum_obj(4); gonexti(); }
|
|
|
|
define_instruction(lit5) { ac = fixnum_obj(5); gonexti(); }
|
|
|
|
define_instruction(lit6) { ac = fixnum_obj(6); gonexti(); }
|
|
|
|
define_instruction(lit7) { ac = fixnum_obj(7); gonexti(); }
|
|
|
|
define_instruction(lit8) { ac = fixnum_obj(8); gonexti(); }
|
|
|
|
define_instruction(lit9) { ac = fixnum_obj(9); gonexti(); }
|
|
|
|
define_instruction(pushlit0) { ac = fixnum_obj(0); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushlit1) { ac = fixnum_obj(1); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushlit2) { ac = fixnum_obj(2); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushlit3) { ac = fixnum_obj(3); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushlit4) { ac = fixnum_obj(4); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushlit5) { ac = fixnum_obj(5); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushlit6) { ac = fixnum_obj(6); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushlit7) { ac = fixnum_obj(7); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushlit8) { ac = fixnum_obj(8); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushlit9) { ac = fixnum_obj(9); spush(ac); gonexti(); }
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
define_instruction(sref0) { ac = sref(0); gonexti(); }
|
|
|
|
define_instruction(sref1) { ac = sref(1); gonexti(); }
|
|
|
|
define_instruction(sref2) { ac = sref(2); gonexti(); }
|
|
|
|
define_instruction(sref3) { ac = sref(3); gonexti(); }
|
|
|
|
define_instruction(sref4) { ac = sref(4); gonexti(); }
|
|
|
|
define_instruction(sref5) { ac = sref(5); gonexti(); }
|
|
|
|
define_instruction(sref6) { ac = sref(6); gonexti(); }
|
|
|
|
define_instruction(sref7) { ac = sref(7); gonexti(); }
|
|
|
|
define_instruction(sref8) { ac = sref(8); gonexti(); }
|
|
|
|
define_instruction(sref9) { ac = sref(9); gonexti(); }
|
|
|
|
define_instruction(pushsref0) { ac = sref(0); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushsref1) { ac = sref(1); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushsref2) { ac = sref(2); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushsref3) { ac = sref(3); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushsref4) { ac = sref(4); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushsref5) { ac = sref(5); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushsref6) { ac = sref(6); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushsref7) { ac = sref(7); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushsref8) { ac = sref(8); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushsref9) { ac = sref(9); spush(ac); gonexti(); }
|
|
|
|
|
2023-03-29 00:14:45 +02:00
|
|
|
define_instruction(srefi0) { ac = box_ref(sref(0)); gonexti(); }
|
|
|
|
define_instruction(srefi1) { ac = box_ref(sref(1)); gonexti(); }
|
|
|
|
define_instruction(srefi2) { ac = box_ref(sref(2)); gonexti(); }
|
|
|
|
define_instruction(srefi3) { ac = box_ref(sref(3)); gonexti(); }
|
|
|
|
define_instruction(srefi4) { ac = box_ref(sref(4)); gonexti(); }
|
|
|
|
define_instruction(pushsrefi0) { ac = box_ref(sref(0)); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushsrefi1) { ac = box_ref(sref(1)); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushsrefi2) { ac = box_ref(sref(2)); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushsrefi3) { ac = box_ref(sref(3)); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushsrefi4) { ac = box_ref(sref(4)); spush(ac); gonexti(); }
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
define_instruction(dref0) { ac = dref(0); gonexti(); }
|
|
|
|
define_instruction(dref1) { ac = dref(1); gonexti(); }
|
|
|
|
define_instruction(dref2) { ac = dref(2); gonexti(); }
|
|
|
|
define_instruction(dref3) { ac = dref(3); gonexti(); }
|
|
|
|
define_instruction(dref4) { ac = dref(4); gonexti(); }
|
|
|
|
define_instruction(pushdref0) { ac = dref(0); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushdref1) { ac = dref(1); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushdref2) { ac = dref(2); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushdref3) { ac = dref(3); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushdref4) { ac = dref(4); spush(ac); gonexti(); }
|
|
|
|
|
2023-03-29 00:14:45 +02:00
|
|
|
define_instruction(drefi0) { ac = box_ref(dref(0)); gonexti(); }
|
|
|
|
define_instruction(drefi1) { ac = box_ref(dref(1)); gonexti(); }
|
|
|
|
define_instruction(drefi2) { ac = box_ref(dref(2)); gonexti(); }
|
|
|
|
define_instruction(drefi3) { ac = box_ref(dref(3)); gonexti(); }
|
|
|
|
define_instruction(drefi4) { ac = box_ref(dref(4)); gonexti(); }
|
|
|
|
define_instruction(pushdrefi0) { ac = box_ref(dref(0)); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushdrefi1) { ac = box_ref(dref(1)); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushdrefi2) { ac = box_ref(dref(2)); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushdrefi3) { ac = box_ref(dref(3)); spush(ac); gonexti(); }
|
|
|
|
define_instruction(pushdrefi4) { ac = box_ref(dref(4)); spush(ac); gonexti(); }
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
define_instruction(call0) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(0);
|
2023-02-28 06:31:08 +01:00
|
|
|
callsubi();
|
|
|
|
}
|
|
|
|
define_instruction(call1) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(1);
|
2023-02-28 06:31:08 +01:00
|
|
|
callsubi();
|
|
|
|
}
|
|
|
|
define_instruction(call2) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(2);
|
2023-02-28 06:31:08 +01:00
|
|
|
callsubi();
|
|
|
|
}
|
|
|
|
define_instruction(call3) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(3);
|
2023-02-28 06:31:08 +01:00
|
|
|
callsubi();
|
|
|
|
}
|
|
|
|
define_instruction(call4) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(4);
|
2023-02-28 06:31:08 +01:00
|
|
|
callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(sreturn1) {
|
|
|
|
sdrop(1); rx = spop(); rd = spop();
|
|
|
|
retfromi();
|
|
|
|
}
|
|
|
|
define_instruction(sreturn2) {
|
|
|
|
sdrop(2); rx = spop(); rd = spop();
|
|
|
|
retfromi();
|
|
|
|
}
|
|
|
|
define_instruction(sreturn3) {
|
|
|
|
sdrop(3); rx = spop(); rd = spop();
|
|
|
|
retfromi();
|
|
|
|
}
|
|
|
|
define_instruction(sreturn4) {
|
|
|
|
sdrop(4); rx = spop(); rd = spop();
|
|
|
|
retfromi();
|
|
|
|
}
|
|
|
|
|
2023-03-20 04:31:28 +01:00
|
|
|
define_instruction(atest0) {
|
2023-03-26 06:16:52 +02:00
|
|
|
if (unlikely(ac != fixnum_obj(0))) fail("argument count error on entry");
|
2023-03-20 04:31:28 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(atest1) {
|
2023-03-26 06:16:52 +02:00
|
|
|
if (unlikely(ac != fixnum_obj(1))) fail("argument count error on entry");
|
2023-03-20 04:31:28 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(atest2) {
|
2023-03-26 06:16:52 +02:00
|
|
|
if (unlikely(ac != fixnum_obj(2))) fail("argument count error on entry");
|
2023-03-20 04:31:28 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(atest3) {
|
2023-03-26 06:16:52 +02:00
|
|
|
if (unlikely(ac != fixnum_obj(3))) fail("argument count error on entry");
|
2023-03-20 04:31:28 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(atest4) {
|
2023-03-26 06:16:52 +02:00
|
|
|
if (unlikely(ac != fixnum_obj(4))) fail("argument count error on entry");
|
2023-03-20 04:31:28 +01:00
|
|
|
gonexti();
|
|
|
|
}
|
2023-02-28 06:31:08 +01:00
|
|
|
|
2023-03-04 06:07:52 +01:00
|
|
|
define_instruction(scall1) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int m = 1, n = get_fixnum(*ip++);
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0);
|
|
|
|
ac = fixnum_obj(n); /* argc */
|
2023-03-30 01:02:18 +02:00
|
|
|
objmove_left(sp-n-m, sp-n, n);
|
2023-03-04 06:07:52 +01:00
|
|
|
sdrop(m); callsubi();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(scall10) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(0);
|
2023-02-28 06:31:08 +01:00
|
|
|
sdrop(1); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall11) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(1);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(1) = sref(0);
|
|
|
|
sdrop(1); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall12) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(2);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(2) = sref(1); sref(1) = sref(0);
|
|
|
|
sdrop(1); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall13) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(3);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(3) = sref(2); sref(2) = sref(1); sref(1) = sref(0);
|
|
|
|
sdrop(1); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall14) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(4);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(4) = sref(3); sref(3) = sref(2); sref(2) = sref(1); sref(1) = sref(0);
|
|
|
|
sdrop(1); callsubi();
|
|
|
|
}
|
|
|
|
|
2023-03-04 06:07:52 +01:00
|
|
|
define_instruction(scall2) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int m = 2, n = get_fixnum(*ip++);
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0);
|
|
|
|
ac = fixnum_obj(n);
|
2023-03-30 01:02:18 +02:00
|
|
|
objmove_left(sp-n-m, sp-n, n);
|
2023-03-04 06:07:52 +01:00
|
|
|
sdrop(m); callsubi();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(scall20) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(0);
|
2023-02-28 06:31:08 +01:00
|
|
|
sdrop(2); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall21) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(1);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(2) = sref(0);
|
|
|
|
sdrop(2); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall22) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(2);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(3) = sref(1); sref(2) = sref(0);
|
|
|
|
sdrop(2); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall23) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(3);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(4) = sref(2); sref(3) = sref(1); sref(2) = sref(0);
|
|
|
|
sdrop(2); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall24) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(4);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(5) = sref(3); sref(4) = sref(2); sref(3) = sref(1); sref(2) = sref(0);
|
|
|
|
sdrop(2); callsubi();
|
|
|
|
}
|
|
|
|
|
2023-03-04 06:07:52 +01:00
|
|
|
define_instruction(scall3) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int m = 3, n = get_fixnum(*ip++);
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0);
|
|
|
|
ac = fixnum_obj(n);
|
2023-03-30 01:02:18 +02:00
|
|
|
objmove_left(sp-n-m, sp-n, n);
|
2023-03-04 06:07:52 +01:00
|
|
|
sdrop(m); callsubi();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(scall30) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(0);
|
2023-02-28 06:31:08 +01:00
|
|
|
sdrop(3); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall31) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(1);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(3) = sref(0);
|
|
|
|
sdrop(3); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall32) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(2);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(4) = sref(1); sref(3) = sref(0);
|
|
|
|
sdrop(3); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall33) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(3);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(5) = sref(2); sref(4) = sref(1); sref(3) = sref(0);
|
|
|
|
sdrop(3); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall34) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(4);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(6) = sref(3); sref(5) = sref(2); sref(4) = sref(1); sref(3) = sref(0);
|
|
|
|
sdrop(3); callsubi();
|
|
|
|
}
|
|
|
|
|
2023-03-04 06:07:52 +01:00
|
|
|
define_instruction(scall4) {
|
2023-03-27 22:49:49 +02:00
|
|
|
int m = 4, n = get_fixnum(*ip++);
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0);
|
|
|
|
ac = fixnum_obj(n);
|
2023-03-30 01:02:18 +02:00
|
|
|
objmove_left(sp-n-m, sp-n, n);
|
2023-03-04 06:07:52 +01:00
|
|
|
sdrop(m); callsubi();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(scall40) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(0);
|
2023-02-28 06:31:08 +01:00
|
|
|
sdrop(4); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall41) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(1);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(4) = sref(0);
|
|
|
|
sdrop(4); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall42) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(2);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(5) = sref(1); sref(4) = sref(0);
|
|
|
|
sdrop(4); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall43) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(3);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(6) = sref(2); sref(5) = sref(1); sref(4) = sref(0);
|
|
|
|
sdrop(4); callsubi();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(scall44) {
|
2023-03-26 06:16:52 +02:00
|
|
|
ckx(ac); rd = ac; rx = fixnum_obj(0); ac = fixnum_obj(4);
|
2023-02-28 06:31:08 +01:00
|
|
|
sref(7) = sref(3); sref(6) = sref(2); sref(5) = sref(1); sref(4) = sref(0);
|
|
|
|
sdrop(4); callsubi();
|
|
|
|
}
|
|
|
|
|
2024-06-25 05:46:19 +02:00
|
|
|
/* <? combo gets in a way of decoding ;>? from (if (< 1 2 3) 4 5)
|
2023-02-28 06:31:08 +01:00
|
|
|
define_instruction(brnotlt) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
|
|
|
int dx = get_fixnum(*ip++);
|
|
|
|
ac = bool_obj(get_fixnum(x) < get_fixnum(y));
|
2023-02-28 06:31:08 +01:00
|
|
|
ip = ac ? ip : ip + dx;
|
|
|
|
gonexti();
|
|
|
|
} else {
|
2023-03-27 22:49:49 +02:00
|
|
|
int dx = get_fixnum(*ip++);
|
2023-02-28 06:31:08 +01:00
|
|
|
double fx, fy;
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(is_flonum(x))) fx = get_flonum(x);
|
|
|
|
else { cki(x); fx = (double)get_fixnum(x); }
|
|
|
|
if (likely(is_flonum(y))) fy = get_flonum(y);
|
|
|
|
else { cki(y); fy = (double)get_fixnum(y); }
|
2023-03-26 06:16:52 +02:00
|
|
|
ac = bool_obj(fx < fy);
|
2023-02-28 06:31:08 +01:00
|
|
|
ip = ac ? ip : ip + dx;
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
}
|
2024-06-25 05:46:19 +02:00
|
|
|
*/
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
define_instruction(pushsub) {
|
|
|
|
obj x = ac, y = spop();
|
2023-03-27 22:49:49 +02:00
|
|
|
if (likely(are_fixnums(x, y))) {
|
|
|
|
long lx = get_fixnum(x), ly = get_fixnum(y);
|
|
|
|
long long llz = (long long)lx - (long long)ly;
|
|
|
|
if (likely(llz >= FIXNUM_MIN && llz <= FIXNUM_MAX)) ac = fixnum_obj((long)llz);
|
|
|
|
else ac = flonum_obj((double)lx - (double)ly);
|
2023-02-28 06:31:08 +01:00
|
|
|
} else {
|
2023-03-27 22:49:49 +02:00
|
|
|
double dx, dy;
|
|
|
|
if (likely(is_flonum(x))) dx = get_flonum(x);
|
|
|
|
else if (likely(is_fixnum(x))) dx = (double)get_fixnum(x);
|
|
|
|
else failtype(x, "number");
|
|
|
|
if (likely(is_flonum(y))) dy = get_flonum(y);
|
|
|
|
else if (likely(is_fixnum(y))) dy = (double)get_fixnum(y);
|
|
|
|
else failtype(y, "number");
|
|
|
|
ac = flonum_obj(dx - dy);
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
spush(ac);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-26 23:10:51 +02:00
|
|
|
define_instruction(fexis) {
|
|
|
|
FILE *f; cks(ac);
|
|
|
|
f = fopen(stringchars(ac), "r"); /* todo: pile #ifdefs here */
|
|
|
|
if (f != NULL) fclose(f);
|
|
|
|
ac = bool_obj(f != NULL);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(frem) {
|
|
|
|
int res; cks(ac);
|
|
|
|
res = remove(stringchars(ac));
|
|
|
|
ac = bool_obj(res == 0);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(fren) {
|
|
|
|
int res; cks(ac); cks(sref(0));
|
|
|
|
res = rename(stringchars(ac), stringchars(sref(0)));
|
|
|
|
spop();
|
|
|
|
ac = bool_obj(res == 0);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2024-07-23 01:22:27 +02:00
|
|
|
define_instruction(getcwd) {
|
|
|
|
extern char *get_cwd(void);
|
|
|
|
char *s = get_cwd();
|
|
|
|
if (s) ac = string_obj(newstring(s));
|
|
|
|
else ac = bool_obj(0);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(setcwd) {
|
|
|
|
extern int set_cwd(char *cwd);
|
|
|
|
int res; cks(ac);
|
|
|
|
res = set_cwd(stringchars(ac));
|
|
|
|
ac = bool_obj(res == 0);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-28 06:32:05 +02:00
|
|
|
define_instruction(argvref) {
|
2024-07-17 19:55:07 +02:00
|
|
|
extern char *argv_ref(int idx);
|
2023-03-28 06:32:05 +02:00
|
|
|
int i; char *s; ckk(ac);
|
|
|
|
i = get_fixnum(ac); /* todo: range-check */
|
2024-07-17 19:55:07 +02:00
|
|
|
s = argv_ref(i);
|
2023-03-28 06:32:05 +02:00
|
|
|
if (s) ac = string_obj(newstring(s));
|
|
|
|
else ac = bool_obj(0);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-26 23:10:51 +02:00
|
|
|
define_instruction(getenv) {
|
|
|
|
char *v; cks(ac);
|
|
|
|
v = getenv(stringchars(ac));
|
|
|
|
if (v) ac = string_obj(newstring(v));
|
|
|
|
else ac = bool_obj(0);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2024-07-17 06:33:46 +02:00
|
|
|
define_instruction(envvref) {
|
2024-07-17 19:55:07 +02:00
|
|
|
extern char *envv_ref(int idx);
|
2024-07-17 06:33:46 +02:00
|
|
|
int i; char *s; ckk(ac);
|
|
|
|
i = get_fixnum(ac); /* todo: range-check */
|
2024-07-17 19:55:07 +02:00
|
|
|
s = envv_ref(i);
|
2024-07-17 06:33:46 +02:00
|
|
|
if (s) ac = string_obj(newstring(s));
|
|
|
|
else ac = bool_obj(0);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-03-26 23:10:51 +02:00
|
|
|
define_instruction(clock) {
|
|
|
|
double d = (double)clock();
|
|
|
|
ac = flonum_obj(d);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(clops) {
|
|
|
|
double d = (double)CLOCKS_PER_SEC;
|
|
|
|
ac = flonum_obj(d);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
|
|
|
define_instruction(cursec) {
|
|
|
|
double d = difftime(time(NULL), 0) + 37.0;
|
|
|
|
ac = flonum_obj(d);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2024-07-11 15:50:28 +02:00
|
|
|
define_instruction(system) {
|
|
|
|
int res; cks(ac);
|
|
|
|
res = system(stringchars(ac));
|
|
|
|
ac = fixnum_obj(res);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2024-07-24 23:42:28 +02:00
|
|
|
define_instruction(gc) {
|
|
|
|
extern int cxg_gccount;
|
|
|
|
hp_collect();
|
|
|
|
ac = fixnum_obj(cxg_gccount);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2024-07-17 05:03:00 +02:00
|
|
|
define_instruction(gccnt) {
|
|
|
|
extern int cxg_gccount;
|
|
|
|
ac = fixnum_obj(cxg_gccount);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2024-07-24 23:42:28 +02:00
|
|
|
define_instruction(bumpcnt) {
|
|
|
|
extern size_t cxg_bumpcount;
|
|
|
|
ac = fixnum_obj((int)cxg_bumpcount);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2024-07-17 05:03:00 +02:00
|
|
|
define_instruction(heapsz) {
|
|
|
|
extern size_t cxg_hsize;
|
|
|
|
ac = fixnum_obj((int)cxg_hsize);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2024-07-23 05:58:22 +02:00
|
|
|
define_instruction(dirsep) {
|
|
|
|
extern int dirsep;
|
|
|
|
ac = char_obj(dirsep);
|
|
|
|
gonexti();
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
#define VM_GEN_DEFGLOBAL
|
|
|
|
#include "i.h"
|
|
|
|
#undef VM_GEN_DEFGLOBAL
|
|
|
|
|
|
|
|
/* integrables table */
|
2023-03-18 19:17:45 +01:00
|
|
|
struct intgtab_entry { int sym; char *igname; int igtype; char *enc; char *lcode; };
|
|
|
|
#define declare_intgtable_entry(enc, igname, igtype, lcode) \
|
|
|
|
{ 0, igname, igtype, enc, lcode },
|
2023-02-28 06:31:08 +01:00
|
|
|
|
|
|
|
static struct intgtab_entry intgtab[] = {
|
|
|
|
#define VM_GEN_INTGTABLE
|
|
|
|
#include "i.h"
|
|
|
|
#undef VM_GEN_INTGTABLE
|
|
|
|
};
|
|
|
|
|
|
|
|
static int intgtab_sorted = 0;
|
|
|
|
static int intgtab_cmp(const void *p1, const void *p2)
|
|
|
|
{
|
2023-03-18 19:17:45 +01:00
|
|
|
int igtype1, igtype2;
|
2023-02-28 06:31:08 +01:00
|
|
|
struct intgtab_entry *pe1 = (struct intgtab_entry *)p1;
|
|
|
|
struct intgtab_entry *pe2 = (struct intgtab_entry *)p2;
|
|
|
|
if (pe1->sym < pe2->sym) return -1;
|
|
|
|
if (pe1->sym > pe2->sym) return 1;
|
2023-03-18 19:17:45 +01:00
|
|
|
igtype1 = pe1->igtype; if (igtype1 >= ' ') igtype1 = ' ';
|
|
|
|
igtype2 = pe2->igtype; if (igtype2 >= ' ') igtype2 = ' ';
|
|
|
|
if (igtype1 < igtype2) return -1;
|
|
|
|
if (igtype1 > igtype2) return 1;
|
2023-02-28 06:31:08 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sort_intgtab(int n)
|
|
|
|
{
|
|
|
|
if (!intgtab_sorted) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
struct intgtab_entry *pe = &intgtab[i];
|
|
|
|
pe->sym = pe->igname ? internsym(pe->igname) : 0;
|
|
|
|
}
|
|
|
|
qsort(&intgtab[0], n, sizeof(intgtab[0]), intgtab_cmp);
|
|
|
|
intgtab_sorted = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-19 00:13:38 +01:00
|
|
|
static int isintegrable(obj o)
|
|
|
|
{
|
|
|
|
int n = sizeof(intgtab)/sizeof(intgtab[0]);
|
2023-03-21 20:29:28 +01:00
|
|
|
if (is_fixnum_obj(o)) {
|
|
|
|
int i = fixnum_from_obj(o);
|
2023-03-19 00:13:38 +01:00
|
|
|
if (i >= 0 && i < n) {
|
|
|
|
struct intgtab_entry *pe = &intgtab[i];
|
|
|
|
return (pe && pe->igtype >= ' ' && pe->igname && pe->enc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct intgtab_entry *integrabledata(obj o)
|
|
|
|
{
|
|
|
|
int n = sizeof(intgtab)/sizeof(intgtab[0]);
|
2023-03-21 20:29:28 +01:00
|
|
|
int i = fixnum_from_obj(o);
|
2023-03-19 00:13:38 +01:00
|
|
|
struct intgtab_entry *pe = &intgtab[i];
|
|
|
|
assert(i >= 0 && i < n);
|
|
|
|
return pe;
|
|
|
|
}
|
|
|
|
|
|
|
|
static obj mkintegrable(struct intgtab_entry *pe)
|
2023-03-18 19:17:45 +01:00
|
|
|
{
|
|
|
|
int n = sizeof(intgtab)/sizeof(intgtab[0]);
|
2023-03-19 00:13:38 +01:00
|
|
|
assert(pe >= &intgtab[0] && pe < &intgtab[n]);
|
2023-03-21 20:29:28 +01:00
|
|
|
return obj_from_fixnum(pe-intgtab);
|
2023-03-18 19:17:45 +01:00
|
|
|
}
|
|
|
|
|
2024-07-07 05:04:35 +02:00
|
|
|
static int intgtab_count(void)
|
|
|
|
{
|
|
|
|
return sizeof(intgtab)/sizeof(intgtab[0]);
|
|
|
|
}
|
|
|
|
|
2023-03-18 19:17:45 +01:00
|
|
|
static struct intgtab_entry *lookup_integrable(int sym)
|
|
|
|
{
|
|
|
|
struct intgtab_entry e, *pe;
|
|
|
|
int n = sizeof(intgtab)/sizeof(intgtab[0]);
|
|
|
|
if (!intgtab_sorted) sort_intgtab(n);
|
|
|
|
e.sym = sym; e.igtype = ' ';
|
|
|
|
pe = bsearch(&e, &intgtab[0], n, sizeof(intgtab[0]), intgtab_cmp);
|
|
|
|
return (pe && pe->igtype >= ' ' && pe->igname && pe->enc) ? pe : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int integrable_type(struct intgtab_entry *pi)
|
|
|
|
{
|
|
|
|
int it = pi->igtype;
|
|
|
|
return (it >= ' ') ? it : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *integrable_global(struct intgtab_entry *pi)
|
|
|
|
{
|
|
|
|
return (pi->igtype >= ' ') ? pi->igname : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *integrable_code(struct intgtab_entry *pi, int n)
|
|
|
|
{
|
2023-03-20 04:31:28 +01:00
|
|
|
char *ps, *code = NULL; int it = pi->igtype;
|
2023-03-18 19:17:45 +01:00
|
|
|
if (it >= ' ') {
|
2023-03-20 04:31:28 +01:00
|
|
|
ps = pi->enc; assert(ps);
|
2023-03-18 19:17:45 +01:00
|
|
|
while (ps && n-- > 0) {
|
2023-03-20 04:31:28 +01:00
|
|
|
ps += strlen(ps) + 1; /* \0 terminates each field */
|
|
|
|
assert(*ps);
|
2023-03-18 19:17:45 +01:00
|
|
|
}
|
2023-03-20 04:31:28 +01:00
|
|
|
code = ps;
|
2023-03-18 19:17:45 +01:00
|
|
|
}
|
|
|
|
return code;
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* deserialization machinery */
|
|
|
|
|
|
|
|
static int rds_char(obj port)
|
|
|
|
{
|
|
|
|
int c = iportgetc(port);
|
|
|
|
if (c == '%') {
|
|
|
|
char buf[3];
|
|
|
|
buf[0] = iportgetc(port); buf[1] = iportgetc(port); buf[2] = 0;
|
|
|
|
c = (int)strtol(buf, NULL, 16);
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2023-03-26 19:20:33 +02:00
|
|
|
static int rds_byte(obj port)
|
|
|
|
{
|
|
|
|
char buf[3]; int b;
|
|
|
|
buf[0] = iportgetc(port); buf[1] = iportgetc(port); buf[2] = 0;
|
|
|
|
b = (int)strtoul(buf, NULL, 16);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2023-02-28 06:31:08 +01:00
|
|
|
static int rds_int(obj port)
|
|
|
|
{
|
|
|
|
char buf[60], *p = buf, *e = p+59;
|
|
|
|
while (p < e) {
|
|
|
|
int c = iportpeekc(port);
|
|
|
|
if (c == '-' || c == '+' || (c >= '0' && c <= '9')) {
|
|
|
|
iportgetc(port);
|
|
|
|
*p++ = c;
|
|
|
|
} else break;
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
return (int)strtol(buf, NULL, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double rds_real(obj port)
|
|
|
|
{
|
2023-03-24 21:34:11 +01:00
|
|
|
char buf[60], *p = buf, *e = p+59, *s; double d;
|
2023-02-28 06:31:08 +01:00
|
|
|
while (p < e) {
|
|
|
|
int c = iportpeekc(port);
|
2023-03-24 21:34:11 +01:00
|
|
|
if (c == '-' || c == '+' || c == '.' || isalnum(c)) {
|
2023-02-28 06:31:08 +01:00
|
|
|
iportgetc(port);
|
|
|
|
*p++ = c;
|
|
|
|
} else break;
|
|
|
|
}
|
|
|
|
*p = 0;
|
2023-03-24 21:34:11 +01:00
|
|
|
errno = 0; s = buf; e = "";
|
|
|
|
if (*s != '+' && *s != '-') d = strtod(s, &e);
|
|
|
|
else if (strcmp_ci(s+1, "inf.0") == 0) d = (*s == '-' ? -HUGE_VAL : HUGE_VAL);
|
|
|
|
else if (strcmp_ci(s+1, "nan.0") == 0) d = HUGE_VAL - HUGE_VAL;
|
|
|
|
else d = strtod(s, &e);
|
|
|
|
if (errno || e == s || *e) assert(0);
|
|
|
|
return d;
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t rds_size(obj port)
|
|
|
|
{
|
|
|
|
int n = rds_int(port);
|
|
|
|
if ((n < 0) || iportgetc(port) != ':') return 0; /* postpone failure */
|
|
|
|
return (size_t)n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* see below */
|
|
|
|
static obj *rds_sexp(obj *r, obj *sp, obj *hp);
|
|
|
|
|
|
|
|
/* protects registers from r to sp, in: ra=port, out: ra=sexp/eof */
|
|
|
|
static obj *rds_elt(obj *r, obj *sp, obj *hp)
|
|
|
|
{
|
|
|
|
obj port = ra;
|
|
|
|
spush(port);
|
|
|
|
hp = rds_sexp(r, sp, hp);
|
|
|
|
port = spop();
|
|
|
|
if (!iseof(ra) && iportgetc(port) != ';') ra = mkeof();
|
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* protects registers from r to sp, in: ra=port, out: ra=sexp/eof */
|
|
|
|
static obj *rds_sexp(obj *r, obj *sp, obj *hp)
|
|
|
|
{
|
|
|
|
obj port = ra;
|
|
|
|
int c = iportgetc(port);
|
|
|
|
switch (c) {
|
|
|
|
default: ra = mkeof(); break;
|
|
|
|
case 'f': ra = obj_from_bool(0); break;
|
|
|
|
case 't': ra = obj_from_bool(1); break;
|
|
|
|
case 'n': ra = mknull(); break;
|
|
|
|
case 'c': ra = obj_from_char(rds_char(port)); break;
|
|
|
|
case 'i': ra = obj_from_fixnum(rds_int(port)); break;
|
|
|
|
case 'j': ra = obj_from_flonum(sp-r, rds_real(port)); break;
|
|
|
|
case 'p': {
|
|
|
|
spush(port);
|
|
|
|
ra = sref(0); hp = rds_elt(r, sp, hp);
|
|
|
|
if (iseof(ra)) { sdrop(1); return hp; } else spush(ra);
|
|
|
|
ra = sref(1); hp = rds_elt(r, sp, hp);
|
|
|
|
if (iseof(ra)) { sdrop(2); return hp; } else spush(ra);
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz(), sp-r);
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0);
|
|
|
|
*--hp = sref(1);
|
2023-03-26 06:16:52 +02:00
|
|
|
ra = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
sdrop(3);
|
|
|
|
} break;
|
|
|
|
case 'l': {
|
|
|
|
size_t n = rds_size(port), i;
|
|
|
|
spush(port); /* sref(0)=port */
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
ra = sref((int)i); hp = rds_elt(r, sp, hp);
|
|
|
|
if (iseof(ra)) { sdrop(i+1); return hp; } else spush(ra);
|
|
|
|
}
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz()*n, sp-r);
|
2023-02-28 06:31:08 +01:00
|
|
|
ra = mknull();
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
*--hp = ra;
|
|
|
|
*--hp = sref((int)i);
|
2023-03-26 06:16:52 +02:00
|
|
|
ra = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
sdrop(n+1);
|
|
|
|
} break;
|
|
|
|
case 'v': {
|
|
|
|
size_t n = rds_size(port), i;
|
|
|
|
spush(port);
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
ra = sref((int)i); hp = rds_elt(r, sp, hp);
|
|
|
|
if (iseof(ra)) { sdrop(i+1); return hp; } else spush(ra);
|
|
|
|
}
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(vecbsz(n), sp-r);
|
2023-02-28 06:31:08 +01:00
|
|
|
hp -= n; memcpy(hp, sp-n, n*sizeof(obj));
|
2023-03-26 06:16:52 +02:00
|
|
|
ra = hend_vec(n);
|
2023-02-28 06:31:08 +01:00
|
|
|
sdrop(n+1);
|
|
|
|
} break;
|
|
|
|
case 's': case 'y': {
|
|
|
|
cbuf_t *pcb = newcb();
|
|
|
|
size_t n = rds_size(port), i;
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
int x = rds_char(port);
|
|
|
|
cbputc(x, pcb);
|
|
|
|
}
|
|
|
|
if (c == 's') ra = hpushstr(sp-r, newstring(cbdata(pcb)));
|
|
|
|
else ra = mksymbol(internsym(cbdata(pcb)));
|
|
|
|
freecb(pcb);
|
2023-03-26 19:20:33 +02:00
|
|
|
} break;
|
|
|
|
case 'b': {
|
|
|
|
cbuf_t *pcb = newcb();
|
|
|
|
size_t n = rds_size(port), i;
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
int x = rds_byte(port);
|
|
|
|
cbputc(x, pcb);
|
|
|
|
}
|
|
|
|
ra = hpushu8v(sp-r, newbytevector((unsigned char *)cbdata(pcb), (int)cblen(pcb)));
|
|
|
|
freecb(pcb);
|
2023-04-20 17:03:14 +02:00
|
|
|
} break;
|
|
|
|
case 'z': {
|
|
|
|
spush(port);
|
|
|
|
ra = sref(0); hp = rds_elt(r, sp, hp);
|
|
|
|
if (iseof(ra)) { sdrop(1); return hp; } else spush(ra);
|
|
|
|
hreserve(boxbsz(), sp-r);
|
|
|
|
*--hp = sref(0);
|
|
|
|
ra = hend_box();
|
|
|
|
sdrop(2);
|
|
|
|
} break;
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* protects registers from r to sp, in: ra=str, out: ra=sexp/eof */
|
|
|
|
static obj *rds_stox(obj *r, obj *sp, obj *hp)
|
|
|
|
{
|
|
|
|
int *d = dupstring(stringdata(ra));
|
|
|
|
ra = mkiport_string(sp-r, sialloc(sdatachars(d), d));
|
|
|
|
hp = rds_sexp(r, sp, hp); /* ra=port => ra=sexp/eof */
|
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct { obj *pg; const char *enc; int etyp; } enctab[] = {
|
|
|
|
#define VM_GEN_ENCTABLE
|
|
|
|
#define declare_enctable_entry(name, enc, etyp) \
|
|
|
|
{ &glue(cx_ins_2D, name), enc, etyp },
|
|
|
|
#include "i.h"
|
|
|
|
#undef declare_enctable_entry
|
|
|
|
#undef VM_GEN_ENCTABLE
|
|
|
|
{ NULL, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct emtrans { int c; struct embranch *pbr; struct emtrans *ptr; };
|
|
|
|
struct embranch { obj g; int etyp; struct emtrans *ptr; };
|
|
|
|
static struct embranch encmap = { 0, 0, NULL };
|
|
|
|
|
|
|
|
static void encmap_add(const char *enc, obj g, int etyp)
|
|
|
|
{
|
|
|
|
const char *pc = enc; struct embranch *pbr = &encmap;
|
|
|
|
down: while (*pc) {
|
|
|
|
int c = *pc++; struct emtrans *ptr = pbr->ptr;
|
|
|
|
while (ptr != NULL) {
|
|
|
|
if (ptr->c != c) {
|
|
|
|
if (ptr->ptr == NULL) break;
|
|
|
|
ptr = ptr->ptr; continue;
|
|
|
|
}
|
|
|
|
pbr = ptr->pbr; goto down;
|
|
|
|
}
|
|
|
|
if (!(ptr = calloc(1, sizeof(struct emtrans))))
|
|
|
|
{ perror("encmap[memt]"); exit(2); }
|
|
|
|
ptr->c = c; ptr->ptr = pbr->ptr; pbr->ptr = ptr;
|
|
|
|
if (!(ptr->pbr = calloc(1, sizeof(struct embranch))))
|
|
|
|
{ perror("encmap[memb]"); exit(2); }
|
|
|
|
pbr = ptr->pbr;
|
|
|
|
}
|
|
|
|
if (pbr->g != 0) {
|
|
|
|
perror("encmap[dup]"); exit(2);
|
|
|
|
}
|
|
|
|
pbr->g = g; pbr->etyp = etyp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct embranch *get_encmap(void)
|
|
|
|
{
|
|
|
|
if (encmap.ptr == NULL) {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; enctab[i].pg != NULL; ++i) {
|
|
|
|
if (enctab[i].enc != NULL) {
|
|
|
|
encmap_add(enctab[i].enc, *(enctab[i].pg), enctab[i].etyp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(encmap.g == 0);
|
|
|
|
assert(encmap.ptr != NULL);
|
|
|
|
}
|
|
|
|
return &encmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct embranch *rds_prefix(obj port)
|
|
|
|
{
|
|
|
|
struct embranch *pbr = get_encmap(); int c;
|
2023-03-04 06:07:52 +01:00
|
|
|
down: while ((c = iportpeekc(port)) != EOF) {
|
2023-02-28 06:31:08 +01:00
|
|
|
struct emtrans *ptr = pbr->ptr;
|
|
|
|
while (ptr != NULL) {
|
|
|
|
if (ptr->c != c) {
|
|
|
|
if (ptr->ptr == NULL) break;
|
|
|
|
ptr = ptr->ptr; continue;
|
|
|
|
}
|
|
|
|
pbr = ptr->pbr; iportgetc(port);
|
|
|
|
goto down;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return pbr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static obj lastpair(obj l)
|
|
|
|
{
|
2023-03-26 06:16:52 +02:00
|
|
|
obj p = bool_obj(0);
|
2023-02-28 06:31:08 +01:00
|
|
|
while (ispair(l)) { p = l; l = cdr(l); }
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fixnum_t length(obj l)
|
|
|
|
{
|
|
|
|
fixnum_t n = 0;
|
|
|
|
while (ispair(l)) { ++n; l = cdr(l); }
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* protects registers from r to sp, in: ra=list, out: ra=vec */
|
|
|
|
obj *revlist2vec(obj *r, obj *sp, obj *hp)
|
|
|
|
{
|
|
|
|
obj l; fixnum_t n = length(ra), i;
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(vecbsz(n), sp-r);
|
2023-03-27 22:49:49 +02:00
|
|
|
for (l = ra, i = 0; i < n; ++i, l = cdr(l)) *--hp = pair_car(l);
|
2023-03-26 06:16:52 +02:00
|
|
|
ra = hend_vec(n);
|
2023-02-28 06:31:08 +01:00
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* protects registers from r to sp, in: ra=code, out: ra=closure */
|
|
|
|
obj *close0(obj *r, obj *sp, obj *hp)
|
|
|
|
{
|
2023-03-03 21:44:12 +01:00
|
|
|
hreserve(vmclobsz(1), sp-r);
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = ra;
|
2023-03-26 06:16:52 +02:00
|
|
|
ra = hend_vmclo(1);
|
2023-02-28 06:31:08 +01:00
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* protects registers from r to sp, in: ra=port, out: ra=sexp/eof */
|
|
|
|
static obj *rds_arg(obj *r, obj *sp, obj *hp)
|
|
|
|
{
|
|
|
|
obj port = ra; int c = iportgetc(port);
|
|
|
|
if ('0' <= c && c <= '9') {
|
|
|
|
ra = obj_from_fixnum(c-'0');
|
|
|
|
} else {
|
|
|
|
if (c != '(') ra = mkeof(); else {
|
|
|
|
spush(port);
|
|
|
|
ra = sref(0); hp = rds_sexp(r, sp, hp);
|
|
|
|
port = sref(0);
|
|
|
|
if (!iseof(ra) && iportgetc(port) != ')') ra = mkeof();
|
|
|
|
sdrop(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* protects registers from r to sp, in: ra=sym, out: ra=loc/eof */
|
|
|
|
static obj *rds_global_loc(obj *r, obj *sp, obj *hp)
|
|
|
|
{
|
2024-07-11 01:28:27 +02:00
|
|
|
unsigned long long base;
|
|
|
|
if (issymbol(ra) && isvector(cx__2Aglobals_2A) && (base = vectorlen(cx__2Aglobals_2A)) > 0) {
|
|
|
|
unsigned long long v = (unsigned long long)ra; int i = (int)(v % base);
|
|
|
|
obj p = isassv(ra, vectorref(cx__2Aglobals_2A, i));
|
2023-03-12 19:20:35 +01:00
|
|
|
if (ispair(p)) ra = cdr(p);
|
2023-03-19 19:52:49 +01:00
|
|
|
else { /* prepend (sym . #&sym) to *globals* */
|
2024-07-11 01:28:27 +02:00
|
|
|
obj box, *pl;
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(boxbsz()*1+pairbsz()*2, sp-r);
|
|
|
|
*--hp = ra;
|
|
|
|
box = hend_box();
|
2023-03-12 19:20:35 +01:00
|
|
|
*--hp = box; *--hp = ra;
|
2023-03-26 06:16:52 +02:00
|
|
|
ra = hend_pair();
|
2024-07-11 01:28:27 +02:00
|
|
|
pl = &vectorref(cx__2Aglobals_2A, i);
|
|
|
|
*--hp = *pl; *--hp = ra;
|
|
|
|
*pl = hend_pair();
|
2023-03-12 19:20:35 +01:00
|
|
|
ra = box;
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ra = mkeof();
|
|
|
|
}
|
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* see below */
|
|
|
|
static obj *rds_seq(obj *r, obj *sp, obj *hp);
|
|
|
|
|
|
|
|
/* protects registers from r to sp, in: ra=port, out: ra=sexp/eof */
|
|
|
|
static obj *rds_block(obj *r, obj *sp, obj *hp)
|
|
|
|
{
|
|
|
|
obj port = ra; int c = iportgetc(port);
|
|
|
|
if (c != '{') ra = mkeof(); else {
|
|
|
|
spush(port);
|
|
|
|
ra = sref(0); hp = rds_seq(r, sp, hp);
|
|
|
|
port = sref(0);
|
|
|
|
if (!iseof(ra) && iportgetc(port) != '}') ra = mkeof();
|
|
|
|
sdrop(1);
|
|
|
|
}
|
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* protects registers from r to sp, in: ra=port, out: ra=sexp/eof */
|
|
|
|
static obj *rds_seq(obj *r, obj *sp, obj *hp)
|
|
|
|
{
|
|
|
|
obj port = ra; int c;
|
|
|
|
spush(port); /* sref[1] = port */
|
|
|
|
spush(mknull()); /* sref[0] = l */
|
|
|
|
more:
|
|
|
|
c = iportpeekc(sref(1));
|
|
|
|
if (c == EOF) {
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz(), sp-r);
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = glue(cx_ins_2D, halt);
|
2023-03-26 06:16:52 +02:00
|
|
|
ra = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
} else if (c == '}') {
|
|
|
|
ra = sref(0);
|
|
|
|
} else {
|
|
|
|
struct embranch *pbr = rds_prefix(sref(1));
|
|
|
|
if (pbr->g == 0) {
|
2023-03-04 06:07:52 +01:00
|
|
|
/* fprintf(stderr, "### rds_seq's remaining chars:\n");
|
|
|
|
while (iportpeekc(sref(1)) != EOF) fputc(iportgetc(sref(1)), stderr);
|
|
|
|
fputc('\n', stderr); */
|
2023-02-28 06:31:08 +01:00
|
|
|
ra = mkeof();
|
|
|
|
} else switch (pbr->etyp) {
|
|
|
|
case 0: {
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz(), sp-r);
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = pbr->g;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
goto more;
|
|
|
|
} break;
|
|
|
|
case 1: {
|
|
|
|
ra = sref(1); hp = rds_arg(r, sp, hp);
|
|
|
|
if (iseof(ra)) goto out;
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz()*2, sp-r);
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = pbr->g;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = ra;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
goto more;
|
|
|
|
} break;
|
|
|
|
case 2: {
|
|
|
|
ra = sref(1); hp = rds_arg(r, sp, hp);
|
|
|
|
if (iseof(ra)) goto out;
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz()*2, sp-r);
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = pbr->g;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = ra;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
ra = sref(1); hp = rds_arg(r, sp, hp);
|
|
|
|
if (iseof(ra)) goto out;
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz(), sp-r);
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = ra;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
goto more;
|
|
|
|
} break;
|
|
|
|
case 'f': case 't': case 'n': {
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz()*2, sp-r);
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = pbr->g;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = (pbr->etyp == 'n' ? mknull() : obj_from_bool(pbr->etyp == 't'));
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
goto more;
|
|
|
|
} break;
|
|
|
|
case 'g': { /* gref/gset */
|
|
|
|
ra = sref(1); hp = rds_arg(r, sp, hp);
|
|
|
|
if (iseof(ra)) goto out;
|
|
|
|
hp = rds_global_loc(r, sp, hp); /* ra => ra */
|
|
|
|
if (iseof(ra)) goto out;
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz()*2, sp-r);
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = pbr->g;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = ra;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
goto more;
|
|
|
|
} break;
|
2023-03-20 04:31:28 +01:00
|
|
|
case 'a': { /* andbo */
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz(), sp-r);
|
2023-03-20 04:31:28 +01:00
|
|
|
*--hp = sref(0); *--hp = pbr->g;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-03-20 04:31:28 +01:00
|
|
|
c = iportpeekc(sref(1));
|
|
|
|
if (c == EOF || c == '}') { ra = mkeof(); goto out; }
|
|
|
|
pbr = rds_prefix(sref(1));
|
|
|
|
if (pbr->g == 0 || pbr->etyp != 0) { ra = mkeof(); goto out; }
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz(), sp-r);
|
2023-03-20 04:31:28 +01:00
|
|
|
*--hp = sref(0); *--hp = pbr->g;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-03-20 04:31:28 +01:00
|
|
|
goto more;
|
|
|
|
} break;
|
2023-02-28 06:31:08 +01:00
|
|
|
case 's': { /* save */
|
|
|
|
fixnum_t n;
|
|
|
|
ra = sref(1); hp = rds_block(r, sp, hp);
|
|
|
|
if (iseof(ra)) goto out;
|
|
|
|
n = length(ra);
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz()*2, sp-r);
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = pbr->g;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = obj_from_fixnum(n);
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
if (n > 0) {
|
|
|
|
obj lp = lastpair(ra); assert(ispair(lp));
|
|
|
|
cdr(lp) = sref(0); sref(0) = ra;
|
|
|
|
}
|
|
|
|
goto more;
|
|
|
|
} break;
|
|
|
|
case 'd': { /* dclose */
|
|
|
|
fixnum_t n;
|
|
|
|
ra = sref(1); hp = rds_arg(r, sp, hp);
|
2023-03-20 04:31:28 +01:00
|
|
|
if (!is_fixnum_obj(ra)) { ra = mkeof(); goto out; }
|
2023-03-27 22:49:49 +02:00
|
|
|
n = get_fixnum(ra);
|
2023-02-28 06:31:08 +01:00
|
|
|
ra = sref(1); hp = rds_block(r, sp, hp);
|
|
|
|
if (iseof(ra)) goto out;
|
|
|
|
hp = revlist2vec(r, sp, hp); /* ra => ra */
|
|
|
|
if (iseof(ra)) goto out;
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz()*3, sp-r);
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = pbr->g;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = obj_from_fixnum(n);
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = ra;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
goto more;
|
|
|
|
} break;
|
|
|
|
case 'b': { /* branches */
|
|
|
|
fixnum_t n; int c;
|
|
|
|
ra = sref(1); hp = rds_block(r, sp, hp);
|
|
|
|
if (iseof(ra)) goto out;
|
|
|
|
c = iportpeekc(sref(1));
|
|
|
|
if (c == '{') { /* 2-arm branch combo */
|
|
|
|
obj bseq1 = ra, bseq2;
|
|
|
|
spush(bseq1); /* ... port, l, bseq1 <= sp */
|
|
|
|
ra = sref(2); hp = rds_block(r, sp, hp);
|
|
|
|
if (iseof(ra)) { sdrop(1); goto out; }
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz()*4, sp-r);
|
2023-02-28 06:31:08 +01:00
|
|
|
bseq1 = spop(); bseq2 = ra;
|
|
|
|
n = length(bseq1);
|
|
|
|
*--hp = sref(0); *--hp = pbr->g;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = obj_from_fixnum(n+2);
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
if (n > 0) {
|
|
|
|
obj lp = lastpair(bseq1); assert(ispair(lp));
|
|
|
|
cdr(lp) = sref(0); sref(0) = bseq1;
|
|
|
|
}
|
|
|
|
*--hp = sref(0); *--hp = glue(cx_ins_2D, br);
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
n = length(bseq2);
|
|
|
|
*--hp = sref(0); *--hp = obj_from_fixnum(n);
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
if (n > 0) {
|
|
|
|
obj lp = lastpair(bseq2); assert(ispair(lp));
|
|
|
|
cdr(lp) = sref(0); sref(0) = bseq2;
|
|
|
|
}
|
|
|
|
} else { /* regular 1-arm branch */
|
|
|
|
n = length(ra);
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz()*2, sp-r);
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = pbr->g;
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
*--hp = sref(0); *--hp = obj_from_fixnum(n);
|
2023-03-26 06:16:52 +02:00
|
|
|
sref(0) = hend_pair();
|
2023-02-28 06:31:08 +01:00
|
|
|
if (n > 0) {
|
|
|
|
obj lp = lastpair(ra); assert(ispair(lp));
|
|
|
|
cdr(lp) = sref(0); sref(0) = ra;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto more;
|
|
|
|
} break;
|
|
|
|
default: assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
sdrop(2);
|
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* protects registers from r to sp, in: ra=str, out: ra=codevec/eof */
|
|
|
|
static obj *rds_stoc(obj *r, obj *sp, obj *hp)
|
|
|
|
{
|
|
|
|
int *d = dupstring(stringdata(ra));
|
|
|
|
/* fprintf(stderr, "** rds_stoc(%s)\n", sdatachars(d)); */
|
|
|
|
ra = mkiport_string(sp-r, sialloc(sdatachars(d), d));
|
|
|
|
hp = rds_seq(r, sp, hp); /* ra=port => ra=revcodelist/eof */
|
|
|
|
if (!iseof(ra)) hp = revlist2vec(r, sp, hp); /* ra => ra */
|
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* protects registers from r to sp */
|
|
|
|
static obj *rds_intgtab(obj *r, obj *sp, obj *hp)
|
|
|
|
{
|
|
|
|
int i, n = sizeof(intgtab)/sizeof(intgtab[0]);
|
2023-03-20 04:31:28 +01:00
|
|
|
char lbuf[200], *lcode, *pe0, *pe1;
|
2023-02-28 06:31:08 +01:00
|
|
|
if (!intgtab_sorted) sort_intgtab(n);
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
struct intgtab_entry *pe = &intgtab[i];
|
|
|
|
if (!pe->igname) continue;
|
|
|
|
lcode = pe->lcode;
|
2023-03-18 19:17:45 +01:00
|
|
|
if (!lcode) switch (pe->igtype) {
|
2023-03-21 20:29:28 +01:00
|
|
|
case '0': {
|
2023-02-28 06:31:08 +01:00
|
|
|
lcode = lbuf; assert(pe->enc);
|
|
|
|
sprintf(lbuf, "%%0%s]0", pe->enc);
|
2023-03-20 05:23:42 +01:00
|
|
|
} break;
|
2023-03-21 20:29:28 +01:00
|
|
|
case '1': {
|
2023-02-28 06:31:08 +01:00
|
|
|
lcode = lbuf; assert(pe->enc);
|
2023-03-20 04:31:28 +01:00
|
|
|
sprintf(lbuf, "%%1_!%s]0", pe->enc);
|
2023-03-20 05:23:42 +01:00
|
|
|
} break;
|
2023-03-21 20:29:28 +01:00
|
|
|
case '2': {
|
2023-02-28 06:31:08 +01:00
|
|
|
lcode = lbuf; assert(pe->enc);
|
2023-03-20 04:31:28 +01:00
|
|
|
sprintf(lbuf, "%%2_!%s]0", pe->enc);
|
2023-03-20 05:23:42 +01:00
|
|
|
} break;
|
2023-03-21 20:29:28 +01:00
|
|
|
case '3': {
|
2023-02-28 06:31:08 +01:00
|
|
|
lcode = lbuf; assert(pe->enc);
|
2023-03-20 04:31:28 +01:00
|
|
|
sprintf(lbuf, "%%3_!%s]0", pe->enc);
|
2023-03-20 05:23:42 +01:00
|
|
|
} break;
|
2023-03-20 04:31:28 +01:00
|
|
|
case 'p': {
|
|
|
|
lcode = lbuf; assert(pe->enc);
|
|
|
|
pe0 = pe->enc; pe1 = pe0 + strlen(pe0) + 1; assert(*pe1);
|
|
|
|
sprintf(lbuf, "%%!0.0u?{%s]1}.0d,.1a,,#0.0,&1{%%2.1u?{.0]2}.1d,.2a,.2%s,:0^[22}.!0.0^_1[12", pe1, pe0);
|
|
|
|
} break;
|
|
|
|
case 'm': {
|
|
|
|
lcode = lbuf; assert(pe->enc);
|
|
|
|
pe0 = pe->enc; pe1 = pe0 + strlen(pe0) + 1; assert(*pe1);
|
|
|
|
sprintf(lbuf, "%%!1.0u?{.1%s]2}.0,.2,,#0.0,&1{%%2.1u?{.0]2}.1d,.2a,.2%s,:0^[22}.!0.0^_1[22", pe1, pe0);
|
|
|
|
} break;
|
2023-03-20 05:23:42 +01:00
|
|
|
case 'c': {
|
|
|
|
lcode = lbuf; assert(pe->enc);
|
|
|
|
sprintf(lbuf, "%%!0.0u,.0?{.0]2}.1d,.2a,,#0.0,&1{%%2.1u,.0?{.0]3}.2a,.0,.3%s?{.3d,.1,:0^[42}f]4}.!0.0^_1[22", pe->enc);
|
|
|
|
} break;
|
2023-03-20 18:49:00 +01:00
|
|
|
case 'x': {
|
|
|
|
lcode = lbuf; assert(pe->enc);
|
|
|
|
sprintf(lbuf, "%%!1.0,.2,,#0.0,&1{%%2.1u?{.0]2}.1d,.2a,.2%s,:0^[22}.!0.0^_1[22}", pe->enc);
|
|
|
|
} break;
|
2023-03-20 21:43:06 +01:00
|
|
|
case 'u': {
|
|
|
|
lcode = lbuf; assert(pe->enc);
|
|
|
|
pe0 = pe->enc; pe1 = pe0 + strlen(pe0) + 1; assert(*pe1);
|
|
|
|
sprintf(lbuf, "%%!0.0u?{%s%s]1}.0du?{.0a%s]1}%%%%", pe1, pe0, pe0);
|
|
|
|
} break;
|
|
|
|
case 'b': {
|
|
|
|
lcode = lbuf; assert(pe->enc);
|
|
|
|
pe0 = pe->enc; pe1 = pe0 + strlen(pe0) + 1; assert(*pe1);
|
|
|
|
sprintf(lbuf, "%%!1.0u?{%s,.2%s]2}.0du?{.0a,.2%s]2}%%%%", pe1, pe0, pe0);
|
|
|
|
} break;
|
2023-03-28 21:39:00 +02:00
|
|
|
case 't': {
|
|
|
|
lcode = lbuf; assert(pe->enc);
|
|
|
|
pe0 = pe->enc; pe1 = pe0 + strlen(pe0) + 1; assert(*pe1);
|
|
|
|
sprintf(lbuf, "%%!2.0u?{%s,.3,.3%s]3}.0du?{.0a,.3,.3%s]3}%%%%", pe1, pe0, pe0);
|
|
|
|
} break;
|
2023-03-21 03:32:33 +01:00
|
|
|
case '#': /* must have explicit lcode */
|
|
|
|
assert(0);
|
2023-03-21 18:43:26 +01:00
|
|
|
case '@': /* must have explicit lcode */
|
|
|
|
assert(0);
|
2023-03-21 03:32:33 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
if (!lcode || *lcode == 0) continue;
|
|
|
|
ra = mksymbol(internsym(pe->igname));
|
|
|
|
hp = rds_global_loc(r, sp, hp); /* ra->ra */
|
2023-03-12 19:20:35 +01:00
|
|
|
spush(ra); assert(isbox(ra));
|
2023-02-28 06:31:08 +01:00
|
|
|
ra = mkiport_string(sp-r, sialloc(lcode, NULL));
|
|
|
|
hp = rds_seq(r, sp, hp); /* ra=port => ra=revcodelist/eof */
|
|
|
|
if (!iseof(ra)) hp = revlist2vec(r, sp, hp); /* ra => ra */
|
|
|
|
if (!iseof(ra)) hp = close0(r, sp, hp); /* ra => ra */
|
2023-03-12 19:20:35 +01:00
|
|
|
if (!iseof(ra)) boxref(spop()) = ra;
|
2023-02-28 06:31:08 +01:00
|
|
|
}
|
|
|
|
return hp;
|
|
|
|
}
|
2023-03-03 19:18:00 +01:00
|
|
|
|
|
|
|
/* protects registers from r to sp=r+2; returns new hp */
|
|
|
|
static obj *init_module(obj *r, obj *sp, obj *hp, const char **mod)
|
|
|
|
{
|
|
|
|
const char **ent;
|
|
|
|
/* make sure we are called in a clean vm state */
|
2023-03-12 19:20:35 +01:00
|
|
|
assert(r == cxg_regs); assert(sp-r == VM_REGC); /* k, ra (for temp use) */
|
2023-03-03 19:18:00 +01:00
|
|
|
/* go over module entries and install/execute */
|
2023-03-22 23:13:12 +01:00
|
|
|
for (ent = mod; ent[0] != NULL || ent[1] != NULL; ent += 2) {
|
2023-03-03 19:18:00 +01:00
|
|
|
const char *name = ent[0], *data = ent[1];
|
2023-03-03 19:47:59 +01:00
|
|
|
/* fprintf(stderr, "## initializing: %s\n%s\n", name?name:"NULL", data); */
|
2023-03-22 23:13:12 +01:00
|
|
|
if (name != 0 && name[0] == 'S' && name[1] == 0) {
|
|
|
|
/* 'syntax' entry: skip prefix */
|
|
|
|
ent += 1; name = ent[0], data = ent[1];
|
|
|
|
assert(name != 0); assert(data != 0);
|
|
|
|
} else if (name != 0 && name[0] == 'C' && name[1] == 0) {
|
|
|
|
/* 'command' entry: skip prefix */
|
|
|
|
ent += 1; name = ent[0], data = ent[1];
|
|
|
|
assert(name == 0); assert(data != 0);
|
|
|
|
} else if (name != 0 && name[0] == 'P' && name[1] == 0) {
|
|
|
|
/* 'procedure' entry: make closure and install */
|
|
|
|
ent += 1; name = ent[0], data = ent[1];
|
|
|
|
assert(name != 0); assert(data != 0);
|
|
|
|
/* install code */
|
|
|
|
ra = mksymbol(internsym((char*)name));
|
|
|
|
hp = rds_global_loc(r, sp, hp); /* ra->ra */
|
|
|
|
spush(ra); assert(isbox(ra));
|
|
|
|
ra = mkiport_string(sp-r, sialloc((char*)data, NULL));
|
|
|
|
hp = rds_seq(r, sp, hp); /* ra=port => ra=revcodelist/eof */
|
|
|
|
if (!iseof(ra)) hp = revlist2vec(r, sp, hp); /* ra => ra */
|
|
|
|
if (!iseof(ra)) hp = close0(r, sp, hp); /* ra => ra */
|
|
|
|
if (!iseof(ra)) boxref(spop()) = ra;
|
|
|
|
continue;
|
2024-07-21 20:15:48 +02:00
|
|
|
} else if (name != 0 && name[0] == 'B' && name[1] == 0) {
|
|
|
|
/* 'builtin' entry: install itself into location */
|
|
|
|
obj sym, val, bnd, al;
|
|
|
|
ent += 1; name = ent[0], data = ent[1];
|
|
|
|
assert(name != 0);
|
|
|
|
sym = mksymbol(internsym((char*)name));
|
|
|
|
val = data ? mksymbol(internsym((char*)data)) : sym;
|
|
|
|
/* look for dst binding (we allow redefinition) */
|
|
|
|
for (bnd = 0, al = cx__2Atransformers_2A; al != mknull(); al = cdr(al)) {
|
|
|
|
obj ael = car(al);
|
|
|
|
if (car(ael) != sym) continue;
|
|
|
|
bnd = ael; break;
|
|
|
|
}
|
|
|
|
/* add new binding */
|
|
|
|
if (!bnd) { /* acons (sym . #f) */
|
|
|
|
hreserve(pairbsz()*2, sp-r);
|
|
|
|
*--hp = obj_from_bool(0); *--hp = sym;
|
|
|
|
bnd = hend_pair();
|
|
|
|
*--hp = cx__2Atransformers_2A; *--hp = bnd;
|
|
|
|
cx__2Atransformers_2A = hend_pair();
|
|
|
|
}
|
|
|
|
cdr(bnd) = val;
|
|
|
|
continue;
|
2023-03-22 23:13:12 +01:00
|
|
|
} else if (name != 0 && name[0] == 'A' && name[1] == 0) {
|
|
|
|
/* 'alias' entry: copy transformer */
|
2023-03-23 04:14:11 +01:00
|
|
|
obj oldsym, sym, oldbnd, oldden, bnd, al;
|
|
|
|
struct intgtab_entry *pe;
|
2023-03-22 23:13:12 +01:00
|
|
|
ent += 1; name = ent[0], data = ent[1];
|
|
|
|
assert(name != 0); assert(data != 0);
|
|
|
|
/* look for dst binding (we allow redefinition) */
|
|
|
|
oldsym = mksymbol(internsym((char*)data));
|
|
|
|
sym = mksymbol(internsym((char*)name));
|
2023-03-23 04:14:11 +01:00
|
|
|
for (oldbnd = 0, al = cx__2Atransformers_2A; al != mknull(); al = cdr(al)) {
|
2023-03-22 23:13:12 +01:00
|
|
|
obj ael = car(al);
|
|
|
|
if (car(ael) != oldsym) continue;
|
|
|
|
oldbnd = ael; break;
|
|
|
|
}
|
2023-03-23 04:14:11 +01:00
|
|
|
oldden = oldbnd ? cdr(oldbnd) : 0;
|
|
|
|
/* missing binding could be an auto-installed integrable */
|
|
|
|
if (!oldden && (pe = lookup_integrable(getsymbol(oldsym)))) {
|
|
|
|
oldden = mkintegrable(pe);
|
|
|
|
}
|
|
|
|
/* we should have it now */
|
|
|
|
assert(oldden); if (!oldden) continue;
|
2023-03-22 23:13:12 +01:00
|
|
|
/* look for existing binding (we allow redefinition) */
|
|
|
|
for (bnd = 0, al = cx__2Atransformers_2A; al != mknull(); al = cdr(al)) {
|
|
|
|
obj ael = car(al);
|
|
|
|
if (car(ael) != sym) continue;
|
|
|
|
bnd = ael; break;
|
|
|
|
}
|
|
|
|
/* or add new binding */
|
2023-03-23 04:14:11 +01:00
|
|
|
spush(oldden); /* protect from gc */
|
2023-03-22 23:13:12 +01:00
|
|
|
if (!bnd) { /* acons (sym . #f) */
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz()*2, sp-r);
|
2023-03-22 23:13:12 +01:00
|
|
|
*--hp = obj_from_bool(0); *--hp = sym;
|
2023-03-26 06:16:52 +02:00
|
|
|
bnd = hend_pair();
|
2023-03-22 23:13:12 +01:00
|
|
|
*--hp = cx__2Atransformers_2A; *--hp = bnd;
|
2023-03-26 06:16:52 +02:00
|
|
|
cx__2Atransformers_2A = hend_pair();
|
2023-03-22 23:13:12 +01:00
|
|
|
}
|
2023-03-23 04:14:11 +01:00
|
|
|
cdr(bnd) = spop(); /* oldden */
|
2023-03-22 23:13:12 +01:00
|
|
|
continue;
|
2023-03-30 05:18:39 +02:00
|
|
|
} else if (name != 0 && name[0] == 'K' && name[1] == 0) {
|
|
|
|
/* special entry for cx_continuation_2Dadapter_2Dcode */
|
|
|
|
ent += 1; name = ent[0], data = ent[1];
|
|
|
|
assert(name == 0); assert(data != 0);
|
|
|
|
ra = mkiport_string(sp-r, sialloc((char*)data, NULL));
|
|
|
|
hp = rds_seq(r, sp, hp); /* ra=port => ra=revcodelist/eof */
|
|
|
|
if (!iseof(ra)) hp = revlist2vec(r, sp, hp); /* ra => ra */
|
|
|
|
assert(!iseof(ra));
|
|
|
|
cx_continuation_2Dadapter_2Dcode = ra;
|
|
|
|
continue;
|
2023-03-22 23:13:12 +01:00
|
|
|
}
|
|
|
|
/* skipped prefix or no prefix */
|
2023-03-03 19:18:00 +01:00
|
|
|
if (name != NULL) {
|
|
|
|
/* install sexp-encoded syntax-rules as a transformer */
|
|
|
|
obj sym = mksymbol(internsym((char*)name));
|
|
|
|
obj al = cx__2Atransformers_2A, bnd = mknull();
|
|
|
|
assert(ispair(al)); /* basic transformers already installed */
|
|
|
|
/* look for existing binding (we allow redefinition) */
|
|
|
|
while (al != mknull()) {
|
|
|
|
obj ael = car(al);
|
|
|
|
if (car(ael) != sym) { al = cdr(al); continue; }
|
|
|
|
bnd = ael; break;
|
|
|
|
}
|
|
|
|
/* or add new binding */
|
|
|
|
if (bnd == mknull()) { /* acons (sym . #f) */
|
2023-03-26 06:16:52 +02:00
|
|
|
hreserve(pairbsz()*2, sp-r);
|
2023-03-03 19:18:00 +01:00
|
|
|
*--hp = obj_from_bool(0); *--hp = sym;
|
2023-03-26 06:16:52 +02:00
|
|
|
bnd = hend_pair();
|
2023-03-03 19:18:00 +01:00
|
|
|
*--hp = cx__2Atransformers_2A; *--hp = bnd;
|
2023-03-26 06:16:52 +02:00
|
|
|
cx__2Atransformers_2A = hend_pair();
|
2023-03-03 19:18:00 +01:00
|
|
|
}
|
|
|
|
/* sexp-decode data into the cdr of the binding */
|
|
|
|
spush(bnd); /* protect from gc */
|
|
|
|
ra = mkiport_string(sp-r, sialloc((char*)data, NULL));
|
|
|
|
hp = rds_sexp(r, sp, hp); /* ra=port => ra=sexp/eof */
|
|
|
|
bnd = spop();
|
|
|
|
assert(ispair(bnd) && (ispair(ra) || issymbol(ra)));
|
|
|
|
cdr(bnd) = ra;
|
|
|
|
} else {
|
|
|
|
/* execute code-encoded thunk */
|
|
|
|
obj *ip;
|
|
|
|
#ifdef VM_AC_IN_REG
|
2023-03-04 19:44:37 +01:00
|
|
|
obj ac;
|
2023-03-03 19:18:00 +01:00
|
|
|
#endif
|
|
|
|
ra = mkiport_string(sp-r, sialloc((char*)data, NULL));
|
|
|
|
hp = rds_seq(r, sp, hp); /* ra=port => ra=revcodelist/eof */
|
|
|
|
if (!iseof(ra)) hp = revlist2vec(r, sp, hp); /* ra => ra */
|
|
|
|
if (!iseof(ra)) hp = close0(r, sp, hp); /* ra => ra */
|
|
|
|
assert(!iseof(ra));
|
|
|
|
/* ra is a thunk closure to execute */
|
|
|
|
rd = ra;
|
|
|
|
ra = obj_from_fixnum(0); /* argc, shadow ac */
|
|
|
|
rx = obj_from_fixnum(0); /* shadow ip */
|
|
|
|
rs = obj_from_fixnum(VM_REGC); /* sp */
|
2023-03-12 19:20:35 +01:00
|
|
|
rz = (obj)(r + VM_STACK_GSZ); /* sp red zone */
|
2023-03-03 19:18:00 +01:00
|
|
|
do { /* unwindi trampoline */
|
|
|
|
reload_ac(); /* ra => ac */
|
|
|
|
reload_ip(); /* rd/rx => ip */
|
|
|
|
reload_sp(); /* rs => sp */
|
|
|
|
hp = (ins_from_obj(*ip))(IARGS1);
|
|
|
|
} while (trampcnd());
|
|
|
|
/* r[0] = k, r[1] = random result */
|
2023-03-12 19:20:35 +01:00
|
|
|
assert(r == cxg_regs);
|
|
|
|
sp = r + VM_REGC;
|
2023-03-03 19:18:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
2024-07-23 07:20:15 +02:00
|
|
|
/* hand-coded! */
|
2023-03-30 05:18:39 +02:00
|
|
|
char *i_code[] = {
|
|
|
|
|
2024-07-21 20:15:48 +02:00
|
|
|
/* initialize *transformers* with xform builtins */
|
|
|
|
"B", "syntax-quote", 0,
|
|
|
|
"B", "quote", 0,
|
|
|
|
"B", "set!", 0,
|
|
|
|
"B", "set&", 0,
|
|
|
|
"B", "if", 0,
|
|
|
|
"B", "lambda", 0,
|
|
|
|
"B", "lambda*", 0,
|
|
|
|
"B", "letcc", 0,
|
|
|
|
"B", "withcc", 0,
|
|
|
|
"B", "body", 0,
|
|
|
|
"B", "begin", 0,
|
|
|
|
"B", "define", 0,
|
|
|
|
"B", "define-syntax", 0,
|
|
|
|
"B", "syntax-lambda", 0,
|
|
|
|
"B", "syntax-rules", 0,
|
|
|
|
"B", "syntax-length", 0,
|
|
|
|
"B", "syntax-error", 0,
|
|
|
|
"B", "define-library", 0,
|
|
|
|
"B", "program", 0,
|
|
|
|
"B", "import", 0,
|
|
|
|
"B", "export", 0,
|
|
|
|
"B", "...", 0,
|
|
|
|
"B", "_", 0,
|
|
|
|
|
2023-03-31 00:13:07 +02:00
|
|
|
/* initialize current port variables */
|
|
|
|
"C", 0,
|
2023-03-31 01:41:22 +02:00
|
|
|
"P10Psi" "P11Pso" "P12Pse",
|
2023-03-31 00:13:07 +02:00
|
|
|
|
|
|
|
/* internal continuation switch code */
|
2023-03-30 05:18:39 +02:00
|
|
|
"P", "%dynamic-state-reroot!",
|
|
|
|
"%1.0,,#0.0,&1{%1.0,yq~?{${.2d,:0^[01}.0ad,.1aa,y,.1,.3c,.1sa.3,.1sdf,."
|
|
|
|
"4san,.4sd.3sy_1.0[30}]1}.!0.0^_1[11",
|
|
|
|
|
2023-03-30 19:08:16 +02:00
|
|
|
/* code for dynamic-wind's internal lambda is modified as follows:
|
|
|
|
* , save argc by pushing it on top of args in stack
|
|
|
|
* ${ push new frame for return from %dynamic-state-reroot!
|
|
|
|
* :0 get 'here' dynamic state from internal lambda's display
|
|
|
|
* , put it on the stack for dynamic-state-reroot!
|
|
|
|
* @(y22:%25dynamic-state-reroot!) get the d-s-r! procedure
|
|
|
|
* [01 call it with 1 argument ('here' dynamic state)
|
|
|
|
* } we will return here when d-s-r! is finished
|
|
|
|
* _! pop saved argc from stack into ac register
|
|
|
|
* K6 use sdmv opcode to return args from the lambda
|
|
|
|
* also, %x procedure checks inserted for early error detection
|
|
|
|
*/
|
|
|
|
"P", "dynamic-wind",
|
|
|
|
"%3y,${.2,.6%x,.5%xcc,@(y22:%25dynamic-state-reroot!)[01}.0,&1{,${:0,@("
|
|
|
|
"y22:%25dynamic-state-reroot!)[01}_!K6},.3,@(y16:call-with-values)[42",
|
|
|
|
|
2023-03-30 05:18:39 +02:00
|
|
|
/* code for the continuation adapter:
|
|
|
|
* k! first attempt; does not return if nothing to un/re-wind
|
|
|
|
* , save argc by pushing it on top of args in stack
|
|
|
|
* ${ push new frame for return from %dynamic-state-reroot!
|
|
|
|
* :0 get old dynamic state from continuation's display
|
2023-03-30 19:08:16 +02:00
|
|
|
* , put it on the stack for dynamic-state-reroot!
|
|
|
|
* @(y22:%25dynamic-state-reroot!) get the d-s-r! procedure
|
2023-03-30 05:18:39 +02:00
|
|
|
* [01 call it with 1 argument (old dynamic state)
|
|
|
|
* } we will return here when d-s-r! is finished
|
2023-03-30 19:08:16 +02:00
|
|
|
* _! pop saved argc from stack; we are ready to retry
|
2023-03-30 05:18:39 +02:00
|
|
|
* k! retry; should not return this time
|
|
|
|
* %% signal an (argument?) error if we return ?? */
|
|
|
|
"K", 0,
|
|
|
|
"k!,${:0,@(y22:%25dynamic-state-reroot!)[01}_!k!%%",
|
|
|
|
|
|
|
|
0, 0, 0
|
|
|
|
};
|
|
|
|
|
2023-03-03 19:18:00 +01:00
|
|
|
/* protects registers from r to sp=r+2; returns new hp */
|
|
|
|
static obj *init_modules(obj *r, obj *sp, obj *hp)
|
|
|
|
{
|
2023-03-30 05:18:39 +02:00
|
|
|
extern char* i_code[]; /* i.c */
|
2023-03-03 19:18:00 +01:00
|
|
|
extern char* s_code[]; /* s.c */
|
2023-03-04 06:07:52 +01:00
|
|
|
extern char* t_code[]; /* t.c */
|
2023-03-30 05:18:39 +02:00
|
|
|
hp = init_module(r, sp, hp, (const char **)i_code);
|
2023-03-04 17:51:51 +01:00
|
|
|
hp = init_module(r, sp, hp, (const char **)s_code);
|
|
|
|
hp = init_module(r, sp, hp, (const char **)t_code);
|
2023-03-03 19:18:00 +01:00
|
|
|
return hp;
|
|
|
|
}
|