blob: c2c0639fc66a2c10c741421a43b7b71c451eec7f [file] [log] [blame]
swissChili8cfb7c42021-04-18 21:17:58 -07001/* -*- mode:c -*- */
2
swissChilica107a02021-04-14 12:07:30 -07003#include "compiler.h"
swissChilif3e7f182021-04-20 13:57:22 -07004#include "lib/std.h"
swissChili53472e82021-05-08 16:06:32 -07005#include "plat/plat.h"
swissChiliddc97542021-07-04 11:47:42 -07006#include "gc.h"
swissChilica107a02021-04-14 12:07:30 -07007
8#include <dasm_proto.h>
9#include <dasm_x86.h>
10
swissChili923b5362021-05-09 20:31:43 -070011#include <stdlib.h>
12#include <string.h>
13
swissChili53472e82021-05-08 16:06:32 -070014#define value_size sizeof(value_t)
swissChilica107a02021-04-14 12:07:30 -070015
16|.arch x86;
17
18|.macro setup, nvars;
swissChili74348422021-07-04 13:23:24 -070019|->function_start:
swissChilica107a02021-04-14 12:07:30 -070020| push ebp;
21| mov ebp, esp;
swissChili8cfb7c42021-04-18 21:17:58 -070022| sub esp, (value_size * nvars);
swissChilica107a02021-04-14 12:07:30 -070023|.endmacro;
24
25|.macro cleanup;
26| mov esp, ebp;
27| pop ebp;
28| ret;
29|.endmacro;
30
swissChili67bdf282021-06-06 18:46:08 -070031|.macro local_var, index;
32|.endmacro;
33
swissChilica107a02021-04-14 12:07:30 -070034dasm_State *d;
35unsigned int npc = 8;
36
swissChili9e57da42021-06-15 22:22:46 -070037|.macro run_gc;
swissChilie9fec8b2021-06-22 13:59:33 -070038| mov eax, esp;
swissChili9e57da42021-06-15 22:22:46 -070039| push ebp;
swissChilie9fec8b2021-06-22 13:59:33 -070040| push eax;
swissChili9e57da42021-06-15 22:22:46 -070041| mov eax, _do_gc;
42| call eax;
43|.endmacro;
swissChili6d6525e2021-06-15 21:20:53 -070044
swissChili53472e82021-05-08 16:06:32 -070045struct function *find_function(struct environment *env, char *name)
swissChilica107a02021-04-14 12:07:30 -070046{
47 struct function *f = env->first;
48
swissChili53472e82021-05-08 16:06:32 -070049 while (strcmp(f->name, name) != 0)
swissChilica107a02021-04-14 12:07:30 -070050 {
swissChili53472e82021-05-08 16:06:32 -070051 if (f->prev)
swissChilica107a02021-04-14 12:07:30 -070052 f = f->prev;
53 else
54 return NULL;
55 }
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;
77 local->stack_slots = realloc(local->stack_slots, local->num_stack_slots * sizeof(bool));
78 // unreadable: set the remaining slots to unused
79 memset(local->stack_slots + old_size, 0, local->num_stack_slots - old_size);
80 local->stack_slots[old_size] = true;
81
82 return old_size;
83}
84
85void local_free(struct local *local, unsigned int slot)
86{
87 local->stack_slots[slot] = false;
88}
89
swissChili708d4c42021-07-04 17:40:07 -070090void del_local(struct local *local)
91{
92 free(local->stack_slots);
93
94 for (struct variable *next, *f = local->first; f; f = next)
95 {
96 next = f->prev;
97 free(f);
98 }
99}
100
101void del_env(struct environment *env)
102{
103 for (struct function *next, *f = env->first; f; f = next)
104 {
105 next = f->prev;
106 // We're not gonna bother munmap()ing the function
107 free(f);
108 }
109}
110
swissChilif1ba8c12021-07-02 18:45:38 -0700111struct dasm_State *compile_function(value_t args, enum namespace namespace,
swissChiliddc97542021-07-04 11:47:42 -0700112 struct environment *env, struct local *local_out,
swissChili74348422021-07-04 13:23:24 -0700113 struct local *local_parent, int *nargs, char *name)
swissChilif1ba8c12021-07-02 18:45:38 -0700114{
115 dasm_State *d;
116 dasm_State **Dst = &d;
117
118 |.section code;
119 dasm_init(&d, DASM_MAXSECTION);
120
121 |.globals lbl_;
122 void *labels[lbl__MAX];
123 dasm_setupglobal(&d, labels, lbl__MAX);
124
125 |.actionlist lisp_actions;
126 dasm_setup(&d, lisp_actions);
127
128 struct local local;
129 local.parent = NULL;
130 local.first = NULL;
131 local.num_vars = 0;
132 local.npc = 8;
133 local.nextpc = 0;
134 local.stack_slots = malloc(sizeof(bool) * 4);
135 memset(local.stack_slots, 0, sizeof(bool) * 4);
136 local.num_stack_slots = 4;
137 local.num_stack_entries = 0;
swissChiliddc97542021-07-04 11:47:42 -0700138 local.num_closure_slots = 0;
139 local.parent = local_parent;
swissChili74348422021-07-04 13:23:24 -0700140 local.current_function_name = name;
swissChilif1ba8c12021-07-02 18:45:38 -0700141
142 dasm_growpc(&d, local.npc);
143
swissChilif1ba8c12021-07-02 18:45:38 -0700144 value_t arglist = car(args);
145 value_t body = cdr(args);
146
swissChili74348422021-07-04 13:23:24 -0700147 local.num_args = length(arglist);
148
swissChilif1ba8c12021-07-02 18:45:38 -0700149 value_t a = arglist;
150 for (int i = 0; !nilp(a); a = cdr(a), i++)
151 {
152 if (!symbolp(car(a)))
153 {
154 err("defun argument must be a symbol");
155 }
156
157 add_variable(&local, V_ARGUMENT, (char *)(car(a) ^ SYMBOL_TAG), i);
158 }
159
160 for (value_t body_ = body; !nilp(body_); body_ = cdr(body_))
161 {
162 walk_and_alloc(&local, car(body_));
163 }
164
165 | setup (local.num_stack_entries);
166
167 memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool));
168 local.num_stack_entries = 0;
169
170 for (; !nilp(body); body = cdr(body))
171 {
172 compile_expression(env, &local, car(body), Dst);
173 }
174
175 | cleanup;
176
177 if (local_out)
178 *local_out = local;
179
180 if (nargs)
181 *nargs = length(arglist);
182
183 return d;
184}
185
swissChili53472e82021-05-08 16:06:32 -0700186void compile_tl(value_t val, struct environment *env)
swissChilica107a02021-04-14 12:07:30 -0700187{
swissChili53472e82021-05-08 16:06:32 -0700188 if (!listp(val))
189 err("Top level must be a list");
swissChilica107a02021-04-14 12:07:30 -0700190
swissChili53472e82021-05-08 16:06:32 -0700191 value_t form = car(val);
192 value_t args = cdr(val);
193
swissChili2999dd12021-07-02 14:19:53 -0700194 if (symstreq(form, "defun") || symstreq(form, "defmacro"))
swissChili8fc5e2f2021-04-22 13:45:10 -0700195 {
swissChili2999dd12021-07-02 14:19:53 -0700196 enum namespace namespace = NS_FUNCTION;
197
198 if (symstreq(form, "defmacro"))
199 namespace = NS_MACRO;
200
swissChili8fc5e2f2021-04-22 13:45:10 -0700201 struct local local;
swissChilif1ba8c12021-07-02 18:45:38 -0700202 int nargs;
swissChili74348422021-07-04 13:23:24 -0700203 char *name = (char *)(car(args) ^ SYMBOL_TAG);
204 dasm_State *d = compile_function(cdr(args), namespace, env, &local, NULL, &nargs, name);
swissChilia820dea2021-05-09 16:46:55 -0700205
swissChili74348422021-07-04 13:23:24 -0700206 add_function(env, name, link(&d),
swissChilif1ba8c12021-07-02 18:45:38 -0700207 nargs, namespace);
swissChili8fc5e2f2021-04-22 13:45:10 -0700208
swissChili53472e82021-05-08 16:06:32 -0700209 dasm_free(&d);
swissChili708d4c42021-07-04 17:40:07 -0700210 del_local(&local);
swissChili67bdf282021-06-06 18:46:08 -0700211 }
212}
213
214void walk_and_alloc(struct local *local, value_t body)
215{
216 if (!listp(body))
217 return;
218
219 value_t args = cdr(body);
220
221 if (symstreq(car(body), "let1"))
222 {
223 int slot = local_alloc(local);
224
225 value_t expr = cdr(args);
swissChilif1ba8c12021-07-02 18:45:38 -0700226 for (; !nilp(expr); expr = cdr(expr))
227 {
swissChiliddc97542021-07-04 11:47:42 -0700228 walk_and_alloc(local, car(expr));
swissChilif1ba8c12021-07-02 18:45:38 -0700229 }
swissChili67bdf282021-06-06 18:46:08 -0700230
231 local_free(local, slot);
232 }
swissChilif1ba8c12021-07-02 18:45:38 -0700233 else if (symstreq(car(body), "lambda"))
234 {
235 // We don't want to walk the lambda because it's another function. When
236 // the lambda is compiled it will be walked.
237 return;
238 }
swissChili67bdf282021-06-06 18:46:08 -0700239 else
240 {
241 for (; !nilp(args); args = cdr(args))
242 {
243 walk_and_alloc(local, car(args));
244 }
swissChili8fc5e2f2021-04-22 13:45:10 -0700245 }
246}
247
swissChili53472e82021-05-08 16:06:32 -0700248struct environment compile_all(struct istream *is)
swissChili8fc5e2f2021-04-22 13:45:10 -0700249{
swissChilib8fd4712021-06-23 15:32:04 -0700250 unsigned char pool = make_pool();
251 unsigned char pop = push_pool(pool);
252
swissChili8fc5e2f2021-04-22 13:45:10 -0700253 value_t val;
swissChilif3e7f182021-04-20 13:57:22 -0700254 struct environment env;
255 env.first = NULL;
swissChili53472e82021-05-08 16:06:32 -0700256 load_std(&env);
257
258 while (read1(is, &val))
swissChili8fc5e2f2021-04-22 13:45:10 -0700259 {
swissChili53472e82021-05-08 16:06:32 -0700260 compile_tl(val, &env);
swissChili8fc5e2f2021-04-22 13:45:10 -0700261 }
swissChilif3e7f182021-04-20 13:57:22 -0700262
swissChilib8fd4712021-06-23 15:32:04 -0700263 pop_pool(pop);
264
swissChili8fc5e2f2021-04-22 13:45:10 -0700265 return env;
swissChilica107a02021-04-14 12:07:30 -0700266}
swissChilib3ca4fb2021-04-20 10:33:00 -0700267
swissChili53472e82021-05-08 16:06:32 -0700268int nextpc(struct local *local, dasm_State **Dst)
swissChilib3ca4fb2021-04-20 10:33:00 -0700269{
swissChili53472e82021-05-08 16:06:32 -0700270 int n = local->nextpc++;
271 if (n > local->npc)
272 {
273 local->npc += 16;
274 dasm_growpc(Dst, local->npc);
275 }
276 return n;
277}
278
swissChili6b47b6d2021-06-30 22:08:55 -0700279void compile_backquote(struct environment *env, struct local *local,
280 value_t val, dasm_State **Dst)
281{
282 if (!listp(val))
283 {
284 | mov eax, (val);
285 }
286 else
287 {
288 value_t fsym = car(val),
289 args = cdr(val);
290 int nargs = length(args);
291
292 // TODO
293 }
294}
295
swissChiliddc97542021-07-04 11:47:42 -0700296void compile_variable(struct variable *v, dasm_State *Dst)
297{
298 switch (v->type)
299 {
300 case V_ARGUMENT:
301 | mov eax, dword [ebp + (value_size * (v->number + 2))];
302 break;
303 case V_BOUND:
304 | mov eax, dword [ebp - ((v->number + 1) * value_size)];
305 break;
306 case V_FREE:
307 // edi is the closure context pointer
308 | mov eax, dword [edi + (v->number * value_size)];
309 break;
310 default:
311 err("Sorry, can only access V_ARGUMENT, V_FREE and V_BOUND variables for now :(");
312 }
313}
314
swissChili53472e82021-05-08 16:06:32 -0700315void compile_expression(struct environment *env, struct local *local,
316 value_t val, dasm_State **Dst)
317{
318 if (symstreq(val, "nil"))
319 {
320 | mov eax, (nil);
321 }
swissChili923b5362021-05-09 20:31:43 -0700322 else if (symstreq(val, "t"))
323 {
324 | mov eax, (t);
325 }
326 else if (integerp(val) || stringp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700327 {
328 | mov eax, val;
329 }
swissChili53472e82021-05-08 16:06:32 -0700330 else if (listp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700331 {
swissChili53472e82021-05-08 16:06:32 -0700332 value_t fsym = car(val);
333 value_t args = cdr(val);
334 int nargs = length(args);
swissChilib3ca4fb2021-04-20 10:33:00 -0700335
swissChili53472e82021-05-08 16:06:32 -0700336 if (!symbolp(fsym))
swissChilif3e7f182021-04-20 13:57:22 -0700337 {
swissChili53472e82021-05-08 16:06:32 -0700338 err("function name must be a symbol");
swissChilif3e7f182021-04-20 13:57:22 -0700339 }
340
swissChili53472e82021-05-08 16:06:32 -0700341 if (symstreq(fsym, "if"))
swissChilib3ca4fb2021-04-20 10:33:00 -0700342 {
swissChili53472e82021-05-08 16:06:32 -0700343 if (nargs < 2 || nargs > 3)
344 err("Must give at least 2 arguments to if");
swissChilib3ca4fb2021-04-20 10:33:00 -0700345
swissChili53472e82021-05-08 16:06:32 -0700346 compile_expression(env, local, car(args), Dst);
347 int false_label = nextpc(local, Dst),
348 after_label = nextpc(local, Dst);
349
350 // result is in eax
351 | cmp eax, (nil);
352 | je =>false_label;
353
354 compile_expression(env, local, elt(args, 1), Dst);
swissChilia820dea2021-05-09 16:46:55 -0700355 | jmp =>after_label;
swissChili923b5362021-05-09 20:31:43 -0700356 |=>false_label:;
swissChili53472e82021-05-08 16:06:32 -0700357 if (nargs == 3)
358 compile_expression(env, local, elt(args, 2), Dst);
359 |=>after_label:
360 }
swissChili67bdf282021-06-06 18:46:08 -0700361 else if (symstreq(fsym, "let1"))
362 {
363 if (nargs < 2)
364 {
365 err("Must give at least 2 arguments to let1");
366 }
367 value_t binding = car(args);
368 value_t rest = cdr(args);
369
370 if (length(binding) != 2)
371 {
372 err("Binding list in let1 must contain exactly two entries");
373 }
374
375 value_t name = car(binding);
376 value_t value = car(cdr(binding));
377
378 compile_expression(env, local, value, Dst);
379
380 int i = local_alloc(local);
381
382 add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i);
383
384 | mov dword [ebp - ((i + 1) * value_size)], eax;
385
386 for (; !nilp(rest); rest = cdr(rest))
387 {
388 compile_expression(env, local, car(rest), Dst);
389 }
390
391 local_free(local, i);
392 }
swissChilie9fec8b2021-06-22 13:59:33 -0700393 else if (symstreq(fsym, "gc"))
394 {
395 if (nargs)
396 {
397 err("gc takes no arguments");
398 }
399
400 | run_gc;
401 }
swissChili6b47b6d2021-06-30 22:08:55 -0700402 else if (symstreq(fsym, "quote"))
403 {
404 if (nargs != 1)
405 err("quote should take exactly 1 argument");
406
407 // Simple!
408 | mov eax, (car(args));
409 }
410 else if (symstreq(fsym, "backquote"))
411 {
412 if (nargs != 1)
413 err("backquote should take exactly 1 argument");
414
415 compile_backquote(env, local, car(args), Dst);
416 }
swissChili74348422021-07-04 13:23:24 -0700417 else if (symstreq(fsym, "function"))
418 {
419 if (nargs != 1)
420 {
421 err("function should take exactly 1 argument");
422 }
423
424 if (!symbolp(car(args)))
425 {
426 err("argument to function should be a symbol resolvable at compile time");
427 }
428
429 struct function *f = find_function(env, (char *)(car(args) ^ SYMBOL_TAG));
430 value_t closure = create_closure(f->code_ptr, f->nargs, 0);
431
432 | mov eax, (closure);
433 }
swissChili6b47b6d2021-06-30 22:08:55 -0700434 else if (symstreq(fsym, "list"))
435 {
436 | push (nil);
437
438 for (int i = nargs - 1; i >= 0; i--)
439 {
440 compile_expression(env, local, elt(args, i), Dst);
441
442 // push the ith item
443 | push eax;
444 // cons the top two stack items
445 | mov ebx, (cons);
446 | call ebx;
447 // remove the stack items from use
448 | add esp, (2 * value_size);
449 // put the new thing on the stack
450 | push eax;
451 }
452
453 | pop eax;
454 }
swissChiliddc97542021-07-04 11:47:42 -0700455 else if (symstreq(fsym, "lambda"))
456 {
457 // Compile the function with this as the parent scope
458 struct local new_local;
459 int nargs_out;
swissChili74348422021-07-04 13:23:24 -0700460 dasm_State *d = compile_function(args, NS_ANONYMOUS, env, &new_local, local, &nargs_out, "recurse");
swissChiliddc97542021-07-04 11:47:42 -0700461
462 // Link the function
463 void *func_ptr = link(&d);
464
465 // Create a closure object with the correct number of captures at
466 // runtime
swissChiliddc97542021-07-04 11:47:42 -0700467 | push (new_local.num_closure_slots);
468 | push (nargs_out);
469 | push (func_ptr);
swissChili74348422021-07-04 13:23:24 -0700470 | mov ebx, (create_closure);
swissChiliddc97542021-07-04 11:47:42 -0700471 | call ebx;
472 | add esp, 12;
473
474 // Walk the generated local scope for V_FREE variables, since each
475 // of these exists in our scope (or higher), evaluate it and set it
476 // as a member of the lambda capture.
477
478 for (struct variable *var = new_local.first; var; var = var->prev)
479 {
480 if (var->type == V_FREE)
481 {
482 // Closure in eax
483 | push eax;
484 // Variable now in eax
485 compile_variable(find_variable(local, var->name), Dst);
486 | push eax;
487
swissChiliddc97542021-07-04 11:47:42 -0700488 // The capture offset
489 | push (var->number);
swissChili74348422021-07-04 13:23:24 -0700490 | mov ebx, (set_closure_capture_variable);
swissChiliddc97542021-07-04 11:47:42 -0700491 | call ebx;
492 // Skip the value and index
493 | add esp, 8;
494 // Pop the closure back in to eax
495 | pop eax;
496 }
497 }
498
499 // Closure is still in eax
500
501 dasm_free(&d);
swissChili708d4c42021-07-04 17:40:07 -0700502 del_local(&new_local);
swissChiliddc97542021-07-04 11:47:42 -0700503 }
swissChili53472e82021-05-08 16:06:32 -0700504 else
505 {
swissChili74348422021-07-04 13:23:24 -0700506 char *name = (char *)(fsym ^ SYMBOL_TAG);
507 struct function *func = find_function(env, name);
508
509 bool is_recursive = false;
510 int nargs_needed = 0;
swissChili53472e82021-05-08 16:06:32 -0700511
swissChili74348422021-07-04 13:23:24 -0700512 if (symstreq(fsym, local->current_function_name))
swissChilif1ba8c12021-07-02 18:45:38 -0700513 {
swissChili74348422021-07-04 13:23:24 -0700514 is_recursive = true;
515 nargs_needed = local->num_args;
516 }
517 else
518 {
519 if (func == NULL)
520 {
521 fprintf(stderr, "Function call: %s at %s:%d\n", name, cons_file(val), cons_line(val));
522 err("Function undefined");
523 }
524
525 nargs_needed = func->nargs;
526 }
527
528 if (nargs != nargs_needed)
529 {
530 fprintf(stderr, "Function call: %s at %s:%d, want %d args but given %d\n",
531 name, cons_file(val), cons_line(val), nargs_needed, nargs);
swissChili53472e82021-05-08 16:06:32 -0700532 err("wrong number of args");
swissChilif1ba8c12021-07-02 18:45:38 -0700533 }
swissChili53472e82021-05-08 16:06:32 -0700534
swissChili74348422021-07-04 13:23:24 -0700535 if (is_recursive || func->namespace == NS_FUNCTION)
swissChili53472e82021-05-08 16:06:32 -0700536 {
swissChili2999dd12021-07-02 14:19:53 -0700537 for (int i = length(args) - 1; i >= 0; i--)
538 {
539 compile_expression(env, local, elt(args, i), Dst);
540 | push eax;
541 }
swissChili74348422021-07-04 13:23:24 -0700542
543 if (is_recursive)
544 {
545 | call ->function_start;
546 }
547 else
548 {
549 | mov ebx, (func->code_addr);
550 | call ebx;
551 }
swissChili2999dd12021-07-02 14:19:53 -0700552 | add esp, (nargs * value_size);
553 // result in eax
554 }
555 else if (func->namespace == NS_MACRO)
556 {
557 value_t expanded_to = call_list(func, args);
558
swissChili2999dd12021-07-02 14:19:53 -0700559 compile_expression(env, local, expanded_to, Dst);
560 }
swissChili53472e82021-05-08 16:06:32 -0700561 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700562 }
swissChili923b5362021-05-09 20:31:43 -0700563 else if (symbolp(val))
564 {
swissChili923b5362021-05-09 20:31:43 -0700565 struct variable *v = find_variable(local, (char *)(val ^ SYMBOL_TAG));
566
567 if (!v)
swissChilie9fec8b2021-06-22 13:59:33 -0700568 {
569 fprintf(stderr, "var: %s\n", (char *)(val ^ SYMBOL_TAG));
swissChili923b5362021-05-09 20:31:43 -0700570 err("Variable unbound");
swissChilie9fec8b2021-06-22 13:59:33 -0700571 }
swissChili923b5362021-05-09 20:31:43 -0700572
swissChiliddc97542021-07-04 11:47:42 -0700573 compile_variable(v, Dst);
swissChili923b5362021-05-09 20:31:43 -0700574 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700575}
swissChilif3e7f182021-04-20 13:57:22 -0700576
swissChili53472e82021-05-08 16:06:32 -0700577void compile_expr_to_func(struct environment *env, char *name, value_t val,
578 dasm_State **Dst)
swissChilif3e7f182021-04-20 13:57:22 -0700579{
580 | setup 0;
581
582 struct local local;
swissChili53472e82021-05-08 16:06:32 -0700583 compile_expression(env, &local, val, Dst);
584
swissChilif3e7f182021-04-20 13:57:22 -0700585 | cleanup;
586
swissChili2999dd12021-07-02 14:19:53 -0700587 add_function(env, name, link(Dst), 0, NS_FUNCTION);
swissChilif3e7f182021-04-20 13:57:22 -0700588}
swissChili923b5362021-05-09 20:31:43 -0700589
590struct variable *add_variable(struct local *local, enum var_type type,
591 char *name, int number)
592{
593 struct variable *var = malloc(sizeof(struct variable));
594 var->prev = local->first;
595 var->type = type;
596 var->name = name;
597 var->number = number;
598
599 local->first = var;
600
601 return var;
602}
603
604void destroy_local(struct local *local)
605{
606 for (struct variable *v = local->first; v;)
607 {
608 struct variable *t = v;
609 v = v->prev;
610 free(t);
611 }
612}
613
614struct variable *find_variable(struct local *local, char *name)
615{
616 struct variable *v = local->first;
617
618 for (; v && strcmp(v->name, name) != 0; v = v->prev)
619 {}
620
swissChiliddc97542021-07-04 11:47:42 -0700621 if (!v)
622 {
623 if (local->parent)
624 {
625 v = find_variable(local->parent, name);
626
627 if (v)
628 {
629 // We found this in a parent scope, add it as a V_FREE variable to skip the search.
630 v = add_variable(local, V_FREE, name, local->num_closure_slots++);
631 }
632 }
633 }
swissChili923b5362021-05-09 20:31:43 -0700634 return v;
635}
swissChili2999dd12021-07-02 14:19:53 -0700636
swissChiliddc97542021-07-04 11:47:42 -0700637extern value_t _call_list(void *addr, value_t list, value_t *edi);
swissChili2999dd12021-07-02 14:19:53 -0700638
639value_t call_list(struct function *func, value_t list)
640{
swissChiliddc97542021-07-04 11:47:42 -0700641 return _call_list(func->code_ptr, list, NULL);
642}
643
644value_t call_list_closure(struct closure *c, value_t list)
645{
646 return _call_list(c->function, list, c->data);
swissChili2999dd12021-07-02 14:19:53 -0700647}