blob: 2ac21f37ff9894d8e46d5b7ebe199c938995ef4e [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
swissChili8fc5e2f2021-04-22 13:45:10 -070067 struct local local;
68 local.first = NULL;
69 local.num_vars = 0;
swissChilia820dea2021-05-09 16:46:55 -070070 local.npc = 8;
71 local.nextpc = 0;
72
73 dasm_growpc(&d, local.npc);
swissChili53472e82021-05-08 16:06:32 -070074
swissChili8fc5e2f2021-04-22 13:45:10 -070075 // Generate code
swissChili53472e82021-05-08 16:06:32 -070076
swissChili8fc5e2f2021-04-22 13:45:10 -070077 | setup 0;
swissChilif3e7f182021-04-20 13:57:22 -070078
swissChili53472e82021-05-08 16:06:32 -070079 value_t name = car(args);
80 args = cdr(args);
81 value_t arglist = car(args);
82 value_t body = cdr(args);
swissChili8fc5e2f2021-04-22 13:45:10 -070083
swissChili53472e82021-05-08 16:06:32 -070084 if ((name & HEAP_MASK) != SYMBOL_TAG)
85 err("function name must be a symbol");
86
87 for (; !nilp(body); body = cdr(body))
swissChili8fc5e2f2021-04-22 13:45:10 -070088 {
swissChili53472e82021-05-08 16:06:32 -070089 compile_expression(env, &local, car(body), Dst);
swissChili8fc5e2f2021-04-22 13:45:10 -070090 }
91
92 | cleanup;
93
swissChili53472e82021-05-08 16:06:32 -070094 add_function(env, (char *)(name ^ SYMBOL_TAG), link(Dst),
95 length(arglist));
swissChili8fc5e2f2021-04-22 13:45:10 -070096
swissChili53472e82021-05-08 16:06:32 -070097 dasm_free(&d);
swissChili8fc5e2f2021-04-22 13:45:10 -070098 }
99}
100
swissChili53472e82021-05-08 16:06:32 -0700101struct environment compile_all(struct istream *is)
swissChili8fc5e2f2021-04-22 13:45:10 -0700102{
103 value_t val;
swissChilif3e7f182021-04-20 13:57:22 -0700104 struct environment env;
105 env.first = NULL;
swissChili53472e82021-05-08 16:06:32 -0700106 load_std(&env);
107
108 while (read1(is, &val))
swissChili8fc5e2f2021-04-22 13:45:10 -0700109 {
swissChili53472e82021-05-08 16:06:32 -0700110 compile_tl(val, &env);
swissChili8fc5e2f2021-04-22 13:45:10 -0700111 }
swissChilif3e7f182021-04-20 13:57:22 -0700112
swissChili8fc5e2f2021-04-22 13:45:10 -0700113 return env;
swissChilica107a02021-04-14 12:07:30 -0700114}
swissChilib3ca4fb2021-04-20 10:33:00 -0700115
swissChili53472e82021-05-08 16:06:32 -0700116int nextpc(struct local *local, dasm_State **Dst)
swissChilib3ca4fb2021-04-20 10:33:00 -0700117{
swissChili53472e82021-05-08 16:06:32 -0700118 int n = local->nextpc++;
119 if (n > local->npc)
120 {
121 local->npc += 16;
122 dasm_growpc(Dst, local->npc);
123 }
124 return n;
125}
126
127void compile_expression(struct environment *env, struct local *local,
128 value_t val, dasm_State **Dst)
129{
130 if (symstreq(val, "nil"))
131 {
132 | mov eax, (nil);
133 }
134 else if (integerp(val) || stringp(val) || symbolp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700135 {
136 | mov eax, val;
137 }
swissChili53472e82021-05-08 16:06:32 -0700138 else if (listp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700139 {
swissChili53472e82021-05-08 16:06:32 -0700140 value_t fsym = car(val);
141 value_t args = cdr(val);
142 int nargs = length(args);
swissChilib3ca4fb2021-04-20 10:33:00 -0700143
swissChili53472e82021-05-08 16:06:32 -0700144 if (!symbolp(fsym))
swissChilif3e7f182021-04-20 13:57:22 -0700145 {
swissChili53472e82021-05-08 16:06:32 -0700146 err("function name must be a symbol");
swissChilif3e7f182021-04-20 13:57:22 -0700147 }
148
swissChili53472e82021-05-08 16:06:32 -0700149 if (symstreq(fsym, "if"))
swissChilib3ca4fb2021-04-20 10:33:00 -0700150 {
swissChili53472e82021-05-08 16:06:32 -0700151 if (nargs < 2 || nargs > 3)
152 err("Must give at least 2 arguments to if");
swissChilib3ca4fb2021-04-20 10:33:00 -0700153
swissChili53472e82021-05-08 16:06:32 -0700154 compile_expression(env, local, car(args), Dst);
155 int false_label = nextpc(local, Dst),
156 after_label = nextpc(local, Dst);
157
158 // result is in eax
159 | cmp eax, (nil);
160 | je =>false_label;
161
162 compile_expression(env, local, elt(args, 1), Dst);
swissChilia820dea2021-05-09 16:46:55 -0700163 | jmp =>after_label;
swissChili53472e82021-05-08 16:06:32 -0700164 |=>false_label:
165 if (nargs == 3)
166 compile_expression(env, local, elt(args, 2), Dst);
167 |=>after_label:
168 }
169 else
170 {
171 struct function *func =
172 find_function(env, (char *)(fsym ^ SYMBOL_TAG));
173
174 if (nargs != func->nargs)
175 err("wrong number of args");
176
177 for (int i = length(args) - 1; i >= 0; i--)
178 {
179 compile_expression(env, local, elt(args, i), Dst);
180 | push eax;
181 }
182
183 | mov ebx, (func->code_addr);
184 | call ebx;
185 | add esp, (nargs * 4);
186 // result in eax
187 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700188 }
189}
swissChilif3e7f182021-04-20 13:57:22 -0700190
swissChili53472e82021-05-08 16:06:32 -0700191void compile_expr_to_func(struct environment *env, char *name, value_t val,
192 dasm_State **Dst)
swissChilif3e7f182021-04-20 13:57:22 -0700193{
194 | setup 0;
195
196 struct local local;
swissChili53472e82021-05-08 16:06:32 -0700197 compile_expression(env, &local, val, Dst);
198
swissChilif3e7f182021-04-20 13:57:22 -0700199 | cleanup;
200
swissChili53472e82021-05-08 16:06:32 -0700201 add_function(env, name, link(Dst), 0);
swissChilif3e7f182021-04-20 13:57:22 -0700202}