blob: f74189ded4529c895b7f568986fc1f03dc4e7cfa [file] [log] [blame]
swissChili8cfb7c42021-04-18 21:17:58 -07001/* -*- mode:c -*- */
2
swissChilica107a02021-04-14 12:07:30 -07003#include "compiler.h"
swissChiliddc97542021-07-04 11:47:42 -07004#include "gc.h"
swissChili7e1393c2021-07-07 12:59:12 -07005#include "lib/std.h"
6#include "lisp.h"
7#include "plat/plat.h"
swissChilica107a02021-04-14 12:07:30 -07008
9#include <dasm_proto.h>
10#include <dasm_x86.h>
11
swissChili7e1393c2021-07-07 12:59:12 -070012#include <libgen.h>
13#include <stdio.h>
swissChili923b5362021-05-09 20:31:43 -070014#include <stdlib.h>
15#include <string.h>
16
swissChili53472e82021-05-08 16:06:32 -070017#define value_size sizeof(value_t)
swissChilica107a02021-04-14 12:07:30 -070018
19|.arch x86;
20
21|.macro setup, nvars;
swissChili53e7cd12021-08-02 21:55:53 -070022|1:;
swissChili484295d2021-07-09 21:25:55 -070023| push ebp;
swissChilica107a02021-04-14 12:07:30 -070024| mov ebp, esp;
swissChili8cfb7c42021-04-18 21:17:58 -070025| sub esp, (value_size * nvars);
swissChilica107a02021-04-14 12:07:30 -070026|.endmacro;
27
28|.macro cleanup;
29| mov esp, ebp;
30| pop ebp;
31| ret;
32|.endmacro;
33
swissChili484295d2021-07-09 21:25:55 -070034|.macro call_extern, address;
swissChili53e7cd12021-08-02 21:55:53 -070035| call &address;
swissChili67bdf282021-06-06 18:46:08 -070036|.endmacro;
37
swissChilica107a02021-04-14 12:07:30 -070038dasm_State *d;
39unsigned int npc = 8;
40
swissChili9e57da42021-06-15 22:22:46 -070041|.macro run_gc;
swissChilie9fec8b2021-06-22 13:59:33 -070042| mov eax, esp;
swissChili9e57da42021-06-15 22:22:46 -070043| push ebp;
swissChilie9fec8b2021-06-22 13:59:33 -070044| push eax;
swissChili9e57da42021-06-15 22:22:46 -070045| mov eax, _do_gc;
46| call eax;
47|.endmacro;
swissChili6d6525e2021-06-15 21:20:53 -070048
swissChili53472e82021-05-08 16:06:32 -070049struct function *find_function(struct environment *env, char *name)
swissChilica107a02021-04-14 12:07:30 -070050{
swissChilif68671f2021-07-05 14:14:44 -070051 struct function *f;
swissChilica107a02021-04-14 12:07:30 -070052
swissChilif68671f2021-07-05 14:14:44 -070053 for (f = env->first; f && strcmp(f->name, name); f = f->prev)
swissChilica107a02021-04-14 12:07:30 -070054 {
swissChilica107a02021-04-14 12:07:30 -070055 }
56
57 return f;
58}
59
swissChili67bdf282021-06-06 18:46:08 -070060unsigned int local_alloc(struct local *local)
61{
62 for (int i = 0; i < local->num_stack_slots; i++)
63 {
64 if (local->stack_slots[i] == false)
65 {
66 local->stack_slots[i] = true;
67
68 if (i >= local->num_stack_entries)
69 local->num_stack_entries++;
70
71 return i;
72 }
73 }
74
75 int old_size = local->num_stack_slots;
76 local->num_stack_slots += 4;
swissChili7e1393c2021-07-07 12:59:12 -070077 local->stack_slots =
78 realloc(local->stack_slots, local->num_stack_slots * sizeof(bool));
swissChili67bdf282021-06-06 18:46:08 -070079 // unreadable: set the remaining slots to unused
80 memset(local->stack_slots + old_size, 0, local->num_stack_slots - old_size);
81 local->stack_slots[old_size] = true;
82
83 return old_size;
84}
85
86void local_free(struct local *local, unsigned int slot)
87{
88 local->stack_slots[slot] = false;
89}
90
swissChili708d4c42021-07-04 17:40:07 -070091void del_local(struct local *local)
92{
93 free(local->stack_slots);
94
95 for (struct variable *next, *f = local->first; f; f = next)
96 {
97 next = f->prev;
98 free(f);
99 }
100}
101
102void del_env(struct environment *env)
103{
104 for (struct function *next, *f = env->first; f; f = next)
105 {
106 next = f->prev;
107 // We're not gonna bother munmap()ing the function
108 free(f);
109 }
swissChilif68671f2021-07-05 14:14:44 -0700110
111 for (struct loaded_file *next, *l = env->first_loaded; l; l = next)
112 {
113 next = l->previous;
114 free(l->resolved_path);
115 free(l);
116 }
swissChili7e1393c2021-07-07 12:59:12 -0700117
118 free(env);
swissChilif68671f2021-07-05 14:14:44 -0700119}
120
121void add_load(struct environment *env, char *path)
122{
123 static char buffer[512];
124 long size = readlink(path, buffer, 512);
125 buffer[size] = '\0';
126 char *resolved = strdup(buffer);
127
128 struct loaded_file *f = malloc(sizeof(struct loaded_file));
129 f->resolved_path = resolved;
130 f->previous = env->first_loaded;
131 env->first_loaded = f;
swissChili708d4c42021-07-04 17:40:07 -0700132}
133
swissChilif1ba8c12021-07-02 18:45:38 -0700134struct dasm_State *compile_function(value_t args, enum namespace namespace,
swissChili7e1393c2021-07-07 12:59:12 -0700135 struct environment *env,
136 struct local *local_out,
137 struct local *local_parent,
138 struct args **args_out, char *name,
139 char *path)
swissChilif1ba8c12021-07-02 18:45:38 -0700140{
141 dasm_State *d;
142 dasm_State **Dst = &d;
143
swissChili484295d2021-07-09 21:25:55 -0700144 |.section code, imports;
swissChilif1ba8c12021-07-02 18:45:38 -0700145 dasm_init(&d, DASM_MAXSECTION);
146
147 |.globals lbl_;
148 void *labels[lbl__MAX];
149 dasm_setupglobal(&d, labels, lbl__MAX);
150
151 |.actionlist lisp_actions;
152 dasm_setup(&d, lisp_actions);
153
154 struct local local;
155 local.parent = NULL;
156 local.first = NULL;
157 local.num_vars = 0;
158 local.npc = 8;
159 local.nextpc = 0;
160 local.stack_slots = malloc(sizeof(bool) * 4);
161 memset(local.stack_slots, 0, sizeof(bool) * 4);
162 local.num_stack_slots = 4;
163 local.num_stack_entries = 0;
swissChiliddc97542021-07-04 11:47:42 -0700164 local.num_closure_slots = 0;
165 local.parent = local_parent;
swissChili74348422021-07-04 13:23:24 -0700166 local.current_function_name = name;
swissChili7e1393c2021-07-07 12:59:12 -0700167 local.current_file_path = path;
swissChilif1ba8c12021-07-02 18:45:38 -0700168
169 dasm_growpc(&d, local.npc);
170
swissChilif1ba8c12021-07-02 18:45:38 -0700171 value_t arglist = car(args);
172 value_t body = cdr(args);
173
swissChili15f1cae2021-07-05 19:08:47 -0700174 // This will add the arguments to local too.
175 struct args *ar = list_to_args(env, arglist, &local);
176 local.args = ar;
swissChili74348422021-07-04 13:23:24 -0700177
swissChili15f1cae2021-07-05 19:08:47 -0700178 if (!ar)
swissChilif1ba8c12021-07-02 18:45:38 -0700179 {
swissChili15f1cae2021-07-05 19:08:47 -0700180 err("Malformed args list");
swissChilif1ba8c12021-07-02 18:45:38 -0700181 }
182
183 for (value_t body_ = body; !nilp(body_); body_ = cdr(body_))
184 {
185 walk_and_alloc(&local, car(body_));
186 }
187
swissChili484295d2021-07-09 21:25:55 -0700188 | setup (local.num_stack_entries);
swissChilif1ba8c12021-07-02 18:45:38 -0700189
190 memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool));
191 local.num_stack_entries = 0;
192
193 for (; !nilp(body); body = cdr(body))
194 {
195 compile_expression(env, &local, car(body), Dst);
196 }
197
198 | cleanup;
199
200 if (local_out)
201 *local_out = local;
202
swissChili15f1cae2021-07-05 19:08:47 -0700203 if (args_out)
204 *args_out = ar;
swissChilif1ba8c12021-07-02 18:45:38 -0700205
206 return d;
207}
208
swissChili7e1393c2021-07-07 12:59:12 -0700209void compile_tl(value_t val, struct environment *env, char *fname)
swissChilica107a02021-04-14 12:07:30 -0700210{
swissChili53472e82021-05-08 16:06:32 -0700211 if (!listp(val))
212 err("Top level must be a list");
swissChilica107a02021-04-14 12:07:30 -0700213
swissChili53472e82021-05-08 16:06:32 -0700214 value_t form = car(val);
215 value_t args = cdr(val);
216
swissChili2999dd12021-07-02 14:19:53 -0700217 if (symstreq(form, "defun") || symstreq(form, "defmacro"))
swissChili8fc5e2f2021-04-22 13:45:10 -0700218 {
swissChili2999dd12021-07-02 14:19:53 -0700219 enum namespace namespace = NS_FUNCTION;
220
221 if (symstreq(form, "defmacro"))
swissChili7e1393c2021-07-07 12:59:12 -0700222 namespace = NS_MACRO;
swissChili2999dd12021-07-02 14:19:53 -0700223
swissChili8fc5e2f2021-04-22 13:45:10 -0700224 struct local local;
swissChili15f1cae2021-07-05 19:08:47 -0700225 struct args *a;
swissChili74348422021-07-04 13:23:24 -0700226 char *name = (char *)(car(args) ^ SYMBOL_TAG);
swissChilif68671f2021-07-05 14:14:44 -0700227
swissChili7e1393c2021-07-07 12:59:12 -0700228 dasm_State *d = compile_function(cdr(args), namespace, env, &local,
229 NULL, &a, name, fname);
swissChilia820dea2021-05-09 16:46:55 -0700230
swissChili7e1393c2021-07-07 12:59:12 -0700231 add_function(env, name, link_program(&d), a, namespace);
swissChili8fc5e2f2021-04-22 13:45:10 -0700232
swissChili53472e82021-05-08 16:06:32 -0700233 dasm_free(&d);
swissChili708d4c42021-07-04 17:40:07 -0700234 del_local(&local);
swissChili67bdf282021-06-06 18:46:08 -0700235 }
swissChilif68671f2021-07-05 14:14:44 -0700236 else if (symstreq(form, "progn"))
237 {
238 for (value_t val = args; !nilp(val); val = cdr(val))
239 {
swissChili7e1393c2021-07-07 12:59:12 -0700240 compile_tl(car(val), env, fname);
swissChilif68671f2021-07-05 14:14:44 -0700241 }
242 }
swissChili484295d2021-07-09 21:25:55 -0700243 else if (symstreq(form, "load"))
244 {
245 if (length(args) != 1)
246 {
247 err_at(val, "load expects exactly 1 argument, %d given",
248 length(args));
249 }
250 load_relative(env, fname, car(args));
251 }
swissChili67bdf282021-06-06 18:46:08 -0700252}
253
254void walk_and_alloc(struct local *local, value_t body)
255{
256 if (!listp(body))
257 return;
258
259 value_t args = cdr(body);
260
261 if (symstreq(car(body), "let1"))
262 {
263 int slot = local_alloc(local);
264
265 value_t expr = cdr(args);
swissChilif1ba8c12021-07-02 18:45:38 -0700266 for (; !nilp(expr); expr = cdr(expr))
267 {
swissChiliddc97542021-07-04 11:47:42 -0700268 walk_and_alloc(local, car(expr));
swissChilif1ba8c12021-07-02 18:45:38 -0700269 }
swissChili67bdf282021-06-06 18:46:08 -0700270
271 local_free(local, slot);
272 }
swissChilif1ba8c12021-07-02 18:45:38 -0700273 else if (symstreq(car(body), "lambda"))
274 {
275 // We don't want to walk the lambda because it's another function. When
276 // the lambda is compiled it will be walked.
277 return;
278 }
swissChili67bdf282021-06-06 18:46:08 -0700279 else
280 {
281 for (; !nilp(args); args = cdr(args))
282 {
283 walk_and_alloc(local, car(args));
284 }
swissChili8fc5e2f2021-04-22 13:45:10 -0700285 }
286}
287
swissChilif68671f2021-07-05 14:14:44 -0700288bool load(struct environment *env, char *path)
swissChili8fc5e2f2021-04-22 13:45:10 -0700289{
swissChilif68671f2021-07-05 14:14:44 -0700290 if (!file_exists(path))
291 return false;
292
293 add_load(env, path);
294
swissChilib8fd4712021-06-23 15:32:04 -0700295 unsigned char pool = make_pool();
296 unsigned char pop = push_pool(pool);
297
swissChilif68671f2021-07-05 14:14:44 -0700298 struct istream *is = new_fistream(path, false);
299 if (!is)
300 return false;
301
swissChili8fc5e2f2021-04-22 13:45:10 -0700302 value_t val;
swissChili53472e82021-05-08 16:06:32 -0700303
304 while (read1(is, &val))
swissChili8fc5e2f2021-04-22 13:45:10 -0700305 {
swissChili7e1393c2021-07-07 12:59:12 -0700306 compile_tl(val, env, path);
swissChili8fc5e2f2021-04-22 13:45:10 -0700307 }
swissChilif3e7f182021-04-20 13:57:22 -0700308
swissChilif68671f2021-07-05 14:14:44 -0700309 del_fistream(is);
swissChilib8fd4712021-06-23 15:32:04 -0700310 pop_pool(pop);
311
swissChilif68671f2021-07-05 14:14:44 -0700312 return true;
313}
314
swissChili7e1393c2021-07-07 12:59:12 -0700315value_t load_relative(struct environment *env, char *to, value_t name)
316{
317 if (!stringp(name))
318 return nil;
319
320 char *new_path = (char *)(name ^ STRING_TAG);
321 char *relative_to = strdup(to);
322 char full_path[512];
323
324 snprintf(full_path, 512, "%s/%s", dirname(relative_to), new_path);
325
326 if (load(env, full_path))
327 return t;
328 else
329 return nil;
330}
331
332struct environment *compile_file(char *filename, bool *ok)
swissChilif68671f2021-07-05 14:14:44 -0700333{
334 value_t val;
swissChili7e1393c2021-07-07 12:59:12 -0700335 struct environment *env = malloc(sizeof(struct environment));
336 env->first = NULL;
337 env->first_loaded = NULL;
swissChilif68671f2021-07-05 14:14:44 -0700338
swissChili7e1393c2021-07-07 12:59:12 -0700339 add_load(env, filename);
340 load_std(env);
swissChilif68671f2021-07-05 14:14:44 -0700341
swissChili7e1393c2021-07-07 12:59:12 -0700342 bool ok_ = load(env, filename);
swissChilif68671f2021-07-05 14:14:44 -0700343
344 if (ok)
345 *ok = ok_;
346
swissChili8fc5e2f2021-04-22 13:45:10 -0700347 return env;
swissChilica107a02021-04-14 12:07:30 -0700348}
swissChilib3ca4fb2021-04-20 10:33:00 -0700349
swissChili53472e82021-05-08 16:06:32 -0700350int nextpc(struct local *local, dasm_State **Dst)
swissChilib3ca4fb2021-04-20 10:33:00 -0700351{
swissChili53472e82021-05-08 16:06:32 -0700352 int n = local->nextpc++;
353 if (n > local->npc)
354 {
355 local->npc += 16;
356 dasm_growpc(Dst, local->npc);
357 }
358 return n;
359}
360
swissChili6b47b6d2021-06-30 22:08:55 -0700361void compile_backquote(struct environment *env, struct local *local,
362 value_t val, dasm_State **Dst)
363{
364 if (!listp(val))
365 {
366 | mov eax, (val);
367 }
368 else
369 {
swissChili7e1393c2021-07-07 12:59:12 -0700370 value_t fsym = car(val), args = cdr(val);
swissChili6b47b6d2021-06-30 22:08:55 -0700371 int nargs = length(args);
372
373 // TODO
374 }
375}
376
swissChili7e1393c2021-07-07 12:59:12 -0700377value_t eval(struct environment *env, value_t form)
378{
379 // Eval!
380 value_t function = cons(nil, cons(form, nil));
381
382 struct local local;
383 struct args *args;
384
385 dasm_State *d = compile_function(function, NS_ANONYMOUS, env, &local, NULL,
386 &args, NULL, "/");
387
388 del_local(&local);
389
390 value_t (*f)() = link_program(&d);
391 return f();
392}
393
swissChiliddc97542021-07-04 11:47:42 -0700394void compile_variable(struct variable *v, dasm_State *Dst)
395{
396 switch (v->type)
397 {
398 case V_ARGUMENT:
swissChili7e1393c2021-07-07 12:59:12 -0700399 | mov eax, dword[ebp + (value_size * (v->number + 2))];
swissChiliddc97542021-07-04 11:47:42 -0700400 break;
401 case V_BOUND:
swissChili7e1393c2021-07-07 12:59:12 -0700402 | mov eax, dword[ebp - ((v->number + 1) * value_size)];
swissChiliddc97542021-07-04 11:47:42 -0700403 break;
404 case V_FREE:
405 // edi is the closure context pointer
swissChili7e1393c2021-07-07 12:59:12 -0700406 | mov eax, dword[edi + (v->number * value_size)];
swissChiliddc97542021-07-04 11:47:42 -0700407 break;
408 default:
swissChili7e1393c2021-07-07 12:59:12 -0700409 err("Sorry, can only access V_ARGUMENT, V_FREE and V_BOUND variables "
410 "for now :(");
swissChiliddc97542021-07-04 11:47:42 -0700411 }
412}
413
swissChili53472e82021-05-08 16:06:32 -0700414void compile_expression(struct environment *env, struct local *local,
415 value_t val, dasm_State **Dst)
416{
swissChili7e1393c2021-07-07 12:59:12 -0700417 if (symstreq(val, "nil") || nilp(val))
swissChili53472e82021-05-08 16:06:32 -0700418 {
419 | mov eax, (nil);
420 }
swissChili923b5362021-05-09 20:31:43 -0700421 else if (symstreq(val, "t"))
422 {
423 | mov eax, (t);
424 }
425 else if (integerp(val) || stringp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700426 {
427 | mov eax, val;
428 }
swissChili53472e82021-05-08 16:06:32 -0700429 else if (listp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700430 {
swissChili53472e82021-05-08 16:06:32 -0700431 value_t fsym = car(val);
432 value_t args = cdr(val);
433 int nargs = length(args);
swissChilib3ca4fb2021-04-20 10:33:00 -0700434
swissChili53472e82021-05-08 16:06:32 -0700435 if (!symbolp(fsym))
swissChilif3e7f182021-04-20 13:57:22 -0700436 {
swissChili7e1393c2021-07-07 12:59:12 -0700437 printval(val, 2);
438 err_at(val, "function name must be a symbol");
swissChilif3e7f182021-04-20 13:57:22 -0700439 }
440
swissChili53472e82021-05-08 16:06:32 -0700441 if (symstreq(fsym, "if"))
swissChilib3ca4fb2021-04-20 10:33:00 -0700442 {
swissChili53472e82021-05-08 16:06:32 -0700443 if (nargs < 2 || nargs > 3)
444 err("Must give at least 2 arguments to if");
swissChilib3ca4fb2021-04-20 10:33:00 -0700445
swissChili53472e82021-05-08 16:06:32 -0700446 compile_expression(env, local, car(args), Dst);
447 int false_label = nextpc(local, Dst),
448 after_label = nextpc(local, Dst);
449
450 // result is in eax
451 | cmp eax, (nil);
swissChili484295d2021-07-09 21:25:55 -0700452 | je =>false_label;
swissChili53472e82021-05-08 16:06:32 -0700453
454 compile_expression(env, local, elt(args, 1), Dst);
swissChili484295d2021-07-09 21:25:55 -0700455 | jmp =>after_label;
456 |=>false_label:;
swissChili53472e82021-05-08 16:06:32 -0700457 if (nargs == 3)
swissChili7e1393c2021-07-07 12:59:12 -0700458 compile_expression(env, local, elt(args, 2), Dst);
swissChili484295d2021-07-09 21:25:55 -0700459 |=>after_label:;
swissChili53472e82021-05-08 16:06:32 -0700460 }
swissChilif68671f2021-07-05 14:14:44 -0700461 else if (symstreq(fsym, "progn"))
462 {
463 for (value_t val = args; !nilp(val); val = cdr(val))
464 {
465 compile_expression(env, local, car(val), Dst);
466 }
467 }
swissChili67bdf282021-06-06 18:46:08 -0700468 else if (symstreq(fsym, "let1"))
469 {
470 if (nargs < 2)
471 {
472 err("Must give at least 2 arguments to let1");
473 }
474 value_t binding = car(args);
475 value_t rest = cdr(args);
476
477 if (length(binding) != 2)
478 {
479 err("Binding list in let1 must contain exactly two entries");
480 }
481
482 value_t name = car(binding);
483 value_t value = car(cdr(binding));
484
485 compile_expression(env, local, value, Dst);
486
487 int i = local_alloc(local);
488
489 add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i);
490
swissChili7e1393c2021-07-07 12:59:12 -0700491 | mov dword[ebp - ((i + 1) * value_size)], eax;
swissChili67bdf282021-06-06 18:46:08 -0700492
493 for (; !nilp(rest); rest = cdr(rest))
494 {
495 compile_expression(env, local, car(rest), Dst);
496 }
497
498 local_free(local, i);
499 }
swissChilie9fec8b2021-06-22 13:59:33 -0700500 else if (symstreq(fsym, "gc"))
501 {
502 if (nargs)
503 {
swissChili7e1393c2021-07-07 12:59:12 -0700504 err_at(val, "gc takes no arguments");
swissChilie9fec8b2021-06-22 13:59:33 -0700505 }
506
507 | run_gc;
508 }
swissChili6b47b6d2021-06-30 22:08:55 -0700509 else if (symstreq(fsym, "quote"))
510 {
511 if (nargs != 1)
512 err("quote should take exactly 1 argument");
513
514 // Simple!
515 | mov eax, (car(args));
516 }
517 else if (symstreq(fsym, "backquote"))
518 {
519 if (nargs != 1)
520 err("backquote should take exactly 1 argument");
521
522 compile_backquote(env, local, car(args), Dst);
523 }
swissChili74348422021-07-04 13:23:24 -0700524 else if (symstreq(fsym, "function"))
525 {
526 if (nargs != 1)
527 {
528 err("function should take exactly 1 argument");
529 }
530
531 if (!symbolp(car(args)))
532 {
swissChili7e1393c2021-07-07 12:59:12 -0700533 err("argument to function should be a symbol resolvable at "
534 "compile time");
swissChili74348422021-07-04 13:23:24 -0700535 }
536
swissChili7e1393c2021-07-07 12:59:12 -0700537 struct function *f =
538 find_function(env, (char *)(car(args) ^ SYMBOL_TAG));
swissChili15f1cae2021-07-05 19:08:47 -0700539 value_t closure = create_closure(f->code_ptr, f->args, 0);
swissChili74348422021-07-04 13:23:24 -0700540
541 | mov eax, (closure);
542 }
swissChili6b47b6d2021-06-30 22:08:55 -0700543 else if (symstreq(fsym, "list"))
544 {
swissChili484295d2021-07-09 21:25:55 -0700545 | push (nil);
swissChili6b47b6d2021-06-30 22:08:55 -0700546
547 for (int i = nargs - 1; i >= 0; i--)
548 {
549 compile_expression(env, local, elt(args, i), Dst);
550
551 // push the ith item
552 | push eax;
553 // cons the top two stack items
swissChili53e7cd12021-08-02 21:55:53 -0700554 | call_extern cons;
swissChili6b47b6d2021-06-30 22:08:55 -0700555 // remove the stack items from use
556 | add esp, (2 * value_size);
557 // put the new thing on the stack
558 | push eax;
559 }
560
561 | pop eax;
562 }
swissChiliddc97542021-07-04 11:47:42 -0700563 else if (symstreq(fsym, "lambda"))
564 {
565 // Compile the function with this as the parent scope
566 struct local new_local;
567 int nargs_out;
swissChili7e1393c2021-07-07 12:59:12 -0700568 dasm_State *d = compile_function(
569 args, NS_ANONYMOUS, env, &new_local, local, &nargs_out,
570 "recurse", local->current_file_path);
swissChiliddc97542021-07-04 11:47:42 -0700571
572 // Link the function
swissChilif68671f2021-07-05 14:14:44 -0700573 void *func_ptr = link_program(&d);
swissChiliddc97542021-07-04 11:47:42 -0700574
575 // Create a closure object with the correct number of captures at
576 // runtime
swissChili484295d2021-07-09 21:25:55 -0700577 | push (new_local.num_closure_slots);
578 | push (nargs_out);
579 | push (func_ptr);
swissChili53e7cd12021-08-02 21:55:53 -0700580 | call_extern create_closure;
swissChiliddc97542021-07-04 11:47:42 -0700581 | add esp, 12;
582
583 // Walk the generated local scope for V_FREE variables, since each
584 // of these exists in our scope (or higher), evaluate it and set it
585 // as a member of the lambda capture.
586
587 for (struct variable *var = new_local.first; var; var = var->prev)
588 {
589 if (var->type == V_FREE)
590 {
591 // Closure in eax
592 | push eax;
593 // Variable now in eax
594 compile_variable(find_variable(local, var->name), Dst);
595 | push eax;
596
swissChiliddc97542021-07-04 11:47:42 -0700597 // The capture offset
swissChili484295d2021-07-09 21:25:55 -0700598 | push (var->number);
swissChili53e7cd12021-08-02 21:55:53 -0700599 | call_extern set_closure_capture_variable;
swissChiliddc97542021-07-04 11:47:42 -0700600 // Skip the value and index
601 | add esp, 8;
602 // Pop the closure back in to eax
603 | pop eax;
604 }
605 }
606
607 // Closure is still in eax
608
609 dasm_free(&d);
swissChili708d4c42021-07-04 17:40:07 -0700610 del_local(&new_local);
swissChiliddc97542021-07-04 11:47:42 -0700611 }
swissChili7e1393c2021-07-07 12:59:12 -0700612 else if (symstreq(fsym, "eval"))
613 {
614 if (nargs != 1)
615 {
616 err("eval takes exactly 1 argument");
617 }
618
619 compile_expression(env, local, car(args), Dst);
620 | push eax;
swissChili484295d2021-07-09 21:25:55 -0700621 | push (env);
swissChili53e7cd12021-08-02 21:55:53 -0700622 | call_extern eval;
swissChili7e1393c2021-07-07 12:59:12 -0700623 }
624 else if (symstreq(fsym, "load"))
625 {
626 if (nargs != 1)
627 {
628 err_at(val, "load takes exactly 1 argument, %d given", nargs);
629 }
630
631 compile_expression(env, local, car(args), Dst);
632 | push eax;
swissChili484295d2021-07-09 21:25:55 -0700633 | push (local->current_file_path);
634 | push (env);
swissChili53e7cd12021-08-02 21:55:53 -0700635 | call_extern load_relative;
swissChili7e1393c2021-07-07 12:59:12 -0700636 }
swissChili53472e82021-05-08 16:06:32 -0700637 else
638 {
swissChili74348422021-07-04 13:23:24 -0700639 char *name = (char *)(fsym ^ SYMBOL_TAG);
640 struct function *func = find_function(env, name);
swissChili7e1393c2021-07-07 12:59:12 -0700641
swissChili74348422021-07-04 13:23:24 -0700642 bool is_recursive = false;
swissChili15f1cae2021-07-05 19:08:47 -0700643 struct args *nargs_needed = NULL;
swissChili53472e82021-05-08 16:06:32 -0700644
swissChili53e7cd12021-08-02 21:55:53 -0700645 // The number of arguments actually passed on the stack,
646 // i.e. all varargs are 1.
647 int real_nargs = nargs;
648
swissChili7e1393c2021-07-07 12:59:12 -0700649 if (local->current_function_name &&
650 symstreq(fsym, local->current_function_name))
swissChilif1ba8c12021-07-02 18:45:38 -0700651 {
swissChili74348422021-07-04 13:23:24 -0700652 is_recursive = true;
swissChili15f1cae2021-07-05 19:08:47 -0700653 nargs_needed = local->args;
swissChili74348422021-07-04 13:23:24 -0700654 }
655 else
656 {
657 if (func == NULL)
658 {
swissChili7e1393c2021-07-07 12:59:12 -0700659 err_at(val, "Function %s undefined", name);
swissChili74348422021-07-04 13:23:24 -0700660 }
661
swissChili15f1cae2021-07-05 19:08:47 -0700662 nargs_needed = func->args;
swissChili74348422021-07-04 13:23:24 -0700663 }
664
swissChili15f1cae2021-07-05 19:08:47 -0700665 if (!are_args_acceptable(nargs_needed, nargs))
swissChili74348422021-07-04 13:23:24 -0700666 {
swissChili7e1393c2021-07-07 12:59:12 -0700667 err_at(val,
668 "wrong number of args in function call: %s at %s:%d, "
669 "want %d args but given %d\n",
670 name, cons_file(val), cons_line(val),
671 nargs_needed->num_required, nargs);
swissChilif1ba8c12021-07-02 18:45:38 -0700672 }
swissChili53472e82021-05-08 16:06:32 -0700673
swissChili53e7cd12021-08-02 21:55:53 -0700674 int total_taken = nargs_needed->num_optional +
675 nargs_needed->num_required;
676
677 if (nargs > total_taken)
678 {
679 real_nargs = total_taken + 1;
680 }
681 else
682 {
683 real_nargs = total_taken;
684 }
685
swissChili74348422021-07-04 13:23:24 -0700686 if (is_recursive || func->namespace == NS_FUNCTION)
swissChili53472e82021-05-08 16:06:32 -0700687 {
swissChili15f1cae2021-07-05 19:08:47 -0700688 int nargs = length(args);
689
swissChili484295d2021-07-09 21:25:55 -0700690 int line = cons_line(val);
691 char *file = cons_file(val);
692
693 if (nargs_needed->variadic)
swissChili15f1cae2021-07-05 19:08:47 -0700694 {
swissChili484295d2021-07-09 21:25:55 -0700695 | push (nil);
696 }
697
698 if (nargs > total_taken && nargs_needed->variadic)
699 {
700 // We are passing varargs, which means we need to make a list
701
702 for (int i = nargs - 1; i >= total_taken; i--)
703 {
704 compile_expression(env, local, elt(args, i), Dst);
705 | push eax;
swissChili53e7cd12021-08-02 21:55:53 -0700706 | call_extern cons;
swissChili484295d2021-07-09 21:25:55 -0700707 | add esp, 8;
708 | push eax;
709 }
swissChili15f1cae2021-07-05 19:08:47 -0700710 }
711
swissChili7e1393c2021-07-07 12:59:12 -0700712 for (int i = nargs_needed->num_optional - 1;
713 i >= nargs - nargs_needed->num_required; i--)
swissChili15f1cae2021-07-05 19:08:47 -0700714 {
715 // Push the default optional values
swissChili484295d2021-07-09 21:25:55 -0700716 | push (nargs_needed->optional_arguments[i].value);
swissChili15f1cae2021-07-05 19:08:47 -0700717 }
718
swissChili484295d2021-07-09 21:25:55 -0700719 int min = MIN(nargs, total_taken);
720
721 for (int i = min - 1; i >= 0; i--)
swissChili2999dd12021-07-02 14:19:53 -0700722 {
723 compile_expression(env, local, elt(args, i), Dst);
724 | push eax;
725 }
swissChili15f1cae2021-07-05 19:08:47 -0700726
swissChili74348422021-07-04 13:23:24 -0700727 if (is_recursive)
728 {
swissChili53e7cd12021-08-02 21:55:53 -0700729 | call <1;
swissChili74348422021-07-04 13:23:24 -0700730 }
731 else
732 {
swissChili484295d2021-07-09 21:25:55 -0700733 // | mov ebx, (func->code_addr);
734 | call_extern func->code_addr;
swissChili74348422021-07-04 13:23:24 -0700735 }
swissChili53e7cd12021-08-02 21:55:53 -0700736 | add esp, (real_nargs * value_size);
swissChili2999dd12021-07-02 14:19:53 -0700737 // result in eax
738 }
739 else if (func->namespace == NS_MACRO)
740 {
swissChili7e1393c2021-07-07 12:59:12 -0700741 // Make sure that the stuff allocated by the macro isn't in a
742 // pool
swissChilif68671f2021-07-05 14:14:44 -0700743 unsigned char pool = push_pool(0);
744
swissChili2999dd12021-07-02 14:19:53 -0700745 value_t expanded_to = call_list(func, args);
746
swissChilif68671f2021-07-05 14:14:44 -0700747 pop_pool(pool);
748
swissChili2999dd12021-07-02 14:19:53 -0700749 compile_expression(env, local, expanded_to, Dst);
750 }
swissChili53472e82021-05-08 16:06:32 -0700751 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700752 }
swissChili923b5362021-05-09 20:31:43 -0700753 else if (symbolp(val))
754 {
swissChili7e1393c2021-07-07 12:59:12 -0700755 if (symstreq(val, "+current-file+"))
swissChilie9fec8b2021-06-22 13:59:33 -0700756 {
swissChili7e1393c2021-07-07 12:59:12 -0700757 value_t file_name_val = strval(local->current_file_path);
758
759 | mov eax, (file_name_val);
swissChilie9fec8b2021-06-22 13:59:33 -0700760 }
swissChili7e1393c2021-07-07 12:59:12 -0700761 else
762 {
763 struct variable *v =
764 find_variable(local, (char *)(val ^ SYMBOL_TAG));
swissChili923b5362021-05-09 20:31:43 -0700765
swissChili7e1393c2021-07-07 12:59:12 -0700766 if (!v)
767 {
768 fprintf(stderr, "var: %s\n", (char *)(val ^ SYMBOL_TAG));
769 err("Variable unbound");
770 }
771
772 compile_variable(v, Dst);
773 }
swissChili923b5362021-05-09 20:31:43 -0700774 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700775}
swissChilif3e7f182021-04-20 13:57:22 -0700776
swissChili923b5362021-05-09 20:31:43 -0700777struct variable *add_variable(struct local *local, enum var_type type,
778 char *name, int number)
779{
780 struct variable *var = malloc(sizeof(struct variable));
781 var->prev = local->first;
782 var->type = type;
783 var->name = name;
784 var->number = number;
785
786 local->first = var;
787
788 return var;
789}
790
791void destroy_local(struct local *local)
792{
793 for (struct variable *v = local->first; v;)
794 {
795 struct variable *t = v;
796 v = v->prev;
797 free(t);
798 }
799}
800
801struct variable *find_variable(struct local *local, char *name)
802{
803 struct variable *v = local->first;
804
805 for (; v && strcmp(v->name, name) != 0; v = v->prev)
swissChili7e1393c2021-07-07 12:59:12 -0700806 {
807 }
swissChili923b5362021-05-09 20:31:43 -0700808
swissChiliddc97542021-07-04 11:47:42 -0700809 if (!v)
810 {
811 if (local->parent)
812 {
813 v = find_variable(local->parent, name);
814
815 if (v)
816 {
swissChili15f1cae2021-07-05 19:08:47 -0700817 // We found this in a parent scope, add it as a V_FREE variable
818 // to skip the search.
swissChili7e1393c2021-07-07 12:59:12 -0700819 v = add_variable(local, V_FREE, name,
820 local->num_closure_slots++);
swissChiliddc97542021-07-04 11:47:42 -0700821 }
822 }
823 }
swissChili923b5362021-05-09 20:31:43 -0700824 return v;
825}
swissChili2999dd12021-07-02 14:19:53 -0700826
swissChiliddc97542021-07-04 11:47:42 -0700827extern value_t _call_list(void *addr, value_t list, value_t *edi);
swissChili2999dd12021-07-02 14:19:53 -0700828
swissChili7e1393c2021-07-07 12:59:12 -0700829value_t call_list_args(void *code_ptr, struct args *args, value_t list,
830 void *data)
swissChili2999dd12021-07-02 14:19:53 -0700831{
swissChili15f1cae2021-07-05 19:08:47 -0700832 list = deep_copy(list);
swissChili484295d2021-07-09 21:25:55 -0700833
swissChili15f1cae2021-07-05 19:08:47 -0700834 int nargs = length(list);
835
swissChili484295d2021-07-09 21:25:55 -0700836 value_t *val = &list;
swissChili15f1cae2021-07-05 19:08:47 -0700837
838 for (value_t i = list; !nilp(i); i = cdr(i))
839 {
840 val = cdrref(i);
841 }
842
843 int total_required = args->num_required + args->num_optional;
844
845 if (nargs > total_required)
846 {
847 // Take the remainder of the list and put it as the last item in the
848 // list.
849 value_t trailing = cxdr(list, total_required);
850 value_t last_item = cons(trailing, nil);
851
852 *cxdrref(&list, total_required) = last_item;
853 }
854 else if (nargs < total_required)
855 {
856 for (int i = nargs - args->num_required; i < args->num_optional; i++)
857 {
858 // Append the i-th defualt argument
859 value_t appended = cons(args->optional_arguments[i].value, nil);
860 *val = appended;
861 val = cdrref(appended);
862 }
863 }
864
865 // We want to call this if we pass the correct # of arguments or less, just
866 // not if we have already passed varargs. Appends a nil argument.
867 if (nargs <= total_required)
868 {
869 // Enough real arguments but no variadic arguments. Pass a nil list.
870 *val = cons(nil, nil);
871 }
872
873 return _call_list(code_ptr, list, data);
874}
875
876value_t call_list(struct function *fun, value_t list)
877{
878 return call_list_args(fun->code_ptr, fun->args, list, NULL);
swissChiliddc97542021-07-04 11:47:42 -0700879}
880
881value_t call_list_closure(struct closure *c, value_t list)
882{
swissChili15f1cae2021-07-05 19:08:47 -0700883 return call_list_args(c->function, c->args, list, c->data);
884}
885
886struct args *new_args()
887{
888 struct args *a = malloc(sizeof(struct args));
889 a->num_optional = 0;
890 a->num_required = 0;
891 a->variadic = false;
892
893 return a;
894}
895
swissChili7e1393c2021-07-07 12:59:12 -0700896struct args *add_optional_arg(struct args *args, value_t name, value_t value)
swissChili15f1cae2021-07-05 19:08:47 -0700897{
898 int i = args->num_optional++;
swissChili7e1393c2021-07-07 12:59:12 -0700899 args =
900 realloc(args, sizeof(struct args) + sizeof(struct optional_argument) *
901 args->num_optional);
swissChili15f1cae2021-07-05 19:08:47 -0700902
swissChili7e1393c2021-07-07 12:59:12 -0700903 args->optional_arguments[i] = (struct optional_argument){
904 .value = value,
905 .name = name,
swissChili15f1cae2021-07-05 19:08:47 -0700906 };
907
908 return args;
909}
910
911bool are_args_acceptable(struct args *args, int number)
912{
913 if (args->variadic)
914 {
915 return number >= args->num_required;
916 }
917 else
918 {
919 return number >= args->num_required &&
swissChili7e1393c2021-07-07 12:59:12 -0700920 number <= args->num_required + args->num_optional;
swissChili15f1cae2021-07-05 19:08:47 -0700921 }
922}
923
swissChili7e1393c2021-07-07 12:59:12 -0700924struct args *list_to_args(struct environment *env, value_t list,
925 struct local *local)
swissChili15f1cae2021-07-05 19:08:47 -0700926{
927 struct args *args = new_args();
928
929 bool in_optional = false;
930
931 for (value_t i = list; !nilp(i); i = cdr(i))
932 {
933 value_t val = car(i);
934 if (symbolp(val))
935 {
936 if (!args->variadic && symstreq(val, "&"))
937 {
938 i = cdr(i);
939 value_t name = car(i);
940
941 if (!symbolp(name))
942 {
swissChili7e1393c2021-07-07 12:59:12 -0700943 err("You must provide a symbol after & in an argument list "
944 "to bind the\n"
945 "variadic arguments to.");
swissChili15f1cae2021-07-05 19:08:47 -0700946 }
947
948 args->variadic = true;
949
950 add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG),
swissChili7e1393c2021-07-07 12:59:12 -0700951 args->num_optional + args->num_required);
swissChili15f1cae2021-07-05 19:08:47 -0700952
953 continue;
954 }
955
956 if (!in_optional)
957 {
swissChili7e1393c2021-07-07 12:59:12 -0700958 add_variable(local, V_ARGUMENT, (char *)(val ^ SYMBOL_TAG),
959 args->num_required++);
swissChili15f1cae2021-07-05 19:08:47 -0700960 }
961 else
962 {
963 char *name = (char *)(val ^ SYMBOL_TAG);
964 if (name[0] == '&')
965 {
swissChili7e1393c2021-07-07 12:59:12 -0700966 err("Non-optional argument following optional arguments "
967 "starts with a &\n"
968 "did you mean to declare a variadic argument? If so "
969 "leave a space\n"
970 "between the & and name.");
swissChili15f1cae2021-07-05 19:08:47 -0700971 }
972 else
973 {
swissChili7e1393c2021-07-07 12:59:12 -0700974 err("Cannot define a non-optional argument after an "
975 "optional one.");
swissChili15f1cae2021-07-05 19:08:47 -0700976 }
977 }
978 }
979 else if (listp(val))
980 {
981 in_optional = true;
982 int len = length(val);
983
984 if (len != 2)
985 {
swissChili7e1393c2021-07-07 12:59:12 -0700986 err("A list defining an optional value must be structured like "
987 "(name expr)\n"
988 "with exactly two arguments.");
swissChili15f1cae2021-07-05 19:08:47 -0700989 }
990
991 value_t name = car(val);
992 value_t expr = car(cdr(val));
993
994 value_t function = cons(nil, cons(expr, nil));
995
swissChili7e1393c2021-07-07 12:59:12 -0700996 dasm_State *d =
997 compile_function(function, NS_ANONYMOUS, env, NULL, NULL, NULL,
998 NULL, local->current_file_path);
swissChili15f1cae2021-07-05 19:08:47 -0700999
1000 // TODO: GC stack top!
1001 value_t (*compiled)() = link_program(&d);
1002
1003 value_t value = compiled();
1004 args = add_optional_arg(args, name, value);
1005
swissChili7e1393c2021-07-07 12:59:12 -07001006 add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG),
1007 args->num_required + args->num_optional - 1);
swissChili15f1cae2021-07-05 19:08:47 -07001008 }
1009 }
1010
1011 return args;
1012}
1013
1014void display_args(struct args *args)
1015{
1016 printf("Args object taking %d require arguments and %d optionals:\n",
swissChili7e1393c2021-07-07 12:59:12 -07001017 args->num_required, args->num_optional);
swissChili15f1cae2021-07-05 19:08:47 -07001018
1019 for (int i = 0; i < args->num_optional; i++)
1020 {
swissChili7e1393c2021-07-07 12:59:12 -07001021 printf(" %d\t%s\n", i,
1022 (char *)(args->optional_arguments[i].name ^ SYMBOL_TAG));
swissChili15f1cae2021-07-05 19:08:47 -07001023 printval(args->optional_arguments[i].value, 2);
1024 }
swissChili2999dd12021-07-02 14:19:53 -07001025}