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 | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame^] | 6 | #include "gc.h" |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 7 | |
| 8 | #include <dasm_proto.h> |
| 9 | #include <dasm_x86.h> |
| 10 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 14 | #define value_size sizeof(value_t) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 15 | |
| 16 | |.arch x86; |
| 17 | |
| 18 | |.macro setup, nvars; |
| 19 | | push ebp; |
| 20 | | mov ebp, esp; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 21 | | sub esp, (value_size * nvars); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 22 | |.endmacro; |
| 23 | |
| 24 | |.macro cleanup; |
| 25 | | mov esp, ebp; |
| 26 | | pop ebp; |
| 27 | | ret; |
| 28 | |.endmacro; |
| 29 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 30 | |.macro local_var, index; |
| 31 | |.endmacro; |
| 32 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 33 | dasm_State *d; |
| 34 | unsigned int npc = 8; |
| 35 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 36 | |.macro run_gc; |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 37 | | mov eax, esp; |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 38 | | push ebp; |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 39 | | push eax; |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 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 | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 89 | struct dasm_State *compile_function(value_t args, enum namespace namespace, |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame^] | 90 | struct environment *env, struct local *local_out, |
| 91 | struct local *local_parent, int *nargs) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 92 | { |
| 93 | dasm_State *d; |
| 94 | dasm_State **Dst = &d; |
| 95 | |
| 96 | |.section code; |
| 97 | dasm_init(&d, DASM_MAXSECTION); |
| 98 | |
| 99 | |.globals lbl_; |
| 100 | void *labels[lbl__MAX]; |
| 101 | dasm_setupglobal(&d, labels, lbl__MAX); |
| 102 | |
| 103 | |.actionlist lisp_actions; |
| 104 | dasm_setup(&d, lisp_actions); |
| 105 | |
| 106 | struct local local; |
| 107 | local.parent = NULL; |
| 108 | local.first = NULL; |
| 109 | local.num_vars = 0; |
| 110 | local.npc = 8; |
| 111 | local.nextpc = 0; |
| 112 | local.stack_slots = malloc(sizeof(bool) * 4); |
| 113 | memset(local.stack_slots, 0, sizeof(bool) * 4); |
| 114 | local.num_stack_slots = 4; |
| 115 | local.num_stack_entries = 0; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame^] | 116 | local.num_closure_slots = 0; |
| 117 | local.parent = local_parent; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 118 | |
| 119 | dasm_growpc(&d, local.npc); |
| 120 | |
| 121 | // Generate code |
| 122 | // TODO: first pass, extract bound and free variables |
| 123 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 124 | value_t arglist = car(args); |
| 125 | value_t body = cdr(args); |
| 126 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 127 | value_t a = arglist; |
| 128 | for (int i = 0; !nilp(a); a = cdr(a), i++) |
| 129 | { |
| 130 | if (!symbolp(car(a))) |
| 131 | { |
| 132 | err("defun argument must be a symbol"); |
| 133 | } |
| 134 | |
| 135 | add_variable(&local, V_ARGUMENT, (char *)(car(a) ^ SYMBOL_TAG), i); |
| 136 | } |
| 137 | |
| 138 | for (value_t body_ = body; !nilp(body_); body_ = cdr(body_)) |
| 139 | { |
| 140 | walk_and_alloc(&local, car(body_)); |
| 141 | } |
| 142 | |
| 143 | | setup (local.num_stack_entries); |
| 144 | |
| 145 | memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool)); |
| 146 | local.num_stack_entries = 0; |
| 147 | |
| 148 | for (; !nilp(body); body = cdr(body)) |
| 149 | { |
| 150 | compile_expression(env, &local, car(body), Dst); |
| 151 | } |
| 152 | |
| 153 | | cleanup; |
| 154 | |
| 155 | if (local_out) |
| 156 | *local_out = local; |
| 157 | |
| 158 | if (nargs) |
| 159 | *nargs = length(arglist); |
| 160 | |
| 161 | return d; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame^] | 162 | |
| 163 | // TODO: local leaks memory! free variables too, not just stack slots (in |
| 164 | // two places). Add a free_local() function that does this. |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 165 | } |
| 166 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 167 | void compile_tl(value_t val, struct environment *env) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 168 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 169 | if (!listp(val)) |
| 170 | err("Top level must be a list"); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 171 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 172 | value_t form = car(val); |
| 173 | value_t args = cdr(val); |
| 174 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 175 | if (symstreq(form, "defun") || symstreq(form, "defmacro")) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 176 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 177 | enum namespace namespace = NS_FUNCTION; |
| 178 | |
| 179 | if (symstreq(form, "defmacro")) |
| 180 | namespace = NS_MACRO; |
| 181 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 182 | struct local local; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 183 | int nargs; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame^] | 184 | dasm_State *d = compile_function(cdr(args), namespace, env, &local, NULL, &nargs); |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 185 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 186 | add_function(env, (char *)(car(args) ^ SYMBOL_TAG), link(&d), |
| 187 | nargs, namespace); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 188 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 189 | dasm_free(&d); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 190 | free(local.stack_slots); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void walk_and_alloc(struct local *local, value_t body) |
| 195 | { |
| 196 | if (!listp(body)) |
| 197 | return; |
| 198 | |
| 199 | value_t args = cdr(body); |
| 200 | |
| 201 | if (symstreq(car(body), "let1")) |
| 202 | { |
| 203 | int slot = local_alloc(local); |
| 204 | |
| 205 | value_t expr = cdr(args); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 206 | for (; !nilp(expr); expr = cdr(expr)) |
| 207 | { |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame^] | 208 | walk_and_alloc(local, car(expr)); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 209 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 210 | |
| 211 | local_free(local, slot); |
| 212 | } |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 213 | else if (symstreq(car(body), "lambda")) |
| 214 | { |
| 215 | // We don't want to walk the lambda because it's another function. When |
| 216 | // the lambda is compiled it will be walked. |
| 217 | return; |
| 218 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 219 | else |
| 220 | { |
| 221 | for (; !nilp(args); args = cdr(args)) |
| 222 | { |
| 223 | walk_and_alloc(local, car(args)); |
| 224 | } |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 228 | struct environment compile_all(struct istream *is) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 229 | { |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 230 | unsigned char pool = make_pool(); |
| 231 | unsigned char pop = push_pool(pool); |
| 232 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 233 | value_t val; |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 234 | struct environment env; |
| 235 | env.first = NULL; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 236 | load_std(&env); |
| 237 | |
| 238 | while (read1(is, &val)) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 239 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 240 | compile_tl(val, &env); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 241 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 242 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 243 | pop_pool(pop); |
| 244 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 245 | return env; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 246 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 247 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 248 | int nextpc(struct local *local, dasm_State **Dst) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 249 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 250 | int n = local->nextpc++; |
| 251 | if (n > local->npc) |
| 252 | { |
| 253 | local->npc += 16; |
| 254 | dasm_growpc(Dst, local->npc); |
| 255 | } |
| 256 | return n; |
| 257 | } |
| 258 | |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 259 | void compile_backquote(struct environment *env, struct local *local, |
| 260 | value_t val, dasm_State **Dst) |
| 261 | { |
| 262 | if (!listp(val)) |
| 263 | { |
| 264 | | mov eax, (val); |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | value_t fsym = car(val), |
| 269 | args = cdr(val); |
| 270 | int nargs = length(args); |
| 271 | |
| 272 | // TODO |
| 273 | } |
| 274 | } |
| 275 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame^] | 276 | void compile_variable(struct variable *v, dasm_State *Dst) |
| 277 | { |
| 278 | switch (v->type) |
| 279 | { |
| 280 | case V_ARGUMENT: |
| 281 | | mov eax, dword [ebp + (value_size * (v->number + 2))]; |
| 282 | break; |
| 283 | case V_BOUND: |
| 284 | | mov eax, dword [ebp - ((v->number + 1) * value_size)]; |
| 285 | break; |
| 286 | case V_FREE: |
| 287 | // edi is the closure context pointer |
| 288 | | mov eax, dword [edi + (v->number * value_size)]; |
| 289 | break; |
| 290 | default: |
| 291 | err("Sorry, can only access V_ARGUMENT, V_FREE and V_BOUND variables for now :("); |
| 292 | } |
| 293 | } |
| 294 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 295 | void compile_expression(struct environment *env, struct local *local, |
| 296 | value_t val, dasm_State **Dst) |
| 297 | { |
| 298 | if (symstreq(val, "nil")) |
| 299 | { |
| 300 | | mov eax, (nil); |
| 301 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 302 | else if (symstreq(val, "t")) |
| 303 | { |
| 304 | | mov eax, (t); |
| 305 | } |
| 306 | else if (integerp(val) || stringp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 307 | { |
| 308 | | mov eax, val; |
| 309 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 310 | else if (listp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 311 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 312 | value_t fsym = car(val); |
| 313 | value_t args = cdr(val); |
| 314 | int nargs = length(args); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 315 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 316 | if (!symbolp(fsym)) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 317 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 318 | err("function name must be a symbol"); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 319 | } |
| 320 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 321 | if (symstreq(fsym, "if")) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 322 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 323 | if (nargs < 2 || nargs > 3) |
| 324 | err("Must give at least 2 arguments to if"); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 325 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 326 | compile_expression(env, local, car(args), Dst); |
| 327 | int false_label = nextpc(local, Dst), |
| 328 | after_label = nextpc(local, Dst); |
| 329 | |
| 330 | // result is in eax |
| 331 | | cmp eax, (nil); |
| 332 | | je =>false_label; |
| 333 | |
| 334 | compile_expression(env, local, elt(args, 1), Dst); |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 335 | | jmp =>after_label; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 336 | |=>false_label:; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 337 | if (nargs == 3) |
| 338 | compile_expression(env, local, elt(args, 2), Dst); |
| 339 | |=>after_label: |
| 340 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 341 | else if (symstreq(fsym, "let1")) |
| 342 | { |
| 343 | if (nargs < 2) |
| 344 | { |
| 345 | err("Must give at least 2 arguments to let1"); |
| 346 | } |
| 347 | value_t binding = car(args); |
| 348 | value_t rest = cdr(args); |
| 349 | |
| 350 | if (length(binding) != 2) |
| 351 | { |
| 352 | err("Binding list in let1 must contain exactly two entries"); |
| 353 | } |
| 354 | |
| 355 | value_t name = car(binding); |
| 356 | value_t value = car(cdr(binding)); |
| 357 | |
| 358 | compile_expression(env, local, value, Dst); |
| 359 | |
| 360 | int i = local_alloc(local); |
| 361 | |
| 362 | add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i); |
| 363 | |
| 364 | | mov dword [ebp - ((i + 1) * value_size)], eax; |
| 365 | |
| 366 | for (; !nilp(rest); rest = cdr(rest)) |
| 367 | { |
| 368 | compile_expression(env, local, car(rest), Dst); |
| 369 | } |
| 370 | |
| 371 | local_free(local, i); |
| 372 | } |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 373 | else if (symstreq(fsym, "gc")) |
| 374 | { |
| 375 | if (nargs) |
| 376 | { |
| 377 | err("gc takes no arguments"); |
| 378 | } |
| 379 | |
| 380 | | run_gc; |
| 381 | } |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 382 | else if (symstreq(fsym, "quote")) |
| 383 | { |
| 384 | if (nargs != 1) |
| 385 | err("quote should take exactly 1 argument"); |
| 386 | |
| 387 | // Simple! |
| 388 | | mov eax, (car(args)); |
| 389 | } |
| 390 | else if (symstreq(fsym, "backquote")) |
| 391 | { |
| 392 | if (nargs != 1) |
| 393 | err("backquote should take exactly 1 argument"); |
| 394 | |
| 395 | compile_backquote(env, local, car(args), Dst); |
| 396 | } |
| 397 | else if (symstreq(fsym, "list")) |
| 398 | { |
| 399 | | push (nil); |
| 400 | |
| 401 | for (int i = nargs - 1; i >= 0; i--) |
| 402 | { |
| 403 | compile_expression(env, local, elt(args, i), Dst); |
| 404 | |
| 405 | // push the ith item |
| 406 | | push eax; |
| 407 | // cons the top two stack items |
| 408 | | mov ebx, (cons); |
| 409 | | call ebx; |
| 410 | // remove the stack items from use |
| 411 | | add esp, (2 * value_size); |
| 412 | // put the new thing on the stack |
| 413 | | push eax; |
| 414 | } |
| 415 | |
| 416 | | pop eax; |
| 417 | } |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame^] | 418 | else if (symstreq(fsym, "lambda")) |
| 419 | { |
| 420 | // Compile the function with this as the parent scope |
| 421 | struct local new_local; |
| 422 | int nargs_out; |
| 423 | dasm_State *d = compile_function(args, NS_ANONYMOUS, env, &new_local, local, &nargs_out); |
| 424 | |
| 425 | // Link the function |
| 426 | void *func_ptr = link(&d); |
| 427 | |
| 428 | // Create a closure object with the correct number of captures at |
| 429 | // runtime |
| 430 | | mov ebx, (create_closure); |
| 431 | | push (new_local.num_closure_slots); |
| 432 | | push (nargs_out); |
| 433 | | push (func_ptr); |
| 434 | | call ebx; |
| 435 | | add esp, 12; |
| 436 | |
| 437 | // Walk the generated local scope for V_FREE variables, since each |
| 438 | // of these exists in our scope (or higher), evaluate it and set it |
| 439 | // as a member of the lambda capture. |
| 440 | |
| 441 | for (struct variable *var = new_local.first; var; var = var->prev) |
| 442 | { |
| 443 | if (var->type == V_FREE) |
| 444 | { |
| 445 | // Closure in eax |
| 446 | | push eax; |
| 447 | // Variable now in eax |
| 448 | compile_variable(find_variable(local, var->name), Dst); |
| 449 | | push eax; |
| 450 | |
| 451 | | mov ebx, (set_closure_capture_variable); |
| 452 | // The capture offset |
| 453 | | push (var->number); |
| 454 | | call ebx; |
| 455 | // Skip the value and index |
| 456 | | add esp, 8; |
| 457 | // Pop the closure back in to eax |
| 458 | | pop eax; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // Closure is still in eax |
| 463 | |
| 464 | dasm_free(&d); |
| 465 | free(new_local.stack_slots); |
| 466 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 467 | else |
| 468 | { |
| 469 | struct function *func = |
| 470 | find_function(env, (char *)(fsym ^ SYMBOL_TAG)); |
| 471 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 472 | if (func == NULL) |
| 473 | err("Function undefined"); |
| 474 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 475 | if (nargs != func->nargs) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 476 | { |
| 477 | fprintf(stderr, "Function: %s at %s:%d\n", func->name, cons_file(val), cons_line(val)); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 478 | err("wrong number of args"); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 479 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 480 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 481 | if (func->namespace == NS_FUNCTION) |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 482 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 483 | for (int i = length(args) - 1; i >= 0; i--) |
| 484 | { |
| 485 | compile_expression(env, local, elt(args, i), Dst); |
| 486 | | push eax; |
| 487 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 488 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 489 | | mov ebx, (func->code_addr); |
| 490 | | call ebx; |
| 491 | | add esp, (nargs * value_size); |
| 492 | // result in eax |
| 493 | } |
| 494 | else if (func->namespace == NS_MACRO) |
| 495 | { |
| 496 | value_t expanded_to = call_list(func, args); |
| 497 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 498 | compile_expression(env, local, expanded_to, Dst); |
| 499 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 500 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 501 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 502 | else if (symbolp(val)) |
| 503 | { |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 504 | struct variable *v = find_variable(local, (char *)(val ^ SYMBOL_TAG)); |
| 505 | |
| 506 | if (!v) |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 507 | { |
| 508 | fprintf(stderr, "var: %s\n", (char *)(val ^ SYMBOL_TAG)); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 509 | err("Variable unbound"); |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 510 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 511 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame^] | 512 | compile_variable(v, Dst); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 513 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 514 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 515 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 516 | void compile_expr_to_func(struct environment *env, char *name, value_t val, |
| 517 | dasm_State **Dst) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 518 | { |
| 519 | | setup 0; |
| 520 | |
| 521 | struct local local; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 522 | compile_expression(env, &local, val, Dst); |
| 523 | |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 524 | | cleanup; |
| 525 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 526 | add_function(env, name, link(Dst), 0, NS_FUNCTION); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 527 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 528 | |
| 529 | struct variable *add_variable(struct local *local, enum var_type type, |
| 530 | char *name, int number) |
| 531 | { |
| 532 | struct variable *var = malloc(sizeof(struct variable)); |
| 533 | var->prev = local->first; |
| 534 | var->type = type; |
| 535 | var->name = name; |
| 536 | var->number = number; |
| 537 | |
| 538 | local->first = var; |
| 539 | |
| 540 | return var; |
| 541 | } |
| 542 | |
| 543 | void destroy_local(struct local *local) |
| 544 | { |
| 545 | for (struct variable *v = local->first; v;) |
| 546 | { |
| 547 | struct variable *t = v; |
| 548 | v = v->prev; |
| 549 | free(t); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | struct variable *find_variable(struct local *local, char *name) |
| 554 | { |
| 555 | struct variable *v = local->first; |
| 556 | |
| 557 | for (; v && strcmp(v->name, name) != 0; v = v->prev) |
| 558 | {} |
| 559 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame^] | 560 | if (!v) |
| 561 | { |
| 562 | if (local->parent) |
| 563 | { |
| 564 | v = find_variable(local->parent, name); |
| 565 | |
| 566 | if (v) |
| 567 | { |
| 568 | // We found this in a parent scope, add it as a V_FREE variable to skip the search. |
| 569 | v = add_variable(local, V_FREE, name, local->num_closure_slots++); |
| 570 | } |
| 571 | } |
| 572 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 573 | return v; |
| 574 | } |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 575 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame^] | 576 | extern value_t _call_list(void *addr, value_t list, value_t *edi); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 577 | |
| 578 | value_t call_list(struct function *func, value_t list) |
| 579 | { |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame^] | 580 | return _call_list(func->code_ptr, list, NULL); |
| 581 | } |
| 582 | |
| 583 | value_t call_list_closure(struct closure *c, value_t list) |
| 584 | { |
| 585 | return _call_list(c->function, list, c->data); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 586 | } |