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 "plat/plat.h" |
| 5 | #include "lib/std.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 | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 10 | #define value_size sizeof (value_t) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 11 | |
| 12 | |.arch x86; |
| 13 | |
| 14 | |.macro setup, nvars; |
| 15 | | push ebp; |
| 16 | | mov ebp, esp; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 17 | | sub esp, (value_size * nvars); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 18 | |.endmacro; |
| 19 | |
| 20 | |.macro cleanup; |
| 21 | | mov esp, ebp; |
| 22 | | pop ebp; |
| 23 | | ret; |
| 24 | |.endmacro; |
| 25 | |
| 26 | dasm_State *d; |
| 27 | unsigned int npc = 8; |
| 28 | |
| 29 | struct function *find_function (struct environment *env, char *name) |
| 30 | { |
| 31 | struct function *f = env->first; |
| 32 | |
| 33 | while ( strcmp (f->name, name) != 0 ) |
| 34 | { |
| 35 | if ( f->prev ) |
| 36 | f = f->prev; |
| 37 | else |
| 38 | return NULL; |
| 39 | } |
| 40 | |
| 41 | return f; |
| 42 | } |
| 43 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame^] | 44 | void compile_tl (value_t val, struct environment *env) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 45 | { |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame^] | 46 | if ( !listp (val) ) |
| 47 | err ("Top level must be a list"); |
| 48 | |
| 49 | value_t form = car (val); |
| 50 | value_t args = cdr (val); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 51 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame^] | 52 | if ( symstreq (form, "defun") ) |
| 53 | { |
| 54 | dasm_State *d; |
| 55 | dasm_State **Dst = &d; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 56 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame^] | 57 | |.section code; |
| 58 | dasm_init (&d, DASM_MAXSECTION); |
| 59 | |
| 60 | |.globals lbl_; |
| 61 | void *labels[ lbl__MAX ]; |
| 62 | dasm_setupglobal (&d, labels, lbl__MAX); |
| 63 | |
| 64 | |.actionlist lisp_actions; |
| 65 | dasm_setup (&d, lisp_actions); |
| 66 | |
| 67 | dasm_growpc (&d, npc); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 68 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame^] | 69 | struct local local; |
| 70 | local.first = NULL; |
| 71 | local.num_vars = 0; |
| 72 | |
| 73 | // Generate code |
| 74 | |
| 75 | | setup 0; |
| 76 | |
| 77 | value_t name = car (args); |
| 78 | args = cdr (args); |
| 79 | value_t arglist = car (args); |
| 80 | value_t body = cdr (args); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 81 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame^] | 82 | if ( (name & HEAP_MASK) != SYMBOL_TAG ) |
| 83 | err ("function name must be a symbol"); |
| 84 | |
| 85 | for ( ; !nilp (body); body = cdr (body) ) |
| 86 | { |
| 87 | compile_expression (env, &local, car (body), Dst); |
| 88 | } |
| 89 | |
| 90 | | cleanup; |
| 91 | |
| 92 | add_function (env, (char *) (name ^ SYMBOL_TAG), link (Dst), length (arglist)); |
| 93 | |
| 94 | dasm_free (&d); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | struct environment compile_all (struct istream *is) |
| 99 | { |
| 100 | value_t val; |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 101 | struct environment env; |
| 102 | env.first = NULL; |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 103 | load_std (&env); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame^] | 104 | |
| 105 | while ( read1 (is, &val) ) |
| 106 | { |
| 107 | compile_tl (val, &env); |
| 108 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 109 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame^] | 110 | return env; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 111 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 112 | |
| 113 | void compile_expression (struct environment *env, struct local *local, |
| 114 | value_t val, dasm_State **Dst) |
| 115 | { |
| 116 | if ( integerp (val) || stringp (val) || symbolp (val) ) |
| 117 | { |
| 118 | | mov eax, val; |
| 119 | } |
| 120 | else if ( listp (val) ) |
| 121 | { |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 122 | value_t fsym = car (val); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 123 | |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 124 | if ( !symbolp (fsym) ) |
| 125 | { |
| 126 | err ("function name must be a symbol"); |
| 127 | } |
| 128 | |
| 129 | struct function *func = find_function (env, (char *) (fsym ^ SYMBOL_TAG)); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 130 | value_t args = cdr (val); |
| 131 | int nargs = length (args); |
| 132 | |
| 133 | if ( nargs != func->nargs ) |
| 134 | err ("wrong number of args"); |
| 135 | |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 136 | for ( int i = length (args) - 1; i >= 0; i-- ) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 137 | { |
| 138 | compile_expression (env, local, elt (args, i), Dst); |
| 139 | | push eax; |
| 140 | } |
| 141 | |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 142 | | mov ebx, (func->code_addr); |
| 143 | | call ebx; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 144 | | add esp, (nargs * 4); |
| 145 | // result in eax |
| 146 | } |
| 147 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 148 | |
| 149 | void compile_expr_to_func (struct environment *env, char *name, value_t val, |
| 150 | dasm_State **Dst) |
| 151 | { |
| 152 | | setup 0; |
| 153 | |
| 154 | struct local local; |
| 155 | compile_expression (env, &local, val, Dst); |
| 156 | |
| 157 | | cleanup; |
| 158 | |
| 159 | add_function (env, name, link (Dst), 0); |
| 160 | } |