![]() |
femtolisp: its sources and their patches | ![]() |
src/Scheme/Femtolisp/femtolisp/
holds femtolisp at its commit ec76010, under its BSD license, with the local patches applied. Only the sources, the boot files and its two main test files are kept (the tests run in a clone, see below).
flisp.c
(the values, the collector, the bytecode interpreter apply_cl; it includes
cvalues.c
, which includes
operators.c
,
types.c
,
print.c
,
read.c
and
equal.c
),
builtins.c
,
string.c
,
table.c
,
equalhash.c
,
iostream.c
. About 8800 lines with the headers.
By the same author, with three files of others (
lookup3.c
,
mt19937ar.c
,
wcwidth.c
): the streams (ios), UTF-8, hash tables keyed by pointers, the hash functions, bit vectors, random numbers, time, paths. About 5800 lines.
socket.c
and
bitvector-ops.c
are compiled and not used.
system.lsp (the standard library and the macro expander) and compiler.lsp (the compiler to bytecode), about 1900 lines, and the boot image made from them, flisp.boot: their compiled code, as text.
fl_core.c
and
fl_llt.c
include these sources, as two compilation units. They are compiled with the directory of llt on the include path of these two files only: llt has headers with common names (
utils.h
…).
Each change of femtolisp is a patch of patches/, numbered in the order they apply. The hooks and the flags do nothing unless TeXmacs sets them; the syntax of the reader and of the printer, and a few forms (curried definitions, < and = with any number of arguments, the unspecified value), are changed for good. The tests of femtolisp, adapted to the new syntax of vectors by patch 0015, are run after a change (make test below).
0002: hooks for the roots of the collector and for the equality and the hash of opaque values. 0003: resolve-global, the hook on the global names of the expanded and compiled code (the modules). 0009: the functions of system.lsp and compiler.lsp call each other through private names. 0010: compile-unknown-call. 0011: *keep-source*, the compiled functions keep their source. 0012: the heap stops growing with an error, at 8 GB since 0023, which makes its size a 64-bit number. 0013: *defer-macro-errors*. 0022: a function with its source hashes as its source.
0004: | and \ in symbols, #{…}#, long tokens, integers too large read as inexact, floating point numbers written in the shortest form. 0005: vectors written #(…); *print-shared*: with #f, labels only for cycles. 0006: strings written as strings of bytes. 0014: a datum at the very end of the input is read. 0015: [ and ] are characters of symbols (they wrote vectors). 0018: *print-closures*: with #f, a closure is written #<procedure name>. 0019: the unspecified value is the symbol #<unspecified>, which evaluates to itself.
0007: a builtin called with a wrong number of arguments is an error when the code runs, not when it is compiled. 0008: curried definitions, (define ((f a) b) …). 0016: a local variable does not hide a special form at the head of a form (a parameter named begin captured the begin which cond expands to); since 0038, except return, which a local variable hides (TeXmacs names callbacks return). 0017: < and = with any number of arguments. 0020: *arith-fallback*. 0026: <= and >= are false when a number is a NaN.
0001: llt builds on any architecture (arm64, WebAssembly). 0021: the reader makes a vector without a label once its elements are read (growing it called the collector at each step). 0025: the handles of the collector grow (a recursion through map failed past 500 levels), and a call from C near the end of the stack of C is a stack overflow error, not a crash. 0028: a hash table no longer inserts a key twice after a deletion (get gave wrong values, and the traversals listed deleted keys). 0029: a closure is no longer written past the end of the heap. 0030: no table loses its finalizer. 0031: undefined behaviour (names of symbols, operands of the bytecode, fixnums, keywords). 0037: the collector copies the lists nested deep in their cars without a recursion of C past 1000 levels (a crash past 150000 levels, or 45000 during an export). 0039: the same for every object (vectors, closures, the tails of improper lists, hash tables), and a hash table which contains itself is copied once (it was copied without end, a crash).
0024: the jumps of the errors save no signal mask on macOS. 0026: >, <=, >= of two variables or constants are compiled as the instructions <, =, number?. 0027: equal? of long lists without hashing their pairs. 0032: for-each is a builtin. 0033–0036: flat closures, which keep the values of the variables they use (the variables captured and assigned are boxed), and a let compiled in the frame of its function, without a closure.
Patches 0028–0031 and 0033–0036 come from the femtolisp of Julia (src/flisp of JuliaLang/julia), with the hashes of its commits in their messages.
A change of a C file is compiled with TeXmacs. Make the patch too, so that patches/ still gives the sources: in a clone of femtolisp with the patches applied, commit the change and run git format-patch.
A change of system.lsp or compiler.lsp needs a new boot image, which a standalone femtolisp makes, compiling the compiler with itself until the image no longer changes:
git clone https://github.com/JeffBezanson/femtolisp && cd femtolisp
git checkout ec76010 && git am <tree>/src/Scheme/Femtolisp/patches/*.patch
# change system.lsp or compiler.lsp here, commit, git format-patch
make -C llt && make release
./flisp mkboot0.lsp system.lsp compiler.lsp > flisp.boot.new
mv flisp.boot.new flisp.boot
./flisp mkboot1.lsp # again, until flisp.boot no longer changes
make test
cp system.lsp compiler.lsp flisp.boot <tree>/src/Scheme/Femtolisp/femtolisp/
<tree>/src/Scheme/Femtolisp/make-boot-header.sh |
Here <tree> is the directory src of TeXmacs, which holds configure. mkboot1.lsp is run until the image no longer changes, two or three times. When the bytecode changes (new instructions, another layout of the frames), the old femtolisp cannot run the new image: keep a copy of the old flisp with its old flisp.boot, and make the first image with it and the new compiler, old/flisp mkboot0.lsp --compiler compiler.lsp system.lsp compiler.lsp. The Makefile of femtolisp does not know which headers a file includes: remove the objects (*.o, llt/*.o) after a change of a header or of
llt/htable.inc
, and in TeXmacs fl_core.o and fl_llt.o.
make-boot-header.sh
writes
fl_boot.h
, the image as a C array. When TeXmacs starts, initialize_scheme computes a checksum of the image and defines it as *fl-boot-id*, which is part of the key of the caches of compiled code: a new compiler makes them invalid by itself.
A new version of femtolisp: apply the patches to it, in order, fix what no longer applies, copy the sources, make the boot image as above. Then check the two places which depend on the insides of femtolisp:
fl_core.c
, which uses its stack and its types (fltm_print_frames reads the frames, of 4 slots since patch 0034) and binds some builtins under the names of R7RS, and the lazy function bodies (%expand-top follows expand, and the stubs depend on the code which the compiler makes for them, see the lazy bodies).