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