blob: 6549ecc07e491690c1ff5a2b76ac7cb788570669 [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
swissChilic0acce42022-07-31 13:38:17 -0700108 if (f->args)
109 free(f->args);
swissChili708d4c42021-07-04 17:40:07 -0700110 free(f);
111 }
swissChilif68671f2021-07-05 14:14:44 -0700112
113 for (struct loaded_file *next, *l = env->first_loaded; l; l = next)
114 {
115 next = l->previous;
116 free(l->resolved_path);
117 free(l);
118 }
swissChili7e1393c2021-07-07 12:59:12 -0700119
120 free(env);
swissChilif68671f2021-07-05 14:14:44 -0700121}
122
123void add_load(struct environment *env, char *path)
124{
125 static char buffer[512];
126 long size = readlink(path, buffer, 512);
127 buffer[size] = '\0';
128 char *resolved = strdup(buffer);
129
130 struct loaded_file *f = malloc(sizeof(struct loaded_file));
131 f->resolved_path = resolved;
132 f->previous = env->first_loaded;
133 env->first_loaded = f;
swissChili708d4c42021-07-04 17:40:07 -0700134}
135
swissChili6d02af42021-08-05 19:49:01 -0700136struct error compile_function(value_t args, enum namespace namespace,
137 struct environment *env,
138 struct local *local_out,
139 struct local *local_parent,
140 struct args **args_out, char *name,
141 char *path,
142 dasm_State **state)
swissChilif1ba8c12021-07-02 18:45:38 -0700143{
swissChilia7568dc2021-08-08 16:52:52 -0700144 UNUSED(namespace);
145
swissChili6d02af42021-08-05 19:49:01 -0700146 E_INIT();
147
swissChilif1ba8c12021-07-02 18:45:38 -0700148 dasm_State *d;
149 dasm_State **Dst = &d;
150
swissChili484295d2021-07-09 21:25:55 -0700151 |.section code, imports;
swissChilif1ba8c12021-07-02 18:45:38 -0700152 dasm_init(&d, DASM_MAXSECTION);
153
154 |.globals lbl_;
155 void *labels[lbl__MAX];
156 dasm_setupglobal(&d, labels, lbl__MAX);
157
158 |.actionlist lisp_actions;
159 dasm_setup(&d, lisp_actions);
160
161 struct local local;
162 local.parent = NULL;
163 local.first = NULL;
164 local.num_vars = 0;
165 local.npc = 8;
166 local.nextpc = 0;
167 local.stack_slots = malloc(sizeof(bool) * 4);
168 memset(local.stack_slots, 0, sizeof(bool) * 4);
169 local.num_stack_slots = 4;
170 local.num_stack_entries = 0;
swissChiliddc97542021-07-04 11:47:42 -0700171 local.num_closure_slots = 0;
172 local.parent = local_parent;
swissChili74348422021-07-04 13:23:24 -0700173 local.current_function_name = name;
swissChili7e1393c2021-07-07 12:59:12 -0700174 local.current_file_path = path;
swissChilif1ba8c12021-07-02 18:45:38 -0700175
176 dasm_growpc(&d, local.npc);
177
swissChilif1ba8c12021-07-02 18:45:38 -0700178 value_t arglist = car(args);
179 value_t body = cdr(args);
180
swissChili15f1cae2021-07-05 19:08:47 -0700181 // This will add the arguments to local too.
swissChili6d02af42021-08-05 19:49:01 -0700182 struct args *ar;
183 TRY(list_to_args(env, arglist, &local, &ar));
swissChili15f1cae2021-07-05 19:08:47 -0700184 local.args = ar;
swissChili74348422021-07-04 13:23:24 -0700185
swissChili15f1cae2021-07-05 19:08:47 -0700186 if (!ar)
swissChilif1ba8c12021-07-02 18:45:38 -0700187 {
swissChili6d02af42021-08-05 19:49:01 -0700188 NEARVAL(arglist);
189 THROW(EMALFORMED, "Malformed argument list");
swissChilif1ba8c12021-07-02 18:45:38 -0700190 }
191
192 for (value_t body_ = body; !nilp(body_); body_ = cdr(body_))
193 {
swissChilifc5c9412021-08-08 19:08:26 -0700194 TRY(walk_and_alloc(env, &local, carref(body_), false));
swissChilif1ba8c12021-07-02 18:45:38 -0700195 }
196
swissChili484295d2021-07-09 21:25:55 -0700197 | setup (local.num_stack_entries);
swissChilif1ba8c12021-07-02 18:45:38 -0700198
199 memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool));
200 local.num_stack_entries = 0;
201
202 for (; !nilp(body); body = cdr(body))
203 {
swissChilib51552c2021-08-03 10:23:37 -0700204 bool tail = nilp(cdr(body));
swissChili6d02af42021-08-05 19:49:01 -0700205 TRY(compile_expression(env, &local, car(body), tail, Dst));
swissChilif1ba8c12021-07-02 18:45:38 -0700206 }
207
208 | cleanup;
209
210 if (local_out)
211 *local_out = local;
swissChilic0acce42022-07-31 13:38:17 -0700212 else
213 del_local(&local);
swissChilif1ba8c12021-07-02 18:45:38 -0700214
swissChili15f1cae2021-07-05 19:08:47 -0700215 if (args_out)
216 *args_out = ar;
swissChilif1ba8c12021-07-02 18:45:38 -0700217
swissChilic0acce42022-07-31 13:38:17 -0700218 if (!local_out && !args_out)
219 free(ar);
220
swissChili6d02af42021-08-05 19:49:01 -0700221 *state = d;
222
223 OKAY();
swissChilif1ba8c12021-07-02 18:45:38 -0700224}
225
swissChili6d02af42021-08-05 19:49:01 -0700226struct error compile_tl(value_t val, struct environment *env, char *fname)
swissChilica107a02021-04-14 12:07:30 -0700227{
swissChili6d02af42021-08-05 19:49:01 -0700228 E_INIT();
229
230 NEARVAL(val);
231
swissChili53472e82021-05-08 16:06:32 -0700232 if (!listp(val))
swissChili6d02af42021-08-05 19:49:01 -0700233 {
234 THROW(EEXPECTED, "Top level form must be a list");
235 }
swissChilica107a02021-04-14 12:07:30 -0700236
swissChili53472e82021-05-08 16:06:32 -0700237 value_t form = car(val);
238 value_t args = cdr(val);
239
swissChili2999dd12021-07-02 14:19:53 -0700240 if (symstreq(form, "defun") || symstreq(form, "defmacro"))
swissChili8fc5e2f2021-04-22 13:45:10 -0700241 {
swissChili2999dd12021-07-02 14:19:53 -0700242 enum namespace namespace = NS_FUNCTION;
243
244 if (symstreq(form, "defmacro"))
swissChilia89ee442021-08-04 20:54:51 -0700245 namespace = NS_MACRO;
swissChili2999dd12021-07-02 14:19:53 -0700246
swissChili8fc5e2f2021-04-22 13:45:10 -0700247 struct local local;
swissChili15f1cae2021-07-05 19:08:47 -0700248 struct args *a;
swissChili74348422021-07-04 13:23:24 -0700249 char *name = (char *)(car(args) ^ SYMBOL_TAG);
swissChilif68671f2021-07-05 14:14:44 -0700250
swissChili6d02af42021-08-05 19:49:01 -0700251 dasm_State *d;
252 TRY(compile_function(cdr(args), namespace, env, &local,
253 NULL, &a, name, fname, &d));
swissChilia820dea2021-05-09 16:46:55 -0700254
swissChili7e1393c2021-07-07 12:59:12 -0700255 add_function(env, name, link_program(&d), a, namespace);
swissChili8fc5e2f2021-04-22 13:45:10 -0700256
swissChili53472e82021-05-08 16:06:32 -0700257 dasm_free(&d);
swissChili708d4c42021-07-04 17:40:07 -0700258 del_local(&local);
swissChili67bdf282021-06-06 18:46:08 -0700259 }
swissChilif68671f2021-07-05 14:14:44 -0700260 else if (symstreq(form, "progn"))
261 {
262 for (value_t val = args; !nilp(val); val = cdr(val))
263 {
swissChili6d02af42021-08-05 19:49:01 -0700264 TRY(compile_tl(car(val), env, fname));
swissChilif68671f2021-07-05 14:14:44 -0700265 }
266 }
swissChili484295d2021-07-09 21:25:55 -0700267 else if (symstreq(form, "load"))
268 {
269 if (length(args) != 1)
270 {
swissChili6d02af42021-08-05 19:49:01 -0700271 NEARVAL(args);
272 THROW(EARGS, "load expects exactly 1 argument, %d given",
273 length(args));
swissChili484295d2021-07-09 21:25:55 -0700274 }
275 load_relative(env, fname, car(args));
276 }
swissChili6d02af42021-08-05 19:49:01 -0700277
278 OKAY();
swissChili67bdf282021-06-06 18:46:08 -0700279}
280
swissChilifc5c9412021-08-08 19:08:26 -0700281struct error walk_and_alloc(struct environment *env, struct local *local, value_t *bp, bool quoted)
swissChili67bdf282021-06-06 18:46:08 -0700282{
swissChilifc5c9412021-08-08 19:08:26 -0700283 // Note: this kind of sucks. Some of the quote-handling code is
284 // duplicated here and compile_expression. TODO: refactor
285 // eventually.
286
swissChili36f2c692021-08-08 14:31:44 -0700287 E_INIT();
288
289 value_t body = *bp;
290
swissChili67bdf282021-06-06 18:46:08 -0700291 if (!listp(body))
swissChili36f2c692021-08-08 14:31:44 -0700292 OKAY();
swissChili67bdf282021-06-06 18:46:08 -0700293
294 value_t args = cdr(body);
295
swissChilifc5c9412021-08-08 19:08:26 -0700296 if (!quoted && symstreq(car(body), "let1"))
swissChili67bdf282021-06-06 18:46:08 -0700297 {
298 int slot = local_alloc(local);
299
300 value_t expr = cdr(args);
swissChilif1ba8c12021-07-02 18:45:38 -0700301 for (; !nilp(expr); expr = cdr(expr))
302 {
swissChilifc5c9412021-08-08 19:08:26 -0700303 walk_and_alloc(env, local, carref(expr), false);
swissChilif1ba8c12021-07-02 18:45:38 -0700304 }
swissChili67bdf282021-06-06 18:46:08 -0700305
306 local_free(local, slot);
307 }
swissChilifc5c9412021-08-08 19:08:26 -0700308 else if (!quoted && symstreq(car(body), "lambda"))
swissChilif1ba8c12021-07-02 18:45:38 -0700309 {
310 // We don't want to walk the lambda because it's another function. When
311 // the lambda is compiled it will be walked.
swissChili36f2c692021-08-08 14:31:44 -0700312 OKAY();
swissChilif1ba8c12021-07-02 18:45:38 -0700313 }
swissChili67bdf282021-06-06 18:46:08 -0700314 else
315 {
swissChilifc5c9412021-08-08 19:08:26 -0700316 if (quoted)
swissChili67bdf282021-06-06 18:46:08 -0700317 {
swissChilifc5c9412021-08-08 19:08:26 -0700318 if (symstreq(car(body), "unquote") || symstreq(car(body), "unquote-splice"))
319 {
320 for (value_t b = cdr(body); !nilp(b); b = cdr(b))
321 {
322 walk_and_alloc(env, local, carref(b), false);
323 }
324 }
swissChili36f2c692021-08-08 14:31:44 -0700325 }
326 else
327 {
swissChilifc5c9412021-08-08 19:08:26 -0700328 // Is this a macro?
329
330 struct function *mac = NULL;
331
332 if (symbolp(car(body)))
333 mac = find_function(env, (char *)(car(body) ^ SYMBOL_TAG));
334 else if (consp(car(body))) // consp, not just listp, since we don't care about nil.
335 walk_and_alloc(env, local, carref(body), false);
336
337 if (mac && mac->namespace == NS_MACRO)
swissChili36f2c692021-08-08 14:31:44 -0700338 {
swissChilifc5c9412021-08-08 19:08:26 -0700339 unsigned char pool = push_pool(0);
340 value_t form = call_list(mac, args);
341 pop_pool(pool);
342
343 add_to_pool(form);
344 *bp = form;
345
346 walk_and_alloc(env, local, bp, false);
347 }
348 else
349 {
350 bool should_quote = symstreq(car(body), "quote") || symstreq(car(body), "backquote");
351
352 for (; !nilp(args); args = cdr(args))
353 {
354 walk_and_alloc(env, local, carref(args), should_quote);
355 }
swissChili36f2c692021-08-08 14:31:44 -0700356 }
swissChili67bdf282021-06-06 18:46:08 -0700357 }
swissChili8fc5e2f2021-04-22 13:45:10 -0700358 }
swissChili36f2c692021-08-08 14:31:44 -0700359
360 OKAY();
swissChili8fc5e2f2021-04-22 13:45:10 -0700361}
362
swissChilif68671f2021-07-05 14:14:44 -0700363bool load(struct environment *env, char *path)
swissChili8fc5e2f2021-04-22 13:45:10 -0700364{
swissChilif68671f2021-07-05 14:14:44 -0700365 if (!file_exists(path))
366 return false;
367
368 add_load(env, path);
369
swissChilib8fd4712021-06-23 15:32:04 -0700370 unsigned char pool = make_pool();
371 unsigned char pop = push_pool(pool);
372
swissChilif68671f2021-07-05 14:14:44 -0700373 struct istream *is = new_fistream(path, false);
374 if (!is)
375 return false;
376
swissChili8fc5e2f2021-04-22 13:45:10 -0700377 value_t val;
swissChili53472e82021-05-08 16:06:32 -0700378
swissChilifc5c9412021-08-08 19:08:26 -0700379 struct error compile_error, read_error;
swissChili36f2c692021-08-08 14:31:44 -0700380
381 while (IS_OKAY((read_error = read1(is, &val))))
swissChili8fc5e2f2021-04-22 13:45:10 -0700382 {
swissChilifc5c9412021-08-08 19:08:26 -0700383 if (!IS_OKAY((compile_error = compile_tl(val, env, path))))
swissChili36f2c692021-08-08 14:31:44 -0700384 {
swissChilifc5c9412021-08-08 19:08:26 -0700385 ereport(compile_error);
swissChili36f2c692021-08-08 14:31:44 -0700386 goto failure;
387 }
388 }
389
390 if (!read_error.safe_state)
391 {
392 goto failure;
swissChili8fc5e2f2021-04-22 13:45:10 -0700393 }
swissChilif3e7f182021-04-20 13:57:22 -0700394
swissChilif68671f2021-07-05 14:14:44 -0700395 del_fistream(is);
swissChilib8fd4712021-06-23 15:32:04 -0700396 pop_pool(pop);
397
swissChilif68671f2021-07-05 14:14:44 -0700398 return true;
swissChili36f2c692021-08-08 14:31:44 -0700399
400failure:
401 del_fistream(is);
402 pop_pool(pool);
403
404 return false;
swissChilif68671f2021-07-05 14:14:44 -0700405}
406
swissChili7e1393c2021-07-07 12:59:12 -0700407value_t load_relative(struct environment *env, char *to, value_t name)
408{
409 if (!stringp(name))
410 return nil;
411
412 char *new_path = (char *)(name ^ STRING_TAG);
413 char *relative_to = strdup(to);
414 char full_path[512];
415
416 snprintf(full_path, 512, "%s/%s", dirname(relative_to), new_path);
417
swissChilic0acce42022-07-31 13:38:17 -0700418 free(relative_to);
419
swissChili7e1393c2021-07-07 12:59:12 -0700420 if (load(env, full_path))
421 return t;
422 else
423 return nil;
424}
425
swissChili6d02af42021-08-05 19:49:01 -0700426struct error compile_file(char *filename, struct environment **e)
swissChilif68671f2021-07-05 14:14:44 -0700427{
swissChili6d02af42021-08-05 19:49:01 -0700428 E_INIT();
429
swissChilif68671f2021-07-05 14:14:44 -0700430 value_t val;
swissChili7e1393c2021-07-07 12:59:12 -0700431 struct environment *env = malloc(sizeof(struct environment));
432 env->first = NULL;
433 env->first_loaded = NULL;
swissChilif68671f2021-07-05 14:14:44 -0700434
swissChili7e1393c2021-07-07 12:59:12 -0700435 add_load(env, filename);
swissChili6d02af42021-08-05 19:49:01 -0700436 TRY(load_std(env));
swissChilif68671f2021-07-05 14:14:44 -0700437
swissChili7e1393c2021-07-07 12:59:12 -0700438 bool ok_ = load(env, filename);
swissChilif68671f2021-07-05 14:14:44 -0700439
swissChili6d02af42021-08-05 19:49:01 -0700440 if (!ok_)
441 {
442 free(env);
swissChili1e8b7562021-12-22 21:22:57 -0800443 NEARFL(filename, 1);
swissChili6d02af42021-08-05 19:49:01 -0700444 THROWSAFE(ENOTFOUND);
445 }
swissChilif68671f2021-07-05 14:14:44 -0700446
swissChili6d02af42021-08-05 19:49:01 -0700447 *e = env;
448
449 OKAY();
swissChilica107a02021-04-14 12:07:30 -0700450}
swissChilib3ca4fb2021-04-20 10:33:00 -0700451
swissChili53472e82021-05-08 16:06:32 -0700452int nextpc(struct local *local, dasm_State **Dst)
swissChilib3ca4fb2021-04-20 10:33:00 -0700453{
swissChili53472e82021-05-08 16:06:32 -0700454 int n = local->nextpc++;
455 if (n > local->npc)
456 {
457 local->npc += 16;
458 dasm_growpc(Dst, local->npc);
459 }
460 return n;
461}
462
swissChili6d02af42021-08-05 19:49:01 -0700463struct error compile_backquote(struct environment *env, struct local *local,
464 value_t val, dasm_State **Dst)
swissChili6b47b6d2021-06-30 22:08:55 -0700465{
swissChili6d02af42021-08-05 19:49:01 -0700466 E_INIT();
467
swissChili6b47b6d2021-06-30 22:08:55 -0700468 if (!listp(val))
469 {
470 | mov eax, (val);
471 }
472 else
473 {
swissChili7e1393c2021-07-07 12:59:12 -0700474 value_t fsym = car(val), args = cdr(val);
swissChili9d151e62021-08-04 13:11:45 -0700475 int nargs = length(args),
476 n = length(val);
swissChili6b47b6d2021-06-30 22:08:55 -0700477
swissChili6d02af42021-08-05 19:49:01 -0700478 NEARVAL(val);
479
swissChili9d151e62021-08-04 13:11:45 -0700480 if (symstreq(fsym, "unquote"))
481 {
482 if (nargs != 1)
483 {
swissChili6d02af42021-08-05 19:49:01 -0700484 THROW(EARGS, "unquote (or ,) takes exactly 1 argument");
swissChili9d151e62021-08-04 13:11:45 -0700485 }
486
swissChili6d02af42021-08-05 19:49:01 -0700487 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili9d151e62021-08-04 13:11:45 -0700488 }
489 else
490 {
491 | push nil;
492
493 for (int i = n - 1; i >= 0; i--)
494 {
swissChilia7568dc2021-08-08 16:52:52 -0700495 value_t v = elt(val, i);
swissChili9d151e62021-08-04 13:11:45 -0700496
swissChilia7568dc2021-08-08 16:52:52 -0700497 if (listp(v) && symstreq(car(v), "unquote-splice"))
498 {
499 NEARVAL(v);
500
501 if (length(v) != 2)
502 {
503 THROW(EARGS, "unquote-splice (or ,@) takes exactly 1 argument");
504 }
505
506 value_t expr = car(cdr(v));
507
swissChilia7568dc2021-08-08 16:52:52 -0700508 TRY(compile_expression(env, local, expr, false, Dst));
509 | push eax;
510 | call_extern merge2;
511 | add esp, 8;
512 | push eax;
513 }
514 else
515 {
516 TRY(compile_backquote(env, local, v, Dst));
517 | push eax;
518 | call_extern cons;
519 | add esp, 8;
520
521 // Remove unnecessary pop
522 | push eax;
523 }
swissChili9d151e62021-08-04 13:11:45 -0700524 }
swissChilia89ee442021-08-04 20:54:51 -0700525 | pop eax;
swissChili9d151e62021-08-04 13:11:45 -0700526 }
swissChili6b47b6d2021-06-30 22:08:55 -0700527 }
swissChili6d02af42021-08-05 19:49:01 -0700528
529 OKAY();
swissChili6b47b6d2021-06-30 22:08:55 -0700530}
531
swissChili7e1393c2021-07-07 12:59:12 -0700532value_t eval(struct environment *env, value_t form)
533{
swissChili80560312022-07-31 21:05:47 -0700534 gc_push_segment(&form, 1);
swissChili7e1393c2021-07-07 12:59:12 -0700535 // Eval!
536 value_t function = cons(nil, cons(form, nil));
537
swissChili80560312022-07-31 21:05:47 -0700538 gc_set_retained(0, function);
539
swissChili7e1393c2021-07-07 12:59:12 -0700540 struct local local;
541 struct args *args;
542
swissChili6d02af42021-08-05 19:49:01 -0700543 dasm_State *d;
544 struct error err;
545
546 if (!IS_OKAY((err = compile_function(function, NS_ANONYMOUS, env, &local, NULL,
547 &args, NULL, "/", &d))))
548 {
549 ereport(err);
550 return nil;
551 }
swissChili7e1393c2021-07-07 12:59:12 -0700552
553 del_local(&local);
554
555 value_t (*f)() = link_program(&d);
swissChili80560312022-07-31 21:05:47 -0700556
swissChili16156be2022-07-31 21:14:02 -0700557 dasm_free(&d);
558 free(args);
559
swissChili80560312022-07-31 21:05:47 -0700560 gc_prepare_call(0);
561 value_t val = f();
562
563 gc_pop_segment();
564
565 return val;
swissChili7e1393c2021-07-07 12:59:12 -0700566}
567
swissChili6d02af42021-08-05 19:49:01 -0700568struct error compile_variable(struct variable *v, dasm_State *Dst)
swissChiliddc97542021-07-04 11:47:42 -0700569{
swissChili6d02af42021-08-05 19:49:01 -0700570 E_INIT();
swissChiliddc97542021-07-04 11:47:42 -0700571 switch (v->type)
572 {
573 case V_ARGUMENT:
swissChili7e1393c2021-07-07 12:59:12 -0700574 | mov eax, dword[ebp + (value_size * (v->number + 2))];
swissChiliddc97542021-07-04 11:47:42 -0700575 break;
576 case V_BOUND:
swissChili7e1393c2021-07-07 12:59:12 -0700577 | mov eax, dword[ebp - ((v->number + 1) * value_size)];
swissChiliddc97542021-07-04 11:47:42 -0700578 break;
579 case V_FREE:
580 // edi is the closure context pointer
swissChili7e1393c2021-07-07 12:59:12 -0700581 | mov eax, dword[edi + (v->number * value_size)];
swissChiliddc97542021-07-04 11:47:42 -0700582 break;
583 default:
swissChili6d02af42021-08-05 19:49:01 -0700584 THROW(EUNIMPL, "Sorry, can only access V_ARGUMENT, V_BOUND, and V_FREE vars");
swissChiliddc97542021-07-04 11:47:42 -0700585 }
swissChili6d02af42021-08-05 19:49:01 -0700586 OKAY();
swissChiliddc97542021-07-04 11:47:42 -0700587}
588
swissChili6d02af42021-08-05 19:49:01 -0700589struct error compile_expression(struct environment *env, struct local *local,
590 value_t val, bool tail, dasm_State **Dst)
swissChili53472e82021-05-08 16:06:32 -0700591{
swissChili6d02af42021-08-05 19:49:01 -0700592 E_INIT();
593
594 NEARVAL(val);
595
swissChili7e1393c2021-07-07 12:59:12 -0700596 if (symstreq(val, "nil") || nilp(val))
swissChili53472e82021-05-08 16:06:32 -0700597 {
598 | mov eax, (nil);
599 }
swissChili923b5362021-05-09 20:31:43 -0700600 else if (symstreq(val, "t"))
601 {
602 | mov eax, (t);
603 }
604 else if (integerp(val) || stringp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700605 {
606 | mov eax, val;
607 }
swissChili53472e82021-05-08 16:06:32 -0700608 else if (listp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700609 {
swissChili53472e82021-05-08 16:06:32 -0700610 value_t fsym = car(val);
611 value_t args = cdr(val);
612 int nargs = length(args);
swissChilib3ca4fb2021-04-20 10:33:00 -0700613
swissChili53472e82021-05-08 16:06:32 -0700614 if (!symbolp(fsym))
swissChilif3e7f182021-04-20 13:57:22 -0700615 {
swissChili6d02af42021-08-05 19:49:01 -0700616 THROW(EEXPECTED, "Function name must be a symbol");
swissChilif3e7f182021-04-20 13:57:22 -0700617 }
618
swissChili53472e82021-05-08 16:06:32 -0700619 if (symstreq(fsym, "if"))
swissChilib3ca4fb2021-04-20 10:33:00 -0700620 {
swissChili53472e82021-05-08 16:06:32 -0700621 if (nargs < 2 || nargs > 3)
swissChili6d02af42021-08-05 19:49:01 -0700622 {
623 THROW(EARGS, "Must give at least 2 arguments to if");
624 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700625
swissChili6d02af42021-08-05 19:49:01 -0700626 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili53472e82021-05-08 16:06:32 -0700627 int false_label = nextpc(local, Dst),
628 after_label = nextpc(local, Dst);
629
630 // result is in eax
631 | cmp eax, (nil);
swissChili484295d2021-07-09 21:25:55 -0700632 | je =>false_label;
swissChili53472e82021-05-08 16:06:32 -0700633
swissChili6d02af42021-08-05 19:49:01 -0700634 TRY(compile_expression(env, local, elt(args, 1), tail, Dst));
swissChili484295d2021-07-09 21:25:55 -0700635 | jmp =>after_label;
636 |=>false_label:;
swissChili53472e82021-05-08 16:06:32 -0700637 if (nargs == 3)
swissChili6d02af42021-08-05 19:49:01 -0700638 TRY(compile_expression(env, local, elt(args, 2), tail, Dst));
swissChili484295d2021-07-09 21:25:55 -0700639 |=>after_label:;
swissChili53472e82021-05-08 16:06:32 -0700640 }
swissChilia89ee442021-08-04 20:54:51 -0700641 else if (symstreq(fsym, "and") || symstreq(fsym, "or"))
642 {
643 bool or = symstreq(fsym, "or"); // false == and
644
645 // Boolean and and or, short circuit like &&/||
646 if (nargs < 1)
647 {
swissChili6d02af42021-08-05 19:49:01 -0700648 THROW(EARGS, "and & or require at least 1 argument.");
swissChilia89ee442021-08-04 20:54:51 -0700649 }
650
651 int after = nextpc(local, Dst);
652
653 for (; !nilp(args); args = cdr(args))
654 {
swissChili6d02af42021-08-05 19:49:01 -0700655 NEARVAL(args);
656
657 TRY(compile_expression(env, local, car(args), false, Dst));
swissChilia89ee442021-08-04 20:54:51 -0700658 if (!nilp(cdr(args)))
659 {
660 | cmp eax, nil;
661 if (or)
662 {
swissChilifbf525f2021-08-04 21:28:07 -0700663 | jne =>after;
swissChilia89ee442021-08-04 20:54:51 -0700664 }
665 else
666 {
swissChilifbf525f2021-08-04 21:28:07 -0700667 | je =>after;
swissChilia89ee442021-08-04 20:54:51 -0700668 }
669 }
670 }
671
672 |=>after:;
673 }
swissChilif68671f2021-07-05 14:14:44 -0700674 else if (symstreq(fsym, "progn"))
675 {
676 for (value_t val = args; !nilp(val); val = cdr(val))
677 {
swissChili6d02af42021-08-05 19:49:01 -0700678 NEARVAL(args);
679
swissChilib51552c2021-08-03 10:23:37 -0700680 bool t = tail && nilp(cdr(val));
swissChili6d02af42021-08-05 19:49:01 -0700681 TRY(compile_expression(env, local, car(val), t, Dst));
swissChilif68671f2021-07-05 14:14:44 -0700682 }
683 }
swissChili67bdf282021-06-06 18:46:08 -0700684 else if (symstreq(fsym, "let1"))
685 {
686 if (nargs < 2)
687 {
swissChili6d02af42021-08-05 19:49:01 -0700688 THROW(EARGS, "Must give at least 2 arguments to let1");
swissChili67bdf282021-06-06 18:46:08 -0700689 }
690 value_t binding = car(args);
691 value_t rest = cdr(args);
692
swissChili6d02af42021-08-05 19:49:01 -0700693 NEARVAL(binding);
swissChili67bdf282021-06-06 18:46:08 -0700694 if (length(binding) != 2)
695 {
swissChili6d02af42021-08-05 19:49:01 -0700696 THROW(EARGS, "Binding list in let1 must contain exactly two entries");
swissChili67bdf282021-06-06 18:46:08 -0700697 }
698
swissChili6d02af42021-08-05 19:49:01 -0700699 NEARVAL(rest);
700
swissChili67bdf282021-06-06 18:46:08 -0700701 value_t name = car(binding);
702 value_t value = car(cdr(binding));
703
swissChili6d02af42021-08-05 19:49:01 -0700704 TRY(compile_expression(env, local, value, false, Dst));
swissChili67bdf282021-06-06 18:46:08 -0700705
706 int i = local_alloc(local);
707
708 add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i);
709
swissChili7e1393c2021-07-07 12:59:12 -0700710 | mov dword[ebp - ((i + 1) * value_size)], eax;
swissChili67bdf282021-06-06 18:46:08 -0700711
712 for (; !nilp(rest); rest = cdr(rest))
713 {
swissChilib51552c2021-08-03 10:23:37 -0700714 bool t = tail && nilp(cdr(rest));
swissChili6d02af42021-08-05 19:49:01 -0700715 NEARVAL(rest);
716 TRY(compile_expression(env, local, car(rest), t, Dst));
swissChili67bdf282021-06-06 18:46:08 -0700717 }
718
719 local_free(local, i);
720 }
swissChilie9fec8b2021-06-22 13:59:33 -0700721 else if (symstreq(fsym, "gc"))
722 {
723 if (nargs)
724 {
swissChili6d02af42021-08-05 19:49:01 -0700725 THROW(EARGS, "gc takes no arguments");
swissChilie9fec8b2021-06-22 13:59:33 -0700726 }
727
728 | run_gc;
729 }
swissChili6b47b6d2021-06-30 22:08:55 -0700730 else if (symstreq(fsym, "quote"))
731 {
732 if (nargs != 1)
swissChili6d02af42021-08-05 19:49:01 -0700733 THROW(EARGS, "quote should take exactly 1 argument");
swissChili6b47b6d2021-06-30 22:08:55 -0700734
735 // Simple!
736 | mov eax, (car(args));
737 }
738 else if (symstreq(fsym, "backquote"))
739 {
740 if (nargs != 1)
swissChili6d02af42021-08-05 19:49:01 -0700741 THROW(EARGS, "backquote should take exactly 1 argument");
swissChili6b47b6d2021-06-30 22:08:55 -0700742
swissChili6d02af42021-08-05 19:49:01 -0700743 TRY(compile_backquote(env, local, car(args), Dst));
swissChili6b47b6d2021-06-30 22:08:55 -0700744 }
swissChili74348422021-07-04 13:23:24 -0700745 else if (symstreq(fsym, "function"))
746 {
747 if (nargs != 1)
748 {
swissChili6d02af42021-08-05 19:49:01 -0700749 THROW(EARGS, "function should take exactly 1 argument");
swissChili74348422021-07-04 13:23:24 -0700750 }
751
swissChili6d02af42021-08-05 19:49:01 -0700752 NEARVAL(args);
swissChili74348422021-07-04 13:23:24 -0700753 if (!symbolp(car(args)))
754 {
swissChili6d02af42021-08-05 19:49:01 -0700755 THROW(EINVALID, "argument to function should be a symbol resolvable at "
swissChili7e1393c2021-07-07 12:59:12 -0700756 "compile time");
swissChili74348422021-07-04 13:23:24 -0700757 }
758
swissChilia89ee442021-08-04 20:54:51 -0700759 char *name = (char *)(car(args) ^ SYMBOL_TAG);
swissChili74348422021-07-04 13:23:24 -0700760
swissChilia89ee442021-08-04 20:54:51 -0700761 if (!strcmp(name, local->current_function_name))
762 {
763 | push 0;
764 | push local->args;
765 | push <1;
766 | call_extern create_closure;
767 }
768 else
769 {
770 struct function *f = find_function(env, name);
771
772 if (!f)
773 {
swissChili6d02af42021-08-05 19:49:01 -0700774 THROW(EINVALID, "Function `%s' does not exist", (char *)(car(args) ^ SYMBOL_TAG));
swissChilia89ee442021-08-04 20:54:51 -0700775 }
776 value_t closure = create_closure(f->code_ptr, f->args, 0);
777 | mov eax, (closure);
778 }
swissChili74348422021-07-04 13:23:24 -0700779 }
swissChili6b47b6d2021-06-30 22:08:55 -0700780 else if (symstreq(fsym, "list"))
781 {
swissChili484295d2021-07-09 21:25:55 -0700782 | push (nil);
swissChili6b47b6d2021-06-30 22:08:55 -0700783
784 for (int i = nargs - 1; i >= 0; i--)
785 {
swissChili6d02af42021-08-05 19:49:01 -0700786 TRY(compile_expression(env, local, elt(args, i), false, Dst));
swissChili6b47b6d2021-06-30 22:08:55 -0700787
swissChili6b47b6d2021-06-30 22:08:55 -0700788 | push eax;
swissChili53e7cd12021-08-02 21:55:53 -0700789 | call_extern cons;
swissChili6b47b6d2021-06-30 22:08:55 -0700790 | add esp, (2 * value_size);
swissChili6b47b6d2021-06-30 22:08:55 -0700791 | push eax;
792 }
swissChili6d02af42021-08-05 19:49:01 -0700793 | pop eax;
swissChili6b47b6d2021-06-30 22:08:55 -0700794 }
swissChiliddc97542021-07-04 11:47:42 -0700795 else if (symstreq(fsym, "lambda"))
796 {
797 // Compile the function with this as the parent scope
798 struct local new_local;
swissChilic0acce42022-07-31 13:38:17 -0700799 struct args *nargs_out;
swissChili6d02af42021-08-05 19:49:01 -0700800 dasm_State *d;
801 TRY(compile_function(
802 args, NS_ANONYMOUS, env, &new_local, local, &nargs_out,
803 "recurse", local->current_file_path, &d));
swissChiliddc97542021-07-04 11:47:42 -0700804
805 // Link the function
swissChilif68671f2021-07-05 14:14:44 -0700806 void *func_ptr = link_program(&d);
swissChiliddc97542021-07-04 11:47:42 -0700807
808 // Create a closure object with the correct number of captures at
809 // runtime
swissChili484295d2021-07-09 21:25:55 -0700810 | push (new_local.num_closure_slots);
811 | push (nargs_out);
812 | push (func_ptr);
swissChili53e7cd12021-08-02 21:55:53 -0700813 | call_extern create_closure;
swissChiliddc97542021-07-04 11:47:42 -0700814 | add esp, 12;
815
816 // Walk the generated local scope for V_FREE variables, since each
817 // of these exists in our scope (or higher), evaluate it and set it
818 // as a member of the lambda capture.
819
820 for (struct variable *var = new_local.first; var; var = var->prev)
821 {
822 if (var->type == V_FREE)
823 {
824 // Closure in eax
825 | push eax;
826 // Variable now in eax
swissChili6d02af42021-08-05 19:49:01 -0700827 TRY(compile_variable(find_variable(local, var->name), Dst));
swissChiliddc97542021-07-04 11:47:42 -0700828 | push eax;
829
swissChiliddc97542021-07-04 11:47:42 -0700830 // The capture offset
swissChili484295d2021-07-09 21:25:55 -0700831 | push (var->number);
swissChili53e7cd12021-08-02 21:55:53 -0700832 | call_extern set_closure_capture_variable;
swissChiliddc97542021-07-04 11:47:42 -0700833 // Skip the value and index
834 | add esp, 8;
835 // Pop the closure back in to eax
836 | pop eax;
837 }
838 }
839
840 // Closure is still in eax
841
842 dasm_free(&d);
swissChili708d4c42021-07-04 17:40:07 -0700843 del_local(&new_local);
swissChiliddc97542021-07-04 11:47:42 -0700844 }
swissChili7e1393c2021-07-07 12:59:12 -0700845 else if (symstreq(fsym, "eval"))
846 {
847 if (nargs != 1)
848 {
swissChili6d02af42021-08-05 19:49:01 -0700849 THROW(EARGS, "eval takes exactly 1 argument");
swissChili7e1393c2021-07-07 12:59:12 -0700850 }
851
swissChili6d02af42021-08-05 19:49:01 -0700852 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili7e1393c2021-07-07 12:59:12 -0700853 | push eax;
swissChili484295d2021-07-09 21:25:55 -0700854 | push (env);
swissChili53e7cd12021-08-02 21:55:53 -0700855 | call_extern eval;
swissChili7e1393c2021-07-07 12:59:12 -0700856 }
857 else if (symstreq(fsym, "load"))
858 {
859 if (nargs != 1)
860 {
swissChili6d02af42021-08-05 19:49:01 -0700861 THROW(EARGS, "load takes exactly 1 argument, %d given", nargs);
swissChili7e1393c2021-07-07 12:59:12 -0700862 }
863
swissChili6d02af42021-08-05 19:49:01 -0700864 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili7e1393c2021-07-07 12:59:12 -0700865 | push eax;
swissChili484295d2021-07-09 21:25:55 -0700866 | push (local->current_file_path);
867 | push (env);
swissChili53e7cd12021-08-02 21:55:53 -0700868 | call_extern load_relative;
swissChili7e1393c2021-07-07 12:59:12 -0700869 }
swissChili53472e82021-05-08 16:06:32 -0700870 else
871 {
swissChili74348422021-07-04 13:23:24 -0700872 char *name = (char *)(fsym ^ SYMBOL_TAG);
873 struct function *func = find_function(env, name);
swissChili7e1393c2021-07-07 12:59:12 -0700874
swissChili74348422021-07-04 13:23:24 -0700875 bool is_recursive = false;
swissChili15f1cae2021-07-05 19:08:47 -0700876 struct args *nargs_needed = NULL;
swissChili53472e82021-05-08 16:06:32 -0700877
swissChili53e7cd12021-08-02 21:55:53 -0700878 // The number of arguments actually passed on the stack,
879 // i.e. all varargs are 1.
swissChilib51552c2021-08-03 10:23:37 -0700880 int real_nargs;
swissChili53e7cd12021-08-02 21:55:53 -0700881
swissChili7e1393c2021-07-07 12:59:12 -0700882 if (local->current_function_name &&
883 symstreq(fsym, local->current_function_name))
swissChilif1ba8c12021-07-02 18:45:38 -0700884 {
swissChili74348422021-07-04 13:23:24 -0700885 is_recursive = true;
swissChili15f1cae2021-07-05 19:08:47 -0700886 nargs_needed = local->args;
swissChili74348422021-07-04 13:23:24 -0700887 }
888 else
889 {
890 if (func == NULL)
891 {
swissChili6d02af42021-08-05 19:49:01 -0700892 THROW(EINVALID, "Function %s undefined", name);
swissChili74348422021-07-04 13:23:24 -0700893 }
894
swissChili15f1cae2021-07-05 19:08:47 -0700895 nargs_needed = func->args;
swissChili74348422021-07-04 13:23:24 -0700896 }
897
swissChili15f1cae2021-07-05 19:08:47 -0700898 if (!are_args_acceptable(nargs_needed, nargs))
swissChili74348422021-07-04 13:23:24 -0700899 {
swissChili6d02af42021-08-05 19:49:01 -0700900 THROW(EARGS,
901 "wrong number of args in function call: %s, "
902 "want %d args but given %d\n",
903 name, nargs_needed->num_required, nargs);
swissChilif1ba8c12021-07-02 18:45:38 -0700904 }
swissChili53472e82021-05-08 16:06:32 -0700905
swissChili53e7cd12021-08-02 21:55:53 -0700906 int total_taken = nargs_needed->num_optional +
907 nargs_needed->num_required;
908
swissChilib51552c2021-08-03 10:23:37 -0700909 real_nargs = total_taken + (nargs_needed->variadic ? 1 : 0);
swissChili53e7cd12021-08-02 21:55:53 -0700910
swissChili74348422021-07-04 13:23:24 -0700911 if (is_recursive || func->namespace == NS_FUNCTION)
swissChili53472e82021-05-08 16:06:32 -0700912 {
swissChili15f1cae2021-07-05 19:08:47 -0700913 int nargs = length(args);
914
swissChili484295d2021-07-09 21:25:55 -0700915 int line = cons_line(val);
916 char *file = cons_file(val);
917
918 if (nargs_needed->variadic)
swissChili15f1cae2021-07-05 19:08:47 -0700919 {
swissChili484295d2021-07-09 21:25:55 -0700920 | push (nil);
921 }
922
923 if (nargs > total_taken && nargs_needed->variadic)
924 {
925 // We are passing varargs, which means we need to make a list
926
927 for (int i = nargs - 1; i >= total_taken; i--)
928 {
swissChili6d02af42021-08-05 19:49:01 -0700929 TRY(compile_expression(env, local, elt(args, i), false, Dst));
swissChili484295d2021-07-09 21:25:55 -0700930 | push eax;
swissChili53e7cd12021-08-02 21:55:53 -0700931 | call_extern cons;
swissChili484295d2021-07-09 21:25:55 -0700932 | add esp, 8;
933 | push eax;
934 }
swissChili15f1cae2021-07-05 19:08:47 -0700935 }
936
swissChili7e1393c2021-07-07 12:59:12 -0700937 for (int i = nargs_needed->num_optional - 1;
938 i >= nargs - nargs_needed->num_required; i--)
swissChili15f1cae2021-07-05 19:08:47 -0700939 {
940 // Push the default optional values
swissChili484295d2021-07-09 21:25:55 -0700941 | push (nargs_needed->optional_arguments[i].value);
swissChili15f1cae2021-07-05 19:08:47 -0700942 }
943
swissChili484295d2021-07-09 21:25:55 -0700944 int min = MIN(nargs, total_taken);
945
946 for (int i = min - 1; i >= 0; i--)
swissChili2999dd12021-07-02 14:19:53 -0700947 {
swissChili6d02af42021-08-05 19:49:01 -0700948 TRY(compile_expression(env, local, elt(args, i), false, Dst));
swissChili2999dd12021-07-02 14:19:53 -0700949 | push eax;
950 }
swissChili15f1cae2021-07-05 19:08:47 -0700951
swissChili74348422021-07-04 13:23:24 -0700952 if (is_recursive)
953 {
swissChilib51552c2021-08-03 10:23:37 -0700954 if (tail)
955 {
956 // Move all the arguments pushed to the stack
957 // back up to the argument bit of the stack.
958
959 for (int i = 0; i < real_nargs; i++)
960 {
961 | pop eax;
962 | mov dword[ebp + (value_size * (i + 2))], eax;
963 }
964
965 // Jmp back to start
966 | mov esp, ebp;
967 | pop ebp;
968 | jmp <1;
969 }
970 else
971 {
972 | call <1;
973 }
swissChili74348422021-07-04 13:23:24 -0700974 }
975 else
976 {
swissChili484295d2021-07-09 21:25:55 -0700977 // | mov ebx, (func->code_addr);
978 | call_extern func->code_addr;
swissChili74348422021-07-04 13:23:24 -0700979 }
swissChili53e7cd12021-08-02 21:55:53 -0700980 | add esp, (real_nargs * value_size);
swissChili2999dd12021-07-02 14:19:53 -0700981 // result in eax
982 }
983 else if (func->namespace == NS_MACRO)
984 {
swissChili7e1393c2021-07-07 12:59:12 -0700985 // Make sure that the stuff allocated by the macro isn't in a
986 // pool
swissChilif68671f2021-07-05 14:14:44 -0700987 unsigned char pool = push_pool(0);
988
swissChili2999dd12021-07-02 14:19:53 -0700989 value_t expanded_to = call_list(func, args);
990
swissChilif68671f2021-07-05 14:14:44 -0700991 pop_pool(pool);
992
swissChili6d02af42021-08-05 19:49:01 -0700993 TRY(compile_expression(env, local, expanded_to, false, Dst));
swissChili2999dd12021-07-02 14:19:53 -0700994 }
swissChili53472e82021-05-08 16:06:32 -0700995 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700996 }
swissChili923b5362021-05-09 20:31:43 -0700997 else if (symbolp(val))
998 {
swissChili7e1393c2021-07-07 12:59:12 -0700999 if (symstreq(val, "+current-file+"))
swissChilie9fec8b2021-06-22 13:59:33 -07001000 {
swissChili7e1393c2021-07-07 12:59:12 -07001001 value_t file_name_val = strval(local->current_file_path);
1002
1003 | mov eax, (file_name_val);
swissChilie9fec8b2021-06-22 13:59:33 -07001004 }
swissChili04d94162022-07-30 21:46:49 -07001005 else if (symstreq(val, "+current-env+"))
1006 {
1007 // TODO: we return this as a raw "integer", which is a bad
1008 // idea. Once classes are added this needs to be wrapped
1009 // in a class.
1010 | mov eax, (env);
1011 }
swissChili7e1393c2021-07-07 12:59:12 -07001012 else
1013 {
1014 struct variable *v =
1015 find_variable(local, (char *)(val ^ SYMBOL_TAG));
swissChili923b5362021-05-09 20:31:43 -07001016
swissChili7e1393c2021-07-07 12:59:12 -07001017 if (!v)
1018 {
swissChili6d02af42021-08-05 19:49:01 -07001019 THROW(EINVALID, "Variable `%s' unbound", (char *)(val ^ SYMBOL_TAG));
swissChili7e1393c2021-07-07 12:59:12 -07001020 }
1021
swissChili6d02af42021-08-05 19:49:01 -07001022 TRY(compile_variable(v, Dst));
swissChili7e1393c2021-07-07 12:59:12 -07001023 }
swissChili923b5362021-05-09 20:31:43 -07001024 }
swissChilia89ee442021-08-04 20:54:51 -07001025 else if (closurep(val))
1026 {
1027 | mov eax, val;
1028 }
1029 else
1030 {
1031 printval(val, 1);
swissChili6d02af42021-08-05 19:49:01 -07001032 THROW(EUNIMPL, "Don't know how to compile this, sorry.");
swissChilia89ee442021-08-04 20:54:51 -07001033 }
swissChili6d02af42021-08-05 19:49:01 -07001034
1035 OKAY();
swissChilib3ca4fb2021-04-20 10:33:00 -07001036}
swissChilif3e7f182021-04-20 13:57:22 -07001037
swissChili923b5362021-05-09 20:31:43 -07001038struct variable *add_variable(struct local *local, enum var_type type,
1039 char *name, int number)
1040{
1041 struct variable *var = malloc(sizeof(struct variable));
1042 var->prev = local->first;
1043 var->type = type;
1044 var->name = name;
1045 var->number = number;
1046
1047 local->first = var;
1048
1049 return var;
1050}
1051
1052void destroy_local(struct local *local)
1053{
1054 for (struct variable *v = local->first; v;)
1055 {
1056 struct variable *t = v;
1057 v = v->prev;
1058 free(t);
1059 }
1060}
1061
1062struct variable *find_variable(struct local *local, char *name)
1063{
1064 struct variable *v = local->first;
1065
1066 for (; v && strcmp(v->name, name) != 0; v = v->prev)
swissChili7e1393c2021-07-07 12:59:12 -07001067 {
1068 }
swissChili923b5362021-05-09 20:31:43 -07001069
swissChiliddc97542021-07-04 11:47:42 -07001070 if (!v)
1071 {
1072 if (local->parent)
1073 {
1074 v = find_variable(local->parent, name);
1075
1076 if (v)
1077 {
swissChili15f1cae2021-07-05 19:08:47 -07001078 // We found this in a parent scope, add it as a V_FREE variable
1079 // to skip the search.
swissChili7e1393c2021-07-07 12:59:12 -07001080 v = add_variable(local, V_FREE, name,
1081 local->num_closure_slots++);
swissChiliddc97542021-07-04 11:47:42 -07001082 }
1083 }
1084 }
swissChili923b5362021-05-09 20:31:43 -07001085 return v;
1086}
swissChili2999dd12021-07-02 14:19:53 -07001087
swissChiliddc97542021-07-04 11:47:42 -07001088extern value_t _call_list(void *addr, value_t list, value_t *edi);
swissChili2999dd12021-07-02 14:19:53 -07001089
swissChili7e1393c2021-07-07 12:59:12 -07001090value_t call_list_args(void *code_ptr, struct args *args, value_t list,
1091 void *data)
swissChili2999dd12021-07-02 14:19:53 -07001092{
swissChili15f1cae2021-07-05 19:08:47 -07001093 list = deep_copy(list);
swissChili484295d2021-07-09 21:25:55 -07001094
swissChili15f1cae2021-07-05 19:08:47 -07001095 int nargs = length(list);
1096
swissChili484295d2021-07-09 21:25:55 -07001097 value_t *val = &list;
swissChili15f1cae2021-07-05 19:08:47 -07001098
1099 for (value_t i = list; !nilp(i); i = cdr(i))
1100 {
1101 val = cdrref(i);
1102 }
1103
1104 int total_required = args->num_required + args->num_optional;
1105
1106 if (nargs > total_required)
1107 {
1108 // Take the remainder of the list and put it as the last item in the
1109 // list.
1110 value_t trailing = cxdr(list, total_required);
1111 value_t last_item = cons(trailing, nil);
1112
1113 *cxdrref(&list, total_required) = last_item;
1114 }
1115 else if (nargs < total_required)
1116 {
1117 for (int i = nargs - args->num_required; i < args->num_optional; i++)
1118 {
1119 // Append the i-th defualt argument
1120 value_t appended = cons(args->optional_arguments[i].value, nil);
1121 *val = appended;
1122 val = cdrref(appended);
1123 }
1124 }
1125
1126 // We want to call this if we pass the correct # of arguments or less, just
1127 // not if we have already passed varargs. Appends a nil argument.
1128 if (nargs <= total_required)
1129 {
1130 // Enough real arguments but no variadic arguments. Pass a nil list.
1131 *val = cons(nil, nil);
1132 }
1133
1134 return _call_list(code_ptr, list, data);
1135}
1136
1137value_t call_list(struct function *fun, value_t list)
1138{
1139 return call_list_args(fun->code_ptr, fun->args, list, NULL);
swissChiliddc97542021-07-04 11:47:42 -07001140}
1141
1142value_t call_list_closure(struct closure *c, value_t list)
1143{
swissChili15f1cae2021-07-05 19:08:47 -07001144 return call_list_args(c->function, c->args, list, c->data);
1145}
1146
1147struct args *new_args()
1148{
1149 struct args *a = malloc(sizeof(struct args));
1150 a->num_optional = 0;
1151 a->num_required = 0;
1152 a->variadic = false;
1153
1154 return a;
1155}
1156
swissChili7e1393c2021-07-07 12:59:12 -07001157struct args *add_optional_arg(struct args *args, value_t name, value_t value)
swissChili15f1cae2021-07-05 19:08:47 -07001158{
1159 int i = args->num_optional++;
swissChili7e1393c2021-07-07 12:59:12 -07001160 args =
1161 realloc(args, sizeof(struct args) + sizeof(struct optional_argument) *
1162 args->num_optional);
swissChili15f1cae2021-07-05 19:08:47 -07001163
swissChili7e1393c2021-07-07 12:59:12 -07001164 args->optional_arguments[i] = (struct optional_argument){
1165 .value = value,
1166 .name = name,
swissChili15f1cae2021-07-05 19:08:47 -07001167 };
1168
1169 return args;
1170}
1171
1172bool are_args_acceptable(struct args *args, int number)
1173{
1174 if (args->variadic)
1175 {
1176 return number >= args->num_required;
1177 }
1178 else
1179 {
1180 return number >= args->num_required &&
swissChili7e1393c2021-07-07 12:59:12 -07001181 number <= args->num_required + args->num_optional;
swissChili15f1cae2021-07-05 19:08:47 -07001182 }
1183}
1184
swissChili6d02af42021-08-05 19:49:01 -07001185struct error list_to_args(struct environment *env, value_t list,
1186 struct local *local, struct args **a)
swissChili15f1cae2021-07-05 19:08:47 -07001187{
swissChili6d02af42021-08-05 19:49:01 -07001188 E_INIT();
1189
swissChili15f1cae2021-07-05 19:08:47 -07001190 struct args *args = new_args();
1191
1192 bool in_optional = false;
1193
1194 for (value_t i = list; !nilp(i); i = cdr(i))
1195 {
1196 value_t val = car(i);
swissChili6d02af42021-08-05 19:49:01 -07001197 NEARVAL(i);
1198
swissChili15f1cae2021-07-05 19:08:47 -07001199 if (symbolp(val))
1200 {
1201 if (!args->variadic && symstreq(val, "&"))
1202 {
1203 i = cdr(i);
1204 value_t name = car(i);
1205
1206 if (!symbolp(name))
1207 {
swissChili6d02af42021-08-05 19:49:01 -07001208 THROW(EEXPECTED, "You must provide a symbol after & in an argument list "
1209 "to bind the\n"
1210 "variadic arguments to.");
swissChili15f1cae2021-07-05 19:08:47 -07001211 }
1212
1213 args->variadic = true;
1214
1215 add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG),
swissChili7e1393c2021-07-07 12:59:12 -07001216 args->num_optional + args->num_required);
swissChili15f1cae2021-07-05 19:08:47 -07001217
1218 continue;
1219 }
1220
1221 if (!in_optional)
1222 {
swissChili7e1393c2021-07-07 12:59:12 -07001223 add_variable(local, V_ARGUMENT, (char *)(val ^ SYMBOL_TAG),
1224 args->num_required++);
swissChili15f1cae2021-07-05 19:08:47 -07001225 }
1226 else
1227 {
1228 char *name = (char *)(val ^ SYMBOL_TAG);
1229 if (name[0] == '&')
1230 {
swissChili6d02af42021-08-05 19:49:01 -07001231 THROW(EINVALID, "Non-optional argument following optional arguments "
1232 "starts with a &\n"
1233 "did you mean to declare a variadic argument? If so "
1234 "leave a space\n"
1235 "between the & and name.");
swissChili15f1cae2021-07-05 19:08:47 -07001236 }
1237 else
1238 {
swissChili6d02af42021-08-05 19:49:01 -07001239 THROW(EINVALID, "Cannot define a non-optional argument after an "
1240 "optional one.");
swissChili15f1cae2021-07-05 19:08:47 -07001241 }
1242 }
1243 }
1244 else if (listp(val))
1245 {
swissChili6d02af42021-08-05 19:49:01 -07001246 NEARVAL(val);
1247
swissChili15f1cae2021-07-05 19:08:47 -07001248 in_optional = true;
1249 int len = length(val);
1250
1251 if (len != 2)
1252 {
swissChili6d02af42021-08-05 19:49:01 -07001253 THROW(EINVALID, "A list defining an optional value must be structured like "
1254 "(name expr)\n"
1255 "with exactly two arguments.");
swissChili15f1cae2021-07-05 19:08:47 -07001256 }
1257
1258 value_t name = car(val);
1259 value_t expr = car(cdr(val));
1260
1261 value_t function = cons(nil, cons(expr, nil));
1262
swissChili6d02af42021-08-05 19:49:01 -07001263 dasm_State *d;
1264 TRY(compile_function(function, NS_ANONYMOUS, env, NULL, NULL, NULL,
1265 NULL, local->current_file_path, &d));
swissChili15f1cae2021-07-05 19:08:47 -07001266
1267 // TODO: GC stack top!
1268 value_t (*compiled)() = link_program(&d);
1269
1270 value_t value = compiled();
1271 args = add_optional_arg(args, name, value);
1272
swissChili7e1393c2021-07-07 12:59:12 -07001273 add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG),
1274 args->num_required + args->num_optional - 1);
swissChilic0acce42022-07-31 13:38:17 -07001275
1276 dasm_free(&d);
swissChili15f1cae2021-07-05 19:08:47 -07001277 }
1278 }
1279
swissChili6d02af42021-08-05 19:49:01 -07001280 *a = args;
1281 OKAY();
swissChili15f1cae2021-07-05 19:08:47 -07001282}
1283
1284void display_args(struct args *args)
1285{
1286 printf("Args object taking %d require arguments and %d optionals:\n",
swissChili7e1393c2021-07-07 12:59:12 -07001287 args->num_required, args->num_optional);
swissChili15f1cae2021-07-05 19:08:47 -07001288
1289 for (int i = 0; i < args->num_optional; i++)
1290 {
swissChili7e1393c2021-07-07 12:59:12 -07001291 printf(" %d\t%s\n", i,
1292 (char *)(args->optional_arguments[i].name ^ SYMBOL_TAG));
swissChili15f1cae2021-07-05 19:08:47 -07001293 printval(args->optional_arguments[i].value, 2);
1294 }
swissChili2999dd12021-07-02 14:19:53 -07001295}