blob: cd64ce7298865dd735bb6d17d4c92d78222ea28a [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
swissChili53472e82021-05-08 16:06:32 -070035struct function *find_function(struct environment *env, char *name)
swissChilica107a02021-04-14 12:07:30 -070036{
37 struct function *f = env->first;
38
swissChili53472e82021-05-08 16:06:32 -070039 while (strcmp(f->name, name) != 0)
swissChilica107a02021-04-14 12:07:30 -070040 {
swissChili53472e82021-05-08 16:06:32 -070041 if (f->prev)
swissChilica107a02021-04-14 12:07:30 -070042 f = f->prev;
43 else
44 return NULL;
45 }
46
47 return f;
48}
49
swissChili67bdf282021-06-06 18:46:08 -070050unsigned int local_alloc(struct local *local)
51{
52 for (int i = 0; i < local->num_stack_slots; i++)
53 {
54 if (local->stack_slots[i] == false)
55 {
56 local->stack_slots[i] = true;
57
58 if (i >= local->num_stack_entries)
59 local->num_stack_entries++;
60
61 return i;
62 }
63 }
64
65 int old_size = local->num_stack_slots;
66 local->num_stack_slots += 4;
67 local->stack_slots = realloc(local->stack_slots, local->num_stack_slots * sizeof(bool));
68 // unreadable: set the remaining slots to unused
69 memset(local->stack_slots + old_size, 0, local->num_stack_slots - old_size);
70 local->stack_slots[old_size] = true;
71
72 return old_size;
73}
74
75void local_free(struct local *local, unsigned int slot)
76{
77 local->stack_slots[slot] = false;
78}
79
swissChili53472e82021-05-08 16:06:32 -070080void compile_tl(value_t val, struct environment *env)
swissChilica107a02021-04-14 12:07:30 -070081{
swissChili53472e82021-05-08 16:06:32 -070082 if (!listp(val))
83 err("Top level must be a list");
swissChilica107a02021-04-14 12:07:30 -070084
swissChili53472e82021-05-08 16:06:32 -070085 value_t form = car(val);
86 value_t args = cdr(val);
87
88 if (symstreq(form, "defun"))
swissChili8fc5e2f2021-04-22 13:45:10 -070089 {
90 dasm_State *d;
91 dasm_State **Dst = &d;
swissChilica107a02021-04-14 12:07:30 -070092
swissChili8fc5e2f2021-04-22 13:45:10 -070093 |.section code;
swissChili53472e82021-05-08 16:06:32 -070094 dasm_init(&d, DASM_MAXSECTION);
95
swissChili8fc5e2f2021-04-22 13:45:10 -070096 |.globals lbl_;
swissChili53472e82021-05-08 16:06:32 -070097 void *labels[lbl__MAX];
98 dasm_setupglobal(&d, labels, lbl__MAX);
99
swissChili8fc5e2f2021-04-22 13:45:10 -0700100 |.actionlist lisp_actions;
swissChili53472e82021-05-08 16:06:32 -0700101 dasm_setup(&d, lisp_actions);
102
swissChili8fc5e2f2021-04-22 13:45:10 -0700103 struct local local;
104 local.first = NULL;
105 local.num_vars = 0;
swissChilia820dea2021-05-09 16:46:55 -0700106 local.npc = 8;
107 local.nextpc = 0;
swissChili67bdf282021-06-06 18:46:08 -0700108 local.stack_slots = malloc(sizeof(bool) * 4);
109 memset(local.stack_slots, 0, sizeof(bool) * 4);
110 local.num_stack_slots = 4;
111 local.num_stack_entries = 0;
swissChilia820dea2021-05-09 16:46:55 -0700112
113 dasm_growpc(&d, local.npc);
swissChili53472e82021-05-08 16:06:32 -0700114
swissChili8fc5e2f2021-04-22 13:45:10 -0700115 // Generate code
swissChili923b5362021-05-09 20:31:43 -0700116 // TODO: first pass, extract bound and free variables
swissChili53472e82021-05-08 16:06:32 -0700117
swissChili53472e82021-05-08 16:06:32 -0700118 value_t name = car(args);
119 args = cdr(args);
120 value_t arglist = car(args);
121 value_t body = cdr(args);
swissChili8fc5e2f2021-04-22 13:45:10 -0700122
swissChili53472e82021-05-08 16:06:32 -0700123 if ((name & HEAP_MASK) != SYMBOL_TAG)
124 err("function name must be a symbol");
125
swissChili923b5362021-05-09 20:31:43 -0700126 value_t a = arglist;
127 for (int i = 0; !nilp(a); a = cdr(a), i++)
128 {
129 if (!symbolp(car(a)))
130 {
131 err("defun argument must be a symbol");
132 }
133
134 add_variable(&local, V_ARGUMENT, (char *)(car(a) ^ SYMBOL_TAG), i);
135 }
136
swissChili67bdf282021-06-06 18:46:08 -0700137 for (value_t body_ = body; !nilp(body_); body_ = cdr(body_))
138 {
139 walk_and_alloc(&local, car(body_));
140 }
141
142 | setup (local.num_stack_entries);
143
144 memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool));
145 local.num_stack_entries = 0;
146
swissChili53472e82021-05-08 16:06:32 -0700147 for (; !nilp(body); body = cdr(body))
swissChili8fc5e2f2021-04-22 13:45:10 -0700148 {
swissChili53472e82021-05-08 16:06:32 -0700149 compile_expression(env, &local, car(body), Dst);
swissChili8fc5e2f2021-04-22 13:45:10 -0700150 }
151
152 | cleanup;
153
swissChili53472e82021-05-08 16:06:32 -0700154 add_function(env, (char *)(name ^ SYMBOL_TAG), link(Dst),
155 length(arglist));
swissChili8fc5e2f2021-04-22 13:45:10 -0700156
swissChili53472e82021-05-08 16:06:32 -0700157 dasm_free(&d);
swissChili67bdf282021-06-06 18:46:08 -0700158 free(local.stack_slots);
159 }
160}
161
162void walk_and_alloc(struct local *local, value_t body)
163{
164 if (!listp(body))
165 return;
166
167 value_t args = cdr(body);
168
169 if (symstreq(car(body), "let1"))
170 {
171 int slot = local_alloc(local);
172
173 value_t expr = cdr(args);
174
175 local_free(local, slot);
176 }
177 else
178 {
179 for (; !nilp(args); args = cdr(args))
180 {
181 walk_and_alloc(local, car(args));
182 }
swissChili8fc5e2f2021-04-22 13:45:10 -0700183 }
184}
185
swissChili53472e82021-05-08 16:06:32 -0700186struct environment compile_all(struct istream *is)
swissChili8fc5e2f2021-04-22 13:45:10 -0700187{
188 value_t val;
swissChilif3e7f182021-04-20 13:57:22 -0700189 struct environment env;
190 env.first = NULL;
swissChili53472e82021-05-08 16:06:32 -0700191 load_std(&env);
192
193 while (read1(is, &val))
swissChili8fc5e2f2021-04-22 13:45:10 -0700194 {
swissChili53472e82021-05-08 16:06:32 -0700195 compile_tl(val, &env);
swissChili8fc5e2f2021-04-22 13:45:10 -0700196 }
swissChilif3e7f182021-04-20 13:57:22 -0700197
swissChili8fc5e2f2021-04-22 13:45:10 -0700198 return env;
swissChilica107a02021-04-14 12:07:30 -0700199}
swissChilib3ca4fb2021-04-20 10:33:00 -0700200
swissChili53472e82021-05-08 16:06:32 -0700201int nextpc(struct local *local, dasm_State **Dst)
swissChilib3ca4fb2021-04-20 10:33:00 -0700202{
swissChili53472e82021-05-08 16:06:32 -0700203 int n = local->nextpc++;
204 if (n > local->npc)
205 {
206 local->npc += 16;
207 dasm_growpc(Dst, local->npc);
208 }
209 return n;
210}
211
212void compile_expression(struct environment *env, struct local *local,
213 value_t val, dasm_State **Dst)
214{
215 if (symstreq(val, "nil"))
216 {
217 | mov eax, (nil);
218 }
swissChili923b5362021-05-09 20:31:43 -0700219 else if (symstreq(val, "t"))
220 {
221 | mov eax, (t);
222 }
223 else if (integerp(val) || stringp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700224 {
225 | mov eax, val;
226 }
swissChili53472e82021-05-08 16:06:32 -0700227 else if (listp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700228 {
swissChili53472e82021-05-08 16:06:32 -0700229 value_t fsym = car(val);
230 value_t args = cdr(val);
231 int nargs = length(args);
swissChilib3ca4fb2021-04-20 10:33:00 -0700232
swissChili53472e82021-05-08 16:06:32 -0700233 if (!symbolp(fsym))
swissChilif3e7f182021-04-20 13:57:22 -0700234 {
swissChili53472e82021-05-08 16:06:32 -0700235 err("function name must be a symbol");
swissChilif3e7f182021-04-20 13:57:22 -0700236 }
237
swissChili53472e82021-05-08 16:06:32 -0700238 if (symstreq(fsym, "if"))
swissChilib3ca4fb2021-04-20 10:33:00 -0700239 {
swissChili53472e82021-05-08 16:06:32 -0700240 if (nargs < 2 || nargs > 3)
241 err("Must give at least 2 arguments to if");
swissChilib3ca4fb2021-04-20 10:33:00 -0700242
swissChili53472e82021-05-08 16:06:32 -0700243 compile_expression(env, local, car(args), Dst);
244 int false_label = nextpc(local, Dst),
245 after_label = nextpc(local, Dst);
246
247 // result is in eax
248 | cmp eax, (nil);
249 | je =>false_label;
250
251 compile_expression(env, local, elt(args, 1), Dst);
swissChilia820dea2021-05-09 16:46:55 -0700252 | jmp =>after_label;
swissChili923b5362021-05-09 20:31:43 -0700253 |=>false_label:;
swissChili53472e82021-05-08 16:06:32 -0700254 if (nargs == 3)
255 compile_expression(env, local, elt(args, 2), Dst);
256 |=>after_label:
257 }
swissChili67bdf282021-06-06 18:46:08 -0700258 else if (symstreq(fsym, "let1"))
259 {
260 if (nargs < 2)
261 {
262 err("Must give at least 2 arguments to let1");
263 }
264 value_t binding = car(args);
265 value_t rest = cdr(args);
266
267 if (length(binding) != 2)
268 {
269 err("Binding list in let1 must contain exactly two entries");
270 }
271
272 value_t name = car(binding);
273 value_t value = car(cdr(binding));
274
275 compile_expression(env, local, value, Dst);
276
277 int i = local_alloc(local);
278
279 add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i);
280
281 | mov dword [ebp - ((i + 1) * value_size)], eax;
282
283 for (; !nilp(rest); rest = cdr(rest))
284 {
285 compile_expression(env, local, car(rest), Dst);
286 }
287
288 local_free(local, i);
289 }
swissChili53472e82021-05-08 16:06:32 -0700290 else
291 {
292 struct function *func =
293 find_function(env, (char *)(fsym ^ SYMBOL_TAG));
294
swissChili923b5362021-05-09 20:31:43 -0700295 if (func == NULL)
296 err("Function undefined");
297
swissChili53472e82021-05-08 16:06:32 -0700298 if (nargs != func->nargs)
299 err("wrong number of args");
300
301 for (int i = length(args) - 1; i >= 0; i--)
302 {
303 compile_expression(env, local, elt(args, i), Dst);
304 | push eax;
305 }
306
307 | mov ebx, (func->code_addr);
308 | call ebx;
swissChili923b5362021-05-09 20:31:43 -0700309 | add esp, (nargs * value_size);
swissChili53472e82021-05-08 16:06:32 -0700310 // result in eax
311 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700312 }
swissChili923b5362021-05-09 20:31:43 -0700313 else if (symbolp(val))
314 {
315 // For now ignore global variables, only search locally
316 struct variable *v = find_variable(local, (char *)(val ^ SYMBOL_TAG));
317
318 if (!v)
319 err("Variable unbound");
320
321 switch (v->type)
322 {
323 case V_ARGUMENT:
swissChili67bdf282021-06-06 18:46:08 -0700324 | mov eax, dword [ebp + (value_size * (v->number + 2))];
325 break;
326 case V_BOUND:
327 | mov eax, dword [ebp - ((v->number + 1) * value_size)]
swissChili923b5362021-05-09 20:31:43 -0700328 break;
329 default:
swissChili67bdf282021-06-06 18:46:08 -0700330 err("Sorry, can only access V_ARGUMENT and V_BOUND variables for now :(");
swissChili923b5362021-05-09 20:31:43 -0700331 }
332 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700333}
swissChilif3e7f182021-04-20 13:57:22 -0700334
swissChili53472e82021-05-08 16:06:32 -0700335void compile_expr_to_func(struct environment *env, char *name, value_t val,
336 dasm_State **Dst)
swissChilif3e7f182021-04-20 13:57:22 -0700337{
338 | setup 0;
339
340 struct local local;
swissChili53472e82021-05-08 16:06:32 -0700341 compile_expression(env, &local, val, Dst);
342
swissChilif3e7f182021-04-20 13:57:22 -0700343 | cleanup;
344
swissChili53472e82021-05-08 16:06:32 -0700345 add_function(env, name, link(Dst), 0);
swissChilif3e7f182021-04-20 13:57:22 -0700346}
swissChili923b5362021-05-09 20:31:43 -0700347
348struct variable *add_variable(struct local *local, enum var_type type,
349 char *name, int number)
350{
351 struct variable *var = malloc(sizeof(struct variable));
352 var->prev = local->first;
353 var->type = type;
354 var->name = name;
355 var->number = number;
356
357 local->first = var;
358
359 return var;
360}
361
362void destroy_local(struct local *local)
363{
364 for (struct variable *v = local->first; v;)
365 {
366 struct variable *t = v;
367 v = v->prev;
368 free(t);
369 }
370}
371
372struct variable *find_variable(struct local *local, char *name)
373{
374 struct variable *v = local->first;
375
376 for (; v && strcmp(v->name, name) != 0; v = v->prev)
377 {}
378
379 return v;
380}