~pixelherodev/c3lib

ref: 25e027fb7ba13f2753cb4e652f57ba311ffce4cc c3lib/sema/expr.c -rw-r--r-- 19.1 KiB
25e027fbNoam Preil merge 2 months ago
                                                                                
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
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../tags.h"
#include "../c3.h"
#include "../util.h"
#include "../stb.h"
#include "../stb_ds.h"

// Lowerizer operates, effectively, on the function level
// The lowerizer takes in functions in base form - using expressions and stems - and replaces it with IR, linking to the original for future analysis

static struct{
	// This is per-block
	struct ctx_block{
		uint32_t *stmts;
		uint32_t *deferred;
		// each function's IR blocks are numbered in the order they're generated 0-indexed.
		// id is the number of the IR block. Not the source block. This is important because
		// e.g. inlining can generate extra blocks, which also get IDs.
		uint32_t id;
	}block;
	// Function node references - given as indices containing indices of C3_VALUE_FNS - which need to be updated to point to generated C3_IR_FNs
	// In addition to everything in here, all entries in the call graph need to be updated
	uint32_t *fn_refs;
	struct{
		uint32_t *values;
		uint32_t *irs;
	} fn_map;
	c3ctx *c3;
	uint32_t next_block_id;
	// during normal analysis, this is set to zero. When inlining a function, this is set to 1. l_block, when inline, sets this to the return value.
	// TODO: add IR for breaking with a value, and use that instead of this hack.
	uint32_t cur_fn;
	uint32_t *inlined_args;
} ctx;

static uint32_t
s_map(uint32_t index)
{
	uint32_t i;
	for(i = 0; i < stb_sb_count(ctx.fn_map.values); i += 1)
		if(ctx.fn_map.values[i] == index)
			return ctx.fn_map.irs[i];
	return -1;
}

static uint32_t l_block(uint32_t index, int type);
static uint32_t s_expr(uint32_t index, uint32_t **list);
static uint32_t s_decl(uint32_t index);
uint32_t t_dedup_tag(uint32_t tag);

static uint32_t
s_defer(uint32_t index)
{
	if(s_expr(c3nodechild(*ctx.c3, index, 0), &ctx.block.deferred) == -1){
		ERROR("Failed to analyze deferred expression", 0);
		return -1;
	}
	return 0;
}

static uint32_t
s_binop(uint32_t index, c3tag tag)
{
	uint32_t lhs, rhs;
	uint32_t olhs, orhs;
	olhs = c3nodechild(*ctx.c3, index, 0);
	orhs = c3nodechild(*ctx.c3, index, 1);
	if(c3nodetag(*ctx.c3, olhs) == C3_VAR || c3nodetag(*ctx.c3, olhs) == C3_CONST)
		lhs = olhs;
	else
		lhs = s_expr(olhs, &ctx.block.stmts);
	rhs = s_expr(orhs, &ctx.block.stmts);
	if(c3nodetag(*ctx.c3, olhs) == C3_DISCARD && tag == C3_IR_SET)
		return 0;
	if(lhs == -1 || rhs == -1){
		ERROR("Failed to analyze binop '%s' subexprs", c3tagstr(tag));
		return -1;
	}
	index = c3append(ctx.c3, tag, 2, lhs, rhs);
	if(index == -1)
		OOM();
	return index;
}


static int
s_scope_args(uint32_t index)
{
	uint32_t i, count;
	uint32_t params;
	uint32_t args;
	uint32_t arg;
	params = c3nodechild(*ctx.c3, c3nodechild(*ctx.c3, ctx.cur_fn, 0), 1);
	args = c3nodechild(*ctx.c3, index, 1);
	count = c3nodekids(*ctx.c3, params);
	if(count != c3nodekids(*ctx.c3, args)){
		ERROR("Args and params count differ: %d vs %d!", count, c3nodekids(*ctx.c3, args));
		return 0;
	}
	for(i = 0; i < count; i += 1){
		WARN("TODO: validate inlined arg types", 0);
		arg = c3nodechild(*ctx.c3, args, i);
		if(stb_sb_push_u32(&ctx.inlined_args, arg) == -1)
			OOM();
	}
	return 1;
}

