2008-06-30 21:54:22 -04:00
|
|
|
/*
|
|
|
|
femtoLisp
|
|
|
|
|
2009-06-29 23:21:41 -04:00
|
|
|
a compact interpreter for a minimal lisp/scheme dialect
|
|
|
|
|
|
|
|
characteristics:
|
|
|
|
* lexical scope, lisp-1
|
|
|
|
* unrestricted macros
|
|
|
|
* data types: 30-bit integer, symbol, pair, vector, char, string, table
|
|
|
|
iostream, procedure, low-level data types
|
|
|
|
* case-sensitive
|
|
|
|
* simple compacting copying garbage collector
|
|
|
|
* Scheme-style varargs (dotted formal argument lists)
|
|
|
|
* "human-readable" bytecode with self-hosted compiler
|
|
|
|
|
|
|
|
extra features:
|
2008-06-30 21:54:22 -04:00
|
|
|
* circular structure can be printed and read
|
2009-06-29 23:21:41 -04:00
|
|
|
* #. read macro for eval-when-read and readably printing builtins
|
2008-06-30 21:54:22 -04:00
|
|
|
* read macros for backquote
|
|
|
|
* symbol character-escaping printer
|
|
|
|
* exceptions
|
|
|
|
* gensyms (can be usefully read back in, too)
|
2009-06-29 23:21:41 -04:00
|
|
|
* #| multiline comments |#, lots of other lexical syntax
|
2008-10-30 22:50:00 -04:00
|
|
|
* generic compare function, cyclic equal
|
2008-06-30 21:54:22 -04:00
|
|
|
* cvalues system providing C data types and a C FFI
|
|
|
|
* constructor notation for nicely printing arbitrary values
|
|
|
|
|
2009-01-04 21:45:21 -05:00
|
|
|
by Jeff Bezanson (C) 2009
|
2008-08-29 22:56:46 -04:00
|
|
|
Distributed under the BSD License
|
2008-06-30 21:54:22 -04:00
|
|
|
*/
|
|
|
|
|
2019-08-09 12:00:17 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2019-08-09 12:00:17 -04:00
|
|
|
#include <limits.h>
|
|
|
|
#include <locale.h>
|
2008-11-23 02:12:37 -05:00
|
|
|
#include <math.h>
|
2019-08-09 12:00:17 -04:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <wctype.h>
|
|
|
|
|
|
|
|
#include "dtypes.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "utf8.h"
|
|
|
|
#include "ios.h"
|
|
|
|
#include "socket.h"
|
|
|
|
#include "timefuncs.h"
|
|
|
|
#include "hashing.h"
|
|
|
|
#include "htable.h"
|
|
|
|
#include "htableh_inc.h"
|
|
|
|
#include "bitvector.h"
|
2019-08-13 18:15:29 -04:00
|
|
|
#include "os.h"
|
2019-08-09 12:00:17 -04:00
|
|
|
#include "random.h"
|
2008-06-30 21:54:22 -04:00
|
|
|
#include "llt.h"
|
2019-08-09 12:00:17 -04:00
|
|
|
|
|
|
|
#include "ieee754.h"
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
#include "flisp.h"
|
2019-08-09 15:08:44 -04:00
|
|
|
|
|
|
|
#include "argcount.h"
|
2019-08-09 17:35:16 -04:00
|
|
|
#include "env.h"
|
|
|
|
#include "opcodes.h"
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-13 16:07:44 -04:00
|
|
|
#include "../scheme-boot/boot_image.h"
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
static char *builtin_names[] = {
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
|
|
|
// predicates
|
|
|
|
"eq?", "eqv?", "equal?", "atom?", "not", "null?", "boolean?", "symbol?",
|
|
|
|
"number?", "bound?", "pair?", "builtin?", "vector?", "fixnum?",
|
|
|
|
"function?",
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// lists
|
|
|
|
"cons", "list", "car", "cdr", "set-car!", "set-cdr!",
|
2009-02-26 13:15:38 -05:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// execution
|
|
|
|
"apply",
|
2009-02-26 13:15:38 -05:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// arithmetic
|
|
|
|
"+", "-", "*", "/", "div0", "=", "<", "compare",
|
2009-02-26 13:15:38 -05:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// sequences
|
|
|
|
"vector", "aref", "aset!", "", "", ""
|
|
|
|
};
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-04-16 23:40:52 -04:00
|
|
|
#define ANYARGS -10000
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
static short builtin_arg_counts[] = {
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, ANYARGS, 1,
|
|
|
|
1, 2, 2, -2, ANYARGS, -1, ANYARGS, -1, 2, 2, 2, 2, ANYARGS, 2, 3
|
|
|
|
};
|
2009-04-16 23:40:52 -04:00
|
|
|
|
2009-07-03 14:43:15 -04:00
|
|
|
static uint32_t N_STACK;
|
|
|
|
static value_t *Stack;
|
2009-05-18 22:54:56 -04:00
|
|
|
static uint32_t SP = 0;
|
2009-07-03 14:43:15 -04:00
|
|
|
static uint32_t curr_frame = 0;
|
2009-05-18 22:54:56 -04:00
|
|
|
#define PUSH(v) (Stack[SP++] = (v))
|
2019-08-09 07:02:02 -04:00
|
|
|
#define POP() (Stack[--SP])
|
|
|
|
#define POPN(n) (SP -= (n))
|
2009-05-18 22:54:56 -04:00
|
|
|
|
|
|
|
#define N_GC_HANDLES 1024
|
|
|
|
static value_t *GCHandleStack[N_GC_HANDLES];
|
|
|
|
static uint32_t N_GCHND = 0;
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-08-09 14:04:03 -04:00
|
|
|
value_t FL_NIL, FL_T, FL_F, FL_EOF, QUOTE;
|
2008-06-30 21:54:22 -04:00
|
|
|
value_t IOError, ParseError, TypeError, ArgError, UnboundError, MemoryError;
|
2009-03-21 22:05:26 -04:00
|
|
|
value_t DivideError, BoundsError, Error, KeyError, EnumerationError;
|
2009-08-13 00:09:35 -04:00
|
|
|
value_t printwidthsym, printreadablysym, printprettysym, printlengthsym;
|
2010-05-05 01:31:46 -04:00
|
|
|
value_t printlevelsym, builtins_table_sym;
|
2009-08-08 17:44:14 -04:00
|
|
|
|
2009-08-09 14:04:03 -04:00
|
|
|
static value_t NIL, LAMBDA, IF, TRYCATCH;
|
2009-08-08 17:44:14 -04:00
|
|
|
static value_t BACKQUOTE, COMMA, COMMAAT, COMMADOT, FUNCTION;
|
|
|
|
|
|
|
|
static value_t pairsym, symbolsym, fixnumsym, vectorsym, builtinsym, vu8sym;
|
2010-12-23 01:49:37 -05:00
|
|
|
static value_t definesym, defmacrosym, forsym, setqsym;
|
2009-08-08 17:44:14 -04:00
|
|
|
static value_t tsym, Tsym, fsym, Fsym, booleansym, nullsym, evalsym, fnsym;
|
|
|
|
// for reading characters
|
2019-08-09 07:02:02 -04:00
|
|
|
static value_t nulsym, alarmsym, backspacesym, tabsym, linefeedsym,
|
|
|
|
newlinesym;
|
2009-08-08 17:44:14 -04:00
|
|
|
static value_t vtabsym, pagesym, returnsym, escsym, spacesym, deletesym;
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-04-09 00:04:27 -04:00
|
|
|
static value_t apply_cl(uint32_t nargs);
|
2008-06-30 21:54:22 -04:00
|
|
|
static value_t *alloc_words(int n);
|
|
|
|
static value_t relocate(value_t v);
|
|
|
|
|
2019-08-09 12:26:42 -04:00
|
|
|
static struct fl_readstate *readstate = NULL;
|
2010-05-02 14:17:47 -04:00
|
|
|
|
2019-08-09 12:26:42 -04:00
|
|
|
static void free_readstate(struct fl_readstate *rs)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2008-11-23 02:12:37 -05:00
|
|
|
htable_free(&rs->backrefs);
|
|
|
|
htable_free(&rs->gensyms);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char *fromspace;
|
|
|
|
static unsigned char *tospace;
|
|
|
|
static unsigned char *curheap;
|
|
|
|
static unsigned char *lim;
|
2019-08-09 07:02:02 -04:00
|
|
|
static uint32_t heapsize; // bytes
|
2008-11-23 02:12:37 -05:00
|
|
|
static uint32_t *consflags;
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// error utilities
|
|
|
|
// ------------------------------------------------------------
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
// saved execution state for an unwind target
|
2019-08-09 12:18:27 -04:00
|
|
|
struct fl_exception_context *fl_ctx = NULL;
|
2019-08-09 07:02:02 -04:00
|
|
|
uint32_t fl_throwing_frame = 0; // active frame when exception was thrown
|
2010-05-02 14:17:47 -04:00
|
|
|
value_t fl_lasterror;
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 12:18:27 -04:00
|
|
|
#define FL_TRY \
|
|
|
|
struct fl_exception_context _ctx; \
|
|
|
|
int l__tr, l__ca; \
|
|
|
|
_ctx.sp = SP; \
|
|
|
|
_ctx.frame = curr_frame; \
|
|
|
|
_ctx.rdst = readstate; \
|
|
|
|
_ctx.prev = fl_ctx; \
|
|
|
|
_ctx.ngchnd = N_GCHND; \
|
|
|
|
fl_ctx = &_ctx; \
|
|
|
|
if (!setjmp(_ctx.buf)) \
|
2019-08-09 07:02:02 -04:00
|
|
|
for (l__tr = 1; l__tr; l__tr = 0, (void)(fl_ctx = fl_ctx->prev))
|
|
|
|
|
|
|
|
#define FL_CATCH \
|
|
|
|
else for (l__ca = 1; l__ca; l__ca = 0, fl_lasterror = FL_NIL, \
|
|
|
|
fl_throwing_frame = 0, SP = _ctx.sp, \
|
|
|
|
curr_frame = _ctx.frame)
|
2010-05-02 14:17:47 -04:00
|
|
|
|
2019-08-09 12:18:27 -04:00
|
|
|
void fl_savestate(struct fl_exception_context *_ctx)
|
2010-05-02 14:17:47 -04:00
|
|
|
{
|
|
|
|
_ctx->sp = SP;
|
|
|
|
_ctx->frame = curr_frame;
|
|
|
|
_ctx->rdst = readstate;
|
|
|
|
_ctx->prev = fl_ctx;
|
|
|
|
_ctx->ngchnd = N_GCHND;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:18:27 -04:00
|
|
|
void fl_restorestate(struct fl_exception_context *_ctx)
|
2010-05-02 14:17:47 -04:00
|
|
|
{
|
|
|
|
fl_lasterror = FL_NIL;
|
|
|
|
fl_throwing_frame = 0;
|
|
|
|
SP = _ctx->sp;
|
|
|
|
curr_frame = _ctx->frame;
|
|
|
|
}
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-08-28 20:54:51 -04:00
|
|
|
void fl_raise(value_t e)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
struct fl_exception_context *thisctx;
|
|
|
|
|
2010-05-02 14:17:47 -04:00
|
|
|
fl_lasterror = e;
|
2008-06-30 21:54:22 -04:00
|
|
|
// unwind read state
|
2010-05-02 14:17:47 -04:00
|
|
|
while (readstate != fl_ctx->rdst) {
|
2008-06-30 21:54:22 -04:00
|
|
|
free_readstate(readstate);
|
|
|
|
readstate = readstate->prev;
|
|
|
|
}
|
2010-05-02 14:17:47 -04:00
|
|
|
if (fl_throwing_frame == 0)
|
|
|
|
fl_throwing_frame = curr_frame;
|
|
|
|
N_GCHND = fl_ctx->ngchnd;
|
2019-08-18 18:14:09 -04:00
|
|
|
thisctx = fl_ctx;
|
2019-08-09 07:02:02 -04:00
|
|
|
if (fl_ctx->prev) // don't throw past toplevel
|
2010-05-02 14:17:47 -04:00
|
|
|
fl_ctx = fl_ctx->prev;
|
2008-06-30 21:54:22 -04:00
|
|
|
longjmp(thisctx->buf, 1);
|
|
|
|
}
|
|
|
|
|
2009-02-18 22:31:40 -05:00
|
|
|
static value_t make_error_msg(char *format, va_list args)
|
|
|
|
{
|
|
|
|
char msgbuf[512];
|
2019-08-18 18:14:09 -04:00
|
|
|
|
2009-02-18 22:31:40 -05:00
|
|
|
vsnprintf(msgbuf, sizeof(msgbuf), format, args);
|
|
|
|
return string_from_cstr(msgbuf);
|
|
|
|
}
|
|
|
|
|
2009-03-24 22:28:21 -04:00
|
|
|
void lerrorf(value_t e, char *format, ...)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
|
|
|
va_list args;
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t msg;
|
|
|
|
|
2009-02-20 00:11:05 -05:00
|
|
|
PUSH(e);
|
2008-06-30 21:54:22 -04:00
|
|
|
va_start(args, format);
|
2019-08-18 18:14:09 -04:00
|
|
|
msg = make_error_msg(format, args);
|
2008-06-30 21:54:22 -04:00
|
|
|
va_end(args);
|
2009-02-20 00:11:05 -05:00
|
|
|
e = POP();
|
2010-04-29 14:01:26 -04:00
|
|
|
fl_raise(fl_list2(e, msg));
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2009-03-24 22:28:21 -04:00
|
|
|
void lerror(value_t e, const char *msg)
|
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t m;
|
|
|
|
|
2009-03-24 22:28:21 -04:00
|
|
|
PUSH(e);
|
2019-08-18 18:14:09 -04:00
|
|
|
m = cvalue_static_cstring(msg);
|
2009-03-24 22:28:21 -04:00
|
|
|
e = POP();
|
2010-04-29 14:01:26 -04:00
|
|
|
fl_raise(fl_list2(e, m));
|
2009-03-24 22:28:21 -04:00
|
|
|
}
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
void type_error(char *fname, char *expected, value_t got)
|
|
|
|
{
|
2010-04-29 14:01:26 -04:00
|
|
|
fl_raise(fl_listn(4, TypeError, symbol(fname), symbol(expected), got));
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void bounds_error(char *fname, value_t arr, value_t ind)
|
|
|
|
{
|
2010-04-29 14:01:26 -04:00
|
|
|
fl_raise(fl_listn(4, BoundsError, symbol(fname), arr, ind));
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// safe cast operators
|
|
|
|
// --------------------------------------------------------
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-08-23 03:06:57 -04:00
|
|
|
#define isstring fl_isstring
|
2019-08-18 18:14:09 -04:00
|
|
|
// TODO: Remove the spurious return statement.
|
2019-08-09 07:02:02 -04:00
|
|
|
#define SAFECAST_OP(type, ctype, cnvt) \
|
|
|
|
ctype to##type(value_t v, char *fname) \
|
|
|
|
{ \
|
|
|
|
if (is##type(v)) \
|
|
|
|
return (ctype)cnvt(v); \
|
|
|
|
type_error(fname, #type, v); \
|
2019-08-18 18:14:09 -04:00
|
|
|
return (ctype)FL_NIL; \
|
2019-08-09 07:02:02 -04:00
|
|
|
}
|
2019-08-09 12:28:14 -04:00
|
|
|
SAFECAST_OP(cons, struct cons *, ptr)
|
2019-08-09 12:30:15 -04:00
|
|
|
SAFECAST_OP(symbol, struct symbol *, ptr)
|
2019-08-09 07:02:02 -04:00
|
|
|
SAFECAST_OP(fixnum, fixnum_t, numval)
|
2019-08-09 12:36:20 -04:00
|
|
|
SAFECAST_OP(cvalue, struct cvalue *, ptr)
|
2019-08-09 07:02:02 -04:00
|
|
|
SAFECAST_OP(string, char *, cvalue_data)
|
2009-08-23 03:06:57 -04:00
|
|
|
#undef isstring
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// symbol table
|
|
|
|
// ---------------------------------------------------------------
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 12:30:15 -04:00
|
|
|
struct symbol *symtab = NULL;
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 17:30:20 -04:00
|
|
|
int fl_is_keyword_name(const char *str, size_t len)
|
2009-07-16 21:30:26 -04:00
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
return ((str[0] == ':' || str[len - 1] == ':') && str[1] != '\0');
|
2009-07-16 21:30:26 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 17:30:20 -04:00
|
|
|
static struct symbol *mk_symbol(const char *str)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-09 12:30:15 -04:00
|
|
|
struct symbol *sym;
|
2008-11-23 02:12:37 -05:00
|
|
|
size_t len = strlen(str);
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 12:30:15 -04:00
|
|
|
sym =
|
|
|
|
(struct symbol *)malloc(sizeof(struct symbol) - sizeof(void *) + len + 1);
|
2019-08-09 14:07:16 -04:00
|
|
|
assert(((uintptr_t)sym & 0x7) == 0); // make sure malloc aligns 8
|
2008-06-30 21:54:22 -04:00
|
|
|
sym->left = sym->right = NULL;
|
2009-07-28 00:16:20 -04:00
|
|
|
sym->flags = 0;
|
2009-07-16 21:30:26 -04:00
|
|
|
if (fl_is_keyword_name(str, len)) {
|
2008-10-30 22:50:00 -04:00
|
|
|
value_t s = tagptr(sym, TAG_SYM);
|
|
|
|
setc(s, s);
|
2009-07-28 00:16:20 -04:00
|
|
|
sym->flags |= 0x2;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2008-10-30 22:50:00 -04:00
|
|
|
sym->binding = UNBOUND;
|
|
|
|
}
|
2009-01-02 18:00:21 -05:00
|
|
|
sym->type = sym->dlcache = NULL;
|
2019-08-09 07:02:02 -04:00
|
|
|
sym->hash = memhash32(str, len) ^ 0xAAAAAAAA;
|
2019-08-09 13:14:42 -04:00
|
|
|
memcpy(&sym->name[0], str, len + 1);
|
2008-06-30 21:54:22 -04:00
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:30:20 -04:00
|
|
|
static struct symbol **symtab_lookup(struct symbol **ptree, const char *str)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
|
|
|
int x;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
while (*ptree != NULL) {
|
2008-06-30 21:54:22 -04:00
|
|
|
x = strcmp(str, (*ptree)->name);
|
|
|
|
if (x == 0)
|
|
|
|
return ptree;
|
|
|
|
if (x < 0)
|
|
|
|
ptree = &(*ptree)->left;
|
|
|
|
else
|
|
|
|
ptree = &(*ptree)->right;
|
|
|
|
}
|
|
|
|
return ptree;
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:30:20 -04:00
|
|
|
value_t symbol(const char *str)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-09 12:30:15 -04:00
|
|
|
struct symbol **pnode;
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
pnode = symtab_lookup(&symtab, str);
|
|
|
|
if (*pnode == NULL)
|
|
|
|
*pnode = mk_symbol(str);
|
|
|
|
return tagptr(*pnode, TAG_SYM);
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
static uint32_t _gensym_ctr = 0;
|
2008-06-30 21:54:22 -04:00
|
|
|
// two static buffers for gensym printing so there can be two
|
|
|
|
// gensym names available at a time, mostly for compare()
|
|
|
|
static char gsname[2][16];
|
2019-08-09 07:02:02 -04:00
|
|
|
static int gsnameno = 0;
|
2009-05-12 21:13:40 -04:00
|
|
|
value_t fl_gensym(value_t *args, uint32_t nargs)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
struct gensym *gs;
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
(void)args;
|
2019-08-18 18:14:09 -04:00
|
|
|
argcount("gensym", nargs, 0);
|
|
|
|
gs = (struct gensym *)alloc_words(sizeof(struct gensym) / sizeof(void *));
|
2008-06-30 21:54:22 -04:00
|
|
|
gs->id = _gensym_ctr++;
|
|
|
|
gs->binding = UNBOUND;
|
2009-07-05 23:56:48 -04:00
|
|
|
gs->isconst = 0;
|
2008-12-10 23:04:17 -05:00
|
|
|
gs->type = NULL;
|
2008-06-30 21:54:22 -04:00
|
|
|
return tagptr(gs, TAG_SYM);
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
int fl_isgensym(value_t v) { return isgensym(v); }
|
2010-05-04 20:00:37 -04:00
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_gensymp(value_t *args, uint32_t nargs)
|
2009-08-09 13:05:40 -04:00
|
|
|
{
|
|
|
|
argcount("gensym?", nargs, 1);
|
|
|
|
return isgensym(args[0]) ? FL_T : FL_F;
|
|
|
|
}
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
char *symbol_name(value_t v)
|
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
struct gensym *gs;
|
|
|
|
char *n;
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
if (ismanaged(v)) {
|
2019-08-18 18:14:09 -04:00
|
|
|
gs = (struct gensym *)ptr(v);
|
2019-08-09 07:02:02 -04:00
|
|
|
gsnameno = 1 - gsnameno;
|
2019-08-18 18:14:09 -04:00
|
|
|
n = uint2str(gsname[gsnameno] + 1, sizeof(gsname[0]) - 1, gs->id, 10);
|
2009-03-11 10:52:37 -04:00
|
|
|
*(--n) = 'g';
|
|
|
|
return n;
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
2019-08-09 12:30:15 -04:00
|
|
|
return ((struct symbol *)ptr(v))->name;
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// conses
|
|
|
|
// ---------------------------------------------------------------------
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
void gc(int mustgrow);
|
|
|
|
|
|
|
|
static value_t mk_cons(void)
|
|
|
|
{
|
2019-08-09 12:28:14 -04:00
|
|
|
struct cons *c;
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2008-12-28 03:01:18 -05:00
|
|
|
if (__unlikely(curheap > lim))
|
2008-06-30 21:54:22 -04:00
|
|
|
gc(0);
|
2019-08-09 12:28:14 -04:00
|
|
|
c = (struct cons *)curheap;
|
|
|
|
curheap += sizeof(struct cons);
|
2008-06-30 21:54:22 -04:00
|
|
|
return tagptr(c, TAG_CONS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static value_t *alloc_words(int n)
|
|
|
|
{
|
|
|
|
value_t *first;
|
|
|
|
|
2008-08-07 01:08:10 -04:00
|
|
|
assert(n > 0);
|
2019-08-09 07:02:02 -04:00
|
|
|
n = LLT_ALIGN(n, 2); // only allocate multiples of 2 words
|
|
|
|
if (__unlikely((value_t *)curheap > ((value_t *)lim) + 2 - n)) {
|
2008-06-30 21:54:22 -04:00
|
|
|
gc(0);
|
2019-08-09 07:02:02 -04:00
|
|
|
while ((value_t *)curheap > ((value_t *)lim) + 2 - n) {
|
2008-06-30 21:54:22 -04:00
|
|
|
gc(1);
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
first = (value_t *)curheap;
|
|
|
|
curheap += (n * sizeof(value_t));
|
2008-06-30 21:54:22 -04:00
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate n consecutive conses
|
|
|
|
#define cons_reserve(n) tagptr(alloc_words((n)*2), TAG_CONS)
|
|
|
|
|
2019-08-09 12:28:14 -04:00
|
|
|
#define cons_index(c) (((struct cons *)ptr(c)) - ((struct cons *)fromspace))
|
2019-08-09 07:02:02 -04:00
|
|
|
#define ismarked(c) bitvector_get(consflags, cons_index(c))
|
|
|
|
#define mark_cons(c) bitvector_set(consflags, cons_index(c), 1)
|
2008-06-30 21:54:22 -04:00
|
|
|
#define unmark_cons(c) bitvector_set(consflags, cons_index(c), 0)
|
|
|
|
|
2009-04-20 20:56:05 -04:00
|
|
|
static value_t the_empty_vector;
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
value_t alloc_vector(size_t n, int init)
|
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t *c;
|
|
|
|
value_t v;
|
|
|
|
unsigned int i;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
if (n == 0)
|
|
|
|
return the_empty_vector;
|
2019-08-18 18:14:09 -04:00
|
|
|
c = alloc_words(n + 1);
|
|
|
|
v = tagptr(c, TAG_VECTOR);
|
2008-06-30 21:54:22 -04:00
|
|
|
vector_setsize(v, n);
|
|
|
|
if (init) {
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 0; i < n; i++)
|
2010-01-06 13:27:28 -05:00
|
|
|
vector_elt(v, i) = FL_UNSPECIFIED;
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// cvalues
|
|
|
|
// --------------------------------------------------------------------
|
2008-12-21 00:55:00 -05:00
|
|
|
|
2019-08-09 12:00:17 -04:00
|
|
|
#include "ptrhash.h"
|
|
|
|
#include "operators.h"
|
2019-08-09 11:30:02 -04:00
|
|
|
#include "cvalues.h"
|
2019-08-09 12:00:17 -04:00
|
|
|
#include "equalhash.h"
|
2019-08-09 11:30:02 -04:00
|
|
|
#include "types.h"
|
2008-12-21 00:55:00 -05:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// print
|
|
|
|
// ----------------------------------------------------------------------
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
static int isnumtok(char *tok, value_t *pval);
|
2019-08-09 14:35:20 -04:00
|
|
|
static int symchar(char c);
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 11:30:02 -04:00
|
|
|
#include "print.h"
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// collector
|
|
|
|
// ------------------------------------------------------------------
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-05-18 22:54:56 -04:00
|
|
|
void fl_gc_handle(value_t *pv)
|
|
|
|
{
|
|
|
|
if (N_GCHND >= N_GC_HANDLES)
|
|
|
|
lerror(MemoryError, "out of gc handles");
|
|
|
|
GCHandleStack[N_GCHND++] = pv;
|
|
|
|
}
|
|
|
|
|
2009-08-02 00:06:07 -04:00
|
|
|
void fl_free_gc_handles(uint32_t n)
|
2009-05-18 22:54:56 -04:00
|
|
|
{
|
|
|
|
assert(N_GCHND >= n);
|
|
|
|
N_GCHND -= n;
|
|
|
|
}
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
static value_t relocate(value_t v)
|
|
|
|
{
|
|
|
|
value_t a, d, nc, first, *pcdr;
|
2019-08-18 18:14:09 -04:00
|
|
|
uintptr_t t;
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-18 18:14:09 -04:00
|
|
|
t = tag(v);
|
2009-01-02 18:00:21 -05:00
|
|
|
if (t == TAG_CONS) {
|
2008-06-30 21:54:22 -04:00
|
|
|
// iterative implementation allows arbitrarily long cons chains
|
|
|
|
pcdr = &first;
|
|
|
|
do {
|
2019-08-09 07:02:02 -04:00
|
|
|
if ((a = car_(v)) == TAG_FWD) {
|
2008-06-30 21:54:22 -04:00
|
|
|
*pcdr = cdr_(v);
|
|
|
|
return first;
|
|
|
|
}
|
2019-08-09 12:28:14 -04:00
|
|
|
*pcdr = nc = tagptr((struct cons *)curheap, TAG_CONS);
|
|
|
|
curheap += sizeof(struct cons);
|
2008-06-30 21:54:22 -04:00
|
|
|
d = cdr_(v);
|
2019-08-09 07:02:02 -04:00
|
|
|
car_(v) = TAG_FWD;
|
|
|
|
cdr_(v) = nc;
|
2008-06-30 21:54:22 -04:00
|
|
|
car_(nc) = relocate(a);
|
|
|
|
pcdr = &cdr_(nc);
|
|
|
|
v = d;
|
|
|
|
} while (iscons(v));
|
2019-08-09 07:02:02 -04:00
|
|
|
*pcdr = (d == NIL) ? NIL : relocate(d);
|
2008-06-30 21:54:22 -04:00
|
|
|
return first;
|
|
|
|
}
|
2009-01-02 18:00:21 -05:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
if ((t & 3) == 0)
|
|
|
|
return v;
|
|
|
|
if (!ismanaged(v))
|
|
|
|
return v;
|
|
|
|
if (isforwarded(v))
|
|
|
|
return forwardloc(v);
|
2009-01-02 18:00:21 -05:00
|
|
|
|
|
|
|
if (t == TAG_VECTOR) {
|
2008-10-30 22:50:00 -04:00
|
|
|
// N.B.: 0-length vectors secretly have space for a first element
|
2009-05-13 00:03:13 -04:00
|
|
|
size_t i, sz = vector_size(v);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (vector_elt(v, -1) & 0x1) {
|
2009-05-13 00:03:13 -04:00
|
|
|
// grown vector
|
2019-08-09 07:02:02 -04:00
|
|
|
nc = relocate(vector_elt(v, 0));
|
2009-05-13 00:03:13 -04:00
|
|
|
forward(v, nc);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
|
|
|
nc = tagptr(alloc_words(sz + 1), TAG_VECTOR);
|
2009-05-13 00:03:13 -04:00
|
|
|
vector_setsize(nc, sz);
|
2019-08-09 07:02:02 -04:00
|
|
|
a = vector_elt(v, 0);
|
2009-05-13 00:03:13 -04:00
|
|
|
forward(v, nc);
|
|
|
|
if (sz > 0) {
|
2019-08-09 07:02:02 -04:00
|
|
|
vector_elt(nc, 0) = relocate(a);
|
|
|
|
for (i = 1; i < sz; i++)
|
|
|
|
vector_elt(nc, i) = relocate(vector_elt(v, i));
|
2009-05-13 00:03:13 -04:00
|
|
|
}
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
2008-08-04 21:43:12 -04:00
|
|
|
return nc;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (t == TAG_CPRIM) {
|
2019-08-09 12:25:43 -04:00
|
|
|
struct cprim *pcp = (struct cprim *)ptr(v);
|
2019-08-09 07:02:02 -04:00
|
|
|
size_t nw = CPRIM_NWORDS - 1 + NWORDS(cp_class(pcp)->size);
|
2019-08-09 12:25:43 -04:00
|
|
|
struct cprim *ncp = (struct cprim *)alloc_words(nw);
|
2009-01-02 18:00:21 -05:00
|
|
|
while (nw--)
|
2019-08-09 07:02:02 -04:00
|
|
|
((value_t *)ncp)[nw] = ((value_t *)pcp)[nw];
|
2009-01-02 18:00:21 -05:00
|
|
|
nc = tagptr(ncp, TAG_CPRIM);
|
|
|
|
forward(v, nc);
|
|
|
|
return nc;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (t == TAG_CVALUE) {
|
2008-08-04 21:43:12 -04:00
|
|
|
return cvalue_relocate(v);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (t == TAG_FUNCTION) {
|
2019-08-09 12:23:30 -04:00
|
|
|
struct function *fn = (struct function *)ptr(v);
|
|
|
|
struct function *nfn = (struct function *)alloc_words(4);
|
2009-04-28 00:10:18 -04:00
|
|
|
nfn->bcode = fn->bcode;
|
|
|
|
nfn->vals = fn->vals;
|
|
|
|
nc = tagptr(nfn, TAG_FUNCTION);
|
|
|
|
forward(v, nc);
|
|
|
|
nfn->env = relocate(fn->env);
|
|
|
|
nfn->vals = relocate(nfn->vals);
|
|
|
|
nfn->bcode = relocate(nfn->bcode);
|
2010-05-04 14:17:55 -04:00
|
|
|
assert(!ismanaged(fn->name));
|
2009-07-10 00:17:39 -04:00
|
|
|
nfn->name = fn->name;
|
2009-04-28 00:10:18 -04:00
|
|
|
return nc;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (t == TAG_SYM) {
|
2019-08-09 12:26:33 -04:00
|
|
|
struct gensym *gs = (struct gensym *)ptr(v);
|
|
|
|
struct gensym *ng =
|
|
|
|
(struct gensym *)alloc_words(sizeof(struct gensym) / sizeof(void *));
|
2008-10-30 22:50:00 -04:00
|
|
|
ng->id = gs->id;
|
|
|
|
ng->binding = gs->binding;
|
2009-07-05 23:56:48 -04:00
|
|
|
ng->isconst = 0;
|
2008-06-30 21:54:22 -04:00
|
|
|
nc = tagptr(ng, TAG_SYM);
|
2008-10-30 22:50:00 -04:00
|
|
|
forward(v, nc);
|
2008-06-30 21:54:22 -04:00
|
|
|
if (ng->binding != UNBOUND)
|
|
|
|
ng->binding = relocate(ng->binding);
|
|
|
|
return nc;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
value_t relocate_lispvalue(value_t v) { return relocate(v); }
|
2008-11-28 16:44:59 -05:00
|
|
|
|
2019-08-09 12:30:15 -04:00
|
|
|
static void trace_globals(struct symbol *root)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
|
|
|
while (root != NULL) {
|
2008-10-30 22:50:00 -04:00
|
|
|
if (root->binding != UNBOUND)
|
|
|
|
root->binding = relocate(root->binding);
|
2008-06-30 21:54:22 -04:00
|
|
|
trace_globals(root->left);
|
|
|
|
root = root->right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-20 00:11:05 -05:00
|
|
|
static value_t memory_exception_value;
|
2008-07-26 00:03:48 -04:00
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
void gc(int mustgrow)
|
|
|
|
{
|
|
|
|
static int grew = 0;
|
|
|
|
void *temp;
|
2009-07-03 14:43:15 -04:00
|
|
|
uint32_t i, f, top;
|
2019-08-09 12:26:42 -04:00
|
|
|
struct fl_readstate *rs;
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
curheap = tospace;
|
2010-07-06 19:13:54 -04:00
|
|
|
if (grew)
|
2019-08-09 12:28:14 -04:00
|
|
|
lim = curheap + heapsize * 2 - sizeof(struct cons);
|
2010-07-06 19:13:54 -04:00
|
|
|
else
|
2019-08-09 12:28:14 -04:00
|
|
|
lim = curheap + heapsize - sizeof(struct cons);
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2010-05-02 14:17:47 -04:00
|
|
|
if (fl_throwing_frame > curr_frame) {
|
|
|
|
top = fl_throwing_frame - 4;
|
2019-08-09 07:02:02 -04:00
|
|
|
f = Stack[fl_throwing_frame - 4];
|
|
|
|
} else {
|
2009-07-05 23:56:48 -04:00
|
|
|
top = SP;
|
|
|
|
f = curr_frame;
|
|
|
|
}
|
2009-07-03 14:43:15 -04:00
|
|
|
while (1) {
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = f; i < top; i++)
|
2009-07-03 14:43:15 -04:00
|
|
|
Stack[i] = relocate(Stack[i]);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (f == 0)
|
|
|
|
break;
|
2009-07-03 14:43:15 -04:00
|
|
|
top = f - 4;
|
2019-08-09 07:02:02 -04:00
|
|
|
f = Stack[f - 4];
|
2009-07-03 14:43:15 -04:00
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 0; i < N_GCHND; i++)
|
2009-05-18 22:54:56 -04:00
|
|
|
*GCHandleStack[i] = relocate(*GCHandleStack[i]);
|
2008-06-30 21:54:22 -04:00
|
|
|
trace_globals(symtab);
|
2008-12-10 23:04:17 -05:00
|
|
|
relocate_typetable();
|
2008-06-30 21:54:22 -04:00
|
|
|
rs = readstate;
|
|
|
|
while (rs) {
|
2010-05-04 14:17:55 -04:00
|
|
|
value_t ent;
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 0; i < rs->backrefs.size; i++) {
|
2010-05-04 14:17:55 -04:00
|
|
|
ent = (value_t)rs->backrefs.table[i];
|
2010-05-04 20:00:37 -04:00
|
|
|
if (ent != (value_t)HT_NOTFOUND)
|
2019-08-09 07:02:02 -04:00
|
|
|
rs->backrefs.table[i] = (void *)relocate(ent);
|
2010-05-04 14:17:55 -04:00
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 0; i < rs->gensyms.size; i++) {
|
2010-05-04 14:17:55 -04:00
|
|
|
ent = (value_t)rs->gensyms.table[i];
|
2010-05-04 20:00:37 -04:00
|
|
|
if (ent != (value_t)HT_NOTFOUND)
|
2019-08-09 07:02:02 -04:00
|
|
|
rs->gensyms.table[i] = (void *)relocate(ent);
|
2010-05-04 14:17:55 -04:00
|
|
|
}
|
2009-02-23 21:21:16 -05:00
|
|
|
rs->source = relocate(rs->source);
|
2008-06-30 21:54:22 -04:00
|
|
|
rs = rs->prev;
|
|
|
|
}
|
2010-05-02 14:17:47 -04:00
|
|
|
fl_lasterror = relocate(fl_lasterror);
|
2009-02-20 00:11:05 -05:00
|
|
|
memory_exception_value = relocate(memory_exception_value);
|
2009-04-20 20:56:05 -04:00
|
|
|
the_empty_vector = relocate(the_empty_vector);
|
2008-12-20 01:16:00 -05:00
|
|
|
|
|
|
|
sweep_finalizers();
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
#ifdef VERBOSEGC
|
2008-12-22 01:36:50 -05:00
|
|
|
printf("GC: found %d/%d live conses\n",
|
2019-08-09 12:28:14 -04:00
|
|
|
(curheap - tospace) / sizeof(struct cons),
|
|
|
|
heapsize / sizeof(struct cons));
|
2008-06-30 21:54:22 -04:00
|
|
|
#endif
|
|
|
|
temp = tospace;
|
|
|
|
tospace = fromspace;
|
|
|
|
fromspace = temp;
|
|
|
|
|
|
|
|
// if we're using > 80% of the space, resize tospace so we have
|
|
|
|
// more space to fill next time. if we grew tospace last time,
|
|
|
|
// grow the other half of the heap this time to catch up.
|
2019-08-09 07:02:02 -04:00
|
|
|
if (grew || ((lim - curheap) < (int)(heapsize / 5)) || mustgrow) {
|
|
|
|
temp = LLT_REALLOC(tospace, heapsize * 2);
|
2008-06-30 21:54:22 -04:00
|
|
|
if (temp == NULL)
|
2009-08-28 20:54:51 -04:00
|
|
|
fl_raise(memory_exception_value);
|
2008-06-30 21:54:22 -04:00
|
|
|
tospace = temp;
|
2010-07-06 19:13:54 -04:00
|
|
|
if (grew) {
|
2019-08-09 07:02:02 -04:00
|
|
|
heapsize *= 2;
|
|
|
|
temp =
|
2019-08-09 12:28:14 -04:00
|
|
|
bitvector_resize(consflags, 0, heapsize / sizeof(struct cons), 1);
|
2008-06-30 21:54:22 -04:00
|
|
|
if (temp == NULL)
|
2009-08-28 20:54:51 -04:00
|
|
|
fl_raise(memory_exception_value);
|
2019-08-09 07:02:02 -04:00
|
|
|
consflags = (uint32_t *)temp;
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
grew = !grew;
|
|
|
|
}
|
|
|
|
if (curheap > lim) // all data was live
|
|
|
|
gc(0);
|
|
|
|
}
|
|
|
|
|
2013-02-11 04:48:21 -05:00
|
|
|
static void grow_stack(void)
|
2009-07-20 00:57:17 -04:00
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
size_t newsz = N_STACK + (N_STACK >> 1);
|
|
|
|
value_t *ns = realloc(Stack, newsz * sizeof(value_t));
|
2009-07-20 00:57:17 -04:00
|
|
|
if (ns == NULL)
|
|
|
|
lerror(MemoryError, "stack overflow");
|
|
|
|
Stack = ns;
|
|
|
|
N_STACK = newsz;
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// utils
|
|
|
|
// ----------------------------------------------------------------------
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-04-16 23:40:52 -04:00
|
|
|
// apply function with n args on the stack
|
|
|
|
static value_t _applyn(uint32_t n)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
value_t f = Stack[SP - n - 1];
|
2009-04-20 20:56:05 -04:00
|
|
|
uint32_t saveSP = SP;
|
|
|
|
value_t v;
|
2009-04-28 00:10:18 -04:00
|
|
|
if (iscbuiltin(f)) {
|
2019-08-09 07:02:02 -04:00
|
|
|
v = ((builtin_t *)ptr(f))[3](&Stack[SP - n], n);
|
|
|
|
} else if (isfunction(f)) {
|
2009-04-20 20:56:05 -04:00
|
|
|
v = apply_cl(n);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (isbuiltin(f)) {
|
2010-05-05 01:31:46 -04:00
|
|
|
value_t tab = symbol_value(builtins_table_sym);
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - n - 1] = vector_elt(tab, uintval(f));
|
2010-05-05 01:31:46 -04:00
|
|
|
v = apply_cl(n);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2009-04-28 00:10:18 -04:00
|
|
|
type_error("apply", "function", f);
|
|
|
|
}
|
|
|
|
SP = saveSP;
|
|
|
|
return v;
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2010-04-29 14:01:26 -04:00
|
|
|
value_t fl_apply(value_t f, value_t l)
|
2009-03-13 18:26:44 -04:00
|
|
|
{
|
2009-04-16 23:40:52 -04:00
|
|
|
value_t v = l;
|
|
|
|
uint32_t n = SP;
|
|
|
|
|
2009-04-14 20:12:01 -04:00
|
|
|
PUSH(f);
|
2009-04-16 23:40:52 -04:00
|
|
|
while (iscons(v)) {
|
2009-07-20 00:57:17 -04:00
|
|
|
if (SP >= N_STACK)
|
|
|
|
grow_stack();
|
2009-04-16 23:40:52 -04:00
|
|
|
PUSH(car_(v));
|
|
|
|
v = cdr_(v);
|
|
|
|
}
|
|
|
|
n = SP - n - 1;
|
|
|
|
v = _applyn(n);
|
2019-08-09 07:02:02 -04:00
|
|
|
POPN(n + 1);
|
2009-04-14 20:12:01 -04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2010-04-29 14:01:26 -04:00
|
|
|
value_t fl_applyn(uint32_t n, value_t f, ...)
|
2009-04-14 20:12:01 -04:00
|
|
|
{
|
|
|
|
va_list ap;
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t v;
|
2009-04-14 20:12:01 -04:00
|
|
|
size_t i;
|
|
|
|
|
2019-08-18 18:14:09 -04:00
|
|
|
va_start(ap, f);
|
2009-04-14 20:12:01 -04:00
|
|
|
PUSH(f);
|
2019-08-09 07:02:02 -04:00
|
|
|
while (SP + n > N_STACK)
|
2009-07-20 00:57:17 -04:00
|
|
|
grow_stack();
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 0; i < n; i++) {
|
2009-04-14 20:12:01 -04:00
|
|
|
value_t a = va_arg(ap, value_t);
|
|
|
|
PUSH(a);
|
|
|
|
}
|
2019-08-18 18:14:09 -04:00
|
|
|
v = _applyn(n);
|
2019-08-09 07:02:02 -04:00
|
|
|
POPN(n + 1);
|
2010-02-05 14:56:10 -05:00
|
|
|
va_end(ap);
|
2009-04-14 20:12:01 -04:00
|
|
|
return v;
|
2009-03-13 18:26:44 -04:00
|
|
|
}
|
|
|
|
|
2010-04-29 14:01:26 -04:00
|
|
|
value_t fl_listn(size_t n, ...)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
struct cons *c;
|
|
|
|
struct cons *l;
|
2008-06-30 21:54:22 -04:00
|
|
|
va_list ap;
|
2019-08-18 18:14:09 -04:00
|
|
|
uint32_t si;
|
2008-06-30 21:54:22 -04:00
|
|
|
size_t i;
|
|
|
|
|
2019-08-18 18:14:09 -04:00
|
|
|
si = SP;
|
|
|
|
va_start(ap, n);
|
2019-08-09 07:02:02 -04:00
|
|
|
while (SP + n > N_STACK)
|
2009-07-20 00:57:17 -04:00
|
|
|
grow_stack();
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 0; i < n; i++) {
|
2008-06-30 21:54:22 -04:00
|
|
|
value_t a = va_arg(ap, value_t);
|
|
|
|
PUSH(a);
|
|
|
|
}
|
2019-08-18 18:14:09 -04:00
|
|
|
c = (struct cons *)alloc_words(n * 2);
|
|
|
|
l = c;
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 0; i < n; i++) {
|
2008-06-30 21:54:22 -04:00
|
|
|
c->car = Stack[si++];
|
2019-08-09 07:02:02 -04:00
|
|
|
c->cdr = tagptr(c + 1, TAG_CONS);
|
2008-06-30 21:54:22 -04:00
|
|
|
c++;
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
(c - 1)->cdr = NIL;
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
POPN(n);
|
|
|
|
va_end(ap);
|
|
|
|
return tagptr(l, TAG_CONS);
|
|
|
|
}
|
|
|
|
|
2010-04-29 14:01:26 -04:00
|
|
|
value_t fl_list2(value_t a, value_t b)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
struct cons *c;
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
PUSH(a);
|
|
|
|
PUSH(b);
|
2019-08-18 18:14:09 -04:00
|
|
|
c = (struct cons *)alloc_words(4);
|
2008-06-30 21:54:22 -04:00
|
|
|
b = POP();
|
|
|
|
a = POP();
|
|
|
|
c[0].car = a;
|
2019-08-09 07:02:02 -04:00
|
|
|
c[0].cdr = tagptr(c + 1, TAG_CONS);
|
2008-06-30 21:54:22 -04:00
|
|
|
c[1].car = b;
|
|
|
|
c[1].cdr = NIL;
|
|
|
|
return tagptr(c, TAG_CONS);
|
|
|
|
}
|
|
|
|
|
|
|
|
value_t fl_cons(value_t a, value_t b)
|
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t c;
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
PUSH(a);
|
|
|
|
PUSH(b);
|
2019-08-18 18:14:09 -04:00
|
|
|
c = mk_cons();
|
2008-06-30 21:54:22 -04:00
|
|
|
cdr_(c) = POP();
|
|
|
|
car_(c) = POP();
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2009-08-23 03:06:57 -04:00
|
|
|
int fl_isnumber(value_t v)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
struct cprim *c;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
if (isfixnum(v))
|
|
|
|
return 1;
|
2009-08-23 01:07:46 -04:00
|
|
|
if (iscprim(v)) {
|
2019-08-18 18:14:09 -04:00
|
|
|
c = (struct cprim *)ptr(v);
|
2009-08-23 01:07:46 -04:00
|
|
|
return c->type != wchartype;
|
|
|
|
}
|
|
|
|
return 0;
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// read
|
|
|
|
// -----------------------------------------------------------------------
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 11:30:02 -04:00
|
|
|
#include "read.h"
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// equal
|
|
|
|
// ----------------------------------------------------------------------
|
2009-07-10 00:17:39 -04:00
|
|
|
|
2019-08-09 11:30:02 -04:00
|
|
|
#include "equal.h"
|
2009-07-10 00:17:39 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// eval
|
|
|
|
// -----------------------------------------------------------------------
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
#define list(a, n) _list((a), (n), 0)
|
2009-05-31 14:58:09 -04:00
|
|
|
|
|
|
|
static value_t _list(value_t *args, uint32_t nargs, int star)
|
2008-12-30 23:45:08 -05:00
|
|
|
{
|
2019-08-09 12:28:14 -04:00
|
|
|
struct cons *c;
|
2009-01-16 09:12:35 -05:00
|
|
|
uint32_t i;
|
2009-04-14 20:12:01 -04:00
|
|
|
value_t v;
|
2019-08-18 18:14:09 -04:00
|
|
|
|
2009-04-14 20:12:01 -04:00
|
|
|
v = cons_reserve(nargs);
|
2019-08-09 12:28:14 -04:00
|
|
|
c = (struct cons *)ptr(v);
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 0; i < nargs; i++) {
|
2009-04-14 20:12:01 -04:00
|
|
|
c->car = args[i];
|
2019-08-09 07:02:02 -04:00
|
|
|
c->cdr = tagptr(c + 1, TAG_CONS);
|
2008-12-30 23:45:08 -05:00
|
|
|
c++;
|
|
|
|
}
|
2009-07-20 00:57:17 -04:00
|
|
|
if (star)
|
2019-08-09 07:02:02 -04:00
|
|
|
(c - 2)->cdr = (c - 1)->car;
|
2009-03-26 23:06:55 -04:00
|
|
|
else
|
2019-08-09 07:02:02 -04:00
|
|
|
(c - 1)->cdr = NIL;
|
2009-04-14 20:12:01 -04:00
|
|
|
return v;
|
2008-12-30 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
2009-12-03 15:12:06 -05:00
|
|
|
static value_t copy_list(value_t L)
|
2009-05-11 19:55:40 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t *plcons;
|
|
|
|
value_t *pL;
|
|
|
|
value_t c;
|
|
|
|
|
2009-12-03 15:12:06 -05:00
|
|
|
if (!iscons(L))
|
|
|
|
return NIL;
|
2009-05-11 19:55:40 -04:00
|
|
|
PUSH(NIL);
|
|
|
|
PUSH(L);
|
2019-08-18 18:14:09 -04:00
|
|
|
plcons = &Stack[SP - 2];
|
|
|
|
pL = &Stack[SP - 1];
|
2019-08-09 07:02:02 -04:00
|
|
|
c = mk_cons();
|
|
|
|
PUSH(c); // save first cons
|
2009-12-03 15:12:06 -05:00
|
|
|
car_(c) = car_(*pL);
|
|
|
|
cdr_(c) = NIL;
|
|
|
|
*plcons = c;
|
|
|
|
*pL = cdr_(*pL);
|
2009-05-11 19:55:40 -04:00
|
|
|
while (iscons(*pL)) {
|
2009-12-03 15:12:06 -05:00
|
|
|
c = mk_cons();
|
|
|
|
car_(c) = car_(*pL);
|
|
|
|
cdr_(c) = NIL;
|
|
|
|
cdr_(*plcons) = c;
|
2009-05-11 19:55:40 -04:00
|
|
|
*plcons = c;
|
|
|
|
*pL = cdr_(*pL);
|
|
|
|
}
|
2009-12-03 15:12:06 -05:00
|
|
|
c = POP(); // first cons
|
2009-05-11 19:55:40 -04:00
|
|
|
POPN(2);
|
2009-12-03 15:12:06 -05:00
|
|
|
return c;
|
2009-05-11 19:55:40 -04:00
|
|
|
}
|
|
|
|
|
2013-02-11 04:48:21 -05:00
|
|
|
static value_t do_trycatch(void)
|
2009-04-14 20:12:01 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t v, thunk;
|
|
|
|
uint32_t saveSP;
|
|
|
|
|
|
|
|
saveSP = SP;
|
|
|
|
thunk = Stack[SP - 2];
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 2] = Stack[SP - 1];
|
|
|
|
Stack[SP - 1] = thunk;
|
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
FL_TRY { v = apply_cl(0); }
|
|
|
|
FL_CATCH
|
|
|
|
{
|
|
|
|
v = Stack[saveSP - 2];
|
|
|
|
PUSH(v);
|
|
|
|
PUSH(fl_lasterror);
|
|
|
|
v = apply_cl(1);
|
|
|
|
}
|
2009-04-14 20:12:01 -04:00
|
|
|
}
|
2009-04-16 10:21:16 -04:00
|
|
|
SP = saveSP;
|
2009-04-14 20:12:01 -04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2009-07-29 00:20:28 -04:00
|
|
|
/*
|
|
|
|
argument layout on stack is
|
|
|
|
|--required args--|--opt args--|--kw args--|--rest args...
|
2009-10-21 23:32:12 -04:00
|
|
|
*/
|
2019-08-09 07:02:02 -04:00
|
|
|
static uint32_t process_keys(value_t kwtable, uint32_t nreq, uint32_t nkw,
|
|
|
|
uint32_t nopt, uint32_t bp, uint32_t nargs,
|
|
|
|
int va)
|
2009-07-29 00:20:28 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t hv;
|
|
|
|
uintptr_t x;
|
|
|
|
uintptr_t idx;
|
|
|
|
uintptr_t n;
|
|
|
|
uint32_t ntot;
|
|
|
|
value_t v;
|
|
|
|
uint32_t extr;
|
|
|
|
value_t *args;
|
|
|
|
uint32_t nrestargs, i, a;
|
|
|
|
value_t s1, s2, s4, s5;
|
|
|
|
|
|
|
|
extr = nopt + nkw;
|
|
|
|
ntot = nreq + extr;
|
|
|
|
if (!(args = calloc(extr, sizeof(*args)))) {
|
|
|
|
lerror(MemoryError, "out of memory");
|
|
|
|
}
|
|
|
|
a = 0;
|
|
|
|
s1 = Stack[SP - 1];
|
|
|
|
s2 = Stack[SP - 2];
|
|
|
|
s4 = Stack[SP - 4];
|
|
|
|
s5 = Stack[SP - 5];
|
2009-07-29 00:20:28 -04:00
|
|
|
if (nargs < nreq)
|
|
|
|
lerror(ArgError, "apply: too few arguments");
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 0; i < extr; i++)
|
|
|
|
args[i] = UNBOUND;
|
|
|
|
for (i = nreq; i < nargs; i++) {
|
|
|
|
v = Stack[bp + i];
|
2019-08-09 12:30:15 -04:00
|
|
|
if (issymbol(v) && iskeyword((struct symbol *)ptr(v)))
|
2009-07-29 00:20:28 -04:00
|
|
|
break;
|
|
|
|
if (a >= nopt)
|
|
|
|
goto no_kw;
|
|
|
|
args[a++] = v;
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
if (i >= nargs)
|
|
|
|
goto no_kw;
|
2009-07-29 00:20:28 -04:00
|
|
|
// now process keywords
|
2019-08-18 18:14:09 -04:00
|
|
|
n = vector_size(kwtable) / 2;
|
2009-07-29 00:20:28 -04:00
|
|
|
do {
|
|
|
|
i++;
|
|
|
|
if (i >= nargs)
|
|
|
|
lerrorf(ArgError, "keyword %s requires an argument",
|
|
|
|
symbol_name(v));
|
2019-08-18 18:14:09 -04:00
|
|
|
hv = fixnum(((struct symbol *)ptr(v))->hash);
|
|
|
|
x = 2 * (labs(numval(hv)) % n);
|
2009-07-29 00:20:28 -04:00
|
|
|
if (vector_elt(kwtable, x) == v) {
|
2019-08-18 18:14:09 -04:00
|
|
|
idx = numval(vector_elt(kwtable, x + 1));
|
2009-07-29 00:20:28 -04:00
|
|
|
assert(idx < nkw);
|
2009-08-02 00:06:07 -04:00
|
|
|
idx += nopt;
|
2009-07-29 00:20:28 -04:00
|
|
|
if (args[idx] == UNBOUND) {
|
|
|
|
// if duplicate key, keep first value
|
2019-08-09 07:02:02 -04:00
|
|
|
args[idx] = Stack[bp + i];
|
2009-07-29 00:20:28 -04:00
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2009-07-29 00:20:28 -04:00
|
|
|
lerrorf(ArgError, "unsupported keyword %s", symbol_name(v));
|
|
|
|
}
|
|
|
|
i++;
|
2019-08-09 07:02:02 -04:00
|
|
|
if (i >= nargs)
|
|
|
|
break;
|
|
|
|
v = Stack[bp + i];
|
2019-08-09 12:30:15 -04:00
|
|
|
} while (issymbol(v) && iskeyword((struct symbol *)ptr(v)));
|
2019-08-09 07:02:02 -04:00
|
|
|
no_kw:
|
2009-07-29 00:20:28 -04:00
|
|
|
nrestargs = nargs - i;
|
|
|
|
if (!va && nrestargs > 0)
|
|
|
|
lerror(ArgError, "apply: too many arguments");
|
|
|
|
nargs = ntot + nrestargs;
|
|
|
|
if (nrestargs)
|
2019-08-09 07:02:02 -04:00
|
|
|
memmove(&Stack[bp + ntot], &Stack[bp + i],
|
|
|
|
nrestargs * sizeof(value_t));
|
|
|
|
memcpy(&Stack[bp + nreq], args, extr * sizeof(value_t));
|
2009-07-29 00:20:28 -04:00
|
|
|
SP = bp + nargs;
|
2019-08-09 07:02:02 -04:00
|
|
|
assert(SP < N_STACK - 5);
|
2009-07-29 00:20:28 -04:00
|
|
|
PUSH(s5);
|
|
|
|
PUSH(s4);
|
|
|
|
PUSH(nargs);
|
|
|
|
PUSH(s2);
|
|
|
|
PUSH(s1);
|
|
|
|
curr_frame = SP;
|
|
|
|
return nargs;
|
|
|
|
}
|
|
|
|
|
2019-08-09 15:04:55 -04:00
|
|
|
#if defined(__amd64__) || defined(_M_AMD64)
|
|
|
|
#define ARCH_X86_64
|
|
|
|
#define __CPU__ 686
|
|
|
|
#elif defined(_M_IX86) // msvs, intel, digital mars, watcom
|
|
|
|
#if !defined(__386__)
|
|
|
|
#error "unsupported target: 16-bit x86"
|
|
|
|
#endif
|
|
|
|
#define ARCH_X86
|
|
|
|
#define __CPU__ (_M_IX86 + 86)
|
|
|
|
#elif defined(__i686__) // gnu c
|
|
|
|
#define ARCH_X86
|
|
|
|
#define __CPU__ 686
|
|
|
|
#elif defined(__i586__) // gnu c
|
|
|
|
#define ARCH_X86
|
|
|
|
#define __CPU__ 586
|
|
|
|
#elif defined(__i486__) // gnu c
|
|
|
|
#define ARCH_X86
|
|
|
|
#define __CPU__ 486
|
|
|
|
#elif defined(__i386__) // gnu c
|
|
|
|
#define ARCH_X86
|
|
|
|
#define __CPU__ 386
|
|
|
|
#else
|
|
|
|
#error "unknown architecture"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ARCH_X86_64
|
|
|
|
#define LEGACY_REGS "=Q"
|
|
|
|
#else
|
|
|
|
#define LEGACY_REGS "=q"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(__INTEL_COMPILER) && (defined(ARCH_X86) || defined(ARCH_X86_64))
|
|
|
|
static uint16_t ByteSwap16(uint16_t x)
|
|
|
|
{
|
|
|
|
__asm("xchgb %b0,%h0" : LEGACY_REGS(x) : "0"(x));
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
#define bswap_16(x) ByteSwap16(x)
|
|
|
|
|
|
|
|
static uint32_t ByteSwap32(uint32_t x)
|
|
|
|
{
|
|
|
|
#if __CPU__ > 386
|
|
|
|
__asm("bswap %0"
|
|
|
|
: "=r"(x)
|
|
|
|
:
|
|
|
|
#else
|
|
|
|
__asm("xchgb %b0,%h0\n"
|
|
|
|
" rorl $16,%0\n"
|
|
|
|
" xchgb %b0,%h0"
|
|
|
|
: LEGACY_REGS(x)
|
|
|
|
:
|
|
|
|
#endif
|
|
|
|
"0"(x));
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define bswap_32(x) ByteSwap32(x)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define bswap_16(x) (((x)&0x00ff) << 8 | ((x)&0xff00) >> 8)
|
|
|
|
|
|
|
|
#ifdef __INTEL_COMPILER
|
|
|
|
#define bswap_32(x) _bswap(x)
|
|
|
|
#else
|
|
|
|
#define bswap_32(x) \
|
|
|
|
((((x)&0xff000000) >> 24) | (((x)&0x00ff0000) >> 8) | \
|
|
|
|
(((x)&0x0000ff00) << 8) | (((x)&0x000000ff) << 24))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-08-09 14:30:30 -04:00
|
|
|
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
2019-08-09 07:02:02 -04:00
|
|
|
#define GET_INT32(a) \
|
|
|
|
((int32_t)((((int32_t)a[0]) << 0) | (((int32_t)a[1]) << 8) | \
|
|
|
|
(((int32_t)a[2]) << 16) | (((int32_t)a[3]) << 24)))
|
|
|
|
#define GET_INT16(a) \
|
|
|
|
((int16_t)((((int16_t)a[0]) << 0) | (((int16_t)a[1]) << 8)))
|
|
|
|
#define PUT_INT32(a, i) (*(int32_t *)(a) = bswap_32((int32_t)(i)))
|
2019-08-09 14:30:30 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
2019-08-09 07:02:02 -04:00
|
|
|
#define GET_INT32(a) (*(int32_t *)a)
|
|
|
|
#define GET_INT16(a) (*(int16_t *)a)
|
|
|
|
#define PUT_INT32(a, i) (*(int32_t *)(a) = (int32_t)(i))
|
2009-06-06 17:15:54 -04:00
|
|
|
#endif
|
2019-08-09 14:30:30 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
#define SWAP_INT32(a) (*(int32_t *)(a) = bswap_32(*(int32_t *)(a)))
|
|
|
|
#define SWAP_INT16(a) (*(int16_t *)(a) = bswap_16(*(int16_t *)(a)))
|
2009-06-06 17:15:54 -04:00
|
|
|
|
2009-06-28 15:47:11 -04:00
|
|
|
#ifdef USE_COMPUTED_GOTO
|
|
|
|
#define OP(x) L_##x:
|
|
|
|
#define NEXT_OP goto *vm_labels[*ip++]
|
|
|
|
#else
|
|
|
|
#define OP(x) case x:
|
|
|
|
#define NEXT_OP goto next_op
|
|
|
|
#endif
|
|
|
|
|
2009-04-09 00:04:27 -04:00
|
|
|
/*
|
2009-07-20 00:57:17 -04:00
|
|
|
stack on entry: <func> <nargs args...>
|
2009-04-09 00:04:27 -04:00
|
|
|
caller's responsibility:
|
|
|
|
- put the stack in this state
|
|
|
|
- provide arg count
|
|
|
|
- respect tail position
|
2009-04-16 10:21:16 -04:00
|
|
|
- restore SP
|
2009-04-09 00:04:27 -04:00
|
|
|
|
|
|
|
callee's responsibility:
|
|
|
|
- check arg counts
|
|
|
|
- allocate vararg array
|
|
|
|
- push closed env, set up new environment
|
|
|
|
*/
|
|
|
|
static value_t apply_cl(uint32_t nargs)
|
|
|
|
{
|
2009-06-28 15:47:11 -04:00
|
|
|
VM_LABELS;
|
2009-07-20 23:42:15 -04:00
|
|
|
VM_APPLY_LABELS;
|
2009-07-03 14:43:15 -04:00
|
|
|
uint32_t top_frame = curr_frame;
|
2009-05-12 21:13:40 -04:00
|
|
|
// frame variables
|
2019-08-09 07:02:02 -04:00
|
|
|
uint32_t n = 0, captured;
|
2009-06-28 15:47:11 -04:00
|
|
|
uint32_t bp;
|
2009-06-14 22:25:21 -04:00
|
|
|
const uint8_t *ip;
|
2009-05-12 21:13:40 -04:00
|
|
|
fixnum_t s, hi;
|
|
|
|
|
|
|
|
// temporary variables (not necessary to preserve across calls)
|
2009-07-20 23:42:15 -04:00
|
|
|
#ifndef USE_COMPUTED_GOTO
|
2009-06-27 19:07:22 -04:00
|
|
|
uint32_t op;
|
2009-07-20 23:42:15 -04:00
|
|
|
#endif
|
2009-06-14 22:25:21 -04:00
|
|
|
uint32_t i;
|
2019-08-09 12:30:15 -04:00
|
|
|
struct symbol *sym;
|
2019-08-09 12:28:14 -04:00
|
|
|
static struct cons *c;
|
2009-06-09 14:46:02 -04:00
|
|
|
static value_t *pv;
|
|
|
|
static int64_t accum;
|
|
|
|
static value_t func, v, e;
|
2009-04-09 00:04:27 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
apply_cl_top:
|
2009-04-16 10:21:16 -04:00
|
|
|
captured = 0;
|
2019-08-09 07:02:02 -04:00
|
|
|
func = Stack[SP - nargs - 1];
|
2019-08-09 12:36:20 -04:00
|
|
|
ip = cv_data((struct cvalue *)ptr(fn_bcode(func)));
|
2019-08-09 14:07:16 -04:00
|
|
|
assert(!ismanaged((uintptr_t)ip));
|
2019-08-09 07:02:02 -04:00
|
|
|
while (SP + GET_INT32(ip) > N_STACK) {
|
2009-07-03 14:43:15 -04:00
|
|
|
grow_stack();
|
|
|
|
}
|
2009-06-27 19:07:22 -04:00
|
|
|
ip += 4;
|
2009-04-09 00:04:27 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
bp = SP - nargs;
|
2009-04-28 00:10:18 -04:00
|
|
|
PUSH(fn_env(func));
|
2009-07-03 14:43:15 -04:00
|
|
|
PUSH(curr_frame);
|
|
|
|
PUSH(nargs);
|
2019-08-09 07:02:02 -04:00
|
|
|
SP++; // PUSH(0); //ip
|
|
|
|
PUSH(0); // captured?
|
2009-07-03 14:43:15 -04:00
|
|
|
curr_frame = SP;
|
2009-04-09 00:04:27 -04:00
|
|
|
|
2009-06-14 22:25:21 -04:00
|
|
|
{
|
2009-06-28 15:47:11 -04:00
|
|
|
#ifdef USE_COMPUTED_GOTO
|
2019-08-09 07:02:02 -04:00
|
|
|
{
|
|
|
|
NEXT_OP;
|
2009-06-28 15:47:11 -04:00
|
|
|
#else
|
2009-04-26 23:21:53 -04:00
|
|
|
next_op:
|
2009-06-14 22:25:21 -04:00
|
|
|
op = *ip++;
|
2009-04-14 20:12:01 -04:00
|
|
|
dispatch:
|
2009-04-09 00:04:27 -04:00
|
|
|
switch (op) {
|
2009-06-28 15:47:11 -04:00
|
|
|
#endif
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_ARGC)
|
2009-06-14 22:25:21 -04:00
|
|
|
n = *ip++;
|
2009-07-20 23:42:15 -04:00
|
|
|
do_argc:
|
2009-04-28 00:10:18 -04:00
|
|
|
if (nargs != n) {
|
|
|
|
if (nargs > n)
|
|
|
|
lerror(ArgError, "apply: too many arguments");
|
|
|
|
else
|
|
|
|
lerror(ArgError, "apply: too few arguments");
|
2009-04-14 20:12:01 -04:00
|
|
|
}
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_VARGC)
|
2009-06-14 22:25:21 -04:00
|
|
|
i = *ip++;
|
2009-07-20 00:57:17 -04:00
|
|
|
do_vargc:
|
2009-04-14 20:12:01 -04:00
|
|
|
s = (fixnum_t)nargs - (fixnum_t)i;
|
|
|
|
if (s > 0) {
|
2019-08-09 07:02:02 -04:00
|
|
|
v = list(&Stack[bp + i], s);
|
|
|
|
Stack[bp + i] = v;
|
2009-07-03 14:43:15 -04:00
|
|
|
if (s > 1) {
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[bp + i + 1] = Stack[bp + nargs + 0];
|
|
|
|
Stack[bp + i + 2] = Stack[bp + nargs + 1];
|
|
|
|
Stack[bp + i + 3] = i + 1;
|
|
|
|
// Stack[bp+i+4] = 0;
|
|
|
|
Stack[bp + i + 5] = 0;
|
|
|
|
SP = bp + i + 6;
|
2009-07-03 14:43:15 -04:00
|
|
|
curr_frame = SP;
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (s < 0) {
|
2009-04-28 00:10:18 -04:00
|
|
|
lerror(ArgError, "apply: too few arguments");
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2009-07-05 23:56:48 -04:00
|
|
|
PUSH(0);
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 3] = i + 1;
|
|
|
|
Stack[SP - 4] = Stack[SP - 5];
|
|
|
|
Stack[SP - 5] = Stack[SP - 6];
|
|
|
|
Stack[SP - 6] = NIL;
|
2009-07-03 14:43:15 -04:00
|
|
|
curr_frame = SP;
|
2009-04-14 20:12:01 -04:00
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
nargs = i + 1;
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_LARGC)
|
|
|
|
n = GET_INT32(ip);
|
|
|
|
ip += 4;
|
2009-07-20 23:42:15 -04:00
|
|
|
goto do_argc;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_LVARGC)
|
|
|
|
i = GET_INT32(ip);
|
|
|
|
ip += 4;
|
2009-07-20 00:57:17 -04:00
|
|
|
goto do_vargc;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_BRBOUND)
|
|
|
|
i = GET_INT32(ip);
|
|
|
|
ip += 4;
|
2009-07-28 00:16:20 -04:00
|
|
|
if (captured)
|
|
|
|
v = vector_elt(Stack[bp], i);
|
|
|
|
else
|
2019-08-09 07:02:02 -04:00
|
|
|
v = Stack[bp + i];
|
|
|
|
if (v != UNBOUND)
|
|
|
|
PUSH(FL_T);
|
|
|
|
else
|
|
|
|
PUSH(FL_F);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_DUP) SP++;
|
|
|
|
Stack[SP - 1] = Stack[SP - 2];
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_POP) POPN(1);
|
2009-07-28 00:16:20 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_TCALL)
|
2009-06-14 22:25:21 -04:00
|
|
|
n = *ip++; // nargs
|
2009-05-11 19:55:40 -04:00
|
|
|
do_tcall:
|
2019-08-09 07:02:02 -04:00
|
|
|
func = Stack[SP - n - 1];
|
2009-07-20 23:42:15 -04:00
|
|
|
if (tag(func) == TAG_FUNCTION) {
|
2019-08-09 07:02:02 -04:00
|
|
|
if (func > (N_BUILTINS << 3)) {
|
|
|
|
curr_frame = Stack[curr_frame - 4];
|
|
|
|
for (s = -1; s < (fixnum_t)n; s++)
|
|
|
|
Stack[bp + s] = Stack[SP - n + s];
|
|
|
|
SP = bp + n;
|
2009-07-20 23:42:15 -04:00
|
|
|
nargs = n;
|
|
|
|
goto apply_cl_top;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2009-07-20 23:42:15 -04:00
|
|
|
i = uintval(func);
|
|
|
|
if (i <= OP_ASET) {
|
|
|
|
s = builtin_arg_counts[i];
|
|
|
|
if (s >= 0)
|
|
|
|
argcount(builtin_names[i], n, s);
|
|
|
|
else if (s != ANYARGS && (signed)n < -s)
|
|
|
|
argcount(builtin_names[i], n, -s);
|
|
|
|
// remove function arg
|
2019-08-09 07:02:02 -04:00
|
|
|
for (s = SP - n - 1; s < (int)SP - 1; s++)
|
|
|
|
Stack[s] = Stack[s + 1];
|
2009-07-20 23:42:15 -04:00
|
|
|
SP--;
|
|
|
|
#ifdef USE_COMPUTED_GOTO
|
|
|
|
if (i == OP_APPLY)
|
|
|
|
goto apply_tapply;
|
|
|
|
goto *vm_apply_labels[i];
|
|
|
|
#else
|
|
|
|
switch (i) {
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_LIST:
|
|
|
|
goto apply_list;
|
|
|
|
case OP_VECTOR:
|
|
|
|
goto apply_vector;
|
|
|
|
case OP_APPLY:
|
|
|
|
goto apply_tapply;
|
|
|
|
case OP_ADD:
|
|
|
|
goto apply_add;
|
|
|
|
case OP_SUB:
|
|
|
|
goto apply_sub;
|
|
|
|
case OP_MUL:
|
|
|
|
goto apply_mul;
|
|
|
|
case OP_DIV:
|
|
|
|
goto apply_div;
|
2009-07-20 23:42:15 -04:00
|
|
|
default:
|
|
|
|
op = (uint8_t)i;
|
|
|
|
goto dispatch;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (iscbuiltin(func)) {
|
2009-07-20 23:42:15 -04:00
|
|
|
s = SP;
|
2019-08-09 07:02:02 -04:00
|
|
|
v = ((builtin_t)(((void **)ptr(func))[3]))(&Stack[SP - n], n);
|
|
|
|
SP = s - n;
|
|
|
|
Stack[SP - 1] = v;
|
2009-07-20 23:42:15 -04:00
|
|
|
NEXT_OP;
|
|
|
|
}
|
|
|
|
type_error("apply", "function", func);
|
2019-08-09 07:02:02 -04:00
|
|
|
// WARNING: repeated code ahead
|
|
|
|
OP(OP_CALL)
|
2009-06-14 22:25:21 -04:00
|
|
|
n = *ip++; // nargs
|
2009-04-09 00:04:27 -04:00
|
|
|
do_call:
|
2019-08-09 07:02:02 -04:00
|
|
|
func = Stack[SP - n - 1];
|
2009-04-28 00:10:18 -04:00
|
|
|
if (tag(func) == TAG_FUNCTION) {
|
2019-08-09 07:02:02 -04:00
|
|
|
if (func > (N_BUILTINS << 3)) {
|
2019-08-09 14:07:16 -04:00
|
|
|
Stack[curr_frame - 2] = (uintptr_t)ip;
|
2009-07-03 14:43:15 -04:00
|
|
|
nargs = n;
|
|
|
|
goto apply_cl_top;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2009-06-10 20:34:50 -04:00
|
|
|
i = uintval(func);
|
2009-07-20 23:42:15 -04:00
|
|
|
if (i <= OP_ASET) {
|
|
|
|
s = builtin_arg_counts[i];
|
|
|
|
if (s >= 0)
|
|
|
|
argcount(builtin_names[i], n, s);
|
|
|
|
else if (s != ANYARGS && (signed)n < -s)
|
|
|
|
argcount(builtin_names[i], n, -s);
|
|
|
|
// remove function arg
|
2019-08-09 07:02:02 -04:00
|
|
|
for (s = SP - n - 1; s < (int)SP - 1; s++)
|
|
|
|
Stack[s] = Stack[s + 1];
|
2009-07-20 23:42:15 -04:00
|
|
|
SP--;
|
|
|
|
#ifdef USE_COMPUTED_GOTO
|
|
|
|
goto *vm_apply_labels[i];
|
|
|
|
#else
|
|
|
|
switch (i) {
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_LIST:
|
|
|
|
goto apply_list;
|
|
|
|
case OP_VECTOR:
|
|
|
|
goto apply_vector;
|
|
|
|
case OP_APPLY:
|
|
|
|
goto apply_apply;
|
|
|
|
case OP_ADD:
|
|
|
|
goto apply_add;
|
|
|
|
case OP_SUB:
|
|
|
|
goto apply_sub;
|
|
|
|
case OP_MUL:
|
|
|
|
goto apply_mul;
|
|
|
|
case OP_DIV:
|
|
|
|
goto apply_div;
|
2009-07-20 23:42:15 -04:00
|
|
|
default:
|
|
|
|
op = (uint8_t)i;
|
|
|
|
goto dispatch;
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-16 23:40:52 -04:00
|
|
|
}
|
2009-04-14 20:12:01 -04:00
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (iscbuiltin(func)) {
|
2009-07-20 23:42:15 -04:00
|
|
|
s = SP;
|
2019-08-09 07:02:02 -04:00
|
|
|
v = ((builtin_t)(((void **)ptr(func))[3]))(&Stack[SP - n], n);
|
|
|
|
SP = s - n;
|
|
|
|
Stack[SP - 1] = v;
|
2009-07-20 23:42:15 -04:00
|
|
|
NEXT_OP;
|
2009-04-28 00:10:18 -04:00
|
|
|
}
|
2009-07-20 23:42:15 -04:00
|
|
|
type_error("apply", "function", func);
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_TCALLL) n = GET_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
goto do_tcall;
|
|
|
|
OP(OP_CALLL) n = GET_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
goto do_call;
|
2019-08-09 14:07:16 -04:00
|
|
|
OP(OP_JMP) ip += (intptr_t)GET_INT16(ip);
|
2019-08-09 07:02:02 -04:00
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_BRF)
|
2009-04-09 00:04:27 -04:00
|
|
|
v = POP();
|
2019-08-09 07:02:02 -04:00
|
|
|
if (v == FL_F)
|
2019-08-09 14:07:16 -04:00
|
|
|
ip += (intptr_t)GET_INT16(ip);
|
2019-08-09 07:02:02 -04:00
|
|
|
else
|
|
|
|
ip += 2;
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_BRT)
|
2009-04-09 00:04:27 -04:00
|
|
|
v = POP();
|
2019-08-09 07:02:02 -04:00
|
|
|
if (v != FL_F)
|
2019-08-09 14:07:16 -04:00
|
|
|
ip += (intptr_t)GET_INT16(ip);
|
2019-08-09 07:02:02 -04:00
|
|
|
else
|
|
|
|
ip += 2;
|
|
|
|
NEXT_OP;
|
2019-08-09 14:07:16 -04:00
|
|
|
OP(OP_JMPL) ip += (intptr_t)GET_INT32(ip);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_BRFL)
|
2009-04-09 00:04:27 -04:00
|
|
|
v = POP();
|
2019-08-09 07:02:02 -04:00
|
|
|
if (v == FL_F)
|
2019-08-09 14:07:16 -04:00
|
|
|
ip += (intptr_t)GET_INT32(ip);
|
2019-08-09 07:02:02 -04:00
|
|
|
else
|
|
|
|
ip += 4;
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_BRTL)
|
2009-04-09 00:04:27 -04:00
|
|
|
v = POP();
|
2019-08-09 07:02:02 -04:00
|
|
|
if (v != FL_F)
|
2019-08-09 14:07:16 -04:00
|
|
|
ip += (intptr_t)GET_INT32(ip);
|
2019-08-09 07:02:02 -04:00
|
|
|
else
|
|
|
|
ip += 4;
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_BRNE)
|
|
|
|
if (Stack[SP - 2] != Stack[SP - 1])
|
2019-08-09 14:07:16 -04:00
|
|
|
ip += (intptr_t)GET_INT16(ip);
|
2019-08-09 07:02:02 -04:00
|
|
|
else
|
|
|
|
ip += 2;
|
2009-07-24 00:20:09 -04:00
|
|
|
POPN(2);
|
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_BRNEL)
|
|
|
|
if (Stack[SP - 2] != Stack[SP - 1])
|
2019-08-09 14:07:16 -04:00
|
|
|
ip += (intptr_t)GET_INT32(ip);
|
2019-08-09 07:02:02 -04:00
|
|
|
else
|
|
|
|
ip += 4;
|
2009-07-24 00:20:09 -04:00
|
|
|
POPN(2);
|
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_BRNN)
|
2009-07-24 00:20:09 -04:00
|
|
|
v = POP();
|
2019-08-09 07:02:02 -04:00
|
|
|
if (v != NIL)
|
2019-08-09 14:07:16 -04:00
|
|
|
ip += (intptr_t)GET_INT16(ip);
|
2019-08-09 07:02:02 -04:00
|
|
|
else
|
|
|
|
ip += 2;
|
2009-07-24 00:20:09 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_BRNNL)
|
2009-07-24 00:20:09 -04:00
|
|
|
v = POP();
|
2019-08-09 07:02:02 -04:00
|
|
|
if (v != NIL)
|
2019-08-09 14:07:16 -04:00
|
|
|
ip += (intptr_t)GET_INT32(ip);
|
2019-08-09 07:02:02 -04:00
|
|
|
else
|
|
|
|
ip += 4;
|
2009-07-24 00:20:09 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_BRN)
|
2009-07-24 00:20:09 -04:00
|
|
|
v = POP();
|
2019-08-09 07:02:02 -04:00
|
|
|
if (v == NIL)
|
2019-08-09 14:07:16 -04:00
|
|
|
ip += (intptr_t)GET_INT16(ip);
|
2019-08-09 07:02:02 -04:00
|
|
|
else
|
|
|
|
ip += 2;
|
2009-07-24 00:20:09 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_BRNL)
|
2009-07-24 00:20:09 -04:00
|
|
|
v = POP();
|
2019-08-09 07:02:02 -04:00
|
|
|
if (v == NIL)
|
2019-08-09 14:07:16 -04:00
|
|
|
ip += (intptr_t)GET_INT32(ip);
|
2019-08-09 07:02:02 -04:00
|
|
|
else
|
|
|
|
ip += 4;
|
2009-07-24 00:20:09 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_RET)
|
2009-07-03 14:43:15 -04:00
|
|
|
v = POP();
|
|
|
|
SP = curr_frame;
|
2019-08-09 07:02:02 -04:00
|
|
|
curr_frame = Stack[SP - 4];
|
|
|
|
if (curr_frame == top_frame)
|
|
|
|
return v;
|
|
|
|
SP -= (5 + nargs);
|
|
|
|
captured = Stack[curr_frame - 1];
|
|
|
|
ip = (uint8_t *)Stack[curr_frame - 2];
|
|
|
|
nargs = Stack[curr_frame - 3];
|
|
|
|
bp = curr_frame - 5 - nargs;
|
|
|
|
Stack[SP - 1] = v;
|
|
|
|
NEXT_OP;
|
|
|
|
|
|
|
|
OP(OP_EQ)
|
|
|
|
Stack[SP - 2] = ((Stack[SP - 2] == Stack[SP - 1]) ? FL_T : FL_F);
|
|
|
|
POPN(1);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_EQV)
|
|
|
|
if (Stack[SP - 2] == Stack[SP - 1]) {
|
2009-04-09 00:04:27 -04:00
|
|
|
v = FL_T;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (!leafp(Stack[SP - 2]) || !leafp(Stack[SP - 1])) {
|
2009-04-09 00:04:27 -04:00
|
|
|
v = FL_F;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
|
|
|
v = (compare_(Stack[SP - 2], Stack[SP - 1], 1) == 0 ? FL_T
|
|
|
|
: FL_F);
|
2009-04-09 00:04:27 -04:00
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 2] = v;
|
|
|
|
POPN(1);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_EQUAL)
|
|
|
|
if (Stack[SP - 2] == Stack[SP - 1]) {
|
2009-04-09 00:04:27 -04:00
|
|
|
v = FL_T;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
|
|
|
v = (compare_(Stack[SP - 2], Stack[SP - 1], 1) == 0 ? FL_T
|
|
|
|
: FL_F);
|
2009-04-09 00:04:27 -04:00
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 2] = v;
|
|
|
|
POPN(1);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_PAIRP)
|
|
|
|
Stack[SP - 1] = (iscons(Stack[SP - 1]) ? FL_T : FL_F);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_ATOMP)
|
|
|
|
Stack[SP - 1] = (iscons(Stack[SP - 1]) ? FL_F : FL_T);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_NOT)
|
|
|
|
Stack[SP - 1] = ((Stack[SP - 1] == FL_F) ? FL_T : FL_F);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_NULLP)
|
|
|
|
Stack[SP - 1] = ((Stack[SP - 1] == NIL) ? FL_T : FL_F);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_BOOLEANP)
|
|
|
|
v = Stack[SP - 1];
|
|
|
|
Stack[SP - 1] = ((v == FL_T || v == FL_F) ? FL_T : FL_F);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_SYMBOLP)
|
|
|
|
Stack[SP - 1] = (issymbol(Stack[SP - 1]) ? FL_T : FL_F);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_NUMBERP)
|
|
|
|
v = Stack[SP - 1];
|
|
|
|
Stack[SP - 1] = (fl_isnumber(v) ? FL_T : FL_F);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_FIXNUMP)
|
|
|
|
Stack[SP - 1] = (isfixnum(Stack[SP - 1]) ? FL_T : FL_F);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_BOUNDP)
|
|
|
|
sym = tosymbol(Stack[SP - 1], "bound?");
|
|
|
|
Stack[SP - 1] = ((sym->binding == UNBOUND) ? FL_F : FL_T);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_BUILTINP)
|
|
|
|
v = Stack[SP - 1];
|
|
|
|
Stack[SP - 1] = (isbuiltin(v) || iscbuiltin(v)) ? FL_T : FL_F;
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_FUNCTIONP)
|
|
|
|
v = Stack[SP - 1];
|
|
|
|
Stack[SP - 1] =
|
|
|
|
((tag(v) == TAG_FUNCTION &&
|
|
|
|
(uintval(v) <= OP_ASET || v > (N_BUILTINS << 3))) ||
|
|
|
|
iscbuiltin(v))
|
|
|
|
? FL_T
|
|
|
|
: FL_F;
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_VECTORP)
|
|
|
|
Stack[SP - 1] = (isvector(Stack[SP - 1]) ? FL_T : FL_F);
|
|
|
|
NEXT_OP;
|
|
|
|
|
|
|
|
OP(OP_CONS)
|
2009-04-09 00:04:27 -04:00
|
|
|
if (curheap > lim)
|
|
|
|
gc(0);
|
2019-08-09 12:28:14 -04:00
|
|
|
c = (struct cons *)curheap;
|
|
|
|
curheap += sizeof(struct cons);
|
2019-08-09 07:02:02 -04:00
|
|
|
c->car = Stack[SP - 2];
|
|
|
|
c->cdr = Stack[SP - 1];
|
|
|
|
Stack[SP - 2] = tagptr(c, TAG_CONS);
|
|
|
|
POPN(1);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_CAR)
|
|
|
|
v = Stack[SP - 1];
|
|
|
|
if (!iscons(v))
|
|
|
|
type_error("car", "cons", v);
|
|
|
|
Stack[SP - 1] = car_(v);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_CDR)
|
|
|
|
v = Stack[SP - 1];
|
|
|
|
if (!iscons(v))
|
|
|
|
type_error("cdr", "cons", v);
|
|
|
|
Stack[SP - 1] = cdr_(v);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_CADR)
|
|
|
|
v = Stack[SP - 1];
|
|
|
|
if (!iscons(v))
|
|
|
|
type_error("cdr", "cons", v);
|
2009-07-24 00:20:09 -04:00
|
|
|
v = cdr_(v);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (!iscons(v))
|
|
|
|
type_error("car", "cons", v);
|
|
|
|
Stack[SP - 1] = car_(v);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_SETCAR)
|
|
|
|
car(Stack[SP - 2]) = Stack[SP - 1];
|
|
|
|
POPN(1);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_SETCDR)
|
|
|
|
cdr(Stack[SP - 2]) = Stack[SP - 1];
|
|
|
|
POPN(1);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_LIST)
|
2009-06-14 22:25:21 -04:00
|
|
|
n = *ip++;
|
2009-04-16 23:40:52 -04:00
|
|
|
apply_list:
|
2009-04-22 19:00:13 -04:00
|
|
|
if (n > 0) {
|
2019-08-09 07:02:02 -04:00
|
|
|
v = list(&Stack[SP - n], n);
|
2009-04-22 19:00:13 -04:00
|
|
|
POPN(n);
|
|
|
|
PUSH(v);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2009-04-22 19:00:13 -04:00
|
|
|
PUSH(NIL);
|
|
|
|
}
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2009-04-09 00:04:27 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_TAPPLY)
|
2009-07-20 23:42:15 -04:00
|
|
|
n = *ip++;
|
|
|
|
apply_tapply:
|
2019-08-09 07:02:02 -04:00
|
|
|
v = POP(); // arglist
|
|
|
|
n = SP - (n - 2); // n-2 == # leading arguments not in the list
|
2009-07-20 23:42:15 -04:00
|
|
|
while (iscons(v)) {
|
|
|
|
if (SP >= N_STACK)
|
|
|
|
grow_stack();
|
|
|
|
PUSH(car_(v));
|
|
|
|
v = cdr_(v);
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
n = SP - n;
|
2009-07-20 23:42:15 -04:00
|
|
|
goto do_tcall;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_APPLY)
|
2009-06-14 22:25:21 -04:00
|
|
|
n = *ip++;
|
2009-05-11 19:55:40 -04:00
|
|
|
apply_apply:
|
2019-08-09 07:02:02 -04:00
|
|
|
v = POP(); // arglist
|
|
|
|
n = SP - (n - 2); // n-2 == # leading arguments not in the list
|
2009-04-09 00:04:27 -04:00
|
|
|
while (iscons(v)) {
|
2009-07-20 00:57:17 -04:00
|
|
|
if (SP >= N_STACK)
|
|
|
|
grow_stack();
|
2009-04-09 00:04:27 -04:00
|
|
|
PUSH(car_(v));
|
|
|
|
v = cdr_(v);
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
n = SP - n;
|
2009-07-20 23:42:15 -04:00
|
|
|
goto do_call;
|
2009-04-09 00:04:27 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_ADD)
|
2009-06-14 22:25:21 -04:00
|
|
|
n = *ip++;
|
2009-04-16 23:40:52 -04:00
|
|
|
apply_add:
|
|
|
|
s = 0;
|
2019-08-09 07:02:02 -04:00
|
|
|
i = SP - n;
|
2009-04-14 20:12:01 -04:00
|
|
|
for (; i < SP; i++) {
|
2009-04-28 00:10:18 -04:00
|
|
|
if (isfixnum(Stack[i])) {
|
2009-04-09 00:04:27 -04:00
|
|
|
s += numval(Stack[i]);
|
2009-04-28 00:10:18 -04:00
|
|
|
if (!fits_fixnum(s)) {
|
2009-04-09 00:04:27 -04:00
|
|
|
i++;
|
|
|
|
goto add_ovf;
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2009-04-09 00:04:27 -04:00
|
|
|
add_ovf:
|
2019-08-09 07:02:02 -04:00
|
|
|
v = fl_add_any(&Stack[i], SP - i, s);
|
2009-04-09 00:04:27 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
if (i == SP)
|
2009-04-09 00:04:27 -04:00
|
|
|
v = fixnum(s);
|
|
|
|
POPN(n);
|
|
|
|
PUSH(v);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_ADD2)
|
|
|
|
if (bothfixnums(Stack[SP - 1], Stack[SP - 2])) {
|
|
|
|
s = numval(Stack[SP - 1]) + numval(Stack[SP - 2]);
|
2009-04-26 23:21:53 -04:00
|
|
|
if (fits_fixnum(s))
|
|
|
|
v = fixnum(s);
|
2009-04-26 18:19:32 -04:00
|
|
|
else
|
2009-04-26 23:21:53 -04:00
|
|
|
v = mk_long(s);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
|
|
|
v = fl_add_any(&Stack[SP - 2], 2, 0);
|
2009-04-26 18:19:32 -04:00
|
|
|
}
|
|
|
|
POPN(1);
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 1] = v;
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_SUB)
|
2009-06-14 22:25:21 -04:00
|
|
|
n = *ip++;
|
2009-04-16 23:40:52 -04:00
|
|
|
apply_sub:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (n == 2)
|
|
|
|
goto do_sub2;
|
|
|
|
if (n == 1)
|
|
|
|
goto do_neg;
|
|
|
|
i = SP - n;
|
2009-04-26 18:19:32 -04:00
|
|
|
// we need to pass the full arglist on to fl_add_any
|
|
|
|
// so it can handle rest args properly
|
|
|
|
PUSH(Stack[i]);
|
|
|
|
Stack[i] = fixnum(0);
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[i + 1] = fl_neg(fl_add_any(&Stack[i], n, 0));
|
2009-04-26 18:19:32 -04:00
|
|
|
Stack[i] = POP();
|
|
|
|
v = fl_add_any(&Stack[i], 2, 0);
|
|
|
|
POPN(n);
|
|
|
|
PUSH(v);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_NEG)
|
2009-04-26 18:19:32 -04:00
|
|
|
do_neg:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (isfixnum(Stack[SP - 1])) {
|
|
|
|
s = fixnum(-numval(Stack[SP - 1]));
|
2019-08-09 12:53:35 -04:00
|
|
|
if (__unlikely(Stack[SP - 1] == (ufixnum_t)s))
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 1] =
|
|
|
|
mk_long(-numval(Stack[SP - 1])); // negate overflows
|
2016-11-15 17:49:18 -05:00
|
|
|
else
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 1] = s;
|
|
|
|
} else
|
|
|
|
Stack[SP - 1] = fl_neg(Stack[SP - 1]);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_SUB2)
|
2009-04-26 18:19:32 -04:00
|
|
|
do_sub2:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (bothfixnums(Stack[SP - 2], Stack[SP - 1])) {
|
|
|
|
s = numval(Stack[SP - 2]) - numval(Stack[SP - 1]);
|
2009-04-28 00:10:18 -04:00
|
|
|
if (fits_fixnum(s))
|
2009-04-26 23:21:53 -04:00
|
|
|
v = fixnum(s);
|
|
|
|
else
|
|
|
|
v = mk_long(s);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
|
|
|
Stack[SP - 1] = fl_neg(Stack[SP - 1]);
|
|
|
|
v = fl_add_any(&Stack[SP - 2], 2, 0);
|
2009-04-09 00:04:27 -04:00
|
|
|
}
|
2009-04-26 18:19:32 -04:00
|
|
|
POPN(1);
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 1] = v;
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_MUL)
|
2009-06-14 22:25:21 -04:00
|
|
|
n = *ip++;
|
2009-04-16 23:40:52 -04:00
|
|
|
apply_mul:
|
|
|
|
accum = 1;
|
2019-08-09 07:02:02 -04:00
|
|
|
i = SP - n;
|
2009-04-14 20:12:01 -04:00
|
|
|
for (; i < SP; i++) {
|
2009-04-28 00:10:18 -04:00
|
|
|
if (isfixnum(Stack[i])) {
|
2009-04-09 00:04:27 -04:00
|
|
|
accum *= numval(Stack[i]);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
|
|
|
v = fl_mul_any(&Stack[i], SP - i, accum);
|
2009-04-09 00:04:27 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == SP) {
|
2009-04-28 00:10:18 -04:00
|
|
|
if (fits_fixnum(accum))
|
2009-04-09 00:04:27 -04:00
|
|
|
v = fixnum(accum);
|
|
|
|
else
|
|
|
|
v = return_from_int64(accum);
|
|
|
|
}
|
|
|
|
POPN(n);
|
|
|
|
PUSH(v);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_DIV)
|
2009-06-14 22:25:21 -04:00
|
|
|
n = *ip++;
|
2009-04-16 23:40:52 -04:00
|
|
|
apply_div:
|
2019-08-09 07:02:02 -04:00
|
|
|
i = SP - n;
|
2009-04-09 00:04:27 -04:00
|
|
|
if (n == 1) {
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 1] = fl_div2(fixnum(1), Stack[i]);
|
|
|
|
} else {
|
2009-04-09 00:04:27 -04:00
|
|
|
if (n > 2) {
|
|
|
|
PUSH(Stack[i]);
|
|
|
|
Stack[i] = fixnum(1);
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[i + 1] = fl_mul_any(&Stack[i], n, 1);
|
2009-04-09 00:04:27 -04:00
|
|
|
Stack[i] = POP();
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
v = fl_div2(Stack[i], Stack[i + 1]);
|
2009-04-09 00:04:27 -04:00
|
|
|
POPN(n);
|
|
|
|
PUSH(v);
|
|
|
|
}
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_IDIV)
|
|
|
|
v = Stack[SP - 2];
|
|
|
|
e = Stack[SP - 1];
|
2009-05-13 22:53:04 -04:00
|
|
|
if (bothfixnums(v, e)) {
|
2019-08-09 07:02:02 -04:00
|
|
|
if (e == 0)
|
|
|
|
DivideByZeroError();
|
2009-05-13 21:30:25 -04:00
|
|
|
v = fixnum(numval(v) / numval(e));
|
2019-08-09 07:02:02 -04:00
|
|
|
} else
|
2009-05-13 21:30:25 -04:00
|
|
|
v = fl_idiv2(v, e);
|
|
|
|
POPN(1);
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 1] = v;
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_NUMEQ)
|
|
|
|
v = Stack[SP - 2];
|
|
|
|
e = Stack[SP - 1];
|
2009-05-13 21:30:25 -04:00
|
|
|
if (bothfixnums(v, e))
|
2009-04-15 19:54:43 -04:00
|
|
|
v = (v == e) ? FL_T : FL_F;
|
2009-05-13 21:30:25 -04:00
|
|
|
else
|
2019-08-09 07:02:02 -04:00
|
|
|
v = (!numeric_compare(v, e, 1, 0, "=")) ? FL_T : FL_F;
|
2009-04-15 19:54:43 -04:00
|
|
|
POPN(1);
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 1] = v;
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_LT)
|
|
|
|
if (bothfixnums(Stack[SP - 2], Stack[SP - 1])) {
|
|
|
|
v =
|
|
|
|
(numval(Stack[SP - 2]) < numval(Stack[SP - 1])) ? FL_T : FL_F;
|
|
|
|
} else {
|
|
|
|
v = (numval(fl_compare(Stack[SP - 2], Stack[SP - 1])) < 0)
|
|
|
|
? FL_T
|
|
|
|
: FL_F;
|
2009-04-09 00:04:27 -04:00
|
|
|
}
|
2009-04-14 20:12:01 -04:00
|
|
|
POPN(1);
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 1] = v;
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_COMPARE)
|
|
|
|
Stack[SP - 2] = compare_(Stack[SP - 2], Stack[SP - 1], 0);
|
2009-04-14 20:12:01 -04:00
|
|
|
POPN(1);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2009-04-09 00:04:27 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_VECTOR)
|
2009-06-14 22:25:21 -04:00
|
|
|
n = *ip++;
|
2009-04-16 23:40:52 -04:00
|
|
|
apply_vector:
|
2009-07-20 00:57:17 -04:00
|
|
|
v = alloc_vector(n, 0);
|
2009-04-20 20:56:05 -04:00
|
|
|
if (n) {
|
2019-08-09 07:02:02 -04:00
|
|
|
memcpy(&vector_elt(v, 0), &Stack[SP - n],
|
|
|
|
n * sizeof(value_t));
|
2009-07-20 00:57:17 -04:00
|
|
|
POPN(n);
|
2009-04-09 00:04:27 -04:00
|
|
|
}
|
|
|
|
PUSH(v);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2009-04-09 00:04:27 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_AREF)
|
|
|
|
v = Stack[SP - 2];
|
2009-04-09 00:04:27 -04:00
|
|
|
if (isvector(v)) {
|
2019-08-09 07:02:02 -04:00
|
|
|
e = Stack[SP - 1];
|
2009-07-10 00:17:39 -04:00
|
|
|
if (isfixnum(e))
|
|
|
|
i = numval(e);
|
|
|
|
else
|
|
|
|
i = (uint32_t)toulong(e, "aref");
|
2009-04-28 00:10:18 -04:00
|
|
|
if ((unsigned)i >= vector_size(v))
|
2009-07-10 00:17:39 -04:00
|
|
|
bounds_error("aref", v, e);
|
2009-04-09 00:04:27 -04:00
|
|
|
v = vector_elt(v, i);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (isarray(v)) {
|
|
|
|
v = cvalue_array_aref(&Stack[SP - 2]);
|
|
|
|
} else {
|
2009-04-09 00:04:27 -04:00
|
|
|
type_error("aref", "sequence", v);
|
|
|
|
}
|
2009-04-14 20:12:01 -04:00
|
|
|
POPN(1);
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 1] = v;
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_ASET)
|
|
|
|
e = Stack[SP - 3];
|
2009-04-09 00:04:27 -04:00
|
|
|
if (isvector(e)) {
|
2019-08-09 07:02:02 -04:00
|
|
|
i = tofixnum(Stack[SP - 2], "aset!");
|
2009-04-28 00:10:18 -04:00
|
|
|
if ((unsigned)i >= vector_size(e))
|
2019-08-09 07:02:02 -04:00
|
|
|
bounds_error("aset!", v, Stack[SP - 1]);
|
|
|
|
vector_elt(e, i) = (v = Stack[SP - 1]);
|
|
|
|
} else if (isarray(e)) {
|
|
|
|
v = cvalue_array_aset(&Stack[SP - 3]);
|
|
|
|
} else {
|
2009-04-09 00:04:27 -04:00
|
|
|
type_error("aset!", "sequence", e);
|
|
|
|
}
|
|
|
|
POPN(2);
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 1] = v;
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_FOR)
|
|
|
|
s = tofixnum(Stack[SP - 3], "for");
|
|
|
|
hi = tofixnum(Stack[SP - 2], "for");
|
|
|
|
// f = Stack[SP-1];
|
2010-01-06 13:27:28 -05:00
|
|
|
v = FL_UNSPECIFIED;
|
2009-04-14 20:12:01 -04:00
|
|
|
SP += 2;
|
2009-06-14 22:25:21 -04:00
|
|
|
n = SP;
|
2019-08-09 07:02:02 -04:00
|
|
|
for (; s <= hi; s++) {
|
|
|
|
Stack[SP - 2] = Stack[SP - 3];
|
|
|
|
Stack[SP - 1] = fixnum(s);
|
2009-04-14 20:12:01 -04:00
|
|
|
v = apply_cl(1);
|
2009-06-14 22:25:21 -04:00
|
|
|
SP = n;
|
2009-04-14 20:12:01 -04:00
|
|
|
}
|
|
|
|
POPN(4);
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 1] = v;
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_LOADT) PUSH(FL_T);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_LOADF) PUSH(FL_F);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_LOADNIL) PUSH(NIL);
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_LOAD0) PUSH(fixnum(0));
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_LOAD1) PUSH(fixnum(1));
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_LOADI8) s = (int8_t)*ip++;
|
|
|
|
PUSH(fixnum(s));
|
|
|
|
NEXT_OP;
|
|
|
|
OP(OP_LOADV)
|
|
|
|
v = fn_vals(Stack[bp - 1]);
|
2009-06-14 22:25:21 -04:00
|
|
|
assert(*ip < vector_size(v));
|
2019-08-09 07:02:02 -04:00
|
|
|
v = vector_elt(v, *ip);
|
|
|
|
ip++;
|
2009-04-09 00:04:27 -04:00
|
|
|
PUSH(v);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_LOADVL)
|
|
|
|
v = fn_vals(Stack[bp - 1]);
|
|
|
|
v = vector_elt(v, GET_INT32(ip));
|
|
|
|
ip += 4;
|
2009-04-09 00:04:27 -04:00
|
|
|
PUSH(v);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_LOADGL)
|
|
|
|
v = fn_vals(Stack[bp - 1]);
|
|
|
|
v = vector_elt(v, GET_INT32(ip));
|
|
|
|
ip += 4;
|
2009-04-09 00:04:27 -04:00
|
|
|
goto do_loadg;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_LOADG)
|
|
|
|
v = fn_vals(Stack[bp - 1]);
|
2009-06-14 22:25:21 -04:00
|
|
|
assert(*ip < vector_size(v));
|
2019-08-09 07:02:02 -04:00
|
|
|
v = vector_elt(v, *ip);
|
|
|
|
ip++;
|
2009-04-09 00:04:27 -04:00
|
|
|
do_loadg:
|
2009-04-14 20:12:01 -04:00
|
|
|
assert(issymbol(v));
|
2019-08-09 12:30:15 -04:00
|
|
|
sym = (struct symbol *)ptr(v);
|
2009-04-09 00:04:27 -04:00
|
|
|
if (sym->binding == UNBOUND)
|
2010-04-29 14:01:26 -04:00
|
|
|
fl_raise(fl_list2(UnboundError, v));
|
2009-04-09 00:04:27 -04:00
|
|
|
PUSH(sym->binding);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2009-04-09 00:04:27 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_SETGL)
|
|
|
|
v = fn_vals(Stack[bp - 1]);
|
|
|
|
v = vector_elt(v, GET_INT32(ip));
|
|
|
|
ip += 4;
|
2009-04-09 00:04:27 -04:00
|
|
|
goto do_setg;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_SETG)
|
|
|
|
v = fn_vals(Stack[bp - 1]);
|
2009-06-14 22:25:21 -04:00
|
|
|
assert(*ip < vector_size(v));
|
2019-08-09 07:02:02 -04:00
|
|
|
v = vector_elt(v, *ip);
|
|
|
|
ip++;
|
2009-04-09 00:04:27 -04:00
|
|
|
do_setg:
|
2009-04-14 20:12:01 -04:00
|
|
|
assert(issymbol(v));
|
2019-08-09 12:30:15 -04:00
|
|
|
sym = (struct symbol *)ptr(v);
|
2019-08-09 07:02:02 -04:00
|
|
|
v = Stack[SP - 1];
|
2009-07-28 00:16:20 -04:00
|
|
|
if (!isconstant(sym))
|
2009-04-09 00:04:27 -04:00
|
|
|
sym->binding = v;
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2009-04-09 00:04:27 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_LOADA)
|
2009-04-14 20:12:01 -04:00
|
|
|
assert(nargs > 0);
|
2009-06-14 22:25:21 -04:00
|
|
|
i = *ip++;
|
2009-04-16 10:21:16 -04:00
|
|
|
if (captured) {
|
2009-06-28 15:47:11 -04:00
|
|
|
e = Stack[bp];
|
2009-06-09 14:46:02 -04:00
|
|
|
assert(isvector(e));
|
|
|
|
assert(i < vector_size(e));
|
|
|
|
v = vector_elt(e, i);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
|
|
|
v = Stack[bp + i];
|
2009-04-14 20:12:01 -04:00
|
|
|
}
|
2009-04-09 00:04:27 -04:00
|
|
|
PUSH(v);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_LOADA0)
|
2009-06-07 23:22:32 -04:00
|
|
|
if (captured)
|
2009-06-28 15:47:11 -04:00
|
|
|
v = vector_elt(Stack[bp], 0);
|
2009-06-07 23:22:32 -04:00
|
|
|
else
|
2009-06-28 15:47:11 -04:00
|
|
|
v = Stack[bp];
|
2009-06-07 23:22:32 -04:00
|
|
|
PUSH(v);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_LOADA1)
|
2009-06-07 23:22:32 -04:00
|
|
|
if (captured)
|
2009-06-28 15:47:11 -04:00
|
|
|
v = vector_elt(Stack[bp], 1);
|
2009-06-07 23:22:32 -04:00
|
|
|
else
|
2019-08-09 07:02:02 -04:00
|
|
|
v = Stack[bp + 1];
|
2009-06-07 23:22:32 -04:00
|
|
|
PUSH(v);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_LOADAL)
|
2009-06-06 17:15:54 -04:00
|
|
|
assert(nargs > 0);
|
2019-08-09 07:02:02 -04:00
|
|
|
i = GET_INT32(ip);
|
|
|
|
ip += 4;
|
2009-06-06 17:15:54 -04:00
|
|
|
if (captured)
|
2009-06-28 15:47:11 -04:00
|
|
|
v = vector_elt(Stack[bp], i);
|
2009-06-06 17:15:54 -04:00
|
|
|
else
|
2019-08-09 07:02:02 -04:00
|
|
|
v = Stack[bp + i];
|
2009-06-06 17:15:54 -04:00
|
|
|
PUSH(v);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_SETA)
|
2009-04-14 20:12:01 -04:00
|
|
|
assert(nargs > 0);
|
2019-08-09 07:02:02 -04:00
|
|
|
v = Stack[SP - 1];
|
2009-06-14 22:25:21 -04:00
|
|
|
i = *ip++;
|
2009-04-16 10:21:16 -04:00
|
|
|
if (captured) {
|
2009-06-28 15:47:11 -04:00
|
|
|
e = Stack[bp];
|
2009-06-09 14:46:02 -04:00
|
|
|
assert(isvector(e));
|
|
|
|
assert(i < vector_size(e));
|
|
|
|
vector_elt(e, i) = v;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
|
|
|
Stack[bp + i] = v;
|
2009-04-14 20:12:01 -04:00
|
|
|
}
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_SETAL)
|
2009-06-06 17:15:54 -04:00
|
|
|
assert(nargs > 0);
|
2019-08-09 07:02:02 -04:00
|
|
|
v = Stack[SP - 1];
|
|
|
|
i = GET_INT32(ip);
|
|
|
|
ip += 4;
|
2009-06-06 17:15:54 -04:00
|
|
|
if (captured)
|
2009-06-28 15:47:11 -04:00
|
|
|
vector_elt(Stack[bp], i) = v;
|
2009-06-06 17:15:54 -04:00
|
|
|
else
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[bp + i] = v;
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_LOADC)
|
2009-06-14 22:25:21 -04:00
|
|
|
s = *ip++;
|
|
|
|
i = *ip++;
|
2019-08-09 07:02:02 -04:00
|
|
|
v = Stack[bp + nargs];
|
2009-04-09 00:04:27 -04:00
|
|
|
while (s--)
|
2019-08-09 07:02:02 -04:00
|
|
|
v = vector_elt(v, vector_size(v) - 1);
|
2009-04-14 20:12:01 -04:00
|
|
|
assert(isvector(v));
|
|
|
|
assert(i < vector_size(v));
|
2009-06-28 15:47:11 -04:00
|
|
|
PUSH(vector_elt(v, i));
|
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_SETC)
|
2009-06-28 15:47:11 -04:00
|
|
|
s = *ip++;
|
|
|
|
i = *ip++;
|
2019-08-09 07:02:02 -04:00
|
|
|
v = Stack[bp + nargs];
|
2009-06-28 15:47:11 -04:00
|
|
|
while (s--)
|
2019-08-09 07:02:02 -04:00
|
|
|
v = vector_elt(v, vector_size(v) - 1);
|
2009-06-28 15:47:11 -04:00
|
|
|
assert(isvector(v));
|
|
|
|
assert(i < vector_size(v));
|
2019-08-09 07:02:02 -04:00
|
|
|
vector_elt(v, i) = Stack[SP - 1];
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_LOADC00)
|
|
|
|
PUSH(vector_elt(Stack[bp + nargs], 0));
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_LOADC01)
|
|
|
|
PUSH(vector_elt(Stack[bp + nargs], 1));
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_LOADCL)
|
|
|
|
s = GET_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
i = GET_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
v = Stack[bp + nargs];
|
2009-06-06 17:15:54 -04:00
|
|
|
while (s--)
|
2019-08-09 07:02:02 -04:00
|
|
|
v = vector_elt(v, vector_size(v) - 1);
|
2009-06-28 15:47:11 -04:00
|
|
|
PUSH(vector_elt(v, i));
|
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_SETCL)
|
|
|
|
s = GET_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
i = GET_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
v = Stack[bp + nargs];
|
2009-06-28 15:47:11 -04:00
|
|
|
while (s--)
|
2019-08-09 07:02:02 -04:00
|
|
|
v = vector_elt(v, vector_size(v) - 1);
|
2010-05-04 14:17:55 -04:00
|
|
|
assert(i < vector_size(v));
|
2019-08-09 07:02:02 -04:00
|
|
|
vector_elt(v, i) = Stack[SP - 1];
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2009-04-09 00:04:27 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_CLOSURE)
|
2009-04-09 12:09:02 -04:00
|
|
|
// build a closure (lambda args body . env)
|
2009-04-16 10:21:16 -04:00
|
|
|
if (nargs > 0 && !captured) {
|
2009-04-09 12:09:02 -04:00
|
|
|
// save temporary environment to the heap
|
2009-06-09 14:46:02 -04:00
|
|
|
n = nargs;
|
|
|
|
pv = alloc_words(n + 2);
|
2009-04-09 12:09:02 -04:00
|
|
|
PUSH(tagptr(pv, TAG_VECTOR));
|
2019-08-09 07:02:02 -04:00
|
|
|
pv[0] = fixnum(n + 1);
|
2009-04-09 12:09:02 -04:00
|
|
|
pv++;
|
2009-06-09 14:46:02 -04:00
|
|
|
do {
|
2019-08-09 07:02:02 -04:00
|
|
|
pv[n] = Stack[bp + n];
|
2009-06-09 14:46:02 -04:00
|
|
|
} while (n--);
|
2009-04-09 12:09:02 -04:00
|
|
|
// environment representation changed; install
|
|
|
|
// the new representation so everybody can see it
|
2009-04-16 10:21:16 -04:00
|
|
|
captured = 1;
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[curr_frame - 1] = 1;
|
|
|
|
Stack[bp] = Stack[SP - 1];
|
|
|
|
} else {
|
|
|
|
PUSH(Stack[bp]); // env has already been captured; share
|
2009-04-09 12:09:02 -04:00
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
if (curheap > lim - 2)
|
2010-05-05 01:31:46 -04:00
|
|
|
gc(0);
|
2019-08-09 07:02:02 -04:00
|
|
|
pv = (value_t *)curheap;
|
|
|
|
curheap += (4 * sizeof(value_t));
|
|
|
|
e = Stack[SP - 2]; // closure to copy
|
2009-08-02 00:06:07 -04:00
|
|
|
assert(isfunction(e));
|
2019-08-09 07:02:02 -04:00
|
|
|
pv[0] = ((value_t *)ptr(e))[0];
|
|
|
|
pv[1] = ((value_t *)ptr(e))[1];
|
|
|
|
pv[2] = Stack[SP - 1]; // env
|
|
|
|
pv[3] = ((value_t *)ptr(e))[3];
|
2009-08-02 00:06:07 -04:00
|
|
|
POPN(1);
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 1] = tagptr(pv, TAG_FUNCTION);
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2009-04-09 12:09:02 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_TRYCATCH)
|
2009-04-20 20:56:05 -04:00
|
|
|
v = do_trycatch();
|
2009-04-14 20:12:01 -04:00
|
|
|
POPN(1);
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 1] = v;
|
2009-06-28 15:47:11 -04:00
|
|
|
NEXT_OP;
|
2009-06-27 19:07:22 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_OPTARGS)
|
|
|
|
i = GET_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
n = GET_INT32(ip);
|
|
|
|
ip += 4;
|
2009-08-02 00:06:07 -04:00
|
|
|
if (nargs < i)
|
|
|
|
lerror(ArgError, "apply: too few arguments");
|
|
|
|
if ((int32_t)n > 0) {
|
|
|
|
if (nargs > n)
|
|
|
|
lerror(ArgError, "apply: too many arguments");
|
2019-08-09 07:02:02 -04:00
|
|
|
} else
|
|
|
|
n = -n;
|
2009-08-02 00:06:07 -04:00
|
|
|
if (n > nargs) {
|
|
|
|
n -= nargs;
|
|
|
|
SP += n;
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[SP - 1] = Stack[SP - n - 1];
|
|
|
|
Stack[SP - 2] = Stack[SP - n - 2];
|
|
|
|
Stack[SP - 3] = nargs + n;
|
|
|
|
Stack[SP - 4] = Stack[SP - n - 4];
|
|
|
|
Stack[SP - 5] = Stack[SP - n - 5];
|
2009-08-02 00:06:07 -04:00
|
|
|
curr_frame = SP;
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
Stack[bp + nargs + i] = UNBOUND;
|
2009-08-02 00:06:07 -04:00
|
|
|
}
|
|
|
|
nargs += n;
|
|
|
|
}
|
|
|
|
NEXT_OP;
|
2019-08-09 07:02:02 -04:00
|
|
|
OP(OP_KEYARGS)
|
|
|
|
v = fn_vals(Stack[bp - 1]);
|
2009-08-02 00:06:07 -04:00
|
|
|
v = vector_elt(v, 0);
|
2019-08-09 07:02:02 -04:00
|
|
|
i = GET_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
n = GET_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
s = GET_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
nargs =
|
|
|
|
process_keys(v, i, n, labs(s) - (i + n), bp, nargs, s < 0);
|
2009-08-02 00:06:07 -04:00
|
|
|
NEXT_OP;
|
|
|
|
|
2009-06-28 15:47:11 -04:00
|
|
|
#ifndef USE_COMPUTED_GOTO
|
2009-06-27 19:07:22 -04:00
|
|
|
default:
|
|
|
|
goto dispatch;
|
2009-06-28 15:47:11 -04:00
|
|
|
#endif
|
2009-06-27 19:07:22 -04:00
|
|
|
}
|
|
|
|
}
|
2009-06-28 15:47:11 -04:00
|
|
|
#ifdef USE_COMPUTED_GOTO
|
|
|
|
return UNBOUND; // not reached
|
|
|
|
#else
|
2009-06-27 19:07:22 -04:00
|
|
|
goto dispatch;
|
2009-06-28 15:47:11 -04:00
|
|
|
#endif
|
2009-06-27 19:07:22 -04:00
|
|
|
}
|
|
|
|
|
2009-08-28 20:54:51 -04:00
|
|
|
static uint32_t compute_maxstack(uint8_t *code, size_t len, int bswap)
|
2009-06-27 19:07:22 -04:00
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
uint8_t *ip = code + 4, *end = code + len;
|
2009-06-27 19:07:22 -04:00
|
|
|
uint8_t op;
|
2009-07-28 00:16:20 -04:00
|
|
|
uint32_t i, n, sp = 0, maxsp = 0;
|
2009-06-27 19:07:22 -04:00
|
|
|
|
|
|
|
while (1) {
|
2019-08-09 07:02:02 -04:00
|
|
|
if ((int32_t)sp > (int32_t)maxsp)
|
|
|
|
maxsp = sp;
|
|
|
|
if (ip >= end)
|
|
|
|
break;
|
2009-06-27 19:07:22 -04:00
|
|
|
op = *ip++;
|
|
|
|
switch (op) {
|
|
|
|
case OP_ARGC:
|
|
|
|
n = *ip++;
|
|
|
|
break;
|
|
|
|
case OP_VARGC:
|
|
|
|
n = *ip++;
|
2019-08-09 07:02:02 -04:00
|
|
|
sp += (n + 2);
|
2009-06-27 19:07:22 -04:00
|
|
|
break;
|
|
|
|
case OP_LARGC:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
n = GET_INT32(ip);
|
|
|
|
ip += 4;
|
2009-06-27 19:07:22 -04:00
|
|
|
break;
|
|
|
|
case OP_LVARGC:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
n = GET_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
sp += (n + 2);
|
2009-06-27 19:07:22 -04:00
|
|
|
break;
|
2009-07-26 23:34:33 -04:00
|
|
|
case OP_OPTARGS:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
i = GET_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
n = abs(GET_INT32(ip));
|
|
|
|
ip += 4;
|
|
|
|
sp += (n - i);
|
2009-08-02 00:06:07 -04:00
|
|
|
break;
|
|
|
|
case OP_KEYARGS:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
i = GET_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
n = GET_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
n = abs(GET_INT32(ip));
|
|
|
|
ip += 4;
|
|
|
|
sp += (n - i);
|
2009-07-28 00:16:20 -04:00
|
|
|
break;
|
|
|
|
case OP_BRBOUND:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
ip += 4;
|
2009-08-28 20:54:51 -04:00
|
|
|
sp++;
|
2009-07-26 23:34:33 -04:00
|
|
|
break;
|
2009-06-27 19:07:22 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_TCALL:
|
|
|
|
case OP_CALL:
|
2009-06-27 19:07:22 -04:00
|
|
|
n = *ip++; // nargs
|
|
|
|
sp -= n;
|
|
|
|
break;
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_TCALLL:
|
|
|
|
case OP_CALLL:
|
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
n = GET_INT32(ip);
|
|
|
|
ip += 4;
|
2009-08-28 20:54:51 -04:00
|
|
|
sp -= n;
|
|
|
|
break;
|
|
|
|
case OP_JMP:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (bswap)
|
|
|
|
SWAP_INT16(ip);
|
|
|
|
ip += 2;
|
|
|
|
break;
|
2009-08-28 20:54:51 -04:00
|
|
|
case OP_JMPL:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
break;
|
|
|
|
case OP_BRF:
|
|
|
|
case OP_BRT:
|
|
|
|
if (bswap)
|
|
|
|
SWAP_INT16(ip);
|
|
|
|
ip += 2;
|
2009-06-27 19:07:22 -04:00
|
|
|
sp--;
|
|
|
|
break;
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_BRFL:
|
|
|
|
case OP_BRTL:
|
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
2009-08-28 20:54:51 -04:00
|
|
|
ip += 4;
|
|
|
|
sp--;
|
|
|
|
break;
|
|
|
|
case OP_BRNE:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (bswap)
|
|
|
|
SWAP_INT16(ip);
|
2009-08-28 20:54:51 -04:00
|
|
|
ip += 2;
|
|
|
|
sp -= 2;
|
|
|
|
break;
|
|
|
|
case OP_BRNEL:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
2009-08-28 20:54:51 -04:00
|
|
|
ip += 4;
|
|
|
|
sp -= 2;
|
|
|
|
break;
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_BRNN:
|
|
|
|
case OP_BRN:
|
|
|
|
if (bswap)
|
|
|
|
SWAP_INT16(ip);
|
2009-08-28 20:54:51 -04:00
|
|
|
ip += 2;
|
|
|
|
sp--;
|
|
|
|
break;
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_BRNNL:
|
|
|
|
case OP_BRNL:
|
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
2009-06-27 19:07:22 -04:00
|
|
|
ip += 4;
|
|
|
|
sp--;
|
|
|
|
break;
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_RET:
|
|
|
|
sp--;
|
|
|
|
break;
|
2009-06-27 19:07:22 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_CONS:
|
|
|
|
case OP_SETCAR:
|
|
|
|
case OP_SETCDR:
|
|
|
|
case OP_POP:
|
|
|
|
case OP_EQ:
|
|
|
|
case OP_EQV:
|
|
|
|
case OP_EQUAL:
|
|
|
|
case OP_ADD2:
|
|
|
|
case OP_SUB2:
|
|
|
|
case OP_IDIV:
|
|
|
|
case OP_NUMEQ:
|
|
|
|
case OP_LT:
|
|
|
|
case OP_COMPARE:
|
|
|
|
case OP_AREF:
|
|
|
|
case OP_TRYCATCH:
|
2009-06-27 19:07:22 -04:00
|
|
|
sp--;
|
|
|
|
break;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_PAIRP:
|
|
|
|
case OP_ATOMP:
|
|
|
|
case OP_NOT:
|
|
|
|
case OP_NULLP:
|
|
|
|
case OP_BOOLEANP:
|
|
|
|
case OP_SYMBOLP:
|
|
|
|
case OP_NUMBERP:
|
|
|
|
case OP_FIXNUMP:
|
|
|
|
case OP_BOUNDP:
|
|
|
|
case OP_BUILTINP:
|
|
|
|
case OP_FUNCTIONP:
|
|
|
|
case OP_VECTORP:
|
|
|
|
case OP_NOP:
|
|
|
|
case OP_CAR:
|
|
|
|
case OP_CDR:
|
|
|
|
case OP_NEG:
|
|
|
|
case OP_CLOSURE:
|
2009-06-27 19:07:22 -04:00
|
|
|
break;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_TAPPLY:
|
|
|
|
case OP_APPLY:
|
2009-06-27 19:07:22 -04:00
|
|
|
n = *ip++;
|
2019-08-09 07:02:02 -04:00
|
|
|
sp -= (n - 1);
|
2009-06-27 19:07:22 -04:00
|
|
|
break;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_LIST:
|
|
|
|
case OP_ADD:
|
|
|
|
case OP_SUB:
|
|
|
|
case OP_MUL:
|
|
|
|
case OP_DIV:
|
2009-06-27 19:07:22 -04:00
|
|
|
case OP_VECTOR:
|
|
|
|
n = *ip++;
|
2019-08-09 07:02:02 -04:00
|
|
|
sp -= (n - 1);
|
2009-06-27 19:07:22 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OP_ASET:
|
|
|
|
sp -= 2;
|
|
|
|
break;
|
|
|
|
case OP_FOR:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (sp + 2 > maxsp)
|
|
|
|
maxsp = sp + 2;
|
|
|
|
sp -= 2;
|
2009-06-27 19:07:22 -04:00
|
|
|
break;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_LOADT:
|
|
|
|
case OP_LOADF:
|
|
|
|
case OP_LOADNIL:
|
|
|
|
case OP_LOAD0:
|
|
|
|
case OP_LOAD1:
|
|
|
|
case OP_LOADA0:
|
|
|
|
case OP_LOADA1:
|
|
|
|
case OP_LOADC00:
|
|
|
|
case OP_LOADC01:
|
|
|
|
case OP_DUP:
|
2009-06-27 19:07:22 -04:00
|
|
|
sp++;
|
|
|
|
break;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_LOADI8:
|
|
|
|
case OP_LOADV:
|
|
|
|
case OP_LOADG:
|
|
|
|
case OP_LOADA:
|
2009-06-27 19:07:22 -04:00
|
|
|
ip++;
|
|
|
|
sp++;
|
|
|
|
break;
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_LOADVL:
|
|
|
|
case OP_LOADGL:
|
|
|
|
case OP_LOADAL:
|
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
ip += 4;
|
2009-06-27 19:07:22 -04:00
|
|
|
sp++;
|
|
|
|
break;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_SETG:
|
|
|
|
case OP_SETA:
|
2009-06-27 19:07:22 -04:00
|
|
|
ip++;
|
|
|
|
break;
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_SETGL:
|
|
|
|
case OP_SETAL:
|
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
ip += 4;
|
2009-06-27 19:07:22 -04:00
|
|
|
break;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
case OP_LOADC:
|
|
|
|
ip += 2;
|
|
|
|
sp++;
|
|
|
|
break;
|
2009-06-27 19:07:22 -04:00
|
|
|
case OP_SETC:
|
2019-08-09 07:02:02 -04:00
|
|
|
ip += 2;
|
2009-06-27 19:07:22 -04:00
|
|
|
break;
|
2009-08-28 20:54:51 -04:00
|
|
|
case OP_LOADCL:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
sp++;
|
|
|
|
break;
|
2009-06-27 19:07:22 -04:00
|
|
|
case OP_SETCL:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
ip += 4;
|
|
|
|
if (bswap)
|
|
|
|
SWAP_INT32(ip);
|
|
|
|
ip += 4;
|
2009-06-27 19:07:22 -04:00
|
|
|
break;
|
2009-04-09 00:04:27 -04:00
|
|
|
}
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
return maxsp + 5;
|
2009-04-09 00:04:27 -04:00
|
|
|
}
|
|
|
|
|
2009-07-05 23:56:48 -04:00
|
|
|
// top = top frame pointer to start at
|
|
|
|
static value_t _stacktrace(uint32_t top)
|
|
|
|
{
|
|
|
|
uint32_t bp, sz;
|
|
|
|
value_t v, lst = NIL;
|
|
|
|
fl_gc_handle(&lst);
|
|
|
|
while (top > 0) {
|
2019-08-09 07:02:02 -04:00
|
|
|
sz = Stack[top - 3] + 1;
|
|
|
|
bp = top - 5 - sz;
|
2009-07-05 23:56:48 -04:00
|
|
|
v = alloc_vector(sz, 0);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (Stack[top - 1] /*captured*/) {
|
2009-07-05 23:56:48 -04:00
|
|
|
vector_elt(v, 0) = Stack[bp];
|
2019-08-09 07:02:02 -04:00
|
|
|
memcpy(&vector_elt(v, 1), &vector_elt(Stack[bp + 1], 0),
|
|
|
|
(sz - 1) * sizeof(value_t));
|
|
|
|
} else {
|
2016-08-09 22:58:44 -04:00
|
|
|
uint32_t i;
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 0; i < sz; i++) {
|
|
|
|
value_t si = Stack[bp + i];
|
2016-08-09 22:58:44 -04:00
|
|
|
// if there's an error evaluating argument defaults some slots
|
|
|
|
// might be left set to UNBOUND (issue #22)
|
2019-08-09 07:02:02 -04:00
|
|
|
vector_elt(v, i) = (si == UNBOUND ? FL_UNSPECIFIED : si);
|
2016-08-09 22:58:44 -04:00
|
|
|
}
|
2009-07-05 23:56:48 -04:00
|
|
|
}
|
|
|
|
lst = fl_cons(v, lst);
|
2019-08-09 07:02:02 -04:00
|
|
|
top = Stack[top - 4];
|
2009-07-05 23:56:48 -04:00
|
|
|
}
|
|
|
|
fl_free_gc_handles(1);
|
|
|
|
return lst;
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// builtins
|
|
|
|
// -------------------------------------------------------------------
|
2008-09-10 22:37:38 -04:00
|
|
|
|
2019-08-09 12:20:18 -04:00
|
|
|
void assign_global_builtins(struct builtinspec *b)
|
2008-11-23 02:12:37 -05:00
|
|
|
{
|
|
|
|
while (b->name != NULL) {
|
2013-06-08 19:29:15 -04:00
|
|
|
setc(symbol(b->name), cbuiltin(b->name, b->fptr));
|
2008-11-23 02:12:37 -05:00
|
|
|
b++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-19 18:22:17 -04:00
|
|
|
static value_t fl_function(value_t *args, uint32_t nargs)
|
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
struct cvalue *arr;
|
|
|
|
char *data;
|
|
|
|
int swap;
|
|
|
|
uint32_t ms;
|
|
|
|
struct function *fn;
|
|
|
|
value_t fv;
|
|
|
|
|
2010-05-08 20:42:37 -04:00
|
|
|
if (nargs == 1 && issymbol(args[0]))
|
|
|
|
return fl_builtin(args, nargs);
|
2009-07-10 00:17:39 -04:00
|
|
|
if (nargs < 2 || nargs > 4)
|
2009-04-19 18:22:17 -04:00
|
|
|
argcount("function", nargs, 2);
|
2009-08-23 03:06:57 -04:00
|
|
|
if (!fl_isstring(args[0]))
|
2009-04-22 20:22:03 -04:00
|
|
|
type_error("function", "string", args[0]);
|
2009-04-19 18:22:17 -04:00
|
|
|
if (!isvector(args[1]))
|
|
|
|
type_error("function", "vector", args[1]);
|
2019-08-18 18:14:09 -04:00
|
|
|
arr = (struct cvalue *)ptr(args[0]);
|
2009-04-21 10:53:18 -04:00
|
|
|
cv_pin(arr);
|
2019-08-18 18:14:09 -04:00
|
|
|
data = cv_data(arr);
|
|
|
|
swap = 0;
|
2009-07-28 00:16:20 -04:00
|
|
|
if ((uint8_t)data[4] >= N_OPCODES) {
|
2009-04-21 11:35:46 -04:00
|
|
|
// read syntax, shifted 48 for compact text representation
|
|
|
|
size_t i, sz = cv_len(arr);
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 0; i < sz; i++)
|
2009-04-21 11:35:46 -04:00
|
|
|
data[i] -= 48;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2019-08-09 14:30:30 -04:00
|
|
|
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
2009-08-28 20:54:51 -04:00
|
|
|
swap = 1;
|
|
|
|
#endif
|
|
|
|
}
|
2019-08-18 18:14:09 -04:00
|
|
|
ms = compute_maxstack((uint8_t *)data, cv_len(arr), swap);
|
2009-06-27 19:07:22 -04:00
|
|
|
PUT_INT32(data, ms);
|
2019-08-18 18:14:09 -04:00
|
|
|
fn = (struct function *)alloc_words(4);
|
|
|
|
fv = tagptr(fn, TAG_FUNCTION);
|
2009-04-19 18:22:17 -04:00
|
|
|
fn->bcode = args[0];
|
|
|
|
fn->vals = args[1];
|
2009-07-10 00:17:39 -04:00
|
|
|
fn->env = NIL;
|
|
|
|
fn->name = LAMBDA;
|
|
|
|
if (nargs > 2) {
|
|
|
|
if (issymbol(args[2])) {
|
|
|
|
fn->name = args[2];
|
|
|
|
if (nargs > 3)
|
|
|
|
fn->env = args[3];
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2009-07-10 00:17:39 -04:00
|
|
|
fn->env = args[2];
|
|
|
|
if (nargs > 3) {
|
|
|
|
if (!issymbol(args[3]))
|
|
|
|
type_error("function", "symbol", args[3]);
|
|
|
|
fn->name = args[3];
|
|
|
|
}
|
|
|
|
}
|
2009-08-07 20:29:55 -04:00
|
|
|
if (isgensym(fn->name))
|
|
|
|
lerror(ArgError, "function: name should not be a gensym");
|
2009-07-10 00:17:39 -04:00
|
|
|
}
|
2009-04-19 18:22:17 -04:00
|
|
|
return fv;
|
|
|
|
}
|
|
|
|
|
2009-05-12 21:13:40 -04:00
|
|
|
static value_t fl_function_code(value_t *args, uint32_t nargs)
|
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t v;
|
|
|
|
|
2009-05-12 21:13:40 -04:00
|
|
|
argcount("function:code", nargs, 1);
|
2019-08-18 18:14:09 -04:00
|
|
|
v = args[0];
|
2019-08-09 07:02:02 -04:00
|
|
|
if (!isclosure(v))
|
|
|
|
type_error("function:code", "function", v);
|
2009-05-12 21:13:40 -04:00
|
|
|
return fn_bcode(v);
|
|
|
|
}
|
|
|
|
static value_t fl_function_vals(value_t *args, uint32_t nargs)
|
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t v;
|
|
|
|
|
2009-05-12 21:13:40 -04:00
|
|
|
argcount("function:vals", nargs, 1);
|
2019-08-18 18:14:09 -04:00
|
|
|
v = args[0];
|
2019-08-09 07:02:02 -04:00
|
|
|
if (!isclosure(v))
|
|
|
|
type_error("function:vals", "function", v);
|
2009-05-12 21:13:40 -04:00
|
|
|
return fn_vals(v);
|
|
|
|
}
|
|
|
|
static value_t fl_function_env(value_t *args, uint32_t nargs)
|
2009-04-19 18:22:17 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t v;
|
|
|
|
|
2009-05-12 21:13:40 -04:00
|
|
|
argcount("function:env", nargs, 1);
|
2019-08-18 18:14:09 -04:00
|
|
|
v = args[0];
|
2019-08-09 07:02:02 -04:00
|
|
|
if (!isclosure(v))
|
|
|
|
type_error("function:env", "function", v);
|
2009-05-12 21:13:40 -04:00
|
|
|
return fn_env(v);
|
2009-04-19 18:22:17 -04:00
|
|
|
}
|
2009-07-10 00:17:39 -04:00
|
|
|
static value_t fl_function_name(value_t *args, uint32_t nargs)
|
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t v;
|
|
|
|
|
2009-07-10 00:17:39 -04:00
|
|
|
argcount("function:name", nargs, 1);
|
2019-08-18 18:14:09 -04:00
|
|
|
v = args[0];
|
2019-08-09 07:02:02 -04:00
|
|
|
if (!isclosure(v))
|
|
|
|
type_error("function:name", "function", v);
|
2009-07-10 00:17:39 -04:00
|
|
|
return fn_name(v);
|
|
|
|
}
|
2009-04-19 18:22:17 -04:00
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_copylist(value_t *args, uint32_t nargs)
|
2009-06-29 23:21:41 -04:00
|
|
|
{
|
|
|
|
argcount("copy-list", nargs, 1);
|
2009-12-03 15:12:06 -05:00
|
|
|
return copy_list(args[0]);
|
2009-06-29 23:21:41 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_append(value_t *args, uint32_t nargs)
|
2009-06-29 23:21:41 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t first, lst, lastcons;
|
|
|
|
uint32_t i;
|
|
|
|
|
2009-06-29 23:21:41 -04:00
|
|
|
if (nargs == 0)
|
|
|
|
return NIL;
|
2019-08-18 18:14:09 -04:00
|
|
|
first = lastcons = NIL;
|
2009-06-29 23:21:41 -04:00
|
|
|
fl_gc_handle(&first);
|
|
|
|
fl_gc_handle(&lastcons);
|
2019-08-18 18:14:09 -04:00
|
|
|
i = 0;
|
2009-06-29 23:21:41 -04:00
|
|
|
while (1) {
|
2009-07-20 00:57:17 -04:00
|
|
|
lst = args[i++];
|
2019-08-09 07:02:02 -04:00
|
|
|
if (i >= nargs)
|
|
|
|
break;
|
2009-06-29 23:21:41 -04:00
|
|
|
if (iscons(lst)) {
|
2009-12-03 15:12:06 -05:00
|
|
|
lst = copy_list(lst);
|
2009-06-29 23:21:41 -04:00
|
|
|
if (first == NIL)
|
|
|
|
first = lst;
|
|
|
|
else
|
|
|
|
cdr_(lastcons) = lst;
|
2019-08-09 12:28:14 -04:00
|
|
|
lastcons = tagptr((((struct cons *)curheap) - 1), TAG_CONS);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (lst != NIL) {
|
2009-06-29 23:21:41 -04:00
|
|
|
type_error("append", "cons", lst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (first == NIL)
|
|
|
|
first = lst;
|
|
|
|
else
|
|
|
|
cdr_(lastcons) = lst;
|
|
|
|
fl_free_gc_handles(2);
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_liststar(value_t *args, uint32_t nargs)
|
2009-06-29 23:21:41 -04:00
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
if (nargs == 1)
|
|
|
|
return args[0];
|
|
|
|
else if (nargs == 0)
|
|
|
|
argcount("list*", nargs, 1);
|
2009-06-29 23:21:41 -04:00
|
|
|
return _list(args, nargs, 1);
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_stacktrace(value_t *args, uint32_t nargs)
|
2009-07-05 23:56:48 -04:00
|
|
|
{
|
|
|
|
(void)args;
|
|
|
|
argcount("stacktrace", nargs, 0);
|
2010-05-02 14:17:47 -04:00
|
|
|
return _stacktrace(fl_throwing_frame ? fl_throwing_frame : curr_frame);
|
2009-07-05 23:56:48 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_map1(value_t *args, uint32_t nargs)
|
2010-12-23 01:49:37 -05:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t first, last, v;
|
|
|
|
int64_t argSP;
|
|
|
|
|
2010-12-23 01:49:37 -05:00
|
|
|
if (nargs < 2)
|
|
|
|
lerror(ArgError, "map: too few arguments");
|
2019-08-09 07:02:02 -04:00
|
|
|
if (!iscons(args[1]))
|
|
|
|
return NIL;
|
2019-08-18 18:14:09 -04:00
|
|
|
argSP = args - Stack;
|
2013-02-11 04:48:21 -05:00
|
|
|
assert(argSP >= 0 && argSP < N_STACK);
|
2010-12-23 01:49:37 -05:00
|
|
|
if (nargs == 2) {
|
2019-08-09 07:02:02 -04:00
|
|
|
if (SP + 3 > N_STACK)
|
|
|
|
grow_stack();
|
2013-02-11 04:48:21 -05:00
|
|
|
PUSH(Stack[argSP]);
|
2019-08-09 07:02:02 -04:00
|
|
|
PUSH(car_(Stack[argSP + 1]));
|
2010-12-23 01:49:37 -05:00
|
|
|
v = _applyn(1);
|
|
|
|
PUSH(v);
|
|
|
|
v = mk_cons();
|
2019-08-09 07:02:02 -04:00
|
|
|
car_(v) = POP();
|
|
|
|
cdr_(v) = NIL;
|
2010-12-23 01:49:37 -05:00
|
|
|
last = first = v;
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[argSP + 1] = cdr_(Stack[argSP + 1]);
|
2010-12-23 01:49:37 -05:00
|
|
|
fl_gc_handle(&first);
|
|
|
|
fl_gc_handle(&last);
|
2019-08-09 07:02:02 -04:00
|
|
|
while (iscons(Stack[argSP + 1])) {
|
|
|
|
Stack[SP - 2] = Stack[argSP];
|
|
|
|
Stack[SP - 1] = car_(Stack[argSP + 1]);
|
2010-12-23 01:49:37 -05:00
|
|
|
v = _applyn(1);
|
|
|
|
PUSH(v);
|
|
|
|
v = mk_cons();
|
2019-08-09 07:02:02 -04:00
|
|
|
car_(v) = POP();
|
|
|
|
cdr_(v) = NIL;
|
2010-12-23 01:49:37 -05:00
|
|
|
cdr_(last) = v;
|
|
|
|
last = v;
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack[argSP + 1] = cdr_(Stack[argSP + 1]);
|
2010-12-23 01:49:37 -05:00
|
|
|
}
|
|
|
|
POPN(2);
|
|
|
|
fl_free_gc_handles(2);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2010-12-23 01:49:37 -05:00
|
|
|
size_t i;
|
2019-08-18 18:14:09 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
while (SP + nargs + 1 > N_STACK)
|
|
|
|
grow_stack();
|
2013-02-11 04:48:21 -05:00
|
|
|
PUSH(Stack[argSP]);
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 1; i < nargs; i++) {
|
|
|
|
PUSH(car(Stack[argSP + i]));
|
|
|
|
Stack[argSP + i] = cdr_(Stack[argSP + i]);
|
2010-12-23 01:49:37 -05:00
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
v = _applyn(nargs - 1);
|
2013-06-10 19:12:53 -04:00
|
|
|
POPN(nargs);
|
2010-12-23 01:49:37 -05:00
|
|
|
PUSH(v);
|
|
|
|
v = mk_cons();
|
2019-08-09 07:02:02 -04:00
|
|
|
car_(v) = POP();
|
|
|
|
cdr_(v) = NIL;
|
2010-12-23 01:49:37 -05:00
|
|
|
last = first = v;
|
|
|
|
fl_gc_handle(&first);
|
|
|
|
fl_gc_handle(&last);
|
2019-08-09 07:02:02 -04:00
|
|
|
while (iscons(Stack[argSP + 1])) {
|
2013-06-10 19:12:53 -04:00
|
|
|
PUSH(Stack[argSP]);
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 1; i < nargs; i++) {
|
|
|
|
PUSH(car(Stack[argSP + i]));
|
|
|
|
Stack[argSP + i] = cdr_(Stack[argSP + i]);
|
2010-12-23 01:49:37 -05:00
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
v = _applyn(nargs - 1);
|
2013-06-10 19:12:53 -04:00
|
|
|
POPN(nargs);
|
2010-12-23 01:49:37 -05:00
|
|
|
PUSH(v);
|
|
|
|
v = mk_cons();
|
2019-08-09 07:02:02 -04:00
|
|
|
car_(v) = POP();
|
|
|
|
cdr_(v) = NIL;
|
2010-12-23 01:49:37 -05:00
|
|
|
cdr_(last) = v;
|
|
|
|
last = v;
|
|
|
|
}
|
|
|
|
fl_free_gc_handles(2);
|
|
|
|
}
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:20:18 -04:00
|
|
|
static struct builtinspec core_builtin_info[] = {
|
2009-04-19 18:22:17 -04:00
|
|
|
{ "function", fl_function },
|
2009-05-12 21:13:40 -04:00
|
|
|
{ "function:code", fl_function_code },
|
|
|
|
{ "function:vals", fl_function_vals },
|
|
|
|
{ "function:env", fl_function_env },
|
2009-07-10 00:17:39 -04:00
|
|
|
{ "function:name", fl_function_name },
|
2009-07-05 23:56:48 -04:00
|
|
|
{ "stacktrace", fl_stacktrace },
|
2009-05-12 21:13:40 -04:00
|
|
|
{ "gensym", fl_gensym },
|
2009-08-09 13:05:40 -04:00
|
|
|
{ "gensym?", fl_gensymp },
|
2009-04-19 18:22:17 -04:00
|
|
|
{ "hash", fl_hash },
|
2009-05-30 13:54:27 -04:00
|
|
|
{ "copy-list", fl_copylist },
|
2009-06-14 22:25:21 -04:00
|
|
|
{ "append", fl_append },
|
2009-05-31 14:58:09 -04:00
|
|
|
{ "list*", fl_liststar },
|
2010-12-23 01:49:37 -05:00
|
|
|
{ "map", fl_map1 },
|
2009-04-19 18:22:17 -04:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// initialization
|
|
|
|
// -------------------------------------------------------------
|
2009-06-29 23:21:41 -04:00
|
|
|
|
2013-06-08 19:29:15 -04:00
|
|
|
extern void builtins_init(void);
|
|
|
|
extern void comparehash_init(void);
|
2009-06-29 23:21:41 -04:00
|
|
|
|
2010-05-02 16:36:39 -04:00
|
|
|
static void lisp_init(size_t initial_heapsize)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
char buf[1024];
|
|
|
|
char *exename;
|
2008-06-30 21:54:22 -04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
llt_init();
|
2012-02-21 12:33:35 -05:00
|
|
|
setlocale(LC_NUMERIC, "C");
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2010-05-02 16:36:39 -04:00
|
|
|
heapsize = initial_heapsize;
|
|
|
|
|
|
|
|
fromspace = LLT_ALLOC(heapsize);
|
2019-08-09 07:02:02 -04:00
|
|
|
tospace = LLT_ALLOC(heapsize);
|
2008-06-30 21:54:22 -04:00
|
|
|
curheap = fromspace;
|
2019-08-09 12:28:14 -04:00
|
|
|
lim = curheap + heapsize - sizeof(struct cons);
|
|
|
|
consflags = bitvector_new(heapsize / sizeof(struct cons), 1);
|
2008-11-23 02:12:37 -05:00
|
|
|
htable_new(&printconses, 32);
|
2008-08-30 18:18:20 -04:00
|
|
|
comparehash_init();
|
2009-07-03 14:43:15 -04:00
|
|
|
N_STACK = 262144;
|
2019-08-09 07:02:02 -04:00
|
|
|
Stack = malloc(N_STACK * sizeof(value_t));
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-08-09 14:04:03 -04:00
|
|
|
FL_NIL = NIL = builtin(OP_THE_EMPTY_LIST);
|
2009-04-24 16:43:56 -04:00
|
|
|
FL_T = builtin(OP_BOOL_CONST_T);
|
|
|
|
FL_F = builtin(OP_BOOL_CONST_F);
|
2009-08-09 14:04:03 -04:00
|
|
|
FL_EOF = builtin(OP_EOF_OBJECT);
|
2019-08-09 07:02:02 -04:00
|
|
|
LAMBDA = symbol("lambda");
|
|
|
|
FUNCTION = symbol("function");
|
|
|
|
QUOTE = symbol("quote");
|
|
|
|
TRYCATCH = symbol("trycatch");
|
|
|
|
BACKQUOTE = symbol("quasiquote");
|
|
|
|
COMMA = symbol("unquote");
|
|
|
|
COMMAAT = symbol("unquote-splicing");
|
|
|
|
COMMADOT = symbol("unquote-nsplicing");
|
|
|
|
IOError = symbol("io-error");
|
|
|
|
ParseError = symbol("parse-error");
|
|
|
|
TypeError = symbol("type-error");
|
|
|
|
ArgError = symbol("arg-error");
|
2008-06-30 21:54:22 -04:00
|
|
|
UnboundError = symbol("unbound-error");
|
2019-08-09 07:02:02 -04:00
|
|
|
KeyError = symbol("key-error");
|
|
|
|
MemoryError = symbol("memory-error");
|
2008-06-30 21:54:22 -04:00
|
|
|
BoundsError = symbol("bounds-error");
|
|
|
|
DivideError = symbol("divide-error");
|
2009-03-21 22:05:26 -04:00
|
|
|
EnumerationError = symbol("enumeration-error");
|
2019-08-09 07:02:02 -04:00
|
|
|
Error = symbol("error");
|
|
|
|
pairsym = symbol("pair");
|
|
|
|
symbolsym = symbol("symbol");
|
|
|
|
fixnumsym = symbol("fixnum");
|
|
|
|
vectorsym = symbol("vector");
|
|
|
|
builtinsym = symbol("builtin");
|
|
|
|
booleansym = symbol("boolean");
|
|
|
|
nullsym = symbol("null");
|
|
|
|
definesym = symbol("define");
|
|
|
|
defmacrosym = symbol("define-macro");
|
2010-12-23 01:49:37 -05:00
|
|
|
forsym = symbol("for");
|
2019-08-09 07:02:02 -04:00
|
|
|
setqsym = symbol("set!");
|
|
|
|
evalsym = symbol("eval");
|
|
|
|
vu8sym = symbol("vu8");
|
|
|
|
fnsym = symbol("fn");
|
|
|
|
nulsym = symbol("nul");
|
|
|
|
alarmsym = symbol("alarm");
|
|
|
|
backspacesym = symbol("backspace");
|
|
|
|
tabsym = symbol("tab");
|
|
|
|
linefeedsym = symbol("linefeed");
|
|
|
|
vtabsym = symbol("vtab");
|
|
|
|
pagesym = symbol("page");
|
|
|
|
returnsym = symbol("return");
|
|
|
|
escsym = symbol("esc");
|
|
|
|
spacesym = symbol("space");
|
|
|
|
deletesym = symbol("delete");
|
|
|
|
newlinesym = symbol("newline");
|
|
|
|
tsym = symbol("t");
|
|
|
|
Tsym = symbol("T");
|
|
|
|
fsym = symbol("f");
|
|
|
|
Fsym = symbol("F");
|
|
|
|
set(printprettysym = symbol("*print-pretty*"), FL_T);
|
|
|
|
set(printreadablysym = symbol("*print-readably*"), FL_T);
|
|
|
|
set(printwidthsym = symbol("*print-width*"), fixnum(SCR_WIDTH));
|
|
|
|
set(printlengthsym = symbol("*print-length*"), FL_F);
|
|
|
|
set(printlevelsym = symbol("*print-level*"), FL_F);
|
2010-05-05 01:31:46 -04:00
|
|
|
builtins_table_sym = symbol("*builtins*");
|
2010-05-02 14:17:47 -04:00
|
|
|
fl_lasterror = NIL;
|
2008-06-30 21:54:22 -04:00
|
|
|
i = 0;
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = OP_EQ; i <= OP_ASET; i++) {
|
2008-06-30 21:54:22 -04:00
|
|
|
setc(symbol(builtin_names[i]), builtin(i));
|
|
|
|
}
|
2009-04-24 16:43:56 -04:00
|
|
|
setc(symbol("eq"), builtin(OP_EQ));
|
|
|
|
setc(symbol("procedure?"), builtin(OP_FUNCTIONP));
|
2009-08-23 01:07:46 -04:00
|
|
|
setc(symbol("top-level-bound?"), builtin(OP_BOUNDP));
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 17:35:16 -04:00
|
|
|
set(symbol("*os-name*"), symbol(env_get_os_name()));
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2009-06-29 23:21:41 -04:00
|
|
|
the_empty_vector = tagptr(alloc_words(1), TAG_VECTOR);
|
|
|
|
vector_setsize(the_empty_vector, 0);
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
cvalues_init();
|
2008-09-10 22:37:38 -04:00
|
|
|
|
2019-08-18 18:14:09 -04:00
|
|
|
exename = get_exename(buf, sizeof(buf));
|
2008-09-10 22:37:38 -04:00
|
|
|
if (exename != NULL) {
|
|
|
|
path_to_dirname(exename);
|
2010-05-02 14:17:47 -04:00
|
|
|
setc(symbol("*install-dir*"), cvalue_static_cstring(strdup(exename)));
|
2008-09-10 22:37:38 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
memory_exception_value =
|
|
|
|
fl_list2(MemoryError, cvalue_static_cstring("out of memory"));
|
2009-02-20 00:11:05 -05:00
|
|
|
|
2009-04-19 18:22:17 -04:00
|
|
|
assign_global_builtins(core_builtin_info);
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
builtins_init();
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
// top level
|
|
|
|
// ------------------------------------------------------------------
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2010-04-29 14:01:26 -04:00
|
|
|
value_t fl_toplevel_eval(value_t expr)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2010-04-29 14:01:26 -04:00
|
|
|
return fl_applyn(1, symbol_value(evalsym), expr);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 13:37:42 -04:00
|
|
|
void fl_init(size_t initial_heapsize) { lisp_init(initial_heapsize); }
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-13 16:07:44 -04:00
|
|
|
int fl_load_boot_image(void)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-13 16:07:44 -04:00
|
|
|
value_t e, f;
|
2009-04-20 20:56:05 -04:00
|
|
|
int saveSP;
|
2019-08-13 16:07:44 -04:00
|
|
|
struct ios *s;
|
2019-08-09 12:30:15 -04:00
|
|
|
struct symbol *sym;
|
2009-02-18 22:31:40 -05:00
|
|
|
|
2019-08-13 16:07:44 -04:00
|
|
|
f = cvalue(iostreamtype, sizeof(struct ios));
|
|
|
|
s = value2c(struct ios *, f);
|
|
|
|
ios_static_buffer(s, boot_image, sizeof(boot_image));
|
|
|
|
PUSH(f);
|
2010-05-02 14:17:47 -04:00
|
|
|
saveSP = SP;
|
2019-08-09 07:02:02 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
FL_TRY
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
e = fl_read_sexpr(Stack[SP - 1]);
|
|
|
|
if (ios_eof(value2c(struct ios *, Stack[SP - 1])))
|
|
|
|
break;
|
|
|
|
if (isfunction(e)) {
|
|
|
|
// stage 0 format: series of thunks
|
|
|
|
PUSH(e);
|
|
|
|
(void)_applyn(0);
|
|
|
|
SP = saveSP;
|
|
|
|
} else {
|
|
|
|
// stage 1 format: list alternating symbol/value
|
|
|
|
while (iscons(e)) {
|
|
|
|
sym = tosymbol(car_(e), "bootstrap");
|
|
|
|
e = cdr_(e);
|
|
|
|
(void)tocons(e, "bootstrap");
|
|
|
|
sym->binding = car_(e);
|
|
|
|
e = cdr_(e);
|
|
|
|
}
|
|
|
|
break;
|
2009-07-16 21:30:26 -04:00
|
|
|
}
|
2009-04-20 20:56:05 -04:00
|
|
|
}
|
2009-02-20 00:11:05 -05:00
|
|
|
}
|
2019-08-18 18:14:09 -04:00
|
|
|
FL_CATCH
|
|
|
|
{
|
|
|
|
ios_puts("fatal error during bootstrap:\n", ios_stderr);
|
|
|
|
fl_print(ios_stderr, fl_lasterror);
|
|
|
|
ios_putc('\n', ios_stderr);
|
|
|
|
return 1;
|
|
|
|
}
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
2019-08-09 12:26:20 -04:00
|
|
|
ios_close(value2c(struct ios *, Stack[SP - 1]));
|
2010-05-02 14:17:47 -04:00
|
|
|
POPN(1);
|
2008-06-30 21:54:22 -04:00
|
|
|
return 0;
|
|
|
|
}
|