The 'dream' Scheme Interpreter

by David Joseph Stith
Welcome to the home of my Scheme interpreter, 'dream', written entirely in assembly language. :-)
All essential syntax and procedures from the R4RS standard are implemented.
The interpreter is properly tail recursive and passes all applicable tests from the 'r4rstest.scm' test suite.
Rational arithmetic with 32 bit numerator and denominator is supported, but no Real or Complex numbers.
Dream is compiled using an x86 assembler I have written in scheme, with a syntax very similar to GAS.
Consequently Dream can compile itself and depends only on the Linux kernel. :-)
Download latest version for Linux on x86: dream.tar.gz
Download old version for Linux on PowerPC: dreamppc1.61.tar.gz

Notes on the Design

The design for the 'dream' Scheme interpreter began with the design given in Abelson and Sussman's Structure and Interpretation of Computer Programs.

Garbage Collection

Two areas of memory of equal size are used for the storage of scheme objects. Both are aligned on an 8-byte boundary. Only one of the two is used at a time by the scheme interpreter; when it becomes full, the garbage collector copies all scheme objects in use to the other memory area (which then becomes the active one). Dynamically allocated scheme objects other than symbols are represented by a discrete number of quad-words which are allocated consecutively within the active memory area.

Scheme Object Types

The simplest object is the scheme pair which consists of two double-words each of which addresses a scheme object.

Symbols and statically allocated objects are not garbage-collected, but they must begin on a 2-byte boundary so that the addresses stored in pairs are always divisible by 2. By virtue of the fact that all scheme objects begin on a 2-byte boundary, scheme objects other than scheme pairs are differentiated from scheme pairs by storing a double-word which is NOT divisible by 2 in the first half of the quad-word. This double-word represents the type of scheme object. The low byte of this type represents the major type classification used by the procedures boolean?, pair?, procedure?, char?, number?, symbol?, string?, vector?, input-port?, and output-port?. Statically allocated objects are given a type which is negative so that the garbage collector can easily ignore them. All 256 ascii chars, #t and #f, and the end-of-file object are statically allocated. Only one double-word is necessary for these statically allocated objects. In the case of chars and booleans, the value is stored in the high byte of the low word.

Symbols are also given a negative type, since they are not garbage collected, but they are dynamically created in a separate memory area devoted to them. Each symbol begins with the double-word type header (on a 2 byte boundary) which is followed by the bytes of ascii code that form the name of the symbol. A null (0) byte marks the end of the symbol name. The address of every symbol is stored in a simple hash keyed on just the first character of the symbol.

Strings, unlike symbols, are stored along with the other dynamically allocated objects, and use the second double-word to store the address of their string of ascii byte codes (ending with a null byte). Furthermore, unlike symbols, the high word of the type field is used to store the length of the string (prior to the terminating null byte.) Another pair of memory areas of equal size is used to store these strings of ascii byte codes. When the active string storage area becomes full, the garbage collector copies in-use string data to the other string storage area (which then becomes the active one). Otherwise the garbage collector leaves these string storage areas untouched.

Vectors are stored as consecutive pairs (but the first half of the first pair is the vector type header.) The high word of the type field is used to store the length of the vector. This and all other objects which require more than a quad-word of storage simply store the address of a scheme object in the second double-word and set the low bit in the high byte of the low word of their type to indicate to the garbage collector that this address in the second double-word must be followed just as if it were the cdr of a pair.

Special forms and built-in procedures store an address to branch to in the second double-word.

Number types are distinguished by the high byte of the low word, which increases as the complexity of the type of number ascends the numeric tower. Integers simply store their 32 bit signed value in the second half of the quad-word. Rationals are stored as a pair of integers, thus the low bit in the high byte of the low word of their type is set so that the garbage collector will see the pair. Inexactness of a number is indicated by setting the lowest bit of the high word of the number's type. Note, however, that all internal representations of numbers are exact (no floating point numbers are used), and so inexactness is given only as an auxiliary property of the number.

Input ports use the low byte of the high word of the type field to store the last character read by the (peek) procedure. The second half of the quad-word for both input and output ports holds the file descriptor associated with the port. The ports returned by (current-input-port) and (current-output-port) are stored internally for efficiency's sake. They therefore must be treated by the garbage collector as if they were registers.

The Stack

The scheme object stack is maintained as a scheme list (dynamically allocated as pairs). The garbage collector, when it runs, begins at the root of this scheme list. Hence when garbage collection commences, only the registers and the current input and output ports need be pushed on to this scheme object stack and popped off afterwards to insure that all reachable objects are retained thoughout the garbage collection process. The native stack (pointed to by the %esp register) is used for the flow of continuation control. Consequently when call-with-current-continuation is invoked, this native stack is copied to a scheme list with each address represented as a special form.

Scheme Registers

The registers denoted by EXP, ENV, UNEV, ARGL, VAL, and FREE in Structure and Interpretation of Computer Programs are implemented by the machine registers EDX, EBP, ESI, EDI, EAX, and EBX respectively. The registers EXP, ENV, UNEV, ARGL, and VAL must point to a valid scheme object (or null) when the garbage collector is invoked. Likewise, the garbage collector registers OLD, NEW, and SCAN are implemented by the machine registers ESI, EDI, and EAX respectively.