blob: 5cd6897889e55d041f803291326a5efa4590f0ed [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
swissChili6d02af42021-08-05 19:49:01 -0700134struct error compile_function(value_t args, enum namespace namespace,
135 struct environment *env,
136 struct local *local_out,
137 struct local *local_parent,
138 struct args **args_out, char *name,
139 char *path,
140 dasm_State **state)
swissChilif1ba8c12021-07-02 18:45:38 -0700141{
swissChilia7568dc2021-08-08 16:52:52 -0700142 UNUSED(namespace);
143
swissChili6d02af42021-08-05 19:49:01 -0700144 E_INIT();
145
swissChilif1ba8c12021-07-02 18:45:38 -0700146 dasm_State *d;
147 dasm_State **Dst = &d;
148
swissChili484295d2021-07-09 21:25:55 -0700149 |.section code, imports;
swissChilif1ba8c12021-07-02 18:45:38 -0700150 dasm_init(&d, DASM_MAXSECTION);
151
152 |.globals lbl_;
153 void *labels[lbl__MAX];
154 dasm_setupglobal(&d, labels, lbl__MAX);
155
156 |.actionlist lisp_actions;
157 dasm_setup(&d, lisp_actions);
158
159 struct local local;
160 local.parent = NULL;
161 local.first = NULL;
162 local.num_vars = 0;
163 local.npc = 8;
164 local.nextpc = 0;
165 local.stack_slots = malloc(sizeof(bool) * 4);
166 memset(local.stack_slots, 0, sizeof(bool) * 4);
167 local.num_stack_slots = 4;
168 local.num_stack_entries = 0;
swissChiliddc97542021-07-04 11:47:42 -0700169 local.num_closure_slots = 0;
170 local.parent = local_parent;
swissChili74348422021-07-04 13:23:24 -0700171 local.current_function_name = name;
swissChili7e1393c2021-07-07 12:59:12 -0700172 local.current_file_path = path;
swissChilif1ba8c12021-07-02 18:45:38 -0700173
174 dasm_growpc(&d, local.npc);
175
swissChilif1ba8c12021-07-02 18:45:38 -0700176 value_t arglist = car(args);
177 value_t body = cdr(args);
178
swissChili15f1cae2021-07-05 19:08:47 -0700179 // This will add the arguments to local too.
swissChili6d02af42021-08-05 19:49:01 -0700180 struct args *ar;
181 TRY(list_to_args(env, arglist, &local, &ar));
swissChili15f1cae2021-07-05 19:08:47 -0700182 local.args = ar;
swissChili74348422021-07-04 13:23:24 -0700183
swissChili15f1cae2021-07-05 19:08:47 -0700184 if (!ar)
swissChilif1ba8c12021-07-02 18:45:38 -0700185 {
swissChili6d02af42021-08-05 19:49:01 -0700186 NEARVAL(arglist);
187 THROW(EMALFORMED, "Malformed argument list");
swissChilif1ba8c12021-07-02 18:45:38 -0700188 }
189
190 for (value_t body_ = body; !nilp(body_); body_ = cdr(body_))
191 {
swissChilifc5c9412021-08-08 19:08:26 -0700192 TRY(walk_and_alloc(env, &local, carref(body_), false));
swissChilif1ba8c12021-07-02 18:45:38 -0700193 }
194
swissChili484295d2021-07-09 21:25:55 -0700195 | setup (local.num_stack_entries);
swissChilif1ba8c12021-07-02 18:45:38 -0700196
197 memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool));
198 local.num_stack_entries = 0;
199
200 for (; !nilp(body); body = cdr(body))
201 {
swissChilib51552c2021-08-03 10:23:37 -0700202 bool tail = nilp(cdr(body));
swissChili6d02af42021-08-05 19:49:01 -0700203 TRY(compile_expression(env, &local, car(body), tail, Dst));
swissChilif1ba8c12021-07-02 18:45:38 -0700204 }
205
206 | cleanup;
207
208 if (local_out)
209 *local_out = local;
210
swissChili15f1cae2021-07-05 19:08:47 -0700211 if (args_out)
212 *args_out = ar;
swissChilif1ba8c12021-07-02 18:45:38 -0700213
swissChili6d02af42021-08-05 19:49:01 -0700214 *state = d;
215
216 OKAY();
swissChilif1ba8c12021-07-02 18:45:38 -0700217}
218
swissChili6d02af42021-08-05 19:49:01 -0700219struct error compile_tl(value_t val, struct environment *env, char *fname)
swissChilica107a02021-04-14 12:07:30 -0700220{
swissChili6d02af42021-08-05 19:49:01 -0700221 E_INIT();
222
223 NEARVAL(val);
224
swissChili53472e82021-05-08 16:06:32 -0700225 if (!listp(val))
swissChili6d02af42021-08-05 19:49:01 -0700226 {
227 THROW(EEXPECTED, "Top level form must be a list");
228 }
swissChilica107a02021-04-14 12:07:30 -0700229
swissChili53472e82021-05-08 16:06:32 -0700230 value_t form = car(val);
231 value_t args = cdr(val);
232
swissChili2999dd12021-07-02 14:19:53 -0700233 if (symstreq(form, "defun") || symstreq(form, "defmacro"))
swissChili8fc5e2f2021-04-22 13:45:10 -0700234 {
swissChili2999dd12021-07-02 14:19:53 -0700235 enum namespace namespace = NS_FUNCTION;
236
237 if (symstreq(form, "defmacro"))
swissChilia89ee442021-08-04 20:54:51 -0700238 namespace = NS_MACRO;
swissChili2999dd12021-07-02 14:19:53 -0700239
swissChili8fc5e2f2021-04-22 13:45:10 -0700240 struct local local;
swissChili15f1cae2021-07-05 19:08:47 -0700241 struct args *a;
swissChili74348422021-07-04 13:23:24 -0700242 char *name = (char *)(car(args) ^ SYMBOL_TAG);
swissChilif68671f2021-07-05 14:14:44 -0700243
swissChili6d02af42021-08-05 19:49:01 -0700244 dasm_State *d;
245 TRY(compile_function(cdr(args), namespace, env, &local,
246 NULL, &a, name, fname, &d));
swissChilia820dea2021-05-09 16:46:55 -0700247
swissChili7e1393c2021-07-07 12:59:12 -0700248 add_function(env, name, link_program(&d), a, namespace);
swissChili8fc5e2f2021-04-22 13:45:10 -0700249
swissChili53472e82021-05-08 16:06:32 -0700250 dasm_free(&d);
swissChili708d4c42021-07-04 17:40:07 -0700251 del_local(&local);
swissChili67bdf282021-06-06 18:46:08 -0700252 }
swissChilif68671f2021-07-05 14:14:44 -0700253 else if (symstreq(form, "progn"))
254 {
255 for (value_t val = args; !nilp(val); val = cdr(val))
256 {
swissChili6d02af42021-08-05 19:49:01 -0700257 TRY(compile_tl(car(val), env, fname));
swissChilif68671f2021-07-05 14:14:44 -0700258 }
259 }
swissChili484295d2021-07-09 21:25:55 -0700260 else if (symstreq(form, "load"))
261 {
262 if (length(args) != 1)
263 {
swissChili6d02af42021-08-05 19:49:01 -0700264 NEARVAL(args);
265 THROW(EARGS, "load expects exactly 1 argument, %d given",
266 length(args));
swissChili484295d2021-07-09 21:25:55 -0700267 }
268 load_relative(env, fname, car(args));
269 }
swissChili6d02af42021-08-05 19:49:01 -0700270
271 OKAY();
swissChili67bdf282021-06-06 18:46:08 -0700272}
273
swissChilifc5c9412021-08-08 19:08:26 -0700274struct error walk_and_alloc(struct environment *env, struct local *local, value_t *bp, bool quoted)
swissChili67bdf282021-06-06 18:46:08 -0700275{
swissChilifc5c9412021-08-08 19:08:26 -0700276 // Note: this kind of sucks. Some of the quote-handling code is
277 // duplicated here and compile_expression. TODO: refactor
278 // eventually.
279
swissChili36f2c692021-08-08 14:31:44 -0700280 E_INIT();
281
282 value_t body = *bp;
283
swissChili67bdf282021-06-06 18:46:08 -0700284 if (!listp(body))
swissChili36f2c692021-08-08 14:31:44 -0700285 OKAY();
swissChili67bdf282021-06-06 18:46:08 -0700286
287 value_t args = cdr(body);
288
swissChilifc5c9412021-08-08 19:08:26 -0700289 if (!quoted && symstreq(car(body), "let1"))
swissChili67bdf282021-06-06 18:46:08 -0700290 {
291 int slot = local_alloc(local);
292
293 value_t expr = cdr(args);
swissChilif1ba8c12021-07-02 18:45:38 -0700294 for (; !nilp(expr); expr = cdr(expr))
295 {
swissChilifc5c9412021-08-08 19:08:26 -0700296 walk_and_alloc(env, local, carref(expr), false);
swissChilif1ba8c12021-07-02 18:45:38 -0700297 }
swissChili67bdf282021-06-06 18:46:08 -0700298
299 local_free(local, slot);
300 }
swissChilifc5c9412021-08-08 19:08:26 -0700301 else if (!quoted && symstreq(car(body), "lambda"))
swissChilif1ba8c12021-07-02 18:45:38 -0700302 {
303 // We don't want to walk the lambda because it's another function. When
304 // the lambda is compiled it will be walked.
swissChili36f2c692021-08-08 14:31:44 -0700305 OKAY();
swissChilif1ba8c12021-07-02 18:45:38 -0700306 }
swissChili67bdf282021-06-06 18:46:08 -0700307 else
308 {
swissChilifc5c9412021-08-08 19:08:26 -0700309 if (quoted)
swissChili67bdf282021-06-06 18:46:08 -0700310 {
swissChilifc5c9412021-08-08 19:08:26 -0700311 if (symstreq(car(body), "unquote") || symstreq(car(body), "unquote-splice"))
312 {
313 for (value_t b = cdr(body); !nilp(b); b = cdr(b))
314 {
315 walk_and_alloc(env, local, carref(b), false);
316 }
317 }
swissChili36f2c692021-08-08 14:31:44 -0700318 }
319 else
320 {
swissChilifc5c9412021-08-08 19:08:26 -0700321 // Is this a macro?
322
323 struct function *mac = NULL;
324
325 if (symbolp(car(body)))
326 mac = find_function(env, (char *)(car(body) ^ SYMBOL_TAG));
327 else if (consp(car(body))) // consp, not just listp, since we don't care about nil.
328 walk_and_alloc(env, local, carref(body), false);
329
330 if (mac && mac->namespace == NS_MACRO)
swissChili36f2c692021-08-08 14:31:44 -0700331 {
swissChilifc5c9412021-08-08 19:08:26 -0700332 unsigned char pool = push_pool(0);
333 value_t form = call_list(mac, args);
334 pop_pool(pool);
335
336 add_to_pool(form);
337 *bp = form;
338
339 walk_and_alloc(env, local, bp, false);
340 }
341 else
342 {
343 bool should_quote = symstreq(car(body), "quote") || symstreq(car(body), "backquote");
344
345 for (; !nilp(args); args = cdr(args))
346 {
347 walk_and_alloc(env, local, carref(args), should_quote);
348 }
swissChili36f2c692021-08-08 14:31:44 -0700349 }
swissChili67bdf282021-06-06 18:46:08 -0700350 }
swissChili8fc5e2f2021-04-22 13:45:10 -0700351 }
swissChili36f2c692021-08-08 14:31:44 -0700352
353 OKAY();
swissChili8fc5e2f2021-04-22 13:45:10 -0700354}
355
swissChilif68671f2021-07-05 14:14:44 -0700356bool load(struct environment *env, char *path)
swissChili8fc5e2f2021-04-22 13:45:10 -0700357{
swissChilif68671f2021-07-05 14:14:44 -0700358 if (!file_exists(path))
359 return false;
360
361 add_load(env, path);
362
swissChilib8fd4712021-06-23 15:32:04 -0700363 unsigned char pool = make_pool();
364 unsigned char pop = push_pool(pool);
365
swissChilif68671f2021-07-05 14:14:44 -0700366 struct istream *is = new_fistream(path, false);
367 if (!is)
368 return false;
369
swissChili8fc5e2f2021-04-22 13:45:10 -0700370 value_t val;
swissChili53472e82021-05-08 16:06:32 -0700371
swissChilifc5c9412021-08-08 19:08:26 -0700372 struct error compile_error, read_error;
swissChili36f2c692021-08-08 14:31:44 -0700373
374 while (IS_OKAY((read_error = read1(is, &val))))
swissChili8fc5e2f2021-04-22 13:45:10 -0700375 {
swissChilifc5c9412021-08-08 19:08:26 -0700376 if (!IS_OKAY((compile_error = compile_tl(val, env, path))))
swissChili36f2c692021-08-08 14:31:44 -0700377 {
swissChilifc5c9412021-08-08 19:08:26 -0700378 ereport(compile_error);
swissChili36f2c692021-08-08 14:31:44 -0700379 goto failure;
380 }
381 }
382
383 if (!read_error.safe_state)
384 {
385 goto failure;
swissChili8fc5e2f2021-04-22 13:45:10 -0700386 }
swissChilif3e7f182021-04-20 13:57:22 -0700387
swissChilif68671f2021-07-05 14:14:44 -0700388 del_fistream(is);
swissChilib8fd4712021-06-23 15:32:04 -0700389 pop_pool(pop);
390
swissChilif68671f2021-07-05 14:14:44 -0700391 return true;
swissChili36f2c692021-08-08 14:31:44 -0700392
393failure:
394 del_fistream(is);
395 pop_pool(pool);
396
397 return false;
swissChilif68671f2021-07-05 14:14:44 -0700398}
399
swissChili7e1393c2021-07-07 12:59:12 -0700400value_t load_relative(struct environment *env, char *to, value_t name)
401{
402 if (!stringp(name))
403 return nil;
404
405 char *new_path = (char *)(name ^ STRING_TAG);
406 char *relative_to = strdup(to);
407 char full_path[512];
408
409 snprintf(full_path, 512, "%s/%s", dirname(relative_to), new_path);
410
411 if (load(env, full_path))
412 return t;
413 else
414 return nil;
415}
416
swissChili6d02af42021-08-05 19:49:01 -0700417struct error compile_file(char *filename, struct environment **e)
swissChilif68671f2021-07-05 14:14:44 -0700418{
swissChili6d02af42021-08-05 19:49:01 -0700419 E_INIT();
420
swissChilif68671f2021-07-05 14:14:44 -0700421 value_t val;
swissChili7e1393c2021-07-07 12:59:12 -0700422 struct environment *env = malloc(sizeof(struct environment));
423 env->first = NULL;
424 env->first_loaded = NULL;
swissChilif68671f2021-07-05 14:14:44 -0700425
swissChili7e1393c2021-07-07 12:59:12 -0700426 add_load(env, filename);
swissChili6d02af42021-08-05 19:49:01 -0700427 TRY(load_std(env));
swissChilif68671f2021-07-05 14:14:44 -0700428
swissChili7e1393c2021-07-07 12:59:12 -0700429 bool ok_ = load(env, filename);
swissChilif68671f2021-07-05 14:14:44 -0700430
swissChili6d02af42021-08-05 19:49:01 -0700431 if (!ok_)
432 {
433 free(env);
434 THROWSAFE(ENOTFOUND);
435 }
swissChilif68671f2021-07-05 14:14:44 -0700436
swissChili6d02af42021-08-05 19:49:01 -0700437 *e = env;
438
439 OKAY();
swissChilica107a02021-04-14 12:07:30 -0700440}
swissChilib3ca4fb2021-04-20 10:33:00 -0700441
swissChili53472e82021-05-08 16:06:32 -0700442int nextpc(struct local *local, dasm_State **Dst)
swissChilib3ca4fb2021-04-20 10:33:00 -0700443{
swissChili53472e82021-05-08 16:06:32 -0700444 int n = local->nextpc++;
445 if (n > local->npc)
446 {
447 local->npc += 16;
448 dasm_growpc(Dst, local->npc);
449 }
450 return n;
451}
452
swissChili6d02af42021-08-05 19:49:01 -0700453struct error compile_backquote(struct environment *env, struct local *local,
454 value_t val, dasm_State **Dst)
swissChili6b47b6d2021-06-30 22:08:55 -0700455{
swissChili6d02af42021-08-05 19:49:01 -0700456 E_INIT();
457
swissChili6b47b6d2021-06-30 22:08:55 -0700458 if (!listp(val))
459 {
460 | mov eax, (val);
461 }
462 else
463 {
swissChili7e1393c2021-07-07 12:59:12 -0700464 value_t fsym = car(val), args = cdr(val);
swissChili9d151e62021-08-04 13:11:45 -0700465 int nargs = length(args),
466 n = length(val);
swissChili6b47b6d2021-06-30 22:08:55 -0700467
swissChili6d02af42021-08-05 19:49:01 -0700468 NEARVAL(val);
469
swissChili9d151e62021-08-04 13:11:45 -0700470 if (symstreq(fsym, "unquote"))
471 {
472 if (nargs != 1)
473 {
swissChili6d02af42021-08-05 19:49:01 -0700474 THROW(EARGS, "unquote (or ,) takes exactly 1 argument");
swissChili9d151e62021-08-04 13:11:45 -0700475 }
476
swissChili6d02af42021-08-05 19:49:01 -0700477 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili9d151e62021-08-04 13:11:45 -0700478 }
479 else
480 {
481 | push nil;
482
483 for (int i = n - 1; i >= 0; i--)
484 {
swissChilia7568dc2021-08-08 16:52:52 -0700485 value_t v = elt(val, i);
swissChili9d151e62021-08-04 13:11:45 -0700486
swissChilia7568dc2021-08-08 16:52:52 -0700487 if (listp(v) && symstreq(car(v), "unquote-splice"))
488 {
489 NEARVAL(v);
490
491 if (length(v) != 2)
492 {
493 THROW(EARGS, "unquote-splice (or ,@) takes exactly 1 argument");
494 }
495
496 value_t expr = car(cdr(v));
497
swissChilia7568dc2021-08-08 16:52:52 -0700498 TRY(compile_expression(env, local, expr, false, Dst));
499 | push eax;
500 | call_extern merge2;
501 | add esp, 8;
502 | push eax;
503 }
504 else
505 {
506 TRY(compile_backquote(env, local, v, Dst));
507 | push eax;
508 | call_extern cons;
509 | add esp, 8;
510
511 // Remove unnecessary pop
512 | push eax;
513 }
swissChili9d151e62021-08-04 13:11:45 -0700514 }
swissChilia89ee442021-08-04 20:54:51 -0700515 | pop eax;
swissChili9d151e62021-08-04 13:11:45 -0700516 }
swissChili6b47b6d2021-06-30 22:08:55 -0700517 }
swissChili6d02af42021-08-05 19:49:01 -0700518
519 OKAY();
swissChili6b47b6d2021-06-30 22:08:55 -0700520}
521
swissChili7e1393c2021-07-07 12:59:12 -0700522value_t eval(struct environment *env, value_t form)
523{
524 // Eval!
525 value_t function = cons(nil, cons(form, nil));
526
527 struct local local;
528 struct args *args;
529
swissChili6d02af42021-08-05 19:49:01 -0700530 dasm_State *d;
531 struct error err;
532
533 if (!IS_OKAY((err = compile_function(function, NS_ANONYMOUS, env, &local, NULL,
534 &args, NULL, "/", &d))))
535 {
536 ereport(err);
537 return nil;
538 }
swissChili7e1393c2021-07-07 12:59:12 -0700539
540 del_local(&local);
541
542 value_t (*f)() = link_program(&d);
543 return f();
544}
545
swissChili6d02af42021-08-05 19:49:01 -0700546struct error compile_variable(struct variable *v, dasm_State *Dst)
swissChiliddc97542021-07-04 11:47:42 -0700547{
swissChili6d02af42021-08-05 19:49:01 -0700548 E_INIT();
swissChiliddc97542021-07-04 11:47:42 -0700549 switch (v->type)
550 {
551 case V_ARGUMENT:
swissChili7e1393c2021-07-07 12:59:12 -0700552 | mov eax, dword[ebp + (value_size * (v->number + 2))];
swissChiliddc97542021-07-04 11:47:42 -0700553 break;
554 case V_BOUND:
swissChili7e1393c2021-07-07 12:59:12 -0700555 | mov eax, dword[ebp - ((v->number + 1) * value_size)];
swissChiliddc97542021-07-04 11:47:42 -0700556 break;
557 case V_FREE:
558 // edi is the closure context pointer
swissChili7e1393c2021-07-07 12:59:12 -0700559 | mov eax, dword[edi + (v->number * value_size)];
swissChiliddc97542021-07-04 11:47:42 -0700560 break;
561 default:
swissChili6d02af42021-08-05 19:49:01 -0700562 THROW(EUNIMPL, "Sorry, can only access V_ARGUMENT, V_BOUND, and V_FREE vars");
swissChiliddc97542021-07-04 11:47:42 -0700563 }
swissChili6d02af42021-08-05 19:49:01 -0700564 OKAY();
swissChiliddc97542021-07-04 11:47:42 -0700565}
566
swissChili6d02af42021-08-05 19:49:01 -0700567struct error compile_expression(struct environment *env, struct local *local,
568 value_t val, bool tail, dasm_State **Dst)
swissChili53472e82021-05-08 16:06:32 -0700569{
swissChili6d02af42021-08-05 19:49:01 -0700570 E_INIT();
571
572 NEARVAL(val);
573
swissChili7e1393c2021-07-07 12:59:12 -0700574 if (symstreq(val, "nil") || nilp(val))
swissChili53472e82021-05-08 16:06:32 -0700575 {
576 | mov eax, (nil);
577 }
swissChili923b5362021-05-09 20:31:43 -0700578 else if (symstreq(val, "t"))
579 {
580 | mov eax, (t);
581 }
582 else if (integerp(val) || stringp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700583 {
584 | mov eax, val;
585 }
swissChili53472e82021-05-08 16:06:32 -0700586 else if (listp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700587 {
swissChili53472e82021-05-08 16:06:32 -0700588 value_t fsym = car(val);
589 value_t args = cdr(val);
590 int nargs = length(args);
swissChilib3ca4fb2021-04-20 10:33:00 -0700591
swissChili53472e82021-05-08 16:06:32 -0700592 if (!symbolp(fsym))
swissChilif3e7f182021-04-20 13:57:22 -0700593 {
swissChili6d02af42021-08-05 19:49:01 -0700594 THROW(EEXPECTED, "Function name must be a symbol");
swissChilif3e7f182021-04-20 13:57:22 -0700595 }
596
swissChili53472e82021-05-08 16:06:32 -0700597 if (symstreq(fsym, "if"))
swissChilib3ca4fb2021-04-20 10:33:00 -0700598 {
swissChili53472e82021-05-08 16:06:32 -0700599 if (nargs < 2 || nargs > 3)
swissChili6d02af42021-08-05 19:49:01 -0700600 {
601 THROW(EARGS, "Must give at least 2 arguments to if");
602 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700603
swissChili6d02af42021-08-05 19:49:01 -0700604 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili53472e82021-05-08 16:06:32 -0700605 int false_label = nextpc(local, Dst),
606 after_label = nextpc(local, Dst);
607
608 // result is in eax
609 | cmp eax, (nil);
swissChili484295d2021-07-09 21:25:55 -0700610 | je =>false_label;
swissChili53472e82021-05-08 16:06:32 -0700611
swissChili6d02af42021-08-05 19:49:01 -0700612 TRY(compile_expression(env, local, elt(args, 1), tail, Dst));
swissChili484295d2021-07-09 21:25:55 -0700613 | jmp =>after_label;
614 |=>false_label:;
swissChili53472e82021-05-08 16:06:32 -0700615 if (nargs == 3)
swissChili6d02af42021-08-05 19:49:01 -0700616 TRY(compile_expression(env, local, elt(args, 2), tail, Dst));
swissChili484295d2021-07-09 21:25:55 -0700617 |=>after_label:;
swissChili53472e82021-05-08 16:06:32 -0700618 }
swissChilia89ee442021-08-04 20:54:51 -0700619 else if (symstreq(fsym, "and") || symstreq(fsym, "or"))
620 {
621 bool or = symstreq(fsym, "or"); // false == and
622
623 // Boolean and and or, short circuit like &&/||
624 if (nargs < 1)
625 {
swissChili6d02af42021-08-05 19:49:01 -0700626 THROW(EARGS, "and & or require at least 1 argument.");
swissChilia89ee442021-08-04 20:54:51 -0700627 }
628
629 int after = nextpc(local, Dst);
630
631 for (; !nilp(args); args = cdr(args))
632 {
swissChili6d02af42021-08-05 19:49:01 -0700633 NEARVAL(args);
634
635 TRY(compile_expression(env, local, car(args), false, Dst));
swissChilia89ee442021-08-04 20:54:51 -0700636 if (!nilp(cdr(args)))
637 {
638 | cmp eax, nil;
639 if (or)
640 {
swissChilifbf525f2021-08-04 21:28:07 -0700641 | jne =>after;
swissChilia89ee442021-08-04 20:54:51 -0700642 }
643 else
644 {
swissChilifbf525f2021-08-04 21:28:07 -0700645 | je =>after;
swissChilia89ee442021-08-04 20:54:51 -0700646 }
647 }
648 }
649
650 |=>after:;
651 }
swissChilif68671f2021-07-05 14:14:44 -0700652 else if (symstreq(fsym, "progn"))
653 {
654 for (value_t val = args; !nilp(val); val = cdr(val))
655 {
swissChili6d02af42021-08-05 19:49:01 -0700656 NEARVAL(args);
657
swissChilib51552c2021-08-03 10:23:37 -0700658 bool t = tail && nilp(cdr(val));
swissChili6d02af42021-08-05 19:49:01 -0700659 TRY(compile_expression(env, local, car(val), t, Dst));
swissChilif68671f2021-07-05 14:14:44 -0700660 }
661 }
swissChili67bdf282021-06-06 18:46:08 -0700662 else if (symstreq(fsym, "let1"))
663 {
664 if (nargs < 2)
665 {
swissChili6d02af42021-08-05 19:49:01 -0700666 THROW(EARGS, "Must give at least 2 arguments to let1");
swissChili67bdf282021-06-06 18:46:08 -0700667 }
668 value_t binding = car(args);
669 value_t rest = cdr(args);
670
swissChili6d02af42021-08-05 19:49:01 -0700671 NEARVAL(binding);
swissChili67bdf282021-06-06 18:46:08 -0700672 if (length(binding) != 2)
673 {
swissChili6d02af42021-08-05 19:49:01 -0700674 THROW(EARGS, "Binding list in let1 must contain exactly two entries");
swissChili67bdf282021-06-06 18:46:08 -0700675 }
676
swissChili6d02af42021-08-05 19:49:01 -0700677 NEARVAL(rest);
678
swissChili67bdf282021-06-06 18:46:08 -0700679 value_t name = car(binding);
680 value_t value = car(cdr(binding));
681
swissChili6d02af42021-08-05 19:49:01 -0700682 TRY(compile_expression(env, local, value, false, Dst));
swissChili67bdf282021-06-06 18:46:08 -0700683
684 int i = local_alloc(local);
685
686 add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i);
687
swissChili7e1393c2021-07-07 12:59:12 -0700688 | mov dword[ebp - ((i + 1) * value_size)], eax;
swissChili67bdf282021-06-06 18:46:08 -0700689
690 for (; !nilp(rest); rest = cdr(rest))
691 {
swissChilib51552c2021-08-03 10:23:37 -0700692 bool t = tail && nilp(cdr(rest));
swissChili6d02af42021-08-05 19:49:01 -0700693 NEARVAL(rest);
694 TRY(compile_expression(env, local, car(rest), t, Dst));
swissChili67bdf282021-06-06 18:46:08 -0700695 }
696
697 local_free(local, i);
698 }
swissChilie9fec8b2021-06-22 13:59:33 -0700699 else if (symstreq(fsym, "gc"))
700 {
701 if (nargs)
702 {
swissChili6d02af42021-08-05 19:49:01 -0700703 THROW(EARGS, "gc takes no arguments");
swissChilie9fec8b2021-06-22 13:59:33 -0700704 }
705
706 | run_gc;
707 }
swissChili6b47b6d2021-06-30 22:08:55 -0700708 else if (symstreq(fsym, "quote"))
709 {
710 if (nargs != 1)
swissChili6d02af42021-08-05 19:49:01 -0700711 THROW(EARGS, "quote should take exactly 1 argument");
swissChili6b47b6d2021-06-30 22:08:55 -0700712
713 // Simple!
714 | mov eax, (car(args));
715 }
716 else if (symstreq(fsym, "backquote"))
717 {
718 if (nargs != 1)
swissChili6d02af42021-08-05 19:49:01 -0700719 THROW(EARGS, "backquote should take exactly 1 argument");
swissChili6b47b6d2021-06-30 22:08:55 -0700720
swissChili6d02af42021-08-05 19:49:01 -0700721 TRY(compile_backquote(env, local, car(args), Dst));
swissChili6b47b6d2021-06-30 22:08:55 -0700722 }
swissChili74348422021-07-04 13:23:24 -0700723 else if (symstreq(fsym, "function"))
724 {
725 if (nargs != 1)
726 {
swissChili6d02af42021-08-05 19:49:01 -0700727 THROW(EARGS, "function should take exactly 1 argument");
swissChili74348422021-07-04 13:23:24 -0700728 }
729
swissChili6d02af42021-08-05 19:49:01 -0700730 NEARVAL(args);
swissChili74348422021-07-04 13:23:24 -0700731 if (!symbolp(car(args)))
732 {
swissChili6d02af42021-08-05 19:49:01 -0700733 THROW(EINVALID, "argument to function should be a symbol resolvable at "
swissChili7e1393c2021-07-07 12:59:12 -0700734 "compile time");
swissChili74348422021-07-04 13:23:24 -0700735 }
736
swissChilia89ee442021-08-04 20:54:51 -0700737 char *name = (char *)(car(args) ^ SYMBOL_TAG);
swissChili74348422021-07-04 13:23:24 -0700738
swissChilia89ee442021-08-04 20:54:51 -0700739 if (!strcmp(name, local->current_function_name))
740 {
741 | push 0;
742 | push local->args;
743 | push <1;
744 | call_extern create_closure;
745 }
746 else
747 {
748 struct function *f = find_function(env, name);
749
750 if (!f)
751 {
swissChili6d02af42021-08-05 19:49:01 -0700752 THROW(EINVALID, "Function `%s' does not exist", (char *)(car(args) ^ SYMBOL_TAG));
swissChilia89ee442021-08-04 20:54:51 -0700753 }
754 value_t closure = create_closure(f->code_ptr, f->args, 0);
755 | mov eax, (closure);
756 }
swissChili74348422021-07-04 13:23:24 -0700757 }
swissChili6b47b6d2021-06-30 22:08:55 -0700758 else if (symstreq(fsym, "list"))
759 {
swissChili484295d2021-07-09 21:25:55 -0700760 | push (nil);
swissChili6b47b6d2021-06-30 22:08:55 -0700761
762 for (int i = nargs - 1; i >= 0; i--)
763 {
swissChili6d02af42021-08-05 19:49:01 -0700764 TRY(compile_expression(env, local, elt(args, i), false, Dst));
swissChili6b47b6d2021-06-30 22:08:55 -0700765
swissChili6b47b6d2021-06-30 22:08:55 -0700766 | push eax;
swissChili53e7cd12021-08-02 21:55:53 -0700767 | call_extern cons;
swissChili6b47b6d2021-06-30 22:08:55 -0700768 | add esp, (2 * value_size);
swissChili6b47b6d2021-06-30 22:08:55 -0700769 | push eax;
770 }
swissChili6d02af42021-08-05 19:49:01 -0700771 | pop eax;
swissChili6b47b6d2021-06-30 22:08:55 -0700772 }
swissChiliddc97542021-07-04 11:47:42 -0700773 else if (symstreq(fsym, "lambda"))
774 {
775 // Compile the function with this as the parent scope
776 struct local new_local;
777 int nargs_out;
swissChili6d02af42021-08-05 19:49:01 -0700778 dasm_State *d;
779 TRY(compile_function(
780 args, NS_ANONYMOUS, env, &new_local, local, &nargs_out,
781 "recurse", local->current_file_path, &d));
swissChiliddc97542021-07-04 11:47:42 -0700782
783 // Link the function
swissChilif68671f2021-07-05 14:14:44 -0700784 void *func_ptr = link_program(&d);
swissChiliddc97542021-07-04 11:47:42 -0700785
786 // Create a closure object with the correct number of captures at
787 // runtime
swissChili484295d2021-07-09 21:25:55 -0700788 | push (new_local.num_closure_slots);
789 | push (nargs_out);
790 | push (func_ptr);
swissChili53e7cd12021-08-02 21:55:53 -0700791 | call_extern create_closure;
swissChiliddc97542021-07-04 11:47:42 -0700792 | add esp, 12;
793
794 // Walk the generated local scope for V_FREE variables, since each
795 // of these exists in our scope (or higher), evaluate it and set it
796 // as a member of the lambda capture.
797
798 for (struct variable *var = new_local.first; var; var = var->prev)
799 {
800 if (var->type == V_FREE)
801 {
802 // Closure in eax
803 | push eax;
804 // Variable now in eax
swissChili6d02af42021-08-05 19:49:01 -0700805 TRY(compile_variable(find_variable(local, var->name), Dst));
swissChiliddc97542021-07-04 11:47:42 -0700806 | push eax;
807
swissChiliddc97542021-07-04 11:47:42 -0700808 // The capture offset
swissChili484295d2021-07-09 21:25:55 -0700809 | push (var->number);
swissChili53e7cd12021-08-02 21:55:53 -0700810 | call_extern set_closure_capture_variable;
swissChiliddc97542021-07-04 11:47:42 -0700811 // Skip the value and index
812 | add esp, 8;
813 // Pop the closure back in to eax
814 | pop eax;
815 }
816 }
817
818 // Closure is still in eax
819
820 dasm_free(&d);
swissChili708d4c42021-07-04 17:40:07 -0700821 del_local(&new_local);
swissChiliddc97542021-07-04 11:47:42 -0700822 }
swissChili7e1393c2021-07-07 12:59:12 -0700823 else if (symstreq(fsym, "eval"))
824 {
825 if (nargs != 1)
826 {
swissChili6d02af42021-08-05 19:49:01 -0700827 THROW(EARGS, "eval takes exactly 1 argument");
swissChili7e1393c2021-07-07 12:59:12 -0700828 }
829
swissChili6d02af42021-08-05 19:49:01 -0700830 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili7e1393c2021-07-07 12:59:12 -0700831 | push eax;
swissChili484295d2021-07-09 21:25:55 -0700832 | push (env);
swissChili53e7cd12021-08-02 21:55:53 -0700833 | call_extern eval;
swissChili7e1393c2021-07-07 12:59:12 -0700834 }
835 else if (symstreq(fsym, "load"))
836 {
837 if (nargs != 1)
838 {
swissChili6d02af42021-08-05 19:49:01 -0700839 THROW(EARGS, "load takes exactly 1 argument, %d given", nargs);
swissChili7e1393c2021-07-07 12:59:12 -0700840 }
841
swissChili6d02af42021-08-05 19:49:01 -0700842 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili7e1393c2021-07-07 12:59:12 -0700843 | push eax;
swissChili484295d2021-07-09 21:25:55 -0700844 | push (local->current_file_path);
845 | push (env);
swissChili53e7cd12021-08-02 21:55:53 -0700846 | call_extern load_relative;
swissChili7e1393c2021-07-07 12:59:12 -0700847 }
swissChili53472e82021-05-08 16:06:32 -0700848 else
849 {
swissChili74348422021-07-04 13:23:24 -0700850 char *name = (char *)(fsym ^ SYMBOL_TAG);
851 struct function *func = find_function(env, name);
swissChili7e1393c2021-07-07 12:59:12 -0700852
swissChili74348422021-07-04 13:23:24 -0700853 bool is_recursive = false;
swissChili15f1cae2021-07-05 19:08:47 -0700854 struct args *nargs_needed = NULL;
swissChili53472e82021-05-08 16:06:32 -0700855
swissChili53e7cd12021-08-02 21:55:53 -0700856 // The number of arguments actually passed on the stack,
857 // i.e. all varargs are 1.
swissChilib51552c2021-08-03 10:23:37 -0700858 int real_nargs;
swissChili53e7cd12021-08-02 21:55:53 -0700859
swissChili7e1393c2021-07-07 12:59:12 -0700860 if (local->current_function_name &&
861 symstreq(fsym, local->current_function_name))
swissChilif1ba8c12021-07-02 18:45:38 -0700862 {
swissChili74348422021-07-04 13:23:24 -0700863 is_recursive = true;
swissChili15f1cae2021-07-05 19:08:47 -0700864 nargs_needed = local->args;
swissChili74348422021-07-04 13:23:24 -0700865 }
866 else
867 {
868 if (func == NULL)
869 {
swissChili6d02af42021-08-05 19:49:01 -0700870 THROW(EINVALID, "Function %s undefined", name);
swissChili74348422021-07-04 13:23:24 -0700871 }
872
swissChili15f1cae2021-07-05 19:08:47 -0700873 nargs_needed = func->args;
swissChili74348422021-07-04 13:23:24 -0700874 }
875
swissChili15f1cae2021-07-05 19:08:47 -0700876 if (!are_args_acceptable(nargs_needed, nargs))
swissChili74348422021-07-04 13:23:24 -0700877 {
swissChili6d02af42021-08-05 19:49:01 -0700878 THROW(EARGS,
879 "wrong number of args in function call: %s, "
880 "want %d args but given %d\n",
881 name, nargs_needed->num_required, nargs);
swissChilif1ba8c12021-07-02 18:45:38 -0700882 }
swissChili53472e82021-05-08 16:06:32 -0700883
swissChili53e7cd12021-08-02 21:55:53 -0700884 int total_taken = nargs_needed->num_optional +
885 nargs_needed->num_required;
886
swissChilib51552c2021-08-03 10:23:37 -0700887 real_nargs = total_taken + (nargs_needed->variadic ? 1 : 0);
swissChili53e7cd12021-08-02 21:55:53 -0700888
swissChili74348422021-07-04 13:23:24 -0700889 if (is_recursive || func->namespace == NS_FUNCTION)
swissChili53472e82021-05-08 16:06:32 -0700890 {
swissChili15f1cae2021-07-05 19:08:47 -0700891 int nargs = length(args);
892
swissChili484295d2021-07-09 21:25:55 -0700893 int line = cons_line(val);
894 char *file = cons_file(val);
895
896 if (nargs_needed->variadic)
swissChili15f1cae2021-07-05 19:08:47 -0700897 {
swissChili484295d2021-07-09 21:25:55 -0700898 | push (nil);
899 }
900
901 if (nargs > total_taken && nargs_needed->variadic)
902 {
903 // We are passing varargs, which means we need to make a list
904
905 for (int i = nargs - 1; i >= total_taken; i--)
906 {
swissChili6d02af42021-08-05 19:49:01 -0700907 TRY(compile_expression(env, local, elt(args, i), false, Dst));
swissChili484295d2021-07-09 21:25:55 -0700908 | push eax;
swissChili53e7cd12021-08-02 21:55:53 -0700909 | call_extern cons;
swissChili484295d2021-07-09 21:25:55 -0700910 | add esp, 8;
911 | push eax;
912 }
swissChili15f1cae2021-07-05 19:08:47 -0700913 }
914
swissChili7e1393c2021-07-07 12:59:12 -0700915 for (int i = nargs_needed->num_optional - 1;
916 i >= nargs - nargs_needed->num_required; i--)
swissChili15f1cae2021-07-05 19:08:47 -0700917 {
918 // Push the default optional values
swissChili484295d2021-07-09 21:25:55 -0700919 | push (nargs_needed->optional_arguments[i].value);
swissChili15f1cae2021-07-05 19:08:47 -0700920 }
921
swissChili484295d2021-07-09 21:25:55 -0700922 int min = MIN(nargs, total_taken);
923
924 for (int i = min - 1; i >= 0; i--)
swissChili2999dd12021-07-02 14:19:53 -0700925 {
swissChili6d02af42021-08-05 19:49:01 -0700926 TRY(compile_expression(env, local, elt(args, i), false, Dst));
swissChili2999dd12021-07-02 14:19:53 -0700927 | push eax;
928 }
swissChili15f1cae2021-07-05 19:08:47 -0700929
swissChili74348422021-07-04 13:23:24 -0700930 if (is_recursive)
931 {
swissChilib51552c2021-08-03 10:23:37 -0700932 if (tail)
933 {
934 // Move all the arguments pushed to the stack
935 // back up to the argument bit of the stack.
936
937 for (int i = 0; i < real_nargs; i++)
938 {
939 | pop eax;
940 | mov dword[ebp + (value_size * (i + 2))], eax;
941 }
942
943 // Jmp back to start
944 | mov esp, ebp;
945 | pop ebp;
946 | jmp <1;
947 }
948 else
949 {
950 | call <1;
951 }
swissChili74348422021-07-04 13:23:24 -0700952 }
953 else
954 {
swissChili484295d2021-07-09 21:25:55 -0700955 // | mov ebx, (func->code_addr);
956 | call_extern func->code_addr;
swissChili74348422021-07-04 13:23:24 -0700957 }
swissChili53e7cd12021-08-02 21:55:53 -0700958 | add esp, (real_nargs * value_size);
swissChili2999dd12021-07-02 14:19:53 -0700959 // result in eax
960 }
961 else if (func->namespace == NS_MACRO)
962 {
swissChili7e1393c2021-07-07 12:59:12 -0700963 // Make sure that the stuff allocated by the macro isn't in a
964 // pool
swissChilif68671f2021-07-05 14:14:44 -0700965 unsigned char pool = push_pool(0);
966
swissChili2999dd12021-07-02 14:19:53 -0700967 value_t expanded_to = call_list(func, args);
968
swissChilif68671f2021-07-05 14:14:44 -0700969 pop_pool(pool);
970
swissChili6d02af42021-08-05 19:49:01 -0700971 TRY(compile_expression(env, local, expanded_to, false, Dst));
swissChili2999dd12021-07-02 14:19:53 -0700972 }
swissChili53472e82021-05-08 16:06:32 -0700973 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700974 }
swissChili923b5362021-05-09 20:31:43 -0700975 else if (symbolp(val))
976 {
swissChili7e1393c2021-07-07 12:59:12 -0700977 if (symstreq(val, "+current-file+"))
swissChilie9fec8b2021-06-22 13:59:33 -0700978 {
swissChili7e1393c2021-07-07 12:59:12 -0700979 value_t file_name_val = strval(local->current_file_path);
980
981 | mov eax, (file_name_val);
swissChilie9fec8b2021-06-22 13:59:33 -0700982 }
swissChili7e1393c2021-07-07 12:59:12 -0700983 else
984 {
985 struct variable *v =
986 find_variable(local, (char *)(val ^ SYMBOL_TAG));
swissChili923b5362021-05-09 20:31:43 -0700987
swissChili7e1393c2021-07-07 12:59:12 -0700988 if (!v)
989 {
swissChili6d02af42021-08-05 19:49:01 -0700990 THROW(EINVALID, "Variable `%s' unbound", (char *)(val ^ SYMBOL_TAG));
swissChili7e1393c2021-07-07 12:59:12 -0700991 }
992
swissChili6d02af42021-08-05 19:49:01 -0700993 TRY(compile_variable(v, Dst));
swissChili7e1393c2021-07-07 12:59:12 -0700994 }
swissChili923b5362021-05-09 20:31:43 -0700995 }
swissChilia89ee442021-08-04 20:54:51 -0700996 else if (closurep(val))
997 {
998 | mov eax, val;
999 }
1000 else
1001 {
1002 printval(val, 1);
swissChili6d02af42021-08-05 19:49:01 -07001003 THROW(EUNIMPL, "Don't know how to compile this, sorry.");
swissChilia89ee442021-08-04 20:54:51 -07001004 }
swissChili6d02af42021-08-05 19:49:01 -07001005
1006 OKAY();
swissChilib3ca4fb2021-04-20 10:33:00 -07001007}
swissChilif3e7f182021-04-20 13:57:22 -07001008
swissChili923b5362021-05-09 20:31:43 -07001009struct variable *add_variable(struct local *local, enum var_type type,
1010 char *name, int number)
1011{
1012 struct variable *var = malloc(sizeof(struct variable));
1013 var->prev = local->first;
1014 var->type = type;
1015 var->name = name;
1016 var->number = number;
1017
1018 local->first = var;
1019
1020 return var;
1021}
1022
1023void destroy_local(struct local *local)
1024{
1025 for (struct variable *v = local->first; v;)
1026 {
1027 struct variable *t = v;
1028 v = v->prev;
1029 free(t);
1030 }
1031}
1032
1033struct variable *find_variable(struct local *local, char *name)
1034{
1035 struct variable *v = local->first;
1036
1037 for (; v && strcmp(v->name, name) != 0; v = v->prev)
swissChili7e1393c2021-07-07 12:59:12 -07001038 {
1039 }
swissChili923b5362021-05-09 20:31:43 -07001040
swissChiliddc97542021-07-04 11:47:42 -07001041 if (!v)
1042 {
1043 if (local->parent)
1044 {
1045 v = find_variable(local->parent, name);
1046
1047 if (v)
1048 {
swissChili15f1cae2021-07-05 19:08:47 -07001049 // We found this in a parent scope, add it as a V_FREE variable
1050 // to skip the search.
swissChili7e1393c2021-07-07 12:59:12 -07001051 v = add_variable(local, V_FREE, name,
1052 local->num_closure_slots++);
swissChiliddc97542021-07-04 11:47:42 -07001053 }
1054 }
1055 }
swissChili923b5362021-05-09 20:31:43 -07001056 return v;
1057}
swissChili2999dd12021-07-02 14:19:53 -07001058
swissChiliddc97542021-07-04 11:47:42 -07001059extern value_t _call_list(void *addr, value_t list, value_t *edi);
swissChili2999dd12021-07-02 14:19:53 -07001060
swissChili7e1393c2021-07-07 12:59:12 -07001061value_t call_list_args(void *code_ptr, struct args *args, value_t list,
1062 void *data)
swissChili2999dd12021-07-02 14:19:53 -07001063{
swissChili15f1cae2021-07-05 19:08:47 -07001064 list = deep_copy(list);
swissChili484295d2021-07-09 21:25:55 -07001065
swissChili15f1cae2021-07-05 19:08:47 -07001066 int nargs = length(list);
1067
swissChili484295d2021-07-09 21:25:55 -07001068 value_t *val = &list;
swissChili15f1cae2021-07-05 19:08:47 -07001069
1070 for (value_t i = list; !nilp(i); i = cdr(i))
1071 {
1072 val = cdrref(i);
1073 }
1074
1075 int total_required = args->num_required + args->num_optional;
1076
1077 if (nargs > total_required)
1078 {
1079 // Take the remainder of the list and put it as the last item in the
1080 // list.
1081 value_t trailing = cxdr(list, total_required);
1082 value_t last_item = cons(trailing, nil);
1083
1084 *cxdrref(&list, total_required) = last_item;
1085 }
1086 else if (nargs < total_required)
1087 {
1088 for (int i = nargs - args->num_required; i < args->num_optional; i++)
1089 {
1090 // Append the i-th defualt argument
1091 value_t appended = cons(args->optional_arguments[i].value, nil);
1092 *val = appended;
1093 val = cdrref(appended);
1094 }
1095 }
1096
1097 // We want to call this if we pass the correct # of arguments or less, just
1098 // not if we have already passed varargs. Appends a nil argument.
1099 if (nargs <= total_required)
1100 {
1101 // Enough real arguments but no variadic arguments. Pass a nil list.
1102 *val = cons(nil, nil);
1103 }
1104
1105 return _call_list(code_ptr, list, data);
1106}
1107
1108value_t call_list(struct function *fun, value_t list)
1109{
1110 return call_list_args(fun->code_ptr, fun->args, list, NULL);
swissChiliddc97542021-07-04 11:47:42 -07001111}
1112
1113value_t call_list_closure(struct closure *c, value_t list)
1114{
swissChili15f1cae2021-07-05 19:08:47 -07001115 return call_list_args(c->function, c->args, list, c->data);
1116}
1117
1118struct args *new_args()
1119{
1120 struct args *a = malloc(sizeof(struct args));
1121 a->num_optional = 0;
1122 a->num_required = 0;
1123 a->variadic = false;
1124
1125 return a;
1126}
1127
swissChili7e1393c2021-07-07 12:59:12 -07001128struct args *add_optional_arg(struct args *args, value_t name, value_t value)
swissChili15f1cae2021-07-05 19:08:47 -07001129{
1130 int i = args->num_optional++;
swissChili7e1393c2021-07-07 12:59:12 -07001131 args =
1132 realloc(args, sizeof(struct args) + sizeof(struct optional_argument) *
1133 args->num_optional);
swissChili15f1cae2021-07-05 19:08:47 -07001134
swissChili7e1393c2021-07-07 12:59:12 -07001135 args->optional_arguments[i] = (struct optional_argument){
1136 .value = value,
1137 .name = name,
swissChili15f1cae2021-07-05 19:08:47 -07001138 };
1139
1140 return args;
1141}
1142
1143bool are_args_acceptable(struct args *args, int number)
1144{
1145 if (args->variadic)
1146 {
1147 return number >= args->num_required;
1148 }
1149 else
1150 {
1151 return number >= args->num_required &&
swissChili7e1393c2021-07-07 12:59:12 -07001152 number <= args->num_required + args->num_optional;
swissChili15f1cae2021-07-05 19:08:47 -07001153 }
1154}
1155
swissChili6d02af42021-08-05 19:49:01 -07001156struct error list_to_args(struct environment *env, value_t list,
1157 struct local *local, struct args **a)
swissChili15f1cae2021-07-05 19:08:47 -07001158{
swissChili6d02af42021-08-05 19:49:01 -07001159 E_INIT();
1160
swissChili15f1cae2021-07-05 19:08:47 -07001161 struct args *args = new_args();
1162
1163 bool in_optional = false;
1164
1165 for (value_t i = list; !nilp(i); i = cdr(i))
1166 {
1167 value_t val = car(i);
swissChili6d02af42021-08-05 19:49:01 -07001168 NEARVAL(i);
1169
swissChili15f1cae2021-07-05 19:08:47 -07001170 if (symbolp(val))
1171 {
1172 if (!args->variadic && symstreq(val, "&"))
1173 {
1174 i = cdr(i);
1175 value_t name = car(i);
1176
1177 if (!symbolp(name))
1178 {
swissChili6d02af42021-08-05 19:49:01 -07001179 THROW(EEXPECTED, "You must provide a symbol after & in an argument list "
1180 "to bind the\n"
1181 "variadic arguments to.");
swissChili15f1cae2021-07-05 19:08:47 -07001182 }
1183
1184 args->variadic = true;
1185
1186 add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG),
swissChili7e1393c2021-07-07 12:59:12 -07001187 args->num_optional + args->num_required);
swissChili15f1cae2021-07-05 19:08:47 -07001188
1189 continue;
1190 }
1191
1192 if (!in_optional)
1193 {
swissChili7e1393c2021-07-07 12:59:12 -07001194 add_variable(local, V_ARGUMENT, (char *)(val ^ SYMBOL_TAG),
1195 args->num_required++);
swissChili15f1cae2021-07-05 19:08:47 -07001196 }
1197 else
1198 {
1199 char *name = (char *)(val ^ SYMBOL_TAG);
1200 if (name[0] == '&')
1201 {
swissChili6d02af42021-08-05 19:49:01 -07001202 THROW(EINVALID, "Non-optional argument following optional arguments "
1203 "starts with a &\n"
1204 "did you mean to declare a variadic argument? If so "
1205 "leave a space\n"
1206 "between the & and name.");
swissChili15f1cae2021-07-05 19:08:47 -07001207 }
1208 else
1209 {
swissChili6d02af42021-08-05 19:49:01 -07001210 THROW(EINVALID, "Cannot define a non-optional argument after an "
1211 "optional one.");
swissChili15f1cae2021-07-05 19:08:47 -07001212 }
1213 }
1214 }
1215 else if (listp(val))
1216 {
swissChili6d02af42021-08-05 19:49:01 -07001217 NEARVAL(val);
1218
swissChili15f1cae2021-07-05 19:08:47 -07001219 in_optional = true;
1220 int len = length(val);
1221
1222 if (len != 2)
1223 {
swissChili6d02af42021-08-05 19:49:01 -07001224 THROW(EINVALID, "A list defining an optional value must be structured like "
1225 "(name expr)\n"
1226 "with exactly two arguments.");
swissChili15f1cae2021-07-05 19:08:47 -07001227 }
1228
1229 value_t name = car(val);
1230 value_t expr = car(cdr(val));
1231
1232 value_t function = cons(nil, cons(expr, nil));
1233
swissChili6d02af42021-08-05 19:49:01 -07001234 dasm_State *d;
1235 TRY(compile_function(function, NS_ANONYMOUS, env, NULL, NULL, NULL,
1236 NULL, local->current_file_path, &d));
swissChili15f1cae2021-07-05 19:08:47 -07001237
1238 // TODO: GC stack top!
1239 value_t (*compiled)() = link_program(&d);
1240
1241 value_t value = compiled();
1242 args = add_optional_arg(args, name, value);
1243
swissChili7e1393c2021-07-07 12:59:12 -07001244 add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG),
1245 args->num_required + args->num_optional - 1);
swissChili15f1cae2021-07-05 19:08:47 -07001246 }
1247 }
1248
swissChili6d02af42021-08-05 19:49:01 -07001249 *a = args;
1250 OKAY();
swissChili15f1cae2021-07-05 19:08:47 -07001251}
1252
1253void display_args(struct args *args)
1254{
1255 printf("Args object taking %d require arguments and %d optionals:\n",
swissChili7e1393c2021-07-07 12:59:12 -07001256 args->num_required, args->num_optional);
swissChili15f1cae2021-07-05 19:08:47 -07001257
1258 for (int i = 0; i < args->num_optional; i++)
1259 {
swissChili7e1393c2021-07-07 12:59:12 -07001260 printf(" %d\t%s\n", i,
1261 (char *)(args->optional_arguments[i].name ^ SYMBOL_TAG));
swissChili15f1cae2021-07-05 19:08:47 -07001262 printval(args->optional_arguments[i].value, 2);
1263 }
swissChili2999dd12021-07-02 14:19:53 -07001264}