-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathir.cpp
405 lines (300 loc) · 9.58 KB
/
ir.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "ir.hpp"
#include "ast.hpp"
#include "tool.hpp"
#include <algorithm>
#include "sexpr.hpp"
namespace ir {
closure::closure(std::size_t argc, vector<expr> captures, block body)
: argc(argc),
captures(captures),
body(body) { }
struct state {
const state* parent = nullptr;
using locals_type = std::map<symbol, local>;
locals_type locals;
std::map<symbol, capture> captures;
state& def(symbol name);
expr find(symbol name);
// currently defined value, if any
const symbol* self = nullptr;
struct scope {
state* owner;
locals_type locals;
scope(state* owner):
owner(owner),
locals(owner->locals) { }
~scope() {
owner->locals = std::move(locals);
}
};
};
state& state::def(symbol name) {
auto info = locals.emplace(name, locals.size());
(void) info; assert(info.second);
return *this;
}
expr state::find(symbol name) {
// try locals first
auto loc = locals.find(name);
if(loc != locals.end()) return loc->second;
// then try captures
auto cap = captures.find(name);
if(cap != captures.end()) return cap->second;
// add capture
if(parent) {
return captures.emplace(name, captures.size()).first->second;
} else {
return global{name};
}
}
////////////////////////////////////////////////////////////////////////////////
static expr compile(state* ctx, ast::expr self);
template<class T>
static expr compile(state* ctx, const T& self) {
throw std::runtime_error("compile not implemented for: "
+ tool::type_name(typeid(T)));
};
template<class T>
static expr compile(state* ctx, ast::lit<T> self) {
return lit<T>{self.value};
}
static expr compile(state* ctx, ast::var self) {
return ctx->find(self.name);
}
static expr compile(state* ctx, ast::let self) {
const state::scope backup(ctx);
// allocate space for variables
for(ast::bind def : self.defs) {
ctx->def(def.id.name);
}
// push defined values
vector<expr> items;
for(ast::bind def : self.defs) {
// TODO exception safety
ctx->self = &def.id.name;
ir::expr value = compile(ctx, def.value);
ctx->self = nullptr;
items.emplace_back(std::move(value));
}
const std::size_t locals = items.size();
// compile let body
items.emplace_back(compile(ctx, *self.body));
items.emplace_back(exit{locals});
return block{std::move(items)};
}
static expr compile(state* ctx, ast::abs self) {
state sub = {ctx};
// allocate stack for function arguments
for(auto arg : self.args) {
sub.def(arg.name());
}
// compile function body
expr body = compile(&sub, *self.body);
// order captures by index
std::vector<std::pair<symbol, capture>> ordered = {sub.captures.begin(),
sub.captures.end()};
std::sort(ordered.begin(), ordered.end(), [](auto lhs, auto rhs) {
return lhs.second.index < rhs.second.index;
});
// define function captures
vector<expr> captures;
for(auto cap : ordered) {
captures.emplace_back(ctx->find(cap.first));
}
// body block
vector<expr> items;
items.emplace_back(body);
return make_ref<closure>(size(self.args), captures, block{std::move(items)});
}
static expr compile(state* ctx, ast::app self) {
return self.func->match([&](const ast::expr& func) -> expr {
vector<expr> items;
// push func
items.emplace_back(compile(ctx, func));
// push args
std::size_t argc = 0;
for(ast::expr arg : self.args) {
items.emplace_back(compile(ctx, arg));
++argc;
};
// call
items.emplace_back(call{argc});
return block{std::move(items)};
},
[&](const ast::sel& func) -> expr {
assert(size(self.args) == 1);
vector<expr> items;
items.emplace_back(compile(ctx, self.args->head));
items.emplace_back(sel{func.id.name});
return block{std::move(items)};
},
[&](const ast::match& func) -> expr {
std::map<symbol, expr> cases;
for(ast::match::handler h: func.cases) {
const state::scope backup(ctx);
// reserve stack for matched variable
ctx->def(h.arg.name());
expr c = compile(ctx, h.value);
cases.emplace(h.id.name, c);
}
return make_ref<match>(std::move(cases), compile(ctx, func.fallback));
});
}
static expr compile(state* ctx, ast::cond self) {
vector<expr> items;
items.emplace_back(compile(ctx, *self.test));
items.emplace_back(make_ref<branch>(compile(ctx, *self.conseq),
compile(ctx, *self.alt)));
return block{items};
}
static expr compile(state* ctx, ast::import self) {
// TODO differentiate between topleve/local imports
if(ctx->parent) {
throw std::runtime_error("unimplemented: non-toplevel import");
} else {
vector<expr> items;
items.emplace_back(import{self.package});
items.emplace_back(def{self.package});
return block{items};
}
}
static expr compile(state* ctx, ast::def self) {
if(ctx->parent) {
throw std::runtime_error("unimplemented: non-toplevel def");
} else {
vector<expr> items;
items.emplace_back(compile(ctx, *self.value));
items.emplace_back(def{self.id.name});
return block{items};
}
}
static expr compile(state* ctx, ast::use self) {
if(ctx->parent) {
throw std::runtime_error("unimplemented: non-toplevel use");
// TODO get help from the type system?
} else {
return make_ref<use>(compile(ctx, *self.env));
}
}
static expr compile(state* ctx, ast::record self) {
vector<expr> items;
vector<symbol> attrs;
for(const ast::record::attr& attr: self.attrs) {
attrs.emplace_back(attr.id.name);
items.emplace_back(compile(ctx, attr.value));
}
items.emplace_back(record{std::move(attrs)});
return block{std::move(items)};
}
static expr compile(state* ctx, ast::match self) {
throw std::runtime_error("unimplemented: naked match");
}
////////////////////////////////////////////////////////////////////////////////
static expr compile(state* ctx, ast::expr self) {
return self.match([&](auto self) {
return compile(ctx, self);
});
};
expr compile(const ast::expr& self) {
state ctx;
return compile(&ctx, self);
}
struct repr_visitor {
template<class T>
sexpr operator()(const T& self) const {
string res;
res += "#<" + tool::type_name(typeid(T)) + ">";
return res;
}
template<class T>
sexpr operator()(const lit<T>& self) const { return self.value; }
sexpr operator()(const lit<unit>& self) const { return sexpr::list(); }
sexpr operator()(const drop& self) const {
return symbol("drop")
>>= sexpr::list();
}
sexpr operator()(const block& self) const {
return symbol("block")
>>= foldr(sexpr::list(), self.items, [&](sexpr::list rhs, ir::expr lhs) {
return repr(lhs) >>= rhs;
});
}
sexpr operator()(const exit& self) const {
return symbol("exit")
>>= integer(self.locals)
>>= sexpr::list();
}
sexpr operator()(const ref<closure>& self) const {
return symbol("closure")
>>= integer(self->argc)
>>= foldr(sexpr::list(), self->captures,
[&](sexpr::list tail, ir::expr head) {
return repr(head) >>= tail;
})
>>= repr(self->body)
>>= sexpr::list();
}
sexpr operator()(const global& self) const {
return symbol("glob")
>>= self.name
>>= sexpr::list();
}
sexpr operator()(const local& self) const {
return symbol("var")
>>= integer(self.index)
>>= sexpr::list();
}
sexpr operator()(const capture& self) const {
return symbol("cap")
>>= integer(self.index)
>>= sexpr::list();
}
sexpr operator()(const ref<branch>& self) const {
return symbol("branch")
>>= repr(self->then)
>>= repr(self->alt)
>>= sexpr::list();
}
sexpr operator()(const call& self) const {
return symbol("call")
>>= integer(self.argc)
>>= sexpr::list();
}
sexpr operator()(const def& self) const {
return symbol("def")
>>= self.name
>>= sexpr::list();
}
sexpr operator()(const ref<use>& self) const {
return symbol("use")
>>= repr(self->env)
>>= sexpr::list();
}
sexpr operator()(const import& self) const {
return symbol("import")
>>= self.package
>>= sexpr::list();
}
sexpr operator()(const sel& self) const {
return symbol("sel")
>>= self.attr
>>= sexpr::list();
}
sexpr operator()(const record& self) const {
return symbol("record")
>>= foldr(sexpr::list(), self.attrs, [&](sexpr::list rhs, symbol lhs) {
return lhs >>= rhs;
});
}
sexpr operator()(const ref<match>& self) const {
auto tail = sexpr::list();
for(const auto& it: self->cases) {
tail = (it.first >>= repr(it.second) >>= sexpr::list()) >>= tail;
}
return symbol("match") >>= tail;
}
};
sexpr repr(const ir::expr& self) {
return self.match(repr_visitor());
}
}