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