summaryrefslogtreecommitdiff
path: root/main.c
blob: debb0ae56036ff0079e4e0e91da6ee31ea7f7cd0 (plain)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>

#include "lex.h"
#include "arena.h"
#include "dynarr.h"
#include "strio.h"
#include "ir.h"
#include "peephole.h"
#include "proc.h"

int no_opt = 0;

typedef struct {
	DYNARR(Proc) procs;
} Unit;

void unit_init(Unit *u) {
	(void)u;
}

void unit_free(Unit *u) {
	for (int i = 0; i < u->procs.len; i++) {
		proc_free(&u->procs.data[i]);
	}
	free(u->procs.data);
}

/* parsing */

Node *parse_expr(Lexer *l, Proc *p, Type *twant);

/* TODO: eliminate unused if-else statements at the end of compilation
 * they don't get pruned out by peephole optimizations if there are phi
 * nodes that are connected to keepalive (due to still being in scope).
 * so probably this will have to be done in a separate step after the
 * graph has been generated.
 */

Node *ctrl(Graph *p, Node *n) {
	if (!n) {
		p->ctrl = node_new_lit(p, (Value) {
			.type = { .lvl = T_XCTRL, .t = T_NONE }
		});
		return NULL;
	}
	p->ctrl = n;
	return n;
}

void parse_return(Lexer *l, Proc *p) {
	lex_next(l);
	Node *n;
	Graph *g = &p->graph;
	if (p->ret_type.t == T_NONE) {
		n = node_new(g, N_RETURN, g->ctrl);
	} else {
		Node *e = parse_expr(l, p, &p->ret_type);
		n = node_new(g, N_RETURN, g->ctrl, e);
	}
	n = node_peephole(n, g, l);
	if (n->type.lvl != T_XCTRL) {
		node_add(g, n, g->stop);
	}
	ctrl(g, n);
	ctrl(g, NULL);
}

Type parse_type(Lexer *l, Proc *proc) {
	(void)proc;
	Type t = { .lvl = T_BOT };
	if (l->tok == TOK_DEREF) {
		lex_next(l);
		t.t = T_PTR;
		t.next = new(&proc->perm, Type);
		*t.next = parse_type(l, proc);
		return t;
	}
	lex_expected(l, TM_IDENT);
	if (l->tok == TOK_IDENT) {
		if (str_eql(l->ident, S("i64"))) {
			t.t = T_INT;
		} else if (str_eql(l->ident, S("bool"))) {
			t.t = T_BOOL;
		} else {
			lex_error(l, LE_ERROR, S("unknown type"));
		}
	}
	lex_next(l);
	return t;
}

void parse_let(Lexer *l, Proc *p) {
	Graph *g = &p->graph;
recurse:
	lex_expect(l, TM_IDENT);
	Str name = l->ident;
	LexSpan pos = l->pos;
	lex_next(l);
	Node *rhs = NULL;
	Type t = { .t = T_NONE };
	if (l->tok != TOK_EQL) {
		t = parse_type(l, p);
		if (l->tok != TOK_EQL) {
			rhs = node_new(g, N_UNINIT, g->start);
			rhs->type = t;
		}
	}
	if (l->tok == TOK_EQL) {
		lex_next(l);
		rhs = parse_expr(l, p, t.t == T_NONE ? NULL : &t);
	}
	NameBinding *b = scope_bind(&p->scope, name, rhs, pos, g);
	if (b) {
		lex_error_at(l, pos, LE_WARN, S("shadowing previous declaration"));
		lex_error_at(l, b->src_pos, LE_WARN, S("declared here"));
	}
	/* TODO: isn't this just a do while loop */
	if (l->tok == TOK_COMMA) goto recurse;
}

void parse_stmt(Lexer *l, Proc *p);