static uint32_t
s_inline(uint32_t index)
{
	uint32_t fn;
	uint32_t prev_fn;
	uint32_t *prev_args;
	uint32_t block;
	prev_fn = ctx.cur_fn;
	prev_args = ctx.inlined_args;
	fn = c3nodechild(*ctx.c3, index, 0);
	block = c3nodechild(*ctx.c3, fn, 1);
	ctx.inlined_args = NULL;
	ctx.cur_fn = fn;
	if(!s_scope_args(index)){
		ERROR("Failed to inject args into inline scope",0);
		return -1;
	}
	block = l_block(block, 2);
	ctx.cur_fn = prev_fn;
	stb_sb_free(ctx.inlined_args);
	ctx.inlined_args = prev_args;
	if(block == -1)
		ERROR("Failed to lower inlined block", 0);
	return block;
}


static uint32_t
s_call(uint32_t index)
{
	uint32_t tmp;
	uint32_t *kids;
	uint32_t i;
	uint16_t count;
	uint32_t param_list;
	c3tag tag;
	uint16_t conv;
	kids = NULL;
	tmp = index;
	index = c3nodechild(*ctx.c3, index, 0);
	tag = c3nodetag(*ctx.c3, index);
	if(tag != C3_VALUE_FN){
		ERROR("ICE: expected VALUE_FN for EXPR_CALL, found '%s'", c3tagstr(tag));
		return -1;
	}
	param_list = c3nodechild(*ctx.c3, index, 0);
	conv = c3nodetag(*ctx.c3, c3nodechild(*ctx.c3, param_list, 2));
	param_list = c3nodechild(*ctx.c3, param_list, 1);
	if(conv == C3_CALL_CONV_INLINE)
		return s_inline(tmp);
	if(conv != C3_CALL_CONV_DEFAULT){
		ERROR("TODO: explicit calling conventions", 0);
		return -1;
	}
	if(!stb_sb_push_u32(&kids, index))
		OOM();
	index = c3nodechild(*ctx.c3, tmp, 1);
	count = c3nodekids(*ctx.c3, index);
	for(i = 0; i < count; i += 1){
		tmp = c3nodechild(*ctx.c3, index, i);
		tmp = s_expr(tmp, &ctx.block.stmts);
		tag = c3nodetag(*ctx.c3, c3nodechild(*ctx.c3, param_list, i));
		if(tag == C3_COMPTIME){
			ERROR("TODO: comptime engine", 0);
			return -1;
		}
		if(tmp == -1){
			ERROR("failed to evaluate function arguments", 0);
			return -1;
		}
		if(!stb_sb_push_u32(&kids, tmp))
			OOM();
	}
	tmp = c3appendarr(ctx.c3, C3_IR_CALL, stb_sb_count(kids), kids);
	if(tmp == -1 || !stb_sb_push_u32(&ctx.fn_refs, tmp + 1))
		OOM();
	return tmp;
}

static uint32_t
s_break(uint32_t index, c3tag tag)
{
	uint32_t val;
	val = s_expr(c3nodechild(*ctx.c3, index, 1), &ctx.block.stmts);
	if(val == -1){
		ERROR("Failed to analyze break value", 0);
		return -1;
	}
	val = c3append(ctx.c3, tag, 2, ctx.c3->ast.nodes[index + 1], val);
	if(val == -1)
		OOM();
	return val;
}

static uint32_t
s_asm(uint32_t index)
{
	uint32_t child;
	uint32_t i, count;
	count = c3nodekids(*ctx.c3, index);
	for(i = 1; i < count; i += 1){
		child = c3nodechild(*ctx.c3, index, i);
		if(c3nodetag(*ctx.c3, child) != C3_CONST && c3nodetag(*ctx.c3, child) != C3_VAR)
			ctx.c3->ast.nodes[index + 1 + i].node_index = s_expr(child, &ctx.block.stmts);
		if(ctx.c3->ast.nodes[index + 1 + i].node_index == -1){
			ERROR("Failed to analyze asm parameter", 0);
			return -1;
		}
	}
	return index;
}

