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 | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 4 | #include "gc.h" |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 5 | #include "lib/std.h" |
| 6 | #include "lisp.h" |
| 7 | #include "plat/plat.h" |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 8 | |
| 9 | #include <dasm_proto.h> |
| 10 | #include <dasm_x86.h> |
| 11 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 12 | #include <libgen.h> |
| 13 | #include <stdio.h> |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 17 | #define value_size sizeof(value_t) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 18 | |
| 19 | |.arch x86; |
| 20 | |
| 21 | |.macro setup, nvars; |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 22 | |1:; |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 23 | | push ebp; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 24 | | mov ebp, esp; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 25 | | sub esp, (value_size * nvars); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 26 | |.endmacro; |
| 27 | |
| 28 | |.macro cleanup; |
| 29 | | mov esp, ebp; |
| 30 | | pop ebp; |
| 31 | | ret; |
| 32 | |.endmacro; |
| 33 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 34 | |.macro call_extern, address; |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 35 | | call &address; |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 36 | |.endmacro; |
| 37 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 38 | dasm_State *d; |
| 39 | unsigned int npc = 8; |
| 40 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 41 | |.macro run_gc; |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 42 | | mov eax, esp; |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 43 | | push ebp; |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 44 | | push eax; |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 45 | | mov eax, _do_gc; |
| 46 | | call eax; |
| 47 | |.endmacro; |
swissChili | 6d6525e | 2021-06-15 21:20:53 -0700 | [diff] [blame] | 48 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 49 | struct function *find_function(struct environment *env, char *name) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 50 | { |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 51 | struct function *f; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 52 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 53 | for (f = env->first; f && strcmp(f->name, name); f = f->prev) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 54 | { |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | return f; |
| 58 | } |
| 59 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 60 | unsigned int local_alloc(struct local *local) |
| 61 | { |
| 62 | for (int i = 0; i < local->num_stack_slots; i++) |
| 63 | { |
| 64 | if (local->stack_slots[i] == false) |
| 65 | { |
| 66 | local->stack_slots[i] = true; |
| 67 | |
| 68 | if (i >= local->num_stack_entries) |
| 69 | local->num_stack_entries++; |
| 70 | |
| 71 | return i; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | int old_size = local->num_stack_slots; |
| 76 | local->num_stack_slots += 4; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 77 | local->stack_slots = |
| 78 | realloc(local->stack_slots, local->num_stack_slots * sizeof(bool)); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 79 | // unreadable: set the remaining slots to unused |
| 80 | memset(local->stack_slots + old_size, 0, local->num_stack_slots - old_size); |
| 81 | local->stack_slots[old_size] = true; |
| 82 | |
| 83 | return old_size; |
| 84 | } |
| 85 | |
| 86 | void local_free(struct local *local, unsigned int slot) |
| 87 | { |
| 88 | local->stack_slots[slot] = false; |
| 89 | } |
| 90 | |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 91 | void del_local(struct local *local) |
| 92 | { |
| 93 | free(local->stack_slots); |
| 94 | |
| 95 | for (struct variable *next, *f = local->first; f; f = next) |
| 96 | { |
| 97 | next = f->prev; |
| 98 | free(f); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void del_env(struct environment *env) |
| 103 | { |
| 104 | for (struct function *next, *f = env->first; f; f = next) |
| 105 | { |
| 106 | next = f->prev; |
| 107 | // We're not gonna bother munmap()ing the function |
| 108 | free(f); |
| 109 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 110 | |
| 111 | for (struct loaded_file *next, *l = env->first_loaded; l; l = next) |
| 112 | { |
| 113 | next = l->previous; |
| 114 | free(l->resolved_path); |
| 115 | free(l); |
| 116 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 117 | |
| 118 | free(env); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void add_load(struct environment *env, char *path) |
| 122 | { |
| 123 | static char buffer[512]; |
| 124 | long size = readlink(path, buffer, 512); |
| 125 | buffer[size] = '\0'; |
| 126 | char *resolved = strdup(buffer); |
| 127 | |
| 128 | struct loaded_file *f = malloc(sizeof(struct loaded_file)); |
| 129 | f->resolved_path = resolved; |
| 130 | f->previous = env->first_loaded; |
| 131 | env->first_loaded = f; |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 132 | } |
| 133 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 134 | struct error compile_function(value_t args, enum namespace namespace, |
| 135 | struct environment *env, |
| 136 | struct local *local_out, |
| 137 | struct local *local_parent, |
| 138 | struct args **args_out, char *name, |
| 139 | char *path, |
| 140 | dasm_State **state) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 141 | { |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 142 | UNUSED(namespace); |
| 143 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 144 | E_INIT(); |
| 145 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 146 | dasm_State *d; |
| 147 | dasm_State **Dst = &d; |
| 148 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 149 | |.section code, imports; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 150 | dasm_init(&d, DASM_MAXSECTION); |
| 151 | |
| 152 | |.globals lbl_; |
| 153 | void *labels[lbl__MAX]; |
| 154 | dasm_setupglobal(&d, labels, lbl__MAX); |
| 155 | |
| 156 | |.actionlist lisp_actions; |
| 157 | dasm_setup(&d, lisp_actions); |
| 158 | |
| 159 | struct local local; |
| 160 | local.parent = NULL; |
| 161 | local.first = NULL; |
| 162 | local.num_vars = 0; |
| 163 | local.npc = 8; |
| 164 | local.nextpc = 0; |
| 165 | local.stack_slots = malloc(sizeof(bool) * 4); |
| 166 | memset(local.stack_slots, 0, sizeof(bool) * 4); |
| 167 | local.num_stack_slots = 4; |
| 168 | local.num_stack_entries = 0; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 169 | local.num_closure_slots = 0; |
| 170 | local.parent = local_parent; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 171 | local.current_function_name = name; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 172 | local.current_file_path = path; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 173 | |
| 174 | dasm_growpc(&d, local.npc); |
| 175 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 176 | value_t arglist = car(args); |
| 177 | value_t body = cdr(args); |
| 178 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 179 | // This will add the arguments to local too. |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 180 | struct args *ar; |
| 181 | TRY(list_to_args(env, arglist, &local, &ar)); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 182 | local.args = ar; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 183 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 184 | if (!ar) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 185 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 186 | NEARVAL(arglist); |
| 187 | THROW(EMALFORMED, "Malformed argument list"); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | for (value_t body_ = body; !nilp(body_); body_ = cdr(body_)) |
| 191 | { |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 192 | TRY(walk_and_alloc(env, &local, carref(body_), false)); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 193 | } |
| 194 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 195 | | setup (local.num_stack_entries); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 196 | |
| 197 | memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool)); |
| 198 | local.num_stack_entries = 0; |
| 199 | |
| 200 | for (; !nilp(body); body = cdr(body)) |
| 201 | { |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 202 | bool tail = nilp(cdr(body)); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 203 | TRY(compile_expression(env, &local, car(body), tail, Dst)); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | | cleanup; |
| 207 | |
| 208 | if (local_out) |
| 209 | *local_out = local; |
| 210 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 211 | if (args_out) |
| 212 | *args_out = ar; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 213 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 214 | *state = d; |
| 215 | |
| 216 | OKAY(); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 217 | } |
| 218 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 219 | struct error compile_tl(value_t val, struct environment *env, char *fname) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 220 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 221 | E_INIT(); |
| 222 | |
| 223 | NEARVAL(val); |
| 224 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 225 | if (!listp(val)) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 226 | { |
| 227 | THROW(EEXPECTED, "Top level form must be a list"); |
| 228 | } |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 229 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 230 | value_t form = car(val); |
| 231 | value_t args = cdr(val); |
| 232 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 233 | if (symstreq(form, "defun") || symstreq(form, "defmacro")) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 234 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 235 | enum namespace namespace = NS_FUNCTION; |
| 236 | |
| 237 | if (symstreq(form, "defmacro")) |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 238 | namespace = NS_MACRO; |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 239 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 240 | struct local local; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 241 | struct args *a; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 242 | char *name = (char *)(car(args) ^ SYMBOL_TAG); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 243 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 244 | dasm_State *d; |
| 245 | TRY(compile_function(cdr(args), namespace, env, &local, |
| 246 | NULL, &a, name, fname, &d)); |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 247 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 248 | add_function(env, name, link_program(&d), a, namespace); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 249 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 250 | dasm_free(&d); |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 251 | del_local(&local); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 252 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 253 | else if (symstreq(form, "progn")) |
| 254 | { |
| 255 | for (value_t val = args; !nilp(val); val = cdr(val)) |
| 256 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 257 | TRY(compile_tl(car(val), env, fname)); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 260 | else if (symstreq(form, "load")) |
| 261 | { |
| 262 | if (length(args) != 1) |
| 263 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 264 | NEARVAL(args); |
| 265 | THROW(EARGS, "load expects exactly 1 argument, %d given", |
| 266 | length(args)); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 267 | } |
| 268 | load_relative(env, fname, car(args)); |
| 269 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 270 | |
| 271 | OKAY(); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 272 | } |
| 273 | |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 274 | struct error walk_and_alloc(struct environment *env, struct local *local, value_t *bp, bool quoted) |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 275 | { |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 276 | // Note: this kind of sucks. Some of the quote-handling code is |
| 277 | // duplicated here and compile_expression. TODO: refactor |
| 278 | // eventually. |
| 279 | |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 280 | E_INIT(); |
| 281 | |
| 282 | value_t body = *bp; |
| 283 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 284 | if (!listp(body)) |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 285 | OKAY(); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 286 | |
| 287 | value_t args = cdr(body); |
| 288 | |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 289 | if (!quoted && symstreq(car(body), "let1")) |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 290 | { |
| 291 | int slot = local_alloc(local); |
| 292 | |
| 293 | value_t expr = cdr(args); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 294 | for (; !nilp(expr); expr = cdr(expr)) |
| 295 | { |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 296 | walk_and_alloc(env, local, carref(expr), false); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 297 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 298 | |
| 299 | local_free(local, slot); |
| 300 | } |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 301 | else if (!quoted && symstreq(car(body), "lambda")) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 302 | { |
| 303 | // We don't want to walk the lambda because it's another function. When |
| 304 | // the lambda is compiled it will be walked. |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 305 | OKAY(); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 306 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 307 | else |
| 308 | { |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 309 | if (quoted) |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 310 | { |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 311 | if (symstreq(car(body), "unquote") || symstreq(car(body), "unquote-splice")) |
| 312 | { |
| 313 | for (value_t b = cdr(body); !nilp(b); b = cdr(b)) |
| 314 | { |
| 315 | walk_and_alloc(env, local, carref(b), false); |
| 316 | } |
| 317 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 318 | } |
| 319 | else |
| 320 | { |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 321 | // Is this a macro? |
| 322 | |
| 323 | struct function *mac = NULL; |
| 324 | |
| 325 | if (symbolp(car(body))) |
| 326 | mac = find_function(env, (char *)(car(body) ^ SYMBOL_TAG)); |
| 327 | else if (consp(car(body))) // consp, not just listp, since we don't care about nil. |
| 328 | walk_and_alloc(env, local, carref(body), false); |
| 329 | |
| 330 | if (mac && mac->namespace == NS_MACRO) |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 331 | { |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 332 | unsigned char pool = push_pool(0); |
| 333 | value_t form = call_list(mac, args); |
| 334 | pop_pool(pool); |
| 335 | |
| 336 | add_to_pool(form); |
| 337 | *bp = form; |
| 338 | |
| 339 | walk_and_alloc(env, local, bp, false); |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | bool should_quote = symstreq(car(body), "quote") || symstreq(car(body), "backquote"); |
| 344 | |
| 345 | for (; !nilp(args); args = cdr(args)) |
| 346 | { |
| 347 | walk_and_alloc(env, local, carref(args), should_quote); |
| 348 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 349 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 350 | } |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 351 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 352 | |
| 353 | OKAY(); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 354 | } |
| 355 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 356 | bool load(struct environment *env, char *path) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 357 | { |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 358 | if (!file_exists(path)) |
| 359 | return false; |
| 360 | |
| 361 | add_load(env, path); |
| 362 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 363 | unsigned char pool = make_pool(); |
| 364 | unsigned char pop = push_pool(pool); |
| 365 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 366 | struct istream *is = new_fistream(path, false); |
| 367 | if (!is) |
| 368 | return false; |
| 369 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 370 | value_t val; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 371 | |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 372 | struct error compile_error, read_error; |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 373 | |
| 374 | while (IS_OKAY((read_error = read1(is, &val)))) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 375 | { |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 376 | if (!IS_OKAY((compile_error = compile_tl(val, env, path)))) |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 377 | { |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 378 | ereport(compile_error); |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 379 | goto failure; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | if (!read_error.safe_state) |
| 384 | { |
| 385 | goto failure; |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 386 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 387 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 388 | del_fistream(is); |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 389 | pop_pool(pop); |
| 390 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 391 | return true; |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 392 | |
| 393 | failure: |
| 394 | del_fistream(is); |
| 395 | pop_pool(pool); |
| 396 | |
| 397 | return false; |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 398 | } |
| 399 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 400 | value_t load_relative(struct environment *env, char *to, value_t name) |
| 401 | { |
| 402 | if (!stringp(name)) |
| 403 | return nil; |
| 404 | |
| 405 | char *new_path = (char *)(name ^ STRING_TAG); |
| 406 | char *relative_to = strdup(to); |
| 407 | char full_path[512]; |
| 408 | |
| 409 | snprintf(full_path, 512, "%s/%s", dirname(relative_to), new_path); |
| 410 | |
| 411 | if (load(env, full_path)) |
| 412 | return t; |
| 413 | else |
| 414 | return nil; |
| 415 | } |
| 416 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 417 | struct error compile_file(char *filename, struct environment **e) |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 418 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 419 | E_INIT(); |
| 420 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 421 | value_t val; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 422 | struct environment *env = malloc(sizeof(struct environment)); |
| 423 | env->first = NULL; |
| 424 | env->first_loaded = NULL; |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 425 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 426 | add_load(env, filename); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 427 | TRY(load_std(env)); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 428 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 429 | bool ok_ = load(env, filename); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 430 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 431 | if (!ok_) |
| 432 | { |
| 433 | free(env); |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 434 | NEARFL(filename, 1); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 435 | THROWSAFE(ENOTFOUND); |
| 436 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 437 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 438 | *e = env; |
| 439 | |
| 440 | OKAY(); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 441 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 442 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 443 | int nextpc(struct local *local, dasm_State **Dst) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 444 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 445 | int n = local->nextpc++; |
| 446 | if (n > local->npc) |
| 447 | { |
| 448 | local->npc += 16; |
| 449 | dasm_growpc(Dst, local->npc); |
| 450 | } |
| 451 | return n; |
| 452 | } |
| 453 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 454 | struct error compile_backquote(struct environment *env, struct local *local, |
| 455 | value_t val, dasm_State **Dst) |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 456 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 457 | E_INIT(); |
| 458 | |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 459 | if (!listp(val)) |
| 460 | { |
| 461 | | mov eax, (val); |
| 462 | } |
| 463 | else |
| 464 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 465 | value_t fsym = car(val), args = cdr(val); |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 466 | int nargs = length(args), |
| 467 | n = length(val); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 468 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 469 | NEARVAL(val); |
| 470 | |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 471 | if (symstreq(fsym, "unquote")) |
| 472 | { |
| 473 | if (nargs != 1) |
| 474 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 475 | THROW(EARGS, "unquote (or ,) takes exactly 1 argument"); |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 476 | } |
| 477 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 478 | TRY(compile_expression(env, local, car(args), false, Dst)); |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 479 | } |
| 480 | else |
| 481 | { |
| 482 | | push nil; |
| 483 | |
| 484 | for (int i = n - 1; i >= 0; i--) |
| 485 | { |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 486 | value_t v = elt(val, i); |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 487 | |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 488 | if (listp(v) && symstreq(car(v), "unquote-splice")) |
| 489 | { |
| 490 | NEARVAL(v); |
| 491 | |
| 492 | if (length(v) != 2) |
| 493 | { |
| 494 | THROW(EARGS, "unquote-splice (or ,@) takes exactly 1 argument"); |
| 495 | } |
| 496 | |
| 497 | value_t expr = car(cdr(v)); |
| 498 | |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 499 | TRY(compile_expression(env, local, expr, false, Dst)); |
| 500 | | push eax; |
| 501 | | call_extern merge2; |
| 502 | | add esp, 8; |
| 503 | | push eax; |
| 504 | } |
| 505 | else |
| 506 | { |
| 507 | TRY(compile_backquote(env, local, v, Dst)); |
| 508 | | push eax; |
| 509 | | call_extern cons; |
| 510 | | add esp, 8; |
| 511 | |
| 512 | // Remove unnecessary pop |
| 513 | | push eax; |
| 514 | } |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 515 | } |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 516 | | pop eax; |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 517 | } |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 518 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 519 | |
| 520 | OKAY(); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 521 | } |
| 522 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 523 | value_t eval(struct environment *env, value_t form) |
| 524 | { |
| 525 | // Eval! |
| 526 | value_t function = cons(nil, cons(form, nil)); |
| 527 | |
| 528 | struct local local; |
| 529 | struct args *args; |
| 530 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 531 | dasm_State *d; |
| 532 | struct error err; |
| 533 | |
| 534 | if (!IS_OKAY((err = compile_function(function, NS_ANONYMOUS, env, &local, NULL, |
| 535 | &args, NULL, "/", &d)))) |
| 536 | { |
| 537 | ereport(err); |
| 538 | return nil; |
| 539 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 540 | |
| 541 | del_local(&local); |
| 542 | |
| 543 | value_t (*f)() = link_program(&d); |
| 544 | return f(); |
| 545 | } |
| 546 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 547 | struct error compile_variable(struct variable *v, dasm_State *Dst) |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 548 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 549 | E_INIT(); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 550 | switch (v->type) |
| 551 | { |
| 552 | case V_ARGUMENT: |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 553 | | mov eax, dword[ebp + (value_size * (v->number + 2))]; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 554 | break; |
| 555 | case V_BOUND: |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 556 | | mov eax, dword[ebp - ((v->number + 1) * value_size)]; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 557 | break; |
| 558 | case V_FREE: |
| 559 | // edi is the closure context pointer |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 560 | | mov eax, dword[edi + (v->number * value_size)]; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 561 | break; |
| 562 | default: |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 563 | THROW(EUNIMPL, "Sorry, can only access V_ARGUMENT, V_BOUND, and V_FREE vars"); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 564 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 565 | OKAY(); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 566 | } |
| 567 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 568 | struct error compile_expression(struct environment *env, struct local *local, |
| 569 | value_t val, bool tail, dasm_State **Dst) |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 570 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 571 | E_INIT(); |
| 572 | |
| 573 | NEARVAL(val); |
| 574 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 575 | if (symstreq(val, "nil") || nilp(val)) |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 576 | { |
| 577 | | mov eax, (nil); |
| 578 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 579 | else if (symstreq(val, "t")) |
| 580 | { |
| 581 | | mov eax, (t); |
| 582 | } |
| 583 | else if (integerp(val) || stringp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 584 | { |
| 585 | | mov eax, val; |
| 586 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 587 | else if (listp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 588 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 589 | value_t fsym = car(val); |
| 590 | value_t args = cdr(val); |
| 591 | int nargs = length(args); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 592 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 593 | if (!symbolp(fsym)) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 594 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 595 | THROW(EEXPECTED, "Function name must be a symbol"); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 596 | } |
| 597 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 598 | if (symstreq(fsym, "if")) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 599 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 600 | if (nargs < 2 || nargs > 3) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 601 | { |
| 602 | THROW(EARGS, "Must give at least 2 arguments to if"); |
| 603 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 604 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 605 | TRY(compile_expression(env, local, car(args), false, Dst)); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 606 | int false_label = nextpc(local, Dst), |
| 607 | after_label = nextpc(local, Dst); |
| 608 | |
| 609 | // result is in eax |
| 610 | | cmp eax, (nil); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 611 | | je =>false_label; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 612 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 613 | TRY(compile_expression(env, local, elt(args, 1), tail, Dst)); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 614 | | jmp =>after_label; |
| 615 | |=>false_label:; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 616 | if (nargs == 3) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 617 | TRY(compile_expression(env, local, elt(args, 2), tail, Dst)); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 618 | |=>after_label:; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 619 | } |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 620 | else if (symstreq(fsym, "and") || symstreq(fsym, "or")) |
| 621 | { |
| 622 | bool or = symstreq(fsym, "or"); // false == and |
| 623 | |
| 624 | // Boolean and and or, short circuit like &&/|| |
| 625 | if (nargs < 1) |
| 626 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 627 | THROW(EARGS, "and & or require at least 1 argument."); |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | int after = nextpc(local, Dst); |
| 631 | |
| 632 | for (; !nilp(args); args = cdr(args)) |
| 633 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 634 | NEARVAL(args); |
| 635 | |
| 636 | TRY(compile_expression(env, local, car(args), false, Dst)); |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 637 | if (!nilp(cdr(args))) |
| 638 | { |
| 639 | | cmp eax, nil; |
| 640 | if (or) |
| 641 | { |
swissChili | fbf525f | 2021-08-04 21:28:07 -0700 | [diff] [blame] | 642 | | jne =>after; |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 643 | } |
| 644 | else |
| 645 | { |
swissChili | fbf525f | 2021-08-04 21:28:07 -0700 | [diff] [blame] | 646 | | je =>after; |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | |=>after:; |
| 652 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 653 | else if (symstreq(fsym, "progn")) |
| 654 | { |
| 655 | for (value_t val = args; !nilp(val); val = cdr(val)) |
| 656 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 657 | NEARVAL(args); |
| 658 | |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 659 | bool t = tail && nilp(cdr(val)); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 660 | TRY(compile_expression(env, local, car(val), t, Dst)); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 661 | } |
| 662 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 663 | else if (symstreq(fsym, "let1")) |
| 664 | { |
| 665 | if (nargs < 2) |
| 666 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 667 | THROW(EARGS, "Must give at least 2 arguments to let1"); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 668 | } |
| 669 | value_t binding = car(args); |
| 670 | value_t rest = cdr(args); |
| 671 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 672 | NEARVAL(binding); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 673 | if (length(binding) != 2) |
| 674 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 675 | THROW(EARGS, "Binding list in let1 must contain exactly two entries"); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 676 | } |
| 677 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 678 | NEARVAL(rest); |
| 679 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 680 | value_t name = car(binding); |
| 681 | value_t value = car(cdr(binding)); |
| 682 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 683 | TRY(compile_expression(env, local, value, false, Dst)); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 684 | |
| 685 | int i = local_alloc(local); |
| 686 | |
| 687 | add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i); |
| 688 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 689 | | mov dword[ebp - ((i + 1) * value_size)], eax; |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 690 | |
| 691 | for (; !nilp(rest); rest = cdr(rest)) |
| 692 | { |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 693 | bool t = tail && nilp(cdr(rest)); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 694 | NEARVAL(rest); |
| 695 | TRY(compile_expression(env, local, car(rest), t, Dst)); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | local_free(local, i); |
| 699 | } |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 700 | else if (symstreq(fsym, "gc")) |
| 701 | { |
| 702 | if (nargs) |
| 703 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 704 | THROW(EARGS, "gc takes no arguments"); |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | | run_gc; |
| 708 | } |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 709 | else if (symstreq(fsym, "quote")) |
| 710 | { |
| 711 | if (nargs != 1) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 712 | THROW(EARGS, "quote should take exactly 1 argument"); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 713 | |
| 714 | // Simple! |
| 715 | | mov eax, (car(args)); |
| 716 | } |
| 717 | else if (symstreq(fsym, "backquote")) |
| 718 | { |
| 719 | if (nargs != 1) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 720 | THROW(EARGS, "backquote should take exactly 1 argument"); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 721 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 722 | TRY(compile_backquote(env, local, car(args), Dst)); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 723 | } |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 724 | else if (symstreq(fsym, "function")) |
| 725 | { |
| 726 | if (nargs != 1) |
| 727 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 728 | THROW(EARGS, "function should take exactly 1 argument"); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 729 | } |
| 730 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 731 | NEARVAL(args); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 732 | if (!symbolp(car(args))) |
| 733 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 734 | THROW(EINVALID, "argument to function should be a symbol resolvable at " |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 735 | "compile time"); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 736 | } |
| 737 | |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 738 | char *name = (char *)(car(args) ^ SYMBOL_TAG); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 739 | |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 740 | if (!strcmp(name, local->current_function_name)) |
| 741 | { |
| 742 | | push 0; |
| 743 | | push local->args; |
| 744 | | push <1; |
| 745 | | call_extern create_closure; |
| 746 | } |
| 747 | else |
| 748 | { |
| 749 | struct function *f = find_function(env, name); |
| 750 | |
| 751 | if (!f) |
| 752 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 753 | THROW(EINVALID, "Function `%s' does not exist", (char *)(car(args) ^ SYMBOL_TAG)); |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 754 | } |
| 755 | value_t closure = create_closure(f->code_ptr, f->args, 0); |
| 756 | | mov eax, (closure); |
| 757 | } |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 758 | } |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 759 | else if (symstreq(fsym, "list")) |
| 760 | { |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 761 | | push (nil); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 762 | |
| 763 | for (int i = nargs - 1; i >= 0; i--) |
| 764 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 765 | TRY(compile_expression(env, local, elt(args, i), false, Dst)); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 766 | |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 767 | | push eax; |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 768 | | call_extern cons; |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 769 | | add esp, (2 * value_size); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 770 | | push eax; |
| 771 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 772 | | pop eax; |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 773 | } |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 774 | else if (symstreq(fsym, "lambda")) |
| 775 | { |
| 776 | // Compile the function with this as the parent scope |
| 777 | struct local new_local; |
| 778 | int nargs_out; |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 779 | dasm_State *d; |
| 780 | TRY(compile_function( |
| 781 | args, NS_ANONYMOUS, env, &new_local, local, &nargs_out, |
| 782 | "recurse", local->current_file_path, &d)); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 783 | |
| 784 | // Link the function |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 785 | void *func_ptr = link_program(&d); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 786 | |
| 787 | // Create a closure object with the correct number of captures at |
| 788 | // runtime |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 789 | | push (new_local.num_closure_slots); |
| 790 | | push (nargs_out); |
| 791 | | push (func_ptr); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 792 | | call_extern create_closure; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 793 | | add esp, 12; |
| 794 | |
| 795 | // Walk the generated local scope for V_FREE variables, since each |
| 796 | // of these exists in our scope (or higher), evaluate it and set it |
| 797 | // as a member of the lambda capture. |
| 798 | |
| 799 | for (struct variable *var = new_local.first; var; var = var->prev) |
| 800 | { |
| 801 | if (var->type == V_FREE) |
| 802 | { |
| 803 | // Closure in eax |
| 804 | | push eax; |
| 805 | // Variable now in eax |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 806 | TRY(compile_variable(find_variable(local, var->name), Dst)); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 807 | | push eax; |
| 808 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 809 | // The capture offset |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 810 | | push (var->number); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 811 | | call_extern set_closure_capture_variable; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 812 | // Skip the value and index |
| 813 | | add esp, 8; |
| 814 | // Pop the closure back in to eax |
| 815 | | pop eax; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | // Closure is still in eax |
| 820 | |
| 821 | dasm_free(&d); |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 822 | del_local(&new_local); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 823 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 824 | else if (symstreq(fsym, "eval")) |
| 825 | { |
| 826 | if (nargs != 1) |
| 827 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 828 | THROW(EARGS, "eval takes exactly 1 argument"); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 829 | } |
| 830 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 831 | TRY(compile_expression(env, local, car(args), false, Dst)); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 832 | | push eax; |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 833 | | push (env); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 834 | | call_extern eval; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 835 | } |
| 836 | else if (symstreq(fsym, "load")) |
| 837 | { |
| 838 | if (nargs != 1) |
| 839 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 840 | THROW(EARGS, "load takes exactly 1 argument, %d given", nargs); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 841 | } |
| 842 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 843 | TRY(compile_expression(env, local, car(args), false, Dst)); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 844 | | push eax; |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 845 | | push (local->current_file_path); |
| 846 | | push (env); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 847 | | call_extern load_relative; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 848 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 849 | else |
| 850 | { |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 851 | char *name = (char *)(fsym ^ SYMBOL_TAG); |
| 852 | struct function *func = find_function(env, name); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 853 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 854 | bool is_recursive = false; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 855 | struct args *nargs_needed = NULL; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 856 | |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 857 | // The number of arguments actually passed on the stack, |
| 858 | // i.e. all varargs are 1. |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 859 | int real_nargs; |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 860 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 861 | if (local->current_function_name && |
| 862 | symstreq(fsym, local->current_function_name)) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 863 | { |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 864 | is_recursive = true; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 865 | nargs_needed = local->args; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 866 | } |
| 867 | else |
| 868 | { |
| 869 | if (func == NULL) |
| 870 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 871 | THROW(EINVALID, "Function %s undefined", name); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 872 | } |
| 873 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 874 | nargs_needed = func->args; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 875 | } |
| 876 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 877 | if (!are_args_acceptable(nargs_needed, nargs)) |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 878 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 879 | THROW(EARGS, |
| 880 | "wrong number of args in function call: %s, " |
| 881 | "want %d args but given %d\n", |
| 882 | name, nargs_needed->num_required, nargs); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 883 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 884 | |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 885 | int total_taken = nargs_needed->num_optional + |
| 886 | nargs_needed->num_required; |
| 887 | |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 888 | real_nargs = total_taken + (nargs_needed->variadic ? 1 : 0); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 889 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 890 | if (is_recursive || func->namespace == NS_FUNCTION) |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 891 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 892 | int nargs = length(args); |
| 893 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 894 | int line = cons_line(val); |
| 895 | char *file = cons_file(val); |
| 896 | |
| 897 | if (nargs_needed->variadic) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 898 | { |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 899 | | push (nil); |
| 900 | } |
| 901 | |
| 902 | if (nargs > total_taken && nargs_needed->variadic) |
| 903 | { |
| 904 | // We are passing varargs, which means we need to make a list |
| 905 | |
| 906 | for (int i = nargs - 1; i >= total_taken; i--) |
| 907 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 908 | TRY(compile_expression(env, local, elt(args, i), false, Dst)); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 909 | | push eax; |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 910 | | call_extern cons; |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 911 | | add esp, 8; |
| 912 | | push eax; |
| 913 | } |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 914 | } |
| 915 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 916 | for (int i = nargs_needed->num_optional - 1; |
| 917 | i >= nargs - nargs_needed->num_required; i--) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 918 | { |
| 919 | // Push the default optional values |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 920 | | push (nargs_needed->optional_arguments[i].value); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 921 | } |
| 922 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 923 | int min = MIN(nargs, total_taken); |
| 924 | |
| 925 | for (int i = min - 1; i >= 0; i--) |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 926 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 927 | TRY(compile_expression(env, local, elt(args, i), false, Dst)); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 928 | | push eax; |
| 929 | } |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 930 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 931 | if (is_recursive) |
| 932 | { |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 933 | if (tail) |
| 934 | { |
| 935 | // Move all the arguments pushed to the stack |
| 936 | // back up to the argument bit of the stack. |
| 937 | |
| 938 | for (int i = 0; i < real_nargs; i++) |
| 939 | { |
| 940 | | pop eax; |
| 941 | | mov dword[ebp + (value_size * (i + 2))], eax; |
| 942 | } |
| 943 | |
| 944 | // Jmp back to start |
| 945 | | mov esp, ebp; |
| 946 | | pop ebp; |
| 947 | | jmp <1; |
| 948 | } |
| 949 | else |
| 950 | { |
| 951 | | call <1; |
| 952 | } |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 953 | } |
| 954 | else |
| 955 | { |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 956 | // | mov ebx, (func->code_addr); |
| 957 | | call_extern func->code_addr; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 958 | } |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 959 | | add esp, (real_nargs * value_size); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 960 | // result in eax |
| 961 | } |
| 962 | else if (func->namespace == NS_MACRO) |
| 963 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 964 | // Make sure that the stuff allocated by the macro isn't in a |
| 965 | // pool |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 966 | unsigned char pool = push_pool(0); |
| 967 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 968 | value_t expanded_to = call_list(func, args); |
| 969 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 970 | pop_pool(pool); |
| 971 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 972 | TRY(compile_expression(env, local, expanded_to, false, Dst)); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 973 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 974 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 975 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 976 | else if (symbolp(val)) |
| 977 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 978 | if (symstreq(val, "+current-file+")) |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 979 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 980 | value_t file_name_val = strval(local->current_file_path); |
| 981 | |
| 982 | | mov eax, (file_name_val); |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 983 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 984 | else |
| 985 | { |
| 986 | struct variable *v = |
| 987 | find_variable(local, (char *)(val ^ SYMBOL_TAG)); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 988 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 989 | if (!v) |
| 990 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 991 | THROW(EINVALID, "Variable `%s' unbound", (char *)(val ^ SYMBOL_TAG)); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 992 | } |
| 993 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 994 | TRY(compile_variable(v, Dst)); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 995 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 996 | } |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 997 | else if (closurep(val)) |
| 998 | { |
| 999 | | mov eax, val; |
| 1000 | } |
| 1001 | else |
| 1002 | { |
| 1003 | printval(val, 1); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1004 | THROW(EUNIMPL, "Don't know how to compile this, sorry."); |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 1005 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1006 | |
| 1007 | OKAY(); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 1008 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 1009 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 1010 | struct variable *add_variable(struct local *local, enum var_type type, |
| 1011 | char *name, int number) |
| 1012 | { |
| 1013 | struct variable *var = malloc(sizeof(struct variable)); |
| 1014 | var->prev = local->first; |
| 1015 | var->type = type; |
| 1016 | var->name = name; |
| 1017 | var->number = number; |
| 1018 | |
| 1019 | local->first = var; |
| 1020 | |
| 1021 | return var; |
| 1022 | } |
| 1023 | |
| 1024 | void destroy_local(struct local *local) |
| 1025 | { |
| 1026 | for (struct variable *v = local->first; v;) |
| 1027 | { |
| 1028 | struct variable *t = v; |
| 1029 | v = v->prev; |
| 1030 | free(t); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | struct variable *find_variable(struct local *local, char *name) |
| 1035 | { |
| 1036 | struct variable *v = local->first; |
| 1037 | |
| 1038 | for (; v && strcmp(v->name, name) != 0; v = v->prev) |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1039 | { |
| 1040 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 1041 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 1042 | if (!v) |
| 1043 | { |
| 1044 | if (local->parent) |
| 1045 | { |
| 1046 | v = find_variable(local->parent, name); |
| 1047 | |
| 1048 | if (v) |
| 1049 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1050 | // We found this in a parent scope, add it as a V_FREE variable |
| 1051 | // to skip the search. |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1052 | v = add_variable(local, V_FREE, name, |
| 1053 | local->num_closure_slots++); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 1057 | return v; |
| 1058 | } |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 1059 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 1060 | extern value_t _call_list(void *addr, value_t list, value_t *edi); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 1061 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1062 | value_t call_list_args(void *code_ptr, struct args *args, value_t list, |
| 1063 | void *data) |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 1064 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1065 | list = deep_copy(list); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 1066 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1067 | int nargs = length(list); |
| 1068 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 1069 | value_t *val = &list; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1070 | |
| 1071 | for (value_t i = list; !nilp(i); i = cdr(i)) |
| 1072 | { |
| 1073 | val = cdrref(i); |
| 1074 | } |
| 1075 | |
| 1076 | int total_required = args->num_required + args->num_optional; |
| 1077 | |
| 1078 | if (nargs > total_required) |
| 1079 | { |
| 1080 | // Take the remainder of the list and put it as the last item in the |
| 1081 | // list. |
| 1082 | value_t trailing = cxdr(list, total_required); |
| 1083 | value_t last_item = cons(trailing, nil); |
| 1084 | |
| 1085 | *cxdrref(&list, total_required) = last_item; |
| 1086 | } |
| 1087 | else if (nargs < total_required) |
| 1088 | { |
| 1089 | for (int i = nargs - args->num_required; i < args->num_optional; i++) |
| 1090 | { |
| 1091 | // Append the i-th defualt argument |
| 1092 | value_t appended = cons(args->optional_arguments[i].value, nil); |
| 1093 | *val = appended; |
| 1094 | val = cdrref(appended); |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | // We want to call this if we pass the correct # of arguments or less, just |
| 1099 | // not if we have already passed varargs. Appends a nil argument. |
| 1100 | if (nargs <= total_required) |
| 1101 | { |
| 1102 | // Enough real arguments but no variadic arguments. Pass a nil list. |
| 1103 | *val = cons(nil, nil); |
| 1104 | } |
| 1105 | |
| 1106 | return _call_list(code_ptr, list, data); |
| 1107 | } |
| 1108 | |
| 1109 | value_t call_list(struct function *fun, value_t list) |
| 1110 | { |
| 1111 | return call_list_args(fun->code_ptr, fun->args, list, NULL); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | value_t call_list_closure(struct closure *c, value_t list) |
| 1115 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1116 | return call_list_args(c->function, c->args, list, c->data); |
| 1117 | } |
| 1118 | |
| 1119 | struct args *new_args() |
| 1120 | { |
| 1121 | struct args *a = malloc(sizeof(struct args)); |
| 1122 | a->num_optional = 0; |
| 1123 | a->num_required = 0; |
| 1124 | a->variadic = false; |
| 1125 | |
| 1126 | return a; |
| 1127 | } |
| 1128 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1129 | struct args *add_optional_arg(struct args *args, value_t name, value_t value) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1130 | { |
| 1131 | int i = args->num_optional++; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1132 | args = |
| 1133 | realloc(args, sizeof(struct args) + sizeof(struct optional_argument) * |
| 1134 | args->num_optional); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1135 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1136 | args->optional_arguments[i] = (struct optional_argument){ |
| 1137 | .value = value, |
| 1138 | .name = name, |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1139 | }; |
| 1140 | |
| 1141 | return args; |
| 1142 | } |
| 1143 | |
| 1144 | bool are_args_acceptable(struct args *args, int number) |
| 1145 | { |
| 1146 | if (args->variadic) |
| 1147 | { |
| 1148 | return number >= args->num_required; |
| 1149 | } |
| 1150 | else |
| 1151 | { |
| 1152 | return number >= args->num_required && |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1153 | number <= args->num_required + args->num_optional; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1157 | struct error list_to_args(struct environment *env, value_t list, |
| 1158 | struct local *local, struct args **a) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1159 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1160 | E_INIT(); |
| 1161 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1162 | struct args *args = new_args(); |
| 1163 | |
| 1164 | bool in_optional = false; |
| 1165 | |
| 1166 | for (value_t i = list; !nilp(i); i = cdr(i)) |
| 1167 | { |
| 1168 | value_t val = car(i); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1169 | NEARVAL(i); |
| 1170 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1171 | if (symbolp(val)) |
| 1172 | { |
| 1173 | if (!args->variadic && symstreq(val, "&")) |
| 1174 | { |
| 1175 | i = cdr(i); |
| 1176 | value_t name = car(i); |
| 1177 | |
| 1178 | if (!symbolp(name)) |
| 1179 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1180 | THROW(EEXPECTED, "You must provide a symbol after & in an argument list " |
| 1181 | "to bind the\n" |
| 1182 | "variadic arguments to."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | args->variadic = true; |
| 1186 | |
| 1187 | add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG), |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1188 | args->num_optional + args->num_required); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1189 | |
| 1190 | continue; |
| 1191 | } |
| 1192 | |
| 1193 | if (!in_optional) |
| 1194 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1195 | add_variable(local, V_ARGUMENT, (char *)(val ^ SYMBOL_TAG), |
| 1196 | args->num_required++); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1197 | } |
| 1198 | else |
| 1199 | { |
| 1200 | char *name = (char *)(val ^ SYMBOL_TAG); |
| 1201 | if (name[0] == '&') |
| 1202 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1203 | THROW(EINVALID, "Non-optional argument following optional arguments " |
| 1204 | "starts with a &\n" |
| 1205 | "did you mean to declare a variadic argument? If so " |
| 1206 | "leave a space\n" |
| 1207 | "between the & and name."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1208 | } |
| 1209 | else |
| 1210 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1211 | THROW(EINVALID, "Cannot define a non-optional argument after an " |
| 1212 | "optional one."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | else if (listp(val)) |
| 1217 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1218 | NEARVAL(val); |
| 1219 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1220 | in_optional = true; |
| 1221 | int len = length(val); |
| 1222 | |
| 1223 | if (len != 2) |
| 1224 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1225 | THROW(EINVALID, "A list defining an optional value must be structured like " |
| 1226 | "(name expr)\n" |
| 1227 | "with exactly two arguments."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | value_t name = car(val); |
| 1231 | value_t expr = car(cdr(val)); |
| 1232 | |
| 1233 | value_t function = cons(nil, cons(expr, nil)); |
| 1234 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1235 | dasm_State *d; |
| 1236 | TRY(compile_function(function, NS_ANONYMOUS, env, NULL, NULL, NULL, |
| 1237 | NULL, local->current_file_path, &d)); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1238 | |
| 1239 | // TODO: GC stack top! |
| 1240 | value_t (*compiled)() = link_program(&d); |
| 1241 | |
| 1242 | value_t value = compiled(); |
| 1243 | args = add_optional_arg(args, name, value); |
| 1244 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1245 | add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG), |
| 1246 | args->num_required + args->num_optional - 1); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1250 | *a = args; |
| 1251 | OKAY(); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | void display_args(struct args *args) |
| 1255 | { |
| 1256 | printf("Args object taking %d require arguments and %d optionals:\n", |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1257 | args->num_required, args->num_optional); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1258 | |
| 1259 | for (int i = 0; i < args->num_optional; i++) |
| 1260 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1261 | printf(" %d\t%s\n", i, |
| 1262 | (char *)(args->optional_arguments[i].name ^ SYMBOL_TAG)); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1263 | printval(args->optional_arguments[i].value, 2); |
| 1264 | } |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 1265 | } |