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 | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 35 | struct function *find_function(struct environment *env, char *name) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 36 | { |
| 37 | struct function *f = env->first; |
| 38 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 39 | while (strcmp(f->name, name) != 0) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 40 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 41 | if (f->prev) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 42 | f = f->prev; |
| 43 | else |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | return f; |
| 48 | } |
| 49 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 50 | unsigned 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 | |
| 75 | void local_free(struct local *local, unsigned int slot) |
| 76 | { |
| 77 | local->stack_slots[slot] = false; |
| 78 | } |
| 79 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 80 | void compile_tl(value_t val, struct environment *env) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 81 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 82 | if (!listp(val)) |
| 83 | err("Top level must be a list"); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 84 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 85 | value_t form = car(val); |
| 86 | value_t args = cdr(val); |
| 87 | |
| 88 | if (symstreq(form, "defun")) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 89 | { |
| 90 | dasm_State *d; |
| 91 | dasm_State **Dst = &d; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 92 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 93 | |.section code; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 94 | dasm_init(&d, DASM_MAXSECTION); |
| 95 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 96 | |.globals lbl_; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 97 | void *labels[lbl__MAX]; |
| 98 | dasm_setupglobal(&d, labels, lbl__MAX); |
| 99 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 100 | |.actionlist lisp_actions; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 101 | dasm_setup(&d, lisp_actions); |
| 102 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 103 | struct local local; |
| 104 | local.first = NULL; |
| 105 | local.num_vars = 0; |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 106 | local.npc = 8; |
| 107 | local.nextpc = 0; |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 108 | 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; |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 112 | |
| 113 | dasm_growpc(&d, local.npc); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 114 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 115 | // Generate code |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 116 | // TODO: first pass, extract bound and free variables |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 117 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 118 | value_t name = car(args); |
| 119 | args = cdr(args); |
| 120 | value_t arglist = car(args); |
| 121 | value_t body = cdr(args); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 122 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 123 | if ((name & HEAP_MASK) != SYMBOL_TAG) |
| 124 | err("function name must be a symbol"); |
| 125 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 126 | 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 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 137 | 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 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 147 | for (; !nilp(body); body = cdr(body)) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 148 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 149 | compile_expression(env, &local, car(body), Dst); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | | cleanup; |
| 153 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 154 | add_function(env, (char *)(name ^ SYMBOL_TAG), link(Dst), |
| 155 | length(arglist)); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 156 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 157 | dasm_free(&d); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 158 | free(local.stack_slots); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void 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 | } |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 186 | struct environment compile_all(struct istream *is) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 187 | { |
| 188 | value_t val; |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 189 | struct environment env; |
| 190 | env.first = NULL; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 191 | load_std(&env); |
| 192 | |
| 193 | while (read1(is, &val)) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 194 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 195 | compile_tl(val, &env); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 196 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 197 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 198 | return env; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 199 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 200 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 201 | int nextpc(struct local *local, dasm_State **Dst) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 202 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 203 | 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 | |
| 212 | void 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 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 219 | else if (symstreq(val, "t")) |
| 220 | { |
| 221 | | mov eax, (t); |
| 222 | } |
| 223 | else if (integerp(val) || stringp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 224 | { |
| 225 | | mov eax, val; |
| 226 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 227 | else if (listp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 228 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 229 | value_t fsym = car(val); |
| 230 | value_t args = cdr(val); |
| 231 | int nargs = length(args); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 232 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 233 | if (!symbolp(fsym)) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 234 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 235 | err("function name must be a symbol"); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 236 | } |
| 237 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 238 | if (symstreq(fsym, "if")) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 239 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 240 | if (nargs < 2 || nargs > 3) |
| 241 | err("Must give at least 2 arguments to if"); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 242 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 243 | 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); |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 252 | | jmp =>after_label; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 253 | |=>false_label:; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 254 | if (nargs == 3) |
| 255 | compile_expression(env, local, elt(args, 2), Dst); |
| 256 | |=>after_label: |
| 257 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 258 | 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 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 290 | else |
| 291 | { |
| 292 | struct function *func = |
| 293 | find_function(env, (char *)(fsym ^ SYMBOL_TAG)); |
| 294 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 295 | if (func == NULL) |
| 296 | err("Function undefined"); |
| 297 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 298 | 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; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 309 | | add esp, (nargs * value_size); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 310 | // result in eax |
| 311 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 312 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 313 | 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: |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 324 | | 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)] |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 328 | break; |
| 329 | default: |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 330 | err("Sorry, can only access V_ARGUMENT and V_BOUND variables for now :("); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 331 | } |
| 332 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 333 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 334 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 335 | void compile_expr_to_func(struct environment *env, char *name, value_t val, |
| 336 | dasm_State **Dst) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 337 | { |
| 338 | | setup 0; |
| 339 | |
| 340 | struct local local; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 341 | compile_expression(env, &local, val, Dst); |
| 342 | |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 343 | | cleanup; |
| 344 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 345 | add_function(env, name, link(Dst), 0); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 346 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 347 | |
| 348 | struct 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 | |
| 362 | void 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 | |
| 372 | struct 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 | } |