/* TODO: return node from this! */
void parse_block(Lexer *l, Proc *p, ScopeNameList *nl) {
	Graph *g = &p->graph;
	lex_next(l);
	arena_reset(&p->scratch);
	scope_push(&p->scope);
	while (l->tok != TOK_RBRACE) {
		lex_expected_not(l, TM_EOF);
		parse_stmt(l, p);
	}
	if (nl) {
		scope_collect(&p->scope, g, nl, &p->scratch);
	}
	scope_pop(&p->scope, g);
	lex_expected(l, TM_RBRACE);
	lex_next(l);
}

void parse_assign(Lexer *l, Proc *p) {
	Str name = l->ident;
	LexSpan pos = l->pos;
	lex_expect(l, TM_ASSIGN);
	lex_next(l);
	NameBinding *b = scope_find(&p->scope, name);
	if (!b) {
		lex_error_at(l, pos, LE_ERROR, S("undeclared identifier"));
	}
	Node *e = parse_expr(l, p, &b->node->type);
	if (node_uninit(e)) {
		lex_error_at(l, e->src_pos, LE_ERROR,
				str_fmt(&p->scratch, "uninitialized %S",
					type_desc(&e->type, &p->scratch)));
	} else if (node_maybe_uninit(e)) {
		lex_error_at(l, e->src_pos, LE_WARN,
				str_fmt(&p->scratch, "possibly uninitialized %S",
					type_desc(&e->type, &p->scratch)));
	}
	scope_update(b, e, &p->graph);
}

/* TODO: Implement a better system for this.
 *
 * Something like:
 *
 * 	ScopeChangeList chg_true = {0}, chg_false = {0};
 * 	scope_track_changes(&p->scope, &chg_true, p, scratch);
 * 	parse_block(l, p);
 * 	scope_rewind_changes(&p->scope, &chg_true, p);
 *	scope_track_changes(&p->scope, &chg_false, p, scratch);
 * 	parse_block(l, p);
 * 	scope_merge_changes(&p->scope, &chg_true, &chg_false, region, p);
 *
 * We put a flag in Scope somewhere that marks where it's tracking changes to,
 * then have scope_update() look up the name binding being changed in the change
 * list;  if present, update value to the new node.  If not, create it with
 * orig set to the previous value of the name binding, and value to the new node.
 * Since we're only tracking _changes_, there should be some way to make sure that
 * newly created let-bindings after tracking starts aren't stored in the list.
 * Maybe give NameBindings an index, note the index of the latest binding at the
 * time scope_track_changes() is called, ignore any larger than it.
 *
 * Take care with making sure changes remain attached to keepalive --- rewind
 * doesn't remove the new values, since it's just to revert temporarily.
 *
 * Also, probably should have a scratch arena for this sort of throwaway parsing
 * data, instead of putting stuff in the procedure graph arena.  It can be reset
 * after each statement.
 *
 * Also also, switch scopes from a linked list to something a bit faster, like
 * a simple arena-backed hash trie.
 *
 */

void merge_scope(Lexer *l, Proc *p, Node *region, ScopeNameList *before, ScopeNameList *ntrue, ScopeNameList *nfalse) {
	Graph *g = &p->graph;
	for (int i = 0; i < before->len; i++) {
		int j, k;
		ScopeName *b4 = &before->data[i];
		for (j = 0; j < ntrue->len && !str_eql(ntrue->data[j].name, b4->name); j++);
		for (k = 0; k < nfalse->len && !str_eql(nfalse->data[k].name, b4->name); k++);
		ScopeName *yes = j < ntrue->len ? &ntrue->data[j] : NULL;
		ScopeName *no = k < nfalse->len ? &nfalse->data[k] : NULL;
		if (!yes && !no) continue; /* no change */
		Node *phi;
		if (!no) {
			if (yes->node == b4->node) continue;
			phi = node_new(g, N_PHI, region, yes->node, b4->node);
		} else if (!yes) {
			if (no->node == b4->node) continue;
			phi = node_new(g, N_PHI, region, b4->node, no->node);
		} else {
			if (yes->node == b4->node && no->node == b4->node) continue;
			phi = node_new(g, N_PHI, region, yes->node, no->node);
		}
		phi = node_peephole(phi, g, l);
		NameBinding *b = scope_find(&p->scope, b4->name);
		assert(b);
		scope_update(b, phi, g);
	}
}

