bitwise
linear.c
1#include "util.h"
2#include "kav.h"
3
4struct lvar {
5 struct lvar *next;
6 struct binding *binding;
7 int used;
8 int borrowed;
9};
10
11static struct lvar *vars;
12
13int
14islinear(struct type *t) {
15 t = deref(t);
16 switch(t->kind) {
17 case YNAME:
18 return t->binding->decl && t->binding->decl->linear || t->binding == bworld;
19 case YTUPLE:
20 /* all tuples are linear */
21 return 1;
22 case YVAR:
23 /*linear unless its utype */
24 return t->binding->param->ann->binding != butype;
25 case YFN:
26 case YMETA:
27 return 0;
28 }
29 return 0;
30}
31
32static void
33track(struct binding *b, int borrowed) {
34 struct lvar *v;
35
36 v = alloc(sizeof(*v));
37 v->binding = b;
38 v->borrowed = borrowed;
39 v->used = 0;
40 v->next = vars;
41 vars = v;
42}
43
44static void
45trackpat(struct pat *p, int borrowed) {
46 struct pat *a;
47
48 switch(p->kind) {
49 case PNAME:
50 if(islinear(p->binding->type))
51 track(p->binding, borrowed);
52 break;
53 case PCON:
54 if(p->tilde && borrowed)
55 errorat(&p->name.loc, "destructor on a borrowed pattern");
56 if(p->tilde && !islinear(p->type))
57 errorat(&p->name.loc, "destructor on nonlinear value");
58 /* we're borrowing*/
59 if(!p->tilde)
60 borrowed = 1;
61 /* from here the case is the same */
62 case PTUPLE:
63 for(a = p->args; a; a = a->next)
64 trackpat(a, borrowed);
65 break;
66 case PWILD:
67 if(!borrowed && islinear(p->type))
68 errorat(&p->name.loc, "linear value discarded by _");
69 break;
70 default:
71 break;
72 }
73}
74
75static int
76count(struct lvar *v) {
77 int n = 0;
78 for(; v; v = v->next)
79 n++;
80 return n;
81}
82
83static int *
84snapshot(struct lvar *mark) {
85 int *flags;
86 int i = 0;
87 struct lvar *v;
88
89 flags = alloc(count(mark) * sizeof(*flags));
90 for(v = mark; v; v = v->next) {
91 flags[i] = v->used;
92 i++;
93 }
94 return flags;
95}
96
97static void
98restore(struct lvar *mark, int *flags) {
99 int i = 0;
100 struct lvar *v;
101
102 for(v = mark; v; v = v->next) {
103 v->used = flags[i];
104 i++;
105 }
106}
107
108static void
109endscope(struct lvar *mark) {
110 struct lvar *v;
111
112 for(v = vars; v != mark; v = v->next)
113 if(!v->used && !v->borrowed)
114 errorat(&v->binding->name.loc, "linear value '", tokdup(v->binding->name), "' is never used");
115 vars = mark;
116}
117
118static void use(struct expr *);
119static void look(struct expr *e);
120
121static void
122arms(struct expr *e, int borrowed) {
123 struct arm *r;
124 struct lvar *mark;
125 int *before;
126 int *after = nil;
127 int *pre;
128 struct lvar *v;
129 int i;
130
131 mark = vars;
132 before = snapshot(mark);
133 for(r = e->arm; r; r = r->next) {
134 restore(mark, before);
135 if(r->cond) {
136 pre = snapshot(mark);
137 look(r->cond);
138 for(v = mark, i = 0; v; v = v->next, i++)
139 if(v->used != pre[i])
140 errorat(&r->loc, "condition consumes '", tokdup(v->binding->name), "'");
141 }
142 if(r->pat)
143 trackpat(r->pat, borrowed);
144 use(r->body);
145 endscope(mark);
146 if(r == e->arm)
147 after = snapshot(mark);
148 else
149 for(v = mark, i = 0; v; v = v->next, i++)
150 if(v->used != after[i])
151 errorat(&r->loc, "arms disagree about '", tokdup(v->binding->name), "'");
152 }
153 restore(mark, after);
154}
155
156static void
157look(struct expr *e) {
158 struct lvar *v;
159
160 if(e->kind != EIDENT) {
161 if(islinear(e->type))
162 errorat(&e->tok.loc, "linear temporary is borrowed and never freed");
163 use(e);
164 return;
165 }
166 for(v = vars; v; v = v->next) {
167 if(v->binding != e->binding)
168 continue;
169 if(v->used)
170 errorat(&e->tok.loc, "linear value '", tokdup(e->tok), "' used after it was consumed");
171 }
172}
173
174static int
175hastilde(struct pat *p) {
176 struct pat *a;
177
178 if(p->tilde)
179 return 1;
180 if(p->kind == PTUPLE)
181 for(a = p->args; a; a = a->next)
182 if(hastilde(a))
183 return 1;
184 return 0;
185}
186
187static int
188haslinear(struct pat *p) {
189 struct pat *a;
190
191 if(p->kind == PNAME)
192 return islinear(p->binding->type);
193 for(a = p->args; a; a = a->next)
194 if(haslinear(a))
195 return 1;
196 return 0;
197}
198
199static struct lvar *
200findvar(struct expr *e) {
201 struct lvar *v;
202
203 if(e->kind != EIDENT)
204 return nil;
205 for(v = vars; v; v = v->next)
206 if(v->binding == e->binding)
207 return v;
208 return nil;
209}
210
211/* f or a case (a, b) of, borrow each tracked identifier in the tuple
212 * instead of treating tuple as a linear temporary */
213static void
214borrowtuple(struct expr *e) {
215 struct expr *a;
216 struct lvar *v;
217 struct lvar *saved[16];
218 int old[16];
219 int n = 0, i;
220
221 for(a = e->scrut->args; a; a = a->next) {
222 look(a);
223 v = findvar(a);
224 if(v && n < 16) {
225 saved[n] = v;
226 old[n] = v->borrowed;
227 n++;
228 v->borrowed = 1;
229 }
230 }
231 arms(e, 1);
232 for(i = n - 1; i >= 0; i--)
233 saved[i]->borrowed = old[i];
234}
235
236static void
237use(struct expr *e) {
238 struct lvar *v;
239 struct expr *a;
240 struct val *l;
241 struct arm *r;
242 struct type *y;
243 struct lvar *mark;
244 int tilde = 0;
245 int old;
246 struct type *ps;
247 struct lvar **locked;
248 usize nlocked;
249 int *pre;
250 int i;
251
252 switch(e->kind) {
253 case EIDENT:
254 for(v = vars; v; v = v->next) {
255 if(v->binding != e->binding)
256 continue;
257 if(v->borrowed)
258 errorat(&e->tok.loc, "cannot consume borrowed value '", tokdup(e->tok), "'");
259 if(v->used)
260 errorat(&e->tok.loc, "linear value '", tokdup(e->tok), "' used twice");
261 v->used = 1;
262 }
263 break;
264 case ECALL:
265 /* the right side of && and || only runs sometimes, so it
266 * can't consume anything or the consume would be conditional */
267 if(e->tok.kind == TAND || e->tok.kind == TOR) {
268 use(e->args);
269 pre = snapshot(vars);
270 use(e->args->next);
271 for(v = vars, i = 0; v; v = v->next, i++)
272 if(v->used != pre[i])
273 errorat(&e->tok.loc, "right side of '", tokdup(e->tok), "' consumes '", tokdup(v->binding->name), "'");
274 break;
275 }
276 if(e->tok.kind == TIDENT && e->binding->type->kind == YFN)
277 ps = e->binding->type->args;
278 else
279 ps = nil;
280 /* for a case where a function with two arguments of the same type borrows one
281 * and consumes the other; the caller passes the same value twice; we cannot
282 * allow that */
283 for(a = e->args, y = ps; a; a = a->next) {
284 if(!y || !y->borrow)
285 use(a);
286 if(y)
287 y = y->next;
288 }
289 for(a = e->args, y = ps; a; a = a->next) {
290 if(y && y->borrow)
291 look(a);
292 if(y)
293 y = y->next;
294 }
295 break;
296 case ETUPLE:
297 for(a = e->args; a; a = a->next)
298 use(a);
299 break;
300 case EBLOCK:
301 mark = vars;
302 nlocked = 0;
303 for(l = e->vals; l; l = l->next)
304 nlocked++;
305 locked = nlocked ? alloc(nlocked * sizeof(*locked)) : nil;
306 nlocked = 0;
307 for(l = e->vals; l; l = l->next) {
308 v = nil;
309 if(l->pat->kind == PCON && !l->pat->tilde)
310 v = findvar(l->init);
311 if(!v) {
312 use(l->init);
313 trackpat(l->pat, 0);
314 continue;
315 }
316 /* val C(x) = v without ~ borrows v like a case does */
317 look(l->init);
318 trackpat(l->pat, 0);
319 if(haslinear(l->pat) && !v->borrowed) {
320 locked[nlocked++] = v;
321 v->borrowed = 1;
322 }
323 }
324 use(e->body);
325 while(nlocked > 0)
326 locked[--nlocked]->borrowed = 0;
327 endscope(mark);
328 break;
329 case ECASE:
330 for(r = e->arm; r; r = r->next) {
331 if(hastilde(r->pat)) {
332 tilde = 1;
333 break;
334 }
335 }
336 if(tilde) {
337 use(e->scrut);
338 arms(e, 0);
339 break;
340 }
341 if(e->scrut->kind == ETUPLE) {
342 borrowtuple(e);
343 break;
344 }
345 v = findvar(e->scrut);
346 if(!v) {
347 /*look at it for a linear temporary like case f(l) of */
348 look(e->scrut);
349 arms(e, 1);
350 break;
351 }
352 look(e->scrut);
353 old = v->borrowed;
354 v->borrowed = 1;
355 arms(e, 1);
356 v->borrowed = old;
357 break;
358 case EGUARD:
359 arms(e, 0);
360 break;
361 case ECAST:
362 use(e->args);
363 break;
364 default:
365 break;
366 }
367}
368
369static void
370linfn(struct decl *d) {
371 struct param *p;
372
373 if(!d->body)
374 return;
375 vars = nil;
376 for(p = d->params; p; p = p->next)
377 if(islinear(p->binding->type))
378 track(p->binding, p->binding->type->borrow);
379 use(d->body);
380 endscope(nil);
381}
382
383void
384linear(struct module *m) {
385 struct decl *d;
386
387 for(; m; m = m->next) {
388 for(d = m->decls; d; d = d->next)
389 if(d->kind == DFN)
390 linfn(d);
391 }
392}