static uint32_t
s_size(uint32_t index)
{
	c3tag tag = c3nodetag(*ctx.c3, index);
	switch(tag){
	case C3_TYPE_U32:
		return 4;
	default:
		ERROR("TODO: size tag %s", c3tagstr(tag));
		return -1;
	}
}

// Generate the IR to initialize the initialized expression
// For a container initializer, this initializes each member
static uint32_t
s_initialized(uint32_t index)
{
	uint32_t type, init, field;
	uint32_t i, j;
	uint32_t *checked_inits;
	uint32_t offset;
	uint32_t tmp, tmp2;
	uint32_t container;
	struct ctx_block old;
	uint32_t block;
	c3tag tag;
	type = c3nodechild(*ctx.c3, index, 0);
	init = c3nodechild(*ctx.c3, index, 1);
	checked_inits = NULL;
	offset = 0;
	old = ctx.block;
	ctx.block.stmts = NULL;
	ctx.block.deferred = NULL;
	ctx.block.id = ctx.next_block_id;
	ctx.next_block_id += 1;
	for(i = 0; i < c3nodekids(*ctx.c3, init); i += 1){
		if(!stb_sb_push_u32(&checked_inits, c3nodechild(*ctx.c3, init, i)))
			OOM();
		if(checked_inits[i] == -1){
			ERROR("ICE: invalid initializer", 0);
			return -1;
		}
	}
	tag = c3nodetag(*ctx.c3, type);
	if(tag != C3_STRUCT){
		ERROR("Expected struct for initializer expr, found %s", c3tagstr(tag));
		return -1;
	}
	container = c3append(ctx.c3, C3_IR_CONTAINER, 1, index);
	if(container == -1 || !stb_sb_push_u32(&ctx.block.stmts, container))
		OOM();
	for(i = 0; i < c3nodekids(*ctx.c3, type); i += 1){
		field = c3nodechild(*ctx.c3, type, i);
		tag = c3nodetag(*ctx.c3, field);
		if(tag != C3_FIELD){
			ERROR("TODO: validate tag %s", c3tagstr(tag));
			continue;
		}
		for(j = 0; j < stb_sb_count(checked_inits); j += 1){
			init = checked_inits[j];
			if(c3nodechild(*ctx.c3, c3nodechild(*ctx.c3, init, 0), 0) == c3nodechild(*ctx.c3, c3nodechild(*ctx.c3, field, 0), 0)){
				checked_inits[j] = -1;
				if(offset != 0){
					ERROR("TODO: multi-member containers", 0);
					return -1;
				}
				tmp = c3append(ctx.c3, C3_CONTAINER_OFFSET, 2, offset, container);
				if(tmp == -1)
					OOM();
				tmp2 = s_size(c3nodechild(*ctx.c3, field, 1));
				if(tmp2 == -1){
					ERROR("ICE: unable to size member", 0);
					return -1;
				}
				offset += tmp2;
				tmp2 = s_expr(c3nodechild(*ctx.c3, init, 1), &ctx.block.stmts);
				if(tmp2 == -1 || tmp2 == 0){
					ERROR("Failed to lower IR for field init", 0);
					return -1;
				}
				tmp = c3append(ctx.c3, C3_IR_SET, 2, tmp, tmp2);
				if(tmp == -1 || !stb_sb_push_u32(&ctx.block.stmts, tmp))
					OOM();
				j = -2;
				break;
			}
		}
		if(j == -2)
			continue;
		ERROR("Initializer for '%s' not found!", h_c3str(*ctx.c3, c3nodechild(*ctx.c3, c3nodechild(*ctx.c3, field, 0), 0)));
		return -1;
	}
	tmp = c3append(ctx.c3, C3_IR_BREAK, 2, ctx.block.id, container);
	if(tmp == -1 || !stb_sb_push_u32(&ctx.block.stmts, tmp))
		OOM();
	block = c3appendarr(ctx.c3, C3_BLOCK, stb_sb_count(ctx.block.stmts), ctx.block.stmts);
	stb_sb_free(ctx.block.stmts);
	if(ctx.block.deferred != NULL){
		ERROR("ICE: deferred statements should not show up within initializers", 0);
		return -1;
	}
	ctx.block = old;
	if(block == -1)
		OOM();
	return block;
}

