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