blob: e5a9444597acb4edc32d73fe47c92eba1abcec65 [file] [log] [blame]
swissChili8cfb7c42021-04-18 21:17:58 -07001/* -*- mode:c -*- */
2
swissChilica107a02021-04-14 12:07:30 -07003#include "compiler.h"
swissChilif3e7f182021-04-20 13:57:22 -07004#include "lib/std.h"
swissChili53472e82021-05-08 16:06:32 -07005#include "plat/plat.h"
swissChilica107a02021-04-14 12:07:30 -07006
7#include <dasm_proto.h>
8#include <dasm_x86.h>
9
swissChili53472e82021-05-08 16:06:32 -070010#define value_size sizeof(value_t)
swissChilica107a02021-04-14 12:07:30 -070011
12|.arch x86;
13
14|.macro setup, nvars;
15| push ebp;
16| mov ebp, esp;
swissChili8cfb7c42021-04-18 21:17:58 -070017| sub esp, (value_size * nvars);
swissChilica107a02021-04-14 12:07:30 -070018|.endmacro;
19
20|.macro cleanup;
21| mov esp, ebp;
22| pop ebp;
23| ret;
24|.endmacro;
25
26dasm_State *d;
27unsigned int npc = 8;
28
swissChili53472e82021-05-08 16:06:32 -070029struct function *find_function(struct environment *env, char *name)
swissChilica107a02021-04-14 12:07:30 -070030{
31 struct function *f = env->first;
32
swissChili53472e82021-05-08 16:06:32 -070033 while (strcmp(f->name, name) != 0)
swissChilica107a02021-04-14 12:07:30 -070034 {
swissChili53472e82021-05-08 16:06:32 -070035 if (f->prev)
swissChilica107a02021-04-14 12:07:30 -070036 f = f->prev;
37 else
38 return NULL;
39 }
40
41 return f;
42}
43
swissChili53472e82021-05-08 16:06:32 -070044void compile_tl(value_t val, struct environment *env)
swissChilica107a02021-04-14 12:07:30 -070045{
swissChili53472e82021-05-08 16:06:32 -070046 if (!listp(val))
47 err("Top level must be a list");
swissChilica107a02021-04-14 12:07:30 -070048
swissChili53472e82021-05-08 16:06:32 -070049 value_t form = car(val);
50 value_t args = cdr(val);
51
52 if (symstreq(form, "defun"))
swissChili8fc5e2f2021-04-22 13:45:10 -070053 {
54 dasm_State *d;
55 dasm_State **Dst = &d;
swissChilica107a02021-04-14 12:07:30 -070056
swissChili8fc5e2f2021-04-22 13:45:10 -070057 |.section code;
swissChili53472e82021-05-08 16:06:32 -070058 dasm_init(&d, DASM_MAXSECTION);
59
swissChili8fc5e2f2021-04-22 13:45:10 -070060 |.globals lbl_;
swissChili53472e82021-05-08 16:06:32 -070061 void *labels[lbl__MAX];
62 dasm_setupglobal(&d, labels, lbl__MAX);
63
swissChili8fc5e2f2021-04-22 13:45:10 -070064 |.actionlist lisp_actions;
swissChili53472e82021-05-08 16:06:32 -070065 dasm_setup(&d, lisp_actions);
66
67 dasm_growpc(&d, npc);
swissChilica107a02021-04-14 12:07:30 -070068
swissChili8fc5e2f2021-04-22 13:45:10 -070069 struct local local;
70 local.first = NULL;
71 local.num_vars = 0;
swissChili53472e82021-05-08 16:06:32 -070072
swissChili8fc5e2f2021-04-22 13:45:10 -070073 // Generate code
swissChili53472e82021-05-08 16:06:32 -070074
swissChili8fc5e2f2021-04-22 13:45:10 -070075 | setup 0;
swissChilif3e7f182021-04-20 13:57:22 -070076
swissChili53472e82021-05-08 16:06:32 -070077 value_t name = car(args);
78 args = cdr(args);
79 value_t arglist = car(args);
80 value_t body = cdr(args);
swissChili8fc5e2f2021-04-22 13:45:10 -070081
swissChili53472e82021-05-08 16:06:32 -070082 if ((name & HEAP_MASK) != SYMBOL_TAG)
83 err("function name must be a symbol");
84
85 for (; !nilp(body); body = cdr(body))
swissChili8fc5e2f2021-04-22 13:45:10 -070086 {
swissChili53472e82021-05-08 16:06:32 -070087 compile_expression(env, &local, car(body), Dst);
swissChili8fc5e2f2021-04-22 13:45:10 -070088 }
89
90 | cleanup;
91
swissChili53472e82021-05-08 16:06:32 -070092 add_function(env, (char *)(name ^ SYMBOL_TAG), link(Dst),
93 length(arglist));
swissChili8fc5e2f2021-04-22 13:45:10 -070094
swissChili53472e82021-05-08 16:06:32 -070095 dasm_free(&d);
swissChili8fc5e2f2021-04-22 13:45:10 -070096 }
97}
98
swissChili53472e82021-05-08 16:06:32 -070099struct environment compile_all(struct istream *is)
swissChili8fc5e2f2021-04-22 13:45:10 -0700100{
101 value_t val;
swissChilif3e7f182021-04-20 13:57:22 -0700102 struct environment env;
103 env.first = NULL;
swissChili53472e82021-05-08 16:06:32 -0700104 load_std(&env);
105
106 while (read1(is, &val))
swissChili8fc5e2f2021-04-22 13:45:10 -0700107 {
swissChili53472e82021-05-08 16:06:32 -0700108 compile_tl(val, &env);
swissChili8fc5e2f2021-04-22 13:45:10 -0700109 }
swissChilif3e7f182021-04-20 13:57:22 -0700110
swissChili8fc5e2f2021-04-22 13:45:10 -0700111 return env;
swissChilica107a02021-04-14 12:07:30 -0700112}
swissChilib3ca4fb2021-04-20 10:33:00 -0700113
swissChili53472e82021-05-08 16:06:32 -0700114int nextpc(struct local *local, dasm_State **Dst)
swissChilib3ca4fb2021-04-20 10:33:00 -0700115{
swissChili53472e82021-05-08 16:06:32 -0700116 int n = local->nextpc++;
117 if (n > local->npc)
118 {
119 local->npc += 16;
120 dasm_growpc(Dst, local->npc);
121 }
122 return n;
123}
124
125void compile_expression(struct environment *env, struct local *local,
126 value_t val, dasm_State **Dst)
127{
128 if (symstreq(val, "nil"))
129 {
130 | mov eax, (nil);
131 }
132 else if (integerp(val) || stringp(val) || symbolp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700133 {
134 | mov eax, val;
135 }
swissChili53472e82021-05-08 16:06:32 -0700136 else if (listp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700137 {
swissChili53472e82021-05-08 16:06:32 -0700138 value_t fsym = car(val);
139 value_t args = cdr(val);
140 int nargs = length(args);
swissChilib3ca4fb2021-04-20 10:33:00 -0700141
swissChili53472e82021-05-08 16:06:32 -0700142 if (!symbolp(fsym))
swissChilif3e7f182021-04-20 13:57:22 -0700143 {
swissChili53472e82021-05-08 16:06:32 -0700144 err("function name must be a symbol");
swissChilif3e7f182021-04-20 13:57:22 -0700145 }
146
swissChili53472e82021-05-08 16:06:32 -0700147 if (symstreq(fsym, "if"))
swissChilib3ca4fb2021-04-20 10:33:00 -0700148 {
swissChili53472e82021-05-08 16:06:32 -0700149 if (nargs < 2 || nargs > 3)
150 err("Must give at least 2 arguments to if");
swissChilib3ca4fb2021-04-20 10:33:00 -0700151
swissChili53472e82021-05-08 16:06:32 -0700152 compile_expression(env, local, car(args), Dst);
153 int false_label = nextpc(local, Dst),
154 after_label = nextpc(local, Dst);
155
156 // result is in eax
157 | cmp eax, (nil);
158 | je =>false_label;
159
160 compile_expression(env, local, elt(args, 1), Dst);
161
162 |=>false_label:
163 if (nargs == 3)
164 compile_expression(env, local, elt(args, 2), Dst);
165 |=>after_label:
166 }
167 else
168 {
169 struct function *func =
170 find_function(env, (char *)(fsym ^ SYMBOL_TAG));
171
172 if (nargs != func->nargs)
173 err("wrong number of args");
174
175 for (int i = length(args) - 1; i >= 0; i--)
176 {
177 compile_expression(env, local, elt(args, i), Dst);
178 | push eax;
179 }
180
181 | mov ebx, (func->code_addr);
182 | call ebx;
183 | add esp, (nargs * 4);
184 // result in eax
185 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700186 }
187}
swissChilif3e7f182021-04-20 13:57:22 -0700188
swissChili53472e82021-05-08 16:06:32 -0700189void compile_expr_to_func(struct environment *env, char *name, value_t val,
190 dasm_State **Dst)
swissChilif3e7f182021-04-20 13:57:22 -0700191{
192 | setup 0;
193
194 struct local local;
swissChili53472e82021-05-08 16:06:32 -0700195 compile_expression(env, &local, val, Dst);
196
swissChilif3e7f182021-04-20 13:57:22 -0700197 | cleanup;
198
swissChili53472e82021-05-08 16:06:32 -0700199 add_function(env, name, link(Dst), 0);
swissChilif3e7f182021-04-20 13:57:22 -0700200}