blob: 8d96a5277bc4f20aa4877b84a1cd3b54b2202314 [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;
19| push ebp;
20| mov ebp, esp;
swissChili8cfb7c42021-04-18 21:17:58 -070021| sub esp, (value_size * nvars);
swissChilica107a02021-04-14 12:07:30 -070022|.endmacro;
23
24|.macro cleanup;
25| mov esp, ebp;
26| pop ebp;
27| ret;
28|.endmacro;
29
swissChili67bdf282021-06-06 18:46:08 -070030|.macro local_var, index;
31|.endmacro;
32
swissChilica107a02021-04-14 12:07:30 -070033dasm_State *d;
34unsigned int npc = 8;
35
swissChili9e57da42021-06-15 22:22:46 -070036|.macro run_gc;
swissChilie9fec8b2021-06-22 13:59:33 -070037| mov eax, esp;
swissChili9e57da42021-06-15 22:22:46 -070038| push ebp;
swissChilie9fec8b2021-06-22 13:59:33 -070039| push eax;
swissChili9e57da42021-06-15 22:22:46 -070040| mov eax, _do_gc;
41| call eax;
42|.endmacro;
swissChili6d6525e2021-06-15 21:20:53 -070043
swissChili53472e82021-05-08 16:06:32 -070044struct function *find_function(struct environment *env, char *name)
swissChilica107a02021-04-14 12:07:30 -070045{
46 struct function *f = env->first;
47
swissChili53472e82021-05-08 16:06:32 -070048 while (strcmp(f->name, name) != 0)
swissChilica107a02021-04-14 12:07:30 -070049 {
swissChili53472e82021-05-08 16:06:32 -070050 if (f->prev)
swissChilica107a02021-04-14 12:07:30 -070051 f = f->prev;
52 else
53 return NULL;
54 }
55
56 return f;
57}
58
swissChili67bdf282021-06-06 18:46:08 -070059unsigned int local_alloc(struct local *local)
60{
61 for (int i = 0; i < local->num_stack_slots; i++)
62 {
63 if (local->stack_slots[i] == false)
64 {
65 local->stack_slots[i] = true;
66
67 if (i >= local->num_stack_entries)
68 local->num_stack_entries++;
69
70 return i;
71 }
72 }
73
74 int old_size = local->num_stack_slots;
75 local->num_stack_slots += 4;
76 local->stack_slots = realloc(local->stack_slots, local->num_stack_slots * sizeof(bool));
77 // unreadable: set the remaining slots to unused
78 memset(local->stack_slots + old_size, 0, local->num_stack_slots - old_size);
79 local->stack_slots[old_size] = true;
80
81 return old_size;
82}
83
84void local_free(struct local *local, unsigned int slot)
85{
86 local->stack_slots[slot] = false;
87}
88
swissChilif1ba8c12021-07-02 18:45:38 -070089struct dasm_State *compile_function(value_t args, enum namespace namespace,
swissChiliddc97542021-07-04 11:47:42 -070090 struct environment *env, struct local *local_out,
91 struct local *local_parent, int *nargs)
swissChilif1ba8c12021-07-02 18:45:38 -070092{
93 dasm_State *d;
94 dasm_State **Dst = &d;
95
96 |.section code;
97 dasm_init(&d, DASM_MAXSECTION);
98
99 |.globals lbl_;
100 void *labels[lbl__MAX];
101 dasm_setupglobal(&d, labels, lbl__MAX);
102
103 |.actionlist lisp_actions;
104 dasm_setup(&d, lisp_actions);
105
106 struct local local;
107 local.parent = NULL;
108 local.first = NULL;
109 local.num_vars = 0;
110 local.npc = 8;
111 local.nextpc = 0;
112 local.stack_slots = malloc(sizeof(bool) * 4);
113 memset(local.stack_slots, 0, sizeof(bool) * 4);
114 local.num_stack_slots = 4;
115 local.num_stack_entries = 0;
swissChiliddc97542021-07-04 11:47:42 -0700116 local.num_closure_slots = 0;
117 local.parent = local_parent;
swissChilif1ba8c12021-07-02 18:45:38 -0700118
119 dasm_growpc(&d, local.npc);
120
121 // Generate code
122 // TODO: first pass, extract bound and free variables
123
swissChilif1ba8c12021-07-02 18:45:38 -0700124 value_t arglist = car(args);
125 value_t body = cdr(args);
126
swissChilif1ba8c12021-07-02 18:45:38 -0700127 value_t a = arglist;
128 for (int i = 0; !nilp(a); a = cdr(a), i++)
129 {
130 if (!symbolp(car(a)))
131 {
132 err("defun argument must be a symbol");
133 }
134
135 add_variable(&local, V_ARGUMENT, (char *)(car(a) ^ SYMBOL_TAG), i);
136 }
137
138 for (value_t body_ = body; !nilp(body_); body_ = cdr(body_))
139 {
140 walk_and_alloc(&local, car(body_));
141 }
142
143 | setup (local.num_stack_entries);
144
145 memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool));
146 local.num_stack_entries = 0;
147
148 for (; !nilp(body); body = cdr(body))
149 {
150 compile_expression(env, &local, car(body), Dst);
151 }
152
153 | cleanup;
154
155 if (local_out)
156 *local_out = local;
157
158 if (nargs)
159 *nargs = length(arglist);
160
161 return d;
swissChiliddc97542021-07-04 11:47:42 -0700162
163 // TODO: local leaks memory! free variables too, not just stack slots (in
164 // two places). Add a free_local() function that does this.
swissChilif1ba8c12021-07-02 18:45:38 -0700165}
166
swissChili53472e82021-05-08 16:06:32 -0700167void compile_tl(value_t val, struct environment *env)
swissChilica107a02021-04-14 12:07:30 -0700168{
swissChili53472e82021-05-08 16:06:32 -0700169 if (!listp(val))
170 err("Top level must be a list");
swissChilica107a02021-04-14 12:07:30 -0700171
swissChili53472e82021-05-08 16:06:32 -0700172 value_t form = car(val);
173 value_t args = cdr(val);
174
swissChili2999dd12021-07-02 14:19:53 -0700175 if (symstreq(form, "defun") || symstreq(form, "defmacro"))
swissChili8fc5e2f2021-04-22 13:45:10 -0700176 {
swissChili2999dd12021-07-02 14:19:53 -0700177 enum namespace namespace = NS_FUNCTION;
178
179 if (symstreq(form, "defmacro"))
180 namespace = NS_MACRO;
181
swissChili8fc5e2f2021-04-22 13:45:10 -0700182 struct local local;
swissChilif1ba8c12021-07-02 18:45:38 -0700183 int nargs;
swissChiliddc97542021-07-04 11:47:42 -0700184 dasm_State *d = compile_function(cdr(args), namespace, env, &local, NULL, &nargs);
swissChilia820dea2021-05-09 16:46:55 -0700185
swissChilif1ba8c12021-07-02 18:45:38 -0700186 add_function(env, (char *)(car(args) ^ SYMBOL_TAG), link(&d),
187 nargs, namespace);
swissChili8fc5e2f2021-04-22 13:45:10 -0700188
swissChili53472e82021-05-08 16:06:32 -0700189 dasm_free(&d);
swissChili67bdf282021-06-06 18:46:08 -0700190 free(local.stack_slots);
191 }
192}
193
194void walk_and_alloc(struct local *local, value_t body)
195{
196 if (!listp(body))
197 return;
198
199 value_t args = cdr(body);
200
201 if (symstreq(car(body), "let1"))
202 {
203 int slot = local_alloc(local);
204
205 value_t expr = cdr(args);
swissChilif1ba8c12021-07-02 18:45:38 -0700206 for (; !nilp(expr); expr = cdr(expr))
207 {
swissChiliddc97542021-07-04 11:47:42 -0700208 walk_and_alloc(local, car(expr));
swissChilif1ba8c12021-07-02 18:45:38 -0700209 }
swissChili67bdf282021-06-06 18:46:08 -0700210
211 local_free(local, slot);
212 }
swissChilif1ba8c12021-07-02 18:45:38 -0700213 else if (symstreq(car(body), "lambda"))
214 {
215 // We don't want to walk the lambda because it's another function. When
216 // the lambda is compiled it will be walked.
217 return;
218 }
swissChili67bdf282021-06-06 18:46:08 -0700219 else
220 {
221 for (; !nilp(args); args = cdr(args))
222 {
223 walk_and_alloc(local, car(args));
224 }
swissChili8fc5e2f2021-04-22 13:45:10 -0700225 }
226}
227
swissChili53472e82021-05-08 16:06:32 -0700228struct environment compile_all(struct istream *is)
swissChili8fc5e2f2021-04-22 13:45:10 -0700229{
swissChilib8fd4712021-06-23 15:32:04 -0700230 unsigned char pool = make_pool();
231 unsigned char pop = push_pool(pool);
232
swissChili8fc5e2f2021-04-22 13:45:10 -0700233 value_t val;
swissChilif3e7f182021-04-20 13:57:22 -0700234 struct environment env;
235 env.first = NULL;
swissChili53472e82021-05-08 16:06:32 -0700236 load_std(&env);
237
238 while (read1(is, &val))
swissChili8fc5e2f2021-04-22 13:45:10 -0700239 {
swissChili53472e82021-05-08 16:06:32 -0700240 compile_tl(val, &env);
swissChili8fc5e2f2021-04-22 13:45:10 -0700241 }
swissChilif3e7f182021-04-20 13:57:22 -0700242
swissChilib8fd4712021-06-23 15:32:04 -0700243 pop_pool(pop);
244
swissChili8fc5e2f2021-04-22 13:45:10 -0700245 return env;
swissChilica107a02021-04-14 12:07:30 -0700246}
swissChilib3ca4fb2021-04-20 10:33:00 -0700247
swissChili53472e82021-05-08 16:06:32 -0700248int nextpc(struct local *local, dasm_State **Dst)
swissChilib3ca4fb2021-04-20 10:33:00 -0700249{
swissChili53472e82021-05-08 16:06:32 -0700250 int n = local->nextpc++;
251 if (n > local->npc)
252 {
253 local->npc += 16;
254 dasm_growpc(Dst, local->npc);
255 }
256 return n;
257}
258
swissChili6b47b6d2021-06-30 22:08:55 -0700259void compile_backquote(struct environment *env, struct local *local,
260 value_t val, dasm_State **Dst)
261{
262 if (!listp(val))
263 {
264 | mov eax, (val);
265 }
266 else
267 {
268 value_t fsym = car(val),
269 args = cdr(val);
270 int nargs = length(args);
271
272 // TODO
273 }
274}
275
swissChiliddc97542021-07-04 11:47:42 -0700276void compile_variable(struct variable *v, dasm_State *Dst)
277{
278 switch (v->type)
279 {
280 case V_ARGUMENT:
281 | mov eax, dword [ebp + (value_size * (v->number + 2))];
282 break;
283 case V_BOUND:
284 | mov eax, dword [ebp - ((v->number + 1) * value_size)];
285 break;
286 case V_FREE:
287 // edi is the closure context pointer
288 | mov eax, dword [edi + (v->number * value_size)];
289 break;
290 default:
291 err("Sorry, can only access V_ARGUMENT, V_FREE and V_BOUND variables for now :(");
292 }
293}
294
swissChili53472e82021-05-08 16:06:32 -0700295void compile_expression(struct environment *env, struct local *local,
296 value_t val, dasm_State **Dst)
297{
298 if (symstreq(val, "nil"))
299 {
300 | mov eax, (nil);
301 }
swissChili923b5362021-05-09 20:31:43 -0700302 else if (symstreq(val, "t"))
303 {
304 | mov eax, (t);
305 }
306 else if (integerp(val) || stringp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700307 {
308 | mov eax, val;
309 }
swissChili53472e82021-05-08 16:06:32 -0700310 else if (listp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700311 {
swissChili53472e82021-05-08 16:06:32 -0700312 value_t fsym = car(val);
313 value_t args = cdr(val);
314 int nargs = length(args);
swissChilib3ca4fb2021-04-20 10:33:00 -0700315
swissChili53472e82021-05-08 16:06:32 -0700316 if (!symbolp(fsym))
swissChilif3e7f182021-04-20 13:57:22 -0700317 {
swissChili53472e82021-05-08 16:06:32 -0700318 err("function name must be a symbol");
swissChilif3e7f182021-04-20 13:57:22 -0700319 }
320
swissChili53472e82021-05-08 16:06:32 -0700321 if (symstreq(fsym, "if"))
swissChilib3ca4fb2021-04-20 10:33:00 -0700322 {
swissChili53472e82021-05-08 16:06:32 -0700323 if (nargs < 2 || nargs > 3)
324 err("Must give at least 2 arguments to if");
swissChilib3ca4fb2021-04-20 10:33:00 -0700325
swissChili53472e82021-05-08 16:06:32 -0700326 compile_expression(env, local, car(args), Dst);
327 int false_label = nextpc(local, Dst),
328 after_label = nextpc(local, Dst);
329
330 // result is in eax
331 | cmp eax, (nil);
332 | je =>false_label;
333
334 compile_expression(env, local, elt(args, 1), Dst);
swissChilia820dea2021-05-09 16:46:55 -0700335 | jmp =>after_label;
swissChili923b5362021-05-09 20:31:43 -0700336 |=>false_label:;
swissChili53472e82021-05-08 16:06:32 -0700337 if (nargs == 3)
338 compile_expression(env, local, elt(args, 2), Dst);
339 |=>after_label:
340 }
swissChili67bdf282021-06-06 18:46:08 -0700341 else if (symstreq(fsym, "let1"))
342 {
343 if (nargs < 2)
344 {
345 err("Must give at least 2 arguments to let1");
346 }
347 value_t binding = car(args);
348 value_t rest = cdr(args);
349
350 if (length(binding) != 2)
351 {
352 err("Binding list in let1 must contain exactly two entries");
353 }
354
355 value_t name = car(binding);
356 value_t value = car(cdr(binding));
357
358 compile_expression(env, local, value, Dst);
359
360 int i = local_alloc(local);
361
362 add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i);
363
364 | mov dword [ebp - ((i + 1) * value_size)], eax;
365
366 for (; !nilp(rest); rest = cdr(rest))
367 {
368 compile_expression(env, local, car(rest), Dst);
369 }
370
371 local_free(local, i);
372 }
swissChilie9fec8b2021-06-22 13:59:33 -0700373 else if (symstreq(fsym, "gc"))
374 {
375 if (nargs)
376 {
377 err("gc takes no arguments");
378 }
379
380 | run_gc;
381 }
swissChili6b47b6d2021-06-30 22:08:55 -0700382 else if (symstreq(fsym, "quote"))
383 {
384 if (nargs != 1)
385 err("quote should take exactly 1 argument");
386
387 // Simple!
388 | mov eax, (car(args));
389 }
390 else if (symstreq(fsym, "backquote"))
391 {
392 if (nargs != 1)
393 err("backquote should take exactly 1 argument");
394
395 compile_backquote(env, local, car(args), Dst);
396 }
397 else if (symstreq(fsym, "list"))
398 {
399 | push (nil);
400
401 for (int i = nargs - 1; i >= 0; i--)
402 {
403 compile_expression(env, local, elt(args, i), Dst);
404
405 // push the ith item
406 | push eax;
407 // cons the top two stack items
408 | mov ebx, (cons);
409 | call ebx;
410 // remove the stack items from use
411 | add esp, (2 * value_size);
412 // put the new thing on the stack
413 | push eax;
414 }
415
416 | pop eax;
417 }
swissChiliddc97542021-07-04 11:47:42 -0700418 else if (symstreq(fsym, "lambda"))
419 {
420 // Compile the function with this as the parent scope
421 struct local new_local;
422 int nargs_out;
423 dasm_State *d = compile_function(args, NS_ANONYMOUS, env, &new_local, local, &nargs_out);
424
425 // Link the function
426 void *func_ptr = link(&d);
427
428 // Create a closure object with the correct number of captures at
429 // runtime
430 | mov ebx, (create_closure);
431 | push (new_local.num_closure_slots);
432 | push (nargs_out);
433 | push (func_ptr);
434 | call ebx;
435 | add esp, 12;
436
437 // Walk the generated local scope for V_FREE variables, since each
438 // of these exists in our scope (or higher), evaluate it and set it
439 // as a member of the lambda capture.
440
441 for (struct variable *var = new_local.first; var; var = var->prev)
442 {
443 if (var->type == V_FREE)
444 {
445 // Closure in eax
446 | push eax;
447 // Variable now in eax
448 compile_variable(find_variable(local, var->name), Dst);
449 | push eax;
450
451 | mov ebx, (set_closure_capture_variable);
452 // The capture offset
453 | push (var->number);
454 | call ebx;
455 // Skip the value and index
456 | add esp, 8;
457 // Pop the closure back in to eax
458 | pop eax;
459 }
460 }
461
462 // Closure is still in eax
463
464 dasm_free(&d);
465 free(new_local.stack_slots);
466 }
swissChili53472e82021-05-08 16:06:32 -0700467 else
468 {
469 struct function *func =
470 find_function(env, (char *)(fsym ^ SYMBOL_TAG));
471
swissChili923b5362021-05-09 20:31:43 -0700472 if (func == NULL)
473 err("Function undefined");
474
swissChili53472e82021-05-08 16:06:32 -0700475 if (nargs != func->nargs)
swissChilif1ba8c12021-07-02 18:45:38 -0700476 {
477 fprintf(stderr, "Function: %s at %s:%d\n", func->name, cons_file(val), cons_line(val));
swissChili53472e82021-05-08 16:06:32 -0700478 err("wrong number of args");
swissChilif1ba8c12021-07-02 18:45:38 -0700479 }
swissChili53472e82021-05-08 16:06:32 -0700480
swissChili2999dd12021-07-02 14:19:53 -0700481 if (func->namespace == NS_FUNCTION)
swissChili53472e82021-05-08 16:06:32 -0700482 {
swissChili2999dd12021-07-02 14:19:53 -0700483 for (int i = length(args) - 1; i >= 0; i--)
484 {
485 compile_expression(env, local, elt(args, i), Dst);
486 | push eax;
487 }
swissChili53472e82021-05-08 16:06:32 -0700488
swissChili2999dd12021-07-02 14:19:53 -0700489 | mov ebx, (func->code_addr);
490 | call ebx;
491 | add esp, (nargs * value_size);
492 // result in eax
493 }
494 else if (func->namespace == NS_MACRO)
495 {
496 value_t expanded_to = call_list(func, args);
497
swissChili2999dd12021-07-02 14:19:53 -0700498 compile_expression(env, local, expanded_to, Dst);
499 }
swissChili53472e82021-05-08 16:06:32 -0700500 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700501 }
swissChili923b5362021-05-09 20:31:43 -0700502 else if (symbolp(val))
503 {
swissChili923b5362021-05-09 20:31:43 -0700504 struct variable *v = find_variable(local, (char *)(val ^ SYMBOL_TAG));
505
506 if (!v)
swissChilie9fec8b2021-06-22 13:59:33 -0700507 {
508 fprintf(stderr, "var: %s\n", (char *)(val ^ SYMBOL_TAG));
swissChili923b5362021-05-09 20:31:43 -0700509 err("Variable unbound");
swissChilie9fec8b2021-06-22 13:59:33 -0700510 }
swissChili923b5362021-05-09 20:31:43 -0700511
swissChiliddc97542021-07-04 11:47:42 -0700512 compile_variable(v, Dst);
swissChili923b5362021-05-09 20:31:43 -0700513 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700514}
swissChilif3e7f182021-04-20 13:57:22 -0700515
swissChili53472e82021-05-08 16:06:32 -0700516void compile_expr_to_func(struct environment *env, char *name, value_t val,
517 dasm_State **Dst)
swissChilif3e7f182021-04-20 13:57:22 -0700518{
519 | setup 0;
520
521 struct local local;
swissChili53472e82021-05-08 16:06:32 -0700522 compile_expression(env, &local, val, Dst);
523
swissChilif3e7f182021-04-20 13:57:22 -0700524 | cleanup;
525
swissChili2999dd12021-07-02 14:19:53 -0700526 add_function(env, name, link(Dst), 0, NS_FUNCTION);
swissChilif3e7f182021-04-20 13:57:22 -0700527}
swissChili923b5362021-05-09 20:31:43 -0700528
529struct variable *add_variable(struct local *local, enum var_type type,
530 char *name, int number)
531{
532 struct variable *var = malloc(sizeof(struct variable));
533 var->prev = local->first;
534 var->type = type;
535 var->name = name;
536 var->number = number;
537
538 local->first = var;
539
540 return var;
541}
542
543void destroy_local(struct local *local)
544{
545 for (struct variable *v = local->first; v;)
546 {
547 struct variable *t = v;
548 v = v->prev;
549 free(t);
550 }
551}
552
553struct variable *find_variable(struct local *local, char *name)
554{
555 struct variable *v = local->first;
556
557 for (; v && strcmp(v->name, name) != 0; v = v->prev)
558 {}
559
swissChiliddc97542021-07-04 11:47:42 -0700560 if (!v)
561 {
562 if (local->parent)
563 {
564 v = find_variable(local->parent, name);
565
566 if (v)
567 {
568 // We found this in a parent scope, add it as a V_FREE variable to skip the search.
569 v = add_variable(local, V_FREE, name, local->num_closure_slots++);
570 }
571 }
572 }
swissChili923b5362021-05-09 20:31:43 -0700573 return v;
574}
swissChili2999dd12021-07-02 14:19:53 -0700575
swissChiliddc97542021-07-04 11:47:42 -0700576extern value_t _call_list(void *addr, value_t list, value_t *edi);
swissChili2999dd12021-07-02 14:19:53 -0700577
578value_t call_list(struct function *func, value_t list)
579{
swissChiliddc97542021-07-04 11:47:42 -0700580 return _call_list(func->code_ptr, list, NULL);
581}
582
583value_t call_list_closure(struct closure *c, value_t list)
584{
585 return _call_list(c->function, list, c->data);
swissChili2999dd12021-07-02 14:19:53 -0700586}