/* TODO: find out a way to encode known relations between nodes, based on the
 * conditional, within the body of an if statement --- e.g., for a statement
 *
 * if a < 5 {
 * 	if a < 10 {
 * 		foo()
 * 	}
 * } else {
 * 	if a > 3 {
 * 		bar()
 * 	}
 * }
 *
 * we should be able to infer that the second condition is always true, because
 * it's only reachable if a < 5, and 5 < 10.  conversely, in the else branch,
 * we know that a >= 5, and can use that to make assumptions on comparisons
 * made there too!
 *
 */

void parse_if(Lexer *l, Proc *p) {
	Graph *g = &p->graph;
	lex_next(l);
	Node *cond = parse_expr(l, p, &(Type) { .t = T_BOOL });
	Node *ctrl_if = NULL, *ctrl_else = NULL;
	Node *if_node = node_new(g, N_IF_ELSE, g->ctrl, cond);
	if_node->val = (Value) {
		.type = { .lvl = T_TOP, .t = T_TUPLE },
		.tuple = { 0 }
	};
	ZDA_PUSH(g->pool->arena, &if_node->val.tuple, (Value) { .type = { .lvl = T_CTRL, .t = T_NONE } });
	ZDA_PUSH(g->pool->arena, &if_node->val.tuple, (Value) { .type = { .lvl = T_CTRL, .t = T_NONE } });
	if_node = node_peephole(if_node, g, l);
	node_add(g, if_node, g->keepalive);
	Node *if_true = node_new(g, N_PROJ, if_node);
	Node *if_false = node_new(g, N_PROJ, if_node);
	if_true->val.i = 0;
	if_false->val.i = 1;
	if_true = node_peephole(if_true, g, l);
	if_false = node_peephole(if_false, g, l);
	node_remove(g, if_node, g->keepalive);
	assert(if_true->in.len > 0);
	assert(if_false->in.len > 0);
	node_add(g, if_true, g->keepalive);
	node_add(g, if_false, g->keepalive);
	ScopeNameList scope_before = { 0 }, scope_true = { 0 }, scope_false = { 0 };
	scope_collect(&p->scope, g, &scope_before, &p->scratch);
	if (cond->type.lvl == T_CONST) {
		if (cond->val.i) if_false->type.lvl = T_XCTRL;
		else if_true->type.lvl = T_XCTRL;
	}
	ctrl(g, if_true);
	lex_expected(l, TM_LBRACE);
	int pos = l->pos.ofs;
	parse_block(l, p, &scope_true);
	if_true->src_pos = (LexSpan) { .ofs = pos, .n = l->pos.ofs - pos };
	ctrl_if = g->ctrl;
	node_add(g, ctrl_if, g->keepalive);
	if (l->tok == TOK_ELSE) {
		for (int i = 0; i < scope_before.len; i++) {
			scope_update(scope_find(&p->scope, scope_before.data[i].name), scope_before.data[i].node, g);
		}
		ctrl(g, if_false);
		lex_expect(l, TM_LBRACE);
		pos = l->pos.ofs;
		parse_block(l, p, &scope_false);
		if_false->src_pos = (LexSpan) { .ofs = pos, .n = l->pos.ofs - pos };
		ctrl_else = g->ctrl;
		node_add(g, ctrl_else, g->keepalive);
	}
	Node *region;
	if (ctrl_else) {
		assert(ctrl_if);
		assert(ctrl_else);
		region = node_new(g, N_REGION, ctrl_if, ctrl_else);
		node_add_out(g, region, g->keepalive);
		node_remove(g, ctrl_if, g->keepalive);
		node_remove(g, ctrl_else, g->keepalive);
	} else {
		assert(ctrl_if);
		assert(if_false);
		region = node_new(g, N_REGION, ctrl_if, if_false);
		node_add_out(g, region, g->keepalive);
		node_remove(g, ctrl_if, g->keepalive);
		assert(if_true->refs > 0);
		assert(if_false->refs > 0);
	}
	ctrl(g, region);
	node_remove(g, if_true, g->keepalive);
	node_remove(g, if_false, g->keepalive);
	assert(g->ctrl->in.len > 0);
	assert(region->in.data[0]);
	assert(region->in.data[1]);
	merge_scope(l, p, region, &scope_before, &scope_true, &scope_false);
	scope_uncollect(&p->scope, g, &scope_true);
	scope_uncollect(&p->scope, g, &scope_false);
	scope_uncollect(&p->scope, g, &scope_before);
	node_del_out(region, g->keepalive);
	/* make sure we're not orphaning any phi nodes*/
	if (g->ctrl->out.len < 1) {
		ctrl(g, node_peephole(g->ctrl, g, l));
	}
}

