diff --git a/README.md b/README.md index 3d526a1..9859d7e 100644 --- a/README.md +++ b/README.md @@ -107,9 +107,9 @@ This file is loaded at startup, so one can recall previous commands Known bugs: * recall of multiline commands does not work as expected. -* this doesn't have tail call optimization, and each call uses a good deal of - the C stack. A recursion deeper than a few thousand calls stops with the error - "Stack overflow: the recursion is too deep". +* a recursion which is not a tail call (such as `(+ 1 (f (- n 1)))`) uses the C stack: + deeper than about 25000 calls it stops with the error "Stack overflow: the + recursion is too deep". Original README (completed) =============== @@ -283,9 +283,21 @@ is returned (`()` if there is none). `()`. This is the only loop supported by MiniLisp. If you are familiar with Scheme, you might be wondering if you could write a -loop by tail recursion in MiniLisp. The answer is no. Tail calls consume stack -space in MiniLisp, so a loop written as recursion will fail with the "Stack -overflow" error after a few thousand iterations. +loop by tail recursion in MiniLisp. The answer is yes: a call in tail position +does not consume any stack space. This is the case of the last expression of the +body of a function, of `progn`, of the branches of `if`, and of whatever a macro +expands to such an expression. So a loop can be written as a recursion with +an accumulator, and run for as long as needed: + + (defun sum (n acc) + (if (= n 0) + acc + (sum (- n 1) (+ n acc)))) ; tail call + (sum 1000000 0) ; -> 500000500000 + +A recursion which is not a tail call, such as `(+ 1 (f (- n 1)))`, still needs +a stack frame for each level and fails with the "Stack overflow" error after +about 25000 calls. ### Imperative programming diff --git a/src/gc.c b/src/gc.c index ea87de8..9e4fcae 100644 --- a/src/gc.c +++ b/src/gc.c @@ -190,11 +190,14 @@ bool gc_collect(void *root, size_t new_size) { while (scan1 < scan2) { switch (scan1->type) { case TINT: - case TSYMBOL: case TPRIMITIVE: case TSTRING: // Any of the above types does not contain a pointer to a GC-managed object. break; + case TSYMBOL: + if (scan1->bind) + scan1->bind = forward(scan1->bind); + break; case TCELL: scan1->car = forward(scan1->car); scan1->cdr = forward(scan1->cdr); diff --git a/src/gc.h b/src/gc.h index 5d0841a..936ad67 100644 --- a/src/gc.h +++ b/src/gc.h @@ -72,6 +72,15 @@ static inline void** add_root_frame(void **prev_frame, int size, void *frame[]) Obj **var3 = (Obj **)(root_frame + 3); \ Obj **var4 = (Obj **)(root_frame + 4); +#define DEFINE5(prev_frame, var1, var2, var3, var4, var5) \ + void *root_frame[7]; \ + prev_frame = add_root_frame(prev_frame, 5, root_frame); \ + Obj **var1 = (Obj **)(root_frame + 1); \ + Obj **var2 = (Obj **)(root_frame + 2); \ + Obj **var3 = (Obj **)(root_frame + 3); \ + Obj **var4 = (Obj **)(root_frame + 4); \ + Obj **var5 = (Obj **)(root_frame + 5); + void gc_init(void); Obj *alloc(void *root, int type, size_t size); diff --git a/src/minilisp.c b/src/minilisp.c index 37bb72c..fac4771 100644 --- a/src/minilisp.c +++ b/src/minilisp.c @@ -75,8 +75,9 @@ static Obj *cons(void *root, Obj **car, Obj **cdr) { } static Obj *make_symbol(void *root, const char *name) { - Obj *sym = alloc(root, TSYMBOL, strlen(name) + 1); - strcpy(sym->name, name); + Obj *sym = alloc(root, TSYMBOL, sizeof(Obj *) + strlen(name) + 1); + sym->bind = NULL; + strcpy(sym->sym_name, name); sym->line_num = filepos.line_num; return sym; } @@ -241,7 +242,7 @@ static Obj *read_list(void *root) { // but return the existing one. static Obj *intern(void *root, const char *name) { for (Obj *p = Symbols; p != Nil; p = p->cdr) - if (strcmp(name, p->car->name) == 0) + if (strcmp(name, p->car->sym_name) == 0) return p->car; DEFINE1(root, sym); *sym = make_symbol(root, name); @@ -360,7 +361,7 @@ static void print(Obj *obj) { case TINT : printf("%lld", obj->value); break; - case TSYMBOL: fputs(obj->name, stdout); + case TSYMBOL: fputs(obj->sym_name, stdout); break; case TPRIMITIVE: fputs("", stdout); break; @@ -403,8 +404,24 @@ static int length(Obj *list) { static Obj *eval(void *root, Obj **env, Obj **obj); +// The global environment is the frame at the top of the chain of frames, the only one which has no +// parent. Its variables are not in the frame itself, they are stored in the symbols. +static inline bool is_global(Obj *env) { + return env->up == Nil; +} + static void add_variable(void *root, Obj **env, Obj **sym, Obj **val) { DEFINE2(root, vars, tmp); + if (is_global(*env)) { + // Redefining a variable just changes its value: the symbol is the only way to reach it. + if ((*sym)->bind) { + (*sym)->bind->cdr = *val; + } else { + *tmp = cons(root, sym, val); + (*sym)->bind = *tmp; + } + return; + } *vars = (*env)->vars; *tmp = acons(root, sym, val, vars); (*env)->vars = *tmp; @@ -459,40 +476,37 @@ static bool is_list(Obj *obj) { return obj == Nil || obj->type == TCELL; } -static Obj *apply_func(void *root, Obj **env, Obj **fn, Obj **args) { - DEFINE3(root, params, newenv, body); +// Returns the new environment frame in which the body of the function fn is evaluated: the +// parameters are bound to the (already evaluated) arguments. The args slot is modified. +static Obj *bind_args(void *root, Obj **fn, Obj **args) { + DEFINE2(root, params, newenv); *params = (*fn)->params; *newenv = (*fn)->env; - *newenv = push_env(root, newenv, params, args); - *body = (*fn)->body; - return progn(root, newenv, body); + return push_env(root, newenv, params, args); } -// Apply fn with args. -static Obj *apply(void *root, Obj **env, Obj **fn, Obj **args) { - if (!is_list(*args)) - error("argument must be a list", (*args)->line_num); - if ((*fn)->type == TPRIMITIVE) - return (*fn)->fn(root, env, args); - if ((*fn)->type == TFUNCTION) { - DEFINE1(root, eargs); - *eargs = eval_list(root, env, args); - return apply_func(root, env, fn, eargs); - } - error("not supported", (*args)->line_num); - return Nil; //fix warning +// Calls the function or macro fn with the (already evaluated, for a function) arguments, and +// returns the value of its body. This is not a tail call: used for the macros expansion. +static Obj *apply_func(void *root, Obj **env, Obj **fn, Obj **args) { + DEFINE2(root, newenv, body); + *newenv = bind_args(root, fn, args); + *body = (*fn)->body; + return progn(root, newenv, body); } // Searches for a variable by symbol. Returns null if not found. static Obj *find(Obj **env, Obj *sym) { - for (Obj *p = *env; p != Nil; p = p->up) { // search all environments + // Search the local environments first... + Obj *p = *env; + for (; !is_global(p); p = p->up) { for (Obj *cell = p->vars; cell != Nil; cell = cell->cdr) { Obj *bind = cell->car; if (sym == bind->car) return bind; } } - return NULL; + // ...then the global one, which is stored in the symbol. + return sym->bind; } // Expands the given macro application form. @@ -508,10 +522,13 @@ static Obj *macroexpand(void *root, Obj **env, Obj **obj) { return apply_func(root, env, macro, args); } -// Evaluates the S expression. -static Obj *eval(void *root, Obj **env, Obj **obj) { - check_stack((*obj)->line_num); - switch ((*obj)->type) { +// The special forms which are evaluated by eval itself, in order to handle the tail calls. +static Obj *prim_if(void *root, Obj **env, Obj **list); +static Obj *prim_progn(void *root, Obj **env, Obj **list); + +// Evaluates the objects which are not function application forms. Returns NULL for a form. +static inline Obj *eval_simple(Obj **env, Obj *obj) { + switch (obj->type) { case TINT: case TPRIMITIVE: case TFUNCTION: @@ -519,43 +536,109 @@ static Obj *eval(void *root, Obj **env, Obj **obj) { case TSTRING: case TNIL: // Self-evaluating objects - return *obj; + return obj; case TSYMBOL: { // Variable - Obj *bind = find(env, *obj); - if (!bind) { - error("Undefined symbol: %s", (*obj)->line_num, (*obj)->name); - } + Obj *bind = find(env, obj); + if (!bind) + error("Undefined symbol: %s", obj->line_num, obj->sym_name); return bind->cdr; } - case TCELL: { + case TCELL: + return NULL; + default: + error("Bug: eval: Unknown tag type: %d", obj->line_num, obj->type); + } +} + +// Evaluates a function application form, or anything else that comes in tail position. +// +// The tail calls do not consume stack: the expression in tail position of an "if", of a "progn", +// of the body of a function or of the expansion of a macro is not evaluated by a recursive call. +// Instead, it becomes the current expression and the loop goes on. For that, the current +// expression and environment live in slots owned by this function. The slots of the caller must +// not be modified, because the caller keeps using them. +static Obj *eval_form(void *root, Obj **env, Obj **obj) { + DEFINE5(root, cur, cenv, fn, args, tmp); + *cur = *obj; + *cenv = *env; + for (;;) { + Obj *r = eval_simple(cenv, *cur); + if (r) + return r; + // Function application form - DEFINE3(root, fn, expanded, args); - *args = (*obj)->cdr; - if ((*obj)->car->type == TSYMBOL) { + *args = (*cur)->cdr; + if ((*cur)->car->type == TSYMBOL) { // Look the head symbol up only once: it is either a macro to expand or the function // to call. - Obj *bind = find(env, (*obj)->car); + Obj *bind = find(cenv, (*cur)->car); if (!bind) - error("Undefined symbol: %s", (*obj)->car->line_num, (*obj)->car->name); + error("Undefined symbol: %s", (*cur)->car->line_num, (*cur)->car->sym_name); *fn = bind->cdr; - if ((*fn)->type == TMACRO) { - *expanded = apply_func(root, env, fn, args); - return eval(root, env, expanded); - } } else { - *fn = (*obj)->car; - *fn = eval(root, env, fn); + *fn = (*cur)->car; + *fn = eval(root, cenv, fn); + } + + if ((*fn)->type == TMACRO) { + // Expand, then evaluate the expansion in place of the form + *cur = apply_func(root, cenv, fn, args); + continue; } if ((*fn)->type != TPRIMITIVE && (*fn)->type != TFUNCTION) error("The head of a list must be a function", - (*fn)->line_num ? (*fn)->line_num : (*obj)->line_num); - return apply(root, env, fn, args); - } - default: - error("Bug: eval: Unknown tag type: %d", (*obj)->line_num, (*obj)->type); + (*fn)->line_num ? (*fn)->line_num : (*cur)->line_num); + if (!is_list(*args)) + error("argument must be a list", (*args)->line_num); + + if ((*fn)->type == TFUNCTION) { + // Evaluate the arguments, then the body in a new environment + *tmp = eval_list(root, cenv, args); + *cenv = bind_args(root, fn, tmp); + *args = (*fn)->body; + goto sequence; + } + + if ((*fn)->fn == prim_if) { + // (if cond then else ...) + if (length(*args) < 2) + error("Malformed if", (*cur)->line_num); + *tmp = (*args)->car; + *tmp = eval(root, cenv, tmp); + if (*tmp != Nil) { + *cur = (*args)->cdr->car; + continue; + } + *args = (*args)->cdr->cdr; + goto sequence; + } + if ((*fn)->fn == prim_progn) + goto sequence; + + // The other primitives are not evaluating anything in tail position + return (*fn)->fn(root, cenv, args); + + sequence: + // Evaluates the expressions of the list *args. The last one is in tail position. + if (*args == Nil) + return Nil; + while ((*args)->type == TCELL && (*args)->cdr != Nil) { + *tmp = (*args)->car; + eval(root, cenv, tmp); + *args = (*args)->cdr; + } + if ((*args)->type != TCELL) + error("Improper list of expressions", (*args)->line_num); + *cur = (*args)->car; } - return Nil; // fix warning +} + +// Evaluates the S expression. +static Obj *eval(void *root, Obj **env, Obj **obj) { + check_stack((*obj)->line_num); + Obj *r = eval_simple(env, *obj); + return r ? r : eval_form(root, env, obj); } //====================================================================== @@ -615,7 +698,7 @@ static Obj *prim_setq(void *root, Obj **env, Obj **list) { DEFINE2(root, bind, value); *bind = find(env, (*list)->car); if (!*bind) - error("Unbound variable %s", (*list)->line_num, (*list)->car->name); + error("Unbound variable %s", (*list)->line_num, (*list)->car->sym_name); *value = (*list)->cdr->car; *value = eval(root, env, value); (*bind)->cdr = *value; @@ -1000,7 +1083,7 @@ static Obj *prim_symbol_to_string(void *root, Obj **env, Obj **list) { // The name is copied out of the GC heap before allocating char name[SYMBOL_MAX_LEN + 1]; - strncpy(name, args->car->name, sizeof(name) - 1); + strncpy(name, args->car->sym_name, sizeof(name) - 1); name[sizeof(name) - 1] = '\0'; return make_string(root, name); } diff --git a/src/minilisp.h b/src/minilisp.h index 9cf8621..a2765d3 100644 --- a/src/minilisp.h +++ b/src/minilisp.h @@ -1,6 +1,8 @@ #ifndef _MINILISP_H_ #define _MINILISP_H_ +#include +#include #include #include #include @@ -38,15 +40,20 @@ typedef struct Obj *Primitive(void *root, struct Obj **env, struct Obj **args); // The object type typedef struct Obj { - // The first word of the object represents the type of the object. Any code that handles object - // needs to check its type first, then access the following union members. - int type; + // The header of an object is 8 bytes long, so that a cons cell takes 24 bytes and an integer 16 + // (instead of 32 and 24 with a header made of three ints plus padding). - // The total size of the object, including "type" field, this field, the contents, and the - // padding at the end of the object. - unsigned int size; + // The type of the object. Any code that handles object needs to check its type first, then + // access the following union members. + uint8_t type; - int line_num; // The Lisp line where object was created + // The Lisp line where the object was created. 24 bits are enough for the line numbers of + // the sources a Lisp program is made of; bigger numbers are truncated. + unsigned int line_num : 24; + + // The total size of the object, including the header, the contents, and the padding at the + // end of the object. It is always a multiple of the alignment (8 bytes). + uint32_t size; // Object values. union { @@ -57,8 +64,15 @@ typedef struct Obj { struct Obj *car; struct Obj *cdr; }; - // Symbol + // String char name[1]; + // Symbol: the binding of the symbol in the global environment, i.e. the pair + // (symbol . value), or NULL if the symbol is not defined globally. Keeping it in the + // symbol makes the lookup of global variables and functions independent of their number. + struct { + struct Obj *bind; + char sym_name[]; + }; // Primitive Primitive *fn; // Function or Macro @@ -78,6 +92,9 @@ typedef struct Obj { }; } Obj; +// The header must not grow: the memory consumption of every object depends on it. +typedef char obj_header_must_be_8_bytes[offsetof(Obj, value) == 8 ? 1 : -1]; + typedef struct { char *filename; size_t file_len; diff --git a/test.sh b/test.sh index 5b29847..3d22e82 100755 --- a/test.sh +++ b/test.sh @@ -194,5 +194,18 @@ run macroexpand '(if (= x 0) (print x))' " (macroexpand (if-zero x (print x)))" +# Tail calls must not consume stack: 20000 nested calls would overflow it otherwise +run tailcall 200010000 '(defun sum (n acc) (if (= n 0) acc (sum (- n 1) (+ n acc)))) (sum 20000 0)' +run tailcall 200010000 '(defun sum (n acc) (progn 1 (if (= n 0) acc (progn 2 (sum (- n 1) (+ n acc)))))) (sum 20000 0)' +run tailcall 200010000 ' + (defmacro my-if (c a b) (list (quote if) c a b)) + (defun sum (n acc) (my-if (= n 0) acc (sum (- n 1) (+ n acc)))) + (sum 20000 0)' +run tailcall t ' + (defun even? (n) (if (= n 0) t (odd? (- n 1)))) + (defun odd? (n) (if (= n 0) () (even? (- n 1)))) + (even? 20000)' +run tailcall 0 '(defun f (n) ((lambda (m) (if (= m 0) 0 (f (- m 1)))) n)) (f 20000)' + # Sum from 0 to 10 run recursion 55 '(defun f (x) (if (= x 0) 0 (+ (f (+ x -1)) x))) (f 10)' \ No newline at end of file