blob: 8c876d3a75b49224dc10526c731174f6c0e3940a [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"
swissChilica107a02021-04-14 12:07:30 -07006
7#include <dasm_proto.h>
8#include <dasm_x86.h>
9
swissChili923b5362021-05-09 20:31:43 -070010#include <stdlib.h>
11#include <string.h>
12
swissChili53472e82021-05-08 16:06:32 -070013#define value_size sizeof(value_t)
swissChilica107a02021-04-14 12:07:30 -070014
15|.arch x86;
16
17|.macro setup, nvars;
18| push ebp;
19| mov ebp, esp;
swissChili8cfb7c42021-04-18 21:17:58 -070020| sub esp, (value_size * nvars);
swissChilica107a02021-04-14 12:07:30 -070021|.endmacro;
22
23|.macro cleanup;
24| mov esp, ebp;
25| pop ebp;
26| ret;
27|.endmacro;
28
swissChili67bdf282021-06-06 18:46:08 -070029|.macro local_var, index;
30|.endmacro;
31
swissChilica107a02021-04-14 12:07:30 -070032dasm_State *d;
33unsigned int npc = 8;
34
swissChili6d6525e2021-06-15 21:20:53 -070035extern void _do_gc(unsigned int ebp, unsigned int esp);
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
swissChilif1ba8c12021-07-02 18:45:38 -070090struct dasm_State *compile_function(value_t args, enum namespace namespace,
91 struct environment *env,
92 struct local *local_out, int *nargs)
93{
94 dasm_State *d;
95 dasm_State **Dst = &d;
96
97 |.section code;
98 dasm_init(&d, DASM_MAXSECTION);
99
100 |.globals lbl_;
101 void *labels[lbl__MAX];
102 dasm_setupglobal(&d, labels, lbl__MAX);
103
104 |.actionlist lisp_actions;
105 dasm_setup(&d, lisp_actions);
106
107 struct local local;
108 local.parent = NULL;
109 local.first = NULL;
110 local.num_vars = 0;
111 local.npc = 8;
112 local.nextpc = 0;
113 local.stack_slots = malloc(sizeof(bool) * 4);
114 memset(local.stack_slots, 0, sizeof(bool) * 4);
115 local.num_stack_slots = 4;
116 local.num_stack_entries = 0;
117
118 dasm_growpc(&d, local.npc);
119
120 // Generate code
121 // TODO: first pass, extract bound and free variables
122
123 value_t name = car(args);
124 args = cdr(args);
125 value_t arglist = car(args);
126 value_t body = cdr(args);
127
128 if ((name & HEAP_MASK) != SYMBOL_TAG)
129 err("function name must be a symbol");
130
131 value_t a = arglist;
132 for (int i = 0; !nilp(a); a = cdr(a), i++)
133 {
134 if (!symbolp(car(a)))
135 {
136 err("defun argument must be a symbol");
137 }
138
139 add_variable(&local, V_ARGUMENT, (char *)(car(a) ^ SYMBOL_TAG), i);
140 }
141
142 for (value_t body_ = body; !nilp(body_); body_ = cdr(body_))
143 {
144 walk_and_alloc(&local, car(body_));
145 }
146
147 | setup (local.num_stack_entries);
148
149 memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool));
150 local.num_stack_entries = 0;
151
152 for (; !nilp(body); body = cdr(body))
153 {
154 compile_expression(env, &local, car(body), Dst);
155 }
156
157 | cleanup;
158
159 if (local_out)
160 *local_out = local;
161
162 if (nargs)
163 *nargs = length(arglist);
164
165 return d;
166}
167
swissChili53472e82021-05-08 16:06:32 -0700168void compile_tl(value_t val, struct environment *env)
swissChilica107a02021-04-14 12:07:30 -0700169{
swissChili53472e82021-05-08 16:06:32 -0700170 if (!listp(val))
171 err("Top level must be a list");
swissChilica107a02021-04-14 12:07:30 -0700172
swissChili53472e82021-05-08 16:06:32 -0700173 value_t form = car(val);
174 value_t args = cdr(val);
175
swissChilif1ba8c12021-07-02 18:45:38 -0700176 printf("Compiling function %s in %s\n", (char *)(car(args) ^ SYMBOL_TAG), (char *)(form ^ SYMBOL_TAG));
177
swissChili2999dd12021-07-02 14:19:53 -0700178 if (symstreq(form, "defun") || symstreq(form, "defmacro"))
swissChili8fc5e2f2021-04-22 13:45:10 -0700179 {
swissChili2999dd12021-07-02 14:19:53 -0700180 enum namespace namespace = NS_FUNCTION;
181
182 if (symstreq(form, "defmacro"))
183 namespace = NS_MACRO;
184
swissChili53472e82021-05-08 16:06:32 -0700185
swissChili8fc5e2f2021-04-22 13:45:10 -0700186 struct local local;
swissChilif1ba8c12021-07-02 18:45:38 -0700187 int nargs;
188 dasm_State *d = compile_function(args, namespace, env, &local, &nargs);
swissChilia820dea2021-05-09 16:46:55 -0700189
swissChilif1ba8c12021-07-02 18:45:38 -0700190 add_function(env, (char *)(car(args) ^ SYMBOL_TAG), link(&d),
191 nargs, namespace);
swissChili8fc5e2f2021-04-22 13:45:10 -0700192
swissChili53472e82021-05-08 16:06:32 -0700193 dasm_free(&d);
swissChili67bdf282021-06-06 18:46:08 -0700194 free(local.stack_slots);
195 }
196}
197
198void walk_and_alloc(struct local *local, value_t body)
199{
200 if (!listp(body))
201 return;
202
203 value_t args = cdr(body);
204
205 if (symstreq(car(body), "let1"))
206 {
207 int slot = local_alloc(local);
208
209 value_t expr = cdr(args);
swissChilif1ba8c12021-07-02 18:45:38 -0700210 for (; !nilp(expr); expr = cdr(expr))
211 {
212 walk_and_alloc(local, expr);
213 }
swissChili67bdf282021-06-06 18:46:08 -0700214
215 local_free(local, slot);
216 }
swissChilif1ba8c12021-07-02 18:45:38 -0700217 else if (symstreq(car(body), "lambda"))
218 {
219 // We don't want to walk the lambda because it's another function. When
220 // the lambda is compiled it will be walked.
221 return;
222 }
swissChili67bdf282021-06-06 18:46:08 -0700223 else
224 {
225 for (; !nilp(args); args = cdr(args))
226 {
227 walk_and_alloc(local, car(args));
228 }
swissChili8fc5e2f2021-04-22 13:45:10 -0700229 }
230}
231
swissChili53472e82021-05-08 16:06:32 -0700232struct environment compile_all(struct istream *is)
swissChili8fc5e2f2021-04-22 13:45:10 -0700233{
swissChilib8fd4712021-06-23 15:32:04 -0700234 unsigned char pool = make_pool();
235 unsigned char pop = push_pool(pool);
236
swissChili8fc5e2f2021-04-22 13:45:10 -0700237 value_t val;
swissChilif3e7f182021-04-20 13:57:22 -0700238 struct environment env;
239 env.first = NULL;
swissChili53472e82021-05-08 16:06:32 -0700240 load_std(&env);
241
242 while (read1(is, &val))
swissChili8fc5e2f2021-04-22 13:45:10 -0700243 {
swissChilif1ba8c12021-07-02 18:45:38 -0700244 printval(val, 0);
swissChili53472e82021-05-08 16:06:32 -0700245 compile_tl(val, &env);
swissChili8fc5e2f2021-04-22 13:45:10 -0700246 }
swissChilif3e7f182021-04-20 13:57:22 -0700247
swissChilib8fd4712021-06-23 15:32:04 -0700248 pop_pool(pop);
249
swissChili8fc5e2f2021-04-22 13:45:10 -0700250 return env;
swissChilica107a02021-04-14 12:07:30 -0700251}
swissChilib3ca4fb2021-04-20 10:33:00 -0700252
swissChili53472e82021-05-08 16:06:32 -0700253int nextpc(struct local *local, dasm_State **Dst)
swissChilib3ca4fb2021-04-20 10:33:00 -0700254{
swissChili53472e82021-05-08 16:06:32 -0700255 int n = local->nextpc++;
256 if (n > local->npc)
257 {
258 local->npc += 16;
259 dasm_growpc(Dst, local->npc);
260 }
261 return n;
262}
263
swissChili6b47b6d2021-06-30 22:08:55 -0700264void compile_backquote(struct environment *env, struct local *local,
265 value_t val, dasm_State **Dst)
266{
267 if (!listp(val))
268 {
269 | mov eax, (val);
270 }
271 else
272 {
273 value_t fsym = car(val),
274 args = cdr(val);
275 int nargs = length(args);
276
277 // TODO
278 }
279}
280
swissChili53472e82021-05-08 16:06:32 -0700281void compile_expression(struct environment *env, struct local *local,
282 value_t val, dasm_State **Dst)
283{
284 if (symstreq(val, "nil"))
285 {
286 | mov eax, (nil);
287 }
swissChili923b5362021-05-09 20:31:43 -0700288 else if (symstreq(val, "t"))
289 {
290 | mov eax, (t);
291 }
292 else if (integerp(val) || stringp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700293 {
294 | mov eax, val;
295 }
swissChili53472e82021-05-08 16:06:32 -0700296 else if (listp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700297 {
swissChili53472e82021-05-08 16:06:32 -0700298 value_t fsym = car(val);
299 value_t args = cdr(val);
300 int nargs = length(args);
swissChilib3ca4fb2021-04-20 10:33:00 -0700301
swissChili53472e82021-05-08 16:06:32 -0700302 if (!symbolp(fsym))
swissChilif3e7f182021-04-20 13:57:22 -0700303 {
swissChili53472e82021-05-08 16:06:32 -0700304 err("function name must be a symbol");
swissChilif3e7f182021-04-20 13:57:22 -0700305 }
306
swissChili53472e82021-05-08 16:06:32 -0700307 if (symstreq(fsym, "if"))
swissChilib3ca4fb2021-04-20 10:33:00 -0700308 {
swissChili53472e82021-05-08 16:06:32 -0700309 if (nargs < 2 || nargs > 3)
310 err("Must give at least 2 arguments to if");
swissChilib3ca4fb2021-04-20 10:33:00 -0700311
swissChili53472e82021-05-08 16:06:32 -0700312 compile_expression(env, local, car(args), Dst);
313 int false_label = nextpc(local, Dst),
314 after_label = nextpc(local, Dst);
315
316 // result is in eax
317 | cmp eax, (nil);
318 | je =>false_label;
319
320 compile_expression(env, local, elt(args, 1), Dst);
swissChilia820dea2021-05-09 16:46:55 -0700321 | jmp =>after_label;
swissChili923b5362021-05-09 20:31:43 -0700322 |=>false_label:;
swissChili53472e82021-05-08 16:06:32 -0700323 if (nargs == 3)
324 compile_expression(env, local, elt(args, 2), Dst);
325 |=>after_label:
326 }
swissChili67bdf282021-06-06 18:46:08 -0700327 else if (symstreq(fsym, "let1"))
328 {
329 if (nargs < 2)
330 {
331 err("Must give at least 2 arguments to let1");
332 }
333 value_t binding = car(args);
334 value_t rest = cdr(args);
335
336 if (length(binding) != 2)
337 {
338 err("Binding list in let1 must contain exactly two entries");
339 }
340
341 value_t name = car(binding);
342 value_t value = car(cdr(binding));
343
344 compile_expression(env, local, value, Dst);
345
346 int i = local_alloc(local);
347
348 add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i);
349
350 | mov dword [ebp - ((i + 1) * value_size)], eax;
351
352 for (; !nilp(rest); rest = cdr(rest))
353 {
354 compile_expression(env, local, car(rest), Dst);
355 }
356
357 local_free(local, i);
358 }
swissChilie9fec8b2021-06-22 13:59:33 -0700359 else if (symstreq(fsym, "gc"))
360 {
361 if (nargs)
362 {
363 err("gc takes no arguments");
364 }
365
366 | run_gc;
367 }
swissChili6b47b6d2021-06-30 22:08:55 -0700368 else if (symstreq(fsym, "quote"))
369 {
370 if (nargs != 1)
371 err("quote should take exactly 1 argument");
372
373 // Simple!
374 | mov eax, (car(args));
375 }
376 else if (symstreq(fsym, "backquote"))
377 {
378 if (nargs != 1)
379 err("backquote should take exactly 1 argument");
380
381 compile_backquote(env, local, car(args), Dst);
382 }
383 else if (symstreq(fsym, "list"))
384 {
385 | push (nil);
386
387 for (int i = nargs - 1; i >= 0; i--)
388 {
389 compile_expression(env, local, elt(args, i), Dst);
390
391 // push the ith item
392 | push eax;
393 // cons the top two stack items
394 | mov ebx, (cons);
395 | call ebx;
396 // remove the stack items from use
397 | add esp, (2 * value_size);
398 // put the new thing on the stack
399 | push eax;
400 }
401
402 | pop eax;
403 }
swissChili53472e82021-05-08 16:06:32 -0700404 else
405 {
406 struct function *func =
407 find_function(env, (char *)(fsym ^ SYMBOL_TAG));
408
swissChili923b5362021-05-09 20:31:43 -0700409 if (func == NULL)
410 err("Function undefined");
411
swissChili53472e82021-05-08 16:06:32 -0700412 if (nargs != func->nargs)
swissChilif1ba8c12021-07-02 18:45:38 -0700413 {
414 fprintf(stderr, "Function: %s at %s:%d\n", func->name, cons_file(val), cons_line(val));
swissChili53472e82021-05-08 16:06:32 -0700415 err("wrong number of args");
swissChilif1ba8c12021-07-02 18:45:38 -0700416 }
swissChili53472e82021-05-08 16:06:32 -0700417
swissChili2999dd12021-07-02 14:19:53 -0700418 if (func->namespace == NS_FUNCTION)
swissChili53472e82021-05-08 16:06:32 -0700419 {
swissChili2999dd12021-07-02 14:19:53 -0700420 for (int i = length(args) - 1; i >= 0; i--)
421 {
422 compile_expression(env, local, elt(args, i), Dst);
423 | push eax;
424 }
swissChili53472e82021-05-08 16:06:32 -0700425
swissChili2999dd12021-07-02 14:19:53 -0700426 | mov ebx, (func->code_addr);
427 | call ebx;
428 | add esp, (nargs * value_size);
429 // result in eax
430 }
431 else if (func->namespace == NS_MACRO)
432 {
433 value_t expanded_to = call_list(func, args);
434
435 printf("Macro expanded to:\n");
436 printval(expanded_to, 2);
437
438 compile_expression(env, local, expanded_to, Dst);
439 }
swissChili53472e82021-05-08 16:06:32 -0700440 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700441 }
swissChili923b5362021-05-09 20:31:43 -0700442 else if (symbolp(val))
443 {
444 // For now ignore global variables, only search locally
445 struct variable *v = find_variable(local, (char *)(val ^ SYMBOL_TAG));
446
447 if (!v)
swissChilie9fec8b2021-06-22 13:59:33 -0700448 {
449 fprintf(stderr, "var: %s\n", (char *)(val ^ SYMBOL_TAG));
swissChili923b5362021-05-09 20:31:43 -0700450 err("Variable unbound");
swissChilie9fec8b2021-06-22 13:59:33 -0700451 }
swissChili923b5362021-05-09 20:31:43 -0700452
453 switch (v->type)
454 {
455 case V_ARGUMENT:
swissChili67bdf282021-06-06 18:46:08 -0700456 | mov eax, dword [ebp + (value_size * (v->number + 2))];
457 break;
458 case V_BOUND:
swissChilie9fec8b2021-06-22 13:59:33 -0700459 | mov eax, dword [ebp - ((v->number + 1) * value_size)];
swissChili923b5362021-05-09 20:31:43 -0700460 break;
461 default:
swissChili67bdf282021-06-06 18:46:08 -0700462 err("Sorry, can only access V_ARGUMENT and V_BOUND variables for now :(");
swissChili923b5362021-05-09 20:31:43 -0700463 }
464 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700465}
swissChilif3e7f182021-04-20 13:57:22 -0700466
swissChili53472e82021-05-08 16:06:32 -0700467void compile_expr_to_func(struct environment *env, char *name, value_t val,
468 dasm_State **Dst)
swissChilif3e7f182021-04-20 13:57:22 -0700469{
470 | setup 0;
471
472 struct local local;
swissChili53472e82021-05-08 16:06:32 -0700473 compile_expression(env, &local, val, Dst);
474
swissChilif3e7f182021-04-20 13:57:22 -0700475 | cleanup;
476
swissChili2999dd12021-07-02 14:19:53 -0700477 add_function(env, name, link(Dst), 0, NS_FUNCTION);
swissChilif3e7f182021-04-20 13:57:22 -0700478}
swissChili923b5362021-05-09 20:31:43 -0700479
480struct variable *add_variable(struct local *local, enum var_type type,
481 char *name, int number)
482{
483 struct variable *var = malloc(sizeof(struct variable));
484 var->prev = local->first;
485 var->type = type;
486 var->name = name;
487 var->number = number;
488
489 local->first = var;
490
491 return var;
492}
493
494void destroy_local(struct local *local)
495{
496 for (struct variable *v = local->first; v;)
497 {
498 struct variable *t = v;
499 v = v->prev;
500 free(t);
501 }
502}
503
504struct variable *find_variable(struct local *local, char *name)
505{
506 struct variable *v = local->first;
507
508 for (; v && strcmp(v->name, name) != 0; v = v->prev)
509 {}
510
511 return v;
512}
swissChili2999dd12021-07-02 14:19:53 -0700513
514extern value_t _call_list(void *addr, value_t list);
515
516value_t call_list(struct function *func, value_t list)
517{
518 return _call_list(func->code_ptr, list);
519}