void parse_stmt(Lexer *l, Proc *p) {
	/* TODO */
	(void)l;
	switch (l->tok) {
	case TOK_RETURN:
		parse_return(l, p);
		break;
	case TOK_LET:
		parse_let(l, p);
		break;
	case TOK_LBRACE:
		parse_block(l, p, NULL);
		break;
	case TOK_IDENT:
		parse_assign(l, p);
		break;
	case TOK_IF:
		parse_if(l, p);
		break;
	default:
		lex_expected(l, TM_RBRACE);
		break;
	}
}

void parse_args_list(Lexer *l, Proc *proc) {
	Node *start = proc->graph.start;
	Graph *g = &proc->graph;
	int i = 0;
	struct {
		Str name;
		LexSpan pos;
	} idbuf[32];
	int id = 0;
	while (l->tok != TOK_RPAREN && l->tok != TOK_EOF) {
		lex_expect(l, TM_IDENT);
		if (id == sizeof idbuf / sizeof *idbuf) {
			lex_error(l, LE_ERROR, S("too many arguments without specifying a type"));
			return;
		}
		idbuf[id].name = l->ident;
		idbuf[id].pos = l->pos;
		id++;
		lex_next(l);
		if (l->tok == TOK_COMMA) continue;
		Value v = (Value) { .type = parse_type(l, proc) };
		lex_expected(l, TM_RPAREN | TM_COMMA);
		for (int j = 0; j < id; j++) {
			Node *proj = node_new(g, N_PROJ, start);
			proj->type = v.type;
			proj->val.i = i++;
			ZDA_PUSH(&proc->perm, &start->val.tuple, v);
			scope_bind(&proc->scope, idbuf[j].name, proj, idbuf[j].pos, g);
		}
		id = 0;
	}
	lex_expected(l, TM_RPAREN);
	lex_next(l);
}

Node *find_return(Node *n) {
	if (n->op == N_RETURN) return n;
	for (int i = 0; i < n->out.len; i++) {
		Node *r = find_return(n->out.data[i]);
		if (r) return r;
	}
	return NULL;
}

void proc_opt_fwd(Proc *p, Lexer *l, Node *n) {
	Graph *g = &p->graph;
	if (n->walked == 2) return;
	n->walked = 2;
	switch (n->op) {
	case N_START:
		for (int i = 0; i < n->out.len; i++) {
			proc_opt_fwd(p, l, n->out.data[i]);
		}
		break;
	case N_IF_ELSE:
		if (n->out.len < 2) {
			//lex_error_at(l, n->src_pos, LE_ERROR, S("not all codepaths return"));
		}
		for (int i = 0; i < n->out.len; i++) {
			Node *r = find_return(n->out.data[i]);
			if (!r) {
				lex_error_at(l, n->out.data[i]->src_pos, LE_ERROR, S("not all codepaths return"));
			}
			proc_opt_fwd(p, l, n->out.data[i]);
		}
		break;
	case N_PROJ:
		proc_opt_fwd(p, l, n->out.data[0]);
		break;
	case N_REGION:
		/* cull empty if else */
		if (n->out.len == 1 && n->in.len == 2
				&& IN(n,0)->op == N_PROJ
				&& IN(n,1)->op == N_PROJ
				&& CTRL(IN(n,0)) == CTRL(IN(n,1))
				&& CTRL(IN(n,0))->op == N_IF_ELSE) {
			assert(n->out.data[0]->op != N_PHI);
			assert(n->out.data[0]->in.data[0] == n);
			Node *new_ctrl = CTRL(CTRL(CTRL(n)));
			Node *out = n->out.data[0];
			node_set_in(g, out, 0, new_ctrl);
			proc_opt_fwd(p, l, new_ctrl);
			return;
		}
		for (int i = 0; i < n->out.len; i++) {
			if (n->out.data[i]->op != N_PHI) {
				proc_opt_fwd(p, l, n->out.data[i]);
			}
		}
		break;
	default:
		break;
	}
}