static uint32_t
s_if(uint32_t index)
{
		uint32_t tmp;
		uint32_t block;
		struct ctx_block old;
		old = ctx.block;
		ctx.block.stmts = NULL;
		ctx.block.id = ctx.next_block_id;
		ctx.next_block_id += 1;
		tmp = s_expr(c3nodechild(*ctx.c3, index, 0), &ctx.block.stmts);
		if(tmp == -1){
			ERROR("Failed to analyze loop condition", 0);
			ctx.block = old;
			return -1;
		}
		tmp = c3append(ctx.c3, C3_IR_INVERT, 1, tmp);
		if(tmp == -1 || !stb_sb_push_u32(&ctx.block.stmts, tmp))
			OOM();
		tmp = c3append(ctx.c3, C3_IR_COND_BR, 1, tmp);
		if(tmp == -1 || !stb_sb_push_u32(&ctx.block.stmts, tmp))
			OOM();
		tmp = l_block(c3nodechild(*ctx.c3, index, 1), 0);
		if(tmp == -1 || !stb_sb_push_u32(&ctx.block.stmts, tmp))
			OOM();
		block = c3appendarr(ctx.c3, C3_BLOCK, stb_sb_count(ctx.block.stmts), ctx.block.stmts);
		stb_sb_free(ctx.block.stmts);
		ctx.block = old;
		return block;
}

static uint32_t
s_param(uint32_t index)
{
	uint32_t i;
	c3tag tag;
	uint32_t node, param;
	// valuefn -> typefn
	node = c3nodechild(*ctx.c3, ctx.cur_fn, 0);
	// typefn -> paramlist
	node = c3nodechild(*ctx.c3, node, 1);
	for(i = 0; i < c3nodekids(*ctx.c3, node); i += 1){
		param = c3nodechild(*ctx.c3, node, i);
		tag = c3nodetag(*ctx.c3, param);
		if(tag == C3_COMPTIME)
			param = c3nodechild(*ctx.c3, param, 0);
		if(param == index){
			if(ctx.inlined_args)
				return ctx.inlined_args[i];
			return c3append(ctx.c3, C3_IR_ARG, 1, i);
		}
	}
	ERROR("ICE: Param not found", 0);
	return -1;
}

static uint32_t
s_panic(uint32_t index)
{
	uint32_t arg = c3nodechild(*ctx.c3, index, 0);
	if(c3nodekids(*ctx.c3, arg) != 1){
		ERROR("Expected 1 argument to @panic!", 0);
		return -1;
	};
	arg = c3nodechild(*ctx.c3, arg, 0);
	if(c3nodetag(*ctx.c3, arg) != C3_LITERAL_STRING){
		ERROR("Expected string literal argument to @panic!", 0);
		return -1;
	}
	return c3append(ctx.c3, C3_IR_PANIC, 1, arg);
}

