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 | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 6 | |
| 7 | #include <dasm_proto.h> |
| 8 | #include <dasm_x86.h> |
| 9 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 13 | #define value_size sizeof(value_t) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 14 | |
| 15 | |.arch x86; |
| 16 | |
| 17 | |.macro setup, nvars; |
| 18 | | push ebp; |
| 19 | | mov ebp, esp; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 20 | | sub esp, (value_size * nvars); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 21 | |.endmacro; |
| 22 | |
| 23 | |.macro cleanup; |
| 24 | | mov esp, ebp; |
| 25 | | pop ebp; |
| 26 | | ret; |
| 27 | |.endmacro; |
| 28 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 29 | |.macro local_var, index; |
| 30 | |.endmacro; |
| 31 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 32 | dasm_State *d; |
| 33 | unsigned int npc = 8; |
| 34 | |
swissChili | 6d6525e | 2021-06-15 21:20:53 -0700 | [diff] [blame] | 35 | extern void _do_gc(unsigned int ebp, unsigned int esp); |
| 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 | { |
| 47 | struct function *f = env->first; |
| 48 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 49 | while (strcmp(f->name, name) != 0) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 50 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 51 | if (f->prev) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 52 | f = f->prev; |
| 53 | else |
| 54 | return NULL; |
| 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; |
| 77 | local->stack_slots = realloc(local->stack_slots, local->num_stack_slots * sizeof(bool)); |
| 78 | // unreadable: set the remaining slots to unused |
| 79 | memset(local->stack_slots + old_size, 0, local->num_stack_slots - old_size); |
| 80 | local->stack_slots[old_size] = true; |
| 81 | |
| 82 | return old_size; |
| 83 | } |
| 84 | |
| 85 | void local_free(struct local *local, unsigned int slot) |
| 86 | { |
| 87 | local->stack_slots[slot] = false; |
| 88 | } |
| 89 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 90 | struct dasm_State *compile_function(value_t args, enum namespace namespace, |
| 91 | struct environment *env, |
| 92 | struct local *local_out, int *nargs) |
| 93 | { |
| 94 | dasm_State *d; |
| 95 | dasm_State **Dst = &d; |
| 96 | |
| 97 | |.section code; |
| 98 | dasm_init(&d, DASM_MAXSECTION); |
| 99 | |
| 100 | |.globals lbl_; |
| 101 | void *labels[lbl__MAX]; |
| 102 | dasm_setupglobal(&d, labels, lbl__MAX); |
| 103 | |
| 104 | |.actionlist lisp_actions; |
| 105 | dasm_setup(&d, lisp_actions); |
| 106 | |
| 107 | struct local local; |
| 108 | local.parent = NULL; |
| 109 | local.first = NULL; |
| 110 | local.num_vars = 0; |
| 111 | local.npc = 8; |
| 112 | local.nextpc = 0; |
| 113 | local.stack_slots = malloc(sizeof(bool) * 4); |
| 114 | memset(local.stack_slots, 0, sizeof(bool) * 4); |
| 115 | local.num_stack_slots = 4; |
| 116 | local.num_stack_entries = 0; |
| 117 | |
| 118 | dasm_growpc(&d, local.npc); |
| 119 | |
| 120 | // Generate code |
| 121 | // TODO: first pass, extract bound and free variables |
| 122 | |
| 123 | value_t name = car(args); |
| 124 | args = cdr(args); |
| 125 | value_t arglist = car(args); |
| 126 | value_t body = cdr(args); |
| 127 | |
| 128 | if ((name & HEAP_MASK) != SYMBOL_TAG) |
| 129 | err("function name must be a symbol"); |
| 130 | |
| 131 | value_t a = arglist; |
| 132 | for (int i = 0; !nilp(a); a = cdr(a), i++) |
| 133 | { |
| 134 | if (!symbolp(car(a))) |
| 135 | { |
| 136 | err("defun argument must be a symbol"); |
| 137 | } |
| 138 | |
| 139 | add_variable(&local, V_ARGUMENT, (char *)(car(a) ^ SYMBOL_TAG), i); |
| 140 | } |
| 141 | |
| 142 | for (value_t body_ = body; !nilp(body_); body_ = cdr(body_)) |
| 143 | { |
| 144 | walk_and_alloc(&local, car(body_)); |
| 145 | } |
| 146 | |
| 147 | | setup (local.num_stack_entries); |
| 148 | |
| 149 | memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool)); |
| 150 | local.num_stack_entries = 0; |
| 151 | |
| 152 | for (; !nilp(body); body = cdr(body)) |
| 153 | { |
| 154 | compile_expression(env, &local, car(body), Dst); |
| 155 | } |
| 156 | |
| 157 | | cleanup; |
| 158 | |
| 159 | if (local_out) |
| 160 | *local_out = local; |
| 161 | |
| 162 | if (nargs) |
| 163 | *nargs = length(arglist); |
| 164 | |
| 165 | return d; |
| 166 | } |
| 167 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 168 | void compile_tl(value_t val, struct environment *env) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 169 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 170 | if (!listp(val)) |
| 171 | err("Top level must be a list"); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 172 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 173 | value_t form = car(val); |
| 174 | value_t args = cdr(val); |
| 175 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 176 | printf("Compiling function %s in %s\n", (char *)(car(args) ^ SYMBOL_TAG), (char *)(form ^ SYMBOL_TAG)); |
| 177 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 178 | if (symstreq(form, "defun") || symstreq(form, "defmacro")) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 179 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 180 | enum namespace namespace = NS_FUNCTION; |
| 181 | |
| 182 | if (symstreq(form, "defmacro")) |
| 183 | namespace = NS_MACRO; |
| 184 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 185 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 186 | struct local local; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 187 | int nargs; |
| 188 | dasm_State *d = compile_function(args, namespace, env, &local, &nargs); |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 189 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 190 | add_function(env, (char *)(car(args) ^ SYMBOL_TAG), link(&d), |
| 191 | nargs, namespace); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 192 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 193 | dasm_free(&d); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 194 | free(local.stack_slots); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void walk_and_alloc(struct local *local, value_t body) |
| 199 | { |
| 200 | if (!listp(body)) |
| 201 | return; |
| 202 | |
| 203 | value_t args = cdr(body); |
| 204 | |
| 205 | if (symstreq(car(body), "let1")) |
| 206 | { |
| 207 | int slot = local_alloc(local); |
| 208 | |
| 209 | value_t expr = cdr(args); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 210 | for (; !nilp(expr); expr = cdr(expr)) |
| 211 | { |
| 212 | walk_and_alloc(local, expr); |
| 213 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 214 | |
| 215 | local_free(local, slot); |
| 216 | } |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 217 | else if (symstreq(car(body), "lambda")) |
| 218 | { |
| 219 | // We don't want to walk the lambda because it's another function. When |
| 220 | // the lambda is compiled it will be walked. |
| 221 | return; |
| 222 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 223 | else |
| 224 | { |
| 225 | for (; !nilp(args); args = cdr(args)) |
| 226 | { |
| 227 | walk_and_alloc(local, car(args)); |
| 228 | } |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 232 | struct environment compile_all(struct istream *is) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 233 | { |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 234 | unsigned char pool = make_pool(); |
| 235 | unsigned char pop = push_pool(pool); |
| 236 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 237 | value_t val; |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 238 | struct environment env; |
| 239 | env.first = NULL; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 240 | load_std(&env); |
| 241 | |
| 242 | while (read1(is, &val)) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 243 | { |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 244 | printval(val, 0); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 245 | compile_tl(val, &env); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 246 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 247 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 248 | pop_pool(pop); |
| 249 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 250 | return env; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 251 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 252 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 253 | int nextpc(struct local *local, dasm_State **Dst) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 254 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 255 | int n = local->nextpc++; |
| 256 | if (n > local->npc) |
| 257 | { |
| 258 | local->npc += 16; |
| 259 | dasm_growpc(Dst, local->npc); |
| 260 | } |
| 261 | return n; |
| 262 | } |
| 263 | |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 264 | void compile_backquote(struct environment *env, struct local *local, |
| 265 | value_t val, dasm_State **Dst) |
| 266 | { |
| 267 | if (!listp(val)) |
| 268 | { |
| 269 | | mov eax, (val); |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | value_t fsym = car(val), |
| 274 | args = cdr(val); |
| 275 | int nargs = length(args); |
| 276 | |
| 277 | // TODO |
| 278 | } |
| 279 | } |
| 280 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 281 | void compile_expression(struct environment *env, struct local *local, |
| 282 | value_t val, dasm_State **Dst) |
| 283 | { |
| 284 | if (symstreq(val, "nil")) |
| 285 | { |
| 286 | | mov eax, (nil); |
| 287 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 288 | else if (symstreq(val, "t")) |
| 289 | { |
| 290 | | mov eax, (t); |
| 291 | } |
| 292 | else if (integerp(val) || stringp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 293 | { |
| 294 | | mov eax, val; |
| 295 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 296 | else if (listp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 297 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 298 | value_t fsym = car(val); |
| 299 | value_t args = cdr(val); |
| 300 | int nargs = length(args); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 301 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 302 | if (!symbolp(fsym)) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 303 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 304 | err("function name must be a symbol"); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 305 | } |
| 306 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 307 | if (symstreq(fsym, "if")) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 308 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 309 | if (nargs < 2 || nargs > 3) |
| 310 | err("Must give at least 2 arguments to if"); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 311 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 312 | compile_expression(env, local, car(args), Dst); |
| 313 | int false_label = nextpc(local, Dst), |
| 314 | after_label = nextpc(local, Dst); |
| 315 | |
| 316 | // result is in eax |
| 317 | | cmp eax, (nil); |
| 318 | | je =>false_label; |
| 319 | |
| 320 | compile_expression(env, local, elt(args, 1), Dst); |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 321 | | jmp =>after_label; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 322 | |=>false_label:; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 323 | if (nargs == 3) |
| 324 | compile_expression(env, local, elt(args, 2), Dst); |
| 325 | |=>after_label: |
| 326 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 327 | else if (symstreq(fsym, "let1")) |
| 328 | { |
| 329 | if (nargs < 2) |
| 330 | { |
| 331 | err("Must give at least 2 arguments to let1"); |
| 332 | } |
| 333 | value_t binding = car(args); |
| 334 | value_t rest = cdr(args); |
| 335 | |
| 336 | if (length(binding) != 2) |
| 337 | { |
| 338 | err("Binding list in let1 must contain exactly two entries"); |
| 339 | } |
| 340 | |
| 341 | value_t name = car(binding); |
| 342 | value_t value = car(cdr(binding)); |
| 343 | |
| 344 | compile_expression(env, local, value, Dst); |
| 345 | |
| 346 | int i = local_alloc(local); |
| 347 | |
| 348 | add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i); |
| 349 | |
| 350 | | mov dword [ebp - ((i + 1) * value_size)], eax; |
| 351 | |
| 352 | for (; !nilp(rest); rest = cdr(rest)) |
| 353 | { |
| 354 | compile_expression(env, local, car(rest), Dst); |
| 355 | } |
| 356 | |
| 357 | local_free(local, i); |
| 358 | } |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 359 | else if (symstreq(fsym, "gc")) |
| 360 | { |
| 361 | if (nargs) |
| 362 | { |
| 363 | err("gc takes no arguments"); |
| 364 | } |
| 365 | |
| 366 | | run_gc; |
| 367 | } |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 368 | else if (symstreq(fsym, "quote")) |
| 369 | { |
| 370 | if (nargs != 1) |
| 371 | err("quote should take exactly 1 argument"); |
| 372 | |
| 373 | // Simple! |
| 374 | | mov eax, (car(args)); |
| 375 | } |
| 376 | else if (symstreq(fsym, "backquote")) |
| 377 | { |
| 378 | if (nargs != 1) |
| 379 | err("backquote should take exactly 1 argument"); |
| 380 | |
| 381 | compile_backquote(env, local, car(args), Dst); |
| 382 | } |
| 383 | else if (symstreq(fsym, "list")) |
| 384 | { |
| 385 | | push (nil); |
| 386 | |
| 387 | for (int i = nargs - 1; i >= 0; i--) |
| 388 | { |
| 389 | compile_expression(env, local, elt(args, i), Dst); |
| 390 | |
| 391 | // push the ith item |
| 392 | | push eax; |
| 393 | // cons the top two stack items |
| 394 | | mov ebx, (cons); |
| 395 | | call ebx; |
| 396 | // remove the stack items from use |
| 397 | | add esp, (2 * value_size); |
| 398 | // put the new thing on the stack |
| 399 | | push eax; |
| 400 | } |
| 401 | |
| 402 | | pop eax; |
| 403 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 404 | else |
| 405 | { |
| 406 | struct function *func = |
| 407 | find_function(env, (char *)(fsym ^ SYMBOL_TAG)); |
| 408 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 409 | if (func == NULL) |
| 410 | err("Function undefined"); |
| 411 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 412 | if (nargs != func->nargs) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 413 | { |
| 414 | fprintf(stderr, "Function: %s at %s:%d\n", func->name, cons_file(val), cons_line(val)); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 415 | err("wrong number of args"); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 416 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 417 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 418 | if (func->namespace == NS_FUNCTION) |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 419 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 420 | for (int i = length(args) - 1; i >= 0; i--) |
| 421 | { |
| 422 | compile_expression(env, local, elt(args, i), Dst); |
| 423 | | push eax; |
| 424 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 425 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 426 | | mov ebx, (func->code_addr); |
| 427 | | call ebx; |
| 428 | | add esp, (nargs * value_size); |
| 429 | // result in eax |
| 430 | } |
| 431 | else if (func->namespace == NS_MACRO) |
| 432 | { |
| 433 | value_t expanded_to = call_list(func, args); |
| 434 | |
| 435 | printf("Macro expanded to:\n"); |
| 436 | printval(expanded_to, 2); |
| 437 | |
| 438 | compile_expression(env, local, expanded_to, Dst); |
| 439 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 440 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 441 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 442 | else if (symbolp(val)) |
| 443 | { |
| 444 | // For now ignore global variables, only search locally |
| 445 | struct variable *v = find_variable(local, (char *)(val ^ SYMBOL_TAG)); |
| 446 | |
| 447 | if (!v) |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 448 | { |
| 449 | fprintf(stderr, "var: %s\n", (char *)(val ^ SYMBOL_TAG)); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 450 | err("Variable unbound"); |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 451 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 452 | |
| 453 | switch (v->type) |
| 454 | { |
| 455 | case V_ARGUMENT: |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 456 | | mov eax, dword [ebp + (value_size * (v->number + 2))]; |
| 457 | break; |
| 458 | case V_BOUND: |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 459 | | mov eax, dword [ebp - ((v->number + 1) * value_size)]; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 460 | break; |
| 461 | default: |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 462 | err("Sorry, can only access V_ARGUMENT and V_BOUND variables for now :("); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 463 | } |
| 464 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 465 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 466 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 467 | void compile_expr_to_func(struct environment *env, char *name, value_t val, |
| 468 | dasm_State **Dst) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 469 | { |
| 470 | | setup 0; |
| 471 | |
| 472 | struct local local; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 473 | compile_expression(env, &local, val, Dst); |
| 474 | |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 475 | | cleanup; |
| 476 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 477 | add_function(env, name, link(Dst), 0, NS_FUNCTION); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 478 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 479 | |
| 480 | struct variable *add_variable(struct local *local, enum var_type type, |
| 481 | char *name, int number) |
| 482 | { |
| 483 | struct variable *var = malloc(sizeof(struct variable)); |
| 484 | var->prev = local->first; |
| 485 | var->type = type; |
| 486 | var->name = name; |
| 487 | var->number = number; |
| 488 | |
| 489 | local->first = var; |
| 490 | |
| 491 | return var; |
| 492 | } |
| 493 | |
| 494 | void destroy_local(struct local *local) |
| 495 | { |
| 496 | for (struct variable *v = local->first; v;) |
| 497 | { |
| 498 | struct variable *t = v; |
| 499 | v = v->prev; |
| 500 | free(t); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | struct variable *find_variable(struct local *local, char *name) |
| 505 | { |
| 506 | struct variable *v = local->first; |
| 507 | |
| 508 | for (; v && strcmp(v->name, name) != 0; v = v->prev) |
| 509 | {} |
| 510 | |
| 511 | return v; |
| 512 | } |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 513 | |
| 514 | extern value_t _call_list(void *addr, value_t list); |
| 515 | |
| 516 | value_t call_list(struct function *func, value_t list) |
| 517 | { |
| 518 | return _call_list(func->code_ptr, list); |
| 519 | } |