Commit 3cfe6dfd authored by Jim Blandy's avatar Jim Blandy
Browse files

Initial revision

parent f7430cb6
/* Fundamental definitions for GNU Emacs Lisp interpreter.
Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
/* Define the fundamental Lisp data structures */
/* This is the set of Lisp data types */
enum Lisp_Type
{
/* Integer. XINT(obj) is the integer value. */
Lisp_Int,
/* Symbol. XSYMBOL (object) points to a struct Lisp_Symbol. */
Lisp_Symbol,
/* Marker (buffer ptr). XMARKER(object) points to a struct Lisp_Marker. */
Lisp_Marker,
/* String. XSTRING (object) points to a struct Lisp_String.
The length of the string, and its contents, are stored therein. */
Lisp_String,
/* Vector of Lisp objects. XVECTOR(object) points to a struct Lisp_Vector.
The length of the vector, and its contents, are stored therein. */
Lisp_Vector,
/* Cons. XCONS (object) points to a struct Lisp_Cons. */
Lisp_Cons,
/* Byte-compiled function. A vector of 4 to 6 elements which are the
arglist, bytecode-string, constant vector, stack size,
(optional) doc string, and (optional) interactive spec. */
Lisp_Compiled,
/* Editor buffer. XBUFFER(obj) points to a struct buffer. */
Lisp_Buffer,
/* Built-in function. XSUBR(obj) points to a struct Lisp_Subr
which describes how to call the function, and its documentation,
as well as pointing to the code. */
Lisp_Subr,
/* Internal value return by subroutines of read.
The user never sees this data type.
Its value is just a number. */
Lisp_Internal,
/* Forwarding pointer to an int variable.
This is allowed only in the value cell of a symbol,
and it means that the symbol's value really lives in the
specified int variable.
XINTPTR(obj) points to the int variable. */
Lisp_Intfwd,
/* Boolean forwarding pointer to an int variable.
This is like Lisp_Intfwd except that the ostensible
"value" of the symbol is t if the int variable is nonzero,
nil if it is zero. XINTPTR(obj) points to the int variable. */
Lisp_Boolfwd,
/* Object describing a connection to a subprocess.
It points to storage of type struct Lisp_Process */
Lisp_Process,
/* Forwarding pointer to a Lisp_Object variable.
This is allowed only in the value cell of a symbol,
and it means that the symbol's value really lives in the
specified variable.
XOBJFWD(obj) points to the Lisp_Object variable. */
Lisp_Objfwd,
/* Pointer to a vector-like object describing a display screen
on which Emacs can display a window hierarchy. */
Lisp_Screen,
/* Used when a FILE * value needs to be passed
in an argument of type Lisp_Object.
You must do *(FILE **) XPNTR(obj) to get the value.
The user will never see this data type. */
Lisp_Internal_Stream,
/* Used in a symbol value cell when the symbol's value is per-buffer.
The actual contents are a cons cell which starts a list like this:
(REALVALUE BUFFER CURRENT-ALIST-ELEMENT . DEFAULT-VALUE)).
BUFFER is the last buffer for which this symbol's value was
made up to date.
CURRENT-ALIST-ELEMENT is a pointer to an element of BUFFER's
b_local_var_alist, that being the element whose car is this variable.
Or it can be a pointer to the (CURRENT-ALIST-ELEMENT . DEFAULT-VALUE), if BUFFER
does not have an element in its alist for this variable
(that is, if BUFFER sees the default value of this variable).
If we want to examine or set the value and BUFFER is current,
we just examine or set REALVALUE.
If BUFFER is not current, we store the current REALVALUE value into
CURRENT-ALIST-ELEMENT, then find the appropriate alist element for
the buffer now current and set up CURRENT-ALIST-ELEMENT.
Then we set REALVALUE out of that element, and store into BUFFER.
If we are setting the variable and the current buffer does not have
an alist entry for this variable, an alist entry is created.
Note that REALVALUE can be a forwarding pointer.
Each time it is examined or set, forwarding must be done. */
Lisp_Buffer_Local_Value,
/* Like Lisp_Buffer_Local_Value with one difference:
merely setting the variable while some buffer is current
does not cause that buffer to have its own local value of this variable.
Only make-local-variable does that. */
Lisp_Some_Buffer_Local_Value,
/* Like Lisp_Objfwd except that value lives in a slot
in the current buffer. Value is byte index of slot within buffer */
Lisp_Buffer_Objfwd,
/* In symbol value cell, means var is unbound.
In symbol function cell, means function name is undefined. */
Lisp_Void,
/* Window used for Emacs display.
Data inside looks like a Lisp_Vector. */
Lisp_Window,
/* Used by save,set,restore-window-configuration */
Lisp_Window_Configuration
#ifdef LISP_FLOAT_TYPE
,
Lisp_Float
#endif /* LISP_FLOAT_TYPE */
};
#ifndef NO_UNION_TYPE
#ifndef BIG_ENDIAN
/* Definition of Lisp_Object for little-endian machines. */
typedef
union Lisp_Object
{
/* Used for comparing two Lisp_Objects;
also, positive integers can be accessed fast this way. */
int i;
struct
{
int val: 24;
char type;
} s;
struct
{
unsigned int val: 24;
char type;
} u;
struct
{
unsigned int val: 24;
enum Lisp_Type type: 7;
/* The markbit is not really part of the value of a Lisp_Object,
and is always zero except during garbage collection. */
unsigned int markbit: 1;
} gu;
}
Lisp_Object;
#else /* If BIG_ENDIAN */
typedef
union Lisp_Object
{
/* Used for comparing two Lisp_Objects;
also, positive integers can be accessed fast this way. */
int i;
struct
{
char type;
int val: 24;
} s;
struct
{
char type;
unsigned int val: 24;
} u;
struct
{
/* The markbit is not really part of the value of a Lisp_Object,
and is always zero except during garbage collection. */
unsigned int markbit: 1;
enum Lisp_Type type: 7;
unsigned int val: 24;
} gu;
}
Lisp_Object;
#endif /* BIG_ENDIAN */
#endif /* NO_UNION_TYPE */
/* If union type is not wanted, define Lisp_Object as just a number
and define the macros below to extract fields by shifting */
#ifdef NO_UNION_TYPE
#define Lisp_Object int
/* These values are overridden by the m- file on some machines. */
#ifndef VALBITS
#define VALBITS 24
#endif
#ifndef GCTYPEBITS
#define GCTYPEBITS 7
#endif
#ifndef VALMASK
#define VALMASK ((1<<VALBITS) - 1)
#endif
#define GCTYPEMASK ((1<<GCTYPEBITS) - 1)
#define MARKBIT (1 << (VALBITS + GCTYPEBITS))
#endif /* NO_UNION_TYPE */
/* These macros extract various sorts of values from a Lisp_Object.
For example, if tem is a Lisp_Object whose type is Lisp_Cons,
XCONS (tem) is the struct Lisp_Cons * pointing to the memory for that cons. */
#ifdef NO_UNION_TYPE
/* One need to override this if there must be high bits set in data space
(doing the result of the below & ((1 << (GCTYPE + 1)) - 1) would work
on all machines, but would penalise machines which don't need it)
*/
#ifndef XTYPE
#define XTYPE(a) ((enum Lisp_Type) ((a) >> VALBITS))
#endif
#ifndef XSETTYPE
#define XSETTYPE(a, b) ((a) = XUINT (a) | ((int)(b) << VALBITS))
#endif
/* Use XFASTINT for fast retrieval and storage of integers known
to be positive. This takes advantage of the fact that Lisp_Int is 0. */
#define XFASTINT(a) (a)
/* Extract the value of a Lisp_Object as a signed integer. */
#ifndef XINT /* Some machines need to do this differently. */
#define XINT(a) (((a) << INTBITS-VALBITS) >> INTBITS-VALBITS)
#endif
/* Extract the value as an unsigned integer. This is a basis
for extracting it as a pointer to a structure in storage. */
#ifndef XUINT
#define XUINT(a) ((a) & VALMASK)
#endif
#ifndef XPNTR
#ifdef HAVE_SHM
/* In this representation, data is found in two widely separated segments. */
#define XPNTR(a) \
(XUINT (a) | (XUINT (a) > PURESIZE ? DATA_SEG_BITS : PURE_SEG_BITS))
#else /* not HAVE_SHM */
#ifdef DATA_SEG_BITS
/* This case is used for the rt-pc.
In the diffs I was given, it checked for ptr = 0
and did not adjust it in that case.
But I don't think that zero should ever be found
in a Lisp object whose data type says it points to something. */
#define XPNTR(a) (XUINT (a) | DATA_SEG_BITS)
#else
#define XPNTR(a) XUINT (a)
#endif
#endif /* not HAVE_SHM */
#endif /* no XPNTR */
#ifndef XSETINT
#define XSETINT(a, b) ((a) = ((a) & ~VALMASK) | ((b) & VALMASK))
#endif
#ifndef XSETUINT
#define XSETUINT(a, b) XSETINT (a, b)
#endif
#ifndef XSETPNTR
#define XSETPNTR(a, b) XSETINT (a, b)
#endif
#ifndef XSET
#define XSET(var, type, ptr) \
((var) = ((int)(type) << VALBITS) + ((int) (ptr) & VALMASK))
#endif
/* During garbage collection, XGCTYPE must be used for extracting types
so that the mark bit is ignored. XMARKBIT accesses the markbit.
Markbits are used only in particular slots of particular structure types.
Other markbits are always zero.
Outside of garbage collection, all mark bits are always zero. */
#ifndef XGCTYPE
#define XGCTYPE(a) ((enum Lisp_Type) (((a) >> VALBITS) & GCTYPEMASK))
#endif
#if VALBITS + GCTYPEBITS == INTBITS - 1
/* Make XMARKBIT faster if mark bit is sign bit. */
#ifndef XMARKBIT
#define XMARKBIT(a) ((a) < 0)
#endif
#endif /* markbit is sign bit */
#ifndef XMARKBIT
#define XMARKBIT(a) ((a) & MARKBIT)
#endif
#ifndef XSETMARKBIT
#define XSETMARKBIT(a,b) ((a) = ((a) & ~MARKBIT) | ((b) ? MARKBIT : 0))
#endif
#ifndef XMARK
#define XMARK(a) ((a) |= MARKBIT)
#endif
#ifndef XUNMARK
#define XUNMARK(a) ((a) &= ~MARKBIT)
#endif
#endif /* NO_UNION_TYPE */
#ifndef NO_UNION_TYPE
#define XTYPE(a) ((enum Lisp_Type) (a).u.type)
#define XSETTYPE(a, b) ((a).u.type = (char) (b))
/* Use XFASTINT for fast retrieval and storage of integers known
to be positive. This takes advantage of the fact that Lisp_Int is 0. */
#define XFASTINT(a) ((a).i)
#ifdef EXPLICIT_SIGN_EXTEND
/* Make sure we sign-extend; compilers have been known to fail to do so. */
#define XINT(a) (((a).i << 8) >> 8)
#else
#define XINT(a) ((a).s.val)
#endif /* EXPLICIT_SIGN_EXTEND */
#define XUINT(a) ((a).u.val)
#define XPNTR(a) ((a).u.val)
#define XSETINT(a, b) ((a).s.val = (int) (b))
#define XSETUINT(a, b) ((a).s.val = (int) (b))
#define XSETPNTR(a, b) ((a).s.val = (int) (b))
#define XSET(var, vartype, ptr) \
(((var).s.type = ((char) (vartype))), ((var).s.val = ((int) (ptr))))
/* During garbage collection, XGCTYPE must be used for extracting types
so that the mark bit is ignored. XMARKBIT access the markbit.
Markbits are used only in particular slots of particular structure types.
Other markbits are always zero.
Outside of garbage collection, all mark bits are always zero. */
#define XGCTYPE(a) ((a).gu.type)
#define XMARKBIT(a) ((a).gu.markbit)
#define XSETMARKBIT(a,b) (XMARKBIT(a) = (b))
#define XMARK(a) (XMARKBIT(a) = 1)
#define XUNMARK(a) (XMARKBIT(a) = 0)
#endif /* NO_UNION_TYPE */
#define XCONS(a) ((struct Lisp_Cons *) XPNTR(a))
#define XBUFFER(a) ((struct buffer *) XPNTR(a))
#define XVECTOR(a) ((struct Lisp_Vector *) XPNTR(a))
#define XSUBR(a) ((struct Lisp_Subr *) XPNTR(a))
#define XSTRING(a) ((struct Lisp_String *) XPNTR(a))
#define XSYMBOL(a) ((struct Lisp_Symbol *) XPNTR(a))
#define XFUNCTION(a) ((Lisp_Object (*)()) XPNTR(a))
#define XMARKER(a) ((struct Lisp_Marker *) XPNTR(a))
#define XOBJFWD(a) ((Lisp_Object *) XPNTR(a))
#define XINTPTR(a) ((int *) XPNTR(a))
#define XWINDOW(a) ((struct window *) XPNTR(a))
#define XPROCESS(a) ((struct Lisp_Process *) XPNTR(a))
#define XFLOAT(a) ((struct Lisp_Float *) XPNTR(a))
#define XSETCONS(a, b) XSETPNTR(a, (int) (b))
#define XSETBUFFER(a, b) XSETPNTR(a, (int) (b))
#define XSETVECTOR(a, b) XSETPNTR(a, (int) (b))
#define XSETSUBR(a, b) XSETPNTR(a, (int) (b))
#define XSETSTRING(a, b) XSETPNTR(a, (int) (b))
#define XSETSYMBOL(a, b) XSETPNTR(a, (int) (b))
#define XSETFUNCTION(a, b) XSETPNTR(a, (int) (b))
#define XSETMARKER(a, b) XSETPNTR(a, (int) (b))
#define XSETOBJFWD(a, b) XSETPNTR(a, (int) (b))
#define XSETINTPTR(a, b) XSETPNTR(a, (int) (b))
#define XSETWINDOW(a, b) XSETPNTR(a, (int) (b))
#define XSETPROCESS(a, b) XSETPNTR(a, (int) (b))
#define XSETFLOAT(a, b) XSETPNTR(a, (int) (b))
/* In a cons, the markbit of the car is the gc mark bit */
struct Lisp_Cons
{
Lisp_Object car, cdr;
};
/* Like a cons, but records info on where the text lives that it was read from */
/* This is not really in use now */
struct Lisp_Buffer_Cons
{
Lisp_Object car, cdr;
struct buffer *buffer;
int bufpos;
};
/* In a string or vector, the sign bit of the `size' is the gc mark bit */
struct Lisp_String
{
int size;
unsigned char data[1];
};
struct Lisp_Vector
{
int size;
struct Lisp_Vector *next;
Lisp_Object contents[1];
};
/* In a symbol, the markbit of the plist is used as the gc mark bit */
struct Lisp_Symbol
{
struct Lisp_String *name;
Lisp_Object value;
Lisp_Object function;
Lisp_Object plist;
struct Lisp_Symbol *next; /* -> next symbol in this obarray bucket */
};
struct Lisp_Subr
{
Lisp_Object (*function) ();
short min_args, max_args;
char *symbol_name;
char *prompt;
char *doc;
};
/* In a marker, the markbit of the chain field is used as the gc mark bit */
struct Lisp_Marker
{
struct buffer *buffer;
Lisp_Object chain;
int bufpos;
int modified;
};
#ifdef LISP_FLOAT_TYPE
/* Optional Lisp floating point type */
struct Lisp_Float
{
Lisp_Object type; /* essentially used for mark-bit
and chaining when on free-list */
double data;
};
#endif /* LISP_FLOAT_TYPE */
/* A character, declared with the following typedef, is a member
of some character set associated with the current buffer. */
typedef unsigned char UCHAR;
/* Meanings of slots in a Lisp_Compiled: */
#define COMPILED_ARGLIST 0
#define COMPILED_BYTECODE 1
#define COMPILED_CONSTANTS 2
#define COMPILED_STACK_DEPTH 3
#define COMPILED_DOC_STRING 4
#define COMPILED_INTERACTIVE 5
/* Data type checking */
#ifdef NULL
#undef NULL
#endif
#define NULL(x) (XFASTINT (x) == XFASTINT (Qnil))
/* #define LISTP(x) (XTYPE ((x)) == Lisp_Cons)*/
#define CONSP(x) (XTYPE ((x)) == Lisp_Cons)
#define EQ(x, y) (XFASTINT (x) == XFASTINT (y))
#define CHECK_LIST(x, i) \
{ if ((XTYPE ((x)) != Lisp_Cons) && !NULL (x)) x = wrong_type_argument (Qlistp, (x)); }
#define CHECK_STRING(x, i) \
{ if (XTYPE ((x)) != Lisp_String) x = wrong_type_argument (Qstringp, (x)); }
#define CHECK_CONS(x, i) \
{ if (XTYPE ((x)) != Lisp_Cons) x = wrong_type_argument (Qconsp, (x)); }
#define CHECK_SYMBOL(x, i) \
{ if (XTYPE ((x)) != Lisp_Symbol) x = wrong_type_argument (Qsymbolp, (x)); }
#define CHECK_VECTOR(x, i) \
{ if (XTYPE ((x)) != Lisp_Vector) x = wrong_type_argument (Qvectorp, (x)); }
#define CHECK_BUFFER(x, i) \
{ if (XTYPE ((x)) != Lisp_Buffer) x = wrong_type_argument (Qbufferp, (x)); }
#define CHECK_WINDOW(x, i) \
{ if (XTYPE ((x)) != Lisp_Window) x = wrong_type_argument (Qwindowp, (x)); }
#define CHECK_PROCESS(x, i) \
{ if (XTYPE ((x)) != Lisp_Process) x = wrong_type_argument (Qprocessp, (x)); }
#define CHECK_NUMBER(x, i) \
{ if (XTYPE ((x)) != Lisp_Int) x = wrong_type_argument (Qintegerp, (x)); }
#define CHECK_NATNUM(x, i) \
{ if (XTYPE ((x)) != Lisp_Int || XINT ((x)) < 0) \
x = wrong_type_argument (Qnatnump, (x)); }
#define CHECK_MARKER(x, i) \
{ if (XTYPE ((x)) != Lisp_Marker) x = wrong_type_argument (Qmarkerp, (x)); }
#define CHECK_NUMBER_COERCE_MARKER(x, i) \
{ if (XTYPE ((x)) == Lisp_Marker) XFASTINT (x) = marker_position (x); \
else if (XTYPE ((x)) != Lisp_Int) x = wrong_type_argument (Qinteger_or_marker_p, (x)); }
#ifdef LISP_FLOAT_TYPE
#ifndef DBL_DIG
#define DBL_DIG 20
#endif
#define XFLOATINT(n) extract_float((n))
#define CHECK_FLOAT(x, i) \
{ if (XTYPE (x) != Lisp_Float) \
x = wrong_type_argument (Qfloatp, (x)); }
#define CHECK_NUMBER_OR_FLOAT(x, i) \
{ if (XTYPE (x) != Lisp_Float && XTYPE (x) != Lisp_Int) \
x = wrong_type_argument (Qinteger_or_floatp, (x)); }
#define CHECK_NUMBER_OR_FLOAT_COERCE_MARKER(x, i) \
{ if (XTYPE (x) == Lisp_Marker) XFASTINT (x) = marker_position (x); \
else if (XTYPE (x) != Lisp_Int && XTYPE (x) != Lisp_Float) \
x = wrong_type_argument (Qinteger_or_float_or_marker_p, (x)); }
#else /* Not LISP_FLOAT_TYPE */
#define CHECK_NUMBER_OR_FLOAT CHECK_NUMBER
#define CHECK_NUMBER_OR_FLOAT_COERCE_MARKER CHECK_NUMBER_COERCE_MARKER
#define XFLOATINT(n) XINT((n))
#endif /* LISP_FLOAT_TYPE */
#ifdef VIRT_ADDR_VARIES
/* For machines like APOLLO where text and data can go anywhere
in virtual memory. */
#define CHECK_IMPURE(obj) \
{ extern int pure[]; \
if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE) \
&& (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure) \
pure_write_error (); }
#else /* not VIRT_ADDR_VARIES */
#ifdef PNTR_COMPARISON_TYPE
/* when PNTR_COMPARISON_TYPE is not the default (unsigned int) */
#define CHECK_IMPURE(obj) \
{ extern int my_edata; \
if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) &my_edata) \
pure_write_error (); }
#else /* not VIRT_ADDRESS_VARIES, not PNTR_COMPARISON_TYPE */
#define CHECK_IMPURE(obj) \
{ extern int my_edata; \
if (XPNTR (obj) < (unsigned int) &my_edata) \
pure_write_error (); }
#endif /* PNTR_COMPARISON_TYPE */
#endif /* VIRT_ADDRESS_VARIES */
/* Cast pointers to this type to compare them. Some machines want int. */
#ifndef PNTR_COMPARISON_TYPE
#define PNTR_COMPARISON_TYPE unsigned int
#endif
/* Define a built-in function for calling from Lisp.
`lname' should be the name to give the function in Lisp,
as a null-terminated C string.
`fnname' should be the name of the function in C.
By convention, it starts with F.
`sname' should be the name for the C constant structure
that records information on this function for internal use.
By convention, it should be the same as `fnname' but with S instead of F.
It's too bad that C macros can't compute this from `fnname'.
`minargs' should be a number, the minimum number of arguments allowed.
`maxargs' should be a number, the maximum number of arguments allowed,
or else MANY or UNEVALLED.
MANY means pass a vector of evaluated arguments,
in the form of an integer number-of-arguments
followed by the address of a vector of Lisp_Objects
which contains the argument values.
UNEVALLED means pass the list of unevaluated arguments
`prompt' says how to read arguments for an interactive call.
This can be zero or a C string.
Zero means that interactive calls are not allowed.
A string is interpreted in a hairy way:
it should contain one line for each argument to be read, terminated by \n.
The first character of the line controls the type of parsing:
s -- read a string.
S -- read a symbol.
k -- read a key sequence and return it as a string.
a -- read a function name (symbol) with completion.
C -- read a command name (symbol) with completion.
v -- read a variable name (symbol) with completion.
b -- read a buffer name (a string) with completion.
B -- buffer name, may be existing buffer or may not be.
f -- read a file name, file must exist.
F -- read a file name, file need not exist.
n -- read a number.
c -- read a character and return it as a number.
p -- use the numeric value of the prefix argument.
P -- use raw value of prefix - can be nil, -, (NUMBER) or NUMBER.
x -- read a Lisp object from the minibuffer.
X -- read a Lisp form from the minibuffer and use its value.
A null string means call interactively with no arguments.
`doc' is documentation for the user.
*/
#define DEFUN(lname, fnname, sname, minargs, maxargs, prompt, doc) \
Lisp_Object fnname (); \
struct Lisp_Subr sname = {fnname, minargs, maxargs, lname, prompt, 0}; \