blob: b3cdb9a0ae2dae5b0ee80c48f437409ea320ea6a [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
swissChili53472e82021-05-08 16:06:32 -070090void compile_tl(value_t val, struct environment *env)
swissChilica107a02021-04-14 12:07:30 -070091{
swissChili53472e82021-05-08 16:06:32 -070092 if (!listp(val))
93 err("Top level must be a list");
swissChilica107a02021-04-14 12:07:30 -070094
swissChili53472e82021-05-08 16:06:32 -070095 value_t form = car(val);
96 value_t args = cdr(val);
97
98 if (symstreq(form, "defun"))
swissChili8fc5e2f2021-04-22 13:45:10 -070099 {
100 dasm_State *d;
101 dasm_State **Dst = &d;
swissChilica107a02021-04-14 12:07:30 -0700102
swissChili8fc5e2f2021-04-22 13:45:10 -0700103 |.section code;
swissChili53472e82021-05-08 16:06:32 -0700104 dasm_init(&d, DASM_MAXSECTION);
105
swissChili8fc5e2f2021-04-22 13:45:10 -0700106 |.globals lbl_;
swissChili53472e82021-05-08 16:06:32 -0700107 void *labels[lbl__MAX];
108 dasm_setupglobal(&d, labels, lbl__MAX);
109
swissChili8fc5e2f2021-04-22 13:45:10 -0700110 |.actionlist lisp_actions;
swissChili53472e82021-05-08 16:06:32 -0700111 dasm_setup(&d, lisp_actions);
112
swissChili8fc5e2f2021-04-22 13:45:10 -0700113 struct local local;
114 local.first = NULL;
115 local.num_vars = 0;
swissChilia820dea2021-05-09 16:46:55 -0700116 local.npc = 8;
117 local.nextpc = 0;
swissChili67bdf282021-06-06 18:46:08 -0700118 local.stack_slots = malloc(sizeof(bool) * 4);
119 memset(local.stack_slots, 0, sizeof(bool) * 4);
120 local.num_stack_slots = 4;
121 local.num_stack_entries = 0;
swissChilia820dea2021-05-09 16:46:55 -0700122
123 dasm_growpc(&d, local.npc);
swissChili53472e82021-05-08 16:06:32 -0700124
swissChili8fc5e2f2021-04-22 13:45:10 -0700125 // Generate code
swissChili923b5362021-05-09 20:31:43 -0700126 // TODO: first pass, extract bound and free variables
swissChili53472e82021-05-08 16:06:32 -0700127
swissChili53472e82021-05-08 16:06:32 -0700128 value_t name = car(args);
129 args = cdr(args);
130 value_t arglist = car(args);
131 value_t body = cdr(args);
swissChili8fc5e2f2021-04-22 13:45:10 -0700132
swissChili53472e82021-05-08 16:06:32 -0700133 if ((name & HEAP_MASK) != SYMBOL_TAG)
134 err("function name must be a symbol");
135
swissChili923b5362021-05-09 20:31:43 -0700136 value_t a = arglist;
137 for (int i = 0; !nilp(a); a = cdr(a), i++)
138 {
139 if (!symbolp(car(a)))
140 {
141 err("defun argument must be a symbol");
142 }
143
144 add_variable(&local, V_ARGUMENT, (char *)(car(a) ^ SYMBOL_TAG), i);
145 }
146
swissChili67bdf282021-06-06 18:46:08 -0700147 for (value_t body_ = body; !nilp(body_); body_ = cdr(body_))
148 {
149 walk_and_alloc(&local, car(body_));
150 }
151
152 | setup (local.num_stack_entries);
153
154 memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool));
155 local.num_stack_entries = 0;
156
swissChili53472e82021-05-08 16:06:32 -0700157 for (; !nilp(body); body = cdr(body))
swissChili8fc5e2f2021-04-22 13:45:10 -0700158 {
swissChili53472e82021-05-08 16:06:32 -0700159 compile_expression(env, &local, car(body), Dst);
swissChili8fc5e2f2021-04-22 13:45:10 -0700160 }
161
162 | cleanup;
163
swissChili53472e82021-05-08 16:06:32 -0700164 add_function(env, (char *)(name ^ SYMBOL_TAG), link(Dst),
165 length(arglist));
swissChili8fc5e2f2021-04-22 13:45:10 -0700166
swissChili53472e82021-05-08 16:06:32 -0700167 dasm_free(&d);
swissChili67bdf282021-06-06 18:46:08 -0700168 free(local.stack_slots);
169 }
170}
171
172void walk_and_alloc(struct local *local, value_t body)
173{
174 if (!listp(body))
175 return;
176
177 value_t args = cdr(body);
178
179 if (symstreq(car(body), "let1"))
180 {
181 int slot = local_alloc(local);
182
183 value_t expr = cdr(args);
184
185 local_free(local, slot);
186 }
187 else
188 {
189 for (; !nilp(args); args = cdr(args))
190 {
191 walk_and_alloc(local, car(args));
192 }
swissChili8fc5e2f2021-04-22 13:45:10 -0700193 }
194}
195
swissChili53472e82021-05-08 16:06:32 -0700196struct environment compile_all(struct istream *is)
swissChili8fc5e2f2021-04-22 13:45:10 -0700197{
198 value_t val;
swissChilif3e7f182021-04-20 13:57:22 -0700199 struct environment env;
200 env.first = NULL;
swissChili53472e82021-05-08 16:06:32 -0700201 load_std(&env);
202
203 while (read1(is, &val))
swissChili8fc5e2f2021-04-22 13:45:10 -0700204 {
swissChili53472e82021-05-08 16:06:32 -0700205 compile_tl(val, &env);
swissChili8fc5e2f2021-04-22 13:45:10 -0700206 }
swissChilif3e7f182021-04-20 13:57:22 -0700207
swissChili8fc5e2f2021-04-22 13:45:10 -0700208 return env;
swissChilica107a02021-04-14 12:07:30 -0700209}
swissChilib3ca4fb2021-04-20 10:33:00 -0700210
swissChili53472e82021-05-08 16:06:32 -0700211int nextpc(struct local *local, dasm_State **Dst)
swissChilib3ca4fb2021-04-20 10:33:00 -0700212{
swissChili53472e82021-05-08 16:06:32 -0700213 int n = local->nextpc++;
214 if (n > local->npc)
215 {
216 local->npc += 16;
217 dasm_growpc(Dst, local->npc);
218 }
219 return n;
220}
221
222void compile_expression(struct environment *env, struct local *local,
223 value_t val, dasm_State **Dst)
224{
225 if (symstreq(val, "nil"))
226 {
227 | mov eax, (nil);
228 }
swissChili923b5362021-05-09 20:31:43 -0700229 else if (symstreq(val, "t"))
230 {
231 | mov eax, (t);
232 }
233 else if (integerp(val) || stringp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700234 {
235 | mov eax, val;
236 }
swissChili53472e82021-05-08 16:06:32 -0700237 else if (listp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700238 {
swissChili53472e82021-05-08 16:06:32 -0700239 value_t fsym = car(val);
240 value_t args = cdr(val);
241 int nargs = length(args);
swissChilib3ca4fb2021-04-20 10:33:00 -0700242
swissChili53472e82021-05-08 16:06:32 -0700243 if (!symbolp(fsym))
swissChilif3e7f182021-04-20 13:57:22 -0700244 {
swissChili53472e82021-05-08 16:06:32 -0700245 err("function name must be a symbol");
swissChilif3e7f182021-04-20 13:57:22 -0700246 }
247
swissChili53472e82021-05-08 16:06:32 -0700248 if (symstreq(fsym, "if"))
swissChilib3ca4fb2021-04-20 10:33:00 -0700249 {
swissChili53472e82021-05-08 16:06:32 -0700250 if (nargs < 2 || nargs > 3)
251 err("Must give at least 2 arguments to if");
swissChilib3ca4fb2021-04-20 10:33:00 -0700252
swissChili53472e82021-05-08 16:06:32 -0700253 compile_expression(env, local, car(args), Dst);
254 int false_label = nextpc(local, Dst),
255 after_label = nextpc(local, Dst);
256
257 // result is in eax
258 | cmp eax, (nil);
259 | je =>false_label;
260
261 compile_expression(env, local, elt(args, 1), Dst);
swissChilia820dea2021-05-09 16:46:55 -0700262 | jmp =>after_label;
swissChili923b5362021-05-09 20:31:43 -0700263 |=>false_label:;
swissChili53472e82021-05-08 16:06:32 -0700264 if (nargs == 3)
265 compile_expression(env, local, elt(args, 2), Dst);
266 |=>after_label:
267 }
swissChili67bdf282021-06-06 18:46:08 -0700268 else if (symstreq(fsym, "let1"))
269 {
270 if (nargs < 2)
271 {
272 err("Must give at least 2 arguments to let1");
273 }
274 value_t binding = car(args);
275 value_t rest = cdr(args);
276
277 if (length(binding) != 2)
278 {
279 err("Binding list in let1 must contain exactly two entries");
280 }
281
282 value_t name = car(binding);
283 value_t value = car(cdr(binding));
284
285 compile_expression(env, local, value, Dst);
286
287 int i = local_alloc(local);
288
289 add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i);
290
291 | mov dword [ebp - ((i + 1) * value_size)], eax;
292
293 for (; !nilp(rest); rest = cdr(rest))
294 {
295 compile_expression(env, local, car(rest), Dst);
296 }
297
298 local_free(local, i);
299 }
swissChilie9fec8b2021-06-22 13:59:33 -0700300 else if (symstreq(fsym, "gc"))
301 {
302 if (nargs)
303 {
304 err("gc takes no arguments");
305 }
306
307 | run_gc;
308 }
swissChili53472e82021-05-08 16:06:32 -0700309 else
310 {
311 struct function *func =
312 find_function(env, (char *)(fsym ^ SYMBOL_TAG));
313
swissChili923b5362021-05-09 20:31:43 -0700314 if (func == NULL)
315 err("Function undefined");
316
swissChili53472e82021-05-08 16:06:32 -0700317 if (nargs != func->nargs)
318 err("wrong number of args");
319
320 for (int i = length(args) - 1; i >= 0; i--)
321 {
322 compile_expression(env, local, elt(args, i), Dst);
323 | push eax;
324 }
325
326 | mov ebx, (func->code_addr);
327 | call ebx;
swissChili923b5362021-05-09 20:31:43 -0700328 | add esp, (nargs * value_size);
swissChili53472e82021-05-08 16:06:32 -0700329 // result in eax
330 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700331 }
swissChili923b5362021-05-09 20:31:43 -0700332 else if (symbolp(val))
333 {
334 // For now ignore global variables, only search locally
335 struct variable *v = find_variable(local, (char *)(val ^ SYMBOL_TAG));
336
337 if (!v)
swissChilie9fec8b2021-06-22 13:59:33 -0700338 {
339 fprintf(stderr, "var: %s\n", (char *)(val ^ SYMBOL_TAG));
swissChili923b5362021-05-09 20:31:43 -0700340 err("Variable unbound");
swissChilie9fec8b2021-06-22 13:59:33 -0700341 }
swissChili923b5362021-05-09 20:31:43 -0700342
343 switch (v->type)
344 {
345 case V_ARGUMENT:
swissChili67bdf282021-06-06 18:46:08 -0700346 | mov eax, dword [ebp + (value_size * (v->number + 2))];
347 break;
348 case V_BOUND:
swissChilie9fec8b2021-06-22 13:59:33 -0700349 | mov eax, dword [ebp - ((v->number + 1) * value_size)];
swissChili923b5362021-05-09 20:31:43 -0700350 break;
351 default:
swissChili67bdf282021-06-06 18:46:08 -0700352 err("Sorry, can only access V_ARGUMENT and V_BOUND variables for now :(");
swissChili923b5362021-05-09 20:31:43 -0700353 }
354 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700355}
swissChilif3e7f182021-04-20 13:57:22 -0700356
swissChili53472e82021-05-08 16:06:32 -0700357void compile_expr_to_func(struct environment *env, char *name, value_t val,
358 dasm_State **Dst)
swissChilif3e7f182021-04-20 13:57:22 -0700359{
360 | setup 0;
361
362 struct local local;
swissChili53472e82021-05-08 16:06:32 -0700363 compile_expression(env, &local, val, Dst);
364
swissChilif3e7f182021-04-20 13:57:22 -0700365 | cleanup;
366
swissChili53472e82021-05-08 16:06:32 -0700367 add_function(env, name, link(Dst), 0);
swissChilif3e7f182021-04-20 13:57:22 -0700368}
swissChili923b5362021-05-09 20:31:43 -0700369
370struct variable *add_variable(struct local *local, enum var_type type,
371 char *name, int number)
372{
373 struct variable *var = malloc(sizeof(struct variable));
374 var->prev = local->first;
375 var->type = type;
376 var->name = name;
377 var->number = number;
378
379 local->first = var;
380
381 return var;
382}
383
384void destroy_local(struct local *local)
385{
386 for (struct variable *v = local->first; v;)
387 {
388 struct variable *t = v;
389 v = v->prev;
390 free(t);
391 }
392}
393
394struct variable *find_variable(struct local *local, char *name)
395{
396 struct variable *v = local->first;
397
398 for (; v && strcmp(v->name, name) != 0; v = v->prev)
399 {}
400
401 return v;
402}