void proc_opt(Proc *p, Lexer *l) {
	Graph *g = &p->graph;
	if (g->stop->in.len == 0) {
		if (p->ret_type.t != T_NONE) {
			lex_error(l, LE_ERROR, str_fmt(&p->scratch, "no return statement in function expecting %S", type_desc(&p->ret_type, &p->scratch)));
		}
	}
	for (int i = 0; i < g->start->out.len; i++) {
		Node *n = g->start->out.data[i];
		if (n->op == N_LIT && n->out.len < 1) {
			node_kill(n, g);
			i--;
		}
	}
	proc_opt_fwd(p, l, g->start);
}

Proc *parse_proc(Lexer *l, Unit *u) {
	int has_ret = l->tok == TOK_FUNC;
	DA_FIT(&u->procs, u->procs.len + 1);
	Proc *proc = &u->procs.data[u->procs.len++];
	lex_expect(l, TM_IDENT);
	proc_init(proc, l->ident);
	scope_push(&proc->scope);
	lex_next(l);
	if (l->tok == TOK_LPAREN) {
		parse_args_list(l, proc);
	}
	if (has_ret) {
		proc->ret_type = parse_type(l, proc);
	} else {
		proc->ret_type = (Type) { .lvl = T_BOT, .t = T_NONE };
	}
	lex_expected(l, TM_LBRACE);
	lex_next(l);
	while (l->tok != TOK_RBRACE) {
		lex_expected_not(l, TM_EOF);
		parse_stmt(l, proc);
	}
	scope_pop(&proc->scope, &proc->graph);
	lex_expected(l, TM_RBRACE);
	lex_next(l);
	proc_opt(proc, l);
	return proc;
}

void uninit_check(Lexer *l, Node *n, LexSpan pos) {
	if (node_uninit(n)) {
		lex_error_at(l, pos, LE_ERROR,
				str_fmt(&l->arena, "uninitialized %S",
					type_desc(&n->type, &l->arena)));
	} else if (node_maybe_uninit(n)) {
		lex_error_at(l, pos, LE_WARN,
				str_fmt(&l->arena, "possibly uninitialized %S",
					type_desc(&n->type, &l->arena)));
	}
}

