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 | |
| 29 | dasm_State *d; |
| 30 | unsigned int npc = 8; |
| 31 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 32 | struct function *find_function(struct environment *env, char *name) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 33 | { |
| 34 | struct function *f = env->first; |
| 35 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 36 | while (strcmp(f->name, name) != 0) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 37 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 38 | if (f->prev) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 39 | f = f->prev; |
| 40 | else |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | return f; |
| 45 | } |
| 46 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 47 | void compile_tl(value_t val, struct environment *env) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 48 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 49 | if (!listp(val)) |
| 50 | err("Top level must be a list"); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 51 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 52 | value_t form = car(val); |
| 53 | value_t args = cdr(val); |
| 54 | |
| 55 | if (symstreq(form, "defun")) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 56 | { |
| 57 | dasm_State *d; |
| 58 | dasm_State **Dst = &d; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 59 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 60 | |.section code; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 61 | dasm_init(&d, DASM_MAXSECTION); |
| 62 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 63 | |.globals lbl_; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 64 | void *labels[lbl__MAX]; |
| 65 | dasm_setupglobal(&d, labels, lbl__MAX); |
| 66 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 67 | |.actionlist lisp_actions; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 68 | dasm_setup(&d, lisp_actions); |
| 69 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 70 | struct local local; |
| 71 | local.first = NULL; |
| 72 | local.num_vars = 0; |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 73 | local.npc = 8; |
| 74 | local.nextpc = 0; |
| 75 | |
| 76 | dasm_growpc(&d, local.npc); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 77 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 78 | // Generate code |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 79 | // TODO: first pass, extract bound and free variables |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 80 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 81 | | setup 0; |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 82 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 83 | value_t name = car(args); |
| 84 | args = cdr(args); |
| 85 | value_t arglist = car(args); |
| 86 | value_t body = cdr(args); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 87 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 88 | if ((name & HEAP_MASK) != SYMBOL_TAG) |
| 89 | err("function name must be a symbol"); |
| 90 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 91 | value_t a = arglist; |
| 92 | for (int i = 0; !nilp(a); a = cdr(a), i++) |
| 93 | { |
| 94 | if (!symbolp(car(a))) |
| 95 | { |
| 96 | err("defun argument must be a symbol"); |
| 97 | } |
| 98 | |
| 99 | add_variable(&local, V_ARGUMENT, (char *)(car(a) ^ SYMBOL_TAG), i); |
| 100 | } |
| 101 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 102 | for (; !nilp(body); body = cdr(body)) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 103 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 104 | compile_expression(env, &local, car(body), Dst); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | | cleanup; |
| 108 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 109 | add_function(env, (char *)(name ^ SYMBOL_TAG), link(Dst), |
| 110 | length(arglist)); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 111 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 112 | dasm_free(&d); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 116 | struct environment compile_all(struct istream *is) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 117 | { |
| 118 | value_t val; |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 119 | struct environment env; |
| 120 | env.first = NULL; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 121 | load_std(&env); |
| 122 | |
| 123 | while (read1(is, &val)) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 124 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 125 | compile_tl(val, &env); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 126 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 127 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 128 | return env; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 129 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 130 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 131 | int nextpc(struct local *local, dasm_State **Dst) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 132 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 133 | int n = local->nextpc++; |
| 134 | if (n > local->npc) |
| 135 | { |
| 136 | local->npc += 16; |
| 137 | dasm_growpc(Dst, local->npc); |
| 138 | } |
| 139 | return n; |
| 140 | } |
| 141 | |
| 142 | void compile_expression(struct environment *env, struct local *local, |
| 143 | value_t val, dasm_State **Dst) |
| 144 | { |
| 145 | if (symstreq(val, "nil")) |
| 146 | { |
| 147 | | mov eax, (nil); |
| 148 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 149 | else if (symstreq(val, "t")) |
| 150 | { |
| 151 | | mov eax, (t); |
| 152 | } |
| 153 | else if (integerp(val) || stringp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 154 | { |
| 155 | | mov eax, val; |
| 156 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 157 | else if (listp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 158 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 159 | value_t fsym = car(val); |
| 160 | value_t args = cdr(val); |
| 161 | int nargs = length(args); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 162 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 163 | if (!symbolp(fsym)) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 164 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 165 | err("function name must be a symbol"); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 166 | } |
| 167 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 168 | if (symstreq(fsym, "if")) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 169 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 170 | if (nargs < 2 || nargs > 3) |
| 171 | err("Must give at least 2 arguments to if"); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 172 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 173 | compile_expression(env, local, car(args), Dst); |
| 174 | int false_label = nextpc(local, Dst), |
| 175 | after_label = nextpc(local, Dst); |
| 176 | |
| 177 | // result is in eax |
| 178 | | cmp eax, (nil); |
| 179 | | je =>false_label; |
| 180 | |
| 181 | compile_expression(env, local, elt(args, 1), Dst); |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 182 | | jmp =>after_label; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 183 | |=>false_label:; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 184 | if (nargs == 3) |
| 185 | compile_expression(env, local, elt(args, 2), Dst); |
| 186 | |=>after_label: |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | struct function *func = |
| 191 | find_function(env, (char *)(fsym ^ SYMBOL_TAG)); |
| 192 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 193 | if (func == NULL) |
| 194 | err("Function undefined"); |
| 195 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 196 | if (nargs != func->nargs) |
| 197 | err("wrong number of args"); |
| 198 | |
| 199 | for (int i = length(args) - 1; i >= 0; i--) |
| 200 | { |
| 201 | compile_expression(env, local, elt(args, i), Dst); |
| 202 | | push eax; |
| 203 | } |
| 204 | |
| 205 | | mov ebx, (func->code_addr); |
| 206 | | call ebx; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 207 | | add esp, (nargs * value_size); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 208 | // result in eax |
| 209 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 210 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 211 | else if (symbolp(val)) |
| 212 | { |
| 213 | // For now ignore global variables, only search locally |
| 214 | struct variable *v = find_variable(local, (char *)(val ^ SYMBOL_TAG)); |
| 215 | |
| 216 | if (!v) |
| 217 | err("Variable unbound"); |
| 218 | |
| 219 | switch (v->type) |
| 220 | { |
| 221 | case V_ARGUMENT: |
| 222 | | mov eax, dword [ebp + value_size * (v->number + 2)]; |
| 223 | break; |
| 224 | default: |
| 225 | err("Sorry, can only access V_ARGUMENT variables for now :("); |
| 226 | } |
| 227 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 228 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 229 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 230 | void compile_expr_to_func(struct environment *env, char *name, value_t val, |
| 231 | dasm_State **Dst) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 232 | { |
| 233 | | setup 0; |
| 234 | |
| 235 | struct local local; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 236 | compile_expression(env, &local, val, Dst); |
| 237 | |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 238 | | cleanup; |
| 239 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 240 | add_function(env, name, link(Dst), 0); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 241 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 242 | |
| 243 | struct variable *add_variable(struct local *local, enum var_type type, |
| 244 | char *name, int number) |
| 245 | { |
| 246 | struct variable *var = malloc(sizeof(struct variable)); |
| 247 | var->prev = local->first; |
| 248 | var->type = type; |
| 249 | var->name = name; |
| 250 | var->number = number; |
| 251 | |
| 252 | local->first = var; |
| 253 | |
| 254 | return var; |
| 255 | } |
| 256 | |
| 257 | void destroy_local(struct local *local) |
| 258 | { |
| 259 | for (struct variable *v = local->first; v;) |
| 260 | { |
| 261 | struct variable *t = v; |
| 262 | v = v->prev; |
| 263 | free(t); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | struct variable *find_variable(struct local *local, char *name) |
| 268 | { |
| 269 | struct variable *v = local->first; |
| 270 | |
| 271 | for (; v && strcmp(v->name, name) != 0; v = v->prev) |
| 272 | {} |
| 273 | |
| 274 | return v; |
| 275 | } |