// if list is non-null, and analysis succeeds, the IR for the resulting expression is appended given insn list
// the return value is the IR for the generated insn list, -1 on failure, or 0 if there was nothing to generate. Note that for e.g. defer statements, IR is generated but not returned, because the statement itself has no value.
static uint32_t
s_expr(uint32_t index, uint32_t **list)
{
	uint32_t tmp;
	uint32_t ret;
	c3tag tag = c3nodetag(*ctx.c3, index);
	switch(tag){
	case C3_LOOP:{
		uint32_t block;
		struct ctx_block old;
		old = ctx.block;
		ctx.block.stmts = NULL;
		ctx.block.id = ctx.next_block_id;
		ctx.next_block_id += 1;
		tmp = s_expr(c3nodechild(*ctx.c3, index, 0), &ctx.block.stmts);
		if(tmp == -1){
			ERROR("Failed to analyze loop condition", 0);
			ctx.block = old;
			return -1;
		}
		tmp = c3append(ctx.c3, C3_IR_INVERT, 1, tmp);
		if(tmp == -1 || !stb_sb_push_u32(&ctx.block.stmts, tmp))
			OOM();
		tmp = c3append(ctx.c3, C3_IR_COND_BR, 1, tmp);
		if(tmp == -1 || !stb_sb_push_u32(&ctx.block.stmts, tmp))
			OOM();
		tmp = l_block(c3nodechild(*ctx.c3, index, 1), 0);
		if(tmp == -1 || !stb_sb_push_u32(&ctx.block.stmts, tmp))
			OOM();
		tmp = c3append(ctx.c3, C3_IR_RELOOP, 0);
		if(tmp == -1 || !stb_sb_push_u32(&ctx.block.stmts, tmp))
			OOM();
		block = c3appendarr(ctx.c3, C3_BLOCK, stb_sb_count(ctx.block.stmts), ctx.block.stmts);
		stb_sb_free(ctx.block.stmts);
		ctx.block = old;
		ret = block;
		break;}
	case C3_EXPR_CALL:
		ret = s_call(index);
		break;
	case C3_BLOCK:
		return l_block(index, 0);
	case C3_EXPR_DEFER:
		return s_defer(index);
	case C3_IDENT:
	case C3_EXPR_MEMBER:
		ERROR("ICE: ident / field lookup found during expr analysis", 0);
		return -1;
	case C3_TYPE_UNRES:
		ERROR("ICE: type not resolved during semantic extraction", 0);
		return -1;
	case C3_VALUE_U32:
	case C3_VALUE_TRUE:
	case C3_VALUE_FALSE:
	case C3_TYPE_COMPTIME_INT:
	case C3_TYPE_U32:
	case C3_LINT:
	case C3_LITERAL_STRING:
		return index;
	case C3_BUILTIN_PANIC:
		ret = s_panic(index);
		break;
	case C3_BAD:
		ERROR("ICE: bad tag '%s' received in expression analysis", c3tagstr(tag));
		return -1;
	case C3_ASM:
		ret = s_asm(index);
		break;
	case C3_VAR:
	case C3_CONST:
		ret = s_decl(index);
		break;
	case C3_EXPR_ASSIGN:
		ret = s_binop(index, C3_IR_SET);
		break;
	case C3_EXPR_ASSIGN_PLUS:
	case C3_EXPR_ASSIGN_MINUS:
		tmp = tag == C3_EXPR_ASSIGN_PLUS;
		ret = s_binop(index, tmp ? C3_IR_ADD : C3_IR_SUB);
		if(ret == -1)
			ERROR("Failed to analyze %s", c3tagstr(tag));
		if(!stb_sb_push_u32(list, ret))
			OOM();
		ret = c3append(ctx.c3, C3_IR_SET, 2, c3nodechild(*ctx.c3, ret, 0), ret);
		break;
	case C3_EXPR_BITWISE_OR:
		ret = s_binop(index, C3_IR_BITWISE_OR);
		break;
	case C3_EXPR_BITSHIFT_LEFT:
		ret = s_binop(index, C3_IR_BITSHIFT_LEFT);
		break;
	case C3_EXPR_ADD:
		ret = s_binop(index, C3_IR_ADD);
		break;
	case C3_EXPR_SUB:
		ret = s_binop(index, C3_IR_SUB);
		break;
	case C3_INITIALIZED:
		ret = s_initialized(index);
		break;
	case C3_FN_PARAM:
		return s_param(index);
	case C3_BREAK:
		ret = s_break(index, C3_IR_BREAK);
		break;
	case C3_EXPR_RETURN:
		ret = s_break(index, C3_IR_RETURN);
		break;
	case C3_EXPR_IF:
		ret = s_if(index);
		break;
	case C3_DISCARD:
		return 0;
	default:
		ERROR("TODO: analyze expr tag %s", c3tagstr(tag));
		return -1;
	}
	if(ret != -1 && ret != 0){
		if(tag == C3_IDENT){
			FATAL("ICE: ident ret", 0);
		}
		if(!stb_sb_push_u32(list, ret))
			OOM();
	}
	if(ret == -1)
		ERROR("Failed to analyze %s", c3tagstr(tag));
	return ret;
}

