aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/c.zig
blob: af8d2d272d2dc235945983dedf12fa96ebd1bc6e (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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
const std = @import("std");
const assert = std.debug.assert;
const mem = std.mem;
const log = std.log.scoped(.c);

const link = @import("../link.zig");
const Module = @import("../Module.zig");
const Compilation = @import("../Compilation.zig");
const ir = @import("../ir.zig");
const Inst = ir.Inst;
const Value = @import("../value.zig").Value;
const Type = @import("../type.zig").Type;
const TypedValue = @import("../TypedValue.zig");
const C = link.File.C;
const Decl = Module.Decl;
const trace = @import("../tracy.zig").trace;

const Mutability = enum { Const, Mut };

pub const CValue = union(enum) {
    none: void,
    /// Index into local_names
    local: usize,
    /// Index into local_names, but take the address.
    local_ref: usize,
    /// A constant instruction, to be rendered inline.
    constant: *Inst,
    /// Index into the parameters
    arg: usize,
    /// By-value
    decl: *Decl,
};

pub const CValueMap = std.AutoHashMap(*Inst, CValue);
pub const TypedefMap = std.HashMap(Type, struct { name: []const u8, rendered: []u8 }, Type.hash, Type.eql, std.hash_map.default_max_load_percentage);

fn formatTypeAsCIdentifier(
    data: Type,
    comptime fmt: []const u8,
    options: std.fmt.FormatOptions,
    writer: anytype,
) !void {
    var buffer = [1]u8{0} ** 128;
    // We don't care if it gets cut off, it's still more unique than a number
    var buf = std.fmt.bufPrint(&buffer, "{}", .{data}) catch &buffer;

    for (buf) |c, i| {
        switch (c) {
            0 => return writer.writeAll(buf[0..i]),
            'a'...'z', 'A'...'Z', '_', '$' => {},
            '0'...'9' => if (i == 0) {
                buf[i] = '_';
            },
            else => buf[i] = '_',
        }
    }
    return writer.writeAll(buf);
}

pub fn typeToCIdentifier(t: Type) std.fmt.Formatter(formatTypeAsCIdentifier) {
    return .{ .data = t };
}

/// This data is available when outputting .c code for a Module.
/// It is not available when generating .h file.
pub const Object = struct {
    dg: DeclGen,
    gpa: *mem.Allocator,
    code: std.ArrayList(u8),
    value_map: CValueMap,
    next_arg_index: usize = 0,
    next_local_index: usize = 0,
    next_block_index: usize = 0,
    indent_writer: IndentWriter(std.ArrayList(u8).Writer),

    fn resolveInst(o: *Object, inst: *Inst) !CValue {
        if (inst.value()) |_| {
            return CValue{ .constant = inst };
        }
        return o.value_map.get(inst).?; // Instruction does not dominate all uses!
    }

    fn allocLocalValue(o: *Object) CValue {
        const result = o.next_local_index;
        o.next_local_index += 1;
        return .{ .local = result };
    }

    fn allocLocal(o: *Object, ty: Type, mutability: Mutability) !CValue {
        const local_value = o.allocLocalValue();
        try o.renderTypeAndName(o.writer(), ty, local_value, mutability);
        return local_value;
    }

    fn writer(o: *Object) IndentWriter(std.ArrayList(u8).Writer).Writer {
        return o.indent_writer.writer();
    }

    fn writeCValue(o: *Object, w: anytype, c_value: CValue) !void {
        switch (c_value) {
            .none => unreachable,
            .local => |i| return w.print("t{d}", .{i}),
            .local_ref => |i| return w.print("&t{d}", .{i}),
            .constant => |inst| return o.dg.renderValue(w, inst.ty, inst.value().?),
            .arg => |i| return w.print("a{d}", .{i}),
            .decl => |decl| return w.writeAll(mem.span(decl.name)),
        }
    }

    fn renderTypeAndName(
        o: *Object,
        w: anytype,
        ty: Type,
        name: CValue,
        mutability: Mutability,
    ) error{ OutOfMemory, AnalysisFail }!void {
        var suffix = std.ArrayList(u8).init(o.gpa);
        defer suffix.deinit();

        var render_ty = ty;
        while (render_ty.zigTypeTag() == .Array) {
            const sentinel_bit = @boolToInt(render_ty.sentinel() != null);
            const c_len = render_ty.arrayLen() + sentinel_bit;
            try suffix.writer().print("[{d}]", .{c_len});
            render_ty = render_ty.elemType();
        }

        try o.dg.renderType(w, render_ty);

        const const_prefix = switch (mutability) {
            .Const => "const ",
            .Mut => "",
        };
        try w.print(" {s}", .{const_prefix});
        try o.writeCValue(w, name);
        try w.writeAll(suffix.items);
    }
};

/// This data is available both when outputting .c code and when outputting an .h file.
pub const DeclGen = struct {
    module: *Module,
    decl: *Decl,
    fwd_decl: std.ArrayList(u8),
    error_msg: ?*Module.ErrorMsg,
    typedefs: TypedefMap,

    fn fail(dg: *DeclGen, src: usize, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } {
        dg.error_msg = try Module.ErrorMsg.create(dg.module.gpa, .{
            .file_scope = dg.decl.getFileScope(),
            .byte_offset = src,
        }, format, args);
        return error.AnalysisFail;
    }

    fn renderValue(
        dg: *DeclGen,
        writer: anytype,
        t: Type,
        val: Value,
    ) error{ OutOfMemory, AnalysisFail }!void {
        if (val.isUndef()) {
            return dg.fail(dg.decl.src(), "TODO: C backend: properly handle undefined in all cases (with debug safety?)", .{});
        }
        switch (t.zigTypeTag()) {
            .Int => {
                if (t.isSignedInt())
                    return writer.print("{d}", .{val.toSignedInt()});
                return writer.print("{d}", .{val.toUnsignedInt()});
            },
            .Pointer => switch (val.tag()) {
                .null_value, .zero => try writer.writeAll("NULL"),
                .one => try writer.writeAll("1"),
                .decl_ref => {
                    const decl = val.castTag(.decl_ref).?.data;

                    // Determine if we must pointer cast.
                    const decl_tv = decl.typed_value.most_recent.typed_value;
                    if (t.eql(decl_tv.ty)) {
                        try writer.print("&{s}", .{decl.name});
                    } else {
                        try writer.writeAll("(");
                        try dg.renderType(writer, t);
                        try writer.print(")&{s}", .{decl.name});
                    }
                },
                .function => {
                    const func = val.castTag(.function).?.data;
                    try writer.print("{s}", .{func.owner_decl.name});
                },
                .extern_fn => {
                    const decl = val.castTag(.extern_fn).?.data;
                    try writer.print("{s}", .{decl.name});
                },
                else => |e| return dg.fail(
                    dg.decl.src(),
                    "TODO: C backend: implement Pointer value {s}",
                    .{@tagName(e)},
                ),
            },
            .Array => {
                // First try specific tag representations for more efficiency.
                switch (val.tag()) {
                    .undef, .empty_struct_value, .empty_array => try writer.writeAll("{}"),
                    .bytes => {
                        const bytes = val.castTag(.bytes).?.data;
                        // TODO: make our own C string escape instead of using std.zig.fmtEscapes
                        try writer.print("\"{}\"", .{std.zig.fmtEscapes(bytes)});
                    },
                    else => {
                        // Fall back to generic implementation.
                        var arena = std.heap.ArenaAllocator.init(dg.module.gpa);
                        defer arena.deinit();

                        try writer.writeAll("{");
                        var index: usize = 0;
                        const len = t.arrayLen();
                        const elem_ty = t.elemType();
                        while (index < len) : (index += 1) {
                            if (index != 0) try writer.writeAll(",");
                            const elem_val = try val.elemValue(&arena.allocator, index);
                            try dg.renderValue(writer, elem_ty, elem_val);
                        }
                        if (t.sentinel()) |sentinel_val| {
                            if (index != 0) try writer.writeAll(",");
                            try dg.renderValue(writer, elem_ty, sentinel_val);
                        }
                        try writer.writeAll("}");
                    },
                }
            },
            .Bool => return writer.print("{}", .{val.toBool()}),
            .Optional => {
                var opt_buf: Type.Payload.ElemType = undefined;
                const child_type = t.optionalChild(&opt_buf);
                if (t.isPtrLikeOptional()) {
                    return dg.renderValue(writer, child_type, val);
                }
                try writer.writeByte('(');
                try dg.renderType(writer, t);
                if (val.tag() == .null_value) {
                    try writer.writeAll("){ .is_null = true }");
                } else {
                    try writer.writeAll("){ .is_null = false, .payload = ");
                    try dg.renderValue(writer, child_type, val);
                    try writer.writeAll(" }");
                }
            },
            .ErrorSet => {
                const payload = val.castTag(.@"error").?;
                // error values will be #defined at the top of the file
                return writer.print("zig_error_{s}", .{payload.data.name});
            },
            .ErrorUnion => {
                const error_type = t.errorUnionSet();
                const payload_type = t.errorUnionChild();
                const data = val.castTag(.error_union).?.data;
                try writer.writeByte('(');
                try dg.renderType(writer, t);
                try writer.writeAll("){");
                if (val.getError()) |_| {
                    try writer.writeAll(" .error = ");
                    try dg.renderValue(
                        writer,
                        error_type,
                        data,
                    );
                    try writer.writeAll(" }");
                } else {
                    try writer.writeAll(" .payload = ");
                    try dg.renderValue(
                        writer,
                        payload_type,
                        data,
                    );
                    try writer.writeAll(", .error = 0 }");
                }
            },
            else => |e| return dg.fail(dg.decl.src(), "TODO: C backend: implement value {s}", .{
                @tagName(e),
            }),
        }
    }

    fn renderFunctionSignature(dg: *DeclGen, w: anytype, is_global: bool) !void {
        if (!is_global) {
            try w.writeAll("static ");
        }
        const tv = dg.decl.typed_value.most_recent.typed_value;
        try dg.renderType(w, tv.ty.fnReturnType());
        const decl_name = mem.span(dg.decl.name);
        try w.print(" {s}(", .{decl_name});
        const param_len = tv.ty.fnParamLen();
        const is_var_args = tv.ty.fnIsVarArgs();
        if (param_len == 0 and !is_var_args)
            try w.writeAll("void")
        else {
            var index: usize = 0;
            while (index < param_len) : (index += 1) {
                if (index > 0) {
                    try w.writeAll(", ");
                }
                try dg.renderType(w, tv.ty.fnParamType(index));
                try w.print(" a{d}", .{index});
            }
        }
        if (is_var_args) {
            if (param_len != 0) try w.writeAll(", ");
            try w.writeAll("...");
        }
        try w.writeByte(')');
    }

    fn renderType(dg: *DeclGen, w: anytype, t: Type) error{ OutOfMemory, AnalysisFail }!void {
        switch (t.zigTypeTag()) {
            .NoReturn => {
                try w.writeAll("zig_noreturn void");
            },
            .Void => try w.writeAll("void"),
            .Bool => try w.writeAll("bool"),
            .Int => {
                switch (t.tag()) {
                    .u8 => try w.writeAll("uint8_t"),
                    .i8 => try w.writeAll("int8_t"),
                    .u16 => try w.writeAll("uint16_t"),
                    .i16 => try w.writeAll("int16_t"),
                    .u32 => try w.writeAll("uint32_t"),
                    .i32 => try w.writeAll("int32_t"),
                    .u64 => try w.writeAll("uint64_t"),
                    .i64 => try w.writeAll("int64_t"),
                    .usize => try w.writeAll("uintptr_t"),
                    .isize => try w.writeAll("intptr_t"),
                    .c_short => try w.writeAll("short"),
                    .c_ushort => try w.writeAll("unsigned short"),
                    .c_int => try w.writeAll("int"),
                    .c_uint => try w.writeAll("unsigned int"),
                    .c_long => try w.writeAll("long"),
                    .c_ulong => try w.writeAll("unsigned long"),
                    .c_longlong => try w.writeAll("long long"),
                    .c_ulonglong => try w.writeAll("unsigned long long"),
                    .int_signed, .int_unsigned => {
                        const info = t.intInfo(dg.module.getTarget());
                        const sign_prefix = switch (info.signedness) {
                            .signed => "",
                            .unsigned => "u",
                        };
                        inline for (.{ 8, 16, 32, 64, 128 }) |nbits| {
                            if (info.bits <= nbits) {
                                try w.print("{s}int{d}_t", .{ sign_prefix, nbits });
                                break;
                            }
                        } else {
                            return dg.fail(dg.decl.src(), "TODO: C backend: implement integer types larger than 128 bits", .{});
                        }
                    },
                    else => unreachable,
                }
            },
            .Pointer => {
                if (t.isSlice()) {
                    return dg.fail(dg.decl.src(), "TODO: C backend: implement slices", .{});
                } else {
                    try dg.renderType(w, t.elemType());
                    try w.writeAll(" *");
                    if (t.isConstPtr()) {
                        try w.writeAll("const ");
                    }
                    if (t.isVolatilePtr()) {
                        try w.writeAll("volatile ");
                    }
                }
            },
            .Array => {
                try dg.renderType(w, t.elemType());
                try w.writeAll(" *");
            },
            .Optional => {
                var opt_buf: Type.Payload.ElemType = undefined;
                const child_type = t.optionalChild(&opt_buf);
                if (t.isPtrLikeOptional()) {
                    return dg.renderType(w, child_type);
                } else if (dg.typedefs.get(t)) |some| {
                    return w.writeAll(some.name);
                }

                var buffer = std.ArrayList(u8).init(dg.typedefs.allocator);
                defer buffer.deinit();
                const bw = buffer.writer();

                try bw.writeAll("typedef struct { ");
                try dg.renderType(bw, child_type);
                try bw.writeAll(" payload; bool is_null; } ");
                const name_index = buffer.items.len;
                try bw.print("zig_opt_{s}_t;\n", .{typeToCIdentifier(child_type)});

                const rendered = buffer.toOwnedSlice();
                errdefer dg.typedefs.allocator.free(rendered);
                const name = rendered[name_index .. rendered.len - 2];

                try dg.typedefs.ensureCapacity(dg.typedefs.capacity() + 1);
                try w.writeAll(name);
                dg.typedefs.putAssumeCapacityNoClobber(t, .{ .name = name, .rendered = rendered });
            },
            .ErrorSet => {
                comptime std.debug.assert(Type.initTag(.anyerror).abiSize(std.Target.current) == 2);
                try w.writeAll("uint16_t");
            },
            .ErrorUnion => {
                if (dg.typedefs.get(t)) |some| {
                    return w.writeAll(some.name);
                }
                const child_type = t.errorUnionChild();
                const set_type = t.errorUnionSet();

                var buffer = std.ArrayList(u8).init(dg.typedefs.allocator);
                defer buffer.deinit();
                const bw = buffer.writer();

                try bw.writeAll("typedef struct { ");
                try dg.renderType(bw, child_type);
                try bw.writeAll(" payload; uint16_t error; } ");
                const name_index = buffer.items.len;
                try bw.print("zig_err_union_{s}_{s}_t;\n", .{ typeToCIdentifier(set_type), typeToCIdentifier(child_type) });

                const rendered = buffer.toOwnedSlice();
                errdefer dg.typedefs.allocator.free(rendered);
                const name = rendered[name_index .. rendered.len - 2];

                try dg.typedefs.ensureCapacity(dg.typedefs.capacity() + 1);
                try w.writeAll(name);
                dg.typedefs.putAssumeCapacityNoClobber(t, .{ .name = name, .rendered = rendered });
            },
            .Null, .Undefined => unreachable, // must be const or comptime
            else => |e| return dg.fail(dg.decl.src(), "TODO: C backend: implement type {s}", .{
                @tagName(e),
            }),
        }
    }

    fn functionIsGlobal(dg: *DeclGen, tv: TypedValue) bool {
        switch (tv.val.tag()) {
            .extern_fn => return true,
            .function => {
                const func = tv.val.castTag(.function).?.data;
                return dg.module.decl_exports.contains(func.owner_decl);
            },
            else => unreachable,
        }
    }
};

pub fn genDecl(o: *Object) !void {
    const tracy = trace(@src());
    defer tracy.end();

    const tv = o.dg.decl.typed_value.most_recent.typed_value;

    if (tv.val.castTag(.function)) |func_payload| {
        const is_global = o.dg.functionIsGlobal(tv);
        const fwd_decl_writer = o.dg.fwd_decl.writer();
        if (is_global) {
            try fwd_decl_writer.writeAll("ZIG_EXTERN_C ");
        }
        try o.dg.renderFunctionSignature(fwd_decl_writer, is_global);
        try fwd_decl_writer.writeAll(";\n");

        const func: *Module.Fn = func_payload.data;
        try o.indent_writer.insertNewline();
        try o.dg.renderFunctionSignature(o.writer(), is_global);

        try o.writer().writeByte(' ');
        try genBody(o, func.body);

        try o.indent_writer.insertNewline();
    } else if (tv.val.tag() == .extern_fn) {
        const writer = o.writer();
        try writer.writeAll("ZIG_EXTERN_C ");
        try o.dg.renderFunctionSignature(writer, true);
        try writer.writeAll(";\n");
    } else {
        const writer = o.writer();
        try writer.writeAll("static ");

        // TODO ask the Decl if it is const
        // https://github.com/ziglang/zig/issues/7582

        const decl_c_value: CValue = .{ .decl = o.dg.decl };
        try o.renderTypeAndName(writer, tv.ty, decl_c_value, .Mut);

        try writer.writeAll(" = ");
        try o.dg.renderValue(writer, tv.ty, tv.val);
        try writer.writeAll(";\n");
    }
}

pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void {
    const tracy = trace(@src());
    defer tracy.end();

    const tv = dg.decl.typed_value.most_recent.typed_value;
    const writer = dg.fwd_decl.writer();

    switch (tv.ty.zigTypeTag()) {
        .Fn => {
            const is_global = dg.functionIsGlobal(tv);
            if (is_global) {
                try writer.writeAll("ZIG_EXTERN_C ");
            }
            try dg.renderFunctionSignature(writer, is_global);
            try dg.fwd_decl.appendSlice(";\n");
        },
        else => {},
    }
}

pub fn genBody(o: *Object, body: ir.Body) error{ AnalysisFail, OutOfMemory }!void {
    const writer = o.writer();
    if (body.instructions.len == 0) {
        try writer.writeAll("{}");
        return;
    }

    try writer.writeAll("{\n");
    o.indent_writer.pushIndent();

    for (body.instructions) |inst| {
        const result_value = switch (inst.tag) {
            .constant => unreachable, // excluded from function bodies
            .add => try genBinOp(o, inst.castTag(.add).?, " + "),
            .alloc => try genAlloc(o, inst.castTag(.alloc).?),
            .arg => genArg(o),
            .assembly => try genAsm(o, inst.castTag(.assembly).?),
            .block => try genBlock(o, inst.castTag(.block).?),
            .bitcast => try genBitcast(o, inst.castTag(.bitcast).?),
            .breakpoint => try genBreakpoint(o, inst.castTag(.breakpoint).?),
            .call => try genCall(o, inst.castTag(.call).?),
            .cmp_eq => try genBinOp(o, inst.castTag(.cmp_eq).?, " == "),
            .cmp_gt => try genBinOp(o, inst.castTag(.cmp_gt).?, " > "),
            .cmp_gte => try genBinOp(o, inst.castTag(.cmp_gte).?, " >= "),
            .cmp_lt => try genBinOp(o, inst.castTag(.cmp_lt).?, " < "),
            .cmp_lte => try genBinOp(o, inst.castTag(.cmp_lte).?, " <= "),
            .cmp_neq => try genBinOp(o, inst.castTag(.cmp_neq).?, " != "),
            .dbg_stmt => try genDbgStmt(o, inst.castTag(.dbg_stmt).?),
            .intcast => try genIntCast(o, inst.castTag(.intcast).?),
            .load => try genLoad(o, inst.castTag(.load).?),
            .ret => try genRet(o, inst.castTag(.ret).?),
            .retvoid => try genRetVoid(o),
            .store => try genStore(o, inst.castTag(.store).?),
            .sub => try genBinOp(o, inst.castTag(.sub).?, " - "),
            .unreach => try genUnreach(o, inst.castTag(.unreach).?),
            .loop => try genLoop(o, inst.castTag(.loop).?),
            .condbr => try genCondBr(o, inst.castTag(.condbr).?),
            .br => try genBr(o, inst.castTag(.br).?),
            .br_void => try genBrVoid(o, inst.castTag(.br_void).?.block),
            .switchbr => try genSwitchBr(o, inst.castTag(.switchbr).?),
            // bool_and and bool_or are non-short-circuit operations
            .bool_and => try genBinOp(o, inst.castTag(.bool_and).?, " & "),
            .bool_or => try genBinOp(o, inst.castTag(.bool_or).?, " | "),
            .bit_and => try genBinOp(o, inst.castTag(.bit_and).?, " & "),
            .bit_or => try genBinOp(o, inst.castTag(.bit_or).?, " | "),
            .xor => try genBinOp(o, inst.castTag(.xor).?, " ^ "),
            .not => try genUnOp(o, inst.castTag(.not).?, "!"),
            .is_null => try genIsNull(o, inst.castTag(.is_null).?),
            .is_non_null => try genIsNull(o, inst.castTag(.is_non_null).?),
            .is_null_ptr => try genIsNull(o, inst.castTag(.is_null_ptr).?),
            .is_non_null_ptr => try genIsNull(o, inst.castTag(.is_non_null_ptr).?),
            .wrap_optional => try genWrapOptional(o, inst.castTag(.wrap_optional).?),
            .optional_payload => try genOptionalPayload(o, inst.castTag(.optional_payload).?),
            .optional_payload_ptr => try genOptionalPayload(o, inst.castTag(.optional_payload_ptr).?),
            .is_err => try genIsErr(o, inst.castTag(.is_err).?),
            .is_err_ptr => try genIsErr(o, inst.castTag(.is_err_ptr).?),
            .unwrap_errunion_payload => try genUnwrapErrUnionPay(o, inst.castTag(.unwrap_errunion_payload).?),
            .unwrap_errunion_err => try genUnwrapErrUnionErr(o, inst.castTag(.unwrap_errunion_err).?),
            .unwrap_errunion_payload_ptr => try genUnwrapErrUnionPay(o, inst.castTag(.unwrap_errunion_payload_ptr).?),
            .unwrap_errunion_err_ptr => try genUnwrapErrUnionErr(o, inst.castTag(.unwrap_errunion_err_ptr).?),
            .wrap_errunion_payload => try genWrapErrUnionPay(o, inst.castTag(.wrap_errunion_payload).?),
            .wrap_errunion_err => try genWrapErrUnionErr(o, inst.castTag(.wrap_errunion_err).?),
            else => |e| return o.dg.fail(o.dg.decl.src(), "TODO: C backend: implement codegen for {}", .{e}),
        };
        switch (result_value) {
            .none => {},
            else => try o.value_map.putNoClobber(inst, result_value),
        }
    }

    o.indent_writer.popIndent();
    try writer.writeAll("}");
}

fn genAlloc(o: *Object, alloc: *Inst.NoOp) !CValue {
    const writer = o.writer();

    // First line: the variable used as data storage.
    const elem_type = alloc.base.ty.elemType();
    const mutability: Mutability = if (alloc.base.ty.isConstPtr()) .Const else .Mut;
    const local = try o.allocLocal(elem_type, mutability);
    try writer.writeAll(";\n");

    return CValue{ .local_ref = local.local };
}

fn genArg(o: *Object) CValue {
    const i = o.next_arg_index;
    o.next_arg_index += 1;
    return .{ .arg = i };
}

fn genRetVoid(o: *Object) !CValue {
    try o.writer().print("return;\n", .{});
    return CValue.none;
}

fn genLoad(o: *Object, inst: *Inst.UnOp) !CValue {
    const operand = try o.resolveInst(inst.operand);
    const writer = o.writer();
    const local = try o.allocLocal(inst.base.ty, .Const);
    switch (operand) {
        .local_ref => |i| {
            const wrapped: CValue = .{ .local = i };
            try writer.writeAll(" = ");
            try o.writeCValue(writer, wrapped);
            try writer.writeAll(";\n");
        },
        else => {
            try writer.writeAll(" = *");
            try o.writeCValue(writer, operand);
            try writer.writeAll(";\n");
        },
    }
    return local;
}

fn genRet(o: *Object, inst: *Inst.UnOp) !CValue {
    const operand = try o.resolveInst(inst.operand);
    const writer = o.writer();
    try writer.writeAll("return ");
    try o.writeCValue(writer, operand);
    try writer.writeAll(";\n");
    return CValue.none;
}

fn genIntCast(o: *Object, inst: *Inst.UnOp) !CValue {
    if (inst.base.isUnused())
        return CValue.none;

    const from = try o.resolveInst(inst.operand);

    const writer = o.writer();
    const local = try o.allocLocal(inst.base.ty, .Const);
    try writer.writeAll(" = (");
    try o.dg.renderType(writer, inst.base.ty);
    try writer.writeAll(")");
    try o.writeCValue(writer, from);
    try writer.writeAll(";\n");
    return local;
}

fn genStore(o: *Object, inst: *Inst.BinOp) !CValue {
    // *a = b;
    const dest_ptr = try o.resolveInst(inst.lhs);
    const src_val = try o.resolveInst(inst.rhs);

    const writer = o.writer();
    switch (dest_ptr) {
        .local_ref => |i| {
            const dest: CValue = .{ .local = i };
            try o.writeCValue(writer, dest);
            try writer.writeAll(" = ");
            try o.writeCValue(writer, src_val);
            try writer.writeAll(";\n");
        },
        else => {
            try writer.writeAll("*");
            try o.writeCValue(writer, dest_ptr);
            try writer.writeAll(" = ");
            try o.writeCValue(writer, src_val);
            try writer.writeAll(";\n");
        },
    }
    return CValue.none;
}

fn genBinOp(o: *Object, inst: *Inst.BinOp, operator: []const u8) !CValue {
    if (inst.base.isUnused())
        return CValue.none;

    const lhs = try o.resolveInst(inst.lhs);
    const rhs = try o.resolveInst(inst.rhs);

    const writer = o.writer();
    const local = try o.allocLocal(inst.base.ty, .Const);

    try writer.writeAll(" = ");
    try o.writeCValue(writer, lhs);
    try writer.writeAll(operator);
    try o.writeCValue(writer, rhs);
    try writer.writeAll(";\n");

    return local;
}

fn genUnOp(o: *Object, inst: *Inst.UnOp, operator: []const u8) !CValue {
    if (inst.base.isUnused())
        return CValue.none;

    const operand = try o.resolveInst(inst.operand);

    const writer = o.writer();
    const local = try o.allocLocal(inst.base.ty, .Const);

    try writer.print(" = {s}", .{operator});
    try o.writeCValue(writer, operand);
    try writer.writeAll(";\n");

    return local;
}

fn genCall(o: *Object, inst: *Inst.Call) !CValue {
    if (inst.func.castTag(.constant)) |func_inst| {
        const fn_decl = if (func_inst.val.castTag(.extern_fn)) |extern_fn|
            extern_fn.data
        else if (func_inst.val.castTag(.function)) |func_payload|
            func_payload.data.owner_decl
        else
            unreachable;

        const fn_ty = fn_decl.typed_value.most_recent.typed_value.ty;
        const ret_ty = fn_ty.fnReturnType();
        const unused_result = inst.base.isUnused();
        var result_local: CValue = .none;

        const writer = o.writer();
        if (unused_result) {
            if (ret_ty.hasCodeGenBits()) {
                try writer.print("(void)", .{});
            }
        } else {
            result_local = try o.allocLocal(ret_ty, .Const);
            try writer.writeAll(" = ");
        }
        const fn_name = mem.spanZ(fn_decl.name);
        try writer.print("{s}(", .{fn_name});
        if (inst.args.len != 0) {
            for (inst.args) |arg, i| {
                if (i > 0) {
                    try writer.writeAll(", ");
                }
                if (arg.value()) |val| {
                    try o.dg.renderValue(writer, arg.ty, val);
                } else {
                    const val = try o.resolveInst(arg);
                    try o.writeCValue(writer, val);
                }
            }
        }
        try writer.writeAll(");\n");
        return result_local;
    } else {
        return o.dg.fail(o.dg.decl.src(), "TODO: C backend: implement function pointers", .{});
    }
}

fn genDbgStmt(o: *Object, inst: *Inst.NoOp) !CValue {
    // TODO emit #line directive here with line number and filename
    return CValue.none;
}

fn genBlock(o: *Object, inst: *Inst.Block) !CValue {
    const block_id: usize = o.next_block_index;
    o.next_block_index += 1;
    const writer = o.writer();

    // store the block id in relocs.capacity as it is not  used for anything else in the C backend.
    inst.codegen.relocs.capacity = block_id;
    const result = if (inst.base.ty.tag() != .void and !inst.base.isUnused()) blk: {
        // allocate a location for the result
        const local = try o.allocLocal(inst.base.ty, .Mut);
        try writer.writeAll(";\n");
        break :blk local;
    } else CValue{ .none = {} };

    inst.codegen.mcv = @bitCast(@import("../codegen.zig").AnyMCValue, result);
    try genBody(o, inst.body);
    try o.indent_writer.insertNewline();
    // label must be followed by an expression, add an empty one.
    try writer.print("zig_block_{d}:;\n", .{block_id});
    return result;
}

fn genBr(o: *Object, inst: *Inst.Br) !CValue {
    const result = @bitCast(CValue, inst.block.codegen.mcv);
    const writer = o.writer();

    // If result is .none then the value of the block is unused.
    if (inst.operand.ty.tag() != .void and result != .none) {
        const operand = try o.resolveInst(inst.operand);
        try o.writeCValue(writer, result);
        try writer.writeAll(" = ");
        try o.writeCValue(writer, operand);
        try writer.writeAll(";\n");
    }

    return genBrVoid(o, inst.block);
}

fn genBrVoid(o: *Object, block: *Inst.Block) !CValue {
    try o.writer().print("goto zig_block_{d};\n", .{block.codegen.relocs.capacity});
    return CValue.none;
}

fn genBitcast(o: *Object, inst: *Inst.UnOp) !CValue {
    const operand = try o.resolveInst(inst.operand);

    const writer = o.writer();
    if (inst.base.ty.zigTypeTag() == .Pointer and inst.operand.ty.zigTypeTag() == .Pointer) {
        const local = try o.allocLocal(inst.base.ty, .Const);
        try writer.writeAll(" = (");
        try o.dg.renderType(writer, inst.base.ty);

        try writer.writeAll(")");
        try o.writeCValue(writer, operand);
        try writer.writeAll(";\n");
        return local;
    }

    const local = try o.allocLocal(inst.base.ty, .Mut);
    try writer.writeAll(";\n");

    try writer.writeAll("memcpy(&");
    try o.writeCValue(writer, local);
    try writer.writeAll(", &");
    try o.writeCValue(writer, operand);
    try writer.writeAll(", sizeof ");
    try o.writeCValue(writer, local);
    try writer.writeAll(");\n");

    return local;
}

fn genBreakpoint(o: *Object, inst: *Inst.NoOp) !CValue {
    try o.writer().writeAll("zig_breakpoint();\n");
    return CValue.none;
}

fn genUnreach(o: *Object, inst: *Inst.NoOp) !CValue {
    try o.writer().writeAll("zig_unreachable();\n");
    return CValue.none;
}

fn genLoop(o: *Object, inst: *Inst.Loop) !CValue {
    try o.writer().writeAll("while (true) ");
    try genBody(o, inst.body);
    try o.indent_writer.insertNewline();
    return CValue.none;
}

fn genCondBr(o: *Object, inst: *Inst.CondBr) !CValue {
    const cond = try o.resolveInst(inst.condition);
    const writer = o.writer();

    try writer.writeAll("if (");
    try o.writeCValue(writer, cond);
    try writer.writeAll(") ");
    try genBody(o, inst.then_body);
    try writer.writeAll(" else ");
    try genBody(o, inst.else_body);
    try o.indent_writer.insertNewline();

    return CValue.none;
}

fn genSwitchBr(o: *Object, inst: *Inst.SwitchBr) !CValue {
    const target = try o.resolveInst(inst.target);
    const writer = o.writer();

    try writer.writeAll("switch (");
    try o.writeCValue(writer, target);
    try writer.writeAll(") {\n");
    o.indent_writer.pushIndent();

    for (inst.cases) |case| {
        try writer.writeAll("case ");
        try o.dg.renderValue(writer, inst.target.ty, case.item);
        try writer.writeAll(": ");
        // the case body must be noreturn so we don't need to insert a break
        try genBody(o, case.body);
        try o.indent_writer.insertNewline();
    }

    try writer.writeAll("default: ");
    try genBody(o, inst.else_body);
    try o.indent_writer.insertNewline();

    o.indent_writer.popIndent();
    try writer.writeAll("}\n");
    return CValue.none;
}

fn genAsm(o: *Object, as: *Inst.Assembly) !CValue {
    if (as.base.isUnused() and !as.is_volatile)
        return CValue.none;

    const writer = o.writer();
    for (as.inputs) |i, index| {
        if (i[0] == '{' and i[i.len - 1] == '}') {
            const reg = i[1 .. i.len - 1];
            const arg = as.args[index];
            const arg_c_value = try o.resolveInst(arg);
            try writer.writeAll("register ");
            try o.dg.renderType(writer, arg.ty);

            try writer.print(" {s}_constant __asm__(\"{s}\") = ", .{ reg, reg });
            try o.writeCValue(writer, arg_c_value);
            try writer.writeAll(";\n");
        } else {
            return o.dg.fail(o.dg.decl.src(), "TODO non-explicit inline asm regs", .{});
        }
    }
    const volatile_string: []const u8 = if (as.is_volatile) "volatile " else "";
    try writer.print("__asm {s}(\"{s}\"", .{ volatile_string, as.asm_source });
    if (as.output) |_| {
        return o.dg.fail(o.dg.decl.src(), "TODO inline asm output", .{});
    }
    if (as.inputs.len > 0) {
        if (as.output == null) {
            try writer.writeAll(" :");
        }
        try writer.writeAll(": ");
        for (as.inputs) |i, index| {
            if (i[0] == '{' and i[i.len - 1] == '}') {
                const reg = i[1 .. i.len - 1];
                const arg = as.args[index];
                if (index > 0) {
                    try writer.writeAll(", ");
                }
                try writer.print("\"r\"({s}_constant)", .{reg});
            } else {
                // This is blocked by the earlier test
                unreachable;
            }
        }
    }
    try writer.writeAll(");\n");

    if (as.base.isUnused())
        return CValue.none;

    return o.dg.fail(o.dg.decl.src(), "TODO: C backend: inline asm expression result used", .{});
}

fn genIsNull(o: *Object, inst: *Inst.UnOp) !CValue {
    const writer = o.writer();
    const invert_logic = inst.base.tag == .is_non_null or inst.base.tag == .is_non_null_ptr;
    const operator = if (invert_logic) "!=" else "==";
    const maybe_deref = if (inst.base.tag == .is_null_ptr or inst.base.tag == .is_non_null_ptr) "[0]" else "";
    const operand = try o.resolveInst(inst.operand);

    const local = try o.allocLocal(Type.initTag(.bool), .Const);
    try writer.writeAll(" = (");
    try o.writeCValue(writer, operand);

    if (inst.operand.ty.isPtrLikeOptional()) {
        // operand is a regular pointer, test `operand !=/== NULL`
        try writer.print("){s} {s} NULL;\n", .{ maybe_deref, operator });
    } else {
        try writer.print("){s}.is_null {s} true;\n", .{ maybe_deref, operator });
    }
    return local;
}

fn genOptionalPayload(o: *Object, inst: *Inst.UnOp) !CValue {
    const writer = o.writer();
    const operand = try o.resolveInst(inst.operand);

    const opt_ty = if (inst.operand.ty.zigTypeTag() == .Pointer)
        inst.operand.ty.elemType()
    else
        inst.operand.ty;

    if (opt_ty.isPtrLikeOptional()) {
        // the operand is just a regular pointer, no need to do anything special.
        // *?*T -> **T and ?*T -> *T are **T -> **T and *T -> *T in C
        return operand;
    }

    const maybe_deref = if (inst.operand.ty.zigTypeTag() == .Pointer) "->" else ".";
    const maybe_addrof = if (inst.base.ty.zigTypeTag() == .Pointer) "&" else "";

    const local = try o.allocLocal(inst.base.ty, .Const);
    try writer.print(" = {s}(", .{maybe_addrof});
    try o.writeCValue(writer, operand);

    try writer.print("){s}payload;\n", .{maybe_deref});
    return local;
}

// *(E!T) -> E NOT *E
fn genUnwrapErrUnionErr(o: *Object, inst: *Inst.UnOp) !CValue {
    const writer = o.writer();
    const operand = try o.resolveInst(inst.operand);

    const maybe_deref = if (inst.operand.ty.zigTypeTag() == .Pointer) "->" else ".";

    const local = try o.allocLocal(inst.base.ty, .Const);
    try writer.writeAll(" = (");
    try o.writeCValue(writer, operand);

    try writer.print("){s}error;\n", .{maybe_deref});
    return local;
}
fn genUnwrapErrUnionPay(o: *Object, inst: *Inst.UnOp) !CValue {
    const writer = o.writer();
    const operand = try o.resolveInst(inst.operand);

    const maybe_deref = if (inst.operand.ty.zigTypeTag() == .Pointer) "->" else ".";
    const maybe_addrof = if (inst.base.ty.zigTypeTag() == .Pointer) "&" else "";

    const local = try o.allocLocal(inst.base.ty, .Const);
    try writer.print(" = {s}(", .{maybe_addrof});
    try o.writeCValue(writer, operand);

    try writer.print("){s}payload;\n", .{maybe_deref});
    return local;
}

fn genWrapOptional(o: *Object, inst: *Inst.UnOp) !CValue {
    const writer = o.writer();
    const operand = try o.resolveInst(inst.operand);

    if (inst.base.ty.isPtrLikeOptional()) {
        // the operand is just a regular pointer, no need to do anything special.
        return operand;
    }

    // .wrap_optional is used to convert non-optionals into optionals so it can never be null.
    const local = try o.allocLocal(inst.base.ty, .Const);
    try writer.writeAll(" = { .is_null = false, .payload =");
    try o.writeCValue(writer, operand);
    try writer.writeAll("};\n");
    return local;
}
fn genWrapErrUnionErr(o: *Object, inst: *Inst.UnOp) !CValue {
    const writer = o.writer();
    const operand = try o.resolveInst(inst.operand);

    const local = try o.allocLocal(inst.base.ty, .Const);
    try writer.writeAll(" = { .error = ");
    try o.writeCValue(writer, operand);
    try writer.writeAll(" };\n");
    return local;
}
fn genWrapErrUnionPay(o: *Object, inst: *Inst.UnOp) !CValue {
    const writer = o.writer();
    const operand = try o.resolveInst(inst.operand);

    const local = try o.allocLocal(inst.base.ty, .Const);
    try writer.writeAll(" = { .error = 0, .payload = ");
    try o.writeCValue(writer, operand);
    try writer.writeAll(" };\n");
    return local;
}

fn genIsErr(o: *Object, inst: *Inst.UnOp) !CValue {
    const writer = o.writer();
    const maybe_deref = if (inst.base.tag == .is_err_ptr) "[0]" else "";
    const operand = try o.resolveInst(inst.operand);

    const local = try o.allocLocal(Type.initTag(.bool), .Const);
    try writer.writeAll(" = (");
    try o.writeCValue(writer, operand);
    try writer.print("){s}.error != 0;\n", .{maybe_deref});
    return local;
}

fn IndentWriter(comptime UnderlyingWriter: type) type {
    return struct {
        const Self = @This();
        pub const Error = UnderlyingWriter.Error;
        pub const Writer = std.io.Writer(*Self, Error, write);

        pub const indent_delta = 4;

        underlying_writer: UnderlyingWriter,
        indent_count: usize = 0,
        current_line_empty: bool = true,

        pub fn writer(self: *Self) Writer {
            return .{ .context = self };
        }

        pub fn write(self: *Self, bytes: []const u8) Error!usize {
            if (bytes.len == 0) return @as(usize, 0);

            const current_indent = self.indent_count * Self.indent_delta;
            if (self.current_line_empty and current_indent > 0) {
                try self.underlying_writer.writeByteNTimes(' ', current_indent);
            }
            self.current_line_empty = false;

            return self.writeNoIndent(bytes);
        }

        pub fn insertNewline(self: *Self) Error!void {
            _ = try self.writeNoIndent("\n");
        }

        pub fn pushIndent(self: *Self) void {
            self.indent_count += 1;
        }

        pub fn popIndent(self: *Self) void {
            assert(self.indent_count != 0);
            self.indent_count -= 1;
        }

        fn writeNoIndent(self: *Self, bytes: []const u8) Error!usize {
            if (bytes.len == 0) return @as(usize, 0);

            try self.underlying_writer.writeAll(bytes);
            if (bytes[bytes.len - 1] == '\n') {
                self.current_line_empty = true;
            }
            return bytes.len;
        }
    };
}