Node *parse_term(Lexer *l, Proc *p, Type *twant) {
	(void)twant; /* to be used for .ENUM_TYPE and stuff */
	Node *node = NULL;
	NodeType op_after = N_START;
	Graph *g = &p->graph;
	if (TMASK(l->tok) & (TM_MINUS | TM_PLUS | TM_NOT)) {
		Token t = l->tok;
		lex_next(l);
		node = parse_term(l, p, twant);
		NodeType post_op = N_START;
		switch (t) {
		case TOK_MINUS: post_op = N_OP_NEG; break;
		case TOK_NOT: post_op = N_OP_NOT; break;
		default: return node;
		}
		if (post_op == N_START) return node;
		return node_peephole(node_new(g, post_op, NULL, node), g, l);
	}
	if (l->tok == TOK_LPAREN) {
		lex_next(l);
		node = parse_expr(l, p, NULL);
		lex_expected(l, TM_RPAREN);
		lex_next(l);
		node->src_pos.ofs--;
		node->src_pos.n += 2;
	} else if (l->tok == TOK_IDENT) {
		NameBinding *b = scope_find(&p->scope, l->ident);
		if (b) {
			node = b->node;
		} else {
			lex_error(l, LE_ERROR, S("undeclared identifier"));
		}
		uninit_check(l, node, l->pos);
		lex_next(l);
	} else if (TMASK(l->tok) & (TM_TRUE | TM_FALSE)) {
		node = node_new_lit_bool(g, l->tok == TOK_TRUE);
		lex_next(l);
	} else {
		lex_expected(l, TM_LIT_NUM);
		int64_t val = 0;
		for (int i = 0; i < l->ident.n; i++) {
			if (!(l->ident.s[i] >= '0' && l->ident.s[i] <= '9')) {
				lex_error(l, LE_ERROR, S("not a digit"));
				break;
			}
			val = (val * 10) + (l->ident.s[i] - '0');
		}
		node = node_new_lit_i64(g, val);
		node->src_pos = l->pos;
		lex_next(l);
	}
	if (op_after != N_START) {
		node = node_new(g, op_after, NULL, node_peephole(node, g, l));
	}
	return node_peephole(node, g, l);
}

NodeType tok_to_bin_op(Token t) {
	switch (t) {
	case TOK_PLUS: return N_OP_ADD; break;
	case TOK_MINUS: return N_OP_SUB; break;
	case TOK_ASTERISK: return N_OP_MUL; break;
	case TOK_SLASH: return N_OP_DIV; break;
	case TOK_NOT: return N_OP_NOT; break;
	case TOK_AND: return N_OP_AND; break;
	case TOK_OR: return N_OP_OR; break;
	case TOK_XOR: return N_OP_XOR; break;
	case TOK_SHL: return N_OP_SHL; break;
	case TOK_SHR: return N_OP_SHR; break;
	case TOK_EQL: return N_CMP_EQL; break;
	case TOK_NEQ: return N_CMP_NEQ; break;
	case TOK_LES: return N_CMP_LES; break;
	case TOK_GTR: return N_CMP_GTR; break;
	case TOK_LTE: return N_CMP_LTE; break;
	case TOK_GTE: return N_CMP_GTE; break;
	default: return N_START; break;
	}
}

/* TODO: operator precedence would be kinda nice actually, sad to say */
Node *parse_expr(Lexer *l, Proc *p, Type *twant) {
	LexSpan pos = l->pos;
	Graph *g = &p->graph;
	Node *lhs = parse_term(l, p, twant);
	NodeType nt = tok_to_bin_op(l->tok);;
	if (lhs->refs <= 0) lex_error(l, LE_ERROR, S("dead lhs"));
	assert(lhs->refs > 0);
	if (nt != N_START) {
		lex_next(l);
		/* necessary because if lhs is a deduplicated literal, it may be an input to rhs
		 * and therefore culled by peephole optimizations */
		Node *rhs;
		NODE_KEEP(g, lhs, {
			rhs = parse_expr(l, p, &lhs->type);
		});
		lhs = node_peephole(node_new(g, nt, NULL, lhs, rhs), g, l);
	}
	lhs->src_pos = (LexSpan) { pos.ofs, l->pos.ofs - pos.ofs };
	if (twant) type_expected(twant, lhs, l);
	return lhs;
}

void parse_toplevel(Lexer *l, Unit *u) {
	switch (l->tok) {
	case TOK_PROC:
	case TOK_FUNC:
		parse_proc(l, u);
		break;
	default:
		lex_expected(l, TM_PROC | TM_FUNC);
		break;
	}
}


void unit_print(Unit *u);
void parse_unit(Lexer *l) {
	Unit u = { 0 };
	unit_init(&u);
	while (l->tok != TOK_EOF) {
		parse_toplevel(l, &u);
	}
	unit_print(&u);
	unit_free(&u);
}