static int
l_inject_deferred(void)
{
	uint32_t i;
	i = stb_sb_count(ctx.block.deferred);
	while(i > 0){
		i -= 1;
		if(!stb_sb_push_u32(&ctx.block.stmts, ctx.block.deferred[i])){
			ERROR("ICE: OOM", 0);
			return 0;
		}
	}
	return 1;
}

static int
s_stmt(uint32_t index)
{
	return s_expr(index, &ctx.block.stmts) != -1;
}

//types: 0 normal, 1 root of fn, 2 inline
static uint32_t
l_block(uint32_t index, int type)
{
	uint32_t i, j;
	uint32_t block;
	c3tag tag;
	// cache of previous context
	struct ctx_block old;
	old = ctx.block;
	ctx.block.stmts = NULL;
	//TODO: use index into per-function array instead so all control flow handles deferred statements correctly
	ctx.block.deferred = NULL;
	ctx.block.id = ctx.next_block_id;
	ctx.next_block_id += 1;
	if(type == 1){
		uint32_t type = c3nodechild(*ctx.c3, ctx.cur_fn, 0);
		if(c3nodetag(*ctx.c3, type) == C3_VOID){
			i = c3append(ctx.c3, C3_IR_RETURN, 0);
			if(i == -1 || !stb_sb_push_u32(&ctx.block.deferred, i))
				OOM();
		}
	}
	tag = c3nodetag(*ctx.c3, index);
	if(tag != C3_BLOCK){
		ERROR("Expected block, found %s", c3tagstr(tag));
		abort();
		return -1;
	}
	for(i = 1; i < c3nodekids(*ctx.c3, index); i += 1){
		if(c3nodetag(*ctx.c3, c3nodechild(*ctx.c3, index, i)) == C3_EXPR_RETURN && type == 2){
			j = s_expr(c3nodechild(*ctx.c3, c3nodechild(*ctx.c3, index, i), 1), &ctx.block.stmts);
			if(j == -1){
				ERROR("Failed to analyze inline return expr", 0);
				return -1;
			}
			j = c3append(ctx.c3, C3_IR_BREAK, 2, ctx.block.id, j);
			if(j == -1 || !stb_sb_push_u32(&ctx.block.stmts, j))
				OOM();
			continue;
		}
		if(!s_stmt(ctx.c3->ast.nodes[index + 1 + i].node_index)){
			ERROR("Failed to lower statement %s", c3tagstr(c3nodetag(*ctx.c3, ctx.c3->ast.nodes[index + 1 + i].node_index)));
			return -1;
		}
	}
	l_inject_deferred();
	block = c3appendarr(ctx.c3, C3_BLOCK, stb_sb_count(ctx.block.stmts), ctx.block.stmts);
	stb_sb_free(ctx.block.stmts);
	stb_sb_free(ctx.block.deferred);
	ctx.block = old;
	if(block == -1)
		OOM();
	return block;
}

static uint32_t
s_decl(uint32_t index)
{
	uint32_t tmp;
	tmp = s_expr(c3nodechild(*ctx.c3, index, 2), &ctx.block.stmts);
	if(tmp == -1)
		return -1;
	if(tmp == 0)
		return 0;
	// Replace the value with the new one
	ctx.c3->ast.nodes[index + 3].node_index = tmp;
	if(c3nodetag(*ctx.c3, index) == C3_VAR){
		index = c3append(ctx.c3, C3_IR_SET, 2, index, tmp);
		if(index == -1){
			ERROR("ICE: OOM", 0);
			return -1;
		}
		return index;
	}
	return 0;
}

