| ... | ... | @@ -91,55 +91,76 @@ pub fn fmtIdent(ident: []const u8) std.fmt.Formatter(formatIdent) { |
| 91 | 91 | return .{ .data = ident }; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | | /// This data is available when outputting .c code for a Module. |
| 94 | /// This data is available when outputting .c code for a `*Module.Fn`. |
| 95 | 95 | /// It is not available when generating .h file. |
| 96 | | pub const Object = struct { |
| 97 | | dg: DeclGen, |
| 96 | pub const Function = struct { |
| 98 | 97 | air: Air, |
| 99 | 98 | liveness: Liveness, |
| 100 | | gpa: *mem.Allocator, |
| 101 | | code: std.ArrayList(u8), |
| 102 | 99 | value_map: CValueMap, |
| 103 | 100 | blocks: std.AutoHashMapUnmanaged(Air.Inst.Index, BlockData) = .{}, |
| 104 | 101 | next_arg_index: usize = 0, |
| 105 | 102 | next_local_index: usize = 0, |
| 106 | 103 | next_block_index: usize = 0, |
| 107 | | indent_writer: IndentWriter(std.ArrayList(u8).Writer), |
| 104 | object: Object, |
| 105 | func: *Module.Fn, |
| 108 | 106 | |
| 109 | | fn resolveInst(o: *Object, inst: Air.Inst.Ref) !CValue { |
| 110 | | if (o.air.value(inst)) |_| { |
| 107 | fn resolveInst(f: *Function, inst: Air.Inst.Ref) !CValue { |
| 108 | if (f.air.value(inst)) |_| { |
| 111 | 109 | return CValue{ .constant = inst }; |
| 112 | 110 | } |
| 113 | 111 | const index = Air.refToIndex(inst).?; |
| 114 | | return o.value_map.get(index).?; // Assertion means instruction does not dominate usage. |
| 112 | return f.value_map.get(index).?; // Assertion means instruction does not dominate usage. |
| 115 | 113 | } |
| 116 | 114 | |
| 117 | | fn allocLocalValue(o: *Object) CValue { |
| 118 | | const result = o.next_local_index; |
| 119 | | o.next_local_index += 1; |
| 115 | fn allocLocalValue(f: *Function) CValue { |
| 116 | const result = f.next_local_index; |
| 117 | f.next_local_index += 1; |
| 120 | 118 | return .{ .local = result }; |
| 121 | 119 | } |
| 122 | 120 | |
| 123 | | fn allocLocal(o: *Object, ty: Type, mutability: Mutability) !CValue { |
| 124 | | const local_value = o.allocLocalValue(); |
| 125 | | try o.renderTypeAndName(o.writer(), ty, local_value, mutability); |
| 121 | fn allocLocal(f: *Function, ty: Type, mutability: Mutability) !CValue { |
| 122 | const local_value = f.allocLocalValue(); |
| 123 | try f.object.renderTypeAndName(f.object.writer(), ty, local_value, mutability); |
| 126 | 124 | return local_value; |
| 127 | 125 | } |
| 128 | 126 | |
| 127 | fn writeCValue(f: *Function, w: anytype, c_value: CValue) !void { |
| 128 | switch (c_value) { |
| 129 | .constant => |inst| { |
| 130 | const ty = f.air.typeOf(inst); |
| 131 | const val = f.air.value(inst).?; |
| 132 | return f.object.dg.renderValue(w, ty, val); |
| 133 | }, |
| 134 | else => return Object.writeCValue(w, c_value), |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | fn fail(f: *Function, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } { |
| 139 | return f.object.dg.fail(format, args); |
| 140 | } |
| 141 | |
| 142 | fn renderType(f: *Function, w: anytype, t: Type) !void { |
| 143 | return f.object.dg.renderType(w, t); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | /// This data is available when outputting .c code for a `Module`. |
| 148 | /// It is not available when generating .h file. |
| 149 | pub const Object = struct { |
| 150 | dg: DeclGen, |
| 151 | code: std.ArrayList(u8), |
| 152 | indent_writer: IndentWriter(std.ArrayList(u8).Writer), |
| 153 | |
| 129 | 154 | fn writer(o: *Object) IndentWriter(std.ArrayList(u8).Writer).Writer { |
| 130 | 155 | return o.indent_writer.writer(); |
| 131 | 156 | } |
| 132 | 157 | |
| 133 | | fn writeCValue(o: *Object, w: anytype, c_value: CValue) !void { |
| 158 | fn writeCValue(w: anytype, c_value: CValue) !void { |
| 134 | 159 | switch (c_value) { |
| 135 | 160 | .none => unreachable, |
| 136 | 161 | .local => |i| return w.print("t{d}", .{i}), |
| 137 | 162 | .local_ref => |i| return w.print("&t{d}", .{i}), |
| 138 | | .constant => |inst| { |
| 139 | | const ty = o.air.typeOf(inst); |
| 140 | | const val = o.air.value(inst).?; |
| 141 | | return o.dg.renderValue(w, ty, val); |
| 142 | | }, |
| 163 | .constant => unreachable, |
| 143 | 164 | .arg => |i| return w.print("a{d}", .{i}), |
| 144 | 165 | .decl => |decl| return w.writeAll(mem.span(decl.name)), |
| 145 | 166 | .decl_ref => |decl| return w.print("&{s}", .{decl.name}), |
| ... | ... | @@ -153,7 +174,7 @@ pub const Object = struct { |
| 153 | 174 | name: CValue, |
| 154 | 175 | mutability: Mutability, |
| 155 | 176 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 156 | | var suffix = std.ArrayList(u8).init(o.gpa); |
| 177 | var suffix = std.ArrayList(u8).init(o.dg.gpa); |
| 157 | 178 | defer suffix.deinit(); |
| 158 | 179 | |
| 159 | 180 | var render_ty = ty; |
| ... | ... | @@ -177,7 +198,7 @@ pub const Object = struct { |
| 177 | 198 | .Const => try w.writeAll("const "), |
| 178 | 199 | .Mut => {}, |
| 179 | 200 | } |
| 180 | | try o.writeCValue(w, name); |
| 201 | try writeCValue(w, name); |
| 181 | 202 | try w.writeAll(")("); |
| 182 | 203 | const param_len = render_ty.fnParamLen(); |
| 183 | 204 | const is_var_args = render_ty.fnIsVarArgs(); |
| ... | ... | @@ -205,7 +226,7 @@ pub const Object = struct { |
| 205 | 226 | .Mut => "", |
| 206 | 227 | }; |
| 207 | 228 | try w.print(" {s}", .{const_prefix}); |
| 208 | | try o.writeCValue(w, name); |
| 229 | try writeCValue(w, name); |
| 209 | 230 | } |
| 210 | 231 | try w.writeAll(suffix.items); |
| 211 | 232 | } |
| ... | ... | @@ -213,11 +234,14 @@ pub const Object = struct { |
| 213 | 234 | |
| 214 | 235 | /// This data is available both when outputting .c code and when outputting an .h file. |
| 215 | 236 | pub const DeclGen = struct { |
| 237 | gpa: *std.mem.Allocator, |
| 216 | 238 | module: *Module, |
| 217 | 239 | decl: *Decl, |
| 218 | 240 | fwd_decl: std.ArrayList(u8), |
| 219 | 241 | error_msg: ?*Module.ErrorMsg, |
| 242 | /// The key of this map is Type which has references to typedefs_arena. |
| 220 | 243 | typedefs: TypedefMap, |
| 244 | typedefs_arena: *std.mem.Allocator, |
| 221 | 245 | |
| 222 | 246 | fn fail(dg: *DeclGen, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } { |
| 223 | 247 | @setCold(true); |
| ... | ... | @@ -545,7 +569,10 @@ pub const DeclGen = struct { |
| 545 | 569 | |
| 546 | 570 | try dg.typedefs.ensureUnusedCapacity(1); |
| 547 | 571 | try w.writeAll(name); |
| 548 | | dg.typedefs.putAssumeCapacityNoClobber(t, .{ .name = name, .rendered = rendered }); |
| 572 | dg.typedefs.putAssumeCapacityNoClobber( |
| 573 | try t.copy(dg.typedefs_arena), |
| 574 | .{ .name = name, .rendered = rendered }, |
| 575 | ); |
| 549 | 576 | } else { |
| 550 | 577 | try dg.renderType(w, t.elemType()); |
| 551 | 578 | try w.writeAll(" *"); |
| ... | ... | @@ -586,7 +613,10 @@ pub const DeclGen = struct { |
| 586 | 613 | |
| 587 | 614 | try dg.typedefs.ensureUnusedCapacity(1); |
| 588 | 615 | try w.writeAll(name); |
| 589 | | dg.typedefs.putAssumeCapacityNoClobber(t, .{ .name = name, .rendered = rendered }); |
| 616 | dg.typedefs.putAssumeCapacityNoClobber( |
| 617 | try t.copy(dg.typedefs_arena), |
| 618 | .{ .name = name, .rendered = rendered }, |
| 619 | ); |
| 590 | 620 | }, |
| 591 | 621 | .ErrorSet => { |
| 592 | 622 | comptime std.debug.assert(Type.initTag(.anyerror).abiSize(std.Target.current) == 2); |
| ... | ... | @@ -626,7 +656,10 @@ pub const DeclGen = struct { |
| 626 | 656 | |
| 627 | 657 | try dg.typedefs.ensureUnusedCapacity(1); |
| 628 | 658 | try w.writeAll(name); |
| 629 | | dg.typedefs.putAssumeCapacityNoClobber(t, .{ .name = name, .rendered = rendered }); |
| 659 | dg.typedefs.putAssumeCapacityNoClobber( |
| 660 | try t.copy(dg.typedefs_arena), |
| 661 | .{ .name = name, .rendered = rendered }, |
| 662 | ); |
| 630 | 663 | }, |
| 631 | 664 | .Struct => { |
| 632 | 665 | if (dg.typedefs.get(t)) |some| { |
| ... | ... | @@ -659,7 +692,10 @@ pub const DeclGen = struct { |
| 659 | 692 | |
| 660 | 693 | try dg.typedefs.ensureUnusedCapacity(1); |
| 661 | 694 | try w.writeAll(name); |
| 662 | | dg.typedefs.putAssumeCapacityNoClobber(t, .{ .name = name, .rendered = rendered }); |
| 695 | dg.typedefs.putAssumeCapacityNoClobber( |
| 696 | try t.copy(dg.typedefs_arena), |
| 697 | .{ .name = name, .rendered = rendered }, |
| 698 | ); |
| 663 | 699 | }, |
| 664 | 700 | .Enum => { |
| 665 | 701 | // For enums, we simply use the integer tag type. |
| ... | ... | @@ -724,6 +760,29 @@ pub const DeclGen = struct { |
| 724 | 760 | } |
| 725 | 761 | }; |
| 726 | 762 | |
| 763 | pub fn genFunc(f: *Function) !void { |
| 764 | const tracy = trace(@src()); |
| 765 | defer tracy.end(); |
| 766 | |
| 767 | const o = &f.object; |
| 768 | const is_global = o.dg.module.decl_exports.contains(f.func.owner_decl); |
| 769 | const fwd_decl_writer = o.dg.fwd_decl.writer(); |
| 770 | if (is_global) { |
| 771 | try fwd_decl_writer.writeAll("ZIG_EXTERN_C "); |
| 772 | } |
| 773 | try o.dg.renderFunctionSignature(fwd_decl_writer, is_global); |
| 774 | try fwd_decl_writer.writeAll(";\n"); |
| 775 | |
| 776 | try o.indent_writer.insertNewline(); |
| 777 | try o.dg.renderFunctionSignature(o.writer(), is_global); |
| 778 | |
| 779 | try o.writer().writeByte(' '); |
| 780 | const main_body = f.air.getMainBody(); |
| 781 | try genBody(f, main_body); |
| 782 | |
| 783 | try o.indent_writer.insertNewline(); |
| 784 | } |
| 785 | |
| 727 | 786 | pub fn genDecl(o: *Object) !void { |
| 728 | 787 | const tracy = trace(@src()); |
| 729 | 788 | defer tracy.end(); |
| ... | ... | @@ -732,28 +791,6 @@ pub fn genDecl(o: *Object) !void { |
| 732 | 791 | .ty = o.dg.decl.ty, |
| 733 | 792 | .val = o.dg.decl.val, |
| 734 | 793 | }; |
| 735 | | if (tv.val.castTag(.function)) |func_payload| { |
| 736 | | const func: *Module.Fn = func_payload.data; |
| 737 | | if (func.owner_decl == o.dg.decl) { |
| 738 | | const is_global = o.dg.declIsGlobal(tv); |
| 739 | | const fwd_decl_writer = o.dg.fwd_decl.writer(); |
| 740 | | if (is_global) { |
| 741 | | try fwd_decl_writer.writeAll("ZIG_EXTERN_C "); |
| 742 | | } |
| 743 | | try o.dg.renderFunctionSignature(fwd_decl_writer, is_global); |
| 744 | | try fwd_decl_writer.writeAll(";\n"); |
| 745 | | |
| 746 | | try o.indent_writer.insertNewline(); |
| 747 | | try o.dg.renderFunctionSignature(o.writer(), is_global); |
| 748 | | |
| 749 | | try o.writer().writeByte(' '); |
| 750 | | const main_body = o.air.getMainBody(); |
| 751 | | try genBody(o, main_body); |
| 752 | | |
| 753 | | try o.indent_writer.insertNewline(); |
| 754 | | return; |
| 755 | | } |
| 756 | | } |
| 757 | 794 | if (tv.val.tag() == .extern_fn) { |
| 758 | 795 | const writer = o.writer(); |
| 759 | 796 | try writer.writeAll("ZIG_EXTERN_C "); |
| ... | ... | @@ -821,250 +858,250 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void { |
| 821 | 858 | } |
| 822 | 859 | } |
| 823 | 860 | |
| 824 | | fn genBody(o: *Object, body: []const Air.Inst.Index) error{ AnalysisFail, OutOfMemory }!void { |
| 825 | | const writer = o.writer(); |
| 861 | fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutOfMemory }!void { |
| 862 | const writer = f.object.writer(); |
| 826 | 863 | if (body.len == 0) { |
| 827 | 864 | try writer.writeAll("{}"); |
| 828 | 865 | return; |
| 829 | 866 | } |
| 830 | 867 | |
| 831 | 868 | try writer.writeAll("{\n"); |
| 832 | | o.indent_writer.pushIndent(); |
| 869 | f.object.indent_writer.pushIndent(); |
| 833 | 870 | |
| 834 | | const air_tags = o.air.instructions.items(.tag); |
| 871 | const air_tags = f.air.instructions.items(.tag); |
| 835 | 872 | |
| 836 | 873 | for (body) |inst| { |
| 837 | 874 | const result_value = switch (air_tags[inst]) { |
| 838 | 875 | // zig fmt: off |
| 839 | 876 | .constant => unreachable, // excluded from function bodies |
| 840 | 877 | .const_ty => unreachable, // excluded from function bodies |
| 841 | | .arg => airArg(o), |
| 878 | .arg => airArg(f), |
| 842 | 879 | |
| 843 | | .breakpoint => try airBreakpoint(o), |
| 844 | | .unreach => try airUnreach(o), |
| 845 | | .fence => try airFence(o, inst), |
| 880 | .breakpoint => try airBreakpoint(f), |
| 881 | .unreach => try airUnreach(f), |
| 882 | .fence => try airFence(f, inst), |
| 846 | 883 | |
| 847 | 884 | // TODO use a different strategy for add that communicates to the optimizer |
| 848 | 885 | // that wrapping is UB. |
| 849 | | .add, .ptr_add => try airBinOp( o, inst, " + "), |
| 850 | | .addwrap => try airWrapOp(o, inst, " + ", "addw_"), |
| 886 | .add, .ptr_add => try airBinOp( f, inst, " + "), |
| 887 | .addwrap => try airWrapOp(f, inst, " + ", "addw_"), |
| 851 | 888 | // TODO use a different strategy for sub that communicates to the optimizer |
| 852 | 889 | // that wrapping is UB. |
| 853 | | .sub, .ptr_sub => try airBinOp( o, inst, " - "), |
| 854 | | .subwrap => try airWrapOp(o, inst, " - ", "subw_"), |
| 890 | .sub, .ptr_sub => try airBinOp( f, inst, " - "), |
| 891 | .subwrap => try airWrapOp(f, inst, " - ", "subw_"), |
| 855 | 892 | // TODO use a different strategy for mul that communicates to the optimizer |
| 856 | 893 | // that wrapping is UB. |
| 857 | | .mul => try airBinOp( o, inst, " * "), |
| 858 | | .mulwrap => try airWrapOp(o, inst, " * ", "mulw_"), |
| 894 | .mul => try airBinOp( f, inst, " * "), |
| 895 | .mulwrap => try airWrapOp(f, inst, " * ", "mulw_"), |
| 859 | 896 | // TODO use a different strategy for div that communicates to the optimizer |
| 860 | 897 | // that wrapping is UB. |
| 861 | | .div => try airBinOp( o, inst, " / "), |
| 862 | | .rem => try airBinOp( o, inst, " % "), |
| 898 | .div => try airBinOp( f, inst, " / "), |
| 899 | .rem => try airBinOp( f, inst, " % "), |
| 863 | 900 | |
| 864 | | .cmp_eq => try airBinOp(o, inst, " == "), |
| 865 | | .cmp_gt => try airBinOp(o, inst, " > "), |
| 866 | | .cmp_gte => try airBinOp(o, inst, " >= "), |
| 867 | | .cmp_lt => try airBinOp(o, inst, " < "), |
| 868 | | .cmp_lte => try airBinOp(o, inst, " <= "), |
| 869 | | .cmp_neq => try airBinOp(o, inst, " != "), |
| 901 | .cmp_eq => try airBinOp(f, inst, " == "), |
| 902 | .cmp_gt => try airBinOp(f, inst, " > "), |
| 903 | .cmp_gte => try airBinOp(f, inst, " >= "), |
| 904 | .cmp_lt => try airBinOp(f, inst, " < "), |
| 905 | .cmp_lte => try airBinOp(f, inst, " <= "), |
| 906 | .cmp_neq => try airBinOp(f, inst, " != "), |
| 870 | 907 | |
| 871 | 908 | // bool_and and bool_or are non-short-circuit operations |
| 872 | | .bool_and => try airBinOp(o, inst, " & "), |
| 873 | | .bool_or => try airBinOp(o, inst, " | "), |
| 874 | | .bit_and => try airBinOp(o, inst, " & "), |
| 875 | | .bit_or => try airBinOp(o, inst, " | "), |
| 876 | | .xor => try airBinOp(o, inst, " ^ "), |
| 877 | | |
| 878 | | .shr => try airBinOp(o, inst, " >> "), |
| 879 | | .shl => try airBinOp(o, inst, " << "), |
| 880 | | |
| 881 | | .not => try airNot( o, inst), |
| 882 | | |
| 883 | | .optional_payload => try airOptionalPayload(o, inst), |
| 884 | | .optional_payload_ptr => try airOptionalPayload(o, inst), |
| 885 | | |
| 886 | | .is_err => try airIsErr(o, inst, "", ".", "!="), |
| 887 | | .is_non_err => try airIsErr(o, inst, "", ".", "=="), |
| 888 | | .is_err_ptr => try airIsErr(o, inst, "*", "->", "!="), |
| 889 | | .is_non_err_ptr => try airIsErr(o, inst, "*", "->", "=="), |
| 890 | | |
| 891 | | .is_null => try airIsNull(o, inst, "==", ""), |
| 892 | | .is_non_null => try airIsNull(o, inst, "!=", ""), |
| 893 | | .is_null_ptr => try airIsNull(o, inst, "==", "[0]"), |
| 894 | | .is_non_null_ptr => try airIsNull(o, inst, "!=", "[0]"), |
| 895 | | |
| 896 | | .alloc => try airAlloc(o, inst), |
| 897 | | .assembly => try airAsm(o, inst), |
| 898 | | .block => try airBlock(o, inst), |
| 899 | | .bitcast => try airBitcast(o, inst), |
| 900 | | .call => try airCall(o, inst), |
| 901 | | .dbg_stmt => try airDbgStmt(o, inst), |
| 902 | | .intcast => try airIntCast(o, inst), |
| 903 | | .trunc => try airTrunc(o, inst), |
| 904 | | .bool_to_int => try airBoolToInt(o, inst), |
| 905 | | .load => try airLoad(o, inst), |
| 906 | | .ret => try airRet(o, inst), |
| 907 | | .store => try airStore(o, inst), |
| 908 | | .loop => try airLoop(o, inst), |
| 909 | | .cond_br => try airCondBr(o, inst), |
| 910 | | .br => try airBr(o, inst), |
| 911 | | .switch_br => try airSwitchBr(o, inst), |
| 912 | | .wrap_optional => try airWrapOptional(o, inst), |
| 913 | | .struct_field_ptr => try airStructFieldPtr(o, inst), |
| 914 | | .array_to_slice => try airArrayToSlice(o, inst), |
| 915 | | .cmpxchg_weak => try airCmpxchg(o, inst, "weak"), |
| 916 | | .cmpxchg_strong => try airCmpxchg(o, inst, "strong"), |
| 917 | | .atomic_rmw => try airAtomicRmw(o, inst), |
| 918 | | .atomic_load => try airAtomicLoad(o, inst), |
| 919 | | |
| 920 | | .int_to_float, .float_to_int => try airSimpleCast(o, inst), |
| 921 | | |
| 922 | | .atomic_store_unordered => try airAtomicStore(o, inst, toMemoryOrder(.Unordered)), |
| 923 | | .atomic_store_monotonic => try airAtomicStore(o, inst, toMemoryOrder(.Monotonic)), |
| 924 | | .atomic_store_release => try airAtomicStore(o, inst, toMemoryOrder(.Release)), |
| 925 | | .atomic_store_seq_cst => try airAtomicStore(o, inst, toMemoryOrder(.SeqCst)), |
| 926 | | |
| 927 | | .struct_field_ptr_index_0 => try airStructFieldPtrIndex(o, inst, 0), |
| 928 | | .struct_field_ptr_index_1 => try airStructFieldPtrIndex(o, inst, 1), |
| 929 | | .struct_field_ptr_index_2 => try airStructFieldPtrIndex(o, inst, 2), |
| 930 | | .struct_field_ptr_index_3 => try airStructFieldPtrIndex(o, inst, 3), |
| 931 | | |
| 932 | | .struct_field_val => try airStructFieldVal(o, inst), |
| 933 | | .slice_ptr => try airSliceField(o, inst, ".ptr;\n"), |
| 934 | | .slice_len => try airSliceField(o, inst, ".len;\n"), |
| 935 | | |
| 936 | | .ptr_elem_val => try airPtrElemVal(o, inst, "["), |
| 937 | | .ptr_ptr_elem_val => try airPtrElemVal(o, inst, "[0]["), |
| 938 | | .ptr_elem_ptr => try airPtrElemPtr(o, inst), |
| 939 | | .slice_elem_val => try airSliceElemVal(o, inst, "["), |
| 940 | | .ptr_slice_elem_val => try airSliceElemVal(o, inst, "[0]["), |
| 941 | | |
| 942 | | .unwrap_errunion_payload => try airUnwrapErrUnionPay(o, inst), |
| 943 | | .unwrap_errunion_err => try airUnwrapErrUnionErr(o, inst), |
| 944 | | .unwrap_errunion_payload_ptr => try airUnwrapErrUnionPay(o, inst), |
| 945 | | .unwrap_errunion_err_ptr => try airUnwrapErrUnionErr(o, inst), |
| 946 | | .wrap_errunion_payload => try airWrapErrUnionPay(o, inst), |
| 947 | | .wrap_errunion_err => try airWrapErrUnionErr(o, inst), |
| 948 | | |
| 949 | | .ptrtoint => return o.dg.fail("TODO: C backend: implement codegen for ptrtoint", .{}), |
| 950 | | .floatcast => return o.dg.fail("TODO: C backend: implement codegen for floatcast", .{}), |
| 909 | .bool_and => try airBinOp(f, inst, " & "), |
| 910 | .bool_or => try airBinOp(f, inst, " | "), |
| 911 | .bit_and => try airBinOp(f, inst, " & "), |
| 912 | .bit_or => try airBinOp(f, inst, " | "), |
| 913 | .xor => try airBinOp(f, inst, " ^ "), |
| 914 | |
| 915 | .shr => try airBinOp(f, inst, " >> "), |
| 916 | .shl => try airBinOp(f, inst, " << "), |
| 917 | |
| 918 | .not => try airNot( f, inst), |
| 919 | |
| 920 | .optional_payload => try airOptionalPayload(f, inst), |
| 921 | .optional_payload_ptr => try airOptionalPayload(f, inst), |
| 922 | |
| 923 | .is_err => try airIsErr(f, inst, "", ".", "!="), |
| 924 | .is_non_err => try airIsErr(f, inst, "", ".", "=="), |
| 925 | .is_err_ptr => try airIsErr(f, inst, "*", "->", "!="), |
| 926 | .is_non_err_ptr => try airIsErr(f, inst, "*", "->", "=="), |
| 927 | |
| 928 | .is_null => try airIsNull(f, inst, "==", ""), |
| 929 | .is_non_null => try airIsNull(f, inst, "!=", ""), |
| 930 | .is_null_ptr => try airIsNull(f, inst, "==", "[0]"), |
| 931 | .is_non_null_ptr => try airIsNull(f, inst, "!=", "[0]"), |
| 932 | |
| 933 | .alloc => try airAlloc(f, inst), |
| 934 | .assembly => try airAsm(f, inst), |
| 935 | .block => try airBlock(f, inst), |
| 936 | .bitcast => try airBitcast(f, inst), |
| 937 | .call => try airCall(f, inst), |
| 938 | .dbg_stmt => try airDbgStmt(f, inst), |
| 939 | .intcast => try airIntCast(f, inst), |
| 940 | .trunc => try airTrunc(f, inst), |
| 941 | .bool_to_int => try airBoolToInt(f, inst), |
| 942 | .load => try airLoad(f, inst), |
| 943 | .ret => try airRet(f, inst), |
| 944 | .store => try airStore(f, inst), |
| 945 | .loop => try airLoop(f, inst), |
| 946 | .cond_br => try airCondBr(f, inst), |
| 947 | .br => try airBr(f, inst), |
| 948 | .switch_br => try airSwitchBr(f, inst), |
| 949 | .wrap_optional => try airWrapOptional(f, inst), |
| 950 | .struct_field_ptr => try airStructFieldPtr(f, inst), |
| 951 | .array_to_slice => try airArrayToSlice(f, inst), |
| 952 | .cmpxchg_weak => try airCmpxchg(f, inst, "weak"), |
| 953 | .cmpxchg_strong => try airCmpxchg(f, inst, "strong"), |
| 954 | .atomic_rmw => try airAtomicRmw(f, inst), |
| 955 | .atomic_load => try airAtomicLoad(f, inst), |
| 956 | |
| 957 | .int_to_float, .float_to_int => try airSimpleCast(f, inst), |
| 958 | |
| 959 | .atomic_store_unordered => try airAtomicStore(f, inst, toMemoryOrder(.Unordered)), |
| 960 | .atomic_store_monotonic => try airAtomicStore(f, inst, toMemoryOrder(.Monotonic)), |
| 961 | .atomic_store_release => try airAtomicStore(f, inst, toMemoryOrder(.Release)), |
| 962 | .atomic_store_seq_cst => try airAtomicStore(f, inst, toMemoryOrder(.SeqCst)), |
| 963 | |
| 964 | .struct_field_ptr_index_0 => try airStructFieldPtrIndex(f, inst, 0), |
| 965 | .struct_field_ptr_index_1 => try airStructFieldPtrIndex(f, inst, 1), |
| 966 | .struct_field_ptr_index_2 => try airStructFieldPtrIndex(f, inst, 2), |
| 967 | .struct_field_ptr_index_3 => try airStructFieldPtrIndex(f, inst, 3), |
| 968 | |
| 969 | .struct_field_val => try airStructFieldVal(f, inst), |
| 970 | .slice_ptr => try airSliceField(f, inst, ".ptr;\n"), |
| 971 | .slice_len => try airSliceField(f, inst, ".len;\n"), |
| 972 | |
| 973 | .ptr_elem_val => try airPtrElemVal(f, inst, "["), |
| 974 | .ptr_ptr_elem_val => try airPtrElemVal(f, inst, "[0]["), |
| 975 | .ptr_elem_ptr => try airPtrElemPtr(f, inst), |
| 976 | .slice_elem_val => try airSliceElemVal(f, inst, "["), |
| 977 | .ptr_slice_elem_val => try airSliceElemVal(f, inst, "[0]["), |
| 978 | |
| 979 | .unwrap_errunion_payload => try airUnwrapErrUnionPay(f, inst), |
| 980 | .unwrap_errunion_err => try airUnwrapErrUnionErr(f, inst), |
| 981 | .unwrap_errunion_payload_ptr => try airUnwrapErrUnionPay(f, inst), |
| 982 | .unwrap_errunion_err_ptr => try airUnwrapErrUnionErr(f, inst), |
| 983 | .wrap_errunion_payload => try airWrapErrUnionPay(f, inst), |
| 984 | .wrap_errunion_err => try airWrapErrUnionErr(f, inst), |
| 985 | |
| 986 | .ptrtoint => return f.fail("TODO: C backend: implement codegen for ptrtoint", .{}), |
| 987 | .floatcast => return f.fail("TODO: C backend: implement codegen for floatcast", .{}), |
| 951 | 988 | // zig fmt: on |
| 952 | 989 | }; |
| 953 | 990 | switch (result_value) { |
| 954 | 991 | .none => {}, |
| 955 | | else => try o.value_map.putNoClobber(inst, result_value), |
| 992 | else => try f.value_map.putNoClobber(inst, result_value), |
| 956 | 993 | } |
| 957 | 994 | } |
| 958 | 995 | |
| 959 | | o.indent_writer.popIndent(); |
| 996 | f.object.indent_writer.popIndent(); |
| 960 | 997 | try writer.writeAll("}"); |
| 961 | 998 | } |
| 962 | 999 | |
| 963 | | fn airSliceField(o: *Object, inst: Air.Inst.Index, suffix: []const u8) !CValue { |
| 964 | | if (o.liveness.isUnused(inst)) |
| 1000 | fn airSliceField(f: *Function, inst: Air.Inst.Index, suffix: []const u8) !CValue { |
| 1001 | if (f.liveness.isUnused(inst)) |
| 965 | 1002 | return CValue.none; |
| 966 | 1003 | |
| 967 | | const ty_op = o.air.instructions.items(.data)[inst].ty_op; |
| 968 | | const operand = try o.resolveInst(ty_op.operand); |
| 969 | | const writer = o.writer(); |
| 970 | | const local = try o.allocLocal(Type.initTag(.usize), .Const); |
| 1004 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 1005 | const operand = try f.resolveInst(ty_op.operand); |
| 1006 | const writer = f.object.writer(); |
| 1007 | const local = try f.allocLocal(Type.initTag(.usize), .Const); |
| 971 | 1008 | try writer.writeAll(" = "); |
| 972 | | try o.writeCValue(writer, operand); |
| 1009 | try f.writeCValue(writer, operand); |
| 973 | 1010 | try writer.writeAll(suffix); |
| 974 | 1011 | return local; |
| 975 | 1012 | } |
| 976 | 1013 | |
| 977 | | fn airPtrElemVal(o: *Object, inst: Air.Inst.Index, prefix: []const u8) !CValue { |
| 1014 | fn airPtrElemVal(f: *Function, inst: Air.Inst.Index, prefix: []const u8) !CValue { |
| 978 | 1015 | const is_volatile = false; // TODO |
| 979 | | if (!is_volatile and o.liveness.isUnused(inst)) |
| 1016 | if (!is_volatile and f.liveness.isUnused(inst)) |
| 980 | 1017 | return CValue.none; |
| 981 | 1018 | |
| 982 | 1019 | _ = prefix; |
| 983 | | return o.dg.fail("TODO: C backend: airPtrElemVal", .{}); |
| 1020 | return f.fail("TODO: C backend: airPtrElemVal", .{}); |
| 984 | 1021 | } |
| 985 | 1022 | |
| 986 | | fn airPtrElemPtr(o: *Object, inst: Air.Inst.Index) !CValue { |
| 987 | | if (o.liveness.isUnused(inst)) |
| 1023 | fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1024 | if (f.liveness.isUnused(inst)) |
| 988 | 1025 | return CValue.none; |
| 989 | 1026 | |
| 990 | | return o.dg.fail("TODO: C backend: airPtrElemPtr", .{}); |
| 1027 | return f.fail("TODO: C backend: airPtrElemPtr", .{}); |
| 991 | 1028 | } |
| 992 | 1029 | |
| 993 | | fn airSliceElemVal(o: *Object, inst: Air.Inst.Index, prefix: []const u8) !CValue { |
| 1030 | fn airSliceElemVal(f: *Function, inst: Air.Inst.Index, prefix: []const u8) !CValue { |
| 994 | 1031 | const is_volatile = false; // TODO |
| 995 | | if (!is_volatile and o.liveness.isUnused(inst)) |
| 1032 | if (!is_volatile and f.liveness.isUnused(inst)) |
| 996 | 1033 | return CValue.none; |
| 997 | 1034 | |
| 998 | | const bin_op = o.air.instructions.items(.data)[inst].bin_op; |
| 999 | | const slice = try o.resolveInst(bin_op.lhs); |
| 1000 | | const index = try o.resolveInst(bin_op.rhs); |
| 1001 | | const writer = o.writer(); |
| 1002 | | const local = try o.allocLocal(o.air.typeOfIndex(inst), .Const); |
| 1035 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 1036 | const slice = try f.resolveInst(bin_op.lhs); |
| 1037 | const index = try f.resolveInst(bin_op.rhs); |
| 1038 | const writer = f.object.writer(); |
| 1039 | const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const); |
| 1003 | 1040 | try writer.writeAll(" = "); |
| 1004 | | try o.writeCValue(writer, slice); |
| 1041 | try f.writeCValue(writer, slice); |
| 1005 | 1042 | try writer.writeAll(prefix); |
| 1006 | | try o.writeCValue(writer, index); |
| 1043 | try f.writeCValue(writer, index); |
| 1007 | 1044 | try writer.writeAll("];\n"); |
| 1008 | 1045 | return local; |
| 1009 | 1046 | } |
| 1010 | 1047 | |
| 1011 | | fn airAlloc(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1012 | | const writer = o.writer(); |
| 1013 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1048 | fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1049 | const writer = f.object.writer(); |
| 1050 | const inst_ty = f.air.typeOfIndex(inst); |
| 1014 | 1051 | |
| 1015 | 1052 | // First line: the variable used as data storage. |
| 1016 | 1053 | const elem_type = inst_ty.elemType(); |
| 1017 | 1054 | const mutability: Mutability = if (inst_ty.isConstPtr()) .Const else .Mut; |
| 1018 | | const local = try o.allocLocal(elem_type, mutability); |
| 1055 | const local = try f.allocLocal(elem_type, mutability); |
| 1019 | 1056 | try writer.writeAll(";\n"); |
| 1020 | 1057 | |
| 1021 | 1058 | return CValue{ .local_ref = local.local }; |
| 1022 | 1059 | } |
| 1023 | 1060 | |
| 1024 | | fn airArg(o: *Object) CValue { |
| 1025 | | const i = o.next_arg_index; |
| 1026 | | o.next_arg_index += 1; |
| 1061 | fn airArg(f: *Function) CValue { |
| 1062 | const i = f.next_arg_index; |
| 1063 | f.next_arg_index += 1; |
| 1027 | 1064 | return .{ .arg = i }; |
| 1028 | 1065 | } |
| 1029 | 1066 | |
| 1030 | | fn airLoad(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1031 | | const ty_op = o.air.instructions.items(.data)[inst].ty_op; |
| 1032 | | const is_volatile = o.air.typeOf(ty_op.operand).isVolatilePtr(); |
| 1033 | | if (!is_volatile and o.liveness.isUnused(inst)) |
| 1067 | fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1068 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 1069 | const is_volatile = f.air.typeOf(ty_op.operand).isVolatilePtr(); |
| 1070 | if (!is_volatile and f.liveness.isUnused(inst)) |
| 1034 | 1071 | return CValue.none; |
| 1035 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1036 | | const operand = try o.resolveInst(ty_op.operand); |
| 1037 | | const writer = o.writer(); |
| 1038 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1072 | const inst_ty = f.air.typeOfIndex(inst); |
| 1073 | const operand = try f.resolveInst(ty_op.operand); |
| 1074 | const writer = f.object.writer(); |
| 1075 | const local = try f.allocLocal(inst_ty, .Const); |
| 1039 | 1076 | switch (operand) { |
| 1040 | 1077 | .local_ref => |i| { |
| 1041 | 1078 | const wrapped: CValue = .{ .local = i }; |
| 1042 | 1079 | try writer.writeAll(" = "); |
| 1043 | | try o.writeCValue(writer, wrapped); |
| 1080 | try f.writeCValue(writer, wrapped); |
| 1044 | 1081 | try writer.writeAll(";\n"); |
| 1045 | 1082 | }, |
| 1046 | 1083 | .decl_ref => |decl| { |
| 1047 | 1084 | const wrapped: CValue = .{ .decl = decl }; |
| 1048 | 1085 | try writer.writeAll(" = "); |
| 1049 | | try o.writeCValue(writer, wrapped); |
| 1086 | try f.writeCValue(writer, wrapped); |
| 1050 | 1087 | try writer.writeAll(";\n"); |
| 1051 | 1088 | }, |
| 1052 | 1089 | else => { |
| 1053 | 1090 | try writer.writeAll(" = *"); |
| 1054 | | try o.writeCValue(writer, operand); |
| 1091 | try f.writeCValue(writer, operand); |
| 1055 | 1092 | try writer.writeAll(";\n"); |
| 1056 | 1093 | }, |
| 1057 | 1094 | } |
| 1058 | 1095 | return local; |
| 1059 | 1096 | } |
| 1060 | 1097 | |
| 1061 | | fn airRet(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1062 | | const un_op = o.air.instructions.items(.data)[inst].un_op; |
| 1063 | | const writer = o.writer(); |
| 1064 | | if (o.air.typeOf(un_op).hasCodeGenBits()) { |
| 1065 | | const operand = try o.resolveInst(un_op); |
| 1098 | fn airRet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1099 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| 1100 | const writer = f.object.writer(); |
| 1101 | if (f.air.typeOf(un_op).hasCodeGenBits()) { |
| 1102 | const operand = try f.resolveInst(un_op); |
| 1066 | 1103 | try writer.writeAll("return "); |
| 1067 | | try o.writeCValue(writer, operand); |
| 1104 | try f.writeCValue(writer, operand); |
| 1068 | 1105 | try writer.writeAll(";\n"); |
| 1069 | 1106 | } else { |
| 1070 | 1107 | try writer.writeAll("return;\n"); |
| ... | ... | @@ -1072,75 +1109,75 @@ fn airRet(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1072 | 1109 | return CValue.none; |
| 1073 | 1110 | } |
| 1074 | 1111 | |
| 1075 | | fn airIntCast(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1076 | | if (o.liveness.isUnused(inst)) |
| 1112 | fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1113 | if (f.liveness.isUnused(inst)) |
| 1077 | 1114 | return CValue.none; |
| 1078 | 1115 | |
| 1079 | | const ty_op = o.air.instructions.items(.data)[inst].ty_op; |
| 1080 | | const operand = try o.resolveInst(ty_op.operand); |
| 1116 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 1117 | const operand = try f.resolveInst(ty_op.operand); |
| 1081 | 1118 | |
| 1082 | | const writer = o.writer(); |
| 1083 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1084 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1119 | const writer = f.object.writer(); |
| 1120 | const inst_ty = f.air.typeOfIndex(inst); |
| 1121 | const local = try f.allocLocal(inst_ty, .Const); |
| 1085 | 1122 | try writer.writeAll(" = ("); |
| 1086 | | try o.dg.renderType(writer, inst_ty); |
| 1123 | try f.renderType(writer, inst_ty); |
| 1087 | 1124 | try writer.writeAll(")"); |
| 1088 | | try o.writeCValue(writer, operand); |
| 1125 | try f.writeCValue(writer, operand); |
| 1089 | 1126 | try writer.writeAll(";\n"); |
| 1090 | 1127 | return local; |
| 1091 | 1128 | } |
| 1092 | 1129 | |
| 1093 | | fn airTrunc(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1094 | | if (o.liveness.isUnused(inst)) |
| 1130 | fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1131 | if (f.liveness.isUnused(inst)) |
| 1095 | 1132 | return CValue.none; |
| 1096 | 1133 | |
| 1097 | | const ty_op = o.air.instructions.items(.data)[inst].ty_op; |
| 1098 | | const operand = try o.resolveInst(ty_op.operand); |
| 1134 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 1135 | const operand = try f.resolveInst(ty_op.operand); |
| 1099 | 1136 | _ = operand; |
| 1100 | | return o.dg.fail("TODO: C backend: airTrunc", .{}); |
| 1137 | return f.fail("TODO: C backend: airTrunc", .{}); |
| 1101 | 1138 | } |
| 1102 | 1139 | |
| 1103 | | fn airBoolToInt(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1104 | | if (o.liveness.isUnused(inst)) |
| 1140 | fn airBoolToInt(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1141 | if (f.liveness.isUnused(inst)) |
| 1105 | 1142 | return CValue.none; |
| 1106 | | const un_op = o.air.instructions.items(.data)[inst].un_op; |
| 1107 | | const writer = o.writer(); |
| 1108 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1109 | | const operand = try o.resolveInst(un_op); |
| 1110 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1143 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| 1144 | const writer = f.object.writer(); |
| 1145 | const inst_ty = f.air.typeOfIndex(inst); |
| 1146 | const operand = try f.resolveInst(un_op); |
| 1147 | const local = try f.allocLocal(inst_ty, .Const); |
| 1111 | 1148 | try writer.writeAll(" = "); |
| 1112 | | try o.writeCValue(writer, operand); |
| 1149 | try f.writeCValue(writer, operand); |
| 1113 | 1150 | try writer.writeAll(";\n"); |
| 1114 | 1151 | return local; |
| 1115 | 1152 | } |
| 1116 | 1153 | |
| 1117 | | fn airStore(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1154 | fn airStore(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1118 | 1155 | // *a = b; |
| 1119 | | const bin_op = o.air.instructions.items(.data)[inst].bin_op; |
| 1120 | | const dest_ptr = try o.resolveInst(bin_op.lhs); |
| 1121 | | const src_val = try o.resolveInst(bin_op.rhs); |
| 1156 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 1157 | const dest_ptr = try f.resolveInst(bin_op.lhs); |
| 1158 | const src_val = try f.resolveInst(bin_op.rhs); |
| 1122 | 1159 | |
| 1123 | | const writer = o.writer(); |
| 1160 | const writer = f.object.writer(); |
| 1124 | 1161 | switch (dest_ptr) { |
| 1125 | 1162 | .local_ref => |i| { |
| 1126 | 1163 | const dest: CValue = .{ .local = i }; |
| 1127 | | try o.writeCValue(writer, dest); |
| 1164 | try f.writeCValue(writer, dest); |
| 1128 | 1165 | try writer.writeAll(" = "); |
| 1129 | | try o.writeCValue(writer, src_val); |
| 1166 | try f.writeCValue(writer, src_val); |
| 1130 | 1167 | try writer.writeAll(";\n"); |
| 1131 | 1168 | }, |
| 1132 | 1169 | .decl_ref => |decl| { |
| 1133 | 1170 | const dest: CValue = .{ .decl = decl }; |
| 1134 | | try o.writeCValue(writer, dest); |
| 1171 | try f.writeCValue(writer, dest); |
| 1135 | 1172 | try writer.writeAll(" = "); |
| 1136 | | try o.writeCValue(writer, src_val); |
| 1173 | try f.writeCValue(writer, src_val); |
| 1137 | 1174 | try writer.writeAll(";\n"); |
| 1138 | 1175 | }, |
| 1139 | 1176 | else => { |
| 1140 | 1177 | try writer.writeAll("*"); |
| 1141 | | try o.writeCValue(writer, dest_ptr); |
| 1178 | try f.writeCValue(writer, dest_ptr); |
| 1142 | 1179 | try writer.writeAll(" = "); |
| 1143 | | try o.writeCValue(writer, src_val); |
| 1180 | try f.writeCValue(writer, src_val); |
| 1144 | 1181 | try writer.writeAll(";\n"); |
| 1145 | 1182 | }, |
| 1146 | 1183 | } |
| ... | ... | @@ -1148,17 +1185,17 @@ fn airStore(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1148 | 1185 | } |
| 1149 | 1186 | |
| 1150 | 1187 | fn airWrapOp( |
| 1151 | | o: *Object, |
| 1188 | f: *Function, |
| 1152 | 1189 | inst: Air.Inst.Index, |
| 1153 | 1190 | str_op: [*:0]const u8, |
| 1154 | 1191 | fn_op: [*:0]const u8, |
| 1155 | 1192 | ) !CValue { |
| 1156 | | if (o.liveness.isUnused(inst)) |
| 1193 | if (f.liveness.isUnused(inst)) |
| 1157 | 1194 | return CValue.none; |
| 1158 | 1195 | |
| 1159 | | const bin_op = o.air.instructions.items(.data)[inst].bin_op; |
| 1160 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1161 | | const int_info = inst_ty.intInfo(o.dg.module.getTarget()); |
| 1196 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 1197 | const inst_ty = f.air.typeOfIndex(inst); |
| 1198 | const int_info = inst_ty.intInfo(f.object.dg.module.getTarget()); |
| 1162 | 1199 | const bits = int_info.bits; |
| 1163 | 1200 | |
| 1164 | 1201 | // if it's an unsigned int with non-arbitrary bit size then we can just add |
| ... | ... | @@ -1168,12 +1205,12 @@ fn airWrapOp( |
| 1168 | 1205 | else => false, |
| 1169 | 1206 | }; |
| 1170 | 1207 | if (ok_bits or inst_ty.tag() != .int_unsigned) { |
| 1171 | | return try airBinOp(o, inst, str_op); |
| 1208 | return try airBinOp(f, inst, str_op); |
| 1172 | 1209 | } |
| 1173 | 1210 | } |
| 1174 | 1211 | |
| 1175 | 1212 | if (bits > 64) { |
| 1176 | | return o.dg.fail("TODO: C backend: airWrapOp for large integers", .{}); |
| 1213 | return f.fail("TODO: C backend: airWrapOp for large integers", .{}); |
| 1177 | 1214 | } |
| 1178 | 1215 | |
| 1179 | 1216 | var min_buf: [80]u8 = undefined; |
| ... | ... | @@ -1220,11 +1257,11 @@ fn airWrapOp( |
| 1220 | 1257 | }, |
| 1221 | 1258 | }; |
| 1222 | 1259 | |
| 1223 | | const lhs = try o.resolveInst(bin_op.lhs); |
| 1224 | | const rhs = try o.resolveInst(bin_op.rhs); |
| 1225 | | const w = o.writer(); |
| 1260 | const lhs = try f.resolveInst(bin_op.lhs); |
| 1261 | const rhs = try f.resolveInst(bin_op.rhs); |
| 1262 | const w = f.object.writer(); |
| 1226 | 1263 | |
| 1227 | | const ret = try o.allocLocal(inst_ty, .Mut); |
| 1264 | const ret = try f.allocLocal(inst_ty, .Mut); |
| 1228 | 1265 | try w.print(" = zig_{s}", .{fn_op}); |
| 1229 | 1266 | |
| 1230 | 1267 | switch (inst_ty.tag()) { |
| ... | ... | @@ -1250,71 +1287,71 @@ fn airWrapOp( |
| 1250 | 1287 | } |
| 1251 | 1288 | |
| 1252 | 1289 | try w.writeByte('('); |
| 1253 | | try o.writeCValue(w, lhs); |
| 1290 | try f.writeCValue(w, lhs); |
| 1254 | 1291 | try w.writeAll(", "); |
| 1255 | | try o.writeCValue(w, rhs); |
| 1292 | try f.writeCValue(w, rhs); |
| 1256 | 1293 | |
| 1257 | 1294 | if (int_info.signedness == .signed) { |
| 1258 | 1295 | try w.print(", {s}", .{min}); |
| 1259 | 1296 | } |
| 1260 | 1297 | |
| 1261 | 1298 | try w.print(", {s});", .{max}); |
| 1262 | | try o.indent_writer.insertNewline(); |
| 1299 | try f.object.indent_writer.insertNewline(); |
| 1263 | 1300 | |
| 1264 | 1301 | return ret; |
| 1265 | 1302 | } |
| 1266 | 1303 | |
| 1267 | | fn airNot(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1268 | | if (o.liveness.isUnused(inst)) |
| 1304 | fn airNot(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1305 | if (f.liveness.isUnused(inst)) |
| 1269 | 1306 | return CValue.none; |
| 1270 | 1307 | |
| 1271 | | const ty_op = o.air.instructions.items(.data)[inst].ty_op; |
| 1272 | | const op = try o.resolveInst(ty_op.operand); |
| 1308 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 1309 | const op = try f.resolveInst(ty_op.operand); |
| 1273 | 1310 | |
| 1274 | | const writer = o.writer(); |
| 1275 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1276 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1311 | const writer = f.object.writer(); |
| 1312 | const inst_ty = f.air.typeOfIndex(inst); |
| 1313 | const local = try f.allocLocal(inst_ty, .Const); |
| 1277 | 1314 | |
| 1278 | 1315 | try writer.writeAll(" = "); |
| 1279 | 1316 | if (inst_ty.zigTypeTag() == .Bool) |
| 1280 | 1317 | try writer.writeAll("!") |
| 1281 | 1318 | else |
| 1282 | 1319 | try writer.writeAll("~"); |
| 1283 | | try o.writeCValue(writer, op); |
| 1320 | try f.writeCValue(writer, op); |
| 1284 | 1321 | try writer.writeAll(";\n"); |
| 1285 | 1322 | |
| 1286 | 1323 | return local; |
| 1287 | 1324 | } |
| 1288 | 1325 | |
| 1289 | | fn airBinOp(o: *Object, inst: Air.Inst.Index, operator: [*:0]const u8) !CValue { |
| 1290 | | if (o.liveness.isUnused(inst)) |
| 1326 | fn airBinOp(f: *Function, inst: Air.Inst.Index, operator: [*:0]const u8) !CValue { |
| 1327 | if (f.liveness.isUnused(inst)) |
| 1291 | 1328 | return CValue.none; |
| 1292 | 1329 | |
| 1293 | | const bin_op = o.air.instructions.items(.data)[inst].bin_op; |
| 1294 | | const lhs = try o.resolveInst(bin_op.lhs); |
| 1295 | | const rhs = try o.resolveInst(bin_op.rhs); |
| 1330 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 1331 | const lhs = try f.resolveInst(bin_op.lhs); |
| 1332 | const rhs = try f.resolveInst(bin_op.rhs); |
| 1296 | 1333 | |
| 1297 | | const writer = o.writer(); |
| 1298 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1299 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1334 | const writer = f.object.writer(); |
| 1335 | const inst_ty = f.air.typeOfIndex(inst); |
| 1336 | const local = try f.allocLocal(inst_ty, .Const); |
| 1300 | 1337 | |
| 1301 | 1338 | try writer.writeAll(" = "); |
| 1302 | | try o.writeCValue(writer, lhs); |
| 1339 | try f.writeCValue(writer, lhs); |
| 1303 | 1340 | try writer.print("{s}", .{operator}); |
| 1304 | | try o.writeCValue(writer, rhs); |
| 1341 | try f.writeCValue(writer, rhs); |
| 1305 | 1342 | try writer.writeAll(";\n"); |
| 1306 | 1343 | |
| 1307 | 1344 | return local; |
| 1308 | 1345 | } |
| 1309 | 1346 | |
| 1310 | | fn airCall(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1311 | | const pl_op = o.air.instructions.items(.data)[inst].pl_op; |
| 1312 | | const extra = o.air.extraData(Air.Call, pl_op.payload); |
| 1313 | | const args = @bitCast([]const Air.Inst.Ref, o.air.extra[extra.end..][0..extra.data.args_len]); |
| 1314 | | const fn_ty = o.air.typeOf(pl_op.operand); |
| 1347 | fn airCall(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1348 | const pl_op = f.air.instructions.items(.data)[inst].pl_op; |
| 1349 | const extra = f.air.extraData(Air.Call, pl_op.payload); |
| 1350 | const args = @bitCast([]const Air.Inst.Ref, f.air.extra[extra.end..][0..extra.data.args_len]); |
| 1351 | const fn_ty = f.air.typeOf(pl_op.operand); |
| 1315 | 1352 | const ret_ty = fn_ty.fnReturnType(); |
| 1316 | | const unused_result = o.liveness.isUnused(inst); |
| 1317 | | const writer = o.writer(); |
| 1353 | const unused_result = f.liveness.isUnused(inst); |
| 1354 | const writer = f.object.writer(); |
| 1318 | 1355 | |
| 1319 | 1356 | var result_local: CValue = .none; |
| 1320 | 1357 | if (unused_result) { |
| ... | ... | @@ -1322,11 +1359,11 @@ fn airCall(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1322 | 1359 | try writer.print("(void)", .{}); |
| 1323 | 1360 | } |
| 1324 | 1361 | } else { |
| 1325 | | result_local = try o.allocLocal(ret_ty, .Const); |
| 1362 | result_local = try f.allocLocal(ret_ty, .Const); |
| 1326 | 1363 | try writer.writeAll(" = "); |
| 1327 | 1364 | } |
| 1328 | 1365 | |
| 1329 | | if (o.air.value(pl_op.operand)) |func_val| { |
| 1366 | if (f.air.value(pl_op.operand)) |func_val| { |
| 1330 | 1367 | const fn_decl = if (func_val.castTag(.extern_fn)) |extern_fn| |
| 1331 | 1368 | extern_fn.data |
| 1332 | 1369 | else if (func_val.castTag(.function)) |func_payload| |
| ... | ... | @@ -1336,8 +1373,8 @@ fn airCall(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1336 | 1373 | |
| 1337 | 1374 | try writer.writeAll(mem.spanZ(fn_decl.name)); |
| 1338 | 1375 | } else { |
| 1339 | | const callee = try o.resolveInst(pl_op.operand); |
| 1340 | | try o.writeCValue(writer, callee); |
| 1376 | const callee = try f.resolveInst(pl_op.operand); |
| 1377 | try f.writeCValue(writer, callee); |
| 1341 | 1378 | } |
| 1342 | 1379 | |
| 1343 | 1380 | try writer.writeAll("("); |
| ... | ... | @@ -1345,113 +1382,113 @@ fn airCall(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1345 | 1382 | if (i != 0) { |
| 1346 | 1383 | try writer.writeAll(", "); |
| 1347 | 1384 | } |
| 1348 | | if (o.air.value(arg)) |val| { |
| 1349 | | try o.dg.renderValue(writer, o.air.typeOf(arg), val); |
| 1385 | if (f.air.value(arg)) |val| { |
| 1386 | try f.object.dg.renderValue(writer, f.air.typeOf(arg), val); |
| 1350 | 1387 | } else { |
| 1351 | | const val = try o.resolveInst(arg); |
| 1352 | | try o.writeCValue(writer, val); |
| 1388 | const val = try f.resolveInst(arg); |
| 1389 | try f.writeCValue(writer, val); |
| 1353 | 1390 | } |
| 1354 | 1391 | } |
| 1355 | 1392 | try writer.writeAll(");\n"); |
| 1356 | 1393 | return result_local; |
| 1357 | 1394 | } |
| 1358 | 1395 | |
| 1359 | | fn airDbgStmt(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1360 | | const dbg_stmt = o.air.instructions.items(.data)[inst].dbg_stmt; |
| 1361 | | const writer = o.writer(); |
| 1396 | fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1397 | const dbg_stmt = f.air.instructions.items(.data)[inst].dbg_stmt; |
| 1398 | const writer = f.object.writer(); |
| 1362 | 1399 | try writer.print("#line {d}\n", .{dbg_stmt.line + 1}); |
| 1363 | 1400 | return CValue.none; |
| 1364 | 1401 | } |
| 1365 | 1402 | |
| 1366 | | fn airBlock(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1367 | | const ty_pl = o.air.instructions.items(.data)[inst].ty_pl; |
| 1368 | | const extra = o.air.extraData(Air.Block, ty_pl.payload); |
| 1369 | | const body = o.air.extra[extra.end..][0..extra.data.body_len]; |
| 1403 | fn airBlock(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1404 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 1405 | const extra = f.air.extraData(Air.Block, ty_pl.payload); |
| 1406 | const body = f.air.extra[extra.end..][0..extra.data.body_len]; |
| 1370 | 1407 | |
| 1371 | | const block_id: usize = o.next_block_index; |
| 1372 | | o.next_block_index += 1; |
| 1373 | | const writer = o.writer(); |
| 1408 | const block_id: usize = f.next_block_index; |
| 1409 | f.next_block_index += 1; |
| 1410 | const writer = f.object.writer(); |
| 1374 | 1411 | |
| 1375 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1376 | | const result = if (inst_ty.tag() != .void and !o.liveness.isUnused(inst)) blk: { |
| 1412 | const inst_ty = f.air.typeOfIndex(inst); |
| 1413 | const result = if (inst_ty.tag() != .void and !f.liveness.isUnused(inst)) blk: { |
| 1377 | 1414 | // allocate a location for the result |
| 1378 | | const local = try o.allocLocal(inst_ty, .Mut); |
| 1415 | const local = try f.allocLocal(inst_ty, .Mut); |
| 1379 | 1416 | try writer.writeAll(";\n"); |
| 1380 | 1417 | break :blk local; |
| 1381 | 1418 | } else CValue{ .none = {} }; |
| 1382 | 1419 | |
| 1383 | | try o.blocks.putNoClobber(o.gpa, inst, .{ |
| 1420 | try f.blocks.putNoClobber(f.object.dg.gpa, inst, .{ |
| 1384 | 1421 | .block_id = block_id, |
| 1385 | 1422 | .result = result, |
| 1386 | 1423 | }); |
| 1387 | 1424 | |
| 1388 | | try genBody(o, body); |
| 1389 | | try o.indent_writer.insertNewline(); |
| 1425 | try genBody(f, body); |
| 1426 | try f.object.indent_writer.insertNewline(); |
| 1390 | 1427 | // label must be followed by an expression, add an empty one. |
| 1391 | 1428 | try writer.print("zig_block_{d}:;\n", .{block_id}); |
| 1392 | 1429 | return result; |
| 1393 | 1430 | } |
| 1394 | 1431 | |
| 1395 | | fn airBr(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1396 | | const branch = o.air.instructions.items(.data)[inst].br; |
| 1397 | | const block = o.blocks.get(branch.block_inst).?; |
| 1432 | fn airBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1433 | const branch = f.air.instructions.items(.data)[inst].br; |
| 1434 | const block = f.blocks.get(branch.block_inst).?; |
| 1398 | 1435 | const result = block.result; |
| 1399 | | const writer = o.writer(); |
| 1436 | const writer = f.object.writer(); |
| 1400 | 1437 | |
| 1401 | 1438 | // If result is .none then the value of the block is unused. |
| 1402 | 1439 | if (result != .none) { |
| 1403 | | const operand = try o.resolveInst(branch.operand); |
| 1404 | | try o.writeCValue(writer, result); |
| 1440 | const operand = try f.resolveInst(branch.operand); |
| 1441 | try f.writeCValue(writer, result); |
| 1405 | 1442 | try writer.writeAll(" = "); |
| 1406 | | try o.writeCValue(writer, operand); |
| 1443 | try f.writeCValue(writer, operand); |
| 1407 | 1444 | try writer.writeAll(";\n"); |
| 1408 | 1445 | } |
| 1409 | 1446 | |
| 1410 | | try o.writer().print("goto zig_block_{d};\n", .{block.block_id}); |
| 1447 | try f.object.writer().print("goto zig_block_{d};\n", .{block.block_id}); |
| 1411 | 1448 | return CValue.none; |
| 1412 | 1449 | } |
| 1413 | 1450 | |
| 1414 | | fn airBitcast(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1415 | | const ty_op = o.air.instructions.items(.data)[inst].ty_op; |
| 1416 | | const operand = try o.resolveInst(ty_op.operand); |
| 1451 | fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1452 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 1453 | const operand = try f.resolveInst(ty_op.operand); |
| 1417 | 1454 | |
| 1418 | | const writer = o.writer(); |
| 1419 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1455 | const writer = f.object.writer(); |
| 1456 | const inst_ty = f.air.typeOfIndex(inst); |
| 1420 | 1457 | if (inst_ty.zigTypeTag() == .Pointer and |
| 1421 | | o.air.typeOf(ty_op.operand).zigTypeTag() == .Pointer) |
| 1458 | f.air.typeOf(ty_op.operand).zigTypeTag() == .Pointer) |
| 1422 | 1459 | { |
| 1423 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1460 | const local = try f.allocLocal(inst_ty, .Const); |
| 1424 | 1461 | try writer.writeAll(" = ("); |
| 1425 | | try o.dg.renderType(writer, inst_ty); |
| 1462 | try f.renderType(writer, inst_ty); |
| 1426 | 1463 | |
| 1427 | 1464 | try writer.writeAll(")"); |
| 1428 | | try o.writeCValue(writer, operand); |
| 1465 | try f.writeCValue(writer, operand); |
| 1429 | 1466 | try writer.writeAll(";\n"); |
| 1430 | 1467 | return local; |
| 1431 | 1468 | } |
| 1432 | 1469 | |
| 1433 | | const local = try o.allocLocal(inst_ty, .Mut); |
| 1470 | const local = try f.allocLocal(inst_ty, .Mut); |
| 1434 | 1471 | try writer.writeAll(";\n"); |
| 1435 | 1472 | |
| 1436 | 1473 | try writer.writeAll("memcpy(&"); |
| 1437 | | try o.writeCValue(writer, local); |
| 1474 | try f.writeCValue(writer, local); |
| 1438 | 1475 | try writer.writeAll(", &"); |
| 1439 | | try o.writeCValue(writer, operand); |
| 1476 | try f.writeCValue(writer, operand); |
| 1440 | 1477 | try writer.writeAll(", sizeof "); |
| 1441 | | try o.writeCValue(writer, local); |
| 1478 | try f.writeCValue(writer, local); |
| 1442 | 1479 | try writer.writeAll(");\n"); |
| 1443 | 1480 | |
| 1444 | 1481 | return local; |
| 1445 | 1482 | } |
| 1446 | 1483 | |
| 1447 | | fn airBreakpoint(o: *Object) !CValue { |
| 1448 | | try o.writer().writeAll("zig_breakpoint();\n"); |
| 1484 | fn airBreakpoint(f: *Function) !CValue { |
| 1485 | try f.object.writer().writeAll("zig_breakpoint();\n"); |
| 1449 | 1486 | return CValue.none; |
| 1450 | 1487 | } |
| 1451 | 1488 | |
| 1452 | | fn airFence(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1453 | | const atomic_order = o.air.instructions.items(.data)[inst].fence; |
| 1454 | | const writer = o.writer(); |
| 1489 | fn airFence(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1490 | const atomic_order = f.air.instructions.items(.data)[inst].fence; |
| 1491 | const writer = f.object.writer(); |
| 1455 | 1492 | |
| 1456 | 1493 | try writer.writeAll("zig_fence("); |
| 1457 | 1494 | try writeMemoryOrder(writer, atomic_order); |
| ... | ... | @@ -1460,85 +1497,85 @@ fn airFence(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1460 | 1497 | return CValue.none; |
| 1461 | 1498 | } |
| 1462 | 1499 | |
| 1463 | | fn airUnreach(o: *Object) !CValue { |
| 1464 | | try o.writer().writeAll("zig_unreachable();\n"); |
| 1500 | fn airUnreach(f: *Function) !CValue { |
| 1501 | try f.object.writer().writeAll("zig_unreachable();\n"); |
| 1465 | 1502 | return CValue.none; |
| 1466 | 1503 | } |
| 1467 | 1504 | |
| 1468 | | fn airLoop(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1469 | | const ty_pl = o.air.instructions.items(.data)[inst].ty_pl; |
| 1470 | | const loop = o.air.extraData(Air.Block, ty_pl.payload); |
| 1471 | | const body = o.air.extra[loop.end..][0..loop.data.body_len]; |
| 1472 | | try o.writer().writeAll("while (true) "); |
| 1473 | | try genBody(o, body); |
| 1474 | | try o.indent_writer.insertNewline(); |
| 1505 | fn airLoop(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1506 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 1507 | const loop = f.air.extraData(Air.Block, ty_pl.payload); |
| 1508 | const body = f.air.extra[loop.end..][0..loop.data.body_len]; |
| 1509 | try f.object.writer().writeAll("while (true) "); |
| 1510 | try genBody(f, body); |
| 1511 | try f.object.indent_writer.insertNewline(); |
| 1475 | 1512 | return CValue.none; |
| 1476 | 1513 | } |
| 1477 | 1514 | |
| 1478 | | fn airCondBr(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1479 | | const pl_op = o.air.instructions.items(.data)[inst].pl_op; |
| 1480 | | const cond = try o.resolveInst(pl_op.operand); |
| 1481 | | const extra = o.air.extraData(Air.CondBr, pl_op.payload); |
| 1482 | | const then_body = o.air.extra[extra.end..][0..extra.data.then_body_len]; |
| 1483 | | const else_body = o.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; |
| 1484 | | const writer = o.writer(); |
| 1515 | fn airCondBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1516 | const pl_op = f.air.instructions.items(.data)[inst].pl_op; |
| 1517 | const cond = try f.resolveInst(pl_op.operand); |
| 1518 | const extra = f.air.extraData(Air.CondBr, pl_op.payload); |
| 1519 | const then_body = f.air.extra[extra.end..][0..extra.data.then_body_len]; |
| 1520 | const else_body = f.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; |
| 1521 | const writer = f.object.writer(); |
| 1485 | 1522 | |
| 1486 | 1523 | try writer.writeAll("if ("); |
| 1487 | | try o.writeCValue(writer, cond); |
| 1524 | try f.writeCValue(writer, cond); |
| 1488 | 1525 | try writer.writeAll(") "); |
| 1489 | | try genBody(o, then_body); |
| 1526 | try genBody(f, then_body); |
| 1490 | 1527 | try writer.writeAll(" else "); |
| 1491 | | try genBody(o, else_body); |
| 1492 | | try o.indent_writer.insertNewline(); |
| 1528 | try genBody(f, else_body); |
| 1529 | try f.object.indent_writer.insertNewline(); |
| 1493 | 1530 | |
| 1494 | 1531 | return CValue.none; |
| 1495 | 1532 | } |
| 1496 | 1533 | |
| 1497 | | fn airSwitchBr(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1498 | | const pl_op = o.air.instructions.items(.data)[inst].pl_op; |
| 1499 | | const condition = try o.resolveInst(pl_op.operand); |
| 1500 | | const condition_ty = o.air.typeOf(pl_op.operand); |
| 1501 | | const switch_br = o.air.extraData(Air.SwitchBr, pl_op.payload); |
| 1502 | | const writer = o.writer(); |
| 1534 | fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1535 | const pl_op = f.air.instructions.items(.data)[inst].pl_op; |
| 1536 | const condition = try f.resolveInst(pl_op.operand); |
| 1537 | const condition_ty = f.air.typeOf(pl_op.operand); |
| 1538 | const switch_br = f.air.extraData(Air.SwitchBr, pl_op.payload); |
| 1539 | const writer = f.object.writer(); |
| 1503 | 1540 | |
| 1504 | 1541 | try writer.writeAll("switch ("); |
| 1505 | | try o.writeCValue(writer, condition); |
| 1542 | try f.writeCValue(writer, condition); |
| 1506 | 1543 | try writer.writeAll(") {"); |
| 1507 | | o.indent_writer.pushIndent(); |
| 1544 | f.object.indent_writer.pushIndent(); |
| 1508 | 1545 | |
| 1509 | 1546 | var extra_index: usize = switch_br.end; |
| 1510 | 1547 | var case_i: u32 = 0; |
| 1511 | 1548 | while (case_i < switch_br.data.cases_len) : (case_i += 1) { |
| 1512 | | const case = o.air.extraData(Air.SwitchBr.Case, extra_index); |
| 1513 | | const items = @bitCast([]const Air.Inst.Ref, o.air.extra[case.end..][0..case.data.items_len]); |
| 1514 | | const case_body = o.air.extra[case.end + items.len ..][0..case.data.body_len]; |
| 1549 | const case = f.air.extraData(Air.SwitchBr.Case, extra_index); |
| 1550 | const items = @bitCast([]const Air.Inst.Ref, f.air.extra[case.end..][0..case.data.items_len]); |
| 1551 | const case_body = f.air.extra[case.end + items.len ..][0..case.data.body_len]; |
| 1515 | 1552 | extra_index = case.end + case.data.items_len + case_body.len; |
| 1516 | 1553 | |
| 1517 | 1554 | for (items) |item| { |
| 1518 | | try o.indent_writer.insertNewline(); |
| 1555 | try f.object.indent_writer.insertNewline(); |
| 1519 | 1556 | try writer.writeAll("case "); |
| 1520 | | try o.dg.renderValue(writer, condition_ty, o.air.value(item).?); |
| 1557 | try f.object.dg.renderValue(writer, condition_ty, f.air.value(item).?); |
| 1521 | 1558 | try writer.writeAll(": "); |
| 1522 | 1559 | } |
| 1523 | 1560 | // The case body must be noreturn so we don't need to insert a break. |
| 1524 | | try genBody(o, case_body); |
| 1561 | try genBody(f, case_body); |
| 1525 | 1562 | } |
| 1526 | 1563 | |
| 1527 | | const else_body = o.air.extra[extra_index..][0..switch_br.data.else_body_len]; |
| 1528 | | try o.indent_writer.insertNewline(); |
| 1564 | const else_body = f.air.extra[extra_index..][0..switch_br.data.else_body_len]; |
| 1565 | try f.object.indent_writer.insertNewline(); |
| 1529 | 1566 | try writer.writeAll("default: "); |
| 1530 | | try genBody(o, else_body); |
| 1531 | | try o.indent_writer.insertNewline(); |
| 1567 | try genBody(f, else_body); |
| 1568 | try f.object.indent_writer.insertNewline(); |
| 1532 | 1569 | |
| 1533 | | o.indent_writer.popIndent(); |
| 1570 | f.object.indent_writer.popIndent(); |
| 1534 | 1571 | try writer.writeAll("}\n"); |
| 1535 | 1572 | return CValue.none; |
| 1536 | 1573 | } |
| 1537 | 1574 | |
| 1538 | | fn airAsm(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1539 | | const air_datas = o.air.instructions.items(.data); |
| 1540 | | const air_extra = o.air.extraData(Air.Asm, air_datas[inst].ty_pl.payload); |
| 1541 | | const zir = o.dg.decl.namespace.file_scope.zir; |
| 1575 | fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1576 | const air_datas = f.air.instructions.items(.data); |
| 1577 | const air_extra = f.air.extraData(Air.Asm, air_datas[inst].ty_pl.payload); |
| 1578 | const zir = f.object.dg.decl.namespace.file_scope.zir; |
| 1542 | 1579 | const extended = zir.instructions.items(.data)[air_extra.data.zir_index].extended; |
| 1543 | 1580 | const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand); |
| 1544 | 1581 | const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source); |
| ... | ... | @@ -1547,14 +1584,14 @@ fn airAsm(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1547 | 1584 | const clobbers_len = @truncate(u5, extended.small >> 10); |
| 1548 | 1585 | _ = clobbers_len; // TODO honor these |
| 1549 | 1586 | const is_volatile = @truncate(u1, extended.small >> 15) != 0; |
| 1550 | | const outputs = @bitCast([]const Air.Inst.Ref, o.air.extra[air_extra.end..][0..outputs_len]); |
| 1551 | | const args = @bitCast([]const Air.Inst.Ref, o.air.extra[air_extra.end + outputs.len ..][0..args_len]); |
| 1587 | const outputs = @bitCast([]const Air.Inst.Ref, f.air.extra[air_extra.end..][0..outputs_len]); |
| 1588 | const args = @bitCast([]const Air.Inst.Ref, f.air.extra[air_extra.end + outputs.len ..][0..args_len]); |
| 1552 | 1589 | |
| 1553 | 1590 | if (outputs_len > 1) { |
| 1554 | | return o.dg.fail("TODO implement codegen for asm with more than 1 output", .{}); |
| 1591 | return f.fail("TODO implement codegen for asm with more than 1 output", .{}); |
| 1555 | 1592 | } |
| 1556 | 1593 | |
| 1557 | | if (o.liveness.isUnused(inst) and !is_volatile) |
| 1594 | if (f.liveness.isUnused(inst) and !is_volatile) |
| 1558 | 1595 | return CValue.none; |
| 1559 | 1596 | |
| 1560 | 1597 | var extra_i: usize = zir_extra.end; |
| ... | ... | @@ -1569,28 +1606,28 @@ fn airAsm(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1569 | 1606 | }; |
| 1570 | 1607 | const args_extra_begin = extra_i; |
| 1571 | 1608 | |
| 1572 | | const writer = o.writer(); |
| 1609 | const writer = f.object.writer(); |
| 1573 | 1610 | for (args) |arg| { |
| 1574 | 1611 | const input = zir.extraData(Zir.Inst.Asm.Input, extra_i); |
| 1575 | 1612 | extra_i = input.end; |
| 1576 | 1613 | const constraint = zir.nullTerminatedString(input.data.constraint); |
| 1577 | 1614 | if (constraint[0] == '{' and constraint[constraint.len - 1] == '}') { |
| 1578 | 1615 | const reg = constraint[1 .. constraint.len - 1]; |
| 1579 | | const arg_c_value = try o.resolveInst(arg); |
| 1616 | const arg_c_value = try f.resolveInst(arg); |
| 1580 | 1617 | try writer.writeAll("register "); |
| 1581 | | try o.dg.renderType(writer, o.air.typeOf(arg)); |
| 1618 | try f.renderType(writer, f.air.typeOf(arg)); |
| 1582 | 1619 | |
| 1583 | 1620 | try writer.print(" {s}_constant __asm__(\"{s}\") = ", .{ reg, reg }); |
| 1584 | | try o.writeCValue(writer, arg_c_value); |
| 1621 | try f.writeCValue(writer, arg_c_value); |
| 1585 | 1622 | try writer.writeAll(";\n"); |
| 1586 | 1623 | } else { |
| 1587 | | return o.dg.fail("TODO non-explicit inline asm regs", .{}); |
| 1624 | return f.fail("TODO non-explicit inline asm regs", .{}); |
| 1588 | 1625 | } |
| 1589 | 1626 | } |
| 1590 | 1627 | const volatile_string: []const u8 = if (is_volatile) "volatile " else ""; |
| 1591 | 1628 | try writer.print("__asm {s}(\"{s}\"", .{ volatile_string, asm_source }); |
| 1592 | 1629 | if (output_constraint) |_| { |
| 1593 | | return o.dg.fail("TODO: CBE inline asm output", .{}); |
| 1630 | return f.fail("TODO: CBE inline asm output", .{}); |
| 1594 | 1631 | } |
| 1595 | 1632 | if (args.len > 0) { |
| 1596 | 1633 | if (output_constraint == null) { |
| ... | ... | @@ -1616,30 +1653,30 @@ fn airAsm(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1616 | 1653 | } |
| 1617 | 1654 | try writer.writeAll(");\n"); |
| 1618 | 1655 | |
| 1619 | | if (o.liveness.isUnused(inst)) |
| 1656 | if (f.liveness.isUnused(inst)) |
| 1620 | 1657 | return CValue.none; |
| 1621 | 1658 | |
| 1622 | | return o.dg.fail("TODO: C backend: inline asm expression result used", .{}); |
| 1659 | return f.fail("TODO: C backend: inline asm expression result used", .{}); |
| 1623 | 1660 | } |
| 1624 | 1661 | |
| 1625 | 1662 | fn airIsNull( |
| 1626 | | o: *Object, |
| 1663 | f: *Function, |
| 1627 | 1664 | inst: Air.Inst.Index, |
| 1628 | 1665 | operator: [*:0]const u8, |
| 1629 | 1666 | deref_suffix: [*:0]const u8, |
| 1630 | 1667 | ) !CValue { |
| 1631 | | if (o.liveness.isUnused(inst)) |
| 1668 | if (f.liveness.isUnused(inst)) |
| 1632 | 1669 | return CValue.none; |
| 1633 | 1670 | |
| 1634 | | const un_op = o.air.instructions.items(.data)[inst].un_op; |
| 1635 | | const writer = o.writer(); |
| 1636 | | const operand = try o.resolveInst(un_op); |
| 1671 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| 1672 | const writer = f.object.writer(); |
| 1673 | const operand = try f.resolveInst(un_op); |
| 1637 | 1674 | |
| 1638 | | const local = try o.allocLocal(Type.initTag(.bool), .Const); |
| 1675 | const local = try f.allocLocal(Type.initTag(.bool), .Const); |
| 1639 | 1676 | try writer.writeAll(" = ("); |
| 1640 | | try o.writeCValue(writer, operand); |
| 1677 | try f.writeCValue(writer, operand); |
| 1641 | 1678 | |
| 1642 | | if (o.air.typeOf(un_op).isPtrLikeOptional()) { |
| 1679 | if (f.air.typeOf(un_op).isPtrLikeOptional()) { |
| 1643 | 1680 | // operand is a regular pointer, test `operand !=/== NULL` |
| 1644 | 1681 | try writer.print("){s} {s} NULL;\n", .{ deref_suffix, operator }); |
| 1645 | 1682 | } else { |
| ... | ... | @@ -1648,14 +1685,14 @@ fn airIsNull( |
| 1648 | 1685 | return local; |
| 1649 | 1686 | } |
| 1650 | 1687 | |
| 1651 | | fn airOptionalPayload(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1652 | | if (o.liveness.isUnused(inst)) |
| 1688 | fn airOptionalPayload(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1689 | if (f.liveness.isUnused(inst)) |
| 1653 | 1690 | return CValue.none; |
| 1654 | 1691 | |
| 1655 | | const ty_op = o.air.instructions.items(.data)[inst].ty_op; |
| 1656 | | const writer = o.writer(); |
| 1657 | | const operand = try o.resolveInst(ty_op.operand); |
| 1658 | | const operand_ty = o.air.typeOf(ty_op.operand); |
| 1692 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 1693 | const writer = f.object.writer(); |
| 1694 | const operand = try f.resolveInst(ty_op.operand); |
| 1695 | const operand_ty = f.air.typeOf(ty_op.operand); |
| 1659 | 1696 | |
| 1660 | 1697 | const opt_ty = if (operand_ty.zigTypeTag() == .Pointer) |
| 1661 | 1698 | operand_ty.elemType() |
| ... | ... | @@ -1668,98 +1705,98 @@ fn airOptionalPayload(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1668 | 1705 | return operand; |
| 1669 | 1706 | } |
| 1670 | 1707 | |
| 1671 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1708 | const inst_ty = f.air.typeOfIndex(inst); |
| 1672 | 1709 | const maybe_deref = if (operand_ty.zigTypeTag() == .Pointer) "->" else "."; |
| 1673 | 1710 | const maybe_addrof = if (inst_ty.zigTypeTag() == .Pointer) "&" else ""; |
| 1674 | 1711 | |
| 1675 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1712 | const local = try f.allocLocal(inst_ty, .Const); |
| 1676 | 1713 | try writer.print(" = {s}(", .{maybe_addrof}); |
| 1677 | | try o.writeCValue(writer, operand); |
| 1714 | try f.writeCValue(writer, operand); |
| 1678 | 1715 | |
| 1679 | 1716 | try writer.print("){s}payload;\n", .{maybe_deref}); |
| 1680 | 1717 | return local; |
| 1681 | 1718 | } |
| 1682 | 1719 | |
| 1683 | | fn airStructFieldPtr(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1684 | | if (o.liveness.isUnused(inst)) |
| 1720 | fn airStructFieldPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1721 | if (f.liveness.isUnused(inst)) |
| 1685 | 1722 | // TODO this @as is needed because of a stage1 bug |
| 1686 | 1723 | return @as(CValue, CValue.none); |
| 1687 | 1724 | |
| 1688 | | const ty_pl = o.air.instructions.items(.data)[inst].ty_pl; |
| 1689 | | const extra = o.air.extraData(Air.StructField, ty_pl.payload).data; |
| 1690 | | const struct_ptr = try o.resolveInst(extra.struct_operand); |
| 1691 | | const struct_ptr_ty = o.air.typeOf(extra.struct_operand); |
| 1692 | | return structFieldPtr(o, inst, struct_ptr_ty, struct_ptr, extra.field_index); |
| 1725 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 1726 | const extra = f.air.extraData(Air.StructField, ty_pl.payload).data; |
| 1727 | const struct_ptr = try f.resolveInst(extra.struct_operand); |
| 1728 | const struct_ptr_ty = f.air.typeOf(extra.struct_operand); |
| 1729 | return structFieldPtr(f, inst, struct_ptr_ty, struct_ptr, extra.field_index); |
| 1693 | 1730 | } |
| 1694 | 1731 | |
| 1695 | | fn airStructFieldPtrIndex(o: *Object, inst: Air.Inst.Index, index: u8) !CValue { |
| 1696 | | if (o.liveness.isUnused(inst)) |
| 1732 | fn airStructFieldPtrIndex(f: *Function, inst: Air.Inst.Index, index: u8) !CValue { |
| 1733 | if (f.liveness.isUnused(inst)) |
| 1697 | 1734 | // TODO this @as is needed because of a stage1 bug |
| 1698 | 1735 | return @as(CValue, CValue.none); |
| 1699 | 1736 | |
| 1700 | | const ty_op = o.air.instructions.items(.data)[inst].ty_op; |
| 1701 | | const struct_ptr = try o.resolveInst(ty_op.operand); |
| 1702 | | const struct_ptr_ty = o.air.typeOf(ty_op.operand); |
| 1703 | | return structFieldPtr(o, inst, struct_ptr_ty, struct_ptr, index); |
| 1737 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 1738 | const struct_ptr = try f.resolveInst(ty_op.operand); |
| 1739 | const struct_ptr_ty = f.air.typeOf(ty_op.operand); |
| 1740 | return structFieldPtr(f, inst, struct_ptr_ty, struct_ptr, index); |
| 1704 | 1741 | } |
| 1705 | 1742 | |
| 1706 | | fn structFieldPtr(o: *Object, inst: Air.Inst.Index, struct_ptr_ty: Type, struct_ptr: CValue, index: u32) !CValue { |
| 1707 | | const writer = o.writer(); |
| 1743 | fn structFieldPtr(f: *Function, inst: Air.Inst.Index, struct_ptr_ty: Type, struct_ptr: CValue, index: u32) !CValue { |
| 1744 | const writer = f.object.writer(); |
| 1708 | 1745 | const struct_obj = struct_ptr_ty.elemType().castTag(.@"struct").?.data; |
| 1709 | 1746 | const field_name = struct_obj.fields.keys()[index]; |
| 1710 | 1747 | |
| 1711 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1712 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1748 | const inst_ty = f.air.typeOfIndex(inst); |
| 1749 | const local = try f.allocLocal(inst_ty, .Const); |
| 1713 | 1750 | switch (struct_ptr) { |
| 1714 | 1751 | .local_ref => |i| { |
| 1715 | 1752 | try writer.print(" = &t{d}.{};\n", .{ i, fmtIdent(field_name) }); |
| 1716 | 1753 | }, |
| 1717 | 1754 | else => { |
| 1718 | 1755 | try writer.writeAll(" = &"); |
| 1719 | | try o.writeCValue(writer, struct_ptr); |
| 1756 | try f.writeCValue(writer, struct_ptr); |
| 1720 | 1757 | try writer.print("->{};\n", .{fmtIdent(field_name)}); |
| 1721 | 1758 | }, |
| 1722 | 1759 | } |
| 1723 | 1760 | return local; |
| 1724 | 1761 | } |
| 1725 | 1762 | |
| 1726 | | fn airStructFieldVal(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1727 | | if (o.liveness.isUnused(inst)) |
| 1763 | fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1764 | if (f.liveness.isUnused(inst)) |
| 1728 | 1765 | return CValue.none; |
| 1729 | 1766 | |
| 1730 | | const ty_pl = o.air.instructions.items(.data)[inst].ty_pl; |
| 1731 | | const extra = o.air.extraData(Air.StructField, ty_pl.payload).data; |
| 1732 | | const writer = o.writer(); |
| 1733 | | const struct_byval = try o.resolveInst(extra.struct_operand); |
| 1734 | | const struct_ty = o.air.typeOf(extra.struct_operand); |
| 1767 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 1768 | const extra = f.air.extraData(Air.StructField, ty_pl.payload).data; |
| 1769 | const writer = f.object.writer(); |
| 1770 | const struct_byval = try f.resolveInst(extra.struct_operand); |
| 1771 | const struct_ty = f.air.typeOf(extra.struct_operand); |
| 1735 | 1772 | const struct_obj = struct_ty.castTag(.@"struct").?.data; |
| 1736 | 1773 | const field_name = struct_obj.fields.keys()[extra.field_index]; |
| 1737 | 1774 | |
| 1738 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1739 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1775 | const inst_ty = f.air.typeOfIndex(inst); |
| 1776 | const local = try f.allocLocal(inst_ty, .Const); |
| 1740 | 1777 | try writer.writeAll(" = "); |
| 1741 | | try o.writeCValue(writer, struct_byval); |
| 1778 | try f.writeCValue(writer, struct_byval); |
| 1742 | 1779 | try writer.print(".{};\n", .{fmtIdent(field_name)}); |
| 1743 | 1780 | return local; |
| 1744 | 1781 | } |
| 1745 | 1782 | |
| 1746 | 1783 | // *(E!T) -> E NOT *E |
| 1747 | | fn airUnwrapErrUnionErr(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1748 | | if (o.liveness.isUnused(inst)) |
| 1784 | fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1785 | if (f.liveness.isUnused(inst)) |
| 1749 | 1786 | return CValue.none; |
| 1750 | 1787 | |
| 1751 | | const ty_op = o.air.instructions.items(.data)[inst].ty_op; |
| 1752 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1753 | | const writer = o.writer(); |
| 1754 | | const operand = try o.resolveInst(ty_op.operand); |
| 1755 | | const operand_ty = o.air.typeOf(ty_op.operand); |
| 1788 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 1789 | const inst_ty = f.air.typeOfIndex(inst); |
| 1790 | const writer = f.object.writer(); |
| 1791 | const operand = try f.resolveInst(ty_op.operand); |
| 1792 | const operand_ty = f.air.typeOf(ty_op.operand); |
| 1756 | 1793 | |
| 1757 | 1794 | const payload_ty = operand_ty.errorUnionPayload(); |
| 1758 | 1795 | if (!payload_ty.hasCodeGenBits()) { |
| 1759 | 1796 | if (operand_ty.zigTypeTag() == .Pointer) { |
| 1760 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1797 | const local = try f.allocLocal(inst_ty, .Const); |
| 1761 | 1798 | try writer.writeAll(" = *"); |
| 1762 | | try o.writeCValue(writer, operand); |
| 1799 | try f.writeCValue(writer, operand); |
| 1763 | 1800 | try writer.writeAll(";\n"); |
| 1764 | 1801 | return local; |
| 1765 | 1802 | } else { |
| ... | ... | @@ -1769,172 +1806,172 @@ fn airUnwrapErrUnionErr(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1769 | 1806 | |
| 1770 | 1807 | const maybe_deref = if (operand_ty.zigTypeTag() == .Pointer) "->" else "."; |
| 1771 | 1808 | |
| 1772 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1809 | const local = try f.allocLocal(inst_ty, .Const); |
| 1773 | 1810 | try writer.writeAll(" = ("); |
| 1774 | | try o.writeCValue(writer, operand); |
| 1811 | try f.writeCValue(writer, operand); |
| 1775 | 1812 | |
| 1776 | 1813 | try writer.print("){s}error;\n", .{maybe_deref}); |
| 1777 | 1814 | return local; |
| 1778 | 1815 | } |
| 1779 | 1816 | |
| 1780 | | fn airUnwrapErrUnionPay(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1781 | | if (o.liveness.isUnused(inst)) |
| 1817 | fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1818 | if (f.liveness.isUnused(inst)) |
| 1782 | 1819 | return CValue.none; |
| 1783 | 1820 | |
| 1784 | | const ty_op = o.air.instructions.items(.data)[inst].ty_op; |
| 1785 | | const writer = o.writer(); |
| 1786 | | const operand = try o.resolveInst(ty_op.operand); |
| 1787 | | const operand_ty = o.air.typeOf(ty_op.operand); |
| 1821 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 1822 | const writer = f.object.writer(); |
| 1823 | const operand = try f.resolveInst(ty_op.operand); |
| 1824 | const operand_ty = f.air.typeOf(ty_op.operand); |
| 1788 | 1825 | |
| 1789 | 1826 | const payload_ty = operand_ty.errorUnionPayload(); |
| 1790 | 1827 | if (!payload_ty.hasCodeGenBits()) { |
| 1791 | 1828 | return CValue.none; |
| 1792 | 1829 | } |
| 1793 | 1830 | |
| 1794 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1831 | const inst_ty = f.air.typeOfIndex(inst); |
| 1795 | 1832 | const maybe_deref = if (operand_ty.zigTypeTag() == .Pointer) "->" else "."; |
| 1796 | 1833 | const maybe_addrof = if (inst_ty.zigTypeTag() == .Pointer) "&" else ""; |
| 1797 | 1834 | |
| 1798 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1835 | const local = try f.allocLocal(inst_ty, .Const); |
| 1799 | 1836 | try writer.print(" = {s}(", .{maybe_addrof}); |
| 1800 | | try o.writeCValue(writer, operand); |
| 1837 | try f.writeCValue(writer, operand); |
| 1801 | 1838 | |
| 1802 | 1839 | try writer.print("){s}payload;\n", .{maybe_deref}); |
| 1803 | 1840 | return local; |
| 1804 | 1841 | } |
| 1805 | 1842 | |
| 1806 | | fn airWrapOptional(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1807 | | if (o.liveness.isUnused(inst)) |
| 1843 | fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1844 | if (f.liveness.isUnused(inst)) |
| 1808 | 1845 | return CValue.none; |
| 1809 | 1846 | |
| 1810 | | const ty_op = o.air.instructions.items(.data)[inst].ty_op; |
| 1811 | | const writer = o.writer(); |
| 1812 | | const operand = try o.resolveInst(ty_op.operand); |
| 1847 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 1848 | const writer = f.object.writer(); |
| 1849 | const operand = try f.resolveInst(ty_op.operand); |
| 1813 | 1850 | |
| 1814 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1851 | const inst_ty = f.air.typeOfIndex(inst); |
| 1815 | 1852 | if (inst_ty.isPtrLikeOptional()) { |
| 1816 | 1853 | // the operand is just a regular pointer, no need to do anything special. |
| 1817 | 1854 | return operand; |
| 1818 | 1855 | } |
| 1819 | 1856 | |
| 1820 | 1857 | // .wrap_optional is used to convert non-optionals into optionals so it can never be null. |
| 1821 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1858 | const local = try f.allocLocal(inst_ty, .Const); |
| 1822 | 1859 | try writer.writeAll(" = { .is_null = false, .payload ="); |
| 1823 | | try o.writeCValue(writer, operand); |
| 1860 | try f.writeCValue(writer, operand); |
| 1824 | 1861 | try writer.writeAll("};\n"); |
| 1825 | 1862 | return local; |
| 1826 | 1863 | } |
| 1827 | | fn airWrapErrUnionErr(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1828 | | if (o.liveness.isUnused(inst)) |
| 1864 | fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1865 | if (f.liveness.isUnused(inst)) |
| 1829 | 1866 | return CValue.none; |
| 1830 | 1867 | |
| 1831 | | const writer = o.writer(); |
| 1832 | | const ty_op = o.air.instructions.items(.data)[inst].ty_op; |
| 1833 | | const operand = try o.resolveInst(ty_op.operand); |
| 1868 | const writer = f.object.writer(); |
| 1869 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 1870 | const operand = try f.resolveInst(ty_op.operand); |
| 1834 | 1871 | |
| 1835 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1836 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1872 | const inst_ty = f.air.typeOfIndex(inst); |
| 1873 | const local = try f.allocLocal(inst_ty, .Const); |
| 1837 | 1874 | try writer.writeAll(" = { .error = "); |
| 1838 | | try o.writeCValue(writer, operand); |
| 1875 | try f.writeCValue(writer, operand); |
| 1839 | 1876 | try writer.writeAll(" };\n"); |
| 1840 | 1877 | return local; |
| 1841 | 1878 | } |
| 1842 | 1879 | |
| 1843 | | fn airWrapErrUnionPay(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1844 | | if (o.liveness.isUnused(inst)) |
| 1880 | fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1881 | if (f.liveness.isUnused(inst)) |
| 1845 | 1882 | return CValue.none; |
| 1846 | 1883 | |
| 1847 | | const ty_op = o.air.instructions.items(.data)[inst].ty_op; |
| 1848 | | const writer = o.writer(); |
| 1849 | | const operand = try o.resolveInst(ty_op.operand); |
| 1884 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 1885 | const writer = f.object.writer(); |
| 1886 | const operand = try f.resolveInst(ty_op.operand); |
| 1850 | 1887 | |
| 1851 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1852 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1888 | const inst_ty = f.air.typeOfIndex(inst); |
| 1889 | const local = try f.allocLocal(inst_ty, .Const); |
| 1853 | 1890 | try writer.writeAll(" = { .error = 0, .payload = "); |
| 1854 | | try o.writeCValue(writer, operand); |
| 1891 | try f.writeCValue(writer, operand); |
| 1855 | 1892 | try writer.writeAll(" };\n"); |
| 1856 | 1893 | return local; |
| 1857 | 1894 | } |
| 1858 | 1895 | |
| 1859 | 1896 | fn airIsErr( |
| 1860 | | o: *Object, |
| 1897 | f: *Function, |
| 1861 | 1898 | inst: Air.Inst.Index, |
| 1862 | 1899 | deref_prefix: [*:0]const u8, |
| 1863 | 1900 | deref_suffix: [*:0]const u8, |
| 1864 | 1901 | op_str: [*:0]const u8, |
| 1865 | 1902 | ) !CValue { |
| 1866 | | if (o.liveness.isUnused(inst)) |
| 1903 | if (f.liveness.isUnused(inst)) |
| 1867 | 1904 | return CValue.none; |
| 1868 | 1905 | |
| 1869 | | const un_op = o.air.instructions.items(.data)[inst].un_op; |
| 1870 | | const writer = o.writer(); |
| 1871 | | const operand = try o.resolveInst(un_op); |
| 1872 | | const operand_ty = o.air.typeOf(un_op); |
| 1873 | | const local = try o.allocLocal(Type.initTag(.bool), .Const); |
| 1906 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| 1907 | const writer = f.object.writer(); |
| 1908 | const operand = try f.resolveInst(un_op); |
| 1909 | const operand_ty = f.air.typeOf(un_op); |
| 1910 | const local = try f.allocLocal(Type.initTag(.bool), .Const); |
| 1874 | 1911 | const payload_ty = operand_ty.errorUnionPayload(); |
| 1875 | 1912 | if (!payload_ty.hasCodeGenBits()) { |
| 1876 | 1913 | try writer.print(" = {s}", .{deref_prefix}); |
| 1877 | | try o.writeCValue(writer, operand); |
| 1914 | try f.writeCValue(writer, operand); |
| 1878 | 1915 | try writer.print(" {s} 0;\n", .{op_str}); |
| 1879 | 1916 | } else { |
| 1880 | 1917 | try writer.writeAll(" = "); |
| 1881 | | try o.writeCValue(writer, operand); |
| 1918 | try f.writeCValue(writer, operand); |
| 1882 | 1919 | try writer.print("{s}error {s} 0;\n", .{ deref_suffix, op_str }); |
| 1883 | 1920 | } |
| 1884 | 1921 | return local; |
| 1885 | 1922 | } |
| 1886 | 1923 | |
| 1887 | | fn airArrayToSlice(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1888 | | if (o.liveness.isUnused(inst)) |
| 1924 | fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1925 | if (f.liveness.isUnused(inst)) |
| 1889 | 1926 | return CValue.none; |
| 1890 | 1927 | |
| 1891 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1892 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1893 | | const ty_op = o.air.instructions.items(.data)[inst].ty_op; |
| 1894 | | const writer = o.writer(); |
| 1895 | | const operand = try o.resolveInst(ty_op.operand); |
| 1896 | | const array_len = o.air.typeOf(ty_op.operand).elemType().arrayLen(); |
| 1928 | const inst_ty = f.air.typeOfIndex(inst); |
| 1929 | const local = try f.allocLocal(inst_ty, .Const); |
| 1930 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 1931 | const writer = f.object.writer(); |
| 1932 | const operand = try f.resolveInst(ty_op.operand); |
| 1933 | const array_len = f.air.typeOf(ty_op.operand).elemType().arrayLen(); |
| 1897 | 1934 | |
| 1898 | 1935 | try writer.writeAll(" = { .ptr = "); |
| 1899 | | try o.writeCValue(writer, operand); |
| 1936 | try f.writeCValue(writer, operand); |
| 1900 | 1937 | try writer.print(", .len = {d} }};\n", .{array_len}); |
| 1901 | 1938 | return local; |
| 1902 | 1939 | } |
| 1903 | 1940 | |
| 1904 | 1941 | /// Emits a local variable with the result type and initializes it |
| 1905 | 1942 | /// with the operand. |
| 1906 | | fn airSimpleCast(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1907 | | if (o.liveness.isUnused(inst)) |
| 1943 | fn airSimpleCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1944 | if (f.liveness.isUnused(inst)) |
| 1908 | 1945 | return CValue.none; |
| 1909 | 1946 | |
| 1910 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1911 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1912 | | const ty_op = o.air.instructions.items(.data)[inst].ty_op; |
| 1913 | | const writer = o.writer(); |
| 1914 | | const operand = try o.resolveInst(ty_op.operand); |
| 1947 | const inst_ty = f.air.typeOfIndex(inst); |
| 1948 | const local = try f.allocLocal(inst_ty, .Const); |
| 1949 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 1950 | const writer = f.object.writer(); |
| 1951 | const operand = try f.resolveInst(ty_op.operand); |
| 1915 | 1952 | |
| 1916 | 1953 | try writer.writeAll(" = "); |
| 1917 | | try o.writeCValue(writer, operand); |
| 1954 | try f.writeCValue(writer, operand); |
| 1918 | 1955 | try writer.writeAll(";\n"); |
| 1919 | 1956 | return local; |
| 1920 | 1957 | } |
| 1921 | 1958 | |
| 1922 | | fn airCmpxchg(o: *Object, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue { |
| 1923 | | const ty_pl = o.air.instructions.items(.data)[inst].ty_pl; |
| 1924 | | const extra = o.air.extraData(Air.Cmpxchg, ty_pl.payload).data; |
| 1925 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1926 | | const ptr = try o.resolveInst(extra.ptr); |
| 1927 | | const expected_value = try o.resolveInst(extra.expected_value); |
| 1928 | | const new_value = try o.resolveInst(extra.new_value); |
| 1929 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1930 | | const writer = o.writer(); |
| 1959 | fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue { |
| 1960 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 1961 | const extra = f.air.extraData(Air.Cmpxchg, ty_pl.payload).data; |
| 1962 | const inst_ty = f.air.typeOfIndex(inst); |
| 1963 | const ptr = try f.resolveInst(extra.ptr); |
| 1964 | const expected_value = try f.resolveInst(extra.expected_value); |
| 1965 | const new_value = try f.resolveInst(extra.new_value); |
| 1966 | const local = try f.allocLocal(inst_ty, .Const); |
| 1967 | const writer = f.object.writer(); |
| 1931 | 1968 | |
| 1932 | 1969 | try writer.print(" = zig_cmpxchg_{s}(", .{flavor}); |
| 1933 | | try o.writeCValue(writer, ptr); |
| 1970 | try f.writeCValue(writer, ptr); |
| 1934 | 1971 | try writer.writeAll(", "); |
| 1935 | | try o.writeCValue(writer, expected_value); |
| 1972 | try f.writeCValue(writer, expected_value); |
| 1936 | 1973 | try writer.writeAll(", "); |
| 1937 | | try o.writeCValue(writer, new_value); |
| 1974 | try f.writeCValue(writer, new_value); |
| 1938 | 1975 | try writer.writeAll(", "); |
| 1939 | 1976 | try writeMemoryOrder(writer, extra.successOrder()); |
| 1940 | 1977 | try writer.writeAll(", "); |
| ... | ... | @@ -1944,19 +1981,19 @@ fn airCmpxchg(o: *Object, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue { |
| 1944 | 1981 | return local; |
| 1945 | 1982 | } |
| 1946 | 1983 | |
| 1947 | | fn airAtomicRmw(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1948 | | const pl_op = o.air.instructions.items(.data)[inst].pl_op; |
| 1949 | | const extra = o.air.extraData(Air.AtomicRmw, pl_op.payload).data; |
| 1950 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1951 | | const ptr = try o.resolveInst(pl_op.operand); |
| 1952 | | const operand = try o.resolveInst(extra.operand); |
| 1953 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1954 | | const writer = o.writer(); |
| 1984 | fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 1985 | const pl_op = f.air.instructions.items(.data)[inst].pl_op; |
| 1986 | const extra = f.air.extraData(Air.AtomicRmw, pl_op.payload).data; |
| 1987 | const inst_ty = f.air.typeOfIndex(inst); |
| 1988 | const ptr = try f.resolveInst(pl_op.operand); |
| 1989 | const operand = try f.resolveInst(extra.operand); |
| 1990 | const local = try f.allocLocal(inst_ty, .Const); |
| 1991 | const writer = f.object.writer(); |
| 1955 | 1992 | |
| 1956 | 1993 | try writer.print(" = zig_atomicrmw_{s}(", .{toAtomicRmwSuffix(extra.op())}); |
| 1957 | | try o.writeCValue(writer, ptr); |
| 1994 | try f.writeCValue(writer, ptr); |
| 1958 | 1995 | try writer.writeAll(", "); |
| 1959 | | try o.writeCValue(writer, operand); |
| 1996 | try f.writeCValue(writer, operand); |
| 1960 | 1997 | try writer.writeAll(", "); |
| 1961 | 1998 | try writeMemoryOrder(writer, extra.ordering()); |
| 1962 | 1999 | try writer.writeAll(");\n"); |
| ... | ... | @@ -1964,15 +2001,15 @@ fn airAtomicRmw(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1964 | 2001 | return local; |
| 1965 | 2002 | } |
| 1966 | 2003 | |
| 1967 | | fn airAtomicLoad(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1968 | | const atomic_load = o.air.instructions.items(.data)[inst].atomic_load; |
| 1969 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1970 | | const ptr = try o.resolveInst(atomic_load.ptr); |
| 1971 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1972 | | const writer = o.writer(); |
| 2004 | fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 2005 | const atomic_load = f.air.instructions.items(.data)[inst].atomic_load; |
| 2006 | const inst_ty = f.air.typeOfIndex(inst); |
| 2007 | const ptr = try f.resolveInst(atomic_load.ptr); |
| 2008 | const local = try f.allocLocal(inst_ty, .Const); |
| 2009 | const writer = f.object.writer(); |
| 1973 | 2010 | |
| 1974 | 2011 | try writer.writeAll(" = zig_atomic_load("); |
| 1975 | | try o.writeCValue(writer, ptr); |
| 2012 | try f.writeCValue(writer, ptr); |
| 1976 | 2013 | try writer.writeAll(", "); |
| 1977 | 2014 | try writeMemoryOrder(writer, atomic_load.order); |
| 1978 | 2015 | try writer.writeAll(");\n"); |
| ... | ... | @@ -1980,18 +2017,18 @@ fn airAtomicLoad(o: *Object, inst: Air.Inst.Index) !CValue { |
| 1980 | 2017 | return local; |
| 1981 | 2018 | } |
| 1982 | 2019 | |
| 1983 | | fn airAtomicStore(o: *Object, inst: Air.Inst.Index, order: [*:0]const u8) !CValue { |
| 1984 | | const bin_op = o.air.instructions.items(.data)[inst].bin_op; |
| 1985 | | const ptr = try o.resolveInst(bin_op.lhs); |
| 1986 | | const element = try o.resolveInst(bin_op.rhs); |
| 1987 | | const inst_ty = o.air.typeOfIndex(inst); |
| 1988 | | const local = try o.allocLocal(inst_ty, .Const); |
| 1989 | | const writer = o.writer(); |
| 2020 | fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CValue { |
| 2021 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 2022 | const ptr = try f.resolveInst(bin_op.lhs); |
| 2023 | const element = try f.resolveInst(bin_op.rhs); |
| 2024 | const inst_ty = f.air.typeOfIndex(inst); |
| 2025 | const local = try f.allocLocal(inst_ty, .Const); |
| 2026 | const writer = f.object.writer(); |
| 1990 | 2027 | |
| 1991 | 2028 | try writer.writeAll(" = zig_atomic_store("); |
| 1992 | | try o.writeCValue(writer, ptr); |
| 2029 | try f.writeCValue(writer, ptr); |
| 1993 | 2030 | try writer.writeAll(", "); |
| 1994 | | try o.writeCValue(writer, element); |
| 2031 | try f.writeCValue(writer, element); |
| 1995 | 2032 | try writer.print(", {s});\n", .{order}); |
| 1996 | 2033 | |
| 1997 | 2034 | return local; |