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