/* graph output */

/* TODO: Print at every stage of graph compilation and generate a frame for
 * each, so problems can be debugged visually as they occur.
 */

void node_print(Node *n, Proc *p) {
	if (n->walked == 1) return;
	n->walked = 1;
	if (n->op == N_START) {
		Str s = S("");
		int c = n->val.tuple.len;
		Value *v = n->val.tuple.data;
		for (int i = 0; i < c; i++) {
			if (i > 0) str_cat(&s, S(", "), &p->scratch);
			str_cat(&s, type_desc(&v[i].type, &p->scratch), &p->scratch);
		}
		printf("\t%d [label=\"start(%.*s)\"]", n->id, (int)s.n, s.s);
	} else if (n->op == N_LIT) {
		switch (n->type.t) {
		case T_INT:
			printf("\t%d [label=\"%ld\"]", n->id, n->val.i);
			break;
		case T_BOOL:
			printf("\t%d [label=\"%s\"]", n->id, n->val.i ? "true" : "false");
			break;
		case T_NONE:
			if (n->type.lvl == T_XCTRL) {
				printf("\t%d [label=\"~ctrl\"]", n->id);
				break;
			}
			/* fallthrough */
		default:
			printf("\t%d [label=\"literal %d\"]", n->id, n->id);
			break;
		}
	} else if (n->op == N_PROJ) {
		Str d = type_desc(&n->in.data[0]->val.tuple.data[n->val.i].type, &p->scratch);
		printf("\t%d [label=\"%.*s(%ld)\", shape=record]", n->id, (int)d.n, d.s, n->val.i);
	} else if (n->op == N_UNINIT) {
		Str s = type_desc(&n->type, &p->scratch);
		printf("\t%d [label=\"uninitialized %.*s\", shape=record]", n->id, (int)s.n, s.s);
	} else {
		printf("\t%d [label=\"%s\", shape=record]", n->id, node_type_name(n->op));
	}
	printf("\n");
	for (int i = 0; i < n->out.len; i++) {
		Node *o = n->out.data[i];
		if (o->op == N_LIT) {
			printf("\t%d -> %d [style=dashed]\n", n->id, o->id);
		} else {
			int j;
			for (j = 0; j < o->in.len && o->in.data[j] != n; j++);
			if (j == 0) {
				printf("\t%d -> %d [color=red,headlabel=%d]\n", n->id, o->id, j);
			} else {
				printf("\t%d -> %d [headlabel=%d]\n", n->id, o->id, j);
			}
		}
	}
	for (int i = 0; i < n->out.len; i++) {
		node_print(n->out.data[i], p);
	}
}

void proc_print(Proc *p) {
	Graph *g = &p->graph;
	if (g->start) {
		Str d = type_desc(&p->ret_type, &p->scratch);
		printf("\t\"%.*s %.*s\" -> %d\n", (int)p->name.n, p->name.s, (int)d.n, d.s, g->start->id);
		node_print(g->start, p);
		if (no_opt) {
			for (NameBinding *b = p->scope.free_bind; b; b = b->prev) {
				uint64_t id = (uintptr_t)b->node;
				printf("\t\t%lu [label=\"%.*s\",shape=none,fontcolor=blue]\n", id, (int)b->name.n, b->name.s);
				printf("\t\t%lu -> %d [arrowhead=none,style=dotted,color=blue]\n", id, b->node->id);
			}
		}
	}
}

void unit_print(Unit *u) {
	puts("digraph {");
	for (int i = 0; i < u->procs.len; i++) {
		proc_print(&u->procs.data[i]);
	}
	puts("}");
}

/* main */

int main(int argc, const char **argv) {
	if (argc != 2) {
		fprintf(stderr, "Usage: %s FILE\n", argv[0]);
		return 1;
	}
	Lexer l = { 0 };
	lex_start(&l, argv[1]);
	parse_unit(&l);
	lex_free(&l);
	return 0;
}