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 | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 22 | |->function_start : | push ebp; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 23 | | mov ebp, esp; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 24 | | sub esp, (value_size * nvars); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 25 | |.endmacro; |
| 26 | |
| 27 | |.macro cleanup; |
| 28 | | mov esp, ebp; |
| 29 | | pop ebp; |
| 30 | | ret; |
| 31 | |.endmacro; |
| 32 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 33 | |.macro local_var, index; |
| 34 | |.endmacro; |
| 35 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 36 | dasm_State *d; |
| 37 | unsigned int npc = 8; |
| 38 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 39 | |.macro run_gc; |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 40 | | mov eax, esp; |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 41 | | push ebp; |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 42 | | push eax; |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 43 | | mov eax, _do_gc; |
| 44 | | call eax; |
| 45 | |.endmacro; |
swissChili | 6d6525e | 2021-06-15 21:20:53 -0700 | [diff] [blame] | 46 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 47 | struct function *find_function(struct environment *env, char *name) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 48 | { |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 49 | struct function *f; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 50 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 51 | for (f = env->first; f && strcmp(f->name, name); f = f->prev) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 52 | { |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | return f; |
| 56 | } |
| 57 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 58 | unsigned int local_alloc(struct local *local) |
| 59 | { |
| 60 | for (int i = 0; i < local->num_stack_slots; i++) |
| 61 | { |
| 62 | if (local->stack_slots[i] == false) |
| 63 | { |
| 64 | local->stack_slots[i] = true; |
| 65 | |
| 66 | if (i >= local->num_stack_entries) |
| 67 | local->num_stack_entries++; |
| 68 | |
| 69 | return i; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | int old_size = local->num_stack_slots; |
| 74 | local->num_stack_slots += 4; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 75 | local->stack_slots = |
| 76 | realloc(local->stack_slots, local->num_stack_slots * sizeof(bool)); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 77 | // unreadable: set the remaining slots to unused |
| 78 | memset(local->stack_slots + old_size, 0, local->num_stack_slots - old_size); |
| 79 | local->stack_slots[old_size] = true; |
| 80 | |
| 81 | return old_size; |
| 82 | } |
| 83 | |
| 84 | void local_free(struct local *local, unsigned int slot) |
| 85 | { |
| 86 | local->stack_slots[slot] = false; |
| 87 | } |
| 88 | |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 89 | void del_local(struct local *local) |
| 90 | { |
| 91 | free(local->stack_slots); |
| 92 | |
| 93 | for (struct variable *next, *f = local->first; f; f = next) |
| 94 | { |
| 95 | next = f->prev; |
| 96 | free(f); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void del_env(struct environment *env) |
| 101 | { |
| 102 | for (struct function *next, *f = env->first; f; f = next) |
| 103 | { |
| 104 | next = f->prev; |
| 105 | // We're not gonna bother munmap()ing the function |
| 106 | free(f); |
| 107 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 108 | |
| 109 | for (struct loaded_file *next, *l = env->first_loaded; l; l = next) |
| 110 | { |
| 111 | next = l->previous; |
| 112 | free(l->resolved_path); |
| 113 | free(l); |
| 114 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 115 | |
| 116 | free(env); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void add_load(struct environment *env, char *path) |
| 120 | { |
| 121 | static char buffer[512]; |
| 122 | long size = readlink(path, buffer, 512); |
| 123 | buffer[size] = '\0'; |
| 124 | char *resolved = strdup(buffer); |
| 125 | |
| 126 | struct loaded_file *f = malloc(sizeof(struct loaded_file)); |
| 127 | f->resolved_path = resolved; |
| 128 | f->previous = env->first_loaded; |
| 129 | env->first_loaded = f; |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 130 | } |
| 131 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 132 | struct dasm_State *compile_function(value_t args, enum namespace namespace, |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 133 | struct environment *env, |
| 134 | struct local *local_out, |
| 135 | struct local *local_parent, |
| 136 | struct args **args_out, char *name, |
| 137 | char *path) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 138 | { |
| 139 | dasm_State *d; |
| 140 | dasm_State **Dst = &d; |
| 141 | |
| 142 | |.section code; |
| 143 | dasm_init(&d, DASM_MAXSECTION); |
| 144 | |
| 145 | |.globals lbl_; |
| 146 | void *labels[lbl__MAX]; |
| 147 | dasm_setupglobal(&d, labels, lbl__MAX); |
| 148 | |
| 149 | |.actionlist lisp_actions; |
| 150 | dasm_setup(&d, lisp_actions); |
| 151 | |
| 152 | struct local local; |
| 153 | local.parent = NULL; |
| 154 | local.first = NULL; |
| 155 | local.num_vars = 0; |
| 156 | local.npc = 8; |
| 157 | local.nextpc = 0; |
| 158 | local.stack_slots = malloc(sizeof(bool) * 4); |
| 159 | memset(local.stack_slots, 0, sizeof(bool) * 4); |
| 160 | local.num_stack_slots = 4; |
| 161 | local.num_stack_entries = 0; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 162 | local.num_closure_slots = 0; |
| 163 | local.parent = local_parent; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 164 | local.current_function_name = name; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 165 | local.current_file_path = path; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 166 | |
| 167 | dasm_growpc(&d, local.npc); |
| 168 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 169 | value_t arglist = car(args); |
| 170 | value_t body = cdr(args); |
| 171 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 172 | // This will add the arguments to local too. |
| 173 | struct args *ar = list_to_args(env, arglist, &local); |
| 174 | local.args = ar; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 175 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 176 | if (!ar) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 177 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 178 | err("Malformed args list"); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | for (value_t body_ = body; !nilp(body_); body_ = cdr(body_)) |
| 182 | { |
| 183 | walk_and_alloc(&local, car(body_)); |
| 184 | } |
| 185 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 186 | | setup(local.num_stack_entries); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 187 | |
| 188 | memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool)); |
| 189 | local.num_stack_entries = 0; |
| 190 | |
| 191 | for (; !nilp(body); body = cdr(body)) |
| 192 | { |
| 193 | compile_expression(env, &local, car(body), Dst); |
| 194 | } |
| 195 | |
| 196 | | cleanup; |
| 197 | |
| 198 | if (local_out) |
| 199 | *local_out = local; |
| 200 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 201 | if (args_out) |
| 202 | *args_out = ar; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 203 | |
| 204 | return d; |
| 205 | } |
| 206 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 207 | void compile_tl(value_t val, struct environment *env, char *fname) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 208 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 209 | if (!listp(val)) |
| 210 | err("Top level must be a list"); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 211 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 212 | value_t form = car(val); |
| 213 | value_t args = cdr(val); |
| 214 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 215 | if (symstreq(form, "defun") || symstreq(form, "defmacro")) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 216 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 217 | enum namespace namespace = NS_FUNCTION; |
| 218 | |
| 219 | if (symstreq(form, "defmacro")) |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 220 | namespace = NS_MACRO; |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 221 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 222 | struct local local; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 223 | struct args *a; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 224 | char *name = (char *)(car(args) ^ SYMBOL_TAG); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 225 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 226 | dasm_State *d = compile_function(cdr(args), namespace, env, &local, |
| 227 | NULL, &a, name, fname); |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 228 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 229 | add_function(env, name, link_program(&d), a, namespace); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 230 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 231 | dasm_free(&d); |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 232 | del_local(&local); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 233 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 234 | else if (symstreq(form, "progn")) |
| 235 | { |
| 236 | for (value_t val = args; !nilp(val); val = cdr(val)) |
| 237 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 238 | compile_tl(car(val), env, fname); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void walk_and_alloc(struct local *local, value_t body) |
| 244 | { |
| 245 | if (!listp(body)) |
| 246 | return; |
| 247 | |
| 248 | value_t args = cdr(body); |
| 249 | |
| 250 | if (symstreq(car(body), "let1")) |
| 251 | { |
| 252 | int slot = local_alloc(local); |
| 253 | |
| 254 | value_t expr = cdr(args); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 255 | for (; !nilp(expr); expr = cdr(expr)) |
| 256 | { |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 257 | walk_and_alloc(local, car(expr)); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 258 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 259 | |
| 260 | local_free(local, slot); |
| 261 | } |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 262 | else if (symstreq(car(body), "lambda")) |
| 263 | { |
| 264 | // We don't want to walk the lambda because it's another function. When |
| 265 | // the lambda is compiled it will be walked. |
| 266 | return; |
| 267 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 268 | else |
| 269 | { |
| 270 | for (; !nilp(args); args = cdr(args)) |
| 271 | { |
| 272 | walk_and_alloc(local, car(args)); |
| 273 | } |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 277 | bool load(struct environment *env, char *path) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 278 | { |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 279 | if (!file_exists(path)) |
| 280 | return false; |
| 281 | |
| 282 | add_load(env, path); |
| 283 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 284 | unsigned char pool = make_pool(); |
| 285 | unsigned char pop = push_pool(pool); |
| 286 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 287 | struct istream *is = new_fistream(path, false); |
| 288 | if (!is) |
| 289 | return false; |
| 290 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 291 | value_t val; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 292 | |
| 293 | while (read1(is, &val)) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 294 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 295 | compile_tl(val, env, path); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 296 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 297 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 298 | del_fistream(is); |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 299 | pop_pool(pop); |
| 300 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 301 | return true; |
| 302 | } |
| 303 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 304 | value_t load_relative(struct environment *env, char *to, value_t name) |
| 305 | { |
| 306 | if (!stringp(name)) |
| 307 | return nil; |
| 308 | |
| 309 | char *new_path = (char *)(name ^ STRING_TAG); |
| 310 | char *relative_to = strdup(to); |
| 311 | char full_path[512]; |
| 312 | |
| 313 | snprintf(full_path, 512, "%s/%s", dirname(relative_to), new_path); |
| 314 | |
| 315 | if (load(env, full_path)) |
| 316 | return t; |
| 317 | else |
| 318 | return nil; |
| 319 | } |
| 320 | |
| 321 | struct environment *compile_file(char *filename, bool *ok) |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 322 | { |
| 323 | value_t val; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 324 | struct environment *env = malloc(sizeof(struct environment)); |
| 325 | env->first = NULL; |
| 326 | env->first_loaded = NULL; |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 327 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 328 | add_load(env, filename); |
| 329 | load_std(env); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 330 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 331 | bool ok_ = load(env, filename); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 332 | |
| 333 | if (ok) |
| 334 | *ok = ok_; |
| 335 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 336 | return env; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 337 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 338 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 339 | int nextpc(struct local *local, dasm_State **Dst) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 340 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 341 | int n = local->nextpc++; |
| 342 | if (n > local->npc) |
| 343 | { |
| 344 | local->npc += 16; |
| 345 | dasm_growpc(Dst, local->npc); |
| 346 | } |
| 347 | return n; |
| 348 | } |
| 349 | |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 350 | void compile_backquote(struct environment *env, struct local *local, |
| 351 | value_t val, dasm_State **Dst) |
| 352 | { |
| 353 | if (!listp(val)) |
| 354 | { |
| 355 | | mov eax, (val); |
| 356 | } |
| 357 | else |
| 358 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 359 | value_t fsym = car(val), args = cdr(val); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 360 | int nargs = length(args); |
| 361 | |
| 362 | // TODO |
| 363 | } |
| 364 | } |
| 365 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 366 | value_t eval(struct environment *env, value_t form) |
| 367 | { |
| 368 | // Eval! |
| 369 | value_t function = cons(nil, cons(form, nil)); |
| 370 | |
| 371 | struct local local; |
| 372 | struct args *args; |
| 373 | |
| 374 | dasm_State *d = compile_function(function, NS_ANONYMOUS, env, &local, NULL, |
| 375 | &args, NULL, "/"); |
| 376 | |
| 377 | del_local(&local); |
| 378 | |
| 379 | value_t (*f)() = link_program(&d); |
| 380 | return f(); |
| 381 | } |
| 382 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 383 | void compile_variable(struct variable *v, dasm_State *Dst) |
| 384 | { |
| 385 | switch (v->type) |
| 386 | { |
| 387 | case V_ARGUMENT: |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 388 | | mov eax, dword[ebp + (value_size * (v->number + 2))]; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 389 | break; |
| 390 | case V_BOUND: |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 391 | | mov eax, dword[ebp - ((v->number + 1) * value_size)]; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 392 | break; |
| 393 | case V_FREE: |
| 394 | // edi is the closure context pointer |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 395 | | mov eax, dword[edi + (v->number * value_size)]; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 396 | break; |
| 397 | default: |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 398 | err("Sorry, can only access V_ARGUMENT, V_FREE and V_BOUND variables " |
| 399 | "for now :("); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 403 | void compile_expression(struct environment *env, struct local *local, |
| 404 | value_t val, dasm_State **Dst) |
| 405 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 406 | if (symstreq(val, "nil") || nilp(val)) |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 407 | { |
| 408 | | mov eax, (nil); |
| 409 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 410 | else if (symstreq(val, "t")) |
| 411 | { |
| 412 | | mov eax, (t); |
| 413 | } |
| 414 | else if (integerp(val) || stringp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 415 | { |
| 416 | | mov eax, val; |
| 417 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 418 | else if (listp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 419 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 420 | value_t fsym = car(val); |
| 421 | value_t args = cdr(val); |
| 422 | int nargs = length(args); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 423 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 424 | if (!symbolp(fsym)) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 425 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 426 | printval(val, 2); |
| 427 | err_at(val, "function name must be a symbol"); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 428 | } |
| 429 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 430 | if (symstreq(fsym, "if")) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 431 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 432 | if (nargs < 2 || nargs > 3) |
| 433 | err("Must give at least 2 arguments to if"); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 434 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 435 | compile_expression(env, local, car(args), Dst); |
| 436 | int false_label = nextpc(local, Dst), |
| 437 | after_label = nextpc(local, Dst); |
| 438 | |
| 439 | // result is in eax |
| 440 | | cmp eax, (nil); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 441 | | je = > false_label; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 442 | |
| 443 | compile_expression(env, local, elt(args, 1), Dst); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 444 | | jmp = > after_label; |
| 445 | |= > false_label:; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 446 | if (nargs == 3) |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 447 | compile_expression(env, local, elt(args, 2), Dst); |
| 448 | |= > after_label: |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 449 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 450 | else if (symstreq(fsym, "progn")) |
| 451 | { |
| 452 | for (value_t val = args; !nilp(val); val = cdr(val)) |
| 453 | { |
| 454 | compile_expression(env, local, car(val), Dst); |
| 455 | } |
| 456 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 457 | else if (symstreq(fsym, "let1")) |
| 458 | { |
| 459 | if (nargs < 2) |
| 460 | { |
| 461 | err("Must give at least 2 arguments to let1"); |
| 462 | } |
| 463 | value_t binding = car(args); |
| 464 | value_t rest = cdr(args); |
| 465 | |
| 466 | if (length(binding) != 2) |
| 467 | { |
| 468 | err("Binding list in let1 must contain exactly two entries"); |
| 469 | } |
| 470 | |
| 471 | value_t name = car(binding); |
| 472 | value_t value = car(cdr(binding)); |
| 473 | |
| 474 | compile_expression(env, local, value, Dst); |
| 475 | |
| 476 | int i = local_alloc(local); |
| 477 | |
| 478 | add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i); |
| 479 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 480 | | mov dword[ebp - ((i + 1) * value_size)], eax; |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 481 | |
| 482 | for (; !nilp(rest); rest = cdr(rest)) |
| 483 | { |
| 484 | compile_expression(env, local, car(rest), Dst); |
| 485 | } |
| 486 | |
| 487 | local_free(local, i); |
| 488 | } |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 489 | else if (symstreq(fsym, "gc")) |
| 490 | { |
| 491 | if (nargs) |
| 492 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 493 | err_at(val, "gc takes no arguments"); |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | | run_gc; |
| 497 | } |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 498 | else if (symstreq(fsym, "quote")) |
| 499 | { |
| 500 | if (nargs != 1) |
| 501 | err("quote should take exactly 1 argument"); |
| 502 | |
| 503 | // Simple! |
| 504 | | mov eax, (car(args)); |
| 505 | } |
| 506 | else if (symstreq(fsym, "backquote")) |
| 507 | { |
| 508 | if (nargs != 1) |
| 509 | err("backquote should take exactly 1 argument"); |
| 510 | |
| 511 | compile_backquote(env, local, car(args), Dst); |
| 512 | } |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 513 | else if (symstreq(fsym, "function")) |
| 514 | { |
| 515 | if (nargs != 1) |
| 516 | { |
| 517 | err("function should take exactly 1 argument"); |
| 518 | } |
| 519 | |
| 520 | if (!symbolp(car(args))) |
| 521 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 522 | err("argument to function should be a symbol resolvable at " |
| 523 | "compile time"); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 524 | } |
| 525 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 526 | struct function *f = |
| 527 | find_function(env, (char *)(car(args) ^ SYMBOL_TAG)); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 528 | value_t closure = create_closure(f->code_ptr, f->args, 0); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 529 | |
| 530 | | mov eax, (closure); |
| 531 | } |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 532 | else if (symstreq(fsym, "list")) |
| 533 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 534 | | push(nil); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 535 | |
| 536 | for (int i = nargs - 1; i >= 0; i--) |
| 537 | { |
| 538 | compile_expression(env, local, elt(args, i), Dst); |
| 539 | |
| 540 | // push the ith item |
| 541 | | push eax; |
| 542 | // cons the top two stack items |
| 543 | | mov ebx, (cons); |
| 544 | | call ebx; |
| 545 | // remove the stack items from use |
| 546 | | add esp, (2 * value_size); |
| 547 | // put the new thing on the stack |
| 548 | | push eax; |
| 549 | } |
| 550 | |
| 551 | | pop eax; |
| 552 | } |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 553 | else if (symstreq(fsym, "lambda")) |
| 554 | { |
| 555 | // Compile the function with this as the parent scope |
| 556 | struct local new_local; |
| 557 | int nargs_out; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 558 | dasm_State *d = compile_function( |
| 559 | args, NS_ANONYMOUS, env, &new_local, local, &nargs_out, |
| 560 | "recurse", local->current_file_path); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 561 | |
| 562 | // Link the function |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 563 | void *func_ptr = link_program(&d); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 564 | |
| 565 | // Create a closure object with the correct number of captures at |
| 566 | // runtime |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 567 | | push(new_local.num_closure_slots); |
| 568 | | push(nargs_out); |
| 569 | | push(func_ptr); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 570 | | mov ebx, (create_closure); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 571 | | call ebx; |
| 572 | | add esp, 12; |
| 573 | |
| 574 | // Walk the generated local scope for V_FREE variables, since each |
| 575 | // of these exists in our scope (or higher), evaluate it and set it |
| 576 | // as a member of the lambda capture. |
| 577 | |
| 578 | for (struct variable *var = new_local.first; var; var = var->prev) |
| 579 | { |
| 580 | if (var->type == V_FREE) |
| 581 | { |
| 582 | // Closure in eax |
| 583 | | push eax; |
| 584 | // Variable now in eax |
| 585 | compile_variable(find_variable(local, var->name), Dst); |
| 586 | | push eax; |
| 587 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 588 | // The capture offset |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 589 | | push(var->number); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 590 | | mov ebx, (set_closure_capture_variable); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 591 | | call ebx; |
| 592 | // Skip the value and index |
| 593 | | add esp, 8; |
| 594 | // Pop the closure back in to eax |
| 595 | | pop eax; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | // Closure is still in eax |
| 600 | |
| 601 | dasm_free(&d); |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 602 | del_local(&new_local); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 603 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 604 | else if (symstreq(fsym, "eval")) |
| 605 | { |
| 606 | if (nargs != 1) |
| 607 | { |
| 608 | err("eval takes exactly 1 argument"); |
| 609 | } |
| 610 | |
| 611 | compile_expression(env, local, car(args), Dst); |
| 612 | | push eax; |
| 613 | | push(env); |
| 614 | | mov ebx, (eval); |
| 615 | | call ebx; |
| 616 | } |
| 617 | else if (symstreq(fsym, "load")) |
| 618 | { |
| 619 | if (nargs != 1) |
| 620 | { |
| 621 | err_at(val, "load takes exactly 1 argument, %d given", nargs); |
| 622 | } |
| 623 | |
| 624 | compile_expression(env, local, car(args), Dst); |
| 625 | | push eax; |
| 626 | | push(local->current_file_path); |
| 627 | | push(env); |
| 628 | | mov ebx, (load_relative); |
| 629 | | call ebx; |
| 630 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 631 | else |
| 632 | { |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 633 | char *name = (char *)(fsym ^ SYMBOL_TAG); |
| 634 | struct function *func = find_function(env, name); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 635 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 636 | bool is_recursive = false; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 637 | struct args *nargs_needed = NULL; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 638 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 639 | if (local->current_function_name && |
| 640 | symstreq(fsym, local->current_function_name)) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 641 | { |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 642 | is_recursive = true; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 643 | nargs_needed = local->args; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 644 | } |
| 645 | else |
| 646 | { |
| 647 | if (func == NULL) |
| 648 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 649 | err_at(val, "Function %s undefined", name); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 650 | } |
| 651 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 652 | nargs_needed = func->args; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 653 | } |
| 654 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 655 | if (!are_args_acceptable(nargs_needed, nargs)) |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 656 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 657 | err_at(val, |
| 658 | "wrong number of args in function call: %s at %s:%d, " |
| 659 | "want %d args but given %d\n", |
| 660 | name, cons_file(val), cons_line(val), |
| 661 | nargs_needed->num_required, nargs); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 662 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 663 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 664 | if (is_recursive || func->namespace == NS_FUNCTION) |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 665 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 666 | int nargs = length(args); |
| 667 | |
| 668 | if (nargs <= nargs_needed->num_required) |
| 669 | { |
| 670 | // Push the variadic list (nil) |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 671 | | push(nil); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 672 | } |
| 673 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 674 | for (int i = nargs_needed->num_optional - 1; |
| 675 | i >= nargs - nargs_needed->num_required; i--) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 676 | { |
| 677 | // Push the default optional values |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 678 | | push(nargs_needed->optional_arguments[i].value); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | for (int i = nargs - 1; i >= 0; i--) |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 682 | { |
| 683 | compile_expression(env, local, elt(args, i), Dst); |
| 684 | | push eax; |
| 685 | } |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 686 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 687 | if (is_recursive) |
| 688 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 689 | | call->function_start; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 690 | } |
| 691 | else |
| 692 | { |
| 693 | | mov ebx, (func->code_addr); |
| 694 | | call ebx; |
| 695 | } |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 696 | | add esp, (nargs * value_size); |
| 697 | // result in eax |
| 698 | } |
| 699 | else if (func->namespace == NS_MACRO) |
| 700 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 701 | // Make sure that the stuff allocated by the macro isn't in a |
| 702 | // pool |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 703 | unsigned char pool = push_pool(0); |
| 704 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 705 | value_t expanded_to = call_list(func, args); |
| 706 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 707 | pop_pool(pool); |
| 708 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 709 | compile_expression(env, local, expanded_to, Dst); |
| 710 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 711 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 712 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 713 | else if (symbolp(val)) |
| 714 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 715 | if (symstreq(val, "+current-file+")) |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 716 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 717 | value_t file_name_val = strval(local->current_file_path); |
| 718 | |
| 719 | | mov eax, (file_name_val); |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 720 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 721 | else |
| 722 | { |
| 723 | struct variable *v = |
| 724 | find_variable(local, (char *)(val ^ SYMBOL_TAG)); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 725 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 726 | if (!v) |
| 727 | { |
| 728 | fprintf(stderr, "var: %s\n", (char *)(val ^ SYMBOL_TAG)); |
| 729 | err("Variable unbound"); |
| 730 | } |
| 731 | |
| 732 | compile_variable(v, Dst); |
| 733 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 734 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 735 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 736 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 737 | struct variable *add_variable(struct local *local, enum var_type type, |
| 738 | char *name, int number) |
| 739 | { |
| 740 | struct variable *var = malloc(sizeof(struct variable)); |
| 741 | var->prev = local->first; |
| 742 | var->type = type; |
| 743 | var->name = name; |
| 744 | var->number = number; |
| 745 | |
| 746 | local->first = var; |
| 747 | |
| 748 | return var; |
| 749 | } |
| 750 | |
| 751 | void destroy_local(struct local *local) |
| 752 | { |
| 753 | for (struct variable *v = local->first; v;) |
| 754 | { |
| 755 | struct variable *t = v; |
| 756 | v = v->prev; |
| 757 | free(t); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | struct variable *find_variable(struct local *local, char *name) |
| 762 | { |
| 763 | struct variable *v = local->first; |
| 764 | |
| 765 | for (; v && strcmp(v->name, name) != 0; v = v->prev) |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 766 | { |
| 767 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 768 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 769 | if (!v) |
| 770 | { |
| 771 | if (local->parent) |
| 772 | { |
| 773 | v = find_variable(local->parent, name); |
| 774 | |
| 775 | if (v) |
| 776 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 777 | // We found this in a parent scope, add it as a V_FREE variable |
| 778 | // to skip the search. |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 779 | v = add_variable(local, V_FREE, name, |
| 780 | local->num_closure_slots++); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 784 | return v; |
| 785 | } |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 786 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 787 | extern value_t _call_list(void *addr, value_t list, value_t *edi); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 788 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 789 | value_t call_list_args(void *code_ptr, struct args *args, value_t list, |
| 790 | void *data) |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 791 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 792 | list = deep_copy(list); |
| 793 | int nargs = length(list); |
| 794 | |
| 795 | value_t *val = NULL; |
| 796 | |
| 797 | for (value_t i = list; !nilp(i); i = cdr(i)) |
| 798 | { |
| 799 | val = cdrref(i); |
| 800 | } |
| 801 | |
| 802 | int total_required = args->num_required + args->num_optional; |
| 803 | |
| 804 | if (nargs > total_required) |
| 805 | { |
| 806 | // Take the remainder of the list and put it as the last item in the |
| 807 | // list. |
| 808 | value_t trailing = cxdr(list, total_required); |
| 809 | value_t last_item = cons(trailing, nil); |
| 810 | |
| 811 | *cxdrref(&list, total_required) = last_item; |
| 812 | } |
| 813 | else if (nargs < total_required) |
| 814 | { |
| 815 | for (int i = nargs - args->num_required; i < args->num_optional; i++) |
| 816 | { |
| 817 | // Append the i-th defualt argument |
| 818 | value_t appended = cons(args->optional_arguments[i].value, nil); |
| 819 | *val = appended; |
| 820 | val = cdrref(appended); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | // We want to call this if we pass the correct # of arguments or less, just |
| 825 | // not if we have already passed varargs. Appends a nil argument. |
| 826 | if (nargs <= total_required) |
| 827 | { |
| 828 | // Enough real arguments but no variadic arguments. Pass a nil list. |
| 829 | *val = cons(nil, nil); |
| 830 | } |
| 831 | |
| 832 | return _call_list(code_ptr, list, data); |
| 833 | } |
| 834 | |
| 835 | value_t call_list(struct function *fun, value_t list) |
| 836 | { |
| 837 | return call_list_args(fun->code_ptr, fun->args, list, NULL); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | value_t call_list_closure(struct closure *c, value_t list) |
| 841 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 842 | return call_list_args(c->function, c->args, list, c->data); |
| 843 | } |
| 844 | |
| 845 | struct args *new_args() |
| 846 | { |
| 847 | struct args *a = malloc(sizeof(struct args)); |
| 848 | a->num_optional = 0; |
| 849 | a->num_required = 0; |
| 850 | a->variadic = false; |
| 851 | |
| 852 | return a; |
| 853 | } |
| 854 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 855 | 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] | 856 | { |
| 857 | int i = args->num_optional++; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 858 | args = |
| 859 | realloc(args, sizeof(struct args) + sizeof(struct optional_argument) * |
| 860 | args->num_optional); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 861 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 862 | args->optional_arguments[i] = (struct optional_argument){ |
| 863 | .value = value, |
| 864 | .name = name, |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 865 | }; |
| 866 | |
| 867 | return args; |
| 868 | } |
| 869 | |
| 870 | bool are_args_acceptable(struct args *args, int number) |
| 871 | { |
| 872 | if (args->variadic) |
| 873 | { |
| 874 | return number >= args->num_required; |
| 875 | } |
| 876 | else |
| 877 | { |
| 878 | return number >= args->num_required && |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 879 | number <= args->num_required + args->num_optional; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 883 | struct args *list_to_args(struct environment *env, value_t list, |
| 884 | struct local *local) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 885 | { |
| 886 | struct args *args = new_args(); |
| 887 | |
| 888 | bool in_optional = false; |
| 889 | |
| 890 | for (value_t i = list; !nilp(i); i = cdr(i)) |
| 891 | { |
| 892 | value_t val = car(i); |
| 893 | if (symbolp(val)) |
| 894 | { |
| 895 | if (!args->variadic && symstreq(val, "&")) |
| 896 | { |
| 897 | i = cdr(i); |
| 898 | value_t name = car(i); |
| 899 | |
| 900 | if (!symbolp(name)) |
| 901 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 902 | err("You must provide a symbol after & in an argument list " |
| 903 | "to bind the\n" |
| 904 | "variadic arguments to."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | args->variadic = true; |
| 908 | |
| 909 | add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG), |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 910 | args->num_optional + args->num_required); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 911 | |
| 912 | continue; |
| 913 | } |
| 914 | |
| 915 | if (!in_optional) |
| 916 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 917 | add_variable(local, V_ARGUMENT, (char *)(val ^ SYMBOL_TAG), |
| 918 | args->num_required++); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 919 | } |
| 920 | else |
| 921 | { |
| 922 | char *name = (char *)(val ^ SYMBOL_TAG); |
| 923 | if (name[0] == '&') |
| 924 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 925 | err("Non-optional argument following optional arguments " |
| 926 | "starts with a &\n" |
| 927 | "did you mean to declare a variadic argument? If so " |
| 928 | "leave a space\n" |
| 929 | "between the & and name."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 930 | } |
| 931 | else |
| 932 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 933 | err("Cannot define a non-optional argument after an " |
| 934 | "optional one."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | } |
| 938 | else if (listp(val)) |
| 939 | { |
| 940 | in_optional = true; |
| 941 | int len = length(val); |
| 942 | |
| 943 | if (len != 2) |
| 944 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 945 | err("A list defining an optional value must be structured like " |
| 946 | "(name expr)\n" |
| 947 | "with exactly two arguments."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | value_t name = car(val); |
| 951 | value_t expr = car(cdr(val)); |
| 952 | |
| 953 | value_t function = cons(nil, cons(expr, nil)); |
| 954 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 955 | dasm_State *d = |
| 956 | compile_function(function, NS_ANONYMOUS, env, NULL, NULL, NULL, |
| 957 | NULL, local->current_file_path); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 958 | |
| 959 | // TODO: GC stack top! |
| 960 | value_t (*compiled)() = link_program(&d); |
| 961 | |
| 962 | value_t value = compiled(); |
| 963 | args = add_optional_arg(args, name, value); |
| 964 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 965 | add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG), |
| 966 | args->num_required + args->num_optional - 1); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
| 970 | return args; |
| 971 | } |
| 972 | |
| 973 | void display_args(struct args *args) |
| 974 | { |
| 975 | printf("Args object taking %d require arguments and %d optionals:\n", |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 976 | args->num_required, args->num_optional); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 977 | |
| 978 | for (int i = 0; i < args->num_optional; i++) |
| 979 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 980 | printf(" %d\t%s\n", i, |
| 981 | (char *)(args->optional_arguments[i].name ^ SYMBOL_TAG)); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 982 | printval(args->optional_arguments[i].value, 2); |
| 983 | } |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 984 | } |