blob: b47ecf2b1456a3ae5ff86c8cce871f343adb8ccd [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;
38| push esp;
39| push ebp;
40| 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
swissChili53472e82021-05-08 16:06:32 -070089void compile_tl(value_t val, struct environment *env)
swissChilica107a02021-04-14 12:07:30 -070090{
swissChili53472e82021-05-08 16:06:32 -070091 if (!listp(val))
92 err("Top level must be a list");
swissChilica107a02021-04-14 12:07:30 -070093
swissChili53472e82021-05-08 16:06:32 -070094 value_t form = car(val);
95 value_t args = cdr(val);
96
97 if (symstreq(form, "defun"))
swissChili8fc5e2f2021-04-22 13:45:10 -070098 {
99 dasm_State *d;
100 dasm_State **Dst = &d;
swissChilica107a02021-04-14 12:07:30 -0700101
swissChili8fc5e2f2021-04-22 13:45:10 -0700102 |.section code;
swissChili53472e82021-05-08 16:06:32 -0700103 dasm_init(&d, DASM_MAXSECTION);
104
swissChili8fc5e2f2021-04-22 13:45:10 -0700105 |.globals lbl_;
swissChili53472e82021-05-08 16:06:32 -0700106 void *labels[lbl__MAX];
107 dasm_setupglobal(&d, labels, lbl__MAX);
108
swissChili8fc5e2f2021-04-22 13:45:10 -0700109 |.actionlist lisp_actions;
swissChili53472e82021-05-08 16:06:32 -0700110 dasm_setup(&d, lisp_actions);
111
swissChili8fc5e2f2021-04-22 13:45:10 -0700112 struct local local;
113 local.first = NULL;
114 local.num_vars = 0;
swissChilia820dea2021-05-09 16:46:55 -0700115 local.npc = 8;
116 local.nextpc = 0;
swissChili67bdf282021-06-06 18:46:08 -0700117 local.stack_slots = malloc(sizeof(bool) * 4);
118 memset(local.stack_slots, 0, sizeof(bool) * 4);
119 local.num_stack_slots = 4;
120 local.num_stack_entries = 0;
swissChilia820dea2021-05-09 16:46:55 -0700121
122 dasm_growpc(&d, local.npc);
swissChili53472e82021-05-08 16:06:32 -0700123
swissChili8fc5e2f2021-04-22 13:45:10 -0700124 // Generate code
swissChili923b5362021-05-09 20:31:43 -0700125 // TODO: first pass, extract bound and free variables
swissChili53472e82021-05-08 16:06:32 -0700126
swissChili53472e82021-05-08 16:06:32 -0700127 value_t name = car(args);
128 args = cdr(args);
129 value_t arglist = car(args);
130 value_t body = cdr(args);
swissChili8fc5e2f2021-04-22 13:45:10 -0700131
swissChili53472e82021-05-08 16:06:32 -0700132 if ((name & HEAP_MASK) != SYMBOL_TAG)
133 err("function name must be a symbol");
134
swissChili923b5362021-05-09 20:31:43 -0700135 value_t a = arglist;
136 for (int i = 0; !nilp(a); a = cdr(a), i++)
137 {
138 if (!symbolp(car(a)))
139 {
140 err("defun argument must be a symbol");
141 }
142
143 add_variable(&local, V_ARGUMENT, (char *)(car(a) ^ SYMBOL_TAG), i);
144 }
145
swissChili67bdf282021-06-06 18:46:08 -0700146 for (value_t body_ = body; !nilp(body_); body_ = cdr(body_))
147 {
148 walk_and_alloc(&local, car(body_));
149 }
150
151 | setup (local.num_stack_entries);
152
153 memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool));
154 local.num_stack_entries = 0;
155
swissChili53472e82021-05-08 16:06:32 -0700156 for (; !nilp(body); body = cdr(body))
swissChili8fc5e2f2021-04-22 13:45:10 -0700157 {
swissChili53472e82021-05-08 16:06:32 -0700158 compile_expression(env, &local, car(body), Dst);
swissChili8fc5e2f2021-04-22 13:45:10 -0700159 }
160
161 | cleanup;
162
swissChili53472e82021-05-08 16:06:32 -0700163 add_function(env, (char *)(name ^ SYMBOL_TAG), link(Dst),
164 length(arglist));
swissChili8fc5e2f2021-04-22 13:45:10 -0700165
swissChili53472e82021-05-08 16:06:32 -0700166 dasm_free(&d);
swissChili67bdf282021-06-06 18:46:08 -0700167 free(local.stack_slots);
168 }
169}
170
171void walk_and_alloc(struct local *local, value_t body)
172{
173 if (!listp(body))
174 return;
175
176 value_t args = cdr(body);
177
178 if (symstreq(car(body), "let1"))
179 {
180 int slot = local_alloc(local);
181
182 value_t expr = cdr(args);
183
184 local_free(local, slot);
185 }
186 else
187 {
188 for (; !nilp(args); args = cdr(args))
189 {
190 walk_and_alloc(local, car(args));
191 }
swissChili8fc5e2f2021-04-22 13:45:10 -0700192 }
193}
194
swissChili53472e82021-05-08 16:06:32 -0700195struct environment compile_all(struct istream *is)
swissChili8fc5e2f2021-04-22 13:45:10 -0700196{
197 value_t val;
swissChilif3e7f182021-04-20 13:57:22 -0700198 struct environment env;
199 env.first = NULL;
swissChili53472e82021-05-08 16:06:32 -0700200 load_std(&env);
201
202 while (read1(is, &val))
swissChili8fc5e2f2021-04-22 13:45:10 -0700203 {
swissChili53472e82021-05-08 16:06:32 -0700204 compile_tl(val, &env);
swissChili8fc5e2f2021-04-22 13:45:10 -0700205 }
swissChilif3e7f182021-04-20 13:57:22 -0700206
swissChili8fc5e2f2021-04-22 13:45:10 -0700207 return env;
swissChilica107a02021-04-14 12:07:30 -0700208}
swissChilib3ca4fb2021-04-20 10:33:00 -0700209
swissChili53472e82021-05-08 16:06:32 -0700210int nextpc(struct local *local, dasm_State **Dst)
swissChilib3ca4fb2021-04-20 10:33:00 -0700211{
swissChili53472e82021-05-08 16:06:32 -0700212 int n = local->nextpc++;
213 if (n > local->npc)
214 {
215 local->npc += 16;
216 dasm_growpc(Dst, local->npc);
217 }
218 return n;
219}
220
221void compile_expression(struct environment *env, struct local *local,
222 value_t val, dasm_State **Dst)
223{
224 if (symstreq(val, "nil"))
225 {
226 | mov eax, (nil);
227 }
swissChili923b5362021-05-09 20:31:43 -0700228 else if (symstreq(val, "t"))
229 {
230 | mov eax, (t);
231 }
232 else if (integerp(val) || stringp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700233 {
234 | mov eax, val;
235 }
swissChili53472e82021-05-08 16:06:32 -0700236 else if (listp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700237 {
swissChili53472e82021-05-08 16:06:32 -0700238 value_t fsym = car(val);
239 value_t args = cdr(val);
240 int nargs = length(args);
swissChilib3ca4fb2021-04-20 10:33:00 -0700241
swissChili53472e82021-05-08 16:06:32 -0700242 if (!symbolp(fsym))
swissChilif3e7f182021-04-20 13:57:22 -0700243 {
swissChili53472e82021-05-08 16:06:32 -0700244 err("function name must be a symbol");
swissChilif3e7f182021-04-20 13:57:22 -0700245 }
246
swissChili53472e82021-05-08 16:06:32 -0700247 if (symstreq(fsym, "if"))
swissChilib3ca4fb2021-04-20 10:33:00 -0700248 {
swissChili53472e82021-05-08 16:06:32 -0700249 if (nargs < 2 || nargs > 3)
250 err("Must give at least 2 arguments to if");
swissChilib3ca4fb2021-04-20 10:33:00 -0700251
swissChili53472e82021-05-08 16:06:32 -0700252 compile_expression(env, local, car(args), Dst);
253 int false_label = nextpc(local, Dst),
254 after_label = nextpc(local, Dst);
255
256 // result is in eax
257 | cmp eax, (nil);
258 | je =>false_label;
259
260 compile_expression(env, local, elt(args, 1), Dst);
swissChilia820dea2021-05-09 16:46:55 -0700261 | jmp =>after_label;
swissChili923b5362021-05-09 20:31:43 -0700262 |=>false_label:;
swissChili53472e82021-05-08 16:06:32 -0700263 if (nargs == 3)
264 compile_expression(env, local, elt(args, 2), Dst);
265 |=>after_label:
266 }
swissChili67bdf282021-06-06 18:46:08 -0700267 else if (symstreq(fsym, "let1"))
268 {
269 if (nargs < 2)
270 {
271 err("Must give at least 2 arguments to let1");
272 }
273 value_t binding = car(args);
274 value_t rest = cdr(args);
275
276 if (length(binding) != 2)
277 {
278 err("Binding list in let1 must contain exactly two entries");
279 }
280
281 value_t name = car(binding);
282 value_t value = car(cdr(binding));
283
284 compile_expression(env, local, value, Dst);
285
286 int i = local_alloc(local);
287
288 add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i);
289
290 | mov dword [ebp - ((i + 1) * value_size)], eax;
291
292 for (; !nilp(rest); rest = cdr(rest))
293 {
294 compile_expression(env, local, car(rest), Dst);
295 }
296
297 local_free(local, i);
298 }
swissChili53472e82021-05-08 16:06:32 -0700299 else
300 {
301 struct function *func =
302 find_function(env, (char *)(fsym ^ SYMBOL_TAG));
303
swissChili923b5362021-05-09 20:31:43 -0700304 if (func == NULL)
305 err("Function undefined");
306
swissChili53472e82021-05-08 16:06:32 -0700307 if (nargs != func->nargs)
308 err("wrong number of args");
309
310 for (int i = length(args) - 1; i >= 0; i--)
311 {
312 compile_expression(env, local, elt(args, i), Dst);
313 | push eax;
314 }
315
316 | mov ebx, (func->code_addr);
317 | call ebx;
swissChili923b5362021-05-09 20:31:43 -0700318 | add esp, (nargs * value_size);
swissChili53472e82021-05-08 16:06:32 -0700319 // result in eax
320 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700321 }
swissChili923b5362021-05-09 20:31:43 -0700322 else if (symbolp(val))
323 {
324 // For now ignore global variables, only search locally
325 struct variable *v = find_variable(local, (char *)(val ^ SYMBOL_TAG));
326
327 if (!v)
328 err("Variable unbound");
329
330 switch (v->type)
331 {
332 case V_ARGUMENT:
swissChili67bdf282021-06-06 18:46:08 -0700333 | mov eax, dword [ebp + (value_size * (v->number + 2))];
334 break;
335 case V_BOUND:
336 | mov eax, dword [ebp - ((v->number + 1) * value_size)]
swissChili923b5362021-05-09 20:31:43 -0700337 break;
338 default:
swissChili67bdf282021-06-06 18:46:08 -0700339 err("Sorry, can only access V_ARGUMENT and V_BOUND variables for now :(");
swissChili923b5362021-05-09 20:31:43 -0700340 }
341 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700342}
swissChilif3e7f182021-04-20 13:57:22 -0700343
swissChili53472e82021-05-08 16:06:32 -0700344void compile_expr_to_func(struct environment *env, char *name, value_t val,
345 dasm_State **Dst)
swissChilif3e7f182021-04-20 13:57:22 -0700346{
347 | setup 0;
348
349 struct local local;
swissChili53472e82021-05-08 16:06:32 -0700350 compile_expression(env, &local, val, Dst);
351
swissChilif3e7f182021-04-20 13:57:22 -0700352 | cleanup;
353
swissChili53472e82021-05-08 16:06:32 -0700354 add_function(env, name, link(Dst), 0);
swissChilif3e7f182021-04-20 13:57:22 -0700355}
swissChili923b5362021-05-09 20:31:43 -0700356
357struct variable *add_variable(struct local *local, enum var_type type,
358 char *name, int number)
359{
360 struct variable *var = malloc(sizeof(struct variable));
361 var->prev = local->first;
362 var->type = type;
363 var->name = name;
364 var->number = number;
365
366 local->first = var;
367
368 return var;
369}
370
371void destroy_local(struct local *local)
372{
373 for (struct variable *v = local->first; v;)
374 {
375 struct variable *t = v;
376 v = v->prev;
377 free(t);
378 }
379}
380
381struct variable *find_variable(struct local *local, char *name)
382{
383 struct variable *v = local->first;
384
385 for (; v && strcmp(v->name, name) != 0; v = v->prev)
386 {}
387
388 return v;
389}