swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 1 | /* -*- mode:c -*- */ |
| 2 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 3 | #include "compiler.h" |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 4 | #include "lib/std.h" |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 5 | #include "plat/plat.h" |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 6 | |
| 7 | #include <dasm_proto.h> |
| 8 | #include <dasm_x86.h> |
| 9 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 13 | #define value_size sizeof(value_t) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 14 | |
| 15 | |.arch x86; |
| 16 | |
| 17 | |.macro setup, nvars; |
| 18 | | push ebp; |
| 19 | | mov ebp, esp; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 20 | | sub esp, (value_size * nvars); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 21 | |.endmacro; |
| 22 | |
| 23 | |.macro cleanup; |
| 24 | | mov esp, ebp; |
| 25 | | pop ebp; |
| 26 | | ret; |
| 27 | |.endmacro; |
| 28 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 29 | |.macro local_var, index; |
| 30 | |.endmacro; |
| 31 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 32 | dasm_State *d; |
| 33 | unsigned int npc = 8; |
| 34 | |
swissChili | 6d6525e | 2021-06-15 21:20:53 -0700 | [diff] [blame] | 35 | extern void _do_gc(unsigned int ebp, unsigned int esp); |
| 36 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 37 | |.macro run_gc; |
| 38 | | push esp; |
| 39 | | push ebp; |
| 40 | | mov eax, _do_gc; |
| 41 | | call eax; |
| 42 | |.endmacro; |
swissChili | 6d6525e | 2021-06-15 21:20:53 -0700 | [diff] [blame] | 43 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 44 | struct function *find_function(struct environment *env, char *name) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 45 | { |
| 46 | struct function *f = env->first; |
| 47 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 48 | while (strcmp(f->name, name) != 0) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 49 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 50 | if (f->prev) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 51 | f = f->prev; |
| 52 | else |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | return f; |
| 57 | } |
| 58 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 59 | unsigned 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 | |
| 84 | void local_free(struct local *local, unsigned int slot) |
| 85 | { |
| 86 | local->stack_slots[slot] = false; |
| 87 | } |
| 88 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 89 | void compile_tl(value_t val, struct environment *env) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 90 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 91 | if (!listp(val)) |
| 92 | err("Top level must be a list"); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 93 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 94 | value_t form = car(val); |
| 95 | value_t args = cdr(val); |
| 96 | |
| 97 | if (symstreq(form, "defun")) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 98 | { |
| 99 | dasm_State *d; |
| 100 | dasm_State **Dst = &d; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 101 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 102 | |.section code; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 103 | dasm_init(&d, DASM_MAXSECTION); |
| 104 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 105 | |.globals lbl_; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 106 | void *labels[lbl__MAX]; |
| 107 | dasm_setupglobal(&d, labels, lbl__MAX); |
| 108 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 109 | |.actionlist lisp_actions; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 110 | dasm_setup(&d, lisp_actions); |
| 111 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 112 | struct local local; |
| 113 | local.first = NULL; |
| 114 | local.num_vars = 0; |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 115 | local.npc = 8; |
| 116 | local.nextpc = 0; |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 117 | 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; |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 121 | |
| 122 | dasm_growpc(&d, local.npc); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 123 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 124 | // Generate code |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 125 | // TODO: first pass, extract bound and free variables |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 126 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 127 | value_t name = car(args); |
| 128 | args = cdr(args); |
| 129 | value_t arglist = car(args); |
| 130 | value_t body = cdr(args); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 131 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 132 | if ((name & HEAP_MASK) != SYMBOL_TAG) |
| 133 | err("function name must be a symbol"); |
| 134 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 135 | 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 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 146 | 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 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 156 | for (; !nilp(body); body = cdr(body)) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 157 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 158 | compile_expression(env, &local, car(body), Dst); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | | cleanup; |
| 162 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 163 | add_function(env, (char *)(name ^ SYMBOL_TAG), link(Dst), |
| 164 | length(arglist)); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 165 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 166 | dasm_free(&d); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 167 | free(local.stack_slots); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void 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 | } |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 195 | struct environment compile_all(struct istream *is) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 196 | { |
| 197 | value_t val; |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 198 | struct environment env; |
| 199 | env.first = NULL; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 200 | load_std(&env); |
| 201 | |
| 202 | while (read1(is, &val)) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 203 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 204 | compile_tl(val, &env); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 205 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 206 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 207 | return env; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 208 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 209 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 210 | int nextpc(struct local *local, dasm_State **Dst) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 211 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 212 | 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 | |
| 221 | void 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 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 228 | else if (symstreq(val, "t")) |
| 229 | { |
| 230 | | mov eax, (t); |
| 231 | } |
| 232 | else if (integerp(val) || stringp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 233 | { |
| 234 | | mov eax, val; |
| 235 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 236 | else if (listp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 237 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 238 | value_t fsym = car(val); |
| 239 | value_t args = cdr(val); |
| 240 | int nargs = length(args); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 241 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 242 | if (!symbolp(fsym)) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 243 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 244 | err("function name must be a symbol"); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 245 | } |
| 246 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 247 | if (symstreq(fsym, "if")) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 248 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 249 | if (nargs < 2 || nargs > 3) |
| 250 | err("Must give at least 2 arguments to if"); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 251 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 252 | 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); |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 261 | | jmp =>after_label; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 262 | |=>false_label:; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 263 | if (nargs == 3) |
| 264 | compile_expression(env, local, elt(args, 2), Dst); |
| 265 | |=>after_label: |
| 266 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 267 | 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 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 299 | else |
| 300 | { |
| 301 | struct function *func = |
| 302 | find_function(env, (char *)(fsym ^ SYMBOL_TAG)); |
| 303 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 304 | if (func == NULL) |
| 305 | err("Function undefined"); |
| 306 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 307 | 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; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 318 | | add esp, (nargs * value_size); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 319 | // result in eax |
| 320 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 321 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 322 | 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: |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 333 | | 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)] |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 337 | break; |
| 338 | default: |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 339 | err("Sorry, can only access V_ARGUMENT and V_BOUND variables for now :("); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 340 | } |
| 341 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 342 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 343 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 344 | void compile_expr_to_func(struct environment *env, char *name, value_t val, |
| 345 | dasm_State **Dst) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 346 | { |
| 347 | | setup 0; |
| 348 | |
| 349 | struct local local; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 350 | compile_expression(env, &local, val, Dst); |
| 351 | |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 352 | | cleanup; |
| 353 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 354 | add_function(env, name, link(Dst), 0); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 355 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 356 | |
| 357 | struct 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 | |
| 371 | void 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 | |
| 381 | struct 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 | } |