uint32_t
l_fn(uint32_t index)
{
	uint32_t tmp;
	ctx.next_block_id = 0;
	ctx.cur_fn = index;
	if(!stb_sb_push_u32(&ctx.fn_map.values, index))
		OOM();
	tmp = l_block(c3nodechild(*ctx.c3, index, 1), 1);
	if(tmp == -1){
		ERROR("Failed to lower function block", 0);
		return -1;
	}
	index = c3append(ctx.c3, C3_IR_FN, 2, tmp, index);
	if(!stb_sb_push_u32(&ctx.fn_map.irs, index))
		OOM();
	return index;
}

int
expr_gen_test(void)
{
	uint32_t *kids = NULL;
	uint32_t i;
	uint32_t call;
	uint32_t block;
	uint32_t args = c3append(ctx.c3, C3_EXPR_LIST,0);
	uint32_t rettype, callconv, param_list, fntype;
	uint32_t main = c3intern(ctx.c3, "main", 4);
	main = c3append(ctx.c3, C3_IDENT, 1, main);
	rettype = t_dedup_tag(C3_VOID);
	callconv = t_dedup_tag(C3_CALL_CONV_INLINE);
	param_list = c3append(ctx.c3, C3_FN_PARAM_LIST, 0);
	if(rettype == -1 || callconv == -1 || param_list == -1 || args == -1)
	OOM();
	fntype = c3append(ctx.c3, C3_TYPE_FN, 3, rettype, param_list, callconv);
	if(!stb_sb_push_u32(&kids, rettype) || fntype == -1 )
		OOM();
	uint32_t *tests = ctx.c3->analysis.tests;
	for(i = 0; i < stb_sb_count(tests); i += 1){
		call = c3append(ctx.c3, C3_EXPR_CALL, 2, c3nodechild(*ctx.c3, tests[i], 0), args);
		if(call == -1 || !stb_sb_push_u32(&kids, call))
			OOM();
	}
	block = c3appendarr(ctx.c3, C3_BLOCK, stb_sb_count(kids), kids);
	if(block == -1)
		OOM();
	ERROR("TODO: reimplement test generation", 0);
	return 0;
}

static void
ctx_init(c3ctx *c3)
{
	ctx.fn_refs = NULL;
	ctx.fn_map.irs = NULL;
	ctx.fn_map.values = NULL;
	ctx.c3 = c3;
	ctx.inlined_args = NULL;
}

int
expr_init(c3ctx *c3)
{
	ctx_init(c3);
	return 1;
}

int
expr_finish(void)
{
	uint32_t i;
	uint32_t index, rindex;
	uint16_t kids;
	uint32_t *fns = ctx.c3->analysis.fns;
	kids = stb_sb_count(fns);
	for(i = 0; i < kids; i += 1){
		index = fns[i];
		// Only map functions with bodies (non-external)
		if(c3nodechild(*ctx.c3, c3nodechild(*ctx.c3, index, 0), 1) == -1)
			continue;
		rindex = s_map(c3nodechild(*ctx.c3, index, 0));
		if(rindex == -1){
			ERROR("ICE: unable to map function from CFG to IR; index %u: %u", index, c3nodechild(*ctx.c3, index, 0));
			c3nodedump(*ctx.c3, rindex, 0, 1);
			return 0;
		}
		ctx.c3->ast.nodes[index + 1].node_index = rindex;
	}
	for(i = 0; i < stb_sb_count(ctx.fn_refs); i += 1){
		rindex = ctx.fn_refs[i];
		index = ctx.c3->ast.nodes[rindex].node_index;
		ctx.c3->ast.nodes[index].node_index = s_map(index);
		if(ctx.c3->ast.nodes[index].node_index == -1){
			ERROR("ICE: unable to map function from child index %d to IR", ctx.c3->ast.nodes[index]);
			return 0;
		}
	}
	stb_sb_free(ctx.fn_refs);
	stb_sb_free(ctx.fn_map.irs);
	stb_sb_free(ctx.fn_map.values);
	return 1;
}