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