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 | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 4 | #include "lib/std.h" |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 5 | #include "plat/plat.h" |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 6 | #include "gc.h" |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 7 | |
| 8 | #include <dasm_proto.h> |
| 9 | #include <dasm_x86.h> |
| 10 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 14 | #define value_size sizeof(value_t) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 15 | |
| 16 | |.arch x86; |
| 17 | |
| 18 | |.macro setup, nvars; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 19 | |->function_start: |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 20 | | push ebp; |
| 21 | | mov ebp, esp; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 22 | | sub esp, (value_size * nvars); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 23 | |.endmacro; |
| 24 | |
| 25 | |.macro cleanup; |
| 26 | | mov esp, ebp; |
| 27 | | pop ebp; |
| 28 | | ret; |
| 29 | |.endmacro; |
| 30 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 31 | |.macro local_var, index; |
| 32 | |.endmacro; |
| 33 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 34 | dasm_State *d; |
| 35 | unsigned int npc = 8; |
| 36 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 37 | |.macro run_gc; |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 38 | | mov eax, esp; |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 39 | | push ebp; |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 40 | | push eax; |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 41 | | mov eax, _do_gc; |
| 42 | | call eax; |
| 43 | |.endmacro; |
swissChili | 6d6525e | 2021-06-15 21:20:53 -0700 | [diff] [blame] | 44 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 45 | struct function *find_function(struct environment *env, char *name) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 46 | { |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 47 | struct function *f; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 48 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 49 | for (f = env->first; f && strcmp(f->name, name); f = f->prev) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 50 | { |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | return f; |
| 54 | } |
| 55 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 56 | unsigned int local_alloc(struct local *local) |
| 57 | { |
| 58 | for (int i = 0; i < local->num_stack_slots; i++) |
| 59 | { |
| 60 | if (local->stack_slots[i] == false) |
| 61 | { |
| 62 | local->stack_slots[i] = true; |
| 63 | |
| 64 | if (i >= local->num_stack_entries) |
| 65 | local->num_stack_entries++; |
| 66 | |
| 67 | return i; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | int old_size = local->num_stack_slots; |
| 72 | local->num_stack_slots += 4; |
| 73 | local->stack_slots = realloc(local->stack_slots, local->num_stack_slots * sizeof(bool)); |
| 74 | // unreadable: set the remaining slots to unused |
| 75 | memset(local->stack_slots + old_size, 0, local->num_stack_slots - old_size); |
| 76 | local->stack_slots[old_size] = true; |
| 77 | |
| 78 | return old_size; |
| 79 | } |
| 80 | |
| 81 | void local_free(struct local *local, unsigned int slot) |
| 82 | { |
| 83 | local->stack_slots[slot] = false; |
| 84 | } |
| 85 | |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 86 | void del_local(struct local *local) |
| 87 | { |
| 88 | free(local->stack_slots); |
| 89 | |
| 90 | for (struct variable *next, *f = local->first; f; f = next) |
| 91 | { |
| 92 | next = f->prev; |
| 93 | free(f); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void del_env(struct environment *env) |
| 98 | { |
| 99 | for (struct function *next, *f = env->first; f; f = next) |
| 100 | { |
| 101 | next = f->prev; |
| 102 | // We're not gonna bother munmap()ing the function |
| 103 | free(f); |
| 104 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 105 | |
| 106 | for (struct loaded_file *next, *l = env->first_loaded; l; l = next) |
| 107 | { |
| 108 | next = l->previous; |
| 109 | free(l->resolved_path); |
| 110 | free(l); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void add_load(struct environment *env, char *path) |
| 115 | { |
| 116 | static char buffer[512]; |
| 117 | long size = readlink(path, buffer, 512); |
| 118 | buffer[size] = '\0'; |
| 119 | char *resolved = strdup(buffer); |
| 120 | |
| 121 | struct loaded_file *f = malloc(sizeof(struct loaded_file)); |
| 122 | f->resolved_path = resolved; |
| 123 | f->previous = env->first_loaded; |
| 124 | env->first_loaded = f; |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 125 | } |
| 126 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 127 | struct dasm_State *compile_function(value_t args, enum namespace namespace, |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 128 | struct environment *env, struct local *local_out, |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 129 | struct local *local_parent, int *nargs, char *name) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 130 | { |
| 131 | dasm_State *d; |
| 132 | dasm_State **Dst = &d; |
| 133 | |
| 134 | |.section code; |
| 135 | dasm_init(&d, DASM_MAXSECTION); |
| 136 | |
| 137 | |.globals lbl_; |
| 138 | void *labels[lbl__MAX]; |
| 139 | dasm_setupglobal(&d, labels, lbl__MAX); |
| 140 | |
| 141 | |.actionlist lisp_actions; |
| 142 | dasm_setup(&d, lisp_actions); |
| 143 | |
| 144 | struct local local; |
| 145 | local.parent = NULL; |
| 146 | local.first = NULL; |
| 147 | local.num_vars = 0; |
| 148 | local.npc = 8; |
| 149 | local.nextpc = 0; |
| 150 | local.stack_slots = malloc(sizeof(bool) * 4); |
| 151 | memset(local.stack_slots, 0, sizeof(bool) * 4); |
| 152 | local.num_stack_slots = 4; |
| 153 | local.num_stack_entries = 0; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 154 | local.num_closure_slots = 0; |
| 155 | local.parent = local_parent; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 156 | local.current_function_name = name; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 157 | |
| 158 | dasm_growpc(&d, local.npc); |
| 159 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 160 | value_t arglist = car(args); |
| 161 | value_t body = cdr(args); |
| 162 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 163 | local.num_args = length(arglist); |
| 164 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 165 | value_t a = arglist; |
| 166 | for (int i = 0; !nilp(a); a = cdr(a), i++) |
| 167 | { |
| 168 | if (!symbolp(car(a))) |
| 169 | { |
| 170 | err("defun argument must be a symbol"); |
| 171 | } |
| 172 | |
| 173 | add_variable(&local, V_ARGUMENT, (char *)(car(a) ^ SYMBOL_TAG), i); |
| 174 | } |
| 175 | |
| 176 | for (value_t body_ = body; !nilp(body_); body_ = cdr(body_)) |
| 177 | { |
| 178 | walk_and_alloc(&local, car(body_)); |
| 179 | } |
| 180 | |
| 181 | | setup (local.num_stack_entries); |
| 182 | |
| 183 | memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool)); |
| 184 | local.num_stack_entries = 0; |
| 185 | |
| 186 | for (; !nilp(body); body = cdr(body)) |
| 187 | { |
| 188 | compile_expression(env, &local, car(body), Dst); |
| 189 | } |
| 190 | |
| 191 | | cleanup; |
| 192 | |
| 193 | if (local_out) |
| 194 | *local_out = local; |
| 195 | |
| 196 | if (nargs) |
| 197 | *nargs = length(arglist); |
| 198 | |
| 199 | return d; |
| 200 | } |
| 201 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 202 | void compile_tl(value_t val, struct environment *env) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 203 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 204 | if (!listp(val)) |
| 205 | err("Top level must be a list"); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 206 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 207 | value_t form = car(val); |
| 208 | value_t args = cdr(val); |
| 209 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 210 | if (symstreq(form, "defun") || symstreq(form, "defmacro")) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 211 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 212 | enum namespace namespace = NS_FUNCTION; |
| 213 | |
| 214 | if (symstreq(form, "defmacro")) |
| 215 | namespace = NS_MACRO; |
| 216 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 217 | struct local local; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 218 | int nargs; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 219 | char *name = (char *)(car(args) ^ SYMBOL_TAG); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 220 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 221 | dasm_State *d = compile_function(cdr(args), namespace, env, &local, NULL, &nargs, name); |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 222 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 223 | add_function(env, name, link_program(&d), |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 224 | nargs, namespace); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 225 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 226 | dasm_free(&d); |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 227 | del_local(&local); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 228 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 229 | else if (symstreq(form, "progn")) |
| 230 | { |
| 231 | for (value_t val = args; !nilp(val); val = cdr(val)) |
| 232 | { |
| 233 | compile_tl(car(val), env); |
| 234 | } |
| 235 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void walk_and_alloc(struct local *local, value_t body) |
| 239 | { |
| 240 | if (!listp(body)) |
| 241 | return; |
| 242 | |
| 243 | value_t args = cdr(body); |
| 244 | |
| 245 | if (symstreq(car(body), "let1")) |
| 246 | { |
| 247 | int slot = local_alloc(local); |
| 248 | |
| 249 | value_t expr = cdr(args); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 250 | for (; !nilp(expr); expr = cdr(expr)) |
| 251 | { |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 252 | walk_and_alloc(local, car(expr)); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 253 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 254 | |
| 255 | local_free(local, slot); |
| 256 | } |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 257 | else if (symstreq(car(body), "lambda")) |
| 258 | { |
| 259 | // We don't want to walk the lambda because it's another function. When |
| 260 | // the lambda is compiled it will be walked. |
| 261 | return; |
| 262 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 263 | else |
| 264 | { |
| 265 | for (; !nilp(args); args = cdr(args)) |
| 266 | { |
| 267 | walk_and_alloc(local, car(args)); |
| 268 | } |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 272 | bool load(struct environment *env, char *path) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 273 | { |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 274 | if (!file_exists(path)) |
| 275 | return false; |
| 276 | |
| 277 | add_load(env, path); |
| 278 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 279 | unsigned char pool = make_pool(); |
| 280 | unsigned char pop = push_pool(pool); |
| 281 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 282 | struct istream *is = new_fistream(path, false); |
| 283 | if (!is) |
| 284 | return false; |
| 285 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 286 | value_t val; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 287 | |
| 288 | while (read1(is, &val)) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 289 | { |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 290 | compile_tl(val, env); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 291 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 292 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 293 | del_fistream(is); |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 294 | pop_pool(pop); |
| 295 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 296 | return true; |
| 297 | } |
| 298 | |
| 299 | struct environment compile_file(char *filename, bool *ok) |
| 300 | { |
| 301 | value_t val; |
| 302 | struct environment env; |
| 303 | env.first = NULL; |
| 304 | env.first_loaded = NULL; |
| 305 | |
| 306 | add_load(&env, filename); |
| 307 | load_std(&env); |
| 308 | |
| 309 | bool ok_ = load(&env, filename); |
| 310 | |
| 311 | if (ok) |
| 312 | *ok = ok_; |
| 313 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 314 | return env; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 315 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 316 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 317 | int nextpc(struct local *local, dasm_State **Dst) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 318 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 319 | int n = local->nextpc++; |
| 320 | if (n > local->npc) |
| 321 | { |
| 322 | local->npc += 16; |
| 323 | dasm_growpc(Dst, local->npc); |
| 324 | } |
| 325 | return n; |
| 326 | } |
| 327 | |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 328 | void compile_backquote(struct environment *env, struct local *local, |
| 329 | value_t val, dasm_State **Dst) |
| 330 | { |
| 331 | if (!listp(val)) |
| 332 | { |
| 333 | | mov eax, (val); |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | value_t fsym = car(val), |
| 338 | args = cdr(val); |
| 339 | int nargs = length(args); |
| 340 | |
| 341 | // TODO |
| 342 | } |
| 343 | } |
| 344 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 345 | void compile_variable(struct variable *v, dasm_State *Dst) |
| 346 | { |
| 347 | switch (v->type) |
| 348 | { |
| 349 | case V_ARGUMENT: |
| 350 | | mov eax, dword [ebp + (value_size * (v->number + 2))]; |
| 351 | break; |
| 352 | case V_BOUND: |
| 353 | | mov eax, dword [ebp - ((v->number + 1) * value_size)]; |
| 354 | break; |
| 355 | case V_FREE: |
| 356 | // edi is the closure context pointer |
| 357 | | mov eax, dword [edi + (v->number * value_size)]; |
| 358 | break; |
| 359 | default: |
| 360 | err("Sorry, can only access V_ARGUMENT, V_FREE and V_BOUND variables for now :("); |
| 361 | } |
| 362 | } |
| 363 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 364 | void compile_expression(struct environment *env, struct local *local, |
| 365 | value_t val, dasm_State **Dst) |
| 366 | { |
| 367 | if (symstreq(val, "nil")) |
| 368 | { |
| 369 | | mov eax, (nil); |
| 370 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 371 | else if (symstreq(val, "t")) |
| 372 | { |
| 373 | | mov eax, (t); |
| 374 | } |
| 375 | else if (integerp(val) || stringp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 376 | { |
| 377 | | mov eax, val; |
| 378 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 379 | else if (listp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 380 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 381 | value_t fsym = car(val); |
| 382 | value_t args = cdr(val); |
| 383 | int nargs = length(args); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 384 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 385 | if (!symbolp(fsym)) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 386 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 387 | err("function name must be a symbol"); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 388 | } |
| 389 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 390 | if (symstreq(fsym, "if")) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 391 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 392 | if (nargs < 2 || nargs > 3) |
| 393 | err("Must give at least 2 arguments to if"); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 394 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 395 | compile_expression(env, local, car(args), Dst); |
| 396 | int false_label = nextpc(local, Dst), |
| 397 | after_label = nextpc(local, Dst); |
| 398 | |
| 399 | // result is in eax |
| 400 | | cmp eax, (nil); |
| 401 | | je =>false_label; |
| 402 | |
| 403 | compile_expression(env, local, elt(args, 1), Dst); |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 404 | | jmp =>after_label; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 405 | |=>false_label:; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 406 | if (nargs == 3) |
| 407 | compile_expression(env, local, elt(args, 2), Dst); |
| 408 | |=>after_label: |
| 409 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 410 | else if (symstreq(fsym, "progn")) |
| 411 | { |
| 412 | for (value_t val = args; !nilp(val); val = cdr(val)) |
| 413 | { |
| 414 | compile_expression(env, local, car(val), Dst); |
| 415 | } |
| 416 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 417 | else if (symstreq(fsym, "let1")) |
| 418 | { |
| 419 | if (nargs < 2) |
| 420 | { |
| 421 | err("Must give at least 2 arguments to let1"); |
| 422 | } |
| 423 | value_t binding = car(args); |
| 424 | value_t rest = cdr(args); |
| 425 | |
| 426 | if (length(binding) != 2) |
| 427 | { |
| 428 | err("Binding list in let1 must contain exactly two entries"); |
| 429 | } |
| 430 | |
| 431 | value_t name = car(binding); |
| 432 | value_t value = car(cdr(binding)); |
| 433 | |
| 434 | compile_expression(env, local, value, Dst); |
| 435 | |
| 436 | int i = local_alloc(local); |
| 437 | |
| 438 | add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i); |
| 439 | |
| 440 | | mov dword [ebp - ((i + 1) * value_size)], eax; |
| 441 | |
| 442 | for (; !nilp(rest); rest = cdr(rest)) |
| 443 | { |
| 444 | compile_expression(env, local, car(rest), Dst); |
| 445 | } |
| 446 | |
| 447 | local_free(local, i); |
| 448 | } |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 449 | else if (symstreq(fsym, "gc")) |
| 450 | { |
| 451 | if (nargs) |
| 452 | { |
| 453 | err("gc takes no arguments"); |
| 454 | } |
| 455 | |
| 456 | | run_gc; |
| 457 | } |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 458 | else if (symstreq(fsym, "quote")) |
| 459 | { |
| 460 | if (nargs != 1) |
| 461 | err("quote should take exactly 1 argument"); |
| 462 | |
| 463 | // Simple! |
| 464 | | mov eax, (car(args)); |
| 465 | } |
| 466 | else if (symstreq(fsym, "backquote")) |
| 467 | { |
| 468 | if (nargs != 1) |
| 469 | err("backquote should take exactly 1 argument"); |
| 470 | |
| 471 | compile_backquote(env, local, car(args), Dst); |
| 472 | } |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 473 | else if (symstreq(fsym, "function")) |
| 474 | { |
| 475 | if (nargs != 1) |
| 476 | { |
| 477 | err("function should take exactly 1 argument"); |
| 478 | } |
| 479 | |
| 480 | if (!symbolp(car(args))) |
| 481 | { |
| 482 | err("argument to function should be a symbol resolvable at compile time"); |
| 483 | } |
| 484 | |
| 485 | struct function *f = find_function(env, (char *)(car(args) ^ SYMBOL_TAG)); |
| 486 | value_t closure = create_closure(f->code_ptr, f->nargs, 0); |
| 487 | |
| 488 | | mov eax, (closure); |
| 489 | } |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 490 | else if (symstreq(fsym, "list")) |
| 491 | { |
| 492 | | push (nil); |
| 493 | |
| 494 | for (int i = nargs - 1; i >= 0; i--) |
| 495 | { |
| 496 | compile_expression(env, local, elt(args, i), Dst); |
| 497 | |
| 498 | // push the ith item |
| 499 | | push eax; |
| 500 | // cons the top two stack items |
| 501 | | mov ebx, (cons); |
| 502 | | call ebx; |
| 503 | // remove the stack items from use |
| 504 | | add esp, (2 * value_size); |
| 505 | // put the new thing on the stack |
| 506 | | push eax; |
| 507 | } |
| 508 | |
| 509 | | pop eax; |
| 510 | } |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 511 | else if (symstreq(fsym, "lambda")) |
| 512 | { |
| 513 | // Compile the function with this as the parent scope |
| 514 | struct local new_local; |
| 515 | int nargs_out; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 516 | dasm_State *d = compile_function(args, NS_ANONYMOUS, env, &new_local, local, &nargs_out, "recurse"); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 517 | |
| 518 | // Link the function |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 519 | void *func_ptr = link_program(&d); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 520 | |
| 521 | // Create a closure object with the correct number of captures at |
| 522 | // runtime |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 523 | | push (new_local.num_closure_slots); |
| 524 | | push (nargs_out); |
| 525 | | push (func_ptr); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 526 | | mov ebx, (create_closure); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 527 | | call ebx; |
| 528 | | add esp, 12; |
| 529 | |
| 530 | // Walk the generated local scope for V_FREE variables, since each |
| 531 | // of these exists in our scope (or higher), evaluate it and set it |
| 532 | // as a member of the lambda capture. |
| 533 | |
| 534 | for (struct variable *var = new_local.first; var; var = var->prev) |
| 535 | { |
| 536 | if (var->type == V_FREE) |
| 537 | { |
| 538 | // Closure in eax |
| 539 | | push eax; |
| 540 | // Variable now in eax |
| 541 | compile_variable(find_variable(local, var->name), Dst); |
| 542 | | push eax; |
| 543 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 544 | // The capture offset |
| 545 | | push (var->number); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 546 | | mov ebx, (set_closure_capture_variable); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 547 | | call ebx; |
| 548 | // Skip the value and index |
| 549 | | add esp, 8; |
| 550 | // Pop the closure back in to eax |
| 551 | | pop eax; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | // Closure is still in eax |
| 556 | |
| 557 | dasm_free(&d); |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 558 | del_local(&new_local); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 559 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 560 | else |
| 561 | { |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 562 | char *name = (char *)(fsym ^ SYMBOL_TAG); |
| 563 | struct function *func = find_function(env, name); |
| 564 | |
| 565 | bool is_recursive = false; |
| 566 | int nargs_needed = 0; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 567 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 568 | if (symstreq(fsym, local->current_function_name)) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 569 | { |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 570 | is_recursive = true; |
| 571 | nargs_needed = local->num_args; |
| 572 | } |
| 573 | else |
| 574 | { |
| 575 | if (func == NULL) |
| 576 | { |
| 577 | fprintf(stderr, "Function call: %s at %s:%d\n", name, cons_file(val), cons_line(val)); |
| 578 | err("Function undefined"); |
| 579 | } |
| 580 | |
| 581 | nargs_needed = func->nargs; |
| 582 | } |
| 583 | |
| 584 | if (nargs != nargs_needed) |
| 585 | { |
| 586 | fprintf(stderr, "Function call: %s at %s:%d, want %d args but given %d\n", |
| 587 | name, cons_file(val), cons_line(val), nargs_needed, nargs); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 588 | err("wrong number of args"); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 589 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 590 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 591 | if (is_recursive || func->namespace == NS_FUNCTION) |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 592 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 593 | for (int i = length(args) - 1; i >= 0; i--) |
| 594 | { |
| 595 | compile_expression(env, local, elt(args, i), Dst); |
| 596 | | push eax; |
| 597 | } |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 598 | |
| 599 | if (is_recursive) |
| 600 | { |
| 601 | | call ->function_start; |
| 602 | } |
| 603 | else |
| 604 | { |
| 605 | | mov ebx, (func->code_addr); |
| 606 | | call ebx; |
| 607 | } |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 608 | | add esp, (nargs * value_size); |
| 609 | // result in eax |
| 610 | } |
| 611 | else if (func->namespace == NS_MACRO) |
| 612 | { |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 613 | // Make sure that the stuff allocated by the macro isn't in a pool |
| 614 | unsigned char pool = push_pool(0); |
| 615 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 616 | value_t expanded_to = call_list(func, args); |
| 617 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 618 | pop_pool(pool); |
| 619 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 620 | compile_expression(env, local, expanded_to, Dst); |
| 621 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 622 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 623 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 624 | else if (symbolp(val)) |
| 625 | { |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 626 | struct variable *v = find_variable(local, (char *)(val ^ SYMBOL_TAG)); |
| 627 | |
| 628 | if (!v) |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 629 | { |
| 630 | fprintf(stderr, "var: %s\n", (char *)(val ^ SYMBOL_TAG)); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 631 | err("Variable unbound"); |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 632 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 633 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 634 | compile_variable(v, Dst); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 635 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 636 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 637 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 638 | void compile_expr_to_func(struct environment *env, char *name, value_t val, |
| 639 | dasm_State **Dst) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 640 | { |
| 641 | | setup 0; |
| 642 | |
| 643 | struct local local; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 644 | compile_expression(env, &local, val, Dst); |
| 645 | |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 646 | | cleanup; |
| 647 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 648 | add_function(env, name, link_program(Dst), 0, NS_FUNCTION); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 649 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 650 | |
| 651 | struct variable *add_variable(struct local *local, enum var_type type, |
| 652 | char *name, int number) |
| 653 | { |
| 654 | struct variable *var = malloc(sizeof(struct variable)); |
| 655 | var->prev = local->first; |
| 656 | var->type = type; |
| 657 | var->name = name; |
| 658 | var->number = number; |
| 659 | |
| 660 | local->first = var; |
| 661 | |
| 662 | return var; |
| 663 | } |
| 664 | |
| 665 | void destroy_local(struct local *local) |
| 666 | { |
| 667 | for (struct variable *v = local->first; v;) |
| 668 | { |
| 669 | struct variable *t = v; |
| 670 | v = v->prev; |
| 671 | free(t); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | struct variable *find_variable(struct local *local, char *name) |
| 676 | { |
| 677 | struct variable *v = local->first; |
| 678 | |
| 679 | for (; v && strcmp(v->name, name) != 0; v = v->prev) |
| 680 | {} |
| 681 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 682 | if (!v) |
| 683 | { |
| 684 | if (local->parent) |
| 685 | { |
| 686 | v = find_variable(local->parent, name); |
| 687 | |
| 688 | if (v) |
| 689 | { |
| 690 | // We found this in a parent scope, add it as a V_FREE variable to skip the search. |
| 691 | v = add_variable(local, V_FREE, name, local->num_closure_slots++); |
| 692 | } |
| 693 | } |
| 694 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 695 | return v; |
| 696 | } |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 697 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 698 | extern value_t _call_list(void *addr, value_t list, value_t *edi); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 699 | |
| 700 | value_t call_list(struct function *func, value_t list) |
| 701 | { |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 702 | return _call_list(func->code_ptr, list, NULL); |
| 703 | } |
| 704 | |
| 705 | value_t call_list_closure(struct closure *c, value_t list) |
| 706 | { |
| 707 | return _call_list(c->function, list, c->data); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 708 | } |