2003-08-30 12:47:54 -04:00
|
|
|
/* object.h: The Scheme object representation, and a few other important
|
2003-08-19 15:19:38 -04:00
|
|
|
* data types.
|
2003-08-30 12:47:54 -04:00
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright 1990, 1991, 1992, 1993, 1994, 1995, Oliver Laumann, Berlin
|
2009-12-19 07:28:26 -05:00
|
|
|
* Copyright 2002, 2003 Sam Hocevar <sam@hocevar.net>, Paris
|
2003-08-30 12:47:54 -04:00
|
|
|
*
|
|
|
|
* This software was derived from Elk 1.2, which was Copyright 1987, 1988,
|
|
|
|
* 1989, Nixdorf Computer AG and TELES GmbH, Berlin (Elk 1.2 has been written
|
|
|
|
* by Oliver Laumann for TELES Telematic Services, Berlin, in a joint project
|
|
|
|
* between TELES and Nixdorf Microprocessor Engineering, Berlin).
|
|
|
|
*
|
|
|
|
* Oliver Laumann, TELES GmbH, Nixdorf Computer AG and Sam Hocevar, as co-
|
|
|
|
* owners or individual owners of copyright in this software, grant to any
|
|
|
|
* person or company a worldwide, royalty free, license to
|
|
|
|
*
|
|
|
|
* i) copy this software,
|
|
|
|
* ii) prepare derivative works based on this software,
|
|
|
|
* iii) distribute copies of this software or derivative works,
|
|
|
|
* iv) perform this software, or
|
|
|
|
* v) display this software,
|
|
|
|
*
|
|
|
|
* provided that this notice is not removed and that neither Oliver Laumann
|
|
|
|
* nor Teles nor Nixdorf are deemed to have made any representations as to
|
|
|
|
* the suitability of this software for any purpose nor are held responsible
|
|
|
|
* for any defects of this software.
|
|
|
|
*
|
|
|
|
* THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
|
2003-08-19 15:19:38 -04:00
|
|
|
*/
|
|
|
|
|
2003-09-09 11:18:55 -04:00
|
|
|
#include <stdio.h>
|
2003-09-06 10:46:24 -04:00
|
|
|
#include <stdlib.h>
|
2003-08-19 15:25:35 -04:00
|
|
|
|
2003-08-19 15:19:38 -04:00
|
|
|
typedef struct {
|
2003-08-19 15:25:35 -04:00
|
|
|
int64_t data;
|
2003-08-19 15:19:38 -04:00
|
|
|
int tag;
|
|
|
|
} Object;
|
|
|
|
|
2003-08-25 11:01:22 -04:00
|
|
|
#define FIXBITS (8 * (int)sizeof(int))
|
2006-03-02 15:54:22 -05:00
|
|
|
#define SIGNBIT ((unsigned int)1 << (FIXBITS-1))
|
2003-08-19 15:19:38 -04:00
|
|
|
#define CONSTBIT 1
|
2003-08-25 11:01:22 -04:00
|
|
|
#define TYPEBITS (8 * (int)sizeof(int) - 1)
|
2003-08-19 15:19:38 -04:00
|
|
|
#define MAX_TYPE ((1 << TYPEBITS) - 1)
|
|
|
|
|
|
|
|
#define UFIXNUM_FITS(i) (((i) & SIGNBIT) == 0)
|
|
|
|
#define FIXNUM_FITS(i) 1
|
|
|
|
|
|
|
|
#define TYPE(x) ((x).tag >> 1)
|
|
|
|
|
|
|
|
#define FIXNUM(x) ((int)(x).data)
|
|
|
|
#define CHAR(x) ((int)(x).data)
|
|
|
|
|
2006-03-02 15:54:22 -05:00
|
|
|
#define POINTER(x) ((void *)(uintptr_t)(x).data)
|
|
|
|
#define SETPOINTER(x,p) ((x).data = (uintptr_t)(void *)(p))
|
2003-08-19 15:25:35 -04:00
|
|
|
#define SET(x,t,p) ((x).tag = (int)t << 1, (x).data = (p))
|
2003-08-19 15:19:38 -04:00
|
|
|
|
|
|
|
#define ISCONST(x) ((x).tag & CONSTBIT)
|
|
|
|
#define SETCONST(x) ((x).tag |= CONSTBIT)
|
|
|
|
|
|
|
|
#define EQ(x,y) ((x).data == (y).data && (x).tag == (y).tag)
|
|
|
|
|
|
|
|
/* GC related macros:
|
|
|
|
*/
|
|
|
|
#define WAS_FORWARDED(obj) (TYPE(*(Object *)POINTER(obj)) == T_Broken_Heart)
|
|
|
|
#define UPDATE_OBJ(obj) SETPOINTER(obj, POINTER(*(Object *)POINTER(obj)))
|
|
|
|
|
|
|
|
#ifdef GENERATIONAL_GC
|
|
|
|
|
2003-08-19 15:25:03 -04:00
|
|
|
typedef int gcspace_t; /* type for space and type arrays */
|
2006-03-02 15:54:22 -05:00
|
|
|
typedef uintptr_t gcptr_t; /* type for pointers */
|
|
|
|
typedef uintptr_t pageno_t; /* type for page numbers */
|
|
|
|
typedef uintptr_t addrarith_t; /* type for address arithmetic */
|
2003-08-19 15:19:38 -04:00
|
|
|
|
|
|
|
extern gcspace_t *space;
|
|
|
|
extern gcspace_t current_space;
|
|
|
|
C_LINKAGE_BEGIN
|
2004-08-09 16:05:25 -04:00
|
|
|
extern int Visit (Object*); /* required for REVIVE_OBJ below */
|
2003-08-19 15:19:38 -04:00
|
|
|
C_LINKAGE_END
|
|
|
|
|
|
|
|
# ifdef ARRAY_BROKEN
|
|
|
|
extern pageno_t pagebase;
|
|
|
|
# else
|
|
|
|
# define pagebase ((pageno_t)0)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# define PAGEBYTES 512
|
|
|
|
# define PAGE_TO_OBJ(p) ((Object *) (((p) + pagebase) * PAGEBYTES))
|
|
|
|
# define OBJ_TO_PAGE(p) ((((gcptr_t)POINTER(p)) / PAGEBYTES) - pagebase)
|
|
|
|
# define STABLE(x) ((~space[(x)]) & 1)
|
|
|
|
# define MAKEOBJ(o,t,p) (SET(o, t, p))
|
|
|
|
# define IS_ALIVE(obj) ((WAS_FORWARDED(obj)) || \
|
|
|
|
(STABLE(OBJ_TO_PAGE(obj))) || \
|
2003-09-02 04:12:11 -04:00
|
|
|
(space[OBJ_TO_PAGE(obj)] == current_space))
|
2003-08-19 15:19:38 -04:00
|
|
|
# define REVIVE_OBJ(obj) Visit (&obj);
|
|
|
|
#else
|
2003-08-19 15:24:23 -04:00
|
|
|
C_LINKAGE_BEGIN
|
2004-08-09 16:05:25 -04:00
|
|
|
extern int Visit (Object*); /* required in heap.c */
|
2003-08-19 15:24:23 -04:00
|
|
|
C_LINKAGE_END
|
2003-08-19 15:19:38 -04:00
|
|
|
# define IS_ALIVE(obj) WAS_FORWARDED(obj)
|
|
|
|
# define REVIVE_OBJ(obj)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Fixed types. Cannot use enum, because the set of types is extensible:
|
|
|
|
*/
|
|
|
|
#define T_Fixnum 0 /* Must be 0 */
|
|
|
|
#define T_Bignum 1
|
|
|
|
#define T_Flonum 2
|
|
|
|
#define T_Null 3 /* empty list */
|
|
|
|
#define T_Boolean 4 /* #t (1) and #f (0) */
|
|
|
|
#define T_Unbound 5 /* only used internally */
|
|
|
|
#define T_Special 6 /* only used internally */
|
|
|
|
#define T_Character 7
|
|
|
|
#define T_Symbol 8
|
|
|
|
#define T_Pair 9
|
|
|
|
#define T_Environment 10 /* A pair */
|
|
|
|
#define T_String 11
|
|
|
|
#define T_Vector 12
|
|
|
|
#define T_Primitive 13 /* Primitive procedure */
|
|
|
|
#define T_Compound 14 /* Compound procedure */
|
|
|
|
#define T_Control_Point 15
|
|
|
|
#define T_Promise 16 /* Result of (delay expression) */
|
|
|
|
#define T_Port 17
|
|
|
|
#define T_End_Of_File 18
|
2003-09-22 18:46:24 -04:00
|
|
|
#define T_Unspecified 19 /* only used internally */
|
|
|
|
#define T_Autoload 20
|
|
|
|
#define T_Macro 21
|
|
|
|
#define T_Broken_Heart 22 /* only used internally */
|
2003-08-19 15:19:38 -04:00
|
|
|
#ifdef GENERATIONAL_GC
|
2003-09-22 18:46:24 -04:00
|
|
|
# define T_Align_8Byte 23 /* only used internally */
|
|
|
|
# define T_Freespace 24 /* only used internally */
|
2003-08-19 15:19:38 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define BIGNUM(x) ((struct S_Bignum *)POINTER(x))
|
|
|
|
#define FLONUM(x) ((struct S_Flonum *)POINTER(x))
|
|
|
|
#define STRING(x) ((struct S_String *)POINTER(x))
|
|
|
|
#define VECTOR(x) ((struct S_Vector *)POINTER(x))
|
|
|
|
#define SYMBOL(x) ((struct S_Symbol *)POINTER(x))
|
|
|
|
#define PAIR(x) ((struct S_Pair *)POINTER(x))
|
|
|
|
#define PRIM(x) ((struct S_Primitive *)POINTER(x))
|
|
|
|
#define COMPOUND(x) ((struct S_Compound *)POINTER(x))
|
|
|
|
#define CONTROL(x) ((struct S_Control *)POINTER(x))
|
|
|
|
#define PROMISE(x) ((struct S_Promise *)POINTER(x))
|
|
|
|
#define PORT(x) ((struct S_Port *)POINTER(x))
|
|
|
|
#define AUTOLOAD(x) ((struct S_Autoload *)POINTER(x))
|
|
|
|
#define MACRO(x) ((struct S_Macro *)POINTER(x))
|
|
|
|
|
2003-09-24 13:42:55 -04:00
|
|
|
typedef uint16_t gran_t; /* Granularity of bignums */
|
2003-08-19 15:19:38 -04:00
|
|
|
|
|
|
|
struct S_Bignum {
|
|
|
|
Object minusp;
|
2003-09-24 13:42:55 -04:00
|
|
|
unsigned int size; /* Number of uint16_t allocated */
|
|
|
|
unsigned int usize; /* Number of uint16_t actually used */
|
2003-09-02 04:12:11 -04:00
|
|
|
gran_t data[1]; /* Data, lsw first */
|
2003-08-19 15:19:38 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct S_Flonum {
|
|
|
|
Object tag; /* Each S_Foo must start with an Object */
|
|
|
|
double val;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct S_Symbol {
|
|
|
|
Object value;
|
|
|
|
Object next;
|
|
|
|
Object name; /* A string */
|
|
|
|
Object plist;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct S_Pair {
|
|
|
|
Object car, cdr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct S_String {
|
|
|
|
Object tag;
|
2003-09-15 08:53:36 -04:00
|
|
|
unsigned int size;
|
2003-08-19 15:19:38 -04:00
|
|
|
char data[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct S_Vector {
|
|
|
|
Object tag;
|
2003-09-15 08:53:36 -04:00
|
|
|
unsigned int size;
|
2003-08-19 15:19:38 -04:00
|
|
|
Object data[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
enum discipline { EVAL, NOEVAL, VARARGS };
|
|
|
|
struct S_Primitive {
|
|
|
|
Object tag;
|
2004-08-09 16:05:25 -04:00
|
|
|
Object (*fun) ();
|
2003-08-19 15:19:38 -04:00
|
|
|
const char *name;
|
|
|
|
int minargs;
|
|
|
|
int maxargs; /* Or MANY */
|
|
|
|
enum discipline disc;
|
|
|
|
};
|
|
|
|
#define MANY 100
|
|
|
|
|
|
|
|
struct S_Compound {
|
|
|
|
Object closure; /* (lambda (args) form ...) */
|
|
|
|
Object env; /* Procedure's environment */
|
|
|
|
int min_args, max_args;
|
|
|
|
int numforms;
|
|
|
|
Object name;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct wind {
|
|
|
|
struct wind *next, *prev;
|
|
|
|
Object inout; /* Pair of thunks */
|
|
|
|
} WIND;
|
|
|
|
|
|
|
|
typedef struct funct {
|
|
|
|
struct funct *next;
|
|
|
|
char *name;
|
2004-08-09 16:05:25 -04:00
|
|
|
void (*func) (void);
|
2003-08-19 15:19:38 -04:00
|
|
|
} FUNCT;
|
|
|
|
|
|
|
|
typedef struct gcnode {
|
|
|
|
struct gcnode *next;
|
|
|
|
int gclen;
|
|
|
|
Object *gcobj;
|
|
|
|
} GCNODE;
|
|
|
|
|
|
|
|
typedef struct mem_node {
|
|
|
|
struct mem_node *next;
|
2003-08-19 15:25:03 -04:00
|
|
|
unsigned int len;
|
|
|
|
unsigned long int refcnt;
|
2003-08-19 15:19:38 -04:00
|
|
|
} MEM_NODE;
|
|
|
|
|
|
|
|
#if defined(vax) || defined(__vax__)
|
|
|
|
typedef int jmp_buf[17];
|
|
|
|
#else
|
|
|
|
# include <setjmp.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct S_Control {
|
|
|
|
Object env;
|
|
|
|
GCNODE *gclist;
|
|
|
|
MEM_NODE *memlist;
|
|
|
|
Object memsave; /* string */
|
|
|
|
Object gcsave; /* vector */
|
|
|
|
WIND *firstwind, *lastwind;
|
|
|
|
int tailcall;
|
2003-09-06 11:30:43 -04:00
|
|
|
intptr_t delta;
|
2003-08-19 15:19:38 -04:00
|
|
|
#ifdef GENERATIONAL_GC
|
|
|
|
int reloc;
|
|
|
|
#endif
|
|
|
|
jmp_buf j;
|
2003-09-15 08:53:36 -04:00
|
|
|
unsigned int size;
|
2003-08-19 15:25:03 -04:00
|
|
|
unsigned long int intrlevel;
|
2003-08-19 15:19:38 -04:00
|
|
|
char stack[1]; /* must be word aligned */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct S_Promise {
|
|
|
|
Object env;
|
|
|
|
Object thunk;
|
|
|
|
int done;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct S_Port {
|
|
|
|
Object name; /* string */
|
2003-09-25 09:28:07 -04:00
|
|
|
uint16_t flags;
|
2003-08-19 15:19:38 -04:00
|
|
|
char unread;
|
2003-09-15 08:53:36 -04:00
|
|
|
unsigned int ptr;
|
2003-08-19 15:19:38 -04:00
|
|
|
FILE *file;
|
2003-08-19 15:25:03 -04:00
|
|
|
unsigned int lno;
|
2004-08-09 16:05:25 -04:00
|
|
|
int (*closefun) (FILE*);
|
2003-08-19 15:19:38 -04:00
|
|
|
};
|
|
|
|
#define P_OPEN 1 /* flags */
|
|
|
|
#define P_INPUT 2
|
|
|
|
#define P_STRING 4
|
|
|
|
#define P_UNREAD 8
|
|
|
|
#define P_BIDIR 16
|
|
|
|
|
|
|
|
#define IS_INPUT(port) (PORT(port)->flags & (P_INPUT|P_BIDIR))
|
|
|
|
#define IS_OUTPUT(port) ((PORT(port)->flags & (P_INPUT|P_BIDIR)) != P_INPUT)
|
|
|
|
|
|
|
|
struct S_Autoload {
|
|
|
|
Object files;
|
|
|
|
Object env;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct S_Macro {
|
|
|
|
Object body;
|
|
|
|
int min_args, max_args;
|
|
|
|
Object name;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* "size" is called with one object and returns the size of the object.
|
|
|
|
* If "size" is NOFUNC, then "const_size" is taken instead.
|
|
|
|
* "eqv" and "equal" are called with two objects and return 0 or 1.
|
|
|
|
* NOFUNC may be passed instead (then eqv and equal always return #f).
|
|
|
|
* "print" is called with an object, a port, a flag indicating whether
|
|
|
|
* the object is to be printed "raw" (a la display), the print-depth,
|
|
|
|
* and the print-length.
|
|
|
|
* "visit" is called with a pointer to an object and a function.
|
|
|
|
* For each component of the object, the function must be called with
|
|
|
|
* a pointer to the component. NOFUNC may be supplied.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
int haspointer;
|
|
|
|
const char *name;
|
2004-08-09 16:05:25 -04:00
|
|
|
int (*size) (Object);
|
2003-08-19 15:19:38 -04:00
|
|
|
int const_size;
|
2004-08-09 16:05:25 -04:00
|
|
|
int (*eqv) (Object, Object);
|
|
|
|
int (*equal) (Object, Object);
|
|
|
|
int (*print) (Object, Object, int, int, int);
|
|
|
|
int (*visit) (Object*, int (*)(Object*));
|
2003-08-19 15:19:38 -04:00
|
|
|
} TYPEDESCR;
|
|
|
|
|
|
|
|
#ifdef ELK_USE_PROTOTYPES
|
|
|
|
# define NOFUNC 0
|
|
|
|
#else
|
|
|
|
# define NOFUNC ((int (*)())0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct sym {
|
|
|
|
struct sym *next;
|
|
|
|
char *name;
|
2003-08-19 15:25:03 -04:00
|
|
|
unsigned long int value;
|
2003-08-19 15:19:38 -04:00
|
|
|
} SYM;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
SYM *first;
|
|
|
|
char *strings;
|
|
|
|
} SYMTAB;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char *name;
|
|
|
|
int type;
|
|
|
|
} SYMPREFIX;
|
|
|
|
|
|
|
|
#define PR_EXTENSION 0 /* Elk extension initializers/finalizers */
|
|
|
|
#define PR_CONSTRUCTOR 1 /* C++ static constructors/destructors */
|
|
|
|
|
|
|
|
|
|
|
|
/* PFO, GENERIC, and MATCHFUN exist for backwards compatibility
|
|
|
|
*/
|
2004-08-09 16:05:25 -04:00
|
|
|
typedef Object (*PFO) (Object);
|
|
|
|
typedef int (*MATCHFUN) ();
|
2003-08-19 15:19:38 -04:00
|
|
|
#define GENERIC char*
|
|
|
|
|
|
|
|
typedef struct weak_node {
|
|
|
|
struct weak_node *next;
|
|
|
|
Object obj;
|
|
|
|
PFO term;
|
|
|
|
GENERIC group;
|
|
|
|
char flags;
|
|
|
|
} WEAK_NODE;
|
|
|
|
|
|
|
|
/* flags */
|
|
|
|
#define WK_LEADER 1
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char *name;
|
2003-08-19 15:25:03 -04:00
|
|
|
unsigned long int val;
|
2003-08-19 15:19:38 -04:00
|
|
|
} SYMDESCR;
|
|
|
|
|
|
|
|
|
|
|
|
/* Function that can be registered as a reader by Define_Reader():
|
|
|
|
*/
|
2004-08-09 16:05:25 -04:00
|
|
|
typedef Object (*READFUN) (Object, int, int);
|